pi-spine 2.5.0 → 2.6.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.mjs +22 -0
- package/package.json +1 -1
- package/src/batch/attached-runner-promote.mjs +95 -11
- package/src/batch/diagnosis-alternatives.mjs +18 -0
- package/src/batch/diagnosis-gate-ready.mjs +23 -0
- package/src/batch/diagnosis-pending-lane.mjs +63 -0
- package/src/batch/diagnosis-retry-command.mjs +23 -0
- package/src/batch/diagnosis-tail-state.mjs +68 -0
- package/src/batch/diagnosis.mjs +69 -52
- package/src/batch/engine-lanes/commit.mjs +2 -0
- package/src/batch/engine-lanes.mjs +2 -3
- package/src/batch/evidence-command.mjs +125 -4
- package/src/batch/lane-commit.mjs +77 -8
- package/src/batch/lifecycle-archive.mjs +71 -0
- package/src/batch/lifecycle.mjs +27 -67
- package/src/batch/orphan-detect.mjs +68 -5
- package/src/batch/post-merge-limbo.mjs +137 -22
- package/src/batch/reconcile-batch.mjs +40 -43
- package/src/batch/reconcile-diagnosis-context.mjs +88 -0
- package/src/batch/reconcile-diagnosis.mjs +14 -32
- package/src/batch/resume-multi-validate.mjs +51 -9
- package/src/batch/retry.mjs +18 -2
- package/src/config/pi-spine-root.mjs +22 -0
- package/src/config/spine-config-load.mjs +26 -0
- package/src/doctor/duplicate-install.mjs +100 -0
- package/src/doctor/run-doctor-checks.mjs +10 -1
- package/src/doctor/worktree-health.mjs +4 -0
- package/templates/spine-config.json +3 -2
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
|
|
6
6
|
import path from "node:path";
|
|
7
7
|
import { execFileSync } from "node:child_process";
|
|
8
|
+
import { validateWorkerLaunchScriptPath } from "../config/worker-launch-script.mjs";
|
|
8
9
|
|
|
9
10
|
/**
|
|
10
11
|
* Allowed first-token executables for evidence commands.
|
|
@@ -13,6 +14,20 @@ import { execFileSync } from "node:child_process";
|
|
|
13
14
|
*/
|
|
14
15
|
export const ALLOWED_EVIDENCE_EXECUTABLES = new Set(["npm", "node", "pnpm", "yarn", "npx"]);
|
|
15
16
|
|
|
17
|
+
/**
|
|
18
|
+
* Interpreter basenames allowed only as project-local relative paths under
|
|
19
|
+
* `.venv/` or `venv/` (see {@link isAllowedProjectLocalInterpreter}).
|
|
20
|
+
* Bare names (PATH lookup) and absolute/outside-project paths stay rejected.
|
|
21
|
+
* @type {Set<string>}
|
|
22
|
+
*/
|
|
23
|
+
export const ALLOWED_PROJECT_LOCAL_INTERPRETERS = new Set(["python", "python3"]);
|
|
24
|
+
|
|
25
|
+
/** Relative venv roots that may host an allowed interpreter (posix-normalized). */
|
|
26
|
+
const PROJECT_LOCAL_VENV_PREFIXES = [".venv/", "venv/"];
|
|
27
|
+
|
|
28
|
+
/** Relative prefix for validated gate-evidence scripts (posix-normalized). */
|
|
29
|
+
const EVIDENCE_SCRIPTS_PREFIX = "scripts/";
|
|
30
|
+
|
|
16
31
|
const SHELL_METACHAR_PATTERN = /[;|&`<>]|>>|\$\(|\$\{/;
|
|
17
32
|
|
|
18
33
|
export class EvidenceCommandError extends Error {
|
|
@@ -58,11 +73,116 @@ export function parseEvidenceCommandArgv(command) {
|
|
|
58
73
|
}
|
|
59
74
|
|
|
60
75
|
const executable = path.basename(argv[0]);
|
|
61
|
-
if (
|
|
62
|
-
|
|
76
|
+
if (ALLOWED_EVIDENCE_EXECUTABLES.has(executable)) {
|
|
77
|
+
return argv;
|
|
78
|
+
}
|
|
79
|
+
if (isAllowedProjectLocalInterpreter(argv[0])) {
|
|
80
|
+
return argv;
|
|
81
|
+
}
|
|
82
|
+
if (isAllowedEvidenceScriptsPath(argv[0])) {
|
|
83
|
+
return argv;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
throw new EvidenceCommandError(`evidence executable not allowed: ${executable}`);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Allow project-local interpreters for gate evidence (e.g. `.venv/bin/python`).
|
|
91
|
+
*
|
|
92
|
+
* Exact rule: first token must be a relative path (not absolute), with no `..`
|
|
93
|
+
* segments after posix normalization, whose normalized form starts with
|
|
94
|
+
* `.venv/` or `venv/`, and whose basename is in
|
|
95
|
+
* {@link ALLOWED_PROJECT_LOCAL_INTERPRETERS} (`python` or `python3`).
|
|
96
|
+
* Rejects bare interpreter names and absolute/outside-project paths.
|
|
97
|
+
*
|
|
98
|
+
* @param {string} firstToken
|
|
99
|
+
* @returns {boolean}
|
|
100
|
+
*/
|
|
101
|
+
export function isAllowedProjectLocalInterpreter(firstToken) {
|
|
102
|
+
if (typeof firstToken !== "string" || !firstToken) {
|
|
103
|
+
return false;
|
|
104
|
+
}
|
|
105
|
+
if (path.isAbsolute(firstToken)) {
|
|
106
|
+
return false;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const posixToken = firstToken.replaceAll("\\", "/");
|
|
110
|
+
const segments = posixToken.split("/");
|
|
111
|
+
if (segments.some((segment) => segment === "..")) {
|
|
112
|
+
return false;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const normalized = path.posix.normalize(posixToken);
|
|
116
|
+
if (normalized.startsWith("../") || normalized === "..") {
|
|
117
|
+
return false;
|
|
118
|
+
}
|
|
119
|
+
if (path.posix.isAbsolute(normalized)) {
|
|
120
|
+
return false;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const underVenv = PROJECT_LOCAL_VENV_PREFIXES.some((prefix) => normalized.startsWith(prefix));
|
|
124
|
+
if (!underVenv) {
|
|
125
|
+
return false;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const basename = path.posix.basename(normalized);
|
|
129
|
+
return ALLOWED_PROJECT_LOCAL_INTERPRETERS.has(basename);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Lightweight parse-time check: first token is a relative path under `scripts/`
|
|
134
|
+
* with no parent traversal. Full symlink/root validation runs at execution via
|
|
135
|
+
* {@link validateWorkerLaunchScriptPath} (same sandbox as workerLaunchScript).
|
|
136
|
+
*
|
|
137
|
+
* @param {string} firstToken
|
|
138
|
+
* @returns {boolean}
|
|
139
|
+
*/
|
|
140
|
+
export function isAllowedEvidenceScriptsPath(firstToken) {
|
|
141
|
+
if (typeof firstToken !== "string" || !firstToken) {
|
|
142
|
+
return false;
|
|
143
|
+
}
|
|
144
|
+
if (path.isAbsolute(firstToken)) {
|
|
145
|
+
return false;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
const posixToken = firstToken.replaceAll("\\", "/");
|
|
149
|
+
const segments = posixToken.split("/");
|
|
150
|
+
if (segments.some((segment) => segment === "..")) {
|
|
151
|
+
return false;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
const normalized = path.posix.normalize(posixToken);
|
|
155
|
+
if (normalized.startsWith("../") || normalized === "..") {
|
|
156
|
+
return false;
|
|
157
|
+
}
|
|
158
|
+
if (path.posix.isAbsolute(normalized)) {
|
|
159
|
+
return false;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
return normalized.startsWith(EVIDENCE_SCRIPTS_PREFIX);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Resolve and validate a scripts/ evidence argv for execFile (no shell).
|
|
167
|
+
*
|
|
168
|
+
* @param {string} projectRoot
|
|
169
|
+
* @param {string[]} argv
|
|
170
|
+
* @returns {string[]}
|
|
171
|
+
*/
|
|
172
|
+
export function resolveEvidenceScriptsArgv(projectRoot, argv) {
|
|
173
|
+
if (!isAllowedEvidenceScriptsPath(argv[0])) {
|
|
174
|
+
return argv;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
const validated = validateWorkerLaunchScriptPath(projectRoot, argv[0]);
|
|
178
|
+
if (!validated.ok) {
|
|
179
|
+
throw new EvidenceCommandError(validated.message ?? "evidence script path invalid");
|
|
180
|
+
}
|
|
181
|
+
if (!validated.scriptPath) {
|
|
182
|
+
throw new EvidenceCommandError("evidence script path invalid");
|
|
63
183
|
}
|
|
64
184
|
|
|
65
|
-
return argv;
|
|
185
|
+
return [validated.scriptPath, ...argv.slice(1)];
|
|
66
186
|
}
|
|
67
187
|
|
|
68
188
|
/**
|
|
@@ -128,7 +248,8 @@ export function runEvidenceCommand(projectRoot, command, maxBytes = 256 * 1024)
|
|
|
128
248
|
|
|
129
249
|
try {
|
|
130
250
|
const argv = parseEvidenceCommandArgv(command);
|
|
131
|
-
const
|
|
251
|
+
const execArgv = resolveEvidenceScriptsArgv(projectRoot, argv);
|
|
252
|
+
const output = execFileSync(execArgv[0], execArgv.slice(1), {
|
|
132
253
|
cwd: projectRoot,
|
|
133
254
|
encoding: "utf-8",
|
|
134
255
|
stdio: ["ignore", "pipe", "pipe"],
|
|
@@ -15,6 +15,7 @@ import {
|
|
|
15
15
|
shouldEnforceStubContractAtLaneCommit,
|
|
16
16
|
verifyStubFileScopeMustChange,
|
|
17
17
|
} from "./contract-verify.mjs";
|
|
18
|
+
import { DEFAULT_WORKTREE_SETUP_IGNORE_PATHS } from "../config/spine-config-load.mjs";
|
|
18
19
|
|
|
19
20
|
/**
|
|
20
21
|
* @param {string} worktreePath
|
|
@@ -72,13 +73,50 @@ function pathMatchesIgnorePattern(filePath, pattern) {
|
|
|
72
73
|
return path.posix.basename(normalizedPath) === normalizedPattern;
|
|
73
74
|
}
|
|
74
75
|
|
|
76
|
+
/**
|
|
77
|
+
* Local fileScope check (avoid importing lane-dirty-check-git — circular via filterPorcelain).
|
|
78
|
+
*
|
|
79
|
+
* @param {string} filePath
|
|
80
|
+
* @param {string[]} [fileScopePaths]
|
|
81
|
+
*/
|
|
82
|
+
function isPathInFileScope(filePath, fileScopePaths) {
|
|
83
|
+
if (!filePath || !Array.isArray(fileScopePaths) || fileScopePaths.length === 0) {
|
|
84
|
+
return false;
|
|
85
|
+
}
|
|
86
|
+
const normalizedPath = filePath.replace(/\\/g, "/");
|
|
87
|
+
for (const rel of fileScopePaths) {
|
|
88
|
+
if (!rel || typeof rel !== "string") continue;
|
|
89
|
+
const normalized = rel.replace(/\\/g, "/");
|
|
90
|
+
const prefix = normalized.endsWith("/") ? normalized : `${normalized}/`;
|
|
91
|
+
if (normalizedPath === normalized || normalizedPath.startsWith(prefix)) {
|
|
92
|
+
return true;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
return false;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Hook ignore path unless task fileScope explicitly lists it (SP-640 / #200).
|
|
100
|
+
*
|
|
101
|
+
* @param {string} filePath
|
|
102
|
+
* @param {string[]} ignorePatterns
|
|
103
|
+
* @param {string[]} [fileScopePaths]
|
|
104
|
+
*/
|
|
105
|
+
function shouldSkipHookIgnorePath(filePath, ignorePatterns, fileScopePaths) {
|
|
106
|
+
if (!Array.isArray(ignorePatterns) || ignorePatterns.length === 0) return false;
|
|
107
|
+
if (isPathInFileScope(filePath, fileScopePaths)) return false;
|
|
108
|
+
return ignorePatterns.some((pattern) => pathMatchesIgnorePattern(filePath, pattern));
|
|
109
|
+
}
|
|
110
|
+
|
|
75
111
|
/**
|
|
76
112
|
* Drop porcelain lines whose path matches any ignore pattern (basename or full path).
|
|
113
|
+
* Paths listed in `fileScopePaths` are kept even when they match ignore patterns.
|
|
77
114
|
*
|
|
78
115
|
* @param {string} porcelain
|
|
79
116
|
* @param {string[]} ignorePatterns
|
|
117
|
+
* @param {string[]} [fileScopePaths]
|
|
80
118
|
*/
|
|
81
|
-
export function filterPorcelain(porcelain, ignorePatterns) {
|
|
119
|
+
export function filterPorcelain(porcelain, ignorePatterns, fileScopePaths = []) {
|
|
82
120
|
if (!porcelain?.trim()) return "";
|
|
83
121
|
if (!Array.isArray(ignorePatterns) || ignorePatterns.length === 0) {
|
|
84
122
|
return porcelain;
|
|
@@ -92,8 +130,8 @@ export function filterPorcelain(porcelain, ignorePatterns) {
|
|
|
92
130
|
kept.push(line);
|
|
93
131
|
continue;
|
|
94
132
|
}
|
|
95
|
-
|
|
96
|
-
|
|
133
|
+
if (shouldSkipHookIgnorePath(filePath, ignorePatterns, fileScopePaths)) continue;
|
|
134
|
+
kept.push(line);
|
|
97
135
|
}
|
|
98
136
|
return kept.length === 0 ? "" : kept.join("\n");
|
|
99
137
|
}
|
|
@@ -152,7 +190,9 @@ function listPorcelainPaths(porcelain) {
|
|
|
152
190
|
* @param {string} params.taskFolder Absolute path to task folder (for `.DONE` check)
|
|
153
191
|
* @param {string} [params.projectRoot] Main repo root for git identity resolution
|
|
154
192
|
* @param {string} [params.baseBranch] Base branch for stub contract diff (default main)
|
|
155
|
-
* @
|
|
193
|
+
* @param {string[]} [params.ignorePatterns] Hook paths to skip (default includes `.venv`)
|
|
194
|
+
* @param {string[]} [params.fileScopePaths] Task fileScope — paths here are never skipped
|
|
195
|
+
* @returns {{ ok: true, committed: boolean, commitSha?: string, skippedGitignoredPaths?: string[], skippedIgnorePaths?: string[] } | { ok: false, error: string, failureClass: string, gitignoredPaths?: string[] }}
|
|
156
196
|
*/
|
|
157
197
|
export function commitLaneWorktree({
|
|
158
198
|
worktreePath,
|
|
@@ -162,14 +202,30 @@ export function commitLaneWorktree({
|
|
|
162
202
|
taskFolder,
|
|
163
203
|
projectRoot,
|
|
164
204
|
baseBranch = "main",
|
|
205
|
+
ignorePatterns,
|
|
206
|
+
fileScopePaths = [],
|
|
165
207
|
}) {
|
|
166
208
|
const identityRoot = projectRoot ?? worktreePath;
|
|
209
|
+
const effectiveIgnorePatterns = Array.isArray(ignorePatterns)
|
|
210
|
+
? ignorePatterns
|
|
211
|
+
: [...DEFAULT_WORKTREE_SETUP_IGNORE_PATHS];
|
|
167
212
|
try {
|
|
168
213
|
const porcelain = gitPorcelain(worktreePath);
|
|
169
214
|
const dirtyPaths = listPorcelainPaths(porcelain);
|
|
215
|
+
const skippedIgnorePaths = dirtyPaths.filter((filePath) =>
|
|
216
|
+
shouldSkipHookIgnorePath(filePath, effectiveIgnorePatterns, fileScopePaths),
|
|
217
|
+
);
|
|
218
|
+
const stageCandidatePaths = dirtyPaths.filter(
|
|
219
|
+
(filePath) => !shouldSkipHookIgnorePath(filePath, effectiveIgnorePatterns, fileScopePaths),
|
|
220
|
+
);
|
|
170
221
|
const ignoredUntrackedPaths = listIgnoredUntrackedPaths(worktreePath);
|
|
171
|
-
|
|
172
|
-
|
|
222
|
+
// Only-hook-noise dirty (e.g. untracked `.venv` symlink) is not a DirtyWorktree failure.
|
|
223
|
+
if (stageCandidatePaths.length === 0 && ignoredUntrackedPaths.length === 0) {
|
|
224
|
+
return {
|
|
225
|
+
ok: true,
|
|
226
|
+
committed: false,
|
|
227
|
+
skippedIgnorePaths,
|
|
228
|
+
};
|
|
173
229
|
}
|
|
174
230
|
|
|
175
231
|
const donePath = path.join(taskFolder, ".DONE");
|
|
@@ -191,7 +247,7 @@ export function commitLaneWorktree({
|
|
|
191
247
|
worktreePath,
|
|
192
248
|
parsedContract,
|
|
193
249
|
baseBranch,
|
|
194
|
-
|
|
250
|
+
stageCandidatePaths,
|
|
195
251
|
);
|
|
196
252
|
if (!scopeCheck.ok) {
|
|
197
253
|
return {
|
|
@@ -203,7 +259,10 @@ export function commitLaneWorktree({
|
|
|
203
259
|
}
|
|
204
260
|
}
|
|
205
261
|
|
|
206
|
-
const { stageable, skipped: gitignoredDirtyPaths } = filterGitignoredPaths(
|
|
262
|
+
const { stageable, skipped: gitignoredDirtyPaths } = filterGitignoredPaths(
|
|
263
|
+
worktreePath,
|
|
264
|
+
stageCandidatePaths,
|
|
265
|
+
);
|
|
207
266
|
const gitignoredPaths = [...new Set([...gitignoredDirtyPaths, ...ignoredUntrackedPaths])];
|
|
208
267
|
if (stageable.length === 0 && gitignoredPaths.length > 0) {
|
|
209
268
|
const { indexTracked, worktreeOnly } = classifyGitignoredPaths(worktreePath, gitignoredPaths);
|
|
@@ -215,6 +274,15 @@ export function commitLaneWorktree({
|
|
|
215
274
|
};
|
|
216
275
|
}
|
|
217
276
|
|
|
277
|
+
if (stageable.length === 0) {
|
|
278
|
+
return {
|
|
279
|
+
ok: true,
|
|
280
|
+
committed: false,
|
|
281
|
+
skippedIgnorePaths,
|
|
282
|
+
skippedGitignoredPaths: gitignoredPaths,
|
|
283
|
+
};
|
|
284
|
+
}
|
|
285
|
+
|
|
218
286
|
const currentBranch = git(worktreePath, ["rev-parse", "--abbrev-ref", "HEAD"], identityRoot);
|
|
219
287
|
if (currentBranch !== taskBranch) {
|
|
220
288
|
git(worktreePath, ["checkout", taskBranch], identityRoot);
|
|
@@ -228,6 +296,7 @@ export function commitLaneWorktree({
|
|
|
228
296
|
committed: true,
|
|
229
297
|
commitSha,
|
|
230
298
|
skippedGitignoredPaths: [...addResult.skipped, ...gitignoredPaths],
|
|
299
|
+
skippedIgnorePaths,
|
|
231
300
|
};
|
|
232
301
|
} catch (err) {
|
|
233
302
|
const message = err instanceof Error ? err.message : String(err);
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
// @ts-nocheck
|
|
2
|
+
/**
|
|
3
|
+
* Batch lifecycle archive and worktree cleanup helpers (FR-BATCH-15/16).
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import fs from "node:fs";
|
|
7
|
+
import path from "node:path";
|
|
8
|
+
import { appendJournalEvent } from "./journal.mjs";
|
|
9
|
+
import { clearActiveBatchStateIfMatches } from "./batch-state-io.mjs";
|
|
10
|
+
import { removeLaneWorktrees, maxLaneNumberFromBatchState } from "./worktree.mjs";
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @param {string} projectRoot
|
|
14
|
+
* @param {string} batchId
|
|
15
|
+
*/
|
|
16
|
+
export function archiveBatchStatePath(projectRoot, batchId) {
|
|
17
|
+
return path.join(projectRoot, ".spine", "runtime", batchId, "archive", "batch-state.json");
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* @param {string} projectRoot
|
|
22
|
+
* @param {string} batchId
|
|
23
|
+
* @param {unknown} raw
|
|
24
|
+
*/
|
|
25
|
+
export function archiveBatchState(projectRoot, batchId, raw) {
|
|
26
|
+
const archivePath = archiveBatchStatePath(projectRoot, batchId);
|
|
27
|
+
fs.mkdirSync(path.dirname(archivePath), { recursive: true });
|
|
28
|
+
fs.writeFileSync(archivePath, `${JSON.stringify(raw, null, 2)}\n`, "utf-8");
|
|
29
|
+
|
|
30
|
+
const fd = fs.openSync(archivePath, "r");
|
|
31
|
+
try {
|
|
32
|
+
fs.fsyncSync(fd);
|
|
33
|
+
} finally {
|
|
34
|
+
fs.closeSync(fd);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return archivePath;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* @param {string|null} batchStatePath
|
|
42
|
+
* @param {string} batchId
|
|
43
|
+
*/
|
|
44
|
+
export function clearCompletedBatchState(batchStatePath, batchId) {
|
|
45
|
+
clearActiveBatchStateIfMatches(batchStatePath, batchId);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* @param {object|null|undefined} config
|
|
50
|
+
*/
|
|
51
|
+
function shouldCleanupWorktreesOnComplete(config) {
|
|
52
|
+
if (config?.lanes?.cleanupWorktreesOnComplete === false) return false;
|
|
53
|
+
return true;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* @param {object} params
|
|
58
|
+
* @param {string} params.projectRoot
|
|
59
|
+
* @param {string} params.batchId
|
|
60
|
+
* @param {unknown} params.batchState
|
|
61
|
+
* @param {object} params.config
|
|
62
|
+
*/
|
|
63
|
+
export function cleanupBatchLaneWorktrees({ projectRoot, batchId, batchState, config }) {
|
|
64
|
+
if (!shouldCleanupWorktreesOnComplete(config)) return;
|
|
65
|
+
const laneCount = maxLaneNumberFromBatchState(batchState);
|
|
66
|
+
removeLaneWorktrees(projectRoot, batchId, laneCount);
|
|
67
|
+
appendJournalEvent(projectRoot, batchId, "batch.worktrees_cleaned", {
|
|
68
|
+
batchId,
|
|
69
|
+
laneCount,
|
|
70
|
+
});
|
|
71
|
+
}
|
package/src/batch/lifecycle.mjs
CHANGED
|
@@ -3,9 +3,18 @@
|
|
|
3
3
|
* Batch lifecycle: dismiss, complete, archive-first (FR-BATCH-15/16, §18.6).
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
-
import fs from "node:fs";
|
|
7
6
|
import path from "node:path";
|
|
8
7
|
import { buildDiagnosisOutput } from "./diagnosis.mjs";
|
|
8
|
+
import {
|
|
9
|
+
buildPendingLaneLandSuggestedCommand,
|
|
10
|
+
findPendingLaneLandTasks,
|
|
11
|
+
} from "./diagnosis-pending-lane.mjs";
|
|
12
|
+
import {
|
|
13
|
+
archiveBatchState,
|
|
14
|
+
cleanupBatchLaneWorktrees,
|
|
15
|
+
clearCompletedBatchState,
|
|
16
|
+
} from "./lifecycle-archive.mjs";
|
|
17
|
+
export { archiveBatchStatePath } from "./lifecycle-archive.mjs";
|
|
9
18
|
import { assertOrchIntegratable } from "./integrate.mjs";
|
|
10
19
|
import { loadSpineConfig } from "../config/spine-config-load.mjs";
|
|
11
20
|
import { appendJournalEvent } from "./journal.mjs";
|
|
@@ -13,12 +22,8 @@ import { recordBatchTerminalMetric } from "./metrics.mjs";
|
|
|
13
22
|
import { writeBatchPostMortem } from "./postmortem.mjs";
|
|
14
23
|
import { appendBatchHistoryEntry, clearBatchEnginePid, saveSpineBatchState } from "./state.mjs";
|
|
15
24
|
import { loadBatchStateFile, parseBatchState, reconcileBatch } from "./reconcile.mjs";
|
|
16
|
-
import {
|
|
17
|
-
clearActiveBatchStateIfMatches,
|
|
18
|
-
readAliveBatchEnginePid,
|
|
19
|
-
} from "./batch-state-io.mjs";
|
|
25
|
+
import { readAliveBatchEnginePid } from "./batch-state-io.mjs";
|
|
20
26
|
import { terminateLaneWorkers } from "./worker-host.mjs";
|
|
21
|
-
import { removeLaneWorktrees, maxLaneNumberFromBatchState } from "./worktree.mjs";
|
|
22
27
|
import { terminateSupervisorIfRunning } from "./supervisor-spawn.mjs";
|
|
23
28
|
import { bumpDashboardInvalidateSignal } from "../dashboard/cache-invalidate.mjs";
|
|
24
29
|
|
|
@@ -81,67 +86,6 @@ export function recordMergeBlocked({
|
|
|
81
86
|
};
|
|
82
87
|
}
|
|
83
88
|
|
|
84
|
-
/**
|
|
85
|
-
* @param {string} projectRoot
|
|
86
|
-
* @param {string} batchId
|
|
87
|
-
*/
|
|
88
|
-
export function archiveBatchStatePath(projectRoot, batchId) {
|
|
89
|
-
return path.join(projectRoot, ".spine", "runtime", batchId, "archive", "batch-state.json");
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
/**
|
|
93
|
-
* @param {string} projectRoot
|
|
94
|
-
* @param {string} batchId
|
|
95
|
-
* @param {unknown} raw
|
|
96
|
-
*/
|
|
97
|
-
function archiveBatchState(projectRoot, batchId, raw) {
|
|
98
|
-
const archivePath = archiveBatchStatePath(projectRoot, batchId);
|
|
99
|
-
fs.mkdirSync(path.dirname(archivePath), { recursive: true });
|
|
100
|
-
fs.writeFileSync(archivePath, `${JSON.stringify(raw, null, 2)}\n`, "utf-8");
|
|
101
|
-
|
|
102
|
-
const fd = fs.openSync(archivePath, "r");
|
|
103
|
-
try {
|
|
104
|
-
fs.fsyncSync(fd);
|
|
105
|
-
} finally {
|
|
106
|
-
fs.closeSync(fd);
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
return archivePath;
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
/**
|
|
113
|
-
* @param {string|null} batchStatePath
|
|
114
|
-
* @param {string} batchId
|
|
115
|
-
*/
|
|
116
|
-
function clearCompletedBatchState(batchStatePath, batchId) {
|
|
117
|
-
clearActiveBatchStateIfMatches(batchStatePath, batchId);
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
/**
|
|
121
|
-
* @param {object|null|undefined} config
|
|
122
|
-
*/
|
|
123
|
-
function shouldCleanupWorktreesOnComplete(config) {
|
|
124
|
-
if (config?.lanes?.cleanupWorktreesOnComplete === false) return false;
|
|
125
|
-
return true;
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
/**
|
|
129
|
-
* @param {object} params
|
|
130
|
-
* @param {string} params.projectRoot
|
|
131
|
-
* @param {string} params.batchId
|
|
132
|
-
* @param {unknown} params.batchState
|
|
133
|
-
* @param {object} params.config
|
|
134
|
-
*/
|
|
135
|
-
function cleanupBatchLaneWorktrees({ projectRoot, batchId, batchState, config }) {
|
|
136
|
-
if (!shouldCleanupWorktreesOnComplete(config)) return;
|
|
137
|
-
const laneCount = maxLaneNumberFromBatchState(batchState);
|
|
138
|
-
removeLaneWorktrees(projectRoot, batchId, laneCount);
|
|
139
|
-
appendJournalEvent(projectRoot, batchId, "batch.worktrees_cleaned", {
|
|
140
|
-
batchId,
|
|
141
|
-
laneCount,
|
|
142
|
-
});
|
|
143
|
-
}
|
|
144
|
-
|
|
145
89
|
/**
|
|
146
90
|
* @param {object} signals
|
|
147
91
|
*/
|
|
@@ -445,6 +389,22 @@ export function completeBatch(ctx) {
|
|
|
445
389
|
};
|
|
446
390
|
}
|
|
447
391
|
|
|
392
|
+
const pendingLaneLandTasks = findPendingLaneLandTasks(signals.tasks);
|
|
393
|
+
if (pendingLaneLandTasks.length > 0 && Boolean(signals.git?.orchMergedToBase)) {
|
|
394
|
+
const pendingTaskIds = pendingLaneLandTasks.map((task) => String(task.taskId ?? "")).filter(Boolean);
|
|
395
|
+
return {
|
|
396
|
+
ok: false,
|
|
397
|
+
exitCode: 1,
|
|
398
|
+
error: "pending_lane_land",
|
|
399
|
+
diagnosis: "pending_lane_land",
|
|
400
|
+
batchId,
|
|
401
|
+
pendingTaskIds,
|
|
402
|
+
headline: `Batch ${batchId} has lane work not on ${signals.git?.baseBranch ?? "main"} (${pendingTaskIds.join(", ")}) — complete refused`,
|
|
403
|
+
suggestedCommand: buildPendingLaneLandSuggestedCommand(batchId, pendingLaneLandTasks),
|
|
404
|
+
alternatives: ["spine status --diagnose", "spine batch salvage --batch <batchId> --dry-run"],
|
|
405
|
+
};
|
|
406
|
+
}
|
|
407
|
+
|
|
448
408
|
terminateSupervisorIfRunning(projectRoot, batchId, "batch_complete");
|
|
449
409
|
|
|
450
410
|
const archivePath = archiveBatchState(projectRoot, batchId, loaded.raw);
|
|
@@ -281,6 +281,43 @@ function hasFiniteWorkerPidForRunningTasks(lanes, runningTasks) {
|
|
|
281
281
|
return false;
|
|
282
282
|
}
|
|
283
283
|
|
|
284
|
+
/**
|
|
285
|
+
* No running task lane has a live worker PID (#203 multi-lane).
|
|
286
|
+
* Missing / non-finite workerPid counts as absent (not live).
|
|
287
|
+
*
|
|
288
|
+
* @param {unknown[]} lanes
|
|
289
|
+
* @param {Array<{ taskId?: string, laneNumber?: number|null }>} runningTasks
|
|
290
|
+
*/
|
|
291
|
+
function noLiveWorkerPidForRunningTasks(lanes, runningTasks) {
|
|
292
|
+
if (runningTasks.length === 0) return false;
|
|
293
|
+
for (const task of runningTasks) {
|
|
294
|
+
const lane = findLane(lanes, task.laneNumber);
|
|
295
|
+
const workerPid = Number(/** @type {{ workerPid?: number }} */ (lane)?.workerPid);
|
|
296
|
+
if (Number.isFinite(workerPid) && workerPid > 0 && isProcessAlive(workerPid)) {
|
|
297
|
+
return false;
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
return true;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/**
|
|
304
|
+
* Prefer the first running task whose lane worker PID is stale (dead-engine orphan #203).
|
|
305
|
+
*
|
|
306
|
+
* @param {Array<{ taskId: string, laneNumber?: number|null }>} runningTasks
|
|
307
|
+
* @param {unknown[]} lanes
|
|
308
|
+
* @returns {string|null}
|
|
309
|
+
*/
|
|
310
|
+
function pickDeadEngineOrphanTaskId(runningTasks, lanes) {
|
|
311
|
+
for (const task of runningTasks) {
|
|
312
|
+
const lane = findLane(lanes, task.laneNumber);
|
|
313
|
+
const workerPid = Number(/** @type {{ workerPid?: number }} */ (lane)?.workerPid);
|
|
314
|
+
if (Number.isFinite(workerPid) && workerPid > 0 && !isProcessAlive(workerPid)) {
|
|
315
|
+
return task.taskId;
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
return runningTasks[0]?.taskId ?? null;
|
|
319
|
+
}
|
|
320
|
+
|
|
284
321
|
/**
|
|
285
322
|
* @param {unknown[]} lanes
|
|
286
323
|
* @param {number|null|undefined} laneNumber
|
|
@@ -318,6 +355,37 @@ export function detectOrphanRunning(ctx) {
|
|
|
318
355
|
}
|
|
319
356
|
|
|
320
357
|
const scopedJournalEvents = journalEventsSinceResume(ctx.journalEvents ?? [], ctx.raw);
|
|
358
|
+
const enginePid = readBatchEnginePid(ctx.raw);
|
|
359
|
+
const suppressPausedResumeEngineOrphan = shouldSuppressPausedResumeEngineOrphan(
|
|
360
|
+
ctx,
|
|
361
|
+
scopedJournalEvents,
|
|
362
|
+
);
|
|
363
|
+
|
|
364
|
+
// Dead or cleared engine + no live workers across multiple lanes → engine orphan (#203).
|
|
365
|
+
// Covers cleared enginePid and missing workerPid; single-lane stays lane/worker_orphaned.
|
|
366
|
+
// Only while phase is running — paused multi-lane must stay paused for resume DX.
|
|
367
|
+
const distinctRunningLanes = new Set(
|
|
368
|
+
runningTasks
|
|
369
|
+
.map((task) => (task.laneNumber == null ? null : Number(task.laneNumber)))
|
|
370
|
+
.filter((laneNumber) => Number.isFinite(laneNumber)),
|
|
371
|
+
);
|
|
372
|
+
const multiLaneRunning = distinctRunningLanes.size > 1;
|
|
373
|
+
const engineDeadOrCleared = enginePid == null || !isProcessAlive(enginePid);
|
|
374
|
+
if (
|
|
375
|
+
ctx.phase === "running" &&
|
|
376
|
+
multiLaneRunning &&
|
|
377
|
+
engineDeadOrCleared &&
|
|
378
|
+
!journalHasTerminalBatchEvent(scopedJournalEvents) &&
|
|
379
|
+
!suppressPausedResumeEngineOrphan &&
|
|
380
|
+
noLiveWorkerPidForRunningTasks(ctx.lanes, runningTasks)
|
|
381
|
+
) {
|
|
382
|
+
/** @type {OrphanRunningSignal} */
|
|
383
|
+
return {
|
|
384
|
+
kind: "engine",
|
|
385
|
+
taskId: pickDeadEngineOrphanTaskId(runningTasks, ctx.lanes),
|
|
386
|
+
enginePid: enginePid ?? null,
|
|
387
|
+
};
|
|
388
|
+
}
|
|
321
389
|
|
|
322
390
|
for (const task of runningTasks) {
|
|
323
391
|
const lane = findLane(ctx.lanes, task.laneNumber);
|
|
@@ -347,11 +415,6 @@ export function detectOrphanRunning(ctx) {
|
|
|
347
415
|
}
|
|
348
416
|
}
|
|
349
417
|
|
|
350
|
-
const enginePid = readBatchEnginePid(ctx.raw);
|
|
351
|
-
const suppressPausedResumeEngineOrphan = shouldSuppressPausedResumeEngineOrphan(
|
|
352
|
-
ctx,
|
|
353
|
-
scopedJournalEvents,
|
|
354
|
-
);
|
|
355
418
|
if (
|
|
356
419
|
enginePid &&
|
|
357
420
|
!isProcessAlive(enginePid) &&
|