Notes

Loops, levels, and building a platform that drives itself

Read as
working implementation · tests
Build treatmentUpdated 14 Jul 2026

Agent operations · Bounded loops

What you will leave with

Build a loop that verifies each transition, stops deterministically, and escalates without losing context.

For engineers implementing a bounded worker loop with explicit state and evidence.~3 min read
PrerequisitesA repeatable taskA deterministic verifierSomewhere durable to record attempts
01 · System view

What you will build

One persisted state machine around a narrow action and an independent verifier.

  1. 01Load stateResume the same attempt safely after interruption.
  2. 02ActPerform one bounded, idempotent operation.
  3. 03ObserveCapture artifact state rather than worker confidence.
  4. 04DecideSucceed, retry, stop, or escalate.
  5. 05PersistRecord transition, budget, evidence, and next action.
02 · Implementation

Build it in sequence

01

Define the state machine first

Name success, retryable failure, terminal failure, waiting, budget exhaustion, and human review. If two states look the same to the code, the loop will eventually choose the unsafe one.

loop-state.tstypescript
type LoopState =
  | 'ready'
  | 'waiting-for-evidence'
  | 'retryable-failure'
  | 'terminal-failure'
  | 'human-review'
  | 'succeeded'
02

Make one iteration idempotent

Give every attempt an idempotency key and check current external state before acting. A process restart must not duplicate a ticket, deployment, payment, notification, or merge.

03

Verify before the next action

Bind the observation to the artifact produced by this iteration. Do not let a green check for an earlier commit or cached deployment advance the current loop.

loop/verify.ts · public contracttypescript
if (evidence.commit !== attempt.commit) return 'terminal-failure'
if (evidence.deployment !== attempt.deployment) return 'terminal-failure'
if (evidence.required.some((check) => check.status !== 'success')) return 'wait'
return 'succeeded'
04

Spend from explicit budgets

Track attempts, wall time, tokens, tool calls, and external mutations separately. Exhaustion produces an escalation; it never silently buys more budget.

05

Return a decision packet

On stop or escalation, report the state, attempts, last verified artifact, contradiction, reversible options, and recommended next action, not a transcript.

03 · Review

Definition of done

  • Success and terminal failure are observable and distinct.
  • One iteration is safe to retry.
  • Evidence is bound to this attempt's artifact.
  • Every resource has a hard ceiling.
  • Restart resumes state without duplicating side effects.
  • Escalation contains a recommendation and rollback context.
04 · Failure modes

What breaks and what it means

The loop keeps making plausible changes without converging.

The verifier is too weak or the stopping condition measures activity rather than outcome.

Retries duplicate external actions.

Add an idempotency key and observe current state before mutation.

Public discussion

Questions, corrections, and useful disagreement

Moderated on GitHub ↗

Sign in with GitHub to join the conversation. Comments are public. Article views are anonymous and counted once per browser, per article, each day.

Read next06

I ran the robot factory by hand first

A full manual dress-rehearsal of the L5 feedback-to-fix factory turned eight pages of handwritten notes into 24 findings, four tickets, and four parallel build lanes. It taught me what to automate before automating it.

3 depths