feat(guard,runs): a guard is shown the agent's diagnosis and the evidence it read before proposing an action (#2006)
This commit is contained in:
7 files changed
+231
-43
No files matched your search
@@ -31,6 +31,15 @@ type GuardRequest struct {
|
||||
// Deadline is how long the host waits; the caller's context enforces it.
|
||||
Deadline time.Duration
|
||||
At time.Time
|
||||
// Diagnosis and Evidence are what the proposing agent concluded and read.
|
||||
Diagnosis string
|
||||
Evidence []GuardEvidence
|
||||
}
|
||||
|
||||
// GuardEvidence is one tool result the agent read.
|
||||
type GuardEvidence struct {
|
||||
Source string
|
||||
Text string
|
||||
}
|
||||
|
||||
// GuardFinding is one thing a guard noticed. Strength is "weak", "moderate"
|
||||
@@ -78,13 +87,18 @@ func (g *Guard) Advisory() bool { return g.advisory }
|
||||
|
||||
// Judge asks the guard about one action. ctx bounds the wait.
|
||||
func (g *Guard) Judge(ctx context.Context, r GuardRequest) (GuardAnswer, error) {
|
||||
evidence := make([]*gpb.Evidence, 0, len(r.Evidence))
|
||||
for _, e := range r.Evidence {
|
||||
evidence = append(evidence, &gpb.Evidence{Source: e.Source, Text: e.Text})
|
||||
}
|
||||
resp, err := g.client.Judge(ctx, &gpb.JudgeRequest{
|
||||
Connector: r.Connector, Name: r.Name, Input: r.Input, Digest: r.Digest,
|
||||
Target: &gpb.Target{Kind: r.Target.Kind, Id: r.Target.ID, Environment: r.Target.Environment, Disposable: r.Target.Disposable},
|
||||
Effect: &gpb.Effect{ReadOnly: r.Effect.ReadOnly, Reversibility: gpb.Reversibility(r.Effect.Reversibility), Summary: r.Effect.Summary},
|
||||
Run: &gpb.RunContext{RunId: r.RunID, DecisionKey: r.DecisionKey},
|
||||
Deadline: durationpb.New(r.Deadline),
|
||||
At: timestamppb.New(r.At),
|
||||
Target: &gpb.Target{Kind: r.Target.Kind, Id: r.Target.ID, Environment: r.Target.Environment, Disposable: r.Target.Disposable},
|
||||
Effect: &gpb.Effect{ReadOnly: r.Effect.ReadOnly, Reversibility: gpb.Reversibility(r.Effect.Reversibility), Summary: r.Effect.Summary},
|
||||
Run: &gpb.RunContext{RunId: r.RunID, DecisionKey: r.DecisionKey},
|
||||
Deadline: durationpb.New(r.Deadline),
|
||||
At: timestamppb.New(r.At),
|
||||
Diagnosis: r.Diagnosis, Evidence: evidence,
|
||||
})
|
||||
if err != nil {
|
||||
return GuardAnswer{}, fmt.Errorf("guard %q: %w", g.name, err)
|
||||
|
||||
@@ -25,6 +25,16 @@ type RunContext struct {
|
||||
RunID uuid.UUID
|
||||
Key string
|
||||
Workflow string
|
||||
// Diagnosis and Evidence are what the proposing agent concluded and read
|
||||
// before proposing the action.
|
||||
Diagnosis string
|
||||
Evidence []Evidence
|
||||
}
|
||||
|
||||
// Evidence is one tool result the agent read, as the tool returned it.
|
||||
type Evidence struct {
|
||||
Source string `json:"source"`
|
||||
Text string `json:"text"`
|
||||
}
|
||||
|
||||
type runKey struct{}
|
||||
@@ -59,6 +69,10 @@ func (e extensionGuard) Judge(ctx context.Context, a Action) (Verdict, error) {
|
||||
RunID: run.RunID.String(), DecisionKey: run.Key,
|
||||
At: time.Now(),
|
||||
}
|
||||
req.Diagnosis = run.Diagnosis
|
||||
for _, e := range run.Evidence {
|
||||
req.Evidence = append(req.Evidence, connectors.GuardEvidence{Source: e.Source, Text: e.Text})
|
||||
}
|
||||
if d, ok := ctx.Deadline(); ok {
|
||||
req.Deadline = time.Until(d)
|
||||
}
|
||||
|
||||
@@ -146,6 +146,10 @@ type Request struct {
|
||||
Input jsontext.Value
|
||||
// Workflow is the workflow the run executes, the principal hard rules see.
|
||||
Workflow string
|
||||
// Diagnosis and Evidence are what the proposing agent concluded and read,
|
||||
// for guards that weigh the reasoning behind an action. Both may be empty.
|
||||
Diagnosis string
|
||||
Evidence []Evidence
|
||||
}
|
||||
|
||||
// Tools finds the tool that resolves and runs an action.
|
||||
@@ -211,7 +215,7 @@ func (m *Module) Assess(ctx context.Context, req Request) (Decision, error) {
|
||||
Connector: req.Connector, Name: req.Action, Input: req.Input,
|
||||
Digest: connectors.Digest(req.Action, req.Input), Target: res.Target, Effect: res.Effect,
|
||||
}
|
||||
d := m.decide(withRun(ctx, RunContext{RunID: req.RunID, Key: req.Key, Workflow: req.Workflow}), a)
|
||||
d := m.decide(withRun(ctx, RunContext{RunID: req.RunID, Key: req.Key, Workflow: req.Workflow, Diagnosis: req.Diagnosis, Evidence: req.Evidence}), a)
|
||||
d.ID, d.RunID, d.Key, d.At = uuid.New(), req.RunID, req.Key, m.now().UTC()
|
||||
return m.record(ctx, d)
|
||||
}
|
||||
|
||||
@@ -317,9 +317,10 @@ func settleOne(rc *Context, judge Judge, ask Approver, settings Settings, d judg
|
||||
}
|
||||
out.By = answer.By
|
||||
fresh, err := rc.Step("judge-again:"+d.Key, func(ctx context.Context, key string) (judgement.Decision, error) {
|
||||
diagnosis, evidence := rc.Rationale()
|
||||
return judge.Assess(ctx, judgement.Request{
|
||||
RunID: rc.RunID(), Key: key, Connector: d.Action.Connector, Action: d.Action.Name, Input: d.Action.Input,
|
||||
Workflow: rc.Workflow(),
|
||||
Workflow: rc.Workflow(), Diagnosis: diagnosis, Evidence: evidence,
|
||||
})
|
||||
})
|
||||
if err != nil {
|
||||
@@ -632,7 +633,11 @@ func call(ctx context.Context, box Toolbox, judge Judge, rc *Context, d connecto
|
||||
// is proposed to a person when the investigation ends, so it comes back to
|
||||
// the agent as something waiting rather than a failure.
|
||||
key := core.IdempotencyKey(ctx)
|
||||
decision, err := judge.Assess(ctx, judgement.Request{RunID: rc.RunID(), Key: key, Connector: d.Connector, Action: d.Name, Input: input, Workflow: rc.Workflow()})
|
||||
diagnosis, evidence := rc.Rationale()
|
||||
decision, err := judge.Assess(ctx, judgement.Request{
|
||||
RunID: rc.RunID(), Key: key, Connector: d.Connector, Action: d.Name, Input: input, Workflow: rc.Workflow(),
|
||||
Diagnosis: diagnosis, Evidence: evidence,
|
||||
})
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
package runs
|
||||
|
||||
import (
|
||||
json "encoding/json/v2"
|
||||
"slices"
|
||||
"strings"
|
||||
|
||||
"github.com/samcharles93/ai-sdk/agentloop"
|
||||
|
||||
"github.com/samcharles93/archie-core/modules/judgement"
|
||||
)
|
||||
|
||||
// What a guard that weighs reasoning is shown: the latest evidence, each
|
||||
// result cut to a length a judge model can read.
|
||||
const (
|
||||
maxEvidenceItems = 12
|
||||
maxEvidenceChars = 2000
|
||||
)
|
||||
|
||||
// Rationale is what the run's agent concluded and read before now: its latest
|
||||
// words, and the tool results it saw, oldest first. It reads the agent loop's
|
||||
// journal, so a resumed run shows what an earlier attempt saw.
|
||||
func (rc *Context) Rationale() (string, []judgement.Evidence) {
|
||||
seqs := make([]int, 0, len(rc.saved))
|
||||
for seq, s := range rc.saved {
|
||||
if strings.HasPrefix(s.Name, "journal:agent:") {
|
||||
seqs = append(seqs, seq)
|
||||
}
|
||||
}
|
||||
slices.Sort(seqs)
|
||||
var diagnosis string
|
||||
var evidence []judgement.Evidence
|
||||
for _, seq := range seqs {
|
||||
var e agentloop.Entry
|
||||
if json.Unmarshal(rc.saved[seq].Output, &e) != nil {
|
||||
continue
|
||||
}
|
||||
switch {
|
||||
case e.Kind == agentloop.EntryModel && e.Message != nil:
|
||||
if text := strings.TrimSpace(e.Message.Text()); text != "" {
|
||||
diagnosis = text
|
||||
}
|
||||
case e.Kind == agentloop.EntryTool && e.Error == "" && !e.Unknown && e.Output != "":
|
||||
evidence = append(evidence, judgement.Evidence{Source: e.ToolName, Text: clip(e.Output)})
|
||||
}
|
||||
}
|
||||
if len(evidence) > maxEvidenceItems {
|
||||
evidence = evidence[len(evidence)-maxEvidenceItems:]
|
||||
}
|
||||
return diagnosis, evidence
|
||||
}
|
||||
|
||||
func clip(s string) string {
|
||||
if len(s) <= maxEvidenceChars {
|
||||
return s
|
||||
}
|
||||
return strings.ToValidUTF8(s[:maxEvidenceChars], "") + "..."
|
||||
}
|
||||
Reference in new issue
Block a user