微算力 · API 导航与自营网关
API 接入 EN
未登录登录
🔁

AI 编码智能体循环编排引擎

Ralph Loop (Agent Mode) · v1.1.0
专业技能 安全 A 级 作者 addozhang 公开目录收录
下载 2.9k 安装 0 收藏 8.8k 浏览 45
原始页面

简介

通过 exec/process 工具编排 AI 编码智能体的循环工作流,支持 TTY 模式运行 OpenCode、Codex、Claude Code 等交互式 CLI。

原始描述

Guide OpenClaw agents to execute Ralph Wiggum loops using exec and process tools. Agent orchestrates coding agents (Codex, Claude Code, OpenCode, Goose) with proper TTY support via pty:true. Plans/builds code via PROMPT.md + AGENTS.md, SPECS and IMPLEMENTATION_PLAN.md. Includes PLANNING vs BUILDING modes, backpressure, sandboxing, and completion conditions. Users request loops, agents execute using tools.

中文使用说明 来自收录源

核心用法

Ralph Loop 是一种用于编排 AI 编码智能体的自动化工作流技能,指导 OpenClaw 代理使用 exec 和 process 工具执行循环任务。其核心模式分为两阶段:

  1. PLANNING 循环:分析需求、创建 specs/*.md 规格文档,仅更新 IMPLEMENTATION_PLAN.md 而不实施代码
  2. BUILDING 循环:按优先级执行任务、实施代码、运行测试(backpressure)、更新计划并提交

关键创新在于使用 exec 工具的 pty: true 参数为交互式 CLI(如 OpenCode、Codex、Claude Code、Goose)提供 TTY 支持,解决传统后台执行导致的挂起问题。配合 background: true 和 process 工具的 poll/log/kill 动作,实现会话监控、实时日志获取和安全清理。

显著优点

  • TTY 兼容性:解决交互式编码智能体的终端依赖问题,支持主流 AI CLI 工具
  • 结构化工作流:明确的规划/实施分离,通过 PROMPT.md 和 AGENTS.md 持久化上下文
  • 灵活监控:基于会话 ID 的轮询机制,支持超时控制和并行会话
  • 完成检测:通过 IMPLEMENTATION_PLAN.md 中的状态标记(如 STATUS: COMPLETE)自动识别任务结束
  • 多智能体支持:可编排 Codex、Claude Code、OpenCode、Goose、Pi 等多种编码代理

潜在局限

  • 复杂度门槛:需要理解 exec/process 工具链、PTY 机制及多种 CLI 的特定参数
  • 安全风险:--full-auto、--yolo、--dangerously-skip-permissions 等自动授权标志可能导致未审查代码执行
  • 超时调优:默认 3600 秒超时可能不适合所有任务规模,需人工调整
  • 环境依赖:部分 CLI(如 Codex)要求 git 仓库上下文
  • 无内置沙箱:虽有建议但未强制实施容器隔离,依赖外部沙箱方案

适合人群

  • 需要批量自动化 AI 编码任务的开发团队
  • 熟悉 OpenClaw 工具链和多种 AI CLI 的高级用户
  • 追求规划-实施分离、可审计工作流的工程管理者
  • 希望并行运行多个编码会话的技术负责人

常规风险

  • 代码质量风险:自动授权模式可能引入未经审查的代码变更
  • 资源泄漏:不当的会话管理可能导致僵尸进程或文件句柄泄漏
  • 并发冲突:多会话并行时可能产生 git 冲突或文件竞争
  • 凭证暴露:交互式 CLI 可能意外记录敏感信息到日志文件
  • 无限循环:完成检测失败或提示词设计不当可能导致循环无法终止

建议使用 git worktree 隔离并行任务,在受控沙箱环境中运行,并始终保留人工审查环节。

技能正文


name: ralph-loop description: Guide OpenClaw agents to execute Ralph Wiggum loops using exec and process tools. Agent orchestrates coding agents (Codex, Claude Code, OpenCode, Goose) with proper TTY support via pty:true. Plans/builds code via PROMPT.md + AGENTS.md, SPECS and IMPLEMENTATION_PLAN.md. Includes PLANNING vs BUILDING modes, backpressure, sandboxing, and completion conditions. Users request loops, agents execute using tools. version: 1.1.0 author: OpenClaw Community keywords: [ralph-loop, ai-agent, coding-agent, pty, tty, automation, loop, opencode, codex, claude, goose, exec-tool, process-tool] license: MIT

Ralph Loop

Overview

This skill guides OpenClaw agents to execute Ralph Loop workflows using the exec and process tools. The agent orchestrates AI coding agent sessions following the Ralph playbook flow:

  1. Define Requirements → JTBD → Focus Topics → specs/*.md
  2. PLANNING Loop → Create/update IMPLEMENTATION_PLAN.md (do not implement)
  3. BUILDING Loop → Implement tasks, run tests (backpressure), update plan, commit

The loop persists context via PROMPT.md + AGENTS.md (loaded each iteration) and the plan/specs on disk.

How This Skill Works

This skill generates instructions for OpenClaw agents to execute Ralph Loops using the exec and process tools.

  • The agent calls exec tool with the coding agent command
  • Uses pty: true to provide TTY for interactive CLIs
  • Uses background: true for monitoring capabilities
  • Uses process tool to monitor progress and detect completion

Important: Users don't run these scripts directly - the OpenClaw agent executes them using its tool capabilities.


TTY Requirements

Some coding agents require a real terminal (TTY) to work properly, or they will hang:

Interactive CLIs (need TTY):

  • OpenCode, Codex, Claude Code, Pi, Goose

Non-interactive CLIs (file-based):

  • aider, custom scripts

Solution: Use exec + process mode for interactive CLIs, simple loops for file-based tools.


Agent Tool Usage Patterns

Interactive CLIs (Recommended Pattern)

For OpenCode, Codex, Claude Code, Pi, and Goose - these require TTY support:

When I (the agent) receive a Ralph Loop request, I will:

  1. Use exec tool to launch the coding agent:

    exec tool with parameters:
    - command: "opencode run --model <MODEL> \"$(cat PROMPT.md)\""
    - workdir: <project_path>
    - background: true
    - pty: true
    - yieldMs: 60000
    - timeout: 3600
    
  2. Capture session ID from exec tool response

  3. Use process tool to monitor:

    process tool with:
    - action: "poll"
    - sessionId: <captured_session_id>
    
    process tool with:
    - action: "log"
    - sessionId: <captured_session_id>
    - offset: -30  (for recent output)
    
  4. Check completion by reading IMPLEMENTATION_PLAN.md for sentinel text

  5. Clean up with process kill if needed:

    process tool with:
    - action: "kill"
    - sessionId: <session_id>
    

Benefits: TTY support, real-time logs, timeout handling, parallel sessions, workdir isolation


Agent Workflow

1) Gather Inputs

Required:

  • Goal / JTBD
  • CLI (opencode, codex, claude, goose, pi, other)
  • Mode (PLANNING, BUILDING, or BOTH)
  • Max iterations (default: PLANNING=5, BUILDING=10)

Optional:

  • Completion sentinel (default: STATUS: COMPLETE in IMPLEMENTATION_PLAN.md)
  • Working directory (default: $PWD)
  • Timeout per iteration (default: 3600s)
  • Sandbox choice
  • Auto-approval flags (--full-auto, --yolo, --dangerously-skip-permissions)

Auto-detect:

  • If CLI in interactive list → use exec tool with pty: true
  • Extract model flag from CLI requirements

2) Requirements → Specs (Optional)

If requirements are unclear:

  • Break JTBD into focus topics
  • Draft specs/<topic>.md for each
  • Keep specs short and testable

3) PROMPT.md + AGENTS.md

PROMPT.md references:

  • specs/*.md
  • IMPLEMENTATION_PLAN.md
  • Relevant project files

AGENTS.md includes:

  • Test commands (backpressure)
  • Build/run instructions
  • Operational learnings

4) Prompt Templates

PLANNING Prompt (no implementation):

You are running a Ralph PLANNING loop for this goal: <goal>.

Read specs/* and the current codebase. Only update IMPLEMENTATION_PLAN.md.

Rules:
- Do not implement
- Do not commit
- Create a prioritized task list
- Write down questions if unclear

Completion:
When plan is ready, add: STATUS: PLANNING_COMPLETE

BUILDING Prompt:

You are running a Ralph BUILDING loop for this goal: <goal>.

Context: specs/*, IMPLEMENTATION_PLAN.md, AGENTS.md

Tasks:
1) Pick the most important task
2) Investigate code
3) Implement
4) Run backpressure commands from AGENTS.md
5) Update IMPLEMENTATION_PLAN.md
6) Update AGENTS.md with learnings
7) Commit with clear message

Completion:
When all done, add: STATUS: COMPLETE

5) CLI Command Reference

The agent constructs command strings using these patterns:

CLI Command String Pattern
OpenCode opencode run --model <MODEL> "$(cat PROMPT.md)"
Codex codex exec <FLAGS> "$(cat PROMPT.md)" (requires git)
Claude Code claude <FLAGS> "$(cat PROMPT.md)"
Pi pi --provider <PROVIDER> --model <MODEL> -p "$(cat PROMPT.md)"
Goose goose run "$(cat PROMPT.md)"

Common flags:

  • Codex: --full-auto, --yolo, --model <model>
  • Claude: --dangerously-skip-permissions

Detailed Agent Tool Usage Examples

Example 1: OpenCode Ralph Loop

Agent executes this sequence:

Step 1: Launch OpenCode with exec tool
{
  command: "opencode run --model github-copilot/claude-opus-4.5 \"$(cat PROMPT.md)\"",
  workdir: "/path/to/project",
  background: true,
  pty: true,
  timeout: 3600,
  yieldMs: 60000
}

Step 2: Capture session ID from response
sessionId: "abc123"

Step 3: Monitor with process tool every 10-30 seconds
{
  action: "poll",
  sessionId: "abc123"
}

Step 4: Check recent logs
{
  action: "log",
  sessionId: "abc123",
  offset: -30
}

Step 5: Read IMPLEMENTATION_PLAN.md to check for completion
- Look for: "STATUS: COMPLETE" or "STATUS: PLANNING_COMPLETE"

Step 6: If complete or timeout, cleanup
{
  action: "kill",
  sessionId: "abc123"
}

Example 2: Codex with Full Auto

Agent tool calls:

exec tool:
{
  command: "codex exec --full-auto --model anthropic/claude-opus-4 \"$(cat PROMPT.md)\"",
  workdir: "/path/to/project",
  background: true,
  pty: true,
  timeout: 3600
}

# Then monitor with process tool as above

Completion Detection

Use flexible regex to match variations:

grep -Eq "STATUS:?\s*(PLANNING_)?COMPLETE" IMPLEMENTATION_PLAN.md

Matches:

  • STATUS: COMPLETE
  • STATUS:COMPLETE
  • STATUS: PLANNING_COMPLETE
  • ## Status: PLANNING_COMPLETE

Safety & Safeguards

Auto-Approval Flags (Risky!)

  • Codex: --full-auto (sandboxed, auto-approve) or --yolo (no sandbox!)
  • Claude: --dangerously-skip-permissions
  • Recommendation: Use sandboxes (docker/e2b/fly) and limited credentials

Escape Hatches

  • Stop: Ctrl+C
  • Kill session: process tool with action: "kill"
  • Rollback: git reset --hard HEAD~N

Best Practices

  1. Start small: Test with 1-2 iterations first
  2. Workdir isolation: Prevent reading unrelated files
  3. Set timeouts: Default 1h may not fit all tasks
  4. Monitor actively: Check logs, don't terminate prematurely
  5. Requirements first: Clear specs before building
  6. Backpressure early: Add tests from the start

Troubleshooting

Problem Solution
OpenCode hangs Ensure agent uses exec tool with pty: true
Session won't start Check CLI path, git repo, command syntax
Completion not detected Verify sentinel format in IMPLEMENTATION_PLAN.md
Process timeout Agent should increase timeout parameter or simplify tasks
Parallel conflicts Agent should use git worktrees for isolation
Can't see progress Agent should use process tool with action: "log"

License

MIT

Credits

This skill builds upon work by:

  • @jordyvandomselaar - Original Ralph Loop concept and workflow design
  • @steipete - Coding agent patterns and exec/process tool usage with pty support

Key improvement: Uses OpenClaw's exec tool with pty: true to provide TTY for interactive CLIs, solving the hanging issue that occurs with simple background bash execution.

正文来自原始发布页, 版权归作者所有,许可未标注。 权利人认为有不妥,可申请下架。

标签

ai-agentcoding-automationcli-orchestrationopencodecodexclaudegooseptyworkflow-enginesandbox

同类 Skill