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
test: specify mint-time unique node ids across TS/Python/Go, plus the mintId helper contract
Alex Matthews431548ef5ecd
diff --git a/test/ask-index.test.ts b/test/ask-index.test.tsindex 234b6b63..a1be9fa5 100644--- a/test/ask-index.test.ts+++ b/test/ask-index.test.ts@@ -221,6 +221,36 @@ test("an unparseable sidecar file falls back to live tokenization", async () =>}});+test("A3: a duplicate-named definition still gets its own ask-index doc (unique ids -> docs.length === nodes.length)", async () => {+ const dir = mkdtempSync(join(tmpdir(), "graft-ask-index-dup-"));+ try {+ writeFileSync(+ join(dir, "dup.ts"),+ `export function helper(): void {\n console.log("first");\n}\n\n` ++ `export function helper(): void {\n console.log("second");\n}\n`,+ );+ await buildGraph(dir);+ const outDir = contextDirFor(dir);+ const graph = readGraph(wiringPath(outDir));+ assert.ok(graph, "wiring graph should exist after build");+ const ids = graph!.nodes.map((n) => n.id);+ assert.equal(new Set(ids).size, ids.length, "sanity: node ids are unique");+ assert.ok(+ ids.includes("dup.ts#helper") && ids.includes("dup.ts#helper~2"),+ "sanity: the dup actually minted an ordinal id",+ );++ const index = readAskIndex(outDir);+ assert.ok(index, "sidecar should exist after build");+ assert.equal(index!.docCount, graph!.nodes.length);+ assert.equal(index!.docs.length, graph!.nodes.length);+ const docIds = new Set(index!.docs.map((d) => d.id));+ for (const id of ids) assert.ok(docIds.has(id), `sidecar missing doc for ${id}`);+ } finally {+ rmSync(dir, { recursive: true, force: true });+ }+});+test("readAskIndex returns null when the sidecar is simply missing", () => {const dir = mkdtempSync(join(tmpdir(), "graft-ask-index-missing-"));try {diff --git a/test/graph-extract-dedup.test.ts b/test/graph-extract-dedup.test.tsnew file mode 100644index 00000000..96e8d5a1--- /dev/null+++ b/test/graph-extract-dedup.test.ts@@ -0,0 +1,114 @@+/**+ * A3 — mint-time unique node ids.+ *+ * extract.ts used to mint a node's id purely from its scope + own name, so two+ * definitions that happen to share a name (a branch-guarded redeclaration, a+ * duplicated class, ...) collided onto the SAME id — the second definition's+ * node silently overwrote the first's in every id-keyed lookup.+ *+ * `walk()` now mints ids against a per-file `minted` Set via the extracted+ * `mintId(base, minted)` helper: the first occurrence keeps its bare id, and+ * every later document-order duplicate gets the next free `~2`, `~3`, ...+ * suffix — collision-proof even against a source name that itself ends in+ * `~N` (which would collide with a single-guess `~2` suffix), because the+ * loop keeps incrementing until it finds a truly free id rather than trusting+ * one guessed candidate.+ *+ * These tests assert the ACTUAL minted ids (not a guessed shape) — the hard+ * invariants are: every id unique, the first occurrence keeps the bare id, and+ * ordinals follow document order.+ */+import { test } from "node:test";+import assert from "node:assert/strict";+import { extractFile, mintId } from "../src/graph/extract.js";++function assertUniqueIds(nodes: { id: string }[], label: string): void {+ const ids = nodes.map((n) => n.id);+ assert.equal(new Set(ids).size, ids.length, `${label}: every node id must be unique, got ${JSON.stringify(ids)}`);+}++test("A3 mintId: the while-loop finds the next truly free ordinal, not a single-guess ~2", () => {+ // Pre-seeding both the bare id AND its ~2 ordinal simulates two prior mints+ // (occurrences 1 and 2) already having claimed them; a THIRD occurrence of+ // the same base must skip the taken ~2 and land on ~3 — proving the loop+ // (not a single `~2` guess) is what finds a truly free id.+ const base = "dup.ts#helper";+ const minted = new Set<string>([base, `${base}~2`]);+ const id = mintId(base, minted);+ assert.equal(id, `${base}~3`, "the third occurrence must land on ~3, skipping the already-taken ~2");+ assert.ok(minted.has(`${base}~3`));+});++test("A3 mintId: an unclaimed base id is returned bare (no suffix) and gets added to the set", () => {+ const minted = new Set<string>();+ const id = mintId("solo.ts#f", minted);+ assert.equal(id, "solo.ts#f");+ assert.ok(minted.has("solo.ts#f"));+});++test("A3 ts: branch-guarded duplicate function + duplicate class w/ method mint distinct, unique ids", () => {+ const src = `+if (true) {+ function f() { return 1; }+} else {+ function f() { return 2; }+}++class C {+ m(): void {}+}+class C {+ m(): void {}+}+`;+ const { nodes } = extractFile("mix.ts", src, "typescript");+ assertUniqueIds(nodes, "ts dup fn/class");++ const ids = nodes.map((n) => n.id);+ // Document order: first `f` bare, second gets ~2; same for the two `C` classes.+ assert.deepEqual(+ ids,+ ["mix.ts", "mix.ts#f", "mix.ts#f~2", "mix.ts#C", "mix.ts#C.m", "mix.ts#C~2", "mix.ts#C.m~2"],+ );+});++test("A3 py: duplicate def + duplicate class/method mint distinct, unique ids", () => {+ const src = `+def f():+ return 1++def f():+ return 2++class C:+ def m(self):+ pass++class C:+ def m(self):+ pass+`;+ const { nodes } = extractFile("dup.py", src, "python");+ assertUniqueIds(nodes, "py dup def/class");++ const ids = nodes.map((n) => n.id);+ assert.deepEqual(+ ids,+ ["dup.py", "dup.py#f", "dup.py#f~2", "dup.py#C", "dup.py#C.m", "dup.py#C~2", "dup.py#C.m~2"],+ );+});++test("A3 go: two same-name methods on one receiver mint distinct, unique ids", () => {+ const src = `package pkg++type Worker struct{}++func (w *Worker) Run() {}+func (w *Worker) Run() {}+`;+ const { nodes } = extractFile("dup.go", src, "go");+ assertUniqueIds(nodes, "go dup receiver method");++ const ids = nodes.map((n) => n.id);+ assert.deepEqual(ids, ["dup.go", "dup.go#Worker", "dup.go#Worker.Run", "dup.go#Worker.Run~2"]);+});diff --git a/test/graph-incremental.test.ts b/test/graph-incremental.test.tsindex bb38347f..1b63285e 100644--- a/test/graph-incremental.test.ts+++ b/test/graph-incremental.test.ts@@ -49,6 +49,25 @@ test("an incremental rebuild writes byte-identical wiring.json to a cold one", aassert.equal(wiringOf(d), cold, "replaying cached parses must not change a single byte of the graph");});+test("A3: an incremental rebuild is still byte-identical to a cold one on a duplicate-name-bearing fixture", async () => {+ const d = mkdtempSync(join(tmpdir(), "graft-incr-dup-"));+ mkdirSync(join(d, "src"), { recursive: true });+ writeFileSync(join(d, "src", "dup.ts"), "export function helper(): void {}\nexport function helper(): void {}\n");++ await buildGraph(d, { reuse: false });+ const cold = wiringOf(d);+ await buildGraph(d);+ assert.equal(wiringOf(d), cold, "replaying cached parses must not change a single byte, even with minted ordinal ids");++ const g = readGraph(wiringPath(outOf(d))) as GraphV1;+ const ids = g.nodes.map((n) => n.id);+ assert.equal(new Set(ids).size, ids.length, "ids stay unique across cold/incremental");+ assert.ok(+ ids.includes("src/dup.ts#helper") && ids.includes("src/dup.ts#helper~2"),+ "sanity: the dup actually minted an ordinal id",+ );+});+test("unchanged files are replayed, not re-parsed", async () => {const d = repo();const first = await buildGraph(d);diff --git a/test/graph-write.test.ts b/test/graph-write.test.tsindex 5a7eaf65..55ccbfa9 100644--- a/test/graph-write.test.ts+++ b/test/graph-write.test.ts@@ -121,6 +121,36 @@ test("buildGraph: the serialized wiring.json has no body_text key on ANY node",for (const n of parsed.nodes) {assert.ok(!("body_text" in n), `node ${n.id} must not carry body_text in the serialized graph`);}+ assertUniqueIds(parsed.nodes);+ } finally {+ rmSync(dir, { recursive: true, force: true });+ }+});++// ── A3 PERMANENT gate: every graph this file builds must have unique node ids ──++/** Mint-time id collisions used to be silent — a duplicate definition's node+ * simply overwrote the first's in every id-keyed lookup. This is checked+ * against every fixture this file builds, as a standing regression gate. */+function assertUniqueIds(nodes: NodeV1[]): void {+ const ids = nodes.map((n) => n.id);+ assert.equal(new Set(ids).size, ids.length, `node ids must be unique, got ${JSON.stringify(ids)}`);+}++test("A3 PERMANENT gate: duplicate-named definitions still produce unique node ids in the written wiring.json", async () => {+ const dir = mkdtempSync(join(tmpdir(), "graft-write-dup-"));+ try {+ writeFileSync(+ join(dir, "dup.ts"),+ "export function helper(): void {}\n\nexport function helper(): void {}\n",+ );+ await buildGraph(dir);+ const outDir = contextDirFor(dir);+ const graph = readGraph(wiringPath(outDir));+ assert.ok(graph);+ assertUniqueIds(graph!.nodes);+ const ids = graph!.nodes.map((n) => n.id);+ assert.ok(ids.includes("dup.ts#helper") && ids.includes("dup.ts#helper~2"), "sanity: the dup actually minted an ordinal id");} finally {rmSync(dir, { recursive: true, force: true });}diff --git a/test/search-grep.test.ts b/test/search-grep.test.tsindex 16cfe766..f035cc91 100644--- a/test/search-grep.test.ts+++ b/test/search-grep.test.ts@@ -161,6 +161,30 @@ test('grepGraph: a hit outside every symbol span groups as file-level (symbol: nassert.match(moduleLevel!.hits[0].text, /NEEDLE at module level/);});+test('A3: a duplicate-named definition displays its minted ordinal in the grouped symbol name', () => {+ const d = mkdtempSync(join(tmpdir(), 'graft-grep-dup-'));+ mkdirSync(join(d, 'src'), { recursive: true });+ writeFileSync(+ join(d, 'src', 'dup.ts'),+ [+ 'export function helper(): void {',+ ' console.log("NEEDLE first");',+ '}',+ '',+ 'export function helper(): void {',+ ' console.log("NEEDLE second");',+ '}',+ '',+ ].join('\n'),+ );+ execFileSync(process.execPath, ['--import', 'tsx', 'src/cli.ts', 'build', d], { stdio: 'pipe' });+ const graph = loadBuiltGraph(d);+ const r = grepGraph(graph, d, 'NEEDLE');++ const names = r.groups.map((g) => g.symbol?.name).sort();+ assert.deepEqual(names, ['helper', 'helper~2'], 'the second definition displays its minted ordinal, not a bare duplicate name');+});+test('grepGraph: `in` filter narrows to matching file paths only', () => {const repo = needleRepo();const graph = loadBuiltGraph(repo);