Bug found while building rony-harness's "Edited Files" info panel: it
scanned the transcript for tool-call messages carrying write/edit
arguments, but those messages never appeared — not even for a plain,
successful top-level write with no delegation involved.
Root cause: RunStream's content-streaming gate (`if !hasToolCalls &&
(hasContent || hasUsage) { yield(chunk, nil) }`) suppresses yielding
*any* chunk once a tool call is seen in that iteration, including the
chunk carrying the tool call itself. So chunk.ToolCalls was executed
internally (hence approvals and results worked) but never yielded to
the caller. Every caller-side "which tool got called" hook depending on
the stream (not the Approver callback) was therefore dead code.
Fix: yield a dedicated chunk carrying just the executed ToolCalls right
after running them, independent of the content-streaming gate below.
Updated TestRun_Stream_WithToolCalls, which asserted the old (buggy)
1-chunk behavior.
|
||
|---|---|---|
| .. | ||
| integration_test.go | ||
| loop.go | ||
| loop_test.go | ||
| README.es.md | ||
| README.md | ||
| subagent.go | ||
| subagent_test.go | ||
pkg/agent
The main loop that runs an LLM agent with guardrails.
Responsibility
Coordinate the iterative cycle between the LLM and tool execution:
while iteration < MaxIterations:
response = llm.Generate(messages, tools)
if no tool calls: return response
for tool_call in response.ToolCalls:
if needs_approval: ask_user()
result = execute(tool_call)
append tool result to messages
Public API
type Loop interface {
Run(ctx context.Context, input string, history ...llm.Message) (Response, error)
RunStream(ctx context.Context, input string, history ...llm.Message) iter.Seq2[Chunk, error]
}
type Config struct {
LLM llm.LLMClient
Persona persona.Persona
Tools tools.Registry
Sandbox Sandbox
MaxIters int
Approver Approver // nil = auto-approve all
OnIteration func(Iteration) // observability hook
}
type Response struct {
Content string
ToolCalls []tools.Call
Iterations int
Duration time.Duration
TokenUsage llm.TokenUsage
}
Guarantees
- Termination: Always terminates (max iterations, error, or final response)
- Idempotency: Re-running with the same input produces the same output (given the same LLM)
- Observability: Each iteration emits an OpenTelemetry span
- Approval: Destructive tools (
Askpermission) require confirmation
See also
- pkg/tools — Tool execution
- pkg/llm — LLMClient interface
- pkg/persona — Persona assembly