fix(ai): degrade unsupported hosted tool history on provider switches by rekram1-node · Pull Request #57256 · anomalyco/opencode · GitHub
Skip to content

fix(ai): degrade unsupported hosted tool history on provider switches - #57256

Open
rekram1-node wants to merge 2 commits into
devfrom
hosted-fallback
Open

rekram1-node wants to merge 2 commits into
devfrom
hosted-fallback

Conversation

@rekram1-node

Copy link
Copy Markdown
Collaborator

Problem

Switching providers after a provider-executed tool can make the session impossible to continue: the destination rejects inline tool results, or Anthropic rejects an unfamiliar server-tool name.

Fixes #57044. Fixes #53633.

Change

Normalize unsupported hosted history during protocol lowering, without changing stored sessions or public schemas.

hosted call + result
  destination can replay natively? → preserve native blocks
  otherwise                       → readable text + supported media
  • Degrade both sides of the pair, so no dangling client-tool call remains.
  • Preserve inputs, readable output and errors; hoist file attachments into user media after any local tool answers.
  • Anthropic retains its supported server tools, including direct native JSON results without replay metadata.
  • Apply the fallback in OpenAI Chat, Anthropic, Gemini, Bedrock Converse, Mistral, Cohere and Google Interactions. Shared protocols cover their compatible providers too.

Out of scope

OpenAI Responses hosted items being stored as JSON text rather than replayed as native items. No tool enabling or schema cleanup is included.

Verification

  • Reproduced failures on unmodified dev with the new regression cases.
  • packages/ai: bun test --timeout 30000 — 1,852 pass, 28 skip, 0 fail.
  • bun test test/hosted-history.test.ts --rerun-each 10 — 290 pass. Covers seven destination protocols, errors, mixed local tools, images, supported Anthropic native blocks and incompatible same-name search results.
  • Root bun run check passes.
  • Live Anthropic and OpenAI Chat requests both continued successfully from foreign hosted-tool history and returned the expected verification token.

@opencode-agent opencode-agent 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.

The fix works. I ran the #57044 reproduction script in packages/core and a protocol-level Anthropic reproduction for #53633 on both checkouts. On the base, OpenAI Chat fails to compile the foreign hosted-tool history and Anthropic rejects unknown server-tool names. With this PR both compile, and the hosted call and result become readable text with no dangling client tool call. All packages/ai tests pass and bun run typecheck is clean.

Public API is unchanged: hostedToolHistory is only added to the internal ProviderShared helpers, and stored sessions and schemas are untouched.

One minor inline note about how the fallback text reads on OpenAI Chat-style protocols. Otherwise the approach looks sound: the fallback lives in protocol lowering, both halves of the pair are degraded by id, and Anthropic keeps its native blocks only when it can actually replay them.

Comment thread packages/ai/src/protocols/shared.ts Outdated

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.

OpenAI Chat (and other protocols that join assistant text parts with "") runs these fallback parts together with the text around them. With the #57044 repro the assistant content becomes Provider tool web_search called with: {"query":"Effect"}Provider tool web_search returned:\nFound itDone. Ending each fallback text part with a newline (or wrapping it in blank lines) would keep the history readable for the model.

This branch has not been deployed

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

Labels

None yet

Projects

None yet

1 participant