pi-spine 1.2.0 → 1.4.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 (76) hide show
  1. package/README.md +4 -4
  2. package/bin/spine-cli/verify.mjs +6 -0
  3. package/bin/spine-deps.mjs +0 -2
  4. package/bin/spine-plan.mjs +49 -1
  5. package/bin/spine-rules.mjs +0 -2
  6. package/bin/spine-settings.mjs +0 -2
  7. package/bin/spine-worker-runner.mjs +136 -4
  8. package/bin/spine.mjs +2 -5
  9. package/package.json +2 -2
  10. package/skills/create-spine-tasks/SKILL.md +7 -2
  11. package/skills/create-spine-tasks/references/contract-template.md +95 -3
  12. package/src/batch/attached-engine-handoff.mjs +0 -1
  13. package/src/batch/attached-runner.mjs +81 -5
  14. package/src/batch/contract-stub-delivery.mjs +110 -0
  15. package/src/batch/contract-task-start.mjs +204 -0
  16. package/src/batch/contract-verify.mjs +160 -16
  17. package/src/batch/detached-start.mjs +72 -2
  18. package/src/batch/diagnosis-launch-failure.mjs +114 -0
  19. package/src/batch/diagnosis-primary-failure.mjs +89 -0
  20. package/src/batch/diagnosis-retry-limbo.mjs +1 -1
  21. package/src/batch/diagnosis-tail-state.mjs +57 -0
  22. package/src/batch/diagnosis.mjs +22 -114
  23. package/src/batch/engine-lanes/commit.mjs +145 -0
  24. package/src/batch/engine-lanes/merge.mjs +1 -2
  25. package/src/batch/engine-lanes/review.mjs +124 -121
  26. package/src/batch/engine-lanes.mjs +21 -81
  27. package/src/batch/engine.mjs +46 -1
  28. package/src/batch/evidence.mjs +1 -2
  29. package/src/batch/git-exec.mjs +5 -2
  30. package/src/batch/heartbeat.mjs +2 -2
  31. package/src/batch/integrate-assert.mjs +69 -0
  32. package/src/batch/integrate-worktree.mjs +354 -0
  33. package/src/batch/integrate.mjs +195 -102
  34. package/src/batch/journal-rebuild.mjs +158 -3
  35. package/src/batch/journal.mjs +63 -0
  36. package/src/batch/lane-commit.mjs +6 -5
  37. package/src/batch/lane-dirty-check.mjs +513 -0
  38. package/src/batch/limbo-detect.mjs +34 -0
  39. package/src/batch/macro-phase.mjs +76 -0
  40. package/src/batch/metrics.mjs +2 -0
  41. package/src/batch/pause.mjs +18 -7
  42. package/src/batch/post-merge-limbo.mjs +3 -28
  43. package/src/batch/reconcile.mjs +79 -10
  44. package/src/batch/resume-common.mjs +27 -0
  45. package/src/batch/resume-multi-lanes.mjs +11 -0
  46. package/src/batch/resume-multi.mjs +3 -1
  47. package/src/batch/resume.mjs +8 -4
  48. package/src/batch/retry.mjs +41 -10
  49. package/src/batch/review-spawn.mjs +1 -1
  50. package/src/batch/review.mjs +0 -4
  51. package/src/batch/rules-manifest-drift.mjs +43 -2
  52. package/src/batch/sequence-state.mjs +290 -0
  53. package/src/batch/sequence.mjs +113 -51
  54. package/src/batch/state.mjs +16 -0
  55. package/src/batch/worker-host.mjs +14 -5
  56. package/src/cli/deps.mjs +0 -2
  57. package/src/cli/sequence.mjs +14 -1
  58. package/src/cli/settings-set.mjs +33 -3
  59. package/src/config/contract.mjs +10 -0
  60. package/src/config/model-id.mjs +103 -0
  61. package/src/config/spine-init-constants.mjs +1 -0
  62. package/src/config/spine-preflight-lib.mjs +23 -1
  63. package/src/dashboard/public/dashboard.css +8 -0
  64. package/src/dashboard/public/dashboard.js +36 -9
  65. package/src/dashboard/running-tail-state.mjs +31 -0
  66. package/src/dashboard/server.mjs +4 -0
  67. package/src/dashboard/snapshot.mjs +260 -29
  68. package/src/dashboard/view.mjs +112 -2
  69. package/src/doctor/agent-models.mjs +53 -0
  70. package/src/doctor/batch-size-guidance.mjs +30 -2
  71. package/src/doctor/pi-extension-conflict.mjs +169 -0
  72. package/src/doctor/run-doctor-checks.mjs +6 -0
  73. package/src/planner/scope.mjs +5 -1
  74. package/src/tasks/packet/parse-prompt.mjs +31 -1
  75. package/src/tasks/packet/validate-contract.mjs +108 -0
  76. package/src/tasks/packet/validate-prompt.mjs +0 -1
