ralph.rb 1.2.435535439

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 (60) hide show
  1. checksums.yaml +7 -0
  2. data/.github/workflows/gem-push.yml +47 -0
  3. data/.gitignore +79 -0
  4. data/.rubocop.yml +6018 -0
  5. data/.ruby-version +1 -0
  6. data/AGENTS.md +113 -0
  7. data/Gemfile +11 -0
  8. data/LICENSE +21 -0
  9. data/README.md +656 -0
  10. data/bin/rubocop +8 -0
  11. data/bin/test +5 -0
  12. data/exe/ralph +8 -0
  13. data/lib/ralph/agents/base.rb +132 -0
  14. data/lib/ralph/agents/claude_code.rb +24 -0
  15. data/lib/ralph/agents/codex.rb +25 -0
  16. data/lib/ralph/agents/open_code.rb +30 -0
  17. data/lib/ralph/agents.rb +24 -0
  18. data/lib/ralph/cli.rb +222 -0
  19. data/lib/ralph/config.rb +40 -0
  20. data/lib/ralph/git/file_snapshot.rb +60 -0
  21. data/lib/ralph/helpers.rb +76 -0
  22. data/lib/ralph/iteration.rb +220 -0
  23. data/lib/ralph/loop.rb +196 -0
  24. data/lib/ralph/output/active_loop_error.rb +13 -0
  25. data/lib/ralph/output/banner.rb +29 -0
  26. data/lib/ralph/output/completion_deferred.rb +12 -0
  27. data/lib/ralph/output/completion_detected.rb +17 -0
  28. data/lib/ralph/output/config_summary.rb +31 -0
  29. data/lib/ralph/output/context_consumed.rb +11 -0
  30. data/lib/ralph/output/iteration.rb +45 -0
  31. data/lib/ralph/output/max_iterations_reached.rb +16 -0
  32. data/lib/ralph/output/no_plugin_warning.rb +14 -0
  33. data/lib/ralph/output/nonzero_exit_warning.rb +11 -0
  34. data/lib/ralph/output/plugin_error.rb +12 -0
  35. data/lib/ralph/output/status.rb +176 -0
  36. data/lib/ralph/output/struggle_warning.rb +18 -0
  37. data/lib/ralph/output/task_completion.rb +12 -0
  38. data/lib/ralph/output/tasks_file_created.rb +11 -0
  39. data/lib/ralph/prompt_template.rb +183 -0
  40. data/lib/ralph/storage/context.rb +58 -0
  41. data/lib/ralph/storage/history.rb +117 -0
  42. data/lib/ralph/storage/state.rb +178 -0
  43. data/lib/ralph/storage/tasks.rb +244 -0
  44. data/lib/ralph/threads/heartbeat.rb +44 -0
  45. data/lib/ralph/threads/stream_reader.rb +50 -0
  46. data/lib/ralph/version.rb +5 -0
  47. data/lib/ralph.rb +67 -0
  48. data/original/bin/ralph.js +13 -0
  49. data/original/ralph.ts +1706 -0
  50. data/ralph.gemspec +35 -0
  51. data/ralph2.gemspec +35 -0
  52. data/screenshot.webp +0 -0
  53. data/specs/README.md +46 -0
  54. data/specs/agents.md +172 -0
  55. data/specs/cli.md +223 -0
  56. data/specs/iteration.md +173 -0
  57. data/specs/output.md +104 -0
  58. data/specs/storage/local-data-structure.md +246 -0
  59. data/specs/tasks.md +295 -0
  60. metadata +150 -0
