feat(runs): an agent that describes an action without calling it gets one more turn to call it (archie #1999)

This commit is contained in:
sam committed 2026-11-16 04:39:49 +11:00
1 parent b9dd744f8c
commit 25007e8316
3 files changed
+79 -1

No files matched your search

+64 -1
View File
@@ -7,6 +7,7 @@ import (
"errors"
"fmt"
"regexp"
"slices"
"strings"
"time"
@@ -112,6 +113,12 @@ func Investigate(settings Settings, box Toolbox, judge Judge, ask Approver, noti
if err != nil {
return nil, err
}
if res, err = followThrough(rc, judge, decls, input, res, AgentRequest{
Provider: provider, Model: model.Model, ModelInfo: info,
Tools: toolSet(box, judge, rc.RunID(), decls),
}); err != nil {
return nil, err
}
outcomes, err := settleProposals(rc, judge, ask, deadline)
if err != nil {
return nil, err
@@ -132,6 +139,62 @@ func Investigate(settings Settings, box Toolbox, judge Judge, ask Approver, noti
}
}
// followThroughBudget bounds the turn that has the agent call what it only
// described.
var followThroughBudget = agentloop.Budget{MaxSteps: 8, MaxTokens: 60_000, WallClock: 5 * time.Minute}
// followThrough gives the agent one more turn when its findings name an action
// it never called: a small model often writes the call out instead of making
// it, and an action that was only described can't be judged or approved.
func followThrough(rc *Context, judge Judge, decls []connectors.Declaration, input jsontext.Value, res AgentResult, req AgentRequest) (AgentResult, error) {
if !res.Completed {
return res, nil
}
made, err := rc.Step("decisions-made", func(ctx context.Context, _ string) ([]string, error) {
decisions, err := judge.ForRun(ctx, rc.RunID())
names := make([]string, 0, len(decisions))
for _, d := range decisions {
names = append(names, toolName(d.Action.Connector, d.Action.Name))
}
return names, err
})
if err != nil {
return res, err
}
missed := uncalled(res.Answer, decls, made)
if len(missed) == 0 {
return res, nil
}
prompt, err := json.Marshal(map[string]any{"event": input, "findings": res.Answer, "described_but_not_called": missed})
if err != nil {
return res, err
}
req.Name, req.System, req.Prompt, req.Budget = "follow-through", prompts.Investigate()+"\n\n"+prompts.FollowThrough(), string(prompt), followThroughBudget
again, err := rc.Agent(req)
if err != nil || !again.Completed {
// The first findings stand when the extra turn doesn't finish.
return res, err
}
return again, nil
}
// uncalled lists the actions an answer names that no decision in the run
// records.
func uncalled(answer string, decls []connectors.Declaration, made []string) []string {
var out []string
for _, d := range decls {
name := toolName(d.Connector, d.Name)
if d.Kind == connectors.KindAction && strings.Contains(answer, name) && !slices.Contains(made, name) {
out = append(out, name)
}
}
return out
}
func toolName(connector, name string) string {
return unsafeToolChars.ReplaceAllString(connector+"_"+name, "_")
}
// approvalOutcome is what became of one action the agent proposed.
type approvalOutcome struct {
Action string `json:"action"`
@@ -339,7 +402,7 @@ var unsafeToolChars = regexp.MustCompile(`[^a-zA-Z0-9_-]`)
func toolSet(box Toolbox, judge Judge, runID uuid.UUID, decls []connectors.Declaration) core.ToolSet {
set := core.ToolSet{}
for _, d := range decls {
name := unsafeToolChars.ReplaceAllString(d.Connector+"_"+d.Name, "_")
name := toolName(d.Connector, d.Name)
set[name] = &core.Tool{
Name: name,
Description: d.Description,