Skip to content

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

feat(hooks): post-edit-sync event — post-edit + background sync in one call

Shrish Dwivedifbb8daace2fe
diff --git a/src/claude/hooks.ts b/src/claude/hooks.ts
index 55bfeb85..c0e057eb 100644
--- a/src/claude/hooks.ts
+++ b/src/claude/hooks.ts
@@ -43,6 +43,28 @@ function emit(eventName: string, additionalContext: string): void {
process.stdout.write(JSON.stringify({ hookSpecificOutput: { hookEventName: eventName, additionalContext } }));
}
+async function handlePostEdit(input: any, dir: string): Promise<void> {
+ const file: string | undefined = input?.tool_input?.file_path;
+ if (!file || underGraft(dir, file)) return;
+ patchStats(dir, { dirty: true, staleCount: checkStaleCount(dir), lastFile: basename(file) });
+ const w = readWiring(dir);
+ if (w) { const br = formatBlastRadius(w, file); if (br) emit('PostToolUse', br); }
+}
+
+function handleStop(dir: string): void {
+ // sync-run.js ships next to this module inside the package, so it resolves in
+ // any repo that installs graft (not just graft's own). Defensive existsSync:
+ // if the package is somehow incomplete, skip rather than wedge on syncing:true.
+ const syncRun = claudeScriptPath('sync-run.js');
+ if (!existsSync(syncRun)) return;
+ const stats = readStats(dir);
+ if (stats?.dirty && acquireLock(dir)) {
+ patchStats(dir, { syncing: true });
+ const child = spawn(process.execPath, [syncRun, dir], { detached: true, stdio: 'ignore' });
+ child.unref();
+ }
+}
+
export async function main(event: string): Promise<void> {
const input = readStdin();
const dir = projectDir(input);
@@ -55,29 +77,11 @@ export async function main(event: string): Promise<void> {
return;
}
- if (event === 'post-edit') {
- const file: string | undefined = input?.tool_input?.file_path;
- if (!file || underGraft(dir, file)) return;
- patchStats(dir, { dirty: true, staleCount: checkStaleCount(dir), lastFile: basename(file) });
- const w = readWiring(dir);
- if (w) { const br = formatBlastRadius(w, file); if (br) emit('PostToolUse', br); }
- return;
- }
+ if (event === 'post-edit') { await handlePostEdit(input, dir); return; }
- if (event === 'stop') {
- // sync-run.js ships next to this module inside the package, so it resolves in
- // any repo that installs graft (not just graft's own). Defensive existsSync:
- // if the package is somehow incomplete, skip rather than wedge on syncing:true.
- const syncRun = claudeScriptPath('sync-run.js');
- if (!existsSync(syncRun)) return;
- const stats = readStats(dir);
- if (stats?.dirty && acquireLock(dir)) {
- patchStats(dir, { syncing: true });
- const child = spawn(process.execPath, [syncRun, dir], { detached: true, stdio: 'ignore' });
- child.unref();
- }
- return;
- }
+ if (event === 'stop') { handleStop(dir); return; }
+
+ if (event === 'post-edit-sync') { await handlePostEdit(input, dir); handleStop(dir); return; }
if (event === 'prompt') {
const prompt = String(input?.prompt ?? '').trim();
diff --git a/test/claude-hooks.test.ts b/test/claude-hooks.test.ts
index 74c421e7..21ee78fe 100644
--- a/test/claude-hooks.test.ts
+++ b/test/claude-hooks.test.ts
@@ -1,12 +1,13 @@
import { test } from 'node:test';
import assert from 'node:assert/strict';
-import { existsSync, mkdtempSync, mkdirSync, writeFileSync } from 'node:fs';
+import { existsSync, mkdtempSync, mkdirSync, writeFileSync, rmSync } from 'node:fs';
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';
+import { claudeScriptPath } from '../src/claude/paths.js';
test('underGraft detects edits inside graft/', () => {
assert.equal(underGraft('/repo', '/repo/graft/x.md'), true);
@@ -40,6 +41,42 @@ async function runWithStdin(text: string, fn: () => Promise<void>): Promise<void
try { await fn(); } finally { delete process.env.GRAFT_TEST_STDIN; }
}
+test('post-edit-sync marks dirty and kicks off the background sync', async () => {
+ const d = mkdtempSync(join(tmpdir(), 'graft-hooks-'));
+ mkdirSync(join(d, 'graft', '.graph'), { recursive: true });
+ writeFileSync(join(d, 'graft', '.graph', 'wiring.json'),
+ JSON.stringify({ meta: { nodeCount: 0, edgeCount: 0, languages: [] }, nodes: [], edges: [] }));
+ process.env.CLAUDE_PROJECT_DIR = d;
+ // handleStop's spawn path is gated on claudeScriptPath('sync-run.js') existing next to this
+ // module. Under `tsx` test runs that resolves to src/claude/sync-run.js (no compiled sibling
+ // ships there — only sync-run.ts does), so stub the sibling in for this test to exercise the
+ // real spawn/lock behavior, then remove it again.
+ const syncRun = claudeScriptPath('sync-run.js');
+ const syncRunExisted = existsSync(syncRun);
+ if (!syncRunExisted) writeFileSync(syncRun, '// test stub: spawned as a detached no-op child\n');
+ try {
+ const stdin = JSON.stringify({ tool_input: { file_path: join(d, 'src', 'auth.ts') } });
+ await runWithStdin(stdin, () => main('post-edit-sync'));
+ const s = readStats(d)!;
+ assert.equal(s.dirty, true, 'post-edit half ran');
+ assert.equal(s.syncing, true, 'stop half ran');
+ assert.equal(existsSync(join(d, 'graft', '.cache', '.sync.lock')), true, 'sync lock file exists');
+ } finally {
+ if (!syncRunExisted) rmSync(syncRun, { force: true });
+ }
+});
+
+test('post-edit-sync on a file under graft/ does not mark dirty', async () => {
+ const d = mkdtempSync(join(tmpdir(), 'graft-hooks-'));
+ process.env.CLAUDE_PROJECT_DIR = d;
+ await runWithStdin(JSON.stringify({ tool_input: { file_path: join(d, 'graft', 'a.md') } }), () => main('post-edit-sync'));
+ // The under-graft guard means handlePostEdit never marks dirty. handleStop still runs after
+ // it, but the sync half may still run if stats were already dirty from a prior call — assert
+ // only that dirty was not newly set by this call.
+ const s = readStats(d);
+ assert.equal(s === null || s.dirty === false, true, 'dirty not newly set by this call');
+});
+
test('runSync clears dirty/syncing, recomputes stats, releases lock', () => {
const d = mkdtempSync(join(tmpdir(), 'graft-sync-'));
mkdirSync(join(d, 'graft', '.graph'), { recursive: true });