skip to content

Which Claude Code hook events fit four rules: block migration edits, add ticket context to prompts, notify when Claude waits, and restore context after compaction?

level: middleimportance: should knowfreq 16%

answer

  1. one event per moment in the loop
  2. before the tool, before the prompt
  3. notification types as matchers
  4. session start has a compact source
  5. not every event feeds Claude

basics

~10 s

Migration guard: PreToolUse on Edit|Write. Ticket context: UserPromptSubmit. Waiting alert: Notification matched on permission_prompt or idle_prompt. Post-compaction context: SessionStart with the compact matcher, whose stdout reaches Claude.

solid answer

~40 s

Pick the event by the moment the rule must act. Blocking a migration edit must happen before the tool runs, so `PreToolUse` with `Edit|Write`. Ticket context belongs on `UserPromptSubmit`, which runs before Claude processes each prompt and adds plain stdout or `additionalContext` alongside it; it has no matcher support. A waiting alert is a `Notification` hook matched on `permission_prompt` (after the dialog has waited about six seconds) or `idle_prompt` (about 60 seconds after Claude finished); use `PermissionRequest` for a signal the moment a dialog appears. Context after compaction is `SessionStart` with matcher `compact`, not `PostCompact`, which cannot add anything to Claude's context.

go deeper

for a junior

Recall the pairs: guard before a tool on PreToolUse, prompt context on UserPromptSubmit, alerts on Notification, post-compaction context on SessionStart with compact.

for a middle

Explain why each near-miss fails: PostCompact cannot feed Claude, UserPromptSubmit ignores matchers, permission_prompt waits about six seconds.

for a senior

Show you check what already survives compaction before adding a hook, and that UserPromptSubmit also fires on scheduled and background-agent turns, so its context must suit them.

for a principal

Decide which team rules deserve hooks at all versus CLAUDE.md or permission rules, and who maintains each script once the whole team depends on it.

## Choose by the moment, not by the name Claude Code fires hook events on three main cadences: **per session** (`SessionStart`, `SessionEnd`), **per turn** (`UserPromptSubmit`, `Stop`, `StopFailure`) and **per tool call** (`PreToolUse`, `PostToolUse`), plus families for permissions, subagents, compaction and standalone signals such as `Notification`, `InstructionsLoaded`, `ConfigChange` and `FileChanged`. Two questions settle most choices: 1. **When must the rule act?** Before an action, after it, or on a signal. 2. **Does the hook need to block, or to put text in front of Claude?** Only some events can block, and only some deliver output to Claude's context. ## The four rules | Rule | Event | Matcher | Why this one | |---|---|---|---| | Never edit `db/migrations/` | `PreToolUse` | `Edit\|Write` | Fires before the call and can deny it | | Add the ticket's context to every prompt | `UserPromptSubmit` | none (not supported) | Runs before Claude processes the prompt; stdout or `additionalContext` is added alongside it | | Tell me when Claude is waiting | `Notification` | `permission_prompt\|idle_prompt` | Fires on the notification types for a pending dialog and for an idle session | | Restore conventions after compaction | `SessionStart` | `compact` | Fires again after a compaction, and its stdout reaches Claude | ## Why the near-misses fail - **`PostToolUse` for the guard.** It fires after a successful call; the migration is already written. - **A `matcher` on `UserPromptSubmit`.** The event has no matcher support and a matcher there is silently ignored, so the hook still runs on every prompt. Filtering by prompt content is the script's job. It also fires on turns Claude Code starts itself, such as a scheduled task or `/loop` iteration and a background subagent reporting back, so the ticket context reaches those turns too. - **`Notification` when you want an instant signal.** In a terminal, `permission_prompt` fires only after the dialog has waited about six seconds without you typing, because it shares its timing with desktop notifications. For a signal the moment Claude asks, register on `PermissionRequest`. - **`PostCompact` or `PreCompact` for restoring context.** `PostCompact` fires after compaction but has no decision control and does not add output to Claude's context. `PreCompact` fires before compaction; it can block it, which is not the goal. Only `SessionStart` with source `compact` both fires at the right time and feeds Claude. - **`InstructionsLoaded` with `compact`.** It fires when instruction files reload after a compaction, but it is for audit and observability: it has no decision control and its output never reaches Claude. - **`Stop` for the waiting alert.** It fires whenever Claude finishes a response, so it could ping you, but it fires on every turn, including while you are watching, and it says nothing about a permission dialog that is waiting mid-turn. ## What survives compaction anyway Before writing the fourth hook, check what Claude Code already restores. The project-root CLAUDE.md and unscoped rules are re-injected from disk after compaction, so stable conventions written there come back without a hook. The `SessionStart` `compact` hook earns its place for **dynamic** state: the current branch, the sprint's focus, the last few commits. Context-window budgeting itself is a separate subject. ## A sketch of the configuration All four live under the `hooks` key of a settings file, one array per event. The rough shape is: - `PreToolUse`: one group, matcher `Edit|Write`, a command that exits 2 for migration paths. - `UserPromptSubmit`: one group, no matcher, a command that prints the ticket summary. - `Notification`: one group, matcher `permission_prompt|idle_prompt`, a command that raises the alert. - `SessionStart`: one group, matcher `compact`, a command that prints the conventions. How each script reads its stdin and what exactly it prints belong to the hook input and output contract; how the alert or the context script is built belongs to the handler patterns. ## Checking the choice 1. Run `/hooks` and confirm each hook is listed under the event you meant. 2. Trigger each moment on purpose: ask for an edit under `db/migrations/`, send a prompt, leave a permission dialog waiting, run `/compact`. 3. For the two context hooks, check the debug log rather than the transcript: injected context does not appear as a chat message.

  • Why might a Notification hook for permission prompts feel late, and what fires sooner?
    In a terminal, `permission_prompt` shares desktop-notification timing: it fires only once the dialog has waited about six seconds without a keystroke, and typing defers it. `PermissionRequest` fires as soon as Claude Code is about to show the dialog, so register there when you need the signal immediately.
  • Your SessionStart hook loads ticket context at startup. Why does it not reappear after `/clear` unless you plan for it?
    SessionStart's matcher filters on the source: `startup`, `resume`, `clear`, `compact` and `fork`. A group matched only on `startup` does not run after `/clear` or a compaction. Omit the matcher, or list the sources you need, such as `startup|clear|compact`.

  • PostCompact is the event for re-adding context after compaction.
  • A matcher on UserPromptSubmit filters which prompts trigger it.
  • The permission_prompt notification fires the instant a dialog appears.
  • PreCompact output is added to Claude's context after the summary.
  • Every event's stdout is added to Claude's context.