pi-crew 0.9.42 → 0.9.44

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 (41) hide show
  1. package/CHANGELOG.md +147 -0
  2. package/README.md +14 -0
  3. package/dist/build-meta.json +6692 -13022
  4. package/dist/index.mjs +54585 -65621
  5. package/dist/index.mjs.map +4 -4
  6. package/package.json +1 -1
  7. package/scripts/build-bundle.mjs +2 -1
  8. package/src/agents/agent-config.ts +1 -1
  9. package/src/config/config.ts +1 -1
  10. package/src/config/types.ts +0 -2
  11. package/src/extension/crew-vibes/config.ts +1 -1
  12. package/src/extension/crew-vibes/font-detect.ts +16 -3
  13. package/src/extension/cross-extension-rpc.ts +8 -4
  14. package/src/extension/registration/commands.ts +5 -1
  15. package/src/extension/registration/foreground-run-controller.ts +28 -0
  16. package/src/extension/team-tool.ts +5 -0
  17. package/src/prompt/prompt-runtime.ts +15 -0
  18. package/src/runtime/child-pi-constants.ts +42 -0
  19. package/src/runtime/child-pi-kill.ts +180 -0
  20. package/src/runtime/child-pi-spawn.ts +234 -0
  21. package/src/runtime/child-pi-steering.ts +128 -0
  22. package/src/runtime/child-pi-streams.ts +296 -0
  23. package/src/runtime/child-pi-transcript.ts +169 -0
  24. package/src/runtime/child-pi.ts +75 -837
  25. package/src/runtime/compact-stages/tail-capture-stage.ts +1 -1
  26. package/src/runtime/dwf-state-store.ts +5 -0
  27. package/src/runtime/dynamic-workflow-context.ts +78 -18
  28. package/src/runtime/dynamic-workflow-runner.ts +18 -2
  29. package/src/runtime/goal-evaluator.ts +59 -27
  30. package/src/runtime/goal-loop-runner.ts +40 -11
  31. package/src/runtime/live-agent-manager.ts +18 -0
  32. package/src/runtime/pi-json-output.ts +2 -1
  33. package/src/runtime/pi-spawn.ts +7 -1
  34. package/src/runtime/run-coalesced-task-group.ts +18 -10
  35. package/src/runtime/task-runner/prompt-builder.ts +2 -2
  36. package/src/runtime/team-runner.ts +6 -1
  37. package/src/state/event-log.ts +1 -0
  38. package/src/state/locks.ts +132 -49
  39. package/src/state/worker-atomic-writer.ts +1 -0
  40. package/src/utils/paths.ts +1 -1
  41. package/src/worktree/worktree-manager.ts +47 -19
