pi-spine 2.1.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-batch.mjs +74 -5
- package/bin/spine-cli/verify.mjs +2 -18
- 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 -634
- 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-lanes/merge.mjs +25 -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 +94 -347
- 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 +44 -149
- 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 +417 -0
- 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
package/bin/spine-batch.mjs
CHANGED
|
@@ -21,6 +21,7 @@ import {
|
|
|
21
21
|
runAttachedBatchEngine,
|
|
22
22
|
} from "../src/batch/attached-runner.mjs";
|
|
23
23
|
import { enforceAttachedOrphanRiskGuard } from "../src/doctor/attached-orphan-risk.mjs";
|
|
24
|
+
import { formatSalvageIntegrateOutput, formatSalvageListOutput, integrateSalvageableLane, listSalvageableLanes } from "../src/batch/salvage-batch.mjs";
|
|
24
25
|
|
|
25
26
|
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
26
27
|
|
|
@@ -29,6 +30,7 @@ const BATCH_FLAG_VALUE_TAKERS = new Set([
|
|
|
29
30
|
"--through-wave",
|
|
30
31
|
"--batch",
|
|
31
32
|
"--reason",
|
|
33
|
+
"--lane",
|
|
32
34
|
]);
|
|
33
35
|
|
|
34
36
|
/**
|
|
@@ -36,7 +38,7 @@ const BATCH_FLAG_VALUE_TAKERS = new Set([
|
|
|
36
38
|
*/
|
|
37
39
|
export function printBatchHelp() {
|
|
38
40
|
return (
|
|
39
|
-
"Usage: spine batch start <scope>|pause|resume|retry <taskId>|skip <taskId>|force-merge [--wave N]|abort|dismiss|complete [--batch ID] [--reason TEXT] [--hard] [--force] [--force-superseded] [--attached] [--dry-run] [--wave N] [--through-wave N] [--skip-preflight] [--detect-manual-merge] [--json]\n"
|
|
41
|
+
"Usage: spine batch start <scope>|pause|resume|retry <taskId>|skip <taskId>|force-merge [--wave N]|salvage|abort|dismiss|complete [--batch ID] [--lane N] [--integrate] [--yes] [--reason TEXT] [--hard] [--force] [--force-superseded] [--attached] [--dry-run] [--wave N] [--through-wave N] [--skip-preflight] [--detect-manual-merge] [--json]\n"
|
|
40
42
|
);
|
|
41
43
|
}
|
|
42
44
|
|
|
@@ -51,9 +53,18 @@ export function isBatchHelpRequest(args) {
|
|
|
51
53
|
return true;
|
|
52
54
|
}
|
|
53
55
|
const subcommand = args.find((token) =>
|
|
54
|
-
[
|
|
55
|
-
|
|
56
|
-
|
|
56
|
+
[
|
|
57
|
+
"dismiss",
|
|
58
|
+
"complete",
|
|
59
|
+
"abort",
|
|
60
|
+
"start",
|
|
61
|
+
"pause",
|
|
62
|
+
"resume",
|
|
63
|
+
"retry",
|
|
64
|
+
"skip",
|
|
65
|
+
"force-merge",
|
|
66
|
+
"salvage",
|
|
67
|
+
].includes(token),
|
|
57
68
|
);
|
|
58
69
|
if (subcommand && args.includes("help")) {
|
|
59
70
|
const helpIdx = args.indexOf("help");
|
|
@@ -143,6 +154,12 @@ export function parseBatchArgs(args) {
|
|
|
143
154
|
waveIndex = Number(args[waveIdx + 1]);
|
|
144
155
|
}
|
|
145
156
|
|
|
157
|
+
let laneNumber = null;
|
|
158
|
+
const laneIdx = args.indexOf("--lane");
|
|
159
|
+
if (laneIdx >= 0 && args[laneIdx + 1]) {
|
|
160
|
+
laneNumber = Number(args[laneIdx + 1]);
|
|
161
|
+
}
|
|
162
|
+
|
|
146
163
|
const subcommand =
|
|
147
164
|
args.find(
|
|
148
165
|
(t) =>
|
|
@@ -154,7 +171,8 @@ export function parseBatchArgs(args) {
|
|
|
154
171
|
t === "resume" ||
|
|
155
172
|
t === "retry" ||
|
|
156
173
|
t === "skip" ||
|
|
157
|
-
t === "force-merge"
|
|
174
|
+
t === "force-merge" ||
|
|
175
|
+
t === "salvage",
|
|
158
176
|
) ?? null;
|
|
159
177
|
|
|
160
178
|
let waveFilter = null;
|
|
@@ -175,6 +193,8 @@ export function parseBatchArgs(args) {
|
|
|
175
193
|
const dryRun = flags.has("--dry-run");
|
|
176
194
|
const skipPreflight = flags.has("--skip-preflight");
|
|
177
195
|
const forceSuperseded = flags.has("--force-superseded");
|
|
196
|
+
const integrate = flags.has("--integrate");
|
|
197
|
+
const yes = flags.has("--yes");
|
|
178
198
|
|
|
179
199
|
return {
|
|
180
200
|
json: flags.has("--json"),
|
|
@@ -187,9 +207,12 @@ export function parseBatchArgs(args) {
|
|
|
187
207
|
dryRun,
|
|
188
208
|
skipPreflight,
|
|
189
209
|
forceSuperseded,
|
|
210
|
+
integrate,
|
|
211
|
+
yes,
|
|
190
212
|
batchId,
|
|
191
213
|
reason,
|
|
192
214
|
waveIndex,
|
|
215
|
+
laneNumber,
|
|
193
216
|
waveFilter,
|
|
194
217
|
waveFilterError,
|
|
195
218
|
subcommand,
|
|
@@ -225,6 +248,52 @@ export async function runSpineBatch(options) {
|
|
|
225
248
|
};
|
|
226
249
|
}
|
|
227
250
|
|
|
251
|
+
if (parsed.subcommand === "salvage") {
|
|
252
|
+
if (!parsed.batchId) {
|
|
253
|
+
return {
|
|
254
|
+
exitCode: 1,
|
|
255
|
+
output:
|
|
256
|
+
"Usage: spine batch salvage --batch <batchId> --dry-run [--json]\n" +
|
|
257
|
+
" spine batch salvage --batch <batchId> --lane <n> --integrate [--yes] [--json]\n",
|
|
258
|
+
};
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
if (parsed.integrate) {
|
|
262
|
+
if (!Number.isFinite(parsed.laneNumber) || parsed.laneNumber <= 0) {
|
|
263
|
+
return {
|
|
264
|
+
exitCode: 1,
|
|
265
|
+
output: "Usage: spine batch salvage --batch <batchId> --lane <n> --integrate [--yes] [--json]\n",
|
|
266
|
+
};
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
const result = await integrateSalvageableLane(projectRoot, parsed.batchId, parsed.laneNumber, {
|
|
270
|
+
yes: parsed.yes,
|
|
271
|
+
forceIntegrate: parsed.force,
|
|
272
|
+
});
|
|
273
|
+
return {
|
|
274
|
+
exitCode: result.exitCode ?? (result.ok ? 0 : 1),
|
|
275
|
+
output: formatSalvageIntegrateOutput(result, { json: parsed.json }),
|
|
276
|
+
result,
|
|
277
|
+
};
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
if (!parsed.dryRun) {
|
|
281
|
+
return {
|
|
282
|
+
exitCode: 1,
|
|
283
|
+
output:
|
|
284
|
+
"Usage: spine batch salvage --batch <batchId> --dry-run [--json]\n" +
|
|
285
|
+
" spine batch salvage --batch <batchId> --lane <n> --integrate [--yes] [--json]\n",
|
|
286
|
+
};
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
const result = listSalvageableLanes(projectRoot, parsed.batchId);
|
|
290
|
+
return {
|
|
291
|
+
exitCode: result.exitCode ?? (result.ok ? 0 : 1),
|
|
292
|
+
output: formatSalvageListOutput(result, { json: parsed.json }),
|
|
293
|
+
result,
|
|
294
|
+
};
|
|
295
|
+
}
|
|
296
|
+
|
|
228
297
|
if (parsed.subcommand === "abort") {
|
|
229
298
|
const result = abortBatch({
|
|
230
299
|
projectRoot,
|
package/bin/spine-cli/verify.mjs
CHANGED
|
@@ -11,24 +11,8 @@ import { c, FAIL, OK } from "./shared.mjs";
|
|
|
11
11
|
|
|
12
12
|
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
13
13
|
|
|
14
|
-
/** Modules >500 LOC grandfathered until v2.3 split
|
|
15
|
-
export const PHASE23_GRANDFATHERED_OVER_500 = [
|
|
16
|
-
"src/batch/engine.mjs",
|
|
17
|
-
"src/batch/integrate.mjs",
|
|
18
|
-
"src/batch/state.mjs",
|
|
19
|
-
"src/batch/worker-host.mjs",
|
|
20
|
-
"src/batch/reconcile.mjs",
|
|
21
|
-
"src/batch/detached-start.mjs",
|
|
22
|
-
"src/batch/review.mjs",
|
|
23
|
-
"src/batch/resume-multi-lanes.mjs",
|
|
24
|
-
"src/batch/resume.mjs",
|
|
25
|
-
"src/batch/sequence.mjs",
|
|
26
|
-
"src/batch/contract-verify.mjs",
|
|
27
|
-
"src/batch/journal-rebuild.mjs",
|
|
28
|
-
"src/batch/lane-dirty-check.mjs",
|
|
29
|
-
"src/batch/attached-runner.mjs",
|
|
30
|
-
"src/batch/lifecycle.mjs",
|
|
31
|
-
];
|
|
14
|
+
/** Modules >500 LOC grandfathered until v2.3 split — emptied after SP-578–592 (issue #117). */
|
|
15
|
+
export const PHASE23_GRANDFATHERED_OVER_500 = [];
|
|
32
16
|
|
|
33
17
|
const PHASE23_ENGINE_LANES_MAX = 500;
|
|
34
18
|
|
package/package.json
CHANGED
|
@@ -0,0 +1,370 @@
|
|
|
1
|
+
// @ts-nocheck
|
|
2
|
+
/**
|
|
3
|
+
* Attached batch promote/exit paths — milestones, land-loop CLI, post-merge limbo (SP-586).
|
|
4
|
+
* Pause/resume reconcile lives in attached-runner-reconcile.mjs (SP-604).
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import {
|
|
8
|
+
finalizeAttachedLandLoopBeforeExit,
|
|
9
|
+
finalizeResumedBatchForIntegrate,
|
|
10
|
+
isPostMergeLimbo,
|
|
11
|
+
} from "./post-merge-limbo.mjs";
|
|
12
|
+
import { detectPostMergeLimboForResume } from "./resume-multi-validate.mjs";
|
|
13
|
+
import { terminateStaleDetachedEngine } from "./resume-engine.mjs";
|
|
14
|
+
import { reconcileBatch } from "./reconcile.mjs";
|
|
15
|
+
import { readJournalEventsCached } from "./journal.mjs";
|
|
16
|
+
import { enforceOperatorPauseOnDisk } from "./pause.mjs";
|
|
17
|
+
import { loadSpineBatchState, saveSpineBatchState } from "./state.mjs";
|
|
18
|
+
import { loadSpineConfig } from "../config/spine-config-load.mjs";
|
|
19
|
+
import { resolveAttachedMilestonePollMs } from "../config/spine-config-schema.mjs";
|
|
20
|
+
import { startParentSessionMonitor } from "./parent-session-monitor.mjs";
|
|
21
|
+
import {
|
|
22
|
+
enforceAttachedEngineSingleOwner,
|
|
23
|
+
reconcilePausedResumeDoneInLane,
|
|
24
|
+
} from "./attached-runner-reconcile.mjs";
|
|
25
|
+
|
|
26
|
+
export { DEFAULT_ATTACHED_MILESTONE_POLL_MS } from "../config/spine-config-schema.mjs";
|
|
27
|
+
|
|
28
|
+
/** @type {boolean} */
|
|
29
|
+
let attachedExitHandlersInstalled = false;
|
|
30
|
+
|
|
31
|
+
/** Journal types surfaced on attached stdout during the land loop. */
|
|
32
|
+
export const ATTACHED_LAND_LOOP_MILESTONE_TYPES = new Set([
|
|
33
|
+
"batch.started",
|
|
34
|
+
"task.started",
|
|
35
|
+
"task.completed",
|
|
36
|
+
"task.failed",
|
|
37
|
+
"batch.merge_started",
|
|
38
|
+
"batch.merge_completed",
|
|
39
|
+
"batch.merge_blocked",
|
|
40
|
+
"gate.opened",
|
|
41
|
+
"gate.evidence_collecting",
|
|
42
|
+
"gate.evidence_completed",
|
|
43
|
+
"gate.evidence_failed",
|
|
44
|
+
"batch.land_loop_finalized",
|
|
45
|
+
"batch.completed",
|
|
46
|
+
]);
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Install journal-aware SIGTERM/SIGINT handlers before attached engine exit (SP-378).
|
|
50
|
+
*
|
|
51
|
+
* @param {object} params
|
|
52
|
+
* @param {string} params.projectRoot
|
|
53
|
+
* @param {string} [params.spineBin]
|
|
54
|
+
*/
|
|
55
|
+
export function installAttachedExitFinalizeHandlers({ projectRoot, spineBin }) {
|
|
56
|
+
if (attachedExitHandlersInstalled) return;
|
|
57
|
+
attachedExitHandlersInstalled = true;
|
|
58
|
+
|
|
59
|
+
const onShutdown = (signal) => {
|
|
60
|
+
const handoff = finalizeAttachedLandLoopBeforeExit({ projectRoot, spineBin, signal });
|
|
61
|
+
if (handoff.handled) {
|
|
62
|
+
process.exit(0);
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
process.once("SIGTERM", () => onShutdown("SIGTERM"));
|
|
67
|
+
process.once("SIGINT", () => onShutdown("SIGINT"));
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* @param {number} ms
|
|
72
|
+
*/
|
|
73
|
+
function sleep(ms) {
|
|
74
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* @param {object} event
|
|
79
|
+
*/
|
|
80
|
+
function milestoneEventKey(event) {
|
|
81
|
+
if (typeof event.eventId === "string" && event.eventId) {
|
|
82
|
+
return event.eventId;
|
|
83
|
+
}
|
|
84
|
+
const taskId = typeof event.taskId === "string" ? event.taskId : "";
|
|
85
|
+
const timestamp = typeof event.timestamp === "string" ? event.timestamp : String(event.timestamp ?? "");
|
|
86
|
+
return `${event.type ?? "unknown"}:${timestamp}:${taskId}`;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* @param {object} event
|
|
91
|
+
* @returns {string}
|
|
92
|
+
*/
|
|
93
|
+
export function formatAttachedMilestoneLine(event) {
|
|
94
|
+
const type = String(event.type ?? "unknown");
|
|
95
|
+
const taskId = typeof event.taskId === "string" && event.taskId ? ` ${event.taskId}` : "";
|
|
96
|
+
const payload =
|
|
97
|
+
event.payload && typeof event.payload === "object" ? /** @type {Record<string, unknown>} */ (event.payload) : {};
|
|
98
|
+
const waveIndex = payload.waveIndex;
|
|
99
|
+
const waveSuffix =
|
|
100
|
+
typeof waveIndex === "number" &&
|
|
101
|
+
(type === "batch.merge_started" || type === "batch.merge_completed" || type === "batch.merge_blocked")
|
|
102
|
+
? ` wave=${waveIndex}`
|
|
103
|
+
: "";
|
|
104
|
+
return `[spine] ${type}${taskId}${waveSuffix}\n`;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* @param {object} params
|
|
109
|
+
* @param {string} params.projectRoot
|
|
110
|
+
* @param {(line: string) => void} [params.write]
|
|
111
|
+
* @param {number} [params.pollIntervalMs]
|
|
112
|
+
* @returns {Promise<{ stop: () => Promise<void> }>}
|
|
113
|
+
*/
|
|
114
|
+
export async function startAttachedMilestoneReporter({
|
|
115
|
+
projectRoot,
|
|
116
|
+
write = (line) => process.stdout.write(line),
|
|
117
|
+
pollIntervalMs,
|
|
118
|
+
}) {
|
|
119
|
+
const configResult = loadSpineConfig(projectRoot);
|
|
120
|
+
const resolvedPollMs =
|
|
121
|
+
pollIntervalMs ??
|
|
122
|
+
resolveAttachedMilestonePollMs({ config: configResult.config ?? {} });
|
|
123
|
+
/** @type {Set<string>} */
|
|
124
|
+
const printed = new Set();
|
|
125
|
+
let batchId = null;
|
|
126
|
+
let stopped = false;
|
|
127
|
+
|
|
128
|
+
const loop = async () => {
|
|
129
|
+
while (!stopped) {
|
|
130
|
+
enforceOperatorPauseOnDisk(projectRoot);
|
|
131
|
+
if (!batchId) {
|
|
132
|
+
const loaded = loadSpineBatchState(projectRoot);
|
|
133
|
+
batchId = loaded.raw?.batchId ? String(loaded.raw.batchId) : null;
|
|
134
|
+
}
|
|
135
|
+
if (batchId) {
|
|
136
|
+
const events = readJournalEventsCached(projectRoot, batchId);
|
|
137
|
+
for (const event of events) {
|
|
138
|
+
const key = milestoneEventKey(event);
|
|
139
|
+
if (printed.has(key)) continue;
|
|
140
|
+
if (!ATTACHED_LAND_LOOP_MILESTONE_TYPES.has(String(event.type ?? ""))) continue;
|
|
141
|
+
printed.add(key);
|
|
142
|
+
write(formatAttachedMilestoneLine(event));
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
await sleep(resolvedPollMs);
|
|
146
|
+
}
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
const loopPromise = loop();
|
|
150
|
+
|
|
151
|
+
return {
|
|
152
|
+
stop: async () => {
|
|
153
|
+
stopped = true;
|
|
154
|
+
await loopPromise;
|
|
155
|
+
if (batchId) {
|
|
156
|
+
const events = readJournalEventsCached(projectRoot, batchId);
|
|
157
|
+
for (const event of events) {
|
|
158
|
+
const key = milestoneEventKey(event);
|
|
159
|
+
if (printed.has(key)) continue;
|
|
160
|
+
if (!ATTACHED_LAND_LOOP_MILESTONE_TYPES.has(String(event.type ?? ""))) continue;
|
|
161
|
+
printed.add(key);
|
|
162
|
+
write(formatAttachedMilestoneLine(event));
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
},
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Run an attached foreground engine while streaming land-loop journal milestones to stdout.
|
|
171
|
+
*
|
|
172
|
+
* @param {object} params
|
|
173
|
+
* @param {string} params.projectRoot
|
|
174
|
+
* @param {() => Promise<object>} params.runEngine
|
|
175
|
+
* @param {(line: string) => void} [params.write]
|
|
176
|
+
* @param {boolean} [params.force]
|
|
177
|
+
* @param {"start"|"resume"} [params.operation]
|
|
178
|
+
*/
|
|
179
|
+
export async function runAttachedBatchEngine({
|
|
180
|
+
projectRoot,
|
|
181
|
+
runEngine,
|
|
182
|
+
write,
|
|
183
|
+
spineBin,
|
|
184
|
+
_force = false,
|
|
185
|
+
operation = "resume",
|
|
186
|
+
}) {
|
|
187
|
+
if (operation === "start") {
|
|
188
|
+
const lock = enforceAttachedEngineSingleOwner({ projectRoot, force: false, operation: "start" });
|
|
189
|
+
if (!lock.ok) {
|
|
190
|
+
return {
|
|
191
|
+
ok: false,
|
|
192
|
+
exitCode: lock.exitCode ?? 1,
|
|
193
|
+
error: lock.error,
|
|
194
|
+
output: lock.output,
|
|
195
|
+
batchId: lock.batchId,
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
installAttachedExitFinalizeHandlers({ projectRoot, spineBin });
|
|
201
|
+
const reporter = await startAttachedMilestoneReporter({ projectRoot, write });
|
|
202
|
+
/** @type {{ stop: () => Promise<void> }} */
|
|
203
|
+
let parentMonitor = { stop: async () => {} };
|
|
204
|
+
if (process.env.SPINE_ALLOW_ATTACHED_HARNESS !== "1") {
|
|
205
|
+
parentMonitor = await startParentSessionMonitor({
|
|
206
|
+
projectRoot,
|
|
207
|
+
onParentDied: () => {
|
|
208
|
+
process.exit(1);
|
|
209
|
+
},
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
try {
|
|
213
|
+
const result = await runEngine();
|
|
214
|
+
const handoff = finalizeAttachedLandLoopBeforeExit({ projectRoot, spineBin, signal: "exit" });
|
|
215
|
+
if (handoff.handled && handoff.action === "finalized_in_process" && handoff.result) {
|
|
216
|
+
return handoff.result;
|
|
217
|
+
}
|
|
218
|
+
return result;
|
|
219
|
+
} finally {
|
|
220
|
+
await parentMonitor.stop();
|
|
221
|
+
await reporter.stop();
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* @param {object} result
|
|
227
|
+
* @param {import("./reconcile.mjs").ReconciliationResult} reconciliation
|
|
228
|
+
*/
|
|
229
|
+
function resolveAttachedBatchExitCode(result, reconciliation) {
|
|
230
|
+
if (reconciliation.phase === "merge_blocked") {
|
|
231
|
+
return result.exitCode ?? 1;
|
|
232
|
+
}
|
|
233
|
+
return result.exitCode ?? (result.ok ? 0 : 1);
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* @param {object} params
|
|
238
|
+
* @param {string} params.projectRoot
|
|
239
|
+
* @param {"start"|"resume"} params.operation
|
|
240
|
+
* @param {object} params.result
|
|
241
|
+
* @param {boolean} [params.json]
|
|
242
|
+
*/
|
|
243
|
+
export function formatAttachedBatchCliResult({ projectRoot, operation, result, json = false }) {
|
|
244
|
+
const loaded = loadSpineBatchState(projectRoot);
|
|
245
|
+
if (loaded.raw?.batchId) {
|
|
246
|
+
reconcilePausedResumeDoneInLane({
|
|
247
|
+
projectRoot,
|
|
248
|
+
state: loaded.raw,
|
|
249
|
+
batchId: String(loaded.raw.batchId),
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
const reconciliation = reconcileBatch({ projectRoot });
|
|
253
|
+
const mergeBlocked = reconciliation.phase === "merge_blocked";
|
|
254
|
+
|
|
255
|
+
if (json) {
|
|
256
|
+
return {
|
|
257
|
+
exitCode: resolveAttachedBatchExitCode(result, reconciliation),
|
|
258
|
+
output: `${JSON.stringify({ ...result, reconciliation }, null, 2)}\n`,
|
|
259
|
+
};
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
const operationLabel = operation === "resume" ? "resumed" : "started";
|
|
263
|
+
const headline = result.ok
|
|
264
|
+
? `Batch ${operationLabel}`
|
|
265
|
+
: mergeBlocked && reconciliation.headline
|
|
266
|
+
? reconciliation.headline
|
|
267
|
+
: operation === "resume"
|
|
268
|
+
? "Batch resume failed"
|
|
269
|
+
: "Batch start failed";
|
|
270
|
+
|
|
271
|
+
/** @type {string[]} */
|
|
272
|
+
const lines = ["", headline, ""];
|
|
273
|
+
|
|
274
|
+
if (result.output) {
|
|
275
|
+
lines.push(result.output.trimEnd());
|
|
276
|
+
} else if (result.error && !mergeBlocked) {
|
|
277
|
+
lines.push(String(result.error));
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
if (reconciliation.headline && reconciliation.headline !== headline) {
|
|
281
|
+
lines.push("", reconciliation.headline);
|
|
282
|
+
}
|
|
283
|
+
if (reconciliation.suggestedCommand) {
|
|
284
|
+
lines.push("", ` → ${reconciliation.suggestedCommand}`);
|
|
285
|
+
}
|
|
286
|
+
if (reconciliation.alternatives?.length) {
|
|
287
|
+
lines.push("", " Alternatives:");
|
|
288
|
+
for (const alt of reconciliation.alternatives) {
|
|
289
|
+
lines.push(` • ${alt}`);
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
if (result.batchId) {
|
|
294
|
+
lines.push("", ` Batch: ${result.batchId}`);
|
|
295
|
+
}
|
|
296
|
+
if (result.taskId) {
|
|
297
|
+
lines.push(` Task: ${result.taskId}`);
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
lines.push("");
|
|
301
|
+
return {
|
|
302
|
+
exitCode: resolveAttachedBatchExitCode(result, reconciliation),
|
|
303
|
+
output: lines.join("\n"),
|
|
304
|
+
};
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
/**
|
|
308
|
+
* Write attached CLI output and exit so the foreground process does not linger after batch.completed.
|
|
309
|
+
*
|
|
310
|
+
* @param {{ exitCode?: number, output?: string }} cli
|
|
311
|
+
* @param {object} [options]
|
|
312
|
+
* @param {boolean} [options.deferExit]
|
|
313
|
+
*/
|
|
314
|
+
export function finishAttachedBatchCli(cli, { deferExit = false, projectRoot, spineBin } = {}) {
|
|
315
|
+
const exitCode = cli.exitCode ?? 1;
|
|
316
|
+
if (cli.output) {
|
|
317
|
+
process.stdout.write(cli.output);
|
|
318
|
+
}
|
|
319
|
+
if (deferExit) {
|
|
320
|
+
return;
|
|
321
|
+
}
|
|
322
|
+
if (projectRoot) {
|
|
323
|
+
finalizeAttachedLandLoopBeforeExit({ projectRoot, spineBin, signal: "cli_exit" });
|
|
324
|
+
}
|
|
325
|
+
process.exit(exitCode);
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
/**
|
|
329
|
+
* Finalize post-merge limbo during resume without re-running workers.
|
|
330
|
+
*
|
|
331
|
+
* @param {object} params
|
|
332
|
+
* @param {string} params.projectRoot
|
|
333
|
+
* @param {object} params.state
|
|
334
|
+
* @param {string} params.batchId
|
|
335
|
+
* @param {string} params.orchBranch
|
|
336
|
+
* @param {string} [params.fromPhase]
|
|
337
|
+
* @param {boolean} [params.resumeForced]
|
|
338
|
+
* @returns {ReturnType<typeof finalizeResumedBatchForIntegrate>|null}
|
|
339
|
+
*/
|
|
340
|
+
export function finalizeResumePostMergeLimbo({
|
|
341
|
+
projectRoot,
|
|
342
|
+
state,
|
|
343
|
+
batchId,
|
|
344
|
+
orchBranch,
|
|
345
|
+
fromPhase = "running",
|
|
346
|
+
resumeForced = false,
|
|
347
|
+
}) {
|
|
348
|
+
if (String(state.phase ?? "") === "completed") {
|
|
349
|
+
return null;
|
|
350
|
+
}
|
|
351
|
+
if (!detectPostMergeLimboForResume({ projectRoot, state }) && !isPostMergeLimbo(state)) {
|
|
352
|
+
return null;
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
terminateStaleDetachedEngine({
|
|
356
|
+
projectRoot,
|
|
357
|
+
state,
|
|
358
|
+
batchId,
|
|
359
|
+
fromPhase,
|
|
360
|
+
});
|
|
361
|
+
saveSpineBatchState(projectRoot, state, { bypassWriteGuard: true });
|
|
362
|
+
|
|
363
|
+
return finalizeResumedBatchForIntegrate({
|
|
364
|
+
projectRoot,
|
|
365
|
+
state,
|
|
366
|
+
batchId,
|
|
367
|
+
orchBranch,
|
|
368
|
+
resumeForced,
|
|
369
|
+
});
|
|
370
|
+
}
|