A streamed Claude subscription turn whose run says nothing for minutes
(the interactive mode tells a message only once it is whole; a long
conversation replayed to a new Claude Code is prefilled uncached first)
gave its client the held stream's 200 and comments, and those stop at
keepaliveLongest (5 min). Claude Code 2.1.316's byte watchdog then ended
the request 300s later, at 599s: the reporter's 599.29s and 599.33s.
Each cancel aborts the TUI run (letGo), so the retry is a new Claude Code
that replays the history and writes it to the cache again (#3808).
keepBridgeHeard wraps the run's events for a stream: after
bridgeHeardAfter (1 min) of a quiet run it sends an empty KStart, which
makes relay begin the reply (message_start), and a KAlive every
keepaliveEvery while it stays quiet, up to translatedQuietLongest, so the
client gets its protocol's pings (#2316's pattern). The hold still holds
the frames to holdLead and passes an error over. The run's own start
later adds its usage (collector), and a failure in the first minute is
still a status.
The gain is bounded. Claude Code 2.1.316 turns every 10s with bytes into
a ping of its own (RSr, __n=1e4) and counts 30 pings in a row (vSr=30),
so a stream of pings and comments alone ends 600s after its last other
event. The run now has until about 14 minutes (message_start at ~4 min
plus 600s), where it had 599s. The reporter's retries answered first at
385s and 527s.
Run:
- Real claude.exe 2.1.316 (-p, sandbox HOME, env -i) against a fake
Anthropic server:
- headers at 3s, comments to 300s: ended at 598.38s ("[byte-watchdog]
... no bytes for 300000ms"; "Stream idle timeout before first event").
- comments forever: ended at ~600s from headers ("no chunks received
for 300s").
- no headers: "Request timed out" at 360s.
- this fix's shape (message_start + ping at 300s, a ping every 60s):
lasted to 904s, then "no chunks received for 300s" and fell back to
non-streaming. That is the 600s bound above.
- mote-run: go test -tags nogui -count=1 -timeout 45m ./internal/gateway
ok (675.8s), at this change before its comment-only edits.
- mote-run: go test -tags nogui -count=1 -timeout 45m ./internal/gateway
ok (680.2s) again at this tree, rebased on origin/main 5ee5274bd.
- mote-run: TestClaudeBridgeQuietRunKeepsClientAlive -count=3 ok, and
gofmt -l internal/gateway clean, at the committed tree.
- mote-run: GOOS=linux and GOOS=windows go vet -tags nogui
./internal/gateway ok.
Broken by hand:
- Without the keepBridgeHeard call, the new test fails: no
message_start or ping before the reply.
- Without the synthetic KStart (KAlive alone), it fails with "0 pings
while the run was quiet": the client gets comments only.
Not tried:
- A real Claude account, the interactive (TUI) mode, a real replay.
The test uses a -p fake Claude Code. The wrapper doesn't depend on
the mode.
- Chat and Responses clients through the bridge. Their encoders take an
empty KStart, but no test covers it here.
- A run quiet for longer than ~14 min still ends. Only a real event of
the reply would keep it alive (#5060).
- magpie's pingEvery comment in gateway.go describes 2.1.311's count
without the synthetic pings. Not changed here.
84 lines
3.6 KiB
Go
84 lines
3.6 KiB
Go
package gateway
|
|
|
|
import (
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
"github.com/yetone/magpie/internal/testenv"
|
|
)
|
|
|
|
// #5043: a Claude Code run that says nothing for minutes before its
|
|
// reply (a long conversation replayed whole, prefilled uncached on a
|
|
// high effort) left a streaming client with the held stream's comments
|
|
// only, and those stop at keepaliveLongest: Claude Code 2.1.316's byte
|
|
// watchdog then hung up 300s later, at 599s, and its event watchdog
|
|
// counts no comment at all. Past bridgeHeardAfter the reply begins
|
|
// (message_start) and the client is sent its protocol's pings while the
|
|
// run is quiet, and the run's own start, when it comes, adds its usage.
|
|
func TestClaudeBridgeQuietRunKeepsClientAlive(t *testing.T) {
|
|
claudeMadeFirst(t, false)
|
|
keepFast(t, 10*time.Millisecond, 50*time.Millisecond, keepaliveLongest)
|
|
heard, ping := bridgeHeardAfter, pingEvery
|
|
bridgeHeardAfter, pingEvery = 200*time.Millisecond, 100*time.Millisecond
|
|
t.Cleanup(func() { bridgeHeardAfter, pingEvery = heard, ping })
|
|
|
|
// a Claude Code quiet for 1.5s before its reply: ~13 pings' room at
|
|
// 100ms from 200ms, of which 3 are asked for
|
|
bin := t.TempDir()
|
|
testenv.Program(t, filepath.Join(bin, "claude"), `#!/bin/sh
|
|
case "$1" in auth) exit 1;; esac
|
|
if [ "$2" = "/usage" ]; then
|
|
echo '{"type":"result","is_error":false,"result":"Current session: 10% used"}'
|
|
exit 0
|
|
fi
|
|
while read -r line; do
|
|
case $line in *'"type":"control_request"'*)
|
|
id=$(printf '%s' "$line" | sed 's/.*"request_id":"\([^"]*\)".*/\1/')
|
|
echo '{"type":"control_response","response":{"subtype":"success","request_id":"'$id'"}}'
|
|
continue ;;
|
|
esac
|
|
sleep 1.5
|
|
echo '{"type":"stream_event","event":{"type":"message_start","message":{"id":"m","model":"claude-sonnet-5","usage":{"input_tokens":4242}}}}'
|
|
echo '{"type":"stream_event","event":{"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"after the quiet"}}}'
|
|
echo '{"type":"stream_event","event":{"type":"message_delta","delta":{"stop_reason":"end_turn"},"usage":{"output_tokens":3}}}'
|
|
echo '{"type":"stream_event","event":{"type":"message_stop"}}'
|
|
echo '{"type":"result","subtype":"success","is_error":false,"result":""}'
|
|
done
|
|
`)
|
|
t.Setenv("PATH", bin+string(os.PathListSeparator)+os.Getenv("PATH"))
|
|
|
|
s := New()
|
|
t.Cleanup(s.subscription.abortAll)
|
|
body := `{"model":"claude-sonnet-5","max_tokens":100,"stream":true,"messages":[{"role":"user","content":"hi"}]}`
|
|
rec := httptest.NewRecorder()
|
|
var u Usage
|
|
p := provider.Provider{ID: "claude", Account: &provider.Account{Agent: "claude", User: "a@example.com"}}
|
|
if code, msg := s.serveClaudeSubscription(rec, httptest.NewRequest("POST", "/v1/messages", strings.NewReader(body)), provider.Anthropic, p, "claude-sonnet-5", []byte(body), &u); code != 200 {
|
|
t.Fatalf("%d %s", code, msg)
|
|
}
|
|
out := rec.Body.String()
|
|
said := strings.Index(out, "after the quiet")
|
|
if said < 0 {
|
|
t.Fatalf("no answer:\n%s", out)
|
|
}
|
|
before := out[:said]
|
|
if n := strings.Count(before, "event: message_start"); n != 1 {
|
|
t.Fatalf("%d message_start before the reply, want 1 (the client heard nothing it counts while the run was quiet):\n%s", n, out)
|
|
}
|
|
if n := strings.Count(before, `data: {"type":"ping"}`); n < 3 {
|
|
t.Fatalf("%d pings while the run was quiet, want 3 or more:\n%s", n, out)
|
|
}
|
|
if n := strings.Count(out, "event: message_start"); n != 1 {
|
|
t.Fatalf("%d message_start in the reply, want 1:\n%s", n, out)
|
|
}
|
|
// the run's own start, after the one sent for it, still counts
|
|
if u.Input != 4242 || !strings.Contains(out, `"input_tokens":4242`) {
|
|
t.Fatalf("the run's start lost its usage: %+v\n%s", u, out)
|
|
}
|
|
}
|