pi-spine 2.25.0 → 2.26.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/package.json +1 -1
- package/src/batch/abort.mjs +74 -38
- package/src/batch/attached-engine-handoff.mjs +2 -2
- package/src/batch/attached-runner-promote.mjs +1 -1
- package/src/batch/attached-runner-reconcile.mjs +1 -1
- package/src/batch/batch-meta-reconstruct.mjs +6 -1
- package/src/batch/batch-state-lock.mjs +173 -31
- package/src/batch/contract-exec.mjs +46 -45
- package/src/batch/contract-spawn.mjs +225 -0
- package/src/batch/contract-verify.mjs +2 -1
- package/src/batch/detached-wait.mjs +1 -1
- package/src/batch/engine-lanes/matrix-run.mjs +6 -6
- package/src/batch/engine-lanes/merge.mjs +135 -34
- package/src/batch/engine-lanes/review-final.mjs +1 -1
- package/src/batch/engine-lanes.mjs +11 -9
- package/src/batch/engine.mjs +10 -10
- package/src/batch/lifecycle-archive.mjs +76 -1
- package/src/batch/lifecycle-cleanup.mjs +110 -0
- package/src/batch/lifecycle.mjs +100 -72
- package/src/batch/parent-session-monitor.mjs +77 -70
- package/src/batch/pause.mjs +92 -25
- package/src/batch/post-merge-limbo.mjs +35 -5
- package/src/batch/resume-gate-reopen.mjs +24 -8
- package/src/batch/resume-multi.mjs +1 -1
- package/src/batch/resume.mjs +1 -1
- package/src/batch/sequence-run.mjs +26 -8
- package/src/batch/sequence-wait.mjs +87 -8
- package/src/batch/state-guards.mjs +39 -7
- package/src/batch/state-io.mjs +131 -17
- package/src/batch/state.mjs +1 -0
- package/src/batch/worker-host.mjs +32 -4
- package/src/config/defaults.mjs +15 -1
- package/src/config/preflight/git-batch.mjs +47 -0
- package/src/config/spine-config-schema.mjs +67 -1
- package/src/fs/atomic-write.mjs +37 -1
|
@@ -25,6 +25,8 @@ import {
|
|
|
25
25
|
import { recordWaveMergeResult } from "../merge/wave-merge-state.mjs";
|
|
26
26
|
import { recordMergeBlocked } from "../lifecycle.mjs";
|
|
27
27
|
import { appendJournalEvent } from "../journal.mjs";
|
|
28
|
+
import { LANES_DEFAULTS } from "../../config/defaults.mjs";
|
|
29
|
+
import { loadSpineConfig } from "../../config/spine-config-load.mjs";
|
|
28
30
|
import { countCommitsAhead, gitPorcelain } from "../lane-commit.mjs";
|
|
29
31
|
import { saveSpineBatchState } from "../state.mjs";
|
|
30
32
|
import { loadTaskFileScopePaths } from "./queue.mjs";
|
|
@@ -54,6 +56,22 @@ function abortInProgressMerge(projectRoot) {
|
|
|
54
56
|
}
|
|
55
57
|
}
|
|
56
58
|
|
|
59
|
+
/**
|
|
60
|
+
* Stderr text of a failed gitExec error (execFileSync attaches piped stderr as a
|
|
61
|
+
* Buffer); falls back to the error message when stderr is empty.
|
|
62
|
+
*
|
|
63
|
+
* @param {unknown} err
|
|
64
|
+
* @returns {string}
|
|
65
|
+
*/
|
|
66
|
+
function gitStderrText(err) {
|
|
67
|
+
const stderr = /** @type {{ stderr?: unknown }} */ (err)?.stderr;
|
|
68
|
+
if (stderr != null) {
|
|
69
|
+
const text = Buffer.isBuffer(stderr) ? stderr.toString("utf-8") : String(stderr);
|
|
70
|
+
if (text.trim()) return text.trim();
|
|
71
|
+
}
|
|
72
|
+
return err instanceof Error ? err.message : String(err);
|
|
73
|
+
}
|
|
74
|
+
|
|
57
75
|
/**
|
|
58
76
|
* @param {string} projectRoot
|
|
59
77
|
*/
|
|
@@ -136,24 +154,46 @@ function pathInLaneFileScope(filePath, laneFileScopePaths) {
|
|
|
136
154
|
}
|
|
137
155
|
|
|
138
156
|
/**
|
|
139
|
-
*
|
|
140
|
-
*
|
|
141
|
-
*
|
|
142
|
-
* @
|
|
157
|
+
* Lane number for fail-closed messages, derived from the laneTaskBranch naming
|
|
158
|
+
* convention (`task/spine-lane-<n>-<batchId>`). Null for foreign branch names.
|
|
159
|
+
*
|
|
160
|
+
* @param {string | undefined} taskBranch
|
|
161
|
+
* @returns {number | null}
|
|
143
162
|
*/
|
|
144
|
-
function
|
|
145
|
-
const
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
163
|
+
function laneNumberFromTaskBranch(taskBranch) {
|
|
164
|
+
const match = typeof taskBranch === "string" ? /-lane-(\d+)-/.exec(taskBranch) : null;
|
|
165
|
+
return match ? Number(match[1]) : null;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Allow-listed out-of-scope paths keep prefer-orch auto-resolution (SP-798 / #304).
|
|
170
|
+
* Matched with the same glob helper `pathInLaneFileScope` uses so File Scope and
|
|
171
|
+
* allow-list patterns behave identically.
|
|
172
|
+
*
|
|
173
|
+
* @param {string} filePath
|
|
174
|
+
* @param {readonly string[] | string[]} allowList
|
|
175
|
+
*/
|
|
176
|
+
function pathInOutOfScopeAllowList(filePath, allowList) {
|
|
177
|
+
if (!Array.isArray(allowList)) return false;
|
|
178
|
+
return allowList.some(
|
|
179
|
+
(pattern) => typeof pattern === "string" && matchesContractPattern(filePath, pattern),
|
|
154
180
|
);
|
|
155
181
|
}
|
|
156
182
|
|
|
183
|
+
/**
|
|
184
|
+
* Load `lanes.outOfScopeMergeAllowList` from the repo config, falling back to
|
|
185
|
+
* LANES_DEFAULTS when the repo has no spine config (bare fixtures).
|
|
186
|
+
*
|
|
187
|
+
* @param {string} projectRoot
|
|
188
|
+
* @returns {string[]}
|
|
189
|
+
*/
|
|
190
|
+
function loadOutOfScopeMergeAllowList(projectRoot) {
|
|
191
|
+
const loaded = loadSpineConfig(projectRoot);
|
|
192
|
+
const configured = loaded.config?.lanes?.outOfScopeMergeAllowList;
|
|
193
|
+
if (Array.isArray(configured)) return configured.filter((pattern) => typeof pattern === "string");
|
|
194
|
+
return [...LANES_DEFAULTS.outOfScopeMergeAllowList];
|
|
195
|
+
}
|
|
196
|
+
|
|
157
197
|
/**
|
|
158
198
|
* @param {string} projectRoot
|
|
159
199
|
*/
|
|
@@ -185,24 +225,46 @@ function resolveRulesManifestMergeConflict(projectRoot) {
|
|
|
185
225
|
}
|
|
186
226
|
|
|
187
227
|
/**
|
|
188
|
-
*
|
|
228
|
+
* Auto-resolve an out-of-scope conflict only when the path is allow-listed via
|
|
229
|
+
* `lanes.outOfScopeMergeAllowList` (SP-798 / #304). A conflicted path is only ever
|
|
230
|
+
* conflicted because both sides changed it, so prefer-orch always discards lane work;
|
|
231
|
+
* the lane blob (`:3:<path>`) is captured first so the caller can journal what was
|
|
232
|
+
* dropped. Paths that are neither in File Scope nor allow-listed fail closed.
|
|
189
233
|
*
|
|
190
234
|
* @param {object} params
|
|
191
235
|
* @param {string} params.projectRoot
|
|
192
236
|
* @param {string} params.filePath
|
|
193
237
|
* @param {string[]} params.laneFileScopePaths
|
|
194
|
-
* @param {
|
|
238
|
+
* @param {string[]} params.outOfScopeAllowList
|
|
239
|
+
* @param {number | null} params.laneNumber
|
|
240
|
+
* @param {string} params.taskBranch
|
|
241
|
+
* @param {string} params.orchBranch
|
|
242
|
+
* @returns {{ ok: true, strategy: "prefer_orch_out_of_scope", discarded: { path: string, laneBlob: string | null }, skippedGitignoredPaths: string[] } | { ok: false, reason: string, failureClass?: string, error?: string }}
|
|
195
243
|
*/
|
|
196
244
|
function tryAutoResolveOutOfScopeMergeConflict({
|
|
197
245
|
projectRoot,
|
|
198
246
|
filePath,
|
|
199
247
|
laneFileScopePaths,
|
|
200
|
-
|
|
248
|
+
outOfScopeAllowList,
|
|
249
|
+
laneNumber,
|
|
250
|
+
taskBranch,
|
|
251
|
+
orchBranch,
|
|
201
252
|
}) {
|
|
202
253
|
if (pathInLaneFileScope(filePath, laneFileScopePaths)) {
|
|
203
254
|
return { ok: false, reason: "in_lane_file_scope" };
|
|
204
255
|
}
|
|
256
|
+
if (!pathInOutOfScopeAllowList(filePath, outOfScopeAllowList)) {
|
|
257
|
+
return {
|
|
258
|
+
ok: false,
|
|
259
|
+
reason: "not_allow_listed",
|
|
260
|
+
failureClass: "MergeConflict",
|
|
261
|
+
error:
|
|
262
|
+
`Lane ${laneNumber ?? "?"} (${taskBranch}) changed out-of-scope path ${filePath} ` +
|
|
263
|
+
`that also changed on ${orchBranch}; add it to File Scope or lanes.outOfScopeMergeAllowList`,
|
|
264
|
+
};
|
|
265
|
+
}
|
|
205
266
|
|
|
267
|
+
const laneBlob = git(projectRoot, ["rev-parse", `:3:${filePath}`], { throwOnError: false });
|
|
206
268
|
gitExec(projectRoot, ["checkout", "--ours", "--", filePath], { projectRoot });
|
|
207
269
|
const { stageable, skipped } = filterGitignoredPaths(projectRoot, [filePath]);
|
|
208
270
|
if (stageable.length > 0) {
|
|
@@ -216,6 +278,7 @@ function tryAutoResolveOutOfScopeMergeConflict({
|
|
|
216
278
|
return {
|
|
217
279
|
ok: true,
|
|
218
280
|
strategy: "prefer_orch_out_of_scope",
|
|
281
|
+
discarded: { path: filePath, laneBlob },
|
|
219
282
|
skippedGitignoredPaths: skipped,
|
|
220
283
|
};
|
|
221
284
|
}
|
|
@@ -252,7 +315,7 @@ function collectLaneWaveFileScope(state, laneNumber, waveTaskIds) {
|
|
|
252
315
|
* @param {string} [options.taskBranch]
|
|
253
316
|
* @param {string} [options.orchBranch]
|
|
254
317
|
* @param {number} [options.waveIndex]
|
|
255
|
-
* @returns {{ ok: true, autoResolved: true, generatedAt?: string, outOfScopePaths?: string[], adoptionDocPaths?: string[], skippedGitignoredPaths?: string[] } | { ok: false, error: string, failureClass?: string, outOfScopePaths?: string[], adoptionDocPaths?: string[], skippedGitignoredPaths?: string[] }}
|
|
318
|
+
* @returns {{ ok: true, autoResolved: true, generatedAt?: string, outOfScopePaths?: string[], adoptionDocPaths?: string[], discardedOutOfScope?: { path: string, laneBlob: string | null }[], skippedGitignoredPaths?: string[] } | { ok: false, error: string, failureClass?: string, outOfScopePaths?: string[], adoptionDocPaths?: string[], skippedGitignoredPaths?: string[] }}
|
|
256
319
|
*/
|
|
257
320
|
export function tryAutoResolveMergeConflicts(projectRoot, options = {}) {
|
|
258
321
|
const { laneFileScopePaths = [], taskBranch, orchBranch, waveIndex } = options;
|
|
@@ -275,17 +338,13 @@ export function tryAutoResolveMergeConflicts(projectRoot, options = {}) {
|
|
|
275
338
|
Boolean(taskBranch) &&
|
|
276
339
|
Boolean(orchBranch);
|
|
277
340
|
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
throwOnError: false,
|
|
283
|
-
});
|
|
284
|
-
if (mergeBase) {
|
|
285
|
-
laneChangedFiles = listBranchChangedFiles(projectRoot, mergeBase, /** @type {string} */ (taskBranch));
|
|
286
|
-
}
|
|
287
|
-
}
|
|
341
|
+
const outOfScopeAllowList = canResolveOutOfScope
|
|
342
|
+
? loadOutOfScopeMergeAllowList(projectRoot)
|
|
343
|
+
: [];
|
|
344
|
+
const laneNumber = laneNumberFromTaskBranch(taskBranch);
|
|
288
345
|
|
|
346
|
+
/** @type {{ path: string, laneBlob: string | null }[]} */
|
|
347
|
+
const discardedOutOfScope = [];
|
|
289
348
|
/** @type {string[]} */
|
|
290
349
|
const resolvedOutOfScope = [];
|
|
291
350
|
/** @type {string[]} */
|
|
@@ -335,13 +394,26 @@ export function tryAutoResolveMergeConflicts(projectRoot, options = {}) {
|
|
|
335
394
|
projectRoot,
|
|
336
395
|
filePath,
|
|
337
396
|
laneFileScopePaths,
|
|
338
|
-
|
|
397
|
+
outOfScopeAllowList,
|
|
398
|
+
laneNumber,
|
|
399
|
+
taskBranch: /** @type {string} */ (taskBranch),
|
|
400
|
+
orchBranch: /** @type {string} */ (orchBranch),
|
|
339
401
|
});
|
|
340
402
|
if (resolved.ok) {
|
|
341
403
|
resolvedOutOfScope.push(filePath);
|
|
404
|
+
discardedOutOfScope.push(resolved.discarded);
|
|
342
405
|
if (Array.isArray(resolved.skippedGitignoredPaths) && resolved.skippedGitignoredPaths.length > 0) {
|
|
343
406
|
skippedGitignoredPaths.push(...resolved.skippedGitignoredPaths);
|
|
344
407
|
}
|
|
408
|
+
} else if (resolved.reason === "not_allow_listed") {
|
|
409
|
+
// Lane-committed out-of-scope conflict — fail closed instead of discarding (#304).
|
|
410
|
+
return {
|
|
411
|
+
ok: false,
|
|
412
|
+
failureClass: "MergeConflict",
|
|
413
|
+
error: /** @type {string} */ (resolved.error),
|
|
414
|
+
outOfScopePaths: resolvedOutOfScope,
|
|
415
|
+
adoptionDocPaths: resolvedAutoMergeDocs,
|
|
416
|
+
};
|
|
345
417
|
} else {
|
|
346
418
|
remaining.push(filePath);
|
|
347
419
|
}
|
|
@@ -356,6 +428,7 @@ export function tryAutoResolveMergeConflicts(projectRoot, options = {}) {
|
|
|
356
428
|
return {
|
|
357
429
|
...manifestResult,
|
|
358
430
|
outOfScopePaths: resolvedOutOfScope,
|
|
431
|
+
discardedOutOfScope,
|
|
359
432
|
skippedGitignoredPaths,
|
|
360
433
|
};
|
|
361
434
|
}
|
|
@@ -372,6 +445,7 @@ export function tryAutoResolveMergeConflicts(projectRoot, options = {}) {
|
|
|
372
445
|
return {
|
|
373
446
|
...manifestResult,
|
|
374
447
|
outOfScopePaths: resolvedOutOfScope,
|
|
448
|
+
discardedOutOfScope,
|
|
375
449
|
skippedGitignoredPaths,
|
|
376
450
|
};
|
|
377
451
|
}
|
|
@@ -382,6 +456,7 @@ export function tryAutoResolveMergeConflicts(projectRoot, options = {}) {
|
|
|
382
456
|
autoResolved: true,
|
|
383
457
|
outOfScopePaths: resolvedOutOfScope,
|
|
384
458
|
adoptionDocPaths: resolvedAutoMergeDocs,
|
|
459
|
+
discardedOutOfScope,
|
|
385
460
|
skippedGitignoredPaths,
|
|
386
461
|
};
|
|
387
462
|
}
|
|
@@ -408,8 +483,8 @@ export function tryAutoResolveMergeConflicts(projectRoot, options = {}) {
|
|
|
408
483
|
failureClass: "MergeConflict",
|
|
409
484
|
error:
|
|
410
485
|
`merge conflict on ${remaining.join(", ")}; automatic resolution supports docs/adoption/*, ` +
|
|
411
|
-
`docs/PRD.md, ${RULES_MANIFEST_REL_PATH}
|
|
412
|
-
"
|
|
486
|
+
`docs/PRD.md, and ${RULES_MANIFEST_REL_PATH}; out-of-scope conflicts are only ` +
|
|
487
|
+
"auto-resolved when allow-listed via lanes.outOfScopeMergeAllowList",
|
|
413
488
|
outOfScopePaths: resolvedOutOfScope,
|
|
414
489
|
adoptionDocPaths: resolvedAutoMergeDocs,
|
|
415
490
|
};
|
|
@@ -496,7 +571,7 @@ function resolveRulesManifestPreMergeDrift(projectRoot) {
|
|
|
496
571
|
* @param {string[]} [params.laneFileScopePaths]
|
|
497
572
|
* @param {string[]} [params.laneTaskFolders]
|
|
498
573
|
* @param {number} [params.waveIndex]
|
|
499
|
-
* @returns {{ ok: true, mergeCommit: string, commitsAhead: number } | { ok: false, failureClass?: string, error: string, outOfScopePaths?: string[], skippedGitignoredPaths?: string[] }}
|
|
574
|
+
* @returns {{ ok: true, mergeCommit: string, commitsAhead: number, discardedOutOfScope: { path: string, laneBlob: string | null }[] } | { ok: false, failureClass?: string, error: string, outOfScopePaths?: string[], skippedGitignoredPaths?: string[] }}
|
|
500
575
|
*/
|
|
501
576
|
export function mergeLaneToOrch({
|
|
502
577
|
projectRoot,
|
|
@@ -511,6 +586,8 @@ export function mergeLaneToOrch({
|
|
|
511
586
|
}) {
|
|
512
587
|
const previous = gitStrict(projectRoot, ["rev-parse", "--abbrev-ref", "HEAD"]);
|
|
513
588
|
let mergeInProgress = false;
|
|
589
|
+
/** @type {{ path: string, laneBlob: string | null }[]} */
|
|
590
|
+
let discardedOutOfScope = [];
|
|
514
591
|
try {
|
|
515
592
|
const drift = resolveRulesManifestPreMergeDrift(projectRoot);
|
|
516
593
|
if (!drift.ok) {
|
|
@@ -558,7 +635,17 @@ export function mergeLaneToOrch({
|
|
|
558
635
|
"-m",
|
|
559
636
|
`merge ${taskBranch} into ${orchBranch}`,
|
|
560
637
|
]);
|
|
561
|
-
} catch {
|
|
638
|
+
} catch (mergeErr) {
|
|
639
|
+
// SP-798 / #304: only real conflicts (unmerged index entries) may run the
|
|
640
|
+
// auto-resolver; hook/lock/untracked-overwrite failures are MergeFailed.
|
|
641
|
+
if (listUnmergedPaths(projectRoot).length === 0) {
|
|
642
|
+
abortInProgressMerge(projectRoot);
|
|
643
|
+
return {
|
|
644
|
+
ok: false,
|
|
645
|
+
failureClass: "MergeFailed",
|
|
646
|
+
error: gitStderrText(mergeErr),
|
|
647
|
+
};
|
|
648
|
+
}
|
|
562
649
|
mergeInProgress = true;
|
|
563
650
|
const autoResolved = tryAutoResolveMergeConflicts(projectRoot, {
|
|
564
651
|
laneFileScopePaths,
|
|
@@ -584,6 +671,9 @@ export function mergeLaneToOrch({
|
|
|
584
671
|
skippedPaths: autoResolved.skippedGitignoredPaths,
|
|
585
672
|
});
|
|
586
673
|
}
|
|
674
|
+
discardedOutOfScope = Array.isArray(autoResolved.discardedOutOfScope)
|
|
675
|
+
? autoResolved.discardedOutOfScope
|
|
676
|
+
: [];
|
|
587
677
|
gitStrict(projectRoot, ["commit", "--no-edit"]);
|
|
588
678
|
}
|
|
589
679
|
|
|
@@ -599,7 +689,7 @@ export function mergeLaneToOrch({
|
|
|
599
689
|
};
|
|
600
690
|
}
|
|
601
691
|
|
|
602
|
-
return { ok: true, mergeCommit, commitsAhead };
|
|
692
|
+
return { ok: true, mergeCommit, commitsAhead, discardedOutOfScope };
|
|
603
693
|
} catch (err) {
|
|
604
694
|
if (mergeInProgress) {
|
|
605
695
|
abortInProgressMerge(projectRoot);
|
|
@@ -749,6 +839,17 @@ export function mergeWaveLanesToOrch({
|
|
|
749
839
|
};
|
|
750
840
|
}
|
|
751
841
|
lastMergeCommit = merge.mergeCommit;
|
|
842
|
+
if (Array.isArray(merge.discardedOutOfScope) && merge.discardedOutOfScope.length > 0) {
|
|
843
|
+
appendJournalEvent(projectRoot, batchId, "batch.merge_out_of_scope_discarded", {
|
|
844
|
+
laneNumber,
|
|
845
|
+
payload: {
|
|
846
|
+
laneNumber,
|
|
847
|
+
taskBranch,
|
|
848
|
+
paths: merge.discardedOutOfScope.map((entry) => entry.path),
|
|
849
|
+
laneBlobs: merge.discardedOutOfScope.map((entry) => entry.laneBlob),
|
|
850
|
+
},
|
|
851
|
+
});
|
|
852
|
+
}
|
|
752
853
|
appendJournalEvent(projectRoot, batchId, "batch.merge_completed", {
|
|
753
854
|
mergeCommit: merge.mergeCommit,
|
|
754
855
|
laneNumber,
|
|
@@ -301,7 +301,7 @@ export async function runFinalReviewPhase({
|
|
|
301
301
|
batchId,
|
|
302
302
|
worktreePath: wt,
|
|
303
303
|
});
|
|
304
|
-
contractVerifyResult = verifyContract(wt, parsedContract, {
|
|
304
|
+
contractVerifyResult = await verifyContract(wt, parsedContract, {
|
|
305
305
|
...config,
|
|
306
306
|
baseBranch,
|
|
307
307
|
sinceCommit: sinceCommit ?? undefined,
|
|
@@ -226,7 +226,7 @@ async function runNonMatrixTaskOnLane({
|
|
|
226
226
|
if (!task.startedAt) task.startedAt = Date.now();
|
|
227
227
|
updateSegmentForTask(state, taskId, "failed");
|
|
228
228
|
recomputeTaskCounters(state);
|
|
229
|
-
saveEngineBatchState(projectRoot, state);
|
|
229
|
+
await saveEngineBatchState(projectRoot, state);
|
|
230
230
|
appendJournalEvent(projectRoot, batchId, "lane.orch_sync_failed", {
|
|
231
231
|
taskId,
|
|
232
232
|
laneNumber,
|
|
@@ -276,7 +276,7 @@ async function runNonMatrixTaskOnLane({
|
|
|
276
276
|
task.status = "running";
|
|
277
277
|
if (!task.startedAt) task.startedAt = Date.now();
|
|
278
278
|
updateSegmentForTask(state, taskId, "running");
|
|
279
|
-
saveEngineBatchState(projectRoot, state);
|
|
279
|
+
await saveEngineBatchState(projectRoot, state);
|
|
280
280
|
appendJournalEvent(projectRoot, batchId, "task.started", {
|
|
281
281
|
taskId,
|
|
282
282
|
laneNumber,
|
|
@@ -295,14 +295,16 @@ async function runNonMatrixTaskOnLane({
|
|
|
295
295
|
laneCorrelationId,
|
|
296
296
|
fileScopePaths,
|
|
297
297
|
config,
|
|
298
|
-
|
|
298
|
+
// Fire-and-forget saves (SP-797 / #302): the host never awaits these callbacks.
|
|
299
|
+
// Safe — the save serializes live state at lock-acquire time, never a stale snapshot.
|
|
300
|
+
onHeartbeat: async (timestamp) => {
|
|
299
301
|
lane.lastHeartbeatAt = timestamp;
|
|
300
|
-
saveEngineBatchState(projectRoot, state);
|
|
302
|
+
await saveEngineBatchState(projectRoot, state);
|
|
301
303
|
},
|
|
302
|
-
onWorkerPid: (pid) => {
|
|
304
|
+
onWorkerPid: async (pid) => {
|
|
303
305
|
if (pid > 0) {
|
|
304
306
|
lane.workerPid = pid;
|
|
305
|
-
saveEngineBatchState(projectRoot, state);
|
|
307
|
+
await saveEngineBatchState(projectRoot, state);
|
|
306
308
|
}
|
|
307
309
|
},
|
|
308
310
|
}));
|
|
@@ -323,7 +325,7 @@ async function runNonMatrixTaskOnLane({
|
|
|
323
325
|
});
|
|
324
326
|
if (lane.workerPid) {
|
|
325
327
|
delete lane.workerPid;
|
|
326
|
-
saveEngineBatchState(projectRoot, state);
|
|
328
|
+
await saveEngineBatchState(projectRoot, state);
|
|
327
329
|
}
|
|
328
330
|
} else {
|
|
329
331
|
const aborted = workerResult.classification === "aborted";
|
|
@@ -339,7 +341,7 @@ async function runNonMatrixTaskOnLane({
|
|
|
339
341
|
task.exitReason = workerResult.classification ?? "worker_failed";
|
|
340
342
|
updateSegmentForTask(state, taskId, aborted ? "aborted" : "failed");
|
|
341
343
|
recomputeTaskCounters(state);
|
|
342
|
-
saveEngineBatchState(projectRoot, state);
|
|
344
|
+
await saveEngineBatchState(projectRoot, state);
|
|
343
345
|
if (!aborted) {
|
|
344
346
|
const salvageFields = recordTaskFailureSalvage({
|
|
345
347
|
projectRoot,
|
|
@@ -378,7 +380,7 @@ async function runNonMatrixTaskOnLane({
|
|
|
378
380
|
|
|
379
381
|
if (lane.workerPid) {
|
|
380
382
|
delete lane.workerPid;
|
|
381
|
-
saveEngineBatchState(projectRoot, state);
|
|
383
|
+
await saveEngineBatchState(projectRoot, state);
|
|
382
384
|
}
|
|
383
385
|
|
|
384
386
|
appendJournalEvent(projectRoot, batchId, "lane.completed", {
|
package/src/batch/engine.mjs
CHANGED
|
@@ -193,7 +193,7 @@ export async function startBatch({
|
|
|
193
193
|
});
|
|
194
194
|
|
|
195
195
|
recordBatchBaseSnapshotOnStart(projectRoot, state);
|
|
196
|
-
saveEngineBatchState(projectRoot, state);
|
|
196
|
+
await saveEngineBatchState(projectRoot, state);
|
|
197
197
|
persistBatchMetaFromStartState(projectRoot, state, /** @type {string} */ (tasksRoot));
|
|
198
198
|
|
|
199
199
|
try {
|
|
@@ -201,7 +201,7 @@ export async function startBatch({
|
|
|
201
201
|
transitionPhase(state, "running", { projectRoot, batchId });
|
|
202
202
|
recordBatchEnginePid(state, process.pid);
|
|
203
203
|
installAttachedEngineShutdownHandlers({ projectRoot });
|
|
204
|
-
saveEngineBatchState(projectRoot, state);
|
|
204
|
+
await saveEngineBatchState(projectRoot, state);
|
|
205
205
|
|
|
206
206
|
for (const lane of state.lanes) {
|
|
207
207
|
lane.correlationId = crypto.randomUUID();
|
|
@@ -258,21 +258,21 @@ export async function startBatch({
|
|
|
258
258
|
correlationId: lane.correlationId,
|
|
259
259
|
});
|
|
260
260
|
}
|
|
261
|
-
saveEngineBatchState(projectRoot, state);
|
|
261
|
+
await saveEngineBatchState(projectRoot, state);
|
|
262
262
|
|
|
263
263
|
let batchAborted = false;
|
|
264
264
|
|
|
265
265
|
for (const wave of effectivePlan.waves) {
|
|
266
|
-
const pauseAtWave = adoptPauseIfRequested({ projectRoot, state, batchId });
|
|
266
|
+
const pauseAtWave = await adoptPauseIfRequested({ projectRoot, state, batchId });
|
|
267
267
|
if (pauseAtWave.stop) {
|
|
268
268
|
return buildEnginePausedResult(batchId);
|
|
269
269
|
}
|
|
270
270
|
|
|
271
271
|
state.currentWaveIndex = wave.index;
|
|
272
|
-
saveEngineBatchState(projectRoot, state);
|
|
272
|
+
await saveEngineBatchState(projectRoot, state);
|
|
273
273
|
|
|
274
274
|
for (const tick of wave.ticks ?? []) {
|
|
275
|
-
const pauseAtTick = adoptPauseIfRequested({ projectRoot, state, batchId });
|
|
275
|
+
const pauseAtTick = await adoptPauseIfRequested({ projectRoot, state, batchId });
|
|
276
276
|
if (pauseAtTick.stop) {
|
|
277
277
|
return buildEnginePausedResult(batchId);
|
|
278
278
|
}
|
|
@@ -359,7 +359,7 @@ export async function startBatch({
|
|
|
359
359
|
await Promise.all(laneExecutions);
|
|
360
360
|
if (batchAborted) break;
|
|
361
361
|
|
|
362
|
-
const pauseAfterTick = adoptPauseIfRequested({ projectRoot, state, batchId });
|
|
362
|
+
const pauseAfterTick = await adoptPauseIfRequested({ projectRoot, state, batchId });
|
|
363
363
|
if (pauseAfterTick.stop) {
|
|
364
364
|
return buildEnginePausedResult(batchId);
|
|
365
365
|
}
|
|
@@ -374,7 +374,7 @@ export async function startBatch({
|
|
|
374
374
|
batchId,
|
|
375
375
|
extra: { taskId: abortedTask?.taskId },
|
|
376
376
|
});
|
|
377
|
-
saveEngineBatchState(projectRoot, state);
|
|
377
|
+
await saveEngineBatchState(projectRoot, state);
|
|
378
378
|
return {
|
|
379
379
|
ok: false,
|
|
380
380
|
exitCode: 1,
|
|
@@ -398,7 +398,7 @@ export async function startBatch({
|
|
|
398
398
|
reason: "mixed_outcome",
|
|
399
399
|
},
|
|
400
400
|
});
|
|
401
|
-
saveEngineBatchState(projectRoot, state);
|
|
401
|
+
await saveEngineBatchState(projectRoot, state);
|
|
402
402
|
appendJournalEvent(projectRoot, batchId, "batch.merge_blocked", {
|
|
403
403
|
waveIndex: wave.index,
|
|
404
404
|
failedTaskIds: mergeEligibility.failedTaskIds,
|
|
@@ -481,7 +481,7 @@ export async function startBatch({
|
|
|
481
481
|
batchId,
|
|
482
482
|
extra: { error: message },
|
|
483
483
|
});
|
|
484
|
-
saveEngineBatchState(projectRoot, state);
|
|
484
|
+
await saveEngineBatchState(projectRoot, state);
|
|
485
485
|
removeAllMatrixSubLaneWorktrees(projectRoot, batchId);
|
|
486
486
|
removeLaneWorktrees(projectRoot, batchId, maxLaneNumber);
|
|
487
487
|
return { ok: false, exitCode: 1, batchId, error: message };
|
|
@@ -6,9 +6,84 @@
|
|
|
6
6
|
import fs from "node:fs";
|
|
7
7
|
import path from "node:path";
|
|
8
8
|
import { appendJournalEvent } from "./journal.mjs";
|
|
9
|
-
import {
|
|
9
|
+
import {
|
|
10
|
+
clearActiveBatchStateIfMatches,
|
|
11
|
+
loadBatchStateFile,
|
|
12
|
+
readBatchStateId,
|
|
13
|
+
} from "./batch-state-io.mjs";
|
|
10
14
|
import { removeLaneWorktrees, maxLaneNumberFromBatchState } from "./worktree.mjs";
|
|
11
15
|
|
|
16
|
+
/**
|
|
17
|
+
* Phases a resume/pause/engine writer can leave behind between a terminal
|
|
18
|
+
* write's pre-lock read and its lock section (SP-792 / #301). When the state
|
|
19
|
+
* re-read inside the lock holds one of these, the batch is active again and
|
|
20
|
+
* the pre-lock dismiss/complete decision no longer holds — fail closed.
|
|
21
|
+
* `executing` is excluded because it is a limbo phase that legitimately
|
|
22
|
+
* allows dismiss (limbo_stale).
|
|
23
|
+
*/
|
|
24
|
+
const ACTIVE_RESUME_PHASES = new Set(["planning", "running", "paused", "merging"]);
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Re-read the active batch state inside the terminal lock section
|
|
28
|
+
* (SP-792 / #301) so archive, history, and clear act on the freshest on-disk
|
|
29
|
+
* snapshot instead of the pre-lock read, which goes stale when a concurrent
|
|
30
|
+
* engine save lands between the read and the lock.
|
|
31
|
+
*
|
|
32
|
+
* Fail-closed contract: returns a ready-to-return non-ok `refusal` when the
|
|
33
|
+
* state file vanished, no longer parses, now describes a different batch, or
|
|
34
|
+
* — with `requireTerminalPhase` — the batch became active again mid-write.
|
|
35
|
+
* Abort passes `requireTerminalPhase: false` because aborting a running
|
|
36
|
+
* batch is its normal use case.
|
|
37
|
+
*
|
|
38
|
+
* @param {object} params
|
|
39
|
+
* @param {string} params.projectRoot
|
|
40
|
+
* @param {string|null} params.batchStatePath path used by the pre-lock read
|
|
41
|
+
* @param {string} params.batchId batch id the reloaded state must still match
|
|
42
|
+
* @param {"abort"|"dismiss"|"complete"} params.action action name for the refusal headline
|
|
43
|
+
* @param {boolean} [params.requireTerminalPhase] re-validate the terminal-phase precondition on the in-lock state
|
|
44
|
+
* @param {boolean} [params.force] `dismiss --force` skips the phase re-validation
|
|
45
|
+
* @param {string|null} [params.diagnosis] pre-lock diagnosis echoed on refusal
|
|
46
|
+
* @returns {{ state: { path: string, raw: object }|null, refusal: object|null }}
|
|
47
|
+
*/
|
|
48
|
+
export function reloadStateForTerminalWrite({
|
|
49
|
+
projectRoot,
|
|
50
|
+
batchStatePath,
|
|
51
|
+
batchId,
|
|
52
|
+
action,
|
|
53
|
+
requireTerminalPhase = false,
|
|
54
|
+
force = false,
|
|
55
|
+
diagnosis = null,
|
|
56
|
+
}) {
|
|
57
|
+
const refuse = () => ({
|
|
58
|
+
ok: false,
|
|
59
|
+
exitCode: 1,
|
|
60
|
+
error: "batch_state_changed_during_terminal_write",
|
|
61
|
+
headline: `Batch state changed during ${action} — re-run spine status --diagnose`,
|
|
62
|
+
suggestedCommand: "spine status --diagnose",
|
|
63
|
+
diagnosis: diagnosis ?? null,
|
|
64
|
+
batchId,
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
const reloaded = loadBatchStateFile(projectRoot, batchStatePath);
|
|
68
|
+
if (!reloaded.path || !reloaded.raw || reloaded.parseError) {
|
|
69
|
+
return { state: null, refusal: refuse() };
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const reloadedBatchId = readBatchStateId(reloaded.raw);
|
|
73
|
+
if (!reloadedBatchId || reloadedBatchId !== batchId) {
|
|
74
|
+
return { state: null, refusal: refuse() };
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (requireTerminalPhase && !force) {
|
|
78
|
+
const phase = String(/** @type {{ phase?: string }} */ (reloaded.raw).phase ?? "");
|
|
79
|
+
if (ACTIVE_RESUME_PHASES.has(phase)) {
|
|
80
|
+
return { state: null, refusal: refuse() };
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
return { state: { path: reloaded.path, raw: reloaded.raw }, refusal: null };
|
|
85
|
+
}
|
|
86
|
+
|
|
12
87
|
/**
|
|
13
88
|
* @param {string} projectRoot
|
|
14
89
|
* @param {string} batchId
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Post-release cleanup for terminal batch lifecycle writes (SP-796 / #302).
|
|
3
|
+
*
|
|
4
|
+
* abort/complete/dismiss hold the global batch-state lock only for state
|
|
5
|
+
* I/O (in-lock reload, archive write, history append, terminal journal
|
|
6
|
+
* event, active-state clear). Post-mortem, metrics, worker termination and
|
|
7
|
+
* worktree cleanup run after the lock is released so concurrent engine
|
|
8
|
+
* saves are never blocked behind them.
|
|
9
|
+
*
|
|
10
|
+
* A failure in this phase must never throw: the batch state was already
|
|
11
|
+
* archived and journaled terminal, so an exception could only turn a
|
|
12
|
+
* finished batch into a reported failure. Each failed step is reported in
|
|
13
|
+
* the caller's `cleanupWarnings` and journaled as `batch.cleanup_failed`
|
|
14
|
+
* `{ step, error }` instead.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import { loadSpineConfig } from "../config/spine-config-load.mjs";
|
|
18
|
+
import { appendJournalEvent } from "./journal.mjs";
|
|
19
|
+
import { cleanupBatchLaneWorktrees } from "./lifecycle-archive.mjs";
|
|
20
|
+
import { recordBatchTerminalMetric } from "./metrics.mjs";
|
|
21
|
+
import { writeBatchPostMortem } from "./postmortem.mjs";
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Run named cleanup steps after the batch-state lock was released. Steps run
|
|
25
|
+
* in order; a failing step records a warning and journals `batch.cleanup_failed`
|
|
26
|
+
* but never stops the remaining steps or throws to the caller.
|
|
27
|
+
*
|
|
28
|
+
* @param {object} params
|
|
29
|
+
* @param {string} params.projectRoot
|
|
30
|
+
* @param {string} params.batchId
|
|
31
|
+
* @param {Array<{ step: string, run: () => void }>} params.steps
|
|
32
|
+
* @returns {string[]} `cleanupWarnings` — one `"<step>: <error>"` per failed step
|
|
33
|
+
*/
|
|
34
|
+
export function runPostReleaseCleanup({ projectRoot, batchId, steps }) {
|
|
35
|
+
const warnings = [];
|
|
36
|
+
for (const { step, run } of steps) {
|
|
37
|
+
try {
|
|
38
|
+
run();
|
|
39
|
+
} catch (err) {
|
|
40
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
41
|
+
warnings.push(`${step}: ${message}`);
|
|
42
|
+
try {
|
|
43
|
+
appendJournalEvent(projectRoot, batchId, "batch.cleanup_failed", {
|
|
44
|
+
step,
|
|
45
|
+
error: message,
|
|
46
|
+
});
|
|
47
|
+
} catch {
|
|
48
|
+
// Journal I/O failing while reporting a cleanup failure must not
|
|
49
|
+
// mask the terminal result — the warning string still carries it.
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return warnings;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Post-release cleanup shared by dismiss and complete (SP-796 / #302):
|
|
58
|
+
* post-mortem write, terminal metrics, and lane-worktree cleanup — all after
|
|
59
|
+
* the in-lock section archived the state and journaled the terminal event.
|
|
60
|
+
*
|
|
61
|
+
* @param {object} params
|
|
62
|
+
* @param {string} params.projectRoot
|
|
63
|
+
* @param {string} params.batchId
|
|
64
|
+
* @param {object} params.batchState the in-lock snapshot that was archived
|
|
65
|
+
* @param {import("./reconcile.mjs").ReconciliationResult} [params.reconciliation]
|
|
66
|
+
* @param {string} params.diagnosis
|
|
67
|
+
* @param {number} params.endedAt
|
|
68
|
+
* @returns {string[]} `cleanupWarnings`
|
|
69
|
+
*/
|
|
70
|
+
export function runLifecycleCleanupAfterRelease({
|
|
71
|
+
projectRoot,
|
|
72
|
+
batchId,
|
|
73
|
+
batchState,
|
|
74
|
+
reconciliation,
|
|
75
|
+
diagnosis,
|
|
76
|
+
endedAt,
|
|
77
|
+
}) {
|
|
78
|
+
return runPostReleaseCleanup({
|
|
79
|
+
projectRoot,
|
|
80
|
+
batchId,
|
|
81
|
+
steps: [
|
|
82
|
+
{
|
|
83
|
+
step: "post_mortem",
|
|
84
|
+
run: () => {
|
|
85
|
+
writeBatchPostMortem({ projectRoot, batchState, reconciliation });
|
|
86
|
+
},
|
|
87
|
+
},
|
|
88
|
+
{
|
|
89
|
+
step: "metrics",
|
|
90
|
+
run: () => {
|
|
91
|
+
const config = loadSpineConfig(projectRoot).config ?? {};
|
|
92
|
+
recordBatchTerminalMetric({
|
|
93
|
+
projectRoot,
|
|
94
|
+
batchId,
|
|
95
|
+
batchState: { ...batchState, endedAt },
|
|
96
|
+
diagnosis,
|
|
97
|
+
config,
|
|
98
|
+
});
|
|
99
|
+
},
|
|
100
|
+
},
|
|
101
|
+
{
|
|
102
|
+
step: "worktrees",
|
|
103
|
+
run: () => {
|
|
104
|
+
const config = loadSpineConfig(projectRoot).config ?? {};
|
|
105
|
+
cleanupBatchLaneWorktrees({ projectRoot, batchId, batchState, config });
|
|
106
|
+
},
|
|
107
|
+
},
|
|
108
|
+
],
|
|
109
|
+
});
|
|
110
|
+
}
|