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>
This commit is contained in:
samandCopilot committed 2026-11-18 23:47:35 +11:00
1 parent 6a3bf5a71a
commit 64dfad6d53
55 files changed
+1162 -517

No files matched your search

+8 -9
View File
@@ -1,5 +1,4 @@
// Package store holds the memory module's SQL: incidents and the proposals
// raised from them.
// Package store persists incident history and proposals for root-cause fixes.
package store
import (
@@ -11,7 +10,7 @@ import (
"github.com/jmoiron/sqlx"
)
// Incident is one investigation of one target as stored.
// Incident is the stored record of a run's investigation.
type Incident struct {
ID uuid.UUID `db:"id"`
RunID uuid.UUID `db:"run_id"`
@@ -24,7 +23,7 @@ type Incident struct {
At time.Time `db:"at"`
}
// Proposal is a root-cause fix asked for on a target.
// 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"`
@@ -42,8 +41,8 @@ type Store struct {
// New returns a store on db.
func New(db *sqlx.DB) *Store { return &Store{db: db} }
// Upsert records the incident of a run, replacing what an earlier attempt of the
// same run recorded.
// 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)
@@ -57,7 +56,7 @@ func (s *Store) Upsert(ctx context.Context, orgID string, in Incident) error {
return nil
}
// SetActions records what was done in a run's incident.
// 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`,
@@ -93,8 +92,8 @@ func (s *Store) ByTarget(ctx context.Context, orgID, kind, id string, limit int)
return out, nil
}
// Propose records p unless the target and cause already have one, and returns
// the one that stands.
// 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, `
+25 -28
View File
@@ -1,7 +1,6 @@
// Package memory is what Archie remembers about a target: each incident
// investigated on it, the cause found, and what was done about it. A cause that
// keeps coming back on a target, whatever became of each attempt to handle it,
// raises a proposal for a root-cause fix.
// Package memory records investigated incidents by target, including their
// causes and action outcomes. When a cause reaches the recurrence threshold
// for a target, the module proposes a root-cause fix regardless of action outcomes.
package memory
import (
@@ -28,17 +27,17 @@ const maxHistory = 200
// MaxRecurrence is the largest recurrence threshold the history can confirm.
const MaxRecurrence = 100
// Target is what an incident happened to: a kind and an id, such as host web-1.
// Target identifies the affected resource by kind and ID, such as host web-1.
type Target struct {
Kind string `json:"kind"`
ID string `json:"id"`
}
// Action is something done during an incident and how it ended.
// Action records an action considered during an incident and its outcome.
type Action struct {
Action string `json:"action"`
Target string `json:"target"`
// State is what became of it: ran, refused, or the approval's state.
// State records whether the action ran, was refused or has an approval status.
State string `json:"state"`
Detail string `json:"detail,omitzero"`
}
@@ -51,7 +50,7 @@ type Incident struct {
Actions []Action `json:"actions"`
}
// Line is the incident as one entry of a list an agent reads.
// Line formats the incident as one entry in the history shown to the agent.
func (i Incident) Line() string {
line := fmt.Sprintf("- %s, cause %q: %s", i.At.Format("2006-01-02"), i.Cause, i.Summary)
if len(i.Actions) == 0 {
@@ -68,17 +67,16 @@ func (i Incident) Line() string {
return line + " Actions: " + strings.Join(parts, "; ") + "."
}
// Review is what recording a run's actions found about its cause.
// Review reports how often the cause occurred and any proposal for a fix.
type Review struct {
// Repeats is how many incidents on the target have this cause, this one
// included.
// Repeats counts matching incidents in the target's recent history,
// including the current incident if it is in that history.
Repeats int
// Proposal is the root-cause fix asked for, empty until the cause has
// recurred enough.
// Proposal is the text of the request for a root-cause fix. It is empty
// below the recurrence threshold.
Proposal string
}
// ledger is where incidents and proposals are kept.
type ledger interface {
Upsert(ctx context.Context, orgID string, in store.Incident) error
SetActions(ctx context.Context, orgID string, runID uuid.UUID, actions []byte) error
@@ -87,7 +85,7 @@ type ledger interface {
Propose(ctx context.Context, orgID string, p store.Proposal) (store.Proposal, error)
}
// Module remembers one organisation's incidents.
// Module manages incident history and proposals for one organisation.
type Module struct {
orgID string
ledger ledger
@@ -100,14 +98,14 @@ func NewModule(db *sqlx.DB, orgID string) *Module {
var unsafeCause = regexp.MustCompile(`[^a-z0-9]+`)
// Cause is a cause as it is compared: lower case words joined by hyphens, so
// "Disk pressure" and "disk-pressure" are one cause.
// Cause normalises labels for comparison: "Disk pressure" and "disk-pressure"
// both produce "disk-pressure".
func Cause(s string) string {
return strings.Trim(unsafeCause.ReplaceAllString(strings.ToLower(s), "-"), "-")
}
// Record remembers what a run's investigation found: the event, the target it
// concerned and the cause. Recording again replaces the earlier attempt's.
// Record saves the incident for a run. Recording it again updates the target,
// cause and summary without replacing the original event.
func (m *Module) Record(ctx context.Context, runID uuid.UUID, event jsontext.Value, t Target, cause, summary string) error {
t = Target{Kind: strings.TrimSpace(t.Kind), ID: strings.TrimSpace(t.ID)}
cause = Cause(cause)
@@ -123,7 +121,7 @@ func (m *Module) Record(ctx context.Context, runID uuid.UUID, event jsontext.Val
})
}
// Past returns the incidents recorded on t before the run's own, newest first.
// Past returns recent incidents for t, newest first, excluding the current run.
func (m *Module) Past(ctx context.Context, runID uuid.UUID, t Target) ([]Incident, error) {
rows, err := m.ledger.ByTarget(ctx, m.orgID, strings.TrimSpace(t.Kind), strings.TrimSpace(t.ID), maxHistory)
if err != nil {
@@ -143,10 +141,9 @@ func (m *Module) Past(ctx context.Context, runID uuid.UUID, t Target) ([]Inciden
return out, nil
}
// Finish remembers what was done in a run's incident and checks whether its
// cause has come back enough to ask for a root-cause fix: that is when
// threshold incidents on the target share the cause, whatever their outcomes.
// A run that recorded no incident has nothing to finish.
// Finish records action outcomes. It requests a root-cause fix if at least
// threshold incidents in the target's recent history share the current cause,
// regardless of their action outcomes. A run with no recorded incident is ignored.
func (m *Module) Finish(ctx context.Context, runID uuid.UUID, actions []Action, threshold int) (Review, error) {
current, ok, err := m.ledger.Incident(ctx, m.orgID, runID)
if err != nil || !ok {
@@ -194,8 +191,8 @@ func incident(r store.Incident) (Incident, error) {
return out, nil
}
// proposalBody asks for the cause to be fixed rather than handled again. same
// is newest first.
// proposalBody expects matching incidents in newest-first order so it can
// report the date of the oldest match.
func proposalBody(current store.Incident, same []store.Incident) string {
var tried []string
for _, r := range same {
@@ -209,10 +206,10 @@ func proposalBody(current store.Incident, same []store.Incident) string {
}
}
}
body := fmt.Sprintf("%q has been the cause of %d incidents on %s %s since %s. Handling each one has not stopped it, so find what keeps producing it and fix that.",
body := fmt.Sprintf("%q caused %d recorded incidents on %s %s since %s. Identify and fix the underlying problem.",
current.Cause, len(same), current.TargetKind, current.TargetID, same[len(same)-1].At.Format("2006-01-02"))
if len(tried) > 0 {
body += " Handled so far with: " + strings.Join(tried, "; ") + "."
body += " Recorded actions: " + strings.Join(tried, "; ") + "."
}
return body
}
+4 -5
View File
@@ -38,9 +38,8 @@ func (f *fakeLedger) Propose(_ context.Context, _ string, p store.Proposal) (sto
return p, nil
}
// TestFinishProposesOnRecurrence guards the rule that a cause seen threshold
// times on a target raises a root-cause fix, whatever happened to the actions
// taken in each incident, and that other causes don't count towards it.
// TestFinishProposesOnRecurrence checks that only incidents with the same
// cause count towards the configured threshold, even if an action was denied.
func TestFinishProposesOnRecurrence(t *testing.T) {
now := time.Date(2026, 11, 16, 0, 0, 0, 0, time.UTC)
run := uuid.New()
@@ -60,8 +59,8 @@ func TestFinishProposesOnRecurrence(t *testing.T) {
{"one short of the threshold", incidents("disk-pressure", "disk-pressure", "disk-pressure", "disk-pressure"), 5, false},
{"the threshold is met by this incident", incidents("disk-pressure", "disk-pressure", "disk-pressure", "disk-pressure", "disk-pressure"), 5, true},
{"other causes are not counted", incidents("disk-pressure", "oom", "oom", "oom", "oom", "disk-pressure"), 5, false},
{"the threshold is the organisation's", incidents("disk-pressure", "disk-pressure", "disk-pressure"), 3, true},
{"past the threshold still proposes", incidents("disk-pressure", "disk-pressure", "disk-pressure", "disk-pressure", "disk-pressure", "disk-pressure"), 5, true},
{"uses the organisation's threshold", incidents("disk-pressure", "disk-pressure", "disk-pressure"), 3, true},
{"proposes above the threshold", incidents("disk-pressure", "disk-pressure", "disk-pressure", "disk-pressure", "disk-pressure", "disk-pressure"), 5, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {