pi-spine 2.18.0 → 2.20.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-batch.mjs +15 -5
- package/bin/spine-gate.mjs +18 -3
- package/bin/spine-status.mjs +63 -2
- package/package.json +1 -1
- package/skills/create-spine-tasks/references/prompt-template.md +16 -1
- package/src/batch/contract-exec.mjs +19 -18
- package/src/batch/diagnosis-handoff-packet.mjs +148 -0
- package/src/batch/diagnosis-suggested-command.mjs +177 -0
- package/src/batch/diagnosis.mjs +12 -168
- package/src/batch/engine-lanes/commit.mjs +6 -0
- package/src/batch/engine-lanes/matrix-run.mjs +238 -22
- package/src/batch/engine-lanes/matrix.mjs +33 -2
- package/src/batch/gate-posture-approve.mjs +16 -2
- package/src/batch/lane-commit.mjs +27 -12
- package/src/batch/readers/spine-state.mjs +4 -0
- package/src/batch/resume-multi-lanes.mjs +31 -0
- package/src/batch/resume.mjs +79 -59
- package/src/batch/retry-row.mjs +328 -0
- package/src/batch/retry.mjs +114 -44
- package/src/batch/state-guards.mjs +4 -5
- package/src/batch/state-io.mjs +2 -2
- package/src/batch/worker-host.mjs +3 -0
- package/src/batch/worker-spawn.mjs +14 -0
- package/src/cli/handoff.mjs +124 -10
- package/src/cli/issue-draft.mjs +59 -7
- package/src/metrics/quota-probes.mjs +44 -0
- package/src/tasks/packet/parse-prompt.mjs +17 -0
- package/src/tasks/packet/validate-contract.mjs +1 -0
- package/templates/agents/supervisor.md +1 -1
- package/templates/tasks/CONTEXT.md +4 -0
package/src/batch/diagnosis.mjs
CHANGED
|
@@ -4,18 +4,15 @@
|
|
|
4
4
|
*/
|
|
5
5
|
import {
|
|
6
6
|
buildWorkerDoneMissingHeadline,
|
|
7
|
-
buildWorkerDoneMissingSuggestedCommand,
|
|
8
7
|
} from "./diagnosis-worker-done-missing.mjs";
|
|
9
8
|
import {
|
|
10
9
|
buildFailedPhasePendingOnlyHeadline,
|
|
11
|
-
buildFailedPhasePendingOnlySuggestedCommand,
|
|
12
10
|
isFailedPhasePendingOnlyLimbo,
|
|
13
11
|
} from "./diagnosis-retry-limbo.mjs";
|
|
14
12
|
import { buildAlternatives } from "./diagnosis-alternatives.mjs";
|
|
15
13
|
export { buildAlternatives } from "./diagnosis-alternatives.mjs";
|
|
16
14
|
import {
|
|
17
15
|
buildMergeFailureHeadline,
|
|
18
|
-
buildGitignoredMergeRepairCommand,
|
|
19
16
|
shouldPreferPrimaryOverGitignoredHeadline,
|
|
20
17
|
} from "./diagnosis-merge-failure.mjs";
|
|
21
18
|
export {
|
|
@@ -36,7 +33,6 @@ export {
|
|
|
36
33
|
export { buildReviewHonorHeadlineSuffix, findLatestReviewHonorSignal } from "./review.mjs";
|
|
37
34
|
import {
|
|
38
35
|
buildStubFailureHeadline,
|
|
39
|
-
buildStubFailureSuggestedCommand,
|
|
40
36
|
STUB_EXIT_REASONS,
|
|
41
37
|
} from "./diagnosis-stub.mjs";
|
|
42
38
|
/** @typedef {import("./reconcile.mjs").ReconciliationResult} ReconciliationResult */
|
|
@@ -48,11 +44,9 @@ export {
|
|
|
48
44
|
} from "./diagnosis-task-done.mjs";
|
|
49
45
|
import {
|
|
50
46
|
buildPrimaryFailureHeadline,
|
|
51
|
-
buildPrimaryFailureSuggestedCommand,
|
|
52
47
|
} from "./diagnosis-primary-failure.mjs";
|
|
53
48
|
import {
|
|
54
49
|
buildEngineOrphanParentExitHeadline,
|
|
55
|
-
buildEngineOrphanParentExitSuggestedCommand,
|
|
56
50
|
} from "./diagnosis-parent-exit.mjs";
|
|
57
51
|
export {
|
|
58
52
|
inferEngineOrphanCause,
|
|
@@ -63,11 +57,9 @@ import { buildReviewHonorHeadlineSuffix } from "./review.mjs";
|
|
|
63
57
|
export { inferLaunchFailureFromWorkerOutputTail, inferLaunchFailureKind } from "./diagnosis-launch-failure.mjs";
|
|
64
58
|
import { isGateReadyHeadlineContext } from "./diagnosis-gate-ready.mjs";
|
|
65
59
|
export { isGateReadyHeadlineContext };
|
|
66
|
-
|
|
67
|
-
export { sanitizeRetrySuggestedCommand };
|
|
60
|
+
export { sanitizeRetrySuggestedCommand } from "./diagnosis-retry-command.mjs";
|
|
68
61
|
import {
|
|
69
62
|
buildPendingLaneLandHeadline,
|
|
70
|
-
buildPendingLaneLandSuggestedCommand,
|
|
71
63
|
} from "./diagnosis-pending-lane.mjs";
|
|
72
64
|
export {
|
|
73
65
|
buildPendingLaneLandHeadline,
|
|
@@ -75,6 +67,10 @@ export {
|
|
|
75
67
|
findPendingLaneLandTasks,
|
|
76
68
|
shouldDiagnosePendingLaneLand,
|
|
77
69
|
} from "./diagnosis-pending-lane.mjs";
|
|
70
|
+
export { buildSuggestedCommand } from "./diagnosis-suggested-command.mjs";
|
|
71
|
+
import { buildSuggestedCommand } from "./diagnosis-suggested-command.mjs";
|
|
72
|
+
export { buildBackground, buildAssessmentReason } from "./diagnosis-handoff-packet.mjs";
|
|
73
|
+
import { buildBackground, buildAssessmentReason } from "./diagnosis-handoff-packet.mjs";
|
|
78
74
|
|
|
79
75
|
export const DIAGNOSIS_TAXONOMY = [
|
|
80
76
|
"running",
|
|
@@ -113,162 +109,6 @@ const REVIEW_SPAWN_FAILURE_EXIT_REASONS = new Set([
|
|
|
113
109
|
"final_review_timeout",
|
|
114
110
|
]);
|
|
115
111
|
|
|
116
|
-
/**
|
|
117
|
-
* @param {string} diagnosis
|
|
118
|
-
* @param {object} [ctx]
|
|
119
|
-
* @param {string|null} [ctx.failedTaskId]
|
|
120
|
-
* @param {string|null} [ctx.batchId]
|
|
121
|
-
* @param {string|null} [ctx.salvageRetryCommand]
|
|
122
|
-
* @param {string|null} [ctx.exitReason]
|
|
123
|
-
* @param {string|null} [ctx.launchFailureKind]
|
|
124
|
-
* @param {boolean} [ctx.mergeGitignoredFailure]
|
|
125
|
-
* @param {boolean} [ctx.mergeFailed]
|
|
126
|
-
* @param {string|null} [ctx.taskBranch]
|
|
127
|
-
* @param {string[]|null} [ctx.gitignoredPaths]
|
|
128
|
-
* @param {object[]} [ctx.pendingLaneLandTasks]
|
|
129
|
-
*/
|
|
130
|
-
export function buildSuggestedCommand(diagnosis, ctx = {}) {
|
|
131
|
-
const preferPrimaryOverGitignored = shouldPreferPrimaryOverGitignoredHeadline(diagnosis, ctx);
|
|
132
|
-
// Prefer gate-approve / orphan primary over recovered merge/gitignored repair (#195 / #205).
|
|
133
|
-
if (ctx.mergeGitignoredFailure && !preferPrimaryOverGitignored) {
|
|
134
|
-
return buildGitignoredMergeRepairCommand(ctx.taskBranch, ctx.gitignoredPaths);
|
|
135
|
-
}
|
|
136
|
-
if (
|
|
137
|
-
ctx.mergeFailed &&
|
|
138
|
-
!preferPrimaryOverGitignored &&
|
|
139
|
-
(diagnosis === "failed" || diagnosis === "needs_retry")
|
|
140
|
-
) {
|
|
141
|
-
return "spine batch resume --force";
|
|
142
|
-
}
|
|
143
|
-
if (ctx.phase === "merge_blocked") {
|
|
144
|
-
return "spine batch resume --force";
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
switch (diagnosis) {
|
|
148
|
-
case "engine_still_running":
|
|
149
|
-
return "spine wait --until completed,failed,needs_integrate --timeout 2h";
|
|
150
|
-
case "limbo_stale":
|
|
151
|
-
case "completed_manual":
|
|
152
|
-
return "spine batch dismiss";
|
|
153
|
-
case "state_drift":
|
|
154
|
-
// Agent-safe detached recovery (#196 / SP-613) — never suggest --attached.
|
|
155
|
-
if (
|
|
156
|
-
ctx.phase === "running" ||
|
|
157
|
-
ctx.staleEnginePid === true ||
|
|
158
|
-
ctx.engineDead === true ||
|
|
159
|
-
ctx.allTasksTerminalSuccess === true
|
|
160
|
-
) {
|
|
161
|
-
return "spine batch resume --force";
|
|
162
|
-
}
|
|
163
|
-
if (ctx.failedTaskId) {
|
|
164
|
-
const driftStatus = String(ctx.driftTaskStatus ?? "").toLowerCase();
|
|
165
|
-
if (driftStatus === "running") {
|
|
166
|
-
return "spine batch resume --force";
|
|
167
|
-
}
|
|
168
|
-
return `spine batch retry ${ctx.failedTaskId}`;
|
|
169
|
-
}
|
|
170
|
-
return "spine status --diagnose";
|
|
171
|
-
case "needs_retry":
|
|
172
|
-
if (STUB_EXIT_REASONS.has(ctx.exitReason ?? "")) {
|
|
173
|
-
return buildStubFailureSuggestedCommand(ctx);
|
|
174
|
-
}
|
|
175
|
-
const primaryCommand = buildPrimaryFailureSuggestedCommand(ctx);
|
|
176
|
-
if (primaryCommand) return primaryCommand;
|
|
177
|
-
if (ctx.launchFailureKind === "pi_spine_root" || ctx.launchFailureKind === "launch_failed") {
|
|
178
|
-
return "spine doctor";
|
|
179
|
-
}
|
|
180
|
-
if (ctx.launchFailureKind === "worktree_unhealthy") {
|
|
181
|
-
return "spine doctor";
|
|
182
|
-
}
|
|
183
|
-
if (ctx.salvageRetryCommand) {
|
|
184
|
-
return sanitizeRetrySuggestedCommand(ctx.salvageRetryCommand, ctx.failedTaskId);
|
|
185
|
-
}
|
|
186
|
-
if (isFailedPhasePendingOnlyLimbo(ctx)) {
|
|
187
|
-
return buildFailedPhasePendingOnlySuggestedCommand(ctx);
|
|
188
|
-
}
|
|
189
|
-
return ctx.failedTaskId
|
|
190
|
-
? `spine batch retry ${ctx.failedTaskId}`
|
|
191
|
-
: "spine status --diagnose";
|
|
192
|
-
case "worker_orphaned":
|
|
193
|
-
if (ctx.planReviewNestedSpawnBlocked) {
|
|
194
|
-
if (ctx.stalePathSpine) {
|
|
195
|
-
return ctx.failedTaskId
|
|
196
|
-
? `node bin/spine.mjs batch retry ${ctx.failedTaskId}`
|
|
197
|
-
: "npm link && spine batch resume --attached";
|
|
198
|
-
}
|
|
199
|
-
if (ctx.failedTaskId) {
|
|
200
|
-
return `spine batch retry ${ctx.failedTaskId}`;
|
|
201
|
-
}
|
|
202
|
-
}
|
|
203
|
-
if (ctx.engineDead) {
|
|
204
|
-
return "spine batch resume --attached --force";
|
|
205
|
-
}
|
|
206
|
-
if (ctx.ghostRunningCluster) return "spine batch abort";
|
|
207
|
-
if (ctx.launchFailureKind === "pi_spine_root" || ctx.launchFailureKind === "launch_failed") {
|
|
208
|
-
return "spine doctor";
|
|
209
|
-
}
|
|
210
|
-
if (ctx.launchFailureKind === "worktree_unhealthy") {
|
|
211
|
-
return "spine doctor";
|
|
212
|
-
}
|
|
213
|
-
if (ctx.salvageRetryCommand) {
|
|
214
|
-
return sanitizeRetrySuggestedCommand(ctx.salvageRetryCommand, ctx.failedTaskId);
|
|
215
|
-
}
|
|
216
|
-
return ctx.failedTaskId
|
|
217
|
-
? `spine batch retry ${ctx.failedTaskId}`
|
|
218
|
-
: "spine batch abort";
|
|
219
|
-
case "worker_done_missing":
|
|
220
|
-
return buildWorkerDoneMissingSuggestedCommand(ctx);
|
|
221
|
-
case "engine_orphaned": {
|
|
222
|
-
const parentExitCommand = buildEngineOrphanParentExitSuggestedCommand(ctx);
|
|
223
|
-
if (parentExitCommand) return parentExitCommand;
|
|
224
|
-
// Terminal-success / doneInLane heal with dead engine — detached resume (#196 / #163).
|
|
225
|
-
if (ctx.allTasksTerminalSuccess === true) {
|
|
226
|
-
return "spine batch resume --force";
|
|
227
|
-
}
|
|
228
|
-
return ctx.failedTaskId
|
|
229
|
-
? `spine batch retry ${ctx.failedTaskId}`
|
|
230
|
-
: "spine batch resume --attached";
|
|
231
|
-
}
|
|
232
|
-
case "needs_merge":
|
|
233
|
-
return "spine batch resume --force";
|
|
234
|
-
case "needs_integrate":
|
|
235
|
-
if (ctx.integrateGateOpen) {
|
|
236
|
-
return "spine gate approve";
|
|
237
|
-
}
|
|
238
|
-
if (ctx.postMergeLimbo && ctx.phase === "running") {
|
|
239
|
-
if (ctx.stalePathSpine) {
|
|
240
|
-
return "node bin/spine.mjs batch resume --attached";
|
|
241
|
-
}
|
|
242
|
-
return "spine batch resume --force";
|
|
243
|
-
}
|
|
244
|
-
return "spine integrate";
|
|
245
|
-
case "needs_replan": {
|
|
246
|
-
const tasksRoot = ctx.tasksRoot ?? "spine-tasks";
|
|
247
|
-
return ctx.failedTaskId
|
|
248
|
-
? `edit ${tasksRoot}/${ctx.failedTaskId}/PROMPT.md then spine batch retry ${ctx.failedTaskId}`
|
|
249
|
-
: "spine status --diagnose";
|
|
250
|
-
}
|
|
251
|
-
case "running":
|
|
252
|
-
return "/spine-status --diagnose";
|
|
253
|
-
case "paused":
|
|
254
|
-
return "spine batch resume";
|
|
255
|
-
case "failed":
|
|
256
|
-
return "spine status --diagnose";
|
|
257
|
-
case "aborted":
|
|
258
|
-
return "spine batch dismiss";
|
|
259
|
-
case "completed":
|
|
260
|
-
return "spine preflight";
|
|
261
|
-
case "human_base_diverged":
|
|
262
|
-
return "spine sync-base";
|
|
263
|
-
case "integrate_isolated_ok":
|
|
264
|
-
return "spine sync-base";
|
|
265
|
-
case "pending_lane_land":
|
|
266
|
-
return buildPendingLaneLandSuggestedCommand(ctx.batchId, ctx.pendingLaneLandTasks ?? []);
|
|
267
|
-
default:
|
|
268
|
-
return "spine status --diagnose";
|
|
269
|
-
}
|
|
270
|
-
}
|
|
271
|
-
|
|
272
112
|
/**
|
|
273
113
|
* @param {string} diagnosis
|
|
274
114
|
* @param {object} ctx
|
|
@@ -340,13 +180,13 @@ export function buildHeadline(diagnosis, ctx = {}) {
|
|
|
340
180
|
const isFinal = ctx.exitReason.startsWith("final_review");
|
|
341
181
|
const isTimeout = ctx.exitReason.endsWith("_timeout");
|
|
342
182
|
const reviewKind = isFinal ? "final review" : "code review";
|
|
343
|
-
|
|
183
|
+
|
|
344
184
|
if (isTimeout) {
|
|
345
185
|
return ctx.failedTaskId
|
|
346
186
|
? `${batchLabel} ${reviewKind} timed out for task ${ctx.failedTaskId} — retry or increase SPINE_REVIEW_TIMEOUT_MS`
|
|
347
187
|
: `${batchLabel} reviewer spawn timed out — retry`;
|
|
348
188
|
}
|
|
349
|
-
|
|
189
|
+
|
|
350
190
|
return ctx.failedTaskId
|
|
351
191
|
? `${batchLabel} ${reviewKind} spawn failed for task ${ctx.failedTaskId} — retry`
|
|
352
192
|
: `${batchLabel} reviewer spawn failed — retry`;
|
|
@@ -482,7 +322,7 @@ export function resolveEffectiveDiagnosis(diagnosis, ctx = {}) {
|
|
|
482
322
|
/**
|
|
483
323
|
* @param {string} diagnosis
|
|
484
324
|
* @param {object} ctx
|
|
485
|
-
* @returns {Pick<ReconciliationResult, "diagnosis" | "headline" | "suggestedCommand" | "alternatives">}
|
|
325
|
+
* @returns {Pick<ReconciliationResult, "diagnosis" | "headline" | "suggestedCommand" | "alternatives"> & { background: string[], assessmentReason: string }}
|
|
486
326
|
*/
|
|
487
327
|
export function buildDiagnosisOutput(diagnosis, ctx = {}) {
|
|
488
328
|
const effectiveDiagnosis = resolveEffectiveDiagnosis(diagnosis, ctx);
|
|
@@ -491,5 +331,9 @@ export function buildDiagnosisOutput(diagnosis, ctx = {}) {
|
|
|
491
331
|
headline: buildHeadline(effectiveDiagnosis, ctx),
|
|
492
332
|
suggestedCommand: buildSuggestedCommand(effectiveDiagnosis, ctx),
|
|
493
333
|
alternatives: buildAlternatives(effectiveDiagnosis, ctx),
|
|
334
|
+
// Additive SBAR fields (#278 / SP-745) — appended after legacy keys so
|
|
335
|
+
// consumers reading positional or partial fields are unaffected.
|
|
336
|
+
background: buildBackground(effectiveDiagnosis, ctx),
|
|
337
|
+
assessmentReason: buildAssessmentReason(effectiveDiagnosis, ctx),
|
|
494
338
|
};
|
|
495
339
|
}
|
|
@@ -65,6 +65,11 @@ export function commitLaneAndValidateWorktree({
|
|
|
65
65
|
porcelain: preCommitPorcelain,
|
|
66
66
|
});
|
|
67
67
|
|
|
68
|
+
const canceledMatrixRowIds = (Array.isArray(task?.matrixRows) ? task.matrixRows : [])
|
|
69
|
+
.filter((row) => row && row.status === "canceled")
|
|
70
|
+
.map((row) => row.rowId)
|
|
71
|
+
.filter(Boolean);
|
|
72
|
+
|
|
68
73
|
const laneCommit = commitLaneWorktree({
|
|
69
74
|
worktreePath,
|
|
70
75
|
taskBranch,
|
|
@@ -74,6 +79,7 @@ export function commitLaneAndValidateWorktree({
|
|
|
74
79
|
projectRoot,
|
|
75
80
|
ignorePatterns,
|
|
76
81
|
fileScopePaths,
|
|
82
|
+
canceledMatrixRowIds,
|
|
77
83
|
});
|
|
78
84
|
if (!laneCommit.ok) {
|
|
79
85
|
task.status = "failed";
|
|
@@ -11,6 +11,8 @@ import { createHash } from "node:crypto";
|
|
|
11
11
|
import {
|
|
12
12
|
acquireLaneSlot,
|
|
13
13
|
aggregateMatrixOutcomes,
|
|
14
|
+
buildMatrixRowEnv,
|
|
15
|
+
loadMatrixTaskRows,
|
|
14
16
|
provisionMatrixSubLaneWorktree,
|
|
15
17
|
recordMatrixEvent,
|
|
16
18
|
releaseLaneSlot,
|
|
@@ -98,13 +100,38 @@ function restoreMatrixRowPrompt({ worktreePath, taskFolderRel, projectRoot }) {
|
|
|
98
100
|
fs.rmSync(path.join(worktreePath, taskFolderRel, ".DONE"), { force: true });
|
|
99
101
|
}
|
|
100
102
|
|
|
103
|
+
/**
|
|
104
|
+
* Effective per-matrix row concurrency (#229): the Contract's optional
|
|
105
|
+
* `matrixMaxParallel` throttle (Slurm `%N` analog) capped by the global
|
|
106
|
+
* `lanes.maxParallel` — a matrix can narrow its share of the pool but never
|
|
107
|
+
* widen it. Missing/invalid throttles fall back to the global cap.
|
|
108
|
+
*
|
|
109
|
+
* @param {object} params
|
|
110
|
+
* @param {number | null} [params.matrixMaxParallel] Contract `matrixMaxParallel` (positive int).
|
|
111
|
+
* @param {number} [params.maxParallel] Global `lanes.maxParallel`.
|
|
112
|
+
* @returns {number}
|
|
113
|
+
*/
|
|
114
|
+
export function resolveMatrixRowConcurrency({ matrixMaxParallel, maxParallel = 1 }) {
|
|
115
|
+
const globalCap = Math.max(1, Math.floor(Number(maxParallel) || 1));
|
|
116
|
+
if (matrixMaxParallel == null) return globalCap;
|
|
117
|
+
const throttle = Number(matrixMaxParallel);
|
|
118
|
+
if (!Number.isFinite(throttle) || throttle < 1) return globalCap;
|
|
119
|
+
return Math.max(1, Math.min(Math.floor(throttle), globalCap));
|
|
120
|
+
}
|
|
121
|
+
|
|
101
122
|
/**
|
|
102
123
|
* Execute a single matrix row in its own worktree. Substitutes `{matrix.X}`
|
|
103
124
|
* placeholders for execution-only rows; LLM rows delegate to `runWorker` with a
|
|
104
125
|
* row-substituted PROMPT.md served into the row worktree (#232). Commits
|
|
105
126
|
* successful row output to the row branch.
|
|
106
127
|
*
|
|
128
|
+
* Rows also receive matrix index environment variables (#229) when `rowIndex`
|
|
129
|
+
* and `rowCount` are supplied: execute rows via the shell env, LLM rows via the
|
|
130
|
+
* worker child env (`buildMatrixRowEnv`).
|
|
131
|
+
*
|
|
107
132
|
* @param {object} params
|
|
133
|
+
* @param {number} [params.rowIndex] 0-based index of this row in the matrix.
|
|
134
|
+
* @param {number} [params.rowCount] Total matrix row count.
|
|
108
135
|
* @returns {Promise<{ rowId: string, ok: boolean, exitCode: number, output: string, worktreePath?: string, branch?: string, commitSha?: string, servedPrompt?: string }>}
|
|
109
136
|
*/
|
|
110
137
|
export async function runMatrixSubLane({
|
|
@@ -120,9 +147,12 @@ export async function runMatrixSubLane({
|
|
|
120
147
|
taskFolderRel,
|
|
121
148
|
config,
|
|
122
149
|
baseBranch,
|
|
150
|
+
rowIndex,
|
|
151
|
+
rowCount,
|
|
123
152
|
}) {
|
|
124
153
|
const rowId = row.rowId;
|
|
125
154
|
const values = row.values;
|
|
155
|
+
const matrixEnv = buildMatrixRowEnv({ taskId, rowId, rowIndex, rowCount });
|
|
126
156
|
let worktreePath;
|
|
127
157
|
let branch;
|
|
128
158
|
|
|
@@ -210,7 +240,7 @@ export async function runMatrixSubLane({
|
|
|
210
240
|
};
|
|
211
241
|
} else {
|
|
212
242
|
const command = substituteRowCommand(rawCommand, values);
|
|
213
|
-
const run = await runShellInDir(worktreePath, command);
|
|
243
|
+
const run = await runShellInDir(worktreePath, command, matrixEnv);
|
|
214
244
|
if (run.exitCode !== 0) {
|
|
215
245
|
result = {
|
|
216
246
|
rowId,
|
|
@@ -314,6 +344,7 @@ export async function runMatrixSubLane({
|
|
|
314
344
|
laneCorrelationId: `${laneCorrelationId}-matrix-${rowId}`,
|
|
315
345
|
fileScopePaths: [],
|
|
316
346
|
config,
|
|
347
|
+
...(matrixEnv ? { extraEnv: matrixEnv } : {}),
|
|
317
348
|
});
|
|
318
349
|
result = {
|
|
319
350
|
rowId,
|
|
@@ -402,6 +433,88 @@ export async function runMatrixSubLane({
|
|
|
402
433
|
return result;
|
|
403
434
|
}
|
|
404
435
|
|
|
436
|
+
/**
|
|
437
|
+
* Resume-path entry for matrix tasks (SP-752 / #230).
|
|
438
|
+
*
|
|
439
|
+
* The single-task (`resume.mjs`) and multi-task (`resume-multi-lanes.mjs`)
|
|
440
|
+
* resume loops run workers directly and have no matrix fan-out — without this
|
|
441
|
+
* dispatch a retried matrix task falls through to a plain worker run on the
|
|
442
|
+
* unsubstituted parent PROMPT. Detect a matrix packet and delegate to
|
|
443
|
+
* `runMatrixTaskOnLane`, which owns per-row execution, carry-over, lane commit,
|
|
444
|
+
* and success/failure recording for the resumed task.
|
|
445
|
+
*
|
|
446
|
+
* On failure the matrix runner has already failed the task, persisted state,
|
|
447
|
+
* and journaled `task.failed`; this finishes the failed-batch bookkeeping
|
|
448
|
+
* (batch phase/counters — the plain worker failure branch does the same) and
|
|
449
|
+
* returns a ready-to-return `cliResult` envelope for the resume caller.
|
|
450
|
+
*
|
|
451
|
+
* @param {object} params
|
|
452
|
+
* @param {string} params.projectRoot
|
|
453
|
+
* @param {object} params.state Live batch state.
|
|
454
|
+
* @param {string} params.batchId
|
|
455
|
+
* @param {string} params.baseBranch
|
|
456
|
+
* @param {object} params.config
|
|
457
|
+
* @param {object} params.task
|
|
458
|
+
* @param {object} params.lane
|
|
459
|
+
* @param {string} params.taskFolderRel
|
|
460
|
+
* @param {string} params.laneCorrelationId
|
|
461
|
+
* @param {string[]} params.fileScopePaths
|
|
462
|
+
* @returns {Promise<{ isMatrix: false } | { isMatrix: true, ok: boolean, cliResult?: object }>}
|
|
463
|
+
*/
|
|
464
|
+
export async function runMatrixTaskForResume({
|
|
465
|
+
projectRoot,
|
|
466
|
+
state,
|
|
467
|
+
batchId,
|
|
468
|
+
baseBranch,
|
|
469
|
+
config,
|
|
470
|
+
task,
|
|
471
|
+
lane,
|
|
472
|
+
taskFolderRel,
|
|
473
|
+
laneCorrelationId,
|
|
474
|
+
fileScopePaths,
|
|
475
|
+
}) {
|
|
476
|
+
const matrix = loadMatrixTaskRows(path.join(projectRoot, taskFolderRel));
|
|
477
|
+
if (!matrix) return { isMatrix: false };
|
|
478
|
+
const result = await runMatrixTaskOnLane({
|
|
479
|
+
projectRoot,
|
|
480
|
+
state,
|
|
481
|
+
batchId,
|
|
482
|
+
baseBranch,
|
|
483
|
+
config,
|
|
484
|
+
task,
|
|
485
|
+
lane,
|
|
486
|
+
taskFolderRel,
|
|
487
|
+
laneCorrelationId,
|
|
488
|
+
fileScopePaths,
|
|
489
|
+
matrix,
|
|
490
|
+
maxParallel: config?.lanes?.maxParallel ?? 1,
|
|
491
|
+
});
|
|
492
|
+
if (!result.ok) {
|
|
493
|
+
const output = result.workerResult?.output ?? result.output ?? "matrix task failed";
|
|
494
|
+
state.failedTasks = 1;
|
|
495
|
+
state.succeededTasks = 0;
|
|
496
|
+
state.phase = "failed";
|
|
497
|
+
state.endedAt = Date.now();
|
|
498
|
+
state.lastError = String(output).slice(0, 500);
|
|
499
|
+
saveEngineBatchState(projectRoot, state);
|
|
500
|
+
return {
|
|
501
|
+
isMatrix: true,
|
|
502
|
+
ok: false,
|
|
503
|
+
cliResult: {
|
|
504
|
+
ok: false,
|
|
505
|
+
exitCode: result.workerResult?.exitCode ?? 1,
|
|
506
|
+
batchId,
|
|
507
|
+
taskId: task.taskId,
|
|
508
|
+
error: result.workerResult?.classification ?? "worker_failed",
|
|
509
|
+
output,
|
|
510
|
+
},
|
|
511
|
+
};
|
|
512
|
+
}
|
|
513
|
+
// Success: the matrix runner already lane-committed, recorded task success,
|
|
514
|
+
// and journaled — the caller skips its plain worker postlude.
|
|
515
|
+
return { isMatrix: true, ok: true };
|
|
516
|
+
}
|
|
517
|
+
|
|
405
518
|
/**
|
|
406
519
|
* Run every matrix row of a task as a first-class lane-pool competitor
|
|
407
520
|
* (SP-697 / #228, supersedes the SP-690 nested throttle).
|
|
@@ -415,10 +528,13 @@ export async function runMatrixSubLane({
|
|
|
415
528
|
* `lane-{n}-…` worktrees.
|
|
416
529
|
*
|
|
417
530
|
* Each row runs in its own worktree off the lane task branch. The parent task
|
|
418
|
-
* succeeds only when every row succeeds; any
|
|
419
|
-
* surfaces the failing row id(s).
|
|
420
|
-
*
|
|
421
|
-
*
|
|
531
|
+
* succeeds only when every non-canceled row succeeds; any failed row fails the
|
|
532
|
+
* task and surfaces the failing row id(s). `canceled` rows (row-scoped skip,
|
|
533
|
+
* #230) are operator exclusions and leave the required set. On a failed sweep
|
|
534
|
+
* only the failed rows' worktrees are removed — succeeded rows keep theirs so a
|
|
535
|
+
* row-scoped retry carries them over without re-execution. Successful row
|
|
536
|
+
* branches (fresh and carried-over) are merged back into the lane worktree so
|
|
537
|
+
* the normal lane-commit + wave-merge carry all rows' output.
|
|
422
538
|
*
|
|
423
539
|
* @param {object} params
|
|
424
540
|
* @param {number} params.maxParallel Global `lanes.maxParallel` (pool size).
|
|
@@ -444,16 +560,72 @@ export async function runMatrixTaskOnLane({
|
|
|
444
560
|
const taskFolderInWorktree = path.join(wt, taskFolderRel);
|
|
445
561
|
const parentTaskFolderAbs = path.join(projectRoot, taskFolderRel);
|
|
446
562
|
|
|
563
|
+
// Per-matrix throttle (#229): an optional Contract `matrixMaxParallel`
|
|
564
|
+
// narrows this matrix's share of the global pool. The pool itself stays
|
|
565
|
+
// sized `maxParallel` — global `lanes.maxParallel` semantics never change.
|
|
566
|
+
const parentContract = readParentContract(parentTaskFolderAbs);
|
|
567
|
+
const matrixMaxParallel = parentContract?.matrixMaxParallel ?? null;
|
|
568
|
+
const rowConcurrency = resolveMatrixRowConcurrency({ matrixMaxParallel, maxParallel });
|
|
569
|
+
|
|
447
570
|
task.status = "running";
|
|
448
571
|
task.isMatrix = true;
|
|
449
572
|
task.matrixType = matrix.type;
|
|
450
573
|
if (!task.startedAt) task.startedAt = Date.now();
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
574
|
+
// Seed per-row entries preserving prior-attempt state (#230): `succeeded`
|
|
575
|
+
// rows keep their status + persisted worktree/branch for retry carry-over,
|
|
576
|
+
// `canceled` rows stay excluded, and only failed/pending rows re-enter the
|
|
577
|
+
// sweep. Fresh rows start `pending`.
|
|
578
|
+
task.matrixRows = matrix.rows.map((row) => {
|
|
579
|
+
const existing = Array.isArray(task.matrixRows)
|
|
580
|
+
? task.matrixRows.find((entry) => entry && entry.rowId === row.rowId)
|
|
581
|
+
: null;
|
|
582
|
+
return existing ?? {
|
|
583
|
+
rowId: row.rowId,
|
|
584
|
+
status: "pending",
|
|
585
|
+
exitCode: null,
|
|
586
|
+
commitSha: null,
|
|
587
|
+
};
|
|
588
|
+
});
|
|
589
|
+
|
|
590
|
+
// Partition rows for this sweep (#230): carry-over succeeded rows (kept
|
|
591
|
+
// worktree/branch from a prior attempt — never re-executed), rows to run now
|
|
592
|
+
// (status pending), and rows skipped this sweep (canceled, or still failed
|
|
593
|
+
// because only a subset was retried).
|
|
594
|
+
const carryOverRows = [];
|
|
595
|
+
const runnableRows = [];
|
|
596
|
+
const skippedRowIds = [];
|
|
597
|
+
for (const row of matrix.rows) {
|
|
598
|
+
const entry = task.matrixRows.find((m) => m.rowId === row.rowId);
|
|
599
|
+
if (entry.status === "succeeded") {
|
|
600
|
+
const carryWorktree = typeof entry.worktreePath === "string" ? entry.worktreePath : null;
|
|
601
|
+
const carryBranch = typeof entry.branch === "string" ? entry.branch : null;
|
|
602
|
+
if (carryWorktree && carryBranch && fs.existsSync(carryWorktree)) {
|
|
603
|
+
carryOverRows.push({ row, worktreePath: carryWorktree, branch: carryBranch, commitSha: entry.commitSha });
|
|
604
|
+
continue;
|
|
605
|
+
}
|
|
606
|
+
// Carry-over material lost (crash recovery / manual cleanup): fall back
|
|
607
|
+
// to re-execution — never worse than the pre-SP-752 whole-sweep rerun.
|
|
608
|
+
// A stale branch would break `worktree add -b`, so drop it first.
|
|
609
|
+
if (carryBranch) {
|
|
610
|
+
gitExec(projectRoot, ["branch", "-D", carryBranch], { throwOnError: false });
|
|
611
|
+
}
|
|
612
|
+
recordMatrixEvent(projectRoot, batchId, "matrix.row_reexecuted", {
|
|
613
|
+
taskId,
|
|
614
|
+
laneNumber,
|
|
615
|
+
rowId: row.rowId,
|
|
616
|
+
correlationId: laneCorrelationId,
|
|
617
|
+
reason: "carry_over_worktree_missing",
|
|
618
|
+
});
|
|
619
|
+
entry.status = "pending";
|
|
620
|
+
runnableRows.push(row);
|
|
621
|
+
continue;
|
|
622
|
+
}
|
|
623
|
+
if (entry.status === "pending") {
|
|
624
|
+
runnableRows.push(row);
|
|
625
|
+
continue;
|
|
626
|
+
}
|
|
627
|
+
skippedRowIds.push(row.rowId);
|
|
628
|
+
}
|
|
457
629
|
updateSegmentForTask(state, taskId, "running");
|
|
458
630
|
saveEngineBatchState(projectRoot, state);
|
|
459
631
|
|
|
@@ -472,14 +644,26 @@ export async function runMatrixTaskOnLane({
|
|
|
472
644
|
rowIds: matrix.rows.map((row) => row.rowId),
|
|
473
645
|
matrixType: matrix.type,
|
|
474
646
|
maxParallel,
|
|
647
|
+
matrixMaxParallel,
|
|
648
|
+
rowConcurrency,
|
|
649
|
+
runnableRowIds: runnableRows.map((row) => row.rowId),
|
|
650
|
+
carriedOverRowIds: carryOverRows.map((carry) => carry.row.rowId),
|
|
651
|
+
skippedRowIds,
|
|
475
652
|
});
|
|
476
653
|
|
|
477
|
-
const { results } = await runConcurrent(
|
|
654
|
+
const { results } = await runConcurrent(runnableRows, rowConcurrency, async (row, rowIndex) => {
|
|
478
655
|
// Compete for the global lane pool: block until a slot frees (a sibling
|
|
479
656
|
// lane task or another row releasing), then run on that slot as the
|
|
480
657
|
// row's distinct lane identity. Always release so a crashed row cannot
|
|
481
658
|
// leak a slot and stall the batch.
|
|
482
659
|
const rowLaneNumber = await acquireLaneSlot(state, maxParallel);
|
|
660
|
+
// Per-row live status (#230): flip to `running` only once the row actually
|
|
661
|
+
// holds a slot, and persist so `spine status` sees it from another process.
|
|
662
|
+
const runningEntry = task.matrixRows.find((m) => m.rowId === row.rowId);
|
|
663
|
+
if (runningEntry) {
|
|
664
|
+
runningEntry.status = "running";
|
|
665
|
+
saveEngineBatchState(projectRoot, state);
|
|
666
|
+
}
|
|
483
667
|
try {
|
|
484
668
|
return await runMatrixSubLane({
|
|
485
669
|
projectRoot,
|
|
@@ -494,6 +678,8 @@ export async function runMatrixTaskOnLane({
|
|
|
494
678
|
taskFolderRel,
|
|
495
679
|
config,
|
|
496
680
|
baseBranch,
|
|
681
|
+
rowIndex,
|
|
682
|
+
rowCount: matrix.rows.length,
|
|
497
683
|
});
|
|
498
684
|
} catch (err) {
|
|
499
685
|
// Defensive: a crashed sub-lane must never lose its row identity, or the
|
|
@@ -523,21 +709,36 @@ export async function runMatrixTaskOnLane({
|
|
|
523
709
|
entry.status = rowResult.ok ? "succeeded" : "failed";
|
|
524
710
|
entry.exitCode = rowResult.exitCode ?? null;
|
|
525
711
|
entry.commitSha = rowResult.commitSha ?? null;
|
|
712
|
+
if (rowResult.ok && rowResult.worktreePath && rowResult.branch) {
|
|
713
|
+
// Persist carry-over material (#230): a later row-scoped retry reuses
|
|
714
|
+
// this worktree/branch instead of re-executing the succeeded row.
|
|
715
|
+
entry.worktreePath = rowResult.worktreePath;
|
|
716
|
+
entry.branch = rowResult.branch;
|
|
717
|
+
}
|
|
526
718
|
}
|
|
527
719
|
}
|
|
528
720
|
|
|
721
|
+
// Aggregate from the full per-row state (#230): `canceled` rows are operator
|
|
722
|
+
// exclusions and leave the required set; everything else must be `succeeded`.
|
|
723
|
+
// The default all-rows-must-succeed policy itself is unchanged (#231 out of
|
|
724
|
+
// scope).
|
|
529
725
|
const { ok, failedRowIds } = aggregateMatrixOutcomes(
|
|
530
|
-
|
|
726
|
+
task.matrixRows
|
|
727
|
+
.filter((entry) => entry.status !== "canceled")
|
|
728
|
+
.map((entry) => ({ rowId: entry.rowId, ok: entry.status === "succeeded" })),
|
|
531
729
|
);
|
|
532
730
|
|
|
533
731
|
if (!ok) {
|
|
534
732
|
for (const rowResult of results) {
|
|
535
|
-
if (rowResult.worktreePath)
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
733
|
+
if (!rowResult.worktreePath) continue;
|
|
734
|
+
if (rowResult.ok) continue;
|
|
735
|
+
// Failed rows' worktrees are cleaned up; succeeded rows keep theirs as
|
|
736
|
+
// carry-over material for a row-scoped retry (#230).
|
|
737
|
+
removeMatrixSubLaneWorktree(projectRoot, rowResult.worktreePath, rowResult.branch);
|
|
738
|
+
const failedEntry = task.matrixRows.find((m) => m.rowId === rowResult.rowId);
|
|
739
|
+
if (failedEntry) {
|
|
740
|
+
delete failedEntry.worktreePath;
|
|
741
|
+
delete failedEntry.branch;
|
|
541
742
|
}
|
|
542
743
|
}
|
|
543
744
|
const failedSummary = failedRowIds.join(", ");
|
|
@@ -587,7 +788,22 @@ export async function runMatrixTaskOnLane({
|
|
|
587
788
|
|
|
588
789
|
// All rows succeeded: merge each row branch into the lane worktree so the
|
|
589
790
|
// lane carries every row's output, then run the standard lane commit.
|
|
590
|
-
|
|
791
|
+
// Carry-over rows (#230) merge their kept prior-attempt branches alongside
|
|
792
|
+
// the freshly executed rows — their outputs land exactly once.
|
|
793
|
+
const allRowResults = [
|
|
794
|
+
...carryOverRows.map((carry) => ({
|
|
795
|
+
rowId: carry.row.rowId,
|
|
796
|
+
ok: true,
|
|
797
|
+
exitCode: 0,
|
|
798
|
+
output: "",
|
|
799
|
+
worktreePath: carry.worktreePath,
|
|
800
|
+
branch: carry.branch,
|
|
801
|
+
commitSha: carry.commitSha ?? null,
|
|
802
|
+
carriedOver: true,
|
|
803
|
+
})),
|
|
804
|
+
...results,
|
|
805
|
+
];
|
|
806
|
+
for (const rowResult of allRowResults) {
|
|
591
807
|
if (!rowResult.branch) continue;
|
|
592
808
|
try {
|
|
593
809
|
gitExec(
|
|
@@ -598,7 +814,7 @@ export async function runMatrixTaskOnLane({
|
|
|
598
814
|
} catch (err) {
|
|
599
815
|
const message = err instanceof Error ? err.message : String(err);
|
|
600
816
|
gitExec(wt, ["merge", "--abort"], { throwOnError: false, projectRoot });
|
|
601
|
-
for (const other of
|
|
817
|
+
for (const other of allRowResults) {
|
|
602
818
|
if (other.worktreePath) {
|
|
603
819
|
removeMatrixSubLaneWorktree(projectRoot, other.worktreePath, other.branch);
|
|
604
820
|
}
|
|
@@ -632,7 +848,7 @@ export async function runMatrixTaskOnLane({
|
|
|
632
848
|
laneNumber,
|
|
633
849
|
laneId: lane.laneId,
|
|
634
850
|
correlationId: laneCorrelationId,
|
|
635
|
-
rowIds:
|
|
851
|
+
rowIds: allRowResults.map((rowResult) => rowResult.rowId),
|
|
636
852
|
});
|
|
637
853
|
|
|
638
854
|
const ignorePatterns = resolveWorktreeSetupIgnorePaths(config);
|