Expand description
Multi-turn agent loop driving an LLM through tool calls to completion.
Why: A task needing file reads, tool runs, and reaction to results cannot be
done in one chat call; the loop is the bounded control structure that calls
the model, dispatches gated tool calls, feeds results back, and iterates.
What: AgentLoop, AgentLoopConfig, AgentLoopError, and Transcript. The
loop is bounded by a turn cap and a wall-clock timeout, accrues token usage
via PerfCollector, and returns an AgentOutput.
Test: agent_loop::tests::* (stubbed two-turn flow, turn-cap abort,
recoverable tool error, usage accrual) plus an #[ignore]-gated live test.
Multi-turn agent loop: drive an LLM through tool calls until it stops.
Why: A single chat call cannot accomplish a task that requires reading files,
running tools, and reacting to results. The agent loop is the control
structure that repeatedly calls the model, dispatches any requested tool
calls through the gated registry, feeds results back, and iterates until the
model produces a final answer — bounded by a turn cap, a wall-clock timeout,
and token-usage accounting so runs stay cheap and observable.
What: Exposes AgentLoop, AgentLoopConfig, AgentLoopError, and (#2056)
ToolEventSink. run seeds a Transcript, then loops: build a
ChatRequest from the running history + registry schemas, call
LlmClientTrait::chat, accrue usage into a PerfCollector, append the
assistant turn, and — if there are tool calls — dispatch each via
ToolRegistry::dispatch_gated (notifying the optional sink around each
dispatch) and append the results. Exits with AgentOutput in either of two
ways: the first assistant turn that has NO tool calls (the D3 no-tool-call
finish convention, preserved unchanged as the fallback), or (#2072) an
explicit, successfully-validated finish_task tool call, whose structured
status/summary/changes/test-count fields become the final
AgentOutput via build_finish_output — UNLESS (#2279) an attached
FinishGate rejects it first, downgrading it to a recoverable retry so
the model gets another turn instead of finishing prematurely. Aborts with
a partial output when the turn cap, timeout, (#2056) an external
cancellation flag, or (#2265 fix #5) an external stop signal fires.
What: The whole chat → dispatch → iterate body runs inside a single
tokio::time::timeout so a stalled model or hung tool cannot block forever.
(#2344) run is now a thin wrapper over [AgentLoop::run_with_transcript],
which drives the SAME loop body against an already-prepared Transcript
instead of always seeding fresh — the entry point
task::executor::run_and_record uses so a session’s PM conversation
persists and grows across repeated task.run calls instead of restarting
from scratch each time.
Test: agent_loop::tests — stubbed two-turn flow, turn-cap abort, recoverable
tool-error continuation, usage accrual, sink notification order, cancellation,
plus an #[ignore]-gated live test.
Structs§
- Agent
Loop - Drives an LLM through a bounded multi-turn tool-calling conversation.
- Agent
Loop Config - Tuning knobs for a single agent-loop run.
- Cadence
Config - Tuning knobs for the #2346 cadence compressor.
- Compaction
Config - Tuning knobs for non-destructive context compaction (#2070, §5.4).
- Goal
Slot - One occupied goal slot: its text, who wrote it, and when.
- Goal
Slots - Exactly
GOAL_SLOT_COUNTfixed, privileged goal slots (#2347). - Transcript
- Running conversation history for one agent-loop run.
Enums§
- Agent
Loop Error - Failure modes of
AgentLoop::run. - Goal
Slot Error - Slot-index validation failure.
- Goal
Source - Who last wrote a given
GoalSlot.
Constants§
- GOAL_
SLOT_ COUNT - The number of fixed goal slots (#2343 design decision: exactly 5).
Traits§
- Tool
Event Sink - Observes tool dispatch lifecycle events from an
AgentLooprun.
Functions§
- resolve_
cadence_ config - Resolve a project’s
CadenceConfigvia thecode_harness.*.claude/settings.jsonprecedence chaincrate::mode::resolve_modeestablishes, adapted to this config’s two knobs.
Type Aliases§
- Finish
Gate - Verify-before-finish gate hook (#2279): inspects the run’s own
Transcriptat the exact moment a successfulfinish_taskcall would otherwise terminate the loop, and may reject it with a human-readable reason instead.