Files
archie/modules/governance/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

139 lines
5.4 KiB
Go

// Package store holds the governance module's SQL: the organisation's model
// roles.
package store
import (
"context"
"database/sql"
"errors"
"fmt"
"github.com/jmoiron/sqlx"
)
// ModelRole is the model an organisation chose for one role.
type ModelRole struct {
OrgID string `db:"org_id"`
Role string `db:"role"`
Class string `db:"class"`
BaseURL string `db:"base_url"`
Model string `db:"model"`
// APIKeyCredential names the credential whose token authenticates the
// model provider, or empty when it needs no key.
APIKeyCredential string `db:"api_key_credential"`
}
// Store is the governance 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} }
// ModelRole returns the organisation's model for role; ok is false when none
// was chosen.
func (s *Store) ModelRole(ctx context.Context, orgID, role string) (ModelRole, bool, error) {
var out ModelRole
err := s.db.GetContext(ctx, &out, `
SELECT org_id, role, class, base_url, model, api_key_credential FROM governance.model_roles
WHERE org_id = $1 AND role = $2`, orgID, role)
if errors.Is(err, sql.ErrNoRows) {
return ModelRole{}, false, nil
}
if err != nil {
return ModelRole{}, false, fmt.Errorf("read model role %q: %w", role, err)
}
return out, true, nil
}
func (s *Store) AgentModelRoles(ctx context.Context, orgID string) ([]ModelRole, error) {
var out []ModelRole
if err := s.db.SelectContext(ctx, &out, `
SELECT org_id, role, class, base_url, model, api_key_credential
FROM governance.model_roles
WHERE org_id = $1 AND (role = 'agent' OR role LIKE 'agent:%')`, orgID); err != nil {
return nil, fmt.Errorf("list agent models: %w", err)
}
return out, nil
}
// PutModelRole records the organisation's model for a role.
func (s *Store) PutModelRole(ctx context.Context, m ModelRole) error {
if _, err := s.db.NamedExecContext(ctx, `
INSERT INTO governance.model_roles (org_id, role, class, base_url, model, api_key_credential)
VALUES (:org_id, :role, :class, :base_url, :model, :api_key_credential)
ON CONFLICT (org_id, role) DO UPDATE
SET class = EXCLUDED.class, base_url = EXCLUDED.base_url, model = EXCLUDED.model,
api_key_credential = EXCLUDED.api_key_credential, updated_at = now()`, m); err != nil {
return fmt.Errorf("put model role %q: %w", m.Role, err)
}
return nil
}
// Settings is an organisation's settings as stored. A duration is kept in
// milliseconds: Postgres has no duration type that scans into one.
type Settings struct {
OrgID string `db:"org_id"`
// ApprovalDeadlineMS is how long an approval request waits for an answer.
ApprovalDeadlineMS int64 `db:"approval_deadline_ms"`
// RaiseAfter is how many approvals in a row earn a proposal to raise an
// action class.
RaiseAfter int `db:"raise_after"`
// Frozen holds every action that changes something at propose and wait.
Frozen bool `db:"frozen"`
// RecurrenceThreshold is how many incidents with one cause on one target
// it takes to propose a root-cause fix.
RecurrenceThreshold int `db:"recurrence_threshold"`
// DefaultOwner is the id of the person who answers for what has no other
// owner, or empty.
DefaultOwner string `db:"default_owner"`
// Escalation is a JSON array of person ids, in the order an unanswered
// request moves through them.
Escalation string `db:"escalation"`
// AttemptsPerRun is how many steps an agent may take in one run.
AttemptsPerRun int `db:"attempts_per_run"`
// Timing is a JSON object: business hours and blackouts.
Timing string `db:"timing"`
// ActionsPerRun and ActionsPerDay are the action budgets.
ActionsPerRun int `db:"actions_per_run"`
ActionsPerDay int `db:"actions_per_day"`
}
// Settings returns the organisation's settings; ok is false when none were
// saved.
func (s *Store) Settings(ctx context.Context, orgID string) (Settings, bool, error) {
var out Settings
err := s.db.GetContext(ctx, &out, `
SELECT org_id, approval_deadline_ms, raise_after, frozen, recurrence_threshold,
default_owner, escalation::text AS escalation, attempts_per_run,
timing::text AS timing, actions_per_run, actions_per_day
FROM governance.settings WHERE org_id = $1`, orgID)
if errors.Is(err, sql.ErrNoRows) {
return Settings{}, false, nil
}
if err != nil {
return Settings{}, false, fmt.Errorf("read settings: %w", err)
}
return out, true, nil
}
// PutSettings records the organisation's settings.
func (s *Store) PutSettings(ctx context.Context, set Settings) error {
if _, err := s.db.NamedExecContext(ctx, `
INSERT INTO governance.settings (org_id, approval_deadline_ms, raise_after, frozen, recurrence_threshold,
default_owner, escalation, attempts_per_run, timing, actions_per_run, actions_per_day)
VALUES (:org_id, :approval_deadline_ms, :raise_after, :frozen, :recurrence_threshold,
:default_owner, CAST(:escalation AS jsonb), :attempts_per_run,
CAST(:timing AS jsonb), :actions_per_run, :actions_per_day)
ON CONFLICT (org_id) DO UPDATE
SET approval_deadline_ms = EXCLUDED.approval_deadline_ms, raise_after = EXCLUDED.raise_after,
frozen = EXCLUDED.frozen, recurrence_threshold = EXCLUDED.recurrence_threshold,
default_owner = EXCLUDED.default_owner, escalation = EXCLUDED.escalation,
attempts_per_run = EXCLUDED.attempts_per_run, timing = EXCLUDED.timing,
actions_per_run = EXCLUDED.actions_per_run, actions_per_day = EXCLUDED.actions_per_day, updated_at = now()`, set); err != nil {
return fmt.Errorf("put settings: %w", err)
}
return nil
}