feat(hooks): fire all lifecycle events (was only Pre/PostToolUse)#129
Merged
Conversation
The HookDispatcher handled all 9 event types but the agent loop only ever dispatched PreToolUse + PostToolUse — so SessionStart/SessionEnd/ UserPromptSubmit/Stop/SubagentStop/PreCompact/PostCompact/Notification hooks configured by users silently never ran. Now they all fire. agent loop (packages/core/src/agent.ts) — works for every host (CLI/desktop/lsp): - UserPromptSubmit: before the prompt is processed; appends any JSON `additionalContext` the hook returns. Top-level only (not sub-agent prompts). - Stop: when the top-level run ends (end_turn / max_turns); depth-guarded so a sub-agent's end is signalled by SubagentStop instead. - SubagentStop: when a Task sub-agent finishes. - PreCompact / PostCompact: around auto-compaction. CLI hosts: - repl.ts: SessionStart at startup, SessionEnd at exit, Notification after each turn (control returns to the user). - headless.ts: SessionStart before the run, SessionEnd in finally (with exitCode). Tests: +4 (agent.test.ts) — UserPromptSubmit+Stop fire; UserPromptSubmit additionalContext injection reaches the provider's user message; SubagentStop fires for a Task; PreCompact+PostCompact fire around compaction. Core 601 green. BEHAVIOR_PARITY: SubagentStop + Notification 🔄→✅ (all 10 events now live). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
The
HookDispatcherhandled all 9 event types, but the agent loop only ever dispatchedPreToolUse+PostToolUse. So hooks a user configured forSessionStart,SessionEnd,UserPromptSubmit,Stop,SubagentStop,PreCompact,PostCompact, orNotificationsilently never ran (the gap surfaced in the plan-vs-state audit). This wires all of them.Where each fires
Agent loop (
agent.ts) — works for every host (CLI / desktop / LSP):additionalContextthe hook returns. Top-level only (a sub-agent's prompt isn't a user prompt).end_turn/max_turns); depth-guarded so a sub-agent's end is reported by SubagentStop instead.Tasksub-agent finishes.CLI hosts:
repl.ts— SessionStart at startup, SessionEnd at exit, Notification after each turn (control returns to the user).headless.ts— SessionStart before the run, SessionEnd infinally(carriesexitCode).All dispatches are wrapped so a failing hook never breaks the agent.
Tests
+4 (
agent.test.ts): UserPromptSubmit+Stop fire on a normal run; UserPromptSubmitadditionalContextreaches the provider's user message; SubagentStop fires for aTask; PreCompact+PostCompact fire around compaction. Core suite 601 green.BEHAVIOR_PARITYSubagentStop + Notification 🔄→✅ — all 10 events now live.🤖 Generated with Claude Code