Autonomous Loop Patterns
Parent: AI Agent Ecosystems · Published reference · snapshot 2026-09-08 · skill ai-agents-orchestration/references/autonomous-loops.md
↓ Facts as markdownall context files
> 兼容性说明 (v1.8.0): autonomous-loops 保留一个发布周期。
These notes link each claim to its source. Some citations identify archived research reports that are no longer hosted on this site. A published reference means the content is available; it does not certify independent review or accuracy.Read the editorial policy and follow the sources before relying on a claim.
自主循环技能
- > 兼容性说明 (v1.8.0): autonomous-loops 保留一个发布周期。 > 规范的技能名称现在是 continuous-agent-loop。新的循环指南应在此处编写,而此技能继续可用以避免破坏现有工作流。 [archived source: https://llms-explorer.com/sources/mdb-context-hub/autonomous-loops/#自主循环技能]
- 在循环中自主运行 Claude Code 的模式、架构和参考实现。涵盖从简单的 claude -p 管道到完整的 RFC 驱动的多智能体 DAG 编排的一切。 [archived source: https://llms-explorer.com/sources/mdb-context-hub/autonomous-loops/#自主循环技能]
何时使用
- 为你的问题选择正确的循环架构(简单与复杂) [archived source: https://llms-explorer.com/sources/mdb-context-hub/autonomous-loops/#何时使用]
1. 顺序管道 (`claude -p`)
- 最简单的循环。 将日常开发分解为一系列非交互式 claude -p 调用。每次调用都是一个具有清晰提示的专注步骤。 [archived source: https://llms-explorer.com/sources/mdb-context-hub/autonomous-loops/#1-顺序管道-claude--p]
核心见解
- > 如果你无法想出这样的循环,那意味着你甚至无法在交互模式下驱动 LLM 来修复你的代码。 [archived source: https://llms-explorer.com/sources/mdb-context-hub/autonomous-loops/#核心见解]
- claude -p 标志以非交互方式运行 Claude Code 并附带提示,完成后退出。链式调用来构建管道: [archived source: https://llms-explorer.com/sources/mdb-context-hub/autonomous-loops/#核心见解]
关键设计原则
- 每个步骤都是隔离的 - 每次 claude -p 调用都是一个新的上下文窗口,意味着步骤之间没有上下文泄露。 [archived source: https://llms-explorer.com/sources/mdb-context-hub/autonomous-loops/#关键设计原则]
- 顺序很重要 - 步骤按顺序执行。每个步骤都建立在前一个步骤留下的文件系统状态之上。 [archived source: https://llms-explorer.com/sources/mdb-context-hub/autonomous-loops/#关键设计原则]
- 否定指令是危险的 - 不要说“不要测试类型系统。”相反,添加一个单独的清理步骤(参见去草率化模式)。 [archived source: https://llms-explorer.com/sources/mdb-context-hub/autonomous-loops/#关键设计原则]
- 退出代码会传播 - set -e 在失败时停止管道。 [archived source: https://llms-explorer.com/sources/mdb-context-hub/autonomous-loops/#关键设计原则]
变体
- 使用 --allowedTools 限制: [archived source: https://llms-explorer.com/sources/mdb-context-hub/autonomous-loops/#变体]
2. NanoClaw REPL
- ECC 内置的持久循环。 一个具有会话感知的 REPL,它使用完整的对话历史同步调用 claude -p。 [archived source: https://llms-explorer.com/sources/mdb-context-hub/autonomous-loops/#2-nanoclaw-repl]
工作原理
- 从 ~/.claude/claw/{session}.md 加载对话历史 [archived source: https://llms-explorer.com/sources/mdb-context-hub/autonomous-loops/#工作原理]
- 每个用户消息都连同完整历史记录作为上下文发送给 claude -p [archived source: https://llms-explorer.com/sources/mdb-context-hub/autonomous-loops/#工作原理]
- 响应被追加到会话文件中(Markdown 作为数据库) [archived source: https://llms-explorer.com/sources/mdb-context-hub/autonomous-loops/#工作原理]
NanoClaw 与顺序管道的选择
- 有关完整详情,请参阅 /claw 命令文档。 [archived source: https://llms-explorer.com/sources/mdb-context-hub/autonomous-loops/#nanoclaw-与顺序管道的选择]
3. 无限智能体循环
- 一个双提示系统,用于编排并行子智能体以进行规范驱动的生成。由 disler 开发(致谢:@disler)。 [archived source: https://llms-explorer.com/sources/mdb-context-hub/autonomous-loops/#3-无限智能体循环]
模式
- 规范分析 - 编排器读取一个定义要生成内容的规范文件(Markdown) [archived source: https://llms-explorer.com/sources/mdb-context-hub/autonomous-loops/#模式]
- 目录侦察 - 扫描现有输出以找到最高的迭代编号 [archived source: https://llms-explorer.com/sources/mdb-context-hub/autonomous-loops/#模式]
- 并行部署 - 启动 N 个子智能体,每个都有: [archived source: https://llms-explorer.com/sources/mdb-context-hub/autonomous-loops/#模式]
- 波次管理 - 对于无限模式,部署 3-5 个智能体的波次,直到上下文耗尽 [archived source: https://llms-explorer.com/sources/mdb-context-hub/autonomous-loops/#模式]
通过 Claude Code 命令实现
- 创建 .claude/commands/infinite.md: [archived source: https://llms-explorer.com/sources/mdb-context-hub/autonomous-loops/#通过-claude-code-命令实现]
关键见解:通过分配实现唯一性
- 不要依赖智能体自我区分。编排器分配给每个智能体一个特定的创意方向和迭代编号。这可以防止并行智能体之间的概念重复。 [archived source: https://llms-explorer.com/sources/mdb-context-hub/autonomous-loops/#关键见解通过分配实现唯一性]
4. 持续 Claude PR 循环
- 一个生产级的 shell 脚本,在持续循环中运行 Claude Code,创建 PR,等待 CI,并自动合并。由 AnandChowdhary 创建(致谢:@AnandChowdhary)。 [archived source: https://llms-explorer.com/sources/mdb-context-hub/autonomous-loops/#4-持续-claude-pr-循环]
跨迭代上下文:SHARED\_TASK\_NOTES.md
- 关键创新:一个 SHARED_TASK_NOTES.md 文件在迭代间持久存在: [archived source: https://llms-explorer.com/sources/mdb-context-hub/autonomous-loops/#跨迭代上下文shared_task_notesmd]
- Claude 在迭代开始时读取此文件,并在迭代结束时更新它。这弥合了独立 claude -p 调用之间的上下文差距。 [archived source: https://llms-explorer.com/sources/mdb-context-hub/autonomous-loops/#跨迭代上下文shared_task_notesmd]
CI 失败恢复
- 当 PR 检查失败时,持续 Claude 会自动: [archived source: https://llms-explorer.com/sources/mdb-context-hub/autonomous-loops/#ci-失败恢复]
- 通过 gh run list 获取失败的运行 ID [archived source: https://llms-explorer.com/sources/mdb-context-hub/autonomous-loops/#ci-失败恢复]
- 生成一个新的带有 CI 修复上下文的 claude -p [archived source: https://llms-explorer.com/sources/mdb-context-hub/autonomous-loops/#ci-失败恢复]
- Claude 通过 gh run view 检查日志,修复代码,提交,推送 [archived source: https://llms-explorer.com/sources/mdb-context-hub/autonomous-loops/#ci-失败恢复]
- 重新等待检查(最多 --ci-retry-max 次尝试) [archived source: https://llms-explorer.com/sources/mdb-context-hub/autonomous-loops/#ci-失败恢复]
完成信号
- Claude 可以通过输出一个魔法短语来发出“我完成了”的信号: [archived source: https://llms-explorer.com/sources/mdb-context-hub/autonomous-loops/#完成信号]
- 连续三次迭代发出完成信号会停止循环,防止在已完成的工作上浪费运行。 [archived source: https://llms-explorer.com/sources/mdb-context-hub/autonomous-loops/#完成信号]
5. 去草率化模式
- 任何循环的附加模式。 在每个实现者步骤之后添加一个专门的清理/重构步骤。 [archived source: https://llms-explorer.com/sources/mdb-context-hub/autonomous-loops/#5-去草率化模式]
问题
- 当你要求 LLM 使用 TDD 实现时,它对“编写测试”的理解过于字面: [archived source: https://llms-explorer.com/sources/mdb-context-hub/autonomous-loops/#问题]
- 测试验证 TypeScript 的类型系统是否有效(测试 typeof x === 'string') [archived source: https://llms-explorer.com/sources/mdb-context-hub/autonomous-loops/#问题]
- 对类型系统已经保证的东西进行过度防御的运行时检查 [archived source: https://llms-explorer.com/sources/mdb-context-hub/autonomous-loops/#问题]
为什么不使用否定指令?
- 在实现者提示中添加“不要测试类型系统”或“不要添加不必要的检查”会产生下游影响: [archived source: https://llms-explorer.com/sources/mdb-context-hub/autonomous-loops/#为什么不使用否定指令]
解决方案:单独的步骤
- 与其限制实现者,不如让它彻底。然后添加一个专注的清理智能体: [archived source: https://llms-explorer.com/sources/mdb-context-hub/autonomous-loops/#解决方案单独的步骤]
关键见解
- > 与其添加具有下游质量影响的否定指令,不如添加一个单独的去草率化步骤。两个专注的智能体胜过一个有约束的智能体。 [archived source: https://llms-explorer.com/sources/mdb-context-hub/autonomous-loops/#关键见解]
6. Ralphinho / RFC 驱动的 DAG 编排
- 最复杂的模式。 一个 RFC 驱动的多智能体管道,将规范分解为依赖关系 DAG,通过分层质量管道运行每个单元,并通过智能体驱动的合并队列落地。由 enitrat 创建(致谢:@enitrat)。 [archived source: https://llms-explorer.com/sources/mdb-context-hub/autonomous-loops/#6-ralphinho-rfc-驱动的-dag-编排]
RFC 分解
- 保持测试与实现在一起(永远不要分开“实现 X” + “测试 X”) [archived source: https://llms-explorer.com/sources/mdb-context-hub/autonomous-loops/#rfc-分解]
- 仅在实际存在代码依赖关系的地方设置依赖关系 [archived source: https://llms-explorer.com/sources/mdb-context-hub/autonomous-loops/#rfc-分解]
复杂度层级
- 这可以防止对简单更改进行昂贵的操作,同时确保架构更改得到彻底审查。 [archived source: https://llms-explorer.com/sources/mdb-context-hub/autonomous-loops/#复杂度层级]
独立的上下文窗口(消除作者偏见)
- 每个阶段在其自己的智能体进程中运行,拥有自己的上下文窗口: [archived source: https://llms-explorer.com/sources/mdb-context-hub/autonomous-loops/#独立的上下文窗口消除作者偏见]
- 关键设计: 审阅者从未编写过它要审阅的代码。这消除了作者偏见——这是自我审阅中遗漏问题的最常见原因。 [archived source: https://llms-explorer.com/sources/mdb-context-hub/autonomous-loops/#独立的上下文窗口消除作者偏见]
具有驱逐功能的合并队列
- 驱逐恢复: 被驱逐时,会捕获完整上下文(冲突文件、差异、测试输出)并反馈给下一个 Ralph 轮次的实现者: [archived source: https://llms-explorer.com/sources/mdb-context-hub/autonomous-loops/#具有驱逐功能的合并队列]
工作树隔离
- 每个单元在隔离的工作树中运行(使用 jj/Jujutsu,而不是 git): [archived source: https://llms-explorer.com/sources/mdb-context-hub/autonomous-loops/#工作树隔离]
- 同一单元的管道阶段共享一个工作树,在 research → plan → implement → test → review 之间保留状态(上下文文件、计划文件、代码更改)。 [archived source: https://llms-explorer.com/sources/mdb-context-hub/autonomous-loops/#工作树隔离]
关键设计原则
- 确定性执行 - 预先分解锁定并行性和顺序 [archived source: https://llms-explorer.com/sources/mdb-context-hub/autonomous-loops/#关键设计原则-1]
- 在杠杆点进行人工审阅 - 工作计划是单一最高杠杆干预点 [archived source: https://llms-explorer.com/sources/mdb-context-hub/autonomous-loops/#关键设计原则-1]
- 关注点分离 - 每个阶段在独立的上下文窗口中,由独立的智能体负责 [archived source: https://llms-explorer.com/sources/mdb-context-hub/autonomous-loops/#关键设计原则-1]
- 带上下文的冲突恢复 - 完整的驱逐上下文支持智能重试,而非盲目重试 [archived source: https://llms-explorer.com/sources/mdb-context-hub/autonomous-loops/#关键设计原则-1]
- 层级驱动的深度 - 琐碎更改跳过研究/审阅;大型更改获得最大审查 [archived source: https://llms-explorer.com/sources/mdb-context-hub/autonomous-loops/#关键设计原则-1]
- 可恢复的工作流 - 完整状态持久化到 SQLite;可从任何点恢复 [archived source: https://llms-explorer.com/sources/mdb-context-hub/autonomous-loops/#关键设计原则-1]
模式组合
- 顺序流水线 + 去草率化 - 最常见的组合。每个实现步骤都进行一次清理。 [archived source: https://llms-explorer.com/sources/mdb-context-hub/autonomous-loops/#模式组合]
- 连续 Claude + 去草率化 - 为每次迭代添加带有去草率化指令的 --review-prompt。 [archived source: https://llms-explorer.com/sources/mdb-context-hub/autonomous-loops/#模式组合]
- 任何循环 + 验证 - 在提交前,使用 ECC 的 /verify 命令或 verification-loop 技能作为关卡。 [archived source: https://llms-explorer.com/sources/mdb-context-hub/autonomous-loops/#模式组合]
- Ralphinho 在简单循环中的分层方法 - 即使在顺序流水线中,你也可以将简单任务路由到 Haiku,复杂任务路由到 Opus: [archived source: https://llms-explorer.com/sources/mdb-context-hub/autonomous-loops/#模式组合]
常见错误
- 没有退出条件的无限循环 - 始终设置最大运行次数、最大成本、最大持续时间或完成信号。 [archived source: https://llms-explorer.com/sources/mdb-context-hub/autonomous-loops/#常见错误]
- 迭代之间没有上下文桥接 - 每次 claude -p 调用都从头开始。使用 SHARED_TASK_NOTES.md 或文件系统状态来桥接上下文。 [archived source: https://llms-explorer.com/sources/mdb-context-hub/autonomous-loops/#常见错误]
- 重试相同的失败 - 如果一次迭代失败,不要只是重试。捕获错误上下文并将其提供给下一次尝试。 [archived source: https://llms-explorer.com/sources/mdb-context-hub/autonomous-loops/#常见错误]
- 使用负面指令而非清理过程 - 不要说“不要做 X”。添加一个单独的步骤来移除 X。 [archived source: https://llms-explorer.com/sources/mdb-context-hub/autonomous-loops/#常见错误]
- 所有智能体都在一个上下文窗口中 - 对于复杂的工作流,将关注点分离到不同的智能体进程中。审查者永远不应该是作者。 [archived source: https://llms-explorer.com/sources/mdb-context-hub/autonomous-loops/#常见错误]
- 在并行工作中忽略文件重叠 - 如果两个并行智能体可能编辑同一个文件,你需要一个合并策略(顺序落地、变基或冲突解决)。 [archived source: https://llms-explorer.com/sources/mdb-context-hub/autonomous-loops/#常见错误]
Where this helps
- Automating a well-defined multi-step coding task, such as a framework migration, by chaining focused claude -p steps instead of one long interactive session. [source] — AI-suggested, synthesized from this pack's existing facts — not extracted from a source document.
- Running unattended overnight or CI-triggered work, such as a continuous PR loop that opens a PR, waits on CI, and auto-fixes failures without a human watching each iteration. [source] — AI-suggested, synthesized from this pack's existing facts — not extracted from a source document.
- Generating many independent creative variations in parallel with the infinite agent loop pattern, when the goal is N distinct outputs against one spec rather than a single best-effort attempt. [source] — AI-suggested, synthesized from this pack's existing facts — not extracted from a source document.
- Large refactors or RFC-scale features that benefit from splitting research, planning, implementation, testing, and review into separate agent processes so no single agent reviews code it wrote itself. [source] — AI-suggested, synthesized from this pack's existing facts — not extracted from a source document.
Project ideas
- Build a sequential claude -p pipeline that breaks a recurring dev task, such as a dependency bump plus test fix plus changelog entry, into discrete non-interactive steps that each start from a clean context window. [source] — AI-suggested, synthesized from this pack's existing facts — not extracted from a source document.
- Implement a SHARED_TASK_NOTES.md-style file that persists across claude -p invocations so later steps can read what earlier steps discovered, closing the context gap between isolated calls. [source] — AI-suggested, synthesized from this pack's existing facts — not extracted from a source document.
- Add a de-sloppification step after an implementer step, as a separate cleanup or refactor agent invocation, instead of stacking negative instructions like “don't over-test” onto the implementer's prompt. [source] — AI-suggested, synthesized from this pack's existing facts — not extracted from a source document.
- Wire a continuous PR loop script that watches gh run list for CI failures and re-invokes claude -p with the failure context attached, so retries carry new information instead of blindly rerunning. [source] — AI-suggested, synthesized from this pack's existing facts — not extracted from a source document.
Antipatterns
- Running a loop with no exit condition — no max iteration count, cost cap, wall-clock limit, or completion signal — risking a runaway loop that burns budget on already-finished work. [source] — AI-suggested, synthesized from this pack's existing facts — not extracted from a source document.
- Piling negative instructions, such as “don't test the type system” or “don't add unnecessary checks,” onto a single implementer prompt instead of adding a dedicated cleanup step, which degrades downstream quality. [source] — AI-suggested, synthesized from this pack's existing facts — not extracted from a source document.
- Retrying an identical prompt after a failed iteration instead of capturing the failure's error context and feeding it into the next attempt. [source] — AI-suggested, synthesized from this pack's existing facts — not extracted from a source document.
- Letting parallel sub-agents self-differentiate instead of assigning each one an explicit direction and iteration number, which produces duplicate or converging outputs. [source] — AI-suggested, synthesized from this pack's existing facts — not extracted from a source document.
Known issues
- Each claude -p call starts a fresh context window, so nothing carries over between steps unless it is explicitly bridged with a file like SHARED_TASK_NOTES.md or filesystem state. [source] — AI-suggested, synthesized from this pack's existing facts — not extracted from a source document.
- The most complex pattern, RFC-driven DAG orchestration, depends on running each unit in an isolated worktree, which adds real operational overhead beyond what a simple sequential pipeline needs. [source] — AI-suggested, synthesized from this pack's existing facts — not extracted from a source document.
- Completion-signal detection, such as three consecutive iterations signaling done, is a heuristic rather than a guarantee — a loop can plateau on subtly wrong output and still emit the signal. [source] — AI-suggested, synthesized from this pack's existing facts — not extracted from a source document.
- A merge queue with eviction, as used in the DAG-orchestration pattern, needs its own recovery path for captured conflict files, diffs, and test output, or an evicted unit's work is simply lost on the next round. [source] — AI-suggested, synthesized from this pack's existing facts — not extracted from a source document.
Children
- Sequential Pipeline Patterns (frontier)
- Infinite Agent Loops (frontier)
- RFC-Driven DAG Orchestration (frontier)
- NanoClaw REPL (frontier)
Frontier under this node: Infinite Agent Loops, NanoClaw REPL, RFC-Driven DAG Orchestration, Sequential Pipeline Patterns