feat(memory,runs,governance): remember each target's incidents, cite them in a run, and propose a root-cause fix after K with one cause (archie #1993)

This commit is contained in:
sam committed 2026-11-16 05:04:15 +11:00
1 parent 22aee72679
commit a0385a56c7
14 files changed
+774 -26

No files matched your search

+51 -11
View File
@@ -38,6 +38,17 @@ const DefaultRaiseAfter = 3
// maxRaiseAfter bounds the count: past it a class could never earn a raise.
const maxRaiseAfter = 100
// DefaultRecurrenceThreshold is how many incidents with one cause on one target
// it takes to propose a root-cause fix when the organisation hasn't said.
const DefaultRecurrenceThreshold = 5
// The bounds on the recurrence threshold. One incident is no pattern; past a
// hundred the history Archie keeps per target can't confirm it.
const (
minRecurrenceThreshold = 2
maxRecurrenceThreshold = 100
)
// The bounds on an approval deadline. Below a minute nobody could answer; past
// a month the request outlives the incident it was raised for.
const (
@@ -56,19 +67,26 @@ type Settings struct {
// Frozen holds every action that changes something at propose and wait,
// without changing any class's level.
Frozen bool
// RecurrenceThreshold is how many incidents with the same cause on one
// target it takes, whatever their outcomes, to propose a root-cause fix.
RecurrenceThreshold int
}
// settingsRequest is the settings as the API shows and sets them. A duration
// travels as a Go duration string ("24h"), the same form every other duration
// in the config takes.
type settingsRequest struct {
ApprovalDeadline string `json:"approval_deadline"`
RaiseAfter int `json:"raise_after"`
Frozen bool `json:"frozen"`
ApprovalDeadline string `json:"approval_deadline"`
RaiseAfter int `json:"raise_after"`
Frozen bool `json:"frozen"`
RecurrenceThreshold int `json:"recurrence_threshold"`
}
func (s Settings) request() settingsRequest {
return settingsRequest{ApprovalDeadline: s.ApprovalDeadline.String(), RaiseAfter: s.RaiseAfter, Frozen: s.Frozen}
return settingsRequest{
ApprovalDeadline: s.ApprovalDeadline.String(), RaiseAfter: s.RaiseAfter, Frozen: s.Frozen,
RecurrenceThreshold: s.RecurrenceThreshold,
}
}
// Model is the model chosen for a role.
@@ -129,11 +147,16 @@ func (m *Module) Settings(ctx context.Context) (Settings, error) {
return Settings{}, err
}
if !ok {
return Settings{ApprovalDeadline: DefaultApprovalDeadline, RaiseAfter: DefaultRaiseAfter}, nil
return Settings{
ApprovalDeadline: DefaultApprovalDeadline, RaiseAfter: DefaultRaiseAfter,
RecurrenceThreshold: DefaultRecurrenceThreshold,
}, nil
}
return Settings{
ApprovalDeadline: time.Duration(row.ApprovalDeadlineMS) * time.Millisecond,
RaiseAfter: row.RaiseAfter, Frozen: row.Frozen,
ApprovalDeadline: time.Duration(row.ApprovalDeadlineMS) * time.Millisecond,
RaiseAfter: row.RaiseAfter,
Frozen: row.Frozen,
RecurrenceThreshold: row.RecurrenceThreshold,
}, nil
}
@@ -156,6 +179,16 @@ func (m *Module) Frozen(ctx context.Context) (bool, error) {
return set.Frozen, nil
}
// RecurrenceThreshold is how many incidents with one cause on one target it
// takes to propose a root-cause fix.
func (m *Module) RecurrenceThreshold(ctx context.Context) (int, error) {
set, err := m.Settings(ctx)
if err != nil {
return 0, err
}
return set.RecurrenceThreshold, nil
}
// ApprovalDeadline is how long an approval request waits for an answer.
func (m *Module) ApprovalDeadline(ctx context.Context) (time.Duration, error) {
set, err := m.Settings(ctx)
@@ -194,14 +227,17 @@ func (m *Module) putSettings(w http.ResponseWriter, r *http.Request) {
httpjson.Error(w, http.StatusBadRequest, "approval deadline is a duration, such as 24h")
return
}
set := Settings{ApprovalDeadline: deadline, RaiseAfter: req.RaiseAfter, Frozen: req.Frozen}
set := Settings{
ApprovalDeadline: deadline, RaiseAfter: req.RaiseAfter, Frozen: req.Frozen,
RecurrenceThreshold: req.RecurrenceThreshold,
}
if err := set.validate(); err != nil {
httpjson.Error(w, http.StatusBadRequest, err.Error())
return
}
if err := m.store.PutSettings(r.Context(), store.Settings{
OrgID: m.orgID, ApprovalDeadlineMS: set.ApprovalDeadline.Milliseconds(),
RaiseAfter: set.RaiseAfter, Frozen: set.Frozen,
RaiseAfter: set.RaiseAfter, Frozen: set.Frozen, RecurrenceThreshold: set.RecurrenceThreshold,
}); err != nil {
m.log.ErrorContext(r.Context(), "put settings", "error", err)
httpjson.Error(w, http.StatusInternalServerError, "couldn't save the settings")
@@ -210,9 +246,13 @@ func (m *Module) putSettings(w http.ResponseWriter, r *http.Request) {
httpjson.Write(w, http.StatusOK, set.request())
}
// validate keeps a deadline inside the range an answer can be given in, and
// the count of approvals a raise needs inside the range a class can earn.
// validate keeps a deadline inside the range an answer can be given in, the
// count of approvals a raise needs inside the range a class can earn, and the
// recurrence threshold inside the range that means a pattern.
func (s Settings) validate() error {
if s.RecurrenceThreshold < minRecurrenceThreshold || s.RecurrenceThreshold > maxRecurrenceThreshold {
return fmt.Errorf("recurrence threshold must be between %d and %d", minRecurrenceThreshold, maxRecurrenceThreshold)
}
if s.ApprovalDeadline < minApprovalDeadline || s.ApprovalDeadline > maxApprovalDeadline {
return fmt.Errorf("approval deadline must be between %s and %s", minApprovalDeadline, maxApprovalDeadline)
}
+8 -4
View File
@@ -67,6 +67,9 @@ type Settings struct {
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"`
}
// Settings returns the organisation's settings; ok is false when none were
@@ -74,7 +77,8 @@ type Settings struct {
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 FROM governance.settings WHERE org_id = $1`, orgID)
SELECT org_id, approval_deadline_ms, raise_after, frozen, recurrence_threshold
FROM governance.settings WHERE org_id = $1`, orgID)
if errors.Is(err, sql.ErrNoRows) {
return Settings{}, false, nil
}
@@ -87,11 +91,11 @@ func (s *Store) Settings(ctx context.Context, orgID string) (Settings, bool, err
// 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)
VALUES (:org_id, :approval_deadline_ms, :raise_after, :frozen)
INSERT INTO governance.settings (org_id, approval_deadline_ms, raise_after, frozen, recurrence_threshold)
VALUES (:org_id, :approval_deadline_ms, :raise_after, :frozen, :recurrence_threshold)
ON CONFLICT (org_id) DO UPDATE
SET approval_deadline_ms = EXCLUDED.approval_deadline_ms, raise_after = EXCLUDED.raise_after,
frozen = EXCLUDED.frozen, updated_at = now()`, set); err != nil {
frozen = EXCLUDED.frozen, recurrence_threshold = EXCLUDED.recurrence_threshold, updated_at = now()`, set); err != nil {
return fmt.Errorf("put settings: %w", err)
}
return nil
+109
View File
@@ -0,0 +1,109 @@
// Package store holds the memory module's SQL: incidents and the proposals
// raised from them.
package store
import (
"context"
"fmt"
"time"
"github.com/google/uuid"
"github.com/jmoiron/sqlx"
)
// Incident is one investigation of one target as stored.
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 root-cause fix asked for 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 records the incident of a run, replacing what an earlier attempt of the
// same run recorded.
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 records what was done in 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 records p unless the target and cause already have one, and returns
// the one that stands.
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
}
+218
View File
@@ -0,0 +1,218 @@
// 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
import (
"context"
"encoding/json/jsontext"
json "encoding/json/v2"
"errors"
"fmt"
"regexp"
"slices"
"strings"
"time"
"github.com/google/uuid"
"github.com/jmoiron/sqlx"
"github.com/samcharles93/archie-core/modules/memory/internal/store"
)
// maxHistory bounds the incidents read for one target. The recurrence setting
// is capped below it.
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.
type Target struct {
Kind string `json:"kind"`
ID string `json:"id"`
}
// Action is something done during an incident and how it ended.
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 string `json:"state"`
Detail string `json:"detail,omitzero"`
}
// Incident is one investigation of a target.
type Incident struct {
At time.Time `json:"at"`
Cause string `json:"cause"`
Summary string `json:"summary"`
Actions []Action `json:"actions"`
}
// Line is the incident as one entry of a list an agent reads.
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 {
return line + " Actions: none."
}
parts := make([]string, 0, len(i.Actions))
for _, a := range i.Actions {
part := a.Action + " on " + a.Target + ": " + a.State
if a.Detail != "" {
part += " (" + a.Detail + ")"
}
parts = append(parts, part)
}
return line + " Actions: " + strings.Join(parts, "; ") + "."
}
// Review is what recording a run's actions found about its cause.
type Review struct {
// Repeats is how many incidents on the target have this cause, this one
// included.
Repeats int
// Proposal is the root-cause fix asked for, empty until the cause has
// recurred enough.
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
Incident(ctx context.Context, orgID string, runID uuid.UUID) (store.Incident, bool, error)
ByTarget(ctx context.Context, orgID, kind, id string, limit int) ([]store.Incident, error)
Propose(ctx context.Context, orgID string, p store.Proposal) (store.Proposal, error)
}
// Module remembers one organisation's incidents.
type Module struct {
orgID string
ledger ledger
}
// NewModule returns the memory module for orgID.
func NewModule(db *sqlx.DB, orgID string) *Module {
return &Module{orgID: orgID, ledger: store.New(db)}
}
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.
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.
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)
summary = strings.TrimSpace(summary)
if t.Kind == "" || t.ID == "" || cause == "" || summary == "" {
return errors.New("a target kind, a target id, a cause and a summary are all required")
}
if !event.IsValid() {
event = jsontext.Value(`null`)
}
return m.ledger.Upsert(ctx, m.orgID, store.Incident{
ID: uuid.New(), RunID: runID, TargetKind: t.Kind, TargetID: t.ID, Cause: cause, Summary: summary, Event: event,
})
}
// Past returns the incidents recorded on t before the run's own, newest first.
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 {
return nil, err
}
out := make([]Incident, 0, len(rows))
for _, r := range rows {
if r.RunID == runID {
continue
}
in, err := incident(r)
if err != nil {
return nil, err
}
out = append(out, in)
}
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.
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 {
return Review{}, err
}
raw, err := json.Marshal(actions)
if err != nil {
return Review{}, fmt.Errorf("encode actions: %w", err)
}
if err := m.ledger.SetActions(ctx, m.orgID, runID, raw); err != nil {
return Review{}, err
}
rows, err := m.ledger.ByTarget(ctx, m.orgID, current.TargetKind, current.TargetID, maxHistory)
if err != nil {
return Review{}, err
}
var same []store.Incident
for _, r := range rows {
if r.Cause == current.Cause {
same = append(same, r)
}
}
review := Review{Repeats: len(same)}
if review.Repeats < threshold {
return review, nil
}
proposal, err := m.ledger.Propose(ctx, m.orgID, store.Proposal{
ID: uuid.New(), TargetKind: current.TargetKind, TargetID: current.TargetID, Cause: current.Cause,
Incidents: review.Repeats, Body: proposalBody(current, same),
})
if err != nil {
return Review{}, err
}
review.Proposal = proposal.Body
return review, nil
}
func incident(r store.Incident) (Incident, error) {
out := Incident{At: r.At, Cause: r.Cause, Summary: r.Summary}
if len(r.Actions) > 0 {
if err := json.Unmarshal(r.Actions, &out.Actions); err != nil {
return Incident{}, fmt.Errorf("decode actions of incident %s: %w", r.ID, err)
}
}
return out, nil
}
// proposalBody asks for the cause to be fixed rather than handled again. same
// is newest first.
func proposalBody(current store.Incident, same []store.Incident) string {
var tried []string
for _, r := range same {
in, err := incident(r)
if err != nil {
continue
}
for _, a := range in.Actions {
if line := a.Action + " on " + a.Target; !slices.Contains(tried, line) {
tried = append(tried, line)
}
}
}
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.",
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, "; ") + "."
}
return body
}
+79
View File
@@ -0,0 +1,79 @@
package memory
import (
"context"
"testing"
"time"
"github.com/google/uuid"
"github.com/samcharles93/archie-core/modules/memory/internal/store"
)
// fakeLedger holds one target's incidents, newest first, and the proposals made.
type fakeLedger struct {
rows []store.Incident
proposals []store.Proposal
}
func (f *fakeLedger) Upsert(context.Context, string, store.Incident) error { return nil }
func (f *fakeLedger) SetActions(context.Context, string, uuid.UUID, []byte) error { return nil }
func (f *fakeLedger) Incident(_ context.Context, _ string, runID uuid.UUID) (store.Incident, bool, error) {
for _, r := range f.rows {
if r.RunID == runID {
return r, true, nil
}
}
return store.Incident{}, false, nil
}
func (f *fakeLedger) ByTarget(context.Context, string, string, string, int) ([]store.Incident, error) {
return f.rows, nil
}
func (f *fakeLedger) Propose(_ context.Context, _ string, p store.Proposal) (store.Proposal, error) {
f.proposals = append(f.proposals, p)
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.
func TestFinishProposesOnRecurrence(t *testing.T) {
now := time.Date(2026, 11, 16, 0, 0, 0, 0, time.UTC)
run := uuid.New()
incidents := func(causes ...string) []store.Incident {
rows := []store.Incident{{RunID: run, Cause: causes[0], At: now}}
for i, c := range causes[1:] {
rows = append(rows, store.Incident{RunID: uuid.New(), Cause: c, At: now.AddDate(0, 0, -i-1)})
}
return rows
}
tests := []struct {
name string
rows []store.Incident
threshold int
want bool
}{
{"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},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ledger := &fakeLedger{rows: tt.rows}
m := &Module{orgID: "org", ledger: ledger}
review, err := m.Finish(t.Context(), run, []Action{{Action: "restart", Target: "host:web-1", State: "denied"}}, tt.threshold)
if err != nil {
t.Fatal(err)
}
if got := review.Proposal != ""; got != tt.want || (len(ledger.proposals) == 1) != tt.want {
t.Fatalf("proposal = %q, recorded %d; want proposal %v", review.Proposal, len(ledger.proposals), tt.want)
}
})
}
}
+35 -8
View File
@@ -21,6 +21,7 @@ import (
"github.com/samcharles93/archie-core/modules/deliveries"
"github.com/samcharles93/archie-core/modules/governance"
"github.com/samcharles93/archie-core/modules/judgement"
"github.com/samcharles93/archie-core/modules/memory"
)
// Settings is where a workflow finds what the organisation decided: the agent
@@ -28,6 +29,7 @@ import (
type Settings interface {
AgentModel(ctx context.Context) (governance.Model, error)
ApprovalDeadline(ctx context.Context) (time.Duration, error)
RecurrenceThreshold(ctx context.Context) (int, error)
}
// Toolbox is where a workflow finds the connector lookups and actions and the
@@ -68,7 +70,7 @@ var investigateBudget = agentloop.Budget{MaxSteps: 20, MaxTokens: 200_000, WallC
// the same tools even if settings or connectors changed meanwhile. An action
// the agent proposes is judged as it calls it; one judgement holds is put to a
// person, and the run waits for the answer before running it.
func Investigate(settings Settings, box Toolbox, judge Judge, ask Approver, notify Notifier) Workflow {
func Investigate(settings Settings, box Toolbox, judge Judge, ask Approver, notify Notifier, mem Memory) Workflow {
return func(rc *Context, input jsontext.Value) (jsontext.Value, error) {
model, err := rc.Step("agent-model", func(ctx context.Context, _ string) (governance.Model, error) {
return settings.AgentModel(ctx)
@@ -90,6 +92,12 @@ func Investigate(settings Settings, box Toolbox, judge Judge, ask Approver, noti
if err != nil {
return nil, fmt.Errorf("approval deadline %q: %w", deadlineText, err)
}
threshold, err := rc.Step("recurrence-threshold", func(ctx context.Context, _ string) (int, error) {
return settings.RecurrenceThreshold(ctx)
})
if err != nil {
return nil, err
}
decls, err := rc.Step("agent-tools", func(context.Context, string) ([]connectors.Declaration, error) {
return box.Declarations(), nil
})
@@ -107,7 +115,7 @@ func Investigate(settings Settings, box Toolbox, judge Judge, ask Approver, noti
ModelInfo: info,
System: prompts.Investigate(),
Prompt: string(input),
Tools: toolSet(box, judge, rc.RunID(), decls),
Tools: withMemory(toolSet(box, judge, rc.RunID(), decls), mem, rc.RunID()),
Budget: investigateBudget,
})
if err != nil {
@@ -115,16 +123,23 @@ func Investigate(settings Settings, box Toolbox, judge Judge, ask Approver, noti
}
if res, err = followThrough(rc, judge, decls, input, res, AgentRequest{
Provider: provider, Model: model.Model, ModelInfo: info,
Tools: toolSet(box, judge, rc.RunID(), decls),
Tools: withMemory(toolSet(box, judge, rc.RunID(), decls), mem, rc.RunID()),
}); err != nil {
return nil, err
}
if err := recordCause(rc, mem, input, res, AgentRequest{Provider: provider, Model: model.Model, ModelInfo: info}); err != nil {
return nil, err
}
outcomes, err := settleProposals(rc, judge, ask, deadline)
if err != nil {
return nil, err
}
review, err := remember(rc, mem, judge, outcomes, threshold)
if err != nil {
return nil, err
}
if _, err := rc.Step("notify", func(ctx context.Context, key string) (bool, error) {
return true, notify.Notify(ctx, findings(rc.RunID(), key, res, outcomes))
return true, notify.Notify(ctx, findings(rc.RunID(), key, res, outcomes, review))
}); err != nil {
return nil, err
}
@@ -202,8 +217,9 @@ type approvalOutcome struct {
RequestID uuid.UUID `json:"request_id"`
State string `json:"state"`
By string `json:"by,omitzero"`
Result string `json:"result,omitzero"`
Error string `json:"error,omitzero"`
digest string
Result string `json:"result,omitzero"`
Error string `json:"error,omitzero"`
// Unknown is an action that was started but can't be confirmed: it may
// have taken effect, and it is never started again.
Unknown bool `json:"unknown,omitzero"`
@@ -236,7 +252,7 @@ func settleProposals(rc *Context, judge Judge, ask Approver, deadline time.Durat
}
func settleOne(rc *Context, judge Judge, ask Approver, d judgement.Decision, deadline time.Duration) (approvalOutcome, error) {
out := approvalOutcome{Action: d.Action.Connector + " " + d.Action.Name, Target: d.Action.Target.Kind + ":" + d.Action.Target.ID}
out := approvalOutcome{digest: d.Action.Digest, Action: d.Action.Connector + " " + d.Action.Name, Target: d.Action.Target.Kind + ":" + d.Action.Target.ID}
request, err := rc.Step("ask:"+d.Key, func(ctx context.Context, _ string) (approvals.Request, error) {
return ask.Ask(ctx, rc.RunID(), waitKey(d.Key), d, time.Now().Add(deadline))
})
@@ -354,7 +370,7 @@ func heldByJudgement(decisions []judgement.Decision) []judgement.Decision {
func waitKey(key string) string { return "approval:" + key }
// findings is the notification an investigation ends with, finished or not.
func findings(runID uuid.UUID, key string, res AgentResult, outcomes []approvalOutcome) deliveries.Notification {
func findings(runID uuid.UUID, key string, res AgentResult, outcomes []approvalOutcome, review memory.Review) deliveries.Notification {
n := deliveries.Notification{RunID: runID, Key: key, Title: "Investigation finished", Body: res.Answer}
if !res.Completed {
n.Title = "Investigation stopped"
@@ -363,6 +379,9 @@ func findings(runID uuid.UUID, key string, res AgentResult, outcomes []approvalO
// The record says plainly when nothing was proposed, whatever the findings
// then claim.
n.Body += "\n\n" + outcomeLines(outcomes)
if note := recurrenceLines(review); note != "" {
n.Body += "\n\n" + note
}
return n
}
@@ -393,6 +412,14 @@ func outcomeLines(outcomes []approvalOutcome) string {
return "Actions:\n" + strings.Join(lines, "\n")
}
// withMemory adds the lookup of a target's earlier incidents to the agent's
// tools.
func withMemory(set core.ToolSet, mem Memory, runID uuid.UUID) core.ToolSet {
tool := pastIncidentsTool(mem, runID)
set[tool.Name] = tool
return set
}
// unsafeToolChars are those a model API refuses in a tool name.
var unsafeToolChars = regexp.MustCompile(`[^a-zA-Z0-9_-]`)
+180
View File
@@ -0,0 +1,180 @@
package runs
import (
"context"
"encoding/json/jsontext"
json "encoding/json/v2"
"fmt"
"strings"
"time"
"github.com/google/uuid"
"github.com/samcharles93/ai-sdk/agentloop"
"github.com/samcharles93/ai-sdk/core"
"github.com/samcharles93/archie-core/internal/platform/prompts"
"github.com/samcharles93/archie-core/modules/judgement"
"github.com/samcharles93/archie-core/modules/memory"
)
// Memory is what an investigation remembers and recalls about its target.
type Memory interface {
Past(ctx context.Context, runID uuid.UUID, t memory.Target) ([]memory.Incident, error)
Record(ctx context.Context, runID uuid.UUID, event jsontext.Value, t memory.Target, cause, summary string) error
Finish(ctx context.Context, runID uuid.UUID, actions []memory.Action, threshold int) (memory.Review, error)
}
// recordCauseBudget bounds the turn that files the cause: one tool call and a
// reply.
var recordCauseBudget = agentloop.Budget{MaxSteps: 4, MaxTokens: 30_000, WallClock: 3 * time.Minute}
// maxRecalled is how many earlier incidents the agent is shown.
const maxRecalled = 10
const targetSchema = `{"type":"object","properties":{
"target_kind":{"type":"string","description":"what the incident is about, such as host"},
"target_id":{"type":"string","description":"which one, such as web-1"}},
"required":["target_kind","target_id"]}`
const causeSchema = `{"type":"object","properties":{
"target_kind":{"type":"string","description":"what the incident happened to, such as host"},
"target_id":{"type":"string","description":"which one, such as web-1"},
"cause":{"type":"string","description":"two to four lowercase words joined by hyphens, such as disk-pressure"},
"summary":{"type":"string","description":"one sentence: what happened and why"}},
"required":["target_kind","target_id","cause","summary"]}`
type targetInput struct {
Kind string `json:"target_kind"`
ID string `json:"target_id"`
}
type causeInput struct {
targetInput
Cause string `json:"cause"`
Summary string `json:"summary"`
}
// pastIncidentsTool lets the agent ask what happened to a target before.
func pastIncidentsTool(mem Memory, runID uuid.UUID) *core.Tool {
return &core.Tool{
Name: "memory_past_incidents",
Description: "Lists earlier incidents on a target: when, the cause found, what was done and how it ended.",
Parameters: []byte(targetSchema),
Execute: func(ctx context.Context, input string) (string, error) {
var in targetInput
if err := json.Unmarshal([]byte(input), &in); err != nil {
return "", fmt.Errorf("read input: %w", err)
}
past, err := mem.Past(ctx, runID, memory.Target{Kind: in.Kind, ID: in.ID})
if err != nil {
return "", err
}
if len(past) == 0 {
return "no earlier incidents on this target", nil
}
past = past[:min(len(past), maxRecalled)]
lines := make([]string, 0, len(past))
for _, p := range past {
lines = append(lines, p.Line())
}
return strings.Join(lines, "\n"), nil
},
}
}
// recordCause has the agent name the target and cause of the investigation it
// just finished, in a turn of its own: a small model that is asked for four
// plain fields right after writing its findings fills them reliably, where one
// asked to remember a tool call at the end of a long investigation does not.
func recordCause(rc *Context, mem Memory, input jsontext.Value, res AgentResult, req AgentRequest) error {
if !res.Completed {
return nil
}
prompt, err := json.Marshal(map[string]any{"event": input, "findings": res.Answer})
if err != nil {
return err
}
req.Name, req.System, req.Prompt, req.Budget = "record-cause", prompts.RecordCause(), string(prompt), recordCauseBudget
req.Tools = core.ToolSet{"record_cause": {
Name: "record_cause",
Description: "Files the target and cause of this incident in memory.",
Parameters: []byte(causeSchema),
Execute: func(ctx context.Context, raw string) (string, error) {
var in causeInput
if err := json.Unmarshal([]byte(raw), &in); err != nil {
return "", fmt.Errorf("read input: %w", err)
}
if err := mem.Record(ctx, rc.RunID(), input, memory.Target{Kind: in.Kind, ID: in.ID}, in.Cause, in.Summary); err != nil {
return "", err
}
return "recorded", nil
},
}}
// A turn that doesn't file anything leaves the run without an incident; the
// investigation's findings stand.
_, err = rc.Agent(req)
return err
}
// incidentActions lists what the run did: each action judged, once, with how it
// ended.
func incidentActions(decisions []judgement.Decision, outcomes []approvalOutcome) []memory.Action {
byDigest := make(map[string]approvalOutcome, len(outcomes))
for _, o := range outcomes {
byDigest[o.digest] = o
}
seen := make(map[string]bool, len(decisions))
out := make([]memory.Action, 0, len(decisions))
for _, d := range decisions {
if seen[d.Action.Digest] {
continue
}
seen[d.Action.Digest] = true
a := memory.Action{Action: d.Action.Connector + " " + d.Action.Name, Target: d.Action.Target.Kind + ":" + d.Action.Target.ID}
switch o, held := byDigest[d.Action.Digest]; {
case held:
a.State, a.Detail = o.State, firstText(o.Error, o.Result)
if o.Unknown {
a.State = "outcome unknown"
}
case d.Level == judgement.Never:
a.State = "refused"
case d.Level.Runs():
a.State = "ran"
default:
a.State = "held"
}
out = append(out, a)
}
return out
}
func firstText(parts ...string) string {
for _, p := range parts {
if p != "" {
return p
}
}
return ""
}
// remember files what the run did and checks whether its cause has recurred
// enough to ask for a root-cause fix.
func remember(rc *Context, mem Memory, judge Judge, outcomes []approvalOutcome, threshold int) (memory.Review, error) {
return rc.Step("remember", func(ctx context.Context, _ string) (memory.Review, error) {
decisions, err := judge.ForRun(ctx, rc.RunID())
if err != nil {
return memory.Review{}, err
}
return mem.Finish(ctx, rc.RunID(), incidentActions(decisions, outcomes), threshold)
})
}
// recurrenceLines is the findings' note on a cause that has come back: that it
// has, and the root-cause fix asked for.
func recurrenceLines(r memory.Review) string {
if r.Proposal == "" {
return ""
}
return fmt.Sprintf("Root-cause fix proposed (this is incident %d with this cause): %s", r.Repeats, r.Proposal)
}