Skip to content

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

feat(init): install post-edit hooks for CLI agents (--no-hooks to skip)

Shrish Dwivedi769478950165
diff --git a/README.md b/README.md
index d48cc0f7..e1e6ad55 100644
--- a/README.md
+++ b/README.md
@@ -84,6 +84,8 @@ or register it by hand:
{ "mcpServers": { "graft": { "command": "npx", "args": ["-y", "@nanonets/graft", "mcp"] } } }
```
+Where a CLI agent supports user-level `hooks.json`, `init` also installs Graft's post-edit hook — blast-radius warnings and automatic `$0` graph re-sync after edits (skip with `--no-hooks`).
+
### Claude Code (deep integration)
`graft init` always wires up Claude Code, and Claude Code gets more than an instruction file. From then on, any Claude Code session opened in the repo gets:
diff --git a/src/cli.ts b/src/cli.ts
index 156977c6..3a363072 100644
--- a/src/cli.ts
+++ b/src/cli.ts
@@ -207,7 +207,8 @@ program
.option("--no-agents", "Claude Code wiring only; skip other agents")
.option("--list-agents", "list known agent ids and exit")
.option("--no-mcp", "skip MCP server registration for other agents")
- .action((dir: string, opts: { build?: boolean; agents?: string[]; allAgents?: boolean; listAgents?: boolean; mcp?: boolean }) => {
+ .option("--no-hooks", "skip hook installation for other agents")
+ .action((dir: string, opts: { build?: boolean; agents?: string[]; allAgents?: boolean; listAgents?: boolean; mcp?: boolean; hooks?: boolean }) => {
if (opts.listAgents) {
for (const id of [...hostIds(), "claude"]) console.log(id);
return;
@@ -242,12 +243,14 @@ program
agents: explicit?.filter((id) => id !== "claude"),
all: opts.allAgents,
mcp: opts.mcp,
+ hooks: opts.hooks,
});
for (const w of r.written) console.error(`✓ ${w.id}: ${w.path} (${w.action})`);
if (!explicit && !opts.allAgents && r.written.length === 0)
console.error("· no other agents detected (see --list-agents / --all-agents)");
// r.unknown is always empty here — ids are validated above, before any writes.
for (const m of r.mcp) console.error(`✓ mcp ${m.id}: ${m.path} (${m.action})`);
+ for (const h of r.hooks) console.error(`✓ hook ${h.id}: ${h.path} (${h.action})`);
}
console.error("\nDone. Claude Code gets live hooks + statusline; other agents read their instruction files.");
console.error("For LLM summaries: set OPENROUTER_API_KEY and run `graft build --deep`.");
diff --git a/src/hosts/init.ts b/src/hosts/init.ts
index 63a81a09..1a606291 100644
--- a/src/hosts/init.ts
+++ b/src/hosts/init.ts
@@ -9,12 +9,14 @@ import { homedir } from 'node:os';
import { HOSTS, detectHosts, type DetectProbe, type HostTarget } from './registry.js';
import { upsertSection } from './sections.js';
import { registerMcpConfigs, type McpWrite } from './mcp-config.js';
+import { installCodexHooks, type HookWrite } from './codex-hooks.js';
export interface HostsInitResult {
written: { id: string; path: string; action: string }[];
skipped: string[];
unknown: string[];
mcp: McpWrite[];
+ hooks: HookWrite[];
}
function probeFor(home: string, repo: string): DetectProbe {
@@ -34,7 +36,7 @@ function writeOwned(path: string, content: string): string {
export function runHostsInit(
repo: string,
- opts: { agents?: string[]; all?: boolean; home?: string; mcp?: boolean } = {},
+ opts: { agents?: string[]; all?: boolean; home?: string; mcp?: boolean; hooks?: boolean } = {},
): HostsInitResult {
const home = opts.home ?? homedir();
const probe = probeFor(home, repo);
@@ -62,5 +64,9 @@ export function runHostsInit(
}
const skipped = HOSTS.filter((h) => !selected.includes(h)).map((h) => h.id);
const mcp = opts.mcp === false ? [] : registerMcpConfigs(repo, selected.map((h) => h.id), { home });
- return { written, skipped, unknown, mcp };
+ const hooks =
+ opts.hooks === false || !selected.some((h) => h.id === 'agents')
+ ? []
+ : installCodexHooks(home);
+ return { written, skipped, unknown, mcp, hooks };
}
diff --git a/test/hosts-init.test.ts b/test/hosts-init.test.ts
index d68b6b27..63233dc8 100644
--- a/test/hosts-init.test.ts
+++ b/test/hosts-init.test.ts
@@ -120,3 +120,19 @@ test('CLI: --no-mcp writes the rule file but no MCP config', () => {
assert.ok(existsSync(join(repo, '.cursor', 'rules', 'graft.mdc')));
assert.ok(!existsSync(join(repo, '.cursor', 'mcp.json')));
});
+
+test('runHostsInit installs hooks for the agents id when the CLI home exists', () => {
+ const home = fresh(); const repo = fresh();
+ mkdirSync(join(home, '.codex'), { recursive: true });
+ const r = runHostsInit(repo, { home, agents: ['agents'] });
+ assert.equal(r.hooks.length, 2);
+ assert.ok(existsSync(join(home, '.codex', 'hooks', 'graft', 'graft-hooks.cjs')));
+});
+
+test('hooks: false skips hook installation', () => {
+ const home = fresh(); const repo = fresh();
+ mkdirSync(join(home, '.codex'), { recursive: true });
+ const r = runHostsInit(repo, { home, agents: ['agents'], hooks: false });
+ assert.deepEqual(r.hooks, []);
+ assert.ok(!existsSync(join(home, '.codex', 'hooks.json')));
+});