From 7530ec4231e55c246fed569583c3943242830214 Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Wed, 23 Sep 2026 03:06:25 +1000 Subject: [PATCH] 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. --- internal/contracts/state/v1/state.pb.go | 21 +-- internal/daemon/dispatch_bindings_test.go | 2 +- internal/domain/storecontract/cursor.go | 54 +++++++ .../domain/storecontract/storecontract.go | 2 +- internal/infrastructure/staterpc/client.go | 4 +- .../staterpc/conformance_test.go | 2 +- internal/infrastructure/staterpc/server.go | 2 +- internal/store/events.go | 39 ++++- internal/store/store.go | 66 ++++++++ internal/store/store_test.go | 150 +++++++++++++++-- internal/webui/api_tasks_actions_test.go | 2 +- internal/webui/event_pump.go | 27 ++-- internal/webui/event_pump_test.go | 15 +- internal/webui/sse.go | 61 +++---- internal/webui/sse_test.go | 152 ++++++++++++------ internal/webui/webui_test.go | 24 +-- proto/state/v1/state.proto | 5 +- 17 files changed, 491 insertions(+), 137 deletions(-) create mode 100644 internal/domain/storecontract/cursor.go diff --git a/internal/contracts/state/v1/state.pb.go b/internal/contracts/state/v1/state.pb.go index d96463dec..cacdc3308 100644 --- a/internal/contracts/state/v1/state.pb.go +++ b/internal/contracts/state/v1/state.pb.go @@ -4133,9 +4133,12 @@ func (x *InsertEventResponse) GetId() int64 { } type EventsSinceRequest struct { - state protoimpl.MessageState `protogen:"open.v1"` - SinceId int64 `protobuf:"varint,1,opt,name=since_id,json=sinceId,proto3" json:"since_id,omitempty"` - Limit int64 `protobuf:"varint,2,opt,name=limit,proto3" json:"limit,omitempty"` + state protoimpl.MessageState `protogen:"open.v1"` + // cursor is the opaque resume cursor returned by the previous page (the SSE + // id: field, echoed back as Last-Event-ID). Empty means "from the + // beginning". + Cursor string `protobuf:"bytes,1,opt,name=cursor,proto3" json:"cursor,omitempty"` + Limit int64 `protobuf:"varint,2,opt,name=limit,proto3" json:"limit,omitempty"` unknownFields protoimpl.UnknownFields sizeCache protoimpl.SizeCache } @@ -4170,11 +4173,11 @@ func (*EventsSinceRequest) Descriptor() ([]byte, []int) { return file_state_v1_state_proto_rawDescGZIP(), []int{70} } -func (x *EventsSinceRequest) GetSinceId() int64 { +func (x *EventsSinceRequest) GetCursor() string { if x != nil { - return x.SinceId + return x.Cursor } - return 0 + return "" } func (x *EventsSinceRequest) GetLimit() int64 { @@ -7925,9 +7928,9 @@ const file_state_v1_state_proto_rawDesc = "" + "\x12InsertEventRequest\x12%\n" + "\x05event\x18\x01 \x01(\v2\x0f.state.v1.EventR\x05event\"%\n" + "\x13InsertEventResponse\x12\x0e\n" + - "\x02id\x18\x01 \x01(\x03R\x02id\"E\n" + - "\x12EventsSinceRequest\x12\x19\n" + - "\bsince_id\x18\x01 \x01(\x03R\asinceId\x12\x14\n" + + "\x02id\x18\x01 \x01(\x03R\x02id\"B\n" + + "\x12EventsSinceRequest\x12\x16\n" + + "\x06cursor\x18\x01 \x01(\tR\x06cursor\x12\x14\n" + "\x05limit\x18\x02 \x01(\x03R\x05limit\">\n" + "\x13EventsSinceResponse\x12'\n" + "\x06events\x18\x01 \x03(\v2\x0f.state.v1.EventR\x06events\",\n" + diff --git a/internal/daemon/dispatch_bindings_test.go b/internal/daemon/dispatch_bindings_test.go index 63e16501b..3d2eb59c5 100644 --- a/internal/daemon/dispatch_bindings_test.go +++ b/internal/daemon/dispatch_bindings_test.go @@ -171,7 +171,7 @@ func TestDispatchBindingsSkipsWhenResolveFails(t *testing.T) { if len(tasks) != 0 { t.Fatalf("required-field failure spawned %d task(s), want 0", len(tasks)) } - events, err := s.EventsSince(t.Context(), 0, 100) + events, err := s.EventsSince(t.Context(), "", 100) if err != nil { t.Fatalf("EventsSince: %v", err) } diff --git a/internal/domain/storecontract/cursor.go b/internal/domain/storecontract/cursor.go new file mode 100644 index 000000000..62e300baa --- /dev/null +++ b/internal/domain/storecontract/cursor.go @@ -0,0 +1,54 @@ +package storecontract + +import ( + "fmt" + "strconv" + "strings" + "time" +) + +// EventCursorLayout is the fixed-width UTC layout every persisted event's at +// column and every EventsSince cursor is built with. Fixed width is the whole +// point: time.RFC3339Nano trims trailing zeros, so a whole-second row +// ("...05Z") and a full-precision row ("...05.123456789Z") would compare out +// of chronological order as strings. With nine fractional digits always +// present, lexicographic order is chronological order. It mirrors edastore's +// capturedAtLayout, which exists for the same reason; the two stores now share +// this rule. +const EventCursorLayout = "2006-01-02T15:04:05.000000000Z" + +// cursorSeparator joins the sort key and the row id inside a cursor. The sort +// key never contains it, so splitting on the last occurrence is unambiguous. +const cursorSeparator = "|" + +// cursorIDWidth zero-pads the row id so the whole cursor is lexicographically +// sortable: the fixed-width sort key is followed by a fixed-width tie-break, +// and string comparison of two cursors is the total order (key, id). +const cursorIDWidth = 20 + +// EventCursor returns the opaque resume cursor for an event that happened at +// at with row id id. The cursor is a string so EventSource can carry it +// verbatim in its id: field and echo it back as the Last-Event-ID header; it +// is opaque to every consumer except EventsSince. +func EventCursor(at time.Time, id int64) string { + return at.UTC().Format(EventCursorLayout) + cursorSeparator + fmt.Sprintf("%0*d", cursorIDWidth, id) +} + +// ParseEventCursor splits a cursor back into its sort key (the fixed-width at +// string) and row id. ok is false for an empty or malformed cursor, which +// means "from the beginning" -- the store must not treat a bad resume point as +// an error worth failing a feed over. +func ParseEventCursor(cursor string) (at string, id int64, ok bool) { + if cursor == "" { + return "", 0, false + } + i := strings.LastIndex(cursor, cursorSeparator) + if i < 0 { + return "", 0, false + } + id, err := strconv.ParseInt(cursor[i+1:], 10, 64) + if err != nil { + return "", 0, false + } + return cursor[:i], id, true +} diff --git a/internal/domain/storecontract/storecontract.go b/internal/domain/storecontract/storecontract.go index 2729d9638..d8c199887 100644 --- a/internal/domain/storecontract/storecontract.go +++ b/internal/domain/storecontract/storecontract.go @@ -80,7 +80,7 @@ type TaskQueries interface { // TaskEvents groups observability and lifecycle methods. type TaskEvents interface { InsertEvent(ctx context.Context, e events.Event) (int64, error) - EventsSince(ctx context.Context, sinceID int64, limit int) ([]events.Event, error) + EventsSince(ctx context.Context, cursor string, limit int) ([]events.Event, error) TaskEvents(ctx context.Context, taskID int64) ([]events.Event, error) WorkflowStats(ctx context.Context) ([]WorkflowStat, error) StageStats(ctx context.Context) ([]StageStat, error) diff --git a/internal/infrastructure/staterpc/client.go b/internal/infrastructure/staterpc/client.go index 3037918e8..9b18c5a7e 100644 --- a/internal/infrastructure/staterpc/client.go +++ b/internal/infrastructure/staterpc/client.go @@ -215,8 +215,8 @@ func (c *Client) InsertEvent(ctx context.Context, e events.Event) (int64, error) return r.Id, nil } -func (c *Client) EventsSince(ctx context.Context, sinceID int64, limit int) ([]events.Event, error) { - r, err := c.client.EventsSince(ctx, &pb.EventsSinceRequest{SinceId: sinceID, Limit: int64(limit)}) +func (c *Client) EventsSince(ctx context.Context, cursor string, limit int) ([]events.Event, error) { + r, err := c.client.EventsSince(ctx, &pb.EventsSinceRequest{Cursor: cursor, Limit: int64(limit)}) if err != nil { return nil, unmapError(err) } diff --git a/internal/infrastructure/staterpc/conformance_test.go b/internal/infrastructure/staterpc/conformance_test.go index b2a322248..7d4f22b1a 100644 --- a/internal/infrastructure/staterpc/conformance_test.go +++ b/internal/infrastructure/staterpc/conformance_test.go @@ -232,7 +232,7 @@ func TestStateStoreConformance(t *testing.T) { if !ok || doc["bot_user"] != "archie" { t.Errorf("config_captured payload = %#v, want the decoded document under its own key", evs[1].Data) } - if _, err := c.EventsSince(ctx, 0, 10); err != nil { + if _, err := c.EventsSince(ctx, "", 10); err != nil { t.Fatalf("EventsSince: %v", err) } if _, err := c.WorkflowStats(ctx); err != nil { diff --git a/internal/infrastructure/staterpc/server.go b/internal/infrastructure/staterpc/server.go index 68b93a3c3..11097ef05 100644 --- a/internal/infrastructure/staterpc/server.go +++ b/internal/infrastructure/staterpc/server.go @@ -283,7 +283,7 @@ func (s *server) InsertEvent(ctx context.Context, r *pb.InsertEventRequest) (*pb } func (s *server) EventsSince(ctx context.Context, r *pb.EventsSinceRequest) (*pb.EventsSinceResponse, error) { - evs, err := s.deps.Tasks.EventsSince(ctx, r.SinceId, int(r.Limit)) + evs, err := s.deps.Tasks.EventsSince(ctx, r.Cursor, int(r.Limit)) if err != nil { return nil, s.logErr("EventsSince", err) } diff --git a/internal/store/events.go b/internal/store/events.go index 081672db1..d860ef832 100644 --- a/internal/store/events.go +++ b/internal/store/events.go @@ -8,6 +8,7 @@ import ( "fmt" "time" + "github.com/samcharles93/archie-core/internal/domain/storecontract" "github.com/samcharles93/archie-core/internal/domain/workflow" "github.com/samcharles93/archie-core/internal/events" ) @@ -31,6 +32,7 @@ CREATE TABLE IF NOT EXISTS events ( ); CREATE INDEX IF NOT EXISTS idx_events_task ON events(task_id, id); CREATE INDEX IF NOT EXISTS idx_events_kind ON events(kind, id); +CREATE INDEX IF NOT EXISTS idx_events_at ON events(at, id); ` // InsertEvent appends an event to the log and returns its row id. @@ -53,7 +55,7 @@ func insertEvent(ctx context.Context, execer eventExecer, e events.Event) (int64 res, err := execer.ExecContext(ctx, ` INSERT INTO events (at, kind, task_id, repo, issue, workflow, stage, attempt, actor_id, actor_kind, principal_id, detail, data) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, - e.At.UTC().Format(time.RFC3339Nano), e.Kind, e.TaskID, e.Repo, e.Issue, + e.At.UTC().Format(storecontract.EventCursorLayout), e.Kind, e.TaskID, e.Repo, e.Issue, e.Workflow, e.Stage, e.Attempt, e.ActorID, e.ActorKind, e.PrincipalID, clip(e.Detail, 4000), string(data)) if err != nil { @@ -74,7 +76,7 @@ func scanEvents(rows *sql.Rows) (out []events.Event, retErr error) { &e.PrincipalID, &e.Detail, &data); err != nil { return nil, err } - e.At, _ = time.Parse(time.RFC3339Nano, at) + e.At = parseEventAt(at) _ = json.Unmarshal([]byte(data), &e.Data) out = append(out, e) } @@ -83,17 +85,42 @@ func scanEvents(rows *sql.Rows) (out []events.Event, retErr error) { const eventCols = "id, at, kind, task_id, repo, issue, workflow, stage, attempt, actor_id, actor_kind, principal_id, detail, data" -// EventsSince returns up to limit events with id > sinceID, oldest -// first -- SSE catch-up and the live feed. -func (s *Store) EventsSince(ctx context.Context, sinceID int64, limit int) ([]events.Event, error) { +// EventsSince returns up to limit events after the opaque cursor, oldest +// first -- SSE catch-up and the live feed. The cursor carries the total +// order (at, id): at is the fixed-width sort key and id only breaks ties, so +// a non-monotonic id scheme can neither skip nor repeat an event. An empty or +// malformed cursor means "from the beginning". +func (s *Store) EventsSince(ctx context.Context, cursor string, limit int) ([]events.Event, error) { + at, id, ok := storecontract.ParseEventCursor(cursor) + if !ok { + rows, err := s.db.QueryContext(ctx, + `SELECT `+eventCols+` FROM events ORDER BY at, id LIMIT ?`, limit) + if err != nil { + return nil, err + } + return scanEvents(rows) + } rows, err := s.db.QueryContext(ctx, - `SELECT `+eventCols+` FROM events WHERE id > ? ORDER BY id LIMIT ?`, sinceID, limit) + `SELECT `+eventCols+` FROM events WHERE at > ? OR (at = ? AND id > ?) ORDER BY at, id LIMIT ?`, + at, at, id, limit) if err != nil { return nil, err } return scanEvents(rows) } +// parseEventAt reads the stored timestamp, tolerating both the fixed-width +// layout new rows are written in and the RFC3339Nano form older rows carried +// before the migration normalised them. +func parseEventAt(raw string) time.Time { + for _, layout := range []string{storecontract.EventCursorLayout, time.RFC3339Nano, time.RFC3339} { + if t, err := time.Parse(layout, raw); err == nil { + return t + } + } + return time.Time{} +} + // TaskEvents returns a task's full timeline, oldest first. func (s *Store) TaskEvents(ctx context.Context, taskID int64) ([]events.Event, error) { rows, err := s.db.QueryContext(ctx, diff --git a/internal/store/store.go b/internal/store/store.go index 066ead888..e307d6a2b 100644 --- a/internal/store/store.go +++ b/internal/store/store.go @@ -15,6 +15,7 @@ import ( _ "modernc.org/sqlite" + "github.com/samcharles93/archie-core/internal/domain/storecontract" "github.com/samcharles93/archie-core/internal/domain/workflow" "github.com/samcharles93/archie-core/internal/events" ) @@ -131,6 +132,9 @@ func migrateTasks(ctx context.Context, db *sql.DB) error { if err := migrateResourceHistoryToPerKindRequestIDs(ctx, tx); err != nil { return err } + if err := migrateEventTimestamps(ctx, tx); err != nil { + return err + } return finishTaskMigration(ctx, tx, columns) } @@ -172,6 +176,68 @@ func migrateResourceHistoryToPerKindRequestIDs(ctx context.Context, tx *sql.Tx) return nil } +// migrateEventTimestamps normalises the events.at column to the fixed-width +// layout the EventsSince cursor sorts on. Rows written before the cursor +// change used time.RFC3339Nano, which trims trailing zeros, so a whole-second +// row (20 chars) and a full-precision row (30 chars) would compare out of +// chronological order as strings. The ALTER-only migration arms never UPDATE +// values, so this is a separate idempotent step (the v4 precedent): it +// re-evaluates on every open, detects an already-normalised file by length, +// and re-runs cleanly on one that has never been migrated. +func migrateEventTimestamps(ctx context.Context, tx *sql.Tx) error { + updates, err := scanLegacyEventTimestamps(ctx, tx) + if err != nil { + return err + } + for _, u := range updates { + if _, err := tx.ExecContext(ctx, `UPDATE events SET at = ? WHERE id = ?`, u.at, u.id); err != nil { + return err + } + } + return nil +} + +// eventTimestampUpdate is one event row whose at predates the fixed-width +// layout, re-rendered into that layout. +type eventTimestampUpdate struct { + id int64 + at string +} + +// scanLegacyEventTimestamps reads every event row whose at predates the +// fixed-width layout, parses it, and re-renders it. The read cursor is closed +// by the defer before this function returns, so the caller's UPDATEs run on a +// free connection -- the store opens with SetMaxOpenConns(1), and a still-open +// read cursor would hold that single connection and deadlock the writes. +func scanLegacyEventTimestamps(ctx context.Context, tx *sql.Tx) ([]eventTimestampUpdate, error) { + // A fixed-width row is exactly len(EventCursorLayout) characters; anything + // else predates the layout and needs re-formatting. + fixed := strings.Repeat("?", len(storecontract.EventCursorLayout)) + rows, err := tx.QueryContext(ctx, `SELECT id, at FROM events WHERE at NOT GLOB '`+fixed+`'`) + if err != nil { + return nil, err + } + defer rows.Close() + + var updates []eventTimestampUpdate + for rows.Next() { + var u eventTimestampUpdate + if err := rows.Scan(&u.id, &u.at); err != nil { + return nil, err + } + parsed, err := time.Parse(time.RFC3339Nano, u.at) + if err != nil { + return nil, fmt.Errorf("normalise event timestamp %q: %w", u.at, err) + } + u.at = parsed.UTC().Format(storecontract.EventCursorLayout) + updates = append(updates, u) + } + if err := rows.Err(); err != nil { + return nil, err + } + return updates, nil +} + func finishTaskMigration(ctx context.Context, tx *sql.Tx, columns map[string]bool) error { if columns["owner"] && columns["repo"] && columns["pr_number"] { if _, err := tx.ExecContext(ctx, `CREATE INDEX IF NOT EXISTS idx_tasks_pr ON tasks(owner, repo, pr_number)`); err != nil { diff --git a/internal/store/store_test.go b/internal/store/store_test.go index 5d1a24eeb..6d8425cd7 100644 --- a/internal/store/store_test.go +++ b/internal/store/store_test.go @@ -12,6 +12,7 @@ import ( "time" "unicode/utf8" + "github.com/samcharles93/archie-core/internal/domain/storecontract" "github.com/samcharles93/archie-core/internal/domain/workflow" "github.com/samcharles93/archie-core/internal/events" ) @@ -425,11 +426,10 @@ func TestStoreOperationsHonorContextCancellation(t *testing.T) { func TestOversizedEventPayload(t *testing.T) { s := openTest(t) detail := strings.Repeat("x", 5000) - id, err := s.InsertEvent(t.Context(), events.Event{Kind: events.KindLog, Detail: detail}) - if err != nil { + if _, err := s.InsertEvent(t.Context(), events.Event{Kind: events.KindLog, Detail: detail}); err != nil { t.Fatal(err) } - got, err := s.EventsSince(t.Context(), id-1, 1) + got, err := s.EventsSince(t.Context(), "", 1) if err != nil { t.Fatal(err) } @@ -440,14 +440,13 @@ func TestOversizedEventPayload(t *testing.T) { func TestInsertEventWithUnmarshalableData(t *testing.T) { s := openTest(t) - id, err := s.InsertEvent(t.Context(), events.Event{ + if _, err := s.InsertEvent(t.Context(), events.Event{ Kind: events.KindLog, Data: map[string]any{"channel": make(chan int)}, - }) - if err != nil { + }); err != nil { t.Fatal(err) } - got, err := s.EventsSince(t.Context(), id-1, 1) + got, err := s.EventsSince(t.Context(), "", 1) if err != nil { t.Fatal(err) } @@ -473,6 +472,133 @@ func openTest(t *testing.T) *Store { return s } +// TestEventsSinceOrdersByAt pins the total-order invariant: the feed cursor +// must page by at, not by row id. Inserting a later-timestamped event before +// an earlier one must still return the earlier one first, because row id is +// insertion order, not chronology. +func TestEventsSinceOrdersByAt(t *testing.T) { + s := openTest(t) + ctx := t.Context() + later := time.Date(2026, 1, 1, 0, 0, 1, 0, time.UTC) + earlier := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC) + if _, err := s.InsertEvent(ctx, events.Event{Kind: "log", At: later, Detail: "later"}); err != nil { + t.Fatal(err) + } + if _, err := s.InsertEvent(ctx, events.Event{Kind: "log", At: earlier, Detail: "earlier"}); err != nil { + t.Fatal(err) + } + + got, err := s.EventsSince(ctx, "", 10) + if err != nil { + t.Fatal(err) + } + if len(got) != 2 { + t.Fatalf("EventsSince returned %d events, want 2", len(got)) + } + if got[0].Detail != "earlier" || got[1].Detail != "later" { + t.Fatalf("order = %q then %q; want earlier then later (at order, not insertion order)", got[0].Detail, got[1].Detail) + } +} + +// TestInsertEventWritesFixedWidthTimeKey pins the write format the total +// order depends on: a whole-second timestamp and a full-precision one must +// render to the same length, or lexicographic order stops being chronological +// order. time.RFC3339Nano trims trailing zeros, so today this fails. +func TestInsertEventWritesFixedWidthTimeKey(t *testing.T) { + s := openTest(t) + ctx := t.Context() + whole := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC) + precise := time.Date(2026, 1, 1, 0, 0, 0, 123456789, time.UTC) + for _, e := range []events.Event{ + {Kind: "log", At: whole, Detail: "whole"}, + {Kind: "log", At: precise, Detail: "precise"}, + } { + if _, err := s.InsertEvent(ctx, e); err != nil { + t.Fatal(err) + } + } + + rows, err := s.db.QueryContext(ctx, `SELECT at FROM events ORDER BY id`) + if err != nil { + t.Fatal(err) + } + defer func() { _ = rows.Close() }() + var ats []string + for rows.Next() { + var at string + if err := rows.Scan(&at); err != nil { + t.Fatal(err) + } + ats = append(ats, at) + } + if err := rows.Err(); err != nil { + t.Fatal(err) + } + if len(ats) != 2 { + t.Fatalf("stored %d event rows, want 2", len(ats)) + } + if len(ats[0]) != len(ats[1]) { + t.Fatalf("at renders %d and %d chars (%q vs %q); want a fixed-width key", len(ats[0]), len(ats[1]), ats[0], ats[1]) + } +} + +// TestEventsSincePagesIdenticalTimestampsWithoutSkipOrRepeat is the +// regression guard for the id tie-break: many events sharing one timestamp +// must still page exactly once each, in a stable order, no matter what their +// ids are. It walks the returned cursor to exhaustion with a page size smaller +// than the set, so every page boundary is exercised. +func TestEventsSincePagesIdenticalTimestampsWithoutSkipOrRepeat(t *testing.T) { + s := openTest(t) + ctx := t.Context() + at := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC) + const total = 50 + inserted := make(map[int64]string, total) + for i := 0; i < total; i++ { + detail := fmt.Sprintf("event-%d", i) + id, err := s.InsertEvent(ctx, events.Event{Kind: "log", At: at, Detail: detail}) + if err != nil { + t.Fatal(err) + } + inserted[id] = detail + } + + var got []events.Event + cursor := "" + for { + page, err := s.EventsSince(ctx, cursor, 7) + if err != nil { + t.Fatal(err) + } + if len(page) == 0 { + break + } + got = append(got, page...) + last := page[len(page)-1] + cursor = storecontract.EventCursor(last.At, last.ID) + } + + if len(got) != total { + t.Fatalf("paged %d events, want %d (a skip or a repeat)", len(got), total) + } + seen := make(map[int64]bool, total) + for i, e := range got { + if _, ok := inserted[e.ID]; !ok { + t.Fatalf("event %d (%d) was never inserted", i, e.ID) + } + if seen[e.ID] { + t.Fatalf("event %d (%d) delivered twice", i, e.ID) + } + seen[e.ID] = true + if i > 0 { + prev := got[i-1] + if storecontract.EventCursor(prev.At, prev.ID) >= storecontract.EventCursor(e.At, e.ID) { + t.Fatalf("order regressed at %d: %q then %q", i, + storecontract.EventCursor(prev.At, prev.ID), storecontract.EventCursor(e.At, e.ID)) + } + } + } +} + // TestTasksBindingColumnsRoundTrip confirms the binding_id and // binding_version columns added in store schema 2 survive an INSERT and a // full SELECT round-trip through the Task read paths (scanTask, @@ -684,20 +810,24 @@ func TestEnqueueIsIdempotent(t *testing.T) { func TestEventLogRoundTrip(t *testing.T) { s := openTest(t) ctx := context.Background() + at := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC) - id1, err := s.InsertEvent(ctx, events.Event{Kind: "stage_start", TaskID: 1, Stage: "plan", Attempt: 1}) + id1, err := s.InsertEvent(ctx, events.Event{Kind: "stage_start", TaskID: 1, Stage: "plan", Attempt: 1, At: at}) if err != nil || id1 == 0 { t.Fatalf("insert = (%d, %v)", id1, err) } id2, err := s.InsertEvent(ctx, events.Event{ - Kind: "stage_finish", TaskID: 1, Workflow: "implement", Stage: "plan", Attempt: 2, + Kind: "stage_finish", TaskID: 1, Workflow: "implement", Stage: "plan", Attempt: 2, At: at, Data: map[string]any{"duration_ms": 1200}, }) if err != nil || id2 <= id1 { t.Fatalf("second insert = (%d, %v)", id2, err) } - evs, err := s.EventsSince(ctx, id1, 10) + // Two events sharing a timestamp must still page exactly once each via the + // id tie-break: the cursor carries (at, id), so resuming after id1 yields + // only id2. + evs, err := s.EventsSince(ctx, storecontract.EventCursor(at, id1), 10) if err != nil || len(evs) != 1 || evs[0].Kind != "stage_finish" { t.Fatalf("EventsSince = (%+v, %v)", evs, err) } diff --git a/internal/webui/api_tasks_actions_test.go b/internal/webui/api_tasks_actions_test.go index cd27650a7..8e9977c14 100644 --- a/internal/webui/api_tasks_actions_test.go +++ b/internal/webui/api_tasks_actions_test.go @@ -686,7 +686,7 @@ func TestArchiveRemovesOnlyOneTerminalTask(t *testing.T) { if other, err := srv.Store.TaskByIssue(ctx, "acme", "widget", 2); err != nil || other == nil { t.Fatalf("archive removed another task: (%+v, %v)", other, err) } - history, err := srv.Store.EventsSince(ctx, 0, 10) + history, err := srv.Store.EventsSince(ctx, "", 10) if err != nil { t.Fatal(err) } diff --git a/internal/webui/event_pump.go b/internal/webui/event_pump.go index ce3d35600..6aba3e75f 100644 --- a/internal/webui/event_pump.go +++ b/internal/webui/event_pump.go @@ -6,6 +6,7 @@ import ( "fmt" "time" + "github.com/samcharles93/archie-core/internal/domain/storecontract" "github.com/samcharles93/archie-core/internal/events" ) @@ -38,18 +39,19 @@ const ( // // Polling is sufficient rather than a compromise, for the reasons recorded in // docs/architecture/migration-decisions.md ("Dashboard live event delivery"): -// the store serialises writes on a single connection, so the id cursor is -// assigned in commit order and cannot skip; sseStream.drain treats a broadcast -// as a wakeup and re-reads the gap from the store, so nothing here changes SSE -// semantics; and reading the table catches writers that never touch a bus, -// including the audit event ArchiveTask writes inside its transaction. +// EventsSince pages a total order over (at, id) with a fixed-width time key +// and a deterministic id tie-break, so the cursor cannot skip or repeat; +// sseStream.drain treats a broadcast as a wakeup and re-reads the gap from the +// store, so nothing here changes SSE semantics; and reading the table catches +// writers that never touch a bus, including the audit event ArchiveTask writes +// inside its transaction. type eventPump struct { store eventReader log func(string, ...any) broadcast func(events.Event) - // watermark is the highest event id already handed to broadcast. It only + // watermark is the highest event cursor already handed to broadcast. It only // ever moves forward, and only past an event that was delivered. - watermark int64 + watermark string // primeRetryMin is the first backoff between priming attempts. Zero // means eventPumpPrimeRetryMin; a test sets it small. primeRetryMin time.Duration @@ -85,7 +87,7 @@ func (p *eventPump) primeWithRetry(ctx context.Context) error { // eventReader is the one method the pump needs, so a test can drive it // without a whole TaskStore. type eventReader interface { - EventsSince(ctx context.Context, sinceID int64, limit int) ([]events.Event, error) + EventsSince(ctx context.Context, cursor string, limit int) ([]events.Event, error) } func (s *Server) newEventPump() *eventPump { @@ -122,8 +124,8 @@ func (p *eventPump) prime(ctx context.Context) error { } before := p.watermark for _, e := range batch { - if e.ID > p.watermark { - p.watermark = e.ID + if cursor := storecontract.EventCursor(e.At, e.ID); cursor > p.watermark { + p.watermark = cursor } } // A short page is the end of the table. The watermark check is the @@ -147,11 +149,12 @@ func (p *eventPump) deliver(ctx context.Context) (int, error) { } before := p.watermark for _, e := range batch { - if e.ID <= p.watermark { + cursor := storecontract.EventCursor(e.At, e.ID) + if cursor <= p.watermark { continue } p.broadcast(e) - p.watermark = e.ID + p.watermark = cursor delivered++ } if len(batch) < eventPumpPageSize || p.watermark == before { diff --git a/internal/webui/event_pump_test.go b/internal/webui/event_pump_test.go index 29f2d4712..c2f953073 100644 --- a/internal/webui/event_pump_test.go +++ b/internal/webui/event_pump_test.go @@ -10,6 +10,7 @@ import ( "testing" "time" + "github.com/samcharles93/archie-core/internal/domain/storecontract" "github.com/samcharles93/archie-core/internal/events" ) @@ -26,8 +27,9 @@ func newTestPump(t *testing.T, srv *Server) *eventPump { // (sseStream.catchUp), not the pump's job. func TestEventPumpPrimesPastHistoryThenDeliversNewEvents(t *testing.T) { srv := newRemoteTestServer(t) + t0 := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC) for _, detail := range []string{"first", "second"} { - if _, err := srv.Store.InsertEvent(t.Context(), events.Event{Kind: "task_created", Detail: detail}); err != nil { + if _, err := srv.Store.InsertEvent(t.Context(), events.Event{Kind: "task_created", Detail: detail, At: t0}); err != nil { t.Fatalf("seed history: %v", err) } } @@ -45,7 +47,8 @@ func TestEventPumpPrimesPastHistoryThenDeliversNewEvents(t *testing.T) { t.Fatalf("pump delivered %d pre-existing events; priming must advance past history", delivered) } - id, err := srv.Store.InsertEvent(t.Context(), events.Event{Kind: "task_started", Detail: "after the pump"}) + t1 := time.Date(2026, 1, 1, 0, 0, 1, 0, time.UTC) + id, err := srv.Store.InsertEvent(t.Context(), events.Event{Kind: "task_started", Detail: "after the pump", At: t1}) if err != nil { t.Fatalf("insert live event: %v", err) } @@ -56,8 +59,8 @@ func TestEventPumpPrimesPastHistoryThenDeliversNewEvents(t *testing.T) { if delivered != 1 { t.Fatalf("pump delivered %d events, want the 1 inserted after priming", delivered) } - if pump.watermark != id { - t.Fatalf("watermark = %d, want %d: the pump must not refetch a delivered event", pump.watermark, id) + if want := storecontract.EventCursor(t1, id); pump.watermark != want { + t.Fatalf("watermark = %q, want %q: the pump must not refetch a delivered event", pump.watermark, want) } } @@ -77,7 +80,7 @@ func TestEventPumpKeepsItsWatermarkAcrossAStoreFailure(t *testing.T) { t.Fatal("deliver against a cancelled context returned no error") } if pump.watermark != before { - t.Fatalf("watermark moved to %d on a failed fetch, want %d", pump.watermark, before) + t.Fatalf("watermark moved to %q on a failed fetch, want %q", pump.watermark, before) } } @@ -172,7 +175,7 @@ type flakyEventReader struct { calls int } -func (f *flakyEventReader) EventsSince(context.Context, int64, int) ([]events.Event, error) { +func (f *flakyEventReader) EventsSince(context.Context, string, int) ([]events.Event, error) { f.calls++ if f.calls <= f.failures { return nil, errors.New("connection refused") diff --git a/internal/webui/sse.go b/internal/webui/sse.go index 5acc24388..36729a06f 100644 --- a/internal/webui/sse.go +++ b/internal/webui/sse.go @@ -5,7 +5,6 @@ import ( "encoding/json" "log/slog" "net/http" - "strconv" "github.com/samcharles93/archie-core/internal/domain/storecontract" "github.com/samcharles93/archie-core/internal/events" @@ -46,22 +45,26 @@ func (s *Server) handleSSE(w http.ResponseWriter, r *http.Request) { conn, unregister := s.registerSSEConn() defer unregister() - if !stream.catchUp(r.Context(), 0) { + if !stream.catchUp(r.Context(), "") { return } stream.drain(r.Context(), conn) } -// sseSince resolves the event ID a client wants to resume after. A valid +// 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. -func sseSince(r *http.Request) int64 { - since, _ := strconv.ParseInt(r.URL.Query().Get("since"), 10, 64) +// 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 != "" { - if fromHeader, err := strconv.ParseInt(header, 10, 64); err == nil { - since = fromHeader - } + since = header + } + if _, _, ok := storecontract.ParseEventCursor(since); !ok { + return "" } return since } @@ -86,19 +89,19 @@ func (s *Server) registerSSEConn() (chan events.Event, func()) { // 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" watermark that deduplicates the backlog against live +// 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 int64 + 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 int64) (*sseStream, bool) { +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 @@ -116,7 +119,7 @@ func (s *sseStream) send(e events.Event) bool { s.log.Error("sse event marshal failed", "error", err, "event_id", e.ID) return false } - if _, err := s.w.Write([]byte("id: " + strconv.FormatInt(e.ID, 10) + "\ndata: " + string(body) + "\n\n")); err != nil { + 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() @@ -134,10 +137,10 @@ func (s *sseStream) writeErrorComment(err error) { } // catchUp sends every persisted event after s.since, advancing s.since as -// it goes, until it reaches targetID (0 means "drain everything currently +// 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, targetID int64) bool { +func (s *sseStream) catchUp(ctx context.Context, target string) bool { for { before := s.since backlog, err := s.store.EventsSince(ctx, s.since, sseBacklogPageSize) @@ -146,7 +149,7 @@ func (s *sseStream) catchUp(ctx context.Context, targetID int64) bool { s.writeErrorComment(err) return false } - reachedTarget, ok := s.sendPage(backlog, targetID) + reachedTarget, ok := s.sendPage(backlog, target) if !ok { return false } @@ -157,24 +160,25 @@ func (s *sseStream) catchUp(ctx context.Context, targetID int64) bool { } // sendPage sends every event in backlog newer than s.since, advancing it as -// it goes. reachedTarget reports whether targetID was reached mid-page -- +// 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 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) { +// 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 { - if e.ID <= s.since { + cursor := storecontract.EventCursor(e.At, e.ID) + if cursor <= s.since { continue } if sseVisible(e) && !s.send(e) { return false, false } - s.since = e.ID - if targetID > 0 && s.since >= targetID { + s.since = cursor + if target != "" && cursor >= target { return true, true } } @@ -192,17 +196,18 @@ func (s *sseStream) drain(ctx context.Context, conn <-chan events.Event) { case <-ctx.Done(): return case e := <-conn: - if e.ID <= s.since { + cursor := storecontract.EventCursor(e.At, e.ID) + if cursor <= s.since { continue } - if !s.catchUp(ctx, e.ID) { + if !s.catchUp(ctx, cursor) { return } - if e.ID > s.since { + if cursor > s.since { if sseVisible(e) && !s.send(e) { return } - s.since = e.ID + s.since = cursor } } } diff --git a/internal/webui/sse_test.go b/internal/webui/sse_test.go index de7ac3413..dd955ac07 100644 --- a/internal/webui/sse_test.go +++ b/internal/webui/sse_test.go @@ -8,12 +8,24 @@ import ( "net/http/httptest" "strings" "testing" + "time" + "github.com/samcharles93/archie-core/internal/domain/storecontract" "github.com/samcharles93/archie-core/internal/events" "github.com/samcharles93/archie-core/internal/store" ) -func newTestSSEStream(since int64) (*sseStream, *httptest.ResponseRecorder) { +// testAt is a fixed instant every synthetic event in this file shares, so a +// cursor can be built for an event from its id alone. Two events sharing a +// timestamp exercise the id tie-break, which is the point of the cursor. +var testAt = time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC) + +// cursor builds the opaque resume cursor for a synthetic event with id id. +func cursor(id int64) string { + return storecontract.EventCursor(testAt, id) +} + +func newTestSSEStream(since string) (*sseStream, *httptest.ResponseRecorder) { rec := httptest.NewRecorder() return &sseStream{ log: slog.New(slog.DiscardHandler), @@ -24,13 +36,19 @@ func newTestSSEStream(since int64) (*sseStream, *httptest.ResponseRecorder) { } // sendPage is the seam catchUp's page-boundary logic depends on: it must -// stop the instant targetID is reached, even mid-page, rather than sending +// stop the instant target is reached, even mid-page, rather than sending // events beyond what the caller asked for. func TestSSEStreamSendPageStopsAtTargetMidPage(t *testing.T) { - s, _ := newTestSSEStream(0) - backlog := []events.Event{{ID: 1}, {ID: 2}, {ID: 3}, {ID: 4}, {ID: 5}} + s, _ := newTestSSEStream("") + backlog := []events.Event{ + {ID: 1, At: testAt}, + {ID: 2, At: testAt}, + {ID: 3, At: testAt}, + {ID: 4, At: testAt}, + {ID: 5, At: testAt}, + } - reachedTarget, ok := s.sendPage(backlog, 3) + reachedTarget, ok := s.sendPage(backlog, cursor(3)) if !ok { t.Fatal("sendPage() ok = false, want true") @@ -38,18 +56,18 @@ func TestSSEStreamSendPageStopsAtTargetMidPage(t *testing.T) { if !reachedTarget { t.Fatal("reachedTarget = false, want true: target 3 was inside the page") } - if s.since != 3 { - t.Fatalf("since = %d, want 3 (stopped at the target, not the end of the page)", s.since) + if s.since != cursor(3) { + t.Fatalf("since = %q, want %q (stopped at the target, not the end of the page)", s.since, cursor(3)) } } // A target beyond the page must not be reported reached: the caller needs // another page. func TestSSEStreamSendPageDoesNotReportTargetReachedBeyondThePage(t *testing.T) { - s, _ := newTestSSEStream(0) - backlog := []events.Event{{ID: 1}, {ID: 2}, {ID: 3}} + s, _ := newTestSSEStream("") + backlog := []events.Event{{ID: 1, At: testAt}, {ID: 2, At: testAt}, {ID: 3, At: testAt}} - reachedTarget, ok := s.sendPage(backlog, 10) + reachedTarget, ok := s.sendPage(backlog, cursor(10)) if !ok { t.Fatal("sendPage() ok = false, want true") @@ -57,36 +75,36 @@ func TestSSEStreamSendPageDoesNotReportTargetReachedBeyondThePage(t *testing.T) if reachedTarget { t.Fatal("reachedTarget = true, want false: target 10 was never reached in this page") } - if s.since != 3 { - t.Fatalf("since = %d, want 3 (the whole page was sent)", s.since) + if s.since != cursor(3) { + t.Fatalf("since = %q, want %q (the whole page was sent)", s.since, cursor(3)) } } -// targetID == 0 means "drain everything available"; sendPage must never +// target == "" means "drain everything available"; sendPage must never // treat it as a target to stop at, or catchUp's initial call (which always -// passes 0) would stop after the very first event. +// passes "") would stop after the very first event. func TestSSEStreamSendPageIgnoresZeroTarget(t *testing.T) { - s, _ := newTestSSEStream(0) - backlog := []events.Event{{ID: 1}, {ID: 2}, {ID: 3}} + s, _ := newTestSSEStream("") + backlog := []events.Event{{ID: 1, At: testAt}, {ID: 2, At: testAt}, {ID: 3, At: testAt}} - reachedTarget, ok := s.sendPage(backlog, 0) + reachedTarget, ok := s.sendPage(backlog, "") if !ok || reachedTarget { - t.Fatalf("sendPage(targetID=0) = (%v, %v), want (false, true)", reachedTarget, ok) + t.Fatalf("sendPage(target=\"\") = (%v, %v), want (false, true)", reachedTarget, ok) } - if s.since != 3 { - t.Fatalf("since = %d, want 3 (the whole page was sent)", s.since) + if s.since != cursor(3) { + t.Fatalf("since = %q, want %q (the whole page was sent)", s.since, cursor(3)) } } // A send failure must stop the page immediately and report failure, not // silently skip the failed event and keep going. func TestSSEStreamSendPageStopsOnWriteFailure(t *testing.T) { - s, _ := newTestSSEStream(0) + s, _ := newTestSSEStream("") s.w = failingWriter{} - backlog := []events.Event{{ID: 1}, {ID: 2}} + backlog := []events.Event{{ID: 1, At: testAt}, {ID: 2, At: testAt}} - reachedTarget, ok := s.sendPage(backlog, 0) + reachedTarget, ok := s.sendPage(backlog, "") if ok { t.Fatal("sendPage() ok = true, want false: the write failed") @@ -94,8 +112,8 @@ func TestSSEStreamSendPageStopsOnWriteFailure(t *testing.T) { if reachedTarget { t.Fatal("reachedTarget = true, want false on failure") } - if s.since != 0 { - t.Fatalf("since = %d, want 0 (unchanged: the first send already failed)", s.since) + if s.since != "" { + t.Fatalf("since = %q, want empty (unchanged: the first send already failed)", s.since) } } @@ -110,16 +128,16 @@ func (failingWriter) WriteHeader(statusCode int) {} var errWriteFailed = errors.New("write failed") // fakeEventStore serves EventsSince from a fixed, pre-seeded slice, paginating -// the same way the real store does: up to limit events with id > sinceID. +// the same way the real store does: up to limit events after cursor. type fakeEventStore struct { store.TaskStore all []events.Event } -func (f *fakeEventStore) EventsSince(_ context.Context, sinceID int64, limit int) ([]events.Event, error) { +func (f *fakeEventStore) EventsSince(_ context.Context, since string, limit int) ([]events.Event, error) { page := make([]events.Event, 0, limit) for _, e := range f.all { - if e.ID <= sinceID { + if storecontract.EventCursor(e.At, e.ID) <= since { continue } page = append(page, e) @@ -137,40 +155,40 @@ func TestSSEStreamCatchUpPagesAcrossMultipleFetches(t *testing.T) { const total = sseBacklogPageSize + 50 all := make([]events.Event, total) for i := range all { - all[i] = events.Event{ID: int64(i + 1)} + all[i] = events.Event{ID: int64(i + 1), At: testAt} } - s, rec := newTestSSEStream(0) + s, rec := newTestSSEStream("") s.store = &fakeEventStore{all: all} - if !s.catchUp(context.Background(), 0) { + if !s.catchUp(context.Background(), "") { t.Fatal("catchUp() = false, want true") } - if s.since != int64(total) { - t.Fatalf("since = %d, want %d (every event drained across pages)", s.since, total) + if s.since != cursor(total) { + t.Fatalf("since = %q, want %q (every event drained across pages)", s.since, cursor(total)) } if n := countSSEFrames(rec.Body.String()); n != total { t.Fatalf("sent %d SSE frames, want %d", n, total) } } -// A targetID landing exactly on the last event of a full page must still be +// A target landing exactly on the last event of a full page must still be // recognised as reached, not mistaken for "page not yet exhausted, fetch // another". func TestSSEStreamCatchUpStopsAtTargetOnPageBoundary(t *testing.T) { all := make([]events.Event, sseBacklogPageSize+50) for i := range all { - all[i] = events.Event{ID: int64(i + 1)} + all[i] = events.Event{ID: int64(i + 1), At: testAt} } - s, _ := newTestSSEStream(0) + s, _ := newTestSSEStream("") s.store = &fakeEventStore{all: all} - if !s.catchUp(context.Background(), sseBacklogPageSize) { + if !s.catchUp(context.Background(), cursor(sseBacklogPageSize)) { t.Fatal("catchUp() = false, want true") } - if s.since != sseBacklogPageSize { - t.Fatalf("since = %d, want %d (stopped exactly at the target, not the whole backlog)", s.since, sseBacklogPageSize) + if s.since != cursor(sseBacklogPageSize) { + t.Fatalf("since = %q, want %q (stopped exactly at the target, not the whole backlog)", s.since, cursor(sseBacklogPageSize)) } } @@ -186,23 +204,23 @@ func countSSEFrames(body string) int { // 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. +// returns events after since. func TestSSEStreamSendPageSkipsNoiseKindsButAdvancesSince(t *testing.T) { - s, rec := newTestSSEStream(0) + s, rec := newTestSSEStream("") backlog := []events.Event{ - {ID: 1, Kind: events.KindTaskQueued}, - {ID: 2, Kind: events.KindTurnCompleted, Detail: "100000000"}, - {ID: 3, Kind: events.KindTurnCompleted, Detail: "100000000"}, - {ID: 4, Kind: events.KindParked}, + {ID: 1, At: testAt, Kind: events.KindTaskQueued}, + {ID: 2, At: testAt, Kind: events.KindTurnCompleted, Detail: "100000000"}, + {ID: 3, At: testAt, Kind: events.KindTurnCompleted, Detail: "100000000"}, + {ID: 4, At: testAt, Kind: events.KindParked}, } - reachedTarget, ok := s.sendPage(backlog, 0) + reachedTarget, ok := s.sendPage(backlog, "") 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 s.since != cursor(4) { + t.Fatalf("since = %q, want %q (advanced past every event, including filtered ones)", s.since, cursor(4)) } if n := countSSEFrames(rec.Body.String()); n != 2 { t.Fatalf("sent %d SSE frames, want 2 (turn_completed excluded)", n) @@ -211,3 +229,41 @@ func TestSSEStreamSendPageSkipsNoiseKindsButAdvancesSince(t *testing.T) { t.Error("SSE body contains a turn_completed frame, want it excluded entirely") } } + +// TestSSESinceDegradesUnparseableCursorToBeginning is the legacy-cursor +// tolerance guard: a browser holding the pre-cursor integer id: field, or a +// ?since= carrying garbage, must resume from the beginning rather than error +// or skip. This deliberately preserves the old failed-ParseInt-yields-0 +// degradation. +func TestSSESinceDegradesUnparseableCursorToBeginning(t *testing.T) { + valid := storecontract.EventCursor(testAt, 1) + tests := []struct { + name string + since string + header string + want string + }{ + {name: "absent", want: ""}, + {name: "legacy integer header", header: "42", want: ""}, + {name: "garbage query", since: "not-a-cursor", want: ""}, + {name: "garbage header", header: "garbage", want: ""}, + {name: "valid query cursor", since: valid, want: valid}, + {name: "valid header cursor beats garbage query", since: "garbage", header: valid, want: valid}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + req := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/events", nil) + if tt.since != "" { + q := req.URL.Query() + q.Set("since", tt.since) + req.URL.RawQuery = q.Encode() + } + if tt.header != "" { + req.Header.Set("Last-Event-ID", tt.header) + } + if got := sseSince(req); got != tt.want { + t.Fatalf("sseSince = %q, want %q", got, tt.want) + } + }) + } +} diff --git a/internal/webui/webui_test.go b/internal/webui/webui_test.go index 5b347cfb6..5b70d4c7f 100644 --- a/internal/webui/webui_test.go +++ b/internal/webui/webui_test.go @@ -26,6 +26,7 @@ import ( "testing" "time" + "github.com/samcharles93/archie-core/internal/domain/storecontract" "github.com/samcharles93/archie-core/internal/domain/workflow" "github.com/samcharles93/archie-core/internal/events" "github.com/samcharles93/archie-core/internal/store" @@ -95,11 +96,11 @@ func (s *stubStore) TokensByDay(ctx context.Context, days int) ([]store.DayToken return s.TaskStore.TokensByDay(ctx, days) } -func (s *stubStore) EventsSince(ctx context.Context, sinceID int64, limit int) ([]events.Event, error) { +func (s *stubStore) EventsSince(ctx context.Context, cursor string, limit int) ([]events.Event, error) { if s.eventsSinceErr != nil { return nil, s.eventsSinceErr } - return s.TaskStore.EventsSince(ctx, sinceID, limit) + return s.TaskStore.EventsSince(ctx, cursor, limit) } func (s *stubStore) TaskEvents(ctx context.Context, taskID int64) ([]events.Event, error) { @@ -350,11 +351,12 @@ func TestHandleSSEUsesLastEventIDForCatchUp(t *testing.T) { srv := newTestServer(t) ctx, cancel := context.WithTimeout(t.Context(), 3*time.Second) defer cancel() - first, err := srv.Store.InsertEvent(ctx, events.Event{Kind: "log", Detail: "first"}) + at := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC) + first, err := srv.Store.InsertEvent(ctx, events.Event{Kind: "log", Detail: "first", At: at}) if err != nil { t.Fatal(err) } - if _, err := srv.Store.InsertEvent(ctx, events.Event{Kind: "log", Detail: "second"}); err != nil { + if _, err := srv.Store.InsertEvent(ctx, events.Event{Kind: "log", Detail: "second", At: at}); err != nil { t.Fatal(err) } @@ -364,7 +366,7 @@ func TestHandleSSEUsesLastEventIDForCatchUp(t *testing.T) { if err != nil { t.Fatal(err) } - req.Header.Set("Last-Event-ID", strconv.FormatInt(first, 10)) + req.Header.Set("Last-Event-ID", storecontract.EventCursor(at, first)) resp, err := http.DefaultClient.Do(req) if err != nil { t.Fatal(err) @@ -394,7 +396,8 @@ func TestHandleSSEBacklogAndLive(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"}) + t0 := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC) + id, err := srv.Store.InsertEvent(ctx, events.Event{Kind: "log", Detail: "backlog", At: t0}) if err != nil { t.Fatal(err) } @@ -426,7 +429,7 @@ func TestHandleSSEBacklogAndLive(t *testing.T) { // Now broadcast a live event and confirm it's delivered too. Give // the SSE handler a moment to register its subscriber. time.Sleep(50 * time.Millisecond) - srv.Broadcast(events.Event{ID: id + 1, Kind: "log", Detail: "live"}) + srv.Broadcast(events.Event{ID: id + 1, Kind: "log", Detail: "live", At: t0.Add(time.Second)}) if _, err := readLineUntil(reader, "live"); err != nil { t.Fatalf("live event not received: %v", err) @@ -442,7 +445,8 @@ 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"}) + t0 := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC) + id, err := srv.Store.InsertEvent(ctx, events.Event{Kind: "log", Detail: "backlog", At: t0}) if err != nil { t.Fatal(err) } @@ -469,8 +473,8 @@ func TestHandleSSEFiltersTurnCompletedFromLiveBroadcast(t *testing.T) { } time.Sleep(50 * time.Millisecond) - srv.Broadcast(events.Event{ID: id + 1, Kind: events.KindTurnCompleted, Detail: "100000000"}) - srv.Broadcast(events.Event{ID: id + 2, Kind: "log", Detail: "after-noise"}) + srv.Broadcast(events.Event{ID: id + 1, Kind: events.KindTurnCompleted, Detail: "100000000", At: t0.Add(time.Second)}) + srv.Broadcast(events.Event{ID: id + 2, Kind: "log", Detail: "after-noise", At: t0.Add(2 * time.Second)}) seen, err := readLinesUntil(reader, "after-noise") if err != nil { diff --git a/proto/state/v1/state.proto b/proto/state/v1/state.proto index c24340147..7778ca162 100644 --- a/proto/state/v1/state.proto +++ b/proto/state/v1/state.proto @@ -470,7 +470,10 @@ message InsertEventRequest { Event event = 1; } message InsertEventResponse { int64 id = 1; } message EventsSinceRequest { - int64 since_id = 1; + // cursor is the opaque resume cursor returned by the previous page (the SSE + // id: field, echoed back as Last-Event-ID). Empty means "from the + // beginning". + string cursor = 1; int64 limit = 2; } message EventsSinceResponse { repeated Event events = 1; }