Skip to content

fix(hooks): pass Claude Code hook input on stdin to Pi tool hooks - #3963

Merged
dyoshikawa merged 5 commits into
mainfrom
resolve-issue-3953-pi-hooks-stdin
Nov 25, 2026
Merged

dyoshikawa merged 5 commits into
mainfrom
resolve-issue-3953-pi-hooks-stdin

Conversation

@dyoshikawa

@dyoshikawa dyoshikawa commented Nov 25, 2026 •

Copy link
Copy Markdown
Owner

Summary

The generated Pi (and oh-my-pi) hooks extension ran every hook command with an empty, never-closed stdin, so hook scripts written for Claude Code / Codex / Cursor that read tool_name / tool_input never matched and a guard silently failed open. On top of that, a canonical Bash matcher was tested only against Pi's lowercase bash, so it never fired at all.

What changed

  • Stdin payload (preToolUse, postToolUse, postToolUseFailure): commands receive Claude Code's hook input as JSON — session_id (ctx.sessionManager.getSessionId()), cwd (ctx.cwd), hook_event_name, tool_name, tool_input, tool_use_id (toolCallId), plus tool_response for PostToolUse and error for PostToolUseFailure. It is serialized lazily, once per invocation, like the OpenCode-style plugin's STDIN_PAYLOADS. Every other command now gets an empty, closed stdin (previously a cat would hang).
  • Canonical tool names (per dialect): bash/read/write/edit/grep → Bash/Read/Write/Edit/Grep; Pi's find and oh-my-pi's glob → Glob (oh-my-pi's find is a search tool and stays unmapped). MCP tools are already mcp__<server>__<tool> in Pi. Others, including Pi's powershell, keep their own name.
  • File-tool arguments: path → an absolute file_path resolved against ctx.cwd (with Pi's @ / ~ handling), edits[].oldText/newText → old_string/new_string, and every edit's text joined into the top-level old_string / new_string. Mapped keys are written last, and any file_path / old_string / new_string the model added is dropped (inside each edit too), so a decoy argument cannot make a guard check a different file. Path resolution is per dialect and copies each host's resolveToCwd: Pi v1.6.0 utils/paths.ts, which has no URL passthrough, and oh-my-pi tools/path-utils.ts at 76b7d84, which strips a stray leading :, accepts @ only in some shapes, and leaves only its own internal URL schemes as written. The raw path is also kept in tool_input. oh-my-pi's replace-mode edit already uses old_string / new_string, so those are kept as they are. oh-my-pi's patch edit modes ({ input } holding the patch) are passed through unparsed. The docs say plainly that Claude-style path guards do not see those edits.
  • Matcher: tested against both Pi's name and the canonical one, so Bash now matches and existing bash matchers keep working.
  • Block semantics on tool_call, reusing the OpenCode-style plugin's PreToolUse contract: any non-zero exit (exit 2 included) denies with stderr as the reason (unchanged), and exit 0 with hookSpecificOutput.permissionDecision: "deny" denies with permissionDecisionReason. The deprecated top-level decision: "block" that Claude Code still honors is accepted when no permissionDecision is set — added to the OpenCode-style plugin too so both runners read the same output the same way.
  • permissionDecision: "ask": Pi has no permission prompt, so the generated handler asks the user with ctx.ui.confirm. With no UI (print/JSON modes), or if the dialog fails, the call is denied.
  • Timeouts: each command is bounded by its hook timeout (default 600 s, via the shared hookTimingOf). It runs in its own process group (not on Windows), and on timeout the whole group is killed. The call returns right away, so a descendant holding stdout open cannot hang it. Hook groups that are still running are killed on session_shutdown, because a detached group does not receive Ctrl+C.

Fail-closed policy (and why)

I did not invent a new policy; it mirrors the existing runners:

  • A crash / non-zero exit / command that cannot be spawned already denied the Pi tool call (that was the generator's existing behavior, and Pi itself treats a throwing tool_call handler as a block).
  • A timed-out preToolUse command denies the call, unless the hook sets failClosed: false, which lets it through and logs it — exactly the OpenCode-style plugin's __rulesyncEnforcePreToolUseResult contract.

Verification

  • Pi's tool_call fields (toolName, toolCallId, input), the { block, reason } return, ctx.cwd, ctx.sessionManager.getSessionId(), built-in tool schemas, and MCP tool naming were checked against Pi v1.6.0's docs/extensions.md, docs/mcp.md, src/core/tools/*.ts and src/core/extensions/types.ts; oh-my-pi's tool events against its extensibility/extensions/types.ts.
  • New runtime tests load the generated module and check the payload, name/argument mapping, both matcher spellings, the issue's exit-2 guard, JSON deny / block / allow precedence, timeouts with and without failClosed: false, post-tool payloads, and the empty stdin. The generated module was also type-checked under --strict against a stub of the Pi types.
  • pnpm cicheck passes.

Not changed

  • stop / beforeSubmitPrompt and the other events still get no payload (just an empty stdin); the issue asks only about tool calls.

Closes #3953

🤖 Generated with Claude Code

https://claude.ai/code/session_01JdNNKCxxrKAMDANMGDEGP4

The generated Pi / oh-my-pi extension ran every hook command with an
unclosed, empty stdin, so Claude-style guards reading tool_name /
tool_input never matched and failed open.

- preToolUse / postToolUse / postToolUseFailure commands now receive the
  Claude Code hook input (session_id, cwd, hook_event_name, tool_name,
  tool_input, tool_use_id, plus tool_response / error), with Pi tool
  names mapped to the canonical Bash / Read / Write / Edit / Grep / Glob
  and file tool arguments respelled (path -> file_path, oldText/newText
  -> old_string/new_string). Other commands get an empty stdin.
- Tool matchers are tested against both Pi's and the canonical name, so
  a canonical `Bash` matcher now matches Pi's `bash`.
- tool_call also denies on exit 0 with a JSON deny decision
  (permissionDecision "deny", or the deprecated decision "block"), and
  each command is bounded by its timeout; a timed-out preToolUse denies
  unless failClosed is false, mirroring the OpenCode-style plugin, which
  now honors the deprecated decision "block" too.

Closes #3953

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01JdNNKCxxrKAMDANMGDEGP4
const extraArgument = extra ? `, { ${extra} }` : "";
lines.push(` let ${accessor}Cache: string | undefined;`);
lines.push(
` const ${accessor} = () => (${accessor}Cache ??= toolHookInput(${JSON.stringify(payload)}, event, ctx${extraArgument}));`,
dyoshikawa and others added 4 commits November 25, 2026 11:04
- Write mapped file_path / old_string / new_string last and drop
  same-named keys the model added (also inside each edit), so an extra
  argument cannot make a guard check a different file or text.
- Resolve file_path against the session cwd (Pi's @ / ~ handling), as
  Claude Code always sends an absolute path.
- Join every edit's text into the top-level old_string / new_string.
- Treat permissionDecision "ask" as a ctx.ui.confirm prompt, denying
  without a UI or when the dialog fails.
- Map tool names per dialect: oh-my-pi's find is not Glob.
- Run each command in its own process group and kill the group on
  timeout, so a descendant holding the output open cannot hang the call.
- Document that Pi's powershell tool has no canonical name.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01JdNNKCxxrKAMDANMGDEGP4
…tdown

- Keep oh-my-pi's own old_string / new_string (replace mode and batch
  form) instead of dropping them; only path becomes file_path there.
- Pass oh-my-pi patch-mode {input} edits through and document that
  Claude-style path guards do not see them.
- Normalize Unicode spaces and file:// URLs in file_path like Pi's
  resolveToCwd, leaving other URLs as written.
- Kill hook command groups still running on session_shutdown.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01JdNNKCxxrKAMDANMGDEGP4
- Pi: mirror utils/paths.ts resolvePath (no URL passthrough, Windows
  shell drive paths) so scheme-looking text resolves against cwd.
- oh-my-pi: mirror tools/path-utils.ts expandPath (stray leading ':',
  restricted '@' shorthand, bare root alias) and exempt only its own
  registered internal URL schemes.
- Keep the raw path in tool_input next to file_path.
- Correct the note on hooks outliving an abruptly killed Pi.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01JdNNKCxxrKAMDANMGDEGP4
…-hooks-stdin

# Conflicts:
#	src/generated/docs-content.ts
@dyoshikawa
dyoshikawa merged commit 67d252a into main Nov 25, 2026
10 checks passed
@dyoshikawa
dyoshikawa deleted the resolve-issue-3953-pi-hooks-stdin branch November 25, 2026 03:04
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.

Pi hooks extension: pass the Claude-style hook payload on stdin so existing hook scripts work

2 participants