pi-spine 1.0.2 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (129) hide show
  1. package/README.md +61 -489
  2. package/bin/get-version.mjs +1 -13
  3. package/bin/spine-batch.mjs +162 -51
  4. package/bin/spine-cli/batch.mjs +3 -3
  5. package/bin/spine-cli/lane-logs.mjs +24 -0
  6. package/bin/spine-cli/scenarios.mjs +298 -0
  7. package/bin/spine-cli/wait.mjs +30 -0
  8. package/bin/spine-cli/watch.mjs +27 -0
  9. package/bin/spine-config.mjs +1 -212
  10. package/bin/spine-doctor.mjs +2 -418
  11. package/bin/spine-init.mjs +1 -398
  12. package/bin/spine-issue.mjs +196 -0
  13. package/bin/spine-journal.mjs +1 -0
  14. package/bin/spine-preflight.mjs +1 -700
  15. package/bin/spine-review-step.mjs +6 -4
  16. package/bin/spine-run.mjs +30 -0
  17. package/bin/spine-status.mjs +19 -1
  18. package/bin/spine-tasks.mjs +112 -13
  19. package/bin/spine-worker-runner.mjs +24 -14
  20. package/bin/spine.mjs +52 -2
  21. package/extensions/spine/worker-tools.ts +14 -12
  22. package/package.json +5 -2
  23. package/skills/create-spine-tasks/SKILL.md +135 -6
  24. package/skills/create-spine-tasks/references/clarify-template.md +73 -0
  25. package/skills/create-spine-tasks/references/contract-template.md +19 -0
  26. package/skills/create-spine-tasks/references/prompt-template.md +2 -0
  27. package/skills/create-spine-tasks/references/requirements-checklist-template.md +94 -0
  28. package/src/batch/abort.mjs +3 -12
  29. package/src/batch/agent-session-worker.mjs +45 -5
  30. package/src/batch/attached-engine-handoff.mjs +188 -0
  31. package/src/batch/attached-runner.mjs +312 -0
  32. package/src/batch/contract-prelanded.mjs +180 -0
  33. package/src/batch/contract-verify.mjs +140 -2
  34. package/src/batch/detached-start.mjs +105 -7
  35. package/src/batch/diagnosis-alternatives.mjs +46 -0
  36. package/src/batch/diagnosis-merge-failure.mjs +79 -0
  37. package/src/batch/diagnosis-retry-limbo.mjs +34 -0
  38. package/src/batch/diagnosis-stub.mjs +109 -0
  39. package/src/batch/diagnosis-task-done.mjs +110 -0
  40. package/src/batch/diagnosis-worker-done-missing.mjs +96 -0
  41. package/src/batch/diagnosis.mjs +174 -62
  42. package/src/batch/engine-lanes/merge.mjs +168 -77
  43. package/src/batch/engine-lanes/queue.mjs +4 -1
  44. package/src/batch/engine-lanes/review.mjs +32 -74
  45. package/src/batch/engine-lanes/watch.mjs +29 -0
  46. package/src/batch/engine-lanes.mjs +1 -0
  47. package/src/batch/engine-scope.mjs +42 -5
  48. package/src/batch/engine.mjs +115 -68
  49. package/src/batch/evidence.mjs +81 -17
  50. package/src/batch/gate.mjs +46 -17
  51. package/src/batch/git-helpers.mjs +96 -0
  52. package/src/batch/heartbeat.mjs +73 -7
  53. package/src/batch/integrate.mjs +3 -5
  54. package/src/batch/journal.mjs +25 -0
  55. package/src/batch/lane-commit.mjs +90 -5
  56. package/src/batch/lifecycle.mjs +131 -4
  57. package/src/batch/macro-phase.mjs +226 -0
  58. package/src/batch/merge/adoption-doc-merge.mjs +243 -0
  59. package/src/batch/merge/wave-merge-state.mjs +108 -0
  60. package/src/batch/metrics.mjs +74 -5
  61. package/src/batch/orphan-detect.mjs +74 -0
  62. package/src/batch/pause.mjs +223 -0
  63. package/src/batch/post-merge-limbo.mjs +345 -17
  64. package/src/batch/reconcile.mjs +473 -50
  65. package/src/batch/resume-common.mjs +6 -2
  66. package/src/batch/resume-engine.mjs +58 -1
  67. package/src/batch/resume-lane-reviews.mjs +16 -0
  68. package/src/batch/resume-multi-lanes.mjs +56 -1
  69. package/src/batch/resume-multi-validate.mjs +148 -8
  70. package/src/batch/resume-multi.mjs +45 -44
  71. package/src/batch/resume.mjs +64 -47
  72. package/src/batch/retry.mjs +56 -5
  73. package/src/batch/review-shared.mjs +196 -0
  74. package/src/batch/review-spawn.mjs +326 -0
  75. package/src/batch/review.mjs +178 -212
  76. package/src/batch/rules-manifest-drift.mjs +157 -0
  77. package/src/batch/salvage.mjs +2 -1
  78. package/src/batch/sequence.mjs +454 -0
  79. package/src/batch/state.mjs +18 -3
  80. package/src/batch/status-json.mjs +67 -0
  81. package/src/batch/task-stall-budget.mjs +94 -10
  82. package/src/batch/worker-host.mjs +185 -37
  83. package/src/batch/worker-output.mjs +196 -3
  84. package/src/batch/worktree.mjs +10 -0
  85. package/src/cli/handoff.mjs +1 -1
  86. package/src/cli/issue-draft.mjs +180 -0
  87. package/src/cli/journal-follow.mjs +239 -0
  88. package/src/cli/lane-logs.mjs +291 -0
  89. package/src/cli/rules.mjs +1 -1
  90. package/src/cli/sequence.mjs +108 -0
  91. package/src/cli/settings-set.mjs +3 -22
  92. package/src/cli/settings-slash.mjs +27 -5
  93. package/src/cli/wait.mjs +187 -0
  94. package/src/cli/watch.mjs +160 -0
  95. package/src/config/agent-model-resolve.mjs +48 -0
  96. package/src/config/cursor-rules/discover.mjs +3 -21
  97. package/src/config/defaults.mjs +7 -1
  98. package/src/config/settings-fields.mjs +48 -0
  99. package/src/config/spine-config-load.mjs +221 -0
  100. package/src/config/spine-init-constants.mjs +424 -0
  101. package/src/config/spine-preflight-lib.mjs +1180 -0
  102. package/src/dashboard/lane-throughput.mjs +351 -0
  103. package/src/dashboard/public/dashboard.css +25 -1
  104. package/src/dashboard/public/dashboard.js +224 -40
  105. package/src/dashboard/public/index.html +15 -9
  106. package/src/dashboard/server.mjs +4 -0
  107. package/src/dashboard/snapshot.mjs +387 -6
  108. package/src/dashboard/view.mjs +170 -15
  109. package/src/doctor/agent-model-inherit.mjs +61 -6
  110. package/src/doctor/run-doctor-checks.mjs +483 -0
  111. package/src/doctor/sequence-safety.mjs +57 -0
  112. package/src/doctor/stale-path.mjs +22 -0
  113. package/src/doctor/stale-worktrees.mjs +102 -0
  114. package/src/fixtures/scenario-registry.mjs +194 -0
  115. package/src/fs/atomic-write.mjs +49 -0
  116. package/src/migrate/taskplane-config.mjs +1 -1
  117. package/src/planner/file-scope.mjs +246 -0
  118. package/src/planner/format-plan.mjs +9 -7
  119. package/src/planner/index.mjs +6 -2
  120. package/src/planner/pending.mjs +92 -1
  121. package/src/planner/wave-scope.mjs +136 -0
  122. package/src/planner/waves.mjs +133 -0
  123. package/src/tasks/analyze/index.mjs +453 -0
  124. package/src/tasks/packet/index.mjs +1 -0
  125. package/src/tasks/packet/parse-prompt.mjs +42 -1
  126. package/src/tasks/packet/validate-prompt.mjs +124 -0
  127. package/src/util/command-exists.mjs +19 -0
  128. package/templates/docs/constitution.md +61 -0
  129. package/templates/spine-config.json +9 -2
