Files
archie/internal/webui/sse.go
T
sam ead8e4016d feat(store): generalise the event feed cursor to a total order
EventsSince no longer depends on events.id being monotonic. The feed now
pages a total order over (at, id): at is a fixed-width, lexicographically
sortable UTC timestamp (storecontract.EventCursorLayout) and id is only a
deterministic tie-break for rows sharing one timestamp. The cursor is an
opaque string on the wire (EventsSinceRequest.cursor), carried verbatim as
the SSE id: field and echoed back via Last-Event-ID, so EventSource resume
needs no client change.

A new idempotent migration normalises pre-existing RFC3339Nano at values to
the fixed-width layout before finishTaskMigration, and idx_events_at backs
the new query. Legacy integer and garbage cursors degrade to "from the
beginning", preserving the old failed-ParseInt-yields-0 behaviour.

Amends the "Dashboard live event delivery" decision's cursor-cannot-skip
justification, which previously rested on commit-order id assignment.
2026-09-23 03:10:30 +10:00

215 lines
7.0 KiB
Go

package webui
import (
"context"
"encoding/json"
"log/slog"
"net/http"
"github.com/samcharles93/archie-core/internal/domain/storecontract"
"github.com/samcharles93/archie-core/internal/events"
)
// 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=<event id>, or
// the Last-Event-ID header on a reconnect), then live from the broadcast
// hub.
func (s *Server) handleSSE(w http.ResponseWriter, r *http.Request) {
stream, ok := newSSEStream(s.Store, s.Log, w, sseSince(r))
if !ok {
http.Error(w, "streaming unsupported", http.StatusInternalServerError)
return
}
// Register before reading the backlog. An event published between the
// backlog read and subscription would otherwise be lost permanently.
conn, unregister := s.registerSSEConn()
defer unregister()
if !stream.catchUp(r.Context(), "") {
return
}
stream.drain(r.Context(), conn)
}
// sseSince resolves the cursor a client wants to resume after. A valid
// Last-Event-ID header -- how EventSource itself resumes a dropped
// connection -- takes precedence over the ?since= query parameter, which
// only matters for a client's very first connection. An absent or unparseable
// cursor degrades to "from the beginning": a browser holding a legacy integer
// id (the pre-cursor id: field) or garbage must not break the feed, exactly
// as a failed ParseInt yielded 0 before the cursor became a string.
func sseSince(r *http.Request) string {
since := r.URL.Query().Get("since")
if header := r.Header.Get("Last-Event-ID"); header != "" {
since = header
}
if _, _, ok := storecontract.ParseEventCursor(since); !ok {
return ""
}
return since
}
// registerSSEConn subscribes a channel to Broadcast and returns the
// function that unsubscribes it.
func (s *Server) registerSSEConn() (chan events.Event, func()) {
c := make(chan events.Event, 64)
s.mu.Lock()
if s.conns == nil {
s.conns = map[chan events.Event]struct{}{}
}
s.conns[c] = struct{}{}
s.mu.Unlock()
return c, func() {
s.mu.Lock()
delete(s.conns, c)
s.mu.Unlock()
}
}
// sseStream renders one client's event-stream response: an SSE frame per
// event, an SSE comment when the persisted backlog can't be read, and the
// running "since" cursor that deduplicates the backlog against live
// broadcasts covering the same event.
type sseStream struct {
store storecontract.TaskStore
log *slog.Logger
w http.ResponseWriter
fl http.Flusher
since string
}
// newSSEStream builds a stream over w, reporting false if w cannot be
// flushed incrementally -- SSE does not work without that.
func newSSEStream(st storecontract.TaskStore, log *slog.Logger, w http.ResponseWriter, since string) (*sseStream, bool) {
fl, ok := w.(http.Flusher)
if !ok {
return nil, false
}
w.Header().Set("Content-Type", "text/event-stream")
w.Header().Set("Cache-Control", "no-cache")
return &sseStream{store: st, log: log, w: w, fl: fl, since: since}, true
}
// send writes one event as an SSE frame. It reports false on a write
// error, meaning the connection is gone and the caller must stop.
func (s *sseStream) send(e events.Event) bool {
body, err := json.Marshal(e)
if err != nil {
s.log.Error("sse event marshal failed", "error", err, "event_id", e.ID)
return false
}
if _, err := s.w.Write([]byte("id: " + storecontract.EventCursor(e.At, e.ID) + "\ndata: " + string(body) + "\n\n")); err != nil {
return false
}
s.fl.Flush()
return true
}
// writeErrorComment reports a backlog fetch failure as an SSE comment.
// Comments are invisible to EventSource's own event parsing, but writing
// one and flushing before the handler returns is what actually reaches the
// client: it ends the response, which is what drives EventSource's
// reconnect.
func (s *sseStream) writeErrorComment(err error) {
_, _ = s.w.Write([]byte(":error " + err.Error() + "\n\n"))
s.fl.Flush()
}
// catchUp sends every persisted event after s.since, advancing s.since as
// it goes, until it reaches target ("" means "drain everything currently
// available"), runs out of backlog, or fails. It reports false on failure
// -- a fetch error or a dead connection -- meaning the caller must stop.
func (s *sseStream) catchUp(ctx context.Context, target string) bool {
for {
before := s.since
backlog, err := s.store.EventsSince(ctx, s.since, sseBacklogPageSize)
if err != nil {
s.log.Error("sse backlog fetch failed", "error", err, "since", s.since)
s.writeErrorComment(err)
return false
}
reachedTarget, ok := s.sendPage(backlog, target)
if !ok {
return false
}
if reachedTarget || len(backlog) < sseBacklogPageSize || s.since == before {
return true
}
}
}
// sendPage sends every event in backlog newer than s.since, advancing it as
// it goes. reachedTarget reports whether target 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 after since, so
// leaving since behind a filtered event would make the next catchUp refetch
// it forever.
func (s *sseStream) sendPage(backlog []events.Event, target string) (reachedTarget, ok bool) {
for _, e := range backlog {
cursor := storecontract.EventCursor(e.At, e.ID)
if cursor <= s.since {
continue
}
if sseVisible(e) && !s.send(e) {
return false, false
}
s.since = cursor
if target != "" && cursor >= target {
return true, true
}
}
return false, true
}
// drain relays broadcast events after the initial catch-up. A broadcast can
// reach the client before its event lands in the durable store, so each one
// first fills any persisted gap ahead of it, then is delivered directly if
// catch-up did not already cover it -- both paths go through the same
// since-based deduplication.
func (s *sseStream) drain(ctx context.Context, conn <-chan events.Event) {
for {
select {
case <-ctx.Done():
return
case e := <-conn:
cursor := storecontract.EventCursor(e.At, e.ID)
if cursor <= s.since {
continue
}
if !s.catchUp(ctx, cursor) {
return
}
if cursor > s.since {
if sseVisible(e) && !s.send(e) {
return
}
s.since = cursor
}
}
}
}