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.
Files changed (56) hide show
  1. package/bin/spine-cli/verify.mjs +15 -46
  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/diagnosis.mjs +35 -4
  15. package/src/batch/engine-lanes/orch-sync.mjs +126 -0
  16. package/src/batch/engine-lanes.mjs +69 -0
  17. package/src/batch/engine.mjs +8 -67
  18. package/src/batch/integrate-git.mjs +21 -0
  19. package/src/batch/integrate.mjs +5 -15
  20. package/src/batch/journal-rebuild-structural.mjs +352 -0
  21. package/src/batch/journal-rebuild.mjs +16 -348
  22. package/src/batch/lane-dirty-check-commit.mjs +361 -0
  23. package/src/batch/lane-dirty-check-git.mjs +298 -0
  24. package/src/batch/lane-dirty-check.mjs +31 -297
  25. package/src/batch/reconcile-batch.mjs +495 -0
  26. package/src/batch/reconcile-classify.mjs +433 -0
  27. package/src/batch/reconcile-diagnosis.mjs +459 -0
  28. package/src/batch/reconcile-light-cache.mjs +61 -0
  29. package/src/batch/reconcile-orphan.mjs +198 -0
  30. package/src/batch/reconcile.mjs +19 -1693
  31. package/src/batch/resume-multi-lanes.mjs +6 -148
  32. package/src/batch/resume-multi-queue.mjs +159 -0
  33. package/src/batch/resume-single-validate.mjs +25 -0
  34. package/src/batch/resume.mjs +3 -20
  35. package/src/batch/review-artifacts.mjs +419 -0
  36. package/src/batch/review-step-run.mjs +383 -0
  37. package/src/batch/review-step.mjs +451 -0
  38. package/src/batch/review.mjs +32 -1199
  39. package/src/batch/salvage-batch-list.mjs +291 -0
  40. package/src/batch/salvage-batch.mjs +10 -284
  41. package/src/batch/sequence-plan.mjs +234 -0
  42. package/src/batch/sequence-run.mjs +418 -0
  43. package/src/batch/sequence-wait.mjs +167 -0
  44. package/src/batch/sequence.mjs +22 -783
  45. package/src/batch/state-guards.mjs +284 -0
  46. package/src/batch/state-io.mjs +135 -0
  47. package/src/batch/state.mjs +27 -396
  48. package/src/batch/worker-heartbeat.mjs +382 -0
  49. package/src/batch/worker-host.mjs +56 -548
  50. package/src/batch/worker-spawn.mjs +326 -0
  51. package/src/batch/worktree.mjs +68 -0
  52. package/src/config/preflight/integrate-plan.mjs +6 -1
  53. package/src/config/preflight/loc-capstone.mjs +274 -0
  54. package/src/config/spine-preflight-lib.mjs +14 -0
  55. package/src/planner/index.mjs +14 -3
  56. package/src/process/terminate-tree.mjs +154 -0
@@ -28,6 +28,7 @@ import {
28
28
  checkPrelandedFileScopeWarn,
29
29
  runPreflightPlanCheck,
30
30
  } from "./preflight/integrate-plan.mjs";
31
+ import { checkLocCapstoneReadiness } from "./preflight/loc-capstone.mjs";
31
32
  import { INTEGRATE_DEFAULTS } from "./defaults.mjs";
32
33
  import { parseContract } from "../tasks/packet/parse-prompt.mjs";
33
34
  import { hasReleaseCriticalContract, isStubWorkerMode } from "../batch/contract-verify.mjs";
@@ -65,6 +66,18 @@ export {
65
66
  runPreflightPlanCheck,
66
67
  } from "./preflight/integrate-plan.mjs";
67
68
 
69
+ export {
70
+ assertLocCapstoneReadinessForPlan,
71
+ BATCH_MODULE_LOC_LIMIT,
72
+ checkLocCapstoneReadiness,
73
+ evaluateBatchLocPolicy,
74
+ evaluateBatchLocPolicyAfterEmptyGrandfather,
75
+ formatOverLimitModules,
76
+ isLocCapstoneEmptyGrandfatherMission,
77
+ listBatchModuleLineCounts,
78
+ listPendingLocCapstoneTasks,
79
+ } from "./preflight/loc-capstone.mjs";
80
+
68
81
  const CONCURRENT_DEV_LABEL = "concurrent development on base branch";
69
82
 
