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,729 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Batch reconciliation (FR-BATCH-12, §17.5).
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import fs from "node:fs";
|
|
6
|
+
import path from "node:path";
|
|
7
|
+
import { execFileSync } from "node:child_process";
|
|
8
|
+
import { loadSpineConfig } from "../../bin/spine-config.mjs";
|
|
9
|
+
import { resolveTasksRootPath } from "../config/env-overrides.mjs";
|
|
10
|
+
import {
|
|
11
|
+
buildDiagnosisOutput,
|
|
12
|
+
inferLaunchFailureFromWorkerOutputTail,
|
|
13
|
+
inferLaunchFailureKind,
|
|
14
|
+
} from "./diagnosis.mjs";
|
|
15
|
+
import { workerOutputLogPath } from "./worker-output.mjs";
|
|
16
|
+
import { detectOrphanRunning, journalEventsSinceResume } from "./orphan-detect.mjs";
|
|
17
|
+
import { isPostMergeLimbo } from "./post-merge-limbo.mjs";
|
|
18
|
+
import { computePendingTasks } from "./resume-multi.mjs";
|
|
19
|
+
import {
|
|
20
|
+
detectBatchStateDrift,
|
|
21
|
+
rebuildBatchStateFromJournal,
|
|
22
|
+
} from "./journal-rebuild.mjs";
|
|
23
|
+
import { extractJournalDiagnosisHints, journalPath, readJournalEvents } from "./journal.mjs";
|
|
24
|
+
import { findLatestSalvageInspection } from "./salvage.mjs";
|
|
25
|
+
import { countCommitsAhead } from "./lane-commit.mjs";
|
|
26
|
+
import {
|
|
27
|
+
loadBatchStateFile,
|
|
28
|
+
parseBatchState,
|
|
29
|
+
resolveBatchStatePath,
|
|
30
|
+
} from "./batch-state-io.mjs";
|
|
31
|
+
|
|
32
|
+
export { loadBatchStateFile, parseBatchState, resolveBatchStatePath } from "./batch-state-io.mjs";
|
|
33
|
+
|
|
34
|
+
const LIMBO_PHASES = new Set(["stopped", "failed", "executing"]);
|
|
35
|
+
const RUNNING_PHASES = new Set(["planning", "running", "executing", "merging"]);
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* @typedef {object} NormalizedTask
|
|
39
|
+
* @property {string} taskId
|
|
40
|
+
* @property {string} status
|
|
41
|
+
* @property {string|null} taskFolder
|
|
42
|
+
* @property {boolean} doneFileFound
|
|
43
|
+
* @property {string} classification
|
|
44
|
+
* @property {boolean} [doneOnDisk]
|
|
45
|
+
*/
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* @typedef {object} NormalizedBatchState
|
|
49
|
+
* @property {"spine"|"taskplane"} source
|
|
50
|
+
* @property {string} batchId
|
|
51
|
+
* @property {string} phase
|
|
52
|
+
* @property {string} baseBranch
|
|
53
|
+
* @property {string|null} orchBranch
|
|
54
|
+
* @property {unknown} startedAt
|
|
55
|
+
* @property {unknown} endedAt
|
|
56
|
+
* @property {number} failedTasks
|
|
57
|
+
* @property {number} succeededTasks
|
|
58
|
+
* @property {number} totalTasks
|
|
59
|
+
* @property {unknown[]} mergeResults
|
|
60
|
+
* @property {NormalizedTask[]} tasks
|
|
61
|
+
* @property {Array<{ segmentId: string, taskId: string, status: string, classification: string }>} segments
|
|
62
|
+
* @property {unknown[]} lanes
|
|
63
|
+
* @property {Record<string, unknown>} raw
|
|
64
|
+
*/
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* @typedef {object} ReconciliationResult
|
|
68
|
+
* @property {string|null} diagnosis
|
|
69
|
+
* @property {string} headline
|
|
70
|
+
* @property {string} suggestedCommand
|
|
71
|
+
* @property {string[]} [alternatives]
|
|
72
|
+
* @property {string|null} [batchId]
|
|
73
|
+
* @property {string|null} [batchStatePath]
|
|
74
|
+
* @property {string|null} [phase]
|
|
75
|
+
* @property {object} [signals]
|
|
76
|
+
*/
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* @param {string} projectRoot
|
|
80
|
+
* @param {ReturnType<typeof loadSpineConfig>} [configResult]
|
|
81
|
+
*/
|
|
82
|
+
export function resolveTasksRoot(projectRoot, configResult) {
|
|
83
|
+
const loaded = configResult ?? loadSpineConfig(projectRoot);
|
|
84
|
+
if (!loaded.config) {
|
|
85
|
+
return null;
|
|
86
|
+
}
|
|
87
|
+
return resolveTasksRootPath(projectRoot, loaded.config);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* @param {string} tasksRoot
|
|
92
|
+
* @param {string} taskId
|
|
93
|
+
* @param {string|null} taskFolder
|
|
94
|
+
*/
|
|
95
|
+
function resolveTaskFolder(tasksRoot, taskId, taskFolder) {
|
|
96
|
+
if (taskFolder) {
|
|
97
|
+
const direct = path.join(tasksRoot, taskFolder);
|
|
98
|
+
if (fs.existsSync(direct)) return direct;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if (!tasksRoot || !fs.existsSync(tasksRoot)) return null;
|
|
102
|
+
|
|
103
|
+
const match = fs
|
|
104
|
+
.readdirSync(tasksRoot, { withFileTypes: true })
|
|
105
|
+
.filter((entry) => entry.isDirectory() && entry.name.startsWith(`${taskId}-`))
|
|
106
|
+
.map((entry) => path.join(tasksRoot, entry.name))[0];
|
|
107
|
+
|
|
108
|
+
return match ?? null;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* @param {NormalizedBatchState} batch
|
|
113
|
+
* @param {string|null} tasksRoot
|
|
114
|
+
*/
|
|
115
|
+
export function classifyTasks(batch, tasksRoot) {
|
|
116
|
+
return batch.tasks.map((task) => {
|
|
117
|
+
const folderPath =
|
|
118
|
+
tasksRoot && task.taskId
|
|
119
|
+
? resolveTaskFolder(tasksRoot, task.taskId, task.taskFolder)
|
|
120
|
+
: null;
|
|
121
|
+
const doneOnDisk = folderPath ? fs.existsSync(path.join(folderPath, ".DONE")) : false;
|
|
122
|
+
|
|
123
|
+
let classification = task.classification;
|
|
124
|
+
if (doneOnDisk || task.doneFileFound) {
|
|
125
|
+
classification = "terminal-success";
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
return {
|
|
129
|
+
...task,
|
|
130
|
+
doneOnDisk,
|
|
131
|
+
classification,
|
|
132
|
+
};
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* @param {string} projectRoot
|
|
138
|
+
*/
|
|
139
|
+
function isInsideGitRepo(projectRoot) {
|
|
140
|
+
try {
|
|
141
|
+
execFileSync("git", ["rev-parse", "--is-inside-work-tree"], {
|
|
142
|
+
cwd: projectRoot,
|
|
143
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
144
|
+
timeout: 5000,
|
|
145
|
+
});
|
|
146
|
+
return true;
|
|
147
|
+
} catch {
|
|
148
|
+
return false;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* @param {string} projectRoot
|
|
154
|
+
* @param {string} ref
|
|
155
|
+
*/
|
|
156
|
+
function gitRefExists(projectRoot, ref) {
|
|
157
|
+
try {
|
|
158
|
+
execFileSync("git", ["rev-parse", "--verify", ref], {
|
|
159
|
+
cwd: projectRoot,
|
|
160
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
161
|
+
timeout: 5000,
|
|
162
|
+
});
|
|
163
|
+
return true;
|
|
164
|
+
} catch {
|
|
165
|
+
return false;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* @param {string} projectRoot
|
|
171
|
+
* @param {string} ancestor
|
|
172
|
+
* @param {string} descendant
|
|
173
|
+
*/
|
|
174
|
+
function gitIsAncestor(projectRoot, ancestor, descendant) {
|
|
175
|
+
try {
|
|
176
|
+
execFileSync("git", ["merge-base", "--is-ancestor", ancestor, descendant], {
|
|
177
|
+
cwd: projectRoot,
|
|
178
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
179
|
+
timeout: 5000,
|
|
180
|
+
});
|
|
181
|
+
return true;
|
|
182
|
+
} catch {
|
|
183
|
+
return false;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* @param {object} result
|
|
189
|
+
* @param {unknown} err
|
|
190
|
+
* @param {string} context
|
|
191
|
+
*/
|
|
192
|
+
function recordGitInspectionError(result, err, context) {
|
|
193
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
194
|
+
const detail = `${context}: ${message}`;
|
|
195
|
+
result.gitInspectionError = result.gitInspectionError
|
|
196
|
+
? `${result.gitInspectionError}; ${detail}`
|
|
197
|
+
: detail;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* @param {string} projectRoot
|
|
202
|
+
* @param {string} batchId
|
|
203
|
+
*/
|
|
204
|
+
function listOrchBranches(projectRoot, batchId) {
|
|
205
|
+
try {
|
|
206
|
+
const output = execFileSync("git", ["branch", "-a", "--list", "*orch*"], {
|
|
207
|
+
cwd: projectRoot,
|
|
208
|
+
encoding: "utf-8",
|
|
209
|
+
timeout: 5000,
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
return output
|
|
213
|
+
.split(/\r?\n/)
|
|
214
|
+
.map((line) => line.trim().replace(/^\*?\s+/, "").replace(/^remotes\/[^/]+\//, ""))
|
|
215
|
+
.filter(Boolean)
|
|
216
|
+
.filter((branch) => branch.includes(batchId));
|
|
217
|
+
} catch {
|
|
218
|
+
return [];
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* @param {object} ctx
|
|
224
|
+
* @param {string} ctx.projectRoot
|
|
225
|
+
* @param {string} ctx.batchId
|
|
226
|
+
* @param {string} ctx.baseBranch
|
|
227
|
+
* @param {string|null} ctx.orchBranch
|
|
228
|
+
*/
|
|
229
|
+
export function inspectGitState(ctx) {
|
|
230
|
+
const { projectRoot, batchId, baseBranch, orchBranch } = ctx;
|
|
231
|
+
const result = {
|
|
232
|
+
inGitRepo: isInsideGitRepo(projectRoot),
|
|
233
|
+
baseBranch,
|
|
234
|
+
orchBranch,
|
|
235
|
+
orchBranches: [],
|
|
236
|
+
orchBranchExists: false,
|
|
237
|
+
orchMergedToBase: false,
|
|
238
|
+
mergedOrchBranch: null,
|
|
239
|
+
orchCommitsAhead: null,
|
|
240
|
+
gitInspectionError: null,
|
|
241
|
+
};
|
|
242
|
+
|
|
243
|
+
if (!result.inGitRepo) return result;
|
|
244
|
+
|
|
245
|
+
if (process.env.SPINE_TEST_GIT_INSPECTION_THROW) {
|
|
246
|
+
result.gitInspectionError = `simulated: ${process.env.SPINE_TEST_GIT_INSPECTION_THROW}`;
|
|
247
|
+
return result;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
const candidates = new Set(listOrchBranches(projectRoot, batchId));
|
|
251
|
+
if (orchBranch) candidates.add(orchBranch);
|
|
252
|
+
candidates.add(`orch/cdelgado-${batchId}`);
|
|
253
|
+
candidates.add(`orch/spine-*-${batchId}`.replace("*", "operator"));
|
|
254
|
+
|
|
255
|
+
result.orchBranches = [...candidates].filter((branch) => !branch.includes("*"));
|
|
256
|
+
|
|
257
|
+
for (const branch of result.orchBranches) {
|
|
258
|
+
if (!gitRefExists(projectRoot, branch)) continue;
|
|
259
|
+
result.orchBranchExists = true;
|
|
260
|
+
if (gitIsAncestor(projectRoot, branch, baseBranch)) {
|
|
261
|
+
result.orchMergedToBase = true;
|
|
262
|
+
result.mergedOrchBranch = branch;
|
|
263
|
+
break;
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
const resolvedOrch = orchBranch && gitRefExists(projectRoot, orchBranch) ? orchBranch : result.mergedOrchBranch;
|
|
268
|
+
if (resolvedOrch && gitRefExists(projectRoot, resolvedOrch) && !result.orchMergedToBase) {
|
|
269
|
+
try {
|
|
270
|
+
result.orchCommitsAhead = countCommitsAhead(projectRoot, baseBranch, resolvedOrch);
|
|
271
|
+
} catch (err) {
|
|
272
|
+
result.orchCommitsAhead = null;
|
|
273
|
+
recordGitInspectionError(result, err, "count_commits_ahead");
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
if (!result.orchMergedToBase) {
|
|
278
|
+
try {
|
|
279
|
+
const merged = execFileSync("git", ["branch", "--merged", baseBranch], {
|
|
280
|
+
cwd: projectRoot,
|
|
281
|
+
encoding: "utf-8",
|
|
282
|
+
timeout: 5000,
|
|
283
|
+
});
|
|
284
|
+
const mergedOrch = merged
|
|
285
|
+
.split(/\r?\n/)
|
|
286
|
+
.map((line) => line.trim().replace(/^\*?\s+/, ""))
|
|
287
|
+
.filter((branch) => branch.includes("orch") && branch.includes(batchId));
|
|
288
|
+
if (mergedOrch.length > 0) {
|
|
289
|
+
result.orchMergedToBase = true;
|
|
290
|
+
result.mergedOrchBranch = mergedOrch[0];
|
|
291
|
+
result.orchBranchExists = true;
|
|
292
|
+
}
|
|
293
|
+
} catch (err) {
|
|
294
|
+
recordGitInspectionError(result, err, "list_merged_branches");
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
return result;
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/**
|
|
302
|
+
* @param {unknown} rawTasks
|
|
303
|
+
* @param {string|null} failedTaskId
|
|
304
|
+
* @returns {string|null}
|
|
305
|
+
*/
|
|
306
|
+
function resolveFailedExitReason(rawTasks, failedTaskId) {
|
|
307
|
+
if (!failedTaskId || !Array.isArray(rawTasks)) return null;
|
|
308
|
+
const match = rawTasks.find((entry) => {
|
|
309
|
+
if (!entry || typeof entry !== "object") return false;
|
|
310
|
+
return String(entry.taskId ?? entry.id ?? "") === failedTaskId;
|
|
311
|
+
});
|
|
312
|
+
if (!match || typeof match !== "object") return null;
|
|
313
|
+
const exitReason = /** @type {{ exitReason?: unknown }} */ (match).exitReason;
|
|
314
|
+
return typeof exitReason === "string" && exitReason ? exitReason : null;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
/**
|
|
318
|
+
* @param {string|null} failedTaskId
|
|
319
|
+
* @param {string|null} exitReason
|
|
320
|
+
* @param {object} signals
|
|
321
|
+
* @returns {{ exitReason: string|null, launchFailureKind: string|null }}
|
|
322
|
+
*/
|
|
323
|
+
function deriveFailureContext(failedTaskId, exitReason, signals) {
|
|
324
|
+
const resolvedExitReason =
|
|
325
|
+
exitReason ?? resolveFailedExitReason(signals.raw?.tasks, failedTaskId);
|
|
326
|
+
const launchFailureKind = inferLaunchFailureKind({
|
|
327
|
+
exitReason: resolvedExitReason,
|
|
328
|
+
journalEvents: signals.journalEvents,
|
|
329
|
+
failedTaskId,
|
|
330
|
+
});
|
|
331
|
+
return { exitReason: resolvedExitReason, launchFailureKind };
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
const WORKER_OUTPUT_TAIL_LINES = 20;
|
|
335
|
+
|
|
336
|
+
/**
|
|
337
|
+
* @param {string} filePath
|
|
338
|
+
* @param {number} [lineCount]
|
|
339
|
+
* @returns {string|null}
|
|
340
|
+
*/
|
|
341
|
+
function readWorkerOutputLogTail(filePath, lineCount = WORKER_OUTPUT_TAIL_LINES) {
|
|
342
|
+
if (!fs.existsSync(filePath)) return null;
|
|
343
|
+
const content = fs.readFileSync(filePath, "utf-8");
|
|
344
|
+
const lines = content.split("\n");
|
|
345
|
+
if (lines.at(-1) === "") lines.pop();
|
|
346
|
+
if (lines.length === 0) return null;
|
|
347
|
+
return lines.slice(-lineCount).join("\n");
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
/**
|
|
351
|
+
* @param {string} projectRoot
|
|
352
|
+
* @param {string} batchId
|
|
353
|
+
* @param {string|null} failedTaskId
|
|
354
|
+
* @param {Array<{ taskId: string, laneNumber?: number|null }>} tasks
|
|
355
|
+
* @param {string|null} launchFailureKind
|
|
356
|
+
* @returns {string|null}
|
|
357
|
+
*/
|
|
358
|
+
function enrichLaunchFailureFromWorkerOutput(projectRoot, batchId, failedTaskId, tasks, launchFailureKind) {
|
|
359
|
+
if (launchFailureKind || !failedTaskId || !batchId) return launchFailureKind;
|
|
360
|
+
const task = tasks.find((entry) => entry.taskId === failedTaskId);
|
|
361
|
+
if (!task || task.laneNumber == null) return launchFailureKind;
|
|
362
|
+
const logPath = workerOutputLogPath(projectRoot, batchId, task.laneNumber, failedTaskId);
|
|
363
|
+
const tail = readWorkerOutputLogTail(logPath);
|
|
364
|
+
return inferLaunchFailureFromWorkerOutputTail(tail) ?? launchFailureKind;
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
/**
|
|
368
|
+
* @param {Array<{ taskId: string, classification: string, laneNumber?: number|null }>} tasks
|
|
369
|
+
* @param {string|null} failedTaskId
|
|
370
|
+
* @returns {boolean}
|
|
371
|
+
*/
|
|
372
|
+
function hasGhostRunningCluster(tasks, failedTaskId) {
|
|
373
|
+
if (!failedTaskId) return false;
|
|
374
|
+
const task = tasks.find((entry) => entry.taskId === failedTaskId);
|
|
375
|
+
if (!task || task.laneNumber == null) return false;
|
|
376
|
+
const laneNumber = Number(task.laneNumber);
|
|
377
|
+
return (
|
|
378
|
+
tasks.filter(
|
|
379
|
+
(entry) =>
|
|
380
|
+
entry.classification === "running" && Number(entry.laneNumber) === laneNumber,
|
|
381
|
+
).length > 1
|
|
382
|
+
);
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
/**
|
|
386
|
+
* @param {string} diagnosis
|
|
387
|
+
* @param {string|null} failedTaskId
|
|
388
|
+
* @param {object} signals
|
|
389
|
+
* @param {string|null} [exitReason]
|
|
390
|
+
*/
|
|
391
|
+
function withFailureContext(diagnosis, failedTaskId, signals, exitReason = null) {
|
|
392
|
+
const context = deriveFailureContext(failedTaskId, exitReason, signals);
|
|
393
|
+
return {
|
|
394
|
+
diagnosis,
|
|
395
|
+
failedTaskId,
|
|
396
|
+
exitReason: context.exitReason,
|
|
397
|
+
launchFailureKind: context.launchFailureKind,
|
|
398
|
+
};
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
/**
|
|
402
|
+
* @param {object} signals
|
|
403
|
+
*/
|
|
404
|
+
function findNeedsReplanTask(signals) {
|
|
405
|
+
const tasks = signals.raw?.tasks ?? signals.tasks ?? [];
|
|
406
|
+
return (
|
|
407
|
+
tasks.find(
|
|
408
|
+
(task) =>
|
|
409
|
+
task?.exitReason === "needs_replan" &&
|
|
410
|
+
(task.status === "failed" || task.classification === "terminal-failure"),
|
|
411
|
+
) ?? null
|
|
412
|
+
);
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
/**
|
|
416
|
+
* @param {object} signals
|
|
417
|
+
*/
|
|
418
|
+
function hasNeedsReplanBlocker(signals) {
|
|
419
|
+
const tasks = signals.raw?.tasks ?? signals.tasks ?? [];
|
|
420
|
+
return tasks.some((task) => task?.exitReason === "needs_replan");
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
/**
|
|
424
|
+
* @param {object} signals
|
|
425
|
+
*/
|
|
426
|
+
export function deriveDiagnosis(signals) {
|
|
427
|
+
const {
|
|
428
|
+
phase,
|
|
429
|
+
endedAt,
|
|
430
|
+
failedTasks,
|
|
431
|
+
allTasksTerminalSuccess,
|
|
432
|
+
hasRunningTasks,
|
|
433
|
+
hasPendingTasks,
|
|
434
|
+
hasFailedTasks,
|
|
435
|
+
hasSegmentDrift,
|
|
436
|
+
failedTaskId,
|
|
437
|
+
mergeResultsEmpty,
|
|
438
|
+
git,
|
|
439
|
+
orphanRunning,
|
|
440
|
+
} = signals;
|
|
441
|
+
|
|
442
|
+
if (orphanRunning) {
|
|
443
|
+
if (orphanRunning.kind === "lane" && orphanRunning.taskId) {
|
|
444
|
+
return withFailureContext("worker_orphaned", orphanRunning.taskId, signals);
|
|
445
|
+
}
|
|
446
|
+
return withFailureContext("engine_orphaned", orphanRunning.taskId ?? null, signals);
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
if (signals.stateDrift?.drifted) {
|
|
450
|
+
const driftTask = signals.stateDrift.entries.find((entry) => entry.taskId !== "*");
|
|
451
|
+
return withFailureContext("state_drift", driftTask?.taskId ?? null, signals);
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
if (git?.gitInspectionError) {
|
|
455
|
+
return withFailureContext("git_unavailable", null, signals);
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
if (phase === "aborted") {
|
|
459
|
+
return withFailureContext("aborted", null, signals);
|
|
460
|
+
}
|
|
461
|
+
if (phase === "completed" && endedAt != null) {
|
|
462
|
+
if (git.orchBranchExists && !git.orchMergedToBase) {
|
|
463
|
+
return withFailureContext("needs_integrate", null, signals);
|
|
464
|
+
}
|
|
465
|
+
return withFailureContext("completed", null, signals);
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
const limboSignals =
|
|
469
|
+
allTasksTerminalSuccess &&
|
|
470
|
+
failedTasks === 0 &&
|
|
471
|
+
LIMBO_PHASES.has(phase) &&
|
|
472
|
+
endedAt == null &&
|
|
473
|
+
mergeResultsEmpty;
|
|
474
|
+
|
|
475
|
+
if (limboSignals && git.orchMergedToBase) {
|
|
476
|
+
return withFailureContext("completed_manual", null, signals);
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
if (limboSignals) {
|
|
480
|
+
return withFailureContext("limbo_stale", null, signals);
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
if (hasFailedTasks || hasSegmentDrift) {
|
|
484
|
+
const replanTask = findNeedsReplanTask(signals);
|
|
485
|
+
if (replanTask) {
|
|
486
|
+
return withFailureContext(
|
|
487
|
+
"needs_replan",
|
|
488
|
+
replanTask.taskId ?? failedTaskId,
|
|
489
|
+
signals,
|
|
490
|
+
"needs_replan",
|
|
491
|
+
);
|
|
492
|
+
}
|
|
493
|
+
return withFailureContext("needs_retry", failedTaskId, signals);
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
if (hasNeedsReplanBlocker(signals)) {
|
|
497
|
+
const replanTask = findNeedsReplanTask(signals);
|
|
498
|
+
return withFailureContext(
|
|
499
|
+
"needs_replan",
|
|
500
|
+
replanTask?.taskId ?? failedTaskId,
|
|
501
|
+
signals,
|
|
502
|
+
"needs_replan",
|
|
503
|
+
);
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
const postMergeLimbo = isPostMergeLimbo(signals.raw ?? {}, git);
|
|
507
|
+
signals.postMergeLimbo = postMergeLimbo;
|
|
508
|
+
|
|
509
|
+
if (postMergeLimbo) {
|
|
510
|
+
return withFailureContext("needs_integrate", null, signals);
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
if (phase === "merging" || (allTasksTerminalSuccess && mergeResultsEmpty && git.orchBranchExists && !git.orchMergedToBase)) {
|
|
514
|
+
if (allTasksTerminalSuccess && git.orchBranchExists && !git.orchMergedToBase && !mergeResultsEmpty) {
|
|
515
|
+
return withFailureContext("needs_integrate", null, signals);
|
|
516
|
+
}
|
|
517
|
+
return withFailureContext("needs_merge", null, signals);
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
if (allTasksTerminalSuccess && git.orchBranchExists && !git.orchMergedToBase && mergeResultsEmpty) {
|
|
521
|
+
return withFailureContext("needs_integrate", null, signals);
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
if (phase === "failed" || (failedTasks > 0 && !hasPendingTasks && !hasRunningTasks)) {
|
|
525
|
+
return withFailureContext("failed", failedTaskId, signals);
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
if (phase === "paused") {
|
|
529
|
+
return withFailureContext("paused", null, signals);
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
if (RUNNING_PHASES.has(phase) || hasRunningTasks || hasPendingTasks) {
|
|
533
|
+
return withFailureContext("running", null, signals);
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
return withFailureContext("paused", null, signals);
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
/**
|
|
540
|
+
* @param {object} ctx
|
|
541
|
+
* @param {string} ctx.projectRoot
|
|
542
|
+
* @param {object|null} [ctx.batchState]
|
|
543
|
+
* @param {string|null} [ctx.batchStatePath]
|
|
544
|
+
* @param {boolean} [ctx.verbose]
|
|
545
|
+
*/
|
|
546
|
+
export function reconcileBatch(ctx) {
|
|
547
|
+
const { projectRoot } = ctx;
|
|
548
|
+
const loaded = ctx.batchState
|
|
549
|
+
? {
|
|
550
|
+
path: ctx.batchStatePath ?? null,
|
|
551
|
+
raw: ctx.batchState,
|
|
552
|
+
parseError: null,
|
|
553
|
+
}
|
|
554
|
+
: loadBatchStateFile(projectRoot, ctx.batchStatePath ?? null);
|
|
555
|
+
|
|
556
|
+
if (!loaded.path && !loaded.raw) {
|
|
557
|
+
return {
|
|
558
|
+
diagnosis: null,
|
|
559
|
+
headline: "No active batch — ready to plan or start",
|
|
560
|
+
suggestedCommand: "spine preflight",
|
|
561
|
+
alternatives: ["spine plan all"],
|
|
562
|
+
batchId: null,
|
|
563
|
+
batchStatePath: null,
|
|
564
|
+
phase: null,
|
|
565
|
+
signals: { idle: true },
|
|
566
|
+
};
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
if (loaded.parseError) {
|
|
570
|
+
return {
|
|
571
|
+
diagnosis: "failed",
|
|
572
|
+
headline: `Cannot parse batch state: ${loaded.parseError}`,
|
|
573
|
+
suggestedCommand: "spine status --diagnose",
|
|
574
|
+
alternatives: ["spine doctor"],
|
|
575
|
+
batchId: null,
|
|
576
|
+
batchStatePath: loaded.path,
|
|
577
|
+
phase: null,
|
|
578
|
+
signals: { parseError: loaded.parseError },
|
|
579
|
+
};
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
const batch = parseBatchState(loaded.raw ?? ctx.batchState, loaded.path ?? ctx.batchStatePath ?? "");
|
|
583
|
+
if (!batch) {
|
|
584
|
+
return {
|
|
585
|
+
diagnosis: "failed",
|
|
586
|
+
headline: "Batch state is unreadable",
|
|
587
|
+
suggestedCommand: "spine status --diagnose",
|
|
588
|
+
batchId: null,
|
|
589
|
+
batchStatePath: loaded.path,
|
|
590
|
+
phase: null,
|
|
591
|
+
signals: { unreadable: true },
|
|
592
|
+
};
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
const tasksRoot = resolveTasksRoot(projectRoot);
|
|
596
|
+
const classifiedTasks = classifyTasks(batch, tasksRoot);
|
|
597
|
+
const git = inspectGitState({
|
|
598
|
+
projectRoot,
|
|
599
|
+
batchId: batch.batchId,
|
|
600
|
+
baseBranch: batch.baseBranch,
|
|
601
|
+
orchBranch: batch.orchBranch,
|
|
602
|
+
});
|
|
603
|
+
|
|
604
|
+
const hasRunningTasks = classifiedTasks.some((task) => task.classification === "running");
|
|
605
|
+
const hasPendingTasks = classifiedTasks.some((task) => task.classification === "pending");
|
|
606
|
+
const hasFailedTasks = classifiedTasks.some((task) => task.classification === "terminal-failure");
|
|
607
|
+
const allTasksTerminalSuccess =
|
|
608
|
+
classifiedTasks.length > 0 &&
|
|
609
|
+
classifiedTasks.every((task) => task.classification === "terminal-success");
|
|
610
|
+
const failedTask = classifiedTasks.find((task) => task.classification === "terminal-failure");
|
|
611
|
+
const pendingWithFailedSegment = batch.segments.some(
|
|
612
|
+
(segment) => segment.classification === "terminal-failure",
|
|
613
|
+
);
|
|
614
|
+
const driftTask = batch.tasks.find((task) => {
|
|
615
|
+
if (task.classification !== "pending") return false;
|
|
616
|
+
return batch.segments.some(
|
|
617
|
+
(segment) => segment.taskId === task.taskId && segment.classification === "terminal-failure",
|
|
618
|
+
);
|
|
619
|
+
});
|
|
620
|
+
|
|
621
|
+
const signals = {
|
|
622
|
+
phase: batch.phase,
|
|
623
|
+
endedAt: batch.endedAt,
|
|
624
|
+
failedTasks: hasFailedTasks ? Math.max(batch.failedTasks, 1) : batch.failedTasks,
|
|
625
|
+
allTasksTerminalSuccess,
|
|
626
|
+
hasRunningTasks,
|
|
627
|
+
hasPendingTasks,
|
|
628
|
+
hasFailedTasks,
|
|
629
|
+
hasSegmentDrift: pendingWithFailedSegment || Boolean(driftTask),
|
|
630
|
+
failedTaskId: failedTask?.taskId ?? driftTask?.taskId ?? null,
|
|
631
|
+
mergeResultsEmpty: batch.mergeResults.length === 0,
|
|
632
|
+
git,
|
|
633
|
+
tasks: classifiedTasks,
|
|
634
|
+
segments: batch.segments,
|
|
635
|
+
lanes: batch.lanes,
|
|
636
|
+
raw: batch.raw,
|
|
637
|
+
};
|
|
638
|
+
|
|
639
|
+
const journalFile = journalPath(projectRoot, batch.batchId);
|
|
640
|
+
/** @type {object[]} */
|
|
641
|
+
let journalEvents = [];
|
|
642
|
+
if (fs.existsSync(journalFile)) {
|
|
643
|
+
journalEvents = readJournalEvents(projectRoot, batch.batchId);
|
|
644
|
+
signals.journalHints = extractJournalDiagnosisHints(journalEvents);
|
|
645
|
+
signals.journalEvents = journalEvents;
|
|
646
|
+
}
|
|
647
|
+
|
|
648
|
+
const engineSessionJournalEvents = journalEventsSinceResume(journalEvents, batch.raw);
|
|
649
|
+
|
|
650
|
+
if (journalEvents.length > 0 && batch.raw) {
|
|
651
|
+
const rebuilt = rebuildBatchStateFromJournal(batch.raw, journalEvents);
|
|
652
|
+
const drift = detectBatchStateDrift(batch.raw, rebuilt, journalEvents);
|
|
653
|
+
signals.stateDrift = drift;
|
|
654
|
+
signals.rebuiltFromJournal = rebuilt;
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
signals.orphanRunning = detectOrphanRunning({
|
|
658
|
+
phase: batch.phase,
|
|
659
|
+
hasRunningTasks,
|
|
660
|
+
tasks: classifiedTasks,
|
|
661
|
+
lanes: batch.lanes,
|
|
662
|
+
raw: batch.raw,
|
|
663
|
+
journalEvents: engineSessionJournalEvents,
|
|
664
|
+
});
|
|
665
|
+
|
|
666
|
+
const pendingTaskCount = computePendingTasks(batch.raw ?? {}).length;
|
|
667
|
+
signals.pendingTaskCount = pendingTaskCount;
|
|
668
|
+
|
|
669
|
+
const { diagnosis, failedTaskId, exitReason, launchFailureKind } = deriveDiagnosis(signals);
|
|
670
|
+
const resolvedLaunchFailureKind =
|
|
671
|
+
diagnosis === "worker_orphaned"
|
|
672
|
+
? enrichLaunchFailureFromWorkerOutput(
|
|
673
|
+
projectRoot,
|
|
674
|
+
batch.batchId,
|
|
675
|
+
failedTaskId,
|
|
676
|
+
classifiedTasks,
|
|
677
|
+
launchFailureKind,
|
|
678
|
+
)
|
|
679
|
+
: launchFailureKind;
|
|
680
|
+
const ghostRunningCluster =
|
|
681
|
+
diagnosis === "worker_orphaned" && hasGhostRunningCluster(classifiedTasks, failedTaskId);
|
|
682
|
+
const salvagePayload = findLatestSalvageInspection(journalEvents, failedTaskId);
|
|
683
|
+
const salvageChangedFileCount = Number(salvagePayload?.changedFileCount ?? 0) || 0;
|
|
684
|
+
const salvageRetryCommand =
|
|
685
|
+
typeof salvagePayload?.retryCommand === "string"
|
|
686
|
+
? salvagePayload.retryCommand
|
|
687
|
+
: signals.orphanRunning?.taskId
|
|
688
|
+
? `spine batch retry ${signals.orphanRunning.taskId}`
|
|
689
|
+
: null;
|
|
690
|
+
|
|
691
|
+
const output = buildDiagnosisOutput(diagnosis, {
|
|
692
|
+
batchId: batch.batchId,
|
|
693
|
+
phase: batch.phase,
|
|
694
|
+
failedTasks: signals.failedTasks,
|
|
695
|
+
failedTaskId,
|
|
696
|
+
exitReason,
|
|
697
|
+
launchFailureKind: resolvedLaunchFailureKind,
|
|
698
|
+
gitMerged: git.orchMergedToBase,
|
|
699
|
+
pendingTaskCount,
|
|
700
|
+
salvageChangedFileCount,
|
|
701
|
+
salvageRetryCommand,
|
|
702
|
+
ghostRunningCluster,
|
|
703
|
+
postMergeLimbo: signals.postMergeLimbo === true,
|
|
704
|
+
});
|
|
705
|
+
|
|
706
|
+
if (diagnosis === "git_unavailable") {
|
|
707
|
+
output.headline = `Batch ${batch.batchId} — git inspection failed: ${git.gitInspectionError}`;
|
|
708
|
+
output.suggestedCommand = "spine doctor";
|
|
709
|
+
output.alternatives = ["spine status --diagnose"];
|
|
710
|
+
}
|
|
711
|
+
|
|
712
|
+
return {
|
|
713
|
+
...output,
|
|
714
|
+
batchId: batch.batchId,
|
|
715
|
+
batchStatePath: loaded.path ?? ctx.batchStatePath ?? null,
|
|
716
|
+
phase: batch.phase,
|
|
717
|
+
signals: ctx.verbose ? signals : undefined,
|
|
718
|
+
};
|
|
719
|
+
}
|
|
720
|
+
|
|
721
|
+
/**
|
|
722
|
+
* @param {object} ctx
|
|
723
|
+
* @param {string} ctx.projectRoot
|
|
724
|
+
* @param {object|null} [ctx.batchState]
|
|
725
|
+
* @param {string|null} [ctx.batchStatePath]
|
|
726
|
+
*/
|
|
727
|
+
export function runReconciliationCheck(ctx) {
|
|
728
|
+
return reconcileBatch(ctx);
|
|
729
|
+
}
|