@@ -0,0 +1,290 @@
1
+ /**
2
+ * Planner wave sequence runtime state (GitHub #54 Tier 2 SP-D).
3
+ * Persists progress under `.spine/runtime/sequence/` for `--resume`.
4
+ */
5
+
6
+ import fs from "node:fs";
7
+ import path from "node:path";
8
+ import { writeJsonAtomic } from "../fs/atomic-write.mjs";
9
+
10
+ export const SEQUENCE_STATE_VERSION = 1;
11
+ export const SEQUENCE_STATE_REL = path.join(".spine", "runtime", "sequence", "state.json");
12
+
13
+ /** @typedef {{ waveIndex: number, batchId: string|null, diagnosis: string|null }} SequenceCompletedWave */
14
+
15
+ /**
16
+ * @param {string} projectRoot
17
+ */
18
+ export function sequenceStatePath(projectRoot) {
19
+ return path.join(projectRoot, SEQUENCE_STATE_REL);
20
+ }
21
+
22
+ /**
23
+ * @param {Date} [now]
24
+ */
25
+ export function generateSequenceId(now = new Date()) {
26
+ const y = now.getUTCFullYear();
27
+ const m = String(now.getUTCMonth() + 1).padStart(2, "0");
28
+ const d = String(now.getUTCDate()).padStart(2, "0");
29
+ const h = String(now.getUTCHours()).padStart(2, "0");
30
+ const min = String(now.getUTCMinutes()).padStart(2, "0");
31
+ const s = String(now.getUTCSeconds()).padStart(2, "0");
32
+ return `seq-${y}${m}${d}T${h}${min}${s}`;
33
+ }
34
+
35
+ /**
36
+ * @param {object} params
37
+ * @param {string} params.scope
38
+ * @param {number} params.fromWave
39
+ * @param {number|null} [params.throughWave]
40
+ */
41
+ export function createInitialSequenceState({ scope, fromWave, throughWave = null }) {
42
+ return {
43
+ version: SEQUENCE_STATE_VERSION,
44
+ sequenceId: generateSequenceId(),
45
+ scope,
46
+ fromWave,
47
+ throughWave,
48
+ completedWaves: /** @type {SequenceCompletedWave[]} */ ([]),
49
+ lastBatchId: null,
50
+ status: "active",
51
+ updatedAt: new Date().toISOString(),
52
+ };
53
+ }
54
+
55
+ /**
56
+ * @param {string} projectRoot
57
+ * @returns {{ ok: true, state: object } | { ok: false, error: string, output?: string }}
58
+ */
59
+ export function loadSequenceState(projectRoot) {
60
+ const filePath = sequenceStatePath(projectRoot);
61
+ if (!fs.existsSync(filePath)) {
62
+ return {
63
+ ok: false,
64
+ error: "no_sequence_state",
65
+ output: "No saved sequence state — start a sequence or pass --from-wave.\n",
66
+ };
67
+ }
68
+
69
+ try {
70
+ const state = JSON.parse(fs.readFileSync(filePath, "utf-8"));
71
+ return { ok: true, state };
72
+ } catch (err) {
73
+ return {
74
+ ok: false,
75
+ error: "sequence_state_corrupt",
76
+ output: `Sequence state is unreadable: ${err instanceof Error ? err.message : String(err)}\n`,
77
+ };
78
+ }
79
+ }
80
+
81
+ /**
82
+ * @param {string} projectRoot
83
+ * @param {object} state
84
+ */
85
+ export function saveSequenceState(projectRoot, state) {
86
+ const filePath = sequenceStatePath(projectRoot);
87
+ writeJsonAtomic(filePath, {
88
+ ...state,
89
+ version: SEQUENCE_STATE_VERSION,
90
+ updatedAt: new Date().toISOString(),
91
+ });
92
+ }
93
+
94
+ /**
95
+ * @param {string} projectRoot
96
+ */
97
+ export function clearSequenceState(projectRoot) {
98
+ const filePath = sequenceStatePath(projectRoot);
99
+ if (fs.existsSync(filePath)) {
100
+ fs.unlinkSync(filePath);
101
+ }
102
+ }
103
+
104
+ /**
105
+ * @param {object|null|undefined} state
106
+ */
107
+ export function resolveResumeFromWave(state) {
108
+ const completed = state?.completedWaves ?? [];
109
+ if (completed.length === 0) {
110
+ return Number.isInteger(state?.fromWave) ? state.fromWave : 0;
111
+ }
112
+ const maxCompleted = Math.max(...completed.map((wave) => wave.waveIndex));
113
+ return maxCompleted + 1;
114
+ }
115
+
116
+ /**
117
+ * @param {object} state
118
+ * @param {string} scope
119
+ */
120
+ export function validateSequenceStateForResume(state, scope) {
121
+ if (!state || typeof state !== "object") {
122
+ return { ok: false, error: "no_sequence_state", output: "No saved sequence state.\n" };
123
+ }
124
+ if (state.status === "completed") {
125
+ return {
126
+ ok: false,
127
+ error: "sequence_already_completed",
128
+ output: "Saved sequence already completed — start a new sequence without --resume.\n",
129
+ };
130
+ }
131
+ if (String(state.scope ?? "") !== String(scope)) {
132
+ return {
133
+ ok: false,
134
+ error: "sequence_scope_mismatch",
135
+ output: `Saved sequence scope is "${state.scope ?? ""}" but command scope is "${scope}".\n`,
136
+ };
137
+ }
138
+ return { ok: true };
139
+ }
140
+
141
+ /**
142
+ * @param {object} state
143
+ * @param {SequenceCompletedWave[]} completedWaves
144
+ * @param {string|null} lastBatchId
145
+ * @param {"active"|"halted"|"completed"} status
146
+ */
147
+ export function buildSequenceStateSnapshot(state, { completedWaves, lastBatchId, status }) {
148
+ return {
149
+ ...state,
150
+ completedWaves,
151
+ lastBatchId,
152
+ status,
153
+ };
154
+ }
155
+
156
+ /**
157
+ * @param {object} params
158
+ * @param {number} params.waveIndex
159
+ * @param {string} [params.error]
160
+ * @param {string|null} [params.diagnosis]
161
+ * @param {string|null} [params.batchId]
162
+ * @param {SequenceCompletedWave[]} params.completedWaves
163
+ */
164
+ export function buildSequenceHaltResult({
165
+ waveIndex,
166
+ error,
167
+ diagnosis = null,
168
+ batchId = null,
169
+ completedWaves,
170
+ }) {
171
+ return {
172
+ ok: false,
173
+ exitCode: 1,
174
+ halted: true,
175
+ waveIndex,
176
+ error,
177
+ diagnosis,
178
+ batchId,
179
+ completedWaves,
180
+ };
181
+ }
182
+
183
+ /**
184
+ * @param {string} projectRoot
185
+ * @param {object|null} sequenceState
186
+ * @param {SequenceCompletedWave[]} completedWaves
187
+ * @param {object} haltParams
188
+ * @param {object} [extra]
189
+ */
190
+ export function haltSequenceAndPersist(projectRoot, sequenceState, completedWaves, haltParams, extra = {}) {
191
+ const halt = { ...buildSequenceHaltResult(haltParams), ...extra };
192
+ persistSequenceState(projectRoot, sequenceState, completedWaves, {
193
+ lastBatchId: haltParams.batchId ?? sequenceState?.lastBatchId ?? null,
194
+ status: "halted",
195
+ });
196
+ return halt;
197
+ }
198
+
199
+ /**
200
+ * @param {string} projectRoot
201
+ * @param {object|null} sequenceState
202
+ * @param {SequenceCompletedWave[]} completedWaves
203
+ * @param {string|null} [lastBatchId]
204
+ * @param {"active"|"halted"} [status]
205
+ */
206
+ export function persistSequenceState(
207
+ projectRoot,
208
+ sequenceState,
209
+ completedWaves,
210
+ { lastBatchId = null, status = "active" } = {},
211
+ ) {
212
+ if (!sequenceState) return;
213
+ saveSequenceState(
214
+ projectRoot,
215
+ buildSequenceStateSnapshot(sequenceState, {
216
+ completedWaves,
217
+ lastBatchId: lastBatchId ?? sequenceState.lastBatchId ?? null,
218
+ status,
219
+ }),
220
+ );
221
+ }
222
+
223
+ /**
224
+ * @param {string} projectRoot
225
+ * @param {object} params
226
+ * @param {boolean} params.resume
227
+ * @param {string} params.scope
228
+ * @param {number} params.fromWave
229
+ * @param {number|null} [params.throughWave]
230
+ * @param {boolean} params.dryRun
231
+ */
232
+ export function prepareSequenceRunState(projectRoot, { resume, scope, fromWave, throughWave, dryRun }) {
233
+ if (resume) {
234
+ const loaded = loadSequenceState(projectRoot);
235
+ if (!loaded.ok) {
236
+ return { ok: false, error: loaded.error, output: loaded.output };
237
+ }
238
+ const resumeCheck = validateSequenceStateForResume(loaded.state, scope);
239
+ if (!resumeCheck.ok) {
240
+ return { ok: false, error: resumeCheck.error, output: resumeCheck.output };
241
+ }
242
+ return {
243
+ ok: true,
244
+ sequenceState: loaded.state,
245
+ effectiveFromWave: resolveResumeFromWave(loaded.state),
246
+ persistedCompletedWaves: [...(loaded.state.completedWaves ?? [])],
247
+ };
248
+ }
249
+
250
+ if (dryRun) {
251
+ return {
252
+ ok: true,
253
+ sequenceState: null,
254
+ effectiveFromWave: fromWave,
255
+ persistedCompletedWaves: [],
256
+ };
257
+ }
258
+
259
+ return {
260
+ ok: true,
261
+ sequenceState: createInitialSequenceState({ scope, fromWave, throughWave }),
262
+ effectiveFromWave: fromWave,
263
+ persistedCompletedWaves: [],
264
+ };
265
+ }
266
+
267
+ /**
268
+ * @param {string} projectRoot
269
+ * @param {object|null} sequenceState
270
+ * @param {SequenceCompletedWave[]} completedWaves
271
+ * @param {object} plan
272
+ */
273
+ export function finalizeSequenceState(projectRoot, sequenceState, completedWaves, plan) {
274
+ if (!sequenceState) return;
275
+
276
+ const lastCompletedWave =
277
+ completedWaves.length > 0 ? Math.max(...completedWaves.map((wave) => wave.waveIndex)) : -1;
278
+ const planLastWave = (plan?.waves?.length ?? 0) - 1;
279
+ const allPlanWavesComplete = lastCompletedWave >= planLastWave;
280
+
281
+ if (allPlanWavesComplete) {
282
+ clearSequenceState(projectRoot);
283
+ return;
284
+ }
285
+
286
+ persistSequenceState(projectRoot, sequenceState, completedWaves, {
287
+ lastBatchId: sequenceState.lastBatchId ?? null,
288
+ status: "active",
289
+ });
290
+ }
@@ -4,19 +4,31 @@
4
4
  */
