Skip to content

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

fix(claude): guard sync-run main() to direct-exec + stay dirty on unreadable wiring

Shrish Dwivedie9f7b4eb8cab
diff --git a/src/claude/sync-run.ts b/src/claude/sync-run.ts
index 5e57dfcc..831ff52b 100644
--- a/src/claude/sync-run.ts
+++ b/src/claude/sync-run.ts
@@ -1,5 +1,6 @@
import { execFileSync } from 'node:child_process';
import { join } from 'node:path';
+import { pathToFileURL } from 'node:url';
import { readWiring, computeStats } from './stats.js';
import { patchStats, releaseLock } from './state.js';
@@ -13,9 +14,11 @@ export function runSync(dir: string, build: (d: string) => void = realBuild): vo
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);
+ if (!w) { patchStats(dir, { syncing: false }); return; } // build ran but output unreadable — stay dirty, retry
+ patchStats(dir, {
+ dirty: false, staleCount: 0, syncing: false, syncedAt: new Date().toISOString(),
+ ...computeStats(w),
+ });
} catch {
patchStats(dir, { syncing: false }); // leave dirty=true; retry next turn
} finally {
@@ -27,4 +30,6 @@ export function main(): void {
const dir = process.argv[2];
if (dir) runSync(dir);
}
-main();
+
+// Run only when executed directly (node dist/claude/sync-run.js <dir>), not on import.
+if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) main();
diff --git a/test/claude-hooks.test.ts b/test/claude-hooks.test.ts
index 9baf4589..440cd730 100644
--- a/test/claude-hooks.test.ts
+++ b/test/claude-hooks.test.ts
@@ -71,3 +71,15 @@ test('runSync clears syncing even if build throws (money-safe failure)', () => {
assert.equal(s.dirty, true, 'stays dirty so the bar keeps ⚠ and it retries next turn');
assert.equal(acquireLock(d), true, 'lock always released');
});
+
+test('runSync stays dirty when build succeeds but wiring is unreadable', () => {
+ const d = mkdtempSync(join(tmpdir(), 'graft-sync-'));
+ writeStats(d, { ...emptyStats(), dirty: true, syncing: true, staleCount: 2 });
+ acquireLock(d);
+ runSync(d, () => { /* build "succeeds" but writes no wiring.json */ });
+ const s = readStats(d)!;
+ assert.equal(s.syncing, false);
+ assert.equal(s.dirty, true, 'unreadable wiring → stay dirty, retry next turn');
+ assert.equal(s.syncedAt, null, 'not marked synced');
+ assert.equal(acquireLock(d), true, 'lock released');
+});