reasonix 0.4.23 → 0.4.26
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +23 -0
- package/dist/cli/{chunk-K6MR4SWS.js → chunk-2BYEKJHX.js} +119 -10
- package/dist/cli/chunk-2BYEKJHX.js.map +1 -0
- package/dist/cli/index.js +773 -81
- package/dist/cli/index.js.map +1 -1
- package/dist/cli/{prompt-VDN5U3YE.js → prompt-6DMLWG2H.js} +2 -2
- package/dist/index.d.ts +268 -2
- package/dist/index.js +656 -12
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/dist/cli/chunk-K6MR4SWS.js.map +0 -1
- /package/dist/cli/{prompt-VDN5U3YE.js.map → prompt-6DMLWG2H.js.map} +0 -0
package/dist/cli/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/cli/index.ts","../../src/config.ts","../../src/client.ts","../../src/retry.ts","../../src/harvest.ts","../../src/consistency.ts","../../src/hooks.ts","../../src/repair/flatten.ts","../../src/tools.ts","../../src/mcp/registry.ts","../../src/memory.ts","../../src/repair/scavenge.ts","../../src/repair/storm.ts","../../src/repair/truncation.ts","../../src/repair/index.ts","../../src/session.ts","../../src/telemetry.ts","../../src/loop.ts","../../src/tools/filesystem.ts","../../src/tools/memory.ts","../../src/tools/plan.ts","../../src/tools/shell.ts","../../src/tools/web.ts","../../src/env.ts","../../src/transcript.ts","../../src/replay.ts","../../src/diff.ts","../../src/mcp/types.ts","../../src/mcp/client.ts","../../src/mcp/stdio.ts","../../src/mcp/sse.ts","../../src/mcp/shell-split.ts","../../src/mcp/spec.ts","../../src/mcp/inspect.ts","../../src/code/edit-blocks.ts","../../src/version.ts","../../src/cli/commands/chat.tsx","../../src/tools/skills.ts","../../src/cli/ui/App.tsx","../../src/cli/ui/EventLog.tsx","../../src/cli/ui/PlanStateBlock.tsx","../../src/cli/ui/markdown.tsx","../../src/cli/ui/ticker.tsx","../../src/cli/ui/PlanConfirm.tsx","../../src/cli/ui/Select.tsx","../../src/cli/ui/PlanRefineInput.tsx","../../src/cli/ui/PromptInput.tsx","../../src/cli/ui/multiline-keys.ts","../../src/cli/ui/ShellConfirm.tsx","../../src/cli/ui/SlashSuggestions.tsx","../../src/cli/ui/StatsPanel.tsx","../../src/cli/ui/slash.ts","../../src/cli/ui/SessionPicker.tsx","../../src/cli/ui/Setup.tsx","../../src/cli/commands/code.tsx","../../src/cli/commands/diff.ts","../../src/cli/ui/DiffApp.tsx","../../src/cli/ui/RecordView.tsx","../../src/cli/commands/mcp-inspect.ts","../../src/mcp/catalog.ts","../../src/cli/commands/mcp.ts","../../src/cli/commands/replay.ts","../../src/cli/ui/ReplayApp.tsx","../../src/cli/commands/run.ts","../../src/cli/commands/sessions.ts","../../src/cli/commands/setup.tsx","../../src/cli/ui/Wizard.tsx","../../src/cli/ui/presets.ts","../../src/cli/commands/stats.ts","../../src/cli/commands/update.ts","../../src/cli/commands/version.ts","../../src/cli/resolve.ts"],"sourcesContent":["import { Command } from \"commander\";\nimport { readConfig } from \"../config.js\";\nimport { VERSION } from \"../index.js\";\nimport { applyMemoryStack } from \"../user-memory.js\";\nimport { chatCommand } from \"./commands/chat.js\";\nimport { codeCommand } from \"./commands/code.js\";\nimport { diffCommand } from \"./commands/diff.js\";\nimport { mcpInspectCommand } from \"./commands/mcp-inspect.js\";\nimport { mcpListCommand } from \"./commands/mcp.js\";\nimport { replayCommand } from \"./commands/replay.js\";\nimport { runCommand } from \"./commands/run.js\";\nimport { sessionsCommand } from \"./commands/sessions.js\";\nimport { setupCommand } from \"./commands/setup.js\";\nimport { statsCommand } from \"./commands/stats.js\";\nimport { updateCommand } from \"./commands/update.js\";\nimport { versionCommand } from \"./commands/version.js\";\nimport { resolveDefaults } from \"./resolve.js\";\n\nconst DEFAULT_SYSTEM =\n \"You are Reasonix, a helpful DeepSeek-powered assistant. Be concise and accurate. Use tools when available.\";\n\nconst program = new Command();\nprogram\n .name(\"reasonix\")\n .description(\"DeepSeek-native agent framework — built for cache hits and cheap tokens.\")\n .version(VERSION);\n\n// `reasonix` with no subcommand → launch the friendliest flow.\n// First run (no config yet) → interactive setup wizard.\n// Otherwise → chat with saved defaults. This is the \"one command to\n// rule them all\" entry for non-power-users: they don't need to learn\n// `chat` / `setup` / `--mcp` — just type `reasonix`.\nprogram.action(async () => {\n const cfg = readConfig();\n if (!cfg.setupCompleted) {\n await setupCommand({});\n return;\n }\n const defaults = resolveDefaults({});\n await chatCommand({\n model: defaults.model,\n system: applyMemoryStack(DEFAULT_SYSTEM, process.cwd()),\n harvest: defaults.harvest,\n branch: defaults.branch,\n session: defaults.session,\n mcp: defaults.mcp,\n });\n});\n\nprogram\n .command(\"setup\")\n .description(\"Interactive wizard — API key, preset, MCP servers. Re-run any time to reconfigure.\")\n .action(async () => {\n await setupCommand({});\n });\n\nprogram\n .command(\"code [dir]\")\n .description(\n \"Code-editing chat — filesystem MCP auto-bridged at <dir> (default: cwd), coding system prompt, smart preset. Model proposes SEARCH/REPLACE blocks; Reasonix applies them to disk.\",\n )\n .option(\"-m, --model <id>\", \"Override default reasoner model\")\n .option(\"--no-session\", \"Disable session persistence for this run\")\n .option(\"-r, --resume\", \"Skip the session picker — always continue prior messages\")\n .option(\"-n, --new\", \"Skip the session picker — always wipe prior messages and start fresh\")\n .option(\"--transcript <path>\", \"Write a JSONL transcript to this path\")\n .action(async (dir: string | undefined, opts) => {\n await codeCommand({\n dir,\n model: opts.model,\n noSession: opts.session === false,\n transcript: opts.transcript,\n forceResume: !!opts.resume,\n forceNew: !!opts.new,\n });\n });\n\nprogram\n .command(\"chat\")\n .description(\"Interactive Ink TUI with live cache/cost panel.\")\n .option(\"-m, --model <id>\", \"DeepSeek model id (overrides preset)\")\n .option(\"-s, --system <prompt>\", \"System prompt (pinned in the immutable prefix)\", DEFAULT_SYSTEM)\n .option(\"--transcript <path>\", \"Write a JSONL transcript to this path\")\n .option(\n \"--preset <name>\",\n \"Bundle of model + harvest + branch. One of: fast, smart, max. Overrides config.preset.\",\n )\n .option(\n \"--harvest\",\n \"Extract typed plan state from R1 reasoning (Pillar 2). Overrides preset's harvest setting.\",\n )\n .option(\n \"--branch <n>\",\n \"Self-consistency: run N parallel samples per turn and pick the most confident (disables streaming; enables harvest)\",\n (v) => Number.parseInt(v, 10),\n )\n .option(\"--session <name>\", \"Use a named session (default: from config, usually 'default').\")\n .option(\"--no-session\", \"Disable session persistence for this run (ephemeral chat)\")\n .option(\"-r, --resume\", \"Skip the session picker — always continue prior messages\")\n .option(\"-n, --new\", \"Skip the session picker — always wipe prior messages and start fresh\")\n .option(\n \"--mcp <spec>\",\n 'MCP server spec; repeatable. \"name=cmd args...\", \"cmd args...\", or a URL (http/https → SSE transport). Overrides config.mcp when provided.',\n (value: string, previous: string[] = []) => [...previous, value],\n [] as string[],\n )\n .option(\n \"--mcp-prefix <str>\",\n \"Global prefix applied to every MCP tool (only honored when no per-spec name is set; avoids collisions with a single anonymous server)\",\n )\n .option(\"--no-config\", \"Ignore `~/.reasonix/config.json` — useful for CI or reproducing issues\")\n .action(async (opts) => {\n const defaults = resolveDefaults({\n model: opts.model,\n harvest: opts.harvest,\n branch: opts.branch,\n mcp: opts.mcp as string[],\n session: opts.session,\n preset: opts.preset,\n noConfig: opts.config === false,\n });\n await chatCommand({\n model: defaults.model,\n system: applyMemoryStack(opts.system, process.cwd()),\n transcript: opts.transcript,\n harvest: defaults.harvest,\n branch: defaults.branch,\n session: defaults.session,\n mcp: defaults.mcp,\n mcpPrefix: opts.mcpPrefix,\n forceResume: !!opts.resume,\n forceNew: !!opts.new,\n });\n });\n\nprogram\n .command(\"run <task>\")\n .description(\"Run a single task non-interactively, streaming output.\")\n .option(\"-m, --model <id>\", \"DeepSeek model id (overrides preset)\")\n .option(\"-s, --system <prompt>\", \"System prompt\", DEFAULT_SYSTEM)\n .option(\"--preset <name>\", \"Bundle of model + harvest + branch: fast | smart | max\")\n .option(\"--harvest\", \"Extract typed plan state from R1 reasoning (Pillar 2)\")\n .option(\n \"--branch <n>\",\n \"Self-consistency: run N parallel samples per turn and pick the most confident\",\n (v) => Number.parseInt(v, 10),\n )\n .option(\"--transcript <path>\", \"Write a JSONL transcript to this path for replay/diff\")\n .option(\n \"--mcp <spec>\",\n 'MCP server spec; repeatable. \"name=cmd args...\", \"cmd args...\", or a URL (http/https → SSE).',\n (value: string, previous: string[] = []) => [...previous, value],\n [] as string[],\n )\n .option(\n \"--mcp-prefix <str>\",\n \"Global prefix (only honored when no per-spec name is set; for a single anonymous server)\",\n )\n .option(\"--no-config\", \"Ignore `~/.reasonix/config.json` — useful for CI or reproducing issues\")\n .action(async (task: string, opts) => {\n const defaults = resolveDefaults({\n model: opts.model,\n harvest: opts.harvest,\n branch: opts.branch,\n mcp: opts.mcp as string[],\n preset: opts.preset,\n noConfig: opts.config === false,\n });\n await runCommand({\n task,\n model: defaults.model,\n system: applyMemoryStack(opts.system, process.cwd()),\n harvest: defaults.harvest,\n branch: defaults.branch,\n transcript: opts.transcript,\n mcp: defaults.mcp,\n mcpPrefix: opts.mcpPrefix,\n });\n });\n\nprogram\n .command(\"stats <transcript>\")\n .description(\"Summarize a JSONL transcript produced by `reasonix chat --transcript`.\")\n .action((transcript: string) => {\n statsCommand({ transcript });\n });\n\nprogram\n .command(\"sessions [name]\")\n .description(\"List saved chat sessions, or inspect one by name.\")\n .option(\"-v, --verbose\", \"Include system prompts + tool-call metadata when inspecting\")\n .action((name: string | undefined, opts) => {\n sessionsCommand({ name, verbose: !!opts.verbose });\n });\n\nprogram\n .command(\"replay <transcript>\")\n .description(\n \"Interactive Ink TUI to scrub through a transcript + rebuild its session summary (cost, cache, prefix stability). No API calls.\",\n )\n .option(\"--print\", \"Dump to stdout instead of mounting the TUI (auto when piped)\")\n .option(\"--head <n>\", \"stdout mode only — show first N records\", (v) => Number.parseInt(v, 10))\n .option(\"--tail <n>\", \"stdout mode only — show last N records\", (v) => Number.parseInt(v, 10))\n .action(async (transcript: string, opts) => {\n await replayCommand({\n path: transcript,\n print: !!opts.print,\n head: Number.isFinite(opts.head) ? opts.head : undefined,\n tail: Number.isFinite(opts.tail) ? opts.tail : undefined,\n });\n });\n\nprogram\n .command(\"diff <a> <b>\")\n .description(\n \"Compare two transcripts in a split-pane Ink TUI (default) or stdout table. Use n/N to jump across divergences.\",\n )\n .option(\"--md <path>\", \"Write a markdown report (blog-ready) to this path\")\n .option(\"--print\", \"Force stdout table instead of the TUI (auto when piped)\")\n .option(\"--tui\", \"Force the TUI even when piped (rare)\")\n .option(\"--label-a <label>\", \"Display label for transcript A (default: filename)\")\n .option(\"--label-b <label>\", \"Display label for transcript B (default: filename)\")\n .action(async (a: string, b: string, opts) => {\n await diffCommand({\n a,\n b,\n mdPath: opts.md,\n labelA: opts.labelA,\n labelB: opts.labelB,\n print: !!opts.print,\n tui: !!opts.tui,\n });\n });\n\nconst mcp = program\n .command(\"mcp\")\n .description(\"Model Context Protocol helpers — discover servers, test your setup.\");\n\nmcp\n .command(\"list\")\n .description(\"Show a curated catalog of popular MCP servers with ready-to-use --mcp commands.\")\n .option(\"--json\", \"Emit the catalog as JSON instead of the human-readable table\")\n .action((opts) => {\n mcpListCommand({ json: !!opts.json });\n });\n\nmcp\n .command(\"inspect <spec>\")\n .description(\n 'Connect to one MCP server and print its server info + tools/resources/prompts. <spec> takes the same forms as --mcp (\"name=cmd args\", \"cmd args\", or an SSE URL).',\n )\n .option(\"--json\", \"Emit the full inspection report as JSON instead of the human-readable summary\")\n .action(async (spec: string, opts) => {\n try {\n await mcpInspectCommand({ spec, json: !!opts.json });\n } catch (err) {\n process.stderr.write(`mcp inspect failed: ${(err as Error).message}\\n`);\n process.exit(1);\n }\n });\n\nprogram.command(\"version\").description(\"Print Reasonix version.\").action(versionCommand);\n\nprogram\n .command(\"update\")\n .description(\n \"Check the npm registry for a newer Reasonix and install it. Detects npx vs global install; for npx users, prints a cache-refresh hint instead of running `npm i -g`.\",\n )\n .option(\"--dry-run\", \"Print the plan without executing the install\")\n .action(async (opts: { dryRun?: boolean }) => {\n await updateCommand({ dryRun: !!opts.dryRun });\n });\n\nprogram.parseAsync(process.argv).catch((err) => {\n console.error(err);\n process.exit(1);\n});\n","/**\n * User-level config storage for the Reasonix CLI.\n *\n * Lookup order for the API key:\n * 1. `DEEPSEEK_API_KEY` env var (highest priority — for CI / power users)\n * 2. `~/.reasonix/config.json` (set by the first-run setup flow)\n *\n * The library itself never touches the config file — it only reads\n * `DEEPSEEK_API_KEY` from the environment. The CLI is responsible for\n * pulling from the config file and exposing it via env var to the loop.\n *\n * Beyond the API key, the config also remembers the user's *defaults*\n * from `reasonix setup`: preset, MCP servers, session. This is what\n * makes `reasonix chat` with no flags \"just work\" after first-run.\n */\n\nimport { chmodSync, mkdirSync, readFileSync, writeFileSync } from \"node:fs\";\nimport { homedir } from \"node:os\";\nimport { dirname, join } from \"node:path\";\n\n/** One of the preset bundles (model + harvest + branch combo). */\nexport type PresetName = \"fast\" | \"smart\" | \"max\";\n\nexport interface ReasonixConfig {\n apiKey?: string;\n baseUrl?: string;\n /**\n * Default preset for `reasonix chat` / `reasonix run` when no flags override.\n * Maps to model + harvest + branch combos (see presets.ts). Missing → \"fast\".\n */\n preset?: PresetName;\n /**\n * Default MCP server specs to bridge on every `reasonix chat`, in the\n * same `\"name=cmd args...\"` format that `--mcp` takes. Stored as strings\n * so `reasonix setup` stays symmetrical with the flag — one parser, one\n * format in the config file, grep-friendly.\n */\n mcp?: string[];\n /**\n * Default session name (null/missing → \"default\", which is what the\n * CLI has been doing anyway). `reasonix setup` lets users pick a name\n * or opt into ephemeral.\n */\n session?: string | null;\n /** Marks that `reasonix setup` has completed at least once. */\n setupCompleted?: boolean;\n /**\n * Whether `web_search` + `web_fetch` tools are registered. Default:\n * enabled (no key required — backed by DuckDuckGo's public HTML\n * endpoint). Set to `false` to keep the session offline.\n */\n search?: boolean;\n /**\n * Per-project state keyed by absolute directory path. Written by the\n * \"always allow\" choice on a shell confirmation prompt; merged into\n * `registerShellTools({ extraAllowed })` when `reasonix code` runs\n * against that directory again.\n */\n projects?: {\n [absoluteRootDir: string]: {\n shellAllowed?: string[];\n };\n };\n}\n\nexport function defaultConfigPath(): string {\n return join(homedir(), \".reasonix\", \"config.json\");\n}\n\nexport function readConfig(path: string = defaultConfigPath()): ReasonixConfig {\n try {\n const raw = readFileSync(path, \"utf8\");\n const parsed = JSON.parse(raw);\n if (parsed && typeof parsed === \"object\") return parsed as ReasonixConfig;\n } catch {\n /* missing or malformed → empty config */\n }\n return {};\n}\n\nexport function writeConfig(cfg: ReasonixConfig, path: string = defaultConfigPath()): void {\n mkdirSync(dirname(path), { recursive: true });\n writeFileSync(path, JSON.stringify(cfg, null, 2), \"utf8\");\n // Restrict permissions on Unix; chmod is a no-op on Windows but won't throw.\n try {\n chmodSync(path, 0o600);\n } catch {\n /* ignore on platforms without chmod */\n }\n}\n\n/** Resolve the API key from env var first, then the config file. */\nexport function loadApiKey(path: string = defaultConfigPath()): string | undefined {\n if (process.env.DEEPSEEK_API_KEY) return process.env.DEEPSEEK_API_KEY;\n return readConfig(path).apiKey;\n}\n\n/**\n * Resolve whether web-search tools should be registered. Default: on.\n * Env `REASONIX_SEARCH=off` or config `search: false` turns it off.\n * Any other value falls through to enabled.\n */\nexport function searchEnabled(path: string = defaultConfigPath()): boolean {\n const env = process.env.REASONIX_SEARCH;\n if (env === \"off\" || env === \"false\" || env === \"0\") return false;\n const cfg = readConfig(path).search;\n if (cfg === false) return false;\n return true;\n}\n\nexport function saveApiKey(key: string, path: string = defaultConfigPath()): void {\n const cfg = readConfig(path);\n cfg.apiKey = key.trim();\n writeConfig(cfg, path);\n}\n\n/**\n * Read the persisted \"always allow\" shell-command prefixes for a\n * given project root. Returns an empty array when nothing's stored.\n */\nexport function loadProjectShellAllowed(\n rootDir: string,\n path: string = defaultConfigPath(),\n): string[] {\n const cfg = readConfig(path);\n return cfg.projects?.[rootDir]?.shellAllowed ?? [];\n}\n\n/**\n * Append a prefix to the project's shell-allowed list, dedup and\n * persist. No-op if the prefix is empty/whitespace or already stored.\n */\nexport function addProjectShellAllowed(\n rootDir: string,\n prefix: string,\n path: string = defaultConfigPath(),\n): void {\n const trimmed = prefix.trim();\n if (!trimmed) return;\n const cfg = readConfig(path);\n if (!cfg.projects) cfg.projects = {};\n if (!cfg.projects[rootDir]) cfg.projects[rootDir] = {};\n const existing = cfg.projects[rootDir].shellAllowed ?? [];\n if (existing.includes(trimmed)) return;\n cfg.projects[rootDir].shellAllowed = [...existing, trimmed];\n writeConfig(cfg, path);\n}\n\nexport function isPlausibleKey(key: string): boolean {\n const trimmed = key.trim();\n return /^sk-[A-Za-z0-9_-]{16,}$/.test(trimmed);\n}\n\n/** Mask a key for display: `sk-abcd...wxyz`. */\nexport function redactKey(key: string): string {\n if (!key) return \"\";\n if (key.length <= 12) return \"****\";\n return `${key.slice(0, 6)}…${key.slice(-4)}`;\n}\n","import { type EventSourceMessage, createParser } from \"eventsource-parser\";\nimport { type RetryOptions, fetchWithRetry } from \"./retry.js\";\nimport type { ChatMessage, ChatRequestOptions, RawUsage, ToolCall, ToolSpec } from \"./types.js\";\n\nexport class Usage {\n constructor(\n public promptTokens = 0,\n public completionTokens = 0,\n public totalTokens = 0,\n public promptCacheHitTokens = 0,\n public promptCacheMissTokens = 0,\n ) {}\n\n get cacheHitRatio(): number {\n const denom = this.promptCacheHitTokens + this.promptCacheMissTokens;\n return denom > 0 ? this.promptCacheHitTokens / denom : 0;\n }\n\n static fromApi(raw: RawUsage | undefined | null): Usage {\n const u = raw ?? {};\n return new Usage(\n u.prompt_tokens ?? 0,\n u.completion_tokens ?? 0,\n u.total_tokens ?? 0,\n u.prompt_cache_hit_tokens ?? 0,\n u.prompt_cache_miss_tokens ?? 0,\n );\n }\n}\n\nexport interface ChatResponse {\n content: string;\n reasoningContent: string | null;\n toolCalls: ToolCall[];\n usage: Usage;\n raw: unknown;\n}\n\nexport interface StreamChunk {\n contentDelta?: string;\n reasoningDelta?: string;\n toolCallDelta?: { index: number; id?: string; name?: string; argumentsDelta?: string };\n usage?: Usage;\n finishReason?: string;\n raw: any;\n}\n\n/**\n * Response shape for DeepSeek's `/user/balance` endpoint. One entry\n * per currency the account is funded in (typically CNY, sometimes\n * USD). `total_balance` is the spendable figure; `granted_balance`\n * counts promotional credits that expire, `topped_up_balance` is\n * what the user paid for and keeps.\n */\nexport interface BalanceInfo {\n currency: string;\n total_balance: string;\n granted_balance?: string;\n topped_up_balance?: string;\n}\n\nexport interface UserBalance {\n is_available: boolean;\n balance_infos: BalanceInfo[];\n}\n\nexport interface DeepSeekClientOptions {\n apiKey?: string;\n baseUrl?: string;\n timeoutMs?: number;\n fetch?: typeof fetch;\n /** Retry configuration. Pass `{ maxAttempts: 1 }` to disable retries. */\n retry?: RetryOptions;\n}\n\nexport class DeepSeekClient {\n readonly apiKey: string;\n readonly baseUrl: string;\n readonly timeoutMs: number;\n readonly retry: RetryOptions;\n private readonly _fetch: typeof fetch;\n\n constructor(opts: DeepSeekClientOptions = {}) {\n const apiKey = opts.apiKey ?? process.env.DEEPSEEK_API_KEY;\n if (!apiKey) {\n throw new Error(\n \"DEEPSEEK_API_KEY is not set. Put it in .env or pass apiKey to DeepSeekClient.\",\n );\n }\n this.apiKey = apiKey;\n this.baseUrl = (\n opts.baseUrl ??\n process.env.DEEPSEEK_BASE_URL ??\n \"https://api.deepseek.com\"\n ).replace(/\\/+$/, \"\");\n this.timeoutMs = opts.timeoutMs ?? 120_000;\n this._fetch = opts.fetch ?? globalThis.fetch.bind(globalThis);\n this.retry = opts.retry ?? {};\n }\n\n private buildPayload(opts: ChatRequestOptions, stream: boolean) {\n const payload: Record<string, unknown> = {\n model: opts.model,\n messages: opts.messages,\n stream,\n };\n if (opts.tools?.length) payload.tools = opts.tools;\n if (opts.temperature !== undefined) payload.temperature = opts.temperature;\n if (opts.maxTokens !== undefined) payload.max_tokens = opts.maxTokens;\n if (opts.responseFormat) payload.response_format = opts.responseFormat;\n return payload;\n }\n\n /**\n * Fetch the current DeepSeek account balance. Separate endpoint\n * from chat completions, no billing impact. Returns null on any\n * network/auth failure so callers can gate the balance display\n * without a hard error — the rest of the session works regardless.\n */\n async getBalance(opts: { signal?: AbortSignal } = {}): Promise<UserBalance | null> {\n try {\n const resp = await this._fetch(`${this.baseUrl}/user/balance`, {\n method: \"GET\",\n headers: { Authorization: `Bearer ${this.apiKey}` },\n signal: opts.signal,\n });\n if (!resp.ok) return null;\n const data = (await resp.json()) as UserBalance;\n if (!data || !Array.isArray(data.balance_infos)) return null;\n return data;\n } catch {\n return null;\n }\n }\n\n async chat(opts: ChatRequestOptions): Promise<ChatResponse> {\n const ctrl = new AbortController();\n const timer = setTimeout(() => ctrl.abort(), this.timeoutMs);\n const signal = opts.signal ?? ctrl.signal;\n\n try {\n const resp = await fetchWithRetry(\n this._fetch,\n `${this.baseUrl}/chat/completions`,\n {\n method: \"POST\",\n headers: {\n Authorization: `Bearer ${this.apiKey}`,\n \"Content-Type\": \"application/json\",\n },\n body: JSON.stringify(this.buildPayload(opts, false)),\n signal,\n },\n { ...this.retry, signal },\n );\n if (!resp.ok) {\n throw new Error(`DeepSeek ${resp.status}: ${await resp.text()}`);\n }\n const data: any = await resp.json();\n const choice = data.choices?.[0]?.message ?? {};\n return {\n content: choice.content ?? \"\",\n reasoningContent: choice.reasoning_content ?? null,\n toolCalls: choice.tool_calls ?? [],\n usage: Usage.fromApi(data.usage),\n raw: data,\n };\n } finally {\n clearTimeout(timer);\n }\n }\n\n async *stream(opts: ChatRequestOptions): AsyncGenerator<StreamChunk> {\n const ctrl = new AbortController();\n const timer = setTimeout(() => ctrl.abort(), this.timeoutMs);\n const signal = opts.signal ?? ctrl.signal;\n\n let resp: Response;\n try {\n // Only the initial fetch is retried. Once the server has started sending\n // the stream body we do NOT retry — a mid-stream retry would re-bill and\n // desync the session context.\n resp = await fetchWithRetry(\n this._fetch,\n `${this.baseUrl}/chat/completions`,\n {\n method: \"POST\",\n headers: {\n Authorization: `Bearer ${this.apiKey}`,\n \"Content-Type\": \"application/json\",\n Accept: \"text/event-stream\",\n },\n body: JSON.stringify(this.buildPayload(opts, true)),\n signal,\n },\n { ...this.retry, signal },\n );\n } catch (err) {\n clearTimeout(timer);\n throw err;\n }\n if (!resp.ok || !resp.body) {\n clearTimeout(timer);\n throw new Error(`DeepSeek ${resp.status}: ${await resp.text().catch(() => \"\")}`);\n }\n\n const queue: StreamChunk[] = [];\n let done = false;\n const parser = createParser({\n onEvent: (ev: EventSourceMessage) => {\n if (!ev.data || ev.data === \"[DONE]\") {\n done = true;\n return;\n }\n try {\n const json = JSON.parse(ev.data);\n const delta = json.choices?.[0]?.delta ?? {};\n const finishReason = json.choices?.[0]?.finish_reason ?? undefined;\n const chunk: StreamChunk = { raw: json, finishReason };\n if (typeof delta.content === \"string\" && delta.content.length > 0) {\n chunk.contentDelta = delta.content;\n }\n if (typeof delta.reasoning_content === \"string\" && delta.reasoning_content.length > 0) {\n chunk.reasoningDelta = delta.reasoning_content;\n }\n if (Array.isArray(delta.tool_calls) && delta.tool_calls.length > 0) {\n const tc = delta.tool_calls[0];\n chunk.toolCallDelta = {\n index: tc.index ?? 0,\n id: tc.id,\n name: tc.function?.name,\n argumentsDelta: tc.function?.arguments,\n };\n }\n if (json.usage) {\n chunk.usage = Usage.fromApi(json.usage);\n }\n queue.push(chunk);\n } catch {\n /* skip malformed sse frame */\n }\n },\n });\n\n const reader = resp.body.getReader();\n const decoder = new TextDecoder();\n try {\n while (true) {\n if (queue.length > 0) {\n yield queue.shift()!;\n continue;\n }\n if (done) break;\n const { value, done: streamDone } = await reader.read();\n if (streamDone) break;\n parser.feed(decoder.decode(value, { stream: true }));\n }\n while (queue.length > 0) yield queue.shift()!;\n } finally {\n clearTimeout(timer);\n reader.releaseLock();\n }\n }\n}\n\nexport type { ChatMessage, ToolCall, ToolSpec };\n","/**\n * Retry layer for DeepSeek API calls.\n *\n * Wraps a `fetch` function so that transient failures (rate limiting, server\n * overload, network blips) don't kill an agent session. We explicitly DO NOT\n * retry:\n * - 4xx client errors other than 408 / 429 (bad key, bad request, ...)\n * - aborted requests (user cancelled)\n * - mid-stream body read errors (retrying costs money AND would desync)\n *\n * Retrying is controlled by attempt count + exponential backoff with jitter.\n * If the server sends a `Retry-After` header we honor it (capped by\n * `maxBackoffMs` so a misconfigured upstream can't park us forever).\n */\n\nexport interface RetryOptions {\n /** Maximum total attempts (including the first). Default 4. */\n maxAttempts?: number;\n /** Initial backoff in ms. Doubles each retry, with jitter. Default 500. */\n initialBackoffMs?: number;\n /** Upper bound on any single backoff delay. Default 10000 (10s). */\n maxBackoffMs?: number;\n /** HTTP statuses to treat as retryable. Default [408, 429, 500, 502, 503, 504]. */\n retryableStatuses?: readonly number[];\n /** Abort signal; we do NOT retry once aborted. */\n signal?: AbortSignal;\n /** Telemetry hook — called before each wait. */\n onRetry?: (info: RetryInfo) => void;\n}\n\nexport interface RetryInfo {\n attempt: number;\n reason: string;\n waitMs: number;\n}\n\nconst DEFAULT_RETRYABLE_STATUSES = [408, 429, 500, 502, 503, 504] as const;\n\nexport async function fetchWithRetry(\n fetchFn: typeof fetch,\n url: string,\n init: RequestInit,\n opts: RetryOptions = {},\n): Promise<Response> {\n const maxAttempts = opts.maxAttempts ?? 4;\n const initial = opts.initialBackoffMs ?? 500;\n const cap = opts.maxBackoffMs ?? 10_000;\n const retryable = new Set(opts.retryableStatuses ?? DEFAULT_RETRYABLE_STATUSES);\n\n let lastError: unknown;\n\n for (let attempt = 0; attempt < maxAttempts; attempt++) {\n if (opts.signal?.aborted) throw new Error(\"aborted\");\n\n try {\n const resp = await fetchFn(url, init);\n\n // Success or non-retryable failure: return as-is.\n if (resp.ok || !retryable.has(resp.status)) return resp;\n\n // Retryable but out of attempts: return the last response so the caller\n // can surface the status to the user.\n if (attempt === maxAttempts - 1) return resp;\n\n // Drain the body so the connection can be reused on the next attempt.\n await resp.text().catch(() => undefined);\n\n const waitMs = computeWait(attempt, initial, cap, resp.headers.get(\"Retry-After\"));\n opts.onRetry?.({ attempt: attempt + 1, reason: `http ${resp.status}`, waitMs });\n await sleep(waitMs, opts.signal);\n } catch (err) {\n lastError = err;\n // Respect explicit aborts — do not retry.\n if (isAbortError(err) || opts.signal?.aborted) throw err;\n if (attempt === maxAttempts - 1) throw err;\n\n const waitMs = computeWait(attempt, initial, cap, null);\n opts.onRetry?.({\n attempt: attempt + 1,\n reason: `network: ${messageOf(err)}`,\n waitMs,\n });\n await sleep(waitMs, opts.signal);\n }\n }\n\n throw lastError ?? new Error(\"fetchWithRetry: loop exited unexpectedly\");\n}\n\nfunction computeWait(\n attempt: number,\n initial: number,\n cap: number,\n retryAfter: string | null,\n): number {\n if (retryAfter) {\n const seconds = Number.parseFloat(retryAfter);\n if (Number.isFinite(seconds) && seconds > 0) {\n return Math.min(seconds * 1000, cap);\n }\n }\n const exp = initial * 2 ** attempt;\n // Jitter range [75%, 125%] to spread retries out when many clients hit 429 together.\n const jitter = exp * (0.75 + Math.random() * 0.5);\n return Math.min(Math.max(jitter, 0), cap);\n}\n\nfunction sleep(ms: number, signal?: AbortSignal): Promise<void> {\n if (ms <= 0) return Promise.resolve();\n return new Promise((resolve, reject) => {\n const timer = setTimeout(resolve, ms);\n if (signal) {\n const onAbort = () => {\n clearTimeout(timer);\n reject(new Error(\"aborted\"));\n };\n if (signal.aborted) onAbort();\n else signal.addEventListener(\"abort\", onAbort, { once: true });\n }\n });\n}\n\nfunction isAbortError(err: unknown): boolean {\n if (!err || typeof err !== \"object\") return false;\n const name = (err as { name?: unknown }).name;\n return name === \"AbortError\";\n}\n\nfunction messageOf(err: unknown): string {\n if (err instanceof Error) return err.message;\n try {\n return String(err);\n } catch {\n return \"unknown error\";\n }\n}\n","/**\n * Pillar 2 — R1 Thought Harvesting.\n *\n * Takes the `reasoning_content` emitted by a thinking model (deepseek-reasoner\n * / R1) and extracts a structured plan state by making a cheap secondary call\n * to V3 in JSON mode. The typed state is intended for the orchestrator to\n * branch on — e.g. trigger self-consistency sampling when `uncertainties.length\n * > 2`, or surface the subgoals to the user.\n *\n * Opt-in: loops disable harvesting by default. Failures (bad JSON, API error,\n * empty reasoning) return an empty TypedPlanState — the main turn is never\n * aborted because of a harvest hiccup.\n */\n\nimport type { DeepSeekClient } from \"./client.js\";\n\nexport interface TypedPlanState {\n subgoals: string[];\n hypotheses: string[];\n uncertainties: string[];\n rejectedPaths: string[];\n}\n\nexport interface HarvestOptions {\n /** Model used for the extraction call. Defaults to the cheap chat model. */\n model?: string;\n /** Cap on how many items land in each array. Default 5. */\n maxItems?: number;\n /** Per-item character cap. Default 80. */\n maxItemLen?: number;\n /** Abort the extraction if R1 reasoning is shorter than this. Default 40. */\n minReasoningLen?: number;\n}\n\nexport function emptyPlanState(): TypedPlanState {\n return { subgoals: [], hypotheses: [], uncertainties: [], rejectedPaths: [] };\n}\n\nexport function isPlanStateEmpty(s: TypedPlanState | null | undefined): boolean {\n if (!s) return true;\n return (\n s.subgoals.length === 0 &&\n s.hypotheses.length === 0 &&\n s.uncertainties.length === 0 &&\n s.rejectedPaths.length === 0\n );\n}\n\nconst SYSTEM_PROMPT = `You extract a typed plan state from a reasoning trace produced by another LLM.\nOutput ONLY a JSON object. No markdown, no prose, no backticks.\n\nSchema:\n{\n \"subgoals\": string[], // concrete intermediate objectives the trace identifies\n \"hypotheses\": string[], // candidate approaches or assumptions being weighed\n \"uncertainties\": string[], // facts the trace flags as unclear / to verify\n \"rejectedPaths\": string[] // approaches the trace considered and then abandoned\n}\n\nConstraints:\n- Every field must be present. Use [] if not applicable.\n- Each array has at most {maxItems} items.\n- Each item is plain text, at most {maxItemLen} characters, no markdown.\n- Write in the same language as the trace (Chinese in → Chinese out, etc.).\n- Do not quote back the trace; write short, specific phrases.`;\n\nexport async function harvest(\n reasoningContent: string | null | undefined,\n client?: DeepSeekClient,\n options: HarvestOptions = {},\n signal?: AbortSignal,\n): Promise<TypedPlanState> {\n if (!client || !reasoningContent) return emptyPlanState();\n // Fast-path the already-aborted case so we don't burn a network\n // round-trip for a result the caller no longer wants.\n if (signal?.aborted) return emptyPlanState();\n const minLen = options.minReasoningLen ?? 40;\n const trimmed = reasoningContent.trim();\n if (trimmed.length < minLen) return emptyPlanState();\n\n const model = options.model ?? \"deepseek-chat\";\n const maxItems = options.maxItems ?? 5;\n const maxItemLen = options.maxItemLen ?? 80;\n const system = SYSTEM_PROMPT.replace(\"{maxItems}\", String(maxItems)).replace(\n \"{maxItemLen}\",\n String(maxItemLen),\n );\n\n try {\n const resp = await client.chat({\n model,\n messages: [\n { role: \"system\", content: system },\n { role: \"user\", content: trimmed },\n ],\n responseFormat: { type: \"json_object\" },\n temperature: 0,\n maxTokens: 600,\n signal,\n });\n return parsePlanState(resp.content, maxItems, maxItemLen);\n } catch {\n return emptyPlanState();\n }\n}\n\nfunction parsePlanState(raw: string, maxItems: number, maxItemLen: number): TypedPlanState {\n const text = (raw ?? \"\").trim();\n if (!text) return emptyPlanState();\n let parsed: unknown;\n try {\n parsed = JSON.parse(text);\n } catch {\n // Occasionally a model wraps JSON in fences despite instructions.\n const match = text.match(/\\{[\\s\\S]*\\}/);\n if (!match) return emptyPlanState();\n try {\n parsed = JSON.parse(match[0]);\n } catch {\n return emptyPlanState();\n }\n }\n if (!parsed || typeof parsed !== \"object\") return emptyPlanState();\n const obj = parsed as Record<string, unknown>;\n return {\n subgoals: sanitizeArray(obj.subgoals, maxItems, maxItemLen),\n hypotheses: sanitizeArray(obj.hypotheses, maxItems, maxItemLen),\n uncertainties: sanitizeArray(obj.uncertainties, maxItems, maxItemLen),\n rejectedPaths: sanitizeArray(obj.rejectedPaths ?? obj.rejected_paths, maxItems, maxItemLen),\n };\n}\n\nfunction sanitizeArray(raw: unknown, maxItems: number, maxItemLen: number): string[] {\n if (!Array.isArray(raw)) return [];\n const out: string[] = [];\n for (const item of raw) {\n if (out.length >= maxItems) break;\n if (typeof item !== \"string\") continue;\n const cleaned = item.trim().replace(/\\s+/g, \" \");\n if (!cleaned) continue;\n out.push(cleaned.length <= maxItemLen ? cleaned : `${cleaned.slice(0, maxItemLen - 1)}…`);\n }\n return out;\n}\n","/**\n * Self-consistency branching.\n *\n * When enabled, the loop fans out into N parallel samples per turn (varied\n * temperatures), runs Pillar 2 harvest on each, and selects the sample with\n * the fewest flagged uncertainties (ties broken by answer length — a crude\n * Occam prior).\n *\n * The unique opportunity here: because DeepSeek is ~20× cheaper than Claude,\n * running N=3–5 samples per turn is still cheaper than a single Claude call,\n * while the majority-confidence selection tends to dominate single-sample\n * answers on fuzzy multi-step reasoning tasks.\n */\n\nimport type { ChatResponse, DeepSeekClient } from \"./client.js\";\nimport { type HarvestOptions, type TypedPlanState, harvest } from \"./harvest.js\";\nimport type { ChatRequestOptions } from \"./types.js\";\n\nexport interface BranchSample {\n index: number;\n temperature: number;\n response: ChatResponse;\n planState: TypedPlanState;\n}\n\nexport type BranchSelector = (samples: BranchSample[]) => BranchSample;\n\nexport interface BranchOptions {\n /** Number of parallel samples. 1 disables branching. Default 1. */\n budget?: number;\n /** Temperatures for each branch. Default spreads across [0, 1]. */\n temperatures?: readonly number[];\n /** Harvest options; the selector needs harvest to score samples. */\n harvestOptions?: HarvestOptions;\n /** Custom selector. Default: min uncertainties, tie-break shortest answer. */\n selector?: BranchSelector;\n /**\n * Fires as each sample finishes (main call + harvest both complete).\n * Useful for progress UI. Not awaited; exceptions are swallowed.\n */\n onSampleDone?: (sample: BranchSample) => void;\n}\n\nexport interface BranchResult {\n chosen: BranchSample;\n samples: BranchSample[];\n}\n\n/** Default: fewest uncertainties wins, ties broken by shorter answer content. */\nexport const defaultSelector: BranchSelector = (samples) => {\n if (samples.length === 0) throw new Error(\"defaultSelector: samples is empty\");\n return samples.slice().sort((a, b) => {\n const uDiff = a.planState.uncertainties.length - b.planState.uncertainties.length;\n if (uDiff !== 0) return uDiff;\n const aLen = a.response.content?.length ?? 0;\n const bLen = b.response.content?.length ?? 0;\n return aLen - bLen;\n })[0]!;\n};\n\nexport async function runBranches(\n client: DeepSeekClient,\n request: ChatRequestOptions,\n opts: BranchOptions = {},\n): Promise<BranchResult> {\n const budget = Math.max(1, opts.budget ?? 1);\n const temperatures = resolveTemperatures(budget, opts.temperatures);\n const selector = opts.selector ?? defaultSelector;\n\n const samples = await Promise.all(\n temperatures.map(async (temperature, index): Promise<BranchSample> => {\n const response = await client.chat({ ...request, temperature });\n const planState = await harvest(response.reasoningContent, client, opts.harvestOptions);\n const sample: BranchSample = { index, temperature, response, planState };\n try {\n opts.onSampleDone?.(sample);\n } catch {\n /* callback errors must not poison the await */\n }\n return sample;\n }),\n );\n\n return { chosen: selector(samples), samples };\n}\n\n/** Sum usage across branch samples for telemetry purposes. */\nexport function aggregateBranchUsage(samples: readonly BranchSample[]) {\n let promptTokens = 0;\n let completionTokens = 0;\n let totalTokens = 0;\n let promptCacheHitTokens = 0;\n let promptCacheMissTokens = 0;\n for (const s of samples) {\n promptTokens += s.response.usage.promptTokens;\n completionTokens += s.response.usage.completionTokens;\n totalTokens += s.response.usage.totalTokens;\n promptCacheHitTokens += s.response.usage.promptCacheHitTokens;\n promptCacheMissTokens += s.response.usage.promptCacheMissTokens;\n }\n return {\n promptTokens,\n completionTokens,\n totalTokens,\n promptCacheHitTokens,\n promptCacheMissTokens,\n };\n}\n\nfunction resolveTemperatures(budget: number, custom?: readonly number[]): number[] {\n if (custom && custom.length >= budget) return [...custom.slice(0, budget)];\n // Spread evenly across [0, 1] to encourage reasoning-path diversity.\n if (budget === 1) return [0];\n const out: number[] = [];\n for (let i = 0; i < budget; i++) {\n out.push(Number((i / (budget - 1)).toFixed(2)));\n }\n return out;\n}\n","/**\n * Hooks — user-defined automation that fires at well-known points in\n * the agent loop. Mirrors the two-scope layout we use for memory and\n * skills:\n *\n * - `<project>/.reasonix/settings.json` — committable per-project\n * - `~/.reasonix/settings.json` — every session\n *\n * A hook is a shell command. We invoke it with stdin = a JSON\n * payload describing the event, and interpret the exit code:\n *\n * - `0` — pass; loop continues normally\n * - `2` — block; for `PreToolUse` / `UserPromptSubmit` the\n * loop refuses to continue with that step and surfaces the\n * hook's stderr as the reason. For `PostToolUse` / `Stop` block\n * is meaningless (the action already happened) — treat as warn.\n * - anything else — warn; loop continues but stderr is rendered\n * to the user as an inline notice.\n *\n * stdin JSON shape (one envelope per event):\n *\n * {\n * \"event\": \"PreToolUse\" | \"PostToolUse\" | \"UserPromptSubmit\" | \"Stop\",\n * \"cwd\": \"<absolute project root or process.cwd()>\",\n * \"toolName\": \"<string>\", // tool events only\n * \"toolArgs\": <unknown>, // tool events only — already JSON-decoded\n * \"toolResult\": \"<string>\", // PostToolUse only — same body the model sees\n * \"prompt\": \"<string>\", // UserPromptSubmit only\n * \"lastAssistantText\": \"<string>\", // Stop only\n * \"turn\": <number>, // Stop only\n * }\n *\n * Hooks are executed in order: project scope first, then global.\n * `Pre*` events stop dispatching at the first block; non-block\n * outcomes accumulate into a single report so the UI can render\n * each warning inline.\n */\n\nimport { spawn } from \"node:child_process\";\nimport { existsSync, readFileSync } from \"node:fs\";\nimport { homedir } from \"node:os\";\nimport { join } from \"node:path\";\n\nexport type HookEvent = \"PreToolUse\" | \"PostToolUse\" | \"UserPromptSubmit\" | \"Stop\";\n\n/** All four events as a const array — drives slash listing + validation. */\nexport const HOOK_EVENTS: readonly HookEvent[] = [\n \"PreToolUse\",\n \"PostToolUse\",\n \"UserPromptSubmit\",\n \"Stop\",\n] as const;\n\n/** Only the gating events can block the loop. */\nconst BLOCKING_EVENTS: ReadonlySet<HookEvent> = new Set([\"PreToolUse\", \"UserPromptSubmit\"]);\n\n/** Per-event default timeout. Tool/prompt hooks gate progress, so they're tight. */\nconst DEFAULT_TIMEOUTS_MS: Record<HookEvent, number> = {\n PreToolUse: 5_000,\n UserPromptSubmit: 5_000,\n PostToolUse: 30_000,\n Stop: 30_000,\n};\n\nexport type HookScope = \"project\" | \"global\";\n\nexport interface HookConfig {\n /**\n * Tool-name pattern (PreToolUse / PostToolUse only). Anchored regex.\n * Omitted or `\"*\"` matches every tool. Ignored for prompt / Stop\n * events (they have no tool name to match against).\n */\n match?: string;\n /** Shell command to run. Spawned through the platform shell. */\n command: string;\n /** Optional human description — surfaced in `/hooks`. */\n description?: string;\n /** Per-hook timeout override in ms. */\n timeout?: number;\n /**\n * Working directory for the spawned process. Defaults to:\n * - project scope → the project root\n * - global scope → process.cwd()\n */\n cwd?: string;\n}\n\n/** Shape of `<scope>/.reasonix/settings.json` — only `hooks` for now. */\nexport interface HookSettings {\n hooks?: Partial<Record<HookEvent, HookConfig[]>>;\n}\n\n/** A loaded hook with its origin scope baked in (used for ordering and `/hooks`). */\nexport interface ResolvedHook extends HookConfig {\n event: HookEvent;\n scope: HookScope;\n /** Absolute path to the settings.json the hook came from. */\n source: string;\n}\n\n/** Outcome of a single hook invocation. */\nexport interface HookOutcome {\n /** Which hook fired. */\n hook: ResolvedHook;\n /**\n * Decision:\n * - `pass` — exit 0\n * - `block` — exit 2 on a blocking event (otherwise downgraded to `warn`)\n * - `warn` — non-zero exit that is not a successful block\n * - `timeout` — the spawn was killed past `timeout`\n * - `error` — could not spawn at all (missing command, etc.)\n */\n decision: \"pass\" | \"block\" | \"warn\" | \"timeout\" | \"error\";\n exitCode: number | null;\n /** Captured stdout (trimmed). May be empty. */\n stdout: string;\n /** Captured stderr (trimmed). The block / warn message comes from here. */\n stderr: string;\n durationMs: number;\n}\n\n/** Aggregate report for `runHooks`. */\nexport interface HookReport {\n event: HookEvent;\n outcomes: HookOutcome[];\n /** True iff at least one outcome was a `block` — only meaningful for blocking events. */\n blocked: boolean;\n}\n\nexport const HOOK_SETTINGS_FILENAME = \"settings.json\";\nexport const HOOK_SETTINGS_DIRNAME = \".reasonix\";\n\n/** Where the global settings.json lives. Equivalent to `~/.reasonix/settings.json`. */\nexport function globalSettingsPath(homeDirOverride?: string): string {\n return join(homeDirOverride ?? homedir(), HOOK_SETTINGS_DIRNAME, HOOK_SETTINGS_FILENAME);\n}\n\n/** Where the project settings.json lives for a given root. */\nexport function projectSettingsPath(projectRoot: string): string {\n return join(projectRoot, HOOK_SETTINGS_DIRNAME, HOOK_SETTINGS_FILENAME);\n}\n\nfunction readSettingsFile(path: string): HookSettings | null {\n if (!existsSync(path)) return null;\n try {\n const raw = readFileSync(path, \"utf8\");\n const parsed = JSON.parse(raw);\n if (parsed && typeof parsed === \"object\") return parsed as HookSettings;\n } catch {\n /* malformed JSON → treat as no hooks; do NOT throw, the user\n * shouldn't lose the whole CLI to a typo in their settings */\n }\n return null;\n}\n\n/**\n * Pull every configured hook out of the project + global settings\n * files, in the order they should fire (project first, global second,\n * within each scope: array order from the file).\n *\n * Returns a flat list — the dispatcher filters by event + match\n * pattern at run time. Loading is cheap (one or two JSON files), so\n * we don't memoize across processes; re-load is allowed via\n * `/hooks reload` and on every fresh App mount.\n */\nexport interface LoadHookSettingsOptions {\n /** Absolute project root, if any. Without it, only global hooks load. */\n projectRoot?: string;\n /** Override `~` for tests. */\n homeDir?: string;\n}\n\nexport function loadHooks(opts: LoadHookSettingsOptions = {}): ResolvedHook[] {\n const out: ResolvedHook[] = [];\n if (opts.projectRoot) {\n const projPath = projectSettingsPath(opts.projectRoot);\n const settings = readSettingsFile(projPath);\n if (settings) appendResolved(out, settings, \"project\", projPath);\n }\n const globalPath = globalSettingsPath(opts.homeDir);\n const settings = readSettingsFile(globalPath);\n if (settings) appendResolved(out, settings, \"global\", globalPath);\n return out;\n}\n\nfunction appendResolved(\n out: ResolvedHook[],\n settings: HookSettings,\n scope: HookScope,\n source: string,\n): void {\n if (!settings.hooks) return;\n for (const event of HOOK_EVENTS) {\n const list = settings.hooks[event];\n if (!Array.isArray(list)) continue;\n for (const cfg of list) {\n if (!cfg || typeof cfg.command !== \"string\" || cfg.command.trim() === \"\") continue;\n out.push({ ...cfg, event, scope, source });\n }\n }\n}\n\n/**\n * True if `toolName` matches the hook's `match` field. `\"*\"` and\n * undefined match everything. Otherwise we anchor the field as a\n * regex — partial-name matches don't fire, so `\"file\"` would not\n * trigger on `read_file` (use `\".*file\"` for that).\n */\nexport function matchesTool(hook: ResolvedHook, toolName: string): boolean {\n if (hook.event !== \"PreToolUse\" && hook.event !== \"PostToolUse\") return true;\n const m = hook.match;\n if (!m || m === \"*\") return true;\n try {\n const re = new RegExp(`^(?:${m})$`);\n return re.test(toolName);\n } catch {\n /* malformed regex → don't fire (safer than firing on every tool) */\n return false;\n }\n}\n\n/** Payload envelope passed to hook stdin. */\nexport interface HookPayload {\n event: HookEvent;\n cwd: string;\n toolName?: string;\n toolArgs?: unknown;\n toolResult?: string;\n prompt?: string;\n lastAssistantText?: string;\n turn?: number;\n}\n\n/** Test seam — same shape as Node's spawn but returns a Promise of the raw outcome bits. */\nexport interface HookSpawnInput {\n command: string;\n cwd: string;\n stdin: string;\n timeoutMs: number;\n}\n\nexport interface HookSpawnResult {\n exitCode: number | null;\n stdout: string;\n stderr: string;\n timedOut: boolean;\n /** True iff spawn() itself failed (ENOENT, EACCES, …). */\n spawnError?: Error;\n}\n\nexport type HookSpawner = (input: HookSpawnInput) => Promise<HookSpawnResult>;\n\n/**\n * Default spawner — runs `command` through the platform shell so\n * `&&`, pipes, env-var expansion all work without a tokenizer.\n * Stdin is the JSON payload, stdout / stderr are buffered.\n *\n * Why `shell: true`? A hook is intentionally a shell command — that's\n * the contract. Treating it like an argv array would surprise users\n * who write `bun run check && eslint .` and expect it to behave the\n * way it does in their terminal.\n */\nfunction defaultSpawner(input: HookSpawnInput): Promise<HookSpawnResult> {\n return new Promise<HookSpawnResult>((resolve) => {\n const child = spawn(input.command, {\n cwd: input.cwd,\n shell: true,\n stdio: [\"pipe\", \"pipe\", \"pipe\"],\n });\n let stdout = \"\";\n let stderr = \"\";\n let timedOut = false;\n const timer = setTimeout(() => {\n timedOut = true;\n child.kill(\"SIGTERM\");\n // SIGTERM may not land on Windows for shell children — followed\n // by a hard kill a moment later if the process is still around.\n setTimeout(() => {\n try {\n child.kill(\"SIGKILL\");\n } catch {\n /* already gone */\n }\n }, 500);\n }, input.timeoutMs);\n\n child.stdout.on(\"data\", (chunk: Buffer) => {\n stdout += chunk.toString(\"utf8\");\n });\n child.stderr.on(\"data\", (chunk: Buffer) => {\n stderr += chunk.toString(\"utf8\");\n });\n child.once(\"error\", (err) => {\n clearTimeout(timer);\n resolve({\n exitCode: null,\n stdout,\n stderr,\n timedOut: false,\n spawnError: err,\n });\n });\n child.once(\"close\", (code) => {\n clearTimeout(timer);\n resolve({\n exitCode: code,\n stdout: stdout.trim(),\n stderr: stderr.trim(),\n timedOut,\n });\n });\n\n try {\n child.stdin.write(input.stdin);\n child.stdin.end();\n } catch {\n /* stdin write can race with spawn errors; the close handler\n * still fires with exit 0/null */\n }\n });\n}\n\n/**\n * Format a hook outcome as a single-line UI string. Used by both the\n * loop (for `warning` events) and the App (for UserPromptSubmit /\n * Stop outcomes). Centralizing keeps the language consistent across\n * scopes.\n */\nexport function formatHookOutcomeMessage(outcome: HookOutcome): string {\n if (outcome.decision === \"pass\") return \"\";\n const detail = (outcome.stderr || outcome.stdout || \"\").trim();\n const tag = `${outcome.hook.scope}/${outcome.hook.event}`;\n const cmd =\n outcome.hook.command.length > 60\n ? `${outcome.hook.command.slice(0, 60)}…`\n : outcome.hook.command;\n const head = `hook ${tag} \\`${cmd}\\` ${outcome.decision}`;\n return detail ? `${head}: ${detail}` : head;\n}\n\n/**\n * Decide the hook's outcome decision from raw spawn results.\n * Pulled out as a pure function so tests can pin the matrix.\n */\nexport function decideOutcome(\n event: HookEvent,\n raw: HookSpawnResult,\n): \"pass\" | \"block\" | \"warn\" | \"timeout\" | \"error\" {\n if (raw.spawnError) return \"error\";\n if (raw.timedOut) return BLOCKING_EVENTS.has(event) ? \"block\" : \"warn\";\n if (raw.exitCode === 0) return \"pass\";\n if (raw.exitCode === 2 && BLOCKING_EVENTS.has(event)) return \"block\";\n return \"warn\";\n}\n\nexport interface RunHooksOptions {\n payload: HookPayload;\n hooks: ResolvedHook[];\n /** Test seam — defaults to a real `spawn`. */\n spawner?: HookSpawner;\n}\n\n/**\n * Filter hooks down to the ones that match `payload.event` (and\n * `payload.toolName`, for tool events), then run them in order.\n * Stops at the first `block` outcome on a blocking event so a\n * gating hook can prevent later hooks from incorrectly seeing a\n * success that wasn't going to happen.\n */\nexport async function runHooks(opts: RunHooksOptions): Promise<HookReport> {\n const spawner = opts.spawner ?? defaultSpawner;\n const event = opts.payload.event;\n const toolName = opts.payload.toolName ?? \"\";\n const matching = opts.hooks.filter((h) => h.event === event && matchesTool(h, toolName));\n\n const outcomes: HookOutcome[] = [];\n let blocked = false;\n const stdin = `${JSON.stringify(opts.payload)}\\n`;\n\n for (const hook of matching) {\n const start = Date.now();\n const timeoutMs = hook.timeout ?? DEFAULT_TIMEOUTS_MS[event];\n const cwd = hook.cwd ?? opts.payload.cwd;\n const raw = await spawner({ command: hook.command, cwd, stdin, timeoutMs });\n const decision = decideOutcome(event, raw);\n outcomes.push({\n hook,\n decision,\n exitCode: raw.exitCode,\n stdout: raw.stdout,\n stderr:\n raw.stderr ||\n (raw.spawnError ? raw.spawnError.message : \"\") ||\n (raw.timedOut ? `hook timed out after ${timeoutMs}ms` : \"\"),\n durationMs: Date.now() - start,\n });\n if (decision === \"block\") {\n blocked = true;\n break;\n }\n }\n\n return { event, outcomes, blocked };\n}\n","/**\n * Schema flattening for DeepSeek tool calls.\n *\n * DeepSeek loses arguments on schemas that are deep (>2 levels of nesting) or\n * wide (>10 leaf parameters). This module transforms such schemas into a\n * dot-notation flat schema and re-nests the model's arguments before dispatch.\n *\n * Example:\n * { user: { profile: { name, age } } } ⇄ \"user.profile.name\", \"user.profile.age\"\n */\n\nimport type { JSONSchema } from \"../types.js\";\n\nexport interface FlattenDecision {\n shouldFlatten: boolean;\n leafCount: number;\n maxDepth: number;\n}\n\nexport function analyzeSchema(schema: JSONSchema | undefined): FlattenDecision {\n if (!schema) return { shouldFlatten: false, leafCount: 0, maxDepth: 0 };\n let leafCount = 0;\n let maxDepth = 0;\n walk(schema, 0, (depth, isLeaf) => {\n if (isLeaf) leafCount++;\n if (depth > maxDepth) maxDepth = depth;\n });\n return {\n shouldFlatten: leafCount > 10 || maxDepth > 2,\n leafCount,\n maxDepth,\n };\n}\n\nexport function flattenSchema(schema: JSONSchema): JSONSchema {\n const flatProps: Record<string, JSONSchema> = {};\n const required: string[] = [];\n collect(\"\", schema, flatProps, required, true);\n return {\n type: \"object\",\n properties: flatProps,\n required,\n };\n}\n\nexport function nestArguments(flatArgs: Record<string, unknown>): Record<string, unknown> {\n const out: Record<string, unknown> = {};\n for (const [key, value] of Object.entries(flatArgs)) {\n setByPath(out, key.split(\".\"), value);\n }\n return out;\n}\n\nfunction walk(\n schema: JSONSchema,\n depth: number,\n visit: (depth: number, isLeaf: boolean) => void,\n): void {\n if (schema.type === \"object\" && schema.properties) {\n for (const child of Object.values(schema.properties)) {\n walk(child, depth + 1, visit);\n }\n return;\n }\n if (schema.type === \"array\" && schema.items) {\n walk(schema.items, depth + 1, visit);\n return;\n }\n visit(depth, true);\n}\n\nfunction collect(\n prefix: string,\n schema: JSONSchema,\n out: Record<string, JSONSchema>,\n required: string[],\n isRootRequired: boolean,\n): void {\n if (schema.type === \"object\" && schema.properties) {\n const requiredSet = new Set(schema.required ?? []);\n for (const [key, child] of Object.entries(schema.properties)) {\n const nextPrefix = prefix ? `${prefix}.${key}` : key;\n const childRequired = isRootRequired && requiredSet.has(key);\n collect(nextPrefix, child, out, required, childRequired);\n }\n return;\n }\n // Treat anything non-object (including arrays) as a leaf for flattening purposes.\n out[prefix] = schema;\n if (isRootRequired) required.push(prefix);\n}\n\nfunction setByPath(target: Record<string, unknown>, path: string[], value: unknown): void {\n let cur: any = target;\n for (let i = 0; i < path.length - 1; i++) {\n const key = path[i]!;\n if (typeof cur[key] !== \"object\" || cur[key] === null) cur[key] = {};\n cur = cur[key];\n }\n cur[path[path.length - 1]!] = value;\n}\n","import { analyzeSchema, flattenSchema, nestArguments } from \"./repair/flatten.js\";\nimport type { JSONSchema, ToolSpec } from \"./types.js\";\n\n/**\n * Per-call context a tool `fn` can optionally consume. Today the only\n * field is `signal`, plumbed through so long-running tools (MCP calls,\n * HTTP requests) can abort when the user presses Esc. Omitted fields\n * stay optional — tools written against the pre-0.4.9 signature keep\n * working; they just ignore cancellation, which is fine for fast\n * local work where \"await finishes\" happens before the next tick anyway.\n */\nexport interface ToolCallContext {\n signal?: AbortSignal;\n}\n\nexport interface ToolDefinition<A = any, R = any> {\n name: string;\n description?: string;\n parameters?: JSONSchema;\n /**\n * Marks a tool as read-only: safe to invoke during plan mode. `true`\n * for tools that only observe (read_file, list_directory, search, web\n * fetch/search). Leave undefined / `false` for anything that can write,\n * execute, or mutate state.\n *\n * The registry enforces this at dispatch: non-readonly tools called\n * while `planMode` is on return a refusal string the model can\n * learn from, instead of actually running.\n */\n readOnly?: boolean;\n /**\n * Dynamic read-only check for tools whose safety depends on arguments\n * — `run_command` with an allowlisted argv is safe, `run_command\n * rm -rf` isn't. Called with the parsed arguments; `true` means \"treat\n * as read-only for plan mode\". Takes precedence over `readOnly` when\n * both are set.\n */\n readOnlyCheck?: (args: A) => boolean;\n fn: (args: A, ctx?: ToolCallContext) => R | Promise<R>;\n}\n\ninterface InternalTool extends ToolDefinition {\n /**\n * Pillar 3 — flatten metadata. Set when the registered schema is deep\n * (>2 levels) or wide (>10 leaf params), conditions on which DeepSeek\n * V3/R1 are known to drop arguments. We advertise the flattened schema\n * to the model, then re-nest the model's args before calling fn.\n */\n flatSchema?: JSONSchema;\n}\n\nexport interface ToolRegistryOptions {\n /**\n * Auto-flatten schemas that exceed depth/width thresholds before sending\n * them to the model. Re-nests arguments transparently on dispatch.\n * Default: true. Pass false to opt out.\n */\n autoFlatten?: boolean;\n}\n\nexport class ToolRegistry {\n private readonly _tools = new Map<string, InternalTool>();\n private readonly _autoFlatten: boolean;\n /**\n * When true, `dispatch` refuses any tool whose `readOnly` flag isn't\n * set (and whose `readOnlyCheck` doesn't pass on the specific args).\n * Drives `reasonix code`'s Plan Mode — the model can still explore\n * via read tools but its writes and non-allowlisted shell calls are\n * bounced until the user approves a submitted plan.\n */\n private _planMode = false;\n\n constructor(opts: ToolRegistryOptions = {}) {\n this._autoFlatten = opts.autoFlatten !== false;\n }\n\n /** Enable / disable plan-mode enforcement at dispatch. */\n setPlanMode(on: boolean): void {\n this._planMode = Boolean(on);\n }\n\n /** True when the registry is currently refusing non-readonly calls. */\n get planMode(): boolean {\n return this._planMode;\n }\n\n register<A, R>(def: ToolDefinition<A, R>): this {\n if (!def.name) throw new Error(\"tool requires a name\");\n const internal: InternalTool = { ...(def as ToolDefinition) };\n if (this._autoFlatten && def.parameters) {\n const decision = analyzeSchema(def.parameters);\n if (decision.shouldFlatten) {\n internal.flatSchema = flattenSchema(def.parameters);\n }\n }\n this._tools.set(def.name, internal);\n return this;\n }\n\n has(name: string): boolean {\n return this._tools.has(name);\n }\n\n get(name: string): ToolDefinition | undefined {\n return this._tools.get(name);\n }\n\n get size(): number {\n return this._tools.size;\n }\n\n /** True if a registered tool's schema was flattened for the model. */\n wasFlattened(name: string): boolean {\n return Boolean(this._tools.get(name)?.flatSchema);\n }\n\n specs(): ToolSpec[] {\n return [...this._tools.values()].map((t) => ({\n type: \"function\",\n function: {\n name: t.name,\n description: t.description ?? \"\",\n parameters: t.flatSchema ?? t.parameters ?? { type: \"object\", properties: {} },\n },\n }));\n }\n\n async dispatch(\n name: string,\n argumentsRaw: string | Record<string, unknown>,\n opts: { signal?: AbortSignal } = {},\n ): Promise<string> {\n const tool = this._tools.get(name);\n if (!tool) {\n return JSON.stringify({ error: `unknown tool: ${name}` });\n }\n let args: Record<string, unknown>;\n try {\n args =\n typeof argumentsRaw === \"string\"\n ? argumentsRaw.trim()\n ? (JSON.parse(argumentsRaw) ?? {})\n : {}\n : (argumentsRaw ?? {});\n } catch (err) {\n return JSON.stringify({\n error: `invalid tool arguments JSON: ${(err as Error).message}`,\n });\n }\n\n // Re-nest dot-notation args back to the original shape, but only when\n // (a) we flattened this tool's schema, AND\n // (b) the incoming args actually use dot keys.\n // The second condition handles the case where a model ignores the flat\n // spec and emits nested args anyway — we shouldn't double-process them.\n if (tool.flatSchema && args && typeof args === \"object\" && hasDotKey(args)) {\n args = nestArguments(args);\n }\n\n // Plan-mode enforcement — runs AFTER arg parsing so a tool with a\n // runtime `readOnlyCheck` can inspect the actual args (e.g.\n // `run_command` is read-only iff the command matches its allowlist).\n if (this._planMode && !isReadOnlyCall(tool, args)) {\n return JSON.stringify({\n error: `${name}: unavailable in plan mode — this is a read-only exploration phase. Use read_file / list_directory / search_files / directory_tree / web_search / allowlisted shell commands to investigate. Call submit_plan with your proposed plan when you're ready for the user's review.`,\n });\n }\n\n try {\n const result = await tool.fn(args, { signal: opts.signal });\n return typeof result === \"string\" ? result : JSON.stringify(result);\n } catch (err) {\n const e = err as Error & { toToolResult?: () => unknown };\n // Errors may opt into a richer tool-result shape by implementing\n // `toToolResult()`. Used by `PlanProposedError` to smuggle the\n // submitted plan text out to the UI without stuffing it into the\n // error message (which the dispatcher truncates at no fixed limit,\n // but keeping payloads structured is cleaner for UI parsing).\n if (typeof e.toToolResult === \"function\") {\n try {\n return JSON.stringify(e.toToolResult());\n } catch {\n /* fall through to the default shape */\n }\n }\n return JSON.stringify({\n error: `${e.name}: ${e.message}`,\n });\n }\n }\n}\n\nfunction isReadOnlyCall(tool: InternalTool, args: Record<string, unknown>): boolean {\n if (tool.readOnlyCheck) {\n try {\n return Boolean(tool.readOnlyCheck(args as never));\n } catch {\n return false;\n }\n }\n return tool.readOnly === true;\n}\n\nfunction hasDotKey(obj: Record<string, unknown>): boolean {\n for (const k of Object.keys(obj)) {\n if (k.includes(\".\")) return true;\n }\n return false;\n}\n","/**\n * Bridge: register an MCP server's tools into a Reasonix ToolRegistry.\n *\n * This is the integration surface. Once done, `CacheFirstLoop` sees the\n * MCP tools as if they were native — they inherit Cache-First + repair\n * (scavenge / truncation / storm) automatically. That's the payoff: any\n * MCP ecosystem tool, wrapped in Reasonix's Pillar 1 + Pillar 3 benefits.\n */\n\nimport { ToolRegistry } from \"../tools.js\";\nimport type { JSONSchema } from \"../types.js\";\nimport type { McpClient } from \"./client.js\";\nimport type { CallToolResult, McpContentBlock } from \"./types.js\";\n\nexport interface BridgeOptions {\n /**\n * Prefix prepended to every MCP tool name when registered. Defaults to\n * empty (no prefix). Useful when bridging multiple servers into one\n * registry and names collide — e.g. `fs` + `gh` both exposing `search`.\n */\n namePrefix?: string;\n /** Registry to populate. Creates a fresh one if omitted. */\n registry?: ToolRegistry;\n /** Auto-flatten deep schemas (Pillar 3). Defaults to the registry's own default (true). */\n autoFlatten?: boolean;\n /**\n * Per-tool-call result cap, in characters. If a tool returns more than\n * this, the result is truncated and a `[…truncated N chars…]` marker is\n * appended before the last KB so the model still sees a useful tail.\n * Defaults to {@link DEFAULT_MAX_RESULT_CHARS}.\n *\n * Why this exists: DeepSeek V3's context is 131,072 tokens. A single\n * `read_file` against a big source file can return >3 MB of text\n * (~900k tokens) and permanently poison the session — every subsequent\n * turn rebuilds the history and 400s. This cap is a floor. Users who\n * legitimately want bigger payloads can raise it explicitly.\n */\n maxResultChars?: number;\n /**\n * Callback fired for every `notifications/progress` frame the server\n * emits during any bridged tool call. Includes the registered\n * (prefix-applied) tool name so a multi-server UI can attribute\n * progress correctly. Absent → no `_meta.progressToken` is sent and\n * the server won't emit progress for these calls.\n */\n onProgress?: (info: {\n toolName: string;\n progress: number;\n total?: number;\n message?: string;\n }) => void;\n}\n\n/**\n * 32,000 chars ≈ 8k English tokens, or ~16k CJK tokens. Small enough to\n * fit comfortably in history even across 5–10 tool calls, large enough\n * that most file reads and directory listings fit un-truncated.\n */\nexport const DEFAULT_MAX_RESULT_CHARS = 32_000;\n\nexport interface BridgeResult {\n registry: ToolRegistry;\n /** Names actually registered (may differ from MCP names when a prefix is applied). */\n registeredNames: string[];\n /** Names the server listed but the bridge skipped (e.g. invalid schemas). */\n skipped: Array<{ name: string; reason: string }>;\n}\n\n/**\n * Walk a connected `McpClient`'s tools/list result, register each into a\n * Reasonix `ToolRegistry`. Each registered `fn` proxies through the\n * client's tools/call. Tool results are flattened into a string (joining\n * text blocks with newlines, prefixing image blocks as placeholders) so\n * they fit Reasonix's existing tool-dispatch contract.\n */\nexport async function bridgeMcpTools(\n client: McpClient,\n opts: BridgeOptions = {},\n): Promise<BridgeResult> {\n const registry = opts.registry ?? new ToolRegistry({ autoFlatten: opts.autoFlatten });\n const prefix = opts.namePrefix ?? \"\";\n const maxResultChars = opts.maxResultChars ?? DEFAULT_MAX_RESULT_CHARS;\n const result: BridgeResult = { registry, registeredNames: [], skipped: [] };\n\n const listed = await client.listTools();\n for (const mcpTool of listed.tools) {\n if (!mcpTool.name) {\n result.skipped.push({ name: \"?\", reason: \"empty tool name\" });\n continue;\n }\n const registeredName = `${prefix}${mcpTool.name}`;\n registry.register({\n name: registeredName,\n description: mcpTool.description ?? \"\",\n parameters: mcpTool.inputSchema as JSONSchema,\n fn: async (args: Record<string, unknown>, ctx) => {\n const toolResult = await client.callTool(mcpTool.name, args, {\n // Forward server-side progress frames to the bridge caller,\n // tagged with the registered name so multi-server UIs can\n // disambiguate. No-op when `onProgress` isn't configured —\n // the client then also omits the _meta.progressToken and\n // the server won't emit progress.\n onProgress: opts.onProgress\n ? (info) => opts.onProgress!({ toolName: registeredName, ...info })\n : undefined,\n // Thread the tool-dispatch AbortSignal all the way down to\n // the MCP request so Esc truly cancels in flight — the\n // client will emit notifications/cancelled AND reject the\n // pending promise immediately, no \"wait for subprocess\".\n signal: ctx?.signal,\n });\n return flattenMcpResult(toolResult, { maxChars: maxResultChars });\n },\n });\n result.registeredNames.push(registeredName);\n }\n return result;\n}\n\nexport interface FlattenOptions {\n /** Cap the flattened string at this many characters. Default: no cap. */\n maxChars?: number;\n}\n\n/**\n * Turn an MCP CallToolResult into a string — the contract Reasonix's\n * ToolRegistry.dispatch returns. We:\n * - join text blocks with newlines (most common case)\n * - stringify image blocks as placeholders (LLM can't use bytes anyway\n * in Reasonix's current surface; image support comes with multimodal\n * prompts later)\n * - prefix error results with \"ERROR: \" so the calling model sees the\n * failure clearly even through JSON mode\n * - optionally truncate to `maxChars` so a single oversized tool result\n * (e.g. a big `read_file`) can't poison the session by blowing past\n * the model's context window\n */\nexport function flattenMcpResult(result: CallToolResult, opts: FlattenOptions = {}): string {\n const parts = result.content.map(blockToString);\n const joined = parts.join(\"\\n\").trim();\n const prefixed = result.isError ? `ERROR: ${joined || \"(no error message from server)\"}` : joined;\n return opts.maxChars ? truncateForModel(prefixed, opts.maxChars) : prefixed;\n}\n\n/**\n * Keep the head AND a short tail so the model sees both \"what the tool\n * started returning\" and \"how it ended\". Head-only loses file endings\n * (e.g. an error message appended at the bottom of a stack trace); the\n * 1KB tail window covers that while costing almost nothing. Exported for\n * tests and reuse by non-MCP tool adapters that want the same policy.\n */\nexport function truncateForModel(s: string, maxChars: number): string {\n if (s.length <= maxChars) return s;\n const tailBudget = Math.min(1024, Math.floor(maxChars * 0.1));\n const headBudget = Math.max(0, maxChars - tailBudget);\n const head = s.slice(0, headBudget);\n const tail = s.slice(-tailBudget);\n const dropped = s.length - head.length - tail.length;\n return `${head}\\n\\n[…truncated ${dropped} chars — raise BridgeOptions.maxResultChars, or call the tool with a narrower scope (filter, head, pagination)…]\\n\\n${tail}`;\n}\n\nfunction blockToString(block: McpContentBlock): string {\n if (block.type === \"text\") return block.text;\n if (block.type === \"image\") return `[image ${block.mimeType}, ${block.data.length} chars base64]`;\n // Unknown block type — preserve for diagnostics.\n return `[unknown block: ${JSON.stringify(block)}]`;\n}\n","import { createHash } from \"node:crypto\";\nimport type { ChatMessage, ToolSpec } from \"./types.js\";\n\nexport interface ImmutablePrefixOptions {\n system: string;\n toolSpecs?: readonly ToolSpec[];\n fewShots?: readonly ChatMessage[];\n}\n\nexport class ImmutablePrefix {\n readonly system: string;\n readonly toolSpecs: readonly ToolSpec[];\n readonly fewShots: readonly ChatMessage[];\n\n constructor(opts: ImmutablePrefixOptions) {\n this.system = opts.system;\n this.toolSpecs = Object.freeze([...(opts.toolSpecs ?? [])]);\n this.fewShots = Object.freeze([...(opts.fewShots ?? [])]);\n }\n\n toMessages(): ChatMessage[] {\n return [{ role: \"system\", content: this.system }, ...this.fewShots.map((m) => ({ ...m }))];\n }\n\n tools(): ToolSpec[] {\n return this.toolSpecs.map((t) => structuredClone(t) as ToolSpec);\n }\n\n get fingerprint(): string {\n const blob = JSON.stringify({\n system: this.system,\n tools: this.toolSpecs,\n shots: this.fewShots,\n });\n return createHash(\"sha256\").update(blob).digest(\"hex\").slice(0, 16);\n }\n}\n\nexport class AppendOnlyLog {\n private _entries: ChatMessage[] = [];\n\n append(message: ChatMessage): void {\n if (!message || typeof message !== \"object\" || !(\"role\" in message)) {\n throw new Error(`invalid log entry: ${JSON.stringify(message)}`);\n }\n this._entries.push(message);\n }\n\n extend(messages: ChatMessage[]): void {\n for (const m of messages) this.append(m);\n }\n\n /**\n * Bulk-replace entries. Intentionally named to be hard to reach for —\n * this is the one mutation path that breaks the log's append-only\n * spirit, reserved for compaction flows (`/compact`) and recovery\n * where the caller has consciously decided to drop old history. Any\n * other use is almost certainly wrong; append() is what you want.\n */\n compactInPlace(replacement: ChatMessage[]): void {\n this._entries = [...replacement];\n }\n\n get entries(): readonly ChatMessage[] {\n return this._entries;\n }\n\n toMessages(): ChatMessage[] {\n return this._entries.map((e) => ({ ...e }));\n }\n\n get length(): number {\n return this._entries.length;\n }\n}\n\nexport class VolatileScratch {\n reasoning: string | null = null;\n planState: Record<string, unknown> | null = null;\n notes: string[] = [];\n\n reset(): void {\n this.reasoning = null;\n this.planState = null;\n this.notes = [];\n }\n}\n","/**\n * Scavenge tool calls leaked into reasoning_content.\n *\n * R1 sometimes emits tool-call JSON inside <think>…</think> and then forgets\n * to surface it in `tool_calls`. This pass extracts plausible calls and\n * proposes them to the loop, which decides whether to merge them with the\n * declared calls.\n */\n\nimport type { ToolCall } from \"../types.js\";\n\nexport interface ScavengeOptions {\n /** Names of tools the model may legitimately call. Other names are ignored. */\n allowedNames: ReadonlySet<string>;\n /** Maximum number of calls to scavenge per pass (defence against runaway). */\n maxCalls?: number;\n}\n\nexport interface ScavengeResult {\n calls: ToolCall[];\n notes: string[];\n}\n\nexport function scavengeToolCalls(\n reasoningContent: string | null | undefined,\n opts: ScavengeOptions,\n): ScavengeResult {\n if (!reasoningContent) return { calls: [], notes: [] };\n const max = opts.maxCalls ?? 4;\n const notes: string[] = [];\n const out: ToolCall[] = [];\n\n // Pattern A: DSML invoke blocks. R1 sometimes emits tool calls as\n // its chat-template markup in the content channel instead of the\n // proper `tool_calls` field. 0.4.3 stripped these from display;\n // here we actually turn them back into proper ToolCalls so the\n // model's intent isn't lost.\n for (const invoke of iterateDsmlInvokes(reasoningContent)) {\n if (out.length >= max) break;\n if (!opts.allowedNames.has(invoke.name)) continue;\n out.push({\n function: {\n name: invoke.name,\n arguments: JSON.stringify(invoke.args),\n },\n });\n notes.push(`scavenged DSML call: ${invoke.name}`);\n }\n\n // Pattern B: raw JSON objects (the original three shapes). Strip\n // any DSML blocks we already processed so parameter JSON buried\n // inside them doesn't get re-scavenged as a standalone call.\n const nonDsml = stripDsmlBlocks(reasoningContent);\n for (const candidate of iterateJsonObjects(nonDsml)) {\n if (out.length >= max) break;\n const call = coerceToToolCall(candidate, opts.allowedNames);\n if (call) {\n out.push(call);\n notes.push(`scavenged call: ${call.function.name}`);\n }\n }\n return { calls: out, notes };\n}\n\ninterface DsmlInvoke {\n name: string;\n args: Record<string, unknown>;\n}\n\n/**\n * Yield DeepSeek DSML invoke blocks scavenged from text. R1's chat\n * template uses these when the server-side tool-call serializer\n * misfires — we see them verbatim in the content channel:\n *\n * <|DSML|function_calls>\n * <|DSML|invoke name=\"filesystem_edit_file\">\n * <|DSML|parameter name=\"path\" string=\"true\">F:/x.html</|DSML|parameter>\n * <|DSML|parameter name=\"edits\" string=\"false\">[…]</|DSML|parameter>\n * </|DSML|invoke>\n * </|DSML|function_calls>\n *\n * The wrapper class (| U+FF5C vs ASCII |) varies by model build, so\n * we accept both. `string=\"true\"` means the parameter value is a\n * literal string; `string=\"false\"` means it's JSON.\n */\n/**\n * Remove DSML invoke/function_calls blocks from text so the raw-JSON\n * scanner below doesn't see parameter payloads as standalone calls.\n * Mirrors the strip pass in `stripHallucinatedToolMarkup` but scoped\n * to scavenge (we also want the `function_calls` wrapper gone —\n * nothing useful to scavenge there).\n */\nfunction stripDsmlBlocks(text: string): string {\n let out = text;\n out = out.replace(/<[||]DSML[||]function_calls>[\\s\\S]*?<\\/?[||]DSML[||]function_calls>/g, \"\");\n out = out.replace(/<[||]DSML[||]invoke\\s+[^>]*>[\\s\\S]*?<\\/[||]DSML[||]invoke>/g, \"\");\n return out;\n}\n\nfunction* iterateDsmlInvokes(text: string): Generator<DsmlInvoke> {\n // `|` (U+FF5C) in practice; `|` (ASCII) as a fallback seen in a\n // minority of builds. `[||]` inside the regex covers both.\n const INVOKE_RE = /<[||]DSML[||]invoke\\s+name=\"([^\"]+)\">([\\s\\S]*?)<\\/[||]DSML[||]invoke>/g;\n for (const match of text.matchAll(INVOKE_RE)) {\n const name = match[1];\n const body = match[2];\n if (!name || body === undefined) continue;\n yield { name, args: parseDsmlParameters(body) };\n }\n}\n\n/**\n * Parse the parameter children of a DSML invoke body into a plain\n * object. We tolerate:\n * - `string=\"true\"` → literal text; whitespace trimmed at the edges.\n * - `string=\"false\"` → JSON-parsed; falls back to literal text if the\n * payload turns out to be malformed (better to hand a string\n * through than lose the parameter entirely).\n * - no `string=\"…\"` attribute → treat as literal text (older/shorter\n * DSML shapes we haven't formally seen but want to handle).\n */\nfunction parseDsmlParameters(body: string): Record<string, unknown> {\n const PARAM_RE =\n /<[||]DSML[||]parameter\\s+name=\"([^\"]+)\"(?:\\s+string=\"(true|false)\")?\\s*>([\\s\\S]*?)<\\/[||]DSML[||]parameter>/g;\n const args: Record<string, unknown> = {};\n for (const m of body.matchAll(PARAM_RE)) {\n const key = m[1];\n const stringFlag = m[2];\n const raw = (m[3] ?? \"\").trim();\n if (!key) continue;\n if (stringFlag === \"false\") {\n try {\n args[key] = JSON.parse(raw);\n continue;\n } catch {\n // Fall through — keep as literal so the information isn't lost.\n }\n }\n args[key] = raw;\n }\n return args;\n}\n\n/** Yield every top-level JSON object substring in `text`. */\nfunction* iterateJsonObjects(text: string): Generator<string> {\n for (let i = 0; i < text.length; i++) {\n if (text[i] !== \"{\") continue;\n let depth = 0;\n let inString = false;\n let escaped = false;\n for (let j = i; j < text.length; j++) {\n const c = text[j]!;\n if (escaped) {\n escaped = false;\n continue;\n }\n if (inString) {\n if (c === \"\\\\\") {\n escaped = true;\n continue;\n }\n if (c === '\"') inString = false;\n continue;\n }\n if (c === '\"') inString = true;\n else if (c === \"{\") depth++;\n else if (c === \"}\") {\n depth--;\n if (depth === 0) {\n yield text.slice(i, j + 1);\n i = j;\n break;\n }\n }\n }\n }\n}\n\nfunction coerceToToolCall(\n candidateJson: string,\n allowedNames: ReadonlySet<string>,\n): ToolCall | null {\n let parsed: any;\n try {\n parsed = JSON.parse(candidateJson);\n } catch {\n return null;\n }\n if (!parsed || typeof parsed !== \"object\") return null;\n\n // Pattern 1: { name, arguments }\n if (typeof parsed.name === \"string\" && allowedNames.has(parsed.name)) {\n const args = parsed.arguments;\n return {\n function: {\n name: parsed.name,\n arguments: typeof args === \"string\" ? args : JSON.stringify(args ?? {}),\n },\n };\n }\n\n // Pattern 2: OpenAI-style { type: \"function\", function: { name, arguments } }\n if (\n parsed.type === \"function\" &&\n parsed.function &&\n typeof parsed.function.name === \"string\" &&\n allowedNames.has(parsed.function.name)\n ) {\n const args = parsed.function.arguments;\n return {\n type: \"function\",\n function: {\n name: parsed.function.name,\n arguments: typeof args === \"string\" ? args : JSON.stringify(args ?? {}),\n },\n };\n }\n\n // Pattern 3: { tool_name, tool_args } (R1 free-form variant)\n if (typeof parsed.tool_name === \"string\" && allowedNames.has(parsed.tool_name)) {\n return {\n function: {\n name: parsed.tool_name,\n arguments: JSON.stringify(parsed.tool_args ?? {}),\n },\n };\n }\n\n return null;\n}\n","import type { ToolCall } from \"../types.js\";\n\n/**\n * Call-storm breaker.\n *\n * Detects (tool, args) tuples repeating within a sliding window and suppresses\n * the offending call. Surfaces a synthetic tool_result advising the model to\n * change strategy on its next turn.\n */\nexport class StormBreaker {\n private readonly windowSize: number;\n private readonly threshold: number;\n private readonly recent: Array<readonly [string, string]> = [];\n\n constructor(windowSize = 6, threshold = 3) {\n this.windowSize = windowSize;\n this.threshold = threshold;\n }\n\n inspect(call: ToolCall): { suppress: boolean; reason?: string } {\n const sig = signature(call);\n if (!sig) return { suppress: false };\n const count = this.recent.reduce(\n (n, [name, args]) => (name === sig[0] && args === sig[1] ? n + 1 : n),\n 0,\n );\n if (count >= this.threshold - 1) {\n return {\n suppress: true,\n reason: `call-storm suppressed: ${sig[0]} called with identical args ${count + 1} times within window=${this.windowSize}`,\n };\n }\n this.recent.push(sig);\n while (this.recent.length > this.windowSize) this.recent.shift();\n return { suppress: false };\n }\n\n reset(): void {\n this.recent.length = 0;\n }\n}\n\nfunction signature(call: ToolCall): readonly [string, string] | null {\n const name = call.function?.name;\n if (!name) return null;\n return [name, call.function?.arguments ?? \"\"] as const;\n}\n","/**\n * Truncation recovery for tool-call argument JSON cut off mid-structure\n * (typically when the model hits max_tokens before finishing the JSON object).\n *\n * Strategy is purely local: balance braces, close strings, fill missing values\n * with `null`. We deliberately do NOT make a continuation API call here — that\n * decision belongs to the loop, which knows about budgets.\n */\n\nexport interface TruncationRepairResult {\n repaired: string;\n changed: boolean;\n notes: string[];\n}\n\nexport function repairTruncatedJson(input: string): TruncationRepairResult {\n const notes: string[] = [];\n if (!input || !input.trim()) {\n return { repaired: \"{}\", changed: input !== \"{}\", notes: [\"empty input → {}\"] };\n }\n // Fast path: already parseable.\n try {\n JSON.parse(input);\n return { repaired: input, changed: false, notes: [] };\n } catch {\n /* fall through */\n }\n\n const stack: (\"{\" | \"[\" | '\"')[] = [];\n let escaped = false;\n let inString = false;\n let lastSignificant = -1;\n\n for (let i = 0; i < input.length; i++) {\n const c = input[i]!;\n if (!/\\s/.test(c)) lastSignificant = i;\n if (escaped) {\n escaped = false;\n continue;\n }\n if (inString) {\n if (c === \"\\\\\") {\n escaped = true;\n continue;\n }\n if (c === '\"') {\n inString = false;\n stack.pop();\n }\n continue;\n }\n if (c === '\"') {\n inString = true;\n stack.push('\"');\n continue;\n }\n if (c === \"{\" || c === \"[\") stack.push(c);\n else if (c === \"}\" || c === \"]\") stack.pop();\n }\n\n let s = input.slice(0, lastSignificant + 1);\n\n // Trim a trailing comma which would block re-parse.\n if (/,$/.test(s)) {\n s = s.replace(/,$/, \"\");\n notes.push(\"trimmed trailing comma\");\n }\n\n // If we ended on a key without a value: \"foo\": → \"foo\": null\n if (/\"\\s*:\\s*$/.test(s)) {\n s += \" null\";\n notes.push(\"filled dangling key with null\");\n }\n\n // If we ended inside a string, close it.\n if (inString) {\n s += '\"';\n stack.pop();\n notes.push(\"closed unterminated string\");\n }\n\n // Pop remaining open structures in reverse order.\n while (stack.length > 0) {\n const top = stack.pop();\n if (top === \"{\") s += \"}\";\n else if (top === \"[\") s += \"]\";\n else if (top === '\"') s += '\"';\n }\n\n try {\n JSON.parse(s);\n return { repaired: s, changed: true, notes };\n } catch (err) {\n notes.push(`fallback to {}: ${(err as Error).message}`);\n return { repaired: \"{}\", changed: true, notes };\n }\n}\n","/**\n * Pillar 3 — Tool-Call Repair pipeline.\n *\n * Order of passes per turn:\n * 1. scavenge — recover tool calls leaked into <think>\n * 2. truncation — close any half-emitted argument JSON\n * 3. storm breaker — drop call-storm repeats\n *\n * Schema flattening is applied during loop construction (it changes what we\n * advertise to the model), not per-turn.\n */\n\nimport type { ToolCall } from \"../types.js\";\nimport { scavengeToolCalls } from \"./scavenge.js\";\nimport { StormBreaker } from \"./storm.js\";\nimport { repairTruncatedJson } from \"./truncation.js\";\n\nexport { analyzeSchema, flattenSchema, nestArguments } from \"./flatten.js\";\nexport type { FlattenDecision } from \"./flatten.js\";\nexport { repairTruncatedJson } from \"./truncation.js\";\nexport type { TruncationRepairResult } from \"./truncation.js\";\nexport { scavengeToolCalls } from \"./scavenge.js\";\nexport type { ScavengeOptions, ScavengeResult } from \"./scavenge.js\";\nexport { StormBreaker } from \"./storm.js\";\n\nexport interface RepairReport {\n scavenged: number;\n truncationsFixed: number;\n stormsBroken: number;\n notes: string[];\n}\n\nexport interface ToolCallRepairOptions {\n allowedToolNames: ReadonlySet<string>;\n stormWindow?: number;\n stormThreshold?: number;\n maxScavenge?: number;\n}\n\nexport class ToolCallRepair {\n private readonly storm: StormBreaker;\n private readonly opts: ToolCallRepairOptions;\n\n constructor(opts: ToolCallRepairOptions) {\n this.opts = opts;\n this.storm = new StormBreaker(opts.stormWindow ?? 6, opts.stormThreshold ?? 3);\n }\n\n /**\n * Drop the StormBreaker's sliding window of recent (name, args)\n * signatures. Called at the start of every user turn — a fresh user\n * message is a new intent, so carrying old repetition state into it\n * would turn a valid \"try again with different input\" flow into a\n * false-positive block.\n */\n resetStorm(): void {\n this.storm.reset();\n }\n\n process(\n declaredCalls: ToolCall[],\n reasoningContent: string | null,\n content: string | null = null,\n ): { calls: ToolCall[]; report: RepairReport } {\n const report: RepairReport = {\n scavenged: 0,\n truncationsFixed: 0,\n stormsBroken: 0,\n notes: [],\n };\n\n // 1. Scavenge — only add calls whose (name,args) signature is novel.\n // Scan both channels: reasoning (where R1 leaks JSON calls into\n // <think>) AND content (where it emits DSML markup in regular\n // turns). Joined with a newline so the scanners see the blobs as\n // independent bodies. Dedup below keeps us from inflating if the\n // same call shows up in both — first seen wins.\n const combined = [reasoningContent ?? \"\", content ?? \"\"].filter(Boolean).join(\"\\n\");\n const scavenged = scavengeToolCalls(combined || null, {\n allowedNames: this.opts.allowedToolNames,\n maxCalls: this.opts.maxScavenge ?? 4,\n });\n const seenSignatures = new Set(declaredCalls.map(signature));\n const merged = [...declaredCalls];\n for (const sc of scavenged.calls) {\n if (!seenSignatures.has(signature(sc))) {\n merged.push(sc);\n report.scavenged++;\n seenSignatures.add(signature(sc));\n }\n }\n report.notes.push(...scavenged.notes);\n\n // 2. Truncation repair on argument JSON.\n for (const call of merged) {\n const args = call.function?.arguments ?? \"\";\n const r = repairTruncatedJson(args);\n if (r.changed) {\n call.function.arguments = r.repaired;\n report.truncationsFixed++;\n report.notes.push(...r.notes.map((n) => `[${call.function.name}] ${n}`));\n }\n }\n\n // 3. Storm breaker.\n const filtered: ToolCall[] = [];\n for (const call of merged) {\n const verdict = this.storm.inspect(call);\n if (verdict.suppress) {\n report.stormsBroken++;\n if (verdict.reason) report.notes.push(verdict.reason);\n continue;\n }\n filtered.push(call);\n }\n\n return { calls: filtered, report };\n }\n}\n\nfunction signature(call: ToolCall): string {\n return `${call.function?.name ?? \"\"}::${call.function?.arguments ?? \"\"}`;\n}\n","/**\n * Session persistence.\n *\n * Every turn's log entries (user / assistant / tool messages) are appended to\n * a JSONL file under `~/.reasonix/sessions/<name>.jsonl`. Next time the user\n * starts the CLI with the same session name, the loop pre-loads the file\n * into its AppendOnlyLog so the new turn has full prior context.\n *\n * Design notes:\n * - JSONL rather than JSON so concurrent writes don't corrupt.\n * - 0600 permissions on Unix (chmod no-ops on Windows).\n * - Name sanitization keeps paths safe: only [\\w-] and CJK letters pass;\n * anything else is replaced with underscore, max 64 chars.\n * - The loop's stats/session aren't persisted — only the message log.\n * Cost accounting resets each run (by design — old costs are sunk).\n */\n\nimport {\n appendFileSync,\n chmodSync,\n existsSync,\n mkdirSync,\n readFileSync,\n readdirSync,\n statSync,\n unlinkSync,\n writeFileSync,\n} from \"node:fs\";\nimport { homedir } from \"node:os\";\nimport { dirname, join } from \"node:path\";\nimport type { ChatMessage } from \"./types.js\";\n\nexport interface SessionInfo {\n name: string;\n path: string;\n size: number;\n messageCount: number;\n mtime: Date;\n}\n\nexport function sessionsDir(): string {\n return join(homedir(), \".reasonix\", \"sessions\");\n}\n\nexport function sessionPath(name: string): string {\n return join(sessionsDir(), `${sanitizeName(name)}.jsonl`);\n}\n\nexport function sanitizeName(name: string): string {\n const cleaned = name.replace(/[^\\w\\-\\u4e00-\\u9fa5]/g, \"_\").slice(0, 64);\n return cleaned || \"default\";\n}\n\nexport function loadSessionMessages(name: string): ChatMessage[] {\n const path = sessionPath(name);\n if (!existsSync(path)) return [];\n try {\n const raw = readFileSync(path, \"utf8\");\n const out: ChatMessage[] = [];\n for (const line of raw.split(/\\r?\\n/)) {\n const trimmed = line.trim();\n if (!trimmed) continue;\n try {\n const msg = JSON.parse(trimmed) as ChatMessage;\n if (msg && typeof msg === \"object\" && \"role\" in msg) out.push(msg);\n } catch {\n /* skip malformed line */\n }\n }\n return out;\n } catch {\n return [];\n }\n}\n\nexport function appendSessionMessage(name: string, message: ChatMessage): void {\n const path = sessionPath(name);\n mkdirSync(dirname(path), { recursive: true });\n appendFileSync(path, `${JSON.stringify(message)}\\n`, \"utf8\");\n try {\n chmodSync(path, 0o600);\n } catch {\n /* chmod not supported on this platform */\n }\n}\n\nexport function listSessions(): SessionInfo[] {\n const dir = sessionsDir();\n if (!existsSync(dir)) return [];\n try {\n const files = readdirSync(dir).filter((f) => f.endsWith(\".jsonl\"));\n return files\n .map((file) => {\n const path = join(dir, file);\n const stat = statSync(path);\n const name = file.replace(/\\.jsonl$/, \"\");\n const messageCount = countLines(path);\n return { name, path, size: stat.size, messageCount, mtime: stat.mtime };\n })\n .sort((a, b) => b.mtime.getTime() - a.mtime.getTime());\n } catch {\n return [];\n }\n}\n\nexport function deleteSession(name: string): boolean {\n const path = sessionPath(name);\n try {\n unlinkSync(path);\n return true;\n } catch {\n return false;\n }\n}\n\n/**\n * Overwrite the session file with a fresh message list. Used by\n * `/compact` so the compacted in-memory log persists across restarts\n * instead of being re-healed from a huge on-disk file every launch.\n * We accept the brief non-atomic window between truncate and write —\n * worst case: a concurrent crash loses the session, which is what\n * `/forget` would have done anyway.\n */\nexport function rewriteSession(name: string, messages: ChatMessage[]): void {\n const path = sessionPath(name);\n mkdirSync(dirname(path), { recursive: true });\n const body = messages.map((m) => JSON.stringify(m)).join(\"\\n\");\n writeFileSync(path, body ? `${body}\\n` : \"\", \"utf8\");\n try {\n chmodSync(path, 0o600);\n } catch {\n /* chmod not supported */\n }\n}\n\nfunction countLines(path: string): number {\n try {\n const raw = readFileSync(path, \"utf8\");\n return raw.split(/\\r?\\n/).filter((l) => l.trim()).length;\n } catch {\n return 0;\n }\n}\n","import type { Usage } from \"./client.js\";\n\n/** USD per 1M tokens. Update as DeepSeek pricing changes. */\nexport const DEEPSEEK_PRICING: Record<\n string,\n { inputCacheHit: number; inputCacheMiss: number; output: number }\n> = {\n \"deepseek-chat\": { inputCacheHit: 0.07, inputCacheMiss: 0.27, output: 1.1 },\n \"deepseek-reasoner\": { inputCacheHit: 0.14, inputCacheMiss: 0.55, output: 2.19 },\n};\n\n/** Reference Claude Sonnet 4.6 pricing (USD per 1M tokens). */\nexport const CLAUDE_SONNET_PRICING = { input: 3.0, output: 15.0 };\n\n/**\n * Maximum prompt-side context window per DeepSeek model, in tokens.\n * Both V3 (`deepseek-chat`) and R1 (`deepseek-reasoner`) currently expose\n * a 131,072-token prompt limit per the OpenAPI spec; completion caps\n * differ but don't affect the prompt budget the StatsPanel shows.\n */\nexport const DEEPSEEK_CONTEXT_TOKENS: Record<string, number> = {\n \"deepseek-chat\": 131_072,\n \"deepseek-reasoner\": 131_072,\n};\n\n/** Fallback when the caller's model id isn't in the table — safe lower bound. */\nexport const DEFAULT_CONTEXT_TOKENS = 131_072;\n\nexport function costUsd(model: string, usage: Usage): number {\n const p = DEEPSEEK_PRICING[model];\n if (!p) return 0;\n return (\n (usage.promptCacheHitTokens * p.inputCacheHit +\n usage.promptCacheMissTokens * p.inputCacheMiss +\n usage.completionTokens * p.output) /\n 1_000_000\n );\n}\n\n/** Input-side cost only (prompt, cache hit + miss). Used for the panel breakdown. */\nexport function inputCostUsd(model: string, usage: Usage): number {\n const p = DEEPSEEK_PRICING[model];\n if (!p) return 0;\n return (\n (usage.promptCacheHitTokens * p.inputCacheHit +\n usage.promptCacheMissTokens * p.inputCacheMiss) /\n 1_000_000\n );\n}\n\n/** Output-side cost only (completion tokens). Used for the panel breakdown. */\nexport function outputCostUsd(model: string, usage: Usage): number {\n const p = DEEPSEEK_PRICING[model];\n if (!p) return 0;\n return (usage.completionTokens * p.output) / 1_000_000;\n}\n\nexport function claudeEquivalentCost(usage: Usage): number {\n return (\n (usage.promptTokens * CLAUDE_SONNET_PRICING.input +\n usage.completionTokens * CLAUDE_SONNET_PRICING.output) /\n 1_000_000\n );\n}\n\nexport interface TurnStats {\n turn: number;\n model: string;\n usage: Usage;\n cost: number;\n cacheHitRatio: number;\n}\n\nexport interface SessionSummary {\n turns: number;\n totalCostUsd: number;\n /**\n * Input-side (prompt) cost aggregated across the session. Split\n * from totalCostUsd so the panel can render \"cost $X (in $Y · out\n * $Z)\" — users asked for visibility into where the spend lands.\n */\n totalInputCostUsd: number;\n /** Output-side (completion) cost aggregated across the session. */\n totalOutputCostUsd: number;\n /** @deprecated Claude reference; kept for benchmarks + replay compat, no longer surfaced in the TUI. */\n claudeEquivalentUsd: number;\n /** @deprecated. Same as claudeEquivalentUsd — synthetic ratio, not a real measurement. */\n savingsVsClaudePct: number;\n cacheHitRatio: number;\n /**\n * Most recent turn's prompt-token count. Used by the TUI's context\n * gauge: we can't know the next call's cost without making it, but\n * the last turn's prompt tokens is the floor (next call is last\n * prompt + user delta + any new tool outputs).\n */\n lastPromptTokens: number;\n}\n\nexport class SessionStats {\n readonly turns: TurnStats[] = [];\n\n record(turn: number, model: string, usage: Usage): TurnStats {\n const cost = costUsd(model, usage);\n const stats: TurnStats = {\n turn,\n model,\n usage,\n cost,\n cacheHitRatio: usage.cacheHitRatio,\n };\n this.turns.push(stats);\n return stats;\n }\n\n get totalCost(): number {\n return this.turns.reduce((sum, t) => sum + t.cost, 0);\n }\n\n get totalClaudeEquivalent(): number {\n return this.turns.reduce((sum, t) => sum + claudeEquivalentCost(t.usage), 0);\n }\n\n get savingsVsClaude(): number {\n const c = this.totalClaudeEquivalent;\n return c > 0 ? 1 - this.totalCost / c : 0;\n }\n\n get totalInputCost(): number {\n return this.turns.reduce((sum, t) => sum + inputCostUsd(t.model, t.usage), 0);\n }\n\n get totalOutputCost(): number {\n return this.turns.reduce((sum, t) => sum + outputCostUsd(t.model, t.usage), 0);\n }\n\n get aggregateCacheHitRatio(): number {\n let hit = 0;\n let miss = 0;\n for (const t of this.turns) {\n hit += t.usage.promptCacheHitTokens;\n miss += t.usage.promptCacheMissTokens;\n }\n const denom = hit + miss;\n return denom > 0 ? hit / denom : 0;\n }\n\n summary(): SessionSummary {\n const last = this.turns[this.turns.length - 1];\n return {\n turns: this.turns.length,\n totalCostUsd: round(this.totalCost, 6),\n totalInputCostUsd: round(this.totalInputCost, 6),\n totalOutputCostUsd: round(this.totalOutputCost, 6),\n claudeEquivalentUsd: round(this.totalClaudeEquivalent, 6),\n savingsVsClaudePct: round(this.savingsVsClaude * 100, 2),\n cacheHitRatio: round(this.aggregateCacheHitRatio, 4),\n lastPromptTokens: last?.usage.promptTokens ?? 0,\n };\n }\n}\n\nfunction round(n: number, digits: number): number {\n const f = 10 ** digits;\n return Math.round(n * f) / f;\n}\n","import { type DeepSeekClient, Usage } from \"./client.js\";\nimport {\n type BranchOptions,\n type BranchSample,\n aggregateBranchUsage,\n runBranches,\n} from \"./consistency.js\";\nimport { type HarvestOptions, type TypedPlanState, emptyPlanState, harvest } from \"./harvest.js\";\nimport {\n type HookOutcome,\n type HookPayload,\n type ResolvedHook,\n formatHookOutcomeMessage,\n runHooks,\n} from \"./hooks.js\";\nimport { DEFAULT_MAX_RESULT_CHARS, truncateForModel } from \"./mcp/registry.js\";\nimport { AppendOnlyLog, type ImmutablePrefix, VolatileScratch } from \"./memory.js\";\nimport { type RepairReport, ToolCallRepair } from \"./repair/index.js\";\nimport { appendSessionMessage, loadSessionMessages, rewriteSession } from \"./session.js\";\nimport {\n DEEPSEEK_CONTEXT_TOKENS,\n DEFAULT_CONTEXT_TOKENS,\n SessionStats,\n type TurnStats,\n} from \"./telemetry.js\";\nimport { ToolRegistry } from \"./tools.js\";\nimport type { ChatMessage, ToolCall } from \"./types.js\";\n\nexport type EventRole =\n | \"assistant_delta\"\n | \"assistant_final\"\n /**\n * Emitted as `tool_calls[].function.arguments` streams in. A tool\n * call with a large arguments payload produces no `content` or\n * `reasoning_content` bytes — this is the only signal the UI has\n * that the stream is alive during that window.\n */\n | \"tool_call_delta\"\n /**\n * Yielded immediately before a tool is dispatched. Lets the TUI put\n * up a \"▸ tool<X> running…\" spinner while the tool's Promise is\n * pending — otherwise the UI looks frozen whenever a tool call\n * takes more than a few hundred ms (a big `filesystem_edit_file`\n * is a typical trigger).\n */\n | \"tool_start\"\n | \"tool\"\n | \"done\"\n | \"error\"\n | \"warning\"\n /**\n * Transient \"what's happening right now\" indicator. Emitted during\n * silent phases — between a tool result and the next iteration's\n * first streaming byte, and right before harvest — so the TUI can\n * show a spinner with explanatory text instead of looking frozen.\n * The UI clears it on the next primary event (assistant_delta,\n * tool_start, tool, assistant_final, error).\n */\n | \"status\"\n | \"branch_start\"\n | \"branch_progress\"\n | \"branch_done\";\n\nexport interface BranchSummary {\n budget: number;\n chosenIndex: number;\n uncertainties: number[]; // per-sample uncertainty counts\n temperatures: number[];\n}\n\nexport interface BranchProgress {\n completed: number;\n total: number;\n latestIndex: number;\n latestTemperature: number;\n latestUncertainties: number;\n}\n\nexport interface LoopEvent {\n turn: number;\n role: EventRole;\n content: string;\n reasoningDelta?: string;\n toolName?: string;\n /**\n * Raw JSON-string arguments the model sent for a tool call (role === \"tool\").\n * Populated so transcripts can persist *why* a tool was called, not just\n * what it returned. Needed by `reasonix diff` to explain divergences.\n */\n toolArgs?: string;\n /** Cumulative arguments-string length for `role === \"tool_call_delta\"`. */\n toolCallArgsChars?: number;\n stats?: TurnStats;\n planState?: TypedPlanState;\n repair?: RepairReport;\n branch?: BranchSummary;\n branchProgress?: BranchProgress;\n error?: string;\n /**\n * True on `assistant_final` events emitted by the no-tools fallback\n * when the loop hit its budget, was aborted, or tripped the\n * token-context guard. Consumers that act on assistant text (notably\n * the code-mode edit applier) MUST treat these as display-only —\n * the model is \"wrapping up,\" not proposing new work. Applying\n * SEARCH/REPLACE blocks found in a forced summary caused the\n * \"analysis became edits\" bug in v0.4.1 and earlier.\n */\n forcedSummary?: boolean;\n}\n\nexport interface CacheFirstLoopOptions {\n client: DeepSeekClient;\n prefix: ImmutablePrefix;\n tools?: ToolRegistry;\n model?: string;\n maxToolIters?: number;\n stream?: boolean;\n /**\n * Pillar 2 — structured harvesting of R1 reasoning into a typed plan state.\n * Pass `true` for defaults or an options object. Off by default (adds a\n * cheap but non-zero V3 call per turn).\n */\n harvest?: boolean | HarvestOptions;\n /**\n * Self-consistency branching. Pass a number for just a budget (e.g. 3) or\n * a full `BranchOptions` object. Disables streaming for the branched turn\n * because all samples must complete before selection. Auto-enables harvest\n * since the default selector scores samples by plan-state uncertainty.\n */\n branch?: number | BranchOptions;\n /**\n * Session name. When set, the loop pre-loads the session's prior messages\n * into its log on construction, and appends every new log entry to\n * `~/.reasonix/sessions/<name>.jsonl` so the next run can resume.\n */\n session?: string;\n /**\n * Resolved hook list — loaded from `<project>/.reasonix/settings.json`\n * + `~/.reasonix/settings.json` by the CLI before constructing the loop.\n * The loop dispatches `PreToolUse` and `PostToolUse` events itself; the\n * CLI handles `UserPromptSubmit` and `Stop` since they live at the App\n * boundary. Empty / unset → no hooks fire (the runtime cost of an empty\n * filter is one ms). See `src/hooks.ts` for the full contract.\n */\n hooks?: ResolvedHook[];\n /**\n * `cwd` reported to hooks via the stdin payload. Defaults to `process.cwd()`.\n * `reasonix code` overrides this to the sandbox root so a hook that does\n * `cd $REASONIX_CWD` lands in the project, not in the user's shell home.\n */\n hookCwd?: string;\n}\n\n/**\n * Pillar 1 — Cache-First Loop.\n *\n * - prefix is immutable (cache target)\n * - log is append-only (preserves prior-turn prefix)\n * - scratch is per-turn volatile (never sent upstream)\n *\n * Yields a stream of events so a TUI can render progressively.\n */\nexport interface ReconfigurableOptions {\n model?: string;\n harvest?: boolean | HarvestOptions;\n branch?: number | BranchOptions;\n stream?: boolean;\n}\n\nexport class CacheFirstLoop {\n readonly client: DeepSeekClient;\n readonly prefix: ImmutablePrefix;\n readonly tools: ToolRegistry;\n readonly maxToolIters: number;\n readonly log = new AppendOnlyLog();\n readonly scratch = new VolatileScratch();\n readonly stats = new SessionStats();\n readonly repair: ToolCallRepair;\n\n // Mutable via configure() — slash commands in the TUI / library callers tweak\n // these mid-session so users don't have to restart to try harvest or branch.\n model: string;\n stream: boolean;\n harvestEnabled: boolean;\n harvestOptions: HarvestOptions;\n branchEnabled: boolean;\n branchOptions: BranchOptions;\n sessionName: string | null;\n\n /**\n * Hook list, mutable so `/hooks reload` can swap it without\n * reconstructing the loop. Default empty — the filter cost on a\n * tool call is one array length check.\n */\n hooks: ResolvedHook[];\n /** `cwd` reported to hook stdin. Resolved once at construction. */\n readonly hookCwd: string;\n\n /** Number of messages that were pre-loaded from the session file. */\n readonly resumedMessageCount: number;\n\n private _turn = 0;\n private _streamPreference: boolean;\n /**\n * AbortController per active turn. Threaded through the DeepSeek\n * HTTP calls AND every tool dispatch so Esc actually cancels the\n * in-flight network/subprocess work — not \"we'll get to it after\n * the current call finishes.\" Re-created at the start of each\n * `step()` (the prior turn's signal has already fired).\n */\n private _turnAbort: AbortController = new AbortController();\n\n constructor(opts: CacheFirstLoopOptions) {\n this.client = opts.client;\n this.prefix = opts.prefix;\n this.tools = opts.tools ?? new ToolRegistry();\n this.model = opts.model ?? \"deepseek-chat\";\n // Iter cap is a safety net, not the primary stop condition. The\n // primary stop is the token-context guard inside step(): after\n // every model response we check whether the prompt is already past\n // 80% of the model's context window, and if so divert to the\n // forced-summary path. 64 is high enough that exploration almost\n // never exhausts it before the token guard fires first — which\n // is the point: let the real constraint (context window) drive\n // the decision, keep the iter cap as a last-resort backstop for\n // the case where something spins without growing the prompt.\n this.maxToolIters = opts.maxToolIters ?? 64;\n this.hooks = opts.hooks ?? [];\n this.hookCwd = opts.hookCwd ?? process.cwd();\n\n // Resolve branch config first (since it forces harvest on).\n if (typeof opts.branch === \"number\") {\n this.branchOptions = { budget: opts.branch };\n } else if (opts.branch && typeof opts.branch === \"object\") {\n this.branchOptions = opts.branch;\n } else {\n this.branchOptions = {};\n }\n this.branchEnabled = (this.branchOptions.budget ?? 1) > 1;\n\n // Branching requires harvest for its default selector to work.\n const harvestForced = this.branchEnabled;\n this.harvestEnabled =\n harvestForced ||\n opts.harvest === true ||\n (typeof opts.harvest === \"object\" && opts.harvest !== null);\n this.harvestOptions =\n typeof opts.harvest === \"object\" && opts.harvest !== null\n ? opts.harvest\n : (this.branchOptions.harvestOptions ?? {});\n\n // Streaming is incompatible with branching (need all samples to select).\n this._streamPreference = opts.stream ?? true;\n this.stream = this.branchEnabled ? false : this._streamPreference;\n\n const allowedNames = new Set([...this.prefix.toolSpecs.map((s) => s.function.name)]);\n this.repair = new ToolCallRepair({ allowedToolNames: allowedNames });\n\n // Session resume: pre-load prior messages into the log if a session name\n // is provided. New messages appended to the log are also persisted.\n //\n // Heal-on-load: if a previous run (or a pre-alpha.6 client) stored a\n // tool result bigger than the cap, the next API call would blow past\n // DeepSeek's 131k-token limit *before the user even types anything*.\n // Truncating here lets the user pick up their session history without\n // losing the conversational context around the oversized call.\n this.sessionName = opts.session ?? null;\n if (this.sessionName) {\n const prior = loadSessionMessages(this.sessionName);\n const { messages, healedCount, healedFrom } = healLoadedMessages(\n prior,\n DEFAULT_MAX_RESULT_CHARS,\n );\n for (const msg of messages) this.log.append(msg);\n this.resumedMessageCount = messages.length;\n if (healedCount > 0) {\n // Persist the healed log back to disk so the damage doesn't\n // re-surface on the next session load — otherwise `heal →\n // append → resume → heal → …` would keep noticing the same\n // broken tail every restart. Non-fatal on I/O error: the\n // in-memory log is already healed so this session still works.\n try {\n rewriteSession(this.sessionName, messages);\n } catch {\n /* disk full / perms — skip, in-memory heal still applies */\n }\n process.stderr.write(\n `▸ session \"${this.sessionName}\": healed ${healedCount} entr${healedCount === 1 ? \"y\" : \"ies\"}${healedFrom > 0 ? ` (was ${healedFrom.toLocaleString()} chars oversized)` : \" (dropped dangling tool_calls tail)\"}. Rewrote session file.\\n`,\n );\n }\n } else {\n this.resumedMessageCount = 0;\n }\n }\n\n /**\n * Shrink the log by re-truncating oversized tool results to a tighter\n * cap, and persist the result back to disk so the next launch doesn't\n * re-inherit a fat session file. Returns a summary the TUI can\n * display.\n *\n * Only tool-role messages are touched (same rationale as\n * {@link healLoadedMessages}). User and assistant messages carry\n * authored intent we can't mechanically shrink without losing\n * meaning.\n */\n compact(tightCapChars = 4000): { healedCount: number; charsSaved: number } {\n const before = this.log.toMessages();\n // Use `shrinkOversizedToolResults` (not `healLoadedMessages`) — the\n // full heal would also strip a dangling `assistant.tool_calls` tail,\n // which during an active turn is legitimate state we still need\n // (tools haven't been dispatched yet). Structural healing is only\n // appropriate at session LOAD; mid-session `/compact` is strictly\n // about shrinking oversized tool payloads.\n const { messages, healedCount, healedFrom } = shrinkOversizedToolResults(before, tightCapChars);\n const afterBytes = messages\n .filter((m) => m.role === \"tool\")\n .reduce((s, m) => s + (typeof m.content === \"string\" ? m.content.length : 0), 0);\n const charsSaved = healedFrom - afterBytes;\n if (healedCount > 0) {\n this.log.compactInPlace(messages);\n if (this.sessionName) {\n try {\n rewriteSession(this.sessionName, messages);\n } catch {\n /* disk full or perms — compaction still applies in-memory */\n }\n }\n }\n return { healedCount, charsSaved };\n }\n\n private appendAndPersist(message: ChatMessage): void {\n this.log.append(message);\n if (this.sessionName) {\n try {\n appendSessionMessage(this.sessionName, message);\n } catch {\n /* disk full or permission denied shouldn't kill the chat */\n }\n }\n }\n\n /**\n * Start a fresh conversation WITHOUT exiting. Drops every message\n * in the in-memory log AND rewrites the session file to empty so\n * a resume won't re-hydrate the old turns. Unlike `/forget`, which\n * deletes the session entirely, this keeps the session name and\n * config intact — it's the \"new chat\" button.\n *\n * The immutable prefix (system prompt + tool specs) is preserved\n * — that's the cache-first invariant, not part of the conversation.\n * Returns the number of messages dropped so the UI can show it.\n */\n clearLog(): { dropped: number } {\n const dropped = this.log.length;\n this.log.compactInPlace([]);\n if (this.sessionName) {\n try {\n rewriteSession(this.sessionName, []);\n } catch {\n /* disk issue shouldn't block the in-memory clear */\n }\n }\n this.scratch.reset();\n return { dropped };\n }\n\n /**\n * Reconfigure model/harvest/branch/stream mid-session. The loop's log,\n * scratch, and stats are preserved — only the per-turn behavior changes.\n * Used by the TUI's slash commands and by library callers who want to\n * flip a knob between turns.\n */\n configure(opts: ReconfigurableOptions): void {\n if (opts.model !== undefined) this.model = opts.model;\n if (opts.stream !== undefined) this._streamPreference = opts.stream;\n\n if (opts.branch !== undefined) {\n if (typeof opts.branch === \"number\") {\n this.branchOptions = { budget: opts.branch };\n } else if (opts.branch && typeof opts.branch === \"object\") {\n this.branchOptions = opts.branch;\n } else {\n this.branchOptions = {};\n }\n this.branchEnabled = (this.branchOptions.budget ?? 1) > 1;\n }\n\n if (opts.harvest !== undefined) {\n const want =\n opts.harvest === true || (typeof opts.harvest === \"object\" && opts.harvest !== null);\n this.harvestEnabled = want || this.branchEnabled;\n if (typeof opts.harvest === \"object\" && opts.harvest !== null) {\n this.harvestOptions = opts.harvest;\n }\n } else if (this.branchEnabled) {\n // branch turned on without explicit harvest → force it on\n this.harvestEnabled = true;\n }\n\n // Branching always forces non-streaming; otherwise honor preference.\n this.stream = this.branchEnabled ? false : this._streamPreference;\n }\n\n private buildMessages(pendingUser: string | null): ChatMessage[] {\n // Full tool_calls ↔ tool pairing validation. DeepSeek 400s on\n // both sides of this contract — unpaired assistant.tool_calls\n // (\"insufficient tool messages following\") OR stray tool entries\n // (\"tool must be a response to a preceding tool_calls\"). A corrupted\n // session from an earlier build can have either. Rather than\n // applying a bunch of narrow tail-trim heuristics, rebuild the\n // message stream through the same validator used at load time so\n // the payload we hand to the API is well-formed by construction.\n const healed = healLoadedMessages(this.log.toMessages(), DEFAULT_MAX_RESULT_CHARS);\n const msgs: ChatMessage[] = [...this.prefix.toMessages(), ...healed.messages];\n if (pendingUser !== null) msgs.push({ role: \"user\", content: pendingUser });\n return msgs;\n }\n\n /**\n * Signal the currently-running {@link step} to stop **now**. Cancels\n * the in-flight network request (DeepSeek HTTP/SSE) AND any tool call\n * currently dispatching (MCP `notifications/cancelled` + promise\n * reject). The loop itself also sees `signal.aborted` at each\n * iteration boundary and exits quickly instead of looping again.\n * Called by the TUI on Esc.\n */\n abort(): void {\n this._turnAbort.abort();\n }\n\n /**\n * Drop everything in the log after (and including) the most recent\n * user message. Used by `/retry` so the caller can re-send that\n * message with a fresh turn instead of layering another response on\n * top of the prior exchange. Returns the content of the dropped user\n * message, or `null` if there isn't one yet.\n *\n * Persists by rewriting the session file — otherwise the next\n * launch would rehydrate the old exchange and `/retry` would seem\n * to have done nothing.\n */\n retryLastUser(): string | null {\n const entries = this.log.entries;\n let lastUserIdx = -1;\n for (let i = entries.length - 1; i >= 0; i--) {\n if (entries[i]!.role === \"user\") {\n lastUserIdx = i;\n break;\n }\n }\n if (lastUserIdx < 0) return null;\n const raw = entries[lastUserIdx]!.content;\n const userText = typeof raw === \"string\" ? raw : \"\";\n // Keep everything strictly before the user message. The caller\n // will submit the text again through the normal path, which\n // re-appends the user turn on first successful API response.\n const preserved = entries.slice(0, lastUserIdx).map((m) => ({ ...m }));\n this.log.compactInPlace(preserved);\n if (this.sessionName) {\n try {\n rewriteSession(this.sessionName, preserved);\n } catch {\n /* disk-full / perms — in-memory compaction still applies */\n }\n }\n return userText;\n }\n\n async *step(userInput: string): AsyncGenerator<LoopEvent> {\n this._turn++;\n this.scratch.reset();\n // A fresh user turn is a new intent — don't let StormBreaker's\n // old sliding window of (name, args) signatures keep blocking\n // calls that are now legitimately on-task. The window repopulates\n // naturally as this turn's tool calls flow through.\n this.repair.resetStorm();\n // Fresh controller for this turn: the prior step's signal has\n // already fired (or stayed clean); either way we don't want its\n // state to bleed into the new turn.\n this._turnAbort = new AbortController();\n const signal = this._turnAbort.signal;\n let pendingUser: string | null = userInput;\n const toolSpecs = this.prefix.tools();\n // 70% of the iter budget is the \"you're getting close\" threshold. We\n // only warn once per step so the user sees a single signal, not a\n // string of identical yellow lines stacked up.\n const warnAt = Math.max(1, Math.floor(this.maxToolIters * 0.7));\n let warnedForIterBudget = false;\n\n for (let iter = 0; iter < this.maxToolIters; iter++) {\n if (signal.aborted) {\n // Esc means \"stop now\" — not \"stop and force another 30-90s\n // reasoner call to produce a summary I didn't ask for\". The\n // user's mental model of cancel is immediate. We emit a\n // synthetic assistant_final (tagged forcedSummary so the\n // code-mode applier ignores it) with a short stopped\n // message, then done. The prior tool outputs are still in\n // the log if the user wants to continue — asking again\n // will hit a warm cache and be cheap.\n //\n // Budget / context-guard still call forceSummaryAfterIterLimit\n // because there the USER didn't choose to stop — we did —\n // and leaving them staring at nothing is worse than one extra\n // call.\n yield {\n turn: this._turn,\n role: \"warning\",\n content: `aborted at iter ${iter}/${this.maxToolIters} — stopped without producing a summary (press ↑ + Enter or /retry to resume)`,\n };\n const stoppedMsg =\n \"[aborted by user (Esc) — no summary produced. Ask again or /retry when ready; prior tool output is still in the log.]\";\n this.appendAndPersist({ role: \"assistant\", content: stoppedMsg });\n yield {\n turn: this._turn,\n role: \"assistant_final\",\n content: stoppedMsg,\n forcedSummary: true,\n };\n yield { turn: this._turn, role: \"done\", content: stoppedMsg };\n return;\n }\n // Bridge the silence between the PREVIOUS iter's tool result and\n // THIS iter's first streaming byte. R1 can spend 20-90s reasoning\n // about tool output before the first delta lands, and prior to\n // this hint the UI had nothing to render. Only emit on iter > 0\n // because iter 0's \"thinking\" phase is already covered by the\n // streaming row / StreamingAssistant's placeholder.\n //\n // Wording is explicit about the two things happening: the tool\n // result IS being uploaded (it's now part of the next prompt) and\n // the model IS thinking. Users were reading \"thinking about the\n // tool result\" as the model-only phase, but the wait also covers\n // the upload round-trip.\n if (iter > 0) {\n yield {\n turn: this._turn,\n role: \"status\",\n content: \"tool result uploaded · model thinking before next response…\",\n };\n }\n if (!warnedForIterBudget && iter >= warnAt) {\n warnedForIterBudget = true;\n yield {\n turn: this._turn,\n role: \"warning\",\n content: `${iter}/${this.maxToolIters} tool calls used — approaching budget. Press Esc to force a summary now.`,\n };\n }\n const messages = this.buildMessages(pendingUser);\n\n let assistantContent = \"\";\n let reasoningContent = \"\";\n let toolCalls: ToolCall[] = [];\n let usage: TurnStats[\"usage\"] | null = null;\n\n let branchSummary: BranchSummary | undefined;\n let preHarvestedPlanState: TypedPlanState | undefined;\n\n try {\n if (this.branchEnabled) {\n const budget = this.branchOptions.budget ?? 1;\n yield {\n turn: this._turn,\n role: \"branch_start\",\n content: \"\",\n branchProgress: {\n completed: 0,\n total: budget,\n latestIndex: -1,\n latestTemperature: -1,\n latestUncertainties: -1,\n },\n };\n\n // Queue samples as they complete so we can yield progress events\n // in resolution order (not launch order).\n const queue: BranchSample[] = [];\n let waiter: ((s: BranchSample) => void) | null = null;\n\n const onSampleDone = (sample: BranchSample) => {\n if (waiter) {\n const w = waiter;\n waiter = null;\n w(sample);\n } else {\n queue.push(sample);\n }\n };\n\n const branchPromise = runBranches(\n this.client,\n {\n model: this.model,\n messages,\n tools: toolSpecs.length ? toolSpecs : undefined,\n signal,\n },\n {\n ...this.branchOptions,\n harvestOptions: this.harvestOptions,\n onSampleDone,\n },\n );\n\n for (let k = 0; k < budget; k++) {\n const sample: BranchSample =\n queue.shift() ??\n (await new Promise<BranchSample>((resolve) => {\n waiter = resolve;\n }));\n yield {\n turn: this._turn,\n role: \"branch_progress\",\n content: \"\",\n branchProgress: {\n completed: k + 1,\n total: budget,\n latestIndex: sample.index,\n latestTemperature: sample.temperature,\n latestUncertainties: sample.planState.uncertainties.length,\n },\n };\n }\n\n const result = await branchPromise;\n assistantContent = result.chosen.response.content;\n reasoningContent = result.chosen.response.reasoningContent ?? \"\";\n toolCalls = result.chosen.response.toolCalls;\n\n // Cost accounting: sum usage across ALL samples, not just the winner.\n // (We paid for all three.) Harvest-call tokens are not tracked; they\n // amount to rounding error compared to the main R1 calls.\n const agg = aggregateBranchUsage(result.samples);\n usage = new Usage(\n agg.promptTokens,\n agg.completionTokens,\n agg.totalTokens,\n agg.promptCacheHitTokens,\n agg.promptCacheMissTokens,\n );\n preHarvestedPlanState = result.chosen.planState;\n branchSummary = summarizeBranch(result.chosen, result.samples);\n yield {\n turn: this._turn,\n role: \"branch_done\",\n content: \"\",\n branch: branchSummary,\n };\n } else if (this.stream) {\n const callBuf: Map<number, ToolCall> = new Map();\n for await (const chunk of this.client.stream({\n model: this.model,\n messages,\n tools: toolSpecs.length ? toolSpecs : undefined,\n signal,\n })) {\n if (chunk.contentDelta) {\n assistantContent += chunk.contentDelta;\n yield {\n turn: this._turn,\n role: \"assistant_delta\",\n content: chunk.contentDelta,\n };\n }\n if (chunk.reasoningDelta) {\n reasoningContent += chunk.reasoningDelta;\n yield {\n turn: this._turn,\n role: \"assistant_delta\",\n content: \"\",\n reasoningDelta: chunk.reasoningDelta,\n };\n }\n if (chunk.toolCallDelta) {\n const d = chunk.toolCallDelta;\n const cur = callBuf.get(d.index) ?? {\n id: d.id,\n type: \"function\" as const,\n function: { name: \"\", arguments: \"\" },\n };\n if (d.id) cur.id = d.id;\n if (d.name) cur.function.name = (cur.function.name ?? \"\") + d.name;\n if (d.argumentsDelta)\n cur.function.arguments = (cur.function.arguments ?? \"\") + d.argumentsDelta;\n callBuf.set(d.index, cur);\n // Skip the id-only opener: name is empty until the next chunk.\n if (cur.function.name) {\n yield {\n turn: this._turn,\n role: \"tool_call_delta\",\n content: \"\",\n toolName: cur.function.name,\n toolCallArgsChars: (cur.function.arguments ?? \"\").length,\n };\n }\n }\n if (chunk.usage) usage = chunk.usage;\n }\n toolCalls = [...callBuf.values()];\n } else {\n const resp = await this.client.chat({\n model: this.model,\n messages,\n tools: toolSpecs.length ? toolSpecs : undefined,\n signal,\n });\n assistantContent = resp.content;\n reasoningContent = resp.reasoningContent ?? \"\";\n toolCalls = resp.toolCalls;\n usage = resp.usage;\n }\n } catch (err) {\n yield {\n turn: this._turn,\n role: \"error\",\n content: \"\",\n error: formatLoopError(err as Error),\n };\n return;\n }\n\n const turnStats = this.stats.record(this._turn, this.model, usage ?? new Usage());\n\n // Commit the user turn to the log only on success of the first round-trip.\n if (pendingUser !== null) {\n this.appendAndPersist({ role: \"user\", content: pendingUser });\n pendingUser = null;\n }\n\n this.scratch.reasoning = reasoningContent || null;\n // Harvest is a second API round-trip (cheap model, but still\n // 1-10s) that was previously silent. Bridge the gap with a\n // status indicator so the TUI shows *something* instead of\n // \"reasoning finished, now staring at the wall.\"\n if (\n !preHarvestedPlanState &&\n this.harvestEnabled &&\n (reasoningContent?.trim().length ?? 0) >= 40\n ) {\n yield {\n turn: this._turn,\n role: \"status\",\n content: \"extracting plan state from reasoning…\",\n };\n }\n const planState = preHarvestedPlanState\n ? preHarvestedPlanState\n : this.harvestEnabled\n ? await harvest(reasoningContent || null, this.client, this.harvestOptions, signal)\n : emptyPlanState();\n\n const { calls: repairedCalls, report } = this.repair.process(\n toolCalls,\n reasoningContent || null,\n assistantContent || null,\n );\n\n this.appendAndPersist(this.assistantMessage(assistantContent, repairedCalls));\n\n yield {\n turn: this._turn,\n role: \"assistant_final\",\n content: assistantContent,\n stats: turnStats,\n planState,\n repair: report,\n branch: branchSummary,\n };\n\n // Loud signal when the storm breaker caught a repeat pattern.\n // The `repair` field on assistant_final already carries the\n // count as a subtext on the assistant row, but a dedicated\n // warning row is far more noticeable — and critical when ALL\n // calls were suppressed, because the turn then ends with no\n // visible explanation of why nothing happened.\n if (report.stormsBroken > 0) {\n const noteTail = report.notes.length ? ` — ${report.notes[report.notes.length - 1]}` : \"\";\n const allSuppressed = repairedCalls.length === 0 && toolCalls.length > 0;\n const phrase = allSuppressed\n ? `stopped the model from calling the same tool with identical args repeatedly (all ${toolCalls.length} call(s) this turn were already in the recent-repeat window). Likely a stuck retry — reword your instruction, rule out the underlying blocker, or try /retry after fixing it`\n : `suppressed ${report.stormsBroken} repeat tool call(s) that had fired 3+ times with identical args in a sliding window`;\n yield {\n turn: this._turn,\n role: \"warning\",\n content: `${phrase}${noteTail}`,\n };\n }\n\n if (repairedCalls.length === 0) {\n yield { turn: this._turn, role: \"done\", content: assistantContent };\n return;\n }\n\n // Token-budget guard — the real stop condition. Iter count is a\n // proxy that misses the actual constraint: how close the prompt\n // already is to DeepSeek's 131k context. If we're over 80%, the\n // NEXT call (with the just-executed tools' results stuffed into\n // history) will be worse, and fairly soon it'll 400 with\n // \"maximum context length\".\n //\n // Strategy, in order:\n // 1. Try auto-compacting the log (shrink oversized tool\n // results). If that gets us back under 80% we keep going —\n // the user doesn't lose their turn to a premature summary.\n // 2. If still over after compact, divert to the forced-summary\n // path. BUT first drop the trailing assistant-with-tool_calls\n // that we just appended — we haven't executed the tools yet,\n // so sending this to the summary call with no matching tool\n // responses would 400 (\"insufficient tool messages following\n // tool_calls\"). The summary is about what was LEARNED so far,\n // not what we intended to do next.\n const ctxMax = DEEPSEEK_CONTEXT_TOKENS[this.model] ?? DEFAULT_CONTEXT_TOKENS;\n if (usage && usage.promptTokens / ctxMax > 0.8) {\n const before = usage.promptTokens;\n const compactResult = this.compact(4000);\n if (compactResult.healedCount > 0) {\n // Rough estimate: 4 chars per token. Good enough to decide\n // whether compaction pushed us back under the threshold; the\n // exact number comes back from the NEXT API response's usage.\n const approxSaved = Math.round(compactResult.charsSaved / 4);\n const after = before - approxSaved;\n yield {\n turn: this._turn,\n role: \"warning\",\n content: `context ${before.toLocaleString()}/${ctxMax.toLocaleString()} — auto-compacted ${compactResult.healedCount} oversized tool result(s), saved ~${approxSaved.toLocaleString()} tokens (now ~${after.toLocaleString()}). Continuing.`,\n };\n // Intentionally don't re-check the threshold here: even if\n // compaction didn't fully clear us under 80%, one more tool\n // call's overhead isn't going to overflow, and the NEXT\n // iter's fresh `usage` from the API will catch real danger.\n } else {\n yield {\n turn: this._turn,\n role: \"warning\",\n content: `context ${before.toLocaleString()}/${ctxMax.toLocaleString()} (${Math.round(\n (before / ctxMax) * 100,\n )}%) — nothing to auto-compact. Forcing summary from what was gathered.`,\n };\n // Drop the trailing assistant-with-tool_calls we just\n // appended. The forced-summary call would otherwise trip\n // DeepSeek's \"insufficient tool messages following tool_calls\"\n // validator, since we bail BEFORE dispatching the tools.\n const tail = this.log.entries[this.log.entries.length - 1];\n if (\n tail &&\n tail.role === \"assistant\" &&\n Array.isArray(tail.tool_calls) &&\n tail.tool_calls.length > 0\n ) {\n const kept = this.log.entries.slice(0, -1);\n this.log.compactInPlace([...kept]);\n if (this.sessionName) {\n try {\n rewriteSession(this.sessionName, kept);\n } catch {\n /* disk issue shouldn't block the summary path */\n }\n }\n }\n yield* this.forceSummaryAfterIterLimit({ reason: \"context-guard\" });\n return;\n }\n }\n\n for (const call of repairedCalls) {\n const name = call.function?.name ?? \"\";\n const args = call.function?.arguments ?? \"{}\";\n // Announce the tool BEFORE awaiting it so the TUI can render a\n // \"running…\" indicator. Without this, the window between\n // assistant_final and the tool-result yield is silent from the\n // UI's perspective — which makes long tool calls feel like the\n // app has hung.\n yield {\n turn: this._turn,\n role: \"tool_start\",\n content: \"\",\n toolName: name,\n toolArgs: args,\n };\n\n // PreToolUse hooks. A `block` decision (exit 2) skips dispatch\n // and surfaces the hook's stderr as the tool result so the model\n // sees a structured refusal instead of a silent omission. Non-\n // block non-zero outcomes are warnings: the loop continues, the\n // UI gets a yellow row.\n const parsedArgs = safeParseToolArgs(args);\n const preReport = await runHooks({\n hooks: this.hooks,\n payload: {\n event: \"PreToolUse\",\n cwd: this.hookCwd,\n toolName: name,\n toolArgs: parsedArgs,\n },\n });\n for (const w of hookWarnings(preReport.outcomes, this._turn)) yield w;\n\n let result: string;\n if (preReport.blocked) {\n const blocking = preReport.outcomes[preReport.outcomes.length - 1];\n const reason = (\n blocking?.stderr ||\n blocking?.stdout ||\n \"blocked by PreToolUse hook\"\n ).trim();\n result = `[hook block] ${blocking?.hook.command ?? \"<unknown>\"}\\n${reason}`;\n } else {\n result = await this.tools.dispatch(name, args, { signal });\n\n // PostToolUse hooks — block is meaningless after the fact, so\n // every non-pass outcome is a warning. Hooks here are the\n // natural place for \"after every edit, run the formatter.\"\n const postReport = await runHooks({\n hooks: this.hooks,\n payload: {\n event: \"PostToolUse\",\n cwd: this.hookCwd,\n toolName: name,\n toolArgs: parsedArgs,\n toolResult: result,\n },\n });\n for (const w of hookWarnings(postReport.outcomes, this._turn)) yield w;\n }\n\n this.appendAndPersist({\n role: \"tool\",\n tool_call_id: call.id ?? \"\",\n name,\n content: result,\n });\n yield {\n turn: this._turn,\n role: \"tool\",\n content: result,\n toolName: name,\n toolArgs: args,\n };\n }\n }\n\n // We exhausted the tool-call budget while the model still wanted to\n // call more tools. Rather than stopping silently (which leaves the\n // user staring at a blank prompt), force one final no-tools call so\n // the model must produce a text summary from everything it has\n // already seen.\n yield* this.forceSummaryAfterIterLimit({ reason: \"budget\" });\n }\n\n private async *forceSummaryAfterIterLimit(\n opts: { reason: \"budget\" | \"aborted\" | \"context-guard\" } = { reason: \"budget\" },\n ): AsyncGenerator<LoopEvent> {\n try {\n // The summary call is non-streaming (reasoner, 30-60s typical).\n // Without this status the user sees nothing happening after the\n // yellow \"budget reached\" warning until the summary arrives.\n yield {\n turn: this._turn,\n role: \"status\",\n content: \"summarizing what was gathered…\",\n };\n const messages = this.buildMessages(null);\n // Passing `tools: undefined` was supposed to force a text\n // response, but R1 can still hallucinate tool-call markup\n // (e.g. DSML `<|DSML|function_calls>…</|DSML|function_calls>`)\n // in prose when it's been primed by prior tool use. An explicit\n // user-role instruction plus post-hoc stripping of known\n // hallucination shapes keeps the user from seeing raw markup.\n messages.push({\n role: \"user\",\n content:\n \"I'm out of tool-call budget for this turn. Summarize in plain prose what you learned from the tool results above. Do NOT emit any tool calls, function-call markup, DSML invocations, or SEARCH/REPLACE edit blocks — they will be silently discarded. Just plain text.\",\n });\n const resp = await this.client.chat({\n model: this.model,\n messages,\n // no tools → model is forced to answer in text\n signal: this._turnAbort.signal,\n });\n const rawContent = resp.content?.trim() ?? \"\";\n const cleaned = stripHallucinatedToolMarkup(rawContent);\n const summary =\n cleaned ||\n \"(model emitted fake tool-call markup instead of a prose summary — try /retry with a narrower question, or /think to inspect R1's reasoning)\";\n const reasonPrefix = reasonPrefixFor(opts.reason, this.maxToolIters);\n const annotated = `${reasonPrefix}\\n\\n${summary}`;\n const summaryStats = this.stats.record(this._turn, this.model, resp.usage ?? new Usage());\n this.appendAndPersist({ role: \"assistant\", content: summary });\n yield {\n turn: this._turn,\n role: \"assistant_final\",\n content: annotated,\n stats: summaryStats,\n forcedSummary: true,\n };\n yield { turn: this._turn, role: \"done\", content: summary };\n } catch (err) {\n const label = errorLabelFor(opts.reason, this.maxToolIters);\n yield {\n turn: this._turn,\n role: \"error\",\n content: \"\",\n error: `${label} and the fallback summary call failed: ${(err as Error).message}. Run /clear and retry with a narrower question, or raise --max-tool-iters.`,\n };\n yield { turn: this._turn, role: \"done\", content: \"\" };\n }\n }\n\n async run(userInput: string, onEvent?: (ev: LoopEvent) => void): Promise<string> {\n let final = \"\";\n for await (const ev of this.step(userInput)) {\n onEvent?.(ev);\n if (ev.role === \"assistant_final\") final = ev.content;\n if (ev.role === \"done\") break;\n }\n return final;\n }\n\n private assistantMessage(content: string, toolCalls: ToolCall[]): ChatMessage {\n const msg: ChatMessage = { role: \"assistant\", content };\n if (toolCalls.length > 0) msg.tool_calls = toolCalls;\n return msg;\n }\n}\n\n/**\n * R1 occasionally hallucinates tool-call markup as plain text when the\n * real tool channel has been closed — typically our forced-summary\n * path, where `tools: undefined` is supposed to force prose but isn't\n * always respected. The markup isn't parsed by our tool-call path\n * (the API response's structured `tool_calls` field is empty), so\n * it's just noise in the user's view. Strip known envelope shapes.\n *\n * Exported so tests can exercise it against concrete R1 outputs.\n */\nexport function stripHallucinatedToolMarkup(s: string): string {\n let out = s;\n // DeepSeek's DSML envelope (both the full-width \"|\" character and\n // the ASCII-only fallback we've seen — the full-width form is the\n // one R1 emits in practice)\n out = out.replace(/<|DSML|function_calls>[\\s\\S]*?<\\/?|DSML|function_calls>/g, \"\");\n out = out.replace(/<\\|DSML\\|function_calls>[\\s\\S]*?<\\/?\\|DSML\\|function_calls>/g, \"\");\n // Anthropic / generic XML-ish envelope\n out = out.replace(/<function_calls>[\\s\\S]*?<\\/function_calls>/g, \"\");\n // Lone unpaired DSML opener left over after the closer was on a\n // different line (seen when R1 truncates mid-call).\n out = out.replace(/<|DSML|[\\s\\S]*$/g, \"\");\n return out.trim();\n}\n\n/**\n * Try to JSON-decode the model's tool-call arguments so PreToolUse /\n * PostToolUse hooks get a structured object instead of a string.\n * Falls back to the raw string when the model emits malformed JSON\n * (the loop's own dispatch already tolerates that — keep parity).\n */\nfunction safeParseToolArgs(raw: string): unknown {\n try {\n return JSON.parse(raw);\n } catch {\n return raw;\n }\n}\n\n/** Format non-pass hook outcomes as `LoopEvent`s of role `warning`. */\nfunction* hookWarnings(outcomes: HookOutcome[], turn: number): Generator<LoopEvent> {\n for (const o of outcomes) {\n if (o.decision === \"pass\") continue;\n yield { turn, role: \"warning\", content: formatHookOutcomeMessage(o) };\n }\n}\n\nfunction reasonPrefixFor(reason: \"budget\" | \"aborted\" | \"context-guard\", iterCap: number): string {\n if (reason === \"aborted\") return \"[aborted by user (Esc) — summarizing what I found so far]\";\n if (reason === \"context-guard\") {\n return \"[context budget running low — summarizing before the next call would overflow]\";\n }\n return `[tool-call budget (${iterCap}) reached — forcing summary from what I found]`;\n}\n\nfunction errorLabelFor(reason: \"budget\" | \"aborted\" | \"context-guard\", iterCap: number): string {\n if (reason === \"aborted\") return \"aborted by user\";\n if (reason === \"context-guard\") return \"context-guard triggered (prompt > 80% of window)\";\n return `tool-call budget (${iterCap}) reached`;\n}\n\nfunction summarizeBranch(chosen: BranchSample, samples: BranchSample[]): BranchSummary {\n return {\n budget: samples.length,\n chosenIndex: chosen.index,\n uncertainties: samples.map((s) => s.planState.uncertainties.length),\n temperatures: samples.map((s) => s.temperature),\n };\n}\n\n/**\n * Truncate any tool-role message whose content exceeds the cap. User\n * and assistant messages are left alone because (a) they're almost\n * always small, (b) truncating user prompts would corrupt conversational\n * intent in a way the user didn't author. Exported for tests.\n */\n/**\n * Shrink oversized tool results only — the original compact concern.\n * Separated from `healLoadedMessages` so `/compact` (live, mid-session)\n * doesn't accidentally strip structural tail that belongs in the\n * current turn's state.\n */\nexport function shrinkOversizedToolResults(\n messages: ChatMessage[],\n maxChars: number,\n): { messages: ChatMessage[]; healedCount: number; healedFrom: number } {\n let healedCount = 0;\n let healedFrom = 0;\n const out = messages.map((msg) => {\n if (msg.role !== \"tool\") return msg;\n const content = typeof msg.content === \"string\" ? msg.content : \"\";\n if (content.length <= maxChars) return msg;\n healedCount += 1;\n healedFrom += content.length;\n return { ...msg, content: truncateForModel(content, maxChars) };\n });\n return { messages: out, healedCount, healedFrom };\n}\n\nexport function healLoadedMessages(\n messages: ChatMessage[],\n maxChars: number,\n): { messages: ChatMessage[]; healedCount: number; healedFrom: number } {\n // Pass 1: shrink oversized tool results (original heal purpose).\n const shrunk = shrinkOversizedToolResults(messages, maxChars);\n let healedCount = shrunk.healedCount;\n // Pass 2: enforce tool_calls ↔ tool pairing across the full log.\n //\n // DeepSeek rejects two shapes at the API boundary:\n // (a) assistant with tool_calls not followed by matching tool\n // responses (\"insufficient tool messages following tool_calls\")\n // (b) tool message without a preceding assistant.tool_calls with\n // the matching tool_call_id (\"must be a response to a preceding\n // message with 'tool_calls'\")\n //\n // Corrupted session files from earlier builds have hit both. Rebuild\n // the message stream so only well-formed (assistant.tool_calls + all\n // matching responses) groups survive. Plain user/assistant messages\n // (no tool_calls) always pass through.\n const out: ChatMessage[] = [];\n const openCallIds = new Set<string>();\n let droppedAssistantCalls = 0;\n let droppedStrayTools = 0;\n for (let i = 0; i < shrunk.messages.length; i++) {\n const msg = shrunk.messages[i]!;\n if (msg.role === \"assistant\" && Array.isArray(msg.tool_calls) && msg.tool_calls.length > 0) {\n // Look ahead for tool responses matching every id in this\n // assistant's tool_calls. If all present (in any order, but\n // contiguous after this message), include the whole group.\n const needed = new Set<string>();\n for (const call of msg.tool_calls) {\n if (call?.id) needed.add(call.id);\n }\n const candidates: ChatMessage[] = [];\n let j = i + 1;\n while (j < shrunk.messages.length && needed.size > 0) {\n const nxt = shrunk.messages[j]!;\n if (nxt.role !== \"tool\") break;\n const id = nxt.tool_call_id ?? \"\";\n if (!needed.has(id)) break;\n needed.delete(id);\n candidates.push(nxt);\n j++;\n }\n if (needed.size === 0) {\n // Every call has a response — emit the whole group.\n out.push(msg);\n for (const r of candidates) out.push(r);\n i = j - 1; // for-loop ++ will advance past the last response\n } else {\n // Drop the assistant entry and anything that was speculatively\n // its responses — they'd become stray tool messages.\n droppedAssistantCalls += 1;\n droppedStrayTools += candidates.length;\n i = j - 1;\n }\n continue;\n }\n if (msg.role === \"tool\") {\n // Any tool message that reaches here did NOT get consumed by\n // the assistant-with-tool_calls branch above, so it's stray.\n // Drop it — surfacing it would 400 the next API call.\n droppedStrayTools += 1;\n continue;\n }\n // Plain user/assistant/system message — pass through.\n out.push(msg);\n }\n healedCount += droppedAssistantCalls + droppedStrayTools;\n return { messages: out, healedCount, healedFrom: shrunk.healedFrom };\n}\n\n/**\n * Annotate the `DeepSeek 400: … maximum context length …` error the API\n * returns when a session's history has grown past 131,072 tokens. The\n * raw message is a JSON blob; we surface a short actionable hint on top\n * so the user knows to `/forget` or `/clear` rather than parsing the\n * JSON themselves. Other errors pass through unchanged — the loop's\n * error channel already formats them well enough.\n */\nexport function formatLoopError(err: Error): string {\n const msg = err.message ?? \"\";\n if (msg.includes(\"maximum context length\")) {\n // Pull the \"requested X tokens\" figure out of the JSON for scale.\n const reqMatch = msg.match(/requested\\s+(\\d+)\\s+tokens/);\n const requested = reqMatch\n ? `${Number(reqMatch[1]).toLocaleString()} tokens`\n : \"too many tokens\";\n return `Context overflow (DeepSeek 400): session history is ${requested}, past the 131,072-token limit. Usually this means a single tool call returned a huge payload. v0.3.0-alpha.6+ caps new tool results at 32k chars, AND auto-heals oversized history on session load — restart Reasonix and this session should come back trimmed. If it still overflows, run /forget (delete the session) or /clear (drop the displayed history) to start fresh.`;\n }\n return msg;\n}\n","/**\n * Built-in filesystem tools for `reasonix code`.\n *\n * Why native instead of the official `@modelcontextprotocol/server-filesystem`:\n * - No subprocess overhead — every call is 50-200 ms cheaper.\n * - Schema shapes tuned for R1: `edit_file` takes a single\n * SEARCH/REPLACE string instead of `string=\"false\"`-encoded\n * JSON arrays, which was the biggest single source of DSML\n * hallucinations in 0.4.x.\n * - Sandbox enforcement lives here so Reasonix can reason about\n * it (tests cover path-traversal, symlink-escape, and the\n * cwd-outside-root case) rather than trusting an external server.\n * - No `npx install` / network dependency in `reasonix code`.\n *\n * Tool names + argument shapes intentionally mirror the official\n * filesystem server so R1's muscle memory carries over. The only\n * intentional divergence is `edit_file`, noted above.\n */\n\nimport { promises as fs } from \"node:fs\";\nimport * as pathMod from \"node:path\";\nimport type { ToolRegistry } from \"../tools.js\";\n\nexport interface FilesystemToolsOptions {\n /** Absolute directory the tools may read/write. Paths outside this are refused. */\n rootDir: string;\n /**\n * When `false`, register only read-side tools (read_file, list_directory,\n * search_files, get_file_info, directory_tree). Useful for read-only\n * workflows where the model should never mutate the tree. Default: true.\n */\n allowWriting?: boolean;\n /**\n * Cap for a single file read, in bytes. Prevents a stray `read_file`\n * on a multi-GB blob from OOM'ing Node. 2 MB is enough for any realistic\n * source file (the biggest single-file TypeScript project checked in to\n * GitHub is ~500 KB); pass higher when working with data files.\n */\n maxReadBytes?: number;\n /**\n * Cap for total bytes returned from search_files / directory_tree /\n * grep, so the model can't accidentally pull down the whole tree as\n * one giant string. 256 KB by default.\n */\n maxListBytes?: number;\n}\n\nconst DEFAULT_MAX_READ_BYTES = 2 * 1024 * 1024;\nconst DEFAULT_MAX_LIST_BYTES = 256 * 1024;\n\nexport function registerFilesystemTools(\n registry: ToolRegistry,\n opts: FilesystemToolsOptions,\n): ToolRegistry {\n const rootDir = pathMod.resolve(opts.rootDir);\n const allowWriting = opts.allowWriting !== false;\n const maxReadBytes = opts.maxReadBytes ?? DEFAULT_MAX_READ_BYTES;\n const maxListBytes = opts.maxListBytes ?? DEFAULT_MAX_LIST_BYTES;\n\n /** Resolve path, enforce it's under rootDir, return absolute. */\n const safePath = (raw: unknown): string => {\n if (typeof raw !== \"string\" || raw.length === 0) {\n throw new Error(\"path must be a non-empty string\");\n }\n // Interpret relative paths against rootDir (the session's scope).\n const resolved = pathMod.resolve(rootDir, raw);\n const normRoot = pathMod.resolve(rootDir);\n // Use relative() to catch any `..` segments that escape.\n const rel = pathMod.relative(normRoot, resolved);\n if (rel.startsWith(\"..\") || pathMod.isAbsolute(rel)) {\n throw new Error(`path escapes sandbox root (${normRoot}): ${raw}`);\n }\n return resolved;\n };\n\n registry.register({\n name: \"read_file\",\n description:\n \"Read a file under the sandbox root. Returns the full contents (truncated with a notice if larger than the per-call cap). Paths may be relative to the root or absolute-under-root.\",\n readOnly: true,\n parameters: {\n type: \"object\",\n properties: {\n path: { type: \"string\", description: \"Path to read (relative to rootDir or absolute).\" },\n head: { type: \"integer\", description: \"If set, return only the first N lines.\" },\n tail: { type: \"integer\", description: \"If set, return only the last N lines.\" },\n },\n required: [\"path\"],\n },\n fn: async (args: { path: string; head?: number; tail?: number }) => {\n const abs = safePath(args.path);\n const stat = await fs.stat(abs);\n if (stat.isDirectory()) {\n throw new Error(`not a file: ${args.path} (it's a directory)`);\n }\n const raw = await fs.readFile(abs);\n if (raw.length > maxReadBytes) {\n const head = raw.slice(0, maxReadBytes).toString(\"utf8\");\n return `${head}\\n\\n[…truncated ${raw.length - maxReadBytes} bytes — file is ${raw.length} B, cap ${maxReadBytes} B. Retry with head/tail for targeted view.]`;\n }\n const text = raw.toString(\"utf8\");\n if (typeof args.head === \"number\" && args.head > 0) {\n return text.split(/\\r?\\n/).slice(0, args.head).join(\"\\n\");\n }\n if (typeof args.tail === \"number\" && args.tail > 0) {\n let lines = text.split(/\\r?\\n/);\n // Most files end with a final '\\n', which splits into an empty\n // trailing string. Drop it before slicing so tail=2 returns the\n // last two *real* lines, not \"last line + empty\".\n if (lines.length > 0 && lines[lines.length - 1] === \"\") lines = lines.slice(0, -1);\n return lines.slice(Math.max(0, lines.length - args.tail)).join(\"\\n\");\n }\n return text;\n },\n });\n\n registry.register({\n name: \"list_directory\",\n description:\n \"List entries in a directory under the sandbox root. Returns one line per entry, marking directories with a trailing slash. Not recursive — use directory_tree for that.\",\n readOnly: true,\n parameters: {\n type: \"object\",\n properties: {\n path: { type: \"string\", description: \"Directory to list (default: root).\" },\n },\n },\n fn: async (args: { path?: string }) => {\n const abs = safePath(args.path ?? \".\");\n const entries = await fs.readdir(abs, { withFileTypes: true });\n const lines: string[] = [];\n for (const e of entries.sort((a, b) => a.name.localeCompare(b.name))) {\n lines.push(e.isDirectory() ? `${e.name}/` : e.name);\n }\n return lines.join(\"\\n\") || \"(empty directory)\";\n },\n });\n\n registry.register({\n name: \"directory_tree\",\n description:\n \"Recursively list entries in a directory. Shows indented tree structure with directories marked '/'. Caps output so a huge tree doesn't drown the context.\",\n readOnly: true,\n parameters: {\n type: \"object\",\n properties: {\n path: { type: \"string\", description: \"Root of the tree (default: sandbox root).\" },\n maxDepth: { type: \"integer\", description: \"Max recursion depth (default 4).\" },\n },\n },\n fn: async (args: { path?: string; maxDepth?: number }) => {\n const startAbs = safePath(args.path ?? \".\");\n const maxDepth = typeof args.maxDepth === \"number\" ? args.maxDepth : 4;\n const lines: string[] = [];\n let totalBytes = 0;\n let truncated = false;\n const walk = async (dir: string, depth: number): Promise<void> => {\n if (truncated) return;\n if (depth > maxDepth) return;\n let entries: import(\"node:fs\").Dirent[];\n try {\n entries = await fs.readdir(dir, { withFileTypes: true });\n } catch {\n return;\n }\n entries.sort((a, b) => a.name.localeCompare(b.name));\n for (const e of entries) {\n if (truncated) return;\n const indent = \" \".repeat(depth);\n const line = e.isDirectory() ? `${indent}${e.name}/` : `${indent}${e.name}`;\n totalBytes += line.length + 1;\n if (totalBytes > maxListBytes) {\n lines.push(` [… tree truncated at ${maxListBytes} bytes …]`);\n truncated = true;\n return;\n }\n lines.push(line);\n if (e.isDirectory()) {\n await walk(pathMod.join(dir, e.name), depth + 1);\n }\n }\n };\n await walk(startAbs, 0);\n return lines.join(\"\\n\") || \"(empty tree)\";\n },\n });\n\n registry.register({\n name: \"search_files\",\n description:\n \"Find files whose NAME matches a substring or regex. Case-insensitive. Walks the directory recursively under the sandbox root. Returns one path per line.\",\n readOnly: true,\n parameters: {\n type: \"object\",\n properties: {\n path: { type: \"string\", description: \"Directory to start the search at (default: root).\" },\n pattern: {\n type: \"string\",\n description: \"Substring (or regex) to match against filenames.\",\n },\n },\n required: [\"pattern\"],\n },\n fn: async (args: { path?: string; pattern: string }) => {\n const startAbs = safePath(args.path ?? \".\");\n const needle = args.pattern.toLowerCase();\n // Try as regex first (permits users who want patterns); fall\n // back to plain substring when it's not a valid regex. Flag `i`\n // so matching is case-insensitive regardless of path.\n let re: RegExp | null = null;\n try {\n re = new RegExp(args.pattern, \"i\");\n } catch {\n re = null;\n }\n const matches: string[] = [];\n let totalBytes = 0;\n const walk = async (dir: string): Promise<void> => {\n let entries: import(\"node:fs\").Dirent[];\n try {\n entries = await fs.readdir(dir, { withFileTypes: true });\n } catch {\n return;\n }\n for (const e of entries) {\n const full = pathMod.join(dir, e.name);\n const lower = e.name.toLowerCase();\n const hit = re ? re.test(e.name) : lower.includes(needle);\n if (hit) {\n const rel = pathMod.relative(rootDir, full);\n if (totalBytes + rel.length + 1 > maxListBytes) {\n matches.push(\"[… search truncated — refine pattern …]\");\n return;\n }\n matches.push(rel);\n totalBytes += rel.length + 1;\n }\n if (e.isDirectory()) await walk(full);\n }\n };\n await walk(startAbs);\n return matches.length === 0 ? \"(no matches)\" : matches.join(\"\\n\");\n },\n });\n\n registry.register({\n name: \"get_file_info\",\n description:\n \"Stat a path under the sandbox root. Returns type (file|directory|symlink), size in bytes, mtime in ISO-8601.\",\n readOnly: true,\n parameters: {\n type: \"object\",\n properties: {\n path: { type: \"string\" },\n },\n required: [\"path\"],\n },\n fn: async (args: { path: string }) => {\n const abs = safePath(args.path);\n const st = await fs.lstat(abs);\n const type = st.isDirectory() ? \"directory\" : st.isSymbolicLink() ? \"symlink\" : \"file\";\n return JSON.stringify({\n type,\n size: st.size,\n mtime: st.mtime.toISOString(),\n });\n },\n });\n\n if (!allowWriting) return registry;\n\n registry.register({\n name: \"write_file\",\n description:\n \"Create or overwrite a file under the sandbox root with the given content. Parent directories are created as needed.\",\n parameters: {\n type: \"object\",\n properties: {\n path: { type: \"string\" },\n content: { type: \"string\" },\n },\n required: [\"path\", \"content\"],\n },\n fn: async (args: { path: string; content: string }) => {\n const abs = safePath(args.path);\n await fs.mkdir(pathMod.dirname(abs), { recursive: true });\n await fs.writeFile(abs, args.content, \"utf8\");\n return `wrote ${args.content.length} chars to ${pathMod.relative(rootDir, abs)}`;\n },\n });\n\n registry.register({\n name: \"edit_file\",\n description:\n \"Apply a SEARCH/REPLACE edit to an existing file. `search` must match exactly (whitespace sensitive) — no regex. The match must be unique in the file; otherwise the edit is refused to avoid surprise rewrites.\",\n parameters: {\n type: \"object\",\n properties: {\n path: { type: \"string\" },\n search: { type: \"string\", description: \"Exact text to find (must be unique).\" },\n replace: { type: \"string\", description: \"Text to substitute in place of `search`.\" },\n },\n required: [\"path\", \"search\", \"replace\"],\n },\n fn: async (args: { path: string; search: string; replace: string }) => {\n const abs = safePath(args.path);\n const before = await fs.readFile(abs, \"utf8\");\n if (args.search.length === 0) {\n throw new Error(\"edit_file: search cannot be empty\");\n }\n const firstIdx = before.indexOf(args.search);\n if (firstIdx < 0) {\n throw new Error(`edit_file: search text not found in ${pathMod.relative(rootDir, abs)}`);\n }\n const nextIdx = before.indexOf(args.search, firstIdx + 1);\n if (nextIdx >= 0) {\n throw new Error(\n `edit_file: search text appears multiple times in ${pathMod.relative(rootDir, abs)} — include more context to disambiguate`,\n );\n }\n const after =\n before.slice(0, firstIdx) + args.replace + before.slice(firstIdx + args.search.length);\n await fs.writeFile(abs, after, \"utf8\");\n const rel = pathMod.relative(rootDir, abs);\n const header = `edited ${rel} (${args.search.length}→${args.replace.length} chars)`;\n // Starting line number of the search block in the original\n // file. `split/length` on the prefix gives a 1-based line\n // count where the match begins, matching git-diff's @@ -N,M\n // +N,M @@ header convention.\n const startLine = before.slice(0, firstIdx).split(/\\r?\\n/).length;\n const diff = renderEditDiff(args.search, args.replace, startLine);\n return `${header}\\n${diff}`;\n },\n });\n\n registry.register({\n name: \"create_directory\",\n description: \"Create a directory (and any missing parents) under the sandbox root.\",\n parameters: {\n type: \"object\",\n properties: { path: { type: \"string\" } },\n required: [\"path\"],\n },\n fn: async (args: { path: string }) => {\n const abs = safePath(args.path);\n await fs.mkdir(abs, { recursive: true });\n return `created ${pathMod.relative(rootDir, abs)}/`;\n },\n });\n\n registry.register({\n name: \"move_file\",\n description: \"Rename/move a file or directory under the sandbox root.\",\n parameters: {\n type: \"object\",\n properties: {\n source: { type: \"string\" },\n destination: { type: \"string\" },\n },\n required: [\"source\", \"destination\"],\n },\n fn: async (args: { source: string; destination: string }) => {\n const src = safePath(args.source);\n const dst = safePath(args.destination);\n await fs.mkdir(pathMod.dirname(dst), { recursive: true });\n await fs.rename(src, dst);\n return `moved ${pathMod.relative(rootDir, src)} → ${pathMod.relative(rootDir, dst)}`;\n },\n });\n\n return registry;\n}\n\n/**\n * Format an edit_file change as a proper line-level diff, styled\n * like `git diff`. Starts with a unified-diff hunk header —\n * `@@ -startLine,oldCount +startLine,newCount @@` — so the user\n * can tell where in the file the change lands. Body uses LCS\n * (longest common subsequence) to mark lines as removed (`-`),\n * added (`+`), or unchanged context (` `). Users were getting\n * hundreds of `-` followed by hundreds of `+` for tiny changes\n * because the naive \"dump both sides\" format can't tell what\n * actually moved vs. stayed — this fixes that and adds line-\n * number context on top.\n */\nfunction renderEditDiff(search: string, replace: string, startLine: number): string {\n const a = search.split(/\\r?\\n/);\n const b = replace.split(/\\r?\\n/);\n const diff = lineDiff(a, b);\n const hunk = `@@ -${startLine},${a.length} +${startLine},${b.length} @@`;\n const body = diff.map((d) => `${d.op === \" \" ? \" \" : d.op} ${d.line}`).join(\"\\n\");\n return `${hunk}\\n${body}`;\n}\n\n/**\n * Compute a line-level diff via classic LCS dynamic programming.\n * Good enough for SEARCH/REPLACE blocks where both sides are\n * typically under a few hundred lines — O(n*m) space + time. For\n * huge blocks we'd want Myers' algorithm, but the caller already\n * caps the inline-display size and `/tool N` shows the full result,\n * so quadratic is fine in practice.\n *\n * Exported so tests can exercise the diff logic without spinning\n * up the full tool dispatch path.\n */\nexport function lineDiff(\n a: readonly string[],\n b: readonly string[],\n): Array<{ op: \"-\" | \"+\" | \" \"; line: string }> {\n const n = a.length;\n const m = b.length;\n // dp[i][j] = LCS length of a[0..i) and b[0..j).\n const dp: number[][] = Array.from({ length: n + 1 }, () => new Array(m + 1).fill(0));\n for (let i = 1; i <= n; i++) {\n for (let j = 1; j <= m; j++) {\n if (a[i - 1] === b[j - 1]) dp[i]![j] = dp[i - 1]![j - 1]! + 1;\n else dp[i]![j] = Math.max(dp[i - 1]![j]!, dp[i]![j - 1]!);\n }\n }\n // Backtrack to recover the op sequence.\n const out: Array<{ op: \"-\" | \"+\" | \" \"; line: string }> = [];\n let i = n;\n let j = m;\n while (i > 0 && j > 0) {\n if (a[i - 1] === b[j - 1]) {\n out.unshift({ op: \" \", line: a[i - 1]! });\n i--;\n j--;\n } else if ((dp[i - 1]![j] ?? 0) > (dp[i]![j - 1] ?? 0)) {\n out.unshift({ op: \"-\", line: a[i - 1]! });\n i--;\n } else {\n // Tie-break goes here (strictly less or equal): take the\n // insertion first during backtrack so the final forward order\n // renders removals BEFORE additions for a substitution —\n // matches git-diff convention of `- old / + new`.\n out.unshift({ op: \"+\", line: b[j - 1]! });\n j--;\n }\n }\n while (i > 0) {\n out.unshift({ op: \"-\", line: a[i - 1]! });\n i--;\n }\n while (j > 0) {\n out.unshift({ op: \"+\", line: b[j - 1]! });\n j--;\n }\n return out;\n}\n","/**\n * `remember` / `forget` / `recall_memory` — tools that let the model\n * read and write the user-memory store across sessions.\n *\n * Scope rules:\n * - `global` — always available (no sandbox needed).\n * - `project` — requires a `projectRoot` on MemoryStore. In chat mode\n * (no sandbox), the tools still register but a `scope=project` call\n * returns a structured refusal so the model can try `global` instead.\n *\n * Memory changes are written eagerly but NOT re-loaded into the prefix\n * mid-session (cache invariant). The user notices at `/new` or the next\n * launch — or they can read fresh content via `recall_memory` which\n * always hits disk.\n */\n\nimport type { ToolRegistry } from \"../tools.js\";\nimport {\n type MemoryScope,\n MemoryStore,\n type MemoryType,\n sanitizeMemoryName,\n} from \"../user-memory.js\";\n\nexport interface MemoryToolsOptions {\n /** Sandbox root for the `project` scope. Omit for chat mode. */\n projectRoot?: string;\n /** Override `~/.reasonix` (tests). */\n homeDir?: string;\n}\n\nexport function registerMemoryTools(\n registry: ToolRegistry,\n opts: MemoryToolsOptions = {},\n): ToolRegistry {\n const store = new MemoryStore({ homeDir: opts.homeDir, projectRoot: opts.projectRoot });\n const hasProject = store.hasProjectScope();\n\n registry.register({\n name: \"remember\",\n description:\n \"Save a memory for future sessions. Use when the user states a preference, corrects your approach, shares a non-obvious fact about this project, or explicitly asks you to remember something. Don't remember transient task state — only things worth recalling next session. The memory is written now but won't re-load into the system prompt until the next `/new` or launch.\",\n parameters: {\n type: \"object\",\n properties: {\n type: {\n type: \"string\",\n enum: [\"user\", \"feedback\", \"project\", \"reference\"],\n description:\n \"'user' = role/skills/prefs; 'feedback' = corrections or confirmed approaches; 'project' = facts/decisions about the current work; 'reference' = pointers to external systems the user uses.\",\n },\n scope: {\n type: \"string\",\n enum: [\"global\", \"project\"],\n description:\n \"'global' = applies across every project (preferences, tooling); 'project' = scoped to the current sandbox (decisions, local facts). Only available in `reasonix code`.\",\n },\n name: {\n type: \"string\",\n description:\n \"filename-safe identifier, 3-40 chars, alnum + _ - . (no path separators, no leading dot).\",\n },\n description: {\n type: \"string\",\n description: \"One-line summary shown in MEMORY.md (under ~150 chars).\",\n },\n content: {\n type: \"string\",\n description:\n \"Full memory body in markdown. For feedback/project types, structure as: rule/fact, then **Why:** line, then **How to apply:** line.\",\n },\n },\n required: [\"type\", \"scope\", \"name\", \"description\", \"content\"],\n },\n fn: async (args: {\n type: MemoryType;\n scope: MemoryScope;\n name: string;\n description: string;\n content: string;\n }) => {\n if (args.scope === \"project\" && !hasProject) {\n return JSON.stringify({\n error:\n \"scope='project' is unavailable in this session (no sandbox root). Retry with scope='global', or ask the user to switch to `reasonix code` for project-scoped memory.\",\n });\n }\n try {\n const path = store.write({\n name: args.name,\n type: args.type,\n scope: args.scope,\n description: args.description,\n body: args.content,\n });\n const key = sanitizeMemoryName(args.name);\n // The return text is load-bearing: it's the ONLY thing keeping\n // the fact visible within the current session, because the\n // prefix isn't re-hashed mid-session (Pillar 1). R1 reads this\n // on its next turn — the wording is deliberately imperative so\n // it doesn't get ignored in favor of explore-first behavior.\n return [\n `✓ REMEMBERED (${args.scope}/${key}): ${args.description}`,\n \"\",\n \"TREAT THIS AS ESTABLISHED FACT for the rest of this session.\",\n \"The user just told you — don't re-explore the filesystem to re-derive it.\",\n `(Saved to ${path}; pins into the system prompt on next /new or launch.)`,\n ].join(\"\\n\");\n } catch (err) {\n return JSON.stringify({ error: `remember failed: ${(err as Error).message}` });\n }\n },\n });\n\n registry.register({\n name: \"forget\",\n description:\n \"Delete a memory file and remove it from MEMORY.md. Use when the user explicitly asks to forget something, or when a previously-remembered fact has become wrong. Irreversible — no tombstone.\",\n parameters: {\n type: \"object\",\n properties: {\n name: { type: \"string\", description: \"Memory name (the identifier used in `remember`).\" },\n scope: { type: \"string\", enum: [\"global\", \"project\"] },\n },\n required: [\"name\", \"scope\"],\n },\n fn: async (args: { name: string; scope: MemoryScope }) => {\n if (args.scope === \"project\" && !hasProject) {\n return JSON.stringify({\n error: \"scope='project' is unavailable in this session (no sandbox root).\",\n });\n }\n try {\n const existed = store.delete(args.scope, args.name);\n return existed\n ? `forgot (${args.scope}/${sanitizeMemoryName(args.name)}). Re-load on next /new or launch.`\n : `no such memory: ${args.scope}/${args.name} (nothing to forget).`;\n } catch (err) {\n return JSON.stringify({ error: `forget failed: ${(err as Error).message}` });\n }\n },\n });\n\n registry.register({\n name: \"recall_memory\",\n description:\n \"Read the full body of a memory file when its MEMORY.md one-liner (already in the system prompt) isn't enough detail. Most of the time the index suffices — only call this when the user's question genuinely requires the full context.\",\n readOnly: true,\n parameters: {\n type: \"object\",\n properties: {\n name: { type: \"string\" },\n scope: { type: \"string\", enum: [\"global\", \"project\"] },\n },\n required: [\"name\", \"scope\"],\n },\n fn: async (args: { name: string; scope: MemoryScope }) => {\n if (args.scope === \"project\" && !hasProject) {\n return JSON.stringify({\n error: \"scope='project' is unavailable in this session (no sandbox root).\",\n });\n }\n try {\n const entry = store.read(args.scope, args.name);\n return [\n `# ${entry.name} (${entry.scope}/${entry.type}, created ${entry.createdAt || \"?\"})`,\n entry.description ? `> ${entry.description}` : \"\",\n \"\",\n entry.body,\n ]\n .filter(Boolean)\n .join(\"\\n\");\n } catch (err) {\n return JSON.stringify({ error: `recall failed: ${(err as Error).message}` });\n }\n },\n });\n\n return registry;\n}\n","/**\n * Plan Mode — read-only exploration phase for `reasonix code`.\n *\n * Shape (mirrors claude-code's plan/act split, adapted for Reasonix):\n *\n * 1. User types `/plan` → registry switches to plan-mode enforcement\n * (write tools refused at dispatch; reads + allowlisted shell\n * still work).\n * 2. Model explores, then calls `submit_plan` with a markdown plan.\n * 3. `submit_plan` throws `PlanProposedError`, which the TUI renders\n * as a picker: Approve / Refine / Cancel.\n * 4. Approve → registry leaves plan mode, a synthetic user message\n * \"The plan has been approved. Implement it now.\" is pushed into\n * the loop so the next turn executes.\n *\n * The read-only enforcement lives in `ToolRegistry.dispatch` via\n * `readOnly` / `readOnlyCheck`; this file only ships the `submit_plan`\n * escape hatch and the error type that carries the plan out of the\n * registry without stuffing it into the message.\n *\n * We do not change `ImmutablePrefix.toolSpecs` when plan mode toggles —\n * that would break Pillar 1's prefix cache. Instead the same full spec\n * list stays pinned, and the registry enforces mode at dispatch time.\n * The refusal string teaches the model the rule; cache hits stay\n * intact.\n */\n\nimport type { ToolRegistry } from \"../tools.js\";\n\n/**\n * Thrown by `submit_plan` when plan mode is active, carrying the plan\n * text the TUI will render for the user's approval.\n *\n * Implements the `toToolResult` protocol so `ToolRegistry.dispatch`\n * serializes the full plan into the tool-result JSON (not just the\n * error message). The TUI parses `{ error, plan }` from the tool event\n * and mounts the `PlanConfirm` picker.\n */\nexport class PlanProposedError extends Error {\n readonly plan: string;\n constructor(plan: string) {\n super(\n \"PlanProposedError: plan submitted. STOP calling tools now — the TUI has shown the plan to the user. Wait for their next message; it will either approve (you'll then implement the plan), request a refinement (you should explore more and submit an updated plan), or cancel (drop the plan and ask what they want instead). Don't call any tools in the meantime.\",\n );\n this.name = \"PlanProposedError\";\n this.plan = plan;\n }\n\n /**\n * Structured tool-result shape. Consumed by the TUI to extract the\n * plan without regex-scraping the error message.\n */\n toToolResult(): { error: string; plan: string } {\n return { error: `${this.name}: ${this.message}`, plan: this.plan };\n }\n}\n\nexport interface PlanToolOptions {\n /**\n * Optional side-channel callback fired when the model submits a plan.\n * The TUI uses this to preview the plan in real time (the tool-result\n * event is also emitted; this is just earlier and friendlier to\n * test harnesses that don't want to parse JSON).\n */\n onPlanSubmitted?: (plan: string) => void;\n}\n\nexport function registerPlanTool(registry: ToolRegistry, opts: PlanToolOptions = {}): ToolRegistry {\n registry.register({\n name: \"submit_plan\",\n description:\n \"Submit a concrete plan to the user for review before executing. Use this for tasks that warrant a review gate — multi-file refactors, architecture changes, anything that would be expensive or confusing to undo. Skip it for small fixes (one-line typo, obvious bug with a clear fix) — just make the change. The user will either approve (you then implement it), ask for refinement, or cancel. If the user has already enabled /plan mode, writes are blocked at dispatch and you MUST use this. Write the plan as markdown with a one-line summary, a bulleted list of files to touch and what will change, and any risks or open questions.\",\n readOnly: true,\n parameters: {\n type: \"object\",\n properties: {\n plan: {\n type: \"string\",\n description:\n \"Markdown-formatted plan. Lead with a one-sentence summary. Then a file-by-file breakdown of what you'll change and why. Flag any risks or open questions at the end so the user can weigh in before you start.\",\n },\n },\n required: [\"plan\"],\n },\n fn: async (args: { plan: string }) => {\n const plan = (args?.plan ?? \"\").trim();\n if (!plan) {\n throw new Error(\"submit_plan: empty plan — write a markdown plan and try again.\");\n }\n // Always fire the picker, not just inside plan mode. Plan mode's\n // role is the *stronger* constraint — it forces you into read-only\n // until you submit. Outside plan mode, submit_plan is your own\n // call: use it when the task is large enough to deserve a review\n // gate (multi-file refactors, architecture changes, anything\n // that would be expensive to undo), skip it for small fixes.\n opts.onPlanSubmitted?.(plan);\n throw new PlanProposedError(plan);\n },\n });\n return registry;\n}\n","/**\n * Native shell tool — lets the model run commands inside the sandbox\n * root so it can actually verify its own work (run tests, check git\n * status, inspect a lockfile, etc.). Without this the coding-mode\n * loop is \"write code, hope it works, ask the user to run it\" —\n * defeats the purpose.\n *\n * Safety model:\n * - Commands run with `cwd` pinned to the registered root. No\n * path traversal via the command itself is enforced (users can\n * `cat ../outside.txt`); the trust boundary is the directory\n * you opened Reasonix from.\n * - Commands are matched against a read-only / testing allowlist.\n * Allowlisted commands execute immediately and return stdout +\n * stderr merged. Everything else throws with a clear message —\n * the UI translates that into an `/apply`-style confirm gate so\n * the user sees the exact command before it runs.\n * - Default timeout: 60s. Output cap: matches tool-result budget.\n * - Every command that DOES run is spawned with `shell: false` and\n * a tokenized argv — no string-to-shell interpolation, so the\n * model can't accidentally construct a chained `rm` via quoting.\n *\n * This is intentionally narrower than what Claude Code / Aider ship:\n * we gate more commands behind confirmation by default. Users who\n * trust the model can widen the allowlist by instantiating their\n * own tool registry.\n */\n\nimport { type SpawnOptions, spawn } from \"node:child_process\";\nimport { existsSync, statSync } from \"node:fs\";\nimport * as pathMod from \"node:path\";\nimport type { ToolRegistry } from \"../tools.js\";\n\nexport interface ShellToolsOptions {\n /** Directory to run commands in. Must be an absolute path. */\n rootDir: string;\n /** Seconds before an individual command is killed. Default: 60. */\n timeoutSec?: number;\n /**\n * Per-command stdout+stderr cap in characters. Default: 32_000 to\n * match the tool-result budget.\n */\n maxOutputChars?: number;\n /**\n * Extra command-name prefixes the user explicitly trusts. Added on\n * top of the built-in allowlist. Examples: `[\"my-ci-script\", \"lint\"]`.\n *\n * Accepts either a fixed array (captured once at registration) or a\n * getter called on every dispatch. The getter form is load-bearing:\n * when the TUI's `ShellConfirm` writes a new prefix to config mid-\n * session, the running `run_command` must pick it up immediately —\n * otherwise the same command gets re-prompted until the next launch.\n */\n extraAllowed?: readonly string[] | (() => readonly string[]);\n /**\n * When true, skip the allowlist entirely and auto-run every command.\n * Off by default — this is an escape hatch for non-interactive use\n * (CI, benchmarks) where a human can't be in the loop to confirm.\n */\n allowAll?: boolean;\n}\n\nconst DEFAULT_TIMEOUT_SEC = 60;\nconst DEFAULT_MAX_OUTPUT_CHARS = 32_000;\n\n/**\n * Command prefixes we consider safe to run without asking the user.\n * Rule of thumb: read-only reports, or test runners whose failure mode\n * is \"exit 1 with output.\" Nothing that can rewrite state, escalate,\n * or touch the network.\n */\nconst BUILTIN_ALLOWLIST: ReadonlyArray<string> = [\n // Repo inspection\n \"git status\",\n \"git diff\",\n \"git log\",\n \"git show\",\n \"git blame\",\n \"git branch\",\n \"git remote\",\n \"git rev-parse\",\n \"git config --get\",\n // Filesystem inspection\n \"ls\",\n \"pwd\",\n \"cat\",\n \"head\",\n \"tail\",\n \"wc\",\n \"file\",\n \"tree\",\n \"find\",\n \"grep\",\n \"rg\",\n // Language version probes\n \"node --version\",\n \"node -v\",\n \"npm --version\",\n \"npx --version\",\n \"python --version\",\n \"python3 --version\",\n \"cargo --version\",\n \"go version\",\n \"rustc --version\",\n \"deno --version\",\n \"bun --version\",\n // Test runners (non-destructive by convention)\n \"npm test\",\n \"npm run test\",\n \"npx vitest run\",\n \"npx vitest\",\n \"npx jest\",\n \"pytest\",\n \"python -m pytest\",\n \"cargo test\",\n \"cargo check\",\n \"cargo clippy\",\n \"go test\",\n \"go vet\",\n \"deno test\",\n \"bun test\",\n // Linters / typecheckers (read-only by convention)\n \"npm run lint\",\n \"npm run typecheck\",\n \"npx tsc --noEmit\",\n \"npx biome check\",\n \"npx eslint\",\n \"npx prettier --check\",\n \"ruff\",\n \"mypy\",\n];\n\n/**\n * Tokenize a shell-ish command string into argv. Handles single/double\n * quoting; rejects unclosed quotes. Does NOT expand env vars, globs,\n * backticks, or `$(…)` — the goal is to prevent the model from\n * accidentally (or not) sneaking arbitrary shells past the allowlist\n * via concatenation. Exported for testing.\n */\nexport function tokenizeCommand(cmd: string): string[] {\n const out: string[] = [];\n let cur = \"\";\n let quote: '\"' | \"'\" | null = null;\n for (let i = 0; i < cmd.length; i++) {\n const ch = cmd[i]!;\n if (quote) {\n if (ch === quote) {\n quote = null;\n } else if (ch === \"\\\\\" && quote === '\"' && i + 1 < cmd.length) {\n cur += cmd[++i];\n } else {\n cur += ch;\n }\n continue;\n }\n if (ch === '\"' || ch === \"'\") {\n quote = ch;\n continue;\n }\n if (ch === \" \" || ch === \"\\t\") {\n if (cur.length > 0) {\n out.push(cur);\n cur = \"\";\n }\n continue;\n }\n cur += ch;\n }\n if (quote) throw new Error(`unclosed ${quote} in command`);\n if (cur.length > 0) out.push(cur);\n return out;\n}\n\n/**\n * Return true when `cmd` matches an allowlisted prefix. Exported for\n * testing. Match is on the space-normalized leading tokens so\n * `git status -s ` and `git status` both match `git status`.\n */\nexport function isAllowed(cmd: string, extra: readonly string[] = []): boolean {\n const normalized = cmd.trim().replace(/\\s+/g, \" \");\n const allowlist = [...BUILTIN_ALLOWLIST, ...extra];\n for (const prefix of allowlist) {\n if (normalized === prefix) return true;\n if (normalized.startsWith(`${prefix} `)) return true;\n }\n return false;\n}\n\nexport interface RunCommandResult {\n exitCode: number | null;\n /** Combined stdout+stderr, truncated to `maxOutputChars` with a marker. */\n output: string;\n /** True when the process was killed for exceeding `timeoutSec`. */\n timedOut: boolean;\n}\n\nexport async function runCommand(\n cmd: string,\n opts: {\n cwd: string;\n timeoutSec?: number;\n maxOutputChars?: number;\n signal?: AbortSignal;\n },\n): Promise<RunCommandResult> {\n const argv = tokenizeCommand(cmd);\n if (argv.length === 0) throw new Error(\"run_command: empty command\");\n const timeoutMs = (opts.timeoutSec ?? DEFAULT_TIMEOUT_SEC) * 1000;\n const maxChars = opts.maxOutputChars ?? DEFAULT_MAX_OUTPUT_CHARS;\n\n const spawnOpts: SpawnOptions = {\n cwd: opts.cwd,\n shell: false, // no shell-expansion — see header comment\n windowsHide: true,\n env: process.env,\n };\n\n // Windows: two layered fixes on top of shell:false —\n // 1. Resolve bare command names via PATH × PATHEXT (CreateProcess\n // ignores PATHEXT, so `npm` alone misses `npm.cmd`).\n // 2. Node 21.7.3+ (CVE-2024-27980) refuses to spawn `.cmd`/`.bat`\n // directly even with shell:false and safe args — throws\n // EINVAL at invocation time. Wrap those via `cmd.exe /d /s /c`\n // with verbatim args + manual quoting, so shell metacharacters\n // in arguments stay literal.\n // Unix path is unchanged.\n const { bin, args, spawnOverrides } = prepareSpawn(argv);\n const effectiveSpawnOpts = { ...spawnOpts, ...spawnOverrides };\n\n return await new Promise<RunCommandResult>((resolve, reject) => {\n let child: import(\"node:child_process\").ChildProcess;\n try {\n child = spawn(bin, args, effectiveSpawnOpts);\n } catch (err) {\n reject(err);\n return;\n }\n let buf = \"\";\n let timedOut = false;\n const killTimer = setTimeout(() => {\n timedOut = true;\n child.kill(\"SIGKILL\");\n }, timeoutMs);\n const onAbort = () => child.kill(\"SIGKILL\");\n opts.signal?.addEventListener(\"abort\", onAbort, { once: true });\n\n const onData = (chunk: Buffer | string) => {\n buf += chunk.toString();\n // Soft cap: we let the process keep running (killing early could\n // hide a real failure), but we stop growing the buffer past 2×\n // the cap so a chatty test can't OOM us.\n if (buf.length > maxChars * 2) buf = `${buf.slice(0, maxChars * 2)}`;\n };\n child.stdout?.on(\"data\", onData);\n child.stderr?.on(\"data\", onData);\n child.on(\"error\", (err) => {\n clearTimeout(killTimer);\n opts.signal?.removeEventListener(\"abort\", onAbort);\n reject(err);\n });\n child.on(\"close\", (code) => {\n clearTimeout(killTimer);\n opts.signal?.removeEventListener(\"abort\", onAbort);\n const output =\n buf.length > maxChars\n ? `${buf.slice(0, maxChars)}\\n\\n[… truncated ${buf.length - maxChars} chars …]`\n : buf;\n resolve({ exitCode: code, output, timedOut });\n });\n });\n}\n\n/**\n * Test/override hooks for {@link resolveExecutable}. Omitting any field\n * falls through to the real process globals — the runtime call path\n * uses defaults; tests inject `platform` + `env` + `isFile` to exercise\n * Windows-specific lookup from a Linux CI runner without touching\n * actual fs.\n */\nexport interface ResolveExecutableOptions {\n platform?: NodeJS.Platform;\n env?: { PATH?: string; PATHEXT?: string };\n /** Predicate swapped in by tests to avoid creating real files. */\n isFile?: (path: string) => boolean;\n /** Path.join used for the lookup. Defaults to Windows semantics on Windows. */\n pathDelimiter?: string;\n}\n\n/**\n * Resolve a bare command name (e.g. `npm`) to its on-disk path via\n * PATH × PATHEXT on Windows. Returns the input unchanged on non-Windows\n * platforms, when the input is already a path (contains `/`, `\\`, or is\n * absolute), or when no match is found in PATH × PATHEXT (caller gets a\n * natural ENOENT from spawn, which surfaces cleanly).\n *\n * Why this exists: `child_process.spawn` with `shell: false` invokes\n * Windows `CreateProcess`, which does not honor `PATHEXT` and does not\n * search for `.cmd` / `.bat` wrappers. Node-ecosystem tools ship as\n * `npm.cmd`, `npx.cmd`, `yarn.cmd`, etc., so a bare `npm` fails with\n * ENOENT under `shell: false`. Flipping to `shell: true` would work\n * but reintroduces shell-expansion (pipes, redirects, chained cmds)\n * that the tool was explicitly designed to forbid. This resolver\n * threads the needle.\n */\nexport function resolveExecutable(cmd: string, opts: ResolveExecutableOptions = {}): string {\n const platform = opts.platform ?? process.platform;\n if (platform !== \"win32\") return cmd;\n if (!cmd) return cmd;\n // Already a path fragment — spawn handles these natively.\n if (cmd.includes(\"/\") || cmd.includes(\"\\\\\") || pathMod.isAbsolute(cmd)) return cmd;\n // If the model wrote `npm.cmd` explicitly, respect that verbatim.\n if (pathMod.extname(cmd)) return cmd;\n\n const env = opts.env ?? process.env;\n const pathExt = (env.PATHEXT ?? \".COM;.EXE;.BAT;.CMD\")\n .split(\";\")\n .map((e) => e.trim())\n .filter(Boolean);\n const delimiter = opts.pathDelimiter ?? (platform === \"win32\" ? \";\" : pathMod.delimiter);\n const pathDirs = (env.PATH ?? \"\").split(delimiter).filter(Boolean);\n const isFile = opts.isFile ?? defaultIsFile;\n\n for (const dir of pathDirs) {\n for (const ext of pathExt) {\n // Force win32 join so CI tests that pass `platform: \"win32\"`\n // from a Linux runner get backslash-joined paths; the real-\n // Windows runtime path lands here too and gets the correct\n // separator regardless of where pathMod defaults.\n const full = pathMod.win32.join(dir, cmd + ext);\n if (isFile(full)) return full;\n }\n }\n return cmd;\n}\n\nfunction defaultIsFile(full: string): boolean {\n try {\n return existsSync(full) && statSync(full).isFile();\n } catch {\n return false;\n }\n}\n\n/**\n * Prepare `(bin, args, spawnOpts)` for the runCommand spawn call,\n * applying Windows-specific workarounds for (a) PATHEXT lookup and\n * (b) the CVE-2024-27980 prohibition on direct `.cmd`/`.bat` spawns.\n *\n * Exported so tests can assert the transformation without booting an\n * actual child process.\n */\nexport function prepareSpawn(\n argv: readonly string[],\n opts: ResolveExecutableOptions = {},\n): { bin: string; args: string[]; spawnOverrides: SpawnOptions } {\n const head = argv[0] ?? \"\";\n const tail = argv.slice(1);\n const platform = opts.platform ?? process.platform;\n const resolved = resolveExecutable(head, opts);\n\n if (platform !== \"win32\") {\n return { bin: resolved, args: [...tail], spawnOverrides: {} };\n }\n\n // `.cmd` / `.bat` wrappers require cmd.exe on post-CVE Node.\n if (/\\.(cmd|bat)$/i.test(resolved)) {\n const cmdline = [resolved, ...tail].map(quoteForCmdExe).join(\" \");\n return {\n bin: \"cmd.exe\",\n args: [\"/d\", \"/s\", \"/c\", cmdline],\n // windowsVerbatimArguments prevents Node from re-quoting the /c\n // payload — we've already composed an exact cmd.exe command\n // line. Without this Node wraps our already-quoted string in\n // another round of quotes and cmd.exe can't parse it.\n spawnOverrides: { windowsVerbatimArguments: true },\n };\n }\n\n // Bare command names that PATH × PATHEXT couldn't resolve to an\n // on-disk file — these are almost always cmd.exe built-ins (`dir`,\n // `echo`, `type`, `ver`, `vol`, `where`, `help`, …) which don't\n // exist as standalone executables. Direct spawn crashes with ENOENT;\n // routing through cmd.exe lets the built-in resolve, and if it's\n // genuinely unknown the user gets the standard \"'foo' is not\n // recognized\" message instead of a raw spawn failure.\n if (isBareWindowsName(resolved) && resolved === head) {\n const cmdline = [head, ...tail].map(quoteForCmdExe).join(\" \");\n return {\n bin: \"cmd.exe\",\n args: [\"/d\", \"/s\", \"/c\", cmdline],\n spawnOverrides: { windowsVerbatimArguments: true },\n };\n }\n\n return { bin: resolved, args: [...tail], spawnOverrides: {} };\n}\n\n/**\n * True when `s` looks like a bare executable name — no path separator,\n * no drive letter, no extension. Such names on Windows, when absent\n * from PATH × PATHEXT, are almost always cmd.exe built-ins.\n */\nfunction isBareWindowsName(s: string): boolean {\n if (!s) return false;\n if (s.includes(\"/\") || s.includes(\"\\\\\")) return false;\n if (pathMod.isAbsolute(s)) return false;\n if (pathMod.extname(s)) return false;\n return true;\n}\n\n/**\n * Quote an argument so cmd.exe parses it back as a single token. We\n * always wrap in double quotes when the arg contains whitespace or\n * any cmd.exe metacharacter, doubling embedded quotes per cmd.exe's\n * `\"\"` escape rule. Bare alphanumeric args pass through unquoted for\n * readability in logs.\n *\n * Exported for test coverage of the quoting semantics.\n */\nexport function quoteForCmdExe(arg: string): string {\n if (arg === \"\") return '\"\"';\n if (!/[\\s\"&|<>^%(),;!]/.test(arg)) return arg;\n return `\"${arg.replace(/\"/g, '\"\"')}\"`;\n}\n\n/** Error thrown by `run_command` when the command isn't allowlisted. */\nexport class NeedsConfirmationError extends Error {\n readonly command: string;\n constructor(command: string) {\n super(\n `run_command: \"${command}\" needs the user's approval before it runs. STOP calling tools now — the TUI has already prompted the user to press y (run) or n (deny). Wait for their next message; it will either be the command's output (if they approved) or an instruction to continue without it (if they denied). Don't retry the command or call other shell commands in the meantime.`,\n );\n this.name = \"NeedsConfirmationError\";\n this.command = command;\n }\n}\n\nexport function registerShellTools(registry: ToolRegistry, opts: ShellToolsOptions): ToolRegistry {\n const rootDir = pathMod.resolve(opts.rootDir);\n const timeoutSec = opts.timeoutSec ?? DEFAULT_TIMEOUT_SEC;\n const maxOutputChars = opts.maxOutputChars ?? DEFAULT_MAX_OUTPUT_CHARS;\n // Resolved on every dispatch so newly-persisted \"always allow\"\n // prefixes take effect inside the session that added them, not just\n // on the next launch. Static arrays are wrapped into a constant\n // getter so the call site below is uniform.\n const getExtraAllowed: () => readonly string[] =\n typeof opts.extraAllowed === \"function\"\n ? opts.extraAllowed\n : (() => {\n const snapshot = opts.extraAllowed ?? [];\n return () => snapshot;\n })();\n const allowAll = opts.allowAll ?? false;\n\n registry.register({\n name: \"run_command\",\n description:\n \"Run a shell command in the project root and return its combined stdout+stderr. Common read-only inspection and test/lint/typecheck commands run immediately; anything that could mutate state, install dependencies, or touch the network is refused until the user confirms it in the TUI. Prefer this over asking the user to run a command manually — after edits, run the project's tests to verify.\",\n // Plan-mode gate: allow allowlisted commands through (git status,\n // cargo check, ls, grep …) so the model can actually investigate\n // during planning. Anything that would otherwise trigger a\n // confirmation prompt is treated as \"not read-only\" and bounced.\n readOnlyCheck: (args: { command?: unknown }) => {\n if (allowAll) return true;\n const cmd = typeof args?.command === \"string\" ? args.command.trim() : \"\";\n if (!cmd) return false;\n return isAllowed(cmd, getExtraAllowed());\n },\n parameters: {\n type: \"object\",\n properties: {\n command: {\n type: \"string\",\n description:\n \"Full command line. Tokenized with POSIX-ish quoting; no shell expansion, no pipes, no redirects.\",\n },\n timeoutSec: {\n type: \"integer\",\n description: `Override the default ${timeoutSec}s timeout for a single command.`,\n },\n },\n required: [\"command\"],\n },\n fn: async (args: { command: string; timeoutSec?: number }, ctx) => {\n const cmd = args.command.trim();\n if (!cmd) throw new Error(\"run_command: empty command\");\n if (!allowAll && !isAllowed(cmd, getExtraAllowed())) {\n throw new NeedsConfirmationError(cmd);\n }\n const effectiveTimeout = Math.max(1, Math.min(600, args.timeoutSec ?? timeoutSec));\n const result = await runCommand(cmd, {\n cwd: rootDir,\n timeoutSec: effectiveTimeout,\n maxOutputChars,\n signal: ctx?.signal,\n });\n return formatCommandResult(cmd, result);\n },\n });\n\n return registry;\n}\n\nexport function formatCommandResult(cmd: string, r: RunCommandResult): string {\n const header = r.timedOut\n ? `$ ${cmd}\\n[killed after timeout]`\n : `$ ${cmd}\\n[exit ${r.exitCode ?? \"?\"}]`;\n return r.output ? `${header}\\n${r.output}` : header;\n}\n","/**\n * Built-in web search + fetch tools.\n *\n * - `web_search(query, topK?)` — Mojeek's public search page. No API\n * key, no signup. We originally shipped this backed by DuckDuckGo's\n * HTML endpoint, but DDG started serving anti-bot interstitials\n * (HTTP 202 with a challenge page) for every unauthenticated POST.\n * Mojeek runs its own independent index, is bot-friendly, and\n * returns parseable HTML.\n * - `web_fetch(url)` — HTTP GET + naïve HTML-to-text extraction.\n *\n * Both are registered by default on `reasonix chat` / `reasonix code`;\n * set `search: false` in config (or `REASONIX_SEARCH=off`) to turn\n * them off. The model decides when to call them based on the query —\n * no slash command required.\n */\n\nimport type { ToolRegistry } from \"../tools.js\";\n\nexport interface SearchResult {\n title: string;\n url: string;\n snippet: string;\n}\n\nexport interface PageContent {\n url: string;\n title?: string;\n text: string;\n /** True when the extracted text was clipped to fit the cap. */\n truncated: boolean;\n}\n\nexport interface WebFetchOptions {\n /** Max bytes of extracted text. Defaults to 32_000 to match tool-result cap. */\n maxChars?: number;\n /** Timeout in ms. Defaults to 15_000. */\n timeoutMs?: number;\n signal?: AbortSignal;\n}\n\nexport interface WebSearchOptions {\n topK?: number;\n signal?: AbortSignal;\n}\n\nconst DEFAULT_FETCH_MAX_CHARS = 32_000;\nconst DEFAULT_FETCH_TIMEOUT_MS = 15_000;\nconst DEFAULT_TOPK = 5;\n// Real-browser UA. Servers like Mojeek are bot-friendly but still gate\n// obvious scraper UAs; a stock Chrome string avoids the fast-path block.\nconst USER_AGENT =\n \"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36\";\nconst MOJEEK_ENDPOINT = \"https://www.mojeek.com/search\";\n\n/**\n * Search the public web via Mojeek. Returns up to `topK` ranked\n * results with title, url, snippet.\n *\n * Mojeek is an independent index (not a Google/Bing front-end) which\n * means coverage on niche or very recent topics can be thinner, but\n * it's reliable from scripts and doesn't gate on cookies or sessions.\n * If the response has 0 results we distinguish \"truly empty\" from\n * \"layout changed or blocked\" so the caller isn't left guessing.\n */\nexport async function webSearch(\n query: string,\n opts: WebSearchOptions = {},\n): Promise<SearchResult[]> {\n const topK = Math.max(1, Math.min(10, opts.topK ?? DEFAULT_TOPK));\n const resp = await fetch(`${MOJEEK_ENDPOINT}?q=${encodeURIComponent(query)}`, {\n headers: {\n \"User-Agent\": USER_AGENT,\n Accept: \"text/html,application/xhtml+xml,application/xml;q=0.9\",\n \"Accept-Language\": \"en-US,en;q=0.9\",\n },\n signal: opts.signal,\n redirect: \"follow\",\n });\n if (!resp.ok) throw new Error(`web_search ${resp.status}`);\n const html = await resp.text();\n const results = parseMojeekResults(html).slice(0, topK);\n if (results.length === 0) {\n if (/no results found|did not match any documents/i.test(html)) return [];\n if (/captcha|verify you are human|access denied|forbidden/i.test(html)) {\n throw new Error(\"web_search: Mojeek anti-bot page — rate-limited or blocked\");\n }\n throw new Error(\n `web_search: 0 results but response doesn't look like a real empty page (${html.length} chars, first 120: ${html.slice(0, 120).replace(/\\s+/g, \" \")})`,\n );\n }\n return results;\n}\n\n/**\n * Extract results from a Mojeek search page.\n *\n * Mojeek's stable shape (as of April 2026):\n * <a … class=\"ob\" href=\"URL\"> … breadcrumb … </a>\n * <h2><a class=\"title\" href=\"URL\">Title</a></h2>\n * <p class=\"s\">snippet text …</p>\n *\n * We do two tolerant passes — title anchors, then snippet paragraphs —\n * and pair them positionally. Attribute order inside a tag varies\n * between versions, so each pass captures the whole element and we\n * re-extract href / inner text with a second regex. Exported for\n * unit testing against a fixture.\n */\nexport function parseMojeekResults(html: string): SearchResult[] {\n const titles: string[] = [];\n const titleAnchorRe = /<a\\b[^>]*\\bclass=\"title\"[^>]*>[\\s\\S]*?<\\/a>/g;\n let m: RegExpExecArray | null;\n while (true) {\n m = titleAnchorRe.exec(html);\n if (m === null) break;\n titles.push(m[0]);\n }\n\n const snippets: string[] = [];\n const snippetRe = /<p\\b[^>]*\\bclass=\"s\"[^>]*>([\\s\\S]*?)<\\/p>/g;\n while (true) {\n m = snippetRe.exec(html);\n if (m === null) break;\n snippets.push(m[1] ?? \"\");\n }\n\n const hrefRe = /href=\"([^\"]+)\"/;\n const innerRe = /<a\\b[^>]*>([\\s\\S]*?)<\\/a>/;\n const results: SearchResult[] = [];\n for (let i = 0; i < titles.length; i++) {\n const anchor = titles[i]!;\n const hrefMatch = anchor.match(hrefRe);\n const innerMatch = anchor.match(innerRe);\n if (!hrefMatch?.[1]) continue;\n results.push({\n title: decodeHtmlEntities(stripHtml(innerMatch?.[1] ?? \"\")).trim(),\n url: hrefMatch[1],\n snippet: decodeHtmlEntities(stripHtml(snippets[i] ?? \"\"))\n .replace(/\\s+/g, \" \")\n .trim(),\n });\n }\n return results;\n}\n\n/**\n * Download a URL, strip HTML down to readable text, return it. Times\n * out at 15s, caps extracted text at 32k chars to fit the tool-result\n * budget.\n */\nexport async function webFetch(url: string, opts: WebFetchOptions = {}): Promise<PageContent> {\n const maxChars = opts.maxChars ?? DEFAULT_FETCH_MAX_CHARS;\n const timeoutMs = opts.timeoutMs ?? DEFAULT_FETCH_TIMEOUT_MS;\n const ctl = new AbortController();\n const timer = setTimeout(() => ctl.abort(), timeoutMs);\n // Forward the caller's abort too so an Esc during a long fetch is respected.\n const cancel = () => ctl.abort();\n opts.signal?.addEventListener(\"abort\", cancel, { once: true });\n let resp: Response;\n try {\n resp = await fetch(url, {\n headers: { \"User-Agent\": USER_AGENT, Accept: \"text/html,text/plain,*/*\" },\n signal: ctl.signal,\n redirect: \"follow\",\n });\n } finally {\n clearTimeout(timer);\n opts.signal?.removeEventListener(\"abort\", cancel);\n }\n if (!resp.ok) throw new Error(`web_fetch ${resp.status} for ${url}`);\n const contentType = resp.headers.get(\"content-type\") ?? \"\";\n const raw = await resp.text();\n const title = extractTitle(raw);\n const text = contentType.includes(\"text/html\") ? htmlToText(raw) : raw;\n const truncated = text.length > maxChars;\n const finalText = truncated\n ? `${text.slice(0, maxChars)}\\n\\n[… truncated ${text.length - maxChars} chars …]`\n : text;\n return { url, title, text: finalText, truncated };\n}\n\n/**\n * Strip HTML to readable text. Removes scripts/styles/nav/footer/aside\n * blocks first, then tags, then collapses whitespace. Not a Readability\n * clone — purpose-built to keep the extracted text small enough for the\n * tool-result budget while preserving paragraph breaks.\n */\nexport function htmlToText(html: string): string {\n let s = html;\n s = s.replace(/<script[\\s\\S]*?<\\/script>/gi, \"\");\n s = s.replace(/<style[\\s\\S]*?<\\/style>/gi, \"\");\n s = s.replace(/<noscript[\\s\\S]*?<\\/noscript>/gi, \"\");\n s = s.replace(/<nav[\\s\\S]*?<\\/nav>/gi, \"\");\n s = s.replace(/<footer[\\s\\S]*?<\\/footer>/gi, \"\");\n s = s.replace(/<aside[\\s\\S]*?<\\/aside>/gi, \"\");\n s = s.replace(/<svg[\\s\\S]*?<\\/svg>/gi, \"\");\n // Preserve paragraph breaks by turning common block tags into newlines.\n s = s.replace(/<\\/?(p|div|br|h[1-6]|li|tr|section|article)\\b[^>]*>/gi, \"\\n\");\n s = s.replace(/<[^>]+>/g, \"\");\n s = decodeHtmlEntities(s);\n s = s.replace(/[ \\t]+/g, \" \");\n s = s.replace(/\\n[ \\t]+/g, \"\\n\");\n s = s.replace(/\\n{3,}/g, \"\\n\\n\");\n return s.trim();\n}\n\nfunction stripHtml(s: string): string {\n return s.replace(/<[^>]+>/g, \"\");\n}\n\nfunction decodeHtmlEntities(s: string): string {\n return s\n .replace(/ /g, \" \")\n .replace(/&/g, \"&\")\n .replace(/</g, \"<\")\n .replace(/>/g, \">\")\n .replace(/"/g, '\"')\n .replace(/'/g, \"'\");\n}\n\nfunction extractTitle(html: string): string | undefined {\n const m = html.match(/<title[^>]*>([\\s\\S]*?)<\\/title>/i);\n if (!m?.[1]) return undefined;\n return m[1].replace(/\\s+/g, \" \").trim() || undefined;\n}\n\nexport interface WebToolsOptions {\n /** Default top-K for `web_search` when the model doesn't specify. */\n defaultTopK?: number;\n /** Byte cap for `web_fetch` extracted text. */\n maxFetchChars?: number;\n}\n\n/**\n * Register `web_search` + `web_fetch` on a ToolRegistry. The model\n * invokes them automatically when a question needs current info —\n * no slash command from the user is required.\n */\nexport function registerWebTools(registry: ToolRegistry, opts: WebToolsOptions = {}): ToolRegistry {\n const defaultTopK = opts.defaultTopK ?? DEFAULT_TOPK;\n const maxFetchChars = opts.maxFetchChars ?? DEFAULT_FETCH_MAX_CHARS;\n\n registry.register({\n name: \"web_search\",\n description:\n \"Search the public web. Returns ranked results with title, url, and snippet. Use this when the question needs information more current than your training data, when you're unsure of a factual detail, or when the user asks about a specific webpage/library/release you haven't seen.\",\n readOnly: true,\n parameters: {\n type: \"object\",\n properties: {\n query: { type: \"string\", description: \"Natural-language search query.\" },\n topK: {\n type: \"integer\",\n description: `Number of results to return (1..10). Default ${defaultTopK}.`,\n },\n },\n required: [\"query\"],\n },\n fn: async (args: { query: string; topK?: number }, ctx) => {\n const results = await webSearch(args.query, {\n topK: args.topK ?? defaultTopK,\n signal: ctx?.signal,\n });\n return formatSearchResults(args.query, results);\n },\n });\n\n registry.register({\n name: \"web_fetch\",\n description:\n \"Download a URL and return its visible text content (HTML pages get scripts/styles/nav stripped). Truncated at the tool-result cap. Use after web_search when a snippet isn't enough.\",\n readOnly: true,\n parameters: {\n type: \"object\",\n properties: {\n url: { type: \"string\", description: \"Absolute http:// or https:// URL.\" },\n },\n required: [\"url\"],\n },\n fn: async (args: { url: string }, ctx) => {\n if (!/^https?:\\/\\//i.test(args.url)) {\n throw new Error(\"web_fetch: url must start with http:// or https://\");\n }\n const page = await webFetch(args.url, { maxChars: maxFetchChars, signal: ctx?.signal });\n const header = page.title ? `${page.title}\\n${page.url}` : page.url;\n return `${header}\\n\\n${page.text}`;\n },\n });\n\n return registry;\n}\n\nexport function formatSearchResults(query: string, results: SearchResult[]): string {\n const lines: string[] = [`query: ${query}`, `\\nresults (${results.length}):`];\n results.forEach((r, i) => {\n lines.push(`\\n${i + 1}. ${r.title}`);\n lines.push(` ${r.url}`);\n if (r.snippet) lines.push(` ${r.snippet}`);\n });\n return lines.join(\"\\n\");\n}\n","import { readFileSync } from \"node:fs\";\nimport { resolve } from \"node:path\";\n\n/**\n * Minimal `.env` loader; no dependency on dotenv.\n *\n * Reads KEY=VALUE lines and populates `process.env` for keys not already set.\n * Silently no-ops if the file is missing. Safe to call from library entry\n * points, CLI commands, examples, and benchmark runners.\n */\nexport function loadDotenv(path = \".env\"): void {\n let raw: string;\n try {\n raw = readFileSync(resolve(process.cwd(), path), \"utf8\");\n } catch {\n return;\n }\n for (const line of raw.split(/\\r?\\n/)) {\n const trimmed = line.trim();\n if (!trimmed || trimmed.startsWith(\"#\")) continue;\n const eq = trimmed.indexOf(\"=\");\n if (eq === -1) continue;\n const key = trimmed.slice(0, eq).trim();\n let value = trimmed.slice(eq + 1).trim();\n if (\n (value.startsWith('\"') && value.endsWith('\"')) ||\n (value.startsWith(\"'\") && value.endsWith(\"'\"))\n ) {\n value = value.slice(1, -1);\n }\n if (process.env[key] === undefined) process.env[key] = value;\n }\n}\n","/**\n * Transcript format — the canonical \"audit log\" of a Reasonix session.\n *\n * Design split:\n * - Session file (`~/.reasonix/sessions/<name>.jsonl`) stores only the\n * `ChatMessage`s the model needs to resume. See session.ts.\n * - Transcript file (this module) stores every LoopEvent with usage, cost,\n * model, and prefix fingerprint attached where available — enough for\n * replay and diff to reconstruct economics.\n *\n * The two are different contracts: sessions are the user's *memory*;\n * transcripts are the *receipts*. Don't conflate them.\n *\n * Backward compatibility: all fields beyond {ts, turn, role, content} are\n * optional on read. A v0.1 transcript (pre-usage) still parses and renders\n * — it just shows cost/cache as n/a.\n */\n\nimport { type WriteStream, createWriteStream, readFileSync } from \"node:fs\";\nimport type { TypedPlanState } from \"./harvest.js\";\nimport type { LoopEvent } from \"./loop.js\";\nimport type { RawUsage } from \"./types.js\";\n\nexport interface TranscriptRecord {\n /** ISO-8601 timestamp at emit time. */\n ts: string;\n /** 1-based turn number within the session. */\n turn: number;\n /** LoopEvent role — \"assistant_delta\" | \"assistant_final\" | \"tool\" | \"done\" | ... */\n role: string;\n /** For assistant events, the final (or delta) text; for tool events, the tool result. */\n content: string;\n /** Tool name (role === \"tool\"). */\n tool?: string;\n /** JSON-string args the model sent for a tool call (role === \"tool\"). Persisted so diff can explain *why* two runs made different calls. */\n args?: string;\n /** DeepSeek token-usage snapshot (role === \"assistant_final\"). */\n usage?: RawUsage;\n /** USD cost of this turn (role === \"assistant_final\"). */\n cost?: number;\n /** Model id that produced this turn. */\n model?: string;\n /**\n * The ImmutablePrefix fingerprint at this turn. Lets diff prove two runs\n * share a prefix — i.e. any cache-hit delta is attributable to log\n * stability, not to a different system prompt.\n */\n prefixHash?: string;\n /**\n * Structured plan state extracted by the Pillar 2 harvester. Present on\n * assistant_final records when harvest was enabled and produced non-empty\n * state. Omitted entirely when harvest is off or produced nothing —\n * absence means \"no data\", not \"empty plan\".\n */\n planState?: TypedPlanState;\n /** Optional error message (role === \"error\"). */\n error?: string;\n}\n\nexport interface TranscriptMeta {\n /**\n * Optional metadata written as the first line of a transcript. Lets\n * downstream tooling know what it's reading without guessing.\n * Recognized by a special role \"_meta\".\n */\n version: 1;\n source: string; // e.g. \"reasonix chat\", \"bench/baseline\", \"bench/reasonix\"\n model?: string;\n task?: string;\n mode?: string;\n repeat?: number;\n startedAt: string;\n}\n\ninterface MetaLine {\n role: \"_meta\";\n meta: TranscriptMeta;\n}\n\nexport interface ReadTranscriptResult {\n meta: TranscriptMeta | null;\n records: TranscriptRecord[];\n}\n\n/**\n * Build a TranscriptRecord from a LoopEvent. Extra fields (model,\n * prefixHash) that the LoopEvent doesn't carry are passed in separately\n * because they're session-level, not event-level.\n */\nexport function recordFromLoopEvent(\n ev: LoopEvent,\n extra: { model: string; prefixHash: string },\n): TranscriptRecord {\n const rec: TranscriptRecord = {\n ts: new Date().toISOString(),\n turn: ev.turn,\n role: ev.role,\n content: ev.content,\n };\n if (ev.toolName !== undefined) rec.tool = ev.toolName;\n if (ev.toolArgs !== undefined) rec.args = ev.toolArgs;\n if (ev.error !== undefined) rec.error = ev.error;\n // Only persist non-empty plan state — empty harvest output is indistinguishable\n // from \"harvest was off\" for replay purposes, and saves transcript bytes.\n if (ev.planState && !isPlanStateEmptyShape(ev.planState)) {\n rec.planState = {\n subgoals: [...ev.planState.subgoals],\n hypotheses: [...ev.planState.hypotheses],\n uncertainties: [...ev.planState.uncertainties],\n rejectedPaths: [...ev.planState.rejectedPaths],\n };\n }\n if (ev.stats) {\n rec.usage = {\n prompt_tokens: ev.stats.usage.promptTokens,\n completion_tokens: ev.stats.usage.completionTokens,\n total_tokens: ev.stats.usage.totalTokens,\n prompt_cache_hit_tokens: ev.stats.usage.promptCacheHitTokens,\n prompt_cache_miss_tokens: ev.stats.usage.promptCacheMissTokens,\n };\n rec.cost = ev.stats.cost;\n rec.model = ev.stats.model;\n rec.prefixHash = extra.prefixHash;\n } else if (ev.role === \"assistant_final\") {\n // assistant_final without stats (shouldn't happen in the live loop but\n // might in test fixtures) — still persist model + prefix for continuity.\n rec.model = extra.model;\n rec.prefixHash = extra.prefixHash;\n }\n return rec;\n}\n\n/**\n * Append a record to an open write stream. Caller owns the stream lifecycle.\n */\nexport function writeRecord(stream: WriteStream, record: TranscriptRecord): void {\n stream.write(`${JSON.stringify(record)}\\n`);\n}\n\n/**\n * Write a _meta line to an open write stream. Call exactly once, at the top.\n */\nexport function writeMeta(stream: WriteStream, meta: TranscriptMeta): void {\n const line: MetaLine = { role: \"_meta\", meta };\n stream.write(`${JSON.stringify(line)}\\n`);\n}\n\n/**\n * Convenience: open a stream, write meta, return stream.\n */\nexport function openTranscriptFile(path: string, meta: TranscriptMeta): WriteStream {\n const stream = createWriteStream(path, { flags: \"a\" });\n writeMeta(stream, meta);\n return stream;\n}\n\n/**\n * Parse a transcript file. Returns meta (if the first line is a _meta record)\n * and the full record list.\n *\n * Robustness contract:\n * - Empty lines are skipped.\n * - Malformed JSON lines are skipped silently (do not crash on partial\n * files — live chats may be mid-write).\n * - Records missing optional fields still parse — they're just rendered\n * with n/a where the optional value would go.\n */\nexport function readTranscript(path: string): ReadTranscriptResult {\n const raw = readFileSync(path, \"utf8\");\n return parseTranscript(raw);\n}\n\nfunction isPlanStateEmptyShape(s: TypedPlanState): boolean {\n return (\n s.subgoals.length === 0 &&\n s.hypotheses.length === 0 &&\n s.uncertainties.length === 0 &&\n s.rejectedPaths.length === 0\n );\n}\n\nexport function parseTranscript(raw: string): ReadTranscriptResult {\n const out: ReadTranscriptResult = { meta: null, records: [] };\n for (const line of raw.split(/\\r?\\n/)) {\n const trimmed = line.trim();\n if (!trimmed) continue;\n let obj: unknown;\n try {\n obj = JSON.parse(trimmed);\n } catch {\n continue;\n }\n if (!obj || typeof obj !== \"object\") continue;\n const rec = obj as Record<string, unknown>;\n if (rec.role === \"_meta\" && rec.meta && typeof rec.meta === \"object\") {\n out.meta = rec.meta as TranscriptMeta;\n continue;\n }\n if (\n typeof rec.ts === \"string\" &&\n typeof rec.turn === \"number\" &&\n typeof rec.role === \"string\" &&\n typeof rec.content === \"string\"\n ) {\n out.records.push(rec as unknown as TranscriptRecord);\n }\n }\n return out;\n}\n","/**\n * Replay — reconstruct session economics from a transcript file.\n *\n * Given a transcript written by App.tsx or the bench runner, rebuild a\n * SessionSummary-compatible aggregate (turn count, total cost, cache-hit\n * ratio, vs-Claude estimate) without replaying the LLM calls.\n *\n * The whole point is offline auditing: a reader should be able to reproduce\n * the headline numbers from a transcript alone, without an API key.\n */\n\nimport { Usage } from \"./client.js\";\nimport {\n type SessionSummary,\n type TurnStats,\n claudeEquivalentCost,\n costUsd,\n inputCostUsd,\n outputCostUsd,\n} from \"./telemetry.js\";\nimport { type ReadTranscriptResult, type TranscriptRecord, readTranscript } from \"./transcript.js\";\n\n/**\n * A single turn's worth of records — the unit of navigation in replay TUI.\n * Records are grouped by their `turn` field, preserving file order within\n * each group (so tool events interleave with assistant_final events the\n * way they were actually emitted).\n */\nexport interface TurnPage {\n turn: number;\n records: TranscriptRecord[];\n}\n\n/**\n * Group transcript records into turn-pages. Pages are returned in ascending\n * turn order. Records without a numeric turn (meta lines, malformed) are\n * already filtered by the transcript reader, so this sees clean input.\n */\nexport function groupRecordsByTurn(records: TranscriptRecord[]): TurnPage[] {\n const byTurn = new Map<number, TranscriptRecord[]>();\n for (const rec of records) {\n const list = byTurn.get(rec.turn);\n if (list) list.push(rec);\n else byTurn.set(rec.turn, [rec]);\n }\n return [...byTurn.entries()]\n .sort(([a], [b]) => a - b)\n .map(([turn, records]) => ({ turn, records }));\n}\n\n/**\n * Cumulative replay stats up to and including pages[0..upToIdx]. Returns\n * empty stats if upToIdx < 0. Used by replay TUI's sidebar to show \"stats\n * so far\" as the user scrolls through a transcript.\n */\nexport function computeCumulativeStats(pages: TurnPage[], upToIdx: number): ReplayStats {\n if (upToIdx < 0) return computeReplayStats([]);\n const flat: TranscriptRecord[] = [];\n for (let i = 0; i <= upToIdx && i < pages.length; i++) {\n const records = pages[i]?.records;\n if (records) flat.push(...records);\n }\n return computeReplayStats(flat);\n}\n\nexport interface ReplayStats extends SessionSummary {\n /** Per-turn stats, in turn order. Only assistant_final records contribute. */\n perTurn: TurnStats[];\n /** Unique models that appeared in the transcript's assistant_final records. */\n models: string[];\n /** Unique prefix hashes that appeared. Length > 1 means the prefix churned (cache-hostile). */\n prefixHashes: string[];\n /** Count of user-role records (user turns issued). */\n userTurns: number;\n /** Count of tool-role records (tool calls executed). */\n toolCalls: number;\n /** Count of assistant_final records that carry a non-empty planState (harvest signal). */\n harvestedTurns: number;\n /** Sum of uncertainties across all harvested turns — a proxy for \"how much did R1 hedge?\" */\n totalUncertainties: number;\n /** Sum of subgoals across all harvested turns. */\n totalSubgoals: number;\n}\n\n/**\n * Parse a transcript file and compute replay stats. Throws only on I/O\n * errors; malformed lines inside the file are skipped silently.\n */\nexport function replayFromFile(path: string): { parsed: ReadTranscriptResult; stats: ReplayStats } {\n const parsed = readTranscript(path);\n return { parsed, stats: computeReplayStats(parsed.records) };\n}\n\nexport function computeReplayStats(records: TranscriptRecord[]): ReplayStats {\n const turns: TurnStats[] = [];\n const models = new Set<string>();\n const prefixHashes = new Set<string>();\n let userTurns = 0;\n let toolCalls = 0;\n let harvestedTurns = 0;\n let totalUncertainties = 0;\n let totalSubgoals = 0;\n\n for (const rec of records) {\n if (rec.role === \"user\") userTurns++;\n else if (rec.role === \"tool\") toolCalls++;\n else if (rec.role === \"assistant_final\") {\n if (rec.model) models.add(rec.model);\n if (rec.prefixHash) prefixHashes.add(rec.prefixHash);\n if (rec.planState) {\n harvestedTurns++;\n totalUncertainties += rec.planState.uncertainties.length;\n totalSubgoals += rec.planState.subgoals.length;\n }\n if (rec.usage && rec.model) {\n const u = new Usage(\n rec.usage.prompt_tokens ?? 0,\n rec.usage.completion_tokens ?? 0,\n rec.usage.total_tokens ?? 0,\n rec.usage.prompt_cache_hit_tokens ?? 0,\n rec.usage.prompt_cache_miss_tokens ?? 0,\n );\n turns.push({\n turn: rec.turn,\n model: rec.model,\n usage: u,\n // `rec.cost` wins when present — honors whatever the writer computed\n // even if pricing tables have since changed. Only recompute when\n // the transcript didn't record it (old format).\n cost: rec.cost ?? costUsd(rec.model, u),\n cacheHitRatio: u.cacheHitRatio,\n });\n }\n }\n }\n\n return {\n perTurn: turns,\n models: [...models],\n prefixHashes: [...prefixHashes],\n userTurns,\n toolCalls,\n harvestedTurns,\n totalUncertainties,\n totalSubgoals,\n ...summarizeTurns(turns),\n };\n}\n\nfunction summarizeTurns(turns: TurnStats[]): SessionSummary {\n const totalCost = turns.reduce((s, t) => s + t.cost, 0);\n const totalInput = turns.reduce((s, t) => s + inputCostUsd(t.model, t.usage), 0);\n const totalOutput = turns.reduce((s, t) => s + outputCostUsd(t.model, t.usage), 0);\n const totalClaude = turns.reduce((s, t) => s + claudeEquivalentCost(t.usage), 0);\n let hit = 0;\n let miss = 0;\n for (const t of turns) {\n hit += t.usage.promptCacheHitTokens;\n miss += t.usage.promptCacheMissTokens;\n }\n const cacheHitRatio = hit + miss > 0 ? hit / (hit + miss) : 0;\n const savingsVsClaude = totalClaude > 0 ? 1 - totalCost / totalClaude : 0;\n const lastTurn = turns[turns.length - 1];\n return {\n turns: turns.length,\n totalCostUsd: round(totalCost, 6),\n totalInputCostUsd: round(totalInput, 6),\n totalOutputCostUsd: round(totalOutput, 6),\n claudeEquivalentUsd: round(totalClaude, 6),\n savingsVsClaudePct: round(savingsVsClaude * 100, 2),\n cacheHitRatio: round(cacheHitRatio, 4),\n lastPromptTokens: lastTurn?.usage.promptTokens ?? 0,\n };\n}\n\nfunction round(n: number, digits: number): number {\n const f = 10 ** digits;\n return Math.round(n * f) / f;\n}\n","/**\n * Diff — compare two transcripts and produce a summary + divergence report.\n *\n * Two transcripts are \"comparable\" when they stem from the same task (or\n * the same user prompt). Alignment is by turn number: assistant_final #N\n * in A pairs with assistant_final #N in B. If one side ran more turns, the\n * extras are labeled \"only in A\" / \"only in B\".\n *\n * What we compute:\n * - Aggregate deltas: turns, tool calls, cache hit, cost, token counts\n * - First divergence: the lowest turn where A and B's tool calls or\n * assistant text differ meaningfully\n * - Prefix-stability story: how many unique prefix hashes each side used\n *\n * Non-goals (deliberately):\n * - LLM-judge quality comparison\n * - Per-token delta rendering — not useful at the fidelity we're at\n * - Embedding similarity — Levenshtein ratio is cheap and good enough\n */\n\nimport { type ReplayStats, computeReplayStats } from \"./replay.js\";\nimport type { ReadTranscriptResult, TranscriptRecord } from \"./transcript.js\";\n\nexport interface DiffSide {\n label: string;\n meta: ReadTranscriptResult[\"meta\"];\n records: TranscriptRecord[];\n stats: ReplayStats;\n}\n\nexport interface TurnPair {\n turn: number;\n aAssistant?: TranscriptRecord;\n bAssistant?: TranscriptRecord;\n aTools: TranscriptRecord[];\n bTools: TranscriptRecord[];\n /**\n * Classification of the pair:\n * \"match\" — both sides present, text & tool calls within threshold\n * \"diverge\" — both sides present, but text or tool calls differ\n * \"only_in_a\" — assistant_final in A but not B\n * \"only_in_b\" — assistant_final in B but not A\n */\n kind: \"match\" | \"diverge\" | \"only_in_a\" | \"only_in_b\";\n /** When kind === \"diverge\", a short one-liner pointing at what differs. */\n divergenceNote?: string;\n}\n\nexport interface DiffReport {\n a: DiffSide;\n b: DiffSide;\n pairs: TurnPair[];\n firstDivergenceTurn: number | null;\n}\n\n// ---------- navigation helpers (used by the Ink diff TUI) ----------\n\n/**\n * Find the next pair (strictly after `fromIdx`) whose kind is not \"match\".\n * Returns -1 when no later divergence exists. Used by DiffApp's `n` key.\n */\nexport function findNextDivergence(pairs: TurnPair[], fromIdx: number): number {\n for (let i = fromIdx + 1; i < pairs.length; i++) {\n if (pairs[i]!.kind !== \"match\") return i;\n }\n return -1;\n}\n\n/**\n * Find the previous pair (strictly before `fromIdx`) whose kind is not\n * \"match\". Returns -1 when no earlier divergence exists. Used by\n * DiffApp's `N` / `p` key.\n */\nexport function findPrevDivergence(pairs: TurnPair[], fromIdx: number): number {\n const start = Math.min(fromIdx - 1, pairs.length - 1);\n for (let i = start; i >= 0; i--) {\n if (pairs[i]!.kind !== \"match\") return i;\n }\n return -1;\n}\n\nexport function diffTranscripts(\n a: { label: string; parsed: ReadTranscriptResult },\n b: { label: string; parsed: ReadTranscriptResult },\n): DiffReport {\n const aSide: DiffSide = {\n label: a.label,\n meta: a.parsed.meta,\n records: a.parsed.records,\n stats: computeReplayStats(a.parsed.records),\n };\n const bSide: DiffSide = {\n label: b.label,\n meta: b.parsed.meta,\n records: b.parsed.records,\n stats: computeReplayStats(b.parsed.records),\n };\n\n const aByTurn = groupByTurn(a.parsed.records);\n const bByTurn = groupByTurn(b.parsed.records);\n const turns = [...new Set([...aByTurn.keys(), ...bByTurn.keys()])].sort((x, y) => x - y);\n\n const pairs: TurnPair[] = [];\n let firstDivergenceTurn: number | null = null;\n for (const turn of turns) {\n const aGroup = aByTurn.get(turn) ?? { assistant: undefined, tools: [] };\n const bGroup = bByTurn.get(turn) ?? { assistant: undefined, tools: [] };\n const aAssistant = aGroup.assistant;\n const bAssistant = bGroup.assistant;\n const aTools = aGroup.tools;\n const bTools = bGroup.tools;\n\n let kind: TurnPair[\"kind\"];\n let divergenceNote: string | undefined;\n if (!aAssistant && bAssistant) kind = \"only_in_b\";\n else if (aAssistant && !bAssistant) kind = \"only_in_a\";\n else if (!aAssistant && !bAssistant)\n kind = \"diverge\"; // tool-only turn (rare)\n else {\n divergenceNote = classifyDivergence(aAssistant!, bAssistant!, aTools, bTools);\n kind = divergenceNote ? \"diverge\" : \"match\";\n }\n\n if (kind !== \"match\" && firstDivergenceTurn === null) firstDivergenceTurn = turn;\n pairs.push({ turn, aAssistant, bAssistant, aTools, bTools, kind, divergenceNote });\n }\n\n return { a: aSide, b: bSide, pairs, firstDivergenceTurn };\n}\n\n// ---------- divergence classification ----------\n\n/**\n * Return a short reason string if two sides meaningfully disagree, or\n * undefined if they're close enough to call a match.\n *\n * Ranking of divergence signals (cheapest first):\n * 1. Different set of tool names → clearest diff\n * 2. Different tool args for the same tool → second-clearest\n * 3. Text similarity below threshold → fuzziest\n */\nfunction classifyDivergence(\n a: TranscriptRecord,\n b: TranscriptRecord,\n aTools: TranscriptRecord[],\n bTools: TranscriptRecord[],\n): string | undefined {\n const aNames = aTools.map((t) => t.tool ?? \"\").sort();\n const bNames = bTools.map((t) => t.tool ?? \"\").sort();\n if (aNames.join(\",\") !== bNames.join(\",\")) {\n return `tool calls differ: A=[${aNames.join(\",\") || \"—\"}] B=[${bNames.join(\",\") || \"—\"}]`;\n }\n // Same tool names — did they pass different args?\n for (let i = 0; i < aTools.length; i++) {\n const at = aTools[i]!;\n const bt = bTools[i]!;\n if (at.tool !== bt.tool) continue;\n if ((at.args ?? \"\") !== (bt.args ?? \"\")) {\n return `\"${at.tool}\" args differ`;\n }\n }\n const simRatio = similarity(a.content, b.content);\n if (simRatio < 0.75) return `text similarity ${(simRatio * 100).toFixed(0)}%`;\n return undefined;\n}\n\n/**\n * Normalized Levenshtein similarity ratio in [0, 1]. 1 = identical.\n * Early-exits for long strings (> 2000 chars) with a cheap token-overlap\n * estimate to keep diff fast on chatty transcripts.\n */\nexport function similarity(a: string, b: string): number {\n if (a === b) return 1;\n if (!a && !b) return 1;\n if (!a || !b) return 0;\n const maxLen = Math.max(a.length, b.length);\n if (maxLen > 2000) return tokenOverlap(a, b);\n const dist = levenshtein(a, b);\n return 1 - dist / maxLen;\n}\n\nfunction tokenOverlap(a: string, b: string): number {\n const ta = new Set(a.toLowerCase().split(/\\s+/).filter(Boolean));\n const tb = new Set(b.toLowerCase().split(/\\s+/).filter(Boolean));\n if (ta.size === 0 && tb.size === 0) return 1;\n let shared = 0;\n for (const t of ta) if (tb.has(t)) shared++;\n return (2 * shared) / (ta.size + tb.size);\n}\n\nfunction levenshtein(a: string, b: string): number {\n const m = a.length;\n const n = b.length;\n if (m === 0) return n;\n if (n === 0) return m;\n let prev = new Array(n + 1);\n let curr = new Array(n + 1);\n for (let j = 0; j <= n; j++) prev[j] = j;\n for (let i = 1; i <= m; i++) {\n curr[0] = i;\n for (let j = 1; j <= n; j++) {\n const cost = a[i - 1] === b[j - 1] ? 0 : 1;\n curr[j] = Math.min(curr[j - 1] + 1, prev[j] + 1, prev[j - 1] + cost);\n }\n [prev, curr] = [curr, prev];\n }\n return prev[n];\n}\n\n// ---------- grouping ----------\n\ninterface TurnGroup {\n assistant?: TranscriptRecord;\n tools: TranscriptRecord[];\n}\n\nfunction groupByTurn(records: TranscriptRecord[]): Map<number, TurnGroup> {\n const out = new Map<number, TurnGroup>();\n for (const rec of records) {\n if (rec.role === \"user\") continue; // user msg is input to the turn, not its output\n const g = out.get(rec.turn) ?? { tools: [] };\n if (rec.role === \"assistant_final\") g.assistant = rec;\n else if (rec.role === \"tool\") g.tools.push(rec);\n out.set(rec.turn, g);\n }\n return out;\n}\n\n// ---------- rendering ----------\n\nexport interface RenderOptions {\n /** Monochrome output (for file redirection or piping). Defaults to true. */\n monochrome?: boolean;\n}\n\nexport function renderSummaryTable(report: DiffReport, _opts: RenderOptions = {}): string {\n const a = report.a;\n const b = report.b;\n const lines: string[] = [];\n lines.push(\"Comparing:\");\n lines.push(` A ${a.label}`);\n lines.push(` B ${b.label}`);\n lines.push(\"\");\n lines.push(row([\"\", \"A\", \"B\", \"Δ\"], [20, 14, 14, 14]));\n lines.push(\n row([\"─\".repeat(20), \"─\".repeat(14), \"─\".repeat(14), \"─\".repeat(14)], [20, 14, 14, 14]),\n );\n lines.push(statRow(\"model calls\", a.stats.turns, b.stats.turns));\n lines.push(statRow(\"user turns\", a.stats.userTurns, b.stats.userTurns));\n lines.push(statRow(\"tool calls\", a.stats.toolCalls, b.stats.toolCalls));\n lines.push(\n row(\n [\n \"cache hit\",\n `${pct(a.stats.cacheHitRatio)}`,\n `${pct(b.stats.cacheHitRatio)}`,\n signPct(b.stats.cacheHitRatio - a.stats.cacheHitRatio),\n ],\n [20, 14, 14, 14],\n ),\n );\n lines.push(\n row(\n [\n \"cost (USD)\",\n `$${a.stats.totalCostUsd.toFixed(6)}`,\n `$${b.stats.totalCostUsd.toFixed(6)}`,\n costDelta(a.stats.totalCostUsd, b.stats.totalCostUsd),\n ],\n [20, 14, 14, 14],\n ),\n );\n lines.push(statRow(\"prefix hashes\", a.stats.prefixHashes.length, b.stats.prefixHashes.length));\n // Harvest signal row — only surface when at least one side carries plan\n // state. Keeps no-harvest diffs visually identical to pre-v0.3 output.\n if (a.stats.harvestedTurns > 0 || b.stats.harvestedTurns > 0) {\n lines.push(\n row(\n [\n \"harvest turns\",\n `${a.stats.harvestedTurns}`,\n `${b.stats.harvestedTurns}`,\n signed(b.stats.harvestedTurns - a.stats.harvestedTurns),\n ],\n [20, 14, 14, 14],\n ),\n );\n lines.push(\n row(\n [\n \" subgoals\",\n `${a.stats.totalSubgoals}`,\n `${b.stats.totalSubgoals}`,\n signed(b.stats.totalSubgoals - a.stats.totalSubgoals),\n ],\n [20, 14, 14, 14],\n ),\n );\n lines.push(\n row(\n [\n \" uncertainties\",\n `${a.stats.totalUncertainties}`,\n `${b.stats.totalUncertainties}`,\n signed(b.stats.totalUncertainties - a.stats.totalUncertainties),\n ],\n [20, 14, 14, 14],\n ),\n );\n }\n lines.push(\"\");\n\n // Prefix stability story — the headline finding when comparing bench modes.\n const aPrefixStable = a.stats.prefixHashes.length <= 1;\n const bPrefixStable = b.stats.prefixHashes.length <= 1;\n if (aPrefixStable !== bPrefixStable) {\n const stable = aPrefixStable ? \"A\" : \"B\";\n const churn = aPrefixStable ? \"B\" : \"A\";\n const churnCount = aPrefixStable ? b.stats.prefixHashes.length : a.stats.prefixHashes.length;\n lines.push(\n `prefix stability: ${stable} stayed byte-stable across ${Math.max(\n a.stats.turns,\n b.stats.turns,\n )} turns; ${churn} churned ${churnCount} distinct prefixes.`,\n );\n lines.push(\"\");\n } else if (a.stats.prefixHashes[0] && a.stats.prefixHashes[0] === b.stats.prefixHashes[0]) {\n lines.push(\n `prefix: A and B share the same prefix hash (${a.stats.prefixHashes[0].slice(0, 12)}…) — cache delta is attributable to log stability, not prompt change.`,\n );\n lines.push(\"\");\n }\n\n if (report.firstDivergenceTurn !== null) {\n const p = report.pairs.find((p) => p.turn === report.firstDivergenceTurn);\n lines.push(\n `first divergence: turn ${report.firstDivergenceTurn} — ${p?.divergenceNote ?? \"?\"}`,\n );\n if (p?.aAssistant) lines.push(` A → ${truncate(p.aAssistant.content, 100)}`);\n if (p?.bAssistant) lines.push(` B → ${truncate(p.bAssistant.content, 100)}`);\n } else {\n lines.push(\"no material divergence detected (texts within similarity threshold).\");\n }\n\n return lines.join(\"\\n\");\n}\n\nexport function renderMarkdown(report: DiffReport): string {\n const a = report.a;\n const b = report.b;\n const out: string[] = [];\n out.push(`# Transcript diff: ${a.label} vs ${b.label}`);\n out.push(\"\");\n if (a.meta || b.meta) {\n out.push(\"## Meta\");\n out.push(\"\");\n out.push(`| | ${a.label} | ${b.label} |`);\n out.push(\"|---|---|---|\");\n out.push(`| source | ${a.meta?.source ?? \"—\"} | ${b.meta?.source ?? \"—\"} |`);\n out.push(`| model | ${a.meta?.model ?? \"—\"} | ${b.meta?.model ?? \"—\"} |`);\n out.push(`| task | ${a.meta?.task ?? \"—\"} | ${b.meta?.task ?? \"—\"} |`);\n out.push(`| startedAt | ${a.meta?.startedAt ?? \"—\"} | ${b.meta?.startedAt ?? \"—\"} |`);\n out.push(\"\");\n }\n\n out.push(\"## Summary\");\n out.push(\"\");\n out.push(`| metric | ${a.label} | ${b.label} | delta |`);\n out.push(\"|---|---:|---:|---:|\");\n out.push(\n `| model calls | ${a.stats.turns} | ${b.stats.turns} | ${signed(b.stats.turns - a.stats.turns)} |`,\n );\n out.push(\n `| user turns | ${a.stats.userTurns} | ${b.stats.userTurns} | ${signed(b.stats.userTurns - a.stats.userTurns)} |`,\n );\n out.push(\n `| tool calls | ${a.stats.toolCalls} | ${b.stats.toolCalls} | ${signed(b.stats.toolCalls - a.stats.toolCalls)} |`,\n );\n out.push(\n `| cache hit | ${pct(a.stats.cacheHitRatio)} | ${pct(b.stats.cacheHitRatio)} | **${signPct(b.stats.cacheHitRatio - a.stats.cacheHitRatio)}** |`,\n );\n out.push(\n `| cost (USD) | $${a.stats.totalCostUsd.toFixed(6)} | $${b.stats.totalCostUsd.toFixed(6)} | ${costDelta(a.stats.totalCostUsd, b.stats.totalCostUsd)} |`,\n );\n out.push(\n `| prefix hashes | ${a.stats.prefixHashes.length} | ${b.stats.prefixHashes.length} | — |`,\n );\n if (a.stats.harvestedTurns > 0 || b.stats.harvestedTurns > 0) {\n out.push(\n `| harvest turns | ${a.stats.harvestedTurns} | ${b.stats.harvestedTurns} | ${signed(b.stats.harvestedTurns - a.stats.harvestedTurns)} |`,\n );\n out.push(\n `| harvest subgoals | ${a.stats.totalSubgoals} | ${b.stats.totalSubgoals} | ${signed(b.stats.totalSubgoals - a.stats.totalSubgoals)} |`,\n );\n out.push(\n `| harvest uncertainties | ${a.stats.totalUncertainties} | ${b.stats.totalUncertainties} | ${signed(b.stats.totalUncertainties - a.stats.totalUncertainties)} |`,\n );\n }\n out.push(\"\");\n\n out.push(\"## Turn-by-turn\");\n out.push(\"\");\n out.push(`| turn | kind | ${a.label} tool calls | ${b.label} tool calls | note |`);\n out.push(\"|---:|:---:|---|---|---|\");\n for (const p of report.pairs) {\n const aTools =\n p.aTools\n .map((t) => t.tool)\n .filter(Boolean)\n .join(\", \") || \"—\";\n const bTools =\n p.bTools\n .map((t) => t.tool)\n .filter(Boolean)\n .join(\", \") || \"—\";\n out.push(`| ${p.turn} | ${p.kind} | ${aTools} | ${bTools} | ${p.divergenceNote ?? \"\"} |`);\n }\n out.push(\"\");\n\n if (report.firstDivergenceTurn !== null) {\n const p = report.pairs.find((x) => x.turn === report.firstDivergenceTurn);\n out.push(`## First divergence (turn ${report.firstDivergenceTurn})`);\n out.push(\"\");\n out.push(p?.divergenceNote ?? \"\");\n out.push(\"\");\n if (p?.aAssistant) {\n out.push(`**${a.label}:**`);\n out.push(\"\");\n out.push(\"```\");\n out.push(p.aAssistant.content);\n out.push(\"```\");\n out.push(\"\");\n }\n if (p?.bAssistant) {\n out.push(`**${b.label}:**`);\n out.push(\"\");\n out.push(\"```\");\n out.push(p.bAssistant.content);\n out.push(\"```\");\n out.push(\"\");\n }\n }\n return out.join(\"\\n\");\n}\n\n// ---------- formatting helpers ----------\n\nfunction row(cols: string[], widths: number[]): string {\n return cols.map((c, i) => padRight(c, widths[i] ?? c.length)).join(\" \");\n}\n\nfunction statRow(label: string, av: number, bv: number): string {\n return row([label, `${av}`, `${bv}`, signed(bv - av)], [20, 14, 14, 14]);\n}\n\nfunction padRight(s: string, w: number): string {\n return s.length >= w ? s : s + \" \".repeat(w - s.length);\n}\n\nfunction signed(n: number): string {\n if (n === 0) return \"0\";\n return `${n > 0 ? \"+\" : \"\"}${n}`;\n}\n\nfunction signPct(diff: number): string {\n if (diff === 0) return \"0pp\";\n const s = (diff * 100).toFixed(1);\n return `${diff > 0 ? \"+\" : \"\"}${s}pp`;\n}\n\nfunction pct(x: number): string {\n return `${(x * 100).toFixed(1)}%`;\n}\n\nfunction costDelta(a: number, b: number): string {\n if (a === 0 && b === 0) return \"—\";\n if (a === 0) return \"new\";\n const pctChange = ((b - a) / a) * 100;\n return `${pctChange > 0 ? \"+\" : \"\"}${pctChange.toFixed(1)}%`;\n}\n\nfunction truncate(s: string, n: number): string {\n return s.length > n ? `${s.slice(0, n)}…` : s;\n}\n","/**\n * MCP (Model Context Protocol) type definitions.\n *\n * Hand-rolled rather than importing @modelcontextprotocol/sdk because:\n * - Reasonix's value-add isn't reimplementing the protocol, but *caching*\n * it. Owning the types lets us tune them for our integration (strip\n * fields we don't use, add the ones we do like Reasonix's prefixHash).\n * - Zero dependencies — consistent with how we wrote the DeepSeek client.\n * - If Anthropic bumps the SDK and introduces a breaking change, we're\n * insulated as long as we keep up with the spec itself.\n *\n * Spec reference: https://spec.modelcontextprotocol.io/ (2024-11-05 draft\n * at time of writing). Reasonix models the subset it consumes: tools\n * list/call, resources list/read, prompts list/get, plus the init\n * handshake. Sampling and progress notifications remain deferred.\n *\n * Transport note: the wire format for stdio MCP is **newline-delimited\n * JSON** (NDJSON), not the LSP-style Content-Length header framing that\n * some readers might expect. One JSON-RPC message per line.\n */\n\n// ---------- JSON-RPC 2.0 base ----------\n\nexport type JsonRpcId = string | number;\n\nexport interface JsonRpcRequest<P = unknown> {\n jsonrpc: \"2.0\";\n id: JsonRpcId;\n method: string;\n params?: P;\n}\n\nexport interface JsonRpcNotification<P = unknown> {\n jsonrpc: \"2.0\";\n method: string;\n params?: P;\n}\n\nexport interface JsonRpcSuccess<R = unknown> {\n jsonrpc: \"2.0\";\n id: JsonRpcId;\n result: R;\n}\n\nexport interface JsonRpcError {\n jsonrpc: \"2.0\";\n id: JsonRpcId | null;\n error: {\n /** JSON-RPC standard codes: -32700 parse, -32600 invalid request, -32601 method not found, -32602 invalid params, -32603 internal. MCP also defines its own range. */\n code: number;\n message: string;\n data?: unknown;\n };\n}\n\nexport type JsonRpcResponse<R = unknown> = JsonRpcSuccess<R> | JsonRpcError;\n\nexport type JsonRpcMessage = JsonRpcRequest | JsonRpcNotification | JsonRpcSuccess | JsonRpcError;\n\n// ---------- MCP initialize ----------\n\nexport interface McpClientInfo {\n name: string;\n version: string;\n}\n\nexport interface McpClientCapabilities {\n /** Empty object advertises support without any optional sub-features. */\n tools?: Record<string, never>;\n /** Advertised when the client can consume `resources/list` + `resources/read`. */\n resources?: Record<string, never>;\n /** Advertised when the client can consume `prompts/list` + `prompts/get`. */\n prompts?: Record<string, never>;\n // sampling would go here — deferred.\n}\n\nexport interface InitializeParams {\n protocolVersion: string;\n capabilities: McpClientCapabilities;\n clientInfo: McpClientInfo;\n}\n\nexport interface InitializeResult {\n protocolVersion: string;\n serverInfo: { name: string; version: string };\n capabilities: {\n tools?: { listChanged?: boolean };\n resources?: unknown;\n prompts?: unknown;\n };\n instructions?: string;\n}\n\n// ---------- MCP tools ----------\n\nexport interface McpToolSchema {\n /** JSON Schema — compatible with Reasonix's tools.ts JSONSchema shape. */\n type?: string;\n properties?: Record<string, unknown>;\n required?: string[];\n [extra: string]: unknown;\n}\n\nexport interface McpTool {\n name: string;\n description?: string;\n /** MCP calls this `inputSchema`. Reasonix's `parameters` field is the same concept. */\n inputSchema: McpToolSchema;\n}\n\nexport interface ListToolsResult {\n tools: McpTool[];\n nextCursor?: string;\n}\n\nexport interface CallToolParams {\n name: string;\n arguments?: Record<string, unknown>;\n /**\n * MCP's `_meta` envelope carries out-of-band protocol metadata.\n * Setting `progressToken` here tells the server \"send me progress\n * notifications back using this token\"; the server must then emit\n * `notifications/progress` frames until the response arrives.\n */\n _meta?: { progressToken?: string | number };\n}\n\n/**\n * Server → client notification emitted during a long-running request\n * that the client subscribed to via `_meta.progressToken`. `progress`\n * and `total` are typically matched units (files scanned, bytes\n * processed, etc.); `total` may be missing when the server can't\n * estimate the upper bound up front.\n */\nexport interface ProgressNotificationParams {\n progressToken: string | number;\n progress: number;\n total?: number;\n message?: string;\n}\n\n/** Values a `ProgressHandler` receives — `progressToken` is already matched away. */\nexport interface McpProgressInfo {\n progress: number;\n total?: number;\n message?: string;\n}\n\nexport type McpProgressHandler = (info: McpProgressInfo) => void;\n\nexport interface McpContentBlockText {\n type: \"text\";\n text: string;\n}\n\nexport interface McpContentBlockImage {\n type: \"image\";\n data: string;\n mimeType: string;\n}\n\n/** MCP result content is an array of typed blocks. Reasonix consumes only text for now — image blocks get stringified with a placeholder. */\nexport type McpContentBlock = McpContentBlockText | McpContentBlockImage;\n\nexport interface CallToolResult {\n content: McpContentBlock[];\n /** True = tool raised an error; the content describes it. */\n isError?: boolean;\n}\n\n// ---------- MCP resources ----------\n\n/**\n * A resource the server can expose — think \"file the model can read.\"\n * The URI is opaque to the client: servers may use `file://`, custom\n * schemes, or bare strings. Reasonix doesn't interpret them.\n */\nexport interface McpResource {\n uri: string;\n name: string;\n description?: string;\n /** Hint for the content type (e.g. \"text/markdown\"). Purely informational. */\n mimeType?: string;\n}\n\nexport interface ListResourcesParams {\n /** Pagination cursor from a previous listResources response. */\n cursor?: string;\n}\n\nexport interface ListResourcesResult {\n resources: McpResource[];\n nextCursor?: string;\n}\n\nexport interface ReadResourceParams {\n uri: string;\n}\n\n/**\n * One resource can return multiple content blobs (e.g. the file + a\n * side-car). `text` is the common case for UTF-8 content; `blob` is\n * base64-encoded bytes for binary content. Servers populate exactly\n * one of the two for each entry.\n */\nexport interface McpResourceContentsText {\n uri: string;\n mimeType?: string;\n text: string;\n}\n\nexport interface McpResourceContentsBlob {\n uri: string;\n mimeType?: string;\n blob: string;\n}\n\nexport type McpResourceContents = McpResourceContentsText | McpResourceContentsBlob;\n\nexport interface ReadResourceResult {\n contents: McpResourceContents[];\n}\n\n// ---------- MCP prompts ----------\n\n/**\n * A parameterizable prompt template the server exposes. Clients fetch\n * it with `prompts/get` and pass the result to the model as-is.\n */\nexport interface McpPromptArgument {\n name: string;\n description?: string;\n required?: boolean;\n}\n\nexport interface McpPrompt {\n name: string;\n description?: string;\n arguments?: McpPromptArgument[];\n}\n\nexport interface ListPromptsParams {\n cursor?: string;\n}\n\nexport interface ListPromptsResult {\n prompts: McpPrompt[];\n nextCursor?: string;\n}\n\nexport interface GetPromptParams {\n name: string;\n arguments?: Record<string, string>;\n}\n\n/**\n * MCP prompt messages are modeled after chat completions: role + content.\n * Content can be a text block OR (per the spec) a resource/image block;\n * Reasonix cares about text in v1, but surfaces the raw array so callers\n * can render other kinds if they need to.\n */\nexport interface McpPromptMessage {\n role: \"user\" | \"assistant\";\n content: McpContentBlock | McpPromptResourceBlock;\n}\n\nexport interface McpPromptResourceBlock {\n type: \"resource\";\n resource: McpResourceContents;\n}\n\nexport interface GetPromptResult {\n description?: string;\n messages: McpPromptMessage[];\n}\n\n// ---------- convenience ----------\n\n/** Current MCP protocol version Reasonix is coded against. */\nexport const MCP_PROTOCOL_VERSION = \"2024-11-05\";\n\n/** Type guard — success vs error response. */\nexport function isJsonRpcError(msg: JsonRpcResponse): msg is JsonRpcError {\n return \"error\" in msg;\n}\n","/**\n * MCP client — request/response correlation, initialize handshake,\n * tools/list, tools/call. Built on top of a McpTransport so the same\n * logic works against a real stdio server or an in-process fake.\n */\n\nimport type { McpTransport } from \"./stdio.js\";\nimport {\n type CallToolParams,\n type CallToolResult,\n type GetPromptParams,\n type GetPromptResult,\n type InitializeParams,\n type InitializeResult,\n type JsonRpcId,\n type JsonRpcMessage,\n type JsonRpcRequest,\n type JsonRpcResponse,\n type ListPromptsParams,\n type ListPromptsResult,\n type ListResourcesParams,\n type ListResourcesResult,\n type ListToolsResult,\n MCP_PROTOCOL_VERSION,\n type McpClientInfo,\n type McpProgressHandler,\n type ProgressNotificationParams,\n type ReadResourceParams,\n type ReadResourceResult,\n isJsonRpcError,\n} from \"./types.js\";\n\nexport interface McpClientOptions {\n transport: McpTransport;\n clientInfo?: McpClientInfo;\n /** Per-request timeout. Default 60s. */\n requestTimeoutMs?: number;\n}\n\ninterface PendingRequest {\n resolve: (value: unknown) => void;\n reject: (err: Error) => void;\n timeout: NodeJS.Timeout;\n}\n\nexport class McpClient {\n private readonly transport: McpTransport;\n private readonly clientInfo: McpClientInfo;\n private readonly requestTimeoutMs: number;\n private readonly pending = new Map<JsonRpcId, PendingRequest>();\n private nextId = 1;\n private readerStarted = false;\n private initialized = false;\n private _serverCapabilities: InitializeResult[\"capabilities\"] = {};\n private _serverInfo: InitializeResult[\"serverInfo\"] = { name: \"\", version: \"\" };\n private _protocolVersion = \"\";\n private _instructions: string | undefined;\n // Progress-token → handler for notifications/progress routing. Tokens\n // are minted per call when the caller supplies an onProgress\n // callback; cleared when the final response lands (or the pending\n // request rejects). No leaks — the `try/finally` in callTool\n // guarantees cleanup even on timeout.\n private readonly progressHandlers = new Map<string | number, McpProgressHandler>();\n private nextProgressToken = 1;\n\n constructor(opts: McpClientOptions) {\n this.transport = opts.transport;\n this.clientInfo = opts.clientInfo ?? { name: \"reasonix\", version: \"0.3.0-dev\" };\n this.requestTimeoutMs = opts.requestTimeoutMs ?? 60_000;\n }\n\n /** Server's advertised capabilities, available after initialize(). */\n get serverCapabilities(): InitializeResult[\"capabilities\"] {\n return this._serverCapabilities;\n }\n\n /** Server's self-reported name + version, available after initialize(). */\n get serverInfo(): InitializeResult[\"serverInfo\"] {\n return this._serverInfo;\n }\n\n /** Protocol version the server agreed to during the handshake. */\n get protocolVersion(): string {\n return this._protocolVersion;\n }\n\n /** Optional free-form instructions the server provides at handshake. */\n get serverInstructions(): string | undefined {\n return this._instructions;\n }\n\n /**\n * Complete the initialize → initialized handshake. Must be called\n * before any other method (otherwise compliant servers reject).\n */\n async initialize(): Promise<InitializeResult> {\n if (this.initialized) throw new Error(\"MCP client already initialized\");\n this.startReaderIfNeeded();\n const result = await this.request<InitializeResult>(\"initialize\", {\n protocolVersion: MCP_PROTOCOL_VERSION,\n // Advertise every method the client can consume so servers know\n // they can send listChanged notifications etc. Sub-feature flags\n // (e.g. `resources.subscribe`) are omitted — we don't implement\n // those yet and the empty object means \"method-level support, no\n // sub-features.\"\n capabilities: { tools: {}, resources: {}, prompts: {} },\n clientInfo: this.clientInfo,\n } satisfies InitializeParams);\n this._serverCapabilities = result.capabilities ?? {};\n this._serverInfo = result.serverInfo ?? { name: \"\", version: \"\" };\n this._protocolVersion = result.protocolVersion ?? \"\";\n this._instructions = result.instructions;\n // Per spec: client sends notifications/initialized after receiving the\n // initialize response. Only then is the connection live for other\n // methods.\n await this.transport.send({\n jsonrpc: \"2.0\",\n method: \"notifications/initialized\",\n });\n this.initialized = true;\n return result;\n }\n\n /** List tools the server exposes. */\n async listTools(): Promise<ListToolsResult> {\n this.assertInitialized();\n return this.request<ListToolsResult>(\"tools/list\", {});\n }\n\n /**\n * Invoke a tool by name. When `onProgress` is supplied, attaches a\n * fresh progress token so the server can send incremental updates\n * via `notifications/progress`; they're routed to the callback until\n * the final response arrives (or the request times out, in which\n * case the handler is simply dropped — no extra notification).\n *\n * When `signal` is supplied, aborting it:\n * 1) fires `notifications/cancelled` to the server (MCP 2024-11-05\n * way of saying \"forget this request, I no longer care\"), and\n * 2) rejects the pending promise immediately with an AbortError,\n * so the caller doesn't have to wait for the subprocess to\n * finish its in-flight file write or network request.\n * The server MAY still emit a late response; we drop it in dispatch\n * since the request id is gone from `pending`.\n */\n async callTool(\n name: string,\n args?: Record<string, unknown>,\n opts: { onProgress?: McpProgressHandler; signal?: AbortSignal } = {},\n ): Promise<CallToolResult> {\n this.assertInitialized();\n const params: CallToolParams = { name, arguments: args ?? {} };\n let token: number | undefined;\n if (opts.onProgress) {\n token = this.nextProgressToken++;\n this.progressHandlers.set(token, opts.onProgress);\n params._meta = { progressToken: token };\n }\n try {\n return await this.request<CallToolResult>(\"tools/call\", params, opts.signal);\n } finally {\n if (token !== undefined) this.progressHandlers.delete(token);\n }\n }\n\n /**\n * List resources the server exposes. Supports a pagination cursor;\n * callers interested in the full set should loop on `nextCursor`.\n * Servers that don't support resources respond with method-not-found\n * (−32601) — we surface that as a thrown Error so callers can gate\n * on the `serverCapabilities.resources` field first.\n */\n async listResources(cursor?: string): Promise<ListResourcesResult> {\n this.assertInitialized();\n return this.request<ListResourcesResult>(\"resources/list\", {\n ...(cursor ? { cursor } : {}),\n } satisfies ListResourcesParams);\n }\n\n /** Read the contents of a resource by URI. */\n async readResource(uri: string): Promise<ReadResourceResult> {\n this.assertInitialized();\n return this.request<ReadResourceResult>(\"resources/read\", {\n uri,\n } satisfies ReadResourceParams);\n }\n\n /** List prompt templates the server exposes. */\n async listPrompts(cursor?: string): Promise<ListPromptsResult> {\n this.assertInitialized();\n return this.request<ListPromptsResult>(\"prompts/list\", {\n ...(cursor ? { cursor } : {}),\n } satisfies ListPromptsParams);\n }\n\n /**\n * Fetch a rendered prompt by name. `args` supplies values for any\n * required template arguments; the server validates. Returns messages\n * ready to prepend to the model's input.\n */\n async getPrompt(name: string, args?: Record<string, string>): Promise<GetPromptResult> {\n this.assertInitialized();\n return this.request<GetPromptResult>(\"prompts/get\", {\n name,\n ...(args ? { arguments: args } : {}),\n } satisfies GetPromptParams);\n }\n\n /** Close the transport and reject any outstanding requests. */\n async close(): Promise<void> {\n for (const [, pending] of this.pending) {\n clearTimeout(pending.timeout);\n pending.reject(new Error(\"MCP client closed\"));\n }\n this.pending.clear();\n await this.transport.close();\n }\n\n // ---------- internals ----------\n\n private assertInitialized(): void {\n if (!this.initialized) throw new Error(\"MCP client not initialized — call initialize() first\");\n }\n\n private async request<R>(method: string, params: unknown, signal?: AbortSignal): Promise<R> {\n const id = this.nextId++;\n const frame: JsonRpcRequest = { jsonrpc: \"2.0\", id, method, params };\n let abortHandler: (() => void) | null = null;\n const promise = new Promise<R>((resolve, reject) => {\n const timeout = setTimeout(() => {\n this.pending.delete(id);\n if (abortHandler && signal) signal.removeEventListener(\"abort\", abortHandler);\n reject(\n new Error(`MCP request ${method} (id=${id}) timed out after ${this.requestTimeoutMs}ms`),\n );\n }, this.requestTimeoutMs);\n this.pending.set(id, {\n resolve: resolve as (value: unknown) => void,\n reject,\n timeout,\n });\n // Wire up cancellation: when signal fires, send an MCP cancellation\n // notification to the server (so it can stop whatever it was doing)\n // and reject the caller immediately — no need to wait for the\n // subprocess to finish its in-flight work. Late responses from the\n // server are dropped by `dispatch` because the id is gone from\n // `pending`.\n if (signal) {\n if (signal.aborted) {\n this.pending.delete(id);\n clearTimeout(timeout);\n reject(new Error(`MCP request ${method} (id=${id}) aborted before send`));\n return;\n }\n abortHandler = () => {\n this.pending.delete(id);\n clearTimeout(timeout);\n void this.transport\n .send({\n jsonrpc: \"2.0\",\n method: \"notifications/cancelled\",\n params: { requestId: id, reason: \"aborted by user\" },\n })\n .catch(() => {\n // Transport may already be closing — swallow; we still\n // reject the caller below so they unblock.\n });\n reject(new Error(`MCP request ${method} (id=${id}) aborted by user`));\n };\n signal.addEventListener(\"abort\", abortHandler, { once: true });\n }\n });\n try {\n await this.transport.send(frame);\n } catch (err) {\n this.pending.delete(id);\n if (abortHandler && signal) signal.removeEventListener(\"abort\", abortHandler);\n throw err;\n }\n try {\n return await promise;\n } finally {\n if (abortHandler && signal) signal.removeEventListener(\"abort\", abortHandler);\n }\n }\n\n private startReaderIfNeeded(): void {\n if (this.readerStarted) return;\n this.readerStarted = true;\n // Fire-and-forget: the reader runs for the lifetime of the client.\n void this.readLoop();\n }\n\n private async readLoop(): Promise<void> {\n try {\n for await (const msg of this.transport.messages()) {\n this.dispatch(msg);\n }\n } catch (err) {\n // Surface as rejections on all pending requests so nobody hangs.\n for (const [, pending] of this.pending) {\n clearTimeout(pending.timeout);\n pending.reject(err as Error);\n }\n this.pending.clear();\n }\n }\n\n private dispatch(msg: JsonRpcMessage): void {\n // Notifications (no `id`): route by method. Progress notifications\n // go to the per-call handler if one was registered; everything\n // else is dropped silently (we don't yet handle tools/list_changed\n // or resources/list_changed).\n if (!(\"id\" in msg) || msg.id === null || msg.id === undefined) {\n if (\"method\" in msg && msg.method === \"notifications/progress\") {\n const p = msg.params as ProgressNotificationParams | undefined;\n if (!p || p.progressToken === undefined) return;\n const handler = this.progressHandlers.get(p.progressToken);\n if (!handler) return; // late notification after the call resolved\n handler({ progress: p.progress, total: p.total, message: p.message });\n }\n return;\n }\n if (!(\"result\" in msg) && !(\"error\" in msg)) return; // it's a request from server\n const pending = this.pending.get(msg.id);\n if (!pending) return; // late response after timeout; drop\n this.pending.delete(msg.id);\n clearTimeout(pending.timeout);\n const resp = msg as JsonRpcResponse;\n if (isJsonRpcError(resp)) {\n pending.reject(new Error(`MCP ${resp.error.code}: ${resp.error.message}`));\n } else {\n pending.resolve(resp.result);\n }\n }\n}\n","/**\n * Stdio transport for MCP.\n *\n * MCP's stdio wire format is **newline-delimited JSON** (one JSON-RPC\n * message per line). We spawn the server as a child process, write\n * frames to its stdin, parse its stdout line-by-line as they arrive.\n *\n * Transport is abstracted behind an interface so unit tests can fake it\n * with an in-process duplex pair — spawning real servers in unit tests\n * is flaky and slow.\n */\n\nimport { type ChildProcess, spawn } from \"node:child_process\";\nimport type { JsonRpcMessage } from \"./types.js\";\n\n/**\n * A transport sends JSON-RPC messages upstream and surfaces messages\n * arriving downstream via an async iterator. One instance per server\n * connection.\n */\nexport interface McpTransport {\n /** Send one JSON-RPC message. Resolves when the bytes are accepted. */\n send(message: JsonRpcMessage): Promise<void>;\n /** Async iterator over incoming messages. Ends when the connection closes. */\n messages(): AsyncIterableIterator<JsonRpcMessage>;\n /** Close the underlying resource (kill child process, close streams). */\n close(): Promise<void>;\n}\n\nexport interface StdioTransportOptions {\n /** Argv to spawn. First element is the command. */\n command: string;\n args?: string[];\n /** Env overlay — merged over process.env unless replaceEnv=true. */\n env?: Record<string, string>;\n /** When true, only the env above is visible to the child. Default false. */\n replaceEnv?: boolean;\n /** CWD for the child. Default: process.cwd(). */\n cwd?: string;\n /**\n * Spawn through a shell. Default: true on win32 (needed to resolve\n * `.cmd` wrappers like `npx.cmd`, `pnpm.cmd`), false elsewhere.\n * Explicitly pass `false` to opt out on Windows; pass `true` to force\n * it on POSIX (rarely needed).\n */\n shell?: boolean;\n}\n\n/**\n * Spawn `command args...` as a child process and use its stdin/stdout as\n * an MCP transport. Stderr is forwarded to the parent's stderr so server\n * diagnostics are still visible.\n */\nexport class StdioTransport implements McpTransport {\n private readonly child: ChildProcess;\n private readonly queue: JsonRpcMessage[] = [];\n private readonly waiters: Array<(m: JsonRpcMessage | null) => void> = [];\n private closed = false;\n private stdoutBuffer = \"\";\n\n constructor(opts: StdioTransportOptions) {\n const env = opts.replaceEnv ? { ...(opts.env ?? {}) } : { ...process.env, ...(opts.env ?? {}) };\n // Windows wraps binaries as .cmd/.bat shims (npx.cmd, pnpm.cmd, …).\n // child_process.spawn without shell:true can't resolve them, which\n // breaks `--mcp \"npx -y some-server\"` — the most common MCP setup.\n // Default shell:true on win32 and leave POSIX alone.\n const shell = opts.shell ?? process.platform === \"win32\";\n\n if (shell) {\n // Node's shell:true + args[] triggers DEP0190 because it concatenates\n // with spaces and doesn't quote args — unsafe if an arg contains\n // shell metacharacters. We build a single command line ourselves,\n // quoting ONLY the args (command stays bare so the shell's PATH /\n // PATHEXT lookup finds `npx` → `npx.cmd` on Windows).\n const line = [\n opts.command,\n ...(opts.args ?? []).map((a) => quoteArg(a, process.platform === \"win32\")),\n ].join(\" \");\n this.child = spawn(line, [], {\n env,\n cwd: opts.cwd,\n stdio: [\"pipe\", \"pipe\", \"inherit\"],\n shell: true,\n });\n } else {\n this.child = spawn(opts.command, opts.args ?? [], {\n env,\n cwd: opts.cwd,\n stdio: [\"pipe\", \"pipe\", \"inherit\"],\n });\n }\n this.child.stdout!.setEncoding(\"utf8\");\n this.child.stdout!.on(\"data\", (chunk: string) => this.onStdout(chunk));\n this.child.on(\"close\", () => this.onClose());\n this.child.on(\"error\", (err) => {\n // Surface spawn errors as a synthetic JsonRpcError so callers don't\n // hang on a stream that never emits anything.\n this.push({\n jsonrpc: \"2.0\",\n id: null,\n error: { code: -32000, message: `transport error: ${err.message}` },\n });\n });\n }\n\n async send(message: JsonRpcMessage): Promise<void> {\n if (this.closed) throw new Error(\"MCP transport is closed\");\n return new Promise((resolve, reject) => {\n const line = `${JSON.stringify(message)}\\n`;\n this.child.stdin!.write(line, \"utf8\", (err) => {\n if (err) reject(err);\n else resolve();\n });\n });\n }\n\n async *messages(): AsyncIterableIterator<JsonRpcMessage> {\n while (true) {\n if (this.queue.length > 0) {\n yield this.queue.shift()!;\n continue;\n }\n if (this.closed) return;\n const next = await new Promise<JsonRpcMessage | null>((resolve) => {\n this.waiters.push(resolve);\n });\n if (next === null) return; // closed while we were waiting\n yield next;\n }\n }\n\n async close(): Promise<void> {\n if (this.closed) return;\n this.closed = true;\n // Signal any pending waiters.\n while (this.waiters.length > 0) this.waiters.shift()!(null);\n try {\n this.child.stdin!.end();\n } catch {\n /* already ended */\n }\n if (this.child.exitCode === null && !this.child.killed) {\n this.child.kill(\"SIGTERM\");\n }\n }\n\n /** Parse incoming stdout chunks into NDJSON messages. */\n private onStdout(chunk: string): void {\n this.stdoutBuffer += chunk;\n let newlineIdx: number;\n // biome-ignore lint/suspicious/noAssignInExpressions: idiomatic loop shape\n while ((newlineIdx = this.stdoutBuffer.indexOf(\"\\n\")) !== -1) {\n const line = this.stdoutBuffer.slice(0, newlineIdx).trim();\n this.stdoutBuffer = this.stdoutBuffer.slice(newlineIdx + 1);\n if (!line) continue;\n try {\n const msg = JSON.parse(line) as JsonRpcMessage;\n this.push(msg);\n } catch {\n // Malformed lines are dropped — some servers emit startup banners\n // before the JSON-RPC loop begins. We surface the noise to stderr\n // via the inherited stderr stream, not our event queue.\n }\n }\n }\n\n private onClose(): void {\n this.closed = true;\n while (this.waiters.length > 0) this.waiters.shift()!(null);\n }\n\n private push(msg: JsonRpcMessage): void {\n const waiter = this.waiters.shift();\n if (waiter) waiter(msg);\n else this.queue.push(msg);\n }\n}\n\n/**\n * Quote a single argument for inclusion in a shell command line.\n * On Windows (cmd.exe): wrap in double quotes, escape internal `\"` as `\"\"`,\n * leave everything else alone. On POSIX: wrap in single quotes, escape\n * internal `'` as `'\\''`. Both handle spaces, wildcards, pipes, and all\n * other metacharacters correctly.\n */\nfunction quoteArg(s: string, windows: boolean): string {\n if (!windows) {\n // POSIX: single-quote, escape single quotes.\n return `'${s.replace(/'/g, \"'\\\\''\")}'`;\n }\n // cmd.exe: double-quote, escape internal quotes by doubling.\n return `\"${s.replace(/\"/g, '\"\"')}\"`;\n}\n","/**\n * HTTP+SSE transport for MCP (spec version 2024-11-05).\n *\n * Wire shape:\n * 1. Client opens GET to the SSE URL with `Accept: text/event-stream`.\n * 2. Server's first SSE event is `event: endpoint`, `data: <url>` — the\n * URL (relative or absolute) the client must POST JSON-RPC requests\n * to. All subsequent server → client messages arrive as `event: message`\n * SSE frames carrying a JSON-RPC response or server-initiated frame.\n * 3. Client POSTs each outgoing JSON-RPC frame to the endpoint URL.\n * The POST response body is ignored — replies land on the SSE stream.\n *\n * This transport exists so Reasonix can talk to hosted/remote MCP servers\n * (e.g. a company's internal knowledge server fronted by auth). Stdio\n * covers local subprocesses; SSE covers everything else.\n *\n * Note: the newer \"Streamable HTTP\" transport (2025 spec) folds the POST\n * and SSE streams onto a single endpoint. We stay on 2024-11-05 here —\n * that's what `MCP_PROTOCOL_VERSION` advertises in the initialize handshake\n * and what currently-published servers implement.\n */\n\nimport { createParser } from \"eventsource-parser\";\nimport type { McpTransport } from \"./stdio.js\";\nimport type { JsonRpcMessage } from \"./types.js\";\n\nexport interface SseTransportOptions {\n /** SSE endpoint URL, e.g. `https://mcp.example.com/sse`. */\n url: string;\n /** Extra headers sent on both the SSE GET and the JSON-RPC POSTs (e.g. `Authorization`). */\n headers?: Record<string, string>;\n}\n\n/**\n * Open an SSE stream to `url`, parse incoming events into JsonRpcMessages,\n * POST outgoing frames to the endpoint URL the server advertises.\n */\nexport class SseTransport implements McpTransport {\n private readonly url: string;\n private readonly headers: Record<string, string>;\n private readonly queue: JsonRpcMessage[] = [];\n private readonly waiters: Array<(m: JsonRpcMessage | null) => void> = [];\n private readonly controller = new AbortController();\n private closed = false;\n private postUrl: string | null = null;\n private readonly endpointReady: Promise<string>;\n private resolveEndpoint!: (url: string) => void;\n private rejectEndpoint!: (err: Error) => void;\n\n constructor(opts: SseTransportOptions) {\n this.url = opts.url;\n this.headers = opts.headers ?? {};\n this.endpointReady = new Promise<string>((resolve, reject) => {\n this.resolveEndpoint = resolve;\n this.rejectEndpoint = reject;\n });\n // Swallow unhandled-rejection noise if nobody ever calls send().\n this.endpointReady.catch(() => undefined);\n void this.runStream();\n }\n\n async send(message: JsonRpcMessage): Promise<void> {\n if (this.closed) throw new Error(\"MCP SSE transport is closed\");\n const postUrl = await this.endpointReady;\n const res = await fetch(postUrl, {\n method: \"POST\",\n headers: { \"content-type\": \"application/json\", ...this.headers },\n body: JSON.stringify(message),\n signal: this.controller.signal,\n });\n // Drain body so the socket returns to the pool even if the server\n // elected to write one. We explicitly don't parse it — responses\n // arrive on the SSE channel.\n await res.arrayBuffer().catch(() => undefined);\n if (!res.ok) {\n throw new Error(`MCP SSE POST ${postUrl} failed: ${res.status} ${res.statusText}`);\n }\n }\n\n async *messages(): AsyncIterableIterator<JsonRpcMessage> {\n while (true) {\n if (this.queue.length > 0) {\n yield this.queue.shift()!;\n continue;\n }\n if (this.closed) return;\n const next = await new Promise<JsonRpcMessage | null>((resolve) => {\n this.waiters.push(resolve);\n });\n if (next === null) return;\n yield next;\n }\n }\n\n async close(): Promise<void> {\n if (this.closed) return;\n this.closed = true;\n while (this.waiters.length > 0) this.waiters.shift()!(null);\n // Reject any still-pending send() that was waiting for the endpoint.\n this.rejectEndpoint(new Error(\"MCP SSE transport closed before endpoint was ready\"));\n try {\n this.controller.abort();\n } catch {\n /* already aborted */\n }\n }\n\n // ---------- internals ----------\n\n private async runStream(): Promise<void> {\n let res: Response;\n try {\n res = await fetch(this.url, {\n method: \"GET\",\n headers: { accept: \"text/event-stream\", ...this.headers },\n signal: this.controller.signal,\n });\n } catch (err) {\n this.failHandshake(`SSE connect to ${this.url} failed: ${(err as Error).message}`);\n return;\n }\n if (!res.ok || !res.body) {\n // Drain body to free the socket before giving up.\n await res.body?.cancel().catch(() => undefined);\n this.failHandshake(`SSE handshake ${this.url} → ${res.status} ${res.statusText}`);\n return;\n }\n\n const parser = createParser({\n onEvent: (ev) => this.handleEvent(ev.event ?? \"message\", ev.data),\n });\n const decoder = new TextDecoder();\n try {\n for await (const chunk of res.body as AsyncIterable<Uint8Array>) {\n parser.feed(decoder.decode(chunk, { stream: true }));\n }\n } catch (err) {\n if (!this.closed) {\n this.pushError(`SSE stream error: ${(err as Error).message}`);\n }\n } finally {\n this.markClosed();\n }\n }\n\n private handleEvent(type: string, data: string): void {\n if (type === \"endpoint\") {\n if (this.postUrl) return; // ignore repeat announcements\n try {\n this.postUrl = new URL(data, this.url).toString();\n this.resolveEndpoint(this.postUrl);\n } catch (err) {\n this.failHandshake(`SSE endpoint event had bad URL \"${data}\": ${(err as Error).message}`);\n }\n return;\n }\n if (type === \"message\") {\n try {\n const parsed = JSON.parse(data) as JsonRpcMessage;\n this.pushMessage(parsed);\n } catch {\n // Malformed JSON-RPC on an SSE frame — drop it, same as stdio.\n }\n return;\n }\n // Unknown event types (server pings, custom extensions) — ignore.\n }\n\n private failHandshake(reason: string): void {\n this.rejectEndpoint(new Error(reason));\n this.pushError(reason);\n this.markClosed();\n }\n\n private pushMessage(msg: JsonRpcMessage): void {\n const waiter = this.waiters.shift();\n if (waiter) waiter(msg);\n else this.queue.push(msg);\n }\n\n private pushError(message: string): void {\n this.pushMessage({\n jsonrpc: \"2.0\",\n id: null,\n error: { code: -32000, message },\n });\n }\n\n private markClosed(): void {\n if (this.closed) return;\n this.closed = true;\n while (this.waiters.length > 0) this.waiters.shift()!(null);\n }\n}\n","/**\n * Split a shell-style command string into argv, respecting single and\n * double quotes. Intended for parsing the user's `--mcp \"cmd args...\"`\n * flag — NOT a full shell parser (no variable expansion, no subshells,\n * no globs, no `&&` / pipes).\n *\n * The tradeoff: users with paths containing spaces need to quote them\n * (e.g. `--mcp 'npx -y pkg \"/my path/here\"'`), which is how they'd\n * already quote them at the shell level.\n *\n * Throws on unterminated quotes — better than silently dropping half\n * the command.\n */\nexport function shellSplit(input: string): string[] {\n const tokens: string[] = [];\n let cur = \"\";\n let quote: '\"' | \"'\" | null = null;\n let i = 0;\n const s = input;\n\n while (i < s.length) {\n const ch = s[i]!;\n\n if (quote) {\n if (ch === quote) {\n quote = null;\n i++;\n continue;\n }\n // backslash escapes inside double quotes only\n if (ch === \"\\\\\" && quote === '\"' && i + 1 < s.length) {\n cur += s[i + 1];\n i += 2;\n continue;\n }\n cur += ch;\n i++;\n continue;\n }\n\n if (ch === '\"' || ch === \"'\") {\n quote = ch as '\"' | \"'\";\n i++;\n continue;\n }\n\n // Backslash escape ONLY applies inside double quotes (handled above).\n // Outside quotes, backslashes pass through literally — otherwise\n // Windows paths like `C:\\path\\to\\exe` get mangled. POSIX users who\n // want to escape a space outside quotes can use single quotes instead.\n\n if (ch === \" \" || ch === \"\\t\") {\n if (cur.length > 0) {\n tokens.push(cur);\n cur = \"\";\n }\n i++;\n continue;\n }\n\n cur += ch;\n i++;\n }\n\n if (quote) {\n throw new Error(\n `shellSplit: unterminated ${quote === '\"' ? \"double\" : \"single\"} quote in input`,\n );\n }\n if (cur.length > 0) tokens.push(cur);\n return tokens;\n}\n","/**\n * Parse the `--mcp` CLI argument into a transport-tagged spec.\n *\n * Accepted forms:\n * \"name=command args...\" → stdio, namespaced (tools prefixed with `name_`)\n * \"command args...\" → stdio, anonymous\n * \"name=https://host/sse\" → SSE, namespaced\n * \"https://host/sse\" → SSE, anonymous\n * (\"http://\" is also honored — useful for local dev servers.)\n *\n * The identifier regex before `=` is deliberately narrow\n * (`[a-zA-Z_][a-zA-Z0-9_]*`) so Windows drive letters (\"C:\\\\...\") and\n * other strings containing `=` or `:` don't accidentally trigger the\n * namespace branch. If a user ever wants their command to literally start\n * with `foo=...` as a bare command, they can wrap it in quotes inside the\n * shell command string.\n *\n * Transport is selected solely by whether the body begins with `http://`\n * or `https://`. Anything else is stdio — including ws:// (unsupported)\n * which will surface later as a spawn error, keeping the rule local.\n */\n\nimport { shellSplit } from \"./shell-split.js\";\n\nexport interface StdioMcpSpec {\n transport: \"stdio\";\n /** Namespace prefix applied to each registered tool, or null if anonymous. */\n name: string | null;\n /** Argv[0]. */\n command: string;\n /** Remaining argv. */\n args: string[];\n}\n\nexport interface SseMcpSpec {\n transport: \"sse\";\n name: string | null;\n /** Fully qualified SSE endpoint URL. */\n url: string;\n}\n\nexport type McpSpec = StdioMcpSpec | SseMcpSpec;\n\nconst NAME_PREFIX = /^([a-zA-Z_][a-zA-Z0-9_]*)=(.*)$/;\nconst HTTP_URL = /^https?:\\/\\//i;\n\nexport function parseMcpSpec(input: string): McpSpec {\n const trimmed = input.trim();\n if (!trimmed) {\n throw new Error(\"empty MCP spec\");\n }\n\n const nameMatch = NAME_PREFIX.exec(trimmed);\n const name = nameMatch ? nameMatch[1]! : null;\n const body = (nameMatch ? nameMatch[2]! : trimmed).trim();\n\n if (!body) {\n throw new Error(`MCP spec has name but no command: ${input}`);\n }\n\n if (HTTP_URL.test(body)) {\n return { transport: \"sse\", name, url: body };\n }\n\n const argv = shellSplit(body);\n if (argv.length === 0) {\n throw new Error(`MCP spec has name but no command: ${input}`);\n }\n const [command, ...args] = argv;\n return { transport: \"stdio\", name, command: command!, args };\n}\n","/**\n * Gather a full inspection report from an initialized MCP client:\n * server info, capabilities, tools, resources, prompts. Methods the\n * server doesn't support come back as `{ supported: false }` instead\n * of throwing, so a CLI or UI can render a consistent \"what this\n * server exposes\" summary even against minimal implementations.\n *\n * Pure with respect to I/O beyond the passed-in client — the CLI\n * layer owns argument parsing, connection setup, and printing.\n */\n\nimport type { McpClient } from \"./client.js\";\nimport type { McpPrompt, McpResource, McpTool } from \"./types.js\";\n\nexport interface InspectionReport {\n protocolVersion: string;\n serverInfo: { name: string; version: string };\n capabilities: Record<string, unknown>;\n instructions?: string;\n tools: SectionResult<McpTool>;\n resources: SectionResult<McpResource>;\n prompts: SectionResult<McpPrompt>;\n}\n\nexport type SectionResult<T> =\n | { supported: true; items: T[] }\n | { supported: false; reason: string };\n\n/**\n * Run an inspection against a **already-initialized** client. Caller\n * is responsible for `initialize()` before this and `close()` after.\n * We keep this pure so unit tests can feed in a FakeMcpTransport and\n * verify the aggregate shape without spinning up a real process.\n */\nexport async function inspectMcpServer(client: McpClient): Promise<InspectionReport> {\n // We always *try* the three listings so the client learns whether a\n // server without explicit capability flags still serves them —\n // some servers omit capabilities but still respond to the methods.\n const tools = await trySection<McpTool>(() => client.listTools().then((r) => r.tools));\n const resources = await trySection<McpResource>(() =>\n client.listResources().then((r) => r.resources),\n );\n const prompts = await trySection<McpPrompt>(() => client.listPrompts().then((r) => r.prompts));\n\n return {\n protocolVersion: client.protocolVersion || \"(unknown)\",\n serverInfo: client.serverInfo,\n capabilities: client.serverCapabilities ?? {},\n instructions: client.serverInstructions,\n tools,\n resources,\n prompts,\n };\n}\n\nasync function trySection<T>(load: () => Promise<T[]>): Promise<SectionResult<T>> {\n try {\n const items = await load();\n return { supported: true, items };\n } catch (err) {\n const msg = (err as Error).message ?? String(err);\n // -32601 is JSON-RPC \"method not found\" — the canonical response\n // from a server that doesn't implement this family. Treat it as\n // \"not supported\" rather than a hard error, so the CLI can render\n // a clean summary instead of aborting on the first missing method.\n if (/-32601/.test(msg) || /method not found/i.test(msg)) {\n return { supported: false, reason: \"method not found (-32601)\" };\n }\n return { supported: false, reason: msg };\n }\n}\n","/**\n * Aider-style SEARCH/REPLACE edit blocks.\n *\n * The model emits blocks in this exact shape, one or more per response:\n *\n * path/to/file.ts\n * <<<<<<< SEARCH\n * exact existing lines (whitespace-sensitive)\n * =======\n * replacement lines\n * >>>>>>> REPLACE\n *\n * We chose this over unified diffs because:\n * - Models produce it reliably — no line-number drift.\n * - It tolerates multi-edit responses without ambiguity over which\n * hunk belongs to which file.\n * - Aider has years of evidence that this format works even against\n * weaker models than DeepSeek R1, so it's a conservative pick.\n *\n * The SEARCH text must match the file byte-for-byte. Empty SEARCH is a\n * sentinel for \"create new file\" — the REPLACE becomes the whole file.\n * If SEARCH doesn't match we refuse the edit and surface the failure;\n * we do NOT guess or fuzzy-match. A wrong silent edit is worse than a\n * missing one — the user can re-ask with the exact current content.\n */\n\nimport { existsSync, mkdirSync, readFileSync, unlinkSync, writeFileSync } from \"node:fs\";\nimport { dirname, resolve } from \"node:path\";\n\nexport interface EditBlock {\n /** Path as written by the model — relative to rootDir, or absolute. */\n path: string;\n /** Literal text to match in the target file. Empty → create new file. */\n search: string;\n /** Replacement text to write in place of `search`. */\n replace: string;\n /** Char offset in the source message where this block started. */\n offset: number;\n}\n\nexport type ApplyStatus =\n /** Edit landed on disk. */\n | \"applied\"\n /** New file created (SEARCH was empty and file didn't exist). */\n | \"created\"\n /** File exists but SEARCH block wasn't found in its content. */\n | \"not-found\"\n /** File doesn't exist and SEARCH was non-empty (can't create without content). */\n | \"file-missing\"\n /** Path escapes rootDir — refused on safety grounds. */\n | \"path-escape\"\n /** fs write / read threw. */\n | \"error\";\n\nexport interface ApplyResult {\n path: string;\n status: ApplyStatus;\n /** Extra detail (e.g. error message) for logs. */\n message?: string;\n}\n\n/**\n * One edit block per match. The regex is anchored to the 7-char marker\n * lines because those are visually distinct and unlikely to appear in\n * normal code.\n *\n * Anchored with `^` + `m` flag so the filename has to live on its own\n * line. Keeps us from matching e.g. a JS-import string that happens to\n * contain `<<<<<<< SEARCH` in inner text.\n */\n// `\\n?` before the =======/REPLACE separators makes the body optional:\n// empty SEARCH (new-file sentinel) works without requiring a gratuitous\n// empty line, and the same holds for empty REPLACE (file-deletion\n// semantics, not yet supported but cheaply representable).\nconst BLOCK_RE = /^(\\S[^\\n]*)\\n<{7} SEARCH\\n([\\s\\S]*?)\\n?={7}\\n([\\s\\S]*?)\\n?>{7} REPLACE/gm;\n\nexport function parseEditBlocks(text: string): EditBlock[] {\n const out: EditBlock[] = [];\n BLOCK_RE.lastIndex = 0;\n let m: RegExpExecArray | null = BLOCK_RE.exec(text);\n while (m !== null) {\n out.push({\n path: m[1]!.trim(),\n search: m[2]!,\n replace: m[3]!,\n offset: m.index,\n });\n m = BLOCK_RE.exec(text);\n }\n return out;\n}\n\nexport function applyEditBlock(block: EditBlock, rootDir: string): ApplyResult {\n const absRoot = resolve(rootDir);\n const absTarget = resolve(absRoot, block.path);\n // Refuse paths that escape rootDir. `resolve` normalizes `..`, so\n // startsWith on the normalized pair is enough.\n if (absTarget !== absRoot && !absTarget.startsWith(`${absRoot}${sep()}`)) {\n return {\n path: block.path,\n status: \"path-escape\",\n message: `resolved path ${absTarget} is outside rootDir ${absRoot}`,\n };\n }\n\n const searchEmpty = block.search.length === 0;\n const exists = existsSync(absTarget);\n\n try {\n if (!exists) {\n if (!searchEmpty) {\n return {\n path: block.path,\n status: \"file-missing\",\n message: \"file does not exist; to create it, use an empty SEARCH block\",\n };\n }\n mkdirSync(dirname(absTarget), { recursive: true });\n writeFileSync(absTarget, block.replace, \"utf8\");\n return { path: block.path, status: \"created\" };\n }\n\n const content = readFileSync(absTarget, \"utf8\");\n if (searchEmpty) {\n return {\n path: block.path,\n status: \"not-found\",\n message: \"empty SEARCH only creates new files — this file already exists\",\n };\n }\n const idx = content.indexOf(block.search);\n if (idx === -1) {\n return {\n path: block.path,\n status: \"not-found\",\n message: \"SEARCH text does not match the current file content exactly\",\n };\n }\n // Replace only the first occurrence — if the model needs multiple\n // identical edits it should emit multiple blocks (each anchored by\n // more surrounding context). Auto-expanding to replace-all is a\n // footgun when the same string legitimately appears in several\n // unrelated places.\n const replaced = `${content.slice(0, idx)}${block.replace}${content.slice(idx + block.search.length)}`;\n writeFileSync(absTarget, replaced, \"utf8\");\n return { path: block.path, status: \"applied\" };\n } catch (err) {\n return { path: block.path, status: \"error\", message: (err as Error).message };\n }\n}\n\nexport function applyEditBlocks(blocks: EditBlock[], rootDir: string): ApplyResult[] {\n return blocks.map((b) => applyEditBlock(b, rootDir));\n}\n\n// ---------- snapshot / restore (for /undo) ----------\n\nexport interface EditSnapshot {\n /** Path relative to rootDir, as the block named it. */\n path: string;\n /**\n * File content before the edit batch was applied. `null` means the\n * file didn't exist yet — restoring that means deleting whatever the\n * edit created.\n */\n prevContent: string | null;\n}\n\n/**\n * Capture the current state of every file an edit batch is about to\n * touch, so `/undo` can roll back if the user doesn't like the result.\n * De-duplicates by path because one batch can contain multiple blocks\n * for the same file, and we only want one \"before\" snapshot per file.\n */\nexport function snapshotBeforeEdits(blocks: EditBlock[], rootDir: string): EditSnapshot[] {\n const absRoot = resolve(rootDir);\n const seen = new Set<string>();\n const snapshots: EditSnapshot[] = [];\n for (const b of blocks) {\n if (seen.has(b.path)) continue;\n seen.add(b.path);\n const abs = resolve(absRoot, b.path);\n if (!existsSync(abs)) {\n snapshots.push({ path: b.path, prevContent: null });\n continue;\n }\n try {\n snapshots.push({ path: b.path, prevContent: readFileSync(abs, \"utf8\") });\n } catch {\n // Unreadable (permission / binary) — record null so we at least\n // don't pretend the snapshot is authoritative. The restore path\n // will treat null as \"delete on undo\", which is wrong in that\n // case but the file wasn't ours to begin with.\n snapshots.push({ path: b.path, prevContent: null });\n }\n }\n return snapshots;\n}\n\n/**\n * Restore files to their snapshotted state. Snapshots with\n * `prevContent === null` were created by the edit, so undo = delete.\n * Otherwise the prior content is written back, replacing whatever the\n * edit left behind.\n */\nexport function restoreSnapshots(snapshots: EditSnapshot[], rootDir: string): ApplyResult[] {\n const absRoot = resolve(rootDir);\n return snapshots.map((snap) => {\n const abs = resolve(absRoot, snap.path);\n if (abs !== absRoot && !abs.startsWith(`${absRoot}${sep()}`)) {\n return {\n path: snap.path,\n status: \"path-escape\",\n message: \"snapshot path escapes rootDir — refusing to restore\",\n };\n }\n try {\n if (snap.prevContent === null) {\n if (existsSync(abs)) unlinkSync(abs);\n return {\n path: snap.path,\n status: \"applied\",\n message: \"removed (the edit had created it)\",\n };\n }\n writeFileSync(abs, snap.prevContent, \"utf8\");\n return {\n path: snap.path,\n status: \"applied\",\n message: \"restored to pre-edit content\",\n };\n } catch (err) {\n return { path: snap.path, status: \"error\", message: (err as Error).message };\n }\n });\n}\n\n/** Platform separator — `\\` on Windows, `/` elsewhere. */\nfunction sep(): string {\n return process.platform === \"win32\" ? \"\\\\\" : \"/\";\n}\n","/**\n * Version module.\n *\n * Two jobs:\n *\n * 1. Expose `VERSION` sourced from the real `package.json` so the\n * constant never drifts from what npm publishes. Works in dev\n * (`tsx src/...`) AND after `tsup` bundles to `dist/` — both\n * layouts sit two levels below the manifest, so a short\n * walk-up finds it.\n *\n * 2. Offer an opt-in `getLatestVersion()` that hits the npm\n * registry with a bounded timeout and a 24-hour on-disk\n * cache at `~/.reasonix/version-cache.json`. Returns `null`\n * on any failure — offline / restricted-network launches\n * should stay silent rather than nag the user.\n *\n * The CLI wires `getLatestVersion` asynchronously at App mount\n * (never in a hot path) and renders the outcome in the stats\n * panel when there's a newer published version.\n */\n\nimport { existsSync, mkdirSync, readFileSync, writeFileSync } from \"node:fs\";\nimport { homedir } from \"node:os\";\nimport { dirname, join } from \"node:path\";\nimport { fileURLToPath } from \"node:url\";\n\n/** npm registry endpoint for the `latest` dist-tag of this package. */\nconst REGISTRY_URL = \"https://registry.npmjs.org/reasonix/latest\";\n\n/** TTL for the on-disk cache entry. 24h keeps noise low; users who\n * want a fresh check can run `reasonix update` which passes\n * `force: true`. */\nexport const LATEST_CACHE_TTL_MS = 24 * 60 * 60 * 1000;\n\n/** Network timeout. Short — we never block the UI waiting on this. */\nexport const LATEST_FETCH_TIMEOUT_MS = 2_000;\n\n/**\n * Walk up from the current source file looking for the `reasonix`\n * package.json. Works for:\n * - dev: `src/version.ts` → `F:/Reasonix/package.json` (2 levels up)\n * - built: `dist/index.js` → `F:/Reasonix/package.json` (2 levels up)\n * - global install: `.../node_modules/reasonix/dist/index.js` → `.../reasonix/package.json`\n *\n * The `name === \"reasonix\"` guard is a cheap safety net against\n * picking up the nearest *other* package.json if we're ever loaded\n * as a dependency and the layout is unusual.\n */\nfunction readPackageVersion(): string {\n try {\n let dir = dirname(fileURLToPath(import.meta.url));\n for (let i = 0; i < 6; i++) {\n const p = join(dir, \"package.json\");\n if (existsSync(p)) {\n const pkg = JSON.parse(readFileSync(p, \"utf8\"));\n if (pkg?.name === \"reasonix\" && typeof pkg.version === \"string\") {\n return pkg.version;\n }\n }\n const parent = dirname(dir);\n if (parent === dir) break;\n dir = parent;\n }\n } catch {\n /* fall through to fallback */\n }\n return \"0.0.0-dev\";\n}\n\nexport const VERSION: string = readPackageVersion();\n\ninterface VersionCacheEntry {\n version: string;\n /** Epoch millis the entry was written. Drives TTL comparisons. */\n checkedAt: number;\n}\n\nfunction cachePath(homeDirOverride?: string): string {\n return join(homeDirOverride ?? homedir(), \".reasonix\", \"version-cache.json\");\n}\n\nfunction readCache(homeDirOverride?: string): VersionCacheEntry | null {\n try {\n const raw = readFileSync(cachePath(homeDirOverride), \"utf8\");\n const parsed = JSON.parse(raw);\n if (parsed && typeof parsed.version === \"string\" && typeof parsed.checkedAt === \"number\") {\n return parsed;\n }\n } catch {\n /* missing or malformed → no cached entry */\n }\n return null;\n}\n\nfunction writeCache(entry: VersionCacheEntry, homeDirOverride?: string): void {\n try {\n const p = cachePath(homeDirOverride);\n mkdirSync(dirname(p), { recursive: true });\n writeFileSync(p, JSON.stringify(entry), \"utf8\");\n } catch {\n /* cache is best-effort — a failed write just means we'll re-fetch\n * next launch. No reason to surface this to the user. */\n }\n}\n\nexport interface GetLatestVersionOptions {\n /** Ignore the cached entry and always fetch fresh. Used by `reasonix update`. */\n force?: boolean;\n /** Registry URL override (tests). */\n registryUrl?: string;\n /** Home-directory override (tests). */\n homeDir?: string;\n /** Fetch implementation override (tests). Defaults to `globalThis.fetch`. */\n fetchImpl?: typeof fetch;\n /** TTL override (tests). */\n ttlMs?: number;\n /** Network timeout override (tests). */\n timeoutMs?: number;\n}\n\n/**\n * Resolve the latest published `reasonix` version from the npm registry.\n *\n * Returns `null` on any network / parse failure. Callers treat `null`\n * as \"don't know, don't nag the user.\" The cache entry is only\n * written on a successful fetch — a bad registry response won't\n * poison the cache.\n */\nexport async function getLatestVersion(opts: GetLatestVersionOptions = {}): Promise<string | null> {\n const ttl = opts.ttlMs ?? LATEST_CACHE_TTL_MS;\n if (!opts.force) {\n const cached = readCache(opts.homeDir);\n if (cached && Date.now() - cached.checkedAt < ttl) return cached.version;\n }\n\n const fetchImpl = opts.fetchImpl ?? globalThis.fetch;\n if (!fetchImpl) return null;\n const url = opts.registryUrl ?? REGISTRY_URL;\n const timeout = opts.timeoutMs ?? LATEST_FETCH_TIMEOUT_MS;\n const controller = new AbortController();\n const timer = setTimeout(() => controller.abort(), timeout);\n try {\n const res = await fetchImpl(url, {\n signal: controller.signal,\n headers: { accept: \"application/json\" },\n });\n if (!res.ok) return null;\n const body = (await res.json()) as { version?: unknown };\n if (typeof body.version !== \"string\") return null;\n writeCache({ version: body.version, checkedAt: Date.now() }, opts.homeDir);\n return body.version;\n } catch {\n return null;\n } finally {\n clearTimeout(timer);\n }\n}\n\n/**\n * Semver compare. Returns a negative number when `a < b`, positive\n * when `a > b`, zero when equal.\n *\n * Minimal pre-release handling: when the CORE (`x.y.z`) parts match,\n * any version WITH a suffix (`-rc.1`, `-alpha.4`) compares LOWER\n * than the bare version. That matches npm's dist-tag semantics —\n * `reasonix@latest` resolves to a real release, not a pre-release.\n *\n * We're deliberately not pulling in `semver` (~50KB). The three\n * cases we care about are: current > latest (future build, no\n * prompt), current < latest (prompt), current === latest (no prompt).\n */\nexport function compareVersions(a: string, b: string): number {\n const [aCore = \"0\", aPre = \"\"] = a.split(\"-\", 2);\n const [bCore = \"0\", bPre = \"\"] = b.split(\"-\", 2);\n const aParts = aCore.split(\".\").map((p) => Number.parseInt(p, 10) || 0);\n const bParts = bCore.split(\".\").map((p) => Number.parseInt(p, 10) || 0);\n for (let i = 0; i < 3; i++) {\n const diff = (aParts[i] ?? 0) - (bParts[i] ?? 0);\n if (diff !== 0) return diff;\n }\n if (!aPre && !bPre) return 0;\n if (!aPre) return 1;\n if (!bPre) return -1;\n return aPre < bPre ? -1 : aPre > bPre ? 1 : 0;\n}\n\n/**\n * Heuristic: did this process launch via `npx` / `pnpm dlx` instead\n * of a global install? The update command takes different advice in\n * each case — a global install can `npm i -g reasonix@latest`, while\n * npx just needs its cache to roll over on next launch.\n *\n * Signals checked, in order:\n * - `process.argv[1]` contains `_npx` (npm's ephemeral dir name)\n * - `process.argv[1]` contains `.pnpm` + `dlx`\n * - `npm_config_user_agent` contains `npx/`\n *\n * Any one hit → npx. False negatives are safe (worst case we suggest\n * `npm i -g` to an npx user, which is a valid way to upgrade too).\n */\nexport function isNpxInstall(): boolean {\n const bin = process.argv[1] ?? \"\";\n if (/[/\\\\]_npx[/\\\\]/.test(bin)) return true;\n if (/[/\\\\]\\.pnpm[/\\\\]/.test(bin) && /dlx/i.test(bin)) return true;\n const ua = process.env.npm_config_user_agent ?? \"\";\n if (ua.includes(\"npx/\")) return true;\n return false;\n}\n","import { existsSync, statSync } from \"node:fs\";\nimport { render } from \"ink\";\nimport React, { useState } from \"react\";\nimport { loadApiKey, searchEnabled } from \"../../config.js\";\nimport { loadDotenv } from \"../../env.js\";\nimport { McpClient } from \"../../mcp/client.js\";\nimport { type InspectionReport, inspectMcpServer } from \"../../mcp/inspect.js\";\nimport { bridgeMcpTools } from \"../../mcp/registry.js\";\nimport { parseMcpSpec } from \"../../mcp/spec.js\";\nimport { SseTransport } from \"../../mcp/sse.js\";\nimport { type McpTransport, StdioTransport } from \"../../mcp/stdio.js\";\nimport {\n loadSessionMessages,\n rewriteSession,\n sessionPath as sessionPathOf,\n} from \"../../session.js\";\nimport { ToolRegistry } from \"../../tools.js\";\nimport { registerMemoryTools } from \"../../tools/memory.js\";\nimport { registerSkillTools } from \"../../tools/skills.js\";\nimport { registerWebTools } from \"../../tools/web.js\";\nimport { App } from \"../ui/App.js\";\nimport { SessionPicker } from \"../ui/SessionPicker.js\";\nimport { Setup } from \"../ui/Setup.js\";\nimport type { McpServerSummary } from \"../ui/slash.js\";\n\nexport interface ProgressInfo {\n toolName: string;\n progress: number;\n total?: number;\n message?: string;\n}\n\nexport interface ChatOptions {\n model: string;\n system: string;\n transcript?: string;\n harvest?: boolean;\n branch?: number;\n session?: string;\n /** Zero or more MCP server specs. Each: `\"name=cmd args...\"` or `\"cmd args...\"`. */\n mcp?: string[];\n /** Global prefix — only used when a single anonymous server is given. */\n mcpPrefix?: string;\n /**\n * Pre-built ToolRegistry used as a seed. MCP bridges (if any) are\n * layered on top of whatever's already registered. Used by\n * `reasonix code` to register native filesystem tools in place of\n * the old `npx -y @modelcontextprotocol/server-filesystem` subprocess.\n */\n seedTools?: ToolRegistry;\n /**\n * Enable SEARCH/REPLACE edit-block processing after each assistant turn.\n * Set by `reasonix code`; plain `reasonix chat` leaves this off.\n */\n codeMode?: { rootDir: string };\n /** Skip the session picker — assume \"Resume\" (backwards-compatible auto-continue). */\n forceResume?: boolean;\n /** Skip the session picker — assume \"New\" (wipe the session file and start fresh). */\n forceNew?: boolean;\n}\n\ninterface RootProps extends ChatOptions {\n initialKey: string | undefined;\n tools: ToolRegistry | undefined;\n mcpSpecs: string[];\n mcpServers: McpServerSummary[];\n /**\n * Shared ref the bridge's `onProgress` callback writes through.\n * App sets `.current` to its own handler on mount so every\n * progress frame from any bridged tool lands in the UI's\n * `OngoingToolRow`. Ref keeps the wire-up synchronous with\n * React reconciliation (no effect-timing surprises).\n */\n progressSink: { current: ((info: ProgressInfo) => void) | null };\n /** Present when the session has prior messages; drives the picker. */\n sessionPreview?: { messageCount: number; lastActive: Date };\n}\n\nfunction Root({\n initialKey,\n tools,\n mcpSpecs,\n mcpServers,\n progressSink,\n sessionPreview,\n ...appProps\n}: RootProps) {\n const [key, setKey] = useState<string | undefined>(initialKey);\n // `null` once the picker is resolved (or was never needed). Starts as\n // the preview so we can render the picker once before mounting App.\n const [pending, setPending] = useState<typeof sessionPreview>(sessionPreview);\n\n if (!key) {\n return (\n <Setup\n onReady={(k) => {\n process.env.DEEPSEEK_API_KEY = k;\n setKey(k);\n }}\n />\n );\n }\n process.env.DEEPSEEK_API_KEY = key;\n\n if (pending && appProps.session) {\n return (\n <SessionPicker\n sessionName={appProps.session}\n messageCount={pending.messageCount}\n lastActive={pending.lastActive}\n onChoose={(choice) => {\n if (choice === \"new\" || choice === \"delete\") {\n // Wipe the session file. \"new\" and \"delete\" do the same thing\n // at this step — the distinction is only in the picker's\n // wording. A future enhancement could archive on \"new\".\n rewriteSession(appProps.session!, []);\n }\n setPending(undefined);\n }}\n />\n );\n }\n\n return (\n <App\n model={appProps.model}\n system={appProps.system}\n transcript={appProps.transcript}\n harvest={appProps.harvest}\n branch={appProps.branch}\n session={appProps.session}\n tools={tools}\n mcpSpecs={mcpSpecs}\n mcpServers={mcpServers}\n progressSink={progressSink}\n codeMode={appProps.codeMode}\n />\n );\n}\n\nexport async function chatCommand(opts: ChatOptions): Promise<void> {\n loadDotenv();\n const initialKey = loadApiKey();\n\n const requestedSpecs = opts.mcp ?? [];\n const clients: McpClient[] = [];\n const successfulSpecs: string[] = [];\n const failedSpecs: Array<{ spec: string; reason: string }> = [];\n const mcpServers: McpServerSummary[] = [];\n // Shared progress sink: the bridge's onProgress callback writes\n // through `progressSink.current`, which App.tsx sets to its UI\n // updater on mount. Started null so early progress frames (before\n // the App has mounted) are dropped rather than buffered.\n const progressSink: { current: ((info: ProgressInfo) => void) | null } = { current: null };\n // Seed registry from the caller (e.g. reasonix code's native\n // filesystem tools) — MCP bridges layer on top rather than\n // replacing. When no seed AND no MCP, tools stays undefined and\n // the loop runs as a bare chat.\n let tools: ToolRegistry | undefined = opts.seedTools;\n\n if (requestedSpecs.length > 0) {\n if (!tools) tools = new ToolRegistry();\n for (const raw of requestedSpecs) {\n try {\n const spec = parseMcpSpec(raw);\n const prefix = spec.name\n ? `${spec.name}_`\n : requestedSpecs.length === 1 && opts.mcpPrefix\n ? opts.mcpPrefix\n : \"\";\n const transport: McpTransport =\n spec.transport === \"sse\"\n ? new SseTransport({ url: spec.url })\n : new StdioTransport({ command: spec.command, args: spec.args });\n const mcp = new McpClient({ transport });\n await mcp.initialize();\n const bridge = await bridgeMcpTools(mcp, {\n registry: tools,\n namePrefix: prefix,\n onProgress: (info) => progressSink.current?.(info),\n });\n // Collect resources + prompts once at startup so the /mcp\n // slash can render them synchronously. Servers that don't\n // support these fall through as `{supported: false}` instead\n // of throwing — see inspectMcpServer.\n let report: InspectionReport;\n try {\n report = await inspectMcpServer(mcp);\n } catch {\n // If the inspect call itself fails (rare — shouldn't happen\n // since inspectMcpServer swallows -32601), synthesize a\n // minimal report so `/mcp` still has something to render.\n report = {\n protocolVersion: mcp.protocolVersion,\n serverInfo: mcp.serverInfo,\n capabilities: mcp.serverCapabilities ?? {},\n tools: { supported: true, items: [] },\n resources: { supported: false, reason: \"inspect failed\" },\n prompts: { supported: false, reason: \"inspect failed\" },\n };\n }\n const label = spec.name ?? \"anon\";\n const source =\n spec.transport === \"sse\" ? spec.url : `${spec.command} ${spec.args.join(\" \")}`;\n process.stderr.write(\n `▸ MCP[${label}]: ${bridge.registeredNames.length} tool(s) from ${source}\\n`,\n );\n clients.push(mcp);\n successfulSpecs.push(raw);\n mcpServers.push({\n label,\n spec: raw,\n toolCount: bridge.registeredNames.length,\n report,\n });\n } catch (err) {\n // Per-server failure is non-fatal: one broken server shouldn't\n // kill a chat that has working servers configured. We record\n // the failure, show a visible warning, and keep going. User\n // can fix via `reasonix setup` (unchecks the broken entry)\n // without losing their other servers.\n const reason = (err as Error).message;\n failedSpecs.push({ spec: raw, reason });\n process.stderr.write(\n `▸ MCP setup SKIPPED for \"${raw}\": ${reason}\\n → this server will not be available this session. Run \\`reasonix setup\\` to remove it, or fix the underlying issue (missing npm package, network, etc.).\\n`,\n );\n }\n }\n // If every requested server failed AND no seed registry was\n // provided, drop the empty registry so the loop still runs as a\n // bare chat instead of advertising zero tools. If the caller\n // passed seedTools we keep the registry — the seed tools are\n // still there and usable.\n if (successfulSpecs.length === 0 && !opts.seedTools) {\n tools = undefined;\n }\n }\n const mcpSpecs = successfulSpecs;\n\n // Register web search/fetch tools unless explicitly disabled. DDG\n // backs them with no key required; the model invokes them whenever\n // a question needs info fresher than its training data.\n if (searchEnabled()) {\n if (!tools) tools = new ToolRegistry();\n registerWebTools(tools);\n }\n\n // Memory tools — available in every session, not just code mode.\n // Chat-mode callers get global scope only; project scope requires\n // the seedTools path from `reasonix code` (which registers its own\n // MemoryStore bound to rootDir before chatCommand runs).\n if (!opts.seedTools) {\n if (!tools) tools = new ToolRegistry();\n registerMemoryTools(tools, {});\n // Skills are a chat-mode concept too — load them here so plain\n // `reasonix chat` users can invoke user-defined skills. Code mode\n // registers its own copy alongside its other seed tools.\n registerSkillTools(tools);\n }\n\n // Decide whether to show the session picker. It's gated on: session\n // persistence is on, the session file already has prior messages, and\n // the caller didn't pre-commit to one of the choices via --resume /\n // --new flags. `--new` wipes the file now (before the loop opens),\n // so the App mounts against a fresh log.\n let sessionPreview: { messageCount: number; lastActive: Date } | undefined;\n if (opts.session && !opts.forceResume && !opts.forceNew) {\n const prior = loadSessionMessages(opts.session);\n if (prior.length > 0) {\n const p = sessionPathOf(opts.session);\n const mtime = existsSync(p) ? statSync(p).mtime : new Date();\n sessionPreview = { messageCount: prior.length, lastActive: mtime };\n }\n } else if (opts.session && opts.forceNew) {\n rewriteSession(opts.session, []);\n }\n\n const { waitUntilExit } = render(\n <Root\n initialKey={initialKey}\n tools={tools}\n mcpSpecs={mcpSpecs}\n mcpServers={mcpServers}\n progressSink={progressSink}\n sessionPreview={sessionPreview}\n {...opts}\n />,\n // patchConsole:false — we never log to console during the TUI, and the\n // patch is a known redraw-glitch source on winpty/MINTTY terminals.\n { exitOnCtrlC: true, patchConsole: false },\n );\n try {\n await waitUntilExit();\n } finally {\n for (const c of clients) await c.close();\n }\n}\n","/**\n * `run_skill` — load one user skill's body into the conversation.\n *\n * The Skills index (names + one-liners) is pinned in the system prompt\n * by `applySkillsIndex`. That's enough context for the model to decide\n * *which* skill to invoke, but the body is NOT in the prefix — calling\n * this tool is how the body enters the turn. The tool result is the\n * full markdown instruction block; the model reads it and continues\n * the normal tool-use loop to follow whatever the skill prescribes.\n *\n * v1 deliberately ignores each skill's `allowed-tools` frontmatter:\n * Reasonix's tool namespace (`filesystem`, `shell`, `web`) doesn't\n * align with Claude Code's (`Read`, `Bash`, `Grep`) so a literal pass\n * would give wrong answers. Skills written for Claude Code still run —\n * the model reads the prose instructions and picks our equivalents.\n */\n\nimport { SkillStore } from \"../skills.js\";\nimport type { ToolRegistry } from \"../tools.js\";\n\nexport interface SkillToolsOptions {\n /** Override `$HOME` — tests set this to a tmpdir. */\n homeDir?: string;\n /**\n * Absolute project root — enables discovery of project-scope skills\n * under `<projectRoot>/.reasonix/skills/`. Omit for chat mode (global\n * scope only).\n */\n projectRoot?: string;\n}\n\nexport function registerSkillTools(\n registry: ToolRegistry,\n opts: SkillToolsOptions = {},\n): ToolRegistry {\n const store = new SkillStore({ homeDir: opts.homeDir, projectRoot: opts.projectRoot });\n\n registry.register({\n name: \"run_skill\",\n description:\n \"Load the full body of a user-defined skill into this conversation. Call when the pinned Skills index (in the system prompt) lists a skill whose description matches what's being asked. Returns the skill's markdown instructions — read them and continue the loop, calling whatever filesystem / shell / web tools the skill's prose requires. Skills are user content; follow their instructions, but keep Reasonix's own safety rules (no destructive ops without confirmation, etc.).\",\n readOnly: true,\n parameters: {\n type: \"object\",\n properties: {\n name: {\n type: \"string\",\n description:\n \"Skill identifier as it appears in the pinned Skills index (e.g. 'review', 'security-review'). Case-sensitive.\",\n },\n arguments: {\n type: \"string\",\n description:\n \"Optional free-form arguments the caller wants the skill to act on. Forwarded verbatim as an 'Arguments:' line appended to the skill body; the skill's own instructions decide how to consume them.\",\n },\n },\n required: [\"name\"],\n },\n fn: async (args: { name?: unknown; arguments?: unknown }) => {\n const name = typeof args.name === \"string\" ? args.name.trim() : \"\";\n if (!name) {\n return JSON.stringify({ error: \"run_skill requires a 'name' argument\" });\n }\n const skill = store.read(name);\n if (!skill) {\n const available = store\n .list()\n .map((s) => s.name)\n .join(\", \");\n return JSON.stringify({\n error: `unknown skill: ${JSON.stringify(name)}`,\n available: available || \"(none — user has not defined any skills)\",\n });\n }\n const rawArgs = typeof args.arguments === \"string\" ? args.arguments.trim() : \"\";\n const header = [\n `# Skill: ${skill.name}`,\n skill.description ? `> ${skill.description}` : \"\",\n `(scope: ${skill.scope} · ${skill.path})`,\n ]\n .filter(Boolean)\n .join(\"\\n\");\n const argsBlock = rawArgs ? `\\n\\nArguments: ${rawArgs}` : \"\";\n // The body is handed to the model verbatim. No truncation — the\n // user authored it, we trust their length choice. The append-only\n // log pays the token cost exactly once per invocation.\n return `${header}\\n\\n${skill.body}${argsBlock}`;\n },\n });\n\n return registry;\n}\n","import type { WriteStream } from \"node:fs\";\nimport { Box, Static, Text, useApp, useInput } from \"ink\";\nimport React, { useCallback, useEffect, useMemo, useRef, useState } from \"react\";\nimport {\n type ApplyResult,\n type EditBlock,\n type EditSnapshot,\n applyEditBlocks,\n parseEditBlocks,\n restoreSnapshots,\n snapshotBeforeEdits,\n} from \"../../code/edit-blocks.js\";\nimport { addProjectShellAllowed } from \"../../config.js\";\nimport { type ResolvedHook, formatHookOutcomeMessage, loadHooks, runHooks } from \"../../hooks.js\";\nimport { CacheFirstLoop, DeepSeekClient, ImmutablePrefix } from \"../../index.js\";\nimport type { LoopEvent } from \"../../loop.js\";\nimport type { SessionSummary } from \"../../telemetry.js\";\nimport type { ToolRegistry } from \"../../tools.js\";\nimport { formatCommandResult, runCommand } from \"../../tools/shell.js\";\nimport { openTranscriptFile, recordFromLoopEvent, writeRecord } from \"../../transcript.js\";\nimport { VERSION, compareVersions, getLatestVersion } from \"../../version.js\";\nimport { type DisplayEvent, EventRow } from \"./EventLog.js\";\nimport { PlanConfirm, type PlanConfirmChoice } from \"./PlanConfirm.js\";\nimport { PlanRefineInput } from \"./PlanRefineInput.js\";\nimport { PromptInput } from \"./PromptInput.js\";\nimport { ShellConfirm, type ShellConfirmChoice, derivePrefix } from \"./ShellConfirm.js\";\nimport { SlashSuggestions } from \"./SlashSuggestions.js\";\nimport { StatsPanel } from \"./StatsPanel.js\";\nimport { type McpServerSummary, handleSlash, parseSlash, suggestSlashCommands } from \"./slash.js\";\nimport { TickerProvider, useElapsedSeconds, useTick } from \"./ticker.js\";\n\nexport interface AppProps {\n model: string;\n system: string;\n transcript?: string;\n harvest?: boolean;\n branch?: number;\n session?: string;\n /**\n * Pre-populated tool registry (e.g. from bridgeMcpTools()). When present,\n * its specs are folded into the ImmutablePrefix so the model sees them,\n * and its dispatch is used for tool calls — MCP tools become first-class.\n */\n tools?: ToolRegistry;\n /** Raw `--mcp` / config-derived spec strings, for `/mcp` slash display. */\n mcpSpecs?: string[];\n /**\n * Pre-captured inspection reports for each connected MCP server,\n * collected once at chat startup. Drives the rich `/mcp` slash view\n * (tools + resources + prompts per server).\n */\n mcpServers?: McpServerSummary[];\n /**\n * Shared ref the MCP bridge's onProgress callback writes through.\n * We attach our updater to `progressSink.current` on mount so any\n * `notifications/progress` frame from any bridged tool flows into\n * the UI. `null` allowed — chat mode without MCP leaves it unset.\n */\n progressSink?: {\n current:\n | ((info: { toolName: string; progress: number; total?: number; message?: string }) => void)\n | null;\n };\n /**\n * When set, parse SEARCH/REPLACE blocks from assistant responses and\n * apply them to disk under `rootDir`. Set by `reasonix code`.\n */\n codeMode?: { rootDir: string };\n}\n\n/**\n * Throttle interval in ms. We flush streaming deltas at most this often to\n * avoid re-rendering the whole UI on every single token from DeepSeek.\n * 100ms ≈ 10Hz, still feels live, gives fragile terminals (winpty/MINTTY)\n * enough room to finish a repaint before the next one arrives.\n */\nconst FLUSH_INTERVAL_MS = 100;\n\n/**\n * True when the user has opted out of live spinner/streaming rows.\n * `REASONIX_UI=plain` suppresses every transient row in the render\n * tree so only the `<Static>` committed history + the input prompt\n * are drawn. Trades liveness for stability on terminals where Ink's\n * cursor-up repaint leaves ghost artifacts.\n */\nconst PLAIN_UI = process.env.REASONIX_UI === \"plain\";\n\ninterface StreamingState {\n id: string;\n text: string;\n reasoning: string;\n toolCallBuild?: { name: string; chars: number };\n}\n\nexport function App({\n model,\n system,\n transcript,\n harvest,\n branch,\n session,\n tools,\n mcpSpecs,\n mcpServers,\n progressSink,\n codeMode,\n}: AppProps) {\n const { exit } = useApp();\n const [historical, setHistorical] = useState<DisplayEvent[]>([]);\n const [streaming, setStreaming] = useState<DisplayEvent | null>(null);\n const [input, setInput] = useState(\"\");\n const [busy, setBusy] = useState(false);\n // Tracks whether the current turn has been aborted via Esc, so the\n // Esc handler only fires once per turn (repeated presses would yield\n // stacked warning events).\n const abortedThisTurn = useRef(false);\n // Name + truncated args of the tool currently dispatching. Populated\n // on `tool_start`, cleared on `tool` (or error). Drives the\n // \"▸ tool<X> running…\" pulse-spinner row so long tool calls don't\n // look like the app hung.\n const [ongoingTool, setOngoingTool] = useState<{ name: string; args?: string } | null>(null);\n // Latest progress frame for the currently-running tool (MCP\n // `notifications/progress`). `null` when no progress has been\n // reported for this tool call — OngoingToolRow still spins, just\n // without a progress number.\n const [toolProgress, setToolProgress] = useState<{\n progress: number;\n total?: number;\n message?: string;\n } | null>(null);\n // Transient \"what's happening\" text set by the loop during silent\n // phases (harvest round-trip, between-iteration R1 thinking, forced\n // summary). Rendered as a dim spinner row; auto-cleared on the next\n // primary event.\n const [statusLine, setStatusLine] = useState<string | null>(null);\n // DeepSeek account balance — fetched once on mount, refreshed after\n // each completed turn so the \"how much do I have left\" number\n // tracks reality. `null` means either the endpoint failed or we\n // haven't fetched yet; the panel hides the cell in that case.\n const [balance, setBalance] = useState<{ currency: string; total: number } | null>(null);\n // Latest published version the npm registry returned, REGARDLESS\n // of whether it's newer than what we're running. `null` only while\n // the background check is in flight or when the network fails —\n // so `/update` can distinguish \"on latest\" from \"still fetching\".\n // The yellow header badge is derived: it only lights up when the\n // fetched version is STRICTLY newer, but the slash surfaces the\n // raw value so the user always gets a concrete number.\n const [latestVersion, setLatestVersion] = useState<string | null>(null);\n const updateAvailable =\n latestVersion && compareVersions(VERSION, latestVersion) < 0 ? latestVersion : null;\n // Loaded user hooks (project + global settings.json). Stays mutable\n // so `/hooks reload` can rescan disk without reconstructing the\n // loop. The loop holds a parallel reference for its tool-event\n // dispatch; we keep them in sync via the effect below.\n const [hookList, setHookList] = useState<ResolvedHook[]>(() =>\n loadHooks({ projectRoot: codeMode?.rootDir }),\n );\n // Working directory reported in every hook's stdin payload. Hook\n // scripts that `cd $REASONIX_CWD` (or read `cwd` from the JSON\n // envelope) land in the project root, not the user's shell home.\n const hookCwd = codeMode?.rootDir ?? process.cwd();\n // Snapshots of every file the *last* edit batch touched, keyed by\n // nothing more than \"most recent\". `/undo` restores from this ref\n // and nulls it out — one level of undo, Aider-style. Multi-step\n // undo would need a proper history stack and a clear policy for\n // when the stack clears; v1 keeps it simple.\n const lastEditSnapshots = useRef<EditSnapshot[] | null>(null);\n // Pending edit blocks awaiting `/apply` or `/discard`. We do NOT\n // auto-apply — v0.4.1 showed that \"model proposed, so apply\" turns\n // analysis into unintended edits. The user explicitly confirms now.\n const pendingEdits = useRef<EditBlock[]>([]);\n // Shell command the model asked to run that wasn't on the auto-run\n // allowlist. Non-null renders the ShellConfirm modal and disables\n // the prompt input; the user picks Run once / Always allow in this\n // project / Deny and we feed the result back as a synthetic user\n // message so the model sees what happened.\n const [pendingShell, setPendingShell] = useState<string | null>(null);\n // Plan text the model submitted via `submit_plan` while plan mode\n // was active. Non-null renders PlanConfirm; user picks Approve /\n // Refine / Cancel and we drive the loop from there. Separate from\n // `planMode` because a pending plan is a one-shot decision even if\n // plan mode stays on (Refine keeps mode on; Approve/Cancel flip off).\n const [pendingPlan, setPendingPlan] = useState<string | null>(null);\n // Stashed plan + intent while the user types free-form feedback\n // (refinement or last instructions on approve). When the picker\n // returns \"refine\" or \"approve\", we defer the loop-resume and show\n // PlanRefineInput. User types + Enter → we ship it; Esc → restore\n // pendingPlan and re-show the picker. Letting Approve also take\n // input closes the \"model left open questions, user had no place\n // to answer them\" hole.\n const [stagedInput, setStagedInput] = useState<{\n plan: string;\n mode: \"refine\" | \"approve\";\n } | null>(null);\n // Plan-mode indicator — displayed in the StatsPanel, mirrored onto\n // the ToolRegistry so dispatch enforces read-only. Toggled via the\n // `/plan` slash and PlanConfirm picker. Ephemeral — not persisted\n // across launches (you explicitly opt in per session).\n const [planMode, setPlanMode] = useState<boolean>(false);\n // Text waiting to be submitted AFTER the current turn finishes.\n // Set by ShellConfirm's onChoose when the user approves faster than\n // the model's \"awaiting confirmation\" response. We can't call\n // handleSubmit directly because it early-returns on `busy === true`,\n // so we abort the in-flight turn and let the effect below fire the\n // submit once busy clears.\n const [queuedSubmit, setQueuedSubmit] = useState<string | null>(null);\n // Shell-style history of user prompts. ↑/↓ while idle walks it;\n // submit pushes to the end. Cursor -1 = \"live input\", 0+ = \"N turns\n // back from newest\". We don't persist history to disk — sessions\n // already keep the message log, and cross-session bash-style recall\n // would need per-project scoping we haven't designed.\n const promptHistory = useRef<string[]>([]);\n const historyCursor = useRef<number>(-1);\n // Disambiguates <Static> keys when a single turn yields multiple assistant_final events.\n const assistantIterCounter = useRef<number>(0);\n // Full untruncated tool results, in arrival order. The EventLog\n // renderer clips tool output at 400 chars for display; `/tool N`\n // reads from this ref to show the real thing. Not persisted — a\n // resumed session replays the log (which has the same content in\n // `tool` messages) but we don't repopulate this ref on resume\n // because the user wouldn't expect `/tool` to reach back across\n // process boundaries.\n const toolHistoryRef = useRef<Array<{ toolName: string; text: string }>>([]);\n // Highlighted suggestion index. ↑/↓ move within the current match\n // set while the user is typing a `/…` prefix; Enter or Tab pick.\n const [slashSelected, setSlashSelected] = useState(0);\n const [summary, setSummary] = useState<SessionSummary>({\n turns: 0,\n totalCostUsd: 0,\n totalInputCostUsd: 0,\n totalOutputCostUsd: 0,\n claudeEquivalentUsd: 0,\n savingsVsClaudePct: 0,\n cacheHitRatio: 0,\n lastPromptTokens: 0,\n });\n\n const transcriptRef = useRef<WriteStream | null>(null);\n if (transcript && !transcriptRef.current) {\n transcriptRef.current = openTranscriptFile(transcript, {\n version: 1,\n source: \"reasonix chat\",\n model,\n startedAt: new Date().toISOString(),\n });\n }\n useEffect(() => {\n return () => {\n transcriptRef.current?.end();\n };\n }, []);\n\n // The currently matching slash suggestions, or `null` when the\n // user isn't in slash-prefix mode. Shared between the useInput\n // handler (navigation + Tab-complete) and SlashSuggestions\n // (rendering + highlight) so both stay in sync.\n const slashMatches = useMemo(() => {\n if (!input.startsWith(\"/\") || input.includes(\" \")) return null;\n return suggestSlashCommands(input.slice(1), !!codeMode);\n }, [input, codeMode]);\n useEffect(() => {\n // Keep selection in range whenever the match set shrinks. Reset\n // to 0 whenever we re-enter slash mode (matches goes null → array\n // triggers the useEffect too).\n setSlashSelected((prev) => {\n if (!slashMatches || slashMatches.length === 0) return 0;\n if (prev >= slashMatches.length) return slashMatches.length - 1;\n return prev;\n });\n }, [slashMatches]);\n\n const loopRef = useRef<CacheFirstLoop | null>(null);\n // hookList + hookCwd intentionally NOT in deps — they seed the loop\n // on first construction (loopRef guards a single instantiation), and\n // later edits flow in through the mutable `loop.hooks = hookList`\n // effect below. Putting them in deps would tear down the loop on\n // every reload, wiping the append-only log mid-session.\n // biome-ignore lint/correctness/useExhaustiveDependencies: hookList — see comment above\n // biome-ignore lint/correctness/useExhaustiveDependencies: hookCwd — see comment above\n const loop = useMemo(() => {\n if (loopRef.current) return loopRef.current;\n const client = new DeepSeekClient();\n const prefix = new ImmutablePrefix({\n system,\n toolSpecs: tools?.specs(),\n });\n const l = new CacheFirstLoop({\n client,\n prefix,\n tools,\n model,\n harvest,\n branch,\n session,\n hooks: hookList,\n hookCwd,\n });\n loopRef.current = l;\n return l;\n }, [model, system, harvest, branch, session, tools]);\n\n // Keep the loop's hook list in sync after a `/hooks reload`. The\n // loop's field is intentionally mutable for exactly this case —\n // construction happens once, hook edits are picked up live.\n useEffect(() => {\n loop.hooks = hookList;\n }, [loop, hookList]);\n\n // Fetch balance once the API key is known. Non-blocking — the\n // session works without it; `null` hides the cell. We also refresh\n // after each completed turn (inside handleSubmit's finally) so the\n // number tracks actual spend rather than freezing at mount-time.\n useEffect(() => {\n let cancelled = false;\n void (async () => {\n const bal = await loop.client.getBalance().catch(() => null);\n if (cancelled || !bal || !bal.balance_infos.length) return;\n const primary = bal.balance_infos[0]!;\n setBalance({ currency: primary.currency, total: Number(primary.total_balance) });\n })();\n return () => {\n cancelled = true;\n };\n }, [loop]);\n\n // Background registry check — 24h disk cache absorbs repeated\n // launches, timeout bounded so a flaky network doesn't delay the\n // notification. Set to `null` on failure (silent: no network, no\n // problem). We store the raw version regardless of whether it's\n // newer; the header badge's newer-only check happens at the\n // `updateAvailable` derivation above.\n useEffect(() => {\n let cancelled = false;\n void (async () => {\n const latest = await getLatestVersion();\n if (cancelled || !latest) return;\n setLatestVersion(latest);\n })();\n return () => {\n cancelled = true;\n };\n }, []);\n\n // Wire the shared progressSink so the bridge's onProgress → us.\n // Only updates progress when the frame belongs to the currently-\n // running tool: late frames from a previous call shouldn't overwrite\n // the spinner of whatever's running next.\n useEffect(() => {\n if (!progressSink) return;\n progressSink.current = (info) => {\n setToolProgress({\n progress: info.progress,\n total: info.total,\n message: info.message,\n });\n };\n return () => {\n if (progressSink.current) progressSink.current = null;\n };\n }, [progressSink]);\n\n // Surface a one-time banner about session state on first mount.\n const sessionBannerShown = useRef(false);\n useEffect(() => {\n if (sessionBannerShown.current) return;\n sessionBannerShown.current = true;\n if (!session) {\n setHistorical((prev) => [\n ...prev,\n {\n id: `sys-session-${Date.now()}`,\n role: \"info\",\n text: \"▸ ephemeral chat (no session persistence) — drop --no-session to enable\",\n },\n ]);\n } else if (loop.resumedMessageCount > 0) {\n setHistorical((prev) => [\n ...prev,\n {\n id: `sys-resume-${Date.now()}`,\n role: \"info\",\n text: `▸ resumed session \"${session}\" with ${loop.resumedMessageCount} prior messages · /forget to start over · /sessions to list`,\n },\n ]);\n } else {\n setHistorical((prev) => [\n ...prev,\n {\n id: `sys-newsession-${Date.now()}`,\n role: \"info\",\n text: `▸ session \"${session}\" (new) — auto-saved as you chat · /forget to delete · /sessions to list`,\n },\n ]);\n }\n }, [session, loop]);\n\n // Esc during busy → forward to the loop as an abort signal. The loop\n // finishes the tool call in flight (we can't kill subprocess stdio\n // mid-write), then diverts to its no-tools summary path so the user\n // gets an answer instead of a hard stop. Only listens while busy so\n // we don't accidentally hijack Esc in other contexts.\n //\n // Also handles ↑/↓ shell-style history while idle. We don't use\n // ink-text-input's (absent) history support; parent-level useInput\n // is simpler and lets us own the cursor semantics.\n useInput((_input, key) => {\n if (key.escape && busy) {\n if (abortedThisTurn.current) return;\n abortedThisTurn.current = true;\n loop.abort();\n return;\n }\n if (busy) return;\n // ShellConfirm owns the full keyboard while it's showing. If we\n // kept handling ↑/↓ / Tab here they'd race with its SingleSelect\n // — the picker would move AND history recall would fire into the\n // (hidden) prompt buffer. Bail early.\n if (pendingShell) return;\n\n // Slash-suggestion mode takes priority over history recall.\n // When the user is typing a `/…` prefix and there are matches,\n // ↑/↓ walk the suggestion list and Tab snaps the input to the\n // highlighted command. Enter is handled in `handleSubmit` so\n // TextInput's onSubmit still fires cleanly.\n if (slashMatches && slashMatches.length > 0) {\n if (key.upArrow) {\n setSlashSelected((i) => Math.max(0, i - 1));\n return;\n }\n if (key.downArrow) {\n setSlashSelected((i) => Math.min(slashMatches.length - 1, i + 1));\n return;\n }\n if (key.tab) {\n const sel = slashMatches[slashSelected] ?? slashMatches[0];\n if (sel) setInput(`/${sel.cmd}`);\n return;\n }\n }\n\n // Outside slash mode: ↑/↓ recall prior prompts — but ONLY when the\n // buffer is empty. A non-empty buffer hands those keys to\n // PromptInput for cursor movement (multi-line navigation, or a\n // no-op on single-line) so the user doesn't accidentally clobber\n // typed text with a recalled prompt.\n if (input.length === 0) {\n const hist = promptHistory.current;\n if (key.upArrow) {\n if (hist.length === 0) return;\n const nextCursor = Math.min(historyCursor.current + 1, hist.length - 1);\n historyCursor.current = nextCursor;\n setInput(hist[hist.length - 1 - nextCursor] ?? \"\");\n return;\n }\n if (key.downArrow) {\n if (historyCursor.current < 0) return;\n const nextCursor = historyCursor.current - 1;\n historyCursor.current = nextCursor;\n setInput(nextCursor < 0 ? \"\" : (hist[hist.length - 1 - nextCursor] ?? \"\"));\n return;\n }\n }\n });\n\n /**\n * Callback wired into the `/undo` slash command. Restores the files\n * last edit batch to their pre-edit state and reports per-file\n * results. Only available when running in code mode — the slash\n * handler gates on this callback's presence.\n */\n const codeUndo = useCallback((): string => {\n if (!codeMode) return \"not in code mode\";\n const snaps = lastEditSnapshots.current;\n if (!snaps || snaps.length === 0) {\n return \"nothing to undo — no recent edit batch to restore\";\n }\n const results = restoreSnapshots(snaps, codeMode.rootDir);\n lastEditSnapshots.current = null;\n return formatUndoResults(results);\n }, [codeMode]);\n\n /**\n * /apply callback — write pending edit blocks to disk, snapshot\n * beforehand so /undo still works, report per-file results.\n */\n const codeApply = useCallback((): string => {\n if (!codeMode) return \"not in code mode\";\n const blocks = pendingEdits.current;\n if (blocks.length === 0) {\n return \"nothing pending — the assistant hasn't proposed edits since the last /apply or /discard.\";\n }\n const snaps = snapshotBeforeEdits(blocks, codeMode.rootDir);\n const results = applyEditBlocks(blocks, codeMode.rootDir);\n const anyApplied = results.some((r) => r.status === \"applied\" || r.status === \"created\");\n if (anyApplied) lastEditSnapshots.current = snaps;\n pendingEdits.current = [];\n return formatEditResults(results);\n }, [codeMode]);\n\n /**\n * /discard callback — forget the pending edits without touching\n * disk. Keeps the conversation going without the user having to\n * argue the model out of its proposal.\n */\n const codeDiscard = useCallback((): string => {\n const count = pendingEdits.current.length;\n if (count === 0) return \"nothing pending to discard.\";\n pendingEdits.current = [];\n return `▸ discarded ${count} pending edit block(s). Nothing was written to disk.`;\n }, []);\n\n const prefixHash = loop.prefix.fingerprint;\n\n const writeTranscript = useCallback(\n (ev: LoopEvent) => {\n const stream = transcriptRef.current;\n if (!stream) return;\n writeRecord(stream, recordFromLoopEvent(ev, { model, prefixHash }));\n },\n [model, prefixHash],\n );\n\n /**\n * Toggle plan mode on the local state AND on the ToolRegistry. The\n * registry's copy is what actually gates dispatch; the local state\n * drives the StatsPanel indicator and slash ergonomics. Kept in sync\n * by funneling every toggle through this setter.\n */\n const togglePlanMode = useCallback(\n (on: boolean) => {\n setPlanMode(on);\n tools?.setPlanMode(on);\n },\n [tools],\n );\n\n /** Clear the pending-plan picker state; safe to call unconditionally. */\n const clearPendingPlan = useCallback(() => {\n setPendingPlan(null);\n }, []);\n\n const handleSubmit = useCallback(\n async (raw: string) => {\n let text = raw.trim();\n if (!text || busy) return;\n\n // Slash auto-complete on Enter. When the user typed a prefix\n // (e.g. \"/he\") and the suggestion list is visible, substitute\n // the highlighted match so Enter runs it — same effect as Tab\n // + Enter, one keystroke less. Skip substitution if the user\n // already typed a full, exact command name (respect verbatim\n // input when they know what they want).\n if (text.startsWith(\"/\") && !text.includes(\" \")) {\n const typed = text.slice(1).toLowerCase();\n const matches = suggestSlashCommands(typed, !!codeMode);\n const exact = matches.find((m) => m.cmd === typed);\n if (!exact && matches.length > 0) {\n const chosen = matches[slashSelected] ?? matches[0];\n if (chosen) text = `/${chosen.cmd}`;\n }\n }\n\n setInput(\"\");\n historyCursor.current = -1;\n\n // Y/N fast-path when edits are pending. One keystroke is all it\n // takes to commit or drop — matches the muscle memory of `git\n // add -p` / most prompts. Deliberately scoped: only when there\n // ARE pending edits, so \"y\" as a normal message still works\n // when nothing's waiting.\n if (codeMode && pendingEdits.current.length > 0 && (text === \"y\" || text === \"n\")) {\n const out = text === \"y\" ? codeApply() : codeDiscard();\n setHistorical((prev) => [...prev, { id: `sys-${Date.now()}`, role: \"info\", text: out }]);\n promptHistory.current.push(text);\n return;\n }\n\n const slash = parseSlash(text);\n if (slash) {\n const result = handleSlash(slash.cmd, slash.args, loop, {\n mcpSpecs,\n mcpServers,\n codeUndo: codeMode ? codeUndo : undefined,\n codeApply: codeMode ? codeApply : undefined,\n codeDiscard: codeMode ? codeDiscard : undefined,\n codeRoot: codeMode?.rootDir,\n pendingEditCount: codeMode ? pendingEdits.current.length : undefined,\n toolHistory: () => toolHistoryRef.current,\n memoryRoot: codeMode?.rootDir ?? process.cwd(),\n planMode,\n setPlanMode: codeMode ? togglePlanMode : undefined,\n clearPendingPlan: codeMode ? clearPendingPlan : undefined,\n reloadHooks: () => {\n const fresh = loadHooks({ projectRoot: codeMode?.rootDir });\n setHookList(fresh);\n return fresh.length;\n },\n latestVersion,\n refreshLatestVersion: () => {\n void (async () => {\n const fresh = await getLatestVersion({ force: true });\n if (fresh) setLatestVersion(fresh);\n })();\n },\n });\n if (result.exit) {\n transcriptRef.current?.end();\n exit();\n return;\n }\n if (result.clear && result.info) {\n // Clear + message: wipe scrollback, then seed the new view\n // with the explanatory info line so the user sees *what\n // happened*. Previously clear alone left them staring at\n // an empty screen with no confirmation.\n setHistorical([\n {\n id: `sys-${Date.now()}`,\n role: \"info\",\n text: result.info,\n },\n ]);\n return;\n }\n if (result.clear) {\n setHistorical([]);\n return;\n }\n if (result.info) {\n setHistorical((prev) => [\n ...prev,\n {\n id: `sys-${Date.now()}`,\n role: \"info\",\n text: result.info!,\n },\n ]);\n }\n // `/retry` (and anything else that requests a resubmit) falls\n // through to the normal user-message flow with the provided\n // text instead of returning.\n if (result.resubmit) {\n text = result.resubmit;\n } else {\n promptHistory.current.push(text);\n return;\n }\n }\n\n // UserPromptSubmit hooks. Exit code 2 from any matching hook\n // drops the message entirely (the user's text never reaches\n // the model). Other non-zero exits surface as warning rows but\n // the prompt still goes through. We render every non-pass\n // outcome's stderr inline so a \"blocked\" choice has a visible\n // explanation.\n if (hookList.some((h) => h.event === \"UserPromptSubmit\")) {\n const promptReport = await runHooks({\n hooks: hookList,\n payload: { event: \"UserPromptSubmit\", cwd: hookCwd, prompt: text },\n });\n if (promptReport.outcomes.length > 0) {\n setHistorical((prev) => [\n ...prev,\n ...promptReport.outcomes\n .filter((o) => o.decision !== \"pass\")\n .map((o) => ({\n id: `hp-${Date.now()}-${Math.random()}`,\n role: \"warning\" as const,\n text: formatHookOutcomeMessage(o),\n })),\n ]);\n }\n if (promptReport.blocked) return;\n }\n\n // User message is immutable — push to Static immediately.\n promptHistory.current.push(text);\n setHistorical((prev) => [...prev, { id: `u-${Date.now()}`, role: \"user\", text }]);\n\n const assistantId = `a-${Date.now()}`;\n // Refs are the source of truth for accumulated streaming text; the React\n // state copy below is only for rendering and gets updated on flush.\n const streamRef: StreamingState = { id: assistantId, text: \"\", reasoning: \"\" };\n const contentBuf = { current: \"\" };\n const reasoningBuf = { current: \"\" };\n // Coalesces tool_call_delta events into one re-render per flush tick.\n const toolCallBuildBuf: { current: { name: string; chars: number } | null } = {\n current: null,\n };\n\n setStreaming({ id: assistantId, role: \"assistant\", text: \"\", streaming: true });\n setBusy(true);\n abortedThisTurn.current = false;\n\n const flush = () => {\n if (!contentBuf.current && !reasoningBuf.current && !toolCallBuildBuf.current) return;\n streamRef.text += contentBuf.current;\n streamRef.reasoning += reasoningBuf.current;\n if (toolCallBuildBuf.current) {\n streamRef.toolCallBuild = toolCallBuildBuf.current;\n }\n contentBuf.current = \"\";\n reasoningBuf.current = \"\";\n toolCallBuildBuf.current = null;\n setStreaming({\n id: assistantId,\n role: \"assistant\",\n text: streamRef.text,\n reasoning: streamRef.reasoning || undefined,\n toolCallBuild: streamRef.toolCallBuild,\n streaming: true,\n });\n };\n // In PLAIN mode the streaming row is suppressed, so flushing into\n // streamRef does no visible work — skip the interval entirely.\n const timer = PLAIN_UI ? null : setInterval(flush, FLUSH_INTERVAL_MS);\n\n try {\n for await (const ev of loop.step(text)) {\n writeTranscript(ev);\n // Status lines are transient — any primary event (streaming\n // starts, a tool fires, etc.) means whatever we were waiting\n // FOR has now arrived, so drop the hint. We do this uniformly\n // at the top of the loop body for every role except \"status\"\n // itself (which SETS the line).\n if (ev.role !== \"status\") {\n setStatusLine((cur) => (cur ? null : cur));\n }\n if (ev.role === \"status\") {\n setStatusLine(ev.content);\n } else if (ev.role === \"assistant_delta\") {\n if (ev.content) contentBuf.current += ev.content;\n if (ev.reasoningDelta) reasoningBuf.current += ev.reasoningDelta;\n } else if (ev.role === \"tool_call_delta\") {\n if (ev.toolName) {\n toolCallBuildBuf.current = {\n name: ev.toolName,\n chars: ev.toolCallArgsChars ?? 0,\n };\n }\n } else if (ev.role === \"branch_start\") {\n setStreaming({\n id: assistantId,\n role: \"assistant\",\n text: \"\",\n streaming: true,\n branchProgress: ev.branchProgress,\n });\n } else if (ev.role === \"branch_progress\") {\n // Live-update the streaming slot with per-sample completion info.\n setStreaming({\n id: assistantId,\n role: \"assistant\",\n text: \"\",\n streaming: true,\n branchProgress: ev.branchProgress,\n });\n } else if (ev.role === \"branch_done\") {\n // Intermediate: branching finished but assistant_final not yet emitted.\n // Keep streaming state alive; actual render happens on assistant_final.\n } else if (ev.role === \"assistant_final\") {\n flush();\n const repairNote = ev.repair ? describeRepair(ev.repair) : \"\";\n setStreaming(null);\n // Update the live stats panel every assistant_final — this is\n // where the loop already recorded per-iter usage. Without\n // this, cost/ctx/cache/hit stay at the PRIOR turn's numbers\n // until the whole step resolves, which is especially\n // confusing in multi-iter tool-call chains.\n setSummary(loop.stats.summary());\n const finalText = ev.content || streamRef.text;\n const iterReasoning = streamRef.reasoning || undefined;\n const iterId = `${assistantId}-i${assistantIterCounter.current++}`;\n setHistorical((prev) => [\n ...prev,\n {\n id: iterId,\n role: \"assistant\",\n text: finalText,\n reasoning: iterReasoning,\n planState: ev.planState,\n branch: ev.branch,\n stats: ev.stats,\n repair: repairNote || undefined,\n streaming: false,\n },\n ]);\n // streamRef is scoped to the whole handleSubmit call but each\n // iteration's deltas must not bleed into the next.\n streamRef.text = \"\";\n streamRef.reasoning = \"\";\n streamRef.toolCallBuild = undefined;\n contentBuf.current = \"\";\n reasoningBuf.current = \"\";\n toolCallBuildBuf.current = null;\n if (codeMode && finalText && !ev.forcedSummary) {\n // Parse SEARCH/REPLACE blocks but DO NOT write them to\n // disk. Store as pending — the user has to say /apply\n // explicitly. This prevents \"analyze the project\" from\n // silently drifting into \"edit the project\".\n //\n // `ev.forcedSummary` gates us out entirely: if the loop\n // had to force a summary (budget / aborted / context-\n // guard), its text is a wrap-up, not a plan to execute.\n // Blocks dropped in a forced summary are display-only.\n const blocks = parseEditBlocks(finalText);\n if (blocks.length > 0) {\n pendingEdits.current = blocks;\n setHistorical((prev) => [\n ...prev,\n {\n id: `pending-${Date.now()}`,\n role: \"info\",\n text: formatPendingPreview(blocks),\n },\n ]);\n }\n }\n } else if (ev.role === \"tool_start\") {\n // Kick off the visual indicator. Cleared when `tool`\n // (result) or `error` arrives, or on the finally below.\n // Also reset any lingering progress from a prior call so\n // the new spinner starts clean.\n setOngoingTool({ name: ev.toolName ?? \"?\", args: ev.toolArgs });\n setToolProgress(null);\n } else if (ev.role === \"tool\") {\n flush();\n setOngoingTool(null);\n setToolProgress(null);\n toolHistoryRef.current.push({\n toolName: ev.toolName ?? \"?\",\n text: ev.content,\n });\n setHistorical((prev) => [\n ...prev,\n {\n id: `t-${Date.now()}-${Math.random()}`,\n role: \"tool\",\n text: ev.content,\n toolName: ev.toolName,\n },\n ]);\n // run_command rejected because the command isn't on the\n // auto-allow list. Stash it so the y/n fast-path can run\n // it after user confirmation. Only the latest such request\n // is tracked — a second rejection overwrites the first.\n if (\n codeMode &&\n ev.toolName === \"run_command\" &&\n ev.content.includes('\"NeedsConfirmationError:') &&\n ev.toolArgs\n ) {\n try {\n const parsed = JSON.parse(ev.toolArgs) as { command?: unknown };\n if (typeof parsed.command === \"string\" && parsed.command.trim()) {\n setPendingShell(parsed.command.trim());\n }\n } catch {\n /* malformed args — skip the prompt */\n }\n }\n // submit_plan fired while plan mode was on — the registry\n // serialized `{ error, plan }` via PlanProposedError's\n // toToolResult(). Extract the plan and mount PlanConfirm.\n // Only the latest submission is tracked; a second overrides.\n if (\n codeMode &&\n ev.toolName === \"submit_plan\" &&\n ev.content.includes('\"PlanProposedError:')\n ) {\n try {\n const parsed = JSON.parse(ev.content) as { plan?: unknown };\n if (typeof parsed.plan === \"string\" && parsed.plan.trim()) {\n setPendingPlan(parsed.plan.trim());\n }\n } catch {\n /* malformed payload — skip the picker */\n }\n }\n } else if (ev.role === \"error\") {\n setHistorical((prev) => [\n ...prev,\n { id: `e-${Date.now()}`, role: \"error\", text: ev.error ?? ev.content },\n ]);\n } else if (ev.role === \"warning\") {\n setHistorical((prev) => [\n ...prev,\n { id: `w-${Date.now()}-${Math.random()}`, role: \"warning\", text: ev.content },\n ]);\n }\n }\n flush();\n\n // Stop hooks — turn has ended (or aborted). Block decisions are\n // meaningless past this point so we treat every non-pass as a\n // warning. Natural place for \"after every turn, run the\n // formatter / lint / tests\" automation.\n if (hookList.some((h) => h.event === \"Stop\")) {\n const stopReport = await runHooks({\n hooks: hookList,\n payload: {\n event: \"Stop\",\n cwd: hookCwd,\n lastAssistantText: streamRef.text,\n turn: loop.stats.summary().turns,\n },\n });\n for (const o of stopReport.outcomes) {\n if (o.decision === \"pass\") continue;\n setHistorical((prev) => [\n ...prev,\n {\n id: `hs-${Date.now()}-${Math.random()}`,\n role: \"warning\",\n text: formatHookOutcomeMessage(o),\n },\n ]);\n }\n }\n } finally {\n if (timer) clearInterval(timer);\n setStreaming(null);\n setOngoingTool(null);\n setToolProgress(null);\n setStatusLine(null);\n setSummary(loop.stats.summary());\n setBusy(false);\n // Refresh balance lazily — don't block the return.\n void (async () => {\n const bal = await loop.client.getBalance().catch(() => null);\n if (bal?.balance_infos.length) {\n const p = bal.balance_infos[0]!;\n setBalance({ currency: p.currency, total: Number(p.total_balance) });\n }\n })();\n }\n },\n [\n busy,\n clearPendingPlan,\n codeApply,\n codeDiscard,\n codeMode,\n codeUndo,\n exit,\n hookCwd,\n hookList,\n loop,\n latestVersion,\n mcpSpecs,\n mcpServers,\n planMode,\n slashSelected,\n togglePlanMode,\n writeTranscript,\n ],\n );\n\n /**\n * ShellConfirm callback. Three outcomes, all of them ending with a\n * synthetic user message fed back into the loop so the model sees\n * what happened next turn:\n * - deny → \"I denied running X.\" and we move on.\n * - run_once / always_allow → run the command inside the sandbox\n * root, attach the formatted output as the user turn. In the\n * always_allow case we also persist the derived prefix to\n * config so next invocation auto-runs.\n */\n const handleShellConfirm = useCallback(\n async (choice: ShellConfirmChoice) => {\n const cmd = pendingShell;\n if (!cmd || !codeMode) return;\n setPendingShell(null);\n\n let synthetic: string;\n if (choice === \"deny\") {\n setHistorical((prev) => [\n ...prev,\n { id: `sh-deny-${Date.now()}`, role: \"info\", text: `▸ denied: ${cmd}` },\n ]);\n synthetic = `I denied running \\`${cmd}\\`. Please continue without running it.`;\n } else {\n if (choice === \"always_allow\") {\n const prefix = derivePrefix(cmd);\n addProjectShellAllowed(codeMode.rootDir, prefix);\n setHistorical((prev) => [\n ...prev,\n {\n id: `sh-allow-${Date.now()}`,\n role: \"info\",\n text: `▸ always allowed \"${prefix}\" for ${codeMode.rootDir}`,\n },\n ]);\n }\n setHistorical((prev) => [\n ...prev,\n { id: `sh-run-${Date.now()}`, role: \"info\", text: `▸ running: ${cmd}` },\n ]);\n let body: string;\n try {\n const res = await runCommand(cmd, { cwd: codeMode.rootDir });\n body = formatCommandResult(cmd, res);\n } catch (err) {\n body = `$ ${cmd}\\n[failed to spawn] ${(err as Error).message}`;\n }\n setHistorical((prev) => [\n ...prev,\n { id: `sh-out-${Date.now()}`, role: \"info\", text: body },\n ]);\n synthetic = `I ran the command you requested. Output:\\n\\n${body}`;\n }\n\n // If the prior turn is still streaming (\"please confirm\" chatter),\n // handleSubmit would early-return on busy=true. Abort the in-flight\n // turn and queue the synthetic for the effect below, which fires\n // once busy clears. Otherwise submit directly.\n if (busy) {\n loop.abort();\n setQueuedSubmit(synthetic);\n } else {\n await handleSubmit(synthetic);\n }\n },\n [pendingShell, codeMode, handleSubmit, busy, loop],\n );\n\n // Drain the shell-confirm queue after the in-flight turn tears down.\n // React closure staleness means handleShellConfirm can't just await\n // the abort itself — this effect is the reliable edge detector.\n useEffect(() => {\n if (!busy && queuedSubmit !== null) {\n const text = queuedSubmit;\n setQueuedSubmit(null);\n void handleSubmit(text);\n }\n }, [busy, queuedSubmit, handleSubmit]);\n\n /**\n * PlanConfirm callback. Three outcomes, all ending with a synthetic\n * user message so the model sees the verdict on its next turn:\n * - approve → exit plan mode, tell the model to implement now.\n * - refine → stay in plan mode, tell the model to revise.\n * - cancel → exit plan mode, tell the model to drop the plan.\n * Mirrors handleShellConfirm's busy-queue dance — if the turn is\n * still streaming \"plan submitted, waiting\" chatter when the user\n * picks, we abort it and queue the synthetic for the effect above.\n *\n * `approve` is also callable with no pending plan (via the\n * `/apply-plan` slash fallback, used when the model wrote a plan in\n * assistant text instead of calling submit_plan). In that case we\n * just flip plan mode off and push the implement-now message.\n */\n const handlePlanConfirm = useCallback(\n async (choice: PlanConfirmChoice) => {\n const hadPendingPlan = pendingPlan !== null;\n if (!hadPendingPlan && choice !== \"approve\") {\n // Refine / Cancel without a pending plan is a no-op; only the\n // /apply-plan fallback makes sense without one.\n return;\n }\n\n if (choice === \"refine\" || choice === \"approve\") {\n // Two-step: stash the plan + the intent, show the input, wait\n // for user feedback before pushing anything. Approve collects\n // \"last instructions / answers to open questions\" (blank is\n // fine, user can just hit Enter). Refine collects required\n // feedback so the model has concrete guidance to revise.\n if (pendingPlan) {\n setStagedInput({ plan: pendingPlan, mode: choice });\n setPendingPlan(null);\n } else if (choice === \"approve\") {\n // /apply-plan fallback path — no pending plan, just approve.\n setStagedInput({ plan: \"\", mode: \"approve\" });\n }\n return;\n }\n\n // Cancel — no input needed, fire immediately.\n setPendingPlan(null);\n togglePlanMode(false);\n const marker = \"▸ plan cancelled\";\n const synthetic =\n \"The plan was cancelled. Drop it entirely. Ask me what I actually want before proposing another plan or making any changes.\";\n setHistorical((prev) => [\n ...prev,\n { id: `plan-${choice}-${Date.now()}`, role: \"info\", text: marker },\n ]);\n if (busy) {\n loop.abort();\n setQueuedSubmit(synthetic);\n } else {\n await handleSubmit(synthetic);\n }\n },\n [pendingPlan, togglePlanMode, busy, loop, handleSubmit],\n );\n\n /**\n * Fired when the user submits feedback from the inline input. The\n * staged `mode` decides whether this is a refine or approve: refine\n * stays in plan mode and asks the model to revise; approve exits\n * plan mode and pushes the implement synthetic, with any user\n * guidance (answers to open questions, last-minute preferences)\n * included verbatim.\n */\n const handleStagedInputSubmit = useCallback(\n async (feedback: string) => {\n const staged = stagedInput;\n setStagedInput(null);\n if (!staged) return;\n const trimmed = feedback.trim();\n\n let synthetic: string;\n let marker: string;\n if (staged.mode === \"approve\") {\n togglePlanMode(false);\n if (trimmed) {\n synthetic = `The plan above has been approved. Implement it now. You are out of plan mode — use edit_file / write_file / run_command as needed.\\n\\nUser's additional instructions / answers to your open questions:\\n\\n${trimmed}\\n\\nFactor these in before the first edit. Stick to the plan unless you discover a concrete reason to deviate; if you do, tell me and wait for a response.`;\n marker = `▸ plan approved + instructions — ${trimmed.length > 50 ? `${trimmed.slice(0, 50)}…` : trimmed}`;\n } else {\n synthetic =\n \"The plan above has been approved. Implement it now. You are out of plan mode — use edit_file / write_file / run_command as needed. If the plan listed open questions and I didn't answer them, default to the safest interpretation and call them out in your first reply. Don't fabricate preferences — if a question is truly unanswerable without me, stop and ask.\";\n marker = \"▸ plan approved — implementing\";\n }\n } else {\n // refine\n if (trimmed) {\n synthetic = `The plan needs refinement. User feedback / answers:\\n\\n${trimmed}\\n\\nStay in plan mode — address the feedback (explore more if needed), then submit an improved submit_plan call. Don't propose a near-identical plan unless you explain why the feedback doesn't apply.`;\n marker = `▸ refining — ${trimmed.length > 50 ? `${trimmed.slice(0, 50)}…` : trimmed}`;\n } else {\n synthetic =\n \"The plan needs refinement, but the user didn't give specifics. Ask them one or two concrete questions — scope, approach, file boundaries, or the risks you flagged — then wait for their answer before submitting an updated plan.\";\n marker = \"▸ refining — asking the model to clarify\";\n }\n }\n\n setHistorical((prev) => [\n ...prev,\n { id: `plan-${staged.mode}-${Date.now()}`, role: \"info\", text: marker },\n ]);\n if (busy) {\n loop.abort();\n setQueuedSubmit(synthetic);\n } else {\n await handleSubmit(synthetic);\n }\n },\n [stagedInput, togglePlanMode, busy, loop, handleSubmit],\n );\n\n /** Esc on the inline input — restore the picker without resuming. */\n const handleStagedInputCancel = useCallback(() => {\n if (stagedInput?.plan) setPendingPlan(stagedInput.plan);\n setStagedInput(null);\n }, [stagedInput]);\n\n return (\n <TickerProvider disabled={PLAIN_UI}>\n <Box flexDirection=\"column\">\n <StatsPanel\n summary={summary}\n model={loop.model}\n prefixHash={prefixHash}\n harvestOn={loop.harvestEnabled}\n branchBudget={loop.branchOptions.budget}\n planMode={planMode}\n balance={balance}\n updateAvailable={updateAvailable}\n />\n <Static items={historical}>{(item) => <EventRow key={item.id} event={item} />}</Static>\n {/*\n Live rows are hidden while the ShellConfirm modal is up — the\n model's concurrent \"please confirm\" stream is noise the user\n doesn't need, and the picker shouldn't fight it for visual\n attention. They come back naturally once the user chooses and\n the next turn begins.\n */}\n {!PLAIN_UI && !pendingShell && !pendingPlan && !stagedInput && streaming ? (\n <Box marginY={1}>\n <EventRow event={streaming} />\n </Box>\n ) : null}\n {!PLAIN_UI && !pendingShell && !pendingPlan && !stagedInput && ongoingTool ? (\n <OngoingToolRow tool={ongoingTool} progress={toolProgress} />\n ) : null}\n {!PLAIN_UI &&\n !pendingShell &&\n !pendingPlan &&\n !stagedInput &&\n !ongoingTool &&\n statusLine ? (\n <StatusRow text={statusLine} />\n ) : null}\n {/*\n Belt-and-suspenders fallback: if we're busy but NONE of the\n specific indicators (streaming, ongoingTool, statusLine) is\n visible, something is still happening — show a generic\n \"processing…\" so the user never stares at a silent ticker\n without a label. Catches micro-gaps between events that the\n targeted status lines don't cover.\n */}\n {!PLAIN_UI &&\n !pendingShell &&\n !pendingPlan &&\n !stagedInput &&\n busy &&\n !streaming &&\n !ongoingTool &&\n !statusLine ? (\n <StatusRow text=\"processing…\" />\n ) : null}\n {stagedInput ? (\n <PlanRefineInput\n mode={stagedInput.mode}\n onSubmit={handleStagedInputSubmit}\n onCancel={handleStagedInputCancel}\n />\n ) : pendingPlan ? (\n <PlanConfirm plan={pendingPlan} onChoose={handlePlanConfirm} />\n ) : pendingShell ? (\n <ShellConfirm\n command={pendingShell}\n allowPrefix={derivePrefix(pendingShell)}\n onChoose={handleShellConfirm}\n />\n ) : (\n <>\n <PromptInput\n value={input}\n onChange={setInput}\n onSubmit={handleSubmit}\n disabled={busy}\n />\n <SlashSuggestions matches={slashMatches} selectedIndex={slashSelected} />\n </>\n )}\n </Box>\n </TickerProvider>\n );\n}\n\n/**\n * Live spinner row while a tool call is in flight. Without this the\n * window between the model's `tool_calls` decision and the tool's\n * result (often seconds for a multi-KB `filesystem_edit_file`) looks\n * like the app has frozen — the streaming assistant display is\n * already cleared and the input is disabled, so there's nothing to\n * look at.\n *\n * We show three signals: a braille spinner (liveness), an elapsed\n * timer in seconds (so \"long\" has a number attached), and a\n * per-tool summary of the most informative argument fields (path,\n * edits count, pattern, etc.). As of 0.4.8, MCP progress frames\n * (`notifications/progress`) land here too — bar + \"n/N\" when the\n * server reports a total, free-form message when not.\n */\n/**\n * Transient \"what's happening now\" row shown during silent phases\n * between the primary events — harvest round-trip, R1 thinking\n * about a tool result before the next streaming delta, forced\n * summary. Matches OngoingToolRow's visual language (braille\n * spinner + elapsed seconds) so the user's eyes track the same\n * spot regardless of which kind of wait it is.\n */\nconst SPINNER_FRAMES = [\"⠋\", \"⠙\", \"⠹\", \"⠸\", \"⠼\", \"⠴\", \"⠦\", \"⠧\", \"⠇\", \"⠏\"];\n\nfunction StatusRow({ text }: { text: string }) {\n const tick = useTick();\n const elapsed = useElapsedSeconds();\n return (\n <Box marginY={1}>\n <Text color=\"magenta\">{SPINNER_FRAMES[tick % SPINNER_FRAMES.length]}</Text>\n <Text color=\"magenta\">{` ${text}`}</Text>\n <Text dimColor>{` ${elapsed}s`}</Text>\n </Box>\n );\n}\n\nfunction OngoingToolRow({\n tool,\n progress,\n}: {\n tool: { name: string; args?: string };\n progress: { progress: number; total?: number; message?: string } | null;\n}) {\n const tick = useTick();\n const elapsed = useElapsedSeconds();\n const summary = summarizeToolArgs(tool.name, tool.args);\n return (\n <Box marginY={1} flexDirection=\"column\">\n <Box>\n <Text color=\"cyan\">{SPINNER_FRAMES[tick % SPINNER_FRAMES.length]}</Text>\n <Text color=\"yellow\">{` tool<${tool.name}> running…`}</Text>\n <Text dimColor>{` ${elapsed}s`}</Text>\n </Box>\n {progress ? (\n <Box paddingLeft={2}>\n <Text color=\"cyan\">{renderProgressLine(progress)}</Text>\n </Box>\n ) : null}\n {summary ? (\n <Box paddingLeft={2}>\n <Text dimColor>{summary}</Text>\n </Box>\n ) : null}\n </Box>\n );\n}\n\n/**\n * Turn raw JSON tool arguments into a one-line human summary. For\n * common filesystem MCP tools we pull the actually-useful fields; for\n * anything else we fall back to a truncated raw string so the user\n * still sees *something* beyond the tool name.\n *\n * Match on suffix (e.g. `_read_file`) rather than exact name because\n * `bridgeMcpTools({ namePrefix: \"filesystem_\" })` prepends the server\n * namespace — tools arrive as `filesystem_read_file` in practice but\n * callers might wire up anonymous too.\n */\n/**\n * Render an MCP progress frame as a single line. When the server\n * reports `total`, show an ASCII progress bar + \"n/total pct%\";\n * otherwise just \"progress\" + optional message. Width is modest\n * so the line fits even in a narrow terminal.\n */\nfunction renderProgressLine(p: { progress: number; total?: number; message?: string }): string {\n const msg = p.message ? ` ${p.message}` : \"\";\n if (p.total && p.total > 0) {\n const ratio = Math.max(0, Math.min(1, p.progress / p.total));\n const width = 20;\n const filled = Math.round(ratio * width);\n const bar = \"█\".repeat(filled) + \"░\".repeat(width - filled);\n const pct = (ratio * 100).toFixed(0);\n return `[${bar}] ${p.progress}/${p.total} ${pct}%${msg}`;\n }\n return `progress: ${p.progress}${msg}`;\n}\n\nfunction summarizeToolArgs(name: string, args?: string): string {\n if (!args || args === \"{}\") return \"\";\n let parsed: Record<string, unknown>;\n try {\n parsed = JSON.parse(args) as Record<string, unknown>;\n } catch {\n // Unparseable JSON — show a head slice so user at least sees\n // what the model tried.\n return args.length > 80 ? `${args.slice(0, 80)}…` : args;\n }\n const hasSuffix = (s: string) => name === s || name.endsWith(`_${s}`);\n const path = typeof parsed.path === \"string\" ? parsed.path : undefined;\n if (hasSuffix(\"read_file\")) {\n const head = typeof parsed.head === \"number\" ? `, head=${parsed.head}` : \"\";\n const tail = typeof parsed.tail === \"number\" ? `, tail=${parsed.tail}` : \"\";\n return `path: ${path ?? \"?\"}${head}${tail}`;\n }\n if (hasSuffix(\"write_file\")) {\n const content = typeof parsed.content === \"string\" ? parsed.content : \"\";\n return `path: ${path ?? \"?\"} (${content.length} chars)`;\n }\n if (hasSuffix(\"edit_file\")) {\n const edits = Array.isArray(parsed.edits) ? parsed.edits.length : 0;\n return `path: ${path ?? \"?\"} (${edits} edit${edits === 1 ? \"\" : \"s\"})`;\n }\n if (hasSuffix(\"list_directory\") || hasSuffix(\"directory_tree\")) {\n return `path: ${path ?? \"?\"}`;\n }\n if (hasSuffix(\"search_files\")) {\n const pattern = typeof parsed.pattern === \"string\" ? parsed.pattern : \"?\";\n return `path: ${path ?? \"?\"} · pattern: ${pattern}`;\n }\n if (hasSuffix(\"move_file\")) {\n const src = typeof parsed.source === \"string\" ? parsed.source : \"?\";\n const dst = typeof parsed.destination === \"string\" ? parsed.destination : \"?\";\n return `${src} → ${dst}`;\n }\n if (hasSuffix(\"get_file_info\")) {\n return `path: ${path ?? \"?\"}`;\n }\n return args.length > 80 ? `${args.slice(0, 80)}…` : args;\n}\n\n/**\n * Render a batch of SEARCH/REPLACE application results as one\n * human-scannable info line per edit. Prefixes denote status so the\n * line reads well even without color (e.g. when piped to a log file\n * or stripped for screenshots):\n * ✓ applied src/foo.ts\n * ✓ created src/new.ts\n * ✗ not-found src/bar.ts (SEARCH text does not match…)\n */\nfunction formatEditResults(results: ApplyResult[]): string {\n const lines = results.map((r) => {\n const mark = r.status === \"applied\" || r.status === \"created\" ? \"✓\" : \"✗\";\n const detail = r.message ? ` (${r.message})` : \"\";\n return ` ${mark} ${r.status.padEnd(11)} ${r.path}${detail}`;\n });\n const ok = results.filter((r) => r.status === \"applied\" || r.status === \"created\").length;\n const total = results.length;\n const header = `▸ edit blocks: ${ok}/${total} applied — /undo to roll back, or \\`git diff\\` to review`;\n return [header, ...lines].join(\"\\n\");\n}\n\n/**\n * Pending-edits preview shown after each assistant turn that proposed\n * changes. We keep it deliberately thin — path + approximate\n * ±line-count — because a full diff would crowd the TUI and the user\n * can always run `git diff` after /apply.\n */\nfunction formatPendingPreview(blocks: EditBlock[]): string {\n const lines = blocks.map((b) => {\n const removed = b.search === \"\" ? 0 : countLines(b.search);\n const added = countLines(b.replace);\n const tag = b.search === \"\" ? \"NEW \" : \" \";\n return ` ${tag}${b.path} (-${removed} +${added} lines)`;\n });\n const header = `▸ ${blocks.length} pending edit block(s) — /apply (or y) to commit · /discard (or n) to drop`;\n return [header, ...lines].join(\"\\n\");\n}\n\nfunction countLines(s: string): number {\n if (s.length === 0) return 0;\n return (s.match(/\\n/g)?.length ?? 0) + 1;\n}\n\nfunction formatUndoResults(results: ApplyResult[]): string {\n const lines = results.map((r) => {\n const mark = r.status === \"applied\" ? \"✓\" : \"✗\";\n const detail = r.message ? ` (${r.message})` : \"\";\n return ` ${mark} ${r.path}${detail}`;\n });\n return [`▸ undo: restored ${results.length} file(s) to pre-edit state`, ...lines].join(\"\\n\");\n}\n\nfunction describeRepair(repair: {\n scavenged: number;\n truncationsFixed: number;\n stormsBroken: number;\n}): string {\n const parts: string[] = [];\n if (repair.scavenged) parts.push(`scavenged ${repair.scavenged}`);\n if (repair.truncationsFixed) parts.push(`repaired ${repair.truncationsFixed} truncation`);\n if (repair.stormsBroken) parts.push(`broke ${repair.stormsBroken} storm`);\n return parts.length ? `[repair] ${parts.join(\", \")}` : \"\";\n}\n","import { Box, Text } from \"ink\";\nimport React from \"react\";\nimport { type TypedPlanState, isPlanStateEmpty } from \"../../harvest.js\";\nimport type { BranchProgress, BranchSummary } from \"../../loop.js\";\nimport type { TurnStats } from \"../../telemetry.js\";\nimport { PlanStateBlock } from \"./PlanStateBlock.js\";\nimport { Markdown } from \"./markdown.js\";\nimport { useElapsedSeconds, useTick } from \"./ticker.js\";\n\nexport type DisplayRole = \"user\" | \"assistant\" | \"tool\" | \"system\" | \"error\" | \"info\" | \"warning\";\n\nexport interface DisplayEvent {\n id: string;\n role: DisplayRole;\n text: string;\n reasoning?: string;\n planState?: TypedPlanState;\n branch?: BranchSummary;\n branchProgress?: BranchProgress;\n toolName?: string;\n stats?: TurnStats;\n repair?: string;\n streaming?: boolean;\n toolCallBuild?: { name: string; chars: number };\n}\n\nexport const EventRow = React.memo(function EventRow({ event }: { event: DisplayEvent }) {\n if (event.role === \"user\") {\n return (\n <Box>\n <Text bold color=\"cyan\">\n you ›{\" \"}\n </Text>\n <Text>{event.text}</Text>\n </Box>\n );\n }\n if (event.role === \"assistant\") {\n if (event.streaming) return <StreamingAssistant event={event} />;\n return (\n <Box flexDirection=\"column\" marginTop={1}>\n <Box>\n <Text bold color=\"green\">\n assistant\n </Text>\n </Box>\n {event.branch ? <BranchBlock branch={event.branch} /> : null}\n {event.reasoning ? <ReasoningBlock reasoning={event.reasoning} /> : null}\n {!isPlanStateEmpty(event.planState) ? (\n <PlanStateBlock planState={event.planState!} />\n ) : null}\n {event.text ? <Markdown text={event.text} /> : <Text dimColor>(no content)</Text>}\n {event.stats ? <StatsLine stats={event.stats} /> : null}\n {event.repair ? <Text color=\"magenta\">{event.repair}</Text> : null}\n </Box>\n );\n }\n if (event.role === \"tool\") {\n // `flattenMcpResult` prefixes server-side errors with \"ERROR: \".\n // Render those in red with a ✗ marker so they don't blend into\n // successful tool output (yellow) — the failure mode is what the\n // model most likely needs to act on next, and the user needs to\n // see at a glance.\n const isError = event.text.startsWith(\"ERROR:\");\n const color = isError ? \"red\" : \"yellow\";\n const marker = isError ? \"✗\" : \"→\";\n // `edit_file` results get a dedicated diff renderer — colored\n // line-by-line so `-` removals show red, `+` additions show\n // green, unchanged context lines dim. Always full, never\n // truncated: users need to see the whole change to trust\n // /apply. Other tools keep the 400-char clip + /tool N escape.\n const isEditFile =\n (event.toolName === \"edit_file\" || event.toolName?.endsWith(\"_edit_file\")) && !isError;\n return (\n <Box flexDirection=\"column\" marginTop={1}>\n <Text color={color}>{`tool<${event.toolName ?? \"?\"}> ${marker}`}</Text>\n {isEditFile ? (\n <EditFileDiff text={event.text} />\n ) : (\n <Text color={isError ? \"red\" : undefined} dimColor={!isError}>\n {\" \"}\n {truncate(event.text, 400)}\n </Text>\n )}\n </Box>\n );\n }\n if (event.role === \"error\") {\n return (\n <Box marginTop={1}>\n <Text color=\"red\" bold>\n error{\" \"}\n </Text>\n <Text color=\"red\">{event.text}</Text>\n </Box>\n );\n }\n if (event.role === \"info\") {\n return (\n <Box>\n <Text dimColor>{event.text}</Text>\n </Box>\n );\n }\n if (event.role === \"warning\") {\n return (\n <Box>\n <Text color=\"yellow\">▸ </Text>\n <Text color=\"yellow\">{event.text}</Text>\n </Box>\n );\n }\n return (\n <Box>\n <Text>{event.text}</Text>\n </Box>\n );\n});\n\n/**\n * Render the payload of an `edit_file` tool result with proper\n * diff coloring: first line is the header (\"edited X (A→B chars)\"),\n * subsequent lines are prefixed with `-` (removed), `+` (added), or\n * space (unchanged context). Unchanged lines render dim so the\n * changed ones pop visually. No truncation — the whole diff is\n * shown so the user can audit what landed before `/apply` or\n * `git diff` review.\n */\nfunction EditFileDiff({ text }: { text: string }) {\n const lines = text.split(/\\r?\\n/);\n // Line 0 is the status header (\"edited X (A→B chars)\"), Line 1 is\n // the unified-diff hunk header (\"@@ -N,M +N,M @@\"), the rest is\n // the colored diff body. We style the two headers distinctly so\n // the hunk marker stands out the way git-diff's cyan `@@` does in\n // most terminals.\n const [statusHeader, hunkHeader, ...body] = lines;\n return (\n <Box flexDirection=\"column\">\n <Text dimColor>{` ${statusHeader ?? \"\"}`}</Text>\n {hunkHeader !== undefined ? (\n <Text color=\"cyan\" bold>\n {hunkHeader}\n </Text>\n ) : null}\n {body.map((line, i) => {\n // Key includes the line content slice so React isn't forced\n // to treat purely-positional identity; lines in the same\n // diff don't reorder but could repeat (e.g. blank lines).\n const key = `${i}-${line.slice(0, 32)}`;\n if (line.startsWith(\"- \")) {\n return (\n <Text key={key} color=\"red\">\n {line}\n </Text>\n );\n }\n if (line.startsWith(\"+ \")) {\n return (\n <Text key={key} color=\"green\">\n {line}\n </Text>\n );\n }\n // Context line (starts with \" \") or unknown — dim.\n return (\n <Text key={key} dimColor>\n {line}\n </Text>\n );\n })}\n </Box>\n );\n}\n\nfunction BranchBlock({ branch }: { branch: BranchSummary }) {\n const per = branch.uncertainties\n .map((u, i) => {\n const marker = i === branch.chosenIndex ? \"▸\" : \" \";\n const t = (branch.temperatures[i] ?? 0).toFixed(1);\n return `${marker} #${i} T=${t} u=${u}`;\n })\n .join(\" \");\n return (\n <Box>\n <Text color=\"blue\">\n {\"🔀 branched \"}\n <Text bold>{branch.budget}</Text>\n {` samples → picked #${branch.chosenIndex} `}\n <Text dimColor>{per}</Text>\n </Text>\n </Box>\n );\n}\n\nfunction ReasoningBlock({ reasoning }: { reasoning: string }) {\n const max = 260;\n const flat = reasoning.replace(/\\s+/g, \" \").trim();\n // Show the TAIL of the reasoning rather than the head. R1 opens\n // with generic scaffolding (\"let me look at the structure...\") that\n // repeats across turns and hides the part users actually want to\n // see — the decision right before the model commits to an action.\n // Users can dump the full reasoning with `/think` if needed.\n const preview =\n flat.length <= max ? flat : `… (+${flat.length - max} earlier chars) ${flat.slice(-max)}`;\n return (\n <Box marginBottom={1}>\n <Text dimColor italic>\n {\"↳ thinking: \"}\n {preview}\n </Text>\n </Box>\n );\n}\n\n/**\n * Compact progress view rendered while a turn is still streaming. We keep\n * this to a fixed ~3-line footprint so the dynamic region never scrolls past\n * the terminal viewport and leaves artifacts in scrollback.\n */\nfunction Elapsed() {\n const s = useElapsedSeconds();\n const mm = String(Math.floor(s / 60)).padStart(2, \"0\");\n const ss = String(s % 60).padStart(2, \"0\");\n return <Text dimColor>{`${mm}:${ss}`}</Text>;\n}\n\nfunction StreamingAssistant({ event }: { event: DisplayEvent }) {\n if (event.branchProgress) {\n const p = event.branchProgress;\n // completed=0 means we've just started; no sample has finished yet.\n if (p.completed === 0) {\n return (\n <Box flexDirection=\"column\" marginTop={1}>\n <Box>\n <Text bold color=\"green\">\n assistant{\" \"}\n </Text>\n <Text color=\"blue\">\n 🔀 launching {p.total} parallel samples (R1 thinking in parallel)…{\" \"}\n </Text>\n <Elapsed />\n </Box>\n <Text dimColor>{\" \"}spread across T=0.0/0.5/1.0 · typical wait 30-90s for reasoner</Text>\n </Box>\n );\n }\n const pct = Math.round((p.completed / p.total) * 100);\n return (\n <Box flexDirection=\"column\" marginTop={1}>\n <Box>\n <Text bold color=\"green\">\n assistant{\" \"}\n </Text>\n <Text color=\"blue\">\n 🔀 branching {p.completed}/{p.total} ({pct}%){\" \"}\n </Text>\n <Elapsed />\n </Box>\n <Text dimColor>\n {\" latest #\"}\n {p.latestIndex}\n {\" T=\"}\n {p.latestTemperature.toFixed(1)}\n {\" u=\"}\n {p.latestUncertainties}\n {p.completed < p.total ? \" · waiting for other samples…\" : \" · selecting winner…\"}\n </Text>\n </Box>\n );\n }\n\n const tail = lastLine(event.text, 140);\n const reasoningTail = event.reasoning ? lastLine(event.reasoning, 120) : \"\";\n const toolCallBuild = event.toolCallBuild;\n // Four distinct phases a turn can be in — label them plainly so\n // the user doesn't have to decode \"streaming · 391 + think 4506\n // chars\" to figure out what's happening:\n // pre-first-byte: request in flight, no bytes back yet\n // reasoning-only: R1 thinking, no visible content yet\n // tool-call-only: tool_call arguments streaming, no content/reasoning bytes\n // writing: content streaming (R1 has already produced its thought)\n // both: rare — content present AND reasoning still growing\n // We can't cheaply distinguish \"reasoning still growing\" from\n // \"reasoning finished but we already saw it\", so when content is\n // present we just say \"writing response\" and surface both counts.\n const preFirstByte = !event.text && !event.reasoning && !toolCallBuild;\n const reasoningOnly = !event.text && !!event.reasoning && !toolCallBuild;\n const toolCallOnly = !event.text && !event.reasoning && !!toolCallBuild;\n let label: string;\n let labelColor: \"yellow\" | \"cyan\" | \"green\" | \"magenta\" | undefined;\n if (preFirstByte) {\n label = \"request sent · waiting for server\";\n labelColor = \"yellow\";\n } else if (reasoningOnly) {\n label = `R1 reasoning · ${event.reasoning?.length ?? 0} chars of thought`;\n labelColor = \"cyan\";\n } else if (toolCallOnly) {\n label = `assembling tool call <${toolCallBuild.name}> · ${toolCallBuild.chars} chars of arguments`;\n labelColor = \"magenta\";\n } else {\n const parts: string[] = [`writing response · ${event.text.length} chars`];\n if (event.reasoning) parts.push(`after ${event.reasoning.length} chars of reasoning`);\n if (toolCallBuild) {\n parts.push(`building tool call <${toolCallBuild.name}> · ${toolCallBuild.chars} chars`);\n }\n label = parts.join(\" · \");\n labelColor = \"green\";\n }\n return (\n <Box flexDirection=\"column\" marginTop={1}>\n <Box>\n <Text bold color=\"green\">\n assistant{\" \"}\n </Text>\n <Pulse />\n <Text color={labelColor}>{` ${label} `}</Text>\n <Elapsed />\n </Box>\n {reasoningTail ? (\n <Text dimColor italic>\n ↳ thinking: {reasoningTail}\n </Text>\n ) : null}\n {tail ? (\n <Text dimColor>▸ {tail}</Text>\n ) : preFirstByte ? (\n <Text dimColor italic>\n {\" connection open, first byte typically in 5-60s depending on model + load\"}\n </Text>\n ) : reasoningOnly ? (\n <Text color=\"yellow\" dimColor>\n {\n \" R1 is thinking before it speaks — body text starts when reasoning completes (typically 20-90s).\"\n }\n </Text>\n ) : toolCallOnly ? (\n <Text color=\"magenta\" dimColor>\n {\" tool-call arguments streaming — the model is about to dispatch a tool\"}\n </Text>\n ) : event.reasoning ? (\n <Text color=\"yellow\" dimColor>\n {\" R1 still reasoning — body text or tool call arrives when thinking completes\"}\n </Text>\n ) : null}\n </Box>\n );\n}\n\n/**\n * Blinking indicator so the user can tell the stream is alive even\n * when the reasoner hasn't produced body text yet. Uses the shared\n * tick (TICK_MS ≈ 120ms) scaled down 4× so the visible blink rate\n * lands around 500ms — feels like a heartbeat, not a progress bar.\n */\nfunction Pulse() {\n const tick = useTick();\n const frames = [\"⠋\", \"⠙\", \"⠹\", \"⠸\", \"⠼\", \"⠴\", \"⠦\", \"⠧\", \"⠇\", \"⠏\"];\n return <Text color=\"cyan\">{frames[Math.floor(tick / 4) % frames.length]}</Text>;\n}\n\nfunction lastLine(s: string, maxChars: number): string {\n const flat = s.replace(/\\s+/g, \" \").trim();\n if (!flat) return \"\";\n return flat.length <= maxChars ? flat : `…${flat.slice(-maxChars)}`;\n}\n\nfunction StatsLine({ stats }: { stats: TurnStats }) {\n const hit = (stats.cacheHitRatio * 100).toFixed(1);\n return (\n <Text dimColor>\n {\" ↳ cache \"}\n {hit}\n {\"% · tokens \"}\n {stats.usage.promptTokens}\n {\"→\"}\n {stats.usage.completionTokens}\n {\" · $\"}\n {stats.cost.toFixed(6)}\n </Text>\n );\n}\n\nfunction truncate(s: string, max: number): string {\n return s.length <= max ? s : `${s.slice(0, max)}… (+${s.length - max} chars)`;\n}\n","/**\n * Shared Ink block that renders a TypedPlanState. Used by the live chat\n * EventLog AND by the RecordView in replay/diff TUIs, so harvest output\n * looks identical live and on replay.\n *\n * Colors are semantic (not decorative):\n * - subgoals: cyan — structure / plan\n * - hypotheses: green — current beliefs (like assistant)\n * - uncertainties: yellow — attention required (like tool)\n * - rejected paths: red dim — ruled out (muted, like error-but-resolved)\n *\n * Only the label is colored + bold. The items themselves render in the\n * terminal's default foreground, so they stay readable on any background —\n * which is why the old single-magenta block was hard to see on dark themes.\n */\n\nimport { Box, Text } from \"ink\";\nimport React from \"react\";\nimport type { TypedPlanState } from \"../../harvest.js\";\n\ntype FieldColor = \"cyan\" | \"green\" | \"yellow\" | \"red\";\n\nexport function PlanStateBlock({ planState }: { planState: TypedPlanState }) {\n const fields: Array<[string, string[], FieldColor, boolean]> = [];\n if (planState.subgoals.length) fields.push([\"subgoals\", planState.subgoals, \"cyan\", false]);\n if (planState.hypotheses.length)\n fields.push([\"hypotheses\", planState.hypotheses, \"green\", false]);\n if (planState.uncertainties.length)\n fields.push([\"uncertainties\", planState.uncertainties, \"yellow\", false]);\n if (planState.rejectedPaths.length)\n fields.push([\"rejected\", planState.rejectedPaths, \"red\", true]);\n if (fields.length === 0) return null;\n\n return (\n <Box flexDirection=\"column\" marginBottom={1}>\n {fields.map(([label, items, color, dim]) => (\n <Text key={label}>\n <Text color={color} bold dimColor={dim}>\n {\"‹ \"}\n {label}\n </Text>\n <Text dimColor>{` (${items.length})`}</Text>\n <Text>{`: ${items.join(\" · \")}`}</Text>\n </Text>\n ))}\n </Box>\n );\n}\n","/**\n * Minimal Markdown → Ink renderer for chat output.\n *\n * Handles the subset that actually shows up in LLM answers:\n * - ATX headers (# ##)\n * - Unordered / ordered lists\n * - Fenced code blocks (```lang)\n * - Inline **bold**, *italic*, `code`\n * - Paragraphs separated by blank lines\n * - LaTeX delimiters are stripped (\\( \\), \\[ \\], \\boxed{X})\n *\n * The goal is not TeX-perfect math — it's \"stop showing raw backslashes to\n * the user.\" When the model insists on LaTeX, we strip the scaffolding and\n * show the expression verbatim; terminals don't do math fonts anyway.\n */\n\nimport { Box, Text } from \"ink\";\nimport React from \"react\";\n\nconst SUPERSCRIPT: Record<string, string> = {\n \"0\": \"⁰\",\n \"1\": \"¹\",\n \"2\": \"²\",\n \"3\": \"³\",\n \"4\": \"⁴\",\n \"5\": \"⁵\",\n \"6\": \"⁶\",\n \"7\": \"⁷\",\n \"8\": \"⁸\",\n \"9\": \"⁹\",\n \"+\": \"⁺\",\n \"-\": \"⁻\",\n n: \"ⁿ\",\n};\nconst SUBSCRIPT: Record<string, string> = {\n \"0\": \"₀\",\n \"1\": \"₁\",\n \"2\": \"₂\",\n \"3\": \"₃\",\n \"4\": \"₄\",\n \"5\": \"₅\",\n \"6\": \"₆\",\n \"7\": \"₇\",\n \"8\": \"₈\",\n \"9\": \"₉\",\n \"+\": \"₊\",\n \"-\": \"₋\",\n};\n\nfunction toSuperscript(s: string): string {\n let out = \"\";\n for (const c of s) out += SUPERSCRIPT[c] ?? c;\n return out;\n}\nfunction toSubscript(s: string): string {\n let out = \"\";\n for (const c of s) out += SUBSCRIPT[c] ?? c;\n return out;\n}\n\nexport function stripMath(s: string): string {\n return (\n s\n // Delimiters\n .replace(/\\\\\\(\\s*/g, \"\")\n .replace(/\\s*\\\\\\)/g, \"\")\n .replace(/\\\\\\[\\s*/g, \"\\n\")\n .replace(/\\s*\\\\\\]/g, \"\\n\")\n // Fractions — \\frac, \\dfrac, \\tfrac. Allow whitespace and one nesting\n // level inside braces (e.g. \\frac{\\sqrt{2}}{3}). Trim captured groups\n // so '\\frac{ a }{ b }' renders as '(a)/(b)'.\n .replace(\n /\\\\[dt]?frac\\s*\\{((?:[^{}]|\\{[^{}]*\\})+)\\}\\s*\\{((?:[^{}]|\\{[^{}]*\\})+)\\}/g,\n (_m, num: string, den: string) => `(${num.trim()})/(${den.trim()})`,\n )\n .replace(\n /\\\\binom\\s*\\{([^{}]+)\\}\\s*\\{([^{}]+)\\}/g,\n (_m, n: string, k: string) => `C(${n.trim()},${k.trim()})`,\n )\n .replace(/\\\\sqrt\\s*\\{([^{}]+)\\}/g, (_m, g: string) => `√(${g.trim()})`)\n .replace(/\\\\boxed\\s*\\{([^{}]+)\\}/g, (_m, g: string) => `【${g.trim()}】`)\n .replace(/\\\\text\\s*\\{([^{}]+)\\}/g, (_m, g: string) => g.trim())\n .replace(/\\\\overline\\s*\\{([^{}]+)\\}/g, (_m, g: string) => `${g.trim()}̄`)\n .replace(/\\\\hat\\s*\\{([^{}]+)\\}/g, (_m, g: string) => `${g.trim()}̂`)\n .replace(/\\\\vec\\s*\\{([^{}]+)\\}/g, (_m, g: string) => `→${g.trim()}`)\n // Operators & symbols\n .replace(/\\\\cdot/g, \"·\")\n .replace(/\\\\times/g, \"×\")\n .replace(/\\\\div/g, \"÷\")\n .replace(/\\\\pm/g, \"±\")\n .replace(/\\\\mp/g, \"∓\")\n .replace(/\\\\leq/g, \"≤\")\n .replace(/\\\\geq/g, \"≥\")\n .replace(/\\\\neq/g, \"≠\")\n .replace(/\\\\approx/g, \"≈\")\n .replace(/\\\\in\\b/g, \"∈\")\n .replace(/\\\\notin\\b/g, \"∉\")\n .replace(/\\\\infty/g, \"∞\")\n .replace(/\\\\sum\\b/g, \"Σ\")\n .replace(/\\\\prod\\b/g, \"Π\")\n .replace(/\\\\int\\b/g, \"∫\")\n // Greek letters\n .replace(/\\\\alpha/g, \"α\")\n .replace(/\\\\beta/g, \"β\")\n .replace(/\\\\gamma/g, \"γ\")\n .replace(/\\\\delta/g, \"δ\")\n .replace(/\\\\theta/g, \"θ\")\n .replace(/\\\\lambda/g, \"λ\")\n .replace(/\\\\mu/g, \"μ\")\n .replace(/\\\\pi/g, \"π\")\n .replace(/\\\\sigma/g, \"σ\")\n .replace(/\\\\phi/g, \"φ\")\n .replace(/\\\\omega/g, \"ω\")\n // Arrows / logic\n .replace(/\\\\implies\\b/g, \"⇒\")\n .replace(/\\\\iff\\b/g, \"⇔\")\n .replace(/\\\\to\\b/g, \"→\")\n .replace(/\\\\rightarrow/g, \"→\")\n .replace(/\\\\Rightarrow/g, \"⇒\")\n .replace(/\\\\leftarrow/g, \"←\")\n .replace(/\\\\Leftarrow/g, \"⇐\")\n .replace(/\\\\ldots/g, \"…\")\n .replace(/\\\\cdots/g, \"⋯\")\n // Spacing commands\n .replace(/\\\\quad/g, \" \")\n .replace(/\\\\qquad/g, \" \")\n .replace(/\\\\,/g, \" \")\n .replace(/\\\\;/g, \" \")\n .replace(/\\\\!/g, \"\")\n .replace(/\\\\\\\\/g, \"\\n\")\n // Superscripts / subscripts — single token or {braced group of [\\w+-]}\n .replace(/\\^\\{([\\w+-]+)\\}/g, (_m, g: string) => toSuperscript(g))\n .replace(/\\^([0-9+\\-n])/g, (_m, g: string) => toSuperscript(g))\n .replace(/_\\{([\\w+-]+)\\}/g, (_m, g: string) => toSubscript(g))\n .replace(/_([0-9+\\-])/g, (_m, g: string) => toSubscript(g))\n // Catch-all fallbacks for any LaTeX command we didn't explicitly handle.\n // Belt-and-braces: even if the model invents a new \\weirdcommand{x}{y},\n // we'd rather show '(x)/(y)' or 'x' than a raw backslash.\n .replace(/\\\\[a-zA-Z]+\\s*\\{([^{}]+)\\}\\s*\\{([^{}]+)\\}/g, \"($1)/($2)\")\n .replace(/\\\\[a-zA-Z]+\\s*\\{([^{}]+)\\}/g, \"$1\")\n .replace(/\\\\[a-zA-Z]+/g, \"\")\n // Collapse multiple whitespace introduced by the stripping above.\n .replace(/[ \\t]{2,}/g, \" \")\n );\n}\n\n/**\n * Split a single line into styled segments for bold / italic / inline\n * code.\n *\n * Triple-backtick (```…```) runs are matched BEFORE the single-backtick\n * case so a one-line code span like ```bash echo hi``` is captured\n * whole instead of the single-backtick regex greedily eating the\n * middle and leaving two stray backticks on each side (what 0.4.15\n * users saw when the model emitted ```bash …``` on the same line as\n * prose). Content may contain single backticks but not newlines —\n * multi-line fenced code is a block-level concern handled in\n * `parseBlocks`.\n */\nconst INLINE_RE =\n /(\\*\\*([^*\\n]+?)\\*\\*|```([^\\n]+?)```|`([^`\\n]+?)`|(?<![*\\w])\\*([^*\\n]+?)\\*(?!\\w))/g;\n\nfunction InlineMd({ text }: { text: string }) {\n const parts: React.ReactNode[] = [];\n let last = 0;\n let idx = 0;\n for (const m of text.matchAll(INLINE_RE)) {\n const start = m.index ?? 0;\n if (start > last) {\n parts.push(<Text key={`t${idx++}`}>{text.slice(last, start)}</Text>);\n }\n // Groups, in the order they appear in INLINE_RE:\n // m[2] = bold content (inside ** **)\n // m[3] = triple-backtick content (strip leading lang tag)\n // m[4] = single-backtick inline code\n // m[5] = italic content (inside * *)\n if (m[2] !== undefined) {\n parts.push(\n <Text key={`b${idx++}`} bold>\n {m[2]}\n </Text>,\n );\n } else if (m[3] !== undefined) {\n // One-line fenced span: ```bash echo hi``` → drop the \"bash \"\n // language tag so the user doesn't see it rendered in code color.\n const stripped = m[3].replace(/^(\\w+)\\s+/, \"\");\n parts.push(\n <Text key={`c${idx++}`} color=\"yellow\">\n {stripped}\n </Text>,\n );\n } else if (m[4] !== undefined) {\n parts.push(\n <Text key={`c${idx++}`} color=\"yellow\">\n {m[4]}\n </Text>,\n );\n } else if (m[5] !== undefined) {\n parts.push(\n <Text key={`i${idx++}`} italic>\n {m[5]}\n </Text>,\n );\n }\n last = start + m[0].length;\n }\n if (last < text.length) {\n parts.push(<Text key={`t${idx++}`}>{text.slice(last)}</Text>);\n }\n return <Text>{parts}</Text>;\n}\n\ninterface ParagraphBlock {\n kind: \"paragraph\";\n text: string;\n}\ninterface HeadingBlock {\n kind: \"heading\";\n level: number;\n text: string;\n}\ninterface BulletBlock {\n kind: \"bullet\";\n items: string[];\n ordered: boolean;\n start: number;\n}\ninterface CodeBlock {\n kind: \"code\";\n lang: string;\n text: string;\n}\ninterface HrBlock {\n kind: \"hr\";\n}\n// First-class Aider-style SEARCH/REPLACE block. We detect these at\n// parse time instead of routing them through the paragraph / inline\n// markdown path because the inline parser would otherwise eat `**`\n// inside JSDoc `/** ... *\\/` comments and `para.join(\" \")` would\n// collapse the block's newlines. Rendered as a diff so the user can\n// actually read what's about to change.\ninterface EditBlockView {\n kind: \"edit-block\";\n filename: string;\n search: string;\n replace: string;\n}\n\n/**\n * GitHub-Flavored-Markdown-ish tables. We don't do alignment flags\n * (:--- / ---:) — column-wise left-alignment is fine for a terminal\n * and the LLM rarely specifies alignment anyway. Columns grow to\n * fit the widest cell, with a hard cap so a pathological 200-char\n * cell doesn't blow past the terminal width.\n */\ninterface TableBlock {\n kind: \"table\";\n header: string[];\n rows: string[][];\n}\n\ntype Block =\n | ParagraphBlock\n | HeadingBlock\n | BulletBlock\n | CodeBlock\n | HrBlock\n | EditBlockView\n | TableBlock;\n\nexport function parseBlocks(raw: string): Block[] {\n const lines = raw.split(/\\r?\\n/);\n const out: Block[] = [];\n let para: string[] = [];\n let inCode = false;\n let codeLang = \"\";\n let codeBuf: string[] = [];\n let listBuf: BulletBlock | null = null;\n\n // Fence length of the currently-open code block so a block opened\n // with ````` closes only on `````, matching GFM. Empty when we're\n // not in code mode.\n let codeFence = \"\";\n\n const flushPara = () => {\n if (para.length) {\n out.push({ kind: \"paragraph\", text: para.join(\" \") });\n para = [];\n }\n };\n const flushList = () => {\n if (listBuf) {\n out.push(listBuf);\n listBuf = null;\n }\n };\n\n for (let i = 0; i < lines.length; i++) {\n const rawLine = lines[i]!;\n const line = rawLine.replace(/\\s+$/g, \"\");\n\n // Detect Aider-style SEARCH/REPLACE block. Matches the preceding\n // non-blank line as the filename, then `<<<<<<< SEARCH`, content,\n // `=======`, content, `>>>>>>> REPLACE`. We don't do markdown\n // inside — neither the paragraph nor inline parsers should touch\n // this content.\n if (!inCode && /^<{7} SEARCH\\s*$/.test(line)) {\n // Filename is the previous non-blank line we just pushed to para.\n // Pull it back out; if there isn't one, treat as literal text.\n const filename = para.pop()?.trim();\n if (filename) {\n flushPara();\n flushList();\n let j = i + 1;\n const searchLines: string[] = [];\n while (j < lines.length && !/^={7}\\s*$/.test(lines[j]!)) {\n searchLines.push(lines[j]!);\n j++;\n }\n const replaceLines: string[] = [];\n let k = j + 1;\n while (k < lines.length && !/^>{7} REPLACE\\s*$/.test(lines[k]!)) {\n replaceLines.push(lines[k]!);\n k++;\n }\n if (j < lines.length && k < lines.length) {\n out.push({\n kind: \"edit-block\",\n filename,\n search: searchLines.join(\"\\n\"),\n replace: replaceLines.join(\"\\n\"),\n });\n i = k;\n continue;\n }\n // Malformed — no separator or no close. Fall through: put\n // the filename back in the paragraph so we don't lose it.\n para.push(filename);\n }\n }\n\n // Fenced code block (GFM). The fence is 3+ backticks, may have up\n // to 3 leading spaces, and carries an optional language tag. A\n // closing fence must be the SAME backtick run length or longer.\n //\n // Two paths:\n // a) Fence on its own line → multi-line block, accumulate until\n // a matching close fence.\n // b) Fence opens AND closes on the same line (e.g.\n // ```bash echo hi```) → emit as a one-line code block so\n // the inline parser doesn't half-eat the backticks.\n if (!inCode) {\n const open = line.match(/^ {0,3}(`{3,})(\\w*)\\s*(.*)$/);\n if (open) {\n const fence = open[1]!;\n const lang = open[2] ?? \"\";\n const rest = open[3] ?? \"\";\n const closeOnSame = rest.match(new RegExp(`^(.*?)${fence}\\\\s*$`));\n if (closeOnSame) {\n flushPara();\n flushList();\n out.push({ kind: \"code\", lang, text: (closeOnSame[1] ?? \"\").trim() });\n continue;\n }\n flushPara();\n flushList();\n inCode = true;\n codeLang = lang;\n codeFence = fence;\n // Anything after the opening fence on the SAME line is\n // still body content (rare but legal).\n if (rest.length > 0) codeBuf.push(rest);\n continue;\n }\n } else {\n // In code mode — check for closing fence. Same indent rules as\n // opening, and the backtick run must be at least as long.\n const close = line.match(/^ {0,3}(`{3,})\\s*$/);\n if (close && close[1]!.length >= codeFence.length) {\n out.push({ kind: \"code\", lang: codeLang, text: codeBuf.join(\"\\n\") });\n codeBuf = [];\n codeLang = \"\";\n codeFence = \"\";\n inCode = false;\n continue;\n }\n codeBuf.push(rawLine);\n continue;\n }\n\n if (line.trim() === \"\") {\n flushPara();\n flushList();\n continue;\n }\n\n if (/^[-*_]{3,}\\s*$/.test(line)) {\n flushPara();\n flushList();\n out.push({ kind: \"hr\" });\n continue;\n }\n\n const hm = line.match(/^(#{1,6})\\s+(.+)$/);\n if (hm) {\n flushPara();\n flushList();\n out.push({ kind: \"heading\", level: hm[1]!.length, text: hm[2]!.trim() });\n continue;\n }\n\n // Table detection: a line with at least one `|` where the NEXT\n // line looks like a GFM separator (`| --- | --- |` or any mix of\n // pipes + dashes + colons + spaces). Both the header row and the\n // separator need to be present — a bare pipe in prose shouldn't\n // trigger the table path.\n if (line.includes(\"|\")) {\n const next = (lines[i + 1] ?? \"\").trim();\n if (/^\\|?\\s*:?-{2,}:?\\s*(\\|\\s*:?-{2,}:?\\s*)+\\|?\\s*$/.test(next)) {\n flushPara();\n flushList();\n const header = splitTableRow(line);\n const rows: string[][] = [];\n let j = i + 2; // skip header + separator\n while (j < lines.length) {\n const r = lines[j]!.replace(/\\s+$/g, \"\");\n if (r.trim() === \"\" || !r.includes(\"|\")) break;\n rows.push(splitTableRow(r));\n j++;\n }\n out.push({ kind: \"table\", header, rows });\n i = j - 1;\n continue;\n }\n }\n\n const bm = line.match(/^\\s*[-*+]\\s+(.+)$/);\n if (bm) {\n flushPara();\n if (!listBuf || listBuf.ordered) {\n flushList();\n listBuf = { kind: \"bullet\", items: [], ordered: false, start: 1 };\n }\n listBuf.items.push(bm[1]!);\n continue;\n }\n\n const om = line.match(/^\\s*(\\d+)\\.\\s+(.+)$/);\n if (om) {\n flushPara();\n if (!listBuf || !listBuf.ordered) {\n flushList();\n listBuf = { kind: \"bullet\", items: [], ordered: true, start: Number(om[1]) };\n }\n listBuf.items.push(om[2]!);\n continue;\n }\n\n flushList();\n para.push(line);\n }\n\n if (inCode && codeBuf.length) {\n out.push({ kind: \"code\", lang: codeLang, text: codeBuf.join(\"\\n\") });\n }\n flushPara();\n flushList();\n return out;\n}\n\nfunction BlockView({ block }: { block: Block }) {\n switch (block.kind) {\n case \"heading\":\n return (\n <Text bold color=\"cyan\">\n <InlineMd text={block.text} />\n </Text>\n );\n case \"paragraph\":\n return <InlineMd text={block.text} />;\n case \"bullet\":\n return (\n <Box flexDirection=\"column\">\n {block.items.map((item, i) => (\n <Box key={`${i}-${item.slice(0, 24)}`}>\n <Text color=\"cyan\">{block.ordered ? ` ${block.start + i}. ` : \" • \"}</Text>\n <InlineMd text={item} />\n </Box>\n ))}\n </Box>\n );\n case \"code\":\n return (\n <Box borderStyle=\"single\" borderColor=\"gray\" paddingX={1}>\n <Text color=\"yellow\">{block.text}</Text>\n </Box>\n );\n case \"edit-block\":\n return <EditBlockRow block={block} />;\n case \"table\":\n return <TableBlockRow block={block} />;\n case \"hr\":\n return <Text dimColor>{\"────────────────────────\"}</Text>;\n }\n}\n\n/**\n * Split one table row into trimmed cells. Leading/trailing pipes are\n * optional (both `| a | b |` and `a | b` are accepted). Pipes escaped\n * as `\\|` stay in the cell content.\n */\nfunction splitTableRow(line: string): string[] {\n // Temporarily replace escaped pipes so split() doesn't fire on them.\n const SENTINEL = \"\\u0000\";\n const masked = line.replace(/\\\\\\|/g, SENTINEL);\n const trimmed = masked.trim().replace(/^\\||\\|$/g, \"\");\n return trimmed.split(\"|\").map((c) => c.trim().replace(new RegExp(SENTINEL, \"g\"), \"|\"));\n}\n\n/**\n * Render a GFM table as an aligned grid. Column widths are the max\n * display length in that column, capped at 40 chars so one huge cell\n * doesn't wreck the layout. Header row is bold + cyan; body rows use\n * the default text color. Separator is a dim row of dashes.\n */\nfunction TableBlockRow({ block }: { block: TableBlock }) {\n const colCount = Math.max(block.header.length, ...block.rows.map((r) => r.length));\n const widths: number[] = [];\n for (let c = 0; c < colCount; c++) {\n const cellLengths = [displayWidth(block.header[c] ?? \"\")];\n for (const r of block.rows) cellLengths.push(displayWidth(r[c] ?? \"\"));\n widths.push(Math.min(40, Math.max(3, ...cellLengths)));\n }\n const pad = (s: string, w: number) => {\n const dw = displayWidth(s);\n if (dw >= w) return s;\n return s + \" \".repeat(w - dw);\n };\n const separator = widths.map((w) => \"─\".repeat(w)).join(\"─┼─\");\n return (\n <Box flexDirection=\"column\">\n <Box>\n {block.header.map((cell, ci) => (\n // biome-ignore lint/suspicious/noArrayIndexKey: table columns never reorder — derived from a static header array\n <Text key={`h-${ci}`} bold color=\"cyan\">\n {pad(cell, widths[ci] ?? 3)}\n {ci < colCount - 1 ? \" │ \" : \"\"}\n </Text>\n ))}\n </Box>\n <Text dimColor>{separator}</Text>\n {block.rows.map((row, ri) => (\n // biome-ignore lint/suspicious/noArrayIndexKey: table rows render in source order and don't reorder\n <Box key={`r-${ri}`}>\n {Array.from({ length: colCount }).map((_, ci) => (\n // biome-ignore lint/suspicious/noArrayIndexKey: same — column axis is fixed by the table shape\n <Text key={`c-${ri}-${ci}`}>\n {pad(row[ci] ?? \"\", widths[ci] ?? 3)}\n {ci < colCount - 1 ? \" │ \" : \"\"}\n </Text>\n ))}\n </Box>\n ))}\n </Box>\n );\n}\n\n/**\n * Terminal display width of a string, approximately. CJK characters\n * and full-width punctuation take 2 columns; everything else is 1.\n * Good enough for aligning table cells in a Chinese-or-English mix;\n * real wcwidth is bigger than we need to drag in for this use case.\n */\nfunction displayWidth(s: string): number {\n let w = 0;\n for (const ch of s) {\n const code = ch.codePointAt(0) ?? 0;\n // CJK unified ideographs, full-width forms, hiragana/katakana,\n // Hangul syllables — rough bucket, close enough for the terminal.\n if (\n (code >= 0x1100 && code <= 0x115f) ||\n (code >= 0x2e80 && code <= 0x303e) ||\n (code >= 0x3041 && code <= 0x33ff) ||\n (code >= 0x3400 && code <= 0x4dbf) ||\n (code >= 0x4e00 && code <= 0x9fff) ||\n (code >= 0xa000 && code <= 0xa4cf) ||\n (code >= 0xac00 && code <= 0xd7a3) ||\n (code >= 0xf900 && code <= 0xfaff) ||\n (code >= 0xfe30 && code <= 0xfe4f) ||\n (code >= 0xff00 && code <= 0xff60) ||\n (code >= 0xffe0 && code <= 0xffe6)\n ) {\n w += 2;\n } else {\n w += 1;\n }\n }\n return w;\n}\n\n/**\n * SEARCH/REPLACE rendered as a minimal diff: filename on top, red\n * `-` lines, a gutter, green `+` lines. No inner markdown / inline\n * parsing — content is shown verbatim so JSDoc `/**` and `*` won't\n * be eaten by bold/italic regex.\n *\n * A truly empty SEARCH means \"new file\" and we label the filename\n * accordingly instead of rendering an empty red half.\n */\nfunction EditBlockRow({ block }: { block: EditBlockView }) {\n const isNewFile = block.search.length === 0;\n const searchLines = block.search.split(\"\\n\");\n const replaceLines = block.replace.split(\"\\n\");\n return (\n <Box flexDirection=\"column\" borderStyle=\"round\" borderColor=\"cyan\" paddingX={1}>\n <Box>\n <Text bold color=\"cyan\">\n {block.filename}\n </Text>\n {isNewFile ? (\n <Text color=\"green\" bold>\n {\" (new file)\"}\n </Text>\n ) : null}\n </Box>\n {isNewFile ? null : (\n <Box flexDirection=\"column\" marginTop={1}>\n {searchLines.map((line, i) => (\n <Text key={`s-${i}-${line.length}`} color=\"red\">\n {`- ${line}`}\n </Text>\n ))}\n </Box>\n )}\n <Box flexDirection=\"column\" marginTop={isNewFile ? 1 : 0}>\n {replaceLines.map((line, i) => (\n <Text key={`r-${i}-${line.length}`} color=\"green\">\n {`+ ${line}`}\n </Text>\n ))}\n </Box>\n </Box>\n );\n}\n\nexport function Markdown({ text }: { text: string }) {\n const cleaned = stripMath(text);\n const blocks = React.useMemo(() => parseBlocks(cleaned), [cleaned]);\n return (\n <Box flexDirection=\"column\" gap={1}>\n {blocks.map((b, i) => (\n <BlockView key={`${i}-${b.kind}`} block={b} />\n ))}\n </Box>\n );\n}\n","import React, { type ReactNode, createContext, useContext, useEffect, useState } from \"react\";\n\n/**\n * Global heartbeat for every animated component (braille spinners,\n * elapsed-seconds counters, cursor blink). A single setInterval feeds\n * all of them instead of each component owning one. Consolidating the\n * 5-6 independent timers that used to exist in App.tsx + EventLog.tsx +\n * PromptInput.tsx into one source dramatically cuts the number of\n * React re-renders per second on heavy turns and stops Ink from\n * patching the terminal 30+ times/sec — the main amplifier of redraw\n * artifacts on winpty/MINTTY-class Windows terminals.\n *\n * Resolution: `TICK_MS` (default 120). Components that want 500ms or\n * 1s cadence just modulo the tick counter.\n */\nexport const TICK_MS = 120;\n\nconst TickContext = createContext(0);\n\nexport interface TickerProviderProps {\n children: ReactNode;\n /**\n * When true, the provider skips the setInterval entirely — tick stays at\n * 0, all consumers render once and never re-render from the timer. Used\n * by PLAIN_UI mode so the cursor and any surviving spinners don't drive\n * repaints on fragile Windows terminals.\n */\n disabled?: boolean;\n}\n\nexport function TickerProvider({ children, disabled }: TickerProviderProps) {\n const [tick, setTick] = useState(0);\n useEffect(() => {\n if (disabled) return;\n const id = setInterval(() => setTick((t) => t + 1), TICK_MS);\n return () => clearInterval(id);\n }, [disabled]);\n return <TickContext.Provider value={tick}>{children}</TickContext.Provider>;\n}\n\n/** Current global tick. Re-renders the calling component every `TICK_MS`. */\nexport function useTick(): number {\n return useContext(TickContext);\n}\n\n/**\n * Seconds elapsed since the calling component mounted. Derived from\n * the shared tick + a fresh Date.now() read, so no dedicated timer\n * is needed.\n */\nexport function useElapsedSeconds(): number {\n const [start] = useState(() => Date.now());\n useTick(); // subscribe to the tick so we re-render\n return Math.floor((Date.now() - start) / 1000);\n}\n","/**\n * Modal-style approval for a `submit_plan` proposal.\n *\n * Three choices:\n * 1. Approve + implement — exits plan mode, pushes a synthetic user\n * message telling the model to implement the plan now.\n * 2. Refine — stays in plan mode; tells the model to explore more\n * and submit an improved plan.\n * 3. Cancel — exits plan mode, drops the plan, tells the model the\n * user didn't want any of it.\n *\n * Mirrors ShellConfirm in structure (border, SingleSelect, three\n * options, no y/n hotkey — mid-typing triggers would be painful).\n * The plan body is rendered verbatim above the picker so the user can\n * actually read what they're approving.\n */\n\nimport { Box, Text } from \"ink\";\nimport React from \"react\";\nimport { SingleSelect } from \"./Select.js\";\nimport { Markdown } from \"./markdown.js\";\n\nexport type PlanConfirmChoice = \"approve\" | \"refine\" | \"cancel\";\n\nexport interface PlanConfirmProps {\n plan: string;\n onChoose: (choice: PlanConfirmChoice) => void;\n /**\n * Cap on rendered plan length. A pathological 20-KB plan would push\n * the picker off the bottom of the terminal; we show the head +\n * \"(…N chars truncated — /tool for full output)\" instead. The picker\n * itself gets the full plan (it's already been committed to the\n * transcript via the tool result).\n */\n maxRenderedChars?: number;\n}\n\nconst DEFAULT_MAX_RENDERED = 2400;\n\nexport function PlanConfirm({ plan, onChoose, maxRenderedChars }: PlanConfirmProps) {\n const cap = maxRenderedChars ?? DEFAULT_MAX_RENDERED;\n const tooLong = plan.length > cap;\n const visible = tooLong\n ? `${plan.slice(0, cap)}\\n\\n… (${plan.length - cap} chars truncated — use /tool to view the full proposal)`\n : plan;\n // Crude signal for \"the model left questions or risks for me\" — the\n // typical section headings. Triggers an extra hint toward the Refine\n // option so users know where to answer them.\n const hasOpenQuestions =\n /^#{1,6}\\s*(open[-\\s]?questions?|risks?|unknowns?|assumptions?|unclear)/im.test(plan) ||\n /^#{1,6}\\s*(待确认|开放问题|风险|未知|假设|不确定)/im.test(plan);\n\n return (\n <Box flexDirection=\"column\" borderStyle=\"round\" borderColor=\"cyan\" paddingX={1} marginY={1}>\n <Box>\n <Text bold color=\"cyan\">\n ▸ plan submitted — awaiting your review\n </Text>\n </Box>\n <Box marginTop={1} flexDirection=\"column\">\n <Markdown text={visible} />\n </Box>\n {hasOpenQuestions ? (\n <Box marginTop={1}>\n <Text color=\"yellow\">\n ▲ the plan has open questions or flagged risks — pick{\" \"}\n <Text bold>Refine / answer questions</Text> to write concrete answers before the model\n moves on.\n </Text>\n </Box>\n ) : null}\n <Box marginTop={1}>\n <SingleSelect\n initialValue={hasOpenQuestions ? \"refine\" : \"approve\"}\n items={[\n {\n value: \"approve\",\n label: \"Approve and implement\",\n hint: \"Exit plan mode. The model starts executing. You'll get a text input to add any last instructions (or just press Enter to skip).\",\n },\n {\n value: \"refine\",\n label: \"Refine / answer questions\",\n hint: \"Stay in plan mode. Write answers, modifications, or critiques; the model revises and re-submits.\",\n },\n {\n value: \"cancel\",\n label: \"Cancel\",\n hint: \"Exit plan mode. Drop the plan; the model won't implement it.\",\n },\n ]}\n onSubmit={(v) => onChoose(v as PlanConfirmChoice)}\n />\n </Box>\n </Box>\n );\n}\n","/**\n * Minimal arrow-key list components for Ink — single-select and\n * multi-select. No external deps beyond Ink's `useInput`.\n *\n * Why hand-roll: `ink-select-input` exists, but it defaults to\n * Enter-only interaction (no space-to-toggle for multi-select), doesn't\n * expose the \"hint / footer\" slot we want under each item, and would be\n * another dep for ~60 lines of UI code. Reasonix already has one React\n * rendering quirk bundled (`ink-text-input`); adding more is low value.\n */\n\nimport { Box, Text, useInput } from \"ink\";\nimport React, { useState } from \"react\";\n\nexport interface SelectItem<V extends string = string> {\n /** Stable identifier — returned to caller on submit. */\n value: V;\n /** First-row label. */\n label: string;\n /** Optional second row rendered dimmed. */\n hint?: string;\n /** If true, item is not selectable (rendered dimmed, skipped on nav). */\n disabled?: boolean;\n}\n\nexport interface SingleSelectProps<V extends string> {\n items: SelectItem<V>[];\n initialValue?: V;\n onSubmit: (value: V) => void;\n onCancel?: () => void;\n}\n\nexport function SingleSelect<V extends string>({\n items,\n initialValue,\n onSubmit,\n onCancel,\n}: SingleSelectProps<V>) {\n const initialIndex = Math.max(\n 0,\n items.findIndex((i) => i.value === initialValue && !i.disabled),\n );\n const [index, setIndex] = useState(initialIndex === -1 ? 0 : initialIndex);\n\n useInput((_input, key) => {\n if (key.upArrow) {\n setIndex((i) => findNextEnabled(items, i, -1));\n } else if (key.downArrow) {\n setIndex((i) => findNextEnabled(items, i, +1));\n } else if (key.return) {\n const chosen = items[index];\n if (chosen && !chosen.disabled) onSubmit(chosen.value);\n } else if (key.escape && onCancel) {\n onCancel();\n }\n });\n\n return (\n <Box flexDirection=\"column\">\n {items.map((item, i) => (\n <SelectRow\n key={item.value}\n item={item}\n active={i === index}\n marker={i === index ? \"▸\" : \" \"}\n />\n ))}\n </Box>\n );\n}\n\nexport interface MultiSelectProps<V extends string> {\n items: SelectItem<V>[];\n initialSelected?: V[];\n onSubmit: (values: V[]) => void;\n onCancel?: () => void;\n /** Footer hint under the list — e.g. \"space toggle · enter confirm\". */\n footer?: string;\n}\n\nexport function MultiSelect<V extends string>({\n items,\n initialSelected = [],\n onSubmit,\n onCancel,\n footer,\n}: MultiSelectProps<V>) {\n const [index, setIndex] = useState(() => {\n const first = items.findIndex((i) => !i.disabled);\n return first === -1 ? 0 : first;\n });\n const [selected, setSelected] = useState<Set<V>>(new Set(initialSelected));\n\n useInput((input, key) => {\n if (key.upArrow) {\n setIndex((i) => findNextEnabled(items, i, -1));\n } else if (key.downArrow) {\n setIndex((i) => findNextEnabled(items, i, +1));\n } else if (input === \" \") {\n const item = items[index];\n if (!item || item.disabled) return;\n setSelected((prev) => {\n const next = new Set(prev);\n if (next.has(item.value)) next.delete(item.value);\n else next.add(item.value);\n return next;\n });\n } else if (key.return) {\n // Preserve catalog order rather than insertion order, so reruns\n // produce the same spec list for the same checkbox set — makes the\n // `config.json` diff trivially stable.\n const ordered = items.filter((i) => selected.has(i.value)).map((i) => i.value);\n onSubmit(ordered);\n } else if (key.escape && onCancel) {\n onCancel();\n }\n });\n\n return (\n <Box flexDirection=\"column\">\n {items.map((item, i) => {\n const checked = selected.has(item.value);\n const marker = checked ? \"[x]\" : \"[ ]\";\n return (\n <SelectRow\n key={item.value}\n item={item}\n active={i === index}\n marker={`${i === index ? \"▸\" : \" \"} ${marker}`}\n />\n );\n })}\n {footer ? (\n <Box marginTop={1}>\n <Text dimColor>{footer}</Text>\n </Box>\n ) : null}\n </Box>\n );\n}\n\n// ---------- internals ----------\n\nfunction SelectRow<V extends string>({\n item,\n active,\n marker,\n}: {\n item: SelectItem<V>;\n active: boolean;\n marker: string;\n}) {\n const color = item.disabled ? \"gray\" : active ? \"cyan\" : undefined;\n return (\n <Box flexDirection=\"column\">\n <Box>\n <Text color={color}>\n {marker} {item.label}\n </Text>\n </Box>\n {item.hint ? (\n <Box paddingLeft={marker.length + 1}>\n <Text dimColor>{item.hint}</Text>\n </Box>\n ) : null}\n </Box>\n );\n}\n\nfunction findNextEnabled<V extends string>(\n items: SelectItem<V>[],\n from: number,\n step: -1 | 1,\n): number {\n if (items.length === 0) return 0;\n let i = from;\n for (let tries = 0; tries < items.length; tries++) {\n i = (i + step + items.length) % items.length;\n if (!items[i]?.disabled) return i;\n }\n return from;\n}\n","/**\n * Inline text input shown after the user picks Approve or Refine in\n * PlanConfirm. Collects free-form feedback before the loop resumes.\n *\n * Why both paths: the plan may contain open questions or risks the\n * model asked the user to weigh in on. If the user just picks Approve\n * with no chance to answer, the model implements against its own\n * guesses. This input lets the user answer questions, pass last-minute\n * constraints, or (on Refine) request concrete changes. Empty input is\n * fine for Approve (skip straight to implement) and triggers a\n * \"ask the user clarifying questions\" path for Refine.\n *\n * Kept minimal: single-line prompt, Enter to submit, Esc to return to\n * the picker without resuming.\n */\n\nimport { Box, Text, useInput } from \"ink\";\nimport React, { useState } from \"react\";\n\nexport interface PlanRefineInputProps {\n /**\n * Which path the user is on. Approve = \"implement with these last\n * instructions\"; refine = \"revise the plan with this feedback\".\n * Drives the header + hint text so users know what kind of message\n * they're writing.\n */\n mode: \"approve\" | \"refine\";\n /** Called with trimmed feedback. Empty string is allowed. */\n onSubmit: (feedback: string) => void;\n /** Called when the user presses Esc to return to the picker. */\n onCancel: () => void;\n}\n\nexport function PlanRefineInput({ mode, onSubmit, onCancel }: PlanRefineInputProps) {\n const [value, setValue] = useState(\"\");\n\n useInput((input, key) => {\n if (key.escape) {\n onCancel();\n return;\n }\n if (key.return) {\n onSubmit(value.trim());\n return;\n }\n if (key.backspace || key.delete) {\n setValue((v) => v.slice(0, -1));\n return;\n }\n // Filter out non-printable chars; accept ordinary text + CJK.\n if (input && !key.ctrl && !key.meta) {\n setValue((v) => v + input);\n }\n });\n\n const title =\n mode === \"approve\"\n ? \"▸ approving — any last instructions or answers to open questions?\"\n : \"▸ refining — what should the model change?\";\n const hint =\n mode === \"approve\"\n ? \"Answer questions the plan raised, add constraints, or just press Enter to approve as-is.\"\n : \"Describe what's wrong or missing, or answer questions the plan raised.\";\n const blankHint =\n mode === \"approve\"\n ? \" (Enter with blank = approve without extra instructions.)\"\n : \" (Enter with blank = ask the model to list concrete questions.)\";\n\n return (\n <Box flexDirection=\"column\" borderStyle=\"round\" borderColor=\"yellow\" paddingX={1} marginY={1}>\n <Box>\n <Text bold color=\"yellow\">\n {title}\n </Text>\n </Box>\n <Box marginTop={1}>\n <Text dimColor>\n {hint} Enter to send · Esc to return to the picker.\n {value === \"\" ? blankHint : \"\"}\n </Text>\n </Box>\n <Box marginTop={1}>\n <Text>\n <Text color=\"yellow\">› </Text>\n <Text>{value || \" \"}</Text>\n <Text color=\"yellow\">▍</Text>\n </Text>\n </Box>\n </Box>\n );\n}\n","import { Box, Text, useInput } from \"ink\";\nimport React, { useRef, useState } from \"react\";\nimport { type MultilineKey, lineAndColumn, processMultilineKey } from \"./multiline-keys.js\";\nimport { useTick } from \"./ticker.js\";\n\nexport interface PromptInputProps {\n value: string;\n onChange: (v: string) => void;\n onSubmit: (v: string) => void;\n disabled?: boolean;\n placeholder?: string;\n}\n\n/**\n * Input box with real cursor support. ←/→ move one column, ↑/↓ move\n * across lines in multi-line buffers, Ctrl+A / Ctrl+E jump to\n * start/end of the current line. Backspace deletes before cursor,\n * Delete deletes under cursor. Multi-line composition via Ctrl+J,\n * Shift+Enter, or bash-style `\\<Enter>`.\n *\n * Cursor state lives locally. When the parent replaces `value` out\n * of band (history recall, slash completion, setup wizard) the\n * cursor jumps to end; the `lastLocalValueRef` guards distinguishes\n * that case from our own edits.\n */\nexport function PromptInput({\n value,\n onChange,\n onSubmit,\n disabled,\n placeholder,\n}: PromptInputProps) {\n const [cursor, setCursor] = useState(value.length);\n // Tracks the last `value` we ourselves produced via onChange. If the\n // incoming `value` prop diverges from this, the parent (or some other\n // source) replaced the buffer — we reset the cursor to end.\n const lastLocalValueRef = useRef(value);\n if (value !== lastLocalValueRef.current) {\n lastLocalValueRef.current = value;\n if (cursor !== value.length) {\n // Conditional setState during render is the \"derived state\" pattern;\n // React schedules the re-render and the else branch of the `if`\n // prevents infinite loops.\n setCursor(value.length);\n }\n }\n\n // Shared ticker drives the cursor blink. Dividing the tick by 4 lands\n // the visible on/off cycle around 480ms — standard cursor cadence.\n const tick = useTick();\n const showCursor = disabled ? false : Math.floor(tick / 4) % 2 === 0;\n\n useInput(\n (input, key) => {\n const ke: MultilineKey = {\n input,\n return: key.return,\n shift: key.shift,\n ctrl: key.ctrl,\n meta: key.meta,\n backspace: key.backspace,\n delete: key.delete,\n tab: key.tab,\n upArrow: key.upArrow,\n downArrow: key.downArrow,\n leftArrow: key.leftArrow,\n rightArrow: key.rightArrow,\n escape: key.escape,\n pageUp: key.pageUp,\n pageDown: key.pageDown,\n };\n const action = processMultilineKey(value, cursor, ke);\n if (action.next !== null) {\n lastLocalValueRef.current = action.next;\n onChange(action.next);\n }\n if (action.cursor !== null) {\n setCursor(action.cursor);\n }\n if (action.submit) onSubmit(action.submitValue ?? value);\n },\n { isActive: !disabled },\n );\n\n const effectivePlaceholder = disabled\n ? (placeholder ?? \"…waiting for response…\")\n : (placeholder ?? \"type a message, or /command · Ctrl+J for newline\");\n\n const lines = value.length > 0 ? value.split(\"\\n\") : [\"\"];\n const borderColor = disabled ? \"gray\" : \"cyan\";\n const { line: cursorLine, col: cursorCol } = lineAndColumn(value, cursor);\n\n return (\n <Box borderStyle=\"round\" borderColor={borderColor} paddingX={1} flexDirection=\"column\">\n {lines.map((line, i) => {\n const isFirst = i === 0;\n const showPlaceholder = isFirst && value.length === 0;\n const isCursorLine = i === cursorLine;\n return (\n // biome-ignore lint/suspicious/noArrayIndexKey: stable by construction — lines are derived from `value.split(\"\\n\")` and never reordered\n <Box key={i}>\n {isFirst ? (\n <Text bold color={borderColor}>\n you ›{\" \"}\n </Text>\n ) : (\n <Text dimColor>{\" \"}</Text>\n )}\n {showPlaceholder ? (\n <>\n {isCursorLine && !disabled ? (\n <Text color={borderColor}>{showCursor ? \"▌\" : \" \"}</Text>\n ) : null}\n <Text dimColor>{effectivePlaceholder}</Text>\n </>\n ) : isCursorLine && !disabled ? (\n <LineWithCursor\n line={line}\n col={cursorCol}\n showCursor={showCursor}\n borderColor={borderColor}\n />\n ) : (\n <Text>{line}</Text>\n )}\n </Box>\n );\n })}\n </Box>\n );\n}\n\nfunction LineWithCursor({\n line,\n col,\n showCursor,\n borderColor,\n}: {\n line: string;\n col: number;\n showCursor: boolean;\n borderColor: \"cyan\" | \"gray\";\n}) {\n const before = line.slice(0, col);\n const atCursor = line.slice(col, col + 1);\n const after = line.slice(col + 1);\n if (atCursor.length === 0) {\n // Cursor sits past the last char of this line (end-of-line). Render\n // a trailing block so the user sees where they're typing next.\n return (\n <>\n <Text>{before}</Text>\n <Text color={borderColor}>{showCursor ? \"▌\" : \" \"}</Text>\n </>\n );\n }\n return (\n <>\n <Text>{before}</Text>\n <Text inverse={showCursor}>{atCursor}</Text>\n <Text>{after}</Text>\n </>\n );\n}\n","/**\n * Pure keystroke → action reducer for PromptInput.\n *\n * Kept separate from the React component so the keyboard semantics\n * are easy to unit-test. The component threads `useInput` through\n * this function and applies the returned action.\n *\n * Edit model:\n * - Full cursor support. ←/→ move one column. ↑/↓ move across\n * lines in a multi-line buffer (column preserved when possible).\n * Ctrl+A / Ctrl+E jump to start / end of the current line.\n * - Backspace deletes the char before the cursor; Delete deletes\n * the char under the cursor.\n * - Printable chars (including multi-char paste bursts) insert\n * at the cursor.\n * - Enter submits unless Shift is held (newline), the line ends\n * with '\\' and cursor is at end (bash-style continuation), or\n * the input is Ctrl+J (LF, terminal-universal newline).\n * - Parent owns Tab, Esc, PageUp/Down (slash-complete, abort,\n * unused). Arrow keys are split: empty buffer → parent (history\n * recall); non-empty → child (cursor movement).\n */\n\nexport interface MultilineKey {\n input: string;\n return?: boolean;\n shift?: boolean;\n ctrl?: boolean;\n meta?: boolean;\n backspace?: boolean;\n delete?: boolean;\n tab?: boolean;\n upArrow?: boolean;\n downArrow?: boolean;\n leftArrow?: boolean;\n rightArrow?: boolean;\n escape?: boolean;\n pageUp?: boolean;\n pageDown?: boolean;\n}\n\nexport interface MultilineAction {\n /** New buffer value. `null` = unchanged. */\n next: string | null;\n /** New cursor position (0..value.length). `null` = unchanged. */\n cursor: number | null;\n /** When `true`, fire `onSubmit(submitValue ?? value)`. */\n submit: boolean;\n submitValue?: string;\n}\n\nconst BACKSLASH_SUFFIX = /\\\\$/;\n\nconst NOOP: MultilineAction = { next: null, cursor: null, submit: false };\n\nexport function processMultilineKey(\n value: string,\n cursor: number,\n key: MultilineKey,\n): MultilineAction {\n // Parent-owned keys: Tab (slash-complete), Esc (abort), PageUp/Down.\n if (key.tab || key.escape || key.pageUp || key.pageDown) {\n return NOOP;\n }\n\n // Empty buffer + ↑/↓ → parent handles history recall.\n if (value.length === 0 && (key.upArrow || key.downArrow)) {\n return NOOP;\n }\n\n // Cursor motion.\n if (key.leftArrow) {\n return { next: null, cursor: Math.max(0, cursor - 1), submit: false };\n }\n if (key.rightArrow) {\n return { next: null, cursor: Math.min(value.length, cursor + 1), submit: false };\n }\n if (key.upArrow) {\n const moved = moveCursorUp(value, cursor);\n return moved === cursor ? NOOP : { next: null, cursor: moved, submit: false };\n }\n if (key.downArrow) {\n const moved = moveCursorDown(value, cursor);\n return moved === cursor ? NOOP : { next: null, cursor: moved, submit: false };\n }\n\n // Emacs-style line jumps (universal across terminals; Home/End aren't\n // reliably reported by Ink so we don't depend on them).\n if (key.ctrl && key.input === \"a\") {\n return { next: null, cursor: startOfLine(value, cursor), submit: false };\n }\n if (key.ctrl && key.input === \"e\") {\n return { next: null, cursor: endOfLine(value, cursor), submit: false };\n }\n\n // Newline: Ctrl+J (LF literal) or ctrl+'j' normalized form.\n if (key.input === \"\\n\" || (key.ctrl && key.input === \"j\")) {\n return insertAt(value, cursor, \"\\n\");\n }\n\n if (key.return) {\n if (key.shift) return insertAt(value, cursor, \"\\n\");\n // Bash-style line continuation: trailing '\\' + Enter (only when the\n // cursor sits at end-of-buffer, so a stray '\\' mid-line doesn't\n // trigger it).\n if (cursor === value.length && BACKSLASH_SUFFIX.test(value)) {\n const replaced = `${value.slice(0, -1)}\\n`;\n return { next: replaced, cursor: replaced.length, submit: false };\n }\n return { next: null, cursor: null, submit: true, submitValue: value };\n }\n\n // Backspace = delete the char BEFORE the cursor. We also accept\n // `key.delete` and the raw DEL (0x7f) / BS (0x08) bytes as backspace\n // for the same purpose — some Windows terminals (cmd.exe, certain\n // winpty configs) report plain Backspace without setting\n // `key.backspace`, which used to leave the user typing into a prompt\n // where the Backspace key did nothing. Reasonix doesn't offer a\n // separate forward-delete operation, so collapsing them is safe.\n if (key.backspace || key.delete || key.input === \"\\x7f\" || key.input === \"\\b\") {\n if (cursor === 0) return NOOP;\n return {\n next: value.slice(0, cursor - 1) + value.slice(cursor),\n cursor: cursor - 1,\n submit: false,\n };\n }\n\n // Bare modifier events (Ctrl/Meta with no printable) and unhandled\n // Ctrl-<letter> chords are dropped so a stray Ctrl+L doesn't insert \"l\".\n if ((key.ctrl || key.meta) && key.input.length === 0) return NOOP;\n if (key.ctrl || key.meta) return NOOP;\n\n // Printable input (may be a multi-char paste; pasted newlines land\n // inside the buffer rather than triggering submit on the first line).\n if (key.input.length > 0) {\n return insertAt(value, cursor, key.input);\n }\n\n return NOOP;\n}\n\nfunction insertAt(value: string, cursor: number, insert: string): MultilineAction {\n return {\n next: value.slice(0, cursor) + insert + value.slice(cursor),\n cursor: cursor + insert.length,\n submit: false,\n };\n}\n\n/**\n * Line + column of a cursor inside a buffer. Exported because the\n * renderer needs the same mapping for drawing the cursor block on the\n * right line.\n */\nexport function lineAndColumn(value: string, cursor: number): { line: number; col: number } {\n let line = 0;\n let col = 0;\n const n = Math.min(cursor, value.length);\n for (let i = 0; i < n; i++) {\n if (value[i] === \"\\n\") {\n line++;\n col = 0;\n } else {\n col++;\n }\n }\n return { line, col };\n}\n\nfunction startOfLine(value: string, cursor: number): number {\n return value.lastIndexOf(\"\\n\", cursor - 1) + 1;\n}\n\nfunction endOfLine(value: string, cursor: number): number {\n const nl = value.indexOf(\"\\n\", cursor);\n return nl === -1 ? value.length : nl;\n}\n\nfunction moveCursorUp(value: string, cursor: number): number {\n const curStart = startOfLine(value, cursor);\n if (curStart === 0) return cursor; // already on the first line\n const col = cursor - curStart;\n const prevEnd = curStart - 1; // the '\\n' between the two lines\n const prevStart = value.lastIndexOf(\"\\n\", prevEnd - 1) + 1;\n const prevLen = prevEnd - prevStart;\n return prevStart + Math.min(col, prevLen);\n}\n\nfunction moveCursorDown(value: string, cursor: number): number {\n const nextNl = value.indexOf(\"\\n\", cursor);\n if (nextNl === -1) return cursor; // already on the last line\n const curStart = startOfLine(value, cursor);\n const col = cursor - curStart;\n const nextStart = nextNl + 1;\n const followingNl = value.indexOf(\"\\n\", nextStart);\n const nextLen = (followingNl === -1 ? value.length : followingNl) - nextStart;\n return nextStart + Math.min(col, nextLen);\n}\n","import { Box, Text } from \"ink\";\nimport React from \"react\";\nimport { SingleSelect } from \"./Select.js\";\n\nexport type ShellConfirmChoice = \"run_once\" | \"always_allow\" | \"deny\";\n\nexport interface ShellConfirmProps {\n command: string;\n /**\n * The prefix that would be persisted if the user picks\n * \"always allow\". Typically the first 1-2 tokens of `command`.\n */\n allowPrefix: string;\n onChoose: (choice: ShellConfirmChoice) => void;\n}\n\n/**\n * Modal-style approval for a shell command the model wants to run.\n * Three choices:\n * 1. Run once — execute this invocation, prefix NOT remembered.\n * 2. Always allow — persist the prefix to `~/.reasonix/config.json`\n * under this project so every future invocation with that prefix\n * auto-runs.\n * 3. Deny — tell the model the user refused.\n * Arrow keys + Enter. No y/n hotkey — too easy to trigger by accident\n * when the user was mid-typing a response.\n */\nexport function ShellConfirm({ command, allowPrefix, onChoose }: ShellConfirmProps) {\n return (\n <Box flexDirection=\"column\" borderStyle=\"round\" borderColor=\"yellow\" paddingX={1} marginY={1}>\n <Box>\n <Text bold color=\"yellow\">\n ▸ model wants to run a shell command\n </Text>\n </Box>\n <Box marginTop={1}>\n <Text>\n <Text dimColor>{\"$ \"}</Text>\n <Text color=\"cyan\">{command}</Text>\n </Text>\n </Box>\n <Box marginTop={1}>\n <SingleSelect\n initialValue=\"run_once\"\n items={[\n {\n value: \"run_once\",\n label: \"Run once\",\n hint: \"Execute this command, don't remember it.\",\n },\n {\n value: \"always_allow\",\n label: `Always allow \"${allowPrefix}\" in this project`,\n hint: \"Save the prefix to ~/.reasonix/config.json; future matches auto-run.\",\n },\n {\n value: \"deny\",\n label: \"Deny\",\n hint: \"Tell the model the user refused; it will continue without this command.\",\n },\n ]}\n onSubmit={(v) => onChoose(v as ShellConfirmChoice)}\n />\n </Box>\n </Box>\n );\n}\n\n/**\n * Pick the \"always allow\" prefix from a full command. Heuristic:\n * - one-token commands (\"ls\", \"pytest\") → the token itself\n * - multi-token → first two tokens for well-known wrappers\n * (`npm install`, `git commit`, `cargo add`, `docker run` …)\n * otherwise just the first token (covers `node <script>` where the\n * second token is usually a file path specific to this invocation).\n * Exported so tests can pin the heuristic.\n */\nexport function derivePrefix(command: string): string {\n const tokens = command.trim().split(/\\s+/).filter(Boolean);\n if (tokens.length === 0) return \"\";\n if (tokens.length === 1) return tokens[0]!;\n const first = tokens[0]!;\n const TWO_TOKEN_WRAPPERS = new Set([\n \"npm\",\n \"npx\",\n \"pnpm\",\n \"yarn\",\n \"bun\",\n \"git\",\n \"cargo\",\n \"go\",\n \"docker\",\n \"kubectl\",\n \"python\",\n \"python3\",\n \"deno\",\n \"pip\",\n \"pip3\",\n \"make\",\n \"rake\",\n \"bundle\",\n \"gem\",\n ]);\n return TWO_TOKEN_WRAPPERS.has(first) ? `${first} ${tokens[1]}` : first;\n}\n","import { Box, Text } from \"ink\";\n// biome-ignore lint/style/useImportType: tsconfig.jsx = \"react\" needs React in value scope for JSX compilation\nimport React from \"react\";\nimport type { SlashCommandSpec } from \"./slash.js\";\n\nexport interface SlashSuggestionsProps {\n /**\n * Current matching suggestions, computed by the parent. `null` means\n * \"not in slash-prefix mode\" — render nothing. Empty array means \"in\n * slash mode but no matches\" — render the \"no matches\" hint.\n */\n matches: SlashCommandSpec[] | null;\n /** Index (within `matches`) of the currently highlighted row. */\n selectedIndex: number;\n}\n\n/**\n * Floating slash-command panel. Rendered below the input box when\n * the user is typing a `/…` prefix. Navigation state lives in the\n * parent (App.tsx owns `slashSelected`) so ↑/↓/Tab/Enter stay\n * consistent with the useInput handler. This component is pure\n * display: `matches` and `selectedIndex` come in, rows go out.\n */\nexport function SlashSuggestions({\n matches,\n selectedIndex,\n}: SlashSuggestionsProps): React.ReactElement | null {\n if (matches === null) return null;\n if (matches.length === 0) {\n return (\n <Box paddingX={1}>\n <Text color=\"yellow\">no slash command matches that prefix</Text>\n <Text dimColor> — Backspace to edit, or /help for the full list</Text>\n </Box>\n );\n }\n // Limit rows so the suggestion list never dwarfs the rest of the\n // UI. Keep the currently-selected row in view by sliding the\n // window when the selection is near either edge.\n const MAX = 8;\n const total = matches.length;\n const windowStart =\n total <= MAX ? 0 : Math.max(0, Math.min(selectedIndex - Math.floor(MAX / 2), total - MAX));\n const shown = matches.slice(windowStart, windowStart + MAX);\n const hiddenAbove = windowStart;\n const hiddenBelow = total - windowStart - shown.length;\n return (\n <Box flexDirection=\"column\" paddingX={1}>\n {hiddenAbove > 0 ? <Text dimColor> ↑ {hiddenAbove} more above</Text> : null}\n {shown.map((spec, i) => (\n <SuggestionRow key={spec.cmd} spec={spec} isSelected={windowStart + i === selectedIndex} />\n ))}\n {hiddenBelow > 0 ? <Text dimColor> ↓ {hiddenBelow} more below</Text> : null}\n <Text dimColor> ↑/↓ navigate · Tab or Enter to pick</Text>\n </Box>\n );\n}\n\nfunction SuggestionRow({ spec, isSelected }: { spec: SlashCommandSpec; isSelected: boolean }) {\n const marker = isSelected ? \"▸\" : \" \";\n const name = `/${spec.cmd}`;\n const argsSuffix = spec.argsHint ? ` ${spec.argsHint}` : \"\";\n // Selected row gets full cyan coloring so it pops out against the\n // dimmed others — mirrors the style GitHub Issues / Discord use\n // for focused dropdown rows.\n if (isSelected) {\n return (\n <Box>\n <Text bold color=\"cyan\">\n {marker} {name.padEnd(12)}\n {argsSuffix.padEnd(16)}\n </Text>\n <Text color=\"cyan\"> {spec.summary}</Text>\n </Box>\n );\n }\n return (\n <Box>\n <Text dimColor>\n {marker} {name.padEnd(12)}\n {argsSuffix.padEnd(16)} {spec.summary}\n </Text>\n </Box>\n );\n}\n","import { Box, Text } from \"ink\";\nimport React from \"react\";\nimport { DEEPSEEK_CONTEXT_TOKENS, DEFAULT_CONTEXT_TOKENS } from \"../../telemetry.js\";\nimport type { SessionSummary } from \"../../telemetry.js\";\nimport { VERSION } from \"../../version.js\";\n\nexport interface StatsPanelProps {\n summary: SessionSummary;\n model: string;\n prefixHash: string;\n harvestOn?: boolean;\n branchBudget?: number;\n /**\n * True when `reasonix code` is currently running in read-only Plan\n * Mode. Surfaced as a red \"PLAN\" tag in the panel header so the user\n * can tell at a glance that edits are gated behind submit_plan +\n * approval.\n */\n planMode?: boolean;\n /**\n * Account balance fetched once at launch (and optionally refreshed\n * per-turn by the TUI). `null` or absent hides the balance cell\n * entirely — /user/balance failed or the user ran with `--no-config`.\n * The top-up warning fires below 1.0 unit of whatever currency\n * the endpoint reports so a Chinese user with CNY and a U.S. user\n * with USD both see \"getting low.\"\n */\n balance?: { currency: string; total: number } | null;\n /**\n * Published npm version newer than VERSION. Rendered as a yellow\n * \"· update: X\" nudge in the panel header. `null` / `undefined`\n * hides the nudge (offline launch, already up to date, or check\n * still in flight).\n */\n updateAvailable?: string | null;\n}\n\nexport function StatsPanel({\n summary,\n model,\n prefixHash,\n harvestOn,\n branchBudget,\n planMode,\n balance,\n updateAvailable,\n}: StatsPanelProps) {\n const hitPct = (summary.cacheHitRatio * 100).toFixed(1);\n const hitColor =\n summary.cacheHitRatio >= 0.7 ? \"green\" : summary.cacheHitRatio >= 0.4 ? \"yellow\" : \"red\";\n const branchOn = (branchBudget ?? 1) > 1;\n\n const ctxMax = DEEPSEEK_CONTEXT_TOKENS[model] ?? DEFAULT_CONTEXT_TOKENS;\n const ctxRatio = summary.lastPromptTokens / ctxMax;\n const ctxColor = ctxRatio >= 0.8 ? \"red\" : ctxRatio >= 0.5 ? \"yellow\" : undefined;\n\n return (\n <Box borderStyle=\"round\" borderColor=\"cyan\" flexDirection=\"column\" paddingX={1}>\n <Box justifyContent=\"space-between\">\n <Text>\n <Text color=\"cyan\" bold>\n Reasonix\n </Text>\n <Text dimColor>{` v${VERSION}`}</Text>\n <Text dimColor> · model </Text>\n <Text color=\"yellow\">{model}</Text>\n <Text dimColor> · prefix </Text>\n <Text dimColor>{prefixHash}</Text>\n {harvestOn ? <Text color=\"magenta\"> · harvest</Text> : null}\n {branchOn ? <Text color=\"blue\"> · branch{branchBudget}</Text> : null}\n {planMode ? (\n <Text color=\"red\" bold>\n {\" \"}\n · PLAN\n </Text>\n ) : null}\n </Text>\n <Text>\n {updateAvailable ? (\n <Text color=\"yellow\" bold>{`update: ${updateAvailable} · `}</Text>\n ) : null}\n <Text dimColor>turns {summary.turns} · type /help</Text>\n </Text>\n </Box>\n <Box marginTop={1} gap={3}>\n <Text>\n <Text dimColor>cache hit </Text>\n <Text color={hitColor} bold>\n {hitPct}%\n </Text>\n </Text>\n <Text>\n <Text dimColor>cost </Text>\n <Text color=\"green\" bold>\n ${summary.totalCostUsd.toFixed(6)}\n </Text>\n <Text dimColor>\n {\" (in \"}${summary.totalInputCostUsd.toFixed(6)}\n {\" · out \"}${summary.totalOutputCostUsd.toFixed(6)}\n {\")\"}\n </Text>\n </Text>\n {summary.lastPromptTokens > 0 ? (\n <Text>\n <Text dimColor>ctx </Text>\n <Text color={ctxColor} bold={ctxColor !== undefined}>\n {formatTokens(summary.lastPromptTokens)}/{formatTokens(ctxMax)}\n </Text>\n <Text dimColor> ({(ctxRatio * 100).toFixed(0)}%)</Text>\n {ctxRatio >= 0.8 ? (\n <Text color=\"red\" bold>\n {\" \"}\n · /compact\n </Text>\n ) : null}\n </Text>\n ) : null}\n {balance ? (\n <Text>\n <Text dimColor>balance </Text>\n <Text color={balance.total < 1 ? \"red\" : balance.total < 5 ? \"yellow\" : \"green\"} bold>\n {balance.currency === \"USD\" ? \"$\" : \"\"}\n {balance.total.toFixed(2)}\n {balance.currency !== \"USD\" ? ` ${balance.currency}` : \"\"}\n </Text>\n </Text>\n ) : null}\n </Box>\n </Box>\n );\n}\n\n/**\n * Compact integer formatter: 1234 → \"1.2k\", 131072 → \"131k\". Keeps the\n * panel narrow enough to fit on 80-col terminals even when the context\n * is near full.\n */\nfunction formatTokens(n: number): string {\n if (n < 1000) return String(n);\n const k = n / 1000;\n return k >= 100 ? `${k.toFixed(0)}k` : `${k.toFixed(1)}k`;\n}\n","import { spawnSync } from \"node:child_process\";\nimport {\n HOOK_EVENTS,\n type HookEvent,\n type ResolvedHook,\n globalSettingsPath,\n projectSettingsPath,\n} from \"../../hooks.js\";\nimport type { CacheFirstLoop } from \"../../loop.js\";\nimport type { InspectionReport } from \"../../mcp/inspect.js\";\nimport { PROJECT_MEMORY_FILE, memoryEnabled, readProjectMemory } from \"../../project-memory.js\";\nimport { deleteSession, listSessions } from \"../../session.js\";\nimport { SkillStore } from \"../../skills.js\";\nimport { DEEPSEEK_CONTEXT_TOKENS, DEFAULT_CONTEXT_TOKENS } from \"../../telemetry.js\";\nimport { type MemoryScope, MemoryStore } from \"../../user-memory.js\";\nimport { VERSION, compareVersions, isNpxInstall } from \"../../version.js\";\n\nexport interface SlashResult {\n /** Text to display back to the user as a system/info line. */\n info?: string;\n /** Exit the app. */\n exit?: boolean;\n /** Clear the visible history. */\n clear?: boolean;\n /** Unknown command — display usage hint. */\n unknown?: boolean;\n /**\n * Re-submit this text as a user message after displaying `info`.\n * Used by `/retry` — the slash command truncates the log, then\n * asks the TUI to push the original text back through the normal\n * submit flow so a fresh turn runs.\n */\n resubmit?: string;\n}\n\n/**\n * Extra runtime context a slash handler may care about but that isn't\n * already on the loop. Kept as an optional object so tests that only\n * need loop-scoped commands can skip it, and callers only populate the\n * slots that apply to their session.\n */\nexport interface SlashContext {\n /**\n * The exact `--mcp` / config-derived spec strings that were bridged\n * into this session (one entry per server). Used by `/mcp`. Empty or\n * omitted → no MCP servers attached.\n */\n mcpSpecs?: string[];\n /**\n * Callback for `/undo` — provided by the TUI when it's running in\n * code mode. Returns a human-readable report of what was restored.\n * Absent outside code mode → `/undo` replies \"not available here\".\n */\n codeUndo?: () => string;\n /**\n * Callback for `/apply` — commits pending edit blocks to disk. Returns\n * a report of what landed. Absent → `/apply` replies \"nothing pending\"\n * or \"not available outside code mode\".\n */\n codeApply?: () => string;\n /**\n * Callback for `/discard` — drops the pending edit blocks without\n * touching disk.\n */\n codeDiscard?: () => string;\n /**\n * Root directory passed by `reasonix code`. Enables `/commit`, which\n * runs `git add -A && git commit` in this directory. Missing → `/commit`\n * replies \"only available in code mode\".\n */\n codeRoot?: string;\n /**\n * How many edit blocks are currently pending `/apply` or `/discard`.\n * Surfaced by `/status`. TUI populates it live from its pending ref;\n * omitted → treat as 0 (chat-only session).\n */\n pendingEditCount?: number;\n /**\n * Callback returning every tool result seen this session in\n * chronological order (oldest first). Powers `/tool [N]` for\n * inspecting the full untruncated output that `EventLog` clips at\n * 400 chars for display. Absent → `/tool` replies \"not available\".\n */\n toolHistory?: () => Array<{ toolName: string; text: string }>;\n /**\n * Pre-captured inspection reports for each connected MCP server.\n * Populated once at chat startup (chat.tsx) so `/mcp` can render\n * tools + resources + prompts synchronously without needing async\n * handler support.\n */\n mcpServers?: McpServerSummary[];\n /**\n * Directory `/memory` should resolve `REASONIX.md` from. In code\n * mode this is the rootDir the filesystem tools are pinned to; in\n * plain chat this is `process.cwd()` at launch time. Absent → the\n * TUI is running in some non-cwd context (tests) and `/memory`\n * replies \"root unknown\" instead of silently reading a different dir.\n */\n memoryRoot?: string;\n /**\n * Current plan-mode state, surfaced in `/status` and toggled by\n * `/plan`. Present iff the session is a `reasonix code` run — chat\n * mode doesn't have plan mode.\n */\n planMode?: boolean;\n /**\n * Callback the `/plan` slash uses to flip plan mode on/off. Also\n * mirrors the state to the underlying ToolRegistry so dispatch\n * enforcement follows. Absent → `/plan` replies \"only available in\n * code mode\".\n */\n setPlanMode?: (on: boolean) => void;\n /**\n * Callback that clears a pending-plan picker state. Called by\n * `/apply-plan` so that when the user force-approves, the picker\n * dismisses without also firing its own approval synthetic (the\n * slash returns its own `resubmit` instead). Safe to call with no\n * pending plan.\n */\n clearPendingPlan?: () => void;\n /**\n * Re-load `~/.reasonix/settings.json` + `<project>/.reasonix/settings.json`\n * and update both the App's hook state and the loop's mutable hook\n * list. Returns the new hook count so the slash can echo a sane\n * confirmation. Absent → `/hooks reload` replies \"not available\".\n */\n reloadHooks?: () => number;\n /**\n * Latest published version if App's background registry check\n * has completed, `null` otherwise (still in flight OR offline).\n * Drives `/update` — the slash shows whatever the async check\n * already resolved, so the command is fully synchronous.\n */\n latestVersion?: string | null;\n /**\n * Fire-and-forget: kick off a fresh registry fetch. `/update`\n * calls this whenever it encounters `latestVersion === null`\n * so the user can rerun the slash a few seconds later and see\n * a concrete answer. Absent → the slash just reports \"pending\"\n * with no retry path.\n */\n refreshLatestVersion?: () => void;\n}\n\nexport interface McpServerSummary {\n /** Short label shown in the `/mcp` output (server namespace or \"anon\"). */\n label: string;\n /** Original --mcp spec string. */\n spec: string;\n /** Count of tools bridged into the Reasonix registry from this server. */\n toolCount: number;\n /** Full inspection snapshot — used for the resources + prompts sections. */\n report: InspectionReport;\n}\n\n/**\n * Slash command registry. Drives `/help`, the on-type suggestion\n * popup (`SlashSuggestions`), and auto-complete. Kept as data rather\n * than derived from the `handleSlash` switch so summaries can be\n * user-facing rather than code comments.\n *\n * `contextual` gates commands that only make sense in certain modes:\n * - `\"code\"` — only show when the TUI is running `reasonix code`\n * - absent → always shown\n */\nexport interface SlashCommandSpec {\n cmd: string;\n summary: string;\n contextual?: \"code\";\n /** If the command takes args, hint text shown after the name. */\n argsHint?: string;\n}\n\nexport const SLASH_COMMANDS: readonly SlashCommandSpec[] = [\n { cmd: \"help\", summary: \"show the full command reference\" },\n { cmd: \"status\", summary: \"current model, flags, context, session\" },\n {\n cmd: \"preset\",\n argsHint: \"<fast|smart|max>\",\n summary: \"one-tap model + harvest + branch bundle\",\n },\n { cmd: \"model\", argsHint: \"<id>\", summary: \"switch DeepSeek model id\" },\n { cmd: \"harvest\", argsHint: \"[on|off]\", summary: \"toggle Pillar-2 plan-state extraction\" },\n { cmd: \"branch\", argsHint: \"<N|off>\", summary: \"run N parallel samples per turn (N>=2)\" },\n { cmd: \"mcp\", summary: \"list MCP servers + tools attached to this session\" },\n { cmd: \"tool\", argsHint: \"[N]\", summary: \"dump full output of the Nth tool call (1=latest)\" },\n {\n cmd: \"memory\",\n argsHint: \"[list|show <name>|forget <name>|clear <scope> confirm]\",\n summary: \"show / manage pinned memory (REASONIX.md + ~/.reasonix/memory)\",\n },\n {\n cmd: \"skill\",\n argsHint: \"[list|show <name>|<name> [args]]\",\n summary: \"list / run user skills (<project>/.reasonix/skills + ~/.reasonix/skills)\",\n },\n {\n cmd: \"hooks\",\n argsHint: \"[reload]\",\n summary: \"list active hooks (settings.json under .reasonix/) · reload re-reads from disk\",\n },\n {\n cmd: \"update\",\n summary: \"show current vs latest version + the shell command to upgrade\",\n },\n { cmd: \"think\", summary: \"dump the last turn's full R1 reasoning (reasoner only)\" },\n { cmd: \"retry\", summary: \"truncate & resend your last message (fresh sample)\" },\n { cmd: \"compact\", argsHint: \"[cap]\", summary: \"shrink oversized tool results in the log\" },\n { cmd: \"sessions\", summary: \"list saved sessions (current marked with ▸)\" },\n { cmd: \"forget\", summary: \"delete the current session from disk\" },\n { cmd: \"setup\", summary: \"reminds you to exit and run `reasonix setup`\" },\n { cmd: \"clear\", summary: \"clear visible scrollback only (log/context kept)\" },\n { cmd: \"new\", summary: \"start a fresh conversation (clear context + scrollback)\" },\n { cmd: \"exit\", summary: \"quit the TUI\" },\n // Code-mode only\n { cmd: \"apply\", summary: \"commit pending edit blocks to disk\", contextual: \"code\" },\n { cmd: \"discard\", summary: \"drop pending edit blocks without writing\", contextual: \"code\" },\n { cmd: \"undo\", summary: \"roll back the last applied edit batch\", contextual: \"code\" },\n {\n cmd: \"commit\",\n argsHint: '\"msg\"',\n summary: \"git add -A && git commit -m ...\",\n contextual: \"code\",\n },\n {\n cmd: \"plan\",\n argsHint: \"[on|off]\",\n summary: \"toggle read-only plan mode (writes bounced until submit_plan + approval)\",\n contextual: \"code\",\n },\n {\n cmd: \"apply-plan\",\n summary: \"force-approve a pending / in-text plan (fallback if picker was missed)\",\n contextual: \"code\",\n },\n];\n\n/**\n * Filter the registry by a prefix string (without the leading `/`).\n * Empty prefix returns the full non-contextual list (plus code-mode\n * entries when `codeMode` is true). Case-insensitive.\n */\nexport function suggestSlashCommands(prefix: string, codeMode = false): SlashCommandSpec[] {\n const p = prefix.toLowerCase();\n return SLASH_COMMANDS.filter((c) => {\n if (c.contextual === \"code\" && !codeMode) return false;\n return c.cmd.startsWith(p);\n });\n}\n\nexport function parseSlash(text: string): { cmd: string; args: string[] } | null {\n if (!text.startsWith(\"/\")) return null;\n const parts = text.slice(1).trim().split(/\\s+/);\n const cmd = parts[0]?.toLowerCase() ?? \"\";\n if (!cmd) return null;\n return { cmd, args: parts.slice(1) };\n}\n\nexport function handleSlash(\n cmd: string,\n args: string[],\n loop: CacheFirstLoop,\n ctx: SlashContext = {},\n): SlashResult {\n switch (cmd) {\n case \"exit\":\n case \"quit\":\n return { exit: true };\n\n case \"clear\":\n return {\n clear: true,\n info: \"▸ cleared visible scrollback only. Context (message log) is intact — next turn still sees everything. Use /new to start fresh, or /forget to delete the session entirely.\",\n };\n\n case \"new\":\n case \"reset\": {\n // Actually drop the in-memory log + rewrite the session file\n // so the NEXT call has no prior context. Keeps session name,\n // model, and other config — just the conversation is reset.\n const { dropped } = loop.clearLog();\n return {\n clear: true,\n info: `▸ new conversation — dropped ${dropped} message(s) from context. Same session, fresh slate.`,\n };\n }\n\n case \"help\":\n case \"?\":\n return {\n info: [\n \"Commands:\",\n \" /help this message\",\n \" /status show current settings\",\n \" /preset <fast|smart|max> one-tap presets — see below\",\n \" /model <id> deepseek-chat or deepseek-reasoner\",\n \" /harvest [on|off] Pillar 2: structured plan-state extraction\",\n \" /branch <N|off> run N parallel samples (N>=2), pick most confident\",\n \" /mcp list MCP servers + tools attached to this session\",\n \" /setup (exit + reconfigure) → run `reasonix setup`\",\n \" /compact [cap] shrink large tool results in history (default 4k/result)\",\n \" /think dump the most recent turn's full R1 reasoning (reasoner only)\",\n \" /tool [N] list tool calls (or dump full output of #N, 1=most recent)\",\n \" /memory [sub] show pinned memory (REASONIX.md + ~/.reasonix/memory).\",\n \" subs: list | show <name> | forget <name> | clear <scope> confirm\",\n \" /skill [sub] list / run user skills (project/.reasonix/skills + ~/.reasonix/skills).\",\n \" subs: list | show <name> | <name> [args] (injects skill body as user turn)\",\n \" /retry truncate & resend your last message (fresh sample from the model)\",\n \" /apply (code mode) commit the pending edit blocks to disk\",\n \" /discard (code mode) drop pending edits without writing\",\n \" /undo (code mode) roll back the last applied edit batch\",\n ' /commit \"msg\" (code mode) git add -A && git commit -m \"msg\"',\n \" /plan [on|off] (code mode) toggle read-only plan mode; writes gated behind submit_plan + your approval\",\n \" /apply-plan (code mode) force-approve pending/in-text plan (fallback)\",\n \" /sessions list saved sessions (current is marked with ▸)\",\n \" /forget delete the current session from disk\",\n \" /new start fresh: drop all context + clear scrollback\",\n \" /clear clear displayed scrollback only (context kept — model still sees it)\",\n \" /exit quit\",\n \"\",\n \"Presets:\",\n \" fast deepseek-chat no harvest no branch ~1¢/100turns ← default\",\n \" smart reasoner harvest ~10x cost, slower\",\n \" max reasoner harvest branch 3 ~30x cost, slowest\",\n \"\",\n \"Sessions (auto-enabled by default, named 'default'):\",\n \" reasonix chat --session <name> use a different named session\",\n \" reasonix chat --no-session disable persistence for this run\",\n ].join(\"\\n\"),\n };\n\n case \"mcp\": {\n const servers = ctx.mcpServers ?? [];\n const specs = ctx.mcpSpecs ?? [];\n const toolSpecs = loop.prefix.toolSpecs ?? [];\n if (servers.length === 0 && specs.length === 0 && toolSpecs.length === 0) {\n return {\n info:\n \"no MCP servers attached. Run `reasonix setup` to pick some, \" +\n 'or launch with --mcp \"<spec>\". `reasonix mcp list` shows the catalog.',\n };\n }\n // Rich path — we have full inspection reports, so show each\n // server with its tools / resources / prompts grouped together.\n if (servers.length > 0) {\n const lines: string[] = [];\n for (const s of servers) {\n const { report } = s;\n const serverName = report.serverInfo.name || \"(unknown)\";\n const serverVer = report.serverInfo.version ? ` v${report.serverInfo.version}` : \"\";\n lines.push(`[${s.label}] ${serverName}${serverVer} — ${s.spec}`);\n lines.push(` tools ${s.toolCount}`);\n appendSection(lines, \"resources\", report.resources);\n appendSection(lines, \"prompts \", report.prompts);\n lines.push(\"\");\n }\n lines.push(\n \"Chat mode consumes tools today; resources+prompts are surfaced here for awareness.\",\n );\n lines.push(\n \"Full catalog: `reasonix mcp list` · deeper diagnosis: `reasonix mcp inspect <spec>`.\",\n );\n return { info: lines.join(\"\\n\") };\n }\n // Fallback — older path when the TUI hasn't populated `mcpServers`.\n const lines: string[] = [];\n if (specs.length > 0) {\n lines.push(`MCP servers (${specs.length}):`);\n for (const spec of specs) lines.push(` · ${spec}`);\n lines.push(\"\");\n }\n if (toolSpecs.length > 0) {\n lines.push(`Tools in registry (${toolSpecs.length}):`);\n for (const t of toolSpecs) lines.push(` · ${t.function.name}`);\n }\n lines.push(\"\");\n lines.push(\"To change this set, exit and run `reasonix setup`.\");\n return { info: lines.join(\"\\n\") };\n }\n\n case \"setup\":\n return {\n info:\n \"To reconfigure (preset, MCP servers, API key), exit this chat and run \" +\n \"`reasonix setup`. Changes take effect on next launch.\",\n };\n\n case \"retry\": {\n const prev = loop.retryLastUser();\n if (!prev) {\n return {\n info: \"nothing to retry — no prior user message in this session's log.\",\n };\n }\n const preview = prev.length > 80 ? `${prev.slice(0, 80)}…` : prev;\n return {\n info: `▸ retrying: \"${preview}\"`,\n resubmit: prev,\n };\n }\n\n case \"memory\": {\n return handleMemorySlash(args, ctx);\n }\n\n case \"skill\":\n case \"skills\": {\n return handleSkillSlash(args, ctx);\n }\n\n case \"hook\":\n case \"hooks\": {\n return handleHooksSlash(args, loop, ctx);\n }\n\n case \"update\": {\n return handleUpdateSlash(ctx);\n }\n\n case \"think\":\n case \"reasoning\": {\n const raw = loop.scratch.reasoning;\n if (!raw || !raw.trim()) {\n return {\n info:\n \"no reasoning cached. `/think` shows the full R1 thought for the most recent turn — \" +\n \"only `deepseek-reasoner` produces it, and only once the turn completes.\",\n };\n }\n return { info: `↳ full thinking (${raw.length} chars):\\n\\n${raw.trim()}` };\n }\n\n case \"tool\": {\n // EventLog truncates tool results at 400 chars for display. When\n // the user wants to check what the model actually read (e.g. to\n // verify it isn't hallucinating a file's contents), they need\n // the full text. `/tool` is the escape hatch.\n const history = ctx.toolHistory?.() ?? [];\n if (history.length === 0) {\n return {\n info:\n \"no tool calls yet in this session. `/tool` lists them once the model has actually \" +\n \"used a tool; `/tool N` dumps the full (untruncated) output of the Nth-most-recent.\",\n };\n }\n const raw = (args[0] ?? \"\").toLowerCase();\n if (raw === \"\" || raw === \"list\" || raw === \"ls\") {\n return { info: formatToolList(history) };\n }\n const n = Number.parseInt(raw, 10);\n if (!Number.isFinite(n) || n < 1) {\n return {\n info: \"usage: /tool [N] (no arg → list; N=1 → most recent result in full, N=2 → previous, …)\",\n };\n }\n if (n > history.length) {\n return {\n info: `only ${history.length} tool call(s) in history — asked for #${n}. Try /tool with no arg to see the list.`,\n };\n }\n const entry = history[history.length - n];\n if (!entry) {\n return { info: `could not read tool call #${n}` };\n }\n return {\n info: `↳ tool<${entry.toolName}> #${n} (${entry.text.length} chars):\\n\\n${entry.text}`,\n };\n }\n\n case \"undo\": {\n if (!ctx.codeUndo) {\n return {\n info: \"/undo is only available inside `reasonix code` — chat mode doesn't apply edits.\",\n };\n }\n return { info: ctx.codeUndo() };\n }\n\n case \"apply\": {\n if (!ctx.codeApply) {\n return {\n info: \"/apply is only available inside `reasonix code` (nothing to apply here).\",\n };\n }\n return { info: ctx.codeApply() };\n }\n\n case \"discard\": {\n if (!ctx.codeDiscard) {\n return {\n info: \"/discard is only available inside `reasonix code`.\",\n };\n }\n return { info: ctx.codeDiscard() };\n }\n\n case \"plan\": {\n if (!ctx.setPlanMode) {\n return {\n info: \"/plan is only available inside `reasonix code` — chat mode doesn't gate tool writes.\",\n };\n }\n const currentOn = Boolean(ctx.planMode);\n const raw = (args[0] ?? \"\").toLowerCase();\n let target: boolean;\n if (raw === \"on\" || raw === \"true\" || raw === \"1\") target = true;\n else if (raw === \"off\" || raw === \"false\" || raw === \"0\") target = false;\n else target = !currentOn;\n ctx.setPlanMode(target);\n if (target) {\n return {\n info: \"▸ plan mode ON — write tools are gated; the model MUST call `submit_plan` before anything executes. (The model can also call submit_plan on its own for big tasks even when plan mode is off — this toggle is the stronger, explicit constraint.) Type /plan off to leave.\",\n };\n }\n return {\n info: \"▸ plan mode OFF — write tools are live again. Model can still propose plans autonomously for large tasks.\",\n };\n }\n\n case \"apply-plan\":\n case \"applyplan\": {\n if (!ctx.setPlanMode) {\n return {\n info: \"/apply-plan is only available inside `reasonix code`.\",\n };\n }\n ctx.setPlanMode(false);\n ctx.clearPendingPlan?.();\n return {\n info: \"▸ plan approved — implementing\",\n resubmit:\n \"The plan above has been approved. Implement it now. You are out of plan mode — use edit_file / write_file / run_command as needed. Stick to the plan unless you discover a concrete reason to deviate; if you do, tell me and wait for a response before making that deviation.\",\n };\n }\n\n case \"commit\": {\n if (!ctx.codeRoot) {\n return {\n info: \"/commit is only available inside `reasonix code` (needs a rooted git repo).\",\n };\n }\n // Reassemble the original argv. The parser lowercases cmd but\n // leaves args alone, and the TUI splits on whitespace which\n // mangles quoted messages — rejoin with spaces and strip a\n // surrounding pair of double quotes if the user wrote them.\n const raw = args.join(\" \").trim();\n const message = stripOuterQuotes(raw);\n if (!message) {\n return {\n info: `usage: /commit \"your commit message\" — runs \\`git add -A && git commit -m \"…\"\\` in ${ctx.codeRoot}`,\n };\n }\n return runGitCommit(ctx.codeRoot, message);\n }\n\n case \"compact\": {\n // Manual companion to the automatic heal-on-load. Re-applies\n // truncation with a tighter cap (4k chars per tool result) and\n // rewrites the session file so the shrink persists. Useful when\n // the ctx gauge in StatsPanel goes yellow/red mid-session and\n // the user wants to keep chatting without /forget'ing everything.\n const tight = Number.parseInt(args[0] ?? \"\", 10);\n const cap = Number.isFinite(tight) && tight >= 500 ? tight : 4000;\n const { healedCount, charsSaved } = loop.compact(cap);\n if (healedCount === 0) {\n return {\n info: `▸ nothing to compact — no tool result in history exceeds ${cap.toLocaleString()} chars.`,\n };\n }\n return {\n info: `▸ compacted ${healedCount} tool result(s), saved ${charsSaved.toLocaleString()} chars (~${Math.round(charsSaved / 4).toLocaleString()} tokens). Session file rewritten.`,\n };\n }\n\n case \"sessions\": {\n const items = listSessions();\n if (items.length === 0) {\n return {\n info: \"no saved sessions yet — chat normally and your messages will be saved automatically\",\n };\n }\n const lines = [\"Saved sessions:\"];\n for (const s of items) {\n const sizeKb = (s.size / 1024).toFixed(1);\n const when = s.mtime.toISOString().replace(\"T\", \" \").slice(0, 16);\n const marker = s.name === loop.sessionName ? \"▸\" : \" \";\n lines.push(\n ` ${marker} ${s.name.padEnd(22)} ${String(s.messageCount).padStart(5)} msgs ${sizeKb.padStart(7)} KB ${when}`,\n );\n }\n lines.push(\"\");\n lines.push(\"Resume with: reasonix chat --session <name>\");\n return { info: lines.join(\"\\n\") };\n }\n\n case \"forget\": {\n if (!loop.sessionName) {\n return { info: \"not in a session — nothing to forget\" };\n }\n const name = loop.sessionName;\n const ok = deleteSession(name);\n return {\n info: ok\n ? `▸ deleted session \"${name}\" — current screen still shows the conversation, but next launch starts fresh`\n : `could not delete session \"${name}\" (already gone?)`,\n };\n }\n\n case \"status\": {\n const branchBudget = loop.branchOptions.budget ?? 1;\n const ctxMax = DEEPSEEK_CONTEXT_TOKENS[loop.model] ?? DEFAULT_CONTEXT_TOKENS;\n const lastPromptTokens = loop.stats.summary().lastPromptTokens;\n const ctxPct = ctxMax > 0 ? Math.round((lastPromptTokens / ctxMax) * 100) : 0;\n const ctxLine =\n lastPromptTokens > 0\n ? ` ctx ${compactNum(lastPromptTokens)}/${compactNum(ctxMax)} (${ctxPct}%)`\n : \" ctx no turns yet\";\n const pending = ctx.pendingEditCount ?? 0;\n const sessionLine = loop.sessionName\n ? ` session \"${loop.sessionName}\" · ${loop.log.length} messages in log (resumed ${loop.resumedMessageCount})`\n : \" session (ephemeral — no persistence)\";\n const mcpCount = ctx.mcpSpecs?.length ?? 0;\n const toolCount = loop.prefix.toolSpecs.length;\n const mcpLine = ` mcp ${mcpCount} server(s), ${toolCount} tool(s) in registry`;\n const pendingLine =\n pending > 0 ? ` edits ${pending} pending (/apply to commit, /discard to drop)` : \"\";\n const planLine = ctx.planMode ? \" plan ON — writes gated (submit_plan + approval)\" : \"\";\n const lines = [\n ` model ${loop.model}`,\n ` flags harvest=${loop.harvestEnabled ? \"on\" : \"off\"} · branch=${branchBudget > 1 ? branchBudget : \"off\"} · stream=${loop.stream ? \"on\" : \"off\"}`,\n ctxLine,\n mcpLine,\n sessionLine,\n ];\n if (pendingLine) lines.push(pendingLine);\n if (planLine) lines.push(planLine);\n return { info: lines.join(\"\\n\") };\n }\n\n case \"model\": {\n const id = args[0];\n if (!id) return { info: \"usage: /model <id> (try deepseek-chat or deepseek-reasoner)\" };\n loop.configure({ model: id });\n return { info: `model → ${id}` };\n }\n\n case \"harvest\": {\n const arg = (args[0] ?? \"\").toLowerCase();\n const on = arg === \"\" ? !loop.harvestEnabled : arg === \"on\" || arg === \"true\" || arg === \"1\";\n loop.configure({ harvest: on });\n return { info: `harvest → ${loop.harvestEnabled ? \"on\" : \"off\"}` };\n }\n\n case \"preset\": {\n const name = (args[0] ?? \"\").toLowerCase();\n if (name === \"fast\" || name === \"default\") {\n loop.configure({ model: \"deepseek-chat\", harvest: false, branch: 1 });\n return { info: \"preset → fast (deepseek-chat, no harvest, no branch)\" };\n }\n if (name === \"smart\") {\n loop.configure({ model: \"deepseek-reasoner\", harvest: true, branch: 1 });\n return { info: \"preset → smart (reasoner + harvest, ~10x cost vs fast)\" };\n }\n if (name === \"max\" || name === \"best\") {\n loop.configure({ model: \"deepseek-reasoner\", harvest: true, branch: 3 });\n return {\n info: \"preset → max (reasoner + harvest + branch3, ~30x cost vs fast, slowest)\",\n };\n }\n return { info: \"usage: /preset <fast|smart|max>\" };\n }\n\n case \"branch\": {\n const raw = (args[0] ?? \"\").toLowerCase();\n if (raw === \"\" || raw === \"off\" || raw === \"0\" || raw === \"1\") {\n loop.configure({ branch: 1 });\n return { info: \"branch → off\" };\n }\n const n = Number.parseInt(raw, 10);\n if (!Number.isFinite(n) || n < 2) {\n return { info: \"usage: /branch <N> (N>=2, or 'off')\" };\n }\n if (n > 8) {\n return { info: \"branch budget capped at 8 to prevent runaway cost\" };\n }\n loop.configure({ branch: n });\n return { info: `branch → ${n} (harvest auto-enabled; streaming disabled)` };\n }\n\n default:\n return { unknown: true, info: `unknown command: /${cmd} (try /help)` };\n }\n}\n\n/**\n * `/skill` family. Bare `/skill` (or `/skill list`) prints the\n * discovered skills from `<projectRoot>/.reasonix/skills` (code mode\n * only) + `~/.reasonix/skills`. `/skill show <name>` dumps one body\n * inline for reading. `/skill <name> [args...]` injects the skill body\n * as a user turn via `resubmit` — the same mechanism `/apply-plan`\n * uses — so the next model turn runs with the skill's instructions\n * fresh in the log.\n *\n * Project scope is only populated when the session has a `codeRoot`\n * (set by `reasonix code`). In plain chat mode the store reads the\n * global scope only, matching how user-memory behaves.\n */\n/**\n * `/update` — inside the TUI we deliberately do NOT spawn `npm install`.\n * stdio:inherit into a running Ink renderer corrupts the display, and\n * the process being upgraded is the same process that's still reading\n * its own binaries (messy on Windows). Instead we surface what we\n * already know from the App's background registry check and print the\n * exact shell command the user should run after exiting.\n *\n * The `latestVersion` ctx field is populated by App.tsx's mount-time\n * `getLatestVersion()` effect. When it's `null` we report the check\n * as pending/offline — still a useful output (current version + how\n * to force a fresh check from another terminal).\n */\nfunction handleUpdateSlash(ctx: SlashContext): SlashResult {\n const latest = ctx.latestVersion ?? null;\n const lines: string[] = [`current: reasonix ${VERSION}`];\n if (latest === null) {\n // Kick off a fresh fetch so a follow-up /update a few seconds\n // later has a real answer instead of the same pending message.\n ctx.refreshLatestVersion?.();\n lines.push(\n \"latest: (not yet resolved — background check in flight or offline)\",\n \"\",\n \"triggered a fresh registry fetch — retry `/update` in a few seconds,\",\n \"or run `reasonix update` in another terminal to force it synchronously.\",\n );\n return { info: lines.join(\"\\n\") };\n }\n lines.push(`latest: reasonix ${latest}`);\n const diff = compareVersions(VERSION, latest);\n if (diff >= 0) {\n lines.push(\"\", \"you're on the latest. nothing to do.\");\n return { info: lines.join(\"\\n\") };\n }\n if (isNpxInstall()) {\n lines.push(\n \"\",\n \"you're running via npx — the next `npx reasonix ...` launch will auto-fetch.\",\n \"to force a refresh sooner: `npm cache clean --force`.\",\n );\n } else {\n lines.push(\n \"\",\n \"to upgrade, exit this session and run:\",\n \" reasonix update (interactive, dry-run supported via --dry-run)\",\n \" npm install -g reasonix@latest (direct)\",\n \"\",\n \"in-session install is deliberately disabled — the npm spawn would\",\n \"corrupt this TUI's rendering and Windows can lock the running binary.\",\n );\n }\n return { info: lines.join(\"\\n\") };\n}\n\nfunction handleHooksSlash(args: string[], loop: CacheFirstLoop, ctx: SlashContext): SlashResult {\n const sub = (args[0] ?? \"\").toLowerCase();\n\n if (sub === \"reload\") {\n if (!ctx.reloadHooks) {\n return {\n info: \"/hooks reload is not available in this context (no reload callback wired).\",\n };\n }\n const count = ctx.reloadHooks();\n return { info: `▸ reloaded hooks · ${count} active` };\n }\n\n if (sub !== \"\" && sub !== \"list\" && sub !== \"ls\") {\n return {\n info: \"usage: /hooks list active hooks\\n /hooks reload re-read settings.json files\",\n };\n }\n\n const hooks = loop.hooks;\n const projPath = ctx.codeRoot ? projectSettingsPath(ctx.codeRoot) : undefined;\n const globPath = globalSettingsPath();\n if (hooks.length === 0) {\n const lines = [\n \"no hooks configured.\",\n \"\",\n \"drop a settings.json with a `hooks` key into either of:\",\n ctx.codeRoot\n ? ` · ${projPath} (project)`\n : \" · <project>/.reasonix/settings.json (project)\",\n ` · ${globPath} (global)`,\n \"\",\n \"events: PreToolUse, PostToolUse, UserPromptSubmit, Stop\",\n \"exit 0 = pass · exit 2 = block (Pre*) · other = warn\",\n ];\n return { info: lines.join(\"\\n\") };\n }\n\n const grouped = new Map<HookEvent, ResolvedHook[]>();\n for (const event of HOOK_EVENTS) grouped.set(event, []);\n for (const h of hooks) grouped.get(h.event)?.push(h);\n\n const lines: string[] = [`▸ ${hooks.length} hook(s) loaded`];\n for (const event of HOOK_EVENTS) {\n const list = grouped.get(event) ?? [];\n if (list.length === 0) continue;\n lines.push(\"\", `${event}:`);\n for (const h of list) {\n const match = h.match && h.match !== \"*\" ? ` match=${h.match}` : \"\";\n const desc = h.description ? ` — ${h.description}` : \"\";\n lines.push(` [${h.scope}]${match} ${h.command}${desc}`);\n }\n }\n lines.push(\"\", `sources: project=${projPath ?? \"(none — chat mode)\"} · global=${globPath}`);\n return { info: lines.join(\"\\n\") };\n}\n\nfunction handleSkillSlash(args: string[], ctx: SlashContext): SlashResult {\n const store = new SkillStore({ projectRoot: ctx.codeRoot });\n const sub = (args[0] ?? \"\").toLowerCase();\n\n if (sub === \"\" || sub === \"list\" || sub === \"ls\") {\n const skills = store.list();\n if (skills.length === 0) {\n const lines = [\"no skills found. Reasonix reads skills from:\"];\n if (store.hasProjectScope()) {\n lines.push(\n \" · <project>/.reasonix/skills/<name>/SKILL.md (or <name>.md) — project scope\",\n );\n }\n lines.push(\" · ~/.reasonix/skills/<name>/SKILL.md (or <name>.md) — global scope\");\n if (!store.hasProjectScope()) {\n lines.push(\" (project scope is only active in `reasonix code`)\");\n }\n lines.push(\n \"\",\n \"Each file's frontmatter needs at least `name` and `description`.\",\n \"Invoke a skill with `/skill <name> [args]` or by asking the model to call `run_skill`.\",\n );\n return { info: lines.join(\"\\n\") };\n }\n const lines = [`User skills (${skills.length}):`];\n for (const s of skills) {\n const scope = `(${s.scope})`.padEnd(11);\n const name = s.name.padEnd(24);\n const desc = s.description.length > 70 ? `${s.description.slice(0, 69)}…` : s.description;\n lines.push(` ${scope} ${name} ${desc}`);\n }\n lines.push(\"\");\n lines.push(\"View body: /skill show <name> Run: /skill <name> [args]\");\n return { info: lines.join(\"\\n\") };\n }\n\n if (sub === \"show\" || sub === \"cat\") {\n const target = args[1];\n if (!target) return { info: \"usage: /skill show <name>\" };\n const skill = store.read(target);\n if (!skill) return { info: `no skill found: ${target}` };\n return {\n info: [\n `▸ ${skill.name} (${skill.scope})`,\n skill.description ? ` ${skill.description}` : \"\",\n ` ${skill.path}`,\n \"\",\n skill.body,\n ]\n .filter((l) => l !== \"\")\n .join(\"\\n\"),\n };\n }\n\n // Bare `/skill <name> [args...]` — inject the body as a user turn.\n // The first arg is the skill name; remaining args are forwarded\n // verbatim as the skill's \"Arguments:\" line.\n const name = args[0] ?? \"\";\n const skill = store.read(name);\n if (!skill) {\n return {\n info: `no skill found: ${name} (try /skill list)`,\n };\n }\n const extra = args.slice(1).join(\" \").trim();\n const header = `# Skill: ${skill.name}${skill.description ? `\\n> ${skill.description}` : \"\"}`;\n const argsLine = extra ? `\\n\\nArguments: ${extra}` : \"\";\n const payload = `${header}\\n\\n${skill.body}${argsLine}`;\n return {\n info: `▸ running skill: ${skill.name}${extra ? ` — ${extra}` : \"\"}`,\n resubmit: payload,\n };\n}\n\n/**\n * `/memory` family. Bare `/memory` shows what's pinned (REASONIX.md +\n * both MEMORY.md blocks). Subcommands manage the user-memory store:\n * list — every memory file, both scopes\n * show <name> — dump one file's body\n * show <scope>/<name> — disambiguate when name exists in both scopes\n * forget <name> — delete (same scope resolution as show)\n * clear <scope> confirm — wipe a scope (typed literal \"confirm\" required)\n */\nfunction handleMemorySlash(args: string[], ctx: SlashContext): SlashResult {\n if (!memoryEnabled()) {\n return {\n info: \"memory is disabled (REASONIX_MEMORY=off in env). Unset the var to re-enable — no REASONIX.md or ~/.reasonix/memory content will be pinned in the meantime.\",\n };\n }\n if (!ctx.memoryRoot) {\n return {\n info: \"no working directory on this session — `/memory` needs a root to resolve REASONIX.md from. (Running in a test harness?)\",\n };\n }\n // `codeRoot` is set only when running `reasonix code`. Chat mode has\n // `memoryRoot` = cwd (for REASONIX.md), but we don't treat cwd as a\n // sandbox — project-scope user memory requires a real code-mode root.\n const store = new MemoryStore({ projectRoot: ctx.codeRoot });\n const sub = (args[0] ?? \"\").toLowerCase();\n\n if (sub === \"list\" || sub === \"ls\") {\n const entries = store.list();\n if (entries.length === 0) {\n return {\n info: \"no user memories yet. The model can call `remember` to save one, or you can create files by hand in ~/.reasonix/memory/global/ or the per-project subdir.\",\n };\n }\n const lines = [`User memories (${entries.length}):`];\n for (const e of entries) {\n const tag = `${e.scope}/${e.type}`.padEnd(18);\n const name = e.name.padEnd(28);\n const desc = e.description.length > 70 ? `${e.description.slice(0, 69)}…` : e.description;\n lines.push(` ${tag} ${name} ${desc}`);\n }\n lines.push(\"\");\n lines.push(\"View body: /memory show <name> Delete: /memory forget <name>\");\n return { info: lines.join(\"\\n\") };\n }\n\n if (sub === \"show\" || sub === \"cat\") {\n const target = args[1];\n if (!target) return { info: \"usage: /memory show <name> or /memory show <scope>/<name>\" };\n const resolved = resolveMemoryTarget(store, target);\n if (!resolved) return { info: `no memory found: ${target}` };\n try {\n const entry = store.read(resolved.scope, resolved.name);\n return {\n info: [\n `▸ ${entry.scope}/${entry.name} (${entry.type}, created ${entry.createdAt || \"?\"})`,\n entry.description ? ` ${entry.description}` : \"\",\n \"\",\n entry.body,\n ]\n .filter((l) => l !== \"\")\n .concat(\"\")\n .join(\"\\n\"),\n };\n } catch (err) {\n return { info: `show failed: ${(err as Error).message}` };\n }\n }\n\n if (sub === \"forget\" || sub === \"rm\" || sub === \"delete\") {\n const target = args[1];\n if (!target) return { info: \"usage: /memory forget <name> or /memory forget <scope>/<name>\" };\n const resolved = resolveMemoryTarget(store, target);\n if (!resolved) return { info: `no memory found: ${target}` };\n try {\n const ok = store.delete(resolved.scope, resolved.name);\n return {\n info: ok\n ? `▸ forgot ${resolved.scope}/${resolved.name}. Next /new or launch won't see it.`\n : `could not forget ${resolved.scope}/${resolved.name} (already gone?)`,\n };\n } catch (err) {\n return { info: `forget failed: ${(err as Error).message}` };\n }\n }\n\n if (sub === \"clear\") {\n const rawScope = (args[1] ?? \"\").toLowerCase();\n if (rawScope !== \"global\" && rawScope !== \"project\") {\n return { info: \"usage: /memory clear <global|project> confirm\" };\n }\n if ((args[2] ?? \"\").toLowerCase() !== \"confirm\") {\n return {\n info: `about to delete every memory in scope=${rawScope}. Re-run with the word 'confirm' to proceed: /memory clear ${rawScope} confirm`,\n };\n }\n const scope = rawScope as MemoryScope;\n const entries = store.list().filter((e) => e.scope === scope);\n let deleted = 0;\n for (const e of entries) {\n try {\n if (store.delete(scope, e.name)) deleted++;\n } catch {\n /* skip */\n }\n }\n return { info: `▸ cleared scope=${scope} — deleted ${deleted} memory file(s).` };\n }\n\n // Bare `/memory` — show REASONIX.md + both MEMORY.md blocks.\n const parts: string[] = [];\n const projMem = readProjectMemory(ctx.memoryRoot);\n if (projMem) {\n const hdr = projMem.truncated\n ? `▸ ${PROJECT_MEMORY_FILE}: ${projMem.path} (${projMem.originalChars.toLocaleString()} chars, truncated)`\n : `▸ ${PROJECT_MEMORY_FILE}: ${projMem.path} (${projMem.originalChars.toLocaleString()} chars)`;\n parts.push(hdr, \"\", projMem.content);\n }\n const globalIdx = store.loadIndex(\"global\");\n if (globalIdx) {\n parts.push(\n \"\",\n `▸ global memory (${globalIdx.originalChars.toLocaleString()} chars${globalIdx.truncated ? \", truncated\" : \"\"})`,\n \"\",\n globalIdx.content,\n );\n }\n const projectIdx = store.loadIndex(\"project\");\n if (projectIdx) {\n parts.push(\n \"\",\n `▸ project memory (${projectIdx.originalChars.toLocaleString()} chars${projectIdx.truncated ? \", truncated\" : \"\"})`,\n \"\",\n projectIdx.content,\n );\n }\n if (parts.length === 0) {\n return {\n info: [\n `no memory pinned in ${ctx.memoryRoot}.`,\n \"\",\n \"Three layers are available:\",\n ` 1. ${PROJECT_MEMORY_FILE} — committable team memory (in the repo).`,\n \" 2. ~/.reasonix/memory/global/ — your cross-project private memory.\",\n ` 3. ~/.reasonix/memory/<project-hash>/ — this project's private memory.`,\n \"\",\n \"Ask the model to `remember` something, or hand-edit files directly.\",\n \"Changes take effect on next /new or launch — the system prompt is hashed once per session to keep the prefix cache warm.\",\n \"\",\n \"Subcommands: /memory list | /memory show <name> | /memory forget <name> | /memory clear <scope> confirm\",\n ].join(\"\\n\"),\n };\n }\n parts.push(\n \"\",\n \"Changes take effect on next /new or launch. Subcommands: /memory list | show | forget | clear\",\n );\n return { info: parts.join(\"\\n\") };\n}\n\n/**\n * Parse a `/memory show|forget` argument. Accepts bare `<name>` or\n * `<scope>/<name>`. For bare names, tries project scope first (more\n * specific, usually what the user means) then falls back to global.\n */\nfunction resolveMemoryTarget(\n store: MemoryStore,\n raw: string,\n): { scope: MemoryScope; name: string } | null {\n const slash = raw.indexOf(\"/\");\n if (slash > 0) {\n const scopeRaw = raw.slice(0, slash).toLowerCase();\n const name = raw.slice(slash + 1);\n if (scopeRaw !== \"global\" && scopeRaw !== \"project\") return null;\n const scope = scopeRaw as MemoryScope;\n if (scope === \"project\" && !store.hasProjectScope()) return null;\n return { scope, name };\n }\n for (const scope of [\"project\", \"global\"] as MemoryScope[]) {\n if (scope === \"project\" && !store.hasProjectScope()) continue;\n try {\n store.read(scope, raw);\n return { scope, name: raw };\n } catch {\n /* next scope */\n }\n }\n return null;\n}\n\n/**\n * Render a section (resources / prompts) of an MCP inspection into a\n * compact \"name count items\" form, collapsing when unsupported.\n * Names-only — descriptions and full metadata live in\n * `reasonix mcp inspect`, which is purpose-built for the deep view.\n */\nfunction appendSection(\n lines: string[],\n label: string,\n section:\n | { supported: true; items: Array<{ name: string }> }\n | { supported: false; reason: string }\n | undefined,\n): void {\n if (!section || !section.supported) {\n lines.push(\n ` ${label.trim()} ${section?.supported === false ? \"(not supported)\" : \"(none)\"}`,\n );\n return;\n }\n const names = section.items.map((i) => i.name);\n if (names.length === 0) {\n lines.push(` ${label.trim()} (none)`);\n return;\n }\n const head = names.slice(0, 5).join(\", \");\n const more = names.length > 5 ? ` +${names.length - 5} more` : \"\";\n lines.push(` ${label.trim()} ${names.length} [${head}${more}]`);\n}\n\nfunction formatToolList(history: Array<{ toolName: string; text: string }>): string {\n const total = history.length;\n const header = `Tool calls in this session (${total}, most recent first):`;\n // Show the 10 most recent. Older ones are rarely what the user\n // wants — and the help footer tells them how to reach any entry\n // by index if they do.\n const shown = Math.min(total, 10);\n const lines: string[] = [header];\n for (let i = 0; i < shown; i++) {\n const entry = history[total - 1 - i];\n if (!entry) continue;\n const idx = i + 1; // 1-based from most recent\n const flat = entry.text.replace(/\\s+/g, \" \").trim();\n const preview = flat.length > 80 ? `${flat.slice(0, 80)}…` : flat;\n const name = entry.toolName.length > 24 ? `${entry.toolName.slice(0, 23)}…` : entry.toolName;\n lines.push(\n ` #${String(idx).padStart(2)} ${name.padEnd(24)} ${String(entry.text.length).padStart(6)} chars ${preview}`,\n );\n }\n if (total > shown) {\n lines.push(` … (${total - shown} earlier, reach with /tool N)`);\n }\n lines.push(\"\");\n lines.push(\"View full output: /tool N (N=1 → most recent)\");\n return lines.join(\"\\n\");\n}\n\nfunction compactNum(n: number): string {\n if (n < 1000) return String(n);\n const k = n / 1000;\n return k >= 100 ? `${Math.round(k)}k` : `${k.toFixed(1)}k`;\n}\n\nfunction stripOuterQuotes(s: string): string {\n if (s.length >= 2 && s.startsWith('\"') && s.endsWith('\"')) {\n return s.slice(1, -1);\n }\n return s;\n}\n\n/**\n * Run `git add -A` then `git commit -m <message>` in `rootDir`. Returns\n * a SlashResult with a human-scannable info line. We surface stderr on\n * failure so the user sees exactly what git complained about (bad\n * config, pre-commit hook rejection, nothing staged, etc.).\n */\nfunction runGitCommit(rootDir: string, message: string): SlashResult {\n const add = spawnSync(\"git\", [\"add\", \"-A\"], { cwd: rootDir, encoding: \"utf8\" });\n if (add.error || add.status !== 0) {\n return { info: `git add failed (${add.status ?? \"?\"}):\\n${gitTail(add)}` };\n }\n const commit = spawnSync(\"git\", [\"commit\", \"-m\", message], {\n cwd: rootDir,\n encoding: \"utf8\",\n });\n if (commit.error || commit.status !== 0) {\n return { info: `git commit failed (${commit.status ?? \"?\"}):\\n${gitTail(commit)}` };\n }\n const firstLine = (commit.stdout || \"\").split(/\\r?\\n/)[0] ?? \"\";\n return { info: `▸ committed: ${message}${firstLine ? `\\n ${firstLine}` : \"\"}` };\n}\n\n/**\n * Safely extract whatever diagnostic text is available from a spawnSync\n * result — on Windows or when cwd doesn't exist, `stderr`/`stdout` can\n * be `undefined` and the caller has only `error.message` to go on.\n */\nfunction gitTail(res: ReturnType<typeof spawnSync>): string {\n const stderr = (res.stderr as string | undefined) ?? \"\";\n const stdout = (res.stdout as string | undefined) ?? \"\";\n const body = stderr.trim() || stdout.trim();\n if (body) return body;\n if (res.error) return (res.error as Error).message;\n return \"(no output from git)\";\n}\n","import { Box, Text } from \"ink\";\nimport React from \"react\";\nimport { SingleSelect } from \"./Select.js\";\n\nexport type SessionChoice = \"resume\" | \"new\" | \"delete\";\n\nexport interface SessionPickerProps {\n sessionName: string;\n messageCount: number;\n /** mtime of the session file; used to render \"last active Nh ago\". */\n lastActive: Date;\n onChoose: (choice: SessionChoice) => void;\n}\n\nexport function SessionPicker({\n sessionName,\n messageCount,\n lastActive,\n onChoose,\n}: SessionPickerProps) {\n return (\n <Box flexDirection=\"column\" marginY={1}>\n <Box marginBottom={1}>\n <Text bold color=\"cyan\">\n {`Session \"${sessionName}\" has ${messageCount} prior message${messageCount === 1 ? \"\" : \"s\"}`}\n </Text>\n <Text dimColor>{` · last active ${relativeTime(lastActive)}`}</Text>\n </Box>\n <SingleSelect\n initialValue=\"new\"\n items={[\n {\n value: \"new\",\n label: \"Start new conversation\",\n hint: \"Previous messages kept on disk; your turn starts fresh.\",\n },\n {\n value: \"resume\",\n label: \"Resume\",\n hint: `Continue where you left off (${messageCount} messages in context).`,\n },\n {\n value: \"delete\",\n label: \"Delete and start new\",\n hint: \"Wipes the session file irreversibly. Other sessions untouched.\",\n },\n ]}\n onSubmit={(v) => onChoose(v as SessionChoice)}\n />\n <Box marginTop={1}>\n <Text dimColor>↑↓ to move · Enter to pick</Text>\n </Box>\n </Box>\n );\n}\n\n/**\n * \"Nh ago\" / \"Nm ago\" / \"yesterday\" style relative time. Deliberately\n * coarse — the picker just needs a sense of \"how stale is this session\".\n */\nfunction relativeTime(date: Date): string {\n const ms = Date.now() - date.getTime();\n const mins = Math.floor(ms / 60_000);\n if (mins < 1) return \"just now\";\n if (mins < 60) return `${mins}m ago`;\n const hours = Math.floor(mins / 60);\n if (hours < 24) return `${hours}h ago`;\n const days = Math.floor(hours / 24);\n if (days === 1) return \"yesterday\";\n if (days < 7) return `${days}d ago`;\n return date.toISOString().slice(0, 10);\n}\n","import { Box, Text, useApp } from \"ink\";\nimport TextInput from \"ink-text-input\";\nimport React, { useState } from \"react\";\nimport { defaultConfigPath, isPlausibleKey, redactKey, saveApiKey } from \"../../config.js\";\n\nexport interface SetupProps {\n onReady: (apiKey: string) => void;\n}\n\nexport function Setup({ onReady }: SetupProps) {\n const [value, setValue] = useState(\"\");\n const [error, setError] = useState<string | null>(null);\n const { exit } = useApp();\n\n const handleSubmit = (raw: string) => {\n const trimmed = raw.trim();\n if (trimmed === \"/exit\" || trimmed === \"/quit\") {\n exit();\n return;\n }\n if (!isPlausibleKey(trimmed)) {\n setError(\"Doesn't look like a DeepSeek key. They start with 'sk-' and are 30+ chars.\");\n setValue(\"\");\n return;\n }\n try {\n saveApiKey(trimmed);\n } catch (err) {\n setError(`Could not save key: ${(err as Error).message}`);\n return;\n }\n onReady(trimmed);\n };\n\n return (\n <Box flexDirection=\"column\" borderStyle=\"round\" borderColor=\"cyan\" paddingX={1}>\n <Text bold color=\"cyan\">\n Welcome to Reasonix.\n </Text>\n <Box marginTop={1}>\n <Text>Paste your DeepSeek API key to get started.</Text>\n </Box>\n <Text dimColor>Get one (free credit on signup): https://platform.deepseek.com/api_keys</Text>\n <Text dimColor>Saved locally to {defaultConfigPath()}</Text>\n <Box marginTop={1}>\n <Text bold color=\"cyan\">\n {\"key › \"}\n </Text>\n <TextInput\n value={value}\n onChange={setValue}\n onSubmit={handleSubmit}\n mask=\"•\"\n placeholder=\"sk-...\"\n />\n </Box>\n {error ? (\n <Box marginTop={1}>\n <Text color=\"red\">{error}</Text>\n </Box>\n ) : value ? (\n <Box marginTop={1}>\n <Text dimColor>preview: {redactKey(value)}</Text>\n </Box>\n ) : null}\n <Box marginTop={1}>\n <Text dimColor>(Type /exit to abort.)</Text>\n </Box>\n </Box>\n );\n}\n","/**\n * `reasonix code [dir]` — opinionated wrapper around `reasonix chat` for\n * code-editing workflows.\n *\n * What it does differently from plain chat:\n * - Registers native filesystem tools rooted at the given directory\n * (CWD by default). No subprocess, no `npx install` step, R1-\n * friendly schemas. Replaced the old `@modelcontextprotocol/server-filesystem`\n * subprocess in 0.4.9 because its `edit_file` argv shape was the\n * biggest driver of R1 DSML hallucinations.\n * - Uses a coding-focused system prompt (src/code/prompt.ts) that\n * teaches the model to propose edits as SEARCH/REPLACE blocks.\n * - Defaults to the `smart` preset (reasoner + harvest) because\n * coding tasks pay back R1 thinking.\n * - Scopes its session to the directory so projects don't share\n * conversation history.\n * - Hooks `codeMode` into the TUI so assistant replies get parsed\n * for SEARCH/REPLACE blocks and applied on disk after each turn.\n */\n\nimport { basename, resolve } from \"node:path\";\nimport { loadProjectShellAllowed } from \"../../config.js\";\nimport { sanitizeName } from \"../../session.js\";\nimport { ToolRegistry } from \"../../tools.js\";\nimport { registerFilesystemTools } from \"../../tools/filesystem.js\";\nimport { registerMemoryTools } from \"../../tools/memory.js\";\nimport { registerPlanTool } from \"../../tools/plan.js\";\nimport { registerShellTools } from \"../../tools/shell.js\";\nimport { registerSkillTools } from \"../../tools/skills.js\";\nimport { chatCommand } from \"./chat.js\";\n\nexport interface CodeOptions {\n /** Directory to root the filesystem tools at. Defaults to process.cwd(). */\n dir?: string;\n /** Override the default `smart` model. */\n model?: string;\n /** Disable session persistence. */\n noSession?: boolean;\n /** Transcript file for replay/diff. */\n transcript?: string;\n /** Skip the session picker — always resume prior messages. */\n forceResume?: boolean;\n /** Skip the session picker — always wipe prior messages and start fresh. */\n forceNew?: boolean;\n}\n\nexport async function codeCommand(opts: CodeOptions = {}): Promise<void> {\n const { codeSystemPrompt } = await import(\"../../code/prompt.js\");\n const rootDir = resolve(opts.dir ?? process.cwd());\n // Per-directory session so switching projects doesn't mix histories.\n // `code-<sanitized-basename>` fits the session name rules without\n // truncating most project names.\n const session = opts.noSession ? undefined : `code-${sanitizeName(basename(rootDir))}`;\n\n // Native filesystem tools. No subprocess, ~50-200 ms faster per call\n // than the MCP server was, and `edit_file` takes a flat SEARCH/REPLACE\n // shape instead of the `string=\"false\"` JSON-in-string array that\n // triggered R1's DSML hallucinations all through 0.4.x.\n const tools = new ToolRegistry();\n registerFilesystemTools(tools, { rootDir });\n registerShellTools(tools, {\n rootDir,\n // Per-project \"always allow\" list persisted from prior ShellConfirm\n // choices; merged on top of the built-in allowlist in shell.ts.\n // GETTER form — re-read every dispatch so a prefix the user adds\n // via ShellConfirm mid-session takes effect on the next shell call\n // instead of waiting for `/new` or a relaunch.\n extraAllowed: () => loadProjectShellAllowed(rootDir),\n });\n // `submit_plan` is always in the spec list so the prefix cache stays\n // stable across plan-mode toggles (Pillar 1). The tool itself is a\n // no-op outside plan mode and throws `PlanProposedError` when the\n // user has `/plan`-enabled the session.\n registerPlanTool(tools);\n // `remember` / `forget` / `recall_memory` — cross-session user memory.\n // Project scope hashes off rootDir so switching projects gets a fresh\n // per-project memory store; the global scope is shared across runs.\n registerMemoryTools(tools, { projectRoot: rootDir });\n // `run_skill` — loads user-authored prompt packs on demand. The\n // index itself (names + descriptions) is already pinned into the\n // system prompt by `applyMemoryStack`; this tool serves the bodies.\n // Passing projectRoot surfaces `<rootDir>/.reasonix/skills/` alongside\n // the global scope.\n registerSkillTools(tools, { projectRoot: rootDir });\n\n process.stderr.write(\n `▸ reasonix code: rooted at ${rootDir}, session \"${session ?? \"(ephemeral)\"}\" · ${tools.size} native tool(s)\\n`,\n );\n\n await chatCommand({\n model: opts.model ?? \"deepseek-reasoner\",\n harvest: true, // smart preset's harvest setting, always on for code\n system: codeSystemPrompt(rootDir),\n transcript: opts.transcript,\n session,\n seedTools: tools,\n codeMode: { rootDir },\n forceResume: opts.forceResume,\n forceNew: opts.forceNew,\n });\n}\n","import { writeFileSync } from \"node:fs\";\nimport { basename } from \"node:path\";\nimport { render } from \"ink\";\nimport React from \"react\";\nimport { diffTranscripts, renderMarkdown, renderSummaryTable } from \"../../diff.js\";\nimport { readTranscript } from \"../../transcript.js\";\nimport { DiffApp } from \"../ui/DiffApp.js\";\n\nexport interface DiffOptions {\n a: string;\n b: string;\n mdPath?: string;\n labelA?: string;\n labelB?: string;\n /** Force stdout summary table (no Ink TUI). Auto when stdout isn't a TTY. */\n print?: boolean;\n /** Force the TUI even when stdout isn't a TTY (rare). */\n tui?: boolean;\n}\n\n/**\n * Compare two transcripts. Three output paths, picked in order:\n * - If --md is passed: write the markdown report. Also prints the stdout\n * summary so the user sees what was exported.\n * - If --print, no TTY, or --md (see above): stdout summary table.\n * - Otherwise: interactive Ink TUI with split-pane + n/N divergence jump.\n */\nexport async function diffCommand(opts: DiffOptions): Promise<void> {\n const aParsed = readTranscript(opts.a);\n const bParsed = readTranscript(opts.b);\n\n const report = diffTranscripts(\n { label: opts.labelA ?? basename(opts.a), parsed: aParsed },\n { label: opts.labelB ?? basename(opts.b), parsed: bParsed },\n );\n\n const wantMarkdown = !!opts.mdPath;\n const wantPrint = opts.print || !process.stdout.isTTY;\n const wantTui = opts.tui || (!wantPrint && !wantMarkdown);\n\n if (wantMarkdown) {\n // Markdown export implies the user wants an artifact, not a TUI.\n // Still echo the stdout summary to confirm the action.\n console.log(renderSummaryTable(report));\n const md = renderMarkdown(report);\n writeFileSync(opts.mdPath!, md, \"utf8\");\n console.log(`\\nmarkdown report written to ${opts.mdPath}`);\n return;\n }\n\n if (wantTui) {\n const { waitUntilExit } = render(React.createElement(DiffApp, { report }), {\n exitOnCtrlC: true,\n patchConsole: false,\n });\n await waitUntilExit();\n return;\n }\n\n // stdout fallback (piped, --print, or non-TTY)\n console.log(renderSummaryTable(report));\n}\n","/**\n * Ink TUI for `reasonix diff`. Split-pane: A on the left, B on the right,\n * shared cursor. Header shows aggregate deltas; footer shows the current\n * pair's divergence note (if any) + key cheat sheet.\n *\n * j/k moves the cursor by one turn; n/N jumps to the next/prev divergent\n * turn — which is the whole point of a diff tool. Quit with q.\n *\n * Pure navigation lives in src/diff.ts (findNextDivergence / findPrevDivergence).\n */\n\nimport { Box, Static, Text, useApp, useInput } from \"ink\";\nimport React, { useState } from \"react\";\nimport {\n type DiffReport,\n type TurnPair,\n findNextDivergence,\n findPrevDivergence,\n} from \"../../diff.js\";\nimport { RecordView } from \"./RecordView.js\";\n\nexport interface DiffAppProps {\n report: DiffReport;\n}\n\nexport function DiffApp({ report }: DiffAppProps) {\n const { exit } = useApp();\n const maxIdx = Math.max(0, report.pairs.length - 1);\n // Start at the first divergence when one exists — that's the user's most\n // likely destination. Falls back to idx 0 for fully-matching diffs.\n const initialIdx = report.firstDivergenceTurn\n ? report.pairs.findIndex((p) => p.turn === report.firstDivergenceTurn)\n : 0;\n const [idx, setIdx] = useState(Math.max(0, initialIdx));\n\n useInput((input, key) => {\n if (input === \"q\" || (key.ctrl && input === \"c\")) {\n exit();\n return;\n }\n if (input === \"j\" || key.downArrow || input === \" \" || key.return) {\n setIdx((i) => Math.min(maxIdx, i + 1));\n } else if (input === \"k\" || key.upArrow) {\n setIdx((i) => Math.max(0, i - 1));\n } else if (input === \"g\") {\n setIdx(0);\n } else if (input === \"G\") {\n setIdx(maxIdx);\n } else if (input === \"n\") {\n const next = findNextDivergence(report.pairs, idx);\n if (next !== -1) setIdx(next);\n } else if (input === \"N\" || input === \"p\") {\n const prev = findPrevDivergence(report.pairs, idx);\n if (prev !== -1) setIdx(prev);\n }\n });\n\n const pair = report.pairs[idx];\n\n return (\n <Box flexDirection=\"column\">\n <DiffHeader report={report} />\n\n <Box marginTop={1} paddingX={1} justifyContent=\"space-between\">\n <Text color=\"cyan\" bold>\n turn {pair?.turn ?? \"?\"} ({idx + 1} / {report.pairs.length})\n </Text>\n <Text>{pair ? <KindBadge kind={pair.kind} /> : null}</Text>\n </Box>\n\n <Box flexDirection=\"row\" marginTop={1}>\n <Pane label={report.a.label} headerColor=\"blue\" records={paneRecords(pair, \"a\")} />\n <Pane label={report.b.label} headerColor=\"magenta\" records={paneRecords(pair, \"b\")} />\n </Box>\n\n {pair?.divergenceNote ? (\n <Box marginTop={1} paddingX={1}>\n <Text color=\"yellow\">★ </Text>\n <Text>{pair.divergenceNote}</Text>\n </Box>\n ) : null}\n\n <Box marginTop={1} paddingX={1} borderStyle=\"single\" borderColor=\"gray\">\n <Text dimColor>\n <Text bold>j</Text>/<Text bold>↓</Text> next · <Text bold>k</Text>/<Text bold>↑</Text>{\" \"}\n prev · <Text bold>n</Text> next-diverge · <Text bold>N</Text>/<Text bold>p</Text>{\" \"}\n prev-diverge · <Text bold>g</Text>/<Text bold>G</Text> first/last · <Text bold>q</Text>{\" \"}\n quit\n </Text>\n </Box>\n </Box>\n );\n}\n\n// ----------------------------------------------------------------------------\n\nfunction DiffHeader({ report }: { report: DiffReport }) {\n const a = report.a;\n const b = report.b;\n\n const cacheDelta = b.stats.cacheHitRatio - a.stats.cacheHitRatio;\n const costDelta =\n a.stats.totalCostUsd > 0\n ? ((b.stats.totalCostUsd - a.stats.totalCostUsd) / a.stats.totalCostUsd) * 100\n : 0;\n\n // Prefix stability one-liner (same logic as the stdout summary).\n const aStable = a.stats.prefixHashes.length <= 1;\n const bStable = b.stats.prefixHashes.length <= 1;\n let prefixLine: string | null = null;\n if (aStable !== bStable) {\n const stableLabel = aStable ? report.a.label : report.b.label;\n const churnLabel = aStable ? report.b.label : report.a.label;\n const churnCount = aStable ? b.stats.prefixHashes.length : a.stats.prefixHashes.length;\n prefixLine = `${stableLabel} stayed byte-stable; ${churnLabel} churned ${churnCount} distinct prefixes.`;\n } else if (a.stats.prefixHashes[0] && a.stats.prefixHashes[0] === b.stats.prefixHashes[0]) {\n prefixLine = `shared prefix hash ${a.stats.prefixHashes[0].slice(0, 12)}… — cache delta attributable to log stability, not prompt change.`;\n }\n\n return (\n <Box flexDirection=\"column\" borderStyle=\"round\" borderColor=\"cyan\" paddingX={1}>\n <Box justifyContent=\"space-between\">\n <Text>\n <Text color=\"cyan\" bold>\n reasonix diff\n </Text>\n <Text dimColor> · A=</Text>\n <Text color=\"blue\">{a.label}</Text>\n <Text dimColor> vs B=</Text>\n <Text color=\"magenta\">{b.label}</Text>\n </Text>\n <Text dimColor>{report.pairs.length} turns aligned</Text>\n </Box>\n\n <Box marginTop={1} gap={3}>\n <Text>\n <Text dimColor>cache </Text>\n <Text>{(a.stats.cacheHitRatio * 100).toFixed(1)}%</Text>\n <Text dimColor> → </Text>\n <Text>{(b.stats.cacheHitRatio * 100).toFixed(1)}%</Text>\n <Text color={cacheDelta >= 0 ? \"green\" : \"red\"} bold>\n {\" \"}\n {cacheDelta >= 0 ? \"+\" : \"\"}\n {(cacheDelta * 100).toFixed(1)}pp\n </Text>\n </Text>\n <Text>\n <Text dimColor>cost </Text>\n <Text>${a.stats.totalCostUsd.toFixed(6)}</Text>\n <Text dimColor> → </Text>\n <Text>${b.stats.totalCostUsd.toFixed(6)}</Text>\n <Text color={costDelta <= 0 ? \"green\" : \"red\"} bold>\n {\" \"}\n {costDelta >= 0 ? \"+\" : \"\"}\n {costDelta.toFixed(1)}%\n </Text>\n </Text>\n <Text>\n <Text dimColor>model calls </Text>\n <Text>\n {a.stats.turns} → {b.stats.turns}\n </Text>\n </Text>\n </Box>\n\n {prefixLine ? (\n <Box marginTop={1}>\n <Text dimColor italic>\n {prefixLine}\n </Text>\n </Box>\n ) : null}\n </Box>\n );\n}\n\nfunction Pane({\n label,\n headerColor,\n records,\n}: {\n label: string;\n headerColor: \"blue\" | \"magenta\";\n records: TurnPair[\"aTools\"];\n}) {\n return (\n <Box\n flexDirection=\"column\"\n flexGrow={1}\n paddingX={1}\n borderStyle=\"single\"\n borderColor={headerColor}\n >\n <Text color={headerColor} bold>\n {label}\n </Text>\n {records.length === 0 ? (\n <Box marginTop={1}>\n <Text dimColor italic>\n (no records on this side for this turn)\n </Text>\n </Box>\n ) : (\n <Static items={records.map((rec, i) => ({ key: `${label}-${i}`, rec }))}>\n {({ key, rec }) => <RecordView key={key} rec={rec} compact />}\n </Static>\n )}\n </Box>\n );\n}\n\nfunction KindBadge({ kind }: { kind: TurnPair[\"kind\"] }) {\n if (kind === \"match\") {\n return <Text color=\"green\">✓ match</Text>;\n }\n if (kind === \"diverge\") {\n return <Text color=\"yellow\">★ diverge</Text>;\n }\n if (kind === \"only_in_a\") {\n return <Text color=\"blue\">← only in A</Text>;\n }\n return <Text color=\"magenta\">→ only in B</Text>;\n}\n\n// ----------------------------------------------------------------------------\n\nfunction paneRecords(pair: TurnPair | undefined, side: \"a\" | \"b\"): TurnPair[\"aTools\"] {\n if (!pair) return [];\n const tools = side === \"a\" ? pair.aTools : pair.bTools;\n const assistant = side === \"a\" ? pair.aAssistant : pair.bAssistant;\n const out: TurnPair[\"aTools\"] = [...tools];\n if (assistant) out.push(assistant);\n return out;\n}\n","/**\n * Shared renderer for a single TranscriptRecord. Used by ReplayApp and\n * DiffApp — both need the same visual grammar (user cyan, assistant green,\n * tool yellow, error red, cache badge colored by threshold) so transcripts\n * look consistent wherever they're displayed.\n *\n * Kept small on purpose: no streaming/branch/planState paths (those are\n * live-chat concerns and never appear in replayed transcripts).\n */\n\nimport { Box, Text } from \"ink\";\nimport React from \"react\";\nimport type { TranscriptRecord } from \"../../transcript.js\";\nimport { PlanStateBlock } from \"./PlanStateBlock.js\";\n\nexport interface RecordViewProps {\n rec: TranscriptRecord;\n /**\n * When rendering side-by-side in diff mode, shorter truncation limits\n * keep long tool results from dominating the pane. Passes through\n * untouched when undefined.\n */\n compact?: boolean;\n}\n\nexport function RecordView({ rec, compact = false }: RecordViewProps) {\n const toolArgsMax = compact ? 120 : 200;\n const toolContentMax = compact ? 200 : 400;\n\n if (rec.role === \"user\") {\n return (\n <Box marginTop={1}>\n <Text bold color=\"cyan\">\n you ›{\" \"}\n </Text>\n <Text>{rec.content}</Text>\n </Box>\n );\n }\n if (rec.role === \"assistant_final\") {\n return (\n <Box flexDirection=\"column\" marginTop={1}>\n <Box>\n <Text bold color=\"green\">\n assistant\n </Text>\n {rec.cost !== undefined ? (\n <Text dimColor>\n {\" $\"}\n {rec.cost.toFixed(6)}\n </Text>\n ) : null}\n {rec.usage ? <CacheBadge usage={rec.usage} /> : null}\n </Box>\n {rec.planState ? <PlanStateBlock planState={rec.planState} /> : null}\n {rec.content ? (\n <Text>{rec.content}</Text>\n ) : (\n <Text dimColor italic>\n (tool-call response only)\n </Text>\n )}\n </Box>\n );\n }\n if (rec.role === \"tool\") {\n return (\n <Box flexDirection=\"column\" marginTop={1}>\n <Text color=\"yellow\">\n {\"tool<\"}\n {rec.tool ?? \"?\"}\n {\">\"}\n </Text>\n {rec.args ? (\n <Text dimColor>\n {\" args: \"}\n {truncate(rec.args, toolArgsMax)}\n </Text>\n ) : null}\n <Text dimColor>\n {\" → \"}\n {truncate(rec.content, toolContentMax)}\n </Text>\n </Box>\n );\n }\n if (rec.role === \"error\") {\n return (\n <Box marginTop={1}>\n <Text color=\"red\" bold>\n error{\" \"}\n </Text>\n <Text color=\"red\">{rec.error ?? rec.content}</Text>\n </Box>\n );\n }\n if (rec.role === \"done\" || rec.role === \"assistant_delta\") {\n // Noise in replay; skip.\n return null;\n }\n return (\n <Box>\n <Text dimColor>\n [{rec.role}] {rec.content}\n </Text>\n </Box>\n );\n}\n\nfunction CacheBadge({ usage }: { usage: NonNullable<TranscriptRecord[\"usage\"]> }) {\n const hit = usage.prompt_cache_hit_tokens ?? 0;\n const miss = usage.prompt_cache_miss_tokens ?? 0;\n const total = hit + miss;\n if (total === 0) return null;\n const pct = (hit / total) * 100;\n const color = pct >= 70 ? \"green\" : pct >= 40 ? \"yellow\" : \"red\";\n return (\n <Text>\n <Text dimColor>{\" · cache \"}</Text>\n <Text color={color}>{pct.toFixed(1)}%</Text>\n </Text>\n );\n}\n\nfunction truncate(s: string, max: number): string {\n return s.length <= max ? s : `${s.slice(0, max)}… (+${s.length - max} chars)`;\n}\n","/**\n * `reasonix mcp inspect <spec>` — connect to one MCP server, list\n * everything it exposes (tools, resources, prompts), print a\n * human-readable summary. Useful as:\n * - a diagnostic tool for \"does my MCP server even work?\"\n * - a discovery tool for \"what can this server do?\"\n * - the only CLI surface for the resources+prompts methods added\n * in 0.4.5 (chat mode consumes tools but hasn't wired up the\n * other two families yet).\n */\n\nimport { McpClient } from \"../../mcp/client.js\";\nimport { inspectMcpServer } from \"../../mcp/inspect.js\";\nimport type { InspectionReport } from \"../../mcp/inspect.js\";\nimport { parseMcpSpec } from \"../../mcp/spec.js\";\nimport { SseTransport } from \"../../mcp/sse.js\";\nimport { type McpTransport, StdioTransport } from \"../../mcp/stdio.js\";\n\nexport interface McpInspectOptions {\n /** The raw --mcp spec string (e.g. `fs=npx -y @modelcontextprotocol/server-filesystem .`). */\n spec: string;\n /** Emit JSON on stdout instead of the human-readable table. */\n json?: boolean;\n}\n\nexport async function mcpInspectCommand(opts: McpInspectOptions): Promise<void> {\n const spec = parseMcpSpec(opts.spec);\n const transport: McpTransport =\n spec.transport === \"sse\"\n ? new SseTransport({ url: spec.url })\n : new StdioTransport({ command: spec.command, args: spec.args });\n const client = new McpClient({ transport });\n try {\n await client.initialize();\n const report = await inspectMcpServer(client);\n if (opts.json) {\n console.log(JSON.stringify(report, null, 2));\n } else {\n console.log(formatReport(spec.name ?? \"(anon)\", report));\n }\n } finally {\n await client.close();\n }\n}\n\nfunction formatReport(nsName: string, r: InspectionReport): string {\n const lines: string[] = [];\n lines.push(`MCP server [${nsName}]`);\n lines.push(\n ` server ${r.serverInfo.name || \"(unknown)\"}${r.serverInfo.version ? ` v${r.serverInfo.version}` : \"\"}`,\n );\n lines.push(` protocol ${r.protocolVersion}`);\n const capKeys = Object.keys(r.capabilities);\n lines.push(` caps ${capKeys.length > 0 ? capKeys.join(\", \") : \"(none advertised)\"}`);\n if (r.instructions) {\n lines.push(` notes ${r.instructions.trim().slice(0, 200)}`);\n }\n lines.push(\"\");\n lines.push(formatSection(\"Tools\", r.tools, toolLine));\n lines.push(formatSection(\"Resources\", r.resources, resourceLine));\n lines.push(formatSection(\"Prompts\", r.prompts, promptLine));\n return lines.join(\"\\n\");\n}\n\nfunction formatSection<T>(\n title: string,\n section: { supported: true; items: T[] } | { supported: false; reason: string },\n render: (item: T) => string,\n): string {\n if (!section.supported) {\n return `${title}: (not supported — ${section.reason})`;\n }\n if (section.items.length === 0) {\n return `${title}: (none)`;\n }\n const lines = [`${title} (${section.items.length}):`];\n for (const item of section.items) lines.push(` ${render(item)}`);\n return lines.join(\"\\n\");\n}\n\nfunction toolLine(t: { name: string; description?: string }): string {\n const desc = t.description ? ` — ${oneLine(t.description, 80)}` : \"\";\n return `· ${t.name}${desc}`;\n}\n\nfunction resourceLine(r: { uri: string; name: string; mimeType?: string }): string {\n const mime = r.mimeType ? ` [${r.mimeType}]` : \"\";\n return `· ${r.name}${mime} ${r.uri}`;\n}\n\nfunction promptLine(p: {\n name: string;\n description?: string;\n arguments?: Array<{ name: string; required?: boolean }>;\n}): string {\n const argPart =\n p.arguments && p.arguments.length > 0\n ? ` (${p.arguments.map((a) => (a.required ? a.name : `${a.name}?`)).join(\", \")})`\n : \"\";\n const desc = p.description ? ` — ${oneLine(p.description, 80)}` : \"\";\n return `· ${p.name}${argPart}${desc}`;\n}\n\nfunction oneLine(s: string, max: number): string {\n const flat = s.replace(/\\s+/g, \" \").trim();\n return flat.length <= max ? flat : `${flat.slice(0, max - 1)}…`;\n}\n","/**\n * Curated catalog of popular MCP servers.\n *\n * Hardcoded because (a) this list changes slowly — maybe monthly, (b)\n * fetching it over the network would make `reasonix mcp list` flaky\n * offline or behind a proxy. When it does change, update this file and\n * ship a patch release.\n *\n * Inclusion criteria:\n * - actively maintained (official Anthropic repo OR widely used)\n * - stdio-compatible (Reasonix doesn't do SSE yet)\n * - installable with one `npx -y ...` command — zero manual setup\n * - has a clear value proposition in one short line\n *\n * Not included: servers that need API keys / OAuth / complex config.\n * Those get their own docs once we have patterns for them.\n */\n\nexport interface CatalogEntry {\n /** Short name, used as the namespace prefix when suggested. */\n name: string;\n /** One-line description shown in `reasonix mcp list`. */\n summary: string;\n /** npm package id (for `npx -y <pkg>`). */\n package: string;\n /** Extra args the user must supply (e.g. a directory path). */\n userArgs?: string;\n /** Notes the user needs to know — shown dimmed. */\n note?: string;\n}\n\n// Every entry below is verified to exist on npm as of this release.\n// `fetch` and `sqlite` are deliberately *absent* — their reference\n// servers are Python-only (`pip install mcp-server-fetch`), so a Node\n// user running `npx -y @modelcontextprotocol/server-fetch` hits a 404\n// from the npm registry. We'd rather ship a smaller list that always\n// works than a longer list where two options silently 404 on the user.\nexport const MCP_CATALOG: CatalogEntry[] = [\n {\n name: \"filesystem\",\n summary: \"read/write/search files inside a sandboxed directory\",\n package: \"@modelcontextprotocol/server-filesystem\",\n userArgs: \"<dir>\",\n note: \"the directory is a hard sandbox — the server refuses access outside it\",\n },\n {\n name: \"memory\",\n summary: \"persistent key-value memory across sessions\",\n package: \"@modelcontextprotocol/server-memory\",\n },\n {\n name: \"github\",\n summary: \"read issues, PRs, code search (needs GITHUB_PERSONAL_ACCESS_TOKEN)\",\n package: \"@modelcontextprotocol/server-github\",\n note: \"set GITHUB_PERSONAL_ACCESS_TOKEN in your env before spawning\",\n },\n {\n name: \"puppeteer\",\n summary: \"browser automation — take screenshots, click, type\",\n package: \"@modelcontextprotocol/server-puppeteer\",\n note: \"downloads Chromium on first run (~200 MB)\",\n },\n {\n name: \"everything\",\n summary: \"official test server — exercises every MCP feature\",\n package: \"@modelcontextprotocol/server-everything\",\n note: \"useful for debugging your Reasonix setup\",\n },\n];\n\n/**\n * Build the `reasonix chat --mcp \"...\"` command line for a catalog entry.\n * Returns a copy-pasteable fragment starting at `--mcp`.\n */\nexport function mcpCommandFor(entry: CatalogEntry): string {\n const pkg = entry.package;\n const tail = entry.userArgs ? ` ${entry.userArgs}` : \"\";\n return `--mcp \"${entry.name}=npx -y ${pkg}${tail}\"`;\n}\n","/**\n * `reasonix mcp <subcmd>` — the MCP discovery / helper surface.\n *\n * Currently one subcommand: `list`. Prints a curated catalog of popular\n * MCP servers with their `--mcp` commands ready to copy-paste. More\n * subcommands (`init`, `try`, etc.) may land in later alphas.\n */\n\nimport { MCP_CATALOG, mcpCommandFor } from \"../../mcp/catalog.js\";\n\nexport interface McpListOptions {\n /** Emit JSON on stdout instead of the human-readable table. */\n json?: boolean;\n}\n\nexport function mcpListCommand(opts: McpListOptions): void {\n if (opts.json) {\n console.log(JSON.stringify(MCP_CATALOG, null, 2));\n return;\n }\n\n console.log(\"Popular MCP servers you can bridge into Reasonix:\");\n console.log(\"\");\n for (const entry of MCP_CATALOG) {\n console.log(` ${pad(entry.name, 12)} ${entry.summary}`);\n console.log(` ${mcpCommandFor(entry)}`);\n if (entry.note) console.log(` · ${entry.note}`);\n console.log(\"\");\n }\n console.log(\"Usage: reasonix chat <one-of-the---mcp-lines-above>\");\n console.log(\n \"Docs: https://github.com/modelcontextprotocol/servers — Anthropic's official server repo\",\n );\n console.log(\n \" https://mcp.so — community-maintained catalog\",\n );\n}\n\nfunction pad(s: string, width: number): string {\n return s.length >= width ? s : s + \" \".repeat(width - s.length);\n}\n","import { render } from \"ink\";\nimport React from \"react\";\nimport { groupRecordsByTurn, replayFromFile } from \"../../replay.js\";\nimport type { TranscriptRecord } from \"../../transcript.js\";\nimport { ReplayApp } from \"../ui/ReplayApp.js\";\n\nexport interface ReplayOptions {\n path: string;\n head?: number;\n tail?: number;\n /** Force stdout pretty-print mode (no Ink TUI). Also auto-enabled when stdout is not a TTY. */\n print?: boolean;\n}\n\n/**\n * Replay a transcript. Two modes:\n * - Interactive TUI (default when stdout is a TTY): Ink-based j/k navigation.\n * - Stdout pretty-print (--print, or when stdout is piped): one-shot text\n * dump + summary. Kept working so shell pipes, CI logs, and `less`\n * workflows still behave sensibly.\n */\nexport async function replayCommand(opts: ReplayOptions): Promise<void> {\n const wantPrint =\n opts.print || !process.stdout.isTTY || opts.head !== undefined || opts.tail !== undefined;\n if (wantPrint) {\n printReplay(opts);\n return;\n }\n\n const { parsed } = replayFromFile(opts.path);\n const pages = groupRecordsByTurn(parsed.records);\n const { waitUntilExit } = render(React.createElement(ReplayApp, { meta: parsed.meta, pages }), {\n exitOnCtrlC: true,\n patchConsole: false,\n });\n await waitUntilExit();\n}\n\n// ----------------------------------------------------------------------------\n// stdout pretty-print path (original behavior, preserved for piping / CI)\n\nfunction printReplay(opts: ReplayOptions): void {\n const { parsed, stats } = replayFromFile(opts.path);\n\n if (parsed.meta) {\n const m = parsed.meta;\n const bits: string[] = [`source=${m.source}`];\n if (m.model) bits.push(`model=${m.model}`);\n if (m.task) bits.push(`task=${m.task}`);\n if (m.mode) bits.push(`mode=${m.mode}`);\n if (m.repeat !== undefined) bits.push(`repeat=${m.repeat}`);\n bits.push(`started=${m.startedAt}`);\n console.log(`[meta] ${bits.join(\" \")}`);\n console.log(\"\");\n }\n\n const records = sliceRecords(parsed.records, opts);\n for (const rec of records) {\n renderRecord(rec);\n }\n\n console.log(\"\");\n console.log(\"── summary ─────────────────────────────────────────\");\n console.log(`model calls: ${stats.turns}`);\n console.log(`user turns: ${stats.userTurns}`);\n console.log(`tool calls: ${stats.toolCalls}`);\n console.log(`cache hit: ${(stats.cacheHitRatio * 100).toFixed(1)}%`);\n console.log(`cost: $${stats.totalCostUsd.toFixed(6)}`);\n console.log(`claude equivalent: $${stats.claudeEquivalentUsd.toFixed(6)}`);\n console.log(`savings vs claude: ${stats.savingsVsClaudePct.toFixed(1)}%`);\n console.log(`models: ${stats.models.join(\", \") || \"—\"}`);\n console.log(`prefix hashes: ${stats.prefixHashes.length} distinct`);\n if (stats.prefixHashes.length === 1) {\n console.log(` (byte-stable prefix: ${stats.prefixHashes[0]?.slice(0, 16)}…)`);\n } else if (stats.prefixHashes.length > 1) {\n console.log(\" (prefix churned — cache-hostile session)\");\n }\n if (stats.harvestedTurns > 0) {\n console.log(`harvest (Pillar 2): ${stats.harvestedTurns} turn(s) produced plan state`);\n console.log(` subgoals: ${stats.totalSubgoals}`);\n console.log(` uncertainties: ${stats.totalUncertainties}`);\n }\n}\n\nfunction sliceRecords(records: TranscriptRecord[], opts: ReplayOptions): TranscriptRecord[] {\n if (opts.head !== undefined && opts.head > 0) return records.slice(0, opts.head);\n if (opts.tail !== undefined && opts.tail > 0) return records.slice(-opts.tail);\n return records;\n}\n\nfunction renderRecord(rec: TranscriptRecord): void {\n const turn = `[t${rec.turn}]`;\n if (rec.role === \"user\") {\n console.log(`${turn} USER: ${oneLine(rec.content)}`);\n } else if (rec.role === \"assistant_final\") {\n const cost = rec.cost !== undefined ? ` $${rec.cost.toFixed(6)}` : \"\";\n const cache =\n rec.usage &&\n (rec.usage.prompt_cache_hit_tokens !== undefined ||\n rec.usage.prompt_cache_miss_tokens !== undefined)\n ? (() => {\n const hit = rec.usage!.prompt_cache_hit_tokens ?? 0;\n const miss = rec.usage!.prompt_cache_miss_tokens ?? 0;\n const total = hit + miss;\n return total > 0 ? ` cache=${((hit / total) * 100).toFixed(1)}%` : \"\";\n })()\n : \"\";\n console.log(`${turn} AGENT:${cost}${cache} ${oneLine(rec.content)}`);\n if (rec.planState) {\n const ps = rec.planState;\n if (ps.subgoals.length)\n console.log(` ‹ subgoals (${ps.subgoals.length}): ${ps.subgoals.join(\" · \")}`);\n if (ps.hypotheses.length)\n console.log(` ‹ hypotheses (${ps.hypotheses.length}): ${ps.hypotheses.join(\" · \")}`);\n if (ps.uncertainties.length)\n console.log(\n ` ‹ uncertainties(${ps.uncertainties.length}): ${ps.uncertainties.join(\" · \")}`,\n );\n if (ps.rejectedPaths.length)\n console.log(\n ` ‹ rejected (${ps.rejectedPaths.length}): ${ps.rejectedPaths.join(\" · \")}`,\n );\n }\n } else if (rec.role === \"tool\") {\n const args = rec.args ? ` args=${oneLine(rec.args, 80)}` : \"\";\n console.log(`${turn} TOOL ${rec.tool ?? \"?\"}:${args} → ${oneLine(rec.content, 120)}`);\n } else if (rec.role === \"error\") {\n console.log(`${turn} ERROR: ${rec.error ?? rec.content}`);\n } else if (rec.role === \"done\") {\n // Suppress — visually noisy, not informative in replay.\n } else {\n console.log(`${turn} ${rec.role}: ${oneLine(rec.content)}`);\n }\n}\n\nfunction oneLine(s: string, max = 200): string {\n const collapsed = s.replace(/\\s+/g, \" \").trim();\n return collapsed.length > max ? `${collapsed.slice(0, max)}…` : collapsed;\n}\n","/**\n * Ink TUI for `reasonix replay`. Read-only: no input box, no loop.\n * j/k navigation across turn-pages, cumulative stats sidebar updates\n * as you move through time.\n *\n * The navigation logic (grouping records into pages, computing cumulative\n * stats) lives in src/replay.ts as pure functions; this file is just\n * presentation + key bindings.\n */\n\nimport { Box, Static, Text, useApp, useInput } from \"ink\";\nimport React, { useMemo, useState } from \"react\";\nimport { type TurnPage, computeCumulativeStats } from \"../../replay.js\";\nimport type { TranscriptMeta } from \"../../transcript.js\";\nimport { RecordView } from \"./RecordView.js\";\nimport { StatsPanel } from \"./StatsPanel.js\";\n\nexport interface ReplayAppProps {\n meta: TranscriptMeta | null;\n pages: TurnPage[];\n}\n\nexport function ReplayApp({ meta, pages }: ReplayAppProps) {\n const { exit } = useApp();\n const maxIdx = Math.max(0, pages.length - 1);\n // Start at the last page — more useful than \"start from the beginning\"\n // in practice: users mostly want to see the summary + last turn first.\n const [idx, setIdx] = useState(maxIdx);\n\n useInput((input, key) => {\n if (input === \"q\" || (key.ctrl && input === \"c\")) {\n exit();\n return;\n }\n if (input === \"j\" || key.downArrow || input === \" \" || key.return) {\n setIdx((i) => Math.min(maxIdx, i + 1));\n } else if (input === \"k\" || key.upArrow) {\n setIdx((i) => Math.max(0, i - 1));\n } else if (input === \"g\") {\n setIdx(0);\n } else if (input === \"G\") {\n setIdx(maxIdx);\n } else if (input === \"h\" || key.leftArrow) {\n setIdx(0);\n } else if (input === \"l\" || key.rightArrow) {\n setIdx(maxIdx);\n }\n });\n\n const cumStats = useMemo(() => computeCumulativeStats(pages, idx), [pages, idx]);\n\n const summary = {\n turns: cumStats.turns,\n totalCostUsd: cumStats.totalCostUsd,\n totalInputCostUsd: cumStats.totalInputCostUsd,\n totalOutputCostUsd: cumStats.totalOutputCostUsd,\n claudeEquivalentUsd: cumStats.claudeEquivalentUsd,\n savingsVsClaudePct: cumStats.savingsVsClaudePct,\n cacheHitRatio: cumStats.cacheHitRatio,\n // Replay is read-only — no live last-turn prompt tokens to show.\n lastPromptTokens: 0,\n };\n\n const prefixHash =\n cumStats.prefixHashes.length === 1\n ? cumStats.prefixHashes[0]!.slice(0, 16)\n : cumStats.prefixHashes.length === 0\n ? \"(untracked)\"\n : `(churned ×${cumStats.prefixHashes.length})`;\n\n const currentPage = pages[idx];\n const progressLabel =\n pages.length === 0 ? \"empty transcript\" : `turn ${idx + 1} / ${pages.length}`;\n\n return (\n <Box flexDirection=\"column\">\n <StatsPanel\n summary={summary}\n model={cumStats.models[0] ?? meta?.model ?? \"?\"}\n prefixHash={prefixHash}\n />\n\n <Box flexDirection=\"column\" marginTop={1} paddingX={1}>\n <Box justifyContent=\"space-between\">\n <Text color=\"cyan\" bold>\n {progressLabel}\n </Text>\n {meta ? (\n <Text dimColor>\n {meta.source}\n {meta.task ? ` · ${meta.task}` : \"\"}\n {meta.mode ? ` · ${meta.mode}` : \"\"}\n </Text>\n ) : null}\n </Box>\n\n {currentPage ? (\n <Static items={currentPage.records.map((rec, i) => ({ key: `${idx}-${i}`, rec }))}>\n {({ key, rec }) => <RecordView key={key} rec={rec} />}\n </Static>\n ) : (\n <Text dimColor italic>\n no records\n </Text>\n )}\n </Box>\n\n <Box marginTop={1} paddingX={1} borderStyle=\"single\" borderColor=\"gray\">\n <Text dimColor>\n <Text bold>j</Text>/<Text bold>↓</Text>/<Text bold>space</Text> next · <Text bold>k</Text>\n /<Text bold>↑</Text> prev · <Text bold>g</Text> first · <Text bold>G</Text> last ·{\" \"}\n <Text bold>q</Text> quit\n </Text>\n </Box>\n </Box>\n );\n}\n","import type { WriteStream } from \"node:fs\";\nimport { stdin, stdout } from \"node:process\";\nimport { createInterface } from \"node:readline/promises\";\nimport { defaultConfigPath, isPlausibleKey, loadApiKey, saveApiKey } from \"../../config.js\";\nimport { loadDotenv } from \"../../env.js\";\nimport { CacheFirstLoop, DeepSeekClient, ImmutablePrefix } from \"../../index.js\";\nimport { McpClient } from \"../../mcp/client.js\";\nimport { bridgeMcpTools } from \"../../mcp/registry.js\";\nimport { parseMcpSpec } from \"../../mcp/spec.js\";\nimport { SseTransport } from \"../../mcp/sse.js\";\nimport { type McpTransport, StdioTransport } from \"../../mcp/stdio.js\";\nimport { ToolRegistry } from \"../../tools.js\";\nimport { openTranscriptFile, recordFromLoopEvent, writeRecord } from \"../../transcript.js\";\n\nexport interface RunOptions {\n task: string;\n model: string;\n system: string;\n /** Opt into Pillar 2 plan-state harvesting. Adds a cheap V3 call per turn. */\n harvest?: boolean;\n /** Self-consistency budget. > 1 runs N parallel samples and picks the best. */\n branch?: number;\n /** JSONL transcript path — lets `reasonix replay` / `diff` audit this run. */\n transcript?: string;\n /** Zero or more MCP server specs. Each: `\"name=cmd args...\"` or `\"cmd args...\"`. */\n mcp?: string[];\n /** Global prefix — only honored when a single anonymous server is given. */\n mcpPrefix?: string;\n}\n\nasync function ensureApiKey(): Promise<string> {\n const existing = loadApiKey();\n if (existing) return existing;\n\n if (!stdin.isTTY) {\n process.stderr.write(\n \"DEEPSEEK_API_KEY is not set and stdin is not a TTY (cannot prompt).\\n\" +\n \"Set the env var, or run `reasonix chat` once interactively to save a key.\\n\",\n );\n process.exit(1);\n }\n\n process.stdout.write(\n \"DeepSeek API key not configured.\\nGet one at https://platform.deepseek.com/api_keys\\n\",\n );\n const rl = createInterface({ input: stdin, output: stdout });\n try {\n while (true) {\n const answer = (await rl.question(\"API key › \")).trim();\n if (!answer) continue;\n if (!isPlausibleKey(answer)) {\n process.stdout.write(\"Invalid format. Keys start with 'sk-' and are 30+ chars.\\n\");\n continue;\n }\n saveApiKey(answer);\n process.stdout.write(`Saved to ${defaultConfigPath()}\\n\\n`);\n return answer;\n }\n } finally {\n rl.close();\n }\n}\n\nexport async function runCommand(opts: RunOptions): Promise<void> {\n loadDotenv();\n const apiKey = await ensureApiKey();\n process.env.DEEPSEEK_API_KEY = apiKey;\n\n // Optional MCP setup — mirrors chat's flow. Must happen before loop\n // construction so the tools make it into the prefix.\n const requestedSpecs = opts.mcp ?? [];\n const clients: McpClient[] = [];\n let tools: ToolRegistry | undefined;\n let successCount = 0;\n if (requestedSpecs.length > 0) {\n tools = new ToolRegistry();\n for (const raw of requestedSpecs) {\n try {\n const spec = parseMcpSpec(raw);\n const prefix = spec.name\n ? `${spec.name}_`\n : requestedSpecs.length === 1 && opts.mcpPrefix\n ? opts.mcpPrefix\n : \"\";\n const transport: McpTransport =\n spec.transport === \"sse\"\n ? new SseTransport({ url: spec.url })\n : new StdioTransport({ command: spec.command, args: spec.args });\n const mcp = new McpClient({ transport });\n await mcp.initialize();\n const bridge = await bridgeMcpTools(mcp, { registry: tools, namePrefix: prefix });\n const source =\n spec.transport === \"sse\" ? spec.url : `${spec.command} ${spec.args.join(\" \")}`;\n process.stderr.write(\n `▸ MCP[${spec.name ?? \"anon\"}]: ${bridge.registeredNames.length} tool(s) from ${source}\\n`,\n );\n clients.push(mcp);\n successCount++;\n } catch (err) {\n // Non-fatal — skip and continue, same as `reasonix chat`. A\n // one-shot `run` invocation with a broken MCP server otherwise\n // fails the whole run over a side-concern tool the task might\n // not even touch.\n process.stderr.write(\n `▸ MCP setup SKIPPED for \"${raw}\": ${(err as Error).message}\\n → run \\`reasonix setup\\` to remove broken entries from your saved config.\\n`,\n );\n }\n }\n if (successCount === 0) tools = undefined;\n }\n\n const client = new DeepSeekClient();\n const prefix = new ImmutablePrefix({\n system: opts.system,\n toolSpecs: tools?.specs(),\n });\n const loop = new CacheFirstLoop({\n client,\n prefix,\n tools,\n model: opts.model,\n harvest: opts.harvest,\n branch: opts.branch,\n });\n const prefixHash = prefix.fingerprint;\n\n let transcriptStream: WriteStream | null = null;\n if (opts.transcript) {\n transcriptStream = openTranscriptFile(opts.transcript, {\n version: 1,\n source: \"reasonix run\",\n model: opts.model,\n startedAt: new Date().toISOString(),\n });\n // Also persist the user turn itself (the loop's event stream starts with\n // assistant output, not the prompt we're about to send).\n writeRecord(transcriptStream, {\n ts: new Date().toISOString(),\n turn: 1,\n role: \"user\",\n content: opts.task,\n });\n }\n\n try {\n for await (const ev of loop.step(opts.task)) {\n if (ev.role === \"assistant_delta\" && ev.content) process.stdout.write(ev.content);\n if (ev.role === \"tool\") process.stdout.write(`\\n[tool ${ev.toolName}] ${ev.content}\\n`);\n if (ev.role === \"error\") process.stderr.write(`\\n[error] ${ev.error}\\n`);\n if (ev.role === \"done\") process.stdout.write(\"\\n\");\n // Persist every non-streaming event — deltas would flood the file and\n // aren't useful for replay (replay renders final content, not keystrokes).\n if (transcriptStream && ev.role !== \"assistant_delta\") {\n writeRecord(transcriptStream, recordFromLoopEvent(ev, { model: opts.model, prefixHash }));\n }\n }\n } finally {\n transcriptStream?.end();\n }\n\n const s = loop.stats.summary();\n process.stdout.write(\n `\\n— turns:${s.turns} cache:${(s.cacheHitRatio * 100).toFixed(1)}% ` +\n `cost:$${s.totalCostUsd.toFixed(6)} save-vs-claude:${s.savingsVsClaudePct.toFixed(1)}%\\n`,\n );\n if (opts.transcript) {\n process.stdout.write(`\\ntranscript: ${opts.transcript}\\n`);\n process.stdout.write(` → npx reasonix replay ${opts.transcript}\\n`);\n }\n\n for (const c of clients) await c.close();\n}\n","/**\n * `reasonix sessions` — CLI equivalent of the `/sessions` slash command.\n *\n * Two modes:\n * - `reasonix sessions` list every session under ~/.reasonix/sessions/\n * - `reasonix sessions <name>` dump one session's messages in readable form\n *\n * Neither needs an API key — pure filesystem reads of JSONL files written\n * by previous chat runs.\n */\n\nimport { listSessions, loadSessionMessages, sessionPath } from \"../../index.js\";\nimport type { ChatMessage } from \"../../index.js\";\n\nexport interface SessionsOptions {\n /** When present, inspect that session instead of listing. */\n name?: string;\n /** Include assistant tool-call metadata in the inspect output. */\n verbose?: boolean;\n}\n\nexport function sessionsCommand(opts: SessionsOptions): void {\n if (opts.name) {\n inspectSession(opts.name, !!opts.verbose);\n } else {\n listAll();\n }\n}\n\nfunction listAll(): void {\n const items = listSessions();\n if (items.length === 0) {\n console.log(\n \"no saved sessions yet — run `reasonix chat` (sessions are auto-saved unless --no-session).\",\n );\n return;\n }\n console.log(\"Saved sessions (~/.reasonix/sessions/):\");\n console.log(\"\");\n console.log(` ${\"name\".padEnd(22)} ${\"msgs\".padStart(6)} ${\"size\".padStart(8)} modified`);\n console.log(` ${\"─\".repeat(60)}`);\n for (const s of items) {\n const sizeKb = `${(s.size / 1024).toFixed(1)} KB`;\n const when = s.mtime.toISOString().replace(\"T\", \" \").slice(0, 16);\n console.log(\n ` ${s.name.padEnd(22)} ${String(s.messageCount).padStart(6)} ${sizeKb.padStart(8)} ${when}`,\n );\n }\n console.log(\"\");\n console.log(\"Inspect: reasonix sessions <name>\");\n console.log(\"Resume: reasonix chat --session <name>\");\n}\n\nfunction inspectSession(name: string, verbose: boolean): void {\n const path = sessionPath(name);\n const messages = loadSessionMessages(name);\n if (messages.length === 0) {\n console.error(`no session named \"${name}\" (or it's empty).`);\n console.error(`looked at: ${path}`);\n process.exit(1);\n }\n\n console.log(`[session] ${name} ${messages.length} messages ${path}`);\n console.log(\"\");\n\n let turnIndex = 0;\n for (const msg of messages) {\n renderMessage(msg, turnIndex, verbose);\n // Roughly bump \"turn\" after each user message so the reader can follow\n // the conversation shape without the transcript's richer turn numbering.\n if (msg.role === \"user\") turnIndex++;\n }\n}\n\nfunction renderMessage(msg: ChatMessage, turnIdx: number, verbose: boolean): void {\n const turn = turnIdx > 0 ? `[t${turnIdx}]` : \"[start]\";\n const content = typeof msg.content === \"string\" ? msg.content : \"\";\n const flat = oneLine(content);\n\n if (msg.role === \"user\") {\n console.log(`${turn} USER: ${flat}`);\n } else if (msg.role === \"assistant\") {\n console.log(`${turn} AGENT: ${flat || \"(tool call only)\"}`);\n if (verbose && msg.tool_calls?.length) {\n for (const tc of msg.tool_calls) {\n console.log(\n ` → call ${tc.function?.name} ${truncate(tc.function?.arguments ?? \"\", 80)}`,\n );\n }\n }\n } else if (msg.role === \"tool\") {\n console.log(`${turn} TOOL ${msg.name ?? \"?\"}: ${truncate(flat, 160)}`);\n } else if (msg.role === \"system\") {\n if (verbose) console.log(`${turn} SYSTEM: ${truncate(flat, 160)}`);\n // otherwise suppress — session's system prompt is usually session-wide\n // boilerplate.\n }\n}\n\nfunction oneLine(s: string, max = 200): string {\n const collapsed = s.replace(/\\s+/g, \" \").trim();\n return collapsed.length > max ? `${collapsed.slice(0, max)}…` : collapsed;\n}\n\nfunction truncate(s: string, max: number): string {\n return s.length <= max ? s : `${s.slice(0, max)}…`;\n}\n","/**\n * `reasonix setup` — re-mount the first-run wizard on demand so users\n * can reconfigure (add/remove MCP servers, switch preset) without\n * editing JSON by hand.\n *\n * Invoked both explicitly (`reasonix setup`) and implicitly (the no-args\n * entry point when `setupCompleted` is false).\n */\n\nimport { render } from \"ink\";\nimport React from \"react\";\nimport { loadApiKey, readConfig } from \"../../config.js\";\nimport { loadDotenv } from \"../../env.js\";\nimport { Wizard } from \"../ui/Wizard.js\";\n\nexport interface SetupOptions {\n /**\n * When true, bypass the API-key step even if no key is saved — useful\n * from test harnesses. Normal CLI use always pushes through the key\n * step when missing.\n */\n skipKeyStep?: boolean;\n}\n\nexport async function setupCommand(_opts: SetupOptions = {}): Promise<void> {\n loadDotenv();\n const existingKey = loadApiKey();\n const existing = readConfig();\n\n const { waitUntilExit, unmount } = render(\n <Wizard\n existingApiKey={existingKey}\n initial={{ preset: existing.preset, mcp: existing.mcp }}\n onComplete={() => {\n // Ink handles its own enter-to-exit inside the \"saved\" step; we\n // just wait for the app to exit naturally.\n }}\n onCancel={() => {\n unmount();\n }}\n />,\n { exitOnCtrlC: true, patchConsole: false },\n );\n await waitUntilExit();\n}\n","/**\n * First-run / re-configure wizard.\n *\n * Walks a new user through: API key → preset pick → MCP server pick →\n * per-server args → save. Saved output lives in `~/.reasonix/config.json`\n * so the next `reasonix chat` starts with everything already wired.\n *\n * The wizard is the antidote to \"too many CLI flags\" — a new user should\n * never have to read `--help` to get MCP + a sensible model combo\n * working. Everything a user could set via `--mcp`, `--harvest`,\n * `--branch`, etc. can be picked here in a few arrow-key presses.\n */\n\nimport { Box, Text, useApp, useInput } from \"ink\";\nimport TextInput from \"ink-text-input\";\n// biome-ignore lint/style/useImportType: JSX (jsx: \"react\") needs React as a value at runtime\nimport React, { useState } from \"react\";\nimport {\n type PresetName,\n type ReasonixConfig,\n defaultConfigPath,\n isPlausibleKey,\n readConfig,\n redactKey,\n writeConfig,\n} from \"../../config.js\";\nimport { type CatalogEntry, MCP_CATALOG } from \"../../mcp/catalog.js\";\nimport { MultiSelect, type SelectItem, SingleSelect } from \"./Select.js\";\nimport { PRESET_DESCRIPTIONS } from \"./presets.js\";\n\nexport interface WizardProps {\n /** Called once the config has been saved. */\n onComplete: (cfg: ReasonixConfig) => void;\n /** Called if the user presses Esc to abort. */\n onCancel?: () => void;\n /** Skip the API-key step if a key already exists (env or config). */\n existingApiKey?: string;\n /** Pre-fill selections when re-running (reconfigure flow). */\n initial?: {\n preset?: PresetName;\n mcp?: string[];\n };\n}\n\ntype Step = \"apiKey\" | \"preset\" | \"mcp\" | \"mcpArgs\" | \"review\" | \"saved\";\n\ninterface WizardData {\n apiKey: string;\n preset: PresetName;\n selectedCatalog: string[]; // entries from MCP_CATALOG by `name`\n /** Captured user inputs per catalog entry that has `userArgs` (e.g. fs dir). */\n catalogArgs: Record<string, string>;\n}\n\nconst CATALOG_BY_NAME = new Map(MCP_CATALOG.map((e) => [e.name, e]));\n\nexport function Wizard({ onComplete, onCancel, existingApiKey, initial }: WizardProps) {\n const { exit } = useApp();\n const [step, setStep] = useState<Step>(existingApiKey ? \"preset\" : \"apiKey\");\n const [data, setData] = useState<WizardData>({\n apiKey: existingApiKey ?? \"\",\n preset: initial?.preset ?? \"fast\",\n selectedCatalog: deriveInitialCatalog(initial?.mcp ?? []),\n catalogArgs: {},\n });\n const [error, setError] = useState<string | null>(null);\n\n // Global Esc → cancel. Disabled once we've started saving to avoid\n // ejecting out of a half-written state.\n useInput((_input, key) => {\n if (key.escape && step !== \"saved\" && onCancel) onCancel();\n });\n\n if (step === \"apiKey\") {\n return (\n <ApiKeyStep\n onSubmit={(key) => {\n setData((d) => ({ ...d, apiKey: key }));\n setError(null);\n setStep(\"preset\");\n }}\n error={error}\n onError={setError}\n />\n );\n }\n\n if (step === \"preset\") {\n return (\n <StepFrame title=\"Pick a preset\" step={1} total={3}>\n <SingleSelect<PresetName>\n items={presetItems()}\n initialValue={data.preset}\n onSubmit={(preset) => {\n setData((d) => ({ ...d, preset }));\n setStep(\"mcp\");\n }}\n />\n <Box marginTop={1}>\n <Text dimColor>↑/↓ move · enter confirm · esc cancel</Text>\n </Box>\n </StepFrame>\n );\n }\n\n if (step === \"mcp\") {\n return (\n <StepFrame title=\"Which MCP servers should Reasonix wire up for you?\" step={2} total={3}>\n <MultiSelect\n items={mcpItems()}\n initialSelected={data.selectedCatalog}\n onSubmit={(selected) => {\n setData((d) => ({ ...d, selectedCatalog: selected }));\n // Only advance to the args step if any selected entry needs args.\n const needsArgs = selected.some((name) => CATALOG_BY_NAME.get(name)?.userArgs);\n setStep(needsArgs ? \"mcpArgs\" : \"review\");\n }}\n footer=\"↑/↓ move · space toggle · enter confirm · esc cancel · leave empty to skip\"\n />\n </StepFrame>\n );\n }\n\n if (step === \"mcpArgs\") {\n const pending = data.selectedCatalog.filter((name) => {\n const entry = CATALOG_BY_NAME.get(name);\n return entry?.userArgs && !data.catalogArgs[name];\n });\n if (pending.length === 0) {\n setStep(\"review\");\n return null;\n }\n const currentName = pending[0]!;\n const entry = CATALOG_BY_NAME.get(currentName)!;\n return (\n <McpArgsStep\n entry={entry}\n error={error}\n onSubmit={(value) => {\n setData((d) => ({\n ...d,\n catalogArgs: { ...d.catalogArgs, [currentName]: value },\n }));\n setError(null);\n }}\n onError={setError}\n />\n );\n }\n\n if (step === \"review\") {\n const specs = data.selectedCatalog.map((name) => buildSpec(name, data.catalogArgs));\n return (\n <StepFrame title=\"Ready to save\" step={3} total={3}>\n <Box flexDirection=\"column\">\n <SummaryLine label=\"API key\" value={redactKey(data.apiKey)} />\n <SummaryLine label=\"Preset\" value={data.preset} />\n <SummaryLine\n label=\"MCP\"\n value={specs.length === 0 ? \"(none)\" : `${specs.length} server(s)`}\n />\n {specs.map((spec, i) => (\n // biome-ignore lint/suspicious/noArrayIndexKey: review-only render, order fixed\n <Box key={i} paddingLeft={14}>\n <Text dimColor>· {spec}</Text>\n </Box>\n ))}\n <Box marginTop={1}>\n <Text>Saves to {defaultConfigPath()}</Text>\n </Box>\n {error ? (\n <Box marginTop={1}>\n <Text color=\"red\">{error}</Text>\n </Box>\n ) : null}\n <Box marginTop={1}>\n <Text dimColor>enter save · esc cancel</Text>\n </Box>\n </Box>\n <ReviewConfirm\n onConfirm={() => {\n try {\n const specsNow = data.selectedCatalog.map((name) =>\n buildSpec(name, data.catalogArgs),\n );\n const prev = readConfig();\n const next: ReasonixConfig = {\n ...prev,\n apiKey: data.apiKey,\n preset: data.preset,\n mcp: specsNow,\n setupCompleted: true,\n };\n writeConfig(next);\n setStep(\"saved\");\n onComplete(next);\n } catch (e) {\n setError(`Could not save config: ${(e as Error).message}`);\n }\n }}\n />\n </StepFrame>\n );\n }\n\n // saved\n return (\n <Box flexDirection=\"column\" borderStyle=\"round\" borderColor=\"green\" paddingX={1}>\n <Text bold color=\"green\">\n ▸ Saved.\n </Text>\n <Box marginTop={1}>\n <Text>Run `reasonix` any time to start chatting — your settings are remembered.</Text>\n </Box>\n <Box marginTop={1}>\n <Text dimColor>Press enter to exit.</Text>\n </Box>\n <ExitOnEnter onExit={exit} />\n </Box>\n );\n}\n\n// ---------- step components ----------\n\nfunction ApiKeyStep({\n onSubmit,\n error,\n onError,\n}: {\n onSubmit: (key: string) => void;\n error: string | null;\n onError: (e: string | null) => void;\n}) {\n const [value, setValue] = useState(\"\");\n return (\n <Box flexDirection=\"column\" borderStyle=\"round\" borderColor=\"cyan\" paddingX={1}>\n <Text bold color=\"cyan\">\n Welcome to Reasonix.\n </Text>\n <Box marginTop={1}>\n <Text>Paste your DeepSeek API key to get started.</Text>\n </Box>\n <Text dimColor>Get one (free credit on signup): https://platform.deepseek.com/api_keys</Text>\n <Text dimColor>Saved locally to {defaultConfigPath()}</Text>\n <Box marginTop={1}>\n <Text bold color=\"cyan\">\n {\"key › \"}\n </Text>\n <TextInput\n value={value}\n onChange={setValue}\n onSubmit={(raw) => {\n const trimmed = raw.trim();\n if (!isPlausibleKey(trimmed)) {\n onError(\"Doesn't look like a DeepSeek key. They start with 'sk-' and are 30+ chars.\");\n setValue(\"\");\n return;\n }\n onSubmit(trimmed);\n }}\n mask=\"•\"\n placeholder=\"sk-...\"\n />\n </Box>\n {error ? (\n <Box marginTop={1}>\n <Text color=\"red\">{error}</Text>\n </Box>\n ) : value ? (\n <Box marginTop={1}>\n <Text dimColor>preview: {redactKey(value)}</Text>\n </Box>\n ) : null}\n </Box>\n );\n}\n\nfunction McpArgsStep({\n entry,\n error,\n onSubmit,\n onError,\n}: {\n entry: CatalogEntry;\n error: string | null;\n onSubmit: (value: string) => void;\n onError: (e: string | null) => void;\n}) {\n const [value, setValue] = useState(\"\");\n return (\n <StepFrame title={`Configure ${entry.name}`} step={2} total={3}>\n <Box flexDirection=\"column\">\n <Text>{entry.summary}</Text>\n {entry.note ? (\n <Box marginTop={1}>\n <Text dimColor>{entry.note}</Text>\n </Box>\n ) : null}\n <Box marginTop={1}>\n <Text>Required parameter: </Text>\n <Text bold>{entry.userArgs}</Text>\n </Box>\n <Box marginTop={1}>\n <Text bold color=\"cyan\">\n {entry.userArgs}\n {\" › \"}\n </Text>\n <TextInput\n value={value}\n onChange={setValue}\n onSubmit={(raw) => {\n const trimmed = raw.trim();\n if (!trimmed) {\n onError(`${entry.name} needs a value — got an empty string.`);\n return;\n }\n onSubmit(trimmed);\n setValue(\"\");\n }}\n placeholder={placeholderFor(entry)}\n />\n </Box>\n {error ? (\n <Box marginTop={1}>\n <Text color=\"red\">{error}</Text>\n </Box>\n ) : null}\n </Box>\n </StepFrame>\n );\n}\n\nfunction ReviewConfirm({ onConfirm }: { onConfirm: () => void }) {\n useInput((_i, key) => {\n if (key.return) onConfirm();\n });\n return null;\n}\n\nfunction ExitOnEnter({ onExit }: { onExit: () => void }) {\n useInput((_i, key) => {\n if (key.return) onExit();\n });\n return null;\n}\n\n// ---------- small bits ----------\n\nfunction StepFrame({\n title,\n step,\n total,\n children,\n}: {\n title: string;\n step: number;\n total: number;\n children: React.ReactNode;\n}) {\n return (\n <Box flexDirection=\"column\" borderStyle=\"round\" borderColor=\"cyan\" paddingX={1}>\n <Box>\n <Text dimColor>\n Step {step}/{total} ·{\" \"}\n </Text>\n <Text bold color=\"cyan\">\n {title}\n </Text>\n </Box>\n <Box marginTop={1} flexDirection=\"column\">\n {children}\n </Box>\n </Box>\n );\n}\n\nfunction SummaryLine({ label, value }: { label: string; value: string }) {\n return (\n <Box>\n <Text>{label.padEnd(12)}</Text>\n <Text bold>{value}</Text>\n </Box>\n );\n}\n\n// ---------- data helpers ----------\n\nfunction presetItems(): SelectItem<PresetName>[] {\n return ([\"fast\", \"smart\", \"max\"] as PresetName[]).map((name) => ({\n value: name,\n label: `${name} — ${PRESET_DESCRIPTIONS[name].headline}`,\n hint: PRESET_DESCRIPTIONS[name].cost,\n }));\n}\n\nfunction mcpItems(): SelectItem<string>[] {\n return MCP_CATALOG.map((entry) => {\n const hintParts: string[] = [entry.summary];\n if (entry.userArgs) hintParts.push(`(you'll provide ${entry.userArgs})`);\n if (entry.note) hintParts.push(entry.note);\n return {\n value: entry.name,\n label: entry.name,\n hint: hintParts.join(\" · \"),\n };\n });\n}\n\nfunction placeholderFor(entry: CatalogEntry): string {\n if (entry.name === \"filesystem\") return \"e.g. /tmp/reasonix-sandbox\";\n if (entry.name === \"sqlite\") return \"e.g. ./notes.sqlite\";\n return entry.userArgs ?? \"\";\n}\n\nfunction deriveInitialCatalog(existingSpecs: string[]): string[] {\n // Best-effort recovery: if the user previously picked catalog entries,\n // the spec strings look like `name=npx -y <pkg> ...`. Match by package\n // so reconfigure pre-checks the same boxes.\n const packageToName = new Map(MCP_CATALOG.map((e) => [e.package, e.name]));\n const out: string[] = [];\n for (const spec of existingSpecs) {\n for (const [pkg, name] of packageToName) {\n if (spec.includes(pkg)) {\n out.push(name);\n break;\n }\n }\n }\n return out;\n}\n\n/**\n * Build the `--mcp` spec string for a catalog entry. Same format\n * `mcpCommandFor` produces for `reasonix mcp list`, minus the leading\n * `--mcp \"...\"` wrapper — we store the inner spec directly.\n */\nexport function buildSpec(name: string, argsByName: Record<string, string>): string {\n const entry = CATALOG_BY_NAME.get(name);\n if (!entry) return name; // shouldn't happen; fall back gracefully\n const userArg = entry.userArgs ? argsByName[name] : undefined;\n const tail = userArg ? ` ${quoteIfNeeded(userArg)}` : \"\";\n return `${entry.name}=npx -y ${entry.package}${tail}`;\n}\n\nfunction quoteIfNeeded(s: string): string {\n return /\\s|\"/.test(s) ? `\"${s.replace(/\"/g, '\\\\\"')}\"` : s;\n}\n","/**\n * One place that defines what each preset means. Both `slash.ts`\n * (in-chat `/preset`) and the wizard (first-run setup) read from here.\n *\n * Presets are the single vocabulary we teach new users: they don't need\n * to know model IDs, Pillar 2, branch budgets, or cost tradeoffs\n * independently — they pick \"fast / smart / max\" and we translate.\n */\n\nimport type { PresetName } from \"../../config.js\";\n\nexport interface PresetSettings {\n model: string;\n harvest: boolean;\n /** Branch budget. `1` means branching off. */\n branch: number;\n}\n\nexport const PRESETS: Record<PresetName, PresetSettings> = {\n fast: { model: \"deepseek-chat\", harvest: false, branch: 1 },\n smart: { model: \"deepseek-reasoner\", harvest: true, branch: 1 },\n max: { model: \"deepseek-reasoner\", harvest: true, branch: 3 },\n};\n\nexport const PRESET_DESCRIPTIONS: Record<PresetName, { headline: string; cost: string }> = {\n fast: {\n headline: \"deepseek-chat, no reasoning harvest, no branching\",\n cost: \"~1¢ per 100 turns · default\",\n },\n smart: {\n headline: \"deepseek-reasoner + Pillar 2 harvest\",\n cost: \"~10× cost vs fast · slower · better on multi-step tasks\",\n },\n max: {\n headline: \"reasoner + harvest + self-consistency (3 branches)\",\n cost: \"~30× cost vs fast · slowest · for hard single-shots\",\n },\n};\n\nexport function resolvePreset(name: PresetName | undefined): PresetSettings {\n return PRESETS[name ?? \"fast\"];\n}\n","import { existsSync, readFileSync } from \"node:fs\";\n\nexport interface StatsOptions {\n transcript: string;\n}\n\nexport function statsCommand(opts: StatsOptions): void {\n if (!existsSync(opts.transcript)) {\n console.error(`no such transcript: ${opts.transcript}`);\n process.exit(1);\n }\n const lines = readFileSync(opts.transcript, \"utf8\").split(/\\r?\\n/).filter(Boolean);\n let assistantTurns = 0;\n let toolCalls = 0;\n let lastTurn = 0;\n for (const line of lines) {\n try {\n const rec = JSON.parse(line);\n if (rec.role === \"assistant_final\") assistantTurns++;\n if (rec.role === \"tool\") toolCalls++;\n if (typeof rec.turn === \"number\") lastTurn = Math.max(lastTurn, rec.turn);\n } catch {\n /* skip */\n }\n }\n console.log(`transcript: ${opts.transcript}`);\n console.log(`assistant turns: ${assistantTurns}`);\n console.log(`tool invocations: ${toolCalls}`);\n console.log(`last turn index: ${lastTurn}`);\n}\n","/**\n * `reasonix update` — self-upgrade command.\n *\n * Talks to the npm registry, compares against the running version,\n * and either runs `npm install -g reasonix@latest` (global install)\n * or advises how to refresh the npx cache (ephemeral install).\n *\n * The decision logic is factored into `planUpdate()` so it can be\n * unit-tested without spawning child processes or hitting the\n * network. `updateCommand()` is the thin CLI wrapper that calls\n * `getLatestVersion`, passes the result into `planUpdate`, and\n * executes any suggested command.\n */\n\nimport { spawn } from \"node:child_process\";\nimport { VERSION, compareVersions, getLatestVersion, isNpxInstall } from \"../../version.js\";\n\nexport type UpdateAction = \"up-to-date\" | \"newer-local\" | \"npx-hint\" | \"run-npm-install\";\n\nexport interface UpdatePlan {\n action: UpdateAction;\n /** Human-readable summary; the CLI prints this verbatim. */\n message: string;\n /**\n * Argv for the install command when `action === \"run-npm-install\"`.\n * Absent otherwise. Kept as array so the CLI can pass it straight\n * to `spawn` without re-parsing shell syntax.\n */\n command?: string[];\n}\n\nexport interface PlanUpdateInput {\n current: string;\n latest: string;\n /** Overrides `isNpxInstall()` (tests). */\n npx?: boolean;\n}\n\n/**\n * Pure decision function: given current + latest + install kind,\n * decide what the CLI should do. No I/O.\n *\n * newer-local — current > latest (dev build, local publish)\n * up-to-date — current === latest\n * npx-hint — current < latest AND running under npx\n * run-npm-install — current < latest AND running as a real install\n */\nexport function planUpdate(input: PlanUpdateInput): UpdatePlan {\n const diff = compareVersions(input.current, input.latest);\n if (diff > 0) {\n return {\n action: \"newer-local\",\n message: `current (${input.current}) is newer than the published ${input.latest} — nothing to do.`,\n };\n }\n if (diff === 0) {\n return { action: \"up-to-date\", message: `reasonix ${input.current} is up to date.` };\n }\n if (input.npx) {\n return {\n action: \"npx-hint\",\n message: [\n `reasonix ${input.latest} is available.`,\n \"you're running via npx — the next `npx reasonix ...` launch will auto-fetch\",\n \"the latest (npx caches packages for a short window). to force a refresh\",\n \"sooner, clear the cache: `npm cache clean --force`.\",\n ].join(\"\\n\"),\n };\n }\n return {\n action: \"run-npm-install\",\n message: `upgrading reasonix ${input.current} → ${input.latest}`,\n command: [\"npm\", \"install\", \"-g\", \"reasonix@latest\"],\n };\n}\n\nexport interface UpdateCommandOptions {\n /** Skip spawning npm; print the decision only. */\n dryRun?: boolean;\n /** Test seam: override the registry lookup. Returns null = offline. */\n fetchLatest?: () => Promise<string | null>;\n /** Test seam: override the npx detector. */\n isNpx?: () => boolean;\n /** Test seam: override the spawner. Must return exit code. */\n spawnInstall?: (argv: string[]) => Promise<number>;\n /** Test seam: stdout writer. */\n write?: (msg: string) => void;\n /** Test seam: process exit — tests don't want to tear down vitest. */\n exit?: (code: number) => void;\n}\n\nfunction defaultSpawn(argv: string[]): Promise<number> {\n return new Promise((resolve, reject) => {\n // `shell: true` on Windows is what lets `npm` resolve to `npm.cmd`\n // without routing through our `prepareSpawn` helper. The args here\n // are literal strings under our control — no user input flows in,\n // so injection is not a concern. Avoiding `prepareSpawn` keeps\n // this command free of a dep on the shell tools module.\n const child = spawn(argv[0]!, argv.slice(1), {\n stdio: \"inherit\",\n shell: process.platform === \"win32\",\n });\n child.once(\"error\", reject);\n child.once(\"exit\", (code) => resolve(code ?? 1));\n });\n}\n\n/**\n * Run the update command. Prints a banner, resolves the latest\n * version, prints the plan, and (unless `--dry-run`) executes the\n * install when applicable.\n */\nexport async function updateCommand(opts: UpdateCommandOptions = {}): Promise<void> {\n const write = opts.write ?? ((m: string) => process.stdout.write(m));\n const exit = opts.exit ?? ((c: number) => process.exit(c));\n const fetchLatest = opts.fetchLatest ?? (() => getLatestVersion({ force: true }));\n const isNpx = opts.isNpx ?? isNpxInstall;\n const doSpawn = opts.spawnInstall ?? defaultSpawn;\n\n write(`current: reasonix ${VERSION}\\n`);\n const latest = await fetchLatest();\n if (!latest) {\n write(\"could not reach registry.npmjs.org — check your network.\\n\");\n exit(1);\n return;\n }\n write(`latest: reasonix ${latest}\\n`);\n\n const plan = planUpdate({ current: VERSION, latest, npx: isNpx() });\n write(`\\n${plan.message}\\n`);\n\n if (plan.action !== \"run-npm-install\" || !plan.command) return;\n if (opts.dryRun) {\n write(`(dry run) would run: ${plan.command.join(\" \")}\\n`);\n return;\n }\n write(`\\nrunning: ${plan.command.join(\" \")}\\n`);\n const code = await doSpawn(plan.command);\n if (code !== 0) {\n write(`\\nnpm exited with code ${code}. upgrade did not complete.\\n`);\n exit(code);\n }\n}\n","import { VERSION } from \"../../index.js\";\n\nexport function versionCommand(): void {\n console.log(`reasonix ${VERSION}`);\n}\n","/**\n * Merge config defaults with CLI flags into the concrete options that\n * `chatCommand` / `runCommand` need.\n *\n * Precedence (highest wins):\n * 1. Explicit per-setting CLI flag (`--model`, `--harvest`, `--branch`, `--mcp`)\n * 2. Explicit `--preset <name>` CLI flag\n * 3. `config.preset` from `~/.reasonix/config.json` (set by `reasonix setup`)\n * 4. Hardcoded \"fast\" preset defaults\n *\n * Keeping this logic in one place — rather than duplicating across\n * `chat` and `run` — means the precedence rule only lives in one unit\n * test and the shape of the merge is identical for both commands.\n */\n\nimport { type PresetName, type ReasonixConfig, readConfig } from \"../config.js\";\nimport { PRESETS } from \"./ui/presets.js\";\n\nexport interface ResolvedDefaults {\n model: string;\n harvest: boolean;\n branch: number | undefined;\n mcp: string[];\n session: string | undefined;\n}\n\nexport interface RawCliFlags {\n model?: string;\n harvest?: boolean;\n /** From `commander`; already parseInt'd. */\n branch?: number;\n mcp?: string[];\n /** Commander's `--no-session` surfaces as `false`; `--session X` as a string. */\n session?: string | false;\n /** `--preset <name>`. */\n preset?: string;\n /** When true, ignore config entirely (power-user escape hatch). */\n noConfig?: boolean;\n}\n\nexport function resolveDefaults(flags: RawCliFlags): ResolvedDefaults {\n const cfg: ReasonixConfig = flags.noConfig ? {} : readConfig();\n const preset = pickPreset(flags.preset, cfg.preset);\n const presetSettings = PRESETS[preset];\n\n const model = flags.model ?? presetSettings.model;\n const harvest = flags.harvest === true ? true : presetSettings.harvest;\n const branchFromFlag = normalizeBranch(flags.branch);\n const branch = branchFromFlag ?? (presetSettings.branch > 1 ? presetSettings.branch : undefined);\n\n // `--mcp` accumulator is [] when absent. Treat empty from flags as\n // \"user didn't pass\" → fall through to config. Users who explicitly\n // want zero MCP servers can pass `--no-config` or edit the file.\n const mcp = flags.mcp && flags.mcp.length > 0 ? flags.mcp : (cfg.mcp ?? []);\n\n const session = resolveSession(flags.session, cfg.session);\n\n return { model, harvest, branch, mcp, session };\n}\n\nfunction pickPreset(\n flagPreset: string | undefined,\n configPreset: PresetName | undefined,\n): PresetName {\n if (flagPreset && isPresetName(flagPreset)) return flagPreset;\n if (configPreset) return configPreset;\n return \"fast\";\n}\n\nfunction isPresetName(s: string): s is PresetName {\n return s === \"fast\" || s === \"smart\" || s === \"max\";\n}\n\nfunction normalizeBranch(raw: number | undefined): number | undefined {\n if (raw === undefined) return undefined;\n if (!Number.isFinite(raw) || raw <= 1) return undefined;\n return Math.min(raw, 8);\n}\n\nfunction resolveSession(\n flag: string | false | undefined,\n configSession: string | null | undefined,\n): string | undefined {\n if (flag === false) return undefined; // --no-session\n if (typeof flag === \"string\" && flag.length > 0) return flag;\n if (configSession === null) return undefined; // config opted out\n if (typeof configSession === \"string\" && configSession.length > 0) return configSession;\n return \"default\";\n}\n"],"mappings":";;;;;;;;;;;;AAAA,SAAS,eAAe;;;ACgBxB,SAAS,WAAW,WAAW,cAAc,qBAAqB;AAClE,SAAS,eAAe;AACxB,SAAS,SAAS,YAAY;AA+CvB,SAAS,oBAA4B;AAC1C,SAAO,KAAK,QAAQ,GAAG,aAAa,aAAa;AACnD;AAEO,SAAS,WAAW,OAAe,kBAAkB,GAAmB;AAC7E,MAAI;AACF,UAAM,MAAM,aAAa,MAAM,MAAM;AACrC,UAAM,SAAS,KAAK,MAAM,GAAG;AAC7B,QAAI,UAAU,OAAO,WAAW,SAAU,QAAO;AAAA,EACnD,QAAQ;AAAA,EAER;AACA,SAAO,CAAC;AACV;AAEO,SAAS,YAAY,KAAqB,OAAe,kBAAkB,GAAS;AACzF,YAAU,QAAQ,IAAI,GAAG,EAAE,WAAW,KAAK,CAAC;AAC5C,gBAAc,MAAM,KAAK,UAAU,KAAK,MAAM,CAAC,GAAG,MAAM;AAExD,MAAI;AACF,cAAU,MAAM,GAAK;AAAA,EACvB,QAAQ;AAAA,EAER;AACF;AAGO,SAAS,WAAW,OAAe,kBAAkB,GAAuB;AACjF,MAAI,QAAQ,IAAI,iBAAkB,QAAO,QAAQ,IAAI;AACrD,SAAO,WAAW,IAAI,EAAE;AAC1B;AAOO,SAAS,cAAc,OAAe,kBAAkB,GAAY;AACzE,QAAM,MAAM,QAAQ,IAAI;AACxB,MAAI,QAAQ,SAAS,QAAQ,WAAW,Q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/C,CAAC;AAEH,QAAQ,WAAW,QAAQ,IAAI,EAAE,MAAM,CAAC,QAAQ;AAC9C,UAAQ,MAAM,GAAG;AACjB,UAAQ,KAAK,CAAC;AAChB,CAAC;","names":["resolve","readFileSync","homedir","join","settings","resolve","stdout","stdin","signature","chmodSync","existsSync","mkdirSync","readFileSync","writeFileSync","homedir","dirname","join","resolve","walk","i","j","spawn","existsSync","statSync","pathMod","resolve","delimiter","readFileSync","resolve","readFileSync","records","round","p","resolve","spawn","resolve","createParser","resolve","existsSync","mkdirSync","readFileSync","unlinkSync","writeFileSync","dirname","resolve","existsSync","mkdirSync","readFileSync","writeFileSync","homedir","dirname","join","existsSync","statSync","React","useState","Box","Text","useInput","React","useEffect","useRef","useState","Box","Text","React","Box","Text","React","pad","row","React","React","EventRow","Box","Text","truncate","pct","Box","Text","React","Box","Text","React","useState","React","Box","Text","Box","Text","useInput","React","useState","Box","Text","useInput","React","useState","useState","useInput","React","Box","Text","Box","Text","React","React","Box","Text","Box","Text","React","Box","Text","React","React","Box","Text","lines","name","skill","stdout","harvest","useState","useRef","useEffect","useInput","React","Box","Text","pct","countLines","Box","Text","React","React","Box","Text","Box","Text","useApp","React","useState","useState","useApp","React","Box","Text","useState","React","mcp","existsSync","statSync","resolve","codeSystemPrompt","resolve","writeFileSync","basename","render","React","Box","Static","Text","useApp","useInput","React","useState","Box","Text","React","React","Box","Text","truncate","pct","useApp","useState","useInput","React","Box","Text","costDelta","Static","basename","writeFileSync","render","React","render","render","React","Box","Static","Text","useApp","useInput","React","useMemo","useState","useApp","useState","useInput","useMemo","React","Box","Text","Static","render","React","oneLine","runCommand","prefix","mcp","oneLine","truncate","render","React","Box","Text","useApp","useInput","TextInput","React","useState","useApp","useState","useInput","React","Box","Text","entry","TextInput","render","React","existsSync","readFileSync","spawn","resolve","spawn","harvest","mcp","runCommand"]}
|
|
1
|
+
{"version":3,"sources":["../../src/cli/index.ts","../../src/config.ts","../../src/client.ts","../../src/retry.ts","../../src/harvest.ts","../../src/consistency.ts","../../src/hooks.ts","../../src/repair/flatten.ts","../../src/tools.ts","../../src/mcp/registry.ts","../../src/memory.ts","../../src/repair/scavenge.ts","../../src/repair/storm.ts","../../src/repair/truncation.ts","../../src/repair/index.ts","../../src/session.ts","../../src/telemetry.ts","../../src/loop.ts","../../src/tools/filesystem.ts","../../src/tools/memory.ts","../../src/tools/plan.ts","../../src/tools/subagent.ts","../../src/tools/shell.ts","../../src/tools/web.ts","../../src/env.ts","../../src/transcript.ts","../../src/replay.ts","../../src/diff.ts","../../src/mcp/types.ts","../../src/mcp/client.ts","../../src/mcp/stdio.ts","../../src/mcp/sse.ts","../../src/mcp/shell-split.ts","../../src/mcp/spec.ts","../../src/mcp/inspect.ts","../../src/code/edit-blocks.ts","../../src/version.ts","../../src/usage.ts","../../src/cli/commands/chat.tsx","../../src/cli/ui/App.tsx","../../src/tools/skills.ts","../../src/cli/ui/EventLog.tsx","../../src/cli/ui/PlanStateBlock.tsx","../../src/cli/ui/markdown.tsx","../../src/cli/ui/ticker.tsx","../../src/cli/ui/PlanConfirm.tsx","../../src/cli/ui/Select.tsx","../../src/cli/ui/PlanRefineInput.tsx","../../src/cli/ui/PromptInput.tsx","../../src/cli/ui/multiline-keys.ts","../../src/cli/ui/ShellConfirm.tsx","../../src/cli/ui/SlashSuggestions.tsx","../../src/cli/ui/StatsPanel.tsx","../../src/cli/ui/slash.ts","../../src/cli/commands/stats.ts","../../src/cli/ui/SessionPicker.tsx","../../src/cli/ui/Setup.tsx","../../src/cli/commands/code.tsx","../../src/cli/commands/diff.ts","../../src/cli/ui/DiffApp.tsx","../../src/cli/ui/RecordView.tsx","../../src/cli/commands/mcp-inspect.ts","../../src/mcp/catalog.ts","../../src/cli/commands/mcp.ts","../../src/cli/commands/replay.ts","../../src/cli/ui/ReplayApp.tsx","../../src/cli/commands/run.ts","../../src/cli/commands/sessions.ts","../../src/cli/commands/setup.tsx","../../src/cli/ui/Wizard.tsx","../../src/cli/ui/presets.ts","../../src/cli/commands/update.ts","../../src/cli/commands/version.ts","../../src/cli/resolve.ts"],"sourcesContent":["import { Command } from \"commander\";\nimport { readConfig } from \"../config.js\";\nimport { VERSION } from \"../index.js\";\nimport { applyMemoryStack } from \"../user-memory.js\";\nimport { chatCommand } from \"./commands/chat.js\";\nimport { codeCommand } from \"./commands/code.js\";\nimport { diffCommand } from \"./commands/diff.js\";\nimport { mcpInspectCommand } from \"./commands/mcp-inspect.js\";\nimport { mcpListCommand } from \"./commands/mcp.js\";\nimport { replayCommand } from \"./commands/replay.js\";\nimport { runCommand } from \"./commands/run.js\";\nimport { sessionsCommand } from \"./commands/sessions.js\";\nimport { setupCommand } from \"./commands/setup.js\";\nimport { statsCommand } from \"./commands/stats.js\";\nimport { updateCommand } from \"./commands/update.js\";\nimport { versionCommand } from \"./commands/version.js\";\nimport { resolveDefaults } from \"./resolve.js\";\n\nconst DEFAULT_SYSTEM =\n \"You are Reasonix, a helpful DeepSeek-powered assistant. Be concise and accurate. Use tools when available.\";\n\nconst program = new Command();\nprogram\n .name(\"reasonix\")\n .description(\"DeepSeek-native agent framework — built for cache hits and cheap tokens.\")\n .version(VERSION);\n\n// `reasonix` with no subcommand → launch the friendliest flow.\n// First run (no config yet) → interactive setup wizard.\n// Otherwise → chat with saved defaults. This is the \"one command to\n// rule them all\" entry for non-power-users: they don't need to learn\n// `chat` / `setup` / `--mcp` — just type `reasonix`.\nprogram.action(async () => {\n const cfg = readConfig();\n if (!cfg.setupCompleted) {\n await setupCommand({});\n return;\n }\n const defaults = resolveDefaults({});\n await chatCommand({\n model: defaults.model,\n system: applyMemoryStack(DEFAULT_SYSTEM, process.cwd()),\n harvest: defaults.harvest,\n branch: defaults.branch,\n session: defaults.session,\n mcp: defaults.mcp,\n });\n});\n\nprogram\n .command(\"setup\")\n .description(\"Interactive wizard — API key, preset, MCP servers. Re-run any time to reconfigure.\")\n .action(async () => {\n await setupCommand({});\n });\n\nprogram\n .command(\"code [dir]\")\n .description(\n \"Code-editing chat — filesystem MCP auto-bridged at <dir> (default: cwd), coding system prompt, smart preset. Model proposes SEARCH/REPLACE blocks; Reasonix applies them to disk.\",\n )\n .option(\"-m, --model <id>\", \"Override default reasoner model\")\n .option(\"--no-session\", \"Disable session persistence for this run\")\n .option(\"-r, --resume\", \"Skip the session picker — always continue prior messages\")\n .option(\"-n, --new\", \"Skip the session picker — always wipe prior messages and start fresh\")\n .option(\"--transcript <path>\", \"Write a JSONL transcript to this path\")\n .action(async (dir: string | undefined, opts) => {\n await codeCommand({\n dir,\n model: opts.model,\n noSession: opts.session === false,\n transcript: opts.transcript,\n forceResume: !!opts.resume,\n forceNew: !!opts.new,\n });\n });\n\nprogram\n .command(\"chat\")\n .description(\"Interactive Ink TUI with live cache/cost panel.\")\n .option(\"-m, --model <id>\", \"DeepSeek model id (overrides preset)\")\n .option(\"-s, --system <prompt>\", \"System prompt (pinned in the immutable prefix)\", DEFAULT_SYSTEM)\n .option(\"--transcript <path>\", \"Write a JSONL transcript to this path\")\n .option(\n \"--preset <name>\",\n \"Bundle of model + harvest + branch. One of: fast, smart, max. Overrides config.preset.\",\n )\n .option(\n \"--harvest\",\n \"Extract typed plan state from R1 reasoning (Pillar 2). Overrides preset's harvest setting.\",\n )\n .option(\n \"--branch <n>\",\n \"Self-consistency: run N parallel samples per turn and pick the most confident (disables streaming; enables harvest)\",\n (v) => Number.parseInt(v, 10),\n )\n .option(\"--session <name>\", \"Use a named session (default: from config, usually 'default').\")\n .option(\"--no-session\", \"Disable session persistence for this run (ephemeral chat)\")\n .option(\"-r, --resume\", \"Skip the session picker — always continue prior messages\")\n .option(\"-n, --new\", \"Skip the session picker — always wipe prior messages and start fresh\")\n .option(\n \"--mcp <spec>\",\n 'MCP server spec; repeatable. \"name=cmd args...\", \"cmd args...\", or a URL (http/https → SSE transport). Overrides config.mcp when provided.',\n (value: string, previous: string[] = []) => [...previous, value],\n [] as string[],\n )\n .option(\n \"--mcp-prefix <str>\",\n \"Global prefix applied to every MCP tool (only honored when no per-spec name is set; avoids collisions with a single anonymous server)\",\n )\n .option(\"--no-config\", \"Ignore `~/.reasonix/config.json` — useful for CI or reproducing issues\")\n .action(async (opts) => {\n const defaults = resolveDefaults({\n model: opts.model,\n harvest: opts.harvest,\n branch: opts.branch,\n mcp: opts.mcp as string[],\n session: opts.session,\n preset: opts.preset,\n noConfig: opts.config === false,\n });\n await chatCommand({\n model: defaults.model,\n system: applyMemoryStack(opts.system, process.cwd()),\n transcript: opts.transcript,\n harvest: defaults.harvest,\n branch: defaults.branch,\n session: defaults.session,\n mcp: defaults.mcp,\n mcpPrefix: opts.mcpPrefix,\n forceResume: !!opts.resume,\n forceNew: !!opts.new,\n });\n });\n\nprogram\n .command(\"run <task>\")\n .description(\"Run a single task non-interactively, streaming output.\")\n .option(\"-m, --model <id>\", \"DeepSeek model id (overrides preset)\")\n .option(\"-s, --system <prompt>\", \"System prompt\", DEFAULT_SYSTEM)\n .option(\"--preset <name>\", \"Bundle of model + harvest + branch: fast | smart | max\")\n .option(\"--harvest\", \"Extract typed plan state from R1 reasoning (Pillar 2)\")\n .option(\n \"--branch <n>\",\n \"Self-consistency: run N parallel samples per turn and pick the most confident\",\n (v) => Number.parseInt(v, 10),\n )\n .option(\"--transcript <path>\", \"Write a JSONL transcript to this path for replay/diff\")\n .option(\n \"--mcp <spec>\",\n 'MCP server spec; repeatable. \"name=cmd args...\", \"cmd args...\", or a URL (http/https → SSE).',\n (value: string, previous: string[] = []) => [...previous, value],\n [] as string[],\n )\n .option(\n \"--mcp-prefix <str>\",\n \"Global prefix (only honored when no per-spec name is set; for a single anonymous server)\",\n )\n .option(\"--no-config\", \"Ignore `~/.reasonix/config.json` — useful for CI or reproducing issues\")\n .action(async (task: string, opts) => {\n const defaults = resolveDefaults({\n model: opts.model,\n harvest: opts.harvest,\n branch: opts.branch,\n mcp: opts.mcp as string[],\n preset: opts.preset,\n noConfig: opts.config === false,\n });\n await runCommand({\n task,\n model: defaults.model,\n system: applyMemoryStack(opts.system, process.cwd()),\n harvest: defaults.harvest,\n branch: defaults.branch,\n transcript: opts.transcript,\n mcp: defaults.mcp,\n mcpPrefix: opts.mcpPrefix,\n });\n });\n\nprogram\n .command(\"stats [transcript]\")\n .description(\n \"Show usage dashboard (today / week / month / all-time · turns · cache hit · cost · savings vs Claude). \" +\n \"Pass a transcript path to fall back to the per-file summary (assistant turns + tool calls).\",\n )\n .action((transcript: string | undefined) => {\n statsCommand({ transcript });\n });\n\nprogram\n .command(\"sessions [name]\")\n .description(\"List saved chat sessions, or inspect one by name.\")\n .option(\"-v, --verbose\", \"Include system prompts + tool-call metadata when inspecting\")\n .action((name: string | undefined, opts) => {\n sessionsCommand({ name, verbose: !!opts.verbose });\n });\n\nprogram\n .command(\"replay <transcript>\")\n .description(\n \"Interactive Ink TUI to scrub through a transcript + rebuild its session summary (cost, cache, prefix stability). No API calls.\",\n )\n .option(\"--print\", \"Dump to stdout instead of mounting the TUI (auto when piped)\")\n .option(\"--head <n>\", \"stdout mode only — show first N records\", (v) => Number.parseInt(v, 10))\n .option(\"--tail <n>\", \"stdout mode only — show last N records\", (v) => Number.parseInt(v, 10))\n .action(async (transcript: string, opts) => {\n await replayCommand({\n path: transcript,\n print: !!opts.print,\n head: Number.isFinite(opts.head) ? opts.head : undefined,\n tail: Number.isFinite(opts.tail) ? opts.tail : undefined,\n });\n });\n\nprogram\n .command(\"diff <a> <b>\")\n .description(\n \"Compare two transcripts in a split-pane Ink TUI (default) or stdout table. Use n/N to jump across divergences.\",\n )\n .option(\"--md <path>\", \"Write a markdown report (blog-ready) to this path\")\n .option(\"--print\", \"Force stdout table instead of the TUI (auto when piped)\")\n .option(\"--tui\", \"Force the TUI even when piped (rare)\")\n .option(\"--label-a <label>\", \"Display label for transcript A (default: filename)\")\n .option(\"--label-b <label>\", \"Display label for transcript B (default: filename)\")\n .action(async (a: string, b: string, opts) => {\n await diffCommand({\n a,\n b,\n mdPath: opts.md,\n labelA: opts.labelA,\n labelB: opts.labelB,\n print: !!opts.print,\n tui: !!opts.tui,\n });\n });\n\nconst mcp = program\n .command(\"mcp\")\n .description(\"Model Context Protocol helpers — discover servers, test your setup.\");\n\nmcp\n .command(\"list\")\n .description(\"Show a curated catalog of popular MCP servers with ready-to-use --mcp commands.\")\n .option(\"--json\", \"Emit the catalog as JSON instead of the human-readable table\")\n .action((opts) => {\n mcpListCommand({ json: !!opts.json });\n });\n\nmcp\n .command(\"inspect <spec>\")\n .description(\n 'Connect to one MCP server and print its server info + tools/resources/prompts. <spec> takes the same forms as --mcp (\"name=cmd args\", \"cmd args\", or an SSE URL).',\n )\n .option(\"--json\", \"Emit the full inspection report as JSON instead of the human-readable summary\")\n .action(async (spec: string, opts) => {\n try {\n await mcpInspectCommand({ spec, json: !!opts.json });\n } catch (err) {\n process.stderr.write(`mcp inspect failed: ${(err as Error).message}\\n`);\n process.exit(1);\n }\n });\n\nprogram.command(\"version\").description(\"Print Reasonix version.\").action(versionCommand);\n\nprogram\n .command(\"update\")\n .description(\n \"Check the npm registry for a newer Reasonix and install it. Detects npx vs global install; for npx users, prints a cache-refresh hint instead of running `npm i -g`.\",\n )\n .option(\"--dry-run\", \"Print the plan without executing the install\")\n .action(async (opts: { dryRun?: boolean }) => {\n await updateCommand({ dryRun: !!opts.dryRun });\n });\n\nprogram.parseAsync(process.argv).catch((err) => {\n console.error(err);\n process.exit(1);\n});\n","/**\n * User-level config storage for the Reasonix CLI.\n *\n * Lookup order for the API key:\n * 1. `DEEPSEEK_API_KEY` env var (highest priority — for CI / power users)\n * 2. `~/.reasonix/config.json` (set by the first-run setup flow)\n *\n * The library itself never touches the config file — it only reads\n * `DEEPSEEK_API_KEY` from the environment. The CLI is responsible for\n * pulling from the config file and exposing it via env var to the loop.\n *\n * Beyond the API key, the config also remembers the user's *defaults*\n * from `reasonix setup`: preset, MCP servers, session. This is what\n * makes `reasonix chat` with no flags \"just work\" after first-run.\n */\n\nimport { chmodSync, mkdirSync, readFileSync, writeFileSync } from \"node:fs\";\nimport { homedir } from \"node:os\";\nimport { dirname, join } from \"node:path\";\n\n/** One of the preset bundles (model + harvest + branch combo). */\nexport type PresetName = \"fast\" | \"smart\" | \"max\";\n\nexport interface ReasonixConfig {\n apiKey?: string;\n baseUrl?: string;\n /**\n * Default preset for `reasonix chat` / `reasonix run` when no flags override.\n * Maps to model + harvest + branch combos (see presets.ts). Missing → \"fast\".\n */\n preset?: PresetName;\n /**\n * Default MCP server specs to bridge on every `reasonix chat`, in the\n * same `\"name=cmd args...\"` format that `--mcp` takes. Stored as strings\n * so `reasonix setup` stays symmetrical with the flag — one parser, one\n * format in the config file, grep-friendly.\n */\n mcp?: string[];\n /**\n * Default session name (null/missing → \"default\", which is what the\n * CLI has been doing anyway). `reasonix setup` lets users pick a name\n * or opt into ephemeral.\n */\n session?: string | null;\n /** Marks that `reasonix setup` has completed at least once. */\n setupCompleted?: boolean;\n /**\n * Whether `web_search` + `web_fetch` tools are registered. Default:\n * enabled (no key required — backed by DuckDuckGo's public HTML\n * endpoint). Set to `false` to keep the session offline.\n */\n search?: boolean;\n /**\n * Per-project state keyed by absolute directory path. Written by the\n * \"always allow\" choice on a shell confirmation prompt; merged into\n * `registerShellTools({ extraAllowed })` when `reasonix code` runs\n * against that directory again.\n */\n projects?: {\n [absoluteRootDir: string]: {\n shellAllowed?: string[];\n };\n };\n}\n\nexport function defaultConfigPath(): string {\n return join(homedir(), \".reasonix\", \"config.json\");\n}\n\nexport function readConfig(path: string = defaultConfigPath()): ReasonixConfig {\n try {\n const raw = readFileSync(path, \"utf8\");\n const parsed = JSON.parse(raw);\n if (parsed && typeof parsed === \"object\") return parsed as ReasonixConfig;\n } catch {\n /* missing or malformed → empty config */\n }\n return {};\n}\n\nexport function writeConfig(cfg: ReasonixConfig, path: string = defaultConfigPath()): void {\n mkdirSync(dirname(path), { recursive: true });\n writeFileSync(path, JSON.stringify(cfg, null, 2), \"utf8\");\n // Restrict permissions on Unix; chmod is a no-op on Windows but won't throw.\n try {\n chmodSync(path, 0o600);\n } catch {\n /* ignore on platforms without chmod */\n }\n}\n\n/** Resolve the API key from env var first, then the config file. */\nexport function loadApiKey(path: string = defaultConfigPath()): string | undefined {\n if (process.env.DEEPSEEK_API_KEY) return process.env.DEEPSEEK_API_KEY;\n return readConfig(path).apiKey;\n}\n\n/**\n * Resolve whether web-search tools should be registered. Default: on.\n * Env `REASONIX_SEARCH=off` or config `search: false` turns it off.\n * Any other value falls through to enabled.\n */\nexport function searchEnabled(path: string = defaultConfigPath()): boolean {\n const env = process.env.REASONIX_SEARCH;\n if (env === \"off\" || env === \"false\" || env === \"0\") return false;\n const cfg = readConfig(path).search;\n if (cfg === false) return false;\n return true;\n}\n\nexport function saveApiKey(key: string, path: string = defaultConfigPath()): void {\n const cfg = readConfig(path);\n cfg.apiKey = key.trim();\n writeConfig(cfg, path);\n}\n\n/**\n * Read the persisted \"always allow\" shell-command prefixes for a\n * given project root. Returns an empty array when nothing's stored.\n */\nexport function loadProjectShellAllowed(\n rootDir: string,\n path: string = defaultConfigPath(),\n): string[] {\n const cfg = readConfig(path);\n return cfg.projects?.[rootDir]?.shellAllowed ?? [];\n}\n\n/**\n * Append a prefix to the project's shell-allowed list, dedup and\n * persist. No-op if the prefix is empty/whitespace or already stored.\n */\nexport function addProjectShellAllowed(\n rootDir: string,\n prefix: string,\n path: string = defaultConfigPath(),\n): void {\n const trimmed = prefix.trim();\n if (!trimmed) return;\n const cfg = readConfig(path);\n if (!cfg.projects) cfg.projects = {};\n if (!cfg.projects[rootDir]) cfg.projects[rootDir] = {};\n const existing = cfg.projects[rootDir].shellAllowed ?? [];\n if (existing.includes(trimmed)) return;\n cfg.projects[rootDir].shellAllowed = [...existing, trimmed];\n writeConfig(cfg, path);\n}\n\nexport function isPlausibleKey(key: string): boolean {\n const trimmed = key.trim();\n return /^sk-[A-Za-z0-9_-]{16,}$/.test(trimmed);\n}\n\n/** Mask a key for display: `sk-abcd...wxyz`. */\nexport function redactKey(key: string): string {\n if (!key) return \"\";\n if (key.length <= 12) return \"****\";\n return `${key.slice(0, 6)}…${key.slice(-4)}`;\n}\n","import { type EventSourceMessage, createParser } from \"eventsource-parser\";\nimport { type RetryOptions, fetchWithRetry } from \"./retry.js\";\nimport type { ChatMessage, ChatRequestOptions, RawUsage, ToolCall, ToolSpec } from \"./types.js\";\n\nexport class Usage {\n constructor(\n public promptTokens = 0,\n public completionTokens = 0,\n public totalTokens = 0,\n public promptCacheHitTokens = 0,\n public promptCacheMissTokens = 0,\n ) {}\n\n get cacheHitRatio(): number {\n const denom = this.promptCacheHitTokens + this.promptCacheMissTokens;\n return denom > 0 ? this.promptCacheHitTokens / denom : 0;\n }\n\n static fromApi(raw: RawUsage | undefined | null): Usage {\n const u = raw ?? {};\n return new Usage(\n u.prompt_tokens ?? 0,\n u.completion_tokens ?? 0,\n u.total_tokens ?? 0,\n u.prompt_cache_hit_tokens ?? 0,\n u.prompt_cache_miss_tokens ?? 0,\n );\n }\n}\n\nexport interface ChatResponse {\n content: string;\n reasoningContent: string | null;\n toolCalls: ToolCall[];\n usage: Usage;\n raw: unknown;\n}\n\nexport interface StreamChunk {\n contentDelta?: string;\n reasoningDelta?: string;\n toolCallDelta?: { index: number; id?: string; name?: string; argumentsDelta?: string };\n usage?: Usage;\n finishReason?: string;\n raw: any;\n}\n\n/**\n * Response shape for DeepSeek's `/user/balance` endpoint. One entry\n * per currency the account is funded in (typically CNY, sometimes\n * USD). `total_balance` is the spendable figure; `granted_balance`\n * counts promotional credits that expire, `topped_up_balance` is\n * what the user paid for and keeps.\n */\nexport interface BalanceInfo {\n currency: string;\n total_balance: string;\n granted_balance?: string;\n topped_up_balance?: string;\n}\n\nexport interface UserBalance {\n is_available: boolean;\n balance_infos: BalanceInfo[];\n}\n\nexport interface DeepSeekClientOptions {\n apiKey?: string;\n baseUrl?: string;\n timeoutMs?: number;\n fetch?: typeof fetch;\n /** Retry configuration. Pass `{ maxAttempts: 1 }` to disable retries. */\n retry?: RetryOptions;\n}\n\nexport class DeepSeekClient {\n readonly apiKey: string;\n readonly baseUrl: string;\n readonly timeoutMs: number;\n readonly retry: RetryOptions;\n private readonly _fetch: typeof fetch;\n\n constructor(opts: DeepSeekClientOptions = {}) {\n const apiKey = opts.apiKey ?? process.env.DEEPSEEK_API_KEY;\n if (!apiKey) {\n throw new Error(\n \"DEEPSEEK_API_KEY is not set. Put it in .env or pass apiKey to DeepSeekClient.\",\n );\n }\n this.apiKey = apiKey;\n this.baseUrl = (\n opts.baseUrl ??\n process.env.DEEPSEEK_BASE_URL ??\n \"https://api.deepseek.com\"\n ).replace(/\\/+$/, \"\");\n this.timeoutMs = opts.timeoutMs ?? 120_000;\n this._fetch = opts.fetch ?? globalThis.fetch.bind(globalThis);\n this.retry = opts.retry ?? {};\n }\n\n private buildPayload(opts: ChatRequestOptions, stream: boolean) {\n const payload: Record<string, unknown> = {\n model: opts.model,\n messages: opts.messages,\n stream,\n };\n if (opts.tools?.length) payload.tools = opts.tools;\n if (opts.temperature !== undefined) payload.temperature = opts.temperature;\n if (opts.maxTokens !== undefined) payload.max_tokens = opts.maxTokens;\n if (opts.responseFormat) payload.response_format = opts.responseFormat;\n return payload;\n }\n\n /**\n * Fetch the current DeepSeek account balance. Separate endpoint\n * from chat completions, no billing impact. Returns null on any\n * network/auth failure so callers can gate the balance display\n * without a hard error — the rest of the session works regardless.\n */\n async getBalance(opts: { signal?: AbortSignal } = {}): Promise<UserBalance | null> {\n try {\n const resp = await this._fetch(`${this.baseUrl}/user/balance`, {\n method: \"GET\",\n headers: { Authorization: `Bearer ${this.apiKey}` },\n signal: opts.signal,\n });\n if (!resp.ok) return null;\n const data = (await resp.json()) as UserBalance;\n if (!data || !Array.isArray(data.balance_infos)) return null;\n return data;\n } catch {\n return null;\n }\n }\n\n async chat(opts: ChatRequestOptions): Promise<ChatResponse> {\n const ctrl = new AbortController();\n const timer = setTimeout(() => ctrl.abort(), this.timeoutMs);\n const signal = opts.signal ?? ctrl.signal;\n\n try {\n const resp = await fetchWithRetry(\n this._fetch,\n `${this.baseUrl}/chat/completions`,\n {\n method: \"POST\",\n headers: {\n Authorization: `Bearer ${this.apiKey}`,\n \"Content-Type\": \"application/json\",\n },\n body: JSON.stringify(this.buildPayload(opts, false)),\n signal,\n },\n { ...this.retry, signal },\n );\n if (!resp.ok) {\n throw new Error(`DeepSeek ${resp.status}: ${await resp.text()}`);\n }\n const data: any = await resp.json();\n const choice = data.choices?.[0]?.message ?? {};\n return {\n content: choice.content ?? \"\",\n reasoningContent: choice.reasoning_content ?? null,\n toolCalls: choice.tool_calls ?? [],\n usage: Usage.fromApi(data.usage),\n raw: data,\n };\n } finally {\n clearTimeout(timer);\n }\n }\n\n async *stream(opts: ChatRequestOptions): AsyncGenerator<StreamChunk> {\n const ctrl = new AbortController();\n const timer = setTimeout(() => ctrl.abort(), this.timeoutMs);\n const signal = opts.signal ?? ctrl.signal;\n\n let resp: Response;\n try {\n // Only the initial fetch is retried. Once the server has started sending\n // the stream body we do NOT retry — a mid-stream retry would re-bill and\n // desync the session context.\n resp = await fetchWithRetry(\n this._fetch,\n `${this.baseUrl}/chat/completions`,\n {\n method: \"POST\",\n headers: {\n Authorization: `Bearer ${this.apiKey}`,\n \"Content-Type\": \"application/json\",\n Accept: \"text/event-stream\",\n },\n body: JSON.stringify(this.buildPayload(opts, true)),\n signal,\n },\n { ...this.retry, signal },\n );\n } catch (err) {\n clearTimeout(timer);\n throw err;\n }\n if (!resp.ok || !resp.body) {\n clearTimeout(timer);\n throw new Error(`DeepSeek ${resp.status}: ${await resp.text().catch(() => \"\")}`);\n }\n\n const queue: StreamChunk[] = [];\n let done = false;\n const parser = createParser({\n onEvent: (ev: EventSourceMessage) => {\n if (!ev.data || ev.data === \"[DONE]\") {\n done = true;\n return;\n }\n try {\n const json = JSON.parse(ev.data);\n const delta = json.choices?.[0]?.delta ?? {};\n const finishReason = json.choices?.[0]?.finish_reason ?? undefined;\n const chunk: StreamChunk = { raw: json, finishReason };\n if (typeof delta.content === \"string\" && delta.content.length > 0) {\n chunk.contentDelta = delta.content;\n }\n if (typeof delta.reasoning_content === \"string\" && delta.reasoning_content.length > 0) {\n chunk.reasoningDelta = delta.reasoning_content;\n }\n if (Array.isArray(delta.tool_calls) && delta.tool_calls.length > 0) {\n const tc = delta.tool_calls[0];\n chunk.toolCallDelta = {\n index: tc.index ?? 0,\n id: tc.id,\n name: tc.function?.name,\n argumentsDelta: tc.function?.arguments,\n };\n }\n if (json.usage) {\n chunk.usage = Usage.fromApi(json.usage);\n }\n queue.push(chunk);\n } catch {\n /* skip malformed sse frame */\n }\n },\n });\n\n const reader = resp.body.getReader();\n const decoder = new TextDecoder();\n try {\n while (true) {\n if (queue.length > 0) {\n yield queue.shift()!;\n continue;\n }\n if (done) break;\n const { value, done: streamDone } = await reader.read();\n if (streamDone) break;\n parser.feed(decoder.decode(value, { stream: true }));\n }\n while (queue.length > 0) yield queue.shift()!;\n } finally {\n clearTimeout(timer);\n reader.releaseLock();\n }\n }\n}\n\nexport type { ChatMessage, ToolCall, ToolSpec };\n","/**\n * Retry layer for DeepSeek API calls.\n *\n * Wraps a `fetch` function so that transient failures (rate limiting, server\n * overload, network blips) don't kill an agent session. We explicitly DO NOT\n * retry:\n * - 4xx client errors other than 408 / 429 (bad key, bad request, ...)\n * - aborted requests (user cancelled)\n * - mid-stream body read errors (retrying costs money AND would desync)\n *\n * Retrying is controlled by attempt count + exponential backoff with jitter.\n * If the server sends a `Retry-After` header we honor it (capped by\n * `maxBackoffMs` so a misconfigured upstream can't park us forever).\n */\n\nexport interface RetryOptions {\n /** Maximum total attempts (including the first). Default 4. */\n maxAttempts?: number;\n /** Initial backoff in ms. Doubles each retry, with jitter. Default 500. */\n initialBackoffMs?: number;\n /** Upper bound on any single backoff delay. Default 10000 (10s). */\n maxBackoffMs?: number;\n /** HTTP statuses to treat as retryable. Default [408, 429, 500, 502, 503, 504]. */\n retryableStatuses?: readonly number[];\n /** Abort signal; we do NOT retry once aborted. */\n signal?: AbortSignal;\n /** Telemetry hook — called before each wait. */\n onRetry?: (info: RetryInfo) => void;\n}\n\nexport interface RetryInfo {\n attempt: number;\n reason: string;\n waitMs: number;\n}\n\nconst DEFAULT_RETRYABLE_STATUSES = [408, 429, 500, 502, 503, 504] as const;\n\nexport async function fetchWithRetry(\n fetchFn: typeof fetch,\n url: string,\n init: RequestInit,\n opts: RetryOptions = {},\n): Promise<Response> {\n const maxAttempts = opts.maxAttempts ?? 4;\n const initial = opts.initialBackoffMs ?? 500;\n const cap = opts.maxBackoffMs ?? 10_000;\n const retryable = new Set(opts.retryableStatuses ?? DEFAULT_RETRYABLE_STATUSES);\n\n let lastError: unknown;\n\n for (let attempt = 0; attempt < maxAttempts; attempt++) {\n if (opts.signal?.aborted) throw new Error(\"aborted\");\n\n try {\n const resp = await fetchFn(url, init);\n\n // Success or non-retryable failure: return as-is.\n if (resp.ok || !retryable.has(resp.status)) return resp;\n\n // Retryable but out of attempts: return the last response so the caller\n // can surface the status to the user.\n if (attempt === maxAttempts - 1) return resp;\n\n // Drain the body so the connection can be reused on the next attempt.\n await resp.text().catch(() => undefined);\n\n const waitMs = computeWait(attempt, initial, cap, resp.headers.get(\"Retry-After\"));\n opts.onRetry?.({ attempt: attempt + 1, reason: `http ${resp.status}`, waitMs });\n await sleep(waitMs, opts.signal);\n } catch (err) {\n lastError = err;\n // Respect explicit aborts — do not retry.\n if (isAbortError(err) || opts.signal?.aborted) throw err;\n if (attempt === maxAttempts - 1) throw err;\n\n const waitMs = computeWait(attempt, initial, cap, null);\n opts.onRetry?.({\n attempt: attempt + 1,\n reason: `network: ${messageOf(err)}`,\n waitMs,\n });\n await sleep(waitMs, opts.signal);\n }\n }\n\n throw lastError ?? new Error(\"fetchWithRetry: loop exited unexpectedly\");\n}\n\nfunction computeWait(\n attempt: number,\n initial: number,\n cap: number,\n retryAfter: string | null,\n): number {\n if (retryAfter) {\n const seconds = Number.parseFloat(retryAfter);\n if (Number.isFinite(seconds) && seconds > 0) {\n return Math.min(seconds * 1000, cap);\n }\n }\n const exp = initial * 2 ** attempt;\n // Jitter range [75%, 125%] to spread retries out when many clients hit 429 together.\n const jitter = exp * (0.75 + Math.random() * 0.5);\n return Math.min(Math.max(jitter, 0), cap);\n}\n\nfunction sleep(ms: number, signal?: AbortSignal): Promise<void> {\n if (ms <= 0) return Promise.resolve();\n return new Promise((resolve, reject) => {\n const timer = setTimeout(resolve, ms);\n if (signal) {\n const onAbort = () => {\n clearTimeout(timer);\n reject(new Error(\"aborted\"));\n };\n if (signal.aborted) onAbort();\n else signal.addEventListener(\"abort\", onAbort, { once: true });\n }\n });\n}\n\nfunction isAbortError(err: unknown): boolean {\n if (!err || typeof err !== \"object\") return false;\n const name = (err as { name?: unknown }).name;\n return name === \"AbortError\";\n}\n\nfunction messageOf(err: unknown): string {\n if (err instanceof Error) return err.message;\n try {\n return String(err);\n } catch {\n return \"unknown error\";\n }\n}\n","/**\n * Pillar 2 — R1 Thought Harvesting.\n *\n * Takes the `reasoning_content` emitted by a thinking model (deepseek-reasoner\n * / R1) and extracts a structured plan state by making a cheap secondary call\n * to V3 in JSON mode. The typed state is intended for the orchestrator to\n * branch on — e.g. trigger self-consistency sampling when `uncertainties.length\n * > 2`, or surface the subgoals to the user.\n *\n * Opt-in: loops disable harvesting by default. Failures (bad JSON, API error,\n * empty reasoning) return an empty TypedPlanState — the main turn is never\n * aborted because of a harvest hiccup.\n */\n\nimport type { DeepSeekClient } from \"./client.js\";\n\nexport interface TypedPlanState {\n subgoals: string[];\n hypotheses: string[];\n uncertainties: string[];\n rejectedPaths: string[];\n}\n\nexport interface HarvestOptions {\n /** Model used for the extraction call. Defaults to the cheap chat model. */\n model?: string;\n /** Cap on how many items land in each array. Default 5. */\n maxItems?: number;\n /** Per-item character cap. Default 80. */\n maxItemLen?: number;\n /** Abort the extraction if R1 reasoning is shorter than this. Default 40. */\n minReasoningLen?: number;\n}\n\nexport function emptyPlanState(): TypedPlanState {\n return { subgoals: [], hypotheses: [], uncertainties: [], rejectedPaths: [] };\n}\n\nexport function isPlanStateEmpty(s: TypedPlanState | null | undefined): boolean {\n if (!s) return true;\n return (\n s.subgoals.length === 0 &&\n s.hypotheses.length === 0 &&\n s.uncertainties.length === 0 &&\n s.rejectedPaths.length === 0\n );\n}\n\nconst SYSTEM_PROMPT = `You extract a typed plan state from a reasoning trace produced by another LLM.\nOutput ONLY a JSON object. No markdown, no prose, no backticks.\n\nSchema:\n{\n \"subgoals\": string[], // concrete intermediate objectives the trace identifies\n \"hypotheses\": string[], // candidate approaches or assumptions being weighed\n \"uncertainties\": string[], // facts the trace flags as unclear / to verify\n \"rejectedPaths\": string[] // approaches the trace considered and then abandoned\n}\n\nConstraints:\n- Every field must be present. Use [] if not applicable.\n- Each array has at most {maxItems} items.\n- Each item is plain text, at most {maxItemLen} characters, no markdown.\n- Write in the same language as the trace (Chinese in → Chinese out, etc.).\n- Do not quote back the trace; write short, specific phrases.`;\n\nexport async function harvest(\n reasoningContent: string | null | undefined,\n client?: DeepSeekClient,\n options: HarvestOptions = {},\n signal?: AbortSignal,\n): Promise<TypedPlanState> {\n if (!client || !reasoningContent) return emptyPlanState();\n // Fast-path the already-aborted case so we don't burn a network\n // round-trip for a result the caller no longer wants.\n if (signal?.aborted) return emptyPlanState();\n const minLen = options.minReasoningLen ?? 40;\n const trimmed = reasoningContent.trim();\n if (trimmed.length < minLen) return emptyPlanState();\n\n const model = options.model ?? \"deepseek-chat\";\n const maxItems = options.maxItems ?? 5;\n const maxItemLen = options.maxItemLen ?? 80;\n const system = SYSTEM_PROMPT.replace(\"{maxItems}\", String(maxItems)).replace(\n \"{maxItemLen}\",\n String(maxItemLen),\n );\n\n try {\n const resp = await client.chat({\n model,\n messages: [\n { role: \"system\", content: system },\n { role: \"user\", content: trimmed },\n ],\n responseFormat: { type: \"json_object\" },\n temperature: 0,\n maxTokens: 600,\n signal,\n });\n return parsePlanState(resp.content, maxItems, maxItemLen);\n } catch {\n return emptyPlanState();\n }\n}\n\nfunction parsePlanState(raw: string, maxItems: number, maxItemLen: number): TypedPlanState {\n const text = (raw ?? \"\").trim();\n if (!text) return emptyPlanState();\n let parsed: unknown;\n try {\n parsed = JSON.parse(text);\n } catch {\n // Occasionally a model wraps JSON in fences despite instructions.\n const match = text.match(/\\{[\\s\\S]*\\}/);\n if (!match) return emptyPlanState();\n try {\n parsed = JSON.parse(match[0]);\n } catch {\n return emptyPlanState();\n }\n }\n if (!parsed || typeof parsed !== \"object\") return emptyPlanState();\n const obj = parsed as Record<string, unknown>;\n return {\n subgoals: sanitizeArray(obj.subgoals, maxItems, maxItemLen),\n hypotheses: sanitizeArray(obj.hypotheses, maxItems, maxItemLen),\n uncertainties: sanitizeArray(obj.uncertainties, maxItems, maxItemLen),\n rejectedPaths: sanitizeArray(obj.rejectedPaths ?? obj.rejected_paths, maxItems, maxItemLen),\n };\n}\n\nfunction sanitizeArray(raw: unknown, maxItems: number, maxItemLen: number): string[] {\n if (!Array.isArray(raw)) return [];\n const out: string[] = [];\n for (const item of raw) {\n if (out.length >= maxItems) break;\n if (typeof item !== \"string\") continue;\n const cleaned = item.trim().replace(/\\s+/g, \" \");\n if (!cleaned) continue;\n out.push(cleaned.length <= maxItemLen ? cleaned : `${cleaned.slice(0, maxItemLen - 1)}…`);\n }\n return out;\n}\n","/**\n * Self-consistency branching.\n *\n * When enabled, the loop fans out into N parallel samples per turn (varied\n * temperatures), runs Pillar 2 harvest on each, and selects the sample with\n * the fewest flagged uncertainties (ties broken by answer length — a crude\n * Occam prior).\n *\n * The unique opportunity here: because DeepSeek is ~20× cheaper than Claude,\n * running N=3–5 samples per turn is still cheaper than a single Claude call,\n * while the majority-confidence selection tends to dominate single-sample\n * answers on fuzzy multi-step reasoning tasks.\n */\n\nimport type { ChatResponse, DeepSeekClient } from \"./client.js\";\nimport { type HarvestOptions, type TypedPlanState, harvest } from \"./harvest.js\";\nimport type { ChatRequestOptions } from \"./types.js\";\n\nexport interface BranchSample {\n index: number;\n temperature: number;\n response: ChatResponse;\n planState: TypedPlanState;\n}\n\nexport type BranchSelector = (samples: BranchSample[]) => BranchSample;\n\nexport interface BranchOptions {\n /** Number of parallel samples. 1 disables branching. Default 1. */\n budget?: number;\n /** Temperatures for each branch. Default spreads across [0, 1]. */\n temperatures?: readonly number[];\n /** Harvest options; the selector needs harvest to score samples. */\n harvestOptions?: HarvestOptions;\n /** Custom selector. Default: min uncertainties, tie-break shortest answer. */\n selector?: BranchSelector;\n /**\n * Fires as each sample finishes (main call + harvest both complete).\n * Useful for progress UI. Not awaited; exceptions are swallowed.\n */\n onSampleDone?: (sample: BranchSample) => void;\n}\n\nexport interface BranchResult {\n chosen: BranchSample;\n samples: BranchSample[];\n}\n\n/** Default: fewest uncertainties wins, ties broken by shorter answer content. */\nexport const defaultSelector: BranchSelector = (samples) => {\n if (samples.length === 0) throw new Error(\"defaultSelector: samples is empty\");\n return samples.slice().sort((a, b) => {\n const uDiff = a.planState.uncertainties.length - b.planState.uncertainties.length;\n if (uDiff !== 0) return uDiff;\n const aLen = a.response.content?.length ?? 0;\n const bLen = b.response.content?.length ?? 0;\n return aLen - bLen;\n })[0]!;\n};\n\nexport async function runBranches(\n client: DeepSeekClient,\n request: ChatRequestOptions,\n opts: BranchOptions = {},\n): Promise<BranchResult> {\n const budget = Math.max(1, opts.budget ?? 1);\n const temperatures = resolveTemperatures(budget, opts.temperatures);\n const selector = opts.selector ?? defaultSelector;\n\n const samples = await Promise.all(\n temperatures.map(async (temperature, index): Promise<BranchSample> => {\n const response = await client.chat({ ...request, temperature });\n const planState = await harvest(response.reasoningContent, client, opts.harvestOptions);\n const sample: BranchSample = { index, temperature, response, planState };\n try {\n opts.onSampleDone?.(sample);\n } catch {\n /* callback errors must not poison the await */\n }\n return sample;\n }),\n );\n\n return { chosen: selector(samples), samples };\n}\n\n/** Sum usage across branch samples for telemetry purposes. */\nexport function aggregateBranchUsage(samples: readonly BranchSample[]) {\n let promptTokens = 0;\n let completionTokens = 0;\n let totalTokens = 0;\n let promptCacheHitTokens = 0;\n let promptCacheMissTokens = 0;\n for (const s of samples) {\n promptTokens += s.response.usage.promptTokens;\n completionTokens += s.response.usage.completionTokens;\n totalTokens += s.response.usage.totalTokens;\n promptCacheHitTokens += s.response.usage.promptCacheHitTokens;\n promptCacheMissTokens += s.response.usage.promptCacheMissTokens;\n }\n return {\n promptTokens,\n completionTokens,\n totalTokens,\n promptCacheHitTokens,\n promptCacheMissTokens,\n };\n}\n\nfunction resolveTemperatures(budget: number, custom?: readonly number[]): number[] {\n if (custom && custom.length >= budget) return [...custom.slice(0, budget)];\n // Spread evenly across [0, 1] to encourage reasoning-path diversity.\n if (budget === 1) return [0];\n const out: number[] = [];\n for (let i = 0; i < budget; i++) {\n out.push(Number((i / (budget - 1)).toFixed(2)));\n }\n return out;\n}\n","/**\n * Hooks — user-defined automation that fires at well-known points in\n * the agent loop. Mirrors the two-scope layout we use for memory and\n * skills:\n *\n * - `<project>/.reasonix/settings.json` — committable per-project\n * - `~/.reasonix/settings.json` — every session\n *\n * A hook is a shell command. We invoke it with stdin = a JSON\n * payload describing the event, and interpret the exit code:\n *\n * - `0` — pass; loop continues normally\n * - `2` — block; for `PreToolUse` / `UserPromptSubmit` the\n * loop refuses to continue with that step and surfaces the\n * hook's stderr as the reason. For `PostToolUse` / `Stop` block\n * is meaningless (the action already happened) — treat as warn.\n * - anything else — warn; loop continues but stderr is rendered\n * to the user as an inline notice.\n *\n * stdin JSON shape (one envelope per event):\n *\n * {\n * \"event\": \"PreToolUse\" | \"PostToolUse\" | \"UserPromptSubmit\" | \"Stop\",\n * \"cwd\": \"<absolute project root or process.cwd()>\",\n * \"toolName\": \"<string>\", // tool events only\n * \"toolArgs\": <unknown>, // tool events only — already JSON-decoded\n * \"toolResult\": \"<string>\", // PostToolUse only — same body the model sees\n * \"prompt\": \"<string>\", // UserPromptSubmit only\n * \"lastAssistantText\": \"<string>\", // Stop only\n * \"turn\": <number>, // Stop only\n * }\n *\n * Hooks are executed in order: project scope first, then global.\n * `Pre*` events stop dispatching at the first block; non-block\n * outcomes accumulate into a single report so the UI can render\n * each warning inline.\n */\n\nimport { spawn } from \"node:child_process\";\nimport { existsSync, readFileSync } from \"node:fs\";\nimport { homedir } from \"node:os\";\nimport { join } from \"node:path\";\n\nexport type HookEvent = \"PreToolUse\" | \"PostToolUse\" | \"UserPromptSubmit\" | \"Stop\";\n\n/** All four events as a const array — drives slash listing + validation. */\nexport const HOOK_EVENTS: readonly HookEvent[] = [\n \"PreToolUse\",\n \"PostToolUse\",\n \"UserPromptSubmit\",\n \"Stop\",\n] as const;\n\n/** Only the gating events can block the loop. */\nconst BLOCKING_EVENTS: ReadonlySet<HookEvent> = new Set([\"PreToolUse\", \"UserPromptSubmit\"]);\n\n/** Per-event default timeout. Tool/prompt hooks gate progress, so they're tight. */\nconst DEFAULT_TIMEOUTS_MS: Record<HookEvent, number> = {\n PreToolUse: 5_000,\n UserPromptSubmit: 5_000,\n PostToolUse: 30_000,\n Stop: 30_000,\n};\n\nexport type HookScope = \"project\" | \"global\";\n\nexport interface HookConfig {\n /**\n * Tool-name pattern (PreToolUse / PostToolUse only). Anchored regex.\n * Omitted or `\"*\"` matches every tool. Ignored for prompt / Stop\n * events (they have no tool name to match against).\n */\n match?: string;\n /** Shell command to run. Spawned through the platform shell. */\n command: string;\n /** Optional human description — surfaced in `/hooks`. */\n description?: string;\n /** Per-hook timeout override in ms. */\n timeout?: number;\n /**\n * Working directory for the spawned process. Defaults to:\n * - project scope → the project root\n * - global scope → process.cwd()\n */\n cwd?: string;\n}\n\n/** Shape of `<scope>/.reasonix/settings.json` — only `hooks` for now. */\nexport interface HookSettings {\n hooks?: Partial<Record<HookEvent, HookConfig[]>>;\n}\n\n/** A loaded hook with its origin scope baked in (used for ordering and `/hooks`). */\nexport interface ResolvedHook extends HookConfig {\n event: HookEvent;\n scope: HookScope;\n /** Absolute path to the settings.json the hook came from. */\n source: string;\n}\n\n/** Outcome of a single hook invocation. */\nexport interface HookOutcome {\n /** Which hook fired. */\n hook: ResolvedHook;\n /**\n * Decision:\n * - `pass` — exit 0\n * - `block` — exit 2 on a blocking event (otherwise downgraded to `warn`)\n * - `warn` — non-zero exit that is not a successful block\n * - `timeout` — the spawn was killed past `timeout`\n * - `error` — could not spawn at all (missing command, etc.)\n */\n decision: \"pass\" | \"block\" | \"warn\" | \"timeout\" | \"error\";\n exitCode: number | null;\n /** Captured stdout (trimmed). May be empty. */\n stdout: string;\n /** Captured stderr (trimmed). The block / warn message comes from here. */\n stderr: string;\n durationMs: number;\n}\n\n/** Aggregate report for `runHooks`. */\nexport interface HookReport {\n event: HookEvent;\n outcomes: HookOutcome[];\n /** True iff at least one outcome was a `block` — only meaningful for blocking events. */\n blocked: boolean;\n}\n\nexport const HOOK_SETTINGS_FILENAME = \"settings.json\";\nexport const HOOK_SETTINGS_DIRNAME = \".reasonix\";\n\n/** Where the global settings.json lives. Equivalent to `~/.reasonix/settings.json`. */\nexport function globalSettingsPath(homeDirOverride?: string): string {\n return join(homeDirOverride ?? homedir(), HOOK_SETTINGS_DIRNAME, HOOK_SETTINGS_FILENAME);\n}\n\n/** Where the project settings.json lives for a given root. */\nexport function projectSettingsPath(projectRoot: string): string {\n return join(projectRoot, HOOK_SETTINGS_DIRNAME, HOOK_SETTINGS_FILENAME);\n}\n\nfunction readSettingsFile(path: string): HookSettings | null {\n if (!existsSync(path)) return null;\n try {\n const raw = readFileSync(path, \"utf8\");\n const parsed = JSON.parse(raw);\n if (parsed && typeof parsed === \"object\") return parsed as HookSettings;\n } catch {\n /* malformed JSON → treat as no hooks; do NOT throw, the user\n * shouldn't lose the whole CLI to a typo in their settings */\n }\n return null;\n}\n\n/**\n * Pull every configured hook out of the project + global settings\n * files, in the order they should fire (project first, global second,\n * within each scope: array order from the file).\n *\n * Returns a flat list — the dispatcher filters by event + match\n * pattern at run time. Loading is cheap (one or two JSON files), so\n * we don't memoize across processes; re-load is allowed via\n * `/hooks reload` and on every fresh App mount.\n */\nexport interface LoadHookSettingsOptions {\n /** Absolute project root, if any. Without it, only global hooks load. */\n projectRoot?: string;\n /** Override `~` for tests. */\n homeDir?: string;\n}\n\nexport function loadHooks(opts: LoadHookSettingsOptions = {}): ResolvedHook[] {\n const out: ResolvedHook[] = [];\n if (opts.projectRoot) {\n const projPath = projectSettingsPath(opts.projectRoot);\n const settings = readSettingsFile(projPath);\n if (settings) appendResolved(out, settings, \"project\", projPath);\n }\n const globalPath = globalSettingsPath(opts.homeDir);\n const settings = readSettingsFile(globalPath);\n if (settings) appendResolved(out, settings, \"global\", globalPath);\n return out;\n}\n\nfunction appendResolved(\n out: ResolvedHook[],\n settings: HookSettings,\n scope: HookScope,\n source: string,\n): void {\n if (!settings.hooks) return;\n for (const event of HOOK_EVENTS) {\n const list = settings.hooks[event];\n if (!Array.isArray(list)) continue;\n for (const cfg of list) {\n if (!cfg || typeof cfg.command !== \"string\" || cfg.command.trim() === \"\") continue;\n out.push({ ...cfg, event, scope, source });\n }\n }\n}\n\n/**\n * True if `toolName` matches the hook's `match` field. `\"*\"` and\n * undefined match everything. Otherwise we anchor the field as a\n * regex — partial-name matches don't fire, so `\"file\"` would not\n * trigger on `read_file` (use `\".*file\"` for that).\n */\nexport function matchesTool(hook: ResolvedHook, toolName: string): boolean {\n if (hook.event !== \"PreToolUse\" && hook.event !== \"PostToolUse\") return true;\n const m = hook.match;\n if (!m || m === \"*\") return true;\n try {\n const re = new RegExp(`^(?:${m})$`);\n return re.test(toolName);\n } catch {\n /* malformed regex → don't fire (safer than firing on every tool) */\n return false;\n }\n}\n\n/** Payload envelope passed to hook stdin. */\nexport interface HookPayload {\n event: HookEvent;\n cwd: string;\n toolName?: string;\n toolArgs?: unknown;\n toolResult?: string;\n prompt?: string;\n lastAssistantText?: string;\n turn?: number;\n}\n\n/** Test seam — same shape as Node's spawn but returns a Promise of the raw outcome bits. */\nexport interface HookSpawnInput {\n command: string;\n cwd: string;\n stdin: string;\n timeoutMs: number;\n}\n\nexport interface HookSpawnResult {\n exitCode: number | null;\n stdout: string;\n stderr: string;\n timedOut: boolean;\n /** True iff spawn() itself failed (ENOENT, EACCES, …). */\n spawnError?: Error;\n}\n\nexport type HookSpawner = (input: HookSpawnInput) => Promise<HookSpawnResult>;\n\n/**\n * Default spawner — runs `command` through the platform shell so\n * `&&`, pipes, env-var expansion all work without a tokenizer.\n * Stdin is the JSON payload, stdout / stderr are buffered.\n *\n * Why `shell: true`? A hook is intentionally a shell command — that's\n * the contract. Treating it like an argv array would surprise users\n * who write `bun run check && eslint .` and expect it to behave the\n * way it does in their terminal.\n */\nfunction defaultSpawner(input: HookSpawnInput): Promise<HookSpawnResult> {\n return new Promise<HookSpawnResult>((resolve) => {\n const child = spawn(input.command, {\n cwd: input.cwd,\n shell: true,\n stdio: [\"pipe\", \"pipe\", \"pipe\"],\n });\n let stdout = \"\";\n let stderr = \"\";\n let timedOut = false;\n const timer = setTimeout(() => {\n timedOut = true;\n child.kill(\"SIGTERM\");\n // SIGTERM may not land on Windows for shell children — followed\n // by a hard kill a moment later if the process is still around.\n setTimeout(() => {\n try {\n child.kill(\"SIGKILL\");\n } catch {\n /* already gone */\n }\n }, 500);\n }, input.timeoutMs);\n\n child.stdout.on(\"data\", (chunk: Buffer) => {\n stdout += chunk.toString(\"utf8\");\n });\n child.stderr.on(\"data\", (chunk: Buffer) => {\n stderr += chunk.toString(\"utf8\");\n });\n child.once(\"error\", (err) => {\n clearTimeout(timer);\n resolve({\n exitCode: null,\n stdout,\n stderr,\n timedOut: false,\n spawnError: err,\n });\n });\n child.once(\"close\", (code) => {\n clearTimeout(timer);\n resolve({\n exitCode: code,\n stdout: stdout.trim(),\n stderr: stderr.trim(),\n timedOut,\n });\n });\n\n try {\n child.stdin.write(input.stdin);\n child.stdin.end();\n } catch {\n /* stdin write can race with spawn errors; the close handler\n * still fires with exit 0/null */\n }\n });\n}\n\n/**\n * Format a hook outcome as a single-line UI string. Used by both the\n * loop (for `warning` events) and the App (for UserPromptSubmit /\n * Stop outcomes). Centralizing keeps the language consistent across\n * scopes.\n */\nexport function formatHookOutcomeMessage(outcome: HookOutcome): string {\n if (outcome.decision === \"pass\") return \"\";\n const detail = (outcome.stderr || outcome.stdout || \"\").trim();\n const tag = `${outcome.hook.scope}/${outcome.hook.event}`;\n const cmd =\n outcome.hook.command.length > 60\n ? `${outcome.hook.command.slice(0, 60)}…`\n : outcome.hook.command;\n const head = `hook ${tag} \\`${cmd}\\` ${outcome.decision}`;\n return detail ? `${head}: ${detail}` : head;\n}\n\n/**\n * Decide the hook's outcome decision from raw spawn results.\n * Pulled out as a pure function so tests can pin the matrix.\n */\nexport function decideOutcome(\n event: HookEvent,\n raw: HookSpawnResult,\n): \"pass\" | \"block\" | \"warn\" | \"timeout\" | \"error\" {\n if (raw.spawnError) return \"error\";\n if (raw.timedOut) return BLOCKING_EVENTS.has(event) ? \"block\" : \"warn\";\n if (raw.exitCode === 0) return \"pass\";\n if (raw.exitCode === 2 && BLOCKING_EVENTS.has(event)) return \"block\";\n return \"warn\";\n}\n\nexport interface RunHooksOptions {\n payload: HookPayload;\n hooks: ResolvedHook[];\n /** Test seam — defaults to a real `spawn`. */\n spawner?: HookSpawner;\n}\n\n/**\n * Filter hooks down to the ones that match `payload.event` (and\n * `payload.toolName`, for tool events), then run them in order.\n * Stops at the first `block` outcome on a blocking event so a\n * gating hook can prevent later hooks from incorrectly seeing a\n * success that wasn't going to happen.\n */\nexport async function runHooks(opts: RunHooksOptions): Promise<HookReport> {\n const spawner = opts.spawner ?? defaultSpawner;\n const event = opts.payload.event;\n const toolName = opts.payload.toolName ?? \"\";\n const matching = opts.hooks.filter((h) => h.event === event && matchesTool(h, toolName));\n\n const outcomes: HookOutcome[] = [];\n let blocked = false;\n const stdin = `${JSON.stringify(opts.payload)}\\n`;\n\n for (const hook of matching) {\n const start = Date.now();\n const timeoutMs = hook.timeout ?? DEFAULT_TIMEOUTS_MS[event];\n const cwd = hook.cwd ?? opts.payload.cwd;\n const raw = await spawner({ command: hook.command, cwd, stdin, timeoutMs });\n const decision = decideOutcome(event, raw);\n outcomes.push({\n hook,\n decision,\n exitCode: raw.exitCode,\n stdout: raw.stdout,\n stderr:\n raw.stderr ||\n (raw.spawnError ? raw.spawnError.message : \"\") ||\n (raw.timedOut ? `hook timed out after ${timeoutMs}ms` : \"\"),\n durationMs: Date.now() - start,\n });\n if (decision === \"block\") {\n blocked = true;\n break;\n }\n }\n\n return { event, outcomes, blocked };\n}\n","/**\n * Schema flattening for DeepSeek tool calls.\n *\n * DeepSeek loses arguments on schemas that are deep (>2 levels of nesting) or\n * wide (>10 leaf parameters). This module transforms such schemas into a\n * dot-notation flat schema and re-nests the model's arguments before dispatch.\n *\n * Example:\n * { user: { profile: { name, age } } } ⇄ \"user.profile.name\", \"user.profile.age\"\n */\n\nimport type { JSONSchema } from \"../types.js\";\n\nexport interface FlattenDecision {\n shouldFlatten: boolean;\n leafCount: number;\n maxDepth: number;\n}\n\nexport function analyzeSchema(schema: JSONSchema | undefined): FlattenDecision {\n if (!schema) return { shouldFlatten: false, leafCount: 0, maxDepth: 0 };\n let leafCount = 0;\n let maxDepth = 0;\n walk(schema, 0, (depth, isLeaf) => {\n if (isLeaf) leafCount++;\n if (depth > maxDepth) maxDepth = depth;\n });\n return {\n shouldFlatten: leafCount > 10 || maxDepth > 2,\n leafCount,\n maxDepth,\n };\n}\n\nexport function flattenSchema(schema: JSONSchema): JSONSchema {\n const flatProps: Record<string, JSONSchema> = {};\n const required: string[] = [];\n collect(\"\", schema, flatProps, required, true);\n return {\n type: \"object\",\n properties: flatProps,\n required,\n };\n}\n\nexport function nestArguments(flatArgs: Record<string, unknown>): Record<string, unknown> {\n const out: Record<string, unknown> = {};\n for (const [key, value] of Object.entries(flatArgs)) {\n setByPath(out, key.split(\".\"), value);\n }\n return out;\n}\n\nfunction walk(\n schema: JSONSchema,\n depth: number,\n visit: (depth: number, isLeaf: boolean) => void,\n): void {\n if (schema.type === \"object\" && schema.properties) {\n for (const child of Object.values(schema.properties)) {\n walk(child, depth + 1, visit);\n }\n return;\n }\n if (schema.type === \"array\" && schema.items) {\n walk(schema.items, depth + 1, visit);\n return;\n }\n visit(depth, true);\n}\n\nfunction collect(\n prefix: string,\n schema: JSONSchema,\n out: Record<string, JSONSchema>,\n required: string[],\n isRootRequired: boolean,\n): void {\n if (schema.type === \"object\" && schema.properties) {\n const requiredSet = new Set(schema.required ?? []);\n for (const [key, child] of Object.entries(schema.properties)) {\n const nextPrefix = prefix ? `${prefix}.${key}` : key;\n const childRequired = isRootRequired && requiredSet.has(key);\n collect(nextPrefix, child, out, required, childRequired);\n }\n return;\n }\n // Treat anything non-object (including arrays) as a leaf for flattening purposes.\n out[prefix] = schema;\n if (isRootRequired) required.push(prefix);\n}\n\nfunction setByPath(target: Record<string, unknown>, path: string[], value: unknown): void {\n let cur: any = target;\n for (let i = 0; i < path.length - 1; i++) {\n const key = path[i]!;\n if (typeof cur[key] !== \"object\" || cur[key] === null) cur[key] = {};\n cur = cur[key];\n }\n cur[path[path.length - 1]!] = value;\n}\n","import { analyzeSchema, flattenSchema, nestArguments } from \"./repair/flatten.js\";\nimport type { JSONSchema, ToolSpec } from \"./types.js\";\n\n/**\n * Per-call context a tool `fn` can optionally consume. Today the only\n * field is `signal`, plumbed through so long-running tools (MCP calls,\n * HTTP requests) can abort when the user presses Esc. Omitted fields\n * stay optional — tools written against the pre-0.4.9 signature keep\n * working; they just ignore cancellation, which is fine for fast\n * local work where \"await finishes\" happens before the next tick anyway.\n */\nexport interface ToolCallContext {\n signal?: AbortSignal;\n}\n\nexport interface ToolDefinition<A = any, R = any> {\n name: string;\n description?: string;\n parameters?: JSONSchema;\n /**\n * Marks a tool as read-only: safe to invoke during plan mode. `true`\n * for tools that only observe (read_file, list_directory, search, web\n * fetch/search). Leave undefined / `false` for anything that can write,\n * execute, or mutate state.\n *\n * The registry enforces this at dispatch: non-readonly tools called\n * while `planMode` is on return a refusal string the model can\n * learn from, instead of actually running.\n */\n readOnly?: boolean;\n /**\n * Dynamic read-only check for tools whose safety depends on arguments\n * — `run_command` with an allowlisted argv is safe, `run_command\n * rm -rf` isn't. Called with the parsed arguments; `true` means \"treat\n * as read-only for plan mode\". Takes precedence over `readOnly` when\n * both are set.\n */\n readOnlyCheck?: (args: A) => boolean;\n fn: (args: A, ctx?: ToolCallContext) => R | Promise<R>;\n}\n\ninterface InternalTool extends ToolDefinition {\n /**\n * Pillar 3 — flatten metadata. Set when the registered schema is deep\n * (>2 levels) or wide (>10 leaf params), conditions on which DeepSeek\n * V3/R1 are known to drop arguments. We advertise the flattened schema\n * to the model, then re-nest the model's args before calling fn.\n */\n flatSchema?: JSONSchema;\n}\n\nexport interface ToolRegistryOptions {\n /**\n * Auto-flatten schemas that exceed depth/width thresholds before sending\n * them to the model. Re-nests arguments transparently on dispatch.\n * Default: true. Pass false to opt out.\n */\n autoFlatten?: boolean;\n}\n\nexport class ToolRegistry {\n private readonly _tools = new Map<string, InternalTool>();\n private readonly _autoFlatten: boolean;\n /**\n * When true, `dispatch` refuses any tool whose `readOnly` flag isn't\n * set (and whose `readOnlyCheck` doesn't pass on the specific args).\n * Drives `reasonix code`'s Plan Mode — the model can still explore\n * via read tools but its writes and non-allowlisted shell calls are\n * bounced until the user approves a submitted plan.\n */\n private _planMode = false;\n\n constructor(opts: ToolRegistryOptions = {}) {\n this._autoFlatten = opts.autoFlatten !== false;\n }\n\n /** Enable / disable plan-mode enforcement at dispatch. */\n setPlanMode(on: boolean): void {\n this._planMode = Boolean(on);\n }\n\n /** True when the registry is currently refusing non-readonly calls. */\n get planMode(): boolean {\n return this._planMode;\n }\n\n register<A, R>(def: ToolDefinition<A, R>): this {\n if (!def.name) throw new Error(\"tool requires a name\");\n const internal: InternalTool = { ...(def as ToolDefinition) };\n if (this._autoFlatten && def.parameters) {\n const decision = analyzeSchema(def.parameters);\n if (decision.shouldFlatten) {\n internal.flatSchema = flattenSchema(def.parameters);\n }\n }\n this._tools.set(def.name, internal);\n return this;\n }\n\n has(name: string): boolean {\n return this._tools.has(name);\n }\n\n get(name: string): ToolDefinition | undefined {\n return this._tools.get(name);\n }\n\n get size(): number {\n return this._tools.size;\n }\n\n /** True if a registered tool's schema was flattened for the model. */\n wasFlattened(name: string): boolean {\n return Boolean(this._tools.get(name)?.flatSchema);\n }\n\n specs(): ToolSpec[] {\n return [...this._tools.values()].map((t) => ({\n type: \"function\",\n function: {\n name: t.name,\n description: t.description ?? \"\",\n parameters: t.flatSchema ?? t.parameters ?? { type: \"object\", properties: {} },\n },\n }));\n }\n\n async dispatch(\n name: string,\n argumentsRaw: string | Record<string, unknown>,\n opts: { signal?: AbortSignal } = {},\n ): Promise<string> {\n const tool = this._tools.get(name);\n if (!tool) {\n return JSON.stringify({ error: `unknown tool: ${name}` });\n }\n let args: Record<string, unknown>;\n try {\n args =\n typeof argumentsRaw === \"string\"\n ? argumentsRaw.trim()\n ? (JSON.parse(argumentsRaw) ?? {})\n : {}\n : (argumentsRaw ?? {});\n } catch (err) {\n return JSON.stringify({\n error: `invalid tool arguments JSON: ${(err as Error).message}`,\n });\n }\n\n // Re-nest dot-notation args back to the original shape, but only when\n // (a) we flattened this tool's schema, AND\n // (b) the incoming args actually use dot keys.\n // The second condition handles the case where a model ignores the flat\n // spec and emits nested args anyway — we shouldn't double-process them.\n if (tool.flatSchema && args && typeof args === \"object\" && hasDotKey(args)) {\n args = nestArguments(args);\n }\n\n // Plan-mode enforcement — runs AFTER arg parsing so a tool with a\n // runtime `readOnlyCheck` can inspect the actual args (e.g.\n // `run_command` is read-only iff the command matches its allowlist).\n if (this._planMode && !isReadOnlyCall(tool, args)) {\n return JSON.stringify({\n error: `${name}: unavailable in plan mode — this is a read-only exploration phase. Use read_file / list_directory / search_files / directory_tree / web_search / allowlisted shell commands to investigate. Call submit_plan with your proposed plan when you're ready for the user's review.`,\n });\n }\n\n try {\n const result = await tool.fn(args, { signal: opts.signal });\n return typeof result === \"string\" ? result : JSON.stringify(result);\n } catch (err) {\n const e = err as Error & { toToolResult?: () => unknown };\n // Errors may opt into a richer tool-result shape by implementing\n // `toToolResult()`. Used by `PlanProposedError` to smuggle the\n // submitted plan text out to the UI without stuffing it into the\n // error message (which the dispatcher truncates at no fixed limit,\n // but keeping payloads structured is cleaner for UI parsing).\n if (typeof e.toToolResult === \"function\") {\n try {\n return JSON.stringify(e.toToolResult());\n } catch {\n /* fall through to the default shape */\n }\n }\n return JSON.stringify({\n error: `${e.name}: ${e.message}`,\n });\n }\n }\n}\n\nfunction isReadOnlyCall(tool: InternalTool, args: Record<string, unknown>): boolean {\n if (tool.readOnlyCheck) {\n try {\n return Boolean(tool.readOnlyCheck(args as never));\n } catch {\n return false;\n }\n }\n return tool.readOnly === true;\n}\n\nfunction hasDotKey(obj: Record<string, unknown>): boolean {\n for (const k of Object.keys(obj)) {\n if (k.includes(\".\")) return true;\n }\n return false;\n}\n","/**\n * Bridge: register an MCP server's tools into a Reasonix ToolRegistry.\n *\n * This is the integration surface. Once done, `CacheFirstLoop` sees the\n * MCP tools as if they were native — they inherit Cache-First + repair\n * (scavenge / truncation / storm) automatically. That's the payoff: any\n * MCP ecosystem tool, wrapped in Reasonix's Pillar 1 + Pillar 3 benefits.\n */\n\nimport { ToolRegistry } from \"../tools.js\";\nimport type { JSONSchema } from \"../types.js\";\nimport type { McpClient } from \"./client.js\";\nimport type { CallToolResult, McpContentBlock } from \"./types.js\";\n\nexport interface BridgeOptions {\n /**\n * Prefix prepended to every MCP tool name when registered. Defaults to\n * empty (no prefix). Useful when bridging multiple servers into one\n * registry and names collide — e.g. `fs` + `gh` both exposing `search`.\n */\n namePrefix?: string;\n /** Registry to populate. Creates a fresh one if omitted. */\n registry?: ToolRegistry;\n /** Auto-flatten deep schemas (Pillar 3). Defaults to the registry's own default (true). */\n autoFlatten?: boolean;\n /**\n * Per-tool-call result cap, in characters. If a tool returns more than\n * this, the result is truncated and a `[…truncated N chars…]` marker is\n * appended before the last KB so the model still sees a useful tail.\n * Defaults to {@link DEFAULT_MAX_RESULT_CHARS}.\n *\n * Why this exists: DeepSeek V3's context is 131,072 tokens. A single\n * `read_file` against a big source file can return >3 MB of text\n * (~900k tokens) and permanently poison the session — every subsequent\n * turn rebuilds the history and 400s. This cap is a floor. Users who\n * legitimately want bigger payloads can raise it explicitly.\n */\n maxResultChars?: number;\n /**\n * Callback fired for every `notifications/progress` frame the server\n * emits during any bridged tool call. Includes the registered\n * (prefix-applied) tool name so a multi-server UI can attribute\n * progress correctly. Absent → no `_meta.progressToken` is sent and\n * the server won't emit progress for these calls.\n */\n onProgress?: (info: {\n toolName: string;\n progress: number;\n total?: number;\n message?: string;\n }) => void;\n}\n\n/**\n * 32,000 chars ≈ 8k English tokens, or ~16k CJK tokens. Small enough to\n * fit comfortably in history even across 5–10 tool calls, large enough\n * that most file reads and directory listings fit un-truncated.\n */\nexport const DEFAULT_MAX_RESULT_CHARS = 32_000;\n\nexport interface BridgeResult {\n registry: ToolRegistry;\n /** Names actually registered (may differ from MCP names when a prefix is applied). */\n registeredNames: string[];\n /** Names the server listed but the bridge skipped (e.g. invalid schemas). */\n skipped: Array<{ name: string; reason: string }>;\n}\n\n/**\n * Walk a connected `McpClient`'s tools/list result, register each into a\n * Reasonix `ToolRegistry`. Each registered `fn` proxies through the\n * client's tools/call. Tool results are flattened into a string (joining\n * text blocks with newlines, prefixing image blocks as placeholders) so\n * they fit Reasonix's existing tool-dispatch contract.\n */\nexport async function bridgeMcpTools(\n client: McpClient,\n opts: BridgeOptions = {},\n): Promise<BridgeResult> {\n const registry = opts.registry ?? new ToolRegistry({ autoFlatten: opts.autoFlatten });\n const prefix = opts.namePrefix ?? \"\";\n const maxResultChars = opts.maxResultChars ?? DEFAULT_MAX_RESULT_CHARS;\n const result: BridgeResult = { registry, registeredNames: [], skipped: [] };\n\n const listed = await client.listTools();\n for (const mcpTool of listed.tools) {\n if (!mcpTool.name) {\n result.skipped.push({ name: \"?\", reason: \"empty tool name\" });\n continue;\n }\n const registeredName = `${prefix}${mcpTool.name}`;\n registry.register({\n name: registeredName,\n description: mcpTool.description ?? \"\",\n parameters: mcpTool.inputSchema as JSONSchema,\n fn: async (args: Record<string, unknown>, ctx) => {\n const toolResult = await client.callTool(mcpTool.name, args, {\n // Forward server-side progress frames to the bridge caller,\n // tagged with the registered name so multi-server UIs can\n // disambiguate. No-op when `onProgress` isn't configured —\n // the client then also omits the _meta.progressToken and\n // the server won't emit progress.\n onProgress: opts.onProgress\n ? (info) => opts.onProgress!({ toolName: registeredName, ...info })\n : undefined,\n // Thread the tool-dispatch AbortSignal all the way down to\n // the MCP request so Esc truly cancels in flight — the\n // client will emit notifications/cancelled AND reject the\n // pending promise immediately, no \"wait for subprocess\".\n signal: ctx?.signal,\n });\n return flattenMcpResult(toolResult, { maxChars: maxResultChars });\n },\n });\n result.registeredNames.push(registeredName);\n }\n return result;\n}\n\nexport interface FlattenOptions {\n /** Cap the flattened string at this many characters. Default: no cap. */\n maxChars?: number;\n}\n\n/**\n * Turn an MCP CallToolResult into a string — the contract Reasonix's\n * ToolRegistry.dispatch returns. We:\n * - join text blocks with newlines (most common case)\n * - stringify image blocks as placeholders (LLM can't use bytes anyway\n * in Reasonix's current surface; image support comes with multimodal\n * prompts later)\n * - prefix error results with \"ERROR: \" so the calling model sees the\n * failure clearly even through JSON mode\n * - optionally truncate to `maxChars` so a single oversized tool result\n * (e.g. a big `read_file`) can't poison the session by blowing past\n * the model's context window\n */\nexport function flattenMcpResult(result: CallToolResult, opts: FlattenOptions = {}): string {\n const parts = result.content.map(blockToString);\n const joined = parts.join(\"\\n\").trim();\n const prefixed = result.isError ? `ERROR: ${joined || \"(no error message from server)\"}` : joined;\n return opts.maxChars ? truncateForModel(prefixed, opts.maxChars) : prefixed;\n}\n\n/**\n * Keep the head AND a short tail so the model sees both \"what the tool\n * started returning\" and \"how it ended\". Head-only loses file endings\n * (e.g. an error message appended at the bottom of a stack trace); the\n * 1KB tail window covers that while costing almost nothing. Exported for\n * tests and reuse by non-MCP tool adapters that want the same policy.\n */\nexport function truncateForModel(s: string, maxChars: number): string {\n if (s.length <= maxChars) return s;\n const tailBudget = Math.min(1024, Math.floor(maxChars * 0.1));\n const headBudget = Math.max(0, maxChars - tailBudget);\n const head = s.slice(0, headBudget);\n const tail = s.slice(-tailBudget);\n const dropped = s.length - head.length - tail.length;\n return `${head}\\n\\n[…truncated ${dropped} chars — raise BridgeOptions.maxResultChars, or call the tool with a narrower scope (filter, head, pagination)…]\\n\\n${tail}`;\n}\n\nfunction blockToString(block: McpContentBlock): string {\n if (block.type === \"text\") return block.text;\n if (block.type === \"image\") return `[image ${block.mimeType}, ${block.data.length} chars base64]`;\n // Unknown block type — preserve for diagnostics.\n return `[unknown block: ${JSON.stringify(block)}]`;\n}\n","import { createHash } from \"node:crypto\";\nimport type { ChatMessage, ToolSpec } from \"./types.js\";\n\nexport interface ImmutablePrefixOptions {\n system: string;\n toolSpecs?: readonly ToolSpec[];\n fewShots?: readonly ChatMessage[];\n}\n\nexport class ImmutablePrefix {\n readonly system: string;\n readonly toolSpecs: readonly ToolSpec[];\n readonly fewShots: readonly ChatMessage[];\n\n constructor(opts: ImmutablePrefixOptions) {\n this.system = opts.system;\n this.toolSpecs = Object.freeze([...(opts.toolSpecs ?? [])]);\n this.fewShots = Object.freeze([...(opts.fewShots ?? [])]);\n }\n\n toMessages(): ChatMessage[] {\n return [{ role: \"system\", content: this.system }, ...this.fewShots.map((m) => ({ ...m }))];\n }\n\n tools(): ToolSpec[] {\n return this.toolSpecs.map((t) => structuredClone(t) as ToolSpec);\n }\n\n get fingerprint(): string {\n const blob = JSON.stringify({\n system: this.system,\n tools: this.toolSpecs,\n shots: this.fewShots,\n });\n return createHash(\"sha256\").update(blob).digest(\"hex\").slice(0, 16);\n }\n}\n\nexport class AppendOnlyLog {\n private _entries: ChatMessage[] = [];\n\n append(message: ChatMessage): void {\n if (!message || typeof message !== \"object\" || !(\"role\" in message)) {\n throw new Error(`invalid log entry: ${JSON.stringify(message)}`);\n }\n this._entries.push(message);\n }\n\n extend(messages: ChatMessage[]): void {\n for (const m of messages) this.append(m);\n }\n\n /**\n * Bulk-replace entries. Intentionally named to be hard to reach for —\n * this is the one mutation path that breaks the log's append-only\n * spirit, reserved for compaction flows (`/compact`) and recovery\n * where the caller has consciously decided to drop old history. Any\n * other use is almost certainly wrong; append() is what you want.\n */\n compactInPlace(replacement: ChatMessage[]): void {\n this._entries = [...replacement];\n }\n\n get entries(): readonly ChatMessage[] {\n return this._entries;\n }\n\n toMessages(): ChatMessage[] {\n return this._entries.map((e) => ({ ...e }));\n }\n\n get length(): number {\n return this._entries.length;\n }\n}\n\nexport class VolatileScratch {\n reasoning: string | null = null;\n planState: Record<string, unknown> | null = null;\n notes: string[] = [];\n\n reset(): void {\n this.reasoning = null;\n this.planState = null;\n this.notes = [];\n }\n}\n","/**\n * Scavenge tool calls leaked into reasoning_content.\n *\n * R1 sometimes emits tool-call JSON inside <think>…</think> and then forgets\n * to surface it in `tool_calls`. This pass extracts plausible calls and\n * proposes them to the loop, which decides whether to merge them with the\n * declared calls.\n */\n\nimport type { ToolCall } from \"../types.js\";\n\nexport interface ScavengeOptions {\n /** Names of tools the model may legitimately call. Other names are ignored. */\n allowedNames: ReadonlySet<string>;\n /** Maximum number of calls to scavenge per pass (defence against runaway). */\n maxCalls?: number;\n}\n\nexport interface ScavengeResult {\n calls: ToolCall[];\n notes: string[];\n}\n\nexport function scavengeToolCalls(\n reasoningContent: string | null | undefined,\n opts: ScavengeOptions,\n): ScavengeResult {\n if (!reasoningContent) return { calls: [], notes: [] };\n const max = opts.maxCalls ?? 4;\n const notes: string[] = [];\n const out: ToolCall[] = [];\n\n // Pattern A: DSML invoke blocks. R1 sometimes emits tool calls as\n // its chat-template markup in the content channel instead of the\n // proper `tool_calls` field. 0.4.3 stripped these from display;\n // here we actually turn them back into proper ToolCalls so the\n // model's intent isn't lost.\n for (const invoke of iterateDsmlInvokes(reasoningContent)) {\n if (out.length >= max) break;\n if (!opts.allowedNames.has(invoke.name)) continue;\n out.push({\n function: {\n name: invoke.name,\n arguments: JSON.stringify(invoke.args),\n },\n });\n notes.push(`scavenged DSML call: ${invoke.name}`);\n }\n\n // Pattern B: raw JSON objects (the original three shapes). Strip\n // any DSML blocks we already processed so parameter JSON buried\n // inside them doesn't get re-scavenged as a standalone call.\n const nonDsml = stripDsmlBlocks(reasoningContent);\n for (const candidate of iterateJsonObjects(nonDsml)) {\n if (out.length >= max) break;\n const call = coerceToToolCall(candidate, opts.allowedNames);\n if (call) {\n out.push(call);\n notes.push(`scavenged call: ${call.function.name}`);\n }\n }\n return { calls: out, notes };\n}\n\ninterface DsmlInvoke {\n name: string;\n args: Record<string, unknown>;\n}\n\n/**\n * Yield DeepSeek DSML invoke blocks scavenged from text. R1's chat\n * template uses these when the server-side tool-call serializer\n * misfires — we see them verbatim in the content channel:\n *\n * <|DSML|function_calls>\n * <|DSML|invoke name=\"filesystem_edit_file\">\n * <|DSML|parameter name=\"path\" string=\"true\">F:/x.html</|DSML|parameter>\n * <|DSML|parameter name=\"edits\" string=\"false\">[…]</|DSML|parameter>\n * </|DSML|invoke>\n * </|DSML|function_calls>\n *\n * The wrapper class (| U+FF5C vs ASCII |) varies by model build, so\n * we accept both. `string=\"true\"` means the parameter value is a\n * literal string; `string=\"false\"` means it's JSON.\n */\n/**\n * Remove DSML invoke/function_calls blocks from text so the raw-JSON\n * scanner below doesn't see parameter payloads as standalone calls.\n * Mirrors the strip pass in `stripHallucinatedToolMarkup` but scoped\n * to scavenge (we also want the `function_calls` wrapper gone —\n * nothing useful to scavenge there).\n */\nfunction stripDsmlBlocks(text: string): string {\n let out = text;\n out = out.replace(/<[||]DSML[||]function_calls>[\\s\\S]*?<\\/?[||]DSML[||]function_calls>/g, \"\");\n out = out.replace(/<[||]DSML[||]invoke\\s+[^>]*>[\\s\\S]*?<\\/[||]DSML[||]invoke>/g, \"\");\n return out;\n}\n\nfunction* iterateDsmlInvokes(text: string): Generator<DsmlInvoke> {\n // `|` (U+FF5C) in practice; `|` (ASCII) as a fallback seen in a\n // minority of builds. `[||]` inside the regex covers both.\n const INVOKE_RE = /<[||]DSML[||]invoke\\s+name=\"([^\"]+)\">([\\s\\S]*?)<\\/[||]DSML[||]invoke>/g;\n for (const match of text.matchAll(INVOKE_RE)) {\n const name = match[1];\n const body = match[2];\n if (!name || body === undefined) continue;\n yield { name, args: parseDsmlParameters(body) };\n }\n}\n\n/**\n * Parse the parameter children of a DSML invoke body into a plain\n * object. We tolerate:\n * - `string=\"true\"` → literal text; whitespace trimmed at the edges.\n * - `string=\"false\"` → JSON-parsed; falls back to literal text if the\n * payload turns out to be malformed (better to hand a string\n * through than lose the parameter entirely).\n * - no `string=\"…\"` attribute → treat as literal text (older/shorter\n * DSML shapes we haven't formally seen but want to handle).\n */\nfunction parseDsmlParameters(body: string): Record<string, unknown> {\n const PARAM_RE =\n /<[||]DSML[||]parameter\\s+name=\"([^\"]+)\"(?:\\s+string=\"(true|false)\")?\\s*>([\\s\\S]*?)<\\/[||]DSML[||]parameter>/g;\n const args: Record<string, unknown> = {};\n for (const m of body.matchAll(PARAM_RE)) {\n const key = m[1];\n const stringFlag = m[2];\n const raw = (m[3] ?? \"\").trim();\n if (!key) continue;\n if (stringFlag === \"false\") {\n try {\n args[key] = JSON.parse(raw);\n continue;\n } catch {\n // Fall through — keep as literal so the information isn't lost.\n }\n }\n args[key] = raw;\n }\n return args;\n}\n\n/** Yield every top-level JSON object substring in `text`. */\nfunction* iterateJsonObjects(text: string): Generator<string> {\n for (let i = 0; i < text.length; i++) {\n if (text[i] !== \"{\") continue;\n let depth = 0;\n let inString = false;\n let escaped = false;\n for (let j = i; j < text.length; j++) {\n const c = text[j]!;\n if (escaped) {\n escaped = false;\n continue;\n }\n if (inString) {\n if (c === \"\\\\\") {\n escaped = true;\n continue;\n }\n if (c === '\"') inString = false;\n continue;\n }\n if (c === '\"') inString = true;\n else if (c === \"{\") depth++;\n else if (c === \"}\") {\n depth--;\n if (depth === 0) {\n yield text.slice(i, j + 1);\n i = j;\n break;\n }\n }\n }\n }\n}\n\nfunction coerceToToolCall(\n candidateJson: string,\n allowedNames: ReadonlySet<string>,\n): ToolCall | null {\n let parsed: any;\n try {\n parsed = JSON.parse(candidateJson);\n } catch {\n return null;\n }\n if (!parsed || typeof parsed !== \"object\") return null;\n\n // Pattern 1: { name, arguments }\n if (typeof parsed.name === \"string\" && allowedNames.has(parsed.name)) {\n const args = parsed.arguments;\n return {\n function: {\n name: parsed.name,\n arguments: typeof args === \"string\" ? args : JSON.stringify(args ?? {}),\n },\n };\n }\n\n // Pattern 2: OpenAI-style { type: \"function\", function: { name, arguments } }\n if (\n parsed.type === \"function\" &&\n parsed.function &&\n typeof parsed.function.name === \"string\" &&\n allowedNames.has(parsed.function.name)\n ) {\n const args = parsed.function.arguments;\n return {\n type: \"function\",\n function: {\n name: parsed.function.name,\n arguments: typeof args === \"string\" ? args : JSON.stringify(args ?? {}),\n },\n };\n }\n\n // Pattern 3: { tool_name, tool_args } (R1 free-form variant)\n if (typeof parsed.tool_name === \"string\" && allowedNames.has(parsed.tool_name)) {\n return {\n function: {\n name: parsed.tool_name,\n arguments: JSON.stringify(parsed.tool_args ?? {}),\n },\n };\n }\n\n return null;\n}\n","import type { ToolCall } from \"../types.js\";\n\n/**\n * Call-storm breaker.\n *\n * Detects (tool, args) tuples repeating within a sliding window and suppresses\n * the offending call. Surfaces a synthetic tool_result advising the model to\n * change strategy on its next turn.\n */\nexport class StormBreaker {\n private readonly windowSize: number;\n private readonly threshold: number;\n private readonly recent: Array<readonly [string, string]> = [];\n\n constructor(windowSize = 6, threshold = 3) {\n this.windowSize = windowSize;\n this.threshold = threshold;\n }\n\n inspect(call: ToolCall): { suppress: boolean; reason?: string } {\n const sig = signature(call);\n if (!sig) return { suppress: false };\n const count = this.recent.reduce(\n (n, [name, args]) => (name === sig[0] && args === sig[1] ? n + 1 : n),\n 0,\n );\n if (count >= this.threshold - 1) {\n return {\n suppress: true,\n reason: `call-storm suppressed: ${sig[0]} called with identical args ${count + 1} times within window=${this.windowSize}`,\n };\n }\n this.recent.push(sig);\n while (this.recent.length > this.windowSize) this.recent.shift();\n return { suppress: false };\n }\n\n reset(): void {\n this.recent.length = 0;\n }\n}\n\nfunction signature(call: ToolCall): readonly [string, string] | null {\n const name = call.function?.name;\n if (!name) return null;\n return [name, call.function?.arguments ?? \"\"] as const;\n}\n","/**\n * Truncation recovery for tool-call argument JSON cut off mid-structure\n * (typically when the model hits max_tokens before finishing the JSON object).\n *\n * Strategy is purely local: balance braces, close strings, fill missing values\n * with `null`. We deliberately do NOT make a continuation API call here — that\n * decision belongs to the loop, which knows about budgets.\n */\n\nexport interface TruncationRepairResult {\n repaired: string;\n changed: boolean;\n notes: string[];\n}\n\nexport function repairTruncatedJson(input: string): TruncationRepairResult {\n const notes: string[] = [];\n if (!input || !input.trim()) {\n return { repaired: \"{}\", changed: input !== \"{}\", notes: [\"empty input → {}\"] };\n }\n // Fast path: already parseable.\n try {\n JSON.parse(input);\n return { repaired: input, changed: false, notes: [] };\n } catch {\n /* fall through */\n }\n\n const stack: (\"{\" | \"[\" | '\"')[] = [];\n let escaped = false;\n let inString = false;\n let lastSignificant = -1;\n\n for (let i = 0; i < input.length; i++) {\n const c = input[i]!;\n if (!/\\s/.test(c)) lastSignificant = i;\n if (escaped) {\n escaped = false;\n continue;\n }\n if (inString) {\n if (c === \"\\\\\") {\n escaped = true;\n continue;\n }\n if (c === '\"') {\n inString = false;\n stack.pop();\n }\n continue;\n }\n if (c === '\"') {\n inString = true;\n stack.push('\"');\n continue;\n }\n if (c === \"{\" || c === \"[\") stack.push(c);\n else if (c === \"}\" || c === \"]\") stack.pop();\n }\n\n let s = input.slice(0, lastSignificant + 1);\n\n // Trim a trailing comma which would block re-parse.\n if (/,$/.test(s)) {\n s = s.replace(/,$/, \"\");\n notes.push(\"trimmed trailing comma\");\n }\n\n // If we ended on a key without a value: \"foo\": → \"foo\": null\n if (/\"\\s*:\\s*$/.test(s)) {\n s += \" null\";\n notes.push(\"filled dangling key with null\");\n }\n\n // If we ended inside a string, close it.\n if (inString) {\n s += '\"';\n stack.pop();\n notes.push(\"closed unterminated string\");\n }\n\n // Pop remaining open structures in reverse order.\n while (stack.length > 0) {\n const top = stack.pop();\n if (top === \"{\") s += \"}\";\n else if (top === \"[\") s += \"]\";\n else if (top === '\"') s += '\"';\n }\n\n try {\n JSON.parse(s);\n return { repaired: s, changed: true, notes };\n } catch (err) {\n notes.push(`fallback to {}: ${(err as Error).message}`);\n return { repaired: \"{}\", changed: true, notes };\n }\n}\n","/**\n * Pillar 3 — Tool-Call Repair pipeline.\n *\n * Order of passes per turn:\n * 1. scavenge — recover tool calls leaked into <think>\n * 2. truncation — close any half-emitted argument JSON\n * 3. storm breaker — drop call-storm repeats\n *\n * Schema flattening is applied during loop construction (it changes what we\n * advertise to the model), not per-turn.\n */\n\nimport type { ToolCall } from \"../types.js\";\nimport { scavengeToolCalls } from \"./scavenge.js\";\nimport { StormBreaker } from \"./storm.js\";\nimport { repairTruncatedJson } from \"./truncation.js\";\n\nexport { analyzeSchema, flattenSchema, nestArguments } from \"./flatten.js\";\nexport type { FlattenDecision } from \"./flatten.js\";\nexport { repairTruncatedJson } from \"./truncation.js\";\nexport type { TruncationRepairResult } from \"./truncation.js\";\nexport { scavengeToolCalls } from \"./scavenge.js\";\nexport type { ScavengeOptions, ScavengeResult } from \"./scavenge.js\";\nexport { StormBreaker } from \"./storm.js\";\n\nexport interface RepairReport {\n scavenged: number;\n truncationsFixed: number;\n stormsBroken: number;\n notes: string[];\n}\n\nexport interface ToolCallRepairOptions {\n allowedToolNames: ReadonlySet<string>;\n stormWindow?: number;\n stormThreshold?: number;\n maxScavenge?: number;\n}\n\nexport class ToolCallRepair {\n private readonly storm: StormBreaker;\n private readonly opts: ToolCallRepairOptions;\n\n constructor(opts: ToolCallRepairOptions) {\n this.opts = opts;\n this.storm = new StormBreaker(opts.stormWindow ?? 6, opts.stormThreshold ?? 3);\n }\n\n /**\n * Drop the StormBreaker's sliding window of recent (name, args)\n * signatures. Called at the start of every user turn — a fresh user\n * message is a new intent, so carrying old repetition state into it\n * would turn a valid \"try again with different input\" flow into a\n * false-positive block.\n */\n resetStorm(): void {\n this.storm.reset();\n }\n\n process(\n declaredCalls: ToolCall[],\n reasoningContent: string | null,\n content: string | null = null,\n ): { calls: ToolCall[]; report: RepairReport } {\n const report: RepairReport = {\n scavenged: 0,\n truncationsFixed: 0,\n stormsBroken: 0,\n notes: [],\n };\n\n // 1. Scavenge — only add calls whose (name,args) signature is novel.\n // Scan both channels: reasoning (where R1 leaks JSON calls into\n // <think>) AND content (where it emits DSML markup in regular\n // turns). Joined with a newline so the scanners see the blobs as\n // independent bodies. Dedup below keeps us from inflating if the\n // same call shows up in both — first seen wins.\n const combined = [reasoningContent ?? \"\", content ?? \"\"].filter(Boolean).join(\"\\n\");\n const scavenged = scavengeToolCalls(combined || null, {\n allowedNames: this.opts.allowedToolNames,\n maxCalls: this.opts.maxScavenge ?? 4,\n });\n const seenSignatures = new Set(declaredCalls.map(signature));\n const merged = [...declaredCalls];\n for (const sc of scavenged.calls) {\n if (!seenSignatures.has(signature(sc))) {\n merged.push(sc);\n report.scavenged++;\n seenSignatures.add(signature(sc));\n }\n }\n report.notes.push(...scavenged.notes);\n\n // 2. Truncation repair on argument JSON.\n for (const call of merged) {\n const args = call.function?.arguments ?? \"\";\n const r = repairTruncatedJson(args);\n if (r.changed) {\n call.function.arguments = r.repaired;\n report.truncationsFixed++;\n report.notes.push(...r.notes.map((n) => `[${call.function.name}] ${n}`));\n }\n }\n\n // 3. Storm breaker.\n const filtered: ToolCall[] = [];\n for (const call of merged) {\n const verdict = this.storm.inspect(call);\n if (verdict.suppress) {\n report.stormsBroken++;\n if (verdict.reason) report.notes.push(verdict.reason);\n continue;\n }\n filtered.push(call);\n }\n\n return { calls: filtered, report };\n }\n}\n\nfunction signature(call: ToolCall): string {\n return `${call.function?.name ?? \"\"}::${call.function?.arguments ?? \"\"}`;\n}\n","/**\n * Session persistence.\n *\n * Every turn's log entries (user / assistant / tool messages) are appended to\n * a JSONL file under `~/.reasonix/sessions/<name>.jsonl`. Next time the user\n * starts the CLI with the same session name, the loop pre-loads the file\n * into its AppendOnlyLog so the new turn has full prior context.\n *\n * Design notes:\n * - JSONL rather than JSON so concurrent writes don't corrupt.\n * - 0600 permissions on Unix (chmod no-ops on Windows).\n * - Name sanitization keeps paths safe: only [\\w-] and CJK letters pass;\n * anything else is replaced with underscore, max 64 chars.\n * - The loop's stats/session aren't persisted — only the message log.\n * Cost accounting resets each run (by design — old costs are sunk).\n */\n\nimport {\n appendFileSync,\n chmodSync,\n existsSync,\n mkdirSync,\n readFileSync,\n readdirSync,\n statSync,\n unlinkSync,\n writeFileSync,\n} from \"node:fs\";\nimport { homedir } from \"node:os\";\nimport { dirname, join } from \"node:path\";\nimport type { ChatMessage } from \"./types.js\";\n\nexport interface SessionInfo {\n name: string;\n path: string;\n size: number;\n messageCount: number;\n mtime: Date;\n}\n\nexport function sessionsDir(): string {\n return join(homedir(), \".reasonix\", \"sessions\");\n}\n\nexport function sessionPath(name: string): string {\n return join(sessionsDir(), `${sanitizeName(name)}.jsonl`);\n}\n\nexport function sanitizeName(name: string): string {\n const cleaned = name.replace(/[^\\w\\-\\u4e00-\\u9fa5]/g, \"_\").slice(0, 64);\n return cleaned || \"default\";\n}\n\nexport function loadSessionMessages(name: string): ChatMessage[] {\n const path = sessionPath(name);\n if (!existsSync(path)) return [];\n try {\n const raw = readFileSync(path, \"utf8\");\n const out: ChatMessage[] = [];\n for (const line of raw.split(/\\r?\\n/)) {\n const trimmed = line.trim();\n if (!trimmed) continue;\n try {\n const msg = JSON.parse(trimmed) as ChatMessage;\n if (msg && typeof msg === \"object\" && \"role\" in msg) out.push(msg);\n } catch {\n /* skip malformed line */\n }\n }\n return out;\n } catch {\n return [];\n }\n}\n\nexport function appendSessionMessage(name: string, message: ChatMessage): void {\n const path = sessionPath(name);\n mkdirSync(dirname(path), { recursive: true });\n appendFileSync(path, `${JSON.stringify(message)}\\n`, \"utf8\");\n try {\n chmodSync(path, 0o600);\n } catch {\n /* chmod not supported on this platform */\n }\n}\n\nexport function listSessions(): SessionInfo[] {\n const dir = sessionsDir();\n if (!existsSync(dir)) return [];\n try {\n const files = readdirSync(dir).filter((f) => f.endsWith(\".jsonl\"));\n return files\n .map((file) => {\n const path = join(dir, file);\n const stat = statSync(path);\n const name = file.replace(/\\.jsonl$/, \"\");\n const messageCount = countLines(path);\n return { name, path, size: stat.size, messageCount, mtime: stat.mtime };\n })\n .sort((a, b) => b.mtime.getTime() - a.mtime.getTime());\n } catch {\n return [];\n }\n}\n\nexport function deleteSession(name: string): boolean {\n const path = sessionPath(name);\n try {\n unlinkSync(path);\n return true;\n } catch {\n return false;\n }\n}\n\n/**\n * Overwrite the session file with a fresh message list. Used by\n * `/compact` so the compacted in-memory log persists across restarts\n * instead of being re-healed from a huge on-disk file every launch.\n * We accept the brief non-atomic window between truncate and write —\n * worst case: a concurrent crash loses the session, which is what\n * `/forget` would have done anyway.\n */\nexport function rewriteSession(name: string, messages: ChatMessage[]): void {\n const path = sessionPath(name);\n mkdirSync(dirname(path), { recursive: true });\n const body = messages.map((m) => JSON.stringify(m)).join(\"\\n\");\n writeFileSync(path, body ? `${body}\\n` : \"\", \"utf8\");\n try {\n chmodSync(path, 0o600);\n } catch {\n /* chmod not supported */\n }\n}\n\nfunction countLines(path: string): number {\n try {\n const raw = readFileSync(path, \"utf8\");\n return raw.split(/\\r?\\n/).filter((l) => l.trim()).length;\n } catch {\n return 0;\n }\n}\n","import type { Usage } from \"./client.js\";\n\n/** USD per 1M tokens. Update as DeepSeek pricing changes. */\nexport const DEEPSEEK_PRICING: Record<\n string,\n { inputCacheHit: number; inputCacheMiss: number; output: number }\n> = {\n \"deepseek-chat\": { inputCacheHit: 0.07, inputCacheMiss: 0.27, output: 1.1 },\n \"deepseek-reasoner\": { inputCacheHit: 0.14, inputCacheMiss: 0.55, output: 2.19 },\n};\n\n/** Reference Claude Sonnet 4.6 pricing (USD per 1M tokens). */\nexport const CLAUDE_SONNET_PRICING = { input: 3.0, output: 15.0 };\n\n/**\n * Maximum prompt-side context window per DeepSeek model, in tokens.\n * Both V3 (`deepseek-chat`) and R1 (`deepseek-reasoner`) currently expose\n * a 131,072-token prompt limit per the OpenAPI spec; completion caps\n * differ but don't affect the prompt budget the StatsPanel shows.\n */\nexport const DEEPSEEK_CONTEXT_TOKENS: Record<string, number> = {\n \"deepseek-chat\": 131_072,\n \"deepseek-reasoner\": 131_072,\n};\n\n/** Fallback when the caller's model id isn't in the table — safe lower bound. */\nexport const DEFAULT_CONTEXT_TOKENS = 131_072;\n\nexport function costUsd(model: string, usage: Usage): number {\n const p = DEEPSEEK_PRICING[model];\n if (!p) return 0;\n return (\n (usage.promptCacheHitTokens * p.inputCacheHit +\n usage.promptCacheMissTokens * p.inputCacheMiss +\n usage.completionTokens * p.output) /\n 1_000_000\n );\n}\n\n/** Input-side cost only (prompt, cache hit + miss). Used for the panel breakdown. */\nexport function inputCostUsd(model: string, usage: Usage): number {\n const p = DEEPSEEK_PRICING[model];\n if (!p) return 0;\n return (\n (usage.promptCacheHitTokens * p.inputCacheHit +\n usage.promptCacheMissTokens * p.inputCacheMiss) /\n 1_000_000\n );\n}\n\n/** Output-side cost only (completion tokens). Used for the panel breakdown. */\nexport function outputCostUsd(model: string, usage: Usage): number {\n const p = DEEPSEEK_PRICING[model];\n if (!p) return 0;\n return (usage.completionTokens * p.output) / 1_000_000;\n}\n\nexport function claudeEquivalentCost(usage: Usage): number {\n return (\n (usage.promptTokens * CLAUDE_SONNET_PRICING.input +\n usage.completionTokens * CLAUDE_SONNET_PRICING.output) /\n 1_000_000\n );\n}\n\nexport interface TurnStats {\n turn: number;\n model: string;\n usage: Usage;\n cost: number;\n cacheHitRatio: number;\n}\n\nexport interface SessionSummary {\n turns: number;\n totalCostUsd: number;\n /**\n * Input-side (prompt) cost aggregated across the session. Split\n * from totalCostUsd so the panel can render \"cost $X (in $Y · out\n * $Z)\" — users asked for visibility into where the spend lands.\n */\n totalInputCostUsd: number;\n /** Output-side (completion) cost aggregated across the session. */\n totalOutputCostUsd: number;\n /** @deprecated Claude reference; kept for benchmarks + replay compat, no longer surfaced in the TUI. */\n claudeEquivalentUsd: number;\n /** @deprecated. Same as claudeEquivalentUsd — synthetic ratio, not a real measurement. */\n savingsVsClaudePct: number;\n cacheHitRatio: number;\n /**\n * Most recent turn's prompt-token count. Used by the TUI's context\n * gauge: we can't know the next call's cost without making it, but\n * the last turn's prompt tokens is the floor (next call is last\n * prompt + user delta + any new tool outputs).\n */\n lastPromptTokens: number;\n}\n\nexport class SessionStats {\n readonly turns: TurnStats[] = [];\n\n record(turn: number, model: string, usage: Usage): TurnStats {\n const cost = costUsd(model, usage);\n const stats: TurnStats = {\n turn,\n model,\n usage,\n cost,\n cacheHitRatio: usage.cacheHitRatio,\n };\n this.turns.push(stats);\n return stats;\n }\n\n get totalCost(): number {\n return this.turns.reduce((sum, t) => sum + t.cost, 0);\n }\n\n get totalClaudeEquivalent(): number {\n return this.turns.reduce((sum, t) => sum + claudeEquivalentCost(t.usage), 0);\n }\n\n get savingsVsClaude(): number {\n const c = this.totalClaudeEquivalent;\n return c > 0 ? 1 - this.totalCost / c : 0;\n }\n\n get totalInputCost(): number {\n return this.turns.reduce((sum, t) => sum + inputCostUsd(t.model, t.usage), 0);\n }\n\n get totalOutputCost(): number {\n return this.turns.reduce((sum, t) => sum + outputCostUsd(t.model, t.usage), 0);\n }\n\n get aggregateCacheHitRatio(): number {\n let hit = 0;\n let miss = 0;\n for (const t of this.turns) {\n hit += t.usage.promptCacheHitTokens;\n miss += t.usage.promptCacheMissTokens;\n }\n const denom = hit + miss;\n return denom > 0 ? hit / denom : 0;\n }\n\n summary(): SessionSummary {\n const last = this.turns[this.turns.length - 1];\n return {\n turns: this.turns.length,\n totalCostUsd: round(this.totalCost, 6),\n totalInputCostUsd: round(this.totalInputCost, 6),\n totalOutputCostUsd: round(this.totalOutputCost, 6),\n claudeEquivalentUsd: round(this.totalClaudeEquivalent, 6),\n savingsVsClaudePct: round(this.savingsVsClaude * 100, 2),\n cacheHitRatio: round(this.aggregateCacheHitRatio, 4),\n lastPromptTokens: last?.usage.promptTokens ?? 0,\n };\n }\n}\n\nfunction round(n: number, digits: number): number {\n const f = 10 ** digits;\n return Math.round(n * f) / f;\n}\n","import { type DeepSeekClient, Usage } from \"./client.js\";\nimport {\n type BranchOptions,\n type BranchSample,\n aggregateBranchUsage,\n runBranches,\n} from \"./consistency.js\";\nimport { type HarvestOptions, type TypedPlanState, emptyPlanState, harvest } from \"./harvest.js\";\nimport {\n type HookOutcome,\n type HookPayload,\n type ResolvedHook,\n formatHookOutcomeMessage,\n runHooks,\n} from \"./hooks.js\";\nimport { DEFAULT_MAX_RESULT_CHARS, truncateForModel } from \"./mcp/registry.js\";\nimport { AppendOnlyLog, type ImmutablePrefix, VolatileScratch } from \"./memory.js\";\nimport { type RepairReport, ToolCallRepair } from \"./repair/index.js\";\nimport { appendSessionMessage, loadSessionMessages, rewriteSession } from \"./session.js\";\nimport {\n DEEPSEEK_CONTEXT_TOKENS,\n DEFAULT_CONTEXT_TOKENS,\n SessionStats,\n type TurnStats,\n} from \"./telemetry.js\";\nimport { ToolRegistry } from \"./tools.js\";\nimport type { ChatMessage, ToolCall } from \"./types.js\";\n\nexport type EventRole =\n | \"assistant_delta\"\n | \"assistant_final\"\n /**\n * Emitted as `tool_calls[].function.arguments` streams in. A tool\n * call with a large arguments payload produces no `content` or\n * `reasoning_content` bytes — this is the only signal the UI has\n * that the stream is alive during that window.\n */\n | \"tool_call_delta\"\n /**\n * Yielded immediately before a tool is dispatched. Lets the TUI put\n * up a \"▸ tool<X> running…\" spinner while the tool's Promise is\n * pending — otherwise the UI looks frozen whenever a tool call\n * takes more than a few hundred ms (a big `filesystem_edit_file`\n * is a typical trigger).\n */\n | \"tool_start\"\n | \"tool\"\n | \"done\"\n | \"error\"\n | \"warning\"\n /**\n * Transient \"what's happening right now\" indicator. Emitted during\n * silent phases — between a tool result and the next iteration's\n * first streaming byte, and right before harvest — so the TUI can\n * show a spinner with explanatory text instead of looking frozen.\n * The UI clears it on the next primary event (assistant_delta,\n * tool_start, tool, assistant_final, error).\n */\n | \"status\"\n | \"branch_start\"\n | \"branch_progress\"\n | \"branch_done\";\n\nexport interface BranchSummary {\n budget: number;\n chosenIndex: number;\n uncertainties: number[]; // per-sample uncertainty counts\n temperatures: number[];\n}\n\nexport interface BranchProgress {\n completed: number;\n total: number;\n latestIndex: number;\n latestTemperature: number;\n latestUncertainties: number;\n}\n\nexport interface LoopEvent {\n turn: number;\n role: EventRole;\n content: string;\n reasoningDelta?: string;\n toolName?: string;\n /**\n * Raw JSON-string arguments the model sent for a tool call (role === \"tool\").\n * Populated so transcripts can persist *why* a tool was called, not just\n * what it returned. Needed by `reasonix diff` to explain divergences.\n */\n toolArgs?: string;\n /** Cumulative arguments-string length for `role === \"tool_call_delta\"`. */\n toolCallArgsChars?: number;\n stats?: TurnStats;\n planState?: TypedPlanState;\n repair?: RepairReport;\n branch?: BranchSummary;\n branchProgress?: BranchProgress;\n error?: string;\n /**\n * True on `assistant_final` events emitted by the no-tools fallback\n * when the loop hit its budget, was aborted, or tripped the\n * token-context guard. Consumers that act on assistant text (notably\n * the code-mode edit applier) MUST treat these as display-only —\n * the model is \"wrapping up,\" not proposing new work. Applying\n * SEARCH/REPLACE blocks found in a forced summary caused the\n * \"analysis became edits\" bug in v0.4.1 and earlier.\n */\n forcedSummary?: boolean;\n}\n\nexport interface CacheFirstLoopOptions {\n client: DeepSeekClient;\n prefix: ImmutablePrefix;\n tools?: ToolRegistry;\n model?: string;\n maxToolIters?: number;\n stream?: boolean;\n /**\n * Pillar 2 — structured harvesting of R1 reasoning into a typed plan state.\n * Pass `true` for defaults or an options object. Off by default (adds a\n * cheap but non-zero V3 call per turn).\n */\n harvest?: boolean | HarvestOptions;\n /**\n * Self-consistency branching. Pass a number for just a budget (e.g. 3) or\n * a full `BranchOptions` object. Disables streaming for the branched turn\n * because all samples must complete before selection. Auto-enables harvest\n * since the default selector scores samples by plan-state uncertainty.\n */\n branch?: number | BranchOptions;\n /**\n * Session name. When set, the loop pre-loads the session's prior messages\n * into its log on construction, and appends every new log entry to\n * `~/.reasonix/sessions/<name>.jsonl` so the next run can resume.\n */\n session?: string;\n /**\n * Resolved hook list — loaded from `<project>/.reasonix/settings.json`\n * + `~/.reasonix/settings.json` by the CLI before constructing the loop.\n * The loop dispatches `PreToolUse` and `PostToolUse` events itself; the\n * CLI handles `UserPromptSubmit` and `Stop` since they live at the App\n * boundary. Empty / unset → no hooks fire (the runtime cost of an empty\n * filter is one ms). See `src/hooks.ts` for the full contract.\n */\n hooks?: ResolvedHook[];\n /**\n * `cwd` reported to hooks via the stdin payload. Defaults to `process.cwd()`.\n * `reasonix code` overrides this to the sandbox root so a hook that does\n * `cd $REASONIX_CWD` lands in the project, not in the user's shell home.\n */\n hookCwd?: string;\n}\n\n/**\n * Pillar 1 — Cache-First Loop.\n *\n * - prefix is immutable (cache target)\n * - log is append-only (preserves prior-turn prefix)\n * - scratch is per-turn volatile (never sent upstream)\n *\n * Yields a stream of events so a TUI can render progressively.\n */\nexport interface ReconfigurableOptions {\n model?: string;\n harvest?: boolean | HarvestOptions;\n branch?: number | BranchOptions;\n stream?: boolean;\n}\n\nexport class CacheFirstLoop {\n readonly client: DeepSeekClient;\n readonly prefix: ImmutablePrefix;\n readonly tools: ToolRegistry;\n readonly maxToolIters: number;\n readonly log = new AppendOnlyLog();\n readonly scratch = new VolatileScratch();\n readonly stats = new SessionStats();\n readonly repair: ToolCallRepair;\n\n // Mutable via configure() — slash commands in the TUI / library callers tweak\n // these mid-session so users don't have to restart to try harvest or branch.\n model: string;\n stream: boolean;\n harvestEnabled: boolean;\n harvestOptions: HarvestOptions;\n branchEnabled: boolean;\n branchOptions: BranchOptions;\n sessionName: string | null;\n\n /**\n * Hook list, mutable so `/hooks reload` can swap it without\n * reconstructing the loop. Default empty — the filter cost on a\n * tool call is one array length check.\n */\n hooks: ResolvedHook[];\n /** `cwd` reported to hook stdin. Resolved once at construction. */\n readonly hookCwd: string;\n\n /** Number of messages that were pre-loaded from the session file. */\n readonly resumedMessageCount: number;\n\n private _turn = 0;\n private _streamPreference: boolean;\n /**\n * AbortController per active turn. Threaded through the DeepSeek\n * HTTP calls AND every tool dispatch so Esc actually cancels the\n * in-flight network/subprocess work — not \"we'll get to it after\n * the current call finishes.\" Re-created at the start of each\n * `step()` (the prior turn's signal has already fired).\n */\n private _turnAbort: AbortController = new AbortController();\n\n constructor(opts: CacheFirstLoopOptions) {\n this.client = opts.client;\n this.prefix = opts.prefix;\n this.tools = opts.tools ?? new ToolRegistry();\n this.model = opts.model ?? \"deepseek-chat\";\n // Iter cap is a safety net, not the primary stop condition. The\n // primary stop is the token-context guard inside step(): after\n // every model response we check whether the prompt is already past\n // 80% of the model's context window, and if so divert to the\n // forced-summary path. 64 is high enough that exploration almost\n // never exhausts it before the token guard fires first — which\n // is the point: let the real constraint (context window) drive\n // the decision, keep the iter cap as a last-resort backstop for\n // the case where something spins without growing the prompt.\n this.maxToolIters = opts.maxToolIters ?? 64;\n this.hooks = opts.hooks ?? [];\n this.hookCwd = opts.hookCwd ?? process.cwd();\n\n // Resolve branch config first (since it forces harvest on).\n if (typeof opts.branch === \"number\") {\n this.branchOptions = { budget: opts.branch };\n } else if (opts.branch && typeof opts.branch === \"object\") {\n this.branchOptions = opts.branch;\n } else {\n this.branchOptions = {};\n }\n this.branchEnabled = (this.branchOptions.budget ?? 1) > 1;\n\n // Branching requires harvest for its default selector to work.\n const harvestForced = this.branchEnabled;\n this.harvestEnabled =\n harvestForced ||\n opts.harvest === true ||\n (typeof opts.harvest === \"object\" && opts.harvest !== null);\n this.harvestOptions =\n typeof opts.harvest === \"object\" && opts.harvest !== null\n ? opts.harvest\n : (this.branchOptions.harvestOptions ?? {});\n\n // Streaming is incompatible with branching (need all samples to select).\n this._streamPreference = opts.stream ?? true;\n this.stream = this.branchEnabled ? false : this._streamPreference;\n\n const allowedNames = new Set([...this.prefix.toolSpecs.map((s) => s.function.name)]);\n this.repair = new ToolCallRepair({ allowedToolNames: allowedNames });\n\n // Session resume: pre-load prior messages into the log if a session name\n // is provided. New messages appended to the log are also persisted.\n //\n // Heal-on-load: if a previous run (or a pre-alpha.6 client) stored a\n // tool result bigger than the cap, the next API call would blow past\n // DeepSeek's 131k-token limit *before the user even types anything*.\n // Truncating here lets the user pick up their session history without\n // losing the conversational context around the oversized call.\n this.sessionName = opts.session ?? null;\n if (this.sessionName) {\n const prior = loadSessionMessages(this.sessionName);\n const { messages, healedCount, healedFrom } = healLoadedMessages(\n prior,\n DEFAULT_MAX_RESULT_CHARS,\n );\n for (const msg of messages) this.log.append(msg);\n this.resumedMessageCount = messages.length;\n if (healedCount > 0) {\n // Persist the healed log back to disk so the damage doesn't\n // re-surface on the next session load — otherwise `heal →\n // append → resume → heal → …` would keep noticing the same\n // broken tail every restart. Non-fatal on I/O error: the\n // in-memory log is already healed so this session still works.\n try {\n rewriteSession(this.sessionName, messages);\n } catch {\n /* disk full / perms — skip, in-memory heal still applies */\n }\n process.stderr.write(\n `▸ session \"${this.sessionName}\": healed ${healedCount} entr${healedCount === 1 ? \"y\" : \"ies\"}${healedFrom > 0 ? ` (was ${healedFrom.toLocaleString()} chars oversized)` : \" (dropped dangling tool_calls tail)\"}. Rewrote session file.\\n`,\n );\n }\n } else {\n this.resumedMessageCount = 0;\n }\n }\n\n /**\n * Shrink the log by re-truncating oversized tool results to a tighter\n * cap, and persist the result back to disk so the next launch doesn't\n * re-inherit a fat session file. Returns a summary the TUI can\n * display.\n *\n * Only tool-role messages are touched (same rationale as\n * {@link healLoadedMessages}). User and assistant messages carry\n * authored intent we can't mechanically shrink without losing\n * meaning.\n */\n compact(tightCapChars = 4000): { healedCount: number; charsSaved: number } {\n const before = this.log.toMessages();\n // Use `shrinkOversizedToolResults` (not `healLoadedMessages`) — the\n // full heal would also strip a dangling `assistant.tool_calls` tail,\n // which during an active turn is legitimate state we still need\n // (tools haven't been dispatched yet). Structural healing is only\n // appropriate at session LOAD; mid-session `/compact` is strictly\n // about shrinking oversized tool payloads.\n const { messages, healedCount, healedFrom } = shrinkOversizedToolResults(before, tightCapChars);\n const afterBytes = messages\n .filter((m) => m.role === \"tool\")\n .reduce((s, m) => s + (typeof m.content === \"string\" ? m.content.length : 0), 0);\n const charsSaved = healedFrom - afterBytes;\n if (healedCount > 0) {\n this.log.compactInPlace(messages);\n if (this.sessionName) {\n try {\n rewriteSession(this.sessionName, messages);\n } catch {\n /* disk full or perms — compaction still applies in-memory */\n }\n }\n }\n return { healedCount, charsSaved };\n }\n\n private appendAndPersist(message: ChatMessage): void {\n this.log.append(message);\n if (this.sessionName) {\n try {\n appendSessionMessage(this.sessionName, message);\n } catch {\n /* disk full or permission denied shouldn't kill the chat */\n }\n }\n }\n\n /**\n * Start a fresh conversation WITHOUT exiting. Drops every message\n * in the in-memory log AND rewrites the session file to empty so\n * a resume won't re-hydrate the old turns. Unlike `/forget`, which\n * deletes the session entirely, this keeps the session name and\n * config intact — it's the \"new chat\" button.\n *\n * The immutable prefix (system prompt + tool specs) is preserved\n * — that's the cache-first invariant, not part of the conversation.\n * Returns the number of messages dropped so the UI can show it.\n */\n clearLog(): { dropped: number } {\n const dropped = this.log.length;\n this.log.compactInPlace([]);\n if (this.sessionName) {\n try {\n rewriteSession(this.sessionName, []);\n } catch {\n /* disk issue shouldn't block the in-memory clear */\n }\n }\n this.scratch.reset();\n return { dropped };\n }\n\n /**\n * Reconfigure model/harvest/branch/stream mid-session. The loop's log,\n * scratch, and stats are preserved — only the per-turn behavior changes.\n * Used by the TUI's slash commands and by library callers who want to\n * flip a knob between turns.\n */\n configure(opts: ReconfigurableOptions): void {\n if (opts.model !== undefined) this.model = opts.model;\n if (opts.stream !== undefined) this._streamPreference = opts.stream;\n\n if (opts.branch !== undefined) {\n if (typeof opts.branch === \"number\") {\n this.branchOptions = { budget: opts.branch };\n } else if (opts.branch && typeof opts.branch === \"object\") {\n this.branchOptions = opts.branch;\n } else {\n this.branchOptions = {};\n }\n this.branchEnabled = (this.branchOptions.budget ?? 1) > 1;\n }\n\n if (opts.harvest !== undefined) {\n const want =\n opts.harvest === true || (typeof opts.harvest === \"object\" && opts.harvest !== null);\n this.harvestEnabled = want || this.branchEnabled;\n if (typeof opts.harvest === \"object\" && opts.harvest !== null) {\n this.harvestOptions = opts.harvest;\n }\n } else if (this.branchEnabled) {\n // branch turned on without explicit harvest → force it on\n this.harvestEnabled = true;\n }\n\n // Branching always forces non-streaming; otherwise honor preference.\n this.stream = this.branchEnabled ? false : this._streamPreference;\n }\n\n private buildMessages(pendingUser: string | null): ChatMessage[] {\n // Full tool_calls ↔ tool pairing validation. DeepSeek 400s on\n // both sides of this contract — unpaired assistant.tool_calls\n // (\"insufficient tool messages following\") OR stray tool entries\n // (\"tool must be a response to a preceding tool_calls\"). A corrupted\n // session from an earlier build can have either. Rather than\n // applying a bunch of narrow tail-trim heuristics, rebuild the\n // message stream through the same validator used at load time so\n // the payload we hand to the API is well-formed by construction.\n const healed = healLoadedMessages(this.log.toMessages(), DEFAULT_MAX_RESULT_CHARS);\n const msgs: ChatMessage[] = [...this.prefix.toMessages(), ...healed.messages];\n if (pendingUser !== null) msgs.push({ role: \"user\", content: pendingUser });\n return msgs;\n }\n\n /**\n * Signal the currently-running {@link step} to stop **now**. Cancels\n * the in-flight network request (DeepSeek HTTP/SSE) AND any tool call\n * currently dispatching (MCP `notifications/cancelled` + promise\n * reject). The loop itself also sees `signal.aborted` at each\n * iteration boundary and exits quickly instead of looping again.\n * Called by the TUI on Esc.\n */\n abort(): void {\n this._turnAbort.abort();\n }\n\n /**\n * Drop everything in the log after (and including) the most recent\n * user message. Used by `/retry` so the caller can re-send that\n * message with a fresh turn instead of layering another response on\n * top of the prior exchange. Returns the content of the dropped user\n * message, or `null` if there isn't one yet.\n *\n * Persists by rewriting the session file — otherwise the next\n * launch would rehydrate the old exchange and `/retry` would seem\n * to have done nothing.\n */\n retryLastUser(): string | null {\n const entries = this.log.entries;\n let lastUserIdx = -1;\n for (let i = entries.length - 1; i >= 0; i--) {\n if (entries[i]!.role === \"user\") {\n lastUserIdx = i;\n break;\n }\n }\n if (lastUserIdx < 0) return null;\n const raw = entries[lastUserIdx]!.content;\n const userText = typeof raw === \"string\" ? raw : \"\";\n // Keep everything strictly before the user message. The caller\n // will submit the text again through the normal path, which\n // re-appends the user turn on first successful API response.\n const preserved = entries.slice(0, lastUserIdx).map((m) => ({ ...m }));\n this.log.compactInPlace(preserved);\n if (this.sessionName) {\n try {\n rewriteSession(this.sessionName, preserved);\n } catch {\n /* disk-full / perms — in-memory compaction still applies */\n }\n }\n return userText;\n }\n\n async *step(userInput: string): AsyncGenerator<LoopEvent> {\n this._turn++;\n this.scratch.reset();\n // A fresh user turn is a new intent — don't let StormBreaker's\n // old sliding window of (name, args) signatures keep blocking\n // calls that are now legitimately on-task. The window repopulates\n // naturally as this turn's tool calls flow through.\n this.repair.resetStorm();\n // Fresh controller for this turn: the prior step's signal has\n // already fired (or stayed clean); either way we don't want its\n // state to bleed into the new turn.\n this._turnAbort = new AbortController();\n const signal = this._turnAbort.signal;\n let pendingUser: string | null = userInput;\n const toolSpecs = this.prefix.tools();\n // 70% of the iter budget is the \"you're getting close\" threshold. We\n // only warn once per step so the user sees a single signal, not a\n // string of identical yellow lines stacked up.\n const warnAt = Math.max(1, Math.floor(this.maxToolIters * 0.7));\n let warnedForIterBudget = false;\n\n for (let iter = 0; iter < this.maxToolIters; iter++) {\n if (signal.aborted) {\n // Esc means \"stop now\" — not \"stop and force another 30-90s\n // reasoner call to produce a summary I didn't ask for\". The\n // user's mental model of cancel is immediate. We emit a\n // synthetic assistant_final (tagged forcedSummary so the\n // code-mode applier ignores it) with a short stopped\n // message, then done. The prior tool outputs are still in\n // the log if the user wants to continue — asking again\n // will hit a warm cache and be cheap.\n //\n // Budget / context-guard still call forceSummaryAfterIterLimit\n // because there the USER didn't choose to stop — we did —\n // and leaving them staring at nothing is worse than one extra\n // call.\n yield {\n turn: this._turn,\n role: \"warning\",\n content: `aborted at iter ${iter}/${this.maxToolIters} — stopped without producing a summary (press ↑ + Enter or /retry to resume)`,\n };\n const stoppedMsg =\n \"[aborted by user (Esc) — no summary produced. Ask again or /retry when ready; prior tool output is still in the log.]\";\n this.appendAndPersist({ role: \"assistant\", content: stoppedMsg });\n yield {\n turn: this._turn,\n role: \"assistant_final\",\n content: stoppedMsg,\n forcedSummary: true,\n };\n yield { turn: this._turn, role: \"done\", content: stoppedMsg };\n return;\n }\n // Bridge the silence between the PREVIOUS iter's tool result and\n // THIS iter's first streaming byte. R1 can spend 20-90s reasoning\n // about tool output before the first delta lands, and prior to\n // this hint the UI had nothing to render. Only emit on iter > 0\n // because iter 0's \"thinking\" phase is already covered by the\n // streaming row / StreamingAssistant's placeholder.\n //\n // Wording is explicit about the two things happening: the tool\n // result IS being uploaded (it's now part of the next prompt) and\n // the model IS thinking. Users were reading \"thinking about the\n // tool result\" as the model-only phase, but the wait also covers\n // the upload round-trip.\n if (iter > 0) {\n yield {\n turn: this._turn,\n role: \"status\",\n content: \"tool result uploaded · model thinking before next response…\",\n };\n }\n if (!warnedForIterBudget && iter >= warnAt) {\n warnedForIterBudget = true;\n yield {\n turn: this._turn,\n role: \"warning\",\n content: `${iter}/${this.maxToolIters} tool calls used — approaching budget. Press Esc to force a summary now.`,\n };\n }\n const messages = this.buildMessages(pendingUser);\n\n let assistantContent = \"\";\n let reasoningContent = \"\";\n let toolCalls: ToolCall[] = [];\n let usage: TurnStats[\"usage\"] | null = null;\n\n let branchSummary: BranchSummary | undefined;\n let preHarvestedPlanState: TypedPlanState | undefined;\n\n try {\n if (this.branchEnabled) {\n const budget = this.branchOptions.budget ?? 1;\n yield {\n turn: this._turn,\n role: \"branch_start\",\n content: \"\",\n branchProgress: {\n completed: 0,\n total: budget,\n latestIndex: -1,\n latestTemperature: -1,\n latestUncertainties: -1,\n },\n };\n\n // Queue samples as they complete so we can yield progress events\n // in resolution order (not launch order).\n const queue: BranchSample[] = [];\n let waiter: ((s: BranchSample) => void) | null = null;\n\n const onSampleDone = (sample: BranchSample) => {\n if (waiter) {\n const w = waiter;\n waiter = null;\n w(sample);\n } else {\n queue.push(sample);\n }\n };\n\n const branchPromise = runBranches(\n this.client,\n {\n model: this.model,\n messages,\n tools: toolSpecs.length ? toolSpecs : undefined,\n signal,\n },\n {\n ...this.branchOptions,\n harvestOptions: this.harvestOptions,\n onSampleDone,\n },\n );\n\n for (let k = 0; k < budget; k++) {\n const sample: BranchSample =\n queue.shift() ??\n (await new Promise<BranchSample>((resolve) => {\n waiter = resolve;\n }));\n yield {\n turn: this._turn,\n role: \"branch_progress\",\n content: \"\",\n branchProgress: {\n completed: k + 1,\n total: budget,\n latestIndex: sample.index,\n latestTemperature: sample.temperature,\n latestUncertainties: sample.planState.uncertainties.length,\n },\n };\n }\n\n const result = await branchPromise;\n assistantContent = result.chosen.response.content;\n reasoningContent = result.chosen.response.reasoningContent ?? \"\";\n toolCalls = result.chosen.response.toolCalls;\n\n // Cost accounting: sum usage across ALL samples, not just the winner.\n // (We paid for all three.) Harvest-call tokens are not tracked; they\n // amount to rounding error compared to the main R1 calls.\n const agg = aggregateBranchUsage(result.samples);\n usage = new Usage(\n agg.promptTokens,\n agg.completionTokens,\n agg.totalTokens,\n agg.promptCacheHitTokens,\n agg.promptCacheMissTokens,\n );\n preHarvestedPlanState = result.chosen.planState;\n branchSummary = summarizeBranch(result.chosen, result.samples);\n yield {\n turn: this._turn,\n role: \"branch_done\",\n content: \"\",\n branch: branchSummary,\n };\n } else if (this.stream) {\n const callBuf: Map<number, ToolCall> = new Map();\n for await (const chunk of this.client.stream({\n model: this.model,\n messages,\n tools: toolSpecs.length ? toolSpecs : undefined,\n signal,\n })) {\n if (chunk.contentDelta) {\n assistantContent += chunk.contentDelta;\n yield {\n turn: this._turn,\n role: \"assistant_delta\",\n content: chunk.contentDelta,\n };\n }\n if (chunk.reasoningDelta) {\n reasoningContent += chunk.reasoningDelta;\n yield {\n turn: this._turn,\n role: \"assistant_delta\",\n content: \"\",\n reasoningDelta: chunk.reasoningDelta,\n };\n }\n if (chunk.toolCallDelta) {\n const d = chunk.toolCallDelta;\n const cur = callBuf.get(d.index) ?? {\n id: d.id,\n type: \"function\" as const,\n function: { name: \"\", arguments: \"\" },\n };\n if (d.id) cur.id = d.id;\n if (d.name) cur.function.name = (cur.function.name ?? \"\") + d.name;\n if (d.argumentsDelta)\n cur.function.arguments = (cur.function.arguments ?? \"\") + d.argumentsDelta;\n callBuf.set(d.index, cur);\n // Skip the id-only opener: name is empty until the next chunk.\n if (cur.function.name) {\n yield {\n turn: this._turn,\n role: \"tool_call_delta\",\n content: \"\",\n toolName: cur.function.name,\n toolCallArgsChars: (cur.function.arguments ?? \"\").length,\n };\n }\n }\n if (chunk.usage) usage = chunk.usage;\n }\n toolCalls = [...callBuf.values()];\n } else {\n const resp = await this.client.chat({\n model: this.model,\n messages,\n tools: toolSpecs.length ? toolSpecs : undefined,\n signal,\n });\n assistantContent = resp.content;\n reasoningContent = resp.reasoningContent ?? \"\";\n toolCalls = resp.toolCalls;\n usage = resp.usage;\n }\n } catch (err) {\n // An aborted signal here is almost always our own doing —\n // either Esc, or App.tsx calling `loop.abort()` to switch to a\n // queued synthetic input (ShellConfirm \"always allow\", PlanConfirm\n // approve, etc.). The DeepSeek client's fetch path translates\n // the abort into a generic `AbortError(\"This operation was\n // aborted\")`, which used to bubble up here and render as a\n // scary red \"error\" row even though nothing actually broke.\n // Treat it as a clean early-exit instead: the next turn (queued\n // synthetic OR user re-prompt) starts immediately and gets to\n // produce its own answer.\n if (signal.aborted) {\n yield { turn: this._turn, role: \"done\", content: \"\" };\n return;\n }\n yield {\n turn: this._turn,\n role: \"error\",\n content: \"\",\n error: formatLoopError(err as Error),\n };\n return;\n }\n\n const turnStats = this.stats.record(this._turn, this.model, usage ?? new Usage());\n\n // Commit the user turn to the log only on success of the first round-trip.\n if (pendingUser !== null) {\n this.appendAndPersist({ role: \"user\", content: pendingUser });\n pendingUser = null;\n }\n\n this.scratch.reasoning = reasoningContent || null;\n // Harvest is a second API round-trip (cheap model, but still\n // 1-10s) that was previously silent. Bridge the gap with a\n // status indicator so the TUI shows *something* instead of\n // \"reasoning finished, now staring at the wall.\"\n if (\n !preHarvestedPlanState &&\n this.harvestEnabled &&\n (reasoningContent?.trim().length ?? 0) >= 40\n ) {\n yield {\n turn: this._turn,\n role: \"status\",\n content: \"extracting plan state from reasoning…\",\n };\n }\n const planState = preHarvestedPlanState\n ? preHarvestedPlanState\n : this.harvestEnabled\n ? await harvest(reasoningContent || null, this.client, this.harvestOptions, signal)\n : emptyPlanState();\n\n const { calls: repairedCalls, report } = this.repair.process(\n toolCalls,\n reasoningContent || null,\n assistantContent || null,\n );\n\n this.appendAndPersist(this.assistantMessage(assistantContent, repairedCalls));\n\n yield {\n turn: this._turn,\n role: \"assistant_final\",\n content: assistantContent,\n stats: turnStats,\n planState,\n repair: report,\n branch: branchSummary,\n };\n\n // Loud signal when the storm breaker caught a repeat pattern.\n // The `repair` field on assistant_final already carries the\n // count as a subtext on the assistant row, but a dedicated\n // warning row is far more noticeable — and critical when ALL\n // calls were suppressed, because the turn then ends with no\n // visible explanation of why nothing happened.\n if (report.stormsBroken > 0) {\n const noteTail = report.notes.length ? ` — ${report.notes[report.notes.length - 1]}` : \"\";\n const allSuppressed = repairedCalls.length === 0 && toolCalls.length > 0;\n const phrase = allSuppressed\n ? `stopped the model from calling the same tool with identical args repeatedly (all ${toolCalls.length} call(s) this turn were already in the recent-repeat window). Likely a stuck retry — reword your instruction, rule out the underlying blocker, or try /retry after fixing it`\n : `suppressed ${report.stormsBroken} repeat tool call(s) that had fired 3+ times with identical args in a sliding window`;\n yield {\n turn: this._turn,\n role: \"warning\",\n content: `${phrase}${noteTail}`,\n };\n }\n\n if (repairedCalls.length === 0) {\n yield { turn: this._turn, role: \"done\", content: assistantContent };\n return;\n }\n\n // Token-budget guard — the real stop condition. Iter count is a\n // proxy that misses the actual constraint: how close the prompt\n // already is to DeepSeek's 131k context. If we're over 80%, the\n // NEXT call (with the just-executed tools' results stuffed into\n // history) will be worse, and fairly soon it'll 400 with\n // \"maximum context length\".\n //\n // Strategy, in order:\n // 1. Try auto-compacting the log (shrink oversized tool\n // results). If that gets us back under 80% we keep going —\n // the user doesn't lose their turn to a premature summary.\n // 2. If still over after compact, divert to the forced-summary\n // path. BUT first drop the trailing assistant-with-tool_calls\n // that we just appended — we haven't executed the tools yet,\n // so sending this to the summary call with no matching tool\n // responses would 400 (\"insufficient tool messages following\n // tool_calls\"). The summary is about what was LEARNED so far,\n // not what we intended to do next.\n const ctxMax = DEEPSEEK_CONTEXT_TOKENS[this.model] ?? DEFAULT_CONTEXT_TOKENS;\n if (usage && usage.promptTokens / ctxMax > 0.8) {\n const before = usage.promptTokens;\n const compactResult = this.compact(4000);\n if (compactResult.healedCount > 0) {\n // Rough estimate: 4 chars per token. Good enough to decide\n // whether compaction pushed us back under the threshold; the\n // exact number comes back from the NEXT API response's usage.\n const approxSaved = Math.round(compactResult.charsSaved / 4);\n const after = before - approxSaved;\n yield {\n turn: this._turn,\n role: \"warning\",\n content: `context ${before.toLocaleString()}/${ctxMax.toLocaleString()} — auto-compacted ${compactResult.healedCount} oversized tool result(s), saved ~${approxSaved.toLocaleString()} tokens (now ~${after.toLocaleString()}). Continuing.`,\n };\n // Intentionally don't re-check the threshold here: even if\n // compaction didn't fully clear us under 80%, one more tool\n // call's overhead isn't going to overflow, and the NEXT\n // iter's fresh `usage` from the API will catch real danger.\n } else {\n yield {\n turn: this._turn,\n role: \"warning\",\n content: `context ${before.toLocaleString()}/${ctxMax.toLocaleString()} (${Math.round(\n (before / ctxMax) * 100,\n )}%) — nothing to auto-compact. Forcing summary from what was gathered.`,\n };\n // Drop the trailing assistant-with-tool_calls we just\n // appended. The forced-summary call would otherwise trip\n // DeepSeek's \"insufficient tool messages following tool_calls\"\n // validator, since we bail BEFORE dispatching the tools.\n const tail = this.log.entries[this.log.entries.length - 1];\n if (\n tail &&\n tail.role === \"assistant\" &&\n Array.isArray(tail.tool_calls) &&\n tail.tool_calls.length > 0\n ) {\n const kept = this.log.entries.slice(0, -1);\n this.log.compactInPlace([...kept]);\n if (this.sessionName) {\n try {\n rewriteSession(this.sessionName, kept);\n } catch {\n /* disk issue shouldn't block the summary path */\n }\n }\n }\n yield* this.forceSummaryAfterIterLimit({ reason: \"context-guard\" });\n return;\n }\n }\n\n for (const call of repairedCalls) {\n const name = call.function?.name ?? \"\";\n const args = call.function?.arguments ?? \"{}\";\n // Announce the tool BEFORE awaiting it so the TUI can render a\n // \"running…\" indicator. Without this, the window between\n // assistant_final and the tool-result yield is silent from the\n // UI's perspective — which makes long tool calls feel like the\n // app has hung.\n yield {\n turn: this._turn,\n role: \"tool_start\",\n content: \"\",\n toolName: name,\n toolArgs: args,\n };\n\n // PreToolUse hooks. A `block` decision (exit 2) skips dispatch\n // and surfaces the hook's stderr as the tool result so the model\n // sees a structured refusal instead of a silent omission. Non-\n // block non-zero outcomes are warnings: the loop continues, the\n // UI gets a yellow row.\n const parsedArgs = safeParseToolArgs(args);\n const preReport = await runHooks({\n hooks: this.hooks,\n payload: {\n event: \"PreToolUse\",\n cwd: this.hookCwd,\n toolName: name,\n toolArgs: parsedArgs,\n },\n });\n for (const w of hookWarnings(preReport.outcomes, this._turn)) yield w;\n\n let result: string;\n if (preReport.blocked) {\n const blocking = preReport.outcomes[preReport.outcomes.length - 1];\n const reason = (\n blocking?.stderr ||\n blocking?.stdout ||\n \"blocked by PreToolUse hook\"\n ).trim();\n result = `[hook block] ${blocking?.hook.command ?? \"<unknown>\"}\\n${reason}`;\n } else {\n result = await this.tools.dispatch(name, args, { signal });\n\n // PostToolUse hooks — block is meaningless after the fact, so\n // every non-pass outcome is a warning. Hooks here are the\n // natural place for \"after every edit, run the formatter.\"\n const postReport = await runHooks({\n hooks: this.hooks,\n payload: {\n event: \"PostToolUse\",\n cwd: this.hookCwd,\n toolName: name,\n toolArgs: parsedArgs,\n toolResult: result,\n },\n });\n for (const w of hookWarnings(postReport.outcomes, this._turn)) yield w;\n }\n\n this.appendAndPersist({\n role: \"tool\",\n tool_call_id: call.id ?? \"\",\n name,\n content: result,\n });\n yield {\n turn: this._turn,\n role: \"tool\",\n content: result,\n toolName: name,\n toolArgs: args,\n };\n }\n }\n\n // We exhausted the tool-call budget while the model still wanted to\n // call more tools. Rather than stopping silently (which leaves the\n // user staring at a blank prompt), force one final no-tools call so\n // the model must produce a text summary from everything it has\n // already seen.\n yield* this.forceSummaryAfterIterLimit({ reason: \"budget\" });\n }\n\n private async *forceSummaryAfterIterLimit(\n opts: { reason: \"budget\" | \"aborted\" | \"context-guard\" } = { reason: \"budget\" },\n ): AsyncGenerator<LoopEvent> {\n try {\n // The summary call is non-streaming (reasoner, 30-60s typical).\n // Without this status the user sees nothing happening after the\n // yellow \"budget reached\" warning until the summary arrives.\n yield {\n turn: this._turn,\n role: \"status\",\n content: \"summarizing what was gathered…\",\n };\n const messages = this.buildMessages(null);\n // Passing `tools: undefined` was supposed to force a text\n // response, but R1 can still hallucinate tool-call markup\n // (e.g. DSML `<|DSML|function_calls>…</|DSML|function_calls>`)\n // in prose when it's been primed by prior tool use. An explicit\n // user-role instruction plus post-hoc stripping of known\n // hallucination shapes keeps the user from seeing raw markup.\n messages.push({\n role: \"user\",\n content:\n \"I'm out of tool-call budget for this turn. Summarize in plain prose what you learned from the tool results above. Do NOT emit any tool calls, function-call markup, DSML invocations, or SEARCH/REPLACE edit blocks — they will be silently discarded. Just plain text.\",\n });\n const resp = await this.client.chat({\n model: this.model,\n messages,\n // no tools → model is forced to answer in text\n signal: this._turnAbort.signal,\n });\n const rawContent = resp.content?.trim() ?? \"\";\n const cleaned = stripHallucinatedToolMarkup(rawContent);\n const summary =\n cleaned ||\n \"(model emitted fake tool-call markup instead of a prose summary — try /retry with a narrower question, or /think to inspect R1's reasoning)\";\n const reasonPrefix = reasonPrefixFor(opts.reason, this.maxToolIters);\n const annotated = `${reasonPrefix}\\n\\n${summary}`;\n const summaryStats = this.stats.record(this._turn, this.model, resp.usage ?? new Usage());\n this.appendAndPersist({ role: \"assistant\", content: summary });\n yield {\n turn: this._turn,\n role: \"assistant_final\",\n content: annotated,\n stats: summaryStats,\n forcedSummary: true,\n };\n yield { turn: this._turn, role: \"done\", content: summary };\n } catch (err) {\n const label = errorLabelFor(opts.reason, this.maxToolIters);\n yield {\n turn: this._turn,\n role: \"error\",\n content: \"\",\n error: `${label} and the fallback summary call failed: ${(err as Error).message}. Run /clear and retry with a narrower question, or raise --max-tool-iters.`,\n };\n yield { turn: this._turn, role: \"done\", content: \"\" };\n }\n }\n\n async run(userInput: string, onEvent?: (ev: LoopEvent) => void): Promise<string> {\n let final = \"\";\n for await (const ev of this.step(userInput)) {\n onEvent?.(ev);\n if (ev.role === \"assistant_final\") final = ev.content;\n if (ev.role === \"done\") break;\n }\n return final;\n }\n\n private assistantMessage(content: string, toolCalls: ToolCall[]): ChatMessage {\n const msg: ChatMessage = { role: \"assistant\", content };\n if (toolCalls.length > 0) msg.tool_calls = toolCalls;\n return msg;\n }\n}\n\n/**\n * R1 occasionally hallucinates tool-call markup as plain text when the\n * real tool channel has been closed — typically our forced-summary\n * path, where `tools: undefined` is supposed to force prose but isn't\n * always respected. The markup isn't parsed by our tool-call path\n * (the API response's structured `tool_calls` field is empty), so\n * it's just noise in the user's view. Strip known envelope shapes.\n *\n * Exported so tests can exercise it against concrete R1 outputs.\n */\nexport function stripHallucinatedToolMarkup(s: string): string {\n let out = s;\n // DeepSeek's DSML envelope (both the full-width \"|\" character and\n // the ASCII-only fallback we've seen — the full-width form is the\n // one R1 emits in practice)\n out = out.replace(/<|DSML|function_calls>[\\s\\S]*?<\\/?|DSML|function_calls>/g, \"\");\n out = out.replace(/<\\|DSML\\|function_calls>[\\s\\S]*?<\\/?\\|DSML\\|function_calls>/g, \"\");\n // Anthropic / generic XML-ish envelope\n out = out.replace(/<function_calls>[\\s\\S]*?<\\/function_calls>/g, \"\");\n // Lone unpaired DSML opener left over after the closer was on a\n // different line (seen when R1 truncates mid-call).\n out = out.replace(/<|DSML|[\\s\\S]*$/g, \"\");\n return out.trim();\n}\n\n/**\n * Try to JSON-decode the model's tool-call arguments so PreToolUse /\n * PostToolUse hooks get a structured object instead of a string.\n * Falls back to the raw string when the model emits malformed JSON\n * (the loop's own dispatch already tolerates that — keep parity).\n */\nfunction safeParseToolArgs(raw: string): unknown {\n try {\n return JSON.parse(raw);\n } catch {\n return raw;\n }\n}\n\n/** Format non-pass hook outcomes as `LoopEvent`s of role `warning`. */\nfunction* hookWarnings(outcomes: HookOutcome[], turn: number): Generator<LoopEvent> {\n for (const o of outcomes) {\n if (o.decision === \"pass\") continue;\n yield { turn, role: \"warning\", content: formatHookOutcomeMessage(o) };\n }\n}\n\nfunction reasonPrefixFor(reason: \"budget\" | \"aborted\" | \"context-guard\", iterCap: number): string {\n if (reason === \"aborted\") return \"[aborted by user (Esc) — summarizing what I found so far]\";\n if (reason === \"context-guard\") {\n return \"[context budget running low — summarizing before the next call would overflow]\";\n }\n return `[tool-call budget (${iterCap}) reached — forcing summary from what I found]`;\n}\n\nfunction errorLabelFor(reason: \"budget\" | \"aborted\" | \"context-guard\", iterCap: number): string {\n if (reason === \"aborted\") return \"aborted by user\";\n if (reason === \"context-guard\") return \"context-guard triggered (prompt > 80% of window)\";\n return `tool-call budget (${iterCap}) reached`;\n}\n\nfunction summarizeBranch(chosen: BranchSample, samples: BranchSample[]): BranchSummary {\n return {\n budget: samples.length,\n chosenIndex: chosen.index,\n uncertainties: samples.map((s) => s.planState.uncertainties.length),\n temperatures: samples.map((s) => s.temperature),\n };\n}\n\n/**\n * Truncate any tool-role message whose content exceeds the cap. User\n * and assistant messages are left alone because (a) they're almost\n * always small, (b) truncating user prompts would corrupt conversational\n * intent in a way the user didn't author. Exported for tests.\n */\n/**\n * Shrink oversized tool results only — the original compact concern.\n * Separated from `healLoadedMessages` so `/compact` (live, mid-session)\n * doesn't accidentally strip structural tail that belongs in the\n * current turn's state.\n */\nexport function shrinkOversizedToolResults(\n messages: ChatMessage[],\n maxChars: number,\n): { messages: ChatMessage[]; healedCount: number; healedFrom: number } {\n let healedCount = 0;\n let healedFrom = 0;\n const out = messages.map((msg) => {\n if (msg.role !== \"tool\") return msg;\n const content = typeof msg.content === \"string\" ? msg.content : \"\";\n if (content.length <= maxChars) return msg;\n healedCount += 1;\n healedFrom += content.length;\n return { ...msg, content: truncateForModel(content, maxChars) };\n });\n return { messages: out, healedCount, healedFrom };\n}\n\nexport function healLoadedMessages(\n messages: ChatMessage[],\n maxChars: number,\n): { messages: ChatMessage[]; healedCount: number; healedFrom: number } {\n // Pass 1: shrink oversized tool results (original heal purpose).\n const shrunk = shrinkOversizedToolResults(messages, maxChars);\n let healedCount = shrunk.healedCount;\n // Pass 2: enforce tool_calls ↔ tool pairing across the full log.\n //\n // DeepSeek rejects two shapes at the API boundary:\n // (a) assistant with tool_calls not followed by matching tool\n // responses (\"insufficient tool messages following tool_calls\")\n // (b) tool message without a preceding assistant.tool_calls with\n // the matching tool_call_id (\"must be a response to a preceding\n // message with 'tool_calls'\")\n //\n // Corrupted session files from earlier builds have hit both. Rebuild\n // the message stream so only well-formed (assistant.tool_calls + all\n // matching responses) groups survive. Plain user/assistant messages\n // (no tool_calls) always pass through.\n const out: ChatMessage[] = [];\n const openCallIds = new Set<string>();\n let droppedAssistantCalls = 0;\n let droppedStrayTools = 0;\n for (let i = 0; i < shrunk.messages.length; i++) {\n const msg = shrunk.messages[i]!;\n if (msg.role === \"assistant\" && Array.isArray(msg.tool_calls) && msg.tool_calls.length > 0) {\n // Look ahead for tool responses matching every id in this\n // assistant's tool_calls. If all present (in any order, but\n // contiguous after this message), include the whole group.\n const needed = new Set<string>();\n for (const call of msg.tool_calls) {\n if (call?.id) needed.add(call.id);\n }\n const candidates: ChatMessage[] = [];\n let j = i + 1;\n while (j < shrunk.messages.length && needed.size > 0) {\n const nxt = shrunk.messages[j]!;\n if (nxt.role !== \"tool\") break;\n const id = nxt.tool_call_id ?? \"\";\n if (!needed.has(id)) break;\n needed.delete(id);\n candidates.push(nxt);\n j++;\n }\n if (needed.size === 0) {\n // Every call has a response — emit the whole group.\n out.push(msg);\n for (const r of candidates) out.push(r);\n i = j - 1; // for-loop ++ will advance past the last response\n } else {\n // Drop the assistant entry and anything that was speculatively\n // its responses — they'd become stray tool messages.\n droppedAssistantCalls += 1;\n droppedStrayTools += candidates.length;\n i = j - 1;\n }\n continue;\n }\n if (msg.role === \"tool\") {\n // Any tool message that reaches here did NOT get consumed by\n // the assistant-with-tool_calls branch above, so it's stray.\n // Drop it — surfacing it would 400 the next API call.\n droppedStrayTools += 1;\n continue;\n }\n // Plain user/assistant/system message — pass through.\n out.push(msg);\n }\n healedCount += droppedAssistantCalls + droppedStrayTools;\n return { messages: out, healedCount, healedFrom: shrunk.healedFrom };\n}\n\n/**\n * Annotate the `DeepSeek 400: … maximum context length …` error the API\n * returns when a session's history has grown past 131,072 tokens. The\n * raw message is a JSON blob; we surface a short actionable hint on top\n * so the user knows to `/forget` or `/clear` rather than parsing the\n * JSON themselves. Other errors pass through unchanged — the loop's\n * error channel already formats them well enough.\n */\nexport function formatLoopError(err: Error): string {\n const msg = err.message ?? \"\";\n if (msg.includes(\"maximum context length\")) {\n // Pull the \"requested X tokens\" figure out of the JSON for scale.\n const reqMatch = msg.match(/requested\\s+(\\d+)\\s+tokens/);\n const requested = reqMatch\n ? `${Number(reqMatch[1]).toLocaleString()} tokens`\n : \"too many tokens\";\n return `Context overflow (DeepSeek 400): session history is ${requested}, past the 131,072-token limit. Usually this means a single tool call returned a huge payload. v0.3.0-alpha.6+ caps new tool results at 32k chars, AND auto-heals oversized history on session load — restart Reasonix and this session should come back trimmed. If it still overflows, run /forget (delete the session) or /clear (drop the displayed history) to start fresh.`;\n }\n return msg;\n}\n","/**\n * Built-in filesystem tools for `reasonix code`.\n *\n * Why native instead of the official `@modelcontextprotocol/server-filesystem`:\n * - No subprocess overhead — every call is 50-200 ms cheaper.\n * - Schema shapes tuned for R1: `edit_file` takes a single\n * SEARCH/REPLACE string instead of `string=\"false\"`-encoded\n * JSON arrays, which was the biggest single source of DSML\n * hallucinations in 0.4.x.\n * - Sandbox enforcement lives here so Reasonix can reason about\n * it (tests cover path-traversal, symlink-escape, and the\n * cwd-outside-root case) rather than trusting an external server.\n * - No `npx install` / network dependency in `reasonix code`.\n *\n * Tool names + argument shapes intentionally mirror the official\n * filesystem server so R1's muscle memory carries over. The only\n * intentional divergence is `edit_file`, noted above.\n */\n\nimport { promises as fs } from \"node:fs\";\nimport * as pathMod from \"node:path\";\nimport type { ToolRegistry } from \"../tools.js\";\n\nexport interface FilesystemToolsOptions {\n /** Absolute directory the tools may read/write. Paths outside this are refused. */\n rootDir: string;\n /**\n * When `false`, register only read-side tools (read_file, list_directory,\n * search_files, get_file_info, directory_tree). Useful for read-only\n * workflows where the model should never mutate the tree. Default: true.\n */\n allowWriting?: boolean;\n /**\n * Cap for a single file read, in bytes. Prevents a stray `read_file`\n * on a multi-GB blob from OOM'ing Node. 2 MB is enough for any realistic\n * source file (the biggest single-file TypeScript project checked in to\n * GitHub is ~500 KB); pass higher when working with data files.\n */\n maxReadBytes?: number;\n /**\n * Cap for total bytes returned from search_files / directory_tree /\n * grep, so the model can't accidentally pull down the whole tree as\n * one giant string. 256 KB by default.\n */\n maxListBytes?: number;\n}\n\nconst DEFAULT_MAX_READ_BYTES = 2 * 1024 * 1024;\nconst DEFAULT_MAX_LIST_BYTES = 256 * 1024;\n\n/**\n * Directory names skipped by `search_content` unless `include_deps:true`\n * is passed. The intent is \"user is asking about THEIR code, not the\n * libraries they depend on\" — vendored / generated trees would otherwise\n * dominate every match list. Pass include_deps when you genuinely need\n * to grep a dependency.\n */\nconst SKIP_DIR_NAMES: ReadonlySet<string> = new Set([\n \"node_modules\",\n \".git\",\n \".hg\",\n \".svn\",\n \"dist\",\n \"build\",\n \"out\",\n \".next\",\n \".nuxt\",\n \"target\", // Rust / Java\n \".venv\",\n \"venv\",\n \"__pycache__\",\n \".pytest_cache\",\n \".mypy_cache\",\n \".cache\",\n \"coverage\",\n]);\n\n/**\n * Cheap binary-by-extension check for `search_content`. We err on the\n * side of skipping so a NUL-byte content sniff is the second line of\n * defense (handles e.g. a `.txt` that's actually a binary dump).\n */\nconst BINARY_EXTENSIONS: ReadonlySet<string> = new Set([\n \".png\",\n \".jpg\",\n \".jpeg\",\n \".gif\",\n \".bmp\",\n \".ico\",\n \".webp\",\n \".tiff\",\n \".pdf\",\n \".zip\",\n \".tar\",\n \".gz\",\n \".bz2\",\n \".xz\",\n \".7z\",\n \".rar\",\n \".exe\",\n \".dll\",\n \".so\",\n \".dylib\",\n \".bin\",\n \".class\",\n \".jar\",\n \".war\",\n \".o\",\n \".obj\",\n \".lib\",\n \".a\",\n \".woff\",\n \".woff2\",\n \".ttf\",\n \".otf\",\n \".eot\",\n \".mp3\",\n \".mp4\",\n \".mov\",\n \".avi\",\n \".webm\",\n \".wasm\",\n \".pyc\",\n \".pyo\",\n]);\n\nfunction isLikelyBinaryByName(name: string): boolean {\n const dot = name.lastIndexOf(\".\");\n if (dot < 0) return false;\n return BINARY_EXTENSIONS.has(name.slice(dot).toLowerCase());\n}\n\nexport function registerFilesystemTools(\n registry: ToolRegistry,\n opts: FilesystemToolsOptions,\n): ToolRegistry {\n const rootDir = pathMod.resolve(opts.rootDir);\n const allowWriting = opts.allowWriting !== false;\n const maxReadBytes = opts.maxReadBytes ?? DEFAULT_MAX_READ_BYTES;\n const maxListBytes = opts.maxListBytes ?? DEFAULT_MAX_LIST_BYTES;\n\n /** Resolve path, enforce it's under rootDir, return absolute. */\n const safePath = (raw: unknown): string => {\n if (typeof raw !== \"string\" || raw.length === 0) {\n throw new Error(\"path must be a non-empty string\");\n }\n // Sandbox-root semantics: a leading POSIX-style `/` (or `\\` on\n // Windows) means \"from the project root\", not \"from the filesystem\n // root\". Models routinely write `path: \"/\"` or `path: \"/src/foo.ts\"`\n // intending the sandbox root — without this normalization,\n // path.resolve interprets `/` as the actual drive root (`F:\\` on\n // Windows, `/` on POSIX) and the escape check rightly rejects it,\n // confusing the model. Strip leading separators so the rest of the\n // resolution treats the input as relative to rootDir. Drive-letter\n // absolutes (`C:\\foo`) and Unix absolutes outside rootDir still\n // get caught by the relative-escape check below.\n let normalized = raw;\n while (normalized.startsWith(\"/\") || normalized.startsWith(\"\\\\\")) {\n normalized = normalized.slice(1);\n }\n if (normalized.length === 0) normalized = \".\";\n const resolved = pathMod.resolve(rootDir, normalized);\n const normRoot = pathMod.resolve(rootDir);\n // Use relative() to catch any `..` segments that escape.\n const rel = pathMod.relative(normRoot, resolved);\n if (rel.startsWith(\"..\") || pathMod.isAbsolute(rel)) {\n throw new Error(`path escapes sandbox root (${normRoot}): ${raw}`);\n }\n return resolved;\n };\n\n registry.register({\n name: \"read_file\",\n description:\n \"Read a file under the sandbox root. Returns the full contents (truncated with a notice if larger than the per-call cap). Paths may be relative to the root or absolute-under-root.\",\n readOnly: true,\n parameters: {\n type: \"object\",\n properties: {\n path: { type: \"string\", description: \"Path to read (relative to rootDir or absolute).\" },\n head: { type: \"integer\", description: \"If set, return only the first N lines.\" },\n tail: { type: \"integer\", description: \"If set, return only the last N lines.\" },\n },\n required: [\"path\"],\n },\n fn: async (args: { path: string; head?: number; tail?: number }) => {\n const abs = safePath(args.path);\n const stat = await fs.stat(abs);\n if (stat.isDirectory()) {\n throw new Error(`not a file: ${args.path} (it's a directory)`);\n }\n const raw = await fs.readFile(abs);\n if (raw.length > maxReadBytes) {\n const head = raw.slice(0, maxReadBytes).toString(\"utf8\");\n return `${head}\\n\\n[…truncated ${raw.length - maxReadBytes} bytes — file is ${raw.length} B, cap ${maxReadBytes} B. Retry with head/tail for targeted view.]`;\n }\n const text = raw.toString(\"utf8\");\n if (typeof args.head === \"number\" && args.head > 0) {\n return text.split(/\\r?\\n/).slice(0, args.head).join(\"\\n\");\n }\n if (typeof args.tail === \"number\" && args.tail > 0) {\n let lines = text.split(/\\r?\\n/);\n // Most files end with a final '\\n', which splits into an empty\n // trailing string. Drop it before slicing so tail=2 returns the\n // last two *real* lines, not \"last line + empty\".\n if (lines.length > 0 && lines[lines.length - 1] === \"\") lines = lines.slice(0, -1);\n return lines.slice(Math.max(0, lines.length - args.tail)).join(\"\\n\");\n }\n return text;\n },\n });\n\n registry.register({\n name: \"list_directory\",\n description:\n \"List entries in a directory under the sandbox root. Returns one line per entry, marking directories with a trailing slash. Not recursive — use directory_tree for that.\",\n readOnly: true,\n parameters: {\n type: \"object\",\n properties: {\n path: { type: \"string\", description: \"Directory to list (default: root).\" },\n },\n },\n fn: async (args: { path?: string }) => {\n const abs = safePath(args.path ?? \".\");\n const entries = await fs.readdir(abs, { withFileTypes: true });\n const lines: string[] = [];\n for (const e of entries.sort((a, b) => a.name.localeCompare(b.name))) {\n lines.push(e.isDirectory() ? `${e.name}/` : e.name);\n }\n return lines.join(\"\\n\") || \"(empty directory)\";\n },\n });\n\n registry.register({\n name: \"directory_tree\",\n description:\n \"Recursively list entries in a directory. Shows indented tree structure with directories marked '/'. Caps output so a huge tree doesn't drown the context.\",\n readOnly: true,\n parameters: {\n type: \"object\",\n properties: {\n path: { type: \"string\", description: \"Root of the tree (default: sandbox root).\" },\n maxDepth: { type: \"integer\", description: \"Max recursion depth (default 4).\" },\n },\n },\n fn: async (args: { path?: string; maxDepth?: number }) => {\n const startAbs = safePath(args.path ?? \".\");\n const maxDepth = typeof args.maxDepth === \"number\" ? args.maxDepth : 4;\n const lines: string[] = [];\n let totalBytes = 0;\n let truncated = false;\n const walk = async (dir: string, depth: number): Promise<void> => {\n if (truncated) return;\n if (depth > maxDepth) return;\n let entries: import(\"node:fs\").Dirent[];\n try {\n entries = await fs.readdir(dir, { withFileTypes: true });\n } catch {\n return;\n }\n entries.sort((a, b) => a.name.localeCompare(b.name));\n for (const e of entries) {\n if (truncated) return;\n const indent = \" \".repeat(depth);\n const line = e.isDirectory() ? `${indent}${e.name}/` : `${indent}${e.name}`;\n totalBytes += line.length + 1;\n if (totalBytes > maxListBytes) {\n lines.push(` [… tree truncated at ${maxListBytes} bytes …]`);\n truncated = true;\n return;\n }\n lines.push(line);\n if (e.isDirectory()) {\n await walk(pathMod.join(dir, e.name), depth + 1);\n }\n }\n };\n await walk(startAbs, 0);\n return lines.join(\"\\n\") || \"(empty tree)\";\n },\n });\n\n registry.register({\n name: \"search_files\",\n description:\n \"Find files whose NAME matches a substring or regex. Case-insensitive. Walks the directory recursively under the sandbox root. Returns one path per line.\",\n readOnly: true,\n parameters: {\n type: \"object\",\n properties: {\n path: { type: \"string\", description: \"Directory to start the search at (default: root).\" },\n pattern: {\n type: \"string\",\n description: \"Substring (or regex) to match against filenames.\",\n },\n },\n required: [\"pattern\"],\n },\n fn: async (args: { path?: string; pattern: string }) => {\n const startAbs = safePath(args.path ?? \".\");\n const needle = args.pattern.toLowerCase();\n // Try as regex first (permits users who want patterns); fall\n // back to plain substring when it's not a valid regex. Flag `i`\n // so matching is case-insensitive regardless of path.\n let re: RegExp | null = null;\n try {\n re = new RegExp(args.pattern, \"i\");\n } catch {\n re = null;\n }\n const matches: string[] = [];\n let totalBytes = 0;\n const walk = async (dir: string): Promise<void> => {\n let entries: import(\"node:fs\").Dirent[];\n try {\n entries = await fs.readdir(dir, { withFileTypes: true });\n } catch {\n return;\n }\n for (const e of entries) {\n const full = pathMod.join(dir, e.name);\n const lower = e.name.toLowerCase();\n const hit = re ? re.test(e.name) : lower.includes(needle);\n if (hit) {\n const rel = pathMod.relative(rootDir, full);\n if (totalBytes + rel.length + 1 > maxListBytes) {\n matches.push(\"[… search truncated — refine pattern …]\");\n return;\n }\n matches.push(rel);\n totalBytes += rel.length + 1;\n }\n if (e.isDirectory()) await walk(full);\n }\n };\n await walk(startAbs);\n return matches.length === 0 ? \"(no matches)\" : matches.join(\"\\n\");\n },\n });\n\n registry.register({\n name: \"search_content\",\n description:\n \"Recursively grep file CONTENTS for a substring or regex. This is the right tool for 'find all places that call X', 'where is Y referenced', 'what files contain Z'. Different from search_files (which matches FILE NAMES). Returns one match per line in 'path:line: text' format. Skips dependency / VCS / build directories (node_modules, .git, dist, build, .next, target, .venv) and binary files by default.\",\n readOnly: true,\n parameters: {\n type: \"object\",\n properties: {\n pattern: {\n type: \"string\",\n description: \"Substring (or regex) to search file contents for.\",\n },\n path: {\n type: \"string\",\n description: \"Directory to start the search at (default: sandbox root).\",\n },\n glob: {\n type: \"string\",\n description:\n \"Optional file-name suffix or substring filter. Examples: '.ts' (only TypeScript), 'test' (any file with 'test' in the name). Reduces noise when you know the file shape.\",\n },\n case_sensitive: {\n type: \"boolean\",\n description: \"When true, match case exactly. Default false (case-insensitive).\",\n },\n include_deps: {\n type: \"boolean\",\n description:\n \"When true, also search inside node_modules / .git / dist / build / etc. Off by default — most exploration questions are about the user's own code.\",\n },\n },\n required: [\"pattern\"],\n },\n fn: async (args: {\n pattern: string;\n path?: string;\n glob?: string;\n case_sensitive?: boolean;\n include_deps?: boolean;\n }) => {\n const startAbs = safePath(args.path ?? \".\");\n const caseSensitive = args.case_sensitive === true;\n const includeDeps = args.include_deps === true;\n const nameFilter = typeof args.glob === \"string\" ? args.glob.toLowerCase() : null;\n // Try the pattern as a regex first (lets the model say `\\bdispatch\\(`\n // for a word-bounded match); fall back to literal substring on\n // invalid regex. No `g` flag — we test once per line, so global\n // statefulness (lastIndex tracking) would just be noise.\n let re: RegExp | null = null;\n try {\n re = new RegExp(args.pattern, caseSensitive ? \"\" : \"i\");\n } catch {\n re = null;\n }\n const needle = caseSensitive ? args.pattern : args.pattern.toLowerCase();\n const matches: string[] = [];\n let totalBytes = 0;\n let scanned = 0;\n let truncated = false;\n\n const walk = async (dir: string): Promise<void> => {\n if (truncated) return;\n let entries: import(\"node:fs\").Dirent[];\n try {\n entries = await fs.readdir(dir, { withFileTypes: true });\n } catch {\n return;\n }\n for (const e of entries) {\n if (truncated) return;\n if (e.isDirectory()) {\n if (!includeDeps && SKIP_DIR_NAMES.has(e.name)) continue;\n await walk(pathMod.join(dir, e.name));\n continue;\n }\n if (!e.isFile()) continue;\n if (nameFilter && !e.name.toLowerCase().includes(nameFilter)) continue;\n if (isLikelyBinaryByName(e.name)) continue;\n const full = pathMod.join(dir, e.name);\n let stat: import(\"node:fs\").Stats;\n try {\n stat = await fs.stat(full);\n } catch {\n continue;\n }\n // Per-file size cap so a 50MB log doesn't dominate the search.\n // Anything legitimately interesting fits in 2 MB; bigger files\n // are usually data dumps or generated bundles.\n if (stat.size > 2 * 1024 * 1024) continue;\n let raw: Buffer;\n try {\n raw = await fs.readFile(full);\n } catch {\n continue;\n }\n // Content-based binary sniff: a NUL byte in the first 8KB is\n // a strong indicator. Catches binaries with .json or .txt\n // extensions (yes, this happens).\n const firstNul = raw.indexOf(0);\n if (firstNul !== -1 && firstNul < 8 * 1024) continue;\n const text = raw.toString(\"utf8\");\n const rel = pathMod.relative(rootDir, full);\n const lines = text.split(/\\r?\\n/);\n for (let li = 0; li < lines.length; li++) {\n const line = lines[li]!;\n const lineForCheck = caseSensitive ? line : line.toLowerCase();\n const hit = re ? re.test(line) : lineForCheck.includes(needle);\n if (!hit) continue;\n // Truncate very long lines so one giant minified file\n // doesn't blow the budget on a single match.\n const display = line.length > 200 ? `${line.slice(0, 200)}…` : line;\n const out = `${rel}:${li + 1}: ${display}`;\n if (totalBytes + out.length + 1 > maxListBytes) {\n matches.push(`[… truncated at ${maxListBytes} bytes — refine pattern or path …]`);\n truncated = true;\n return;\n }\n matches.push(out);\n totalBytes += out.length + 1;\n }\n scanned++;\n }\n };\n await walk(startAbs);\n if (matches.length === 0) {\n return scanned === 0\n ? \"(no files scanned — path empty or all files filtered out)\"\n : `(no matches across ${scanned} file${scanned === 1 ? \"\" : \"s\"})`;\n }\n return matches.join(\"\\n\");\n },\n });\n\n registry.register({\n name: \"get_file_info\",\n description:\n \"Stat a path under the sandbox root. Returns type (file|directory|symlink), size in bytes, mtime in ISO-8601.\",\n readOnly: true,\n parameters: {\n type: \"object\",\n properties: {\n path: { type: \"string\" },\n },\n required: [\"path\"],\n },\n fn: async (args: { path: string }) => {\n const abs = safePath(args.path);\n const st = await fs.lstat(abs);\n const type = st.isDirectory() ? \"directory\" : st.isSymbolicLink() ? \"symlink\" : \"file\";\n return JSON.stringify({\n type,\n size: st.size,\n mtime: st.mtime.toISOString(),\n });\n },\n });\n\n if (!allowWriting) return registry;\n\n registry.register({\n name: \"write_file\",\n description:\n \"Create or overwrite a file under the sandbox root with the given content. Parent directories are created as needed.\",\n parameters: {\n type: \"object\",\n properties: {\n path: { type: \"string\" },\n content: { type: \"string\" },\n },\n required: [\"path\", \"content\"],\n },\n fn: async (args: { path: string; content: string }) => {\n const abs = safePath(args.path);\n await fs.mkdir(pathMod.dirname(abs), { recursive: true });\n await fs.writeFile(abs, args.content, \"utf8\");\n return `wrote ${args.content.length} chars to ${pathMod.relative(rootDir, abs)}`;\n },\n });\n\n registry.register({\n name: \"edit_file\",\n description:\n \"Apply a SEARCH/REPLACE edit to an existing file. `search` must match exactly (whitespace sensitive) — no regex. The match must be unique in the file; otherwise the edit is refused to avoid surprise rewrites.\",\n parameters: {\n type: \"object\",\n properties: {\n path: { type: \"string\" },\n search: { type: \"string\", description: \"Exact text to find (must be unique).\" },\n replace: { type: \"string\", description: \"Text to substitute in place of `search`.\" },\n },\n required: [\"path\", \"search\", \"replace\"],\n },\n fn: async (args: { path: string; search: string; replace: string }) => {\n const abs = safePath(args.path);\n const before = await fs.readFile(abs, \"utf8\");\n if (args.search.length === 0) {\n throw new Error(\"edit_file: search cannot be empty\");\n }\n const firstIdx = before.indexOf(args.search);\n if (firstIdx < 0) {\n throw new Error(`edit_file: search text not found in ${pathMod.relative(rootDir, abs)}`);\n }\n const nextIdx = before.indexOf(args.search, firstIdx + 1);\n if (nextIdx >= 0) {\n throw new Error(\n `edit_file: search text appears multiple times in ${pathMod.relative(rootDir, abs)} — include more context to disambiguate`,\n );\n }\n const after =\n before.slice(0, firstIdx) + args.replace + before.slice(firstIdx + args.search.length);\n await fs.writeFile(abs, after, \"utf8\");\n const rel = pathMod.relative(rootDir, abs);\n const header = `edited ${rel} (${args.search.length}→${args.replace.length} chars)`;\n // Starting line number of the search block in the original\n // file. `split/length` on the prefix gives a 1-based line\n // count where the match begins, matching git-diff's @@ -N,M\n // +N,M @@ header convention.\n const startLine = before.slice(0, firstIdx).split(/\\r?\\n/).length;\n const diff = renderEditDiff(args.search, args.replace, startLine);\n return `${header}\\n${diff}`;\n },\n });\n\n registry.register({\n name: \"create_directory\",\n description: \"Create a directory (and any missing parents) under the sandbox root.\",\n parameters: {\n type: \"object\",\n properties: { path: { type: \"string\" } },\n required: [\"path\"],\n },\n fn: async (args: { path: string }) => {\n const abs = safePath(args.path);\n await fs.mkdir(abs, { recursive: true });\n return `created ${pathMod.relative(rootDir, abs)}/`;\n },\n });\n\n registry.register({\n name: \"move_file\",\n description: \"Rename/move a file or directory under the sandbox root.\",\n parameters: {\n type: \"object\",\n properties: {\n source: { type: \"string\" },\n destination: { type: \"string\" },\n },\n required: [\"source\", \"destination\"],\n },\n fn: async (args: { source: string; destination: string }) => {\n const src = safePath(args.source);\n const dst = safePath(args.destination);\n await fs.mkdir(pathMod.dirname(dst), { recursive: true });\n await fs.rename(src, dst);\n return `moved ${pathMod.relative(rootDir, src)} → ${pathMod.relative(rootDir, dst)}`;\n },\n });\n\n return registry;\n}\n\n/**\n * Format an edit_file change as a proper line-level diff, styled\n * like `git diff`. Starts with a unified-diff hunk header —\n * `@@ -startLine,oldCount +startLine,newCount @@` — so the user\n * can tell where in the file the change lands. Body uses LCS\n * (longest common subsequence) to mark lines as removed (`-`),\n * added (`+`), or unchanged context (` `). Users were getting\n * hundreds of `-` followed by hundreds of `+` for tiny changes\n * because the naive \"dump both sides\" format can't tell what\n * actually moved vs. stayed — this fixes that and adds line-\n * number context on top.\n */\nfunction renderEditDiff(search: string, replace: string, startLine: number): string {\n const a = search.split(/\\r?\\n/);\n const b = replace.split(/\\r?\\n/);\n const diff = lineDiff(a, b);\n const hunk = `@@ -${startLine},${a.length} +${startLine},${b.length} @@`;\n const body = diff.map((d) => `${d.op === \" \" ? \" \" : d.op} ${d.line}`).join(\"\\n\");\n return `${hunk}\\n${body}`;\n}\n\n/**\n * Compute a line-level diff via classic LCS dynamic programming.\n * Good enough for SEARCH/REPLACE blocks where both sides are\n * typically under a few hundred lines — O(n*m) space + time. For\n * huge blocks we'd want Myers' algorithm, but the caller already\n * caps the inline-display size and `/tool N` shows the full result,\n * so quadratic is fine in practice.\n *\n * Exported so tests can exercise the diff logic without spinning\n * up the full tool dispatch path.\n */\nexport function lineDiff(\n a: readonly string[],\n b: readonly string[],\n): Array<{ op: \"-\" | \"+\" | \" \"; line: string }> {\n const n = a.length;\n const m = b.length;\n // dp[i][j] = LCS length of a[0..i) and b[0..j).\n const dp: number[][] = Array.from({ length: n + 1 }, () => new Array(m + 1).fill(0));\n for (let i = 1; i <= n; i++) {\n for (let j = 1; j <= m; j++) {\n if (a[i - 1] === b[j - 1]) dp[i]![j] = dp[i - 1]![j - 1]! + 1;\n else dp[i]![j] = Math.max(dp[i - 1]![j]!, dp[i]![j - 1]!);\n }\n }\n // Backtrack to recover the op sequence.\n const out: Array<{ op: \"-\" | \"+\" | \" \"; line: string }> = [];\n let i = n;\n let j = m;\n while (i > 0 && j > 0) {\n if (a[i - 1] === b[j - 1]) {\n out.unshift({ op: \" \", line: a[i - 1]! });\n i--;\n j--;\n } else if ((dp[i - 1]![j] ?? 0) > (dp[i]![j - 1] ?? 0)) {\n out.unshift({ op: \"-\", line: a[i - 1]! });\n i--;\n } else {\n // Tie-break goes here (strictly less or equal): take the\n // insertion first during backtrack so the final forward order\n // renders removals BEFORE additions for a substitution —\n // matches git-diff convention of `- old / + new`.\n out.unshift({ op: \"+\", line: b[j - 1]! });\n j--;\n }\n }\n while (i > 0) {\n out.unshift({ op: \"-\", line: a[i - 1]! });\n i--;\n }\n while (j > 0) {\n out.unshift({ op: \"+\", line: b[j - 1]! });\n j--;\n }\n return out;\n}\n","/**\n * `remember` / `forget` / `recall_memory` — tools that let the model\n * read and write the user-memory store across sessions.\n *\n * Scope rules:\n * - `global` — always available (no sandbox needed).\n * - `project` — requires a `projectRoot` on MemoryStore. In chat mode\n * (no sandbox), the tools still register but a `scope=project` call\n * returns a structured refusal so the model can try `global` instead.\n *\n * Memory changes are written eagerly but NOT re-loaded into the prefix\n * mid-session (cache invariant). The user notices at `/new` or the next\n * launch — or they can read fresh content via `recall_memory` which\n * always hits disk.\n */\n\nimport type { ToolRegistry } from \"../tools.js\";\nimport {\n type MemoryScope,\n MemoryStore,\n type MemoryType,\n sanitizeMemoryName,\n} from \"../user-memory.js\";\n\nexport interface MemoryToolsOptions {\n /** Sandbox root for the `project` scope. Omit for chat mode. */\n projectRoot?: string;\n /** Override `~/.reasonix` (tests). */\n homeDir?: string;\n}\n\nexport function registerMemoryTools(\n registry: ToolRegistry,\n opts: MemoryToolsOptions = {},\n): ToolRegistry {\n const store = new MemoryStore({ homeDir: opts.homeDir, projectRoot: opts.projectRoot });\n const hasProject = store.hasProjectScope();\n\n registry.register({\n name: \"remember\",\n description:\n \"Save a memory for future sessions. Use when the user states a preference, corrects your approach, shares a non-obvious fact about this project, or explicitly asks you to remember something. Don't remember transient task state — only things worth recalling next session. The memory is written now but won't re-load into the system prompt until the next `/new` or launch.\",\n parameters: {\n type: \"object\",\n properties: {\n type: {\n type: \"string\",\n enum: [\"user\", \"feedback\", \"project\", \"reference\"],\n description:\n \"'user' = role/skills/prefs; 'feedback' = corrections or confirmed approaches; 'project' = facts/decisions about the current work; 'reference' = pointers to external systems the user uses.\",\n },\n scope: {\n type: \"string\",\n enum: [\"global\", \"project\"],\n description:\n \"'global' = applies across every project (preferences, tooling); 'project' = scoped to the current sandbox (decisions, local facts). Only available in `reasonix code`.\",\n },\n name: {\n type: \"string\",\n description:\n \"filename-safe identifier, 3-40 chars, alnum + _ - . (no path separators, no leading dot).\",\n },\n description: {\n type: \"string\",\n description: \"One-line summary shown in MEMORY.md (under ~150 chars).\",\n },\n content: {\n type: \"string\",\n description:\n \"Full memory body in markdown. For feedback/project types, structure as: rule/fact, then **Why:** line, then **How to apply:** line.\",\n },\n },\n required: [\"type\", \"scope\", \"name\", \"description\", \"content\"],\n },\n fn: async (args: {\n type: MemoryType;\n scope: MemoryScope;\n name: string;\n description: string;\n content: string;\n }) => {\n if (args.scope === \"project\" && !hasProject) {\n return JSON.stringify({\n error:\n \"scope='project' is unavailable in this session (no sandbox root). Retry with scope='global', or ask the user to switch to `reasonix code` for project-scoped memory.\",\n });\n }\n try {\n const path = store.write({\n name: args.name,\n type: args.type,\n scope: args.scope,\n description: args.description,\n body: args.content,\n });\n const key = sanitizeMemoryName(args.name);\n // The return text is load-bearing: it's the ONLY thing keeping\n // the fact visible within the current session, because the\n // prefix isn't re-hashed mid-session (Pillar 1). R1 reads this\n // on its next turn — the wording is deliberately imperative so\n // it doesn't get ignored in favor of explore-first behavior.\n return [\n `✓ REMEMBERED (${args.scope}/${key}): ${args.description}`,\n \"\",\n \"TREAT THIS AS ESTABLISHED FACT for the rest of this session.\",\n \"The user just told you — don't re-explore the filesystem to re-derive it.\",\n `(Saved to ${path}; pins into the system prompt on next /new or launch.)`,\n ].join(\"\\n\");\n } catch (err) {\n return JSON.stringify({ error: `remember failed: ${(err as Error).message}` });\n }\n },\n });\n\n registry.register({\n name: \"forget\",\n description:\n \"Delete a memory file and remove it from MEMORY.md. Use when the user explicitly asks to forget something, or when a previously-remembered fact has become wrong. Irreversible — no tombstone.\",\n parameters: {\n type: \"object\",\n properties: {\n name: { type: \"string\", description: \"Memory name (the identifier used in `remember`).\" },\n scope: { type: \"string\", enum: [\"global\", \"project\"] },\n },\n required: [\"name\", \"scope\"],\n },\n fn: async (args: { name: string; scope: MemoryScope }) => {\n if (args.scope === \"project\" && !hasProject) {\n return JSON.stringify({\n error: \"scope='project' is unavailable in this session (no sandbox root).\",\n });\n }\n try {\n const existed = store.delete(args.scope, args.name);\n return existed\n ? `forgot (${args.scope}/${sanitizeMemoryName(args.name)}). Re-load on next /new or launch.`\n : `no such memory: ${args.scope}/${args.name} (nothing to forget).`;\n } catch (err) {\n return JSON.stringify({ error: `forget failed: ${(err as Error).message}` });\n }\n },\n });\n\n registry.register({\n name: \"recall_memory\",\n description:\n \"Read the full body of a memory file when its MEMORY.md one-liner (already in the system prompt) isn't enough detail. Most of the time the index suffices — only call this when the user's question genuinely requires the full context.\",\n readOnly: true,\n parameters: {\n type: \"object\",\n properties: {\n name: { type: \"string\" },\n scope: { type: \"string\", enum: [\"global\", \"project\"] },\n },\n required: [\"name\", \"scope\"],\n },\n fn: async (args: { name: string; scope: MemoryScope }) => {\n if (args.scope === \"project\" && !hasProject) {\n return JSON.stringify({\n error: \"scope='project' is unavailable in this session (no sandbox root).\",\n });\n }\n try {\n const entry = store.read(args.scope, args.name);\n return [\n `# ${entry.name} (${entry.scope}/${entry.type}, created ${entry.createdAt || \"?\"})`,\n entry.description ? `> ${entry.description}` : \"\",\n \"\",\n entry.body,\n ]\n .filter(Boolean)\n .join(\"\\n\");\n } catch (err) {\n return JSON.stringify({ error: `recall failed: ${(err as Error).message}` });\n }\n },\n });\n\n return registry;\n}\n","/**\n * Plan Mode — read-only exploration phase for `reasonix code`.\n *\n * Shape (mirrors claude-code's plan/act split, adapted for Reasonix):\n *\n * 1. User types `/plan` → registry switches to plan-mode enforcement\n * (write tools refused at dispatch; reads + allowlisted shell\n * still work).\n * 2. Model explores, then calls `submit_plan` with a markdown plan.\n * 3. `submit_plan` throws `PlanProposedError`, which the TUI renders\n * as a picker: Approve / Refine / Cancel.\n * 4. Approve → registry leaves plan mode, a synthetic user message\n * \"The plan has been approved. Implement it now.\" is pushed into\n * the loop so the next turn executes.\n *\n * The read-only enforcement lives in `ToolRegistry.dispatch` via\n * `readOnly` / `readOnlyCheck`; this file only ships the `submit_plan`\n * escape hatch and the error type that carries the plan out of the\n * registry without stuffing it into the message.\n *\n * We do not change `ImmutablePrefix.toolSpecs` when plan mode toggles —\n * that would break Pillar 1's prefix cache. Instead the same full spec\n * list stays pinned, and the registry enforces mode at dispatch time.\n * The refusal string teaches the model the rule; cache hits stay\n * intact.\n */\n\nimport type { ToolRegistry } from \"../tools.js\";\n\n/**\n * Thrown by `submit_plan` when plan mode is active, carrying the plan\n * text the TUI will render for the user's approval.\n *\n * Implements the `toToolResult` protocol so `ToolRegistry.dispatch`\n * serializes the full plan into the tool-result JSON (not just the\n * error message). The TUI parses `{ error, plan }` from the tool event\n * and mounts the `PlanConfirm` picker.\n */\nexport class PlanProposedError extends Error {\n readonly plan: string;\n constructor(plan: string) {\n super(\n \"PlanProposedError: plan submitted. STOP calling tools now — the TUI has shown the plan to the user. Wait for their next message; it will either approve (you'll then implement the plan), request a refinement (you should explore more and submit an updated plan), or cancel (drop the plan and ask what they want instead). Don't call any tools in the meantime.\",\n );\n this.name = \"PlanProposedError\";\n this.plan = plan;\n }\n\n /**\n * Structured tool-result shape. Consumed by the TUI to extract the\n * plan without regex-scraping the error message.\n */\n toToolResult(): { error: string; plan: string } {\n return { error: `${this.name}: ${this.message}`, plan: this.plan };\n }\n}\n\nexport interface PlanToolOptions {\n /**\n * Optional side-channel callback fired when the model submits a plan.\n * The TUI uses this to preview the plan in real time (the tool-result\n * event is also emitted; this is just earlier and friendlier to\n * test harnesses that don't want to parse JSON).\n */\n onPlanSubmitted?: (plan: string) => void;\n}\n\nexport function registerPlanTool(registry: ToolRegistry, opts: PlanToolOptions = {}): ToolRegistry {\n registry.register({\n name: \"submit_plan\",\n description:\n \"Submit a concrete plan to the user for review before executing. Use this for tasks that warrant a review gate — multi-file refactors, architecture changes, anything that would be expensive or confusing to undo. Skip it for small fixes (one-line typo, obvious bug with a clear fix) — just make the change. The user will either approve (you then implement it), ask for refinement, or cancel. If the user has already enabled /plan mode, writes are blocked at dispatch and you MUST use this. Write the plan as markdown with a one-line summary, a bulleted list of files to touch and what will change, and any risks or open questions.\",\n readOnly: true,\n parameters: {\n type: \"object\",\n properties: {\n plan: {\n type: \"string\",\n description:\n \"Markdown-formatted plan. Lead with a one-sentence summary. Then a file-by-file breakdown of what you'll change and why. Flag any risks or open questions at the end so the user can weigh in before you start.\",\n },\n },\n required: [\"plan\"],\n },\n fn: async (args: { plan: string }) => {\n const plan = (args?.plan ?? \"\").trim();\n if (!plan) {\n throw new Error(\"submit_plan: empty plan — write a markdown plan and try again.\");\n }\n // Always fire the picker, not just inside plan mode. Plan mode's\n // role is the *stronger* constraint — it forces you into read-only\n // until you submit. Outside plan mode, submit_plan is your own\n // call: use it when the task is large enough to deserve a review\n // gate (multi-file refactors, architecture changes, anything\n // that would be expensive to undo), skip it for small fixes.\n opts.onPlanSubmitted?.(plan);\n throw new PlanProposedError(plan);\n },\n });\n return registry;\n}\n","/**\n * Subagent runtime — isolated child loops for offloading exploration or\n * self-contained subtasks.\n *\n * Two surfaces sit on top of the same `spawnSubagent` core:\n *\n * 1. `registerSubagentTool` — exposes a low-level `spawn_subagent`\n * function-call tool. Library API. NOT registered into the model\n * tool list by `reasonix code` since 0.4.26 — Skills (with\n * `runAs: subagent` frontmatter) became the user-facing surface.\n * Kept exported because library callers and tests still want\n * direct access to the primitive.\n *\n * 2. `run_skill` (in src/tools/skills.ts) — when the resolved skill\n * has `runAs: subagent`, it calls `spawnSubagent` with the skill\n * body as the system prompt and the user's `arguments` as the\n * task. Subagent skills are listed in the pinned Skills index\n * with a 🧬 marker, which gives the model a clear pattern-match\n * trigger without forcing it to reason about \"is this task big\n * enough to delegate.\"\n *\n * Why R1 specifically benefits:\n * - R1 reasoning tokens are expensive AND inflate the parent context.\n * A subagent runs its own private loop, then surfaces only the\n * distilled final answer back to the parent — the main session\n * never sees the reasoning trail.\n *\n * Invariants common to both surfaces:\n * - Serial only — no parallel spawn (MVP).\n * - Inherits parent's tool registry MINUS `spawn_subagent` itself\n * (no recursion via the tool API) and MINUS `submit_plan`\n * (subagents don't propose plans to the user).\n * - No hooks, no session — runs are ephemeral.\n * - Lower default `maxToolIters` than the parent (16 vs 64).\n * - Independent prefix cache (subagent's prefix has its own\n * fingerprint).\n * - Parent registry's plan-mode state propagates: subagents can't\n * escape `/plan`.\n * - Non-streaming child loop — the parent isn't watching deltas, so\n * streaming would only add an SSE parser to the critical path.\n * Cancellation still works via the AbortSignal.\n */\n\nimport type { DeepSeekClient } from \"../client.js\";\nimport { CacheFirstLoop } from \"../loop.js\";\nimport { ImmutablePrefix } from \"../memory.js\";\nimport { applyProjectMemory } from \"../project-memory.js\";\nimport { ToolRegistry } from \"../tools.js\";\n\n/**\n * Live event emitted by a running subagent. Surfaced via the optional\n * `sink` ref the TUI attaches its handler to. Side-channel only — these\n * events do NOT pass through the parent loop's `LoopEvent` stream\n * because subagents run inside a tool-dispatch frame, after the parent's\n * `step()` has already yielded `tool_start` and is awaiting the result.\n */\nexport interface SubagentEvent {\n kind: \"start\" | \"progress\" | \"end\";\n /** First ~30 chars of the task prompt — used for the TUI status row. */\n task: string;\n /** Iteration count inside the child loop (number of tool results so far). */\n iter?: number;\n /** Wall-clock ms since the subagent started. */\n elapsedMs?: number;\n /** First ~120 chars of the final assistant message. Set on `end`. */\n summary?: string;\n /** Error message if the subagent failed. Set on `end`. */\n error?: string;\n /** Total turns the subagent took. Set on `end`. */\n turns?: number;\n}\n\n/**\n * Mutable ref the registration writes through. The TUI sets `.current`\n * to its own handler on mount; nothing receives events before that\n * happens (and headless callers leave `.current = null`, which is the\n * library-mode default — they read the final result from the helper's\n * return value instead).\n */\nexport interface SubagentSink {\n current: ((ev: SubagentEvent) => void) | null;\n}\n\n/**\n * Per-spawn options for {@link spawnSubagent}. All but `parentRegistry`\n * + `client` + `system` + `task` are tunables with sensible defaults.\n */\nexport interface SpawnSubagentOptions {\n /** Shared DeepSeek client. The subagent reuses it (same auth, same retries). */\n client: DeepSeekClient;\n /**\n * Parent registry — the subagent inherits a copy of its tools (minus\n * the never-inherited set: `spawn_subagent`, `submit_plan`).\n */\n parentRegistry: ToolRegistry;\n /**\n * System prompt for the child loop. Skills' subagent path passes\n * the skill body here; the spawn_subagent tool passes its default\n * (or the model's `system` argument override).\n */\n system: string;\n /** The task / question / instruction the subagent must address. */\n task: string;\n /** Model id for the child loop. Defaults to `deepseek-chat`. */\n model?: string;\n /** Iteration ceiling for the child loop. Defaults to 16. */\n maxToolIters?: number;\n /**\n * Maximum chars of the final assistant message returned. Long answers\n * are truncated with a notice — the parent's prompt budget shouldn't\n * be blown out by an over-eager subagent.\n */\n maxResultChars?: number;\n /** Optional sink for TUI live updates. */\n sink?: SubagentSink;\n /**\n * Parent's per-tool-dispatch AbortSignal. When the parent aborts (Esc),\n * we forward the cancel into the child loop. Omit for headless callers\n * that don't care about cancellation.\n */\n parentSignal?: AbortSignal;\n}\n\n/**\n * Structured result of a subagent run. The two convenience JSON wrappers\n * (`spawn_subagent` tool, `run_skill` subagent path) serialize this for\n * the model; library callers can read the typed object directly.\n */\nexport interface SubagentResult {\n success: boolean;\n /** Final assistant message (possibly truncated). Empty on error. */\n output: string;\n /** Set when the run failed (network, child-loop error, etc.). */\n error?: string;\n /** Turns the child loop took. */\n turns: number;\n /** Tool calls dispatched inside the child loop. */\n toolIters: number;\n /** Wall-clock ms. */\n elapsedMs: number;\n /** USD spent in the child loop, summed across its turns. */\n costUsd: number;\n}\n\nexport interface SubagentToolOptions {\n /** Shared DeepSeek client. */\n client: DeepSeekClient;\n /**\n * Default system prompt used when the model doesn't pass one. Project\n * memory (REASONIX.md) is appended automatically when `projectRoot` is\n * set.\n */\n defaultSystem?: string;\n /** Project root for `applyProjectMemory` lookup. Omit in chat mode. */\n projectRoot?: string;\n /** Default model. `deepseek-chat` (V3) by default. */\n defaultModel?: string;\n /** Iteration ceiling. Lower than the parent (16 by default). */\n maxToolIters?: number;\n /** Maximum chars returned in the tool result. */\n maxResultChars?: number;\n /** Optional sink the TUI attaches its handler to for live updates. */\n sink?: SubagentSink;\n}\n\nconst DEFAULT_SUBAGENT_SYSTEM = `You are a Reasonix subagent. The parent agent spawned you to handle one focused subtask, then return.\n\nRules:\n- Stay on the task you were given. Do not expand scope.\n- Use tools as needed. You share the parent's sandbox + safety rules.\n- When you're done, your final assistant message is the only thing the parent will see — make it complete and self-contained. No follow-up offers, no questions, no \"let me know if you need more.\"\n- Prefer one clear, distilled answer over a long log of what you tried.\n\nFormatting rules (the parent renders your reply in a TUI with a real markdown renderer):\n- For tabular data use GitHub-Flavored Markdown tables with ASCII pipes: \\`| col | col |\\` headers, \\`| --- | --- |\\` separator. NEVER draw tables with Unicode box-drawing characters (│ ─ ┼ ┌ ┐ └ ┘ ├ ┤). They look intentional but break terminal word-wrap and produce garbled output.\n- Keep table cells short — one short phrase per cell, not multi-line paragraphs. If a description doesn't fit in ~40 chars, use bullets below the table instead.\n- Use fenced code blocks (\\`\\`\\`) for any code, file paths with line ranges, or shell commands.\n- NEVER draw decorative frames around content with \\`┌──┐ │ └──┘\\` box-drawing characters. The renderer handles code blocks and headings on its own — extra ASCII art adds noise without value and breaks at narrow terminal widths.\n- For flow charts and diagrams: use a markdown bullet list with \\`→\\` or \\`↓\\` between steps. Don't try to draw boxes-and-arrows in ASCII; it never survives word-wrap.`;\n\nconst DEFAULT_MAX_RESULT_CHARS = 8000;\nconst DEFAULT_MAX_ITERS = 16;\nconst DEFAULT_SUBAGENT_MODEL = \"deepseek-chat\";\n\nconst SUBAGENT_TOOL_NAME = \"spawn_subagent\";\n/**\n * Tools the subagent never inherits from the parent registry:\n * - spawn_subagent itself: would allow unbounded recursion via the\n * tool API. Depth=1 hard cap by construction.\n * - submit_plan: only the parent talks to the user about plan\n * approval; a subagent submitting a plan would surface a picker\n * midway through the parent's turn, which the user did not ask for.\n */\nconst NEVER_INHERITED_TOOLS = new Set<string>([SUBAGENT_TOOL_NAME, \"submit_plan\"]);\n\n/**\n * Run one subagent to completion. The unified primitive both\n * `spawn_subagent` (function-call tool) and `run_skill` (subagent\n * skills) call into.\n *\n * Headless: returns a `SubagentResult` regardless of success/failure.\n * Errors are captured in the structured shape, never thrown — the\n * caller decides how to surface them (tool result JSON, log line, etc.).\n */\nexport async function spawnSubagent(opts: SpawnSubagentOptions): Promise<SubagentResult> {\n const model = opts.model ?? DEFAULT_SUBAGENT_MODEL;\n const maxToolIters = opts.maxToolIters ?? DEFAULT_MAX_ITERS;\n const maxResultChars = opts.maxResultChars ?? DEFAULT_MAX_RESULT_CHARS;\n const sink = opts.sink;\n\n const startedAt = Date.now();\n const taskPreview = opts.task.length > 30 ? `${opts.task.slice(0, 30)}…` : opts.task;\n sink?.current?.({\n kind: \"start\",\n task: taskPreview,\n iter: 0,\n elapsedMs: 0,\n });\n\n const childTools = forkRegistryExcluding(opts.parentRegistry, NEVER_INHERITED_TOOLS);\n const childPrefix = new ImmutablePrefix({\n system: opts.system,\n toolSpecs: childTools.specs(),\n });\n const childLoop = new CacheFirstLoop({\n client: opts.client,\n prefix: childPrefix,\n tools: childTools,\n model,\n maxToolIters,\n hooks: [],\n stream: false,\n });\n\n const onParentAbort = () => childLoop.abort();\n opts.parentSignal?.addEventListener(\"abort\", onParentAbort, { once: true });\n\n let final = \"\";\n let errorMessage: string | undefined;\n let toolIter = 0;\n try {\n for await (const ev of childLoop.step(opts.task)) {\n if (ev.role === \"tool\") {\n toolIter++;\n sink?.current?.({\n kind: \"progress\",\n task: taskPreview,\n iter: toolIter,\n elapsedMs: Date.now() - startedAt,\n });\n }\n if (ev.role === \"assistant_final\") {\n final = ev.content ?? \"\";\n }\n if (ev.role === \"error\") {\n errorMessage = ev.error ?? \"subagent error\";\n }\n }\n } catch (err) {\n errorMessage = (err as Error).message;\n } finally {\n opts.parentSignal?.removeEventListener(\"abort\", onParentAbort);\n }\n // The loop yields `done` without an `error` event when its API call\n // is aborted mid-flight (intentional UX — see the matching catch in\n // CacheFirstLoop.step). From a SUBAGENT consumer's perspective that\n // still counts as a failure: no answer came back, the parent has\n // nothing to render. Synthesize an error so `success: false` and the\n // UI surfaces the abort instead of returning empty output.\n if (!errorMessage && !final) {\n errorMessage = opts.parentSignal?.aborted\n ? \"subagent aborted before producing an answer\"\n : \"subagent ended without producing an answer\";\n }\n\n const elapsedMs = Date.now() - startedAt;\n const turns = childLoop.stats.turns.length;\n const costUsd = childLoop.stats.totalCost;\n\n const truncated =\n final.length > maxResultChars\n ? `${final.slice(0, maxResultChars)}\\n\\n[…truncated ${final.length - maxResultChars} chars; ask the subagent for a tighter summary if you need more.]`\n : final;\n\n sink?.current?.({\n kind: \"end\",\n task: taskPreview,\n iter: toolIter,\n elapsedMs,\n summary: errorMessage ? undefined : truncated.slice(0, 120),\n error: errorMessage,\n turns,\n });\n\n return {\n success: !errorMessage,\n output: errorMessage ? \"\" : truncated,\n error: errorMessage,\n turns,\n toolIters: toolIter,\n elapsedMs,\n costUsd,\n };\n}\n\n/**\n * Serialize a {@link SubagentResult} into the JSON tool-result shape\n * the model consumes. Both the spawn_subagent tool and the run_skill\n * subagent path return this string verbatim, so the schema stays\n * stable across both surfaces.\n */\nexport function formatSubagentResult(r: SubagentResult): string {\n if (!r.success) {\n return JSON.stringify({\n success: false,\n error: r.error ?? \"unknown subagent error\",\n turns: r.turns,\n tool_iters: r.toolIters,\n elapsed_ms: r.elapsedMs,\n });\n }\n return JSON.stringify({\n success: true,\n output: r.output,\n turns: r.turns,\n tool_iters: r.toolIters,\n elapsed_ms: r.elapsedMs,\n cost_usd: r.costUsd,\n });\n}\n\n/**\n * Register the spawn_subagent tool into the parent registry. Library\n * surface — `reasonix code` does NOT call this since 0.4.26 (Skills\n * with `runAs: subagent` are the user-facing surface), but library\n * consumers who want the low-level tool can opt in.\n */\nexport function registerSubagentTool(\n parentRegistry: ToolRegistry,\n opts: SubagentToolOptions,\n): ToolRegistry {\n const baseSystem = opts.defaultSystem ?? DEFAULT_SUBAGENT_SYSTEM;\n // Bake project memory into the default once — re-reading on every\n // spawn would (a) make the child prefix unstable when REASONIX.md\n // changes mid-session, defeating cache reuse across multiple\n // subagent calls, and (b) cost a stat() per call. The parent itself\n // also reads memory once at startup; matching that semantics keeps\n // subagent and parent on the same page.\n const defaultSystem = opts.projectRoot\n ? applyProjectMemory(baseSystem, opts.projectRoot)\n : baseSystem;\n const defaultModel = opts.defaultModel ?? DEFAULT_SUBAGENT_MODEL;\n const maxToolIters = opts.maxToolIters ?? DEFAULT_MAX_ITERS;\n const maxResultChars = opts.maxResultChars ?? DEFAULT_MAX_RESULT_CHARS;\n const sink = opts.sink;\n\n parentRegistry.register({\n name: SUBAGENT_TOOL_NAME,\n description:\n \"Spawn an isolated subagent to handle a self-contained subtask in a fresh context, returning only its final answer. Use for: deep codebase exploration that would flood the main context, multi-step research where you only need the conclusion, or any focused subtask whose intermediate reasoning the user does not need to see. The subagent inherits all your tools (filesystem, shell, web, MCP, etc.) but runs in its own isolated message log — its tool calls and reasoning never enter your context. Only the final assistant message comes back as this tool's result. Keep tasks focused; the subagent has a stricter iter budget than you do.\",\n parameters: {\n type: \"object\",\n properties: {\n task: {\n type: \"string\",\n description:\n \"The subtask the subagent should perform. Be specific and self-contained — the subagent has none of your conversation context, only what you write here.\",\n },\n system: {\n type: \"string\",\n description:\n \"Optional override for the subagent's system prompt. The default tells it to stay focused and return a concise answer; override only when the subtask needs a specialized persona.\",\n },\n model: {\n type: \"string\",\n enum: [\"deepseek-chat\", \"deepseek-reasoner\"],\n description:\n \"Which DeepSeek model the subagent runs on. 'deepseek-chat' (V3) is the default — fast and cheap. Use 'deepseek-reasoner' (R1) only when the subtask genuinely needs planning or multi-step reasoning; it is roughly 5-10x more expensive.\",\n },\n },\n required: [\"task\"],\n },\n fn: async (args: { task?: unknown; system?: unknown; model?: unknown }, ctx) => {\n const task = typeof args.task === \"string\" ? args.task.trim() : \"\";\n if (!task) {\n return JSON.stringify({\n error: \"spawn_subagent requires a non-empty 'task' argument.\",\n });\n }\n const system =\n typeof args.system === \"string\" && args.system.trim().length > 0\n ? args.system.trim()\n : defaultSystem;\n const model =\n typeof args.model === \"string\" && args.model.startsWith(\"deepseek-\")\n ? args.model\n : defaultModel;\n const result = await spawnSubagent({\n client: opts.client,\n parentRegistry,\n system,\n task,\n model,\n maxToolIters,\n maxResultChars,\n sink,\n parentSignal: ctx?.signal,\n });\n return formatSubagentResult(result);\n },\n });\n\n return parentRegistry;\n}\n\n/**\n * Build a child ToolRegistry that copies every tool from `parent` except\n * those whose names are in `exclude`. Plan-mode state propagates so a\n * subagent spawned while the parent is under `/plan` cannot escape it.\n *\n * Exported for tests + library callers who want the same fork behavior\n * for their own nested-loop patterns.\n */\nexport function forkRegistryExcluding(\n parent: ToolRegistry,\n exclude: ReadonlySet<string>,\n): ToolRegistry {\n const child = new ToolRegistry();\n for (const spec of parent.specs()) {\n const name = spec.function.name;\n if (exclude.has(name)) continue;\n const def = parent.get(name);\n if (!def) continue;\n // Re-register copies the public ToolDefinition fields. The child\n // re-runs auto-flatten analysis on its own, which produces an\n // identical flatSchema for the same input — no surprise.\n child.register(def);\n }\n if (parent.planMode) child.setPlanMode(true);\n return child;\n}\n","/**\n * Native shell tool — lets the model run commands inside the sandbox\n * root so it can actually verify its own work (run tests, check git\n * status, inspect a lockfile, etc.). Without this the coding-mode\n * loop is \"write code, hope it works, ask the user to run it\" —\n * defeats the purpose.\n *\n * Safety model:\n * - Commands run with `cwd` pinned to the registered root. No\n * path traversal via the command itself is enforced (users can\n * `cat ../outside.txt`); the trust boundary is the directory\n * you opened Reasonix from.\n * - Commands are matched against a read-only / testing allowlist.\n * Allowlisted commands execute immediately and return stdout +\n * stderr merged. Everything else throws with a clear message —\n * the UI translates that into an `/apply`-style confirm gate so\n * the user sees the exact command before it runs.\n * - Default timeout: 60s. Output cap: matches tool-result budget.\n * - Every command that DOES run is spawned with `shell: false` and\n * a tokenized argv — no string-to-shell interpolation, so the\n * model can't accidentally construct a chained `rm` via quoting.\n *\n * This is intentionally narrower than what Claude Code / Aider ship:\n * we gate more commands behind confirmation by default. Users who\n * trust the model can widen the allowlist by instantiating their\n * own tool registry.\n */\n\nimport { type SpawnOptions, spawn } from \"node:child_process\";\nimport { existsSync, statSync } from \"node:fs\";\nimport * as pathMod from \"node:path\";\nimport type { ToolRegistry } from \"../tools.js\";\n\nexport interface ShellToolsOptions {\n /** Directory to run commands in. Must be an absolute path. */\n rootDir: string;\n /** Seconds before an individual command is killed. Default: 60. */\n timeoutSec?: number;\n /**\n * Per-command stdout+stderr cap in characters. Default: 32_000 to\n * match the tool-result budget.\n */\n maxOutputChars?: number;\n /**\n * Extra command-name prefixes the user explicitly trusts. Added on\n * top of the built-in allowlist. Examples: `[\"my-ci-script\", \"lint\"]`.\n *\n * Accepts either a fixed array (captured once at registration) or a\n * getter called on every dispatch. The getter form is load-bearing:\n * when the TUI's `ShellConfirm` writes a new prefix to config mid-\n * session, the running `run_command` must pick it up immediately —\n * otherwise the same command gets re-prompted until the next launch.\n */\n extraAllowed?: readonly string[] | (() => readonly string[]);\n /**\n * When true, skip the allowlist entirely and auto-run every command.\n * Off by default — this is an escape hatch for non-interactive use\n * (CI, benchmarks) where a human can't be in the loop to confirm.\n */\n allowAll?: boolean;\n}\n\nconst DEFAULT_TIMEOUT_SEC = 60;\nconst DEFAULT_MAX_OUTPUT_CHARS = 32_000;\n\n/**\n * Command prefixes we consider safe to run without asking the user.\n * Rule of thumb: read-only reports, or test runners whose failure mode\n * is \"exit 1 with output.\" Nothing that can rewrite state, escalate,\n * or touch the network.\n */\nconst BUILTIN_ALLOWLIST: ReadonlyArray<string> = [\n // Repo inspection\n \"git status\",\n \"git diff\",\n \"git log\",\n \"git show\",\n \"git blame\",\n \"git branch\",\n \"git remote\",\n \"git rev-parse\",\n \"git config --get\",\n // Filesystem inspection\n \"ls\",\n \"pwd\",\n \"cat\",\n \"head\",\n \"tail\",\n \"wc\",\n \"file\",\n \"tree\",\n \"find\",\n \"grep\",\n \"rg\",\n // Language version probes\n \"node --version\",\n \"node -v\",\n \"npm --version\",\n \"npx --version\",\n \"python --version\",\n \"python3 --version\",\n \"cargo --version\",\n \"go version\",\n \"rustc --version\",\n \"deno --version\",\n \"bun --version\",\n // Test runners (non-destructive by convention)\n \"npm test\",\n \"npm run test\",\n \"npx vitest run\",\n \"npx vitest\",\n \"npx jest\",\n \"pytest\",\n \"python -m pytest\",\n \"cargo test\",\n \"cargo check\",\n \"cargo clippy\",\n \"go test\",\n \"go vet\",\n \"deno test\",\n \"bun test\",\n // Linters / typecheckers (read-only by convention)\n \"npm run lint\",\n \"npm run typecheck\",\n \"npx tsc --noEmit\",\n \"npx biome check\",\n \"npx eslint\",\n \"npx prettier --check\",\n \"ruff\",\n \"mypy\",\n];\n\n/**\n * Tokenize a shell-ish command string into argv. Handles single/double\n * quoting; rejects unclosed quotes. Does NOT expand env vars, globs,\n * backticks, or `$(…)` — the goal is to prevent the model from\n * accidentally (or not) sneaking arbitrary shells past the allowlist\n * via concatenation. Exported for testing.\n */\nexport function tokenizeCommand(cmd: string): string[] {\n const out: string[] = [];\n let cur = \"\";\n let quote: '\"' | \"'\" | null = null;\n for (let i = 0; i < cmd.length; i++) {\n const ch = cmd[i]!;\n if (quote) {\n if (ch === quote) {\n quote = null;\n } else if (ch === \"\\\\\" && quote === '\"' && i + 1 < cmd.length) {\n cur += cmd[++i];\n } else {\n cur += ch;\n }\n continue;\n }\n if (ch === '\"' || ch === \"'\") {\n quote = ch;\n continue;\n }\n if (ch === \" \" || ch === \"\\t\") {\n if (cur.length > 0) {\n out.push(cur);\n cur = \"\";\n }\n continue;\n }\n cur += ch;\n }\n if (quote) throw new Error(`unclosed ${quote} in command`);\n if (cur.length > 0) out.push(cur);\n return out;\n}\n\n/**\n * Return true when `cmd` matches an allowlisted prefix. Exported for\n * testing. Match is on the space-normalized leading tokens so\n * `git status -s ` and `git status` both match `git status`.\n */\nexport function isAllowed(cmd: string, extra: readonly string[] = []): boolean {\n const normalized = cmd.trim().replace(/\\s+/g, \" \");\n const allowlist = [...BUILTIN_ALLOWLIST, ...extra];\n for (const prefix of allowlist) {\n if (normalized === prefix) return true;\n if (normalized.startsWith(`${prefix} `)) return true;\n }\n return false;\n}\n\nexport interface RunCommandResult {\n exitCode: number | null;\n /** Combined stdout+stderr, truncated to `maxOutputChars` with a marker. */\n output: string;\n /** True when the process was killed for exceeding `timeoutSec`. */\n timedOut: boolean;\n}\n\nexport async function runCommand(\n cmd: string,\n opts: {\n cwd: string;\n timeoutSec?: number;\n maxOutputChars?: number;\n signal?: AbortSignal;\n },\n): Promise<RunCommandResult> {\n const argv = tokenizeCommand(cmd);\n if (argv.length === 0) throw new Error(\"run_command: empty command\");\n const timeoutMs = (opts.timeoutSec ?? DEFAULT_TIMEOUT_SEC) * 1000;\n const maxChars = opts.maxOutputChars ?? DEFAULT_MAX_OUTPUT_CHARS;\n\n const spawnOpts: SpawnOptions = {\n cwd: opts.cwd,\n shell: false, // no shell-expansion — see header comment\n windowsHide: true,\n env: process.env,\n };\n\n // Windows: two layered fixes on top of shell:false —\n // 1. Resolve bare command names via PATH × PATHEXT (CreateProcess\n // ignores PATHEXT, so `npm` alone misses `npm.cmd`).\n // 2. Node 21.7.3+ (CVE-2024-27980) refuses to spawn `.cmd`/`.bat`\n // directly even with shell:false and safe args — throws\n // EINVAL at invocation time. Wrap those via `cmd.exe /d /s /c`\n // with verbatim args + manual quoting, so shell metacharacters\n // in arguments stay literal.\n // Unix path is unchanged.\n const { bin, args, spawnOverrides } = prepareSpawn(argv);\n const effectiveSpawnOpts = { ...spawnOpts, ...spawnOverrides };\n\n return await new Promise<RunCommandResult>((resolve, reject) => {\n let child: import(\"node:child_process\").ChildProcess;\n try {\n child = spawn(bin, args, effectiveSpawnOpts);\n } catch (err) {\n reject(err);\n return;\n }\n let buf = \"\";\n let timedOut = false;\n const killTimer = setTimeout(() => {\n timedOut = true;\n child.kill(\"SIGKILL\");\n }, timeoutMs);\n const onAbort = () => child.kill(\"SIGKILL\");\n opts.signal?.addEventListener(\"abort\", onAbort, { once: true });\n\n const onData = (chunk: Buffer | string) => {\n buf += chunk.toString();\n // Soft cap: we let the process keep running (killing early could\n // hide a real failure), but we stop growing the buffer past 2×\n // the cap so a chatty test can't OOM us.\n if (buf.length > maxChars * 2) buf = `${buf.slice(0, maxChars * 2)}`;\n };\n child.stdout?.on(\"data\", onData);\n child.stderr?.on(\"data\", onData);\n child.on(\"error\", (err) => {\n clearTimeout(killTimer);\n opts.signal?.removeEventListener(\"abort\", onAbort);\n reject(err);\n });\n child.on(\"close\", (code) => {\n clearTimeout(killTimer);\n opts.signal?.removeEventListener(\"abort\", onAbort);\n const output =\n buf.length > maxChars\n ? `${buf.slice(0, maxChars)}\\n\\n[… truncated ${buf.length - maxChars} chars …]`\n : buf;\n resolve({ exitCode: code, output, timedOut });\n });\n });\n}\n\n/**\n * Test/override hooks for {@link resolveExecutable}. Omitting any field\n * falls through to the real process globals — the runtime call path\n * uses defaults; tests inject `platform` + `env` + `isFile` to exercise\n * Windows-specific lookup from a Linux CI runner without touching\n * actual fs.\n */\nexport interface ResolveExecutableOptions {\n platform?: NodeJS.Platform;\n env?: { PATH?: string; PATHEXT?: string };\n /** Predicate swapped in by tests to avoid creating real files. */\n isFile?: (path: string) => boolean;\n /** Path.join used for the lookup. Defaults to Windows semantics on Windows. */\n pathDelimiter?: string;\n}\n\n/**\n * Resolve a bare command name (e.g. `npm`) to its on-disk path via\n * PATH × PATHEXT on Windows. Returns the input unchanged on non-Windows\n * platforms, when the input is already a path (contains `/`, `\\`, or is\n * absolute), or when no match is found in PATH × PATHEXT (caller gets a\n * natural ENOENT from spawn, which surfaces cleanly).\n *\n * Why this exists: `child_process.spawn` with `shell: false` invokes\n * Windows `CreateProcess`, which does not honor `PATHEXT` and does not\n * search for `.cmd` / `.bat` wrappers. Node-ecosystem tools ship as\n * `npm.cmd`, `npx.cmd`, `yarn.cmd`, etc., so a bare `npm` fails with\n * ENOENT under `shell: false`. Flipping to `shell: true` would work\n * but reintroduces shell-expansion (pipes, redirects, chained cmds)\n * that the tool was explicitly designed to forbid. This resolver\n * threads the needle.\n */\nexport function resolveExecutable(cmd: string, opts: ResolveExecutableOptions = {}): string {\n const platform = opts.platform ?? process.platform;\n if (platform !== \"win32\") return cmd;\n if (!cmd) return cmd;\n // Already a path fragment — spawn handles these natively.\n if (cmd.includes(\"/\") || cmd.includes(\"\\\\\") || pathMod.isAbsolute(cmd)) return cmd;\n // If the model wrote `npm.cmd` explicitly, respect that verbatim.\n if (pathMod.extname(cmd)) return cmd;\n\n const env = opts.env ?? process.env;\n const pathExt = (env.PATHEXT ?? \".COM;.EXE;.BAT;.CMD\")\n .split(\";\")\n .map((e) => e.trim())\n .filter(Boolean);\n const delimiter = opts.pathDelimiter ?? (platform === \"win32\" ? \";\" : pathMod.delimiter);\n const pathDirs = (env.PATH ?? \"\").split(delimiter).filter(Boolean);\n const isFile = opts.isFile ?? defaultIsFile;\n\n for (const dir of pathDirs) {\n for (const ext of pathExt) {\n // Force win32 join so CI tests that pass `platform: \"win32\"`\n // from a Linux runner get backslash-joined paths; the real-\n // Windows runtime path lands here too and gets the correct\n // separator regardless of where pathMod defaults.\n const full = pathMod.win32.join(dir, cmd + ext);\n if (isFile(full)) return full;\n }\n }\n return cmd;\n}\n\nfunction defaultIsFile(full: string): boolean {\n try {\n return existsSync(full) && statSync(full).isFile();\n } catch {\n return false;\n }\n}\n\n/**\n * Prepare `(bin, args, spawnOpts)` for the runCommand spawn call,\n * applying Windows-specific workarounds for (a) PATHEXT lookup and\n * (b) the CVE-2024-27980 prohibition on direct `.cmd`/`.bat` spawns.\n *\n * Exported so tests can assert the transformation without booting an\n * actual child process.\n */\nexport function prepareSpawn(\n argv: readonly string[],\n opts: ResolveExecutableOptions = {},\n): { bin: string; args: string[]; spawnOverrides: SpawnOptions } {\n const head = argv[0] ?? \"\";\n const tail = argv.slice(1);\n const platform = opts.platform ?? process.platform;\n const resolved = resolveExecutable(head, opts);\n\n if (platform !== \"win32\") {\n return { bin: resolved, args: [...tail], spawnOverrides: {} };\n }\n\n // `.cmd` / `.bat` wrappers require cmd.exe on post-CVE Node.\n if (/\\.(cmd|bat)$/i.test(resolved)) {\n const cmdline = [resolved, ...tail].map(quoteForCmdExe).join(\" \");\n return {\n bin: \"cmd.exe\",\n args: [\"/d\", \"/s\", \"/c\", withUtf8Codepage(cmdline)],\n // windowsVerbatimArguments prevents Node from re-quoting the /c\n // payload — we've already composed an exact cmd.exe command\n // line. Without this Node wraps our already-quoted string in\n // another round of quotes and cmd.exe can't parse it.\n spawnOverrides: { windowsVerbatimArguments: true },\n };\n }\n\n // Bare command names that PATH × PATHEXT couldn't resolve to an\n // on-disk file — these are almost always cmd.exe built-ins (`dir`,\n // `echo`, `type`, `ver`, `vol`, `where`, `help`, …) which don't\n // exist as standalone executables. Direct spawn crashes with ENOENT;\n // routing through cmd.exe lets the built-in resolve, and if it's\n // genuinely unknown the user gets the standard \"'foo' is not\n // recognized\" message instead of a raw spawn failure.\n if (isBareWindowsName(resolved) && resolved === head) {\n const cmdline = [head, ...tail].map(quoteForCmdExe).join(\" \");\n return {\n bin: \"cmd.exe\",\n args: [\"/d\", \"/s\", \"/c\", withUtf8Codepage(cmdline)],\n spawnOverrides: { windowsVerbatimArguments: true },\n };\n }\n\n // PowerShell variants: chcp 65001 doesn't help here because PowerShell\n // sets its own [Console]::OutputEncoding at startup — usually system\n // codepage (CP936/CP932/CP949 on CJK Windows) or UTF-16. The result\n // is mojibake when our `chunk.toString()` UTF-8-decodes its stdout.\n // Inject a UTF-8 setup prelude into the `-Command` (or `-c`) arg so\n // any output produced thereafter is UTF-8.\n if (isPowerShellExe(resolved)) {\n const patched = injectPowerShellUtf8(tail);\n if (patched) {\n return { bin: resolved, args: patched, spawnOverrides: {} };\n }\n }\n\n return { bin: resolved, args: [...tail], spawnOverrides: {} };\n}\n\n/** Resolved bin path looks like Windows PowerShell or PowerShell Core. */\nfunction isPowerShellExe(resolved: string): boolean {\n return /(?:^|[\\\\/])(?:powershell|pwsh)(?:\\.exe)?$/i.test(resolved);\n}\n\n/**\n * Locate `-Command` / `-c` in `args` and prepend the UTF-8 setup prelude\n * to its value. Returns the patched args, or `null` when no `-Command`\n * arg is present (in which case we leave the invocation untouched —\n * inline-expression and script-file modes have their own conventions\n * we don't want to silently rewrite).\n *\n * Why not always wrap: PowerShell's quoting semantics are finicky enough\n * that adding a prelude to a script file invocation could break it.\n * `-Command` is the case the model actually uses, and where mojibake\n * matters; targeting just it keeps the blast radius small.\n *\n * Exported for tests.\n */\nexport function injectPowerShellUtf8(args: readonly string[]): string[] | null {\n const prelude =\n \"[Console]::OutputEncoding=[System.Text.Encoding]::UTF8;$OutputEncoding=[System.Text.Encoding]::UTF8;\";\n for (let i = 0; i < args.length; i++) {\n const a = args[i] ?? \"\";\n if (/^-(?:Command|c)$/i.test(a) && i + 1 < args.length) {\n const out = [...args];\n out[i + 1] = `${prelude}${args[i + 1] ?? \"\"}`;\n return out;\n }\n }\n return null;\n}\n\n/**\n * Prefix a cmd.exe command line with `chcp 65001 >nul &` so output\n * (from cmd.exe and any child it spawns) is UTF-8-encoded. Without\n * this, on Chinese / Japanese / Korean Windows, `dir`, `findstr`,\n * `where`, etc. emit text in the system codepage (CP936, CP932,\n * CP949, …) and `chunk.toString()` — which decodes as UTF-8 — produces\n * garbled mojibake the model then sees as poisoned input on the next\n * turn.\n *\n * Scope: chcp affects ONLY this cmd.exe instance, which exits after\n * `/c`. No global console state changes. Single `&` (not `&&`) so the\n * command still runs even on the rare Windows builds where chcp\n * itself returns a non-zero exit (Win7 quirks; harmless on Win10+).\n *\n * Exported so tests can verify the wrapping shape.\n */\nexport function withUtf8Codepage(cmdline: string): string {\n return `chcp 65001 >nul & ${cmdline}`;\n}\n\n/**\n * True when `s` looks like a bare executable name — no path separator,\n * no drive letter, no extension. Such names on Windows, when absent\n * from PATH × PATHEXT, are almost always cmd.exe built-ins.\n */\nfunction isBareWindowsName(s: string): boolean {\n if (!s) return false;\n if (s.includes(\"/\") || s.includes(\"\\\\\")) return false;\n if (pathMod.isAbsolute(s)) return false;\n if (pathMod.extname(s)) return false;\n return true;\n}\n\n/**\n * Quote an argument so cmd.exe parses it back as a single token. We\n * always wrap in double quotes when the arg contains whitespace or\n * any cmd.exe metacharacter, doubling embedded quotes per cmd.exe's\n * `\"\"` escape rule. Bare alphanumeric args pass through unquoted for\n * readability in logs.\n *\n * Exported for test coverage of the quoting semantics.\n */\nexport function quoteForCmdExe(arg: string): string {\n if (arg === \"\") return '\"\"';\n if (!/[\\s\"&|<>^%(),;!]/.test(arg)) return arg;\n return `\"${arg.replace(/\"/g, '\"\"')}\"`;\n}\n\n/** Error thrown by `run_command` when the command isn't allowlisted. */\nexport class NeedsConfirmationError extends Error {\n readonly command: string;\n constructor(command: string) {\n super(\n `run_command: \"${command}\" needs the user's approval before it runs. STOP calling tools now — the TUI has already prompted the user to press y (run) or n (deny). Wait for their next message; it will either be the command's output (if they approved) or an instruction to continue without it (if they denied). Don't retry the command or call other shell commands in the meantime.`,\n );\n this.name = \"NeedsConfirmationError\";\n this.command = command;\n }\n}\n\nexport function registerShellTools(registry: ToolRegistry, opts: ShellToolsOptions): ToolRegistry {\n const rootDir = pathMod.resolve(opts.rootDir);\n const timeoutSec = opts.timeoutSec ?? DEFAULT_TIMEOUT_SEC;\n const maxOutputChars = opts.maxOutputChars ?? DEFAULT_MAX_OUTPUT_CHARS;\n // Resolved on every dispatch so newly-persisted \"always allow\"\n // prefixes take effect inside the session that added them, not just\n // on the next launch. Static arrays are wrapped into a constant\n // getter so the call site below is uniform.\n const getExtraAllowed: () => readonly string[] =\n typeof opts.extraAllowed === \"function\"\n ? opts.extraAllowed\n : (() => {\n const snapshot = opts.extraAllowed ?? [];\n return () => snapshot;\n })();\n const allowAll = opts.allowAll ?? false;\n\n registry.register({\n name: \"run_command\",\n description:\n \"Run a shell command in the project root and return its combined stdout+stderr. Common read-only inspection and test/lint/typecheck commands run immediately; anything that could mutate state, install dependencies, or touch the network is refused until the user confirms it in the TUI. Prefer this over asking the user to run a command manually — after edits, run the project's tests to verify.\",\n // Plan-mode gate: allow allowlisted commands through (git status,\n // cargo check, ls, grep …) so the model can actually investigate\n // during planning. Anything that would otherwise trigger a\n // confirmation prompt is treated as \"not read-only\" and bounced.\n readOnlyCheck: (args: { command?: unknown }) => {\n if (allowAll) return true;\n const cmd = typeof args?.command === \"string\" ? args.command.trim() : \"\";\n if (!cmd) return false;\n return isAllowed(cmd, getExtraAllowed());\n },\n parameters: {\n type: \"object\",\n properties: {\n command: {\n type: \"string\",\n description:\n \"Full command line. Tokenized with POSIX-ish quoting; no shell expansion, no pipes, no redirects.\",\n },\n timeoutSec: {\n type: \"integer\",\n description: `Override the default ${timeoutSec}s timeout for a single command.`,\n },\n },\n required: [\"command\"],\n },\n fn: async (args: { command: string; timeoutSec?: number }, ctx) => {\n const cmd = args.command.trim();\n if (!cmd) throw new Error(\"run_command: empty command\");\n if (!allowAll && !isAllowed(cmd, getExtraAllowed())) {\n throw new NeedsConfirmationError(cmd);\n }\n const effectiveTimeout = Math.max(1, Math.min(600, args.timeoutSec ?? timeoutSec));\n const result = await runCommand(cmd, {\n cwd: rootDir,\n timeoutSec: effectiveTimeout,\n maxOutputChars,\n signal: ctx?.signal,\n });\n return formatCommandResult(cmd, result);\n },\n });\n\n return registry;\n}\n\nexport function formatCommandResult(cmd: string, r: RunCommandResult): string {\n const header = r.timedOut\n ? `$ ${cmd}\\n[killed after timeout]`\n : `$ ${cmd}\\n[exit ${r.exitCode ?? \"?\"}]`;\n return r.output ? `${header}\\n${r.output}` : header;\n}\n","/**\n * Built-in web search + fetch tools.\n *\n * - `web_search(query, topK?)` — Mojeek's public search page. No API\n * key, no signup. We originally shipped this backed by DuckDuckGo's\n * HTML endpoint, but DDG started serving anti-bot interstitials\n * (HTTP 202 with a challenge page) for every unauthenticated POST.\n * Mojeek runs its own independent index, is bot-friendly, and\n * returns parseable HTML.\n * - `web_fetch(url)` — HTTP GET + naïve HTML-to-text extraction.\n *\n * Both are registered by default on `reasonix chat` / `reasonix code`;\n * set `search: false` in config (or `REASONIX_SEARCH=off`) to turn\n * them off. The model decides when to call them based on the query —\n * no slash command required.\n */\n\nimport type { ToolRegistry } from \"../tools.js\";\n\nexport interface SearchResult {\n title: string;\n url: string;\n snippet: string;\n}\n\nexport interface PageContent {\n url: string;\n title?: string;\n text: string;\n /** True when the extracted text was clipped to fit the cap. */\n truncated: boolean;\n}\n\nexport interface WebFetchOptions {\n /** Max bytes of extracted text. Defaults to 32_000 to match tool-result cap. */\n maxChars?: number;\n /** Timeout in ms. Defaults to 15_000. */\n timeoutMs?: number;\n signal?: AbortSignal;\n}\n\nexport interface WebSearchOptions {\n topK?: number;\n signal?: AbortSignal;\n}\n\nconst DEFAULT_FETCH_MAX_CHARS = 32_000;\nconst DEFAULT_FETCH_TIMEOUT_MS = 15_000;\nconst DEFAULT_TOPK = 5;\n// Real-browser UA. Servers like Mojeek are bot-friendly but still gate\n// obvious scraper UAs; a stock Chrome string avoids the fast-path block.\nconst USER_AGENT =\n \"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36\";\nconst MOJEEK_ENDPOINT = \"https://www.mojeek.com/search\";\n\n/**\n * Search the public web via Mojeek. Returns up to `topK` ranked\n * results with title, url, snippet.\n *\n * Mojeek is an independent index (not a Google/Bing front-end) which\n * means coverage on niche or very recent topics can be thinner, but\n * it's reliable from scripts and doesn't gate on cookies or sessions.\n * If the response has 0 results we distinguish \"truly empty\" from\n * \"layout changed or blocked\" so the caller isn't left guessing.\n */\nexport async function webSearch(\n query: string,\n opts: WebSearchOptions = {},\n): Promise<SearchResult[]> {\n const topK = Math.max(1, Math.min(10, opts.topK ?? DEFAULT_TOPK));\n const resp = await fetch(`${MOJEEK_ENDPOINT}?q=${encodeURIComponent(query)}`, {\n headers: {\n \"User-Agent\": USER_AGENT,\n Accept: \"text/html,application/xhtml+xml,application/xml;q=0.9\",\n \"Accept-Language\": \"en-US,en;q=0.9\",\n },\n signal: opts.signal,\n redirect: \"follow\",\n });\n if (!resp.ok) throw new Error(`web_search ${resp.status}`);\n const html = await resp.text();\n const results = parseMojeekResults(html).slice(0, topK);\n if (results.length === 0) {\n if (/no results found|did not match any documents/i.test(html)) return [];\n if (/captcha|verify you are human|access denied|forbidden/i.test(html)) {\n throw new Error(\"web_search: Mojeek anti-bot page — rate-limited or blocked\");\n }\n throw new Error(\n `web_search: 0 results but response doesn't look like a real empty page (${html.length} chars, first 120: ${html.slice(0, 120).replace(/\\s+/g, \" \")})`,\n );\n }\n return results;\n}\n\n/**\n * Extract results from a Mojeek search page.\n *\n * Mojeek's stable shape (as of April 2026):\n * <a … class=\"ob\" href=\"URL\"> … breadcrumb … </a>\n * <h2><a class=\"title\" href=\"URL\">Title</a></h2>\n * <p class=\"s\">snippet text …</p>\n *\n * We do two tolerant passes — title anchors, then snippet paragraphs —\n * and pair them positionally. Attribute order inside a tag varies\n * between versions, so each pass captures the whole element and we\n * re-extract href / inner text with a second regex. Exported for\n * unit testing against a fixture.\n */\nexport function parseMojeekResults(html: string): SearchResult[] {\n const titles: string[] = [];\n const titleAnchorRe = /<a\\b[^>]*\\bclass=\"title\"[^>]*>[\\s\\S]*?<\\/a>/g;\n let m: RegExpExecArray | null;\n while (true) {\n m = titleAnchorRe.exec(html);\n if (m === null) break;\n titles.push(m[0]);\n }\n\n const snippets: string[] = [];\n const snippetRe = /<p\\b[^>]*\\bclass=\"s\"[^>]*>([\\s\\S]*?)<\\/p>/g;\n while (true) {\n m = snippetRe.exec(html);\n if (m === null) break;\n snippets.push(m[1] ?? \"\");\n }\n\n const hrefRe = /href=\"([^\"]+)\"/;\n const innerRe = /<a\\b[^>]*>([\\s\\S]*?)<\\/a>/;\n const results: SearchResult[] = [];\n for (let i = 0; i < titles.length; i++) {\n const anchor = titles[i]!;\n const hrefMatch = anchor.match(hrefRe);\n const innerMatch = anchor.match(innerRe);\n if (!hrefMatch?.[1]) continue;\n results.push({\n title: decodeHtmlEntities(stripHtml(innerMatch?.[1] ?? \"\")).trim(),\n url: hrefMatch[1],\n snippet: decodeHtmlEntities(stripHtml(snippets[i] ?? \"\"))\n .replace(/\\s+/g, \" \")\n .trim(),\n });\n }\n return results;\n}\n\n/**\n * Download a URL, strip HTML down to readable text, return it. Times\n * out at 15s, caps extracted text at 32k chars to fit the tool-result\n * budget.\n */\nexport async function webFetch(url: string, opts: WebFetchOptions = {}): Promise<PageContent> {\n const maxChars = opts.maxChars ?? DEFAULT_FETCH_MAX_CHARS;\n const timeoutMs = opts.timeoutMs ?? DEFAULT_FETCH_TIMEOUT_MS;\n const ctl = new AbortController();\n const timer = setTimeout(() => ctl.abort(), timeoutMs);\n // Forward the caller's abort too so an Esc during a long fetch is respected.\n const cancel = () => ctl.abort();\n opts.signal?.addEventListener(\"abort\", cancel, { once: true });\n let resp: Response;\n try {\n resp = await fetch(url, {\n headers: { \"User-Agent\": USER_AGENT, Accept: \"text/html,text/plain,*/*\" },\n signal: ctl.signal,\n redirect: \"follow\",\n });\n } finally {\n clearTimeout(timer);\n opts.signal?.removeEventListener(\"abort\", cancel);\n }\n if (!resp.ok) throw new Error(`web_fetch ${resp.status} for ${url}`);\n const contentType = resp.headers.get(\"content-type\") ?? \"\";\n const raw = await resp.text();\n const title = extractTitle(raw);\n const text = contentType.includes(\"text/html\") ? htmlToText(raw) : raw;\n const truncated = text.length > maxChars;\n const finalText = truncated\n ? `${text.slice(0, maxChars)}\\n\\n[… truncated ${text.length - maxChars} chars …]`\n : text;\n return { url, title, text: finalText, truncated };\n}\n\n/**\n * Strip HTML to readable text. Removes scripts/styles/nav/footer/aside\n * blocks first, then tags, then collapses whitespace. Not a Readability\n * clone — purpose-built to keep the extracted text small enough for the\n * tool-result budget while preserving paragraph breaks.\n */\nexport function htmlToText(html: string): string {\n let s = html;\n s = s.replace(/<script[\\s\\S]*?<\\/script>/gi, \"\");\n s = s.replace(/<style[\\s\\S]*?<\\/style>/gi, \"\");\n s = s.replace(/<noscript[\\s\\S]*?<\\/noscript>/gi, \"\");\n s = s.replace(/<nav[\\s\\S]*?<\\/nav>/gi, \"\");\n s = s.replace(/<footer[\\s\\S]*?<\\/footer>/gi, \"\");\n s = s.replace(/<aside[\\s\\S]*?<\\/aside>/gi, \"\");\n s = s.replace(/<svg[\\s\\S]*?<\\/svg>/gi, \"\");\n // Preserve paragraph breaks by turning common block tags into newlines.\n s = s.replace(/<\\/?(p|div|br|h[1-6]|li|tr|section|article)\\b[^>]*>/gi, \"\\n\");\n s = s.replace(/<[^>]+>/g, \"\");\n s = decodeHtmlEntities(s);\n s = s.replace(/[ \\t]+/g, \" \");\n s = s.replace(/\\n[ \\t]+/g, \"\\n\");\n s = s.replace(/\\n{3,}/g, \"\\n\\n\");\n return s.trim();\n}\n\nfunction stripHtml(s: string): string {\n return s.replace(/<[^>]+>/g, \"\");\n}\n\nfunction decodeHtmlEntities(s: string): string {\n return s\n .replace(/ /g, \" \")\n .replace(/&/g, \"&\")\n .replace(/</g, \"<\")\n .replace(/>/g, \">\")\n .replace(/"/g, '\"')\n .replace(/'/g, \"'\");\n}\n\nfunction extractTitle(html: string): string | undefined {\n const m = html.match(/<title[^>]*>([\\s\\S]*?)<\\/title>/i);\n if (!m?.[1]) return undefined;\n return m[1].replace(/\\s+/g, \" \").trim() || undefined;\n}\n\nexport interface WebToolsOptions {\n /** Default top-K for `web_search` when the model doesn't specify. */\n defaultTopK?: number;\n /** Byte cap for `web_fetch` extracted text. */\n maxFetchChars?: number;\n}\n\n/**\n * Register `web_search` + `web_fetch` on a ToolRegistry. The model\n * invokes them automatically when a question needs current info —\n * no slash command from the user is required.\n */\nexport function registerWebTools(registry: ToolRegistry, opts: WebToolsOptions = {}): ToolRegistry {\n const defaultTopK = opts.defaultTopK ?? DEFAULT_TOPK;\n const maxFetchChars = opts.maxFetchChars ?? DEFAULT_FETCH_MAX_CHARS;\n\n registry.register({\n name: \"web_search\",\n description:\n \"Search the public web. Returns ranked results with title, url, and snippet. Use this when the question needs information more current than your training data, when you're unsure of a factual detail, or when the user asks about a specific webpage/library/release you haven't seen.\",\n readOnly: true,\n parameters: {\n type: \"object\",\n properties: {\n query: { type: \"string\", description: \"Natural-language search query.\" },\n topK: {\n type: \"integer\",\n description: `Number of results to return (1..10). Default ${defaultTopK}.`,\n },\n },\n required: [\"query\"],\n },\n fn: async (args: { query: string; topK?: number }, ctx) => {\n const results = await webSearch(args.query, {\n topK: args.topK ?? defaultTopK,\n signal: ctx?.signal,\n });\n return formatSearchResults(args.query, results);\n },\n });\n\n registry.register({\n name: \"web_fetch\",\n description:\n \"Download a URL and return its visible text content (HTML pages get scripts/styles/nav stripped). Truncated at the tool-result cap. Use after web_search when a snippet isn't enough.\",\n readOnly: true,\n parameters: {\n type: \"object\",\n properties: {\n url: { type: \"string\", description: \"Absolute http:// or https:// URL.\" },\n },\n required: [\"url\"],\n },\n fn: async (args: { url: string }, ctx) => {\n if (!/^https?:\\/\\//i.test(args.url)) {\n throw new Error(\"web_fetch: url must start with http:// or https://\");\n }\n const page = await webFetch(args.url, { maxChars: maxFetchChars, signal: ctx?.signal });\n const header = page.title ? `${page.title}\\n${page.url}` : page.url;\n return `${header}\\n\\n${page.text}`;\n },\n });\n\n return registry;\n}\n\nexport function formatSearchResults(query: string, results: SearchResult[]): string {\n const lines: string[] = [`query: ${query}`, `\\nresults (${results.length}):`];\n results.forEach((r, i) => {\n lines.push(`\\n${i + 1}. ${r.title}`);\n lines.push(` ${r.url}`);\n if (r.snippet) lines.push(` ${r.snippet}`);\n });\n return lines.join(\"\\n\");\n}\n","import { readFileSync } from \"node:fs\";\nimport { resolve } from \"node:path\";\n\n/**\n * Minimal `.env` loader; no dependency on dotenv.\n *\n * Reads KEY=VALUE lines and populates `process.env` for keys not already set.\n * Silently no-ops if the file is missing. Safe to call from library entry\n * points, CLI commands, examples, and benchmark runners.\n */\nexport function loadDotenv(path = \".env\"): void {\n let raw: string;\n try {\n raw = readFileSync(resolve(process.cwd(), path), \"utf8\");\n } catch {\n return;\n }\n for (const line of raw.split(/\\r?\\n/)) {\n const trimmed = line.trim();\n if (!trimmed || trimmed.startsWith(\"#\")) continue;\n const eq = trimmed.indexOf(\"=\");\n if (eq === -1) continue;\n const key = trimmed.slice(0, eq).trim();\n let value = trimmed.slice(eq + 1).trim();\n if (\n (value.startsWith('\"') && value.endsWith('\"')) ||\n (value.startsWith(\"'\") && value.endsWith(\"'\"))\n ) {\n value = value.slice(1, -1);\n }\n if (process.env[key] === undefined) process.env[key] = value;\n }\n}\n","/**\n * Transcript format — the canonical \"audit log\" of a Reasonix session.\n *\n * Design split:\n * - Session file (`~/.reasonix/sessions/<name>.jsonl`) stores only the\n * `ChatMessage`s the model needs to resume. See session.ts.\n * - Transcript file (this module) stores every LoopEvent with usage, cost,\n * model, and prefix fingerprint attached where available — enough for\n * replay and diff to reconstruct economics.\n *\n * The two are different contracts: sessions are the user's *memory*;\n * transcripts are the *receipts*. Don't conflate them.\n *\n * Backward compatibility: all fields beyond {ts, turn, role, content} are\n * optional on read. A v0.1 transcript (pre-usage) still parses and renders\n * — it just shows cost/cache as n/a.\n */\n\nimport { type WriteStream, createWriteStream, readFileSync } from \"node:fs\";\nimport type { TypedPlanState } from \"./harvest.js\";\nimport type { LoopEvent } from \"./loop.js\";\nimport type { RawUsage } from \"./types.js\";\n\nexport interface TranscriptRecord {\n /** ISO-8601 timestamp at emit time. */\n ts: string;\n /** 1-based turn number within the session. */\n turn: number;\n /** LoopEvent role — \"assistant_delta\" | \"assistant_final\" | \"tool\" | \"done\" | ... */\n role: string;\n /** For assistant events, the final (or delta) text; for tool events, the tool result. */\n content: string;\n /** Tool name (role === \"tool\"). */\n tool?: string;\n /** JSON-string args the model sent for a tool call (role === \"tool\"). Persisted so diff can explain *why* two runs made different calls. */\n args?: string;\n /** DeepSeek token-usage snapshot (role === \"assistant_final\"). */\n usage?: RawUsage;\n /** USD cost of this turn (role === \"assistant_final\"). */\n cost?: number;\n /** Model id that produced this turn. */\n model?: string;\n /**\n * The ImmutablePrefix fingerprint at this turn. Lets diff prove two runs\n * share a prefix — i.e. any cache-hit delta is attributable to log\n * stability, not to a different system prompt.\n */\n prefixHash?: string;\n /**\n * Structured plan state extracted by the Pillar 2 harvester. Present on\n * assistant_final records when harvest was enabled and produced non-empty\n * state. Omitted entirely when harvest is off or produced nothing —\n * absence means \"no data\", not \"empty plan\".\n */\n planState?: TypedPlanState;\n /** Optional error message (role === \"error\"). */\n error?: string;\n}\n\nexport interface TranscriptMeta {\n /**\n * Optional metadata written as the first line of a transcript. Lets\n * downstream tooling know what it's reading without guessing.\n * Recognized by a special role \"_meta\".\n */\n version: 1;\n source: string; // e.g. \"reasonix chat\", \"bench/baseline\", \"bench/reasonix\"\n model?: string;\n task?: string;\n mode?: string;\n repeat?: number;\n startedAt: string;\n}\n\ninterface MetaLine {\n role: \"_meta\";\n meta: TranscriptMeta;\n}\n\nexport interface ReadTranscriptResult {\n meta: TranscriptMeta | null;\n records: TranscriptRecord[];\n}\n\n/**\n * Build a TranscriptRecord from a LoopEvent. Extra fields (model,\n * prefixHash) that the LoopEvent doesn't carry are passed in separately\n * because they're session-level, not event-level.\n */\nexport function recordFromLoopEvent(\n ev: LoopEvent,\n extra: { model: string; prefixHash: string },\n): TranscriptRecord {\n const rec: TranscriptRecord = {\n ts: new Date().toISOString(),\n turn: ev.turn,\n role: ev.role,\n content: ev.content,\n };\n if (ev.toolName !== undefined) rec.tool = ev.toolName;\n if (ev.toolArgs !== undefined) rec.args = ev.toolArgs;\n if (ev.error !== undefined) rec.error = ev.error;\n // Only persist non-empty plan state — empty harvest output is indistinguishable\n // from \"harvest was off\" for replay purposes, and saves transcript bytes.\n if (ev.planState && !isPlanStateEmptyShape(ev.planState)) {\n rec.planState = {\n subgoals: [...ev.planState.subgoals],\n hypotheses: [...ev.planState.hypotheses],\n uncertainties: [...ev.planState.uncertainties],\n rejectedPaths: [...ev.planState.rejectedPaths],\n };\n }\n if (ev.stats) {\n rec.usage = {\n prompt_tokens: ev.stats.usage.promptTokens,\n completion_tokens: ev.stats.usage.completionTokens,\n total_tokens: ev.stats.usage.totalTokens,\n prompt_cache_hit_tokens: ev.stats.usage.promptCacheHitTokens,\n prompt_cache_miss_tokens: ev.stats.usage.promptCacheMissTokens,\n };\n rec.cost = ev.stats.cost;\n rec.model = ev.stats.model;\n rec.prefixHash = extra.prefixHash;\n } else if (ev.role === \"assistant_final\") {\n // assistant_final without stats (shouldn't happen in the live loop but\n // might in test fixtures) — still persist model + prefix for continuity.\n rec.model = extra.model;\n rec.prefixHash = extra.prefixHash;\n }\n return rec;\n}\n\n/**\n * Append a record to an open write stream. Caller owns the stream lifecycle.\n */\nexport function writeRecord(stream: WriteStream, record: TranscriptRecord): void {\n stream.write(`${JSON.stringify(record)}\\n`);\n}\n\n/**\n * Write a _meta line to an open write stream. Call exactly once, at the top.\n */\nexport function writeMeta(stream: WriteStream, meta: TranscriptMeta): void {\n const line: MetaLine = { role: \"_meta\", meta };\n stream.write(`${JSON.stringify(line)}\\n`);\n}\n\n/**\n * Convenience: open a stream, write meta, return stream.\n */\nexport function openTranscriptFile(path: string, meta: TranscriptMeta): WriteStream {\n const stream = createWriteStream(path, { flags: \"a\" });\n writeMeta(stream, meta);\n return stream;\n}\n\n/**\n * Parse a transcript file. Returns meta (if the first line is a _meta record)\n * and the full record list.\n *\n * Robustness contract:\n * - Empty lines are skipped.\n * - Malformed JSON lines are skipped silently (do not crash on partial\n * files — live chats may be mid-write).\n * - Records missing optional fields still parse — they're just rendered\n * with n/a where the optional value would go.\n */\nexport function readTranscript(path: string): ReadTranscriptResult {\n const raw = readFileSync(path, \"utf8\");\n return parseTranscript(raw);\n}\n\nfunction isPlanStateEmptyShape(s: TypedPlanState): boolean {\n return (\n s.subgoals.length === 0 &&\n s.hypotheses.length === 0 &&\n s.uncertainties.length === 0 &&\n s.rejectedPaths.length === 0\n );\n}\n\nexport function parseTranscript(raw: string): ReadTranscriptResult {\n const out: ReadTranscriptResult = { meta: null, records: [] };\n for (const line of raw.split(/\\r?\\n/)) {\n const trimmed = line.trim();\n if (!trimmed) continue;\n let obj: unknown;\n try {\n obj = JSON.parse(trimmed);\n } catch {\n continue;\n }\n if (!obj || typeof obj !== \"object\") continue;\n const rec = obj as Record<string, unknown>;\n if (rec.role === \"_meta\" && rec.meta && typeof rec.meta === \"object\") {\n out.meta = rec.meta as TranscriptMeta;\n continue;\n }\n if (\n typeof rec.ts === \"string\" &&\n typeof rec.turn === \"number\" &&\n typeof rec.role === \"string\" &&\n typeof rec.content === \"string\"\n ) {\n out.records.push(rec as unknown as TranscriptRecord);\n }\n }\n return out;\n}\n","/**\n * Replay — reconstruct session economics from a transcript file.\n *\n * Given a transcript written by App.tsx or the bench runner, rebuild a\n * SessionSummary-compatible aggregate (turn count, total cost, cache-hit\n * ratio, vs-Claude estimate) without replaying the LLM calls.\n *\n * The whole point is offline auditing: a reader should be able to reproduce\n * the headline numbers from a transcript alone, without an API key.\n */\n\nimport { Usage } from \"./client.js\";\nimport {\n type SessionSummary,\n type TurnStats,\n claudeEquivalentCost,\n costUsd,\n inputCostUsd,\n outputCostUsd,\n} from \"./telemetry.js\";\nimport { type ReadTranscriptResult, type TranscriptRecord, readTranscript } from \"./transcript.js\";\n\n/**\n * A single turn's worth of records — the unit of navigation in replay TUI.\n * Records are grouped by their `turn` field, preserving file order within\n * each group (so tool events interleave with assistant_final events the\n * way they were actually emitted).\n */\nexport interface TurnPage {\n turn: number;\n records: TranscriptRecord[];\n}\n\n/**\n * Group transcript records into turn-pages. Pages are returned in ascending\n * turn order. Records without a numeric turn (meta lines, malformed) are\n * already filtered by the transcript reader, so this sees clean input.\n */\nexport function groupRecordsByTurn(records: TranscriptRecord[]): TurnPage[] {\n const byTurn = new Map<number, TranscriptRecord[]>();\n for (const rec of records) {\n const list = byTurn.get(rec.turn);\n if (list) list.push(rec);\n else byTurn.set(rec.turn, [rec]);\n }\n return [...byTurn.entries()]\n .sort(([a], [b]) => a - b)\n .map(([turn, records]) => ({ turn, records }));\n}\n\n/**\n * Cumulative replay stats up to and including pages[0..upToIdx]. Returns\n * empty stats if upToIdx < 0. Used by replay TUI's sidebar to show \"stats\n * so far\" as the user scrolls through a transcript.\n */\nexport function computeCumulativeStats(pages: TurnPage[], upToIdx: number): ReplayStats {\n if (upToIdx < 0) return computeReplayStats([]);\n const flat: TranscriptRecord[] = [];\n for (let i = 0; i <= upToIdx && i < pages.length; i++) {\n const records = pages[i]?.records;\n if (records) flat.push(...records);\n }\n return computeReplayStats(flat);\n}\n\nexport interface ReplayStats extends SessionSummary {\n /** Per-turn stats, in turn order. Only assistant_final records contribute. */\n perTurn: TurnStats[];\n /** Unique models that appeared in the transcript's assistant_final records. */\n models: string[];\n /** Unique prefix hashes that appeared. Length > 1 means the prefix churned (cache-hostile). */\n prefixHashes: string[];\n /** Count of user-role records (user turns issued). */\n userTurns: number;\n /** Count of tool-role records (tool calls executed). */\n toolCalls: number;\n /** Count of assistant_final records that carry a non-empty planState (harvest signal). */\n harvestedTurns: number;\n /** Sum of uncertainties across all harvested turns — a proxy for \"how much did R1 hedge?\" */\n totalUncertainties: number;\n /** Sum of subgoals across all harvested turns. */\n totalSubgoals: number;\n}\n\n/**\n * Parse a transcript file and compute replay stats. Throws only on I/O\n * errors; malformed lines inside the file are skipped silently.\n */\nexport function replayFromFile(path: string): { parsed: ReadTranscriptResult; stats: ReplayStats } {\n const parsed = readTranscript(path);\n return { parsed, stats: computeReplayStats(parsed.records) };\n}\n\nexport function computeReplayStats(records: TranscriptRecord[]): ReplayStats {\n const turns: TurnStats[] = [];\n const models = new Set<string>();\n const prefixHashes = new Set<string>();\n let userTurns = 0;\n let toolCalls = 0;\n let harvestedTurns = 0;\n let totalUncertainties = 0;\n let totalSubgoals = 0;\n\n for (const rec of records) {\n if (rec.role === \"user\") userTurns++;\n else if (rec.role === \"tool\") toolCalls++;\n else if (rec.role === \"assistant_final\") {\n if (rec.model) models.add(rec.model);\n if (rec.prefixHash) prefixHashes.add(rec.prefixHash);\n if (rec.planState) {\n harvestedTurns++;\n totalUncertainties += rec.planState.uncertainties.length;\n totalSubgoals += rec.planState.subgoals.length;\n }\n if (rec.usage && rec.model) {\n const u = new Usage(\n rec.usage.prompt_tokens ?? 0,\n rec.usage.completion_tokens ?? 0,\n rec.usage.total_tokens ?? 0,\n rec.usage.prompt_cache_hit_tokens ?? 0,\n rec.usage.prompt_cache_miss_tokens ?? 0,\n );\n turns.push({\n turn: rec.turn,\n model: rec.model,\n usage: u,\n // `rec.cost` wins when present — honors whatever the writer computed\n // even if pricing tables have since changed. Only recompute when\n // the transcript didn't record it (old format).\n cost: rec.cost ?? costUsd(rec.model, u),\n cacheHitRatio: u.cacheHitRatio,\n });\n }\n }\n }\n\n return {\n perTurn: turns,\n models: [...models],\n prefixHashes: [...prefixHashes],\n userTurns,\n toolCalls,\n harvestedTurns,\n totalUncertainties,\n totalSubgoals,\n ...summarizeTurns(turns),\n };\n}\n\nfunction summarizeTurns(turns: TurnStats[]): SessionSummary {\n const totalCost = turns.reduce((s, t) => s + t.cost, 0);\n const totalInput = turns.reduce((s, t) => s + inputCostUsd(t.model, t.usage), 0);\n const totalOutput = turns.reduce((s, t) => s + outputCostUsd(t.model, t.usage), 0);\n const totalClaude = turns.reduce((s, t) => s + claudeEquivalentCost(t.usage), 0);\n let hit = 0;\n let miss = 0;\n for (const t of turns) {\n hit += t.usage.promptCacheHitTokens;\n miss += t.usage.promptCacheMissTokens;\n }\n const cacheHitRatio = hit + miss > 0 ? hit / (hit + miss) : 0;\n const savingsVsClaude = totalClaude > 0 ? 1 - totalCost / totalClaude : 0;\n const lastTurn = turns[turns.length - 1];\n return {\n turns: turns.length,\n totalCostUsd: round(totalCost, 6),\n totalInputCostUsd: round(totalInput, 6),\n totalOutputCostUsd: round(totalOutput, 6),\n claudeEquivalentUsd: round(totalClaude, 6),\n savingsVsClaudePct: round(savingsVsClaude * 100, 2),\n cacheHitRatio: round(cacheHitRatio, 4),\n lastPromptTokens: lastTurn?.usage.promptTokens ?? 0,\n };\n}\n\nfunction round(n: number, digits: number): number {\n const f = 10 ** digits;\n return Math.round(n * f) / f;\n}\n","/**\n * Diff — compare two transcripts and produce a summary + divergence report.\n *\n * Two transcripts are \"comparable\" when they stem from the same task (or\n * the same user prompt). Alignment is by turn number: assistant_final #N\n * in A pairs with assistant_final #N in B. If one side ran more turns, the\n * extras are labeled \"only in A\" / \"only in B\".\n *\n * What we compute:\n * - Aggregate deltas: turns, tool calls, cache hit, cost, token counts\n * - First divergence: the lowest turn where A and B's tool calls or\n * assistant text differ meaningfully\n * - Prefix-stability story: how many unique prefix hashes each side used\n *\n * Non-goals (deliberately):\n * - LLM-judge quality comparison\n * - Per-token delta rendering — not useful at the fidelity we're at\n * - Embedding similarity — Levenshtein ratio is cheap and good enough\n */\n\nimport { type ReplayStats, computeReplayStats } from \"./replay.js\";\nimport type { ReadTranscriptResult, TranscriptRecord } from \"./transcript.js\";\n\nexport interface DiffSide {\n label: string;\n meta: ReadTranscriptResult[\"meta\"];\n records: TranscriptRecord[];\n stats: ReplayStats;\n}\n\nexport interface TurnPair {\n turn: number;\n aAssistant?: TranscriptRecord;\n bAssistant?: TranscriptRecord;\n aTools: TranscriptRecord[];\n bTools: TranscriptRecord[];\n /**\n * Classification of the pair:\n * \"match\" — both sides present, text & tool calls within threshold\n * \"diverge\" — both sides present, but text or tool calls differ\n * \"only_in_a\" — assistant_final in A but not B\n * \"only_in_b\" — assistant_final in B but not A\n */\n kind: \"match\" | \"diverge\" | \"only_in_a\" | \"only_in_b\";\n /** When kind === \"diverge\", a short one-liner pointing at what differs. */\n divergenceNote?: string;\n}\n\nexport interface DiffReport {\n a: DiffSide;\n b: DiffSide;\n pairs: TurnPair[];\n firstDivergenceTurn: number | null;\n}\n\n// ---------- navigation helpers (used by the Ink diff TUI) ----------\n\n/**\n * Find the next pair (strictly after `fromIdx`) whose kind is not \"match\".\n * Returns -1 when no later divergence exists. Used by DiffApp's `n` key.\n */\nexport function findNextDivergence(pairs: TurnPair[], fromIdx: number): number {\n for (let i = fromIdx + 1; i < pairs.length; i++) {\n if (pairs[i]!.kind !== \"match\") return i;\n }\n return -1;\n}\n\n/**\n * Find the previous pair (strictly before `fromIdx`) whose kind is not\n * \"match\". Returns -1 when no earlier divergence exists. Used by\n * DiffApp's `N` / `p` key.\n */\nexport function findPrevDivergence(pairs: TurnPair[], fromIdx: number): number {\n const start = Math.min(fromIdx - 1, pairs.length - 1);\n for (let i = start; i >= 0; i--) {\n if (pairs[i]!.kind !== \"match\") return i;\n }\n return -1;\n}\n\nexport function diffTranscripts(\n a: { label: string; parsed: ReadTranscriptResult },\n b: { label: string; parsed: ReadTranscriptResult },\n): DiffReport {\n const aSide: DiffSide = {\n label: a.label,\n meta: a.parsed.meta,\n records: a.parsed.records,\n stats: computeReplayStats(a.parsed.records),\n };\n const bSide: DiffSide = {\n label: b.label,\n meta: b.parsed.meta,\n records: b.parsed.records,\n stats: computeReplayStats(b.parsed.records),\n };\n\n const aByTurn = groupByTurn(a.parsed.records);\n const bByTurn = groupByTurn(b.parsed.records);\n const turns = [...new Set([...aByTurn.keys(), ...bByTurn.keys()])].sort((x, y) => x - y);\n\n const pairs: TurnPair[] = [];\n let firstDivergenceTurn: number | null = null;\n for (const turn of turns) {\n const aGroup = aByTurn.get(turn) ?? { assistant: undefined, tools: [] };\n const bGroup = bByTurn.get(turn) ?? { assistant: undefined, tools: [] };\n const aAssistant = aGroup.assistant;\n const bAssistant = bGroup.assistant;\n const aTools = aGroup.tools;\n const bTools = bGroup.tools;\n\n let kind: TurnPair[\"kind\"];\n let divergenceNote: string | undefined;\n if (!aAssistant && bAssistant) kind = \"only_in_b\";\n else if (aAssistant && !bAssistant) kind = \"only_in_a\";\n else if (!aAssistant && !bAssistant)\n kind = \"diverge\"; // tool-only turn (rare)\n else {\n divergenceNote = classifyDivergence(aAssistant!, bAssistant!, aTools, bTools);\n kind = divergenceNote ? \"diverge\" : \"match\";\n }\n\n if (kind !== \"match\" && firstDivergenceTurn === null) firstDivergenceTurn = turn;\n pairs.push({ turn, aAssistant, bAssistant, aTools, bTools, kind, divergenceNote });\n }\n\n return { a: aSide, b: bSide, pairs, firstDivergenceTurn };\n}\n\n// ---------- divergence classification ----------\n\n/**\n * Return a short reason string if two sides meaningfully disagree, or\n * undefined if they're close enough to call a match.\n *\n * Ranking of divergence signals (cheapest first):\n * 1. Different set of tool names → clearest diff\n * 2. Different tool args for the same tool → second-clearest\n * 3. Text similarity below threshold → fuzziest\n */\nfunction classifyDivergence(\n a: TranscriptRecord,\n b: TranscriptRecord,\n aTools: TranscriptRecord[],\n bTools: TranscriptRecord[],\n): string | undefined {\n const aNames = aTools.map((t) => t.tool ?? \"\").sort();\n const bNames = bTools.map((t) => t.tool ?? \"\").sort();\n if (aNames.join(\",\") !== bNames.join(\",\")) {\n return `tool calls differ: A=[${aNames.join(\",\") || \"—\"}] B=[${bNames.join(\",\") || \"—\"}]`;\n }\n // Same tool names — did they pass different args?\n for (let i = 0; i < aTools.length; i++) {\n const at = aTools[i]!;\n const bt = bTools[i]!;\n if (at.tool !== bt.tool) continue;\n if ((at.args ?? \"\") !== (bt.args ?? \"\")) {\n return `\"${at.tool}\" args differ`;\n }\n }\n const simRatio = similarity(a.content, b.content);\n if (simRatio < 0.75) return `text similarity ${(simRatio * 100).toFixed(0)}%`;\n return undefined;\n}\n\n/**\n * Normalized Levenshtein similarity ratio in [0, 1]. 1 = identical.\n * Early-exits for long strings (> 2000 chars) with a cheap token-overlap\n * estimate to keep diff fast on chatty transcripts.\n */\nexport function similarity(a: string, b: string): number {\n if (a === b) return 1;\n if (!a && !b) return 1;\n if (!a || !b) return 0;\n const maxLen = Math.max(a.length, b.length);\n if (maxLen > 2000) return tokenOverlap(a, b);\n const dist = levenshtein(a, b);\n return 1 - dist / maxLen;\n}\n\nfunction tokenOverlap(a: string, b: string): number {\n const ta = new Set(a.toLowerCase().split(/\\s+/).filter(Boolean));\n const tb = new Set(b.toLowerCase().split(/\\s+/).filter(Boolean));\n if (ta.size === 0 && tb.size === 0) return 1;\n let shared = 0;\n for (const t of ta) if (tb.has(t)) shared++;\n return (2 * shared) / (ta.size + tb.size);\n}\n\nfunction levenshtein(a: string, b: string): number {\n const m = a.length;\n const n = b.length;\n if (m === 0) return n;\n if (n === 0) return m;\n let prev = new Array(n + 1);\n let curr = new Array(n + 1);\n for (let j = 0; j <= n; j++) prev[j] = j;\n for (let i = 1; i <= m; i++) {\n curr[0] = i;\n for (let j = 1; j <= n; j++) {\n const cost = a[i - 1] === b[j - 1] ? 0 : 1;\n curr[j] = Math.min(curr[j - 1] + 1, prev[j] + 1, prev[j - 1] + cost);\n }\n [prev, curr] = [curr, prev];\n }\n return prev[n];\n}\n\n// ---------- grouping ----------\n\ninterface TurnGroup {\n assistant?: TranscriptRecord;\n tools: TranscriptRecord[];\n}\n\nfunction groupByTurn(records: TranscriptRecord[]): Map<number, TurnGroup> {\n const out = new Map<number, TurnGroup>();\n for (const rec of records) {\n if (rec.role === \"user\") continue; // user msg is input to the turn, not its output\n const g = out.get(rec.turn) ?? { tools: [] };\n if (rec.role === \"assistant_final\") g.assistant = rec;\n else if (rec.role === \"tool\") g.tools.push(rec);\n out.set(rec.turn, g);\n }\n return out;\n}\n\n// ---------- rendering ----------\n\nexport interface RenderOptions {\n /** Monochrome output (for file redirection or piping). Defaults to true. */\n monochrome?: boolean;\n}\n\nexport function renderSummaryTable(report: DiffReport, _opts: RenderOptions = {}): string {\n const a = report.a;\n const b = report.b;\n const lines: string[] = [];\n lines.push(\"Comparing:\");\n lines.push(` A ${a.label}`);\n lines.push(` B ${b.label}`);\n lines.push(\"\");\n lines.push(row([\"\", \"A\", \"B\", \"Δ\"], [20, 14, 14, 14]));\n lines.push(\n row([\"─\".repeat(20), \"─\".repeat(14), \"─\".repeat(14), \"─\".repeat(14)], [20, 14, 14, 14]),\n );\n lines.push(statRow(\"model calls\", a.stats.turns, b.stats.turns));\n lines.push(statRow(\"user turns\", a.stats.userTurns, b.stats.userTurns));\n lines.push(statRow(\"tool calls\", a.stats.toolCalls, b.stats.toolCalls));\n lines.push(\n row(\n [\n \"cache hit\",\n `${pct(a.stats.cacheHitRatio)}`,\n `${pct(b.stats.cacheHitRatio)}`,\n signPct(b.stats.cacheHitRatio - a.stats.cacheHitRatio),\n ],\n [20, 14, 14, 14],\n ),\n );\n lines.push(\n row(\n [\n \"cost (USD)\",\n `$${a.stats.totalCostUsd.toFixed(6)}`,\n `$${b.stats.totalCostUsd.toFixed(6)}`,\n costDelta(a.stats.totalCostUsd, b.stats.totalCostUsd),\n ],\n [20, 14, 14, 14],\n ),\n );\n lines.push(statRow(\"prefix hashes\", a.stats.prefixHashes.length, b.stats.prefixHashes.length));\n // Harvest signal row — only surface when at least one side carries plan\n // state. Keeps no-harvest diffs visually identical to pre-v0.3 output.\n if (a.stats.harvestedTurns > 0 || b.stats.harvestedTurns > 0) {\n lines.push(\n row(\n [\n \"harvest turns\",\n `${a.stats.harvestedTurns}`,\n `${b.stats.harvestedTurns}`,\n signed(b.stats.harvestedTurns - a.stats.harvestedTurns),\n ],\n [20, 14, 14, 14],\n ),\n );\n lines.push(\n row(\n [\n \" subgoals\",\n `${a.stats.totalSubgoals}`,\n `${b.stats.totalSubgoals}`,\n signed(b.stats.totalSubgoals - a.stats.totalSubgoals),\n ],\n [20, 14, 14, 14],\n ),\n );\n lines.push(\n row(\n [\n \" uncertainties\",\n `${a.stats.totalUncertainties}`,\n `${b.stats.totalUncertainties}`,\n signed(b.stats.totalUncertainties - a.stats.totalUncertainties),\n ],\n [20, 14, 14, 14],\n ),\n );\n }\n lines.push(\"\");\n\n // Prefix stability story — the headline finding when comparing bench modes.\n const aPrefixStable = a.stats.prefixHashes.length <= 1;\n const bPrefixStable = b.stats.prefixHashes.length <= 1;\n if (aPrefixStable !== bPrefixStable) {\n const stable = aPrefixStable ? \"A\" : \"B\";\n const churn = aPrefixStable ? \"B\" : \"A\";\n const churnCount = aPrefixStable ? b.stats.prefixHashes.length : a.stats.prefixHashes.length;\n lines.push(\n `prefix stability: ${stable} stayed byte-stable across ${Math.max(\n a.stats.turns,\n b.stats.turns,\n )} turns; ${churn} churned ${churnCount} distinct prefixes.`,\n );\n lines.push(\"\");\n } else if (a.stats.prefixHashes[0] && a.stats.prefixHashes[0] === b.stats.prefixHashes[0]) {\n lines.push(\n `prefix: A and B share the same prefix hash (${a.stats.prefixHashes[0].slice(0, 12)}…) — cache delta is attributable to log stability, not prompt change.`,\n );\n lines.push(\"\");\n }\n\n if (report.firstDivergenceTurn !== null) {\n const p = report.pairs.find((p) => p.turn === report.firstDivergenceTurn);\n lines.push(\n `first divergence: turn ${report.firstDivergenceTurn} — ${p?.divergenceNote ?? \"?\"}`,\n );\n if (p?.aAssistant) lines.push(` A → ${truncate(p.aAssistant.content, 100)}`);\n if (p?.bAssistant) lines.push(` B → ${truncate(p.bAssistant.content, 100)}`);\n } else {\n lines.push(\"no material divergence detected (texts within similarity threshold).\");\n }\n\n return lines.join(\"\\n\");\n}\n\nexport function renderMarkdown(report: DiffReport): string {\n const a = report.a;\n const b = report.b;\n const out: string[] = [];\n out.push(`# Transcript diff: ${a.label} vs ${b.label}`);\n out.push(\"\");\n if (a.meta || b.meta) {\n out.push(\"## Meta\");\n out.push(\"\");\n out.push(`| | ${a.label} | ${b.label} |`);\n out.push(\"|---|---|---|\");\n out.push(`| source | ${a.meta?.source ?? \"—\"} | ${b.meta?.source ?? \"—\"} |`);\n out.push(`| model | ${a.meta?.model ?? \"—\"} | ${b.meta?.model ?? \"—\"} |`);\n out.push(`| task | ${a.meta?.task ?? \"—\"} | ${b.meta?.task ?? \"—\"} |`);\n out.push(`| startedAt | ${a.meta?.startedAt ?? \"—\"} | ${b.meta?.startedAt ?? \"—\"} |`);\n out.push(\"\");\n }\n\n out.push(\"## Summary\");\n out.push(\"\");\n out.push(`| metric | ${a.label} | ${b.label} | delta |`);\n out.push(\"|---|---:|---:|---:|\");\n out.push(\n `| model calls | ${a.stats.turns} | ${b.stats.turns} | ${signed(b.stats.turns - a.stats.turns)} |`,\n );\n out.push(\n `| user turns | ${a.stats.userTurns} | ${b.stats.userTurns} | ${signed(b.stats.userTurns - a.stats.userTurns)} |`,\n );\n out.push(\n `| tool calls | ${a.stats.toolCalls} | ${b.stats.toolCalls} | ${signed(b.stats.toolCalls - a.stats.toolCalls)} |`,\n );\n out.push(\n `| cache hit | ${pct(a.stats.cacheHitRatio)} | ${pct(b.stats.cacheHitRatio)} | **${signPct(b.stats.cacheHitRatio - a.stats.cacheHitRatio)}** |`,\n );\n out.push(\n `| cost (USD) | $${a.stats.totalCostUsd.toFixed(6)} | $${b.stats.totalCostUsd.toFixed(6)} | ${costDelta(a.stats.totalCostUsd, b.stats.totalCostUsd)} |`,\n );\n out.push(\n `| prefix hashes | ${a.stats.prefixHashes.length} | ${b.stats.prefixHashes.length} | — |`,\n );\n if (a.stats.harvestedTurns > 0 || b.stats.harvestedTurns > 0) {\n out.push(\n `| harvest turns | ${a.stats.harvestedTurns} | ${b.stats.harvestedTurns} | ${signed(b.stats.harvestedTurns - a.stats.harvestedTurns)} |`,\n );\n out.push(\n `| harvest subgoals | ${a.stats.totalSubgoals} | ${b.stats.totalSubgoals} | ${signed(b.stats.totalSubgoals - a.stats.totalSubgoals)} |`,\n );\n out.push(\n `| harvest uncertainties | ${a.stats.totalUncertainties} | ${b.stats.totalUncertainties} | ${signed(b.stats.totalUncertainties - a.stats.totalUncertainties)} |`,\n );\n }\n out.push(\"\");\n\n out.push(\"## Turn-by-turn\");\n out.push(\"\");\n out.push(`| turn | kind | ${a.label} tool calls | ${b.label} tool calls | note |`);\n out.push(\"|---:|:---:|---|---|---|\");\n for (const p of report.pairs) {\n const aTools =\n p.aTools\n .map((t) => t.tool)\n .filter(Boolean)\n .join(\", \") || \"—\";\n const bTools =\n p.bTools\n .map((t) => t.tool)\n .filter(Boolean)\n .join(\", \") || \"—\";\n out.push(`| ${p.turn} | ${p.kind} | ${aTools} | ${bTools} | ${p.divergenceNote ?? \"\"} |`);\n }\n out.push(\"\");\n\n if (report.firstDivergenceTurn !== null) {\n const p = report.pairs.find((x) => x.turn === report.firstDivergenceTurn);\n out.push(`## First divergence (turn ${report.firstDivergenceTurn})`);\n out.push(\"\");\n out.push(p?.divergenceNote ?? \"\");\n out.push(\"\");\n if (p?.aAssistant) {\n out.push(`**${a.label}:**`);\n out.push(\"\");\n out.push(\"```\");\n out.push(p.aAssistant.content);\n out.push(\"```\");\n out.push(\"\");\n }\n if (p?.bAssistant) {\n out.push(`**${b.label}:**`);\n out.push(\"\");\n out.push(\"```\");\n out.push(p.bAssistant.content);\n out.push(\"```\");\n out.push(\"\");\n }\n }\n return out.join(\"\\n\");\n}\n\n// ---------- formatting helpers ----------\n\nfunction row(cols: string[], widths: number[]): string {\n return cols.map((c, i) => padRight(c, widths[i] ?? c.length)).join(\" \");\n}\n\nfunction statRow(label: string, av: number, bv: number): string {\n return row([label, `${av}`, `${bv}`, signed(bv - av)], [20, 14, 14, 14]);\n}\n\nfunction padRight(s: string, w: number): string {\n return s.length >= w ? s : s + \" \".repeat(w - s.length);\n}\n\nfunction signed(n: number): string {\n if (n === 0) return \"0\";\n return `${n > 0 ? \"+\" : \"\"}${n}`;\n}\n\nfunction signPct(diff: number): string {\n if (diff === 0) return \"0pp\";\n const s = (diff * 100).toFixed(1);\n return `${diff > 0 ? \"+\" : \"\"}${s}pp`;\n}\n\nfunction pct(x: number): string {\n return `${(x * 100).toFixed(1)}%`;\n}\n\nfunction costDelta(a: number, b: number): string {\n if (a === 0 && b === 0) return \"—\";\n if (a === 0) return \"new\";\n const pctChange = ((b - a) / a) * 100;\n return `${pctChange > 0 ? \"+\" : \"\"}${pctChange.toFixed(1)}%`;\n}\n\nfunction truncate(s: string, n: number): string {\n return s.length > n ? `${s.slice(0, n)}…` : s;\n}\n","/**\n * MCP (Model Context Protocol) type definitions.\n *\n * Hand-rolled rather than importing @modelcontextprotocol/sdk because:\n * - Reasonix's value-add isn't reimplementing the protocol, but *caching*\n * it. Owning the types lets us tune them for our integration (strip\n * fields we don't use, add the ones we do like Reasonix's prefixHash).\n * - Zero dependencies — consistent with how we wrote the DeepSeek client.\n * - If Anthropic bumps the SDK and introduces a breaking change, we're\n * insulated as long as we keep up with the spec itself.\n *\n * Spec reference: https://spec.modelcontextprotocol.io/ (2024-11-05 draft\n * at time of writing). Reasonix models the subset it consumes: tools\n * list/call, resources list/read, prompts list/get, plus the init\n * handshake. Sampling and progress notifications remain deferred.\n *\n * Transport note: the wire format for stdio MCP is **newline-delimited\n * JSON** (NDJSON), not the LSP-style Content-Length header framing that\n * some readers might expect. One JSON-RPC message per line.\n */\n\n// ---------- JSON-RPC 2.0 base ----------\n\nexport type JsonRpcId = string | number;\n\nexport interface JsonRpcRequest<P = unknown> {\n jsonrpc: \"2.0\";\n id: JsonRpcId;\n method: string;\n params?: P;\n}\n\nexport interface JsonRpcNotification<P = unknown> {\n jsonrpc: \"2.0\";\n method: string;\n params?: P;\n}\n\nexport interface JsonRpcSuccess<R = unknown> {\n jsonrpc: \"2.0\";\n id: JsonRpcId;\n result: R;\n}\n\nexport interface JsonRpcError {\n jsonrpc: \"2.0\";\n id: JsonRpcId | null;\n error: {\n /** JSON-RPC standard codes: -32700 parse, -32600 invalid request, -32601 method not found, -32602 invalid params, -32603 internal. MCP also defines its own range. */\n code: number;\n message: string;\n data?: unknown;\n };\n}\n\nexport type JsonRpcResponse<R = unknown> = JsonRpcSuccess<R> | JsonRpcError;\n\nexport type JsonRpcMessage = JsonRpcRequest | JsonRpcNotification | JsonRpcSuccess | JsonRpcError;\n\n// ---------- MCP initialize ----------\n\nexport interface McpClientInfo {\n name: string;\n version: string;\n}\n\nexport interface McpClientCapabilities {\n /** Empty object advertises support without any optional sub-features. */\n tools?: Record<string, never>;\n /** Advertised when the client can consume `resources/list` + `resources/read`. */\n resources?: Record<string, never>;\n /** Advertised when the client can consume `prompts/list` + `prompts/get`. */\n prompts?: Record<string, never>;\n // sampling would go here — deferred.\n}\n\nexport interface InitializeParams {\n protocolVersion: string;\n capabilities: McpClientCapabilities;\n clientInfo: McpClientInfo;\n}\n\nexport interface InitializeResult {\n protocolVersion: string;\n serverInfo: { name: string; version: string };\n capabilities: {\n tools?: { listChanged?: boolean };\n resources?: unknown;\n prompts?: unknown;\n };\n instructions?: string;\n}\n\n// ---------- MCP tools ----------\n\nexport interface McpToolSchema {\n /** JSON Schema — compatible with Reasonix's tools.ts JSONSchema shape. */\n type?: string;\n properties?: Record<string, unknown>;\n required?: string[];\n [extra: string]: unknown;\n}\n\nexport interface McpTool {\n name: string;\n description?: string;\n /** MCP calls this `inputSchema`. Reasonix's `parameters` field is the same concept. */\n inputSchema: McpToolSchema;\n}\n\nexport interface ListToolsResult {\n tools: McpTool[];\n nextCursor?: string;\n}\n\nexport interface CallToolParams {\n name: string;\n arguments?: Record<string, unknown>;\n /**\n * MCP's `_meta` envelope carries out-of-band protocol metadata.\n * Setting `progressToken` here tells the server \"send me progress\n * notifications back using this token\"; the server must then emit\n * `notifications/progress` frames until the response arrives.\n */\n _meta?: { progressToken?: string | number };\n}\n\n/**\n * Server → client notification emitted during a long-running request\n * that the client subscribed to via `_meta.progressToken`. `progress`\n * and `total` are typically matched units (files scanned, bytes\n * processed, etc.); `total` may be missing when the server can't\n * estimate the upper bound up front.\n */\nexport interface ProgressNotificationParams {\n progressToken: string | number;\n progress: number;\n total?: number;\n message?: string;\n}\n\n/** Values a `ProgressHandler` receives — `progressToken` is already matched away. */\nexport interface McpProgressInfo {\n progress: number;\n total?: number;\n message?: string;\n}\n\nexport type McpProgressHandler = (info: McpProgressInfo) => void;\n\nexport interface McpContentBlockText {\n type: \"text\";\n text: string;\n}\n\nexport interface McpContentBlockImage {\n type: \"image\";\n data: string;\n mimeType: string;\n}\n\n/** MCP result content is an array of typed blocks. Reasonix consumes only text for now — image blocks get stringified with a placeholder. */\nexport type McpContentBlock = McpContentBlockText | McpContentBlockImage;\n\nexport interface CallToolResult {\n content: McpContentBlock[];\n /** True = tool raised an error; the content describes it. */\n isError?: boolean;\n}\n\n// ---------- MCP resources ----------\n\n/**\n * A resource the server can expose — think \"file the model can read.\"\n * The URI is opaque to the client: servers may use `file://`, custom\n * schemes, or bare strings. Reasonix doesn't interpret them.\n */\nexport interface McpResource {\n uri: string;\n name: string;\n description?: string;\n /** Hint for the content type (e.g. \"text/markdown\"). Purely informational. */\n mimeType?: string;\n}\n\nexport interface ListResourcesParams {\n /** Pagination cursor from a previous listResources response. */\n cursor?: string;\n}\n\nexport interface ListResourcesResult {\n resources: McpResource[];\n nextCursor?: string;\n}\n\nexport interface ReadResourceParams {\n uri: string;\n}\n\n/**\n * One resource can return multiple content blobs (e.g. the file + a\n * side-car). `text` is the common case for UTF-8 content; `blob` is\n * base64-encoded bytes for binary content. Servers populate exactly\n * one of the two for each entry.\n */\nexport interface McpResourceContentsText {\n uri: string;\n mimeType?: string;\n text: string;\n}\n\nexport interface McpResourceContentsBlob {\n uri: string;\n mimeType?: string;\n blob: string;\n}\n\nexport type McpResourceContents = McpResourceContentsText | McpResourceContentsBlob;\n\nexport interface ReadResourceResult {\n contents: McpResourceContents[];\n}\n\n// ---------- MCP prompts ----------\n\n/**\n * A parameterizable prompt template the server exposes. Clients fetch\n * it with `prompts/get` and pass the result to the model as-is.\n */\nexport interface McpPromptArgument {\n name: string;\n description?: string;\n required?: boolean;\n}\n\nexport interface McpPrompt {\n name: string;\n description?: string;\n arguments?: McpPromptArgument[];\n}\n\nexport interface ListPromptsParams {\n cursor?: string;\n}\n\nexport interface ListPromptsResult {\n prompts: McpPrompt[];\n nextCursor?: string;\n}\n\nexport interface GetPromptParams {\n name: string;\n arguments?: Record<string, string>;\n}\n\n/**\n * MCP prompt messages are modeled after chat completions: role + content.\n * Content can be a text block OR (per the spec) a resource/image block;\n * Reasonix cares about text in v1, but surfaces the raw array so callers\n * can render other kinds if they need to.\n */\nexport interface McpPromptMessage {\n role: \"user\" | \"assistant\";\n content: McpContentBlock | McpPromptResourceBlock;\n}\n\nexport interface McpPromptResourceBlock {\n type: \"resource\";\n resource: McpResourceContents;\n}\n\nexport interface GetPromptResult {\n description?: string;\n messages: McpPromptMessage[];\n}\n\n// ---------- convenience ----------\n\n/** Current MCP protocol version Reasonix is coded against. */\nexport const MCP_PROTOCOL_VERSION = \"2024-11-05\";\n\n/** Type guard — success vs error response. */\nexport function isJsonRpcError(msg: JsonRpcResponse): msg is JsonRpcError {\n return \"error\" in msg;\n}\n","/**\n * MCP client — request/response correlation, initialize handshake,\n * tools/list, tools/call. Built on top of a McpTransport so the same\n * logic works against a real stdio server or an in-process fake.\n */\n\nimport type { McpTransport } from \"./stdio.js\";\nimport {\n type CallToolParams,\n type CallToolResult,\n type GetPromptParams,\n type GetPromptResult,\n type InitializeParams,\n type InitializeResult,\n type JsonRpcId,\n type JsonRpcMessage,\n type JsonRpcRequest,\n type JsonRpcResponse,\n type ListPromptsParams,\n type ListPromptsResult,\n type ListResourcesParams,\n type ListResourcesResult,\n type ListToolsResult,\n MCP_PROTOCOL_VERSION,\n type McpClientInfo,\n type McpProgressHandler,\n type ProgressNotificationParams,\n type ReadResourceParams,\n type ReadResourceResult,\n isJsonRpcError,\n} from \"./types.js\";\n\nexport interface McpClientOptions {\n transport: McpTransport;\n clientInfo?: McpClientInfo;\n /** Per-request timeout. Default 60s. */\n requestTimeoutMs?: number;\n}\n\ninterface PendingRequest {\n resolve: (value: unknown) => void;\n reject: (err: Error) => void;\n timeout: NodeJS.Timeout;\n}\n\nexport class McpClient {\n private readonly transport: McpTransport;\n private readonly clientInfo: McpClientInfo;\n private readonly requestTimeoutMs: number;\n private readonly pending = new Map<JsonRpcId, PendingRequest>();\n private nextId = 1;\n private readerStarted = false;\n private initialized = false;\n private _serverCapabilities: InitializeResult[\"capabilities\"] = {};\n private _serverInfo: InitializeResult[\"serverInfo\"] = { name: \"\", version: \"\" };\n private _protocolVersion = \"\";\n private _instructions: string | undefined;\n // Progress-token → handler for notifications/progress routing. Tokens\n // are minted per call when the caller supplies an onProgress\n // callback; cleared when the final response lands (or the pending\n // request rejects). No leaks — the `try/finally` in callTool\n // guarantees cleanup even on timeout.\n private readonly progressHandlers = new Map<string | number, McpProgressHandler>();\n private nextProgressToken = 1;\n\n constructor(opts: McpClientOptions) {\n this.transport = opts.transport;\n this.clientInfo = opts.clientInfo ?? { name: \"reasonix\", version: \"0.3.0-dev\" };\n this.requestTimeoutMs = opts.requestTimeoutMs ?? 60_000;\n }\n\n /** Server's advertised capabilities, available after initialize(). */\n get serverCapabilities(): InitializeResult[\"capabilities\"] {\n return this._serverCapabilities;\n }\n\n /** Server's self-reported name + version, available after initialize(). */\n get serverInfo(): InitializeResult[\"serverInfo\"] {\n return this._serverInfo;\n }\n\n /** Protocol version the server agreed to during the handshake. */\n get protocolVersion(): string {\n return this._protocolVersion;\n }\n\n /** Optional free-form instructions the server provides at handshake. */\n get serverInstructions(): string | undefined {\n return this._instructions;\n }\n\n /**\n * Complete the initialize → initialized handshake. Must be called\n * before any other method (otherwise compliant servers reject).\n */\n async initialize(): Promise<InitializeResult> {\n if (this.initialized) throw new Error(\"MCP client already initialized\");\n this.startReaderIfNeeded();\n const result = await this.request<InitializeResult>(\"initialize\", {\n protocolVersion: MCP_PROTOCOL_VERSION,\n // Advertise every method the client can consume so servers know\n // they can send listChanged notifications etc. Sub-feature flags\n // (e.g. `resources.subscribe`) are omitted — we don't implement\n // those yet and the empty object means \"method-level support, no\n // sub-features.\"\n capabilities: { tools: {}, resources: {}, prompts: {} },\n clientInfo: this.clientInfo,\n } satisfies InitializeParams);\n this._serverCapabilities = result.capabilities ?? {};\n this._serverInfo = result.serverInfo ?? { name: \"\", version: \"\" };\n this._protocolVersion = result.protocolVersion ?? \"\";\n this._instructions = result.instructions;\n // Per spec: client sends notifications/initialized after receiving the\n // initialize response. Only then is the connection live for other\n // methods.\n await this.transport.send({\n jsonrpc: \"2.0\",\n method: \"notifications/initialized\",\n });\n this.initialized = true;\n return result;\n }\n\n /** List tools the server exposes. */\n async listTools(): Promise<ListToolsResult> {\n this.assertInitialized();\n return this.request<ListToolsResult>(\"tools/list\", {});\n }\n\n /**\n * Invoke a tool by name. When `onProgress` is supplied, attaches a\n * fresh progress token so the server can send incremental updates\n * via `notifications/progress`; they're routed to the callback until\n * the final response arrives (or the request times out, in which\n * case the handler is simply dropped — no extra notification).\n *\n * When `signal` is supplied, aborting it:\n * 1) fires `notifications/cancelled` to the server (MCP 2024-11-05\n * way of saying \"forget this request, I no longer care\"), and\n * 2) rejects the pending promise immediately with an AbortError,\n * so the caller doesn't have to wait for the subprocess to\n * finish its in-flight file write or network request.\n * The server MAY still emit a late response; we drop it in dispatch\n * since the request id is gone from `pending`.\n */\n async callTool(\n name: string,\n args?: Record<string, unknown>,\n opts: { onProgress?: McpProgressHandler; signal?: AbortSignal } = {},\n ): Promise<CallToolResult> {\n this.assertInitialized();\n const params: CallToolParams = { name, arguments: args ?? {} };\n let token: number | undefined;\n if (opts.onProgress) {\n token = this.nextProgressToken++;\n this.progressHandlers.set(token, opts.onProgress);\n params._meta = { progressToken: token };\n }\n try {\n return await this.request<CallToolResult>(\"tools/call\", params, opts.signal);\n } finally {\n if (token !== undefined) this.progressHandlers.delete(token);\n }\n }\n\n /**\n * List resources the server exposes. Supports a pagination cursor;\n * callers interested in the full set should loop on `nextCursor`.\n * Servers that don't support resources respond with method-not-found\n * (−32601) — we surface that as a thrown Error so callers can gate\n * on the `serverCapabilities.resources` field first.\n */\n async listResources(cursor?: string): Promise<ListResourcesResult> {\n this.assertInitialized();\n return this.request<ListResourcesResult>(\"resources/list\", {\n ...(cursor ? { cursor } : {}),\n } satisfies ListResourcesParams);\n }\n\n /** Read the contents of a resource by URI. */\n async readResource(uri: string): Promise<ReadResourceResult> {\n this.assertInitialized();\n return this.request<ReadResourceResult>(\"resources/read\", {\n uri,\n } satisfies ReadResourceParams);\n }\n\n /** List prompt templates the server exposes. */\n async listPrompts(cursor?: string): Promise<ListPromptsResult> {\n this.assertInitialized();\n return this.request<ListPromptsResult>(\"prompts/list\", {\n ...(cursor ? { cursor } : {}),\n } satisfies ListPromptsParams);\n }\n\n /**\n * Fetch a rendered prompt by name. `args` supplies values for any\n * required template arguments; the server validates. Returns messages\n * ready to prepend to the model's input.\n */\n async getPrompt(name: string, args?: Record<string, string>): Promise<GetPromptResult> {\n this.assertInitialized();\n return this.request<GetPromptResult>(\"prompts/get\", {\n name,\n ...(args ? { arguments: args } : {}),\n } satisfies GetPromptParams);\n }\n\n /** Close the transport and reject any outstanding requests. */\n async close(): Promise<void> {\n for (const [, pending] of this.pending) {\n clearTimeout(pending.timeout);\n pending.reject(new Error(\"MCP client closed\"));\n }\n this.pending.clear();\n await this.transport.close();\n }\n\n // ---------- internals ----------\n\n private assertInitialized(): void {\n if (!this.initialized) throw new Error(\"MCP client not initialized — call initialize() first\");\n }\n\n private async request<R>(method: string, params: unknown, signal?: AbortSignal): Promise<R> {\n const id = this.nextId++;\n const frame: JsonRpcRequest = { jsonrpc: \"2.0\", id, method, params };\n let abortHandler: (() => void) | null = null;\n const promise = new Promise<R>((resolve, reject) => {\n const timeout = setTimeout(() => {\n this.pending.delete(id);\n if (abortHandler && signal) signal.removeEventListener(\"abort\", abortHandler);\n reject(\n new Error(`MCP request ${method} (id=${id}) timed out after ${this.requestTimeoutMs}ms`),\n );\n }, this.requestTimeoutMs);\n this.pending.set(id, {\n resolve: resolve as (value: unknown) => void,\n reject,\n timeout,\n });\n // Wire up cancellation: when signal fires, send an MCP cancellation\n // notification to the server (so it can stop whatever it was doing)\n // and reject the caller immediately — no need to wait for the\n // subprocess to finish its in-flight work. Late responses from the\n // server are dropped by `dispatch` because the id is gone from\n // `pending`.\n if (signal) {\n if (signal.aborted) {\n this.pending.delete(id);\n clearTimeout(timeout);\n reject(new Error(`MCP request ${method} (id=${id}) aborted before send`));\n return;\n }\n abortHandler = () => {\n this.pending.delete(id);\n clearTimeout(timeout);\n void this.transport\n .send({\n jsonrpc: \"2.0\",\n method: \"notifications/cancelled\",\n params: { requestId: id, reason: \"aborted by user\" },\n })\n .catch(() => {\n // Transport may already be closing — swallow; we still\n // reject the caller below so they unblock.\n });\n reject(new Error(`MCP request ${method} (id=${id}) aborted by user`));\n };\n signal.addEventListener(\"abort\", abortHandler, { once: true });\n }\n });\n try {\n await this.transport.send(frame);\n } catch (err) {\n this.pending.delete(id);\n if (abortHandler && signal) signal.removeEventListener(\"abort\", abortHandler);\n throw err;\n }\n try {\n return await promise;\n } finally {\n if (abortHandler && signal) signal.removeEventListener(\"abort\", abortHandler);\n }\n }\n\n private startReaderIfNeeded(): void {\n if (this.readerStarted) return;\n this.readerStarted = true;\n // Fire-and-forget: the reader runs for the lifetime of the client.\n void this.readLoop();\n }\n\n private async readLoop(): Promise<void> {\n try {\n for await (const msg of this.transport.messages()) {\n this.dispatch(msg);\n }\n } catch (err) {\n // Surface as rejections on all pending requests so nobody hangs.\n for (const [, pending] of this.pending) {\n clearTimeout(pending.timeout);\n pending.reject(err as Error);\n }\n this.pending.clear();\n }\n }\n\n private dispatch(msg: JsonRpcMessage): void {\n // Notifications (no `id`): route by method. Progress notifications\n // go to the per-call handler if one was registered; everything\n // else is dropped silently (we don't yet handle tools/list_changed\n // or resources/list_changed).\n if (!(\"id\" in msg) || msg.id === null || msg.id === undefined) {\n if (\"method\" in msg && msg.method === \"notifications/progress\") {\n const p = msg.params as ProgressNotificationParams | undefined;\n if (!p || p.progressToken === undefined) return;\n const handler = this.progressHandlers.get(p.progressToken);\n if (!handler) return; // late notification after the call resolved\n handler({ progress: p.progress, total: p.total, message: p.message });\n }\n return;\n }\n if (!(\"result\" in msg) && !(\"error\" in msg)) return; // it's a request from server\n const pending = this.pending.get(msg.id);\n if (!pending) return; // late response after timeout; drop\n this.pending.delete(msg.id);\n clearTimeout(pending.timeout);\n const resp = msg as JsonRpcResponse;\n if (isJsonRpcError(resp)) {\n pending.reject(new Error(`MCP ${resp.error.code}: ${resp.error.message}`));\n } else {\n pending.resolve(resp.result);\n }\n }\n}\n","/**\n * Stdio transport for MCP.\n *\n * MCP's stdio wire format is **newline-delimited JSON** (one JSON-RPC\n * message per line). We spawn the server as a child process, write\n * frames to its stdin, parse its stdout line-by-line as they arrive.\n *\n * Transport is abstracted behind an interface so unit tests can fake it\n * with an in-process duplex pair — spawning real servers in unit tests\n * is flaky and slow.\n */\n\nimport { type ChildProcess, spawn } from \"node:child_process\";\nimport type { JsonRpcMessage } from \"./types.js\";\n\n/**\n * A transport sends JSON-RPC messages upstream and surfaces messages\n * arriving downstream via an async iterator. One instance per server\n * connection.\n */\nexport interface McpTransport {\n /** Send one JSON-RPC message. Resolves when the bytes are accepted. */\n send(message: JsonRpcMessage): Promise<void>;\n /** Async iterator over incoming messages. Ends when the connection closes. */\n messages(): AsyncIterableIterator<JsonRpcMessage>;\n /** Close the underlying resource (kill child process, close streams). */\n close(): Promise<void>;\n}\n\nexport interface StdioTransportOptions {\n /** Argv to spawn. First element is the command. */\n command: string;\n args?: string[];\n /** Env overlay — merged over process.env unless replaceEnv=true. */\n env?: Record<string, string>;\n /** When true, only the env above is visible to the child. Default false. */\n replaceEnv?: boolean;\n /** CWD for the child. Default: process.cwd(). */\n cwd?: string;\n /**\n * Spawn through a shell. Default: true on win32 (needed to resolve\n * `.cmd` wrappers like `npx.cmd`, `pnpm.cmd`), false elsewhere.\n * Explicitly pass `false` to opt out on Windows; pass `true` to force\n * it on POSIX (rarely needed).\n */\n shell?: boolean;\n}\n\n/**\n * Spawn `command args...` as a child process and use its stdin/stdout as\n * an MCP transport. Stderr is forwarded to the parent's stderr so server\n * diagnostics are still visible.\n */\nexport class StdioTransport implements McpTransport {\n private readonly child: ChildProcess;\n private readonly queue: JsonRpcMessage[] = [];\n private readonly waiters: Array<(m: JsonRpcMessage | null) => void> = [];\n private closed = false;\n private stdoutBuffer = \"\";\n\n constructor(opts: StdioTransportOptions) {\n const env = opts.replaceEnv ? { ...(opts.env ?? {}) } : { ...process.env, ...(opts.env ?? {}) };\n // Windows wraps binaries as .cmd/.bat shims (npx.cmd, pnpm.cmd, …).\n // child_process.spawn without shell:true can't resolve them, which\n // breaks `--mcp \"npx -y some-server\"` — the most common MCP setup.\n // Default shell:true on win32 and leave POSIX alone.\n const shell = opts.shell ?? process.platform === \"win32\";\n\n if (shell) {\n // Node's shell:true + args[] triggers DEP0190 because it concatenates\n // with spaces and doesn't quote args — unsafe if an arg contains\n // shell metacharacters. We build a single command line ourselves,\n // quoting ONLY the args (command stays bare so the shell's PATH /\n // PATHEXT lookup finds `npx` → `npx.cmd` on Windows).\n const line = [\n opts.command,\n ...(opts.args ?? []).map((a) => quoteArg(a, process.platform === \"win32\")),\n ].join(\" \");\n this.child = spawn(line, [], {\n env,\n cwd: opts.cwd,\n stdio: [\"pipe\", \"pipe\", \"inherit\"],\n shell: true,\n });\n } else {\n this.child = spawn(opts.command, opts.args ?? [], {\n env,\n cwd: opts.cwd,\n stdio: [\"pipe\", \"pipe\", \"inherit\"],\n });\n }\n this.child.stdout!.setEncoding(\"utf8\");\n this.child.stdout!.on(\"data\", (chunk: string) => this.onStdout(chunk));\n this.child.on(\"close\", () => this.onClose());\n this.child.on(\"error\", (err) => {\n // Surface spawn errors as a synthetic JsonRpcError so callers don't\n // hang on a stream that never emits anything.\n this.push({\n jsonrpc: \"2.0\",\n id: null,\n error: { code: -32000, message: `transport error: ${err.message}` },\n });\n });\n }\n\n async send(message: JsonRpcMessage): Promise<void> {\n if (this.closed) throw new Error(\"MCP transport is closed\");\n return new Promise((resolve, reject) => {\n const line = `${JSON.stringify(message)}\\n`;\n this.child.stdin!.write(line, \"utf8\", (err) => {\n if (err) reject(err);\n else resolve();\n });\n });\n }\n\n async *messages(): AsyncIterableIterator<JsonRpcMessage> {\n while (true) {\n if (this.queue.length > 0) {\n yield this.queue.shift()!;\n continue;\n }\n if (this.closed) return;\n const next = await new Promise<JsonRpcMessage | null>((resolve) => {\n this.waiters.push(resolve);\n });\n if (next === null) return; // closed while we were waiting\n yield next;\n }\n }\n\n async close(): Promise<void> {\n if (this.closed) return;\n this.closed = true;\n // Signal any pending waiters.\n while (this.waiters.length > 0) this.waiters.shift()!(null);\n try {\n this.child.stdin!.end();\n } catch {\n /* already ended */\n }\n if (this.child.exitCode === null && !this.child.killed) {\n this.child.kill(\"SIGTERM\");\n }\n }\n\n /** Parse incoming stdout chunks into NDJSON messages. */\n private onStdout(chunk: string): void {\n this.stdoutBuffer += chunk;\n let newlineIdx: number;\n // biome-ignore lint/suspicious/noAssignInExpressions: idiomatic loop shape\n while ((newlineIdx = this.stdoutBuffer.indexOf(\"\\n\")) !== -1) {\n const line = this.stdoutBuffer.slice(0, newlineIdx).trim();\n this.stdoutBuffer = this.stdoutBuffer.slice(newlineIdx + 1);\n if (!line) continue;\n try {\n const msg = JSON.parse(line) as JsonRpcMessage;\n this.push(msg);\n } catch {\n // Malformed lines are dropped — some servers emit startup banners\n // before the JSON-RPC loop begins. We surface the noise to stderr\n // via the inherited stderr stream, not our event queue.\n }\n }\n }\n\n private onClose(): void {\n this.closed = true;\n while (this.waiters.length > 0) this.waiters.shift()!(null);\n }\n\n private push(msg: JsonRpcMessage): void {\n const waiter = this.waiters.shift();\n if (waiter) waiter(msg);\n else this.queue.push(msg);\n }\n}\n\n/**\n * Quote a single argument for inclusion in a shell command line.\n * On Windows (cmd.exe): wrap in double quotes, escape internal `\"` as `\"\"`,\n * leave everything else alone. On POSIX: wrap in single quotes, escape\n * internal `'` as `'\\''`. Both handle spaces, wildcards, pipes, and all\n * other metacharacters correctly.\n */\nfunction quoteArg(s: string, windows: boolean): string {\n if (!windows) {\n // POSIX: single-quote, escape single quotes.\n return `'${s.replace(/'/g, \"'\\\\''\")}'`;\n }\n // cmd.exe: double-quote, escape internal quotes by doubling.\n return `\"${s.replace(/\"/g, '\"\"')}\"`;\n}\n","/**\n * HTTP+SSE transport for MCP (spec version 2024-11-05).\n *\n * Wire shape:\n * 1. Client opens GET to the SSE URL with `Accept: text/event-stream`.\n * 2. Server's first SSE event is `event: endpoint`, `data: <url>` — the\n * URL (relative or absolute) the client must POST JSON-RPC requests\n * to. All subsequent server → client messages arrive as `event: message`\n * SSE frames carrying a JSON-RPC response or server-initiated frame.\n * 3. Client POSTs each outgoing JSON-RPC frame to the endpoint URL.\n * The POST response body is ignored — replies land on the SSE stream.\n *\n * This transport exists so Reasonix can talk to hosted/remote MCP servers\n * (e.g. a company's internal knowledge server fronted by auth). Stdio\n * covers local subprocesses; SSE covers everything else.\n *\n * Note: the newer \"Streamable HTTP\" transport (2025 spec) folds the POST\n * and SSE streams onto a single endpoint. We stay on 2024-11-05 here —\n * that's what `MCP_PROTOCOL_VERSION` advertises in the initialize handshake\n * and what currently-published servers implement.\n */\n\nimport { createParser } from \"eventsource-parser\";\nimport type { McpTransport } from \"./stdio.js\";\nimport type { JsonRpcMessage } from \"./types.js\";\n\nexport interface SseTransportOptions {\n /** SSE endpoint URL, e.g. `https://mcp.example.com/sse`. */\n url: string;\n /** Extra headers sent on both the SSE GET and the JSON-RPC POSTs (e.g. `Authorization`). */\n headers?: Record<string, string>;\n}\n\n/**\n * Open an SSE stream to `url`, parse incoming events into JsonRpcMessages,\n * POST outgoing frames to the endpoint URL the server advertises.\n */\nexport class SseTransport implements McpTransport {\n private readonly url: string;\n private readonly headers: Record<string, string>;\n private readonly queue: JsonRpcMessage[] = [];\n private readonly waiters: Array<(m: JsonRpcMessage | null) => void> = [];\n private readonly controller = new AbortController();\n private closed = false;\n private postUrl: string | null = null;\n private readonly endpointReady: Promise<string>;\n private resolveEndpoint!: (url: string) => void;\n private rejectEndpoint!: (err: Error) => void;\n\n constructor(opts: SseTransportOptions) {\n this.url = opts.url;\n this.headers = opts.headers ?? {};\n this.endpointReady = new Promise<string>((resolve, reject) => {\n this.resolveEndpoint = resolve;\n this.rejectEndpoint = reject;\n });\n // Swallow unhandled-rejection noise if nobody ever calls send().\n this.endpointReady.catch(() => undefined);\n void this.runStream();\n }\n\n async send(message: JsonRpcMessage): Promise<void> {\n if (this.closed) throw new Error(\"MCP SSE transport is closed\");\n const postUrl = await this.endpointReady;\n const res = await fetch(postUrl, {\n method: \"POST\",\n headers: { \"content-type\": \"application/json\", ...this.headers },\n body: JSON.stringify(message),\n signal: this.controller.signal,\n });\n // Drain body so the socket returns to the pool even if the server\n // elected to write one. We explicitly don't parse it — responses\n // arrive on the SSE channel.\n await res.arrayBuffer().catch(() => undefined);\n if (!res.ok) {\n throw new Error(`MCP SSE POST ${postUrl} failed: ${res.status} ${res.statusText}`);\n }\n }\n\n async *messages(): AsyncIterableIterator<JsonRpcMessage> {\n while (true) {\n if (this.queue.length > 0) {\n yield this.queue.shift()!;\n continue;\n }\n if (this.closed) return;\n const next = await new Promise<JsonRpcMessage | null>((resolve) => {\n this.waiters.push(resolve);\n });\n if (next === null) return;\n yield next;\n }\n }\n\n async close(): Promise<void> {\n if (this.closed) return;\n this.closed = true;\n while (this.waiters.length > 0) this.waiters.shift()!(null);\n // Reject any still-pending send() that was waiting for the endpoint.\n this.rejectEndpoint(new Error(\"MCP SSE transport closed before endpoint was ready\"));\n try {\n this.controller.abort();\n } catch {\n /* already aborted */\n }\n }\n\n // ---------- internals ----------\n\n private async runStream(): Promise<void> {\n let res: Response;\n try {\n res = await fetch(this.url, {\n method: \"GET\",\n headers: { accept: \"text/event-stream\", ...this.headers },\n signal: this.controller.signal,\n });\n } catch (err) {\n this.failHandshake(`SSE connect to ${this.url} failed: ${(err as Error).message}`);\n return;\n }\n if (!res.ok || !res.body) {\n // Drain body to free the socket before giving up.\n await res.body?.cancel().catch(() => undefined);\n this.failHandshake(`SSE handshake ${this.url} → ${res.status} ${res.statusText}`);\n return;\n }\n\n const parser = createParser({\n onEvent: (ev) => this.handleEvent(ev.event ?? \"message\", ev.data),\n });\n const decoder = new TextDecoder();\n try {\n for await (const chunk of res.body as AsyncIterable<Uint8Array>) {\n parser.feed(decoder.decode(chunk, { stream: true }));\n }\n } catch (err) {\n if (!this.closed) {\n this.pushError(`SSE stream error: ${(err as Error).message}`);\n }\n } finally {\n this.markClosed();\n }\n }\n\n private handleEvent(type: string, data: string): void {\n if (type === \"endpoint\") {\n if (this.postUrl) return; // ignore repeat announcements\n try {\n this.postUrl = new URL(data, this.url).toString();\n this.resolveEndpoint(this.postUrl);\n } catch (err) {\n this.failHandshake(`SSE endpoint event had bad URL \"${data}\": ${(err as Error).message}`);\n }\n return;\n }\n if (type === \"message\") {\n try {\n const parsed = JSON.parse(data) as JsonRpcMessage;\n this.pushMessage(parsed);\n } catch {\n // Malformed JSON-RPC on an SSE frame — drop it, same as stdio.\n }\n return;\n }\n // Unknown event types (server pings, custom extensions) — ignore.\n }\n\n private failHandshake(reason: string): void {\n this.rejectEndpoint(new Error(reason));\n this.pushError(reason);\n this.markClosed();\n }\n\n private pushMessage(msg: JsonRpcMessage): void {\n const waiter = this.waiters.shift();\n if (waiter) waiter(msg);\n else this.queue.push(msg);\n }\n\n private pushError(message: string): void {\n this.pushMessage({\n jsonrpc: \"2.0\",\n id: null,\n error: { code: -32000, message },\n });\n }\n\n private markClosed(): void {\n if (this.closed) return;\n this.closed = true;\n while (this.waiters.length > 0) this.waiters.shift()!(null);\n }\n}\n","/**\n * Split a shell-style command string into argv, respecting single and\n * double quotes. Intended for parsing the user's `--mcp \"cmd args...\"`\n * flag — NOT a full shell parser (no variable expansion, no subshells,\n * no globs, no `&&` / pipes).\n *\n * The tradeoff: users with paths containing spaces need to quote them\n * (e.g. `--mcp 'npx -y pkg \"/my path/here\"'`), which is how they'd\n * already quote them at the shell level.\n *\n * Throws on unterminated quotes — better than silently dropping half\n * the command.\n */\nexport function shellSplit(input: string): string[] {\n const tokens: string[] = [];\n let cur = \"\";\n let quote: '\"' | \"'\" | null = null;\n let i = 0;\n const s = input;\n\n while (i < s.length) {\n const ch = s[i]!;\n\n if (quote) {\n if (ch === quote) {\n quote = null;\n i++;\n continue;\n }\n // backslash escapes inside double quotes only\n if (ch === \"\\\\\" && quote === '\"' && i + 1 < s.length) {\n cur += s[i + 1];\n i += 2;\n continue;\n }\n cur += ch;\n i++;\n continue;\n }\n\n if (ch === '\"' || ch === \"'\") {\n quote = ch as '\"' | \"'\";\n i++;\n continue;\n }\n\n // Backslash escape ONLY applies inside double quotes (handled above).\n // Outside quotes, backslashes pass through literally — otherwise\n // Windows paths like `C:\\path\\to\\exe` get mangled. POSIX users who\n // want to escape a space outside quotes can use single quotes instead.\n\n if (ch === \" \" || ch === \"\\t\") {\n if (cur.length > 0) {\n tokens.push(cur);\n cur = \"\";\n }\n i++;\n continue;\n }\n\n cur += ch;\n i++;\n }\n\n if (quote) {\n throw new Error(\n `shellSplit: unterminated ${quote === '\"' ? \"double\" : \"single\"} quote in input`,\n );\n }\n if (cur.length > 0) tokens.push(cur);\n return tokens;\n}\n","/**\n * Parse the `--mcp` CLI argument into a transport-tagged spec.\n *\n * Accepted forms:\n * \"name=command args...\" → stdio, namespaced (tools prefixed with `name_`)\n * \"command args...\" → stdio, anonymous\n * \"name=https://host/sse\" → SSE, namespaced\n * \"https://host/sse\" → SSE, anonymous\n * (\"http://\" is also honored — useful for local dev servers.)\n *\n * The identifier regex before `=` is deliberately narrow\n * (`[a-zA-Z_][a-zA-Z0-9_]*`) so Windows drive letters (\"C:\\\\...\") and\n * other strings containing `=` or `:` don't accidentally trigger the\n * namespace branch. If a user ever wants their command to literally start\n * with `foo=...` as a bare command, they can wrap it in quotes inside the\n * shell command string.\n *\n * Transport is selected solely by whether the body begins with `http://`\n * or `https://`. Anything else is stdio — including ws:// (unsupported)\n * which will surface later as a spawn error, keeping the rule local.\n */\n\nimport { shellSplit } from \"./shell-split.js\";\n\nexport interface StdioMcpSpec {\n transport: \"stdio\";\n /** Namespace prefix applied to each registered tool, or null if anonymous. */\n name: string | null;\n /** Argv[0]. */\n command: string;\n /** Remaining argv. */\n args: string[];\n}\n\nexport interface SseMcpSpec {\n transport: \"sse\";\n name: string | null;\n /** Fully qualified SSE endpoint URL. */\n url: string;\n}\n\nexport type McpSpec = StdioMcpSpec | SseMcpSpec;\n\nconst NAME_PREFIX = /^([a-zA-Z_][a-zA-Z0-9_]*)=(.*)$/;\nconst HTTP_URL = /^https?:\\/\\//i;\n\nexport function parseMcpSpec(input: string): McpSpec {\n const trimmed = input.trim();\n if (!trimmed) {\n throw new Error(\"empty MCP spec\");\n }\n\n const nameMatch = NAME_PREFIX.exec(trimmed);\n const name = nameMatch ? nameMatch[1]! : null;\n const body = (nameMatch ? nameMatch[2]! : trimmed).trim();\n\n if (!body) {\n throw new Error(`MCP spec has name but no command: ${input}`);\n }\n\n if (HTTP_URL.test(body)) {\n return { transport: \"sse\", name, url: body };\n }\n\n const argv = shellSplit(body);\n if (argv.length === 0) {\n throw new Error(`MCP spec has name but no command: ${input}`);\n }\n const [command, ...args] = argv;\n return { transport: \"stdio\", name, command: command!, args };\n}\n","/**\n * Gather a full inspection report from an initialized MCP client:\n * server info, capabilities, tools, resources, prompts. Methods the\n * server doesn't support come back as `{ supported: false }` instead\n * of throwing, so a CLI or UI can render a consistent \"what this\n * server exposes\" summary even against minimal implementations.\n *\n * Pure with respect to I/O beyond the passed-in client — the CLI\n * layer owns argument parsing, connection setup, and printing.\n */\n\nimport type { McpClient } from \"./client.js\";\nimport type { McpPrompt, McpResource, McpTool } from \"./types.js\";\n\nexport interface InspectionReport {\n protocolVersion: string;\n serverInfo: { name: string; version: string };\n capabilities: Record<string, unknown>;\n instructions?: string;\n tools: SectionResult<McpTool>;\n resources: SectionResult<McpResource>;\n prompts: SectionResult<McpPrompt>;\n}\n\nexport type SectionResult<T> =\n | { supported: true; items: T[] }\n | { supported: false; reason: string };\n\n/**\n * Run an inspection against a **already-initialized** client. Caller\n * is responsible for `initialize()` before this and `close()` after.\n * We keep this pure so unit tests can feed in a FakeMcpTransport and\n * verify the aggregate shape without spinning up a real process.\n */\nexport async function inspectMcpServer(client: McpClient): Promise<InspectionReport> {\n // We always *try* the three listings so the client learns whether a\n // server without explicit capability flags still serves them —\n // some servers omit capabilities but still respond to the methods.\n const tools = await trySection<McpTool>(() => client.listTools().then((r) => r.tools));\n const resources = await trySection<McpResource>(() =>\n client.listResources().then((r) => r.resources),\n );\n const prompts = await trySection<McpPrompt>(() => client.listPrompts().then((r) => r.prompts));\n\n return {\n protocolVersion: client.protocolVersion || \"(unknown)\",\n serverInfo: client.serverInfo,\n capabilities: client.serverCapabilities ?? {},\n instructions: client.serverInstructions,\n tools,\n resources,\n prompts,\n };\n}\n\nasync function trySection<T>(load: () => Promise<T[]>): Promise<SectionResult<T>> {\n try {\n const items = await load();\n return { supported: true, items };\n } catch (err) {\n const msg = (err as Error).message ?? String(err);\n // -32601 is JSON-RPC \"method not found\" — the canonical response\n // from a server that doesn't implement this family. Treat it as\n // \"not supported\" rather than a hard error, so the CLI can render\n // a clean summary instead of aborting on the first missing method.\n if (/-32601/.test(msg) || /method not found/i.test(msg)) {\n return { supported: false, reason: \"method not found (-32601)\" };\n }\n return { supported: false, reason: msg };\n }\n}\n","/**\n * Aider-style SEARCH/REPLACE edit blocks.\n *\n * The model emits blocks in this exact shape, one or more per response:\n *\n * path/to/file.ts\n * <<<<<<< SEARCH\n * exact existing lines (whitespace-sensitive)\n * =======\n * replacement lines\n * >>>>>>> REPLACE\n *\n * We chose this over unified diffs because:\n * - Models produce it reliably — no line-number drift.\n * - It tolerates multi-edit responses without ambiguity over which\n * hunk belongs to which file.\n * - Aider has years of evidence that this format works even against\n * weaker models than DeepSeek R1, so it's a conservative pick.\n *\n * The SEARCH text must match the file byte-for-byte. Empty SEARCH is a\n * sentinel for \"create new file\" — the REPLACE becomes the whole file.\n * If SEARCH doesn't match we refuse the edit and surface the failure;\n * we do NOT guess or fuzzy-match. A wrong silent edit is worse than a\n * missing one — the user can re-ask with the exact current content.\n */\n\nimport { existsSync, mkdirSync, readFileSync, unlinkSync, writeFileSync } from \"node:fs\";\nimport { dirname, resolve } from \"node:path\";\n\nexport interface EditBlock {\n /** Path as written by the model — relative to rootDir, or absolute. */\n path: string;\n /** Literal text to match in the target file. Empty → create new file. */\n search: string;\n /** Replacement text to write in place of `search`. */\n replace: string;\n /** Char offset in the source message where this block started. */\n offset: number;\n}\n\nexport type ApplyStatus =\n /** Edit landed on disk. */\n | \"applied\"\n /** New file created (SEARCH was empty and file didn't exist). */\n | \"created\"\n /** File exists but SEARCH block wasn't found in its content. */\n | \"not-found\"\n /** File doesn't exist and SEARCH was non-empty (can't create without content). */\n | \"file-missing\"\n /** Path escapes rootDir — refused on safety grounds. */\n | \"path-escape\"\n /** fs write / read threw. */\n | \"error\";\n\nexport interface ApplyResult {\n path: string;\n status: ApplyStatus;\n /** Extra detail (e.g. error message) for logs. */\n message?: string;\n}\n\n/**\n * One edit block per match. The regex is anchored to the 7-char marker\n * lines because those are visually distinct and unlikely to appear in\n * normal code.\n *\n * Anchored with `^` + `m` flag so the filename has to live on its own\n * line. Keeps us from matching e.g. a JS-import string that happens to\n * contain `<<<<<<< SEARCH` in inner text.\n */\n// `\\n?` before the =======/REPLACE separators makes the body optional:\n// empty SEARCH (new-file sentinel) works without requiring a gratuitous\n// empty line, and the same holds for empty REPLACE (file-deletion\n// semantics, not yet supported but cheaply representable).\nconst BLOCK_RE = /^(\\S[^\\n]*)\\n<{7} SEARCH\\n([\\s\\S]*?)\\n?={7}\\n([\\s\\S]*?)\\n?>{7} REPLACE/gm;\n\nexport function parseEditBlocks(text: string): EditBlock[] {\n const out: EditBlock[] = [];\n BLOCK_RE.lastIndex = 0;\n let m: RegExpExecArray | null = BLOCK_RE.exec(text);\n while (m !== null) {\n out.push({\n path: m[1]!.trim(),\n search: m[2]!,\n replace: m[3]!,\n offset: m.index,\n });\n m = BLOCK_RE.exec(text);\n }\n return out;\n}\n\nexport function applyEditBlock(block: EditBlock, rootDir: string): ApplyResult {\n const absRoot = resolve(rootDir);\n const absTarget = resolve(absRoot, block.path);\n // Refuse paths that escape rootDir. `resolve` normalizes `..`, so\n // startsWith on the normalized pair is enough.\n if (absTarget !== absRoot && !absTarget.startsWith(`${absRoot}${sep()}`)) {\n return {\n path: block.path,\n status: \"path-escape\",\n message: `resolved path ${absTarget} is outside rootDir ${absRoot}`,\n };\n }\n\n const searchEmpty = block.search.length === 0;\n const exists = existsSync(absTarget);\n\n try {\n if (!exists) {\n if (!searchEmpty) {\n return {\n path: block.path,\n status: \"file-missing\",\n message: \"file does not exist; to create it, use an empty SEARCH block\",\n };\n }\n mkdirSync(dirname(absTarget), { recursive: true });\n writeFileSync(absTarget, block.replace, \"utf8\");\n return { path: block.path, status: \"created\" };\n }\n\n const content = readFileSync(absTarget, \"utf8\");\n if (searchEmpty) {\n return {\n path: block.path,\n status: \"not-found\",\n message: \"empty SEARCH only creates new files — this file already exists\",\n };\n }\n const idx = content.indexOf(block.search);\n if (idx === -1) {\n return {\n path: block.path,\n status: \"not-found\",\n message: \"SEARCH text does not match the current file content exactly\",\n };\n }\n // Replace only the first occurrence — if the model needs multiple\n // identical edits it should emit multiple blocks (each anchored by\n // more surrounding context). Auto-expanding to replace-all is a\n // footgun when the same string legitimately appears in several\n // unrelated places.\n const replaced = `${content.slice(0, idx)}${block.replace}${content.slice(idx + block.search.length)}`;\n writeFileSync(absTarget, replaced, \"utf8\");\n return { path: block.path, status: \"applied\" };\n } catch (err) {\n return { path: block.path, status: \"error\", message: (err as Error).message };\n }\n}\n\nexport function applyEditBlocks(blocks: EditBlock[], rootDir: string): ApplyResult[] {\n return blocks.map((b) => applyEditBlock(b, rootDir));\n}\n\n// ---------- snapshot / restore (for /undo) ----------\n\nexport interface EditSnapshot {\n /** Path relative to rootDir, as the block named it. */\n path: string;\n /**\n * File content before the edit batch was applied. `null` means the\n * file didn't exist yet — restoring that means deleting whatever the\n * edit created.\n */\n prevContent: string | null;\n}\n\n/**\n * Capture the current state of every file an edit batch is about to\n * touch, so `/undo` can roll back if the user doesn't like the result.\n * De-duplicates by path because one batch can contain multiple blocks\n * for the same file, and we only want one \"before\" snapshot per file.\n */\nexport function snapshotBeforeEdits(blocks: EditBlock[], rootDir: string): EditSnapshot[] {\n const absRoot = resolve(rootDir);\n const seen = new Set<string>();\n const snapshots: EditSnapshot[] = [];\n for (const b of blocks) {\n if (seen.has(b.path)) continue;\n seen.add(b.path);\n const abs = resolve(absRoot, b.path);\n if (!existsSync(abs)) {\n snapshots.push({ path: b.path, prevContent: null });\n continue;\n }\n try {\n snapshots.push({ path: b.path, prevContent: readFileSync(abs, \"utf8\") });\n } catch {\n // Unreadable (permission / binary) — record null so we at least\n // don't pretend the snapshot is authoritative. The restore path\n // will treat null as \"delete on undo\", which is wrong in that\n // case but the file wasn't ours to begin with.\n snapshots.push({ path: b.path, prevContent: null });\n }\n }\n return snapshots;\n}\n\n/**\n * Restore files to their snapshotted state. Snapshots with\n * `prevContent === null` were created by the edit, so undo = delete.\n * Otherwise the prior content is written back, replacing whatever the\n * edit left behind.\n */\nexport function restoreSnapshots(snapshots: EditSnapshot[], rootDir: string): ApplyResult[] {\n const absRoot = resolve(rootDir);\n return snapshots.map((snap) => {\n const abs = resolve(absRoot, snap.path);\n if (abs !== absRoot && !abs.startsWith(`${absRoot}${sep()}`)) {\n return {\n path: snap.path,\n status: \"path-escape\",\n message: \"snapshot path escapes rootDir — refusing to restore\",\n };\n }\n try {\n if (snap.prevContent === null) {\n if (existsSync(abs)) unlinkSync(abs);\n return {\n path: snap.path,\n status: \"applied\",\n message: \"removed (the edit had created it)\",\n };\n }\n writeFileSync(abs, snap.prevContent, \"utf8\");\n return {\n path: snap.path,\n status: \"applied\",\n message: \"restored to pre-edit content\",\n };\n } catch (err) {\n return { path: snap.path, status: \"error\", message: (err as Error).message };\n }\n });\n}\n\n/** Platform separator — `\\` on Windows, `/` elsewhere. */\nfunction sep(): string {\n return process.platform === \"win32\" ? \"\\\\\" : \"/\";\n}\n","/**\n * Version module.\n *\n * Two jobs:\n *\n * 1. Expose `VERSION` sourced from the real `package.json` so the\n * constant never drifts from what npm publishes. Works in dev\n * (`tsx src/...`) AND after `tsup` bundles to `dist/` — both\n * layouts sit two levels below the manifest, so a short\n * walk-up finds it.\n *\n * 2. Offer an opt-in `getLatestVersion()` that hits the npm\n * registry with a bounded timeout and a 24-hour on-disk\n * cache at `~/.reasonix/version-cache.json`. Returns `null`\n * on any failure — offline / restricted-network launches\n * should stay silent rather than nag the user.\n *\n * The CLI wires `getLatestVersion` asynchronously at App mount\n * (never in a hot path) and renders the outcome in the stats\n * panel when there's a newer published version.\n */\n\nimport { existsSync, mkdirSync, readFileSync, writeFileSync } from \"node:fs\";\nimport { homedir } from \"node:os\";\nimport { dirname, join } from \"node:path\";\nimport { fileURLToPath } from \"node:url\";\n\n/** npm registry endpoint for the `latest` dist-tag of this package. */\nconst REGISTRY_URL = \"https://registry.npmjs.org/reasonix/latest\";\n\n/** TTL for the on-disk cache entry. 24h keeps noise low; users who\n * want a fresh check can run `reasonix update` which passes\n * `force: true`. */\nexport const LATEST_CACHE_TTL_MS = 24 * 60 * 60 * 1000;\n\n/** Network timeout. Short — we never block the UI waiting on this. */\nexport const LATEST_FETCH_TIMEOUT_MS = 2_000;\n\n/**\n * Walk up from the current source file looking for the `reasonix`\n * package.json. Works for:\n * - dev: `src/version.ts` → `F:/Reasonix/package.json` (2 levels up)\n * - built: `dist/index.js` → `F:/Reasonix/package.json` (2 levels up)\n * - global install: `.../node_modules/reasonix/dist/index.js` → `.../reasonix/package.json`\n *\n * The `name === \"reasonix\"` guard is a cheap safety net against\n * picking up the nearest *other* package.json if we're ever loaded\n * as a dependency and the layout is unusual.\n */\nfunction readPackageVersion(): string {\n try {\n let dir = dirname(fileURLToPath(import.meta.url));\n for (let i = 0; i < 6; i++) {\n const p = join(dir, \"package.json\");\n if (existsSync(p)) {\n const pkg = JSON.parse(readFileSync(p, \"utf8\"));\n if (pkg?.name === \"reasonix\" && typeof pkg.version === \"string\") {\n return pkg.version;\n }\n }\n const parent = dirname(dir);\n if (parent === dir) break;\n dir = parent;\n }\n } catch {\n /* fall through to fallback */\n }\n return \"0.0.0-dev\";\n}\n\nexport const VERSION: string = readPackageVersion();\n\ninterface VersionCacheEntry {\n version: string;\n /** Epoch millis the entry was written. Drives TTL comparisons. */\n checkedAt: number;\n}\n\nfunction cachePath(homeDirOverride?: string): string {\n return join(homeDirOverride ?? homedir(), \".reasonix\", \"version-cache.json\");\n}\n\nfunction readCache(homeDirOverride?: string): VersionCacheEntry | null {\n try {\n const raw = readFileSync(cachePath(homeDirOverride), \"utf8\");\n const parsed = JSON.parse(raw);\n if (parsed && typeof parsed.version === \"string\" && typeof parsed.checkedAt === \"number\") {\n return parsed;\n }\n } catch {\n /* missing or malformed → no cached entry */\n }\n return null;\n}\n\nfunction writeCache(entry: VersionCacheEntry, homeDirOverride?: string): void {\n try {\n const p = cachePath(homeDirOverride);\n mkdirSync(dirname(p), { recursive: true });\n writeFileSync(p, JSON.stringify(entry), \"utf8\");\n } catch {\n /* cache is best-effort — a failed write just means we'll re-fetch\n * next launch. No reason to surface this to the user. */\n }\n}\n\nexport interface GetLatestVersionOptions {\n /** Ignore the cached entry and always fetch fresh. Used by `reasonix update`. */\n force?: boolean;\n /** Registry URL override (tests). */\n registryUrl?: string;\n /** Home-directory override (tests). */\n homeDir?: string;\n /** Fetch implementation override (tests). Defaults to `globalThis.fetch`. */\n fetchImpl?: typeof fetch;\n /** TTL override (tests). */\n ttlMs?: number;\n /** Network timeout override (tests). */\n timeoutMs?: number;\n}\n\n/**\n * Resolve the latest published `reasonix` version from the npm registry.\n *\n * Returns `null` on any network / parse failure. Callers treat `null`\n * as \"don't know, don't nag the user.\" The cache entry is only\n * written on a successful fetch — a bad registry response won't\n * poison the cache.\n */\nexport async function getLatestVersion(opts: GetLatestVersionOptions = {}): Promise<string | null> {\n const ttl = opts.ttlMs ?? LATEST_CACHE_TTL_MS;\n if (!opts.force) {\n const cached = readCache(opts.homeDir);\n if (cached && Date.now() - cached.checkedAt < ttl) return cached.version;\n }\n\n const fetchImpl = opts.fetchImpl ?? globalThis.fetch;\n if (!fetchImpl) return null;\n const url = opts.registryUrl ?? REGISTRY_URL;\n const timeout = opts.timeoutMs ?? LATEST_FETCH_TIMEOUT_MS;\n const controller = new AbortController();\n const timer = setTimeout(() => controller.abort(), timeout);\n try {\n const res = await fetchImpl(url, {\n signal: controller.signal,\n headers: { accept: \"application/json\" },\n });\n if (!res.ok) return null;\n const body = (await res.json()) as { version?: unknown };\n if (typeof body.version !== \"string\") return null;\n writeCache({ version: body.version, checkedAt: Date.now() }, opts.homeDir);\n return body.version;\n } catch {\n return null;\n } finally {\n clearTimeout(timer);\n }\n}\n\n/**\n * Semver compare. Returns a negative number when `a < b`, positive\n * when `a > b`, zero when equal.\n *\n * Minimal pre-release handling: when the CORE (`x.y.z`) parts match,\n * any version WITH a suffix (`-rc.1`, `-alpha.4`) compares LOWER\n * than the bare version. That matches npm's dist-tag semantics —\n * `reasonix@latest` resolves to a real release, not a pre-release.\n *\n * We're deliberately not pulling in `semver` (~50KB). The three\n * cases we care about are: current > latest (future build, no\n * prompt), current < latest (prompt), current === latest (no prompt).\n */\nexport function compareVersions(a: string, b: string): number {\n const [aCore = \"0\", aPre = \"\"] = a.split(\"-\", 2);\n const [bCore = \"0\", bPre = \"\"] = b.split(\"-\", 2);\n const aParts = aCore.split(\".\").map((p) => Number.parseInt(p, 10) || 0);\n const bParts = bCore.split(\".\").map((p) => Number.parseInt(p, 10) || 0);\n for (let i = 0; i < 3; i++) {\n const diff = (aParts[i] ?? 0) - (bParts[i] ?? 0);\n if (diff !== 0) return diff;\n }\n if (!aPre && !bPre) return 0;\n if (!aPre) return 1;\n if (!bPre) return -1;\n return aPre < bPre ? -1 : aPre > bPre ? 1 : 0;\n}\n\n/**\n * Heuristic: did this process launch via `npx` / `pnpm dlx` instead\n * of a global install? The update command takes different advice in\n * each case — a global install can `npm i -g reasonix@latest`, while\n * npx just needs its cache to roll over on next launch.\n *\n * Signals checked, in order:\n * - `process.argv[1]` contains `_npx` (npm's ephemeral dir name)\n * - `process.argv[1]` contains `.pnpm` + `dlx`\n * - `npm_config_user_agent` contains `npx/`\n *\n * Any one hit → npx. False negatives are safe (worst case we suggest\n * `npm i -g` to an npx user, which is a valid way to upgrade too).\n */\nexport function isNpxInstall(): boolean {\n const bin = process.argv[1] ?? \"\";\n if (/[/\\\\]_npx[/\\\\]/.test(bin)) return true;\n if (/[/\\\\]\\.pnpm[/\\\\]/.test(bin) && /dlx/i.test(bin)) return true;\n const ua = process.env.npm_config_user_agent ?? \"\";\n if (ua.includes(\"npx/\")) return true;\n return false;\n}\n","/**\n * Persistent per-turn usage log at `~/.reasonix/usage.jsonl`.\n *\n * Each line is a single `UsageRecord` — one turn's tokens + cost\n * snapshot — appended after every `assistant_final` event. This is\n * what drives `reasonix stats` (the dashboard, no-arg form), so the\n * user can see how much they've spent vs what the equivalent Claude\n * spend would have been. The Pillar 1 pitch (94–97% cost reduction\n * vs Claude, from the v0.3 hard-number table) becomes a fact users\n * can verify on their own machine.\n *\n * Format choices:\n * - **append-only JSONL** — one line per turn, durable, survives\n * abrupt exits. A corrupted tail line loses at most one record.\n * - **flat keys, no nesting** — readable with `jq` / `cut` / `awk`;\n * the model doesn't need to parse this, humans do.\n * - **best-effort writes** — disk errors never propagate into the\n * turn. We log nothing (no `console.error`) because the TUI is\n * rendering Ink; a silent skip is the least-worst failure mode.\n * - **no PII, no prompts, no completions** — the log contains\n * tokens and costs, that's it. Sessions are identified by the\n * user-chosen name (never a prompt).\n *\n * This file is deliberately NOT wired through project memory or\n * skills — those are content pins. Usage is pure telemetry.\n */\n\nimport { appendFileSync, existsSync, mkdirSync, readFileSync, statSync } from \"node:fs\";\nimport { homedir } from \"node:os\";\nimport { dirname, join } from \"node:path\";\nimport type { Usage } from \"./client.js\";\nimport {\n CLAUDE_SONNET_PRICING,\n DEEPSEEK_PRICING,\n claudeEquivalentCost,\n costUsd,\n} from \"./telemetry.js\";\n\n/** One turn's snapshot — serialized verbatim as a JSONL line. */\nexport interface UsageRecord {\n /** Epoch millis when the record was written. */\n ts: number;\n /** Session name if the turn ran inside a persisted session, `null` for ephemeral. */\n session: string | null;\n /** Model id the turn ran against (drives the pricing lookup). */\n model: string;\n promptTokens: number;\n completionTokens: number;\n cacheHitTokens: number;\n cacheMissTokens: number;\n /** Total cost of the turn in USD. */\n costUsd: number;\n /** What the same turn would have cost at Claude Sonnet 4.6 rates. */\n claudeEquivUsd: number;\n}\n\n/** Where the log lives. Tests override via `opts.path`. */\nexport function defaultUsageLogPath(homeDirOverride?: string): string {\n return join(homeDirOverride ?? homedir(), \".reasonix\", \"usage.jsonl\");\n}\n\nexport interface AppendUsageInput {\n session: string | null;\n model: string;\n usage: Usage;\n /** Override the timestamp (tests). */\n now?: number;\n /** Override the log path (tests). */\n path?: string;\n}\n\n/**\n * Append one record and return it. Swallows disk errors — the TUI\n * should keep working even if `~/.reasonix/` is read-only.\n *\n * Returns the record that was written (or would have been written\n * if the disk had cooperated) so tests / callers can assert on the\n * computed cost fields without a round trip through the log file.\n */\nexport function appendUsage(input: AppendUsageInput): UsageRecord {\n const record: UsageRecord = {\n ts: input.now ?? Date.now(),\n session: input.session,\n model: input.model,\n promptTokens: input.usage.promptTokens,\n completionTokens: input.usage.completionTokens,\n cacheHitTokens: input.usage.promptCacheHitTokens,\n cacheMissTokens: input.usage.promptCacheMissTokens,\n costUsd: costUsd(input.model, input.usage),\n claudeEquivUsd: claudeEquivalentCost(input.usage),\n };\n\n const path = input.path ?? defaultUsageLogPath();\n try {\n mkdirSync(dirname(path), { recursive: true });\n appendFileSync(path, `${JSON.stringify(record)}\\n`, \"utf8\");\n } catch {\n /* best-effort — disk failure shouldn't break the chat */\n }\n return record;\n}\n\n/**\n * Read + parse the log. Malformed lines are silently skipped so a\n * single corrupted write (half-flushed on power loss, user hand-edit)\n * doesn't throw away the rest of the history.\n */\nexport function readUsageLog(path: string = defaultUsageLogPath()): UsageRecord[] {\n if (!existsSync(path)) return [];\n let raw: string;\n try {\n raw = readFileSync(path, \"utf8\");\n } catch {\n return [];\n }\n const out: UsageRecord[] = [];\n for (const line of raw.split(/\\r?\\n/)) {\n if (!line.trim()) continue;\n try {\n const rec = JSON.parse(line);\n if (isValidRecord(rec)) out.push(rec);\n } catch {\n /* skip malformed */\n }\n }\n return out;\n}\n\nfunction isValidRecord(rec: unknown): rec is UsageRecord {\n if (!rec || typeof rec !== \"object\") return false;\n const r = rec as Partial<UsageRecord>;\n return (\n typeof r.ts === \"number\" &&\n typeof r.model === \"string\" &&\n typeof r.promptTokens === \"number\" &&\n typeof r.completionTokens === \"number\" &&\n typeof r.cacheHitTokens === \"number\" &&\n typeof r.cacheMissTokens === \"number\" &&\n typeof r.costUsd === \"number\" &&\n typeof r.claudeEquivUsd === \"number\"\n );\n}\n\n/** One row of the `reasonix stats` dashboard — a rolled-up window. */\nexport interface UsageBucket {\n label: string;\n /** Start of the window as epoch millis. `0` = unbounded (all-time). */\n since: number;\n turns: number;\n promptTokens: number;\n completionTokens: number;\n cacheHitTokens: number;\n cacheMissTokens: number;\n costUsd: number;\n claudeEquivUsd: number;\n}\n\n/** Cache hit ratio for a bucket — zero denominator returns 0. */\nexport function bucketCacheHitRatio(b: UsageBucket): number {\n const denom = b.cacheHitTokens + b.cacheMissTokens;\n return denom > 0 ? b.cacheHitTokens / denom : 0;\n}\n\n/** Savings vs Claude as a fraction (0.94 = 94% savings). 0 if Claude cost is 0. */\nexport function bucketSavingsFraction(b: UsageBucket): number {\n return b.claudeEquivUsd > 0 ? 1 - b.costUsd / b.claudeEquivUsd : 0;\n}\n\nfunction emptyBucket(label: string, since: number): UsageBucket {\n return {\n label,\n since,\n turns: 0,\n promptTokens: 0,\n completionTokens: 0,\n cacheHitTokens: 0,\n cacheMissTokens: 0,\n costUsd: 0,\n claudeEquivUsd: 0,\n };\n}\n\nfunction addToBucket(b: UsageBucket, r: UsageRecord): void {\n b.turns += 1;\n b.promptTokens += r.promptTokens;\n b.completionTokens += r.completionTokens;\n b.cacheHitTokens += r.cacheHitTokens;\n b.cacheMissTokens += r.cacheMissTokens;\n b.costUsd += r.costUsd;\n b.claudeEquivUsd += r.claudeEquivUsd;\n}\n\nexport interface AggregateOptions {\n /** Override `Date.now()` for deterministic tests. */\n now?: number;\n}\n\nexport interface UsageAggregate {\n /** Fixed-order rolling windows: today, week, month, all-time. */\n buckets: UsageBucket[];\n /** Model id → turn count. Sorted descending; top entry is the \"most used.\" */\n byModel: Array<{ model: string; turns: number }>;\n /** Session name → turn count. Sorted descending. Null sessions are grouped under `\"(ephemeral)\"`. */\n bySession: Array<{ session: string; turns: number }>;\n /** Earliest record's ts, or `null` when the log is empty. Drives \"saved $X since <date>\". */\n firstSeen: number | null;\n /** Latest record's ts, or `null` when the log is empty. */\n lastSeen: number | null;\n}\n\n/**\n * Fold a flat record list into the dashboard shape — rolling windows\n * plus model / session histograms. Windows are INCLUSIVE of boundary:\n * - today = last 24h (rolling, not calendar-day)\n * - week = last 7d\n * - month = last 30d\n * - all = every record\n * Rolling windows avoid \"it's 00:03, 'today' is empty\" surprises.\n */\nexport function aggregateUsage(\n records: UsageRecord[],\n opts: AggregateOptions = {},\n): UsageAggregate {\n const now = opts.now ?? Date.now();\n const day = 24 * 60 * 60 * 1000;\n const today = emptyBucket(\"today\", now - day);\n const week = emptyBucket(\"week\", now - 7 * day);\n const month = emptyBucket(\"month\", now - 30 * day);\n const all = emptyBucket(\"all-time\", 0);\n\n const modelCounts = new Map<string, number>();\n const sessionCounts = new Map<string, number>();\n let firstSeen: number | null = null;\n let lastSeen: number | null = null;\n\n for (const r of records) {\n addToBucket(all, r);\n if (r.ts >= today.since) addToBucket(today, r);\n if (r.ts >= week.since) addToBucket(week, r);\n if (r.ts >= month.since) addToBucket(month, r);\n\n modelCounts.set(r.model, (modelCounts.get(r.model) ?? 0) + 1);\n const sessKey = r.session ?? \"(ephemeral)\";\n sessionCounts.set(sessKey, (sessionCounts.get(sessKey) ?? 0) + 1);\n\n if (firstSeen === null || r.ts < firstSeen) firstSeen = r.ts;\n if (lastSeen === null || r.ts > lastSeen) lastSeen = r.ts;\n }\n\n const byModel = Array.from(modelCounts.entries())\n .map(([model, turns]) => ({ model, turns }))\n .sort((a, b) => b.turns - a.turns);\n const bySession = Array.from(sessionCounts.entries())\n .map(([session, turns]) => ({ session, turns }))\n .sort((a, b) => b.turns - a.turns);\n\n return {\n buckets: [today, week, month, all],\n byModel,\n bySession,\n firstSeen,\n lastSeen,\n };\n}\n\n/** File-size helper for the stats header — \"1.2 MB\" etc. Returns \"\" if missing. */\nexport function formatLogSize(path: string = defaultUsageLogPath()): string {\n if (!existsSync(path)) return \"\";\n try {\n const s = statSync(path);\n const bytes = s.size;\n if (bytes < 1024) return `${bytes} B`;\n if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`;\n return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;\n } catch {\n return \"\";\n }\n}\n\n/** Re-exports for downstream consumers that also want the pricing constants. */\nexport { CLAUDE_SONNET_PRICING, DEEPSEEK_PRICING };\n","import { existsSync, statSync } from \"node:fs\";\nimport { render } from \"ink\";\nimport React, { useState } from \"react\";\nimport { loadApiKey, searchEnabled } from \"../../config.js\";\nimport { loadDotenv } from \"../../env.js\";\nimport { McpClient } from \"../../mcp/client.js\";\nimport { type InspectionReport, inspectMcpServer } from \"../../mcp/inspect.js\";\nimport { bridgeMcpTools } from \"../../mcp/registry.js\";\nimport { parseMcpSpec } from \"../../mcp/spec.js\";\nimport { SseTransport } from \"../../mcp/sse.js\";\nimport { type McpTransport, StdioTransport } from \"../../mcp/stdio.js\";\nimport {\n loadSessionMessages,\n rewriteSession,\n sessionPath as sessionPathOf,\n} from \"../../session.js\";\nimport { ToolRegistry } from \"../../tools.js\";\nimport { registerMemoryTools } from \"../../tools/memory.js\";\nimport { registerWebTools } from \"../../tools/web.js\";\nimport { App } from \"../ui/App.js\";\nimport { SessionPicker } from \"../ui/SessionPicker.js\";\nimport { Setup } from \"../ui/Setup.js\";\nimport type { McpServerSummary } from \"../ui/slash.js\";\n\nexport interface ProgressInfo {\n toolName: string;\n progress: number;\n total?: number;\n message?: string;\n}\n\nexport interface ChatOptions {\n model: string;\n system: string;\n transcript?: string;\n harvest?: boolean;\n branch?: number;\n session?: string;\n /** Zero or more MCP server specs. Each: `\"name=cmd args...\"` or `\"cmd args...\"`. */\n mcp?: string[];\n /** Global prefix — only used when a single anonymous server is given. */\n mcpPrefix?: string;\n /**\n * Pre-built ToolRegistry used as a seed. MCP bridges (if any) are\n * layered on top of whatever's already registered. Used by\n * `reasonix code` to register native filesystem tools in place of\n * the old `npx -y @modelcontextprotocol/server-filesystem` subprocess.\n */\n seedTools?: ToolRegistry;\n /**\n * Enable SEARCH/REPLACE edit-block processing after each assistant turn.\n * Set by `reasonix code`; plain `reasonix chat` leaves this off.\n */\n codeMode?: { rootDir: string };\n /** Skip the session picker — assume \"Resume\" (backwards-compatible auto-continue). */\n forceResume?: boolean;\n /** Skip the session picker — assume \"New\" (wipe the session file and start fresh). */\n forceNew?: boolean;\n}\n\ninterface RootProps extends ChatOptions {\n initialKey: string | undefined;\n tools: ToolRegistry | undefined;\n mcpSpecs: string[];\n mcpServers: McpServerSummary[];\n /**\n * Shared ref the bridge's `onProgress` callback writes through.\n * App sets `.current` to its own handler on mount so every\n * progress frame from any bridged tool lands in the UI's\n * `OngoingToolRow`. Ref keeps the wire-up synchronous with\n * React reconciliation (no effect-timing surprises).\n */\n progressSink: { current: ((info: ProgressInfo) => void) | null };\n /** Present when the session has prior messages; drives the picker. */\n sessionPreview?: { messageCount: number; lastActive: Date };\n}\n\nfunction Root({\n initialKey,\n tools,\n mcpSpecs,\n mcpServers,\n progressSink,\n sessionPreview,\n ...appProps\n}: RootProps) {\n const [key, setKey] = useState<string | undefined>(initialKey);\n // `null` once the picker is resolved (or was never needed). Starts as\n // the preview so we can render the picker once before mounting App.\n const [pending, setPending] = useState<typeof sessionPreview>(sessionPreview);\n\n if (!key) {\n return (\n <Setup\n onReady={(k) => {\n process.env.DEEPSEEK_API_KEY = k;\n setKey(k);\n }}\n />\n );\n }\n process.env.DEEPSEEK_API_KEY = key;\n\n if (pending && appProps.session) {\n return (\n <SessionPicker\n sessionName={appProps.session}\n messageCount={pending.messageCount}\n lastActive={pending.lastActive}\n onChoose={(choice) => {\n if (choice === \"new\" || choice === \"delete\") {\n // Wipe the session file. \"new\" and \"delete\" do the same thing\n // at this step — the distinction is only in the picker's\n // wording. A future enhancement could archive on \"new\".\n rewriteSession(appProps.session!, []);\n }\n setPending(undefined);\n }}\n />\n );\n }\n\n return (\n <App\n model={appProps.model}\n system={appProps.system}\n transcript={appProps.transcript}\n harvest={appProps.harvest}\n branch={appProps.branch}\n session={appProps.session}\n tools={tools}\n mcpSpecs={mcpSpecs}\n mcpServers={mcpServers}\n progressSink={progressSink}\n codeMode={appProps.codeMode}\n />\n );\n}\n\nexport async function chatCommand(opts: ChatOptions): Promise<void> {\n loadDotenv();\n const initialKey = loadApiKey();\n\n const requestedSpecs = opts.mcp ?? [];\n const clients: McpClient[] = [];\n const successfulSpecs: string[] = [];\n const failedSpecs: Array<{ spec: string; reason: string }> = [];\n const mcpServers: McpServerSummary[] = [];\n // Shared progress sink: the bridge's onProgress callback writes\n // through `progressSink.current`, which App.tsx sets to its UI\n // updater on mount. Started null so early progress frames (before\n // the App has mounted) are dropped rather than buffered.\n const progressSink: { current: ((info: ProgressInfo) => void) | null } = { current: null };\n // Seed registry from the caller (e.g. reasonix code's native\n // filesystem tools) — MCP bridges layer on top rather than\n // replacing. When no seed AND no MCP, tools stays undefined and\n // the loop runs as a bare chat.\n let tools: ToolRegistry | undefined = opts.seedTools;\n\n if (requestedSpecs.length > 0) {\n if (!tools) tools = new ToolRegistry();\n for (const raw of requestedSpecs) {\n try {\n const spec = parseMcpSpec(raw);\n const prefix = spec.name\n ? `${spec.name}_`\n : requestedSpecs.length === 1 && opts.mcpPrefix\n ? opts.mcpPrefix\n : \"\";\n const transport: McpTransport =\n spec.transport === \"sse\"\n ? new SseTransport({ url: spec.url })\n : new StdioTransport({ command: spec.command, args: spec.args });\n const mcp = new McpClient({ transport });\n await mcp.initialize();\n const bridge = await bridgeMcpTools(mcp, {\n registry: tools,\n namePrefix: prefix,\n onProgress: (info) => progressSink.current?.(info),\n });\n // Collect resources + prompts once at startup so the /mcp\n // slash can render them synchronously. Servers that don't\n // support these fall through as `{supported: false}` instead\n // of throwing — see inspectMcpServer.\n let report: InspectionReport;\n try {\n report = await inspectMcpServer(mcp);\n } catch {\n // If the inspect call itself fails (rare — shouldn't happen\n // since inspectMcpServer swallows -32601), synthesize a\n // minimal report so `/mcp` still has something to render.\n report = {\n protocolVersion: mcp.protocolVersion,\n serverInfo: mcp.serverInfo,\n capabilities: mcp.serverCapabilities ?? {},\n tools: { supported: true, items: [] },\n resources: { supported: false, reason: \"inspect failed\" },\n prompts: { supported: false, reason: \"inspect failed\" },\n };\n }\n const label = spec.name ?? \"anon\";\n const source =\n spec.transport === \"sse\" ? spec.url : `${spec.command} ${spec.args.join(\" \")}`;\n process.stderr.write(\n `▸ MCP[${label}]: ${bridge.registeredNames.length} tool(s) from ${source}\\n`,\n );\n clients.push(mcp);\n successfulSpecs.push(raw);\n mcpServers.push({\n label,\n spec: raw,\n toolCount: bridge.registeredNames.length,\n report,\n });\n } catch (err) {\n // Per-server failure is non-fatal: one broken server shouldn't\n // kill a chat that has working servers configured. We record\n // the failure, show a visible warning, and keep going. User\n // can fix via `reasonix setup` (unchecks the broken entry)\n // without losing their other servers.\n const reason = (err as Error).message;\n failedSpecs.push({ spec: raw, reason });\n process.stderr.write(\n `▸ MCP setup SKIPPED for \"${raw}\": ${reason}\\n → this server will not be available this session. Run \\`reasonix setup\\` to remove it, or fix the underlying issue (missing npm package, network, etc.).\\n`,\n );\n }\n }\n // If every requested server failed AND no seed registry was\n // provided, drop the empty registry so the loop still runs as a\n // bare chat instead of advertising zero tools. If the caller\n // passed seedTools we keep the registry — the seed tools are\n // still there and usable.\n if (successfulSpecs.length === 0 && !opts.seedTools) {\n tools = undefined;\n }\n }\n const mcpSpecs = successfulSpecs;\n\n // Register web search/fetch tools unless explicitly disabled. DDG\n // backs them with no key required; the model invokes them whenever\n // a question needs info fresher than its training data.\n if (searchEnabled()) {\n if (!tools) tools = new ToolRegistry();\n registerWebTools(tools);\n }\n\n // Memory tools — available in every session, not just code mode.\n // Chat-mode callers get global scope only; project scope requires\n // the seedTools path from `reasonix code` (which registers its own\n // MemoryStore bound to rootDir before chatCommand runs).\n // `run_skill` is registered later in App.tsx (where the client\n // exists) so it can wire the subagent runner for runAs:subagent\n // skills.\n if (!opts.seedTools) {\n if (!tools) tools = new ToolRegistry();\n registerMemoryTools(tools, {});\n }\n\n // Decide whether to show the session picker. It's gated on: session\n // persistence is on, the session file already has prior messages, and\n // the caller didn't pre-commit to one of the choices via --resume /\n // --new flags. `--new` wipes the file now (before the loop opens),\n // so the App mounts against a fresh log.\n let sessionPreview: { messageCount: number; lastActive: Date } | undefined;\n if (opts.session && !opts.forceResume && !opts.forceNew) {\n const prior = loadSessionMessages(opts.session);\n if (prior.length > 0) {\n const p = sessionPathOf(opts.session);\n const mtime = existsSync(p) ? statSync(p).mtime : new Date();\n sessionPreview = { messageCount: prior.length, lastActive: mtime };\n }\n } else if (opts.session && opts.forceNew) {\n rewriteSession(opts.session, []);\n }\n\n const { waitUntilExit } = render(\n <Root\n initialKey={initialKey}\n tools={tools}\n mcpSpecs={mcpSpecs}\n mcpServers={mcpServers}\n progressSink={progressSink}\n sessionPreview={sessionPreview}\n {...opts}\n />,\n // patchConsole:false — we never log to console during the TUI, and the\n // patch is a known redraw-glitch source on winpty/MINTTY terminals.\n { exitOnCtrlC: true, patchConsole: false },\n );\n try {\n await waitUntilExit();\n } finally {\n for (const c of clients) await c.close();\n }\n}\n","import type { WriteStream } from \"node:fs\";\nimport { Box, Static, Text, useApp, useInput } from \"ink\";\nimport React, { useCallback, useEffect, useMemo, useRef, useState } from \"react\";\nimport {\n type ApplyResult,\n type EditBlock,\n type EditSnapshot,\n applyEditBlocks,\n parseEditBlocks,\n restoreSnapshots,\n snapshotBeforeEdits,\n} from \"../../code/edit-blocks.js\";\nimport { addProjectShellAllowed } from \"../../config.js\";\nimport { type ResolvedHook, formatHookOutcomeMessage, loadHooks, runHooks } from \"../../hooks.js\";\nimport { CacheFirstLoop, DeepSeekClient, ImmutablePrefix } from \"../../index.js\";\nimport type { LoopEvent } from \"../../loop.js\";\nimport type { SessionSummary } from \"../../telemetry.js\";\nimport type { ToolRegistry } from \"../../tools.js\";\nimport { formatCommandResult, runCommand } from \"../../tools/shell.js\";\nimport { registerSkillTools } from \"../../tools/skills.js\";\nimport {\n type SubagentEvent,\n type SubagentSink,\n formatSubagentResult,\n spawnSubagent,\n} from \"../../tools/subagent.js\";\nimport { openTranscriptFile, recordFromLoopEvent, writeRecord } from \"../../transcript.js\";\nimport { appendUsage } from \"../../usage.js\";\nimport { VERSION, compareVersions, getLatestVersion } from \"../../version.js\";\nimport { type DisplayEvent, EventRow } from \"./EventLog.js\";\nimport { PlanConfirm, type PlanConfirmChoice } from \"./PlanConfirm.js\";\nimport { PlanRefineInput } from \"./PlanRefineInput.js\";\nimport { PromptInput } from \"./PromptInput.js\";\nimport { ShellConfirm, type ShellConfirmChoice, derivePrefix } from \"./ShellConfirm.js\";\nimport { SlashSuggestions } from \"./SlashSuggestions.js\";\nimport { StatsPanel } from \"./StatsPanel.js\";\nimport { type McpServerSummary, handleSlash, parseSlash, suggestSlashCommands } from \"./slash.js\";\nimport { TickerProvider, useElapsedSeconds, useTick } from \"./ticker.js\";\n\nexport interface AppProps {\n model: string;\n system: string;\n transcript?: string;\n harvest?: boolean;\n branch?: number;\n session?: string;\n /**\n * Pre-populated tool registry (e.g. from bridgeMcpTools()). When present,\n * its specs are folded into the ImmutablePrefix so the model sees them,\n * and its dispatch is used for tool calls — MCP tools become first-class.\n */\n tools?: ToolRegistry;\n /** Raw `--mcp` / config-derived spec strings, for `/mcp` slash display. */\n mcpSpecs?: string[];\n /**\n * Pre-captured inspection reports for each connected MCP server,\n * collected once at chat startup. Drives the rich `/mcp` slash view\n * (tools + resources + prompts per server).\n */\n mcpServers?: McpServerSummary[];\n /**\n * Shared ref the MCP bridge's onProgress callback writes through.\n * We attach our updater to `progressSink.current` on mount so any\n * `notifications/progress` frame from any bridged tool flows into\n * the UI. `null` allowed — chat mode without MCP leaves it unset.\n */\n progressSink?: {\n current:\n | ((info: { toolName: string; progress: number; total?: number; message?: string }) => void)\n | null;\n };\n /**\n * When set, parse SEARCH/REPLACE blocks from assistant responses and\n * apply them to disk under `rootDir`. Set by `reasonix code`.\n */\n codeMode?: { rootDir: string };\n}\n\n/**\n * Throttle interval in ms. We flush streaming deltas at most this often to\n * avoid re-rendering the whole UI on every single token from DeepSeek.\n * 100ms ≈ 10Hz, still feels live, gives fragile terminals (winpty/MINTTY)\n * enough room to finish a repaint before the next one arrives.\n */\nconst FLUSH_INTERVAL_MS = 100;\n\n/**\n * True when the user has opted out of live spinner/streaming rows.\n * `REASONIX_UI=plain` suppresses every transient row in the render\n * tree so only the `<Static>` committed history + the input prompt\n * are drawn. Trades liveness for stability on terminals where Ink's\n * cursor-up repaint leaves ghost artifacts.\n */\nconst PLAIN_UI = process.env.REASONIX_UI === \"plain\";\n\ninterface StreamingState {\n id: string;\n text: string;\n reasoning: string;\n toolCallBuild?: { name: string; chars: number };\n}\n\nexport function App({\n model,\n system,\n transcript,\n harvest,\n branch,\n session,\n tools,\n mcpSpecs,\n mcpServers,\n progressSink,\n codeMode,\n}: AppProps) {\n const { exit } = useApp();\n const [historical, setHistorical] = useState<DisplayEvent[]>([]);\n const [streaming, setStreaming] = useState<DisplayEvent | null>(null);\n const [input, setInput] = useState(\"\");\n const [busy, setBusy] = useState(false);\n // Tracks whether the current turn has been aborted via Esc, so the\n // Esc handler only fires once per turn (repeated presses would yield\n // stacked warning events).\n const abortedThisTurn = useRef(false);\n // Name + truncated args of the tool currently dispatching. Populated\n // on `tool_start`, cleared on `tool` (or error). Drives the\n // \"▸ tool<X> running…\" pulse-spinner row so long tool calls don't\n // look like the app hung.\n const [ongoingTool, setOngoingTool] = useState<{ name: string; args?: string } | null>(null);\n // Latest progress frame for the currently-running tool (MCP\n // `notifications/progress`). `null` when no progress has been\n // reported for this tool call — OngoingToolRow still spins, just\n // without a progress number.\n const [toolProgress, setToolProgress] = useState<{\n progress: number;\n total?: number;\n message?: string;\n } | null>(null);\n // Live state for an in-flight subagent. The subagent runs inside a\n // tool dispatch frame, so its events come in via a sink ref instead\n // of through the parent loop's event channel. `null` when no\n // subagent is currently active. A new spawn overwrites the previous\n // entry — MVP is serial, never two at once.\n const [subagentActivity, setSubagentActivity] = useState<{\n task: string;\n iter: number;\n elapsedMs: number;\n } | null>(null);\n // Transient \"what's happening\" text set by the loop during silent\n // phases (harvest round-trip, between-iteration R1 thinking, forced\n // summary). Rendered as a dim spinner row; auto-cleared on the next\n // primary event.\n const [statusLine, setStatusLine] = useState<string | null>(null);\n // DeepSeek account balance — fetched once on mount, refreshed after\n // each completed turn so the \"how much do I have left\" number\n // tracks reality. `null` means either the endpoint failed or we\n // haven't fetched yet; the panel hides the cell in that case.\n const [balance, setBalance] = useState<{ currency: string; total: number } | null>(null);\n // Latest published version the npm registry returned, REGARDLESS\n // of whether it's newer than what we're running. `null` only while\n // the background check is in flight or when the network fails —\n // so `/update` can distinguish \"on latest\" from \"still fetching\".\n // The yellow header badge is derived: it only lights up when the\n // fetched version is STRICTLY newer, but the slash surfaces the\n // raw value so the user always gets a concrete number.\n const [latestVersion, setLatestVersion] = useState<string | null>(null);\n const updateAvailable =\n latestVersion && compareVersions(VERSION, latestVersion) < 0 ? latestVersion : null;\n // Loaded user hooks (project + global settings.json). Stays mutable\n // so `/hooks reload` can rescan disk without reconstructing the\n // loop. The loop holds a parallel reference for its tool-event\n // dispatch; we keep them in sync via the effect below.\n const [hookList, setHookList] = useState<ResolvedHook[]>(() =>\n loadHooks({ projectRoot: codeMode?.rootDir }),\n );\n // Working directory reported in every hook's stdin payload. Hook\n // scripts that `cd $REASONIX_CWD` (or read `cwd` from the JSON\n // envelope) land in the project root, not the user's shell home.\n const hookCwd = codeMode?.rootDir ?? process.cwd();\n // Snapshots of every file the *last* edit batch touched, keyed by\n // nothing more than \"most recent\". `/undo` restores from this ref\n // and nulls it out — one level of undo, Aider-style. Multi-step\n // undo would need a proper history stack and a clear policy for\n // when the stack clears; v1 keeps it simple.\n const lastEditSnapshots = useRef<EditSnapshot[] | null>(null);\n // Pending edit blocks awaiting `/apply` or `/discard`. We do NOT\n // auto-apply — v0.4.1 showed that \"model proposed, so apply\" turns\n // analysis into unintended edits. The user explicitly confirms now.\n const pendingEdits = useRef<EditBlock[]>([]);\n // Shell command the model asked to run that wasn't on the auto-run\n // allowlist. Non-null renders the ShellConfirm modal and disables\n // the prompt input; the user picks Run once / Always allow in this\n // project / Deny and we feed the result back as a synthetic user\n // message so the model sees what happened.\n const [pendingShell, setPendingShell] = useState<string | null>(null);\n // Plan text the model submitted via `submit_plan` while plan mode\n // was active. Non-null renders PlanConfirm; user picks Approve /\n // Refine / Cancel and we drive the loop from there. Separate from\n // `planMode` because a pending plan is a one-shot decision even if\n // plan mode stays on (Refine keeps mode on; Approve/Cancel flip off).\n const [pendingPlan, setPendingPlan] = useState<string | null>(null);\n // Stashed plan + intent while the user types free-form feedback\n // (refinement or last instructions on approve). When the picker\n // returns \"refine\" or \"approve\", we defer the loop-resume and show\n // PlanRefineInput. User types + Enter → we ship it; Esc → restore\n // pendingPlan and re-show the picker. Letting Approve also take\n // input closes the \"model left open questions, user had no place\n // to answer them\" hole.\n const [stagedInput, setStagedInput] = useState<{\n plan: string;\n mode: \"refine\" | \"approve\";\n } | null>(null);\n // Plan-mode indicator — displayed in the StatsPanel, mirrored onto\n // the ToolRegistry so dispatch enforces read-only. Toggled via the\n // `/plan` slash and PlanConfirm picker. Ephemeral — not persisted\n // across launches (you explicitly opt in per session).\n const [planMode, setPlanMode] = useState<boolean>(false);\n // Text waiting to be submitted AFTER the current turn finishes.\n // Set by ShellConfirm's onChoose when the user approves faster than\n // the model's \"awaiting confirmation\" response. We can't call\n // handleSubmit directly because it early-returns on `busy === true`,\n // so we abort the in-flight turn and let the effect below fire the\n // submit once busy clears.\n const [queuedSubmit, setQueuedSubmit] = useState<string | null>(null);\n // Shell-style history of user prompts. ↑/↓ while idle walks it;\n // submit pushes to the end. Cursor -1 = \"live input\", 0+ = \"N turns\n // back from newest\". We don't persist history to disk — sessions\n // already keep the message log, and cross-session bash-style recall\n // would need per-project scoping we haven't designed.\n const promptHistory = useRef<string[]>([]);\n const historyCursor = useRef<number>(-1);\n // Disambiguates <Static> keys when a single turn yields multiple assistant_final events.\n const assistantIterCounter = useRef<number>(0);\n // Full untruncated tool results, in arrival order. The EventLog\n // renderer clips tool output at 400 chars for display; `/tool N`\n // reads from this ref to show the real thing. Not persisted — a\n // resumed session replays the log (which has the same content in\n // `tool` messages) but we don't repopulate this ref on resume\n // because the user wouldn't expect `/tool` to reach back across\n // process boundaries.\n const toolHistoryRef = useRef<Array<{ toolName: string; text: string }>>([]);\n // Highlighted suggestion index. ↑/↓ move within the current match\n // set while the user is typing a `/…` prefix; Enter or Tab pick.\n const [slashSelected, setSlashSelected] = useState(0);\n const [summary, setSummary] = useState<SessionSummary>({\n turns: 0,\n totalCostUsd: 0,\n totalInputCostUsd: 0,\n totalOutputCostUsd: 0,\n claudeEquivalentUsd: 0,\n savingsVsClaudePct: 0,\n cacheHitRatio: 0,\n lastPromptTokens: 0,\n });\n\n const transcriptRef = useRef<WriteStream | null>(null);\n if (transcript && !transcriptRef.current) {\n transcriptRef.current = openTranscriptFile(transcript, {\n version: 1,\n source: \"reasonix chat\",\n model,\n startedAt: new Date().toISOString(),\n });\n }\n useEffect(() => {\n return () => {\n transcriptRef.current?.end();\n };\n }, []);\n\n // The currently matching slash suggestions, or `null` when the\n // user isn't in slash-prefix mode. Shared between the useInput\n // handler (navigation + Tab-complete) and SlashSuggestions\n // (rendering + highlight) so both stay in sync.\n const slashMatches = useMemo(() => {\n if (!input.startsWith(\"/\") || input.includes(\" \")) return null;\n return suggestSlashCommands(input.slice(1), !!codeMode);\n }, [input, codeMode]);\n useEffect(() => {\n // Keep selection in range whenever the match set shrinks. Reset\n // to 0 whenever we re-enter slash mode (matches goes null → array\n // triggers the useEffect too).\n setSlashSelected((prev) => {\n if (!slashMatches || slashMatches.length === 0) return 0;\n if (prev >= slashMatches.length) return slashMatches.length - 1;\n return prev;\n });\n }, [slashMatches]);\n\n const loopRef = useRef<CacheFirstLoop | null>(null);\n // Sink the subagent tool emits live events through (`start` →\n // `progress` → `end`). App attaches its updater on first loop\n // construction; the registration captures the ref by closure so even\n // late spawns find the current handler.\n const subagentSinkRef = useRef<SubagentSink>({ current: null });\n // hookList + hookCwd intentionally NOT in deps — they seed the loop\n // on first construction (loopRef guards a single instantiation), and\n // later edits flow in through the mutable `loop.hooks = hookList`\n // effect below. Putting them in deps would tear down the loop on\n // every reload, wiping the append-only log mid-session.\n // biome-ignore lint/correctness/useExhaustiveDependencies: hookList — see comment above\n // biome-ignore lint/correctness/useExhaustiveDependencies: hookCwd — see comment above\n const loop = useMemo(() => {\n if (loopRef.current) return loopRef.current;\n const client = new DeepSeekClient();\n // Register run_skill HERE (not in code.tsx / chat.tsx) because\n // subagent-runAs skills need the client + parent registry to\n // spawn child loops. Wiring lives in App.tsx so the same code\n // path covers both code mode and chat mode.\n //\n // The closure captures `tools` (parent registry), `client`, and\n // the subagent sink ref by lexical scope — `spawnSubagent` reads\n // them per invocation, so a sink handler attached after this\n // registration still receives events.\n if (tools && !tools.has(\"run_skill\")) {\n registerSkillTools(tools, {\n projectRoot: codeMode?.rootDir,\n subagentRunner: async (skill, task) => {\n const result = await spawnSubagent({\n client,\n parentRegistry: tools,\n // Skill body is the subagent's persona/playbook; the user-\n // supplied task is what to actually do inside it.\n system: skill.body,\n task,\n // Per-skill model override (frontmatter `model: ...`),\n // else falls through to spawnSubagent's default.\n model: skill.model,\n sink: subagentSinkRef.current,\n });\n return formatSubagentResult(result);\n },\n });\n }\n const prefix = new ImmutablePrefix({\n system,\n toolSpecs: tools?.specs(),\n });\n const l = new CacheFirstLoop({\n client,\n prefix,\n tools,\n model,\n harvest,\n branch,\n session,\n hooks: hookList,\n hookCwd,\n });\n loopRef.current = l;\n return l;\n }, [model, system, harvest, branch, session, tools, codeMode]);\n\n // Keep the loop's hook list in sync after a `/hooks reload`. The\n // loop's field is intentionally mutable for exactly this case —\n // construction happens once, hook edits are picked up live.\n useEffect(() => {\n loop.hooks = hookList;\n }, [loop, hookList]);\n\n // Fetch balance once the API key is known. Non-blocking — the\n // session works without it; `null` hides the cell. We also refresh\n // after each completed turn (inside handleSubmit's finally) so the\n // number tracks actual spend rather than freezing at mount-time.\n useEffect(() => {\n let cancelled = false;\n void (async () => {\n const bal = await loop.client.getBalance().catch(() => null);\n if (cancelled || !bal || !bal.balance_infos.length) return;\n const primary = bal.balance_infos[0]!;\n setBalance({ currency: primary.currency, total: Number(primary.total_balance) });\n })();\n return () => {\n cancelled = true;\n };\n }, [loop]);\n\n // Background registry check — 24h disk cache absorbs repeated\n // launches, timeout bounded so a flaky network doesn't delay the\n // notification. Set to `null` on failure (silent: no network, no\n // problem). We store the raw version regardless of whether it's\n // newer; the header badge's newer-only check happens at the\n // `updateAvailable` derivation above.\n useEffect(() => {\n let cancelled = false;\n void (async () => {\n const latest = await getLatestVersion();\n if (cancelled || !latest) return;\n setLatestVersion(latest);\n })();\n return () => {\n cancelled = true;\n };\n }, []);\n\n // Wire the shared progressSink so the bridge's onProgress → us.\n // Only updates progress when the frame belongs to the currently-\n // running tool: late frames from a previous call shouldn't overwrite\n // the spinner of whatever's running next.\n useEffect(() => {\n if (!progressSink) return;\n progressSink.current = (info) => {\n setToolProgress({\n progress: info.progress,\n total: info.total,\n message: info.message,\n });\n };\n return () => {\n if (progressSink.current) progressSink.current = null;\n };\n }, [progressSink]);\n\n // Wire the subagent sink. `start` opens the activity row; each\n // `progress` updates iter + elapsed in place; `end` clears the row\n // and posts an info line summarizing the run. Only one subagent is\n // active at a time in the MVP, so we treat overlapping events as a\n // simple replace.\n useEffect(() => {\n subagentSinkRef.current.current = (ev: SubagentEvent) => {\n if (ev.kind === \"start\") {\n setSubagentActivity({\n task: ev.task,\n iter: ev.iter ?? 0,\n elapsedMs: ev.elapsedMs ?? 0,\n });\n return;\n }\n if (ev.kind === \"progress\") {\n setSubagentActivity({\n task: ev.task,\n iter: ev.iter ?? 0,\n elapsedMs: ev.elapsedMs ?? 0,\n });\n return;\n }\n // end\n setSubagentActivity(null);\n const seconds = ((ev.elapsedMs ?? 0) / 1000).toFixed(1);\n const summary = ev.error\n ? `🧬 subagent \"${ev.task}\" failed after ${seconds}s · ${ev.iter ?? 0} tool call(s) — ${ev.error}`\n : `🧬 subagent \"${ev.task}\" done in ${seconds}s · ${ev.iter ?? 0} tool call(s) · ${ev.turns ?? 0} turn(s)`;\n setHistorical((prev) => [\n ...prev,\n {\n id: `subagent-end-${Date.now()}`,\n role: \"info\",\n text: summary,\n },\n ]);\n };\n return () => {\n subagentSinkRef.current.current = null;\n };\n }, []);\n\n // Surface a one-time banner about session state on first mount.\n const sessionBannerShown = useRef(false);\n useEffect(() => {\n if (sessionBannerShown.current) return;\n sessionBannerShown.current = true;\n if (!session) {\n setHistorical((prev) => [\n ...prev,\n {\n id: `sys-session-${Date.now()}`,\n role: \"info\",\n text: \"▸ ephemeral chat (no session persistence) — drop --no-session to enable\",\n },\n ]);\n } else if (loop.resumedMessageCount > 0) {\n setHistorical((prev) => [\n ...prev,\n {\n id: `sys-resume-${Date.now()}`,\n role: \"info\",\n text: `▸ resumed session \"${session}\" with ${loop.resumedMessageCount} prior messages · /forget to start over · /sessions to list`,\n },\n ]);\n } else {\n setHistorical((prev) => [\n ...prev,\n {\n id: `sys-newsession-${Date.now()}`,\n role: \"info\",\n text: `▸ session \"${session}\" (new) — auto-saved as you chat · /forget to delete · /sessions to list`,\n },\n ]);\n }\n }, [session, loop]);\n\n // Esc during busy → forward to the loop as an abort signal. The loop\n // finishes the tool call in flight (we can't kill subprocess stdio\n // mid-write), then diverts to its no-tools summary path so the user\n // gets an answer instead of a hard stop. Only listens while busy so\n // we don't accidentally hijack Esc in other contexts.\n //\n // Also handles ↑/↓ shell-style history while idle. We don't use\n // ink-text-input's (absent) history support; parent-level useInput\n // is simpler and lets us own the cursor semantics.\n useInput((_input, key) => {\n if (key.escape && busy) {\n if (abortedThisTurn.current) return;\n abortedThisTurn.current = true;\n loop.abort();\n return;\n }\n if (busy) return;\n // ShellConfirm owns the full keyboard while it's showing. If we\n // kept handling ↑/↓ / Tab here they'd race with its SingleSelect\n // — the picker would move AND history recall would fire into the\n // (hidden) prompt buffer. Bail early.\n if (pendingShell) return;\n\n // Slash-suggestion mode takes priority over history recall.\n // When the user is typing a `/…` prefix and there are matches,\n // ↑/↓ walk the suggestion list and Tab snaps the input to the\n // highlighted command. Enter is handled in `handleSubmit` so\n // TextInput's onSubmit still fires cleanly.\n if (slashMatches && slashMatches.length > 0) {\n if (key.upArrow) {\n setSlashSelected((i) => Math.max(0, i - 1));\n return;\n }\n if (key.downArrow) {\n setSlashSelected((i) => Math.min(slashMatches.length - 1, i + 1));\n return;\n }\n if (key.tab) {\n const sel = slashMatches[slashSelected] ?? slashMatches[0];\n if (sel) setInput(`/${sel.cmd}`);\n return;\n }\n }\n\n // Outside slash mode: ↑/↓ recall prior prompts — but ONLY when the\n // buffer is empty. A non-empty buffer hands those keys to\n // PromptInput for cursor movement (multi-line navigation, or a\n // no-op on single-line) so the user doesn't accidentally clobber\n // typed text with a recalled prompt.\n if (input.length === 0) {\n const hist = promptHistory.current;\n if (key.upArrow) {\n if (hist.length === 0) return;\n const nextCursor = Math.min(historyCursor.current + 1, hist.length - 1);\n historyCursor.current = nextCursor;\n setInput(hist[hist.length - 1 - nextCursor] ?? \"\");\n return;\n }\n if (key.downArrow) {\n if (historyCursor.current < 0) return;\n const nextCursor = historyCursor.current - 1;\n historyCursor.current = nextCursor;\n setInput(nextCursor < 0 ? \"\" : (hist[hist.length - 1 - nextCursor] ?? \"\"));\n return;\n }\n }\n });\n\n /**\n * Callback wired into the `/undo` slash command. Restores the files\n * last edit batch to their pre-edit state and reports per-file\n * results. Only available when running in code mode — the slash\n * handler gates on this callback's presence.\n */\n const codeUndo = useCallback((): string => {\n if (!codeMode) return \"not in code mode\";\n const snaps = lastEditSnapshots.current;\n if (!snaps || snaps.length === 0) {\n return \"nothing to undo — no recent edit batch to restore\";\n }\n const results = restoreSnapshots(snaps, codeMode.rootDir);\n lastEditSnapshots.current = null;\n return formatUndoResults(results);\n }, [codeMode]);\n\n /**\n * /apply callback — write pending edit blocks to disk, snapshot\n * beforehand so /undo still works, report per-file results.\n */\n const codeApply = useCallback((): string => {\n if (!codeMode) return \"not in code mode\";\n const blocks = pendingEdits.current;\n if (blocks.length === 0) {\n return \"nothing pending — the assistant hasn't proposed edits since the last /apply or /discard.\";\n }\n const snaps = snapshotBeforeEdits(blocks, codeMode.rootDir);\n const results = applyEditBlocks(blocks, codeMode.rootDir);\n const anyApplied = results.some((r) => r.status === \"applied\" || r.status === \"created\");\n if (anyApplied) lastEditSnapshots.current = snaps;\n pendingEdits.current = [];\n return formatEditResults(results);\n }, [codeMode]);\n\n /**\n * /discard callback — forget the pending edits without touching\n * disk. Keeps the conversation going without the user having to\n * argue the model out of its proposal.\n */\n const codeDiscard = useCallback((): string => {\n const count = pendingEdits.current.length;\n if (count === 0) return \"nothing pending to discard.\";\n pendingEdits.current = [];\n return `▸ discarded ${count} pending edit block(s). Nothing was written to disk.`;\n }, []);\n\n const prefixHash = loop.prefix.fingerprint;\n\n const writeTranscript = useCallback(\n (ev: LoopEvent) => {\n const stream = transcriptRef.current;\n if (!stream) return;\n writeRecord(stream, recordFromLoopEvent(ev, { model, prefixHash }));\n },\n [model, prefixHash],\n );\n\n /**\n * Toggle plan mode on the local state AND on the ToolRegistry. The\n * registry's copy is what actually gates dispatch; the local state\n * drives the StatsPanel indicator and slash ergonomics. Kept in sync\n * by funneling every toggle through this setter.\n */\n const togglePlanMode = useCallback(\n (on: boolean) => {\n setPlanMode(on);\n tools?.setPlanMode(on);\n },\n [tools],\n );\n\n /** Clear the pending-plan picker state; safe to call unconditionally. */\n const clearPendingPlan = useCallback(() => {\n setPendingPlan(null);\n }, []);\n\n const handleSubmit = useCallback(\n async (raw: string) => {\n let text = raw.trim();\n if (!text || busy) return;\n\n // Slash auto-complete on Enter. When the user typed a prefix\n // (e.g. \"/he\") and the suggestion list is visible, substitute\n // the highlighted match so Enter runs it — same effect as Tab\n // + Enter, one keystroke less. Skip substitution if the user\n // already typed a full, exact command name (respect verbatim\n // input when they know what they want).\n if (text.startsWith(\"/\") && !text.includes(\" \")) {\n const typed = text.slice(1).toLowerCase();\n const matches = suggestSlashCommands(typed, !!codeMode);\n const exact = matches.find((m) => m.cmd === typed);\n if (!exact && matches.length > 0) {\n const chosen = matches[slashSelected] ?? matches[0];\n if (chosen) text = `/${chosen.cmd}`;\n }\n }\n\n setInput(\"\");\n historyCursor.current = -1;\n\n // Y/N fast-path when edits are pending. One keystroke is all it\n // takes to commit or drop — matches the muscle memory of `git\n // add -p` / most prompts. Deliberately scoped: only when there\n // ARE pending edits, so \"y\" as a normal message still works\n // when nothing's waiting.\n if (codeMode && pendingEdits.current.length > 0 && (text === \"y\" || text === \"n\")) {\n const out = text === \"y\" ? codeApply() : codeDiscard();\n setHistorical((prev) => [...prev, { id: `sys-${Date.now()}`, role: \"info\", text: out }]);\n promptHistory.current.push(text);\n return;\n }\n\n const slash = parseSlash(text);\n if (slash) {\n const result = handleSlash(slash.cmd, slash.args, loop, {\n mcpSpecs,\n mcpServers,\n codeUndo: codeMode ? codeUndo : undefined,\n codeApply: codeMode ? codeApply : undefined,\n codeDiscard: codeMode ? codeDiscard : undefined,\n codeRoot: codeMode?.rootDir,\n pendingEditCount: codeMode ? pendingEdits.current.length : undefined,\n toolHistory: () => toolHistoryRef.current,\n memoryRoot: codeMode?.rootDir ?? process.cwd(),\n planMode,\n setPlanMode: codeMode ? togglePlanMode : undefined,\n clearPendingPlan: codeMode ? clearPendingPlan : undefined,\n reloadHooks: () => {\n const fresh = loadHooks({ projectRoot: codeMode?.rootDir });\n setHookList(fresh);\n return fresh.length;\n },\n latestVersion,\n refreshLatestVersion: () => {\n void (async () => {\n const fresh = await getLatestVersion({ force: true });\n if (fresh) setLatestVersion(fresh);\n })();\n },\n });\n if (result.exit) {\n transcriptRef.current?.end();\n exit();\n return;\n }\n if (result.clear && result.info) {\n // Clear + message: wipe scrollback, then seed the new view\n // with the explanatory info line so the user sees *what\n // happened*. Previously clear alone left them staring at\n // an empty screen with no confirmation.\n setHistorical([\n {\n id: `sys-${Date.now()}`,\n role: \"info\",\n text: result.info,\n },\n ]);\n return;\n }\n if (result.clear) {\n setHistorical([]);\n return;\n }\n if (result.info) {\n setHistorical((prev) => [\n ...prev,\n {\n id: `sys-${Date.now()}`,\n role: \"info\",\n text: result.info!,\n },\n ]);\n }\n // `/retry` (and anything else that requests a resubmit) falls\n // through to the normal user-message flow with the provided\n // text instead of returning.\n if (result.resubmit) {\n text = result.resubmit;\n } else {\n promptHistory.current.push(text);\n return;\n }\n }\n\n // UserPromptSubmit hooks. Exit code 2 from any matching hook\n // drops the message entirely (the user's text never reaches\n // the model). Other non-zero exits surface as warning rows but\n // the prompt still goes through. We render every non-pass\n // outcome's stderr inline so a \"blocked\" choice has a visible\n // explanation.\n if (hookList.some((h) => h.event === \"UserPromptSubmit\")) {\n const promptReport = await runHooks({\n hooks: hookList,\n payload: { event: \"UserPromptSubmit\", cwd: hookCwd, prompt: text },\n });\n if (promptReport.outcomes.length > 0) {\n setHistorical((prev) => [\n ...prev,\n ...promptReport.outcomes\n .filter((o) => o.decision !== \"pass\")\n .map((o) => ({\n id: `hp-${Date.now()}-${Math.random()}`,\n role: \"warning\" as const,\n text: formatHookOutcomeMessage(o),\n })),\n ]);\n }\n if (promptReport.blocked) return;\n }\n\n // User message is immutable — push to Static immediately.\n promptHistory.current.push(text);\n setHistorical((prev) => [...prev, { id: `u-${Date.now()}`, role: \"user\", text }]);\n\n const assistantId = `a-${Date.now()}`;\n // Refs are the source of truth for accumulated streaming text; the React\n // state copy below is only for rendering and gets updated on flush.\n const streamRef: StreamingState = { id: assistantId, text: \"\", reasoning: \"\" };\n const contentBuf = { current: \"\" };\n const reasoningBuf = { current: \"\" };\n // Coalesces tool_call_delta events into one re-render per flush tick.\n const toolCallBuildBuf: { current: { name: string; chars: number } | null } = {\n current: null,\n };\n\n setStreaming({ id: assistantId, role: \"assistant\", text: \"\", streaming: true });\n setBusy(true);\n abortedThisTurn.current = false;\n\n const flush = () => {\n if (!contentBuf.current && !reasoningBuf.current && !toolCallBuildBuf.current) return;\n streamRef.text += contentBuf.current;\n streamRef.reasoning += reasoningBuf.current;\n if (toolCallBuildBuf.current) {\n streamRef.toolCallBuild = toolCallBuildBuf.current;\n }\n contentBuf.current = \"\";\n reasoningBuf.current = \"\";\n toolCallBuildBuf.current = null;\n setStreaming({\n id: assistantId,\n role: \"assistant\",\n text: streamRef.text,\n reasoning: streamRef.reasoning || undefined,\n toolCallBuild: streamRef.toolCallBuild,\n streaming: true,\n });\n };\n // In PLAIN mode the streaming row is suppressed, so flushing into\n // streamRef does no visible work — skip the interval entirely.\n const timer = PLAIN_UI ? null : setInterval(flush, FLUSH_INTERVAL_MS);\n\n try {\n for await (const ev of loop.step(text)) {\n writeTranscript(ev);\n // Status lines are transient — any primary event (streaming\n // starts, a tool fires, etc.) means whatever we were waiting\n // FOR has now arrived, so drop the hint. We do this uniformly\n // at the top of the loop body for every role except \"status\"\n // itself (which SETS the line).\n if (ev.role !== \"status\") {\n setStatusLine((cur) => (cur ? null : cur));\n }\n if (ev.role === \"status\") {\n setStatusLine(ev.content);\n } else if (ev.role === \"assistant_delta\") {\n if (ev.content) contentBuf.current += ev.content;\n if (ev.reasoningDelta) reasoningBuf.current += ev.reasoningDelta;\n } else if (ev.role === \"tool_call_delta\") {\n if (ev.toolName) {\n toolCallBuildBuf.current = {\n name: ev.toolName,\n chars: ev.toolCallArgsChars ?? 0,\n };\n }\n } else if (ev.role === \"branch_start\") {\n setStreaming({\n id: assistantId,\n role: \"assistant\",\n text: \"\",\n streaming: true,\n branchProgress: ev.branchProgress,\n });\n } else if (ev.role === \"branch_progress\") {\n // Live-update the streaming slot with per-sample completion info.\n setStreaming({\n id: assistantId,\n role: \"assistant\",\n text: \"\",\n streaming: true,\n branchProgress: ev.branchProgress,\n });\n } else if (ev.role === \"branch_done\") {\n // Intermediate: branching finished but assistant_final not yet emitted.\n // Keep streaming state alive; actual render happens on assistant_final.\n } else if (ev.role === \"assistant_final\") {\n flush();\n const repairNote = ev.repair ? describeRepair(ev.repair) : \"\";\n setStreaming(null);\n // Update the live stats panel every assistant_final — this is\n // where the loop already recorded per-iter usage. Without\n // this, cost/ctx/cache/hit stay at the PRIOR turn's numbers\n // until the whole step resolves, which is especially\n // confusing in multi-iter tool-call chains.\n setSummary(loop.stats.summary());\n // Persist a compact per-turn record to ~/.reasonix/usage.jsonl\n // so `reasonix stats` (no arg) can aggregate across every\n // session the user has ever run. Best-effort: a disk error\n // inside appendUsage is swallowed and won't break the turn.\n if (ev.stats?.usage) {\n appendUsage({\n session: session ?? null,\n model: ev.stats.model,\n usage: ev.stats.usage,\n });\n }\n const finalText = ev.content || streamRef.text;\n const iterReasoning = streamRef.reasoning || undefined;\n const iterId = `${assistantId}-i${assistantIterCounter.current++}`;\n setHistorical((prev) => [\n ...prev,\n {\n id: iterId,\n role: \"assistant\",\n text: finalText,\n reasoning: iterReasoning,\n planState: ev.planState,\n branch: ev.branch,\n stats: ev.stats,\n repair: repairNote || undefined,\n streaming: false,\n },\n ]);\n // streamRef is scoped to the whole handleSubmit call but each\n // iteration's deltas must not bleed into the next.\n streamRef.text = \"\";\n streamRef.reasoning = \"\";\n streamRef.toolCallBuild = undefined;\n contentBuf.current = \"\";\n reasoningBuf.current = \"\";\n toolCallBuildBuf.current = null;\n if (codeMode && finalText && !ev.forcedSummary) {\n // Parse SEARCH/REPLACE blocks but DO NOT write them to\n // disk. Store as pending — the user has to say /apply\n // explicitly. This prevents \"analyze the project\" from\n // silently drifting into \"edit the project\".\n //\n // `ev.forcedSummary` gates us out entirely: if the loop\n // had to force a summary (budget / aborted / context-\n // guard), its text is a wrap-up, not a plan to execute.\n // Blocks dropped in a forced summary are display-only.\n const blocks = parseEditBlocks(finalText);\n if (blocks.length > 0) {\n pendingEdits.current = blocks;\n setHistorical((prev) => [\n ...prev,\n {\n id: `pending-${Date.now()}`,\n role: \"info\",\n text: formatPendingPreview(blocks),\n },\n ]);\n }\n }\n } else if (ev.role === \"tool_start\") {\n // Kick off the visual indicator. Cleared when `tool`\n // (result) or `error` arrives, or on the finally below.\n // Also reset any lingering progress from a prior call so\n // the new spinner starts clean.\n setOngoingTool({ name: ev.toolName ?? \"?\", args: ev.toolArgs });\n setToolProgress(null);\n } else if (ev.role === \"tool\") {\n flush();\n setOngoingTool(null);\n setToolProgress(null);\n toolHistoryRef.current.push({\n toolName: ev.toolName ?? \"?\",\n text: ev.content,\n });\n setHistorical((prev) => [\n ...prev,\n {\n id: `t-${Date.now()}-${Math.random()}`,\n role: \"tool\",\n text: ev.content,\n toolName: ev.toolName,\n },\n ]);\n // run_command rejected because the command isn't on the\n // auto-allow list. Stash it so the y/n fast-path can run\n // it after user confirmation. Only the latest such request\n // is tracked — a second rejection overwrites the first.\n if (\n codeMode &&\n ev.toolName === \"run_command\" &&\n ev.content.includes('\"NeedsConfirmationError:') &&\n ev.toolArgs\n ) {\n try {\n const parsed = JSON.parse(ev.toolArgs) as { command?: unknown };\n if (typeof parsed.command === \"string\" && parsed.command.trim()) {\n setPendingShell(parsed.command.trim());\n }\n } catch {\n /* malformed args — skip the prompt */\n }\n }\n // submit_plan fired while plan mode was on — the registry\n // serialized `{ error, plan }` via PlanProposedError's\n // toToolResult(). Extract the plan and mount PlanConfirm.\n // Only the latest submission is tracked; a second overrides.\n if (\n codeMode &&\n ev.toolName === \"submit_plan\" &&\n ev.content.includes('\"PlanProposedError:')\n ) {\n try {\n const parsed = JSON.parse(ev.content) as { plan?: unknown };\n if (typeof parsed.plan === \"string\" && parsed.plan.trim()) {\n setPendingPlan(parsed.plan.trim());\n }\n } catch {\n /* malformed payload — skip the picker */\n }\n }\n } else if (ev.role === \"error\") {\n setHistorical((prev) => [\n ...prev,\n { id: `e-${Date.now()}`, role: \"error\", text: ev.error ?? ev.content },\n ]);\n } else if (ev.role === \"warning\") {\n setHistorical((prev) => [\n ...prev,\n { id: `w-${Date.now()}-${Math.random()}`, role: \"warning\", text: ev.content },\n ]);\n }\n }\n flush();\n\n // Stop hooks — turn has ended (or aborted). Block decisions are\n // meaningless past this point so we treat every non-pass as a\n // warning. Natural place for \"after every turn, run the\n // formatter / lint / tests\" automation.\n if (hookList.some((h) => h.event === \"Stop\")) {\n const stopReport = await runHooks({\n hooks: hookList,\n payload: {\n event: \"Stop\",\n cwd: hookCwd,\n lastAssistantText: streamRef.text,\n turn: loop.stats.summary().turns,\n },\n });\n for (const o of stopReport.outcomes) {\n if (o.decision === \"pass\") continue;\n setHistorical((prev) => [\n ...prev,\n {\n id: `hs-${Date.now()}-${Math.random()}`,\n role: \"warning\",\n text: formatHookOutcomeMessage(o),\n },\n ]);\n }\n }\n } finally {\n if (timer) clearInterval(timer);\n setStreaming(null);\n setOngoingTool(null);\n setToolProgress(null);\n setStatusLine(null);\n setSummary(loop.stats.summary());\n setBusy(false);\n // Refresh balance lazily — don't block the return.\n void (async () => {\n const bal = await loop.client.getBalance().catch(() => null);\n if (bal?.balance_infos.length) {\n const p = bal.balance_infos[0]!;\n setBalance({ currency: p.currency, total: Number(p.total_balance) });\n }\n })();\n }\n },\n [\n busy,\n clearPendingPlan,\n codeApply,\n codeDiscard,\n codeMode,\n codeUndo,\n exit,\n hookCwd,\n hookList,\n loop,\n latestVersion,\n mcpSpecs,\n mcpServers,\n planMode,\n session,\n slashSelected,\n togglePlanMode,\n writeTranscript,\n ],\n );\n\n /**\n * ShellConfirm callback. Three outcomes, all of them ending with a\n * synthetic user message fed back into the loop so the model sees\n * what happened next turn:\n * - deny → \"I denied running X.\" and we move on.\n * - run_once / always_allow → run the command inside the sandbox\n * root, attach the formatted output as the user turn. In the\n * always_allow case we also persist the derived prefix to\n * config so next invocation auto-runs.\n */\n const handleShellConfirm = useCallback(\n async (choice: ShellConfirmChoice) => {\n const cmd = pendingShell;\n if (!cmd || !codeMode) return;\n setPendingShell(null);\n\n let synthetic: string;\n if (choice === \"deny\") {\n setHistorical((prev) => [\n ...prev,\n { id: `sh-deny-${Date.now()}`, role: \"info\", text: `▸ denied: ${cmd}` },\n ]);\n synthetic = `I denied running \\`${cmd}\\`. Please continue without running it.`;\n } else {\n if (choice === \"always_allow\") {\n const prefix = derivePrefix(cmd);\n addProjectShellAllowed(codeMode.rootDir, prefix);\n setHistorical((prev) => [\n ...prev,\n {\n id: `sh-allow-${Date.now()}`,\n role: \"info\",\n text: `▸ always allowed \"${prefix}\" for ${codeMode.rootDir}`,\n },\n ]);\n }\n setHistorical((prev) => [\n ...prev,\n { id: `sh-run-${Date.now()}`, role: \"info\", text: `▸ running: ${cmd}` },\n ]);\n let body: string;\n try {\n const res = await runCommand(cmd, { cwd: codeMode.rootDir });\n body = formatCommandResult(cmd, res);\n } catch (err) {\n body = `$ ${cmd}\\n[failed to spawn] ${(err as Error).message}`;\n }\n setHistorical((prev) => [\n ...prev,\n { id: `sh-out-${Date.now()}`, role: \"info\", text: body },\n ]);\n synthetic = `I ran the command you requested. Output:\\n\\n${body}`;\n }\n\n // If the prior turn is still streaming (\"please confirm\" chatter),\n // handleSubmit would early-return on busy=true. Abort the in-flight\n // turn and queue the synthetic for the effect below, which fires\n // once busy clears. Otherwise submit directly.\n if (busy) {\n loop.abort();\n setQueuedSubmit(synthetic);\n } else {\n await handleSubmit(synthetic);\n }\n },\n [pendingShell, codeMode, handleSubmit, busy, loop],\n );\n\n // Drain the shell-confirm queue after the in-flight turn tears down.\n // React closure staleness means handleShellConfirm can't just await\n // the abort itself — this effect is the reliable edge detector.\n useEffect(() => {\n if (!busy && queuedSubmit !== null) {\n const text = queuedSubmit;\n setQueuedSubmit(null);\n void handleSubmit(text);\n }\n }, [busy, queuedSubmit, handleSubmit]);\n\n /**\n * PlanConfirm callback. Three outcomes, all ending with a synthetic\n * user message so the model sees the verdict on its next turn:\n * - approve → exit plan mode, tell the model to implement now.\n * - refine → stay in plan mode, tell the model to revise.\n * - cancel → exit plan mode, tell the model to drop the plan.\n * Mirrors handleShellConfirm's busy-queue dance — if the turn is\n * still streaming \"plan submitted, waiting\" chatter when the user\n * picks, we abort it and queue the synthetic for the effect above.\n *\n * `approve` is also callable with no pending plan (via the\n * `/apply-plan` slash fallback, used when the model wrote a plan in\n * assistant text instead of calling submit_plan). In that case we\n * just flip plan mode off and push the implement-now message.\n */\n const handlePlanConfirm = useCallback(\n async (choice: PlanConfirmChoice) => {\n const hadPendingPlan = pendingPlan !== null;\n if (!hadPendingPlan && choice !== \"approve\") {\n // Refine / Cancel without a pending plan is a no-op; only the\n // /apply-plan fallback makes sense without one.\n return;\n }\n\n if (choice === \"refine\" || choice === \"approve\") {\n // Two-step: stash the plan + the intent, show the input, wait\n // for user feedback before pushing anything. Approve collects\n // \"last instructions / answers to open questions\" (blank is\n // fine, user can just hit Enter). Refine collects required\n // feedback so the model has concrete guidance to revise.\n if (pendingPlan) {\n setStagedInput({ plan: pendingPlan, mode: choice });\n setPendingPlan(null);\n } else if (choice === \"approve\") {\n // /apply-plan fallback path — no pending plan, just approve.\n setStagedInput({ plan: \"\", mode: \"approve\" });\n }\n return;\n }\n\n // Cancel — no input needed, fire immediately.\n setPendingPlan(null);\n togglePlanMode(false);\n const marker = \"▸ plan cancelled\";\n const synthetic =\n \"The plan was cancelled. Drop it entirely. Ask me what I actually want before proposing another plan or making any changes.\";\n setHistorical((prev) => [\n ...prev,\n { id: `plan-${choice}-${Date.now()}`, role: \"info\", text: marker },\n ]);\n if (busy) {\n loop.abort();\n setQueuedSubmit(synthetic);\n } else {\n await handleSubmit(synthetic);\n }\n },\n [pendingPlan, togglePlanMode, busy, loop, handleSubmit],\n );\n\n /**\n * Fired when the user submits feedback from the inline input. The\n * staged `mode` decides whether this is a refine or approve: refine\n * stays in plan mode and asks the model to revise; approve exits\n * plan mode and pushes the implement synthetic, with any user\n * guidance (answers to open questions, last-minute preferences)\n * included verbatim.\n */\n const handleStagedInputSubmit = useCallback(\n async (feedback: string) => {\n const staged = stagedInput;\n setStagedInput(null);\n if (!staged) return;\n const trimmed = feedback.trim();\n\n let synthetic: string;\n let marker: string;\n if (staged.mode === \"approve\") {\n togglePlanMode(false);\n if (trimmed) {\n synthetic = `The plan above has been approved. Implement it now. You are out of plan mode — use edit_file / write_file / run_command as needed.\\n\\nUser's additional instructions / answers to your open questions:\\n\\n${trimmed}\\n\\nFactor these in before the first edit. Stick to the plan unless you discover a concrete reason to deviate; if you do, tell me and wait for a response.`;\n marker = `▸ plan approved + instructions — ${trimmed.length > 50 ? `${trimmed.slice(0, 50)}…` : trimmed}`;\n } else {\n synthetic =\n \"The plan above has been approved. Implement it now. You are out of plan mode — use edit_file / write_file / run_command as needed. If the plan listed open questions and I didn't answer them, default to the safest interpretation and call them out in your first reply. Don't fabricate preferences — if a question is truly unanswerable without me, stop and ask.\";\n marker = \"▸ plan approved — implementing\";\n }\n } else {\n // refine\n if (trimmed) {\n synthetic = `The plan needs refinement. User feedback / answers:\\n\\n${trimmed}\\n\\nStay in plan mode — address the feedback (explore more if needed), then submit an improved submit_plan call. Don't propose a near-identical plan unless you explain why the feedback doesn't apply.`;\n marker = `▸ refining — ${trimmed.length > 50 ? `${trimmed.slice(0, 50)}…` : trimmed}`;\n } else {\n synthetic =\n \"The plan needs refinement, but the user didn't give specifics. Ask them one or two concrete questions — scope, approach, file boundaries, or the risks you flagged — then wait for their answer before submitting an updated plan.\";\n marker = \"▸ refining — asking the model to clarify\";\n }\n }\n\n setHistorical((prev) => [\n ...prev,\n { id: `plan-${staged.mode}-${Date.now()}`, role: \"info\", text: marker },\n ]);\n if (busy) {\n loop.abort();\n setQueuedSubmit(synthetic);\n } else {\n await handleSubmit(synthetic);\n }\n },\n [stagedInput, togglePlanMode, busy, loop, handleSubmit],\n );\n\n /** Esc on the inline input — restore the picker without resuming. */\n const handleStagedInputCancel = useCallback(() => {\n if (stagedInput?.plan) setPendingPlan(stagedInput.plan);\n setStagedInput(null);\n }, [stagedInput]);\n\n return (\n <TickerProvider disabled={PLAIN_UI}>\n <Box flexDirection=\"column\">\n <StatsPanel\n summary={summary}\n model={loop.model}\n prefixHash={prefixHash}\n harvestOn={loop.harvestEnabled}\n branchBudget={loop.branchOptions.budget}\n planMode={planMode}\n balance={balance}\n updateAvailable={updateAvailable}\n />\n <Static items={historical}>{(item) => <EventRow key={item.id} event={item} />}</Static>\n {/*\n Live rows are hidden while the ShellConfirm modal is up — the\n model's concurrent \"please confirm\" stream is noise the user\n doesn't need, and the picker shouldn't fight it for visual\n attention. They come back naturally once the user chooses and\n the next turn begins.\n */}\n {!PLAIN_UI && !pendingShell && !pendingPlan && !stagedInput && streaming ? (\n <Box marginY={1}>\n <EventRow event={streaming} />\n </Box>\n ) : null}\n {!PLAIN_UI && !pendingShell && !pendingPlan && !stagedInput && ongoingTool ? (\n <OngoingToolRow tool={ongoingTool} progress={toolProgress} />\n ) : null}\n {!PLAIN_UI && !pendingShell && !pendingPlan && !stagedInput && subagentActivity ? (\n <SubagentRow activity={subagentActivity} />\n ) : null}\n {!PLAIN_UI &&\n !pendingShell &&\n !pendingPlan &&\n !stagedInput &&\n !ongoingTool &&\n statusLine ? (\n <StatusRow text={statusLine} />\n ) : null}\n {/*\n Belt-and-suspenders fallback: if we're busy but NONE of the\n specific indicators (streaming, ongoingTool, statusLine) is\n visible, something is still happening — show a generic\n \"processing…\" so the user never stares at a silent ticker\n without a label. Catches micro-gaps between events that the\n targeted status lines don't cover.\n */}\n {!PLAIN_UI &&\n !pendingShell &&\n !pendingPlan &&\n !stagedInput &&\n busy &&\n !streaming &&\n !ongoingTool &&\n !statusLine ? (\n <StatusRow text=\"processing…\" />\n ) : null}\n {stagedInput ? (\n <PlanRefineInput\n mode={stagedInput.mode}\n onSubmit={handleStagedInputSubmit}\n onCancel={handleStagedInputCancel}\n />\n ) : pendingPlan ? (\n <PlanConfirm plan={pendingPlan} onChoose={handlePlanConfirm} />\n ) : pendingShell ? (\n <ShellConfirm\n command={pendingShell}\n allowPrefix={derivePrefix(pendingShell)}\n onChoose={handleShellConfirm}\n />\n ) : (\n <>\n <PromptInput\n value={input}\n onChange={setInput}\n onSubmit={handleSubmit}\n disabled={busy}\n />\n <SlashSuggestions matches={slashMatches} selectedIndex={slashSelected} />\n </>\n )}\n </Box>\n </TickerProvider>\n );\n}\n\n/**\n * Live spinner row while a tool call is in flight. Without this the\n * window between the model's `tool_calls` decision and the tool's\n * result (often seconds for a multi-KB `filesystem_edit_file`) looks\n * like the app has frozen — the streaming assistant display is\n * already cleared and the input is disabled, so there's nothing to\n * look at.\n *\n * We show three signals: a braille spinner (liveness), an elapsed\n * timer in seconds (so \"long\" has a number attached), and a\n * per-tool summary of the most informative argument fields (path,\n * edits count, pattern, etc.). As of 0.4.8, MCP progress frames\n * (`notifications/progress`) land here too — bar + \"n/N\" when the\n * server reports a total, free-form message when not.\n */\n/**\n * Transient \"what's happening now\" row shown during silent phases\n * between the primary events — harvest round-trip, R1 thinking\n * about a tool result before the next streaming delta, forced\n * summary. Matches OngoingToolRow's visual language (braille\n * spinner + elapsed seconds) so the user's eyes track the same\n * spot regardless of which kind of wait it is.\n */\nconst SPINNER_FRAMES = [\"⠋\", \"⠙\", \"⠹\", \"⠸\", \"⠼\", \"⠴\", \"⠦\", \"⠧\", \"⠇\", \"⠏\"];\n\nfunction StatusRow({ text }: { text: string }) {\n const tick = useTick();\n const elapsed = useElapsedSeconds();\n return (\n <Box marginY={1}>\n <Text color=\"magenta\">{SPINNER_FRAMES[tick % SPINNER_FRAMES.length]}</Text>\n <Text color=\"magenta\">{` ${text}`}</Text>\n <Text dimColor>{` ${elapsed}s`}</Text>\n </Box>\n );\n}\n\n/**\n * Live one-line indicator for a running subagent. Sits below the\n * OngoingToolRow (the parent's tool dispatch row for `spawn_subagent`)\n * so the user sees both layers at once: outer \"spawn_subagent\n * running…\" + inner \"🧬 subagent: <task> · iter N · 12.3s\". Cleared\n * when the subagent ends; a one-line summary lands in historical.\n */\nfunction SubagentRow({\n activity,\n}: {\n activity: { task: string; iter: number; elapsedMs: number };\n}) {\n const tick = useTick();\n const seconds = (activity.elapsedMs / 1000).toFixed(1);\n return (\n <Box paddingLeft={2}>\n <Text color=\"magenta\">{SPINNER_FRAMES[tick % SPINNER_FRAMES.length]}</Text>\n <Text color=\"magenta\">{` 🧬 subagent: ${activity.task}`}</Text>\n <Text dimColor>{` · iter ${activity.iter} · ${seconds}s`}</Text>\n </Box>\n );\n}\n\nfunction OngoingToolRow({\n tool,\n progress,\n}: {\n tool: { name: string; args?: string };\n progress: { progress: number; total?: number; message?: string } | null;\n}) {\n const tick = useTick();\n const elapsed = useElapsedSeconds();\n const summary = summarizeToolArgs(tool.name, tool.args);\n return (\n <Box marginY={1} flexDirection=\"column\">\n <Box>\n <Text color=\"cyan\">{SPINNER_FRAMES[tick % SPINNER_FRAMES.length]}</Text>\n <Text color=\"yellow\">{` tool<${tool.name}> running…`}</Text>\n <Text dimColor>{` ${elapsed}s`}</Text>\n </Box>\n {progress ? (\n <Box paddingLeft={2}>\n <Text color=\"cyan\">{renderProgressLine(progress)}</Text>\n </Box>\n ) : null}\n {summary ? (\n <Box paddingLeft={2}>\n <Text dimColor>{summary}</Text>\n </Box>\n ) : null}\n </Box>\n );\n}\n\n/**\n * Turn raw JSON tool arguments into a one-line human summary. For\n * common filesystem MCP tools we pull the actually-useful fields; for\n * anything else we fall back to a truncated raw string so the user\n * still sees *something* beyond the tool name.\n *\n * Match on suffix (e.g. `_read_file`) rather than exact name because\n * `bridgeMcpTools({ namePrefix: \"filesystem_\" })` prepends the server\n * namespace — tools arrive as `filesystem_read_file` in practice but\n * callers might wire up anonymous too.\n */\n/**\n * Render an MCP progress frame as a single line. When the server\n * reports `total`, show an ASCII progress bar + \"n/total pct%\";\n * otherwise just \"progress\" + optional message. Width is modest\n * so the line fits even in a narrow terminal.\n */\nfunction renderProgressLine(p: { progress: number; total?: number; message?: string }): string {\n const msg = p.message ? ` ${p.message}` : \"\";\n if (p.total && p.total > 0) {\n const ratio = Math.max(0, Math.min(1, p.progress / p.total));\n const width = 20;\n const filled = Math.round(ratio * width);\n const bar = \"█\".repeat(filled) + \"░\".repeat(width - filled);\n const pct = (ratio * 100).toFixed(0);\n return `[${bar}] ${p.progress}/${p.total} ${pct}%${msg}`;\n }\n return `progress: ${p.progress}${msg}`;\n}\n\nfunction summarizeToolArgs(name: string, args?: string): string {\n if (!args || args === \"{}\") return \"\";\n let parsed: Record<string, unknown>;\n try {\n parsed = JSON.parse(args) as Record<string, unknown>;\n } catch {\n // Unparseable JSON — show a head slice so user at least sees\n // what the model tried.\n return args.length > 80 ? `${args.slice(0, 80)}…` : args;\n }\n const hasSuffix = (s: string) => name === s || name.endsWith(`_${s}`);\n const path = typeof parsed.path === \"string\" ? parsed.path : undefined;\n if (hasSuffix(\"read_file\")) {\n const head = typeof parsed.head === \"number\" ? `, head=${parsed.head}` : \"\";\n const tail = typeof parsed.tail === \"number\" ? `, tail=${parsed.tail}` : \"\";\n return `path: ${path ?? \"?\"}${head}${tail}`;\n }\n if (hasSuffix(\"write_file\")) {\n const content = typeof parsed.content === \"string\" ? parsed.content : \"\";\n return `path: ${path ?? \"?\"} (${content.length} chars)`;\n }\n if (hasSuffix(\"edit_file\")) {\n const edits = Array.isArray(parsed.edits) ? parsed.edits.length : 0;\n return `path: ${path ?? \"?\"} (${edits} edit${edits === 1 ? \"\" : \"s\"})`;\n }\n if (hasSuffix(\"list_directory\") || hasSuffix(\"directory_tree\")) {\n return `path: ${path ?? \"?\"}`;\n }\n if (hasSuffix(\"search_files\")) {\n const pattern = typeof parsed.pattern === \"string\" ? parsed.pattern : \"?\";\n return `path: ${path ?? \"?\"} · pattern: ${pattern}`;\n }\n if (hasSuffix(\"move_file\")) {\n const src = typeof parsed.source === \"string\" ? parsed.source : \"?\";\n const dst = typeof parsed.destination === \"string\" ? parsed.destination : \"?\";\n return `${src} → ${dst}`;\n }\n if (hasSuffix(\"get_file_info\")) {\n return `path: ${path ?? \"?\"}`;\n }\n return args.length > 80 ? `${args.slice(0, 80)}…` : args;\n}\n\n/**\n * Render a batch of SEARCH/REPLACE application results as one\n * human-scannable info line per edit. Prefixes denote status so the\n * line reads well even without color (e.g. when piped to a log file\n * or stripped for screenshots):\n * ✓ applied src/foo.ts\n * ✓ created src/new.ts\n * ✗ not-found src/bar.ts (SEARCH text does not match…)\n */\nfunction formatEditResults(results: ApplyResult[]): string {\n const lines = results.map((r) => {\n const mark = r.status === \"applied\" || r.status === \"created\" ? \"✓\" : \"✗\";\n const detail = r.message ? ` (${r.message})` : \"\";\n return ` ${mark} ${r.status.padEnd(11)} ${r.path}${detail}`;\n });\n const ok = results.filter((r) => r.status === \"applied\" || r.status === \"created\").length;\n const total = results.length;\n const header = `▸ edit blocks: ${ok}/${total} applied — /undo to roll back, or \\`git diff\\` to review`;\n return [header, ...lines].join(\"\\n\");\n}\n\n/**\n * Pending-edits preview shown after each assistant turn that proposed\n * changes. We keep it deliberately thin — path + approximate\n * ±line-count — because a full diff would crowd the TUI and the user\n * can always run `git diff` after /apply.\n */\nfunction formatPendingPreview(blocks: EditBlock[]): string {\n const lines = blocks.map((b) => {\n const removed = b.search === \"\" ? 0 : countLines(b.search);\n const added = countLines(b.replace);\n const tag = b.search === \"\" ? \"NEW \" : \" \";\n return ` ${tag}${b.path} (-${removed} +${added} lines)`;\n });\n const header = `▸ ${blocks.length} pending edit block(s) — /apply (or y) to commit · /discard (or n) to drop`;\n return [header, ...lines].join(\"\\n\");\n}\n\nfunction countLines(s: string): number {\n if (s.length === 0) return 0;\n return (s.match(/\\n/g)?.length ?? 0) + 1;\n}\n\nfunction formatUndoResults(results: ApplyResult[]): string {\n const lines = results.map((r) => {\n const mark = r.status === \"applied\" ? \"✓\" : \"✗\";\n const detail = r.message ? ` (${r.message})` : \"\";\n return ` ${mark} ${r.path}${detail}`;\n });\n return [`▸ undo: restored ${results.length} file(s) to pre-edit state`, ...lines].join(\"\\n\");\n}\n\nfunction describeRepair(repair: {\n scavenged: number;\n truncationsFixed: number;\n stormsBroken: number;\n}): string {\n const parts: string[] = [];\n if (repair.scavenged) parts.push(`scavenged ${repair.scavenged}`);\n if (repair.truncationsFixed) parts.push(`repaired ${repair.truncationsFixed} truncation`);\n if (repair.stormsBroken) parts.push(`broke ${repair.stormsBroken} storm`);\n return parts.length ? `[repair] ${parts.join(\", \")}` : \"\";\n}\n","/**\n * `run_skill` — invoke a user-authored playbook from the Skills index.\n *\n * Two execution modes, picked at registration time per skill via\n * frontmatter `runAs`:\n *\n * - `inline` (default) — the skill body becomes the tool result and\n * enters the parent's append-only log. The model reads the\n * instructions and continues the normal loop, calling whatever\n * tools the skill prescribes. Cheap, no isolation. Best for\n * \"load a checklist\" / \"load a coding style\" / \"load review\n * criteria\" patterns.\n *\n * - `subagent` — the skill body becomes the *system prompt* of an\n * isolated child loop; the user-supplied `arguments` becomes the\n * `task`. Only the child's final answer comes back as the tool\n * result. Best for big-context exploration or research playbooks\n * where the parent doesn't need to see the trail.\n *\n * Subagent dispatch is opt-in: callers must supply a `subagentRunner`\n * at registration. When omitted (chat mode without a configured\n * client), invoking a subagent skill returns a structured error\n * instead of silently downgrading to inline — that would surprise the\n * skill author.\n *\n * v1 deliberately ignores each skill's `allowed-tools` frontmatter:\n * Reasonix's tool namespace doesn't align with Claude Code's so a\n * literal pass would give wrong answers.\n */\n\nimport { type Skill, SkillStore } from \"../skills.js\";\nimport type { ToolRegistry } from \"../tools.js\";\n\n/**\n * Caller-supplied closure that knows how to spawn a subagent for the\n * given resolved skill + task. Decoupled from `registerSkillTools` so\n * the skills tool doesn't need to know about DeepSeekClient or the\n * parent ToolRegistry — the App / library wiring assembles those once\n * and hands in this function.\n *\n * Returns the JSON tool-result string verbatim (already serialized via\n * `formatSubagentResult`), so the dispatch path is pure pass-through.\n */\nexport type SubagentRunner = (skill: Skill, task: string) => Promise<string>;\n\nexport interface SkillToolsOptions {\n /** Override `$HOME` — tests set this to a tmpdir. */\n homeDir?: string;\n /**\n * Absolute project root — enables discovery of project-scope skills\n * under `<projectRoot>/.reasonix/skills/`. Omit for chat mode (global\n * scope only).\n */\n projectRoot?: string;\n /**\n * Closure that spawns a subagent for `runAs: subagent` skills. When\n * omitted, invoking a subagent skill returns an error directing the\n * user to wire up the runner — silent fallback to inline would be\n * worse, since the skill author wrote the body assuming isolation.\n */\n subagentRunner?: SubagentRunner;\n /** Hide built-in skills (test-only knob; production callers leave off). */\n disableBuiltins?: boolean;\n}\n\nexport function registerSkillTools(\n registry: ToolRegistry,\n opts: SkillToolsOptions = {},\n): ToolRegistry {\n const store = new SkillStore({\n homeDir: opts.homeDir,\n projectRoot: opts.projectRoot,\n disableBuiltins: opts.disableBuiltins,\n });\n const subagentRunner = opts.subagentRunner;\n\n registry.register({\n name: \"run_skill\",\n description:\n \"Invoke a playbook from the Skills index pinned in the system prompt. Each entry is a self-contained instruction block. Skills marked with 🧬 in the index spawn an isolated subagent — only the final distilled answer comes back, the model's tool calls + reasoning during the run never enter your context. Plain skills are inlined: the body becomes a tool result you read and follow. For 🧬 subagent skills, supply 'arguments' describing the concrete task — they'll be the only context the subagent has.\",\n readOnly: true,\n parameters: {\n type: \"object\",\n properties: {\n name: {\n type: \"string\",\n description:\n \"Skill identifier as it appears in the pinned Skills index (e.g. 'explore', 'review', 'security-review'). Case-sensitive.\",\n },\n arguments: {\n type: \"string\",\n description:\n \"Free-form arguments the skill should act on. For inline skills: appended to the body as an 'Arguments:' line; the skill's own instructions decide how to consume them. For 🧬 subagent skills: REQUIRED — becomes the entire task description the subagent receives, since it has no other context.\",\n },\n },\n required: [\"name\"],\n },\n fn: async (args: { name?: unknown; arguments?: unknown }) => {\n const name = typeof args.name === \"string\" ? args.name.trim() : \"\";\n if (!name) {\n return JSON.stringify({ error: \"run_skill requires a 'name' argument\" });\n }\n const skill = store.read(name);\n if (!skill) {\n const available = store\n .list()\n .map((s) => s.name)\n .join(\", \");\n return JSON.stringify({\n error: `unknown skill: ${JSON.stringify(name)}`,\n available: available || \"(none — user has not defined any skills)\",\n });\n }\n const rawArgs = typeof args.arguments === \"string\" ? args.arguments.trim() : \"\";\n\n if (skill.runAs === \"subagent\") {\n if (!subagentRunner) {\n return JSON.stringify({\n error: `run_skill: skill ${JSON.stringify(name)} is marked runAs=subagent but no subagent runner is configured for this session. Skill authors who need isolation should run inside reasonix code (or a library setup that passes subagentRunner to registerSkillTools).`,\n });\n }\n if (!rawArgs) {\n return JSON.stringify({\n error: `run_skill: skill ${JSON.stringify(name)} is a subagent and requires 'arguments' — the subagent has no other context, so describe the concrete task in the arguments field.`,\n });\n }\n return subagentRunner(skill, rawArgs);\n }\n\n // inline path — body becomes the tool result.\n const header = [\n `# Skill: ${skill.name}`,\n skill.description ? `> ${skill.description}` : \"\",\n `(scope: ${skill.scope} · ${skill.path})`,\n ]\n .filter(Boolean)\n .join(\"\\n\");\n const argsBlock = rawArgs ? `\\n\\nArguments: ${rawArgs}` : \"\";\n // The body is handed to the model verbatim. No truncation — the\n // user authored it, we trust their length choice. The append-only\n // log pays the token cost exactly once per invocation.\n return `${header}\\n\\n${skill.body}${argsBlock}`;\n },\n });\n\n return registry;\n}\n","import { Box, Text } from \"ink\";\nimport React from \"react\";\nimport { type TypedPlanState, isPlanStateEmpty } from \"../../harvest.js\";\nimport type { BranchProgress, BranchSummary } from \"../../loop.js\";\nimport type { TurnStats } from \"../../telemetry.js\";\nimport { PlanStateBlock } from \"./PlanStateBlock.js\";\nimport { Markdown } from \"./markdown.js\";\nimport { useElapsedSeconds, useTick } from \"./ticker.js\";\n\nexport type DisplayRole = \"user\" | \"assistant\" | \"tool\" | \"system\" | \"error\" | \"info\" | \"warning\";\n\nexport interface DisplayEvent {\n id: string;\n role: DisplayRole;\n text: string;\n reasoning?: string;\n planState?: TypedPlanState;\n branch?: BranchSummary;\n branchProgress?: BranchProgress;\n toolName?: string;\n stats?: TurnStats;\n repair?: string;\n streaming?: boolean;\n toolCallBuild?: { name: string; chars: number };\n}\n\nexport const EventRow = React.memo(function EventRow({ event }: { event: DisplayEvent }) {\n if (event.role === \"user\") {\n return (\n <Box>\n <Text bold color=\"cyan\">\n you ›{\" \"}\n </Text>\n <Text>{event.text}</Text>\n </Box>\n );\n }\n if (event.role === \"assistant\") {\n if (event.streaming) return <StreamingAssistant event={event} />;\n return (\n <Box flexDirection=\"column\" marginTop={1}>\n <Box>\n <Text bold color=\"green\">\n assistant\n </Text>\n </Box>\n {event.branch ? <BranchBlock branch={event.branch} /> : null}\n {event.reasoning ? <ReasoningBlock reasoning={event.reasoning} /> : null}\n {!isPlanStateEmpty(event.planState) ? (\n <PlanStateBlock planState={event.planState!} />\n ) : null}\n {event.text ? <Markdown text={event.text} /> : <Text dimColor>(no content)</Text>}\n {event.stats ? <StatsLine stats={event.stats} /> : null}\n {event.repair ? <Text color=\"magenta\">{event.repair}</Text> : null}\n </Box>\n );\n }\n if (event.role === \"tool\") {\n // `flattenMcpResult` prefixes server-side errors with \"ERROR: \".\n // Render those in red with a ✗ marker so they don't blend into\n // successful tool output (yellow) — the failure mode is what the\n // model most likely needs to act on next, and the user needs to\n // see at a glance.\n const isError = event.text.startsWith(\"ERROR:\");\n const color = isError ? \"red\" : \"yellow\";\n const marker = isError ? \"✗\" : \"→\";\n // `edit_file` results get a dedicated diff renderer — colored\n // line-by-line so `-` removals show red, `+` additions show\n // green, unchanged context lines dim. Always full, never\n // truncated: users need to see the whole change to trust\n // /apply. Other tools keep the 400-char clip + /tool N escape.\n const isEditFile =\n (event.toolName === \"edit_file\" || event.toolName?.endsWith(\"_edit_file\")) && !isError;\n return (\n <Box flexDirection=\"column\" marginTop={1}>\n <Text color={color}>{`tool<${event.toolName ?? \"?\"}> ${marker}`}</Text>\n {isEditFile ? (\n <EditFileDiff text={event.text} />\n ) : (\n <Text color={isError ? \"red\" : undefined} dimColor={!isError}>\n {\" \"}\n {truncate(event.text, 400)}\n </Text>\n )}\n </Box>\n );\n }\n if (event.role === \"error\") {\n return (\n <Box marginTop={1}>\n <Text color=\"red\" bold>\n error{\" \"}\n </Text>\n <Text color=\"red\">{event.text}</Text>\n </Box>\n );\n }\n if (event.role === \"info\") {\n return (\n <Box>\n <Text dimColor>{event.text}</Text>\n </Box>\n );\n }\n if (event.role === \"warning\") {\n return (\n <Box>\n <Text color=\"yellow\">▸ </Text>\n <Text color=\"yellow\">{event.text}</Text>\n </Box>\n );\n }\n return (\n <Box>\n <Text>{event.text}</Text>\n </Box>\n );\n});\n\n/**\n * Render the payload of an `edit_file` tool result with proper\n * diff coloring: first line is the header (\"edited X (A→B chars)\"),\n * subsequent lines are prefixed with `-` (removed), `+` (added), or\n * space (unchanged context). Unchanged lines render dim so the\n * changed ones pop visually. No truncation — the whole diff is\n * shown so the user can audit what landed before `/apply` or\n * `git diff` review.\n */\nfunction EditFileDiff({ text }: { text: string }) {\n const lines = text.split(/\\r?\\n/);\n // Line 0 is the status header (\"edited X (A→B chars)\"), Line 1 is\n // the unified-diff hunk header (\"@@ -N,M +N,M @@\"), the rest is\n // the colored diff body. We style the two headers distinctly so\n // the hunk marker stands out the way git-diff's cyan `@@` does in\n // most terminals.\n const [statusHeader, hunkHeader, ...body] = lines;\n return (\n <Box flexDirection=\"column\">\n <Text dimColor>{` ${statusHeader ?? \"\"}`}</Text>\n {hunkHeader !== undefined ? (\n <Text color=\"cyan\" bold>\n {hunkHeader}\n </Text>\n ) : null}\n {body.map((line, i) => {\n // Key includes the line content slice so React isn't forced\n // to treat purely-positional identity; lines in the same\n // diff don't reorder but could repeat (e.g. blank lines).\n const key = `${i}-${line.slice(0, 32)}`;\n if (line.startsWith(\"- \")) {\n return (\n <Text key={key} color=\"red\">\n {line}\n </Text>\n );\n }\n if (line.startsWith(\"+ \")) {\n return (\n <Text key={key} color=\"green\">\n {line}\n </Text>\n );\n }\n // Context line (starts with \" \") or unknown — dim.\n return (\n <Text key={key} dimColor>\n {line}\n </Text>\n );\n })}\n </Box>\n );\n}\n\nfunction BranchBlock({ branch }: { branch: BranchSummary }) {\n const per = branch.uncertainties\n .map((u, i) => {\n const marker = i === branch.chosenIndex ? \"▸\" : \" \";\n const t = (branch.temperatures[i] ?? 0).toFixed(1);\n return `${marker} #${i} T=${t} u=${u}`;\n })\n .join(\" \");\n return (\n <Box>\n <Text color=\"blue\">\n {\"🔀 branched \"}\n <Text bold>{branch.budget}</Text>\n {` samples → picked #${branch.chosenIndex} `}\n <Text dimColor>{per}</Text>\n </Text>\n </Box>\n );\n}\n\nfunction ReasoningBlock({ reasoning }: { reasoning: string }) {\n const max = 260;\n const flat = reasoning.replace(/\\s+/g, \" \").trim();\n // Show the TAIL of the reasoning rather than the head. R1 opens\n // with generic scaffolding (\"let me look at the structure...\") that\n // repeats across turns and hides the part users actually want to\n // see — the decision right before the model commits to an action.\n // Users can dump the full reasoning with `/think` if needed.\n const preview =\n flat.length <= max ? flat : `… (+${flat.length - max} earlier chars) ${flat.slice(-max)}`;\n return (\n <Box marginBottom={1}>\n <Text dimColor italic>\n {\"↳ thinking: \"}\n {preview}\n </Text>\n </Box>\n );\n}\n\n/**\n * Compact progress view rendered while a turn is still streaming. We keep\n * this to a fixed ~3-line footprint so the dynamic region never scrolls past\n * the terminal viewport and leaves artifacts in scrollback.\n */\nfunction Elapsed() {\n const s = useElapsedSeconds();\n const mm = String(Math.floor(s / 60)).padStart(2, \"0\");\n const ss = String(s % 60).padStart(2, \"0\");\n return <Text dimColor>{`${mm}:${ss}`}</Text>;\n}\n\nfunction StreamingAssistant({ event }: { event: DisplayEvent }) {\n if (event.branchProgress) {\n const p = event.branchProgress;\n // completed=0 means we've just started; no sample has finished yet.\n if (p.completed === 0) {\n return (\n <Box flexDirection=\"column\" marginTop={1}>\n <Box>\n <Text bold color=\"green\">\n assistant{\" \"}\n </Text>\n <Text color=\"blue\">\n 🔀 launching {p.total} parallel samples (R1 thinking in parallel)…{\" \"}\n </Text>\n <Elapsed />\n </Box>\n <Text dimColor>{\" \"}spread across T=0.0/0.5/1.0 · typical wait 30-90s for reasoner</Text>\n </Box>\n );\n }\n const pct = Math.round((p.completed / p.total) * 100);\n return (\n <Box flexDirection=\"column\" marginTop={1}>\n <Box>\n <Text bold color=\"green\">\n assistant{\" \"}\n </Text>\n <Text color=\"blue\">\n 🔀 branching {p.completed}/{p.total} ({pct}%){\" \"}\n </Text>\n <Elapsed />\n </Box>\n <Text dimColor>\n {\" latest #\"}\n {p.latestIndex}\n {\" T=\"}\n {p.latestTemperature.toFixed(1)}\n {\" u=\"}\n {p.latestUncertainties}\n {p.completed < p.total ? \" · waiting for other samples…\" : \" · selecting winner…\"}\n </Text>\n </Box>\n );\n }\n\n const tail = lastLine(event.text, 140);\n const reasoningTail = event.reasoning ? lastLine(event.reasoning, 120) : \"\";\n const toolCallBuild = event.toolCallBuild;\n // Four distinct phases a turn can be in — label them plainly so\n // the user doesn't have to decode \"streaming · 391 + think 4506\n // chars\" to figure out what's happening:\n // pre-first-byte: request in flight, no bytes back yet\n // reasoning-only: R1 thinking, no visible content yet\n // tool-call-only: tool_call arguments streaming, no content/reasoning bytes\n // writing: content streaming (R1 has already produced its thought)\n // both: rare — content present AND reasoning still growing\n // We can't cheaply distinguish \"reasoning still growing\" from\n // \"reasoning finished but we already saw it\", so when content is\n // present we just say \"writing response\" and surface both counts.\n const preFirstByte = !event.text && !event.reasoning && !toolCallBuild;\n const reasoningOnly = !event.text && !!event.reasoning && !toolCallBuild;\n const toolCallOnly = !event.text && !event.reasoning && !!toolCallBuild;\n let label: string;\n let labelColor: \"yellow\" | \"cyan\" | \"green\" | \"magenta\" | undefined;\n if (preFirstByte) {\n label = \"request sent · waiting for server\";\n labelColor = \"yellow\";\n } else if (reasoningOnly) {\n label = `R1 reasoning · ${event.reasoning?.length ?? 0} chars of thought`;\n labelColor = \"cyan\";\n } else if (toolCallOnly) {\n label = `assembling tool call <${toolCallBuild.name}> · ${toolCallBuild.chars} chars of arguments`;\n labelColor = \"magenta\";\n } else {\n const parts: string[] = [`writing response · ${event.text.length} chars`];\n if (event.reasoning) parts.push(`after ${event.reasoning.length} chars of reasoning`);\n if (toolCallBuild) {\n parts.push(`building tool call <${toolCallBuild.name}> · ${toolCallBuild.chars} chars`);\n }\n label = parts.join(\" · \");\n labelColor = \"green\";\n }\n return (\n <Box flexDirection=\"column\" marginTop={1}>\n <Box>\n <Text bold color=\"green\">\n assistant{\" \"}\n </Text>\n <Pulse />\n <Text color={labelColor}>{` ${label} `}</Text>\n <Elapsed />\n </Box>\n {reasoningTail ? (\n <Text dimColor italic>\n ↳ thinking: {reasoningTail}\n </Text>\n ) : null}\n {tail ? (\n <Text dimColor>▸ {tail}</Text>\n ) : preFirstByte ? (\n <Text dimColor italic>\n {\" connection open, first byte typically in 5-60s depending on model + load\"}\n </Text>\n ) : reasoningOnly ? (\n <Text color=\"yellow\" dimColor>\n {\n \" R1 is thinking before it speaks — body text starts when reasoning completes (typically 20-90s).\"\n }\n </Text>\n ) : toolCallOnly ? (\n <Text color=\"magenta\" dimColor>\n {\" tool-call arguments streaming — the model is about to dispatch a tool\"}\n </Text>\n ) : event.reasoning ? (\n <Text color=\"yellow\" dimColor>\n {\" R1 still reasoning — body text or tool call arrives when thinking completes\"}\n </Text>\n ) : null}\n </Box>\n );\n}\n\n/**\n * Blinking indicator so the user can tell the stream is alive even\n * when the reasoner hasn't produced body text yet. Uses the shared\n * tick (TICK_MS ≈ 120ms) scaled down 4× so the visible blink rate\n * lands around 500ms — feels like a heartbeat, not a progress bar.\n */\nfunction Pulse() {\n const tick = useTick();\n const frames = [\"⠋\", \"⠙\", \"⠹\", \"⠸\", \"⠼\", \"⠴\", \"⠦\", \"⠧\", \"⠇\", \"⠏\"];\n return <Text color=\"cyan\">{frames[Math.floor(tick / 4) % frames.length]}</Text>;\n}\n\nfunction lastLine(s: string, maxChars: number): string {\n const flat = s.replace(/\\s+/g, \" \").trim();\n if (!flat) return \"\";\n return flat.length <= maxChars ? flat : `…${flat.slice(-maxChars)}`;\n}\n\nfunction StatsLine({ stats }: { stats: TurnStats }) {\n const hit = (stats.cacheHitRatio * 100).toFixed(1);\n return (\n <Text dimColor>\n {\" ↳ cache \"}\n {hit}\n {\"% · tokens \"}\n {stats.usage.promptTokens}\n {\"→\"}\n {stats.usage.completionTokens}\n {\" · $\"}\n {stats.cost.toFixed(6)}\n </Text>\n );\n}\n\nfunction truncate(s: string, max: number): string {\n return s.length <= max ? s : `${s.slice(0, max)}… (+${s.length - max} chars)`;\n}\n","/**\n * Shared Ink block that renders a TypedPlanState. Used by the live chat\n * EventLog AND by the RecordView in replay/diff TUIs, so harvest output\n * looks identical live and on replay.\n *\n * Colors are semantic (not decorative):\n * - subgoals: cyan — structure / plan\n * - hypotheses: green — current beliefs (like assistant)\n * - uncertainties: yellow — attention required (like tool)\n * - rejected paths: red dim — ruled out (muted, like error-but-resolved)\n *\n * Only the label is colored + bold. The items themselves render in the\n * terminal's default foreground, so they stay readable on any background —\n * which is why the old single-magenta block was hard to see on dark themes.\n */\n\nimport { Box, Text } from \"ink\";\nimport React from \"react\";\nimport type { TypedPlanState } from \"../../harvest.js\";\n\ntype FieldColor = \"cyan\" | \"green\" | \"yellow\" | \"red\";\n\nexport function PlanStateBlock({ planState }: { planState: TypedPlanState }) {\n const fields: Array<[string, string[], FieldColor, boolean]> = [];\n if (planState.subgoals.length) fields.push([\"subgoals\", planState.subgoals, \"cyan\", false]);\n if (planState.hypotheses.length)\n fields.push([\"hypotheses\", planState.hypotheses, \"green\", false]);\n if (planState.uncertainties.length)\n fields.push([\"uncertainties\", planState.uncertainties, \"yellow\", false]);\n if (planState.rejectedPaths.length)\n fields.push([\"rejected\", planState.rejectedPaths, \"red\", true]);\n if (fields.length === 0) return null;\n\n return (\n <Box flexDirection=\"column\" marginBottom={1}>\n {fields.map(([label, items, color, dim]) => (\n <Text key={label}>\n <Text color={color} bold dimColor={dim}>\n {\"‹ \"}\n {label}\n </Text>\n <Text dimColor>{` (${items.length})`}</Text>\n <Text>{`: ${items.join(\" · \")}`}</Text>\n </Text>\n ))}\n </Box>\n );\n}\n","/**\n * Minimal Markdown → Ink renderer for chat output.\n *\n * Handles the subset that actually shows up in LLM answers:\n * - ATX headers (# ##)\n * - Unordered / ordered lists\n * - Fenced code blocks (```lang)\n * - Inline **bold**, *italic*, `code`\n * - Paragraphs separated by blank lines\n * - LaTeX delimiters are stripped (\\( \\), \\[ \\], \\boxed{X})\n *\n * The goal is not TeX-perfect math — it's \"stop showing raw backslashes to\n * the user.\" When the model insists on LaTeX, we strip the scaffolding and\n * show the expression verbatim; terminals don't do math fonts anyway.\n */\n\nimport { Box, Text } from \"ink\";\nimport React from \"react\";\n\nconst SUPERSCRIPT: Record<string, string> = {\n \"0\": \"⁰\",\n \"1\": \"¹\",\n \"2\": \"²\",\n \"3\": \"³\",\n \"4\": \"⁴\",\n \"5\": \"⁵\",\n \"6\": \"⁶\",\n \"7\": \"⁷\",\n \"8\": \"⁸\",\n \"9\": \"⁹\",\n \"+\": \"⁺\",\n \"-\": \"⁻\",\n n: \"ⁿ\",\n};\nconst SUBSCRIPT: Record<string, string> = {\n \"0\": \"₀\",\n \"1\": \"₁\",\n \"2\": \"₂\",\n \"3\": \"₃\",\n \"4\": \"₄\",\n \"5\": \"₅\",\n \"6\": \"₆\",\n \"7\": \"₇\",\n \"8\": \"₈\",\n \"9\": \"₉\",\n \"+\": \"₊\",\n \"-\": \"₋\",\n};\n\nfunction toSuperscript(s: string): string {\n let out = \"\";\n for (const c of s) out += SUPERSCRIPT[c] ?? c;\n return out;\n}\nfunction toSubscript(s: string): string {\n let out = \"\";\n for (const c of s) out += SUBSCRIPT[c] ?? c;\n return out;\n}\n\nexport function stripMath(s: string): string {\n return (\n s\n // Delimiters\n .replace(/\\\\\\(\\s*/g, \"\")\n .replace(/\\s*\\\\\\)/g, \"\")\n .replace(/\\\\\\[\\s*/g, \"\\n\")\n .replace(/\\s*\\\\\\]/g, \"\\n\")\n // Fractions — \\frac, \\dfrac, \\tfrac. Allow whitespace and one nesting\n // level inside braces (e.g. \\frac{\\sqrt{2}}{3}). Trim captured groups\n // so '\\frac{ a }{ b }' renders as '(a)/(b)'.\n .replace(\n /\\\\[dt]?frac\\s*\\{((?:[^{}]|\\{[^{}]*\\})+)\\}\\s*\\{((?:[^{}]|\\{[^{}]*\\})+)\\}/g,\n (_m, num: string, den: string) => `(${num.trim()})/(${den.trim()})`,\n )\n .replace(\n /\\\\binom\\s*\\{([^{}]+)\\}\\s*\\{([^{}]+)\\}/g,\n (_m, n: string, k: string) => `C(${n.trim()},${k.trim()})`,\n )\n .replace(/\\\\sqrt\\s*\\{([^{}]+)\\}/g, (_m, g: string) => `√(${g.trim()})`)\n .replace(/\\\\boxed\\s*\\{([^{}]+)\\}/g, (_m, g: string) => `【${g.trim()}】`)\n .replace(/\\\\text\\s*\\{([^{}]+)\\}/g, (_m, g: string) => g.trim())\n .replace(/\\\\overline\\s*\\{([^{}]+)\\}/g, (_m, g: string) => `${g.trim()}̄`)\n .replace(/\\\\hat\\s*\\{([^{}]+)\\}/g, (_m, g: string) => `${g.trim()}̂`)\n .replace(/\\\\vec\\s*\\{([^{}]+)\\}/g, (_m, g: string) => `→${g.trim()}`)\n // Operators & symbols\n .replace(/\\\\cdot/g, \"·\")\n .replace(/\\\\times/g, \"×\")\n .replace(/\\\\div/g, \"÷\")\n .replace(/\\\\pm/g, \"±\")\n .replace(/\\\\mp/g, \"∓\")\n .replace(/\\\\leq/g, \"≤\")\n .replace(/\\\\geq/g, \"≥\")\n .replace(/\\\\neq/g, \"≠\")\n .replace(/\\\\approx/g, \"≈\")\n .replace(/\\\\in\\b/g, \"∈\")\n .replace(/\\\\notin\\b/g, \"∉\")\n .replace(/\\\\infty/g, \"∞\")\n .replace(/\\\\sum\\b/g, \"Σ\")\n .replace(/\\\\prod\\b/g, \"Π\")\n .replace(/\\\\int\\b/g, \"∫\")\n // Greek letters\n .replace(/\\\\alpha/g, \"α\")\n .replace(/\\\\beta/g, \"β\")\n .replace(/\\\\gamma/g, \"γ\")\n .replace(/\\\\delta/g, \"δ\")\n .replace(/\\\\theta/g, \"θ\")\n .replace(/\\\\lambda/g, \"λ\")\n .replace(/\\\\mu/g, \"μ\")\n .replace(/\\\\pi/g, \"π\")\n .replace(/\\\\sigma/g, \"σ\")\n .replace(/\\\\phi/g, \"φ\")\n .replace(/\\\\omega/g, \"ω\")\n // Arrows / logic\n .replace(/\\\\implies\\b/g, \"⇒\")\n .replace(/\\\\iff\\b/g, \"⇔\")\n .replace(/\\\\to\\b/g, \"→\")\n .replace(/\\\\rightarrow/g, \"→\")\n .replace(/\\\\Rightarrow/g, \"⇒\")\n .replace(/\\\\leftarrow/g, \"←\")\n .replace(/\\\\Leftarrow/g, \"⇐\")\n .replace(/\\\\ldots/g, \"…\")\n .replace(/\\\\cdots/g, \"⋯\")\n // Spacing commands\n .replace(/\\\\quad/g, \" \")\n .replace(/\\\\qquad/g, \" \")\n .replace(/\\\\,/g, \" \")\n .replace(/\\\\;/g, \" \")\n .replace(/\\\\!/g, \"\")\n .replace(/\\\\\\\\/g, \"\\n\")\n // Superscripts / subscripts — single token or {braced group of [\\w+-]}\n .replace(/\\^\\{([\\w+-]+)\\}/g, (_m, g: string) => toSuperscript(g))\n .replace(/\\^([0-9+\\-n])/g, (_m, g: string) => toSuperscript(g))\n .replace(/_\\{([\\w+-]+)\\}/g, (_m, g: string) => toSubscript(g))\n .replace(/_([0-9+\\-])/g, (_m, g: string) => toSubscript(g))\n // Catch-all fallbacks for any LaTeX command we didn't explicitly handle.\n // Belt-and-braces: even if the model invents a new \\weirdcommand{x}{y},\n // we'd rather show '(x)/(y)' or 'x' than a raw backslash.\n .replace(/\\\\[a-zA-Z]+\\s*\\{([^{}]+)\\}\\s*\\{([^{}]+)\\}/g, \"($1)/($2)\")\n .replace(/\\\\[a-zA-Z]+\\s*\\{([^{}]+)\\}/g, \"$1\")\n .replace(/\\\\[a-zA-Z]+/g, \"\")\n // Collapse multiple whitespace introduced by the stripping above.\n .replace(/[ \\t]{2,}/g, \" \")\n );\n}\n\n/**\n * Split a single line into styled segments for bold / italic / inline\n * code.\n *\n * Triple-backtick (```…```) runs are matched BEFORE the single-backtick\n * case so a one-line code span like ```bash echo hi``` is captured\n * whole instead of the single-backtick regex greedily eating the\n * middle and leaving two stray backticks on each side (what 0.4.15\n * users saw when the model emitted ```bash …``` on the same line as\n * prose). Content may contain single backticks but not newlines —\n * multi-line fenced code is a block-level concern handled in\n * `parseBlocks`.\n */\nconst INLINE_RE =\n /(\\*\\*([^*\\n]+?)\\*\\*|```([^\\n]+?)```|`([^`\\n]+?)`|(?<![*\\w])\\*([^*\\n]+?)\\*(?!\\w))/g;\n\nfunction InlineMd({ text, padTo }: { text: string; padTo?: number }) {\n const parts: React.ReactNode[] = [];\n let last = 0;\n let idx = 0;\n for (const m of text.matchAll(INLINE_RE)) {\n const start = m.index ?? 0;\n if (start > last) {\n parts.push(<Text key={`t${idx++}`}>{text.slice(last, start)}</Text>);\n }\n // Groups, in the order they appear in INLINE_RE:\n // m[2] = bold content (inside ** **)\n // m[3] = triple-backtick content (strip leading lang tag)\n // m[4] = single-backtick inline code\n // m[5] = italic content (inside * *)\n if (m[2] !== undefined) {\n parts.push(\n <Text key={`b${idx++}`} bold>\n {m[2]}\n </Text>,\n );\n } else if (m[3] !== undefined) {\n // One-line fenced span: ```bash echo hi``` → drop the \"bash \"\n // language tag so the user doesn't see it rendered in code color.\n const stripped = m[3].replace(/^(\\w+)\\s+/, \"\");\n parts.push(\n <Text key={`c${idx++}`} color=\"yellow\">\n {stripped}\n </Text>,\n );\n } else if (m[4] !== undefined) {\n parts.push(\n <Text key={`c${idx++}`} color=\"yellow\">\n {m[4]}\n </Text>,\n );\n } else if (m[5] !== undefined) {\n parts.push(\n <Text key={`i${idx++}`} italic>\n {m[5]}\n </Text>,\n );\n }\n last = start + m[0].length;\n }\n if (last < text.length) {\n parts.push(<Text key={`t${idx++}`}>{text.slice(last)}</Text>);\n }\n // Trailing pad — used by table cells so column widths line up after\n // the inline markup is rendered (markup chars like `**` and `` ` ``\n // are invisible in output, so naive `pad(rawText, width)` over-pads\n // styled cells and the columns drift out of alignment).\n if (padTo !== undefined) {\n const seen = visibleWidth(text);\n if (seen < padTo) {\n parts.push(<Text key={`pad${idx++}`}>{\" \".repeat(padTo - seen)}</Text>);\n }\n }\n return <Text>{parts}</Text>;\n}\n\n/**\n * Strip inline markdown markers (**, _, single + triple backtick) so the\n * remaining text reflects what the user actually SEES on screen. Used\n * to compute correct column widths for table cells where the raw cell\n * length includes invisible markup chars.\n */\nexport function stripInlineMarkup(s: string): string {\n return s\n .replace(/\\*\\*([^*\\n]+?)\\*\\*/g, \"$1\")\n .replace(/```([^\\n]+?)```/g, (_m, c: string) => c.replace(/^(\\w+)\\s+/, \"\"))\n .replace(/`([^`\\n]+?)`/g, \"$1\")\n .replace(/(?<![*\\w])\\*([^*\\n]+?)\\*(?!\\w)/g, \"$1\");\n}\n\n/**\n * Display width AFTER stripping inline markup. The visible-on-screen\n * column width — what padding decisions should be based on.\n */\nexport function visibleWidth(s: string): number {\n return displayWidth(stripInlineMarkup(s));\n}\n\ninterface ParagraphBlock {\n kind: \"paragraph\";\n text: string;\n}\ninterface HeadingBlock {\n kind: \"heading\";\n level: number;\n text: string;\n}\ninterface BulletBlock {\n kind: \"bullet\";\n items: string[];\n ordered: boolean;\n start: number;\n}\ninterface CodeBlock {\n kind: \"code\";\n lang: string;\n text: string;\n}\ninterface HrBlock {\n kind: \"hr\";\n}\n// First-class Aider-style SEARCH/REPLACE block. We detect these at\n// parse time instead of routing them through the paragraph / inline\n// markdown path because the inline parser would otherwise eat `**`\n// inside JSDoc `/** ... *\\/` comments and `para.join(\" \")` would\n// collapse the block's newlines. Rendered as a diff so the user can\n// actually read what's about to change.\ninterface EditBlockView {\n kind: \"edit-block\";\n filename: string;\n search: string;\n replace: string;\n}\n\n/**\n * GitHub-Flavored-Markdown-ish tables. We don't do alignment flags\n * (:--- / ---:) — column-wise left-alignment is fine for a terminal\n * and the LLM rarely specifies alignment anyway. Columns grow to\n * fit the widest cell, with a hard cap so a pathological 200-char\n * cell doesn't blow past the terminal width.\n */\ninterface TableBlock {\n kind: \"table\";\n header: string[];\n rows: string[][];\n}\n\ntype Block =\n | ParagraphBlock\n | HeadingBlock\n | BulletBlock\n | CodeBlock\n | HrBlock\n | EditBlockView\n | TableBlock;\n\nexport function parseBlocks(raw: string): Block[] {\n const lines = raw.split(/\\r?\\n/);\n const out: Block[] = [];\n let para: string[] = [];\n let inCode = false;\n let codeLang = \"\";\n let codeBuf: string[] = [];\n let listBuf: BulletBlock | null = null;\n\n // Fence length of the currently-open code block so a block opened\n // with ````` closes only on `````, matching GFM. Empty when we're\n // not in code mode.\n let codeFence = \"\";\n\n const flushPara = () => {\n if (para.length) {\n out.push({ kind: \"paragraph\", text: para.join(\" \") });\n para = [];\n }\n };\n const flushList = () => {\n if (listBuf) {\n out.push(listBuf);\n listBuf = null;\n }\n };\n\n for (let i = 0; i < lines.length; i++) {\n const rawLine = lines[i]!;\n const line = rawLine.replace(/\\s+$/g, \"\");\n\n // Detect Aider-style SEARCH/REPLACE block. Matches the preceding\n // non-blank line as the filename, then `<<<<<<< SEARCH`, content,\n // `=======`, content, `>>>>>>> REPLACE`. We don't do markdown\n // inside — neither the paragraph nor inline parsers should touch\n // this content.\n if (!inCode && /^<{7} SEARCH\\s*$/.test(line)) {\n // Filename is the previous non-blank line we just pushed to para.\n // Pull it back out; if there isn't one, treat as literal text.\n const filename = para.pop()?.trim();\n if (filename) {\n flushPara();\n flushList();\n let j = i + 1;\n const searchLines: string[] = [];\n while (j < lines.length && !/^={7}\\s*$/.test(lines[j]!)) {\n searchLines.push(lines[j]!);\n j++;\n }\n const replaceLines: string[] = [];\n let k = j + 1;\n while (k < lines.length && !/^>{7} REPLACE\\s*$/.test(lines[k]!)) {\n replaceLines.push(lines[k]!);\n k++;\n }\n if (j < lines.length && k < lines.length) {\n out.push({\n kind: \"edit-block\",\n filename,\n search: searchLines.join(\"\\n\"),\n replace: replaceLines.join(\"\\n\"),\n });\n i = k;\n continue;\n }\n // Malformed — no separator or no close. Fall through: put\n // the filename back in the paragraph so we don't lose it.\n para.push(filename);\n }\n }\n\n // Fenced code block (GFM). The fence is 3+ backticks, may have up\n // to 3 leading spaces, and carries an optional language tag. A\n // closing fence must be the SAME backtick run length or longer.\n //\n // Two paths:\n // a) Fence on its own line → multi-line block, accumulate until\n // a matching close fence.\n // b) Fence opens AND closes on the same line (e.g.\n // ```bash echo hi```) → emit as a one-line code block so\n // the inline parser doesn't half-eat the backticks.\n if (!inCode) {\n const open = line.match(/^ {0,3}(`{3,})(\\w*)\\s*(.*)$/);\n if (open) {\n const fence = open[1]!;\n const lang = open[2] ?? \"\";\n const rest = open[3] ?? \"\";\n const closeOnSame = rest.match(new RegExp(`^(.*?)${fence}\\\\s*$`));\n if (closeOnSame) {\n flushPara();\n flushList();\n out.push({ kind: \"code\", lang, text: (closeOnSame[1] ?? \"\").trim() });\n continue;\n }\n flushPara();\n flushList();\n inCode = true;\n codeLang = lang;\n codeFence = fence;\n // Anything after the opening fence on the SAME line is\n // still body content (rare but legal).\n if (rest.length > 0) codeBuf.push(rest);\n continue;\n }\n } else {\n // In code mode — check for closing fence. Same indent rules as\n // opening, and the backtick run must be at least as long.\n const close = line.match(/^ {0,3}(`{3,})\\s*$/);\n if (close && close[1]!.length >= codeFence.length) {\n out.push({ kind: \"code\", lang: codeLang, text: codeBuf.join(\"\\n\") });\n codeBuf = [];\n codeLang = \"\";\n codeFence = \"\";\n inCode = false;\n continue;\n }\n codeBuf.push(rawLine);\n continue;\n }\n\n if (line.trim() === \"\") {\n flushPara();\n flushList();\n continue;\n }\n\n if (/^[-*_]{3,}\\s*$/.test(line)) {\n flushPara();\n flushList();\n out.push({ kind: \"hr\" });\n continue;\n }\n\n const hm = line.match(/^(#{1,6})\\s+(.+)$/);\n if (hm) {\n flushPara();\n flushList();\n out.push({ kind: \"heading\", level: hm[1]!.length, text: hm[2]!.trim() });\n continue;\n }\n\n // Box-drawing frame detection: top edge `┌────┐`, body lines that\n // each begin and end with `│`, bottom edge `└────┘`. Models love to\n // draw decorative frames around code snippets and flow charts using\n // these characters; without this branch, every body line gets\n // word-wrapped by Ink and the frame turns into garbage. Rendering\n // the inner content as a code block preserves the fixed-width\n // layout the model intended AND gives it a real border via the\n // existing code-block renderer. Only triggers OUTSIDE code mode\n // (where `inCode` is false) so a literal box-drawing character\n // inside a real fenced block isn't grabbed.\n if (/^\\s*┌─+┐\\s*$/.test(line)) {\n let j = i + 1;\n const bodyLines: string[] = [];\n while (j < lines.length && !/^\\s*└─+┘\\s*$/.test(lines[j]!)) {\n const inner = lines[j]!;\n // Strip outer `│ ... │` so the content reads naturally.\n const m = inner.match(/^\\s*│\\s?(.*?)\\s?│\\s*$/);\n bodyLines.push(m ? (m[1] ?? \"\") : inner);\n j++;\n }\n if (j < lines.length) {\n flushPara();\n flushList();\n out.push({ kind: \"code\", lang: \"\", text: bodyLines.join(\"\\n\") });\n i = j;\n continue;\n }\n // No closing edge — fall through and let the line render as\n // paragraph rather than eating to EOF.\n }\n\n // Table detection: a line with at least one column separator where\n // the NEXT line looks like a separator row. Two flavors accepted:\n //\n // - Standard GFM: `|` columns + `---` / `:---:` separators.\n // - Unicode box-drawing: `│` columns (U+2502) + `─` / `┼` (U+2500\n // / U+253C) separators. Models trained on Chinese text routinely\n // pick the box-drawing characters even when GFM was an option;\n // accepting both keeps their output legible without forcing a\n // re-prompt. `splitTableRow` normalizes `│` → `|` so the rest of\n // the path stays uniform.\n //\n // Both the header row and the separator must be present — a bare\n // pipe in prose shouldn't trigger the table path.\n if (line.includes(\"|\") || line.includes(\"│\")) {\n const next = (lines[i + 1] ?? \"\").trim();\n const isGfmSep = /^\\|?\\s*:?-{2,}:?\\s*(\\|\\s*:?-{2,}:?\\s*)+\\|?\\s*$/.test(next);\n const isBoxSep = /^[│─┼┬┴┌┐└┘├┤\\s]+$/.test(next) && /─{2,}/.test(next);\n if (isGfmSep || isBoxSep) {\n flushPara();\n flushList();\n const header = splitTableRow(line);\n const colCount = header.length;\n const rows: string[][] = [];\n let j = i + 2; // skip header + separator\n while (j < lines.length) {\n const r = lines[j]!.replace(/\\s+$/g, \"\");\n if (r.trim() === \"\") break;\n if (!r.includes(\"|\") && !r.includes(\"│\")) {\n // Continuation row: model wrapped a long cell across lines\n // without re-emitting the column separator. Fold this line\n // back into the LAST cell of the previous row so its inline\n // markup (backticks, bold) parses as one piece instead of\n // bleeding into the paragraph stream below the table.\n const prev = rows[rows.length - 1];\n if (prev && prev.length === colCount) {\n const lastIdx = prev.length - 1;\n prev[lastIdx] = `${prev[lastIdx] ?? \"\"} ${r.trim()}`;\n j++;\n continue;\n }\n break;\n }\n rows.push(splitTableRow(r));\n j++;\n }\n out.push({ kind: \"table\", header, rows });\n i = j - 1;\n continue;\n }\n }\n\n const bm = line.match(/^\\s*[-*+]\\s+(.+)$/);\n if (bm) {\n flushPara();\n if (!listBuf || listBuf.ordered) {\n flushList();\n listBuf = { kind: \"bullet\", items: [], ordered: false, start: 1 };\n }\n listBuf.items.push(bm[1]!);\n continue;\n }\n\n const om = line.match(/^\\s*(\\d+)\\.\\s+(.+)$/);\n if (om) {\n flushPara();\n if (!listBuf || !listBuf.ordered) {\n flushList();\n listBuf = { kind: \"bullet\", items: [], ordered: true, start: Number(om[1]) };\n }\n listBuf.items.push(om[2]!);\n continue;\n }\n\n flushList();\n para.push(line);\n }\n\n if (inCode && codeBuf.length) {\n out.push({ kind: \"code\", lang: codeLang, text: codeBuf.join(\"\\n\") });\n }\n flushPara();\n flushList();\n return out;\n}\n\nfunction BlockView({ block }: { block: Block }) {\n switch (block.kind) {\n case \"heading\":\n return (\n <Text bold color=\"cyan\">\n <InlineMd text={block.text} />\n </Text>\n );\n case \"paragraph\":\n return <InlineMd text={block.text} />;\n case \"bullet\":\n return (\n <Box flexDirection=\"column\">\n {block.items.map((item, i) => (\n <Box key={`${i}-${item.slice(0, 24)}`}>\n <Text color=\"cyan\">{block.ordered ? ` ${block.start + i}. ` : \" • \"}</Text>\n <InlineMd text={item} />\n </Box>\n ))}\n </Box>\n );\n case \"code\":\n return (\n <Box borderStyle=\"single\" borderColor=\"gray\" paddingX={1}>\n <Text color=\"yellow\">{block.text}</Text>\n </Box>\n );\n case \"edit-block\":\n return <EditBlockRow block={block} />;\n case \"table\":\n return <TableBlockRow block={block} />;\n case \"hr\":\n return <Text dimColor>{\"────────────────────────\"}</Text>;\n }\n}\n\n/**\n * Split one table row into trimmed cells. Leading/trailing column\n * markers are optional (both `| a | b |` and `a | b` are accepted).\n * Pipes escaped as `\\|` stay in the cell content. Unicode `│`\n * (U+2502 BOX DRAWINGS LIGHT VERTICAL) is normalized to `|` first so\n * box-drawing tables and GFM tables share one code path.\n */\nfunction splitTableRow(line: string): string[] {\n // Temporarily replace escaped pipes so split() doesn't fire on them.\n const SENTINEL = \"\\u0000\";\n const masked = line.replace(/\\\\\\|/g, SENTINEL).replace(/│/g, \"|\");\n const trimmed = masked.trim().replace(/^\\||\\|$/g, \"\");\n return trimmed.split(\"|\").map((c) => c.trim().replace(new RegExp(SENTINEL, \"g\"), \"|\"));\n}\n\n/**\n * Render a GFM table as an aligned grid. Column widths are the max\n * display length in that column, capped at 40 chars so one huge cell\n * doesn't wreck the layout. Header row is bold + cyan; body rows use\n * the default text color. Separator is a dim row of dashes.\n */\nfunction TableBlockRow({ block }: { block: TableBlock }) {\n const colCount = Math.max(block.header.length, ...block.rows.map((r) => r.length));\n const widths: number[] = [];\n for (let c = 0; c < colCount; c++) {\n // Use VISIBLE width (post-markup-strip) for column sizing —\n // otherwise a cell like `**定义** \\`dispatch\\` 方法` would be\n // measured with the ** and ` chars included, over-padding once\n // the markers vanish at render time and shoving subsequent\n // columns rightward.\n const cellLengths = [visibleWidth(block.header[c] ?? \"\")];\n for (const r of block.rows) cellLengths.push(visibleWidth(r[c] ?? \"\"));\n widths.push(Math.min(40, Math.max(3, ...cellLengths)));\n }\n const separator = widths.map((w) => \"─\".repeat(w)).join(\"─┼─\");\n return (\n <Box flexDirection=\"column\">\n <Box>\n {block.header.map((cell, ci) => (\n // biome-ignore lint/suspicious/noArrayIndexKey: table columns never reorder — derived from a static header array\n <Text key={`h-${ci}`} bold color=\"cyan\">\n <InlineMd text={cell} padTo={widths[ci] ?? 3} />\n {ci < colCount - 1 ? \" │ \" : \"\"}\n </Text>\n ))}\n </Box>\n <Text dimColor>{separator}</Text>\n {block.rows.map((row, ri) => (\n // biome-ignore lint/suspicious/noArrayIndexKey: table rows render in source order and don't reorder\n <Box key={`r-${ri}`}>\n {Array.from({ length: colCount }).map((_, ci) => (\n // biome-ignore lint/suspicious/noArrayIndexKey: same — column axis is fixed by the table shape\n <Text key={`c-${ri}-${ci}`}>\n <InlineMd text={row[ci] ?? \"\"} padTo={widths[ci] ?? 3} />\n {ci < colCount - 1 ? \" │ \" : \"\"}\n </Text>\n ))}\n </Box>\n ))}\n </Box>\n );\n}\n\n/**\n * Terminal display width of a string, approximately. CJK characters\n * and full-width punctuation take 2 columns; everything else is 1.\n * Good enough for aligning table cells in a Chinese-or-English mix;\n * real wcwidth is bigger than we need to drag in for this use case.\n */\nfunction displayWidth(s: string): number {\n let w = 0;\n for (const ch of s) {\n const code = ch.codePointAt(0) ?? 0;\n // CJK unified ideographs, full-width forms, hiragana/katakana,\n // Hangul syllables — rough bucket, close enough for the terminal.\n if (\n (code >= 0x1100 && code <= 0x115f) ||\n (code >= 0x2e80 && code <= 0x303e) ||\n (code >= 0x3041 && code <= 0x33ff) ||\n (code >= 0x3400 && code <= 0x4dbf) ||\n (code >= 0x4e00 && code <= 0x9fff) ||\n (code >= 0xa000 && code <= 0xa4cf) ||\n (code >= 0xac00 && code <= 0xd7a3) ||\n (code >= 0xf900 && code <= 0xfaff) ||\n (code >= 0xfe30 && code <= 0xfe4f) ||\n (code >= 0xff00 && code <= 0xff60) ||\n (code >= 0xffe0 && code <= 0xffe6)\n ) {\n w += 2;\n } else {\n w += 1;\n }\n }\n return w;\n}\n\n/**\n * SEARCH/REPLACE rendered as a minimal diff: filename on top, red\n * `-` lines, a gutter, green `+` lines. No inner markdown / inline\n * parsing — content is shown verbatim so JSDoc `/**` and `*` won't\n * be eaten by bold/italic regex.\n *\n * A truly empty SEARCH means \"new file\" and we label the filename\n * accordingly instead of rendering an empty red half.\n */\nfunction EditBlockRow({ block }: { block: EditBlockView }) {\n const isNewFile = block.search.length === 0;\n const searchLines = block.search.split(\"\\n\");\n const replaceLines = block.replace.split(\"\\n\");\n return (\n <Box flexDirection=\"column\" borderStyle=\"round\" borderColor=\"cyan\" paddingX={1}>\n <Box>\n <Text bold color=\"cyan\">\n {block.filename}\n </Text>\n {isNewFile ? (\n <Text color=\"green\" bold>\n {\" (new file)\"}\n </Text>\n ) : null}\n </Box>\n {isNewFile ? null : (\n <Box flexDirection=\"column\" marginTop={1}>\n {searchLines.map((line, i) => (\n <Text key={`s-${i}-${line.length}`} color=\"red\">\n {`- ${line}`}\n </Text>\n ))}\n </Box>\n )}\n <Box flexDirection=\"column\" marginTop={isNewFile ? 1 : 0}>\n {replaceLines.map((line, i) => (\n <Text key={`r-${i}-${line.length}`} color=\"green\">\n {`+ ${line}`}\n </Text>\n ))}\n </Box>\n </Box>\n );\n}\n\nexport function Markdown({ text }: { text: string }) {\n const cleaned = stripMath(text);\n const blocks = React.useMemo(() => parseBlocks(cleaned), [cleaned]);\n return (\n <Box flexDirection=\"column\" gap={1}>\n {blocks.map((b, i) => (\n <BlockView key={`${i}-${b.kind}`} block={b} />\n ))}\n </Box>\n );\n}\n","import React, { type ReactNode, createContext, useContext, useEffect, useState } from \"react\";\n\n/**\n * Global heartbeat for every animated component (braille spinners,\n * elapsed-seconds counters, cursor blink). A single setInterval feeds\n * all of them instead of each component owning one. Consolidating the\n * 5-6 independent timers that used to exist in App.tsx + EventLog.tsx +\n * PromptInput.tsx into one source dramatically cuts the number of\n * React re-renders per second on heavy turns and stops Ink from\n * patching the terminal 30+ times/sec — the main amplifier of redraw\n * artifacts on winpty/MINTTY-class Windows terminals.\n *\n * Resolution: `TICK_MS` (default 120). Components that want 500ms or\n * 1s cadence just modulo the tick counter.\n */\nexport const TICK_MS = 120;\n\nconst TickContext = createContext(0);\n\nexport interface TickerProviderProps {\n children: ReactNode;\n /**\n * When true, the provider skips the setInterval entirely — tick stays at\n * 0, all consumers render once and never re-render from the timer. Used\n * by PLAIN_UI mode so the cursor and any surviving spinners don't drive\n * repaints on fragile Windows terminals.\n */\n disabled?: boolean;\n}\n\nexport function TickerProvider({ children, disabled }: TickerProviderProps) {\n const [tick, setTick] = useState(0);\n useEffect(() => {\n if (disabled) return;\n const id = setInterval(() => setTick((t) => t + 1), TICK_MS);\n return () => clearInterval(id);\n }, [disabled]);\n return <TickContext.Provider value={tick}>{children}</TickContext.Provider>;\n}\n\n/** Current global tick. Re-renders the calling component every `TICK_MS`. */\nexport function useTick(): number {\n return useContext(TickContext);\n}\n\n/**\n * Seconds elapsed since the calling component mounted. Derived from\n * the shared tick + a fresh Date.now() read, so no dedicated timer\n * is needed.\n */\nexport function useElapsedSeconds(): number {\n const [start] = useState(() => Date.now());\n useTick(); // subscribe to the tick so we re-render\n return Math.floor((Date.now() - start) / 1000);\n}\n","/**\n * Modal-style approval for a `submit_plan` proposal.\n *\n * Three choices:\n * 1. Approve + implement — exits plan mode, pushes a synthetic user\n * message telling the model to implement the plan now.\n * 2. Refine — stays in plan mode; tells the model to explore more\n * and submit an improved plan.\n * 3. Cancel — exits plan mode, drops the plan, tells the model the\n * user didn't want any of it.\n *\n * Mirrors ShellConfirm in structure (border, SingleSelect, three\n * options, no y/n hotkey — mid-typing triggers would be painful).\n * The plan body is rendered verbatim above the picker so the user can\n * actually read what they're approving.\n */\n\nimport { Box, Text } from \"ink\";\nimport React from \"react\";\nimport { SingleSelect } from \"./Select.js\";\nimport { Markdown } from \"./markdown.js\";\n\nexport type PlanConfirmChoice = \"approve\" | \"refine\" | \"cancel\";\n\nexport interface PlanConfirmProps {\n plan: string;\n onChoose: (choice: PlanConfirmChoice) => void;\n /**\n * Cap on rendered plan length. A pathological 20-KB plan would push\n * the picker off the bottom of the terminal; we show the head +\n * \"(…N chars truncated — /tool for full output)\" instead. The picker\n * itself gets the full plan (it's already been committed to the\n * transcript via the tool result).\n */\n maxRenderedChars?: number;\n}\n\nconst DEFAULT_MAX_RENDERED = 2400;\n\nexport function PlanConfirm({ plan, onChoose, maxRenderedChars }: PlanConfirmProps) {\n const cap = maxRenderedChars ?? DEFAULT_MAX_RENDERED;\n const tooLong = plan.length > cap;\n const visible = tooLong\n ? `${plan.slice(0, cap)}\\n\\n… (${plan.length - cap} chars truncated — use /tool to view the full proposal)`\n : plan;\n // Crude signal for \"the model left questions or risks for me\" — the\n // typical section headings. Triggers an extra hint toward the Refine\n // option so users know where to answer them.\n const hasOpenQuestions =\n /^#{1,6}\\s*(open[-\\s]?questions?|risks?|unknowns?|assumptions?|unclear)/im.test(plan) ||\n /^#{1,6}\\s*(待确认|开放问题|风险|未知|假设|不确定)/im.test(plan);\n\n return (\n <Box flexDirection=\"column\" borderStyle=\"round\" borderColor=\"cyan\" paddingX={1} marginY={1}>\n <Box>\n <Text bold color=\"cyan\">\n ▸ plan submitted — awaiting your review\n </Text>\n </Box>\n <Box marginTop={1} flexDirection=\"column\">\n <Markdown text={visible} />\n </Box>\n {hasOpenQuestions ? (\n <Box marginTop={1}>\n <Text color=\"yellow\">\n ▲ the plan has open questions or flagged risks — pick{\" \"}\n <Text bold>Refine / answer questions</Text> to write concrete answers before the model\n moves on.\n </Text>\n </Box>\n ) : null}\n <Box marginTop={1}>\n <SingleSelect\n initialValue={hasOpenQuestions ? \"refine\" : \"approve\"}\n items={[\n {\n value: \"approve\",\n label: \"Approve and implement\",\n hint: \"Exit plan mode. The model starts executing. You'll get a text input to add any last instructions (or just press Enter to skip).\",\n },\n {\n value: \"refine\",\n label: \"Refine / answer questions\",\n hint: \"Stay in plan mode. Write answers, modifications, or critiques; the model revises and re-submits.\",\n },\n {\n value: \"cancel\",\n label: \"Cancel\",\n hint: \"Exit plan mode. Drop the plan; the model won't implement it.\",\n },\n ]}\n onSubmit={(v) => onChoose(v as PlanConfirmChoice)}\n />\n </Box>\n </Box>\n );\n}\n","/**\n * Minimal arrow-key list components for Ink — single-select and\n * multi-select. No external deps beyond Ink's `useInput`.\n *\n * Why hand-roll: `ink-select-input` exists, but it defaults to\n * Enter-only interaction (no space-to-toggle for multi-select), doesn't\n * expose the \"hint / footer\" slot we want under each item, and would be\n * another dep for ~60 lines of UI code. Reasonix already has one React\n * rendering quirk bundled (`ink-text-input`); adding more is low value.\n */\n\nimport { Box, Text, useInput } from \"ink\";\nimport React, { useState } from \"react\";\n\nexport interface SelectItem<V extends string = string> {\n /** Stable identifier — returned to caller on submit. */\n value: V;\n /** First-row label. */\n label: string;\n /** Optional second row rendered dimmed. */\n hint?: string;\n /** If true, item is not selectable (rendered dimmed, skipped on nav). */\n disabled?: boolean;\n}\n\nexport interface SingleSelectProps<V extends string> {\n items: SelectItem<V>[];\n initialValue?: V;\n onSubmit: (value: V) => void;\n onCancel?: () => void;\n}\n\nexport function SingleSelect<V extends string>({\n items,\n initialValue,\n onSubmit,\n onCancel,\n}: SingleSelectProps<V>) {\n const initialIndex = Math.max(\n 0,\n items.findIndex((i) => i.value === initialValue && !i.disabled),\n );\n const [index, setIndex] = useState(initialIndex === -1 ? 0 : initialIndex);\n\n useInput((_input, key) => {\n if (key.upArrow) {\n setIndex((i) => findNextEnabled(items, i, -1));\n } else if (key.downArrow) {\n setIndex((i) => findNextEnabled(items, i, +1));\n } else if (key.return) {\n const chosen = items[index];\n if (chosen && !chosen.disabled) onSubmit(chosen.value);\n } else if (key.escape && onCancel) {\n onCancel();\n }\n });\n\n return (\n <Box flexDirection=\"column\">\n {items.map((item, i) => (\n <SelectRow\n key={item.value}\n item={item}\n active={i === index}\n marker={i === index ? \"▸\" : \" \"}\n />\n ))}\n </Box>\n );\n}\n\nexport interface MultiSelectProps<V extends string> {\n items: SelectItem<V>[];\n initialSelected?: V[];\n onSubmit: (values: V[]) => void;\n onCancel?: () => void;\n /** Footer hint under the list — e.g. \"space toggle · enter confirm\". */\n footer?: string;\n}\n\nexport function MultiSelect<V extends string>({\n items,\n initialSelected = [],\n onSubmit,\n onCancel,\n footer,\n}: MultiSelectProps<V>) {\n const [index, setIndex] = useState(() => {\n const first = items.findIndex((i) => !i.disabled);\n return first === -1 ? 0 : first;\n });\n const [selected, setSelected] = useState<Set<V>>(new Set(initialSelected));\n\n useInput((input, key) => {\n if (key.upArrow) {\n setIndex((i) => findNextEnabled(items, i, -1));\n } else if (key.downArrow) {\n setIndex((i) => findNextEnabled(items, i, +1));\n } else if (input === \" \") {\n const item = items[index];\n if (!item || item.disabled) return;\n setSelected((prev) => {\n const next = new Set(prev);\n if (next.has(item.value)) next.delete(item.value);\n else next.add(item.value);\n return next;\n });\n } else if (key.return) {\n // Preserve catalog order rather than insertion order, so reruns\n // produce the same spec list for the same checkbox set — makes the\n // `config.json` diff trivially stable.\n const ordered = items.filter((i) => selected.has(i.value)).map((i) => i.value);\n onSubmit(ordered);\n } else if (key.escape && onCancel) {\n onCancel();\n }\n });\n\n return (\n <Box flexDirection=\"column\">\n {items.map((item, i) => {\n const checked = selected.has(item.value);\n const marker = checked ? \"[x]\" : \"[ ]\";\n return (\n <SelectRow\n key={item.value}\n item={item}\n active={i === index}\n marker={`${i === index ? \"▸\" : \" \"} ${marker}`}\n />\n );\n })}\n {footer ? (\n <Box marginTop={1}>\n <Text dimColor>{footer}</Text>\n </Box>\n ) : null}\n </Box>\n );\n}\n\n// ---------- internals ----------\n\nfunction SelectRow<V extends string>({\n item,\n active,\n marker,\n}: {\n item: SelectItem<V>;\n active: boolean;\n marker: string;\n}) {\n const color = item.disabled ? \"gray\" : active ? \"cyan\" : undefined;\n return (\n <Box flexDirection=\"column\">\n <Box>\n <Text color={color}>\n {marker} {item.label}\n </Text>\n </Box>\n {item.hint ? (\n <Box paddingLeft={marker.length + 1}>\n <Text dimColor>{item.hint}</Text>\n </Box>\n ) : null}\n </Box>\n );\n}\n\nfunction findNextEnabled<V extends string>(\n items: SelectItem<V>[],\n from: number,\n step: -1 | 1,\n): number {\n if (items.length === 0) return 0;\n let i = from;\n for (let tries = 0; tries < items.length; tries++) {\n i = (i + step + items.length) % items.length;\n if (!items[i]?.disabled) return i;\n }\n return from;\n}\n","/**\n * Inline text input shown after the user picks Approve or Refine in\n * PlanConfirm. Collects free-form feedback before the loop resumes.\n *\n * Why both paths: the plan may contain open questions or risks the\n * model asked the user to weigh in on. If the user just picks Approve\n * with no chance to answer, the model implements against its own\n * guesses. This input lets the user answer questions, pass last-minute\n * constraints, or (on Refine) request concrete changes. Empty input is\n * fine for Approve (skip straight to implement) and triggers a\n * \"ask the user clarifying questions\" path for Refine.\n *\n * Kept minimal: single-line prompt, Enter to submit, Esc to return to\n * the picker without resuming.\n */\n\nimport { Box, Text, useInput } from \"ink\";\nimport React, { useState } from \"react\";\n\nexport interface PlanRefineInputProps {\n /**\n * Which path the user is on. Approve = \"implement with these last\n * instructions\"; refine = \"revise the plan with this feedback\".\n * Drives the header + hint text so users know what kind of message\n * they're writing.\n */\n mode: \"approve\" | \"refine\";\n /** Called with trimmed feedback. Empty string is allowed. */\n onSubmit: (feedback: string) => void;\n /** Called when the user presses Esc to return to the picker. */\n onCancel: () => void;\n}\n\nexport function PlanRefineInput({ mode, onSubmit, onCancel }: PlanRefineInputProps) {\n const [value, setValue] = useState(\"\");\n\n useInput((input, key) => {\n if (key.escape) {\n onCancel();\n return;\n }\n if (key.return) {\n onSubmit(value.trim());\n return;\n }\n if (key.backspace || key.delete) {\n setValue((v) => v.slice(0, -1));\n return;\n }\n // Filter out non-printable chars; accept ordinary text + CJK.\n if (input && !key.ctrl && !key.meta) {\n setValue((v) => v + input);\n }\n });\n\n const title =\n mode === \"approve\"\n ? \"▸ approving — any last instructions or answers to open questions?\"\n : \"▸ refining — what should the model change?\";\n const hint =\n mode === \"approve\"\n ? \"Answer questions the plan raised, add constraints, or just press Enter to approve as-is.\"\n : \"Describe what's wrong or missing, or answer questions the plan raised.\";\n const blankHint =\n mode === \"approve\"\n ? \" (Enter with blank = approve without extra instructions.)\"\n : \" (Enter with blank = ask the model to list concrete questions.)\";\n\n return (\n <Box flexDirection=\"column\" borderStyle=\"round\" borderColor=\"yellow\" paddingX={1} marginY={1}>\n <Box>\n <Text bold color=\"yellow\">\n {title}\n </Text>\n </Box>\n <Box marginTop={1}>\n <Text dimColor>\n {hint} Enter to send · Esc to return to the picker.\n {value === \"\" ? blankHint : \"\"}\n </Text>\n </Box>\n <Box marginTop={1}>\n <Text>\n <Text color=\"yellow\">› </Text>\n <Text>{value || \" \"}</Text>\n <Text color=\"yellow\">▍</Text>\n </Text>\n </Box>\n </Box>\n );\n}\n","import { Box, Text, useInput } from \"ink\";\nimport React, { useRef, useState } from \"react\";\nimport { type MultilineKey, lineAndColumn, processMultilineKey } from \"./multiline-keys.js\";\nimport { useTick } from \"./ticker.js\";\n\nexport interface PromptInputProps {\n value: string;\n onChange: (v: string) => void;\n onSubmit: (v: string) => void;\n disabled?: boolean;\n placeholder?: string;\n}\n\n/**\n * Input box with real cursor support. ←/→ move one column, ↑/↓ move\n * across lines in multi-line buffers, Ctrl+A / Ctrl+E jump to\n * start/end of the current line. Backspace deletes before cursor,\n * Delete deletes under cursor. Multi-line composition via Ctrl+J,\n * Shift+Enter, or bash-style `\\<Enter>`.\n *\n * Cursor state lives locally. When the parent replaces `value` out\n * of band (history recall, slash completion, setup wizard) the\n * cursor jumps to end; the `lastLocalValueRef` guards distinguishes\n * that case from our own edits.\n */\nexport function PromptInput({\n value,\n onChange,\n onSubmit,\n disabled,\n placeholder,\n}: PromptInputProps) {\n const [cursor, setCursor] = useState(value.length);\n // Tracks the last `value` we ourselves produced via onChange. If the\n // incoming `value` prop diverges from this, the parent (or some other\n // source) replaced the buffer — we reset the cursor to end.\n const lastLocalValueRef = useRef(value);\n if (value !== lastLocalValueRef.current) {\n lastLocalValueRef.current = value;\n if (cursor !== value.length) {\n // Conditional setState during render is the \"derived state\" pattern;\n // React schedules the re-render and the else branch of the `if`\n // prevents infinite loops.\n setCursor(value.length);\n }\n }\n\n // Shared ticker drives the cursor blink. Dividing the tick by 4 lands\n // the visible on/off cycle around 480ms — standard cursor cadence.\n const tick = useTick();\n const showCursor = disabled ? false : Math.floor(tick / 4) % 2 === 0;\n\n useInput(\n (input, key) => {\n const ke: MultilineKey = {\n input,\n return: key.return,\n shift: key.shift,\n ctrl: key.ctrl,\n meta: key.meta,\n backspace: key.backspace,\n delete: key.delete,\n tab: key.tab,\n upArrow: key.upArrow,\n downArrow: key.downArrow,\n leftArrow: key.leftArrow,\n rightArrow: key.rightArrow,\n escape: key.escape,\n pageUp: key.pageUp,\n pageDown: key.pageDown,\n };\n const action = processMultilineKey(value, cursor, ke);\n if (action.next !== null) {\n lastLocalValueRef.current = action.next;\n onChange(action.next);\n }\n if (action.cursor !== null) {\n setCursor(action.cursor);\n }\n if (action.submit) onSubmit(action.submitValue ?? value);\n },\n { isActive: !disabled },\n );\n\n const effectivePlaceholder = disabled\n ? (placeholder ?? \"…waiting for response…\")\n : (placeholder ?? \"type a message, or /command · Ctrl+J for newline\");\n\n const lines = value.length > 0 ? value.split(\"\\n\") : [\"\"];\n const borderColor = disabled ? \"gray\" : \"cyan\";\n const { line: cursorLine, col: cursorCol } = lineAndColumn(value, cursor);\n\n return (\n <Box borderStyle=\"round\" borderColor={borderColor} paddingX={1} flexDirection=\"column\">\n {lines.map((line, i) => {\n const isFirst = i === 0;\n const showPlaceholder = isFirst && value.length === 0;\n const isCursorLine = i === cursorLine;\n return (\n // biome-ignore lint/suspicious/noArrayIndexKey: stable by construction — lines are derived from `value.split(\"\\n\")` and never reordered\n <Box key={i}>\n {isFirst ? (\n <Text bold color={borderColor}>\n you ›{\" \"}\n </Text>\n ) : (\n <Text dimColor>{\" \"}</Text>\n )}\n {showPlaceholder ? (\n <>\n {isCursorLine && !disabled ? (\n <Text color={borderColor}>{showCursor ? \"▌\" : \" \"}</Text>\n ) : null}\n <Text dimColor>{effectivePlaceholder}</Text>\n </>\n ) : isCursorLine && !disabled ? (\n <LineWithCursor\n line={line}\n col={cursorCol}\n showCursor={showCursor}\n borderColor={borderColor}\n />\n ) : (\n <Text>{line}</Text>\n )}\n </Box>\n );\n })}\n </Box>\n );\n}\n\nfunction LineWithCursor({\n line,\n col,\n showCursor,\n borderColor,\n}: {\n line: string;\n col: number;\n showCursor: boolean;\n borderColor: \"cyan\" | \"gray\";\n}) {\n const before = line.slice(0, col);\n const atCursor = line.slice(col, col + 1);\n const after = line.slice(col + 1);\n if (atCursor.length === 0) {\n // Cursor sits past the last char of this line (end-of-line). Render\n // a trailing block so the user sees where they're typing next.\n return (\n <>\n <Text>{before}</Text>\n <Text color={borderColor}>{showCursor ? \"▌\" : \" \"}</Text>\n </>\n );\n }\n return (\n <>\n <Text>{before}</Text>\n <Text inverse={showCursor}>{atCursor}</Text>\n <Text>{after}</Text>\n </>\n );\n}\n","/**\n * Pure keystroke → action reducer for PromptInput.\n *\n * Kept separate from the React component so the keyboard semantics\n * are easy to unit-test. The component threads `useInput` through\n * this function and applies the returned action.\n *\n * Edit model:\n * - Full cursor support. ←/→ move one column. ↑/↓ move across\n * lines in a multi-line buffer (column preserved when possible).\n * Ctrl+A / Ctrl+E jump to start / end of the current line.\n * - Backspace deletes the char before the cursor; Delete deletes\n * the char under the cursor.\n * - Printable chars (including multi-char paste bursts) insert\n * at the cursor.\n * - Enter submits unless Shift is held (newline), the line ends\n * with '\\' and cursor is at end (bash-style continuation), or\n * the input is Ctrl+J (LF, terminal-universal newline).\n * - Parent owns Tab, Esc, PageUp/Down (slash-complete, abort,\n * unused). Arrow keys are split: empty buffer → parent (history\n * recall); non-empty → child (cursor movement).\n */\n\nexport interface MultilineKey {\n input: string;\n return?: boolean;\n shift?: boolean;\n ctrl?: boolean;\n meta?: boolean;\n backspace?: boolean;\n delete?: boolean;\n tab?: boolean;\n upArrow?: boolean;\n downArrow?: boolean;\n leftArrow?: boolean;\n rightArrow?: boolean;\n escape?: boolean;\n pageUp?: boolean;\n pageDown?: boolean;\n}\n\nexport interface MultilineAction {\n /** New buffer value. `null` = unchanged. */\n next: string | null;\n /** New cursor position (0..value.length). `null` = unchanged. */\n cursor: number | null;\n /** When `true`, fire `onSubmit(submitValue ?? value)`. */\n submit: boolean;\n submitValue?: string;\n}\n\nconst BACKSLASH_SUFFIX = /\\\\$/;\n\nconst NOOP: MultilineAction = { next: null, cursor: null, submit: false };\n\nexport function processMultilineKey(\n value: string,\n cursor: number,\n key: MultilineKey,\n): MultilineAction {\n // Parent-owned keys: Tab (slash-complete), Esc (abort), PageUp/Down.\n if (key.tab || key.escape || key.pageUp || key.pageDown) {\n return NOOP;\n }\n\n // Empty buffer + ↑/↓ → parent handles history recall.\n if (value.length === 0 && (key.upArrow || key.downArrow)) {\n return NOOP;\n }\n\n // Cursor motion.\n if (key.leftArrow) {\n return { next: null, cursor: Math.max(0, cursor - 1), submit: false };\n }\n if (key.rightArrow) {\n return { next: null, cursor: Math.min(value.length, cursor + 1), submit: false };\n }\n if (key.upArrow) {\n const moved = moveCursorUp(value, cursor);\n return moved === cursor ? NOOP : { next: null, cursor: moved, submit: false };\n }\n if (key.downArrow) {\n const moved = moveCursorDown(value, cursor);\n return moved === cursor ? NOOP : { next: null, cursor: moved, submit: false };\n }\n\n // Emacs-style line jumps (universal across terminals; Home/End aren't\n // reliably reported by Ink so we don't depend on them).\n if (key.ctrl && key.input === \"a\") {\n return { next: null, cursor: startOfLine(value, cursor), submit: false };\n }\n if (key.ctrl && key.input === \"e\") {\n return { next: null, cursor: endOfLine(value, cursor), submit: false };\n }\n\n // Newline: Ctrl+J (LF literal) or ctrl+'j' normalized form.\n if (key.input === \"\\n\" || (key.ctrl && key.input === \"j\")) {\n return insertAt(value, cursor, \"\\n\");\n }\n\n if (key.return) {\n if (key.shift) return insertAt(value, cursor, \"\\n\");\n // Bash-style line continuation: trailing '\\' + Enter (only when the\n // cursor sits at end-of-buffer, so a stray '\\' mid-line doesn't\n // trigger it).\n if (cursor === value.length && BACKSLASH_SUFFIX.test(value)) {\n const replaced = `${value.slice(0, -1)}\\n`;\n return { next: replaced, cursor: replaced.length, submit: false };\n }\n return { next: null, cursor: null, submit: true, submitValue: value };\n }\n\n // Backspace = delete the char BEFORE the cursor. We also accept\n // `key.delete` and the raw DEL (0x7f) / BS (0x08) bytes as backspace\n // for the same purpose — some Windows terminals (cmd.exe, certain\n // winpty configs) report plain Backspace without setting\n // `key.backspace`, which used to leave the user typing into a prompt\n // where the Backspace key did nothing. Reasonix doesn't offer a\n // separate forward-delete operation, so collapsing them is safe.\n if (key.backspace || key.delete || key.input === \"\\x7f\" || key.input === \"\\b\") {\n if (cursor === 0) return NOOP;\n return {\n next: value.slice(0, cursor - 1) + value.slice(cursor),\n cursor: cursor - 1,\n submit: false,\n };\n }\n\n // Bare modifier events (Ctrl/Meta with no printable) and unhandled\n // Ctrl-<letter> chords are dropped so a stray Ctrl+L doesn't insert \"l\".\n if ((key.ctrl || key.meta) && key.input.length === 0) return NOOP;\n if (key.ctrl || key.meta) return NOOP;\n\n // Printable input (may be a multi-char paste; pasted newlines land\n // inside the buffer rather than triggering submit on the first line).\n if (key.input.length > 0) {\n return insertAt(value, cursor, key.input);\n }\n\n return NOOP;\n}\n\nfunction insertAt(value: string, cursor: number, insert: string): MultilineAction {\n return {\n next: value.slice(0, cursor) + insert + value.slice(cursor),\n cursor: cursor + insert.length,\n submit: false,\n };\n}\n\n/**\n * Line + column of a cursor inside a buffer. Exported because the\n * renderer needs the same mapping for drawing the cursor block on the\n * right line.\n */\nexport function lineAndColumn(value: string, cursor: number): { line: number; col: number } {\n let line = 0;\n let col = 0;\n const n = Math.min(cursor, value.length);\n for (let i = 0; i < n; i++) {\n if (value[i] === \"\\n\") {\n line++;\n col = 0;\n } else {\n col++;\n }\n }\n return { line, col };\n}\n\nfunction startOfLine(value: string, cursor: number): number {\n return value.lastIndexOf(\"\\n\", cursor - 1) + 1;\n}\n\nfunction endOfLine(value: string, cursor: number): number {\n const nl = value.indexOf(\"\\n\", cursor);\n return nl === -1 ? value.length : nl;\n}\n\nfunction moveCursorUp(value: string, cursor: number): number {\n const curStart = startOfLine(value, cursor);\n if (curStart === 0) return cursor; // already on the first line\n const col = cursor - curStart;\n const prevEnd = curStart - 1; // the '\\n' between the two lines\n const prevStart = value.lastIndexOf(\"\\n\", prevEnd - 1) + 1;\n const prevLen = prevEnd - prevStart;\n return prevStart + Math.min(col, prevLen);\n}\n\nfunction moveCursorDown(value: string, cursor: number): number {\n const nextNl = value.indexOf(\"\\n\", cursor);\n if (nextNl === -1) return cursor; // already on the last line\n const curStart = startOfLine(value, cursor);\n const col = cursor - curStart;\n const nextStart = nextNl + 1;\n const followingNl = value.indexOf(\"\\n\", nextStart);\n const nextLen = (followingNl === -1 ? value.length : followingNl) - nextStart;\n return nextStart + Math.min(col, nextLen);\n}\n","import { Box, Text } from \"ink\";\nimport React from \"react\";\nimport { SingleSelect } from \"./Select.js\";\n\nexport type ShellConfirmChoice = \"run_once\" | \"always_allow\" | \"deny\";\n\nexport interface ShellConfirmProps {\n command: string;\n /**\n * The prefix that would be persisted if the user picks\n * \"always allow\". Typically the first 1-2 tokens of `command`.\n */\n allowPrefix: string;\n onChoose: (choice: ShellConfirmChoice) => void;\n}\n\n/**\n * Modal-style approval for a shell command the model wants to run.\n * Three choices:\n * 1. Run once — execute this invocation, prefix NOT remembered.\n * 2. Always allow — persist the prefix to `~/.reasonix/config.json`\n * under this project so every future invocation with that prefix\n * auto-runs.\n * 3. Deny — tell the model the user refused.\n * Arrow keys + Enter. No y/n hotkey — too easy to trigger by accident\n * when the user was mid-typing a response.\n */\nexport function ShellConfirm({ command, allowPrefix, onChoose }: ShellConfirmProps) {\n return (\n <Box flexDirection=\"column\" borderStyle=\"round\" borderColor=\"yellow\" paddingX={1} marginY={1}>\n <Box>\n <Text bold color=\"yellow\">\n ▸ model wants to run a shell command\n </Text>\n </Box>\n <Box marginTop={1}>\n <Text>\n <Text dimColor>{\"$ \"}</Text>\n <Text color=\"cyan\">{command}</Text>\n </Text>\n </Box>\n <Box marginTop={1}>\n <SingleSelect\n initialValue=\"run_once\"\n items={[\n {\n value: \"run_once\",\n label: \"Run once\",\n hint: \"Execute this command, don't remember it.\",\n },\n {\n value: \"always_allow\",\n label: `Always allow \"${allowPrefix}\" in this project`,\n hint: \"Save the prefix to ~/.reasonix/config.json; future matches auto-run.\",\n },\n {\n value: \"deny\",\n label: \"Deny\",\n hint: \"Tell the model the user refused; it will continue without this command.\",\n },\n ]}\n onSubmit={(v) => onChoose(v as ShellConfirmChoice)}\n />\n </Box>\n </Box>\n );\n}\n\n/**\n * Pick the \"always allow\" prefix from a full command. Heuristic:\n * - one-token commands (\"ls\", \"pytest\") → the token itself\n * - multi-token → first two tokens for well-known wrappers\n * (`npm install`, `git commit`, `cargo add`, `docker run` …)\n * otherwise just the first token (covers `node <script>` where the\n * second token is usually a file path specific to this invocation).\n * Exported so tests can pin the heuristic.\n */\nexport function derivePrefix(command: string): string {\n const tokens = command.trim().split(/\\s+/).filter(Boolean);\n if (tokens.length === 0) return \"\";\n if (tokens.length === 1) return tokens[0]!;\n const first = tokens[0]!;\n const TWO_TOKEN_WRAPPERS = new Set([\n \"npm\",\n \"npx\",\n \"pnpm\",\n \"yarn\",\n \"bun\",\n \"git\",\n \"cargo\",\n \"go\",\n \"docker\",\n \"kubectl\",\n \"python\",\n \"python3\",\n \"deno\",\n \"pip\",\n \"pip3\",\n \"make\",\n \"rake\",\n \"bundle\",\n \"gem\",\n ]);\n return TWO_TOKEN_WRAPPERS.has(first) ? `${first} ${tokens[1]}` : first;\n}\n","import { Box, Text } from \"ink\";\n// biome-ignore lint/style/useImportType: tsconfig.jsx = \"react\" needs React in value scope for JSX compilation\nimport React from \"react\";\nimport type { SlashCommandSpec } from \"./slash.js\";\n\nexport interface SlashSuggestionsProps {\n /**\n * Current matching suggestions, computed by the parent. `null` means\n * \"not in slash-prefix mode\" — render nothing. Empty array means \"in\n * slash mode but no matches\" — render the \"no matches\" hint.\n */\n matches: SlashCommandSpec[] | null;\n /** Index (within `matches`) of the currently highlighted row. */\n selectedIndex: number;\n}\n\n/**\n * Floating slash-command panel. Rendered below the input box when\n * the user is typing a `/…` prefix. Navigation state lives in the\n * parent (App.tsx owns `slashSelected`) so ↑/↓/Tab/Enter stay\n * consistent with the useInput handler. This component is pure\n * display: `matches` and `selectedIndex` come in, rows go out.\n */\nexport function SlashSuggestions({\n matches,\n selectedIndex,\n}: SlashSuggestionsProps): React.ReactElement | null {\n if (matches === null) return null;\n if (matches.length === 0) {\n return (\n <Box paddingX={1}>\n <Text color=\"yellow\">no slash command matches that prefix</Text>\n <Text dimColor> — Backspace to edit, or /help for the full list</Text>\n </Box>\n );\n }\n // Limit rows so the suggestion list never dwarfs the rest of the\n // UI. Keep the currently-selected row in view by sliding the\n // window when the selection is near either edge.\n const MAX = 8;\n const total = matches.length;\n const windowStart =\n total <= MAX ? 0 : Math.max(0, Math.min(selectedIndex - Math.floor(MAX / 2), total - MAX));\n const shown = matches.slice(windowStart, windowStart + MAX);\n const hiddenAbove = windowStart;\n const hiddenBelow = total - windowStart - shown.length;\n return (\n <Box flexDirection=\"column\" paddingX={1}>\n {hiddenAbove > 0 ? <Text dimColor> ↑ {hiddenAbove} more above</Text> : null}\n {shown.map((spec, i) => (\n <SuggestionRow key={spec.cmd} spec={spec} isSelected={windowStart + i === selectedIndex} />\n ))}\n {hiddenBelow > 0 ? <Text dimColor> ↓ {hiddenBelow} more below</Text> : null}\n <Text dimColor> ↑/↓ navigate · Tab or Enter to pick</Text>\n </Box>\n );\n}\n\nfunction SuggestionRow({ spec, isSelected }: { spec: SlashCommandSpec; isSelected: boolean }) {\n const marker = isSelected ? \"▸\" : \" \";\n const name = `/${spec.cmd}`;\n const argsSuffix = spec.argsHint ? ` ${spec.argsHint}` : \"\";\n // Selected row gets full cyan coloring so it pops out against the\n // dimmed others — mirrors the style GitHub Issues / Discord use\n // for focused dropdown rows.\n if (isSelected) {\n return (\n <Box>\n <Text bold color=\"cyan\">\n {marker} {name.padEnd(12)}\n {argsSuffix.padEnd(16)}\n </Text>\n <Text color=\"cyan\"> {spec.summary}</Text>\n </Box>\n );\n }\n return (\n <Box>\n <Text dimColor>\n {marker} {name.padEnd(12)}\n {argsSuffix.padEnd(16)} {spec.summary}\n </Text>\n </Box>\n );\n}\n","import { Box, Text } from \"ink\";\nimport React from \"react\";\nimport { DEEPSEEK_CONTEXT_TOKENS, DEFAULT_CONTEXT_TOKENS } from \"../../telemetry.js\";\nimport type { SessionSummary } from \"../../telemetry.js\";\nimport { VERSION } from \"../../version.js\";\n\nexport interface StatsPanelProps {\n summary: SessionSummary;\n model: string;\n prefixHash: string;\n harvestOn?: boolean;\n branchBudget?: number;\n /**\n * True when `reasonix code` is currently running in read-only Plan\n * Mode. Surfaced as a red \"PLAN\" tag in the panel header so the user\n * can tell at a glance that edits are gated behind submit_plan +\n * approval.\n */\n planMode?: boolean;\n /**\n * Account balance fetched once at launch (and optionally refreshed\n * per-turn by the TUI). `null` or absent hides the balance cell\n * entirely — /user/balance failed or the user ran with `--no-config`.\n * The top-up warning fires below 1.0 unit of whatever currency\n * the endpoint reports so a Chinese user with CNY and a U.S. user\n * with USD both see \"getting low.\"\n */\n balance?: { currency: string; total: number } | null;\n /**\n * Published npm version newer than VERSION. Rendered as a yellow\n * \"· update: X\" nudge in the panel header. `null` / `undefined`\n * hides the nudge (offline launch, already up to date, or check\n * still in flight).\n */\n updateAvailable?: string | null;\n}\n\nexport function StatsPanel({\n summary,\n model,\n prefixHash,\n harvestOn,\n branchBudget,\n planMode,\n balance,\n updateAvailable,\n}: StatsPanelProps) {\n const hitPct = (summary.cacheHitRatio * 100).toFixed(1);\n const hitColor =\n summary.cacheHitRatio >= 0.7 ? \"green\" : summary.cacheHitRatio >= 0.4 ? \"yellow\" : \"red\";\n const branchOn = (branchBudget ?? 1) > 1;\n\n const ctxMax = DEEPSEEK_CONTEXT_TOKENS[model] ?? DEFAULT_CONTEXT_TOKENS;\n const ctxRatio = summary.lastPromptTokens / ctxMax;\n const ctxColor = ctxRatio >= 0.8 ? \"red\" : ctxRatio >= 0.5 ? \"yellow\" : undefined;\n\n return (\n <Box borderStyle=\"round\" borderColor=\"cyan\" flexDirection=\"column\" paddingX={1}>\n <Box justifyContent=\"space-between\">\n <Text>\n <Text color=\"cyan\" bold>\n Reasonix\n </Text>\n <Text dimColor>{` v${VERSION}`}</Text>\n <Text dimColor> · model </Text>\n <Text color=\"yellow\">{model}</Text>\n <Text dimColor> · prefix </Text>\n <Text dimColor>{prefixHash}</Text>\n {harvestOn ? <Text color=\"magenta\"> · harvest</Text> : null}\n {branchOn ? <Text color=\"blue\"> · branch{branchBudget}</Text> : null}\n {planMode ? (\n <Text color=\"red\" bold>\n {\" \"}\n · PLAN\n </Text>\n ) : null}\n </Text>\n <Text>\n {updateAvailable ? (\n <Text color=\"yellow\" bold>{`update: ${updateAvailable} · `}</Text>\n ) : null}\n <Text dimColor>turns {summary.turns} · type /help</Text>\n </Text>\n </Box>\n <Box marginTop={1} gap={3}>\n <Text>\n <Text dimColor>cache hit </Text>\n <Text color={hitColor} bold>\n {hitPct}%\n </Text>\n </Text>\n <Text>\n <Text dimColor>cost </Text>\n <Text color=\"green\" bold>\n ${summary.totalCostUsd.toFixed(6)}\n </Text>\n <Text dimColor>\n {\" (in \"}${summary.totalInputCostUsd.toFixed(6)}\n {\" · out \"}${summary.totalOutputCostUsd.toFixed(6)}\n {\")\"}\n </Text>\n </Text>\n {summary.lastPromptTokens > 0 ? (\n <Text>\n <Text dimColor>ctx </Text>\n <Text color={ctxColor} bold={ctxColor !== undefined}>\n {formatTokens(summary.lastPromptTokens)}/{formatTokens(ctxMax)}\n </Text>\n <Text dimColor> ({(ctxRatio * 100).toFixed(0)}%)</Text>\n {ctxRatio >= 0.8 ? (\n <Text color=\"red\" bold>\n {\" \"}\n · /compact\n </Text>\n ) : null}\n </Text>\n ) : null}\n {balance ? (\n <Text>\n <Text dimColor>balance </Text>\n <Text color={balance.total < 1 ? \"red\" : balance.total < 5 ? \"yellow\" : \"green\"} bold>\n {balance.currency === \"USD\" ? \"$\" : \"\"}\n {balance.total.toFixed(2)}\n {balance.currency !== \"USD\" ? ` ${balance.currency}` : \"\"}\n </Text>\n </Text>\n ) : null}\n </Box>\n </Box>\n );\n}\n\n/**\n * Compact integer formatter: 1234 → \"1.2k\", 131072 → \"131k\". Keeps the\n * panel narrow enough to fit on 80-col terminals even when the context\n * is near full.\n */\nfunction formatTokens(n: number): string {\n if (n < 1000) return String(n);\n const k = n / 1000;\n return k >= 100 ? `${k.toFixed(0)}k` : `${k.toFixed(1)}k`;\n}\n","import { spawnSync } from \"node:child_process\";\nimport {\n HOOK_EVENTS,\n type HookEvent,\n type ResolvedHook,\n globalSettingsPath,\n projectSettingsPath,\n} from \"../../hooks.js\";\nimport type { CacheFirstLoop } from \"../../loop.js\";\nimport type { InspectionReport } from \"../../mcp/inspect.js\";\nimport { PROJECT_MEMORY_FILE, memoryEnabled, readProjectMemory } from \"../../project-memory.js\";\nimport { deleteSession, listSessions } from \"../../session.js\";\nimport { SkillStore } from \"../../skills.js\";\nimport { DEEPSEEK_CONTEXT_TOKENS, DEFAULT_CONTEXT_TOKENS } from \"../../telemetry.js\";\nimport { aggregateUsage, defaultUsageLogPath, readUsageLog } from \"../../usage.js\";\nimport { type MemoryScope, MemoryStore } from \"../../user-memory.js\";\nimport { VERSION, compareVersions, isNpxInstall } from \"../../version.js\";\nimport { renderDashboard } from \"../commands/stats.js\";\n\nexport interface SlashResult {\n /** Text to display back to the user as a system/info line. */\n info?: string;\n /** Exit the app. */\n exit?: boolean;\n /** Clear the visible history. */\n clear?: boolean;\n /** Unknown command — display usage hint. */\n unknown?: boolean;\n /**\n * Re-submit this text as a user message after displaying `info`.\n * Used by `/retry` — the slash command truncates the log, then\n * asks the TUI to push the original text back through the normal\n * submit flow so a fresh turn runs.\n */\n resubmit?: string;\n}\n\n/**\n * Extra runtime context a slash handler may care about but that isn't\n * already on the loop. Kept as an optional object so tests that only\n * need loop-scoped commands can skip it, and callers only populate the\n * slots that apply to their session.\n */\nexport interface SlashContext {\n /**\n * The exact `--mcp` / config-derived spec strings that were bridged\n * into this session (one entry per server). Used by `/mcp`. Empty or\n * omitted → no MCP servers attached.\n */\n mcpSpecs?: string[];\n /**\n * Callback for `/undo` — provided by the TUI when it's running in\n * code mode. Returns a human-readable report of what was restored.\n * Absent outside code mode → `/undo` replies \"not available here\".\n */\n codeUndo?: () => string;\n /**\n * Callback for `/apply` — commits pending edit blocks to disk. Returns\n * a report of what landed. Absent → `/apply` replies \"nothing pending\"\n * or \"not available outside code mode\".\n */\n codeApply?: () => string;\n /**\n * Callback for `/discard` — drops the pending edit blocks without\n * touching disk.\n */\n codeDiscard?: () => string;\n /**\n * Root directory passed by `reasonix code`. Enables `/commit`, which\n * runs `git add -A && git commit` in this directory. Missing → `/commit`\n * replies \"only available in code mode\".\n */\n codeRoot?: string;\n /**\n * How many edit blocks are currently pending `/apply` or `/discard`.\n * Surfaced by `/status`. TUI populates it live from its pending ref;\n * omitted → treat as 0 (chat-only session).\n */\n pendingEditCount?: number;\n /**\n * Callback returning every tool result seen this session in\n * chronological order (oldest first). Powers `/tool [N]` for\n * inspecting the full untruncated output that `EventLog` clips at\n * 400 chars for display. Absent → `/tool` replies \"not available\".\n */\n toolHistory?: () => Array<{ toolName: string; text: string }>;\n /**\n * Pre-captured inspection reports for each connected MCP server.\n * Populated once at chat startup (chat.tsx) so `/mcp` can render\n * tools + resources + prompts synchronously without needing async\n * handler support.\n */\n mcpServers?: McpServerSummary[];\n /**\n * Directory `/memory` should resolve `REASONIX.md` from. In code\n * mode this is the rootDir the filesystem tools are pinned to; in\n * plain chat this is `process.cwd()` at launch time. Absent → the\n * TUI is running in some non-cwd context (tests) and `/memory`\n * replies \"root unknown\" instead of silently reading a different dir.\n */\n memoryRoot?: string;\n /**\n * Current plan-mode state, surfaced in `/status` and toggled by\n * `/plan`. Present iff the session is a `reasonix code` run — chat\n * mode doesn't have plan mode.\n */\n planMode?: boolean;\n /**\n * Callback the `/plan` slash uses to flip plan mode on/off. Also\n * mirrors the state to the underlying ToolRegistry so dispatch\n * enforcement follows. Absent → `/plan` replies \"only available in\n * code mode\".\n */\n setPlanMode?: (on: boolean) => void;\n /**\n * Callback that clears a pending-plan picker state. Called by\n * `/apply-plan` so that when the user force-approves, the picker\n * dismisses without also firing its own approval synthetic (the\n * slash returns its own `resubmit` instead). Safe to call with no\n * pending plan.\n */\n clearPendingPlan?: () => void;\n /**\n * Re-load `~/.reasonix/settings.json` + `<project>/.reasonix/settings.json`\n * and update both the App's hook state and the loop's mutable hook\n * list. Returns the new hook count so the slash can echo a sane\n * confirmation. Absent → `/hooks reload` replies \"not available\".\n */\n reloadHooks?: () => number;\n /**\n * Latest published version if App's background registry check\n * has completed, `null` otherwise (still in flight OR offline).\n * Drives `/update` — the slash shows whatever the async check\n * already resolved, so the command is fully synchronous.\n */\n latestVersion?: string | null;\n /**\n * Fire-and-forget: kick off a fresh registry fetch. `/update`\n * calls this whenever it encounters `latestVersion === null`\n * so the user can rerun the slash a few seconds later and see\n * a concrete answer. Absent → the slash just reports \"pending\"\n * with no retry path.\n */\n refreshLatestVersion?: () => void;\n}\n\nexport interface McpServerSummary {\n /** Short label shown in the `/mcp` output (server namespace or \"anon\"). */\n label: string;\n /** Original --mcp spec string. */\n spec: string;\n /** Count of tools bridged into the Reasonix registry from this server. */\n toolCount: number;\n /** Full inspection snapshot — used for the resources + prompts sections. */\n report: InspectionReport;\n}\n\n/**\n * Slash command registry. Drives `/help`, the on-type suggestion\n * popup (`SlashSuggestions`), and auto-complete. Kept as data rather\n * than derived from the `handleSlash` switch so summaries can be\n * user-facing rather than code comments.\n *\n * `contextual` gates commands that only make sense in certain modes:\n * - `\"code\"` — only show when the TUI is running `reasonix code`\n * - absent → always shown\n */\nexport interface SlashCommandSpec {\n cmd: string;\n summary: string;\n contextual?: \"code\";\n /** If the command takes args, hint text shown after the name. */\n argsHint?: string;\n}\n\nexport const SLASH_COMMANDS: readonly SlashCommandSpec[] = [\n { cmd: \"help\", summary: \"show the full command reference\" },\n { cmd: \"status\", summary: \"current model, flags, context, session\" },\n {\n cmd: \"preset\",\n argsHint: \"<fast|smart|max>\",\n summary: \"one-tap model + harvest + branch bundle\",\n },\n { cmd: \"model\", argsHint: \"<id>\", summary: \"switch DeepSeek model id\" },\n { cmd: \"harvest\", argsHint: \"[on|off]\", summary: \"toggle Pillar-2 plan-state extraction\" },\n { cmd: \"branch\", argsHint: \"<N|off>\", summary: \"run N parallel samples per turn (N>=2)\" },\n { cmd: \"mcp\", summary: \"list MCP servers + tools attached to this session\" },\n { cmd: \"tool\", argsHint: \"[N]\", summary: \"dump full output of the Nth tool call (1=latest)\" },\n {\n cmd: \"memory\",\n argsHint: \"[list|show <name>|forget <name>|clear <scope> confirm]\",\n summary: \"show / manage pinned memory (REASONIX.md + ~/.reasonix/memory)\",\n },\n {\n cmd: \"skill\",\n argsHint: \"[list|show <name>|<name> [args]]\",\n summary: \"list / run user skills (<project>/.reasonix/skills + ~/.reasonix/skills)\",\n },\n {\n cmd: \"hooks\",\n argsHint: \"[reload]\",\n summary: \"list active hooks (settings.json under .reasonix/) · reload re-reads from disk\",\n },\n {\n cmd: \"update\",\n summary: \"show current vs latest version + the shell command to upgrade\",\n },\n {\n cmd: \"stats\",\n summary:\n \"cross-session cost dashboard (today / week / month / all-time · cache hit · vs Claude)\",\n },\n { cmd: \"think\", summary: \"dump the last turn's full R1 reasoning (reasoner only)\" },\n { cmd: \"retry\", summary: \"truncate & resend your last message (fresh sample)\" },\n { cmd: \"compact\", argsHint: \"[cap]\", summary: \"shrink oversized tool results in the log\" },\n { cmd: \"sessions\", summary: \"list saved sessions (current marked with ▸)\" },\n { cmd: \"forget\", summary: \"delete the current session from disk\" },\n { cmd: \"setup\", summary: \"reminds you to exit and run `reasonix setup`\" },\n { cmd: \"clear\", summary: \"clear visible scrollback only (log/context kept)\" },\n { cmd: \"new\", summary: \"start a fresh conversation (clear context + scrollback)\" },\n { cmd: \"exit\", summary: \"quit the TUI\" },\n // Code-mode only\n { cmd: \"apply\", summary: \"commit pending edit blocks to disk\", contextual: \"code\" },\n { cmd: \"discard\", summary: \"drop pending edit blocks without writing\", contextual: \"code\" },\n { cmd: \"undo\", summary: \"roll back the last applied edit batch\", contextual: \"code\" },\n {\n cmd: \"commit\",\n argsHint: '\"msg\"',\n summary: \"git add -A && git commit -m ...\",\n contextual: \"code\",\n },\n {\n cmd: \"plan\",\n argsHint: \"[on|off]\",\n summary: \"toggle read-only plan mode (writes bounced until submit_plan + approval)\",\n contextual: \"code\",\n },\n {\n cmd: \"apply-plan\",\n summary: \"force-approve a pending / in-text plan (fallback if picker was missed)\",\n contextual: \"code\",\n },\n];\n\n/**\n * Filter the registry by a prefix string (without the leading `/`).\n * Empty prefix returns the full non-contextual list (plus code-mode\n * entries when `codeMode` is true). Case-insensitive.\n */\nexport function suggestSlashCommands(prefix: string, codeMode = false): SlashCommandSpec[] {\n const p = prefix.toLowerCase();\n return SLASH_COMMANDS.filter((c) => {\n if (c.contextual === \"code\" && !codeMode) return false;\n return c.cmd.startsWith(p);\n });\n}\n\nexport function parseSlash(text: string): { cmd: string; args: string[] } | null {\n if (!text.startsWith(\"/\")) return null;\n const parts = text.slice(1).trim().split(/\\s+/);\n const cmd = parts[0]?.toLowerCase() ?? \"\";\n if (!cmd) return null;\n return { cmd, args: parts.slice(1) };\n}\n\nexport function handleSlash(\n cmd: string,\n args: string[],\n loop: CacheFirstLoop,\n ctx: SlashContext = {},\n): SlashResult {\n switch (cmd) {\n case \"exit\":\n case \"quit\":\n return { exit: true };\n\n case \"clear\":\n return {\n clear: true,\n info: \"▸ cleared visible scrollback only. Context (message log) is intact — next turn still sees everything. Use /new to start fresh, or /forget to delete the session entirely.\",\n };\n\n case \"new\":\n case \"reset\": {\n // Actually drop the in-memory log + rewrite the session file\n // so the NEXT call has no prior context. Keeps session name,\n // model, and other config — just the conversation is reset.\n const { dropped } = loop.clearLog();\n return {\n clear: true,\n info: `▸ new conversation — dropped ${dropped} message(s) from context. Same session, fresh slate.`,\n };\n }\n\n case \"help\":\n case \"?\":\n return {\n info: [\n \"Commands:\",\n \" /help this message\",\n \" /status show current settings\",\n \" /preset <fast|smart|max> one-tap presets — see below\",\n \" /model <id> deepseek-chat or deepseek-reasoner\",\n \" /harvest [on|off] Pillar 2: structured plan-state extraction\",\n \" /branch <N|off> run N parallel samples (N>=2), pick most confident\",\n \" /mcp list MCP servers + tools attached to this session\",\n \" /setup (exit + reconfigure) → run `reasonix setup`\",\n \" /compact [cap] shrink large tool results in history (default 4k/result)\",\n \" /think dump the most recent turn's full R1 reasoning (reasoner only)\",\n \" /tool [N] list tool calls (or dump full output of #N, 1=most recent)\",\n \" /memory [sub] show pinned memory (REASONIX.md + ~/.reasonix/memory).\",\n \" subs: list | show <name> | forget <name> | clear <scope> confirm\",\n \" /skill [sub] list / run user skills (project/.reasonix/skills + ~/.reasonix/skills).\",\n \" subs: list | show <name> | <name> [args] (injects skill body as user turn)\",\n \" /retry truncate & resend your last message (fresh sample from the model)\",\n \" /apply (code mode) commit the pending edit blocks to disk\",\n \" /discard (code mode) drop pending edits without writing\",\n \" /undo (code mode) roll back the last applied edit batch\",\n ' /commit \"msg\" (code mode) git add -A && git commit -m \"msg\"',\n \" /plan [on|off] (code mode) toggle read-only plan mode; writes gated behind submit_plan + your approval\",\n \" /apply-plan (code mode) force-approve pending/in-text plan (fallback)\",\n \" /sessions list saved sessions (current is marked with ▸)\",\n \" /forget delete the current session from disk\",\n \" /new start fresh: drop all context + clear scrollback\",\n \" /clear clear displayed scrollback only (context kept — model still sees it)\",\n \" /exit quit\",\n \"\",\n \"Presets:\",\n \" fast deepseek-chat no harvest no branch ~1¢/100turns ← default\",\n \" smart reasoner harvest ~10x cost, slower\",\n \" max reasoner harvest branch 3 ~30x cost, slowest\",\n \"\",\n \"Sessions (auto-enabled by default, named 'default'):\",\n \" reasonix chat --session <name> use a different named session\",\n \" reasonix chat --no-session disable persistence for this run\",\n ].join(\"\\n\"),\n };\n\n case \"mcp\": {\n const servers = ctx.mcpServers ?? [];\n const specs = ctx.mcpSpecs ?? [];\n const toolSpecs = loop.prefix.toolSpecs ?? [];\n if (servers.length === 0 && specs.length === 0 && toolSpecs.length === 0) {\n return {\n info:\n \"no MCP servers attached. Run `reasonix setup` to pick some, \" +\n 'or launch with --mcp \"<spec>\". `reasonix mcp list` shows the catalog.',\n };\n }\n // Rich path — we have full inspection reports, so show each\n // server with its tools / resources / prompts grouped together.\n if (servers.length > 0) {\n const lines: string[] = [];\n for (const s of servers) {\n const { report } = s;\n const serverName = report.serverInfo.name || \"(unknown)\";\n const serverVer = report.serverInfo.version ? ` v${report.serverInfo.version}` : \"\";\n lines.push(`[${s.label}] ${serverName}${serverVer} — ${s.spec}`);\n lines.push(` tools ${s.toolCount}`);\n appendSection(lines, \"resources\", report.resources);\n appendSection(lines, \"prompts \", report.prompts);\n lines.push(\"\");\n }\n lines.push(\n \"Chat mode consumes tools today; resources+prompts are surfaced here for awareness.\",\n );\n lines.push(\n \"Full catalog: `reasonix mcp list` · deeper diagnosis: `reasonix mcp inspect <spec>`.\",\n );\n return { info: lines.join(\"\\n\") };\n }\n // Fallback — older path when the TUI hasn't populated `mcpServers`.\n const lines: string[] = [];\n if (specs.length > 0) {\n lines.push(`MCP servers (${specs.length}):`);\n for (const spec of specs) lines.push(` · ${spec}`);\n lines.push(\"\");\n }\n if (toolSpecs.length > 0) {\n lines.push(`Tools in registry (${toolSpecs.length}):`);\n for (const t of toolSpecs) lines.push(` · ${t.function.name}`);\n }\n lines.push(\"\");\n lines.push(\"To change this set, exit and run `reasonix setup`.\");\n return { info: lines.join(\"\\n\") };\n }\n\n case \"setup\":\n return {\n info:\n \"To reconfigure (preset, MCP servers, API key), exit this chat and run \" +\n \"`reasonix setup`. Changes take effect on next launch.\",\n };\n\n case \"retry\": {\n const prev = loop.retryLastUser();\n if (!prev) {\n return {\n info: \"nothing to retry — no prior user message in this session's log.\",\n };\n }\n const preview = prev.length > 80 ? `${prev.slice(0, 80)}…` : prev;\n return {\n info: `▸ retrying: \"${preview}\"`,\n resubmit: prev,\n };\n }\n\n case \"memory\": {\n return handleMemorySlash(args, ctx);\n }\n\n case \"skill\":\n case \"skills\": {\n return handleSkillSlash(args, ctx);\n }\n\n case \"hook\":\n case \"hooks\": {\n return handleHooksSlash(args, loop, ctx);\n }\n\n case \"update\": {\n return handleUpdateSlash(ctx);\n }\n\n case \"stats\": {\n return handleStatsSlash();\n }\n\n case \"think\":\n case \"reasoning\": {\n const raw = loop.scratch.reasoning;\n if (!raw || !raw.trim()) {\n return {\n info:\n \"no reasoning cached. `/think` shows the full R1 thought for the most recent turn — \" +\n \"only `deepseek-reasoner` produces it, and only once the turn completes.\",\n };\n }\n return { info: `↳ full thinking (${raw.length} chars):\\n\\n${raw.trim()}` };\n }\n\n case \"tool\": {\n // EventLog truncates tool results at 400 chars for display. When\n // the user wants to check what the model actually read (e.g. to\n // verify it isn't hallucinating a file's contents), they need\n // the full text. `/tool` is the escape hatch.\n const history = ctx.toolHistory?.() ?? [];\n if (history.length === 0) {\n return {\n info:\n \"no tool calls yet in this session. `/tool` lists them once the model has actually \" +\n \"used a tool; `/tool N` dumps the full (untruncated) output of the Nth-most-recent.\",\n };\n }\n const raw = (args[0] ?? \"\").toLowerCase();\n if (raw === \"\" || raw === \"list\" || raw === \"ls\") {\n return { info: formatToolList(history) };\n }\n const n = Number.parseInt(raw, 10);\n if (!Number.isFinite(n) || n < 1) {\n return {\n info: \"usage: /tool [N] (no arg → list; N=1 → most recent result in full, N=2 → previous, …)\",\n };\n }\n if (n > history.length) {\n return {\n info: `only ${history.length} tool call(s) in history — asked for #${n}. Try /tool with no arg to see the list.`,\n };\n }\n const entry = history[history.length - n];\n if (!entry) {\n return { info: `could not read tool call #${n}` };\n }\n return {\n info: `↳ tool<${entry.toolName}> #${n} (${entry.text.length} chars):\\n\\n${entry.text}`,\n };\n }\n\n case \"undo\": {\n if (!ctx.codeUndo) {\n return {\n info: \"/undo is only available inside `reasonix code` — chat mode doesn't apply edits.\",\n };\n }\n return { info: ctx.codeUndo() };\n }\n\n case \"apply\": {\n if (!ctx.codeApply) {\n return {\n info: \"/apply is only available inside `reasonix code` (nothing to apply here).\",\n };\n }\n return { info: ctx.codeApply() };\n }\n\n case \"discard\": {\n if (!ctx.codeDiscard) {\n return {\n info: \"/discard is only available inside `reasonix code`.\",\n };\n }\n return { info: ctx.codeDiscard() };\n }\n\n case \"plan\": {\n if (!ctx.setPlanMode) {\n return {\n info: \"/plan is only available inside `reasonix code` — chat mode doesn't gate tool writes.\",\n };\n }\n const currentOn = Boolean(ctx.planMode);\n const raw = (args[0] ?? \"\").toLowerCase();\n let target: boolean;\n if (raw === \"on\" || raw === \"true\" || raw === \"1\") target = true;\n else if (raw === \"off\" || raw === \"false\" || raw === \"0\") target = false;\n else target = !currentOn;\n ctx.setPlanMode(target);\n if (target) {\n return {\n info: \"▸ plan mode ON — write tools are gated; the model MUST call `submit_plan` before anything executes. (The model can also call submit_plan on its own for big tasks even when plan mode is off — this toggle is the stronger, explicit constraint.) Type /plan off to leave.\",\n };\n }\n return {\n info: \"▸ plan mode OFF — write tools are live again. Model can still propose plans autonomously for large tasks.\",\n };\n }\n\n case \"apply-plan\":\n case \"applyplan\": {\n if (!ctx.setPlanMode) {\n return {\n info: \"/apply-plan is only available inside `reasonix code`.\",\n };\n }\n ctx.setPlanMode(false);\n ctx.clearPendingPlan?.();\n return {\n info: \"▸ plan approved — implementing\",\n resubmit:\n \"The plan above has been approved. Implement it now. You are out of plan mode — use edit_file / write_file / run_command as needed. Stick to the plan unless you discover a concrete reason to deviate; if you do, tell me and wait for a response before making that deviation.\",\n };\n }\n\n case \"commit\": {\n if (!ctx.codeRoot) {\n return {\n info: \"/commit is only available inside `reasonix code` (needs a rooted git repo).\",\n };\n }\n // Reassemble the original argv. The parser lowercases cmd but\n // leaves args alone, and the TUI splits on whitespace which\n // mangles quoted messages — rejoin with spaces and strip a\n // surrounding pair of double quotes if the user wrote them.\n const raw = args.join(\" \").trim();\n const message = stripOuterQuotes(raw);\n if (!message) {\n return {\n info: `usage: /commit \"your commit message\" — runs \\`git add -A && git commit -m \"…\"\\` in ${ctx.codeRoot}`,\n };\n }\n return runGitCommit(ctx.codeRoot, message);\n }\n\n case \"compact\": {\n // Manual companion to the automatic heal-on-load. Re-applies\n // truncation with a tighter cap (4k chars per tool result) and\n // rewrites the session file so the shrink persists. Useful when\n // the ctx gauge in StatsPanel goes yellow/red mid-session and\n // the user wants to keep chatting without /forget'ing everything.\n const tight = Number.parseInt(args[0] ?? \"\", 10);\n const cap = Number.isFinite(tight) && tight >= 500 ? tight : 4000;\n const { healedCount, charsSaved } = loop.compact(cap);\n if (healedCount === 0) {\n return {\n info: `▸ nothing to compact — no tool result in history exceeds ${cap.toLocaleString()} chars.`,\n };\n }\n return {\n info: `▸ compacted ${healedCount} tool result(s), saved ${charsSaved.toLocaleString()} chars (~${Math.round(charsSaved / 4).toLocaleString()} tokens). Session file rewritten.`,\n };\n }\n\n case \"sessions\": {\n const items = listSessions();\n if (items.length === 0) {\n return {\n info: \"no saved sessions yet — chat normally and your messages will be saved automatically\",\n };\n }\n const lines = [\"Saved sessions:\"];\n for (const s of items) {\n const sizeKb = (s.size / 1024).toFixed(1);\n const when = s.mtime.toISOString().replace(\"T\", \" \").slice(0, 16);\n const marker = s.name === loop.sessionName ? \"▸\" : \" \";\n lines.push(\n ` ${marker} ${s.name.padEnd(22)} ${String(s.messageCount).padStart(5)} msgs ${sizeKb.padStart(7)} KB ${when}`,\n );\n }\n lines.push(\"\");\n lines.push(\"Resume with: reasonix chat --session <name>\");\n return { info: lines.join(\"\\n\") };\n }\n\n case \"forget\": {\n if (!loop.sessionName) {\n return { info: \"not in a session — nothing to forget\" };\n }\n const name = loop.sessionName;\n const ok = deleteSession(name);\n return {\n info: ok\n ? `▸ deleted session \"${name}\" — current screen still shows the conversation, but next launch starts fresh`\n : `could not delete session \"${name}\" (already gone?)`,\n };\n }\n\n case \"status\": {\n const branchBudget = loop.branchOptions.budget ?? 1;\n const ctxMax = DEEPSEEK_CONTEXT_TOKENS[loop.model] ?? DEFAULT_CONTEXT_TOKENS;\n const lastPromptTokens = loop.stats.summary().lastPromptTokens;\n const ctxPct = ctxMax > 0 ? Math.round((lastPromptTokens / ctxMax) * 100) : 0;\n const ctxLine =\n lastPromptTokens > 0\n ? ` ctx ${compactNum(lastPromptTokens)}/${compactNum(ctxMax)} (${ctxPct}%)`\n : \" ctx no turns yet\";\n const pending = ctx.pendingEditCount ?? 0;\n const sessionLine = loop.sessionName\n ? ` session \"${loop.sessionName}\" · ${loop.log.length} messages in log (resumed ${loop.resumedMessageCount})`\n : \" session (ephemeral — no persistence)\";\n const mcpCount = ctx.mcpSpecs?.length ?? 0;\n const toolCount = loop.prefix.toolSpecs.length;\n const mcpLine = ` mcp ${mcpCount} server(s), ${toolCount} tool(s) in registry`;\n const pendingLine =\n pending > 0 ? ` edits ${pending} pending (/apply to commit, /discard to drop)` : \"\";\n const planLine = ctx.planMode ? \" plan ON — writes gated (submit_plan + approval)\" : \"\";\n const lines = [\n ` model ${loop.model}`,\n ` flags harvest=${loop.harvestEnabled ? \"on\" : \"off\"} · branch=${branchBudget > 1 ? branchBudget : \"off\"} · stream=${loop.stream ? \"on\" : \"off\"}`,\n ctxLine,\n mcpLine,\n sessionLine,\n ];\n if (pendingLine) lines.push(pendingLine);\n if (planLine) lines.push(planLine);\n return { info: lines.join(\"\\n\") };\n }\n\n case \"model\": {\n const id = args[0];\n if (!id) return { info: \"usage: /model <id> (try deepseek-chat or deepseek-reasoner)\" };\n loop.configure({ model: id });\n return { info: `model → ${id}` };\n }\n\n case \"harvest\": {\n const arg = (args[0] ?? \"\").toLowerCase();\n const on = arg === \"\" ? !loop.harvestEnabled : arg === \"on\" || arg === \"true\" || arg === \"1\";\n loop.configure({ harvest: on });\n return { info: `harvest → ${loop.harvestEnabled ? \"on\" : \"off\"}` };\n }\n\n case \"preset\": {\n const name = (args[0] ?? \"\").toLowerCase();\n if (name === \"fast\" || name === \"default\") {\n loop.configure({ model: \"deepseek-chat\", harvest: false, branch: 1 });\n return { info: \"preset → fast (deepseek-chat, no harvest, no branch)\" };\n }\n if (name === \"smart\") {\n loop.configure({ model: \"deepseek-reasoner\", harvest: true, branch: 1 });\n return { info: \"preset → smart (reasoner + harvest, ~10x cost vs fast)\" };\n }\n if (name === \"max\" || name === \"best\") {\n loop.configure({ model: \"deepseek-reasoner\", harvest: true, branch: 3 });\n return {\n info: \"preset → max (reasoner + harvest + branch3, ~30x cost vs fast, slowest)\",\n };\n }\n return { info: \"usage: /preset <fast|smart|max>\" };\n }\n\n case \"branch\": {\n const raw = (args[0] ?? \"\").toLowerCase();\n if (raw === \"\" || raw === \"off\" || raw === \"0\" || raw === \"1\") {\n loop.configure({ branch: 1 });\n return { info: \"branch → off\" };\n }\n const n = Number.parseInt(raw, 10);\n if (!Number.isFinite(n) || n < 2) {\n return { info: \"usage: /branch <N> (N>=2, or 'off')\" };\n }\n if (n > 8) {\n return { info: \"branch budget capped at 8 to prevent runaway cost\" };\n }\n loop.configure({ branch: n });\n return { info: `branch → ${n} (harvest auto-enabled; streaming disabled)` };\n }\n\n default:\n return { unknown: true, info: `unknown command: /${cmd} (try /help)` };\n }\n}\n\n/**\n * `/skill` family. Bare `/skill` (or `/skill list`) prints the\n * discovered skills from `<projectRoot>/.reasonix/skills` (code mode\n * only) + `~/.reasonix/skills`. `/skill show <name>` dumps one body\n * inline for reading. `/skill <name> [args...]` injects the skill body\n * as a user turn via `resubmit` — the same mechanism `/apply-plan`\n * uses — so the next model turn runs with the skill's instructions\n * fresh in the log.\n *\n * Project scope is only populated when the session has a `codeRoot`\n * (set by `reasonix code`). In plain chat mode the store reads the\n * global scope only, matching how user-memory behaves.\n */\n/**\n * `/update` — inside the TUI we deliberately do NOT spawn `npm install`.\n * stdio:inherit into a running Ink renderer corrupts the display, and\n * the process being upgraded is the same process that's still reading\n * its own binaries (messy on Windows). Instead we surface what we\n * already know from the App's background registry check and print the\n * exact shell command the user should run after exiting.\n *\n * The `latestVersion` ctx field is populated by App.tsx's mount-time\n * `getLatestVersion()` effect. When it's `null` we report the check\n * as pending/offline — still a useful output (current version + how\n * to force a fresh check from another terminal).\n */\n/**\n * `/stats` — dashboard view of `~/.reasonix/usage.jsonl`, the same\n * roll-up `reasonix stats` (no arg) prints at the shell. Synchronous\n * disk read; cheap enough that we don't bother caching between slash\n * invocations.\n *\n * No transcript-path variant in-TUI: the per-file summary is scripty\n * and rarely wanted mid-session. If someone needs it they have the\n * CLI form (`reasonix stats <path>`).\n */\nfunction handleStatsSlash(): SlashResult {\n const path = defaultUsageLogPath();\n const records = readUsageLog(path);\n if (records.length === 0) {\n return {\n info: [\n \"no usage data yet.\",\n \"\",\n ` ${path}`,\n \"\",\n \"every turn you run here appends one record — this session's turns\",\n \"will show up in the dashboard once you send a message.\",\n ].join(\"\\n\"),\n };\n }\n const agg = aggregateUsage(records);\n return { info: renderDashboard(agg, path) };\n}\n\nfunction handleUpdateSlash(ctx: SlashContext): SlashResult {\n const latest = ctx.latestVersion ?? null;\n const lines: string[] = [`current: reasonix ${VERSION}`];\n if (latest === null) {\n // Kick off a fresh fetch so a follow-up /update a few seconds\n // later has a real answer instead of the same pending message.\n ctx.refreshLatestVersion?.();\n lines.push(\n \"latest: (not yet resolved — background check in flight or offline)\",\n \"\",\n \"triggered a fresh registry fetch — retry `/update` in a few seconds,\",\n \"or run `reasonix update` in another terminal to force it synchronously.\",\n );\n return { info: lines.join(\"\\n\") };\n }\n lines.push(`latest: reasonix ${latest}`);\n const diff = compareVersions(VERSION, latest);\n if (diff >= 0) {\n lines.push(\"\", \"you're on the latest. nothing to do.\");\n return { info: lines.join(\"\\n\") };\n }\n if (isNpxInstall()) {\n lines.push(\n \"\",\n \"you're running via npx — the next `npx reasonix ...` launch will auto-fetch.\",\n \"to force a refresh sooner: `npm cache clean --force`.\",\n );\n } else {\n lines.push(\n \"\",\n \"to upgrade, exit this session and run:\",\n \" reasonix update (interactive, dry-run supported via --dry-run)\",\n \" npm install -g reasonix@latest (direct)\",\n \"\",\n \"in-session install is deliberately disabled — the npm spawn would\",\n \"corrupt this TUI's rendering and Windows can lock the running binary.\",\n );\n }\n return { info: lines.join(\"\\n\") };\n}\n\nfunction handleHooksSlash(args: string[], loop: CacheFirstLoop, ctx: SlashContext): SlashResult {\n const sub = (args[0] ?? \"\").toLowerCase();\n\n if (sub === \"reload\") {\n if (!ctx.reloadHooks) {\n return {\n info: \"/hooks reload is not available in this context (no reload callback wired).\",\n };\n }\n const count = ctx.reloadHooks();\n return { info: `▸ reloaded hooks · ${count} active` };\n }\n\n if (sub !== \"\" && sub !== \"list\" && sub !== \"ls\") {\n return {\n info: \"usage: /hooks list active hooks\\n /hooks reload re-read settings.json files\",\n };\n }\n\n const hooks = loop.hooks;\n const projPath = ctx.codeRoot ? projectSettingsPath(ctx.codeRoot) : undefined;\n const globPath = globalSettingsPath();\n if (hooks.length === 0) {\n const lines = [\n \"no hooks configured.\",\n \"\",\n \"drop a settings.json with a `hooks` key into either of:\",\n ctx.codeRoot\n ? ` · ${projPath} (project)`\n : \" · <project>/.reasonix/settings.json (project)\",\n ` · ${globPath} (global)`,\n \"\",\n \"events: PreToolUse, PostToolUse, UserPromptSubmit, Stop\",\n \"exit 0 = pass · exit 2 = block (Pre*) · other = warn\",\n ];\n return { info: lines.join(\"\\n\") };\n }\n\n const grouped = new Map<HookEvent, ResolvedHook[]>();\n for (const event of HOOK_EVENTS) grouped.set(event, []);\n for (const h of hooks) grouped.get(h.event)?.push(h);\n\n const lines: string[] = [`▸ ${hooks.length} hook(s) loaded`];\n for (const event of HOOK_EVENTS) {\n const list = grouped.get(event) ?? [];\n if (list.length === 0) continue;\n lines.push(\"\", `${event}:`);\n for (const h of list) {\n const match = h.match && h.match !== \"*\" ? ` match=${h.match}` : \"\";\n const desc = h.description ? ` — ${h.description}` : \"\";\n lines.push(` [${h.scope}]${match} ${h.command}${desc}`);\n }\n }\n lines.push(\"\", `sources: project=${projPath ?? \"(none — chat mode)\"} · global=${globPath}`);\n return { info: lines.join(\"\\n\") };\n}\n\nfunction handleSkillSlash(args: string[], ctx: SlashContext): SlashResult {\n const store = new SkillStore({ projectRoot: ctx.codeRoot });\n const sub = (args[0] ?? \"\").toLowerCase();\n\n if (sub === \"\" || sub === \"list\" || sub === \"ls\") {\n const skills = store.list();\n if (skills.length === 0) {\n const lines = [\"no skills found. Reasonix reads skills from:\"];\n if (store.hasProjectScope()) {\n lines.push(\n \" · <project>/.reasonix/skills/<name>/SKILL.md (or <name>.md) — project scope\",\n );\n }\n lines.push(\" · ~/.reasonix/skills/<name>/SKILL.md (or <name>.md) — global scope\");\n if (!store.hasProjectScope()) {\n lines.push(\" (project scope is only active in `reasonix code`)\");\n }\n lines.push(\n \"\",\n \"Each file's frontmatter needs at least `name` and `description`.\",\n \"Invoke a skill with `/skill <name> [args]` or by asking the model to call `run_skill`.\",\n );\n return { info: lines.join(\"\\n\") };\n }\n const lines = [`User skills (${skills.length}):`];\n for (const s of skills) {\n const scope = `(${s.scope})`.padEnd(11);\n const name = s.name.padEnd(24);\n const desc = s.description.length > 70 ? `${s.description.slice(0, 69)}…` : s.description;\n lines.push(` ${scope} ${name} ${desc}`);\n }\n lines.push(\"\");\n lines.push(\"View body: /skill show <name> Run: /skill <name> [args]\");\n return { info: lines.join(\"\\n\") };\n }\n\n if (sub === \"show\" || sub === \"cat\") {\n const target = args[1];\n if (!target) return { info: \"usage: /skill show <name>\" };\n const skill = store.read(target);\n if (!skill) return { info: `no skill found: ${target}` };\n return {\n info: [\n `▸ ${skill.name} (${skill.scope})`,\n skill.description ? ` ${skill.description}` : \"\",\n ` ${skill.path}`,\n \"\",\n skill.body,\n ]\n .filter((l) => l !== \"\")\n .join(\"\\n\"),\n };\n }\n\n // Bare `/skill <name> [args...]` — inject the body as a user turn.\n // The first arg is the skill name; remaining args are forwarded\n // verbatim as the skill's \"Arguments:\" line.\n const name = args[0] ?? \"\";\n const skill = store.read(name);\n if (!skill) {\n return {\n info: `no skill found: ${name} (try /skill list)`,\n };\n }\n const extra = args.slice(1).join(\" \").trim();\n const header = `# Skill: ${skill.name}${skill.description ? `\\n> ${skill.description}` : \"\"}`;\n const argsLine = extra ? `\\n\\nArguments: ${extra}` : \"\";\n const payload = `${header}\\n\\n${skill.body}${argsLine}`;\n return {\n info: `▸ running skill: ${skill.name}${extra ? ` — ${extra}` : \"\"}`,\n resubmit: payload,\n };\n}\n\n/**\n * `/memory` family. Bare `/memory` shows what's pinned (REASONIX.md +\n * both MEMORY.md blocks). Subcommands manage the user-memory store:\n * list — every memory file, both scopes\n * show <name> — dump one file's body\n * show <scope>/<name> — disambiguate when name exists in both scopes\n * forget <name> — delete (same scope resolution as show)\n * clear <scope> confirm — wipe a scope (typed literal \"confirm\" required)\n */\nfunction handleMemorySlash(args: string[], ctx: SlashContext): SlashResult {\n if (!memoryEnabled()) {\n return {\n info: \"memory is disabled (REASONIX_MEMORY=off in env). Unset the var to re-enable — no REASONIX.md or ~/.reasonix/memory content will be pinned in the meantime.\",\n };\n }\n if (!ctx.memoryRoot) {\n return {\n info: \"no working directory on this session — `/memory` needs a root to resolve REASONIX.md from. (Running in a test harness?)\",\n };\n }\n // `codeRoot` is set only when running `reasonix code`. Chat mode has\n // `memoryRoot` = cwd (for REASONIX.md), but we don't treat cwd as a\n // sandbox — project-scope user memory requires a real code-mode root.\n const store = new MemoryStore({ projectRoot: ctx.codeRoot });\n const sub = (args[0] ?? \"\").toLowerCase();\n\n if (sub === \"list\" || sub === \"ls\") {\n const entries = store.list();\n if (entries.length === 0) {\n return {\n info: \"no user memories yet. The model can call `remember` to save one, or you can create files by hand in ~/.reasonix/memory/global/ or the per-project subdir.\",\n };\n }\n const lines = [`User memories (${entries.length}):`];\n for (const e of entries) {\n const tag = `${e.scope}/${e.type}`.padEnd(18);\n const name = e.name.padEnd(28);\n const desc = e.description.length > 70 ? `${e.description.slice(0, 69)}…` : e.description;\n lines.push(` ${tag} ${name} ${desc}`);\n }\n lines.push(\"\");\n lines.push(\"View body: /memory show <name> Delete: /memory forget <name>\");\n return { info: lines.join(\"\\n\") };\n }\n\n if (sub === \"show\" || sub === \"cat\") {\n const target = args[1];\n if (!target) return { info: \"usage: /memory show <name> or /memory show <scope>/<name>\" };\n const resolved = resolveMemoryTarget(store, target);\n if (!resolved) return { info: `no memory found: ${target}` };\n try {\n const entry = store.read(resolved.scope, resolved.name);\n return {\n info: [\n `▸ ${entry.scope}/${entry.name} (${entry.type}, created ${entry.createdAt || \"?\"})`,\n entry.description ? ` ${entry.description}` : \"\",\n \"\",\n entry.body,\n ]\n .filter((l) => l !== \"\")\n .concat(\"\")\n .join(\"\\n\"),\n };\n } catch (err) {\n return { info: `show failed: ${(err as Error).message}` };\n }\n }\n\n if (sub === \"forget\" || sub === \"rm\" || sub === \"delete\") {\n const target = args[1];\n if (!target) return { info: \"usage: /memory forget <name> or /memory forget <scope>/<name>\" };\n const resolved = resolveMemoryTarget(store, target);\n if (!resolved) return { info: `no memory found: ${target}` };\n try {\n const ok = store.delete(resolved.scope, resolved.name);\n return {\n info: ok\n ? `▸ forgot ${resolved.scope}/${resolved.name}. Next /new or launch won't see it.`\n : `could not forget ${resolved.scope}/${resolved.name} (already gone?)`,\n };\n } catch (err) {\n return { info: `forget failed: ${(err as Error).message}` };\n }\n }\n\n if (sub === \"clear\") {\n const rawScope = (args[1] ?? \"\").toLowerCase();\n if (rawScope !== \"global\" && rawScope !== \"project\") {\n return { info: \"usage: /memory clear <global|project> confirm\" };\n }\n if ((args[2] ?? \"\").toLowerCase() !== \"confirm\") {\n return {\n info: `about to delete every memory in scope=${rawScope}. Re-run with the word 'confirm' to proceed: /memory clear ${rawScope} confirm`,\n };\n }\n const scope = rawScope as MemoryScope;\n const entries = store.list().filter((e) => e.scope === scope);\n let deleted = 0;\n for (const e of entries) {\n try {\n if (store.delete(scope, e.name)) deleted++;\n } catch {\n /* skip */\n }\n }\n return { info: `▸ cleared scope=${scope} — deleted ${deleted} memory file(s).` };\n }\n\n // Bare `/memory` — show REASONIX.md + both MEMORY.md blocks.\n const parts: string[] = [];\n const projMem = readProjectMemory(ctx.memoryRoot);\n if (projMem) {\n const hdr = projMem.truncated\n ? `▸ ${PROJECT_MEMORY_FILE}: ${projMem.path} (${projMem.originalChars.toLocaleString()} chars, truncated)`\n : `▸ ${PROJECT_MEMORY_FILE}: ${projMem.path} (${projMem.originalChars.toLocaleString()} chars)`;\n parts.push(hdr, \"\", projMem.content);\n }\n const globalIdx = store.loadIndex(\"global\");\n if (globalIdx) {\n parts.push(\n \"\",\n `▸ global memory (${globalIdx.originalChars.toLocaleString()} chars${globalIdx.truncated ? \", truncated\" : \"\"})`,\n \"\",\n globalIdx.content,\n );\n }\n const projectIdx = store.loadIndex(\"project\");\n if (projectIdx) {\n parts.push(\n \"\",\n `▸ project memory (${projectIdx.originalChars.toLocaleString()} chars${projectIdx.truncated ? \", truncated\" : \"\"})`,\n \"\",\n projectIdx.content,\n );\n }\n if (parts.length === 0) {\n return {\n info: [\n `no memory pinned in ${ctx.memoryRoot}.`,\n \"\",\n \"Three layers are available:\",\n ` 1. ${PROJECT_MEMORY_FILE} — committable team memory (in the repo).`,\n \" 2. ~/.reasonix/memory/global/ — your cross-project private memory.\",\n ` 3. ~/.reasonix/memory/<project-hash>/ — this project's private memory.`,\n \"\",\n \"Ask the model to `remember` something, or hand-edit files directly.\",\n \"Changes take effect on next /new or launch — the system prompt is hashed once per session to keep the prefix cache warm.\",\n \"\",\n \"Subcommands: /memory list | /memory show <name> | /memory forget <name> | /memory clear <scope> confirm\",\n ].join(\"\\n\"),\n };\n }\n parts.push(\n \"\",\n \"Changes take effect on next /new or launch. Subcommands: /memory list | show | forget | clear\",\n );\n return { info: parts.join(\"\\n\") };\n}\n\n/**\n * Parse a `/memory show|forget` argument. Accepts bare `<name>` or\n * `<scope>/<name>`. For bare names, tries project scope first (more\n * specific, usually what the user means) then falls back to global.\n */\nfunction resolveMemoryTarget(\n store: MemoryStore,\n raw: string,\n): { scope: MemoryScope; name: string } | null {\n const slash = raw.indexOf(\"/\");\n if (slash > 0) {\n const scopeRaw = raw.slice(0, slash).toLowerCase();\n const name = raw.slice(slash + 1);\n if (scopeRaw !== \"global\" && scopeRaw !== \"project\") return null;\n const scope = scopeRaw as MemoryScope;\n if (scope === \"project\" && !store.hasProjectScope()) return null;\n return { scope, name };\n }\n for (const scope of [\"project\", \"global\"] as MemoryScope[]) {\n if (scope === \"project\" && !store.hasProjectScope()) continue;\n try {\n store.read(scope, raw);\n return { scope, name: raw };\n } catch {\n /* next scope */\n }\n }\n return null;\n}\n\n/**\n * Render a section (resources / prompts) of an MCP inspection into a\n * compact \"name count items\" form, collapsing when unsupported.\n * Names-only — descriptions and full metadata live in\n * `reasonix mcp inspect`, which is purpose-built for the deep view.\n */\nfunction appendSection(\n lines: string[],\n label: string,\n section:\n | { supported: true; items: Array<{ name: string }> }\n | { supported: false; reason: string }\n | undefined,\n): void {\n if (!section || !section.supported) {\n lines.push(\n ` ${label.trim()} ${section?.supported === false ? \"(not supported)\" : \"(none)\"}`,\n );\n return;\n }\n const names = section.items.map((i) => i.name);\n if (names.length === 0) {\n lines.push(` ${label.trim()} (none)`);\n return;\n }\n const head = names.slice(0, 5).join(\", \");\n const more = names.length > 5 ? ` +${names.length - 5} more` : \"\";\n lines.push(` ${label.trim()} ${names.length} [${head}${more}]`);\n}\n\nfunction formatToolList(history: Array<{ toolName: string; text: string }>): string {\n const total = history.length;\n const header = `Tool calls in this session (${total}, most recent first):`;\n // Show the 10 most recent. Older ones are rarely what the user\n // wants — and the help footer tells them how to reach any entry\n // by index if they do.\n const shown = Math.min(total, 10);\n const lines: string[] = [header];\n for (let i = 0; i < shown; i++) {\n const entry = history[total - 1 - i];\n if (!entry) continue;\n const idx = i + 1; // 1-based from most recent\n const flat = entry.text.replace(/\\s+/g, \" \").trim();\n const preview = flat.length > 80 ? `${flat.slice(0, 80)}…` : flat;\n const name = entry.toolName.length > 24 ? `${entry.toolName.slice(0, 23)}…` : entry.toolName;\n lines.push(\n ` #${String(idx).padStart(2)} ${name.padEnd(24)} ${String(entry.text.length).padStart(6)} chars ${preview}`,\n );\n }\n if (total > shown) {\n lines.push(` … (${total - shown} earlier, reach with /tool N)`);\n }\n lines.push(\"\");\n lines.push(\"View full output: /tool N (N=1 → most recent)\");\n return lines.join(\"\\n\");\n}\n\nfunction compactNum(n: number): string {\n if (n < 1000) return String(n);\n const k = n / 1000;\n return k >= 100 ? `${Math.round(k)}k` : `${k.toFixed(1)}k`;\n}\n\nfunction stripOuterQuotes(s: string): string {\n if (s.length >= 2 && s.startsWith('\"') && s.endsWith('\"')) {\n return s.slice(1, -1);\n }\n return s;\n}\n\n/**\n * Run `git add -A` then `git commit -m <message>` in `rootDir`. Returns\n * a SlashResult with a human-scannable info line. We surface stderr on\n * failure so the user sees exactly what git complained about (bad\n * config, pre-commit hook rejection, nothing staged, etc.).\n */\nfunction runGitCommit(rootDir: string, message: string): SlashResult {\n const add = spawnSync(\"git\", [\"add\", \"-A\"], { cwd: rootDir, encoding: \"utf8\" });\n if (add.error || add.status !== 0) {\n return { info: `git add failed (${add.status ?? \"?\"}):\\n${gitTail(add)}` };\n }\n const commit = spawnSync(\"git\", [\"commit\", \"-m\", message], {\n cwd: rootDir,\n encoding: \"utf8\",\n });\n if (commit.error || commit.status !== 0) {\n return { info: `git commit failed (${commit.status ?? \"?\"}):\\n${gitTail(commit)}` };\n }\n const firstLine = (commit.stdout || \"\").split(/\\r?\\n/)[0] ?? \"\";\n return { info: `▸ committed: ${message}${firstLine ? `\\n ${firstLine}` : \"\"}` };\n}\n\n/**\n * Safely extract whatever diagnostic text is available from a spawnSync\n * result — on Windows or when cwd doesn't exist, `stderr`/`stdout` can\n * be `undefined` and the caller has only `error.message` to go on.\n */\nfunction gitTail(res: ReturnType<typeof spawnSync>): string {\n const stderr = (res.stderr as string | undefined) ?? \"\";\n const stdout = (res.stdout as string | undefined) ?? \"\";\n const body = stderr.trim() || stdout.trim();\n if (body) return body;\n if (res.error) return (res.error as Error).message;\n return \"(no output from git)\";\n}\n","/**\n * `reasonix stats` — two modes, decided by whether the user passed\n * a transcript path.\n *\n * - `reasonix stats` → cross-session usage dashboard\n * (reads `~/.reasonix/usage.jsonl`)\n * - `reasonix stats <path>` → summary of a single JSONL transcript\n * (original v0.1 behavior, kept so\n * existing scripts don't break)\n *\n * The dashboard is the new story: for every turn `reasonix chat|code|run`\n * has ever executed on this machine, we aggregate tokens + cost + the\n * equivalent Claude spend, and print rolling-window totals (today /\n * week / month / all-time). The savings column is the Pillar 1 pitch\n * made concrete — \"you paid $X; Claude would have charged $Y, a N%\n * reduction.\"\n */\n\nimport { existsSync, readFileSync } from \"node:fs\";\nimport {\n type UsageAggregate,\n type UsageBucket,\n aggregateUsage,\n bucketCacheHitRatio,\n bucketSavingsFraction,\n defaultUsageLogPath,\n formatLogSize,\n readUsageLog,\n} from \"../../usage.js\";\n\nexport interface StatsOptions {\n /** Optional transcript path. Absent → dashboard mode. */\n transcript?: string;\n /** Override usage log location (tests). */\n logPath?: string;\n /** Inject a fixed timestamp (tests) so rolling windows are deterministic. */\n now?: number;\n}\n\nexport function statsCommand(opts: StatsOptions): void {\n if (opts.transcript) {\n transcriptSummary(opts.transcript);\n return;\n }\n dashboard(opts);\n}\n\n// ---------------------------------------------------------------------\n// Transcript summary — legacy v0.1 behavior, preserved verbatim.\n// ---------------------------------------------------------------------\n\nfunction transcriptSummary(path: string): void {\n if (!existsSync(path)) {\n console.error(`no such transcript: ${path}`);\n process.exit(1);\n }\n const lines = readFileSync(path, \"utf8\").split(/\\r?\\n/).filter(Boolean);\n let assistantTurns = 0;\n let toolCalls = 0;\n let lastTurn = 0;\n for (const line of lines) {\n try {\n const rec = JSON.parse(line);\n if (rec.role === \"assistant_final\") assistantTurns++;\n if (rec.role === \"tool\") toolCalls++;\n if (typeof rec.turn === \"number\") lastTurn = Math.max(lastTurn, rec.turn);\n } catch {\n /* skip */\n }\n }\n console.log(`transcript: ${path}`);\n console.log(`assistant turns: ${assistantTurns}`);\n console.log(`tool invocations: ${toolCalls}`);\n console.log(`last turn index: ${lastTurn}`);\n}\n\n// ---------------------------------------------------------------------\n// Dashboard — aggregates ~/.reasonix/usage.jsonl.\n// ---------------------------------------------------------------------\n\nfunction dashboard(opts: StatsOptions): void {\n const path = opts.logPath ?? defaultUsageLogPath();\n const records = readUsageLog(path);\n if (records.length === 0) {\n console.log(\"no usage data yet.\");\n console.log(\"\");\n console.log(` ${path}`);\n console.log(\"\");\n console.log(\"run `reasonix chat`, `reasonix code`, or `reasonix run <task>` — every turn\");\n console.log(\"appends one line to the log and `reasonix stats` will roll it up.\");\n return;\n }\n\n const agg = aggregateUsage(records, { now: opts.now });\n console.log(renderDashboard(agg, path));\n}\n\n/** Pure renderer — pulled out so tests can assert on the string directly. */\nexport function renderDashboard(agg: UsageAggregate, logPath: string): string {\n const lines: string[] = [];\n const size = formatLogSize(logPath);\n lines.push(`Reasonix usage — ${logPath}${size ? ` (${size})` : \"\"}`);\n lines.push(\"\");\n lines.push(header());\n lines.push(divider());\n for (const b of agg.buckets) {\n lines.push(bucketRow(b));\n }\n lines.push(\"\");\n\n // Model + session breakdown — both trim to top 3 so a user with 20\n // sessions doesn't drown the table.\n if (agg.byModel.length > 0) {\n const totalTurns = agg.buckets[agg.buckets.length - 1]?.turns ?? 0;\n const top = agg.byModel[0];\n if (top && totalTurns > 0) {\n const pct = ((top.turns / totalTurns) * 100).toFixed(0);\n lines.push(`most used model: ${top.model} (${pct}% of turns)`);\n }\n }\n if (agg.bySession.length > 0) {\n const top = agg.bySession[0];\n if (top) lines.push(`top session: ${top.session} (${top.turns} turns)`);\n }\n if (agg.firstSeen) {\n lines.push(`tracked since: ${new Date(agg.firstSeen).toISOString().slice(0, 10)}`);\n }\n return lines.join(\"\\n\");\n}\n\nfunction header(): string {\n // Fixed column widths so alignment works in any TTY.\n return [\n pad(\"\", 10),\n pad(\"turns\", 8, \"right\"),\n pad(\"cache hit\", 10, \"right\"),\n pad(\"cost (USD)\", 14, \"right\"),\n pad(\"vs Claude\", 14, \"right\"),\n pad(\"saved\", 10, \"right\"),\n ].join(\" \");\n}\n\nfunction divider(): string {\n return \"-\".repeat(70);\n}\n\nfunction bucketRow(b: UsageBucket): string {\n const hit = bucketCacheHitRatio(b);\n const savings = bucketSavingsFraction(b);\n return [\n pad(b.label, 10),\n pad(b.turns.toString(), 8, \"right\"),\n pad(b.turns > 0 ? `${(hit * 100).toFixed(1)}%` : \"—\", 10, \"right\"),\n pad(b.turns > 0 ? `$${b.costUsd.toFixed(6)}` : \"—\", 14, \"right\"),\n pad(b.turns > 0 ? `$${b.claudeEquivUsd.toFixed(4)}` : \"—\", 14, \"right\"),\n pad(b.turns > 0 && savings > 0 ? `${(savings * 100).toFixed(1)}%` : \"—\", 10, \"right\"),\n ].join(\" \");\n}\n\nfunction pad(s: string, width: number, align: \"left\" | \"right\" = \"left\"): string {\n if (s.length >= width) return s;\n const fill = \" \".repeat(width - s.length);\n return align === \"right\" ? `${fill}${s}` : `${s}${fill}`;\n}\n","import { Box, Text } from \"ink\";\nimport React from \"react\";\nimport { SingleSelect } from \"./Select.js\";\n\nexport type SessionChoice = \"resume\" | \"new\" | \"delete\";\n\nexport interface SessionPickerProps {\n sessionName: string;\n messageCount: number;\n /** mtime of the session file; used to render \"last active Nh ago\". */\n lastActive: Date;\n onChoose: (choice: SessionChoice) => void;\n}\n\nexport function SessionPicker({\n sessionName,\n messageCount,\n lastActive,\n onChoose,\n}: SessionPickerProps) {\n return (\n <Box flexDirection=\"column\" marginY={1}>\n <Box marginBottom={1}>\n <Text bold color=\"cyan\">\n {`Session \"${sessionName}\" has ${messageCount} prior message${messageCount === 1 ? \"\" : \"s\"}`}\n </Text>\n <Text dimColor>{` · last active ${relativeTime(lastActive)}`}</Text>\n </Box>\n <SingleSelect\n initialValue=\"new\"\n items={[\n {\n value: \"new\",\n label: \"Start new conversation\",\n hint: \"Previous messages kept on disk; your turn starts fresh.\",\n },\n {\n value: \"resume\",\n label: \"Resume\",\n hint: `Continue where you left off (${messageCount} messages in context).`,\n },\n {\n value: \"delete\",\n label: \"Delete and start new\",\n hint: \"Wipes the session file irreversibly. Other sessions untouched.\",\n },\n ]}\n onSubmit={(v) => onChoose(v as SessionChoice)}\n />\n <Box marginTop={1}>\n <Text dimColor>↑↓ to move · Enter to pick</Text>\n </Box>\n </Box>\n );\n}\n\n/**\n * \"Nh ago\" / \"Nm ago\" / \"yesterday\" style relative time. Deliberately\n * coarse — the picker just needs a sense of \"how stale is this session\".\n */\nfunction relativeTime(date: Date): string {\n const ms = Date.now() - date.getTime();\n const mins = Math.floor(ms / 60_000);\n if (mins < 1) return \"just now\";\n if (mins < 60) return `${mins}m ago`;\n const hours = Math.floor(mins / 60);\n if (hours < 24) return `${hours}h ago`;\n const days = Math.floor(hours / 24);\n if (days === 1) return \"yesterday\";\n if (days < 7) return `${days}d ago`;\n return date.toISOString().slice(0, 10);\n}\n","import { Box, Text, useApp } from \"ink\";\nimport TextInput from \"ink-text-input\";\nimport React, { useState } from \"react\";\nimport { defaultConfigPath, isPlausibleKey, redactKey, saveApiKey } from \"../../config.js\";\n\nexport interface SetupProps {\n onReady: (apiKey: string) => void;\n}\n\nexport function Setup({ onReady }: SetupProps) {\n const [value, setValue] = useState(\"\");\n const [error, setError] = useState<string | null>(null);\n const { exit } = useApp();\n\n const handleSubmit = (raw: string) => {\n const trimmed = raw.trim();\n if (trimmed === \"/exit\" || trimmed === \"/quit\") {\n exit();\n return;\n }\n if (!isPlausibleKey(trimmed)) {\n setError(\"Doesn't look like a DeepSeek key. They start with 'sk-' and are 30+ chars.\");\n setValue(\"\");\n return;\n }\n try {\n saveApiKey(trimmed);\n } catch (err) {\n setError(`Could not save key: ${(err as Error).message}`);\n return;\n }\n onReady(trimmed);\n };\n\n return (\n <Box flexDirection=\"column\" borderStyle=\"round\" borderColor=\"cyan\" paddingX={1}>\n <Text bold color=\"cyan\">\n Welcome to Reasonix.\n </Text>\n <Box marginTop={1}>\n <Text>Paste your DeepSeek API key to get started.</Text>\n </Box>\n <Text dimColor>Get one (free credit on signup): https://platform.deepseek.com/api_keys</Text>\n <Text dimColor>Saved locally to {defaultConfigPath()}</Text>\n <Box marginTop={1}>\n <Text bold color=\"cyan\">\n {\"key › \"}\n </Text>\n <TextInput\n value={value}\n onChange={setValue}\n onSubmit={handleSubmit}\n mask=\"•\"\n placeholder=\"sk-...\"\n />\n </Box>\n {error ? (\n <Box marginTop={1}>\n <Text color=\"red\">{error}</Text>\n </Box>\n ) : value ? (\n <Box marginTop={1}>\n <Text dimColor>preview: {redactKey(value)}</Text>\n </Box>\n ) : null}\n <Box marginTop={1}>\n <Text dimColor>(Type /exit to abort.)</Text>\n </Box>\n </Box>\n );\n}\n","/**\n * `reasonix code [dir]` — opinionated wrapper around `reasonix chat` for\n * code-editing workflows.\n *\n * What it does differently from plain chat:\n * - Registers native filesystem tools rooted at the given directory\n * (CWD by default). No subprocess, no `npx install` step, R1-\n * friendly schemas. Replaced the old `@modelcontextprotocol/server-filesystem`\n * subprocess in 0.4.9 because its `edit_file` argv shape was the\n * biggest driver of R1 DSML hallucinations.\n * - Uses a coding-focused system prompt (src/code/prompt.ts) that\n * teaches the model to propose edits as SEARCH/REPLACE blocks.\n * - Defaults to the `smart` preset (reasoner + harvest) because\n * coding tasks pay back R1 thinking.\n * - Scopes its session to the directory so projects don't share\n * conversation history.\n * - Hooks `codeMode` into the TUI so assistant replies get parsed\n * for SEARCH/REPLACE blocks and applied on disk after each turn.\n */\n\nimport { basename, resolve } from \"node:path\";\nimport { loadProjectShellAllowed } from \"../../config.js\";\nimport { sanitizeName } from \"../../session.js\";\nimport { ToolRegistry } from \"../../tools.js\";\nimport { registerFilesystemTools } from \"../../tools/filesystem.js\";\nimport { registerMemoryTools } from \"../../tools/memory.js\";\nimport { registerPlanTool } from \"../../tools/plan.js\";\nimport { registerShellTools } from \"../../tools/shell.js\";\nimport { chatCommand } from \"./chat.js\";\n\nexport interface CodeOptions {\n /** Directory to root the filesystem tools at. Defaults to process.cwd(). */\n dir?: string;\n /** Override the default `smart` model. */\n model?: string;\n /** Disable session persistence. */\n noSession?: boolean;\n /** Transcript file for replay/diff. */\n transcript?: string;\n /** Skip the session picker — always resume prior messages. */\n forceResume?: boolean;\n /** Skip the session picker — always wipe prior messages and start fresh. */\n forceNew?: boolean;\n}\n\nexport async function codeCommand(opts: CodeOptions = {}): Promise<void> {\n const { codeSystemPrompt } = await import(\"../../code/prompt.js\");\n const rootDir = resolve(opts.dir ?? process.cwd());\n // Per-directory session so switching projects doesn't mix histories.\n // `code-<sanitized-basename>` fits the session name rules without\n // truncating most project names.\n const session = opts.noSession ? undefined : `code-${sanitizeName(basename(rootDir))}`;\n\n // Native filesystem tools. No subprocess, ~50-200 ms faster per call\n // than the MCP server was, and `edit_file` takes a flat SEARCH/REPLACE\n // shape instead of the `string=\"false\"` JSON-in-string array that\n // triggered R1's DSML hallucinations all through 0.4.x.\n const tools = new ToolRegistry();\n registerFilesystemTools(tools, { rootDir });\n registerShellTools(tools, {\n rootDir,\n // Per-project \"always allow\" list persisted from prior ShellConfirm\n // choices; merged on top of the built-in allowlist in shell.ts.\n // GETTER form — re-read every dispatch so a prefix the user adds\n // via ShellConfirm mid-session takes effect on the next shell call\n // instead of waiting for `/new` or a relaunch.\n extraAllowed: () => loadProjectShellAllowed(rootDir),\n });\n // `submit_plan` is always in the spec list so the prefix cache stays\n // stable across plan-mode toggles (Pillar 1). The tool itself is a\n // no-op outside plan mode and throws `PlanProposedError` when the\n // user has `/plan`-enabled the session.\n registerPlanTool(tools);\n // `remember` / `forget` / `recall_memory` — cross-session user memory.\n // Project scope hashes off rootDir so switching projects gets a fresh\n // per-project memory store; the global scope is shared across runs.\n registerMemoryTools(tools, { projectRoot: rootDir });\n // `run_skill` is intentionally NOT registered here — App.tsx wires it\n // up with the subagent runner attached, so `runAs: subagent` skills\n // can spawn isolated child loops. Doing it here would mean the App's\n // re-registration would shadow the no-runner version, which works\n // (last write wins) but obscures the wiring.\n\n process.stderr.write(\n `▸ reasonix code: rooted at ${rootDir}, session \"${session ?? \"(ephemeral)\"}\" · ${tools.size} native tool(s)\\n`,\n );\n\n await chatCommand({\n model: opts.model ?? \"deepseek-reasoner\",\n harvest: true, // smart preset's harvest setting, always on for code\n system: codeSystemPrompt(rootDir),\n transcript: opts.transcript,\n session,\n seedTools: tools,\n codeMode: { rootDir },\n forceResume: opts.forceResume,\n forceNew: opts.forceNew,\n });\n}\n","import { writeFileSync } from \"node:fs\";\nimport { basename } from \"node:path\";\nimport { render } from \"ink\";\nimport React from \"react\";\nimport { diffTranscripts, renderMarkdown, renderSummaryTable } from \"../../diff.js\";\nimport { readTranscript } from \"../../transcript.js\";\nimport { DiffApp } from \"../ui/DiffApp.js\";\n\nexport interface DiffOptions {\n a: string;\n b: string;\n mdPath?: string;\n labelA?: string;\n labelB?: string;\n /** Force stdout summary table (no Ink TUI). Auto when stdout isn't a TTY. */\n print?: boolean;\n /** Force the TUI even when stdout isn't a TTY (rare). */\n tui?: boolean;\n}\n\n/**\n * Compare two transcripts. Three output paths, picked in order:\n * - If --md is passed: write the markdown report. Also prints the stdout\n * summary so the user sees what was exported.\n * - If --print, no TTY, or --md (see above): stdout summary table.\n * - Otherwise: interactive Ink TUI with split-pane + n/N divergence jump.\n */\nexport async function diffCommand(opts: DiffOptions): Promise<void> {\n const aParsed = readTranscript(opts.a);\n const bParsed = readTranscript(opts.b);\n\n const report = diffTranscripts(\n { label: opts.labelA ?? basename(opts.a), parsed: aParsed },\n { label: opts.labelB ?? basename(opts.b), parsed: bParsed },\n );\n\n const wantMarkdown = !!opts.mdPath;\n const wantPrint = opts.print || !process.stdout.isTTY;\n const wantTui = opts.tui || (!wantPrint && !wantMarkdown);\n\n if (wantMarkdown) {\n // Markdown export implies the user wants an artifact, not a TUI.\n // Still echo the stdout summary to confirm the action.\n console.log(renderSummaryTable(report));\n const md = renderMarkdown(report);\n writeFileSync(opts.mdPath!, md, \"utf8\");\n console.log(`\\nmarkdown report written to ${opts.mdPath}`);\n return;\n }\n\n if (wantTui) {\n const { waitUntilExit } = render(React.createElement(DiffApp, { report }), {\n exitOnCtrlC: true,\n patchConsole: false,\n });\n await waitUntilExit();\n return;\n }\n\n // stdout fallback (piped, --print, or non-TTY)\n console.log(renderSummaryTable(report));\n}\n","/**\n * Ink TUI for `reasonix diff`. Split-pane: A on the left, B on the right,\n * shared cursor. Header shows aggregate deltas; footer shows the current\n * pair's divergence note (if any) + key cheat sheet.\n *\n * j/k moves the cursor by one turn; n/N jumps to the next/prev divergent\n * turn — which is the whole point of a diff tool. Quit with q.\n *\n * Pure navigation lives in src/diff.ts (findNextDivergence / findPrevDivergence).\n */\n\nimport { Box, Static, Text, useApp, useInput } from \"ink\";\nimport React, { useState } from \"react\";\nimport {\n type DiffReport,\n type TurnPair,\n findNextDivergence,\n findPrevDivergence,\n} from \"../../diff.js\";\nimport { RecordView } from \"./RecordView.js\";\n\nexport interface DiffAppProps {\n report: DiffReport;\n}\n\nexport function DiffApp({ report }: DiffAppProps) {\n const { exit } = useApp();\n const maxIdx = Math.max(0, report.pairs.length - 1);\n // Start at the first divergence when one exists — that's the user's most\n // likely destination. Falls back to idx 0 for fully-matching diffs.\n const initialIdx = report.firstDivergenceTurn\n ? report.pairs.findIndex((p) => p.turn === report.firstDivergenceTurn)\n : 0;\n const [idx, setIdx] = useState(Math.max(0, initialIdx));\n\n useInput((input, key) => {\n if (input === \"q\" || (key.ctrl && input === \"c\")) {\n exit();\n return;\n }\n if (input === \"j\" || key.downArrow || input === \" \" || key.return) {\n setIdx((i) => Math.min(maxIdx, i + 1));\n } else if (input === \"k\" || key.upArrow) {\n setIdx((i) => Math.max(0, i - 1));\n } else if (input === \"g\") {\n setIdx(0);\n } else if (input === \"G\") {\n setIdx(maxIdx);\n } else if (input === \"n\") {\n const next = findNextDivergence(report.pairs, idx);\n if (next !== -1) setIdx(next);\n } else if (input === \"N\" || input === \"p\") {\n const prev = findPrevDivergence(report.pairs, idx);\n if (prev !== -1) setIdx(prev);\n }\n });\n\n const pair = report.pairs[idx];\n\n return (\n <Box flexDirection=\"column\">\n <DiffHeader report={report} />\n\n <Box marginTop={1} paddingX={1} justifyContent=\"space-between\">\n <Text color=\"cyan\" bold>\n turn {pair?.turn ?? \"?\"} ({idx + 1} / {report.pairs.length})\n </Text>\n <Text>{pair ? <KindBadge kind={pair.kind} /> : null}</Text>\n </Box>\n\n <Box flexDirection=\"row\" marginTop={1}>\n <Pane label={report.a.label} headerColor=\"blue\" records={paneRecords(pair, \"a\")} />\n <Pane label={report.b.label} headerColor=\"magenta\" records={paneRecords(pair, \"b\")} />\n </Box>\n\n {pair?.divergenceNote ? (\n <Box marginTop={1} paddingX={1}>\n <Text color=\"yellow\">★ </Text>\n <Text>{pair.divergenceNote}</Text>\n </Box>\n ) : null}\n\n <Box marginTop={1} paddingX={1} borderStyle=\"single\" borderColor=\"gray\">\n <Text dimColor>\n <Text bold>j</Text>/<Text bold>↓</Text> next · <Text bold>k</Text>/<Text bold>↑</Text>{\" \"}\n prev · <Text bold>n</Text> next-diverge · <Text bold>N</Text>/<Text bold>p</Text>{\" \"}\n prev-diverge · <Text bold>g</Text>/<Text bold>G</Text> first/last · <Text bold>q</Text>{\" \"}\n quit\n </Text>\n </Box>\n </Box>\n );\n}\n\n// ----------------------------------------------------------------------------\n\nfunction DiffHeader({ report }: { report: DiffReport }) {\n const a = report.a;\n const b = report.b;\n\n const cacheDelta = b.stats.cacheHitRatio - a.stats.cacheHitRatio;\n const costDelta =\n a.stats.totalCostUsd > 0\n ? ((b.stats.totalCostUsd - a.stats.totalCostUsd) / a.stats.totalCostUsd) * 100\n : 0;\n\n // Prefix stability one-liner (same logic as the stdout summary).\n const aStable = a.stats.prefixHashes.length <= 1;\n const bStable = b.stats.prefixHashes.length <= 1;\n let prefixLine: string | null = null;\n if (aStable !== bStable) {\n const stableLabel = aStable ? report.a.label : report.b.label;\n const churnLabel = aStable ? report.b.label : report.a.label;\n const churnCount = aStable ? b.stats.prefixHashes.length : a.stats.prefixHashes.length;\n prefixLine = `${stableLabel} stayed byte-stable; ${churnLabel} churned ${churnCount} distinct prefixes.`;\n } else if (a.stats.prefixHashes[0] && a.stats.prefixHashes[0] === b.stats.prefixHashes[0]) {\n prefixLine = `shared prefix hash ${a.stats.prefixHashes[0].slice(0, 12)}… — cache delta attributable to log stability, not prompt change.`;\n }\n\n return (\n <Box flexDirection=\"column\" borderStyle=\"round\" borderColor=\"cyan\" paddingX={1}>\n <Box justifyContent=\"space-between\">\n <Text>\n <Text color=\"cyan\" bold>\n reasonix diff\n </Text>\n <Text dimColor> · A=</Text>\n <Text color=\"blue\">{a.label}</Text>\n <Text dimColor> vs B=</Text>\n <Text color=\"magenta\">{b.label}</Text>\n </Text>\n <Text dimColor>{report.pairs.length} turns aligned</Text>\n </Box>\n\n <Box marginTop={1} gap={3}>\n <Text>\n <Text dimColor>cache </Text>\n <Text>{(a.stats.cacheHitRatio * 100).toFixed(1)}%</Text>\n <Text dimColor> → </Text>\n <Text>{(b.stats.cacheHitRatio * 100).toFixed(1)}%</Text>\n <Text color={cacheDelta >= 0 ? \"green\" : \"red\"} bold>\n {\" \"}\n {cacheDelta >= 0 ? \"+\" : \"\"}\n {(cacheDelta * 100).toFixed(1)}pp\n </Text>\n </Text>\n <Text>\n <Text dimColor>cost </Text>\n <Text>${a.stats.totalCostUsd.toFixed(6)}</Text>\n <Text dimColor> → </Text>\n <Text>${b.stats.totalCostUsd.toFixed(6)}</Text>\n <Text color={costDelta <= 0 ? \"green\" : \"red\"} bold>\n {\" \"}\n {costDelta >= 0 ? \"+\" : \"\"}\n {costDelta.toFixed(1)}%\n </Text>\n </Text>\n <Text>\n <Text dimColor>model calls </Text>\n <Text>\n {a.stats.turns} → {b.stats.turns}\n </Text>\n </Text>\n </Box>\n\n {prefixLine ? (\n <Box marginTop={1}>\n <Text dimColor italic>\n {prefixLine}\n </Text>\n </Box>\n ) : null}\n </Box>\n );\n}\n\nfunction Pane({\n label,\n headerColor,\n records,\n}: {\n label: string;\n headerColor: \"blue\" | \"magenta\";\n records: TurnPair[\"aTools\"];\n}) {\n return (\n <Box\n flexDirection=\"column\"\n flexGrow={1}\n paddingX={1}\n borderStyle=\"single\"\n borderColor={headerColor}\n >\n <Text color={headerColor} bold>\n {label}\n </Text>\n {records.length === 0 ? (\n <Box marginTop={1}>\n <Text dimColor italic>\n (no records on this side for this turn)\n </Text>\n </Box>\n ) : (\n <Static items={records.map((rec, i) => ({ key: `${label}-${i}`, rec }))}>\n {({ key, rec }) => <RecordView key={key} rec={rec} compact />}\n </Static>\n )}\n </Box>\n );\n}\n\nfunction KindBadge({ kind }: { kind: TurnPair[\"kind\"] }) {\n if (kind === \"match\") {\n return <Text color=\"green\">✓ match</Text>;\n }\n if (kind === \"diverge\") {\n return <Text color=\"yellow\">★ diverge</Text>;\n }\n if (kind === \"only_in_a\") {\n return <Text color=\"blue\">← only in A</Text>;\n }\n return <Text color=\"magenta\">→ only in B</Text>;\n}\n\n// ----------------------------------------------------------------------------\n\nfunction paneRecords(pair: TurnPair | undefined, side: \"a\" | \"b\"): TurnPair[\"aTools\"] {\n if (!pair) return [];\n const tools = side === \"a\" ? pair.aTools : pair.bTools;\n const assistant = side === \"a\" ? pair.aAssistant : pair.bAssistant;\n const out: TurnPair[\"aTools\"] = [...tools];\n if (assistant) out.push(assistant);\n return out;\n}\n","/**\n * Shared renderer for a single TranscriptRecord. Used by ReplayApp and\n * DiffApp — both need the same visual grammar (user cyan, assistant green,\n * tool yellow, error red, cache badge colored by threshold) so transcripts\n * look consistent wherever they're displayed.\n *\n * Kept small on purpose: no streaming/branch/planState paths (those are\n * live-chat concerns and never appear in replayed transcripts).\n */\n\nimport { Box, Text } from \"ink\";\nimport React from \"react\";\nimport type { TranscriptRecord } from \"../../transcript.js\";\nimport { PlanStateBlock } from \"./PlanStateBlock.js\";\n\nexport interface RecordViewProps {\n rec: TranscriptRecord;\n /**\n * When rendering side-by-side in diff mode, shorter truncation limits\n * keep long tool results from dominating the pane. Passes through\n * untouched when undefined.\n */\n compact?: boolean;\n}\n\nexport function RecordView({ rec, compact = false }: RecordViewProps) {\n const toolArgsMax = compact ? 120 : 200;\n const toolContentMax = compact ? 200 : 400;\n\n if (rec.role === \"user\") {\n return (\n <Box marginTop={1}>\n <Text bold color=\"cyan\">\n you ›{\" \"}\n </Text>\n <Text>{rec.content}</Text>\n </Box>\n );\n }\n if (rec.role === \"assistant_final\") {\n return (\n <Box flexDirection=\"column\" marginTop={1}>\n <Box>\n <Text bold color=\"green\">\n assistant\n </Text>\n {rec.cost !== undefined ? (\n <Text dimColor>\n {\" $\"}\n {rec.cost.toFixed(6)}\n </Text>\n ) : null}\n {rec.usage ? <CacheBadge usage={rec.usage} /> : null}\n </Box>\n {rec.planState ? <PlanStateBlock planState={rec.planState} /> : null}\n {rec.content ? (\n <Text>{rec.content}</Text>\n ) : (\n <Text dimColor italic>\n (tool-call response only)\n </Text>\n )}\n </Box>\n );\n }\n if (rec.role === \"tool\") {\n return (\n <Box flexDirection=\"column\" marginTop={1}>\n <Text color=\"yellow\">\n {\"tool<\"}\n {rec.tool ?? \"?\"}\n {\">\"}\n </Text>\n {rec.args ? (\n <Text dimColor>\n {\" args: \"}\n {truncate(rec.args, toolArgsMax)}\n </Text>\n ) : null}\n <Text dimColor>\n {\" → \"}\n {truncate(rec.content, toolContentMax)}\n </Text>\n </Box>\n );\n }\n if (rec.role === \"error\") {\n return (\n <Box marginTop={1}>\n <Text color=\"red\" bold>\n error{\" \"}\n </Text>\n <Text color=\"red\">{rec.error ?? rec.content}</Text>\n </Box>\n );\n }\n if (rec.role === \"done\" || rec.role === \"assistant_delta\") {\n // Noise in replay; skip.\n return null;\n }\n return (\n <Box>\n <Text dimColor>\n [{rec.role}] {rec.content}\n </Text>\n </Box>\n );\n}\n\nfunction CacheBadge({ usage }: { usage: NonNullable<TranscriptRecord[\"usage\"]> }) {\n const hit = usage.prompt_cache_hit_tokens ?? 0;\n const miss = usage.prompt_cache_miss_tokens ?? 0;\n const total = hit + miss;\n if (total === 0) return null;\n const pct = (hit / total) * 100;\n const color = pct >= 70 ? \"green\" : pct >= 40 ? \"yellow\" : \"red\";\n return (\n <Text>\n <Text dimColor>{\" · cache \"}</Text>\n <Text color={color}>{pct.toFixed(1)}%</Text>\n </Text>\n );\n}\n\nfunction truncate(s: string, max: number): string {\n return s.length <= max ? s : `${s.slice(0, max)}… (+${s.length - max} chars)`;\n}\n","/**\n * `reasonix mcp inspect <spec>` — connect to one MCP server, list\n * everything it exposes (tools, resources, prompts), print a\n * human-readable summary. Useful as:\n * - a diagnostic tool for \"does my MCP server even work?\"\n * - a discovery tool for \"what can this server do?\"\n * - the only CLI surface for the resources+prompts methods added\n * in 0.4.5 (chat mode consumes tools but hasn't wired up the\n * other two families yet).\n */\n\nimport { McpClient } from \"../../mcp/client.js\";\nimport { inspectMcpServer } from \"../../mcp/inspect.js\";\nimport type { InspectionReport } from \"../../mcp/inspect.js\";\nimport { parseMcpSpec } from \"../../mcp/spec.js\";\nimport { SseTransport } from \"../../mcp/sse.js\";\nimport { type McpTransport, StdioTransport } from \"../../mcp/stdio.js\";\n\nexport interface McpInspectOptions {\n /** The raw --mcp spec string (e.g. `fs=npx -y @modelcontextprotocol/server-filesystem .`). */\n spec: string;\n /** Emit JSON on stdout instead of the human-readable table. */\n json?: boolean;\n}\n\nexport async function mcpInspectCommand(opts: McpInspectOptions): Promise<void> {\n const spec = parseMcpSpec(opts.spec);\n const transport: McpTransport =\n spec.transport === \"sse\"\n ? new SseTransport({ url: spec.url })\n : new StdioTransport({ command: spec.command, args: spec.args });\n const client = new McpClient({ transport });\n try {\n await client.initialize();\n const report = await inspectMcpServer(client);\n if (opts.json) {\n console.log(JSON.stringify(report, null, 2));\n } else {\n console.log(formatReport(spec.name ?? \"(anon)\", report));\n }\n } finally {\n await client.close();\n }\n}\n\nfunction formatReport(nsName: string, r: InspectionReport): string {\n const lines: string[] = [];\n lines.push(`MCP server [${nsName}]`);\n lines.push(\n ` server ${r.serverInfo.name || \"(unknown)\"}${r.serverInfo.version ? ` v${r.serverInfo.version}` : \"\"}`,\n );\n lines.push(` protocol ${r.protocolVersion}`);\n const capKeys = Object.keys(r.capabilities);\n lines.push(` caps ${capKeys.length > 0 ? capKeys.join(\", \") : \"(none advertised)\"}`);\n if (r.instructions) {\n lines.push(` notes ${r.instructions.trim().slice(0, 200)}`);\n }\n lines.push(\"\");\n lines.push(formatSection(\"Tools\", r.tools, toolLine));\n lines.push(formatSection(\"Resources\", r.resources, resourceLine));\n lines.push(formatSection(\"Prompts\", r.prompts, promptLine));\n return lines.join(\"\\n\");\n}\n\nfunction formatSection<T>(\n title: string,\n section: { supported: true; items: T[] } | { supported: false; reason: string },\n render: (item: T) => string,\n): string {\n if (!section.supported) {\n return `${title}: (not supported — ${section.reason})`;\n }\n if (section.items.length === 0) {\n return `${title}: (none)`;\n }\n const lines = [`${title} (${section.items.length}):`];\n for (const item of section.items) lines.push(` ${render(item)}`);\n return lines.join(\"\\n\");\n}\n\nfunction toolLine(t: { name: string; description?: string }): string {\n const desc = t.description ? ` — ${oneLine(t.description, 80)}` : \"\";\n return `· ${t.name}${desc}`;\n}\n\nfunction resourceLine(r: { uri: string; name: string; mimeType?: string }): string {\n const mime = r.mimeType ? ` [${r.mimeType}]` : \"\";\n return `· ${r.name}${mime} ${r.uri}`;\n}\n\nfunction promptLine(p: {\n name: string;\n description?: string;\n arguments?: Array<{ name: string; required?: boolean }>;\n}): string {\n const argPart =\n p.arguments && p.arguments.length > 0\n ? ` (${p.arguments.map((a) => (a.required ? a.name : `${a.name}?`)).join(\", \")})`\n : \"\";\n const desc = p.description ? ` — ${oneLine(p.description, 80)}` : \"\";\n return `· ${p.name}${argPart}${desc}`;\n}\n\nfunction oneLine(s: string, max: number): string {\n const flat = s.replace(/\\s+/g, \" \").trim();\n return flat.length <= max ? flat : `${flat.slice(0, max - 1)}…`;\n}\n","/**\n * Curated catalog of popular MCP servers.\n *\n * Hardcoded because (a) this list changes slowly — maybe monthly, (b)\n * fetching it over the network would make `reasonix mcp list` flaky\n * offline or behind a proxy. When it does change, update this file and\n * ship a patch release.\n *\n * Inclusion criteria:\n * - actively maintained (official Anthropic repo OR widely used)\n * - stdio-compatible (Reasonix doesn't do SSE yet)\n * - installable with one `npx -y ...` command — zero manual setup\n * - has a clear value proposition in one short line\n *\n * Not included: servers that need API keys / OAuth / complex config.\n * Those get their own docs once we have patterns for them.\n */\n\nexport interface CatalogEntry {\n /** Short name, used as the namespace prefix when suggested. */\n name: string;\n /** One-line description shown in `reasonix mcp list`. */\n summary: string;\n /** npm package id (for `npx -y <pkg>`). */\n package: string;\n /** Extra args the user must supply (e.g. a directory path). */\n userArgs?: string;\n /** Notes the user needs to know — shown dimmed. */\n note?: string;\n}\n\n// Every entry below is verified to exist on npm as of this release.\n// `fetch` and `sqlite` are deliberately *absent* — their reference\n// servers are Python-only (`pip install mcp-server-fetch`), so a Node\n// user running `npx -y @modelcontextprotocol/server-fetch` hits a 404\n// from the npm registry. We'd rather ship a smaller list that always\n// works than a longer list where two options silently 404 on the user.\nexport const MCP_CATALOG: CatalogEntry[] = [\n {\n name: \"filesystem\",\n summary: \"read/write/search files inside a sandboxed directory\",\n package: \"@modelcontextprotocol/server-filesystem\",\n userArgs: \"<dir>\",\n note: \"the directory is a hard sandbox — the server refuses access outside it\",\n },\n {\n name: \"memory\",\n summary: \"persistent key-value memory across sessions\",\n package: \"@modelcontextprotocol/server-memory\",\n },\n {\n name: \"github\",\n summary: \"read issues, PRs, code search (needs GITHUB_PERSONAL_ACCESS_TOKEN)\",\n package: \"@modelcontextprotocol/server-github\",\n note: \"set GITHUB_PERSONAL_ACCESS_TOKEN in your env before spawning\",\n },\n {\n name: \"puppeteer\",\n summary: \"browser automation — take screenshots, click, type\",\n package: \"@modelcontextprotocol/server-puppeteer\",\n note: \"downloads Chromium on first run (~200 MB)\",\n },\n {\n name: \"everything\",\n summary: \"official test server — exercises every MCP feature\",\n package: \"@modelcontextprotocol/server-everything\",\n note: \"useful for debugging your Reasonix setup\",\n },\n];\n\n/**\n * Build the `reasonix chat --mcp \"...\"` command line for a catalog entry.\n * Returns a copy-pasteable fragment starting at `--mcp`.\n */\nexport function mcpCommandFor(entry: CatalogEntry): string {\n const pkg = entry.package;\n const tail = entry.userArgs ? ` ${entry.userArgs}` : \"\";\n return `--mcp \"${entry.name}=npx -y ${pkg}${tail}\"`;\n}\n","/**\n * `reasonix mcp <subcmd>` — the MCP discovery / helper surface.\n *\n * Currently one subcommand: `list`. Prints a curated catalog of popular\n * MCP servers with their `--mcp` commands ready to copy-paste. More\n * subcommands (`init`, `try`, etc.) may land in later alphas.\n */\n\nimport { MCP_CATALOG, mcpCommandFor } from \"../../mcp/catalog.js\";\n\nexport interface McpListOptions {\n /** Emit JSON on stdout instead of the human-readable table. */\n json?: boolean;\n}\n\nexport function mcpListCommand(opts: McpListOptions): void {\n if (opts.json) {\n console.log(JSON.stringify(MCP_CATALOG, null, 2));\n return;\n }\n\n console.log(\"Popular MCP servers you can bridge into Reasonix:\");\n console.log(\"\");\n for (const entry of MCP_CATALOG) {\n console.log(` ${pad(entry.name, 12)} ${entry.summary}`);\n console.log(` ${mcpCommandFor(entry)}`);\n if (entry.note) console.log(` · ${entry.note}`);\n console.log(\"\");\n }\n console.log(\"Usage: reasonix chat <one-of-the---mcp-lines-above>\");\n console.log(\n \"Docs: https://github.com/modelcontextprotocol/servers — Anthropic's official server repo\",\n );\n console.log(\n \" https://mcp.so — community-maintained catalog\",\n );\n}\n\nfunction pad(s: string, width: number): string {\n return s.length >= width ? s : s + \" \".repeat(width - s.length);\n}\n","import { render } from \"ink\";\nimport React from \"react\";\nimport { groupRecordsByTurn, replayFromFile } from \"../../replay.js\";\nimport type { TranscriptRecord } from \"../../transcript.js\";\nimport { ReplayApp } from \"../ui/ReplayApp.js\";\n\nexport interface ReplayOptions {\n path: string;\n head?: number;\n tail?: number;\n /** Force stdout pretty-print mode (no Ink TUI). Also auto-enabled when stdout is not a TTY. */\n print?: boolean;\n}\n\n/**\n * Replay a transcript. Two modes:\n * - Interactive TUI (default when stdout is a TTY): Ink-based j/k navigation.\n * - Stdout pretty-print (--print, or when stdout is piped): one-shot text\n * dump + summary. Kept working so shell pipes, CI logs, and `less`\n * workflows still behave sensibly.\n */\nexport async function replayCommand(opts: ReplayOptions): Promise<void> {\n const wantPrint =\n opts.print || !process.stdout.isTTY || opts.head !== undefined || opts.tail !== undefined;\n if (wantPrint) {\n printReplay(opts);\n return;\n }\n\n const { parsed } = replayFromFile(opts.path);\n const pages = groupRecordsByTurn(parsed.records);\n const { waitUntilExit } = render(React.createElement(ReplayApp, { meta: parsed.meta, pages }), {\n exitOnCtrlC: true,\n patchConsole: false,\n });\n await waitUntilExit();\n}\n\n// ----------------------------------------------------------------------------\n// stdout pretty-print path (original behavior, preserved for piping / CI)\n\nfunction printReplay(opts: ReplayOptions): void {\n const { parsed, stats } = replayFromFile(opts.path);\n\n if (parsed.meta) {\n const m = parsed.meta;\n const bits: string[] = [`source=${m.source}`];\n if (m.model) bits.push(`model=${m.model}`);\n if (m.task) bits.push(`task=${m.task}`);\n if (m.mode) bits.push(`mode=${m.mode}`);\n if (m.repeat !== undefined) bits.push(`repeat=${m.repeat}`);\n bits.push(`started=${m.startedAt}`);\n console.log(`[meta] ${bits.join(\" \")}`);\n console.log(\"\");\n }\n\n const records = sliceRecords(parsed.records, opts);\n for (const rec of records) {\n renderRecord(rec);\n }\n\n console.log(\"\");\n console.log(\"── summary ─────────────────────────────────────────\");\n console.log(`model calls: ${stats.turns}`);\n console.log(`user turns: ${stats.userTurns}`);\n console.log(`tool calls: ${stats.toolCalls}`);\n console.log(`cache hit: ${(stats.cacheHitRatio * 100).toFixed(1)}%`);\n console.log(`cost: $${stats.totalCostUsd.toFixed(6)}`);\n console.log(`claude equivalent: $${stats.claudeEquivalentUsd.toFixed(6)}`);\n console.log(`savings vs claude: ${stats.savingsVsClaudePct.toFixed(1)}%`);\n console.log(`models: ${stats.models.join(\", \") || \"—\"}`);\n console.log(`prefix hashes: ${stats.prefixHashes.length} distinct`);\n if (stats.prefixHashes.length === 1) {\n console.log(` (byte-stable prefix: ${stats.prefixHashes[0]?.slice(0, 16)}…)`);\n } else if (stats.prefixHashes.length > 1) {\n console.log(\" (prefix churned — cache-hostile session)\");\n }\n if (stats.harvestedTurns > 0) {\n console.log(`harvest (Pillar 2): ${stats.harvestedTurns} turn(s) produced plan state`);\n console.log(` subgoals: ${stats.totalSubgoals}`);\n console.log(` uncertainties: ${stats.totalUncertainties}`);\n }\n}\n\nfunction sliceRecords(records: TranscriptRecord[], opts: ReplayOptions): TranscriptRecord[] {\n if (opts.head !== undefined && opts.head > 0) return records.slice(0, opts.head);\n if (opts.tail !== undefined && opts.tail > 0) return records.slice(-opts.tail);\n return records;\n}\n\nfunction renderRecord(rec: TranscriptRecord): void {\n const turn = `[t${rec.turn}]`;\n if (rec.role === \"user\") {\n console.log(`${turn} USER: ${oneLine(rec.content)}`);\n } else if (rec.role === \"assistant_final\") {\n const cost = rec.cost !== undefined ? ` $${rec.cost.toFixed(6)}` : \"\";\n const cache =\n rec.usage &&\n (rec.usage.prompt_cache_hit_tokens !== undefined ||\n rec.usage.prompt_cache_miss_tokens !== undefined)\n ? (() => {\n const hit = rec.usage!.prompt_cache_hit_tokens ?? 0;\n const miss = rec.usage!.prompt_cache_miss_tokens ?? 0;\n const total = hit + miss;\n return total > 0 ? ` cache=${((hit / total) * 100).toFixed(1)}%` : \"\";\n })()\n : \"\";\n console.log(`${turn} AGENT:${cost}${cache} ${oneLine(rec.content)}`);\n if (rec.planState) {\n const ps = rec.planState;\n if (ps.subgoals.length)\n console.log(` ‹ subgoals (${ps.subgoals.length}): ${ps.subgoals.join(\" · \")}`);\n if (ps.hypotheses.length)\n console.log(` ‹ hypotheses (${ps.hypotheses.length}): ${ps.hypotheses.join(\" · \")}`);\n if (ps.uncertainties.length)\n console.log(\n ` ‹ uncertainties(${ps.uncertainties.length}): ${ps.uncertainties.join(\" · \")}`,\n );\n if (ps.rejectedPaths.length)\n console.log(\n ` ‹ rejected (${ps.rejectedPaths.length}): ${ps.rejectedPaths.join(\" · \")}`,\n );\n }\n } else if (rec.role === \"tool\") {\n const args = rec.args ? ` args=${oneLine(rec.args, 80)}` : \"\";\n console.log(`${turn} TOOL ${rec.tool ?? \"?\"}:${args} → ${oneLine(rec.content, 120)}`);\n } else if (rec.role === \"error\") {\n console.log(`${turn} ERROR: ${rec.error ?? rec.content}`);\n } else if (rec.role === \"done\") {\n // Suppress — visually noisy, not informative in replay.\n } else {\n console.log(`${turn} ${rec.role}: ${oneLine(rec.content)}`);\n }\n}\n\nfunction oneLine(s: string, max = 200): string {\n const collapsed = s.replace(/\\s+/g, \" \").trim();\n return collapsed.length > max ? `${collapsed.slice(0, max)}…` : collapsed;\n}\n","/**\n * Ink TUI for `reasonix replay`. Read-only: no input box, no loop.\n * j/k navigation across turn-pages, cumulative stats sidebar updates\n * as you move through time.\n *\n * The navigation logic (grouping records into pages, computing cumulative\n * stats) lives in src/replay.ts as pure functions; this file is just\n * presentation + key bindings.\n */\n\nimport { Box, Static, Text, useApp, useInput } from \"ink\";\nimport React, { useMemo, useState } from \"react\";\nimport { type TurnPage, computeCumulativeStats } from \"../../replay.js\";\nimport type { TranscriptMeta } from \"../../transcript.js\";\nimport { RecordView } from \"./RecordView.js\";\nimport { StatsPanel } from \"./StatsPanel.js\";\n\nexport interface ReplayAppProps {\n meta: TranscriptMeta | null;\n pages: TurnPage[];\n}\n\nexport function ReplayApp({ meta, pages }: ReplayAppProps) {\n const { exit } = useApp();\n const maxIdx = Math.max(0, pages.length - 1);\n // Start at the last page — more useful than \"start from the beginning\"\n // in practice: users mostly want to see the summary + last turn first.\n const [idx, setIdx] = useState(maxIdx);\n\n useInput((input, key) => {\n if (input === \"q\" || (key.ctrl && input === \"c\")) {\n exit();\n return;\n }\n if (input === \"j\" || key.downArrow || input === \" \" || key.return) {\n setIdx((i) => Math.min(maxIdx, i + 1));\n } else if (input === \"k\" || key.upArrow) {\n setIdx((i) => Math.max(0, i - 1));\n } else if (input === \"g\") {\n setIdx(0);\n } else if (input === \"G\") {\n setIdx(maxIdx);\n } else if (input === \"h\" || key.leftArrow) {\n setIdx(0);\n } else if (input === \"l\" || key.rightArrow) {\n setIdx(maxIdx);\n }\n });\n\n const cumStats = useMemo(() => computeCumulativeStats(pages, idx), [pages, idx]);\n\n const summary = {\n turns: cumStats.turns,\n totalCostUsd: cumStats.totalCostUsd,\n totalInputCostUsd: cumStats.totalInputCostUsd,\n totalOutputCostUsd: cumStats.totalOutputCostUsd,\n claudeEquivalentUsd: cumStats.claudeEquivalentUsd,\n savingsVsClaudePct: cumStats.savingsVsClaudePct,\n cacheHitRatio: cumStats.cacheHitRatio,\n // Replay is read-only — no live last-turn prompt tokens to show.\n lastPromptTokens: 0,\n };\n\n const prefixHash =\n cumStats.prefixHashes.length === 1\n ? cumStats.prefixHashes[0]!.slice(0, 16)\n : cumStats.prefixHashes.length === 0\n ? \"(untracked)\"\n : `(churned ×${cumStats.prefixHashes.length})`;\n\n const currentPage = pages[idx];\n const progressLabel =\n pages.length === 0 ? \"empty transcript\" : `turn ${idx + 1} / ${pages.length}`;\n\n return (\n <Box flexDirection=\"column\">\n <StatsPanel\n summary={summary}\n model={cumStats.models[0] ?? meta?.model ?? \"?\"}\n prefixHash={prefixHash}\n />\n\n <Box flexDirection=\"column\" marginTop={1} paddingX={1}>\n <Box justifyContent=\"space-between\">\n <Text color=\"cyan\" bold>\n {progressLabel}\n </Text>\n {meta ? (\n <Text dimColor>\n {meta.source}\n {meta.task ? ` · ${meta.task}` : \"\"}\n {meta.mode ? ` · ${meta.mode}` : \"\"}\n </Text>\n ) : null}\n </Box>\n\n {currentPage ? (\n <Static items={currentPage.records.map((rec, i) => ({ key: `${idx}-${i}`, rec }))}>\n {({ key, rec }) => <RecordView key={key} rec={rec} />}\n </Static>\n ) : (\n <Text dimColor italic>\n no records\n </Text>\n )}\n </Box>\n\n <Box marginTop={1} paddingX={1} borderStyle=\"single\" borderColor=\"gray\">\n <Text dimColor>\n <Text bold>j</Text>/<Text bold>↓</Text>/<Text bold>space</Text> next · <Text bold>k</Text>\n /<Text bold>↑</Text> prev · <Text bold>g</Text> first · <Text bold>G</Text> last ·{\" \"}\n <Text bold>q</Text> quit\n </Text>\n </Box>\n </Box>\n );\n}\n","import type { WriteStream } from \"node:fs\";\nimport { stdin, stdout } from \"node:process\";\nimport { createInterface } from \"node:readline/promises\";\nimport { defaultConfigPath, isPlausibleKey, loadApiKey, saveApiKey } from \"../../config.js\";\nimport { loadDotenv } from \"../../env.js\";\nimport { CacheFirstLoop, DeepSeekClient, ImmutablePrefix } from \"../../index.js\";\nimport { McpClient } from \"../../mcp/client.js\";\nimport { bridgeMcpTools } from \"../../mcp/registry.js\";\nimport { parseMcpSpec } from \"../../mcp/spec.js\";\nimport { SseTransport } from \"../../mcp/sse.js\";\nimport { type McpTransport, StdioTransport } from \"../../mcp/stdio.js\";\nimport { ToolRegistry } from \"../../tools.js\";\nimport { openTranscriptFile, recordFromLoopEvent, writeRecord } from \"../../transcript.js\";\nimport { appendUsage } from \"../../usage.js\";\n\nexport interface RunOptions {\n task: string;\n model: string;\n system: string;\n /** Opt into Pillar 2 plan-state harvesting. Adds a cheap V3 call per turn. */\n harvest?: boolean;\n /** Self-consistency budget. > 1 runs N parallel samples and picks the best. */\n branch?: number;\n /** JSONL transcript path — lets `reasonix replay` / `diff` audit this run. */\n transcript?: string;\n /** Zero or more MCP server specs. Each: `\"name=cmd args...\"` or `\"cmd args...\"`. */\n mcp?: string[];\n /** Global prefix — only honored when a single anonymous server is given. */\n mcpPrefix?: string;\n}\n\nasync function ensureApiKey(): Promise<string> {\n const existing = loadApiKey();\n if (existing) return existing;\n\n if (!stdin.isTTY) {\n process.stderr.write(\n \"DEEPSEEK_API_KEY is not set and stdin is not a TTY (cannot prompt).\\n\" +\n \"Set the env var, or run `reasonix chat` once interactively to save a key.\\n\",\n );\n process.exit(1);\n }\n\n process.stdout.write(\n \"DeepSeek API key not configured.\\nGet one at https://platform.deepseek.com/api_keys\\n\",\n );\n const rl = createInterface({ input: stdin, output: stdout });\n try {\n while (true) {\n const answer = (await rl.question(\"API key › \")).trim();\n if (!answer) continue;\n if (!isPlausibleKey(answer)) {\n process.stdout.write(\"Invalid format. Keys start with 'sk-' and are 30+ chars.\\n\");\n continue;\n }\n saveApiKey(answer);\n process.stdout.write(`Saved to ${defaultConfigPath()}\\n\\n`);\n return answer;\n }\n } finally {\n rl.close();\n }\n}\n\nexport async function runCommand(opts: RunOptions): Promise<void> {\n loadDotenv();\n const apiKey = await ensureApiKey();\n process.env.DEEPSEEK_API_KEY = apiKey;\n\n // Optional MCP setup — mirrors chat's flow. Must happen before loop\n // construction so the tools make it into the prefix.\n const requestedSpecs = opts.mcp ?? [];\n const clients: McpClient[] = [];\n let tools: ToolRegistry | undefined;\n let successCount = 0;\n if (requestedSpecs.length > 0) {\n tools = new ToolRegistry();\n for (const raw of requestedSpecs) {\n try {\n const spec = parseMcpSpec(raw);\n const prefix = spec.name\n ? `${spec.name}_`\n : requestedSpecs.length === 1 && opts.mcpPrefix\n ? opts.mcpPrefix\n : \"\";\n const transport: McpTransport =\n spec.transport === \"sse\"\n ? new SseTransport({ url: spec.url })\n : new StdioTransport({ command: spec.command, args: spec.args });\n const mcp = new McpClient({ transport });\n await mcp.initialize();\n const bridge = await bridgeMcpTools(mcp, { registry: tools, namePrefix: prefix });\n const source =\n spec.transport === \"sse\" ? spec.url : `${spec.command} ${spec.args.join(\" \")}`;\n process.stderr.write(\n `▸ MCP[${spec.name ?? \"anon\"}]: ${bridge.registeredNames.length} tool(s) from ${source}\\n`,\n );\n clients.push(mcp);\n successCount++;\n } catch (err) {\n // Non-fatal — skip and continue, same as `reasonix chat`. A\n // one-shot `run` invocation with a broken MCP server otherwise\n // fails the whole run over a side-concern tool the task might\n // not even touch.\n process.stderr.write(\n `▸ MCP setup SKIPPED for \"${raw}\": ${(err as Error).message}\\n → run \\`reasonix setup\\` to remove broken entries from your saved config.\\n`,\n );\n }\n }\n if (successCount === 0) tools = undefined;\n }\n\n const client = new DeepSeekClient();\n const prefix = new ImmutablePrefix({\n system: opts.system,\n toolSpecs: tools?.specs(),\n });\n const loop = new CacheFirstLoop({\n client,\n prefix,\n tools,\n model: opts.model,\n harvest: opts.harvest,\n branch: opts.branch,\n });\n const prefixHash = prefix.fingerprint;\n\n let transcriptStream: WriteStream | null = null;\n if (opts.transcript) {\n transcriptStream = openTranscriptFile(opts.transcript, {\n version: 1,\n source: \"reasonix run\",\n model: opts.model,\n startedAt: new Date().toISOString(),\n });\n // Also persist the user turn itself (the loop's event stream starts with\n // assistant output, not the prompt we're about to send).\n writeRecord(transcriptStream, {\n ts: new Date().toISOString(),\n turn: 1,\n role: \"user\",\n content: opts.task,\n });\n }\n\n try {\n for await (const ev of loop.step(opts.task)) {\n if (ev.role === \"assistant_delta\" && ev.content) process.stdout.write(ev.content);\n if (ev.role === \"tool\") process.stdout.write(`\\n[tool ${ev.toolName}] ${ev.content}\\n`);\n if (ev.role === \"error\") process.stderr.write(`\\n[error] ${ev.error}\\n`);\n if (ev.role === \"done\") process.stdout.write(\"\\n\");\n if (ev.role === \"assistant_final\" && ev.stats?.usage) {\n // `reasonix run` is often used in CI / scripting — we want\n // those turns to show up in `reasonix stats` too so the\n // dashboard reflects all DeepSeek spend, not just TUI sessions.\n appendUsage({ session: null, model: ev.stats.model, usage: ev.stats.usage });\n }\n // Persist every non-streaming event — deltas would flood the file and\n // aren't useful for replay (replay renders final content, not keystrokes).\n if (transcriptStream && ev.role !== \"assistant_delta\") {\n writeRecord(transcriptStream, recordFromLoopEvent(ev, { model: opts.model, prefixHash }));\n }\n }\n } finally {\n transcriptStream?.end();\n }\n\n const s = loop.stats.summary();\n process.stdout.write(\n `\\n— turns:${s.turns} cache:${(s.cacheHitRatio * 100).toFixed(1)}% ` +\n `cost:$${s.totalCostUsd.toFixed(6)} save-vs-claude:${s.savingsVsClaudePct.toFixed(1)}%\\n`,\n );\n if (opts.transcript) {\n process.stdout.write(`\\ntranscript: ${opts.transcript}\\n`);\n process.stdout.write(` → npx reasonix replay ${opts.transcript}\\n`);\n }\n\n for (const c of clients) await c.close();\n}\n","/**\n * `reasonix sessions` — CLI equivalent of the `/sessions` slash command.\n *\n * Two modes:\n * - `reasonix sessions` list every session under ~/.reasonix/sessions/\n * - `reasonix sessions <name>` dump one session's messages in readable form\n *\n * Neither needs an API key — pure filesystem reads of JSONL files written\n * by previous chat runs.\n */\n\nimport { listSessions, loadSessionMessages, sessionPath } from \"../../index.js\";\nimport type { ChatMessage } from \"../../index.js\";\n\nexport interface SessionsOptions {\n /** When present, inspect that session instead of listing. */\n name?: string;\n /** Include assistant tool-call metadata in the inspect output. */\n verbose?: boolean;\n}\n\nexport function sessionsCommand(opts: SessionsOptions): void {\n if (opts.name) {\n inspectSession(opts.name, !!opts.verbose);\n } else {\n listAll();\n }\n}\n\nfunction listAll(): void {\n const items = listSessions();\n if (items.length === 0) {\n console.log(\n \"no saved sessions yet — run `reasonix chat` (sessions are auto-saved unless --no-session).\",\n );\n return;\n }\n console.log(\"Saved sessions (~/.reasonix/sessions/):\");\n console.log(\"\");\n console.log(` ${\"name\".padEnd(22)} ${\"msgs\".padStart(6)} ${\"size\".padStart(8)} modified`);\n console.log(` ${\"─\".repeat(60)}`);\n for (const s of items) {\n const sizeKb = `${(s.size / 1024).toFixed(1)} KB`;\n const when = s.mtime.toISOString().replace(\"T\", \" \").slice(0, 16);\n console.log(\n ` ${s.name.padEnd(22)} ${String(s.messageCount).padStart(6)} ${sizeKb.padStart(8)} ${when}`,\n );\n }\n console.log(\"\");\n console.log(\"Inspect: reasonix sessions <name>\");\n console.log(\"Resume: reasonix chat --session <name>\");\n}\n\nfunction inspectSession(name: string, verbose: boolean): void {\n const path = sessionPath(name);\n const messages = loadSessionMessages(name);\n if (messages.length === 0) {\n console.error(`no session named \"${name}\" (or it's empty).`);\n console.error(`looked at: ${path}`);\n process.exit(1);\n }\n\n console.log(`[session] ${name} ${messages.length} messages ${path}`);\n console.log(\"\");\n\n let turnIndex = 0;\n for (const msg of messages) {\n renderMessage(msg, turnIndex, verbose);\n // Roughly bump \"turn\" after each user message so the reader can follow\n // the conversation shape without the transcript's richer turn numbering.\n if (msg.role === \"user\") turnIndex++;\n }\n}\n\nfunction renderMessage(msg: ChatMessage, turnIdx: number, verbose: boolean): void {\n const turn = turnIdx > 0 ? `[t${turnIdx}]` : \"[start]\";\n const content = typeof msg.content === \"string\" ? msg.content : \"\";\n const flat = oneLine(content);\n\n if (msg.role === \"user\") {\n console.log(`${turn} USER: ${flat}`);\n } else if (msg.role === \"assistant\") {\n console.log(`${turn} AGENT: ${flat || \"(tool call only)\"}`);\n if (verbose && msg.tool_calls?.length) {\n for (const tc of msg.tool_calls) {\n console.log(\n ` → call ${tc.function?.name} ${truncate(tc.function?.arguments ?? \"\", 80)}`,\n );\n }\n }\n } else if (msg.role === \"tool\") {\n console.log(`${turn} TOOL ${msg.name ?? \"?\"}: ${truncate(flat, 160)}`);\n } else if (msg.role === \"system\") {\n if (verbose) console.log(`${turn} SYSTEM: ${truncate(flat, 160)}`);\n // otherwise suppress — session's system prompt is usually session-wide\n // boilerplate.\n }\n}\n\nfunction oneLine(s: string, max = 200): string {\n const collapsed = s.replace(/\\s+/g, \" \").trim();\n return collapsed.length > max ? `${collapsed.slice(0, max)}…` : collapsed;\n}\n\nfunction truncate(s: string, max: number): string {\n return s.length <= max ? s : `${s.slice(0, max)}…`;\n}\n","/**\n * `reasonix setup` — re-mount the first-run wizard on demand so users\n * can reconfigure (add/remove MCP servers, switch preset) without\n * editing JSON by hand.\n *\n * Invoked both explicitly (`reasonix setup`) and implicitly (the no-args\n * entry point when `setupCompleted` is false).\n */\n\nimport { render } from \"ink\";\nimport React from \"react\";\nimport { loadApiKey, readConfig } from \"../../config.js\";\nimport { loadDotenv } from \"../../env.js\";\nimport { Wizard } from \"../ui/Wizard.js\";\n\nexport interface SetupOptions {\n /**\n * When true, bypass the API-key step even if no key is saved — useful\n * from test harnesses. Normal CLI use always pushes through the key\n * step when missing.\n */\n skipKeyStep?: boolean;\n}\n\nexport async function setupCommand(_opts: SetupOptions = {}): Promise<void> {\n loadDotenv();\n const existingKey = loadApiKey();\n const existing = readConfig();\n\n const { waitUntilExit, unmount } = render(\n <Wizard\n existingApiKey={existingKey}\n initial={{ preset: existing.preset, mcp: existing.mcp }}\n onComplete={() => {\n // Ink handles its own enter-to-exit inside the \"saved\" step; we\n // just wait for the app to exit naturally.\n }}\n onCancel={() => {\n unmount();\n }}\n />,\n { exitOnCtrlC: true, patchConsole: false },\n );\n await waitUntilExit();\n}\n","/**\n * First-run / re-configure wizard.\n *\n * Walks a new user through: API key → preset pick → MCP server pick →\n * per-server args → save. Saved output lives in `~/.reasonix/config.json`\n * so the next `reasonix chat` starts with everything already wired.\n *\n * The wizard is the antidote to \"too many CLI flags\" — a new user should\n * never have to read `--help` to get MCP + a sensible model combo\n * working. Everything a user could set via `--mcp`, `--harvest`,\n * `--branch`, etc. can be picked here in a few arrow-key presses.\n */\n\nimport { Box, Text, useApp, useInput } from \"ink\";\nimport TextInput from \"ink-text-input\";\n// biome-ignore lint/style/useImportType: JSX (jsx: \"react\") needs React as a value at runtime\nimport React, { useState } from \"react\";\nimport {\n type PresetName,\n type ReasonixConfig,\n defaultConfigPath,\n isPlausibleKey,\n readConfig,\n redactKey,\n writeConfig,\n} from \"../../config.js\";\nimport { type CatalogEntry, MCP_CATALOG } from \"../../mcp/catalog.js\";\nimport { MultiSelect, type SelectItem, SingleSelect } from \"./Select.js\";\nimport { PRESET_DESCRIPTIONS } from \"./presets.js\";\n\nexport interface WizardProps {\n /** Called once the config has been saved. */\n onComplete: (cfg: ReasonixConfig) => void;\n /** Called if the user presses Esc to abort. */\n onCancel?: () => void;\n /** Skip the API-key step if a key already exists (env or config). */\n existingApiKey?: string;\n /** Pre-fill selections when re-running (reconfigure flow). */\n initial?: {\n preset?: PresetName;\n mcp?: string[];\n };\n}\n\ntype Step = \"apiKey\" | \"preset\" | \"mcp\" | \"mcpArgs\" | \"review\" | \"saved\";\n\ninterface WizardData {\n apiKey: string;\n preset: PresetName;\n selectedCatalog: string[]; // entries from MCP_CATALOG by `name`\n /** Captured user inputs per catalog entry that has `userArgs` (e.g. fs dir). */\n catalogArgs: Record<string, string>;\n}\n\nconst CATALOG_BY_NAME = new Map(MCP_CATALOG.map((e) => [e.name, e]));\n\nexport function Wizard({ onComplete, onCancel, existingApiKey, initial }: WizardProps) {\n const { exit } = useApp();\n const [step, setStep] = useState<Step>(existingApiKey ? \"preset\" : \"apiKey\");\n const [data, setData] = useState<WizardData>({\n apiKey: existingApiKey ?? \"\",\n preset: initial?.preset ?? \"fast\",\n selectedCatalog: deriveInitialCatalog(initial?.mcp ?? []),\n catalogArgs: {},\n });\n const [error, setError] = useState<string | null>(null);\n\n // Global Esc → cancel. Disabled once we've started saving to avoid\n // ejecting out of a half-written state.\n useInput((_input, key) => {\n if (key.escape && step !== \"saved\" && onCancel) onCancel();\n });\n\n if (step === \"apiKey\") {\n return (\n <ApiKeyStep\n onSubmit={(key) => {\n setData((d) => ({ ...d, apiKey: key }));\n setError(null);\n setStep(\"preset\");\n }}\n error={error}\n onError={setError}\n />\n );\n }\n\n if (step === \"preset\") {\n return (\n <StepFrame title=\"Pick a preset\" step={1} total={3}>\n <SingleSelect<PresetName>\n items={presetItems()}\n initialValue={data.preset}\n onSubmit={(preset) => {\n setData((d) => ({ ...d, preset }));\n setStep(\"mcp\");\n }}\n />\n <Box marginTop={1}>\n <Text dimColor>↑/↓ move · enter confirm · esc cancel</Text>\n </Box>\n </StepFrame>\n );\n }\n\n if (step === \"mcp\") {\n return (\n <StepFrame title=\"Which MCP servers should Reasonix wire up for you?\" step={2} total={3}>\n <MultiSelect\n items={mcpItems()}\n initialSelected={data.selectedCatalog}\n onSubmit={(selected) => {\n setData((d) => ({ ...d, selectedCatalog: selected }));\n // Only advance to the args step if any selected entry needs args.\n const needsArgs = selected.some((name) => CATALOG_BY_NAME.get(name)?.userArgs);\n setStep(needsArgs ? \"mcpArgs\" : \"review\");\n }}\n footer=\"↑/↓ move · space toggle · enter confirm · esc cancel · leave empty to skip\"\n />\n </StepFrame>\n );\n }\n\n if (step === \"mcpArgs\") {\n const pending = data.selectedCatalog.filter((name) => {\n const entry = CATALOG_BY_NAME.get(name);\n return entry?.userArgs && !data.catalogArgs[name];\n });\n if (pending.length === 0) {\n setStep(\"review\");\n return null;\n }\n const currentName = pending[0]!;\n const entry = CATALOG_BY_NAME.get(currentName)!;\n return (\n <McpArgsStep\n entry={entry}\n error={error}\n onSubmit={(value) => {\n setData((d) => ({\n ...d,\n catalogArgs: { ...d.catalogArgs, [currentName]: value },\n }));\n setError(null);\n }}\n onError={setError}\n />\n );\n }\n\n if (step === \"review\") {\n const specs = data.selectedCatalog.map((name) => buildSpec(name, data.catalogArgs));\n return (\n <StepFrame title=\"Ready to save\" step={3} total={3}>\n <Box flexDirection=\"column\">\n <SummaryLine label=\"API key\" value={redactKey(data.apiKey)} />\n <SummaryLine label=\"Preset\" value={data.preset} />\n <SummaryLine\n label=\"MCP\"\n value={specs.length === 0 ? \"(none)\" : `${specs.length} server(s)`}\n />\n {specs.map((spec, i) => (\n // biome-ignore lint/suspicious/noArrayIndexKey: review-only render, order fixed\n <Box key={i} paddingLeft={14}>\n <Text dimColor>· {spec}</Text>\n </Box>\n ))}\n <Box marginTop={1}>\n <Text>Saves to {defaultConfigPath()}</Text>\n </Box>\n {error ? (\n <Box marginTop={1}>\n <Text color=\"red\">{error}</Text>\n </Box>\n ) : null}\n <Box marginTop={1}>\n <Text dimColor>enter save · esc cancel</Text>\n </Box>\n </Box>\n <ReviewConfirm\n onConfirm={() => {\n try {\n const specsNow = data.selectedCatalog.map((name) =>\n buildSpec(name, data.catalogArgs),\n );\n const prev = readConfig();\n const next: ReasonixConfig = {\n ...prev,\n apiKey: data.apiKey,\n preset: data.preset,\n mcp: specsNow,\n setupCompleted: true,\n };\n writeConfig(next);\n setStep(\"saved\");\n onComplete(next);\n } catch (e) {\n setError(`Could not save config: ${(e as Error).message}`);\n }\n }}\n />\n </StepFrame>\n );\n }\n\n // saved\n return (\n <Box flexDirection=\"column\" borderStyle=\"round\" borderColor=\"green\" paddingX={1}>\n <Text bold color=\"green\">\n ▸ Saved.\n </Text>\n <Box marginTop={1}>\n <Text>Run `reasonix` any time to start chatting — your settings are remembered.</Text>\n </Box>\n <Box marginTop={1}>\n <Text dimColor>Press enter to exit.</Text>\n </Box>\n <ExitOnEnter onExit={exit} />\n </Box>\n );\n}\n\n// ---------- step components ----------\n\nfunction ApiKeyStep({\n onSubmit,\n error,\n onError,\n}: {\n onSubmit: (key: string) => void;\n error: string | null;\n onError: (e: string | null) => void;\n}) {\n const [value, setValue] = useState(\"\");\n return (\n <Box flexDirection=\"column\" borderStyle=\"round\" borderColor=\"cyan\" paddingX={1}>\n <Text bold color=\"cyan\">\n Welcome to Reasonix.\n </Text>\n <Box marginTop={1}>\n <Text>Paste your DeepSeek API key to get started.</Text>\n </Box>\n <Text dimColor>Get one (free credit on signup): https://platform.deepseek.com/api_keys</Text>\n <Text dimColor>Saved locally to {defaultConfigPath()}</Text>\n <Box marginTop={1}>\n <Text bold color=\"cyan\">\n {\"key › \"}\n </Text>\n <TextInput\n value={value}\n onChange={setValue}\n onSubmit={(raw) => {\n const trimmed = raw.trim();\n if (!isPlausibleKey(trimmed)) {\n onError(\"Doesn't look like a DeepSeek key. They start with 'sk-' and are 30+ chars.\");\n setValue(\"\");\n return;\n }\n onSubmit(trimmed);\n }}\n mask=\"•\"\n placeholder=\"sk-...\"\n />\n </Box>\n {error ? (\n <Box marginTop={1}>\n <Text color=\"red\">{error}</Text>\n </Box>\n ) : value ? (\n <Box marginTop={1}>\n <Text dimColor>preview: {redactKey(value)}</Text>\n </Box>\n ) : null}\n </Box>\n );\n}\n\nfunction McpArgsStep({\n entry,\n error,\n onSubmit,\n onError,\n}: {\n entry: CatalogEntry;\n error: string | null;\n onSubmit: (value: string) => void;\n onError: (e: string | null) => void;\n}) {\n const [value, setValue] = useState(\"\");\n return (\n <StepFrame title={`Configure ${entry.name}`} step={2} total={3}>\n <Box flexDirection=\"column\">\n <Text>{entry.summary}</Text>\n {entry.note ? (\n <Box marginTop={1}>\n <Text dimColor>{entry.note}</Text>\n </Box>\n ) : null}\n <Box marginTop={1}>\n <Text>Required parameter: </Text>\n <Text bold>{entry.userArgs}</Text>\n </Box>\n <Box marginTop={1}>\n <Text bold color=\"cyan\">\n {entry.userArgs}\n {\" › \"}\n </Text>\n <TextInput\n value={value}\n onChange={setValue}\n onSubmit={(raw) => {\n const trimmed = raw.trim();\n if (!trimmed) {\n onError(`${entry.name} needs a value — got an empty string.`);\n return;\n }\n onSubmit(trimmed);\n setValue(\"\");\n }}\n placeholder={placeholderFor(entry)}\n />\n </Box>\n {error ? (\n <Box marginTop={1}>\n <Text color=\"red\">{error}</Text>\n </Box>\n ) : null}\n </Box>\n </StepFrame>\n );\n}\n\nfunction ReviewConfirm({ onConfirm }: { onConfirm: () => void }) {\n useInput((_i, key) => {\n if (key.return) onConfirm();\n });\n return null;\n}\n\nfunction ExitOnEnter({ onExit }: { onExit: () => void }) {\n useInput((_i, key) => {\n if (key.return) onExit();\n });\n return null;\n}\n\n// ---------- small bits ----------\n\nfunction StepFrame({\n title,\n step,\n total,\n children,\n}: {\n title: string;\n step: number;\n total: number;\n children: React.ReactNode;\n}) {\n return (\n <Box flexDirection=\"column\" borderStyle=\"round\" borderColor=\"cyan\" paddingX={1}>\n <Box>\n <Text dimColor>\n Step {step}/{total} ·{\" \"}\n </Text>\n <Text bold color=\"cyan\">\n {title}\n </Text>\n </Box>\n <Box marginTop={1} flexDirection=\"column\">\n {children}\n </Box>\n </Box>\n );\n}\n\nfunction SummaryLine({ label, value }: { label: string; value: string }) {\n return (\n <Box>\n <Text>{label.padEnd(12)}</Text>\n <Text bold>{value}</Text>\n </Box>\n );\n}\n\n// ---------- data helpers ----------\n\nfunction presetItems(): SelectItem<PresetName>[] {\n return ([\"fast\", \"smart\", \"max\"] as PresetName[]).map((name) => ({\n value: name,\n label: `${name} — ${PRESET_DESCRIPTIONS[name].headline}`,\n hint: PRESET_DESCRIPTIONS[name].cost,\n }));\n}\n\nfunction mcpItems(): SelectItem<string>[] {\n return MCP_CATALOG.map((entry) => {\n const hintParts: string[] = [entry.summary];\n if (entry.userArgs) hintParts.push(`(you'll provide ${entry.userArgs})`);\n if (entry.note) hintParts.push(entry.note);\n return {\n value: entry.name,\n label: entry.name,\n hint: hintParts.join(\" · \"),\n };\n });\n}\n\nfunction placeholderFor(entry: CatalogEntry): string {\n if (entry.name === \"filesystem\") return \"e.g. /tmp/reasonix-sandbox\";\n if (entry.name === \"sqlite\") return \"e.g. ./notes.sqlite\";\n return entry.userArgs ?? \"\";\n}\n\nfunction deriveInitialCatalog(existingSpecs: string[]): string[] {\n // Best-effort recovery: if the user previously picked catalog entries,\n // the spec strings look like `name=npx -y <pkg> ...`. Match by package\n // so reconfigure pre-checks the same boxes.\n const packageToName = new Map(MCP_CATALOG.map((e) => [e.package, e.name]));\n const out: string[] = [];\n for (const spec of existingSpecs) {\n for (const [pkg, name] of packageToName) {\n if (spec.includes(pkg)) {\n out.push(name);\n break;\n }\n }\n }\n return out;\n}\n\n/**\n * Build the `--mcp` spec string for a catalog entry. Same format\n * `mcpCommandFor` produces for `reasonix mcp list`, minus the leading\n * `--mcp \"...\"` wrapper — we store the inner spec directly.\n */\nexport function buildSpec(name: string, argsByName: Record<string, string>): string {\n const entry = CATALOG_BY_NAME.get(name);\n if (!entry) return name; // shouldn't happen; fall back gracefully\n const userArg = entry.userArgs ? argsByName[name] : undefined;\n const tail = userArg ? ` ${quoteIfNeeded(userArg)}` : \"\";\n return `${entry.name}=npx -y ${entry.package}${tail}`;\n}\n\nfunction quoteIfNeeded(s: string): string {\n return /\\s|\"/.test(s) ? `\"${s.replace(/\"/g, '\\\\\"')}\"` : s;\n}\n","/**\n * One place that defines what each preset means. Both `slash.ts`\n * (in-chat `/preset`) and the wizard (first-run setup) read from here.\n *\n * Presets are the single vocabulary we teach new users: they don't need\n * to know model IDs, Pillar 2, branch budgets, or cost tradeoffs\n * independently — they pick \"fast / smart / max\" and we translate.\n */\n\nimport type { PresetName } from \"../../config.js\";\n\nexport interface PresetSettings {\n model: string;\n harvest: boolean;\n /** Branch budget. `1` means branching off. */\n branch: number;\n}\n\nexport const PRESETS: Record<PresetName, PresetSettings> = {\n fast: { model: \"deepseek-chat\", harvest: false, branch: 1 },\n smart: { model: \"deepseek-reasoner\", harvest: true, branch: 1 },\n max: { model: \"deepseek-reasoner\", harvest: true, branch: 3 },\n};\n\nexport const PRESET_DESCRIPTIONS: Record<PresetName, { headline: string; cost: string }> = {\n fast: {\n headline: \"deepseek-chat, no reasoning harvest, no branching\",\n cost: \"~1¢ per 100 turns · default\",\n },\n smart: {\n headline: \"deepseek-reasoner + Pillar 2 harvest\",\n cost: \"~10× cost vs fast · slower · better on multi-step tasks\",\n },\n max: {\n headline: \"reasoner + harvest + self-consistency (3 branches)\",\n cost: \"~30× cost vs fast · slowest · for hard single-shots\",\n },\n};\n\nexport function resolvePreset(name: PresetName | undefined): PresetSettings {\n return PRESETS[name ?? \"fast\"];\n}\n","/**\n * `reasonix update` — self-upgrade command.\n *\n * Talks to the npm registry, compares against the running version,\n * and either runs `npm install -g reasonix@latest` (global install)\n * or advises how to refresh the npx cache (ephemeral install).\n *\n * The decision logic is factored into `planUpdate()` so it can be\n * unit-tested without spawning child processes or hitting the\n * network. `updateCommand()` is the thin CLI wrapper that calls\n * `getLatestVersion`, passes the result into `planUpdate`, and\n * executes any suggested command.\n */\n\nimport { spawn } from \"node:child_process\";\nimport { VERSION, compareVersions, getLatestVersion, isNpxInstall } from \"../../version.js\";\n\nexport type UpdateAction = \"up-to-date\" | \"newer-local\" | \"npx-hint\" | \"run-npm-install\";\n\nexport interface UpdatePlan {\n action: UpdateAction;\n /** Human-readable summary; the CLI prints this verbatim. */\n message: string;\n /**\n * Argv for the install command when `action === \"run-npm-install\"`.\n * Absent otherwise. Kept as array so the CLI can pass it straight\n * to `spawn` without re-parsing shell syntax.\n */\n command?: string[];\n}\n\nexport interface PlanUpdateInput {\n current: string;\n latest: string;\n /** Overrides `isNpxInstall()` (tests). */\n npx?: boolean;\n}\n\n/**\n * Pure decision function: given current + latest + install kind,\n * decide what the CLI should do. No I/O.\n *\n * newer-local — current > latest (dev build, local publish)\n * up-to-date — current === latest\n * npx-hint — current < latest AND running under npx\n * run-npm-install — current < latest AND running as a real install\n */\nexport function planUpdate(input: PlanUpdateInput): UpdatePlan {\n const diff = compareVersions(input.current, input.latest);\n if (diff > 0) {\n return {\n action: \"newer-local\",\n message: `current (${input.current}) is newer than the published ${input.latest} — nothing to do.`,\n };\n }\n if (diff === 0) {\n return { action: \"up-to-date\", message: `reasonix ${input.current} is up to date.` };\n }\n if (input.npx) {\n return {\n action: \"npx-hint\",\n message: [\n `reasonix ${input.latest} is available.`,\n \"you're running via npx — the next `npx reasonix ...` launch will auto-fetch\",\n \"the latest (npx caches packages for a short window). to force a refresh\",\n \"sooner, clear the cache: `npm cache clean --force`.\",\n ].join(\"\\n\"),\n };\n }\n return {\n action: \"run-npm-install\",\n message: `upgrading reasonix ${input.current} → ${input.latest}`,\n command: [\"npm\", \"install\", \"-g\", \"reasonix@latest\"],\n };\n}\n\nexport interface UpdateCommandOptions {\n /** Skip spawning npm; print the decision only. */\n dryRun?: boolean;\n /** Test seam: override the registry lookup. Returns null = offline. */\n fetchLatest?: () => Promise<string | null>;\n /** Test seam: override the npx detector. */\n isNpx?: () => boolean;\n /** Test seam: override the spawner. Must return exit code. */\n spawnInstall?: (argv: string[]) => Promise<number>;\n /** Test seam: stdout writer. */\n write?: (msg: string) => void;\n /** Test seam: process exit — tests don't want to tear down vitest. */\n exit?: (code: number) => void;\n}\n\nfunction defaultSpawn(argv: string[]): Promise<number> {\n return new Promise((resolve, reject) => {\n // `shell: true` on Windows is what lets `npm` resolve to `npm.cmd`\n // without routing through our `prepareSpawn` helper. The args here\n // are literal strings under our control — no user input flows in,\n // so injection is not a concern. Avoiding `prepareSpawn` keeps\n // this command free of a dep on the shell tools module.\n const child = spawn(argv[0]!, argv.slice(1), {\n stdio: \"inherit\",\n shell: process.platform === \"win32\",\n });\n child.once(\"error\", reject);\n child.once(\"exit\", (code) => resolve(code ?? 1));\n });\n}\n\n/**\n * Run the update command. Prints a banner, resolves the latest\n * version, prints the plan, and (unless `--dry-run`) executes the\n * install when applicable.\n */\nexport async function updateCommand(opts: UpdateCommandOptions = {}): Promise<void> {\n const write = opts.write ?? ((m: string) => process.stdout.write(m));\n const exit = opts.exit ?? ((c: number) => process.exit(c));\n const fetchLatest = opts.fetchLatest ?? (() => getLatestVersion({ force: true }));\n const isNpx = opts.isNpx ?? isNpxInstall;\n const doSpawn = opts.spawnInstall ?? defaultSpawn;\n\n write(`current: reasonix ${VERSION}\\n`);\n const latest = await fetchLatest();\n if (!latest) {\n write(\"could not reach registry.npmjs.org — check your network.\\n\");\n exit(1);\n return;\n }\n write(`latest: reasonix ${latest}\\n`);\n\n const plan = planUpdate({ current: VERSION, latest, npx: isNpx() });\n write(`\\n${plan.message}\\n`);\n\n if (plan.action !== \"run-npm-install\" || !plan.command) return;\n if (opts.dryRun) {\n write(`(dry run) would run: ${plan.command.join(\" \")}\\n`);\n return;\n }\n write(`\\nrunning: ${plan.command.join(\" \")}\\n`);\n const code = await doSpawn(plan.command);\n if (code !== 0) {\n write(`\\nnpm exited with code ${code}. upgrade did not complete.\\n`);\n exit(code);\n }\n}\n","import { VERSION } from \"../../index.js\";\n\nexport function versionCommand(): void {\n console.log(`reasonix ${VERSION}`);\n}\n","/**\n * Merge config defaults with CLI flags into the concrete options that\n * `chatCommand` / `runCommand` need.\n *\n * Precedence (highest wins):\n * 1. Explicit per-setting CLI flag (`--model`, `--harvest`, `--branch`, `--mcp`)\n * 2. Explicit `--preset <name>` CLI flag\n * 3. `config.preset` from `~/.reasonix/config.json` (set by `reasonix setup`)\n * 4. Hardcoded \"fast\" preset defaults\n *\n * Keeping this logic in one place — rather than duplicating across\n * `chat` and `run` — means the precedence rule only lives in one unit\n * test and the shape of the merge is identical for both commands.\n */\n\nimport { type PresetName, type ReasonixConfig, readConfig } from \"../config.js\";\nimport { PRESETS } from \"./ui/presets.js\";\n\nexport interface ResolvedDefaults {\n model: string;\n harvest: boolean;\n branch: number | undefined;\n mcp: string[];\n session: string | undefined;\n}\n\nexport interface RawCliFlags {\n model?: string;\n harvest?: boolean;\n /** From `commander`; already parseInt'd. */\n branch?: number;\n mcp?: string[];\n /** Commander's `--no-session` surfaces as `false`; `--session X` as a string. */\n session?: string | false;\n /** `--preset <name>`. */\n preset?: string;\n /** When true, ignore config entirely (power-user escape hatch). */\n noConfig?: boolean;\n}\n\nexport function resolveDefaults(flags: RawCliFlags): ResolvedDefaults {\n const cfg: ReasonixConfig = flags.noConfig ? {} : readConfig();\n const preset = pickPreset(flags.preset, cfg.preset);\n const presetSettings = PRESETS[preset];\n\n const model = flags.model ?? presetSettings.model;\n const harvest = flags.harvest === true ? true : presetSettings.harvest;\n const branchFromFlag = normalizeBranch(flags.branch);\n const branch = branchFromFlag ?? (presetSettings.branch > 1 ? presetSettings.branch : undefined);\n\n // `--mcp` accumulator is [] when absent. Treat empty from flags as\n // \"user didn't pass\" → fall through to config. Users who explicitly\n // want zero MCP servers can pass `--no-config` or edit the file.\n const mcp = flags.mcp && flags.mcp.length > 0 ? flags.mcp : (cfg.mcp ?? []);\n\n const session = resolveSession(flags.session, cfg.session);\n\n return { model, harvest, branch, mcp, session };\n}\n\nfunction pickPreset(\n flagPreset: string | undefined,\n configPreset: PresetName | undefined,\n): PresetName {\n if (flagPreset && isPresetName(flagPreset)) return flagPreset;\n if (configPreset) return configPreset;\n return \"fast\";\n}\n\nfunction isPresetName(s: string): s is PresetName {\n return s === \"fast\" || s === \"smart\" || s === \"max\";\n}\n\nfunction normalizeBranch(raw: number | undefined): number | undefined {\n if (raw === undefined) return undefined;\n if (!Number.isFinite(raw) || raw <= 1) return undefined;\n return Math.min(raw, 8);\n}\n\nfunction resolveSession(\n flag: string | false | undefined,\n configSession: string | null | undefined,\n): string | undefined {\n if (flag === false) return undefined; // --no-session\n if (typeof flag === \"string\" && flag.length > 0) return flag;\n if (configSession === null) return undefined; // config opted out\n if (typeof configSession === \"string\" && configSession.length > 0) return configSession;\n return \"default\";\n}\n"],"mappings":";;;;;;;;;;;;AAAA,SAAS,eAAe;;;ACgBxB,SAAS,WAAW,WAAW,cAAc,qBAAqB;AAClE,SAAS,eAAe;AACxB,SAAS,SAAS,YAAY;AA+CvB,SAAS,oBAA4B;AAC1C,SAAO,KAAK,QAAQ,GAAG,aAAa,aAAa;AACnD;AAEO,SAAS,WAAW,OAAe,kBAAkB,GAAmB;AAC7E,MAAI;AACF,UAAM,MAAM,aAAa,MAAM,MAAM;AACrC,UAAM,SAAS,KAAK,MAAM,GAAG;AAC7B,QAAI,UAAU,OAAO,WAAW,SAAU,QAAO;AAAA,EACnD,QAAQ;AAAA,EAER;AACA,SAAO,CAAC;AACV;AAEO,SAAS,YAAY,KAAqB,OAAe,kBAAkB,GAAS;AACzF,YAAU,QAAQ,IAAI,GAAG,EAAE,WAAW,KAAK,CAAC;AAC5C,gBAAc,MAAM,KAAK,UAAU,KAAK,MAAM,CAAC,GAAG,MAAM;AAExD,MAAI;AACF,cAAU,MAAM,GAAK;AAAA,EACvB,QAAQ;AAAA,EAER;AACF;AAGO,SAAS,WAAW,OAAe,kBAAkB,GAAuB;AACjF,MAAI,QAAQ,IAAI,iBAAkB,QAAO,QAAQ,IAAI;AACrD,SAAO,WAAW,IAAI,EAAE;AAC1B;AAOO,SAAS,cAAc,OAAe,kBAAkB,GAAY;AACzE,QAAM,MAAM,QAAQ,IAAI;AACxB,MAAI,QAAQ,SAAS,QAAQ,WAAW,QAAQ,IAAK,QAAO;AAC5D,QAAM,MAAM,WAAW,IAAI,EAAE;AAC7B,MAAI,QAAQ,MAAO,QAAO;AAC1B,SAAO;AACT;AAEO,SAAS,WAAW,KAAa,OAAe,kBAAkB,GAAS;AAChF,QAAM,MAAM,WAAW,IAAI;AAC3B,MAAI,SAAS,IAAI,KAAK;AACtB,cAAY,KAAK,IA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AChB,CAAC;","names":["resolve","readFileSync","homedir","join","settings","resolve","stdout","stdin","signature","chmodSync","existsSync","mkdirSync","readFileSync","writeFileSync","homedir","dirname","join","resolve","walk","header","i","j","DEFAULT_MAX_RESULT_CHARS","costUsd","spawn","existsSync","statSync","pathMod","resolve","delimiter","header","header","readFileSync","resolve","readFileSync","records","round","p","resolve","spawn","resolve","createParser","resolve","existsSync","mkdirSync","readFileSync","unlinkSync","writeFileSync","dirname","resolve","existsSync","mkdirSync","readFileSync","writeFileSync","homedir","dirname","join","appendFileSync","existsSync","mkdirSync","readFileSync","statSync","homedir","dirname","join","join","homedir","mkdirSync","dirname","appendFileSync","existsSync","readFileSync","statSync","existsSync","statSync","React","useState","Box","Text","useInput","React","useEffect","useRef","useState","header","Box","Text","React","Box","Text","React","header","row","React","React","EventRow","Box","Text","truncate","pct","Box","Text","React","Box","Text","React","useState","React","Box","Text","Box","Text","useInput","React","useState","Box","Text","useInput","React","useState","useState","useInput","React","Box","Text","Box","Text","React","React","Box","Text","Box","Text","React","Box","Text","React","React","Box","Text","existsSync","readFileSync","existsSync","readFileSync","pct","lines","name","skill","header","stdout","harvest","useState","useRef","useEffect","summary","useInput","React","Box","Text","pct","header","countLines","Box","Text","React","React","Box","Text","Box","Text","useApp","React","useState","useState","useApp","React","Box","Text","useState","React","mcp","existsSync","statSync","resolve","codeSystemPrompt","resolve","writeFileSync","basename","render","React","Box","Static","Text","useApp","useInput","React","useState","Box","Text","React","React","Box","Text","truncate","pct","useApp","useState","useInput","React","Box","Text","costDelta","Static","basename","writeFileSync","render","React","render","pad","render","React","Box","Static","Text","useApp","useInput","React","useMemo","useState","useApp","useState","useInput","useMemo","React","Box","Text","Static","render","React","oneLine","runCommand","prefix","mcp","oneLine","truncate","render","React","Box","Text","useApp","useInput","TextInput","React","useState","useApp","useState","useInput","React","Box","Text","entry","TextInput","render","React","spawn","resolve","spawn","harvest","mcp","runCommand"]}
|