mirror of
https://github.com/colbymchenry/codegraph.git
synced 2026-10-19 06:22:38 +08:00
16e17495f4
`isGeneratedFile` was path-only, but Go's own convention is a CONTENT marker (`// Code generated by <tool>. DO NOT EDIT.`), not a filename one. A Go monorepo with generated CRUD in ordinarily-named files sitting beside hand-written use-cases was therefore invisible to every generated-file down-rank in the codebase — that is #1500. Measured on kubernetes/client-go (2,453 Go files): the canonical banner appears in 2,001 of them, the path check flags 0, the new content check flags exactly those 2,001 — no false positives, no misses. Design: decide at INDEX time (content is already in memory for parsing), persist on `files.generated`, read from the DB. Explore never reads file headers per request. - `hasGeneratedHeader(content)` recognizes the standard banners — Go's, protoc's, `@generated`, `<auto-generated>`, Thrift, OpenAPI Generator, FlatBuffers, bindgen, ANTLR. Precision-first and fenced three ways: an 8KB/60-line header window, a comment-line requirement (leader or open block comment), and markers tight enough that prose can't trip them. A generator's own source, holding the banner as a string constant in its body, is not flagged; neither is this module itself (pinned by test). - `isGeneratedFile(path)` is unchanged — cheap, sync, still the fallback. - Schema v9 adds `files.generated` + a PARTIAL index. DDL only, no backfill: the flag derives from content the migration cannot see, so rows stay 0 until a re-index and every reader unions the flag with the path check — an un-migrated index keeps pre-#1500 behavior rather than regressing. Re-index required; noted in the CHANGELOG. - `generatedPredicateFor(paths)` gives ranking a bounded probe + O(1) lookups. Bounded, not cached: no invalidation, so a ranking call can never serve a verdict the last sync already replaced. Wired into explore ranking, findSymbolMatches, findAllSymbols, search (MCP + CLI), the context formatter, and the dominant-file/route-file hygiene filters. Cost (acceptance bar was no measurable index-time regression): a single unanchored `/generat/i` test over the header rejects ~every hand-written file before any line splitting. 4.6 µs/file on client-go (worst case — 82% generated). End-to-end `codegraph init` on client-go, n=3 alternating arms: 5.73s median with detection vs 5.76s path-only baseline; the arms cross over between runs, so the difference is inside run-to-run noise. Scope note: generated status remains a stable TIEBREAK at equal score, exactly where it was. Making it a strong negative signal is CG-10, which this unblocks by making the signal correct and available. Two pre-existing tests hard-coded schema version 8; both now track CURRENT_SCHEMA_VERSION (or the migration table) so future migrations don't require editing them. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
208 lines
9.2 KiB
SQL
208 lines
9.2 KiB
SQL
-- CodeGraph SQLite Schema
|
|
-- Version 1
|
|
|
|
-- Schema version tracking
|
|
CREATE TABLE IF NOT EXISTS schema_versions (
|
|
version INTEGER PRIMARY KEY,
|
|
applied_at INTEGER NOT NULL,
|
|
description TEXT
|
|
);
|
|
|
|
-- Insert initial version
|
|
INSERT INTO schema_versions (version, applied_at, description)
|
|
VALUES (1, strftime('%s', 'now') * 1000, 'Initial schema');
|
|
|
|
-- =============================================================================
|
|
-- Core Tables
|
|
-- =============================================================================
|
|
|
|
-- Nodes: Code symbols (functions, classes, variables, etc.)
|
|
CREATE TABLE IF NOT EXISTS nodes (
|
|
id TEXT PRIMARY KEY,
|
|
kind TEXT NOT NULL,
|
|
name TEXT NOT NULL,
|
|
qualified_name TEXT NOT NULL,
|
|
file_path TEXT NOT NULL,
|
|
language TEXT NOT NULL,
|
|
start_line INTEGER NOT NULL,
|
|
end_line INTEGER NOT NULL,
|
|
start_column INTEGER NOT NULL,
|
|
end_column INTEGER NOT NULL,
|
|
docstring TEXT,
|
|
signature TEXT,
|
|
visibility TEXT,
|
|
is_exported INTEGER DEFAULT 0,
|
|
is_async INTEGER DEFAULT 0,
|
|
is_static INTEGER DEFAULT 0,
|
|
is_abstract INTEGER DEFAULT 0,
|
|
decorators TEXT, -- JSON array
|
|
type_parameters TEXT, -- JSON array
|
|
return_type TEXT, -- normalized return/result type name (e.g. C++ method return, for receiver-type inference)
|
|
updated_at INTEGER NOT NULL
|
|
);
|
|
|
|
-- Edges: Relationships between nodes
|
|
CREATE TABLE IF NOT EXISTS edges (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
source TEXT NOT NULL,
|
|
target TEXT NOT NULL,
|
|
kind TEXT NOT NULL,
|
|
metadata TEXT, -- JSON object
|
|
line INTEGER,
|
|
col INTEGER,
|
|
provenance TEXT DEFAULT NULL,
|
|
FOREIGN KEY (source) REFERENCES nodes(id) ON DELETE CASCADE,
|
|
FOREIGN KEY (target) REFERENCES nodes(id) ON DELETE CASCADE
|
|
);
|
|
|
|
-- Files: Tracked source files.
|
|
-- `generated` is the index-time verdict from extraction/generated-detection.ts:
|
|
-- the filename convention (*.pb.go, *.g.dart, …) OR a generation banner in the
|
|
-- file's header. Go's convention is a CONTENT marker, so a generated
|
|
-- `payroll.go` beside hand-written use-cases is invisible to the path check
|
|
-- alone (#1500) — deciding it here means ranking never reads file headers per
|
|
-- request. Migration v9 adds the column to existing databases; rows keep the
|
|
-- 0 default until the next full index, so readers treat it as a hint that
|
|
-- only ever ADDS to the path signal, never overrides it.
|
|
CREATE TABLE IF NOT EXISTS files (
|
|
path TEXT PRIMARY KEY,
|
|
content_hash TEXT NOT NULL,
|
|
language TEXT NOT NULL,
|
|
size INTEGER NOT NULL,
|
|
modified_at INTEGER NOT NULL,
|
|
indexed_at INTEGER NOT NULL,
|
|
node_count INTEGER DEFAULT 0,
|
|
errors TEXT, -- JSON array
|
|
generated INTEGER NOT NULL DEFAULT 0
|
|
);
|
|
|
|
-- Unresolved References: References that need resolution after full indexing.
|
|
-- status lifecycle: rows are inserted 'pending' by extraction; a completed
|
|
-- resolution pass either deletes a row (resolved) or marks it 'failed'
|
|
-- (attempted, no match — kept so a later sync can retry it when a changed
|
|
-- file introduces a symbol that could satisfy it, #1240). name_tail is the
|
|
-- last segment of reference_name ('util.greet' → 'greet'), written when a
|
|
-- row is marked failed, so the retry lookup matches new node names against
|
|
-- dotted refs too. Rows follow their from_node via ON DELETE CASCADE, so
|
|
-- re-extracting or deleting a file clears its stale rows in any status.
|
|
CREATE TABLE IF NOT EXISTS unresolved_refs (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
from_node_id TEXT NOT NULL,
|
|
reference_name TEXT NOT NULL,
|
|
reference_kind TEXT NOT NULL,
|
|
line INTEGER NOT NULL,
|
|
col INTEGER NOT NULL,
|
|
candidates TEXT, -- JSON array
|
|
file_path TEXT NOT NULL DEFAULT '',
|
|
language TEXT NOT NULL DEFAULT 'unknown',
|
|
status TEXT NOT NULL DEFAULT 'pending',
|
|
name_tail TEXT NOT NULL DEFAULT '',
|
|
FOREIGN KEY (from_node_id) REFERENCES nodes(id) ON DELETE CASCADE
|
|
);
|
|
|
|
-- =============================================================================
|
|
-- Indexes for Query Performance
|
|
-- =============================================================================
|
|
|
|
-- Node indexes
|
|
CREATE INDEX IF NOT EXISTS idx_nodes_kind ON nodes(kind);
|
|
CREATE INDEX IF NOT EXISTS idx_nodes_name ON nodes(name);
|
|
CREATE INDEX IF NOT EXISTS idx_nodes_qualified_name ON nodes(qualified_name);
|
|
CREATE INDEX IF NOT EXISTS idx_nodes_file_path ON nodes(file_path);
|
|
CREATE INDEX IF NOT EXISTS idx_nodes_language ON nodes(language);
|
|
CREATE INDEX IF NOT EXISTS idx_nodes_file_line ON nodes(file_path, start_line);
|
|
CREATE INDEX IF NOT EXISTS idx_nodes_lower_name ON nodes(lower(name));
|
|
|
|
-- Full-text search index on node names, docstrings, and signatures
|
|
CREATE VIRTUAL TABLE IF NOT EXISTS nodes_fts USING fts5(
|
|
id,
|
|
name,
|
|
qualified_name,
|
|
docstring,
|
|
signature,
|
|
content='nodes',
|
|
content_rowid='rowid'
|
|
);
|
|
|
|
-- Triggers to keep FTS index in sync
|
|
CREATE TRIGGER IF NOT EXISTS nodes_ai AFTER INSERT ON nodes BEGIN
|
|
INSERT INTO nodes_fts(rowid, id, name, qualified_name, docstring, signature)
|
|
VALUES (NEW.rowid, NEW.id, NEW.name, NEW.qualified_name, NEW.docstring, NEW.signature);
|
|
END;
|
|
|
|
CREATE TRIGGER IF NOT EXISTS nodes_ad AFTER DELETE ON nodes BEGIN
|
|
INSERT INTO nodes_fts(nodes_fts, rowid, id, name, qualified_name, docstring, signature)
|
|
VALUES ('delete', OLD.rowid, OLD.id, OLD.name, OLD.qualified_name, OLD.docstring, OLD.signature);
|
|
END;
|
|
|
|
CREATE TRIGGER IF NOT EXISTS nodes_au AFTER UPDATE ON nodes BEGIN
|
|
INSERT INTO nodes_fts(nodes_fts, rowid, id, name, qualified_name, docstring, signature)
|
|
VALUES ('delete', OLD.rowid, OLD.id, OLD.name, OLD.qualified_name, OLD.docstring, OLD.signature);
|
|
INSERT INTO nodes_fts(rowid, id, name, qualified_name, docstring, signature)
|
|
VALUES (NEW.rowid, NEW.id, NEW.name, NEW.qualified_name, NEW.docstring, NEW.signature);
|
|
END;
|
|
|
|
-- Prose-word → symbol-name lookup for the prompt hook's graph-derived gate.
|
|
-- One row per (segment, name): segment is a lowercased word of a symbol name
|
|
-- ("OrderStateMachine" → order, state, machine — see identifier-segments.ts),
|
|
-- which lets natural-language prompt words be verified against the graph in
|
|
-- any language whose technical nouns are Latin script. File nodes are
|
|
-- excluded — a file's basename duplicates the symbols inside it and skews the
|
|
-- singleton-vs-cluster rarity statistics. FTS can't serve this lookup (its
|
|
-- tokenizer keeps camelCase names as single tokens), so segments are
|
|
-- materialized on the node write path.
|
|
-- Deletions leave orphan rows ON PURPOSE: rows are PROPOSALS, always
|
|
-- re-verified against nodes before being surfaced (CodeGraph.getSegmentMatches),
|
|
-- and a full index clears the table at its start. Populated lazily on old
|
|
-- databases (empty until the next index/sync heals it).
|
|
CREATE TABLE IF NOT EXISTS name_segment_vocab (
|
|
segment TEXT NOT NULL,
|
|
name TEXT NOT NULL,
|
|
PRIMARY KEY (segment, name)
|
|
) WITHOUT ROWID;
|
|
|
|
-- Edge indexes.
|
|
-- idx_edges_source / idx_edges_target are intentionally omitted —
|
|
-- the (source, kind) and (target, kind) composites below cover the
|
|
-- corresponding source-only / target-only lookups via SQLite's
|
|
-- left-prefix scan, so the narrow indexes are dead weight on writes.
|
|
-- Migration v4 drops them on existing databases.
|
|
CREATE INDEX IF NOT EXISTS idx_edges_kind ON edges(kind);
|
|
CREATE INDEX IF NOT EXISTS idx_edges_source_kind ON edges(source, kind);
|
|
CREATE INDEX IF NOT EXISTS idx_edges_target_kind ON edges(target, kind);
|
|
|
|
-- Edge identity uniqueness. An edge IS uniquely (source, target, kind, line,
|
|
-- col); insertEdge uses `INSERT OR IGNORE`, but without something UNIQUE to
|
|
-- conflict on it behaved like a plain INSERT, so two passes emitting the same
|
|
-- edge produced byte-identical duplicate rows that inflated counts and flowed
|
|
-- into callers/impact (#1034). IFNULL folds the nullable line/col so
|
|
-- coordinate-less edges (synthesized / file-level) dedup too — SQLite treats
|
|
-- each NULL as distinct otherwise. Migration v6 dedups existing rows + adds
|
|
-- this on older databases.
|
|
CREATE UNIQUE INDEX IF NOT EXISTS idx_edges_identity
|
|
ON edges(source, target, kind, IFNULL(line, -1), IFNULL(col, -1));
|
|
|
|
-- File indexes.
|
|
-- idx_files_generated is PARTIAL: the generated set is a small minority of any
|
|
-- repo, so a lookup that intersects a bounded candidate list with it stays
|
|
-- proportional to the generated files, not to the repo.
|
|
CREATE INDEX IF NOT EXISTS idx_files_language ON files(language);
|
|
CREATE INDEX IF NOT EXISTS idx_files_modified_at ON files(modified_at);
|
|
CREATE INDEX IF NOT EXISTS idx_files_generated ON files(path) WHERE generated = 1;
|
|
|
|
-- Unresolved refs indexes
|
|
CREATE INDEX IF NOT EXISTS idx_unresolved_from_node ON unresolved_refs(from_node_id);
|
|
CREATE INDEX IF NOT EXISTS idx_unresolved_name ON unresolved_refs(reference_name);
|
|
CREATE INDEX IF NOT EXISTS idx_unresolved_file_path ON unresolved_refs(file_path);
|
|
CREATE INDEX IF NOT EXISTS idx_unresolved_from_name ON unresolved_refs(from_node_id, reference_name);
|
|
CREATE INDEX IF NOT EXISTS idx_unresolved_status ON unresolved_refs(status);
|
|
CREATE INDEX IF NOT EXISTS idx_unresolved_failed_tail ON unresolved_refs(name_tail) WHERE status = 'failed';
|
|
CREATE INDEX IF NOT EXISTS idx_edges_provenance ON edges(provenance);
|
|
|
|
-- Project metadata for version/provenance tracking
|
|
CREATE TABLE IF NOT EXISTS project_metadata (
|
|
key TEXT PRIMARY KEY,
|
|
value TEXT NOT NULL,
|
|
updated_at INTEGER NOT NULL
|
|
);
|