pi-spine 2.2.0 → 2.3.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-cli/verify.mjs +2 -19
- package/package.json +1 -1
- package/src/batch/attached-runner-promote.mjs +370 -0
- package/src/batch/attached-runner-reconcile.mjs +291 -0
- package/src/batch/attached-runner.mjs +24 -645
- package/src/batch/batch-guards.mjs +69 -0
- package/src/batch/contract-exec.mjs +483 -0
- package/src/batch/contract-parse.mjs +243 -0
- package/src/batch/contract-verify.mjs +32 -707
- package/src/batch/detached-diagnostics.mjs +255 -0
- package/src/batch/detached-run.mjs +326 -0
- package/src/batch/detached-start.mjs +18 -894
- package/src/batch/detached-wait.mjs +346 -0
- package/src/batch/engine.mjs +8 -67
- package/src/batch/integrate-git.mjs +21 -0
- package/src/batch/integrate.mjs +5 -15
- package/src/batch/journal-rebuild-structural.mjs +352 -0
- package/src/batch/journal-rebuild.mjs +16 -348
- package/src/batch/lane-dirty-check-commit.mjs +361 -0
- package/src/batch/lane-dirty-check-git.mjs +298 -0
- package/src/batch/lane-dirty-check.mjs +31 -297
- package/src/batch/reconcile-batch.mjs +483 -0
- package/src/batch/reconcile-classify.mjs +433 -0
- package/src/batch/reconcile-diagnosis.mjs +459 -0
- package/src/batch/reconcile-light-cache.mjs +61 -0
- package/src/batch/reconcile-orphan.mjs +198 -0
- package/src/batch/reconcile.mjs +19 -1693
- package/src/batch/resume-multi-lanes.mjs +6 -148
- package/src/batch/resume-multi-queue.mjs +159 -0
- package/src/batch/resume-single-validate.mjs +25 -0
- package/src/batch/resume.mjs +3 -20
- package/src/batch/review-artifacts.mjs +419 -0
- package/src/batch/review-step-run.mjs +383 -0
- package/src/batch/review-step.mjs +451 -0
- package/src/batch/review.mjs +32 -1199
- package/src/batch/salvage-batch-list.mjs +291 -0
- package/src/batch/salvage-batch.mjs +10 -284
- package/src/batch/sequence-plan.mjs +234 -0
- package/src/batch/sequence-run.mjs +418 -0
- package/src/batch/sequence-wait.mjs +167 -0
- package/src/batch/sequence.mjs +22 -783
- package/src/batch/state-guards.mjs +284 -0
- package/src/batch/state-io.mjs +135 -0
- package/src/batch/state.mjs +27 -396
- package/src/batch/worker-heartbeat.mjs +375 -0
- package/src/batch/worker-host.mjs +50 -541
- package/src/batch/worker-spawn.mjs +315 -0
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
// @ts-nocheck
|
|
2
|
+
/**
|
|
3
|
+
* Salvage list (dry-run) after batch abort/dismiss (FR-REL220-03, #158).
|
|
4
|
+
* Extracted from salvage-batch.mjs (SP-591); integrate remains in salvage-batch.mjs until SP-605.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import fs from "node:fs";
|
|
8
|
+
import { execFileSync } from "node:child_process";
|
|
9
|
+
import { archiveBatchStatePath } from "./lifecycle.mjs";
|
|
10
|
+
import { countCommitsAhead } from "./lane-commit.mjs";
|
|
11
|
+
import { journalPath, readJournalEvents } from "./journal.mjs";
|
|
12
|
+
import { rebuildBatchStateFromJournal } from "./journal-rebuild.mjs";
|
|
13
|
+
import { loadBatchStateFile } from "./reconcile.mjs";
|
|
14
|
+
import { laneTaskBranch } from "./worktree.mjs";
|
|
15
|
+
|
|
16
|
+
/** Exit reasons that block salvage even when a lane commit exists. */
|
|
17
|
+
export const NON_SALVAGEABLE_EXIT_REASONS = new Set([
|
|
18
|
+
"contract_failed",
|
|
19
|
+
"review_exhausted",
|
|
20
|
+
"review_failed",
|
|
21
|
+
"code_review_invalid_verdict",
|
|
22
|
+
"final_review_invalid_verdict",
|
|
23
|
+
"CONTRACT_FAIL",
|
|
24
|
+
]);
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* @param {string|null|undefined} exitReason
|
|
28
|
+
*/
|
|
29
|
+
export function isNonSalvageableExitReason(exitReason) {
|
|
30
|
+
if (!exitReason) return false;
|
|
31
|
+
return NON_SALVAGEABLE_EXIT_REASONS.has(String(exitReason));
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* @param {string} projectRoot
|
|
36
|
+
* @param {string} baseBranch
|
|
37
|
+
* @param {string} headRef
|
|
38
|
+
*/
|
|
39
|
+
function collectDiffStat(projectRoot, baseBranch, headRef) {
|
|
40
|
+
try {
|
|
41
|
+
return execFileSync("git", ["diff", "--stat", `${baseBranch}..${headRef}`], {
|
|
42
|
+
cwd: projectRoot,
|
|
43
|
+
encoding: "utf-8",
|
|
44
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
45
|
+
}).trim();
|
|
46
|
+
} catch (err) {
|
|
47
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
48
|
+
return `(git diff --stat failed: ${message})`;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* @param {string} projectRoot
|
|
54
|
+
* @param {string} ref
|
|
55
|
+
*/
|
|
56
|
+
function branchExists(projectRoot, ref) {
|
|
57
|
+
try {
|
|
58
|
+
execFileSync("git", ["rev-parse", "--verify", ref], {
|
|
59
|
+
cwd: projectRoot,
|
|
60
|
+
encoding: "utf-8",
|
|
61
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
62
|
+
});
|
|
63
|
+
return true;
|
|
64
|
+
} catch {
|
|
65
|
+
return false;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* @param {object[]} journalEvents
|
|
71
|
+
*/
|
|
72
|
+
function laneCommittedTaskIds(journalEvents) {
|
|
73
|
+
/** @type {Set<string>} */
|
|
74
|
+
const committed = new Set();
|
|
75
|
+
for (const event of journalEvents) {
|
|
76
|
+
if (String(event?.type ?? "") !== "lane.committed") continue;
|
|
77
|
+
const taskId = typeof event.taskId === "string" ? event.taskId : event.payload?.taskId;
|
|
78
|
+
if (taskId) committed.add(String(taskId));
|
|
79
|
+
}
|
|
80
|
+
return committed;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* @param {string} projectRoot
|
|
85
|
+
* @param {string} batchId
|
|
86
|
+
*/
|
|
87
|
+
function loadSalvageBatchSeed(projectRoot, batchId) {
|
|
88
|
+
const archivePath = archiveBatchStatePath(projectRoot, batchId);
|
|
89
|
+
if (fs.existsSync(archivePath)) {
|
|
90
|
+
try {
|
|
91
|
+
return JSON.parse(fs.readFileSync(archivePath, "utf-8"));
|
|
92
|
+
} catch {
|
|
93
|
+
return null;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const loaded = loadBatchStateFile(projectRoot);
|
|
98
|
+
if (loaded.raw && String(loaded.raw.batchId ?? loaded.raw.id ?? "") === batchId) {
|
|
99
|
+
return loaded.raw;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
return null;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* @param {object[]} tasks
|
|
107
|
+
*/
|
|
108
|
+
function laneNumbersFromTasks(tasks) {
|
|
109
|
+
/** @type {Set<number>} */
|
|
110
|
+
const lanes = new Set();
|
|
111
|
+
for (const task of tasks ?? []) {
|
|
112
|
+
const laneNumber = Number(task?.laneNumber);
|
|
113
|
+
if (Number.isFinite(laneNumber) && laneNumber > 0) lanes.add(laneNumber);
|
|
114
|
+
}
|
|
115
|
+
return lanes;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* @param {object} task
|
|
120
|
+
* @param {Set<string>} committedTaskIds
|
|
121
|
+
*/
|
|
122
|
+
function isSalvageableTask(task, committedTaskIds) {
|
|
123
|
+
const taskId = String(task?.taskId ?? "");
|
|
124
|
+
if (!taskId) return false;
|
|
125
|
+
if (!committedTaskIds.has(taskId)) return false;
|
|
126
|
+
|
|
127
|
+
const status = String(task?.status ?? "").toLowerCase();
|
|
128
|
+
if (status !== "succeeded" && status !== "skipped") return false;
|
|
129
|
+
if (isNonSalvageableExitReason(task?.exitReason)) return false;
|
|
130
|
+
return true;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* List lanes with salvageable commits after abort/dismiss (read-only).
|
|
135
|
+
*
|
|
136
|
+
* @param {string} projectRoot
|
|
137
|
+
* @param {string} batchId
|
|
138
|
+
*/
|
|
139
|
+
export function listSalvageableLanes(projectRoot, batchId) {
|
|
140
|
+
const resolvedBatchId = String(batchId ?? "").trim();
|
|
141
|
+
if (!resolvedBatchId) {
|
|
142
|
+
return {
|
|
143
|
+
ok: false,
|
|
144
|
+
exitCode: 1,
|
|
145
|
+
error: "batch_id_required",
|
|
146
|
+
headline: "Batch id is required",
|
|
147
|
+
suggestedCommand: "spine batch salvage --batch <batchId> --dry-run",
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
const journalFile = journalPath(projectRoot, resolvedBatchId);
|
|
152
|
+
if (!fs.existsSync(journalFile)) {
|
|
153
|
+
return {
|
|
154
|
+
ok: false,
|
|
155
|
+
exitCode: 1,
|
|
156
|
+
error: "journal_missing",
|
|
157
|
+
headline: `No journal found for batch ${resolvedBatchId}`,
|
|
158
|
+
suggestedCommand: "spine status --diagnose",
|
|
159
|
+
batchId: resolvedBatchId,
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
const journalEvents = readJournalEvents(projectRoot, resolvedBatchId);
|
|
164
|
+
const seedState = loadSalvageBatchSeed(projectRoot, resolvedBatchId);
|
|
165
|
+
const rebuilt = rebuildBatchStateFromJournal(seedState, journalEvents);
|
|
166
|
+
const baseBranch = String(rebuilt.baseBranch ?? seedState?.baseBranch ?? "main");
|
|
167
|
+
const committedTaskIds = laneCommittedTaskIds(journalEvents);
|
|
168
|
+
|
|
169
|
+
/** @type {Map<number, { laneNumber: number, salvageableTasks: string[], excludedTasks: string[] }>} */
|
|
170
|
+
const laneMap = new Map();
|
|
171
|
+
|
|
172
|
+
for (const task of rebuilt.tasks ?? []) {
|
|
173
|
+
const laneNumber = Number(task?.laneNumber);
|
|
174
|
+
if (!Number.isFinite(laneNumber) || laneNumber <= 0) continue;
|
|
175
|
+
|
|
176
|
+
if (!laneMap.has(laneNumber)) {
|
|
177
|
+
laneMap.set(laneNumber, { laneNumber, salvageableTasks: [], excludedTasks: [] });
|
|
178
|
+
}
|
|
179
|
+
const laneEntry = laneMap.get(laneNumber);
|
|
180
|
+
|
|
181
|
+
const taskId = String(task.taskId ?? "");
|
|
182
|
+
const status = String(task.status ?? "").toLowerCase();
|
|
183
|
+
const hasLaneCommit = committedTaskIds.has(taskId);
|
|
184
|
+
|
|
185
|
+
if (hasLaneCommit && isNonSalvageableExitReason(task.exitReason)) {
|
|
186
|
+
laneEntry.excludedTasks.push(taskId);
|
|
187
|
+
continue;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
if (isSalvageableTask(task, committedTaskIds)) {
|
|
191
|
+
laneEntry.salvageableTasks.push(taskId);
|
|
192
|
+
} else if (
|
|
193
|
+
hasLaneCommit &&
|
|
194
|
+
(status === "failed" || isNonSalvageableExitReason(task.exitReason))
|
|
195
|
+
) {
|
|
196
|
+
laneEntry.excludedTasks.push(taskId);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
for (const laneNumber of laneNumbersFromTasks(rebuilt.tasks)) {
|
|
201
|
+
if (!laneMap.has(laneNumber)) {
|
|
202
|
+
laneMap.set(laneNumber, { laneNumber, salvageableTasks: [], excludedTasks: [] });
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/** @type {object[]} */
|
|
207
|
+
const lanes = [];
|
|
208
|
+
for (const laneNumber of [...laneMap.keys()].sort((a, b) => a - b)) {
|
|
209
|
+
const entry = laneMap.get(laneNumber);
|
|
210
|
+
if (entry.salvageableTasks.length === 0) continue;
|
|
211
|
+
|
|
212
|
+
const taskBranch = laneTaskBranch(resolvedBatchId, laneNumber);
|
|
213
|
+
if (!branchExists(projectRoot, taskBranch)) continue;
|
|
214
|
+
|
|
215
|
+
const commitsAhead = countCommitsAhead(projectRoot, baseBranch, taskBranch);
|
|
216
|
+
if (commitsAhead <= 0) continue;
|
|
217
|
+
|
|
218
|
+
lanes.push({
|
|
219
|
+
laneNumber,
|
|
220
|
+
taskBranch,
|
|
221
|
+
commitsAhead,
|
|
222
|
+
diffStat: collectDiffStat(projectRoot, baseBranch, taskBranch),
|
|
223
|
+
salvageableTasks: [...entry.salvageableTasks],
|
|
224
|
+
excludedTasks: [...entry.excludedTasks],
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
return {
|
|
229
|
+
ok: true,
|
|
230
|
+
exitCode: 0,
|
|
231
|
+
dryRun: true,
|
|
232
|
+
batchId: resolvedBatchId,
|
|
233
|
+
baseBranch,
|
|
234
|
+
lanes,
|
|
235
|
+
headline:
|
|
236
|
+
lanes.length === 0
|
|
237
|
+
? `No salvageable lane commits for batch ${resolvedBatchId}`
|
|
238
|
+
: `${lanes.length} salvageable lane(s) for batch ${resolvedBatchId}`,
|
|
239
|
+
suggestedCommand:
|
|
240
|
+
lanes.length === 0
|
|
241
|
+
? "spine status --diagnose"
|
|
242
|
+
: "spine batch salvage --batch <batchId> --lane <n> --integrate",
|
|
243
|
+
};
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* @param {ReturnType<typeof listSalvageableLanes>} result
|
|
248
|
+
* @param {{ json?: boolean }} [options]
|
|
249
|
+
*/
|
|
250
|
+
export function formatSalvageListOutput(result, options = {}) {
|
|
251
|
+
if (options.json) {
|
|
252
|
+
return `${JSON.stringify(result, null, 2)}\n`;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
const lines = ["", result.ok ? "Salvage list (dry-run)" : "Salvage list failed", "", ` ${result.headline}`];
|
|
256
|
+
|
|
257
|
+
if (result.error) {
|
|
258
|
+
lines.push("", ` Error: ${result.error}`);
|
|
259
|
+
}
|
|
260
|
+
if (result.batchId) {
|
|
261
|
+
lines.push("", ` Batch: ${result.batchId}`);
|
|
262
|
+
}
|
|
263
|
+
if (result.baseBranch) {
|
|
264
|
+
lines.push(` Base: ${result.baseBranch}`);
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
if (result.ok && Array.isArray(result.lanes)) {
|
|
268
|
+
for (const lane of result.lanes) {
|
|
269
|
+
lines.push(
|
|
270
|
+
"",
|
|
271
|
+
` Lane ${lane.laneNumber}: ${lane.taskBranch} (${lane.commitsAhead} commit(s) ahead)`,
|
|
272
|
+
` Tasks: ${lane.salvageableTasks.join(", ")}`,
|
|
273
|
+
);
|
|
274
|
+
if (lane.excludedTasks?.length) {
|
|
275
|
+
lines.push(` Excluded: ${lane.excludedTasks.join(", ")}`);
|
|
276
|
+
}
|
|
277
|
+
if (lane.diffStat) {
|
|
278
|
+
for (const statLine of String(lane.diffStat).split("\n")) {
|
|
279
|
+
lines.push(` ${statLine}`);
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
if (result.suggestedCommand) {
|
|
286
|
+
lines.push("", ` → ${result.suggestedCommand}`);
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
lines.push("");
|
|
290
|
+
return lines.join("\n");
|
|
291
|
+
}
|
|
@@ -1,300 +1,26 @@
|
|
|
1
1
|
// @ts-nocheck
|
|
2
2
|
/**
|
|
3
3
|
* Operator salvage after batch abort/dismiss (FR-REL220-03, #158).
|
|
4
|
-
* List
|
|
4
|
+
* List API → salvage-batch-list.mjs (SP-591); integrate remains here until SP-605.
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
-
import fs from "node:fs";
|
|
8
7
|
import readline from "node:readline/promises";
|
|
9
|
-
import { execFileSync } from "node:child_process";
|
|
10
8
|
import { loadSpineConfig } from "../config/spine-config-load.mjs";
|
|
11
|
-
import { archiveBatchStatePath } from "./lifecycle.mjs";
|
|
12
9
|
import { countCommitsAhead } from "./lane-commit.mjs";
|
|
13
10
|
import { checkIntegrateGate } from "./gate.mjs";
|
|
14
11
|
import { gitExec } from "./git-exec.mjs";
|
|
15
12
|
import { mergeOrchIntoBaseIsolated, syncPlumbingMergePathsToWorktree } from "./integrate-worktree.mjs";
|
|
16
|
-
import {
|
|
17
|
-
import { rebuildBatchStateFromJournal } from "./journal-rebuild.mjs";
|
|
18
|
-
import { loadBatchStateFile } from "./reconcile.mjs";
|
|
13
|
+
import { appendJournalEvent } from "./journal.mjs";
|
|
19
14
|
import { resolveRulesManifestIntegrateDrift } from "./rules-manifest-drift.mjs";
|
|
20
15
|
import { laneTaskBranch } from "./worktree.mjs";
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
export
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
"CONTRACT_FAIL",
|
|
30
|
-
]);
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* @param {string|null|undefined} exitReason
|
|
34
|
-
*/
|
|
35
|
-
export function isNonSalvageableExitReason(exitReason) {
|
|
36
|
-
if (!exitReason) return false;
|
|
37
|
-
return NON_SALVAGEABLE_EXIT_REASONS.has(String(exitReason));
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* @param {string} projectRoot
|
|
42
|
-
* @param {string} baseBranch
|
|
43
|
-
* @param {string} headRef
|
|
44
|
-
*/
|
|
45
|
-
function collectDiffStat(projectRoot, baseBranch, headRef) {
|
|
46
|
-
try {
|
|
47
|
-
return execFileSync("git", ["diff", "--stat", `${baseBranch}..${headRef}`], {
|
|
48
|
-
cwd: projectRoot,
|
|
49
|
-
encoding: "utf-8",
|
|
50
|
-
stdio: ["ignore", "pipe", "pipe"],
|
|
51
|
-
}).trim();
|
|
52
|
-
} catch (err) {
|
|
53
|
-
const message = err instanceof Error ? err.message : String(err);
|
|
54
|
-
return `(git diff --stat failed: ${message})`;
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* @param {string} projectRoot
|
|
60
|
-
* @param {string} ref
|
|
61
|
-
*/
|
|
62
|
-
function branchExists(projectRoot, ref) {
|
|
63
|
-
try {
|
|
64
|
-
execFileSync("git", ["rev-parse", "--verify", ref], {
|
|
65
|
-
cwd: projectRoot,
|
|
66
|
-
encoding: "utf-8",
|
|
67
|
-
stdio: ["ignore", "pipe", "pipe"],
|
|
68
|
-
});
|
|
69
|
-
return true;
|
|
70
|
-
} catch {
|
|
71
|
-
return false;
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
* @param {object[]} journalEvents
|
|
77
|
-
*/
|
|
78
|
-
function laneCommittedTaskIds(journalEvents) {
|
|
79
|
-
/** @type {Set<string>} */
|
|
80
|
-
const committed = new Set();
|
|
81
|
-
for (const event of journalEvents) {
|
|
82
|
-
if (String(event?.type ?? "") !== "lane.committed") continue;
|
|
83
|
-
const taskId = typeof event.taskId === "string" ? event.taskId : event.payload?.taskId;
|
|
84
|
-
if (taskId) committed.add(String(taskId));
|
|
85
|
-
}
|
|
86
|
-
return committed;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
/**
|
|
90
|
-
* @param {string} projectRoot
|
|
91
|
-
* @param {string} batchId
|
|
92
|
-
*/
|
|
93
|
-
function loadSalvageBatchSeed(projectRoot, batchId) {
|
|
94
|
-
const archivePath = archiveBatchStatePath(projectRoot, batchId);
|
|
95
|
-
if (fs.existsSync(archivePath)) {
|
|
96
|
-
try {
|
|
97
|
-
return JSON.parse(fs.readFileSync(archivePath, "utf-8"));
|
|
98
|
-
} catch {
|
|
99
|
-
return null;
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
const loaded = loadBatchStateFile(projectRoot);
|
|
104
|
-
if (loaded.raw && String(loaded.raw.batchId ?? loaded.raw.id ?? "") === batchId) {
|
|
105
|
-
return loaded.raw;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
return null;
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
/**
|
|
112
|
-
* @param {object[]} tasks
|
|
113
|
-
*/
|
|
114
|
-
function laneNumbersFromTasks(tasks) {
|
|
115
|
-
/** @type {Set<number>} */
|
|
116
|
-
const lanes = new Set();
|
|
117
|
-
for (const task of tasks ?? []) {
|
|
118
|
-
const laneNumber = Number(task?.laneNumber);
|
|
119
|
-
if (Number.isFinite(laneNumber) && laneNumber > 0) lanes.add(laneNumber);
|
|
120
|
-
}
|
|
121
|
-
return lanes;
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
/**
|
|
125
|
-
* @param {object} task
|
|
126
|
-
* @param {Set<string>} committedTaskIds
|
|
127
|
-
*/
|
|
128
|
-
function isSalvageableTask(task, committedTaskIds) {
|
|
129
|
-
const taskId = String(task?.taskId ?? "");
|
|
130
|
-
if (!taskId) return false;
|
|
131
|
-
if (!committedTaskIds.has(taskId)) return false;
|
|
132
|
-
|
|
133
|
-
const status = String(task?.status ?? "").toLowerCase();
|
|
134
|
-
if (status !== "succeeded" && status !== "skipped") return false;
|
|
135
|
-
if (isNonSalvageableExitReason(task?.exitReason)) return false;
|
|
136
|
-
return true;
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
/**
|
|
140
|
-
* List lanes with salvageable commits after abort/dismiss (read-only).
|
|
141
|
-
*
|
|
142
|
-
* @param {string} projectRoot
|
|
143
|
-
* @param {string} batchId
|
|
144
|
-
*/
|
|
145
|
-
export function listSalvageableLanes(projectRoot, batchId) {
|
|
146
|
-
const resolvedBatchId = String(batchId ?? "").trim();
|
|
147
|
-
if (!resolvedBatchId) {
|
|
148
|
-
return {
|
|
149
|
-
ok: false,
|
|
150
|
-
exitCode: 1,
|
|
151
|
-
error: "batch_id_required",
|
|
152
|
-
headline: "Batch id is required",
|
|
153
|
-
suggestedCommand: "spine batch salvage --batch <batchId> --dry-run",
|
|
154
|
-
};
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
const journalFile = journalPath(projectRoot, resolvedBatchId);
|
|
158
|
-
if (!fs.existsSync(journalFile)) {
|
|
159
|
-
return {
|
|
160
|
-
ok: false,
|
|
161
|
-
exitCode: 1,
|
|
162
|
-
error: "journal_missing",
|
|
163
|
-
headline: `No journal found for batch ${resolvedBatchId}`,
|
|
164
|
-
suggestedCommand: "spine status --diagnose",
|
|
165
|
-
batchId: resolvedBatchId,
|
|
166
|
-
};
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
const journalEvents = readJournalEvents(projectRoot, resolvedBatchId);
|
|
170
|
-
const seedState = loadSalvageBatchSeed(projectRoot, resolvedBatchId);
|
|
171
|
-
const rebuilt = rebuildBatchStateFromJournal(seedState, journalEvents);
|
|
172
|
-
const baseBranch = String(rebuilt.baseBranch ?? seedState?.baseBranch ?? "main");
|
|
173
|
-
const committedTaskIds = laneCommittedTaskIds(journalEvents);
|
|
174
|
-
|
|
175
|
-
/** @type {Map<number, { laneNumber: number, salvageableTasks: string[], excludedTasks: string[] }>} */
|
|
176
|
-
const laneMap = new Map();
|
|
177
|
-
|
|
178
|
-
for (const task of rebuilt.tasks ?? []) {
|
|
179
|
-
const laneNumber = Number(task?.laneNumber);
|
|
180
|
-
if (!Number.isFinite(laneNumber) || laneNumber <= 0) continue;
|
|
181
|
-
|
|
182
|
-
if (!laneMap.has(laneNumber)) {
|
|
183
|
-
laneMap.set(laneNumber, { laneNumber, salvageableTasks: [], excludedTasks: [] });
|
|
184
|
-
}
|
|
185
|
-
const laneEntry = laneMap.get(laneNumber);
|
|
186
|
-
|
|
187
|
-
const taskId = String(task.taskId ?? "");
|
|
188
|
-
const status = String(task.status ?? "").toLowerCase();
|
|
189
|
-
const hasLaneCommit = committedTaskIds.has(taskId);
|
|
190
|
-
|
|
191
|
-
if (hasLaneCommit && isNonSalvageableExitReason(task.exitReason)) {
|
|
192
|
-
laneEntry.excludedTasks.push(taskId);
|
|
193
|
-
continue;
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
if (isSalvageableTask(task, committedTaskIds)) {
|
|
197
|
-
laneEntry.salvageableTasks.push(taskId);
|
|
198
|
-
} else if (
|
|
199
|
-
hasLaneCommit &&
|
|
200
|
-
(status === "failed" || isNonSalvageableExitReason(task.exitReason))
|
|
201
|
-
) {
|
|
202
|
-
laneEntry.excludedTasks.push(taskId);
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
for (const laneNumber of laneNumbersFromTasks(rebuilt.tasks)) {
|
|
207
|
-
if (!laneMap.has(laneNumber)) {
|
|
208
|
-
laneMap.set(laneNumber, { laneNumber, salvageableTasks: [], excludedTasks: [] });
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
/** @type {object[]} */
|
|
213
|
-
const lanes = [];
|
|
214
|
-
for (const laneNumber of [...laneMap.keys()].sort((a, b) => a - b)) {
|
|
215
|
-
const entry = laneMap.get(laneNumber);
|
|
216
|
-
if (entry.salvageableTasks.length === 0) continue;
|
|
217
|
-
|
|
218
|
-
const taskBranch = laneTaskBranch(resolvedBatchId, laneNumber);
|
|
219
|
-
if (!branchExists(projectRoot, taskBranch)) continue;
|
|
220
|
-
|
|
221
|
-
const commitsAhead = countCommitsAhead(projectRoot, baseBranch, taskBranch);
|
|
222
|
-
if (commitsAhead <= 0) continue;
|
|
223
|
-
|
|
224
|
-
lanes.push({
|
|
225
|
-
laneNumber,
|
|
226
|
-
taskBranch,
|
|
227
|
-
commitsAhead,
|
|
228
|
-
diffStat: collectDiffStat(projectRoot, baseBranch, taskBranch),
|
|
229
|
-
salvageableTasks: [...entry.salvageableTasks],
|
|
230
|
-
excludedTasks: [...entry.excludedTasks],
|
|
231
|
-
});
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
return {
|
|
235
|
-
ok: true,
|
|
236
|
-
exitCode: 0,
|
|
237
|
-
dryRun: true,
|
|
238
|
-
batchId: resolvedBatchId,
|
|
239
|
-
baseBranch,
|
|
240
|
-
lanes,
|
|
241
|
-
headline:
|
|
242
|
-
lanes.length === 0
|
|
243
|
-
? `No salvageable lane commits for batch ${resolvedBatchId}`
|
|
244
|
-
: `${lanes.length} salvageable lane(s) for batch ${resolvedBatchId}`,
|
|
245
|
-
suggestedCommand:
|
|
246
|
-
lanes.length === 0
|
|
247
|
-
? "spine status --diagnose"
|
|
248
|
-
: "spine batch salvage --batch <batchId> --lane <n> --integrate",
|
|
249
|
-
};
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
/**
|
|
253
|
-
* @param {ReturnType<typeof listSalvageableLanes>} result
|
|
254
|
-
* @param {{ json?: boolean }} [options]
|
|
255
|
-
*/
|
|
256
|
-
export function formatSalvageListOutput(result, options = {}) {
|
|
257
|
-
if (options.json) {
|
|
258
|
-
return `${JSON.stringify(result, null, 2)}\n`;
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
const lines = ["", result.ok ? "Salvage list (dry-run)" : "Salvage list failed", "", ` ${result.headline}`];
|
|
262
|
-
|
|
263
|
-
if (result.error) {
|
|
264
|
-
lines.push("", ` Error: ${result.error}`);
|
|
265
|
-
}
|
|
266
|
-
if (result.batchId) {
|
|
267
|
-
lines.push("", ` Batch: ${result.batchId}`);
|
|
268
|
-
}
|
|
269
|
-
if (result.baseBranch) {
|
|
270
|
-
lines.push(` Base: ${result.baseBranch}`);
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
if (result.ok && Array.isArray(result.lanes)) {
|
|
274
|
-
for (const lane of result.lanes) {
|
|
275
|
-
lines.push(
|
|
276
|
-
"",
|
|
277
|
-
` Lane ${lane.laneNumber}: ${lane.taskBranch} (${lane.commitsAhead} commit(s) ahead)`,
|
|
278
|
-
` Tasks: ${lane.salvageableTasks.join(", ")}`,
|
|
279
|
-
);
|
|
280
|
-
if (lane.excludedTasks?.length) {
|
|
281
|
-
lines.push(` Excluded: ${lane.excludedTasks.join(", ")}`);
|
|
282
|
-
}
|
|
283
|
-
if (lane.diffStat) {
|
|
284
|
-
for (const statLine of String(lane.diffStat).split("\n")) {
|
|
285
|
-
lines.push(` ${statLine}`);
|
|
286
|
-
}
|
|
287
|
-
}
|
|
288
|
-
}
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
if (result.suggestedCommand) {
|
|
292
|
-
lines.push("", ` → ${result.suggestedCommand}`);
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
lines.push("");
|
|
296
|
-
return lines.join("\n");
|
|
297
|
-
}
|
|
16
|
+
import { listSalvageableLanes } from "./salvage-batch-list.mjs";
|
|
17
|
+
|
|
18
|
+
export {
|
|
19
|
+
NON_SALVAGEABLE_EXIT_REASONS,
|
|
20
|
+
formatSalvageListOutput,
|
|
21
|
+
isNonSalvageableExitReason,
|
|
22
|
+
listSalvageableLanes,
|
|
23
|
+
} from "./salvage-batch-list.mjs";
|
|
298
24
|
|
|
299
25
|
/**
|
|
300
26
|
* @param {string} projectRoot
|