From de1a89907260e8e3c0121514032b1968fcb8cec6 Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Tue, 4 Aug 2026 22:47:48 +1000 Subject: [PATCH] fix(gateway): make both backends agree on newest-first SQLite ordered List and GetByChannel by created_at while NellDB reversed document order, i.e. by session ID. Both documented "newest first", both passed the shared suite, and neither meant recency -- so a cutover between backends could change which session a restart resumes, and Touch, the live path that makes a session the newest one, changed nothing on either. Recency is now LastActiveAt when set and CreatedAt otherwise, on both sides, with an index matching the SQLite expression. The conformance suite gains the case that would have caught this: three sessions whose creation order, ID order and use order all disagree, plus a Touch, asserted against List and GetByChannel on both backends. Refs archie-core-8u8.2 --- internal/gateway/session_store_sqlite.go | 17 +++- .../gateway/sessionstore_conformance_test.go | 83 +++++++++++++++++++ 2 files changed, 98 insertions(+), 2 deletions(-) diff --git a/internal/gateway/session_store_sqlite.go b/internal/gateway/session_store_sqlite.go index 2fdabf9e7..d76703828 100644 --- a/internal/gateway/session_store_sqlite.go +++ b/internal/gateway/session_store_sqlite.go @@ -58,6 +58,11 @@ CREATE TABLE IF NOT EXISTS sessions ( last_active_at INTEGER NOT NULL DEFAULT 0 ); CREATE INDEX IF NOT EXISTS idx_sessions_channel ON sessions(platform, channel_id); +-- Ordering is by recency, which is last_active_at when set and created_at +-- otherwise (see sessionRecencySQL). The index matches that expression so +-- the newest-first listings do not sort at query time. +CREATE INDEX IF NOT EXISTS idx_sessions_recency + ON sessions(platform, channel_id, MAX(last_active_at, created_at) DESC); CREATE TABLE IF NOT EXISTS messages ( id INTEGER PRIMARY KEY AUTOINCREMENT, @@ -170,7 +175,7 @@ func (s *sqliteSessionStore) Get(ctx context.Context, sessionID string) (*Sessio func (s *sqliteSessionStore) GetByChannel(ctx context.Context, platform, channelID string) ([]SessionContext, error) { rows, err := s.db.QueryContext(ctx, - sessionSelect+` WHERE platform = ? AND channel_id = ? ORDER BY created_at DESC`, + sessionSelect+` WHERE platform = ? AND channel_id = ? ORDER BY `+sessionRecencySQL+` DESC`, platform, channelID) if err != nil { return nil, fmt.Errorf("sessionstore: get by channel: %w", err) @@ -214,13 +219,21 @@ func (s *sqliteSessionStore) Touch(ctx context.Context, sessionID string) error } func (s *sqliteSessionStore) List(ctx context.Context) ([]SessionContext, error) { - rows, err := s.db.QueryContext(ctx, sessionSelect+` ORDER BY created_at DESC`) + rows, err := s.db.QueryContext(ctx, sessionSelect+` ORDER BY `+sessionRecencySQL+` DESC`) if err != nil { return nil, fmt.Errorf("sessionstore: list: %w", err) } return scanSessions(rows) } +// sessionRecencySQL is a session's recency: LastActiveAt when it has been +// set, CreatedAt otherwise. It mirrors sessionRecency on the NellDB side, so +// both backends answer "newest first" with the same order -- they previously +// disagreed (created_at here, document order there) and the shared suite did +// not catch it, which meant a backend cutover could change which session a +// restart resumed. +const sessionRecencySQL = `MAX(last_active_at, created_at)` + const sessionSelect = ` SELECT session_id, platform, bot_user, channel_id, thread_id, title, parent_session_id, branch_name, created_at, last_active_at diff --git a/internal/gateway/sessionstore_conformance_test.go b/internal/gateway/sessionstore_conformance_test.go index 5bf358240..1c1852147 100644 --- a/internal/gateway/sessionstore_conformance_test.go +++ b/internal/gateway/sessionstore_conformance_test.go @@ -66,6 +66,7 @@ func runSessionStoreSuite(t *testing.T, newStore func(t *testing.T) SessionStore t.Helper() t.Run("SessionCRUD", func(t *testing.T) { testSessionCRUD(t, newStore) }) + t.Run("NewestFirstOrdering", func(t *testing.T) { testNewestFirstOrdering(t, newStore) }) t.Run("SaveMessagesAppends", func(t *testing.T) { testSaveMessagesAppends(t, newStore) }) t.Run("DeleteThenSaveDoesNotReuseKeys", func(t *testing.T) { testDeleteThenSaveDoesNotReuseKeys(t, newStore) }) t.Run("MessagesOrderByTimestamp", func(t *testing.T) { testMessagesOrderByTimestamp(t, newStore) }) @@ -1214,3 +1215,85 @@ func testSearchEmptyQueryReturnsEmptyPage(t *testing.T, newStore func(t *testing func TestSessionStoreConformanceNell(t *testing.T) { runSessionStoreSuite(t, func(t *testing.T) SessionStore { return NewSessionStoreMemory("test-node") }) } + +// testNewestFirstOrdering pins the observable newest-first contract that List +// and GetByChannel both document. +// +// The two backends disagreed and the suite did not notice: SQLite ordered by +// created_at while NellDB reversed document order (by session ID). Recency is +// LastActiveAt -- that is what Touch updates and what "newest" means to +// someone reading /sessions -- so a session created first but used most +// recently sorts first, and a cutover between backends must not change which +// session a restart resumes. +func testNewestFirstOrdering(t *testing.T, newStore func(t *testing.T) SessionStore) { + ctx := context.Background() + s := newStore(t) + t.Cleanup(func() { _ = s.Close() }) + + // Created oldest-to-newest, but used in a different order, so ordering + // by creation and ordering by recency give different answers. IDs also + // disagree with both, so document order cannot pass by luck. + seed := []struct { + id string + created time.Duration + active time.Duration + }{ + {"zzz-created-first", 0, 3 * time.Hour}, + {"aaa-created-second", time.Hour, time.Hour}, + {"mmm-created-last", 2 * time.Hour, 2 * time.Hour}, + } + for _, sd := range seed { + if err := s.Save(ctx, SessionContext{ + SessionID: sd.id, + Source: SessionSource{ + Platform: "telegram", + BotUser: "archie", + ChannelID: "chat-1", + }, + CreatedAt: base.Add(sd.created), + LastActiveAt: base.Add(sd.active), + }); err != nil { + t.Fatalf("Save %s: %v", sd.id, err) + } + } + + want := []string{"zzz-created-first", "mmm-created-last", "aaa-created-second"} + + assertOrder := func(what string, got []SessionContext) { + t.Helper() + if len(got) != len(want) { + t.Fatalf("%s returned %d sessions, want %d", what, len(got), len(want)) + } + for i, w := range want { + if got[i].SessionID != w { + t.Errorf("%s[%d] = %q, want %q (full order %v)", what, i, got[i].SessionID, w, sessionIDs(got)) + } + } + } + + list, err := s.List(ctx) + if err != nil { + t.Fatalf("List: %v", err) + } + assertOrder("List", list) + + byChannel, err := s.GetByChannel(ctx, "telegram", "chat-1") + if err != nil { + t.Fatalf("GetByChannel: %v", err) + } + assertOrder("GetByChannel", byChannel) + + // Touch is the live path that makes a session the newest one. Ordering + // that ignores it silently resumes a stale conversation. + if err := s.Touch(ctx, "aaa-created-second"); err != nil { + t.Fatalf("Touch: %v", err) + } + touched, err := s.GetByChannel(ctx, "telegram", "chat-1") + if err != nil { + t.Fatalf("GetByChannel after Touch: %v", err) + } + if len(touched) == 0 || touched[0].SessionID != "aaa-created-second" { + t.Fatalf("after Touch, GetByChannel[0] = %v, want the touched session first", + sessionIDs(touched)) + } +}