diff --git a/internal/webui/sse.go b/internal/webui/sse.go index 38a7e0ebe..233f3b5a9 100644 --- a/internal/webui/sse.go +++ b/internal/webui/sse.go @@ -14,6 +14,23 @@ import ( // sseBacklogPageSize bounds one EventsSince fetch during catch-up. const sseBacklogPageSize = 200 +// sseNoiseKinds are event kinds published for internal wiring rather than +// for a dashboard viewer: turn_completed fires on every chat turn so +// input-driven curators can wake on it (see events.KindTurnCompleted), and +// carries no task correlation (TaskID is always 0, Detail a raw session ID) +// -- on a dashboard where "Live activity" is otherwise almost entirely +// task lifecycle events, it drowned the signal it sits next to. Curators +// read the Bus directly and are unaffected by excluding a kind here, at the +// SSE boundary, rather than at publish. +var sseNoiseKinds = map[string]bool{ + events.KindTurnCompleted: true, +} + +// sseVisible reports whether e should reach an SSE client. +func sseVisible(e events.Event) bool { + return !sseNoiseKinds[e.Kind] +} + // handleSSE streams events: catch-up from the store (?since=, or // the Last-Event-ID header on a reconnect), then live from the broadcast // hub. @@ -143,12 +160,17 @@ func (s *sseStream) catchUp(ctx context.Context, targetID int64) bool { // it goes. reachedTarget reports whether targetID was reached mid-page -- // stopping there is correct, since nothing beyond it is needed yet -- and // ok reports whether every send succeeded. +// +// since advances past a filtered-out event (sseVisible false) exactly as it +// would for a sent one: EventsSince only ever returns events with +// id > since, so leaving since behind a filtered event would make the next +// catchUp refetch it forever. func (s *sseStream) sendPage(backlog []events.Event, targetID int64) (reachedTarget, ok bool) { for _, e := range backlog { if e.ID <= s.since { continue } - if !s.send(e) { + if sseVisible(e) && !s.send(e) { return false, false } s.since = e.ID @@ -177,7 +199,7 @@ func (s *sseStream) drain(ctx context.Context, conn <-chan events.Event) { return } if e.ID > s.since { - if !s.send(e) { + if sseVisible(e) && !s.send(e) { return } s.since = e.ID diff --git a/internal/webui/sse_test.go b/internal/webui/sse_test.go index 79dd230e2..573598d20 100644 --- a/internal/webui/sse_test.go +++ b/internal/webui/sse_test.go @@ -179,3 +179,35 @@ func TestSSEStreamCatchUpStopsAtTargetOnPageBoundary(t *testing.T) { func countSSEFrames(body string) int { return strings.Count(body, "\ndata: ") } + +// TestSSEStreamSendPageSkipsNoiseKindsButAdvancesSince is the regression +// test for archie-core-521: turn_completed fires on every chat turn with no +// task correlation (Detail is a raw session ID, no TaskID), and dominated +// the dashboard's Live Activity panel. It must never reach the SSE client, +// but since must still advance past it -- otherwise the next catchUp +// refetches the same filtered event forever, since EventsSince only ever +// returns events with id > since. +func TestSSEStreamSendPageSkipsNoiseKindsButAdvancesSince(t *testing.T) { + s, rec := newTestSSEStream(0) + backlog := []events.Event{ + {ID: 1, Kind: events.KindTaskQueued}, + {ID: 2, Kind: events.KindTurnCompleted, Detail: "1312197967"}, + {ID: 3, Kind: events.KindTurnCompleted, Detail: "1312197967"}, + {ID: 4, Kind: events.KindParked}, + } + + reachedTarget, ok := s.sendPage(backlog, 0) + + if !ok || reachedTarget { + t.Fatalf("sendPage() = (%v, %v), want (false, true)", reachedTarget, ok) + } + if s.since != 4 { + t.Fatalf("since = %d, want 4 (advanced past every event, including filtered ones)", s.since) + } + if n := countSSEFrames(rec.Body.String()); n != 2 { + t.Fatalf("sent %d SSE frames, want 2 (turn_completed excluded)", n) + } + if strings.Contains(rec.Body.String(), "turn_completed") { + t.Error("SSE body contains a turn_completed frame, want it excluded entirely") + } +} diff --git a/internal/webui/webui_test.go b/internal/webui/webui_test.go index d9cde5136..c38a05e5e 100644 --- a/internal/webui/webui_test.go +++ b/internal/webui/webui_test.go @@ -412,6 +412,56 @@ func TestHandleSSEBacklogAndLive(t *testing.T) { } } +// TestHandleSSEFiltersTurnCompletedFromLiveBroadcast covers the live half of +// the archie-core-521 fix: sendPage's filtering is exercised directly in +// sse_test.go, but the live broadcast path (drain) has its own send call. +// A turn_completed broadcast between two ordinary events must never reach +// the client, while the ordinary events either side of it still do. +func TestHandleSSEFiltersTurnCompletedFromLiveBroadcast(t *testing.T) { + srv := newTestServer(t) + ctx, cancel := context.WithTimeout(t.Context(), 3*time.Second) + defer cancel() + id, err := srv.Store.InsertEvent(ctx, events.Event{Kind: "log", Detail: "backlog"}) + if err != nil { + t.Fatal(err) + } + + ts := httptest.NewServer(srv.Handler()) + t.Cleanup(ts.Close) + + req, err := http.NewRequestWithContext(ctx, http.MethodGet, ts.URL+"/events", nil) + if err != nil { + t.Fatal(err) + } + resp, err := http.DefaultClient.Do(req) + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { _ = resp.Body.Close() }) + if resp.StatusCode != http.StatusOK { + t.Fatalf("status = %d", resp.StatusCode) + } + + reader := bufio.NewReader(resp.Body) + if _, err := readLineUntil(reader, "backlog"); err != nil { + t.Fatalf("backlog event not received: %v", err) + } + + time.Sleep(50 * time.Millisecond) + srv.Broadcast(events.Event{ID: id + 1, Kind: events.KindTurnCompleted, Detail: "1312197967"}) + srv.Broadcast(events.Event{ID: id + 2, Kind: "log", Detail: "after-noise"}) + + seen, err := readLinesUntil(reader, "after-noise") + if err != nil { + t.Fatalf("event after the filtered one was not received: %v", err) + } + for _, line := range seen { + if strings.Contains(line, "turn_completed") { + t.Fatalf("the filtered event's frame reached the client: %q", line) + } + } +} + // readLineUntil scans SSE "data:" lines until one contains want. func readLineUntil(r *bufio.Reader, want string) (string, error) { deadline := time.Now().Add(3 * time.Second) @@ -426,3 +476,26 @@ func readLineUntil(r *bufio.Reader, want string) (string, error) { } return "", io.EOF } + +// readLinesUntil scans SSE "data:" lines, collecting every one seen, until +// one contains want -- unlike readLineUntil, callers get the full scan, not +// just the matched line, so a frame that arrived before the match but was +// never asserted on can't hide there. +func readLinesUntil(r *bufio.Reader, want string) ([]string, error) { + var seen []string + deadline := time.Now().Add(3 * time.Second) + for time.Now().Before(deadline) { + line, err := r.ReadString('\n') + if err != nil { + return seen, err + } + if !strings.HasPrefix(line, "data:") { + continue + } + seen = append(seen, line) + if strings.Contains(line, want) { + return seen, nil + } + } + return seen, io.EOF +}