Repository navigation
fix(hooks): show an OpenCode V2 postToolUse command's exit-2 stderr to the model - #4030
Merged
Merged
Conversation
…o the model On the `opencode` (OpenCode 2.x) target, a `postToolUse` command that exits with code 2 now has its sanitized stderr appended to the tool result the model sees (`event.result.output` when it is a string, and `event.result.content` whether it is a string or an array of parts), matching Claude Code's PostToolUse contract. Exit 0 and other codes are still only logged, and stdout still streams to the host. postToolUseFailure stays observe-only, since a plugin cannot replace OpenCode's typed tool error. Document the hook, and that OpenCode 2.x's write/edit tools name the file `path` in `tool_input` (not Claude Code's `file_path`). Closes #4027 Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01JdNNKCxxrKAMDANMGDEGP4
A postToolUse command's stderr is read for its exit-code-2 feedback, so `__rulesyncRunCommand` now writes it to the host's stderr itself: after the command ends, and on the exec path also when it times out (it was dropped there). On the Bun Shell fallback both stdout and stderr reach the host when the command ends. The separate write in the postToolUse helper is gone, so nothing is printed twice. Stringify a structured output kept ahead of the feedback like OpenCode does, falling back to `String` when JSON cannot hold it, and document the stderr cap, the Bun Shell stdout buffering and the exception in the shared execution note. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01JdNNKCxxrKAMDANMGDEGP4
…encode-posttooluse-exit2 # Conflicts: # src/generated/docs-content.ts
This was referenced Nov 30, 2026
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.
Closes #4027
Summary
On the
opencode(OpenCode 2.x) target, apostToolUsecommand that exits with code 2 used to be only logged. It now follows Claude Code'sPostToolUsecontract (https://code.claude.com/docs/en/hooks): the tool already ran, so nothing is blocked, but the command's stderr reaches the model.execute.afterhandler runs eachpostToolUsecommand through a new__rulesyncRunPostToolUseHookhelper. On exit 2 it takes the command's stderr (sanitized with the sharedsanitizeBlockReason, orpostToolUse hook exited with code 2.when there is none) and__rulesyncAppendToolResultreplacesevent.resultwith a copy that hasPostToolUse hook feedback:\n<reason>appended:outputwhen it is a string;content, whether it is a string or an array of parts (a text part is added);contentis missing or empty, upstream treats it as the output, so the output (stringified if structured) goes first, then the feedback.Any failure there is caught and logged: a throwing
execute.afterhook would fail the call.__rulesyncRunCommandgets anecho: "stdout"mode (stdout inherited, stderr read with the existing 1 MiB cap), the mirror of thesessionStartecho: "stderr"mode. The captured stderr is written to the host's stderr after the command ends, so it stays visible there. The Bun Shell fallback path handles the new mode too.postToolUsenote" indocs/reference/file-formats.md(and the regeneratedsrc/generated/docs-content.ts).OpenCode V2 primary sources checked (v2.4.3)
execute.afterevent type:status: "completed"events carry a mutableresult: Tool.Result, andstatus: "error"events carryerror: Tool.Error(packages/plugin/src/promise/tool.ts).Tool.Resultis{ output?, content?: string | Content[], metadata? }(packages/schema/src/tool.ts).adapted.resultback onto the host event (packages/plugin/src/promise/adapter.ts). The host then re-normalizescontentagainstoutputbefore returning it (packages/core/src/tool.ts,normalizeContentinpackages/core/src/tool/runtime.ts).event.result = { ...event.result, content: [...] }(packages/core/src/plugin/tool-input-repair.ts).Design decisions
postToolUseFailurestays observe-only. Claude Code shows its exit-2 stderr too, but in OpenCode 2.x a failed call'sevent.erroris a typed Effect error (ToolFailure/Tool.Error) that the hostyield*s after the hook. The built-in tool-repair plugin constructs a newToolFailure, which a generated JS plugin cannot import. Putting anything else in its place would turn the tool's failure into a defect, so this is documented as a known difference instead.stophelper). This is Claude Code's PostToolUse contract (exit 2 → stderr). It also keeps today's streaming of stdout to the host, which an existing test pins down (50 MB of stdout is not buffered). Capturing stdout for a fallback would buffer it and cap it at 1 MiB.outputandcontent. The reporter confirmed in the issue comment, on 2.4.1, that appending tocontentalone was not enough.tool_inputrenaming. rulesync passestool_input: event.inputthrough unchanged for the OpenCode V2 payloads, and there is no normalization layer to extend. Adding one (e.g. a syntheticfile_path) would be a new payload contract, so this PR only documents the difference. Per the v2.4.1–v2.4.3 sources, both thewriteandedittools name the filepath(write.ts, edit.ts), andpatchtakespatchText. The issue comment saideditusesfilePath, so the docs recommendtool_input.path ?? tool_input.filePath ?? tool_input.file_path.Tests
bun execpath and the Bun Shell fallback:output+content[]result, in a stringcontent, and in an emptycontentwith structuredoutput;postToolUseFailurestays log-only;postToolUseuses the new helper (postToolUseFailureandafterShellExecutionkeep__rulesyncRunHook) and thatsanitizeBlockReasonis defined exactly once.pnpm cicheck: fmt, lint, typecheck and content checks pass.vp testhit the known two-vitest-versions issue, so the full suite ran withpnpm exec vitest run(665 files, all passing).🤖 Generated with Claude Code
https://claude.ai/code/session_01JdNNKCxxrKAMDANMGDEGP4