pi-spine 2.24.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.
Files changed (53) hide show
  1. package/bin/spine-worker-runner.mjs +2 -1
  2. package/package.json +1 -1
  3. package/src/batch/abort.mjs +74 -38
  4. package/src/batch/attached-engine-handoff.mjs +2 -2
  5. package/src/batch/attached-runner-promote.mjs +15 -1
  6. package/src/batch/attached-runner-reconcile.mjs +1 -1
  7. package/src/batch/batch-meta-reconstruct.mjs +6 -1
  8. package/src/batch/batch-state-io.mjs +120 -45
  9. package/src/batch/batch-state-lock.mjs +173 -31
  10. package/src/batch/contract-exec.mjs +46 -45
  11. package/src/batch/contract-spawn.mjs +225 -0
  12. package/src/batch/contract-verify.mjs +2 -1
  13. package/src/batch/detached-wait.mjs +1 -1
  14. package/src/batch/engine-crash-guard.mjs +143 -0
  15. package/src/batch/engine-lanes/matrix-run.mjs +34 -8
  16. package/src/batch/engine-lanes/matrix.mjs +143 -8
  17. package/src/batch/engine-lanes/merge.mjs +135 -34
  18. package/src/batch/engine-lanes/review-final.mjs +1 -1
  19. package/src/batch/engine-lanes.mjs +11 -9
  20. package/src/batch/engine.mjs +10 -10
  21. package/src/batch/integrate-dirty-overlap.mjs +35 -0
  22. package/src/batch/integrate-worktree.mjs +106 -13
  23. package/src/batch/integrate.mjs +34 -93
  24. package/src/batch/journal-checksum.mjs +26 -0
  25. package/src/batch/journal.mjs +32 -13
  26. package/src/batch/lifecycle-archive.mjs +84 -3
  27. package/src/batch/lifecycle-cleanup.mjs +110 -0
  28. package/src/batch/lifecycle.mjs +100 -72
  29. package/src/batch/parent-session-monitor.mjs +77 -70
  30. package/src/batch/pause.mjs +92 -25
  31. package/src/batch/post-merge-limbo.mjs +35 -5
  32. package/src/batch/reconcile-batch.mjs +13 -1
  33. package/src/batch/resume-gate-reopen.mjs +24 -8
  34. package/src/batch/resume-multi.mjs +1 -1
  35. package/src/batch/resume.mjs +1 -1
  36. package/src/batch/salvage-batch-integrate.mjs +13 -17
  37. package/src/batch/sequence-run.mjs +26 -8
  38. package/src/batch/sequence-wait.mjs +87 -8
  39. package/src/batch/state-guards.mjs +39 -7
  40. package/src/batch/state-io.mjs +131 -17
  41. package/src/batch/state.mjs +23 -1
  42. package/src/batch/worker-heartbeat.mjs +29 -8
  43. package/src/batch/worker-host.mjs +61 -8
  44. package/src/batch/worker-output.mjs +9 -6
  45. package/src/batch/worker-prompt.mjs +4 -2
  46. package/src/batch/worker-spawn.mjs +99 -12
  47. package/src/config/defaults.mjs +15 -1
  48. package/src/config/preflight/discovery.mjs +20 -12
  49. package/src/config/preflight/git-batch.mjs +47 -0
  50. package/src/config/spine-config-schema.mjs +67 -1
  51. package/src/fs/atomic-write.mjs +37 -1
  52. package/src/planner/scope.mjs +3 -1
  53. package/src/tasks/packet/discover.mjs +59 -3
@@ -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
- * @param {string} projectRoot
140
- * @param {string} mergeBase
141
- * @param {string} taskBranch
142
- * @returns {Set<string>}
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 listBranchChangedFiles(projectRoot, mergeBase, taskBranch) {
145
- const output = git(projectRoot, ["diff", "--name-only", `${mergeBase}..${taskBranch}`], {
146
- throwOnError: false,
147
- });
148
- if (!output) return new Set();
149
- return new Set(
150
- output
151
- .split("\n")
152
- .map((line) => line.trim())
153
- .filter(Boolean),
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
- * Prefer orch (merge --ours) for stale dependency artifacts outside lane file scope.
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 {Set<string>} params.laneChangedFiles
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
- laneChangedFiles: _laneChangedFiles,
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
- /** @type {Set<string>} */
279
- let laneChangedFiles = new Set();
280
- if (canResolveOutOfScope) {
281
- const mergeBase = git(projectRoot, ["merge-base", /** @type {string} */ (orchBranch), /** @type {string} */ (taskBranch)], {
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
- laneChangedFiles,
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}, and out-of-scope dependency drift ` +
412
- "(prefer orch when the lane did not commit the path)",
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
- onHeartbeat: (timestamp) => {
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", {
@@ -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 };
@@ -0,0 +1,35 @@
1
+ import { appendJournalEvent } from "./journal.mjs";
2
+
3
+ /**
4
+ * Journal merged paths the sync skipped (uncommitted operator edits), then return the
5
+ * operator-facing warning string; null when there is no overlap (SP-784 / #298).
6
+ *
7
+ * @param {object} params
8
+ * @param {string} params.projectRoot
9
+ * @param {string} params.batchId
10
+ * @param {string} params.baseBranch
11
+ * @param {string} params.orchBranch
12
+ * @param {string} params.mergeCommit
13
+ * @param {string[]} [params.skippedDirtyPaths]
14
+ * @param {number|null} [params.laneNumber] Salvage-only lane number added to the journal event.
15
+ * @returns {string | null}
16
+ */
17
+ export function reportDirtyOverlap({
18
+ projectRoot,
19
+ batchId,
20
+ baseBranch,
21
+ orchBranch,
22
+ mergeCommit,
23
+ skippedDirtyPaths = [],
24
+ laneNumber = null,
25
+ }) {
26
+ if (skippedDirtyPaths.length === 0) return null;
27
+ appendJournalEvent(projectRoot, batchId, "integrate.dirty_overlap", {
28
+ baseBranch,
29
+ orchBranch,
30
+ mergeCommit,
31
+ ...(laneNumber != null ? { laneNumber } : {}),
32
+ skippedDirtyPaths,
33
+ });
34
+ return `DirtyOverlap: ${skippedDirtyPaths.join(", ")} kept local edits — run git diff / git restore --source ${baseBranch} -- ${skippedDirtyPaths.join(" ")} after review`;
35
+ }