anvil-dev-framework 0.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (190) hide show
  1. package/README.md +719 -0
  2. package/VERSION +1 -0
  3. package/docs/ANVIL-REPO-IMPLEMENTATION-PLAN.md +441 -0
  4. package/docs/FIRST-SKILL-TUTORIAL.md +408 -0
  5. package/docs/INSTALLATION-RETRO-NOTES.md +458 -0
  6. package/docs/INSTALLATION.md +984 -0
  7. package/docs/anvil-hud.md +469 -0
  8. package/docs/anvil-init.md +255 -0
  9. package/docs/anvil-state.md +210 -0
  10. package/docs/boris-cherny-ralph-wiggum-insights.md +608 -0
  11. package/docs/command-reference.md +2022 -0
  12. package/docs/hooks-tts.md +368 -0
  13. package/docs/implementation-guide.md +810 -0
  14. package/docs/linear-github-integration.md +247 -0
  15. package/docs/local-issues.md +677 -0
  16. package/docs/patterns/README.md +419 -0
  17. package/docs/planning-responsibilities.md +139 -0
  18. package/docs/session-workflow.md +573 -0
  19. package/docs/simplification-plan-template.md +297 -0
  20. package/docs/simplification-principles.md +129 -0
  21. package/docs/specifications/CCS-RALPH-INTEGRATION-DESIGN.md +633 -0
  22. package/docs/specifications/CCS-RESEARCH-REPORT.md +169 -0
  23. package/docs/specifications/PLAN-ANV-verification-ralph-wiggum.md +403 -0
  24. package/docs/specifications/PLAN-parallel-tracks-anvil-memory-ccs.md +494 -0
  25. package/docs/specifications/SPEC-ANV-VRW/component-01-verify.md +208 -0
  26. package/docs/specifications/SPEC-ANV-VRW/component-02-stop-gate.md +226 -0
  27. package/docs/specifications/SPEC-ANV-VRW/component-03-posttooluse.md +209 -0
  28. package/docs/specifications/SPEC-ANV-VRW/component-04-ralph-wiggum.md +604 -0
  29. package/docs/specifications/SPEC-ANV-VRW/component-05-atomic-actions.md +311 -0
  30. package/docs/specifications/SPEC-ANV-VRW/component-06-verify-subagent.md +264 -0
  31. package/docs/specifications/SPEC-ANV-VRW/component-07-claude-md.md +363 -0
  32. package/docs/specifications/SPEC-ANV-VRW/index.md +182 -0
  33. package/docs/specifications/SPEC-ANV-anvil-memory.md +573 -0
  34. package/docs/specifications/SPEC-ANV-context-checkpoints.md +781 -0
  35. package/docs/specifications/SPEC-ANV-verification-ralph-wiggum.md +789 -0
  36. package/docs/sync.md +122 -0
  37. package/global/CLAUDE.md +140 -0
  38. package/global/agents/verify-app.md +164 -0
  39. package/global/commands/anvil-settings.md +527 -0
  40. package/global/commands/anvil-sync.md +121 -0
  41. package/global/commands/change.md +197 -0
  42. package/global/commands/clarify.md +252 -0
  43. package/global/commands/cleanup.md +292 -0
  44. package/global/commands/commit-push-pr.md +207 -0
  45. package/global/commands/decay-review.md +127 -0
  46. package/global/commands/discover.md +158 -0
  47. package/global/commands/doc-coverage.md +122 -0
  48. package/global/commands/evidence.md +307 -0
  49. package/global/commands/explore.md +121 -0
  50. package/global/commands/force-exit.md +135 -0
  51. package/global/commands/handoff.md +191 -0
  52. package/global/commands/healthcheck.md +302 -0
  53. package/global/commands/hud.md +84 -0
  54. package/global/commands/insights.md +319 -0
  55. package/global/commands/linear-setup.md +184 -0
  56. package/global/commands/lint-fix.md +198 -0
  57. package/global/commands/orient.md +510 -0
  58. package/global/commands/plan.md +228 -0
  59. package/global/commands/ralph.md +346 -0
  60. package/global/commands/ready.md +182 -0
  61. package/global/commands/release.md +305 -0
  62. package/global/commands/retro.md +96 -0
  63. package/global/commands/shard.md +166 -0
  64. package/global/commands/spec.md +227 -0
  65. package/global/commands/sprint.md +184 -0
  66. package/global/commands/tasks.md +228 -0
  67. package/global/commands/test-and-commit.md +151 -0
  68. package/global/commands/validate.md +132 -0
  69. package/global/commands/verify.md +251 -0
  70. package/global/commands/weekly-review.md +156 -0
  71. package/global/hooks/__pycache__/ralph_context_monitor.cpython-314.pyc +0 -0
  72. package/global/hooks/__pycache__/statusline_agent_sync.cpython-314.pyc +0 -0
  73. package/global/hooks/anvil_memory_observe.ts +322 -0
  74. package/global/hooks/anvil_memory_session.ts +166 -0
  75. package/global/hooks/anvil_memory_stop.ts +187 -0
  76. package/global/hooks/parse_transcript.py +116 -0
  77. package/global/hooks/post_merge_cleanup.sh +132 -0
  78. package/global/hooks/post_tool_format.sh +215 -0
  79. package/global/hooks/ralph_context_monitor.py +240 -0
  80. package/global/hooks/ralph_stop.sh +502 -0
  81. package/global/hooks/statusline.sh +1110 -0
  82. package/global/hooks/statusline_agent_sync.py +224 -0
  83. package/global/hooks/stop_gate.sh +250 -0
  84. package/global/lib/.claude/anvil-state.json +21 -0
  85. package/global/lib/__pycache__/agent_registry.cpython-314.pyc +0 -0
  86. package/global/lib/__pycache__/claim_service.cpython-314.pyc +0 -0
  87. package/global/lib/__pycache__/coderabbit_service.cpython-314.pyc +0 -0
  88. package/global/lib/__pycache__/config_service.cpython-314.pyc +0 -0
  89. package/global/lib/__pycache__/coordination_service.cpython-314.pyc +0 -0
  90. package/global/lib/__pycache__/doc_coverage_service.cpython-314.pyc +0 -0
  91. package/global/lib/__pycache__/gate_logger.cpython-314.pyc +0 -0
  92. package/global/lib/__pycache__/github_service.cpython-314.pyc +0 -0
  93. package/global/lib/__pycache__/hygiene_service.cpython-314.pyc +0 -0
  94. package/global/lib/__pycache__/issue_models.cpython-314.pyc +0 -0
  95. package/global/lib/__pycache__/issue_provider.cpython-314.pyc +0 -0
  96. package/global/lib/__pycache__/linear_data_service.cpython-314.pyc +0 -0
  97. package/global/lib/__pycache__/linear_provider.cpython-314.pyc +0 -0
  98. package/global/lib/__pycache__/local_provider.cpython-314.pyc +0 -0
  99. package/global/lib/__pycache__/quality_service.cpython-314.pyc +0 -0
  100. package/global/lib/__pycache__/ralph_state.cpython-314.pyc +0 -0
  101. package/global/lib/__pycache__/state_manager.cpython-314.pyc +0 -0
  102. package/global/lib/__pycache__/transcript_parser.cpython-314.pyc +0 -0
  103. package/global/lib/__pycache__/verification_runner.cpython-314.pyc +0 -0
  104. package/global/lib/__pycache__/verify_iteration.cpython-314.pyc +0 -0
  105. package/global/lib/__pycache__/verify_subagent.cpython-314.pyc +0 -0
  106. package/global/lib/agent_registry.py +995 -0
  107. package/global/lib/anvil-state.sh +435 -0
  108. package/global/lib/claim_service.py +515 -0
  109. package/global/lib/coderabbit_service.py +314 -0
  110. package/global/lib/config_service.py +423 -0
  111. package/global/lib/coordination_service.py +331 -0
  112. package/global/lib/doc_coverage_service.py +1305 -0
  113. package/global/lib/gate_logger.py +316 -0
  114. package/global/lib/github_service.py +310 -0
  115. package/global/lib/handoff_generator.py +775 -0
  116. package/global/lib/hygiene_service.py +712 -0
  117. package/global/lib/issue_models.py +257 -0
  118. package/global/lib/issue_provider.py +339 -0
  119. package/global/lib/linear_data_service.py +210 -0
  120. package/global/lib/linear_provider.py +987 -0
  121. package/global/lib/linear_provider.py.backup +671 -0
  122. package/global/lib/local_provider.py +486 -0
  123. package/global/lib/orient_fast.py +457 -0
  124. package/global/lib/quality_service.py +470 -0
  125. package/global/lib/ralph_prompt_generator.py +563 -0
  126. package/global/lib/ralph_state.py +1202 -0
  127. package/global/lib/state_manager.py +417 -0
  128. package/global/lib/transcript_parser.py +597 -0
  129. package/global/lib/verification_runner.py +557 -0
  130. package/global/lib/verify_iteration.py +490 -0
  131. package/global/lib/verify_subagent.py +250 -0
  132. package/global/skills/README.md +155 -0
  133. package/global/skills/quality-gates/SKILL.md +252 -0
  134. package/global/skills/skill-template/SKILL.md +109 -0
  135. package/global/skills/testing-strategies/SKILL.md +337 -0
  136. package/global/templates/CHANGE-template.md +105 -0
  137. package/global/templates/HANDOFF-template.md +63 -0
  138. package/global/templates/PLAN-template.md +111 -0
  139. package/global/templates/SPEC-template.md +93 -0
  140. package/global/templates/ralph/PROMPT.md.template +89 -0
  141. package/global/templates/ralph/fix_plan.md.template +31 -0
  142. package/global/templates/ralph/progress.txt.template +23 -0
  143. package/global/tests/__pycache__/test_doc_coverage.cpython-314.pyc +0 -0
  144. package/global/tests/test_doc_coverage.py +520 -0
  145. package/global/tests/test_issue_models.py +299 -0
  146. package/global/tests/test_local_provider.py +323 -0
  147. package/global/tools/README.md +178 -0
  148. package/global/tools/__pycache__/anvil-hud.cpython-314.pyc +0 -0
  149. package/global/tools/anvil-hud.py +3622 -0
  150. package/global/tools/anvil-hud.py.bak +3318 -0
  151. package/global/tools/anvil-issue.py +432 -0
  152. package/global/tools/anvil-memory/CLAUDE.md +49 -0
  153. package/global/tools/anvil-memory/README.md +42 -0
  154. package/global/tools/anvil-memory/bun.lock +25 -0
  155. package/global/tools/anvil-memory/bunfig.toml +9 -0
  156. package/global/tools/anvil-memory/package.json +23 -0
  157. package/global/tools/anvil-memory/src/__tests__/ccs/context-monitor.test.ts +535 -0
  158. package/global/tools/anvil-memory/src/__tests__/ccs/edge-cases.test.ts +645 -0
  159. package/global/tools/anvil-memory/src/__tests__/ccs/fixtures.ts +363 -0
  160. package/global/tools/anvil-memory/src/__tests__/ccs/index.ts +8 -0
  161. package/global/tools/anvil-memory/src/__tests__/ccs/integration.test.ts +417 -0
  162. package/global/tools/anvil-memory/src/__tests__/ccs/prompt-generator.test.ts +571 -0
  163. package/global/tools/anvil-memory/src/__tests__/ccs/ralph-stop.test.ts +440 -0
  164. package/global/tools/anvil-memory/src/__tests__/ccs/test-utils.ts +252 -0
  165. package/global/tools/anvil-memory/src/__tests__/commands.test.ts +657 -0
  166. package/global/tools/anvil-memory/src/__tests__/db.test.ts +641 -0
  167. package/global/tools/anvil-memory/src/__tests__/hooks.test.ts +272 -0
  168. package/global/tools/anvil-memory/src/__tests__/performance.test.ts +427 -0
  169. package/global/tools/anvil-memory/src/__tests__/test-utils.ts +113 -0
  170. package/global/tools/anvil-memory/src/commands/checkpoint.ts +197 -0
  171. package/global/tools/anvil-memory/src/commands/get.ts +115 -0
  172. package/global/tools/anvil-memory/src/commands/init.ts +94 -0
  173. package/global/tools/anvil-memory/src/commands/observe.ts +163 -0
  174. package/global/tools/anvil-memory/src/commands/search.ts +112 -0
  175. package/global/tools/anvil-memory/src/db.ts +638 -0
  176. package/global/tools/anvil-memory/src/index.ts +205 -0
  177. package/global/tools/anvil-memory/src/types.ts +122 -0
  178. package/global/tools/anvil-memory/tsconfig.json +29 -0
  179. package/global/tools/ralph-loop.sh +359 -0
  180. package/package.json +45 -0
  181. package/scripts/anvil +822 -0
  182. package/scripts/extract_patterns.py +222 -0
  183. package/scripts/init-project.sh +541 -0
  184. package/scripts/install.sh +229 -0
  185. package/scripts/postinstall.js +41 -0
  186. package/scripts/rollback.sh +188 -0
  187. package/scripts/sync.sh +623 -0
  188. package/scripts/test-statusline.sh +248 -0
  189. package/scripts/update_claude_md.py +224 -0
  190. package/scripts/verify.sh +255 -0
