pi-spine 1.0.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/LICENSE +21 -0
- package/README.md +563 -0
- package/bin/get-version.mjs +35 -0
- package/bin/spine-batch.mjs +425 -0
- package/bin/spine-cli/batch.mjs +47 -0
- package/bin/spine-cli/gate.mjs +10 -0
- package/bin/spine-cli/integrate.mjs +10 -0
- package/bin/spine-cli/plan.mjs +12 -0
- package/bin/spine-cli/shared.mjs +73 -0
- package/bin/spine-cli/status.mjs +18 -0
- package/bin/spine-cli/verify.mjs +199 -0
- package/bin/spine-config.mjs +212 -0
- package/bin/spine-dashboard.mjs +178 -0
- package/bin/spine-deps.mjs +69 -0
- package/bin/spine-doctor.mjs +468 -0
- package/bin/spine-gate.mjs +137 -0
- package/bin/spine-init.mjs +398 -0
- package/bin/spine-integrate.mjs +77 -0
- package/bin/spine-journal.mjs +198 -0
- package/bin/spine-migrate-from-taskplane.mjs +136 -0
- package/bin/spine-plan.mjs +83 -0
- package/bin/spine-preflight.mjs +700 -0
- package/bin/spine-report-progress.d.mts +28 -0
- package/bin/spine-report-progress.mjs +126 -0
- package/bin/spine-review-step.d.mts +24 -0
- package/bin/spine-review-step.mjs +124 -0
- package/bin/spine-rules.mjs +35 -0
- package/bin/spine-settings.mjs +186 -0
- package/bin/spine-state.mjs +110 -0
- package/bin/spine-status.mjs +79 -0
- package/bin/spine-tasks.mjs +293 -0
- package/bin/spine-worker-runner.mjs +314 -0
- package/bin/spine.mjs +404 -0
- package/extensions/spine/settings-slash.ts +20 -0
- package/extensions/spine/slash-commands.ts +806 -0
- package/extensions/spine/worker-tools.ts +279 -0
- package/extensions/spine-orchestrator.ts +12 -0
- package/package.json +72 -0
- package/skills/.gitkeep +0 -0
- package/skills/create-spine-tasks/SKILL.md +375 -0
- package/skills/create-spine-tasks/references/context-template.md +25 -0
- package/skills/create-spine-tasks/references/contract-template.md +98 -0
- package/skills/create-spine-tasks/references/explore-template.md +63 -0
- package/skills/create-spine-tasks/references/prompt-template.md +276 -0
- package/src/batch/abort.mjs +272 -0
- package/src/batch/agent-session-worker.mjs +297 -0
- package/src/batch/batch-state-io.mjs +53 -0
- package/src/batch/contract-verify.mjs +310 -0
- package/src/batch/detached-start.mjs +778 -0
- package/src/batch/diagnosis.mjs +378 -0
- package/src/batch/engine-lanes/merge.mjs +632 -0
- package/src/batch/engine-lanes/queue.mjs +208 -0
- package/src/batch/engine-lanes/review.mjs +970 -0
- package/src/batch/engine-lanes.mjs +388 -0
- package/src/batch/engine-scope.mjs +251 -0
- package/src/batch/engine.mjs +445 -0
- package/src/batch/evidence-command.mjs +157 -0
- package/src/batch/evidence.mjs +203 -0
- package/src/batch/gate.mjs +359 -0
- package/src/batch/git-commit-env.mjs +46 -0
- package/src/batch/git-exec.mjs +26 -0
- package/src/batch/heartbeat.mjs +430 -0
- package/src/batch/integrate.mjs +414 -0
- package/src/batch/journal-rebuild.mjs +388 -0
- package/src/batch/journal.mjs +373 -0
- package/src/batch/lane-commit.mjs +153 -0
- package/src/batch/lifecycle.mjs +364 -0
- package/src/batch/metrics.mjs +265 -0
- package/src/batch/orphan-detect.mjs +210 -0
- package/src/batch/post-merge-limbo.mjs +80 -0
- package/src/batch/postmortem.mjs +296 -0
- package/src/batch/readers/spine-state.mjs +95 -0
- package/src/batch/readers/taskplane-state.mjs +136 -0
- package/src/batch/reconcile.mjs +729 -0
- package/src/batch/resume-common.mjs +104 -0
- package/src/batch/resume-engine.mjs +66 -0
- package/src/batch/resume-multi-lanes.mjs +479 -0
- package/src/batch/resume-multi-validate.mjs +187 -0
- package/src/batch/resume-multi.mjs +254 -0
- package/src/batch/resume.mjs +475 -0
- package/src/batch/retry.mjs +257 -0
- package/src/batch/review-scope.mjs +136 -0
- package/src/batch/review.mjs +1045 -0
- package/src/batch/salvage.mjs +332 -0
- package/src/batch/state.mjs +680 -0
- package/src/batch/task-stall-budget.mjs +88 -0
- package/src/batch/worker-host.mjs +683 -0
- package/src/batch/worker-output.mjs +253 -0
- package/src/batch/worker-prompt.mjs +84 -0
- package/src/batch/worktree.mjs +290 -0
- package/src/cli/deps.mjs +141 -0
- package/src/cli/handoff.mjs +364 -0
- package/src/cli/rules.mjs +429 -0
- package/src/cli/settings-set.mjs +171 -0
- package/src/cli/settings-show.mjs +140 -0
- package/src/cli/settings-slash.mjs +107 -0
- package/src/config/contract.mjs +56 -0
- package/src/config/cursor-rules/discover.mjs +416 -0
- package/src/config/cursor-rules/index.mjs +39 -0
- package/src/config/cursor-rules/match-globs.mjs +132 -0
- package/src/config/cursor-rules/parse-frontmatter.mjs +222 -0
- package/src/config/cursor-rules/priority.mjs +26 -0
- package/src/config/cursor-rules/profile.mjs +403 -0
- package/src/config/cursor-rules/select.mjs +259 -0
- package/src/config/defaults.mjs +36 -0
- package/src/config/env-overrides.mjs +207 -0
- package/src/config/merge-defaults.mjs +47 -0
- package/src/config/pi-spine-root.mjs +93 -0
- package/src/config/reviewer-context.mjs +243 -0
- package/src/config/settings-fields.mjs +265 -0
- package/src/config/worker-backend.mjs +116 -0
- package/src/config/worker-context.mjs +402 -0
- package/src/config/worker-launch-script.mjs +130 -0
- package/src/config/worktree-setup-hook.mjs +122 -0
- package/src/dashboard/public/dashboard.css +77 -0
- package/src/dashboard/public/dashboard.js +349 -0
- package/src/dashboard/public/index.html +80 -0
- package/src/dashboard/server.mjs +155 -0
- package/src/dashboard/snapshot.mjs +414 -0
- package/src/dashboard/sse.mjs +15 -0
- package/src/dashboard/view.mjs +292 -0
- package/src/doctor/agent-model-inherit.mjs +101 -0
- package/src/doctor/batch-size-guidance.mjs +72 -0
- package/src/doctor/coexistence.mjs +312 -0
- package/src/doctor/rules-manifest.mjs +133 -0
- package/src/doctor/stale-path.mjs +156 -0
- package/src/doctor/stall-config.mjs +108 -0
- package/src/doctor/suggest-max-parallel.mjs +53 -0
- package/src/doctor/task-packet-size.mjs +110 -0
- package/src/doctor/worktree-health.mjs +71 -0
- package/src/migrate/taskplane-config.mjs +116 -0
- package/src/planner/cycles.mjs +63 -0
- package/src/planner/format-plan.mjs +132 -0
- package/src/planner/graph.mjs +99 -0
- package/src/planner/index.mjs +125 -0
- package/src/planner/lanes.mjs +111 -0
- package/src/planner/pending.mjs +39 -0
- package/src/planner/scope.mjs +142 -0
- package/src/process/liveness.mjs +21 -0
- package/src/tasks/packet/discover.mjs +40 -0
- package/src/tasks/packet/index.mjs +81 -0
- package/src/tasks/packet/merge-deps.mjs +79 -0
- package/src/tasks/packet/parse-prompt.mjs +445 -0
- package/src/tasks/packet/parse-status.mjs +118 -0
- package/src/tasks/packet/validate-contract.mjs +115 -0
- package/src/tasks/packet/validate-prompt.mjs +99 -0
- package/src/worker-tools/report-progress.d.mts +18 -0
- package/src/worker-tools/report-progress.mjs +59 -0
- package/src/worker-tools/request-gate.d.mts +17 -0
- package/src/worker-tools/request-gate.mjs +54 -0
- package/templates/agents/reviewer.md +123 -0
- package/templates/agents/supervisor.md +59 -0
- package/templates/agents/worker.md +122 -0
- package/templates/rules-profile.json +27 -0
- package/templates/spine-config.json +64 -0
- package/templates/tasks/CONTEXT.md +30 -0
|
@@ -0,0 +1,388 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Rebuild batch task/segment status from append-only journal (FR-REL-01/02, PRD §11.4).
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { readJournalEvents, STRUCTURAL_JOURNAL_EVENT_TYPES } from "./journal.mjs";
|
|
6
|
+
import {
|
|
7
|
+
clearTaskFailureMetadata,
|
|
8
|
+
createInitialBatchState,
|
|
9
|
+
recomputeTaskCounters,
|
|
10
|
+
updateSegmentForTask,
|
|
11
|
+
} from "./state.mjs";
|
|
12
|
+
|
|
13
|
+
/** @type {ReadonlySet<string>} */
|
|
14
|
+
export const TASK_LIFECYCLE_EVENT_TYPES = new Set([
|
|
15
|
+
"task.started",
|
|
16
|
+
"task.completed",
|
|
17
|
+
"task.failed",
|
|
18
|
+
"task.skipped",
|
|
19
|
+
"task.skipped_done_on_disk",
|
|
20
|
+
"task.retry_requested",
|
|
21
|
+
"task.prompt_parse_failed",
|
|
22
|
+
]);
|
|
23
|
+
|
|
24
|
+
/** @type {ReadonlySet<string>} */
|
|
25
|
+
export const BATCH_PHASE_EVENT_TYPES = new Set([
|
|
26
|
+
"batch.started",
|
|
27
|
+
"batch.resumed",
|
|
28
|
+
"batch.paused",
|
|
29
|
+
"batch.completed",
|
|
30
|
+
"batch.failed",
|
|
31
|
+
"batch.aborted",
|
|
32
|
+
"batch.dismissed",
|
|
33
|
+
]);
|
|
34
|
+
|
|
35
|
+
export function readJournalTimeline(events) {
|
|
36
|
+
return events.filter(
|
|
37
|
+
(event) =>
|
|
38
|
+
TASK_LIFECYCLE_EVENT_TYPES.has(String(event?.type ?? "")) ||
|
|
39
|
+
BATCH_PHASE_EVENT_TYPES.has(String(event?.type ?? "")),
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function readJournalTimelineFromDisk(projectRoot, batchId) {
|
|
44
|
+
return readJournalTimeline(readJournalEvents(projectRoot, batchId));
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function payloadOf(event) {
|
|
48
|
+
return event.payload && typeof event.payload === "object" ? event.payload : {};
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function tsOf(event) {
|
|
52
|
+
return Date.parse(String(event.timestamp ?? "")) || Date.now();
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function taskIdsOf(value) {
|
|
56
|
+
if (typeof value === "string") return value.split(/\s+/).map((s) => s.trim()).filter(Boolean);
|
|
57
|
+
if (!Array.isArray(value)) return [];
|
|
58
|
+
return value.map((entry) => String(entry ?? "").trim()).filter(Boolean);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function taskStub(map, taskId, hints = {}) {
|
|
62
|
+
const row = map.get(taskId) ?? { taskId };
|
|
63
|
+
if (hints.laneNumber != null) row.laneNumber = Number(hints.laneNumber) || row.laneNumber || 1;
|
|
64
|
+
if (typeof hints.taskFolder === "string" && hints.taskFolder) row.taskFolder = hints.taskFolder;
|
|
65
|
+
map.set(taskId, row);
|
|
66
|
+
return row;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function laneStub(map, laneNumber, hints = {}) {
|
|
70
|
+
const row = map.get(laneNumber) ?? { laneNumber, laneId: hints.laneId ?? `lane-${laneNumber}`, taskIds: [] };
|
|
71
|
+
row.laneId = hints.laneId ?? row.laneId;
|
|
72
|
+
if (typeof hints.worktreePath === "string") row.worktreePath = hints.worktreePath;
|
|
73
|
+
if (typeof hints.taskBranch === "string") row.branch = hints.taskBranch;
|
|
74
|
+
if (!Array.isArray(row.taskIds)) row.taskIds = [];
|
|
75
|
+
map.set(laneNumber, row);
|
|
76
|
+
return row;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function finalizeTasks(tasksById, seedTasks) {
|
|
80
|
+
const seedById = new Map((seedTasks ?? []).filter((t) => t?.taskId).map((t) => [String(t.taskId), t]));
|
|
81
|
+
return [...new Set([...tasksById.keys(), ...seedById.keys()])].map((taskId) => {
|
|
82
|
+
const derived = tasksById.get(taskId) ?? { taskId };
|
|
83
|
+
const seed = seedById.get(taskId);
|
|
84
|
+
return {
|
|
85
|
+
taskId,
|
|
86
|
+
laneNumber: Number(derived.laneNumber ?? seed?.laneNumber ?? 1) || 1,
|
|
87
|
+
status: "pending",
|
|
88
|
+
taskFolder: derived.taskFolder ?? seed?.taskFolder ?? null,
|
|
89
|
+
sessionName: seed?.sessionName,
|
|
90
|
+
startedAt: null,
|
|
91
|
+
endedAt: null,
|
|
92
|
+
doneFileFound: false,
|
|
93
|
+
exitReason: null,
|
|
94
|
+
};
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function finalizeLanes(lanesByNumber, seedLanes, tasks) {
|
|
99
|
+
const seedByNumber = new Map((seedLanes ?? []).filter((l) => l?.laneNumber != null).map((l) => [Number(l.laneNumber), l]));
|
|
100
|
+
const laneNumbers = new Set([...lanesByNumber.keys(), ...seedByNumber.keys()]);
|
|
101
|
+
for (const task of tasks) if (task?.laneNumber != null) laneNumbers.add(Number(task.laneNumber));
|
|
102
|
+
if (laneNumbers.size === 0) laneNumbers.add(1);
|
|
103
|
+
return [...laneNumbers].sort((a, b) => a - b).map((laneNumber) => {
|
|
104
|
+
const derived = lanesByNumber.get(laneNumber) ?? { laneNumber, laneId: `lane-${laneNumber}` };
|
|
105
|
+
const seed = seedByNumber.get(laneNumber);
|
|
106
|
+
const taskIds = [...new Set([
|
|
107
|
+
...(derived.taskIds ?? []),
|
|
108
|
+
...(seed?.taskIds ?? []),
|
|
109
|
+
...tasks.filter((t) => Number(t.laneNumber) === laneNumber).map((t) => t.taskId),
|
|
110
|
+
])];
|
|
111
|
+
return {
|
|
112
|
+
laneNumber,
|
|
113
|
+
laneId: derived.laneId ?? seed?.laneId ?? `lane-${laneNumber}`,
|
|
114
|
+
laneSessionId: seed?.laneSessionId,
|
|
115
|
+
worktreePath: derived.worktreePath ?? seed?.worktreePath ?? null,
|
|
116
|
+
branch: derived.branch ?? seed?.branch ?? null,
|
|
117
|
+
taskIds,
|
|
118
|
+
lastHeartbeatAt: seed?.lastHeartbeatAt ?? null,
|
|
119
|
+
status: seed?.status ?? "pending",
|
|
120
|
+
};
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** Derive structural batch-state fields from journal; cache seed fills gaps only (FR-SHIP-10). */
|
|
125
|
+
export function deriveStructuralBatchStateFromJournal(events, seedState = null) {
|
|
126
|
+
const lanesByNumber = new Map();
|
|
127
|
+
const tasksById = new Map();
|
|
128
|
+
const wavePlanRows = [];
|
|
129
|
+
const mergeResults = [];
|
|
130
|
+
let batchId = String(seedState?.batchId ?? "").trim();
|
|
131
|
+
let baseBranch = null;
|
|
132
|
+
let orchBranch = null;
|
|
133
|
+
let startedAt = null;
|
|
134
|
+
|
|
135
|
+
for (const event of events) {
|
|
136
|
+
const type = String(event?.type ?? "");
|
|
137
|
+
if (!STRUCTURAL_JOURNAL_EVENT_TYPES.has(type)) continue;
|
|
138
|
+
if (!batchId && typeof event.batchId === "string") batchId = event.batchId;
|
|
139
|
+
const payload = payloadOf(event);
|
|
140
|
+
const timestamp = tsOf(event);
|
|
141
|
+
const taskId = typeof event.taskId === "string" ? event.taskId : typeof payload.taskId === "string" ? payload.taskId : null;
|
|
142
|
+
|
|
143
|
+
if (type === "batch.started" || type === "batch.resumed") {
|
|
144
|
+
if (startedAt == null) startedAt = timestamp;
|
|
145
|
+
if (typeof payload.baseBranch === "string" && payload.baseBranch) baseBranch = payload.baseBranch;
|
|
146
|
+
if (typeof payload.orchBranch === "string" && payload.orchBranch) orchBranch = payload.orchBranch;
|
|
147
|
+
if (Array.isArray(payload.wavePlan)) {
|
|
148
|
+
for (const wave of payload.wavePlan) if (Array.isArray(wave)) wavePlanRows.push([...wave]);
|
|
149
|
+
}
|
|
150
|
+
for (const scopedTaskId of taskIdsOf(payload.scope ?? payload.taskIds)) taskStub(tasksById, scopedTaskId, payload);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
if (type === "lane.provisioned") {
|
|
154
|
+
const laneNumber = Number(payload.laneNumber);
|
|
155
|
+
if (Number.isFinite(laneNumber)) laneStub(lanesByNumber, laneNumber, payload);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
if (type === "lane.tasks_serialized") {
|
|
159
|
+
const laneNumber = Number(payload.laneNumber);
|
|
160
|
+
const waveIndex = Number(payload.waveIndex ?? wavePlanRows.length);
|
|
161
|
+
const serializedTaskIds = taskIdsOf(payload.taskIds);
|
|
162
|
+
if (serializedTaskIds.length > 0) {
|
|
163
|
+
while (wavePlanRows.length <= waveIndex) wavePlanRows.push([]);
|
|
164
|
+
wavePlanRows[waveIndex] = [...new Set([...wavePlanRows[waveIndex], ...serializedTaskIds])];
|
|
165
|
+
}
|
|
166
|
+
if (Number.isFinite(laneNumber)) {
|
|
167
|
+
const lane = laneStub(lanesByNumber, laneNumber, payload);
|
|
168
|
+
for (const id of serializedTaskIds) {
|
|
169
|
+
if (!lane.taskIds.includes(id)) lane.taskIds.push(id);
|
|
170
|
+
taskStub(tasksById, id, { laneNumber, laneId: lane.laneId });
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
if (type === "task.started" && taskId) {
|
|
176
|
+
const laneNumber = Number(payload.laneNumber ?? event.laneNumber);
|
|
177
|
+
taskStub(tasksById, taskId, { laneNumber, laneId: payload.laneId ?? event.laneId });
|
|
178
|
+
if (Number.isFinite(laneNumber)) {
|
|
179
|
+
const lane = laneStub(lanesByNumber, laneNumber, { laneId: payload.laneId ?? event.laneId });
|
|
180
|
+
if (!lane.taskIds.includes(taskId)) lane.taskIds.push(taskId);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
if (type === "task.skipped_done_on_disk" && taskId) taskStub(tasksById, taskId, payload);
|
|
185
|
+
|
|
186
|
+
if (type === "batch.merge_started") {
|
|
187
|
+
if (typeof payload.baseBranch === "string" && payload.baseBranch) baseBranch = payload.baseBranch;
|
|
188
|
+
if (typeof payload.orchBranch === "string" && payload.orchBranch) orchBranch = payload.orchBranch;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
if (type === "batch.merge_completed" || type === "lane.committed") {
|
|
192
|
+
const mergeCommit = payload.mergeCommit ?? payload.commitSha;
|
|
193
|
+
if (mergeCommit) {
|
|
194
|
+
mergeResults.push({ mergeCommit, taskId: taskId ?? payload.taskId ?? null, laneNumber: payload.laneNumber ?? null });
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
const resolvedBatchId = batchId || String(seedState?.batchId ?? "").trim();
|
|
200
|
+
const tasks = finalizeTasks(tasksById, seedState?.tasks);
|
|
201
|
+
let wavePlan = wavePlanRows.filter((wave) => wave.length > 0);
|
|
202
|
+
if (wavePlan.length === 0 && Array.isArray(seedState?.wavePlan) && seedState.wavePlan.length > 0) {
|
|
203
|
+
wavePlan = seedState.wavePlan;
|
|
204
|
+
} else if (wavePlan.length === 0 && tasks.length > 0) {
|
|
205
|
+
wavePlan = [tasks.map((task) => task.taskId)];
|
|
206
|
+
} else if (wavePlan.length === 0) {
|
|
207
|
+
wavePlan = [[]];
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
const structural = createInitialBatchState({
|
|
211
|
+
batchId: resolvedBatchId,
|
|
212
|
+
baseBranch: baseBranch ?? seedState?.baseBranch ?? "main",
|
|
213
|
+
orchBranch: orchBranch ?? seedState?.orchBranch ?? (resolvedBatchId ? `orch/spine-${resolvedBatchId}` : "main"),
|
|
214
|
+
wavePlan,
|
|
215
|
+
tasks,
|
|
216
|
+
lanes: finalizeLanes(lanesByNumber, seedState?.lanes, tasks),
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
structural.startedAt = startedAt ?? seedState?.startedAt ?? Date.now();
|
|
220
|
+
structural.mergeResults = mergeResults.length > 0 ? mergeResults : Array.isArray(seedState?.mergeResults) ? seedState.mergeResults : [];
|
|
221
|
+
structural.currentWaveIndex = Number(seedState?.currentWaveIndex ?? 0) || 0;
|
|
222
|
+
structural.blockedTasks = Number(seedState?.blockedTasks ?? 0) || 0;
|
|
223
|
+
structural.blockedTaskIds = Array.isArray(seedState?.blockedTaskIds) ? [...seedState.blockedTaskIds] : [];
|
|
224
|
+
structural.resilience = seedState?.resilience && typeof seedState.resilience === "object" ? structuredClone(seedState.resilience) : structural.resilience;
|
|
225
|
+
structural.lastError = seedState?.lastError ?? null;
|
|
226
|
+
return structural;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
function mergeSeedOnlyFields(rebuilt, seedState) {
|
|
230
|
+
if (!seedState || typeof seedState !== "object") return;
|
|
231
|
+
rebuilt.currentWaveIndex = Number(seedState.currentWaveIndex ?? rebuilt.currentWaveIndex ?? 0) || 0;
|
|
232
|
+
rebuilt.blockedTasks = Number(seedState.blockedTasks ?? rebuilt.blockedTasks ?? 0) || 0;
|
|
233
|
+
rebuilt.blockedTaskIds = Array.isArray(seedState.blockedTaskIds) ? [...seedState.blockedTaskIds] : rebuilt.blockedTaskIds;
|
|
234
|
+
if (seedState.resilience && typeof seedState.resilience === "object") rebuilt.resilience = structuredClone(seedState.resilience);
|
|
235
|
+
if (rebuilt.mergeResults.length === 0 && Array.isArray(seedState.mergeResults)) rebuilt.mergeResults = structuredClone(seedState.mergeResults);
|
|
236
|
+
|
|
237
|
+
const seedTasks = new Map((seedState.tasks ?? []).filter((t) => t?.taskId).map((t) => [String(t.taskId), t]));
|
|
238
|
+
for (const task of rebuilt.tasks ?? []) {
|
|
239
|
+
const seedTask = seedTasks.get(String(task.taskId));
|
|
240
|
+
if (!seedTask) continue;
|
|
241
|
+
if (!task.taskFolder && seedTask.taskFolder) task.taskFolder = seedTask.taskFolder;
|
|
242
|
+
if (!task.sessionName && seedTask.sessionName) task.sessionName = seedTask.sessionName;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
const seedLanes = new Map((seedState.lanes ?? []).filter((l) => l?.laneNumber != null).map((l) => [Number(l.laneNumber), l]));
|
|
246
|
+
for (const lane of rebuilt.lanes ?? []) {
|
|
247
|
+
const seedLane = seedLanes.get(Number(lane.laneNumber));
|
|
248
|
+
if (!seedLane) continue;
|
|
249
|
+
if (!lane.worktreePath && seedLane.worktreePath) lane.worktreePath = seedLane.worktreePath;
|
|
250
|
+
if (!lane.branch && seedLane.branch) lane.branch = seedLane.branch;
|
|
251
|
+
if (!lane.laneSessionId && seedLane.laneSessionId) lane.laneSessionId = seedLane.laneSessionId;
|
|
252
|
+
}
|
|
253
|
+
if (!rebuilt.segments?.length && Array.isArray(seedState.segments) && seedState.segments.length > 0) {
|
|
254
|
+
rebuilt.segments = structuredClone(seedState.segments);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
function applyJournalEvent(state, event) {
|
|
259
|
+
const type = String(event?.type ?? "");
|
|
260
|
+
const taskId = typeof event.taskId === "string" ? event.taskId : null;
|
|
261
|
+
const payload = payloadOf(event);
|
|
262
|
+
const timestamp = tsOf(event);
|
|
263
|
+
if (!taskId && !BATCH_PHASE_EVENT_TYPES.has(type)) return;
|
|
264
|
+
const task = taskId ? (state.tasks ?? []).find((entry) => entry?.taskId === taskId) : null;
|
|
265
|
+
|
|
266
|
+
switch (type) {
|
|
267
|
+
case "batch.started":
|
|
268
|
+
case "batch.resumed":
|
|
269
|
+
state.phase = "running";
|
|
270
|
+
if (state.startedAt == null) state.startedAt = Number(payload.startedAt) || timestamp;
|
|
271
|
+
break;
|
|
272
|
+
case "task.started":
|
|
273
|
+
if (!task) return;
|
|
274
|
+
task.status = "running";
|
|
275
|
+
task.startedAt = Number(payload.startedAt) || timestamp;
|
|
276
|
+
task.endedAt = null;
|
|
277
|
+
clearTaskFailureMetadata(task);
|
|
278
|
+
updateSegmentForTask(state, taskId, "running");
|
|
279
|
+
break;
|
|
280
|
+
case "task.completed":
|
|
281
|
+
if (!task) return;
|
|
282
|
+
task.status = "succeeded";
|
|
283
|
+
task.endedAt = Number(payload.endedAt) || timestamp;
|
|
284
|
+
task.doneFileFound = payload.doneFileFound !== false;
|
|
285
|
+
task.exitReason = String(payload.exitReason ?? "done");
|
|
286
|
+
if ("classification" in task) delete task.classification;
|
|
287
|
+
updateSegmentForTask(state, taskId, "succeeded");
|
|
288
|
+
break;
|
|
289
|
+
case "task.failed":
|
|
290
|
+
case "task.prompt_parse_failed":
|
|
291
|
+
if (!task) return;
|
|
292
|
+
task.status = "failed";
|
|
293
|
+
task.endedAt = Number(payload.endedAt) || timestamp;
|
|
294
|
+
task.doneFileFound = false;
|
|
295
|
+
task.exitReason = String(payload.exitReason ?? payload.reason ?? type);
|
|
296
|
+
if (payload.classification) task.classification = String(payload.classification);
|
|
297
|
+
updateSegmentForTask(state, taskId, "failed");
|
|
298
|
+
break;
|
|
299
|
+
case "task.skipped":
|
|
300
|
+
case "task.skipped_done_on_disk":
|
|
301
|
+
if (!task) return;
|
|
302
|
+
task.status = "skipped";
|
|
303
|
+
task.endedAt = Number(payload.endedAt) || timestamp;
|
|
304
|
+
task.doneFileFound = type === "task.skipped_done_on_disk";
|
|
305
|
+
task.exitReason = String(payload.exitReason ?? "skipped");
|
|
306
|
+
updateSegmentForTask(state, taskId, "skipped");
|
|
307
|
+
break;
|
|
308
|
+
case "task.retry_requested":
|
|
309
|
+
if (!task) return;
|
|
310
|
+
clearTaskFailureMetadata(task);
|
|
311
|
+
task.status = "pending";
|
|
312
|
+
task.startedAt = null;
|
|
313
|
+
task.endedAt = null;
|
|
314
|
+
task.doneFileFound = false;
|
|
315
|
+
updateSegmentForTask(state, taskId, "pending");
|
|
316
|
+
break;
|
|
317
|
+
case "batch.paused":
|
|
318
|
+
state.phase = "paused";
|
|
319
|
+
break;
|
|
320
|
+
case "batch.completed":
|
|
321
|
+
state.phase = "completed";
|
|
322
|
+
state.endedAt = Number(payload.endedAt) || timestamp;
|
|
323
|
+
break;
|
|
324
|
+
case "batch.failed":
|
|
325
|
+
state.phase = "failed";
|
|
326
|
+
state.endedAt = Number(payload.endedAt) || timestamp;
|
|
327
|
+
break;
|
|
328
|
+
case "batch.aborted":
|
|
329
|
+
case "batch.dismissed":
|
|
330
|
+
state.phase = "aborted";
|
|
331
|
+
state.endedAt = Number(payload.endedAt) || timestamp;
|
|
332
|
+
break;
|
|
333
|
+
default:
|
|
334
|
+
break;
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
/** Apply journal timeline to journal-derived structural state; cache seed fills non-journal fields only. */
|
|
339
|
+
export function rebuildBatchStateFromJournal(seedState, events) {
|
|
340
|
+
const timeline = readJournalTimeline(events);
|
|
341
|
+
const rebuilt = deriveStructuralBatchStateFromJournal(events, seedState ?? null);
|
|
342
|
+
mergeSeedOnlyFields(rebuilt, seedState ?? null);
|
|
343
|
+
for (const event of timeline) applyJournalEvent(rebuilt, event);
|
|
344
|
+
recomputeTaskCounters(rebuilt);
|
|
345
|
+
return rebuilt;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
export function rebuildBatchStateFromDisk(projectRoot, batchId, seedState) {
|
|
349
|
+
return rebuildBatchStateFromJournal(seedState, readJournalEvents(projectRoot, batchId));
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
/** @typedef {{ taskId: string, field: string, cached: unknown, rebuilt: unknown }} DriftEntry */
|
|
353
|
+
|
|
354
|
+
function lastLifecycleEventForTask(events, taskId) {
|
|
355
|
+
const taskEvents = events.filter(
|
|
356
|
+
(event) => event?.taskId === taskId && TASK_LIFECYCLE_EVENT_TYPES.has(String(event?.type ?? "")),
|
|
357
|
+
);
|
|
358
|
+
return taskEvents.length > 0 ? taskEvents[taskEvents.length - 1] : null;
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
export function detectBatchStateDrift(cachedState, rebuiltState, events = []) {
|
|
362
|
+
/** @type {DriftEntry[]} */
|
|
363
|
+
const entries = [];
|
|
364
|
+
const rebuiltById = new Map((rebuiltState?.tasks ?? []).map((task) => [String(task?.taskId), task]));
|
|
365
|
+
|
|
366
|
+
for (const cached of cachedState?.tasks ?? []) {
|
|
367
|
+
if (!cached?.taskId) continue;
|
|
368
|
+
const taskId = String(cached.taskId);
|
|
369
|
+
const rebuilt = rebuiltById.get(taskId);
|
|
370
|
+
if (!rebuilt) continue;
|
|
371
|
+
const lastEvent = lastLifecycleEventForTask(events, taskId);
|
|
372
|
+
if (!lastEvent) continue;
|
|
373
|
+
const lastType = String(lastEvent.type ?? "");
|
|
374
|
+
|
|
375
|
+
if (lastType === "task.completed" && cached.status !== "succeeded") {
|
|
376
|
+
entries.push({ taskId, field: "status", cached: cached.status, rebuilt: rebuilt.status });
|
|
377
|
+
continue;
|
|
378
|
+
}
|
|
379
|
+
if (lastType === "task.retry_requested" && cached.status === "failed" && rebuilt.status === "pending") {
|
|
380
|
+
entries.push({ taskId, field: "status", cached: cached.status, rebuilt: rebuilt.status });
|
|
381
|
+
continue;
|
|
382
|
+
}
|
|
383
|
+
if (lastType === "task.started" && cached.status === "failed") continue;
|
|
384
|
+
if ((lastType === "task.failed" || lastType === "task.prompt_parse_failed") && cached.status === rebuilt.status) continue;
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
return { drifted: entries.length > 0, entries };
|
|
388
|
+
}
|