Skip to content

fix(agents): enforce message_stop for every model, not just Fable-5 - #143

Merged
convyusboy merged 2 commits into
boonfrom
ridho/eng-18472-enforce-message-stop-truncated-completions
Aug 21, 2026
Merged

convyusboy merged 2 commits into
boonfrom
ridho/eng-18472-enforce-message-stop-truncated-completions

Conversation

@convyusboy

@convyusboy convyusboy commented Aug 21, 2026 •

Copy link
Copy Markdown
Collaborator

What Problem This Solves

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, 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.ts provider already enforces this
correctly (sawMessageStart || requireMessageStop) — the transport rewrite
narrowed the check to the Fable-5 refusal-buffer case only, dropping that
disjunct.

  • Drop the refusalBuffer && gate in anthropic-transport-stream.ts so
    message_stop is enforced unconditionally. The throw flows through the
    existing failTransportStream → stopReason: "error" path, the same
    mechanism ENG-16815 already validated in production for a different
    trigger (a 5xx status) — no new persistence/replay behavior.
  • Mirrored the identical one-line fix in
    extensions/amazon-bedrock/stream.runtime.ts (OpenClaw's direct-to-Bedrock
    provider plugin) — same defect class.
  • embedded-agent-helpers/failover-matches.ts: added a message pattern
    (/\bstream ended before message_?stop\b/i) so the resulting error
    classifies 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 walking
    agents.defaults.model.fallbacks — dead-ending the customer's turn even
    though a fallback chain is configured.

User Impact

  • A truncated stream (dropped connection, any provider on this transport)
    now correctly triggers the configured model-fallback chain instead of
    persisting a one-word answer as if it were the full response.
  • No change to any stream that completes normally — a real message_stop
    is required from every provider already, this only closes the gap where
    it was silently optional for non-Fable models.

Evidence

  • New tests: anthropic-transport-stream.test.ts (non-Fable
    truncation-throws + regression that a real message_stop still completes
    normally), extensions/amazon-bedrock/stream.runtime.test.ts (same pair
    for the Bedrock Converse path), embedded-agent-helpers.isbillingerrormessage.test.ts
    (classifier now returns "timeout" for both the Anthropic and Bedrock
    error-message shapes), embedded-agent-runner/run/failover-policy.test.ts
    (end-to-end classify → resolveRunFailoverDecision → fallback_model,
    mirroring the existing ENG-16815 test block).
  • Fixed 4 pre-existing test fixtures in anthropic-transport-stream.test.ts
    (plus the file's shared default mock response) that were missing a real
    message_stop event — they happened to pass before only because nothing
    checked for it.
  • node scripts/run-vitest.mjs green on every directly touched and adjacent
    suite (anthropic-transport-stream.test.ts 80/80,
    extensions/amazon-bedrock/stream.runtime.test.ts 22/22,
    embedded-agent-helpers/failover-matches.test.ts +
    embedded-agent-runner/run/failover-policy.test.ts +
    embedded-agent-helpers.isbillingerrormessage.test.ts 221/221 combined).
    tsgo:core and tsgo:extensions (non-test lanes) both clean; the
    *-test tsgo lanes hit local heavy-check lock contention in this worktree
    and 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.

  • Unconditionally require message_stop in the Anthropic transport and Bedrock direct stream; missing terminator returns stopReason:"error".
  • Add a failover pattern for “stream ended before message[_]stop” so truncations classify as "timeout" (failover-eligible).
  • Add regression tests for both transports; update fixtures to include a real terminator; no change to streams that complete normally.
  • Cleanup: remove Linear ticket references from code and test comments per repo convention.
  • Linear ENG-18472: detects dropped mid-stream connections and activates fallback instead of persisting partial replies.

Written for commit c9761b7. Summary will update on new commits.

Review in cubic

…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

claude Bot commented Aug 21, 2026 •

Copy link
Copy Markdown

Claude encountered an error —— View job


I'll analyze this and get back to you.

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@gandalfboon gandalfboon Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

  1. packages/ai/src/transports/anthropic-transport-stream.ts still gates the same check on isDirectAnthropicModel(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.
  2. 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.

@convyusboy
convyusboy merged commit ee44ec1 into boon Aug 21, 2026
146 of 153 checks passed
@convyusboy
convyusboy deleted the ridho/eng-18472-enforce-message-stop-truncated-completions branch August 21, 2026 13:47
convyusboy added a commit that referenced this pull request Aug 25, 2026
Cut a fork release containing the message_stop truncation-detection fix
from #143 and the sentry-monitor error-classification/release-tag rework
from #144.

Bump the gateway and published fork plugins in lockstep.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant