Skip to content

fix(provider): stabilize Pollinations tool calls across protocol dialects - #871

Merged
agenteinstinct1-max merged 2 commits into
dev-vibecodedfrom
fix/pollinations-protocol-stability
Nov 27, 2026
Merged

agenteinstinct1-max merged 2 commits into
dev-vibecodedfrom
fix/pollinations-protocol-stability

Conversation

@agenteinstinct1-max

@agenteinstinct1-max agenteinstinct1-max commented Nov 27, 2026 •

Copy link
Copy Markdown
Collaborator

Issue for this PR

Fixes #870

Type of change

  • Bug fix

What does this PR do?

Some Pollinations backends (observed: community/morriszdweck/glm-5.3-flash-free and community/scriptsnsenses-sys/gpt-6-luna:free, both Poe-routed) return tool calls as text instead of structured tool_calls. The middleware from #867 decoded the nested {"name":"...","arguments":{...}} envelope, but real agent loops still broke:

  1. Flat dialect: the real agent loop emits parameters as top-level keys — {"name":"shell","command":"echo hello-world"} — not nested under arguments. The decoder only accepted the nested shape, so calls leaked as raw JSON text, the command never ran, the model hallucinated the output and the turn died after finish-tool nudges.
  2. An unterminated think block (token cap / disconnect) threw unterminated reasoning block and killed the turn.
  3. An envelope naming a tool that was not offered threw model requested unavailable tool.
  4. All text and protocol reasoning buffered until stream end, so the UI looked frozen.

Changes in packages/opencode/src/provider/pollinations-protocol.ts:

  • Decode both envelope dialects (nested arguments and flat top-level parameters). Promotion still requires the tool to be offered and the content to start (after whitespace) with a think block or an envelope-shaped object, so prose or docs quoting the protocol never execute.
  • Salvage an unterminated leading think block as reasoning instead of throwing; demote unknown-tool envelopes to visible text instead of throwing.
  • Stream plain text and per-block reasoning live; hold real text wrappers until the mode resolves so the emitted part sequence stays clean (no nesting, no double text-end); unique tool call ids across parts; stream and generate share the same classification (generate no longer returns raw content when there is no tool call, so reasoning tags no longer leak).

Deliberate limitation: text before the protocol (a preamble like Sure! <think>...) stays ordinary text and never executes — a preamble cannot be distinguished deterministically from documentation quoting the protocol. The raw protocol stays visible in that case.

How did you verify your code works?

  • bun test test/provider/pollinations-protocol.test.ts: 17/17, including new regression tests for salvage, demote, false-promotion cases, chunk-boundary whitespace parity vs batch, progressive reasoning, clean stream shape for demoted tools, unique ids across parts, the flat dialect, and no-tool generate decoding.
  • Fixture matrix from real API captures (GLM round 1+2, mistral native, truncation, unknown tool, preamble, prose-with-JSON, plain text): 9/9 through the real @ai-sdk/openai-compatible stack against a local SSE replay; a two-round SDK tool loop round-trips the synthetic tool-call id.
  • Live headless runs (opencode run, this build, live API, free models only, no nudges): glm-5.3-flash-free echo task executes and finishes cleanly (before this PR: raw JSON text, hallucinated output, turn killed); same model on a multi-step write-file / read-back / verify task passes; repeat echo passes; gpt-6-luna:free echo passes; Saauf/mistral-large-4 (native structured path, control) passes with no regression.

Interactive TUI was not verifiable from the Linux sandbox used for this work (the TUI boots under a pty but frames/input do not materialize there); the validated run path shares the same provider wiring. An interactive check on a real terminal is still worthwhile.

Screenshots / recordings

N/A — provider middleware, no UI change.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

…ects

Models routed through some Pollinations backends (observed: GLM-5.3-Flash
and GPT-6-Luna via Poe) return tool calls as text. The merged middleware
already decoded the nested {"name","arguments"} envelope, but real agent
loops still broke:

- Turns died with "unterminated reasoning block" when a think block was
  truncated; now salvaged as reasoning.
- Turns died when an envelope named an unoffered tool; now demoted to
  visible text instead of throwing.
- The flat dialect seen in real agent loops ({"name":"shell","command":...})
  never decoded, so calls leaked as raw JSON and the shell never ran; both
  dialects now decode, with promotion still requiring an offered tool and
  the anchored protocol scope (content must start with a think block or an
  envelope, so quoted examples in prose are never executed).
- All text was buffered until stream end; plain text now streams live and
  protocol reasoning emits as each think block closes.
- Stream event shape is clean: real text wrappers are held until the mode
  resolves, synthetic parts are sequential, tool call ids are unique across
  parts, and stream/generate classification uses the same anchored rule.

Known limitation (deliberate): text before the protocol (a preamble) stays
ordinary text, because a preamble cannot be distinguished deterministically
from documentation that quotes the protocol.

Validated with 17 provider tests, a fixture matrix from real API captures,
an SDK-level two-round tool loop, and a live headless agent run in which
glm-5.3-flash-free executed echo hello-world through the shell tool and
finished cleanly.
@github-actions

Copy link
Copy Markdown

Thanks for your contribution!

This PR doesn't have a linked issue. All PRs must reference an existing issue.

Please:

  1. Open an issue describing the bug/feature (if one doesn't exist)
  2. Add Fixes #<number> or Closes #<number> to this PR description

See CONTRIBUTING.md for details.

@github-actions

Copy link
Copy Markdown

Thanks for updating your PR! It now meets our contributing guidelines. 👍

if (keys === "arguments,name") return isRecord(value.arguments)
// Flat dialect observed in the real agent loop (e.g. GLM calling shell):
// {"name":"shell","command":"pwd"} instead of nesting under "arguments".
return !("arguments" in value)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

SUGGESTION: Flat-dialect envelope matching is overly broad and can promote non-tool JSON as tool calls

isEnvelopeShape treats any object with a string name and no arguments key as a flat-dialect tool call (line 406). If such an object appears at the start of content and a same-named tool is offered, it is promoted and executed -- for example {"name":"read","path":"./env"} could invoke a read tool. Promotion still requires an offered tool, but the broad match raises false-positive risk. Consider narrowing the flat-dialect match to expected parameter shapes, or documenting the accepted risk.

@kilo-code-bot

kilo-code-bot Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

Code Review Summary

Status: No Issues Found | Recommendation: Merge

Files Reviewed (2 files)
  • packages/opencode/src/provider/pollinations-protocol.ts - 0 issues
  • packages/opencode/test/provider/pollinations-protocol.test.ts - 0 issues
Previous Review Summary (commit a56e1ad)

Current summary above is authoritative. Previous snapshots are kept for context only.

Previous review (commit a56e1ad)

Status: 1 Issues Found | Recommendation: Address before merge

Overview

Severity Count
CRITICAL 0
WARNING 0
SUGGESTION 1
Issue Details (click to expand)

SUGGESTION

File Line Issue
packages/opencode/src/provider/pollinations-protocol.ts 406 Flat-dialect envelope matching is overly broad and can promote non-tool JSON as tool calls
Files Reviewed (2 files)
  • packages/opencode/src/provider/pollinations-protocol.ts - 1 issue
  • packages/opencode/test/provider/pollinations-protocol.test.ts - reviewed for context, no issues

Fix these issues in Kilo Cloud


Reviewed by free · Input: 102.9K · Output: 32K · Cached: 429.3K

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant