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) }