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,781 @@
1
+ ---
2
+ spec_id: SPEC-ANV-CCS
3
+ title: Context Checkpoint System - Intelligent Context Preservation
4
+ status: draft
5
+ created: 2026-01-07
6
+ updated: 2026-01-07
7
+ linear_issue: TBD
8
+ ---
9
+
10
+ # Context Checkpoint System (CCS)
11
+
12
+ ## Overview
13
+
14
+ Replace Claude Code's auto-compact with an intelligent, proactive context preservation system. CCS monitors context usage, estimates task scope, triggers structured handoffs at optimal breakpoints, and integrates with Linear for task-oriented session continuity.
15
+
16
+ ## Problem Statement
17
+
18
+ Claude Code's auto-compact has significant limitations:
19
+
20
+ 1. **Reactive, not proactive** - Triggers at ~95% usage, leaving no room for graceful handoff
21
+ 2. **Lossy summarization** - Forgets CLAUDE.md instructions, tool configurations, learned patterns
22
+ 3. **No task awareness** - Compacts mid-task without regard for logical breakpoints
23
+ 4. **No scope estimation** - Starts tasks that will inevitably exceed context
24
+ 5. **Poor continuation** - Next session lacks structured context to resume effectively
25
+
26
+ **Evidence from research:**
27
+ - Multiple GitHub issues (#6689, #12053) request disabling auto-compact
28
+ - Users report Claude "forgets" project rules after compaction
29
+ - Disabling recovers ~22.5% context but sessions end abruptly
30
+ - Manual compaction at strategic points is the recommended workaround
31
+
32
+ ## Goals
33
+
34
+ 1. **Proactive Monitoring** - Alert before hitting limits, not after
35
+ 2. **Task-Aware Checkpoints** - Align context boundaries with logical work boundaries
36
+ 3. **Zero-Loss Handoff** - Preserve all critical context through structured documents
37
+ 4. **Scope Estimation** - Predict and prevent context overruns
38
+ 5. **Linear Integration** - Tie checkpoints to issue lifecycle for seamless continuation
39
+
40
+ ---
41
+
42
+ ## Design Principles
43
+
44
+ ### Principle 1: Proactive Over Reactive
45
+
46
+ Trigger checkpoints based on trajectory, not just current position:
47
+
48
+ ```
49
+ Context at 60% + large task ahead → Initiate handoff at 70%
50
+ Context at 80% + small task remaining → Complete then handoff
51
+ Context at 90% + any task → Emergency handoff immediately
52
+ ```
53
+
54
+ ### Principle 2: Task-Oriented Checkpoints
55
+
56
+ Checkpoints should align with natural work boundaries:
57
+
58
+ | Good Checkpoint | Bad Checkpoint |
59
+ |-----------------|----------------|
60
+ | After completing Linear issue | Mid-function implementation |
61
+ | After passing tests | During debugging |
62
+ | After PR creation | During code review response |
63
+ | Between sharded sub-tasks | In the middle of a shard |
64
+
65
+ ### Principle 3: Hierarchical Context Preservation
66
+
67
+ Different context types need different preservation strategies:
68
+
69
+ | Layer | Content | Preservation |
70
+ |-------|---------|--------------|
71
+ | **Permanent** | CLAUDE.md, constitution | Always loaded (never summarized) |
72
+ | **Session** | Current task, decisions, blockers | Captured in handoff document |
73
+ | **Working** | Current file contents, recent edits | Git diff, uncommitted changes |
74
+ | **Ephemeral** | Tool outputs, search results | Summarize key findings only |
75
+
76
+ ### Principle 4: Sharding for Prevention
77
+
78
+ Break large tasks before they exceed context, not after:
79
+
80
+ | Estimated Size | Action |
81
+ |----------------|--------|
82
+ | <30% context | Execute normally |
83
+ | 30-60% context | Consider sharding |
84
+ | >60% context | Must shard before starting |
85
+
86
+ ### Principle 5: Structured Handoff Over Summarization
87
+
88
+ Replace lossy auto-summarization with structured handoff documents:
89
+
90
+ | Auto-Compact | CCS Handoff |
91
+ |--------------|-------------|
92
+ | Lossy summary | Structured markdown |
93
+ | Forgets instructions | References CLAUDE.md explicitly |
94
+ | No file tracking | Git status + modified files |
95
+ | No task context | Linear issue + progress state |
96
+ | No continuation path | Explicit next steps |
97
+
98
+ ---
99
+
100
+ ## System Architecture
101
+
102
+ ### Components
103
+
104
+ ```
105
+ ┌─────────────────────────────────────────────────────────────┐
106
+ │ Context Checkpoint System │
107
+ ├─────────────────────────────────────────────────────────────┤
108
+ │ │
109
+ │ ┌───────────────┐ ┌───────────────┐ ┌───────────────┐ │
110
+ │ │ MONITOR │ │ ESTIMATOR │ │ HANDLER │ │
111
+ │ │ │ │ │ │ │ │
112
+ │ │ - Poll context│ │ - Task sizing │ │ - L1: Warning │ │
113
+ │ │ - Track trend │ │ - Scope check │ │ - L2: Handoff │ │
114
+ │ │ - Emit alerts │ │ - Shard hints │ │ - L3: Emergency│ │
115
+ │ └───────┬───────┘ └───────┬───────┘ └───────┬───────┘ │
116
+ │ │ │ │ │
117
+ │ └──────────────────┼──────────────────┘ │
118
+ │ │ │
119
+ │ ┌────────▼────────┐ │
120
+ │ │ ORCHESTRATOR │ │
121
+ │ │ │ │
122
+ │ │ - Decision logic│ │
123
+ │ │ - Linear sync │ │
124
+ │ │ - Handoff flow │ │
125
+ │ └────────┬────────┘ │
126
+ │ │ │
127
+ │ ┌──────────────────┼──────────────────┐ │
128
+ │ │ │ │ │
129
+ │ ┌───────▼───────┐ ┌───────▼───────┐ ┌───────▼───────┐ │
130
+ │ │ HANDOFF │ │ SHARD │ │ LINEAR │ │
131
+ │ │ GENERATOR │ │ MANAGER │ │ INTEGRATION │ │
132
+ │ │ │ │ │ │ │ │
133
+ │ │ Structured doc│ │ Break tasks │ │ Issue state │ │
134
+ │ └───────────────┘ └───────────────┘ └───────────────┘ │
135
+ │ │
136
+ └─────────────────────────────────────────────────────────────┘
137
+ ```
138
+
139
+ ### Context Threshold Levels
140
+
141
+ | Level | Threshold | State | Action |
142
+ |-------|-----------|-------|--------|
143
+ | L0 | 0-69% | Normal | Continue work, no alerts |
144
+ | L1 | 70-84% | Warning | Alert + prepare for handoff |
145
+ | L2 | 85-94% | Critical | Initiate handoff sequence |
146
+ | L3 | 95%+ | Emergency | Force immediate handoff |
147
+
148
+ ### Hook Integration Points
149
+
150
+ | Hook | Purpose |
151
+ |------|---------|
152
+ | `PostToolUse` | Monitor context after each tool call |
153
+ | `PreCompact` | Intercept auto-compact, run CCS handoff instead |
154
+ | `SessionStart` | Load handoff context if resuming |
155
+ | `Stop` | Prompt handoff if context > 50% |
156
+
157
+ ---
158
+
159
+ ## Requirements
160
+
161
+ ### Context Monitor
162
+
163
+ | ID | Requirement | Priority |
164
+ |----|-------------|----------|
165
+ | MON-001 | Poll context percentage from statusline data | P0 |
166
+ | MON-002 | Track context growth rate (tokens/minute) | P1 |
167
+ | MON-003 | Predict time-to-threshold based on trend | P1 |
168
+ | MON-004 | Emit alerts at L1, L2, L3 thresholds | P0 |
169
+ | MON-005 | Cache polling to avoid overhead (<100ms) | P2 |
170
+
171
+ ### Task Estimator
172
+
173
+ | ID | Requirement | Priority |
174
+ |----|-------------|----------|
175
+ | EST-001 | Estimate task size from spec/plan | P1 |
176
+ | EST-002 | Warn if estimated size > remaining context | P0 |
177
+ | EST-003 | Suggest sharding for oversized tasks | P1 |
178
+ | EST-004 | Learn from actual vs. estimated for future accuracy | P2 |
179
+
180
+ ### Checkpoint Handler
181
+
182
+ | ID | Requirement | Priority |
183
+ |----|-------------|----------|
184
+ | CHK-001 | L1: Display warning in statusline | P0 |
185
+ | CHK-002 | L1: Recommend completing current task | P1 |
186
+ | CHK-003 | L2: Initiate handoff generation | P0 |
187
+ | CHK-004 | L2: Commit WIP if uncommitted changes | P0 |
188
+ | CHK-005 | L3: Force handoff with max preservation | P0 |
189
+ | CHK-006 | L3: Update Linear issue with checkpoint note | P1 |
190
+
191
+ ### Handoff Generator
192
+
193
+ | ID | Requirement | Priority |
194
+ |----|-------------|----------|
195
+ | HND-001 | Generate structured handoff document | P0 |
196
+ | HND-002 | Include git status and modified files | P0 |
197
+ | HND-003 | Include Linear issue state and progress | P0 |
198
+ | HND-004 | Include explicit next steps | P0 |
199
+ | HND-005 | Reference CLAUDE.md sections to reload | P1 |
200
+ | HND-006 | Summarize decisions made this session | P1 |
201
+
202
+ ### Shard Manager
203
+
204
+ | ID | Requirement | Priority |
205
+ |----|-------------|----------|
206
+ | SHD-001 | Integrate with existing `/shard` command | P1 |
207
+ | SHD-002 | Auto-suggest sharding when task > 60% context | P1 |
208
+ | SHD-003 | Create sub-issues in Linear for shards | P1 |
209
+ | SHD-004 | Track shard progress in parent issue | P2 |
210
+
211
+ ### Linear Integration
212
+
213
+ | ID | Requirement | Priority |
214
+ |----|-------------|----------|
215
+ | LIN-001 | Update issue status on checkpoint | P0 |
216
+ | LIN-002 | Add checkpoint comment with handoff reference | P1 |
217
+ | LIN-003 | Link handoff document in issue | P1 |
218
+ | LIN-004 | Set "context-checkpointed" label | P2 |
219
+
220
+ ---
221
+
222
+ ## User Interface
223
+
224
+ ### Statusline Integration
225
+
226
+ Enhance existing statusline with checkpoint awareness:
227
+
228
+ ```
229
+ Before (current):
230
+ [CTX: ████████░░ 82%] [ISSUE: ANV-123] [PHASE: IMPL]
231
+
232
+ After (with CCS):
233
+ [CTX: ████████░░ 82% ⚠️ L2] [ISSUE: ANV-123] [PHASE: IMPL] [CKPT: Ready]
234
+ ```
235
+
236
+ ### Alert Messages
237
+
238
+ **L1 Warning (70%):**
239
+ ```
240
+ ⚠️ Context at 70% - Consider completing current task before starting new work.
241
+ Run `/handoff` to checkpoint, or continue if task is nearly complete.
242
+ ```
243
+
244
+ **L2 Critical (85%):**
245
+ ```
246
+ 🔶 Context at 85% - Initiating checkpoint sequence.
247
+ Completing current edit, then generating handoff document.
248
+ Linear issue ANV-123 will be updated with progress.
249
+ ```
250
+
251
+ **L3 Emergency (95%):**
252
+ ```
253
+ 🔴 Context at 95% - Emergency checkpoint!
254
+ Committing WIP changes and generating handoff NOW.
255
+ Next session should run `/orient` to continue.
256
+ ```
257
+
258
+ ### Commands
259
+
260
+ | Command | Purpose |
261
+ |---------|---------|
262
+ | `/checkpoint` | Manual checkpoint at current state |
263
+ | `/checkpoint --estimate [task]` | Estimate if task fits in remaining context |
264
+ | `/checkpoint --status` | Show current context state and recommendation |
265
+
266
+ ---
267
+
268
+ ## Data Model
269
+
270
+ ### Checkpoint State
271
+
272
+ Stored in `.claude/checkpoint-state.json`:
273
+
274
+ ```json
275
+ {
276
+ "version": "1.0.0",
277
+ "sessionId": "abc123",
278
+ "startedAt": "2026-01-07T11:00:00Z",
279
+ "currentLevel": "L1",
280
+ "contextPercent": 72,
281
+ "contextTrend": "increasing",
282
+ "tokensPerMinute": 850,
283
+ "estimatedTimeToL2": "15m",
284
+ "linearIssue": "ANV-123",
285
+ "lastCheckpoint": {
286
+ "timestamp": "2026-01-07T10:45:00Z",
287
+ "handoffFile": ".claude/handoffs/2026-01-07-1045.md",
288
+ "reason": "manual"
289
+ },
290
+ "uncommittedChanges": true,
291
+ "modifiedFiles": [
292
+ "src/components/Feature.tsx",
293
+ "src/services/api.ts"
294
+ ]
295
+ }
296
+ ```
297
+
298
+ ### Handoff Document Enhancement
299
+
300
+ Extend existing `/handoff` format with CCS metadata:
301
+
302
+ ```markdown
303
+ ---
304
+ session_date: 2026-01-07
305
+ session_time: 11:45
306
+ branch: feature/ANV-123-new-feature
307
+ linear_issues: ANV-123
308
+ checkpoint_trigger: L2 (context at 86%)
309
+ context_at_checkpoint: 86%
310
+ ---
311
+
312
+ # Session Handoff: ANV-123 Feature Implementation
313
+
314
+ ## Checkpoint Reason
315
+ Context reached 86% (L2 threshold) during implementation of UserProfile component.
316
+
317
+ ## Context to Reload
318
+ - **CLAUDE.md**: Sections 2.3 (Error Handling), 4.1 (File Conventions)
319
+ - **Active Spec**: .claude/specs/current/SPEC-ANV-123.md
320
+
321
+ ## Remaining Context Budget
322
+ Estimated 14% (~28,000 tokens) remaining in original session.
323
+
324
+ ... (rest of standard handoff format)
325
+ ```
326
+
327
+ ---
328
+
329
+ ## Acceptance Criteria
330
+
331
+ ### Scenario: L1 Warning Alert
332
+
333
+ ```gherkin
334
+ GIVEN context usage is at 68%
335
+ AND agent is working on task ANV-123
336
+ WHEN context crosses 70%
337
+ THEN statusline displays L1 warning indicator
338
+ AND agent receives alert message recommending task completion
339
+ AND no automatic action is taken
340
+ ```
341
+
342
+ ### Scenario: L2 Handoff Initiation
343
+
344
+ ```gherkin
345
+ GIVEN context usage is at 84%
346
+ AND agent has uncommitted changes
347
+ WHEN context crosses 85%
348
+ THEN agent completes current edit (max 1 minute)
349
+ AND WIP commit is created with message "[WIP] ANV-123 - checkpoint"
350
+ AND handoff document is generated
351
+ AND Linear issue is updated with checkpoint comment
352
+ AND agent suggests resumption command for next session
353
+ ```
354
+
355
+ ### Scenario: Task Estimation Warning
356
+
357
+ ```gherkin
358
+ GIVEN context usage is at 55%
359
+ AND user requests new task from spec
360
+ WHEN estimated task size is 50% of total context
361
+ THEN agent warns that task may exceed remaining context
362
+ AND suggests sharding or checkpointing first
363
+ AND provides option to proceed anyway
364
+ ```
365
+
366
+ ### Scenario: Shard Suggestion
367
+
368
+ ```gherkin
369
+ GIVEN spec file has estimated size of 70% context
370
+ WHEN agent prepares to implement spec
371
+ THEN agent automatically suggests sharding
372
+ AND provides breakdown into 3 smaller shards
373
+ AND offers to create Linear sub-issues for each shard
374
+ ```
375
+
376
+ ### Scenario: Session Resume
377
+
378
+ ```gherkin
379
+ GIVEN previous session ended with L2 checkpoint
380
+ AND handoff document exists at .claude/handoffs/2026-01-07-1045.md
381
+ WHEN new session starts with `/orient`
382
+ THEN handoff document is loaded
383
+ AND key context is summarized for agent
384
+ AND Linear issue state is displayed
385
+ AND explicit next steps are presented
386
+ ```
387
+
388
+ ---
389
+
390
+ ## Implementation Phases
391
+
392
+ ### Phase 1: Monitoring Foundation (P0)
393
+
394
+ 1. Enhance statusline hook to emit context alerts
395
+ 2. Add L1/L2/L3 visual indicators
396
+ 3. Create checkpoint-state.json tracking
397
+ 4. Integrate with existing `/handoff` command
398
+
399
+ ### Phase 2: Proactive Handoff (P0)
400
+
401
+ 1. Implement L2 automatic handoff trigger
402
+ 2. Add WIP commit before handoff
403
+ 3. Enhance handoff document with CCS metadata
404
+ 4. Add Linear issue checkpoint comments
405
+
406
+ ### Phase 3: Task Estimation (P1)
407
+
408
+ 1. Build task size estimator from spec analysis
409
+ 2. Add `/checkpoint --estimate` command
410
+ 3. Warn when task may exceed remaining context
411
+ 4. Learn from actual vs. estimated accuracy
412
+
413
+ ### Phase 4: Shard Integration (P1)
414
+
415
+ 1. Trigger automatic shard suggestions
416
+ 2. Create Linear sub-issues for shards
417
+ 3. Track shard progress in parent issue
418
+ 4. Enable seamless shard-to-shard handoffs
419
+
420
+ ### Phase 5: Intelligence Layer (P2)
421
+
422
+ 1. Predict optimal checkpoint timing
423
+ 2. Learn user preferences for checkpoint timing
424
+ 3. Suggest task reordering to optimize context usage
425
+ 4. Provide session planning recommendations
426
+
427
+ ---
428
+
429
+ ## Out of Scope
430
+
431
+ - **Disabling auto-compact** - CCS works alongside, not instead of auto-compact initially
432
+ - **Cross-session memory persistence** - Use claude-mem for that, CCS focuses on single session
433
+ - **Multi-agent coordination** - Each agent manages its own context independently
434
+ - **Custom compaction algorithm** - We use structured handoff, not modified summarization
435
+
436
+ ---
437
+
438
+ ## Risks and Mitigations
439
+
440
+ | Risk | Impact | Mitigation |
441
+ |------|--------|------------|
442
+ | Over-aggressive checkpoints interrupt flow | Medium | Allow user to defer L1/L2 for "just one more task" |
443
+ | Estimation inaccuracy wastes context | Medium | Learn from history, start conservative |
444
+ | Linear API failures during checkpoint | High | Queue updates, retry, log locally as fallback |
445
+ | Handoff documents grow too large | Low | Focus on structure over detail, link to specs |
446
+
447
+ ---
448
+
449
+ ## Success Metrics
450
+
451
+ | Metric | Target |
452
+ |--------|--------|
453
+ | Context lost to auto-compact | Reduce by 80% |
454
+ | Session continuity success rate | >90% successful resumes |
455
+ | Task completion per session | Increase by 20% |
456
+ | Mid-task compaction events | Reduce to <5% of sessions |
457
+ | User-initiated checkpoints | >50% (showing adoption) |
458
+
459
+ ---
460
+
461
+ ## Ralph Wiggum Integration
462
+
463
+ ### Critical Requirement
464
+
465
+ Ralph Wiggum is a continuous-loop autonomous execution system that WILL hit context limits frequently during overnight runs. CCS must be deeply integrated into Ralph to enable graceful handoffs within and across iterations.
466
+
467
+ ### Two-Level Integration Model
468
+
469
+ CCS operates at two levels within Ralph:
470
+
471
+ | Level | Scope | Purpose |
472
+ |-------|-------|---------|
473
+ | **Micro** | Within single iteration | Monitor context during Claude Code session |
474
+ | **Macro** | Across iterations | Pass context state from iteration N to N+1 |
475
+
476
+ ### Architecture
477
+
478
+ ```
479
+ ┌─────────────────────────────────────────────────────────────────────┐
480
+ │ CCS-RALPH INTEGRATION │
481
+ ├─────────────────────────────────────────────────────────────────────┤
482
+ │ │
483
+ │ ralph-loop.sh (Macro Level) │
484
+ │ ┌────────┐ ┌────────┐ ┌────────┐ ┌────────┐ │
485
+ │ │ Iter 1 │───▶│ Iter 2 │───▶│ Iter 3 │───▶│ Iter N │ │
486
+ │ └────────┘ └────────┘ └────────┘ └────────┘ │
487
+ │ │ │ │ │ │
488
+ │ ▼ ▼ ▼ ▼ │
489
+ │ [Checkpoint] [Resume] [Resume] [Complete] │
490
+ │ │
491
+ │ Within Each Iteration (Micro Level): │
492
+ │ ┌─────────────────────────────────────────────────────────────┐ │
493
+ │ │ Claude Code Session │ │
494
+ │ │ Tool #1 → Tool #2 → ... → Tool #N │ │
495
+ │ │ ↓ ↓ ↓ │ │
496
+ │ │ [20%] [45%] [86%] → L2 Checkpoint │ │
497
+ │ └─────────────────────────────────────────────────────────────┘ │
498
+ │ │
499
+ └─────────────────────────────────────────────────────────────────────┘
500
+ ```
501
+
502
+ ### Ralph State Enhancement
503
+
504
+ Add to `ralph-state.json`:
505
+
506
+ ```json
507
+ {
508
+ "context_checkpoint": {
509
+ "active": true,
510
+ "level": "L2",
511
+ "percent_at_checkpoint": 87,
512
+ "timestamp": "2026-01-07T11:45:00Z",
513
+ "handoff_file": ".claude/handoffs/2026-01-07-1145.md",
514
+ "resume_summary": "Implementing OAuth callback handler",
515
+ "files_in_progress": [
516
+ {"path": "src/auth/oauth.ts", "lines": "45-120"}
517
+ ],
518
+ "current_todo_item": "Implement OAuth callback",
519
+ "progress_on_item": "70%"
520
+ },
521
+ "context_history": [
522
+ {"iteration": 1, "peak_percent": 45, "checkpoint": false},
523
+ {"iteration": 2, "peak_percent": 88, "checkpoint": true}
524
+ ]
525
+ }
526
+ ```
527
+
528
+ ### Ralph-Specific Requirements
529
+
530
+ | ID | Requirement | Priority |
531
+ |----|-------------|----------|
532
+ | RLP-001 | Add `context_checkpoint` fields to ralph-state.json | P0 |
533
+ | RLP-002 | Create `ralph_context_monitor.py` PostToolUse hook | P0 |
534
+ | RLP-003 | Enhance `ralph_stop.sh` with CCS checkpoint handling | P0 |
535
+ | RLP-004 | Regenerate PROMPT.md with resume context on checkpoint | P0 |
536
+ | RLP-005 | Track context history across iterations | P1 |
537
+ | RLP-006 | Estimate context per TODO item before starting | P1 |
538
+ | RLP-007 | Add `/ralph context` status command | P1 |
539
+ | RLP-008 | Add `/ralph checkpoint` manual checkpoint command | P1 |
540
+
541
+ ### Checkpoint Flow Within Ralph
542
+
543
+ ```
544
+ Iteration N at 85% context (L2 threshold)
545
+
546
+ ├── 1. ralph_context_monitor.py detects L2
547
+ │ └── Sets context_checkpoint.active = true
548
+
549
+ ├── 2. Claude completes current edit (max 60s)
550
+
551
+ ├── 3. CCS generates handoff document
552
+ │ └── .claude/handoffs/YYYY-MM-DD-HHMM.md
553
+
554
+ ├── 4. WIP commit created
555
+ │ └── "ralph: checkpoint iter N (L2 at 85%)"
556
+
557
+ ├── 5. Session ends → ralph_stop.sh fires
558
+
559
+ ├── 6. ralph_stop.sh detects checkpoint
560
+ │ ├── Regenerates PROMPT.md with resume context
561
+ │ └── exit 1 (restart loop)
562
+
563
+ └── 7. Iteration N+1 starts with fresh context
564
+ └── PROMPT.md contains resume instructions
565
+ ```
566
+
567
+ ### Enhanced PROMPT.md for Resume
568
+
569
+ ```markdown
570
+ # Task: [Original Task]
571
+
572
+ ## CONTEXT CHECKPOINT RESUME
573
+
574
+ > This iteration is resuming from a context checkpoint.
575
+ > Previous session hit 87% context (L2 threshold).
576
+
577
+ ### Resume From
578
+ - **File**: src/auth/oauth.ts (lines 45-120)
579
+ - **Item**: Implement OAuth callback (70% complete)
580
+ - **Handoff**: .claude/handoffs/2026-01-07-1145.md
581
+
582
+ ### Instructions
583
+ 1. DO NOT restart the task from scratch
584
+ 2. Read the handoff document first
585
+ 3. Continue from where previous session left off
586
+ ```
587
+
588
+ ### Ralph-Specific Success Metrics
589
+
590
+ | Metric | Target |
591
+ |--------|--------|
592
+ | Context loss per Ralph run | <5% (vs 100% without CCS) |
593
+ | Successful checkpoint resumes | >95% |
594
+ | Iterations per context window | Increase by 30% |
595
+ | Overnight run success rate | >90% (vs ~60% without CCS) |
596
+
597
+ ### Ralph Implementation Phases
598
+
599
+ **Phase R1: Core Integration (P0)**
600
+ 1. Add `context_checkpoint` to ralph-state.json schema
601
+ 2. Create `ralph_context_monitor.py` PostToolUse hook
602
+ 3. Modify `ralph_stop.sh` to handle CCS checkpoints
603
+ 4. Update PROMPT.md generation for resume context
604
+
605
+ **Phase R2: Automatic Checkpointing (P0)**
606
+ 1. Implement L2 automatic checkpoint trigger in Ralph
607
+ 2. Generate CCS-enhanced handoff documents
608
+ 3. Test full checkpoint → resume cycle
609
+
610
+ **Phase R3: Context Efficiency (P1)**
611
+ 1. Add context budget estimation per TODO item
612
+ 2. Recommend subagent delegation for heavy tasks
613
+ 3. Add `/ralph context` and `/ralph checkpoint` commands
614
+
615
+ ---
616
+
617
+ ## Claude-Mem Integration
618
+
619
+ ### Overview
620
+
621
+ Claude-mem is a persistent cross-session memory system already installed in the Anvil framework. Rather than building a separate persistence layer, CCS can leverage claude-mem for session-to-session continuity.
622
+
623
+ ### How Claude-Mem Works
624
+
625
+ Claude-mem operates through lifecycle hooks that run automatically:
626
+
627
+ | Hook | When | Purpose |
628
+ |------|------|---------|
629
+ | `SessionStart` | On startup/compact | Loads recent context into session |
630
+ | `UserPromptSubmit` | Each user message | Captures prompts |
631
+ | `PostToolUse` | After every tool call | Creates observations automatically |
632
+ | `Stop` | Session end | Generates session summary |
633
+ | `SessionEnd` | Cleanup | Handles cleanup tasks |
634
+
635
+ **Key Insight**: Observations are created automatically. When CCS writes a handoff document or updates ralph-state.json, these become searchable observations without any additional code.
636
+
637
+ ### MCP Tools Available
638
+
639
+ Claude-mem exposes an MCP server with these tools:
640
+
641
+ | Tool | Purpose |
642
+ |------|---------|
643
+ | `search` | Find observations by query, type, date, concepts, files |
644
+ | `timeline` | Get context around a specific observation ID |
645
+ | `get_recent_context` | Fast retrieval of recent sessions and observations |
646
+ | `get_observation` | Full details for specific observation by ID |
647
+ | `get_batch_observations` | Batch fetch multiple observations |
648
+
649
+ ### Integration Architecture
650
+
651
+ ```
652
+ ┌──────────────────────────────────────────────────────────────────┐
653
+ │ CCS + CLAUDE-MEM INTEGRATION │
654
+ ├──────────────────────────────────────────────────────────────────┤
655
+ │ │
656
+ │ Ralph Iteration N (Context Checkpoint Event): │
657
+ │ ┌─────────────────────────────────────────────────────────────┐ │
658
+ │ │ 1. CCS detects L2 threshold (85%) │ │
659
+ │ │ 2. Creates handoff doc → Captured as observation #3500 │ │
660
+ │ │ 3. Updates ralph-state.json → Captured as observation #3501 │ │
661
+ │ │ 4. Commits WIP → Captured as observation #3502 │ │
662
+ │ │ 5. Session ends → Stop hook creates session summary │ │
663
+ │ └─────────────────────────────────────────────────────────────┘ │
664
+ │ │ │
665
+ │ ▼ │
666
+ │ Ralph Iteration N+1 (Session Start): │
667
+ │ ┌─────────────────────────────────────────────────────────────┐ │
668
+ │ │ 1. SessionStart hook fires │ │
669
+ │ │ 2. context-hook.js loads recent context (auto) │ │
670
+ │ │ 3. PROMPT.md uses get_recent_context for checkpoint detail │ │
671
+ │ │ 4. Claude resumes with full checkpoint context │ │
672
+ │ └─────────────────────────────────────────────────────────────┘ │
673
+ │ │
674
+ └──────────────────────────────────────────────────────────────────┘
675
+ ```
676
+
677
+ ### What Claude-Mem Already Provides
678
+
679
+ | CCS Need | Claude-Mem Solution |
680
+ |----------|---------------------|
681
+ | Persist checkpoint state | Observations are auto-captured on file writes |
682
+ | Resume context on restart | SessionStart hook provides recent context |
683
+ | Search past checkpoints | `search` with `type=change` or `query=checkpoint` |
684
+ | Timeline of events | `timeline` around checkpoint observation ID |
685
+ | Cross-iteration memory | Automatic via persistent database |
686
+
687
+ ### What CCS Needs to Add
688
+
689
+ | Addition | Purpose |
690
+ |----------|---------|
691
+ | Structured checkpoint metadata | Ensure checkpoint observations have predictable format |
692
+ | PROMPT.md claude-mem queries | Fetch checkpoint context at iteration start |
693
+ | Checkpoint observation type | Tag observations as `checkpoint` type for filtering |
694
+
695
+ ### Enhanced PROMPT.md for Claude-Mem Resume
696
+
697
+ ```markdown
698
+ # Task: [Original Task]
699
+
700
+ ## CONTEXT CHECKPOINT RESUME
701
+
702
+ > This iteration is resuming from a context checkpoint.
703
+ > Previous session hit 87% context (L2 threshold).
704
+
705
+ ### Memory Query (Run First)
706
+
707
+ Use claude-mem to retrieve checkpoint context:
708
+
709
+ ```
710
+ 1. Get recent context: mcp__claude-mem-search__get_recent_context(limit=5)
711
+ 2. Search checkpoints: mcp__claude-mem-search__search(query="checkpoint L2", type="observations")
712
+ ```
713
+
714
+ ### Resume Instructions
715
+ 1. Read the memory search results first
716
+ 2. Read the handoff document referenced in results
717
+ 3. Continue from where previous session left off
718
+ ```
719
+
720
+ ### Progressive Disclosure for Token Efficiency
721
+
722
+ Claude-mem implements progressive disclosure to minimize token usage:
723
+
724
+ 1. **Index First**: Search returns minimal metadata (~50-100 tokens/result)
725
+ 2. **Drill Down**: Full details only for relevant items (~500-1000 tokens/result)
726
+ 3. **Batch Fetch**: Get multiple observations in one request
727
+
728
+ For CCS-Ralph, this means:
729
+ - Iteration starts with ~500-1000 tokens of checkpoint context
730
+ - Full handoff details loaded only when needed
731
+ - 75% token savings vs loading everything upfront
732
+
733
+ ### Claude-Mem Integration Requirements
734
+
735
+ | ID | Requirement | Priority |
736
+ |----|-------------|----------|
737
+ | MEM-001 | Tag checkpoint observations with `checkpoint` concept | P0 |
738
+ | MEM-002 | Include checkpoint level (L1/L2/L3) in observation title | P0 |
739
+ | MEM-003 | Add PROMPT.md template with claude-mem queries | P0 |
740
+ | MEM-004 | Query `get_recent_context` at iteration start | P1 |
741
+ | MEM-005 | Use `timeline` to understand pre-checkpoint context | P2 |
742
+ | MEM-006 | Search past checkpoints for pattern analysis | P2 |
743
+
744
+ ### Implementation Notes
745
+
746
+ 1. **No Write API Needed**: Claude-mem automatically captures observations via PostToolUse hook. CCS just needs to perform tool calls (write files, git commit) and they become searchable.
747
+
748
+ 2. **SessionStart Already Works**: The existing context-hook.js runs on `startup|clear|compact`, meaning every new Ralph iteration already gets recent context.
749
+
750
+ 3. **Predictable Observation Titles**: CCS should use consistent titles like "Context Checkpoint L2 at 87% - ANV-123" so searches are reliable.
751
+
752
+ 4. **Handoff + Memory = Complete Picture**: The handoff document provides structured detail; claude-mem provides the search/retrieval layer.
753
+
754
+ ---
755
+
756
+ ## Open Questions
757
+
758
+ 1. **Context API**: How does PostToolUse hook get context percentage from Claude Code?
759
+ - Option A: Environment variable `CLAUDE_CONTEXT_PERCENT`
760
+ - Option B: Parse from statusline output
761
+ - Option C: Query internal Claude Code API
762
+
763
+ 2. **Checkpoint granularity**: Checkpoint mid-file or only at file boundaries?
764
+ - Recommendation: File boundaries when possible, mid-file only at L3
765
+
766
+ 3. **Linear comments**: Append to existing or create separate comments per checkpoint?
767
+ - Recommendation: Append with iteration prefix
768
+
769
+ 4. **Claude-mem observation type**: Use existing types or create new `checkpoint` type?
770
+ - Recommendation: Use `change` type with `checkpoint` in concepts for filtering
771
+
772
+ ---
773
+
774
+ ## References
775
+
776
+ - [GitHub Issue #6689: Request for --no-auto-compact flag](https://github.com/anthropics/claude-code/issues/6689)
777
+ - [GitHub Issue #12053: Disable auto-compact buffer](https://github.com/anthropics/claude-code/issues/12053)
778
+ - [ClaudeLog: Strategic Manual Compacting](https://claudelog.com)
779
+ - Existing Anvil: `global/hooks/statusline.sh`, `global/commands/handoff.md`, `global/commands/shard.md`
780
+ - Ralph System: `global/commands/ralph.md`, `global/lib/ralph_state.py`, `global/tools/ralph-loop.sh`, `global/hooks/ralph_stop.sh`
781
+ - CCS-Ralph Design: `.claude/explorations/2026-01-07-ccs-ralph-integration-design.md`