Skip to content

fix(hooks): show an OpenCode V2 postToolUse command's exit-2 stderr to the model - #4030

Merged
dyoshikawa merged 3 commits into
mainfrom
resolve-issue-4027-opencode-posttooluse-exit2
Nov 30, 2026
Merged

dyoshikawa merged 3 commits into
mainfrom
resolve-issue-4027-opencode-posttooluse-exit2

Conversation

@dyoshikawa

Copy link
Copy Markdown
Owner

Closes #4027

Summary

On the opencode (OpenCode 2.x) target, a postToolUse command that exits with code 2 used to be only logged. It now follows Claude Code's PostToolUse contract (https://code.claude.com/docs/en/hooks): the tool already ran, so nothing is blocked, but the command's stderr reaches the model.

  • The generated execute.after handler runs each postToolUse command through a new __rulesyncRunPostToolUseHook helper. On exit 2 it takes the command's stderr (sanitized with the shared sanitizeBlockReason, or postToolUse hook exited with code 2. when there is none) and __rulesyncAppendToolResult replaces event.result with a copy that has PostToolUse hook feedback:\n<reason> appended:
    • to output when it is a string;
    • to content, whether it is a string or an array of parts (a text part is added);
    • when content is 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.after hook would fail the call.
  • Exit 0, other non-zero codes, timeouts and spawn failures stay log-only, as before.
  • stdout still streams to the host as it did. The shared __rulesyncRunCommand gets an echo: "stdout" mode (stdout inherited, stderr read with the existing 1 MiB cap), the mirror of the sessionStart echo: "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.
  • Docs: a new "OpenCode V2 postToolUse note" in docs/reference/file-formats.md (and the regenerated src/generated/docs-content.ts).

OpenCode V2 primary sources checked (v2.4.3)

Design decisions

  • postToolUseFailure stays observe-only. Claude Code shows its exit-2 stderr too, but in OpenCode 2.x a failed call's event.error is a typed Effect error (ToolFailure / Tool.Error) that the host yield*s after the hook. The built-in tool-repair plugin constructs a new ToolFailure, 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.
  • stderr only, no stdout fallback (unlike the stop helper). 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.
  • Append to both output and content. The reporter confirmed in the issue comment, on 2.4.1, that appending to content alone was not enough.
  • No tool_input renaming. rulesync passes tool_input: event.input through unchanged for the OpenCode V2 payloads, and there is no normalization layer to extend. Adding one (e.g. a synthetic file_path) would be a new payload contract, so this PR only documents the difference. Per the v2.4.1–v2.4.3 sources, both the write and edit tools name the file path (write.ts, edit.ts), and patch takes patchText. The issue comment said edit uses filePath, so the docs recommend tool_input.path ?? tool_input.filePath ?? tool_input.file_path.
  • No opt-in key: it is a bug fix to match Claude Code. Commands that exit 0 or with another code behave exactly as before. The one visible change for those commands is that their stderr reaches the host when the command ends instead of while it runs.

Tests

  • A runtime test under real Bun covers both the bun exec path and the Bun Shell fallback:
    • exit-2 feedback lands in an output + content[] result, in a string content, and in an empty content with structured output;
    • an exit 2 with no stderr gets the fixed fallback message;
    • control characters are stripped;
    • exit 0 and exit 1 leave the result unchanged;
    • postToolUseFailure stays log-only;
    • stdout still reaches the host.
  • A generation test checks that only postToolUse uses the new helper (postToolUseFailure and afterShellExecution keep __rulesyncRunHook) and that sanitizeBlockReason is defined exactly once.
  • pnpm cicheck: fmt, lint, typecheck and content checks pass. vp test hit the known two-vitest-versions issue, so the full suite ran with pnpm exec vitest run (665 files, all passing).

🤖 Generated with Claude Code

https://claude.ai/code/session_01JdNNKCxxrKAMDANMGDEGP4

dyoshikawa and others added 3 commits November 30, 2026 22:47
…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
@dyoshikawa
dyoshikawa merged commit 8ae8bbc into main Nov 30, 2026
10 checks passed
@dyoshikawa
dyoshikawa deleted the resolve-issue-4027-opencode-posttooluse-exit2 branch November 30, 2026 14:20
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.

OpenCode V2 hooks: postToolUse exit code 2 stderr never reaches the model (Claude Code parity)

1 participant