pi-spine 2.2.0 → 2.3.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.
- package/bin/spine-cli/verify.mjs +2 -19
- package/package.json +1 -1
- package/src/batch/attached-runner-promote.mjs +370 -0
- package/src/batch/attached-runner-reconcile.mjs +291 -0
- package/src/batch/attached-runner.mjs +24 -645
- package/src/batch/batch-guards.mjs +69 -0
- package/src/batch/contract-exec.mjs +483 -0
- package/src/batch/contract-parse.mjs +243 -0
- package/src/batch/contract-verify.mjs +32 -707
- package/src/batch/detached-diagnostics.mjs +255 -0
- package/src/batch/detached-run.mjs +326 -0
- package/src/batch/detached-start.mjs +18 -894
- package/src/batch/detached-wait.mjs +346 -0
- package/src/batch/engine.mjs +8 -67
- package/src/batch/integrate-git.mjs +21 -0
- package/src/batch/integrate.mjs +5 -15
- package/src/batch/journal-rebuild-structural.mjs +352 -0
- package/src/batch/journal-rebuild.mjs +16 -348
- package/src/batch/lane-dirty-check-commit.mjs +361 -0
- package/src/batch/lane-dirty-check-git.mjs +298 -0
- package/src/batch/lane-dirty-check.mjs +31 -297
- package/src/batch/reconcile-batch.mjs +483 -0
- package/src/batch/reconcile-classify.mjs +433 -0
- package/src/batch/reconcile-diagnosis.mjs +459 -0
- package/src/batch/reconcile-light-cache.mjs +61 -0
- package/src/batch/reconcile-orphan.mjs +198 -0
- package/src/batch/reconcile.mjs +19 -1693
- package/src/batch/resume-multi-lanes.mjs +6 -148
- package/src/batch/resume-multi-queue.mjs +159 -0
- package/src/batch/resume-single-validate.mjs +25 -0
- package/src/batch/resume.mjs +3 -20
- package/src/batch/review-artifacts.mjs +419 -0
- package/src/batch/review-step-run.mjs +383 -0
- package/src/batch/review-step.mjs +451 -0
- package/src/batch/review.mjs +32 -1199
- package/src/batch/salvage-batch-list.mjs +291 -0
- package/src/batch/salvage-batch.mjs +10 -284
- package/src/batch/sequence-plan.mjs +234 -0
- package/src/batch/sequence-run.mjs +418 -0
- package/src/batch/sequence-wait.mjs +167 -0
- package/src/batch/sequence.mjs +22 -783
- package/src/batch/state-guards.mjs +284 -0
- package/src/batch/state-io.mjs +135 -0
- package/src/batch/state.mjs +27 -396
- package/src/batch/worker-heartbeat.mjs +375 -0
- package/src/batch/worker-host.mjs +50 -541
- package/src/batch/worker-spawn.mjs +315 -0
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sequence planning: release profile, wave resolution, dry-run builders (SP-582 / FR-SHIP-02).
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { loadSpineConfig } from "../config/spine-config-load.mjs";
|
|
6
|
+
import { resolveTasksRoot } from "../config/spine-preflight-lib.mjs";
|
|
7
|
+
import { buildPlan } from "../planner/index.mjs";
|
|
8
|
+
import { resolveWaveTaskIds } from "../planner/wave-scope.mjs";
|
|
9
|
+
|
|
10
|
+
/** Release sequence profile (FR-STA-25 / SP-536). See docs/release/manifest-v1.10.0-example.md. */
|
|
11
|
+
export const SEQUENCE_RELEASE_PROFILE = {
|
|
12
|
+
id: "release",
|
|
13
|
+
/** Max parallel tasks per planner wave (release-profiles.md minor). */
|
|
14
|
+
maxTasksPerWave: 4,
|
|
15
|
+
/** Operator-only pause points between waves; sequence does not auto-approve these. */
|
|
16
|
+
gateOnlyPausePoints: ["gate_approve"],
|
|
17
|
+
/** Dry-run prints the wave land loop without starting batches. */
|
|
18
|
+
dryRunSupported: true,
|
|
19
|
+
manifestDocPath: "docs/release/manifest-v1.10.0-example.md",
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* @param {string|null|undefined} scope
|
|
24
|
+
* @returns {boolean}
|
|
25
|
+
*/
|
|
26
|
+
export function isReleaseSequenceScope(scope) {
|
|
27
|
+
const normalized = String(scope ?? "").trim();
|
|
28
|
+
return normalized.includes(",");
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* @param {object} [params]
|
|
33
|
+
* @param {string|null|undefined} [params.scope]
|
|
34
|
+
* @param {string|null|undefined} [params.profile]
|
|
35
|
+
* @returns {typeof SEQUENCE_RELEASE_PROFILE|null}
|
|
36
|
+
*/
|
|
37
|
+
export function resolveSequenceProfile({ scope, profile = null } = {}) {
|
|
38
|
+
const normalized = String(profile ?? "")
|
|
39
|
+
.trim()
|
|
40
|
+
.toLowerCase();
|
|
41
|
+
if (normalized === "release") {
|
|
42
|
+
return SEQUENCE_RELEASE_PROFILE;
|
|
43
|
+
}
|
|
44
|
+
if (isReleaseSequenceScope(scope)) {
|
|
45
|
+
return SEQUENCE_RELEASE_PROFILE;
|
|
46
|
+
}
|
|
47
|
+
return null;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* @param {object} plan
|
|
52
|
+
* @param {typeof SEQUENCE_RELEASE_PROFILE} [profile]
|
|
53
|
+
*/
|
|
54
|
+
export function validateReleaseSequenceWaveCaps(plan, profile = SEQUENCE_RELEASE_PROFILE) {
|
|
55
|
+
const waves = plan?.waves ?? [];
|
|
56
|
+
/** @type {Array<{ waveIndex: number, taskCount: number }>} */
|
|
57
|
+
const violations = [];
|
|
58
|
+
|
|
59
|
+
for (const wave of waves) {
|
|
60
|
+
const waveIndex = wave.waveIndex ?? wave.index ?? 0;
|
|
61
|
+
const taskCount = Array.isArray(wave.taskIds) ? wave.taskIds.length : 0;
|
|
62
|
+
if (taskCount > profile.maxTasksPerWave) {
|
|
63
|
+
violations.push({ waveIndex, taskCount });
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
if (violations.length === 0) {
|
|
68
|
+
return { ok: true };
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const lines = [
|
|
72
|
+
`Release sequence profile wave cap exceeded (max ${profile.maxTasksPerWave} tasks per wave):`,
|
|
73
|
+
];
|
|
74
|
+
for (const { waveIndex, taskCount } of violations) {
|
|
75
|
+
lines.push(` Wave ${waveIndex}: ${taskCount} tasks`);
|
|
76
|
+
}
|
|
77
|
+
lines.push(`See ${profile.manifestDocPath} and release-profiles.md to split waves.`);
|
|
78
|
+
|
|
79
|
+
return {
|
|
80
|
+
ok: false,
|
|
81
|
+
error: "release_wave_cap_exceeded",
|
|
82
|
+
violations,
|
|
83
|
+
output: `${lines.join("\n")}\n`,
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* @param {typeof SEQUENCE_RELEASE_PROFILE} profile
|
|
89
|
+
*/
|
|
90
|
+
export function buildReleaseSequenceDryRunHeader(profile = SEQUENCE_RELEASE_PROFILE) {
|
|
91
|
+
const pausePoints = profile.gateOnlyPausePoints.join(", ");
|
|
92
|
+
return [
|
|
93
|
+
`# Release sequence profile (${profile.id})`,
|
|
94
|
+
`# Manifest: ${profile.manifestDocPath}`,
|
|
95
|
+
`# Operator pause points: ${pausePoints} (per wave; publish approval is manual)`,
|
|
96
|
+
`# Wave cap: max ${profile.maxTasksPerWave} tasks per wave`,
|
|
97
|
+
];
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* @param {object} plan
|
|
102
|
+
* @param {object} [options]
|
|
103
|
+
* @param {number} [options.fromWave]
|
|
104
|
+
* @param {number|null} [options.throughWave]
|
|
105
|
+
*/
|
|
106
|
+
export function resolveSequenceWaves(plan, { fromWave = 0, throughWave = null } = {}) {
|
|
107
|
+
const waves = plan?.waves ?? [];
|
|
108
|
+
if (waves.length === 0) {
|
|
109
|
+
return { ok: false, error: "plan_has_no_waves", output: "Planner scope has no waves." };
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
const lastWave = throughWave ?? waves.length - 1;
|
|
113
|
+
if (!Number.isInteger(fromWave) || fromWave < 0 || fromWave >= waves.length) {
|
|
114
|
+
return {
|
|
115
|
+
ok: false,
|
|
116
|
+
error: "from_wave_out_of_range",
|
|
117
|
+
fromWave,
|
|
118
|
+
waveCount: waves.length,
|
|
119
|
+
output: `--from-wave ${fromWave} is out of range (plan has ${waves.length} wave(s)).`,
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
if (!Number.isInteger(lastWave) || lastWave < fromWave || lastWave >= waves.length) {
|
|
123
|
+
return {
|
|
124
|
+
ok: false,
|
|
125
|
+
error: "through_wave_out_of_range",
|
|
126
|
+
throughWave: lastWave,
|
|
127
|
+
fromWave,
|
|
128
|
+
waveCount: waves.length,
|
|
129
|
+
output: `--through-wave ${lastWave} is invalid for plan with ${waves.length} wave(s) (from-wave=${fromWave}).`,
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** @type {Array<{ waveIndex: number, taskIds: string[] }>} */
|
|
134
|
+
const entries = [];
|
|
135
|
+
for (let waveIndex = fromWave; waveIndex <= lastWave; waveIndex++) {
|
|
136
|
+
const resolved = resolveWaveTaskIds(plan, waveIndex);
|
|
137
|
+
if (!resolved.ok) return resolved;
|
|
138
|
+
entries.push({ waveIndex, taskIds: resolved.taskIds });
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
return { ok: true, waves: entries, waveCount: waves.length, fromWave, throughWave: lastWave };
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* @param {object} params
|
|
146
|
+
* @param {number} params.waveIndex
|
|
147
|
+
* @param {string[]} params.taskIds
|
|
148
|
+
* @param {boolean} [params.autoApproveGate]
|
|
149
|
+
* @param {typeof SEQUENCE_RELEASE_PROFILE|null} [params.profile]
|
|
150
|
+
*/
|
|
151
|
+
export function buildSequenceWaveCommands({
|
|
152
|
+
waveIndex,
|
|
153
|
+
taskIds,
|
|
154
|
+
autoApproveGate = false,
|
|
155
|
+
profile = null,
|
|
156
|
+
}) {
|
|
157
|
+
const taskScope = taskIds.join(" ");
|
|
158
|
+
const gateOnly =
|
|
159
|
+
profile?.gateOnlyPausePoints?.includes("gate_approve") && !autoApproveGate;
|
|
160
|
+
const gateLine = autoApproveGate
|
|
161
|
+
? "spine gate approve"
|
|
162
|
+
: gateOnly
|
|
163
|
+
? "spine gate approve # GATE-ONLY: operator approval required (release profile)"
|
|
164
|
+
: "spine gate approve # when integrate gate is open";
|
|
165
|
+
return [
|
|
166
|
+
`# Wave ${waveIndex}`,
|
|
167
|
+
`spine batch start ${taskScope}`,
|
|
168
|
+
"spine status --diagnose # wait for terminal batch phase",
|
|
169
|
+
gateLine,
|
|
170
|
+
"spine integrate",
|
|
171
|
+
"spine batch complete",
|
|
172
|
+
];
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* @param {object} params
|
|
177
|
+
* @param {object} params.plan
|
|
178
|
+
* @param {number} [params.fromWave]
|
|
179
|
+
* @param {number|null} [params.throughWave]
|
|
180
|
+
* @param {boolean} [params.autoApproveGate]
|
|
181
|
+
* @param {typeof SEQUENCE_RELEASE_PROFILE|null} [params.profile]
|
|
182
|
+
*/
|
|
183
|
+
export function buildSequenceDryRunPlan({
|
|
184
|
+
plan,
|
|
185
|
+
fromWave = 0,
|
|
186
|
+
throughWave = null,
|
|
187
|
+
autoApproveGate = false,
|
|
188
|
+
profile = null,
|
|
189
|
+
}) {
|
|
190
|
+
const wavePlan = resolveSequenceWaves(plan, { fromWave, throughWave });
|
|
191
|
+
if (!wavePlan.ok) return wavePlan;
|
|
192
|
+
|
|
193
|
+
if (profile) {
|
|
194
|
+
const capCheck = validateReleaseSequenceWaveCaps(plan, profile);
|
|
195
|
+
if (!capCheck.ok) return capCheck;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
const waveCommands = wavePlan.waves.flatMap((wave) =>
|
|
199
|
+
buildSequenceWaveCommands({
|
|
200
|
+
waveIndex: wave.waveIndex,
|
|
201
|
+
taskIds: wave.taskIds,
|
|
202
|
+
autoApproveGate,
|
|
203
|
+
profile,
|
|
204
|
+
}),
|
|
205
|
+
);
|
|
206
|
+
const header = profile ? buildReleaseSequenceDryRunHeader(profile) : [];
|
|
207
|
+
const commands = [...header, ...waveCommands];
|
|
208
|
+
|
|
209
|
+
return {
|
|
210
|
+
ok: true,
|
|
211
|
+
profile: profile?.id ?? null,
|
|
212
|
+
waves: wavePlan.waves,
|
|
213
|
+
waveCount: wavePlan.waveCount,
|
|
214
|
+
fromWave: wavePlan.fromWave,
|
|
215
|
+
throughWave: wavePlan.throughWave,
|
|
216
|
+
commands,
|
|
217
|
+
output: `${commands.join("\n")}\n`,
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* @param {string} projectRoot
|
|
223
|
+
* @param {string} scope
|
|
224
|
+
*/
|
|
225
|
+
export function buildSequencePlan(projectRoot, scope) {
|
|
226
|
+
const configResult = loadSpineConfig(projectRoot);
|
|
227
|
+
const config = configResult.config ?? {};
|
|
228
|
+
const tasksRoot = resolveTasksRoot(projectRoot, configResult);
|
|
229
|
+
if (!tasksRoot) {
|
|
230
|
+
return { ok: false, error: "tasks_root_missing", output: "Tasks root is not configured.\n" };
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
return { ok: true, plan: buildPlan({ scope, config, tasksRoot }), config, tasksRoot };
|
|
234
|
+
}
|
|
@@ -0,0 +1,418 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sequence runtime: runSequence orchestrator (SP-600 / FR-SHIP-02).
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { loadSpineConfig } from "../config/spine-config-load.mjs";
|
|
6
|
+
import { resolveSequencePollMs } from "../config/spine-config-schema.mjs";
|
|
7
|
+
import {
|
|
8
|
+
formatPreflightHuman,
|
|
9
|
+
runBatchPreflight,
|
|
10
|
+
} from "../config/spine-preflight-lib.mjs";
|
|
11
|
+
import { filterPendingTaskIds } from "../planner/pending.mjs";
|
|
12
|
+
import { discoverTasks } from "../tasks/packet/discover.mjs";
|
|
13
|
+
import { startBatch } from "./engine.mjs";
|
|
14
|
+
import { startBatchDetached } from "./detached-start.mjs";
|
|
15
|
+
import { isProcessAlive } from "../process/liveness.mjs";
|
|
16
|
+
import { reconcileBatch } from "./reconcile.mjs";
|
|
17
|
+
import { loadSpineBatchState } from "./state.mjs";
|
|
18
|
+
import { validateSequenceAutoApproveGate } from "../doctor/sequence-safety.mjs";
|
|
19
|
+
import {
|
|
20
|
+
buildSequenceSatisfiedTaskIds,
|
|
21
|
+
collectWaveTaskOutcomes,
|
|
22
|
+
isMergeBlockedBatchOutcome,
|
|
23
|
+
planSequenceWaveTasks,
|
|
24
|
+
resolveWaveAfterMergeBlocked,
|
|
25
|
+
} from "./sequence-waves.mjs";
|
|
26
|
+
import {
|
|
27
|
+
buildSequenceStateSnapshot,
|
|
28
|
+
finalizeSequenceState,
|
|
29
|
+
haltSequenceAndPersist,
|
|
30
|
+
persistSequenceState,
|
|
31
|
+
prepareSequenceRunState,
|
|
32
|
+
} from "./sequence-state.mjs";
|
|
33
|
+
import {
|
|
34
|
+
buildSequenceDryRunPlan,
|
|
35
|
+
resolveSequenceProfile,
|
|
36
|
+
resolveSequenceWaves,
|
|
37
|
+
validateReleaseSequenceWaveCaps,
|
|
38
|
+
} from "./sequence-plan.mjs";
|
|
39
|
+
import {
|
|
40
|
+
isSequenceBatchFailure,
|
|
41
|
+
runSequenceWaveLandLoop,
|
|
42
|
+
waitForSequenceBatchTerminal,
|
|
43
|
+
} from "./sequence-wait.mjs";
|
|
44
|
+
|
|
45
|
+
export {
|
|
46
|
+
isSequenceBatchFailure,
|
|
47
|
+
isSequenceBatchSettled,
|
|
48
|
+
isSequenceBatchWaiting,
|
|
49
|
+
runSequenceWaveLandLoop,
|
|
50
|
+
waitForSequenceBatchTerminal,
|
|
51
|
+
} from "./sequence-wait.mjs";
|
|
52
|
+
|
|
53
|
+
export async function runSequence(ctx) {
|
|
54
|
+
const {
|
|
55
|
+
projectRoot,
|
|
56
|
+
plan,
|
|
57
|
+
scope = "pending",
|
|
58
|
+
fromWave = 0,
|
|
59
|
+
throughWave = null,
|
|
60
|
+
resume = false,
|
|
61
|
+
attached = false,
|
|
62
|
+
autoApproveGate = false,
|
|
63
|
+
force = false,
|
|
64
|
+
stopOnFailure = true,
|
|
65
|
+
dryRun = false,
|
|
66
|
+
skipPreflight = false,
|
|
67
|
+
profile: profileOverride = null,
|
|
68
|
+
pollIntervalMs: pollIntervalMsOverride,
|
|
69
|
+
timeoutMs = 120_000,
|
|
70
|
+
spineBin = null,
|
|
71
|
+
startBatchFn = startBatch,
|
|
72
|
+
} = ctx;
|
|
73
|
+
|
|
74
|
+
const sequenceProfile = resolveSequenceProfile({ scope, profile: profileOverride });
|
|
75
|
+
|
|
76
|
+
const configResult = loadSpineConfig(projectRoot);
|
|
77
|
+
const pollIntervalMs =
|
|
78
|
+
pollIntervalMsOverride ??
|
|
79
|
+
resolveSequencePollMs({ config: configResult.config ?? {} });
|
|
80
|
+
|
|
81
|
+
let sequenceState = null;
|
|
82
|
+
let effectiveFromWave = fromWave;
|
|
83
|
+
/** @type {Array<{ waveIndex: number, batchId: string|null, diagnosis: string|null }>} */
|
|
84
|
+
let persistedCompletedWaves = [];
|
|
85
|
+
|
|
86
|
+
const prepared = prepareSequenceRunState(projectRoot, {
|
|
87
|
+
resume,
|
|
88
|
+
scope,
|
|
89
|
+
fromWave,
|
|
90
|
+
throughWave,
|
|
91
|
+
dryRun,
|
|
92
|
+
});
|
|
93
|
+
if (!prepared.ok) {
|
|
94
|
+
return { ok: false, exitCode: 1, error: prepared.error, output: prepared.output };
|
|
95
|
+
}
|
|
96
|
+
sequenceState = prepared.sequenceState;
|
|
97
|
+
effectiveFromWave = prepared.effectiveFromWave;
|
|
98
|
+
persistedCompletedWaves = prepared.persistedCompletedWaves;
|
|
99
|
+
|
|
100
|
+
const wavePlan = resolveSequenceWaves(plan, { fromWave: effectiveFromWave, throughWave });
|
|
101
|
+
if (!wavePlan.ok) {
|
|
102
|
+
return { ok: false, exitCode: 1, error: wavePlan.error, output: wavePlan.output };
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const autoApproveCheck = validateSequenceAutoApproveGate({
|
|
106
|
+
autoApproveGate,
|
|
107
|
+
force,
|
|
108
|
+
profile: sequenceProfile,
|
|
109
|
+
});
|
|
110
|
+
if (!autoApproveCheck.ok) {
|
|
111
|
+
return {
|
|
112
|
+
ok: false,
|
|
113
|
+
exitCode: 1,
|
|
114
|
+
error: autoApproveCheck.error,
|
|
115
|
+
output: autoApproveCheck.output,
|
|
116
|
+
suggestedCommand: autoApproveCheck.suggestedCommand,
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
if (sequenceProfile) {
|
|
121
|
+
const capCheck = validateReleaseSequenceWaveCaps(plan, sequenceProfile);
|
|
122
|
+
if (!capCheck.ok) {
|
|
123
|
+
return {
|
|
124
|
+
ok: false,
|
|
125
|
+
exitCode: 1,
|
|
126
|
+
error: capCheck.error,
|
|
127
|
+
output: capCheck.output,
|
|
128
|
+
violations: capCheck.violations,
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
if (dryRun) {
|
|
134
|
+
const dryPlan = buildSequenceDryRunPlan({
|
|
135
|
+
plan,
|
|
136
|
+
fromWave: effectiveFromWave,
|
|
137
|
+
throughWave,
|
|
138
|
+
autoApproveGate,
|
|
139
|
+
profile: sequenceProfile,
|
|
140
|
+
});
|
|
141
|
+
if (!dryPlan.ok) {
|
|
142
|
+
return {
|
|
143
|
+
ok: false,
|
|
144
|
+
exitCode: 1,
|
|
145
|
+
error: dryPlan.error,
|
|
146
|
+
output: dryPlan.output,
|
|
147
|
+
violations: dryPlan.violations,
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
return {
|
|
151
|
+
ok: true,
|
|
152
|
+
exitCode: 0,
|
|
153
|
+
dryRun: true,
|
|
154
|
+
profile: dryPlan.profile,
|
|
155
|
+
waves: dryPlan.waves,
|
|
156
|
+
commands: dryPlan.commands,
|
|
157
|
+
output: dryPlan.output,
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
if (!skipPreflight) {
|
|
162
|
+
const preflight = runBatchPreflight({ projectRoot, skipDoctor: false });
|
|
163
|
+
if (!preflight.ok) {
|
|
164
|
+
return {
|
|
165
|
+
ok: false,
|
|
166
|
+
exitCode: preflight.exitCode ?? 1,
|
|
167
|
+
error: "preflight_failed",
|
|
168
|
+
output: formatPreflightHuman(preflight),
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
if (sequenceState) {
|
|
174
|
+
persistSequenceState(projectRoot, sequenceState, persistedCompletedWaves);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/** @type {Array<{ waveIndex: number, batchId: string|null, diagnosis: string|null }>} */
|
|
178
|
+
const completedWaves = [...persistedCompletedWaves];
|
|
179
|
+
|
|
180
|
+
const tasksRoot = plan?.metadata?.tasksRoot ?? null;
|
|
181
|
+
const discoveredTasks = tasksRoot ? discoverTasks(tasksRoot) : [];
|
|
182
|
+
const pendingTaskIds = tasksRoot ? filterPendingTaskIds(discoveredTasks, tasksRoot) : [];
|
|
183
|
+
const doneOnMainTaskIds = discoveredTasks
|
|
184
|
+
.map((task) => task.taskId)
|
|
185
|
+
.filter((taskId) => !pendingTaskIds.includes(taskId));
|
|
186
|
+
/** @type {Array<{ succeeded?: string[], skipped?: string[], failed?: string[] }>} */
|
|
187
|
+
const priorMergeOutcomes = [];
|
|
188
|
+
let satisfiedTaskIds = buildSequenceSatisfiedTaskIds(doneOnMainTaskIds, priorMergeOutcomes);
|
|
189
|
+
let mergeBlockedWaveIndex = null;
|
|
190
|
+
let sequenceHadMergeBlocked = false;
|
|
191
|
+
/** @type {string[]} */
|
|
192
|
+
const sequenceOutputLines = [];
|
|
193
|
+
/** @type {Array<{ waveIndex: number, message: string }>} */
|
|
194
|
+
const skippedWaves = [];
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* @param {number} waveIndex
|
|
198
|
+
* @param {string[]} waveTaskIds
|
|
199
|
+
*/
|
|
200
|
+
function resolveRunnableWaveTasks(waveIndex, waveTaskIds) {
|
|
201
|
+
if (mergeBlockedWaveIndex != null && waveIndex > mergeBlockedWaveIndex) {
|
|
202
|
+
const lastOutcome = priorMergeOutcomes[priorMergeOutcomes.length - 1] ?? {};
|
|
203
|
+
return resolveWaveAfterMergeBlocked({
|
|
204
|
+
plan,
|
|
205
|
+
waveIndex,
|
|
206
|
+
waveTaskIds,
|
|
207
|
+
satisfiedTaskIds,
|
|
208
|
+
mergeBlockedWaveIndex,
|
|
209
|
+
failedTaskIds: lastOutcome.failed ?? [],
|
|
210
|
+
succeededTaskIds: lastOutcome.succeeded ?? [],
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
return planSequenceWaveTasks({ plan, waveIndex, waveTaskIds, satisfiedTaskIds });
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* @param {number} waveIndex
|
|
218
|
+
* @param {object|null} batchState
|
|
219
|
+
* @param {string} [fallbackMessage]
|
|
220
|
+
*/
|
|
221
|
+
function absorbMergeBlockedWave(waveIndex, batchState, fallbackMessage = "") {
|
|
222
|
+
const outcomes = collectWaveTaskOutcomes(batchState);
|
|
223
|
+
priorMergeOutcomes.push(outcomes);
|
|
224
|
+
satisfiedTaskIds = buildSequenceSatisfiedTaskIds(doneOnMainTaskIds, priorMergeOutcomes);
|
|
225
|
+
mergeBlockedWaveIndex = waveIndex;
|
|
226
|
+
sequenceHadMergeBlocked = true;
|
|
227
|
+
const message =
|
|
228
|
+
String(fallbackMessage ?? "").trim() ||
|
|
229
|
+
String(batchState?.lastError ?? "").trim() ||
|
|
230
|
+
`Wave merge blocked (§17.4 mixed-outcome policy) at wave ${waveIndex}.`;
|
|
231
|
+
sequenceOutputLines.push(message);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
for (const wave of wavePlan.waves) {
|
|
235
|
+
const wavePlanResolution = resolveRunnableWaveTasks(wave.waveIndex, wave.taskIds);
|
|
236
|
+
if (wavePlanResolution.action === "skip") {
|
|
237
|
+
skippedWaves.push({
|
|
238
|
+
waveIndex: wave.waveIndex,
|
|
239
|
+
message: wavePlanResolution.message ?? `Sequence wave ${wave.waveIndex} skipped.`,
|
|
240
|
+
});
|
|
241
|
+
sequenceOutputLines.push(
|
|
242
|
+
wavePlanResolution.message ?? `Sequence wave ${wave.waveIndex} skipped.`,
|
|
243
|
+
);
|
|
244
|
+
continue;
|
|
245
|
+
}
|
|
246
|
+
if (wavePlanResolution.message) {
|
|
247
|
+
sequenceOutputLines.push(wavePlanResolution.message);
|
|
248
|
+
} else if (wavePlanResolution.partialSkipMessage) {
|
|
249
|
+
sequenceOutputLines.push(wavePlanResolution.partialSkipMessage);
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
const taskScope = wavePlanResolution.runnableTaskIds.join(" ");
|
|
253
|
+
if (!taskScope) continue;
|
|
254
|
+
|
|
255
|
+
let batchId = null;
|
|
256
|
+
let detachedEnginePid = null;
|
|
257
|
+
|
|
258
|
+
if (attached) {
|
|
259
|
+
const startResult = await startBatchFn({
|
|
260
|
+
projectRoot,
|
|
261
|
+
scope: taskScope,
|
|
262
|
+
skipPreflight: true,
|
|
263
|
+
});
|
|
264
|
+
if (!startResult.ok) {
|
|
265
|
+
const reconciliation = reconcileBatch({ projectRoot });
|
|
266
|
+
if (isMergeBlockedBatchOutcome({ startResult, reconciliation })) {
|
|
267
|
+
absorbMergeBlockedWave(
|
|
268
|
+
wave.waveIndex,
|
|
269
|
+
loadSpineBatchState(projectRoot).raw,
|
|
270
|
+
startResult.output ?? "",
|
|
271
|
+
);
|
|
272
|
+
continue;
|
|
273
|
+
}
|
|
274
|
+
if (!stopOnFailure) continue;
|
|
275
|
+
return haltSequenceAndPersist(
|
|
276
|
+
projectRoot,
|
|
277
|
+
sequenceState,
|
|
278
|
+
completedWaves,
|
|
279
|
+
{
|
|
280
|
+
waveIndex: wave.waveIndex,
|
|
281
|
+
error: startResult.error ?? "batch_start_failed",
|
|
282
|
+
completedWaves,
|
|
283
|
+
},
|
|
284
|
+
{ output: startResult.output },
|
|
285
|
+
);
|
|
286
|
+
}
|
|
287
|
+
batchId = startResult.batchId ?? loadSpineBatchState(projectRoot).raw?.batchId ?? null;
|
|
288
|
+
} else {
|
|
289
|
+
if (!spineBin) {
|
|
290
|
+
return {
|
|
291
|
+
ok: false,
|
|
292
|
+
exitCode: 1,
|
|
293
|
+
error: "spine_bin_required",
|
|
294
|
+
output: "Detached sequence runs require spineBin.\n",
|
|
295
|
+
};
|
|
296
|
+
}
|
|
297
|
+
const detached = await startBatchDetached({
|
|
298
|
+
projectRoot,
|
|
299
|
+
spineBin,
|
|
300
|
+
scope: taskScope,
|
|
301
|
+
skipPreflight: true,
|
|
302
|
+
waitTerminal: false,
|
|
303
|
+
});
|
|
304
|
+
detachedEnginePid = detached.result?.enginePid ?? null;
|
|
305
|
+
if (!detached.ok) {
|
|
306
|
+
const engineStartedButTimeout =
|
|
307
|
+
detached.result?.status === "engine_started" &&
|
|
308
|
+
detached.result?.batchId &&
|
|
309
|
+
isProcessAlive(detachedEnginePid);
|
|
310
|
+
if (engineStartedButTimeout) {
|
|
311
|
+
batchId = detached.result.batchId;
|
|
312
|
+
} else {
|
|
313
|
+
if (!stopOnFailure) continue;
|
|
314
|
+
return haltSequenceAndPersist(
|
|
315
|
+
projectRoot,
|
|
316
|
+
sequenceState,
|
|
317
|
+
completedWaves,
|
|
318
|
+
{
|
|
319
|
+
waveIndex: wave.waveIndex,
|
|
320
|
+
error: detached.result?.error ?? "batch_start_failed",
|
|
321
|
+
completedWaves,
|
|
322
|
+
},
|
|
323
|
+
{ output: detached.output },
|
|
324
|
+
);
|
|
325
|
+
}
|
|
326
|
+
} else {
|
|
327
|
+
batchId = detached.result?.batchId ?? null;
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
const wait = attached
|
|
332
|
+
? {
|
|
333
|
+
ok: true,
|
|
334
|
+
batchId,
|
|
335
|
+
diagnosis: reconcileBatch({ projectRoot }).diagnosis ?? null,
|
|
336
|
+
}
|
|
337
|
+
: await waitForSequenceBatchTerminal({ projectRoot, pollIntervalMs, timeoutMs, enginePid: detachedEnginePid ?? null });
|
|
338
|
+
|
|
339
|
+
if (!wait.ok || isSequenceBatchFailure(wait.diagnosis)) {
|
|
340
|
+
const reconciliation = wait.reconciliation ?? reconcileBatch({ projectRoot });
|
|
341
|
+
if (isMergeBlockedBatchOutcome({ reconciliation })) {
|
|
342
|
+
absorbMergeBlockedWave(
|
|
343
|
+
wave.waveIndex,
|
|
344
|
+
reconciliation.signals?.raw ?? loadSpineBatchState(projectRoot).raw,
|
|
345
|
+
reconciliation.headline ?? "",
|
|
346
|
+
);
|
|
347
|
+
continue;
|
|
348
|
+
}
|
|
349
|
+
if (!stopOnFailure) continue;
|
|
350
|
+
return haltSequenceAndPersist(projectRoot, sequenceState, completedWaves, {
|
|
351
|
+
waveIndex: wave.waveIndex,
|
|
352
|
+
error: wait.error ?? "batch_failed",
|
|
353
|
+
diagnosis: wait.diagnosis ?? null,
|
|
354
|
+
batchId: wait.batchId ?? batchId,
|
|
355
|
+
completedWaves,
|
|
356
|
+
});
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
const land = runSequenceWaveLandLoop({ projectRoot, batchId: wait.batchId ?? batchId, autoApproveGate });
|
|
360
|
+
if (!land.ok) {
|
|
361
|
+
if (!stopOnFailure) continue;
|
|
362
|
+
return haltSequenceAndPersist(
|
|
363
|
+
projectRoot,
|
|
364
|
+
sequenceState,
|
|
365
|
+
completedWaves,
|
|
366
|
+
{
|
|
367
|
+
waveIndex: wave.waveIndex,
|
|
368
|
+
error: land.error ?? "land_loop_failed",
|
|
369
|
+
diagnosis: land.diagnosis ?? wait.diagnosis ?? null,
|
|
370
|
+
batchId: land.batchId ?? wait.batchId ?? batchId,
|
|
371
|
+
completedWaves,
|
|
372
|
+
},
|
|
373
|
+
{ step: land.step, headline: land.headline },
|
|
374
|
+
);
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
const waveResult = {
|
|
378
|
+
waveIndex: wave.waveIndex,
|
|
379
|
+
batchId: land.batchId ?? wait.batchId ?? batchId,
|
|
380
|
+
diagnosis: wait.diagnosis ?? null,
|
|
381
|
+
};
|
|
382
|
+
completedWaves.push(waveResult);
|
|
383
|
+
|
|
384
|
+
for (const taskId of wavePlanResolution.runnableTaskIds) {
|
|
385
|
+
satisfiedTaskIds.add(taskId);
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
if (sequenceState) {
|
|
389
|
+
const lastBatchId = waveResult.batchId ?? sequenceState.lastBatchId ?? null;
|
|
390
|
+
sequenceState = buildSequenceStateSnapshot(sequenceState, {
|
|
391
|
+
completedWaves,
|
|
392
|
+
lastBatchId,
|
|
393
|
+
status: "active",
|
|
394
|
+
});
|
|
395
|
+
persistSequenceState(projectRoot, sequenceState, completedWaves, { lastBatchId });
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
finalizeSequenceState(projectRoot, sequenceState, completedWaves, plan);
|
|
400
|
+
|
|
401
|
+
if (completedWaves.length > 0) {
|
|
402
|
+
sequenceOutputLines.push(`Sequence completed ${completedWaves.length} wave(s).`);
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
const partialMergeBlocked = sequenceHadMergeBlocked && completedWaves.length < wavePlan.waves.length;
|
|
406
|
+
|
|
407
|
+
return {
|
|
408
|
+
ok: !sequenceHadMergeBlocked,
|
|
409
|
+
exitCode: sequenceHadMergeBlocked ? 1 : 0,
|
|
410
|
+
completedWaves,
|
|
411
|
+
skippedWaves,
|
|
412
|
+
mergeBlocked: sequenceHadMergeBlocked,
|
|
413
|
+
partial: partialMergeBlocked,
|
|
414
|
+
waveCount: wavePlan.waves.length,
|
|
415
|
+
resumed: resume,
|
|
416
|
+
output: `${sequenceOutputLines.join("\n")}\n`,
|
|
417
|
+
};
|
|
418
|
+
}
|