pi-spine 1.6.0 → 1.7.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 (37) hide show
  1. package/bin/spine-wait.mjs +12 -0
  2. package/package.json +2 -2
  3. package/skills/spine-release-operator/SKILL.md +37 -7
  4. package/src/batch/attached-runner.mjs +18 -7
  5. package/src/batch/contract-task-start.mjs +12 -7
  6. package/src/batch/contract-verify.mjs +25 -1
  7. package/src/batch/detached-spawn.mjs +78 -0
  8. package/src/batch/detached-start.mjs +25 -71
  9. package/src/batch/diagnosis.mjs +25 -2
  10. package/src/batch/evidence.mjs +30 -250
  11. package/src/batch/gate-evidence-collect.mjs +172 -0
  12. package/src/batch/gate-evidence-read.mjs +145 -0
  13. package/src/batch/gate.mjs +28 -38
  14. package/src/batch/lane-dirty-check.mjs +102 -0
  15. package/src/batch/lifecycle.mjs +6 -5
  16. package/src/batch/post-merge-limbo.mjs +1 -1
  17. package/src/batch/postmortem.mjs +1 -2
  18. package/src/batch/reconcile.mjs +1 -1
  19. package/src/batch/resume-common.mjs +7 -1
  20. package/src/batch/resume-multi-validate.mjs +25 -101
  21. package/src/batch/resume-validation.mjs +120 -0
  22. package/src/batch/sequence.mjs +11 -2
  23. package/src/batch/supervisor-spawn.mjs +379 -0
  24. package/src/cli/spine-wait.mjs +114 -0
  25. package/src/cli/wait.mjs +7 -43
  26. package/src/config/defaults.mjs +4 -1
  27. package/src/config/settings-fields.mjs +23 -0
  28. package/src/config/spine-config-load.mjs +6 -0
  29. package/src/config/spine-config-schema.mjs +121 -0
  30. package/src/config/spine-init-constants.mjs +19 -1
  31. package/src/config/worktree-setup-hook.mjs +221 -0
  32. package/src/doctor/agent-models.mjs +1 -0
  33. package/src/doctor/run-doctor-checks.mjs +14 -0
  34. package/src/doctor/supervisor.mjs +66 -0
  35. package/src/doctor/worktree-health.mjs +1 -1
  36. package/templates/agents/supervisor.md +27 -18
  37. package/templates/spine-config.json +16 -1
@@ -3,36 +3,35 @@
3
3
  */
4
4
 
5
5
  import crypto from "node:crypto";
6
- import fs from "node:fs";
7
- import path from "node:path";
8
6
  import { loadSpineConfig } from "../config/spine-config-load.mjs";
9
7
  import { writeJsonAtomic } from "../fs/atomic-write.mjs";
10
- import { collectCoreEvidenceBundle, collectExtendedEvidenceBundle, finalizeEvidenceBundleComplete } from "./evidence.mjs";
8
+ import {
9
+ collectCoreEvidenceBundle,
10
+ collectExtendedEvidenceBundle,
11
+ collectEvidenceBundle,
12
+ finalizeEvidenceBundleComplete,
13
+ } from "./gate-evidence-collect.mjs";
14
+ import {
15
+ evidenceCompletePath,
16
+ evidenceDir,
17
+ formatGateSummary,
18
+ gateRecordPath,
19
+ loadGateRecord,
20
+ } from "./gate-evidence-read.mjs";
11
21
  import { appendJournalEvent } from "./journal.mjs";
12
-
13
- /**
14
- * @param {string} projectRoot
15
- * @param {string} batchId
16
- */
17
- export function gateRecordPath(projectRoot, batchId) {
18
- return path.join(projectRoot, ".spine", "runtime", batchId, "gate.json");
19
- }
20
-
21
- /**
22
- * @param {string} projectRoot
23
- * @param {string} batchId
24
- * @returns {object|null}
25
- */
26
- export function loadGateRecord(projectRoot, batchId) {
27
- const filePath = gateRecordPath(projectRoot, batchId);
28
- if (!fs.existsSync(filePath)) return null;
29
-
30
- try {
31
- return JSON.parse(fs.readFileSync(filePath, "utf-8"));
32
- } catch {
33
- return null;
34
- }
35
- }
22
+ import { reconcileBatch } from "./reconcile.mjs";
23
+
24
+ export {
25
+ collectCoreEvidenceBundle,
26
+ collectExtendedEvidenceBundle,
27
+ collectEvidenceBundle,
28
+ evidenceCompletePath,
29
+ evidenceDir,
30
+ finalizeEvidenceBundleComplete,
31
+ formatGateSummary,
32
+ gateRecordPath,
33
+ loadGateRecord,
34
+ };
36
35
 