5
5
 
6
6
  import { loadSpineConfig } from "../config/spine-config-load.mjs";
7
- import { runBatchPreflight, resolveTasksRoot } from "../config/spine-preflight-lib.mjs";
7
+ import {
8
+ formatPreflightHuman,
9
+ runBatchPreflight,
10
+ resolveTasksRoot,
11
+ } from "../config/spine-preflight-lib.mjs";
8
12
  import { buildPlan } from "../planner/index.mjs";
9
13
  import { resolveWaveTaskIds } from "../planner/wave-scope.mjs";
10
14
 
11
15
  export { resolveWaveTaskIds };
12
16
  import { startBatch } from "./engine.mjs";
13
17
  import { startBatchDetached } from "./detached-start.mjs";
18
+ import { isProcessAlive } from "../process/liveness.mjs";
14
19
  import { approveIntegrateGate, loadGateRecord } from "./gate.mjs";
15
20
  import { integrateOrchToBase } from "./integrate.mjs";
16
21
  import { completeBatch } from "./lifecycle.mjs";
17
22
  import { reconcileBatch } from "./reconcile.mjs";
18
23
  import { loadSpineBatchState } from "./state.mjs";
19
24
  import { validateSequenceAutoApproveGate } from "../doctor/sequence-safety.mjs";
