refactor(gateway): replace the SQLite session store with an in-memory store

This commit is contained in:
sam committed 2026-09-24 14:26:42 +10:00
1 parent c485998160
commit 57ddd4e002
11 files changed
+581 -2666

No files matched your search

+2 -8
View File
@@ -12,14 +12,8 @@ import (
// compressBackends provides the persistent store used by compression tests.
func compressBackends() map[string]func(t *testing.T) SessionStore {
return map[string]func(t *testing.T) SessionStore{
"sqlite": func(t *testing.T) SessionStore {
s, err := NewSQLiteSessionStoreMemory()
if err != nil {
t.Fatalf("NewSQLiteSessionStoreMemory: %v", err)
}
t.Cleanup(func() { _ = s.Close() })
return s
},
"memory": func(*testing.T) SessionStore { return NewSessionStoreMemory() },
"postgres": func(t *testing.T) SessionStore { return newPostgresStore(t) },
}
}
+1 -4
View File
@@ -38,10 +38,7 @@ func (s *countingReplaceStore) ReplaceMessages(ctx context.Context, sessionID st
// written to it.
func newTriggerStore(t *testing.T) *countingReplaceStore {
t.Helper()
inner, err := NewSQLiteSessionStoreMemory()
if err != nil {
t.Fatalf("NewSQLiteSessionStoreMemory: %v", err)
}
inner := NewSessionStoreMemory()
t.Cleanup(func() { _ = inner.Close() })
ledger, ok := inner.(TurnLedger)
if !ok {
-9
View File
@@ -10,15 +10,6 @@ import (
"github.com/samcharles93/archie-core/internal/domain/messaging"
)
// NewSessionStoreMemory returns an in-memory SQLite SessionStore for tests.
func NewSessionStoreMemory() SessionStore {
store, err := NewSQLiteSessionStoreMemory()
if err != nil {
panic(err)
}
return store
}
// sessionRecency is the later of a session's creation and activity times.
func sessionRecency(sc SessionContext) time.Time {
if sc.LastActiveAt.After(sc.CreatedAt) {
+482
View File
@@ -0,0 +1,482 @@
package gateway
import (
"context"
"fmt"
"slices"
"strings"
"sync"
"time"
"unicode"
"github.com/samcharles93/archie-core/internal/domain/messaging"
)
// memorySessionStore is the in-process SessionStore for tests and embedders
// that need no durability. It follows the PostgreSQL store's contract
// (sessionstore_conformance_test.go) including the optional TurnLedger,
// TurnHistory and TurnReplayStore capabilities: millisecond timestamps, the
// strictly-increasing append clamp, redelivery dedup on canonical IDs, and a
// search that ANDs lowercase word terms over sender and text.
type memorySessionStore struct {
mu sync.Mutex
sessions map[string]SessionContext
messages map[string][]memoryMessage
turns map[string]TurnRecord
seq int64
}
type memoryMessage struct {
seq int64
ts int64
msg messaging.Message
}
// NewSessionStoreMemory returns an empty in-memory SessionStore.
func NewSessionStoreMemory() SessionStore {
return &memorySessionStore{
sessions: map[string]SessionContext{},
messages: map[string][]memoryMessage{},
turns: map[string]TurnRecord{},
}
}
func (s *memorySessionStore) Close() error { return nil }
func truncateMilli(t time.Time) time.Time { return time.UnixMilli(t.UnixMilli()).UTC() }
// ── Sessions ────────────────────────────────────────────────────────────────
func (s *memorySessionStore) Save(_ context.Context, sc SessionContext) error {
s.mu.Lock()
defer s.mu.Unlock()
sc.CreatedAt = truncateMilli(sc.CreatedAt)
sc.LastActiveAt = truncateMilli(sc.LastActiveAt)
s.sessions[sc.SessionID] = sc
return nil
}
func (s *memorySessionStore) Get(_ context.Context, sessionID string) (*SessionContext, error) {
s.mu.Lock()
defer s.mu.Unlock()
sc, ok := s.sessions[sessionID]
if !ok {
return nil, nil
}
return &sc, nil
}
func (s *memorySessionStore) GetByChannel(_ context.Context, platform, channelID string) ([]SessionContext, error) {
s.mu.Lock()
defer s.mu.Unlock()
return s.sortedSessions(func(sc SessionContext) bool {
return sc.Source.Platform == platform && sc.Source.ChannelID == channelID
}), nil
}
func (s *memorySessionStore) List(_ context.Context) ([]SessionContext, error) {
s.mu.Lock()
defer s.mu.Unlock()
return s.sortedSessions(func(SessionContext) bool { return true }), nil
}
func (s *memorySessionStore) sortedSessions(keep func(SessionContext) bool) []SessionContext {
out := make([]SessionContext, 0, len(s.sessions))
for _, sc := range s.sessions {
if keep(sc) {
out = append(out, sc)
}
}
slices.SortFunc(out, func(a, b SessionContext) int {
if c := sessionRecency(b).Compare(sessionRecency(a)); c != 0 {
return c
}
return strings.Compare(a.SessionID, b.SessionID)
})
return out
}
func (s *memorySessionStore) Delete(_ context.Context, sessionID string) error {
s.mu.Lock()
defer s.mu.Unlock()
delete(s.sessions, sessionID)
delete(s.messages, sessionID)
for id, turn := range s.turns {
if turn.SessionID == sessionID {
delete(s.turns, id)
}
}
return nil
}
func (s *memorySessionStore) Touch(_ context.Context, sessionID string) error {
s.mu.Lock()
defer s.mu.Unlock()
if sc, ok := s.sessions[sessionID]; ok {
sc.LastActiveAt = truncateMilli(time.Now().UTC())
s.sessions[sessionID] = sc
}
return nil
}
// ── Messages ────────────────────────────────────────────────────────────────
func (s *memorySessionStore) SaveMessage(_ context.Context, sessionID string, msg messaging.Message) error {
s.mu.Lock()
defer s.mu.Unlock()
s.saveMessage(sessionID, msg, true)
return nil
}
func (s *memorySessionStore) SaveMessages(_ context.Context, sessionID string, msgs []messaging.Message) error {
s.mu.Lock()
defer s.mu.Unlock()
for _, msg := range msgs {
s.saveMessage(sessionID, msg, true)
}
return nil
}
func (s *memorySessionStore) indexOf(sessionID, id string) int {
return slices.IndexFunc(s.messages[sessionID], func(m memoryMessage) bool { return string(m.msg.ID) == id })
}
// saveMessage stores msg and returns the canonical ID it lives under. clamp
// selects the append path, whose timestamp must exceed the session's newest.
func (s *memorySessionStore) saveMessage(sessionID string, msg messaging.Message, clamp bool) string {
id := string(msg.ID)
legacyID := ""
if msg.SourceID == "" {
if id == "" {
id = newMessageID(sessionID, "")
}
} else {
currentID, compatibleLegacyID := canonicalMessageIDs(sessionID, msg.SourceID)
if id == "" {
id = currentID
}
if id == currentID {
legacyID = compatibleLegacyID
}
}
if legacyID != "" && s.indexOf(sessionID, legacyID) >= 0 {
return legacyID
}
if s.indexOf(sessionID, id) >= 0 {
return id
}
role := msg.Role
if role == "" {
role = messaging.RoleUser
}
ts := stamp(msg).UnixMilli()
history := s.messages[sessionID]
if clamp {
for _, m := range history {
if m.ts >= ts {
ts = m.ts + 1
}
}
}
s.seq++
stored := messaging.Message{
ID: messaging.MessageID(id), SourceID: msg.SourceID, Sender: msg.Sender,
SenderID: msg.SenderID, Role: role, Text: msg.Text,
}
history = append(history, memoryMessage{seq: s.seq, ts: ts, msg: stored})
slices.SortStableFunc(history, func(a, b memoryMessage) int {
if a.ts != b.ts {
return int(a.ts - b.ts)
}
return int(a.seq - b.seq)
})
s.messages[sessionID] = history
return id
}
func (s *memorySessionStore) ReplaceMessages(_ context.Context, sessionID string, msgs []messaging.Message, superseded []string) error {
s.mu.Lock()
defer s.mu.Unlock()
survivors := make(map[string]struct{}, len(msgs))
for _, msg := range msgs {
survivors[s.saveMessage(sessionID, msg, false)] = struct{}{}
}
doomed := make(map[string]struct{}, len(superseded))
for _, id := range superseded {
if _, kept := survivors[id]; !kept {
doomed[id] = struct{}{}
}
}
s.messages[sessionID] = slices.DeleteFunc(s.messages[sessionID], func(m memoryMessage) bool {
_, drop := doomed[string(m.msg.ID)]
return drop
})
return nil
}
// read returns the stored message with its session's conversation address.
func (s *memorySessionStore) read(sessionID string, m memoryMessage) messaging.Message {
out := m.msg
out.At = time.UnixMilli(m.ts).UTC()
if sc, ok := s.sessions[sessionID]; ok {
out.ConversationID = messaging.ConversationID{ChannelID: sc.Source.ChannelID, ThreadID: sc.Source.ThreadID}
}
return out
}
func (s *memorySessionStore) RecentMessages(_ context.Context, sessionID string, n int) ([]messaging.Message, error) {
if n <= 0 {
return nil, nil
}
s.mu.Lock()
defer s.mu.Unlock()
history := s.messages[sessionID]
history = history[max(len(history)-n, 0):]
out := make([]messaging.Message, 0, len(history))
for _, m := range history {
out = append(out, s.read(sessionID, m))
}
return out, nil
}
func (s *memorySessionStore) DeleteRecentMessages(_ context.Context, sessionID string, n int) (int, error) {
if n <= 0 {
return 0, nil
}
s.mu.Lock()
defer s.mu.Unlock()
history := s.messages[sessionID]
deleted := min(n, len(history))
s.messages[sessionID] = history[:len(history)-deleted]
return deleted, nil
}
func (s *memorySessionStore) MessageCount(_ context.Context, sessionID string) (int, error) {
s.mu.Lock()
defer s.mu.Unlock()
return len(s.messages[sessionID]), nil
}
func (s *memorySessionStore) FindPriorReply(_ context.Context, sessionID, sourceID, identity string) (string, error) {
if sourceID == "" {
return "", nil
}
s.mu.Lock()
defer s.mu.Unlock()
currentID, legacyID := canonicalMessageIDs(sessionID, sourceID)
i := -1
if legacyID != "" {
i = s.indexOf(sessionID, legacyID)
}
if i < 0 {
i = s.indexOf(sessionID, currentID)
}
history := s.messages[sessionID]
if i < 0 || i+1 >= len(history) {
return "", nil
}
next := history[i+1].msg
if next.Sender != identity || isCompressionSummary(next.Text) {
return "", nil
}
return next.Text, nil
}
// searchTerms splits text into lowercase letter-and-digit words.
func searchTerms(text string) []string {
return strings.FieldsFunc(strings.ToLower(text), func(r rune) bool {
return !unicode.IsLetter(r) && !unicode.IsDigit(r)
})
}
func (s *memorySessionStore) SearchMessages(_ context.Context, sessionID string, q MessageQuery) (MessagePage, error) {
query := strings.TrimSpace(q.Query)
if query == "" {
return MessagePage{}, nil
}
if len(query) > MaxMessageSearchQueryBytes {
return MessagePage{}, fmt.Errorf("sessionstore: search query exceeds %d bytes", MaxMessageSearchQueryBytes)
}
if terms := len(strings.Fields(query)); terms > MaxMessageSearchQueryTerms {
return MessagePage{}, fmt.Errorf("sessionstore: search query exceeds %d terms", MaxMessageSearchQueryTerms)
}
terms := searchTerms(query)
if len(terms) == 0 {
return MessagePage{}, nil
}
limit := q.Limit
if limit <= 0 {
limit = DefaultMessagePageSize
}
limit = min(limit, MaxMessagePageSize)
offset := max(q.Offset, 0)
s.mu.Lock()
defer s.mu.Unlock()
var matches []messaging.Message
history := s.messages[sessionID]
for _, m := range slices.Backward(history) {
words := searchTerms(m.msg.Sender + " " + m.msg.Text)
if !allIn(terms, words) {
continue
}
matches = append(matches, s.read(sessionID, m))
}
if offset >= len(matches) {
return MessagePage{}, nil
}
page := matches[offset:min(offset+limit, len(matches))]
next := offset + len(page)
return MessagePage{Messages: page, NextOffset: next, HasMore: next < len(matches)}, nil
}
func allIn(terms, words []string) bool {
for _, term := range terms {
if !slices.Contains(words, term) {
return false
}
}
return true
}
// ── Turns ───────────────────────────────────────────────────────────────────
// storedTurn normalises a turn the way a database round trip does.
func storedTurn(turn TurnRecord) (TurnRecord, error) {
raw, err := marshalToolCalls(turn.ToolCalls)
if err != nil {
return TurnRecord{}, err
}
if turn.ToolCalls, err = unmarshalToolCalls(raw); err != nil {
return TurnRecord{}, err
}
turn.CreatedAt = truncateMilli(turn.CreatedAt)
turn.UpdatedAt = truncateMilli(turn.UpdatedAt)
return turn, nil
}
func (s *memorySessionStore) ClaimTurn(_ context.Context, initial TurnRecord) (TurnRecord, TurnClaim, error) {
initial, _, err := prepareInitialTurn(initial, time.Now().UTC())
if err != nil {
return TurnRecord{}, "", err
}
if initial, err = storedTurn(initial); err != nil {
return TurnRecord{}, "", err
}
s.mu.Lock()
defer s.mu.Unlock()
turn, exists := s.turns[initial.TurnID]
if !exists {
s.turns[initial.TurnID] = initial
return initial, TurnClaimOwned, nil
}
switch turn.Status {
case TurnStatusCompleted:
return turn, TurnClaimCompleted, nil
case TurnStatusFailed, TurnStatusCancelled:
if turn.AssistantMessageID != "" {
turn.Status = TurnStatusCompleted
turn.Error = ""
turn.UpdatedAt = truncateMilli(time.Now().UTC())
s.turns[turn.TurnID] = turn
return turn, TurnClaimCompleted, nil
}
turn.Status = TurnStatusRunning
turn.Attempt++
turn.OwnerID = initial.OwnerID
turn.Error = ""
turn.UpdatedAt = truncateMilli(time.Now().UTC())
s.turns[turn.TurnID] = turn
return turn, TurnClaimOwned, nil
default:
return turn, TurnClaimInProgress, nil
}
}
func recoverable(turn TurnRecord) bool {
switch turn.Status {
case TurnStatusAccepted, TurnStatusRunning, TurnStatusPartial:
return true
default:
return false
}
}
func (s *memorySessionStore) RecoverTurns(_ context.Context, ownerID string) error {
if ownerID == "" {
return fmt.Errorf("sessionstore: recovery owner ID is required")
}
s.mu.Lock()
defer s.mu.Unlock()
now := truncateMilli(time.Now().UTC())
for id, turn := range s.turns {
if recoverable(turn) && turn.OwnerID != ownerID {
turn.Status = TurnStatusFailed
turn.Error = "recovered after process restart"
turn.UpdatedAt = now
s.turns[id] = turn
}
}
return nil
}
func (s *memorySessionStore) GetTurn(_ context.Context, turnID string) (TurnRecord, bool, error) {
s.mu.Lock()
defer s.mu.Unlock()
turn, ok := s.turns[turnID]
return turn, ok, nil
}
func (s *memorySessionStore) RecentTurns(_ context.Context, sessionID string, n int) ([]TurnRecord, error) {
if n <= 0 {
return []TurnRecord{}, nil
}
n = min(n, 500)
s.mu.Lock()
defer s.mu.Unlock()
var out []TurnRecord
for _, turn := range s.turns {
if turn.SessionID == sessionID {
out = append(out, turn)
}
}
slices.SortFunc(out, func(a, b TurnRecord) int { return a.CreatedAt.Compare(b.CreatedAt) })
return append([]TurnRecord{}, out[max(len(out)-n, 0):]...), nil
}
func (s *memorySessionStore) SaveTurn(_ context.Context, turn TurnRecord) error {
if turn.TurnID == "" {
return fmt.Errorf("sessionstore: turn ID is required")
}
s.mu.Lock()
defer s.mu.Unlock()
current, ok := s.turns[turn.TurnID]
if !ok {
return fmt.Errorf("sessionstore: save turn: turn not found")
}
if current.Attempt != turn.Attempt || current.OwnerID != turn.OwnerID {
return fmt.Errorf("%w: %s", ErrTurnOwnershipLost, turn.TurnID)
}
if turn.UpdatedAt.IsZero() {
turn.UpdatedAt = time.Now().UTC()
}
stored, err := storedTurn(turn)
if err != nil {
return err
}
s.turns[turn.TurnID] = stored
return nil
}
func (s *memorySessionStore) ListRecoverableTurns(_ context.Context) ([]TurnRecord, error) {
s.mu.Lock()
defer s.mu.Unlock()
var out []TurnRecord
for _, turn := range s.turns {
if recoverable(turn) {
out = append(out, turn)
}
}
slices.SortFunc(out, func(a, b TurnRecord) int { return a.UpdatedAt.Compare(b.UpdatedAt) })
return out, nil
}
@@ -23,7 +23,14 @@ func TestMain(m *testing.M) {
// applied and registers pool cleanup.
func newPostgresPool(t *testing.T) *pgxpool.Pool {
t.Helper()
pool, err := postgres.Open(t.Context(), pgtest.URL(t))
return openPostgresPoolAt(t, pgtest.URL(t))
}
// openPostgresPoolAt opens a migrated pool on url, so a test can reopen the
// same database through a second pool.
func openPostgresPoolAt(t *testing.T, url string) *pgxpool.Pool {
t.Helper()
pool, err := postgres.Open(t.Context(), url)
if err != nil {
t.Fatalf("postgres.Open: %v", err)
}
@@ -42,6 +49,18 @@ func newPostgresStore(t *testing.T) SessionStore {
return NewPostgresSessionStore(newPostgresPool(t))
}
// openPostgresStoreAt returns a SessionStore over its own pool on url.
func openPostgresStoreAt(t *testing.T, url string) SessionStore {
t.Helper()
return NewPostgresSessionStore(openPostgresPoolAt(t, url))
}
// TestSessionStoreConformanceMemory runs the shared SessionStore contract
// against the in-memory implementation.
func TestSessionStoreConformanceMemory(t *testing.T) {
runSessionStoreSuite(t, func(*testing.T) SessionStore { return NewSessionStoreMemory() })
}
// TestSessionStoreConformancePostgres runs the shared SessionStore contract
// against the PostgreSQL implementation.
func TestSessionStoreConformancePostgres(t *testing.T) {
File diff suppressed because it is too large. Load diff
@@ -1,102 +0,0 @@
package gateway
import (
"context"
"fmt"
"path/filepath"
"sync"
"testing"
"time"
)
// TestClaimTurnConcurrentInsertRaceResolvesToOneOwner verifies that ordinary
// contention between processes sharing the session database is resolved by
// ClaimTurn itself. Callers must not need to recognize and retry SQLite's
// SQLITE_BUSY or SQLITE_BUSY_SNAPSHOT implementation details.
//
// Two invariants must hold under contention: every ClaimTurn call returns
// without error, and EXACTLY ONE of the racers ends up TurnClaimOwned while
// the other observes the committed row and returns a non-owning claim.
// The second invariant matters more than the first: a bug that returns
// TurnClaimOwned to both racers means two archied processes drive the same
// turn to completion, and the assistant reply gets emitted twice to the
// chat. The first invariant alone would not catch that.
//
// A single sqliteSessionStore instance cannot exhibit any of this: its own
// mutex serializes every ClaimTurn call against that instance's connection,
// so this uses two separate store instances (two connections, two mutexes)
// against the same file-backed database -- the real-world shape of the
// scenario (two archied/worker processes or connections sharing one session
// DB).
func TestClaimTurnConcurrentInsertRaceResolvesToOneOwner(t *testing.T) {
path := filepath.Join(t.TempDir(), "sessions.db")
const stores = 2
const trials = 100
openers := make([]TurnLedger, stores)
for i := range openers {
store, err := OpenSQLiteSessionStore(path)
if err != nil {
t.Fatalf("OpenSQLiteSessionStore[%d]: %v", i, err)
}
closer, ok := store.(interface{ Close() error })
if !ok {
t.Fatalf("store[%d] is %T, want a Close() error", i, store)
}
t.Cleanup(func() { _ = closer.Close() })
ledger, ok := store.(TurnLedger)
if !ok {
t.Fatalf("store[%d] is %T, want TurnLedger", i, store)
}
openers[i] = ledger
}
ctx := context.Background()
now := time.UnixMilli(1000).UTC()
// Each trial races the two independent stores over one turn ID, then waits
// before starting the next trial. This isolates the same-turn claim race
// from unrelated 100-way global writer contention.
for trial := range trials {
turnID := fmt.Sprintf("race-turn-%d", trial)
initial := TurnRecord{
TurnID: turnID,
SessionID: "race-session",
SourceID: turnID,
Status: TurnStatusAccepted,
CreatedAt: now,
UpdatedAt: now,
}
errs := make([]error, stores)
claims := make([]TurnClaim, stores)
var wg sync.WaitGroup
var start sync.WaitGroup
start.Add(1)
for i := range stores {
wg.Add(1)
go func(i int) {
defer wg.Done()
start.Wait()
_, claims[i], errs[i] = openers[i].ClaimTurn(ctx, initial)
}(i)
}
start.Done()
wg.Wait()
for i, err := range errs {
if err != nil {
t.Fatalf("trial %d, store %d: ClaimTurn() unexpected error = %v", trial, i, err)
}
}
owners := 0
for _, c := range claims {
if c == TurnClaimOwned {
owners++
}
}
if owners != 1 {
t.Fatalf("trial %d: expected exactly one TurnClaimOwned across %d racers, got %d (claims=%v)", trial, stores, owners, claims)
}
}
}
@@ -1,15 +0,0 @@
package gateway
import "testing"
// TestSessionStoreConformanceSQLite runs the shared SessionStore contract
// against the SQLite implementation.
func TestSessionStoreConformanceSQLite(t *testing.T) {
runSessionStoreSuite(t, func(t *testing.T) SessionStore {
s, err := NewSQLiteSessionStoreMemory()
if err != nil {
t.Fatalf("NewSQLiteSessionStoreMemory: %v", err)
}
return s
})
}
File diff suppressed because it is too large. Load diff
+42
View File
@@ -2,8 +2,11 @@ package gateway
import (
"context"
"encoding/json"
"errors"
"fmt"
"strings"
"time"
"github.com/google/uuid"
)
@@ -72,3 +75,42 @@ func CanonicalTurnID(sessionID, sourceID string) string {
func legacyCanonicalTurnID(sessionID, sourceID string) string {
return uuid.NewSHA1(uuid.NameSpaceURL, []byte("archie-turn:\x00"+sessionID+"\x00"+sourceID)).String()
}
func prepareInitialTurn(initial TurnRecord, now time.Time) (TurnRecord, string, error) {
if initial.TurnID == "" {
return TurnRecord{}, "", fmt.Errorf("sessionstore: turn ID is required")
}
initial.Status = TurnStatusRunning
initial.Attempt = 1
if initial.CreatedAt.IsZero() {
initial.CreatedAt = now
}
initial.UpdatedAt = now
toolCalls, err := marshalToolCalls(initial.ToolCalls)
return initial, toolCalls, err
}
// marshalToolCalls encodes tool activity for storage; an empty slice becomes
// "[]" so it round-trips through unmarshalToolCalls to nil.
func marshalToolCalls(events []ToolCallEvent) (string, error) {
if len(events) == 0 {
return "[]", nil
}
encoded, err := json.Marshal(events)
if err != nil {
return "", fmt.Errorf("sessionstore: marshal tool calls: %w", err)
}
return string(encoded), nil
}
// unmarshalToolCalls decodes tool activity persisted by marshalToolCalls.
func unmarshalToolCalls(raw string) ([]ToolCallEvent, error) {
if raw == "" || raw == "[]" {
return nil, nil
}
var events []ToolCallEvent
if err := json.Unmarshal([]byte(raw), &events); err != nil {
return nil, fmt.Errorf("sessionstore: unmarshal tool calls: %w", err)
}
return events, nil
}
+34 -50
View File
@@ -3,9 +3,10 @@ package gateway
import (
"context"
"errors"
"path/filepath"
"testing"
"time"
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgtest"
)
func TestTurnLedgerClaimLifecycleIsIdempotent(t *testing.T) {
@@ -14,17 +15,17 @@ func TestTurnLedgerClaimLifecycleIsIdempotent(t *testing.T) {
open func(t *testing.T) (TurnLedger, func())
}{
{
name: "sqlite memory",
name: "memory",
open: func(t *testing.T) (TurnLedger, func()) {
store, err := NewSQLiteSessionStoreMemory()
if err != nil {
t.Fatal(err)
}
ledger, ok := store.(TurnLedger)
if !ok {
t.Fatalf("store is %T, want TurnLedger", store)
}
return ledger, func() { _ = store.Close() }
store := NewSessionStoreMemory()
return store.(TurnLedger), func() { _ = store.Close() }
},
},
{
name: "postgres",
open: func(t *testing.T) (TurnLedger, func()) {
store := newPostgresStore(t)
return store.(TurnLedger), func() { _ = store.Close() }
},
},
}
@@ -109,17 +110,17 @@ func TestTurnLedgerReclaimsAcrossProcessOwnersAndRejectsStaleWrites(t *testing.T
open func(t *testing.T) (TurnLedger, func())
}{
{
name: "sqlite memory",
name: "memory",
open: func(t *testing.T) (TurnLedger, func()) {
store, err := NewSQLiteSessionStoreMemory()
if err != nil {
t.Fatal(err)
}
ledger, ok := store.(TurnLedger)
if !ok {
t.Fatalf("store is %T, want TurnLedger", store)
}
return ledger, func() { _ = store.Close() }
store := NewSessionStoreMemory()
return store.(TurnLedger), func() { _ = store.Close() }
},
},
{
name: "postgres",
open: func(t *testing.T) (TurnLedger, func()) {
store := newPostgresStore(t)
return store.(TurnLedger), func() { _ = store.Close() }
},
},
}
@@ -171,15 +172,10 @@ func TestSessionDeleteRemovesTurns(t *testing.T) {
open func(t *testing.T) SessionStore
}{
{
name: "sqlite memory",
open: func(t *testing.T) SessionStore {
store, err := NewSQLiteSessionStoreMemory()
if err != nil {
t.Fatal(err)
}
return store
},
name: "memory",
open: func(*testing.T) SessionStore { return NewSessionStoreMemory() },
},
{name: "postgres", open: newPostgresStore},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
@@ -208,12 +204,9 @@ func TestSessionDeleteRemovesTurns(t *testing.T) {
}
func TestTurnLedgerPersistsToolCallsAcrossClaimAndReopen(t *testing.T) {
path := filepath.Join(t.TempDir(), "sessions.db")
url := pgtest.URL(t)
ctx := context.Background()
store, err := OpenSQLiteSessionStore(path)
if err != nil {
t.Fatalf("OpenSQLiteSessionStore() error = %v", err)
}
store := openPostgresStoreAt(t, url)
ledger, ok := store.(TurnLedger)
if !ok {
t.Fatalf("store is %T, want TurnLedger", store)
@@ -251,10 +244,7 @@ func TestTurnLedgerPersistsToolCallsAcrossClaimAndReopen(t *testing.T) {
if err := store.Close(); err != nil {
t.Fatalf("Close() error = %v", err)
}
reopened, err := OpenSQLiteSessionStore(path)
if err != nil {
t.Fatalf("reopen error = %v", err)
}
reopened := openPostgresStoreAt(t, url)
defer func() { _ = reopened.Close() }()
reopenedLedger, ok := reopened.(TurnLedger)
if !ok {
@@ -269,13 +259,10 @@ func TestTurnLedgerPersistsToolCallsAcrossClaimAndReopen(t *testing.T) {
}
}
func TestSQLiteTurnLedgerSurvivesReopen(t *testing.T) {
path := filepath.Join(t.TempDir(), "sessions.db")
func TestPostgresTurnLedgerSurvivesReopen(t *testing.T) {
url := pgtest.URL(t)
ctx := context.Background()
store, err := OpenSQLiteSessionStore(path)
if err != nil {
t.Fatalf("OpenSQLiteSessionStore() error = %v", err)
}
store := openPostgresStoreAt(t, url)
ledger, ok := store.(TurnLedger)
if !ok {
t.Fatalf("store is %T, want TurnLedger", store)
@@ -300,10 +287,7 @@ func TestSQLiteTurnLedgerSurvivesReopen(t *testing.T) {
t.Fatalf("Close() error = %v", err)
}
reopened, err := OpenSQLiteSessionStore(path)
if err != nil {
t.Fatalf("reopen error = %v", err)
}
reopened := openPostgresStoreAt(t, url)
defer func() { _ = reopened.Close() }()
reopenedLedger, ok := reopened.(TurnLedger)
if !ok {
@@ -347,7 +331,7 @@ func TestCanonicalTurnIDComponentsAreInjectivelyEncoded(t *testing.T) {
func TestTurnRunnerCanonicalTurnIDPreservesMatchingLegacyRecord(t *testing.T) {
t.Parallel()
store := newTestSQLiteStore(t)
store := NewSessionStoreMemory()
ledger, ok := store.(TurnLedger)
if !ok {
t.Fatalf("store is %T, want TurnLedger", store)
@@ -378,7 +362,7 @@ func TestTurnRunnerCanonicalTurnIDPreservesMatchingLegacyRecord(t *testing.T) {
func TestTurnRunnerCanonicalTurnIDRejectsCollidingLegacyRecord(t *testing.T) {
t.Parallel()
store := newTestSQLiteStore(t)
store := NewSessionStoreMemory()
ledger, ok := store.(TurnLedger)
if !ok {
t.Fatalf("store is %T, want TurnLedger", store)