pi-spine 2.3.1 → 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/bin/spine-batch.mjs +10 -2
- package/package.json +1 -1
- package/skills/create-spine-tasks/SKILL.md +18 -1
- package/skills/spine-autonomous-operator/SKILL.md +3 -1
- package/src/batch/abort.mjs +28 -1
- package/src/batch/batch-meta-reconstruct.mjs +400 -0
- package/src/batch/batch-meta.mjs +115 -0
- package/src/batch/detached-run.mjs +2 -0
- package/src/batch/diagnosis.mjs +15 -2
- 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 +44 -6
- package/src/batch/resume.mjs +12 -5
- package/src/batch/salvage-batch-integrate.mjs +410 -0
- package/src/batch/salvage-batch-list.mjs +48 -15
- package/src/batch/salvage-batch.mjs +7 -406
|
@@ -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
|
+
}
|
|
@@ -265,6 +265,8 @@ export async function resumeBatchDetached({
|
|
|
265
265
|
}
|
|
266
266
|
|
|
267
267
|
prepareDetachedResumeEngineHandoff(projectRoot);
|
|
268
|
+
// SP-613: validateMultiTaskResume already admits pidless terminal-success + pending
|
|
269
|
+
// wave merge (#196); spawn the attached engine argv in detached mode (no --attached).
|
|
268
270
|
const argv = buildAttachedBatchResumeArgv({ force });
|
|
269
271
|
const { enginePid, logPath } = spawnDetachedBatchEngine({ projectRoot, spineBin, argv });
|
|
270
272
|
releaseResumeLock?.();
|
package/src/batch/diagnosis.mjs
CHANGED
|
@@ -161,9 +161,18 @@ export function buildSuggestedCommand(diagnosis, ctx = {}) {
|
|
|
161
161
|
case "completed_manual":
|
|
162
162
|
return "spine batch dismiss";
|
|
163
163
|
case "state_drift":
|
|
164
|
+
// Agent-safe detached recovery (#196 / SP-613) — never suggest --attached.
|
|
165
|
+
if (
|
|
166
|
+
ctx.phase === "running" ||
|
|
167
|
+
ctx.staleEnginePid === true ||
|
|
168
|
+
ctx.engineDead === true ||
|
|
169
|
+
ctx.allTasksTerminalSuccess === true
|
|
170
|
+
) {
|
|
171
|
+
return "spine batch resume --force";
|
|
172
|
+
}
|
|
164
173
|
if (ctx.failedTaskId) {
|
|
165
174
|
const driftStatus = String(ctx.driftTaskStatus ?? "").toLowerCase();
|
|
166
|
-
if (driftStatus === "running"
|
|
175
|
+
if (driftStatus === "running") {
|
|
167
176
|
return "spine batch resume --force";
|
|
168
177
|
}
|
|
169
178
|
return `spine batch retry ${ctx.failedTaskId}`;
|
|
@@ -224,12 +233,16 @@ export function buildSuggestedCommand(diagnosis, ctx = {}) {
|
|
|
224
233
|
case "engine_orphaned": {
|
|
225
234
|
const parentExitCommand = buildEngineOrphanParentExitSuggestedCommand(ctx);
|
|
226
235
|
if (parentExitCommand) return parentExitCommand;
|
|
236
|
+
// Terminal-success / doneInLane heal with dead engine — detached resume (#196 / #163).
|
|
237
|
+
if (ctx.allTasksTerminalSuccess === true) {
|
|
238
|
+
return "spine batch resume --force";
|
|
239
|
+
}
|
|
227
240
|
return ctx.failedTaskId
|
|
228
241
|
? `spine batch retry ${ctx.failedTaskId}`
|
|
229
242
|
: "spine batch resume --attached";
|
|
230
243
|
}
|
|
231
244
|
case "needs_merge":
|
|
232
|
-
return "
|
|
245
|
+
return "spine batch resume --force";
|
|
233
246
|
case "needs_integrate":
|
|
234
247
|
if (ctx.postMergeLimbo && ctx.phase === "running") {
|
|
235
248
|
if (ctx.integrateGateOpen) {
|
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);
|
|
@@ -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";
|