25
+ import {
26
+ buildSequenceStateSnapshot,
27
+ finalizeSequenceState,
28
+ haltSequenceAndPersist,
29
+ persistSequenceState,
30
+ prepareSequenceRunState,
31
+ } from "./sequence-state.mjs";
20
32
 
21
33
  const WAVE_BATCH_SETTLED_DIAGNOSES = new Set([
22
34
  "completed",
@@ -56,9 +68,10 @@ export async function waitForSequenceBatchTerminal({
56
68
  projectRoot,
57
69
  pollIntervalMs = 250,
58
70
  timeoutMs = 120_000,
71
+ enginePid = null,
59
72
  }) {
60
73
  const deadline = Date.now() + timeoutMs;
61
- while (Date.now() < deadline) {
74
+ while (Date.now() < deadline || isEngineStillRunning(enginePid, projectRoot)) {
62
75
  const reconciliation = reconcileBatch({ projectRoot });
63
76
  const diagnosis = reconciliation.diagnosis;
64
77
  if (isSequenceBatchFailure(diagnosis)) {
@@ -90,6 +103,18 @@ export async function waitForSequenceBatchTerminal({
90
103
  };
91
104
  }
92
105
 
106
+ /**
107
+ * Returns true when a detached engine PID is alive or the batch phase is active,
108
+ * preventing sequence timeout while work is still in progress.
109
+ */
110
+ function isEngineStillRunning(enginePid, projectRoot) {
111
+ if (enginePid && isProcessAlive(enginePid)) return true;
112
+ const { raw } = loadSpineBatchState(projectRoot);
113
+ const pid = raw?.enginePid ?? null;
114
+ if (pid && isProcessAlive(pid)) return true;
115
+ return false;
116
+ }
117
+
93
118
  /**
94
119
  * @param {object} plan
95
120
  * @param {object} [options]
@@ -265,27 +290,6 @@ export function runSequenceWaveLandLoop({ projectRoot, batchId, autoApproveGate
265
290
  };
266
291
  }
267
292
 
268
- /**
269
- * @param {object} params
270
- * @param {number} params.waveIndex
271
- * @param {string} [params.error]
272
- * @param {string|null} [params.diagnosis]
273
- * @param {string|null} [params.batchId]
274
- * @param {Array<{ waveIndex: number, batchId: string|null, diagnosis: string|null }>} params.completedWaves
275
- */
276
- function sequenceHalt({ waveIndex, error, diagnosis = null, batchId = null, completedWaves }) {
277
- return {
278
- ok: false,
279
- exitCode: 1,
280
- halted: true,
281
- waveIndex,
282
- error,
283
- diagnosis,
284
- batchId,
285
- completedWaves,
286
- };
287
- }
288
-
289
293
  /**
290
294
  * @param {object} ctx
291
295
  */
@@ -293,8 +297,10 @@ export async function runSequence(ctx) {
293
297
  const {
294
298
  projectRoot,
295
299
  plan,
300
+ scope = "pending",
296
301
  fromWave = 0,
297
302
  throughWave = null,
303
+ resume = false,
298
304
  attached = false,
299
305
  autoApproveGate = false,
300
306
  force = false,
@@ -307,7 +313,26 @@ export async function runSequence(ctx) {
307
313
  startBatchFn = startBatch,
308
314
  } = ctx;
309
315
 
310
- const wavePlan = resolveSequenceWaves(plan, { fromWave, throughWave });
316
+ let sequenceState = null;
317
+ let effectiveFromWave = fromWave;
318
+ /** @type {Array<{ waveIndex: number, batchId: string|null, diagnosis: string|null }>} */
319
+ let persistedCompletedWaves = [];
320
+
321
+ const prepared = prepareSequenceRunState(projectRoot, {
322
+ resume,
323
+ scope,
324
+ fromWave,
325
+ throughWave,
326
+ dryRun,
327
+ });
328
+ if (!prepared.ok) {
329
+ return { ok: false, exitCode: 1, error: prepared.error, output: prepared.output };
330
+ }
331
+ sequenceState = prepared.sequenceState;
332
+ effectiveFromWave = prepared.effectiveFromWave;
333
+ persistedCompletedWaves = prepared.persistedCompletedWaves;
334
+
335
+ const wavePlan = resolveSequenceWaves(plan, { fromWave: effectiveFromWave, throughWave });
311
336
  if (!wavePlan.ok) {
312
337
  return { ok: false, exitCode: 1, error: wavePlan.error, output: wavePlan.output };
313
338
  }
@@ -342,17 +367,22 @@ export async function runSequence(ctx) {
342
367
  ok: false,
343
368
  exitCode: preflight.exitCode ?? 1,
344
369
  error: "preflight_failed",
345
- output: preflight.output,
370
+ output: formatPreflightHuman(preflight),
346
371
  };
347
372
  }
348
373
  }
349
374
 
375
+ if (sequenceState) {
376
+ persistSequenceState(projectRoot, sequenceState, persistedCompletedWaves);
377
+ }
378
+
350
379
  /** @type {Array<{ waveIndex: number, batchId: string|null, diagnosis: string|null }>} */
351
- const completedWaves = [];
380
+ const completedWaves = [...persistedCompletedWaves];
352
381
 
353
382
  for (const wave of wavePlan.waves) {
354
383
  const taskScope = wave.taskIds.join(" ");
355
384
  let batchId = null;
385
+ let detachedEnginePid = null;
356
386
 
357
387
  if (attached) {
358
388
  const startResult = await startBatchFn({
@@ -362,14 +392,17 @@ export async function runSequence(ctx) {
362
392
  });
363
393
  if (!startResult.ok) {
364
394
  if (!stopOnFailure) continue;
365
- return {
366
- ...sequenceHalt({
395
+ return haltSequenceAndPersist(
396
+ projectRoot,
397
+ sequenceState,
398
+ completedWaves,
399
+ {
367
400
  waveIndex: wave.waveIndex,
368
401
  error: startResult.error ?? "batch_start_failed",
369
402
  completedWaves,
370
- }),
371
- output: startResult.output,
372
- };
403
+ },
404
+ { output: startResult.output },
405
+ );
373
406
  }
374
407
  batchId = startResult.batchId ?? loadSpineBatchState(projectRoot).raw?.batchId ?? null;
375
408
  } else {
@@ -386,20 +419,33 @@ export async function runSequence(ctx) {
386
419
  spineBin,
387
420
  scope: taskScope,
388
421
  skipPreflight: true,
389
- waitTerminal: true,
422
+ waitTerminal: false,
390
423
  });
424
+ detachedEnginePid = detached.result?.enginePid ?? null;
391
425
  if (!detached.ok) {
392
- if (!stopOnFailure) continue;
393
- return {
394
- ...sequenceHalt({
395
- waveIndex: wave.waveIndex,
396
- error: detached.result?.error ?? "batch_start_failed",
426
+ const engineStartedButTimeout =
427
+ detached.result?.status === "engine_started" &&
428
+ detached.result?.batchId &&
429
+ isProcessAlive(detachedEnginePid);
430
+ if (engineStartedButTimeout) {
431
+ batchId = detached.result.batchId;
432
+ } else {
433
+ if (!stopOnFailure) continue;
434
+ return haltSequenceAndPersist(
435
+ projectRoot,
436
+ sequenceState,
397
437
  completedWaves,
398
- }),
399
- output: detached.output,
400
- };
438
+ {
439
+ waveIndex: wave.waveIndex,
440
+ error: detached.result?.error ?? "batch_start_failed",
441
+ completedWaves,
442
+ },
443
+ { output: detached.output },
444
+ );
445
+ }
446
+ } else {
447
+ batchId = detached.result?.batchId ?? null;
401
448
  }
402
- batchId = detached.result?.batchId ?? null;
403
449
  }
404
450
 
405
451
  const wait = attached
@@ -408,11 +454,11 @@ export async function runSequence(ctx) {
408
454
  batchId,
409
455
  diagnosis: reconcileBatch({ projectRoot }).diagnosis ?? null,
410
456
  }
411
- : await waitForSequenceBatchTerminal({ projectRoot, pollIntervalMs, timeoutMs });
457
+ : await waitForSequenceBatchTerminal({ projectRoot, pollIntervalMs, timeoutMs, enginePid: detachedEnginePid ?? null });
412
458
 
413
459
  if (!wait.ok || isSequenceBatchFailure(wait.diagnosis)) {
414
460
  if (!stopOnFailure) continue;
415
- return sequenceHalt({
461
+ return haltSequenceAndPersist(projectRoot, sequenceState, completedWaves, {
416
462
  waveIndex: wave.waveIndex,
417
463
  error: wait.error ?? "batch_failed",
418
464
  diagnosis: wait.diagnosis ?? null,
@@ -424,31 +470,47 @@ export async function runSequence(ctx) {
424
470
  const land = runSequenceWaveLandLoop({ projectRoot, batchId: wait.batchId ?? batchId, autoApproveGate });
425
471
  if (!land.ok) {
426
472
  if (!stopOnFailure) continue;
427
- return {
428
- ...sequenceHalt({
473
+ return haltSequenceAndPersist(
474
+ projectRoot,
475
+ sequenceState,
476
+ completedWaves,
477
+ {
429
478
  waveIndex: wave.waveIndex,
430
479
  error: land.error ?? "land_loop_failed",
431
480
  diagnosis: land.diagnosis ?? wait.diagnosis ?? null,
432
481
  batchId: land.batchId ?? wait.batchId ?? batchId,
433
482
  completedWaves,
434
- }),
435
- step: land.step,
436
- headline: land.headline,
437
- };
483
+ },
484
+ { step: land.step, headline: land.headline },
485
+ );
438
486
  }
439
487
 
440
- completedWaves.push({
488
+ const waveResult = {
441
489
  waveIndex: wave.waveIndex,
442
490
  batchId: land.batchId ?? wait.batchId ?? batchId,
443
491
  diagnosis: wait.diagnosis ?? null,
444
- });
492
+ };
493
+ completedWaves.push(waveResult);
494
+
495
+ if (sequenceState) {
496
+ const lastBatchId = waveResult.batchId ?? sequenceState.lastBatchId ?? null;
497
+ sequenceState = buildSequenceStateSnapshot(sequenceState, {
498
+ completedWaves,
499
+ lastBatchId,
500
+ status: "active",
501
+ });
502
+ persistSequenceState(projectRoot, sequenceState, completedWaves, { lastBatchId });
503
+ }
445
504
  }
446
505
 
506
+ finalizeSequenceState(projectRoot, sequenceState, completedWaves, plan);
507
+
447
508
  return {
448
509
  ok: true,
449
510
  exitCode: 0,
450
511
  completedWaves,
451
512
  waveCount: wavePlan.waves.length,
513
+ resumed: resume,
452
514
  output: `Sequence completed ${completedWaves.length} wave(s).\n`,
453
515
  };
454
516
  }
@@ -156,6 +156,22 @@ export function recordBatchEnginePid(state, enginePid) {
156
156
  state.resilience.engineStartedAt = Date.now();
157
157
  }
158
158
 
159
+ /**
160
+ * Reset lane stall clocks at resume handoff so dashboard does not show pre-resume heartbeats as stale.
161
+ *
162
+ * @param {object} state
163
+ */
164
+ export function refreshLaneHeartbeatsOnResume(state) {
165
+ const resilience =
166
+ state.resilience && typeof state.resilience === "object" ? state.resilience : {};
167
+ const reference =
168
+ Number(resilience.engineStartedAt) > 0 ? Number(resilience.engineStartedAt) : Date.now();
169
+ for (const lane of state.lanes ?? []) {
170
+ if (!lane || typeof lane !== "object") continue;
171
+ lane.lastHeartbeatAt = reference;
172
+ }
173
+ }
174
+
159
175
  /**
160
176
  * @param {object} state
161
177
  */
@@ -44,6 +44,7 @@ const PACKAGE_ROOT = path.resolve(__dirname, "../..");
44
44
 
45
45
  const DEFAULT_TIMEOUT_MS = 60 * 60 * 1000;
46
46
  const POST_DONE_KILL_BACKOFF_MS = 5_000;
47
+ const CHILD_DONE_TIMEOUT_MS = 15_000;
47
48
 
48
49
  /**
49
50
  * Force-terminate lane worker processes tracked in batch state.
@@ -136,6 +137,7 @@ export function buildWorkerChildEnv({
136
137
  SPINE_TASK_FOLDER: taskFolder,
137
138
  SPINE_WORKTREE: worktreePath,
138
139
  SPINE_WORKER_RUNNER: runner,
140
+ SPINE_IS_WORKER: "1",
139
141
  PI_SPINE_ROOT: resolvePiSpineRoot(config, projectRoot ?? process.cwd()),
140
142
  };
141
143
  if (projectRoot) env.SPINE_PROJECT_ROOT = projectRoot;
@@ -181,7 +183,7 @@ function spawnWorkerChild({
181
183
  worktreePath,
182
184
  taskFolder,
183
185
  useStub,
184
- timeoutMs,
186
+ timeoutMs: _timeoutMs,
185
187
  projectRoot,
186
188
  batchId,
187
189
  laneNumber,
@@ -456,7 +458,7 @@ export async function runWorker({
456
458
  onHeartbeat,
457
459
  onWorkerPid,
458
460
  fileScopePaths = [],
459
- timeoutMs = DEFAULT_TIMEOUT_MS,
461
+ timeoutMs: _timeoutMs = DEFAULT_TIMEOUT_MS,
460
462
  workerBackendDeps = {},
461
463
  }) {
462
464
  const donePath = path.join(taskFolder, ".DONE");
@@ -743,8 +745,7 @@ export async function runWorker({
743
745
  });
744
746
  stallWarningSent = true;
745
747
  }
746
- workerChild.kill("SIGTERM");
747
- const { output } = await childDone;
748
+ const { output } = await terminateHungWorkerChild(workerChild, childDone);
748
749
  return buildWorkerFailureResult({
749
750
  rawOutput: output,
750
751
  classification: "stall_timeout",
@@ -769,7 +770,15 @@ export async function runWorker({
769
770
  await sleep(Math.min(stallConfig.pollIntervalMs, 5_000));
770
771
  }
771
772
 
772
- const { exitCode, output } = await childDone;
773
+ const childResult = await Promise.race([childDone, sleep(CHILD_DONE_TIMEOUT_MS).then(() => null)]);
774
+ let exitCode, output;
775
+ if (childResult) {
776
+ ({ exitCode, output } = childResult);
777
+ } else {
778
+ // close event didn't fire — sub-processes likely hold stdio pipes open.
779
+ const fallback = await terminateHungWorkerChild(workerChild, childDone);
780
+ ({ exitCode, output } = fallback);
781
+ }
773
782
  const doneFound = fs.existsSync(donePath);
774
783
  if (postDoneTerminated && !doneFound) {
775
784
  return buildWorkerFailureResult({
package/src/cli/deps.mjs CHANGED
@@ -2,8 +2,6 @@
2
2
  * spine deps — dependency graph inspection (FR-SCHED-01 subset).
3
3
  */
4
4
 
5
- import path from 'node:path';
6
-
7
5
  import { resolveTasksRootPath } from '../config/env-overrides.mjs';
8
6
  import {
9
7
  discoverTasks,