Files
archie/modules/chat/internal/store/store.go
T
samandCopilot cbfcd43018 feat(chat): add the chat module to the archie binary (#2018)
Conversations are the chat module's own tables. A turn is stored before it
is acknowledged, runs after the turns before it in its session, and records
everything it streams, so a reader replays it from a cursor. The agent uses
the configured agent model, the same connector tools as a run and the
dashboard's own reads; every action is judged, and one judgement holds is
put to people through approvals and run once when approved. A restart fails
the turns it interrupted visibly and never restarts a started action.

Conversation memory is a set of notes in the memory module. The chat
prompts are copied from internal/prompts (chat-system.tmpl, soul.md,
session-title.md) into internal/platform/prompts while the gateway still
reads them; the tool parameter redaction is copied from
internal/domain/messaging/tool_parameters.go.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-11-19 19:40:44 +11:00

395 lines
14 KiB
Go

// Package store holds the chat module's SQL: conversations, their turns, what
// each turn streamed, and the actions its agent proposed.
package store
import (
"context"
"database/sql"
"encoding/json/jsontext"
"errors"
"fmt"
"time"
"github.com/google/uuid"
"github.com/jmoiron/sqlx"
)
// ErrNotFound means there is no such session, turn or action.
var ErrNotFound = errors.New("not found")
// Session is one conversation.
type Session struct {
ID uuid.UUID `db:"id"`
Title string `db:"title"`
CreatedAt time.Time `db:"created_at"`
UpdatedAt time.Time `db:"updated_at"`
}
// Turn is one operator message and the reply to it.
type Turn struct {
Seq int64 `db:"seq"`
ID uuid.UUID `db:"id"`
SessionID uuid.UUID `db:"session_id"`
State string `db:"state"`
Text string `db:"text"`
Page string `db:"page"`
PageLabel string `db:"page_label"`
Reply string `db:"reply"`
Reasoning string `db:"reasoning"`
Error string `db:"error"`
Cancel bool `db:"cancel"`
CreatedAt time.Time `db:"created_at"`
StartedAt sql.NullTime `db:"started_at"`
FinishedAt sql.NullTime `db:"finished_at"`
}
// Event is one thing a turn streamed.
type Event struct {
Seq int64 `db:"seq"`
TurnID uuid.UUID `db:"turn_id"`
Kind string `db:"kind"`
Data jsontext.Value `db:"data"`
CreatedAt time.Time `db:"created_at"`
}
// Action is an action judgement held for approval.
type Action struct {
ID uuid.UUID `db:"id"`
TurnID uuid.UUID `db:"turn_id"`
Key string `db:"key"`
Decision jsontext.Value `db:"decision"`
RequestID uuid.UUID `db:"request_id"`
Path jsontext.Value `db:"path"`
Step int `db:"step"`
StepDue time.Time `db:"step_due"`
State string `db:"state"`
Answer []byte `db:"answer"`
Result string `db:"result"`
}
// Store is the chat module's Postgres store.
type Store struct {
db *sqlx.DB
}
// New returns a store on db.
func New(db *sqlx.DB) *Store { return &Store{db: db} }
const turnColumns = `seq, id, session_id, state, text, page, page_label, reply, reasoning, error, cancel, created_at, started_at, finished_at`
// CreateSession stores a new conversation.
func (s *Store) CreateSession(ctx context.Context, orgID string, id uuid.UUID) (Session, error) {
var out Session
if err := s.db.GetContext(ctx, &out, `
INSERT INTO chat.sessions (id, org_id) VALUES ($1, $2)
RETURNING id, title, created_at, updated_at`, id, orgID); err != nil {
return Session{}, fmt.Errorf("create session: %w", err)
}
return out, nil
}
// Sessions lists the conversations, most recently active first.
func (s *Store) Sessions(ctx context.Context, orgID string, limit int) ([]Session, error) {
var out []Session
if err := s.db.SelectContext(ctx, &out, `
SELECT id, title, created_at, updated_at FROM chat.sessions
WHERE org_id = $1 ORDER BY updated_at DESC LIMIT $2`, orgID, limit); err != nil {
return nil, fmt.Errorf("list sessions: %w", err)
}
return out, nil
}
// Session returns one conversation.
func (s *Store) Session(ctx context.Context, orgID string, id uuid.UUID) (Session, error) {
var out Session
err := s.db.GetContext(ctx, &out, `
SELECT id, title, created_at, updated_at FROM chat.sessions WHERE org_id = $1 AND id = $2`, orgID, id)
if errors.Is(err, sql.ErrNoRows) {
return Session{}, ErrNotFound
}
if err != nil {
return Session{}, fmt.Errorf("read session: %w", err)
}
return out, nil
}
// Rename sets a conversation's title.
func (s *Store) Rename(ctx context.Context, orgID string, id uuid.UUID, title string) error {
res, err := s.db.ExecContext(ctx, `UPDATE chat.sessions SET title = $3 WHERE org_id = $1 AND id = $2`, orgID, id, title)
return affected(res, err, "rename session")
}
// TitleIfUntitled sets a title only on a conversation that has none, so a
// generated title never replaces one the operator chose.
func (s *Store) TitleIfUntitled(ctx context.Context, orgID string, id uuid.UUID, title string) error {
if _, err := s.db.ExecContext(ctx, `
UPDATE chat.sessions SET title = $3 WHERE org_id = $1 AND id = $2 AND title = ''`, orgID, id, title); err != nil {
return fmt.Errorf("title session: %w", err)
}
return nil
}
// DeleteSession removes a conversation with its turns.
func (s *Store) DeleteSession(ctx context.Context, orgID string, id uuid.UUID) error {
res, err := s.db.ExecContext(ctx, `DELETE FROM chat.sessions WHERE org_id = $1 AND id = $2`, orgID, id)
return affected(res, err, "delete session")
}
// AddTurn stores a turn as queued and marks its conversation active.
func (s *Store) AddTurn(ctx context.Context, orgID string, t Turn) (Turn, error) {
tx, err := s.db.BeginTxx(ctx, nil)
if err != nil {
return Turn{}, fmt.Errorf("begin: %w", err)
}
defer func() { _ = tx.Rollback() }()
res, err := tx.ExecContext(ctx, `UPDATE chat.sessions SET updated_at = now() WHERE org_id = $1 AND id = $2`, orgID, t.SessionID)
if err := affected(res, err, "touch session"); err != nil {
return Turn{}, err
}
var out Turn
if err := tx.GetContext(ctx, &out, `
INSERT INTO chat.turns (id, org_id, session_id, state, text, page, page_label)
VALUES ($1, $2, $3, 'queued', $4, $5, $6)
RETURNING `+turnColumns, t.ID, orgID, t.SessionID, t.Text, t.Page, t.PageLabel); err != nil {
return Turn{}, fmt.Errorf("add turn: %w", err)
}
if err := tx.Commit(); err != nil {
return Turn{}, fmt.Errorf("commit turn: %w", err)
}
return out, nil
}
// Turns returns a conversation's turns, oldest first.
func (s *Store) Turns(ctx context.Context, orgID string, sessionID uuid.UUID) ([]Turn, error) {
var out []Turn
if err := s.db.SelectContext(ctx, &out, `
SELECT `+turnColumns+` FROM chat.turns WHERE org_id = $1 AND session_id = $2 ORDER BY seq`, orgID, sessionID); err != nil {
return nil, fmt.Errorf("list turns: %w", err)
}
return out, nil
}
// Turn returns one turn.
func (s *Store) Turn(ctx context.Context, orgID string, id uuid.UUID) (Turn, error) {
var out Turn
err := s.db.GetContext(ctx, &out, `SELECT `+turnColumns+` FROM chat.turns WHERE org_id = $1 AND id = $2`, orgID, id)
if errors.Is(err, sql.ErrNoRows) {
return Turn{}, ErrNotFound
}
if err != nil {
return Turn{}, fmt.Errorf("read turn: %w", err)
}
return out, nil
}
// IsTurn reports whether id is a chat turn.
func (s *Store) IsTurn(ctx context.Context, id uuid.UUID) (bool, error) {
var ok bool
if err := s.db.GetContext(ctx, &ok, `SELECT EXISTS (SELECT 1 FROM chat.turns WHERE id = $1)`, id); err != nil {
return false, fmt.Errorf("find turn: %w", err)
}
return ok, nil
}
// Claim starts the oldest queued turn of a conversation with no turn running,
// so a conversation's turns run one at a time in the order they were sent.
func (s *Store) Claim(ctx context.Context) (Turn, bool, error) {
var out Turn
err := s.db.GetContext(ctx, &out, `
UPDATE chat.turns SET state = 'running', started_at = now()
WHERE id = (
SELECT t.id FROM chat.turns t
WHERE t.state = 'queued' AND NOT t.cancel AND NOT EXISTS (
SELECT 1 FROM chat.turns r WHERE r.session_id = t.session_id AND r.state = 'running')
AND NOT EXISTS (
SELECT 1 FROM chat.turns q WHERE q.session_id = t.session_id AND q.state = 'queued' AND q.seq < t.seq)
ORDER BY t.seq LIMIT 1 FOR UPDATE SKIP LOCKED)
RETURNING `+turnColumns)
if errors.Is(err, sql.ErrNoRows) {
return Turn{}, false, nil
}
if err != nil {
return Turn{}, false, fmt.Errorf("claim turn: %w", err)
}
return out, true, nil
}
// Finish ends a running turn with its outcome.
func (s *Store) Finish(ctx context.Context, id uuid.UUID, state, reply, reasoning, failure string) error {
if _, err := s.db.ExecContext(ctx, `
UPDATE chat.turns SET state = $2, reply = $3, reasoning = $4, error = $5, finished_at = now()
WHERE id = $1 AND state IN ('queued', 'running')`, id, state, reply, reasoning, failure); err != nil {
return fmt.Errorf("finish turn: %w", err)
}
return nil
}
// RequestCancel asks a turn to stop. A queued turn is cancelled at once and
// reported true; a running one stops when its worker sees the request.
func (s *Store) RequestCancel(ctx context.Context, orgID string, id uuid.UUID) (bool, error) {
var state string
err := s.db.GetContext(ctx, &state, `
UPDATE chat.turns SET cancel = true,
state = CASE WHEN state = 'queued' THEN 'cancelled' ELSE state END,
finished_at = CASE WHEN state = 'queued' THEN now() ELSE finished_at END
WHERE org_id = $1 AND id = $2 RETURNING state`, orgID, id)
if errors.Is(err, sql.ErrNoRows) {
return false, ErrNotFound
}
if err != nil {
return false, fmt.Errorf("cancel turn: %w", err)
}
return state == "cancelled", nil
}
// Interrupted fails every turn left running by a process that stopped, and
// returns them.
func (s *Store) Interrupted(ctx context.Context, reason string) ([]uuid.UUID, error) {
var out []uuid.UUID
if err := s.db.SelectContext(ctx, &out, `
UPDATE chat.turns SET state = 'failed', error = $1, finished_at = now()
WHERE state = 'running' RETURNING id`, reason); err != nil {
return nil, fmt.Errorf("fail interrupted turns: %w", err)
}
return out, nil
}
// History returns a conversation's finished turns before the turn seq, oldest
// first, at most limit of them.
func (s *Store) History(ctx context.Context, sessionID uuid.UUID, before int64, limit int) ([]Turn, error) {
var out []Turn
if err := s.db.SelectContext(ctx, &out, `
SELECT * FROM (
SELECT `+turnColumns+` FROM chat.turns
WHERE session_id = $1 AND seq < $2 AND state IN ('succeeded', 'failed', 'cancelled')
ORDER BY seq DESC LIMIT $3) recent
ORDER BY seq`, sessionID, before, limit); err != nil {
return nil, fmt.Errorf("read history: %w", err)
}
return out, nil
}
// AddEvent records what a turn streamed and returns its position.
func (s *Store) AddEvent(ctx context.Context, turnID uuid.UUID, kind string, data jsontext.Value) (int64, error) {
var seq int64
if err := s.db.GetContext(ctx, &seq, `
INSERT INTO chat.events (turn_id, kind, data) VALUES ($1, $2, $3) RETURNING seq`, turnID, kind, data); err != nil {
return 0, fmt.Errorf("record event: %w", err)
}
return seq, nil
}
// Events returns a turn's events after seq, oldest first.
func (s *Store) Events(ctx context.Context, turnID uuid.UUID, after int64) ([]Event, error) {
var out []Event
if err := s.db.SelectContext(ctx, &out, `
SELECT seq, turn_id, kind, data, created_at FROM chat.events
WHERE turn_id = $1 AND seq > $2 ORDER BY seq`, turnID, after); err != nil {
return nil, fmt.Errorf("list events: %w", err)
}
return out, nil
}
// EventsOf returns the events of the kinds given for a conversation's turns,
// oldest first.
func (s *Store) EventsOf(ctx context.Context, sessionID uuid.UUID, kinds []string) ([]Event, error) {
query, args, err := sqlx.In(`
SELECT e.seq, e.turn_id, e.kind, e.data, e.created_at FROM chat.events e
JOIN chat.turns t ON t.id = e.turn_id
WHERE t.session_id = ? AND e.kind IN (?) ORDER BY e.seq`, sessionID, kinds)
if err != nil {
return nil, err
}
var out []Event
if err := s.db.SelectContext(ctx, &out, s.db.Rebind(query), args...); err != nil {
return nil, fmt.Errorf("list session events: %w", err)
}
return out, nil
}
// PutAction stores an action held for approval unless the turn already has
// one under its key, and returns the stored one either way.
func (s *Store) PutAction(ctx context.Context, orgID string, a Action) (Action, error) {
if _, err := s.db.ExecContext(ctx, `
INSERT INTO chat.actions (id, org_id, turn_id, key, decision, request_id, path, step_due, state)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, 'waiting')
ON CONFLICT (turn_id, key) DO NOTHING`,
a.ID, orgID, a.TurnID, a.Key, a.Decision, a.RequestID, a.Path, a.StepDue); err != nil {
return Action{}, fmt.Errorf("record held action: %w", err)
}
return s.actionBy(ctx, `turn_id = $1 AND key = $2`, a.TurnID, a.Key)
}
const actionColumns = `id, turn_id, key, decision, request_id, path, step, step_due, state, answer, result`
func (s *Store) actionBy(ctx context.Context, where string, args ...any) (Action, error) {
var out Action
err := s.db.GetContext(ctx, &out, `SELECT `+actionColumns+` FROM chat.actions WHERE `+where, args...)
if errors.Is(err, sql.ErrNoRows) {
return Action{}, ErrNotFound
}
if err != nil {
return Action{}, fmt.Errorf("read action: %w", err)
}
return out, nil
}
// Answer records the answer to a waiting action under its turn and key, and
// reports whether it was waiting.
func (s *Store) Answer(ctx context.Context, turnID uuid.UUID, key, state string, answer []byte) (bool, error) {
res, err := s.db.ExecContext(ctx, `
UPDATE chat.actions SET state = $3, answer = $4 WHERE turn_id = $1 AND key = $2 AND state = 'waiting'`,
turnID, key, state, answer)
if err != nil {
return false, fmt.Errorf("answer action: %w", err)
}
n, err := res.RowsAffected()
return n > 0, err
}
// Open returns the actions still to settle: waiting for an answer, approved
// and not yet started, or started.
func (s *Store) Open(ctx context.Context) ([]Action, error) {
var out []Action
if err := s.db.SelectContext(ctx, &out, `
SELECT `+actionColumns+` FROM chat.actions WHERE state IN ('waiting', 'approved', 'running') ORDER BY created_at`); err != nil {
return nil, fmt.Errorf("list open actions: %w", err)
}
return out, nil
}
// Move changes an action from one state to another and reports whether it was
// in the first.
func (s *Store) Move(ctx context.Context, id uuid.UUID, from, to, result string) (bool, error) {
res, err := s.db.ExecContext(ctx, `
UPDATE chat.actions SET state = $3, result = $4 WHERE id = $1 AND state = $2`, id, from, to, result)
if err != nil {
return false, fmt.Errorf("move action: %w", err)
}
n, err := res.RowsAffected()
return n > 0, err
}
// Escalated records that a waiting action moved to the next step of its path.
func (s *Store) Escalated(ctx context.Context, id uuid.UUID, step int, due time.Time) error {
if _, err := s.db.ExecContext(ctx, `
UPDATE chat.actions SET step = $2, step_due = $3 WHERE id = $1 AND state = 'waiting'`, id, step, due); err != nil {
return fmt.Errorf("escalate action: %w", err)
}
return nil
}
func affected(res sql.Result, err error, what string) error {
if err != nil {
return fmt.Errorf("%s: %w", what, err)
}
n, err := res.RowsAffected()
if err != nil {
return fmt.Errorf("%s: %w", what, err)
}
if n == 0 {
return ErrNotFound
}
return nil
}