Skip to content

Commit b8aec39

Browse files
colbymchenryclaude
andauthored
fix(installer): strip stale auto-sync hooks on install and uninstall (colbymchenry#278)
Pre-0.8 installers wrote `codegraph mark-dirty` / `sync-if-dirty` hooks to Claude Code's settings.json. Both subcommands were removed from the CLI, so the Stop hook fails every turn ("unknown command 'sync-if-dirty'"). The cleanup that once removed them was lost when the installer moved to the per-target architecture. Add cleanupLegacyHooks(), wired into both install (upgrades self-heal) and uninstall (so the npm preuninstall step fully reverses a legacy install). Surgical at the command level: only codegraph's own hook entries are dropped, so unrelated hooks sharing a matcher group or event (e.g. GitKraken's `gk ai hook run`) survive, and a settings.json with no legacy hooks is left byte-for-byte untouched. Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 4329a52 commit b8aec39

3 files changed

Lines changed: 225 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,20 @@ and adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
2121
signatures, generics, and Roblox instance-path `require(script.Parent.X)`
2222
imports.
2323

24+
### Fixed
25+
- **Installer**: re-running `codegraph install` now removes the broken
26+
auto-sync hooks that pre-0.8 versions wrote to Claude Code's
27+
`settings.json`. Those builds added a `Stop → codegraph sync-if-dirty`
28+
hook (and a `PostToolUse → codegraph mark-dirty` partner); both
29+
subcommands were later removed from the CLI, so Claude Code reported
30+
`Stop hook error: ... unknown command 'sync-if-dirty'` on every turn.
31+
The cleanup is surgical — only codegraph's own hook entries are
32+
stripped, so unrelated hooks sharing the same file or event (e.g. a
33+
GitKraken `gk ai hook run` hook) are left untouched — and it also runs
34+
on uninstall, so the npm `preuninstall` step fully reverses a legacy
35+
install. Re-run `codegraph install` once on an affected machine to
36+
clear the error.
37+
2438
## [0.8.0] - 2026-05-20
2539

2640
### Added

__tests__/installer-targets.test.ts

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import * as path from 'path';
2020
import * as os from 'os';
2121
import { ALL_TARGETS, getTarget, resolveTargetFlag } from '../src/installer/targets/registry';
2222
import { upsertTomlTable, removeTomlTable, buildTomlTable } from '../src/installer/targets/toml';
23+
import { cleanupLegacyHooks } from '../src/installer/targets/claude';
2324

2425
function mkTmpDir(label: string): string {
2526
return fs.mkdtempSync(path.join(os.tmpdir(), `cg-targets-${label}-`));
@@ -433,6 +434,120 @@ describe('Installer targets — partial-state idempotency', () => {
433434
expect(legacy.mcpServers.codegraph).toBeUndefined();
434435
expect(legacy.mcpServers.other).toBeDefined();
435436
});
437+
438+
// ---- Legacy auto-sync hook cleanup ----
439+
// Pre-0.8 installs wrote `codegraph mark-dirty` / `sync-if-dirty`
440+
// hooks to settings.json. Both subcommands were removed from the CLI,
441+
// so the Stop hook fails every turn ("unknown command
442+
// 'sync-if-dirty'"). The installer must strip them on upgrade and
443+
// uninstall — without touching the user's unrelated hooks.
444+
445+
function seedSettings(loc: 'global' | 'local', settings: Record<string, any>): string {
446+
const dir = path.join(loc === 'global' ? tmpHome : tmpCwd, '.claude');
447+
fs.mkdirSync(dir, { recursive: true });
448+
const file = path.join(dir, 'settings.json');
449+
fs.writeFileSync(file, JSON.stringify(settings, null, 2) + '\n');
450+
return file;
451+
}
452+
453+
// Realistic pre-0.8 settings.json: our two auto-sync hooks plus an
454+
// unrelated GitKraken Stop hook the user added (matches the report).
455+
function legacyHookSettings(): Record<string, any> {
456+
return {
457+
hooks: {
458+
PostToolUse: [
459+
{ matcher: 'Edit|Write', hooks: [{ type: 'command', command: 'codegraph mark-dirty', async: true }] },
460+
],
461+
Stop: [
462+
{ hooks: [{ type: 'command', command: 'codegraph sync-if-dirty' }] },
463+
{ hooks: [{ type: 'command', command: '"/Users/me/gk" ai hook run --host claude-code' }] },
464+
],
465+
},
466+
};
467+
}
468+
469+
it('claude: install strips stale codegraph auto-sync hooks but keeps the user\'s GitKraken hook', () => {
470+
const claude = getTarget('claude')!;
471+
const file = seedSettings('global', legacyHookSettings());
472+
473+
claude.install('global', { autoAllow: true });
474+
475+
const after = JSON.parse(fs.readFileSync(file, 'utf-8'));
476+
// The only PostToolUse group held mark-dirty → the event is gone.
477+
expect(after.hooks?.PostToolUse).toBeUndefined();
478+
const stopCommands = (after.hooks?.Stop ?? []).flatMap((g: any) =>
479+
(g.hooks ?? []).map((h: any) => h.command),
480+
);
481+
expect(stopCommands).not.toContain('codegraph sync-if-dirty');
482+
// The unrelated GitKraken hook survives untouched.
483+
expect(stopCommands.some((c: string) => c.includes('gk') && c.includes('ai hook run'))).toBe(true);
484+
// Permissions still written as normal alongside the cleanup.
485+
expect(after.permissions?.allow).toContain('mcp__codegraph__codegraph_search');
486+
});
487+
488+
it('claude: cleanupLegacyHooks preserves a sibling hook sharing our matcher group', () => {
489+
const file = seedSettings('global', {
490+
hooks: {
491+
Stop: [
492+
{
493+
hooks: [
494+
{ type: 'command', command: 'codegraph sync-if-dirty' },
495+
{ type: 'command', command: 'gk ai hook run --host claude-code' },
496+
],
497+
},
498+
],
499+
},
500+
});
501+
502+
expect(cleanupLegacyHooks('global').action).toBe('removed');
503+
504+
const after = JSON.parse(fs.readFileSync(file, 'utf-8'));
505+
expect(after.hooks.Stop[0].hooks.map((h: any) => h.command)).toEqual([
506+
'gk ai hook run --host claude-code',
507+
]);
508+
});
509+
510+
it('claude: cleanupLegacyHooks is a byte-for-byte no-op without codegraph hooks', () => {
511+
const original =
512+
JSON.stringify({ hooks: { Stop: [{ hooks: [{ type: 'command', command: 'gk ai hook run' }] }] } }, null, 2) + '\n';
513+
const file = seedSettings('global', JSON.parse(original));
514+
515+
expect(cleanupLegacyHooks('global').action).toBe('unchanged');
516+
expect(fs.readFileSync(file, 'utf-8')).toBe(original);
517+
});
518+
519+
it('claude: cleanupLegacyHooks reports not-found when settings.json is absent', () => {
520+
expect(cleanupLegacyHooks('global').action).toBe('not-found');
521+
});
522+
523+
it('claude: re-running install after a legacy cleanup leaves settings.json unchanged', () => {
524+
const claude = getTarget('claude')!;
525+
const file = seedSettings('global', legacyHookSettings());
526+
claude.install('global', { autoAllow: true });
527+
const firstPass = fs.readFileSync(file, 'utf-8');
528+
claude.install('global', { autoAllow: true });
529+
expect(fs.readFileSync(file, 'utf-8')).toBe(firstPass);
530+
});
531+
532+
it('claude: uninstall strips stale hooks written in the npx form (local)', () => {
533+
const claude = getTarget('claude')!;
534+
const file = seedSettings('local', {
535+
hooks: {
536+
PostToolUse: [
537+
{ matcher: 'Edit|Write', hooks: [{ type: 'command', command: 'npx @colbymchenry/codegraph mark-dirty', async: true }] },
538+
],
539+
Stop: [
540+
{ hooks: [{ type: 'command', command: 'npx @colbymchenry/codegraph sync-if-dirty' }] },
541+
],
542+
},
543+
});
544+
545+
claude.uninstall('local');
546+
547+
const after = JSON.parse(fs.readFileSync(file, 'utf-8'));
548+
// Both events emptied → the whole `hooks` object is removed.
549+
expect(after.hooks).toBeUndefined();
550+
});
436551
});
437552

438553
describe('Installer targets — registry', () => {

src/installer/targets/claude.ts

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,15 @@ class ClaudeCodeTarget implements AgentTarget {
114114
files.push(writePermissionsEntry(loc));
115115
}
116116

117+
// 2b. Strip stale auto-sync hooks left by a pre-0.8 install. Those
118+
// versions wrote `codegraph mark-dirty` / `sync-if-dirty` hooks to
119+
// settings.json; both subcommands are gone from the CLI, so the
120+
// Stop hook now fails every turn with "unknown command
121+
// 'sync-if-dirty'". Cleaning up on install makes an upgrade
122+
// self-healing. Only surfaced when something was actually removed.
123+
const hookCleanup = cleanupLegacyHooks(loc);
124+
if (hookCleanup.action === 'removed') files.push(hookCleanup);
125+
117126
// 3. CLAUDE.md instructions
118127
files.push(writeInstructionsEntry(loc));
119128

@@ -168,6 +177,14 @@ class ClaudeCodeTarget implements AgentTarget {
168177
files.push({ path: settingsPath, action: 'not-found' });
169178
}
170179

180+
// 2b. Strip any stale auto-sync hooks a pre-0.8 install left in
181+
// settings.json. The hook-cleanup step was lost when the installer
182+
// moved to the per-target architecture; restoring it here means
183+
// uninstall — and the npm `preuninstall` hook that drives it — fully
184+
// reverses a legacy install.
185+
const hookCleanup = cleanupLegacyHooks(loc);
186+
if (hookCleanup.action === 'removed') files.push(hookCleanup);
187+
171188
// 3. Instructions
172189
const instr = instructionsPath(loc);
173190
const action = removeMarkedSection(instr, CODEGRAPH_SECTION_START, CODEGRAPH_SECTION_END);
@@ -241,6 +258,85 @@ function cleanupLegacyLocalMcp(): WriteResult['files'][number] | null {
241258
return { path: file, action: 'removed' };
242259
}
243260

261+
/**
262+
* True when a Claude Code hook `command` is one of the auto-sync hooks
263+
* a pre-0.8 install wrote. Those installers added
264+
* `PostToolUse(Edit|Write) → codegraph mark-dirty` and
265+
* `Stop → codegraph sync-if-dirty` (local builds used the
266+
* `npx @colbymchenry/codegraph …` form, which still contains the
267+
* `codegraph <subcommand>` substring). Both subcommands were later
268+
* removed from the CLI, so the Stop hook fails every turn with
269+
* "unknown command 'sync-if-dirty'". Matching on the codegraph-scoped
270+
* subcommand keeps unrelated user hooks (e.g. GitKraken's
271+
* `gk ai hook run`) untouched.
272+
*/
273+
function isLegacyCodegraphHookCommand(command: unknown): boolean {
274+
if (typeof command !== 'string') return false;
275+
return (
276+
command.includes('codegraph mark-dirty') ||
277+
command.includes('codegraph sync-if-dirty')
278+
);
279+
}
280+
281+
/**
282+
* Remove stale codegraph auto-sync hooks from Claude `settings.json`.
283+
*
284+
* Surgical at the individual-command level: only entries matching
285+
* `isLegacyCodegraphHookCommand` are dropped, so a sibling hook sharing
286+
* a matcher group (or the Stop event) with ours survives. We prune a
287+
* matcher group only once its `hooks` array is empty, an event only
288+
* once it has no groups left, and `hooks` itself only once every event
289+
* is gone — and none of that runs unless we actually removed a
290+
* codegraph command, so a settings.json with no legacy hooks is left
291+
* byte-for-byte untouched and reported `unchanged`.
292+
*
293+
* Exported so it can be unit-tested directly and reused by both
294+
* `install` (an upgrade self-heals) and `uninstall`.
295+
*/
296+
export function cleanupLegacyHooks(loc: Location): WriteResult['files'][number] {
297+
const file = settingsJsonPath(loc);
298+
if (!fs.existsSync(file)) return { path: file, action: 'not-found' };
299+
300+
const settings = readJsonFile(file);
301+
const hooks = settings.hooks;
302+
if (!hooks || typeof hooks !== 'object' || Array.isArray(hooks)) {
303+
return { path: file, action: 'unchanged' };
304+
}
305+
306+
// Pass 1: drop the legacy command(s) from inside every matcher group.
307+
let removedAny = false;
308+
for (const event of Object.keys(hooks)) {
309+
const groups = hooks[event];
310+
if (!Array.isArray(groups)) continue;
311+
for (const group of groups) {
312+
if (!group || !Array.isArray(group.hooks)) continue;
313+
const before = group.hooks.length;
314+
group.hooks = group.hooks.filter(
315+
(h: any) => !isLegacyCodegraphHookCommand(h?.command),
316+
);
317+
if (group.hooks.length !== before) removedAny = true;
318+
}
319+
}
320+
321+
if (!removedAny) return { path: file, action: 'unchanged' };
322+
323+
// Pass 2: prune empty matcher groups, then events with no groups
324+
// left, then an empty top-level `hooks`. Guarded by `removedAny` so
325+
// we never restructure a settings.json that had no codegraph hooks.
326+
for (const event of Object.keys(hooks)) {
327+
const groups = hooks[event];
328+
if (!Array.isArray(groups)) continue;
329+
hooks[event] = groups.filter(
330+
(g: any) => !(g && Array.isArray(g.hooks) && g.hooks.length === 0),
331+
);
332+
if (hooks[event].length === 0) delete hooks[event];
333+
}
334+
if (Object.keys(hooks).length === 0) delete settings.hooks;
335+
336+
writeJsonFile(file, settings);
337+
return { path: file, action: 'removed' };
338+
}
339+
244340
export function writePermissionsEntry(loc: Location): WriteResult['files'][number] {
245341
const file = settingsJsonPath(loc);
246342
const settings = readJsonFile(file);

0 commit comments

Comments
 (0)