🐚 Ralph loop
The roughest and most famous pattern in the house: the Ralph loop (Geoffrey Huntley / HumanLayer). It’s an infinite shell loop that repeats the SAME prompt endlessly. Each pass mutates the code—and that mutated code becomes the starting state for the next pass.
What to look at: the same prompt goes into every round (the arrow doesn’t change); what changes is the code in the middle, which carries progress into the next iteration. It’s the externalized state from 4.1 taken to the extreme.
Objective of the code below
Run the same prompt in an infinite shell loop, letting the agent advance the task each round — without you retyping anything.
# O Ralph one-liner — cole no seu terminal, na raiz do projeto
# PROMPT.md contém a tarefa; troque o agente pela ferramenta que você usa.
while :; do cat PROMPT.md | npx --yes @sourcegraph/amp ; done
How to verify / when to use: run it in a disposable repository (preferably in an isolated worktree — topic 3) with a git status open in another tab: you'll see commits/changes appear on each turn. Use it for large, repetitive tasks with built-in verification; stop early — Huntley warns that running too long produces "bizarre emergent behavior." The infinite loop is powerful and dangerous in equal measure: ALWAYS pair it with guardrails (topic 4).
⏰ Scheduled loops
Owain Lewis (as reported) describes the “fleet” approach: a loop is a job the agent repeats — wakes up on a schedule or event, reads the state of the work, does ONE scoped job within fixed permissions, writes the result, sleeps, and repeats. It’s the opposite of Ralph: disciplined, small, controlled.
What to look at: it's not a genius agent doing everything—it's a fleet of small agents, each with a narrow job and fixed permissions. Coordination is external (the control plane), not in any agent's head.
🗂️ Manager (split)
Classifies the backlog: separates easy, low-risk work from what needs a human. Doesn't write code — just routes work.
🔧 Worker
Implement low-risk tickets and open PRs. Never merge. The human approves at the end — we keep the final gate.
Read the flow: GitHub Issues are the control plane — the shared work queue. The manager triages, the worker implements and opens a PR, and the last gate (the merge) is always human. No agent closes the loop alone.
🌲 Worktrees, sandboxes, and fixed permissions
To run several agents in parallel without them stepping on each other's toes—and without any of them doing something irreversible—you need isolation. Three pieces: worktrees, sandboxes, and fixed permissions.
🌲 Worktree
Isolate parallel work: each agent in its own worktree/branch, with no file conflicts.
📦 Sandbox
Isolate the environment: the agent runs somewhere where breaking things costs nothing outside it.
🔒 Fixed permissions
Define what it CAN do: that’s how you let a loop run without doing anything irreversible.
💡 The core idea behind fixed permissions
Fixed permissions are the autonomous loop's seat belt. You decide BEFOREHAND what the agent can touch (these files, these commands) and what's forbidden (prod, secrets, git push --force). Since nobody is watching every pass, the limit has to be in the environment, not in your attention.
🛡️ Guardrails = the real work
The grassroots community consensus is direct: "the guardrails are the real work, not the agents." The agent is the easy part. The hard part—and what separates a safe system from a disaster—is designing the boundaries. The worker opens a PR, but never merges it; a human approves it.
Objective of the prompt below
Give the fleet agent an explicit permissions contract—what it can do, what’s prohibited, and where it must stop and ask for human approval.
# Cole no topo do PROMPT.md do seu worker (ajuste os nomes ao seu repo)
REGRAS (não negociáveis):
- Trabalhe APENAS nos arquivos sob .
- Abra um Pull Request ao terminar. NUNCA dê merge você mesmo.
- NUNCA toque em produção, em segredos (.env, chaves) nem em CI/CD.
- PROIBIDO: git push --force, deletar branches, alterar o histórico.
- PARE e peça aprovação humana antes de: ,
ou .
- Se a verificação falhar 3 vezes, pare e escreva o que travou em BLOCKED.md.
How to verify: run the worker in a test worktree and check the result against every line — opened a PR without merging? didn't touch .env? did it stop when it hit a migration? If a rule was violated, the guardrail is too weak and the prompt needs to be more specific. The guardrail is only real when you test it against an agent trying to get around it.
🥊 Adversarial maker/checker & verification specialist
You saw Maker→Checker in Track 2. Here it levels up: the checker stays adversarial. Instead of just checking, a second agent (or panel) tries to BREAK the work — it’s prompted to refute it, find the flaw, prove it wrong.
🚨 Beware the source: speculation, not fact
Hesamation read Claude Code’s system prompt and interpreted that it would introduce a "verification specialist" subagent—but THEY THEMSELVES marked this as speculative inference, possibly inactive.
Treat the "verification specialist" as a useful design idea, not as a confirmed feature of any tool. The pattern (separating who does the work from who refutes it) is solid; the specific claim about the system prompt is speculation.
The adversarial difference: the checker doesn’t ask "is it good?" It asks "how can I prove it’s wrong?" It only approves when it CAN’T break it. The person who builds it never grades their own work—that’s the golden rule of verification taken to the attack.
🕸️ Multi-agent / fan-out / orchestration
The fleet grows: a manager prompting agents that prompt agents — "Russian nesting dolls," nesting within nesting. The pattern that organizes everything is a sequence: orchestrate → delegate (fan-out) → verify.
Read the fan-out: A (an orchestrator) → N (multiple agents in parallel) → 1 (a verifier that brings everything together). Parallelism adds speed; the final verification keeps N independent errors from turning into a polished, broken product.
🧠 Cross-trial memory (Reflexion)
The last pattern changes the unit of the loop: instead of one iteration, a episode entire. The cross-trial memory (memory between attempts, from the Reflexion paper) lets the loop span multiple attempts: act → receive feedback → reflect in text → save to episodic memory → try again, better, next time.
| Step | What happens |
|---|---|
| 1. Act | the agent tries to solve the task (the "trial") |
| 2. Feedback | receives a success/failure signal (tests, evaluator, environment) |
| 3. Reflect | writes down why it failed and what it would do differently |
| 4. Save | save this reflection to episodic memory |
| 5. Try again | reads the memory and makes a better attempt—without retraining anything |
🔗 Where this takes you
Reflexion is the “learns from mistakes” version of the loop. Combine it with the other patterns: a worker in a fleet that SAVES what it learned gets better with every ticket — without you adjusting anything. It’s also the root of the “comprehension debt” idea in 4.3: the more the system learns on its own, the less you understand why it does what it does.
In the scheduled fleet pattern, who merges the PR?
🧾 Module Summary
Next:
4.3 — Risks, cost, and case study (the course finale)