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,633 @@
1
+ # CCS-Ralph Integration Design
2
+
3
+ **Date**: 2026-01-07
4
+ **Author**: Claude (Ultrathink Analysis)
5
+ **Related**: SPEC-ANV-context-checkpoints.md, Ralph Wiggum System
6
+
7
+ ---
8
+
9
+ ## Executive Summary
10
+
11
+ Ralph Wiggum is a continuous-loop autonomous execution system. It WILL hit context limits frequently during overnight runs. The Context Checkpoint System (CCS) must be deeply integrated into Ralph to enable graceful handoffs within and across iterations.
12
+
13
+ **Key Insight**: CCS must operate at TWO levels:
14
+ 1. **Micro-level**: Within a single Ralph iteration (single Claude Code session)
15
+ 2. **Macro-level**: Across Ralph iterations (loop continuity)
16
+
17
+ ---
18
+
19
+ ## Ralph Architecture Analysis
20
+
21
+ ### Current Components
22
+
23
+ | Component | File | Purpose |
24
+ |-----------|------|---------|
25
+ | Command | `global/commands/ralph.md` | User-facing `/ralph` commands |
26
+ | State Manager | `global/lib/ralph_state.py` | Python state management |
27
+ | Loop Script | `global/tools/ralph-loop.sh` | External bash loop |
28
+ | Stop Hook | `global/hooks/ralph_stop.sh` | Exit decision logic |
29
+
30
+ ### Current Flow
31
+
32
+ ```
33
+ ┌─────────────────────────────────────────────────────────────────────┐
34
+ │ CURRENT RALPH FLOW │
35
+ ├─────────────────────────────────────────────────────────────────────┤
36
+ │ │
37
+ │ ralph-loop.sh │
38
+ │ ┌──────────────────────────────────────────────────────────────┐ │
39
+ │ │ while true; do │ │
40
+ │ │ cat PROMPT.md | npx claude-code --print │ │
41
+ │ │ # Claude Code runs until session ends │ │
42
+ │ │ # ralph_stop.sh fires as Stop hook │ │
43
+ │ │ # Exit code decides: 0 = stop loop, 1 = continue │ │
44
+ │ │ done │ │
45
+ │ └──────────────────────────────────────────────────────────────┘ │
46
+ │ │
47
+ │ ralph_stop.sh checks: │
48
+ │ ├── completion promise? → exit 0 (task done) │
49
+ │ ├── fatal error? → exit 0 (unrecoverable) │
50
+ │ ├── max iterations? → exit 0 (limit reached) │
51
+ │ ├── circuit breaker? → exit 0 (stuck) │
52
+ │ └── else → exit 1 (continue loop) │
53
+ │ │
54
+ └─────────────────────────────────────────────────────────────────────┘
55
+ ```
56
+
57
+ ### Critical Gap: No Context Awareness
58
+
59
+ **The Problem**: Ralph has NO awareness of context window usage.
60
+
61
+ | Scenario | Current Behavior | Consequence |
62
+ |----------|------------------|-------------|
63
+ | Context fills up | Auto-compact triggers | Lossy summarization, forgets context |
64
+ | Auto-compact disabled | Session ends abruptly | No continuation, work lost |
65
+ | Long iteration | Context exhausted mid-task | Incomplete work, no handoff |
66
+ | Complex task | Multiple iterations needed | No context carryover |
67
+
68
+ ---
69
+
70
+ ## CCS-Ralph Integration Design
71
+
72
+ ### Two-Level Integration Model
73
+
74
+ ```
75
+ ┌─────────────────────────────────────────────────────────────────────┐
76
+ │ CCS-RALPH INTEGRATION MODEL │
77
+ ├─────────────────────────────────────────────────────────────────────┤
78
+ │ │
79
+ │ MACRO LEVEL: Across Iterations │
80
+ │ ┌──────────────────────────────────────────────────────────────┐ │
81
+ │ │ ralph-loop.sh │ │
82
+ │ │ ┌────────┐ ┌────────┐ ┌────────┐ ┌────────┐ │ │
83
+ │ │ │ Iter 1 │───▶│ Iter 2 │───▶│ Iter 3 │───▶│ Iter N │ │ │
84
+ │ │ └────────┘ └────────┘ └────────┘ └────────┘ │ │
85
+ │ │ │ │ │ │ │ │
86
+ │ │ ▼ ▼ ▼ ▼ │ │
87
+ │ │ [Checkpoint] [Resume] [Resume] [Complete] │ │
88
+ │ │ │ │
89
+ │ │ State passed via ralph-state.json + handoff docs │ │
90
+ │ └──────────────────────────────────────────────────────────────┘ │
91
+ │ │
92
+ │ MICRO LEVEL: Within Single Iteration │
93
+ │ ┌──────────────────────────────────────────────────────────────┐ │
94
+ │ │ Claude Code Session (Iteration N) │ │
95
+ │ │ │ │
96
+ │ │ ┌─────┐ ┌─────┐ ┌─────┐ ┌─────┐ ┌─────┐ │ │
97
+ │ │ │Tool │───▶│Tool │───▶│Tool │───▶│Tool │───▶│Tool │ │ │
98
+ │ │ │ #1 │ │ #2 │ │ #3 │ │ #N │ │ #M │ │ │
99
+ │ │ └─────┘ └─────┘ └─────┘ └─────┘ └─────┘ │ │
100
+ │ │ │ │ │ │ │ │ │
101
+ │ │ ▼ ▼ ▼ ▼ ▼ │ │
102
+ │ │ [20%] [35%] [55%] [72%] [86%] │ │
103
+ │ │ │ │ │ │
104
+ │ │ L1 ⚠️ L2 🔶 │ │
105
+ │ │ Warning Checkpoint │ │
106
+ │ │ │ │ │
107
+ │ │ ▼ │ │
108
+ │ │ [Generate Handoff] │ │
109
+ │ │ [Update State] │ │
110
+ │ │ [Signal Stop] │ │
111
+ │ └──────────────────────────────────────────────────────────────┘ │
112
+ │ │
113
+ └─────────────────────────────────────────────────────────────────────┘
114
+ ```
115
+
116
+ ### New Data Flow
117
+
118
+ ```
119
+ Iteration N starts
120
+
121
+ ├── 1. PROMPT.md loaded (includes CCS checkpoint data if resuming)
122
+
123
+ ├── 2. PostToolUse hook fires after each tool call
124
+ │ │
125
+ │ ├── Poll context percentage from Claude Code
126
+ │ │
127
+ │ ├── L0-L1 (0-84%): Continue working normally
128
+ │ │
129
+ │ └── L2 (85%+): Trigger CCS checkpoint sequence
130
+ │ │
131
+ │ ├── A. Complete current edit (max 60 seconds)
132
+ │ ├── B. Commit WIP with checkpoint message
133
+ │ ├── C. Generate CCS handoff document
134
+ │ ├── D. Update ralph-state.json with checkpoint data
135
+ │ └── E. Signal session stop → goes to ralph_stop.sh
136
+
137
+ ├── 3. ralph_stop.sh fires
138
+ │ │
139
+ │ ├── Check: Completion promise? → status=completed, exit 0
140
+ │ │
141
+ │ ├── Check: CCS checkpoint? → NEW PATH
142
+ │ │ ├── Preserve checkpoint data
143
+ │ │ ├── Regenerate PROMPT.md with resume context
144
+ │ │ └── exit 1 (restart with checkpoint)
145
+ │ │
146
+ │ ├── Check: Fatal error? → status=fatal_error, exit 0
147
+ │ ├── Check: Circuit breaker? → status=circuit_breaker, exit 0
148
+ │ └── Default: exit 1 (continue)
149
+
150
+ └── 4. If exit 1: Loop continues with Iteration N+1
151
+
152
+ └── PROMPT.md now contains:
153
+ ├── Original task context
154
+ ├── CCS checkpoint summary
155
+ ├── Handoff document reference
156
+ └── Resume instructions
157
+ ```
158
+
159
+ ---
160
+
161
+ ## Enhanced State Model
162
+
163
+ ### ralph-state.json Additions
164
+
165
+ ```json
166
+ {
167
+ "task_name": "Migrate Jest to Vitest",
168
+ "objective": "Convert all test files from Jest to Vitest",
169
+ "iteration": 5,
170
+ "started_at": "2026-01-07T00:00:00Z",
171
+ "status": "running",
172
+
173
+ // Existing fields...
174
+ "no_change_count": 0,
175
+ "last_diff_hash": "abc123",
176
+ "error_hashes": [],
177
+ "max_iterations": 50,
178
+ "completion_promise": "COMPLETE",
179
+ "todo_items": ["Item 3", "Item 4"],
180
+ "completed_items": ["Item 1", "Item 2"],
181
+
182
+ // NEW: CCS Integration Fields
183
+ "context_checkpoint": {
184
+ "active": true,
185
+ "level": "L2",
186
+ "percent_at_checkpoint": 87,
187
+ "timestamp": "2026-01-07T11:45:00Z",
188
+ "handoff_file": ".claude/handoffs/2026-01-07-1145.md",
189
+ "resume_summary": "Implementing OAuth callback handler, completed token validation",
190
+ "files_in_progress": [
191
+ {
192
+ "path": "src/auth/oauth.ts",
193
+ "lines": "45-120",
194
+ "description": "OAuth callback handler"
195
+ }
196
+ ],
197
+ "current_todo_item": "Implement OAuth callback",
198
+ "progress_on_item": "70%"
199
+ },
200
+
201
+ // NEW: Context history for pattern detection
202
+ "context_history": [
203
+ {"iteration": 1, "peak_percent": 45, "checkpoint": false},
204
+ {"iteration": 2, "peak_percent": 62, "checkpoint": false},
205
+ {"iteration": 3, "peak_percent": 88, "checkpoint": true},
206
+ {"iteration": 4, "peak_percent": 56, "checkpoint": false},
207
+ {"iteration": 5, "peak_percent": 87, "checkpoint": true}
208
+ ]
209
+ }
210
+ ```
211
+
212
+ ### Enhanced PROMPT.md Template
213
+
214
+ ```markdown
215
+ # Task: Migrate Jest to Vitest
216
+
217
+ ## Objective
218
+ Convert all test files from Jest to Vitest
219
+
220
+ ## Current State
221
+ - **Iteration**: 5
222
+ - **Last action**: OAuth callback implementation (70% complete)
223
+ - **Remaining items**: 3 of 5
224
+ - **Started**: 2026-01-07T00:00:00Z
225
+
226
+ ---
227
+
228
+ ## CONTEXT CHECKPOINT RESUME
229
+
230
+ > ⚠️ This iteration is resuming from a context checkpoint.
231
+ > The previous session hit 87% context (L2 threshold).
232
+
233
+ ### What Was In Progress
234
+ Implementing OAuth callback handler. Completed token validation,
235
+ was working on callback URL parsing when checkpoint triggered.
236
+
237
+ ### Files To Resume
238
+ | File | Lines | Status |
239
+ |------|-------|--------|
240
+ | src/auth/oauth.ts | 45-120 | 70% complete |
241
+
242
+ ### Handoff Document
243
+ Read `.claude/handoffs/2026-01-07-1145.md` for full context.
244
+
245
+ ### Resume Instructions
246
+ 1. **DO NOT restart the task from scratch**
247
+ 2. Read the handoff document first
248
+ 3. Continue from line 90 of oauth.ts
249
+ 4. The token validation is complete, focus on URL parsing
250
+
251
+ ---
252
+
253
+ ## Instructions
254
+
255
+ ### Step 1: Check If Resuming
256
+ If `context_checkpoint.active == true`:
257
+ 1. Read the handoff document
258
+ 2. Continue from where previous session left off
259
+ 3. DO NOT re-read files already understood
260
+
261
+ ### Step 2: Execute ONE Item
262
+ ... (rest of standard PROMPT.md template)
263
+ ```
264
+
265
+ ---
266
+
267
+ ## Hook Integration
268
+
269
+ ### PostToolUse Hook Enhancement
270
+
271
+ Create new hook: `global/hooks/ralph_context_monitor.py`
272
+
273
+ ```python
274
+ """
275
+ ralph_context_monitor.py - CCS Monitor for Ralph Mode
276
+
277
+ Runs after each tool use during Ralph execution.
278
+ Monitors context percentage and triggers checkpoints.
279
+ """
280
+
281
+ import json
282
+ import os
283
+ import sys
284
+ from pathlib import Path
285
+ from datetime import datetime, timezone
286
+
287
+ # Thresholds
288
+ THRESHOLD_L1 = 70
289
+ THRESHOLD_L2 = 85
290
+ THRESHOLD_L3 = 95
291
+
292
+ STATE_FILE = os.environ.get("RALPH_STATE_FILE", ".claude/ralph-state.json")
293
+
294
+
295
+ def get_context_percent():
296
+ """Get current context percentage from Claude Code."""
297
+ # Parse from environment or Claude Code API
298
+ # This would be provided by Claude Code's context tracking
299
+ return int(os.environ.get("CLAUDE_CONTEXT_PERCENT", "50"))
300
+
301
+
302
+ def is_ralph_active():
303
+ """Check if Ralph mode is active."""
304
+ return Path(STATE_FILE).exists()
305
+
306
+
307
+ def load_state():
308
+ """Load Ralph state."""
309
+ with open(STATE_FILE) as f:
310
+ return json.load(f)
311
+
312
+
313
+ def save_state(state):
314
+ """Save Ralph state."""
315
+ with open(STATE_FILE, "w") as f:
316
+ json.dump(state, f, indent=2)
317
+
318
+
319
+ def trigger_checkpoint(level, percent, state):
320
+ """Trigger a CCS checkpoint within Ralph iteration."""
321
+ timestamp = datetime.now(timezone.utc).isoformat()
322
+ handoff_file = f".claude/handoffs/{datetime.now().strftime('%Y-%m-%d-%H%M')}.md"
323
+
324
+ state["context_checkpoint"] = {
325
+ "active": True,
326
+ "level": level,
327
+ "percent_at_checkpoint": percent,
328
+ "timestamp": timestamp,
329
+ "handoff_file": handoff_file,
330
+ "resume_summary": "", # Filled by handoff generation
331
+ "files_in_progress": [],
332
+ "current_todo_item": state.get("todo_items", [""])[0] if state.get("todo_items") else "",
333
+ "progress_on_item": "unknown"
334
+ }
335
+
336
+ # Add to context history
337
+ if "context_history" not in state:
338
+ state["context_history"] = []
339
+
340
+ state["context_history"].append({
341
+ "iteration": state.get("iteration", 0),
342
+ "peak_percent": percent,
343
+ "checkpoint": True
344
+ })
345
+
346
+ save_state(state)
347
+
348
+ # Signal that checkpoint should be generated
349
+ # This triggers the /handoff command with CCS metadata
350
+ print(f"CCS_CHECKPOINT_TRIGGERED|{level}|{percent}|{handoff_file}")
351
+
352
+
353
+ def main():
354
+ if not is_ralph_active():
355
+ return # Not in Ralph mode
356
+
357
+ percent = get_context_percent()
358
+ state = load_state()
359
+
360
+ # Track peak context for history
361
+ current_peak = state.get("context_history", [{}])[-1].get("peak_percent", 0)
362
+ if percent > current_peak:
363
+ # Update peak but don't trigger checkpoint yet
364
+ pass
365
+
366
+ # Check thresholds
367
+ if percent >= THRESHOLD_L3:
368
+ trigger_checkpoint("L3", percent, state)
369
+ sys.exit(1) # Signal emergency stop
370
+
371
+ elif percent >= THRESHOLD_L2:
372
+ trigger_checkpoint("L2", percent, state)
373
+ # Don't exit yet - let current edit complete
374
+ # The checkpoint signal will be handled by Claude
375
+
376
+ elif percent >= THRESHOLD_L1:
377
+ # Just warn, don't checkpoint
378
+ print(f"CCS_WARNING|L1|{percent}|Context at {percent}%")
379
+
380
+
381
+ if __name__ == "__main__":
382
+ main()
383
+ ```
384
+
385
+ ### ralph_stop.sh Enhancement
386
+
387
+ Add CCS checkpoint handling:
388
+
389
+ ```bash
390
+ # =============================================================================
391
+ # NEW: Check for CCS Checkpoint
392
+ # =============================================================================
393
+
394
+ check_ccs_checkpoint() {
395
+ # Check if a context checkpoint was triggered this iteration
396
+ local checkpoint_active
397
+ checkpoint_active=$(jq -r '.context_checkpoint.active // false' "$STATE_FILE" 2>/dev/null)
398
+
399
+ if [[ "$checkpoint_active" == "true" ]]; then
400
+ return 0 # Checkpoint was triggered
401
+ fi
402
+
403
+ return 1
404
+ }
405
+
406
+ handle_ccs_checkpoint() {
407
+ log_event "INFO" "CCS checkpoint detected - preparing resume context"
408
+
409
+ # Read checkpoint data
410
+ local level percent handoff_file
411
+ level=$(jq -r '.context_checkpoint.level' "$STATE_FILE")
412
+ percent=$(jq -r '.context_checkpoint.percent_at_checkpoint' "$STATE_FILE")
413
+ handoff_file=$(jq -r '.context_checkpoint.handoff_file' "$STATE_FILE")
414
+
415
+ log_event "INFO" "Checkpoint: $level at $percent%, handoff: $handoff_file"
416
+
417
+ # Regenerate PROMPT.md with resume context
418
+ # This calls ralph_state.py to rebuild PROMPT.md with checkpoint data
419
+ python3 global/lib/ralph_state.py regenerate-prompt --checkpoint
420
+
421
+ # Reset checkpoint.active for next iteration
422
+ jq '.context_checkpoint.active = false' "$STATE_FILE" > tmp.$$ && mv tmp.$$ "$STATE_FILE"
423
+
424
+ # Continue loop
425
+ return 1 # Exit code 1 = restart loop
426
+ }
427
+
428
+ # In main() function, add before other checks:
429
+ main() {
430
+ # ... existing init code ...
431
+
432
+ # NEW: Check for CCS checkpoint
433
+ if check_ccs_checkpoint; then
434
+ handle_ccs_checkpoint
435
+ exit 1 # Restart loop with checkpoint context
436
+ fi
437
+
438
+ # ... rest of existing checks ...
439
+ }
440
+ ```
441
+
442
+ ---
443
+
444
+ ## Checkpoint Flow Diagram
445
+
446
+ ```
447
+ ┌─────────────────────────────────────────────────────────────────────┐
448
+ │ CCS CHECKPOINT WITHIN RALPH ITERATION │
449
+ ├─────────────────────────────────────────────────────────────────────┤
450
+ │ │
451
+ │ Context at 85% (L2 threshold) │
452
+ │ │ │
453
+ │ ▼ │
454
+ │ ralph_context_monitor.py fires │
455
+ │ │ │
456
+ │ ├── 1. Set context_checkpoint.active = true in state │
457
+ │ ├── 2. Record level, percent, timestamp │
458
+ │ ├── 3. Output: CCS_CHECKPOINT_TRIGGERED|L2|85|handoff.md │
459
+ │ │ │
460
+ │ ▼ │
461
+ │ Claude Code receives signal │
462
+ │ │ │
463
+ │ ├── 4. Complete current edit (max 60s) │
464
+ │ ├── 5. Run /handoff with CCS metadata │
465
+ │ │ └── Generates .claude/handoffs/2026-01-07-1145.md │
466
+ │ ├── 6. Commit WIP: "ralph: checkpoint iter 5 (L2 at 85%)" │
467
+ │ ├── 7. Update Linear issue with checkpoint note │
468
+ │ └── 8. End session │
469
+ │ │
470
+ │ ▼ │
471
+ │ ralph_stop.sh fires │
472
+ │ │ │
473
+ │ ├── 9. Detect context_checkpoint.active = true │
474
+ │ ├── 10. Call handle_ccs_checkpoint() │
475
+ │ │ ├── Regenerate PROMPT.md with resume context │
476
+ │ │ └── Reset checkpoint.active = false │
477
+ │ └── 11. exit 1 (restart loop) │
478
+ │ │
479
+ │ ▼ │
480
+ │ ralph-loop.sh continues │
481
+ │ │ │
482
+ │ └── 12. Start iteration 6 with fresh context + resume instructions │
483
+ │ │
484
+ └─────────────────────────────────────────────────────────────────────┘
485
+ ```
486
+
487
+ ---
488
+
489
+ ## Context Efficiency Strategies for Ralph
490
+
491
+ ### Strategy 1: Aggressive Subagent Delegation
492
+
493
+ Ralph already recommends subagents for research. Enhance this:
494
+
495
+ | Task Type | Subagent | Context Savings |
496
+ |-----------|----------|-----------------|
497
+ | File exploration | Explore agent | ~20% |
498
+ | Code review | Code-reviewer agent | ~15% |
499
+ | Test analysis | spec-tester agent | ~15% |
500
+ | Documentation | docs-architect agent | ~10% |
501
+
502
+ ### Strategy 2: Incremental Context Loading
503
+
504
+ Instead of loading all context at start, load incrementally:
505
+
506
+ ```markdown
507
+ ## PROMPT.md - Iteration 5
508
+
509
+ ### Minimal Context (Always Load)
510
+ - fix_plan.md (TODO list)
511
+ - progress.txt (last 2 iterations only)
512
+ - checkpoint resume data (if any)
513
+
514
+ ### Load On Demand
515
+ - Full file contents: Only when editing
516
+ - Previous handoffs: Only if resuming from checkpoint
517
+ - Spec files: Only if referenced in current TODO item
518
+ ```
519
+
520
+ ### Strategy 3: Context Budget Per TODO Item
521
+
522
+ Estimate context per item:
523
+
524
+ | Item Complexity | Estimated Context | Max Per Iteration |
525
+ |-----------------|-------------------|-------------------|
526
+ | Trivial (<50 LOC) | 5-10% | 8-10 items |
527
+ | Simple (50-150 LOC) | 10-20% | 4-5 items |
528
+ | Medium (150-300 LOC) | 20-40% | 2-3 items |
529
+ | Complex (>300 LOC) | 40-60% | 1 item |
530
+ | Very Complex | 60%+ | MUST shard |
531
+
532
+ If estimated context for item exceeds remaining context, checkpoint BEFORE starting item.
533
+
534
+ ---
535
+
536
+ ## New Commands
537
+
538
+ ### /ralph checkpoint
539
+
540
+ Manual checkpoint within Ralph:
541
+
542
+ ```
543
+ /ralph checkpoint
544
+
545
+ ## Ralph Checkpoint Created
546
+
547
+ **Iteration**: 5
548
+ **Context**: 72%
549
+ **Handoff**: .claude/handoffs/2026-01-07-1200.md
550
+
551
+ Next iteration will resume from this point.
552
+ Continue with current iteration? (will restart on next natural stop)
553
+ ```
554
+
555
+ ### /ralph context
556
+
557
+ Show context status within Ralph:
558
+
559
+ ```
560
+ /ralph context
561
+
562
+ ## Ralph Context Status
563
+
564
+ | Metric | Value |
565
+ |--------|-------|
566
+ | Current Context | 67% |
567
+ | Level | L0 (Normal) |
568
+ | Estimated Remaining | ~66k tokens |
569
+ | Current Item | "Implement OAuth callback" |
570
+ | Item Complexity | Medium (est. 25% context) |
571
+
572
+ **Recommendation**: Safe to continue.
573
+ Checkpoint will trigger automatically at 85%.
574
+ ```
575
+
576
+ ---
577
+
578
+ ## Success Criteria
579
+
580
+ | Metric | Target |
581
+ |--------|--------|
582
+ | Context loss per Ralph run | <5% (vs 100% without CCS) |
583
+ | Successful checkpoint resumes | >95% |
584
+ | Iterations per context window | Increase by 30% |
585
+ | Task continuity across checkpoints | 100% (no lost work) |
586
+ | Overnight run success rate | >90% (vs ~60% without CCS) |
587
+
588
+ ---
589
+
590
+ ## Implementation Priority
591
+
592
+ ### Phase 1: Core Integration (P0)
593
+ 1. Add `context_checkpoint` fields to ralph-state.json
594
+ 2. Create `ralph_context_monitor.py` PostToolUse hook
595
+ 3. Enhance `ralph_stop.sh` with CCS checkpoint handling
596
+ 4. Modify PROMPT.md generation to include resume context
597
+
598
+ ### Phase 2: Automatic Checkpointing (P0)
599
+ 1. Implement L2 automatic checkpoint trigger
600
+ 2. Generate CCS-enhanced handoff documents
601
+ 3. Test checkpoint → resume cycle
602
+
603
+ ### Phase 3: Context Efficiency (P1)
604
+ 1. Implement subagent delegation recommendations
605
+ 2. Add context budget estimation per TODO item
606
+ 3. Add `/ralph context` status command
607
+
608
+ ### Phase 4: Intelligence (P2)
609
+ 1. Learn context patterns from history
610
+ 2. Predict optimal checkpoint timing
611
+ 3. Recommend task reordering for efficiency
612
+
613
+ ---
614
+
615
+ ## Open Questions
616
+
617
+ 1. **Context percentage API**: How does PostToolUse hook get context percentage from Claude Code?
618
+ - Option A: Environment variable set by Claude Code
619
+ - Option B: Parse from statusline output
620
+ - Option C: Query Claude Code API
621
+
622
+ 2. **Checkpoint granularity**: Should we checkpoint mid-file or only at file boundaries?
623
+ - Recommendation: File boundaries when possible, mid-file only at L3
624
+
625
+ 3. **Linear integration**: Should Ralph iterations create separate Linear comments or append to existing?
626
+ - Recommendation: Append with iteration number prefix
627
+
628
+ 4. **Handoff document size**: How much detail in checkpoint handoff?
629
+ - Recommendation: Structured summary + references, not full context dump
630
+
631
+ ---
632
+
633
+ *This design document prepared 2026-01-07 for CCS-Ralph integration planning.*