data/README.md ADDED
@@ -0,0 +1,656 @@
1
+ <p align="center">
2
+ <h1 align="center">Open Ralph Wiggum</h1>
3
+ <h3 align="center">Autonomous Agentic Loop for Claude Code, Codex & OpenCode</h3>
4
+ </p>
5
+
6
+ <p align="center">
7
+ <img src="screenshot.webp" alt="Open Ralph Wiggum - Iterative AI coding loop for Claude Code and Codex" />
8
+ </p>
9
+
10
+ <p align="center">
11
+ <strong>Run any AI coding agent in a self-correcting loop until your task is done.</strong><br>
12
+ <em>Works with <b>Claude Code</b>, <b>OpenAI Codex</b>, and <b>OpenCode</b> — switch agents with <code>--agent</code>.</em><br>
13
+ <em>Based on the <a href="https://ghuntley.com/ralph/">Ralph Wiggum technique</a> by Geoffrey Huntley</em>
14
+ </p>
15
+
16
+ <p align="center">
17
+ <a href="https://github.com/Th0rgal/ralph-wiggum/blob/master/LICENSE"><img src="https://img.shields.io/badge/license-MIT-blue.svg" alt="MIT License"></a>
18
+ <a href="https://github.com/Th0rgal/ralph-wiggum"><img src="https://img.shields.io/badge/built%20with-Bun%20%2B%20TypeScript-f472b6.svg" alt="Built with Bun + TypeScript"></a>
19
+ <a href="https://github.com/Th0rgal/ralph-wiggum/releases"><img src="https://img.shields.io/github/v/release/Th0rgal/ralph-wiggum?include_prereleases" alt="Release"></a>
20
+ </p>
21
+
22
+ <p align="center">
23
+ <a href="#supported-agents">Supported Agents</a> •
24
+ <a href="#what-is-open-ralph-wiggum">What is Ralph?</a> •
25
+ <a href="#installation">Installation</a> •
26
+ <a href="#quick-start">Quick Start</a> •
27
+ <a href="#commands">Commands</a>
28
+ </p>
29
+
30
+ <p align="center">
31
+ <strong>Tired of agents breaking your local environment?</strong><br>
32
+ <a href="https://github.com/Th0rgal/openagent">OpenAgent</a> gives each task an isolated Linux workspace. Self-hosted. Git-backed.
33
+ </p>
34
+
35
+ ---
36
+
37
+ ## Supported Agents
38
+
39
+ Open Ralph Wiggum works with multiple AI coding agents. Switch between them using the `--agent` flag:
40
+
41
+ | Agent | Flag | Description |
42
+ |-------|------|-------------|
43
+ | **Claude Code** | `--agent claude-code` | Anthropic's Claude Code CLI for autonomous coding |
44
+ | **Codex** | `--agent codex` | OpenAI's Codex CLI for AI-powered development |
45
+ | **OpenCode** | `--agent opencode` | Default agent, open-source AI coding assistant |
46
+
47
+ ```bash
48
+ # Use Claude Code
49
+ ralph "Build a REST API" --agent claude-code --max-iterations 10
50
+
51
+ # Use OpenAI Codex
52
+ ralph "Create a CLI tool" --agent codex --max-iterations 10
53
+
54
+ # Use OpenCode (default)
55
+ ralph "Fix the failing tests" --max-iterations 10
56
+ ```
57
+
58
+ ---
59
+
60
+ ## What is Open Ralph Wiggum?
61
+
62
+ Open Ralph Wiggum implements the **Ralph Wiggum technique** — an autonomous agentic loop where an AI coding agent (Claude Code, Codex, or OpenCode) receives the **same prompt repeatedly** until it completes a task. Each iteration, the AI sees its previous work in files and git history, enabling self-correction and incremental progress.
63
+
64
+ This is a **CLI tool** that wraps any supported AI coding agent in a persistent development loop. No plugins required — just install and run.
65
+
66
+ ```bash
67
+ # The essence of the Ralph loop:
68
+ while true; do
69
+ claude-code "Build feature X. Output <promise>DONE</promise> when complete." # or codex, opencode
70
+ done
71
+ ```
72
+
73
+ **Why this works:** The AI doesn't talk to itself between iterations. It sees the same prompt each time, but the codebase has changed from previous iterations. This creates a powerful feedback loop where the agent iteratively improves its work until all tests pass.
74
+
75
+ ### Multi-Agent Flexibility
76
+
77
+ Switch between AI coding agents without changing your workflow:
78
+
79
+ - **Claude Code** (`--agent claude-code`) — Anthropic's powerful coding agent
80
+ - **Codex** (`--agent codex`) — OpenAI's code-specialized model
81
+ - **OpenCode** (`--agent opencode`) — Open-source default option
82
+
83
+ ## Why Ralph? (Not Just a `while true` Loop)
84
+
85
+ At its core, the Ralph technique is a `while true` loop that calls an AI coding agent. So what does this tool actually add? A bare loop has no way to stop, no observability, no error recovery, no task management, and no way to intervene mid-run. Ralph wraps the loop with termination logic, state management, struggle detection, live monitoring, and mid-flight steering — turning a dumb retry loop into a supervised autonomous agent runner.
86
+
87
+ | Feature | Problem It Solves |
88
+ |---------|-------------------|
89
+ | **Completion Promise Detection** | A bare loop never knows when to stop. Ralph detects `<promise>COMPLETE</promise>` in agent output to terminate cleanly. |
90
+ | **Min/Max Iteration Bounds** | No safety net on cost or runaway loops. `--max-iterations` caps runs; `--min-iterations` prevents premature completion. |
91
+ | **Task Decomposition Mode** | Large projects overwhelm a single prompt. Ralph breaks work into a `.ralph/ralph-tasks.md` file, feeds one task at a time, and advances automatically. |
92
+ | **Task Management CLI** | Editing a markdown task file by hand is tedious. `--list-tasks`, `--add-task`, `--remove-task` manage it from the terminal. |
93
+ | **Mid-Loop Context Injection** | You see the agent going the wrong way but don't want to kill the loop. `--add-context "hint"` from another terminal steers the next iteration. |
94
+ | **Status Dashboard** (`--status`) | No visibility into a running loop. Shows iteration count, elapsed time, task progress, recent history, and struggle indicators from another terminal. |
95
+ | **Struggle Detection** | Agent silently spins doing nothing useful. Ralph tracks no-file-change streaks, short iterations (<30s), and repeated errors, then warns you. |
96
+ | **Git-Based File Change Tracking** | Can't tell if the agent actually did anything. Git snapshots before/after each iteration detect modifications. |
97
+ | **Streaming + Compact Tool Summaries** | Raw agent output is extremely noisy with tool invocations. Ralph suppresses tool lines and prints periodic compact summaries (e.g., `Bash 5 . Edit 3 . Read 2`). |
98
+ | **Heartbeat Indicator** | Long silent periods make you think the loop is hung. A background thread prints `working... elapsed X` every 10s. |
99
+ | **Multi-Agent Support** | Locked into one agent. Ralph supports OpenCode, Claude Code, and Codex behind a uniform interface. |
100
+ | **Auto-Approve Permissions** | Interactive permission prompts block an unattended loop. `--allow-all` passes the right flags per agent (`--dangerously-skip-permissions`, `--full-auto`, etc.). |
101
+ | **State Persistence** (`.ralph/`) | A killed loop loses all context. Ralph persists session state, iteration history, context, and tasks to disk for recovery and monitoring. |
102
+ | **Iteration History & Metrics** | No record of what happened. Each iteration records duration, tools used, files changed, errors, and exit code. |
103
+ | **Error Extraction** | Errors are buried in pages of output. Ralph scans output for error patterns and surfaces them in history/status. |
104
+ | **Active Loop Guard** | Two loops running simultaneously corrupt state. Ralph detects an existing active session and refuses to start a second. |
105
+ | **Graceful Signal Handling** | Ctrl+C leaves orphaned agent processes and corrupt state. Ralph sends TERM to the subprocess and cleans up state files. |
106
+ | **Nonzero Exit Resilience** | Agent crashes kill the loop. Ralph logs a warning and continues to the next iteration instead. |
107
+ | **Agent Binary Validation** | Cryptic errors if the agent CLI isn't installed. Ralph checks `$PATH` upfront and fails with a clear message. |
108
+ | **Plugin Filtering** | OpenCode's legacy `ralph-wiggum` plugin causes fatal errors. `--no-plugins` filters it out. |
109
+ | **Iteration-Aware Prompt Building** | Each iteration needs structured context (iteration number, task focus, completion instructions). Ralph constructs iteration-aware prompts automatically. |
110
+ | **Model Selection** | Each agent has different `--model` flag syntax. Ralph normalizes `--model MODEL` across all agents. |
111
+
112
+ ## Installation
113
+
114
+ **Prerequisites:**
115
+ - [Bun](https://bun.sh) runtime
116
+ - At least one AI coding agent CLI:
117
+ - [Claude Code](https://docs.anthropic.com/en/docs/claude-code) — Anthropic's Claude Code CLI
118
+ - [Codex](https://github.com/openai/codex) — OpenAI's Codex CLI
119
+ - [OpenCode](https://opencode.ai) — Open-source AI coding assistant
120
+
121
+ ### npm (recommended)
122
+
123
+ ```bash
124
+ npm install -g @th0rgal/ralph-wiggum
125
+ ```
126
+
127
+ ### Bun
128
+
129
+ ```bash
130
+ bun add -g @th0rgal/ralph-wiggum
131
+ ```
132
+
133
+ ### From source
134
+
135
+ ```bash
136
+ git clone https://github.com/Th0rgal/ralph-wiggum
137
+ cd ralph-wiggum
138
+ ./install.sh
139
+ ```
140
+
141
+ ```powershell
142
+ git clone https://github.com/Th0rgal/ralph-wiggum
143
+ cd ralph-wiggum
144
+ .\install.ps1
145
+ ```
146
+
147
+ This installs the `ralph` CLI command globally.
148
+
149
+ ## Quick Start
150
+
151
+ ```bash
152
+ # Simple task with iteration limit
153
+ ralph "Create a hello.txt file with 'Hello World'. Output <promise>DONE</promise> when complete." \
154
+ --max-iterations 5
155
+
156
+ # Build something real
157
+ ralph "Build a REST API for todos with CRUD operations and tests. \
158
+ Run tests after each change. Output <promise>COMPLETE</promise> when all tests pass." \
159
+ --max-iterations 20
160
+
161
+ # Use Claude Code instead of OpenCode
162
+ ralph "Create a small CLI and document usage. Output <promise>COMPLETE</promise> when done." \
163
+ --agent claude-code --model claude-sonnet-4 --max-iterations 5
164
+
165
+ # Use Codex instead of OpenCode
166
+ ralph "Create a small CLI and document usage. Output <promise>COMPLETE</promise> when done." \
167
+ --agent codex --model gpt-5-codex --max-iterations 5
168
+
169
+ # Complex project with Tasks Mode
170
+ ralph "Build a full-stack web application with user auth and database" \
171
+ --tasks --max-iterations 50
172
+ ```
173
+
174
+ ## Commands
175
+
176
+ ### Running a Loop
177
+
178
+ ```bash
179
+ ralph "<prompt>" [options]
180
+
181
+ Options:
182
+ --agent AGENT AI agent to use: opencode (default), claude-code, codex
183
+ --min-iterations N Minimum iterations before completion allowed (default: 1)
184
+ --max-iterations N Stop after N iterations (default: unlimited)
185
+ --completion-promise T Text that signals completion (default: COMPLETE)
186
+ --tasks, -t Enable Tasks Mode for structured task tracking
187
+ --task-promise T Text that signals task completion (default: READY_FOR_NEXT_TASK)
188
+ --model MODEL Model to use (agent-specific)
189
+ --no-stream Buffer agent output and print at the end
190
+ --verbose-tools Print every tool line (disable compact tool summary)
191
+ --no-plugins Disable non-auth OpenCode plugins for this run (opencode only)
192
+ --no-commit Don't auto-commit after iterations
193
+ --allow-all Auto-approve all tool permissions (default: on)
194
+ --no-allow-all Require interactive permission prompts
195
+ --help Show help
196
+ ```
197
+
198
+ ### Tasks Mode
199
+
200
+ Tasks Mode allows you to break complex projects into smaller, manageable tasks. Ralph works on one task at a time and tracks progress in a markdown file.
201
+
202
+ ```bash
203
+ # Enable Tasks Mode
204
+ ralph "Build a complete web application" --tasks --max-iterations 20
205
+
206
+ # Custom task completion signal
207
+ ralph "Multi-feature project" --tasks --task-promise "TASK_DONE"
208
+ ```
209
+
210
+ #### Task Management Commands
211
+
212
+ ```bash
213
+ # List current tasks
214
+ ralph --list-tasks
215
+
216
+ # Add a new task
217
+ ralph --add-task "Implement user authentication"
218
+
219
+ # Remove task by index
220
+ ralph --remove-task 3
221
+
222
+ # Show status (tasks shown automatically when tasks mode is active)
223
+ ralph --status
224
+ ```
225
+
226
+ #### How Tasks Mode Works
227
+
228
+ 1. **Task File**: Tasks are stored in `.ralph/ralph-tasks.md`
229
+ 2. **One Task Per Iteration**: Ralph focuses on a single task to reduce confusion
230
+ 3. **Automatic Progression**: When a task completes (`<promise>READY_FOR_NEXT_TASK</promise>`), Ralph moves to the next
231
+ 4. **Persistent State**: Tasks survive loop restarts
232
+ 5. **Focused Context**: Smaller contexts per iteration reduce costs and improve reliability
233
+
234
+ Task status indicators:
235
+ - `[ ]` - Not started
236
+ - `[/]` - In progress
237
+ - `[x]` - Complete
238
+
239
+ Example task file:
240
+ ```markdown
241
+ # Ralph Tasks
242
+
243
+ - [ ] Set up project structure
244
+ - [x] Initialize git repository
245
+ - [/] Implement user authentication
246
+ - [ ] Create login page
247
+ - [ ] Add JWT handling
248
+ - [ ] Build dashboard UI
249
+ ```
250
+
251
+ ### Monitoring & Control
252
+
253
+ ```bash
254
+ # Check status of active loop (run from another terminal)
255
+ ralph --status
256
+
257
+ # Add context/hints for the next iteration
258
+ ralph --add-context "Focus on fixing the auth module first"
259
+
260
+ # Clear pending context
261
+ ralph --clear-context
262
+ ```
263
+
264
+ ### Status Dashboard
265
+
266
+ The `--status` command shows:
267
+ - **Active loop info**: Current iteration, elapsed time, prompt
268
+ - **Pending context**: Any hints queued for next iteration
269
+ - **Current tasks**: Automatically shown when tasks mode is active (or use `--tasks`)
270
+ - **Iteration history**: Last 5 iterations with tools used, duration
271
+ - **Struggle indicators**: Warnings if agent is stuck (no progress, repeated errors)
272
+
273
+ ```
274
+ ╔══════════════════════════════════════════════════════════════════╗
275
+ ║ Ralph Wiggum Status ║
276
+ ╚══════════════════════════════════════════════════════════════════╝
277
+
278
+ 🔄 ACTIVE LOOP
279
+ Iteration: 3 / 10
280
+ Elapsed: 5m 23s
281
+ Promise: COMPLETE
282
+ Prompt: Build a REST API...
283
+
284
+ 📊 HISTORY (3 iterations)
285
+ Total time: 5m 23s
286
+
287
+ Recent iterations:
288
+ 🔄 #1: 2m 10s | Bash:5 Write:3 Read:2
289
+ 🔄 #2: 1m 45s | Edit:4 Bash:3 Read:2
290
+ 🔄 #3: 1m 28s | Bash:2 Edit:1
291
+
292
+ ⚠️ STRUGGLE INDICATORS:
293
+ - No file changes in 3 iterations
294
+ 💡 Consider using: ralph --add-context "your hint here"
295
+ ```
296
+
297
+ ### Mid-Loop Context Injection
298
+
299
+ Guide a struggling agent without stopping the loop:
300
+
301
+ ```bash
302
+ # In another terminal while loop is running
303
+ ralph --add-context "The bug is in utils/parser.ts line 42"
304
+ ralph --add-context "Try using the singleton pattern for config"
305
+ ```
306
+
307
+ Context is automatically consumed after one iteration.
308
+
309
+ ## Troubleshooting
310
+
311
+ ### Plugin errors
312
+
313
+ This package is **CLI-only**. If OpenCode tries to load a `ralph-wiggum` or `open-ralph-wiggum` plugin,
314
+ remove it from your OpenCode `plugin` list (opencode.json), or run:
315
+
316
+ ```bash
317
+ ralph "Your task" --no-plugins
318
+ ```
319
+
320
+ ### "bun: command not found"
321
+
322
+ Install Bun: https://bun.sh
323
+
324
+ ## Writing Good Prompts
325
+
326
+ ### Include Clear Success Criteria
327
+
328
+ ❌ Bad:
329
+ ```
330
+ Build a todo API
331
+ ```
332
+
333
+ ✅ Good:
334
+ ```
335
+ Build a REST API for todos with:
336
+ - CRUD endpoints (GET, POST, PUT, DELETE)
337
+ - Input validation
338
+ - Tests for each endpoint
339
+
340
+ Run tests after changes. Output <promise>COMPLETE</promise> when all tests pass.
341
+ ```
342
+
343
+ ### Use Verifiable Conditions
344
+
345
+ ❌ Bad:
346
+ ```
347
+ Make the code better
348
+ ```
349
+
350
+ ✅ Good:
351
+ ```
352
+ Refactor auth.ts to:
353
+ 1. Extract validation into separate functions
354
+ 2. Add error handling for network failures
355
+ 3. Ensure all existing tests still pass
356
+
357
+ Output <promise>DONE</promise> when refactored and tests pass.
358
+ ```
359
+
360
+ ### Always Set Max Iterations
361
+
362
+ ```bash
363
+ # Safety net for runaway loops
364
+ ralph "Your task" --max-iterations 20
365
+ ```
366
+
367
+ ## Recommended PRD Format
368
+
369
+ For best results, use a concise PRD with:
370
+
371
+ - **Goal**: one sentence summary of the desired outcome
372
+ - **Scope**: what is in/out
373
+ - **Requirements**: numbered, testable items
374
+ - **Constraints**: tech stack, performance, security, compatibility
375
+ - **Acceptance criteria**: explicit success checks
376
+ - **Completion promise**: include `<promise>COMPLETE</promise>` (or match your `--completion-promise`)
377
+
378
+ Example (Markdown):
379
+
380
+ ```markdown
381
+ # PRD: Add Export Button
382
+
383
+ ## Goal
384
+ Let users export reports as CSV from the dashboard.
385
+
386
+ ## Scope
387
+ - In: export current report view
388
+ - Out: background exports, scheduling
389
+
390
+ ## Requirements
391
+ 1. Add "Export CSV" button to dashboard header.
392
+ 2. CSV includes columns: date, revenue, sessions.
393
+ 3. Works for reports up to 10k rows.
394
+
395
+ ## Constraints
396
+ - Keep current UI styling.
397
+ - Use existing CSV utility in utils/csv.ts.
398
+
399
+ ## Acceptance Criteria
400
+ - Clicking button downloads a valid CSV.
401
+ - CSV opens cleanly in Excel/Sheets.
402
+ - All existing tests pass.
403
+
404
+ ## Completion Promise
405
+ <promise>COMPLETE</promise>
406
+ ```
407
+
408
+ ### JSON Feature List (Recommended for Complex Projects)
409
+
410
+ For larger projects, a structured JSON feature list works better than prose. Based on [Anthropic's research on effective agent harnesses](https://www.anthropic.com/engineering/effective-harnesses-for-long-running-agents), JSON format reduces the chance of agents inappropriately modifying test definitions.
411
+
412
+ Create a `features.json` file:
413
+
414
+ ```json
415
+ {
416
+ "features": [
417
+ {
418
+ "category": "functional",
419
+ "description": "Export button downloads CSV with current report data",
420
+ "steps": [
421
+ "Navigate to dashboard",
422
+ "Click 'Export CSV' button",
423
+ "Verify CSV file downloads",
424
+ "Open CSV and verify columns: date, revenue, sessions",
425
+ "Verify data matches displayed report"
426
+ ],
427
+ "passes": false
428
+ },
429
+ {
430
+ "category": "functional",
431
+ "description": "Export handles large reports up to 10k rows",
432
+ "steps": [
433
+ "Load report with 10,000 rows",
434
+ "Click 'Export CSV' button",
435
+ "Verify export completes without timeout",
436
+ "Verify all rows present in CSV"
437
+ ],
438
+ "passes": false
439
+ },
440
+ {
441
+ "category": "ui",
442
+ "description": "Export button matches existing dashboard styling",
443
+ "steps": [
444
+ "Navigate to dashboard",
445
+ "Verify button uses existing button component",
446
+ "Verify button placement in header area"
447
+ ],
448
+ "passes": false
449
+ }
450
+ ]
451
+ }
452
+ ```
453
+
454
+ Then reference it in your prompt:
455
+
456
+ ```
457
+ Read features.json for the feature list. Work through each feature one at a time.
458
+ After verifying a feature works end-to-end, update its "passes" field to true.
459
+ Do NOT modify the description or steps - only change the passes boolean.
460
+ Output <promise>COMPLETE</promise> when all features pass.
461
+ ```
462
+
463
+ **Why JSON?** Agents are less likely to inappropriately modify JSON test definitions compared to Markdown. The structured format keeps agents focused on implementation rather than redefining success criteria.
464
+
465
+ ## When to Use Ralph
466
+
467
+ **Good for:**
468
+ - Tasks with automatic verification (tests, linters, type checking)
469
+ - Well-defined tasks with clear completion criteria
470
+ - Greenfield projects where you can walk away
471
+ - Iterative refinement (getting tests to pass)
472
+
473
+ **Not good for:**
474
+ - Tasks requiring human judgment
475
+ - One-shot operations
476
+ - Unclear success criteria
477
+ - Production debugging
478
+
479
+ ## How It Works
480
+
481
+ ```
482
+ ┌─────────────────────────────────────────────────────────────┐
483
+ │ │
484
+ │ ┌──────────┐ same prompt ┌──────────┐ │
485
+ │ │ │ ───────────────▶ │ │ │
486
+ │ │ ralph │ │ AI Agent │ │
487
+ │ │ CLI │ ◀─────────────── │ │ │
488
+ │ │ │ output + files │ │ │
489
+ │ └──────────┘ └──────────┘ │
490
+ │ │ │ │
491
+ │ │ check for │ modify │
492
+ │ │ <promise> │ files │
493
+ │ ▼ ▼ │
494
+ │ ┌──────────┐ ┌──────────┐ │
495
+ │ │ Complete │ │ Git │ │
496
+ │ │ or │ │ Repo │ │
497
+ │ │ Retry │ │ (state) │ │
498
+ │ └──────────┘ └──────────┘ │
499
+ │ │
500
+ └─────────────────────────────────────────────────────────────┘
501
+ ```
502
+
503
+ 1. Ralph sends your prompt to the selected agent
504
+ 2. The agent works on the task, modifies files
505
+ 3. Ralph checks output for completion promise
506
+ 4. If not found, repeat with same prompt
507
+ 5. AI sees previous work in files
508
+ 6. Loop until success or max iterations
509
+
510
+ ## Project Structure
511
+
512
+ ```
513
+ ralph-wiggum/
514
+ ├── exe/ralph.js # CLI entrypoint (npm wrapper)
515
+ ├── ralph.ts # Main loop implementation
516
+ ├── package.json # Package config
517
+ ├── install.sh / install.ps1 # Installation scripts
518
+ └── uninstall.sh / uninstall.ps1 # Uninstallation scripts
519
+ ```
520
+
521
+ ### State Files (in `.ralph/`)
522
+
523
+ During operation, Ralph stores state in `.ralph/`. Here's what each file contains:
524
+
525
+ #### `ralph-loop.state.json` (gitignored)
526
+
527
+ Tracks the active loop session. Deleted when the loop ends.
528
+
529
+ ```json
530
+ {
531
+ "active": true,
532
+ "iteration": 3,
533
+ "minIterations": 1,
534
+ "maxIterations": 10,
535
+ "completionPromise": "COMPLETE",
536
+ "tasksMode": true,
537
+ "taskPromise": "READY_FOR_NEXT_TASK",
538
+ "prompt": "Build a REST API for todos with CRUD operations and tests.",
539
+ "startedAt": "2026-02-02T12:00:00Z",
540
+ "model": "claude-sonnet-4",
541
+ "agent": "claude-code"
542
+ }
543
+ ```
544
+
545
+ #### `ralph-history.json`
546
+
547
+ Records every iteration -- timing, tools used, files changed, errors, and struggle indicators for detecting when the agent is stuck.
548
+
549
+ ```json
550
+ {
551
+ "iterations": [
552
+ {
553
+ "iteration": 1,
554
+ "started_at": "2026-02-02T12:00:00Z",
555
+ "ended_at": "2026-02-02T12:05:30Z",
556
+ "duration_ms": 330000,
557
+ "tools_used": { "Read": 5, "Edit": 3, "Bash": 2 },
558
+ "files_modified": ["lib/auth.rb", "test/auth_test.rb"],
559
+ "exit_code": 0,
560
+ "completion_detected": false,
561
+ "errors": []
562
+ },
563
+ {
564
+ "iteration": 2,
565
+ "started_at": "2026-02-02T12:05:31Z",
566
+ "ended_at": "2026-02-02T12:07:15Z",
567
+ "duration_ms": 104000,
568
+ "tools_used": { "Edit": 4, "Bash": 3, "Read": 2 },
569
+ "files_modified": ["lib/auth.rb"],
570
+ "exit_code": 0,
571
+ "completion_detected": false,
572
+ "errors": []
573
+ }
574
+ ],
575
+ "total_duration_ms": 434000,
576
+ "struggle_indicators": {
577
+ "repeated_errors": {},
578
+ "no_progress_iterations": 0,
579
+ "short_iterations": 0
580
+ }
581
+ }
582
+ ```
583
+
584
+ `struggle_indicators` tracks signs the agent is spinning its wheels:
585
+ - `repeated_errors` -- error messages that keep recurring (keyed by message, valued by count)
586
+ - `no_progress_iterations` -- consecutive iterations where no files were modified
587
+ - `short_iterations` -- consecutive iterations under 30 seconds (agent likely not doing real work)
588
+
589
+ #### `ralph-context.md`
590
+
591
+ Holds context injected via `--add-context` for the next iteration. The first write adds a header, subsequent appends concatenate directly. Content is included in the prompt sent to the agent.
592
+
593
+ ```markdown
594
+ # Ralph Loop Context
595
+ The auth module uses JWT tokens and the test suite expects a running Redis instance.
596
+ Focus on fixing the login endpoint before moving to registration.
597
+ ```
598
+
599
+ #### `ralph-tasks.md`
600
+
601
+ Task list for Tasks Mode (created when `--tasks` is used). Both machine-parsed and human-editable. See [Tasks Mode](#tasks-mode) for details on the checkbox syntax (`[ ]` todo, `[/]` in progress, `[x]` complete).
602
+
603
+ ## Uninstall
604
+
605
+ ```bash
606
+ npm uninstall -g @th0rgal/ralph-wiggum
607
+ ```
608
+
609
+ ```powershell
610
+ npm uninstall -g @th0rgal/ralph-wiggum
611
+ ```
612
+
613
+ ## Agent-Specific Notes
614
+
615
+ ### Claude Code
616
+
617
+ [Claude Code](https://docs.anthropic.com/en/docs/claude-code) is Anthropic's official CLI for Claude. Use it with Open Ralph Wiggum for powerful autonomous coding:
618
+
619
+ ```bash
620
+ ralph "Refactor the auth module and ensure tests pass" \
621
+ --agent claude-code \
622
+ --model claude-sonnet-4 \
623
+ --max-iterations 15
624
+ ```
625
+
626
+ ### OpenAI Codex
627
+
628
+ [Codex](https://github.com/openai/codex) is OpenAI's code-specialized agent. Perfect for code generation and refactoring tasks:
629
+
630
+ ```bash
631
+ ralph "Generate unit tests for all utility functions" \
632
+ --agent codex \
633
+ --model gpt-5-codex \
634
+ --max-iterations 10
635
+ ```
636
+
637
+ ### OpenCode
638
+
639
+ [OpenCode](https://opencode.ai) is an open-source AI coding assistant. It's the default agent:
640
+
641
+ ```bash
642
+ ralph "Fix all TypeScript errors" --max-iterations 10
643
+ ```
644
+
645
+ ## Learn More
646
+
647
+ - [Original Ralph Wiggum technique by Geoffrey Huntley](https://ghuntley.com/ralph/)
648
+ - [Ralph Orchestrator](https://github.com/mikeyobrien/ralph-orchestrator)
649
+
650
+ ## See Also
651
+
652
+ Check out [OpenAgent](https://github.com/Th0rgal/openagent) — a dashboard for orchestrating AI agents with workspace management, real-time monitoring, and multi-agent workflows.
653
+
654
+ ## License
655
+
656
+ MIT
data/bin/rubocop ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+ require "rubygems"
3
+ require "bundler/setup"
4
+
5
+ # explicit rubocop config increases performance slightly while avoiding config confusion.
6
+ ARGV.unshift("--config", File.expand_path("../.rubocop.yml", __dir__))
7
+
8
+ load Gem.bin_path("rubocop", "rubocop")
data/bin/test ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+
4
+ cd "$(dirname "$0")/.."
5
+ ruby -Ilib -e 'Dir.glob("test/test_*.rb").each { |f| require_relative f }'
data/exe/ralph ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ $LOAD_PATH.unshift(File.expand_path("../lib", __dir__))
5
+
6
+ require "ralph"
7
+
8
+ Ralph::CLI.new(ARGV).run