Skip to content

Commit 96f2d2e

Browse files
duyetduyetbot
andcommitted
refactor(ralph-wiggum): simplify plugin architecture
- Remove unused lib/ scripts (circuit_breaker, api_limit_handler, etc.) - Remove session-start hook - Simplify stop-hook.sh, scripts, and commands - Streamline README and CLAUDE.md documentation - Add tests/ directory 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: duyetbot <duyetbot@users.noreply.github.com>
1 parent 3bd3353 commit 96f2d2e

22 files changed

Lines changed: 732 additions & 1539 deletions

ralph-wiggum/.claude-plugin/plugin.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "ralph-wiggum",
3-
"version": "1.1.2",
4-
"description": "Self-referential development loop for Claude Code with session isolation. Run iterative tasks with automatic progress detection and safety controls.",
3+
"version": "2.0.0",
4+
"description": "Self-referential development loop for Claude Code. Run iterative tasks with automatic progress detection and circuit breaker safety.",
55
"author": {
66
"name": "duyet",
77
"contributors": ["Daisy Hollman <daisy@anthropic.com>"]

ralph-wiggum/CLAUDE.md

Lines changed: 10 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Ralph Wiggum Plugin
22

3-
Self-referential development loop for Claude Code with session isolation.
3+
Self-referential development loop for Claude Code.
44

55
## Commands
66

@@ -9,48 +9,26 @@ Self-referential development loop for Claude Code with session isolation.
99
- `/cancel-ralph` - Cancel active loop
1010
- `/help` - Usage documentation
1111

12-
## Hooks
12+
## Hook
1313

14-
Two hooks in `hooks/hooks.json`:
15-
16-
| Hook | File | Purpose |
17-
|------|------|---------|
18-
| SessionStart | `session-start-hook.sh` | Initialize session ID for state isolation |
19-
| Stop | `stop-hook.sh` | Intercept exit, feed prompt back for next iteration |
20-
21-
## Key Files
22-
23-
```
24-
lib/
25-
├── utils.sh # Session isolation utilities (get_session_id, get_state_file_path)
26-
├── circuit_breaker.sh # Safety circuit breaker for error detection
27-
├── response_analyzer.sh # Smart exit detection based on response patterns
28-
├── api_limit_handler.sh # Rate limit detection and handling
29-
└── task_manager.sh # Task tracking across iterations
30-
```
14+
Stop hook in `hooks/hooks.json` intercepts exit and feeds prompt back for next iteration.
3115

3216
## State Files
3317

34-
State stored in `.claude/ralph-session.local/` with session ID suffix:
35-
- `ralph-loop.{session_id}.md` - Loop state (iteration, prompt, settings)
36-
- `ralph-circuit.{session_id}.json` - Circuit breaker state
37-
- `ralph-analysis.{session_id}.json` - Response analysis data
18+
State stored in `.claude/`:
19+
- `ralph-loop.local.md` - Loop state (iteration, prompt, settings)
20+
- `ralph-circuit.local.json` - Circuit breaker state
3821

39-
## Testing Hooks
22+
## Testing
4023

4124
Test stop hook manually:
4225
```bash
4326
cd ralph-wiggum
4427
echo '{"session_id": "test", "transcript_path": "/tmp/test.json"}' | ./hooks/stop-hook.sh
4528
```
4629

47-
Test with set -euo pipefail (catches exit code issues):
48-
```bash
49-
bash -euo pipefail -c 'source lib/utils.sh && get_session_id'
50-
```
51-
5230
## Common Issues
5331

54-
1. **Hook exits with code 1**: Check that all functions in `lib/utils.sh` return 0 explicitly
55-
2. **Session state persists**: State files use session ID suffix for isolation
56-
3. **Circuit breaker trips**: Check `.claude/ralph-session.local/ralph-circuit.*.json`
32+
1. **Hook exits early**: Check state file exists at `.claude/ralph-loop.local.md`
33+
2. **Circuit breaker trips**: Check `.claude/ralph-circuit.local.json` for state
34+
3. **Loop doesn't stop**: Ensure `<promise>` tags match exactly

ralph-wiggum/README.md

Lines changed: 15 additions & 190 deletions
Original file line numberDiff line numberDiff line change
@@ -41,36 +41,14 @@ The loop stops when any condition is met:
4141
|-----------|---------|
4242
| **Promise** | Output `<promise>TEXT</promise>` matching `--completion-promise` |
4343
| **Iterations** | Reach `--max-iterations` limit |
44-
| **Circuit Breaker** | No file changes for 3 iterations |
45-
| **Smart Exit** | High confidence completion detected |
46-
| **API Limit** | Rate limit or usage limit reached |
44+
| **Circuit Breaker** | No file changes for 3 iterations or 5 consecutive errors |
4745

48-
## Safety Features
49-
50-
### Circuit Breaker
46+
## Safety: Circuit Breaker
5147

5248
Prevents runaway loops through stagnation detection:
5349

5450
- **No Progress**: Stops after 3 iterations without file changes
5551
- **Errors**: Stops after 5 consecutive errors in output
56-
- **Duplicates**: Stops after 3 identical outputs
57-
- **States**: `CLOSED``HALF_OPEN``OPEN`
58-
59-
### Intelligent Exit
60-
61-
Analyzes responses for completion signals:
62-
63-
- Keyword detection: "done", "complete", "finished", "all tests pass"
64-
- Test-only loop detection: running tests without new code
65-
- Confidence scoring: exits at 40+ points
66-
67-
### API Limit Handler
68-
69-
Gracefully handles rate limits:
70-
71-
- 5-hour usage limit detection
72-
- 429 rate limit detection
73-
- Automatic pause with wait recommendations
7452

7553
## Options
7654

@@ -79,23 +57,12 @@ Gracefully handles rate limits:
7957
| `--max-iterations <n>` | Stop after N iterations |
8058
| `--completion-promise <text>` | Promise phrase to signal completion |
8159
| `--no-circuit-breaker` | Disable stagnation detection |
82-
| `--no-smart-exit` | Disable completion analysis |
83-
| `--no-rate-limit` | Disable rate limit handling |
8460
| `--reset-circuit` | Reset circuit breaker state |
8561

8662
## Prompt Guidelines
8763

8864
Write prompts with clear, verifiable completion criteria:
8965

90-
### Structure
91-
92-
1. **Goal**: What you want to build/fix
93-
2. **Requirements**: Specific, testable criteria
94-
3. **Completion signal**: How to know when done
95-
96-
### Examples
97-
98-
**Bug fix with tests:**
9966
```markdown
10067
Fix the token refresh bug in auth.ts.
10168

@@ -107,67 +74,25 @@ Requirements:
10774
Output <promise>FIXED</promise> when tests pass.
10875
```
10976

110-
**Feature implementation:**
111-
```markdown
112-
Add rate limiting to the API.
113-
114-
Requirements:
115-
- 100 requests per minute per IP
116-
- Return 429 status when exceeded
117-
- Include retry-after header
118-
- Add rate limit tests
119-
120-
Output <promise>DONE</promise> when feature works.
121-
```
122-
123-
**Refactoring task:**
124-
```markdown
125-
Refactor user service to use repository pattern.
126-
127-
Requirements:
128-
- Extract database calls to UserRepository
129-
- Service depends on repository interface
130-
- Existing tests still pass
131-
- No TypeScript errors
132-
133-
Output <promise>REFACTORED</promise> when complete.
134-
```
135-
136-
### Tips
137-
138-
- Use measurable criteria ("tests pass", "no errors", "build succeeds")
139-
- Include the `<promise>` tag in your prompt so Claude knows how to exit
140-
- Keep requirements focused—fewer is better for iterative tasks
141-
14277
## Monitoring
14378

144-
Use the status command or inspect files directly:
145-
14679
```bash
147-
# Quick status check (shows session ID)
148-
/status
149-
150-
# List all session state files
151-
ls -la .claude/ralph-session.local/
152-
153-
# View specific session state (session_id shown in /status)
154-
cat .claude/ralph-session.local/ralph-loop.{session_id}.local.md
155-
cat .claude/ralph-session.local/ralph-circuit.{session_id}.json
156-
cat .claude/ralph-session.local/ralph-analysis.{session_id}.json
80+
/status # Quick status check
81+
cat .claude/ralph-loop.local.md # Loop state
82+
cat .claude/ralph-circuit.local.json # Circuit breaker state
15783
```
15884

159-
State files are stored in `.claude/ralph-session.local/` with session_id as filename suffix. Each Claude Code session has isolated state. The `.local` suffix ensures gitignore compatibility.
160-
161-
## Configuration
85+
## When to Use
16286

163-
Environment variables:
87+
**Good for:**
88+
- Tasks with clear success criteria (tests pass, build succeeds)
89+
- Iterative refinement (debugging, optimization)
90+
- Greenfield implementation with defined requirements
16491

165-
| Variable | Default | Description |
166-
|----------|---------|-------------|
167-
| `RALPH_MAX_NO_PROGRESS` | 3 | Iterations without progress before halt |
168-
| `RALPH_MAX_ERRORS` | 5 | Consecutive errors before halt |
169-
| `RALPH_MAX_IDENTICAL` | 3 | Identical outputs before halt |
170-
| `RALPH_COMPLETION_THRESHOLD` | 40 | Confidence score for smart exit |
92+
**Not for:**
93+
- Tasks requiring human judgment or design decisions
94+
- One-shot operations
95+
- Production debugging
17196

17297
## Architecture
17398

@@ -182,115 +107,15 @@ ralph-wiggum/
182107
│ └── help.md # Plugin documentation
183108
├── hooks/
184109
│ ├── hooks.json # Hook configuration
185-
│ ├── session-start-hook.sh # Session ID capture
186110
│ └── stop-hook.sh # Main loop interceptor
187-
├── lib/
188-
│ ├── utils.sh # Session isolation utilities
189-
│ ├── circuit_breaker.sh # Stagnation detection
190-
│ ├── response_analyzer.sh # Completion analysis
191-
│ ├── api_limit_handler.sh # Rate limit handling
192-
│ └── task_manager.sh # Task state management
193111
└── scripts/
194112
├── setup-ralph-loop.sh # Loop initialization
195113
├── status.sh # Status display
196114
└── cancel-ralph.sh # Cancellation handler
197-
198-
Project directory (.claude/):
199-
├── ralph-session.local/
200-
│ ├── ralph-loop.{session_id}.local.md
201-
│ ├── ralph-circuit.{session_id}.json
202-
│ ├── ralph-analysis.{session_id}.json
203-
│ ├── ralph-limits.{session_id}.json
204-
│ └── ralph-tasks.{session_id}.json
205-
```
206-
207-
## When to Use
208-
209-
**Good for:**
210-
- Tasks with clear success criteria (tests pass, build succeeds)
211-
- Iterative refinement (debugging, optimization)
212-
- Greenfield implementation with defined requirements
213-
214-
**Not for:**
215-
- Tasks requiring human judgment or design decisions
216-
- One-shot operations
217-
- Production debugging
218-
219-
## Troubleshooting
220-
221-
### Loop won't start
222-
223-
1. Check if another loop is active: `/status`
224-
2. Cancel any existing loop: `/cancel-ralph`
225-
3. Try again with your prompt
226-
227-
### Loop exits too early
228-
229-
The smart exit feature may trigger on completion keywords. Options:
230-
- Use `--no-smart-exit` to disable completion detection
231-
- Add more specific requirements to your prompt
232-
- Check `.claude/ralph-session.local/ralph-analysis.{session_id}.json` to see what triggered the exit
233-
234-
### Loop runs forever
235-
236-
Ensure your prompt includes:
237-
- Clear, testable completion criteria
238-
- A `<promise>` tag if using `--completion-promise`
239-
- Measurable success conditions (tests pass, build succeeds)
240-
241-
### Circuit breaker keeps triggering
242-
243-
The circuit breaker stops loops that aren't making progress:
244-
- Ensure each iteration changes files
245-
- Check `.claude/ralph-session.local/ralph-circuit.{session_id}.json` for details
246-
- Use `--reset-circuit` to reset state if needed
247-
248-
### Rate limits
249-
250-
If you hit Claude's API limits:
251-
- The loop pauses automatically
252-
- Wait for the recommended duration
253-
- Resume with `/ralph-loop` using the same prompt
254-
255-
### Debugging
256-
257-
```bash
258-
# Check all session state files
259-
ls -la .claude/ralph-session.local/
260-
261-
# View loop configuration (use session_id from /status)
262-
cat .claude/ralph-session.local/ralph-loop.{session_id}.local.md
263-
264-
# Check circuit breaker state
265-
cat .claude/ralph-session.local/ralph-circuit.{session_id}.json
266-
267-
# Review completion analysis
268-
cat .claude/ralph-session.local/ralph-analysis.{session_id}.json
269115
```
270116

271-
## Changelog
272-
273-
### [1.1.0] - Session Isolation
274-
275-
**Added**
276-
- Session isolation: State files now stored as `.claude/ralph-session.local/{name}.{session_id}.{ext}`
277-
- SessionStart hook: Captures session_id at session start
278-
- Session ID display: `/status` shows current session ID
279-
- lib/utils.sh: New utility module for session isolation
280-
281-
**Fixed**
282-
- Cross-session interference: Stop hook now only processes state belonging to its session, preventing multiple Claude Code sessions from hijacking each other's loops
283-
284-
**Changed**
285-
- State file location moved from `.claude/ralph-*.local.*` to `.claude/ralph-session.local/{name}.{session_id}.{ext}`
286-
- All library modules use dynamic path resolution
287-
- `.local` suffix on directory ensures gitignore compatibility
288-
289-
### [1.0.1] - Initial Release
290-
291-
Self-referential development loop with circuit breaker, smart exit, and API limit handling.
292-
293117
## References
294118

295119
- [ghuntley.com/ralph](https://ghuntley.com/ralph/)
120+
- [Official Anthropic plugin](https://github.com/anthropics/claude-code/tree/main/plugins/ralph-wiggum)
296121
- [frankbria/ralph-claude-code](https://github.com/frankbria/ralph-claude-code)
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
---
22
description: "Cancel active Ralph Wiggum loop"
3-
allowed-tools: ["Bash"]
4-
hide-from-slash-command-tool: "true"
3+
allowed-tools: ["Bash(${CLAUDE_PLUGIN_ROOT}/scripts/cancel-ralph.sh)"]
54
---
65

7-
# Cancel Ralph
6+
Cancel the active Ralph loop:
87

9-
Check if a Ralph loop is active and cancel it: !`bash "${CLAUDE_PLUGIN_ROOT}/scripts/cancel-ralph.sh"`
8+
```!
9+
"${CLAUDE_PLUGIN_ROOT}/scripts/cancel-ralph.sh"
10+
```

0 commit comments

Comments
 (0)