Skip to content

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

fix(claude): recover graft check stdout on non-zero exit (real stale count) + harden path match

Shrish Dwivedi605fd7bb71bf
diff --git a/src/claude/format.ts b/src/claude/format.ts
index 4d58e20d..fb4db79a 100644
--- a/src/claude/format.ts
+++ b/src/claude/format.ts
@@ -48,7 +48,7 @@ export function renderStatusline(
function nodeIdsInFile(w: GraphV1, filePath: string): Set<string> {
const nodes = w.nodes ?? [];
return new Set(
- nodes.filter((n) => n.path && (filePath === n.path || filePath.endsWith(`/${n.path}`) || filePath.endsWith(n.path)))
+ nodes.filter((n) => n.path && (filePath === n.path || filePath.endsWith(`/${n.path}`)))
.map((n) => n.id),
);
}
diff --git a/src/claude/hooks.ts b/src/claude/hooks.ts
index 3b790375..ab800382 100644
--- a/src/claude/hooks.ts
+++ b/src/claude/hooks.ts
@@ -24,7 +24,14 @@ function graftJson(dir: string, args: string[]): any | null {
const out = execFileSync(process.execPath, [join(dir, 'dist', 'cli.js'), ...args],
{ cwd: dir, encoding: 'utf8', timeout: 8000, stdio: ['ignore', 'pipe', 'ignore'] });
return JSON.parse(out);
- } catch { return null; }
+ } catch (e: any) {
+ // `graft check` exits non-zero when the graph is stale (by design) but still
+ // prints valid JSON to stdout; recover it from the thrown error before giving up.
+ if (e && typeof e.stdout === 'string' && e.stdout.trim()) {
+ try { return JSON.parse(e.stdout); } catch { /* not JSON — fall through */ }
+ }
+ return null;
+ }
}
function checkStaleCount(dir: string): number {
const r = graftJson(dir, ['check', '.', '--json']);
diff --git a/src/claude/state.ts b/src/claude/state.ts
index e20b9933..41374554 100644
--- a/src/claude/state.ts
+++ b/src/claude/state.ts
@@ -40,6 +40,8 @@ function writeJsonAtomic(p: string, value: unknown): void {
export function readStats(d: string): Stats | null { return readJson<Stats>(statsPath(d)); }
export function writeStats(d: string, s: Stats): void { writeJsonAtomic(statsPath(d), s); }
+// Best-effort read-modify-write; not atomic across concurrent processes, but acceptable
+// for episodic hook writes (worst case is a lost update, not corruption).
export function patchStats(d: string, patch: Partial<Stats>): Stats {
const next: Stats = { ...(readStats(d) ?? emptyStats()), ...patch };
writeStats(d, next);