pi-crew 0.9.42 → 0.9.46
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/CHANGELOG.md +223 -0
- package/README.md +35 -0
- package/dist/build-meta.json +4778 -11087
- package/dist/index.mjs +54648 -65435
- package/dist/index.mjs.map +4 -4
- package/package.json +1 -1
- package/scripts/build-bundle.mjs +2 -1
- package/src/agents/agent-config.ts +1 -1
- package/src/agents/discover-agents.ts +1 -1
- package/src/config/config.ts +1 -1
- package/src/config/types.ts +0 -2
- package/src/extension/crew-vibes/config.ts +1 -1
- package/src/extension/crew-vibes/font-detect.ts +16 -3
- package/src/extension/cross-extension-rpc.ts +8 -4
- package/src/extension/registration/commands.ts +5 -1
- package/src/extension/registration/foreground-run-controller.ts +28 -0
- package/src/extension/registration/lifecycle-handlers.ts +34 -3
- package/src/extension/registration/subagent-manager-setup.ts +178 -59
- package/src/extension/run-import.ts +21 -1
- package/src/extension/team-tool/api.ts +4 -2
- package/src/extension/team-tool.ts +5 -0
- package/src/prompt/prompt-runtime.ts +15 -0
- package/src/runtime/async-runner.ts +9 -1
- package/src/runtime/child-pi-constants.ts +42 -0
- package/src/runtime/child-pi-kill.ts +180 -0
- package/src/runtime/child-pi-spawn.ts +234 -0
- package/src/runtime/child-pi-steering.ts +128 -0
- package/src/runtime/child-pi-streams.ts +296 -0
- package/src/runtime/child-pi-transcript.ts +169 -0
- package/src/runtime/child-pi.ts +75 -837
- package/src/runtime/compact-stages/tail-capture-stage.ts +1 -1
- package/src/runtime/dwf-state-store.ts +5 -0
- package/src/runtime/dynamic-workflow-context.ts +78 -18
- package/src/runtime/dynamic-workflow-runner.ts +18 -2
- package/src/runtime/goal-evaluator.ts +59 -27
- package/src/runtime/goal-loop-runner.ts +40 -11
- package/src/runtime/live-agent-manager.ts +18 -0
- package/src/runtime/manifest-cache.ts +30 -0
- package/src/runtime/pi-json-output.ts +2 -1
- package/src/runtime/pi-spawn.ts +7 -1
- package/src/runtime/resilient-edit.ts +16 -15
- package/src/runtime/role-permission.ts +27 -2
- package/src/runtime/run-coalesced-task-group.ts +90 -25
- package/src/runtime/task-packet.ts +1 -1
- package/src/runtime/task-runner/prompt-builder.ts +2 -2
- package/src/runtime/team-runner.ts +6 -1
- package/src/state/event-log.ts +89 -34
- package/src/state/locks.ts +185 -49
- package/src/state/mailbox.ts +165 -4
- package/src/state/run-metrics.ts +40 -12
- package/src/state/worker-atomic-writer.ts +1 -0
- package/src/ui/live-run-sidebar.ts +1 -9
- package/src/ui/run-dashboard.ts +1 -9
- package/src/utils/incremental-reader.ts +105 -0
- package/src/utils/paths.ts +1 -1
- package/src/utils/visual.ts +27 -91
- package/src/worktree/worktree-manager.ts +47 -19
- package/src/runtime/auto-resume.ts +0 -100
- package/src/runtime/notebook-helpers.ts +0 -88
- package/src/runtime/orphan-sentinel.ts +0 -7
|
@@ -9,6 +9,7 @@ import { WINDOWS_ESSENTIAL_ENV_VARS } from "../utils/env-allowlist.ts";
|
|
|
9
9
|
import { sanitizeEnvSecrets } from "../utils/env-filter.ts";
|
|
10
10
|
import { logInternalError } from "../utils/internal-error.ts";
|
|
11
11
|
import { packageRoot } from "../utils/paths.ts";
|
|
12
|
+
import { redactSecretString } from "../utils/redaction.ts";
|
|
12
13
|
import { registerWorker, unregisterWorker } from "./orphan-worker-registry.ts";
|
|
13
14
|
import { PEER_DEP_DIR_ENV, resolvePeerDepDir } from "./peer-dep.ts";
|
|
14
15
|
|
|
@@ -318,7 +319,14 @@ export async function spawnBackgroundTeamRun(manifest: TeamRunManifest): Promise
|
|
|
318
319
|
}
|
|
319
320
|
stderrChunks.length = 0;
|
|
320
321
|
try {
|
|
321
|
-
|
|
322
|
+
// FIND-14: route child stderr through redactSecretString before writing
|
|
323
|
+
// to the log so API keys / bearer tokens / inline secrets emitted by the
|
|
324
|
+
// child are scrubbed. Without this, a child crash trace containing
|
|
325
|
+
// `Authorization: Bearer ...` or a stack trace with `MINIMAX_API_KEY=...`
|
|
326
|
+
// would land in background.log unredacted and ship to disk (and to the
|
|
327
|
+
// V8 fatal-error report which writes environmentVariables unredacted).
|
|
328
|
+
const redacted = redactSecretString(body);
|
|
329
|
+
fs.appendFileSync(logPath, `[child stderr] ${redacted}${redacted.endsWith("\n") ? "" : "\n"}`, "utf-8");
|
|
322
330
|
} catch {
|
|
323
331
|
/* best-effort */
|
|
324
332
|
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* child-pi-constants.ts — Shared timing and capture constants for child-pi runtime.
|
|
3
|
+
*
|
|
4
|
+
* Extracted from child-pi.ts (H-7 decomposition, step 3). Zero behavior change.
|
|
5
|
+
*
|
|
6
|
+
* These constants are shared between runChildPi (in child-pi.ts) and the kill
|
|
7
|
+
* helpers (in child-pi-kill.ts). Centralizing them here avoids circular imports
|
|
8
|
+
* and makes the timing budget configurable from one place.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { DEFAULT_CHILD_PI } from "../config/defaults.ts";
|
|
12
|
+
|
|
13
|
+
/** Post-exit window during which stdio is guarded against late writes. */
|
|
14
|
+
export const POST_EXIT_STDIO_GUARD_MS = DEFAULT_CHILD_PI.postExitStdioGuardMs;
|
|
15
|
+
|
|
16
|
+
/** Maximum time to wait for a final assistant event after the last stdout byte. */
|
|
17
|
+
export const FINAL_DRAIN_MS = DEFAULT_CHILD_PI.finalDrainMs;
|
|
18
|
+
|
|
19
|
+
/** Time after SIGTERM to escalate to SIGKILL. */
|
|
20
|
+
export const HARD_KILL_MS = DEFAULT_CHILD_PI.hardKillMs;
|
|
21
|
+
|
|
22
|
+
/** Maximum time with no output before the child is considered unresponsive. */
|
|
23
|
+
export const RESPONSE_TIMEOUT_MS = DEFAULT_CHILD_PI.responseTimeoutMs;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Maximum size (bytes) for the ChildPiLineObserver's line accumulation buffer.
|
|
27
|
+
* When exceeded, the buffer is force-flushed to prevent unbounded memory growth
|
|
28
|
+
* from chatty child processes that produce output without newlines.
|
|
29
|
+
*/
|
|
30
|
+
export const MAX_LINE_BUFFER_BYTES = 1024 * 1024; // 1 MB
|
|
31
|
+
|
|
32
|
+
/** Maximum characters for assistant text fragments in compacted events. */
|
|
33
|
+
export const MAX_ASSISTANT_TEXT_CHARS = DEFAULT_CHILD_PI.maxAssistantTextChars;
|
|
34
|
+
|
|
35
|
+
/** Maximum characters for tool-result fragments in compacted events. */
|
|
36
|
+
export const MAX_TOOL_RESULT_CHARS = DEFAULT_CHILD_PI.maxToolResultChars;
|
|
37
|
+
|
|
38
|
+
/** Maximum characters for tool-input fragments in compacted events. */
|
|
39
|
+
export const MAX_TOOL_INPUT_CHARS = DEFAULT_CHILD_PI.maxToolInputChars;
|
|
40
|
+
|
|
41
|
+
/** Maximum characters for general compactable content (used by TruncationStage). */
|
|
42
|
+
export const MAX_COMPACT_CONTENT_CHARS = DEFAULT_CHILD_PI.maxCompactContentChars;
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* child-pi-kill.ts — Kill/escape path for child Pi worker processes.
|
|
3
|
+
*
|
|
4
|
+
* Extracted from child-pi.ts (H-7 decomposition, step 2). Zero behavior change.
|
|
5
|
+
*
|
|
6
|
+
* Responsibilities:
|
|
7
|
+
* - appendBoundedTail(): bounded concatenation with truncation marker.
|
|
8
|
+
* - killProcessPid(): send SIGTERM → SIGKILL escalation, with Windows taskkill fallback.
|
|
9
|
+
* - killProcessTree(): log+kill a child by pid, optionally attach exit listener to clear timer.
|
|
10
|
+
* - terminateActiveChildPiProcesses(): kill all known active children.
|
|
11
|
+
* - registerActiveChild()/unregisterActiveChild(): book-keeping for the active-children Map.
|
|
12
|
+
* - Periodic zombie reaper (setInterval) that drops dead entries.
|
|
13
|
+
*
|
|
14
|
+
* The activeChildrenMap and hardKillTimers Map are kept here (not re-exported)
|
|
15
|
+
* because they are tightly coupled to the kill lifecycle.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import type { ChildProcess } from "node:child_process";
|
|
19
|
+
import { spawn } from "node:child_process";
|
|
20
|
+
import { DEFAULT_CHILD_PI } from "../config/defaults.ts";
|
|
21
|
+
import { logInternalError } from "../utils/internal-error.ts";
|
|
22
|
+
import { HARD_KILL_MS } from "./child-pi-constants.ts";
|
|
23
|
+
import { TailCaptureStage } from "./compact-stages/tail-capture-stage.ts";
|
|
24
|
+
|
|
25
|
+
const MAX_CAPTURE_BYTES = DEFAULT_CHILD_PI.maxCaptureBytes;
|
|
26
|
+
|
|
27
|
+
// Active children bookkeeping. Mutated by registerActiveChild/unregisterActiveChild.
|
|
28
|
+
const activeChildProcesses = new Map<number, ChildProcess>();
|
|
29
|
+
const childHardKillTimers = new Map<number, NodeJS.Timeout>();
|
|
30
|
+
|
|
31
|
+
// Periodic cleanup of dead child process entries to prevent memory leaks.
|
|
32
|
+
// If a child process never emits exit/close (zombie), the entry would leak.
|
|
33
|
+
setInterval(() => {
|
|
34
|
+
for (const [pid, child] of activeChildProcesses) {
|
|
35
|
+
try {
|
|
36
|
+
process.kill(pid, 0); // Throws ESRCH if dead
|
|
37
|
+
} catch {
|
|
38
|
+
activeChildProcesses.delete(pid);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}, 60_000).unref();
|
|
42
|
+
|
|
43
|
+
/** Register a newly-spawned child so it can be tracked + killed on shutdown. */
|
|
44
|
+
export function registerActiveChild(pid: number, child: ChildProcess): void {
|
|
45
|
+
activeChildProcesses.set(pid, child);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** Remove a child from the active set once it has exited (or before kill). */
|
|
49
|
+
export function unregisterActiveChild(pid: number): void {
|
|
50
|
+
activeChildProcesses.delete(pid);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** Clear the SIGKILL escalation timer for a pid (called on early exit). */
|
|
54
|
+
export function clearHardKillTimer(pid: number | undefined): void {
|
|
55
|
+
if (pid === undefined) return;
|
|
56
|
+
const timer = childHardKillTimers.get(pid);
|
|
57
|
+
if (timer) {
|
|
58
|
+
clearTimeout(timer);
|
|
59
|
+
childHardKillTimers.delete(pid);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Append `chunk` to `current`, keeping the result within `maxBytes`. When the
|
|
65
|
+
* cap is exceeded, returns the tail (most recent bytes) prefixed with a marker
|
|
66
|
+
* showing how much was dropped. Delegates to TailCaptureStage so the logic
|
|
67
|
+
* stays consistent with the rest of the compaction pipeline.
|
|
68
|
+
*/
|
|
69
|
+
export function appendBoundedTail(current: string, chunk: string, maxBytes = MAX_CAPTURE_BYTES): string {
|
|
70
|
+
return new TailCaptureStage({
|
|
71
|
+
maxBytes,
|
|
72
|
+
marker: `[pi-crew captured output truncated to last ${Math.round(maxBytes / 1024)} KiB]`,
|
|
73
|
+
}).apply(current + chunk);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function spawnTaskkillSafe(pid: number): void {
|
|
77
|
+
// B6: spawn taskkill and attach an 'error' listener. spawn() emits ENOENT/EACCES
|
|
78
|
+
// via the 'error' event — unhandled these would become uncaughtException and
|
|
79
|
+
// can crash the parent as an uncaught exception. taskkill is a standard Windows
|
|
80
|
+
// utility and rarely fails to spawn, but be defensive.
|
|
81
|
+
try {
|
|
82
|
+
const taskkillChild = spawn("taskkill", ["/pid", String(pid), "/t", "/f"], {
|
|
83
|
+
stdio: "ignore",
|
|
84
|
+
detached: false,
|
|
85
|
+
});
|
|
86
|
+
taskkillChild.on("error", (err) => {
|
|
87
|
+
logInternalError("child-pi.taskkill-spawn-error", err instanceof Error ? err : new Error(String(err)), `pid=${pid}`);
|
|
88
|
+
});
|
|
89
|
+
taskkillChild.unref();
|
|
90
|
+
} catch (error) {
|
|
91
|
+
logInternalError("child-pi.taskkill-sync-error", error instanceof Error ? error : new Error(String(error)), `pid=${pid}`);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export function killProcessPid(pid: number): void {
|
|
96
|
+
if (!Number.isInteger(pid) || pid <= 0) return;
|
|
97
|
+
try {
|
|
98
|
+
if (process.platform === "win32") {
|
|
99
|
+
// 3.8: Windows path uses taskkill /T /F (force kill the entire tree).
|
|
100
|
+
// taskkill itself can silently fail (PID gone, permission denied, etc.)
|
|
101
|
+
// so verify after 2s and log a warning if the process is still alive.
|
|
102
|
+
spawnTaskkillSafe(pid);
|
|
103
|
+
const verifyTimer = setTimeout(() => {
|
|
104
|
+
try {
|
|
105
|
+
process.kill(pid, 0); // throws ESRCH when dead
|
|
106
|
+
// Still alive — log and retry once.
|
|
107
|
+
logInternalError(
|
|
108
|
+
"child-pi.taskkill-stuck",
|
|
109
|
+
new Error(`process ${pid} still alive 2s after taskkill /T /F; retrying`),
|
|
110
|
+
`pid=${pid}`,
|
|
111
|
+
"error",
|
|
112
|
+
);
|
|
113
|
+
try {
|
|
114
|
+
spawnTaskkillSafe(pid);
|
|
115
|
+
} catch {
|
|
116
|
+
/* best-effort */
|
|
117
|
+
}
|
|
118
|
+
} catch {
|
|
119
|
+
// ESRCH or EPERM — process is gone. OK.
|
|
120
|
+
}
|
|
121
|
+
}, 2000);
|
|
122
|
+
verifyTimer.unref();
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
try {
|
|
126
|
+
process.kill(-pid, "SIGTERM");
|
|
127
|
+
} catch (error) {
|
|
128
|
+
logInternalError("child-pi.sigterm", error, `pid=${pid}`);
|
|
129
|
+
try {
|
|
130
|
+
process.kill(pid, "SIGTERM");
|
|
131
|
+
} catch (fallbackError) {
|
|
132
|
+
logInternalError("child-pi.sigterm-absolute", fallbackError, `pid=${pid}`);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
clearHardKillTimer(pid);
|
|
136
|
+
const hardKillTimer = setTimeout(() => {
|
|
137
|
+
try {
|
|
138
|
+
process.kill(-pid, "SIGKILL");
|
|
139
|
+
} catch (error) {
|
|
140
|
+
logInternalError("child-pi.sigkill", error, `pid=${pid}`);
|
|
141
|
+
try {
|
|
142
|
+
process.kill(pid, "SIGKILL");
|
|
143
|
+
} catch (fallbackError) {
|
|
144
|
+
logInternalError("child-pi.sigkill-absolute", fallbackError, `pid=${pid}`);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
childHardKillTimers.delete(pid);
|
|
148
|
+
}, HARD_KILL_MS);
|
|
149
|
+
hardKillTimer.unref();
|
|
150
|
+
childHardKillTimers.set(pid, hardKillTimer);
|
|
151
|
+
} catch (error) {
|
|
152
|
+
logInternalError("child-pi.kill-process-pid", error, `pid=${pid}`);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
export function killProcessTree(pid: number | undefined, child?: ChildProcess): void {
|
|
157
|
+
// Phase-0 diagnostic (HB-003a): capture who invoked killProcessTree so the
|
|
158
|
+
// exit-null race has a provenance trail. .stack is best-effort (may be undefined
|
|
159
|
+
// under deep async), so we take a snapshot lazily.
|
|
160
|
+
try {
|
|
161
|
+
const callerStack = new Error("killProcessTree caller").stack ?? "(no stack)";
|
|
162
|
+
logInternalError(
|
|
163
|
+
"child-pi.kill-process-tree-invoked",
|
|
164
|
+
new Error(`pid=${pid} called from:\n${callerStack.split("\n").slice(0, 8).join("\n")}`),
|
|
165
|
+
`pid=${pid}`,
|
|
166
|
+
);
|
|
167
|
+
} catch {
|
|
168
|
+
/* diagnostic best-effort */
|
|
169
|
+
}
|
|
170
|
+
if (!pid || !Number.isInteger(pid) || pid <= 0) return;
|
|
171
|
+
if (child && child.exitCode !== null) return;
|
|
172
|
+
killProcessPid(pid);
|
|
173
|
+
child?.once("exit", () => clearHardKillTimer(pid));
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
export function terminateActiveChildPiProcesses(): number {
|
|
177
|
+
const entries = [...activeChildProcesses.entries()];
|
|
178
|
+
for (const [pid, child] of entries) killProcessTree(pid, child);
|
|
179
|
+
return entries.length;
|
|
180
|
+
}
|
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* child-pi-spawn.ts — Spawn options + env filtering for child Pi worker processes.
|
|
3
|
+
*
|
|
4
|
+
* Extracted from child-pi.ts (H-7 decomposition, step 6). Zero behavior change.
|
|
5
|
+
*
|
|
6
|
+
* Responsibilities:
|
|
7
|
+
* - BASE_ALLOWLIST: env var names always passed to child workers.
|
|
8
|
+
* - buildChildPiSpawnOptions(): pure function that builds SpawnOptions from
|
|
9
|
+
* (cwd, env, model). Validates cwd, filters env via allowlist, validates
|
|
10
|
+
* NODE_PATH against safe prefixes.
|
|
11
|
+
* - assertOnlyControlEnvKeys(): runtime canary that verifies the caller only
|
|
12
|
+
* put PI_CREW (prefix) or PI_TEAMS (prefix) keys into the per-call built.env (defense in
|
|
13
|
+
* depth against accidental secret leakage).
|
|
14
|
+
* - prepareSpawnContext(): pre-spawn helper that builds the spawn command spec
|
|
15
|
+
* from buildPiWorkerArgs output, handles the pre-spawn abort check, and
|
|
16
|
+
* returns either an immediate-abort result or the spawn context.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import type { SpawnOptions } from "node:child_process";
|
|
20
|
+
import * as fs from "node:fs";
|
|
21
|
+
import * as path from "node:path";
|
|
22
|
+
import { WINDOWS_ESSENTIAL_ENV_VARS } from "../utils/env-allowlist.ts";
|
|
23
|
+
import { buildScopedAllowList, sanitizeEnvSecrets } from "../utils/env-filter.ts";
|
|
24
|
+
import { logInternalError } from "../utils/internal-error.ts";
|
|
25
|
+
import type { ChildPiRunInput, ChildPiRunResult } from "./child-pi.ts";
|
|
26
|
+
import { buildPiWorkerArgs } from "./pi-args.ts";
|
|
27
|
+
import { getPiSpawnCommand } from "./pi-spawn.ts";
|
|
28
|
+
|
|
29
|
+
// ── Env allowlist (base set always passed to children) ──────────────────
|
|
30
|
+
// Provider API keys are injected dynamically via buildScopedAllowList() only
|
|
31
|
+
// when a model is assigned to the task (per-task key scoping).
|
|
32
|
+
export const BASE_ALLOWLIST: string[] = [
|
|
33
|
+
"PATH",
|
|
34
|
+
"HOME",
|
|
35
|
+
"USER",
|
|
36
|
+
"SHELL",
|
|
37
|
+
"TERM",
|
|
38
|
+
"LANG",
|
|
39
|
+
"LC_ALL",
|
|
40
|
+
"LC_COLLATE",
|
|
41
|
+
"LC_CTYPE",
|
|
42
|
+
"LC_MESSAGES",
|
|
43
|
+
"LC_MONETARY",
|
|
44
|
+
"LC_NUMERIC",
|
|
45
|
+
"LC_TIME",
|
|
46
|
+
"XDG_CONFIG_HOME",
|
|
47
|
+
"XDG_DATA_HOME",
|
|
48
|
+
"XDG_CACHE_HOME",
|
|
49
|
+
"XDG_RUNTIME_DIR",
|
|
50
|
+
// Windows essentials — see WINDOWS_ESSENTIAL_ENV_VARS (src/utils/env-allowlist.ts).
|
|
51
|
+
...WINDOWS_ESSENTIAL_ENV_VARS,
|
|
52
|
+
"NVM_BIN",
|
|
53
|
+
"NVM_DIR",
|
|
54
|
+
"NVM_INC",
|
|
55
|
+
"NODE_DISABLE_COLORS",
|
|
56
|
+
"NODE_EXTRA_CA_CERTS",
|
|
57
|
+
"NPM_CONFIG_REGISTRY",
|
|
58
|
+
"NPM_CONFIG_USERCONFIG",
|
|
59
|
+
"NPM_CONFIG_GLOBALCONFIG",
|
|
60
|
+
"PI_CREW_DEPTH",
|
|
61
|
+
];
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Build the SpawnOptions for a child Pi worker process. Pure function — does
|
|
65
|
+
* not call spawn() itself; the caller does that.
|
|
66
|
+
*
|
|
67
|
+
* Responsibilities:
|
|
68
|
+
* 1. Validate cwd (realpath + isDirectory) — fall back to lexical path on ENOENT.
|
|
69
|
+
* 2. Filter env vars to the allowlist (model-aware provider key scoping).
|
|
70
|
+
* 3. Validate NODE_PATH against safe prefixes (/opt, /lib, /usr, /home).
|
|
71
|
+
* 4. Add PI_CREW_PARENT_PID for the child-side parent-guard.
|
|
72
|
+
*/
|
|
73
|
+
export function buildChildPiSpawnOptions(cwd: string, env: NodeJS.ProcessEnv, model?: string): SpawnOptions {
|
|
74
|
+
// SECURITY FIX (Issue #1): Validate cwd before passing to spawn.
|
|
75
|
+
// If cwd comes from an untrusted source (user input, workspace config), a malicious cwd
|
|
76
|
+
// could cause the child process to operate in an attacker-controlled directory,
|
|
77
|
+
// enabling path traversal attacks, unintended file access, or exposure of sensitive paths.
|
|
78
|
+
// Use realpathSync to resolve any symlinks and verify the path exists and is a directory.
|
|
79
|
+
let validatedCwd: string;
|
|
80
|
+
try {
|
|
81
|
+
validatedCwd = fs.realpathSync(cwd);
|
|
82
|
+
const stats = fs.statSync(validatedCwd);
|
|
83
|
+
if (!stats.isDirectory()) {
|
|
84
|
+
throw new Error(`cwd is not a directory: ${cwd}`);
|
|
85
|
+
}
|
|
86
|
+
} catch (error) {
|
|
87
|
+
// If cwd doesn't exist (ENOENT) and isn't a security concern, fall back
|
|
88
|
+
// to the lexical path. The child process will create the directory if
|
|
89
|
+
// needed. Throwing would break tests/callers that pass not-yet-existing
|
|
90
|
+
// paths and isn't a security issue for the env-filtering behavior this
|
|
91
|
+
// function is primarily about.
|
|
92
|
+
if ((error as NodeJS.ErrnoException).code === "ENOENT" && error instanceof Error && error.message.includes("ENOENT")) {
|
|
93
|
+
validatedCwd = path.resolve(cwd);
|
|
94
|
+
} else {
|
|
95
|
+
throw new Error(`Invalid cwd: ${cwd} — ${error instanceof Error ? error.message : String(error)}`);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// Filter out env vars whose keys match secret patterns to avoid leaking credentials to child processes.
|
|
100
|
+
// IMPORTANT: preserve model provider API keys — they are needed by the child Pi to call the LLM.
|
|
101
|
+
// Also preserve essential non-secret vars (PATH, HOME, USER, etc.) so the child process can function.
|
|
102
|
+
// Bug #12 fix: essential env vars (PATH, HOME, etc.) are always preserved so child can find npm/node.
|
|
103
|
+
//
|
|
104
|
+
// PER-TASK KEY SCOPING: when a model is provided, only the env keys for that
|
|
105
|
+
// provider are injected (via buildScopedAllowList). When no model is given,
|
|
106
|
+
// only BASE_ALLOWLIST system vars pass through — no provider keys leak.
|
|
107
|
+
const allowList = model ? buildScopedAllowList(BASE_ALLOWLIST, [model]) : BASE_ALLOWLIST;
|
|
108
|
+
const filteredEnv = sanitizeEnvSecrets(env, { allowList });
|
|
109
|
+
// FIX: Removed delete workarounds — with explicit allowlist, these vars
|
|
110
|
+
// are no longer auto-leaked. The wildcard approach was fragile.
|
|
111
|
+
|
|
112
|
+
// SECURITY FIX (Issue #1): Validate NODE_PATH to ensure it only contains standard
|
|
113
|
+
// system locations or legitimate user paths (NVM). NODE_PATH can reveal user
|
|
114
|
+
// environment information and could theoretically be exploited if it contains
|
|
115
|
+
// untrusted entries. Only allow paths under standard system directories
|
|
116
|
+
// (/opt, /lib, /usr) or NVM paths under /home/<user>/.nvm/... which are legitimate
|
|
117
|
+
// for Node.js module loading in user environments.
|
|
118
|
+
if (filteredEnv.NODE_PATH) {
|
|
119
|
+
const validPrefixes = ["/opt/", "/lib/", "/usr/local/", "/usr/", "/home/"];
|
|
120
|
+
const validPaths = filteredEnv.NODE_PATH.split(":").filter((p) => {
|
|
121
|
+
return validPrefixes.some((prefix) => p.startsWith(prefix));
|
|
122
|
+
});
|
|
123
|
+
if (validPaths.length > 0) {
|
|
124
|
+
filteredEnv.NODE_PATH = validPaths.join(":");
|
|
125
|
+
} else {
|
|
126
|
+
// No standard paths found — remove NODE_PATH entirely to avoid
|
|
127
|
+
// passing user-specific paths that could reveal environment info.
|
|
128
|
+
delete filteredEnv.NODE_PATH;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
return {
|
|
133
|
+
cwd: validatedCwd,
|
|
134
|
+
env: { ...filteredEnv, PI_CREW_PARENT_PID: String(process.pid) },
|
|
135
|
+
stdio: ["ignore", "pipe", "pipe"], // stdin=ignore: child doesn't wait for input; task comes via CLI args
|
|
136
|
+
detached: process.platform !== "win32",
|
|
137
|
+
setsid: true,
|
|
138
|
+
// NOTE: setsid creates a new session; the child process becomes the session leader
|
|
139
|
+
// and its parent becomes that session leader (still the team-runner in the same
|
|
140
|
+
// process group). PI_CREW_PARENT_PID is set before spawn using process.pid (team-runner).
|
|
141
|
+
// The parent-guard in the child checks direct parent liveness via process.kill(pid, 0) —
|
|
142
|
+
// it does NOT follow the lineage beyond the direct parent. If the team-runner's parent
|
|
143
|
+
// (the original pi session) dies, the team-runner becomes an orphan but the child still
|
|
144
|
+
// sees its direct parent (team-runner) as alive. This is correct for the parent-guard model.
|
|
145
|
+
windowsHide: true,
|
|
146
|
+
} as SpawnOptions;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Throw if `built.env` contains keys outside the PI_CREW_ (prefix) / PI_TEAMS_ (prefix) namespaces.
|
|
151
|
+
* Called right before spawn() as a runtime canary — protects against future
|
|
152
|
+
* regressions where someone accidentally adds a secret key to built.env.
|
|
153
|
+
*/
|
|
154
|
+
export function assertOnlyControlEnvKeys(builtEnv: Record<string, string | undefined>): void {
|
|
155
|
+
// Verifies built.env (the per-call env we add on top of process.env) only
|
|
156
|
+
// contains PI_CREW_*/PI_TEAMS_* control keys. built.env does NOT include
|
|
157
|
+
// process.env values — those are merged separately via spread and filtered
|
|
158
|
+
// by the allowlist in buildChildPiSpawnOptions. This assertion guards
|
|
159
|
+
// against accidental additions to built.env leaking secrets to children.
|
|
160
|
+
for (const key of Object.keys(builtEnv)) {
|
|
161
|
+
if (!key.startsWith("PI_CREW_") && !key.startsWith("PI_TEAMS_")) {
|
|
162
|
+
throw new Error(
|
|
163
|
+
`SECURITY: built.env contains unexpected key "${key}"; expected only PI_CREW_* or PI_TEAMS_* execution-control vars`,
|
|
164
|
+
);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/** What the spawn site needs to start the child process. */
|
|
170
|
+
export interface SpawnContext {
|
|
171
|
+
/** The command + args returned by getPiSpawnCommand. */
|
|
172
|
+
spawnSpec: ReturnType<typeof getPiSpawnCommand>;
|
|
173
|
+
/** The merged env (process.env + built.env) to pass to spawn(). */
|
|
174
|
+
mergedEnv: NodeJS.ProcessEnv;
|
|
175
|
+
/** Temp dir created by buildPiWorkerArgs (caller must clean up after spawn). */
|
|
176
|
+
tempDir: string | undefined;
|
|
177
|
+
/** The per-call built.env (control vars only) — for security canary assertions. */
|
|
178
|
+
builtEnv: Record<string, string | undefined>;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* Build the spawn context for a child Pi run: calls buildPiWorkerArgs,
|
|
183
|
+
* attaches PI_CREW_STEERING_FILE if a steering file is configured, then returns
|
|
184
|
+
* the spawn command spec + merged env. Does NOT spawn.
|
|
185
|
+
*
|
|
186
|
+
* If the parent AbortSignal has already fired, returns an immediate-abort
|
|
187
|
+
* ChildPiRunResult instead — spawn is then skipped entirely (saves resources).
|
|
188
|
+
*/
|
|
189
|
+
export function prepareSpawnContext(
|
|
190
|
+
input: ChildPiRunInput,
|
|
191
|
+
effectiveTask: string,
|
|
192
|
+
): { kind: "ready"; ctx: SpawnContext } | { kind: "aborted"; result: ChildPiRunResult } {
|
|
193
|
+
const built = buildPiWorkerArgs({
|
|
194
|
+
task: effectiveTask,
|
|
195
|
+
agent: input.agent,
|
|
196
|
+
model: input.model,
|
|
197
|
+
sessionEnabled: true,
|
|
198
|
+
maxDepth: input.maxDepth,
|
|
199
|
+
skillPaths: input.skillPaths,
|
|
200
|
+
role: input.role,
|
|
201
|
+
});
|
|
202
|
+
// Pass steering file path to child for real-time steer injection
|
|
203
|
+
if (input.steeringFile) built.env.PI_CREW_STEERING_FILE = input.steeringFile;
|
|
204
|
+
// B5: if the parent already aborted before we spawn, do not start the child
|
|
205
|
+
// at all. Spawning a doomed process wastes resources, and the abort listener
|
|
206
|
+
// registered below will not re-fire for an already-aborted signal (so the
|
|
207
|
+
// child would only be killed later by the response-timeout path). Return a
|
|
208
|
+
// cancelled-style result immediately.
|
|
209
|
+
if (input.signal?.aborted) {
|
|
210
|
+
return {
|
|
211
|
+
kind: "aborted",
|
|
212
|
+
result: {
|
|
213
|
+
exitCode: null,
|
|
214
|
+
stdout: "",
|
|
215
|
+
stderr: "",
|
|
216
|
+
error: "Aborted before spawn (parent AbortSignal already aborted)",
|
|
217
|
+
aborted: true,
|
|
218
|
+
},
|
|
219
|
+
};
|
|
220
|
+
}
|
|
221
|
+
const spawnSpec = getPiSpawnCommand(built.args);
|
|
222
|
+
return {
|
|
223
|
+
kind: "ready",
|
|
224
|
+
ctx: {
|
|
225
|
+
spawnSpec,
|
|
226
|
+
mergedEnv: { ...process.env, ...built.env },
|
|
227
|
+
tempDir: built.tempDir,
|
|
228
|
+
builtEnv: built.env,
|
|
229
|
+
},
|
|
230
|
+
};
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
// Silence unused-import warning for logInternalError if not consumed by future helpers.
|
|
234
|
+
void logInternalError;
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* child-pi-steering.ts — Turn-count-based steering controller for child Pi.
|
|
3
|
+
*
|
|
4
|
+
* Extracted from child-pi.ts (H-7 decomposition, step 5). Zero behavior change.
|
|
5
|
+
*
|
|
6
|
+
* The controller encapsulates the steering state machine:
|
|
7
|
+
* - Tracks turn count from `turn_end` events.
|
|
8
|
+
* - At `maxTurns`: triggers soft-limit steer (append advisory to steering file).
|
|
9
|
+
* - At `maxTurns + graceTurns`: triggers hard abort via killProcessTree.
|
|
10
|
+
*
|
|
11
|
+
* The hardAbortInitiated flag prevents the no-response timer from being
|
|
12
|
+
* restarted after the hard-abort fires (CP-1 fix — see UPGRADE_REVIEW.md).
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import * as fs from "node:fs";
|
|
16
|
+
import { logInternalError } from "../utils/internal-error.ts";
|
|
17
|
+
import { killProcessTree } from "./child-pi-kill.ts";
|
|
18
|
+
|
|
19
|
+
/** Action emitted by the controller when a `turn_end` event is processed. */
|
|
20
|
+
export type SteeringAction =
|
|
21
|
+
| { kind: "steer" }
|
|
22
|
+
| { kind: "hardAbort"; pid: number; child: import("node:child_process").ChildProcess }
|
|
23
|
+
| { kind: "none" };
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* State machine for turn-count-based steering.
|
|
27
|
+
*
|
|
28
|
+
* Usage:
|
|
29
|
+
* const controller = new ChildPiSteeringController(maxTurns, graceTurns);
|
|
30
|
+
* // In onJsonEvent, when event.type === "turn_end":
|
|
31
|
+
* const action = controller.onTurnEnd(child.pid, child, input.steeringFile);
|
|
32
|
+
* if (action.kind === "hardAbort") killProcessTree(action.pid, action.child);
|
|
33
|
+
* // In all restartNoResponseTimer sites:
|
|
34
|
+
* if (!controller.isHardAbortInitiated()) restartNoResponseTimer();
|
|
35
|
+
*/
|
|
36
|
+
export class ChildPiSteeringController {
|
|
37
|
+
private turnCount = 0;
|
|
38
|
+
private softLimitReached = false;
|
|
39
|
+
private hardAbortInitiatedFlag = false;
|
|
40
|
+
private readonly maxTurns: number | undefined;
|
|
41
|
+
private readonly graceTurns: number | undefined;
|
|
42
|
+
|
|
43
|
+
constructor(maxTurns: number | undefined, graceTurns: number | undefined) {
|
|
44
|
+
this.maxTurns = maxTurns;
|
|
45
|
+
// FIX (Issue #1): Bound graceTurns to prevent the hard abort condition from
|
|
46
|
+
// never triggering when an arbitrarily large value is passed.
|
|
47
|
+
this.graceTurns = graceTurns !== undefined && graceTurns > 1000 ? 1000 : graceTurns;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** Called on each `turn_end` event. Returns the action to take (if any). */
|
|
51
|
+
onTurnEnd(
|
|
52
|
+
pid: number | undefined,
|
|
53
|
+
child: import("node:child_process").ChildProcess | undefined,
|
|
54
|
+
steeringFile: string | undefined,
|
|
55
|
+
): SteeringAction {
|
|
56
|
+
this.turnCount += 1;
|
|
57
|
+
// Soft limit: first turn at or beyond maxTurns → deliver "wrap up" advisory.
|
|
58
|
+
if (this.maxTurns !== undefined && !this.softLimitReached && this.turnCount >= this.maxTurns) {
|
|
59
|
+
this.softLimitReached = true;
|
|
60
|
+
// C8: deliver the "wrap up" advisory by appending to the steering JSONL
|
|
61
|
+
// file the child polls (PI_CREW_STEERING_FILE). The child is spawned with
|
|
62
|
+
// stdio:["ignore",...], so child.stdin is null and the old stdin branch was
|
|
63
|
+
// dead code that only spammed logs on every soft-limit hit. Advisory only —
|
|
64
|
+
// the hard-abort below at maxTurns + graceTurns is the real enforcement, so
|
|
65
|
+
// a failed write must NOT kill the worker.
|
|
66
|
+
if (steeringFile) {
|
|
67
|
+
try {
|
|
68
|
+
fs.appendFileSync(
|
|
69
|
+
steeringFile,
|
|
70
|
+
JSON.stringify({
|
|
71
|
+
type: "steer",
|
|
72
|
+
message: "You have reached your turn limit. Wrap up immediately — provide your final answer now.",
|
|
73
|
+
}) + "\n",
|
|
74
|
+
"utf-8",
|
|
75
|
+
);
|
|
76
|
+
} catch (err) {
|
|
77
|
+
logInternalError("child-pi.steer-write-failed", err instanceof Error ? err : new Error(String(err)), `pid=${pid}`);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return { kind: "steer" };
|
|
81
|
+
}
|
|
82
|
+
// Hard abort: turn count reached maxTurns + graceTurns after soft limit was hit.
|
|
83
|
+
if (this.maxTurns !== undefined && this.softLimitReached && this.turnCount >= this.maxTurns + (this.graceTurns ?? 5)) {
|
|
84
|
+
// CP-1: escalate to killProcessTree (same as abort/noResponseTimer paths) and
|
|
85
|
+
// set flag so onJsonEvent stops restarting the no-response timer.
|
|
86
|
+
this.hardAbortInitiatedFlag = true;
|
|
87
|
+
if (pid !== undefined && child) {
|
|
88
|
+
killProcessTree(pid, child);
|
|
89
|
+
return { kind: "hardAbort", pid, child };
|
|
90
|
+
}
|
|
91
|
+
return { kind: "none" };
|
|
92
|
+
}
|
|
93
|
+
return { kind: "none" };
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Returns true once the hard-abort has been initiated. Callers (onJsonEvent,
|
|
98
|
+
* onStdoutLine, stdout/stderr data handlers) should skip restartNoResponseTimer
|
|
99
|
+
* when this returns true to avoid masking a SIGTERM-ignoring child.
|
|
100
|
+
*/
|
|
101
|
+
isHardAbortInitiated(): boolean {
|
|
102
|
+
return this.hardAbortInitiatedFlag;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Returns true once the soft-limit steer has been delivered (the worker has
|
|
107
|
+
* been notified to wrap up). Used by runChildPi's settle path to distinguish
|
|
108
|
+
* a graceful-abort from a parent-abort.
|
|
109
|
+
*/
|
|
110
|
+
isSoftLimitReached(): boolean {
|
|
111
|
+
return this.softLimitReached;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** Current turn count (incremented on each `turn_end` event). */
|
|
115
|
+
getTurnCount(): number {
|
|
116
|
+
return this.turnCount;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** Max turns configured (undefined = no limit). */
|
|
120
|
+
getMaxTurns(): number | undefined {
|
|
121
|
+
return this.maxTurns;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** Grace turns configured (after soft limit before hard abort). */
|
|
125
|
+
getGraceTurns(): number | undefined {
|
|
126
|
+
return this.graceTurns;
|
|
127
|
+
}
|
|
128
|
+
}
|