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
|
@@ -0,0 +1,328 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Row-scoped matrix operations (SP-752 / #230).
|
|
3
|
+
*
|
|
4
|
+
* Split out of `retry.mjs` to keep that module under the Phase 23 500 LOC cap.
|
|
5
|
+
* Shared phase/load helpers live in `retry.mjs` and are imported from there
|
|
6
|
+
* (one-way dependency — `retry.mjs` never imports this module).
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import {
|
|
10
|
+
countPendingSegments,
|
|
11
|
+
recordTaskTransition,
|
|
12
|
+
resetTaskForRetry,
|
|
13
|
+
validateBatchState,
|
|
14
|
+
} from "./state.mjs";
|
|
15
|
+
import { unblockBatchAfterRetry, loadMutableBatch, guardPhaseForMutation, findTask } from "./retry.mjs";
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Parse a matrix row identity token `SP-X[rowId]` / `TP-X[rowId]`.
|
|
19
|
+
* Plain task ids and malformed brackets return null (CLI falls back to whole-task retry/skip).
|
|
20
|
+
*
|
|
21
|
+
* @param {unknown} raw
|
|
22
|
+
* @returns {{ taskId: string, rowId: string } | null}
|
|
23
|
+
*/
|
|
24
|
+
export function parseMatrixRowRef(raw) {
|
|
25
|
+
if (typeof raw !== "string" || raw.length === 0) return null;
|
|
26
|
+
// Exact match: one pair of brackets, non-empty row id, no nested brackets.
|
|
27
|
+
const match = /^([A-Za-z]+-\d+)\[([^\[\]]+)\]$/.exec(raw);
|
|
28
|
+
if (!match) return null;
|
|
29
|
+
return { taskId: match[1], rowId: match[2] };
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Row-scoped matrix retry (#230): `spine batch retry SP-X[rowId]` resets one
|
|
34
|
+
* `failed` row for re-execution without touching sibling rows — succeeded rows
|
|
35
|
+
* are never re-executed (their worktrees carry over), other failed rows stay
|
|
36
|
+
* failed until retried individually. The parent task re-enters the sweep so
|
|
37
|
+
* resume re-runs exactly the rows marked `pending`.
|
|
38
|
+
*
|
|
39
|
+
* @param {object} params
|
|
40
|
+
* @param {string} params.projectRoot
|
|
41
|
+
* @param {string} params.taskId Parent matrix task id.
|
|
42
|
+
* @param {string} params.rowId Row id inside `task.matrixRows`.
|
|
43
|
+
*/
|
|
44
|
+
export function retryTaskRow({ projectRoot, taskId, rowId }) {
|
|
45
|
+
const loadedBatch = loadMutableBatch(projectRoot);
|
|
46
|
+
if (!loadedBatch.ok) return loadedBatch.result;
|
|
47
|
+
const state = loadedBatch.state;
|
|
48
|
+
const phase = String(state.phase ?? "");
|
|
49
|
+
|
|
50
|
+
const guard = guardPhaseForMutation({ phase, operation: "retry" });
|
|
51
|
+
if (!guard.ok) {
|
|
52
|
+
return {
|
|
53
|
+
ok: false,
|
|
54
|
+
exitCode: 1,
|
|
55
|
+
error: guard.error,
|
|
56
|
+
output: guard.output,
|
|
57
|
+
batchId: state.batchId,
|
|
58
|
+
phase,
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const validation = validateBatchState(state);
|
|
63
|
+
if (!validation.ok) {
|
|
64
|
+
return {
|
|
65
|
+
ok: false,
|
|
66
|
+
exitCode: 1,
|
|
67
|
+
error: "invalid_batch_state",
|
|
68
|
+
output: `Batch state validation failed:\n ${validation.errors.join("\n ")}\n`,
|
|
69
|
+
batchId: state.batchId,
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const task = findTask(state, taskId);
|
|
74
|
+
if (!task) {
|
|
75
|
+
return {
|
|
76
|
+
ok: false,
|
|
77
|
+
exitCode: 1,
|
|
78
|
+
error: "task_not_found",
|
|
79
|
+
output: `Task ${taskId} not found in batch ${state.batchId}.\n`,
|
|
80
|
+
batchId: state.batchId,
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const rows = Array.isArray(task.matrixRows) ? task.matrixRows : [];
|
|
85
|
+
if (rows.length === 0) {
|
|
86
|
+
return {
|
|
87
|
+
ok: false,
|
|
88
|
+
exitCode: 1,
|
|
89
|
+
error: "not_a_matrix_task",
|
|
90
|
+
output: `Task ${taskId} has no matrix rows — retry the whole task with \`spine batch retry ${taskId}\`.\n`,
|
|
91
|
+
batchId: state.batchId,
|
|
92
|
+
taskId,
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const row = rows.find((entry) => entry && typeof entry === "object" && entry.rowId === rowId);
|
|
97
|
+
if (!row) {
|
|
98
|
+
return {
|
|
99
|
+
ok: false,
|
|
100
|
+
exitCode: 1,
|
|
101
|
+
error: "row_not_found",
|
|
102
|
+
output: `Row ${taskId}[${rowId}] not found. Matrix rows: ${rows
|
|
103
|
+
.map((entry) => entry?.rowId)
|
|
104
|
+
.join(", ")}\n`,
|
|
105
|
+
batchId: state.batchId,
|
|
106
|
+
taskId,
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
if (row.status !== "failed") {
|
|
111
|
+
const reason =
|
|
112
|
+
row.status === "succeeded"
|
|
113
|
+
? "already succeeded — succeeded rows are never re-executed"
|
|
114
|
+
: row.status === "canceled"
|
|
115
|
+
? "was canceled — re-run the whole task to restore it"
|
|
116
|
+
: `is ${row.status ?? "pending"} — only failed rows can be retried`;
|
|
117
|
+
return {
|
|
118
|
+
ok: false,
|
|
119
|
+
exitCode: 1,
|
|
120
|
+
error: "row_not_retryable",
|
|
121
|
+
output: `Row ${taskId}[${rowId}] ${reason}.\n`,
|
|
122
|
+
batchId: state.batchId,
|
|
123
|
+
taskId,
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// Parent/task/segment reset is identical to a whole-task retry; the only
|
|
128
|
+
// difference is which matrix rows re-enter the sweep as `pending`.
|
|
129
|
+
const reset = resetTaskForRetry(state, taskId);
|
|
130
|
+
if (!reset) {
|
|
131
|
+
return {
|
|
132
|
+
ok: false,
|
|
133
|
+
exitCode: 1,
|
|
134
|
+
error: "task_not_found",
|
|
135
|
+
output: `Task ${taskId} not found.\n`,
|
|
136
|
+
batchId: state.batchId,
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
row.status = "pending";
|
|
140
|
+
row.exitCode = null;
|
|
141
|
+
|
|
142
|
+
const unblocked = unblockBatchAfterRetry(state);
|
|
143
|
+
if (!unblocked) {
|
|
144
|
+
state.phase = "failed";
|
|
145
|
+
state.endedAt = null;
|
|
146
|
+
state.lastError = null;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
state.resilience = state.resilience ?? {};
|
|
150
|
+
state.resilience.retryCountByScope = state.resilience.retryCountByScope ?? {};
|
|
151
|
+
const rowScope = `${taskId}[${rowId}]`;
|
|
152
|
+
state.resilience.retryCountByScope[rowScope] = (state.resilience.retryCountByScope[rowScope] ?? 0) + 1;
|
|
153
|
+
|
|
154
|
+
const pendingSegments = countPendingSegments(state, taskId);
|
|
155
|
+
recordTaskTransition({
|
|
156
|
+
projectRoot,
|
|
157
|
+
state,
|
|
158
|
+
journalType: "task.retry_requested",
|
|
159
|
+
journalPayload: {
|
|
160
|
+
taskId,
|
|
161
|
+
rowId,
|
|
162
|
+
previousClassification: reset.previousClassification,
|
|
163
|
+
pendingSegments,
|
|
164
|
+
retriedRowIds: [rowId],
|
|
165
|
+
},
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
if (unblocked) {
|
|
169
|
+
recordTaskTransition({
|
|
170
|
+
projectRoot,
|
|
171
|
+
state,
|
|
172
|
+
journalType: "batch.retry_unblocked",
|
|
173
|
+
journalPayload: {
|
|
174
|
+
taskId,
|
|
175
|
+
rowId,
|
|
176
|
+
pendingSegments,
|
|
177
|
+
fromPhase: phase,
|
|
178
|
+
},
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
const resumeHint = unblocked ? "spine batch resume" : "spine batch resume --force";
|
|
183
|
+
|
|
184
|
+
return {
|
|
185
|
+
ok: true,
|
|
186
|
+
exitCode: 0,
|
|
187
|
+
batchId: state.batchId,
|
|
188
|
+
taskId,
|
|
189
|
+
rowId,
|
|
190
|
+
pendingSegments,
|
|
191
|
+
unblocked,
|
|
192
|
+
output: `Row ${taskId}[${rowId}] reset for retry (pendingSegments=${pendingSegments}); sibling rows keep their status.\n → ${resumeHint}\n`,
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* Row-scoped matrix cancel (#230): `spine batch skip SP-X[rowId]` marks one row
|
|
198
|
+
* `canceled`. Canceled rows do not execute on resume and are excluded from the
|
|
199
|
+
* parent aggregation (the all-rows-must-succeed default now covers only the
|
|
200
|
+
* remaining rows). Cancel the whole matrix with `spine batch skip SP-X`.
|
|
201
|
+
*
|
|
202
|
+
* @param {object} params
|
|
203
|
+
* @param {string} params.projectRoot
|
|
204
|
+
* @param {string} params.taskId Parent matrix task id.
|
|
205
|
+
* @param {string} params.rowId Row id inside `task.matrixRows`.
|
|
206
|
+
*/
|
|
207
|
+
export function skipTaskRow({ projectRoot, taskId, rowId }) {
|
|
208
|
+
const loadedBatch = loadMutableBatch(projectRoot);
|
|
209
|
+
if (!loadedBatch.ok) return loadedBatch.result;
|
|
210
|
+
const state = loadedBatch.state;
|
|
211
|
+
const phase = String(state.phase ?? "");
|
|
212
|
+
|
|
213
|
+
const guard = guardPhaseForMutation({ phase, operation: "skip" });
|
|
214
|
+
if (!guard.ok) {
|
|
215
|
+
return {
|
|
216
|
+
ok: false,
|
|
217
|
+
exitCode: 1,
|
|
218
|
+
error: guard.error,
|
|
219
|
+
output: guard.output,
|
|
220
|
+
batchId: state.batchId,
|
|
221
|
+
phase,
|
|
222
|
+
};
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
const validation = validateBatchState(state);
|
|
226
|
+
if (!validation.ok) {
|
|
227
|
+
return {
|
|
228
|
+
ok: false,
|
|
229
|
+
exitCode: 1,
|
|
230
|
+
error: "invalid_batch_state",
|
|
231
|
+
output: `Batch state validation failed:\n ${validation.errors.join("\n ")}\n`,
|
|
232
|
+
batchId: state.batchId,
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
const task = findTask(state, taskId);
|
|
237
|
+
if (!task) {
|
|
238
|
+
return {
|
|
239
|
+
ok: false,
|
|
240
|
+
exitCode: 1,
|
|
241
|
+
error: "task_not_found",
|
|
242
|
+
output: `Task ${taskId} not found in batch ${state.batchId}.\n`,
|
|
243
|
+
batchId: state.batchId,
|
|
244
|
+
};
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
const rows = Array.isArray(task.matrixRows) ? task.matrixRows : [];
|
|
248
|
+
if (rows.length === 0) {
|
|
249
|
+
return {
|
|
250
|
+
ok: false,
|
|
251
|
+
exitCode: 1,
|
|
252
|
+
error: "not_a_matrix_task",
|
|
253
|
+
output: `Task ${taskId} has no matrix rows — skip the whole task with \`spine batch skip ${taskId}\`.\n`,
|
|
254
|
+
batchId: state.batchId,
|
|
255
|
+
taskId,
|
|
256
|
+
};
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
const row = rows.find((entry) => entry && typeof entry === "object" && entry.rowId === rowId);
|
|
260
|
+
if (!row) {
|
|
261
|
+
return {
|
|
262
|
+
ok: false,
|
|
263
|
+
exitCode: 1,
|
|
264
|
+
error: "row_not_found",
|
|
265
|
+
output: `Row ${taskId}[${rowId}] not found. Matrix rows: ${rows
|
|
266
|
+
.map((entry) => entry?.rowId)
|
|
267
|
+
.join(", ")}\n`,
|
|
268
|
+
batchId: state.batchId,
|
|
269
|
+
taskId,
|
|
270
|
+
};
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
const previousStatus = String(row.status ?? "pending");
|
|
274
|
+
if (previousStatus === "succeeded") {
|
|
275
|
+
return {
|
|
276
|
+
ok: false,
|
|
277
|
+
exitCode: 1,
|
|
278
|
+
error: "row_not_cancellable",
|
|
279
|
+
output: `Row ${taskId}[${rowId}] already succeeded — cannot cancel a succeeded row.\n`,
|
|
280
|
+
batchId: state.batchId,
|
|
281
|
+
taskId,
|
|
282
|
+
};
|
|
283
|
+
}
|
|
284
|
+
if (previousStatus === "canceled") {
|
|
285
|
+
return {
|
|
286
|
+
ok: false,
|
|
287
|
+
exitCode: 1,
|
|
288
|
+
error: "row_not_cancellable",
|
|
289
|
+
output: `Row ${taskId}[${rowId}] is already canceled.\n`,
|
|
290
|
+
batchId: state.batchId,
|
|
291
|
+
taskId,
|
|
292
|
+
};
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
row.status = "canceled";
|
|
296
|
+
row.exitCode = null;
|
|
297
|
+
row.endedAt = Date.now();
|
|
298
|
+
|
|
299
|
+
recordTaskTransition({
|
|
300
|
+
projectRoot,
|
|
301
|
+
state,
|
|
302
|
+
journalType: "matrix.row_skipped",
|
|
303
|
+
journalPayload: {
|
|
304
|
+
taskId,
|
|
305
|
+
rowId,
|
|
306
|
+
previousStatus,
|
|
307
|
+
},
|
|
308
|
+
});
|
|
309
|
+
|
|
310
|
+
const resumeHint = phase === "failed" ? "spine batch resume --force" : "spine batch resume";
|
|
311
|
+
let output = `Row ${taskId}[${rowId}] canceled (was ${previousStatus}). It will not execute on resume and is excluded from matrix aggregation.\n`;
|
|
312
|
+
const allResolved = rows.every(
|
|
313
|
+
(entry) => entry && typeof entry === "object" && (entry.status === "succeeded" || entry.status === "canceled"),
|
|
314
|
+
);
|
|
315
|
+
if (allResolved) {
|
|
316
|
+
output += ` All rows are now succeeded or canceled — retry the task to land carried-over rows.\n → spine batch retry ${taskId}\n`;
|
|
317
|
+
}
|
|
318
|
+
output += ` → ${resumeHint}\n`;
|
|
319
|
+
|
|
320
|
+
return {
|
|
321
|
+
ok: true,
|
|
322
|
+
exitCode: 0,
|
|
323
|
+
batchId: state.batchId,
|
|
324
|
+
taskId,
|
|
325
|
+
rowId,
|
|
326
|
+
output,
|
|
327
|
+
};
|
|
328
|
+
}
|
package/src/batch/retry.mjs
CHANGED
|
@@ -18,6 +18,7 @@ import {
|
|
|
18
18
|
const RETRY_ALLOWED_PHASES = new Set(["paused", "failed"]);
|
|
19
19
|
const RETRY_BLOCKED_PHASES = new Set(["running", "planning"]);
|
|
20
20
|
|
|
21
|
+
|
|
21
22
|
/**
|
|
22
23
|
* Fail running tasks and clear dead engine/worker PIDs before retry/skip (#203 / SP-647).
|
|
23
24
|
*
|
|
@@ -28,11 +29,93 @@ function reconcileOrphanBeforeTaskMutation(projectRoot, state) {
|
|
|
28
29
|
return reconcileOrphanRunningState({ projectRoot, state });
|
|
29
30
|
}
|
|
30
31
|
|
|
32
|
+
/**
|
|
33
|
+
* Shared preamble for task/row mutations: load batch state, reconcile orphan
|
|
34
|
+
* running state, reload. Returns the live state or a ready-to-return error.
|
|
35
|
+
*
|
|
36
|
+
* @param {string} projectRoot
|
|
37
|
+
* @returns {{ ok: true, state: object } | { ok: false, result: { ok: false, exitCode: number, error: string, output: string } }}
|
|
38
|
+
*/
|
|
39
|
+
export function loadMutableBatch(projectRoot) {
|
|
40
|
+
const loaded = loadSpineBatchState(projectRoot);
|
|
41
|
+
if (!loaded.raw) {
|
|
42
|
+
return {
|
|
43
|
+
ok: false,
|
|
44
|
+
result: { ok: false, exitCode: 1, error: "no_active_batch", output: "No active pi-spine batch.\n" },
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
reconcileOrphanBeforeTaskMutation(projectRoot, loaded.raw);
|
|
49
|
+
|
|
50
|
+
const reloaded = loadSpineBatchState(projectRoot);
|
|
51
|
+
const state = reloaded.raw;
|
|
52
|
+
if (!state) {
|
|
53
|
+
return {
|
|
54
|
+
ok: false,
|
|
55
|
+
result: { ok: false, exitCode: 1, error: "no_active_batch", output: "No active pi-spine batch.\n" },
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
return { ok: true, state };
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Phase guard shared by retry/skip at task and row scope. `operation` only
|
|
63
|
+
* shapes the error text ("retry" vs "skip"); the phase sets are shared.
|
|
64
|
+
*
|
|
65
|
+
* @param {object} params
|
|
66
|
+
* @param {string} params.phase
|
|
67
|
+
* @param {"retry" | "skip"} params.operation
|
|
68
|
+
* @returns {{ ok: true } | { ok: false, error: string, output: string, batchId?: string, phase: string }}
|
|
69
|
+
*/
|
|
70
|
+
export function guardPhaseForMutation({ phase, operation }) {
|
|
71
|
+
const verb = operation === "skip" ? "skip" : "retry";
|
|
72
|
+
if (RETRY_BLOCKED_PHASES.has(phase)) {
|
|
73
|
+
return {
|
|
74
|
+
ok: false,
|
|
75
|
+
error: `cannot_${verb}`,
|
|
76
|
+
output: `Cannot ${verb} task while batch phase is ${phase}. Pause the batch first.\n`,
|
|
77
|
+
phase,
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
if (operation === "retry" && !RETRY_ALLOWED_PHASES.has(phase)) {
|
|
81
|
+
return {
|
|
82
|
+
ok: false,
|
|
83
|
+
error: "cannot_retry",
|
|
84
|
+
output: `Cannot retry task in batch phase ${phase}. Retry is allowed in paused or failed batches.\n`,
|
|
85
|
+
phase,
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
return { ok: true };
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Flip a matrix task's `failed` rows back to `pending` for a whole-task retry
|
|
93
|
+
* while preserving `succeeded` rows (never re-executed — #230) and `canceled`
|
|
94
|
+
* rows (operator exclusion wins).
|
|
95
|
+
*
|
|
96
|
+
* @param {object} task
|
|
97
|
+
* @returns {string[]} Row ids reset for re-execution.
|
|
98
|
+
*/
|
|
99
|
+
function resetMatrixRowsForRetry(task) {
|
|
100
|
+
const rows = Array.isArray(task?.matrixRows) ? task.matrixRows : [];
|
|
101
|
+
/** @type {string[]} */
|
|
102
|
+
const retried = [];
|
|
103
|
+
for (const row of rows) {
|
|
104
|
+
if (!row || typeof row !== "object") continue;
|
|
105
|
+
if (row.status === "failed") {
|
|
106
|
+
row.status = "pending";
|
|
107
|
+
row.exitCode = null;
|
|
108
|
+
retried.push(row.rowId);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
return retried;
|
|
112
|
+
}
|
|
113
|
+
|
|
31
114
|
/**
|
|
32
115
|
* @param {object} state
|
|
33
116
|
* @param {string} taskId
|
|
34
117
|
*/
|
|
35
|
-
function findTask(state, taskId) {
|
|
118
|
+
export function findTask(state, taskId) {
|
|
36
119
|
return (state.tasks ?? []).find(
|
|
37
120
|
(task) => task && typeof task === "object" && task.taskId === taskId,
|
|
38
121
|
);
|
|
@@ -120,37 +203,18 @@ export function detectSegmentDrift(state) {
|
|
|
120
203
|
* @param {string} params.taskId
|
|
121
204
|
*/
|
|
122
205
|
export function retryTask({ projectRoot, taskId }) {
|
|
123
|
-
const
|
|
124
|
-
if (!
|
|
125
|
-
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
reconcileOrphanBeforeTaskMutation(projectRoot, loaded.raw);
|
|
129
|
-
|
|
130
|
-
const reloaded = loadSpineBatchState(projectRoot);
|
|
131
|
-
const state = reloaded.raw;
|
|
132
|
-
if (!state) {
|
|
133
|
-
return { ok: false, exitCode: 1, error: "no_active_batch", output: "No active pi-spine batch.\n" };
|
|
134
|
-
}
|
|
206
|
+
const loadedBatch = loadMutableBatch(projectRoot);
|
|
207
|
+
if (!loadedBatch.ok) return loadedBatch.result;
|
|
208
|
+
const state = loadedBatch.state;
|
|
135
209
|
const phase = String(state.phase ?? "");
|
|
136
210
|
|
|
137
|
-
|
|
211
|
+
const guard = guardPhaseForMutation({ phase, operation: "retry" });
|
|
212
|
+
if (!guard.ok) {
|
|
138
213
|
return {
|
|
139
214
|
ok: false,
|
|
140
215
|
exitCode: 1,
|
|
141
|
-
error:
|
|
142
|
-
output:
|
|
143
|
-
batchId: state.batchId,
|
|
144
|
-
phase,
|
|
145
|
-
};
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
if (!RETRY_ALLOWED_PHASES.has(phase)) {
|
|
149
|
-
return {
|
|
150
|
-
ok: false,
|
|
151
|
-
exitCode: 1,
|
|
152
|
-
error: "cannot_retry",
|
|
153
|
-
output: `Cannot retry task in batch phase ${phase}. Retry is allowed in paused or failed batches.\n`,
|
|
216
|
+
error: guard.error,
|
|
217
|
+
output: guard.output,
|
|
154
218
|
batchId: state.batchId,
|
|
155
219
|
phase,
|
|
156
220
|
};
|
|
@@ -204,6 +268,10 @@ export function retryTask({ projectRoot, taskId }) {
|
|
|
204
268
|
};
|
|
205
269
|
}
|
|
206
270
|
|
|
271
|
+
// Matrix tasks (#230): succeeded rows carry over (never re-executed) and
|
|
272
|
+
// canceled rows stay excluded — only failed rows re-enter the sweep.
|
|
273
|
+
const retriedRowIds = resetMatrixRowsForRetry(task);
|
|
274
|
+
|
|
207
275
|
const unblocked = unblockBatchAfterRetry(state);
|
|
208
276
|
if (!unblocked) {
|
|
209
277
|
state.phase = "failed";
|
|
@@ -216,7 +284,8 @@ export function retryTask({ projectRoot, taskId }) {
|
|
|
216
284
|
state.resilience.retryCountByScope[taskId] = (state.resilience.retryCountByScope[taskId] ?? 0) + 1;
|
|
217
285
|
|
|
218
286
|
const pendingSegments = countPendingSegments(state, taskId);
|
|
219
|
-
|
|
287
|
+
// The shared preamble already reloaded state, so the live phase is `phase`.
|
|
288
|
+
const previousPhase = phase;
|
|
220
289
|
recordTaskTransition({
|
|
221
290
|
projectRoot,
|
|
222
291
|
state,
|
|
@@ -225,6 +294,7 @@ export function retryTask({ projectRoot, taskId }) {
|
|
|
225
294
|
taskId,
|
|
226
295
|
previousClassification: reset.previousClassification,
|
|
227
296
|
pendingSegments,
|
|
297
|
+
...(retriedRowIds.length > 0 ? { retriedRowIds } : {}),
|
|
228
298
|
},
|
|
229
299
|
});
|
|
230
300
|
|
|
@@ -243,6 +313,12 @@ export function retryTask({ projectRoot, taskId }) {
|
|
|
243
313
|
|
|
244
314
|
const resumeHint = unblocked ? "spine batch resume" : "spine batch resume --force";
|
|
245
315
|
|
|
316
|
+
let output = `Task ${taskId} reset for retry (pendingSegments=${pendingSegments}).`;
|
|
317
|
+
if (retriedRowIds.length > 0) {
|
|
318
|
+
output += `\n Matrix rows re-running: ${retriedRowIds.join(", ")} (succeeded rows carry over).`;
|
|
319
|
+
}
|
|
320
|
+
output += `\n → ${resumeHint}\n`;
|
|
321
|
+
|
|
246
322
|
return {
|
|
247
323
|
ok: true,
|
|
248
324
|
exitCode: 0,
|
|
@@ -250,7 +326,8 @@ export function retryTask({ projectRoot, taskId }) {
|
|
|
250
326
|
taskId,
|
|
251
327
|
pendingSegments,
|
|
252
328
|
unblocked,
|
|
253
|
-
|
|
329
|
+
...(retriedRowIds.length > 0 ? { retriedRowIds } : {}),
|
|
330
|
+
output,
|
|
254
331
|
};
|
|
255
332
|
}
|
|
256
333
|
|
|
@@ -260,26 +337,18 @@ export function retryTask({ projectRoot, taskId }) {
|
|
|
260
337
|
* @param {string} params.taskId
|
|
261
338
|
*/
|
|
262
339
|
export function skipTask({ projectRoot, taskId }) {
|
|
263
|
-
const
|
|
264
|
-
if (!
|
|
265
|
-
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
reconcileOrphanBeforeTaskMutation(projectRoot, loaded.raw);
|
|
269
|
-
|
|
270
|
-
const reloaded = loadSpineBatchState(projectRoot);
|
|
271
|
-
const state = reloaded.raw;
|
|
272
|
-
if (!state) {
|
|
273
|
-
return { ok: false, exitCode: 1, error: "no_active_batch", output: "No active pi-spine batch.\n" };
|
|
274
|
-
}
|
|
340
|
+
const loadedBatch = loadMutableBatch(projectRoot);
|
|
341
|
+
if (!loadedBatch.ok) return loadedBatch.result;
|
|
342
|
+
const state = loadedBatch.state;
|
|
275
343
|
const phase = String(state.phase ?? "");
|
|
276
344
|
|
|
277
|
-
|
|
345
|
+
const guard = guardPhaseForMutation({ phase, operation: "skip" });
|
|
346
|
+
if (!guard.ok) {
|
|
278
347
|
return {
|
|
279
348
|
ok: false,
|
|
280
349
|
exitCode: 1,
|
|
281
|
-
error:
|
|
282
|
-
output:
|
|
350
|
+
error: guard.error,
|
|
351
|
+
output: guard.output,
|
|
283
352
|
batchId: state.batchId,
|
|
284
353
|
phase,
|
|
285
354
|
};
|
|
@@ -354,3 +423,4 @@ export function skipTask({ projectRoot, taskId }) {
|
|
|
354
423
|
: `Task ${taskId} skipped.\n → spine batch resume --force\n`,
|
|
355
424
|
};
|
|
356
425
|
}
|
|
426
|
+
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
// @ts-nocheck
|
|
2
1
|
/**
|
|
3
2
|
* Batch-state write guards, engine PID tracking, and schema validation (SP-587 / FR-SHIP-02).
|
|
4
3
|
*/
|
|
@@ -39,7 +38,7 @@ function archivedBatchStatePath(projectRoot, batchId) {
|
|
|
39
38
|
* Reject cache writes from non-owner engines or post-archive resurrection (SP-254).
|
|
40
39
|
*
|
|
41
40
|
* @param {string} projectRoot
|
|
42
|
-
* @param {
|
|
41
|
+
* @param {Record<string, any>} state
|
|
43
42
|
* @returns {{ allowed: boolean, reason?: string }}
|
|
44
43
|
*/
|
|
45
44
|
export function evaluateBatchStateWriteGuard(projectRoot, state) {
|
|
@@ -121,7 +120,7 @@ export function readBatchEngineStartedAt(raw) {
|
|
|
121
120
|
}
|
|
122
121
|
|
|
123
122
|
/**
|
|
124
|
-
* @param {
|
|
123
|
+
* @param {Record<string, any>} state
|
|
125
124
|
* @param {number} enginePid
|
|
126
125
|
*/
|
|
127
126
|
export function recordBatchEnginePid(state, enginePid) {
|
|
@@ -133,7 +132,7 @@ export function recordBatchEnginePid(state, enginePid) {
|
|
|
133
132
|
}
|
|
134
133
|
|
|
135
134
|
/**
|
|
136
|
-
* @param {
|
|
135
|
+
* @param {Record<string, any>} state
|
|
137
136
|
*/
|
|
138
137
|
export function clearBatchEnginePid(state) {
|
|
139
138
|
if (!state?.resilience || typeof state.resilience !== "object") return;
|
|
@@ -237,7 +236,7 @@ export function validateBatchState(state) {
|
|
|
237
236
|
? /** @type {{ taskIds?: string[] }} */ (lane).taskIds
|
|
238
237
|
: [];
|
|
239
238
|
for (const taskId of taskIds) {
|
|
240
|
-
const found = (raw.tasks ?? []).some(
|
|
239
|
+
const found = (/** @type {unknown[]} */ (raw.tasks ?? [])).some(
|
|
241
240
|
(task) =>
|
|
242
241
|
task &&
|
|
243
242
|
typeof task === "object" &&
|
package/src/batch/state-io.mjs
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
// @ts-nocheck
|
|
2
1
|
/**
|
|
3
2
|
* Batch-state read/write/archive I/O (SP-587 / FR-SHIP-02).
|
|
4
3
|
*/
|
|
@@ -26,6 +25,7 @@ export function spineBatchStatePath(projectRoot) {
|
|
|
26
25
|
|
|
27
26
|
/**
|
|
28
27
|
* @param {string} projectRoot
|
|
28
|
+
* @returns {{ path: string | null, raw: any, parseError: string | null }}
|
|
29
29
|
*/
|
|
30
30
|
export function loadSpineBatchState(projectRoot) {
|
|
31
31
|
const filePath = spineBatchStatePath(projectRoot);
|
|
@@ -60,7 +60,7 @@ function archivedBatchStatePath(projectRoot, batchId) {
|
|
|
60
60
|
* (engine vs. CLI complete/resume/abort).
|
|
61
61
|
*
|
|
62
62
|
* @param {string} projectRoot
|
|
63
|
-
* @param {
|
|
63
|
+
* @param {Record<string, any>} state
|
|
64
64
|
* @param {{ bypassWriteGuard?: boolean }} [options]
|
|
65
65
|
*/
|
|
66
66
|
export function saveSpineBatchState(projectRoot, state, options = {}) {
|
|
@@ -155,6 +155,7 @@ function buildWorkerFailureResult({
|
|
|
155
155
|
* @param {string[]} [params.fileScopePaths]
|
|
156
156
|
* @param {number} [params.timeoutMs]
|
|
157
157
|
* @param {object} [params.workerBackendDeps] Test-only injectables for agentSession backend
|
|
158
|
+
* @param {Record<string, string>} [params.extraEnv] Extra child env (matrix row identity, #229)
|
|
158
159
|
*/
|
|
159
160
|
export async function runWorker({
|
|
160
161
|
worktreePath,
|
|
@@ -171,6 +172,7 @@ export async function runWorker({
|
|
|
171
172
|
fileScopePaths = [],
|
|
172
173
|
timeoutMs: _timeoutMs = DEFAULT_TIMEOUT_MS,
|
|
173
174
|
workerBackendDeps = {},
|
|
175
|
+
extraEnv,
|
|
174
176
|
}) {
|
|
175
177
|
const donePath = path.join(taskFolder, ".DONE");
|
|
176
178
|
if (fs.existsSync(donePath)) {
|
|
@@ -250,6 +252,7 @@ export async function runWorker({
|
|
|
250
252
|
fileScopePaths,
|
|
251
253
|
config,
|
|
252
254
|
workerBackendDeps,
|
|
255
|
+
extraEnv,
|
|
253
256
|
});
|
|
254
257
|
const workerChild = /** @type {WorkerChildHandle} */ (child);
|
|
255
258
|
let childPastPreflight = isExecute ? true : !useLaunchScript;
|