pi-ptc-subagents 0.1.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.
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","names":["dispatchBinding","DESCRIPTION","PARAMETERS"],"sources":["../src/runtime/dispatch.ts","../src/runtime/bindings.ts","../src/runtime/limits.ts","../src/runtime/protocol.ts","../src/runtime/worker-main.ts","../src/runtime/worker-source.ts","../src/runtime/dispatcher.ts","../src/tools/text.ts","../src/tools/common.ts","../src/tools/render.ts","../src/tools/run-code.ts","../src/tools/workflow.ts","../src/mode/ptc-mode.ts","../src/mode/skills-section.ts","../src/index.ts"],"sourcesContent":["/**\n * pi.dispatch: a parallel binding that spawns a fresh pi subprocess per call.\n *\n * ADR-0016 (2026-09-23) is the contract. This file is the type surface and the spawn\n * skeleton; the actual subprocess plumbing (agent-md resolution, JSON-line parser,\n * usage accumulator, signal propagation across worker_threads / child_process) is a\n * follow-up. The stubs here are typed against the contract so call sites can land\n * before the spawn path is finalised, and so a wrong shape is a compile error rather\n * than a runtime one.\n *\n * Behaviourally compatible with pi's examples/extensions/subagent/index.ts reference\n * (--mode json, -p, --no-session, --append-system-prompt <tmpfile>), but not cooperative:\n * does not require the user to install pi's subagent extension. See ADR-0016 section 2.\n */\n\nimport { spawn } from \"node:child_process\";\nimport * as fs from \"node:fs\";\nimport * as os from \"node:os\";\nimport * as path from \"node:path\";\nimport type { ThinkingLevel } from \"@earendil-works/pi-ai\";\n\n/**\n * The CLI command used to invoke a fresh pi subprocess for one dispatch.\n *\n * Always `\"pi\"` — pi-ptc is a library, not a CLI, and `dispatch` must spawn the\n * separate `pi` binary regardless of how the host process was launched. An\n * earlier copy of this function mirrored pi's own subagent extension and tried\n * to reuse `process.execPath + process.argv[1]`, which works only when the host\n * is `pi` itself; in any other host (test scripts, the dispatcher host process,\n * etc.) the function would spawn the host script in place of `pi`, producing\n * infinite recursion and a stdout pipe that never drains (so `proc.on('close')`\n * never fires). The caller surfaces a missing-binary condition via the child's\n * `error` event — `spawn` itself does not throw synchronously on ENOENT, the\n * async `error` listener does.\n */\nconst PI_COMMAND = \"pi\";\n\n/** Per-call input. Mirrors pi's subagent extension's parameters (single-mode only). */\nexport interface DispatchInput {\n agent: string;\n task: string;\n cwd?: string;\n agentScope?: \"user\" | \"project\" | \"both\";\n model?: string;\n thinkingLevel?: ThinkingLevel;\n}\n\n/** Token usage accumulated from the child's message_end events. Optional. */\nexport interface DispatchUsage {\n input: number;\n output: number;\n cacheRead: number;\n cacheWrite: number;\n cost: number;\n turns: number;\n}\n\n/** Structured return value of pi.dispatch(...). Shape mirrors Promise.allSettled records. */\nexport interface DispatchResult {\n text: string;\n status: \"fulfilled\" | \"rejected\";\n agentName: string;\n durationMs: number;\n exitCode: number;\n usage?: DispatchUsage;\n stderr?: string;\n errorMessage?: string;\n}\n\n/** Discovery of an agent by name; mirrors pi's subagent extension AgentConfig shape. */\nexport interface AgentConfigLike {\n readonly name: string;\n readonly source: \"user\" | \"project\" | \"unknown\";\n readonly model?: string;\n readonly systemPrompt: string;\n readonly tools?: readonly string[];\n}\n\n/** Per-run context handed to a dispatch binding; same shape as BindingContext. */\nexport interface DispatchContext {\n signal?: AbortSignal;\n callId: number;\n /** Run cwd (F4); default for `DispatchInput.cwd` when not overridden. */\n cwd: string;\n /** Depth of the calling PTC run: 0 for the parent turn, 1+ for a child of `pi.dispatch`. */\n depth: number;\n /** Maximum allowed depth for any run reachable from this dispatch (ADR-0016 Recursive section). */\n maxDepth: number;\n}\n\n/**\n * Compose the system prompt handed to the child subprocess.\n *\n * The agent's own markdown body is the user-supplied content; pi-ptc appends a\n * `<pi-ptc-context depth=\"N\" max-depth=\"M\">...</pi-ptc-context>` hint so the child\n * agent can see how much recursion room it has. The shape mirrors DSH's existing\n * subagent-context injection so the field recognises it.\n */\nexport function appendDepthHint(systemPrompt: string, depth: number, maxDepth: number): string {\n const hint = [\n '<pi-ptc-context depth=\"' + depth + '\" max-depth=\"' + maxDepth + '\">',\n \"You are a PTC run at depth \" +\n depth +\n \" (root is depth 0). You may write PTC programs and you may\",\n \"call pi.dispatch(...) to spawn further children, but each level costs a fresh pi subprocess.\",\n \"The remaining depth budget is \" +\n maxDepth +\n \" - \" +\n depth +\n \". Beyond it, pi.dispatch rejects with\",\n '{ status: \"rejected\", errorMessage: \"dispatch depth limit reached\" }.',\n \"</pi-ptc-context>\",\n ].join(\"\\n\");\n return systemPrompt.length === 0 ? hint : systemPrompt + \"\\n\\n\" + hint;\n}\n\n/** Default error returned when the depth limit is exceeded. */\nexport function dispatchDepthLimitReached(currentDepth: number): DispatchResult {\n return {\n text: \"\",\n status: \"rejected\",\n agentName: \"unknown\",\n durationMs: 0,\n exitCode: -1,\n errorMessage:\n \"dispatch depth limit reached (current depth \" + currentDepth + \", max-depth exceeded)\",\n };\n}\n\n/**\n * Inputs to {@link decideCloseOutcome}: the shape the child's `close` event\n * hands the host, distilled to the fields that drive the resolve decision.\n */\nexport interface CloseOutcomeInput {\n /** `code ?? -1` from the child's close event. `-1` covers signal kill and no-code exit. */\n exitCode: number;\n /** Accumulated text from the last assistant message_end event. */\n finalText: string;\n /** `true` when `ctx.signal` aborted before this close fired. */\n aborted: boolean;\n}\n\n/** Result of {@link decideCloseOutcome} — what the dispatcher commits to. */\nexport interface CloseOutcome {\n status: \"fulfilled\" | \"rejected\";\n errorMessage?: string;\n}\n\n/**\n * Decide the outcome of one `dispatch(...)` based on the child's `close` event.\n *\n * Cancellation has priority over every other branch: when the host's signal\n * aborted the run, the child exited because we asked it to, and the contract\n * (ADR-0016 §4) is to surface \"dispatch cancelled\" rather than leave the\n * caller guessing why an aborted run came back `rejected`.\n *\n * Non-cancellation cases follow the original close-handler ladder:\n * - exit 0 with final text → fulfilled\n * - exit 0 without final text → rejected (the model never answered)\n * - any other exit code → rejected, no errorMessage (spawn-error path labels\n * its own failures with `failed to spawn pi: ...`)\n */\nexport function decideCloseOutcome(input: CloseOutcomeInput): CloseOutcome {\n if (input.aborted) {\n return { status: \"rejected\", errorMessage: \"dispatch cancelled\" };\n }\n if (input.exitCode === 0 && input.finalText.length > 0) {\n return { status: \"fulfilled\" };\n }\n if (input.exitCode === 0) {\n return { status: \"rejected\", errorMessage: \"dispatch produced no final text\" };\n }\n return { status: \"rejected\" };\n}\n\n/**\n * Minimal interface for the bits of `node:child_process`'s ChildProcess that\n * `safeKill` touches — narrow on purpose so tests can pass plain objects.\n *\n * `pid` is optional on `ChildProcess` (`pid?: number`); a fresh process whose\n * pid has not yet been assigned reports `undefined`, and the kill must be a\n * no-op rather than a TS error. `killed` is intentionally absent: callers read\n * it on the real ChildProcess directly, not through this helper's contract.\n */\nexport interface Killable {\n pid?: number | undefined;\n kill(signal: NodeJS.Signals): boolean;\n}\n\n/**\n * Send `signal` to `proc` iff the process is still attached, swallowing\n * \"process already gone\" throws. Returns whether a kill was actually issued.\n *\n * `proc.pid === undefined` happens for processes spawned without a usable\n * pid (rare, but TS-strict demands the guard); `proc.kill` throwing happens\n * for processes that exited between the guard and the syscall (the OS hands\n * back ESRCH). Both are \"no-op successfully\" for our purposes.\n */\nexport function safeKill(proc: Killable, signal: NodeJS.Signals): boolean {\n if (proc.pid === undefined) return false;\n try {\n proc.kill(signal);\n return true;\n } catch {\n return false;\n }\n}\n\n/**\n * Minimal YAML frontmatter scalar parser. Returns the trimmed value, with surrounding\n * double or single quotes stripped when both are present.\n */\nfunction extractYamlString(yaml: string, key: string): string | undefined {\n const m = yaml.match(new RegExp(\"^\" + key + \":\\\\s*(.+)$\", \"m\"));\n if (!m) return undefined;\n let v = (m[1] ?? \"\").trim();\n if (v.length >= 2) {\n const first = v[0];\n const last = v[v.length - 1];\n if ((first === '\"' && last === '\"') || (first === \"'\" && last === \"'\")) {\n v = v.slice(1, -1);\n }\n }\n return v.length === 0 ? undefined : v;\n}\n\n/**\n * Minimal YAML list parser: accepts inline `[a, b, c]` and bare `a, b, c`.\n */\nfunction extractYamlList(yaml: string, key: string): string[] | undefined {\n const m = yaml.match(new RegExp(\"^\" + key + \":\\\\s*(.+)$\", \"m\"));\n if (!m) return undefined;\n const raw = (m[1] ?? \"\").trim();\n let inner = raw;\n if (inner.startsWith(\"[\") && inner.endsWith(\"]\")) {\n inner = inner.slice(1, -1);\n }\n const parts = inner\n .split(\",\")\n .map((s) => s.trim())\n .filter((s) => s.length > 0);\n return parts.length === 0 ? undefined : parts;\n}\n\n/**\n * Parse an agent markdown file: YAML frontmatter (name / description / model / tools)\n * plus the body, returned as the system prompt. Tolerates a missing or partial\n * frontmatter; the caller falls back to defaults when this returns null.\n */\nexport function parseAgentMarkdown(content: string): {\n name: string;\n description?: string;\n model?: string;\n tools?: readonly string[];\n systemPrompt: string;\n} | null {\n const fmMatch = content.match(/^---\\n([\\s\\S]*?)\\n---\\n([\\s\\S]*)$/);\n if (!fmMatch) return null;\n const frontmatter = fmMatch[1] ?? \"\";\n const body = fmMatch[2] ?? \"\";\n const name = extractYamlString(frontmatter, \"name\");\n if (!name) return null;\n const description = extractYamlString(frontmatter, \"description\") ?? \"\";\n const model = extractYamlString(frontmatter, \"model\") ?? \"\";\n const tools = extractYamlList(frontmatter, \"tools\");\n const out: {\n name: string;\n description?: string;\n model?: string;\n tools?: readonly string[];\n systemPrompt: string;\n } = {\n name,\n systemPrompt: body.trim(),\n };\n if (description !== undefined) out.description = description;\n if (model !== undefined) out.model = model;\n if (tools !== undefined) out.tools = tools;\n return out;\n}\n\n/**\n * Discover an agent by name. Mirrors pi's subagent extension agentScope behaviour:\n * user-level (`~/.pi/agent/agents/`) is loaded by default; project-level\n * (`<cwd>/.pi/agents/`) is loaded only when agentScope includes it.\n */\nexport function discoverAgent(\n name: string,\n cwd: string,\n agentScope: \"user\" | \"project\" | \"both\",\n): AgentConfigLike | null {\n const userDir = path.join(os.homedir(), \".pi\", \"agent\", \"agents\");\n const projectDir = path.join(cwd, \".pi\", \"agents\");\n type Candidate = { path: string; source: \"user\" | \"project\" };\n const candidates: Candidate[] = [];\n if (agentScope === \"user\" || agentScope === \"both\") {\n candidates.push({ path: path.join(userDir, name + \".md\"), source: \"user\" });\n }\n if (agentScope === \"project\" || agentScope === \"both\") {\n candidates.push({ path: path.join(projectDir, name + \".md\"), source: \"project\" });\n }\n for (const c of candidates) {\n let raw: string;\n try {\n raw = fs.readFileSync(c.path, \"utf-8\");\n } catch {\n continue;\n }\n const parsed = parseAgentMarkdown(raw);\n if (parsed && parsed.name === name) {\n const out: AgentConfigLike = {\n name: parsed.name,\n source: c.source,\n systemPrompt: parsed.systemPrompt,\n };\n if (parsed.model !== undefined) (out as { model?: string }).model = parsed.model;\n if (parsed.tools !== undefined) (out as { tools?: readonly string[] }).tools = parsed.tools;\n return out;\n }\n }\n return null;\n}\n\n/**\n * Build the argv handed to the child pi subprocess. Model / thinking / tools / agentScope\n * precedence: dispatch input (call site) > agent markdown frontmatter > nothing.\n * The agent's system prompt (with the depth hint appended) is materialised to a tmpfile\n * and passed via --append-system-prompt, exactly as the subagent extension does.\n */\nexport function buildArgv(\n input: DispatchInput,\n agent: AgentConfigLike,\n promptFilePath: string,\n): string[] {\n const args: string[] = [\"--mode\", \"json\", \"-p\", \"--no-session\"];\n const model = input.model ?? agent.model;\n if (model) args.push(\"--model\", model);\n /*\n * Default to --thinking off: a dispatched child is a tool call, not a long-form reasoning\n * task. Thinking blocks the model from emitting text for many seconds; the parent turn keeps\n * its own thinking settings, the child inherits only what the parent's DispatchInput.\n * thinkingLevel says (and defaults to \"off\" otherwise).\n */\n const thinking = input.thinkingLevel ?? \"off\";\n\n args.push(\"--thinking\", thinking);\n // Note: agentScope is honoured at our layer via discoverAgent(); pi itself does not\n // accept an --agent-scope flag (the subagent extension does not pass one either), and\n // in --no-session mode pi loads user-scope agents from ~/.pi/agent/agents by default.\n // Project agents would need a different mechanism (e.g. PI_CODING_AGENT_DIR override).\n if (agent.tools && agent.tools.length > 0) {\n args.push(\"--tools\", agent.tools.join(\",\"));\n }\n args.push(\"--append-system-prompt\", promptFilePath);\n args.push(\"Task: \" + input.task);\n return args;\n}\n\n/**\n * One line of the child's stdout, parsed as JSON. The child emits the events described\n * in examples/extensions/subagent/index.ts (message_end, tool_result_end, error).\n * Anything unparseable is dropped: the line is one event the host cannot interpret, and\n * the run continues.\n */\ninterface ParsedAgentEvent {\n type: string;\n message?: {\n role?: string;\n content?: ReadonlyArray<{ type?: string; text?: string }>;\n usage?: {\n input?: number;\n output?: number;\n cacheRead?: number;\n cacheWrite?: number;\n cost?: { total?: number };\n };\n model?: string;\n stopReason?: string;\n errorMessage?: string;\n };\n message_text?: string;\n}\n\nexport function parseAgentEvent(line: string): ParsedAgentEvent | null {\n const trimmed = line.trim();\n if (trimmed.length === 0) return null;\n try {\n return JSON.parse(trimmed) as ParsedAgentEvent;\n } catch {\n return null;\n }\n}\n/**\n * Write the system prompt (with depth hint appended) to a tmpfile. The caller is\n * responsible for cleanup; we use mkdtemp + 0o600 to keep the prompt private, then\n * best-effort cleanup in the dispatch() Promise.\n */\nasync function writePromptToTempFile(\n agentName: string,\n prompt: string,\n): Promise<{ dir: string; filePath: string }> {\n const tmpDir = await fs.promises.mkdtemp(path.join(os.tmpdir(), \"pi-dispatch-\"));\n const safeName = agentName.replace(/[^\\w.-]+/g, \"_\");\n const filePath = path.join(tmpDir, \"prompt-\" + safeName + \".md\");\n await fs.promises.writeFile(filePath, prompt, { encoding: \"utf-8\", mode: 0o600 });\n return { dir: tmpDir, filePath };\n}\n\n/**\n * Best-effort cleanup of the tmpfile + dir used for the system prompt. Errors are\n * deliberately swallowed: leaving a tmpfile around is preferable to crashing the run.\n */\nfunction cleanupTmp(tmp: { dir: string; filePath: string }): void {\n try {\n fs.unlinkSync(tmp.filePath);\n } catch {\n /* ignore */\n }\n try {\n fs.rmdirSync(tmp.dir);\n } catch {\n /* ignore */\n }\n}\n\n/**\n * Spawn one pi subprocess for one dispatch and resolve with a DispatchResult.\n *\n * Behavioural contract (ADR-0016 sections 1, 3, 4):\n * - Resolves with status: fulfilled when the child exits 0 and emits at least one\n * assistant message whose text is non-empty.\n * - Resolves with status: rejected on non-zero exit, signal kill, spawn failure,\n * unknown agent, or no usable final text.\n * - Never rejects. The caller can pattern-match on outcome.status the same way\n * it would pattern-match a Promise.allSettled record.\n * - Honours signal with SIGTERM, then SIGKILL after a 5s grace window.\n */\nexport async function dispatch(\n input: DispatchInput,\n ctx: DispatchContext,\n): Promise<DispatchResult> {\n // ADR-0016 Recursive dispatch: bound recursion explicitly.\n const childDepth = ctx.depth + 1;\n if (childDepth > ctx.maxDepth) {\n return dispatchDepthLimitReached(ctx.depth);\n }\n\n const cwd = input.cwd ?? ctx.cwd;\n const agentScope = input.agentScope ?? \"user\";\n const start = Date.now();\n\n const agent = discoverAgent(input.agent, cwd, agentScope);\n if (!agent) {\n return {\n text: \"\",\n status: \"rejected\",\n agentName: input.agent,\n durationMs: Date.now() - start,\n exitCode: 1,\n errorMessage:\n \"unknown agent: \" + input.agent + \" (agentScope=\" + agentScope + \", cwd=\" + cwd + \")\",\n };\n }\n\n const fullPrompt = appendDepthHint(agent.systemPrompt, childDepth, ctx.maxDepth);\n const tmp = await writePromptToTempFile(agent.name, fullPrompt);\n\n const argv = buildArgv(input, agent, tmp.filePath);\n\n return await new Promise<DispatchResult>((resolve) => {\n let stdoutBuffer = \"\";\n let stderrBuf = \"\";\n let finalText = \"\";\n let usage: DispatchUsage | undefined;\n let exitCode = -1;\n let resolved = false;\n let killTimer: NodeJS.Timeout | undefined;\n let proc: ReturnType<typeof spawn> | undefined;\n let aborted = false;\n\n const finalize = (status: \"fulfilled\" | \"rejected\", errorMessage?: string): void => {\n if (resolved) return;\n resolved = true;\n if (killTimer) clearTimeout(killTimer);\n if (ctx.signal) ctx.signal.removeEventListener(\"abort\", onAbort);\n cleanupTmp(tmp);\n const out: DispatchResult = {\n text: finalText,\n status,\n agentName: agent.name,\n durationMs: Date.now() - start,\n exitCode,\n };\n if (usage) out.usage = usage;\n if (stderrBuf.length > 0) out.stderr = stderrBuf;\n if (errorMessage) out.errorMessage = errorMessage;\n resolve(out);\n };\n\n const onAbort = (): void => {\n if (!proc || proc.killed || resolved) return;\n aborted = true;\n safeKill(proc, \"SIGTERM\");\n killTimer = setTimeout(() => {\n if (proc && !proc.killed) safeKill(proc, \"SIGKILL\");\n }, 5000);\n };\n\n try {\n proc = spawn(PI_COMMAND, argv, {\n cwd,\n shell: false,\n stdio: [\"ignore\", \"pipe\", \"pipe\"],\n });\n } catch (err) {\n const message = err instanceof Error ? err.message : String(err);\n finalize(\"rejected\", \"failed to spawn pi: \" + message);\n return;\n }\n\n if (ctx.signal) {\n if (ctx.signal.aborted) onAbort();\n else ctx.signal.addEventListener(\"abort\", onAbort, { once: true });\n }\n\n const stdout = proc.stdout;\n if (!stdout) return;\n stdout.on(\"data\", (data: Buffer) => {\n stdoutBuffer += data.toString(\"utf-8\");\n let nl = stdoutBuffer.indexOf(\"\\n\");\n while (nl >= 0) {\n const line = stdoutBuffer.slice(0, nl);\n stdoutBuffer = stdoutBuffer.slice(nl + 1);\n const ev = parseAgentEvent(line);\n if (!ev) {\n nl = stdoutBuffer.indexOf(\"\\n\");\n continue;\n }\n if (ev.type === \"message_end\" && ev.message && ev.message.role === \"assistant\") {\n const m = ev.message;\n if (m.usage) {\n const cur: DispatchUsage = usage ?? {\n input: 0,\n output: 0,\n cacheRead: 0,\n cacheWrite: 0,\n cost: 0,\n turns: 0,\n };\n cur.input += m.usage.input ?? 0;\n cur.output += m.usage.output ?? 0;\n cur.cacheRead += m.usage.cacheRead ?? 0;\n cur.cacheWrite += m.usage.cacheWrite ?? 0;\n cur.cost += m.usage.cost?.total ?? 0;\n cur.turns += 1;\n usage = cur;\n }\n if (Array.isArray(m.content)) {\n for (const part of m.content) {\n if (part && part.type === \"text\" && typeof part.text === \"string\") {\n finalText = part.text;\n }\n }\n }\n }\n nl = stdoutBuffer.indexOf(\"\\n\");\n }\n });\n\n const stderrStream = proc.stderr;\n if (!stderrStream) return;\n stderrStream.on(\"data\", (data: Buffer) => {\n stderrBuf += data.toString(\"utf-8\");\n });\n\n proc.on(\"close\", (code) => {\n exitCode = code ?? -1;\n const { status, errorMessage } = decideCloseOutcome({ exitCode, finalText, aborted });\n finalize(status, errorMessage);\n });\n\n proc.on(\"error\", (err) => {\n stderrBuf += \"[spawn-error] \" + err.message + \"\\n\";\n finalize(\"rejected\", \"failed to spawn pi: \" + err.message);\n });\n });\n}\n","/**\n * Bindings: the pi tools a PTC program may call as `tools.<name>(args)`.\n *\n * R5 found that pi's extension API exposes no tool-dispatch surface — `pi.on('tool_call')`\n * only fires from the agent loop, `extensionsResult.runtime` is state plumbing, and\n * `pi.exec` has no `execArgv`/`resourceLimits`/`stdio`. So a binding builds the tool with\n * pi's own SDK factory and calls `execute()` on it directly. That keeps the tool\n * implementation identical to the native one, but it also means worker-driven calls are\n * **not** seen by pi's tool-call pipeline: `tool_call` guards, `protected-paths`-style\n * extensions, permission gates and sandbox overrides never run for them. This is the\n * inherited risk ADR-0005 accepts and records — it must not be described as resolved, and\n * nothing here may pretend to route through `pi.on('tool_call')`.\n *\n * Arguments are validated with pi's own `validateToolArguments`, the same helper the agent\n * loop uses, so a bad call fails with the same message (and the same coercion) it would\n * get from the native pipeline.\n */\nimport { validateToolArguments } from \"@earendil-works/pi-ai\";\nimport {\n createBashTool,\n createEditTool,\n createFindTool,\n createGrepTool,\n createLsTool,\n createReadTool,\n createWriteTool,\n} from \"@earendil-works/pi-coding-agent\";\nimport type { TSchema } from \"typebox\";\n\nimport { dispatch as dispatchBinding, type DispatchInput } from \"./dispatch.ts\";\n\n/** pi's built-in tools that can be exposed as bindings, in native order. */\nexport const BUILTIN_BINDING_NAMES = [\n \"read\",\n \"bash\",\n \"edit\",\n \"write\",\n \"grep\",\n \"find\",\n \"ls\",\n] as const;\nexport type BuiltinBindingName = (typeof BUILTIN_BINDING_NAMES)[number];\n\n/**\n * Bindings exposed when the caller does not pass an explicit name list.\n *\n * `bash` is included on purpose: DSH's PTC preset still mounts `tool-bash`/`tool-pwsh`\n * (R1 §2 — PTC mode hides the tools from the wire list and re-exposes the same registry\n * as bindings), so a PTC program written against DSH may run shell commands. Leaving it\n * out would silently shrink the surface relative to DSH. Callers that want a read-only\n * PTC surface pass an explicit subset.\n */\n/** Parallel binding name (ADR-0016). Always bound alongside the builtin set;\n * opt-out is the callers responsibility via an explicit subset to\n * createBuiltinBindings (today the subset is restricted to builtin names,\n * so opt-out is effectively use a future flag). */\nexport const DISPATCH_BINDING_NAME = \"pi.dispatch\" as const;\n\nexport const DEFAULT_BINDING_NAMES: readonly BuiltinBindingName[] = BUILTIN_BINDING_NAMES;\n\nexport interface BindingContext {\n /** Aborted when the run is cancelled, times out, or settles. */\n signal?: AbortSignal;\n /** Wire call id; also used to build the tool call id the tools see. */\n callId: number;\n /** Depth of the current PTC run (0 for parent turn, 1+ for a child of `pi.dispatch`). */\n depth: number;\n /** Maximum allowed depth; passed through to `pi.dispatch` for the depth check. */\n maxDispatchDepth: number;\n}\n\nexport interface Binding {\n readonly name: string;\n execute(args: unknown, context: BindingContext): Promise<unknown>;\n}\n\nexport type BindingTable = ReadonlyMap<string, Binding>;\n\n/**\n * Structural view of an SDK tool, wide enough for every `createXxxTool(cwd)` factory.\n *\n * `params: never` is what makes the seven typed factories assignable to one shape: a\n * function that takes `Static<typeof readSchema>` accepts a `never`, and `never` is what\n * the wire hands us before validation.\n */\ninterface AnyBuiltinTool {\n readonly name: string;\n readonly description: string;\n readonly parameters: TSchema;\n execute(\n toolCallId: string,\n params: never,\n signal?: AbortSignal,\n onUpdate?: unknown,\n ): Promise<{ content?: unknown; details?: unknown }>;\n}\n\nconst BUILTIN_TOOL_FACTORIES: Record<BuiltinBindingName, (cwd: string) => AnyBuiltinTool> = {\n read: createReadTool,\n bash: createBashTool,\n edit: createEditTool,\n write: createWriteTool,\n grep: createGrepTool,\n find: createFindTool,\n ls: createLsTool,\n};\n\n/** Argument type of pi's `validateToolArguments`, i.e. pi-ai's `ToolCall`. */\ntype ToolCallLike = Parameters<typeof validateToolArguments>[1];\n\nexport interface CreateBuiltinBindingsOptions {\n /** Per-run working directory: the F4 `RunConfig.cwd`, used to build every tool. */\n cwd: string;\n /** Subset of {@link BUILTIN_BINDING_NAMES}; defaults to all of them (bash included). */\n names?: readonly string[];\n}\n\n/**\n * Build the binding table for one run.\n *\n * Tools are created once per table and reused across calls, matching pi's own extension\n * examples (`createXxxTool(cwd)` caches nothing internally, so one instance per run is\n * the intended granularity).\n */\nexport function createBuiltinBindings(options: CreateBuiltinBindingsOptions): BindingTable {\n const names = options.names ?? DEFAULT_BINDING_NAMES;\n const table = new Map<string, Binding>();\n for (const name of names) {\n const factory = BUILTIN_TOOL_FACTORIES[name as BuiltinBindingName];\n if (!factory) {\n throw new TypeError(\n `unknown PTC binding \"${name}\"; known bindings: ${BUILTIN_BINDING_NAMES.join(\", \")}`,\n );\n }\n const tool = factory(options.cwd);\n table.set(name, {\n name,\n execute: async (args, context) => {\n const toolCallId = `ptc:${context.callId}`;\n const toolCall = {\n type: \"toolCall\",\n id: toolCallId,\n name: tool.name,\n arguments: args,\n } as unknown as ToolCallLike;\n const validated = validateToolArguments(tool, toolCall);\n const result = await tool.execute(\n toolCallId,\n validated as never,\n context.signal,\n undefined,\n );\n // What crosses the wire is the tool's model-facing payload: `usage` and\n // `terminate` are agent-loop plumbing with no meaning inside a PTC program.\n return {\n content: result.content,\n details: result.details === undefined ? null : result.details,\n };\n },\n });\n }\n // Register the parallel binding alongside the builtin set (ADR-0016).\n // The binding is added unconditionally when the caller accepts the default set,\n // but an explicit subset (the read-only PTC surface pattern, R3) is honoured:\n // `pi.dispatch` is not mixed into a caller-curated list, because the caller\n // has signalled they want a specific surface.\n if (names === DEFAULT_BINDING_NAMES) {\n table.set(DISPATCH_BINDING_NAME, {\n name: DISPATCH_BINDING_NAME,\n execute: async (args, context) => {\n return dispatchBinding(args as DispatchInput, {\n signal: context.signal,\n callId: context.callId,\n cwd: options.cwd,\n depth: context.depth,\n maxDepth: context.maxDispatchDepth,\n });\n },\n });\n }\n return table;\n}\n","/**\n * PTC run limits and spawn-time hardening, in one frozen `DEFAULT_CONFIG`.\n *\n * The numbers are DSH's (`dsh-v0.1.6-alpha.2`, `@deepseek-ai/dsh-ptc-runtime-node`)\n * carried over verbatim — see ADR-0003 (output budget), ADR-0004 (pending calls) and\n * ADR-0005 (execution boundary, F1–F4). Tests assert against these constants rather\n * than repeating the literals, so a future re-sync only has to change this file.\n *\n * Deliberately absent:\n * - `syncTimeoutMs` / `maxConcurrentAgents` / `maxTotalAgents` — workflow-engine caps\n * for a cooperative VM and for `agent()`. We run the program directly in the worker\n * realm (no VM) and ship no `agent()` (G1 #13 → B), so there is nothing to cap.\n * - `sandbox-unavailable` is not an error kind either: ADR-0007 ships no OS sandbox.\n */\n\n/** The two worker surfaces. Each run gets a fresh worker with exactly one of them. */\nexport type PtcSurface = \"run_code\" | \"workflow\";\n\nexport interface PtcConfig {\n /** Default elapsed deadline for a run, including nested binding waits (R1 §1). */\n timeoutMs: number;\n /** Ceiling the requested deadline is clamped to. */\n maxTimeoutMs: number;\n /** Joint budget for serialized logs + completion value (ADR-0003). */\n maxOutputBytes: number;\n /** Cap on a single control frame, either direction (R1 §1). */\n maxMessageBytes: number;\n /** Admission control for simultaneously in-flight worker→host binding calls (ADR-0004). */\n maxPendingCalls: number;\n /** Concurrent binding dispatches; DSH's `maxParallelSubCalls` (ADR-0004 consequence).\n * Renamed in spirit by ADR-0016 section 2: the cap that really matters for\n * resource safety is the per-run `dispatchConcurrency` below. This field\n * is kept for backward compatibility (and for the in-process builtin\n * binding fan-out) but is no longer the authoritative limit on the\n * parallel binding `pi.dispatch`. */\n maxParallelSubCalls: number;\n /** Per-run hard cap on concurrently in-flight `pi.dispatch(...)` calls.\n * Default 8, matches pi's `subagent` extension `MAX_PARALLEL_TASKS`.\n * ADR-0016 section 2. */\n dispatchConcurrency: number;\n /** Maximum recursion depth for `pi.dispatch`. The child PTC run spawned by\n * the (depth+1)-th dispatch is allowed only when childDepth <= maxDispatchDepth.\n * Default 3. Aligns with dsh's `SubagentCapabilities.depthLimit` and codex's\n * `agent_max_depth`. ADR-0016 Recursive dispatch section. */\n maxDispatchDepth: number;\n /** Items accepted by a single `parallel()` / `pipeline()` call (R1 §1, workflow-side; pinned\n * here because it guards a helper call rather than an agent budget). */\n maxItemsPerCall: number;\n /** Cooperative-cancel grace before the worker is terminated outright. */\n graceMs: number;\n /** V8 old-generation cap handed to `new Worker({ resourceLimits })` (F2). */\n maxOldGenerationSizeMb: number;\n /** V8 young-generation cap handed to `new Worker({ resourceLimits })` (F2). */\n maxYoungGenerationSizeMb: number;\n}\n\nexport const DEFAULT_CONFIG: Readonly<PtcConfig> = Object.freeze({\n timeoutMs: 120_000,\n maxTimeoutMs: 600_000,\n maxOutputBytes: 67_108_864,\n maxMessageBytes: 134_217_728,\n maxPendingCalls: 128,\n maxParallelSubCalls: 10,\n dispatchConcurrency: 8,\n maxDispatchDepth: 3,\n maxItemsPerCall: 4_096,\n graceMs: 3_000,\n maxOldGenerationSizeMb: 512,\n maxYoungGenerationSizeMb: 64,\n});\n\n/**\n * F1 — the only environment variables a PTC worker may inherit.\n *\n * These are what a shell needs to resolve binaries on each platform; everything\n * else the host has (tokens, API keys, proxies, home paths) stays out of the worker.\n */\nexport const WORKER_ENV_ALLOW_LIST: readonly string[] = Object.freeze([\n \"PATH\",\n \"PATHEXT\",\n \"SYSTEMROOT\",\n \"WINDIR\",\n \"TEMP\",\n \"TMP\",\n]);\n\n/**\n * Build the per-run environment snapshot: the allow-list entries that actually exist.\n *\n * Unset or empty entries are dropped rather than passed through as empty strings, so\n * `Object.keys(workerEnv)` is exactly what the worker sees in `process.env`.\n */\nexport function createWorkerEnv(source: NodeJS.ProcessEnv = process.env): Record<string, string> {\n const env: Record<string, string> = {};\n for (const name of WORKER_ENV_ALLOW_LIST) {\n const value = source[name];\n if (typeof value === \"string\" && value.length > 0) env[name] = value;\n }\n return env;\n}\n\n/**\n * Merge per-run overrides onto `DEFAULT_CONFIG`.\n *\n * Overrides are validated rather than coerced: a negative or non-numeric limit is a\n * caller bug, and silently substituting a default would hide it.\n */\nexport function resolveConfig(overrides: Partial<PtcConfig> = {}): PtcConfig {\n const resolved: PtcConfig = { ...DEFAULT_CONFIG };\n for (const key of Object.keys(DEFAULT_CONFIG) as Array<keyof PtcConfig>) {\n const override = overrides[key];\n if (override === undefined) continue;\n if (typeof override !== \"number\" || !Number.isFinite(override) || override <= 0) {\n throw new TypeError(`invalid PTC config override for ${key}: ${String(override)}`);\n }\n resolved[key] = override;\n }\n return resolved;\n}\n\n/**\n * Resolve the effective deadline for one run.\n *\n * DSH semantics (R1 §3): `0` does not disable the deadline, it falls back to the\n * default; anything larger is clamped to `maxTimeoutMs`.\n */\nexport function effectiveTimeoutMs(\n requested: number | undefined,\n config: PtcConfig = DEFAULT_CONFIG,\n): number {\n if (requested === undefined || !Number.isFinite(requested) || requested <= 0)\n return config.timeoutMs;\n return Math.min(requested, config.maxTimeoutMs);\n}\n","/**\n * The PTC wire protocol: every frame that crosses the host↔worker boundary, plus a\n * runtime guard for every arm of both closed unions.\n *\n * Shape rules:\n * - Frame kinds, log levels and error kinds live here once; the worker receives the\n * same tables as plain data at spawn (`workerProtocolSpec`), so the two ends cannot\n * drift on names.\n * - Guards validate *shape only*. They never walk payloads: a `log` frame may carry a\n * 64 MiB string and a `call` frame arbitrary tool arguments, so byte accounting is\n * the dispatcher's job (see `serializedBytes` usage there).\n * - Error kinds are R1 §3's taxonomy minus `sandbox-unavailable` (ADR-0007 ships no OS\n * sandbox) and minus any `agent`-related kind (there is no `agent()` helper).\n */\nimport { MessagePort } from \"node:worker_threads\";\nimport type { PtcSurface } from \"./limits.ts\";\n\n/** host → worker */\nconst HOST_FRAME_KIND_VALUES = {\n connect: \"connect\",\n init: \"init\",\n callResult: \"call-result\",\n cancel: \"cancel\",\n} as const;\nexport const HOST_FRAME_KIND: typeof HOST_FRAME_KIND_VALUES = Object.freeze(HOST_FRAME_KIND_VALUES);\n\n/** worker → host */\nconst WORKER_FRAME_KIND_VALUES = {\n ready: \"ready\",\n call: \"call\",\n log: \"log\",\n narration: \"narration\",\n phase: \"phase\",\n result: \"result\",\n error: \"error\",\n} as const;\nexport const WORKER_FRAME_KIND: typeof WORKER_FRAME_KIND_VALUES =\n Object.freeze(WORKER_FRAME_KIND_VALUES);\n\nconst PTC_LOG_LEVEL_VALUES = {\n log: \"log\",\n info: \"info\",\n warn: \"warn\",\n error: \"error\",\n debug: \"debug\",\n} as const;\nexport const PTC_LOG_LEVEL: typeof PTC_LOG_LEVEL_VALUES = Object.freeze(PTC_LOG_LEVEL_VALUES);\nexport type PtcLogLevel = (typeof PTC_LOG_LEVEL)[keyof typeof PTC_LOG_LEVEL];\n\nconst PTC_ERROR_KIND_VALUES = {\n /** Program parse error or thrown exception (includes `ReferenceError` from a helper that does not exist on this surface). */\n exception: \"exception\",\n /** Elapsed deadline expiry. */\n timeout: \"timeout\",\n /** Caller cancellation. */\n abort: \"abort\",\n /** Malformed or excessive control traffic. */\n protocol: \"protocol\",\n /** Early worker exit or a worker-level crash. */\n workerExit: \"worker-exit\",\n /** Completion value could not be materialized as lossless JSON. */\n invalidOutput: \"invalid-output\",\n /** Oversized outer result; collected logs are retained. */\n outputLimit: \"output-limit\",\n} as const;\nexport const PTC_ERROR_KIND: typeof PTC_ERROR_KIND_VALUES = Object.freeze(PTC_ERROR_KIND_VALUES);\nexport type PtcErrorKind = (typeof PTC_ERROR_KIND)[keyof typeof PTC_ERROR_KIND];\n\nexport type PtcCancelReason = \"timeout\" | \"abort\";\n\n/** Lossless-JSON payloads, the only values that may cross the boundary as data. */\nexport interface PtcJsonObject {\n [key: string]: PtcJsonValue;\n}\nexport type PtcJsonValue = string | number | boolean | null | PtcJsonValue[] | PtcJsonObject;\n\nexport interface PtcErrorShape {\n kind: PtcErrorKind;\n message: string;\n stack?: string;\n}\n\n/* -------------------------------------------------------------------------- */\n/* host → worker */\n/* -------------------------------------------------------------------------- */\n\n/**\n * Handshake frame sent over `parentPort` right after spawn.\n *\n * The port must appear *in the payload*, not only in the transfer list: Node silently\n * drops a transfer-list-only entry (the worker receives the message without any port\n * and would wait forever), and it does not populate `msg.ports` for `worker_threads`\n * messages the way the browser `MessageEvent.ports` does. Both were verified against\n * Node v24.18.0 while writing this module.\n */\nexport interface PtcConnectFrame {\n kind: \"connect\";\n port: MessagePort;\n}\n\n/** First frame on the control port: installs the surface and starts the program. */\nexport interface PtcInitFrame {\n kind: \"init\";\n runId: string;\n surface: PtcSurface;\n code: string;\n /** Binding names the worker may expose as `tools.<name>`; the keys of the host table. */\n bindings: readonly string[];\n /**\n * Names the host could have bound (its built-in factory set). Candidates absent from\n * `bindings` get a throwing stub in the worker, so a disabled tool fails with an\n * actionable error instead of `tools.x is not a function` (T7, #21).\n */\n bindingCandidates: readonly string[];\n maxItemsPerCall: number;\n /**\n * In-flight call ceiling the worker must maintain itself (ADR-0004).\n *\n * The worker admits calls before posting them — a wide `Promise.all` / `parallel()`\n * burst then queues inside the worker instead of arriving at the host all at once,\n * which is what makes \"maximum simultaneous host binding calls\" a real invariant\n * rather than an arrival-counted tripwire. The host keeps its own counting as a\n * defense-in-depth backstop for a worker that ignores this field.\n */\n maxPendingCalls: number;\n /** Workflow surface only: value bound to the `args` global. */\n args?: unknown;\n}\n\nexport interface PtcCallResultOkFrame {\n kind: \"call-result\";\n callId: number;\n tool: string;\n ok: true;\n value: unknown;\n}\n\nexport interface PtcCallResultErrorFrame {\n kind: \"call-result\";\n callId: number;\n tool: string;\n ok: false;\n message: string;\n}\n\nexport type PtcCallResultFrame = PtcCallResultOkFrame | PtcCallResultErrorFrame;\n\nexport interface PtcCancelFrame {\n kind: \"cancel\";\n reason: PtcCancelReason;\n}\n\nexport type PtcHostFrame = PtcConnectFrame | PtcInitFrame | PtcCallResultFrame | PtcCancelFrame;\n\n/* -------------------------------------------------------------------------- */\n/* worker → host */\n/* -------------------------------------------------------------------------- */\n\n/** Control port is live and the surface may be installed. */\nexport interface PtcReadyFrame {\n kind: \"ready\";\n}\n\n/** `tools.<tool>(args)`: the worker is waiting for a `call-result` with the same id. */\nexport interface PtcCallFrame {\n kind: \"call\";\n callId: number;\n tool: string;\n args: unknown;\n}\n\nexport interface PtcLogFrame {\n kind: \"log\";\n level: PtcLogLevel;\n text: string;\n}\n\n/** Workflow `log(message)`: narration for the UI, not console output. */\nexport interface PtcNarrationFrame {\n kind: \"narration\";\n message: string;\n}\n\n/** Workflow `phase(title)`. */\nexport interface PtcPhaseFrame {\n kind: \"phase\";\n title: string;\n}\n\n/** Program completed. `value` is absent when the program returned nothing. */\nexport interface PtcResultFrame {\n kind: \"result\";\n value?: PtcJsonValue;\n}\n\nexport interface PtcErrorFrame {\n kind: \"error\";\n error: PtcErrorShape;\n}\n\nexport type PtcWorkerFrame =\n | PtcReadyFrame\n | PtcCallFrame\n | PtcLogFrame\n | PtcNarrationFrame\n | PtcPhaseFrame\n | PtcResultFrame\n | PtcErrorFrame;\n\n/* -------------------------------------------------------------------------- */\n/* guards */\n/* -------------------------------------------------------------------------- */\n\nfunction isRecord(value: unknown): value is Record<string, unknown> {\n return typeof value === \"object\" && value !== null && !Array.isArray(value);\n}\n\nexport function isPtcLogLevel(value: unknown): value is PtcLogLevel {\n return typeof value === \"string\" && Object.values(PTC_LOG_LEVEL).some((level) => level === value);\n}\n\nexport function isPtcErrorKind(value: unknown): value is PtcErrorKind {\n return typeof value === \"string\" && Object.values(PTC_ERROR_KIND).some((kind) => kind === value);\n}\n\nexport function isPtcCancelReason(value: unknown): value is PtcCancelReason {\n return value === \"timeout\" || value === \"abort\";\n}\n\nexport function isPtcConnectFrame(value: unknown): value is PtcConnectFrame {\n return (\n isRecord(value) && value.kind === HOST_FRAME_KIND.connect && value.port instanceof MessagePort\n );\n}\n\nexport function isPtcInitFrame(value: unknown): value is PtcInitFrame {\n return (\n isRecord(value) &&\n value.kind === HOST_FRAME_KIND.init &&\n typeof value.runId === \"string\" &&\n (value.surface === \"run_code\" || value.surface === \"workflow\") &&\n typeof value.code === \"string\" &&\n Array.isArray(value.bindings) &&\n value.bindings.every((name) => typeof name === \"string\") &&\n Array.isArray(value.bindingCandidates) &&\n value.bindingCandidates.every((name) => typeof name === \"string\") &&\n typeof value.maxItemsPerCall === \"number\" &&\n Number.isFinite(value.maxItemsPerCall) &&\n typeof value.maxPendingCalls === \"number\" &&\n Number.isFinite(value.maxPendingCalls)\n );\n}\n\nexport function isPtcCallResultFrame(value: unknown): value is PtcCallResultFrame {\n if (!isRecord(value) || value.kind !== HOST_FRAME_KIND.callResult) return false;\n if (!Number.isInteger(value.callId) || typeof value.tool !== \"string\") return false;\n if (value.ok === true) return \"value\" in value;\n return value.ok === false && typeof value.message === \"string\";\n}\n\nexport function isPtcCancelFrame(value: unknown): value is PtcCancelFrame {\n return (\n isRecord(value) && value.kind === HOST_FRAME_KIND.cancel && isPtcCancelReason(value.reason)\n );\n}\n\nexport function isPtcHostFrame(value: unknown): value is PtcHostFrame {\n return (\n isPtcConnectFrame(value) ||\n isPtcInitFrame(value) ||\n isPtcCallResultFrame(value) ||\n isPtcCancelFrame(value)\n );\n}\n\nexport function isPtcReadyFrame(value: unknown): value is PtcReadyFrame {\n return isRecord(value) && value.kind === WORKER_FRAME_KIND.ready;\n}\n\nexport function isPtcCallFrame(value: unknown): value is PtcCallFrame {\n return (\n isRecord(value) &&\n value.kind === WORKER_FRAME_KIND.call &&\n Number.isInteger(value.callId) &&\n typeof value.tool === \"string\" &&\n value.tool.length > 0 &&\n \"args\" in value\n );\n}\n\nexport function isPtcLogFrame(value: unknown): value is PtcLogFrame {\n return (\n isRecord(value) &&\n value.kind === WORKER_FRAME_KIND.log &&\n isPtcLogLevel(value.level) &&\n typeof value.text === \"string\"\n );\n}\n\nexport function isPtcNarrationFrame(value: unknown): value is PtcNarrationFrame {\n return (\n isRecord(value) &&\n value.kind === WORKER_FRAME_KIND.narration &&\n typeof value.message === \"string\"\n );\n}\n\nexport function isPtcPhaseFrame(value: unknown): value is PtcPhaseFrame {\n return (\n isRecord(value) && value.kind === WORKER_FRAME_KIND.phase && typeof value.title === \"string\"\n );\n}\n\nexport function isPtcResultFrame(value: unknown): value is PtcResultFrame {\n return isRecord(value) && value.kind === WORKER_FRAME_KIND.result;\n}\n\nexport function isPtcErrorFrame(value: unknown): value is PtcErrorFrame {\n if (!isRecord(value) || value.kind !== WORKER_FRAME_KIND.error) return false;\n const error = value.error;\n return (\n isRecord(error) &&\n isPtcErrorKind(error.kind) &&\n typeof error.message === \"string\" &&\n (error.stack === undefined || typeof error.stack === \"string\")\n );\n}\n\nexport function isPtcWorkerFrame(value: unknown): value is PtcWorkerFrame {\n return (\n isPtcReadyFrame(value) ||\n isPtcCallFrame(value) ||\n isPtcLogFrame(value) ||\n isPtcNarrationFrame(value) ||\n isPtcPhaseFrame(value) ||\n isPtcResultFrame(value) ||\n isPtcErrorFrame(value)\n );\n}\n\n/* -------------------------------------------------------------------------- */\n/* worker-side spec */\n/* -------------------------------------------------------------------------- */\n\n/**\n * The protocol tables as plain data, injected into the worker at spawn.\n *\n * The worker bootstrap is self-contained (see `worker-main.ts`), so it cannot import\n * these constants — it receives them instead, which is what keeps both ends on the\n * same strings.\n */\nexport interface WorkerProtocolSpec {\n hostFrame: typeof HOST_FRAME_KIND;\n workerFrame: typeof WORKER_FRAME_KIND;\n logLevel: typeof PTC_LOG_LEVEL;\n errorKind: typeof PTC_ERROR_KIND;\n}\n\nexport function workerProtocolSpec(): WorkerProtocolSpec {\n return {\n hostFrame: HOST_FRAME_KIND,\n workerFrame: WORKER_FRAME_KIND,\n logLevel: PTC_LOG_LEVEL,\n errorKind: PTC_ERROR_KIND,\n };\n}\n","/**\n * Worker-side PTC runtime: installs one surface, runs one program, routes `tools.*`\n * calls back to the host.\n *\n * ⚠️ SELF-CONTAINMENT RULE — read before editing.\n *\n * `workerMain` is serialized with `Function.prototype.toString()` and re-evaluated\n * inside a freshly spawned worker (see `worker-source.ts`). Everything it touches at\n * runtime must therefore be one of:\n *\n * 1. a worker-realm global (`process`, `globalThis`, `console`, `Map`, `Promise`, …),\n * 2. a declaration nested inside this function,\n * 3. a `deps` property injected at spawn.\n *\n * It must never reference an imported *value* or any other module-scope binding: those\n * names are resolved in the host bundle, not inside the worker, and the composed source\n * would fail with a `ReferenceError`. TypeScript types from this repo are fine — they\n * are erased from both the dev (type-stripping) and bundled (`rolldown`) forms of the\n * function, which is what makes `toString()` output valid plain JavaScript in both.\n */\nimport type {\n PtcCancelReason,\n PtcErrorKind,\n PtcJsonObject,\n PtcJsonValue,\n PtcLogLevel,\n WorkerProtocolSpec,\n} from \"./protocol.ts\";\n\n/** The subset of `MessagePort`/`parentPort` the worker uses. */\nexport interface WorkerMainPort {\n on(event: \"message\", listener: (value: unknown) => void): unknown;\n on(event: \"close\", listener: () => void): unknown;\n postMessage(value: unknown): void;\n}\n\nexport interface WorkerMainDeps {\n parentPort: WorkerMainPort;\n /** The spawning host's `workerData`: `{ runId, env }` (F3). */\n workerData: unknown;\n /** `node:util`'s `inspect`, used to render non-string console arguments. */\n inspect: (value: unknown, options?: Record<string, unknown>) => string;\n /** `node:module`'s `stripTypeScriptTypes`: the program is TypeScript, run type-stripped. */\n stripTypes: (code: string, options: { mode: \"strip\" }) => string;\n protocol: WorkerProtocolSpec;\n}\n\nexport function workerMain(deps: WorkerMainDeps): void {\n const hostFrame = deps.protocol.hostFrame;\n const workerFrame = deps.protocol.workerFrame;\n const logLevel = deps.protocol.logLevel;\n const errorKind = deps.protocol.errorKind;\n /** Wrapper name used to compile a program body (`return …` is legal inside it). */\n const programName = \"__ptcProgram\";\n\n let control: WorkerMainPort | undefined;\n let started = false;\n let cancelled: PtcCancelReason | undefined;\n let nextCallId = 1;\n /** Call frames posted and not yet answered; capped by `maxPendingCalls` (ADR-0004). */\n let inFlightCalls = 0;\n let maxPendingCalls = Number.POSITIVE_INFINITY;\n const admissionWaiters: Array<{ resolve: () => void; reject: (error: unknown) => void }> = [];\n const pending = new Map<\n number,\n { resolve: (value: unknown) => void; reject: (error: unknown) => void }\n >();\n\n const globals = (): Record<string, unknown> => globalThis as unknown as Record<string, unknown>;\n\n class ToolCallError extends Error {\n toolName: string;\n constructor(toolName: string, message: string) {\n super(message);\n this.name = \"ToolCallError\";\n this.toolName = toolName;\n }\n }\n\n const messageOf = (error: unknown): string =>\n error instanceof Error ? error.message : String(error);\n\n /**\n * Trim a stack down to what a reader can act on.\n *\n * Frames inside the worker bootstrap point at the `data:` URL the worker was spawned\n * from, which is tens of kilobytes of encoded source; dropping those lines and capping\n * the depth keeps the model-facing stack (and the output budget) sane.\n */\n const stackOf = (error: unknown): string | undefined => {\n if (!(error instanceof Error) || typeof error.stack !== \"string\") return undefined;\n const lines = error.stack.split(\"\\n\");\n const kept = [lines[0] ?? \"\"];\n for (const line of lines.slice(1)) {\n if (line.includes(\"data:text/javascript\")) continue;\n kept.push(line);\n if (kept.length >= 6) break;\n }\n return kept.join(\"\\n\");\n };\n const describeValue = (value: unknown): string => {\n if (value === null) return \"null\";\n if (Array.isArray(value)) return \"an array\";\n return `a ${typeof value}`;\n };\n\n const post = (frame: unknown): boolean => {\n if (!control) return false;\n try {\n control.postMessage(frame);\n return true;\n } catch {\n // Port closed (run already settled) — nothing useful to do with the frame.\n return false;\n }\n };\n\n const postLog = (level: PtcLogLevel, text: string): void => {\n post({ kind: workerFrame.log, level, text });\n };\n\n const postError = (kind: PtcErrorKind, message: string, stack?: string): void => {\n post(\n stack === undefined\n ? { kind: workerFrame.error, error: { kind, message } }\n : { kind: workerFrame.error, error: { kind, message, stack } },\n );\n };\n\n const abortError = (reason: PtcCancelReason): Error => {\n const error = new Error(reason === \"timeout\" ? \"PTC run timed out\" : \"PTC run was cancelled\");\n error.name = \"AbortError\";\n return error;\n };\n\n const closedError = (): Error => {\n const error = new Error(\"the PTC control channel closed before the call completed\");\n error.name = \"AbortError\";\n return error;\n };\n\n /* ------------------------------------------------------------------ */\n /* call admission */\n /* ------------------------------------------------------------------ */\n\n /**\n * Admission control for host binding calls.\n *\n * \"Maximum simultaneous host binding calls\" is the worker's invariant to keep: a call\n * frame is only posted once fewer than `maxPendingCalls` are outstanding, so a wide\n * `Promise.all` / `parallel()` burst queues here rather than arriving at the host in one\n * tick. The host still counts arrivals as a backstop, but it must never see this budget\n * exceeded by a well-behaved program.\n *\n * Waiting for a slot is cancel-aware: `cancel` and a closed control port reject waiters\n * immediately, so an oversized burst unwinds at once instead of waiting out the grace\n * window before the host terminates the worker.\n */\n const acquireCallSlot = async (): Promise<void> => {\n if (inFlightCalls < maxPendingCalls) {\n inFlightCalls += 1;\n return;\n }\n await new Promise<void>((resolve, reject) => {\n admissionWaiters.push({ resolve, reject });\n });\n };\n\n const releaseCallSlot = (): void => {\n const next = admissionWaiters.shift();\n if (next) {\n // Hand the slot over: `inFlightCalls` already accounts for it, so it stays put.\n next.resolve();\n return;\n }\n inFlightCalls -= 1;\n };\n\n const rejectAdmissionWaiters = (error: unknown): void => {\n while (admissionWaiters.length > 0) {\n const waiter = admissionWaiters.shift();\n if (waiter) waiter.reject(error);\n }\n };\n\n /** Settle every outstanding call and every admission wait, so nothing can hang. */\n const flushPendingCalls = (error: Error): void => {\n rejectAdmissionWaiters(error);\n for (const [callId, entry] of pending) {\n pending.delete(callId);\n entry.reject(error);\n }\n };\n\n /**\n * F3 — reinstall the frozen per-run environment carried in `workerData`.\n *\n * The spawn already passes the same snapshot as the worker's `env` option; rebuilding\n * `process.env` from `workerData` here is what makes the snapshot the authority: the\n * program's environment is the frozen record, never whatever `process.env` happened\n * to hold when a binding call crossed the wire.\n */\n const installFrozenEnv = (): void => {\n const data = deps.workerData;\n if (typeof data !== \"object\" || data === null) return;\n const snapshot = (data as { env?: unknown }).env;\n if (typeof snapshot !== \"object\" || snapshot === null || Array.isArray(snapshot)) return;\n const env = snapshot as Record<string, unknown>;\n for (const key of Object.keys(process.env)) {\n if (!(key in env)) delete process.env[key];\n }\n for (const key of Object.keys(env)) {\n const value = env[key];\n if (typeof value === \"string\") process.env[key] = value;\n }\n };\n\n /**\n * Lossless-JSON materialization of the program's return value (R1 §3 `invalid-output`).\n *\n * Plain JSON only: class instances, `Date`/`Map`/`Set`, functions, symbols, bigints,\n * non-finite numbers and cycles are rejected with a path-qualified reason instead of\n * being silently lossy through the transport. `undefined` follows `JSON.stringify`:\n * object properties are dropped, array slots become `null`.\n */\n const toJsonValue = (\n value: unknown,\n ): { ok: true; value: PtcJsonValue | undefined } | { ok: false; reason: string } => {\n const seen = new Set<object>();\n\n const walk = (\n candidate: unknown,\n path: string,\n ): { ok: true; value: PtcJsonValue | undefined } | { ok: false; reason: string } => {\n if (candidate === null) return { ok: true, value: null };\n if (typeof candidate === \"string\" || typeof candidate === \"boolean\")\n return { ok: true, value: candidate };\n if (typeof candidate === \"undefined\") return { ok: true, value: undefined };\n if (typeof candidate === \"number\") {\n if (!Number.isFinite(candidate)) {\n return {\n ok: false,\n reason: `${path} is ${String(candidate)}, which is not representable as JSON`,\n };\n }\n return { ok: true, value: candidate };\n }\n if (typeof candidate !== \"object\") {\n return {\n ok: false,\n reason: `${path} is ${describeValue(candidate)}; results must be lossless JSON`,\n };\n }\n\n if (Array.isArray(candidate)) {\n if (seen.has(candidate)) return { ok: false, reason: `${path} is a circular reference` };\n seen.add(candidate);\n const items: PtcJsonValue[] = [];\n for (let index = 0; index < candidate.length; index += 1) {\n const item = walk(candidate[index], `${path}[${index}]`);\n if (!item.ok) return item;\n items.push(item.value === undefined ? null : item.value);\n }\n seen.delete(candidate);\n return { ok: true, value: items };\n }\n\n const prototype = Object.getPrototypeOf(candidate);\n if (prototype !== Object.prototype && prototype !== null) {\n const ctor = (candidate as { constructor?: { name?: string } }).constructor;\n const name =\n ctor && typeof ctor.name === \"string\" && ctor.name.length > 0 ? ctor.name : \"an object\";\n return { ok: false, reason: `${path} is a ${name}; results must be plain JSON objects` };\n }\n if (seen.has(candidate)) return { ok: false, reason: `${path} is a circular reference` };\n seen.add(candidate);\n const entries: PtcJsonObject = {};\n for (const key of Object.keys(candidate)) {\n const item = walk((candidate as Record<string, unknown>)[key], `${path}.${key}`);\n if (!item.ok) return item;\n if (item.value !== undefined) entries[key] = item.value;\n }\n seen.delete(candidate);\n return { ok: true, value: entries };\n };\n\n return walk(value, \"result\");\n };\n\n const formatArgs = (args: readonly unknown[]): string =>\n args\n .map((arg) =>\n typeof arg === \"string\"\n ? arg\n : deps.inspect(arg, { depth: 4, breakLength: Infinity, colors: false }),\n )\n .join(\" \");\n\n /**\n * Capture `console` instead of letting worker stdio reach the host terminal.\n *\n * Only the printing methods exist on this surface; everything routes to `log` frames\n * so the model (not the terminal) sees the program's output.\n */\n const installConsole = (): void => {\n const write = (level: PtcLogLevel) => {\n return (...args: unknown[]): void => {\n postLog(level, formatArgs(args));\n };\n };\n globals().console = {\n log: write(logLevel.log),\n info: write(logLevel.info),\n warn: write(logLevel.warn),\n error: write(logLevel.error),\n debug: write(logLevel.debug),\n };\n };\n\n /**\n * Route `process.emitWarning` output through `log` frames.\n *\n * Node's default warning printer writes to stderr, which is piped to the host terminal.\n * `ExperimentalWarning`s are dropped rather than forwarded: the PTC runtime itself uses\n * experimental Node APIs (type stripping, for one) and those warnings are about the\n * harness, not about the program — forwarding them would put Node-internals noise into\n * every run's logs.\n */\n const installWarningCapture = (): void => {\n if (typeof process.removeAllListeners !== \"function\" || typeof process.on !== \"function\")\n return;\n process.removeAllListeners(\"warning\");\n process.on(\"warning\", (warning: unknown) => {\n const record =\n typeof warning === \"object\" && warning !== null\n ? (warning as Record<string, unknown>)\n : undefined;\n const name = record && typeof record.name === \"string\" ? record.name : \"Warning\";\n if (name === \"ExperimentalWarning\") return;\n const detail =\n record && typeof record.message === \"string\" ? record.message : String(warning);\n postLog(logLevel.warn, `${name}: ${detail}`);\n });\n };\n\n /**\n * Compile a program body into a callable.\n *\n * The body is wrapped in an async function so `return` and `await` work at the top\n * level, then run through `stripTypeScriptTypes` so the same body may carry type\n * annotations (DSH's \"type annotations are advisory, the code runs type-stripped\").\n * JavaScript the TypeScript parser rejects falls back to the unstripped source; a\n * genuine syntax error then surfaces from the `Function` constructor instead.\n */\n const compileProgram = (code: string): (() => unknown) => {\n const wrapped = `async function ${programName}() {\\n${code}\\n}`;\n let source = wrapped;\n try {\n source = deps.stripTypes(wrapped, { mode: \"strip\" });\n } catch {\n source = wrapped;\n }\n // oxlint-disable-next-line typescript/no-implied-eval -- compiling the submitted program is this worker's job; it never compiles host code.\n return new Function(`${source}\\nreturn ${programName}();`) as () => unknown;\n };\n\n const runProgram = async (code: string): Promise<void> => {\n let program: () => unknown;\n try {\n program = compileProgram(code);\n } catch (error) {\n postError(\n errorKind.exception,\n `program failed to compile: ${messageOf(error)}`,\n stackOf(error),\n );\n return;\n }\n try {\n const value = await program();\n const normalized = toJsonValue(value);\n if (!normalized.ok) {\n postError(errorKind.invalidOutput, normalized.reason);\n return;\n }\n post(\n normalized.value === undefined\n ? { kind: workerFrame.result }\n : { kind: workerFrame.result, value: normalized.value },\n );\n } catch (error) {\n postError(cancelled ?? errorKind.exception, messageOf(error), stackOf(error));\n }\n };\n\n /**\n * `tools.<name>(args)` — acquire an admission slot, post a call frame, and await the\n * matching `call-result`.\n *\n * The slot is released on every exit path (settled, admission refused by `post`, cancel),\n * which is what keeps a burst draining instead of deadlocking.\n */\n const makeBinding = (name: string) => {\n return async (args: unknown): Promise<unknown> => {\n if (cancelled) throw abortError(cancelled);\n await acquireCallSlot();\n try {\n if (cancelled) throw abortError(cancelled);\n const callId = nextCallId;\n nextCallId += 1;\n const promise = new Promise<unknown>((resolve, reject) => {\n pending.set(callId, { resolve, reject });\n });\n const sent = post({ kind: workerFrame.call, callId, tool: name, args });\n if (!sent) {\n pending.delete(callId);\n throw new ToolCallError(\n name,\n `${name}() could not be called: the arguments are not transferable or the run has ended`,\n );\n }\n return await promise;\n } finally {\n releaseCallSlot();\n }\n };\n };\n\n const settleCall = (frame: Record<string, unknown>): void => {\n const callId = frame.callId;\n if (typeof callId !== \"number\") return;\n const entry = pending.get(callId);\n if (!entry) return;\n pending.delete(callId);\n if (frame.ok === true) {\n entry.resolve(frame.value);\n return;\n }\n entry.reject(\n new ToolCallError(\n typeof frame.tool === \"string\" ? frame.tool : \"unknown\",\n typeof frame.message === \"string\" ? frame.message : \"binding call failed\",\n ),\n );\n };\n\n const cancelPending = (reason: PtcCancelReason): void => {\n cancelled = reason;\n flushPendingCalls(abortError(reason));\n };\n\n /* ------------------------------------------------------------------ */\n /* workflow helpers (ptc_workflow surface only) */\n /* ------------------------------------------------------------------ */\n\n const assertItemCount = (helper: string, count: number, maxItemsPerCall: number): void => {\n if (count > maxItemsPerCall) {\n throw new RangeError(\n `${helper}() received ${count} items, exceeding maxItemsPerCall (${maxItemsPerCall})`,\n );\n }\n };\n\n const installWorkflowHelpers = (args: unknown, maxItemsPerCall: number): void => {\n const target = globals();\n target.args = args === undefined ? null : args;\n\n target.log = (message: unknown): void => {\n if (typeof message !== \"string\")\n throw new TypeError(`log(message) expects a string, received ${describeValue(message)}`);\n post({ kind: workerFrame.narration, message });\n };\n\n target.phase = (title: unknown): void => {\n if (typeof title !== \"string\")\n throw new TypeError(`phase(title) expects a string, received ${describeValue(title)}`);\n post({ kind: workerFrame.phase, title });\n };\n\n // parallel(thunks): all thunks start together; a rejected item becomes `null`\n // instead of failing the call (R1 §4).\n target.parallel = async (thunks: unknown): Promise<unknown[]> => {\n if (!Array.isArray(thunks))\n throw new TypeError(\n `parallel(thunks) expects an array of functions, received ${describeValue(thunks)}`,\n );\n assertItemCount(\"parallel\", thunks.length, maxItemsPerCall);\n for (let index = 0; index < thunks.length; index += 1) {\n if (typeof thunks[index] !== \"function\") {\n throw new TypeError(\n `parallel(thunks) expects functions; item ${index} is ${describeValue(thunks[index])}`,\n );\n }\n }\n return await Promise.all(\n (thunks as Array<() => unknown>).map(async (thunk) => {\n try {\n return await thunk();\n } catch {\n return null;\n }\n }),\n );\n };\n\n // pipeline(items, ...stages): stages run per item with no cross-stage barrier; a\n // failing item becomes `null` while its siblings continue (R1 §4).\n target.pipeline = async (items: unknown, ...stages: unknown[]): Promise<unknown[]> => {\n if (!Array.isArray(items))\n throw new TypeError(\n `pipeline(items, ...stages) expects an array of items, received ${describeValue(items)}`,\n );\n assertItemCount(\"pipeline\", items.length, maxItemsPerCall);\n if (stages.length === 0)\n throw new TypeError(\"pipeline(items, ...stages) requires at least one stage function\");\n for (let index = 0; index < stages.length; index += 1) {\n if (typeof stages[index] !== \"function\") {\n throw new TypeError(\n `pipeline(items, ...stages) expects stage functions; stage ${index} is ${describeValue(stages[index])}`,\n );\n }\n }\n const stageFunctions = stages as Array<\n (previous: unknown, item: unknown, index: number) => unknown\n >;\n return await Promise.all(\n (items as unknown[]).map(async (item, index) => {\n try {\n let current: unknown = item;\n for (const stage of stageFunctions) {\n current = await stage(current, item, index);\n }\n return current;\n } catch {\n return null;\n }\n }),\n );\n };\n };\n\n /* ------------------------------------------------------------------ */\n /* frame handling */\n /* ------------------------------------------------------------------ */\n\n const startRun = (frame: Record<string, unknown>): void => {\n installFrozenEnv();\n installConsole();\n\n // ADR-0004's ceiling, handed over per run. The host's init guard requires the field;\n // the fallback only exists so a hand-built frame cannot silently wedge the surface\n // (the host's arrival-counted backstop still catches a flood in that case).\n maxPendingCalls =\n typeof frame.maxPendingCalls === \"number\" &&\n Number.isFinite(frame.maxPendingCalls) &&\n frame.maxPendingCalls > 0\n ? frame.maxPendingCalls\n : Number.POSITIVE_INFINITY;\n\n const tools: Record<string, (args: unknown) => Promise<unknown>> = {};\n const bindingNames = Array.isArray(frame.bindings)\n ? frame.bindings.filter((name): name is string => typeof name === \"string\")\n : [];\n for (const name of bindingNames) tools[name] = makeBinding(name);\n // Known-but-disabled tools (T7, #21): stub each with an actionable error instead of\n // letting the program hit `tools.write is not a function`.\n const available = bindingNames.join(\", \") || \"(none)\";\n const candidates = Array.isArray(frame.bindingCandidates) ? frame.bindingCandidates : [];\n for (const name of candidates) {\n if (typeof name !== \"string\" || tools[name]) continue;\n tools[name] = async () => {\n throw new ToolCallError(\n name,\n `no binding named \"${name}\" in this run; available bindings: ${available}`,\n );\n };\n }\n globals().tools = tools;\n\n if (frame.surface === \"workflow\") {\n const maxItemsPerCall = typeof frame.maxItemsPerCall === \"number\" ? frame.maxItemsPerCall : 0;\n installWorkflowHelpers(frame.args, maxItemsPerCall);\n }\n\n void runProgram(typeof frame.code === \"string\" ? frame.code : \"\");\n };\n\n const handleHostFrame = (raw: unknown): void => {\n if (typeof raw !== \"object\" || raw === null || Array.isArray(raw)) return;\n const frame = raw as Record<string, unknown>;\n if (frame.kind === hostFrame.init) {\n if (started) return;\n started = true;\n startRun(frame);\n return;\n }\n if (frame.kind === hostFrame.callResult) {\n settleCall(frame);\n return;\n }\n if (frame.kind === hostFrame.cancel) {\n cancelPending(frame.reason === \"timeout\" ? \"timeout\" : \"abort\");\n }\n };\n\n // `port.on(\"message\", ...)` hands over the raw value — unlike a browser `MessageEvent`\n // there is no `{ data }` wrapper to unwrap here.\n deps.parentPort.on(\"message\", (value: unknown) => {\n if (typeof value !== \"object\" || value === null) return;\n const frame = value as Record<string, unknown>;\n if (frame.kind !== hostFrame.connect) return;\n const port = frame.port;\n if (\n typeof port !== \"object\" ||\n port === null ||\n typeof (port as WorkerMainPort).on !== \"function\"\n )\n return;\n control = port as WorkerMainPort;\n control.on(\"message\", handleHostFrame);\n // The host is gone (it closed the control port, e.g. after terminating the run): no\n // response can arrive any more, so stop waiting for one.\n control.on(\"close\", () => {\n flushPendingCalls(closedError());\n });\n installWarningCapture();\n post({ kind: workerFrame.ready });\n });\n}\n","/**\n * Worker bootstrap assembly.\n *\n * The worker code is composed at spawn time from three parts:\n *\n * 1. a tiny ES-module preamble that imports the Node builtins the surface needs,\n * 2. the protocol tables and limits as a JSON literal,\n * 3. `workerMain.toString()` — the worker runtime itself (`worker-main.ts`).\n *\n * Two Node behaviours this relies on, both verified against v24.18.0:\n * - `new Worker(<data: URL>)` runs the payload as an ES module (\"the data is interpreted\n * based on MIME type using the ECMAScript module loader\"). The `type: 'module'` option\n * is undocumented for `Worker`, and `eval: true` workers are CommonJS, so a data URL is\n * the documented way to get an ESM worker from an in-memory string.\n * - `Function.prototype.toString()` returns usable source for a function that came\n * through Node's type stripping (dev/tests) and through rolldown (dist), which is what\n * lets the bundle stay a single self-contained `dist/index.js` with no second entry\n * file to publish.\n *\n * Because the composed source is evaluated in a fresh realm, `workerMain` must be\n * self-contained — see the rule at the top of `worker-main.ts`.\n */\nimport type { WorkerProtocolSpec } from \"./protocol.ts\";\nimport { workerMain } from \"./worker-main.ts\";\n\n/** Build the worker module source for one spawn. */\nexport function buildWorkerSource(protocol: WorkerProtocolSpec): string {\n const deps = JSON.stringify({ protocol });\n return [\n `import { parentPort, workerData } from \"node:worker_threads\";`,\n `import { inspect } from \"node:util\";`,\n `import { stripTypeScriptTypes } from \"node:module\";`,\n `const ptcDeps = ${deps};`,\n `(${workerMain.toString()})({ ...ptcDeps, parentPort, workerData, inspect, stripTypes: stripTypeScriptTypes });`,\n ].join(\"\\n\");\n}\n\n/** The worker entry as a `data:` module URL, ready for `new Worker(url, options)`. */\nexport function buildWorkerUrl(protocol: WorkerProtocolSpec): URL {\n return new URL(`data:text/javascript,${encodeURIComponent(buildWorkerSource(protocol))}`);\n}\n","/**\n * Host-side dispatcher: one `runPtcProgram()` call = one worker = one program.\n *\n * Responsibilities:\n * - spawn a hardened worker (F1 env scrub, F2 V8 caps, F3 frozen per-run env in\n * `workerData`, F4 `cwd` carried in the run config and handed to the bindings),\n * - complete the `MessageChannel` handshake and send the `init` frame,\n * - route `tools.*` calls to the binding table — concurrently, with DSH's\n * `maxParallelSubCalls` forwarding cap and `maxPendingCalls` admission control,\n * - collect logs / narration / phases and enforce the joint output budget,\n * - enforce the deadline, honour caller cancellation, and always tear the worker down.\n *\n * Everything the worker can influence is treated as untrusted input: every frame goes\n * through the protocol guards, is size-checked, and can only ever end the run.\n */\nimport { randomUUID } from \"node:crypto\";\nimport { MessageChannel, Worker } from \"node:worker_threads\";\nimport type { MessagePort } from \"node:worker_threads\";\nimport { BUILTIN_BINDING_NAMES, type BindingTable } from \"./bindings.ts\";\nimport { createWorkerEnv, effectiveTimeoutMs, resolveConfig } from \"./limits.ts\";\nimport type { PtcConfig, PtcSurface } from \"./limits.ts\";\nimport {\n HOST_FRAME_KIND,\n isPtcHostFrame,\n isPtcWorkerFrame,\n PTC_ERROR_KIND,\n WORKER_FRAME_KIND,\n workerProtocolSpec,\n} from \"./protocol.ts\";\nimport type {\n PtcCallFrame,\n PtcCallResultErrorFrame,\n PtcCallResultFrame,\n PtcCancelReason,\n PtcErrorKind,\n PtcErrorShape,\n PtcHostFrame,\n PtcJsonValue,\n} from \"./protocol.ts\";\nimport { buildWorkerUrl } from \"./worker-source.ts\";\n\nexport interface RunPtcProgramOptions {\n /** Program body: an async function body (`return`/`await` at the top level). */\n code: string;\n surface: PtcSurface;\n /** Working directory for bindings and the run's recorded cwd (F4). */\n cwd: string;\n /** Bindings the program may call; the table keys become `tools.<name>`. */\n bindings: BindingTable;\n /** Workflow surface only: value bound to the program's `args` global. */\n args?: unknown;\n /** Requested deadline; `0`/absent fall back to `timeoutMs`, then clamped to `maxTimeoutMs`. */\n timeoutMs?: number;\n /** Per-run limit overrides on top of `DEFAULT_CONFIG`. */\n config?: Partial<PtcConfig>;\n /** Cancels the run; the worker gets a cooperative cancel window before termination. */\n signal?: AbortSignal;\n /** Identifier carried to the worker; generated when omitted. */\n runId?: string;\n}\n\n/**\n * One image hoisted out of a successful binding result (DSH parity — see ADR-0014).\n *\n * `data` is base64 exactly as pi's own `read` tool returns it, so the tool layer can forward it as\n * an `ImageContent` block without re-encoding.\n */\nexport interface PtcImage {\n data: string;\n mimeType: string;\n}\n\nexport interface PtcRunOutcome {\n /** `console.*` output in arrival order. */\n logs: string[];\n /** Workflow `log(message)` narration (observers only, never console output). */\n narrations: string[];\n /** Workflow `phase(title)` titles in arrival order. */\n phases: string[];\n /** Completion value; absent when the program returned nothing or the run failed. */\n value?: PtcJsonValue;\n /**\n * Images hoisted out of successful binding results, in call order.\n *\n * Every image a program's tool calls produced, with no cap and no dedupe: how many images a run\n * attaches is the program's business, exactly as it is in DSH. Present only when at least one was\n * hoisted — a failed or cancelled run attaches nothing, because the tool layer throws for it\n * (`codeRunFailedError`) and its image would never reach the model.\n */\n images?: PtcImage[];\n /** Failure details; absent on success. */\n error?: PtcErrorShape;\n}\n\nfunction messageOf(error: unknown): string {\n return error instanceof Error ? error.message : String(error);\n}\n\n/** Serialized byte size of a frame, the unit both the output and message budgets use. */\nfunction serializedBytes(value: unknown): number {\n try {\n return Buffer.byteLength(JSON.stringify(value) ?? \"\", \"utf8\");\n } catch {\n // Cyclic or otherwise unserializable payload: report as unbounded so the caller fails the run.\n return Number.POSITIVE_INFINITY;\n }\n}\n\n/**\n * Run one PTC program in a fresh worker and resolve with its outcome.\n *\n * Never rejects for program/ limit / cancellation failures — those are reported as\n * `outcome.error` so callers have one place to render from. It only throws on caller bugs\n * (invalid config overrides).\n */\nexport async function runPtcProgram(options: RunPtcProgramOptions): Promise<PtcRunOutcome> {\n const config = resolveConfig(options.config);\n const protocol = workerProtocolSpec();\n const env = createWorkerEnv();\n const runId = options.runId ?? randomUUID();\n const timeoutMs = effectiveTimeoutMs(options.timeoutMs, config);\n\n const logs: string[] = [];\n const narrations: string[] = [];\n const phases: string[] = [];\n\n if (options.signal?.aborted) {\n return {\n logs,\n narrations,\n phases,\n error: { kind: PTC_ERROR_KIND.abort, message: \"run cancelled before start\" },\n };\n }\n\n return await new Promise<PtcRunOutcome>((resolve) => {\n const worker = new Worker(buildWorkerUrl(protocol), {\n name: `ptc-${options.surface}`,\n // F1: allow-list only — never the host's full environment.\n env,\n // F3: the same frozen snapshot is the worker's recorded environment.\n workerData: { runId, env },\n // F2: V8 caps (ADR-0005).\n resourceLimits: {\n maxOldGenerationSizeMb: config.maxOldGenerationSizeMb,\n maxYoungGenerationSizeMb: config.maxYoungGenerationSizeMb,\n },\n });\n const channel = new MessageChannel();\n const control: MessagePort = channel.port1;\n const workerPort: MessagePort = channel.port2;\n\n const bindingAbort = new AbortController();\n let settled = false;\n let cancelling: PtcCancelReason | undefined;\n let pendingCalls = 0;\n let outputBytes = 0;\n let activeDispatches = 0;\n const dispatchWaiters: Array<() => void> = [];\n // Per-run depth for `pi.dispatch` (ADR-0016 Recursive section).\n // The parent turn's PTC run is depth 0; a child spawned by `pi.dispatch`\n // is depth 1, and so on. The depth is read by the binding's `execute`;\n // the dispatcher itself does not change it within a single run.\n const runDepth = 0;\n let runTimer: NodeJS.Timeout | undefined;\n let graceTimer: NodeJS.Timeout | undefined;\n\n /* --------------------------- hoisted images (ADR-0014) --------------------------- */\n\n const images: PtcImage[] = [];\n\n /**\n * Hoist the images out of one successful binding result.\n *\n * DSH does this in its scheduler's commit step: a subtool result whose content carries an image\n * block has that content attached to the caller's context after the run, so the picture reaches\n * the model without travelling through the program's lossless-JSON return value (`dsh-tools`:\n * `exec.deferContext(createUserMessage(...))`). The host sees every binding result before it is\n * posted to the worker, so this is the same seam.\n *\n * Everything is hoisted: no count cap, no byte cap, no dedupe. How much context a run spends on\n * images is the program's call, and hiding a cap behind a warning would make this layer a\n * gatekeeper DSH does not have. pi's per-model resize (`inputLimits.images.resize`) still bounds\n * what a single attachment costs at the provider.\n */\n const hoistImages = (value: unknown): void => {\n const content = (value as { content?: unknown } | null)?.content;\n if (!Array.isArray(content)) return;\n for (const block of content) {\n if (block === null || typeof block !== \"object\") continue;\n const candidate = block as { type?: unknown; data?: unknown; mimeType?: unknown };\n if (candidate.type !== \"image\") continue;\n const data = typeof candidate.data === \"string\" ? candidate.data : \"\";\n if (data.length === 0) continue;\n images.push({\n data,\n mimeType:\n typeof candidate.mimeType === \"string\"\n ? candidate.mimeType\n : \"application/octet-stream\",\n });\n }\n };\n\n const cancelMessage = (reason: PtcCancelReason): string =>\n reason === \"timeout\" ? `run timed out after ${timeoutMs} ms` : \"run cancelled\";\n\n const finish = (outcome: PtcRunOutcome): void => {\n if (settled) return;\n settled = true;\n if (runTimer) clearTimeout(runTimer);\n if (graceTimer) clearTimeout(graceTimer);\n options.signal?.removeEventListener(\"abort\", onAbortSignal);\n bindingAbort.abort();\n control.close();\n void worker.terminate();\n resolve(outcome);\n };\n\n const fail = (kind: PtcErrorKind, message: string, stack?: string): void => {\n finish({\n logs,\n narrations,\n phases,\n error: stack === undefined ? { kind, message } : { kind, message, stack },\n });\n };\n\n /* -------------------------------- sending -------------------------------- */\n\n const sendControl = (frame: PtcHostFrame): void => {\n if (settled) return;\n // Every outbound frame is validated by the same guard the worker's inbound frames\n // go through: a construction bug fails the run here rather than inside the worker.\n if (!isPtcHostFrame(frame)) {\n fail(PTC_ERROR_KIND.protocol, \"internal error: malformed host frame\");\n return;\n }\n control.postMessage(frame);\n };\n\n const sendInit = (): void => {\n const frame: PtcHostFrame = {\n kind: HOST_FRAME_KIND.init,\n runId,\n surface: options.surface,\n code: options.code,\n bindings: [...options.bindings.keys()],\n bindingCandidates: [...BUILTIN_BINDING_NAMES],\n maxItemsPerCall: config.maxItemsPerCall,\n maxPendingCalls: config.maxPendingCalls,\n ...(options.args === undefined ? {} : { args: options.args }),\n };\n if (!isPtcHostFrame(frame)) {\n fail(PTC_ERROR_KIND.protocol, \"internal error: malformed init frame\");\n return;\n }\n const bytes = serializedBytes(frame);\n if (bytes > config.maxMessageBytes) {\n fail(\n PTC_ERROR_KIND.protocol,\n `init frame of ${bytes} bytes exceeds maxMessageBytes (${config.maxMessageBytes})`,\n );\n return;\n }\n sendControl(frame);\n };\n\n /* ------------------------------- receiving ------------------------------- */\n\n const accountOutput = (bytes: number, source: string): boolean => {\n outputBytes += bytes;\n if (outputBytes <= config.maxOutputBytes) return true;\n // R1 §3: an oversize payload keeps the log prefix that did fit.\n fail(\n PTC_ERROR_KIND.outputLimit,\n `${source} exceeded the output budget: logs + result reached ${outputBytes} bytes (maxOutputBytes=${config.maxOutputBytes}); ${logs.length} log line(s) retained`,\n );\n return false;\n };\n\n const acquireDispatchSlot = async (): Promise<void> => {\n if (activeDispatches < config.maxParallelSubCalls) {\n activeDispatches += 1;\n return;\n }\n await new Promise<void>((slot) => {\n dispatchWaiters.push(slot);\n });\n };\n\n const releaseDispatchSlot = (): void => {\n const next = dispatchWaiters.shift();\n if (next) {\n // Hand the slot over directly; `activeDispatches` is unchanged.\n next();\n return;\n }\n activeDispatches -= 1;\n };\n\n const handleCall = (frame: PtcCallFrame): void => {\n pendingCalls += 1;\n if (pendingCalls > config.maxPendingCalls) {\n // Defense in depth only. The shipped worker admits calls before posting them\n // (`maxPendingCalls` travels in the init frame), so a normal program — including a\n // wide `Promise.all` or `parallel()` fan-out — never reaches this branch: the burst\n // queues in the worker, and arrivals here stay at or below the ceiling. This fires\n // only for a worker that ignores its admission budget, which is why it fails the\n // run instead of throttling.\n fail(\n PTC_ERROR_KIND.protocol,\n `more than maxPendingCalls (${config.maxPendingCalls}) binding calls in flight`,\n );\n return;\n }\n void dispatchCall(frame).finally(() => {\n pendingCalls -= 1;\n });\n };\n\n const dispatchCall = async (frame: PtcCallFrame): Promise<void> => {\n const binding = options.bindings.get(frame.tool);\n if (!binding) {\n postCallResult({\n kind: HOST_FRAME_KIND.callResult,\n callId: frame.callId,\n tool: frame.tool,\n ok: false,\n message:\n `no binding named \"${frame.tool}\" in this run; available bindings: ` +\n ([...options.bindings.keys()].join(\", \") || \"(none)\"),\n });\n return;\n }\n await acquireDispatchSlot();\n if (settled) {\n releaseDispatchSlot();\n return;\n }\n try {\n const value = await binding.execute(frame.args, {\n signal: bindingAbort.signal,\n callId: frame.callId,\n depth: runDepth,\n maxDispatchDepth: config.maxDispatchDepth,\n });\n const posted = postCallResult({\n kind: HOST_FRAME_KIND.callResult,\n callId: frame.callId,\n tool: frame.tool,\n ok: true,\n value,\n });\n // Hoist only a result the worker actually received: DSH's condition is a *successful*\n // subtool result, and a payload the port rejected was not one (ADR-0014).\n if (posted) hoistImages(value);\n } catch (error) {\n postCallResult({\n kind: HOST_FRAME_KIND.callResult,\n callId: frame.callId,\n tool: frame.tool,\n ok: false,\n message: messageOf(error),\n });\n } finally {\n releaseDispatchSlot();\n }\n };\n\n /** `true` when the worker actually received the result (see the hoist at the call site). */\n const postCallResult = (frame: PtcCallResultFrame): boolean => {\n if (settled) return false;\n try {\n control.postMessage(frame);\n return true;\n } catch (error) {\n // The binding's value could not be structured-cloned; report the call as failed\n // instead of leaving the worker waiting on a response that will never arrive.\n // The failing payload is dropped rather than resent — it is exactly what the\n // clone rejected.\n try {\n control.postMessage({\n kind: HOST_FRAME_KIND.callResult,\n callId: frame.callId,\n tool: frame.tool,\n ok: false,\n message: `binding result could not be transferred: ${messageOf(error)}`,\n } satisfies PtcCallResultErrorFrame);\n } catch {\n fail(PTC_ERROR_KIND.protocol, \"binding result is not transferable\");\n }\n return false;\n }\n };\n\n const settleTerminal = (outcome: PtcRunOutcome): PtcRunOutcome => {\n if (!cancelling) return outcome;\n // Once cancelled, the caller asked for a stop: the run reports the stop, not the\n // value the program managed to produce inside the grace window.\n return {\n logs,\n narrations,\n phases,\n error: {\n kind: cancelling === \"timeout\" ? PTC_ERROR_KIND.timeout : PTC_ERROR_KIND.abort,\n message: cancelMessage(cancelling),\n },\n };\n };\n\n const handleFrame = (frame: unknown): void => {\n if (settled) return;\n if (!isPtcWorkerFrame(frame)) {\n fail(PTC_ERROR_KIND.protocol, \"malformed worker frame\");\n return;\n }\n const bytes = frame.kind === WORKER_FRAME_KIND.ready ? 0 : serializedBytes(frame);\n if (!Number.isFinite(bytes)) {\n fail(PTC_ERROR_KIND.protocol, \"worker frame could not be serialized for accounting\");\n return;\n }\n if (bytes > config.maxMessageBytes) {\n fail(\n PTC_ERROR_KIND.protocol,\n `control frame of ${bytes} bytes exceeds maxMessageBytes (${config.maxMessageBytes})`,\n );\n return;\n }\n\n switch (frame.kind) {\n case WORKER_FRAME_KIND.ready:\n sendInit();\n return;\n case WORKER_FRAME_KIND.call:\n handleCall(frame);\n return;\n case WORKER_FRAME_KIND.log:\n if (accountOutput(bytes, \"console output\")) logs.push(frame.text);\n return;\n case WORKER_FRAME_KIND.narration:\n if (accountOutput(bytes, \"workflow narration\")) narrations.push(frame.message);\n return;\n case WORKER_FRAME_KIND.phase:\n if (accountOutput(bytes, \"workflow phase\")) phases.push(frame.title);\n return;\n case WORKER_FRAME_KIND.result: {\n if (!accountOutput(bytes, \"completion value\")) return;\n // `settleTerminal` rebuilds the outcome when a cancel owns the terminal state, which\n // drops these fields — the same \"a failed run attaches nothing\" rule as above.\n const base = {\n logs,\n narrations,\n phases,\n ...(images.length > 0 ? { images } : {}),\n };\n finish(\n settleTerminal(frame.value === undefined ? base : { ...base, value: frame.value }),\n );\n return;\n }\n case WORKER_FRAME_KIND.error:\n finish(settleTerminal({ logs, narrations, phases, error: frame.error }));\n return;\n default:\n fail(PTC_ERROR_KIND.protocol, \"unknown worker frame\");\n }\n };\n\n /* ------------------------------- lifecycle ------------------------------- */\n\n function onAbortSignal(): void {\n beginCancel(\"abort\");\n }\n\n function beginCancel(reason: PtcCancelReason): void {\n if (settled || cancelling) return;\n cancelling = reason;\n if (runTimer) {\n clearTimeout(runTimer);\n runTimer = undefined;\n }\n bindingAbort.abort();\n sendControl({ kind: HOST_FRAME_KIND.cancel, reason });\n graceTimer = setTimeout(() => {\n fail(\n reason === \"timeout\" ? PTC_ERROR_KIND.timeout : PTC_ERROR_KIND.abort,\n cancelMessage(reason),\n );\n }, config.graceMs);\n }\n\n worker.on(\"error\", (error: Error) => {\n fail(PTC_ERROR_KIND.workerExit, `worker failed: ${messageOf(error)}`);\n });\n worker.on(\"exit\", (code: number) => {\n fail(PTC_ERROR_KIND.workerExit, `worker exited with code ${code} before the run completed`);\n });\n control.on(\"message\", handleFrame);\n control.on(\"messageerror\", () => {\n fail(PTC_ERROR_KIND.protocol, \"worker sent a frame that could not be deserialized\");\n });\n\n options.signal?.addEventListener(\"abort\", onAbortSignal, { once: true });\n runTimer = setTimeout(() => {\n beginCancel(\"timeout\");\n }, timeoutMs);\n\n // The connect frame carries the control port *in the payload*; a transfer-list-only\n // entry is silently dropped by Node and the worker would wait forever.\n const connectFrame: PtcHostFrame = { kind: HOST_FRAME_KIND.connect, port: workerPort };\n if (!isPtcHostFrame(connectFrame)) {\n fail(PTC_ERROR_KIND.protocol, \"internal error: malformed connect frame\");\n } else {\n worker.postMessage(connectFrame, [workerPort]);\n }\n });\n}\n","/**\n * Model-facing text hygiene: the step pi's built-in tools take and PTC's own text assembly missed.\n *\n * Every built-in tool result is cleaned before the model reads it —\n * `stripAnsi` → `sanitizeBinaryOutput` → drop `\\r` (`pi-coding-agent`:\n * `dist/core/tools/render-utils.js`) — because a tool's output is arbitrary bytes: ANSI colour\n * codes from `pnpm`/`vitest`, carriage-return progress bars, NUL bytes from a binary file.\n *\n * The PTC tools assemble their own text block (`common.ts: renderToolResult`) and did none of\n * that, so a program returning captured `bash` output shipped `\\u001b[38;2;212;212;212m` to the\n * model as literal escapes, and any string with a real newline arrived JSON-escaped as `\\n`.\n * Both are pure noise the model has to read — and ANSI noise is charged per character, in tokens.\n *\n * **Scope: the text block only.** `details` stays raw so the TUI keeps rendering real colours\n * (`@earendil-works/pi-tui`'s `Text` understands ANSI), and a binding's result inside a program\n * is untouched — a program that wants coloured bytes for a file still has them. See ADR-0012.\n *\n * pi exports neither helper (its `exports` map has only `.`, `./rpc-entry`, `./client`,\n * `./experimental/plugin`), so both are mirrored here rather than imported. The ANSI pattern\n * follows ansi-regex / strip-ansi (MIT, Sindre Sorhus) via pi's own implementation.\n */\nimport type { PtcJsonValue } from \"../runtime/protocol.ts\";\n\n/** Hard cap for one rendered line; longer lines are truncated with `…`. */\nexport const MAX_LINE_CHARS = 200;\n\n/** A value stays on one line while its inline form fits this; otherwise it renders as a block. */\nconst INLINE_MAX_CHARS = 100;\n\n/** Indent applied per nesting level in block form. */\nconst INDENT = \" \";\n\n/**\n * ANSI escapes, in the two shapes that matter: OSC (\\`ESC ] …\\` to BEL, \\`ESC \\\\\\\\` or C1 ST) and\n * CSI plus friends (introducer, optional intermediates, optional params, final byte).\n *\n * Built through the \\`RegExp\\` constructor from an escaped source so no control character appears\n * literally — the repo lints via oxlint, whose \\`no-control-regex\\` rule rejects that.\n */\nconst ANSI_PATTERN = new RegExp(\n [\n \"(?:\\\\u001B\\\\][\\\\s\\\\S]*?(?:\\\\u0007|\\\\u001B\\\\u005C|\\\\u009C))\",\n \"[\\\\u001B\\\\u009B][[\\\\]()#;?]*(?:\\\\d{1,4}(?:[;:]\\\\d{0,4})*)?[\\\\dA-PR-TZcf-nq-uy=><~]\",\n ].join(\"|\"),\n \"g\",\n);\n\n/** Drop ANSI escape sequences (colours, cursor moves, OSC hyperlinks). */\nexport function stripAnsi(value: string): string {\n // Fast path: without an ESC or C1 introducer there is nothing to strip.\n if (!value.includes(String.fromCharCode(27)) && !value.includes(String.fromCharCode(155))) {\n return value;\n }\n return value.replace(ANSI_PATTERN, \"\");\n}\n\n/**\n * Make arbitrary program output safe to hand the model.\n *\n * Same three steps as pi's built-ins: strip ANSI, remove `\\r` (a progress bar's carriage\n * return is not a line break), then drop control characters other than `\\n`/`\\t` and the\n * Unicode format characters that break width math. Lone surrogates fall out of code-point\n * iteration for free.\n */\nexport function sanitizeText(value: string): string {\n const stripped = stripAnsi(value).replace(/\\r/g, \"\");\n if (stripped.length === 0) return stripped;\n let out = \"\";\n for (const char of stripped) {\n const code = char.codePointAt(0);\n if (code === undefined) continue;\n if (code === 0x0a || code === 0x09) {\n out += char;\n continue;\n }\n if (code <= 0x1f) continue;\n if (code >= 0xfff9 && code <= 0xfffb) continue;\n out += char;\n }\n return out;\n}\n\n/** Object keys that read as identifiers in the summary style are left unquoted. */\nconst BARE_KEY = /^[A-Za-z_$][A-Za-z0-9_$]*$/;\n\nfunction keyText(key: string): string {\n return BARE_KEY.test(key) ? key : JSON.stringify(key);\n}\n\n/**\n * The inline form of a value, or `undefined` when it cannot be inlined.\n *\n * A string with a real newline is never inlined: keeping its line structure is the whole point\n * (see ADR-0012), and quoting it would turn one readable block back into `\\n` escapes.\n */\nfunction inlineForm(value: PtcJsonValue): string | undefined {\n if (value === null) return \"null\";\n if (typeof value === \"number\" || typeof value === \"boolean\") return String(value);\n if (typeof value === \"string\") {\n const sanitized = sanitizeText(value);\n return sanitized.includes(\"\\n\") ? undefined : JSON.stringify(sanitized);\n }\n if (Array.isArray(value)) {\n const items: string[] = [];\n for (const item of value) {\n const rendered = inlineForm(item);\n if (rendered === undefined) return undefined;\n items.push(rendered);\n }\n return `[${items.join(\", \")}]`;\n }\n const keys = Object.keys(value);\n if (keys.length === 0) return \"{}\";\n const parts: string[] = [];\n for (const key of keys) {\n const rendered = inlineForm(value[key] ?? null);\n if (rendered === undefined) return undefined;\n parts.push(`${keyText(key)}: ${rendered}`);\n }\n return `{${parts.join(\", \")}}`;\n}\n\n/** Inline rendering, but only while it stays short enough to read on one line. */\nfunction tryInline(value: PtcJsonValue): string | undefined {\n const rendered = inlineForm(value);\n if (rendered === undefined) return undefined;\n return rendered.length <= INLINE_MAX_CHARS ? rendered : undefined;\n}\n\n/** A non-container value on its own line (block form only meets these defensively). */\nfunction scalarText(value: string | number | boolean | null): string {\n if (value === null) return \"null\";\n return typeof value === \"string\" ? sanitizeText(value) : String(value);\n}\n\n/** One key per line, braces kept, string values raw (real newlines preserved, indented). */\nfunction blockLines(value: PtcJsonValue, indent: string): string[] {\n if (value === null || typeof value !== \"object\") return [`${indent}${scalarText(value)}`];\n if (Array.isArray(value)) {\n if (value.length === 0) return [`${indent}[]`];\n const lines = [`${indent}[`];\n for (const item of value) {\n const inline = tryInline(item);\n if (inline !== undefined) {\n lines.push(`${indent}${INDENT}${inline}`);\n continue;\n }\n lines.push(...valueLines(item, indent + INDENT));\n }\n lines.push(`${indent}]`);\n return lines;\n }\n\n const keys = Object.keys(value);\n if (keys.length === 0) return [`${indent}{}`];\n const lines = [`${indent}{`];\n const inner = indent + INDENT;\n for (const key of keys) {\n const child = value[key] ?? null;\n const inline = tryInline(child);\n if (inline !== undefined) {\n lines.push(`${inner}${keyText(key)}: ${inline}`);\n continue;\n }\n if (typeof child === \"string\") {\n // A multi-line string keeps its own lines; the key line above delimits it.\n lines.push(`${inner}${keyText(key)}:`);\n for (const line of sanitizeText(child).split(\"\\n\")) {\n // A blank line inside a multi-line value stays blank — indenting it would leave trailing\n // whitespace in every transcript that shows a block ending in a newline.\n lines.push(line.length === 0 ? \"\" : `${inner}${INDENT}${line}`);\n }\n continue;\n }\n // A container opens on its key's line (`outer: {`) and closes back at the key's indent, so a\n // nested value does not push its braces onto lines of their own.\n const nested = blockLines(child, inner);\n lines.push(\n `${inner}${keyText(key)}: ${(nested[0] ?? \"\").trimStart()}`.trimEnd(),\n ...nested.slice(1),\n );\n }\n lines.push(`${indent}}`);\n return lines;\n}\n\nfunction valueLines(value: PtcJsonValue, indent: string): string[] {\n if (typeof value === \"string\") return sanitizeText(value).split(\"\\n\");\n if (value === null || typeof value !== \"object\") return [String(value)];\n return blockLines(value, indent);\n}\n\n/** Truncate every line that exceeds {@link MAX_LINE_CHARS}. */\nfunction capLines(text: string): string {\n return text\n .split(\"\\n\")\n .map((line) => (line.length > MAX_LINE_CHARS ? `${line.slice(0, MAX_LINE_CHARS - 1)}…` : line))\n .join(\"\\n\");\n}\n\n/**\n * Render a completion value for the model: strings verbatim, everything else as a compact\n * `{key: value}` summary or an indented block — never `JSON.stringify` on the whole value, whose\n * escaping destroys exactly the content a reader needs (newlines, quotes, non-ASCII spacing).\n */\nexport function renderModelValue(value: PtcJsonValue): string {\n // A program that returns text reads as text — quoting a top-level string would turn a report\n // into one escaped line, which is the failure mode this module exists to remove.\n if (typeof value === \"string\") return capLines(sanitizeText(value));\n if (value === null || typeof value !== \"object\") return String(value);\n const inline = tryInline(value);\n const body = inline !== undefined ? [inline] : valueLines(value, \"\");\n return capLines(body.join(\"\\n\"));\n}\n","/**\n * Shared plumbing for the two model-facing PTC tools (`ptc_run_code`, `ptc_workflow`).\n *\n * Both tools are thin. They resolve the run's working directory, hand the program to\n * `runPtcProgram()` (T3), and turn the dispatcher's outcome into one of pi's two tool-result\n * shapes:\n *\n * - **success** — `content` carries one text block (logs, workflow narration, phases, then the\n * completion value) and `details` carries the same facts structurally for rendering and logs;\n * - **failure** — the tool *throws* (pi's convention: `AgentTool.execute` — \"Throw on failure\n * instead of encoding errors in `content`\"), with R1 §3's message shape\n * `code run failed (<kind>): <message>` plus a `Captured output:` block.\n *\n * The dispatcher never rejects for program failures — it reports them as `outcome.error` — so\n * \"did the run fail\" is one check in one place, shared by both tools.\n *\n * The text block is the model's copy, so it is cleaned and laid out for reading (`./text.ts`:\n * ANSI stripped, control characters dropped, strings with real newlines kept multi-line) while\n * `details` stays raw for the TUI. See ADR-0012.\n */\nimport { randomUUID } from \"node:crypto\";\nimport { writeFileSync } from \"node:fs\";\nimport { tmpdir } from \"node:os\";\nimport { join } from \"node:path\";\nimport type { ImageContent, TextContent } from \"@earendil-works/pi-ai\";\nimport { BUILTIN_BINDING_NAMES } from \"../runtime/bindings.ts\";\nimport {\n DEFAULT_MAX_BYTES,\n DEFAULT_MAX_LINES,\n formatSize,\n truncateTail,\n} from \"@earendil-works/pi-coding-agent\";\nimport type { AgentToolResult, ExtensionContext } from \"@earendil-works/pi-coding-agent\";\nimport type { PtcRunOutcome } from \"../runtime/dispatcher.ts\";\nimport type { PtcConfig, PtcSurface } from \"../runtime/limits.ts\";\nimport type { PtcJsonValue } from \"../runtime/protocol.ts\";\nimport { renderModelValue, sanitizeText } from \"./text.ts\";\n\n/** Options every PTC tool factory accepts. */\nexport interface PtcToolOptions {\n /**\n * Per-run limit overrides, merged over `DEFAULT_CONFIG` by the dispatcher (`resolveConfig`).\n *\n * The shipped extension registers the tools without overrides; the seam exists so tests can\n * exercise budget/grace behaviour without materializing 64 MiB of logs, and so a future\n * settings surface has somewhere to plug in.\n */\n config?: Partial<PtcConfig>;\n /**\n * The tool names the binding table is built from, read once per execute.\n *\n * The shipped extension returns the session's active tools — except while PTC mode is on, when\n * it returns the mode's base snapshot instead (the built-ins the mode hid must stay callable\n * from inside a program). See `src/mode/ptc-mode.ts`.\n *\n * Omitting it means \"no session context\" (direct library use, or a hand-built test stub) and\n * asserts the full built-in surface.\n */\n getBindingSourceNames?: () => readonly string[];\n}\n\n/**\n * Binding names for one run: the built-ins this session actually has enabled (T7, #21).\n *\n * A PTC program must never reach further than the session it runs in — a session started\n * with `--tools read` or `--no-builtin-tools` must not be escapable through `tools.<name>`\n * calls. Names outside the built-in factory set are ignored: only tools this package can\n * build adapters for can be bound.\n */\nexport function resolveBindingNames(\n activeToolNames: readonly string[] | undefined,\n): readonly string[] {\n if (activeToolNames === undefined) return BUILTIN_BINDING_NAMES;\n const active = new Set(activeToolNames);\n return BUILTIN_BINDING_NAMES.filter((name) => active.has(name));\n}\n\n/**\n * One-line entry for the system prompt's \"Available tools\" section.\n *\n * Custom tools are omitted from that section unless they set this, which matters most in PTC mode:\n * there the two PTC surfaces are the *only* callable tools, so a prompt that lists no usable tool\n * would contradict the tool declarations. `promptGuidelines` cannot substitute for it — those\n * bullets land in the flat `Guidelines` section, which does not tell the model what it can call.\n */\nexport const PTC_RUN_CODE_SNIPPET =\n \"Run a TypeScript program that composes pi's tools in one shot (tools.<name>(args)); only its return value and console.log come back\";\n\nexport const PTC_WORKFLOW_SNIPPET =\n \"Run a structured TypeScript workflow with named phases, narration (log/phase) and structured concurrency (parallel/pipeline)\";\n\n/**\n * Guidelines are appended flat to the `Guidelines` section with no tool-name prefix, so each bullet\n * must name its tool explicitly (pi's docs call this out; \"Use this tool when…\" is ambiguous).\n * These are the normal-mode hints — in PTC mode the injected briefing supersedes them.\n */\nexport const PTC_TOOL_GUIDELINES: readonly string[] = [\n // DSH states this contract in `run_code`'s own description (\"Image-bearing subtool results are\n // attached after the run.\"); the model has to know because the images never appear in the value it\n // returns (ADR-0014).\n \"Image-bearing tool results inside a program (a `tools.read` on a PNG, say) are attached to you after the run — never return image data as the completion value.\",\n \"Use ptc_run_code when a task needs several tool calls whose intermediate output you do not need to see — gather it inside the program and return only the final value.\",\n \"Use ptc_workflow instead of ptc_run_code when the work has named phases and the user benefits from seeing progress narration.\",\n];\n\n/** Tool name per worker surface — the names the model calls and reads in failure messages. */\nexport const SURFACE_TOOL_NAME: Record<PtcSurface, string> = {\n run_code: \"ptc_run_code\",\n workflow: \"ptc_workflow\",\n};\n\n/**\n * Resolve the run's working directory from pi's tool execution context.\n *\n * Investigated against `@earendil-works/pi-coding-agent@0.86.1`: `execute`'s fifth parameter is\n * `ExtensionContext`, and the type *does* expose `cwd: string` — documented there as \"Current\n * working directory\" (`dist/core/extensions/types.d.ts`), the same value pi's own built-in tools\n * resolve relative paths against (`ToolRenderContext.cwd`, and the extension guide's\n * `resolve(ctx.cwd, params.path)` pattern). So the session cwd comes from pi, and this package\n * never has to guess from `process.cwd()`.\n *\n * The `process.cwd()` fallback stays for hand-built contexts (tests, or a future caller passing a\n * partial object); pi always supplies the field.\n */\nexport function resolveToolCwd(ctx: ExtensionContext): string {\n const cwd: unknown = (ctx as { cwd?: unknown } | undefined)?.cwd;\n return typeof cwd === \"string\" && cwd.length > 0 ? cwd : process.cwd();\n}\n\n/** Structured, model-independent facts about a run: pi's `details` payload for both tools. */\nexport interface PtcToolDetails {\n /** Which worker surface produced this result. */\n surface: PtcSurface;\n /** `console.*` output in arrival order. */\n logs: string[];\n /** Workflow `log(message)` narration (always empty on `ptc_run_code`). */\n narrations: string[];\n /** Workflow `phase(title)` titles in arrival order (always empty on `ptc_run_code`). */\n phases: string[];\n /** Non-fatal diagnostics the run itself could not report (currently: unlisted phase titles). */\n warnings: string[];\n /** Completion value; absent when the program returned nothing. */\n result?: PtcJsonValue;\n /** Wall-clock duration of the whole run, including worker spawn and teardown. */\n durationMs: number;\n /**\n * How many images the run hoisted out of tool results (`outcome.images`).\n *\n * The images themselves live in the tool result's `content` (pi forwards them to the model from\n * there), so `details` needs only the count — a second copy here would pin the same base64 twice.\n * See ADR-0014.\n */\n imageCount: number;\n /**\n * Where the untruncated text block was written when pi's truncation contract cut it.\n *\n * Present only for a truncated run. The full text has no other home — a program's logs exist only\n * for the length of the run — so the file is the model's way back to the parts the text block had\n * to drop (it can `tools.read` it from inside the next program).\n */\n fullOutputPath?: string;\n}\n\n/**\n * The captured-output lines R1 §3 appends when a run fails.\n *\n * Phases and narrations are prefixed because they are workflow bookkeeping rather than console\n * output; `ptc_run_code` has neither, so its block is exactly the `logs` join R1 describes.\n */\nfunction capturedOutputLines(outcome: PtcRunOutcome): string[] {\n return [\n ...outcome.phases.map((title) => `[phase] ${sanitizeText(title)}`),\n ...outcome.narrations.map((message) => `[log] ${sanitizeText(message)}`),\n ...outcome.logs.map((line) => sanitizeText(line)),\n ];\n}\n\n/**\n * Build the error a failed run is surfaced with (R1 §3's `CodeRunFailedError` message).\n *\n * The model must be able to fix its program from this message alone: the error kind and message\n * explain what broke, and the captured output shows how far the program got before it did. There\n * is no `sandbox` text and no escalation guidance — ADR-0007 ships no OS sandbox, so the only\n * failure classes are the protocol ones.\n */\nexport function codeRunFailedError(outcome: PtcRunOutcome): Error {\n const error = outcome.error;\n if (error === undefined) {\n // Callers check `outcome.error` first; reaching here means the tool layer asked to render a\n // success as a failure, which is a bug in this package rather than in the model's program.\n return new Error(\"internal error: a successful PTC run was rendered as a failure\");\n }\n const captured = capturedOutputLines(outcome);\n const block = captured.length === 0 ? \"\" : `\\nCaptured output:\\n${captured.join(\"\\n\")}`;\n // A program can throw a message containing the raw bytes it just read (a coloured shell\n // error, a log line with escapes); the failure text is model-facing too.\n const failure = new Error(\n sanitizeText(`code run failed (${error.kind}): ${error.message}${block}`),\n );\n failure.name = \"CodeRunFailedError\";\n return failure;\n}\n\n/** Structural view of pi's `truncateTail` outcome, so the footer does not re-derive the numbers. */\ntype TailTruncation = ReturnType<typeof truncateTail>;\n\n/**\n * Write the untruncated text block to a temp file and return its path.\n *\n * Mirrors what pi's own `bash` does with a long output (its `fullOutputPath`): the run is the only\n * place the text ever existed, so a file under the OS temp dir is the difference between \"the rest is\n * gone\" and \"the rest is one `tools.read` away\".\n */\nfunction writeFullOutput(text: string): string {\n const filePath = join(tmpdir(), `pi-ptc-output-${randomUUID()}.txt`);\n writeFileSync(filePath, text, \"utf8\");\n return filePath;\n}\n\n/**\n * The `[Showing … Full output: path]` footer pi's built-ins use, worded for the same numbers.\n *\n * `truncatedBy` decides the wording: a line cut reports the line range, a byte cut also names the\n * ceiling, and a single oversized last line reports its own size — exactly what `bash` prints.\n */\nfunction truncationFooter(truncation: TailTruncation, fullOutputPath: string): string {\n const startLine = truncation.totalLines - truncation.outputLines + 1;\n const endLine = truncation.totalLines;\n if (truncation.lastLinePartial) {\n return `[Showing last ${formatSize(truncation.outputBytes)} of line ${endLine}. Full output: ${fullOutputPath}]`;\n }\n if (truncation.truncatedBy === \"lines\") {\n return `[Showing lines ${startLine}-${endLine} of ${truncation.totalLines}. Full output: ${fullOutputPath}]`;\n }\n return `[Showing lines ${startLine}-${endLine} of ${truncation.totalLines} (${formatSize(DEFAULT_MAX_BYTES)} limit). Full output: ${fullOutputPath}]`;\n}\n\n/**\n * Render one successful outcome as pi's tool result.\n *\n * `content` is ordered the way a reader reconstructs the run: the phase roll-up, then workflow\n * narration, then console output, then the completion value, then warnings. Empty runs get R1's\n * placeholder (`(run_code completed with no output)`) so the model always sees a non-empty text\n * block — a tool result with nothing in it looks like a harness failure.\n *\n * `details` is deliberately the *uncleaned* copy: logs keep their ANSI so the TUI can still\n * colour them, and `result` stays structured for `render.ts`.\n */\nexport function renderToolResult(input: {\n outcome: PtcRunOutcome;\n surface: PtcSurface;\n durationMs: number;\n warnings?: readonly string[];\n}): AgentToolResult<PtcToolDetails> {\n const { outcome, surface } = input;\n const warnings = [...(input.warnings ?? [])];\n const images = outcome.images ?? [];\n const parts: string[] = [];\n if (outcome.phases.length > 0) {\n parts.push(`Phases: ${outcome.phases.map((title) => sanitizeText(title)).join(\" → \")}`);\n }\n if (outcome.narrations.length > 0) {\n parts.push(outcome.narrations.map((message) => sanitizeText(message)).join(\"\\n\"));\n }\n if (outcome.logs.length > 0) {\n parts.push(outcome.logs.map((line) => sanitizeText(line)).join(\"\\n\"));\n }\n if (outcome.value !== undefined) parts.push(renderModelValue(outcome.value));\n for (const warning of warnings) parts.push(`Warning: ${sanitizeText(warning)}`);\n const built =\n parts.length > 0\n ? parts.join(\"\\n\")\n : `(${SURFACE_TOOL_NAME[surface]} completed with no output)`;\n // pi's rule for a tool's model-facing text is \"Tools MUST truncate their output\" — 50 KB / 2000\n // lines, keeping the tail, with the remainder pointed at — and this package's own 64 MiB run\n // budget (ADR-0003) is what makes it bite. `fullOutputPath` is written before the tail is cut, so\n // the pointer never names a partial file.\n const truncation = truncateTail(built, {\n maxLines: DEFAULT_MAX_LINES,\n maxBytes: DEFAULT_MAX_BYTES,\n });\n let fullOutputPath: string | undefined;\n let text = truncation.content;\n if (truncation.truncated) {\n fullOutputPath = writeFullOutput(built);\n text += `\\n\\n${truncationFooter(truncation, fullOutputPath)}`;\n }\n // The text block is the model's copy; hoisted images ride the same result as image blocks, which\n // is how pi's own `read` hands a picture to the model (ADR-0014).\n const content: (TextContent | ImageContent)[] = [\n { type: \"text\", text },\n ...images.map((image): ImageContent => ({\n type: \"image\",\n data: image.data,\n mimeType: image.mimeType,\n })),\n ];\n return {\n content,\n details: {\n surface,\n logs: [...outcome.logs],\n narrations: [...outcome.narrations],\n phases: [...outcome.phases],\n warnings,\n ...(outcome.value === undefined ? {} : { result: outcome.value }),\n durationMs: input.durationMs,\n imageCount: images.length,\n ...(fullOutputPath === undefined ? {} : { fullOutputPath }),\n },\n };\n}\n","/**\n * Shared TUI renderer for `ptc_run_code` and `ptc_workflow`.\n *\n * Both tools produce the same `PtcToolDetails` shape (see `common.ts`), so one pair of helpers\n * drives both `renderCall` and `renderResult`.\n *\n * ## Shape: a tree, not a payload dump\n *\n * The row reads as a node in the transcript tree:\n *\n * `└─ PTC <description>` (call row)\n * ` → {file, clipNow} • 1 image · 536ms` (result collapsed — summary + right meta)\n * ` ├─ code <line 1>` (result expanded — children with tree connectors)\n * ` │ <line 2>`\n * ` ├─ out [bash stdout] …`\n * ` ├─ value { file: … }`\n * ` └─ image 1 image attached`\n *\n * The summary line and the children share the call's column, so the connectors only need to\n * distinguish nesting (`├─/└─`) from continuation (`│/space`). Right-aligned meta lives on the\n * result row, not the call row, because `renderCall` does not see the run outcome — pi's TUI\n * calls renderCall and renderResult separately, and threading the result details into renderCall\n * would break that seam.\n *\n * ## What a PTC row must never do\n *\n * Print the completion value via `JSON.stringify`. Escaping turns a returned report into `\\n` noise\n * wrapped over five rows — that is the ugly report this renderer exists to fix (ADR-0013). Values\n * render through `renderModelValue`, which keeps newlines real and indents containers.\n */\nimport type { Theme } from \"@earendil-works/pi-coding-agent\";\nimport { truncateToWidth, visibleWidth } from \"@earendil-works/pi-tui\";\nimport type { PtcToolDetails } from \"./common.ts\";\nimport type { PtcJsonObject, PtcJsonValue } from \"../runtime/protocol.ts\";\nimport { renderModelValue, sanitizeText } from \"./text.ts\";\n\n/** Call-row label per surface; `ptc_workflow` gets its own so the two rows stay tellable apart. */\nconst TOOL_TITLE: Record<PtcToolDetails[\"surface\"], string> = {\n run_code: \"PTC\",\n workflow: \"PTC workflow\",\n};\n\n/**\n * Tree-connector layout. The call row owns column 0–2 (`└─ `); everything below it sits at column 3.\n * Children are siblings of the call's content (the summary line `→ {hint}`), so a child row needs a\n * connector to distinguish itself from the summary — and a continuation prefix to distinguish its\n * own wrapped lines from the child's first line.\n *\n * - Root call row: `└─ PTC …`\n * - Root continuation: ` → {hint}` or ` failed: …` (summary; same column as children)\n * - First / middle child: ` ├─ code …`\n * - First / middle cont.: ` │ …`\n * - Last child: ` └─ image …`\n * - Last cont.: ` …` (no connector — same shape as a wrapped value line)\n *\n * `TREE_INDENT` (3 spaces) is the column children and root continuations share.\n */\nconst TREE_ROOT = \"└─ \"; // call-row prefix\nconst TREE_ROOT_CONT = \" \"; // continuation under the root (summary line + error text)\nconst TREE_INDENT = \" \"; // column children start at\nconst TREE_FIRST = \"├─ \"; // connector of first / middle child\nconst TREE_LAST = \"└─ \"; // connector of last child\nconst TREE_CONT_FIRST = \"│ \"; // continuation of first / middle child\nconst TREE_CONT_LAST = \" \"; // continuation of last child (just spaces — no connector)\n\n/**\n * Gutter width for child labels. `code` / `out` / `value` / `warn` / `image` are 3–5 chars; `phases`\n * is 6 — pad to 8 so the longest label gets two trailing spaces before its content.\n */\nconst LABEL_WIDTH = 8;\n\nconst MAX_CODE_LINES_EXPANDED = 3;\nconst MAX_CODE_LINE_CHARS = 120;\nconst MAX_LOG_LINES_EXPANDED = 12;\nconst MAX_PHASES_EXPANDED = 8;\nconst MAX_WARNINGS_EXPANDED = 4;\nconst MAX_RESULT_HINT_CHARS = 60;\nconst MAX_ERROR_CHARS = 120;\n\n/** One rendered row: left content, plus an optional segment pinned to the right edge. */\ninterface RowLine {\n readonly left: string;\n readonly right?: string;\n}\n\n/**\n * A `Component` that right-aligns one meta segment per line.\n *\n * Stateless: it holds already-styled strings and pads them at render time, so pi can re-render at a\n * new width (terminal resize) without any invalidation bookkeeping.\n */\nclass PtcRow {\n private readonly lines: readonly RowLine[];\n\n constructor(lines: readonly RowLine[]) {\n this.lines = lines;\n }\n\n /** Part of the `Component` contract; nothing is cached, so nothing has to be dropped. */\n invalidate(): void {}\n\n render(width: number): string[] {\n const safeWidth = Math.max(1, Math.floor(width));\n return this.lines.map((line) => alignRow(line, safeWidth));\n }\n}\n\n/**\n * Lay out one row inside `width` columns.\n *\n * With no meta segment the line is simply truncated. With one, the meta is what must survive — a\n * count that has been cut in half (`• 19 outpu…`) tells the reader nothing — so the left side gives\n * up room first, and only a meta wider than the whole viewport is truncated itself.\n */\nfunction alignRow(line: RowLine, width: number): string {\n const { left, right } = line;\n if (right === undefined) return truncateToWidth(left, width, \"…\");\n const rightWidth = visibleWidth(right);\n if (rightWidth >= width) return truncateToWidth(right, width, \"…\");\n const leftText = truncateToWidth(left, Math.max(0, width - rightWidth - 1), \"…\");\n const pad = Math.max(1, width - rightWidth - visibleWidth(leftText));\n return leftText + \" \".repeat(pad) + right;\n}\n\n/** Truncate `s` to `n` chars, appending `…` when the input was longer. */\nfunction truncateChars(s: string, n: number): string {\n return s.length > n ? `${s.slice(0, n - 1)}…` : s;\n}\n\n/** First non-blank line of a block of text, sanitized and capped. */\nfunction firstLine(value: string): string {\n const line = sanitizeText(value)\n .split(\"\\n\")\n .find((candidate) => candidate.trim().length > 0);\n return truncateChars((line ?? \"\").trim(), MAX_ERROR_CHARS);\n}\n\n/** `n` rendered as `1.2s` / `850ms` (so users can tell short from long at a glance). */\nfunction formatDuration(ms: number): string {\n if (ms < 1000) return `${ms}ms`;\n return `${(ms / 1000).toFixed(2).replace(/\\.?0+$/, \"\")}s`;\n}\n\n/** `3 phases` / `1 phase` — singular and plural are both reachable from a real run. */\nfunction plural(count: number, noun: string): string {\n return `${count} ${noun}${count === 1 ? \"\" : \"s\"}`;\n}\n\n/**\n * Pull the first line of code that is actually doing something — not a leading comment, blank\n * line, or import. Used as the call row's fallback when the model sent no description.\n */\nexport function firstMeaningfulCodeLine(code: string): string | undefined {\n for (const raw of code.split(\"\\n\")) {\n const line = raw.trim();\n if (line.length === 0) continue;\n if (line.startsWith(\"//\")) continue;\n if (line.startsWith(\"/*\")) continue;\n if (line.startsWith(\"*\")) continue;\n // Strip trailing inline comment so the preview doesn't double as a docstring\n const withoutTrailingComment = line.replace(/\\s*\\/\\/.*$/, \"\");\n return truncateChars(withoutTrailingComment, MAX_CODE_LINE_CHARS);\n }\n return undefined;\n}\n\n/**\n * The completion value as a single line a human can read: the value itself while it stays short\n * and single-line, otherwise its shape (`{version, node}`, `Array(3)`).\n *\n * A multi-line string contributes its first non-blank line plus a `+N lines` tail, which is the\n * whole difference between one readable row and the escaped wall this replaces.\n */\nfunction resultHint(value: PtcJsonValue | undefined): string | undefined {\n if (value === undefined) return undefined;\n if (typeof value === \"string\") {\n const lines = sanitizeText(value).split(\"\\n\");\n const first = lines.find((line) => line.trim().length > 0)?.trim() ?? \"\";\n const extra = lines.length > 1 ? ` (+${lines.length - 1} lines)` : \"\";\n if (first.length === 0 && extra.length === 0) return `\"\"`;\n return truncateChars(first, MAX_RESULT_HINT_CHARS) + extra;\n }\n if (value === null || typeof value !== \"object\") return String(value);\n const inline = renderModelValue(value);\n if (!inline.includes(\"\\n\") && inline.length <= MAX_RESULT_HINT_CHARS) return inline;\n if (Array.isArray(value)) return `Array(${value.length})`;\n const keys = Object.keys(value);\n if (keys.length === 0) return \"{}\";\n const head = keys.slice(0, 4).join(\", \");\n return `{${head}${keys.length > 4 ? `, +${keys.length - 4}` : \"\"}}`;\n}\n\n/**\n * Right-aligned meta: the run's countable facts, in the order a reader asks for them — how much\n * output, how long it took, how many phases (workflow), and anything that went wrong.\n */\nfunction metaText(details: PtcToolDetails | undefined, theme: Theme): string | undefined {\n if (details === undefined) return undefined;\n const segments: string[] = [];\n const outputLines = details.logs.length + details.narrations.length;\n if (outputLines > 0) segments.push(theme.fg(\"dim\", plural(outputLines, \"output line\")));\n // Images rode the tool result (ADR-0014): the count says so even though pi renders them itself.\n if (details.imageCount > 0) {\n segments.push(theme.fg(\"toolOutput\", plural(details.imageCount, \"image\")));\n }\n if (details.surface === \"workflow\" && details.phases.length > 0) {\n segments.push(theme.fg(\"dim\", plural(details.phases.length, \"phase\")));\n }\n if (details.warnings.length > 0) {\n segments.push(theme.fg(\"warning\", plural(details.warnings.length, \"warning\")));\n }\n // pi's truncation contract cut the text block: the tail is what the model read, the file is the rest.\n if (details.fullOutputPath !== undefined) segments.push(theme.fg(\"warning\", \"truncated\"));\n segments.push(theme.fg(\"dim\", formatDuration(details.durationMs)));\n return `${theme.fg(\"dim\", \"•\")} ${segments.join(theme.fg(\"dim\", \" · \"))}`;\n}\n\n/**\n * One child block in the expanded view: a `label` (right-padded into the gutter), N content lines,\n * and an optional `more` count for the `…+N more lines` tail.\n *\n * Block ordering is decided by the caller (see `renderPtcToolResultExpanded`); this module only\n * owns how a block lays itself out.\n */\ninterface ChildBlock {\n readonly label: string;\n readonly lines: readonly string[];\n readonly more?: number;\n}\n\n/** Render one child block as `RowLine[]`, with `├─` / `│` / `└─` connectors decided by position. */\nfunction renderChildBlock(child: ChildBlock, isLast: boolean, theme: Theme): RowLine[] {\n const firstPrefix = TREE_INDENT + (isLast ? TREE_LAST : TREE_FIRST);\n const contPrefix = TREE_INDENT + (isLast ? TREE_CONT_LAST : TREE_CONT_FIRST);\n const gutter = \" \".repeat(LABEL_WIDTH);\n const rows: RowLine[] = [];\n const first = child.lines[0] ?? \"\";\n rows.push({\n left: `${firstPrefix}${child.label.padEnd(LABEL_WIDTH)}${theme.fg(\"dim\", first)}`,\n });\n for (let i = 1; i < child.lines.length; i++) {\n rows.push({\n left: `${contPrefix}${gutter}${theme.fg(\"dim\", child.lines[i] ?? \"\")}`,\n });\n }\n if (child.more !== undefined && child.more > 0) {\n rows.push({\n left: `${contPrefix}${gutter}…+${child.more} more lines`,\n });\n }\n return rows;\n}\n\n// ---------------------------------------------------------------------------\n// Value-as-tree renderer (ADR-0013 §5)\n//\n// The value a program returns is a tree when it is a JSON object or array:\n// each property (or index) gets its own row with a ├─ / └─ connector, recursive\n// children inherit the parent's continuation bar, and a tail row collapses\n// what was withheld. This module only knows PtcJsonValue; consumers wrap each\n// emitted row with TREE_ROOT_CONT (so a row that starts with ├─ lands at the\n// call row's column-3 indent).\n// ---------------------------------------------------------------------------\n\n/** Defaults: collapse at 4 levels, 6 children per container, 120 chars per row. */\nexport const TREE_VALUE_MAX_DEPTH = 4;\nexport const TREE_VALUE_MAX_CHILDREN = 6;\nexport const TREE_VALUE_MAX_LINE_CHARS = 120;\n\n/** A JSON value is \"expandable\" when it is a non-empty object or array. */\nexport function isExpandableContainer(\n value: PtcJsonValue,\n): value is PtcJsonObject | PtcJsonValue[] {\n if (value === null || typeof value !== \"object\") return false;\n if (Array.isArray(value)) return value.length > 0;\n return Object.keys(value).length > 0;\n}\n\n/** Object keys that read as identifiers are left unquoted; the rest get JSON quoting. */\nconst TREE_BARE_KEY = /^[A-Za-z_$][A-Za-z0-9_$]*$/;\nfunction treeFormatKey(key: string): string {\n return TREE_BARE_KEY.test(key) ? key : JSON.stringify(key);\n}\n\n/** A scalar JSON value as a single readable token; strings are JSON-quoted, newlines folded. */\nfunction treeScalarPreview(value: PtcJsonValue, maxChars: number): string {\n if (value === null) return \"null\";\n if (typeof value === \"boolean\" || typeof value === \"number\") return String(value);\n if (typeof value === \"string\") {\n const sanitized = sanitizeText(value);\n if (sanitized.length === 0) return '\"\"';\n if (sanitized.includes(\"\\n\")) {\n const flat = sanitized.replace(/\\n/g, \" \");\n const head = truncateChars(flat, Math.max(2, maxChars - 5));\n return JSON.stringify(head) + \"…\";\n }\n if (sanitized.length + 2 <= maxChars) return JSON.stringify(sanitized);\n return JSON.stringify(truncateChars(sanitized, Math.max(2, maxChars - 5))) + \"…\";\n }\n return \"?\";\n}\n\nfunction treeIsScalarJson(value: PtcJsonValue): boolean {\n return value === null || typeof value !== \"object\";\n}\n\n/** Try to render a container as a one-liner bracket form ({k: v} / [v, v]); undefined when it does not fit or children are deep. */\nfunction treeInlinePreview(value: PtcJsonValue, maxChars: number): string | undefined {\n if (maxChars < 8) return undefined;\n if (Array.isArray(value)) {\n if (value.length === 0) return \"[]\";\n if (value.length > 5 || !value.every(treeIsScalarJson)) return undefined;\n const items = value.map((item) =>\n treeScalarPreview(item, Math.max(2, Math.floor(maxChars / 4))),\n );\n const inline = \"[\" + items.join(\", \") + \"]\";\n return inline.length <= maxChars ? inline : undefined;\n }\n if (value === null || typeof value !== \"object\") return undefined;\n const keys = Object.keys(value);\n if (keys.length === 0) return \"{}\";\n if (keys.length > 5 || !Object.values(value).every(treeIsScalarJson)) return undefined;\n const entries = keys.map((key) => {\n const child = (value as Record<string, PtcJsonValue>)[key] ?? null;\n return (\n treeFormatKey(key) + \": \" + treeScalarPreview(child, Math.max(2, Math.floor(maxChars / 4)))\n );\n });\n const inline = \"{\" + entries.join(\", \") + \"}\";\n return inline.length <= maxChars ? inline : undefined;\n}\n\n/**\n * Append one pre-prefixed tree row to out, recursing for containers. Each level passes the\n * continuation indent its children should use; the connector itself encodes \"more siblings\".\n */\nfunction treeAppendNode(\n value: PtcJsonValue,\n label: string, // \"key: \", \"[0] \", or \"\" — the row's own text after the connector\n parentPrefix: string, // page above this row's connector (root's children pass \"\")\n isLast: boolean,\n maxDepth: number,\n maxChildren: number,\n maxLineChars: number,\n depth: number,\n out: string[],\n): void {\n const connector = isLast ? \"└─ \" : \"├─ \";\n const ownPrefix = parentPrefix + connector;\n const continuation = parentPrefix + (isLast ? \" \" : \"│ \");\n\n // Depth guard: collapse to ellipsis and stop recursing.\n if (depth >= maxDepth) {\n out.push(treeTruncateIndent(ownPrefix + label + \"…\", maxLineChars));\n return;\n }\n\n // Empty containers are leaves.\n if (Array.isArray(value) && value.length === 0) {\n out.push(treeTruncateIndent(ownPrefix + label + \"[]\", maxLineChars));\n return;\n }\n if (\n !Array.isArray(value) &&\n value !== null &&\n typeof value === \"object\" &&\n Object.keys(value).length === 0\n ) {\n out.push(treeTruncateIndent(ownPrefix + label + \"{}\", maxLineChars));\n return;\n }\n\n // Scalars.\n if (value === null || typeof value !== \"object\") {\n const budget = Math.max(0, maxLineChars - visibleWidth(ownPrefix) - label.length);\n out.push(\n treeTruncateIndent(ownPrefix + label + treeScalarPreview(value, budget), maxLineChars),\n );\n return;\n }\n\n // Small all-scalar containers collapse to one line.\n const inlineBudget = Math.max(0, maxLineChars - visibleWidth(ownPrefix) - label.length);\n const inline = treeInlinePreview(value, inlineBudget);\n if (inline !== undefined) {\n out.push(treeTruncateIndent(ownPrefix + label + inline, maxLineChars));\n return;\n }\n\n // Larger containers: header row, then recurse.\n const listLike = Array.isArray(value);\n const childCount = listLike ? value.length : Object.keys(value).length;\n let header: string;\n if (listLike) {\n header = \"Array(\" + childCount + \")\";\n } else {\n header =\n childCount > maxChildren\n ? \"{\" + maxChildren + \" of \" + childCount + \"}\"\n : \"{\" + childCount + \" keys}\";\n }\n out.push(treeTruncateIndent(ownPrefix + label + header, maxLineChars));\n\n const limit = Math.min(maxChildren, childCount);\n if (listLike) {\n for (let index = 0; index < limit; index += 1) {\n const isChildLast = index === limit - 1 && childCount <= limit;\n treeAppendNode(\n (value as PtcJsonValue[])[index] ?? null,\n \"[\" + index + \"] \",\n continuation,\n isChildLast,\n maxDepth,\n maxChildren,\n maxLineChars,\n depth + 1,\n out,\n );\n }\n } else {\n const keys = Object.keys(value);\n for (let index = 0; index < limit; index += 1) {\n const isChildLast = index === limit - 1 && childCount <= limit;\n const key = keys[index] as string;\n treeAppendNode(\n (value as Record<string, PtcJsonValue>)[key] ?? null,\n treeFormatKey(key) + \": \",\n continuation,\n isChildLast,\n maxDepth,\n maxChildren,\n maxLineChars,\n depth + 1,\n out,\n );\n }\n }\n if (childCount > limit) {\n const tail = listLike ? \"items\" : \"keys\";\n out.push(\n treeTruncateIndent(\n continuation + \"└─ \" + \"…+\" + (childCount - limit) + \" more \" + tail,\n maxLineChars,\n ),\n );\n }\n}\n\n/** Truncate a rendered row, preserving the leading tree-prefix indent (│/├/└ etc.). */\nfunction treeTruncateIndent(line: string, maxLineChars: number): string {\n if (visibleWidth(line) <= maxLineChars) return line;\n const match = line.match(/^([\\s│├└─]*)/);\n const indent = match && match[1] ? match[1] : \"\";\n const indentWidth = visibleWidth(indent);\n const budget = Math.max(0, maxLineChars - indentWidth - 1);\n let body = \"\";\n let used = 0;\n for (const ch of line.slice(indent.length)) {\n const w = visibleWidth(ch);\n if (used + w > budget) break;\n body += ch;\n used += w;\n }\n return indent + body + \"…\";\n}\n\n/**\n * Render a JSON value as an array of indented tree rows.\n *\n * Each row is a complete prefixed line ready to live under the call row's \" \" indent\n * (TREE_ROOT_CONT): root children start at \"├─ \" / \"└─ \", deeper levels add their own\n * connectors on top of the continuation bar. Caller supplies the row's TREE_ROOT_CONT — a\n * row that starts with \"├─ \" lands at column 3, exactly where other call-row children sit.\n *\n * Empty / scalar values return a one-row preview; non-empty containers recurse.\n */\nexport function renderValueTree(\n value: PtcJsonValue,\n options: {\n maxDepth?: number;\n maxChildren?: number;\n maxLineChars?: number;\n moreAfter?: boolean;\n } = {},\n): string[] {\n const maxDepth = options.maxDepth ?? TREE_VALUE_MAX_DEPTH;\n const maxChildren = options.maxChildren ?? TREE_VALUE_MAX_CHILDREN;\n const maxLineChars = options.maxLineChars ?? TREE_VALUE_MAX_LINE_CHARS;\n // A tree followed by sibling blocks must keep the chain open: its last row stays ├─.\n const moreAfter = options.moreAfter === true;\n if (!isExpandableContainer(value)) {\n // An empty container is not \"expandable\" but still must show its own shape, not the\n // scalar fallback (\"?\").\n if (value !== null && typeof value === \"object\") {\n return [Array.isArray(value) ? \"[]\" : \"{}\"];\n }\n return [treeScalarPreview(value, maxLineChars)];\n }\n const out: string[] = [];\n if (Array.isArray(value)) {\n const total = value.length;\n const limit = Math.min(maxChildren, total);\n for (let index = 0; index < limit; index += 1) {\n const isLast = !moreAfter && index === limit - 1 && total <= limit;\n treeAppendNode(\n value[index] ?? null,\n \"[\" + index + \"] \",\n \"\",\n isLast,\n maxDepth,\n maxChildren,\n maxLineChars,\n 0,\n out,\n );\n }\n if (total > limit) {\n out.push((moreAfter ? \"├─ \" : \"└─ \") + \"…+\" + (total - limit) + \" more items\");\n }\n return out;\n }\n const keys = Object.keys(value);\n const total = keys.length;\n const limit = Math.min(maxChildren, total);\n for (let index = 0; index < limit; index += 1) {\n const isLast = !moreAfter && index === limit - 1 && total <= limit;\n const key = keys[index] as string;\n const childValue = (value as Record<string, PtcJsonValue>)[key] ?? null;\n treeAppendNode(\n childValue,\n treeFormatKey(key) + \": \",\n \"\",\n isLast,\n maxDepth,\n maxChildren,\n maxLineChars,\n 0,\n out,\n );\n }\n if (total > limit) {\n out.push((moreAfter ? \"├─ \" : \"└─ \") + \"…+\" + (total - limit) + \" more keys\");\n }\n return out;\n}\n\n// ---------------------------------------------------------------------------\n// Public API: renderCall / renderResult\n// ---------------------------------------------------------------------------\n\n/** The tool-call arguments the renderer reads (a subset of both tools' parameter objects). */\nexport interface PtcRenderArgs {\n description?: string;\n code?: string;\n script?: string;\n meta?: { name?: string };\n}\n\n/**\n * One-line tool-call header: the tree root, then the tool label, then the model's own description.\n *\n * The description is the only detail that gets to sit next to the label — the previous renderer\n * appended the first code line as well, which is what made the call row read as a paragraph rather\n * than a heading. The code is one `ctrl+e` away in the expanded view.\n */\nexport function renderPtcToolCall(\n args: PtcRenderArgs,\n theme: Theme,\n surface: PtcToolDetails[\"surface\"] = \"run_code\",\n): PtcRow {\n const description =\n (typeof args.description === \"string\" && args.description.trim().length > 0\n ? args.description.trim()\n : args.meta?.name) ?? \"\";\n const fallback = firstMeaningfulCodeLine(args.code ?? args.script ?? \"\");\n const detail = description.length > 0 ? description : (fallback ?? \"(no description)\");\n\n const left = `${theme.fg(\"toolTitle\", theme.bold(TOOL_TITLE[surface]))} ${theme.fg(\"accent\", detail)}`;\n return new PtcRow([{ left: `${TREE_ROOT}${left}` }]);\n}\n\n/** The result shape both tools hand to `renderResult` (a structural subset of `AgentToolResult`). */\nexport interface PtcRenderResult {\n readonly content?: ReadonlyArray<{ type: string; text?: string }>;\n readonly details?: PtcToolDetails;\n}\n\n/** Text body of a failed run — pi surfaces the thrown error in the first text block. */\nfunction errorText(result: PtcRenderResult): string {\n const text = result.content?.find(\n (block) => block.type === \"text\" && (block.text ?? \"\").length > 0,\n );\n return firstLine(text?.text ?? \"\") || \"failed\";\n}\n\n/**\n * The result area under the call row.\n *\n * One of four shapes, all hanging at column 3 so a column of PTC rows stays scannable:\n * - error: one line with the failure reason in the error colour.\n * - no completion value: one line, \"done\".\n * - scalar / empty container: one line, `→ <hint>` (or \"done\" for null).\n * - non-empty container: a tree of rows (recursive `├─/└─/│` connectors from\n * `renderValueTree`), with the right-aligned meta pinned to\n * the tree's first row. ADR-0013 §5.\n *\n * Each row returns its left-side text WITHOUT the `TREE_ROOT_CONT` prefix being in scope;\n * we prepend it here so the same output works for both surfaces.\n */\nfunction resultArea(\n result: PtcRenderResult,\n isError: boolean,\n theme: Theme,\n moreAfter = false,\n): RowLine[] {\n const right = metaText(result.details, theme);\n const attachRight = (row: RowLine): RowLine => (right === undefined ? row : { ...row, right });\n\n if (isError) {\n return [\n attachRight({\n left: `${TREE_ROOT_CONT}${theme.fg(\"error\", `failed: ${errorText(result)}`)}`,\n }),\n ];\n }\n\n const value = result.details?.result;\n if (value === undefined) {\n return [\n attachRight({\n left: `${TREE_ROOT_CONT}${theme.fg(\"muted\", \"done\")}`,\n }),\n ];\n }\n\n if (isExpandableContainer(value)) {\n const tree = renderValueTree(value, {\n maxDepth: TREE_VALUE_MAX_DEPTH,\n maxChildren: TREE_VALUE_MAX_CHILDREN,\n maxLineChars: TREE_VALUE_MAX_LINE_CHARS,\n moreAfter,\n });\n return tree.map((line, index) => {\n const row: RowLine = { left: `${TREE_ROOT_CONT}${line}` };\n return index === 0 && right !== undefined ? { ...row, right } : row;\n });\n }\n\n // Scalar / empty / null — one-line `→ <hint>` (or \"done\" when no hint is producible).\n const hint = resultHint(value);\n if (hint === undefined) {\n return [\n attachRight({\n left: `${TREE_ROOT_CONT}${theme.fg(\"muted\", \"done\")}`,\n }),\n ];\n }\n return [\n attachRight({\n left: `${TREE_ROOT_CONT}${theme.fg(\"dim\", `→ ${hint}`)}`,\n }),\n ];\n}\n\n/**\n * Collapsed result: the result area only.\n *\n * A scalar row fits on one line; a container value expands into a tree (1+ lines). Right-aligned\n * meta (output lines / phases / warnings / duration) is pinned to the first row of the area.\n */\nexport function renderPtcToolResultCollapsed(\n result: PtcRenderResult,\n isError: boolean,\n theme: Theme,\n): PtcRow {\n return new PtcRow(resultArea(result, isError, theme));\n}\n\n/**\n * Expanded view: the result area, then the run's children as labelled blocks.\n *\n * Each child block opens with a fixed-width gutter (`code`, `phases`, `log`, `out`, `warn`,\n * `image`); the `value` block is gone — for a scalar value the result area already shows the\n * hint, and for a container value the area shows the full tree (ADR-0013 §5). Children that\n * overflow cap themselves and report `+N more lines`; the view stays bounded.\n */\nexport function renderPtcToolResultExpanded(\n result: PtcRenderResult,\n args: PtcRenderArgs,\n isError: boolean,\n theme: Theme,\n): PtcRow {\n if (isError) {\n const lines: RowLine[] = resultArea(result, true, theme);\n // The failure text is the reason the reader expanded the row at all: give it the full width\n // under the summary, no tree connectors (it is the error message, not a structured block).\n const text = sanitizeText(\n result.content?.find((block) => block.type === \"text\")?.text ?? errorText(result),\n );\n for (const line of text.split(\"\\n\").slice(0, MAX_LOG_LINES_EXPANDED)) {\n lines.push({ left: `${TREE_ROOT_CONT}${theme.fg(\"error\", line)}` });\n }\n return new PtcRow(lines);\n }\n\n const details = result.details;\n const children: ChildBlock[] = [];\n\n const code = args.code ?? args.script ?? \"\";\n if (code.length > 0) {\n const codeLines = code.split(\"\\n\").map((line) => truncateChars(line, MAX_CODE_LINE_CHARS));\n const shown = codeLines.slice(0, MAX_CODE_LINES_EXPANDED);\n children.push({\n label: \"code\",\n lines: shown,\n ...(codeLines.length > shown.length ? { more: codeLines.length - shown.length } : {}),\n });\n }\n\n if (details !== undefined && details.surface === \"workflow\" && details.phases.length > 0) {\n const shown = details.phases.slice(0, MAX_PHASES_EXPANDED);\n const tail = details.phases.length > shown.length ? \" …\" : \"\";\n children.push({ label: \"phases\", lines: [`${shown.join(\" → \")}${tail}`] });\n }\n\n if (details !== undefined && details.narrations.length > 0) {\n const shown = details.narrations.slice(0, MAX_LOG_LINES_EXPANDED);\n children.push({\n label: \"log\",\n lines: shown.map((message) => sanitizeText(message)),\n ...(details.narrations.length > shown.length\n ? { more: details.narrations.length - shown.length }\n : {}),\n });\n }\n\n if (details !== undefined && details.logs.length > 0) {\n const shown = details.logs.slice(0, MAX_LOG_LINES_EXPANDED);\n children.push({\n label: \"out\",\n lines: shown.map((line) => sanitizeText(line)),\n ...(details.logs.length > shown.length ? { more: details.logs.length - shown.length } : {}),\n });\n }\n\n if (details !== undefined && details.warnings.length > 0) {\n const shown = details.warnings.slice(0, MAX_WARNINGS_EXPANDED);\n children.push({\n label: \"warn\",\n lines: shown.map((warning) => sanitizeText(warning)),\n ...(details.warnings.length > shown.length\n ? { more: details.warnings.length - shown.length }\n : {}),\n });\n }\n\n if (details !== undefined && details.imageCount > 0) {\n const desc =\n details.imageCount === 1 ? \"1 image attached\" : `${details.imageCount} images attached`;\n children.push({ label: \"image\", lines: [desc] });\n }\n\n // Result rows first, then the labelled blocks. `moreAfter` keeps the connector chain open\n // while blocks follow, so the value tree and the blocks read as one list of siblings.\n const lines: RowLine[] = resultArea(result, false, theme, children.length > 0);\n\n for (let index = 0; index < children.length; index += 1) {\n const isLast = index === children.length - 1;\n lines.push(...renderChildBlock(children[index] ?? { label: \"\", lines: [] }, isLast, theme));\n }\n\n return new PtcRow(lines);\n}\n","/**\n * `ptc_run_code` — the unstructured PTC surface.\n *\n * The model writes the body of an async function; inside it, pi's built-in tools are reachable as\n * `tools.<name>(args)` (all seven: read, bash, edit, write, grep, find, ls — ADR-0005's set), and\n * the program's return value plus `console.log` output are what come back. There are no helpers\n * on this surface: `log` / `phase` / `parallel` / `pipeline` belong to `ptc_workflow` (G1 #13 →\n * decision B; a call to a helper that does not exist is a plain `ReferenceError` through the\n * normal code-run error path).\n *\n * Scope notes, so this stays a faithful subset of DSH's `run_code`:\n * - DSH's `sandbox_permissions` / `justification` are deliberately absent — they exist to request\n * a wider sandbox from an approval pipeline this package does not have (ADR-0007 ships no OS\n * sandbox, and the map decision on #8 dropped the approval path).\n * - `timeoutMs: 0` does not disable the deadline; it falls back to `DEFAULT_CONFIG.timeoutMs`,\n * exactly as DSH's resolver does (R1 §3). The description names the default and the ceiling.\n */\nimport { defineTool } from \"@earendil-works/pi-coding-agent\";\nimport type { ToolDefinition } from \"@earendil-works/pi-coding-agent\";\nimport { Type } from \"typebox\";\nimport type { TNumber, TOptional, TObject, TString } from \"typebox\";\nimport { createBuiltinBindings } from \"../runtime/bindings.ts\";\nimport { runPtcProgram } from \"../runtime/dispatcher.ts\";\nimport { DEFAULT_CONFIG } from \"../runtime/limits.ts\";\nimport {\n codeRunFailedError,\n PTC_RUN_CODE_SNIPPET,\n PTC_TOOL_GUIDELINES,\n renderToolResult,\n resolveBindingNames,\n resolveToolCwd,\n} from \"./common.ts\";\nimport type { PtcToolDetails, PtcToolOptions } from \"./common.ts\";\nimport {\n renderPtcToolCall,\n renderPtcToolResultCollapsed,\n renderPtcToolResultExpanded,\n} from \"./render.ts\";\n\nconst DESCRIPTION = [\n \"Run a TypeScript program that composes pi's tools in one shot. Required arguments: `code` —\",\n \"the body of an async function (top-level `return` and `await` work; type annotations are\",\n \"advisory, the code runs type-stripped) — and `description`, a 5-10 word summary of what the\",\n \"program does.\",\n \"\",\n \"Inside the program, call this session's enabled built-in tools as `tools.<name>(args)` — e.g.\",\n '`await tools.read({ path: \"src/index.ts\" })` or `await tools.bash({ command: \"npm test\" })`.',\n \"The bound names mirror the session's active tools (a default session has `read`, `bash`, `edit`,\",\n \"`write`); calling a name that is not bound rejects with an error the program can catch, and\",\n \"independent calls may overlap under `Promise.all`.\",\n \"\",\n \"Only the program's return value and its `console.log` output come back. This surface has no\",\n \"helpers: `log` / `phase` / `parallel` / `pipeline` exist only in `ptc_workflow`.\",\n \"\",\n \"Image-bearing tool results inside the program (a `tools.read` on a PNG, say) are attached to you\",\n \"after the run, so never return image data as the completion value — that only spends your context\",\n \"on base64.\",\n].join(\"\\n\");\n\n/** Schema type written out explicitly for `isolatedDeclarations` (emit must not infer it). */\ntype RunCodeParameters = TObject<{\n code: TString;\n description: TString;\n timeoutMs: TOptional<TNumber>;\n}>;\n\nconst PARAMETERS: RunCodeParameters = Type.Object({\n code: Type.String({\n description: \"The program: the body of an async TypeScript function.\",\n }),\n description: Type.String({\n description:\n \"Clear, concise description of what this program does in active voice, 5-10 words (shown in the UI).\",\n }),\n timeoutMs: Type.Optional(\n Type.Number({\n description:\n `Elapsed deadline in milliseconds for the whole run, including tool calls. ` +\n `Omit it (or pass 0) for the default of ${DEFAULT_CONFIG.timeoutMs / 1000} s; ` +\n `anything above ${DEFAULT_CONFIG.maxTimeoutMs / 1000} s is capped.`,\n }),\n ),\n});\n\n/**\n * Build the `ptc_run_code` tool definition.\n *\n * Called once by the extension factory. Every run gets a fresh worker and a fresh binding table\n * built against the run's own cwd, so two concurrent calls cannot share state.\n */\nexport function createPtcRunCodeTool(\n options: PtcToolOptions = {},\n): ToolDefinition<RunCodeParameters, PtcToolDetails> {\n return defineTool({\n name: \"ptc_run_code\",\n label: \"PTC Run Code\",\n description: DESCRIPTION,\n promptSnippet: PTC_RUN_CODE_SNIPPET,\n promptGuidelines: [...PTC_TOOL_GUIDELINES],\n parameters: PARAMETERS,\n async execute(_toolCallId, params, signal, _onUpdate, ctx) {\n const cwd = resolveToolCwd(ctx);\n const names = resolveBindingNames(options.getBindingSourceNames?.());\n const startedAt = Date.now();\n const outcome = await runPtcProgram({\n code: params.code,\n surface: \"run_code\",\n cwd,\n bindings: createBuiltinBindings({ cwd, names }),\n ...(params.timeoutMs === undefined ? {} : { timeoutMs: params.timeoutMs }),\n ...(signal === undefined ? {} : { signal }),\n ...(options.config === undefined ? {} : { config: options.config }),\n });\n if (outcome.error !== undefined) throw codeRunFailedError(outcome);\n return renderToolResult({ outcome, surface: \"run_code\", durationMs: Date.now() - startedAt });\n },\n\n // Compact TUI rendering — see `render.ts` and ADR-0013. `renderShell` stays at the default\n // `ToolExecutionComponent` shell so PTC rows match the visual rhythm of `read`/`bash`.\n renderCall(args, theme) {\n return renderPtcToolCall(args, theme, \"run_code\");\n },\n renderResult(result, options, theme, context) {\n if (options.expanded) {\n return renderPtcToolResultExpanded(result, context.args, context.isError, theme);\n }\n return renderPtcToolResultCollapsed(result, context.isError, theme);\n },\n });\n}\n","/**\n * `ptc_workflow` — the structured PTC surface.\n *\n * Same worker, same bindings, same Node surface as `ptc_run_code`; what it adds is a plan\n * (`meta` with an ordered phase list), plain-JSON input (`args`, bound as the program's `args`\n * global) and the four workflow helpers: `log` / `phase` / `parallel` / `pipeline` (G1 #13 →\n * decision B — there is no `agent()`, not even a stub).\n *\n * Three decisions recorded here rather than re-litigated per call:\n * - **Language**: the same type-stripping pipeline as `ptc_run_code`. The worker compiles both\n * surfaces through one `compileProgram`, and the pipeline is type-*strip* only — it adds no\n * runtime semantics that a workflow could not tolerate. There is no JS-only mode because there\n * is no concrete reason for one (the old pi-ptc \"JS-only\" idea is explicitly not inherited).\n * - **Phase strictness**: warning, not error. A `phase(title)` outside `meta.phases` is a plan\n * drift — usually a typo or a forgotten declaration — but the run has already happened by the\n * time the titles are known, and failing then would discard a completed result over a cosmetic\n * mismatch. Each distinct unlisted title is reported once, as a `Warning:` line in `content`\n * and in `details.warnings`. When `meta.phases` is absent there is no declared plan to drift\n * from, so nothing is reported.\n * - **`args`**: validated as plain JSON before dispatch (see `validateWorkflowArgs`), because a\n * function/symbol/`undefined`/cycle would otherwise be silently dropped or mangled by the\n * structured clone on its way to the worker.\n */\nimport { defineTool } from \"@earendil-works/pi-coding-agent\";\nimport type { ToolDefinition } from \"@earendil-works/pi-coding-agent\";\nimport { Type } from \"typebox\";\nimport type { Static, TArray, TObject, TOptional, TRecord, TString, TUnknown } from \"typebox\";\nimport { createBuiltinBindings } from \"../runtime/bindings.ts\";\nimport { runPtcProgram } from \"../runtime/dispatcher.ts\";\nimport type { PtcRunOutcome } from \"../runtime/dispatcher.ts\";\nimport {\n codeRunFailedError,\n PTC_TOOL_GUIDELINES,\n PTC_WORKFLOW_SNIPPET,\n renderToolResult,\n resolveBindingNames,\n resolveToolCwd,\n} from \"./common.ts\";\nimport type { PtcToolDetails, PtcToolOptions } from \"./common.ts\";\nimport {\n renderPtcToolCall,\n renderPtcToolResultCollapsed,\n renderPtcToolResultExpanded,\n} from \"./render.ts\";\n\nconst DESCRIPTION = [\n \"Run a structured TypeScript workflow: a named plan that reports phases and narration as it\",\n \"goes. `script` is the body of an async function, `meta` names it (`{ name, description, phases? }`,\",\n \"where `phases` is the ordered plan), and optional `args` is plain-JSON input bound to the\",\n \"program's `args` global.\",\n \"\",\n \"Helpers inside the script: `log(message)` narrates progress; `phase(title)` frames the workflow\",\n \"(titles outside `meta.phases` are reported as warnings); `parallel(thunks)` runs thunks\",\n \"concurrently, a failed item becoming `null`; `pipeline(items, ...stages)` chains stages per item\",\n \"with the same per-item `null` on failure. There is no `agent()` helper.\",\n \"\",\n \"Tools are reachable as `tools.<name>(args)` exactly as in `ptc_run_code`, and the bound names\",\n \"mirror the session's enabled tools. What comes back is the script's return value, its\",\n \"`log`/`phase` narration and its `console.log` output.\",\n \"\",\n \"Image-bearing tool results inside the script are attached to you after the run — never return\",\n \"image data as the script's value.\",\n].join(\"\\n\");\n\n/** Schema types written out explicitly for `isolatedDeclarations` (emit must not infer them). */\ntype WorkflowPhaseSchema = TObject<{ name: TString }>;\n\ntype WorkflowParameters = TObject<{\n meta: TObject<{\n name: TString;\n description: TString;\n phases: TOptional<TArray<WorkflowPhaseSchema>>;\n }>;\n script: TString;\n args: TOptional<TRecord<string, TUnknown>>;\n}>;\n\nconst PARAMETERS: WorkflowParameters = Type.Object({\n meta: Type.Object(\n {\n name: Type.String({ description: \"Short workflow name, shown in the UI.\" }),\n description: Type.String({ description: \"What this workflow does, in active voice.\" }),\n phases: Type.Optional(\n Type.Array(\n Type.Object({\n name: Type.String({ description: \"Phase title, matched against phase(title) calls.\" }),\n }),\n {\n description:\n \"Ordered plan of the workflow's phases. phase() titles outside this list are reported as warnings.\",\n },\n ),\n ),\n },\n { description: \"Workflow metadata: what this run is and which phases it declares.\" },\n ),\n script: Type.String({ description: \"The program: the body of an async TypeScript function.\" }),\n args: Type.Optional(\n Type.Record(Type.String(), Type.Unknown(), {\n description:\n \"Plain-JSON input bound to the program's `args` global. No functions, symbols, undefined values or cycles.\",\n }),\n ),\n});\n\n/** Parameters after pi's schema validation; `args` is still unvalidated plain JSON to be checked. */\nexport type PtcWorkflowParams = Static<typeof PARAMETERS>;\n\nfunction describeValue(value: unknown): string {\n if (value === null) return \"null\";\n if (Array.isArray(value)) return \"an array\";\n return `a ${typeof value}`;\n}\n\n/**\n * Reject anything that is not plain JSON, with a path-qualified reason.\n *\n * The rule is the worker's own result rule (`toJsonValue`) applied one direction earlier, so both\n * ends agree on what \"plain JSON\" means: objects with `Object.prototype` (or null), arrays,\n * strings, finite numbers, booleans and null. Class instances, functions, symbols, bigints,\n * non-finite numbers, circular references and `undefined` values are all rejected — `undefined`\n * because the ticket calls it out and because a structured clone would drop it silently, leaving\n * the script reading a key that is not there.\n */\nexport function validateWorkflowArgs(args: unknown): void {\n if (args === undefined) return;\n if (typeof args !== \"object\" || args === null || Array.isArray(args)) {\n throw new TypeError(\n `ptc_workflow args must be a plain JSON object, received ${describeValue(args)}`,\n );\n }\n for (const key of Object.keys(args)) {\n const problem = findJsonProblem(\n (args as Record<string, unknown>)[key],\n `args.${key}`,\n new Set(),\n );\n if (problem !== undefined)\n throw new TypeError(`ptc_workflow args must be plain JSON: ${problem}`);\n }\n}\n\n/** Path-qualified reason a value cannot cross the wire as plain JSON, or `undefined` when it can. */\nfunction findJsonProblem(value: unknown, path: string, seen: Set<object>): string | undefined {\n if (value === null || typeof value === \"string\" || typeof value === \"boolean\") return undefined;\n if (typeof value === \"undefined\") return `${path} is undefined`;\n if (typeof value === \"number\") {\n return Number.isFinite(value)\n ? undefined\n : `${path} is ${String(value)}, which is not representable as JSON`;\n }\n if (typeof value !== \"object\") return `${path} is ${describeValue(value)}`;\n\n const container = value as object;\n if (seen.has(container)) return `${path} is a circular reference`;\n seen.add(container);\n try {\n if (Array.isArray(container)) {\n for (let index = 0; index < container.length; index += 1) {\n const problem = findJsonProblem(container[index], `${path}[${index}]`, seen);\n if (problem !== undefined) return problem;\n }\n return undefined;\n }\n const prototype = Object.getPrototypeOf(container);\n if (prototype !== Object.prototype && prototype !== null) {\n const name = (container as { constructor?: { name?: string } }).constructor?.name;\n return `${path} is a ${typeof name === \"string\" && name.length > 0 ? name : \"non-plain object\"}, not plain JSON`;\n }\n for (const key of Object.keys(container)) {\n const problem = findJsonProblem(\n (container as Record<string, unknown>)[key],\n `${path}.${key}`,\n seen,\n );\n if (problem !== undefined) return problem;\n }\n return undefined;\n } finally {\n seen.delete(container);\n }\n}\n\n/**\n * Collect the plan-drift warnings for one workflow run: distinct `phase()` titles the declared\n * `meta.phases` list does not mention, in first-seen order.\n */\nexport function unlistedPhaseWarnings(\n outcome: PtcRunOutcome,\n declared: readonly string[] | undefined,\n): string[] {\n if (declared === undefined || declared.length === 0) return [];\n const known = new Set(declared);\n const reported = new Set<string>();\n const warnings: string[] = [];\n for (const title of outcome.phases) {\n if (known.has(title) || reported.has(title)) continue;\n reported.add(title);\n warnings.push(\n `phase \"${title}\" is not listed in meta.phases (declared: ${[...known].join(\", \")})`,\n );\n }\n return warnings;\n}\n\n/**\n * Build the `ptc_workflow` tool definition.\n *\n * `args` is validated before `runPtcProgram` is called, so a malformed payload never spawns a\n * worker. Everything after dispatch is identical to `ptc_run_code`, plus the phase roll-up.\n */\nexport function createPtcWorkflowTool(\n options: PtcToolOptions = {},\n): ToolDefinition<WorkflowParameters, PtcToolDetails> {\n return defineTool({\n name: \"ptc_workflow\",\n label: \"PTC Workflow\",\n description: DESCRIPTION,\n promptSnippet: PTC_WORKFLOW_SNIPPET,\n promptGuidelines: [...PTC_TOOL_GUIDELINES],\n parameters: PARAMETERS,\n async execute(_toolCallId, params, signal, _onUpdate, ctx) {\n validateWorkflowArgs(params.args);\n const cwd = resolveToolCwd(ctx);\n const names = resolveBindingNames(options.getBindingSourceNames?.());\n const startedAt = Date.now();\n const outcome = await runPtcProgram({\n code: params.script,\n surface: \"workflow\",\n cwd,\n bindings: createBuiltinBindings({ cwd, names }),\n ...(params.args === undefined ? {} : { args: params.args }),\n ...(signal === undefined ? {} : { signal }),\n ...(options.config === undefined ? {} : { config: options.config }),\n });\n if (outcome.error !== undefined) throw codeRunFailedError(outcome);\n const declared = params.meta.phases?.map((phase) => phase.name);\n return renderToolResult({\n outcome,\n surface: \"workflow\",\n warnings: unlistedPhaseWarnings(outcome, declared),\n durationMs: Date.now() - startedAt,\n });\n },\n\n // Same renderer as `ptc_run_code`; the `workflow` label tells the two rows apart, the collapsed\n // meta adds a phase count, and the expanded view puts the phases roll-up first so a reader sees\n // the workflow plan up top.\n renderCall(args, theme) {\n return renderPtcToolCall(args, theme, \"workflow\");\n },\n renderResult(result, options, theme, context) {\n if (options.expanded) {\n return renderPtcToolResultExpanded(result, context.args, context.isError, theme);\n }\n return renderPtcToolResultCollapsed(result, context.isError, theme);\n },\n });\n}\n","/**\n * PTC default mode — the session-level tool-restriction state machine.\n *\n * ## What \"mode\" means here\n *\n * While enabled, the session's visible tool loadout is narrowed so the model reaches\n * `read` / `bash` / `edit` / `write` / `grep` / `find` / `ls` only from *inside* a program\n * (`tools.<name>(args)`). That is DSH's PTC-preset shape, and it is what makes the mode visible\n * in the TUI: the transcript stops showing file-operation rows and shows PTC rows instead.\n *\n * The mode is decided per session (`session_start`), announced once, and always reversible.\n *\n * ## Why bindings need a base snapshot (the trap this module exists to avoid)\n *\n * `resolveBindingNames()` computes bindings as `BUILTIN_BINDING_NAMES ∩ pi.getActiveTools()`\n * (T7, ADR-0005 addendum). But `pi.getActiveTools()` reads the *live* loadout — the very set this\n * mode narrows. Hiding the built-ins therefore empties the binding table, and every\n * `tools.read(...)` fails with \"not bound\": the extension would neuter itself.\n *\n * The fix is a **base snapshot**. `base` is captured *before* the mode hides anything, and\n * bindings derive from `base` rather than the live loadout while the mode is on. T7's invariant\n * survives, because `base` is itself read from `pi.getActiveTools()`: a session launched with\n * `--tools read` has `base = [\"read\"]`, so bindings stay `[\"read\"]`. The mode never widens access\n * beyond what the session was launched with — it only makes its **own** narrowing transparent to\n * the binding table.\n *\n * ## Fail-safe on external loadout changes\n *\n * Another extension can call `setActiveTools()` (pi's own `preset.ts` and `tools.ts` examples do).\n * `ourLoadout` records the exact array this mode wrote, so a mismatch means someone else owns the\n * loadout now. The mode then exits and restores `base` instead of fighting over it.\n *\n * ## Where the mode never runs\n *\n * Print / JSON / RPC sessions (`ctx.mode !== \"tui\"`) are left alone: forcing the shape on CI\n * scripts buys nothing and breaks them. A session that was *launched* with an explicit tool\n * restriction is left alone too — see `decideModeEntry`.\n */\nimport { readFileSync } from \"node:fs\";\nimport { join } from \"node:path\";\nimport { BUILTIN_BINDING_NAMES } from \"../runtime/bindings.ts\";\n\n/** The two surfaces this package exposes. `/ptc on` needs at least one of them active. */\nexport const PTC_MODE_TOOL_NAMES: readonly string[] = [\"ptc_run_code\", \"ptc_workflow\"];\n\n/**\n * Tools that must all be present for the **automatic** entry path to proceed.\n *\n * These four are pi's default session surface (`agent-session.js`: \"[\"read\", \"bash\", \"edit\",\n * \"write\"]\"), so \"all four present\" means \"this session was not narrowed\". Comparing against\n * `BUILTIN_BINDING_NAMES` instead would be wrong: that list is the set of names that *can* be\n * bound, not what a default session has (`grep` / `find` / `ls` are bindable but off by default),\n * so requiring all seven would misread every ordinary session as restricted and never turn on.\n */\nexport const MODE_REQUIRED_TOOL_NAMES: readonly string[] = [\"read\", \"bash\", \"edit\", \"write\"];\n\n/** `customType` for the persisted mode entry (`pi.appendEntry`). */\nexport const PTC_MODE_ENTRY_TYPE = \"ptc-mode\";\n\n/** Footer status key (`ctx.ui.setStatus`). */\nexport const PTC_MODE_STATUS_KEY = \"ptc-mode\";\n\n/** Config file name, resolved against pi's agent dir (`~/.pi/agent/`). */\nexport const PTC_MODE_CONFIG_FILE = \"ptc.json\";\n\n/** How much of the loadout the mode hides. */\nexport type ModeHideStrategy = \"builtins-only\" | \"all-but-ptc\";\n\n/**\n * Which tools the mode hides while it is on.\n *\n * - `\"builtins-only\"` (default) — hide the seven built-in tools; tools contributed by *other*\n * extensions (`web_search`, `todo`, `subagent`, …) stay visible and directly callable. The\n * model is forced to program for file and shell work, which is the bulk of a coding session,\n * without losing capabilities this package cannot re-expose: extension tools have no bindings\n * (`pi.getAllTools()` returns metadata only — no `execute`), so hiding one makes it\n * unreachable for the whole session.\n * - `\"all-but-ptc\"` — hide everything except the two PTC surfaces. Closest to a strict reading of\n * DSH's preset, at the cost of making other extensions' tools unreachable until `/ptc off`.\n */\nexport const DEFAULT_HIDE_STRATEGY: ModeHideStrategy = \"builtins-only\";\n\n/** Live mode state. `base` / `ourLoadout` are `undefined` while the mode is off. */\nexport interface PtcModeState {\n enabled: boolean;\n /** Loadout captured before the mode narrowed anything; bindings derive from this. */\n base: readonly string[] | undefined;\n /** Exactly the array handed to `setActiveTools`; a mismatch means someone else changed it. */\n ourLoadout: readonly string[] | undefined;\n}\n\n/** Fresh, disabled state. */\nexport function initialModeState(): PtcModeState {\n return { enabled: false, base: undefined, ourLoadout: undefined };\n}\n\n/**\n * A persisted mode record lives in the session (`pi.appendEntry`), so `/ptc off` survives a\n * resume and a `/reload` can put the loadout back the way it found it.\n */\nexport interface PersistedModeState {\n enabled: boolean;\n base?: readonly string[];\n}\n\n// --------------------------------------------------------------------------------------\n// Config: ~/.pi/agent/ptc.json → { \"defaultMode\": false }\n// --------------------------------------------------------------------------------------\n\n/** Result of reading the opt-out config, with enough detail to warn about a broken file. */\nexport interface DefaultModeConfig {\n defaultMode: boolean;\n source: \"file\" | \"default\" | \"invalid\";\n error?: string;\n}\n\n/**\n * Read `defaultMode` from the agent-dir config file.\n *\n * Absent file → on (this package is default-on by design; see CONTEXT.md). A present file with a\n * non-boolean `defaultMode`, or unparseable JSON, is reported as `invalid` **and still defaults to\n * on** — the caller should surface the problem rather than silently changing behavior.\n */\nexport function readDefaultModeConfig(agentDir: string): DefaultModeConfig {\n const path = join(agentDir, PTC_MODE_CONFIG_FILE);\n let raw: string;\n try {\n raw = readFileSync(path, \"utf8\");\n } catch {\n return { defaultMode: true, source: \"default\" };\n }\n let parsed: unknown;\n try {\n parsed = JSON.parse(raw);\n } catch (error) {\n return {\n defaultMode: true,\n source: \"invalid\",\n error: `${path} is not valid JSON (${error instanceof Error ? error.message : String(error)})`,\n };\n }\n if (parsed === null || typeof parsed !== \"object\" || Array.isArray(parsed)) {\n return { defaultMode: true, source: \"invalid\", error: `${path} must contain a JSON object` };\n }\n const value = (parsed as { defaultMode?: unknown }).defaultMode;\n if (value === undefined) {\n return { defaultMode: true, source: \"default\" };\n }\n if (typeof value !== \"boolean\") {\n return {\n defaultMode: true,\n source: \"invalid\",\n error: `${path}: \"defaultMode\" must be a boolean, received ${typeof value}`,\n };\n }\n return { defaultMode: value, source: \"file\" };\n}\n\n// --------------------------------------------------------------------------------------\n// Entry decision\n// --------------------------------------------------------------------------------------\n\n/** Why the mode declined to turn on. Surfaced in the entry notification / debug logs. */\nexport type ModeBlockReason = \"not-tui\" | \"config-off\" | \"tools-unavailable\" | \"restricted-session\";\n\n/** Either the loadout to apply, or the reason the mode stayed off. */\nexport type ModeEntryDecision =\n | { enter: true; base: readonly string[]; loadout: readonly string[] }\n | { enter: false; reason: ModeBlockReason };\n\n/** Inputs to the entry decision. `active` is `pi.getActiveTools()` at decision time. */\nexport interface ModeEntryInput {\n /** `ctx.mode` — only `\"tui\"` participates. */\n mode: string;\n /** Resolved `defaultMode` from the config file. */\n defaultMode: boolean;\n /** The session's current tool loadout. */\n active: readonly string[];\n /** True for `/ptc on`, which overrides the config and the restricted-session policy. */\n manual: boolean;\n /** Overrides `DEFAULT_HIDE_STRATEGY` (tests, or a future settings surface). */\n hide?: ModeHideStrategy | undefined;\n}\n\n/** Normalize to a de-duplicated name list, preserving order. */\nfunction uniqueNames(names: readonly string[]): string[] {\n return [...new Set(names)];\n}\n\n/** Order- and duplicate-insensitive set equality, for loadout comparison. */\nexport function sameToolSet(a: readonly string[], b: readonly string[]): boolean {\n if (a.length !== b.length) return false;\n const left = [...a].sort();\n const right = [...b].sort();\n return left.every((name, index) => name === right[index]);\n}\n\n/**\n * The loadout applied while the mode is on, given the session's current one.\n *\n * Always keeps the PTC surfaces; what else survives depends on the hide strategy (see\n * `DEFAULT_HIDE_STRATEGY`). Order follows `active`, so the visible list reads the way the session\n * was configured.\n */\nexport function modeLoadout(active: readonly string[], hide: ModeHideStrategy): string[] {\n const isPtc = (name: string): boolean => PTC_MODE_TOOL_NAMES.includes(name);\n if (hide === \"all-but-ptc\") return uniqueNames(active.filter(isPtc));\n const builtins: ReadonlySet<string> = new Set(BUILTIN_BINDING_NAMES);\n return uniqueNames(active.filter((name) => isPtc(name) || !builtins.has(name)));\n}\n\n/**\n * Decide whether the mode turns on for this session.\n *\n * The policy, in order:\n * 1. **TUI only** — print / JSON / RPC sessions are never touched.\n * 2. **Config** — `{\"defaultMode\": false}` opts out; `/ptc on` overrides it.\n * 3. **PTC tools must exist** — if this session has them disabled, there is nothing to run.\n * 4. **Explicit restriction wins** — a session launched with `--tools` / `--exclude-tools` /\n * `--no-builtin-tools` that is missing any of `MODE_REQUIRED_TOOL_NAMES` (pi's default four)\n * stays as launched. Narrowing *further* would be the extension overriding a deliberate user\n * instruction, and the PTC surface would be degraded anyway (bindings = `BUILTIN ∩ base`).\n * `/ptc on` overrides this: the manual path enters with the restricted loadout as `base`, so\n * bindings stay inside the user's allowlist.\n */\nexport function decideModeEntry(input: ModeEntryInput): ModeEntryDecision {\n const { mode, defaultMode, active, manual } = input;\n if (mode !== \"tui\") return { enter: false, reason: \"not-tui\" };\n if (!defaultMode && !manual) return { enter: false, reason: \"config-off\" };\n if (!PTC_MODE_TOOL_NAMES.some((name) => active.includes(name))) {\n return { enter: false, reason: \"tools-unavailable\" };\n }\n if (!manual) {\n const missing = MODE_REQUIRED_TOOL_NAMES.filter((name) => !active.includes(name));\n if (missing.length > 0) return { enter: false, reason: \"restricted-session\" };\n }\n return {\n enter: true,\n base: uniqueNames(active),\n loadout: modeLoadout(active, input.hide ?? DEFAULT_HIDE_STRATEGY),\n };\n}\n\n// --------------------------------------------------------------------------------------\n// Runtime helpers\n// --------------------------------------------------------------------------------------\n\n/**\n * Tool names the binding table should be built from.\n *\n * While the mode is on this is the base snapshot (so the built-ins the mode just hid remain\n * callable from inside a program); otherwise it is simply the live loadout, i.e. T7's rule.\n */\nexport function bindingSource(state: PtcModeState, active: readonly string[]): readonly string[] {\n return state.enabled && state.base !== undefined ? state.base : active;\n}\n\n/**\n * Whether the live loadout is no longer the one this mode wrote — i.e. another extension (or the\n * user) took ownership of the tool set. Always false while the mode is off.\n */\nexport function detectExternalLoadoutChange(\n state: PtcModeState,\n active: readonly string[],\n): boolean {\n if (!state.enabled || state.ourLoadout === undefined) return false;\n return !sameToolSet(state.ourLoadout, active);\n}\n\n/**\n * Reconstruct the base loadout on `session_start`.\n *\n * Two shapes are possible:\n * - The session is still narrowed from a previous run of this mode (a `/reload`, or a resumed\n * session): the live loadout is a subset of the PTC tools and the persisted record says the mode\n * was on. The persisted `base` is then authoritative — but only to *undo our own hiding*.\n * - Otherwise the live loadout is whatever this pi process was launched with, and it wins. That is\n * what keeps a new `--tools …` choice from being overridden by an older session's snapshot.\n */\nexport function resolveBaseOnStart(\n persisted: PersistedModeState | undefined,\n active: readonly string[],\n): { base: readonly string[]; restoreFirst: boolean } {\n const looksNarrowedByUs =\n active.length > 0 && active.every((name) => PTC_MODE_TOOL_NAMES.includes(name));\n if (persisted?.enabled === true && looksNarrowedByUs && persisted.base !== undefined) {\n return { base: persisted.base, restoreFirst: true };\n }\n return { base: active, restoreFirst: false };\n}\n\n// --------------------------------------------------------------------------------------\n// Instruction injected into the conversation once per mode entry\n// --------------------------------------------------------------------------------------\n\n/**\n * Build the system-visible briefing for the mode.\n *\n * Generated from the *actual* binding list rather than hardcoded, because bindings vary by session\n * (`base` is whatever the session was launched with) and a stale list would have the model calling\n * names that are not bound.\n */\nexport function buildModeInstruction(bindings: readonly string[], hide: ModeHideStrategy): string {\n const builtinNote =\n hide === \"all-but-ptc\"\n ? \"Every built-in tool is hidden from you, and so are other extensions' tools.\"\n : \"The built-in tools (read, bash, edit, write, grep, find, ls) are hidden from you.\";\n const callable = bindings.map((name) => `tools.${name}(args)`);\n const examples = bindings\n .slice(0, 2)\n .map((name) =>\n name === \"read\"\n ? 'await tools.read({ path: \"src/index.ts\" })'\n : name === \"bash\"\n ? 'await tools.bash({ command: \"pnpm test\" })'\n : `await tools.${name}({ /* … */ })`,\n )\n .join(\"\\n\");\n\n return [\n \"[PTC MODE ACTIVE]\",\n \"This session runs in PTC mode. \" + builtinNote,\n \"\",\n \"Work by writing one TypeScript program per task and running it:\",\n \" - ptc_run_code for a plain program (its return value and console.log come back to you)\",\n \" - ptc_workflow when the task has named phases and you want narration (log / phase /\",\n \" parallel / pipeline are available there)\",\n \"\",\n `The program body is an async function. Available inside it: ${callable.join(\", \")}.`,\n examples.length > 0 ? `Examples:\\n${examples}` : \"\",\n \"\",\n \"Batch a task's steps into ONE program instead of issuing many tool calls — gathering\",\n \"intermediate output yourself and returning only what matters is the point of this mode.\",\n \"Image-bearing tool results inside a program are attached to you after the run — never return\",\n \"base64 image data as your program's value.\",\n \"If the PTC surfaces cannot express what you need, say so and the user can run /ptc off.\",\n ]\n .filter((line) => line.length > 0)\n .join(\"\\n\");\n}\n","/**\n * The system-prompt skills section, restored for PTC mode.\n *\n * pi advertises skills only when the session can actually open one:\n *\n * const skillFileReadTool = [\"read\", \"bash\"].find((tool) => selectedTools.includes(tool));\n * if (skillFileReadTool && skills.length > 0) promptSections.skills = ...\n * (pi-coding-agent: dist/core/system-prompt.js)\n *\n * PTC mode hides exactly those two tools, so that gate drops the section — and with it the model's\n * only way to learn that skills exist. Measured on real session files: sessions before\n * PTC-mode-by-default carried 43 skills in `SystemMessage.sections.skills`; PTC sessions carried\n * no `skills` section at all. See ADR-0011.\n *\n * The list body stays pi's own — `formatSkillsForPrompt` is a public export and its XML shape is\n * the agentskills.io spec, so re-deriving it here would only create drift. Only the loading\n * sentence changes: \"Use the read tool\" is false in a session where `read` is hidden, and the way\n * in is `tools.read` from inside a program.\n */\nimport { formatSkillsForPrompt } from \"@earendil-works/pi-coding-agent\";\nimport type { Skill } from \"@earendil-works/pi-coding-agent\";\n\n/** The two tools pi accepts as \"this session can read a skill file\". */\nexport const SKILL_READING_TOOL_NAMES: readonly string[] = [\"read\", \"bash\"];\n\n/** pi's sentence, which names a tool PTC mode hides. */\nconst PI_LOAD_INSTRUCTION =\n \"Use the read tool to load a skill's file when the task matches its description.\";\n\n/** What replaces it: the same instruction, in the only call form this session has. */\nexport const PTC_SKILL_LOAD_INSTRUCTION =\n \"Use tools.read({ path }) inside a ptc_run_code program to load a skill's file when the task matches its description.\";\n\n/**\n * Would pi withhold the skills section for this loadout?\n *\n * Mirrors the gate rather than guessing: it is true exactly when neither `read` nor `bash` is\n * directly callable, which is the state the mode deliberately puts an ordinary session in.\n */\nexport function skillsSectionDropped(visibleTools: readonly string[]): boolean {\n return !SKILL_READING_TOOL_NAMES.some((name) => visibleTools.includes(name));\n}\n\n/**\n * Build the `skills` section body (pi wraps it in `<skills>…</skills>` itself).\n *\n * Returns `\"\"` when nothing is advertisable — pi's formatter filters out skills marked\n * `disable-model-invocation`, which is the correct behaviour to inherit: those are\n * `/skill:name`-only by design.\n *\n * `format` is injectable so a test can drive the reworded-header branch without patching pi.\n */\nexport function buildPtcSkillsSection(\n skills: readonly Skill[],\n format: (skills: Skill[], fileReadTool?: \"read\" | \"bash\") => string = formatSkillsForPrompt,\n): string {\n const section = format([...skills], \"read\").trim();\n if (section.length === 0) return \"\";\n if (section.includes(PI_LOAD_INSTRUCTION)) {\n return section.replace(PI_LOAD_INSTRUCTION, PTC_SKILL_LOAD_INSTRUCTION);\n }\n // pi reworded its header. Keep the list, but never inherit a sentence that names a tool this\n // session cannot call — a wrong instruction is worse than a redundant one.\n return `${PTC_SKILL_LOAD_INSTRUCTION}\\n\\n${section}`;\n}\n","/**\n * pi-ptc-subagents — DSH PTC mode (Programmable Tool Calling) for pi.\n *\n * This module is the extension entrypoint pi loads (`package.json` → `pi.extensions`).\n * The PTC machinery it exposes is complete and tested (T3): wire protocol, dispatcher,\n * bindings and the two worker surfaces. On top of it sit the two model-facing tools:\n *\n * - `ptc_run_code` — bindings + Node + `console.log`, program's return value is the result\n * - `ptc_workflow` — the same plus `log` / `phase` / `parallel` / `pipeline` and `args`\n *\n * There is no `agent()` helper on either surface (G1 #13 → decision B): calling it produces\n * a plain `ReferenceError` through the normal code-run error path.\n */\nimport { getAgentDir } from \"@earendil-works/pi-coding-agent\";\nimport type {\n ExtensionAPI,\n ExtensionContext,\n NormalizedBuildSystemPromptOptions,\n} from \"@earendil-works/pi-coding-agent\";\nimport { createPtcRunCodeTool } from \"./tools/run-code.ts\";\nimport { createPtcWorkflowTool } from \"./tools/workflow.ts\";\nimport { resolveBindingNames } from \"./tools/common.ts\";\nimport {\n bindingSource,\n buildModeInstruction,\n decideModeEntry,\n DEFAULT_HIDE_STRATEGY,\n detectExternalLoadoutChange,\n initialModeState,\n PTC_MODE_ENTRY_TYPE,\n PTC_MODE_STATUS_KEY,\n readDefaultModeConfig,\n resolveBaseOnStart,\n} from \"./mode/ptc-mode.ts\";\nimport type { ModeHideStrategy, PersistedModeState } from \"./mode/ptc-mode.ts\";\nimport { buildPtcSkillsSection, skillsSectionDropped } from \"./mode/skills-section.ts\";\n\nexport {\n bindingSource,\n buildModeInstruction,\n decideModeEntry,\n DEFAULT_HIDE_STRATEGY,\n detectExternalLoadoutChange,\n initialModeState,\n modeLoadout,\n MODE_REQUIRED_TOOL_NAMES,\n PTC_MODE_CONFIG_FILE,\n PTC_MODE_ENTRY_TYPE,\n PTC_MODE_STATUS_KEY,\n PTC_MODE_TOOL_NAMES,\n readDefaultModeConfig,\n resolveBaseOnStart,\n sameToolSet,\n} from \"./mode/ptc-mode.ts\";\nexport type {\n DefaultModeConfig,\n ModeBlockReason,\n ModeEntryDecision,\n ModeEntryInput,\n ModeHideStrategy,\n PersistedModeState,\n PtcModeState,\n} from \"./mode/ptc-mode.ts\";\nexport {\n buildPtcSkillsSection,\n PTC_SKILL_LOAD_INSTRUCTION,\n SKILL_READING_TOOL_NAMES,\n skillsSectionDropped,\n} from \"./mode/skills-section.ts\";\nexport { MAX_LINE_CHARS, renderModelValue, sanitizeText, stripAnsi } from \"./tools/text.ts\";\n/** Footer status label shown while the mode is on. */\nconst MODE_STATUS_TEXT = \"PTC\";\n\n/**\n * Which tools the mode hides. The shipped extension always uses the default; it is a named\n * constant so a future settings surface (or a test) has one place to change it.\n */\nconst MODE_HIDE_STRATEGY: ModeHideStrategy = DEFAULT_HIDE_STRATEGY;\n\n/**\n * What to tell the user when the mode turns on. Names what became unreachable, because that is the\n * part that surprises people — and the binding list, because that is the part they need in order\n * to know what a program can still do.\n */\nfunction describeEntry(base: readonly string[], hide: ModeHideStrategy): string {\n const hidden =\n hide === \"all-but-ptc\"\n ? \"all other tools\"\n : \"built-in tools (now reachable only as tools.<name> inside a program)\";\n return [\n `PTC mode on — ${hidden} hidden.`,\n `Bindings available inside a program: ${resolveBindingNames(base).join(\", \") || \"(none)\"}.`,\n \"Run /ptc off to restore the normal tool set.\",\n ].join(\"\\n\");\n}\n\nexport {\n BUILTIN_BINDING_NAMES,\n createBuiltinBindings,\n DEFAULT_BINDING_NAMES,\n} from \"./runtime/bindings.ts\";\nexport type {\n Binding,\n BindingContext,\n BindingTable,\n CreateBuiltinBindingsOptions,\n} from \"./runtime/bindings.ts\";\nexport { runPtcProgram } from \"./runtime/dispatcher.ts\";\nexport type { PtcRunOutcome, RunPtcProgramOptions } from \"./runtime/dispatcher.ts\";\nexport {\n createWorkerEnv,\n DEFAULT_CONFIG,\n effectiveTimeoutMs,\n resolveConfig,\n WORKER_ENV_ALLOW_LIST,\n} from \"./runtime/limits.ts\";\nexport type { PtcConfig, PtcSurface } from \"./runtime/limits.ts\";\nexport {\n PTC_ERROR_KIND,\n PTC_LOG_LEVEL,\n HOST_FRAME_KIND,\n WORKER_FRAME_KIND,\n} from \"./runtime/protocol.ts\";\nexport type { PtcErrorKind, PtcErrorShape, PtcJsonValue } from \"./runtime/protocol.ts\";\n\nexport default function ptcSubagents(pi: ExtensionAPI): void {\n const mode = initialModeState();\n // Set on entry, cleared after the briefing has been injected, so the instruction lands once\n // per mode entry instead of on every turn.\n let briefingPending = false;\n\n /**\n * Bindings come from the mode's base snapshot while PTC mode is on, and from the live loadout\n * otherwise (T7, #21). See `src/mode/ptc-mode.ts` for why the snapshot is required: the mode\n * hides the built-ins, which would otherwise empty the binding table and make every\n * `tools.<name>(...)` call fail.\n */\n const getBindingSourceNames = (): readonly string[] => bindingSource(mode, pi.getActiveTools());\n\n pi.registerTool(createPtcRunCodeTool({ getBindingSourceNames }));\n pi.registerTool(createPtcWorkflowTool({ getBindingSourceNames }));\n\n const paintStatus = (ctx: ExtensionContext): void => {\n ctx.ui.setStatus(\n PTC_MODE_STATUS_KEY,\n mode.enabled ? ctx.ui.theme.fg(\"accent\", MODE_STATUS_TEXT) : undefined,\n );\n };\n\n const enterMode = (\n base: readonly string[],\n loadout: readonly string[],\n ctx: ExtensionContext,\n announcement: string,\n ): void => {\n mode.enabled = true;\n mode.base = base;\n mode.ourLoadout = loadout;\n pi.setActiveTools([...loadout]);\n briefingPending = true;\n pi.appendEntry(PTC_MODE_ENTRY_TYPE, { enabled: true, base: [...base] });\n paintStatus(ctx);\n ctx.ui.notify(announcement, \"info\");\n };\n\n const exitMode = (\n ctx: ExtensionContext,\n opts: { announce: boolean; customType: string },\n ): void => {\n const base = mode.base;\n mode.enabled = false;\n mode.base = undefined;\n mode.ourLoadout = undefined;\n briefingPending = false;\n if (base !== undefined) pi.setActiveTools([...base]);\n pi.appendEntry(opts.customType, { enabled: false });\n paintStatus(ctx);\n if (opts.announce) {\n ctx.ui.notify(\"PTC mode off — built-in tools are directly callable again.\", \"info\");\n }\n };\n\n /** Restore the persisted mode record, if this session has one. */\n const readPersistedMode = (ctx: ExtensionContext): PersistedModeState | undefined => {\n const entries = ctx.sessionManager.getEntries();\n for (let index = entries.length - 1; index >= 0; index -= 1) {\n const entry = entries[index];\n if (entry?.type !== \"custom\" || entry.customType !== PTC_MODE_ENTRY_TYPE) continue;\n const data = entry.data as { enabled?: unknown; base?: unknown } | undefined;\n const base = Array.isArray(data?.base)\n ? data.base.filter((name): name is string => typeof name === \"string\")\n : undefined;\n return {\n enabled: data?.enabled === true,\n ...(base === undefined ? {} : { base }),\n };\n }\n return undefined;\n };\n\n pi.registerCommand(\"ptc\", {\n description:\n \"Show or toggle PTC mode (built-in tools reachable only from inside a PTC program)\",\n handler: async (args, ctx) => {\n const action = args.trim().toLowerCase();\n if (action === \"off\") {\n if (!mode.enabled) {\n ctx.ui.notify(\"PTC mode is already off.\", \"info\");\n return;\n }\n exitMode(ctx, { announce: true, customType: PTC_MODE_ENTRY_TYPE });\n return;\n }\n if (action !== \"on\") {\n ctx.ui.notify(\n mode.enabled\n ? `PTC mode is ON — run /ptc off to restore built-in tools.\\nBindings: ${resolveBindingNames(mode.base).join(\", \") || \"(none)\"}`\n : \"PTC mode is OFF — run /ptc on to enter it.\",\n \"info\",\n );\n return;\n }\n if (mode.enabled) {\n ctx.ui.notify(\"PTC mode is already on.\", \"info\");\n return;\n }\n // Manual entry bypasses the config and the restricted-session policy; a restricted session\n // simply enters with its own (smaller) loadout as `base`, so bindings stay inside it.\n const decision = decideModeEntry({\n mode: ctx.mode,\n defaultMode: true,\n active: pi.getActiveTools(),\n manual: true,\n hide: MODE_HIDE_STRATEGY,\n });\n if (!decision.enter) {\n ctx.ui.notify(\n decision.reason === \"tools-unavailable\"\n ? \"Cannot enter PTC mode: ptc_run_code / ptc_workflow are not active in this session.\"\n : \"Cannot enter PTC mode in this run mode.\",\n \"warning\",\n );\n return;\n }\n enterMode(\n decision.base,\n decision.loadout,\n ctx,\n describeEntry(decision.base, MODE_HIDE_STRATEGY),\n );\n },\n });\n\n pi.on(\"session_start\", async (_event, ctx) => {\n const config = readDefaultModeConfig(getAgentDir());\n if (config.error !== undefined) {\n ctx.ui.notify(`pi-ptc-subagents: ${config.error}`, \"warning\");\n }\n\n const persisted = readPersistedMode(ctx);\n const active = pi.getActiveTools();\n const { base, restoreFirst } = resolveBaseOnStart(persisted, active);\n // Undo our own narrowing before deciding, so the decision sees the loadout this pi process\n // was actually launched with rather than the one a previous mode invocation left behind.\n if (restoreFirst) pi.setActiveTools([...base]);\n\n const decision = decideModeEntry({\n mode: ctx.mode,\n defaultMode: config.defaultMode,\n active: base,\n manual: false,\n hide: MODE_HIDE_STRATEGY,\n });\n // A persisted record is the user's most recent explicit intent: `/ptc off` survives a resume\n // even when the config still says default-on.\n const wantMode = persisted?.enabled ?? decision.enter;\n\n if (decision.enter && wantMode) {\n enterMode(base, decision.loadout, ctx, describeEntry(base, MODE_HIDE_STRATEGY));\n return;\n }\n paintStatus(ctx);\n if (!decision.enter && decision.reason !== \"not-tui\" && persisted === undefined) {\n if (decision.reason === \"restricted-session\") {\n ctx.ui.notify(\n \"PTC mode skipped: this session was launched with an explicit tool restriction.\",\n \"info\",\n );\n }\n }\n });\n\n /**\n * Put back the skills section pi withholds while the mode hides `read`/`bash` (ADR-0011).\n *\n * pi advertises skills only when the session can open a skill file at all (`read` or `bash`\n * among the selected tools), so a PTC session silently loses the whole `<available_skills>`\n * list: the model stops knowing skills exist. Writing `sections.skills` here is the seam pi\n * leaves open — custom sections are merged after its own, so this one wins.\n *\n * The same options object is reused across turns and sessions of a resume, so the injection is\n * undone the moment the mode stops hiding those tools: a `/ptc off` must not leave PTC-flavoured\n * loading instructions behind while `read` is directly callable again.\n */\n let skillsSectionInjected = false;\n const syncSkillsSection = (\n options: NormalizedBuildSystemPromptOptions | undefined,\n hidden: boolean,\n ): void => {\n if (options === undefined) return;\n if (!mode.enabled || !hidden) {\n if (skillsSectionInjected) {\n delete options.sections.skills;\n skillsSectionInjected = false;\n }\n return;\n }\n const section = buildPtcSkillsSection(options.skills);\n if (section.length === 0) return;\n options.sections.skills = section;\n skillsSectionInjected = true;\n };\n\n pi.on(\"before_agent_start\", async (event, ctx) => {\n // A live pi always supplies these; the test stub emits a bare event, hence the undefined case.\n const options = event.systemPromptOptions;\n syncSkillsSection(options, skillsSectionDropped(pi.getActiveTools()));\n if (!mode.enabled) return;\n if (detectExternalLoadoutChange(mode, pi.getActiveTools())) {\n // Another extension owns the loadout now; hand it back rather than fight over it.\n exitMode(ctx, { announce: false, customType: PTC_MODE_ENTRY_TYPE });\n syncSkillsSection(options, skillsSectionDropped(pi.getActiveTools()));\n ctx.ui.notify(\"PTC mode off — another extension changed the active tool set.\", \"warning\");\n return;\n }\n if (!briefingPending) return;\n briefingPending = false;\n return {\n message: {\n customType: \"ptc-mode-briefing\",\n content: buildModeInstruction(resolveBindingNames(mode.base), MODE_HIDE_STRATEGY),\n display: false,\n },\n };\n });\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAmCA,MAAM,aAAa;;;;;;;;;AA+DnB,SAAgB,gBAAgB,cAAsB,OAAe,UAA0B;CAC7F,MAAM,OAAO;EACX,6BAA4B,QAAQ,oBAAkB,WAAW;EACjE,gCACE,QACA;EACF;EACA,mCACE,WACA,QACA,QACA;EACF;EACA;CACF,CAAC,CAAC,KAAK,IAAI;CACX,OAAO,aAAa,WAAW,IAAI,OAAO,eAAe,SAAS;AACpE;;AAGA,SAAgB,0BAA0B,cAAsC;CAC9E,OAAO;EACL,MAAM;EACN,QAAQ;EACR,WAAW;EACX,YAAY;EACZ,UAAU;EACV,cACE,iDAAiD,eAAe;CACpE;AACF;;;;;;;;;;;;;;;AAmCA,SAAgB,mBAAmB,OAAwC;CACzE,IAAI,MAAM,SACR,OAAO;EAAE,QAAQ;EAAY,cAAc;CAAqB;CAElE,IAAI,MAAM,aAAa,KAAK,MAAM,UAAU,SAAS,GACnD,OAAO,EAAE,QAAQ,YAAY;CAE/B,IAAI,MAAM,aAAa,GACrB,OAAO;EAAE,QAAQ;EAAY,cAAc;CAAkC;CAE/E,OAAO,EAAE,QAAQ,WAAW;AAC9B;;;;;;;;;;AAyBA,SAAgB,SAAS,MAAgB,QAAiC;CACxE,IAAI,KAAK,QAAQ,KAAA,GAAW,OAAO;CACnC,IAAI;EACF,KAAK,KAAK,MAAM;EAChB,OAAO;CACT,QAAQ;EACN,OAAO;CACT;AACF;;;;;AAMA,SAAS,kBAAkB,MAAc,KAAiC;CACxE,MAAM,IAAI,KAAK,MAAM,IAAI,OAAO,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