Skip to content

Turbocharge Claude Code, Cursor, Codex, Gemini & every coding agent: faster, cheaper, with contextual understanding specific to your codebase.

fix(graph): skip unchanged card writes (#472)

ANIRUDDHA ADAKacbec101234c
diff --git a/src/graph/cards.ts b/src/graph/cards.ts
index 7d5cbb9d..836ef5e6 100644
--- a/src/graph/cards.ts
+++ b/src/graph/cards.ts
@@ -28,6 +28,12 @@ const INDEX_FILE = "INDEX.md";
* node (same stem as a slug — Laravel `server.php` vs a "Server" concept). */
const ROOT_CARD_DIR = "_root";
+function writeIfChanged(path: string, content: string): void {
+ if (existsSync(path) && readFileSync(path, "utf8") === content) return;
+ mkdirSync(dirname(path), { recursive: true });
+ writeFileSync(path, content);
+}
+
export interface CardFileInfo {
/** Card path relative to the context dir, e.g. "src/ai/providers.md". */
card: string;
@@ -185,8 +191,7 @@ export function writeCards(graph: GraphV1, outDir: string): CardStats {
const fileNode = group.find((n) => n.kind === "file");
const symbols = group.filter((n) => n.kind !== "file");
const cardPath = cardPathFor(outDir, sourcePath);
- mkdirSync(dirname(cardPath), { recursive: true });
- writeFileSync(cardPath, renderCard(sourcePath, fileNode, symbols, concepts.get(sourcePath) ?? []));
+ writeIfChanged(cardPath, renderCard(sourcePath, fileNode, symbols, concepts.get(sourcePath) ?? []));
written.add(cardPath);
files.push({ card: relPosix(outDir, cardPath), path: sourcePath, symbols: symbols.length });
}
@@ -249,7 +254,7 @@ export function writeIndex(outDir: string, files: CardFileInfo[]): void {
);
}
- writeFileSync(join(outDir, INDEX_FILE), lines.join("\n"));
+ writeIfChanged(join(outDir, INDEX_FILE), lines.join("\n"));
}
/** One symbol a concept node covers: its name, kind, and `path:span` pointer. */
@@ -309,7 +314,7 @@ export function writeCovers(graph: GraphV1, outDir: string): number {
// Re-stringify with covers appended last, so re-runs produce a stable diff.
const { covers: _prev, ...rest } = parsed.data as Record<string, unknown>;
- writeFileSync(full, matter.stringify(parsed.content, { ...rest, covers }));
+ writeIfChanged(full, matter.stringify(parsed.content, { ...rest, covers }));
enriched++;
}
return enriched;
diff --git a/test/cards.test.ts b/test/cards.test.ts
new file mode 100644
index 00000000..4133a3fa
--- /dev/null
+++ b/test/cards.test.ts
@@ -0,0 +1,47 @@
+import { test } from "node:test";
+import assert from "node:assert/strict";
+import { mkdtempSync, rmSync, statSync, utimesSync, writeFileSync } from "node:fs";
+import { tmpdir } from "node:os";
+import { join } from "node:path";
+import matter from "gray-matter";
+import { writeCards, writeCovers, writeIndex } from "../src/graph/cards.js";
+import type { GraphV1, NodeV1 } from "../src/graph/types.js";
+
+const node = (overrides: Partial<NodeV1>): NodeV1 => ({
+ id: "src/a.ts#run", name: "run", kind: "function", path: "src/a.ts", span: "L1-L2",
+ signature: "run(): void", exported: true, origin: "ast", body_hash: "symbol",
+ summary_state: "pending", summary: null, crux: null, ...overrides,
+});
+
+test("unchanged cards, index, and covers keep their modification times", () => {
+ const out = mkdtempSync(join(tmpdir(), "graft-cards-"));
+ try {
+ const concept = join(out, "example.md");
+ writeFileSync(concept, matter.stringify("# Example\n", {
+ slug: "example", name: "Example", sources: [{ path: "src/a.ts", hash: "source" }],
+ }));
+ const graph: GraphV1 = {
+ meta: { version: 1, nodeCount: 2, edgeCount: 0, languages: ["typescript"] },
+ nodes: [node({ id: "src/a.ts", name: "a.ts", kind: "file", signature: null }), node()], edges: [],
+ };
+
+ const first = writeCards(graph, out);
+ writeIndex(out, first.files);
+ assert.equal(writeCovers(graph, out), 1);
+ const paths = [join(out, "src", "a.md"), join(out, "INDEX.md"), concept];
+ const old = new Date("2001-01-01T00:00:00.000Z");
+ const mtimes = new Map<string, number>();
+ for (const path of paths) {
+ utimesSync(path, old, old);
+ mtimes.set(path, statSync(path).mtimeMs);
+ }
+
+ const second = writeCards(graph, out);
+ writeIndex(out, second.files);
+ assert.equal(writeCovers(graph, out), 1);
+ assert.equal(second.written, 1);
+ for (const path of paths) assert.equal(statSync(path).mtimeMs, mtimes.get(path));
+ } finally {
+ rmSync(out, { recursive: true, force: true });
+ }
+});