Repository navigation
Description
Summary
blockedToolCall() in extensions/hooks/decisions.ts returns { block: true, reason, terminate: true } for every PreToolUse block. Pi treats terminate as "stop after the current tool batch", so when a hook blocks the only call in a batch, the agent run ends right there. In a headless run (--mode json or -p) the run exits 0 with no final answer, and nothing in stderr says why.
Claude Code continues after a blocked call: the hook's reason goes back to the model as the tool result, and the model chooses another approach. Since pi-code exists to run Claude Code hooks with Claude's semantics, this is a behavior difference.
Versions
- pi-code 1.5.2
- @earendil-works/pi-coding-agent 1.3.0
- macOS, Node 24
Reproduce
- Add a PreToolUse hook for
Bashto~/.claude/settings.jsonthat blocks a recognizable command by exiting 2 with a message on stderr. For example, a script that exits 2 when the command containsps -o args. - Run Pi headless with a prompt that triggers it and then asks for a final word:
pi --mode json --no-session --tools bash -p 'Run exactly this bash command: `ps -o args -p $$`. Whatever happens, do not run anything else, then reply with exactly one word: SURVIVED'- Inspect the events: the bash
tool_execution_endhasisError: trueand carries the hook's reason, thenturn_end, thenagent_end.
Actual: one assistant message, stopReason: "toolUse", no final text. The run ends after the block.
Expected (Claude Code parity): the reason is returned to the model, which answers SURVIVED. After removing terminate: true locally, the same run produces two assistant messages and the final text SURVIVED.
Impact
Any headless or delegated Pi run that is guarded by a security hook stops at the first blocked command, with no answer and a clean exit. We hit it with read-only review agents: each run ended partway through the review whenever a secret-guard hook blocked one shell command. The cause was only visible by inspecting the JSON event stream.
Context
The comment on blockedToolCall cites #7715: with terminate set, Pi skips the follow-up model call that a plain block would otherwise cost. That saves one call in an interactive session, where the user can simply reply. A headless session has no one to reply, so the saving costs the whole run.
Proposed fix
Either of these, with a preference for the first:
- Claude parity by default. Return
{ block: true, reason }and let the model continue. Offer the current behavior as an opt-in, for example an environment variable such asPI_CODE_TERMINATE_ON_BLOCK=1. - Terminate only when interactive. Pass
terminate: ctx.hasUI === true, the same headless test the extension already uses forpermissionDecision: "ask"and for SessionEnd cleanup.
Whichever is chosen, a short line in docs/hooks.md stating what happens after a block would help users who port Claude Code hooks.
Workaround
Patch extensions/hooks/decisions.ts locally so blockedToolCall returns { block: true, reason }. The patch is lost on pi update.
Activity
Return a blocked PreToolUse reason to the model instead of ending the…