Repository navigation
feat(opencode): fall back to another model on a content-filter refusal - #74
Conversation
A refusal ends the stream cleanly, so the fallback never saw it and the chat stayed stuck on a model that keeps refusing its history. With fallbackOnRefusal set in the fallback config, a content-filter finish fails the step and switches models like a provider error. Co-Authored-By: Claude <noreply@anthropic.com>
|
The following comment was made by an LLM, it may be inaccurate: |
| if (value.reason === "content-filter" && SessionFallback.config()?.fallbackOnRefusal) | ||
| return yield* Effect.fail( | ||
| new SessionV1.ContentFilterError({ | ||
| message: "The response was blocked by the provider's content filter", | ||
| }).toObject(), | ||
| ) |
There was a problem hiding this comment.
🔴 Fallback overflow aborts compaction
When the fallback overflows after a refusal, ctx.assistantMessage.finish still says content-filter. The prompt loop reports that refusal before handling the requested compaction, leaving the chat stuck.
Learn more
The refused step stores finish: "content-filter" before the new failure enters the retry policy. A successful fallback overwrites it, but a fallback that fails with context overflow before step-finish leaves it in place. halt requests compaction without setting an assistant error. The prompt loop checks the stale finish before processing result === "compact", so it stops on a refusal instead. A fallback API failure before step-finish also inherits the wrong finish reason.
Example: Sonnet refuses a long history; the configured smaller fallback rejects that history as too long. Instead of compacting and retrying, the turn ends with ContentFilterError attributed to Sonnet.
Recommended fix: Clear and persist the refused attempt's finish when the processor commits to a fallback swap, while retaining the refused attempt's cost and step-finish part. Verify both fallback context overflow and fallback errors before step-finish.
Was this helpful? React with 👍 or 👎 to provide feedback.
There was a problem hiding this comment.
All reported issues were addressed across 5 files
Tip: Get a Slack DM when cubic finishes reviewing your PRs. Connect Slack
View guided diff | Turn on auto-fix | Re-trigger cubic
…p happens A swap now clears the refused attempt's finish, so a fallback that fails before finishing a step is not read as a refusal. A refusal no route answers is left for the prompt loop to report, as with the setting off, and compaction is excluded. Co-Authored-By: Claude <noreply@anthropic.com>
Lets a chat that the provider's content filter refuses carry on with another model, using the fallback we already have for provider outages. Off unless the coordinator's fallback config sets
fallbackOnRefusal: true.Symptom
Since 2026-11-09, Sonnet 5.5 (OpenRouter → Anthropic) refuses about 35 replies a day, up from 3 in the nine days before. In 20 of 22 affected chats the first refusal came 15–60 seconds after an auto-compaction, with 0 output tokens. The chat never recovers: every later message is refused, and the customer sees "Something went wrong with this turn." Linear: USE-2845, earlier REPL-35643.
Tested by hand on one stuck production chat on 2026-11-13: Sonnet 5.5 was refused 3 times out of 3, while Opus 5, Opus 5.5, GPT-6.1 Terra and GPT-5.6 Terra each answered the same history. Going back to Sonnet 5.5 afterwards was refused again.
Cause
The fallback only reacts to errors: a failed request, or an error chunk inside a 200 stream. A refusal is a normal stream that ends with the finish reason
content-filter. The loop turns that intoContentFilterErrorafter the step is over, outside the retry policy, so the fallback never sees it.Why the filter fires is not known. OpenRouter forwards
native_finish_reason: refusalwith no category.Fix
When a step's stream ends with
content-filterand the config opts in, the processor fails the attempt withContentFilterErrorafter the step's own bookkeeping has run, and the fallback treats that error as a reason to switch. The rest is existing behaviour:route()starts each step on the model that last worked. When the cooldown ends, the next step tries the original model once, is refused, and swaps again inside the same step.finish. Otherwise a fallback that fails before finishing a step of its own, for example with a context overflow, would be read as a refusal and the loop would stop without compacting.finish: "content-filter"and the prompt loop reportsContentFilterErroronce, on the same path as with the setting off. That covers a refused fallback, a spent swap budget, a missing target, and a step that already ran a tool, which the fallback never swaps because another model could run the tool again.The refused request's cost stays on the assistant row and on its
step-finishpart.fallbackOnRefusalis optional, so this release changes nothing until the coordinator sends it. Turning it off again is a coordinator deploy, not a fleet-wide opencode downgrade.Trade-offs
[model-fallback]log line read as a provider failure for a refusal too. The two Agent Model Fallback alerts in andytown count that log line, so they need to exclude refusals before this is turned on.Tests
fallback.test.ts: a refusal switches models only when the config opts in.processor-effect.test.ts: a refused step is answered by the fallback with one request each; a fallback that then fails leaves its own error and no refusedfinish; a refused step that ran a tool is not swapped; a refusal no route answers is left for the loop.prompt.test.ts, through the loop: a refused turn is answered with no error event; a fallback that overflows after a refusal compacts and answers; a refusal on both routes reports oneContentFilterError.The overflow and tool cases came out of a review of the first version of this PR, where they failed. Ran the
processor-effect,fallback,retryandpromptfiles (163 pass, 0 fail) andtsgo --noEmitonpackages/opencode. Not run against a live provider or in a sandbox.Release
Needs
v1.18.32-12perRELEASING.md. andytown then bumps the three opencode pins; the config that turns this on is replohq/andytown#32481. andytown currently pins1.18.32-10, so that bump also picks up-11.🤖 Generated with Claude Code