-
Notifications
You must be signed in to change notification settings - Fork 1.2k
fix(claude-code): --with-hooks for MCP-standalone users (closes #508) #581
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
rohitg00
wants to merge
2
commits into
main
Choose a base branch
from
fix/claude-code-with-hooks
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+178
−5
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| import { describe, it, expect } from "vitest"; | ||
| import { resolve } from "node:path"; | ||
| import { | ||
| buildMergedHooks, | ||
| findPluginRoot, | ||
| type HookManifest, | ||
| } from "../src/cli/connect/codex-hooks.js"; | ||
|
|
||
| const PLUGIN_ROOT = resolve(__dirname, "..", "plugin"); | ||
|
|
||
| describe("buildMergedHooks against plugin/hooks/hooks.json (Claude Code)", () => { | ||
| it("locates the same plugin root used by the codex variant", () => { | ||
| expect(findPluginRoot()).toBe(PLUGIN_ROOT); | ||
| }); | ||
|
|
||
| it("rewrites ${CLAUDE_PLUGIN_ROOT} to absolute pluginRoot in every command", () => { | ||
| const merged = buildMergedHooks(null, PLUGIN_ROOT, "hooks.json"); | ||
| for (const entries of Object.values(merged.hooks)) { | ||
| for (const entry of entries) { | ||
| for (const handler of entry.hooks) { | ||
| expect(handler.command).not.toContain("${CLAUDE_PLUGIN_ROOT}"); | ||
| expect(handler.command).toContain(`${PLUGIN_ROOT}/scripts/`); | ||
| } | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| it("includes Claude-only events that hooks.codex.json omits", () => { | ||
| const merged = buildMergedHooks(null, PLUGIN_ROOT, "hooks.json"); | ||
| const events = Object.keys(merged.hooks); | ||
| expect(events).toContain("SessionStart"); | ||
| expect(events).toContain("Stop"); | ||
| const claudeOnly = ["SessionEnd", "SubagentStop", "Notification"]; | ||
| expect( | ||
| claudeOnly.some((e) => events.includes(e)), | ||
| `hooks.json should include at least one Claude-only event (${claudeOnly.join(", ")})`, | ||
| ).toBe(true); | ||
| }); | ||
|
|
||
| it("appends to existing user hooks without dropping them", () => { | ||
| const existing: HookManifest = { | ||
| hooks: { | ||
| SessionStart: [ | ||
| { hooks: [{ type: "command", command: "echo user-custom-claude" }] }, | ||
| ], | ||
| }, | ||
| }; | ||
| const merged = buildMergedHooks(existing, PLUGIN_ROOT, "hooks.json"); | ||
| const sessionStart = merged.hooks["SessionStart"]!; | ||
| expect( | ||
| sessionStart.some((e) => | ||
| e.hooks.some((h) => h.command === "echo user-custom-claude"), | ||
| ), | ||
| ).toBe(true); | ||
| expect( | ||
| sessionStart.some((e) => | ||
| e.hooks.some((h) => | ||
| h.command.includes(`${PLUGIN_ROOT}/scripts/session-start.mjs`), | ||
| ), | ||
| ), | ||
| ).toBe(true); | ||
| }); | ||
|
|
||
| it("re-install strips previous agentmemory entries (idempotent)", () => { | ||
| const first = buildMergedHooks(null, PLUGIN_ROOT, "hooks.json"); | ||
| const second = buildMergedHooks(first, PLUGIN_ROOT, "hooks.json"); | ||
| for (const event of Object.keys(first.hooks)) { | ||
| expect( | ||
| second.hooks[event]!.length, | ||
| `${event} should not double after second install`, | ||
| ).toBe(first.hooks[event]!.length); | ||
| } | ||
| }); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Make hook fallback non-fatal by catching merge/write failures.
installClaudeHooks()only guardsfindPluginRoot(). If manifest read/parse or settings write fails, it throws and can abort the whole connect flow instead of returningkind: "skipped"for the warning path.Suggested fix
function installClaudeHooks(opts: ConnectOptions): ConnectResult { let pluginRoot: string; try { pluginRoot = findPluginRoot(); } catch (err) { return { kind: "skipped", reason: err instanceof Error ? err.message : String(err), }; } - type ClaudeSettings = { hooks?: HookManifest["hooks"]; [key: string]: unknown }; - const existing = readJsonSafe<ClaudeSettings>(CLAUDE_SETTINGS) ?? {}; - const existingHooks: HookManifest | null = existing.hooks - ? { hooks: existing.hooks } - : null; - const merged = buildMergedHooks(existingHooks, pluginRoot, "hooks.json"); + try { + type ClaudeSettings = { hooks?: HookManifest["hooks"]; [key: string]: unknown }; + const existing = readJsonSafe<ClaudeSettings>(CLAUDE_SETTINGS) ?? {}; + const existingHooks: HookManifest | null = existing.hooks + ? { hooks: existing.hooks } + : null; + const merged = buildMergedHooks(existingHooks, pluginRoot, "hooks.json"); - if (opts.dryRun) { - p.log.info( - `[dry-run] Would merge agentmemory hook entries into ${CLAUDE_SETTINGS} (${Object.keys(merged.hooks).length} event(s))`, - ); - return { kind: "installed", mutatedPath: CLAUDE_SETTINGS }; - } + if (opts.dryRun) { + p.log.info( + `[dry-run] Would merge agentmemory hook entries into ${CLAUDE_SETTINGS} (${Object.keys(merged.hooks).length} event(s))`, + ); + return { kind: "installed", mutatedPath: CLAUDE_SETTINGS }; + } - let backupPath: string | undefined; - if (existsSync(CLAUDE_SETTINGS)) { - backupPath = backupFile(CLAUDE_SETTINGS, "claude-settings", "json"); - logBackup(backupPath); - } else { - mkdirSync(CLAUDE_DIR, { recursive: true }); - } + let backupPath: string | undefined; + if (existsSync(CLAUDE_SETTINGS)) { + backupPath = backupFile(CLAUDE_SETTINGS, "claude-settings", "json"); + logBackup(backupPath); + } else { + mkdirSync(CLAUDE_DIR, { recursive: true }); + } - const next: ClaudeSettings = { ...existing, hooks: merged.hooks }; - writeJsonAtomic(CLAUDE_SETTINGS, next); + const next: ClaudeSettings = { ...existing, hooks: merged.hooks }; + writeJsonAtomic(CLAUDE_SETTINGS, next); - logInstalled("Claude Code hooks (workaround for `#508`)", CLAUDE_SETTINGS); - p.log.info( - "User-scope hook entries reference absolute paths under the bundled plugin/ dir. Re-run `agentmemory connect claude-code --with-hooks` after upgrading agentmemory to refresh them.", - ); + logInstalled("Claude Code hooks (workaround for `#508`)", CLAUDE_SETTINGS); + p.log.info( + "User-scope hook entries reference absolute paths under the bundled plugin/ dir. Re-run `agentmemory connect claude-code --with-hooks` after upgrading agentmemory to refresh them.", + ); - return { - kind: "installed", - mutatedPath: CLAUDE_SETTINGS, - ...(backupPath !== undefined && { backupPath }), - }; + return { + kind: "installed", + mutatedPath: CLAUDE_SETTINGS, + ...(backupPath !== undefined && { backupPath }), + }; + } catch (err) { + return { + kind: "skipped", + reason: err instanceof Error ? err.message : String(err), + }; + } }Also applies to: 164-166
🤖 Prompt for AI Agents