feat(modelloop): run a step's tool calls in parallel as each tool's classification allows
This commit is contained in:
11 files changed
+79
-26
No files matched your search
@@ -74,14 +74,16 @@ func (r *LoopRunner) Run(ctx context.Context, workspace string, req agentrun.Req
|
||||
}
|
||||
captureTools := captureToolSet(req.CaptureTools, captures)
|
||||
res, err := r.run(ctx, agentloop.Config{
|
||||
Runtime: r.runtime,
|
||||
ModelRef: req.Model,
|
||||
WorkDir: workspace,
|
||||
Mission: req.Mission,
|
||||
ExtraRules: agentexec.ProjectScopedRules(workspace, req.ExtraRules),
|
||||
Notes: notes,
|
||||
Gate: toAgentGate(req.Gate),
|
||||
Preflight: toAgentCommands(req.Preflight),
|
||||
Runtime: r.runtime,
|
||||
ModelRef: req.Model,
|
||||
WorkDir: workspace,
|
||||
|
||||
MaxParallelToolCalls: MaxParallelToolCalls,
|
||||
Mission: req.Mission,
|
||||
ExtraRules: agentexec.ProjectScopedRules(workspace, req.ExtraRules),
|
||||
Notes: notes,
|
||||
Gate: toAgentGate(req.Gate),
|
||||
Preflight: toAgentCommands(req.Preflight),
|
||||
Budget: agentloop.Budget{
|
||||
MaxSteps: req.Budget.MaxSteps,
|
||||
WallClock: req.Budget.WallClock,
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
|
||||
aicore "github.com/samcharles93/ai-sdk/core"
|
||||
@@ -148,11 +149,45 @@ func BuildToolSetFrom(entries []tools.ToolEntry, opts ToolSetOptions) (aicore.To
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("tool %s schema: %w", entry.Name, err)
|
||||
}
|
||||
set[entry.Name] = aicore.NewTool(entry.Name, entry.Description, params, toolExecute(entry, opts))
|
||||
tool := aicore.NewTool(entry.Name, entry.Description, params, toolExecute(entry, opts))
|
||||
tool.Concurrency = concurrency(entry.Classification)
|
||||
set[entry.Name] = tool
|
||||
}
|
||||
return set, nil
|
||||
}
|
||||
|
||||
// MaxParallelToolCalls bounds how many of one model step's tool calls run at
|
||||
// once. Each tool's classification decides which calls may overlap.
|
||||
const MaxParallelToolCalls = 8
|
||||
|
||||
// concurrency maps a classification to how a call may overlap others.
|
||||
// Unclassified and approval-gated tools run alone: approvals are asked one
|
||||
// at a time, and an unknown tool is never assumed safe.
|
||||
func concurrency(class tools.ToolClassification) func(string) aicore.Concurrency {
|
||||
switch {
|
||||
case class.IsApprovalRequired():
|
||||
return nil
|
||||
case class.IsIdempotent():
|
||||
return func(input string) aicore.Concurrency { return pathKeyed(input, true) }
|
||||
case class.IsMutating():
|
||||
return func(input string) aicore.Concurrency { return pathKeyed(input, false) }
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// pathKeyed orders calls per path argument, so a read and a write of one
|
||||
// file keep the model's order. A call without a path runs in parallel when
|
||||
// pathless is set and alone otherwise.
|
||||
func pathKeyed(input string, pathless bool) aicore.Concurrency {
|
||||
var args struct {
|
||||
Path string `json:"path"`
|
||||
}
|
||||
if json.Unmarshal([]byte(input), &args) != nil || args.Path == "" {
|
||||
return aicore.Concurrency{Parallel: pathless}
|
||||
}
|
||||
return aicore.Concurrency{Parallel: true, Key: filepath.Clean(args.Path)}
|
||||
}
|
||||
|
||||
func safeToolAvailable(entry tools.ToolEntry) (available bool, err error) {
|
||||
defer func() {
|
||||
if recovered := recover(); recovered != nil {
|
||||
|
||||
Reference in new issue
Block a user