Skip to content

fix: register the hook as PostToolUse so it actually fires#3

Open
quad341 wants to merge 1 commit into
vincent-k2026:masterfrom
quad341:fix/posttooluse-hook-registration
Open

fix: register the hook as PostToolUse so it actually fires#3
quad341 wants to merge 1 commit into
vincent-k2026:masterfrom
quad341:fix/posttooluse-hook-registration

Conversation

@quad341

@quad341 quad341 commented Jul 23, 2026

Copy link
Copy Markdown

The bug

codachi init writes the hook into ~/.claude/settings.json like this:

"hooks": {
  "PostToolExecution": [
    { "matcher": "", "command": "codachi-hook" }
  ]
}

Two problems: Claude Code has no PostToolExecution event (it's PostToolUse), and a matcher entry carries its commands in a nested hooks: [{type, command}] array rather than a flat command key.

The result is a hook that never runs. Claude Code ignores the unknown event, codachi-hook is never invoked, nothing is ever appended to events.json, and the mood engine has no events to read — so pets sit on idle/context messages forever and none of the ~40 event categories or the reactions built on them ever surface. It fails silently: the install prints success and the statusline renders fine, which is presumably why it went unnoticed.

CLAUDE.md already documents the data flow as PostToolUse, so this looks like a straightforward slip in init.ts rather than a deliberate choice.

The fix

  • init writes PostToolUse with the nested hooks array and matcher: "*".
  • init also drops any stale codachi entry left under PostToolExecution, so existing users are migrated by re-running codachi init — no manual settings editing.
  • uninstall cleans up both event names.
  • Both paths recognise the flat legacy shape and the nested one, and leave other tools' hooks (and other events) untouched.
  • Docs updated: README.md manual-setup block and prose, CLAUDE.md, hook.ts header.

Verification

src/init.test.ts is new — 14 tests covering the written shape, statusLine, idempotent re-init, legacy migration, preservation of foreign hooks and unrelated settings, local-clone absolute paths, and the uninstall paths including the no-op cases.

Full suite: 375 passed (20 files), tsc --noEmit clean.

Also verified end-to-end against a throwaway $HOME containing a legacy install plus an unrelated PreToolUse hook: init migrated it to PostToolUse, left the foreign hook alone, and uninstall removed only codachi's entry.

I'm running this fix locally and the hook now records events as expected.

🤖 Generated with Claude Code

https://claude.ai/code/session_018sFUvWzeRmjzQGDUsLnYgY

`init` wrote the hook under `PostToolExecution` with a flat `command` key.
Claude Code has no such event, and matcher entries need a nested
`hooks: [{type, command}]` array — so the hook was silently ignored on every
install. No events reached events.json, and the mood engine never saw a tool
use, leaving pets on idle messages permanently.

Write the documented shape instead, and clear the stale `PostToolExecution`
entry on init so existing installs are migrated by re-running `codachi init`.
`uninstall` now cleans up both event names, and both paths leave other tools'
hooks untouched.

Adds src/init.test.ts (14 tests) covering the written shape, idempotency,
legacy migration, foreign-hook preservation, and uninstall.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant