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.
Files changed (156) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +563 -0
  3. package/bin/get-version.mjs +35 -0
  4. package/bin/spine-batch.mjs +425 -0
  5. package/bin/spine-cli/batch.mjs +47 -0
  6. package/bin/spine-cli/gate.mjs +10 -0
  7. package/bin/spine-cli/integrate.mjs +10 -0
  8. package/bin/spine-cli/plan.mjs +12 -0
  9. package/bin/spine-cli/shared.mjs +73 -0
  10. package/bin/spine-cli/status.mjs +18 -0
  11. package/bin/spine-cli/verify.mjs +199 -0
  12. package/bin/spine-config.mjs +212 -0
  13. package/bin/spine-dashboard.mjs +178 -0
  14. package/bin/spine-deps.mjs +69 -0
  15. package/bin/spine-doctor.mjs +468 -0
  16. package/bin/spine-gate.mjs +137 -0
  17. package/bin/spine-init.mjs +398 -0
  18. package/bin/spine-integrate.mjs +77 -0
  19. package/bin/spine-journal.mjs +198 -0
  20. package/bin/spine-migrate-from-taskplane.mjs +136 -0
  21. package/bin/spine-plan.mjs +83 -0
  22. package/bin/spine-preflight.mjs +700 -0
  23. package/bin/spine-report-progress.d.mts +28 -0
  24. package/bin/spine-report-progress.mjs +126 -0
  25. package/bin/spine-review-step.d.mts +24 -0
  26. package/bin/spine-review-step.mjs +124 -0
  27. package/bin/spine-rules.mjs +35 -0
  28. package/bin/spine-settings.mjs +186 -0
  29. package/bin/spine-state.mjs +110 -0
  30. package/bin/spine-status.mjs +79 -0
  31. package/bin/spine-tasks.mjs +293 -0
  32. package/bin/spine-worker-runner.mjs +314 -0
  33. package/bin/spine.mjs +404 -0
  34. package/extensions/spine/settings-slash.ts +20 -0
  35. package/extensions/spine/slash-commands.ts +806 -0
  36. package/extensions/spine/worker-tools.ts +279 -0
  37. package/extensions/spine-orchestrator.ts +12 -0
  38. package/package.json +72 -0
  39. package/skills/.gitkeep +0 -0
  40. package/skills/create-spine-tasks/SKILL.md +375 -0
  41. package/skills/create-spine-tasks/references/context-template.md +25 -0
  42. package/skills/create-spine-tasks/references/contract-template.md +98 -0
  43. package/skills/create-spine-tasks/references/explore-template.md +63 -0
  44. package/skills/create-spine-tasks/references/prompt-template.md +276 -0
  45. package/src/batch/abort.mjs +272 -0
  46. package/src/batch/agent-session-worker.mjs +297 -0
  47. package/src/batch/batch-state-io.mjs +53 -0
  48. package/src/batch/contract-verify.mjs +310 -0
  49. package/src/batch/detached-start.mjs +778 -0
  50. package/src/batch/diagnosis.mjs +378 -0
  51. package/src/batch/engine-lanes/merge.mjs +632 -0
  52. package/src/batch/engine-lanes/queue.mjs +208 -0
  53. package/src/batch/engine-lanes/review.mjs +970 -0
  54. package/src/batch/engine-lanes.mjs +388 -0
  55. package/src/batch/engine-scope.mjs +251 -0
  56. package/src/batch/engine.mjs +445 -0
  57. package/src/batch/evidence-command.mjs +157 -0
  58. package/src/batch/evidence.mjs +203 -0
  59. package/src/batch/gate.mjs +359 -0
  60. package/src/batch/git-commit-env.mjs +46 -0
  61. package/src/batch/git-exec.mjs +26 -0
  62. package/src/batch/heartbeat.mjs +430 -0
  63. package/src/batch/integrate.mjs +414 -0
  64. package/src/batch/journal-rebuild.mjs +388 -0
  65. package/src/batch/journal.mjs +373 -0
  66. package/src/batch/lane-commit.mjs +153 -0
  67. package/src/batch/lifecycle.mjs +364 -0
  68. package/src/batch/metrics.mjs +265 -0
  69. package/src/batch/orphan-detect.mjs +210 -0
  70. package/src/batch/post-merge-limbo.mjs +80 -0
  71. package/src/batch/postmortem.mjs +296 -0
  72. package/src/batch/readers/spine-state.mjs +95 -0
  73. package/src/batch/readers/taskplane-state.mjs +136 -0
  74. package/src/batch/reconcile.mjs +729 -0
  75. package/src/batch/resume-common.mjs +104 -0
  76. package/src/batch/resume-engine.mjs +66 -0
  77. package/src/batch/resume-multi-lanes.mjs +479 -0
  78. package/src/batch/resume-multi-validate.mjs +187 -0
  79. package/src/batch/resume-multi.mjs +254 -0
  80. package/src/batch/resume.mjs +475 -0
  81. package/src/batch/retry.mjs +257 -0
  82. package/src/batch/review-scope.mjs +136 -0
  83. package/src/batch/review.mjs +1045 -0
  84. package/src/batch/salvage.mjs +332 -0
  85. package/src/batch/state.mjs +680 -0
  86. package/src/batch/task-stall-budget.mjs +88 -0
  87. package/src/batch/worker-host.mjs +683 -0
  88. package/src/batch/worker-output.mjs +253 -0
  89. package/src/batch/worker-prompt.mjs +84 -0
  90. package/src/batch/worktree.mjs +290 -0
  91. package/src/cli/deps.mjs +141 -0
  92. package/src/cli/handoff.mjs +364 -0
  93. package/src/cli/rules.mjs +429 -0
  94. package/src/cli/settings-set.mjs +171 -0
  95. package/src/cli/settings-show.mjs +140 -0
  96. package/src/cli/settings-slash.mjs +107 -0
  97. package/src/config/contract.mjs +56 -0
  98. package/src/config/cursor-rules/discover.mjs +416 -0
  99. package/src/config/cursor-rules/index.mjs +39 -0
  100. package/src/config/cursor-rules/match-globs.mjs +132 -0
  101. package/src/config/cursor-rules/parse-frontmatter.mjs +222 -0
  102. package/src/config/cursor-rules/priority.mjs +26 -0
  103. package/src/config/cursor-rules/profile.mjs +403 -0
  104. package/src/config/cursor-rules/select.mjs +259 -0
  105. package/src/config/defaults.mjs +36 -0
  106. package/src/config/env-overrides.mjs +207 -0
  107. package/src/config/merge-defaults.mjs +47 -0
  108. package/src/config/pi-spine-root.mjs +93 -0
  109. package/src/config/reviewer-context.mjs +243 -0
  110. package/src/config/settings-fields.mjs +265 -0
  111. package/src/config/worker-backend.mjs +116 -0
  112. package/src/config/worker-context.mjs +402 -0
  113. package/src/config/worker-launch-script.mjs +130 -0
  114. package/src/config/worktree-setup-hook.mjs +122 -0
  115. package/src/dashboard/public/dashboard.css +77 -0
  116. package/src/dashboard/public/dashboard.js +349 -0
  117. package/src/dashboard/public/index.html +80 -0
  118. package/src/dashboard/server.mjs +155 -0
  119. package/src/dashboard/snapshot.mjs +414 -0
  120. package/src/dashboard/sse.mjs +15 -0
  121. package/src/dashboard/view.mjs +292 -0
  122. package/src/doctor/agent-model-inherit.mjs +101 -0
  123. package/src/doctor/batch-size-guidance.mjs +72 -0
  124. package/src/doctor/coexistence.mjs +312 -0
  125. package/src/doctor/rules-manifest.mjs +133 -0
  126. package/src/doctor/stale-path.mjs +156 -0
  127. package/src/doctor/stall-config.mjs +108 -0
  128. package/src/doctor/suggest-max-parallel.mjs +53 -0
  129. package/src/doctor/task-packet-size.mjs +110 -0
  130. package/src/doctor/worktree-health.mjs +71 -0
  131. package/src/migrate/taskplane-config.mjs +116 -0
  132. package/src/planner/cycles.mjs +63 -0
  133. package/src/planner/format-plan.mjs +132 -0
  134. package/src/planner/graph.mjs +99 -0
  135. package/src/planner/index.mjs +125 -0
  136. package/src/planner/lanes.mjs +111 -0
  137. package/src/planner/pending.mjs +39 -0
  138. package/src/planner/scope.mjs +142 -0
  139. package/src/process/liveness.mjs +21 -0
  140. package/src/tasks/packet/discover.mjs +40 -0
  141. package/src/tasks/packet/index.mjs +81 -0
  142. package/src/tasks/packet/merge-deps.mjs +79 -0
  143. package/src/tasks/packet/parse-prompt.mjs +445 -0
  144. package/src/tasks/packet/parse-status.mjs +118 -0
  145. package/src/tasks/packet/validate-contract.mjs +115 -0
  146. package/src/tasks/packet/validate-prompt.mjs +99 -0
  147. package/src/worker-tools/report-progress.d.mts +18 -0
  148. package/src/worker-tools/report-progress.mjs +59 -0
  149. package/src/worker-tools/request-gate.d.mts +17 -0
  150. package/src/worker-tools/request-gate.mjs +54 -0
  151. package/templates/agents/reviewer.md +123 -0
  152. package/templates/agents/supervisor.md +59 -0
  153. package/templates/agents/worker.md +122 -0
  154. package/templates/rules-profile.json +27 -0
  155. package/templates/spine-config.json +64 -0
  156. package/templates/tasks/CONTEXT.md +30 -0
@@ -0,0 +1,104 @@
1
+ /**
2
+ * Shared resume validation and task-path helpers (SP-076).
3
+ */
4
+
5
+ import fs from "node:fs";
6
+ import path from "node:path";
7
+ import { DEFAULT_TASKS_ROOT } from "../../bin/spine-init.mjs";
8
+ import { appendJournalEvent } from "./journal.mjs";
9
+ import { recomputeTaskCounters } from "./state.mjs";
10
+ import { loadTaskFileScopePaths } from "./engine-lanes.mjs";
11
+ import { laneWorktreePath } from "./worktree.mjs";
12
+
13
+ /**
14
+ * @param {object} state
15
+ */
16
+ export { recomputeTaskCounters };
17
+
18
+ /**
19
+ * @param {object[]} events
20
+ * @param {string} taskId
21
+ */
22
+ export function journalHasTaskCompleted(events, taskId) {
23
+ return events.some((event) => event.type === "task.completed" && event.taskId === taskId);
24
+ }
25
+
26
+ /**
27
+ * @param {object} params
28
+ */
29
+ export function taskAlreadyComplete({ taskFolder, events, task }) {
30
+ const doneOnDisk = fs.existsSync(path.join(taskFolder, ".DONE"));
31
+ return (
32
+ doneOnDisk ||
33
+ task.doneFileFound ||
34
+ task.status === "succeeded" ||
35
+ journalHasTaskCompleted(events, task.taskId)
36
+ );
37
+ }
38
+
39
+ /**
40
+ * @param {string} projectRoot
41
+ * @param {string} batchId
42
+ * @param {string} fromPhase
43
+ * @param {string} toPhase
44
+ * @param {object} [extra]
45
+ */
46
+ export function recordResumePhaseTransition(projectRoot, batchId, fromPhase, toPhase, extra = {}) {
47
+ if (fromPhase === toPhase) return;
48
+ if (toPhase === "paused") {
49
+ appendJournalEvent(projectRoot, batchId, "batch.paused", { fromPhase, toPhase, ...extra });
50
+ }
51
+ if (
52
+ toPhase === "running" &&
53
+ (fromPhase === "paused" || (fromPhase === "failed" && extra.resumeForced))
54
+ ) {
55
+ appendJournalEvent(projectRoot, batchId, "batch.resumed", { fromPhase, toPhase, ...extra });
56
+ }
57
+ }
58
+
59
+ /**
60
+ * @param {object} task
61
+ * @param {string} projectRoot
62
+ */
63
+ export function resolveTaskFolderRel(task, projectRoot) {
64
+ if (!task.taskFolder) return null;
65
+ return path.isAbsolute(task.taskFolder)
66
+ ? path.relative(projectRoot, task.taskFolder)
67
+ : task.taskFolder;
68
+ }
69
+
70
+ /**
71
+ * @param {object} params
72
+ */
73
+ export function resolveTaskFolderInWorktree({
74
+ projectRoot,
75
+ task,
76
+ lane,
77
+ tasksRootRel = DEFAULT_TASKS_ROOT,
78
+ batchId,
79
+ }) {
80
+ const taskFolderRel = resolveTaskFolderRel(task, projectRoot);
81
+ const laneNumber = Number(lane.laneNumber ?? 1);
82
+ const wt =
83
+ lane.worktreePath ?? laneWorktreePath(projectRoot, batchId ?? "", laneNumber);
84
+ return taskFolderRel
85
+ ? path.join(wt, taskFolderRel)
86
+ : path.join(wt, tasksRootRel, `${task.taskId}-smoke`);
87
+ }
88
+
89
+ /**
90
+ * @param {string} projectRoot
91
+ * @param {string|null} taskFolderRel
92
+ * @param {string} tasksRootRel
93
+ * @param {string} taskId
94
+ */
95
+ export function resolveTaskFolderOnHost(projectRoot, taskFolderRel, tasksRootRel, taskId) {
96
+ return path.join(projectRoot, taskFolderRel ?? path.join(tasksRootRel, `${taskId}-smoke`));
97
+ }
98
+
99
+ /**
100
+ * @param {string} taskFolderOnHost
101
+ */
102
+ export function loadResumeFileScopePaths(taskFolderOnHost) {
103
+ return loadTaskFileScopePaths(taskFolderOnHost);
104
+ }
@@ -0,0 +1,66 @@
1
+ /**
2
+ * Detached engine lifecycle helpers for batch resume (SP-203).
3
+ */
4
+
5
+ import { isProcessAlive } from "../process/liveness.mjs";
6
+ import { appendJournalEvent } from "./journal.mjs";
7
+ import { isPostMergeLimbo } from "./post-merge-limbo.mjs";
8
+ import { clearBatchEnginePid, readBatchEnginePid } from "./state.mjs";
9
+
10
+ /**
11
+ * SIGTERM a stale detached engine before the resume engine takes over.
12
+ *
13
+ * @param {object} params
14
+ * @param {string} params.projectRoot
15
+ * @param {object} params.state
16
+ * @param {string} params.batchId
17
+ * @param {string} params.fromPhase
18
+ * @param {object} [params.git]
19
+ */
20
+ export function terminateStaleDetachedEngine({
21
+ projectRoot,
22
+ state,
23
+ batchId,
24
+ fromPhase,
25
+ git = {},
26
+ }) {
27
+ const stalePid = readBatchEnginePid(state);
28
+ if (!stalePid || stalePid === process.pid) {
29
+ return { terminated: false, reason: "no_stale_pid" };
30
+ }
31
+
32
+ if (!isProcessAlive(stalePid)) {
33
+ clearBatchEnginePid(state);
34
+ return { terminated: false, reason: "already_dead", stalePid };
35
+ }
36
+
37
+ const eligiblePhase =
38
+ fromPhase === "paused" ||
39
+ fromPhase === "failed" ||
40
+ (fromPhase === "running" && isPostMergeLimbo(state, git));
41
+ if (!eligiblePhase) {
42
+ return { terminated: false, reason: "phase_not_eligible", stalePid };
43
+ }
44
+
45
+ try {
46
+ process.kill(stalePid, "SIGTERM");
47
+ } catch {
48
+ /* process may have exited between liveness check and kill */
49
+ }
50
+
51
+ if (isProcessAlive(stalePid)) {
52
+ try {
53
+ process.kill(stalePid, "SIGKILL");
54
+ } catch {
55
+ /* ignore */
56
+ }
57
+ }
58
+
59
+ appendJournalEvent(projectRoot, batchId, "engine.orphan_terminated", {
60
+ stalePid,
61
+ fromPhase,
62
+ signal: "SIGTERM",
63
+ });
64
+ clearBatchEnginePid(state);
65
+ return { terminated: true, stalePid };
66
+ }
@@ -0,0 +1,479 @@
1
+ /**
2
+ * Per-lane task execution and wave queue wiring for multi-task resume (extracted from resume-multi.mjs).
3
+ */
4
+
5
+ import path from "node:path";
6
+ import crypto from "node:crypto";
7
+ import {
8
+ journalHasTaskCompleted,
9
+ loadResumeFileScopePaths,
10
+ recomputeTaskCounters,
11
+ resolveTaskFolderInWorktree,
12
+ taskAlreadyComplete,
13
+ } from "./resume-common.mjs";
14
+ import { appendJournalEvent, readJournalEvents } from "./journal.mjs";
15
+ import { commitLaneWorktree, filterPorcelain, gitPorcelain } from "./lane-commit.mjs";
16
+ import { recordTaskFailureSalvage } from "./salvage.mjs";
17
+ import { saveSpineBatchState, updateSegmentForTask } from "./state.mjs";
18
+ import { laneTaskBranch } from "./worktree.mjs";
19
+ import { runWorker } from "./worker-host.mjs";
20
+ import { isTaskResumable } from "./resume-multi-validate.mjs";
21
+
22
+ /**
23
+ * @param {object} params
24
+ */
25
+ export function resetFailedTasksForForceResume({ state, pendingTasks }) {
26
+ state.resilience = state.resilience ?? {};
27
+ state.resilience.resumeForced = true;
28
+ state.failedTasks = 0;
29
+
30
+ for (const task of pendingTasks) {
31
+ if (task.status !== "failed") continue;
32
+ task.status = "pending";
33
+ task.startedAt = null;
34
+ task.endedAt = null;
35
+ task.exitReason = null;
36
+ task.doneFileFound = false;
37
+ updateSegmentForTask(state, task.taskId, "pending");
38
+ }
39
+ }
40
+
41
+ /**
42
+ * @param {object} params
43
+ */
44
+ async function markTaskCompleteFromDisk({
45
+ projectRoot,
46
+ state,
47
+ batchId,
48
+ config,
49
+ task,
50
+ lane,
51
+ taskBranch,
52
+ taskFolderInWorktree,
53
+ laneCorrelationId,
54
+ }) {
55
+ const taskId = task.taskId;
56
+ const laneNumber = lane.laneNumber;
57
+ const wt = lane.worktreePath;
58
+ const ignorePatterns = Array.isArray(config?.worktreeSetupIgnorePaths)
59
+ ? config.worktreeSetupIgnorePaths
60
+ : [];
61
+
62
+ const laneCommit = commitLaneWorktree({
63
+ worktreePath: wt,
64
+ taskBranch,
65
+ taskId,
66
+ batchId,
67
+ taskFolder: taskFolderInWorktree,
68
+ projectRoot,
69
+ });
70
+ if (!laneCommit.ok) {
71
+ task.status = "failed";
72
+ task.endedAt = Date.now();
73
+ task.exitReason = laneCommit.failureClass ?? "lane_commit_failed";
74
+ updateSegmentForTask(state, taskId, "failed");
75
+ recomputeTaskCounters(state);
76
+ saveSpineBatchState(projectRoot, state);
77
+ appendJournalEvent(projectRoot, batchId, "task.failed", {
78
+ taskId,
79
+ laneNumber,
80
+ laneId: lane.laneId,
81
+ correlationId: laneCorrelationId,
82
+ classification: laneCommit.failureClass ?? "lane_commit_failed",
83
+ exitCode: 1,
84
+ output: laneCommit.error,
85
+ resumed: true,
86
+ });
87
+ return {
88
+ ok: false,
89
+ error: "lane_commit_failed",
90
+ output: laneCommit.error,
91
+ taskId,
92
+ laneNumber,
93
+ };
94
+ }
95
+ if (laneCommit.committed) {
96
+ appendJournalEvent(projectRoot, batchId, "lane.committed", {
97
+ taskId,
98
+ laneNumber,
99
+ commitSha: laneCommit.commitSha,
100
+ });
101
+ }
102
+
103
+ const remainingDirty = filterPorcelain(gitPorcelain(wt), ignorePatterns);
104
+ if (remainingDirty) {
105
+ const dirtyOutput =
106
+ "Lane worktree still has uncommitted changes after auto-commit — commit manually or fix worker output";
107
+ task.status = "failed";
108
+ task.endedAt = Date.now();
109
+ task.exitReason = "DirtyWorktree";
110
+ updateSegmentForTask(state, taskId, "failed");
111
+ recomputeTaskCounters(state);
112
+ saveSpineBatchState(projectRoot, state);
113
+ appendJournalEvent(projectRoot, batchId, "task.failed", {
114
+ taskId,
115
+ laneNumber,
116
+ laneId: lane.laneId,
117
+ correlationId: laneCorrelationId,
118
+ classification: "DirtyWorktree",
119
+ exitCode: 1,
120
+ output: dirtyOutput,
121
+ resumed: true,
122
+ });
123
+ return {
124
+ ok: false,
125
+ error: "dirty_after_lane_commit",
126
+ output: dirtyOutput,
127
+ taskId,
128
+ laneNumber,
129
+ };
130
+ }
131
+
132
+ task.status = "succeeded";
133
+ task.doneFileFound = true;
134
+ task.exitReason = task.exitReason ?? "done";
135
+ if (!task.endedAt) task.endedAt = Date.now();
136
+ updateSegmentForTask(state, taskId, "succeeded");
137
+ recomputeTaskCounters(state);
138
+ saveSpineBatchState(projectRoot, state);
139
+
140
+ if (!journalHasTaskCompleted(readJournalEvents(projectRoot, batchId), taskId)) {
141
+ appendJournalEvent(projectRoot, batchId, "task.completed", {
142
+ taskId,
143
+ laneNumber,
144
+ laneId: lane.laneId,
145
+ correlationId: laneCorrelationId,
146
+ resumed: true,
147
+ skippedDoneOnDisk: true,
148
+ taskFolder: taskFolderInWorktree,
149
+ });
150
+ }
151
+
152
+ return { ok: true, skipped: true, taskId, laneNumber };
153
+ }
154
+
155
+ /**
156
+ * @param {object} params
157
+ */
158
+ async function runResumedTaskOnLane({
159
+ projectRoot,
160
+ state,
161
+ batchId,
162
+ config,
163
+ task,
164
+ lane,
165
+ taskFolderRel,
166
+ laneCorrelationId,
167
+ }) {
168
+ const taskId = task.taskId;
169
+ const laneNumber = lane.laneNumber;
170
+ const wt = lane.worktreePath;
171
+ const taskBranch = lane.branch ?? laneTaskBranch(batchId, laneNumber);
172
+ const taskFolderInWorktree = path.join(wt, taskFolderRel);
173
+ const scopeResult = loadResumeFileScopePaths(path.join(projectRoot, taskFolderRel));
174
+ const fileScopePaths = scopeResult.ok ? scopeResult.fileScopePaths : [];
175
+
176
+ task.status = "running";
177
+ if (!task.startedAt) task.startedAt = Date.now();
178
+ updateSegmentForTask(state, taskId, "running");
179
+ saveSpineBatchState(projectRoot, state);
180
+ appendJournalEvent(projectRoot, batchId, "task.started", {
181
+ taskId,
182
+ laneNumber,
183
+ laneId: lane.laneId,
184
+ correlationId: laneCorrelationId,
185
+ resumed: true,
186
+ });
187
+
188
+ const workerResult = await runWorker({
189
+ worktreePath: wt,
190
+ taskFolder: taskFolderInWorktree,
191
+ projectRoot,
192
+ batchId,
193
+ laneNumber,
194
+ taskId,
195
+ laneBranch: taskBranch,
196
+ laneCorrelationId,
197
+ fileScopePaths,
198
+ config,
199
+ onHeartbeat: (timestamp) => {
200
+ lane.lastHeartbeatAt = timestamp;
201
+ saveSpineBatchState(projectRoot, state);
202
+ },
203
+ onWorkerPid: (pid) => {
204
+ if (pid > 0) {
205
+ lane.workerPid = pid;
206
+ saveSpineBatchState(projectRoot, state);
207
+ }
208
+ },
209
+ });
210
+
211
+ if (!workerResult.ok) {
212
+ const aborted = workerResult.classification === "aborted";
213
+ appendJournalEvent(projectRoot, batchId, "lane.died", {
214
+ laneNumber,
215
+ laneId: lane.laneId,
216
+ taskId,
217
+ correlationId: laneCorrelationId,
218
+ reason: workerResult.classification ?? "worker_failed",
219
+ });
220
+ task.status = aborted ? "aborted" : "failed";
221
+ task.endedAt = Date.now();
222
+ task.exitReason = workerResult.classification ?? "worker_failed";
223
+ updateSegmentForTask(state, taskId, aborted ? "aborted" : "failed");
224
+ recomputeTaskCounters(state);
225
+ saveSpineBatchState(projectRoot, state);
226
+ if (!aborted) {
227
+ const salvageFields = recordTaskFailureSalvage({
228
+ projectRoot,
229
+ batchId,
230
+ laneNumber,
231
+ laneId: lane.laneId,
232
+ taskId,
233
+ correlationId: laneCorrelationId,
234
+ worktreePath: wt,
235
+ fileScopePaths,
236
+ taskFolder: taskFolderInWorktree,
237
+ workerResult,
238
+ config,
239
+ batchPhase: state.phase,
240
+ taskBranch,
241
+ });
242
+ appendJournalEvent(projectRoot, batchId, "task.failed", {
243
+ taskId,
244
+ laneNumber,
245
+ laneId: lane.laneId,
246
+ correlationId: laneCorrelationId,
247
+ ...workerResult,
248
+ ...salvageFields,
249
+ });
250
+ }
251
+ return { ok: false, aborted, workerResult, taskId, laneNumber };
252
+ }
253
+
254
+ appendJournalEvent(projectRoot, batchId, "lane.completed", {
255
+ laneNumber,
256
+ laneId: lane.laneId,
257
+ taskId,
258
+ correlationId: laneCorrelationId,
259
+ });
260
+
261
+ const ignorePatterns = Array.isArray(config?.worktreeSetupIgnorePaths)
262
+ ? config.worktreeSetupIgnorePaths
263
+ : [];
264
+ const laneCommit = commitLaneWorktree({
265
+ worktreePath: wt,
266
+ taskBranch,
267
+ taskId,
268
+ batchId,
269
+ taskFolder: taskFolderInWorktree,
270
+ projectRoot,
271
+ });
272
+ if (!laneCommit.ok) {
273
+ task.status = "failed";
274
+ task.endedAt = Date.now();
275
+ task.exitReason = laneCommit.failureClass ?? "lane_commit_failed";
276
+ updateSegmentForTask(state, taskId, "failed");
277
+ recomputeTaskCounters(state);
278
+ saveSpineBatchState(projectRoot, state);
279
+ appendJournalEvent(projectRoot, batchId, "task.failed", {
280
+ taskId,
281
+ laneNumber,
282
+ laneId: lane.laneId,
283
+ correlationId: laneCorrelationId,
284
+ classification: laneCommit.failureClass ?? "lane_commit_failed",
285
+ exitCode: 1,
286
+ output: laneCommit.error,
287
+ resumed: true,
288
+ });
289
+ return {
290
+ ok: false,
291
+ error: "lane_commit_failed",
292
+ output: laneCommit.error,
293
+ taskId,
294
+ laneNumber,
295
+ };
296
+ }
297
+ if (laneCommit.committed) {
298
+ appendJournalEvent(projectRoot, batchId, "lane.committed", {
299
+ taskId,
300
+ laneNumber,
301
+ commitSha: laneCommit.commitSha,
302
+ });
303
+ }
304
+
305
+ const remainingDirty = filterPorcelain(gitPorcelain(wt), ignorePatterns);
306
+ if (remainingDirty) {
307
+ const dirtyOutput =
308
+ "Lane worktree still has uncommitted changes after auto-commit — commit manually or fix worker output";
309
+ task.status = "failed";
310
+ task.endedAt = Date.now();
311
+ task.exitReason = "DirtyWorktree";
312
+ updateSegmentForTask(state, taskId, "failed");
313
+ recomputeTaskCounters(state);
314
+ saveSpineBatchState(projectRoot, state);
315
+ appendJournalEvent(projectRoot, batchId, "task.failed", {
316
+ taskId,
317
+ laneNumber,
318
+ laneId: lane.laneId,
319
+ correlationId: laneCorrelationId,
320
+ classification: "DirtyWorktree",
321
+ exitCode: 1,
322
+ output: dirtyOutput,
323
+ resumed: true,
324
+ });
325
+ return {
326
+ ok: false,
327
+ error: "dirty_after_lane_commit",
328
+ output: dirtyOutput,
329
+ taskId,
330
+ laneNumber,
331
+ };
332
+ }
333
+
334
+ task.status = "succeeded";
335
+ task.endedAt = Date.now();
336
+ task.doneFileFound = true;
337
+ task.exitReason = "done";
338
+ updateSegmentForTask(state, taskId, "succeeded");
339
+ recomputeTaskCounters(state);
340
+ saveSpineBatchState(projectRoot, state);
341
+ appendJournalEvent(projectRoot, batchId, "task.completed", {
342
+ taskId,
343
+ laneNumber,
344
+ laneId: lane.laneId,
345
+ correlationId: laneCorrelationId,
346
+ });
347
+
348
+ return { ok: true, taskId, laneNumber };
349
+ }
350
+
351
+ /**
352
+ * Build lane queues and run all tasks in a resume wave.
353
+ *
354
+ * @param {object} params
355
+ * @returns {Promise<{ ok: true, waveResults: object[], batchAborted: boolean } | { ok: false, result: object }>}
356
+ */
357
+ export async function executeResumeWave({
358
+ projectRoot,
359
+ state,
360
+ batchId,
361
+ config,
362
+ tasksRootRel,
363
+ waveIndex,
364
+ waveTaskIds,
365
+ events,
366
+ }) {
367
+ /** @type {Map<number, { lane: object, runs: Array<{ taskId: string, run: () => Promise<object> }> }>} */
368
+ const runsByLane = new Map();
369
+ let batchAborted = false;
370
+
371
+ for (const taskId of waveTaskIds) {
372
+ const task = (state.tasks ?? []).find((entry) => entry?.taskId === taskId);
373
+ if (!task) continue;
374
+
375
+ const laneNumber = task.laneNumber;
376
+ const lane = (state.lanes ?? []).find((entry) => entry.laneNumber === laneNumber);
377
+ if (!lane) {
378
+ return {
379
+ ok: false,
380
+ result: {
381
+ ok: false,
382
+ exitCode: 1,
383
+ batchId,
384
+ taskId,
385
+ error: "lane_not_found",
386
+ output: `No lane assigned for task ${taskId} (lane ${laneNumber}).\n`,
387
+ },
388
+ };
389
+ }
390
+
391
+ const taskFolderRel = task.taskFolder
392
+ ? path.isAbsolute(task.taskFolder)
393
+ ? path.relative(projectRoot, task.taskFolder)
394
+ : task.taskFolder
395
+ : path.join(tasksRootRel, `${taskId}-smoke`);
396
+ const taskFolderInWorktree = resolveTaskFolderInWorktree({
397
+ projectRoot,
398
+ task,
399
+ lane,
400
+ tasksRootRel,
401
+ batchId,
402
+ });
403
+ const laneCorrelationId = lane.correlationId ?? crypto.randomUUID();
404
+ lane.correlationId = laneCorrelationId;
405
+
406
+ if (!runsByLane.has(laneNumber)) {
407
+ runsByLane.set(laneNumber, { lane, runs: [] });
408
+ }
409
+ const laneQueue = runsByLane.get(laneNumber);
410
+
411
+ if (taskAlreadyComplete({ taskFolder: taskFolderInWorktree, events, task })) {
412
+ laneQueue.runs.push({
413
+ taskId,
414
+ run: () =>
415
+ markTaskCompleteFromDisk({
416
+ projectRoot,
417
+ state,
418
+ batchId,
419
+ config,
420
+ task,
421
+ lane,
422
+ taskBranch: lane.branch ?? laneTaskBranch(batchId, laneNumber),
423
+ taskFolderInWorktree,
424
+ laneCorrelationId,
425
+ }),
426
+ });
427
+ continue;
428
+ }
429
+
430
+ if (!isTaskResumable(state, task)) {
431
+ continue;
432
+ }
433
+
434
+ laneQueue.runs.push({
435
+ taskId,
436
+ run: () =>
437
+ runResumedTaskOnLane({
438
+ projectRoot,
439
+ state,
440
+ batchId,
441
+ config,
442
+ task,
443
+ lane,
444
+ taskFolderRel,
445
+ laneCorrelationId,
446
+ }).then((result) => {
447
+ if (result.aborted) batchAborted = true;
448
+ return result;
449
+ }),
450
+ });
451
+ }
452
+
453
+ const laneExecutions = [...runsByLane.entries()].map(async ([laneNumber, { lane, runs }]) => {
454
+ if (runs.length > 1) {
455
+ appendJournalEvent(projectRoot, batchId, "lane.tasks_serialized", {
456
+ laneNumber,
457
+ laneId: lane.laneId,
458
+ waveIndex,
459
+ taskIds: runs.map((entry) => entry.taskId),
460
+ correlationId: lane.correlationId ?? null,
461
+ });
462
+ }
463
+
464
+ /** @type {object[]} */
465
+ const laneResults = [];
466
+ for (const { run } of runs) {
467
+ const result = await run();
468
+ laneResults.push(result);
469
+ if (result.aborted) {
470
+ batchAborted = true;
471
+ return laneResults;
472
+ }
473
+ }
474
+ return laneResults;
475
+ });
476
+
477
+ const waveResults = (await Promise.all(laneExecutions)).flat();
478
+ return { ok: true, waveResults, batchAborted };
479
+ }