fix(core): safely recover malformed tool input#37698
Merged
Merged
Conversation
github-actions Bot
pushed a commit
to ReStranger/opencode
that referenced
this pull request
Jul 19, 2026
* upstream/v2: mini: fix shell tool output display (anomalyco#37711) fix(core): detach disposed MCP registrations from root scope (anomalyco#37660) fix(core): preserve the first terminal failure (anomalyco#37705) fix(core): continue after malformed tool input (anomalyco#37701) fix(core): safely recover malformed tool input (anomalyco#37698) fix(simulation): render screenshot symbol glyphs (anomalyco#37691) fix(core): authorize relative external paths (anomalyco#37689) fix(tui): auto-approve permissions in auto mode feat(core): allow MCP Code Mode opt-out (anomalyco#37681) fix(codemode): stop leaking undefined into tool arguments (anomalyco#37652) fix(tui): style interrupted compaction neutrally (anomalyco#37655) fix(cli): harden managed service election (anomalyco#37645)
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.
What
Recover safely when a model emits malformed JSON for a local tool call.
The malformed call is recorded as failed and
executed: false, valid siblings settle normally, the originating Step remains truthfully failed, and V2 may issue one new logical repair Step from freshly projected history. Raw malformed input remains diagnostic-only; the model receives a protocol-valid{}call paired with a concise error asking it to retry with valid JSON.Before / After
Before: Native protocol adapters failed the provider stream while finalizing malformed local arguments, which could discard valid parallel calls. The generic AI SDK adapter instead converted invalid JSON into a string input that could reach local tool dispatch. Neither path provided a safe, bounded semantic continuation.
After: Adapters emit a non-executable
tool-input-erroronly for malformed local calls. Core durably closes that call, waits for valid siblings, records the failed Step and its snapshot metadata, then reloads history for at most one corrective model request. Interruptions, later provider failures, infrastructure failures, and malformed provider-executed calls remain terminal.How
packages/ai:ToolStreamfinalizes parallel calls independently and emits diagnostictool-input-errorevents for malformed local input. OpenAI Responses and Bedrock preserve the correcttool-callsfinish classification; malformed hosted Anthropic input still fails terminally.packages/core/src/aisdk.ts: the generic AI SDK route now uses the same typed JSON parser instead of treating malformed JSON as executable string input.packages/core/src/session/runner: the publisher records a failed, unexecuted tool with safe model feedback. The runner waits for siblings, preserves terminal error precedence, and permits one monotonic repair continuation.packages/schemaand the message projector: failed Steps retain end snapshots and changed-file paths, so successful file-changing siblings remain revertible.Scope
packages/opencode.Testing
cd packages/ai && bun run test: 343 passed, 29 skipped.cd packages/core && bun run test test/session-runner.test.ts test/session-runner-message.test.ts test/session-runner-tool-events.test.ts test/aisdk.test.ts: 167 passed.bun turbo typecheck --concurrency=3: 32 tasks passed across the 39-package scope.oxlint: 0 errors; existing warnings remain.git diff --check: passed.webfetchtimeout, an extra configuredexecutetool, and user-levelcmuxplugins appearing in an isolation assertion.Flow
sequenceDiagram participant Provider participant Adapter participant Runner participant Tools participant Store participant Model Provider->>Adapter: malformed local arguments + valid siblings Adapter-->>Runner: tool-input-error (raw diagnostic only) Adapter-->>Runner: valid tool-call events Runner->>Tools: execute valid local siblings Tools-->>Runner: settle success or failure Runner->>Store: failed tool (executed=false) Runner->>Store: failed Step + snapshot/files Runner->>Model: new Step with {} call + safe error Note over Runner,Model: At most one repair continuation