pi-spine 2.1.0 → 2.3.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 (49) hide show
  1. package/bin/spine-batch.mjs +74 -5
  2. package/bin/spine-cli/verify.mjs +2 -18
  3. package/package.json +1 -1
  4. package/src/batch/attached-runner-promote.mjs +370 -0
  5. package/src/batch/attached-runner-reconcile.mjs +291 -0
  6. package/src/batch/attached-runner.mjs +24 -634
  7. package/src/batch/batch-guards.mjs +69 -0
  8. package/src/batch/contract-exec.mjs +483 -0
  9. package/src/batch/contract-parse.mjs +243 -0
  10. package/src/batch/contract-verify.mjs +32 -707
  11. package/src/batch/detached-diagnostics.mjs +255 -0
  12. package/src/batch/detached-run.mjs +326 -0
  13. package/src/batch/detached-start.mjs +18 -894
  14. package/src/batch/detached-wait.mjs +346 -0
  15. package/src/batch/engine-lanes/merge.mjs +25 -0
  16. package/src/batch/engine.mjs +8 -67
  17. package/src/batch/integrate-git.mjs +21 -0
  18. package/src/batch/integrate.mjs +5 -15
  19. package/src/batch/journal-rebuild-structural.mjs +352 -0
  20. package/src/batch/journal-rebuild.mjs +94 -347
  21. package/src/batch/lane-dirty-check-commit.mjs +361 -0
  22. package/src/batch/lane-dirty-check-git.mjs +298 -0
  23. package/src/batch/lane-dirty-check.mjs +31 -297
  24. package/src/batch/reconcile-batch.mjs +483 -0
  25. package/src/batch/reconcile-classify.mjs +433 -0
  26. package/src/batch/reconcile-diagnosis.mjs +459 -0
  27. package/src/batch/reconcile-light-cache.mjs +61 -0
  28. package/src/batch/reconcile-orphan.mjs +198 -0
  29. package/src/batch/reconcile.mjs +19 -1693
  30. package/src/batch/resume-multi-lanes.mjs +44 -149
  31. package/src/batch/resume-multi-queue.mjs +159 -0
  32. package/src/batch/resume-single-validate.mjs +25 -0
  33. package/src/batch/resume.mjs +3 -20
  34. package/src/batch/review-artifacts.mjs +419 -0
  35. package/src/batch/review-step-run.mjs +383 -0
  36. package/src/batch/review-step.mjs +451 -0
  37. package/src/batch/review.mjs +32 -1199
  38. package/src/batch/salvage-batch-list.mjs +291 -0
  39. package/src/batch/salvage-batch.mjs +417 -0
  40. package/src/batch/sequence-plan.mjs +234 -0
  41. package/src/batch/sequence-run.mjs +418 -0
  42. package/src/batch/sequence-wait.mjs +167 -0
  43. package/src/batch/sequence.mjs +22 -783
  44. package/src/batch/state-guards.mjs +284 -0
  45. package/src/batch/state-io.mjs +135 -0
  46. package/src/batch/state.mjs +27 -396
  47. package/src/batch/worker-heartbeat.mjs +375 -0
  48. package/src/batch/worker-host.mjs +50 -541
  49. package/src/batch/worker-spawn.mjs +315 -0
