Skip to content

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

feat(claude): turn-end auto-sync — detached $0 rebuild under lock (never --deep)

Shrish Dwivedi6eb5d3fab894
diff --git a/.claude/settings.json b/.claude/settings.json
index 30d18a2d..f5a74e26 100644
--- a/.claude/settings.json
+++ b/.claude/settings.json
@@ -11,6 +11,13 @@
{ "type": "command", "command": "node \"${CLAUDE_PROJECT_DIR:-.}/.claude/helpers/graft-hooks.cjs\" post-edit", "timeout": 10000 }
]
}
+ ],
+ "Stop": [
+ {
+ "hooks": [
+ { "type": "command", "command": "node \"${CLAUDE_PROJECT_DIR:-.}/.claude/helpers/graft-hooks.cjs\" stop", "timeout": 8000 }
+ ]
+ }
]
}
}
diff --git a/src/claude/sync-run.ts b/src/claude/sync-run.ts
new file mode 100644
index 00000000..5e57dfcc
--- /dev/null
+++ b/src/claude/sync-run.ts
@@ -0,0 +1,30 @@
+import { execFileSync } from 'node:child_process';
+import { join } from 'node:path';
+import { readWiring, computeStats } from './stats.js';
+import { patchStats, releaseLock } from './state.js';
+
+/** MONEY GUARD: plain `graft build` only — structural, $0, offline. Never --deep. */
+function realBuild(dir: string): void {
+ execFileSync(process.execPath, [join(dir, 'dist', 'cli.js'), 'build', '.'],
+ { cwd: dir, stdio: 'ignore', timeout: 120000 });
+}
+
+export function runSync(dir: string, build: (d: string) => void = realBuild): void {
+ try {
+ build(dir);
+ const w = readWiring(dir);
+ const patch: Record<string, unknown> = { dirty: false, staleCount: 0, syncing: false, syncedAt: new Date().toISOString() };
+ if (w) Object.assign(patch, computeStats(w));
+ patchStats(dir, patch);
+ } catch {
+ patchStats(dir, { syncing: false }); // leave dirty=true; retry next turn
+ } finally {
+ releaseLock(dir);
+ }
+}
+
+export function main(): void {
+ const dir = process.argv[2];
+ if (dir) runSync(dir);
+}
+main();
diff --git a/test/claude-hooks.test.ts b/test/claude-hooks.test.ts
index 2678298b..9baf4589 100644
--- a/test/claude-hooks.test.ts
+++ b/test/claude-hooks.test.ts
@@ -5,6 +5,8 @@ import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { underGraft, main } from '../src/claude/hooks.js';
import { readStats } from '../src/claude/state.js';
+import { runSync } from '../src/claude/sync-run.js';
+import { writeStats, emptyStats, acquireLock } from '../src/claude/state.js';
test('underGraft detects edits inside graft/', () => {
assert.equal(underGraft('/repo', '/repo/graft/x.md'), true);
@@ -37,3 +39,35 @@ async function runWithStdin(text: string, fn: () => Promise<void>): Promise<void
process.env.GRAFT_TEST_STDIN = text;
try { await fn(); } finally { delete process.env.GRAFT_TEST_STDIN; }
}
+
+test('runSync clears dirty/syncing, recomputes stats, releases lock', () => {
+ const d = mkdtempSync(join(tmpdir(), 'graft-sync-'));
+ mkdirSync(join(d, 'graft', '.graph'), { recursive: true });
+ writeStats(d, { ...emptyStats(), dirty: true, syncing: true, staleCount: 3 });
+ acquireLock(d);
+ // fake build: write a fresh wiring.json with 2 nodes, 1 ready
+ const fakeBuild = (dir: string) => writeFileSync(join(dir, 'graft', '.graph', 'wiring.json'),
+ JSON.stringify({ meta: { nodeCount: 2, edgeCount: 1, languages: ['typescript'] },
+ nodes: [{ id: 'a', summary_state: 'ready' }, { id: 'b', summary_state: 'pending' }],
+ edges: [{ from: 'a', to: 'b' }] }));
+ runSync(d, fakeBuild);
+ const s = readStats(d)!;
+ assert.equal(s.dirty, false);
+ assert.equal(s.syncing, false);
+ assert.equal(s.staleCount, 0);
+ assert.equal(s.nodeCount, 2);
+ assert.equal(s.readyCount, 1);
+ assert.ok(s.syncedAt);
+ assert.equal(acquireLock(d), true, 'lock released, so reacquire succeeds');
+});
+
+test('runSync clears syncing even if build throws (money-safe failure)', () => {
+ const d = mkdtempSync(join(tmpdir(), 'graft-sync-'));
+ writeStats(d, { ...emptyStats(), dirty: true, syncing: true });
+ acquireLock(d);
+ runSync(d, () => { throw new Error('build failed'); });
+ const s = readStats(d)!;
+ assert.equal(s.syncing, false);
+ assert.equal(s.dirty, true, 'stays dirty so the bar keeps ⚠ and it retries next turn');
+ assert.equal(acquireLock(d), true, 'lock always released');
+});