70
83
  /**
@@ -360,6 +373,7 @@ export function runBatchPreflight(options) {
360
373
  checks.push(checkWorktreeSetupHook(ctx));
361
374
  checks.push(checkTasksValidate(ctx));
362
375
  checks.push(checkStubReleaseCritical(ctx));
376
+ checks.push(checkLocCapstoneReadiness(ctx));
363
377
  checks.push(checkOrchMergeConflictWarn(ctx));
364
378
  checks.push(checkPrelandedFileScopeWarn(ctx));
365
379
 
@@ -17,11 +17,13 @@ import { buildGraph, topoWaves } from './graph.mjs';
17
17
  import { findCyclePath } from './cycles.mjs';
18
18
  import { parseScope } from './scope.mjs';
19
19
  import { collectWaveSizeWarnings, formatFileScopeOverlapWarnings, planWaves } from './waves.mjs';
20
+ import { assertLocCapstoneReadinessForPlan } from '../config/preflight/loc-capstone.mjs';
21
+ import path from 'node:path';
20
22
 
21
23
  /**
22
- * @param {{ scope?: any, config: { lanes?: { maxParallel?: number, queueExcess?: boolean } }, tasksRoot: string }} args
24
+ * @param {{ scope?: any, config: { lanes?: { maxParallel?: number, queueExcess?: boolean } }, tasksRoot: string, projectRoot?: string }} args
23
25
  */