37
36
  /**
38
37
  * @param {string} projectRoot
@@ -43,16 +42,6 @@ export function saveGateRecord(projectRoot, gate) {
43
42
  writeJsonAtomic(filePath, gate);
44
43
  }
45
44
 
46
- /**
47
- * @param {object} gate
48
- */
49
- export function formatGateSummary(gate) {
50
- const status = String(gate.status ?? "unknown");
51
- const kind = String(gate.kind ?? "integrate");
52
- const evidenceCount = Array.isArray(gate.evidenceRefs) ? gate.evidenceRefs.length : 0;
53
- return `${kind} gate ${status} (${evidenceCount} evidence artifact(s))`;
54
- }
55
-
56
45
  /**
57
46
  * @param {object} ctx
58
47
  * @param {string} ctx.projectRoot
@@ -67,7 +56,8 @@ export function openIntegrateGate(ctx) {
67
56
  return { gate: existing, opened: false, evidenceRefs: existing.evidenceRefs ?? [] };
68
57
  }
69
58
 
70
- const core = collectCoreEvidenceBundle({ projectRoot, batchId, batchState, config });
59
+ const reconciliation = reconcileBatch({ projectRoot, batchState, verbose: true });
60
+ const core = collectCoreEvidenceBundle({ projectRoot, batchId, batchState, config, reconciliation });
71
61
  /** @type {string[]} */
72
62
  let evidenceRefs = [...core.evidenceRefs];
73
63
 
@@ -4,6 +4,8 @@
4
4
  */
5
5
 
6
6
  import { execFileSync } from "node:child_process";
7
+ import fs from "node:fs";
8
+ import path from "node:path";
7
9
  import { loadSpineConfig } from "../config/spine-config-load.mjs";
8
10
  import { resolveWorktreeSetupHook } from "../config/worktree-setup-hook.mjs";
9
11
  import { filterPorcelain, gitPorcelain } from "./lane-commit.mjs";
@@ -31,6 +33,106 @@ export const GITIGNORED_ARTIFACT_MARKERS = [
31
33
  "graphify-out/",
32
34
  ];
33
35
 
