Repository navigation
Description
Problem
A provider can stop a response with a content filter or a refusal. Many Zoo Code providers ignore this stop reason. Zoo Code then reports an empty response, or accepts partial text as a complete response.
Context
This affects users when a provider filters or refuses a response. A user reported it with OpenRouter: OpenRouter showed content_filter, and Zoo Code showed "The language model did not provide any assistant messages". With auto-approval on, Zoo Code sends the same request again.
Reproduction steps
- Use the OpenRouter provider.
- Send a request that the content filter stops.
- Compare the finish reason in OpenRouter with the message in Zoo Code.
A mocked OpenRouter stream gives the same result without a live request.
Expected result
Zoo Code says that a content filter or a refusal stopped the response. It asks the user before it sends the request again.
Actual result
- No text: Zoo Code shows the empty-response error. With auto-approval, it retries with backoff. We found no retry limit in this branch.
- Partial text: Zoo Code accepts the text as a normal response.
Version
3.98.3 source (main). Reported provider: OpenRouter.
Cause
OpenRouterHandler.createMessage reads finish_reason, but acts only on tool_calls.
Other providers
We found these results in a code search and a code read. We replayed only OpenRouter. We did not check which services send these values.
| API style | Providers | Filtered or refused response |
|---|---|---|
| Chat Completions | OpenRouter, OpenAI and OpenAI Compatible, DeepSeek, Moonshot, Kimi Code, MiMo, Baseten, Fireworks, Friendli, SambaNova, Z.ai, LiteLLM, Requesty, Unbound, Kenari, NanoGPT, Vercel AI Gateway, Zoo Gateway, LM Studio, Qwen Code, Opencode Go (this route) | Zoo Code ignores content_filter. |
| Anthropic Messages | Anthropic, Anthropic on Vertex, MiniMax, Opencode Go (this route) | Zoo Code ignores the refusal stop reason. |
| Bedrock | Amazon Bedrock | Zoo Code handles max_tokens, but not content_filtered or guardrail_intervened. |
| Gemini | Gemini, Vertex | A finishReason such as SAFETY causes no error. Zoo Code does not read blockReason. |
| OpenAI Responses, own parser | OpenAI Native, OpenAI Codex | Zoo Code does not act on response.incomplete. Refusal text shows in chat. |
| OpenAI Responses, shared parser | OpenAI Responses route, xAI, Opencode Go (this route) | An error names the reason. It uses the generic retry path, so Zoo Code can send the request again without a question. |
| Other | Mistral, Ollama, Poe | Zoo Code does not read a stop reason. |
| VS Code LM | VS Code LM, GitHub Copilot | We found no stop reason to read. The behavior is unknown. |
Constraints for the fix
These constraints apply to the task loop, so they apply to each provider.
A new error that the handler throws is not enough. We checked each item below with a TLA+ model (StreamStopRetry) and with a temporary test on main. The test stubs the provider stream.
| # | Constraint | Evidence on main |
|---|---|---|
| 1 | Keep the usage of the stopped response. | recursivelyMakeClineRequests reads the next chunk before it processes the current chunk. A throw after the usage chunk records 0 input and 0 output tokens. With one more chunk before the throw, it records 1234 and 5. |
| 2 | Do not use the generic mid-stream error path. | It sends the request again with no question, even when auto-approval is off. |
| 3 | Handle an error that arrives before the first chunk. | attemptApiRequest retries it automatically under auto-approval. |
| 4 | Do not copy retryAttempt: 0 from the output-limit retry. |
The retry adds a second user turn. History validation changes the second tool result into a text block, so the tool output appears twice. This replay gave no duplicate-ID error. |
Related defects on main
Constraints 1 and 4 are also current defects of OutputTokenLimitError:
- A confirmed output-limit retry adds a second user turn. An existing test in
Task.spec.tsexpectsretryAttemptvalues[0, 0], but does not check the history. - A throw directly after the usage chunk loses the usage. Amazon Bedrock throws in this order. We did not replay the real Bedrock handler.
Suggested plan
- First PR: fix the task loop. Add one shared check for Chat Completions stop reasons, and start with OpenRouter. Move the shared Responses parser to the new error.
- Follow-up PRs: Anthropic Messages, Bedrock, Gemini, and the two own Responses parsers. Confirm the behavior of each with a recorded or mocked stream.
The model covers the task loop. Its provider side uses only the OpenRouter chunk order. Check the chunk order of each other API style.
Acceptance criteria for the first PR
- A filtered response with no text shows a content filter message.
- A filtered response with partial text is not accepted as complete.
- Zoo Code does not send a filtered request again without user consent. This applies with auto-approval on and off.
- A filtered response keeps its token usage and cost.
- A confirmed retry does not add a second user turn.
- A normal response and a normal empty response behave as before.
- Tests cover no text, reasoning only, partial text, and no usage chunk.
- The
StreamStopRetrymodel and a property test come with the fix.
Not yet covered
- A filtered response that contains a tool call. The model does not cover it. Check it during the fix.
- Related: Duplicate tool results after empty-response retries, history reload, or repeated flushes #2691 and [Fix] Session history sends duplicate tool results after an empty-response retry #2701 fixed duplicate tool results after an empty-response retry.
- Related: bug: processToolCalls() ignores finish_reason "length" and stream-end without finish_reason, causing missing nativeArgs #425 reports that
finish_reasonlengthis ignored. A shared stop reason check can help with both.
Activity
[Fix] Content filter stops show as empty or complete responses (#2769)