pi-spine 2.9.0 → 2.10.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/contract-exec.mjs +12 -2
- package/src/batch/contract-parse.mjs +55 -0
- package/src/batch/contract-verify.mjs +41 -1
- package/src/batch/engine-lanes/matrix-run.mjs +532 -0
- package/src/batch/engine-lanes/matrix.mjs +314 -0
- package/src/batch/engine-lanes.mjs +81 -48
- package/src/batch/engine.mjs +3 -4
- package/src/batch/journal.mjs +5 -0
- package/src/batch/lane-commit.mjs +54 -4
- package/src/batch/lane-dirty-check-git.mjs +63 -0
- package/src/batch/worker-host.mjs +38 -21
- package/src/batch/worker-spawn.mjs +49 -0
- package/src/planner/lanes.mjs +42 -8
- package/src/planner/matrix.mjs +122 -0
- package/src/planner/waves.mjs +39 -8
- package/src/tasks/packet/parse-prompt.mjs +40 -1
|
@@ -0,0 +1,314 @@
|
|
|
1
|
+
// @ts-nocheck
|
|
2
|
+
/**
|
|
3
|
+
* Matrix sub-lane execution (SP-671 / #217).
|
|
4
|
+
*
|
|
5
|
+
* Because `buildPlan` does not propagate `matrix` into its `tasksById`, the
|
|
6
|
+
* planner's virtual sub-lane expansion is inert in production. The engine
|
|
7
|
+
* therefore drives matrix execution itself: when a task's packet declares a
|
|
8
|
+
* `## Matrix` table, each row is run as an independent sub-lane in its own
|
|
9
|
+
* worktree, the substituted command (execution-only or LLM worker) runs per
|
|
10
|
+
* row, and the parent task aggregates the per-row outcomes.
|
|
11
|
+
*
|
|
12
|
+
* A matrix task succeeds only when every row succeeds; any failed row fails the
|
|
13
|
+
* whole task and surfaces the failing row id. Rows respect `lanes.maxParallel`.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import fs from "node:fs";
|
|
17
|
+
import path from "node:path";
|
|
18
|
+
import { spawn } from "node:child_process";
|
|
19
|
+
import { batchWorktreeDir } from "../worktree.mjs";
|
|
20
|
+
import { loadTaskPacket } from "../../tasks/packet/index.mjs";
|
|
21
|
+
import {
|
|
22
|
+
deriveMatrixRowId,
|
|
23
|
+
substituteMatrixVariables,
|
|
24
|
+
} from "../../planner/matrix.mjs";
|
|
25
|
+
import { gitExec } from "../git-exec.mjs";
|
|
26
|
+
import { normalizeLaneWorktreeGitPaths } from "../worktree.mjs";
|
|
27
|
+
import { appendJournalEvent } from "../journal.mjs";
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Read a task folder's packet and extract its matrix rows, or null when the
|
|
31
|
+
* task is not a matrix task.
|
|
32
|
+
*
|
|
33
|
+
* @param {string} taskFolderPath Absolute path to the task folder (parent for matrix).
|
|
34
|
+
* @returns {{ rows: Array<{ rowId: string, values: Record<string,string> }>, columns: string[], type: string } | null}
|
|
35
|
+
*/
|
|
36
|
+
export function loadMatrixTaskRows(taskFolderPath) {
|
|
37
|
+
if (!taskFolderPath || !fs.existsSync(path.join(taskFolderPath, "PROMPT.md"))) {
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
let packet;
|
|
41
|
+
try {
|
|
42
|
+
packet = loadTaskPacket(taskFolderPath);
|
|
43
|
+
} catch {
|
|
44
|
+
return null;
|
|
45
|
+
}
|
|
46
|
+
const prompt = packet?.prompt;
|
|
47
|
+
const matrix = Array.isArray(prompt?.matrix) ? prompt.matrix : null;
|
|
48
|
+
if (!matrix || matrix.length === 0) return null;
|
|
49
|
+
const columns = Array.isArray(prompt.matrixColumns) ? prompt.matrixColumns : [];
|
|
50
|
+
const rows = matrix.map((values) => ({
|
|
51
|
+
rowId: deriveMatrixRowId(values, columns),
|
|
52
|
+
values,
|
|
53
|
+
}));
|
|
54
|
+
return { rows, columns, type: prompt.type === "execute" ? "execute" : "llm" };
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Slugify a token for use in worktree dir names and git branch names.
|
|
59
|
+
* Keeps it filesystem- and ref-safe: lowercase, `[a-z0-9_]+`, `_` for separators.
|
|
60
|
+
*
|
|
61
|
+
* @param {string} token
|
|
62
|
+
* @returns {string}
|
|
63
|
+
*/
|
|
64
|
+
export function slugifyMatrixToken(token) {
|
|
65
|
+
return String(token ?? "")
|
|
66
|
+
.trim()
|
|
67
|
+
.toLowerCase()
|
|
68
|
+
.replace(/[^a-z0-9]+/g, "_")
|
|
69
|
+
.replace(/^_+|_+$/g, "") || "row";
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Deterministic worktree directory name for a matrix sub-lane.
|
|
74
|
+
* Matches the PROMPT example shape `lane-1-sp-669-a_shell_a`.
|
|
75
|
+
*
|
|
76
|
+
* @param {number} laneNumber
|
|
77
|
+
* @param {string} parentTaskId
|
|
78
|
+
* @param {string} rowId
|
|
79
|
+
* @returns {string}
|
|
80
|
+
*/
|
|
81
|
+
export function matrixWorktreeDir(laneNumber, parentTaskId, rowId) {
|
|
82
|
+
const parentSlug = slugifyMatrixToken(parentTaskId);
|
|
83
|
+
const rowSlug = slugifyMatrixToken(rowId);
|
|
84
|
+
return `lane-${laneNumber}-${parentSlug}-${rowSlug}`;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Absolute worktree path for a matrix sub-lane, nested under the batch worktree dir.
|
|
89
|
+
*
|
|
90
|
+
* @param {string} projectRoot
|
|
91
|
+
* @param {string} batchId
|
|
92
|
+
* @param {number} laneNumber
|
|
93
|
+
* @param {string} parentTaskId
|
|
94
|
+
* @param {string} rowId
|
|
95
|
+
* @returns {string}
|
|
96
|
+
*/
|
|
97
|
+
export function matrixWorktreePath(projectRoot, batchId, laneNumber, parentTaskId, rowId) {
|
|
98
|
+
const dir = matrixWorktreeDir(laneNumber, parentTaskId, rowId);
|
|
99
|
+
return path.join(projectRoot, ".worktrees", `spine-${batchId}`, dir);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Deterministic git branch name for a matrix sub-lane.
|
|
104
|
+
*
|
|
105
|
+
* @param {string} batchId
|
|
106
|
+
* @param {number} laneNumber
|
|
107
|
+
* @param {string} parentTaskId
|
|
108
|
+
* @param {string} rowId
|
|
109
|
+
* @returns {string}
|
|
110
|
+
*/
|
|
111
|
+
export function matrixSubLaneBranch(batchId, laneNumber, parentTaskId, rowId) {
|
|
112
|
+
const parentSlug = slugifyMatrixToken(parentTaskId);
|
|
113
|
+
const rowSlug = slugifyMatrixToken(rowId);
|
|
114
|
+
return `task/spine-matrix-${laneNumber}-${batchId}-${parentSlug}-${rowSlug}`;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Provision a dedicated worktree for a matrix sub-lane off `baseRef` (the lane
|
|
119
|
+
* task branch). Reuses git-worktree provisioning + gitdir normalization so host
|
|
120
|
+
* and devcontainer git both stay healthy (SP-101).
|
|
121
|
+
*
|
|
122
|
+
* @param {object} params
|
|
123
|
+
* @param {string} params.projectRoot
|
|
124
|
+
* @param {string} params.batchId
|
|
125
|
+
* @param {number} params.laneNumber
|
|
126
|
+
* @param {string} params.parentTaskId
|
|
127
|
+
* @param {string} params.rowId
|
|
128
|
+
* @param {string} params.baseRef Git ref the row worktree branches from.
|
|
129
|
+
* @returns {{ worktreePath: string, branch: string }}
|
|
130
|
+
*/
|
|
131
|
+
export function provisionMatrixSubLaneWorktree({
|
|
132
|
+
projectRoot,
|
|
133
|
+
batchId,
|
|
134
|
+
laneNumber,
|
|
135
|
+
parentTaskId,
|
|
136
|
+
rowId,
|
|
137
|
+
baseRef,
|
|
138
|
+
}) {
|
|
139
|
+
const worktreePath = matrixWorktreePath(projectRoot, batchId, laneNumber, parentTaskId, rowId);
|
|
140
|
+
const branch = matrixSubLaneBranch(batchId, laneNumber, parentTaskId, rowId);
|
|
141
|
+
if (fs.existsSync(worktreePath)) {
|
|
142
|
+
throw new Error(`Matrix sub-lane worktree already exists: ${worktreePath}`);
|
|
143
|
+
}
|
|
144
|
+
fs.mkdirSync(path.dirname(worktreePath), { recursive: true });
|
|
145
|
+
gitExec(projectRoot, ["worktree", "add", "-b", branch, worktreePath, baseRef]);
|
|
146
|
+
normalizeLaneWorktreeGitPaths({ projectRoot, worktreePath });
|
|
147
|
+
return { worktreePath, branch };
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Remove a matrix sub-lane worktree and its branch. Idempotent.
|
|
152
|
+
*
|
|
153
|
+
* @param {string} projectRoot
|
|
154
|
+
* @param {string} worktreePath
|
|
155
|
+
* @param {string} [branch]
|
|
156
|
+
*/
|
|
157
|
+
export function removeMatrixSubLaneWorktree(projectRoot, worktreePath, branch) {
|
|
158
|
+
if (worktreePath && fs.existsSync(worktreePath)) {
|
|
159
|
+
try {
|
|
160
|
+
gitExec(projectRoot, ["worktree", "remove", "--force", worktreePath], {
|
|
161
|
+
throwOnError: false,
|
|
162
|
+
});
|
|
163
|
+
} catch {
|
|
164
|
+
fs.rmSync(worktreePath, { recursive: true, force: true, maxRetries: 3 });
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
if (branch) {
|
|
168
|
+
gitExec(projectRoot, ["branch", "-D", branch], { throwOnError: false });
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Run a shell command in a directory, resolving with exit code and combined output.
|
|
174
|
+
* Uses async spawn so concurrent rows genuinely overlap (required for maxParallel).
|
|
175
|
+
*
|
|
176
|
+
* @param {string} cwd
|
|
177
|
+
* @param {string} command
|
|
178
|
+
* @returns {Promise<{ exitCode: number, output: string }>}
|
|
179
|
+
*/
|
|
180
|
+
export function runShellInDir(cwd, command) {
|
|
181
|
+
return new Promise((resolve) => {
|
|
182
|
+
const child = spawn("/bin/sh", ["-c", command], {
|
|
183
|
+
cwd,
|
|
184
|
+
env: process.env,
|
|
185
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
186
|
+
});
|
|
187
|
+
let output = "";
|
|
188
|
+
const append = (chunk) => {
|
|
189
|
+
output += chunk.toString();
|
|
190
|
+
};
|
|
191
|
+
child.stdout?.on("data", append);
|
|
192
|
+
child.stderr?.on("data", append);
|
|
193
|
+
child.on("error", (err) => {
|
|
194
|
+
resolve({ exitCode: 1, output: output + String(err) });
|
|
195
|
+
});
|
|
196
|
+
child.on("close", (code) => {
|
|
197
|
+
resolve({ exitCode: code ?? 1, output });
|
|
198
|
+
});
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Generic concurrency-limited map. Runs `workerFn` over `items` with at most
|
|
204
|
+
* `limit` in flight. Returns per-index results and the peak concurrency observed.
|
|
205
|
+
* A worker that throws yields an Error in its result slot (does not abort siblings).
|
|
206
|
+
*
|
|
207
|
+
* @template T, R
|
|
208
|
+
* @param {T[]} items
|
|
209
|
+
* @param {number} limit
|
|
210
|
+
* @param {(item: T, index: number) => Promise<R>} workerFn
|
|
211
|
+
* @returns {Promise<{ results: R[], peakConcurrency: number }>}
|
|
212
|
+
*/
|
|
213
|
+
export async function runConcurrent(items, limit, workerFn) {
|
|
214
|
+
const effectiveLimit = Math.max(1, Math.floor(Number(limit) || 1));
|
|
215
|
+
/** @type {R[]} */
|
|
216
|
+
const results = new Array(items.length);
|
|
217
|
+
let cursor = 0;
|
|
218
|
+
let active = 0;
|
|
219
|
+
let peak = 0;
|
|
220
|
+
|
|
221
|
+
async function runOne() {
|
|
222
|
+
while (cursor < items.length) {
|
|
223
|
+
const index = cursor;
|
|
224
|
+
cursor += 1;
|
|
225
|
+
active += 1;
|
|
226
|
+
if (active > peak) peak = active;
|
|
227
|
+
try {
|
|
228
|
+
results[index] = await workerFn(items[index], index);
|
|
229
|
+
} catch (err) {
|
|
230
|
+
results[index] = /** @type {R} */ (err);
|
|
231
|
+
} finally {
|
|
232
|
+
active -= 1;
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
const runnerCount = Math.min(effectiveLimit, items.length);
|
|
238
|
+
await Promise.all(Array.from({ length: runnerCount }, () => runOne()));
|
|
239
|
+
return { results, peakConcurrency: peak };
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* Substitute `{matrix.<column>}` placeholders in a command string for a row.
|
|
244
|
+
* Returns the original command when no row is supplied (defensive).
|
|
245
|
+
*
|
|
246
|
+
* @param {string} command
|
|
247
|
+
* @param {Record<string,string>} rowValues
|
|
248
|
+
* @returns {string}
|
|
249
|
+
*/
|
|
250
|
+
export function substituteRowCommand(command, rowValues) {
|
|
251
|
+
if (!rowValues || Object.keys(rowValues).length === 0) return command;
|
|
252
|
+
return substituteMatrixVariables(command, rowValues);
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* Derive the aggregate outcome for a parent matrix task from its per-row results.
|
|
257
|
+
*
|
|
258
|
+
* @param {Array<{ rowId: string, ok: boolean }>} rowResults
|
|
259
|
+
* @returns {{ ok: boolean, failedRowIds: string[] }}
|
|
260
|
+
*/
|
|
261
|
+
export function aggregateMatrixOutcomes(rowResults) {
|
|
262
|
+
const failedRowIds = rowResults
|
|
263
|
+
.filter((row) => row && row.ok === false)
|
|
264
|
+
.map((row) => row.rowId)
|
|
265
|
+
.filter(Boolean);
|
|
266
|
+
return { ok: failedRowIds.length === 0, failedRowIds };
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* Record a matrix lifecycle journal event. Thin wrapper so callers stay terse
|
|
271
|
+
* and the `matrix.*` event types are centralized.
|
|
272
|
+
*
|
|
273
|
+
* @param {string} projectRoot
|
|
274
|
+
* @param {string} batchId
|
|
275
|
+
* @param {string} type
|
|
276
|
+
* @param {Record<string, unknown>} payload
|
|
277
|
+
*/
|
|
278
|
+
export function recordMatrixEvent(projectRoot, batchId, type, payload) {
|
|
279
|
+
return appendJournalEvent(projectRoot, batchId, type, payload);
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/**
|
|
283
|
+
* Test whether a batch worktree dir name is a matrix sub-lane (not a plain lane).
|
|
284
|
+
* Plain lanes are exactly `lane-<n>`; sub-lanes are `lane-<n>-<slug>-<slug>`.
|
|
285
|
+
*
|
|
286
|
+
* @param {string} name
|
|
287
|
+
* @returns {boolean}
|
|
288
|
+
*/
|
|
289
|
+
export function isMatrixSubLaneWorktreeDir(name) {
|
|
290
|
+
return /^lane-\d+-.+-.+$/.test(name);
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/**
|
|
294
|
+
* Remove every matrix sub-lane worktree for a batch (defensive cleanup on batch
|
|
295
|
+
* failure/abort). Plain lane worktrees are left to `removeLaneWorktrees`.
|
|
296
|
+
*
|
|
297
|
+
* @param {string} projectRoot
|
|
298
|
+
* @param {string} batchId
|
|
299
|
+
*/
|
|
300
|
+
export function removeAllMatrixSubLaneWorktrees(projectRoot, batchId) {
|
|
301
|
+
const dir = batchWorktreeDir(projectRoot, batchId);
|
|
302
|
+
if (!fs.existsSync(dir)) return;
|
|
303
|
+
for (const entry of fs.readdirSync(dir)) {
|
|
304
|
+
if (!isMatrixSubLaneWorktreeDir(entry)) continue;
|
|
305
|
+
const wtPath = path.join(dir, entry);
|
|
306
|
+
try {
|
|
307
|
+
gitExec(projectRoot, ["worktree", "remove", "--force", wtPath], {
|
|
308
|
+
throwOnError: false,
|
|
309
|
+
});
|
|
310
|
+
} catch {
|
|
311
|
+
fs.rmSync(wtPath, { recursive: true, force: true, maxRetries: 3 });
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
}
|
|
@@ -24,6 +24,8 @@ import { runWorker } from "./worker-host.mjs";
|
|
|
24
24
|
import { runCodeReviewPhase, runFinalReviewPhase } from "./engine-lanes/review.mjs";
|
|
25
25
|
import { ensureLaneSyncedForSharedScopeDeps } from "./engine-lanes/orch-sync.mjs";
|
|
26
26
|
import { resolveWorktreeSetupIgnorePaths } from "../config/spine-config-load.mjs";
|
|
27
|
+
import { loadMatrixTaskRows } from "./engine-lanes/matrix.mjs";
|
|
28
|
+
import { runMatrixTaskOnLane } from "./engine-lanes/matrix-run.mjs";
|
|
27
29
|
|
|
28
30
|
export {
|
|
29
31
|
buildTasksAndLanesFromPlan,
|
|
@@ -54,6 +56,22 @@ export {
|
|
|
54
56
|
ensureLaneSyncedForSharedScopeDeps,
|
|
55
57
|
} from "./engine-lanes/orch-sync.mjs";
|
|
56
58
|
|
|
59
|
+
export {
|
|
60
|
+
aggregateMatrixOutcomes,
|
|
61
|
+
isMatrixSubLaneWorktreeDir,
|
|
62
|
+
loadMatrixTaskRows,
|
|
63
|
+
matrixSubLaneBranch,
|
|
64
|
+
matrixWorktreeDir,
|
|
65
|
+
matrixWorktreePath,
|
|
66
|
+
provisionMatrixSubLaneWorktree,
|
|
67
|
+
removeAllMatrixSubLaneWorktrees,
|
|
68
|
+
removeMatrixSubLaneWorktree,
|
|
69
|
+
runConcurrent,
|
|
70
|
+
runShellInDir,
|
|
71
|
+
} from "./engine-lanes/matrix.mjs";
|
|
72
|
+
|
|
73
|
+
export { runMatrixTaskOnLane, runMatrixSubLane } from "./engine-lanes/matrix-run.mjs";
|
|
74
|
+
|
|
57
75
|
/**
|
|
58
76
|
* @param {string} fromPhase
|
|
59
77
|
* @param {string} toPhase
|
|
@@ -71,19 +89,14 @@ function phaseTransitionEventType(fromPhase, toPhase) {
|
|
|
71
89
|
*/
|
|
72
90
|
function recordPhaseTransition({ projectRoot, batchId, fromPhase, toPhase, extra = {} }) {
|
|
73
91
|
const type = phaseTransitionEventType(fromPhase, toPhase);
|
|
74
|
-
if (!type) return
|
|
75
|
-
|
|
92
|
+
if (!type) return;
|
|
93
|
+
appendJournalEvent(projectRoot, batchId, type, {
|
|
76
94
|
fromPhase,
|
|
77
95
|
toPhase,
|
|
78
96
|
...extra,
|
|
79
97
|
});
|
|
80
98
|
}
|
|
81
99
|
|
|
82
|
-
/**
|
|
83
|
-
* @param {object} state
|
|
84
|
-
* @param {string} newPhase
|
|
85
|
-
* @param {object} ctx
|
|
86
|
-
*/
|
|
87
100
|
export function transitionPhase(state, newPhase, ctx) {
|
|
88
101
|
const fromPhase = state.phase;
|
|
89
102
|
if (fromPhase === newPhase) return;
|
|
@@ -129,6 +142,26 @@ export async function runTaskOnLane({
|
|
|
129
142
|
}
|
|
130
143
|
const fileScopePaths = scopeResult.fileScopePaths;
|
|
131
144
|
|
|
145
|
+
// Matrix tasks fan out into per-row sub-lane worktrees (SP-671 / #217).
|
|
146
|
+
// Non-matrix tasks fall through to the single-worker path below.
|
|
147
|
+
const matrixRows = loadMatrixTaskRows(path.join(projectRoot, taskFolderRel));
|
|
148
|
+
if (matrixRows) {
|
|
149
|
+
return runMatrixTaskOnLane({
|
|
150
|
+
projectRoot,
|
|
151
|
+
state,
|
|
152
|
+
batchId,
|
|
153
|
+
baseBranch,
|
|
154
|
+
config,
|
|
155
|
+
task,
|
|
156
|
+
lane,
|
|
157
|
+
taskFolderRel,
|
|
158
|
+
laneCorrelationId,
|
|
159
|
+
fileScopePaths,
|
|
160
|
+
matrix: matrixRows,
|
|
161
|
+
maxParallel: config?.lanes?.maxParallel ?? 1,
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
|
|
132
165
|
const syncResult = ensureLaneSyncedForSharedScopeDeps({
|
|
133
166
|
projectRoot,
|
|
134
167
|
state,
|
|
@@ -244,53 +277,53 @@ export async function runTaskOnLane({
|
|
|
244
277
|
saveEngineBatchState(projectRoot, state);
|
|
245
278
|
}
|
|
246
279
|
} else {
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
laneNumber,
|
|
250
|
-
laneId: lane.laneId,
|
|
251
|
-
taskId,
|
|
252
|
-
correlationId: laneCorrelationId,
|
|
253
|
-
reason: workerResult.classification ?? "worker_failed",
|
|
254
|
-
});
|
|
255
|
-
task.status = aborted ? "aborted" : "failed";
|
|
256
|
-
task.endedAt = Date.now();
|
|
257
|
-
task.exitReason = workerResult.classification ?? "worker_failed";
|
|
258
|
-
updateSegmentForTask(state, taskId, aborted ? "aborted" : "failed");
|
|
259
|
-
recomputeTaskCounters(state);
|
|
260
|
-
saveEngineBatchState(projectRoot, state);
|
|
261
|
-
if (!aborted) {
|
|
262
|
-
const salvageFields = recordTaskFailureSalvage({
|
|
263
|
-
projectRoot,
|
|
264
|
-
batchId,
|
|
280
|
+
const aborted = workerResult.classification === "aborted";
|
|
281
|
+
appendJournalEvent(projectRoot, batchId, "lane.died", {
|
|
265
282
|
laneNumber,
|
|
266
283
|
laneId: lane.laneId,
|
|
267
284
|
taskId,
|
|
268
285
|
correlationId: laneCorrelationId,
|
|
269
|
-
|
|
270
|
-
fileScopePaths,
|
|
271
|
-
taskFolder: taskFolderInWorktree,
|
|
272
|
-
workerResult,
|
|
273
|
-
config,
|
|
274
|
-
batchPhase: state.phase,
|
|
275
|
-
taskBranch,
|
|
286
|
+
reason: workerResult.classification ?? "worker_failed",
|
|
276
287
|
});
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
288
|
+
task.status = aborted ? "aborted" : "failed";
|
|
289
|
+
task.endedAt = Date.now();
|
|
290
|
+
task.exitReason = workerResult.classification ?? "worker_failed";
|
|
291
|
+
updateSegmentForTask(state, taskId, aborted ? "aborted" : "failed");
|
|
292
|
+
recomputeTaskCounters(state);
|
|
293
|
+
saveEngineBatchState(projectRoot, state);
|
|
294
|
+
if (!aborted) {
|
|
295
|
+
const salvageFields = recordTaskFailureSalvage({
|
|
296
|
+
projectRoot,
|
|
297
|
+
batchId,
|
|
298
|
+
laneNumber,
|
|
299
|
+
laneId: lane.laneId,
|
|
300
|
+
taskId,
|
|
301
|
+
correlationId: laneCorrelationId,
|
|
302
|
+
worktreePath: wt,
|
|
303
|
+
fileScopePaths,
|
|
304
|
+
taskFolder: taskFolderInWorktree,
|
|
305
|
+
workerResult,
|
|
306
|
+
config,
|
|
307
|
+
batchPhase: state.phase,
|
|
308
|
+
taskBranch,
|
|
309
|
+
});
|
|
310
|
+
appendJournalEvent(projectRoot, batchId, "task.failed", {
|
|
311
|
+
taskId,
|
|
312
|
+
laneNumber,
|
|
313
|
+
laneId: lane.laneId,
|
|
314
|
+
correlationId: laneCorrelationId,
|
|
315
|
+
...workerResult,
|
|
316
|
+
...salvageFields,
|
|
317
|
+
});
|
|
318
|
+
}
|
|
319
|
+
recordLaneTaskMetric({
|
|
320
|
+
projectRoot,
|
|
321
|
+
batchId,
|
|
322
|
+
task,
|
|
323
|
+
config,
|
|
324
|
+
taskFolder: taskFolderInWorktree,
|
|
284
325
|
});
|
|
285
|
-
|
|
286
|
-
recordLaneTaskMetric({
|
|
287
|
-
projectRoot,
|
|
288
|
-
batchId,
|
|
289
|
-
task,
|
|
290
|
-
config,
|
|
291
|
-
taskFolder: taskFolderInWorktree,
|
|
292
|
-
});
|
|
293
|
-
return { ok: false, aborted, workerResult };
|
|
326
|
+
return { ok: false, aborted, workerResult };
|
|
294
327
|
}
|
|
295
328
|
}
|
|
296
329
|
|
package/src/batch/engine.mjs
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
// @ts-check
|
|
2
|
-
/**
|
|
3
|
-
* pi-spine batch engine (Phase 2 single-lane, Phase 3 multi-lane + §17.4 merge policy).
|
|
4
|
-
*/
|
|
2
|
+
/** pi-spine batch engine (Phase 2 single-lane, Phase 3 multi-lane + §17.4 merge policy). */
|
|
5
3
|
|
|
6
4
|
import fs from "node:fs";
|
|
7
5
|
import path from "node:path";
|
|
@@ -42,6 +40,7 @@ import { filterPlanToWave } from "../planner/wave-scope.mjs";
|
|
|
42
40
|
import {
|
|
43
41
|
buildTasksAndLanesFromPlan,
|
|
44
42
|
mergeWaveLanesToOrch,
|
|
43
|
+
removeAllMatrixSubLaneWorktrees,
|
|
45
44
|
runTaskOnLane,
|
|
46
45
|
skipTaskDoneOnDisk,
|
|
47
46
|
transitionPhase,
|
|
@@ -75,7 +74,6 @@ export {
|
|
|
75
74
|
export { loadTaskFileScopePaths, mergeLaneToOrch } from "./engine-lanes.mjs";
|
|
76
75
|
export { detectNestedWorkerContext } from "./batch-guards.mjs";
|
|
77
76
|
|
|
78
|
-
/** Widened so terminal transitions can assign endedAt/lastError without casts. */
|
|
79
77
|
/** @typedef {Omit<ReturnType<typeof createInitialBatchState>, "endedAt" | "lastError"> & { endedAt: number | null; lastError: string | null }} SpineBatchState */
|
|
80
78
|
|
|
81
79
|
/**
|
|
@@ -492,6 +490,7 @@ export async function startBatch({
|
|
|
492
490
|
extra: { error: message },
|
|
493
491
|
});
|
|
494
492
|
saveEngineBatchState(projectRoot, state);
|
|
493
|
+
removeAllMatrixSubLaneWorktrees(projectRoot, batchId);
|
|
495
494
|
removeLaneWorktrees(projectRoot, batchId, maxLaneNumber);
|
|
496
495
|
return { ok: false, exitCode: 1, batchId, error: message };
|
|
497
496
|
}
|
package/src/batch/journal.mjs
CHANGED
|
@@ -344,6 +344,10 @@ export function summarizeJournalEvent(event) {
|
|
|
344
344
|
}
|
|
345
345
|
if (payload.workerOutputLogRef) parts.push(`→ ${payload.workerOutputLogRef}`);
|
|
346
346
|
if (payload.exitReason) parts.push(String(payload.exitReason));
|
|
347
|
+
if (payload.rowId) parts.push(`row ${payload.rowId}`);
|
|
348
|
+
if (Array.isArray(payload.failedRowIds) && payload.failedRowIds.length > 0) {
|
|
349
|
+
parts.push(`failed row(s): ${payload.failedRowIds.join(", ")}`);
|
|
350
|
+
}
|
|
347
351
|
if (payload.mergeCommit) parts.push(`merge ${String(payload.mergeCommit).slice(0, 8)}`);
|
|
348
352
|
if (payload.baseBranch && payload.orchBranch) {
|
|
349
353
|
parts.push(`${payload.baseBranch} → ${payload.orchBranch}`);
|
|
@@ -413,6 +417,7 @@ export function extractJournalDiagnosisHints(events) {
|
|
|
413
417
|
"batch.failed",
|
|
414
418
|
"batch.merge_blocked",
|
|
415
419
|
"task.failed",
|
|
420
|
+
"matrix.task_failed",
|
|
416
421
|
"review.crash_recovered",
|
|
417
422
|
"review.skipped_fresh_artifact",
|
|
418
423
|
"review.resumed",
|
|
@@ -14,7 +14,8 @@ import {
|
|
|
14
14
|
sanitizeGitignoredArtifactsBeforeLaneCommit,
|
|
15
15
|
} from "./lane-dirty-check.mjs";
|
|
16
16
|
import { gitExec } from "./git-exec.mjs";
|
|
17
|
-
import { parseContract } from "../tasks/packet/parse-prompt.mjs";
|
|
17
|
+
import { parseContract, parsePrompt } from "../tasks/packet/parse-prompt.mjs";
|
|
18
|
+
import { substituteMatrixVariables } from "../planner/matrix.mjs";
|
|
18
19
|
import {
|
|
19
20
|
hasReleaseCriticalContract,
|
|
20
21
|
shouldEnforceStubContractAtLaneCommit,
|
|
@@ -22,6 +23,37 @@ import {
|
|
|
22
23
|
} from "./contract-verify.mjs";
|
|
23
24
|
import { DEFAULT_WORKTREE_SETUP_IGNORE_PATHS } from "../config/spine-config-load.mjs";
|
|
24
25
|
|
|
26
|
+
/**
|
|
27
|
+
* Expand `{matrix.<column>}` fileScopeMustChange patterns across every matrix row
|
|
28
|
+
* so the stub file-scope check matches concrete per-row output paths. Non-matrix
|
|
29
|
+
* patterns (and non-matrix tasks) pass through unchanged.
|
|
30
|
+
*
|
|
31
|
+
* @param {string[]} patterns
|
|
32
|
+
* @param {Array<Record<string, string>>} matrixRows
|
|
33
|
+
* @returns {string[]}
|
|
34
|
+
*/
|
|
35
|
+
export function expandMatrixFileScopePatterns(patterns, matrixRows) {
|
|
36
|
+
if (!Array.isArray(patterns) || patterns.length === 0) return patterns;
|
|
37
|
+
if (!Array.isArray(matrixRows) || matrixRows.length === 0) return patterns;
|
|
38
|
+
/** @type {Set<string>} */
|
|
39
|
+
const expanded = new Set();
|
|
40
|
+
for (const pattern of patterns) {
|
|
41
|
+
let anySubstituted = false;
|
|
42
|
+
if (typeof pattern === "string" && pattern.includes("{matrix.")) {
|
|
43
|
+
for (const row of matrixRows) {
|
|
44
|
+
try {
|
|
45
|
+
expanded.add(substituteMatrixVariables(pattern, row));
|
|
46
|
+
anySubstituted = true;
|
|
47
|
+
} catch {
|
|
48
|
+
// Unknown column reference — fall through to keep the original pattern.
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
if (!anySubstituted) expanded.add(pattern);
|
|
53
|
+
}
|
|
54
|
+
return [...expanded];
|
|
55
|
+
}
|
|
56
|
+
|
|
25
57
|
/**
|
|
26
58
|
* @param {string} worktreePath
|
|
27
59
|
* @param {string[]} args
|
|
@@ -244,13 +276,31 @@ export function commitLaneWorktree({
|
|
|
244
276
|
|
|
245
277
|
if (shouldEnforceStubContractAtLaneCommit(donePath)) {
|
|
246
278
|
const promptPath = path.join(taskFolder, "PROMPT.md");
|
|
247
|
-
const
|
|
248
|
-
?
|
|
279
|
+
const promptText = fs.existsSync(promptPath)
|
|
280
|
+
? fs.readFileSync(promptPath, "utf-8")
|
|
281
|
+
: "";
|
|
282
|
+
const parsedContract = promptText
|
|
283
|
+
? parseContract(promptText)
|
|
249
284
|
: { fileScopeMustChange: [] };
|
|
250
285
|
if (hasReleaseCriticalContract(parsedContract)) {
|
|
286
|
+
// Matrix tasks carry `{matrix.<column>}` placeholders in fileScopeMustChange.
|
|
287
|
+
// Expand them across every matrix row so the stub check matches the
|
|
288
|
+
// concrete per-row output paths the rows actually produced.
|
|
289
|
+
const prompt = promptText ? parsePrompt(promptText) : null;
|
|
290
|
+
const matrixRows = Array.isArray(prompt?.matrix) ? prompt.matrix : [];
|
|
291
|
+
const effectiveContract =
|
|
292
|
+
matrixRows.length > 0
|
|
293
|
+
? {
|
|
294
|
+
...parsedContract,
|
|
295
|
+
fileScopeMustChange: expandMatrixFileScopePatterns(
|
|
296
|
+
parsedContract.fileScopeMustChange,
|
|
297
|
+
matrixRows,
|
|
298
|
+
),
|
|
299
|
+
}
|
|
300
|
+
: parsedContract;
|
|
251
301
|
const scopeCheck = verifyStubFileScopeMustChange(
|
|
252
302
|
worktreePath,
|
|
253
|
-
|
|
303
|
+
effectiveContract,
|
|
254
304
|
baseBranch,
|
|
255
305
|
stageCandidatePaths,
|
|
256
306
|
);
|
|
@@ -8,6 +8,8 @@ import { loadSpineConfig } from "../config/spine-config-load.mjs";
|
|
|
8
8
|
import { resolveWorktreeSetupHook } from "../config/worktree-setup-hook.mjs";
|
|
9
9
|
import { filterPorcelain } from "./lane-commit.mjs";
|
|
10
10
|
import { runWorktreeSetupHook } from "./worktree.mjs";
|
|
11
|
+
import { execFileSync } from "node:child_process";
|
|
12
|
+
import { matchesContractPattern } from "./contract-parse.mjs";
|
|
11
13
|
|
|
12
14
|
/**
|
|
13
15
|
* Path segments that identify generated coverage report artifacts.
|
|
@@ -296,3 +298,64 @@ export function filterOutOfScopeCoveragePorcelain(porcelain, fileScopePaths, ign
|
|
|
296
298
|
}
|
|
297
299
|
return kept.length === 0 ? "" : kept.join("\n");
|
|
298
300
|
}
|
|
301
|
+
|
|
302
|
+
/**
|
|
303
|
+
* Stages untracked files matching fileScopeMustChange patterns so workers are not penalized
|
|
304
|
+
* for forgetting to `git add` new files before contract verification (SP-668 / #219).
|
|
305
|
+
*
|
|
306
|
+
* @param {string} worktreePath
|
|
307
|
+
* @param {string[]} fileScopeMustChange
|
|
308
|
+
* @returns {{ stagedCount: number, error?: string }}
|
|
309
|
+
*/
|
|
310
|
+
export function stageUntrackedScopeFiles(worktreePath, fileScopeMustChange = []) {
|
|
311
|
+
if (!Array.isArray(fileScopeMustChange) || fileScopeMustChange.length === 0) {
|
|
312
|
+
return { stagedCount: 0 };
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
let porcelain;
|
|
316
|
+
try {
|
|
317
|
+
porcelain = execFileSync("git", ["status", "--porcelain", "-uall"], {
|
|
318
|
+
cwd: worktreePath,
|
|
319
|
+
encoding: "utf-8",
|
|
320
|
+
stdio: ["ignore", "pipe", "pipe"]
|
|
321
|
+
});
|
|
322
|
+
} catch (error) {
|
|
323
|
+
return { stagedCount: 0, error: `git status failed: ${error.message}` };
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
const toStage = [];
|
|
327
|
+
for (const line of porcelain.split("\n")) {
|
|
328
|
+
if (!line.trim()) continue;
|
|
329
|
+
const status = line.slice(0, 2);
|
|
330
|
+
if (status !== "??") continue;
|
|
331
|
+
|
|
332
|
+
let filePath = extractPorcelainPath(line);
|
|
333
|
+
if (!filePath) continue;
|
|
334
|
+
if (filePath.startsWith('"') && filePath.endsWith('"')) {
|
|
335
|
+
filePath = filePath.slice(1, -1);
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
const matched = fileScopeMustChange.some((pattern) =>
|
|
339
|
+
pattern.endsWith("/") ? filePath.startsWith(pattern) : matchesContractPattern(filePath, pattern)
|
|
340
|
+
);
|
|
341
|
+
|
|
342
|
+
if (matched) {
|
|
343
|
+
toStage.push(filePath);
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
if (toStage.length === 0) {
|
|
348
|
+
return { stagedCount: 0 };
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
try {
|
|
352
|
+
execFileSync("git", ["add", "--", ...toStage], {
|
|
353
|
+
cwd: worktreePath,
|
|
354
|
+
encoding: "utf-8",
|
|
355
|
+
stdio: ["ignore", "pipe", "pipe"]
|
|
356
|
+
});
|
|
357
|
+
return { stagedCount: toStage.length };
|
|
358
|
+
} catch (error) {
|
|
359
|
+
return { stagedCount: 0, error: `git add failed: ${error.message}` };
|
|
360
|
+
}
|
|
361
|
+
}
|