36
+ /** Flutter build output dir that can pollute unscoped `flutter analyze` (#78). */
37
+ export const FLUTTER_BUILD_DIR = "build";
38
+
39
+ /**
40
+ * @param {string} segment
41
+ * @returns {boolean}
42
+ */
43
+ export function isUnscopedFlutterAnalyzeSegment(segment) {
44
+ const trimmed = String(segment ?? "").trim().toLowerCase();
45
+ if (!/\bflutter\s+analyze\b/.test(trimmed)) {
46
+ return false;
47
+ }
48
+
49
+ const match = trimmed.match(/\bflutter\s+analyze\b(.*)$/);
50
+ if (!match) {
51
+ return false;
52
+ }
53
+
54
+ const remainder = (match[1] ?? "").trim();
55
+ if (!remainder) {
56
+ return true;
57
+ }
58
+
59
+ for (const token of remainder.split(/\s+/)) {
60
+ if (token.startsWith("-")) {
61
+ continue;
62
+ }
63
+ return false;
64
+ }
65
+
66
+ return true;
67
+ }
68
+
69
+ /**
70
+ * True when testCommand invokes unscoped `flutter analyze` (whole project tree).
71
+ * Scoped forms like `flutter analyze lib test` are excluded.
72
+ *
73
+ * @param {string} command
74
+ * @returns {boolean}
75
+ */
76
+ export function isUnscopedFlutterAnalyzeCommand(command) {
77
+ const trimmed = String(command ?? "").trim();
78
+ if (!trimmed) {
79
+ return false;
80
+ }
81
+
82
+ const segments = trimmed
83
+ .split(/&&|;/)
84
+ .map((segment) => segment.trim())
85
+ .filter(Boolean);
86
+ return segments.some((segment) => isUnscopedFlutterAnalyzeSegment(segment));
87
+ }
88
+
89
+ /**
90
+ * True when testCommand combines flutter analyze with flutter test.
91
+ *
92
+ * @param {string} command
93
+ * @returns {boolean}
94
+ */
95
+ export function isFlutterAnalyzeTestCompoundCommand(command) {
96
+ const trimmed = String(command ?? "").trim().toLowerCase();
97
+ return /\bflutter\s+analyze\b/.test(trimmed) && /\bflutter\s+test\b/.test(trimmed);
98
+ }
99
+
100
+ /**
101
+ * Whether contract verify should remove `build/` before running testCommand.
102
+ *
103
+ * @param {string} testCommand
104
+ * @param {object} [config]
105
+ * @returns {boolean}
106
+ */
107
+ export function shouldCleanFlutterBuildBeforeAnalyze(testCommand, config = {}) {
108
+ const enabled = config?.contract?.flutterAnalyzerHygiene ?? true;
109
+ if (!enabled) {
110
+ return false;
111
+ }
112
+ return isUnscopedFlutterAnalyzeCommand(testCommand);
113
+ }
114
+
115
+ /**
116
+ * Remove stale Flutter build artifacts so unscoped analyze does not scan SourcePackages (#78).
117
+ *
118
+ * @param {string} worktreePath
119
+ * @param {{ enabled?: boolean }} [options]
120
+ * @returns {{ cleaned: boolean, buildDir?: string, reason?: string }}
121
+ */
122
+ export function sanitizeFlutterBuildBeforeAnalyze(worktreePath, { enabled = true } = {}) {
123
+ if (!enabled) {
124
+ return { cleaned: false, reason: "disabled" };
125
+ }
126
+
127
+ const buildPath = path.join(worktreePath, FLUTTER_BUILD_DIR);
128
+ if (!fs.existsSync(buildPath)) {
129
+ return { cleaned: false, reason: "no_build_dir" };
130
+ }
131
+
132
+ fs.rmSync(buildPath, { recursive: true, force: true });
133
+ return { cleaned: true, buildDir: FLUTTER_BUILD_DIR };
134
+ }
135
+
34
136
  /**
35
137
  * @param {string} filePath
36
138
  * @returns {boolean}
@@ -10,17 +10,14 @@ import { loadSpineConfig } from "../config/spine-config-load.mjs";
10
10
  import { appendJournalEvent } from "./journal.mjs";
11
11
  import { recordBatchTerminalMetric } from "./metrics.mjs";
12
12
  import { writeBatchPostMortem } from "./postmortem.mjs";
13
- import {
14
- appendBatchHistoryEntry,
15
- clearBatchEnginePid,
16
- saveSpineBatchState,
17
- } from "./state.mjs";
13
+ import { appendBatchHistoryEntry, clearBatchEnginePid, saveSpineBatchState } from "./state.mjs";
18
14
  import { loadBatchStateFile, parseBatchState, reconcileBatch } from "./reconcile.mjs";
19
15
  import {
20
16
  clearActiveBatchStateIfMatches,
21
17
  } from "./batch-state-io.mjs";
22
18
  import { terminateLaneWorkers } from "./worker-host.mjs";
23
19
  import { removeLaneWorktrees } from "./worktree.mjs";
20
+ import { terminateSupervisorIfRunning } from "./supervisor-spawn.mjs";
24
21
 
25
22
  const DISMISS_ALLOWED = new Set(["limbo_stale", "completed_manual", "aborted"]);
26
23
 
@@ -267,6 +264,8 @@ export function dismissBatch(ctx) {
267
264
  };
268
265
  }
269
266
 
267
+ terminateSupervisorIfRunning(projectRoot, batchId, "batch_dismiss");
268
+
270
269
  const terminatedWorkers = terminateLaneWorkers(loaded.raw.lanes, { hard: true });
271
270
  for (const entry of terminatedWorkers) {
272
271
  appendJournalEvent(projectRoot, batchId, "lane.worker_terminated", {
@@ -444,6 +443,8 @@ export function completeBatch(ctx) {
444
443
  };
445
444
  }
446
445
 
446
+ terminateSupervisorIfRunning(projectRoot, batchId, "batch_complete");
447
+
447
448
  const archivePath = archiveBatchState(projectRoot, batchId, loaded.raw);
448
449
  const postMortemPath = writeBatchPostMortem({
449
450
  projectRoot,
@@ -7,7 +7,7 @@ import { fileURLToPath } from "node:url";
7
7
  import {
8
8
  buildAttachedBatchResumeArgv,
9
9
  spawnDetachedBatchEngine,
10
- } from "./detached-start.mjs";
10
+ } from "./detached-spawn.mjs";
11
11
  import { loadGateRecord, openIntegrateGateAfterBatchComplete } from "./gate.mjs";
12
12
  import { appendJournalEvent, readJournalEvents } from "./journal.mjs";
13
13
  import { recordWaveMergeResult } from "./merge/wave-merge-state.mjs";
@@ -4,8 +4,7 @@
4
4
 
5
5
  import fs from "node:fs";
6
6
  import path from "node:path";
7
- import { buildTaskScorecard } from "./evidence.mjs";
8
- import { loadGateRecord, formatGateSummary } from "./gate.mjs";
7
+ import { buildTaskScorecard, formatGateSummary, loadGateRecord } from "./gate-evidence-read.mjs";
9
8
  import { readJournalEvents, readJournalTail, summarizeJournalEvent } from "./journal.mjs";
10
9
  import { reconcileBatch } from "./reconcile.mjs";
11
10
 
@@ -10,7 +10,7 @@ import { PACKAGE_ROOT } from "../config/spine-init-constants.mjs";
10
10
  import { resolveTasksRootPath } from "../config/env-overrides.mjs";
11
11
  import { resolveCurrentGitBranch } from "../config/spine-preflight-lib.mjs";
12
12
  import { buildStalePathDoctorCheck } from "../doctor/stale-path.mjs";
13
- import { loadGateRecord } from "./gate.mjs";
13
+ import { loadGateRecord } from "./gate-evidence-read.mjs";
14
14
  import { deriveMacroPhase, macroPhaseLabel } from "./macro-phase.mjs";
15
15
  import {
16
16
  buildDiagnosisOutput,
@@ -55,10 +55,16 @@ export function taskTerminalSuccessInBatch({ events, task, taskFolder }) {
55
55
  */