24
- export function buildPlan({ scope, config, tasksRoot }) {
26
+ export function buildPlan({ scope, config, tasksRoot, projectRoot }) {
25
27
  if (!tasksRoot) throw new Error('buildPlan requires tasksRoot');
26
28
  if (!config) throw new Error('buildPlan requires config');
27
29
 
@@ -32,7 +34,7 @@ export function buildPlan({ scope, config, tasksRoot }) {
32
34
 
33
35
  const depsJson = loadDependenciesJson(tasksRoot);
34
36
 
35
- /** @type {Record<string, { taskId: string, title: string|null, fileScope: string[], dependencies: string[] }> } */
37
+ /** @type {Record<string, { taskId: string, title: string|null, fileScope: string[], dependencies: string[], missionText?: string, folderName?: string }> } */
36
38
  const tasksById = {};
37
39
  /** @type {Array<{ taskId: string, promptPath?: string, errors: string[] }>} */
38
40
  const promptValidationFailures = [];
@@ -56,6 +58,8 @@ export function buildPlan({ scope, config, tasksRoot }) {
56
58
  title: prompt.title ?? null,
57
59
  fileScope: Array.isArray(prompt.fileScope) ? prompt.fileScope : [],
58
60
  dependencies: mergedDeps,
61
+ missionText: prompt.sections?.Mission ?? '',
62
+ folderName: path.basename(discoveredTask.folderPath),
59
63
  };
60
64
  }
61
65
 
@@ -63,6 +67,13 @@ export function buildPlan({ scope, config, tasksRoot }) {
63
67
  throw new Error(formatPromptValidationFailures(promptValidationFailures));
64
68
  }
65
69
 
70
+ const resolvedProjectRoot =
71
+ projectRoot ?? path.resolve(tasksRoot, '..');
72
+ assertLocCapstoneReadinessForPlan({
73
+ projectRoot: resolvedProjectRoot,
74
+ tasks: selectedIdsSorted.map((taskId) => tasksById[taskId]).filter(Boolean),
75
+ });
76
+
66
77
  /** @type {Record<string, string[]>} */
67
78
  const depsByTask = {};
68
79
  for (const taskId of selectedIdsSorted) {
@@ -0,0 +1,154 @@
1
+ /**
2
+ * Process-tree terminate — reap a PID and its descendants (SP-609 / #194).
3
+ *
4
+ * Killing only the tracked workerPid leaves nested `pi` grandchildren alive.
5
+ * List children before signaling the parent so reparenting to init cannot hide them.
6
+ */
7
+
8
+ import { spawnSync } from "node:child_process";
9
+
10
+ /** Hard cap on BFS depth when walking the process tree. */
11
+ const MAX_TREE_DEPTH = 32;
12
+
13
+ /** Hard cap on total descendant PIDs collected. */
14
+ const MAX_DESCENDANTS = 256;
15
+
16
+ /**
17
+ * @param {unknown} pid
18
+ * @returns {number[]}
19
+ */
20
+ export function listDirectChildPids(pid) {
21
+ const parent = Number(pid);
22
+ if (!Number.isFinite(parent) || parent <= 0) return [];
23
+ if (process.platform === "win32") {
24
+ return listDirectChildPidsWindows(parent);
25
+ }
26
+ const result = spawnSync("pgrep", ["-P", String(parent)], {
27
+ encoding: "utf-8",
28
+ timeout: 5_000,
29
+ });
30
+ if (result.status !== 0 && result.status !== 1) {
31
+ return [];
32
+ }
33
+ const stdout = result.stdout ?? "";
34
+ return stdout
35
+ .split(/\s+/)
36
+ .map((line) => Number(line.trim()))
37
+ .filter((n) => Number.isFinite(n) && n > 0);
38
+ }
39
+
40
+ /**
41
+ * @param {number} parent
42
+ * @returns {number[]}
43
+ */
44
+ function listDirectChildPidsWindows(parent) {
45
+ const result = spawnSync(
46
+ "wmic",
47
+ ["process", "where", `ParentProcessId=${parent}`, "get", "ProcessId", "/value"],
48
+ { encoding: "utf-8", timeout: 5_000, windowsHide: true },
49
+ );
50
+ if (result.error || (result.status !== 0 && result.status !== 1)) {
51
+ return [];
52
+ }
53
+ const stdout = result.stdout ?? "";
54
+ /** @type {number[]} */
55
+ const children = [];
56
+ for (const match of stdout.matchAll(/ProcessId=(\d+)/g)) {
57
+ const n = Number(match[1]);
58
+ if (Number.isFinite(n) && n > 0) children.push(n);
59
+ }
60
+ return children;
61
+ }
62
+
63
+ /**
64
+ * Breadth-first descendant PIDs (excludes the root). Bounded depth and count.
65
+ *
66
+ * @param {unknown} pid
67
+ * @returns {number[]}
68
+ */
69
+ export function listDescendantPids(pid) {
70
+ const root = Number(pid);
71
+ if (!Number.isFinite(root) || root <= 0) return [];
72
+ /** @type {number[]} */
73
+ const descendants = [];
74
+ /** @type {Set<number>} */
75
+ const seen = new Set([root]);
76
+ /** @type {Array<{ pid: number, depth: number }>} */
77
+ const queue = [{ pid: root, depth: 0 }];
78
+
79
+ while (queue.length > 0) {
80
+ const current = queue.shift();
81
+ if (!current) break;
82
+ if (current.depth >= MAX_TREE_DEPTH) continue;
83
+ if (descendants.length >= MAX_DESCENDANTS) break;
84
+ for (const child of listDirectChildPids(current.pid)) {
85
+ if (seen.has(child)) continue;
86
+ seen.add(child);
87
+ descendants.push(child);
88
+ if (descendants.length >= MAX_DESCENDANTS) break;
89
+ queue.push({ pid: child, depth: current.depth + 1 });
90
+ }
91
+ }
92
+ return descendants;
93
+ }
94
+
95
+ /**
96
+ * @param {number} pid
97
+ * @param {NodeJS.Signals} signal
98
+ * @returns {boolean} true when the signal was delivered
99
+ */
100
+ function tryKill(pid, signal) {
101
+ try {
102
+ process.kill(pid, signal);
103
+ return true;
104
+ } catch {
105
+ return false;
106
+ }
107
+ }
108
+
109
+ /**
110
+ * Terminate a process and its descendants.
111
+ *
112
+ * On Unix, also attempts process-group kill (`-pid`) when the target is a
113
+ * group leader. On Windows, prefers `taskkill /T` then falls back to tree walk.
114
+ *
115
+ * @param {unknown} pid
116
+ * @param {{ signal?: NodeJS.Signals }} [options]
117
+ * @returns {{ rootPid: number, signal: NodeJS.Signals, descendantPids: number[], signaled: number[] }}
118
+ */
119
+ export function terminateProcessTree(pid, { signal = "SIGKILL" } = {}) {
120
+ const rootPid = Number(pid);
121
+ /** @type {number[]} */
122
+ const signaled = [];
123
+ if (!Number.isFinite(rootPid) || rootPid <= 0) {
124
+ return { rootPid: 0, signal, descendantPids: [], signaled };
125
+ }
126
+
127
+ if (process.platform === "win32") {
128
+ const taskkill = spawnSync(
129
+ "taskkill",
130
+ ["/PID", String(rootPid), "/T", "/F"],
131
+ { encoding: "utf-8", timeout: 10_000, windowsHide: true },
132
+ );
133
+ if (taskkill.status === 0) {
134
+ return { rootPid, signal, descendantPids: [], signaled: [rootPid] };
135
+ }
136
+ }
137
+
138
+ const descendantPids = listDescendantPids(rootPid);
139
+
140
+ // Deepest-first so parents do not reparent before children are signaled.
141
+ for (let i = descendantPids.length - 1; i >= 0; i -= 1) {
142
+ const childPid = descendantPids[i];
143
+ if (tryKill(childPid, signal)) signaled.push(childPid);
144
+ }
145
+
146
+ if (process.platform !== "win32") {
147
+ // Negative PID = process group; succeeds when root is the group leader.
148
+ tryKill(-rootPid, signal);
149
+ }
150
+
151
+ if (tryKill(rootPid, signal)) signaled.push(rootPid);
152
+
153
+ return { rootPid, signal, descendantPids, signaled };
154
+ }