Files
magpie/docs
forever d151c10839 gateway: a whole reply after keepalives reaches the agent as its stream, not as an "OK" error (#1175)
Sometimes a held try's vendor says nothing for longer than keepHeldAfter.
keepAlive then sends the agent the stream's 200 and SSE comments. On the
passthrough path the vendor could then ignore stream:true and answer 200
with its reply whole. The try wasn't a failure, so release() told it to
the agent as the stream's error, "OK: {...the reply...}", and the agent
lost a reply that was billed. Found by reading the code, then reproduced
for Chat, Responses and Anthropic, in a group and for a lone provider.

Once the agent has a stream's headers, release() now sends a whole 200
reply as that protocol's stream (wholeAsStream). Each choice, output item
and content block is sent in the events that stream it, as the vendor
wrote it:
- text, with its citations (Anthropic citations_delta, Responses
  output_text.annotation.added)
- reasoning summaries, and thinking with its signature
- tool calls, refusals and stop details
- usage

A 200 under any type other than JSON or SSE is held too. Before, a
text/plain reply was written raw into the SSE stream. Its first bytes
decide (sseStart). If its first line begins with an SSE field, it was a
stream all along under the wrong type, and it goes through as it comes,
as before. While those bytes have yet to come, the agent is still kept
alive.

A 200 that is no reply of the protocol's now fails in settle(), the way
a translated reply that didn't stream does (502, "... did not stream").
The next member answers. On the last member the agent gets that error
and the request is logged as a 502.

Gemini gets no keepalives, so this changes nothing for it.

Verified:
- Each new test fails on the code before its fix and passes with it:
  - TestQuietWholeReplyStreamed covers Chat, Chat as text/plain,
    Responses, Anthropic, "not a reply" and "a stream as octet-stream".
    Before the fix the first four get "error:OK: {...}", or nothing for
    text/plain. The octet-stream case is the regression found on
    review: its stream was buffered and failed over.
  - TestQuietNotAReplyLastFails: before the fix the agent got the OK
    error, logged as 200.
  - TestWholeAsStreamClientReads and TestWholeAsStreamResponsesCitations.
  - TestHeldUnsureStreamPasses covers prefixes split across writes,
    id:/retry: first, and a whole reply.
  - TestHeldUnsureKeptAlive.
- go test -tags nogui -race -count=20 on these tests together with
  TestQuiet|TestSilentHeld|TestKeepAlive.
- go test -tags nogui ./internal/gateway, and the prebuilt test binary
  with GOMAXPROCS=2.
- go vet ./....
- go build, GOOS=linux go build -tags nogui, and GOOS=windows go build.
- go test -tags nogui ./... under a temp HOME.
- The synthesized streams were also fed to openai-node 7.30.0
  (chat.completions.stream, responses.stream) and @anthropic-ai/sdk
  0.131.0 (messages.stream, beta.messages.stream). Each accumulated the
  whole body's reply field for field.
- Not tried against a real vendor that ignores stream:true.
2026-10-08 06:48:48 +08:00
..