feat(opencode): fall back to another model on a content-filter refusal

Merged
GO
11d ago
+367-2

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 into ContentFilterError after 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: refusal with no category.

Fix

When a step's stream ends with content-filter and the config opts in, the processor fails the attempt with ContentFilterError after the step's own bookkeeping has run, and the fallback treats that error as a reason to switch. The rest is existing behaviour:

  • No same-model retry. The error is not retryable, so the swap is immediate.
  • Later steps stay on the fallback. The refused model is put on the usual cooldown, and 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.
  • A swap clears the refused attempt's 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.
  • A refusal no route answers is left exactly as today. The processor does not record an error for it. The row keeps finish: "content-filter" and the prompt loop reports ContentFilterError once, 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.
  • Compaction is excluded. A refused summary step is handled as before.

The refused request's cost stays on the assistant row and on its step-finish part.

fallbackOnRefusal is 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

  • The cooldown is per model for the whole sandbox, not per chat. One refused chat moves that user's other chats in the same project, subagents included, to the fallback for the cooldown (5 minutes in production).
  • A refusal that is correct also triggers the swap. The fallback model then applies its own safeguards. That is the price of treating the filter's false positives as recoverable.
  • The status line and the [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 refused finish; 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 one ContentFilterError.

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, retry and prompt files (163 pass, 0 fail) and tsgo --noEmit on packages/opencode. Not run against a live provider or in a sandbox.

Release

Needs v1.18.32-12 per RELEASING.md. andytown then bumps the three opencode pins; the config that turns this on is replohq/andytown#32481. andytown currently pins 1.18.32-10, so that bump also picks up -11.

šŸ¤– Generated with Claude Code