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
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
// @ts-nocheck
|
|
2
|
+
/**
|
|
3
|
+
* Attached pause/resume reconcile paths — handoff locks, single-owner enforce,
|
|
4
|
+
* and paused-resume `.DONE` promotion (SP-604).
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import fs from "node:fs";
|
|
8
|
+
import path from "node:path";
|
|
9
|
+
import { isProcessAlive } from "../process/liveness.mjs";
|
|
10
|
+
import { terminateStaleDetachedEngine } from "./resume-engine.mjs";
|
|
11
|
+
import { appendJournalEvent, readJournalEvents } from "./journal.mjs";
|
|
12
|
+
import {
|
|
13
|
+
loadSpineBatchState,
|
|
14
|
+
readBatchEnginePid,
|
|
15
|
+
saveSpineBatchState,
|
|
16
|
+
updateSegmentForTask,
|
|
17
|
+
recomputeTaskCounters,
|
|
18
|
+
clearBatchEnginePid,
|
|
19
|
+
} from "./state.mjs";
|
|
20
|
+
import { loadSpineConfig } from "../config/spine-config-load.mjs";
|
|
21
|
+
import {
|
|
22
|
+
journalHasContractVerified,
|
|
23
|
+
journalIndicatesPausedForceResume,
|
|
24
|
+
} from "./orphan-detect.mjs";
|
|
25
|
+
import { classifyTaskDoneSemantics } from "./diagnosis-task-done.mjs";
|
|
26
|
+
import { laneDoneMarkerReadyForPromote } from "./journal-rebuild.mjs";
|
|
27
|
+
import { resolveTasksRoot } from "../config/spine-preflight-lib.mjs";
|
|
28
|
+
import { journalHasTaskCompleted } from "./resume-common.mjs";
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* @param {string} projectRoot
|
|
32
|
+
* @param {string} batchId
|
|
33
|
+
*/
|
|
34
|
+
export function resumeHandoffLockPath(projectRoot, batchId) {
|
|
35
|
+
return path.join(projectRoot, ".spine", "runtime", batchId, "resume-handoff.lock");
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Exclusive lock for forced resume handoff (SP-533 / #167).
|
|
40
|
+
*
|
|
41
|
+
* @param {string} projectRoot
|
|
42
|
+
* @param {string} batchId
|
|
43
|
+
* @param {boolean} [allowRetry]
|
|
44
|
+
* @returns {{ ok: true, release: () => void } | { ok: false, holderPid?: number|null, startedAt?: number }}
|
|
45
|
+
*/
|
|
46
|
+
export function tryAcquireResumeHandoffLock(projectRoot, batchId, allowRetry = true) {
|
|
47
|
+
const lockPath = resumeHandoffLockPath(projectRoot, batchId);
|
|
48
|
+
fs.mkdirSync(path.dirname(lockPath), { recursive: true });
|
|
49
|
+
const payload = JSON.stringify({ pid: process.pid, startedAt: Date.now() });
|
|
50
|
+
|
|
51
|
+
try {
|
|
52
|
+
fs.writeFileSync(lockPath, payload, { encoding: "utf-8", flag: "wx" });
|
|
53
|
+
return {
|
|
54
|
+
ok: true,
|
|
55
|
+
release: () => releaseResumeHandoffLock(projectRoot, batchId),
|
|
56
|
+
};
|
|
57
|
+
} catch (err) {
|
|
58
|
+
if (/** @type {NodeJS.ErrnoException} */ (err).code !== "EEXIST") {
|
|
59
|
+
throw err;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** @type {{ pid?: number, startedAt?: number } | null} */
|
|
64
|
+
let holder = null;
|
|
65
|
+
try {
|
|
66
|
+
holder = JSON.parse(fs.readFileSync(lockPath, "utf-8"));
|
|
67
|
+
} catch {
|
|
68
|
+
try {
|
|
69
|
+
fs.unlinkSync(lockPath);
|
|
70
|
+
} catch {
|
|
71
|
+
/* ignore stale corrupt lock cleanup */
|
|
72
|
+
}
|
|
73
|
+
if (allowRetry) {
|
|
74
|
+
return tryAcquireResumeHandoffLock(projectRoot, batchId, false);
|
|
75
|
+
}
|
|
76
|
+
return { ok: false, holderPid: null };
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const holderPid = Number(holder?.pid);
|
|
80
|
+
if (Number.isFinite(holderPid) && holderPid > 0 && isProcessAlive(holderPid)) {
|
|
81
|
+
return {
|
|
82
|
+
ok: false,
|
|
83
|
+
holderPid,
|
|
84
|
+
startedAt: Number(holder?.startedAt) || undefined,
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
try {
|
|
89
|
+
fs.unlinkSync(lockPath);
|
|
90
|
+
} catch {
|
|
91
|
+
/* ignore stale lock cleanup */
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
try {
|
|
95
|
+
fs.writeFileSync(lockPath, payload, { encoding: "utf-8", flag: "wx" });
|
|
96
|
+
return {
|
|
97
|
+
ok: true,
|
|
98
|
+
release: () => releaseResumeHandoffLock(projectRoot, batchId),
|
|
99
|
+
};
|
|
100
|
+
} catch (err) {
|
|
101
|
+
if (/** @type {NodeJS.ErrnoException} */ (err).code === "EEXIST") {
|
|
102
|
+
return { ok: false, holderPid: Number.isFinite(holderPid) ? holderPid : null };
|
|
103
|
+
}
|
|
104
|
+
throw err;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* @param {string} projectRoot
|
|
110
|
+
* @param {string} batchId
|
|
111
|
+
*/
|
|
112
|
+
export function releaseResumeHandoffLock(projectRoot, batchId) {
|
|
113
|
+
const lockPath = resumeHandoffLockPath(projectRoot, batchId);
|
|
114
|
+
try {
|
|
115
|
+
if (!fs.existsSync(lockPath)) return;
|
|
116
|
+
const holder = JSON.parse(fs.readFileSync(lockPath, "utf-8"));
|
|
117
|
+
if (Number(holder?.pid) === process.pid) {
|
|
118
|
+
fs.unlinkSync(lockPath);
|
|
119
|
+
}
|
|
120
|
+
} catch {
|
|
121
|
+
/* ignore release races */
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Reject a second attached engine when resilience.enginePid is alive (SP-434, GitHub #89).
|
|
127
|
+
* Serialized forced resume handoff when another resume --force is in flight (SP-533, #167).
|
|
128
|
+
*
|
|
129
|
+
* @param {object} params
|
|
130
|
+
* @param {string} params.projectRoot
|
|
131
|
+
* @param {boolean} [params.force] Orphan-terminate prior engine before handoff
|
|
132
|
+
* @param {"start"|"resume"} [params.operation]
|
|
133
|
+
*/
|
|
134
|
+
export function enforceAttachedEngineSingleOwner({ projectRoot, force = false, operation = "resume" }) {
|
|
135
|
+
const loaded = loadSpineBatchState(projectRoot);
|
|
136
|
+
const state = loaded.raw;
|
|
137
|
+
if (!state) {
|
|
138
|
+
return { ok: true };
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
const batchId = String(state.batchId ?? "");
|
|
142
|
+
/** @type {(() => void) | undefined} */
|
|
143
|
+
let releaseResumeLock;
|
|
144
|
+
|
|
145
|
+
if (force && operation === "resume") {
|
|
146
|
+
const lock = tryAcquireResumeHandoffLock(projectRoot, batchId);
|
|
147
|
+
if (!lock.ok) {
|
|
148
|
+
const enginePid = readBatchEnginePid(state);
|
|
149
|
+
const holderPid = lock.holderPid ?? null;
|
|
150
|
+
const output =
|
|
151
|
+
`Another batch resume --force is already in progress (batch ${batchId}` +
|
|
152
|
+
`${holderPid ? `, holder PID ${holderPid}` : ""}).\n` +
|
|
153
|
+
`Wait for the in-flight forced resume to finish before starting another.\n`;
|
|
154
|
+
return {
|
|
155
|
+
ok: false,
|
|
156
|
+
exitCode: 1,
|
|
157
|
+
error: "concurrent_resume_blocked",
|
|
158
|
+
output,
|
|
159
|
+
batchId,
|
|
160
|
+
enginePid,
|
|
161
|
+
holderPid,
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
releaseResumeLock = lock.release;
|
|
165
|
+
appendJournalEvent(projectRoot, batchId, "batch.resume_handoff_started", {
|
|
166
|
+
pid: process.pid,
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
const enginePid = readBatchEnginePid(state);
|
|
171
|
+
if (enginePid == null || enginePid === process.pid || !isProcessAlive(enginePid)) {
|
|
172
|
+
return releaseResumeLock ? { ok: true, releaseResumeLock } : { ok: true };
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
const fromPhase = String(state.phase ?? "");
|
|
176
|
+
if (force) {
|
|
177
|
+
const terminateResult = terminateStaleDetachedEngine({
|
|
178
|
+
projectRoot,
|
|
179
|
+
state,
|
|
180
|
+
batchId,
|
|
181
|
+
fromPhase,
|
|
182
|
+
allowRunningOrphanTerminate: true,
|
|
183
|
+
});
|
|
184
|
+
saveSpineBatchState(projectRoot, state, { bypassWriteGuard: true });
|
|
185
|
+
return {
|
|
186
|
+
ok: true,
|
|
187
|
+
handoff: true,
|
|
188
|
+
terminated: terminateResult.terminated,
|
|
189
|
+
stalePid: terminateResult.stalePid ?? enginePid,
|
|
190
|
+
releaseResumeLock,
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
const operationLabel = operation === "start" ? "start" : "resume";
|
|
195
|
+
const output =
|
|
196
|
+
`Attached batch engine already running (PID ${enginePid}, batch ${batchId}).\n` +
|
|
197
|
+
`Stop the existing engine or run spine batch ${operationLabel} --attached --force to orphan it first.\n`;
|
|
198
|
+
return {
|
|
199
|
+
ok: false,
|
|
200
|
+
exitCode: 1,
|
|
201
|
+
error: "attached_engine_already_running",
|
|
202
|
+
output,
|
|
203
|
+
batchId,
|
|
204
|
+
enginePid,
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* Promote running cache tasks with lane `.DONE` and contract verify after paused force resume (SP-513 / #184).
|
|
210
|
+
*
|
|
211
|
+
* @param {object} params
|
|
212
|
+
* @param {string} params.projectRoot
|
|
213
|
+
* @param {object} params.state
|
|
214
|
+
* @param {string} params.batchId
|
|
215
|
+
* @returns {{ reconciled: boolean, taskIds?: string[] }}
|
|
216
|
+
*/
|
|
217
|
+
export function reconcilePausedResumeDoneInLane({ projectRoot, state, batchId }) {
|
|
218
|
+
if (!state || typeof state !== "object" || !batchId) {
|
|
219
|
+
return { reconciled: false };
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
const journalEvents = readJournalEvents(projectRoot, batchId);
|
|
223
|
+
if (!journalIndicatesPausedForceResume(journalEvents)) {
|
|
224
|
+
return { reconciled: false };
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
const configResult = loadSpineConfig(projectRoot);
|
|
228
|
+
const tasksRoot = resolveTasksRoot(projectRoot, configResult);
|
|
229
|
+
const lanes = state.lanes ?? [];
|
|
230
|
+
/** @type {string[]} */
|
|
231
|
+
const promotedTaskIds = [];
|
|
232
|
+
let changed = false;
|
|
233
|
+
|
|
234
|
+
for (const task of state.tasks ?? []) {
|
|
235
|
+
const status = String(task?.status ?? "").toLowerCase();
|
|
236
|
+
if (status !== "running" && status !== "pending") continue;
|
|
237
|
+
|
|
238
|
+
const classified = classifyTaskDoneSemantics(task, {
|
|
239
|
+
tasksRoot,
|
|
240
|
+
projectRoot,
|
|
241
|
+
batchId,
|
|
242
|
+
lanes,
|
|
243
|
+
});
|
|
244
|
+
if (
|
|
245
|
+
!laneDoneMarkerReadyForPromote({
|
|
246
|
+
projectRoot,
|
|
247
|
+
batchId,
|
|
248
|
+
task,
|
|
249
|
+
lanes,
|
|
250
|
+
classified,
|
|
251
|
+
})
|
|
252
|
+
) {
|
|
253
|
+
continue;
|
|
254
|
+
}
|
|
255
|
+
if (!journalHasContractVerified(journalEvents, task.taskId)) continue;
|
|
256
|
+
|
|
257
|
+
task.status = "succeeded";
|
|
258
|
+
task.doneFileFound = true;
|
|
259
|
+
task.exitReason = task.exitReason ?? "done";
|
|
260
|
+
if (!task.endedAt) task.endedAt = Date.now();
|
|
261
|
+
updateSegmentForTask(state, task.taskId, "succeeded");
|
|
262
|
+
promotedTaskIds.push(task.taskId);
|
|
263
|
+
changed = true;
|
|
264
|
+
|
|
265
|
+
if (!journalHasTaskCompleted(journalEvents, task.taskId)) {
|
|
266
|
+
const laneNumber = Number(task.laneNumber ?? 1);
|
|
267
|
+
const lane = lanes.find((entry) => Number(entry?.laneNumber) === laneNumber);
|
|
268
|
+
appendJournalEvent(projectRoot, batchId, "task.completed", {
|
|
269
|
+
taskId: task.taskId,
|
|
270
|
+
laneNumber,
|
|
271
|
+
laneId: lane?.laneId ?? `lane-${laneNumber}`,
|
|
272
|
+
resumed: true,
|
|
273
|
+
skippedDoneOnDisk: true,
|
|
274
|
+
reconciled: true,
|
|
275
|
+
});
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
if (!changed) {
|
|
280
|
+
return { reconciled: false };
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
const enginePid = readBatchEnginePid(state);
|
|
284
|
+
if (enginePid != null && !isProcessAlive(enginePid)) {
|
|
285
|
+
clearBatchEnginePid(state);
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
recomputeTaskCounters(state);
|
|
289
|
+
saveSpineBatchState(projectRoot, state);
|
|
290
|
+
return { reconciled: true, taskIds: promotedTaskIds };
|
|
291
|
+
}
|