pi-spine 2.2.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 (47) hide show
  1. package/bin/spine-cli/verify.mjs +2 -19
  2. package/package.json +1 -1
  3. package/src/batch/attached-runner-promote.mjs +370 -0
  4. package/src/batch/attached-runner-reconcile.mjs +291 -0
  5. package/src/batch/attached-runner.mjs +24 -645
  6. package/src/batch/batch-guards.mjs +69 -0
  7. package/src/batch/contract-exec.mjs +483 -0
  8. package/src/batch/contract-parse.mjs +243 -0
  9. package/src/batch/contract-verify.mjs +32 -707
  10. package/src/batch/detached-diagnostics.mjs +255 -0
  11. package/src/batch/detached-run.mjs +326 -0
  12. package/src/batch/detached-start.mjs +18 -894
  13. package/src/batch/detached-wait.mjs +346 -0
  14. package/src/batch/engine.mjs +8 -67
  15. package/src/batch/integrate-git.mjs +21 -0
  16. package/src/batch/integrate.mjs +5 -15
  17. package/src/batch/journal-rebuild-structural.mjs +352 -0
  18. package/src/batch/journal-rebuild.mjs +16 -348
  19. package/src/batch/lane-dirty-check-commit.mjs +361 -0
  20. package/src/batch/lane-dirty-check-git.mjs +298 -0
  21. package/src/batch/lane-dirty-check.mjs +31 -297
  22. package/src/batch/reconcile-batch.mjs +483 -0
  23. package/src/batch/reconcile-classify.mjs +433 -0
  24. package/src/batch/reconcile-diagnosis.mjs +459 -0
  25. package/src/batch/reconcile-light-cache.mjs +61 -0
  26. package/src/batch/reconcile-orphan.mjs +198 -0
  27. package/src/batch/reconcile.mjs +19 -1693
  28. package/src/batch/resume-multi-lanes.mjs +6 -148
  29. package/src/batch/resume-multi-queue.mjs +159 -0
  30. package/src/batch/resume-single-validate.mjs +25 -0
  31. package/src/batch/resume.mjs +3 -20
  32. package/src/batch/review-artifacts.mjs +419 -0
  33. package/src/batch/review-step-run.mjs +383 -0
  34. package/src/batch/review-step.mjs +451 -0
  35. package/src/batch/review.mjs +32 -1199
  36. package/src/batch/salvage-batch-list.mjs +291 -0
  37. package/src/batch/salvage-batch.mjs +10 -284
  38. package/src/batch/sequence-plan.mjs +234 -0
  39. package/src/batch/sequence-run.mjs +418 -0
  40. package/src/batch/sequence-wait.mjs +167 -0
  41. package/src/batch/sequence.mjs +22 -783
  42. package/src/batch/state-guards.mjs +284 -0
  43. package/src/batch/state-io.mjs +135 -0
  44. package/src/batch/state.mjs +27 -396
  45. package/src/batch/worker-heartbeat.mjs +375 -0
  46. package/src/batch/worker-host.mjs +50 -541
  47. package/src/batch/worker-spawn.mjs +315 -0
