Skip to content

fix(prompt-hook): skip Claude Code task notifications (#1832) - #1884

Open
danusha2345 wants to merge 1 commit into
colbymchenry:mainfrom
danusha2345:fix/1832-prompt-hook-task-notification
Open

danusha2345 wants to merge 1 commit into
colbymchenry:mainfrom
danusha2345:fix/1832-prompt-hook-task-notification

Conversation

@danusha2345

Copy link
Copy Markdown
Contributor

Symptom

Claude Code's UserPromptSubmit hook also fires for the <task-notification>…</task-notification> blocks the host injects as user messages when a background Agent finishes. Those blocks are long machine text (task IDs, toolu_* IDs, scratchpad paths, an agent's prose summary about code), so they pass the structural gate on the merits and the whole blob is sent to codegraph_explore as the query. Reported cost: 3.5 s median / 7.4 s max on the blocking path, on text the user never typed (36% of the hook's firings in the reporter's history).

Fix

  • isTaskNotification(prompt) in src/directory.ts: true only when the entire prompt (modulo leading/trailing whitespace) is one <task-notification>…</task-notification> envelope.
  • codegraph prompt-hook returns immediately after parsing stdin when the predicate matches — before telemetry, the gate, project lookup or any explore work. No output is written.
  • A prompt that merely mentions the marker, or has any text around the envelope, is still treated as a user prompt (no arbitrary XML-looking prompts are skipped).
  • CHANGELOG entry under [Unreleased]### Fixes.

Verification

  • npx tsc --noEmit -p tsconfig.json → exit 0.
  • npx vitest run __tests__/frontload-hook.test.ts → 42 passed (new describe('system task notifications (#1832)') covers the envelope with/without surrounding whitespace and multi-line body, and the negative cases: marker inside a question, trailing text, other tag, plain prompt).
  • Smoke test of the built dist/bin/codegraph.js prompt-hook from an indexed repo: a <task-notification> payload (Claude Code's {"prompt","cwd"} JSON on stdin) prints 0 bytes and exits 0 in ~0.26 s; a normal structural prompt still prints the <codegraph_context> block.

Fixes #1832

🤖 Generated with Claude Code

)

Claude Code's UserPromptSubmit hook also fires for the
`<task-notification>…</task-notification>` blocks the host injects as
`user` messages when a background agent finishes. Those blocks are long
machine text full of identifier-shaped tokens, so they pass the structural
gate and the whole blob is sent to codegraph_explore as the query — a
3.5s median (7.4s max) stall on the blocking path, on text the user never
typed.

Recognize a prompt that consists solely of one task-notification envelope
(leading/trailing whitespace allowed) and exit before any project lookup,
gate or explore work. A prompt that merely mentions the marker, or has
text around it, is still treated as a user prompt.

Fixes colbymchenry#1832

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

This branch has not been deployed

No deployments
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.

prompt-hook fires on Claude Code's system-injected <task-notification> messages: 3.5s median, 7.4s max on the blocking path

1 participant