Files
archie/modules/memory/internal/store/store.go
T
samandCopilot 64dfad6d53 feat(runs): add typed results and model aliases (#2043)
Also fix repository-free message.send validation (#2046) and add a single-binary development smoke path (#1977).

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-11-18 23:47:35 +11:00

109 lines
4.0 KiB
Go

// Package store persists incident history and proposals for root-cause fixes.
package store
import (
"context"
"fmt"
"time"
"github.com/google/uuid"
"github.com/jmoiron/sqlx"
)
// Incident is the stored record of a run's investigation.
type Incident struct {
ID uuid.UUID `db:"id"`
RunID uuid.UUID `db:"run_id"`
TargetKind string `db:"target_kind"`
TargetID string `db:"target_id"`
Cause string `db:"cause"`
Summary string `db:"summary"`
Event []byte `db:"event"`
Actions []byte `db:"actions"`
At time.Time `db:"at"`
}
// Proposal is a request to fix a recurring cause on a target.
type Proposal struct {
ID uuid.UUID `db:"id"`
TargetKind string `db:"target_kind"`
TargetID string `db:"target_id"`
Cause string `db:"cause"`
Incidents int `db:"incidents"`
Body string `db:"body"`
}
// Store is the memory 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} }
// Upsert inserts an incident or updates the target, cause and summary for
// the same organisation and run. It preserves the original event and timestamp.
func (s *Store) Upsert(ctx context.Context, orgID string, in Incident) error {
if _, err := s.db.ExecContext(ctx, `
INSERT INTO memory.incidents (id, org_id, run_id, target_kind, target_id, cause, summary, event)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
ON CONFLICT (org_id, run_id) DO UPDATE
SET target_kind = EXCLUDED.target_kind, target_id = EXCLUDED.target_id,
cause = EXCLUDED.cause, summary = EXCLUDED.summary`,
in.ID, orgID, in.RunID, in.TargetKind, in.TargetID, in.Cause, in.Summary, in.Event); err != nil {
return fmt.Errorf("record incident: %w", err)
}
return nil
}
// SetActions replaces the recorded action outcomes for a run's incident.
func (s *Store) SetActions(ctx context.Context, orgID string, runID uuid.UUID, actions []byte) error {
if _, err := s.db.ExecContext(ctx, `
UPDATE memory.incidents SET actions = $3 WHERE org_id = $1 AND run_id = $2`,
orgID, runID, actions); err != nil {
return fmt.Errorf("record incident actions: %w", err)
}
return nil
}
// Incident returns a run's incident; ok is false when it has none.
func (s *Store) Incident(ctx context.Context, orgID string, runID uuid.UUID) (Incident, bool, error) {
var out []Incident
if err := s.db.SelectContext(ctx, &out, `
SELECT id, run_id, target_kind, target_id, cause, summary, event, actions, at
FROM memory.incidents WHERE org_id = $1 AND run_id = $2`, orgID, runID); err != nil {
return Incident{}, false, fmt.Errorf("read incident: %w", err)
}
if len(out) == 0 {
return Incident{}, false, nil
}
return out[0], true, nil
}
// ByTarget returns a target's incidents, newest first, up to limit.
func (s *Store) ByTarget(ctx context.Context, orgID, kind, id string, limit int) ([]Incident, error) {
var out []Incident
if err := s.db.SelectContext(ctx, &out, `
SELECT id, run_id, target_kind, target_id, cause, summary, event, actions, at
FROM memory.incidents WHERE org_id = $1 AND target_kind = $2 AND target_id = $3
ORDER BY at DESC LIMIT $4`, orgID, kind, id, limit); err != nil {
return nil, fmt.Errorf("list incidents: %w", err)
}
return out, nil
}
// Propose stores one proposal per organisation, target and cause. If one
// already exists, it updates the incident count and returns the original text.
func (s *Store) Propose(ctx context.Context, orgID string, p Proposal) (Proposal, error) {
var out Proposal
if err := s.db.GetContext(ctx, &out, `
INSERT INTO memory.proposals (id, org_id, target_kind, target_id, cause, incidents, body)
VALUES ($1, $2, $3, $4, $5, $6, $7)
ON CONFLICT (org_id, target_kind, target_id, cause) DO UPDATE SET incidents = EXCLUDED.incidents
RETURNING id, target_kind, target_id, cause, incidents, body`,
p.ID, orgID, p.TargetKind, p.TargetID, p.Cause, p.Incidents, p.Body); err != nil {
return Proposal{}, fmt.Errorf("record proposal: %w", err)
}
return out, nil
}