Repository navigation
Description
Summary
Execute independent tool calls concurrently instead of sequentially in the agentic loop.
Motivation
DocsClaw's RunToolLoop() in pkg/tools/loop.go processes tool calls one at a time in a for loop. When the LLM requests multiple independent tools (e.g., fetch a document + check permissions + get user profile), sequential execution adds unnecessary latency.
Go's goroutines make this a natural fit. Pi-mono defaults to parallel execution with a per-tool sequential override for tools that must run exclusively.
Proposed design
Tool execution mode
type ExecutionMode string
const (
ExecutionModeParallel ExecutionMode = "parallel"
ExecutionModeSequential ExecutionMode = "sequential"
)
type Tool interface {
Name() string
Description() string
Parameters() map[string]any
Execute(ctx context.Context, args map[string]any) *ToolResult
ExecutionMode() ExecutionMode // default: parallel
}Loop changes
// In RunToolLoop, replace sequential execution with:
// 1. Validate all tool calls (find tool, check hook) — sequential
// 2. Execute allowed tools — parallel via errgroup, respecting per-tool mode
// 3. Collect results in original order — for deterministic message assemblyPer-tool override
Most tools (webfetch, readfile, fetchdoc) are safe to run in parallel. Some (exec, writefile) may need sequential mode to avoid race conditions on shared state.
Additional improvement: AfterToolCall hook
Add AfterToolCall to the Hook interface alongside the existing BeforeToolCall. This enables post-execution logging, result transformation, and audit — useful for business compliance scenarios.
type Hook interface {
BeforeToolCall(ctx context.Context, name string, args map[string]any) (allow bool, reason string)
AfterToolCall(ctx context.Context, name string, args map[string]any, result *ToolResult)
}Related
- Benefits from OTel tracing (feat: add OpenTelemetry tracing for agent execution #34) — parallel spans show concurrent execution
- Tool result truncation (#TBD) should apply before results are sent to LLM
Identified from analysis of pi-mono's executeToolCallsParallel in packages/agent/src/agent-loop.ts.
Activity
feat: parallel tool execution and AfterToolCall hook (#50)