@@ -0,0 +1,315 @@
1
+ // @ts-check
2
+ /**
3
+ * pi-spine worker spawn — child process setup, env, and output streaming (SP-581).
4
+ */
5
+
6
+ import path from "node:path";
7
+ import { spawn } from "node:child_process";
8
+ import { fileURLToPath } from "node:url";
9
+ import { startAgentSessionWorker } from "./agent-session-worker.mjs";
10
+ import { resolveWorkerBackend } from "../config/worker-backend.mjs";
11
+ import { resolvePiSpineRoot } from "../config/pi-spine-root.mjs";
12
+ import { resolveSafeWorkerLaunchScript } from "../config/worker-launch-script.mjs";
13
+
14
+ const __dirname = path.dirname(fileURLToPath(import.meta.url));
15
+ const PACKAGE_ROOT = path.resolve(__dirname, "../..");
16
+
17
+ export const POST_DONE_KILL_BACKOFF_MS = 5_000;
18
+ export const CHILD_DONE_TIMEOUT_MS = 15_000;
19
+
20
+ /** @typedef {"launching" | "pi" | "verify" | "unknown"} WorkerPhase */
21
+
22
+ /**
23
+ * @typedef {import("node:child_process").ChildProcess | {
24
+ * pid: number;
25
+ * exitCode: number | null;
26
+ * kill: (signal?: NodeJS.Signals) => boolean;
27
+ * wait?: () => Promise<{ exitCode: number; output: string }>;
28
+ * stdout?: NodeJS.ReadableStream | null;
29
+ * stderr?: NodeJS.ReadableStream | null;
30
+ * on?: import("node:events").EventEmitter["on"];
31
+ * }} WorkerChildHandle
32
+ */
33
+
34
+ /**
35
+ * @param {number} ms
36
+ */
37
+ function sleep(ms) {
38
+ return new Promise((resolve) => setTimeout(resolve, ms));
39
+ }
40
+
41
+ /**
42
+ * @param {string} projectRoot
43
+ * @param {object} [config]
44
+ * @returns {string|null}
45
+ */
46
+ export function resolveWorkerLaunchScript(projectRoot, config = {}) {
47
+ return resolveSafeWorkerLaunchScript(projectRoot, config);
48
+ }
49
+
50
+ /**
51
+ * @param {object} params
52
+ * @param {string} params.taskFolder
53
+ * @param {string} params.worktreePath
54
+ * @param {string} [params.projectRoot]
55
+ * @param {string} [params.batchId]
56
+ * @param {number} [params.laneNumber]
57
+ * @param {string} [params.taskId]
58
+ * @param {string} [params.laneCorrelationId]
59
+ * @param {string[]} [params.fileScopePaths]
60
+ * @param {object} [params.config]
61
+ * @param {number} [params.piTimeoutMs]
62
+ */
63
+ export function buildWorkerChildEnv({
64
+ taskFolder,
65
+ worktreePath,
66
+ projectRoot,
67
+ batchId,
68
+ laneNumber,
69
+ taskId,
70
+ laneCorrelationId,
71
+ fileScopePaths = [],
72
+ config = {},
73
+ piTimeoutMs,
74
+ }) {
75
+ const runner = path.join(PACKAGE_ROOT, "bin", "spine-worker-runner.mjs");
76
+ /** @type {NodeJS.ProcessEnv} */
77
+ const env = {
78
+ ...process.env,
79
+ SPINE_TASK_FOLDER: taskFolder,
80
+ SPINE_WORKTREE: worktreePath,
81
+ SPINE_WORKER_RUNNER: runner,
82
+ SPINE_IS_WORKER: "1",
83
+ PI_SPINE_ROOT: resolvePiSpineRoot(config, projectRoot ?? process.cwd()),
84
+ };
85
+ if (projectRoot) env.SPINE_PROJECT_ROOT = projectRoot;
86
+ if (worktreePath) env.SPINE_RULES_PROJECT_ROOT = worktreePath;
87
+ if (batchId) {
88
+ env.SPINE_BATCH_ID = batchId;
89
+ env.SPINE_JOURNAL_ATTACH = "1";
90
+ delete env.SPINE_SUPPRESS_JOURNAL_ATTACH;
91
+ }
92
+ if (laneNumber != null) env.SPINE_LANE_NUMBER = String(laneNumber);
93
+ if (taskId) env.SPINE_TASK_ID = taskId;
94
+ if (laneCorrelationId) env.SPINE_LANE_CORRELATION_ID = laneCorrelationId;
95
+ if (Array.isArray(fileScopePaths) && fileScopePaths.length > 0) {
96
+ env.SPINE_TASK_FILE_SCOPE = JSON.stringify(fileScopePaths);
97
+ }
98
+ if (process.env.SPINE_WORKER_STUB === "1") {
99
+ env.SPINE_REVIEW_STUB = "1";
100
+ }
101
+ if (piTimeoutMs != null && Number.isFinite(piTimeoutMs) && piTimeoutMs > 0) {
102
+ env.SPINE_WORKER_PI_TIMEOUT_MS = String(
103
+ process.env.SPINE_WORKER_PI_TIMEOUT_MS ?? piTimeoutMs,
104
+ );
105
+ }
106
+ return env;
107
+ }
108
+
109
+ /**
110
+ * @param {object} params
111
+ * @param {string} params.worktreePath
112
+ * @param {string} params.taskFolder
113
+ * @param {boolean} params.useStub
114
+ * @param {number} params.timeoutMs
115
+ * @param {string} [params.projectRoot]
116
+ * @param {string} [params.batchId]
117
+ * @param {number} [params.laneNumber]
118
+ * @param {string} [params.taskId]
119
+ * @param {string} [params.laneCorrelationId]
120
+ * @param {string[]} [params.fileScopePaths]
121
+ * @param {object} [params.config]
122
+ * @param {number} [params.piTimeoutMs]
123
+ */
124
+ export function spawnWorkerChild({
125
+ worktreePath,
126
+ taskFolder,
127
+ useStub,
128
+ timeoutMs: _timeoutMs,
129
+ projectRoot,
130
+ batchId,
131
+ laneNumber,
132
+ taskId,
133
+ laneCorrelationId,
134
+ fileScopePaths = [],
135
+ config = {},
136
+ piTimeoutMs,
137
+ }) {
138
+ const runner = path.join(PACKAGE_ROOT, "bin", "spine-worker-runner.mjs");
139
+ const env = buildWorkerChildEnv({
140
+ taskFolder,
141
+ worktreePath,
142
+ projectRoot,
143
+ batchId,
144
+ laneNumber,
145
+ taskId,
146
+ laneCorrelationId,
147
+ fileScopePaths,
148
+ config,
149
+ piTimeoutMs,
150
+ });
151
+ const args = useStub ? ["--stub"] : ["--pi"];
152
+
153
+ const launchScript = projectRoot ? resolveWorkerLaunchScript(projectRoot, config) : null;
154
+ if (launchScript) {
155
+ return spawn(launchScript, [runner, ...args], {
156
+ cwd: worktreePath,
157
+ env,
158
+ stdio: ["ignore", "pipe", "pipe"],
159
+ });
160
+ }
161
+
162
+ return spawn(process.execPath, [runner, ...args], {
163
+ cwd: worktreePath,
164
+ env,
165
+ stdio: ["ignore", "pipe", "pipe"],
166
+ });
167
+ }
168
+
169
+ /**
170
+ * @param {WorkerChildHandle} child
171
+ * @param {() => void} onPreflightComplete
172
+ */
173
+ export function markChildPastPreflight(child, onPreflightComplete) {
174
+ if (typeof child.stdout?.on === "function") {
175
+ child.stdout.on("data", onPreflightComplete);
176
+ }
177
+ if (typeof child.stderr?.on === "function") {
178
+ child.stderr.on("data", onPreflightComplete);
179
+ }
180
+ }
181
+
182
+ /**
183
+ * @param {object} params
184
+ * @param {boolean} params.childPastPreflight
185
+ * @param {boolean} params.useStub
186
+ * @param {string} params.workerBackend
187
+ * @returns {WorkerPhase}
188
+ */
189
+ export function resolveWorkerPhase({
190
+ childPastPreflight,
191
+ useStub,
192
+ workerBackend,
193
+ }) {
194
+ if (!childPastPreflight) return "launching";
195
+ if (useStub || workerBackend === "agentSession") return "pi";
196
+ return "pi";
197
+ }
198
+
199
+ /**
200
+ * SIGTERM then SIGKILL when a worker stays alive after post-.DONE grace.
201
+ *
202
+ * @param {WorkerChildHandle} child
203
+ * @param {Promise<{ exitCode: number; output: string }>} childDone
204
+ */
205
+ export async function terminateHungWorkerChild(child, childDone) {
206
+ child.kill("SIGTERM");
207
+ const raced = await Promise.race([childDone, sleep(POST_DONE_KILL_BACKOFF_MS)]);
208
+ if (raced && typeof raced === "object" && "exitCode" in raced) {
209
+ return raced;
210
+ }
211
+ if (child.exitCode === null && typeof child.kill === "function") {
212
+ child.kill("SIGKILL");
213
+ }
214
+ return childDone;
215
+ }
216
+
217
+ /**
218
+ * @param {WorkerChildHandle} child
219
+ * @param {{ append: (rawChunk: string) => void } | null} [liveLogWriter]
220
+ */
221
+ export function collectChildOutput(child, liveLogWriter) {
222
+ if ("wait" in child && typeof child.wait === "function") {
223
+ return child.wait();
224
+ }
225
+ return new Promise((resolve) => {
226
+ let stdout = "";
227
+ let stderr = "";
228
+ child.stdout?.on("data", (/** @type {Buffer | string} */ chunk) => {
229
+ const text = chunk.toString();
230
+ stdout += text;
231
+ liveLogWriter?.append(text);
232
+ });
233
+ child.stderr?.on("data", (/** @type {Buffer | string} */ chunk) => {
234
+ const text = chunk.toString();
235
+ stderr += text;
236
+ liveLogWriter?.append(text);
237
+ });
238
+ child.on?.("close", (/** @type {number | null} */ code) => {
239
+ resolve({ exitCode: code ?? 1, output: `${stdout}${stderr}` });
240
+ });
241
+ });
242
+ }
243
+
244
+ /**
245
+ * @param {object} params
246
+ * @param {string} params.worktreePath
247
+ * @param {string} params.taskFolder
248
+ * @param {boolean} params.useStub
249
+ * @param {number} params.timeoutMs
250
+ * @param {number} [params.piTimeoutMs]
251
+ * @param {string} [params.projectRoot]
252
+ * @param {string} [params.batchId]
253
+ * @param {number} [params.laneNumber]
254
+ * @param {string} [params.taskId]
255
+ * @param {string} [params.laneCorrelationId]
256
+ * @param {string[]} [params.fileScopePaths]
257
+ * @param {object} [params.config]
258
+ * @param {object} [params.workerBackendDeps]
259
+ */
260
+ export function spawnWorkerHandle({
261
+ worktreePath,
262
+ taskFolder,
263
+ useStub,
264
+ timeoutMs,
265
+ piTimeoutMs,
266
+ projectRoot,
267
+ batchId,
268
+ laneNumber,
269
+ taskId,
270
+ laneCorrelationId,
271
+ fileScopePaths = [],
272
+ config,
273
+ workerBackendDeps,
274
+ }) {
275
+ const journal =
276
+ projectRoot && batchId
277
+ ? {
278
+ projectRoot,
279
+ batchId,
280
+ taskId,
281
+ laneNumber,
282
+ correlationId: laneCorrelationId,
283
+ }
284
+ : undefined;
285
+
286
+ if (!useStub && resolveWorkerBackend(config) === "agentSession") {
287
+ return startAgentSessionWorker(
288
+ /** @type {{ worktreePath: string; taskFolder: string; config?: object; taskFileScope?: string[]; journal?: import("../config/worker-context.mjs").WorkerRulesJournalContext; projectRoot?: string }} */ ({
289
+ worktreePath,
290
+ taskFolder,
291
+ config,
292
+ taskFileScope: fileScopePaths,
293
+ journal,
294
+ projectRoot,
295
+ }),
296
+ workerBackendDeps ?? {},
297
+ );
298
+ }
299
+
300
+ return spawnWorkerChild({
301
+ worktreePath,
302
+ taskFolder,
303
+ useStub,
304
+ timeoutMs,
305
+ piTimeoutMs,
306
+ projectRoot,
307
+ batchId,
308
+ laneNumber,
309
+ taskId,
310
+ laneCorrelationId,
311
+ fileScopePaths,
312
+ config,
313
+ });
314
+ }
315
+