Repository navigation
fix(agents): enforce message_stop for every model, not just Fable-5 - #143
Conversation
…ENG-18472] anthropic-transport-stream.ts only enforced message_stop when a Fable-5 refusal buffer was active. For every Sonnet/Opus/Haiku model — i.e. all Bedrock/boon-llm-gateway traffic — a stream that ended at a clean EOF without message_stop finalized as stopReason:"stop", byte-identical to a legitimate short answer. This is the exact shape a dropped upstream connection produces (observed with the gateway/Bedrock path): a partial answer streamed, then nothing. - Drop the refusalBuffer gate so message_stop is enforced unconditionally, matching the legacy src/llm/providers/anthropic.ts provider's existing behavior (sawMessageStart || requireMessageStop). Mirrored in extensions/amazon-bedrock/stream.runtime.ts for the direct-to-Bedrock path (same defect class). - embedded-agent-helpers/failover-matches.ts: added a message pattern so the resulting error classifies as "timeout" (failover-eligible) instead of "unclassified" — without it the truncation would classify the same way ENG-16815 found and dead-end the customer's turn instead of walking agents.defaults.model.fallbacks. Added regression tests in all 4 relevant suites and fixed 4 pre-existing test fixtures that were (incorrectly) missing message_stop in their SSE bodies.
|
Claude encountered an error —— View job I'll analyze this and get back to you. |
There was a problem hiding this comment.
1 issue found across 7 files
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="extensions/amazon-bedrock/stream.runtime.ts">
<violation number="1" location="extensions/amazon-bedrock/stream.runtime.ts:289">
P1: According to linked Linear issue ENG-18472, suspiciously short or empty Bedrock completions must fail and enter retry/failover instead of reaching the caller. This guard validates only the terminal marker, so a dropped/degraded response that includes `messageStop` with no usable content—or only a token—still emits `done`; add provider-safe content validation while allowing legitimate tool-call and thinking-only responses.</violation>
</file>
Heads up: you’re close to your included review allowance. Set a flex budget so reviews don’t pause.
Re-trigger cubic
Remove Linear ticket references from code/test comments per repo convention (AGENTS.md) — cubic-dev-ai flagged 8 spots where "ENG-18472" had leaked into inline comments and test/describe titles. Kept the explanatory rationale, trimmed one overly-long comment block to the repo's 1-3 line convention.
There was a problem hiding this comment.
Solid fix. The old refusalBuffer && !sawMessageStop gate on src/agents/anthropic-transport-stream.ts was Fable-5-only — every Sonnet/Opus/Haiku turn on Bedrock/gateway silently accepted a truncated stream as stopReason: 'stop'. Unconditional enforcement is right, and mirroring it in extensions/amazon-bedrock/stream.runtime.ts closes the direct-to-Bedrock twin. The \bstream ended before message_?stop\b matcher in failover-matches.ts is precise and correctly bucketed as timeout, and the end-to-end classify → resolveRunFailoverDecision → fallback_model test mirrors the ENG-16815 shape well. Fixture updates (the 4 pre-existing suites + the shared default mock) are strictly required by the new invariant, not incidental churn. CI shards are all green, review threads resolved.
Two follow-up thoughts, non-blocking:
packages/ai/src/transports/anthropic-transport-stream.tsstill gates the same check onisDirectAnthropicModel(model)(line ~1721) — same defect class on the sibling transport if that path is on any live provider surface. Worth a quick trace / follow-up PR.- cubic's P1 about 'messageStop with empty/token-only content' is genuinely a different bug — narrowly correct to leave for a follow-up; upstream detection lives with boon-llm-gateway#86.
Ship it.
What Problem This Solves
anthropic-transport-stream.tsonly enforcedmessage_stopwhen a Fable-5refusal buffer was active. For every Sonnet/Opus/Haiku model — i.e. all
Bedrock/boon-llm-gateway traffic — a stream that ended at a clean EOF
without
message_stopfinalized asstopReason: "stop", byte-identical toa legitimate short answer, and the partial text got persisted into session
history as a completed turn.
This is one half of ENG-18472: boon-llm-gateway was reporting a dropped
Bedrock connection as a "successful" completion (fixed in
boon-llm-gateway#86),
but even a truncated stream that slips through some other path should never
be silently accepted as a finished, correct answer on the client side either.
Why This Change Was Made
The legacy
src/llm/providers/anthropic.tsprovider already enforces thiscorrectly (
sawMessageStart || requireMessageStop) — the transport rewritenarrowed the check to the Fable-5 refusal-buffer case only, dropping that
disjunct.
refusalBuffer &&gate inanthropic-transport-stream.tssomessage_stopis enforced unconditionally. The throw flows through theexisting
failTransportStream→stopReason: "error"path, the samemechanism ENG-16815 already validated in production for a different
trigger (a 5xx status) — no new persistence/replay behavior.
extensions/amazon-bedrock/stream.runtime.ts(OpenClaw's direct-to-Bedrockprovider plugin) — same defect class.
embedded-agent-helpers/failover-matches.ts: added a message pattern(
/\bstream ended before message_?stop\b/i) so the resulting errorclassifies as
"timeout"(failover-eligible) instead of"unclassified".Without this, per ENG-16815's own finding, the error would route to
continue_normal(no failover) instead of walkingagents.defaults.model.fallbacks— dead-ending the customer's turn eventhough a fallback chain is configured.
User Impact
now correctly triggers the configured model-fallback chain instead of
persisting a one-word answer as if it were the full response.
message_stopis required from every provider already, this only closes the gap where
it was silently optional for non-Fable models.
Evidence
anthropic-transport-stream.test.ts(non-Fabletruncation-throws + regression that a real
message_stopstill completesnormally),
extensions/amazon-bedrock/stream.runtime.test.ts(same pairfor the Bedrock Converse path),
embedded-agent-helpers.isbillingerrormessage.test.ts(classifier now returns
"timeout"for both the Anthropic and Bedrockerror-message shapes),
embedded-agent-runner/run/failover-policy.test.ts(end-to-end classify →
resolveRunFailoverDecision→fallback_model,mirroring the existing ENG-16815 test block).
anthropic-transport-stream.test.ts(plus the file's shared default mock response) that were missing a real
message_stopevent — they happened to pass before only because nothingchecked for it.
node scripts/run-vitest.mjsgreen on every directly touched and adjacentsuite (
anthropic-transport-stream.test.ts80/80,extensions/amazon-bedrock/stream.runtime.test.ts22/22,embedded-agent-helpers/failover-matches.test.ts+embedded-agent-runner/run/failover-policy.test.ts+embedded-agent-helpers.isbillingerrormessage.test.ts221/221 combined).tsgo:coreandtsgo:extensions(non-test lanes) both clean; the*-testtsgo lanes hit local heavy-check lock contention in this worktreeand are worth a follow-up CI check.
Linear: ENG-18472
Summary by cubic
Enforces the message_stop terminator for all Anthropic and Bedrock streams so truncated outputs no longer finalize as successful answers. Previously only Fable-5 enforced this; now a missing terminator throws, classifies as a timeout, and triggers the configured model fallback.
Written for commit c9761b7. Summary will update on new commits.