@@ -0,0 +1,604 @@
1
+ ---
2
+ shard_id: SPEC-ANV-VRW/component-04
3
+ parent: SPEC-ANV-VRW
4
+ title: Ralph Wiggum Mode (/ralph)
5
+ status: draft
6
+ linear_issue: ANV-144, ANV-145
7
+ priority: P1
8
+ estimated_hours: 10-12
9
+ dependencies:
10
+ - component-01-verify
11
+ - component-02-stop-gate
12
+ research_sources:
13
+ - https://github.com/anthropics/claude-code/tree/main/plugins/ralph-wiggum
14
+ - https://github.com/frankbria/ralph-claude-code
15
+ - https://paddo.dev/blog/ralph-wiggum-autonomous-loops/
16
+ - https://dev.to/sivarampg/the-ralph-wiggum-approach-running-ai-coding-agents-for-hours-not-minutes-57c1
17
+ ---
18
+
19
+ # Component 4: Ralph Wiggum Mode (`/ralph`)
20
+
21
+ ## Overview
22
+
23
+ Long-running unattended execution mode based on Geoffrey Huntley's technique. Uses a bash loop with completion promises and stop hook interception for multi-hour autonomous sessions.
24
+
25
+ > Geoffrey Huntley: "6 repos overnight", "$50k contract for $297"
26
+
27
+ **Official Plugin**: `ralph-wiggum@claude-plugins-official` (Anthropic-maintained)
28
+
29
+ ---
30
+
31
+ ## Core Mechanism
32
+
33
+ ```bash
34
+ # The Ralph Wiggum loop
35
+ while :; do
36
+ cat PROMPT.md | npx --yes @anthropic/claude-code --print
37
+ done
38
+ ```
39
+
40
+ **Key Insight**: Each iteration sees the modified files and git history from previous runs. Claude autonomously improves by reading its own past work.
41
+
42
+ ---
43
+
44
+ ## Requirements
45
+
46
+ | ID | Requirement | Priority | Acceptance Criteria |
47
+ |----|-------------|----------|---------------------|
48
+ | REQ-RW-100 | Create PROMPT.md with structured task format | P0 | Template generates valid prompt |
49
+ | REQ-RW-101 | Implement completion promise pattern | P0 | Configurable promise detected |
50
+ | REQ-RW-102 | Stop hook intercepts exit, restarts on incomplete | P0 | Loop continues until promise |
51
+ | REQ-RW-103 | Maximum iterations safety (default: 50) | P0 | Hard limit prevents runaway |
52
+ | REQ-RW-104 | Track iteration count and progress | P0 | State file updated each loop |
53
+ | REQ-RW-105 | Single-item focus per iteration (critical) | P0 | Prompt enforces one item |
54
+ | REQ-RW-106 | Context management via subagents | P1 | Research uses subagents |
55
+ | REQ-RW-107 | Self-learning AGENT.md updates | P2 | Claude can update guidance |
56
+ | REQ-RW-108 | **Circuit breaker** for stuck loops | P0 | Stops on no-change/repeated-error |
57
+ | REQ-RW-109 | **Progress file** (`progress.txt`) | P0 | Appended each iteration |
58
+ | REQ-RW-110 | **Git checkpoint** before restart | P1 | Auto-commit prevents lost work |
59
+ | REQ-RW-111 | **Configurable promise** flag | P1 | `--completion-promise "TEXT"` |
60
+ | REQ-RW-112 | **jq dependency** documentation | P1 | Windows/Git Bash warning |
61
+
62
+ ---
63
+
64
+ ## Circuit Breaker System (CRITICAL)
65
+
66
+ Based on frankbria/ralph-claude-code implementation:
67
+
68
+ ```
69
+ ┌─────────────────────────────────────────────────────────────┐
70
+ │ CIRCUIT BREAKER │
71
+ ├─────────────────────────────────────────────────────────────┤
72
+ │ Trigger Conditions (any one activates): │
73
+ │ │
74
+ │ 1. 3 consecutive loops with NO file changes → STOP │
75
+ │ 2. 5+ loops repeating identical error pattern → STOP │
76
+ │ 3. Output volume declining >70% → STOP │
77
+ │ 4. Claude reports "I'm stuck" or similar → STOP │
78
+ │ │
79
+ │ States: │
80
+ │ • CLOSED - Operating normally │
81
+ │ • OPEN - Broken, stops execution │
82
+ │ • HALF - Monitoring for recovery │
83
+ └─────────────────────────────────────────────────────────────┘
84
+ ```
85
+
86
+ ---
87
+
88
+ ## Progress File Pattern (CRITICAL)
89
+
90
+ Each iteration MUST append to `progress.txt`:
91
+
92
+ ```markdown
93
+ ## Iteration 5 - 2026-01-07T14:30:00Z
94
+
95
+ ### Completed
96
+ - Implemented OAuth callback handler
97
+ - Added error handling for token refresh
98
+
99
+ ### Attempted (failed)
100
+ - Tried to add session persistence (test failure)
101
+
102
+ ### Next
103
+ - Fix session persistence test
104
+ - Add logout endpoint
105
+
106
+ ### Blockers
107
+ - None
108
+ ```
109
+
110
+ **Purpose**: Prevents repeated mistakes by giving Claude visibility into past attempts.
111
+
112
+ ---
113
+
114
+ ## Git Checkpointing
115
+
116
+ Before each iteration restart:
117
+
118
+ ```bash
119
+ # Auto-checkpoint before restart
120
+ git add -A
121
+ git commit -m "ralph: checkpoint iteration $ITERATION" --allow-empty
122
+ ```
123
+
124
+ **Why**: Lost progress is a common failure mode. Auto-checkpointing ensures recovery.
125
+
126
+ ---
127
+
128
+ ## Three-Phase Architecture
129
+
130
+ Based on Ralph Wiggum's proven pattern:
131
+
132
+ ```
133
+ Phase 1: Requirements → specs/*.md
134
+ User provides high-level requirements
135
+ Claude generates detailed specifications
136
+
137
+ Phase 2: Specs → fix_plan.md (TODO list)
138
+ Specifications broken into atomic tasks
139
+ Each task is independently completable
140
+
141
+ Phase 3: TODO items → Implementation (one at a time)
142
+ Loop processes one item per iteration
143
+ Commits on success, continues until done
144
+ ```
145
+
146
+ ---
147
+
148
+ ## Implementation Tasks
149
+
150
+ ### Task 1: Create Ralph Stop Hook
151
+ **File**: `global/hooks/ralph_stop.sh`
152
+ **Estimated**: 3 hours
153
+
154
+ - [ ] Read iteration count from state file
155
+ - [ ] Increment iteration counter
156
+ - [ ] Check max iterations limit (50 default)
157
+ - [ ] Scan transcript for configurable completion promise
158
+ - [ ] Scan for `<fatal>` error signal
159
+ - [ ] **Circuit breaker detection**:
160
+ - [ ] Track file changes (git diff --stat)
161
+ - [ ] Track error patterns (hash last 3 errors)
162
+ - [ ] Stop on 3 no-change loops
163
+ - [ ] Stop on 5 repeated error loops
164
+ - [ ] **Git checkpoint** before restart
165
+ - [ ] Return exit code (0 = allow, non-zero = restart)
166
+ - [ ] Clean up state on completion
167
+
168
+ ### Task 2: Create Prompt Templates
169
+ **File**: `global/templates/ralph/`
170
+ **Estimated**: 2 hours
171
+
172
+ - [ ] Create `PROMPT.md.template`
173
+ - [ ] Create `fix_plan.md.template`
174
+ - [ ] Create `progress.txt.template`
175
+ - [ ] Implement variable substitution
176
+ - [ ] Add iteration tracking variables
177
+ - [ ] Include constraint reminders
178
+ - [ ] **Add progress.txt update instructions**
179
+
180
+ ### Task 3: Create /ralph Skill
181
+ **File**: `global/commands/ralph.md`
182
+ **Estimated**: 2.5 hours
183
+
184
+ - [ ] `start [description]` - Initialize Ralph mode
185
+ - [ ] `status` - Show current progress
186
+ - [ ] `stop` - Force exit Ralph mode
187
+ - [ ] **`--completion-promise "TEXT"` flag**
188
+ - [ ] **`--max-iterations N` flag**
189
+ - [ ] Document critical constraints
190
+ - [ ] Add examples and anti-patterns
191
+
192
+ ### Task 4: Create Execution Script
193
+ **File**: `global/tools/ralph-loop.sh`
194
+ **Estimated**: 1.5 hours
195
+
196
+ - [ ] Set up bash loop
197
+ - [ ] Feed PROMPT.md to Claude CLI
198
+ - [ ] Handle exit codes
199
+ - [ ] Implement restart logic
200
+ - [ ] Add logging
201
+
202
+ ### Task 5: State Management
203
+ **File**: `global/lib/ralph_state.py`
204
+ **Estimated**: 1 hour
205
+
206
+ - [ ] Store state in `.claude/ralph-state.json`
207
+ - [ ] Track: iteration, last_action, remaining_items
208
+ - [ ] Clean up on completion
209
+ - [ ] Handle corruption gracefully
210
+
211
+ ---
212
+
213
+ ## PROMPT.md Template
214
+
215
+ ```markdown
216
+ # Task: {task_name}
217
+
218
+ ## Objective
219
+ {objective}
220
+
221
+ ## Current State
222
+ - Iteration: {iteration_count}
223
+ - Last action: {last_action}
224
+ - Remaining items: {remaining_count}
225
+
226
+ ## Instructions
227
+ 1. **Read progress.txt** first - learn from previous iterations
228
+ 2. Read fix_plan.md for current TODO list
229
+ 3. Complete ONLY the first unchecked item
230
+ 4. Run tests after completion
231
+ 5. If tests pass, mark item complete in fix_plan.md
232
+ 6. **Update progress.txt** with what you did this iteration
233
+ 7. If all items complete, output: <promise>{completion_promise}</promise>
234
+
235
+ ## Progress Tracking (REQUIRED)
236
+ After each iteration, append to progress.txt:
237
+ ```
238
+ ## Iteration {n} - {timestamp}
239
+ ### Completed: [what you finished]
240
+ ### Attempted: [what you tried but failed]
241
+ ### Next: [what to do next iteration]
242
+ ### Blockers: [anything blocking progress]
243
+ ```
244
+
245
+ ## Constraints
246
+ - ONE item per iteration (critical)
247
+ - Do not skip verification
248
+ - Commit on each successful item
249
+ - Use subagents for research (max 500)
250
+ - Use only 1 agent for build/test
251
+ - ALWAYS update progress.txt
252
+
253
+ ## Completion Signal
254
+ When ALL items are done and tests pass:
255
+ <promise>{completion_promise}</promise>
256
+
257
+ ## Fatal Error Signal
258
+ If stuck or unrecoverable after multiple attempts:
259
+ <fatal>Description of issue</fatal>
260
+ ```
261
+
262
+ ---
263
+
264
+ ## Stop Hook Implementation
265
+
266
+ ```bash
267
+ #!/bin/bash
268
+ # global/hooks/ralph_stop.sh
269
+ # Ralph Wiggum stop hook with circuit breaker
270
+ #
271
+ # DEPENDENCY: jq (apt install jq / brew install jq)
272
+ # WARNING: Windows/Git Bash users must install jq separately or use WSL
273
+
274
+ set -euo pipefail
275
+
276
+ TRANSCRIPT="$CLAUDE_TRANSCRIPT"
277
+ MAX_ITERATIONS="${RALPH_MAX_ITERATIONS:-50}"
278
+ COMPLETION_PROMISE="${RALPH_COMPLETION_PROMISE:-COMPLETE}"
279
+ STATE_FILE=".claude/ralph-state.json"
280
+ PROGRESS_FILE="progress.txt"
281
+
282
+ # =============================================================================
283
+ # State Management
284
+ # =============================================================================
285
+
286
+ init_state() {
287
+ if [[ ! -f "$STATE_FILE" ]]; then
288
+ echo '{"iteration":0,"no_change_count":0,"error_hashes":[],"last_diff_hash":""}' > "$STATE_FILE"
289
+ fi
290
+ }
291
+
292
+ read_state() {
293
+ jq -r ".$1 // \"$2\"" "$STATE_FILE" 2>/dev/null || echo "$2"
294
+ }
295
+
296
+ update_state() {
297
+ local key="$1" value="$2"
298
+ jq --arg v "$value" ".$key = \$v" "$STATE_FILE" > tmp.$$ && mv tmp.$$ "$STATE_FILE"
299
+ }
300
+
301
+ # =============================================================================
302
+ # Circuit Breaker Detection
303
+ # =============================================================================
304
+
305
+ get_current_diff_hash() {
306
+ git diff --stat 2>/dev/null | md5sum | cut -d' ' -f1
307
+ }
308
+
309
+ check_circuit_breaker() {
310
+ local iteration="$1"
311
+
312
+ # Check 1: No file changes for 3 consecutive iterations
313
+ local current_hash
314
+ current_hash=$(get_current_diff_hash)
315
+ local last_hash
316
+ last_hash=$(read_state "last_diff_hash" "")
317
+
318
+ if [[ "$current_hash" == "$last_hash" ]]; then
319
+ local no_change_count
320
+ no_change_count=$(read_state "no_change_count" "0")
321
+ no_change_count=$((no_change_count + 1))
322
+ update_state "no_change_count" "$no_change_count"
323
+
324
+ if [[ $no_change_count -ge 3 ]]; then
325
+ echo "🔴 Circuit breaker: 3 iterations with no file changes"
326
+ return 1
327
+ fi
328
+ else
329
+ update_state "no_change_count" "0"
330
+ update_state "last_diff_hash" "$current_hash"
331
+ fi
332
+
333
+ # Check 2: Repeated error patterns (tracked via error_hashes in state)
334
+ # Implementation would compare last N error outputs
335
+
336
+ return 0
337
+ }
338
+
339
+ # =============================================================================
340
+ # Git Checkpointing
341
+ # =============================================================================
342
+
343
+ git_checkpoint() {
344
+ local iteration="$1"
345
+ if git diff --quiet 2>/dev/null; then
346
+ return 0 # No changes to commit
347
+ fi
348
+ git add -A
349
+ git commit -m "ralph: checkpoint iteration $iteration" --no-verify 2>/dev/null || true
350
+ }
351
+
352
+ # =============================================================================
353
+ # Main Logic
354
+ # =============================================================================
355
+
356
+ main() {
357
+ init_state
358
+
359
+ local iteration
360
+ iteration=$(read_state "iteration" "0")
361
+ iteration=$((iteration + 1))
362
+ update_state "iteration" "$iteration"
363
+
364
+ echo "🔄 Ralph Wiggum: Iteration $iteration"
365
+
366
+ # Safety: Max iterations
367
+ if [[ $iteration -ge $MAX_ITERATIONS ]]; then
368
+ echo "⚠️ Maximum iterations ($MAX_ITERATIONS) reached"
369
+ rm -f "$STATE_FILE"
370
+ exit 0
371
+ fi
372
+
373
+ # Check for completion promise
374
+ if grep -q "<promise>$COMPLETION_PROMISE</promise>" "$TRANSCRIPT" 2>/dev/null; then
375
+ echo "✅ Task complete after $iteration iterations"
376
+ rm -f "$STATE_FILE"
377
+ exit 0
378
+ fi
379
+
380
+ # Check for fatal error
381
+ if grep -q "<fatal>" "$TRANSCRIPT" 2>/dev/null; then
382
+ echo "❌ Fatal error detected"
383
+ rm -f "$STATE_FILE"
384
+ exit 0
385
+ fi
386
+
387
+ # Circuit breaker check
388
+ if ! check_circuit_breaker "$iteration"; then
389
+ echo "🔴 Circuit breaker activated - stopping loop"
390
+ rm -f "$STATE_FILE"
391
+ exit 0
392
+ fi
393
+
394
+ # Git checkpoint before restart
395
+ git_checkpoint "$iteration"
396
+
397
+ echo "🔄 Restarting iteration $((iteration + 1))..."
398
+ exit 1 # Block exit, trigger restart
399
+ }
400
+
401
+ main "$@"
402
+ ```
403
+
404
+ ### Dependencies Note
405
+
406
+ **jq is required**: The stop hook uses `jq` for JSON state management.
407
+
408
+ | Platform | Installation |
409
+ |----------|--------------|
410
+ | macOS | `brew install jq` |
411
+ | Ubuntu/Debian | `apt install jq` |
412
+ | Windows/Git Bash | Install manually or use WSL |
413
+
414
+ **Known Issue**: Windows/Git Bash users report the plugin breaks without jq installed.
415
+
416
+ ---
417
+
418
+ ## /ralph Skill
419
+
420
+ ```markdown
421
+ # /ralph - Ralph Wiggum Long-Running Mode
422
+
423
+ > Enable long-running unattended execution with completion promises.
424
+
425
+ ## Arguments
426
+ - `start [task-description]` - Initialize Ralph mode with task
427
+ - `status` - Show current iteration and progress
428
+ - `stop` - Force exit Ralph mode
429
+
430
+ ## Execution: /ralph start
431
+
432
+ ### Step 1: Create Task Structure
433
+ Create PROMPT.md with:
434
+ - Clear single objective
435
+ - Current state tracking
436
+ - Constraints and completion signal
437
+
438
+ ### Step 2: Enable Stop Hook
439
+ Activate ralph_stop.sh hook that:
440
+ - Checks for <promise>COMPLETE</promise>
441
+ - Blocks exit if incomplete
442
+ - Tracks iteration count
443
+ - Has max iterations safety
444
+
445
+ ### Step 3: Create fix_plan.md
446
+ Break task into atomic TODO items:
447
+ - [ ] Item 1
448
+ - [ ] Item 2
449
+ - ...
450
+
451
+ ### Step 4: Start Loop
452
+ Execute bash loop that:
453
+ - Feeds PROMPT.md to Claude
454
+ - Processes one item per iteration
455
+ - Commits on success
456
+ - Restarts until complete
457
+
458
+ ## Critical Rules
459
+ 1. **ONE item per loop** - Never try to do multiple items
460
+ 2. **Subagents for research** - Don't pollute main context
461
+ 3. **Commit on success** - Each item gets committed
462
+ 4. **Tests gate progress** - No marking complete without tests
463
+ 5. **500 research / 1 build** - Subagent ratio
464
+ ```
465
+
466
+ ---
467
+
468
+ ## State File Schema
469
+
470
+ ```json
471
+ {
472
+ "task_name": "Implement OAuth",
473
+ "started_at": "2026-01-04T14:00:00Z",
474
+ "iteration": 5,
475
+ "last_action": "Completed OAuth callback handler",
476
+ "remaining_items": 3,
477
+ "total_items": 8,
478
+ "completed_items": [
479
+ "Set up OAuth provider config",
480
+ "Create login endpoint",
481
+ "Implement token exchange",
482
+ "Add session storage",
483
+ "Create callback handler"
484
+ ]
485
+ }
486
+ ```
487
+
488
+ ---
489
+
490
+ ## Test Cases
491
+
492
+ | Test Case | Setup | Expected |
493
+ |-----------|-------|----------|
494
+ | Simple task | 3-item fix_plan | Completes in 3 iterations |
495
+ | Max iterations | Unfixable task | Stops at 50, logs warning |
496
+ | Completion promise | All items done | Exits with success |
497
+ | Custom promise | `--completion-promise "DONE"` | Detects custom promise |
498
+ | Fatal error | `<fatal>` output | Exits immediately |
499
+ | Resume after crash | Existing state file | Continues from last iteration |
500
+ | **Circuit breaker: no changes** | 3 loops, no file changes | Stops, logs "no file changes" |
501
+ | **Circuit breaker: repeated error** | 5 loops, same error | Stops, logs "repeated error" |
502
+ | **Git checkpoint** | Loop with changes | Commits before restart |
503
+ | **Progress file** | Any loop | progress.txt updated each iteration |
504
+
505
+ ---
506
+
507
+ ## Safety Mechanisms
508
+
509
+ | Risk | Mitigation |
510
+ |------|------------|
511
+ | Infinite loop | Max iterations (50) |
512
+ | Stuck loops | **Circuit breaker** (3 no-change, 5 repeated-error) |
513
+ | Context explosion | Subagent usage |
514
+ | Lost progress | **Git checkpointing** + commit on each item |
515
+ | Repeated mistakes | **progress.txt** tracking |
516
+ | Stuck state | Fatal error signal |
517
+ | Resource exhaustion | Single build agent |
518
+ | Runaway costs | Max iterations + circuit breaker |
519
+
520
+ ---
521
+
522
+ ## When to Use Ralph
523
+
524
+ **Good Use Cases:**
525
+ - ✅ Well-defined tasks with clear success criteria (tests pass, build succeeds)
526
+ - ✅ Large-scale refactoring with measurable completion
527
+ - ✅ Framework/dependency migrations (Jest→Vitest, etc.)
528
+ - ✅ Test coverage expansion (reach X% coverage)
529
+ - ✅ Greenfield projects where you can walk away overnight
530
+ - ✅ TDD workflows: implement features to pass failing tests
531
+
532
+ **Bad Use Cases:**
533
+ - ❌ Ambiguous requirements ("make it better")
534
+ - ❌ Architectural decisions requiring human judgment
535
+ - ❌ Security-sensitive code (auth, payments, data handling)
536
+ - ❌ Exploratory work ("figure out why it's slow")
537
+ - ❌ Tasks with no objective success criteria
538
+ - ❌ Production debugging
539
+
540
+ ---
541
+
542
+ ## Cost Considerations
543
+
544
+ **Token Consumption Warning:**
545
+
546
+ | Scenario | Estimated Cost |
547
+ |----------|----------------|
548
+ | 10-iteration loop, small codebase | $5-15 |
549
+ | 50-iteration loop, medium codebase | $50-100+ |
550
+ | 100+ iterations, large codebase | $200+ |
551
+
552
+ **Best Practices:**
553
+ 1. Start conservative: `--max-iterations 10`
554
+ 2. Monitor costs during initial runs
555
+ 3. Use clear completion criteria to minimize wasted iterations
556
+ 4. Consider time-boxing (kill after N hours regardless)
557
+
558
+ **On Claude Code subscription**: You'll hit usage limits faster with Ralph loops.
559
+
560
+ ---
561
+
562
+ ## Effective Prompt Engineering
563
+
564
+ ### Strong Prompt Example
565
+ ```
566
+ Migrate all tests from Jest to Vitest.
567
+
568
+ Requirements:
569
+ - Update test file syntax
570
+ - Update package.json scripts
571
+ - Remove Jest dependencies, add Vitest
572
+ - Run tests after migration
573
+
574
+ Completion Criteria:
575
+ - ALL tests passing
576
+ - No Jest references remaining
577
+
578
+ Output: <promise>MIGRATED</promise> when complete.
579
+ ```
580
+
581
+ ### Weak Prompt Example
582
+ ```
583
+ Make it better.
584
+ ```
585
+
586
+ **Key Insight**: Objective, measurable criteria (tests pass, build succeeds, coverage threshold) outperform subjective Claude self-assessment.
587
+
588
+ ---
589
+
590
+ ## Integration Points
591
+
592
+ - **Trigger**: `/ralph start` command
593
+ - **Input**: Task description, optional fix_plan.md
594
+ - **Output**: Completed implementation with commits
595
+ - **Dependencies**: component-01 (verify), component-02 (stop-gate)
596
+
597
+ ---
598
+
599
+ ## References
600
+
601
+ - [Official Plugin](https://github.com/anthropics/claude-plugins-official/tree/main/plugins/ralph-wiggum) - Anthropic-maintained
602
+ - [frankbria/ralph-claude-code](https://github.com/frankbria/ralph-claude-code) - Circuit breaker implementation
603
+ - [Geoffrey Huntley's Original](https://ghuntley.com/ralph/) - Technique origin
604
+ - [Ralph Orchestrator](https://github.com/mikeyobrien/ralph-orchestrator) - Multi-AI coordination