Commit Graph
3 Commits
Author SHA1 Message Date
ggbdpq ec03bca72e tests: the search queue's idle client times the state, not the headers (#2547)
TestSearchQueueKeepsCodexAlive/silent went red under load (yetone's
runs at load ~250 and ~330-420, passing alone): pings=0, complete=false
at search_queue_test.go:139. The client's 300ms idle was armed at the
response headers, so the loaded path from headers to the first
translated event counted against the window, cancel cut the stream
before the first ping, and a covered-while-speaking upstream looked
like a covered-forever one.

What changed
- The idle window now starts at the first data line, the same state
  line its resets already followed: 300ms still bounds every gap the
  stream leaves, the 5s hard cap still ends a stream that never speaks,
  and a truly covered-forever upstream still fails the pings>12 check.
- The silent subtest holds the fake queue's first event back 500ms
  past the headers, which deterministically reds the old headers-armed
  window (pings=0) without any real load.

Run at ba1dc7f97 (upstream/main), -tags nogui ./internal/gateway:
- Old window, held first event: silent red,
  "silent upstream covered indefinitely: pings=0 complete=false",
  comments/progress/cancel green.
- New window, -run TestSearchQueueKeepsCodexAlive -count=3: ok.

Fixes #2547
Generated-by: GLM-5.3-Flash (ZCode)
2026-11-01 16:43:21 +08:00
yetone 13f658a37d gateway: a translated stream whose provider goes quiet keeps its client alive for up to 30 minutes (youyo.p)
youyo.p on Discord (Windows, magpie 0.1.1609): Claude Code on
/v1/messages, translated to a custom provider's /v1/responses, was
canceled with 499 at ~357s and ~795s while the provider was still open.

Once the provider had begun its reply, a translated stream sent the
client something only when the provider did: the encoder's keepalive
went out on each upstream keepalive, and a provider silent mid-turn
(a long reasoning step sent whole at its end) sent nothing at all.

Now streamTranslated runs keepThroughSilence beside readSSEAlive: every
keepaliveEvery with the provider quiet, the client gets its own
protocol's keepalive through the encoder (Anthropic a ping, Responses
response.in_progress, Chat a comment, Gemini nothing), for up to
translatedQuietLongest (30 min: longer than upstreamHeaderWait's 10, long
enough for a reasoning turn sent whole), then nothing, so a provider
that never answers still ends. Writes from the reader and the ticker go
under one lock. Failover is unchanged: nothing is kept alive once a try
failed, was cut or relooped, and a held try's writes still go through
holdWriter.

Anthropic pings are spaced pingEvery (1 min) apart, comments in
between: Claude Code's 300s event watchdog takes at most 30 pings in a
row, and one ping per upstream keepalive spent them in minutes. Before
message_start the keepalive is a comment, as the SDKs' MessageStream
fails on any event before it.

Sibling: search.go's askTranslated, the other readSSEAlive caller, hands
on a KAlive from the same ticker under the same bound. The relays
(claude_subscription relay/relayEvents) keep keepaliveLongest (5 min)
and are unchanged, as is holdWriter's held comments.

Not changed: a provider that sends headers and then no byte at all is
waited out by notAnAPIReply's first-byte read under the held writer's
comments only, before any encoder exists; that path stays as it was.

A translated try held for failover (holdWriter) has two keepers: the
held writer's own comments, bounded by keepaliveLongest (5 min), and the
encoder's. Its comments bypass the encoder's writer, so for the first 5
minutes such a client can get two keepalives per keepaliveEvery. That is
harmless (comments and spaced pings) and left as is;
TestQuietKeptAliveOnlySoLong now sets both bounds and allows both
keepers' count.

Tests: TestTranslatedSilenceStillTimesOut is replaced by
TestTranslatedSilenceKeptAliveForABound (anthropic from responses,
responses from chat, chat from responses: the provider begins, then is
silent; it waits for 10 keepalives, not a duration, and checks they stop
after the bound, with room both ways). TestTranslatedStreamKeepsClientAlive
bounds pings by pingEvery; TestAnthropicKeepaliveBeforeStartIsAComment;
TestRelayKeepsClientAlive counts comments too; TestSearchQueueKeepsCodexAlive
sets the bound; TestQuietKeptAliveOnlySoLong sets both bounds
(red with the quiet bound removed). Prompts and headers sent upstream are unchanged.

Run at this head (rebased on origin/main 6ce0f14b9):
- go test ./internal/gateway -count=1 -timeout 45m: started before the push, its result is in the follow-up
  (owner rule: ship a verified fix at once). Targeted at this head:
  go test -race -run 'TestQuiet|Keepalive|KeepAlive|KeptAlive|TestTranslatedSilence|TestSearchQueue|TestRelay': ok
- go test -race on the keepalive tests: ok
- gofmt -l clean; go vet darwin, GOOS=linux -tags nogui, GOOS=windows: ok

Broken by hand:
- the ticker never calling keep: the bound test red in all 3 (0, 1, 3 of 10)
- no quiet bound: red, 125/199/220 more after the first 10
- a ping on every keepalive: TestTranslatedStreamKeepsClientAlive red
  (75 pings in 1.5s, want at most 20) and the encoder test red
- a ping before message_start: TestAnthropicKeepaliveBeforeStartIsAComment red

Not tried: a real Claude Code against a real silent provider, or
youyo.p's provider; Windows.
2026-10-27 15:17:26 +08:00
yetone 6f6b7d4077 gateway: a Codex request with web_search stays alive while its provider is queued, not cut at Codex's idle timeout (#2316, clxsh)
A request that offers web_search to a model that can't run it goes through
searchReply, which reads the provider's reply in rounds. That reader used
readSSE, so the provider's keepalives (": keepalive", response.in_progress)
were dropped, and relay waited for the first event past KStart before it
made the client's encoder. A provider queued for minutes sent the client
nothing but the holdWriter's comments, which Codex's idle timeout doesn't
count: it hung up at ~315s and asked again, losing its place in the queue.
The plain translated path has kept the client alive since #436.

Now askTranslated reads with readSSEAlive and, after keepaliveGap with
nothing else sent, hands its reader a KAlive event (nothing of the reply).
searchRounds passes it on without collecting it, the collector ignores it
(no reasoning block boundary moves), and relay takes it as the provider
being heard from: it begins the client's stream and sends the protocol's
own keepalive (response.in_progress, Anthropic's ping) when the client has
been quiet keepaliveGap. A provider that goes silent sends no KAlive, so
relayEvents' keepaliveLongest still ends the keepalives; an error before
any keepalive is still a status for the next account.

Test: clxsh's TestSearchQueueKeepsCodexAlive as filed (comments, progress,
cancel, silent). On the old code all four fail (pings=0 complete=false,
only comments reach the client); with the fix they pass, -count=3.

Run: go test -tags nogui ./internal/gateway -run
'Search|Keep|Alive|Relay|Zen|Draw|Probe|Held|Hold|Queue|Codex.*Stream|Translated'
ok (75s); gofmt -l clean; go vet -tags nogui ./internal/gateway on
darwin, linux, windows clean. Not tried against a real queued provider.

Full suite: SUITE_LINE
2026-10-15 23:08:01 +08:00