Skip to main content

Module agent_loop

Module agent_loop 

Source
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§

AgentLoop
Drives an LLM through a bounded multi-turn tool-calling conversation.
AgentLoopConfig
Tuning knobs for a single agent-loop run.
CadenceConfig
Tuning knobs for the #2346 cadence compressor.
CompactionConfig
Tuning knobs for non-destructive context compaction (#2070, §5.4).
GoalSlot
One occupied goal slot: its text, who wrote it, and when.
GoalSlots
Exactly GOAL_SLOT_COUNT fixed, privileged goal slots (#2347).
Transcript
Running conversation history for one agent-loop run.

Enums§

AgentLoopError
Failure modes of AgentLoop::run.
GoalSlotError
Slot-index validation failure.
GoalSource
Who last wrote a given GoalSlot.

Constants§

GOAL_SLOT_COUNT
The number of fixed goal slots (#2343 design decision: exactly 5).

Traits§

ToolEventSink
Observes tool dispatch lifecycle events from an AgentLoop run.

Functions§

resolve_cadence_config
Resolve a project’s CadenceConfig via the code_harness.* .claude/settings.json precedence chain crate::mode::resolve_mode establishes, adapted to this config’s two knobs.

Type Aliases§

FinishGate
Verify-before-finish gate hook (#2279): inspects the run’s own Transcript at the exact moment a successful finish_task call would otherwise terminate the loop, and may reject it with a human-readable reason instead.