Skip to content

fix(converters): batch consecutive tool results before emitting images - #84

Merged
patlux merged 2 commits into
patlux:mainfrom
Star-233:fix/converters-batch-tool-results
Sep 15, 2026
Merged

patlux merged 2 commits into
patlux:mainfrom
Star-233:fix/converters-batch-tool-results

Conversation

@Star-233

@Star-233 Star-233 commented Sep 5, 2026

Copy link
Copy Markdown
Contributor

Why

When an assistant turn issues multiple parallel tool calls that include images (for example, reading multiple preview screenshots in Oh My Pi), the generate transport fails with:

Error: Tool result is missing for tool call <call_id>.: server_error

Cause

In src/converters.ts, messagesToCC() iterated through messages one by one. For each toolResult containing an image, it pushed the role: "tool" entry and immediately pushed a role: "user" message with the image.

When an assistant turn contains multiple parallel tool calls:

  1. assistant (call_1, call_2)
  2. tool (call_1 result)
  3. user (call_1 image)
  4. tool (call_2 result)

The Command Code /alpha/generate gateway closes the tool turn once it sees the user message. Because call_2 result has not arrived yet, the gateway considers call_2 unanswered and aborts with server_error.

Fix

Batch consecutive toolResult messages before emitting image content:

  • Group all consecutive toolResult messages and emit their role: "tool" entries first.
  • Collect all images across that batch.
  • Emit a single following role: "user" message with the accumulated images only after all tool results are in place.

This matches the pattern used by @earendil-works/pi-ai and oh-my-pi.

Verification

  • npm run typecheck — clean
  • npm run test:unit — 50 tests pass, including new regression test in tests/test-pure-functions.ts
  • npm run format:check — clean
  • git diff --check — clean
  • Tested live against Qwen/Qwen3.8-Flash via /alpha/generate with parallel image reads; response completed with status 200 without missing tool call errors.

@Star-233
Star-233 requested a review from patlux as a code owner September 5, 2026 09:18
When an assistant turn contains multiple parallel tool calls (such as reading
multiple images), messagesToCC previously emitted a user message with the image
immediately after each individual tool result.

This interleaved a user message into the middle of the tool-result sequence.
The Command Code generate transport closes the tool turn upon seeing a user
message and rejects the request with "Tool result is missing for tool call <id>."

Batch consecutive tool results first, then emit the attached images in a single
following user message.
@patlux

patlux commented Sep 15, 2026

Copy link
Copy Markdown
Owner

Integrated main and added a mixed image/text/error parallel-result regression. Both image-ordering tests fail against the main converter and pass with this implementation. Full npm test passes with real Pi and OMP, plus format/diff checks. Waiting for CI.

@patlux
patlux merged commit e1513ea into patlux:main Sep 15, 2026
13 checks passed
@patlux

patlux commented Sep 15, 2026

Copy link
Copy Markdown
Owner

Shipped in pi-commandcode-provider@0.7.0: https://github.com/patlux/pi-commandcode-provider/releases/tag/v0.7.0 . Public npm package model discovery and GOAT live smoke verified.

@Star-233
Star-233 deleted the fix/converters-batch-tool-results branch September 15, 2026 07:49
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.

2 participants