Files
archie/modules/runs/memory.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

176 lines
5.9 KiB
Go

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 stores and retrieves incident history for an investigation 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)
}
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":"the resource type, such as host"},
"target_id":{"type":"string","description":"the resource ID, such as web-1"}},
"required":["target_kind","target_id"]}`
const causeSchema = `{"type":"object","properties":{
"target_kind":{"type":"string","description":"the resource type, such as host"},
"target_id":{"type":"string","description":"the resource ID, 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"`
}
func pastIncidentsTool(mem Memory, runID uuid.UUID) *core.Tool {
return &core.Tool{
Name: "memory_past_incidents",
Description: "Lists previous incidents for a target, including dates, causes, actions and outcomes.",
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 uses a separate turn because small models are more reliable
// when asked for four fields after reporting than when asked to remember
// a tool call throughout the investigation.
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: "Records the incident target, cause and summary.",
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
},
}}
// If the agent records no incident, keep the investigation findings.
_, 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 records the incident and checks whether it meets the recurrence
// threshold for proposing 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)
}