@sienklogic/plan-build-run 2.0.0 → 2.0.1

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 (225) hide show
  1. package/CHANGELOG.md +56 -56
  2. package/CLAUDE.md +149 -149
  3. package/LICENSE +21 -21
  4. package/README.md +247 -247
  5. package/dashboard/bin/cli.js +25 -25
  6. package/dashboard/package.json +34 -34
  7. package/dashboard/public/css/layout.css +406 -406
  8. package/dashboard/public/css/status-colors.css +98 -98
  9. package/dashboard/public/js/htmx-title.js +5 -5
  10. package/dashboard/public/js/sidebar-toggle.js +20 -20
  11. package/dashboard/src/app.js +78 -78
  12. package/dashboard/src/middleware/errorHandler.js +52 -52
  13. package/dashboard/src/middleware/notFoundHandler.js +9 -9
  14. package/dashboard/src/repositories/planning.repository.js +128 -128
  15. package/dashboard/src/routes/events.routes.js +40 -40
  16. package/dashboard/src/routes/index.routes.js +31 -31
  17. package/dashboard/src/routes/pages.routes.js +245 -195
  18. package/dashboard/src/server.js +42 -42
  19. package/dashboard/src/services/dashboard.service.js +222 -222
  20. package/dashboard/src/services/phase.service.js +220 -167
  21. package/dashboard/src/services/project.service.js +57 -57
  22. package/dashboard/src/services/roadmap.service.js +171 -171
  23. package/dashboard/src/services/sse.service.js +58 -58
  24. package/dashboard/src/services/todo.service.js +254 -254
  25. package/dashboard/src/services/watcher.service.js +48 -48
  26. package/dashboard/src/views/coming-soon.ejs +11 -11
  27. package/dashboard/src/views/error.ejs +13 -13
  28. package/dashboard/src/views/index.ejs +5 -5
  29. package/dashboard/src/views/layout.ejs +1 -1
  30. package/dashboard/src/views/partials/dashboard-content.ejs +77 -77
  31. package/dashboard/src/views/partials/footer.ejs +3 -3
  32. package/dashboard/src/views/partials/head.ejs +21 -21
  33. package/dashboard/src/views/partials/header.ejs +12 -12
  34. package/dashboard/src/views/partials/layout-bottom.ejs +15 -15
  35. package/dashboard/src/views/partials/layout-top.ejs +8 -8
  36. package/dashboard/src/views/partials/phase-content.ejs +188 -181
  37. package/dashboard/src/views/partials/phase-doc-content.ejs +38 -0
  38. package/dashboard/src/views/partials/phases-content.ejs +117 -117
  39. package/dashboard/src/views/partials/roadmap-content.ejs +142 -142
  40. package/dashboard/src/views/partials/sidebar.ejs +38 -38
  41. package/dashboard/src/views/partials/todo-create-content.ejs +53 -53
  42. package/dashboard/src/views/partials/todo-detail-content.ejs +38 -38
  43. package/dashboard/src/views/partials/todos-content.ejs +53 -53
  44. package/dashboard/src/views/phase-detail.ejs +5 -5
  45. package/dashboard/src/views/phase-doc.ejs +5 -0
  46. package/dashboard/src/views/phases.ejs +5 -5
  47. package/dashboard/src/views/roadmap.ejs +5 -5
  48. package/dashboard/src/views/todo-create.ejs +5 -5
  49. package/dashboard/src/views/todo-detail.ejs +5 -5
  50. package/dashboard/src/views/todos.ejs +5 -5
  51. package/package.json +57 -57
  52. package/plugins/pbr/.claude-plugin/plugin.json +13 -13
  53. package/plugins/pbr/UI-CONSISTENCY-GAPS.md +61 -61
  54. package/plugins/pbr/agents/codebase-mapper.md +279 -271
  55. package/plugins/pbr/agents/debugger.md +281 -281
  56. package/plugins/pbr/agents/executor.md +428 -407
  57. package/plugins/pbr/agents/general.md +164 -164
  58. package/plugins/pbr/agents/integration-checker.md +169 -141
  59. package/plugins/pbr/agents/plan-checker.md +296 -280
  60. package/plugins/pbr/agents/planner.md +358 -358
  61. package/plugins/pbr/agents/researcher.md +363 -363
  62. package/plugins/pbr/agents/synthesizer.md +230 -230
  63. package/plugins/pbr/agents/verifier.md +489 -454
  64. package/plugins/pbr/commands/begin.md +5 -5
  65. package/plugins/pbr/commands/build.md +5 -5
  66. package/plugins/pbr/commands/config.md +5 -5
  67. package/plugins/pbr/commands/continue.md +5 -5
  68. package/plugins/pbr/commands/debug.md +5 -5
  69. package/plugins/pbr/commands/discuss.md +5 -5
  70. package/plugins/pbr/commands/explore.md +5 -5
  71. package/plugins/pbr/commands/health.md +5 -5
  72. package/plugins/pbr/commands/help.md +5 -5
  73. package/plugins/pbr/commands/import.md +5 -5
  74. package/plugins/pbr/commands/milestone.md +5 -5
  75. package/plugins/pbr/commands/note.md +5 -5
  76. package/plugins/pbr/commands/pause.md +5 -5
  77. package/plugins/pbr/commands/plan.md +5 -5
  78. package/plugins/pbr/commands/quick.md +5 -5
  79. package/plugins/pbr/commands/resume.md +5 -5
  80. package/plugins/pbr/commands/review.md +5 -5
  81. package/plugins/pbr/commands/scan.md +5 -5
  82. package/plugins/pbr/commands/setup.md +5 -5
  83. package/plugins/pbr/commands/status.md +5 -5
  84. package/plugins/pbr/commands/todo.md +5 -5
  85. package/plugins/pbr/contexts/dev.md +27 -27
  86. package/plugins/pbr/contexts/research.md +28 -28
  87. package/plugins/pbr/contexts/review.md +36 -36
  88. package/plugins/pbr/hooks/hooks.json +183 -183
  89. package/plugins/pbr/references/agent-anti-patterns.md +24 -24
  90. package/plugins/pbr/references/agent-interactions.md +134 -134
  91. package/plugins/pbr/references/agent-teams.md +54 -54
  92. package/plugins/pbr/references/checkpoints.md +157 -157
  93. package/plugins/pbr/references/common-bug-patterns.md +13 -13
  94. package/plugins/pbr/references/config-reference.md +441 -0
  95. package/plugins/pbr/references/continuation-format.md +212 -212
  96. package/plugins/pbr/references/deviation-rules.md +112 -112
  97. package/plugins/pbr/references/git-integration.md +226 -226
  98. package/plugins/pbr/references/integration-patterns.md +117 -117
  99. package/plugins/pbr/references/model-profiles.md +99 -99
  100. package/plugins/pbr/references/model-selection.md +31 -31
  101. package/plugins/pbr/references/pbr-rules.md +193 -193
  102. package/plugins/pbr/references/plan-authoring.md +181 -181
  103. package/plugins/pbr/references/plan-format.md +287 -283
  104. package/plugins/pbr/references/planning-config.md +213 -213
  105. package/plugins/pbr/references/questioning.md +214 -214
  106. package/plugins/pbr/references/reading-verification.md +127 -127
  107. package/plugins/pbr/references/stub-patterns.md +160 -160
  108. package/plugins/pbr/references/subagent-coordination.md +119 -119
  109. package/plugins/pbr/references/ui-formatting.md +461 -399
  110. package/plugins/pbr/references/verification-patterns.md +198 -198
  111. package/plugins/pbr/references/wave-execution.md +95 -95
  112. package/plugins/pbr/scripts/auto-continue.js +80 -80
  113. package/plugins/pbr/scripts/check-dangerous-commands.js +136 -136
  114. package/plugins/pbr/scripts/check-doc-sprawl.js +102 -102
  115. package/plugins/pbr/scripts/check-phase-boundary.js +196 -196
  116. package/plugins/pbr/scripts/check-plan-format.js +270 -270
  117. package/plugins/pbr/scripts/check-roadmap-sync.js +322 -252
  118. package/plugins/pbr/scripts/check-skill-workflow.js +262 -262
  119. package/plugins/pbr/scripts/check-state-sync.js +476 -476
  120. package/plugins/pbr/scripts/check-subagent-output.js +144 -144
  121. package/plugins/pbr/scripts/config-schema.json +251 -251
  122. package/plugins/pbr/scripts/context-budget-check.js +287 -287
  123. package/plugins/pbr/scripts/event-handler.js +151 -151
  124. package/plugins/pbr/scripts/event-logger.js +92 -92
  125. package/plugins/pbr/scripts/hook-logger.js +80 -76
  126. package/plugins/pbr/scripts/hooks-schema.json +79 -79
  127. package/plugins/pbr/scripts/log-subagent.js +164 -152
  128. package/plugins/pbr/scripts/log-tool-failure.js +88 -88
  129. package/plugins/pbr/scripts/pbr-tools.js +1378 -1301
  130. package/plugins/pbr/scripts/post-write-dispatch.js +66 -66
  131. package/plugins/pbr/scripts/post-write-quality.js +207 -207
  132. package/plugins/pbr/scripts/pre-bash-dispatch.js +86 -56
  133. package/plugins/pbr/scripts/pre-write-dispatch.js +97 -62
  134. package/plugins/pbr/scripts/progress-tracker.js +281 -228
  135. package/plugins/pbr/scripts/run-hook.js +92 -0
  136. package/plugins/pbr/scripts/session-cleanup.js +254 -254
  137. package/plugins/pbr/scripts/status-line.js +288 -285
  138. package/plugins/pbr/scripts/suggest-compact.js +119 -119
  139. package/plugins/pbr/scripts/task-completed.js +45 -45
  140. package/plugins/pbr/scripts/track-context-budget.js +149 -119
  141. package/plugins/pbr/scripts/validate-commit.js +200 -200
  142. package/plugins/pbr/scripts/validate-plugin-structure.js +183 -172
  143. package/plugins/pbr/scripts/validate-task.js +106 -0
  144. package/plugins/pbr/skills/begin/SKILL.md +594 -545
  145. package/plugins/pbr/skills/begin/templates/PROJECT.md.tmpl +33 -33
  146. package/plugins/pbr/skills/begin/templates/REQUIREMENTS.md.tmpl +18 -18
  147. package/plugins/pbr/skills/begin/templates/STATE.md.tmpl +49 -49
  148. package/plugins/pbr/skills/begin/templates/config.json.tmpl +64 -63
  149. package/plugins/pbr/skills/begin/templates/researcher-prompt.md.tmpl +19 -19
  150. package/plugins/pbr/skills/begin/templates/roadmap-prompt.md.tmpl +30 -30
  151. package/plugins/pbr/skills/begin/templates/synthesis-prompt.md.tmpl +16 -16
  152. package/plugins/pbr/skills/build/SKILL.md +943 -962
  153. package/plugins/pbr/skills/config/SKILL.md +256 -241
  154. package/plugins/pbr/skills/continue/SKILL.md +164 -127
  155. package/plugins/pbr/skills/debug/SKILL.md +515 -489
  156. package/plugins/pbr/skills/debug/templates/continuation-prompt.md.tmpl +16 -16
  157. package/plugins/pbr/skills/debug/templates/initial-investigation-prompt.md.tmpl +27 -27
  158. package/plugins/pbr/skills/discuss/SKILL.md +347 -338
  159. package/plugins/pbr/skills/discuss/templates/CONTEXT.md.tmpl +61 -61
  160. package/plugins/pbr/skills/discuss/templates/decision-categories.md +9 -9
  161. package/plugins/pbr/skills/explore/SKILL.md +378 -362
  162. package/plugins/pbr/skills/health/SKILL.md +221 -186
  163. package/plugins/pbr/skills/health/templates/check-pattern.md.tmpl +30 -30
  164. package/plugins/pbr/skills/health/templates/output-format.md.tmpl +63 -63
  165. package/plugins/pbr/skills/help/SKILL.md +155 -140
  166. package/plugins/pbr/skills/import/SKILL.md +504 -490
  167. package/plugins/pbr/skills/milestone/SKILL.md +704 -673
  168. package/plugins/pbr/skills/milestone/templates/audit-report.md.tmpl +48 -48
  169. package/plugins/pbr/skills/milestone/templates/stats-file.md.tmpl +30 -30
  170. package/plugins/pbr/skills/note/SKILL.md +231 -212
  171. package/plugins/pbr/skills/pause/SKILL.md +249 -235
  172. package/plugins/pbr/skills/pause/templates/continue-here.md.tmpl +71 -71
  173. package/plugins/pbr/skills/plan/SKILL.md +685 -628
  174. package/plugins/pbr/skills/plan/decimal-phase-calc.md +98 -98
  175. package/plugins/pbr/skills/plan/templates/checker-prompt.md.tmpl +21 -21
  176. package/plugins/pbr/skills/plan/templates/gap-closure-prompt.md.tmpl +32 -32
  177. package/plugins/pbr/skills/plan/templates/planner-prompt.md.tmpl +38 -38
  178. package/plugins/pbr/skills/plan/templates/researcher-prompt.md.tmpl +19 -19
  179. package/plugins/pbr/skills/plan/templates/revision-prompt.md.tmpl +23 -23
  180. package/plugins/pbr/skills/quick/SKILL.md +354 -335
  181. package/plugins/pbr/skills/resume/SKILL.md +402 -388
  182. package/plugins/pbr/skills/review/SKILL.md +686 -652
  183. package/plugins/pbr/skills/review/templates/debugger-prompt.md.tmpl +60 -60
  184. package/plugins/pbr/skills/review/templates/gap-planner-prompt.md.tmpl +40 -40
  185. package/plugins/pbr/skills/review/templates/verifier-prompt.md.tmpl +115 -115
  186. package/plugins/pbr/skills/scan/SKILL.md +304 -269
  187. package/plugins/pbr/skills/scan/templates/mapper-prompt.md.tmpl +201 -201
  188. package/plugins/pbr/skills/setup/SKILL.md +253 -227
  189. package/plugins/pbr/skills/shared/commit-planning-docs.md +35 -35
  190. package/plugins/pbr/skills/shared/config-loading.md +102 -102
  191. package/plugins/pbr/skills/shared/context-budget.md +40 -40
  192. package/plugins/pbr/skills/shared/context-loader-task.md +86 -86
  193. package/plugins/pbr/skills/shared/digest-select.md +79 -79
  194. package/plugins/pbr/skills/shared/domain-probes.md +125 -125
  195. package/plugins/pbr/skills/shared/error-reporting.md +79 -79
  196. package/plugins/pbr/skills/shared/gate-prompts.md +388 -388
  197. package/plugins/pbr/skills/shared/phase-argument-parsing.md +45 -45
  198. package/plugins/pbr/skills/shared/progress-display.md +53 -53
  199. package/plugins/pbr/skills/shared/revision-loop.md +81 -81
  200. package/plugins/pbr/skills/shared/state-loading.md +62 -62
  201. package/plugins/pbr/skills/shared/state-update.md +161 -161
  202. package/plugins/pbr/skills/shared/universal-anti-patterns.md +33 -33
  203. package/plugins/pbr/skills/status/SKILL.md +367 -353
  204. package/plugins/pbr/skills/todo/SKILL.md +198 -181
  205. package/plugins/pbr/templates/CONTEXT.md.tmpl +52 -52
  206. package/plugins/pbr/templates/INTEGRATION-REPORT.md.tmpl +151 -151
  207. package/plugins/pbr/templates/RESEARCH-SUMMARY.md.tmpl +97 -97
  208. package/plugins/pbr/templates/ROADMAP.md.tmpl +40 -40
  209. package/plugins/pbr/templates/SUMMARY.md.tmpl +81 -81
  210. package/plugins/pbr/templates/VERIFICATION-DETAIL.md.tmpl +116 -116
  211. package/plugins/pbr/templates/codebase/ARCHITECTURE.md.tmpl +98 -98
  212. package/plugins/pbr/templates/codebase/CONCERNS.md.tmpl +93 -93
  213. package/plugins/pbr/templates/codebase/CONVENTIONS.md.tmpl +104 -104
  214. package/plugins/pbr/templates/codebase/INTEGRATIONS.md.tmpl +78 -78
  215. package/plugins/pbr/templates/codebase/STACK.md.tmpl +78 -78
  216. package/plugins/pbr/templates/codebase/STRUCTURE.md.tmpl +80 -80
  217. package/plugins/pbr/templates/codebase/TESTING.md.tmpl +107 -107
  218. package/plugins/pbr/templates/continue-here.md.tmpl +73 -73
  219. package/plugins/pbr/templates/prompt-partials/phase-project-context.md.tmpl +37 -37
  220. package/plugins/pbr/templates/research/ARCHITECTURE.md.tmpl +124 -124
  221. package/plugins/pbr/templates/research/STACK.md.tmpl +71 -71
  222. package/plugins/pbr/templates/research/SUMMARY.md.tmpl +112 -112
  223. package/plugins/pbr/templates/research-outputs/phase-research.md.tmpl +81 -81
  224. package/plugins/pbr/templates/research-outputs/project-research.md.tmpl +99 -99
  225. package/plugins/pbr/templates/research-outputs/synthesis.md.tmpl +36 -36