@@ -0,0 +1,375 @@
1
+ // @ts-check
2
+ /**
3
+ * pi-spine worker heartbeat — polling, stall detection, and progress heartbeats (SP-599).
4
+ */
5
+
6
+ import fs from "node:fs";
7
+ import { readAbortSignal } from "./abort.mjs";
8
+ import {
9
+ activitySignalsChanged,
10
+ checkpointSignalsChanged,
11
+ collectProgressSignals,
12
+ computeStallDeadline,
13
+ recordCheckpointWarning,
14
+ recordLaneHeartbeat,
15
+ recordLaneProgressSnapshot,
16
+ recordStallWarning,
17
+ resolveHeartbeatKind,
18
+ resolveStallConfig as _resolveStallConfig,
19
+ shouldEmitCheckpointWarning,
20
+ shouldEmitProgressSnapshot,
21
+ buildProgressSnapshotPayload,
22
+ progressSnapshotPayloadChanged,
23
+ } from "./heartbeat.mjs";
24
+ import { appendJournalEvent } from "./journal.mjs";
25
+ import { nextStallAnchorAt } from "./engine-lanes/watch.mjs";
26
+ import {
27
+ resolveWorkerPhase,
28
+ terminateHungWorkerChild,
29
+ } from "./worker-spawn.mjs";
30
+
31
+ /** @typedef {import("./worker-spawn.mjs").WorkerPhase} WorkerPhase */
32
+ /** @typedef {import("./worker-spawn.mjs").WorkerChildHandle} WorkerChildHandle */
33
+ /** @typedef {ReturnType<typeof _resolveStallConfig>} StallConfig */
34
+
35
+ /**
36
+ * @param {number} ms
37
+ */
38
+ function sleep(ms) {
39
+ return new Promise((resolve) => setTimeout(resolve, ms));
40
+ }
41
+
42
+ /**
43
+ * @typedef {object} WorkerPollState
44
+ * @property {number} stallAnchorAt
45
+ * @property {number} lastCheckpointAt
46
+ * @property {number} lastHeartbeatAt
47
+ * @property {number} lastProgressSnapshotAt
48
+ * @property {object | null} lastSnapshotPayload
49
+ * @property {object | null} lastSignals
50
+ * @property {boolean} activitySinceCheckpoint
51
+ * @property {boolean} checkpointWarningSent
52
+ * @property {boolean} stallWarningSent
53
+ * @property {number | null} postDoneStartedAt
54
+ * @property {boolean} postDoneTerminated
55
+ * @property {WorkerPhase} workerPhase
56
+ */
57
+
58
+ /**
59
+ * @param {number} startedAt
60
+ * @param {WorkerPhase} workerPhase
61
+ * @returns {WorkerPollState}
62
+ */
63
+ export function createWorkerPollState(startedAt, workerPhase) {
64
+ return {
65
+ stallAnchorAt: startedAt,
66
+ lastCheckpointAt: startedAt,
67
+ lastHeartbeatAt: 0,
68
+ lastProgressSnapshotAt: 0,
69
+ lastSnapshotPayload: null,
70
+ lastSignals: null,
71
+ activitySinceCheckpoint: false,
72
+ checkpointWarningSent: false,
73
+ stallWarningSent: false,
74
+ postDoneStartedAt: null,
75
+ postDoneTerminated: false,
76
+ workerPhase,
77
+ };
78
+ }
79
+
80
+ /**
81
+ * Poll worker child until .DONE + exit, stall timeout, or abort.
82
+ *
83
+ * @param {object} params
84
+ * @param {string} params.donePath
85
+ * @param {WorkerChildHandle} params.workerChild
86
+ * @param {Promise<{ exitCode: number; output: string }>} params.childDone
87
+ * @param {StallConfig} params.stallConfig
88
+ * @param {number} params.startedAt
89
+ * @param {WorkerPollState} params.pollState
90
+ * @param {string} params.worktreePath
91
+ * @param {string} params.taskFolder
92
+ * @param {string} [params.laneBranch]
93
+ * @param {string[]} [params.fileScopePaths]
94
+ * @param {string} [params.projectRoot]
95
+ * @param {string} [params.batchId]
96
+ * @param {number} [params.laneNumber]
97
+ * @param {string} [params.taskId]
98
+ * @param {string} [params.laneCorrelationId]
99
+ * @param {boolean} params.useStub
100
+ * @param {string} params.workerBackend
101
+ * @param {boolean} params.childPastPreflight
102
+ * @param {(timestamp: number) => void} [params.onHeartbeat]
103
+ * @param {(failure: {
104
+ * rawOutput: string;
105
+ * classification: string;
106
+ * exitCode: number;
107
+ * mode: string;
108
+ * doneFound: boolean;
109
+ * stallDeadline?: number;
110
+ * signals?: object;
111
+ * }) => object} params.buildFailureResult
112
+ * @param {string} params.workerMode
113
+ * @returns {Promise<
114
+ * | { kind: "settled"; postDoneTerminated: boolean }
115
+ * | { kind: "failure"; result: object }
116
+ * >}
117
+ */
118
+ export async function pollWorkerUntilSettled({
119
+ donePath,
120
+ workerChild,
121
+ childDone,
122
+ stallConfig,
123
+ startedAt,
124
+ pollState,
125
+ worktreePath,
126
+ taskFolder,
127
+ laneBranch,
128
+ fileScopePaths = [],
129
+ projectRoot,
130
+ batchId,
131
+ laneNumber,
132
+ taskId,
133
+ laneCorrelationId,
134
+ useStub,
135
+ workerBackend,
136
+ childPastPreflight,
137
+ onHeartbeat,
138
+ buildFailureResult,
139
+ workerMode,
140
+ }) {
141
+ let {
142
+ stallAnchorAt,
143
+ lastCheckpointAt,
144
+ lastHeartbeatAt,
145
+ lastProgressSnapshotAt,
146
+ lastSnapshotPayload,
147
+ lastSignals,
148
+ activitySinceCheckpoint,
149
+ checkpointWarningSent,
150
+ stallWarningSent,
151
+ postDoneStartedAt,
152
+ postDoneTerminated,
153
+ workerPhase,
154
+ } = pollState;
155
+
156
+ while (true) {
157
+ const doneOnDisk = fs.existsSync(donePath);
158
+ const now = Date.now();
159
+
160
+ if (doneOnDisk && postDoneStartedAt === null) {
161
+ postDoneStartedAt = now;
162
+ }
163
+
164
+ if (projectRoot && batchId) {
165
+ const abortSignal = readAbortSignal(projectRoot, batchId);
166
+ if (abortSignal) {
167
+ const hard = Boolean(abortSignal.hard);
168
+ workerChild.kill(hard ? "SIGKILL" : "SIGTERM");
169
+ const { output } = await childDone;
170
+ return {
171
+ kind: "failure",
172
+ result: buildFailureResult({
173
+ rawOutput: output,
174
+ classification: "aborted",
175
+ exitCode: hard ? 137 : 130,
176
+ mode: workerMode,
177
+ doneFound: fs.existsSync(donePath),
178
+ }),
179
+ };
180
+ }
181
+ }
182
+
183
+ if (doneOnDisk) {
184
+ if (workerChild.exitCode !== null) {
185
+ break;
186
+ }
187
+ const graceElapsed = now - (postDoneStartedAt ?? now);
188
+ if (graceElapsed >= stallConfig.postDoneGraceMs) {
189
+ if (projectRoot && batchId) {
190
+ appendJournalEvent(projectRoot, batchId, "worker.post_done_terminated", {
191
+ laneNumber,
192
+ taskId,
193
+ correlationId: laneCorrelationId,
194
+ graceElapsedMs: graceElapsed,
195
+ postDoneGraceMs: stallConfig.postDoneGraceMs,
196
+ childPid: workerChild.pid ?? null,
197
+ });
198
+ }
199
+ postDoneTerminated = true;
200
+ await terminateHungWorkerChild(workerChild, childDone);
201
+ break;
202
+ }
203
+ await sleep(Math.min(stallConfig.pollIntervalMs, 5_000));
204
+ continue;
205
+ }
206
+
207
+ const signals = collectProgressSignals(
208
+ /** @type {Parameters<typeof collectProgressSignals>[0]} */ ({
209
+ worktreePath,
210
+ taskFolder,
211
+ laneBranch,
212
+ fileScopePaths,
213
+ journalContext:
214
+ projectRoot && batchId
215
+ ? { projectRoot, batchId, laneNumber, taskId }
216
+ : undefined,
217
+ }),
218
+ );
219
+ const nextWorkerPhase = resolveWorkerPhase({ childPastPreflight, useStub, workerBackend });
220
+ if (nextWorkerPhase !== "launching" && workerPhase === "launching") {
221
+ stallAnchorAt = now;
222
+ lastCheckpointAt = now;
223
+ activitySinceCheckpoint = false;
224
+ checkpointWarningSent = false;
225
+ lastSignals = null;
226
+ lastSnapshotPayload = null;
227
+ }
228
+ workerPhase = nextWorkerPhase;
229
+
230
+ const checkpointChanged =
231
+ workerPhase !== "launching" && checkpointSignalsChanged(lastSignals, signals);
232
+ const activityChanged =
233
+ workerPhase !== "launching" && activitySignalsChanged(lastSignals, signals);
234
+
235
+ if (checkpointChanged) {
236
+ lastCheckpointAt = now;
237
+ activitySinceCheckpoint = false;
238
+ checkpointWarningSent = false;
239
+ } else if (activityChanged) {
240
+ activitySinceCheckpoint = true;
241
+ if (stallConfig.extendGraceOnFileScope) {
242
+ lastCheckpointAt = now;
243
+ }
244
+ }
245
+
246
+ if (
247
+ projectRoot &&
248
+ batchId &&
249
+ !checkpointWarningSent &&
250
+ shouldEmitCheckpointWarning({
251
+ now,
252
+ lastCheckpointAt,
253
+ signals,
254
+ stallConfig,
255
+ activitySinceCheckpoint,
256
+ workerPhase,
257
+ })
258
+ ) {
259
+ recordCheckpointWarning({
260
+ projectRoot,
261
+ batchId,
262
+ laneNumber,
263
+ taskId,
264
+ signals,
265
+ lastCheckpointAt,
266
+ correlationId: laneCorrelationId,
267
+ });
268
+ checkpointWarningSent = true;
269
+ }
270
+
271
+ lastSignals = signals;
272
+
273
+ if (
274
+ projectRoot &&
275
+ batchId &&
276
+ shouldEmitProgressSnapshot({
277
+ now,
278
+ lastEmittedAt: lastProgressSnapshotAt,
279
+ intervalMs: stallConfig.progressSnapshotIntervalMs,
280
+ })
281
+ ) {
282
+ const snapshotPayload = buildProgressSnapshotPayload(signals, workerPhase);
283
+ if (progressSnapshotPayloadChanged(lastSnapshotPayload, snapshotPayload)) {
284
+ recordLaneProgressSnapshot({
285
+ projectRoot,
286
+ batchId,
287
+ laneNumber,
288
+ taskId,
289
+ signals,
290
+ correlationId: laneCorrelationId,
291
+ workerPhase,
292
+ });
293
+ lastSnapshotPayload = snapshotPayload;
294
+ }
295
+ lastProgressSnapshotAt = now;
296
+ }
297
+
298
+ if (
299
+ projectRoot &&
300
+ batchId &&
301
+ now - lastHeartbeatAt >= stallConfig.heartbeatIntervalMs
302
+ ) {
303
+ const heartbeatKind = resolveHeartbeatKind(
304
+ /** @type {Parameters<typeof resolveHeartbeatKind>[0]} */ ({
305
+ workerPhase,
306
+ checkpointChanged,
307
+ activityChanged,
308
+ }),
309
+ );
310
+ recordLaneHeartbeat(
311
+ /** @type {Parameters<typeof recordLaneHeartbeat>[0]} */ ({
312
+ projectRoot,
313
+ batchId,
314
+ laneNumber,
315
+ taskId,
316
+ signals,
317
+ correlationId: laneCorrelationId,
318
+ workerPhase,
319
+ heartbeatKind,
320
+ }),
321
+ );
322
+ stallAnchorAt = nextStallAnchorAt({
323
+ stallAnchorAt,
324
+ now,
325
+ workerPhase,
326
+ heartbeatKind,
327
+ });
328
+ onHeartbeat?.(now);
329
+ lastHeartbeatAt = now;
330
+ }
331
+
332
+ const stallDeadline = computeStallDeadline({
333
+ startedAt,
334
+ lastProgressAt: lastCheckpointAt,
335
+ lastAliveAt: stallAnchorAt,
336
+ stallConfig,
337
+ });
338
+
339
+ if (now >= stallDeadline) {
340
+ if (!stallWarningSent && projectRoot && batchId) {
341
+ recordStallWarning({
342
+ projectRoot,
343
+ batchId,
344
+ laneNumber,
345
+ taskId,
346
+ signals,
347
+ stallDeadline,
348
+ correlationId: laneCorrelationId,
349
+ });
350
+ stallWarningSent = true;
351
+ }
352
+ const { output } = await terminateHungWorkerChild(workerChild, childDone);
353
+ return {
354
+ kind: "failure",
355
+ result: buildFailureResult({
356
+ rawOutput: output,
357
+ classification: "stall_timeout",
358
+ exitCode: 124,
359
+ mode: workerMode,
360
+ doneFound: fs.existsSync(donePath),
361
+ stallDeadline,
362
+ signals,
363
+ }),
364
+ };
365
+ }
366
+
367
+ if (workerChild.exitCode !== null) {
368
+ break;
369
+ }
370
+
371
+ await sleep(Math.min(stallConfig.pollIntervalMs, 5_000));
372
+ }
373
+
374
+ return { kind: "settled", postDoneTerminated };
375
+ }