Skip to content

fix(command-code): pair tool results and synthesize missing-result errors - #1411

Merged
Wibias merged 2 commits into
devfrom
ocx-120-provider-compatibility-command-code-returns-502
Aug 10, 2026
Merged

fix(command-code): pair tool results and synthesize missing-result errors#1411
Wibias merged 2 commits into
devfrom
ocx-120-provider-compatibility-command-code-returns-502

Conversation

@Wibias

@Wibias Wibias commented Aug 10, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • Command Code (/alpha/generate) rejects any assistant tool call that has no matching tool result on the continuation, surfacing as a generic 502 (Tool result is missing for tool call <id>). The adapter now keeps that pairing invariant before sending: every declared call is closed by a result, and orphan results degrade to text carriers.
  • Tool results that match a declared assistant call keep the native tool-result wire shape (unchanged); unpaired calls get an explicit error-text result so the upstream never fails the whole turn.
  • The upstream missing-tool-result error is now classified distinctly (errorType: upstream_error, code: missing_tool_result) so the dashboard/logs distinguish provider-side tool-result validation from generic stream failures.

Validation

  • bun test tests/command-code-provider.test.ts — pass (26 tests, incl. new pairing, orphan-carrier, and classification coverage)
  • bun run typecheck — pass
  • bun run privacy:scan — pass
  • Pre-open gate (pre-open-gate.mjs) — ready; bug + security lenses and required probes verified
  • Full bun run test — not fully green on this machine; failures reproduced identically on the base tip (2036ed734) in unrelated subsystems (catalog writer, codex-auth WHAM, restore/journal) and are pre-existing Windows/Bun environment issues, not caused by this change

Review notes

Fixes #1383

Summary by CodeRabbit

  • Bug Fixes
    • Improved handling of assistant tool calls and their results during command processing.
    • Converts unmatched tool results into readable user content.
    • Reports unresolved tool calls with explicit error results instead of leaving requests incomplete.
    • Preserves images included in tool results.
    • Provides clearer error details when upstream responses omit required tool results.

…rors

The /alpha/generate wire requires every assistant tool-call to be closed
by a matching tool-result. Codex history can leave a call unpaired
(interrupted turns, compaction, multi-step rounds), and the upstream
rejects that with 'Tool result is missing for tool call <id>', surfacing
as a generic 502 (#1383).

- Track declared assistant tool-call ids and pair tool results to them.
- Close every declared call that never received a result with an explicit
  error-text tool-result before the next non-tool message.
- Degrade orphan tool results (no declared call) to a user text carrier
  instead of a 400-prone standalone tool message.
- Classify the upstream missing-tool-result error distinctly
  (errorType upstream_error, code missing_tool_result).
@github-actions

Copy link
Copy Markdown
Contributor

Deterministic PR hygiene checks passed.

@github-actions github-actions Bot added the bug Something isn't working label Aug 10, 2026
@coderabbitai

coderabbitai Bot commented Aug 10, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 8d6568a8-fd09-498e-8c0b-857d028ee6ab

📥 Commits

Reviewing files that changed from the base of the PR and between aca2752 and fc1c729.

📒 Files selected for processing (2)
  • src/adapters/command-code.ts
  • tests/command-code-provider.test.ts

📝 Walkthrough

Walkthrough

wireMessages now preserves assistant tool-call/result pairing, synthesizes results for unresolved calls, converts orphan results to user text, buffers images, and classifies upstream missing-tool-result stream errors with dedicated metadata.

Changes

Command Code tool-result handling

Layer / File(s) Summary
Tool-call and result wiring
src/adapters/command-code.ts, tests/command-code-provider.test.ts
The adapter pairs results by call ID, buffers tool-result images, converts orphan results to user text, and synthesizes error results before later messages or completion. Tests cover these cases and preserve contiguous tool results.
Missing-tool-result stream classification
src/adapters/command-code.ts, tests/command-code-provider.test.ts
The adapter recognizes spaced and underscored missing-tool-result errors and emits errorType: "upstream_error" with code: "missing_tool_result". Other errors retain the generic 502 response shape. Tests verify the terminal done event.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Sequence Diagram(s)

sequenceDiagram
  participant Client
  participant CommandCodeAdapter
  participant wireMessages
  participant CommandCodeProvider
  Client->>CommandCodeAdapter: submit conversation
  CommandCodeAdapter->>wireMessages: serialize tool calls and results
  wireMessages-->>CommandCodeAdapter: paired or synthesized tool results
  CommandCodeAdapter->>CommandCodeProvider: send continuation
  CommandCodeProvider-->>CommandCodeAdapter: stream response or missing-tool-result error
  CommandCodeAdapter-->>Client: response or classified stream error
Loading

Possibly related PRs

  • lidge-jun/opencodex#1066: Modifies the same Command Code adapter and tests for related tool-call/result pairing and stream error handling.

Suggested reviewers: ingwannu, lidge-jun

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 60.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly identifies the primary change: pairing tool results and synthesizing errors for missing results.
Linked Issues check ✅ Passed The changes address issue #1383 by pairing tool results, synthesizing missing-result errors, preserving images, and classifying upstream validation failures.
Out of Scope Changes check ✅ Passed All changes support issue #1383, including image buffering needed to preserve tool-result adjacency and targeted regression tests.
✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch ocx-120-provider-compatibility-command-code-returns-502

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/adapters/command-code.ts`:
- Around line 99-112: Update the image-carrier handling in
src/adapters/command-code.ts around lines 99-112 to buffer user image carriers
while pendingCalls is non-empty, flushing them only after every
assistant-declared call has a native or synthesized tool result so tool results
remain contiguous. Add the two-call image-first scenario in
tests/command-code-provider.test.ts around lines 165-183 and assert both
tool-result messages precede the user image carrier.

In `@tests/command-code-provider.test.ts`:
- Around line 360-372: Extend the missing-tool-result coverage in the test using
createCommandCodeAdapter and isMissingToolResultError by adding an upstream
error message containing “tool_result is missing” instead of the spaced variant.
Assert that parsing produces the same errorType “upstream_error” and code
“missing_tool_result” while preserving the existing response behavior.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 8e72932b-8a05-4368-980c-e969d2ff3856

📥 Commits

Reviewing files that changed from the base of the PR and between 4f32280 and aca2752.

📒 Files selected for processing (2)
  • src/adapters/command-code.ts
  • tests/command-code-provider.test.ts

Comment thread src/adapters/command-code.ts
Comment thread tests/command-code-provider.test.ts
CodeRabbit flagged that a paired tool result carrying an image emitted its
user image carrier immediately, breaking the adjacency of remaining pending
tool results on the /alpha/generate wire (assistant -> tool(call_1) -> user
image -> tool(call_2)).

Buffer image carriers while any declared call is still pending and flush
them only after every call has its native or synthesized result. Also add
the underscored tool_result is missing classification test.
@Wibias
Wibias merged commit 0de4fd2 into dev Aug 10, 2026
27 checks passed
@Wibias
Wibias deleted the ocx-120-provider-compatibility-command-code-returns-502 branch August 10, 2026 08:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant