@canivel/ralph 0.2.0

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 (40) hide show
  1. package/.agents/ralph/PROMPT_build.md +126 -0
  2. package/.agents/ralph/agents.sh +15 -0
  3. package/.agents/ralph/config.sh +25 -0
  4. package/.agents/ralph/log-activity.sh +15 -0
  5. package/.agents/ralph/loop.sh +1001 -0
  6. package/.agents/ralph/references/CONTEXT_ENGINEERING.md +126 -0
  7. package/.agents/ralph/references/GUARDRAILS.md +174 -0
  8. package/AGENTS.md +20 -0
  9. package/README.md +266 -0
  10. package/bin/ralph +766 -0
  11. package/diagram.svg +55 -0
  12. package/examples/commands.md +46 -0
  13. package/package.json +39 -0
  14. package/ralph.webp +0 -0
  15. package/skills/commit/SKILL.md +219 -0
  16. package/skills/commit/references/commit_examples.md +292 -0
  17. package/skills/dev-browser/SKILL.md +211 -0
  18. package/skills/dev-browser/bun.lock +443 -0
  19. package/skills/dev-browser/package-lock.json +2988 -0
  20. package/skills/dev-browser/package.json +31 -0
  21. package/skills/dev-browser/references/scraping.md +155 -0
  22. package/skills/dev-browser/scripts/start-relay.ts +32 -0
  23. package/skills/dev-browser/scripts/start-server.ts +117 -0
  24. package/skills/dev-browser/server.sh +24 -0
  25. package/skills/dev-browser/src/client.ts +474 -0
  26. package/skills/dev-browser/src/index.ts +287 -0
  27. package/skills/dev-browser/src/relay.ts +731 -0
  28. package/skills/dev-browser/src/snapshot/__tests__/snapshot.test.ts +223 -0
  29. package/skills/dev-browser/src/snapshot/browser-script.ts +877 -0
  30. package/skills/dev-browser/src/snapshot/index.ts +14 -0
  31. package/skills/dev-browser/src/snapshot/inject.ts +13 -0
  32. package/skills/dev-browser/src/types.ts +34 -0
  33. package/skills/dev-browser/tsconfig.json +36 -0
  34. package/skills/dev-browser/vitest.config.ts +12 -0
  35. package/skills/prd/SKILL.md +235 -0
  36. package/tests/agent-loops.mjs +79 -0
  37. package/tests/agent-ping.mjs +39 -0
  38. package/tests/audit.md +56 -0
  39. package/tests/cli-smoke.mjs +47 -0
  40. package/tests/real-agents.mjs +127 -0
@@ -0,0 +1,126 @@
1
+ # Context Engineering Reference
2
+
3
+ This document explains the malloc/free metaphor for LLM context management that underlies the Ralph technique.
4
+
5
+ ## The malloc() Metaphor
6
+
7
+ In traditional programming:
8
+ - `malloc()` allocates memory
9
+ - `free()` releases memory
10
+ - Memory leaks occur when you allocate without freeing
11
+
12
+ In LLM context:
13
+ - Reading files, receiving responses, tool outputs = `malloc()`
14
+ - **There is no `free()`** - context cannot be released
15
+ - The only way to "free" is to start a new conversation
16
+
17
+ ## Why This Matters
18
+
19
+ ### Context Pollution
20
+
21
+ When you work on multiple unrelated tasks in the same context:
22
+
23
+ ```
24
+ Task 1: Build authentication → context contains auth code, JWT docs, security patterns
25
+ Task 2: Build UI components → context now ALSO contains auth stuff
26
+
27
+ Result: LLM might suggest auth-related patterns when building UI
28
+ or mix concerns inappropriately
29
+ ```
30
+
31
+ ### Autoregressive Failure
32
+
33
+ LLMs predict the next token based on ALL context. When context contains:
34
+ - Unrelated information
35
+ - Failed attempts
36
+ - Mixed concerns
37
+
38
+ The model can "spiral" into wrong territory, generating increasingly off-base responses.
39
+
40
+ ### The Gutter Metaphor
41
+
42
+ > "If the bowling ball is in the gutter, there's no saving it."
43
+
44
+ Once context is polluted with failed attempts or mixed concerns, the model will keep referencing that pollution. Starting fresh is often faster than trying to correct course.
45
+
46
+ ## Context Health Indicators
47
+
48
+ ### 🟢 Healthy Context
49
+ - Single focused task
50
+ - Relevant files only
51
+ - Clear progress
52
+ - Under 60% capacity
53
+
54
+ ### 🟡 Warning Signs
55
+ - Multiple unrelated topics discussed
56
+ - Several failed attempts in history
57
+ - Approaching 80% capacity
58
+ - Repeated similar errors
59
+
60
+ ### 🔴 Critical / Gutter
61
+ - Mixed concerns throughout
62
+ - Circular failure patterns
63
+ - Over 90% capacity
64
+ - Model suggesting irrelevant solutions
65
+
66
+ ## Best Practices
67
+
68
+ ### 1. One Task Per Context
69
+
70
+ Don't ask "fix the auth bug AND add the new feature". Do them in separate conversations.
71
+
72
+ ### 2. Fresh Start on Topic Change
73
+
74
+ Finished auth? Start a new conversation for the next feature.
75
+
76
+ ### 3. Don't Redline
77
+
78
+ Stay under 80% of context capacity. Quality degrades as you approach limits.
79
+
80
+ ### 4. Recognize the Gutter
81
+
82
+ If you're seeing:
83
+ - Same error 3+ times
84
+ - Solutions that don't match the problem
85
+ - Circular suggestions
86
+
87
+ Start fresh. Your progress is in the files.
88
+
89
+ ### 5. State in Files, Not Context
90
+
91
+ Write progress to files. The next conversation can read them. Context is ephemeral; files are permanent.
92
+
93
+ ## Ralph's Approach
94
+
95
+ The original Ralph technique (`while :; do cat PROMPT.md | agent ; done`) naturally implements these principles:
96
+
97
+ 1. **Each iteration is a fresh process** - Context is freed
98
+ 2. **State persists in files** - Progress survives context resets
99
+ 3. **Same prompt each time** - Focused, single-task context
100
+ 4. **Failures inform guardrails** - Learning without context pollution
101
+
102
+ This Cursor implementation aims to bring these benefits while working within Cursor's session model.
103
+
104
+ ## Measuring Context
105
+
106
+ Rough estimates:
107
+ - 1 token ≈ 4 characters
108
+ - Average code file: 500-2000 tokens
109
+ - Large file: 5000+ tokens
110
+ - Conversation history: 100-500 tokens per exchange
111
+
112
+ Track allocations in `.ralph/context-log.md` to stay aware.
113
+
114
+ ## When to Start Fresh
115
+
116
+ **Definitely start fresh when:**
117
+ - Switching to unrelated task
118
+ - Context over 90% full
119
+ - Same error 3+ times
120
+ - Model suggestions are off-topic
121
+
122
+ **Consider starting fresh when:**
123
+ - Context over 70% full
124
+ - Significant topic shift within task
125
+ - Feeling "stuck"
126
+ - Multiple failed approaches in history
@@ -0,0 +1,174 @@
1
+ # Guardrails Reference ("Signs")
2
+
3
+ This document explains how to create and use guardrails in Ralph.
4
+
5
+ ## The Signs Metaphor
6
+
7
+ From Geoffrey Huntley:
8
+
9
+ > "Ralph is very good at making playgrounds, but he comes home bruised because he fell off the slide, so one then tunes Ralph by adding a sign next to the slide saying 'SLIDE DOWN, DON'T JUMP, LOOK AROUND,' and Ralph is more likely to look and see the sign."
10
+
11
+ Signs are explicit instructions added to prevent known failure modes.
12
+
13
+ ## Anatomy of a Sign
14
+
15
+ ```markdown
16
+ ### Sign: [Descriptive Name]
17
+ - **Trigger**: When this situation occurs
18
+ - **Instruction**: What to do instead
19
+ - **Added after**: When/why this was added
20
+ - **Example**: Concrete example if helpful
21
+ ```
22
+
23
+ ## Types of Signs
24
+
25
+ ### 1. Preventive Signs
26
+
27
+ Stop problems before they happen:
28
+
29
+ ```markdown
30
+ ### Sign: Validate Before Trust
31
+ - **Trigger**: When receiving external input
32
+ - **Instruction**: Always validate and sanitize input before using it
33
+ - **Added after**: Iteration 3 - SQL injection vulnerability
34
+ ```
35
+
36
+ ### 2. Corrective Signs
37
+
38
+ Fix recurring mistakes:
39
+
40
+ ```markdown
41
+ ### Sign: Check Return Values
42
+ - **Trigger**: When calling functions that can fail
43
+ - **Instruction**: Always check return values and handle errors
44
+ - **Added after**: Iteration 7 - Null pointer exception
45
+ ```
46
+
47
+ ### 3. Process Signs
48
+
49
+ Enforce good practices:
50
+
51
+ ```markdown
52
+ ### Sign: Test Before Commit
53
+ - **Trigger**: Before committing changes
54
+ - **Instruction**: Run the test suite and ensure all tests pass
55
+ - **Added after**: Iteration 2 - Broken tests committed
56
+ ```
57
+
58
+ ### 4. Architecture Signs
59
+
60
+ Guide design decisions:
61
+
62
+ ```markdown
63
+ ### Sign: Single Responsibility
64
+ - **Trigger**: When a function grows beyond 50 lines
65
+ - **Instruction**: Consider splitting into smaller, focused functions
66
+ - **Added after**: Iteration 12 - Unmaintainable god function
67
+ ```
68
+
69
+ ## When to Add Signs
70
+
71
+ Add a sign when:
72
+
73
+ 1. **The same mistake happens twice** - Once is learning, twice is a pattern
74
+ 2. **A subtle bug is found** - Prevent future occurrences
75
+ 3. **A best practice is violated** - Reinforce good habits
76
+ 4. **Context-specific knowledge is needed** - Project-specific conventions
77
+
78
+ ## Sign Lifecycle
79
+
80
+ ### Creation
81
+
82
+ ```markdown
83
+ ### Sign: [New Sign]
84
+ - **Trigger**: [When it applies]
85
+ - **Instruction**: [What to do]
86
+ - **Added after**: Iteration N - [What happened]
87
+ ```
88
+
89
+ ### Refinement
90
+
91
+ If a sign isn't working:
92
+ - Make the trigger more specific
93
+ - Make the instruction clearer
94
+ - Add examples
95
+
96
+ ### Retirement
97
+
98
+ Signs can be removed when:
99
+ - The underlying issue is fixed at a deeper level
100
+ - The sign is no longer relevant
101
+ - The sign is causing more problems than it solves
102
+
103
+ ## Example Signs Library
104
+
105
+ ### Security
106
+
107
+ ```markdown
108
+ ### Sign: Sanitize All Input
109
+ - **Trigger**: Any user-provided data
110
+ - **Instruction**: Use parameterized queries, escape HTML, validate types
111
+ - **Example**: `db.query("SELECT * FROM users WHERE id = ?", [userId])`
112
+ ```
113
+
114
+ ### Error Handling
115
+
116
+ ```markdown
117
+ ### Sign: Graceful Degradation
118
+ - **Trigger**: External service calls
119
+ - **Instruction**: Always have a fallback for when services are unavailable
120
+ - **Example**: Cache results, provide default values, show friendly errors
121
+ ```
122
+
123
+ ### Testing
124
+
125
+ ```markdown
126
+ ### Sign: Test the Unhappy Path
127
+ - **Trigger**: Writing tests for new functionality
128
+ - **Instruction**: Include tests for error cases, edge cases, and invalid input
129
+ ```
130
+
131
+ ### Code Quality
132
+
133
+ ```markdown
134
+ ### Sign: Explain Why, Not What
135
+ - **Trigger**: Writing comments
136
+ - **Instruction**: Comments should explain reasoning, not describe obvious code
137
+ - **Example**: `// Using retry because API is flaky under load` not `// Call the API`
138
+ ```
139
+
140
+ ## Automatic Sign Detection
141
+
142
+ The Ralph hooks can automatically detect some patterns and suggest signs:
143
+
144
+ - **Thrashing**: Same file edited many times → "Step back and reconsider"
145
+ - **Repeated errors**: Same test failing → "Check the test assumptions"
146
+ - **Large changes**: Big diffs → "Consider smaller increments"
147
+
148
+ These are logged in `.ralph/failures.md` and can be promoted to guardrails.
149
+
150
+ ## Using Signs Effectively
151
+
152
+ ### Do
153
+
154
+ - Keep signs concise and actionable
155
+ - Include concrete examples
156
+ - Update signs when they're not working
157
+ - Remove outdated signs
158
+
159
+ ### Don't
160
+
161
+ - Add signs for every minor issue
162
+ - Make signs too vague ("be careful")
163
+ - Ignore signs that keep triggering
164
+ - Let the guardrails file become overwhelming
165
+
166
+ ## Integration with Ralph
167
+
168
+ Signs are:
169
+ 1. Stored in `.ralph/guardrails.md`
170
+ 2. Injected into context at the start of each iteration
171
+ 3. Referenced when relevant situations arise
172
+ 4. Updated based on observed failures
173
+
174
+ The goal is a self-improving system where each failure makes future iterations smarter.
package/AGENTS.md ADDED
@@ -0,0 +1,20 @@
1
+ # AGENTS
2
+
3
+ Keep this file short. It is always loaded into context.
4
+
5
+ ## Build & test
6
+ - No build step.
7
+ - Tests (dry-run): `npm test`
8
+ - Fast real agent check: `npm run test:ping`
9
+ - Full real loop: `npm run test:real`
10
+
11
+ ## CLI shape
12
+ - CLI entry: `bin/ralph`
13
+ - Templates: `.agents/ralph/` (copied to repos on install)
14
+ - State/logs: `.ralph/` (local only)
15
+ - Skills: `skills/`
16
+ - Tests: `tests/`
17
+ - Docs/examples: `README.md`, `examples/`
18
+
19
+ ## Quirks / Guardrails
20
+ **Add any common quirks guiderails here as needed**
package/README.md ADDED
@@ -0,0 +1,266 @@
1
+ # Ralph
2
+
3
+ ![Ralph](ralph.webp)
4
+
5
+ Ralph is a minimal, file-based agent loop for autonomous coding. Each iteration starts fresh, reads the same on-disk state, and commits work for one story at a time.
6
+
7
+ > **Fork Note:** This is a fork of [@iannuttall/ralph](https://github.com/iannuttall/ralph) with improved agent support and first-run configuration.
8
+
9
+ ## What's New in This Fork
10
+
11
+ - **First-run agent selection** - Prompted to choose your default agent on first use
12
+ - **`ralph config` command** - Reconfigure your default agent anytime
13
+ - **Improved Claude support** - Direct spawning with proper TTY handling for PRD generation
14
+ - **Better Windows compatibility** - Fixed shell quoting issues
15
+ - **Global config storage** - Settings persist in `~/.ralph/config.json`
16
+
17
+ ## How It Works
18
+
19
+ Ralph treats **files and git** as memory, not the model context:
20
+
21
+ - **PRD (JSON)** - Defines stories, quality gates, and status
22
+ - **Loop** - Executes one story per iteration
23
+ - **State** - Persists in `.ralph/` directory
24
+
25
+ ![Ralph architecture](diagram.svg)
26
+
27
+ ## Installation
28
+
29
+ ### From npm (recommended)
30
+
31
+ ```bash
32
+ npm i -g @canivel/ralph
33
+ ```
34
+
35
+ ### From source
36
+
37
+ ```bash
38
+ git clone https://github.com/canivel/ralph.git
39
+ cd ralph
40
+ npm install
41
+ npm link
42
+ ```
43
+
44
+ ### Prerequisites
45
+
46
+ You need at least one AI agent CLI installed:
47
+
48
+ | Agent | Install Command |
49
+ |-------|-----------------|
50
+ | Claude | `curl -fsSL https://claude.ai/install.sh \| bash` |
51
+ | Codex | `npm i -g @openai/codex` |
52
+ | Droid | `curl -fsSL https://app.factory.ai/cli \| sh` |
53
+ | OpenCode | `curl -fsSL https://opencode.ai/install.sh \| bash` |
54
+
55
+ ## Quick Start
56
+
57
+ ```bash
58
+ # 1. Navigate to your project
59
+ cd my-project
60
+
61
+ # 2. Generate a PRD (first run prompts for agent selection)
62
+ ralph prd "A task management app with projects and due dates"
63
+
64
+ # 3. Build one story at a time
65
+ ralph build 1
66
+ ```
67
+
68
+ On first run, you'll see the agent selection prompt:
69
+
70
+ ```
71
+ Ralph Configuration
72
+ ? Select your default agent
73
+ > claude (Anthropic Claude CLI)
74
+ codex (OpenAI Codex CLI)
75
+ droid (Factory Droid CLI)
76
+ opencode (OpenCode CLI)
77
+ ```
78
+
79
+ ## Commands
80
+
81
+ | Command | Description |
82
+ |---------|-------------|
83
+ | `ralph prd ["<request>"]` | Generate a PRD JSON file via agent |
84
+ | `ralph build [n]` | Run n build iterations (default: continuous) |
85
+ | `ralph config` | Configure or change default agent |
86
+ | `ralph install` | Copy templates to current repo for customization |
87
+ | `ralph install --skills` | Install required skills (commit, dev-browser, prd) |
88
+ | `ralph overview` | Generate human-readable overview from PRD |
89
+ | `ralph ping` | Health check for agent connectivity |
90
+ | `ralph log "<message>"` | Append to activity log |
91
+ | `ralph help` | Show help message |
92
+
93
+ ### Options
94
+
95
+ | Option | Description |
96
+ |--------|-------------|
97
+ | `--agent <name>` | Override agent (codex, claude, droid, opencode) |
98
+ | `--prd <path>` | Override PRD file path |
99
+ | `--out <path>` | Override PRD output path (for `prd` command) |
100
+ | `--progress <path>` | Override progress log path |
101
+ | `--no-commit` | Dry run without committing (for `build` command) |
102
+ | `--force` | Force overwrite (for `install` command) |
103
+
104
+ ## Usage Examples
105
+
106
+ ### Generate a PRD
107
+
108
+ ```bash
109
+ # Interactive mode - prompts for description
110
+ ralph prd
111
+
112
+ # Direct mode - pass description as argument
113
+ ralph prd "A REST API for user authentication with JWT tokens"
114
+
115
+ # Specify output path
116
+ ralph prd --out .agents/tasks/prd-auth.json "Auth API"
117
+ ```
118
+
119
+ ### Build Stories
120
+
121
+ ```bash
122
+ # Build one story
123
+ ralph build 1
124
+
125
+ # Build 5 stories
126
+ ralph build 5
127
+
128
+ # Dry run (no commits)
129
+ ralph build 1 --no-commit
130
+
131
+ # Use specific PRD file
132
+ ralph build 1 --prd .agents/tasks/prd-auth.json
133
+
134
+ # Override agent for this run
135
+ ralph build 1 --agent codex
136
+ ```
137
+
138
+ ### Configuration
139
+
140
+ ```bash
141
+ # Change default agent
142
+ ralph config
143
+
144
+ # Install templates for customization
145
+ ralph install
146
+
147
+ # Install skills (commit, dev-browser, prd)
148
+ ralph install --skills
149
+ ```
150
+
151
+ ## Configuration
152
+
153
+ ### Global Config
154
+
155
+ Ralph stores global settings in `~/.ralph/config.json`:
156
+
157
+ ```json
158
+ {
159
+ "defaultAgent": "claude",
160
+ "configuredAt": "2026-01-19T12:00:00.000Z"
161
+ }
162
+ ```
163
+
164
+ To change your default agent:
165
+
166
+ ```bash
167
+ ralph config
168
+ ```
169
+
170
+ ### Project Config
171
+
172
+ After running `ralph install`, you can customize behavior in `.agents/ralph/config.sh`:
173
+
174
+ ```bash
175
+ # Override agent command
176
+ AGENT_CMD="claude -p --dangerously-skip-permissions \"\$(cat {prompt})\""
177
+
178
+ # Build settings
179
+ NO_COMMIT=false
180
+ MAX_ITERATIONS=25
181
+ STALE_SECONDS=0
182
+ ```
183
+
184
+ ## Template Hierarchy
185
+
186
+ Ralph looks for templates in this order:
187
+
188
+ 1. `.agents/ralph/` in current project (if present)
189
+ 2. Bundled defaults from the package
190
+
191
+ State and logs always go to `.ralph/` in the project.
192
+
193
+ ## PRD Story Status
194
+
195
+ The build loop automatically updates story status:
196
+
197
+ | Status | Meaning |
198
+ |--------|---------|
199
+ | `open` | Available for selection |
200
+ | `in_progress` | Currently being worked on (with `startedAt`) |
201
+ | `done` | Completed (with `completedAt`) |
202
+
203
+ If a loop crashes while a story is `in_progress`, set `STALE_SECONDS` in config to auto-reopen stalled stories.
204
+
205
+ ## State Files
206
+
207
+ All state is stored in `.ralph/` in your project:
208
+
209
+ | File | Purpose |
210
+ |------|---------|
211
+ | `progress.md` | Append-only progress log |
212
+ | `guardrails.md` | Lessons learned ("Signs") |
213
+ | `activity.log` | Activity and timing log |
214
+ | `errors.log` | Repeated failures and notes |
215
+ | `runs/` | Raw run logs and summaries |
216
+
217
+ ## Advanced
218
+
219
+ ### Multiple PRD Files
220
+
221
+ If you have multiple PRD JSON files in `.agents/tasks/` and don't specify `--prd`, Ralph will prompt you to choose.
222
+
223
+ ### OpenCode Server Mode
224
+
225
+ For faster performance with OpenCode, run `opencode serve` in a separate terminal and uncomment the server mode lines in `.agents/ralph/agents.sh`:
226
+
227
+ ```bash
228
+ AGENT_OPENCODE_CMD="opencode run --attach http://localhost:4096 \"\$(cat {prompt})\""
229
+ ```
230
+
231
+ ### Custom Agent Commands
232
+
233
+ Use `{prompt}` placeholder when the agent needs a file path instead of stdin:
234
+
235
+ ```bash
236
+ AGENT_CMD="my-agent --file {prompt}"
237
+ ```
238
+
239
+ ## Development
240
+
241
+ ### Running Tests
242
+
243
+ ```bash
244
+ # Dry-run smoke tests (no agent required)
245
+ npm test
246
+
247
+ # Fast agent health check
248
+ npm run test:ping
249
+
250
+ # Integration tests (requires agents)
251
+ RALPH_INTEGRATION=1 npm test
252
+
253
+ # Full real-agent loop test
254
+ npm run test:real
255
+ ```
256
+
257
+ ### Publishing
258
+
259
+ ```bash
260
+ npm login
261
+ npm publish --access public
262
+ ```
263
+
264
+ ## License
265
+
266
+ MIT