aiwcli 0.12.6 → 0.12.7

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 (124) hide show
  1. package/bin/dev.cmd +3 -3
  2. package/bin/dev.js +16 -16
  3. package/bin/run.cmd +3 -3
  4. package/bin/run.js +21 -21
  5. package/dist/commands/branch.js +7 -2
  6. package/dist/lib/bmad-installer.js +37 -37
  7. package/dist/lib/terminal.d.ts +2 -0
  8. package/dist/lib/terminal.js +57 -7
  9. package/dist/templates/CLAUDE.md +205 -205
  10. package/dist/templates/_shared/.claude/commands/handoff-resume.md +12 -12
  11. package/dist/templates/_shared/.claude/commands/handoff.md +12 -12
  12. package/dist/templates/_shared/.claude/settings.json +65 -65
  13. package/dist/templates/_shared/.codex/workflows/handoff.md +226 -226
  14. package/dist/templates/_shared/.windsurf/workflows/handoff.md +226 -226
  15. package/dist/templates/_shared/handoff-system/CLAUDE.md +421 -421
  16. package/dist/templates/_shared/handoff-system/lib/document-generator.ts +215 -215
  17. package/dist/templates/_shared/handoff-system/lib/handoff-reader.ts +158 -158
  18. package/dist/templates/_shared/handoff-system/scripts/resume_handoff.ts +373 -373
  19. package/dist/templates/_shared/handoff-system/scripts/save_handoff.ts +469 -469
  20. package/dist/templates/_shared/handoff-system/workflows/handoff-resume.md +66 -66
  21. package/dist/templates/_shared/handoff-system/workflows/handoff.md +254 -254
  22. package/dist/templates/_shared/hooks-ts/_utils/git-state.ts +2 -2
  23. package/dist/templates/_shared/hooks-ts/archive_plan.ts +159 -159
  24. package/dist/templates/_shared/hooks-ts/context_monitor.ts +147 -147
  25. package/dist/templates/_shared/hooks-ts/file-suggestion.ts +128 -128
  26. package/dist/templates/_shared/hooks-ts/pre_compact.ts +49 -49
  27. package/dist/templates/_shared/hooks-ts/session_end.ts +196 -196
  28. package/dist/templates/_shared/hooks-ts/session_start.ts +163 -163
  29. package/dist/templates/_shared/hooks-ts/task_create_capture.ts +48 -48
  30. package/dist/templates/_shared/hooks-ts/task_update_capture.ts +74 -74
  31. package/dist/templates/_shared/hooks-ts/user_prompt_submit.ts +93 -93
  32. package/dist/templates/_shared/lib-ts/CLAUDE.md +367 -367
  33. package/dist/templates/_shared/lib-ts/base/atomic-write.ts +138 -138
  34. package/dist/templates/_shared/lib-ts/base/constants.ts +303 -303
  35. package/dist/templates/_shared/lib-ts/base/git-state.ts +58 -58
  36. package/dist/templates/_shared/lib-ts/base/hook-utils.ts +582 -582
  37. package/dist/templates/_shared/lib-ts/base/inference.ts +301 -301
  38. package/dist/templates/_shared/lib-ts/base/logger.ts +247 -247
  39. package/dist/templates/_shared/lib-ts/base/state-io.ts +202 -202
  40. package/dist/templates/_shared/lib-ts/base/stop-words.ts +184 -184
  41. package/dist/templates/_shared/lib-ts/base/utils.ts +184 -184
  42. package/dist/templates/_shared/lib-ts/context/context-formatter.ts +566 -566
  43. package/dist/templates/_shared/lib-ts/context/context-selector.ts +524 -524
  44. package/dist/templates/_shared/lib-ts/context/context-store.ts +712 -712
  45. package/dist/templates/_shared/lib-ts/context/plan-manager.ts +312 -312
  46. package/dist/templates/_shared/lib-ts/context/task-tracker.ts +185 -185
  47. package/dist/templates/_shared/lib-ts/package.json +20 -20
  48. package/dist/templates/_shared/lib-ts/templates/formatters.ts +102 -102
  49. package/dist/templates/_shared/lib-ts/templates/plan-context.ts +58 -58
  50. package/dist/templates/_shared/lib-ts/tsconfig.json +13 -13
  51. package/dist/templates/_shared/lib-ts/types.ts +186 -186
  52. package/dist/templates/_shared/scripts/resolve_context.ts +33 -33
  53. package/dist/templates/_shared/scripts/status_line.ts +690 -690
  54. package/dist/templates/cc-native/.claude/commands/cc-native/rlm/ask.md +136 -136
  55. package/dist/templates/cc-native/.claude/commands/cc-native/rlm/index.md +21 -21
  56. package/dist/templates/cc-native/.claude/commands/cc-native/rlm/overview.md +56 -56
  57. package/dist/templates/cc-native/.claude/commands/cc-native/specdev.md +10 -10
  58. package/dist/templates/cc-native/.windsurf/workflows/cc-native/fix.md +8 -8
  59. package/dist/templates/cc-native/.windsurf/workflows/cc-native/implement.md +8 -8
  60. package/dist/templates/cc-native/.windsurf/workflows/cc-native/research.md +8 -8
  61. package/dist/templates/cc-native/CC-NATIVE-README.md +189 -189
  62. package/dist/templates/cc-native/TEMPLATE-SCHEMA.md +304 -304
  63. package/dist/templates/cc-native/_cc-native/agents/CLAUDE.md +143 -143
  64. package/dist/templates/cc-native/_cc-native/agents/PLAN-ORCHESTRATOR.md +213 -213
  65. package/dist/templates/cc-native/_cc-native/agents/plan-questions/PLAN-QUESTIONER.md +70 -70
  66. package/dist/templates/cc-native/_cc-native/cc-native.config.json +96 -96
  67. package/dist/templates/cc-native/_cc-native/hooks/CLAUDE.md +247 -247
  68. package/dist/templates/cc-native/_cc-native/hooks/cc-native-plan-review.ts +76 -76
  69. package/dist/templates/cc-native/_cc-native/hooks/enhance_plan_post_subagent.ts +54 -54
  70. package/dist/templates/cc-native/_cc-native/hooks/enhance_plan_post_write.ts +51 -51
  71. package/dist/templates/cc-native/_cc-native/hooks/mark_questions_asked.ts +53 -53
  72. package/dist/templates/cc-native/_cc-native/hooks/plan_questions_early.ts +61 -61
  73. package/dist/templates/cc-native/_cc-native/lib-ts/agent-selection.ts +163 -163
  74. package/dist/templates/cc-native/_cc-native/lib-ts/aggregate-agents.ts +156 -156
  75. package/dist/templates/cc-native/_cc-native/lib-ts/artifacts/format.ts +597 -597
  76. package/dist/templates/cc-native/_cc-native/lib-ts/artifacts/index.ts +26 -26
  77. package/dist/templates/cc-native/_cc-native/lib-ts/artifacts/tracker.ts +107 -107
  78. package/dist/templates/cc-native/_cc-native/lib-ts/artifacts/write.ts +119 -119
  79. package/dist/templates/cc-native/_cc-native/lib-ts/artifacts.ts +21 -21
  80. package/dist/templates/cc-native/_cc-native/lib-ts/cc-native-state.ts +319 -319
  81. package/dist/templates/cc-native/_cc-native/lib-ts/cli-output-parser.ts +144 -144
  82. package/dist/templates/cc-native/_cc-native/lib-ts/config.ts +57 -57
  83. package/dist/templates/cc-native/_cc-native/lib-ts/constants.ts +83 -83
  84. package/dist/templates/cc-native/_cc-native/lib-ts/corroboration.ts +119 -119
  85. package/dist/templates/cc-native/_cc-native/lib-ts/debug.ts +79 -79
  86. package/dist/templates/cc-native/_cc-native/lib-ts/graduation.ts +132 -132
  87. package/dist/templates/cc-native/_cc-native/lib-ts/index.ts +116 -116
  88. package/dist/templates/cc-native/_cc-native/lib-ts/json-parser.ts +168 -168
  89. package/dist/templates/cc-native/_cc-native/lib-ts/orchestrator.ts +70 -70
  90. package/dist/templates/cc-native/_cc-native/lib-ts/output-builder.ts +130 -130
  91. package/dist/templates/cc-native/_cc-native/lib-ts/plan-discovery.ts +80 -80
  92. package/dist/templates/cc-native/_cc-native/lib-ts/plan-enhancement.ts +41 -41
  93. package/dist/templates/cc-native/_cc-native/lib-ts/plan-questions.ts +101 -101
  94. package/dist/templates/cc-native/_cc-native/lib-ts/review-pipeline.ts +511 -511
  95. package/dist/templates/cc-native/_cc-native/lib-ts/reviewers/agent.ts +71 -71
  96. package/dist/templates/cc-native/_cc-native/lib-ts/reviewers/base/base-agent.ts +217 -217
  97. package/dist/templates/cc-native/_cc-native/lib-ts/reviewers/index.ts +12 -12
  98. package/dist/templates/cc-native/_cc-native/lib-ts/reviewers/providers/claude-agent.ts +66 -66
  99. package/dist/templates/cc-native/_cc-native/lib-ts/reviewers/providers/codex-agent.ts +184 -184
  100. package/dist/templates/cc-native/_cc-native/lib-ts/reviewers/providers/gemini-agent.ts +39 -39
  101. package/dist/templates/cc-native/_cc-native/lib-ts/reviewers/providers/orchestrator-claude-agent.ts +196 -196
  102. package/dist/templates/cc-native/_cc-native/lib-ts/reviewers/schemas.ts +201 -201
  103. package/dist/templates/cc-native/_cc-native/lib-ts/reviewers/types.ts +21 -21
  104. package/dist/templates/cc-native/_cc-native/lib-ts/rlm/CLAUDE.md +480 -480
  105. package/dist/templates/cc-native/_cc-native/lib-ts/rlm/embedding-indexer.ts +287 -287
  106. package/dist/templates/cc-native/_cc-native/lib-ts/rlm/hyde.ts +148 -148
  107. package/dist/templates/cc-native/_cc-native/lib-ts/rlm/index.ts +54 -54
  108. package/dist/templates/cc-native/_cc-native/lib-ts/rlm/logger.ts +58 -58
  109. package/dist/templates/cc-native/_cc-native/lib-ts/rlm/ollama-client.ts +208 -208
  110. package/dist/templates/cc-native/_cc-native/lib-ts/rlm/retrieval-pipeline.ts +460 -460
  111. package/dist/templates/cc-native/_cc-native/lib-ts/rlm/transcript-indexer.ts +446 -446
  112. package/dist/templates/cc-native/_cc-native/lib-ts/rlm/transcript-loader.ts +280 -280
  113. package/dist/templates/cc-native/_cc-native/lib-ts/rlm/transcript-searcher.ts +274 -274
  114. package/dist/templates/cc-native/_cc-native/lib-ts/rlm/types.ts +201 -201
  115. package/dist/templates/cc-native/_cc-native/lib-ts/rlm/vector-store.ts +278 -278
  116. package/dist/templates/cc-native/_cc-native/lib-ts/settings.ts +184 -184
  117. package/dist/templates/cc-native/_cc-native/lib-ts/state.ts +275 -275
  118. package/dist/templates/cc-native/_cc-native/lib-ts/tsconfig.json +18 -18
  119. package/dist/templates/cc-native/_cc-native/lib-ts/types.ts +329 -329
  120. package/dist/templates/cc-native/_cc-native/lib-ts/verdict.ts +72 -72
  121. package/dist/templates/cc-native/_cc-native/workflows/specdev.md +9 -9
  122. package/oclif.manifest.json +1 -1
  123. package/package.json +108 -108
  124. package/dist/templates/cc-native/_cc-native/lib-ts/nul +0 -3
