Turbocharge Claude Code, Cursor, Codex, Gemini & every coding agent: faster, cheaper, with contextual understanding specific to your codebase.
TypeScript ai-agentsanthropicclaude-codeclicode-graphcodexcontext-engineeringcursordeveloper-toolsgeminiknowledge-graphllmmcpmcp-serveropen-sourceopenaitree-sitter
feat(graph): emit resolved reference edges from the breadth tier
Shrish Dwivedi84d39afbe95f
diff --git a/src/graph/generic.ts b/src/graph/generic.tsindex cb3ea741..e5857af6 100644--- a/src/graph/generic.ts+++ b/src/graph/generic.ts@@ -235,6 +235,7 @@ function tagsExtract(// producing bogus self-loops (foo→foo). Skip any call at a definition's name token.const defNameAt = new Set<number>();const calls: Array<{ name: string; at: number }> = [];+ const refs: Array<{ name: string; at: number }> = [];for (const m of matches) {const cap: Record<string, TsNode> = {};for (const c of m.captures) cap[c.name] = c.node;@@ -245,14 +246,33 @@ function tagsExtract(}if (("reference.call" in cap || "reference.send" in cap) && cap.name)calls.push({ name: cap.name.text, at: cap.name.startIndex });+ // Structural references the grammar already marks: a supertype (extends), an+ // implemented interface, an object creation (`new Foo`), a module alias. Grammars+ // label these @reference.class/.interface/.implementation/.module — heterogeneous+ // syntactically but all "names this symbol without calling it". They become+ // `references` edges the same precision-first resolver settles to a type-like def+ // (same-file certain, unique cross-file inferred, ambiguous dropped), so a data+ // class that's only ever extended or instantiated stops being an orphan.+ if (("reference.class" in cap || "reference.interface" in cap ||+ "reference.implementation" in cap || "reference.module" in cap) && cap.name)+ refs.push({ name: cap.name.text, at: cap.name.startIndex });}+ // innermost enclosing definition of a token at byte offset `at`+ const enclosing = (at: number) =>+ defs+ .filter((d) => d.startIndex <= at && at < d.endIndex)+ .sort((a, b) => (a.endIndex - a.startIndex) - (b.endIndex - b.startIndex))[0];for (const c of calls) {if (defNameAt.has(c.at)) continue;- const enc = defs- .filter((d) => d.startIndex <= c.at && c.at < d.endIndex)- .sort((a, b) => (a.endIndex - a.startIndex) - (b.endIndex - b.startIndex))[0];+ const enc = enclosing(c.at);rawEdges.push({ source: enc ? enc.id : rel, relation: "calls", file: rel, name: c.name });}+ for (const r of refs) {+ if (defNameAt.has(r.at)) continue;+ const enc = enclosing(r.at);+ if (!enc) continue; // a reference with no enclosing definition has no sound source+ rawEdges.push({ source: enc.id, relation: "references", file: rel, name: r.name });+ }}// Node-type → Kind for the walker fallback. A node is a definition when its typediff --git a/src/graph/resolve.ts b/src/graph/resolve.tsindex 32048071..5c08de89 100644--- a/src/graph/resolve.ts+++ b/src/graph/resolve.ts@@ -102,14 +102,25 @@ export function resolveEdges(const hit = resolveName(e.name!, e.file, kinds, perFileName, globalName);// an unresolved base is usually an external/imported type — keep the name.add(e.source, hit?.id ?? e.name!, e.relation, hit?.confidence ?? "inferred");- } else if (e.relation === "references" && e.name && e.specifier) {- // A named import gives both halves needed for sound resolution: the module- // it came from and the exported name. Resolve inside that file only, so a- // same-named symbol elsewhere in the repo cannot become a false edge.- const targetFile = resolveImport(e.specifier, e.file, byId);- if (!byId.has(targetFile)) continue; // external or unresolved module- const candidates = perFileName.get(targetFile)?.get(e.name) ?? [];- if (candidates.length === 1) add(e.source, candidates[0].id, "references", "extracted");+ } else if (e.relation === "references" && e.name) {+ if (e.specifier) {+ // A named import gives both halves needed for sound resolution: the module+ // it came from and the exported name. Resolve inside that file only, so a+ // same-named symbol elsewhere in the repo cannot become a false edge.+ const targetFile = resolveImport(e.specifier, e.file, byId);+ if (!byId.has(targetFile)) continue; // external or unresolved module+ const candidates = perFileName.get(targetFile)?.get(e.name) ?? [];+ if (candidates.length === 1) add(e.source, candidates[0].id, "references", "extracted");+ } else if (byId.get(e.source)?.origin === "generic") {+ // Breadth tier: a bare-name structural reference (extends / implements /+ // object-creation / module alias) the grammar marked but cannot type. Resolve+ // to a type-like definition, drop-rather-than-guess, never a self-loop. Gated on+ // generic origin so depth-tier references (which always carry a specifier) are+ // provably untouched.+ const refKinds: Kind[] = ["class", "interface", "struct", "enum", "type", "module"];+ const hit = resolveName(e.name, e.file, refKinds, perFileName, globalName);+ if (hit && hit.id !== e.source) add(e.source, hit.id, "references", hit.confidence);+ }} else if (e.relation === "calls") {if (e.viaMember) {if (!e.recvType) continue;diff --git a/test/generic-extract.test.ts b/test/generic-extract.test.tsindex 82edb9f8..02bb4043 100644--- a/test/generic-extract.test.ts+++ b/test/generic-extract.test.ts@@ -115,6 +115,36 @@ for (const s of SNIPPETS) {});}+// Structural references the grammar already marks — a supertype (extends), an+// implemented interface, an object creation — become `references` edges the resolver+// settles to a type-like definition. This is the breadth-tier orphan-rate fix: a class+// that is only ever extended or instantiated (never "called") stops being disconnected.+test("breadth tier: Java extends/implements/new become resolved references edges (precision-safe)", async () => {+ await warmGenericGrammars(["java"]);+ const src =+ `interface Animal {}\n` ++ `class Base {}\n` ++ `class Dog extends Base implements Animal {\n` ++ ` Base make() { return new Base(); }\n` ++ `}\n` ++ `class Cat extends UnknownExternal {}\n`; // external supertype → must be dropped, never guessed+ const { nodes, rawEdges } = extractGeneric("A.java", src, "java");+ const edges = resolveEdges(nodes, rawEdges);+ const refs = edges.filter((e) => e.relation === "references");+ const pairs = refs.map((e) => `${e.source.split("#")[1]}→${e.target.split("#")[1] ?? e.target}`);++ // extends + implements are attributed to the declaring class; `new Base()` to the method+ assert.ok(pairs.includes("Dog→Base"), `Dog extends Base (got ${pairs.join(", ")})`);+ assert.ok(pairs.includes("Dog→Animal"), "Dog implements Animal");+ assert.ok(pairs.includes("make→Base"), "make() references Base via `new Base()`");+ // same-file targets resolve as certain+ assert.ok(refs.every((e) => e.confidence === "extracted"), "same-file references are extracted");+ // an external/undefined supertype is dropped, not emitted as a bogus edge+ assert.ok(!refs.some((e) => e.target.includes("UnknownExternal")), "unresolved external type is dropped");+ // no self-loops (a class referencing its own name never becomes X→X)+ assert.ok(!refs.some((e) => e.source === e.target), "no self-loop references");+});+test("buildGraph + checkGraph handle a breadth-tier (.rs) repo end-to-end", async () => {const dir = mkdtempSync(join(tmpdir(), "graft-rust-"));mkdirSync(join(dir, "src"), { recursive: true });