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
|
@@ -0,0 +1,397 @@
|
|
|
1
|
+
// @ts-nocheck
|
|
2
|
+
/**
|
|
3
|
+
* Batch-state drift detect/reconcile and done-marker helpers (FR-STA-01, #170/#190).
|
|
4
|
+
* Structural derivation lives in journal-rebuild-structural.mjs (SP-584).
|
|
5
|
+
* Public API remains journal-rebuild.mjs re-exports (SP-602).
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import fs from "node:fs";
|
|
9
|
+
import path from "node:path";
|
|
10
|
+
import { execFileSync } from "node:child_process";
|
|
11
|
+
import { appendJournalEvent } from "./journal.mjs";
|
|
12
|
+
import { laneTaskBranch } from "./worktree.mjs";
|
|
13
|
+
import { parseReviewVerdict } from "./review-shared.mjs";
|
|
14
|
+
import { journalHasTaskCompleted } from "./resume-common.mjs";
|
|
15
|
+
import { recordTaskSucceeded, saveSpineBatchState } from "./state.mjs";
|
|
16
|
+
import { TASK_LIFECYCLE_EVENT_TYPES } from "./journal-rebuild-structural.mjs";
|
|
17
|
+
|
|
18
|
+
/** @typedef {{ taskId: string, field: string, cached: unknown, rebuilt: unknown }} DriftEntry */
|
|
19
|
+
|
|
20
|
+
const NON_TERMINAL_CACHE_STATUSES = new Set(["pending", "running"]);
|
|
21
|
+
const JOURNAL_TERMINAL_LIFECYCLE = new Set([
|
|
22
|
+
"task.completed",
|
|
23
|
+
"task.skipped",
|
|
24
|
+
"task.skipped_done_on_disk",
|
|
25
|
+
"task.failed",
|
|
26
|
+
"task.prompt_parse_failed",
|
|
27
|
+
]);
|
|
28
|
+
|
|
29
|
+
function lastLifecycleEventForTask(events, taskId) {
|
|
30
|
+
const taskEvents = events.filter(
|
|
31
|
+
(event) => event?.taskId === taskId && TASK_LIFECYCLE_EVENT_TYPES.has(String(event?.type ?? "")),
|
|
32
|
+
);
|
|
33
|
+
return taskEvents.length > 0 ? taskEvents[taskEvents.length - 1] : null;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function hasJournalTerminalLifecycle(lastEvent) {
|
|
37
|
+
if (!lastEvent) return false;
|
|
38
|
+
return JOURNAL_TERMINAL_LIFECYCLE.has(String(lastEvent.type ?? ""));
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* @param {object|null|undefined} classified
|
|
43
|
+
*/
|
|
44
|
+
function classifiedShowsDoneInLaneDrift(classified) {
|
|
45
|
+
if (!classified || typeof classified !== "object") return false;
|
|
46
|
+
if (classified.doneInLane === true) return true;
|
|
47
|
+
return (
|
|
48
|
+
classified.classification === "terminal-success" &&
|
|
49
|
+
!["succeeded", "skipped"].includes(String(classified.status ?? "").toLowerCase())
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function normalizeTaskFolderRel(projectRoot, taskFolder) {
|
|
54
|
+
if (!taskFolder) return null;
|
|
55
|
+
if (path.isAbsolute(taskFolder)) {
|
|
56
|
+
return path.relative(projectRoot, taskFolder).replace(/\\/g, "/");
|
|
57
|
+
}
|
|
58
|
+
return String(taskFolder).replace(/\\/g, "/");
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* True when `.DONE` is committed on the lane task branch (fail-closed / #190).
|
|
63
|
+
*
|
|
64
|
+
* @param {string} projectRoot
|
|
65
|
+
* @param {string} taskBranch
|
|
66
|
+
* @param {string} taskFolderRel
|
|
67
|
+
*/
|
|
68
|
+
export function laneDoneMarkerCommittedOnBranch(projectRoot, taskBranch, taskFolderRel) {
|
|
69
|
+
const folderRel = normalizeTaskFolderRel(projectRoot, taskFolderRel);
|
|
70
|
+
if (!projectRoot || !taskBranch || !folderRel) return false;
|
|
71
|
+
const donePath = `${folderRel.replace(/\/+$/, "")}/.DONE`;
|
|
72
|
+
try {
|
|
73
|
+
execFileSync("git", ["cat-file", "-e", `${taskBranch}:${donePath}`], {
|
|
74
|
+
cwd: projectRoot,
|
|
75
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
76
|
+
timeout: 5000,
|
|
77
|
+
});
|
|
78
|
+
return true;
|
|
79
|
+
} catch {
|
|
80
|
+
return false;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* @param {object} task
|
|
86
|
+
* @param {string} batchId
|
|
87
|
+
* @param {unknown[]} lanes
|
|
88
|
+
* @param {string} projectRoot
|
|
89
|
+
*/
|
|
90
|
+
function laneTaskBranchForTask(task, batchId, lanes) {
|
|
91
|
+
const laneNumber = Number(task.laneNumber ?? 1);
|
|
92
|
+
const lane = Array.isArray(lanes)
|
|
93
|
+
? lanes.find((entry) => Number(entry?.laneNumber) === laneNumber)
|
|
94
|
+
: null;
|
|
95
|
+
if (lane && typeof lane.branch === "string" && lane.branch) {
|
|
96
|
+
return lane.branch;
|
|
97
|
+
}
|
|
98
|
+
return laneTaskBranch(batchId, laneNumber);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Fail-closed gate: filesystem `.DONE` in lane worktree and committed on lane branch.
|
|
103
|
+
*
|
|
104
|
+
* @param {object} params
|
|
105
|
+
* @param {string} params.projectRoot
|
|
106
|
+
* @param {string} params.batchId
|
|
107
|
+
* @param {object} params.task
|
|
108
|
+
* @param {unknown[]} [params.lanes]
|
|
109
|
+
* @param {object|null|undefined} params.classified
|
|
110
|
+
*/
|
|
111
|
+
export function laneDoneMarkerReadyForPromote({ projectRoot, batchId, task, lanes = [], classified }) {
|
|
112
|
+
if (classified?.doneInLane !== true) return false;
|
|
113
|
+
const taskFolder = normalizeTaskFolderRel(projectRoot, task?.taskFolder);
|
|
114
|
+
if (!taskFolder) return false;
|
|
115
|
+
const taskBranch = laneTaskBranchForTask(task, batchId, lanes);
|
|
116
|
+
return laneDoneMarkerCommittedOnBranch(projectRoot, taskBranch, taskFolder);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Detect review.started events that have no matching review.completed for the same
|
|
121
|
+
* taskId + reviewType. These represent engine crashes mid-review where the artifact
|
|
122
|
+
* may exist on disk but the completion was never journaled.
|
|
123
|
+
*
|
|
124
|
+
* @param {object[]} events
|
|
125
|
+
* @returns {object[]} orphaned review.started events
|
|
126
|
+
*/
|
|
127
|
+
export function detectOrphanedReviewStarted(events) {
|
|
128
|
+
/** @type {Map<string, object>} keyed by "taskId:reviewType" */
|
|
129
|
+
const latestStarted = new Map();
|
|
130
|
+
/** @type {Set<string>} */
|
|
131
|
+
const completedKeys = new Set();
|
|
132
|
+
|
|
133
|
+
for (const event of events) {
|
|
134
|
+
const type = String(event?.type ?? "");
|
|
135
|
+
const payload = event.payload && typeof event.payload === "object" ? event.payload : {};
|
|
136
|
+
const taskId = event.taskId ?? payload.taskId;
|
|
137
|
+
if (!taskId) continue;
|
|
138
|
+
const reviewType = payload.reviewType;
|
|
139
|
+
if (!reviewType) continue;
|
|
140
|
+
const key = `${taskId}:${reviewType}`;
|
|
141
|
+
|
|
142
|
+
if (type === "review.started") {
|
|
143
|
+
latestStarted.set(key, event);
|
|
144
|
+
} else if (type === "review.completed") {
|
|
145
|
+
completedKeys.add(key);
|
|
146
|
+
latestStarted.delete(key);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
const orphaned = [];
|
|
151
|
+
for (const [key, event] of latestStarted) {
|
|
152
|
+
if (!completedKeys.has(key)) {
|
|
153
|
+
orphaned.push(event);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
return orphaned;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/** @type {ReadonlySet<string>} Verdicts that indicate a successful review outcome. */
|
|
160
|
+
const APPROVED_VERDICTS = new Set(["APPROVE", "PASS"]);
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Reconcile orphaned review.started events by checking for on-disk artifacts with
|
|
164
|
+
* valid verdicts and synthesizing the missing review.completed + task.completed events.
|
|
165
|
+
* Called at resume time to self-heal after engine crashes mid-review (SP-484 / #131).
|
|
166
|
+
*
|
|
167
|
+
* @param {object} params
|
|
168
|
+
* @param {string} params.projectRoot
|
|
169
|
+
* @param {string} params.batchId
|
|
170
|
+
* @param {object[]} params.events - journal events
|
|
171
|
+
* @returns {{ synthesized: Array<{ taskId: string, reviewType: string, verdict: string, artifactPath: string }> }}
|
|
172
|
+
*/
|
|
173
|
+
export function reconcileOrphanedReviewEvents({ projectRoot, batchId, events }) {
|
|
174
|
+
const orphaned = detectOrphanedReviewStarted(events);
|
|
175
|
+
/** @type {Array<{ taskId: string, reviewType: string, verdict: string, artifactPath: string }>} */
|
|
176
|
+
const synthesized = [];
|
|
177
|
+
|
|
178
|
+
for (const event of orphaned) {
|
|
179
|
+
const payload = event.payload && typeof event.payload === "object" ? event.payload : {};
|
|
180
|
+
const artifactPath = typeof payload.artifactPath === "string" ? payload.artifactPath : "";
|
|
181
|
+
const taskId = event.taskId ?? payload.taskId;
|
|
182
|
+
if (!taskId || !artifactPath) continue;
|
|
183
|
+
if (!fs.existsSync(artifactPath)) continue;
|
|
184
|
+
|
|
185
|
+
let content;
|
|
186
|
+
try {
|
|
187
|
+
content = fs.readFileSync(artifactPath, "utf-8");
|
|
188
|
+
} catch {
|
|
189
|
+
continue;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
const reviewType = payload.reviewType ?? "code";
|
|
193
|
+
const { verdict, feedback } = parseReviewVerdict(content, { reviewType });
|
|
194
|
+
if (!verdict || !APPROVED_VERDICTS.has(verdict)) continue;
|
|
195
|
+
|
|
196
|
+
appendJournalEvent(projectRoot, batchId, "review.completed", {
|
|
197
|
+
taskId,
|
|
198
|
+
laneNumber: payload.laneNumber ?? null,
|
|
199
|
+
correlationId: payload.correlationId ?? null,
|
|
200
|
+
stepNumber: payload.stepNumber ?? null,
|
|
201
|
+
reviewType,
|
|
202
|
+
reviewLevel: payload.reviewLevel ?? null,
|
|
203
|
+
verdict,
|
|
204
|
+
feedback: feedback ?? "",
|
|
205
|
+
artifactPath,
|
|
206
|
+
synthesized: true,
|
|
207
|
+
synthesizeReason: "orphaned_review_crash_recovery",
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
appendJournalEvent(projectRoot, batchId, "task.completed", {
|
|
211
|
+
taskId,
|
|
212
|
+
laneNumber: payload.laneNumber ?? null,
|
|
213
|
+
correlationId: payload.correlationId ?? null,
|
|
214
|
+
synthesized: true,
|
|
215
|
+
synthesizeReason: "orphaned_review_crash_recovery",
|
|
216
|
+
exitReason: "done",
|
|
217
|
+
doneFileFound: true,
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
synthesized.push({ taskId, reviewType, verdict, artifactPath });
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
return { synthesized };
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
export function detectBatchStateDrift(cachedState, rebuiltState, events = [], classifiedTasks = null) {
|
|
227
|
+
/** @type {DriftEntry[]} */
|
|
228
|
+
const entries = [];
|
|
229
|
+
const rebuiltById = new Map((rebuiltState?.tasks ?? []).map((task) => [String(task?.taskId), task]));
|
|
230
|
+
const classifiedById = Array.isArray(classifiedTasks)
|
|
231
|
+
? new Map(classifiedTasks.map((task) => [String(task?.taskId), task]))
|
|
232
|
+
: null;
|
|
233
|
+
|
|
234
|
+
for (const cached of cachedState?.tasks ?? []) {
|
|
235
|
+
if (!cached?.taskId) continue;
|
|
236
|
+
const taskId = String(cached.taskId);
|
|
237
|
+
const rebuilt = rebuiltById.get(taskId);
|
|
238
|
+
if (!rebuilt) continue;
|
|
239
|
+
const classified = classifiedById?.get(taskId) ?? null;
|
|
240
|
+
const cachedStatus = String(cached.status ?? "").toLowerCase();
|
|
241
|
+
const lastEvent = lastLifecycleEventForTask(events, taskId);
|
|
242
|
+
const lastType = lastEvent ? String(lastEvent.type ?? "") : "";
|
|
243
|
+
|
|
244
|
+
if (
|
|
245
|
+
NON_TERMINAL_CACHE_STATUSES.has(cachedStatus) &&
|
|
246
|
+
classifiedShowsDoneInLaneDrift(classified) &&
|
|
247
|
+
!hasJournalTerminalLifecycle(lastEvent)
|
|
248
|
+
) {
|
|
249
|
+
entries.push({
|
|
250
|
+
taskId,
|
|
251
|
+
field: "doneInLane",
|
|
252
|
+
cached: cached.status,
|
|
253
|
+
rebuilt: classified?.classification ?? "terminal-success",
|
|
254
|
+
});
|
|
255
|
+
continue;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
if (!lastEvent) continue;
|
|
259
|
+
|
|
260
|
+
if (lastType === "task.completed" && cached.status !== "succeeded") {
|
|
261
|
+
entries.push({ taskId, field: "status", cached: cached.status, rebuilt: rebuilt.status });
|
|
262
|
+
continue;
|
|
263
|
+
}
|
|
264
|
+
if (lastType === "task.retry_requested" && cached.status === "failed" && rebuilt.status === "pending") {
|
|
265
|
+
entries.push({ taskId, field: "status", cached: cached.status, rebuilt: rebuilt.status });
|
|
266
|
+
continue;
|
|
267
|
+
}
|
|
268
|
+
if (lastType === "task.started" && cached.status === "failed") continue;
|
|
269
|
+
if ((lastType === "task.failed" || lastType === "task.prompt_parse_failed") && cached.status === rebuilt.status) continue;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
return { drifted: entries.length > 0, entries };
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/**
|
|
276
|
+
* Journal evidence that lane work finished and review/contract passed (FR-STA-01 / #170).
|
|
277
|
+
*
|
|
278
|
+
* @param {object[]} events
|
|
279
|
+
* @param {string} taskId
|
|
280
|
+
*/
|
|
281
|
+
export function journalShowsDoneInLaneTerminalArtifacts(events, taskId) {
|
|
282
|
+
let hasApprovedReview = false;
|
|
283
|
+
let hasLaneCompleted = false;
|
|
284
|
+
let hasContractVerified = false;
|
|
285
|
+
|
|
286
|
+
for (const event of events ?? []) {
|
|
287
|
+
const type = String(event?.type ?? "");
|
|
288
|
+
const payload = event.payload && typeof event.payload === "object" ? event.payload : {};
|
|
289
|
+
const eventTaskId = event.taskId ?? payload.taskId;
|
|
290
|
+
if (eventTaskId !== taskId) continue;
|
|
291
|
+
|
|
292
|
+
if (type === "review.completed" && APPROVED_VERDICTS.has(String(payload.verdict ?? ""))) {
|
|
293
|
+
hasApprovedReview = true;
|
|
294
|
+
}
|
|
295
|
+
if (type === "task.verdict_recorded" && APPROVED_VERDICTS.has(String(payload.verdict ?? ""))) {
|
|
296
|
+
hasApprovedReview = true;
|
|
297
|
+
}
|
|
298
|
+
if (type === "lane.completed") {
|
|
299
|
+
hasLaneCompleted = true;
|
|
300
|
+
}
|
|
301
|
+
if (type === "contract.verified" && payload.ok === true) {
|
|
302
|
+
hasContractVerified = true;
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
return hasLaneCompleted && (hasApprovedReview || hasContractVerified);
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
/**
|
|
310
|
+
* Promote batch-state tasks when lane on-disk truth and journal show terminal success
|
|
311
|
+
* but cache still shows pending/running (idempotent; FR-STA-01 / SP-512).
|
|
312
|
+
*
|
|
313
|
+
* @param {object} params
|
|
314
|
+
* @param {string} params.projectRoot
|
|
315
|
+
* @param {object} params.state
|
|
316
|
+
* @param {object[]} [params.classifiedTasks]
|
|
317
|
+
* @param {object[]} [params.journalEvents]
|
|
318
|
+
* @param {{ drifted?: boolean, entries?: Array<{ taskId: string, field: string, rebuilt?: unknown }> }} [params.drift]
|
|
319
|
+
* @returns {{ reconciled: boolean, taskIds: string[] }}
|
|
320
|
+
*/
|
|
321
|
+
export function reconcileBatchStateDrift({
|
|
322
|
+
projectRoot,
|
|
323
|
+
state,
|
|
324
|
+
classifiedTasks = [],
|
|
325
|
+
journalEvents = [],
|
|
326
|
+
drift = null,
|
|
327
|
+
}) {
|
|
328
|
+
if (!state || typeof state !== "object" || !drift?.drifted || !Array.isArray(drift.entries)) {
|
|
329
|
+
return { reconciled: false, taskIds: [] };
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
const batchId = String(state.batchId ?? "");
|
|
333
|
+
if (!batchId) {
|
|
334
|
+
return { reconciled: false, taskIds: [] };
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
const classifiedById = new Map(
|
|
338
|
+
(classifiedTasks ?? []).map((task) => [String(task?.taskId ?? ""), task]),
|
|
339
|
+
);
|
|
340
|
+
/** @type {string[]} */
|
|
341
|
+
const reconciledTaskIds = [];
|
|
342
|
+
let changed = false;
|
|
343
|
+
|
|
344
|
+
for (const entry of drift.entries) {
|
|
345
|
+
const taskId = String(entry?.taskId ?? "");
|
|
346
|
+
if (!taskId || taskId === "*") continue;
|
|
347
|
+
|
|
348
|
+
const task = (state.tasks ?? []).find((row) => row?.taskId === taskId);
|
|
349
|
+
if (!task) continue;
|
|
350
|
+
|
|
351
|
+
if (entry.field === "status" && String(entry.rebuilt ?? "") === "succeeded") {
|
|
352
|
+
if (String(task.status ?? "") === "succeeded") continue;
|
|
353
|
+
if (!recordTaskSucceeded(state, taskId, { doneFileFound: true, exitReason: "done" })) continue;
|
|
354
|
+
reconciledTaskIds.push(taskId);
|
|
355
|
+
changed = true;
|
|
356
|
+
continue;
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
if (entry.field !== "doneInLane") continue;
|
|
360
|
+
|
|
361
|
+
const classified = classifiedById.get(taskId);
|
|
362
|
+
if (!classifiedShowsDoneInLaneDrift(classified)) continue;
|
|
363
|
+
if (
|
|
364
|
+
!laneDoneMarkerReadyForPromote({
|
|
365
|
+
projectRoot,
|
|
366
|
+
batchId,
|
|
367
|
+
task,
|
|
368
|
+
lanes: state.lanes ?? [],
|
|
369
|
+
classified,
|
|
370
|
+
})
|
|
371
|
+
) {
|
|
372
|
+
continue;
|
|
373
|
+
}
|
|
374
|
+
if (journalHasTaskCompleted(journalEvents, taskId)) continue;
|
|
375
|
+
if (!journalShowsDoneInLaneTerminalArtifacts(journalEvents, taskId)) continue;
|
|
376
|
+
if (String(task.status ?? "") === "succeeded") continue;
|
|
377
|
+
|
|
378
|
+
if (!recordTaskSucceeded(state, taskId, { doneFileFound: true, exitReason: "done" })) continue;
|
|
379
|
+
|
|
380
|
+
appendJournalEvent(projectRoot, batchId, "task.completed", {
|
|
381
|
+
taskId,
|
|
382
|
+
laneNumber: task.laneNumber ?? null,
|
|
383
|
+
reconciled: true,
|
|
384
|
+
reconcileReason: "done_in_lane_terminal",
|
|
385
|
+
skippedDoneOnDisk: true,
|
|
386
|
+
});
|
|
387
|
+
|
|
388
|
+
reconciledTaskIds.push(taskId);
|
|
389
|
+
changed = true;
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
if (changed) {
|
|
393
|
+
saveSpineBatchState(projectRoot, state);
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
return { reconciled: changed, taskIds: reconciledTaskIds };
|
|
397
|
+
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// @ts-nocheck
|
|
2
2
|
/**
|
|
3
3
|
* Structural batch-state derivation from journal events (FR-REL-01/02, FR-SHIP-10).
|
|
4
|
-
* Drift reconcile lives in journal-rebuild.mjs
|
|
4
|
+
* Drift reconcile lives in journal-rebuild-drift.mjs (SP-602); public API via journal-rebuild.mjs.
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
7
|
import { readJournalEvents, STRUCTURAL_JOURNAL_EVENT_TYPES } from "./journal.mjs";
|