pi-spine 2.3.0 → 2.3.2

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.
@@ -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
+ }