pi-spine 2.2.0 → 2.3.1
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 +15 -46
- 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/diagnosis.mjs +35 -4
- package/src/batch/engine-lanes/orch-sync.mjs +126 -0
- package/src/batch/engine-lanes.mjs +69 -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 +495 -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 +382 -0
- package/src/batch/worker-host.mjs +56 -548
- package/src/batch/worker-spawn.mjs +326 -0
- package/src/batch/worktree.mjs +68 -0
- package/src/config/preflight/integrate-plan.mjs +6 -1
- package/src/config/preflight/loc-capstone.mjs +274 -0
- package/src/config/spine-preflight-lib.mjs +14 -0
- package/src/planner/index.mjs +14 -3
- package/src/process/terminate-tree.mjs +154 -0
package/src/batch/engine.mjs
CHANGED
|
@@ -45,6 +45,7 @@ import {
|
|
|
45
45
|
skipTaskDoneOnDisk,
|
|
46
46
|
transitionPhase,
|
|
47
47
|
} from "./engine-lanes.mjs";
|
|
48
|
+
import { rejectNestedBatchStart } from "./batch-guards.mjs";
|
|
48
49
|
|
|
49
50
|
/**
|
|
50
51
|
* @param {string} batchId
|
|
@@ -71,55 +72,13 @@ export {
|
|
|
71
72
|
resolveBatchStartScope,
|
|
72
73
|
} from "./engine-scope.mjs";
|
|
73
74
|
export { loadTaskFileScopePaths, mergeLaneToOrch } from "./engine-lanes.mjs";
|
|
75
|
+
export { detectNestedWorkerContext } from "./batch-guards.mjs";
|
|
74
76
|
|
|
75
|
-
/**
|
|
76
|
-
|
|
77
|
-
* terminal transitions can assign endedAt/lastError without any casts.
|
|
78
|
-
*
|
|
79
|
-
* @typedef {Omit<ReturnType<typeof createInitialBatchState>, "endedAt" | "lastError"> & {
|
|
80
|
-
* endedAt: number | null;
|
|
81
|
-
* lastError: string | null;
|
|
82
|
-
* }} SpineBatchState
|
|
83
|
-
*/
|
|
84
|
-
|
|
85
|
-
const WORKTREE_SPINE_PATTERN = /[/\\]\.worktrees[/\\]spine-/;
|
|
86
|
-
|
|
87
|
-
/**
|
|
88
|
-
* Detect whether the current process is running inside a spine worker context.
|
|
89
|
-
* Returns a human-readable reason string if nested, or null if safe to proceed.
|
|
90
|
-
*
|
|
91
|
-
* Two guards: (1) SPINE_IS_WORKER env set by worker-host, and
|
|
92
|
-
* (2) projectRoot inside a .worktrees/spine-* directory (catches cases where
|
|
93
|
-
* the env was not inherited but the target is clearly a lane worktree).
|
|
94
|
-
*
|
|
95
|
-
* @param {string} projectRoot — the directory where the batch would run
|
|
96
|
-
* @returns {string | null}
|
|
97
|
-
*/
|
|
98
|
-
export function detectNestedWorkerContext(projectRoot) {
|
|
99
|
-
if (process.env.SPINE_IS_WORKER === "1") {
|
|
100
|
-
return "SPINE_IS_WORKER=1 is set (running inside a worker process)";
|
|
101
|
-
}
|
|
102
|
-
const parentBatchId = process.env.SPINE_PARENT_BATCH_ID ?? process.env.SPINE_BATCH_ID;
|
|
103
|
-
if (parentBatchId && WORKTREE_SPINE_PATTERN.test(projectRoot)) {
|
|
104
|
-
return (
|
|
105
|
-
`parent batch ${parentBatchId} is active and projectRoot is inside a ` +
|
|
106
|
-
`.worktrees/spine-* lane directory`
|
|
107
|
-
);
|
|
108
|
-
}
|
|
109
|
-
if (WORKTREE_SPINE_PATTERN.test(projectRoot)) {
|
|
110
|
-
return "projectRoot is inside a .worktrees/spine-* lane directory";
|
|
111
|
-
}
|
|
112
|
-
return null;
|
|
113
|
-
}
|
|
77
|
+
/** Widened so terminal transitions can assign endedAt/lastError without casts. */
|
|
78
|
+
/** @typedef {Omit<ReturnType<typeof createInitialBatchState>, "endedAt" | "lastError"> & { endedAt: number | null; lastError: string | null }} SpineBatchState */
|
|
114
79
|
|
|
115
80
|
/**
|
|
116
|
-
* @param {
|
|
117
|
-
* @param {string} options.projectRoot
|
|
118
|
-
* @param {string} [options.scope]
|
|
119
|
-
* @param {boolean} [options.dryRun]
|
|
120
|
-
* @param {boolean} [options.skipPreflight]
|
|
121
|
-
* @param {boolean} [options.forceSuperseded]
|
|
122
|
-
* @param {number|null} [options.waveFilter]
|
|
81
|
+
* @param {{ projectRoot: string, scope?: string, dryRun?: boolean, skipPreflight?: boolean, forceSuperseded?: boolean, waveFilter?: number|null }} options
|
|
123
82
|
*/
|
|
124
83
|
export async function startBatch({
|
|
125
84
|
projectRoot,
|
|
@@ -129,27 +88,9 @@ export async function startBatch({
|
|
|
129
88
|
forceSuperseded = false,
|
|
130
89
|
waveFilter = null,
|
|
131
90
|
}) {
|
|
132
|
-
const
|
|
133
|
-
if (
|
|
134
|
-
|
|
135
|
-
try {
|
|
136
|
-
appendJournalEvent(projectRoot, parentBatchId, "engine.nested_spawn_blocked", {
|
|
137
|
-
projectRoot,
|
|
138
|
-
parentBatchId,
|
|
139
|
-
reason: nestedReason,
|
|
140
|
-
});
|
|
141
|
-
} catch {
|
|
142
|
-
// Journal may not be writable from a worker worktree; best-effort.
|
|
143
|
-
}
|
|
144
|
-
return {
|
|
145
|
-
ok: false,
|
|
146
|
-
exitCode: 1,
|
|
147
|
-
error: "nested_batch_spawn_blocked",
|
|
148
|
-
output:
|
|
149
|
-
`Nested batch start blocked: ${nestedReason}. ` +
|
|
150
|
-
`Workers must not spawn batch engines. ` +
|
|
151
|
-
`Parent batch: ${parentBatchId}, projectRoot: ${projectRoot}\n`,
|
|
152
|
-
};
|
|
91
|
+
const nestedBlock = rejectNestedBatchStart(projectRoot);
|
|
92
|
+
if (nestedBlock) {
|
|
93
|
+
return nestedBlock;
|
|
153
94
|
}
|
|
154
95
|
|
|
155
96
|
if (!skipPreflight) {
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
// @ts-nocheck
|
|
2
|
+
/**
|
|
3
|
+
* Git helpers for orch→base integrate recovery (FR-SHIP-02 / #116).
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { gitExec } from "./git-exec.mjs";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Best-effort checkout restore after integrate failure.
|
|
10
|
+
* Leaves the operator on the current branch when checkout fails (dirty tree / lock).
|
|
11
|
+
*
|
|
12
|
+
* @param {string} projectRoot
|
|
13
|
+
* @param {string} branch Target branch (typically previous HEAD or baseBranch)
|
|
14
|
+
*/
|
|
15
|
+
export function tryRestoreBranch(projectRoot, branch) {
|
|
16
|
+
try {
|
|
17
|
+
gitExec(projectRoot, ["checkout", branch], { projectRoot });
|
|
18
|
+
} catch {
|
|
19
|
+
// Leave operator on current branch for manual recovery.
|
|
20
|
+
}
|
|
21
|
+
}
|
package/src/batch/integrate.mjs
CHANGED
|
@@ -13,6 +13,8 @@ import {
|
|
|
13
13
|
} from "./rules-manifest-drift.mjs";
|
|
14
14
|
import { assertOrchIntegratable } from "./integrate-assert.mjs";
|
|
15
15
|
export { assertOrchIntegratable } from "./integrate-assert.mjs";
|
|
16
|
+
import { tryRestoreBranch } from "./integrate-git.mjs";
|
|
17
|
+
export { tryRestoreBranch } from "./integrate-git.mjs";
|
|
16
18
|
import { checkIntegrateGate } from "./gate.mjs";
|
|
17
19
|
import { appendJournalEvent } from "./journal.mjs";
|
|
18
20
|
import { countCommitsAhead } from "./lane-commit.mjs";
|
|
@@ -379,11 +381,7 @@ export function integrateOrchToBase(ctx) {
|
|
|
379
381
|
error: message.slice(0, 500),
|
|
380
382
|
conflict,
|
|
381
383
|
});
|
|
382
|
-
|
|
383
|
-
git(projectRoot, ["checkout", previous || baseBranch]);
|
|
384
|
-
} catch {
|
|
385
|
-
// leave operator on current branch for manual recovery
|
|
386
|
-
}
|
|
384
|
+
tryRestoreBranch(projectRoot, previous || baseBranch);
|
|
387
385
|
return {
|
|
388
386
|
ok: false,
|
|
389
387
|
exitCode: 1,
|
|
@@ -430,11 +428,7 @@ export function integrateOrchToBase(ctx) {
|
|
|
430
428
|
mergeCommit,
|
|
431
429
|
});
|
|
432
430
|
|
|
433
|
-
|
|
434
|
-
git(projectRoot, ["checkout", previous || baseBranch]);
|
|
435
|
-
} catch {
|
|
436
|
-
// Leave operator on current branch for manual recovery.
|
|
437
|
-
}
|
|
431
|
+
tryRestoreBranch(projectRoot, previous || baseBranch);
|
|
438
432
|
|
|
439
433
|
return {
|
|
440
434
|
ok: false,
|
|
@@ -484,11 +478,7 @@ export function integrateOrchToBase(ctx) {
|
|
|
484
478
|
conflict,
|
|
485
479
|
});
|
|
486
480
|
|
|
487
|
-
|
|
488
|
-
git(projectRoot, ["checkout", previous || baseBranch]);
|
|
489
|
-
} catch {
|
|
490
|
-
// leave operator on current branch for manual recovery
|
|
491
|
-
}
|
|
481
|
+
tryRestoreBranch(projectRoot, previous || baseBranch);
|
|
492
482
|
|
|
493
483
|
return {
|
|
494
484
|
ok: false,
|
|
@@ -0,0 +1,352 @@
|
|
|
1
|
+
// @ts-nocheck
|
|
2
|
+
/**
|
|
3
|
+
* Structural batch-state derivation from journal events (FR-REL-01/02, FR-SHIP-10).
|
|
4
|
+
* Drift reconcile lives in journal-rebuild.mjs until SP-602.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { readJournalEvents, STRUCTURAL_JOURNAL_EVENT_TYPES } from "./journal.mjs";
|
|
8
|
+
import {
|
|
9
|
+
clearTaskFailureMetadata,
|
|
10
|
+
createInitialBatchState,
|
|
11
|
+
recomputeTaskCounters,
|
|
12
|
+
updateSegmentForTask,
|
|
13
|
+
} from "./state.mjs";
|
|
14
|
+
|
|
15
|
+
/** @type {ReadonlySet<string>} */
|
|
16
|
+
export const TASK_LIFECYCLE_EVENT_TYPES = new Set([
|
|
17
|
+
"task.started",
|
|
18
|
+
"task.completed",
|
|
19
|
+
"task.failed",
|
|
20
|
+
"task.skipped",
|
|
21
|
+
"task.skipped_done_on_disk",
|
|
22
|
+
"task.retry_requested",
|
|
23
|
+
"task.prompt_parse_failed",
|
|
24
|
+
]);
|
|
25
|
+
|
|
26
|
+
/** @type {ReadonlySet<string>} */
|
|
27
|
+
export const BATCH_PHASE_EVENT_TYPES = new Set([
|
|
28
|
+
"batch.started",
|
|
29
|
+
"batch.resumed",
|
|
30
|
+
"batch.paused",
|
|
31
|
+
"batch.completed",
|
|
32
|
+
"batch.failed",
|
|
33
|
+
"batch.aborted",
|
|
34
|
+
"batch.dismissed",
|
|
35
|
+
]);
|
|
36
|
+
|
|
37
|
+
export function readJournalTimeline(events) {
|
|
38
|
+
return events.filter(
|
|
39
|
+
(event) =>
|
|
40
|
+
TASK_LIFECYCLE_EVENT_TYPES.has(String(event?.type ?? "")) ||
|
|
41
|
+
BATCH_PHASE_EVENT_TYPES.has(String(event?.type ?? "")),
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function readJournalTimelineFromDisk(projectRoot, batchId) {
|
|
46
|
+
return readJournalTimeline(readJournalEvents(projectRoot, batchId));
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function payloadOf(event) {
|
|
50
|
+
return event.payload && typeof event.payload === "object" ? event.payload : {};
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function tsOf(event) {
|
|
54
|
+
return Date.parse(String(event.timestamp ?? "")) || Date.now();
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function taskIdsOf(value) {
|
|
58
|
+
if (typeof value === "string") return value.split(/\s+/).map((s) => s.trim()).filter(Boolean);
|
|
59
|
+
if (!Array.isArray(value)) return [];
|
|
60
|
+
return value.map((entry) => String(entry ?? "").trim()).filter(Boolean);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function taskStub(map, taskId, hints = {}) {
|
|
64
|
+
const row = map.get(taskId) ?? { taskId };
|
|
65
|
+
if (hints.laneNumber != null) row.laneNumber = Number(hints.laneNumber) || row.laneNumber || 1;
|
|
66
|
+
if (typeof hints.taskFolder === "string" && hints.taskFolder) row.taskFolder = hints.taskFolder;
|
|
67
|
+
map.set(taskId, row);
|
|
68
|
+
return row;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function laneStub(map, laneNumber, hints = {}) {
|
|
72
|
+
const row = map.get(laneNumber) ?? { laneNumber, laneId: hints.laneId ?? `lane-${laneNumber}`, taskIds: [] };
|
|
73
|
+
row.laneId = hints.laneId ?? row.laneId;
|
|
74
|
+
if (typeof hints.worktreePath === "string") row.worktreePath = hints.worktreePath;
|
|
75
|
+
if (typeof hints.taskBranch === "string") row.branch = hints.taskBranch;
|
|
76
|
+
if (!Array.isArray(row.taskIds)) row.taskIds = [];
|
|
77
|
+
map.set(laneNumber, row);
|
|
78
|
+
return row;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function finalizeTasks(tasksById, seedTasks) {
|
|
82
|
+
const seedById = new Map((seedTasks ?? []).filter((t) => t?.taskId).map((t) => [String(t.taskId), t]));
|
|
83
|
+
return [...new Set([...tasksById.keys(), ...seedById.keys()])].map((taskId) => {
|
|
84
|
+
const derived = tasksById.get(taskId) ?? { taskId };
|
|
85
|
+
const seed = seedById.get(taskId);
|
|
86
|
+
return {
|
|
87
|
+
taskId,
|
|
88
|
+
laneNumber: Number(derived.laneNumber ?? seed?.laneNumber ?? 1) || 1,
|
|
89
|
+
status: "pending",
|
|
90
|
+
taskFolder: derived.taskFolder ?? seed?.taskFolder ?? null,
|
|
91
|
+
sessionName: seed?.sessionName,
|
|
92
|
+
startedAt: null,
|
|
93
|
+
endedAt: null,
|
|
94
|
+
doneFileFound: false,
|
|
95
|
+
exitReason: null,
|
|
96
|
+
};
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function finalizeLanes(lanesByNumber, seedLanes, tasks) {
|
|
101
|
+
const seedByNumber = new Map((seedLanes ?? []).filter((l) => l?.laneNumber != null).map((l) => [Number(l.laneNumber), l]));
|
|
102
|
+
const laneNumbers = new Set([...lanesByNumber.keys(), ...seedByNumber.keys()]);
|
|
103
|
+
for (const task of tasks) if (task?.laneNumber != null) laneNumbers.add(Number(task.laneNumber));
|
|
104
|
+
if (laneNumbers.size === 0) laneNumbers.add(1);
|
|
105
|
+
return [...laneNumbers].sort((a, b) => a - b).map((laneNumber) => {
|
|
106
|
+
const derived = lanesByNumber.get(laneNumber) ?? { laneNumber, laneId: `lane-${laneNumber}` };
|
|
107
|
+
const seed = seedByNumber.get(laneNumber);
|
|
108
|
+
const taskIds = [...new Set([
|
|
109
|
+
...(derived.taskIds ?? []),
|
|
110
|
+
...(seed?.taskIds ?? []),
|
|
111
|
+
...tasks.filter((t) => Number(t.laneNumber) === laneNumber).map((t) => t.taskId),
|
|
112
|
+
])];
|
|
113
|
+
return {
|
|
114
|
+
laneNumber,
|
|
115
|
+
laneId: derived.laneId ?? seed?.laneId ?? `lane-${laneNumber}`,
|
|
116
|
+
laneSessionId: seed?.laneSessionId,
|
|
117
|
+
worktreePath: derived.worktreePath ?? seed?.worktreePath ?? null,
|
|
118
|
+
branch: derived.branch ?? seed?.branch ?? null,
|
|
119
|
+
taskIds,
|
|
120
|
+
lastHeartbeatAt: seed?.lastHeartbeatAt ?? null,
|
|
121
|
+
status: seed?.status ?? "pending",
|
|
122
|
+
};
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** Derive structural batch-state fields from journal; cache seed fills gaps only (FR-SHIP-10). */
|
|
127
|
+
export function deriveStructuralBatchStateFromJournal(events, seedState = null) {
|
|
128
|
+
const lanesByNumber = new Map();
|
|
129
|
+
const tasksById = new Map();
|
|
130
|
+
const wavePlanRows = [];
|
|
131
|
+
const mergeResults = [];
|
|
132
|
+
let batchId = String(seedState?.batchId ?? "").trim();
|
|
133
|
+
let baseBranch = null;
|
|
134
|
+
let orchBranch = null;
|
|
135
|
+
let startedAt = null;
|
|
136
|
+
|
|
137
|
+
for (const event of events) {
|
|
138
|
+
const type = String(event?.type ?? "");
|
|
139
|
+
if (!STRUCTURAL_JOURNAL_EVENT_TYPES.has(type)) continue;
|
|
140
|
+
if (!batchId && typeof event.batchId === "string") batchId = event.batchId;
|
|
141
|
+
const payload = payloadOf(event);
|
|
142
|
+
const timestamp = tsOf(event);
|
|
143
|
+
const taskId = typeof event.taskId === "string" ? event.taskId : typeof payload.taskId === "string" ? payload.taskId : null;
|
|
144
|
+
|
|
145
|
+
if (type === "batch.started" || type === "batch.resumed") {
|
|
146
|
+
if (startedAt == null) startedAt = timestamp;
|
|
147
|
+
if (typeof payload.baseBranch === "string" && payload.baseBranch) baseBranch = payload.baseBranch;
|
|
148
|
+
if (typeof payload.orchBranch === "string" && payload.orchBranch) orchBranch = payload.orchBranch;
|
|
149
|
+
if (Array.isArray(payload.wavePlan)) {
|
|
150
|
+
for (const wave of payload.wavePlan) if (Array.isArray(wave)) wavePlanRows.push([...wave]);
|
|
151
|
+
}
|
|
152
|
+
for (const scopedTaskId of taskIdsOf(payload.scope ?? payload.taskIds)) taskStub(tasksById, scopedTaskId, payload);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
if (type === "lane.provisioned") {
|
|
156
|
+
const laneNumber = Number(payload.laneNumber);
|
|
157
|
+
if (Number.isFinite(laneNumber)) laneStub(lanesByNumber, laneNumber, payload);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
if (type === "lane.tasks_serialized") {
|
|
161
|
+
const laneNumber = Number(payload.laneNumber);
|
|
162
|
+
const waveIndex = Number(payload.waveIndex ?? wavePlanRows.length);
|
|
163
|
+
const serializedTaskIds = taskIdsOf(payload.taskIds);
|
|
164
|
+
if (serializedTaskIds.length > 0) {
|
|
165
|
+
while (wavePlanRows.length <= waveIndex) wavePlanRows.push([]);
|
|
166
|
+
wavePlanRows[waveIndex] = [...new Set([...wavePlanRows[waveIndex], ...serializedTaskIds])];
|
|
167
|
+
}
|
|
168
|
+
if (Number.isFinite(laneNumber)) {
|
|
169
|
+
const lane = laneStub(lanesByNumber, laneNumber, payload);
|
|
170
|
+
for (const id of serializedTaskIds) {
|
|
171
|
+
if (!lane.taskIds.includes(id)) lane.taskIds.push(id);
|
|
172
|
+
taskStub(tasksById, id, { laneNumber, laneId: lane.laneId });
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
if (type === "task.started" && taskId) {
|
|
178
|
+
const laneNumber = Number(payload.laneNumber ?? event.laneNumber);
|
|
179
|
+
taskStub(tasksById, taskId, { laneNumber, laneId: payload.laneId ?? event.laneId });
|
|
180
|
+
if (Number.isFinite(laneNumber)) {
|
|
181
|
+
const lane = laneStub(lanesByNumber, laneNumber, { laneId: payload.laneId ?? event.laneId });
|
|
182
|
+
if (!lane.taskIds.includes(taskId)) lane.taskIds.push(taskId);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
if (type === "task.skipped_done_on_disk" && taskId) taskStub(tasksById, taskId, payload);
|
|
187
|
+
|
|
188
|
+
if (type === "batch.merge_started") {
|
|
189
|
+
if (typeof payload.baseBranch === "string" && payload.baseBranch) baseBranch = payload.baseBranch;
|
|
190
|
+
if (typeof payload.orchBranch === "string" && payload.orchBranch) orchBranch = payload.orchBranch;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
if (type === "batch.merge_completed" || type === "lane.committed") {
|
|
194
|
+
const mergeCommit = payload.mergeCommit ?? payload.commitSha;
|
|
195
|
+
if (mergeCommit) {
|
|
196
|
+
mergeResults.push({ mergeCommit, taskId: taskId ?? payload.taskId ?? null, laneNumber: payload.laneNumber ?? null });
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
const resolvedBatchId = batchId || String(seedState?.batchId ?? "").trim();
|
|
202
|
+
const tasks = finalizeTasks(tasksById, seedState?.tasks);
|
|
203
|
+
let wavePlan = wavePlanRows.filter((wave) => wave.length > 0);
|
|
204
|
+
if (wavePlan.length === 0 && Array.isArray(seedState?.wavePlan) && seedState.wavePlan.length > 0) {
|
|
205
|
+
wavePlan = seedState.wavePlan;
|
|
206
|
+
} else if (wavePlan.length === 0 && tasks.length > 0) {
|
|
207
|
+
wavePlan = [tasks.map((task) => task.taskId)];
|
|
208
|
+
} else if (wavePlan.length === 0) {
|
|
209
|
+
wavePlan = [[]];
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
const structural = createInitialBatchState({
|
|
213
|
+
batchId: resolvedBatchId,
|
|
214
|
+
baseBranch: baseBranch ?? seedState?.baseBranch ?? "main",
|
|
215
|
+
orchBranch: orchBranch ?? seedState?.orchBranch ?? (resolvedBatchId ? `orch/spine-${resolvedBatchId}` : "main"),
|
|
216
|
+
wavePlan,
|
|
217
|
+
tasks,
|
|
218
|
+
lanes: finalizeLanes(lanesByNumber, seedState?.lanes, tasks),
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
structural.startedAt = startedAt ?? seedState?.startedAt ?? Date.now();
|
|
222
|
+
structural.mergeResults = mergeResults.length > 0 ? mergeResults : Array.isArray(seedState?.mergeResults) ? seedState.mergeResults : [];
|
|
223
|
+
structural.currentWaveIndex = Number(seedState?.currentWaveIndex ?? 0) || 0;
|
|
224
|
+
structural.blockedTasks = Number(seedState?.blockedTasks ?? 0) || 0;
|
|
225
|
+
structural.blockedTaskIds = Array.isArray(seedState?.blockedTaskIds) ? [...seedState.blockedTaskIds] : [];
|
|
226
|
+
structural.resilience = seedState?.resilience && typeof seedState.resilience === "object" ? structuredClone(seedState.resilience) : structural.resilience;
|
|
227
|
+
structural.lastError = seedState?.lastError ?? null;
|
|
228
|
+
return structural;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
function mergeSeedOnlyFields(rebuilt, seedState) {
|
|
232
|
+
if (!seedState || typeof seedState !== "object") return;
|
|
233
|
+
rebuilt.currentWaveIndex = Number(seedState.currentWaveIndex ?? rebuilt.currentWaveIndex ?? 0) || 0;
|
|
234
|
+
rebuilt.blockedTasks = Number(seedState.blockedTasks ?? rebuilt.blockedTasks ?? 0) || 0;
|
|
235
|
+
rebuilt.blockedTaskIds = Array.isArray(seedState.blockedTaskIds) ? [...seedState.blockedTaskIds] : rebuilt.blockedTaskIds;
|
|
236
|
+
if (seedState.resilience && typeof seedState.resilience === "object") rebuilt.resilience = structuredClone(seedState.resilience);
|
|
237
|
+
if (rebuilt.mergeResults.length === 0 && Array.isArray(seedState.mergeResults)) rebuilt.mergeResults = structuredClone(seedState.mergeResults);
|
|
238
|
+
|
|
239
|
+
const seedTasks = new Map((seedState.tasks ?? []).filter((t) => t?.taskId).map((t) => [String(t.taskId), t]));
|
|
240
|
+
for (const task of rebuilt.tasks ?? []) {
|
|
241
|
+
const seedTask = seedTasks.get(String(task.taskId));
|
|
242
|
+
if (!seedTask) continue;
|
|
243
|
+
if (!task.taskFolder && seedTask.taskFolder) task.taskFolder = seedTask.taskFolder;
|
|
244
|
+
if (!task.sessionName && seedTask.sessionName) task.sessionName = seedTask.sessionName;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
const seedLanes = new Map((seedState.lanes ?? []).filter((l) => l?.laneNumber != null).map((l) => [Number(l.laneNumber), l]));
|
|
248
|
+
for (const lane of rebuilt.lanes ?? []) {
|
|
249
|
+
const seedLane = seedLanes.get(Number(lane.laneNumber));
|
|
250
|
+
if (!seedLane) continue;
|
|
251
|
+
if (!lane.worktreePath && seedLane.worktreePath) lane.worktreePath = seedLane.worktreePath;
|
|
252
|
+
if (!lane.branch && seedLane.branch) lane.branch = seedLane.branch;
|
|
253
|
+
if (!lane.laneSessionId && seedLane.laneSessionId) lane.laneSessionId = seedLane.laneSessionId;
|
|
254
|
+
}
|
|
255
|
+
if (!rebuilt.segments?.length && Array.isArray(seedState.segments) && seedState.segments.length > 0) {
|
|
256
|
+
rebuilt.segments = structuredClone(seedState.segments);
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
function applyJournalEvent(state, event) {
|
|
261
|
+
const type = String(event?.type ?? "");
|
|
262
|
+
const taskId = typeof event.taskId === "string" ? event.taskId : null;
|
|
263
|
+
const payload = payloadOf(event);
|
|
264
|
+
const timestamp = tsOf(event);
|
|
265
|
+
if (!taskId && !BATCH_PHASE_EVENT_TYPES.has(type)) return;
|
|
266
|
+
const task = taskId ? (state.tasks ?? []).find((entry) => entry?.taskId === taskId) : null;
|
|
267
|
+
|
|
268
|
+
switch (type) {
|
|
269
|
+
case "batch.started":
|
|
270
|
+
case "batch.resumed":
|
|
271
|
+
state.phase = "running";
|
|
272
|
+
if (state.startedAt == null) state.startedAt = Number(payload.startedAt) || timestamp;
|
|
273
|
+
break;
|
|
274
|
+
case "task.started":
|
|
275
|
+
if (!task) return;
|
|
276
|
+
task.status = "running";
|
|
277
|
+
task.startedAt = Number(payload.startedAt) || timestamp;
|
|
278
|
+
task.endedAt = null;
|
|
279
|
+
clearTaskFailureMetadata(task);
|
|
280
|
+
updateSegmentForTask(state, taskId, "running");
|
|
281
|
+
break;
|
|
282
|
+
case "task.completed":
|
|
283
|
+
if (!task) return;
|
|
284
|
+
task.status = "succeeded";
|
|
285
|
+
task.endedAt = Number(payload.endedAt) || timestamp;
|
|
286
|
+
task.doneFileFound = payload.doneFileFound !== false;
|
|
287
|
+
task.exitReason = String(payload.exitReason ?? "done");
|
|
288
|
+
if ("classification" in task) delete task.classification;
|
|
289
|
+
updateSegmentForTask(state, taskId, "succeeded");
|
|
290
|
+
break;
|
|
291
|
+
case "task.failed":
|
|
292
|
+
case "task.prompt_parse_failed":
|
|
293
|
+
if (!task) return;
|
|
294
|
+
task.status = "failed";
|
|
295
|
+
task.endedAt = Number(payload.endedAt) || timestamp;
|
|
296
|
+
task.doneFileFound = false;
|
|
297
|
+
task.exitReason = String(payload.exitReason ?? payload.reason ?? type);
|
|
298
|
+
if (payload.classification) task.classification = String(payload.classification);
|
|
299
|
+
updateSegmentForTask(state, taskId, "failed");
|
|
300
|
+
break;
|
|
301
|
+
case "task.skipped":
|
|
302
|
+
case "task.skipped_done_on_disk":
|
|
303
|
+
if (!task) return;
|
|
304
|
+
task.status = "skipped";
|
|
305
|
+
task.endedAt = Number(payload.endedAt) || timestamp;
|
|
306
|
+
task.doneFileFound = type === "task.skipped_done_on_disk";
|
|
307
|
+
task.exitReason = String(payload.exitReason ?? "skipped");
|
|
308
|
+
updateSegmentForTask(state, taskId, "skipped");
|
|
309
|
+
break;
|
|
310
|
+
case "task.retry_requested":
|
|
311
|
+
if (!task) return;
|
|
312
|
+
clearTaskFailureMetadata(task);
|
|
313
|
+
task.status = "pending";
|
|
314
|
+
task.startedAt = null;
|
|
315
|
+
task.endedAt = null;
|
|
316
|
+
task.doneFileFound = false;
|
|
317
|
+
updateSegmentForTask(state, taskId, "pending");
|
|
318
|
+
break;
|
|
319
|
+
case "batch.paused":
|
|
320
|
+
state.phase = "paused";
|
|
321
|
+
break;
|
|
322
|
+
case "batch.completed":
|
|
323
|
+
state.phase = "completed";
|
|
324
|
+
state.endedAt = Number(payload.endedAt) || timestamp;
|
|
325
|
+
break;
|
|
326
|
+
case "batch.failed":
|
|
327
|
+
state.phase = "failed";
|
|
328
|
+
state.endedAt = Number(payload.endedAt) || timestamp;
|
|
329
|
+
break;
|
|
330
|
+
case "batch.aborted":
|
|
331
|
+
case "batch.dismissed":
|
|
332
|
+
state.phase = "aborted";
|
|
333
|
+
state.endedAt = Number(payload.endedAt) || timestamp;
|
|
334
|
+
break;
|
|
335
|
+
default:
|
|
336
|
+
break;
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
/** Apply journal timeline to journal-derived structural state; cache seed fills non-journal fields only. */
|
|
341
|
+
export function rebuildBatchStateFromJournal(seedState, events) {
|
|
342
|
+
const timeline = readJournalTimeline(events);
|
|
343
|
+
const rebuilt = deriveStructuralBatchStateFromJournal(events, seedState ?? null);
|
|
344
|
+
mergeSeedOnlyFields(rebuilt, seedState ?? null);
|
|
345
|
+
for (const event of timeline) applyJournalEvent(rebuilt, event);
|
|
346
|
+
recomputeTaskCounters(rebuilt);
|
|
347
|
+
return rebuilt;
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
export function rebuildBatchStateFromDisk(projectRoot, batchId, seedState) {
|
|
351
|
+
return rebuildBatchStateFromJournal(seedState, readJournalEvents(projectRoot, batchId));
|
|
352
|
+
}
|