Skip to content

Day 20 — A Bounded Agent Loop

Today Forge becomes an agent loop. The hard part is not repetition; it is knowing whether an iteration made verified progress and when the system must stop.

Terminal window
cd rust/rust-ai-engineering
cargo test -p mosaic-harness agent_loop
cargo run -q -p mosaic-harness --example bounded_agent_loop_lab
pub enum TerminalState {
Completed,
BudgetExhausted(BudgetKind),
NoProgress,
RepeatedAction,
PolicyDenied,
NeedsReconciliation,
Failed,
}

Completion is not the string “done.” It requires the behavior contract’s completion evidence. Progress requires a successful state change plus new independently identified evidence.

Before a model or tool call, reserve from the relevant budget. On known failure, release or commit according to policy. On an indeterminate external effect, stop for reconciliation—blind retry may duplicate a write.

Track maximum steps, model calls, tool calls, repairs, wall time, output bytes, and cost. One loop owner decides termination precedence.

A malformed structured output may get bounded repair feedback. A provider timeout follows retry policy. A policy denial is not repaired by asking more persuasively. Keep these loops distinct.

Add mutations where the model:

  • repeats a successful action;
  • claims completion without a receipt;
  • changes text but not state;
  • consumes a budget without accounting;
  • proposes a step after a terminal event;
  • receives an indeterminate write result.

The replayed receipt must reach the same terminal state without calling a model.

Draw the state machine, name every terminal, and prove one run for successful completion, repeated action, no progress, exhausted budget, and uncertain effect.

Deep reference: Agent loops, progress, stopping, and repair.

Next: Day 21 — Authorization →.