server: write a subscribe snapshot in full before its ack
handleRunEvents seeded a new subscription by pushing every matching run through the subscription channel (runEventSubscriberBuffer = 32) and only then acked. sendMessage is a non-blocking send that closes the subscription when the channel is full, so a snapshot larger than the buffer dropped the subscription it was seeding: the remaining snapshot frames hit the closed-done early return, the ack was never sent, and no later delta could arrive. The connection stayed open, so the client could not tell it was no longer subscribed. This was the app's only subscription - AppViewModel subscribes with a blank workspace, matching every run - so the app's live state froze. The subscription now starts in a snapshot phase. The subscribing connection itself writes the snapshot frames via writeSnapshot (one direct write per frame, outside the bounded channel, so the drop policy cannot fire on them) and the ack, and ready then moves any deltas held meanwhile into the channel in order and starts the writer goroutine. sendMessage holds deltas in a bounded pending queue while snapshotting instead of closing, so the drop-a-slow-subscriber policy still applies, but only to deltas. Ordering is snapshot, ack, then deltas; a write failure or a listRuns error closes the subscription instead of leaving it registered. Numbers reported by this run: the probe against the deployed server saw blank-scope frames=2 ack=0 (scoped frames=1 ack=0) because the snapshot was ~252 frames into a 32-frame buffer; the scratch-server probe with the fix saw 40 snapshot frames, ack=1 and a delta after a run transition. Guarded now by TestRunEventSubscribeSnapshotLargerThanBuffer (seeds 40 finished runs, asserts every snapshot frame, the ack strictly after them, and a terminal delta for a run started after subscribing), which fails against the previous runevents.go. Checked for staleness: the packaged templates and the packaged authoring skill references internal/author/skill/references/ carry no run-event subscription contract (their "snapshot" mentions are the workspace-change snapshots in operations.md), and README carries none either, so none of them needs updating. No UI surface changed. Checks run before committing: gofmt -l . prints nothing, go build ./..., go vet ./..., go test ./... (all packages ok), go test -race ./internal/server (ok).
This commit is contained in:
@@ -2,6 +2,7 @@ package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http/httptest"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
@@ -212,6 +213,95 @@ func TestRunEventSubscribeSendsSnapshot(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestRunEventSubscribeSnapshotLargerThanBuffer proves a subscribe whose
|
||||
// snapshot holds more runs than the subscription's bounded buffer delivers the
|
||||
// snapshot in full, then the ack, then a live delta for a run started
|
||||
// afterwards. Before the snapshot fix, the subscription wrote its own snapshot
|
||||
// through the bounded channel, so sendMessage's drop-a-slow-subscriber policy
|
||||
// fired on the snapshot itself: the subscription closed part-way through, the
|
||||
// client saw at most runEventSubscriberBuffer frames, and it never received the
|
||||
// ack or a single later delta.
|
||||
func TestRunEventSubscribeSnapshotLargerThanBuffer(t *testing.T) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
defer cancel()
|
||||
_, handler, ids := eventStore(t, ctx, "one")
|
||||
handler.runner.execute = func(context.Context, stage.Definition, stage.Values) (stage.Result, error) {
|
||||
return stage.Result{Outcome: "success", Output: stage.Values{}}, nil
|
||||
}
|
||||
const seeded = runEventSubscriberBuffer + 8
|
||||
// Seed more finished runs than the buffer holds, each in its own workspace so
|
||||
// no two runs share a tree, then wait for every one to settle. A blank-scope
|
||||
// subscribe sees them all.
|
||||
seededIDs := make([]int64, 0, seeded)
|
||||
for i := 0; i < seeded; i++ {
|
||||
run, err := handler.runner.start(protocol.Message{PipelineID: ids[0], Version: 1, RequestKey: fmt.Sprintf("snapshot-%d", i), Workspace: eventWorkspace(t), Input: map[string]any{"prompt": "test"}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
seededIDs = append(seededIDs, run.ID)
|
||||
}
|
||||
handler.runner.wg.Wait()
|
||||
|
||||
client := dialRunEvents(t, ctx, handler)
|
||||
client.send(protocol.Message{Type: protocol.Subscribe, ID: "snap", Workspace: ""})
|
||||
snapshotCtx, cancelSnapshot := context.WithTimeout(ctx, 8*time.Second)
|
||||
defer cancelSnapshot()
|
||||
seen := map[int64]bool{}
|
||||
var ack *protocol.Message
|
||||
for ack == nil || len(seen) < seeded {
|
||||
var message protocol.Message
|
||||
if err := wsjson.Read(snapshotCtx, client.conn, &message); err != nil {
|
||||
t.Fatalf("snapshot stopped after %d of %d frames (ack=%v): %v", len(seen), seeded, ack != nil, err)
|
||||
}
|
||||
switch message.Type {
|
||||
case protocol.RunEvent:
|
||||
if message.RunEvent != nil {
|
||||
seen[message.RunEvent.RunID] = true
|
||||
}
|
||||
case protocol.Subscribe:
|
||||
got := message
|
||||
ack = &got
|
||||
// The snapshot precedes the ack: when the ack is read every snapshot
|
||||
// frame must already have been, or a frame was reordered after it.
|
||||
if len(seen) != seeded {
|
||||
t.Fatalf("ack arrived after %d of %d snapshot frames", len(seen), seeded)
|
||||
}
|
||||
}
|
||||
}
|
||||
if ack.ID != "snap" {
|
||||
t.Fatalf("subscribe ack = %+v", *ack)
|
||||
}
|
||||
for _, id := range seededIDs {
|
||||
if !seen[id] {
|
||||
t.Fatalf("snapshot omitted run %d: got %d of %d frames", id, len(seen), seeded)
|
||||
}
|
||||
}
|
||||
|
||||
// The subscription must outlive its own snapshot: a run started after it must
|
||||
// still deliver a delta, through to its terminal frame.
|
||||
late := eventWorkspace(t)
|
||||
client.send(protocol.Message{Type: protocol.RunPipeline, ID: "late", PipelineID: ids[0], Version: 1, RequestKey: "snapshot-late", Workspace: late, Input: map[string]any{"prompt": "test"}})
|
||||
deltaCtx, cancelDelta := context.WithTimeout(ctx, 8*time.Second)
|
||||
defer cancelDelta()
|
||||
var terminal *protocol.RunEventFrame
|
||||
for terminal == nil {
|
||||
var message protocol.Message
|
||||
if err := wsjson.Read(deltaCtx, client.conn, &message); err != nil {
|
||||
t.Fatalf("no terminal delta after the snapshot: %v", err)
|
||||
}
|
||||
if message.Type != protocol.RunEvent || message.RunEvent == nil || message.RunEvent.Workspace != late {
|
||||
continue
|
||||
}
|
||||
switch message.RunEvent.Status {
|
||||
case "succeeded", "failed", "canceled":
|
||||
terminal = message.RunEvent
|
||||
}
|
||||
}
|
||||
if terminal.RunID == 0 || terminal.Outcome != "success" {
|
||||
t.Fatalf("late terminal frame = %+v", terminal)
|
||||
}
|
||||
}
|
||||
|
||||
// TestMergeRunEventFrameKeepsIdentity proves a coalesced frame keeps the run's
|
||||
// identity from an earlier transition when a later delta omits it, so the merged
|
||||
// frame a client actually receives still carries what its row needs.
|
||||
|
||||
Reference in New Issue
Block a user