@@ -0,0 +1,351 @@
1
+ /**
2
+ * Per-lane throughput stats derivation (Phase 42 — task-based, not token-based).
3
+ *
4
+ * Derives active elapsed time, terminal task counts, and tasks/hour from batch-state
5
+ * lanes plus journal events, optionally refined by run-metrics task records.
6
+ */
7
+
8
+ const MS_PER_HOUR = 3_600_000;
9
+
10
+ /**
11
+ * @typedef {object} LaneThroughputStats
12
+ * @property {number} activeElapsedMs
13
+ * @property {number} completedCount
14
+ * @property {number} failedCount
15
+ * @property {number} throughputTasksPerHour
16
+ */
17
+
18
+ /**
19
+ * @returns {LaneThroughputStats}
20
+ */
21
+ export function emptyLaneThroughputStats() {
22
+ return {
23
+ activeElapsedMs: 0,
24
+ completedCount: 0,
25
+ failedCount: 0,
26
+ throughputTasksPerHour: 0,
27
+ };
28
+ }
29
+
30
+ /**
31
+ * @param {number} laneNumber
32
+ * @param {object} event
33
+ */
34
+ function laneEventMatches(laneNumber, event) {
35
+ const laneId = `lane-${laneNumber}`;
36
+ const payload = event.payload && typeof event.payload === "object" ? event.payload : {};
37
+ if (payload.laneNumber != null && Number(payload.laneNumber) !== laneNumber) return false;
38
+ if (event.laneId && event.laneId !== laneId) return false;
39
+ return true;
40
+ }
41
+
42
+ /**
43
+ * @param {object} event
44
+ * @returns {string|null}
45
+ */
46
+ function journalEventTaskId(event) {
47
+ if (typeof event.taskId === "string" && event.taskId) return event.taskId;
48
+ const payload = event.payload && typeof event.payload === "object" ? event.payload : {};
49
+ return typeof payload.taskId === "string" ? payload.taskId : null;
50
+ }
51
+
52
+ /**
53
+ * @param {object} event
54
+ * @returns {number|null}
55
+ */
56
+ function journalEventTimestampMs(event) {
57
+ if (event.timestamp == null) return null;
58
+ const ts =
59
+ typeof event.timestamp === "number"
60
+ ? event.timestamp
61
+ : Date.parse(String(event.timestamp));
62
+ return Number.isNaN(ts) ? null : ts;
63
+ }
64
+
65
+ /**
66
+ * @param {number} completedCount
67
+ * @param {number} activeElapsedMs
68
+ * @returns {number}
69
+ */
70
+ export function computeThroughputTasksPerHour(completedCount, activeElapsedMs) {
71
+ if (completedCount <= 0 || activeElapsedMs <= 0) return 0;
72
+ return (completedCount * MS_PER_HOUR) / activeElapsedMs;
73
+ }
74
+
75
+ /**
76
+ * @param {unknown[]} tasks
77
+ * @returns {Map<string, number>}
78
+ */
79
+ function buildTaskLaneIndex(tasks) {
80
+ /** @type {Map<string, number>} */
81
+ const index = new Map();
82
+ for (const task of tasks ?? []) {
83
+ if (!task || typeof task !== "object") continue;
84
+ const taskId = /** @type {{ taskId?: string }} */ (task).taskId;
85
+ const laneNumber = Number(/** @type {{ laneNumber?: number }} */ (task).laneNumber);
86
+ if (!taskId || !Number.isFinite(laneNumber) || laneNumber <= 0) continue;
87
+ index.set(String(taskId), laneNumber);
88
+ }
89
+ return index;
90
+ }
91
+
92
+ /**
93
+ * @param {object} params
94
+ * @param {number} params.laneNumber
95
+ * @param {Set<string>} params.laneTaskIds
96
+ * @param {Map<string, number>} params.taskLaneById
97
+ * @param {object} event
98
+ */
99
+ function eventBelongsToLane({ laneNumber, laneTaskIds, taskLaneById }, event) {
100
+ if (laneEventMatches(laneNumber, event)) return true;
101
+ const taskId = journalEventTaskId(event);
102
+ if (!taskId) return false;
103
+ if (laneTaskIds.has(taskId)) return true;
104
+ return taskLaneById.get(taskId) === laneNumber;
105
+ }
106
+
107
+ /**
108
+ * @param {object} params
109
+ * @param {number} params.laneNumber
110
+ * @param {object[]} params.journalEvents
111
+ * @param {Set<string>} params.laneTaskIds
112
+ * @param {Map<string, number>} params.taskLaneById
113
+ * @param {number} params.now
114
+ */
115
+ function deriveCountsAndJournalElapsed({
116
+ laneNumber,
117
+ journalEvents,
118
+ laneTaskIds,
119
+ taskLaneById,
120
+ now,
121
+ }) {
122
+ let completedCount = 0;
123
+ let failedCount = 0;
124
+ let activeElapsedMs = 0;
125
+
126
+ /** @type {Map<string, number>} */
127
+ const openStartedAt = new Map();
128
+
129
+ for (const event of journalEvents ?? []) {
130
+ if (!eventBelongsToLane({ laneNumber, laneTaskIds, taskLaneById }, event)) continue;
131
+
132
+ const taskId = journalEventTaskId(event);
133
+ const timestampMs = journalEventTimestampMs(event);
134
+ if (timestampMs == null) continue;
135
+
136
+ if (event.type === "task.started" && taskId) {
137
+ openStartedAt.set(taskId, timestampMs);
138
+ continue;
139
+ }
140
+
141
+ if (event.type === "task.completed") {
142
+ completedCount += 1;
143
+ if (taskId && openStartedAt.has(taskId)) {
144
+ activeElapsedMs += Math.max(0, timestampMs - openStartedAt.get(taskId));
145
+ openStartedAt.delete(taskId);
146
+ }
147
+ continue;
148
+ }
149
+
150
+ if (event.type === "task.failed") {
151
+ failedCount += 1;
152
+ if (taskId && openStartedAt.has(taskId)) {
153
+ activeElapsedMs += Math.max(0, timestampMs - openStartedAt.get(taskId));
154
+ openStartedAt.delete(taskId);
155
+ }
156
+ }
157
+ }
158
+
159
+ for (const startedAt of openStartedAt.values()) {
160
+ activeElapsedMs += Math.max(0, now - startedAt);
161
+ }
162
+
163
+ return { completedCount, failedCount, activeElapsedMs };
164
+ }
165
+
166
+ /**
167
+ * @param {object[]} metricsLines
168
+ * @param {number} laneNumber
169
+ * @returns {{ elapsedMs: number, hasRecords: boolean }}
170
+ */
171
+ function sumMetricsElapsedForLane(metricsLines, laneNumber) {
172
+ let elapsedMs = 0;
173
+ let hasRecords = false;
174
+
175
+ for (const line of metricsLines ?? []) {
176
+ if (!line || typeof line !== "object") continue;
177
+ if (line.recordType !== "task") continue;
178
+ if (Number(line.laneNumber) !== laneNumber) continue;
179
+ hasRecords = true;
180
+ if (Number.isFinite(line.durationMs)) {
181
+ elapsedMs += Math.max(0, Number(line.durationMs));
182
+ }
183
+ }
184
+
185
+ return { elapsedMs, hasRecords };
186
+ }
187
+
188
+ /**
189
+ * @param {object} params
190
+ * @param {object} params.lane
191
+ * @param {object[]} [params.journalEvents]
192
+ * @param {object[]} [params.metricsLines]
193
+ * @param {unknown[]} [params.tasks]
194
+ * @param {number} [params.now]
195
+ * @returns {LaneThroughputStats}
196
+ */
197
+ export function deriveLaneThroughputStats({
198
+ lane,
199
+ journalEvents = [],
200
+ metricsLines = [],
201
+ tasks = [],
202
+ now = Date.now(),
203
+ }) {
204
+ if (!lane || typeof lane !== "object") return emptyLaneThroughputStats();
205
+
206
+ const laneNumber = Number(/** @type {{ laneNumber?: number }} */ (lane).laneNumber);
207
+ if (!Number.isFinite(laneNumber) || laneNumber <= 0) return emptyLaneThroughputStats();
208
+
209
+ const laneTaskIds = new Set(
210
+ (Array.isArray(/** @type {{ taskIds?: string[] }} */ (lane).taskIds)
211
+ ? /** @type {{ taskIds?: string[] }} */ (lane).taskIds
212
+ : []
213
+ ).map(String),
214
+ );
215
+ const taskLaneById = buildTaskLaneIndex(tasks);
216
+
217
+ const journalDerived = deriveCountsAndJournalElapsed({
218
+ laneNumber,
219
+ journalEvents,
220
+ laneTaskIds,
221
+ taskLaneById,
222
+ now,
223
+ });
224
+
225
+ const metricsDerived = sumMetricsElapsedForLane(metricsLines, laneNumber);
226
+ const activeElapsedMs = metricsDerived.hasRecords
227
+ ? metricsDerived.elapsedMs
228
+ : journalDerived.activeElapsedMs;
229
+
230
+ return {
231
+ activeElapsedMs,
232
+ completedCount: journalDerived.completedCount,
233
+ failedCount: journalDerived.failedCount,
234
+ throughputTasksPerHour: computeThroughputTasksPerHour(
235
+ journalDerived.completedCount,
236
+ activeElapsedMs,
237
+ ),
238
+ };
239
+ }
240
+
241
+ /**
242
+ * @param {object} params
243
+ * @param {object[]} [params.lanes]
244
+ * @param {object[]} [params.journalEvents]
245
+ * @param {object[]} [params.metricsLines]
246
+ * @param {unknown[]} [params.tasks]
247
+ * @param {number} [params.now]
248
+ * @returns {Map<number, LaneThroughputStats>}
249
+ */
250
+ export function deriveLanesThroughput({
251
+ lanes = [],
252
+ journalEvents = [],
253
+ metricsLines = [],
254
+ tasks = [],
255
+ now = Date.now(),
256
+ }) {
257
+ /** @type {Map<number, LaneThroughputStats>} */
258
+ const statsByLane = new Map();
259
+
260
+ for (const lane of lanes ?? []) {
261
+ if (!lane || typeof lane !== "object") continue;
262
+ const laneNumber = Number(/** @type {{ laneNumber?: number }} */ (lane).laneNumber);
263
+ if (!Number.isFinite(laneNumber) || laneNumber <= 0) continue;
264
+ statsByLane.set(
265
+ laneNumber,
266
+ deriveLaneThroughputStats({
267
+ lane,
268
+ journalEvents,
269
+ metricsLines,
270
+ tasks,
271
+ now,
272
+ }),
273
+ );
274
+ }
275
+
276
+ return statsByLane;
277
+ }
278
+
279
+ /**
280
+ * @param {number} value
281
+ * @returns {number}
282
+ */
283
+ export function roundThroughputRate(value) {
284
+ const num = Number(value);
285
+ if (!Number.isFinite(num)) {
286
+ return 0;
287
+ }
288
+ return Math.round(num * 10) / 10;
289
+ }
290
+
291
+ /**
292
+ * @param {number} ms
293
+ * @returns {string}
294
+ */
295
+ export function formatElapsedMs(ms) {
296
+ const value = Number(ms);
297
+ if (!Number.isFinite(value) || value <= 0) {
298
+ return "—";
299
+ }
300
+
301
+ const totalMinutes = Math.floor(value / 60000);
302
+ const hours = Math.floor(totalMinutes / 60);
303
+ const minutes = totalMinutes % 60;
304
+
305
+ if (hours === 0) {
306
+ return `${minutes}m`;
307
+ }
308
+ if (minutes === 0) {
309
+ return `${hours}h`;
310
+ }
311
+ return `${hours}h ${minutes}m`;
312
+ }
313
+
314
+ /**
315
+ * @param {number} rate
316
+ * @returns {string}
317
+ */
318
+ export function formatThroughputRate(rate) {
319
+ const value = Number(rate);
320
+ if (!Number.isFinite(value) || value < 0) {
321
+ return "—";
322
+ }
323
+ return value.toFixed(1);
324
+ }
325
+
326
+ /**
327
+ * Aggregate per-lane throughput stats into a batch-level summary row.
328
+ *
329
+ * @param {Map<number, LaneThroughputStats>} statsByLane
330
+ * @returns {LaneThroughputStats}
331
+ */
332
+ export function summarizeLaneThroughput(statsByLane) {
333
+ let activeElapsedMs = 0;
334
+ let completedCount = 0;
335
+ let failedCount = 0;
336
+
337
+ for (const stats of statsByLane?.values?.() ?? []) {
338
+ activeElapsedMs += stats.activeElapsedMs ?? 0;
339
+ completedCount += stats.completedCount ?? 0;
340
+ failedCount += stats.failedCount ?? 0;
341
+ }
342
+
343
+ return {
344
+ activeElapsedMs,
345
+ completedCount,
346
+ failedCount,
347
+ throughputTasksPerHour: roundThroughputRate(
348
+ computeThroughputTasksPerHour(completedCount, activeElapsedMs),
349
+ ),
350
+ };
351
+ }
@@ -60,8 +60,32 @@ body { margin: 0; min-height: 100vh; background: var(--bg); color: var(--text);
60
60
  .kv-grid dd { margin: 0; font-family: var(--font-mono); font-size: 0.85rem; }
61
61
  .wave-summary { font-family: var(--font-mono); }
62
62
  .wave-list, .journal-list { margin: 0; padding-left: 1.25rem; font-family: var(--font-mono); font-size: 0.85rem; }
63
+ .journal-header { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 0.75rem; margin-bottom: 0.75rem; }
64
+ .journal-header h3 { margin: 0; }
65
+ .journal-filter-label { display: inline-flex; align-items: center; gap: 0.5rem; color: var(--muted); font-size: 0.8rem; }
66
+ .journal-lane-filter { background: #21262d; color: var(--text); border: 1px solid var(--border); border-radius: 4px; padding: 0.2rem 0.4rem; font-family: var(--font-mono); font-size: 0.8rem; }
67
+ .lane-table tbody tr.lane-row { cursor: pointer; }
68
+ .lane-table tbody tr.lane-row:hover { background: rgba(88,166,255,0.06); }
69
+ .lane-table tbody tr.lane-row-expanded { background: rgba(88,166,255,0.08); }
70
+ .lane-detail-row td { padding: 0.75rem 0.5rem; background: #0d1117; border-bottom: 1px solid var(--border); vertical-align: top; }
71
+ .lane-detail-panel { display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); gap: 1rem; }
72
+ .lane-detail-section h4 { margin: 0 0 0.5rem; font-size: 0.75rem; color: var(--muted); text-transform: uppercase; }
73
+ .lane-detail-events, .lane-detail-log { margin: 0; padding-left: 1.1rem; font-family: var(--font-mono); font-size: 0.8rem; }
74
+ .lane-detail-log-ref { margin: 0 0 0.35rem; color: var(--muted); font-family: var(--font-mono); font-size: 0.75rem; }
75
+ .lane-detail-log pre { margin: 0; white-space: pre-wrap; word-break: break-word; font-family: var(--font-mono); font-size: 0.8rem; color: var(--text); }
63
76
  .wave-list li[data-status="active"] { color: var(--accent); }
64
- .lane-table { width: 100%; border-collapse: collapse; font-size: 0.85rem; }
77
+ .lane-table { width: 100%; border-collapse: collapse; font-size: 0.85rem; min-width: 920px; }
78
+ .table-wrap { overflow-x: auto; }
79
+ .lane-task-running { color: var(--running); font-weight: 600; }
80
+ .lane-task-queued { color: var(--text); }
81
+ @media (max-width: 720px) {
82
+ .lane-table th.col-running,
83
+ .lane-table th.col-queued,
84
+ .lane-table td.col-running,
85
+ .lane-table td.col-queued {
86
+ font-size: 0.8rem;
87
+ }
88
+ }
65
89
  .lane-table th, .lane-table td { padding: 0.4rem 0.5rem; border-bottom: 1px solid var(--border); text-align: left; }
66
90
  .lane-table th { color: var(--muted); }
67
91
  .lane-table td { font-family: var(--font-mono); }