Skip to content

A blocked PreToolUse hook ends a headless run instead of returning the reason to the model #410

Closed

Description

@outlawpractice

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

  1. Add a PreToolUse hook for Bash to ~/.claude/settings.json that blocks a recognizable command by exiting 2 with a message on stderr. For example, a script that exits 2 when the command contains ps -o args.
  2. 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'
  1. Inspect the events: the bash tool_execution_end has isError: true and carries the hook's reason, then turn_end, then agent_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:

  1. 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 as PI_CODE_TERMINATE_ON_BLOCK=1.
  2. Terminate only when interactive. Pass terminate: ctx.hasUI === true, the same headless test the extension already uses for permissionDecision: "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

  1. added a commit that references this issue on Nov 3, 2026
    8255be8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions