pi-spine 2.23.0 → 2.25.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/README.md +1 -1
- package/bin/spine-worker-runner.mjs +2 -1
- package/package.json +5 -5
- package/src/batch/attached-runner-promote.mjs +14 -0
- package/src/batch/batch-state-io.mjs +120 -45
- package/src/batch/engine-crash-guard.mjs +143 -0
- package/src/batch/engine-lanes/matrix-run.mjs +28 -2
- package/src/batch/engine-lanes/matrix.mjs +143 -8
- package/src/batch/integrate-dirty-overlap.mjs +35 -0
- package/src/batch/integrate-worktree.mjs +106 -13
- package/src/batch/integrate.mjs +34 -93
- package/src/batch/journal-checksum.mjs +26 -0
- package/src/batch/journal.mjs +32 -13
- package/src/batch/lifecycle-archive.mjs +8 -2
- package/src/batch/lifecycle.mjs +2 -2
- package/src/batch/reconcile-batch.mjs +26 -19
- package/src/batch/reconcile-classify.mjs +21 -11
- package/src/batch/reconcile-diagnosis-context.mjs +1 -2
- package/src/batch/reconcile-diagnosis.mjs +18 -19
- package/src/batch/reconcile-light-cache.mjs +0 -1
- package/src/batch/reconcile-orphan.mjs +9 -8
- package/src/batch/reconcile.mjs +0 -1
- package/src/batch/salvage-batch-integrate.mjs +13 -17
- package/src/batch/state.mjs +22 -1
- package/src/batch/worker-heartbeat.mjs +29 -8
- package/src/batch/worker-host.mjs +29 -4
- package/src/batch/worker-output.mjs +9 -6
- package/src/batch/worker-prompt.mjs +4 -2
- package/src/batch/worker-spawn.mjs +99 -12
- package/src/config/preflight/discovery.mjs +20 -12
- package/src/doctor/run-doctor-checks.mjs +41 -3
- package/src/planner/scope.mjs +3 -1
- package/src/tasks/packet/discover.mjs +59 -3
|
@@ -22,6 +22,7 @@ import {
|
|
|
22
22
|
substituteMatrixVariables,
|
|
23
23
|
} from "../../planner/matrix.mjs";
|
|
24
24
|
import { gitExec } from "../git-exec.mjs";
|
|
25
|
+
import { terminateProcessTree } from "../../process/terminate-tree.mjs";
|
|
25
26
|
import {
|
|
26
27
|
normalizeLaneWorktreeGitPaths,
|
|
27
28
|
runWorktreeSetupHook,
|
|
@@ -285,35 +286,169 @@ export function buildMatrixRowEnv({ taskId, rowId, rowIndex, rowCount }) {
|
|
|
285
286
|
};
|
|
286
287
|
}
|
|
287
288
|
|
|
289
|
+
/**
|
|
290
|
+
* Default matrix row shell timeout (SP-783 / #297). Matches the contract
|
|
291
|
+
* `testCommand` timeout so a hung row cannot hold a global lane slot forever.
|
|
292
|
+
*
|
|
293
|
+
* @type {number}
|
|
294
|
+
*/
|
|
295
|
+
export const DEFAULT_MATRIX_ROW_TIMEOUT_MS = 600_000;
|
|
296
|
+
|
|
297
|
+
/**
|
|
298
|
+
* Default matrix row output cap (SP-783 / #297). Matches the worker-output
|
|
299
|
+
* default so a chatty row keeps only a bounded tail in engine memory.
|
|
300
|
+
*
|
|
301
|
+
* @type {number}
|
|
302
|
+
*/
|
|
303
|
+
export const DEFAULT_MATRIX_ROW_OUTPUT_MAX_BYTES = 262_144;
|
|
304
|
+
|
|
305
|
+
/** Grace between SIGTERM and the SIGKILL escalation when a row times out. */
|
|
306
|
+
const TREE_KILL_GRACE_MS = 5_000;
|
|
307
|
+
|
|
308
|
+
/** Marker prefixed to output when the cap dropped bytes (SP-783 / #297). */
|
|
309
|
+
const OUTPUT_TRUNCATION_MARKER_PREFIX = "[… ";
|
|
310
|
+
const OUTPUT_TRUNCATION_MARKER_SUFFIX = " bytes truncated …]\n";
|
|
311
|
+
|
|
312
|
+
/**
|
|
313
|
+
* Resolve the `SPINE_MATRIX_ROW_TIMEOUT_MS` override (positive integer ms),
|
|
314
|
+
* mirroring `SPINE_SYNC_TIMEOUT_MS`: invalid or absent values return null so
|
|
315
|
+
* the caller falls back to the default instead of failing the row.
|
|
316
|
+
*
|
|
317
|
+
* @param {{ env?: Record<string, string | undefined> }} [params]
|
|
318
|
+
* @returns {number | null} Override in ms, or null when unset/invalid.
|
|
319
|
+
*/
|
|
320
|
+
export function resolveMatrixRowTimeoutOverride(params = {}) {
|
|
321
|
+
const env = params.env ?? process.env;
|
|
322
|
+
const raw = env.SPINE_MATRIX_ROW_TIMEOUT_MS;
|
|
323
|
+
if (raw == null || raw === "") return null;
|
|
324
|
+
const parsed = Number(raw);
|
|
325
|
+
return Number.isInteger(parsed) && parsed > 0 ? parsed : null;
|
|
326
|
+
}
|
|
327
|
+
|
|
288
328
|
/**
|
|
289
329
|
* Run a shell command in a directory, resolving with exit code and combined output.
|
|
290
330
|
* Uses async spawn so concurrent rows genuinely overlap (required for maxParallel).
|
|
291
331
|
*
|
|
332
|
+
* Bounded by default (SP-783 / #297): a row that outlives `timeoutMs` gets its
|
|
333
|
+
* whole process tree terminated (SIGTERM, then SIGKILL after a short grace —
|
|
334
|
+
* the shell is spawned detached so it is a process-group leader whose
|
|
335
|
+
* descendants are reachable) and resolves with `exitCode: 124` and
|
|
336
|
+
* `timedOut: true`. Output is kept as a bounded tail of at most
|
|
337
|
+
* `maxOutputBytes` bytes; when the cap dropped anything the result is prefixed
|
|
338
|
+
* with a truncation marker and carries `outputTruncated: true`.
|
|
339
|
+
*
|
|
292
340
|
* @param {string} cwd
|
|
293
341
|
* @param {string} command
|
|
294
342
|
* @param {Record<string, string> | null} [extraEnv] Extra environment variables
|
|
295
343
|
* layered over `process.env` (matrix row identity, #229). Null injects nothing.
|
|
296
|
-
* @
|
|
344
|
+
* @param {{ timeoutMs?: number, maxOutputBytes?: number }} [options]
|
|
345
|
+
* `timeoutMs` defaults to `SPINE_MATRIX_ROW_TIMEOUT_MS` (positive integer)
|
|
346
|
+
* when set, else `DEFAULT_MATRIX_ROW_TIMEOUT_MS`; `maxOutputBytes` defaults
|
|
347
|
+
* to `DEFAULT_MATRIX_ROW_OUTPUT_MAX_BYTES`.
|
|
348
|
+
* @returns {Promise<{ exitCode: number, output: string, timedOut?: boolean, outputTruncated?: boolean }>}
|
|
297
349
|
*/
|
|
298
|
-
export function runShellInDir(cwd, command, extraEnv = null) {
|
|
350
|
+
export function runShellInDir(cwd, command, extraEnv = null, options = {}) {
|
|
351
|
+
const optionTimeoutMs = Number(options.timeoutMs);
|
|
352
|
+
const timeoutMs =
|
|
353
|
+
Number.isInteger(optionTimeoutMs) && optionTimeoutMs > 0
|
|
354
|
+
? optionTimeoutMs
|
|
355
|
+
: (resolveMatrixRowTimeoutOverride() ?? DEFAULT_MATRIX_ROW_TIMEOUT_MS);
|
|
356
|
+
const optionMaxOutputBytes = Number(options.maxOutputBytes);
|
|
357
|
+
const maxOutputBytes =
|
|
358
|
+
Number.isInteger(optionMaxOutputBytes) && optionMaxOutputBytes > 0
|
|
359
|
+
? optionMaxOutputBytes
|
|
360
|
+
: DEFAULT_MATRIX_ROW_OUTPUT_MAX_BYTES;
|
|
361
|
+
|
|
299
362
|
return new Promise((resolve) => {
|
|
363
|
+
let settled = false;
|
|
364
|
+
let timedOut = false;
|
|
365
|
+
let droppedBytes = 0;
|
|
366
|
+
/** @type {NodeJS.Timeout | undefined} */
|
|
367
|
+
let killGraceTimer;
|
|
368
|
+
|
|
369
|
+
// Bounded tail buffer: once the cap is exceeded, chunks are dropped from
|
|
370
|
+
// the front (copying the retained slice) so memory stays O(maxOutputBytes)
|
|
371
|
+
// no matter how chatty the row is — never the full stream.
|
|
372
|
+
/** @type {Buffer[]} */
|
|
373
|
+
const tailChunks = [];
|
|
374
|
+
let tailBytes = 0;
|
|
375
|
+
/** @type {(chunk: unknown) => void} */
|
|
376
|
+
const append = (chunk) => {
|
|
377
|
+
const buf = Buffer.isBuffer(chunk) ? chunk : Buffer.from(String(chunk));
|
|
378
|
+
if (buf.length === 0) return;
|
|
379
|
+
tailChunks.push(buf);
|
|
380
|
+
tailBytes += buf.length;
|
|
381
|
+
while (tailBytes > maxOutputBytes && tailChunks.length > 0) {
|
|
382
|
+
const excess = tailBytes - maxOutputBytes;
|
|
383
|
+
const first = tailChunks[0];
|
|
384
|
+
if (first.length > excess) {
|
|
385
|
+
tailChunks[0] = Buffer.from(first.subarray(excess));
|
|
386
|
+
droppedBytes += excess;
|
|
387
|
+
tailBytes = maxOutputBytes;
|
|
388
|
+
} else {
|
|
389
|
+
tailChunks.shift();
|
|
390
|
+
droppedBytes += first.length;
|
|
391
|
+
tailBytes -= first.length;
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
};
|
|
395
|
+
|
|
300
396
|
const child = spawn("/bin/sh", ["-c", command], {
|
|
301
397
|
cwd,
|
|
302
398
|
env: extraEnv ? { ...process.env, ...extraEnv } : process.env,
|
|
303
399
|
stdio: ["ignore", "pipe", "pipe"],
|
|
400
|
+
// Process-group leader on POSIX so tree termination reaches grandchildren.
|
|
401
|
+
detached: process.platform !== "win32",
|
|
304
402
|
});
|
|
305
|
-
|
|
306
|
-
/** @
|
|
307
|
-
const
|
|
308
|
-
|
|
403
|
+
|
|
404
|
+
/** @param {{ exitCode: number, output: string, timedOut?: boolean, outputTruncated?: boolean }} result */
|
|
405
|
+
const settle = (result) => {
|
|
406
|
+
if (settled) return;
|
|
407
|
+
settled = true;
|
|
408
|
+
clearTimeout(timeoutTimer);
|
|
409
|
+
clearTimeout(killGraceTimer);
|
|
410
|
+
resolve(result);
|
|
309
411
|
};
|
|
412
|
+
|
|
413
|
+
/** @type {NodeJS.Timeout} */
|
|
414
|
+
const timeoutTimer = setTimeout(() => {
|
|
415
|
+
timedOut = true;
|
|
416
|
+
terminateProcessTree(child.pid, { signal: "SIGTERM" });
|
|
417
|
+
// Escalate when the tree ignores SIGTERM past the grace window.
|
|
418
|
+
killGraceTimer = setTimeout(() => {
|
|
419
|
+
if (!settled) terminateProcessTree(child.pid, { signal: "SIGKILL" });
|
|
420
|
+
}, TREE_KILL_GRACE_MS);
|
|
421
|
+
killGraceTimer.unref();
|
|
422
|
+
}, timeoutMs);
|
|
423
|
+
timeoutTimer.unref();
|
|
424
|
+
|
|
310
425
|
child.stdout?.on("data", append);
|
|
311
426
|
child.stderr?.on("data", append);
|
|
312
427
|
child.on("error", (err) => {
|
|
313
|
-
|
|
428
|
+
settle({
|
|
429
|
+
exitCode: 1,
|
|
430
|
+
output: Buffer.concat(tailChunks).toString("utf8") + String(err),
|
|
431
|
+
});
|
|
314
432
|
});
|
|
315
433
|
child.on("close", (code) => {
|
|
316
|
-
|
|
434
|
+
const tail = Buffer.concat(tailChunks).toString("utf8");
|
|
435
|
+
if (timedOut) {
|
|
436
|
+
settle({
|
|
437
|
+
exitCode: 124,
|
|
438
|
+
output: `${tail}matrix row command timed out after ${timeoutMs}ms\n`,
|
|
439
|
+
timedOut: true,
|
|
440
|
+
...(droppedBytes > 0 ? { outputTruncated: true } : {}),
|
|
441
|
+
});
|
|
442
|
+
return;
|
|
443
|
+
}
|
|
444
|
+
settle({
|
|
445
|
+
exitCode: code ?? 1,
|
|
446
|
+
output:
|
|
447
|
+
droppedBytes > 0
|
|
448
|
+
? `${OUTPUT_TRUNCATION_MARKER_PREFIX}${droppedBytes}${OUTPUT_TRUNCATION_MARKER_SUFFIX}${tail}`
|
|
449
|
+
: tail,
|
|
450
|
+
...(droppedBytes > 0 ? { outputTruncated: true } : {}),
|
|
451
|
+
});
|
|
317
452
|
});
|
|
318
453
|
});
|
|
319
454
|
}
|
|
@@ -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
|
+
}
|
|
@@ -11,6 +11,7 @@ import { gitExec } from "./git-exec.mjs";
|
|
|
11
11
|
import { resolveGitCommitEnv } from "./git-commit-env.mjs";
|
|
12
12
|
import { tryAutoResolveRulesManifestMergeConflict } from "./engine-lanes/merge.mjs";
|
|
13
13
|
import { appendJournalEvent } from "./journal.mjs";
|
|
14
|
+
import { listIntegrateDirtyPaths } from "./rules-manifest-drift.mjs";
|
|
14
15
|
|
|
15
16
|
export const DEFAULT_SYNC_TIMEOUT_MS = 60_000;
|
|
16
17
|
|
|
@@ -115,13 +116,49 @@ function mergeTreeOutputHasConflict(output) {
|
|
|
115
116
|
}
|
|
116
117
|
|
|
117
118
|
/**
|
|
119
|
+
* Move a branch ref to newSha only if it still points at expectedOldSha (compare-and-swap).
|
|
120
|
+
* Prevents a concurrent commit on base from being silently orphaned mid-integrate (#298).
|
|
121
|
+
*
|
|
122
|
+
* @param {object} params
|
|
123
|
+
* @param {string} params.projectRoot
|
|
124
|
+
* @param {string} params.baseBranch
|
|
125
|
+
* @param {string} params.newSha
|
|
126
|
+
* @param {string} params.expectedOldSha
|
|
127
|
+
* @returns {{ ok: boolean, failureClass?: string, error?: string }}
|
|
128
|
+
*/
|
|
129
|
+
export function casUpdateBaseRef({ projectRoot, baseBranch, newSha, expectedOldSha }) {
|
|
130
|
+
try {
|
|
131
|
+
gitExec(projectRoot, ["update-ref", `refs/heads/${baseBranch}`, newSha, expectedOldSha], {
|
|
132
|
+
projectRoot,
|
|
133
|
+
});
|
|
134
|
+
return { ok: true };
|
|
135
|
+
} catch {
|
|
136
|
+
let current = "unknown";
|
|
137
|
+
try {
|
|
138
|
+
current = gitExec(projectRoot, ["rev-parse", `refs/heads/${baseBranch}`], { projectRoot });
|
|
139
|
+
} catch {
|
|
140
|
+
// keep "unknown" — the branch may have been deleted mid-integrate
|
|
141
|
+
}
|
|
142
|
+
return {
|
|
143
|
+
ok: false,
|
|
144
|
+
failureClass: "BaseMoved",
|
|
145
|
+
error: `${baseBranch} moved during integrate (expected ${expectedOldSha}, found ${current}) — re-run spine integrate`,
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Plumbing merge of orch into base without touching any worktree.
|
|
152
|
+
* Shared implementation for integrate and salvage paths; merge-tree runs on captured SHAs
|
|
153
|
+
* and the base ref update is compare-and-swap so a mid-integrate base move fails loudly (#298).
|
|
154
|
+
*
|
|
118
155
|
* @param {object} params
|
|
119
156
|
* @param {string} params.projectRoot
|
|
120
157
|
* @param {string} params.baseBranch
|
|
121
158
|
* @param {string} params.orchBranch
|
|
122
159
|
* @param {string} params.mergeMessage
|
|
123
160
|
*/
|
|
124
|
-
function plumbingMergeOrchIntoBase({ projectRoot, baseBranch, orchBranch, mergeMessage }) {
|
|
161
|
+
export function plumbingMergeOrchIntoBase({ projectRoot, baseBranch, orchBranch, mergeMessage }) {
|
|
125
162
|
const baseSha = git(projectRoot, ["rev-parse", baseBranch]);
|
|
126
163
|
const orchSha = git(projectRoot, ["rev-parse", orchBranch]);
|
|
127
164
|
|
|
@@ -130,7 +167,7 @@ function plumbingMergeOrchIntoBase({ projectRoot, baseBranch, orchBranch, mergeM
|
|
|
130
167
|
try {
|
|
131
168
|
mergeTreeOutput = execFileSync(
|
|
132
169
|
"git",
|
|
133
|
-
["merge-tree", "--write-tree",
|
|
170
|
+
["merge-tree", "--write-tree", baseSha, orchSha],
|
|
134
171
|
{
|
|
135
172
|
cwd: projectRoot,
|
|
136
173
|
encoding: "utf-8",
|
|
@@ -170,7 +207,10 @@ function plumbingMergeOrchIntoBase({ projectRoot, baseBranch, orchBranch, mergeM
|
|
|
170
207
|
projectRoot,
|
|
171
208
|
["commit-tree", treeSha, "-p", baseSha, "-p", orchSha, "-m", mergeMessage],
|
|
172
209
|
);
|
|
173
|
-
|
|
210
|
+
const cas = casUpdateBaseRef({ projectRoot, baseBranch, newSha: mergeCommit, expectedOldSha: baseSha });
|
|
211
|
+
if (!cas.ok) {
|
|
212
|
+
return cas;
|
|
213
|
+
}
|
|
174
214
|
|
|
175
215
|
return { ok: true, mergeCommit, mode: "plumbing" };
|
|
176
216
|
}
|
|
@@ -242,16 +282,17 @@ function pathExistsInTree(cwd, treeRef, filePath, { timeoutMs } = {}) {
|
|
|
242
282
|
}
|
|
243
283
|
|
|
244
284
|
/**
|
|
245
|
-
* Materialize paths
|
|
246
|
-
*
|
|
285
|
+
* Materialize paths changed by a plumbing merge; dirty paths (uncommitted local edits) are
|
|
286
|
+
* skipped and reported via skippedDirtyPaths (SP-784 / #298). Capture dirtyPaths BEFORE the
|
|
287
|
+
* merge moves the base ref: afterwards git status reports unmaterialized paths as deleted.
|
|
247
288
|
*
|
|
248
289
|
* @param {string} projectRoot
|
|
249
290
|
* @param {string} baseSha
|
|
250
291
|
* @param {string} mergeCommit
|
|
251
|
-
* @param {{ timeoutMs?: number }} [options]
|
|
252
|
-
* @returns {{ ok: boolean, timedOut?: boolean, error?: string, processedPaths?: number, totalPaths?: number }}
|
|
292
|
+
* @param {{ timeoutMs?: number, dirtyPaths?: string[] }} [options]
|
|
293
|
+
* @returns {{ ok: boolean, timedOut?: boolean, error?: string, processedPaths?: number, totalPaths?: number, skippedDirtyPaths?: string[] }}
|
|
253
294
|
*/
|
|
254
|
-
export function syncPlumbingMergePathsToWorktree(projectRoot, baseSha, mergeCommit, { timeoutMs = DEFAULT_SYNC_TIMEOUT_MS } = {}) {
|
|
295
|
+
export function syncPlumbingMergePathsToWorktree(projectRoot, baseSha, mergeCommit, { timeoutMs = DEFAULT_SYNC_TIMEOUT_MS, dirtyPaths } = {}) {
|
|
255
296
|
const envTimeoutMs = process.env.SPINE_SYNC_TIMEOUT_MS
|
|
256
297
|
? Number(process.env.SPINE_SYNC_TIMEOUT_MS)
|
|
257
298
|
: null;
|
|
@@ -272,14 +313,20 @@ export function syncPlumbingMergePathsToWorktree(projectRoot, baseSha, mergeComm
|
|
|
272
313
|
}
|
|
273
314
|
|
|
274
315
|
const paths = output.split("\n").map((line) => line.trim()).filter(Boolean);
|
|
316
|
+
const dirtyPathSet = new Set(dirtyPaths ?? listIntegrateDirtyPaths(projectRoot));
|
|
317
|
+
const skippedDirtyPaths = [];
|
|
275
318
|
let processedCount = 0;
|
|
276
319
|
|
|
277
320
|
for (const filePath of paths) {
|
|
278
|
-
|
|
321
|
+
// Dirty entry = local edit only when the file is in the worktree (modified/untracked) or
|
|
322
|
+
// tracked at baseSha (operator deleted); missing from both = merge output to restore (SP-784).
|
|
323
|
+
let existsInMerge = false, operatorTouched = false;
|
|
279
324
|
try {
|
|
280
325
|
existsInMerge = pathExistsInTree(projectRoot, mergeCommit, filePath, {
|
|
281
326
|
timeoutMs: effectiveTimeout,
|
|
282
327
|
});
|
|
328
|
+
if (dirtyPathSet.has(filePath)) operatorTouched = fs.existsSync(path.join(projectRoot, filePath)) ||
|
|
329
|
+
pathExistsInTree(projectRoot, baseSha, filePath, { timeoutMs: effectiveTimeout });
|
|
283
330
|
} catch (err) {
|
|
284
331
|
if (isTimeoutError(err)) {
|
|
285
332
|
return {
|
|
@@ -300,6 +347,11 @@ export function syncPlumbingMergePathsToWorktree(projectRoot, baseSha, mergeComm
|
|
|
300
347
|
continue;
|
|
301
348
|
}
|
|
302
349
|
|
|
350
|
+
if (dirtyPathSet.has(filePath) && operatorTouched) {
|
|
351
|
+
skippedDirtyPaths.push(filePath);
|
|
352
|
+
continue;
|
|
353
|
+
}
|
|
354
|
+
|
|
303
355
|
try {
|
|
304
356
|
gitWithTimeout(
|
|
305
357
|
projectRoot,
|
|
@@ -330,7 +382,35 @@ export function syncPlumbingMergePathsToWorktree(projectRoot, baseSha, mergeComm
|
|
|
330
382
|
processedCount++;
|
|
331
383
|
}
|
|
332
384
|
|
|
333
|
-
return { ok: true, processedPaths: processedCount, totalPaths: paths.length };
|
|
385
|
+
return { ok: true, processedPaths: processedCount, totalPaths: paths.length, skippedDirtyPaths };
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
/**
|
|
389
|
+
* Post-merge path sync gated on baseBranch being checked out right now (SP-784 / #298);
|
|
390
|
+
* returns null (no sync) otherwise. Options forward to syncPlumbingMergePathsToWorktree.
|
|
391
|
+
* @param {string} projectRoot
|
|
392
|
+
* @param {string} baseBranch
|
|
393
|
+
*/
|
|
394
|
+
export function syncMergePathsToCheckedOutBase(projectRoot, baseBranch, baseSha, mergeCommit, options) {
|
|
395
|
+
if (!isBranchCheckedOutInWorktree(projectRoot, baseBranch)) return null;
|
|
396
|
+
return syncPlumbingMergePathsToWorktree(projectRoot, baseSha, mergeCommit, options);
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
/**
|
|
400
|
+
* Sync a landed merge into projectRoot when base is checked out. Pre-merge base: `baseShaBefore`
|
|
401
|
+
* (fast-forward) or the merge's first parent (plumbing); worktree merges imply no checkout (null).
|
|
402
|
+
* @param {{ mode?: string, mergeCommit: string, baseShaBefore?: string }} mergeResult
|
|
403
|
+
*/
|
|
404
|
+
export function syncMergeResultToCheckedOutBase(projectRoot, baseBranch, mergeResult, options) {
|
|
405
|
+
const { mode, mergeCommit } = mergeResult;
|
|
406
|
+
if (mode === "fast-forward") {
|
|
407
|
+
return syncMergePathsToCheckedOutBase(projectRoot, baseBranch, mergeResult.baseShaBefore, mergeCommit, options);
|
|
408
|
+
}
|
|
409
|
+
if (mode === "plumbing") {
|
|
410
|
+
const baseSha = git(projectRoot, ["rev-parse", `${mergeCommit}^1`]);
|
|
411
|
+
return syncMergePathsToCheckedOutBase(projectRoot, baseBranch, baseSha, mergeCommit, options);
|
|
412
|
+
}
|
|
413
|
+
return null;
|
|
334
414
|
}
|
|
335
415
|
|
|
336
416
|
/**
|
|
@@ -342,6 +422,10 @@ export function syncPlumbingMergePathsToWorktree(projectRoot, baseSha, mergeComm
|
|
|
342
422
|
* @param {string} params.mergeMessage
|
|
343
423
|
*/
|
|
344
424
|
function mergeInIntegrateWorktree({ projectRoot, worktreePath, baseBranch, orchBranch, mergeMessage }) {
|
|
425
|
+
// The worktree has baseBranch checked out, so the merge itself advances the shared ref.
|
|
426
|
+
// Capture the base tip before the merge and verify the merge commit's first parent matches
|
|
427
|
+
// so a base move between provisioning and merge fails loudly instead of being clobbered (#298).
|
|
428
|
+
const baseShaBefore = git(projectRoot, ["rev-parse", baseBranch]);
|
|
345
429
|
try {
|
|
346
430
|
git(worktreePath, ["merge", "--no-ff", orchBranch, "-m", mergeMessage]);
|
|
347
431
|
} catch {
|
|
@@ -365,9 +449,18 @@ function mergeInIntegrateWorktree({ projectRoot, worktreePath, baseBranch, orchB
|
|
|
365
449
|
}
|
|
366
450
|
|
|
367
451
|
const mergeCommit = git(worktreePath, ["rev-parse", "HEAD"]);
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
452
|
+
if (mergeCommit === baseShaBefore) {
|
|
453
|
+
// Merge was a no-op (orch already contained in base) — HEAD did not move, so there is
|
|
454
|
+
// no new merge commit whose first parent could be verified.
|
|
455
|
+
return { ok: true, mergeCommit, mode: "worktree" };
|
|
456
|
+
}
|
|
457
|
+
const firstParent = git(projectRoot, ["rev-parse", `${mergeCommit}^1`]);
|
|
458
|
+
if (firstParent !== baseShaBefore) {
|
|
459
|
+
return {
|
|
460
|
+
ok: false,
|
|
461
|
+
failureClass: "BaseMoved",
|
|
462
|
+
error: `${baseBranch} moved during integrate (expected ${baseShaBefore}, found ${firstParent}) — re-run spine integrate`,
|
|
463
|
+
};
|
|
371
464
|
}
|
|
372
465
|
|
|
373
466
|
return { ok: true, mergeCommit, mode: "worktree" };
|
package/src/batch/integrate.mjs
CHANGED
|
@@ -3,12 +3,19 @@
|
|
|
3
3
|
* Orch → base integration (FR-INT-01–05, TP-016).
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
-
import { execFileSync } from "node:child_process";
|
|
7
6
|
import { gitExec } from "./git-exec.mjs";
|
|
8
7
|
import { loadSpineConfig } from "../config/spine-config-load.mjs";
|
|
9
|
-
import {
|
|
8
|
+
import {
|
|
9
|
+
mergeOrchIntoBaseIsolated,
|
|
10
|
+
plumbingMergeOrchIntoBase,
|
|
11
|
+
casUpdateBaseRef,
|
|
12
|
+
isBranchCheckedOutInWorktree,
|
|
13
|
+
syncMergeResultToCheckedOutBase,
|
|
14
|
+
} from "./integrate-worktree.mjs";
|
|
15
|
+
import { reportDirtyOverlap } from "./integrate-dirty-overlap.mjs";
|
|
10
16
|
import {
|
|
11
17
|
isFastForwardCapableIntegrate,
|
|
18
|
+
listIntegrateDirtyPaths,
|
|
12
19
|
resolveRulesManifestIntegrateDrift,
|
|
13
20
|
} from "./rules-manifest-drift.mjs";
|
|
14
21
|
import { assertOrchIntegratable } from "./integrate-assert.mjs";
|
|
@@ -28,78 +35,10 @@ function git(projectRoot, args) {
|
|
|
28
35
|
return gitExec(projectRoot, args, { projectRoot });
|
|
29
36
|
}
|
|
30
37
|
|
|
31
|
-
/**
|
|
32
|
-
* @param {string} output
|
|
33
|
-
*/
|
|
34
|
-
function mergeTreeOutputHasConflict(output) {
|
|
35
|
-
return /CONFLICT/i.test(output);
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Ref-only merge when base is not checked out in projectRoot (avoids worktree git merge).
|
|
40
|
-
*
|
|
41
|
-
* @param {object} params
|
|
42
|
-
* @param {string} params.projectRoot
|
|
43
|
-
* @param {string} params.baseBranch
|
|
44
|
-
* @param {string} params.orchBranch
|
|
45
|
-
* @param {string} params.mergeMessage
|
|
46
|
-
*/
|
|
47
|
-
function mergeOrchIntoBaseViaRefs({ projectRoot, baseBranch, orchBranch, mergeMessage }) {
|
|
48
|
-
const baseSha = git(projectRoot, ["rev-parse", baseBranch]);
|
|
49
|
-
const orchSha = git(projectRoot, ["rev-parse", orchBranch]);
|
|
50
|
-
|
|
51
|
-
let mergeTreeOutput = "";
|
|
52
|
-
let treeSha = "";
|
|
53
|
-
try {
|
|
54
|
-
mergeTreeOutput = execFileSync(
|
|
55
|
-
"git",
|
|
56
|
-
["merge-tree", "--write-tree", baseBranch, orchBranch],
|
|
57
|
-
{
|
|
58
|
-
cwd: projectRoot,
|
|
59
|
-
encoding: "utf-8",
|
|
60
|
-
stdio: ["ignore", "pipe", "pipe"],
|
|
61
|
-
env: { ...process.env },
|
|
62
|
-
},
|
|
63
|
-
).trim();
|
|
64
|
-
treeSha = mergeTreeOutput.split("\n").pop()?.trim() ?? "";
|
|
65
|
-
} catch (err) {
|
|
66
|
-
const stdout = err && typeof err === "object" && "stdout" in err ? String(err.stdout ?? "") : "";
|
|
67
|
-
const stderr = err && typeof err === "object" && "stderr" in err ? String(err.stderr ?? "") : "";
|
|
68
|
-
mergeTreeOutput = `${stdout}\n${stderr}`.trim();
|
|
69
|
-
if (mergeTreeOutputHasConflict(mergeTreeOutput)) {
|
|
70
|
-
return {
|
|
71
|
-
ok: false,
|
|
72
|
-
failureClass: "MergeConflict",
|
|
73
|
-
error: mergeTreeOutput.split("\n").slice(-3).join(" ") || "merge conflict",
|
|
74
|
-
};
|
|
75
|
-
}
|
|
76
|
-
const message = err instanceof Error ? err.message : String(err);
|
|
77
|
-
return {
|
|
78
|
-
ok: false,
|
|
79
|
-
failureClass: "IntegrateFailed",
|
|
80
|
-
error: message,
|
|
81
|
-
};
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
if (!treeSha || mergeTreeOutputHasConflict(mergeTreeOutput)) {
|
|
85
|
-
return {
|
|
86
|
-
ok: false,
|
|
87
|
-
failureClass: "MergeConflict",
|
|
88
|
-
error: mergeTreeOutput || `merge conflict integrating ${orchBranch} into ${baseBranch}`,
|
|
89
|
-
};
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
const mergeCommit = git(
|
|
93
|
-
projectRoot,
|
|
94
|
-
["commit-tree", treeSha, "-p", baseSha, "-p", orchSha, "-m", mergeMessage],
|
|
95
|
-
);
|
|
96
|
-
git(projectRoot, ["update-ref", `refs/heads/${baseBranch}`, mergeCommit]);
|
|
97
|
-
|
|
98
|
-
return { ok: true, mergeCommit, mode: "plumbing" };
|
|
99
|
-
}
|
|
100
|
-
|
|
101
38
|
/**
|
|
102
39
|
* Fast-forward base to orch tip without checking out base in projectRoot.
|
|
40
|
+
* The ref update is compare-and-swap on baseShaBefore so a commit landing on base
|
|
41
|
+
* mid-integrate fails loudly instead of being orphaned (#298).
|
|
103
42
|
*
|
|
104
43
|
* @param {object} params
|
|
105
44
|
* @param {string} params.projectRoot
|
|
@@ -109,7 +48,10 @@ function mergeOrchIntoBaseViaRefs({ projectRoot, baseBranch, orchBranch, mergeMe
|
|
|
109
48
|
function fastForwardOrchIntoBase({ projectRoot, baseBranch, orchBranch }) {
|
|
110
49
|
const baseShaBefore = git(projectRoot, ["rev-parse", baseBranch]);
|
|
111
50
|
const orchSha = git(projectRoot, ["rev-parse", orchBranch]);
|
|
112
|
-
|
|
51
|
+
const cas = casUpdateBaseRef({ projectRoot, baseBranch, newSha: orchSha, expectedOldSha: baseShaBefore });
|
|
52
|
+
if (!cas.ok) {
|
|
53
|
+
return cas;
|
|
54
|
+
}
|
|
113
55
|
return {
|
|
114
56
|
ok: true,
|
|
115
57
|
mergeCommit: orchSha,
|
|
@@ -133,7 +75,7 @@ function runIntegrateMerge({ projectRoot, baseBranch, orchBranch, batchId }) {
|
|
|
133
75
|
if (isBranchCheckedOutInWorktree(projectRoot, baseBranch)) {
|
|
134
76
|
return mergeOrchIntoBaseIsolated({ projectRoot, baseBranch, orchBranch, batchId });
|
|
135
77
|
}
|
|
136
|
-
return
|
|
78
|
+
return plumbingMergeOrchIntoBase({ projectRoot, baseBranch, orchBranch, mergeMessage });
|
|
137
79
|
}
|
|
138
80
|
|
|
139
81
|
/**
|
|
@@ -371,6 +313,9 @@ export function integrateOrchToBase(ctx) {
|
|
|
371
313
|
});
|
|
372
314
|
}
|
|
373
315
|
|
|
316
|
+
// Capture before the merge moves the base ref: afterwards git status reports
|
|
317
|
+
// not-yet-materialized merged paths as modified/deleted vs the new HEAD (SP-784).
|
|
318
|
+
const preMergeDirtyPaths = listIntegrateDirtyPaths(projectRoot);
|
|
374
319
|
const mergeResult = runIntegrateMerge({ projectRoot, baseBranch, orchBranch, batchId });
|
|
375
320
|
|
|
376
321
|
if (!mergeResult.ok) {
|
|
@@ -399,25 +344,11 @@ export function integrateOrchToBase(ctx) {
|
|
|
399
344
|
|
|
400
345
|
const mergeCommit = mergeResult.mergeCommit;
|
|
401
346
|
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
mergeResult.baseShaBefore,
|
|
408
|
-
mergeCommit,
|
|
409
|
-
);
|
|
410
|
-
} else if (mergeResult.mode === "plumbing") {
|
|
411
|
-
const baseSha = git(projectRoot, ["rev-parse", `${mergeCommit}^1`]);
|
|
412
|
-
syncResult = syncPlumbingMergePathsToWorktree(projectRoot, baseSha, mergeCommit);
|
|
413
|
-
} else if (!baseCheckedOutAtStart) {
|
|
414
|
-
try {
|
|
415
|
-
git(projectRoot, ["checkout", baseBranch]);
|
|
416
|
-
git(projectRoot, ["reset", "--hard", "HEAD"]);
|
|
417
|
-
} catch {
|
|
418
|
-
// Dirty tree may block checkout after isolated land — operator syncs manually.
|
|
419
|
-
}
|
|
420
|
-
}
|
|
347
|
+
// No sync when base is not checked out right now (SP-784 / #298): the merge commit is
|
|
348
|
+
// already on the base ref and the checkout must stay untouched.
|
|
349
|
+
const syncResult = syncMergeResultToCheckedOutBase(projectRoot, baseBranch, mergeResult, {
|
|
350
|
+
dirtyPaths: preMergeDirtyPaths,
|
|
351
|
+
});
|
|
421
352
|
|
|
422
353
|
if (syncResult && !syncResult.ok) {
|
|
423
354
|
appendJournalEvent(projectRoot, batchId, "integrate.failed", {
|
|
@@ -446,6 +377,15 @@ export function integrateOrchToBase(ctx) {
|
|
|
446
377
|
};
|
|
447
378
|
}
|
|
448
379
|
|
|
380
|
+
const dirtyOverlapWarning = reportDirtyOverlap({
|
|
381
|
+
projectRoot,
|
|
382
|
+
batchId,
|
|
383
|
+
baseBranch,
|
|
384
|
+
orchBranch,
|
|
385
|
+
mergeCommit,
|
|
386
|
+
skippedDirtyPaths: syncResult?.skippedDirtyPaths,
|
|
387
|
+
});
|
|
388
|
+
|
|
449
389
|
appendJournalEvent(projectRoot, batchId, "integrate.completed", {
|
|
450
390
|
baseBranch,
|
|
451
391
|
orchBranch,
|
|
@@ -467,6 +407,7 @@ export function integrateOrchToBase(ctx) {
|
|
|
467
407
|
headline: `Integrated ${orchBranch} into ${baseBranch}`,
|
|
468
408
|
suggestedCommand: "spine batch complete",
|
|
469
409
|
alternatives: ["spine status --diagnose"],
|
|
410
|
+
...(dirtyOverlapWarning ? { warnings: [dirtyOverlapWarning] } : {}),
|
|
470
411
|
};
|
|
471
412
|
} catch (err) {
|
|
472
413
|
const message = err instanceof Error ? err.message : String(err);
|
|
@@ -46,6 +46,31 @@ export function verifyJournalChecksum(event) {
|
|
|
46
46
|
return computeJournalChecksum(rest) === checksum;
|
|
47
47
|
}
|
|
48
48
|
|
|
49
|
+
const NEWLINE_BYTE = 0x0a;
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Isolate a torn tail before appending: when a previous append was torn by a
|
|
53
|
+
* crash or disk-full, the file can end mid-line without a trailing newline.
|
|
54
|
+
* Writing a newline first keeps the torn fragment on its own line so the new
|
|
55
|
+
* event stays parseable and the corruption stops propagating (SP-780).
|
|
56
|
+
* @param {string} filePath
|
|
57
|
+
*/
|
|
58
|
+
function appendNewlineIfTornTail(filePath) {
|
|
59
|
+
if (!fs.existsSync(filePath)) return;
|
|
60
|
+
const stat = fs.statSync(filePath);
|
|
61
|
+
if (stat.size === 0) return;
|
|
62
|
+
const fd = fs.openSync(filePath, "r");
|
|
63
|
+
try {
|
|
64
|
+
const tail = Buffer.alloc(1);
|
|
65
|
+
fs.readSync(fd, tail, 0, 1, stat.size - 1);
|
|
66
|
+
if (tail[0] !== NEWLINE_BYTE) {
|
|
67
|
+
fs.appendFileSync(filePath, "\n", "utf-8");
|
|
68
|
+
}
|
|
69
|
+
} finally {
|
|
70
|
+
fs.closeSync(fd);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
49
74
|
/**
|
|
50
75
|
* Append one jsonl line with fsync and bounded EBUSY/ENOENT retry.
|
|
51
76
|
* Does not rewrite the journal file. In-process callers remain serialized
|
|
@@ -60,6 +85,7 @@ export function appendJsonlLineSync(filePath, line) {
|
|
|
60
85
|
try {
|
|
61
86
|
if (!appended) {
|
|
62
87
|
fs.mkdirSync(path.dirname(filePath), { recursive: true });
|
|
88
|
+
appendNewlineIfTornTail(filePath);
|
|
63
89
|
fs.appendFileSync(filePath, line, "utf-8");
|
|
64
90
|
appended = true;
|
|
65
91
|
}
|