@@ -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
+ }
@@ -0,0 +1,296 @@
1
+ /**
2
+ * child-pi-streams.ts — Stdout/stderr line parsing + observation for child Pi.
3
+ *
4
+ * Extracted from child-pi.ts (H-7 decomposition, step 4). The code is a verbatim
5
+ * copy of what lived in child-pi.ts before this extraction — only the imports
6
+ * were updated to import from child-pi-transcript.ts (compactString,
7
+ * compactValue, appendTranscript, flushPendingTranscriptWrites) and
8
+ * child-pi-constants.ts (MAX_*_CHARS, MAX_LINE_BUFFER_BYTES).
9
+ */
10
+
11
+ import { logInternalError } from "../utils/internal-error.ts";
12
+ import type { ChildPiRunInput } from "./child-pi.ts";
13
+ import { MAX_ASSISTANT_TEXT_CHARS, MAX_LINE_BUFFER_BYTES, MAX_TOOL_INPUT_CHARS, MAX_TOOL_RESULT_CHARS } from "./child-pi-constants.ts";
14
+ import { appendTranscript, compactString, compactValue, flushPendingTranscriptWrites } from "./child-pi-transcript.ts";
15
+ import { extractText } from "./pi-json-output.ts";
16
+
17
+ function asRecord(value: unknown): Record<string, unknown> | undefined {
18
+ if (!value || typeof value !== "object" || Array.isArray(value)) return undefined;
19
+ return value as Record<string, unknown>;
20
+ }
21
+
22
+ function compactContentPart(part: unknown): unknown | undefined {
23
+ const record = asRecord(part);
24
+ if (!record) return undefined;
25
+ if (record.type === "text")
26
+ return {
27
+ type: "text",
28
+ text:
29
+ typeof record.text === "string"
30
+ ? compactString(record.text, MAX_ASSISTANT_TEXT_CHARS, {
31
+ preserveImportant: false,
32
+ })
33
+ : "",
34
+ };
35
+ if (record.type === "toolCall")
36
+ return {
37
+ type: "toolCall",
38
+ name: record.name,
39
+ input: compactValue(typeof record.input === "string" ? compactString(record.input, MAX_TOOL_INPUT_CHARS) : record.input),
40
+ };
41
+ if (record.type === "toolResult")
42
+ return {
43
+ type: "toolResult",
44
+ name: record.name,
45
+ content: compactValue(
46
+ typeof record.content === "string" ? compactString(record.content, MAX_TOOL_RESULT_CHARS) : record.content,
47
+ ),
48
+ };
49
+ return undefined;
50
+ }
51
+
52
+ function compactChildPiEvent(event: unknown): unknown | undefined {
53
+ const record = asRecord(event);
54
+ if (!record) return undefined;
55
+ if (record.type === "message_update") return undefined;
56
+ if (record.type === "tool_execution_start" || record.type === "tool_execution_end") {
57
+ return {
58
+ type: record.type,
59
+ toolName: record.toolName,
60
+ args: record.args,
61
+ };
62
+ }
63
+ if (record.type === "tool_result_end" || record.type === "message_end" || record.type === "message") {
64
+ const message = asRecord(record.message);
65
+ if (message?.role === "user" || message?.role === "system") return undefined;
66
+ const content = Array.isArray(message?.content)
67
+ ? message.content.map(compactContentPart).filter((part) => part !== undefined)
68
+ : undefined;
69
+ return {
70
+ type: record.type,
71
+ ...(typeof record.text === "string" ? { text: record.text } : {}),
72
+ ...(message
73
+ ? {
74
+ message: {
75
+ role: message.role,
76
+ ...(content ? { content } : {}),
77
+ usage: message.usage,
78
+ model: message.model,
79
+ errorMessage: message.errorMessage,
80
+ stopReason: message.stopReason,
81
+ },
82
+ }
83
+ : {}),
84
+ usage: record.usage,
85
+ model: record.model,
86
+ provider: record.provider,
87
+ stopReason: record.stopReason,
88
+ };
89
+ }
90
+ return record.type ? { type: record.type } : undefined;
91
+ }
92
+
93
+ function displayTextFromCompactEvent(event: unknown): string | undefined {
94
+ const record = asRecord(event);
95
+ if (!record) return undefined;
96
+ if (record.type === "tool_execution_start") {
97
+ return typeof record.toolName === "string" ? `tool: ${record.toolName}` : "tool started";
98
+ }
99
+ if (record.type !== "message" && record.type !== "message_end") return undefined;
100
+ const message = asRecord(record.message);
101
+ if (message?.role !== undefined && message.role !== "assistant") return undefined;
102
+ const content = Array.isArray(message?.content) ? message.content : [];
103
+ const text = content
104
+ .flatMap((part) => {
105
+ const item = asRecord(part);
106
+ return item?.type === "text" && typeof item.text === "string" ? [item.text] : [];
107
+ })
108
+ .join("\n")
109
+ .trim();
110
+ return text || (typeof record.text === "string" ? record.text : undefined);
111
+ }
112
+
113
+ function nonJsonLineResult(line: string): {
114
+ persistedLine: string;
115
+ event?: unknown;
116
+ displayLine?: string;
117
+ json: boolean;
118
+ } {
119
+ return { json: false, persistedLine: line, displayLine: line };
120
+ }
121
+
122
+ function compactChildPiLine(
123
+ line: string,
124
+ preParsed?: unknown,
125
+ ): {
126
+ persistedLine: string;
127
+ event?: unknown;
128
+ displayLine?: string;
129
+ json: boolean;
130
+ } {
131
+ // OPT-PHASE2: when the caller (emitLine) already parsed the line, pass the
132
+ // result via preParsed to avoid a redundant JSON.parse. Standalone callers
133
+ // without a preParsed fall back to their own parse+catch (DRY: single
134
+ // compact+return path for both branches).
135
+ let parsed: unknown;
136
+ if (preParsed !== undefined) {
137
+ parsed = preParsed;
138
+ } else {
139
+ try {
140
+ parsed = JSON.parse(line);
141
+ } catch {
142
+ return nonJsonLineResult(line);
143
+ }
144
+ }
145
+ const compact = compactChildPiEvent(parsed);
146
+ return {
147
+ json: true,
148
+ event: compact,
149
+ persistedLine: compact ? JSON.stringify(compact) : "",
150
+ displayLine: displayTextFromCompactEvent(compact),
151
+ };
152
+ }
153
+
154
+ export class ChildPiLineObserver {
155
+ private buffer = "";
156
+ private readonly input: ChildPiRunInput;
157
+ /** F9: bounded ring buffer for RAW assistant-text fragments. Consumers
158
+ * (getRawFinalText) only read the last element, but the legacy implementation
159
+ * accumulated every fragment unconditionally, which let a verbose/long-running
160
+ * worker grow this array linearly with output. We retain the last 2 entries:
161
+ * the consumer needs the last; we keep the second-to-last only as a defensive
162
+ * fence against a race where a final event arrives just after the consumer
163
+ * read (the previous "last" is still the most-recent pre-final text in that
164
+ * window). 2 is well below any plausible consumer's "tail-only" need while
165
+ * bounding memory. */
166
+ private static readonly MAX_RAW_TEXT_EVENTS = 2;
167
+ private readonly rawTextEvents: string[] = [];
168
+ /** F9: bounded ring buffer for intermediate findings. The downstream digest
169
+ * (getIntermediateFindings) slices the last 20, but the array previously grew
170
+ * to 1000s of entries. We keep MAX_INTERMEDIATE_DIGEST_LINES + headroom so
171
+ * the public API behaviour is preserved (still returns "last 20 lines"). */
172
+ private static readonly MAX_INTERMEDIATE_FINDINGS = 32;
173
+ private readonly intermediateFindings: string[] = [];
174
+
175
+ constructor(input: ChildPiRunInput) {
176
+ this.input = input;
177
+ }
178
+
179
+ observe(text: string): void {
180
+ this.buffer += text;
181
+ // Cap the buffer to prevent unbounded memory growth when a child process
182
+ // produces output without newlines (RT-F8). When exceeded, force-flush
183
+ // the buffer as a single line and log a warning.
184
+ if (this.buffer.length > MAX_LINE_BUFFER_BYTES) {
185
+ logInternalError(
186
+ "child-pi.buffer-overflow",
187
+ new Error(`Line buffer exceeded ${MAX_LINE_BUFFER_BYTES} bytes; force-flushing`),
188
+ `bufferLen=${this.buffer.length}`,
189
+ );
190
+ const line = this.buffer;
191
+ this.buffer = "";
192
+ this.emitLine(line);
193
+ return;
194
+ }
195
+ const lines = this.buffer.split(/\r?\n/);
196
+ this.buffer = lines.pop() ?? "";
197
+ for (const line of lines) this.emitLine(line);
198
+ }
199
+
200
+ flush(): Promise<void> {
201
+ if (this.buffer) {
202
+ const line = this.buffer;
203
+ this.buffer = "";
204
+ this.emitLine(line);
205
+ }
206
+ // OPT-06 follow-up: appendTranscript is fire-and-forget async, so the file
207
+ // may not exist on disk by the time this returns. Drain the module-scoped
208
+ // transcript batch buffer before resolving so callers that immediately read the
209
+ // transcript file (e.g. integration tests at phase4-runtime:37/:68/:103
210
+ // after `await observer.flush()`) see the full content.
211
+ return flushPendingTranscriptWrites();
212
+ }
213
+
214
+ /** Last non-empty RAW assistant text (mirrors {@link parsePiJsonOutput}'s
215
+ * finalText semantics but uncapped). Undefined when no assistant text was
216
+ * seen by this observer. {@link extractText} already drops empty fragments,
217
+ * so the last entry is the final assistant utterance. */
218
+ getRawFinalText(): string | undefined {
219
+ return this.rawTextEvents.length > 0 ? this.rawTextEvents[this.rawTextEvents.length - 1] : undefined;
220
+ }
221
+
222
+ /** #7 hardening: returns a bounded digest of intermediate findings accumulated
223
+ * during the run. This is NOT the final answer — it is a best-effort capture
224
+ * of the last assistant text or tool-result display lines before budget
225
+ * exhaustion. Only populated when getRawFinalText() would return undefined.
226
+ * @param maxChars - maximum total characters to return (default 500). */
227
+ getIntermediateFindings(maxChars = 500): string {
228
+ const MAX_INTERMEDIATE_DIGEST_LINES = 20;
229
+ if (this.intermediateFindings.length === 0) return "";
230
+ // Take the last N lines and join, then cap.
231
+ const lines = this.intermediateFindings.slice(-MAX_INTERMEDIATE_DIGEST_LINES);
232
+ const joined = lines.join("\n");
233
+ if (joined.length <= maxChars) return joined;
234
+ // Return the tail within the budget.
235
+ return joined.slice(-maxChars);
236
+ }
237
+
238
+ private emitLine(line: string): void {
239
+ if (!line.trim()) return;
240
+ // OPT-PHASE2: parse the line EXACTLY ONCE. The parsed value feeds both
241
+ // (a) raw assistant-text extraction for the authoritative result and
242
+ // (b) compaction for the telemetry transcript — previously each path
243
+ // called JSON.parse independently (2 parses/line). When the line is not
244
+ // valid JSON, parsed stays undefined and compactChildPiLine runs its own
245
+ // catch path to produce the json:false fallback.
246
+ let parsed: unknown;
247
+ try {
248
+ parsed = JSON.parse(line);
249
+ } catch {
250
+ parsed = undefined;
251
+ }
252
+ if (parsed !== undefined) {
253
+ const rawTexts = extractText(parsed);
254
+ if (rawTexts.length > 0) {
255
+ // F9: trim from the front if the push would exceed the cap. Slice's
256
+ // second arg excludes the index, so this drops the oldest entries
257
+ // while keeping the freshly pushed tail.
258
+ this.rawTextEvents.push(...rawTexts);
259
+ const rawOverflow = this.rawTextEvents.length - ChildPiLineObserver.MAX_RAW_TEXT_EVENTS;
260
+ if (rawOverflow > 0) this.rawTextEvents.splice(0, rawOverflow);
261
+ // Also capture raw assistant text as intermediate findings — the last raw
262
+ // text may be a partial answer before the worker ran out of budget.
263
+ const last = rawTexts[rawTexts.length - 1];
264
+ if (last.trim().length > 0) {
265
+ this.intermediateFindings.push(last.trim());
266
+ const findingsOverflow = this.intermediateFindings.length - ChildPiLineObserver.MAX_INTERMEDIATE_FINDINGS;
267
+ if (findingsOverflow > 0) this.intermediateFindings.splice(0, findingsOverflow);
268
+ }
269
+ }
270
+ }
271
+ // OPT-PHASE2: construct the non-JSON fallback directly when parsing failed,
272
+ // so a broken line triggers exactly ONE (failed) parse instead of two.
273
+ const compact = parsed !== undefined ? compactChildPiLine(line, parsed) : nonJsonLineResult(line);
274
+ if (compact.event !== undefined) {
275
+ try {
276
+ this.input.onJsonEvent?.(compact.event);
277
+ } catch (error) {
278
+ logInternalError("child-pi.on-json-event", error, `line=${compact.persistedLine ?? compact.displayLine ?? ""}`);
279
+ }
280
+ }
281
+ if (compact.persistedLine) appendTranscript(this.input, compact.persistedLine);
282
+ if (compact.displayLine?.trim()) {
283
+ try {
284
+ this.input.onStdoutLine?.(compact.displayLine);
285
+ } catch (error) {
286
+ logInternalError("child-pi.on-stdout-line", error, `line=${compact.displayLine}`);
287
+ }
288
+ // #7 hardening: capture display lines (tool results, stdout) as intermediate
289
+ // findings. This ensures we capture tool output even when no assistant text
290
+ // is emitted (budget exhausted on tool calls).
291
+ this.intermediateFindings.push(compact.displayLine!.trim());
292
+ const findingsOverflow = this.intermediateFindings.length - ChildPiLineObserver.MAX_INTERMEDIATE_FINDINGS;
293
+ if (findingsOverflow > 0) this.intermediateFindings.splice(0, findingsOverflow);
294
+ }
295
+ }
296
+ }