Repository navigation
fix(hooks): pass Claude Code hook input on stdin to Pi tool hooks - #3963
Merged
Merged
Conversation
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}));`, |
- 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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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_inputnever matched and a guard silently failed open. On top of that, a canonicalBashmatcher was tested only against Pi's lowercasebash, so it never fired at all.What changed
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), plustool_responsefor PostToolUse anderrorfor PostToolUseFailure. It is serialized lazily, once per invocation, like the OpenCode-style plugin'sSTDIN_PAYLOADS. Every other command now gets an empty, closed stdin (previously acatwould hang).bash/read/write/edit/grep→Bash/Read/Write/Edit/Grep; Pi'sfindand oh-my-pi'sglob→Glob(oh-my-pi'sfindis a search tool and stays unmapped). MCP tools are alreadymcp__<server>__<tool>in Pi. Others, including Pi'spowershell, keep their own name.path→ an absolutefile_pathresolved againstctx.cwd(with Pi's@/~handling),edits[].oldText/newText→old_string/new_string, and every edit's text joined into the top-levelold_string/new_string. Mapped keys are written last, and anyfile_path/old_string/new_stringthe 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'sresolveToCwd: Pi v1.6.0utils/paths.ts, which has no URL passthrough, and oh-my-pitools/path-utils.tsat 76b7d84, which strips a stray leading:, accepts@only in some shapes, and leaves only its own internal URL schemes as written. The rawpathis also kept intool_input. oh-my-pi's replace-mode edit already usesold_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.Bashnow matches and existingbashmatchers keep working.tool_call, reusing the OpenCode-style plugin'sPreToolUsecontract: any non-zero exit (exit 2 included) denies with stderr as the reason (unchanged), and exit 0 withhookSpecificOutput.permissionDecision: "deny"denies withpermissionDecisionReason. The deprecated top-leveldecision: "block"that Claude Code still honors is accepted when nopermissionDecisionis 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 withctx.ui.confirm. With no UI (print/JSON modes), or if the dialog fails, the call is denied.timeout(default 600 s, via the sharedhookTimingOf). 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 onsession_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:
tool_callhandler as a block).preToolUsecommand denies the call, unless the hook setsfailClosed: false, which lets it through and logs it — exactly the OpenCode-style plugin's__rulesyncEnforcePreToolUseResultcontract.Verification
tool_callfields (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'sdocs/extensions.md,docs/mcp.md,src/core/tools/*.tsandsrc/core/extensions/types.ts; oh-my-pi's tool events against itsextensibility/extensions/types.ts.failClosed: false, post-tool payloads, and the empty stdin. The generated module was also type-checked under--strictagainst a stub of the Pi types.pnpm cicheckpasses.Not changed
stop/beforeSubmitPromptand 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