@@ -1,421 +1,421 @@
1
- # Handoff System
2
-
3
- Comprehensive specification for the handoff workflow system.
4
-
5
- ## Overview
6
-
7
- - **Purpose:** Capture session state for asynchronous handoff to next session
8
- - **Philosophy:** Structured sections (dead-ends, pending, decisions, etc.) + executable restoration
9
- - **When to use:** End of work session when context needs preservation
10
- - **Runtime location:** `_output/contexts/{context_id}/handoffs/{YYYY-MM-DD-HHMM}/`
11
-
12
- ## Directory Structure
13
-
14
- ```
15
- handoff-system/
16
- ├── CLAUDE.md # This file — comprehensive spec
17
- ├── lib/ # Reusable modules
18
- │ ├── handoff-reader.ts # Read and parse handoff sections
19
- │ └── document-generator.ts # Generate handoff markdown (not currently imported)
20
- ├── scripts/ # CLI entry points
21
- │ ├── save_handoff.ts # Create handoff from stdin markdown
22
- │ └── resume_handoff.ts # Load handoff and format for restoration
23
- └── workflows/ # Procedural documentation
24
- ├── handoff.md # Creation workflow
25
- └── handoff-resume.md # Restoration workflow
26
- ```
27
-
28
- ## Data Model & Schema
29
-
30
- ### Handoff Folder Structure
31
-
32
- Runtime location: `_output/contexts/{context_id}/handoffs/{YYYY-MM-DD-HHMM}/`
33
-
34
- **Files created:**
35
- - `index.md` — Entry point with frontmatter
36
- - `completed-work.md` — What was accomplished
37
- - `dead-ends.md` — Failed approaches to avoid
38
- - `decisions.md` — Key decisions made
39
- - `pending.md` — Incomplete work items
40
- - `context.md` — Background context
41
- - `plan.md` — Optional, copied from context if plan exists
42
-
43
- ### HandoffSections Interface
44
-
45
- ```typescript
46
- // From _shared/lib-ts/types.ts
47
- interface HandoffSections {
48
- index: string; // Entry point with frontmatter
49
- deadEnds: string | null; // Failed approaches to avoid
50
- pending: string | null; // Incomplete work items
51
- plan: string | null; // Copy of plan file if exists
52
- decisions: string | null; // Key decisions made
53
- completedWork: string | null; // What was accomplished
54
- context: string | null; // Background context
55
- }
56
- ```
57
-
58
- **Why inline interfaces:** Makes CLAUDE.md self-contained — agents don't jump between files.
59
-
60
- ### Section Markers
61
-
62
- Content parsed via HTML comments: `<!-- SECTION: name -->`
63
-
64
- **Valid section names:**
65
- - `CONTEXT`
66
- - `COMPLETED_WORK`
67
- - `DEAD_ENDS`
68
- - `DECISIONS`
69
- - `PENDING`
70
- - `PLAN`
71
-
72
- **Critical:** Markers must be HTML comments, not markdown headings. Parser searches for `<!-- SECTION: -->` format.
73
-
74
- ### Frontmatter Schema
75
-
76
- YAML frontmatter in index.md:
77
-
78
- ```yaml
79
- ---
80
- title: Handoff - {project_name}
81
- date: {ISO timestamp}
82
- session_id: {Claude session ID}
83
- project: {context directory name}
84
- plan_document: {path to plan file if exists}
85
- ---
86
- ```
87
-
88
- ## Lifecycle & Hook Integration
89
-
90
- ### Creation Flow (save_handoff.ts script)
91
-
92
- **Trigger:** `/handoff` command invokes script with stdin markdown
93
-
94
- **Process:**
95
- 1. Parse frontmatter and section markers from stdin
96
- 2. Resolve context ID (5-tier resolution: `--context-id`, `--session-id`, frontmatter, `CLAUDE_SESSION_ID`, fallback to active)
97
- 3. Create timestamped folder: `_output/contexts/{context_id}/handoffs/{YYYY-MM-DD-HHMM}/`
98
- 4. Shard content into section files
99
- 5. Update state.json with handoff metadata
100
-
101
- **State updates:**
102
- - `handoff_path`: Set to `handoffs/{timestamp}/index.md` (relative to context folder)
103
- - `work_consumed`: Set to `false` (enables staging)
104
- - `next_artifact_type`: Set to `"handoff"` (explicit artifact tracking)
105
- - **Latest-wins replacement:** If `plan_hash` exists, clear plan fields (`plan_path`, `plan_hash`, `plan_signature`)
106
-
107
- **Context resolution tiers** (first match wins):
108
- 1. `--context-id` CLI arg
109
- 2. `--session-id` CLI arg (lookup via CLAUDE_SESSION_ID → context)
110
- 3. `session_id` in frontmatter (lookup via session → context)
111
- 4. `CLAUDE_SESSION_ID` env var (lookup via session → context)
112
- 5. Fallback: most recent active context
113
-
114
- ### Staging (session_end.ts hook)
115
-
116
- **Trigger:** SessionEnd event
117
-
118
- **Condition for staging:**
119
- ```typescript
120
- if (state.handoff_path && !state.work_consumed) {
121
- state.mode = "has_staged_work";
122
- state.next_artifact_type = "handoff";
123
- }
124
- ```
125
-
126
- **Latest-wins detection:**
127
- If `plan_hash` differs from `plan_hash_consumed`, new plan detected:
128
- - Clear `handoff_path` (plan wins)
129
- - Set `work_consumed = false`
130
- - Set `next_artifact_type = "plan"`
131
-
132
- **Unified staging mode:** `has_staged_work` replaces old `has_plan` and `has_handoff` modes (v0.13.0+).
133
-
134
- ### Restoration (session_start.ts hook)
135
-
136
- **Trigger:** SessionStart event with `source = "clear"`
137
-
138
- **Process:**
139
- 1. Find context with `mode = "has_staged_work"`
140
- 2. Dispatch by `next_artifact_type`:
141
- - If `"handoff"`: call `formatHandoffContinuation(ctx)`
142
- - If `"plan"`: inject plan content
143
- 3. Bind session to context
144
- 4. Transition `has_staged_work` → `active`
145
- 5. Set `work_consumed = true` (one-shot latch prevents re-staging)
146
-
147
- **formatHandoffContinuation()** (from context-formatter.ts):
148
- - Reads handoff sections via `readHandoffSections()`
149
- - Assembles restoration context: dead-ends → pending → plan remaining → decisions → git delta → completed work
150
- - Injects as system-reminder for Claude
151
-
152
- ### Fallback Matching (user_prompt_submit.ts hook)
153
-
154
- **Trigger:** UserPromptSubmit when no staged work mode detected
155
-
156
- **Process in determineContext():**
157
- 1. Filter contexts by `has_staged_work` mode
158
- 2. Separate by `determineArtifactType()`:
159
- - Plan artifacts: check `plan_hash` and `plan_path`
160
- - Handoff artifacts: check `handoff_path`
161
- 3. Try plan match first (content-based via plan hash)
162
- 4. Fall back to handoff match (first-match by recency)
163
- 5. Set `work_consumed = true` if match found
164
-
165
- **determineArtifactType() utility:**
166
- - Checks `next_artifact_type` field first (authoritative)
167
- - Fallback: field detection (`plan_hash` + `plan_path` vs `handoff_path`)
168
- - Logs warning if both plan and handoff fields exist (bug - violates latest-wins)
169
-
170
- ## Scripts
171
-
172
- ### save_handoff.ts
173
-
174
- **Usage:**
175
- ```bash
176
- bun .aiwcli/_shared/handoff-system/scripts/save_handoff.ts [--context-id ID] [--session-id SID] < handoff.md
177
- ```
178
-
179
- **Stdin format:**
180
- ```markdown
181
- ---
182
- title: Handoff - Project Name
183
- session_id: abc123
184
- ---
185
-
186
- # Handoff Document
187
-
188
- <!-- SECTION: CONTEXT -->
189
- Context details here...
190
-
191
- <!-- SECTION: PENDING -->
192
- Pending items...
193
- ```
194
-
195
- **Output:**
196
- - Creates folder: `_output/contexts/{context_id}/handoffs/{YYYY-MM-DD-HHMM}/`
197
- - Shards to files: `index.md`, `pending.md`, `dead-ends.md`, etc.
198
- - Updates `state.json`: `handoff_path`, `work_consumed=false`, `next_artifact_type="handoff"`
199
-
200
- **Collision handling:**
201
- If timestamp folder exists, appends `-2`, `-3`, etc.
202
-
203
- **State updates (latest-wins):**
204
- - Sets handoff fields
205
- - **Clears plan fields if they exist** (`plan_path`, `plan_hash`, `plan_signature` → null)
206
-
207
- ### resume_handoff.ts
208
-
209
- **Usage:**
210
- ```bash
211
- # Auto-discover from current session
212
- bun .aiwcli/_shared/handoff-system/scripts/resume_handoff.ts
213
-
214
- # Explicit handoff path
215
- bun .aiwcli/_shared/handoff-system/scripts/resume_handoff.ts path/to/handoff/index.md
216
-
217
- # Explicit context
218
- bun .aiwcli/_shared/handoff-system/scripts/resume_handoff.ts --context context-id
219
- ```
220
-
221
- **Output format (to stdout):**
222
- ```markdown
223
- # Handoff Restoration
224
-
225
- ## Dead Ends (Priority: Address First)
226
- ...
227
-
228
- ## Pending Items
229
- ...
230
-
231
- ## Plan Status
232
- 42% complete (5/12 tasks)
233
-
234
- ## Decisions Made
235
- ...
236
-
237
- ## Git Delta
238
- Changed files since handoff...
239
-
240
- ## Completed Work
241
- ...
242
-
243
- ## Full Plan (Appendix)
244
- ...
245
- ```
246
-
247
- **Features:**
248
- - **Staleness warnings:** If handoff > 7 days old, warns in output
249
- - **Plan progress:** Calculates % complete from plan anchors
250
- - **Git delta:** Compares current git state to plan's git anchors
251
- - **Priority ordering:** Dead-ends first, then pending, then context
252
-
253
- **Auto-discovery:**
254
- Uses `CLAUDE_SESSION_ID` env var → lookup context → find latest handoff in `handoffs/` folder.
255
-
256
- ## Library Modules
257
-
258
- ### handoff-reader.ts
259
-
260
- **Location:** `_shared/handoff-system/lib/handoff-reader.ts`
261
-
262
- **Exports:**
263
-
264
- ```typescript
265
- // Find most recent handoff in context
266
- function findLatestHandoff(contextPath: string): string | null
267
-
268
- // Read and parse handoff sections
269
- function readHandoffSections(handoffPath: string): HandoffSections
270
-
271
- // Extract timestamp from handoff path
272
- function getHandoffTimestamp(handoffPath: string): Date | null
273
-
274
- // Get plan reference from handoff frontmatter
275
- function getHandoffPlanReference(handoffPath: string): string | null
276
- ```
277
-
278
- **Section mapping:**
279
- ```typescript
280
- const SECTION_FILES = {
281
- index: "index.md",
282
- deadEnds: "dead-ends.md",
283
- pending: "pending.md",
284
- plan: "plan.md",
285
- decisions: "decisions.md",
286
- completedWork: "completed-work.md",
287
- context: "context.md",
288
- };
289
- ```
290
-
291
- **Returns null for missing optional sections** (dead-ends, pending, etc.).
292
-
293
- ### document-generator.ts
294
-
295
- **Location:** `_shared/handoff-system/lib/document-generator.ts`
296
-
297
- **Exports:**
298
-
299
- ```typescript
300
- // Generate complete handoff markdown with sections
301
- function generateHandoffDocument(options: HandoffOptions): string
302
-
303
- // Section builders
304
- function buildContextSection(ctx: ContextState): string
305
- function buildCompletedWorkSection(history: WorkHistory): string
306
- function buildDeadEndsSection(deadEnds: DeadEnd[]): string
307
- // ... (other section builders)
308
- ```
309
-
310
- **Used by:** `/handoff` workflow to programmatically generate handoff content before piping to `save_handoff.ts`.
311
-
312
- **Note:** Currently not imported by any files. Moved here for logical grouping and completeness.
313
-
314
- ## Command Integration
315
-
316
- **Thin pointer pattern:**
317
-
318
- `.claude/commands/handoff.md` (user-facing, discoverable via `/`)
319
- → References `.aiwcli/_shared/handoff-system/workflows/handoff.md` (detailed procedural steps)
320
-
321
- **Benefits:**
322
- - Command files stay concise (easy to scan in `/` menu)
323
- - Workflow files can expand without bloating command discovery
324
- - Single source of truth for procedural details
325
-
326
- **Example reference format:**
327
- ```markdown
328
- See `.aiwcli/_shared/handoff-system/workflows/handoff.md` for complete process documentation.
329
- ```
330
-
331
- ## Testing
332
-
333
- **Integration test:**
334
- `_shared/lib-ts/__tests__/integration/handoff-lifecycle.test.ts`
335
-
336
- **Coverage:**
337
- - Creation: active + handoff → session_end → has_staged_work
338
- - Restoration: /clear → session_start → active + work_consumed=true
339
- - One-shot latch: subsequent session_end does NOT re-stage (work_consumed=true)
340
-
341
- **Hook execution test:**
342
- ```bash
343
- echo '{"hook_event_name":"SessionStart","session_id":"test","source":"clear"}' | \
344
- bun .aiwcli/_shared/hooks-ts/session_start.ts
345
- ```
346
-
347
- Expected: No import errors, clean execution.
348
-
349
- ## Migration Notes (v0.13.0+)
350
-
351
- **Unified lifecycle:**
352
- - Old modes `has_plan` and `has_handoff` → unified `has_staged_work`
353
- - Old flags `plan_consumed` and `handoff_consumed` → unified `work_consumed`
354
- - New field `next_artifact_type` (`"plan" | "handoff" | null`) for explicit artifact tracking
355
-
356
- **Migration handled by migrateConsumedFlags()** in `state-io.ts`:
357
- - Runs on every `state.json` read
358
- - Transparently converts old modes to new structure
359
- - Idempotent (safe to run multiple times)
360
-
361
- **Latest-wins principle:**
362
- - Only ONE artifact staged at a time
363
- - New handoff clears plan fields
364
- - New plan clears handoff_path
365
- - Most recent creation wins
366
-
367
- **Work consumed as one-shot latch:**
368
- - Set to `true` when `has_staged_work` → `active` transition occurs
369
- - Prevents `session_end` from re-staging same artifact
370
- - Reset to `false` when new artifact created
371
-
372
- ## Architecture Decisions
373
-
374
- **Why folder sharding (not single file)?**
375
- - Enables selective loading (resume script loads only needed sections)
376
- - Allows future extensions (e.g., attachments, screenshots)
377
- - Chronological discovery via timestamp folders
378
-
379
- **Why section markers (not frontmatter)?**
380
- - Flexible content generation (Claude outputs sections in natural flow)
381
- - No strict ordering required (script extracts by marker)
382
- - Easy to extend (add new sections without schema version bump)
383
-
384
- **Why latest-wins (not dual artifact tracking)?**
385
- - Simplifies state machine (one artifact, one mode transition)
386
- - Prevents ambiguity (which artifact to restore?)
387
- - Matches user mental model (most recent work is relevant)
388
-
389
- **Why unified work_consumed flag?**
390
- - Prevents infinite re-staging loop
391
- - Simpler than per-artifact flags (plan_consumed, handoff_consumed)
392
- - One-shot latch pattern is well-understood
393
-
394
- ## Gotchas
395
-
396
- **Template sync is mandatory:**
397
- Both `.aiwcli/_shared/handoff-system/` (working copy) and `packages/cli/src/templates/_shared/handoff-system/` (template source) must stay in sync per CLAUDE.md template sync rules.
398
-
399
- **Import paths after move:**
400
- - From `scripts/resume_handoff.ts` → `lib/handoff-reader.ts`: `../lib/handoff-reader.js`
401
- - From `lib/handoff-reader.ts` → `lib-ts/base/constants.ts`: `../../lib-ts/base/constants.js`
402
-
403
- **Hooks don't import handoff-reader:**
404
- - `session_start.ts` uses `formatHandoffContinuation()` from `context-formatter.ts`
405
- - `context-formatter.ts` reads `ctx.handoff_path` directly
406
- - No direct handoff-reader dependency in hooks
407
-
408
- **Command file script paths are absolute:**
409
- - Reference from project root: `.aiwcli/_shared/handoff-system/scripts/save_handoff.ts`
410
- - NOT relative to command file location
411
-
412
- **Section markers must be HTML comments:**
413
- ```markdown
414
- <!-- SECTION: PENDING --> ✅ Correct
415
- # SECTION: PENDING ❌ Incorrect (parsed as heading, not marker)
416
- ```
417
-
418
- **Windsurf and Codex workflows also reference scripts:**
419
- When updating script paths, check:
420
- - `packages/cli/src/templates/_shared/.windsurf/workflows/handoff.md`
421
- - `packages/cli/src/templates/_shared/.codex/workflows/handoff.md`
1
+ # Handoff System
2
+
3
+ Comprehensive specification for the handoff workflow system.
4
+
5
+ ## Overview
6
+
7
+ - **Purpose:** Capture session state for asynchronous handoff to next session
8
+ - **Philosophy:** Structured sections (dead-ends, pending, decisions, etc.) + executable restoration
9
+ - **When to use:** End of work session when context needs preservation
10
+ - **Runtime location:** `_output/contexts/{context_id}/handoffs/{YYYY-MM-DD-HHMM}/`
11
+
12
+ ## Directory Structure
13
+
14
+ ```
15
+ handoff-system/
16
+ ├── CLAUDE.md # This file — comprehensive spec
17
+ ├── lib/ # Reusable modules
18
+ │ ├── handoff-reader.ts # Read and parse handoff sections
19
+ │ └── document-generator.ts # Generate handoff markdown (not currently imported)
20
+ ├── scripts/ # CLI entry points
21
+ │ ├── save_handoff.ts # Create handoff from stdin markdown
22
+ │ └── resume_handoff.ts # Load handoff and format for restoration
23
+ └── workflows/ # Procedural documentation
24
+ ├── handoff.md # Creation workflow
25
+ └── handoff-resume.md # Restoration workflow
26
+ ```
27
+
28
+ ## Data Model & Schema
29
+
30
+ ### Handoff Folder Structure
31
+
32
+ Runtime location: `_output/contexts/{context_id}/handoffs/{YYYY-MM-DD-HHMM}/`
33
+
34
+ **Files created:**
35
+ - `index.md` — Entry point with frontmatter
36
+ - `completed-work.md` — What was accomplished
37
+ - `dead-ends.md` — Failed approaches to avoid
38
+ - `decisions.md` — Key decisions made
39
+ - `pending.md` — Incomplete work items
40
+ - `context.md` — Background context
41
+ - `plan.md` — Optional, copied from context if plan exists
42
+
43
+ ### HandoffSections Interface
44
+
45
+ ```typescript
46
+ // From _shared/lib-ts/types.ts
47
+ interface HandoffSections {
48
+ index: string; // Entry point with frontmatter
49
+ deadEnds: string | null; // Failed approaches to avoid
50
+ pending: string | null; // Incomplete work items
51
+ plan: string | null; // Copy of plan file if exists
52
+ decisions: string | null; // Key decisions made
53
+ completedWork: string | null; // What was accomplished
54
+ context: string | null; // Background context
55
+ }
56
+ ```
57
+
58
+ **Why inline interfaces:** Makes CLAUDE.md self-contained — agents don't jump between files.
59
+
60
+ ### Section Markers
61
+
62
+ Content parsed via HTML comments: `<!-- SECTION: name -->`
63
+
64
+ **Valid section names:**
65
+ - `CONTEXT`
66
+ - `COMPLETED_WORK`
67
+ - `DEAD_ENDS`
68
+ - `DECISIONS`
69
+ - `PENDING`
70
+ - `PLAN`
71
+
72
+ **Critical:** Markers must be HTML comments, not markdown headings. Parser searches for `<!-- SECTION: -->` format.
73
+
74
+ ### Frontmatter Schema
75
+
76
+ YAML frontmatter in index.md:
77
+
78
+ ```yaml
79
+ ---
80
+ title: Handoff - {project_name}
81
+ date: {ISO timestamp}
82
+ session_id: {Claude session ID}
83
+ project: {context directory name}
84
+ plan_document: {path to plan file if exists}
85
+ ---
86
+ ```
87
+
88
+ ## Lifecycle & Hook Integration
89
+
90
+ ### Creation Flow (save_handoff.ts script)
91
+
92
+ **Trigger:** `/handoff` command invokes script with stdin markdown
93
+
94
+ **Process:**
95
+ 1. Parse frontmatter and section markers from stdin
96
+ 2. Resolve context ID (5-tier resolution: `--context-id`, `--session-id`, frontmatter, `CLAUDE_SESSION_ID`, fallback to active)
97
+ 3. Create timestamped folder: `_output/contexts/{context_id}/handoffs/{YYYY-MM-DD-HHMM}/`
98
+ 4. Shard content into section files
99
+ 5. Update state.json with handoff metadata
100
+
101
+ **State updates:**
102
+ - `handoff_path`: Set to `handoffs/{timestamp}/index.md` (relative to context folder)
103
+ - `work_consumed`: Set to `false` (enables staging)
104
+ - `next_artifact_type`: Set to `"handoff"` (explicit artifact tracking)
105
+ - **Latest-wins replacement:** If `plan_hash` exists, clear plan fields (`plan_path`, `plan_hash`, `plan_signature`)
106
+
107
+ **Context resolution tiers** (first match wins):
108
+ 1. `--context-id` CLI arg
109
+ 2. `--session-id` CLI arg (lookup via CLAUDE_SESSION_ID → context)
110
+ 3. `session_id` in frontmatter (lookup via session → context)
111
+ 4. `CLAUDE_SESSION_ID` env var (lookup via session → context)
112
+ 5. Fallback: most recent active context
113
+
114
+ ### Staging (session_end.ts hook)
115
+
116
+ **Trigger:** SessionEnd event
117
+
118
+ **Condition for staging:**
119
+ ```typescript
120
+ if (state.handoff_path && !state.work_consumed) {
121
+ state.mode = "has_staged_work";
122
+ state.next_artifact_type = "handoff";
123
+ }
124
+ ```
125
+
126
+ **Latest-wins detection:**
127
+ If `plan_hash` differs from `plan_hash_consumed`, new plan detected:
128
+ - Clear `handoff_path` (plan wins)
129
+ - Set `work_consumed = false`
130
+ - Set `next_artifact_type = "plan"`
131
+
132
+ **Unified staging mode:** `has_staged_work` replaces old `has_plan` and `has_handoff` modes (v0.13.0+).
133
+
134
+ ### Restoration (session_start.ts hook)
135
+
136
+ **Trigger:** SessionStart event with `source = "clear"`
137
+
138
+ **Process:**
139
+ 1. Find context with `mode = "has_staged_work"`
140
+ 2. Dispatch by `next_artifact_type`:
141
+ - If `"handoff"`: call `formatHandoffContinuation(ctx)`
142
+ - If `"plan"`: inject plan content
143
+ 3. Bind session to context
144
+ 4. Transition `has_staged_work` → `active`
145
+ 5. Set `work_consumed = true` (one-shot latch prevents re-staging)
146
+
147
+ **formatHandoffContinuation()** (from context-formatter.ts):
148
+ - Reads handoff sections via `readHandoffSections()`
149
+ - Assembles restoration context: dead-ends → pending → plan remaining → decisions → git delta → completed work
150
+ - Injects as system-reminder for Claude
151
+
152
+ ### Fallback Matching (user_prompt_submit.ts hook)
153
+
154
+ **Trigger:** UserPromptSubmit when no staged work mode detected
155
+
156
+ **Process in determineContext():**
157
+ 1. Filter contexts by `has_staged_work` mode
158
+ 2. Separate by `determineArtifactType()`:
159
+ - Plan artifacts: check `plan_hash` and `plan_path`
160
+ - Handoff artifacts: check `handoff_path`
161
+ 3. Try plan match first (content-based via plan hash)
162
+ 4. Fall back to handoff match (first-match by recency)
163
+ 5. Set `work_consumed = true` if match found
164
+
165
+ **determineArtifactType() utility:**
166
+ - Checks `next_artifact_type` field first (authoritative)
167
+ - Fallback: field detection (`plan_hash` + `plan_path` vs `handoff_path`)
168
+ - Logs warning if both plan and handoff fields exist (bug - violates latest-wins)
169
+
170
+ ## Scripts
171
+
172
+ ### save_handoff.ts
173
+
174
+ **Usage:**
175
+ ```bash
176
+ bun .aiwcli/_shared/handoff-system/scripts/save_handoff.ts [--context-id ID] [--session-id SID] < handoff.md
177
+ ```
178
+
179
+ **Stdin format:**
180
+ ```markdown
181
+ ---
182
+ title: Handoff - Project Name
183
+ session_id: abc123
184
+ ---
185
+
186
+ # Handoff Document
187
+
188
+ <!-- SECTION: CONTEXT -->
189
+ Context details here...
190
+
191
+ <!-- SECTION: PENDING -->
192
+ Pending items...
193
+ ```
194
+
195
+ **Output:**
196
+ - Creates folder: `_output/contexts/{context_id}/handoffs/{YYYY-MM-DD-HHMM}/`
197
+ - Shards to files: `index.md`, `pending.md`, `dead-ends.md`, etc.
198
+ - Updates `state.json`: `handoff_path`, `work_consumed=false`, `next_artifact_type="handoff"`
199
+
200
+ **Collision handling:**
201
+ If timestamp folder exists, appends `-2`, `-3`, etc.
202
+
203
+ **State updates (latest-wins):**
204
+ - Sets handoff fields
205
+ - **Clears plan fields if they exist** (`plan_path`, `plan_hash`, `plan_signature` → null)
206
+
207
+ ### resume_handoff.ts
208
+
209
+ **Usage:**
210
+ ```bash
211
+ # Auto-discover from current session
212
+ bun .aiwcli/_shared/handoff-system/scripts/resume_handoff.ts
213
+
214
+ # Explicit handoff path
215
+ bun .aiwcli/_shared/handoff-system/scripts/resume_handoff.ts path/to/handoff/index.md
216
+
217
+ # Explicit context
218
+ bun .aiwcli/_shared/handoff-system/scripts/resume_handoff.ts --context context-id
219
+ ```
220
+
221
+ **Output format (to stdout):**
222
+ ```markdown
223
+ # Handoff Restoration
224
+
225
+ ## Dead Ends (Priority: Address First)
226
+ ...
227
+
228
+ ## Pending Items
229
+ ...
230
+
231
+ ## Plan Status
232
+ 42% complete (5/12 tasks)
233
+
234
+ ## Decisions Made
235
+ ...
236
+
237
+ ## Git Delta
238
+ Changed files since handoff...
239
+
240
+ ## Completed Work
241
+ ...
242
+
243
+ ## Full Plan (Appendix)
244
+ ...
245
+ ```
246
+
247
+ **Features:**
248
+ - **Staleness warnings:** If handoff > 7 days old, warns in output
249
+ - **Plan progress:** Calculates % complete from plan anchors
250
+ - **Git delta:** Compares current git state to plan's git anchors
251
+ - **Priority ordering:** Dead-ends first, then pending, then context
252
+
253
+ **Auto-discovery:**
254
+ Uses `CLAUDE_SESSION_ID` env var → lookup context → find latest handoff in `handoffs/` folder.
255
+
256
+ ## Library Modules
257
+
258
+ ### handoff-reader.ts
259
+
260
+ **Location:** `_shared/handoff-system/lib/handoff-reader.ts`
261
+
262
+ **Exports:**
263
+
264
+ ```typescript
265
+ // Find most recent handoff in context
266
+ function findLatestHandoff(contextPath: string): string | null
267
+
268
+ // Read and parse handoff sections
269
+ function readHandoffSections(handoffPath: string): HandoffSections
270
+
271
+ // Extract timestamp from handoff path
272
+ function getHandoffTimestamp(handoffPath: string): Date | null
273
+
274
+ // Get plan reference from handoff frontmatter
275
+ function getHandoffPlanReference(handoffPath: string): string | null
276
+ ```
277
+
278
+ **Section mapping:**
279
+ ```typescript
280
+ const SECTION_FILES = {
281
+ index: "index.md",
282
+ deadEnds: "dead-ends.md",
283
+ pending: "pending.md",
284
+ plan: "plan.md",
285
+ decisions: "decisions.md",
286
+ completedWork: "completed-work.md",
287
+ context: "context.md",
288
+ };
289
+ ```
290
+
291
+ **Returns null for missing optional sections** (dead-ends, pending, etc.).
292
+
293
+ ### document-generator.ts
294
+
295
+ **Location:** `_shared/handoff-system/lib/document-generator.ts`
296
+
297
+ **Exports:**
298
+
299
+ ```typescript
300
+ // Generate complete handoff markdown with sections
301
+ function generateHandoffDocument(options: HandoffOptions): string
302
+
303
+ // Section builders
304
+ function buildContextSection(ctx: ContextState): string
305
+ function buildCompletedWorkSection(history: WorkHistory): string
306
+ function buildDeadEndsSection(deadEnds: DeadEnd[]): string
307
+ // ... (other section builders)
308
+ ```
309
+
310
+ **Used by:** `/handoff` workflow to programmatically generate handoff content before piping to `save_handoff.ts`.
311
+
312
+ **Note:** Currently not imported by any files. Moved here for logical grouping and completeness.
313
+
314
+ ## Command Integration
315
+
316
+ **Thin pointer pattern:**
317
+
318
+ `.claude/commands/handoff.md` (user-facing, discoverable via `/`)
319
+ → References `.aiwcli/_shared/handoff-system/workflows/handoff.md` (detailed procedural steps)
320
+
321
+ **Benefits:**
322
+ - Command files stay concise (easy to scan in `/` menu)
323
+ - Workflow files can expand without bloating command discovery
324
+ - Single source of truth for procedural details
325
+
326
+ **Example reference format:**
327
+ ```markdown
328
+ See `.aiwcli/_shared/handoff-system/workflows/handoff.md` for complete process documentation.
329
+ ```
330
+
331
+ ## Testing
332
+
333
+ **Integration test:**
334
+ `_shared/lib-ts/__tests__/integration/handoff-lifecycle.test.ts`
335
+
336
+ **Coverage:**
337
+ - Creation: active + handoff → session_end → has_staged_work
338
+ - Restoration: /clear → session_start → active + work_consumed=true
339
+ - One-shot latch: subsequent session_end does NOT re-stage (work_consumed=true)
340
+
341
+ **Hook execution test:**
342
+ ```bash
343
+ echo '{"hook_event_name":"SessionStart","session_id":"test","source":"clear"}' | \
344
+ bun .aiwcli/_shared/hooks-ts/session_start.ts
345
+ ```
346
+
347
+ Expected: No import errors, clean execution.
348
+
349
+ ## Migration Notes (v0.13.0+)
350
+
351
+ **Unified lifecycle:**
352
+ - Old modes `has_plan` and `has_handoff` → unified `has_staged_work`
353
+ - Old flags `plan_consumed` and `handoff_consumed` → unified `work_consumed`
354
+ - New field `next_artifact_type` (`"plan" | "handoff" | null`) for explicit artifact tracking
355
+
356
+ **Migration handled by migrateConsumedFlags()** in `state-io.ts`:
357
+ - Runs on every `state.json` read
358
+ - Transparently converts old modes to new structure
359
+ - Idempotent (safe to run multiple times)
360
+
361
+ **Latest-wins principle:**
362
+ - Only ONE artifact staged at a time
363
+ - New handoff clears plan fields
364
+ - New plan clears handoff_path
365
+ - Most recent creation wins
366
+
367
+ **Work consumed as one-shot latch:**
368
+ - Set to `true` when `has_staged_work` → `active` transition occurs
369
+ - Prevents `session_end` from re-staging same artifact
370
+ - Reset to `false` when new artifact created
371
+
372
+ ## Architecture Decisions
373
+
374
+ **Why folder sharding (not single file)?**
375
+ - Enables selective loading (resume script loads only needed sections)
376
+ - Allows future extensions (e.g., attachments, screenshots)
377
+ - Chronological discovery via timestamp folders
378
+
379
+ **Why section markers (not frontmatter)?**
380
+ - Flexible content generation (Claude outputs sections in natural flow)
381
+ - No strict ordering required (script extracts by marker)
382
+ - Easy to extend (add new sections without schema version bump)
383
+
384
+ **Why latest-wins (not dual artifact tracking)?**
385
+ - Simplifies state machine (one artifact, one mode transition)
386
+ - Prevents ambiguity (which artifact to restore?)
387
+ - Matches user mental model (most recent work is relevant)
388
+
389
+ **Why unified work_consumed flag?**
390
+ - Prevents infinite re-staging loop
391
+ - Simpler than per-artifact flags (plan_consumed, handoff_consumed)
392
+ - One-shot latch pattern is well-understood
393
+
394
+ ## Gotchas
395
+
396
+ **Template sync is mandatory:**
397
+ Both `.aiwcli/_shared/handoff-system/` (working copy) and `packages/cli/src/templates/_shared/handoff-system/` (template source) must stay in sync per CLAUDE.md template sync rules.
398
+
399
+ **Import paths after move:**
400
+ - From `scripts/resume_handoff.ts` → `lib/handoff-reader.ts`: `../lib/handoff-reader.js`
401
+ - From `lib/handoff-reader.ts` → `lib-ts/base/constants.ts`: `../../lib-ts/base/constants.js`
402
+
403
+ **Hooks don't import handoff-reader:**
404
+ - `session_start.ts` uses `formatHandoffContinuation()` from `context-formatter.ts`
405
+ - `context-formatter.ts` reads `ctx.handoff_path` directly
406
+ - No direct handoff-reader dependency in hooks
407
+
408
+ **Command file script paths are absolute:**
409
+ - Reference from project root: `.aiwcli/_shared/handoff-system/scripts/save_handoff.ts`
410
+ - NOT relative to command file location
411
+
412
+ **Section markers must be HTML comments:**
413
+ ```markdown
414
+ <!-- SECTION: PENDING --> ✅ Correct
415
+ # SECTION: PENDING ❌ Incorrect (parsed as heading, not marker)
416
+ ```
417
+
418
+ **Windsurf and Codex workflows also reference scripts:**
419
+ When updating script paths, check:
420
+ - `packages/cli/src/templates/_shared/.windsurf/workflows/handoff.md`
421
+ - `packages/cli/src/templates/_shared/.codex/workflows/handoff.md`