Move sessions, messages, turns and full-text search off the per-process SQLite file onto Postgres behind the existing messaging.SessionStore contract. New adapter internal/gateway/session_store_postgres.go, sqlc queries/gateway.sql, migration 0003_gateway.sql, regenerated postgresdb. FTS5 becomes a STORED generated tsvector column over (sender, text) plus GIN. plainto_tsquery is the tsvector analog of ftsMatchQuery's quote-escaping: query text is parsed as plain text, never query syntax, and the surviving terms are ANDed. MessagePage.Truncated stays always-false on Postgres too: the result set is counted exactly. On internal/domain/messaging/session_store.go: this is a comment-only edit to the MessagePage.Truncated doc comment (the brief's own "change the comment" instruction). It changes no type, method or value, so L2 and the import lane still build against an unchanged contract and the wire is unaffected. No interface widened or altered. Semantics preserved verbatim: - The timestamp clamp is SQL-atomic (INSERT ... SELECT MAX(ts)+1). Appends to one session are serialised by a transaction-scoped advisory lock keyed on the session so two concurrent appends cannot read the same MAX(ts); a Go clamp would race (proven by the new cross-handle monotonicity test). - Session ordering is GREATEST(last_active_at, created_at) with a matching expression index. - ClaimTurn is insert-first (ON CONFLICT DO NOTHING), with a race test across two handles. State outside the store is NOT made shared by this change, and none of it is silently improved or worsened: - sessionTracker.active, Router.titling and per-session persona selection are in-memory, per-process caches; a second Gateway still diverges from the first. - messaging.Turns lanes and the per-process recovery latch stay per-process. - RecoverTurns still fails every turn owned by another process, so a second Gateway starting would still fail the first's in-flight turns. This lane does NOT make that safe and does not try to: the safeguard is D4.1's single-Gateway advisory serve-ownership lock (not implemented here), which prevents a second Gateway from serving at all. The turn-recovery semantics are left exactly as they were. Production openers still return SQLite; this lane adds the adapter and tests only. The cutover and the state-sharing questions remain follow-up work.
192 lines
5.6 KiB
Go
192 lines
5.6 KiB
Go
package gateway
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"os"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
|
|
"github.com/samcharles93/archie-core/internal/domain/messaging"
|
|
"github.com/samcharles93/archie-core/internal/infrastructure/postgres"
|
|
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgtest"
|
|
)
|
|
|
|
func TestMain(m *testing.M) {
|
|
os.Exit(pgtest.Main(m))
|
|
}
|
|
|
|
// newPostgresPool opens a pool to a fresh database with the full schema
|
|
// applied and registers pool cleanup.
|
|
func newPostgresPool(t *testing.T) *pgxpool.Pool {
|
|
t.Helper()
|
|
pool, err := postgres.Open(t.Context(), pgtest.URL(t))
|
|
if err != nil {
|
|
t.Fatalf("postgres.Open: %v", err)
|
|
}
|
|
if err := postgres.Migrate(t.Context(), pool, postgres.Migrations()); err != nil {
|
|
pool.Close()
|
|
t.Fatalf("postgres.Migrate: %v", err)
|
|
}
|
|
t.Cleanup(pool.Close)
|
|
return pool
|
|
}
|
|
|
|
// newPostgresStore returns a fresh PostgreSQL SessionStore with the gateway
|
|
// schema applied.
|
|
func newPostgresStore(t *testing.T) SessionStore {
|
|
t.Helper()
|
|
return NewPostgresSessionStore(newPostgresPool(t))
|
|
}
|
|
|
|
// TestSessionStoreConformancePostgres runs the shared SessionStore contract
|
|
// against the PostgreSQL implementation.
|
|
func TestSessionStoreConformancePostgres(t *testing.T) {
|
|
runSessionStoreSuite(t, newPostgresStore)
|
|
}
|
|
|
|
// TestPostgresMessageTimestampsIncreaseAcrossStoreHandles pins the clamp is
|
|
// SQL-atomic, not mutex-derived: two independent stores (two connections)
|
|
// appending concurrently to one session must produce strictly increasing
|
|
// timestamps. The per-session advisory lock serialises the appends so the
|
|
// INSERT ... SELECT MAX(ts) clamp sees the prior committed append.
|
|
func TestPostgresMessageTimestampsIncreaseAcrossStoreHandles(t *testing.T) {
|
|
pool := newPostgresPool(t)
|
|
stores := make([]SessionStore, 2)
|
|
for i := range stores {
|
|
stores[i] = NewPostgresSessionStore(pool)
|
|
}
|
|
|
|
const perStore = 100
|
|
wantTime := time.Date(2026, 8, 3, 12, 0, 0, 0, time.UTC)
|
|
var wg sync.WaitGroup
|
|
for storeIndex, st := range stores {
|
|
for messageIndex := range perStore {
|
|
wg.Go(func() {
|
|
err := st.SaveMessage(t.Context(), "sess", messaging.Message{
|
|
ID: messaging.MessageID(fmt.Sprintf("%d-%d", storeIndex, messageIndex)),
|
|
Sender: "user", Role: messaging.RoleUser,
|
|
Text: "concurrent",
|
|
At: wantTime,
|
|
})
|
|
if err != nil {
|
|
t.Errorf("SaveMessage: %v", err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
wg.Wait()
|
|
|
|
messages, err := stores[0].RecentMessages(t.Context(), "sess", perStore*len(stores))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(messages) != perStore*len(stores) {
|
|
t.Fatalf("messages = %d, want %d", len(messages), perStore*len(stores))
|
|
}
|
|
for i := 1; i < len(messages); i++ {
|
|
if !messages[i].At.After(messages[i-1].At) {
|
|
t.Fatalf("timestamp %d (%s) is not after timestamp %d (%s)", i, messages[i].At, i-1, messages[i-1].At)
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestPostgresClaimTurnConcurrentInsertRaceResolvesToOneOwner pins the
|
|
// insert-first claim: racing two independent stores over one turn ID must
|
|
// yield exactly one TurnClaimOwned, never two.
|
|
func TestPostgresClaimTurnConcurrentInsertRaceResolvesToOneOwner(t *testing.T) {
|
|
pool := newPostgresPool(t)
|
|
|
|
const stores = 2
|
|
const trials = 100
|
|
|
|
ledgers := make([]TurnLedger, stores)
|
|
for i := range ledgers {
|
|
store, ok := NewPostgresSessionStore(pool).(TurnLedger)
|
|
if !ok {
|
|
t.Fatalf("store[%d] is %T, want TurnLedger", i, store)
|
|
}
|
|
ledgers[i] = store
|
|
}
|
|
|
|
ctx := context.Background()
|
|
now := time.UnixMilli(1000).UTC()
|
|
|
|
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] = ledgers[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)
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestPostgresSessionStoreRecentTurnsReturnsReplayableToolHistory pins the
|
|
// TurnLedger/TurnHistory round-trip: a completed turn reads back with its
|
|
// tool calls and response text intact.
|
|
func TestPostgresSessionStoreRecentTurnsReturnsReplayableToolHistory(t *testing.T) {
|
|
st := newPostgresStore(t)
|
|
ledger, ok := st.(TurnLedger)
|
|
if !ok {
|
|
t.Fatalf("store is %T, want TurnLedger", st)
|
|
}
|
|
turn, claim, err := ledger.ClaimTurn(t.Context(), TurnRecord{
|
|
TurnID: "turn-history", SessionID: "session-history", SourceID: "source-history", OwnerID: "owner",
|
|
ToolCalls: []ToolCallEvent{{Name: "shell", Output: "exit 0"}},
|
|
})
|
|
if err != nil || claim != TurnClaimOwned {
|
|
t.Fatalf("ClaimTurn() = %#v, %v, %v", turn, claim, err)
|
|
}
|
|
turn.Status = TurnStatusCompleted
|
|
turn.ResponseText = "answer"
|
|
turn.UpdatedAt = time.Now().UTC()
|
|
if err := ledger.SaveTurn(t.Context(), turn); err != nil {
|
|
t.Fatalf("SaveTurn() error = %v", err)
|
|
}
|
|
history, ok := st.(TurnHistory)
|
|
if !ok {
|
|
t.Fatalf("store is %T, want TurnHistory", st)
|
|
}
|
|
got, err := history.RecentTurns(t.Context(), "session-history", 10)
|
|
if err != nil || len(got) != 1 || len(got[0].ToolCalls) != 1 || got[0].ResponseText != "answer" {
|
|
t.Fatalf("RecentTurns() = %#v, %v; want one replayable turn", got, err)
|
|
}
|
|
}
|