Skip to content

Run async/asyncRewake hooks in the background instead of awaiting them - #124

Merged
ilovepixelart merged 2 commits into
mainfrom
fix/async-hooks-block-turn
Sep 1, 2026
Merged

ilovepixelart merged 2 commits into
mainfrom
fix/async-hooks-block-turn

Conversation

@ilovepixelart

Copy link
Copy Markdown
Owner

Closes #123.

Hooks flagged async/asyncRewake were silently ignored and every hook ran blocking, so a Stop hook with a long timeout held pi's turn open for the hook's full duration and the session appeared frozen. They now follow Claude Code's documented background contract, on every event and for type: "command" hooks only:

  • A background hook fires without blocking and renders no decision (decision, permissionDecision, continue, updatedInput are inert), implemented as a single seam in the hook dispatcher so Stop, PreToolUse, PostToolUse, UserPromptSubmit and the notify events are all covered.
  • timeout is not enforced on async hooks (the timer is raised to the Node ceiling); asyncRewake keeps its own timeout.
  • An asyncRewake hook exiting 2 wakes the model with its stderr (stdout when stderr is empty) as a new turn.
  • Any other background completion delivers the JSON response's systemMessage/additionalContext to the model on the next turn, shown to nobody else; wrong-typed fields are dropped.
  • A timeout kill discards the hook's output.
  • Background hooks still running at session end are killed, so a hung hook cannot pin the event loop past a one-shot run.

The README hooks row and the extension header document the behavior.

  • npm run check passes (Biome, strict tsc, Vitest)
  • Tests cover the change (11 new tests, written red first against the blocking behavior; timeout-discard and teardown-kill verified by mutation)

Claude Code runs command hooks flagged async/asyncRewake in the
background. pi-code awaited every hook, so a Stop hook with a long
timeout held the turn open for the hook's full duration and the session
appeared frozen (#123).

Per the documented contract: background hooks (command type only) fire
without blocking and render no decision; timeout is not enforced on
async while asyncRewake keeps its own; an asyncRewake exit 2 wakes the
model with the hook's stderr as a new turn; other completions deliver
systemMessage/additionalContext to the model on the next turn, invisible
to the user; a timeout kill discards the output; hooks still running at
session end are killed.
@sonarqubecloud

sonarqubecloud Bot commented Sep 1, 2026

Copy link
Copy Markdown

@ilovepixelart
ilovepixelart merged commit b82c43f into main Sep 1, 2026
4 checks passed
@ilovepixelart
ilovepixelart deleted the fix/async-hooks-block-turn branch September 1, 2026 14:42
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.

Stop hooks with async/asyncRewake run synchronously, freezing the session for the hook's full timeout

1 participant