Files

59 lines
1.6 KiB
Go

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], "") + "..."
}