一键导入
ralph-loop
Use when task scope exceeds one context window and decomposes into independent pass/fail stories with clean iteration boundaries
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
Use when task scope exceeds one context window and decomposes into independent pass/fail stories with clean iteration boundaries
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
基于 SOC 职业分类
| name | ralph-loop |
| description | Use when task scope exceeds one context window and decomposes into independent pass/fail stories with clean iteration boundaries |
| tags | ["process","execution","autonomous","loop"] |
| triggers | ["large scope","multiple stories","too big for one session","autonomous loop","training loop"] |
| chains_to | ["story-decomposition","prd-generator","verification-before-completion"] |
| priority | escalation |
| gate | false |
For tasks too large for one context window: decompose into stories, generate prd.json, activate the Stop hook loop, and iterate inside the current session until all stories pass.
ALL of these must be true:
executing-plans)The ralph loop uses Claude Code's Stop hook to keep the session alive:
.claude/ralph-loop.local.md (state file with loop prompt)This is fully automatic. The planning chain activates it when scope demands it.
During writing-plans, assess whether the task triggers ralph-loop conditions:
| Signal | Threshold |
|---|---|
| Estimated stories | 4+ independent stories |
| Estimated changes | 500+ lines across 10+ files |
| Context window fit | Plan + implementation won't fit in one session |
| Story independence | Each story is verifiable in isolation |
If ALL signals fire, propose the training loop to the user.
Chain to story-decomposition then prd-generator. This generates in .claude/plugin/ralph/:
prd.json — stories with passes: false initiallyprogress.txt — human-readable progress lognotes/ — per-story implementation notes (written during iterations)Present the decomposition and ask for approval before activating:
This task would benefit from a ralph loop.
**Stories**: N stories, max 20 iterations
**Mechanism**: Stop hook — runs inside this session, fully interactive
**Completion**: Automatic when all prd.json stories pass
Stories:
1. [Title] — [one-line goal]
2. [Title] — [one-line goal]
...
Ready to start? I'll activate the loop now.
Do not activate without user approval.
After approval, write the state file .claude/ralph-loop.local.md:
---
active: true
iteration: 1
session_id: <CLAUDE_CODE_SESSION_ID>
max_iterations: 20
prd_path: .claude/plugin/ralph/prd.json
started_at: "<current ISO 8601 timestamp>"
---
Read .claude/plugin/ralph/prd.json. Find the first story where passes is false.
Check its dependencies are met (all dependency story IDs must have passes: true).
Implement the story following its acceptance criteria. Run the verification
command. If it passes, update prd.json (set passes: true, increment
completedStories) and progress.txt (mark [x]). Write implementation notes to
.claude/plugin/ralph/notes/story-<id>.md. One story per iteration.
If you are stuck on a story after 3 attempts, or blocked on something requiring
human input, delete .claude/ralph-loop.local.md and explain what happened.
Write this file using the Write tool. The session_id should be read from the CLAUDE_CODE_SESSION_ID environment variable via Bash. The max_iterations defaults to 20 but can be adjusted based on story count or user preference.
Then immediately begin working on the first story.
Each iteration (after the Stop hook re-injects the prompt):
passes is falsepasses: false, skip to the next eligible storypasses: true in prd.json for this storycompletedStories in prd.json[x] in progress.txtnotes/story-<id>.mdONE story per iteration. Do not attempt multiple stories in one pass.
Claude MUST delete .claude/ralph-loop.local.md and stop when:
When self-exiting, explain:
After the loop ends (all stories pass, self-exit, or max iterations):
.claude/plugin/ralph/notes/verification-before-completion| Exit | Trigger | Who |
|---|---|---|
| All stories pass | prd.json check in Stop hook | Automatic |
| Claude bails out | Claude deletes state file | Agent |
| Safety net | max_iterations reached | Automatic |
During the loop:
# Current iteration
grep '^iteration:' .claude/ralph-loop.local.md
# Story status
jq '.stories[] | {title, passes}' .claude/plugin/ralph/prd.json
# Progress
cat .claude/plugin/ralph/progress.txt
| Anti-Pattern | Why It's Wrong |
|---|---|
| Using ralph-loop for small tasks | Overhead exceeds benefit. Just execute the plan. |
| Stories without verification commands | The loop can't check if stories pass. |
| Attempting multiple stories per iteration | Context overflow; defeats the loop's purpose. |
| Skipping user approval | The user must approve the decomposition before activation. |
| Ignoring self-exit rules | Leads to infinite loops. Delete the state file when stuck. |
| Running without a worktree on sensitive branches | Partial failures pollute the branch. |
REQUIRED: syntaxninja-dojo:story-decomposition (to produce the stories) OPTIONAL: syntaxninja-dojo:prd-generator (for the PRD document) REQUIRED: syntaxninja-dojo:verification-before-completion (after loop completes)
Use when a structured PRD document is needed for the ralph loop — generates prd.json and progress.txt
Use when implementation is complete, all tests pass, and you need to decide how to integrate the work — merge, PR, or cleanup
Use when starting feature work that needs isolation from the current workspace or before executing plans that could leave the branch in a broken state
Use when a hard problem was just solved and the solution should be captured as a learning for future reference
Use when 3+ distinct occurrences suggest a skill should be updated with a new trigger, anti-pattern, or verification step
Use when creating a new skill or pattern for the dojo — guides the SKILL.md and PATTERN.md format, CSO lint rules, and index registration