Agent Loop
internal/agent is the strict inner-loop kernel. It is reusable execution
machinery, not the Anyy product runtime.
Loop Responsibilities
The loop accepts a RunRequest, streams with an LLMProvider, records assistant
messages, executes requested AgentTool calls, appends tool-result messages,
and repeats until completion or a stop condition.
Continue resumes with extra steering messages. Cancellation comes from the
context passed by gateway/runtime.
Runtime Responsibilities
Runtime code supplies provider and tool interfaces. It owns persistence, approvals, ChangePlan checks, audit, model fallback, context assembly, memory, skills, channel routing, and user-facing delivery.
Those concerns enter the loop through request fields and hooks, not through
imports from internal/agent.
Messages And Tool Calls
Loop messages are AgentMessage values. Provider adapters convert them into the
wire format for each model API and convert streamed deltas back into loop events.
Tool calls are complete when they reach the loop. Partial provider-specific tool argument assembly belongs in the provider adapter.
Interrupts
The loop stops when its context is cancelled or when runtime hooks return a runtime tool error. Gateway exposes this through session stop, interrupt, queue, and steering RPCs.
Runtime may inject steering into the next tool result. The loop itself does not understand TUI commands or channel commands.
Compaction
The loop can produce a final no-tool summary request when it reaches the turn iteration budget. Session compression, memory suggestions, raw retention, and summary storage are runtime/state responsibilities.