@@ -1,254 +1,254 @@
1
- #!/usr/bin/env node
2
-
3
- /**
4
- * SessionEnd cleanup hook.
5
- *
6
- * Removes stale planning artifacts that shouldn't persist across sessions:
7
- * - .planning/.auto-next (prevents confusion on next session start)
8
- * - .planning/.active-operation (stale operation lock)
9
- * - .planning/.active-skill (stale skill tracking)
10
- *
11
- * Additional cleanup:
12
- * - Removes stale .checkpoint-manifest.json files (>24h old)
13
- * - Rotates hooks.jsonl when >200KB (moves to hooks.jsonl.1)
14
- * - Warns about orphaned .PROGRESS-* files (executor crash artifacts)
15
- * - Writes session summary to logs/sessions.jsonl
16
- *
17
- * Logs session end with reason to hook-log.
18
- * Non-blocking — best-effort cleanup, fails silently.
19
- */
20
-
21
- const fs = require('fs');
22
- const path = require('path');
23
- const { logHook } = require('./hook-logger');
24
- const { tailLines } = require('./pbr-tools');
25
-
26
- function readStdin() {
27
- try {
28
- const input = fs.readFileSync(0, 'utf8').trim();
29
- if (input) return JSON.parse(input);
30
- } catch (_e) {
31
- // empty or non-JSON stdin
32
- }
33
- return {};
34
- }
35
-
36
- function tryRemove(filePath) {
37
- try {
38
- if (fs.existsSync(filePath)) {
39
- fs.unlinkSync(filePath);
40
- return true;
41
- }
42
- } catch (_e) {
43
- // best-effort — don't fail the hook
44
- }
45
- return false;
46
- }
47
-
48
- const STALE_CHECKPOINT_MS = 24 * 60 * 60 * 1000; // 24 hours
49
- const MAX_HOOKS_LOG_BYTES = 200 * 1024; // 200KB
50
-
51
- function cleanStaleCheckpoints(planningDir) {
52
- const removed = [];
53
- try {
54
- const phasesDir = path.join(planningDir, 'phases');
55
- if (!fs.existsSync(phasesDir)) return removed;
56
-
57
- const dirs = fs.readdirSync(phasesDir);
58
- for (const dir of dirs) {
59
- const manifestPath = path.join(phasesDir, dir, '.checkpoint-manifest.json');
60
- if (!fs.existsSync(manifestPath)) continue;
61
-
62
- const stat = fs.statSync(manifestPath);
63
- const ageMs = Date.now() - stat.mtimeMs;
64
- if (ageMs > STALE_CHECKPOINT_MS) {
65
- fs.unlinkSync(manifestPath);
66
- removed.push(path.join('phases', dir, '.checkpoint-manifest.json'));
67
- }
68
- }
69
- } catch (_e) {
70
- // best-effort
71
- }
72
- return removed;
73
- }
74
-
75
- function rotateHooksLog(planningDir) {
76
- try {
77
- const logsDir = path.join(planningDir, 'logs');
78
- const hooksLog = path.join(logsDir, 'hooks.jsonl');
79
- if (!fs.existsSync(hooksLog)) return false;
80
-
81
- const stat = fs.statSync(hooksLog);
82
- if (stat.size <= MAX_HOOKS_LOG_BYTES) return false;
83
-
84
- const rotatedPath = hooksLog + '.1';
85
- // Overwrite any existing .1 file
86
- fs.renameSync(hooksLog, rotatedPath);
87
- return true;
88
- } catch (_e) {
89
- return false;
90
- }
91
- }
92
-
93
- function findOrphanedProgressFiles(planningDir) {
94
- const orphans = [];
95
- try {
96
- const phasesDir = path.join(planningDir, 'phases');
97
- if (!fs.existsSync(phasesDir)) return orphans;
98
-
99
- const dirs = fs.readdirSync(phasesDir);
100
- for (const dir of dirs) {
101
- const phaseDir = path.join(phasesDir, dir);
102
- const files = fs.readdirSync(phaseDir);
103
- for (const file of files) {
104
- if (file.startsWith('.PROGRESS-')) {
105
- orphans.push(path.join('phases', dir, file));
106
- }
107
- }
108
- }
109
- } catch (_e) {
110
- // best-effort
111
- }
112
- return orphans;
113
- }
114
-
115
- const MAX_SESSION_ENTRIES = 100;
116
-
117
- function writeSessionHistory(planningDir, data) {
118
- try {
119
- const logsDir = path.join(planningDir, 'logs');
120
- if (!fs.existsSync(logsDir)) {
121
- fs.mkdirSync(logsDir, { recursive: true });
122
- }
123
-
124
- const sessionsFile = path.join(logsDir, 'sessions.jsonl');
125
-
126
- // Mine existing logs for session stats
127
- const hooksLog = path.join(logsDir, 'hooks.jsonl');
128
- const eventsLog = path.join(logsDir, 'events.jsonl');
129
-
130
- let agentsSpawned = 0;
131
- let commitsCreated = 0;
132
- const commandsRun = [];
133
- let sessionStart = null;
134
-
135
- // Count agents from hooks log (SubagentStart entries)
136
- // Hooks log is capped at 200 entries; read last 200 to cover the full session
137
- const hookLines = tailLines(hooksLog, 200);
138
- for (const line of hookLines) {
139
- try {
140
- const entry = JSON.parse(line);
141
- if (entry.event === 'SubagentStart' && entry.decision === 'spawned') {
142
- agentsSpawned++;
143
- }
144
- // Track earliest timestamp as session start
145
- if (entry.ts && (!sessionStart || entry.ts < sessionStart)) {
146
- sessionStart = entry.ts;
147
- }
148
- } catch (_e) { /* skip malformed lines */ }
149
- }
150
-
151
- // Count commits and commands from events log
152
- // Read last 200 entries — sufficient for a single session's events
153
- const eventLines = tailLines(eventsLog, 200);
154
- for (const line of eventLines) {
155
- try {
156
- const entry = JSON.parse(line);
157
- if (entry.event === 'commit-validated' && entry.status === 'allow') {
158
- commitsCreated++;
159
- }
160
- if (entry.cat === 'workflow' && entry.event) {
161
- if (!commandsRun.includes(entry.event)) {
162
- commandsRun.push(entry.event);
163
- }
164
- }
165
- if (entry.ts && (!sessionStart || entry.ts < sessionStart)) {
166
- sessionStart = entry.ts;
167
- }
168
- } catch (_e) { /* skip malformed lines */ }
169
- }
170
-
171
- const sessionEnd = new Date().toISOString();
172
- let durationMinutes = null;
173
- if (sessionStart) {
174
- durationMinutes = Math.round((new Date(sessionEnd) - new Date(sessionStart)) / 60000);
175
- }
176
-
177
- const summary = {
178
- start: sessionStart || sessionEnd,
179
- end: sessionEnd,
180
- duration_minutes: durationMinutes,
181
- reason: data.reason || null,
182
- agents_spawned: agentsSpawned,
183
- commits_created: commitsCreated,
184
- commands_run: commandsRun
185
- };
186
-
187
- // Append to sessions.jsonl, cap at MAX_SESSION_ENTRIES
188
- let lines = [];
189
- if (fs.existsSync(sessionsFile)) {
190
- const content = fs.readFileSync(sessionsFile, 'utf8').trim();
191
- if (content) {
192
- lines = content.split('\n');
193
- }
194
- }
195
- lines.push(JSON.stringify(summary));
196
- if (lines.length > MAX_SESSION_ENTRIES) {
197
- lines = lines.slice(lines.length - MAX_SESSION_ENTRIES);
198
- }
199
- fs.writeFileSync(sessionsFile, lines.join('\n') + '\n', 'utf8');
200
- } catch (_e) {
201
- // Best-effort — don't fail the hook
202
- }
203
- }
204
-
205
- function main() {
206
- const data = readStdin();
207
- const cwd = process.cwd();
208
- const planningDir = path.join(cwd, '.planning');
209
-
210
- if (!fs.existsSync(planningDir)) {
211
- process.exit(0);
212
- }
213
-
214
- const cleaned = [];
215
-
216
- if (tryRemove(path.join(planningDir, '.auto-next'))) {
217
- cleaned.push('.auto-next');
218
- }
219
- if (tryRemove(path.join(planningDir, '.active-operation'))) {
220
- cleaned.push('.active-operation');
221
- }
222
- if (tryRemove(path.join(planningDir, '.active-skill'))) {
223
- cleaned.push('.active-skill');
224
- }
225
- if (tryRemove(path.join(planningDir, '.active-plan'))) {
226
- cleaned.push('.active-plan');
227
- }
228
-
229
- // Clean stale checkpoint manifests (>24h old)
230
- const staleCheckpoints = cleanStaleCheckpoints(planningDir);
231
- cleaned.push(...staleCheckpoints);
232
-
233
- // Rotate hooks.jsonl if >200KB
234
- const rotated = rotateHooksLog(planningDir);
235
-
236
- // Detect orphaned .PROGRESS-* files (executor crash artifacts)
237
- const orphans = findOrphanedProgressFiles(planningDir);
238
-
239
- // Write session history log
240
- writeSessionHistory(planningDir, data);
241
-
242
- const decision = cleaned.length > 0 ? 'cleaned' : 'nothing';
243
- logHook('session-cleanup', 'SessionEnd', decision, {
244
- reason: data.reason || null,
245
- removed: cleaned,
246
- log_rotated: rotated,
247
- orphaned_progress_files: orphans.length > 0 ? orphans : undefined
248
- });
249
-
250
- process.exit(0);
251
- }
252
-
253
- module.exports = { writeSessionHistory, tryRemove, cleanStaleCheckpoints, rotateHooksLog, findOrphanedProgressFiles };
254
- if (require.main === module) { main(); }
1
+ #!/usr/bin/env node
2
+
3
+ /**
4
+ * SessionEnd cleanup hook.
5
+ *
6
+ * Removes stale planning artifacts that shouldn't persist across sessions:
7
+ * - .planning/.auto-next (prevents confusion on next session start)
8
+ * - .planning/.active-operation (stale operation lock)
9
+ * - .planning/.active-skill (stale skill tracking)
10
+ *
11
+ * Additional cleanup:
12
+ * - Removes stale .checkpoint-manifest.json files (>24h old)
13
+ * - Rotates hooks.jsonl when >200KB (moves to hooks.jsonl.1)
14
+ * - Warns about orphaned .PROGRESS-* files (executor crash artifacts)
15
+ * - Writes session summary to logs/sessions.jsonl
16
+ *
17
+ * Logs session end with reason to hook-log.
18
+ * Non-blocking — best-effort cleanup, fails silently.
19
+ */
20
+
21
+ const fs = require('fs');
22
+ const path = require('path');
23
+ const { logHook } = require('./hook-logger');
24
+ const { tailLines } = require('./pbr-tools');
25
+
26
+ function readStdin() {
27
+ try {
28
+ const input = fs.readFileSync(0, 'utf8').trim();
29
+ if (input) return JSON.parse(input);
30
+ } catch (_e) {
31
+ // empty or non-JSON stdin
32
+ }
33
+ return {};
34
+ }
35
+
36
+ function tryRemove(filePath) {
37
+ try {
38
+ if (fs.existsSync(filePath)) {
39
+ fs.unlinkSync(filePath);
40
+ return true;
41
+ }
42
+ } catch (_e) {
43
+ // best-effort — don't fail the hook
44
+ }
45
+ return false;
46
+ }
47
+
48
+ const STALE_CHECKPOINT_MS = 24 * 60 * 60 * 1000; // 24 hours
49
+ const MAX_HOOKS_LOG_BYTES = 200 * 1024; // 200KB
50
+
51
+ function cleanStaleCheckpoints(planningDir) {
52
+ const removed = [];
53
+ try {
54
+ const phasesDir = path.join(planningDir, 'phases');
55
+ if (!fs.existsSync(phasesDir)) return removed;
56
+
57
+ const dirs = fs.readdirSync(phasesDir);
58
+ for (const dir of dirs) {
59
+ const manifestPath = path.join(phasesDir, dir, '.checkpoint-manifest.json');
60
+ if (!fs.existsSync(manifestPath)) continue;
61
+
62
+ const stat = fs.statSync(manifestPath);
63
+ const ageMs = Date.now() - stat.mtimeMs;
64
+ if (ageMs > STALE_CHECKPOINT_MS) {
65
+ fs.unlinkSync(manifestPath);
66
+ removed.push(path.join('phases', dir, '.checkpoint-manifest.json'));
67
+ }
68
+ }
69
+ } catch (_e) {
70
+ // best-effort
71
+ }
72
+ return removed;
73
+ }
74
+
75
+ function rotateHooksLog(planningDir) {
76
+ try {
77
+ const logsDir = path.join(planningDir, 'logs');
78
+ const hooksLog = path.join(logsDir, 'hooks.jsonl');
79
+ if (!fs.existsSync(hooksLog)) return false;
80
+
81
+ const stat = fs.statSync(hooksLog);
82
+ if (stat.size <= MAX_HOOKS_LOG_BYTES) return false;
83
+
84
+ const rotatedPath = hooksLog + '.1';
85
+ // Overwrite any existing .1 file
86
+ fs.renameSync(hooksLog, rotatedPath);
87
+ return true;
88
+ } catch (_e) {
89
+ return false;
90
+ }
91
+ }
92
+
93
+ function findOrphanedProgressFiles(planningDir) {
94
+ const orphans = [];
95
+ try {
96
+ const phasesDir = path.join(planningDir, 'phases');
97
+ if (!fs.existsSync(phasesDir)) return orphans;
98
+
99
+ const dirs = fs.readdirSync(phasesDir);
100
+ for (const dir of dirs) {
101
+ const phaseDir = path.join(phasesDir, dir);
102
+ const files = fs.readdirSync(phaseDir);
103
+ for (const file of files) {
104
+ if (file.startsWith('.PROGRESS-')) {
105
+ orphans.push(path.join('phases', dir, file));
106
+ }
107
+ }
108
+ }
109
+ } catch (_e) {
110
+ // best-effort
111
+ }
112
+ return orphans;
113
+ }
114
+
115
+ const MAX_SESSION_ENTRIES = 100;
116
+
117
+ function writeSessionHistory(planningDir, data) {
118
+ try {
119
+ const logsDir = path.join(planningDir, 'logs');
120
+ if (!fs.existsSync(logsDir)) {
121
+ fs.mkdirSync(logsDir, { recursive: true });
122
+ }
123
+
124
+ const sessionsFile = path.join(logsDir, 'sessions.jsonl');
125
+
126
+ // Mine existing logs for session stats
127
+ const hooksLog = path.join(logsDir, 'hooks.jsonl');
128
+ const eventsLog = path.join(logsDir, 'events.jsonl');
129
+
130
+ let agentsSpawned = 0;
131
+ let commitsCreated = 0;
132
+ const commandsRun = [];
133
+ let sessionStart = null;
134
+
135
+ // Count agents from hooks log (SubagentStart entries)
136
+ // Hooks log is capped at 200 entries; read last 200 to cover the full session
137
+ const hookLines = tailLines(hooksLog, 200);
138
+ for (const line of hookLines) {
139
+ try {
140
+ const entry = JSON.parse(line);
141
+ if (entry.event === 'SubagentStart' && entry.decision === 'spawned') {
142
+ agentsSpawned++;
143
+ }
144
+ // Track earliest timestamp as session start
145
+ if (entry.ts && (!sessionStart || entry.ts < sessionStart)) {
146
+ sessionStart = entry.ts;
147
+ }
148
+ } catch (_e) { /* skip malformed lines */ }
149
+ }
150
+
151
+ // Count commits and commands from events log
152
+ // Read last 200 entries — sufficient for a single session's events
153
+ const eventLines = tailLines(eventsLog, 200);
154
+ for (const line of eventLines) {
155
+ try {
156
+ const entry = JSON.parse(line);
157
+ if (entry.event === 'commit-validated' && entry.status === 'allow') {
158
+ commitsCreated++;
159
+ }
160
+ if (entry.cat === 'workflow' && entry.event) {
161
+ if (!commandsRun.includes(entry.event)) {
162
+ commandsRun.push(entry.event);
163
+ }
164
+ }
165
+ if (entry.ts && (!sessionStart || entry.ts < sessionStart)) {
166
+ sessionStart = entry.ts;
167
+ }
168
+ } catch (_e) { /* skip malformed lines */ }
169
+ }
170
+
171
+ const sessionEnd = new Date().toISOString();
172
+ let durationMinutes = null;
173
+ if (sessionStart) {
174
+ durationMinutes = Math.round((new Date(sessionEnd) - new Date(sessionStart)) / 60000);
175
+ }
176
+
177
+ const summary = {
178
+ start: sessionStart || sessionEnd,
179
+ end: sessionEnd,
180
+ duration_minutes: durationMinutes,
181
+ reason: data.reason || null,
182
+ agents_spawned: agentsSpawned,
183
+ commits_created: commitsCreated,
184
+ commands_run: commandsRun
185
+ };
186
+
187
+ // Append to sessions.jsonl, cap at MAX_SESSION_ENTRIES
188
+ let lines = [];
189
+ if (fs.existsSync(sessionsFile)) {
190
+ const content = fs.readFileSync(sessionsFile, 'utf8').trim();
191
+ if (content) {
192
+ lines = content.split('\n');
193
+ }
194
+ }
195
+ lines.push(JSON.stringify(summary));
196
+ if (lines.length > MAX_SESSION_ENTRIES) {
197
+ lines = lines.slice(lines.length - MAX_SESSION_ENTRIES);
198
+ }
199
+ fs.writeFileSync(sessionsFile, lines.join('\n') + '\n', 'utf8');
200
+ } catch (_e) {
201
+ // Best-effort — don't fail the hook
202
+ }
203
+ }
204
+
205
+ function main() {
206
+ const data = readStdin();
207
+ const cwd = process.cwd();
208
+ const planningDir = path.join(cwd, '.planning');
209
+
210
+ if (!fs.existsSync(planningDir)) {
211
+ process.exit(0);
212
+ }
213
+
214
+ const cleaned = [];
215
+
216
+ if (tryRemove(path.join(planningDir, '.auto-next'))) {
217
+ cleaned.push('.auto-next');
218
+ }
219
+ if (tryRemove(path.join(planningDir, '.active-operation'))) {
220
+ cleaned.push('.active-operation');
221
+ }
222
+ if (tryRemove(path.join(planningDir, '.active-skill'))) {
223
+ cleaned.push('.active-skill');
224
+ }
225
+ if (tryRemove(path.join(planningDir, '.active-plan'))) {
226
+ cleaned.push('.active-plan');
227
+ }
228
+
229
+ // Clean stale checkpoint manifests (>24h old)
230
+ const staleCheckpoints = cleanStaleCheckpoints(planningDir);
231
+ cleaned.push(...staleCheckpoints);
232
+
233
+ // Rotate hooks.jsonl if >200KB
234
+ const rotated = rotateHooksLog(planningDir);
235
+
236
+ // Detect orphaned .PROGRESS-* files (executor crash artifacts)
237
+ const orphans = findOrphanedProgressFiles(planningDir);
238
+
239
+ // Write session history log
240
+ writeSessionHistory(planningDir, data);
241
+
242
+ const decision = cleaned.length > 0 ? 'cleaned' : 'nothing';
243
+ logHook('session-cleanup', 'SessionEnd', decision, {
244
+ reason: data.reason || null,
245
+ removed: cleaned,
246
+ log_rotated: rotated,
247
+ orphaned_progress_files: orphans.length > 0 ? orphans : undefined
248
+ });
249
+
250
+ process.exit(0);
251
+ }
252
+
253
+ module.exports = { writeSessionHistory, tryRemove, cleanStaleCheckpoints, rotateHooksLog, findOrphanedProgressFiles };
254
+ if (require.main === module) { main(); }