An action judgement holds at propose and wait is no longer a dead end. The new approvals module owns the request: it binds to the decision and the action's digest, so an answer covers the action the person was shown and nothing else, and it settles once. The answer wakes the run, which judges the action again and runs it only as the approval allows: a refusal still refuses, an altered input runs nothing, and a judgement that came back worse runs nothing on that approval. The composer carries the request as a message with the request id and its deadline, and its answer is a message too, so the trail shows what was asked and what was answered. A request nobody answers inside its deadline is settled as expired and nothing runs. How long a request waits is an organisation setting, edited in the dashboard and stored in milliseconds, because a Postgres interval scans as text. A run pins the deadline it started with, like its model. An agent that proposes the same action twice is asked about it once, and the notification says when the investigation proposed nothing at all.
93 lines
2.9 KiB
Go
93 lines
2.9 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"`
|
|
}
|
|
|
|
// 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 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
|
|
}
|
|
|
|
// 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)
|
|
VALUES (:org_id, :role, :class, :base_url, :model)
|
|
ON CONFLICT (org_id, role) DO UPDATE
|
|
SET class = EXCLUDED.class, base_url = EXCLUDED.base_url, model = EXCLUDED.model, 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"`
|
|
}
|
|
|
|
// 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 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)
|
|
VALUES (:org_id, :approval_deadline_ms)
|
|
ON CONFLICT (org_id) DO UPDATE
|
|
SET approval_deadline_ms = EXCLUDED.approval_deadline_ms, updated_at = now()`, set); err != nil {
|
|
return fmt.Errorf("put settings: %w", err)
|
|
}
|
|
return nil
|
|
}
|