56
56
  export function taskAlreadyComplete({ taskFolder, events, task }) {
57
57
  const status = String(task?.status ?? "");
58
- if (status === "pending" || status === "failed" || status === "running") {
58
+ if (status === "failed") {
59
59
  return false;
60
60
  }
61
61
  const doneOnDisk = fs.existsSync(path.join(taskFolder, ".DONE"));
62
+ if (doneOnDisk && (status === "running" || status === "pending")) {
63
+ return true;
64
+ }
65
+ if (status === "pending" || status === "running") {
66
+ return false;
67
+ }
62
68
  return (
63
69
  doneOnDisk ||
64
70
  task.doneFileFound ||
@@ -4,18 +4,18 @@
4
4
 
5
5
  import fs from "node:fs";
6
6
  import { isProcessAlive } from "../process/liveness.mjs";
7
- import {
8
- findFirstWaveNeedingMerge,
9
- hasPendingWaveMerge,
10
- succeededWaveMergeIndices,
11
- waveTasksAllTerminal,
12
- } from "./merge/wave-merge-state.mjs";
7
+ import { hasPendingWaveMerge } from "./merge/wave-merge-state.mjs";
13
8
  import { loadGateRecord } from "./gate.mjs";
14
9
  import { readJournalEvents } from "./journal.mjs";
15
10
  import { detectOrphanRunning, journalEventsSinceResume } from "./orphan-detect.mjs";
16
- import { isPostMergeLimbo } from "./post-merge-limbo.mjs";
17
11
  import { inspectGitState } from "./reconcile.mjs";
18
12
  import { detectSegmentDrift } from "./retry.mjs";
13
+ import {
14
+ classifyTasksForOrphanDetect,
15
+ computePendingTasks,
16
+ detectPostMergeLimboFromResumeSignals,
17
+ findResumableWave,
18
+ } from "./resume-validation.mjs";
19
19
  import { loadSpineBatchState, readBatchEnginePid, validateBatchState } from "./state.mjs";
20
20
  import {
21
21
  assertLaneWorktreeGitHealthy,
@@ -23,6 +23,13 @@ import {
23
23
  repairLaneWorktreeGitMetadata,
24
24
  } from "./worktree.mjs";
25
25
 
26
+ export {
27
+ classifyTasksForOrphanDetect,
28
+ computePendingTasks,
29
+ findResumableWave,
30
+ isTaskResumable,
31
+ } from "./resume-validation.mjs";
32
+
26
33
  /**
27
34
  * Resume-time limbo detection — state file, git signals, and journal merge events (SP-358, GitHub #41).
28
35
  *
@@ -31,109 +38,26 @@ import {
31
38
  * @param {object} params.state
32
39
  */
33
40
  export function detectPostMergeLimboForResume({ projectRoot, state }) {
34
- if (!state || typeof state !== "object") return false;
35
- if (String(state.phase ?? "") === "completed") return false;
36
-
37
- if (isPostMergeLimbo(state)) return true;
38
-
39
- const batchId = String(state.batchId ?? "");
41
+ const batchId = String(state?.batchId ?? "");
40
42
  const git = inspectGitState({
41
43
  projectRoot,
42
44
  batchId,
43
- baseBranch: state.baseBranch ?? "main",
44
- orchBranch: state.orchBranch ?? null,
45
+ baseBranch: state?.baseBranch ?? "main",
46
+ orchBranch: state?.orchBranch ?? null,
45
47
  });
46
- if (isPostMergeLimbo(state, git)) return true;
47
-
48
- const phase = String(state.phase ?? "");
49
- if (phase !== "running" && phase !== "merging") return false;
50
- if (state.endedAt != null) return false;
51
- if (!git.orchBranchExists || git.orchMergedToBase) return false;
52
- if (!batchId || loadGateRecord(projectRoot, batchId)) return false;
53
-
54
- const tasks = state.tasks ?? [];
55
- if (tasks.length === 0) return false;
56
- const allSucceeded = tasks.every((task) => String(task?.status ?? "") === "succeeded");
57
- if (!allSucceeded) return false;
58
-
59
- const totalWaves = Number(state.totalWaves ?? state.wavePlan?.length ?? 0);
60
- if (!Number.isFinite(totalWaves) || totalWaves <= 0) return false;
61
-
62
- const events = readJournalEvents(projectRoot, batchId);
63
- const mergeCompleted = events.filter((event) => event.type === "batch.merge_completed");
64
- if (mergeCompleted.length < 1) return false;
65
-
66
- const lastWaveIndex = totalWaves - 1;
67
- return mergeCompleted.some((event) => Number(event.payload?.waveIndex ?? -1) === lastWaveIndex);
68
- }
69
-
70
- /**
71
- * @param {object} state
72
- * @param {object} task
73
- */
74
- export function isTaskResumable(state, task) {
75
- const status = String(task.status ?? "").toLowerCase();
76
- if (status === "pending" || status === "running") return true;
77
-
78
- return (state.segments ?? []).some((segment) => {
79
- if (!segment || segment.taskId !== task.taskId) return false;
80
- const segmentStatus = String(segment.status ?? "").toLowerCase();
81
- return segmentStatus === "pending" || segmentStatus === "running";
48
+ const journalEvents = batchId ? readJournalEvents(projectRoot, batchId) : [];
49
+ const gateRecordExists = batchId ? loadGateRecord(projectRoot, batchId) != null : false;
50
+
51
+ return detectPostMergeLimboFromResumeSignals({
52
+ state,
53
+ git,
54
+ journalEvents,
55
+ gateRecordExists,
82
56
  });
83
57
  }
84
58
 
85
- /**
86
- * @param {object} state
87
- */
88
- export function computePendingTasks(state) {
89
- return (state.tasks ?? []).filter((task) => task && isTaskResumable(state, task));
90
- }
91
-
92
59
  export { hasPendingWaveMerge } from "./merge/wave-merge-state.mjs";
93
60
 
94
- /**
95
- * @param {object} state
96
- * @param {object[]} pendingTasks
97
- */
98
- export function findResumableWave(state, pendingTasks) {
99
- const pendingIds = new Set(pendingTasks.map((task) => task.taskId));
100
- const wavePlan = state.wavePlan ?? [];
101
- const succeededMerges = succeededWaveMergeIndices(state);
102
-
103
- for (let waveIndex = 0; waveIndex < wavePlan.length; waveIndex++) {
104
- if (succeededMerges.has(waveIndex) && waveTasksAllTerminal(state, waveIndex)) {
105
- continue;
106
- }
107
- const waveTaskIds = wavePlan[waveIndex] ?? [];
108
- if (waveTaskIds.some((taskId) => pendingIds.has(taskId))) {
109
- return waveIndex;
110
- }
111
- }
112
-
113
- const pendingMergeWave = findFirstWaveNeedingMerge(state);
114
- if (pendingMergeWave != null) {
115
- return pendingMergeWave;
116
- }
117
-
118
- return Number(state.currentWaveIndex ?? 0);
119
- }
120
-
121
- /**
122
- * Classify tasks for orphan detection during resume validation (status-only).
123
- *
124
- * @param {object[]} tasks
125
- */
126
- function classifyTasksForOrphanDetect(tasks) {
127
- return (tasks ?? []).map((task) => {
128
- const status = String(task?.status ?? "").toLowerCase();
129
- return {
130
- taskId: task.taskId,
131
- laneNumber: task.laneNumber,
132
- classification: status === "running" ? "running" : status,
133
- };
134
- });
135
- }
136
-
137
61
  /**
138
62
  * Assess whether a running-phase batch may resume after a dead detached engine (SP-296).
139
63
  *
@@ -0,0 +1,120 @@
1
+ /**
2
+ * Pure resume validation helpers — leaf module (SP-468 / #83-B).
3
+ * No reconcile import; I/O wrappers live in resume-multi-validate.mjs.
4
+ */
5
+
6
+ import { isPostMergeLimbo } from "./limbo-detect.mjs";
7
+ import {
8
+ findFirstWaveNeedingMerge,
9
+ succeededWaveMergeIndices,
10
+ waveTasksAllTerminal,
11
+ } from "./merge/wave-merge-state.mjs";
12
+
13
+ /**
14
+ * Resume-time post-merge limbo from state, git, journal, and gate signals (pure).
15
+ *
16
+ * @param {object} params
17
+ * @param {object|null|undefined} params.state
18
+ * @param {object} [params.git]
19
+ * @param {object[]} [params.journalEvents]
20
+ * @param {boolean} [params.gateRecordExists]
21
+ */
22
+ export function detectPostMergeLimboFromResumeSignals({
23
+ state,
24
+ git = {},
25
+ journalEvents = [],
26
+ gateRecordExists = false,
27
+ }) {
28
+ if (!state || typeof state !== "object") return false;
29
+ if (String(state.phase ?? "") === "completed") return false;
30
+
31
+ if (isPostMergeLimbo(state)) return true;
32
+ if (isPostMergeLimbo(state, git)) return true;
33
+
34
+ const phase = String(state.phase ?? "");
35
+ if (phase !== "running" && phase !== "merging") return false;
36
+ if (state.endedAt != null) return false;
37
+ if (!git.orchBranchExists || git.orchMergedToBase) return false;
38
+
39
+ const batchId = String(state.batchId ?? "");
40
+ if (!batchId || gateRecordExists) return false;
41
+
42
+ const tasks = state.tasks ?? [];
43
+ if (tasks.length === 0) return false;
44
+ const allSucceeded = tasks.every((task) => String(task?.status ?? "") === "succeeded");
45
+ if (!allSucceeded) return false;
46
+
47
+ const totalWaves = Number(state.totalWaves ?? state.wavePlan?.length ?? 0);
48
+ if (!Number.isFinite(totalWaves) || totalWaves <= 0) return false;
49
+
50
+ const mergeCompleted = (journalEvents ?? []).filter((event) => event.type === "batch.merge_completed");
51
+ if (mergeCompleted.length < 1) return false;
52
+
53
+ const lastWaveIndex = totalWaves - 1;
54
+ return mergeCompleted.some((event) => Number(event.payload?.waveIndex ?? -1) === lastWaveIndex);
55
+ }
56
+
57
+ /**
58
+ * @param {object} state
59
+ * @param {object} task
60
+ */
61
+ export function isTaskResumable(state, task) {
62
+ const status = String(task.status ?? "").toLowerCase();
63
+ if (status === "pending" || status === "running") return true;
64
+
65
+ return (state.segments ?? []).some((segment) => {
66
+ if (!segment || segment.taskId !== task.taskId) return false;
67
+ const segmentStatus = String(segment.status ?? "").toLowerCase();
68
+ return segmentStatus === "pending" || segmentStatus === "running";
69
+ });
70
+ }
71
+
72
+ /**
73
+ * @param {object} state
74
+ */
75
+ export function computePendingTasks(state) {
76
+ return (state.tasks ?? []).filter((task) => task && isTaskResumable(state, task));
77
+ }
78
+
79
+ /**
80
+ * @param {object} state
81
+ * @param {object[]} pendingTasks
82
+ */
83
+ export function findResumableWave(state, pendingTasks) {
84
+ const pendingIds = new Set(pendingTasks.map((task) => task.taskId));
85
+ const wavePlan = state.wavePlan ?? [];
86
+ const succeededMerges = succeededWaveMergeIndices(state);
87
+
88
+ for (let waveIndex = 0; waveIndex < wavePlan.length; waveIndex++) {
89
+ if (succeededMerges.has(waveIndex) && waveTasksAllTerminal(state, waveIndex)) {
90
+ continue;
91
+ }
92
+ const waveTaskIds = wavePlan[waveIndex] ?? [];
93
+ if (waveTaskIds.some((taskId) => pendingIds.has(taskId))) {
94
+ return waveIndex;
95
+ }
96
+ }
97
+
98
+ const pendingMergeWave = findFirstWaveNeedingMerge(state);
99
+ if (pendingMergeWave != null) {
100
+ return pendingMergeWave;
101
+ }
102
+
103
+ return Number(state.currentWaveIndex ?? 0);
104
+ }
105
+
106
+ /**
107
+ * Classify tasks for orphan detection during resume validation (status-only).
108
+ *
109
+ * @param {object[]} tasks
110
+ */
111
+ export function classifyTasksForOrphanDetect(tasks) {
112
+ return (tasks ?? []).map((task) => {
113
+ const status = String(task?.status ?? "").toLowerCase();
114
+ return {
115
+ taskId: task.taskId,
116
+ laneNumber: task.laneNumber,
117
+ classification: status === "running" ? "running" : status,
118
+ };
119
+ });
120
+ }
@@ -4,6 +4,10 @@
4
4
  */
5
5
 
6
6
  import { loadSpineConfig } from "../config/spine-config-load.mjs";
7
+ import {
8
+ DEFAULT_SEQUENCE_POLL_MS,
9
+ resolveSequencePollMs,
10
+ } from "../config/spine-config-schema.mjs";
7
11
  import {
8
12
  formatPreflightHuman,
9
13
  runBatchPreflight,
@@ -75,7 +79,7 @@ export function isSequenceBatchWaiting(diagnosis) {
75
79
 
76
80
  export async function waitForSequenceBatchTerminal({
77
81
  projectRoot,
78
- pollIntervalMs = 250,
82
+ pollIntervalMs = DEFAULT_SEQUENCE_POLL_MS,
79
83
  timeoutMs = 120_000,
80
84
  enginePid = null,
81
85
  }) {
@@ -316,12 +320,17 @@ export async function runSequence(ctx) {
316
320
  stopOnFailure = true,
317
321
  dryRun = false,
318
322
  skipPreflight = false,
319
- pollIntervalMs = 250,
323
+ pollIntervalMs: pollIntervalMsOverride,
320
324
  timeoutMs = 120_000,
321
325
  spineBin = null,
322
326
  startBatchFn = startBatch,
323
327
  } = ctx;
324
328
 
329
+ const configResult = loadSpineConfig(projectRoot);
330
+ const pollIntervalMs =
331
+ pollIntervalMsOverride ??
332
+ resolveSequencePollMs({ config: configResult.config ?? {} });
333
+
325
334
  let sequenceState = null;
326
335
  let effectiveFromWave = fromWave;
327
336
  /** @type {Array<{ waveIndex: number, batchId: string|null, diagnosis: string|null }>} */