Files
archie/modules/runs/investigate.go
T
sam 02df83ccb7 feat(runs): the in-process agent loop, checkpointed as run steps, and the investigate workflow (#1977, #1889)
The agent model is an organisation setting in the new governance module.
Every model and tool call is a journal step, so a killed run resumes without
repeating either. Agents are offered connector lookups and read-only actions
only, until judgement exists.
2026-11-15 21:49:53 +11:00

142 lines
4.3 KiB
Go

package runs
import (
"context"
"encoding/json/jsontext"
json "encoding/json/v2"
"errors"
"fmt"
"regexp"
"time"
"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/connectors"
"github.com/samcharles93/archie-core/modules/governance"
)
// AgentModels is where a workflow finds the organisation's agent model.
type AgentModels interface {
AgentModel(ctx context.Context) (governance.Model, error)
}
// Toolbox is where a workflow finds the connector lookups and actions it may
// offer an agent, and the connectors that run them.
type Toolbox interface {
Declarations() []connectors.Declaration
Get(name string) (*connectors.Connector, bool)
}
// investigateBudget bounds one investigation. A token cap set by the
// organisation replaces it when settings grow one.
var investigateBudget = agentloop.Budget{MaxSteps: 20, MaxTokens: 200_000, WallClock: 10 * time.Minute}
// Investigate is the workflow that has an agent look into the event that
// started the run and report its findings. The agent model and the tools are
// pinned in the run's first steps, so a resumed run asks the same model with
// the same tools even if settings or connectors changed meanwhile.
func Investigate(models AgentModels, box Toolbox) Workflow {
return func(rc *Context, input jsontext.Value) (jsontext.Value, error) {
model, err := rc.Step("agent-model", func(ctx context.Context, _ string) (governance.Model, error) {
return models.AgentModel(ctx)
})
if err != nil {
return nil, err
}
decls, err := rc.Step("agent-tools", func(context.Context, string) ([]connectors.Declaration, error) {
return offered(box.Declarations()), nil
})
if err != nil {
return nil, err
}
provider, info, err := model.Provider(rc.Context())
if err != nil {
return nil, err
}
res, err := rc.Agent(AgentRequest{
Name: "investigate",
Provider: provider,
Model: model.Model,
ModelInfo: info,
System: prompts.Investigate(),
Prompt: string(input),
Tools: toolSet(box, decls),
Budget: investigateBudget,
})
if err != nil {
return nil, err
}
out, err := json.Marshal(res)
if err != nil {
return nil, fmt.Errorf("encode findings: %w", err)
}
if !res.Completed {
return out, fmt.Errorf("investigation stopped: %s", res.StopReason)
}
return out, nil
}
}
// offered keeps what an agent may call before judgement exists: lookups,
// which change nothing, and read-only actions.
func offered(all []connectors.Declaration) []connectors.Declaration {
var out []connectors.Declaration
for _, d := range all {
if d.ReadOnly {
out = append(out, d)
}
}
return out
}
// unsafeToolChars are those a model API refuses in a tool name.
var unsafeToolChars = regexp.MustCompile(`[^a-zA-Z0-9_-]`)
// toolSet turns pinned declarations into the agent's tools. Each tool calls
// its connector by name when the model asks, so a connector that has since
// stopped fails that call rather than the run.
func toolSet(box Toolbox, decls []connectors.Declaration) core.ToolSet {
set := core.ToolSet{}
for _, d := range decls {
name := unsafeToolChars.ReplaceAllString(d.Connector+"_"+d.Name, "_")
set[name] = &core.Tool{
Name: name,
Description: d.Description,
Parameters: []byte(d.Schema),
Execute: func(ctx context.Context, input string) (string, error) {
return call(ctx, box, d, jsontext.Value(input))
},
}
}
return set
}
func call(ctx context.Context, box Toolbox, d connectors.Declaration, input jsontext.Value) (string, error) {
conn, ok := box.Get(d.Connector)
if !ok {
return "", fmt.Errorf("connector %q isn't running", d.Connector)
}
if d.Kind == connectors.KindLookup {
out, found, err := conn.Lookup[jsontext.Value](ctx, d.Name, input)
switch {
case err != nil:
return "", err
case !found:
return "not found", nil
}
return string(out), nil
}
out, err := conn.Invoke[jsontext.Value](ctx, connectors.Invocation{
Action: d.Name, Input: input, IdempotencyKey: core.IdempotencyKey(ctx),
})
if errors.Is(err, connectors.ErrOutcomeUnknown) {
return "", fmt.Errorf("%w: %w", core.ErrOutcomeUnknown, err)
}
if err != nil {
return "", err
}
return string(out), nil
}