pi-spine 2.3.2 → 2.4.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/batch-meta-reconstruct.mjs +400 -0
- package/src/batch/batch-meta.mjs +115 -0
- package/src/batch/engine.mjs +2 -0
- package/src/batch/journal-rebuild-drift.mjs +397 -0
- package/src/batch/journal-rebuild-structural.mjs +1 -1
- package/src/batch/journal-rebuild.mjs +12 -392
- package/src/batch/resume-multi-validate.mjs +15 -2
- package/src/batch/resume.mjs +12 -5
- package/src/batch/salvage-batch-integrate.mjs +410 -0
- package/src/batch/salvage-batch-list.mjs +1 -1
- package/src/batch/salvage-batch.mjs +7 -406
package/package.json
CHANGED
|
@@ -0,0 +1,400 @@
|
|
|
1
|
+
// @ts-nocheck
|
|
2
|
+
/**
|
|
3
|
+
* Reconstruct batch-state from batch-meta + runtime artifacts (SP-620 / FR-REL240-04 / #126).
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import fs from "node:fs";
|
|
7
|
+
import path from "node:path";
|
|
8
|
+
import { BATCH_META_FILENAME, batchMetaPath } from "./batch-meta.mjs";
|
|
9
|
+
import { appendJournalEvent, readJournalEvents } from "./journal.mjs";
|
|
10
|
+
import { rebuildBatchStateFromJournal } from "./journal-rebuild.mjs";
|
|
11
|
+
import { computePendingTasks, findResumableWave } from "./resume-validation.mjs";
|
|
12
|
+
import { createInitialBatchState, clearBatchEnginePid, saveSpineBatchState } from "./state.mjs";
|
|
13
|
+
import { loadSpineBatchState } from "./state-io.mjs";
|
|
14
|
+
import { laneTaskBranch, laneWorktreePath } from "./worktree.mjs";
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* @param {string} projectRoot
|
|
18
|
+
*/
|
|
19
|
+
function runtimeRoot(projectRoot) {
|
|
20
|
+
return path.join(projectRoot, ".spine", "runtime");
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* @param {string} projectRoot
|
|
25
|
+
* @param {string} batchId
|
|
26
|
+
*/
|
|
27
|
+
function archiveBatchStatePath(projectRoot, batchId) {
|
|
28
|
+
return path.join(runtimeRoot(projectRoot), String(batchId), "archive", "batch-state.json");
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* @param {unknown} value
|
|
33
|
+
* @returns {string[][]}
|
|
34
|
+
*/
|
|
35
|
+
function normalizeWavePlan(value) {
|
|
36
|
+
if (!Array.isArray(value)) return [];
|
|
37
|
+
return value.map((wave) => (Array.isArray(wave) ? wave.map((id) => String(id)) : []));
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Fail-closed when journal-derived waves disagree with persisted meta topology.
|
|
42
|
+
*
|
|
43
|
+
* @param {string[][]} metaPlan
|
|
44
|
+
* @param {string[][]} journalPlan
|
|
45
|
+
*/
|
|
46
|
+
export function wavePlansConflict(metaPlan, journalPlan) {
|
|
47
|
+
const meta = normalizeWavePlan(metaPlan).filter((wave) => wave.length > 0);
|
|
48
|
+
const journal = normalizeWavePlan(journalPlan).filter((wave) => wave.length > 0);
|
|
49
|
+
if (meta.length === 0 || journal.length === 0) return false;
|
|
50
|
+
if (meta.length !== journal.length) return true;
|
|
51
|
+
for (let i = 0; i < meta.length; i++) {
|
|
52
|
+
const a = [...meta[i]].sort();
|
|
53
|
+
const b = [...journal[i]].sort();
|
|
54
|
+
if (a.length !== b.length) return true;
|
|
55
|
+
for (let j = 0; j < a.length; j++) {
|
|
56
|
+
if (a[j] !== b[j]) return true;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return false;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* @param {string} projectRoot
|
|
64
|
+
* @returns {{ batchId: string, path: string }[]}
|
|
65
|
+
*/
|
|
66
|
+
export function listBatchMetaArtifacts(projectRoot) {
|
|
67
|
+
const root = runtimeRoot(projectRoot);
|
|
68
|
+
if (!fs.existsSync(root)) return [];
|
|
69
|
+
/** @type {{ batchId: string, path: string }[]} */
|
|
70
|
+
const found = [];
|
|
71
|
+
for (const entry of fs.readdirSync(root, { withFileTypes: true })) {
|
|
72
|
+
if (!entry.isDirectory()) continue;
|
|
73
|
+
const filePath = path.join(root, entry.name, BATCH_META_FILENAME);
|
|
74
|
+
if (fs.existsSync(filePath)) {
|
|
75
|
+
found.push({ batchId: entry.name, path: filePath });
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
return found.sort((a, b) => a.batchId.localeCompare(b.batchId));
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* @param {string} projectRoot
|
|
83
|
+
* @param {string} batchId
|
|
84
|
+
* @returns {{ ok: true, meta: object, path: string } | { ok: false, error: string, output: string }}
|
|
85
|
+
*/
|
|
86
|
+
export function loadBatchMetaRuntimeArtifact(projectRoot, batchId) {
|
|
87
|
+
const resolvedBatchId = String(batchId ?? "").trim();
|
|
88
|
+
if (!resolvedBatchId) {
|
|
89
|
+
return {
|
|
90
|
+
ok: false,
|
|
91
|
+
error: "batch_meta_missing",
|
|
92
|
+
output:
|
|
93
|
+
"Cannot reconstruct batch state: batchId is required to load batch-meta.json.\n" +
|
|
94
|
+
" → Inspect .spine/runtime/*/batch-meta.json then spine batch resume --force\n",
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const filePath = batchMetaPath(projectRoot, resolvedBatchId);
|
|
99
|
+
if (!fs.existsSync(filePath)) {
|
|
100
|
+
return {
|
|
101
|
+
ok: false,
|
|
102
|
+
error: "batch_meta_missing",
|
|
103
|
+
output:
|
|
104
|
+
`Cannot reconstruct batch state: missing ${path.relative(projectRoot, filePath)}.\n` +
|
|
105
|
+
" → Force-resume requires surviving batch-meta from batch start (SP-619).\n",
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
try {
|
|
110
|
+
const meta = JSON.parse(fs.readFileSync(filePath, "utf-8"));
|
|
111
|
+
if (!meta || typeof meta !== "object") {
|
|
112
|
+
return {
|
|
113
|
+
ok: false,
|
|
114
|
+
error: "batch_meta_corrupt",
|
|
115
|
+
output: `Cannot reconstruct batch state: corrupt batch-meta at ${filePath}\n`,
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
const wavePlan = normalizeWavePlan(meta.wavePlan);
|
|
119
|
+
if (wavePlan.length < 1 || wavePlan.every((wave) => wave.length < 1)) {
|
|
120
|
+
return {
|
|
121
|
+
ok: false,
|
|
122
|
+
error: "batch_meta_ambiguous",
|
|
123
|
+
output:
|
|
124
|
+
`Cannot reconstruct batch state: batch-meta for ${resolvedBatchId} has empty wavePlan.\n` +
|
|
125
|
+
" → Prefer a clear error over guessing the wave topology.\n",
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
return {
|
|
129
|
+
ok: true,
|
|
130
|
+
meta: { ...meta, batchId: String(meta.batchId ?? resolvedBatchId), wavePlan },
|
|
131
|
+
path: filePath,
|
|
132
|
+
};
|
|
133
|
+
} catch (err) {
|
|
134
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
135
|
+
return {
|
|
136
|
+
ok: false,
|
|
137
|
+
error: "batch_meta_corrupt",
|
|
138
|
+
output: `Cannot reconstruct batch state: failed to parse batch-meta (${message})\n`,
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* @param {string} projectRoot
|
|
145
|
+
* @param {object} meta
|
|
146
|
+
*/
|
|
147
|
+
function seedStateFromBatchMeta(projectRoot, meta) {
|
|
148
|
+
const batchId = String(meta.batchId);
|
|
149
|
+
const wavePlan = normalizeWavePlan(meta.wavePlan);
|
|
150
|
+
const taskIds = [...new Set(wavePlan.flat())];
|
|
151
|
+
const tasks = taskIds.map((taskId) => ({
|
|
152
|
+
taskId,
|
|
153
|
+
laneNumber: 1,
|
|
154
|
+
status: "pending",
|
|
155
|
+
taskFolder: null,
|
|
156
|
+
startedAt: null,
|
|
157
|
+
endedAt: null,
|
|
158
|
+
doneFileFound: false,
|
|
159
|
+
exitReason: null,
|
|
160
|
+
}));
|
|
161
|
+
const lanes = [
|
|
162
|
+
{
|
|
163
|
+
laneNumber: 1,
|
|
164
|
+
laneId: "lane-1",
|
|
165
|
+
worktreePath: laneWorktreePath(projectRoot, batchId, 1),
|
|
166
|
+
branch: laneTaskBranch(batchId, 1),
|
|
167
|
+
taskIds: [...taskIds],
|
|
168
|
+
lastHeartbeatAt: null,
|
|
169
|
+
},
|
|
170
|
+
];
|
|
171
|
+
const state = createInitialBatchState({
|
|
172
|
+
batchId,
|
|
173
|
+
baseBranch: String(meta.baseBranch ?? "main") || "main",
|
|
174
|
+
orchBranch: String(meta.orchBranch ?? "") || `orch/spine-${batchId}`,
|
|
175
|
+
wavePlan,
|
|
176
|
+
tasks,
|
|
177
|
+
lanes,
|
|
178
|
+
});
|
|
179
|
+
state.phase = "failed";
|
|
180
|
+
state.totalWaves = Number(meta.totalWaves ?? wavePlan.length) || wavePlan.length;
|
|
181
|
+
return state;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* @param {string} projectRoot
|
|
186
|
+
* @param {string} batchId
|
|
187
|
+
*/
|
|
188
|
+
function loadArchiveSeed(projectRoot, batchId) {
|
|
189
|
+
const archivePath = archiveBatchStatePath(projectRoot, batchId);
|
|
190
|
+
if (!fs.existsSync(archivePath)) return null;
|
|
191
|
+
try {
|
|
192
|
+
const raw = JSON.parse(fs.readFileSync(archivePath, "utf-8"));
|
|
193
|
+
return raw && typeof raw === "object" ? raw : null;
|
|
194
|
+
} catch {
|
|
195
|
+
return null;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* Rebuild usable batch-state from batch-meta + journal/archive artifacts (FR-REL240-04).
|
|
201
|
+
*
|
|
202
|
+
* @param {string} projectRoot
|
|
203
|
+
* @param {{ batchId?: string }} [options]
|
|
204
|
+
*/
|
|
205
|
+
export function reconstructBatchStateFromRuntime(projectRoot, options = {}) {
|
|
206
|
+
const requestedBatchId = String(options.batchId ?? "").trim();
|
|
207
|
+
let batchId = requestedBatchId;
|
|
208
|
+
|
|
209
|
+
if (!batchId) {
|
|
210
|
+
const candidates = listBatchMetaArtifacts(projectRoot);
|
|
211
|
+
if (candidates.length < 1) {
|
|
212
|
+
return {
|
|
213
|
+
ok: false,
|
|
214
|
+
exitCode: 1,
|
|
215
|
+
error: "batch_meta_missing",
|
|
216
|
+
output:
|
|
217
|
+
"Cannot reconstruct batch state: no .spine/runtime/*/batch-meta.json found.\n" +
|
|
218
|
+
" → Live batch-state is missing/corrupt and survival meta is absent.\n",
|
|
219
|
+
};
|
|
220
|
+
}
|
|
221
|
+
if (candidates.length > 1) {
|
|
222
|
+
const ids = candidates.map((entry) => entry.batchId).join(", ");
|
|
223
|
+
return {
|
|
224
|
+
ok: false,
|
|
225
|
+
exitCode: 1,
|
|
226
|
+
error: "batch_meta_ambiguous",
|
|
227
|
+
output:
|
|
228
|
+
`Cannot reconstruct batch state: multiple batch-meta artifacts (${ids}).\n` +
|
|
229
|
+
" → Refuse to guess which wave/batch to resume; remove stale runtime dirs or specify the batch.\n",
|
|
230
|
+
};
|
|
231
|
+
}
|
|
232
|
+
batchId = candidates[0].batchId;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
const loadedMeta = loadBatchMetaRuntimeArtifact(projectRoot, batchId);
|
|
236
|
+
if (!loadedMeta.ok) {
|
|
237
|
+
return { ...loadedMeta, exitCode: 1 };
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
const meta = loadedMeta.meta;
|
|
241
|
+
const archiveSeed = loadArchiveSeed(projectRoot, batchId);
|
|
242
|
+
const metaSeed = seedStateFromBatchMeta(projectRoot, meta);
|
|
243
|
+
const seed =
|
|
244
|
+
archiveSeed && normalizeWavePlan(archiveSeed.wavePlan).length > 0
|
|
245
|
+
? {
|
|
246
|
+
...metaSeed,
|
|
247
|
+
...archiveSeed,
|
|
248
|
+
batchId,
|
|
249
|
+
baseBranch: String(archiveSeed.baseBranch ?? metaSeed.baseBranch),
|
|
250
|
+
orchBranch: String(archiveSeed.orchBranch ?? metaSeed.orchBranch),
|
|
251
|
+
wavePlan:
|
|
252
|
+
normalizeWavePlan(archiveSeed.wavePlan).length > 0
|
|
253
|
+
? normalizeWavePlan(archiveSeed.wavePlan)
|
|
254
|
+
: metaSeed.wavePlan,
|
|
255
|
+
}
|
|
256
|
+
: metaSeed;
|
|
257
|
+
|
|
258
|
+
if (wavePlansConflict(meta.wavePlan, seed.wavePlan)) {
|
|
259
|
+
return {
|
|
260
|
+
ok: false,
|
|
261
|
+
exitCode: 1,
|
|
262
|
+
error: "batch_meta_wave_conflict",
|
|
263
|
+
output:
|
|
264
|
+
`Cannot reconstruct batch state: archive wavePlan conflicts with batch-meta for ${batchId}.\n` +
|
|
265
|
+
" → Prefer a clear error over silently resuming the wrong wave.\n",
|
|
266
|
+
};
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
const events = readJournalEvents(projectRoot, batchId);
|
|
270
|
+
const rebuilt = rebuildBatchStateFromJournal(seed, events);
|
|
271
|
+
|
|
272
|
+
if (wavePlansConflict(meta.wavePlan, rebuilt.wavePlan)) {
|
|
273
|
+
return {
|
|
274
|
+
ok: false,
|
|
275
|
+
exitCode: 1,
|
|
276
|
+
error: "batch_meta_wave_conflict",
|
|
277
|
+
output:
|
|
278
|
+
`Cannot reconstruct batch state: journal topology conflicts with batch-meta for ${batchId}.\n` +
|
|
279
|
+
" → Prefer a clear error over silently resuming the wrong wave.\n",
|
|
280
|
+
};
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
// Meta is the survival source of truth for wave topology.
|
|
284
|
+
rebuilt.wavePlan = normalizeWavePlan(meta.wavePlan);
|
|
285
|
+
rebuilt.totalWaves = Number(meta.totalWaves ?? rebuilt.wavePlan.length) || rebuilt.wavePlan.length;
|
|
286
|
+
rebuilt.batchId = batchId;
|
|
287
|
+
rebuilt.baseBranch = String(meta.baseBranch || rebuilt.baseBranch || "main");
|
|
288
|
+
rebuilt.orchBranch = String(meta.orchBranch || rebuilt.orchBranch || `orch/spine-${batchId}`);
|
|
289
|
+
|
|
290
|
+
const phase = String(rebuilt.phase ?? "");
|
|
291
|
+
if (phase === "completed") {
|
|
292
|
+
return {
|
|
293
|
+
ok: false,
|
|
294
|
+
exitCode: 1,
|
|
295
|
+
error: "batch_already_completed",
|
|
296
|
+
output:
|
|
297
|
+
`Cannot reconstruct batch ${batchId} for resume: journal indicates completed.\n` +
|
|
298
|
+
" → Use spine batch dismiss / integrate / salvage as appropriate.\n",
|
|
299
|
+
};
|
|
300
|
+
}
|
|
301
|
+
// Force-resume reconstruct must land in a force-resumable phase. Journal rebuild
|
|
302
|
+
// often yields "running" without a live engine; remap so --force can continue.
|
|
303
|
+
if (phase === "aborted" || phase === "planning" || phase === "running") {
|
|
304
|
+
rebuilt.phase = "failed";
|
|
305
|
+
rebuilt.endedAt = null;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
clearBatchEnginePid(rebuilt);
|
|
309
|
+
const priorResilience =
|
|
310
|
+
rebuilt.resilience && typeof rebuilt.resilience === "object" ? rebuilt.resilience : {};
|
|
311
|
+
rebuilt.resilience = {
|
|
312
|
+
resumeForced: true,
|
|
313
|
+
retryCountByScope: priorResilience.retryCountByScope ?? {},
|
|
314
|
+
lastFailureClass: priorResilience.lastFailureClass ?? null,
|
|
315
|
+
repairHistory: Array.isArray(priorResilience.repairHistory)
|
|
316
|
+
? priorResilience.repairHistory
|
|
317
|
+
: [],
|
|
318
|
+
...(priorResilience.forceMergedWaves
|
|
319
|
+
? { forceMergedWaves: priorResilience.forceMergedWaves }
|
|
320
|
+
: {}),
|
|
321
|
+
};
|
|
322
|
+
rebuilt.currentWaveIndex = findResumableWave(rebuilt, computePendingTasks(rebuilt));
|
|
323
|
+
rebuilt.updatedAt = Date.now();
|
|
324
|
+
|
|
325
|
+
return {
|
|
326
|
+
ok: true,
|
|
327
|
+
exitCode: 0,
|
|
328
|
+
batchId,
|
|
329
|
+
state: rebuilt,
|
|
330
|
+
meta,
|
|
331
|
+
metaPath: loadedMeta.path,
|
|
332
|
+
reconstructedFrom: events.length > 0 ? "batch-meta+journal" : "batch-meta",
|
|
333
|
+
};
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
/**
|
|
337
|
+
* When force-resume sees missing/corrupt live state, reconstruct and persist it.
|
|
338
|
+
*
|
|
339
|
+
* @param {string} projectRoot
|
|
340
|
+
* @param {{ force?: boolean, batchId?: string }} [options]
|
|
341
|
+
*/
|
|
342
|
+
export function ensureForceResumeBatchState(projectRoot, options = {}) {
|
|
343
|
+
const force = Boolean(options.force);
|
|
344
|
+
const loaded = loadSpineBatchState(projectRoot);
|
|
345
|
+
if (loaded.raw) {
|
|
346
|
+
return { ok: true, attempted: false, state: loaded.raw, path: loaded.path };
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
if (!force) {
|
|
350
|
+
return {
|
|
351
|
+
ok: false,
|
|
352
|
+
attempted: false,
|
|
353
|
+
exitCode: 1,
|
|
354
|
+
error: "no_active_batch",
|
|
355
|
+
output: "No active pi-spine batch.\n",
|
|
356
|
+
};
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
const reason = loaded.parseError ? "corrupt" : "missing";
|
|
360
|
+
const reconstructed = reconstructBatchStateFromRuntime(projectRoot, {
|
|
361
|
+
batchId: options.batchId,
|
|
362
|
+
});
|
|
363
|
+
if (!reconstructed.ok) {
|
|
364
|
+
return {
|
|
365
|
+
...reconstructed,
|
|
366
|
+
attempted: true,
|
|
367
|
+
reason,
|
|
368
|
+
};
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
if (!reconstructed.state) {
|
|
372
|
+
return {
|
|
373
|
+
ok: false,
|
|
374
|
+
attempted: true,
|
|
375
|
+
exitCode: 1,
|
|
376
|
+
error: "batch_meta_reconstruct_failed",
|
|
377
|
+
reason,
|
|
378
|
+
output: "Failed to reconstruct batch state from batch-meta.\n",
|
|
379
|
+
};
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
const saved = saveSpineBatchState(projectRoot, reconstructed.state, { bypassWriteGuard: true });
|
|
383
|
+
appendJournalEvent(projectRoot, reconstructed.batchId, "batch.state_reconstructed", {
|
|
384
|
+
reason,
|
|
385
|
+
source: reconstructed.reconstructedFrom ?? "batch-meta",
|
|
386
|
+
resumeForced: true,
|
|
387
|
+
});
|
|
388
|
+
return {
|
|
389
|
+
ok: true,
|
|
390
|
+
attempted: true,
|
|
391
|
+
reason,
|
|
392
|
+
batchId: reconstructed.batchId,
|
|
393
|
+
state: saved,
|
|
394
|
+
path: loadSpineBatchState(projectRoot).path,
|
|
395
|
+
reconstructedFrom: reconstructed.reconstructedFrom,
|
|
396
|
+
output:
|
|
397
|
+
`Reconstructed batch-state for ${reconstructed.batchId} from ${reconstructed.reconstructedFrom} ` +
|
|
398
|
+
`(live state was ${reason}).\n`,
|
|
399
|
+
};
|
|
400
|
+
}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
// @ts-nocheck
|
|
2
|
+
/**
|
|
3
|
+
* Survival batch-meta.json artifact (SP-619 / FR-REL240-03 / #126).
|
|
4
|
+
* Persist at batch start; reconstruct helpers live in batch-meta-reconstruct.mjs (SP-620).
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import path from "node:path";
|
|
8
|
+
import { writeJsonAtomic } from "../fs/atomic-write.mjs";
|
|
9
|
+
|
|
10
|
+
export const BATCH_META_SCHEMA_VERSION = 1;
|
|
11
|
+
export const BATCH_META_FILENAME = "batch-meta.json";
|
|
12
|
+
|
|
13
|
+
/** Default orchestration mode when caller does not supply one. */
|
|
14
|
+
export const BATCH_META_DEFAULT_MODE = "batch";
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Absolute path to `.spine/runtime/{batchId}/batch-meta.json`.
|
|
18
|
+
*
|
|
19
|
+
* @param {string} projectRoot
|
|
20
|
+
* @param {string} batchId
|
|
21
|
+
*/
|
|
22
|
+
export function batchMetaPath(projectRoot, batchId) {
|
|
23
|
+
return path.join(projectRoot, ".spine", "runtime", String(batchId), BATCH_META_FILENAME);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Prefer a project-relative tasksRoot for portability across machines.
|
|
28
|
+
*
|
|
29
|
+
* @param {string} projectRoot
|
|
30
|
+
* @param {string} tasksRoot
|
|
31
|
+
*/
|
|
32
|
+
export function normalizeBatchMetaTasksRoot(projectRoot, tasksRoot) {
|
|
33
|
+
const root = String(projectRoot ?? "");
|
|
34
|
+
const resolved = String(tasksRoot ?? "");
|
|
35
|
+
if (!resolved) return resolved;
|
|
36
|
+
if (!path.isAbsolute(resolved)) {
|
|
37
|
+
return resolved.split(path.sep).join("/");
|
|
38
|
+
}
|
|
39
|
+
const relative = path.relative(root, resolved);
|
|
40
|
+
if (!relative || relative.startsWith("..") || path.isAbsolute(relative)) {
|
|
41
|
+
return resolved;
|
|
42
|
+
}
|
|
43
|
+
return relative.split(path.sep).join("/");
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Persist minimal wave topology for abort-survival / force-resume reconstruct.
|
|
48
|
+
*
|
|
49
|
+
* @param {object} params
|
|
50
|
+
* @param {string} params.projectRoot
|
|
51
|
+
* @param {string} params.batchId
|
|
52
|
+
* @param {string} params.baseBranch
|
|
53
|
+
* @param {string} params.orchBranch
|
|
54
|
+
* @param {number} params.totalWaves
|
|
55
|
+
* @param {string} [params.mode]
|
|
56
|
+
* @param {string} params.tasksRoot
|
|
57
|
+
* @param {string[][]} params.wavePlan Wave→task mapping (index = wave index)
|
|
58
|
+
* @returns {{ path: string, meta: object }}
|
|
59
|
+
*/
|
|
60
|
+
export function saveBatchMetaRuntimeArtifact({
|
|
61
|
+
projectRoot,
|
|
62
|
+
batchId,
|
|
63
|
+
baseBranch,
|
|
64
|
+
orchBranch,
|
|
65
|
+
totalWaves,
|
|
66
|
+
mode = BATCH_META_DEFAULT_MODE,
|
|
67
|
+
tasksRoot,
|
|
68
|
+
wavePlan,
|
|
69
|
+
}) {
|
|
70
|
+
const resolvedBatchId = String(batchId ?? "").trim();
|
|
71
|
+
if (!resolvedBatchId) {
|
|
72
|
+
throw new Error("saveBatchMetaRuntimeArtifact: batchId is required");
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const waves = Array.isArray(wavePlan)
|
|
76
|
+
? wavePlan.map((wave) => (Array.isArray(wave) ? [...wave] : []))
|
|
77
|
+
: [];
|
|
78
|
+
const waveCount = Number.isFinite(Number(totalWaves)) ? Number(totalWaves) : waves.length;
|
|
79
|
+
|
|
80
|
+
const meta = {
|
|
81
|
+
schemaVersion: BATCH_META_SCHEMA_VERSION,
|
|
82
|
+
batchId: resolvedBatchId,
|
|
83
|
+
baseBranch: String(baseBranch ?? ""),
|
|
84
|
+
orchBranch: String(orchBranch ?? ""),
|
|
85
|
+
totalWaves: waveCount,
|
|
86
|
+
mode: String(mode ?? BATCH_META_DEFAULT_MODE),
|
|
87
|
+
tasksRoot: normalizeBatchMetaTasksRoot(projectRoot, tasksRoot),
|
|
88
|
+
wavePlan: waves,
|
|
89
|
+
createdAt: Date.now(),
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
const filePath = batchMetaPath(projectRoot, resolvedBatchId);
|
|
93
|
+
writeJsonAtomic(filePath, meta);
|
|
94
|
+
return { path: filePath, meta };
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Persist batch-meta from initial batch-state (attached + detached share startBatch).
|
|
99
|
+
*
|
|
100
|
+
* @param {string} projectRoot
|
|
101
|
+
* @param {{ batchId?: string, baseBranch?: string, orchBranch?: string, totalWaves?: number, wavePlan?: string[][] }} state
|
|
102
|
+
* @param {string} tasksRoot
|
|
103
|
+
*/
|
|
104
|
+
export function persistBatchMetaFromStartState(projectRoot, state, tasksRoot) {
|
|
105
|
+
return saveBatchMetaRuntimeArtifact({
|
|
106
|
+
projectRoot,
|
|
107
|
+
batchId: String(state?.batchId ?? ""),
|
|
108
|
+
baseBranch: String(state?.baseBranch ?? ""),
|
|
109
|
+
orchBranch: String(state?.orchBranch ?? ""),
|
|
110
|
+
totalWaves: Number(state?.totalWaves ?? 0),
|
|
111
|
+
mode: BATCH_META_DEFAULT_MODE,
|
|
112
|
+
tasksRoot,
|
|
113
|
+
wavePlan: Array.isArray(state?.wavePlan) ? state.wavePlan : [],
|
|
114
|
+
});
|
|
115
|
+
}
|
package/src/batch/engine.mjs
CHANGED
|
@@ -15,6 +15,7 @@ import { installAttachedEngineShutdownHandlers } from "./attached-engine-handoff
|
|
|
15
15
|
import { finalizeBatchForIntegrate, tryFinalizePostMergeLimbo } from "./post-merge-limbo.mjs";
|
|
16
16
|
import { detectPostMergeLimboForResume } from "./resume-multi-validate.mjs";
|
|
17
17
|
import { appendJournalEvent } from "./journal.mjs";
|
|
18
|
+
import { persistBatchMetaFromStartState } from "./batch-meta.mjs";
|
|
18
19
|
import { recordBatchBaseSnapshotOnStart } from "./lifecycle.mjs";
|
|
19
20
|
import { adoptPauseIfRequested, saveEngineBatchState } from "./pause.mjs";
|
|
20
21
|
import {
|
|
@@ -204,6 +205,7 @@ export async function startBatch({
|
|
|
204
205
|
|
|
205
206
|
recordBatchBaseSnapshotOnStart(projectRoot, state);
|
|
206
207
|
saveEngineBatchState(projectRoot, state);
|
|
208
|
+
persistBatchMetaFromStartState(projectRoot, state, /** @type {string} */ (tasksRoot));
|
|
207
209
|
|
|
208
210
|
try {
|
|
209
211
|
ensureOrchBranch(projectRoot, baseBranch, orchBranch);
|