Merge branch 'run/pg-w2-5' into worktree-giggly-marinating-lamport
# Conflicts: # internal/infrastructure/postgres/postgresdb/models.go
This commit is contained in:
commit
ecc1997384
7 files changed
+2080
-2
No files matched your search
@@ -131,8 +131,9 @@ type MessagePage struct {
|
||||
// NextOffset is meaningful only when HasMore is true.
|
||||
NextOffset int
|
||||
HasMore bool
|
||||
// Truncated is retained in the transport contract and is always false for
|
||||
// SQLite because it can count the complete result set.
|
||||
// Truncated is retained in the transport contract and is always false:
|
||||
// both store implementations (SQLite FTS5 and Postgres tsvector) count the
|
||||
// complete result set, so no page is ever reported as truncated.
|
||||
Truncated bool
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,743 @@
|
||||
package gateway
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/messaging"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/postgresdb"
|
||||
)
|
||||
|
||||
// ── PostgreSQL implementation ───────────────────────────────────────────────
|
||||
//
|
||||
// postgresSessionStore is the PostgreSQL-backed SessionStore. It shares the
|
||||
// SQLite store's wire contract (messaging.SessionStore plus the optional
|
||||
// TurnLedger, TurnHistory and TurnReplayStore capability interfaces) and its
|
||||
// on-disk representation: timestamps remain bigint milliseconds so the
|
||||
// monotonic clamp (MAX(ts) + 1) keeps the same unit, and full-text search
|
||||
// moves from FTS5 to a tsvector + GIN generated column over (sender, text).
|
||||
//
|
||||
// Unlike the SQLite store it carries no in-process mutex. Two semantics make
|
||||
// that safe:
|
||||
// - The strictly-increasing message timestamp is assigned inside
|
||||
// InsertMessageClamped, and appends to one session are serialised by a
|
||||
// transaction-scoped advisory lock (LockSessionMessages), so two
|
||||
// concurrent appends cannot both read the same MAX(ts).
|
||||
// - ClaimTurn is insert-first: InsertTurn's ON CONFLICT DO NOTHING makes the
|
||||
// claim winner/loser decision atomically in the database.
|
||||
type postgresSessionStore struct {
|
||||
pool *pgxpool.Pool
|
||||
}
|
||||
|
||||
// NewPostgresSessionStore returns a SessionStore backed by pool. The pool must
|
||||
// already have the gateway schema applied (postgres.Migrations). The returned
|
||||
// store owns the pool: Close closes it.
|
||||
func NewPostgresSessionStore(pool *pgxpool.Pool) SessionStore {
|
||||
return &postgresSessionStore{pool: pool}
|
||||
}
|
||||
|
||||
func (s *postgresSessionStore) Close() error {
|
||||
s.pool.Close()
|
||||
return nil
|
||||
}
|
||||
|
||||
// ── Sessions ────────────────────────────────────────────────────────────────
|
||||
|
||||
func (s *postgresSessionStore) Save(ctx context.Context, sc SessionContext) error {
|
||||
q := postgresdb.New(s.pool)
|
||||
err := q.SaveSession(ctx, postgresdb.SaveSessionParams{
|
||||
SessionID: sc.SessionID,
|
||||
Platform: sc.Source.Platform,
|
||||
BotUser: sc.Source.BotUser,
|
||||
ChannelID: sc.Source.ChannelID,
|
||||
ThreadID: sc.Source.ThreadID,
|
||||
Title: sc.Title,
|
||||
ParentSessionID: sc.ParentSessionID,
|
||||
BranchName: sc.BranchName,
|
||||
CreatedAt: sc.CreatedAt.UnixMilli(),
|
||||
LastActiveAt: sc.LastActiveAt.UnixMilli(),
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("sessionstore: save: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *postgresSessionStore) Get(ctx context.Context, sessionID string) (*SessionContext, error) {
|
||||
q := postgresdb.New(s.pool)
|
||||
row, err := q.SessionByID(ctx, sessionID)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return nil, nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("sessionstore: get: %w", err)
|
||||
}
|
||||
sc := sessionFromRow(row)
|
||||
return &sc, nil
|
||||
}
|
||||
|
||||
func (s *postgresSessionStore) GetByChannel(ctx context.Context, platform, channelID string) ([]SessionContext, error) {
|
||||
q := postgresdb.New(s.pool)
|
||||
rows, err := q.SessionsByChannel(ctx, postgresdb.SessionsByChannelParams{
|
||||
Platform: platform, ChannelID: channelID,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("sessionstore: get by channel: %w", err)
|
||||
}
|
||||
return sessionsFromRows(rows), nil
|
||||
}
|
||||
|
||||
func (s *postgresSessionStore) Delete(ctx context.Context, sessionID string) error {
|
||||
tx, err := s.pool.Begin(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("sessionstore: delete: begin: %w", err)
|
||||
}
|
||||
defer func() { _ = tx.Rollback(ctx) }()
|
||||
|
||||
q := postgresdb.New(tx)
|
||||
if err := q.DeleteSessionMessages(ctx, sessionID); err != nil {
|
||||
return fmt.Errorf("sessionstore: delete messages: %w", err)
|
||||
}
|
||||
if err := q.DeleteSessionTurns(ctx, sessionID); err != nil {
|
||||
return fmt.Errorf("sessionstore: delete turns: %w", err)
|
||||
}
|
||||
if err := q.DeleteSession(ctx, sessionID); err != nil {
|
||||
return fmt.Errorf("sessionstore: delete session: %w", err)
|
||||
}
|
||||
if err := tx.Commit(ctx); err != nil {
|
||||
return fmt.Errorf("sessionstore: delete: commit: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *postgresSessionStore) Touch(ctx context.Context, sessionID string) error {
|
||||
q := postgresdb.New(s.pool)
|
||||
err := q.TouchSession(ctx, postgresdb.TouchSessionParams{
|
||||
SessionID: sessionID, LastActiveAt: time.Now().UTC().UnixMilli(),
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("sessionstore: touch: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *postgresSessionStore) List(ctx context.Context) ([]SessionContext, error) {
|
||||
q := postgresdb.New(s.pool)
|
||||
rows, err := q.ListSessions(ctx)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("sessionstore: list: %w", err)
|
||||
}
|
||||
return sessionsFromRows(rows), nil
|
||||
}
|
||||
|
||||
func sessionFromRow(row postgresdb.Session) SessionContext {
|
||||
return SessionContext{
|
||||
SessionID: row.SessionID,
|
||||
Source: SessionSource{Platform: row.Platform, BotUser: row.BotUser, ChannelID: row.ChannelID, ThreadID: row.ThreadID},
|
||||
Title: row.Title,
|
||||
ParentSessionID: row.ParentSessionID,
|
||||
BranchName: row.BranchName,
|
||||
CreatedAt: time.UnixMilli(row.CreatedAt).UTC(),
|
||||
LastActiveAt: time.UnixMilli(row.LastActiveAt).UTC(),
|
||||
}
|
||||
}
|
||||
|
||||
func sessionsFromRows(rows []postgresdb.Session) []SessionContext {
|
||||
out := make([]SessionContext, 0, len(rows))
|
||||
for _, row := range rows {
|
||||
out = append(out, sessionFromRow(row))
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// ── Messages ────────────────────────────────────────────────────────────────
|
||||
|
||||
func (s *postgresSessionStore) SaveMessage(ctx context.Context, sessionID string, msg messaging.Message) error {
|
||||
tx, err := s.pool.Begin(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("sessionstore: save message: begin: %w", err)
|
||||
}
|
||||
defer func() { _ = tx.Rollback(ctx) }()
|
||||
|
||||
q := postgresdb.New(tx)
|
||||
if err := q.LockSessionMessages(ctx, sessionID); err != nil {
|
||||
return fmt.Errorf("sessionstore: lock session messages: %w", err)
|
||||
}
|
||||
if _, err := saveMessagePG(ctx, q, sessionID, msg, true); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := tx.Commit(ctx); err != nil {
|
||||
return fmt.Errorf("sessionstore: save message: commit: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *postgresSessionStore) SaveMessages(ctx context.Context, sessionID string, msgs []messaging.Message) error {
|
||||
tx, err := s.pool.Begin(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("sessionstore: save messages: begin: %w", err)
|
||||
}
|
||||
defer func() { _ = tx.Rollback(ctx) }()
|
||||
|
||||
q := postgresdb.New(tx)
|
||||
if err := q.LockSessionMessages(ctx, sessionID); err != nil {
|
||||
return fmt.Errorf("sessionstore: lock session messages: %w", err)
|
||||
}
|
||||
for _, msg := range msgs {
|
||||
if _, err := saveMessagePG(ctx, q, sessionID, msg, true); err != nil {
|
||||
return fmt.Errorf("sessionstore: save messages: %w", err)
|
||||
}
|
||||
}
|
||||
if err := tx.Commit(ctx); err != nil {
|
||||
return fmt.Errorf("sessionstore: save messages: commit: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// saveMessage persists one message through q and reports the canonical ID it
|
||||
// was stored under (or was already stored under, for a redelivered message).
|
||||
// The clamp parameter selects the append path's monotonic clamp, mirroring the
|
||||
// SQLite store's saveMessageAt.
|
||||
func saveMessagePG(ctx context.Context, q *postgresdb.Queries, sessionID string, msg messaging.Message, clamp bool) (string, error) {
|
||||
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
|
||||
}
|
||||
}
|
||||
role := string(msg.Role)
|
||||
if role == "" {
|
||||
role = string(messaging.RoleUser)
|
||||
}
|
||||
if legacyID != "" {
|
||||
existingID, err := q.MessageID(ctx, postgresdb.MessageIDParams{
|
||||
SessionID: sessionID, MessageID: legacyID,
|
||||
})
|
||||
switch {
|
||||
case err == nil:
|
||||
return existingID, nil
|
||||
case !errors.Is(err, pgx.ErrNoRows):
|
||||
return "", fmt.Errorf("sessionstore: read legacy message identity: %w", err)
|
||||
}
|
||||
}
|
||||
at := stamp(msg)
|
||||
if clamp {
|
||||
err := q.InsertMessageClamped(ctx, postgresdb.InsertMessageClampedParams{
|
||||
MessageID: id, SessionID: sessionID, SourceID: msg.SourceID,
|
||||
Sender: msg.Sender, SenderID: msg.SenderID, Role: role, Text: msg.Text,
|
||||
Ts: at.UnixMilli(),
|
||||
})
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("sessionstore: save message: %w", err)
|
||||
}
|
||||
return id, nil
|
||||
}
|
||||
|
||||
existingID, err := q.MessageID(ctx, postgresdb.MessageIDParams{
|
||||
SessionID: sessionID, MessageID: id,
|
||||
})
|
||||
switch {
|
||||
case err == nil:
|
||||
return existingID, nil
|
||||
case !errors.Is(err, pgx.ErrNoRows):
|
||||
return "", fmt.Errorf("sessionstore: read existing message: %w", err)
|
||||
}
|
||||
|
||||
err = q.InsertMessageAt(ctx, postgresdb.InsertMessageAtParams{
|
||||
MessageID: id, SessionID: sessionID, SourceID: msg.SourceID,
|
||||
Sender: msg.Sender, SenderID: msg.SenderID, Role: role, Text: msg.Text,
|
||||
Ts: at.UnixMilli(),
|
||||
})
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("sessionstore: save message: %w", err)
|
||||
}
|
||||
return id, nil
|
||||
}
|
||||
|
||||
func (s *postgresSessionStore) ReplaceMessages(
|
||||
ctx context.Context,
|
||||
sessionID string,
|
||||
msgs []messaging.Message,
|
||||
superseded []string,
|
||||
) error {
|
||||
tx, err := s.pool.Begin(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("sessionstore: replace messages: begin: %w", err)
|
||||
}
|
||||
defer func() { _ = tx.Rollback(ctx) }()
|
||||
|
||||
q := postgresdb.New(tx)
|
||||
survivors := make(map[string]struct{}, len(msgs))
|
||||
for _, msg := range msgs {
|
||||
// No clamp: the caller has already decided where each record belongs.
|
||||
id, err := saveMessagePG(ctx, q, sessionID, msg, false)
|
||||
if err != nil {
|
||||
return fmt.Errorf("sessionstore: replace messages: %w", err)
|
||||
}
|
||||
survivors[id] = struct{}{}
|
||||
}
|
||||
|
||||
doomed := make([]string, 0, len(superseded))
|
||||
for _, id := range superseded {
|
||||
if _, kept := survivors[id]; kept {
|
||||
continue
|
||||
}
|
||||
doomed = append(doomed, id)
|
||||
}
|
||||
if len(doomed) > 0 {
|
||||
if err := q.DeleteMessagesByID(ctx, postgresdb.DeleteMessagesByIDParams{
|
||||
SessionID: sessionID, Ids: doomed,
|
||||
}); err != nil {
|
||||
return fmt.Errorf("sessionstore: replace messages: delete old: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
if err := tx.Commit(ctx); err != nil {
|
||||
return fmt.Errorf("sessionstore: replace messages: commit: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *postgresSessionStore) FindPriorReply(ctx context.Context, sessionID, sourceID, identity string) (string, error) {
|
||||
if sourceID == "" {
|
||||
return "", nil
|
||||
}
|
||||
q := postgresdb.New(s.pool)
|
||||
|
||||
currentID, legacyID := canonicalMessageIDs(sessionID, sourceID)
|
||||
ts, err := q.SourceMessageTs(ctx, postgresdb.SourceMessageTsParams{
|
||||
SessionID: sessionID, MessageID: currentID, MessageID_2: legacyID,
|
||||
})
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return "", nil
|
||||
}
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("sessionstore: find source message: %w", err)
|
||||
}
|
||||
|
||||
next, err := q.NextReplyAfterTs(ctx, postgresdb.NextReplyAfterTsParams{
|
||||
SessionID: sessionID, Ts: ts,
|
||||
})
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return "", nil
|
||||
}
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("sessionstore: find prior reply: %w", err)
|
||||
}
|
||||
if next.Sender != identity || isCompressionSummary(next.Text) {
|
||||
return "", nil
|
||||
}
|
||||
return next.Text, nil
|
||||
}
|
||||
|
||||
func (s *postgresSessionStore) RecentMessages(ctx context.Context, sessionID string, n int) ([]messaging.Message, error) {
|
||||
if n <= 0 {
|
||||
return nil, nil
|
||||
}
|
||||
q := postgresdb.New(s.pool)
|
||||
rows, err := q.RecentMessages(ctx, postgresdb.RecentMessagesParams{
|
||||
SessionID: sessionID, Limit: int32(n),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("sessionstore: recent messages: %w", err)
|
||||
}
|
||||
return recentMessagesFromRows(rows), nil
|
||||
}
|
||||
|
||||
func (s *postgresSessionStore) DeleteRecentMessages(ctx context.Context, sessionID string, n int) (int, error) {
|
||||
if n <= 0 {
|
||||
return 0, nil
|
||||
}
|
||||
q := postgresdb.New(s.pool)
|
||||
deleted, err := q.DeleteRecentMessages(ctx, postgresdb.DeleteRecentMessagesParams{
|
||||
SessionID: sessionID, Limit: int32(n),
|
||||
})
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("sessionstore: delete recent messages: %w", err)
|
||||
}
|
||||
return int(deleted), nil
|
||||
}
|
||||
|
||||
func (s *postgresSessionStore) MessageCount(ctx context.Context, sessionID string) (int, error) {
|
||||
q := postgresdb.New(s.pool)
|
||||
n, err := q.CountMessages(ctx, sessionID)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("sessionstore: message count: %w", err)
|
||||
}
|
||||
return int(n), nil
|
||||
}
|
||||
|
||||
func recentMessagesFromRows(rows []postgresdb.RecentMessagesRow) []messaging.Message {
|
||||
out := make([]messaging.Message, 0, len(rows))
|
||||
for _, row := range rows {
|
||||
out = append(out, messaging.Message{
|
||||
ID: messaging.MessageID(row.MessageID),
|
||||
SourceID: row.SourceID,
|
||||
Sender: row.Sender,
|
||||
SenderID: row.SenderID,
|
||||
Role: messaging.Role(row.Role),
|
||||
Text: row.Text,
|
||||
At: time.UnixMilli(row.Ts).UTC(),
|
||||
ConversationID: messaging.ConversationID{
|
||||
ChannelID: row.ChannelID, ThreadID: row.ThreadID,
|
||||
},
|
||||
})
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// SearchMessages runs a tsvector search over the session's entire message
|
||||
// history, matching message text and sender only. The generated search column
|
||||
// indexes exactly (sender, text), so ts, source_id and the other metadata are
|
||||
// not part of the indexed content -- a query like "2026" cannot match every
|
||||
// message via its timestamp.
|
||||
//
|
||||
// plainto_tsquery is the tsvector analog of the SQLite store's ftsMatchQuery:
|
||||
// it parses the query as plain text, never as query syntax, and ANDs the
|
||||
// surviving terms. Punctuation in the input is therefore treated as text to
|
||||
// tokenize, not as a Boolean operator or column filter, which is the
|
||||
// quote-escaping behaviour ftsMatchQuery provided for FTS5.
|
||||
//
|
||||
// Like SQLite, the full result set is counted exactly, so MessagePage.Truncated
|
||||
// is always false.
|
||||
func (s *postgresSessionStore) SearchMessages(ctx 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)
|
||||
}
|
||||
limit := q.Limit
|
||||
if limit <= 0 {
|
||||
limit = DefaultMessagePageSize
|
||||
}
|
||||
limit = min(limit, MaxMessagePageSize)
|
||||
offset := max(q.Offset, 0)
|
||||
|
||||
dbq := postgresdb.New(s.pool)
|
||||
total, err := dbq.SearchMessagesCount(ctx, postgresdb.SearchMessagesCountParams{
|
||||
SessionID: sessionID, PlaintoTsquery: query,
|
||||
})
|
||||
if err != nil {
|
||||
return MessagePage{}, fmt.Errorf("sessionstore: search messages: count: %w", err)
|
||||
}
|
||||
if offset >= int(total) {
|
||||
return MessagePage{}, nil
|
||||
}
|
||||
|
||||
rows, err := dbq.SearchMessagesPage(ctx, postgresdb.SearchMessagesPageParams{
|
||||
SessionID: sessionID, PlaintoTsquery: query, Limit: int32(limit), Offset: int32(offset),
|
||||
})
|
||||
if err != nil {
|
||||
return MessagePage{}, fmt.Errorf("sessionstore: search messages: %w", err)
|
||||
}
|
||||
|
||||
msgs := make([]messaging.Message, 0, len(rows))
|
||||
for _, row := range rows {
|
||||
msgs = append(msgs, messaging.Message{
|
||||
ID: messaging.MessageID(row.MessageID),
|
||||
SourceID: row.SourceID,
|
||||
Sender: row.Sender,
|
||||
SenderID: row.SenderID,
|
||||
Role: messaging.Role(row.Role),
|
||||
Text: row.Text,
|
||||
At: time.UnixMilli(row.Ts).UTC(),
|
||||
ConversationID: messaging.ConversationID{
|
||||
ChannelID: row.ChannelID, ThreadID: row.ThreadID,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
next := offset + len(msgs)
|
||||
return MessagePage{
|
||||
Messages: msgs,
|
||||
NextOffset: next,
|
||||
HasMore: next < int(total),
|
||||
Truncated: false,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// ── Turns ───────────────────────────────────────────────────────────────────
|
||||
|
||||
func (s *postgresSessionStore) ClaimTurn(ctx context.Context, initial TurnRecord) (TurnRecord, TurnClaim, error) {
|
||||
initial, initialToolCalls, err := prepareInitialTurn(initial, time.Now().UTC())
|
||||
if err != nil {
|
||||
return TurnRecord{}, "", err
|
||||
}
|
||||
|
||||
tx, err := s.pool.Begin(ctx)
|
||||
if err != nil {
|
||||
return TurnRecord{}, "", fmt.Errorf("sessionstore: claim turn: begin: %w", err)
|
||||
}
|
||||
defer func() { _ = tx.Rollback(ctx) }()
|
||||
|
||||
q := postgresdb.New(tx)
|
||||
inserted, err := insertInitialTurnPG(ctx, q, initial, initialToolCalls)
|
||||
if err != nil {
|
||||
return TurnRecord{}, "", err
|
||||
}
|
||||
if inserted {
|
||||
if err := tx.Commit(ctx); err != nil {
|
||||
return TurnRecord{}, "", fmt.Errorf("sessionstore: claim turn: commit: %w", err)
|
||||
}
|
||||
return initial, TurnClaimOwned, nil
|
||||
}
|
||||
|
||||
turn, err := q.TurnByID(ctx, initial.TurnID)
|
||||
if err != nil {
|
||||
return TurnRecord{}, "", fmt.Errorf("sessionstore: read turn: %w", err)
|
||||
}
|
||||
record, err := turnFromRow(turn)
|
||||
if err != nil {
|
||||
return TurnRecord{}, "", err
|
||||
}
|
||||
return claimExistingTurnPG(ctx, q, tx, record, initial)
|
||||
}
|
||||
|
||||
func insertInitialTurnPG(ctx context.Context, q *postgresdb.Queries, initial TurnRecord, toolCallsJSON string) (bool, error) {
|
||||
affected, err := q.InsertTurn(ctx, postgresdb.InsertTurnParams{
|
||||
TurnID: initial.TurnID,
|
||||
SessionID: initial.SessionID,
|
||||
SourceID: initial.SourceID,
|
||||
Status: string(initial.Status),
|
||||
Attempt: int64(initial.Attempt),
|
||||
OwnerID: initial.OwnerID,
|
||||
InputMessageID: initial.InputMessageID,
|
||||
AssistantMessageID: initial.AssistantMessageID,
|
||||
PartialText: initial.PartialText,
|
||||
ResponseText: initial.ResponseText,
|
||||
ToolCalls: toolCallsJSON,
|
||||
Error: initial.Error,
|
||||
CreatedAt: initial.CreatedAt.UnixMilli(),
|
||||
UpdatedAt: initial.UpdatedAt.UnixMilli(),
|
||||
})
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("sessionstore: claim turn: insert: %w", err)
|
||||
}
|
||||
return affected == 1, nil
|
||||
}
|
||||
|
||||
func claimExistingTurnPG(ctx context.Context, q *postgresdb.Queries, tx pgx.Tx, turn, initial TurnRecord) (TurnRecord, TurnClaim, error) {
|
||||
switch turn.Status {
|
||||
case TurnStatusCompleted:
|
||||
return turn, TurnClaimCompleted, nil
|
||||
case TurnStatusAccepted, TurnStatusRunning, TurnStatusPartial:
|
||||
return turn, TurnClaimInProgress, nil
|
||||
case TurnStatusFailed, TurnStatusCancelled:
|
||||
if turn.AssistantMessageID != "" {
|
||||
affected, err := q.FinalizeTurn(ctx, postgresdb.FinalizeTurnParams{
|
||||
TurnID: turn.TurnID,
|
||||
Status: string(TurnStatusCompleted),
|
||||
Error: "",
|
||||
UpdatedAt: time.Now().UTC().UnixMilli(),
|
||||
Attempt: int64(turn.Attempt),
|
||||
OwnerID: turn.OwnerID,
|
||||
})
|
||||
if err != nil {
|
||||
return TurnRecord{}, "", fmt.Errorf("sessionstore: finalize turn: %w", err)
|
||||
}
|
||||
if affected != 1 {
|
||||
return TurnRecord{}, "", fmt.Errorf("%w: %s", ErrTurnOwnershipLost, turn.TurnID)
|
||||
}
|
||||
if err := tx.Commit(ctx); err != nil {
|
||||
return TurnRecord{}, "", fmt.Errorf("sessionstore: finalize turn: commit: %w", err)
|
||||
}
|
||||
turn.Status = TurnStatusCompleted
|
||||
turn.Error = ""
|
||||
return turn, TurnClaimCompleted, nil
|
||||
}
|
||||
oldOwner, oldAttempt := turn.OwnerID, turn.Attempt
|
||||
turn.Status = TurnStatusRunning
|
||||
turn.Attempt++
|
||||
turn.OwnerID = initial.OwnerID
|
||||
turn.Error = ""
|
||||
turn.UpdatedAt = time.Now().UTC()
|
||||
affected, err := q.RetryTurn(ctx, postgresdb.RetryTurnParams{
|
||||
TurnID: turn.TurnID,
|
||||
Status: string(turn.Status),
|
||||
Attempt: int64(turn.Attempt),
|
||||
OwnerID: turn.OwnerID,
|
||||
Error: turn.Error,
|
||||
UpdatedAt: turn.UpdatedAt.UnixMilli(),
|
||||
Attempt_2: int64(oldAttempt),
|
||||
OwnerID_2: oldOwner,
|
||||
})
|
||||
if err != nil {
|
||||
return TurnRecord{}, "", fmt.Errorf("sessionstore: retry turn: %w", err)
|
||||
}
|
||||
if affected != 1 {
|
||||
return TurnRecord{}, "", fmt.Errorf("%w: %s", ErrTurnOwnershipLost, turn.TurnID)
|
||||
}
|
||||
if err := tx.Commit(ctx); err != nil {
|
||||
return TurnRecord{}, "", fmt.Errorf("sessionstore: retry turn: commit: %w", err)
|
||||
}
|
||||
return turn, TurnClaimOwned, nil
|
||||
default:
|
||||
return turn, TurnClaimInProgress, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (s *postgresSessionStore) RecoverTurns(ctx context.Context, ownerID string) error {
|
||||
if ownerID == "" {
|
||||
return fmt.Errorf("sessionstore: recovery owner ID is required")
|
||||
}
|
||||
q := postgresdb.New(s.pool)
|
||||
err := q.RecoverTurns(ctx, postgresdb.RecoverTurnsParams{
|
||||
UpdatedAt: time.Now().UTC().UnixMilli(), OwnerID: ownerID,
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("sessionstore: recover turns: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *postgresSessionStore) GetTurn(ctx context.Context, turnID string) (TurnRecord, bool, error) {
|
||||
if turnID == "" {
|
||||
return TurnRecord{}, false, nil
|
||||
}
|
||||
q := postgresdb.New(s.pool)
|
||||
turn, err := q.TurnByID(ctx, turnID)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return TurnRecord{}, false, nil
|
||||
}
|
||||
if err != nil {
|
||||
return TurnRecord{}, false, fmt.Errorf("sessionstore: get turn: %w", err)
|
||||
}
|
||||
record, err := turnFromRow(turn)
|
||||
if err != nil {
|
||||
return TurnRecord{}, false, err
|
||||
}
|
||||
return record, true, nil
|
||||
}
|
||||
|
||||
func (s *postgresSessionStore) RecentTurns(ctx context.Context, sessionID string, n int) ([]TurnRecord, error) {
|
||||
if n <= 0 {
|
||||
return []TurnRecord{}, nil
|
||||
}
|
||||
if n > 500 {
|
||||
n = 500
|
||||
}
|
||||
q := postgresdb.New(s.pool)
|
||||
rows, err := q.RecentTurns(ctx, postgresdb.RecentTurnsParams{
|
||||
SessionID: sessionID, Limit: int32(n),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("sessionstore: recent turns: %w", err)
|
||||
}
|
||||
// RecentTurns orders newest-first; the contract returns oldest-first.
|
||||
out := make([]TurnRecord, 0, len(rows))
|
||||
for i := len(rows) - 1; i >= 0; i-- {
|
||||
record, err := turnFromRow(rows[i])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, record)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (s *postgresSessionStore) SaveTurn(ctx context.Context, turn TurnRecord) error {
|
||||
if turn.TurnID == "" {
|
||||
return fmt.Errorf("sessionstore: turn ID is required")
|
||||
}
|
||||
q := postgresdb.New(s.pool)
|
||||
current, err := q.TurnAttemptOwner(ctx, turn.TurnID)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return fmt.Errorf("sessionstore: save turn: turn not found")
|
||||
}
|
||||
if err != nil {
|
||||
return fmt.Errorf("sessionstore: save turn: read: %w", err)
|
||||
}
|
||||
if int(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()
|
||||
}
|
||||
toolCallsJSON, err := marshalToolCalls(turn.ToolCalls)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
affected, err := q.SaveTurn(ctx, postgresdb.SaveTurnParams{
|
||||
TurnID: turn.TurnID,
|
||||
SessionID: turn.SessionID,
|
||||
SourceID: turn.SourceID,
|
||||
Status: string(turn.Status),
|
||||
Attempt: int64(turn.Attempt),
|
||||
OwnerID: turn.OwnerID,
|
||||
InputMessageID: turn.InputMessageID,
|
||||
AssistantMessageID: turn.AssistantMessageID,
|
||||
PartialText: turn.PartialText,
|
||||
ResponseText: turn.ResponseText,
|
||||
ToolCalls: toolCallsJSON,
|
||||
Error: turn.Error,
|
||||
CreatedAt: turn.CreatedAt.UnixMilli(),
|
||||
UpdatedAt: turn.UpdatedAt.UnixMilli(),
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("sessionstore: save turn: %w", err)
|
||||
}
|
||||
if affected != 1 {
|
||||
return fmt.Errorf("%w: %s", ErrTurnOwnershipLost, turn.TurnID)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *postgresSessionStore) ListRecoverableTurns(ctx context.Context) ([]TurnRecord, error) {
|
||||
q := postgresdb.New(s.pool)
|
||||
rows, err := q.ListRecoverableTurns(ctx)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("sessionstore: list recoverable turns: %w", err)
|
||||
}
|
||||
out := make([]TurnRecord, 0, len(rows))
|
||||
for _, row := range rows {
|
||||
record, err := turnFromRow(row)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, record)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func turnFromRow(row postgresdb.Turn) (TurnRecord, error) {
|
||||
toolCalls, err := unmarshalToolCalls(row.ToolCalls)
|
||||
if err != nil {
|
||||
return TurnRecord{}, err
|
||||
}
|
||||
return TurnRecord{
|
||||
TurnID: row.TurnID,
|
||||
SessionID: row.SessionID,
|
||||
SourceID: row.SourceID,
|
||||
Status: TurnStatus(row.Status),
|
||||
Attempt: int(row.Attempt),
|
||||
OwnerID: row.OwnerID,
|
||||
InputMessageID: row.InputMessageID,
|
||||
AssistantMessageID: row.AssistantMessageID,
|
||||
PartialText: row.PartialText,
|
||||
ResponseText: row.ResponseText,
|
||||
ToolCalls: toolCalls,
|
||||
Error: row.Error,
|
||||
CreatedAt: time.UnixMilli(row.CreatedAt).UTC(),
|
||||
UpdatedAt: time.UnixMilli(row.UpdatedAt).UTC(),
|
||||
}, nil
|
||||
}
|
||||
@@ -0,0 +1,191 @@
|
||||
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)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
-- Gateway conversation schema: sessions, messages, turns and full-text search.
|
||||
--
|
||||
-- Translations from the SQLite schema this replaces (session_store_sqlite.go),
|
||||
-- and why:
|
||||
-- * Times stay bigint milliseconds. SQLite stored them as INTEGER ms and the
|
||||
-- monotonic clamp adds one to that integer (MAX(ts) + 1); a timestamptz
|
||||
-- translation would change the unit of the "+1" and therefore the strict
|
||||
-- message-ordering contract, so the millisecond representation is kept
|
||||
-- verbatim and the Go adapter converts with time.UnixMilli exactly as the
|
||||
-- SQLite store did.
|
||||
-- * FTS5 becomes a STORED generated tsvector column over (sender, text) plus
|
||||
-- a GIN index. The external-content FTS5 table was kept in sync by three
|
||||
-- triggers on messages; a generated column cannot drift from its row, so
|
||||
-- the triggers are unnecessary here. The 'simple' configuration matches
|
||||
-- FTS5's default tokenizer: case-folding only, no stemming, no stop words.
|
||||
-- * Every column behind a Go int is bigint: SQLite's INTEGER is 64-bit.
|
||||
|
||||
-- +goose Up
|
||||
|
||||
CREATE TABLE sessions (
|
||||
session_id text PRIMARY KEY,
|
||||
platform text NOT NULL DEFAULT '',
|
||||
bot_user text NOT NULL DEFAULT '',
|
||||
channel_id text NOT NULL DEFAULT '',
|
||||
thread_id text NOT NULL DEFAULT '',
|
||||
title text NOT NULL DEFAULT '',
|
||||
parent_session_id text NOT NULL DEFAULT '',
|
||||
branch_name text NOT NULL DEFAULT '',
|
||||
created_at bigint NOT NULL DEFAULT 0,
|
||||
last_active_at bigint NOT NULL DEFAULT 0
|
||||
);
|
||||
CREATE INDEX idx_sessions_channel ON sessions (platform, channel_id);
|
||||
-- Ordering is by recency: last_active_at when set, created_at otherwise
|
||||
-- (GREATEST is the two-argument max SQLite's MAX(a, b) expressed). The index
|
||||
-- matches that expression so newest-first listings do not sort at query time.
|
||||
CREATE INDEX idx_sessions_recency
|
||||
ON sessions (platform, channel_id, GREATEST(last_active_at, created_at) DESC);
|
||||
|
||||
CREATE TABLE messages (
|
||||
id bigint GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
|
||||
message_id text NOT NULL,
|
||||
session_id text NOT NULL,
|
||||
source_id text NOT NULL DEFAULT '',
|
||||
sender text NOT NULL DEFAULT '',
|
||||
sender_id text NOT NULL DEFAULT '',
|
||||
role text NOT NULL DEFAULT '',
|
||||
text text NOT NULL DEFAULT '',
|
||||
ts bigint NOT NULL,
|
||||
-- Full-text search over sender and text only: ts, source_id and other
|
||||
-- metadata are not indexed, so a query like "2026" cannot match every
|
||||
-- message via its timestamp (the SQLite FTS5 external-content index kept
|
||||
-- exactly these two columns).
|
||||
search tsvector GENERATED ALWAYS AS (to_tsvector('simple', sender || ' ' || text)) STORED,
|
||||
UNIQUE (session_id, message_id)
|
||||
);
|
||||
CREATE INDEX idx_messages_session_ts ON messages (session_id, ts);
|
||||
CREATE INDEX idx_messages_search ON messages USING GIN (search);
|
||||
|
||||
CREATE TABLE turns (
|
||||
turn_id text PRIMARY KEY,
|
||||
session_id text NOT NULL,
|
||||
source_id text NOT NULL DEFAULT '',
|
||||
status text NOT NULL,
|
||||
attempt bigint NOT NULL DEFAULT 0,
|
||||
owner_id text NOT NULL DEFAULT '',
|
||||
input_message_id text NOT NULL DEFAULT '',
|
||||
assistant_message_id text NOT NULL DEFAULT '',
|
||||
partial_text text NOT NULL DEFAULT '',
|
||||
response_text text NOT NULL DEFAULT '',
|
||||
tool_calls text NOT NULL DEFAULT '[]',
|
||||
error text NOT NULL DEFAULT '',
|
||||
created_at bigint NOT NULL,
|
||||
updated_at bigint NOT NULL
|
||||
);
|
||||
CREATE INDEX idx_turns_session_source ON turns (session_id, source_id);
|
||||
CREATE INDEX idx_turns_status_updated ON turns (status, updated_at);
|
||||
|
||||
-- +goose Down
|
||||
|
||||
DROP TABLE turns;
|
||||
DROP TABLE messages;
|
||||
DROP TABLE sessions;
|
||||
@@ -0,0 +1,844 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.31.1
|
||||
// source: gateway.sql
|
||||
|
||||
package postgresdb
|
||||
|
||||
import (
|
||||
"context"
|
||||
)
|
||||
|
||||
const countMessages = `-- name: CountMessages :one
|
||||
SELECT COUNT(*) FROM messages WHERE session_id = $1
|
||||
`
|
||||
|
||||
func (q *Queries) CountMessages(ctx context.Context, sessionID string) (int64, error) {
|
||||
row := q.db.QueryRow(ctx, countMessages, sessionID)
|
||||
var count int64
|
||||
err := row.Scan(&count)
|
||||
return count, err
|
||||
}
|
||||
|
||||
const deleteMessagesByID = `-- name: DeleteMessagesByID :exec
|
||||
DELETE FROM messages WHERE session_id = $1 AND message_id = ANY($2::text[])
|
||||
`
|
||||
|
||||
type DeleteMessagesByIDParams struct {
|
||||
SessionID string
|
||||
Ids []string
|
||||
}
|
||||
|
||||
func (q *Queries) DeleteMessagesByID(ctx context.Context, arg DeleteMessagesByIDParams) error {
|
||||
_, err := q.db.Exec(ctx, deleteMessagesByID, arg.SessionID, arg.Ids)
|
||||
return err
|
||||
}
|
||||
|
||||
const deleteRecentMessages = `-- name: DeleteRecentMessages :execrows
|
||||
DELETE FROM messages WHERE id IN (
|
||||
SELECT m.id FROM messages m WHERE m.session_id = $1 ORDER BY m.ts DESC LIMIT $2
|
||||
)
|
||||
`
|
||||
|
||||
type DeleteRecentMessagesParams struct {
|
||||
SessionID string
|
||||
Limit int32
|
||||
}
|
||||
|
||||
func (q *Queries) DeleteRecentMessages(ctx context.Context, arg DeleteRecentMessagesParams) (int64, error) {
|
||||
result, err := q.db.Exec(ctx, deleteRecentMessages, arg.SessionID, arg.Limit)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return result.RowsAffected(), nil
|
||||
}
|
||||
|
||||
const deleteSession = `-- name: DeleteSession :exec
|
||||
DELETE FROM sessions WHERE session_id = $1
|
||||
`
|
||||
|
||||
func (q *Queries) DeleteSession(ctx context.Context, sessionID string) error {
|
||||
_, err := q.db.Exec(ctx, deleteSession, sessionID)
|
||||
return err
|
||||
}
|
||||
|
||||
const deleteSessionMessages = `-- name: DeleteSessionMessages :exec
|
||||
DELETE FROM messages WHERE session_id = $1
|
||||
`
|
||||
|
||||
func (q *Queries) DeleteSessionMessages(ctx context.Context, sessionID string) error {
|
||||
_, err := q.db.Exec(ctx, deleteSessionMessages, sessionID)
|
||||
return err
|
||||
}
|
||||
|
||||
const deleteSessionTurns = `-- name: DeleteSessionTurns :exec
|
||||
DELETE FROM turns WHERE session_id = $1
|
||||
`
|
||||
|
||||
func (q *Queries) DeleteSessionTurns(ctx context.Context, sessionID string) error {
|
||||
_, err := q.db.Exec(ctx, deleteSessionTurns, sessionID)
|
||||
return err
|
||||
}
|
||||
|
||||
const finalizeTurn = `-- name: FinalizeTurn :execrows
|
||||
UPDATE turns SET status = $2, error = $3, updated_at = $4
|
||||
WHERE turn_id = $1 AND attempt = $5 AND owner_id = $6
|
||||
`
|
||||
|
||||
type FinalizeTurnParams struct {
|
||||
TurnID string
|
||||
Status string
|
||||
Error string
|
||||
UpdatedAt int64
|
||||
Attempt int64
|
||||
OwnerID string
|
||||
}
|
||||
|
||||
func (q *Queries) FinalizeTurn(ctx context.Context, arg FinalizeTurnParams) (int64, error) {
|
||||
result, err := q.db.Exec(ctx, finalizeTurn,
|
||||
arg.TurnID,
|
||||
arg.Status,
|
||||
arg.Error,
|
||||
arg.UpdatedAt,
|
||||
arg.Attempt,
|
||||
arg.OwnerID,
|
||||
)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return result.RowsAffected(), nil
|
||||
}
|
||||
|
||||
const insertMessageAt = `-- name: InsertMessageAt :exec
|
||||
INSERT INTO messages (message_id, session_id, source_id, sender, sender_id, role, text, ts)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
|
||||
ON CONFLICT (session_id, message_id) DO NOTHING
|
||||
`
|
||||
|
||||
type InsertMessageAtParams struct {
|
||||
MessageID string
|
||||
SessionID string
|
||||
SourceID string
|
||||
Sender string
|
||||
SenderID string
|
||||
Role string
|
||||
Text string
|
||||
Ts int64
|
||||
}
|
||||
|
||||
// Rewriting history places records deliberately, so no clamp applies.
|
||||
func (q *Queries) InsertMessageAt(ctx context.Context, arg InsertMessageAtParams) error {
|
||||
_, err := q.db.Exec(ctx, insertMessageAt,
|
||||
arg.MessageID,
|
||||
arg.SessionID,
|
||||
arg.SourceID,
|
||||
arg.Sender,
|
||||
arg.SenderID,
|
||||
arg.Role,
|
||||
arg.Text,
|
||||
arg.Ts,
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
const insertMessageClamped = `-- name: InsertMessageClamped :exec
|
||||
INSERT INTO messages (message_id, session_id, source_id, sender, sender_id, role, text, ts)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, (
|
||||
SELECT CASE
|
||||
WHEN MAX(m.ts) IS NOT NULL AND MAX(m.ts) >= $8 THEN MAX(m.ts) + 1
|
||||
ELSE $8
|
||||
END
|
||||
FROM messages m WHERE m.session_id = $2
|
||||
))
|
||||
ON CONFLICT (session_id, message_id) DO NOTHING
|
||||
`
|
||||
|
||||
type InsertMessageClampedParams struct {
|
||||
MessageID string
|
||||
SessionID string
|
||||
SourceID string
|
||||
Sender string
|
||||
SenderID string
|
||||
Role string
|
||||
Text string
|
||||
Ts int64
|
||||
}
|
||||
|
||||
// The append path's strictly-increasing timestamp. The clamp is computed in
|
||||
// SQL, not in Go: the CASE assigns MAX(ts)+1 when the caller's timestamp is
|
||||
// not ahead of the session's newest, else the caller's timestamp. A Go-side
|
||||
// read-then-write would race two concurrent appends to the same session.
|
||||
func (q *Queries) InsertMessageClamped(ctx context.Context, arg InsertMessageClampedParams) error {
|
||||
_, err := q.db.Exec(ctx, insertMessageClamped,
|
||||
arg.MessageID,
|
||||
arg.SessionID,
|
||||
arg.SourceID,
|
||||
arg.Sender,
|
||||
arg.SenderID,
|
||||
arg.Role,
|
||||
arg.Text,
|
||||
arg.Ts,
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
const insertTurn = `-- name: InsertTurn :execrows
|
||||
|
||||
INSERT INTO turns (turn_id, session_id, source_id, status, attempt, owner_id,
|
||||
input_message_id, assistant_message_id, partial_text,
|
||||
response_text, tool_calls, error, created_at, updated_at)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14)
|
||||
ON CONFLICT (turn_id) DO NOTHING
|
||||
`
|
||||
|
||||
type InsertTurnParams struct {
|
||||
TurnID string
|
||||
SessionID string
|
||||
SourceID string
|
||||
Status string
|
||||
Attempt int64
|
||||
OwnerID string
|
||||
InputMessageID string
|
||||
AssistantMessageID string
|
||||
PartialText string
|
||||
ResponseText string
|
||||
ToolCalls string
|
||||
Error string
|
||||
CreatedAt int64
|
||||
UpdatedAt int64
|
||||
}
|
||||
|
||||
// ── Turns ───────────────────────────────────────────────────────────────────
|
||||
func (q *Queries) InsertTurn(ctx context.Context, arg InsertTurnParams) (int64, error) {
|
||||
result, err := q.db.Exec(ctx, insertTurn,
|
||||
arg.TurnID,
|
||||
arg.SessionID,
|
||||
arg.SourceID,
|
||||
arg.Status,
|
||||
arg.Attempt,
|
||||
arg.OwnerID,
|
||||
arg.InputMessageID,
|
||||
arg.AssistantMessageID,
|
||||
arg.PartialText,
|
||||
arg.ResponseText,
|
||||
arg.ToolCalls,
|
||||
arg.Error,
|
||||
arg.CreatedAt,
|
||||
arg.UpdatedAt,
|
||||
)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return result.RowsAffected(), nil
|
||||
}
|
||||
|
||||
const listRecoverableTurns = `-- name: ListRecoverableTurns :many
|
||||
SELECT turn_id, session_id, source_id, status, attempt, owner_id,
|
||||
input_message_id, assistant_message_id, partial_text,
|
||||
response_text, tool_calls, error, created_at, updated_at
|
||||
FROM turns WHERE status IN ('accepted', 'running', 'partial') ORDER BY updated_at ASC
|
||||
`
|
||||
|
||||
func (q *Queries) ListRecoverableTurns(ctx context.Context) ([]Turn, error) {
|
||||
rows, err := q.db.Query(ctx, listRecoverableTurns)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []Turn
|
||||
for rows.Next() {
|
||||
var i Turn
|
||||
if err := rows.Scan(
|
||||
&i.TurnID,
|
||||
&i.SessionID,
|
||||
&i.SourceID,
|
||||
&i.Status,
|
||||
&i.Attempt,
|
||||
&i.OwnerID,
|
||||
&i.InputMessageID,
|
||||
&i.AssistantMessageID,
|
||||
&i.PartialText,
|
||||
&i.ResponseText,
|
||||
&i.ToolCalls,
|
||||
&i.Error,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const listSessions = `-- name: ListSessions :many
|
||||
SELECT session_id, platform, bot_user, channel_id, thread_id,
|
||||
title, parent_session_id, branch_name, created_at, last_active_at
|
||||
FROM sessions
|
||||
ORDER BY GREATEST(last_active_at, created_at) DESC
|
||||
`
|
||||
|
||||
func (q *Queries) ListSessions(ctx context.Context) ([]Session, error) {
|
||||
rows, err := q.db.Query(ctx, listSessions)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []Session
|
||||
for rows.Next() {
|
||||
var i Session
|
||||
if err := rows.Scan(
|
||||
&i.SessionID,
|
||||
&i.Platform,
|
||||
&i.BotUser,
|
||||
&i.ChannelID,
|
||||
&i.ThreadID,
|
||||
&i.Title,
|
||||
&i.ParentSessionID,
|
||||
&i.BranchName,
|
||||
&i.CreatedAt,
|
||||
&i.LastActiveAt,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const lockSessionMessages = `-- name: LockSessionMessages :exec
|
||||
|
||||
SELECT pg_advisory_xact_lock(hashtextextended('messages:' || $1::text, 0))
|
||||
`
|
||||
|
||||
// ── Messages ────────────────────────────────────────────────────────────────
|
||||
// Serialises message appends per session so the monotonic clamp's MAX(ts)
|
||||
// read cannot race a concurrent append of the same session. Transaction-scoped:
|
||||
// released at commit/rollback, which is exactly the lifetime of the append.
|
||||
func (q *Queries) LockSessionMessages(ctx context.Context, dollar_1 string) error {
|
||||
_, err := q.db.Exec(ctx, lockSessionMessages, dollar_1)
|
||||
return err
|
||||
}
|
||||
|
||||
const messageID = `-- name: MessageID :one
|
||||
SELECT message_id FROM messages WHERE session_id = $1 AND message_id = $2
|
||||
`
|
||||
|
||||
type MessageIDParams struct {
|
||||
SessionID string
|
||||
MessageID string
|
||||
}
|
||||
|
||||
func (q *Queries) MessageID(ctx context.Context, arg MessageIDParams) (string, error) {
|
||||
row := q.db.QueryRow(ctx, messageID, arg.SessionID, arg.MessageID)
|
||||
var message_id string
|
||||
err := row.Scan(&message_id)
|
||||
return message_id, err
|
||||
}
|
||||
|
||||
const nextReplyAfterTs = `-- name: NextReplyAfterTs :one
|
||||
SELECT sender, text FROM messages
|
||||
WHERE session_id = $1 AND ts > $2 ORDER BY ts ASC LIMIT 1
|
||||
`
|
||||
|
||||
type NextReplyAfterTsParams struct {
|
||||
SessionID string
|
||||
Ts int64
|
||||
}
|
||||
|
||||
type NextReplyAfterTsRow struct {
|
||||
Sender string
|
||||
Text string
|
||||
}
|
||||
|
||||
func (q *Queries) NextReplyAfterTs(ctx context.Context, arg NextReplyAfterTsParams) (NextReplyAfterTsRow, error) {
|
||||
row := q.db.QueryRow(ctx, nextReplyAfterTs, arg.SessionID, arg.Ts)
|
||||
var i NextReplyAfterTsRow
|
||||
err := row.Scan(&i.Sender, &i.Text)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const recentMessages = `-- name: RecentMessages :many
|
||||
SELECT m.message_id, m.source_id, m.sender, m.sender_id, m.role, m.text, m.ts,
|
||||
COALESCE(s.channel_id, '') AS channel_id, COALESCE(s.thread_id, '') AS thread_id
|
||||
FROM (
|
||||
SELECT msg.message_id, msg.source_id, msg.sender, msg.sender_id, msg.role, msg.text, msg.ts, msg.session_id
|
||||
FROM messages msg WHERE msg.session_id = $1 ORDER BY msg.ts DESC LIMIT $2
|
||||
) m
|
||||
LEFT JOIN sessions s ON s.session_id = m.session_id
|
||||
ORDER BY m.ts ASC
|
||||
`
|
||||
|
||||
type RecentMessagesParams struct {
|
||||
SessionID string
|
||||
Limit int32
|
||||
}
|
||||
|
||||
type RecentMessagesRow struct {
|
||||
MessageID string
|
||||
SourceID string
|
||||
Sender string
|
||||
SenderID string
|
||||
Role string
|
||||
Text string
|
||||
Ts int64
|
||||
ChannelID string
|
||||
ThreadID string
|
||||
}
|
||||
|
||||
func (q *Queries) RecentMessages(ctx context.Context, arg RecentMessagesParams) ([]RecentMessagesRow, error) {
|
||||
rows, err := q.db.Query(ctx, recentMessages, arg.SessionID, arg.Limit)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []RecentMessagesRow
|
||||
for rows.Next() {
|
||||
var i RecentMessagesRow
|
||||
if err := rows.Scan(
|
||||
&i.MessageID,
|
||||
&i.SourceID,
|
||||
&i.Sender,
|
||||
&i.SenderID,
|
||||
&i.Role,
|
||||
&i.Text,
|
||||
&i.Ts,
|
||||
&i.ChannelID,
|
||||
&i.ThreadID,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const recentTurns = `-- name: RecentTurns :many
|
||||
SELECT turn_id, session_id, source_id, status, attempt, owner_id,
|
||||
input_message_id, assistant_message_id, partial_text,
|
||||
response_text, tool_calls, error, created_at, updated_at
|
||||
FROM turns WHERE session_id = $1 ORDER BY created_at DESC LIMIT $2
|
||||
`
|
||||
|
||||
type RecentTurnsParams struct {
|
||||
SessionID string
|
||||
Limit int32
|
||||
}
|
||||
|
||||
func (q *Queries) RecentTurns(ctx context.Context, arg RecentTurnsParams) ([]Turn, error) {
|
||||
rows, err := q.db.Query(ctx, recentTurns, arg.SessionID, arg.Limit)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []Turn
|
||||
for rows.Next() {
|
||||
var i Turn
|
||||
if err := rows.Scan(
|
||||
&i.TurnID,
|
||||
&i.SessionID,
|
||||
&i.SourceID,
|
||||
&i.Status,
|
||||
&i.Attempt,
|
||||
&i.OwnerID,
|
||||
&i.InputMessageID,
|
||||
&i.AssistantMessageID,
|
||||
&i.PartialText,
|
||||
&i.ResponseText,
|
||||
&i.ToolCalls,
|
||||
&i.Error,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const recoverTurns = `-- name: RecoverTurns :exec
|
||||
UPDATE turns SET status = 'failed', error = 'recovered after process restart', updated_at = $1
|
||||
WHERE status IN ('accepted', 'running', 'partial') AND owner_id <> $2
|
||||
`
|
||||
|
||||
type RecoverTurnsParams struct {
|
||||
UpdatedAt int64
|
||||
OwnerID string
|
||||
}
|
||||
|
||||
func (q *Queries) RecoverTurns(ctx context.Context, arg RecoverTurnsParams) error {
|
||||
_, err := q.db.Exec(ctx, recoverTurns, arg.UpdatedAt, arg.OwnerID)
|
||||
return err
|
||||
}
|
||||
|
||||
const retryTurn = `-- name: RetryTurn :execrows
|
||||
UPDATE turns SET status = $2, attempt = $3, owner_id = $4, error = $5, updated_at = $6
|
||||
WHERE turn_id = $1 AND attempt = $7 AND owner_id = $8
|
||||
`
|
||||
|
||||
type RetryTurnParams struct {
|
||||
TurnID string
|
||||
Status string
|
||||
Attempt int64
|
||||
OwnerID string
|
||||
Error string
|
||||
UpdatedAt int64
|
||||
Attempt_2 int64
|
||||
OwnerID_2 string
|
||||
}
|
||||
|
||||
func (q *Queries) RetryTurn(ctx context.Context, arg RetryTurnParams) (int64, error) {
|
||||
result, err := q.db.Exec(ctx, retryTurn,
|
||||
arg.TurnID,
|
||||
arg.Status,
|
||||
arg.Attempt,
|
||||
arg.OwnerID,
|
||||
arg.Error,
|
||||
arg.UpdatedAt,
|
||||
arg.Attempt_2,
|
||||
arg.OwnerID_2,
|
||||
)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return result.RowsAffected(), nil
|
||||
}
|
||||
|
||||
const saveSession = `-- name: SaveSession :exec
|
||||
|
||||
|
||||
INSERT INTO sessions (session_id, platform, bot_user, channel_id, thread_id,
|
||||
title, parent_session_id, branch_name, created_at, last_active_at)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
|
||||
ON CONFLICT (session_id) DO UPDATE SET
|
||||
platform = EXCLUDED.platform,
|
||||
bot_user = EXCLUDED.bot_user,
|
||||
channel_id = EXCLUDED.channel_id,
|
||||
thread_id = EXCLUDED.thread_id,
|
||||
title = EXCLUDED.title,
|
||||
parent_session_id = EXCLUDED.parent_session_id,
|
||||
branch_name = EXCLUDED.branch_name,
|
||||
created_at = EXCLUDED.created_at,
|
||||
last_active_at = EXCLUDED.last_active_at
|
||||
`
|
||||
|
||||
type SaveSessionParams struct {
|
||||
SessionID string
|
||||
Platform string
|
||||
BotUser string
|
||||
ChannelID string
|
||||
ThreadID string
|
||||
Title string
|
||||
ParentSessionID string
|
||||
BranchName string
|
||||
CreatedAt int64
|
||||
LastActiveAt int64
|
||||
}
|
||||
|
||||
// Gateway conversation queries: sessions, messages, turns, full-text search.
|
||||
// These back internal/gateway's PostgreSQL SessionStore.
|
||||
// ── Sessions ────────────────────────────────────────────────────────────────
|
||||
func (q *Queries) SaveSession(ctx context.Context, arg SaveSessionParams) error {
|
||||
_, err := q.db.Exec(ctx, saveSession,
|
||||
arg.SessionID,
|
||||
arg.Platform,
|
||||
arg.BotUser,
|
||||
arg.ChannelID,
|
||||
arg.ThreadID,
|
||||
arg.Title,
|
||||
arg.ParentSessionID,
|
||||
arg.BranchName,
|
||||
arg.CreatedAt,
|
||||
arg.LastActiveAt,
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
const saveTurn = `-- name: SaveTurn :execrows
|
||||
UPDATE turns SET
|
||||
session_id = $2, source_id = $3, status = $4, attempt = $5, owner_id = $6,
|
||||
input_message_id = $7, assistant_message_id = $8, partial_text = $9,
|
||||
response_text = $10, tool_calls = $11, error = $12, created_at = $13, updated_at = $14
|
||||
WHERE turn_id = $1 AND attempt = $5 AND owner_id = $6
|
||||
`
|
||||
|
||||
type SaveTurnParams struct {
|
||||
TurnID string
|
||||
SessionID string
|
||||
SourceID string
|
||||
Status string
|
||||
Attempt int64
|
||||
OwnerID string
|
||||
InputMessageID string
|
||||
AssistantMessageID string
|
||||
PartialText string
|
||||
ResponseText string
|
||||
ToolCalls string
|
||||
Error string
|
||||
CreatedAt int64
|
||||
UpdatedAt int64
|
||||
}
|
||||
|
||||
func (q *Queries) SaveTurn(ctx context.Context, arg SaveTurnParams) (int64, error) {
|
||||
result, err := q.db.Exec(ctx, saveTurn,
|
||||
arg.TurnID,
|
||||
arg.SessionID,
|
||||
arg.SourceID,
|
||||
arg.Status,
|
||||
arg.Attempt,
|
||||
arg.OwnerID,
|
||||
arg.InputMessageID,
|
||||
arg.AssistantMessageID,
|
||||
arg.PartialText,
|
||||
arg.ResponseText,
|
||||
arg.ToolCalls,
|
||||
arg.Error,
|
||||
arg.CreatedAt,
|
||||
arg.UpdatedAt,
|
||||
)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return result.RowsAffected(), nil
|
||||
}
|
||||
|
||||
const searchMessagesCount = `-- name: SearchMessagesCount :one
|
||||
SELECT COUNT(*) FROM messages
|
||||
WHERE session_id = $1 AND search @@ plainto_tsquery('simple', $2)
|
||||
`
|
||||
|
||||
type SearchMessagesCountParams struct {
|
||||
SessionID string
|
||||
PlaintoTsquery string
|
||||
}
|
||||
|
||||
func (q *Queries) SearchMessagesCount(ctx context.Context, arg SearchMessagesCountParams) (int64, error) {
|
||||
row := q.db.QueryRow(ctx, searchMessagesCount, arg.SessionID, arg.PlaintoTsquery)
|
||||
var count int64
|
||||
err := row.Scan(&count)
|
||||
return count, err
|
||||
}
|
||||
|
||||
const searchMessagesPage = `-- name: SearchMessagesPage :many
|
||||
SELECT m.message_id, m.source_id, m.sender, m.sender_id, m.role, m.text, m.ts,
|
||||
COALESCE(s.channel_id, '') AS channel_id, COALESCE(s.thread_id, '') AS thread_id
|
||||
FROM messages m
|
||||
LEFT JOIN sessions s ON s.session_id = m.session_id
|
||||
WHERE m.session_id = $1 AND m.search @@ plainto_tsquery('simple', $2)
|
||||
ORDER BY ts_rank(m.search, plainto_tsquery('simple', $2)) DESC, m.ts DESC, m.id DESC
|
||||
LIMIT $3 OFFSET $4
|
||||
`
|
||||
|
||||
type SearchMessagesPageParams struct {
|
||||
SessionID string
|
||||
PlaintoTsquery string
|
||||
Limit int32
|
||||
Offset int32
|
||||
}
|
||||
|
||||
type SearchMessagesPageRow struct {
|
||||
MessageID string
|
||||
SourceID string
|
||||
Sender string
|
||||
SenderID string
|
||||
Role string
|
||||
Text string
|
||||
Ts int64
|
||||
ChannelID string
|
||||
ThreadID string
|
||||
}
|
||||
|
||||
func (q *Queries) SearchMessagesPage(ctx context.Context, arg SearchMessagesPageParams) ([]SearchMessagesPageRow, error) {
|
||||
rows, err := q.db.Query(ctx, searchMessagesPage,
|
||||
arg.SessionID,
|
||||
arg.PlaintoTsquery,
|
||||
arg.Limit,
|
||||
arg.Offset,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []SearchMessagesPageRow
|
||||
for rows.Next() {
|
||||
var i SearchMessagesPageRow
|
||||
if err := rows.Scan(
|
||||
&i.MessageID,
|
||||
&i.SourceID,
|
||||
&i.Sender,
|
||||
&i.SenderID,
|
||||
&i.Role,
|
||||
&i.Text,
|
||||
&i.Ts,
|
||||
&i.ChannelID,
|
||||
&i.ThreadID,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const sessionByID = `-- name: SessionByID :one
|
||||
SELECT session_id, platform, bot_user, channel_id, thread_id,
|
||||
title, parent_session_id, branch_name, created_at, last_active_at
|
||||
FROM sessions WHERE session_id = $1
|
||||
`
|
||||
|
||||
func (q *Queries) SessionByID(ctx context.Context, sessionID string) (Session, error) {
|
||||
row := q.db.QueryRow(ctx, sessionByID, sessionID)
|
||||
var i Session
|
||||
err := row.Scan(
|
||||
&i.SessionID,
|
||||
&i.Platform,
|
||||
&i.BotUser,
|
||||
&i.ChannelID,
|
||||
&i.ThreadID,
|
||||
&i.Title,
|
||||
&i.ParentSessionID,
|
||||
&i.BranchName,
|
||||
&i.CreatedAt,
|
||||
&i.LastActiveAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const sessionsByChannel = `-- name: SessionsByChannel :many
|
||||
SELECT session_id, platform, bot_user, channel_id, thread_id,
|
||||
title, parent_session_id, branch_name, created_at, last_active_at
|
||||
FROM sessions
|
||||
WHERE platform = $1 AND channel_id = $2
|
||||
ORDER BY GREATEST(last_active_at, created_at) DESC
|
||||
`
|
||||
|
||||
type SessionsByChannelParams struct {
|
||||
Platform string
|
||||
ChannelID string
|
||||
}
|
||||
|
||||
func (q *Queries) SessionsByChannel(ctx context.Context, arg SessionsByChannelParams) ([]Session, error) {
|
||||
rows, err := q.db.Query(ctx, sessionsByChannel, arg.Platform, arg.ChannelID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []Session
|
||||
for rows.Next() {
|
||||
var i Session
|
||||
if err := rows.Scan(
|
||||
&i.SessionID,
|
||||
&i.Platform,
|
||||
&i.BotUser,
|
||||
&i.ChannelID,
|
||||
&i.ThreadID,
|
||||
&i.Title,
|
||||
&i.ParentSessionID,
|
||||
&i.BranchName,
|
||||
&i.CreatedAt,
|
||||
&i.LastActiveAt,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const sourceMessageTs = `-- name: SourceMessageTs :one
|
||||
SELECT ts FROM messages
|
||||
WHERE session_id = $1 AND message_id IN ($2, $3)
|
||||
ORDER BY CASE WHEN message_id = $3 THEN 0 ELSE 1 END
|
||||
LIMIT 1
|
||||
`
|
||||
|
||||
type SourceMessageTsParams struct {
|
||||
SessionID string
|
||||
MessageID string
|
||||
MessageID_2 string
|
||||
}
|
||||
|
||||
func (q *Queries) SourceMessageTs(ctx context.Context, arg SourceMessageTsParams) (int64, error) {
|
||||
row := q.db.QueryRow(ctx, sourceMessageTs, arg.SessionID, arg.MessageID, arg.MessageID_2)
|
||||
var ts int64
|
||||
err := row.Scan(&ts)
|
||||
return ts, err
|
||||
}
|
||||
|
||||
const touchSession = `-- name: TouchSession :exec
|
||||
UPDATE sessions SET last_active_at = $2 WHERE session_id = $1
|
||||
`
|
||||
|
||||
type TouchSessionParams struct {
|
||||
SessionID string
|
||||
LastActiveAt int64
|
||||
}
|
||||
|
||||
func (q *Queries) TouchSession(ctx context.Context, arg TouchSessionParams) error {
|
||||
_, err := q.db.Exec(ctx, touchSession, arg.SessionID, arg.LastActiveAt)
|
||||
return err
|
||||
}
|
||||
|
||||
const turnAttemptOwner = `-- name: TurnAttemptOwner :one
|
||||
SELECT attempt, owner_id FROM turns WHERE turn_id = $1
|
||||
`
|
||||
|
||||
type TurnAttemptOwnerRow struct {
|
||||
Attempt int64
|
||||
OwnerID string
|
||||
}
|
||||
|
||||
func (q *Queries) TurnAttemptOwner(ctx context.Context, turnID string) (TurnAttemptOwnerRow, error) {
|
||||
row := q.db.QueryRow(ctx, turnAttemptOwner, turnID)
|
||||
var i TurnAttemptOwnerRow
|
||||
err := row.Scan(&i.Attempt, &i.OwnerID)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const turnByID = `-- name: TurnByID :one
|
||||
SELECT turn_id, session_id, source_id, status, attempt, owner_id,
|
||||
input_message_id, assistant_message_id, partial_text,
|
||||
response_text, tool_calls, error, created_at, updated_at
|
||||
FROM turns WHERE turn_id = $1
|
||||
`
|
||||
|
||||
func (q *Queries) TurnByID(ctx context.Context, turnID string) (Turn, error) {
|
||||
row := q.db.QueryRow(ctx, turnByID, turnID)
|
||||
var i Turn
|
||||
err := row.Scan(
|
||||
&i.TurnID,
|
||||
&i.SessionID,
|
||||
&i.SourceID,
|
||||
&i.Status,
|
||||
&i.Attempt,
|
||||
&i.OwnerID,
|
||||
&i.InputMessageID,
|
||||
&i.AssistantMessageID,
|
||||
&i.PartialText,
|
||||
&i.ResponseText,
|
||||
&i.ToolCalls,
|
||||
&i.Error,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
@@ -128,6 +128,19 @@ type Mapping struct {
|
||||
UpdatedAt time.Time
|
||||
}
|
||||
|
||||
type Message struct {
|
||||
ID int64
|
||||
MessageID string
|
||||
SessionID string
|
||||
SourceID string
|
||||
Sender string
|
||||
SenderID string
|
||||
Role string
|
||||
Text string
|
||||
Ts int64
|
||||
Search interface{}
|
||||
}
|
||||
|
||||
type PlaybookDispatch struct {
|
||||
PlaybookID string
|
||||
PlaybookVersion string
|
||||
@@ -156,6 +169,19 @@ type ResourceHistory struct {
|
||||
At time.Time
|
||||
}
|
||||
|
||||
type Session struct {
|
||||
SessionID string
|
||||
Platform string
|
||||
BotUser string
|
||||
ChannelID string
|
||||
ThreadID string
|
||||
Title string
|
||||
ParentSessionID string
|
||||
BranchName string
|
||||
CreatedAt int64
|
||||
LastActiveAt int64
|
||||
}
|
||||
|
||||
type Task struct {
|
||||
ID int64
|
||||
Owner string
|
||||
@@ -212,3 +238,20 @@ type Transition struct {
|
||||
ToStatus string
|
||||
Detail string
|
||||
}
|
||||
|
||||
type Turn struct {
|
||||
TurnID string
|
||||
SessionID string
|
||||
SourceID string
|
||||
Status string
|
||||
Attempt int64
|
||||
OwnerID string
|
||||
InputMessageID string
|
||||
AssistantMessageID string
|
||||
PartialText string
|
||||
ResponseText string
|
||||
ToolCalls string
|
||||
Error string
|
||||
CreatedAt int64
|
||||
UpdatedAt int64
|
||||
}
|
||||
@@ -0,0 +1,174 @@
|
||||
-- Gateway conversation queries: sessions, messages, turns, full-text search.
|
||||
-- These back internal/gateway's PostgreSQL SessionStore.
|
||||
|
||||
-- ── Sessions ────────────────────────────────────────────────────────────────
|
||||
|
||||
-- name: SaveSession :exec
|
||||
INSERT INTO sessions (session_id, platform, bot_user, channel_id, thread_id,
|
||||
title, parent_session_id, branch_name, created_at, last_active_at)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
|
||||
ON CONFLICT (session_id) DO UPDATE SET
|
||||
platform = EXCLUDED.platform,
|
||||
bot_user = EXCLUDED.bot_user,
|
||||
channel_id = EXCLUDED.channel_id,
|
||||
thread_id = EXCLUDED.thread_id,
|
||||
title = EXCLUDED.title,
|
||||
parent_session_id = EXCLUDED.parent_session_id,
|
||||
branch_name = EXCLUDED.branch_name,
|
||||
created_at = EXCLUDED.created_at,
|
||||
last_active_at = EXCLUDED.last_active_at;
|
||||
|
||||
-- name: SessionByID :one
|
||||
SELECT session_id, platform, bot_user, channel_id, thread_id,
|
||||
title, parent_session_id, branch_name, created_at, last_active_at
|
||||
FROM sessions WHERE session_id = $1;
|
||||
|
||||
-- name: SessionsByChannel :many
|
||||
SELECT session_id, platform, bot_user, channel_id, thread_id,
|
||||
title, parent_session_id, branch_name, created_at, last_active_at
|
||||
FROM sessions
|
||||
WHERE platform = $1 AND channel_id = $2
|
||||
ORDER BY GREATEST(last_active_at, created_at) DESC;
|
||||
|
||||
-- name: ListSessions :many
|
||||
SELECT session_id, platform, bot_user, channel_id, thread_id,
|
||||
title, parent_session_id, branch_name, created_at, last_active_at
|
||||
FROM sessions
|
||||
ORDER BY GREATEST(last_active_at, created_at) DESC;
|
||||
|
||||
-- name: DeleteSessionMessages :exec
|
||||
DELETE FROM messages WHERE session_id = $1;
|
||||
|
||||
-- name: DeleteSessionTurns :exec
|
||||
DELETE FROM turns WHERE session_id = $1;
|
||||
|
||||
-- name: DeleteSession :exec
|
||||
DELETE FROM sessions WHERE session_id = $1;
|
||||
|
||||
-- name: TouchSession :exec
|
||||
UPDATE sessions SET last_active_at = $2 WHERE session_id = $1;
|
||||
|
||||
-- ── Messages ────────────────────────────────────────────────────────────────
|
||||
|
||||
-- name: LockSessionMessages :exec
|
||||
-- Serialises message appends per session so the monotonic clamp's MAX(ts)
|
||||
-- read cannot race a concurrent append of the same session. Transaction-scoped:
|
||||
-- released at commit/rollback, which is exactly the lifetime of the append.
|
||||
SELECT pg_advisory_xact_lock(hashtextextended('messages:' || $1::text, 0));
|
||||
|
||||
-- name: InsertMessageClamped :exec
|
||||
-- The append path's strictly-increasing timestamp. The clamp is computed in
|
||||
-- SQL, not in Go: the CASE assigns MAX(ts)+1 when the caller's timestamp is
|
||||
-- not ahead of the session's newest, else the caller's timestamp. A Go-side
|
||||
-- read-then-write would race two concurrent appends to the same session.
|
||||
INSERT INTO messages (message_id, session_id, source_id, sender, sender_id, role, text, ts)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, (
|
||||
SELECT CASE
|
||||
WHEN MAX(m.ts) IS NOT NULL AND MAX(m.ts) >= $8 THEN MAX(m.ts) + 1
|
||||
ELSE $8
|
||||
END
|
||||
FROM messages m WHERE m.session_id = $2
|
||||
))
|
||||
ON CONFLICT (session_id, message_id) DO NOTHING;
|
||||
|
||||
-- name: InsertMessageAt :exec
|
||||
-- Rewriting history places records deliberately, so no clamp applies.
|
||||
INSERT INTO messages (message_id, session_id, source_id, sender, sender_id, role, text, ts)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
|
||||
ON CONFLICT (session_id, message_id) DO NOTHING;
|
||||
|
||||
-- name: MessageID :one
|
||||
SELECT message_id FROM messages WHERE session_id = $1 AND message_id = $2;
|
||||
|
||||
-- name: RecentMessages :many
|
||||
SELECT m.message_id, m.source_id, m.sender, m.sender_id, m.role, m.text, m.ts,
|
||||
COALESCE(s.channel_id, '') AS channel_id, COALESCE(s.thread_id, '') AS thread_id
|
||||
FROM (
|
||||
SELECT msg.message_id, msg.source_id, msg.sender, msg.sender_id, msg.role, msg.text, msg.ts, msg.session_id
|
||||
FROM messages msg WHERE msg.session_id = $1 ORDER BY msg.ts DESC LIMIT $2
|
||||
) m
|
||||
LEFT JOIN sessions s ON s.session_id = m.session_id
|
||||
ORDER BY m.ts ASC;
|
||||
|
||||
-- name: DeleteRecentMessages :execrows
|
||||
DELETE FROM messages WHERE id IN (
|
||||
SELECT m.id FROM messages m WHERE m.session_id = $1 ORDER BY m.ts DESC LIMIT $2
|
||||
);
|
||||
|
||||
-- name: DeleteMessagesByID :exec
|
||||
DELETE FROM messages WHERE session_id = $1 AND message_id = ANY(sqlc.arg(ids)::text[]);
|
||||
|
||||
-- name: CountMessages :one
|
||||
SELECT COUNT(*) FROM messages WHERE session_id = $1;
|
||||
|
||||
-- name: SearchMessagesCount :one
|
||||
SELECT COUNT(*) FROM messages
|
||||
WHERE session_id = $1 AND search @@ plainto_tsquery('simple', $2);
|
||||
|
||||
-- name: SearchMessagesPage :many
|
||||
SELECT m.message_id, m.source_id, m.sender, m.sender_id, m.role, m.text, m.ts,
|
||||
COALESCE(s.channel_id, '') AS channel_id, COALESCE(s.thread_id, '') AS thread_id
|
||||
FROM messages m
|
||||
LEFT JOIN sessions s ON s.session_id = m.session_id
|
||||
WHERE m.session_id = $1 AND m.search @@ plainto_tsquery('simple', $2)
|
||||
ORDER BY ts_rank(m.search, plainto_tsquery('simple', $2)) DESC, m.ts DESC, m.id DESC
|
||||
LIMIT $3 OFFSET $4;
|
||||
|
||||
-- name: SourceMessageTs :one
|
||||
SELECT ts FROM messages
|
||||
WHERE session_id = $1 AND message_id IN ($2, $3)
|
||||
ORDER BY CASE WHEN message_id = $3 THEN 0 ELSE 1 END
|
||||
LIMIT 1;
|
||||
|
||||
-- name: NextReplyAfterTs :one
|
||||
SELECT sender, text FROM messages
|
||||
WHERE session_id = $1 AND ts > $2 ORDER BY ts ASC LIMIT 1;
|
||||
|
||||
-- ── Turns ───────────────────────────────────────────────────────────────────
|
||||
|
||||
-- name: InsertTurn :execrows
|
||||
INSERT INTO turns (turn_id, session_id, source_id, status, attempt, owner_id,
|
||||
input_message_id, assistant_message_id, partial_text,
|
||||
response_text, tool_calls, error, created_at, updated_at)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14)
|
||||
ON CONFLICT (turn_id) DO NOTHING;
|
||||
|
||||
-- name: TurnByID :one
|
||||
SELECT turn_id, session_id, source_id, status, attempt, owner_id,
|
||||
input_message_id, assistant_message_id, partial_text,
|
||||
response_text, tool_calls, error, created_at, updated_at
|
||||
FROM turns WHERE turn_id = $1;
|
||||
|
||||
-- name: TurnAttemptOwner :one
|
||||
SELECT attempt, owner_id FROM turns WHERE turn_id = $1;
|
||||
|
||||
-- name: FinalizeTurn :execrows
|
||||
UPDATE turns SET status = $2, error = $3, updated_at = $4
|
||||
WHERE turn_id = $1 AND attempt = $5 AND owner_id = $6;
|
||||
|
||||
-- name: RetryTurn :execrows
|
||||
UPDATE turns SET status = $2, attempt = $3, owner_id = $4, error = $5, updated_at = $6
|
||||
WHERE turn_id = $1 AND attempt = $7 AND owner_id = $8;
|
||||
|
||||
-- name: RecoverTurns :exec
|
||||
UPDATE turns SET status = 'failed', error = 'recovered after process restart', updated_at = $1
|
||||
WHERE status IN ('accepted', 'running', 'partial') AND owner_id <> $2;
|
||||
|
||||
-- name: SaveTurn :execrows
|
||||
UPDATE turns SET
|
||||
session_id = $2, source_id = $3, status = $4, attempt = $5, owner_id = $6,
|
||||
input_message_id = $7, assistant_message_id = $8, partial_text = $9,
|
||||
response_text = $10, tool_calls = $11, error = $12, created_at = $13, updated_at = $14
|
||||
WHERE turn_id = $1 AND attempt = $5 AND owner_id = $6;
|
||||
|
||||
-- name: RecentTurns :many
|
||||
SELECT turn_id, session_id, source_id, status, attempt, owner_id,
|
||||
input_message_id, assistant_message_id, partial_text,
|
||||
response_text, tool_calls, error, created_at, updated_at
|
||||
FROM turns WHERE session_id = $1 ORDER BY created_at DESC LIMIT $2;
|
||||
|
||||
-- name: ListRecoverableTurns :many
|
||||
SELECT turn_id, session_id, source_id, status, attempt, owner_id,
|
||||
input_message_id, assistant_message_id, partial_text,
|
||||
response_text, tool_calls, error, created_at, updated_at
|
||||
FROM turns WHERE status IN ('accepted', 'running', 'partial') ORDER BY updated_at ASC;
|
||||
Reference in new issue
Block a user