zidane 5.11.2 → 5.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{agent-D0W9yClt.d.ts → agent-Dt3mALPV.d.ts} +209 -30
- package/dist/agent-Dt3mALPV.d.ts.map +1 -0
- package/dist/chat/pure.d.ts +3 -3
- package/dist/chat.d.ts +6 -6
- package/dist/chat.js +2 -2
- package/dist/contexts/e2b.d.ts +1 -1
- package/dist/eval.d.ts +1 -1
- package/dist/eval.js +2 -2
- package/dist/{headless-Bb5gU8AR.js → headless-BqfIgk1W.js} +39 -16
- package/dist/headless-BqfIgk1W.js.map +1 -0
- package/dist/headless.d.ts +1 -1
- package/dist/headless.js +1 -1
- package/dist/{index-DZR99FD4.d.ts → index-BDRh3kup.d.ts} +13 -2
- package/dist/index-BDRh3kup.d.ts.map +1 -0
- package/dist/{index-D60tX5XC.d.ts → index-Do7IZGW5.d.ts} +2 -2
- package/dist/{index-D60tX5XC.d.ts.map → index-Do7IZGW5.d.ts.map} +1 -1
- package/dist/index.d.ts +5 -5
- package/dist/index.js +15 -12
- package/dist/index.js.map +1 -1
- package/dist/{logger-n4LsLISE.d.ts → logger-C2E41UWq.d.ts} +2 -2
- package/dist/{logger-n4LsLISE.d.ts.map → logger-C2E41UWq.d.ts.map} +1 -1
- package/dist/{login-BHhOdTp9.js → login-DRKh-Uit.js} +33 -5
- package/dist/login-DRKh-Uit.js.map +1 -0
- package/dist/{mcp-Cy9mgCcr.js → mcp-CKlcFeLQ.js} +140 -13
- package/dist/mcp-CKlcFeLQ.js.map +1 -0
- package/dist/mcp.d.ts +1 -1
- package/dist/mcp.js +1 -1
- package/dist/media-sniff-Bn76JxAu.js +216 -0
- package/dist/media-sniff-Bn76JxAu.js.map +1 -0
- package/dist/{messages-RPKrEPvH.js → messages-DOKdwQBD.js} +538 -53
- package/dist/messages-DOKdwQBD.js.map +1 -0
- package/dist/output/stream-json.d.ts +2 -2
- package/dist/output/stream-json.js +1 -1
- package/dist/output/terminal.d.ts +2 -2
- package/dist/{presets-D5ibZTml.js → presets-DErpoTHg.js} +2 -2
- package/dist/{presets-D5ibZTml.js.map → presets-DErpoTHg.js.map} +1 -1
- package/dist/presets.d.ts +2 -2
- package/dist/presets.js +1 -1
- package/dist/{providers-C2cxujp_.js → providers-BOEzzCRs.js} +54 -21
- package/dist/providers-BOEzzCRs.js.map +1 -0
- package/dist/providers.d.ts +1 -1
- package/dist/providers.js +2 -2
- package/dist/{read-state-BFqpQRc5.js → read-state-DH2IuQHX.js} +2 -2
- package/dist/{read-state-BFqpQRc5.js.map → read-state-DH2IuQHX.js.map} +1 -1
- package/dist/restate.d.ts +1 -1
- package/dist/restate.js +1 -1
- package/dist/session/sqlite.d.ts +1 -1
- package/dist/{session-Do_TQV7c.js → session-W5_HQYU8.js} +2 -2
- package/dist/{session-Do_TQV7c.js.map → session-W5_HQYU8.js.map} +1 -1
- package/dist/session.d.ts +1 -1
- package/dist/session.js +2 -2
- package/dist/skills.d.ts +2 -2
- package/dist/{tool-formatters-RT5-gyE2.d.ts → tool-formatters-COmtAwgF.d.ts} +2 -2
- package/dist/{tool-formatters-RT5-gyE2.d.ts.map → tool-formatters-COmtAwgF.d.ts.map} +1 -1
- package/dist/tools/fetch-url.d.ts +1 -1
- package/dist/tools/web-search.d.ts +1 -1
- package/dist/{tools-ZHKOh44k.js → tools-CVFNtlyc.js} +48 -101
- package/dist/tools-CVFNtlyc.js.map +1 -0
- package/dist/tools.d.ts +2 -2
- package/dist/tools.js +2 -2
- package/dist/{transcript-anchors-0zzqcSm5.js → transcript-anchors-BftfURAc.js} +22 -15
- package/dist/transcript-anchors-BftfURAc.js.map +1 -0
- package/dist/{transcript-anchors-B4FxkG-8.d.ts → transcript-anchors-DLa8m9_E.d.ts} +4 -4
- package/dist/{transcript-anchors-B4FxkG-8.d.ts.map → transcript-anchors-DLa8m9_E.d.ts.map} +1 -1
- package/dist/tui.d.ts +3 -3
- package/dist/tui.js +8 -8
- package/dist/tui.js.map +1 -1
- package/dist/{turn-operations-CoRj3mYZ.d.ts → turn-operations-ifKg5muR.d.ts} +3 -3
- package/dist/{turn-operations-CoRj3mYZ.d.ts.map → turn-operations-ifKg5muR.d.ts.map} +1 -1
- package/dist/{types-BiobHM1D.js → types-DxHDaqN7.js} +23 -6
- package/dist/{types-BiobHM1D.js.map → types-DxHDaqN7.js.map} +1 -1
- package/dist/types.d.ts +3 -3
- package/dist/types.js +1 -1
- package/docs/RESTATE.md +25 -0
- package/package.json +2 -1
- package/dist/agent-D0W9yClt.d.ts.map +0 -1
- package/dist/headless-Bb5gU8AR.js.map +0 -1
- package/dist/image-sniff-B7uFSNO1.js +0 -90
- package/dist/image-sniff-B7uFSNO1.js.map +0 -1
- package/dist/index-DZR99FD4.d.ts.map +0 -1
- package/dist/login-BHhOdTp9.js.map +0 -1
- package/dist/mcp-Cy9mgCcr.js.map +0 -1
- package/dist/messages-RPKrEPvH.js.map +0 -1
- package/dist/providers-C2cxujp_.js.map +0 -1
- package/dist/tools-ZHKOh44k.js.map +0 -1
- package/dist/transcript-anchors-0zzqcSm5.js.map +0 -1
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"login-BHhOdTp9.js","names":[],"sources":["../src/compact/errors.ts","../src/compact/messages.ts","../src/compact/prompt.ts","../src/compact/compact.ts","../src/compact/restore.ts","../src/mcp/oauth-callback.ts","../src/mcp/login.ts"],"sourcesContent":["/**\n * Typed errors thrown by the compaction helper.\n *\n * Lives in its own file so both the runner and the pure messages module\n * can import without circular dependencies.\n */\n\n/**\n * Raised when the caller's inputs make compaction meaningless before any\n * API call is attempted. Common cases:\n * - empty `turns`\n * - `keepTurns >= turns.length` (no older content to summarize)\n * - `'from'` / `'up_to'` anchor id not found in `turns`\n * - the resolved `toSummarize` slice has no text-bearing content\n *\n * Synchronous — thrown from `compactConversation()` before the provider\n * call so the caller can recover without a network round-trip.\n */\nexport class CompactInvalidInputError extends Error {\n constructor(message: string) {\n super(message)\n this.name = 'CompactInvalidInputError'\n }\n}\n\n/**\n * Raised when the provider rejects the compaction request with\n * `prompt_too_long` (or an equivalent) and the head-truncation retry\n * budget has been exhausted. Callers can inspect `ptlRetries` to log\n * how far the retry loop got before giving up.\n */\nexport class CompactPromptTooLongError extends Error {\n constructor(message: string, public readonly ptlRetries: number) {\n super(message)\n this.name = 'CompactPromptTooLongError'\n }\n}\n","/**\n * Pure helpers for compaction — slicing, image stripping, head-truncation\n * on `prompt_too_long`, and the synthetic-turn builder.\n *\n * Everything in this file is total and side-effect-free. Inputs are never\n * mutated; helpers return fresh arrays whenever they modify content.\n */\n\nimport type {\n SessionContentBlock,\n SessionTurn,\n ToolResultContent,\n TurnUsage,\n} from '../types'\nimport { CompactInvalidInputError } from './errors'\n\n/**\n * What to summarize and what to preserve verbatim.\n *\n * - `'full'` — summarize everything, no preserved tail.\n * - `'tail'` — summarize everything before the last `keepTurns` turns.\n * - `{ kind: 'from', turnId }` — summarize from the anchor turn onward.\n * - `{ kind: 'up_to', turnId }` — summarize up to (and including) the anchor.\n */\nexport type CompactScope\n = | 'full'\n | 'tail'\n | { kind: 'from', turnId: string }\n | { kind: 'up_to', turnId: string }\n\nexport interface CompactionSlice {\n /** The portion of the conversation that will be summarized. */\n toSummarize: readonly SessionTurn[]\n /** The portion that stays verbatim in the post-compact history. */\n preserved: readonly SessionTurn[]\n}\n\n/**\n * Partition `turns` into `(toSummarize, preserved)` according to `scope`.\n *\n * Throws {@link CompactInvalidInputError} on degenerate inputs so the\n * caller doesn't pay for a doomed provider call:\n * - empty `turns`\n * - `scope: 'tail'` with `keepTurns >= turns.length` (nothing to summarize)\n * - `scope: { from | up_to }` with an anchor id that isn't in `turns`\n * - the resulting `toSummarize` slice contains no text-bearing content\n * (only system turns or empty content)\n *\n * Pure. Returns references to the original `SessionTurn` objects — the\n * caller can compare by identity (=== Object.is) to confirm.\n */\nexport function sliceForCompaction(\n turns: readonly SessionTurn[],\n scope: CompactScope,\n keepTurns: number,\n): CompactionSlice {\n if (turns.length === 0)\n throw new CompactInvalidInputError('No turns to compact.')\n\n let toSummarize: readonly SessionTurn[]\n let preserved: readonly SessionTurn[]\n\n if (scope === 'full') {\n toSummarize = turns\n preserved = []\n }\n else if (scope === 'tail') {\n const keep = Math.max(0, keepTurns)\n if (keep >= turns.length) {\n throw new CompactInvalidInputError(\n `Nothing to compact: keepTurns (${keep}) covers the entire conversation (${turns.length} turns).`,\n )\n }\n const safeCut = findSafeRoundBoundary(turns, turns.length - keep)\n toSummarize = turns.slice(0, safeCut)\n preserved = turns.slice(safeCut)\n }\n else if (scope.kind === 'from') {\n const idx = turns.findIndex(t => t.id === scope.turnId)\n if (idx < 0)\n throw new CompactInvalidInputError(`Anchor turn not found: \"${scope.turnId}\".`)\n // 'from' summarizes the recent portion (anchor onward); everything\n // before stays verbatim. Walk the anchor BACKWARD to the start of\n // the round containing it so neither half ends mid-round (avoids\n // orphan `tool_use` in `preserved` AND orphan `tool_result` at\n // the head of `toSummarize`).\n const safeIdx = findSafeRoundBoundary(turns, idx)\n preserved = turns.slice(0, safeIdx)\n toSummarize = turns.slice(safeIdx)\n }\n else {\n // 'up_to' summarizes the older portion (up to and including the anchor);\n // everything after stays verbatim. Same `tool_use ↔ tool_result`\n // adjacency rule as 'tail': cut backward if the proposed boundary\n // would orphan a `tool_use` at the end of `toSummarize`.\n const idx = turns.findIndex(t => t.id === scope.turnId)\n if (idx < 0)\n throw new CompactInvalidInputError(`Anchor turn not found: \"${scope.turnId}\".`)\n const safeCut = findSafeRoundBoundary(turns, idx + 1)\n toSummarize = turns.slice(0, safeCut)\n preserved = turns.slice(safeCut)\n }\n\n if (toSummarize.length === 0)\n throw new CompactInvalidInputError('Compaction scope resolved to zero turns.')\n if (!hasTextBearingContent(toSummarize))\n throw new CompactInvalidInputError('Compaction scope contains no text-bearing turns to summarize.')\n\n return { toSummarize, preserved }\n}\n\n/**\n * Replace every attachment block in `turns` with a short text marker.\n *\n * Covers two shapes:\n * - Top-level `{ type: 'image', ... }` content blocks on user turns.\n * - Image entries inside `tool_result.output` array form (multimodal\n * tool results — e.g. an MCP browser screenshot).\n *\n * Unconditional by design: even on vision-capable models, the summary\n * call doesn't benefit from raw image bytes (the model can't refer to\n * them after the summary lands), and stripping uniformly avoids\n * `prompt_too_long` on image-heavy sessions.\n *\n * Returns a fresh array; input turns / blocks are never mutated.\n */\nexport function stripImagesFromTurns(turns: readonly SessionTurn[]): SessionTurn[] {\n return turns.map(turn => stripImagesFromTurn(turn))\n}\n\nfunction stripImagesFromTurn(turn: SessionTurn): SessionTurn {\n let touched = false\n const nextContent: SessionContentBlock[] = []\n for (const block of turn.content) {\n if (block.type === 'image') {\n touched = true\n nextContent.push({ type: 'text', text: '[image]' })\n continue\n }\n if (block.type === 'document') {\n touched = true\n nextContent.push({ type: 'text', text: '[document]' })\n continue\n }\n if (block.type === 'tool_result' && Array.isArray(block.output)) {\n const flat = stripAttachmentsFromToolResult(block.output)\n if (flat) {\n touched = true\n nextContent.push({ ...block, output: flat })\n continue\n }\n }\n nextContent.push(block)\n }\n return touched ? { ...turn, content: nextContent } : turn\n}\n\n/**\n * Return a fresh `ToolResultContent[]` with images flattened to `[image]`\n * text placeholders, or `null` when no image blocks were present (caller\n * keeps the original input). Returning a new mutable array — never the\n * input — keeps the `tool_result.output` slot's mutable type contract\n * satisfied without leaky `as`-casts at the call site.\n */\nfunction stripAttachmentsFromToolResult(parts: readonly ToolResultContent[]): ToolResultContent[] | null {\n let touched = false\n const out: ToolResultContent[] = []\n for (const part of parts) {\n if (part.type === 'image') {\n touched = true\n out.push({ type: 'text', text: '[image]' })\n }\n else if (part.type === 'document') {\n touched = true\n out.push({ type: 'text', text: '[document]' })\n }\n else {\n out.push(part)\n }\n }\n return touched ? out : null\n}\n\n/**\n * Drop the oldest \"round\" from `turns` and return a fresh array. Used by\n * the PTL retry path to shrink the prompt one round at a time.\n *\n * A round is a contiguous `[user, assistant?, tool_results?]` group. The\n * function walks forward from index 0, advances through the user turn\n * and any trailing assistant + tool-result turns belonging to the same\n * exchange, and returns the remainder.\n *\n * Adjacency-safe: when the oldest user turn carries `tool_result` blocks\n * answering an assistant turn ahead of it (rare — happens during\n * resume), the function keeps walking until the next clean boundary so\n * the resulting array still respects every provider's `tool_use ↔\n * tool_result` adjacency rule.\n *\n * Returns `turns` unchanged when only one round (or less) remains — the\n * caller is expected to interpret that as \"cannot shrink further\" and\n * give up the retry loop.\n */\nexport function truncateHeadForPtlRetry(turns: readonly SessionTurn[]): SessionTurn[] {\n if (turns.length <= 1)\n return turns.slice()\n\n // Find the first turn that opens a clean conversational round we can\n // drop ending on — start with the first `user` turn (it's the natural\n // start-of-round marker).\n const firstUserIdx = turns.findIndex(t => t.role === 'user')\n if (firstUserIdx < 0)\n return turns.slice()\n\n // Skip past every turn that belongs to this round: the user turn\n // itself, the assistant reply (if any), and the next user turn whose\n // content is *only* tool_result blocks (an immediate follow-up that\n // closes out tool calls). Stop when we reach the next round-opening\n // user turn (i.e. one that carries non-tool_result content).\n let cursor = firstUserIdx + 1\n while (cursor < turns.length) {\n const turn = turns[cursor]\n if (turn.role === 'assistant') {\n cursor++\n continue\n }\n if (turn.role === 'user' && isToolResultsOnlyTurn(turn)) {\n cursor++\n continue\n }\n break\n }\n\n // If the cursor walked all the way to the end, refuse to truncate —\n // the remaining slice would be empty, which is never useful.\n if (cursor >= turns.length)\n return turns.slice()\n\n return turns.slice(cursor)\n}\n\nfunction isToolResultsOnlyTurn(turn: SessionTurn): boolean {\n if (turn.content.length === 0)\n return false\n return turn.content.every(block => block.type === 'tool_result')\n}\n\n/**\n * Walk `proposedCut` backward to the nearest position where splitting at\n * that index produces a round-boundary-clean partition — i.e. neither\n * half breaks the `tool_use ↔ tool_result` adjacency rule that every\n * provider (most strictly Anthropic) enforces.\n *\n * The hazardous case: the proposed cut lands BETWEEN an assistant turn\n * carrying `tool_call` blocks and the user turn carrying the matching\n * `tool_result` blocks. Then `toSummarize` ends with an orphan\n * `tool_use` (provider 400 on the summarization request) AND `preserved`\n * starts with an orphan `tool_result` (provider 400 on the next live\n * agent run against the wire-level cutoff output).\n *\n * Algorithm: keep walking `cut` backward as long as `turns[cut - 1]`\n * is an assistant turn with at least one `tool_call` block. The walk\n * stops when:\n * - we reach `cut = 0` (slice would be empty; caller's existing\n * \"scope resolved to zero turns\" guard handles it), or\n * - the trailing turn is user-role (clean — model emits no pending\n * tool_use from user turns), or\n * - the trailing turn is assistant text without tool_use (clean —\n * text-only response is a complete round).\n *\n * Returning the adjusted cut over-preserves the tail relative to the\n * caller's request — `keepTurns` is interpreted as the MINIMUM number\n * of turns kept verbatim, not the exact count.\n */\nfunction findSafeRoundBoundary(turns: readonly SessionTurn[], proposedCut: number): number {\n let cut = Math.max(0, Math.min(turns.length, proposedCut))\n while (cut > 0 && hasPendingToolUse(turns[cut - 1]))\n cut--\n return cut\n}\n\n/** Does this turn end with any unanswered `tool_use` blocks? */\nfunction hasPendingToolUse(turn: SessionTurn): boolean {\n if (turn.role !== 'assistant')\n return false\n for (const block of turn.content) {\n if (block.type === 'tool_call')\n return true\n }\n return false\n}\n\nfunction hasTextBearingContent(turns: readonly SessionTurn[]): boolean {\n for (const turn of turns) {\n if (turn.role === 'system')\n continue\n for (const block of turn.content) {\n if (block.type === 'text' && block.text.trim().length > 0)\n return true\n if (block.type === 'tool_call' || block.type === 'tool_result')\n return true\n }\n }\n return false\n}\n\n// ---------------------------------------------------------------------------\n// Summary turn builder\n// ---------------------------------------------------------------------------\n\n/**\n * Maximum length of an anchor turn's textual preview, in characters. Long\n * enough to give the model recognizable context (the first paragraph of\n * a typical user message), short enough that it doesn't blow the\n * cache-stability invariant for the prompt prefix.\n */\nexport const ANCHOR_PREVIEW_MAX_CHARS = 200\n\n/**\n * Extract the first ~200 chars of text-bearing content from a turn — the\n * preview surfaced in `from` / `up_to` direction prompts so the model\n * knows where the slice begins.\n */\nexport function anchorPreviewFor(turn: SessionTurn): string {\n for (const block of turn.content) {\n if (block.type === 'text' && block.text.trim().length > 0) {\n const flat = block.text.replace(/\\s+/g, ' ').trim()\n return flat.length > ANCHOR_PREVIEW_MAX_CHARS\n ? `${flat.slice(0, ANCHOR_PREVIEW_MAX_CHARS - 1)}…`\n : flat\n }\n }\n return '(no preview available)'\n}\n\n/**\n * Input shape for {@link summaryToTurn}. Designed to align with the\n * fields of `CompactResult` so a caller can spread the runner's output\n * with a single `replacesTurnIds` rename — no field-by-field unpacking\n * required.\n *\n * Primitive shape (no `CompactResult` import) keeps this module free of\n * dependencies on the runner — `compact.ts` imports from here, not the\n * other way around.\n */\nexport interface SummaryToTurnInput {\n /** Summary text — typically `CompactResult.summary`. */\n summary: string\n /** Turn ids being replaced — typically `CompactResult.summarizedTurnIds`. */\n replacesTurnIds: readonly string[]\n /** Model id that produced the summary. */\n model: string\n /** Token usage from the summary call. */\n usage: TurnUsage\n /** Defaults to `Date.now()` when omitted. */\n compactedAt?: number\n}\n\n/**\n * Build a synthetic `SessionTurn` carrying a single `compact-summary`\n * block, ready to append to a session.\n *\n * The turn's role is `'user'` so it sits at a conversational boundary\n * the way the model expects. The caller is responsible for\n * `session.appendTurns([turn])`. The id is freshly generated via\n * `crypto.randomUUID()` so collisions are statistically impossible.\n *\n * Typical use after running `compactConversation`:\n *\n * ```ts\n * const result = await compactConversation({ provider, turns })\n * const turn = summaryToTurn({\n * summary: result.summary,\n * replacesTurnIds: result.summarizedTurnIds,\n * model: result.model,\n * usage: result.usage,\n * })\n * await session.appendTurns([turn])\n * ```\n */\nexport function summaryToTurn(input: SummaryToTurnInput): SessionTurn {\n const compactedAt = input.compactedAt ?? Date.now()\n return {\n id: crypto.randomUUID(),\n role: 'user',\n content: [{\n type: 'compact-summary',\n replacesTurnIds: input.replacesTurnIds,\n summary: input.summary,\n model: input.model,\n usage: input.usage,\n compactedAt,\n }],\n createdAt: compactedAt,\n }\n}\n","/**\n * Pure prompt builders for conversation compaction.\n *\n * The builders produce the **system prompt** for a no-tools summary call.\n * They are total functions of `(direction, anchorPreview?)` and produce\n * byte-stable output for the same inputs — that's load-bearing for the\n * provider's prompt cache: repeated compactions in the same host process\n * share the same prefix and read cache instead of writing it.\n *\n * Inspired by Claude Code's `services/compact/prompt.ts` — same 9-section\n * scaffold, same `<analysis> + <summary>` envelope, same no-tools\n * preamble. Adapted to zidane-specific wording (no \"Claude\" references)\n * and trimmed of the bits that don't apply (no `marble_origami`-style\n * query sources, no compaction-fingerprint header).\n */\n\n/** Identifier for the section of the conversation being summarized. */\nexport type CompactDirection = 'full' | 'tail' | 'from' | 'up_to'\n\nexport interface CompactPromptOptions {\n direction: CompactDirection\n /**\n * Short preview of the anchor turn's text — only used by `'from'` and\n * `'up_to'`. Pass the last ~200 chars of the anchor turn so the model\n * has a recognizable handle on where the slice begins / ends. Empty /\n * undefined for `'full'` and `'tail'`.\n */\n anchorPreview?: string\n}\n\n/**\n * Function shape for callers that want to swap in a domain-specific\n * summary prompt (security review handoff, support-ticket continuation,\n * etc.) without touching the runner. Default: {@link buildCompactPrompt}.\n */\nexport type CompactPromptBuilder = (opts: CompactPromptOptions) => string\n\n// ---------------------------------------------------------------------------\n// Composable blocks — each one is a frozen string for cache-stability and\n// quoted verbatim from inside the named builders below. Exported so\n// callers building custom prompts can stitch them together.\n// ---------------------------------------------------------------------------\n\n/**\n * No-tools guard. The runner sends `tools: []` to the provider already,\n * but some models still hallucinate tool-call intent on a long\n * conversation. The prose guard is cheap insurance.\n */\nexport const NO_TOOLS_PREAMBLE = `CRITICAL: Respond with TEXT ONLY. Do NOT call any tools.\n\n- Do NOT use Read, Bash, Grep, Glob, Edit, Write, or ANY other tool.\n- You already have all the context you need in the conversation above.\n- Tool calls will be REJECTED and will waste your only turn — you will fail the task.\n- Your entire response must be plain text: an <analysis> block followed by a <summary> block.`\n\n/**\n * Body shared by every direction. Lays out the 9-section scaffold,\n * mirrors Claude Code's `BASE_COMPACT_PROMPT` so a model already trained\n * on the layout produces the same shape.\n */\nexport const BASE_INSTRUCTIONS = `Your task is to create a detailed summary of the conversation so far, paying close attention to the user's explicit requests and your previous actions.\n\nThis summary should be thorough in capturing technical details, code patterns, and architectural decisions that would be essential for continuing development work without losing context.\n\nBefore providing your final summary, wrap your analysis in <analysis> tags to organize your thoughts and ensure you've covered all necessary points. In your analysis process:\n\n1. Chronologically analyze each message and section of the conversation. For each section thoroughly identify:\n - The user's explicit requests and intents\n - Your approach to addressing the user's requests\n - Key decisions, technical concepts and code patterns\n - Specific details like file names, full code snippets, function signatures, file edits\n - Errors that you ran into and how you fixed them\n - Pay special attention to specific user feedback that you received, especially if the user told you to do something differently.\n2. Double-check for technical accuracy and completeness.\n\nYour summary, wrapped in <summary> tags, must include the following sections:\n\n1. Primary Request and Intent\n2. Key Technical Concepts\n3. Files and Code Sections (with paths; include code snippets only when load-bearing)\n4. Errors and fixes\n5. Problem Solving\n6. All user messages (list ALL non-tool user messages, verbatim)\n7. Pending Tasks\n8. Current Work\n9. Optional Next Step (include direct quotes from the most recent conversation when relevant)`\n\n/** Trailer prompting the model to begin. Same on every direction. */\nexport const TRAILER = 'Provide your <analysis> and <summary> now.'\n\n// ---------------------------------------------------------------------------\n// Direction blurbs — the only knob that varies between builders. Each is\n// frozen and concise so cache-key drift is impossible between calls with\n// the same direction.\n// ---------------------------------------------------------------------------\n\nconst FULL_BLURB = `## Scope\nThe conversation above is being summarized in full. Capture every section the user might need to resume from a fresh context.`\n\nconst TAIL_BLURB = `## Scope\nSummarize the conversation above. The most recent turns will be preserved verbatim alongside your summary, so prioritize older context that would otherwise be lost.`\n\nconst FROM_BLURB = `## Scope\nSummarize the conversation FROM the marked anchor onward (the recent portion). Everything before the anchor will be preserved verbatim.\n\nAnchor turn (preview):\n%ANCHOR_PREVIEW%`\n\nconst UP_TO_BLURB = `## Scope\nSummarize the conversation UP TO the marked anchor (the older portion). Everything from the anchor onward will be preserved verbatim — your summary's job is to compress the prior context the user can no longer scroll back to.\n\nAnchor turn (preview):\n%ANCHOR_PREVIEW%`\n\n// ---------------------------------------------------------------------------\n// Named builders — discoverable, single-purpose, cache-stable.\n// ---------------------------------------------------------------------------\n\n/** Compose the full prompt with a custom direction-blurb. Internal helper. */\nfunction compose(blurb: string): string {\n return [NO_TOOLS_PREAMBLE, BASE_INSTRUCTIONS, blurb, TRAILER].join('\\n\\n')\n}\n\nexport function buildFullCompactPrompt(): string {\n return compose(FULL_BLURB)\n}\n\nexport function buildTailCompactPrompt(): string {\n return compose(TAIL_BLURB)\n}\n\nexport function buildFromCompactPrompt(anchorPreview: string): string {\n return compose(FROM_BLURB.replace('%ANCHOR_PREVIEW%', anchorPreview))\n}\n\nexport function buildUpToCompactPrompt(anchorPreview: string): string {\n return compose(UP_TO_BLURB.replace('%ANCHOR_PREVIEW%', anchorPreview))\n}\n\n/**\n * Default public builder. Dispatches by direction to the four named\n * builders. Throws when `from` / `up_to` are passed without an\n * `anchorPreview` — those scopes only make sense with an anchor and a\n * silent fallback would produce a prompt that doesn't tell the model\n * where the slice begins.\n */\nexport const buildCompactPrompt: CompactPromptBuilder = (opts) => {\n switch (opts.direction) {\n case 'full':\n return buildFullCompactPrompt()\n case 'tail':\n return buildTailCompactPrompt()\n case 'from': {\n const preview = opts.anchorPreview ?? ''\n if (preview.length === 0)\n throw new Error('buildCompactPrompt: `anchorPreview` is required for direction \"from\".')\n return buildFromCompactPrompt(preview)\n }\n case 'up_to': {\n const preview = opts.anchorPreview ?? ''\n if (preview.length === 0)\n throw new Error('buildCompactPrompt: `anchorPreview` is required for direction \"up_to\".')\n return buildUpToCompactPrompt(preview)\n }\n }\n}\n","/**\n * `compactConversation` — drive a one-shot summary call against a\n * provider and return a structured envelope describing what was\n * summarized and what stays verbatim.\n *\n * This is the harness primitive. It does not mutate the session, does\n * not own re-entrancy guards, does not enforce a circuit breaker — those\n * belong to whoever wires compaction into a control loop (the agent\n * loop's autocompact trigger, the TUI's session-details modal action,\n * an SDK consumer's batch job).\n *\n * Architecture: mirrors {@link generateSessionTitle} verbatim. The\n * provider's `stream()` is the one-shot completion primitive zidane\n * already exposes; this module reuses it directly instead of layering\n * a second abstraction on top.\n *\n * Caching: the system prompt is byte-stable per `(direction,\n * anchorPreview)` pair (see `./prompt.ts`). Repeated compactions in the\n * same host process share that prefix and read provider cache instead\n * of writing it.\n */\n\nimport type { Provider } from '../providers'\nimport type {\n SessionContentBlock,\n SessionMessage,\n SessionTurn,\n ThinkingLevel,\n TurnUsage,\n} from '../types'\nimport type { CompactionSlice, CompactScope } from './messages'\nimport type { CompactPromptBuilder } from './prompt'\nimport { ensureEndsWithUserMessage, ensureToolResultPairing } from '../session/messages'\nimport { toolOutputByteLength } from '../types'\nimport { CompactPromptTooLongError } from './errors'\nimport {\n anchorPreviewFor,\n sliceForCompaction,\n stripImagesFromTurns,\n truncateHeadForPtlRetry,\n} from './messages'\nimport { buildCompactPrompt } from './prompt'\nimport { utf8ByteLength } from './utils'\n\n// ---------------------------------------------------------------------------\n// Public API\n// ---------------------------------------------------------------------------\n\nexport interface CompactOptions {\n /** Provider used for the summary call. Called with empty tools list. */\n provider: Provider\n /** Conversation to compact, in chronological order. */\n turns: readonly SessionTurn[]\n /**\n * What to summarize. Default: `'tail'` — summarize everything before\n * the last `keepTurns` turns. See {@link CompactScope}.\n */\n scope?: CompactScope\n /**\n * Trailing turns left untouched when `scope: 'tail'`. Default: 4.\n * Matches `AgentBehavior.compactKeepTurns` so a host that uses both\n * mechanisms shares one knob.\n */\n keepTurns?: number\n /** Model id used for the summary call. Default: `provider.meta.defaultModel`. */\n model?: string\n /**\n * Maximum tokens the summary itself can occupy. Default: 20_000 — the\n * p99.99 of summary output size in Claude Code's `COMPACT_MAX_OUTPUT`.\n * Reasonable for any model with a 200k+ window.\n */\n maxOutputTokens?: number\n /** Optional reasoning level. Default: `'off'` — summarization rarely benefits from thinking. */\n thinking?: ThinkingLevel\n /** Optional cancellation signal forwarded to `provider.stream()`. */\n signal?: AbortSignal\n /**\n * Retries with head-truncation when the provider reports\n * `prompt_too_long`. Each retry drops the oldest conversational round\n * before re-issuing the call. Default: 3. Set 0 to fail-fast.\n */\n maxPtlRetries?: number\n /**\n * Optional builder override. The default builder dispatches by\n * direction to the named builders in `./prompt.ts`; pass a custom\n * function to swap in a domain-specific summary prompt.\n */\n prompt?: CompactPromptBuilder\n /**\n * Lifecycle hook for observability. Fires once per provider call,\n * including retries. The `kind` field tells you whether the call is\n * the initial attempt, a head-truncated retry, or a transient-error\n * retry — useful for surfacing progress in a UI spinner.\n */\n onAttempt?: (event: { attempt: number, kind: 'initial' | 'ptl-retry' | 'transient-retry' }) => void\n}\n\n/**\n * Directive appended as the trailing user message when the conversation\n * to summarize ends with an assistant turn. Mirrors the system-prompt\n * TRAILER text so the model gets the same imperative cue from both\n * surfaces — system instruction + final user message.\n *\n * Compaction-specific: paired with the compaction system prompt. Other\n * call-sites of {@link ensureEndsWithUserMessage} pass their own directive\n * (or the neutral `DEFAULT_USER_TAIL_DIRECTIVE`) so unrelated codepaths\n * don't inject a \"give me a summary\" cue into a regular turn.\n */\nconst SUMMARY_USER_DIRECTIVE = 'Provide your <analysis> and <summary> now.'\n\nexport interface CompactResult {\n /** The summary text, with any `<analysis>` block stripped. */\n summary: string\n /** Token usage from the (last successful) summary call. */\n usage: TurnUsage\n /** Model id used to produce the summary. */\n model: string\n /** Number of `prompt_too_long` retries that fired before success. 0 on first-try success. */\n ptlRetries: number\n /**\n * Turn ids actually covered by the summary — i.e., the ids of turns\n * that made it to the provider. **PTL-retry safe**: when head-truncation\n * shrank the scope, only the surviving (post-truncation) turn ids\n * appear here. Drives `summaryToTurn`'s `replacesTurnIds`; the wire-\n * level cutoff in `applyCompactSummaryCutoff` reads the same field\n * from the persisted marker.\n */\n summarizedTurnIds: readonly string[]\n /**\n * Turn ids dropped by PTL head-truncation before reaching the\n * provider. **Empty on first-try success.** These turns are NOT\n * covered by the summary — callers should either leave them in the\n * conversation history (they stay visible to the model verbatim,\n * since the id-based cutoff only elides ids the marker explicitly\n * claims) or surface a \"compaction lost N turns of context\" warning.\n *\n * The harness keeps these in the wire-level conversation by default;\n * the safety contract is \"the summary describes exactly what's in\n * `summarizedTurnIds` — nothing more, nothing less\".\n */\n droppedDueToPtl: readonly string[]\n /** Turns left untouched (the preserved tail / verbatim slice). */\n preservedTurns: readonly SessionTurn[]\n /** Byte length of turns actually summarized (post-PTL-truncation). */\n beforeBytes: number\n /** UTF-8 byte length of the summary text (rough \"after\" measure). */\n afterBytes: number\n}\n\nexport { CompactInvalidInputError, CompactPromptTooLongError } from './errors'\nexport type { CompactionSlice, CompactScope, SummaryToTurnInput } from './messages'\nexport type { CompactDirection, CompactPromptBuilder, CompactPromptOptions } from './prompt'\n\n// ---------------------------------------------------------------------------\n// Defaults — locked constants so behavior is predictable across host setups.\n// ---------------------------------------------------------------------------\n\n/** Default `keepTurns` for `scope: 'tail'`. Matches `AgentBehavior.compactKeepTurns`. */\nconst DEFAULT_KEEP_TURNS = 4\n\n/** Default max output tokens for the summary call. */\nconst DEFAULT_MAX_OUTPUT_TOKENS = 20_000\n\n/** Default PTL retry budget. */\nconst DEFAULT_MAX_PTL_RETRIES = 3\n\n/** Maximum transient-error retries before giving up. Independent of PTL. */\nconst TRANSIENT_RETRY_BUDGET = 2\n\n// ---------------------------------------------------------------------------\n// Runner\n// ---------------------------------------------------------------------------\n\nexport async function compactConversation(opts: CompactOptions): Promise<CompactResult> {\n // ---- Phase 1: pure preflight (no API call) -----------------------------\n // Throws CompactInvalidInputError on degenerate inputs so the caller can\n // bail without spending a network round-trip on a doomed request.\n const slice = sliceForCompaction(\n opts.turns,\n opts.scope ?? 'tail',\n opts.keepTurns ?? DEFAULT_KEEP_TURNS,\n )\n\n const direction = scopeToDirection(opts.scope ?? 'tail')\n const anchorPreview = direction === 'from' || direction === 'up_to'\n ? anchorPreviewFor(anchorTurnFor(slice, opts.scope!))\n : undefined\n\n const builder = opts.prompt ?? buildCompactPrompt\n const systemPrompt = builder({\n direction,\n ...(anchorPreview !== undefined ? { anchorPreview } : {}),\n })\n\n const model = opts.model ?? opts.provider.meta.defaultModel\n const maxOutputTokens = opts.maxOutputTokens ?? DEFAULT_MAX_OUTPUT_TOKENS\n const maxPtlRetries = Math.max(0, opts.maxPtlRetries ?? DEFAULT_MAX_PTL_RETRIES)\n\n // ---- Phase 2: retry-aware provider call --------------------------------\n // Strips images once up-front (cheap, deterministic) so each retry\n // doesn't re-flatten the same blocks.\n let workingTurns: readonly SessionTurn[] = stripImagesFromTurns(slice.toSummarize)\n let ptlRetries = 0\n let transientRetries = 0\n let attempt = 0\n\n while (true) {\n attempt++\n const kind: 'initial' | 'ptl-retry' | 'transient-retry'\n = ptlRetries > 0 ? 'ptl-retry' : transientRetries > 0 ? 'transient-retry' : 'initial'\n opts.onAttempt?.({ attempt, kind })\n\n try {\n // Build wire messages, then run the same defensive pairing pass\n // the agent loop uses on every live turn (see `applyPairingRepair`\n // in `src/loop.ts`). A session can contain orphan `tool_use` blocks\n // even after `sliceForCompaction`'s round-boundary walk: PTL\n // head-truncation shrinks the working set after the first attempt\n // (which can lop the head off an in-flight round), prior-loop\n // crashes can leave dangling tool_use ids in the persisted history,\n // and `turnsToMessages` itself can drop the only `text` block on\n // an assistant turn carrying signed thinking that has no signature.\n // Without this pass the summarizer fires a request the provider 400s\n // on with `'tool_use' ids were found without 'tool_result' blocks\n // immediately after`, which surfaces to the user as\n // \"compaction failed\" with a raw provider message.\n const paired = ensureToolResultPairing(turnsToMessages(workingTurns))\n const messages = ensureEndsWithUserMessage(paired, opts.provider, SUMMARY_USER_DIRECTIVE)\n const { summary, usage } = await runOnce({\n provider: opts.provider,\n model,\n system: systemPrompt,\n messages,\n maxTokens: maxOutputTokens,\n ...(opts.thinking !== undefined ? { thinking: opts.thinking } : {}),\n ...(opts.signal !== undefined ? { signal: opts.signal } : {}),\n })\n\n // PTL retries shrink the scope — the marker should ONLY claim\n // ownership of turns that actually fed the summary. Compute the\n // dropped set up-front (cheap; turn counts are O(100s) at worst)\n // so callers can decide whether to warn the user.\n const workingIds = new Set(workingTurns.map(t => t.id))\n const droppedDueToPtl: string[] = []\n if (ptlRetries > 0) {\n for (const t of slice.toSummarize) {\n if (!workingIds.has(t.id))\n droppedDueToPtl.push(t.id)\n }\n }\n\n return {\n summary,\n usage: { ...usage, modelId: usage.modelId ?? model },\n model,\n ptlRetries,\n summarizedTurnIds: workingTurns.map(t => t.id),\n droppedDueToPtl,\n preservedTurns: slice.preserved,\n beforeBytes: bytesIn(workingTurns),\n afterBytes: utf8ByteLength(summary),\n }\n }\n catch (err) {\n // Abort flows propagate unchanged so callers can pattern-match.\n if (isAbortError(err, opts.signal))\n throw err\n\n if (isPromptTooLongError(err)) {\n if (ptlRetries >= maxPtlRetries) {\n throw new CompactPromptTooLongError(\n `Compaction failed: prompt_too_long after ${ptlRetries} retries.`,\n ptlRetries,\n )\n }\n const truncated = truncateHeadForPtlRetry(workingTurns)\n if (truncated.length === workingTurns.length) {\n // Nothing more to truncate — refuse to spin.\n throw new CompactPromptTooLongError(\n `Compaction failed: prompt_too_long and conversation cannot be shrunk further.`,\n ptlRetries,\n )\n }\n workingTurns = truncated\n ptlRetries++\n continue\n }\n\n if (isTransientError(err) && transientRetries < TRANSIENT_RETRY_BUDGET) {\n transientRetries++\n continue\n }\n\n throw err\n }\n }\n}\n\n// ---------------------------------------------------------------------------\n// Internals\n// ---------------------------------------------------------------------------\n\ninterface RunOnceOptions {\n provider: Provider\n model: string\n system: string\n messages: SessionMessage[]\n maxTokens: number\n thinking?: ThinkingLevel\n signal?: AbortSignal\n}\n\ninterface RunOnceResult {\n summary: string\n usage: TurnUsage\n}\n\n/**\n * Single provider call. Strips the `<analysis>` block and returns the\n * post-trim summary text along with the usage report.\n *\n * Why not just use `result.text`: some providers stream deltas without\n * emitting a concatenated `text` field at the end (or set it to an\n * empty string when the stream finished via tool-call). Accumulating\n * deltas in `text` and falling back to `result.text` mirrors\n * `generateSessionTitle` and handles every adapter shape.\n */\nasync function runOnce(opts: RunOnceOptions): Promise<RunOnceResult> {\n let streamed = ''\n const result = await opts.provider.stream(\n {\n model: opts.model,\n system: opts.system,\n tools: [],\n messages: opts.messages,\n maxTokens: opts.maxTokens,\n ...(opts.thinking !== undefined ? { thinking: opts.thinking } : {}),\n ...(opts.signal !== undefined ? { signal: opts.signal } : {}),\n },\n {\n onText: (delta) => { streamed += delta },\n },\n )\n\n const raw = streamed.length > 0 ? streamed : result.text\n let summary = stripAnalysisBlock(raw).trim()\n\n // Reasoning-model fallback. Some models (Qwen-thinking, GLM, DeepSeek\n // reasoning variants, …) write the entire response into their\n // reasoning channel rather than `delta.content`. From this layer the\n // `text` stream is empty, but the assistant message carries\n // `{ type: 'thinking', text }` blocks containing the actual summary\n // (often still wrapped in `<analysis>` / `<summary>` tags from the\n // prompt). Walk those blocks and apply the same extraction waterfall\n // before giving up. Saves the user from \"model misbehaved, retry\" on\n // every compaction when their selected model happens to reason\n // instead of speak.\n if (summary.length === 0) {\n const thinking = collectThinkingText(result.assistantMessage)\n if (thinking.length > 0)\n summary = stripAnalysisBlock(thinking).trim()\n }\n\n if (summary.length === 0) {\n // Truly empty — aborted stream, tool-call-only turn, or a model\n // that refused to summarize. `stripAnalysisBlock` already fell back\n // to raw `<analysis>` content when the model wrapped everything in\n // the wrong tag; the thinking fallback above caught the reasoning-\n // channel case. Reaching this branch means there's nothing usable\n // anywhere in the response.\n throw new Error('Compaction failed: provider returned no summary text.')\n }\n return { summary, usage: result.usage }\n}\n\n/**\n * Concatenate every `thinking`-block's text from an assistant message.\n *\n * Reasoning models route their output through provider-specific\n * \"thinking\" channels (Anthropic native thinking, OpenAI o-series\n * reasoning, OpenAI-compat `reasoning_content`, OpenRouter\n * `reasoning_details`). All of them surface as `{ type: 'thinking', text }`\n * blocks on the normalized `SessionMessage.content` by the time the\n * provider's `stream()` returns. Walking the blocks is provider-agnostic\n * — works for every adapter without bespoke per-provider handling.\n *\n * Returns the empty string when no thinking blocks are present, so the\n * caller's \"did we get anything?\" check stays a single `.length === 0`.\n */\nfunction collectThinkingText(message: SessionMessage): string {\n const parts: string[] = []\n for (const block of message.content) {\n if (block.type === 'thinking' && typeof block.text === 'string' && block.text.length > 0)\n parts.push(block.text)\n }\n return parts.join('\\n')\n}\n\n/**\n * Extract the summary text from the provider's response, peeling off\n * any envelope the model wrapped it in.\n *\n * The compact prompt asks for `<analysis>...</analysis><summary>...</summary>`\n * but real models drift from the format. This function tries four\n * extraction paths in order of strictness:\n *\n * 1. **Strict path** — strip `<analysis>...</analysis>` blocks and\n * extract the `<summary>...</summary>` envelope. Matches the\n * prompt-following ideal.\n * 2. **Loose path** — same strip, but accept whatever's outside the\n * `<analysis>` tags as the summary (no `<summary>` envelope\n * required). Handles models that drop the wrapper but keep the\n * analysis.\n * 3. **Analysis-as-summary fallback** — when the entire response is a\n * single `<analysis>` block (model conflated the two concepts),\n * return the analysis content. Better than failing the compaction\n * and forcing the user to retry.\n * 4. **Raw passthrough** — no recognized envelope. Return the text\n * as-is and let `runOnce`'s empty-check decide whether to throw.\n *\n * Matches Claude Code's `formatCompactSummary` for path (1) + (2) and\n * adds (3) as a graceful-degradation layer we ran into in the wild\n * (smaller / non-Anthropic models sometimes produce analysis-only).\n */\nfunction stripAnalysisBlock(text: string): string {\n // Strict + loose: strip `<analysis>...</analysis>` non-greedy + dot-all\n // (no `s` flag in Bun's V8 — `[\\s\\S]` does the same).\n const analysisStripped = text.replace(/<analysis>[\\s\\S]*?<\\/analysis>/g, '')\n\n // Path 1: explicit `<summary>...</summary>` envelope.\n const summaryMatch = analysisStripped.match(/<summary>([\\s\\S]*?)<\\/summary>/)\n if (summaryMatch)\n return summaryMatch[1]\n\n // Path 2: anything outside the `<analysis>` block.\n if (analysisStripped.trim().length > 0)\n return analysisStripped\n\n // Path 3: response was nothing but an `<analysis>` block — extract\n // its content and use that as the summary. The model meant to give\n // us a summary; it just wrapped it in the wrong tag.\n const analysisMatch = text.match(/<analysis>([\\s\\S]*?)<\\/analysis>/)\n if (analysisMatch)\n return analysisMatch[1]\n\n // Path 4: no envelope of any shape — return verbatim.\n return text\n}\n\n/**\n * Convert turns into the wire-level `SessionMessage[]` shape. Drops\n * system turns (rare; they'd confuse a summary call), and inlines any\n * pre-existing `compact-summary` markers as plain text so the\n * provider's wire converter doesn't have to know about zidane's\n * internal block type.\n *\n * Inlining (instead of dropping) is intentional: a session already\n * compacted once still contains the prior summary as load-bearing\n * context. Surfacing it as `[Previous compaction summary]\\n…` lets the\n * new summarization integrate it instead of forgetting it.\n */\nfunction turnsToMessages(turns: readonly SessionTurn[]): SessionMessage[] {\n const out: SessionMessage[] = []\n for (const turn of turns) {\n if (turn.role === 'system')\n continue\n const content: SessionContentBlock[] = []\n for (const block of turn.content) {\n if (block.type === 'compact-summary') {\n content.push({\n type: 'text',\n text: `[Previous compaction summary]\\n${block.summary}`,\n })\n continue\n }\n // Strip `thinking` blocks. They're the model's internal reasoning,\n // signed by the producing provider (`signatureProducer: 'anthropic'`),\n // and only valid in a follow-up request when that same request has\n // extended thinking enabled. The compaction call deliberately runs\n // with `thinking: undefined` (summarization rarely benefits from\n // reasoning, and we don't want to pay the budget), so re-sending\n // thinking blocks puts Anthropic in an inconsistent state: it\n // accepts the request without 400-ing but silently returns an\n // empty `text` response. The user-visible symptom is \"Compaction\n // failed: provider returned no summary text\" on sessions where\n // any turn used extended thinking. Thinking is opaque-by-design;\n // dropping it for the summary call doesn't lose conversational\n // content (the model's visible text is in adjacent `text` blocks).\n if (block.type === 'thinking')\n continue\n content.push(block)\n }\n if (content.length === 0)\n continue\n out.push({ role: turn.role, content })\n }\n return out\n}\n\nfunction scopeToDirection(scope: CompactScope): 'full' | 'tail' | 'from' | 'up_to' {\n if (scope === 'full' || scope === 'tail')\n return scope\n return scope.kind\n}\n\nfunction anchorTurnFor(slice: CompactionSlice, scope: CompactScope): SessionTurn {\n // Caller-side invariant: `sliceForCompaction` already verified the anchor\n // exists when scope.kind is set. We re-locate it here so the preview is\n // built from the exact same turn the prompt builder will reference.\n if (typeof scope === 'string')\n throw new Error('anchorTurnFor: scope must be object form')\n if (scope.kind === 'from')\n return slice.toSummarize[0]!\n return slice.toSummarize[slice.toSummarize.length - 1]!\n}\n\nfunction bytesIn(turns: readonly SessionTurn[]): number {\n let total = 0\n for (const turn of turns) {\n for (const block of turn.content) {\n if (block.type === 'text')\n total += utf8ByteLength(block.text)\n else if (block.type === 'tool_result')\n total += toolOutputByteLength(block.output)\n else if (block.type === 'tool_call')\n total += utf8ByteLength(JSON.stringify(block.input))\n else if (block.type === 'thinking')\n total += utf8ByteLength(block.text)\n }\n }\n return total\n}\n\n// ---------------------------------------------------------------------------\n// Error classification\n// ---------------------------------------------------------------------------\n\n/**\n * Provider-agnostic predicate for the \"prompt is too long\" rejection.\n * Inspects error code, type, status, and message substring — every\n * provider names this case differently but the message is recognizable.\n */\nfunction isPromptTooLongError(err: unknown): boolean {\n if (!err || typeof err !== 'object')\n return false\n const e = err as Record<string, unknown>\n if (typeof e.code === 'string' && /prompt[_ ]too[_ ]long/i.test(e.code))\n return true\n if (typeof e.status === 'number' && (e.status === 413 || e.status === 400)) {\n const message = typeof e.message === 'string' ? e.message : ''\n if (/prompt[_ ]too[_ ]long|context[_ ]length|maximum[_ ]context|too many tokens/i.test(message))\n return true\n }\n if (typeof e.message === 'string'\n && /prompt[_ ]too[_ ]long|context[_ ]length[_ ]exceeded|context window/i.test(e.message)) {\n return true\n }\n // Anthropic SDK shape: `error.error.type === 'invalid_request_error'`\n // with a `prompt is too long` message.\n const nested = (e.error ?? {}) as Record<string, unknown>\n if (typeof nested.type === 'string' && nested.type === 'invalid_request_error') {\n const message = typeof nested.message === 'string' ? nested.message : ''\n if (/prompt[_ ]too[_ ]long|too long|context window/i.test(message))\n return true\n }\n return false\n}\n\n/**\n * Transient network / 5xx errors worth retrying once or twice.\n */\nfunction isTransientError(err: unknown): boolean {\n if (!err || typeof err !== 'object')\n return false\n const e = err as Record<string, unknown>\n if (typeof e.status === 'number' && e.status >= 500 && e.status < 600)\n return true\n if (typeof e.code === 'string' && /ECONNRESET|ETIMEDOUT|ENETUNREACH|EAI_AGAIN|fetch failed/i.test(e.code))\n return true\n if (typeof e.message === 'string'\n && /socket hang up|fetch failed|network error|terminated|ECONNRESET|read ETIMEDOUT/i.test(e.message)) {\n return true\n }\n return false\n}\n\nfunction isAbortError(err: unknown, signal: AbortSignal | undefined): boolean {\n if (signal?.aborted)\n return true\n if (!err || typeof err !== 'object')\n return false\n const e = err as Record<string, unknown>\n if (e.name === 'AbortError')\n return true\n if (typeof e.message === 'string' && /aborted/i.test(e.message))\n return true\n return false\n}\n","/**\n * Post-compact restoration — re-inject load-bearing working state as\n * synthetic tool-call/tool-result pairs after a {@link compactConversation}\n * marker lands in a session.\n *\n * Without this step, the model has a narrative summary but loses direct\n * access to the files it was actively editing and the skills it was\n * following. Restoration re-attaches the top-N recently-read files and\n * active skills so the next turn starts with full working context — no\n * forced re-reads, no degraded continuation.\n *\n * Design — synthetic tool_call/tool_result pairs:\n *\n * Two turns are appended after the compaction marker:\n *\n * [marker] ← from `summaryToTurn(result)`\n * [assistant, tool_calls × N] ← synthetic, one tool_call per item\n * [user, tool_results × N] ← synthetic, matching results\n * [new prompt] ← user's next message\n *\n * The synthetic turns look identical to what the agent would produce if it\n * had actually run `read_file` / `skills_use` — by design, because at the\n * moment of compaction those operations had just happened with that data.\n *\n * Persisted blocks use **canonical** tool names (e.g. `read_file`). The\n * agent loop's `rewriteMessagesToWire` translates them to whatever alias\n * the host configured before they reach the provider, exactly as it does\n * for real calls. Restoration therefore \"just works\" through aliasing —\n * the helper does not need to consult the alias map.\n *\n * Budgets — mirror Claude Code's `services/compact/compact.ts:122-130`:\n *\n * - 50_000 tokens total file budget, 5_000 per file, max 5 files\n * - 25_000 tokens total skill budget, 5_000 per skill (no count cap)\n *\n * Tokens are estimated at 4 chars/token — same heuristic Claude Code uses\n * for budget arithmetic. Hosts can override every limit.\n *\n * Failure modes:\n *\n * - File read fails (deleted, permissions) → skip silently. Other items\n * still proceed.\n * - No `execution` / `handle` passed → file restoration is a no-op;\n * skill restoration still works (skills carry their content inline).\n * - Empty `recentFiles` AND empty `activeSkills` → returns `{ turns: [] }`\n * so the caller's `appendTurns([summary, ...attachments])` is a no-op\n * on the attachments.\n */\n\nimport type { ExecutionContext, ExecutionHandle } from '../contexts'\nimport type { Session } from '../session'\nimport type { ActiveSkill } from '../skills/activation'\nimport type { SessionContentBlock, SessionTurn } from '../types'\nimport { getReadState } from '../tools/read-state'\nimport { BYTES_PER_TOKEN, estimateTokens, utf8ByteLength } from './utils'\n\n// ---------------------------------------------------------------------------\n// Defaults — match Claude Code's published constants for parity.\n// ---------------------------------------------------------------------------\n\nconst DEFAULT_FILE_TOKEN_BUDGET = 50_000\nconst DEFAULT_FILE_TOKEN_PER_FILE_CAP = 5_000\nconst DEFAULT_MAX_FILES_TO_RESTORE = 5\n\nconst DEFAULT_SKILL_TOKEN_BUDGET = 25_000\nconst DEFAULT_SKILL_TOKEN_PER_SKILL_CAP = 5_000\n\n/** Default canonical tool names — `rewriteMessagesToWire` handles aliasing. */\nconst DEFAULT_READ_FILE_TOOL_NAME = 'read_file'\nconst DEFAULT_SKILLS_USE_TOOL_NAME = 'skills_use'\n\n// ---------------------------------------------------------------------------\n// Public types\n// ---------------------------------------------------------------------------\n\n/** One file selected for restoration, with its last-read timestamp for ranking. */\nexport interface RecentFile {\n /** Path relative to the execution context's `handle.cwd`. */\n path: string\n /** Wall-clock when the file was last read, in ms. Drives recency ranking. */\n mtimeMs: number\n}\n\nexport interface PostCompactRestoreOptions {\n // ---- What to restore ---------------------------------------------------\n\n /**\n * Files to consider for restoration, ranked by recency. Typically derived\n * via {@link selectFilesFromReadState} from `getReadState(session)`.\n * The helper takes the top {@link PostCompactRestoreOptions.maxFilesToRestore}\n * by `mtimeMs` descending, fetches their current content, and synthesizes\n * `read_file` tool_call/tool_result pairs.\n */\n recentFiles?: readonly RecentFile[]\n\n /**\n * Active skills to re-inject. Typically `agent.activeSkills`. Each entry\n * yields a synthetic `skills_use` tool_call/tool_result pair carrying the\n * skill's instructions (possibly truncated to the per-skill budget).\n */\n activeSkills?: readonly ActiveSkill[]\n\n // ---- I/O ---------------------------------------------------------------\n\n /**\n * Execution context used to fetch file content. **Required for file\n * restoration.** Skills come pre-loaded (instructions live on the\n * `SkillConfig`), so they don't need the context.\n */\n execution?: ExecutionContext\n handle?: ExecutionHandle\n\n // ---- Tool naming -------------------------------------------------------\n\n /**\n * Canonical name of the file-read tool. Defaults to `read_file`. Aliases\n * (e.g. `Read`) are NOT specified here — the agent loop's\n * `rewriteMessagesToWire` handles wire conversion automatically.\n *\n * Override only when a host registered file-read under a different\n * canonical name (not just an alias).\n */\n readFileToolName?: string\n /**\n * Canonical name of the skill-activation tool. Defaults to `skills_use`.\n * Same aliasing semantics as `readFileToolName`.\n */\n skillsUseToolName?: string\n\n // ---- Budgets -----------------------------------------------------------\n\n /** Total token budget for file restoration. Default: 50_000. */\n fileTokenBudget?: number\n /** Per-file token cap. Default: 5_000. */\n fileTokenPerFileCap?: number\n /** Maximum file count. Default: 5. */\n maxFilesToRestore?: number\n\n /** Total token budget for skill restoration. Default: 25_000. */\n skillTokenBudget?: number\n /** Per-skill token cap. Default: 5_000. */\n skillTokenPerSkillCap?: number\n\n // ---- Filtering ---------------------------------------------------------\n\n /**\n * Paths to skip — typically the file paths already covered by the\n * compaction result's `preservedTurns`. Avoids double-injection when the\n * recent reads sit in the preserved tail.\n */\n excludePaths?: readonly string[]\n\n // ---- Synthesis ---------------------------------------------------------\n\n /**\n * Optional `runId` to tag the synthetic turns with — useful for hosts\n * that want the restoration turns to roll up under the same run as the\n * compaction marker. Defaults to undefined (orphan turns).\n */\n runId?: string\n}\n\n/**\n * Envelope returned by {@link buildPostCompactAttachments}. The caller\n * spreads `turns` into `session.appendTurns([summaryTurn, ...turns])`.\n * Count fields drive UI banners (\"restored 5 files + 2 skills\").\n */\nexport interface PostCompactAttachments {\n /**\n * Two synthetic turns when at least one item was restored, otherwise\n * an empty array. The pair is `[assistant_with_tool_calls,\n * user_with_tool_results]` — adjacent and well-formed for every\n * provider's `tool_use ↔ tool_result` invariant.\n */\n turns: readonly SessionTurn[]\n /** Count of files actually restored (post-budget). */\n restoredFiles: number\n /** Count of skills actually restored (post-budget). */\n restoredSkills: number\n /** Rough total token cost of the restoration payload (sum of all content). */\n estimatedTokens: number\n}\n\n// ---------------------------------------------------------------------------\n// Pure helpers — exported for tests + caller convenience\n// ---------------------------------------------------------------------------\n\n/**\n * Convert a raw read-state map into a deduped, path-ranked list ready for\n * restoration. Multiple entries for the same path (different\n * `(offset, limit, maxBytes)` slices) collapse to one — keeping the most\n * recent `mtimeMs`.\n *\n * Filters out entries whose key doesn't share the given `cwd` prefix:\n * those came from a different execution context and can't be read back\n * through this agent's handle.\n *\n * Pure. Most callers want {@link selectFilesFromSession}, which wraps\n * `getReadState(session)` + this function in one call so the host\n * doesn't have to reach into the tools layer.\n */\nexport function selectFilesFromReadState(\n readState: ReadonlyMap<string, { mtimeMs: number }>,\n cwd: string,\n): RecentFile[] {\n const prefix = `${cwd}::`\n const byPath = new Map<string, number>()\n for (const [key, entry] of readState) {\n if (!key.startsWith(prefix))\n continue\n const path = key.slice(prefix.length)\n if (path.length === 0)\n continue\n const prior = byPath.get(path) ?? -Infinity\n if (entry.mtimeMs > prior)\n byPath.set(path, entry.mtimeMs)\n }\n return Array.from(byPath, ([path, mtimeMs]) => ({ path, mtimeMs }))\n .sort((a, b) => b.mtimeMs - a.mtimeMs)\n}\n\n/**\n * Session-aware convenience: extract recently-read files directly from\n * a {@link Session} via its per-session read-state map.\n *\n * Hosts (TUI / SDK consumers) typically have a `Session` and a `cwd`\n * (the active agent's `handle.cwd`) on hand — this wrapper saves them\n * from reaching into `src/tools/read-state.ts` directly. Returns an\n * empty list when no read state has been recorded yet (fresh session,\n * or `behavior.dedupReads === false`).\n *\n * Equivalent to:\n *\n * ```ts\n * const state = getReadState(session)\n * return state ? selectFilesFromReadState(state, cwd) : []\n * ```\n */\nexport function selectFilesFromSession(\n session: Session,\n cwd: string,\n): RecentFile[] {\n const state = getReadState(session)\n return state ? selectFilesFromReadState(state, cwd) : []\n}\n\n/**\n * Pick the top `maxFiles` from `files` (descending by `mtimeMs`),\n * dropping any whose path appears in `excludePaths`.\n *\n * Stable for equal mtimes — files with the same timestamp retain their\n * input order. Pure.\n */\nexport function selectRecentFiles(\n files: readonly RecentFile[],\n opts: { maxFiles: number, excludePaths?: readonly string[] },\n): RecentFile[] {\n const excluded = new Set(opts.excludePaths ?? [])\n const filtered = files.filter(f => !excluded.has(f.path))\n // Defensive sort — caller may pass an already-sorted list, but doing it\n // here keeps the function total regardless of input order.\n const sorted = filtered.slice().sort((a, b) => b.mtimeMs - a.mtimeMs)\n return sorted.slice(0, Math.max(0, opts.maxFiles))\n}\n\n// ---------------------------------------------------------------------------\n// Internal — content formatting + truncation\n// ---------------------------------------------------------------------------\n\ninterface FormattedFile {\n /** Tool-result content (line-numbered, with optional truncation footer). */\n body: string\n /** Whether the content was truncated to fit the per-file budget. */\n truncated: boolean\n /** Estimated token cost of `body` (post-truncation). */\n estimatedTokens: number\n}\n\ninterface FormattedSkill {\n body: string\n truncated: boolean\n estimatedTokens: number\n}\n\n/**\n * Format a file's contents to match `read_file`'s output shape — 1-indexed\n * line numbers separated by tabs, identical to what the model has seen\n * from real `read_file` calls. Applies the per-file token cap by truncating\n * at the nearest line boundary; appends a footer pointing at the next\n * offset so the model can re-read the rest if needed.\n */\nfunction formatFileForRestoration(\n content: string,\n perFileTokenCap: number,\n): FormattedFile {\n const totalBytes = utf8ByteLength(content)\n const allLines = content.split('\\n')\n const totalLines = allLines.length\n\n // Build the line-numbered body first; tokens are counted on the\n // numbered form because that's what reaches the provider.\n const numbered: string[] = []\n let runningChars = 0\n const charCap = Math.max(1, perFileTokenCap) * BYTES_PER_TOKEN\n let truncatedAt = -1\n let midLineCut = false\n\n for (let i = 0; i < allLines.length; i++) {\n const numberedLine = `${i + 1}\\t${allLines[i]}`\n const lineCharCost = numberedLine.length + (i < allLines.length - 1 ? 1 : 0) // +1 for the joining '\\n' on every line but the last\n if (runningChars + lineCharCost > charCap) {\n if (numbered.length === 0) {\n // The first line on its own already overflows (typical for\n // minified JS, CSV-on-one-line, etc.). Cut it at the char\n // boundary so we return SOMETHING useful — the head of the\n // file — instead of either failing or admitting a huge first\n // line that blows the budget. Mid-line cuts mean we can't\n // suggest a precise re-read offset; the footer says so.\n const cutTo = Math.max(1, charCap - `${i + 1}\\t`.length)\n numbered.push(`${i + 1}\\t${allLines[i].slice(0, cutTo)}`)\n midLineCut = true\n }\n truncatedAt = i\n break\n }\n numbered.push(numberedLine)\n runningChars += lineCharCost\n }\n\n const body = numbered.join('\\n')\n if (truncatedAt < 0)\n return { body, truncated: false, estimatedTokens: estimateTokens(body) }\n\n const lineLabel = midLineCut\n ? `line ${truncatedAt + 1} (mid-line)`\n : `line ${truncatedAt}`\n const offsetHint = midLineCut\n ? `mid-line cut prevents a precise offset — re-read with offset=${truncatedAt + 1} and a larger maxBytes`\n : `re-read with offset=${truncatedAt + 1} to continue`\n const footer = `\\n\\n…truncated at ${lineLabel} (post-compact restoration cap: ${perFileTokenCap} tokens). File has ${totalLines} lines, ${totalBytes} bytes total — ${offsetHint}.`\n const truncated = body + footer\n return { body: truncated, truncated: true, estimatedTokens: estimateTokens(truncated) }\n}\n\n/**\n * Format a skill's instructions for restoration. Skills are plain markdown\n * — no line numbering, no wrapping XML envelope (the model already\n * understands the format from real `skills_use` calls). Truncation cuts\n * at a line boundary when possible; appends a marker so the model knows\n * the body is incomplete.\n */\nfunction formatSkillForRestoration(\n instructions: string,\n perSkillTokenCap: number,\n): FormattedSkill {\n const charCap = Math.max(1, perSkillTokenCap) * BYTES_PER_TOKEN\n if (instructions.length <= charCap) {\n return { body: instructions, truncated: false, estimatedTokens: estimateTokens(instructions) }\n }\n // Truncate at the nearest line boundary at or before charCap so the\n // body stays markdown-parseable.\n const head = instructions.slice(0, charCap)\n const lastNewline = head.lastIndexOf('\\n')\n const cutoff = lastNewline > 0 ? lastNewline : charCap\n const truncatedBody\n = `${instructions.slice(0, cutoff).trimEnd()}\\n\\n…[truncated post-compact at ${perSkillTokenCap} tokens; full skill body lives at the skill's location]`\n return {\n body: truncatedBody,\n truncated: true,\n estimatedTokens: estimateTokens(truncatedBody),\n }\n}\n\n// ---------------------------------------------------------------------------\n// Public runner\n// ---------------------------------------------------------------------------\n\n/**\n * Build the synthetic turns to append after a `compact-summary` marker.\n *\n * Returns a `PostCompactAttachments` envelope; the caller is responsible\n * for `session.appendTurns([summaryTurn, ...result.turns])`. The two\n * synthetic turns are always emitted together (or both omitted when\n * nothing was restored) so the `tool_use ↔ tool_result` adjacency\n * invariant holds regardless of caller code path.\n *\n * Failure isolation: a single file's read failure never blocks the rest\n * of restoration — that file is skipped silently and processing\n * continues. The returned `restoredFiles` count reflects what actually\n * landed in the synthesized turns.\n */\nexport async function buildPostCompactAttachments(\n opts: PostCompactRestoreOptions,\n): Promise<PostCompactAttachments> {\n const fileTokenBudget = opts.fileTokenBudget ?? DEFAULT_FILE_TOKEN_BUDGET\n const fileTokenPerFileCap = opts.fileTokenPerFileCap ?? DEFAULT_FILE_TOKEN_PER_FILE_CAP\n const maxFilesToRestore = opts.maxFilesToRestore ?? DEFAULT_MAX_FILES_TO_RESTORE\n const skillTokenBudget = opts.skillTokenBudget ?? DEFAULT_SKILL_TOKEN_BUDGET\n const skillTokenPerSkillCap = opts.skillTokenPerSkillCap ?? DEFAULT_SKILL_TOKEN_PER_SKILL_CAP\n const readFileToolName = opts.readFileToolName ?? DEFAULT_READ_FILE_TOOL_NAME\n const skillsUseToolName = opts.skillsUseToolName ?? DEFAULT_SKILLS_USE_TOOL_NAME\n\n // ---- Phase 1: pick candidates --------------------------------------\n //\n // Tool-availability is governed at the input layer: callers control\n // which categories are restored by what they pass. To skip files,\n // pass empty `recentFiles` (or none). To skip skills, pass empty\n // `activeSkills` (or none). The runner trusts the caller to know\n // what tools the next agent run will have access to — auto-injected\n // tools (`skills_use`, MCP, interaction tools) aren't introspectable\n // from a profile config and constructing a tool-allowlist here would\n // be incomplete by design.\n const candidateFiles = opts.recentFiles && opts.recentFiles.length > 0\n ? selectRecentFiles(opts.recentFiles, {\n maxFiles: maxFilesToRestore,\n ...(opts.excludePaths ? { excludePaths: opts.excludePaths } : {}),\n })\n : []\n const candidateSkills = opts.activeSkills ?? []\n\n // ---- Phase 2: fetch + format files (with per-file + group budget) --\n const fileCalls: Array<{ callId: string, path: string, body: string, estimatedTokens: number }> = []\n let fileBudgetUsed = 0\n if (candidateFiles.length > 0 && opts.execution && opts.handle) {\n for (let i = 0; i < candidateFiles.length; i++) {\n const file = candidateFiles[i]\n let content: string\n try {\n content = await opts.execution.readFile(opts.handle, file.path)\n }\n catch {\n // File deleted / unreadable since the last read — skip silently.\n continue\n }\n const formatted = formatFileForRestoration(content, fileTokenPerFileCap)\n if (fileBudgetUsed + formatted.estimatedTokens > fileTokenBudget) {\n // Adding this file would blow the group budget. Stop here — the\n // remaining files are older anyway (candidates are pre-sorted by\n // recency descending).\n break\n }\n fileBudgetUsed += formatted.estimatedTokens\n fileCalls.push({\n callId: `compact-restore-file-${i}`,\n path: file.path,\n body: formatted.body,\n estimatedTokens: formatted.estimatedTokens,\n })\n }\n }\n\n // ---- Phase 3: format skills (no I/O — instructions are inline) -----\n const skillCalls: Array<{ callId: string, name: string, body: string, estimatedTokens: number }> = []\n let skillBudgetUsed = 0\n for (let i = 0; i < candidateSkills.length; i++) {\n const active = candidateSkills[i]\n const instructions = active.skill.instructions ?? ''\n if (instructions.trim().length === 0)\n continue\n const formatted = formatSkillForRestoration(instructions, skillTokenPerSkillCap)\n if (skillBudgetUsed + formatted.estimatedTokens > skillTokenBudget)\n break\n skillBudgetUsed += formatted.estimatedTokens\n skillCalls.push({\n callId: `compact-restore-skill-${i}`,\n name: active.skill.name,\n body: formatted.body,\n estimatedTokens: formatted.estimatedTokens,\n })\n }\n\n // ---- Phase 4: synthesize turns -------------------------------------\n if (fileCalls.length === 0 && skillCalls.length === 0) {\n return { turns: [], restoredFiles: 0, restoredSkills: 0, estimatedTokens: 0 }\n }\n\n const assistantBlocks: SessionContentBlock[] = []\n const userBlocks: SessionContentBlock[] = []\n\n for (const fc of fileCalls) {\n assistantBlocks.push({\n type: 'tool_call',\n id: fc.callId,\n name: readFileToolName,\n input: { path: fc.path },\n })\n userBlocks.push({\n type: 'tool_result',\n callId: fc.callId,\n output: fc.body,\n })\n }\n for (const sc of skillCalls) {\n assistantBlocks.push({\n type: 'tool_call',\n id: sc.callId,\n name: skillsUseToolName,\n input: { name: sc.name },\n })\n userBlocks.push({\n type: 'tool_result',\n callId: sc.callId,\n output: sc.body,\n })\n }\n\n const now = Date.now()\n const tag = opts.runId ? { runId: opts.runId } : {}\n const turns: SessionTurn[] = [\n {\n id: crypto.randomUUID(),\n role: 'assistant',\n content: assistantBlocks,\n createdAt: now,\n ...tag,\n },\n {\n id: crypto.randomUUID(),\n role: 'user',\n content: userBlocks,\n // +1 ms so the user turn sorts after the assistant turn under\n // tie-breaking sorts that walk by `createdAt`.\n createdAt: now + 1,\n ...tag,\n },\n ]\n\n return {\n turns,\n restoredFiles: fileCalls.length,\n restoredSkills: skillCalls.length,\n estimatedTokens: fileBudgetUsed + skillBudgetUsed,\n }\n}\n","/**\n * Local loopback HTTP callback for OAuth 2.0 authorization code flows.\n *\n * Stands up a one-shot server on `127.0.0.1:<random>` that captures the\n * `?code=...` redirect from a browser-driven OAuth flow and resolves a\n * promise with the code. Used as the `redirectUrl` half of the MCP SDK's\n * `OAuthClientProvider` (the persistence half lives separately).\n *\n * Design:\n * - Loopback-only (`127.0.0.1`) — the OAuth spec treats `http://127.0.0.1:<port>`\n * as a public-client redirect URI per RFC 8252 §7.3. Browsers do NOT block it,\n * and Anthropic / OpenAI / Linear / GitHub all accept it.\n * - Random port (`port = 0`) — the OS picks an unused one. We read the actual\n * port back from `server.address()` after `listen()`.\n * - Single-shot — the first GET to `path` with a `code` (or `error`) wins;\n * subsequent requests get 404. The server keeps listening (in case the user\n * hits \"back\" and re-authorizes), so callers must `close()` once they have\n * the code or have given up.\n * - Abort-aware — wiring an external `AbortSignal` rejects the promise and\n * closes the server immediately. Required for the TUI's \"esc cancels login\"\n * UX.\n * - No HTML framework — a single inline `<html>` string keeps this isolated\n * from any UI dependency.\n */\n\nimport type { AddressInfo } from 'node:net'\nimport { createServer } from 'node:http'\n\n/**\n * Result of a successful callback. `state` is forwarded verbatim from the\n * query string — callers verify it against their pre-flight value to defend\n * against CSRF (the MCP SDK does this internally when it controls `state`).\n */\nexport interface OAuthCallbackResult {\n code: string\n state?: string\n}\n\nexport interface OAuthCallbackHandle {\n /**\n * Full URI to register with the authorization server, e.g.\n * `http://127.0.0.1:51823/callback`. Stable for the lifetime of the\n * handle.\n */\n redirectUri: string\n /**\n * Resolves with `{ code, state }` on a successful callback. Rejects with:\n * - The OAuth-spec `error` field (`access_denied`, `server_error`, ...)\n * when the authorization server redirects with `?error=...`.\n * - `'OAuth callback aborted'` when the external `AbortSignal` fires.\n * - `'OAuth callback server closed'` when `close()` is called before any\n * callback arrives.\n *\n * Single-shot — only the first matching request resolves the promise.\n */\n promise: Promise<OAuthCallbackResult>\n /**\n * Idempotent shutdown. Safe to call from a `finally` block whether the\n * flow succeeded, failed, or was aborted. Resolves once the server stops\n * accepting connections.\n */\n close: () => Promise<void>\n}\n\nexport interface OAuthCallbackOptions {\n /** Cancels the flow — rejects `promise` and closes the server. */\n signal?: AbortSignal\n /**\n * Path component the authorization server should redirect to. Defaults\n * to `/callback`. Useful when matching a pre-registered URI that uses a\n * different path.\n */\n path?: string\n /**\n * Override the loopback host. Defaults to `127.0.0.1`. Don't bind to\n * `0.0.0.0` here — the OAuth code is a one-time secret and the server\n * would otherwise accept it from any host on the LAN.\n */\n host?: string\n /**\n * Override the port. Defaults to `0` (OS-assigned). Pin to a fixed port\n * only when the authorization server requires a pre-registered redirect\n * URI; the random-port path is preferred so concurrent flows don't clash.\n */\n port?: number\n}\n\nconst DEFAULT_PATH = '/callback'\nconst DEFAULT_HOST = '127.0.0.1'\n\nconst SUCCESS_HTML = `<!doctype html>\n<html><head><meta charset=\"utf-8\"><title>Logged in</title>\n<style>body{font:14px/1.5 -apple-system,system-ui,sans-serif;margin:6rem auto;max-width:28rem;text-align:center;color:#1d1d1f}.ok{color:#1f8a4c;font-weight:600}</style>\n</head><body>\n<p class=\"ok\">Logged in.</p>\n<p>You can close this tab and return to the terminal.</p>\n</body></html>`\n\nfunction errorHtml(message: string): string {\n // No interpolation into HTML attributes — message goes only inside <p> text\n // where the SAMEORIGIN browser context is rendering. Escape angle brackets\n // and ampersands defensively anyway: a malicious authorization server could\n // theoretically craft an `error_description` containing markup, and we\n // don't want stored XSS even in a one-shot loopback page.\n const escaped = message\n .replace(/&/g, '&')\n .replace(/</g, '<')\n .replace(/>/g, '>')\n return `<!doctype html>\n<html><head><meta charset=\"utf-8\"><title>Login failed</title>\n<style>body{font:14px/1.5 -apple-system,system-ui,sans-serif;margin:6rem auto;max-width:28rem;text-align:center;color:#1d1d1f}.err{color:#c43c2c;font-weight:600}</style>\n</head><body>\n<p class=\"err\">Login failed.</p>\n<p>${escaped}</p>\n<p>You can close this tab and return to the terminal.</p>\n</body></html>`\n}\n\n/**\n * Start a one-shot OAuth callback server. The returned handle's `redirectUri`\n * should be passed to the authorization server as the `redirect_uri` query\n * parameter; `promise` resolves once the user finishes the browser flow.\n *\n * Always `await handle.close()` in a `finally` block — even on success, the\n * server stays open until told to shut down (so it can serve the\n * \"you can close this tab\" page).\n */\nexport async function startOAuthCallback(\n opts: OAuthCallbackOptions = {},\n): Promise<OAuthCallbackHandle> {\n const path = opts.path ?? DEFAULT_PATH\n const host = opts.host ?? DEFAULT_HOST\n const port = opts.port ?? 0\n\n if (!path.startsWith('/'))\n throw new Error(`OAuth callback path must start with \"/\" (got: ${JSON.stringify(path)})`)\n\n if (opts.signal?.aborted)\n throw new Error('OAuth callback aborted')\n\n let resolveResult: (value: OAuthCallbackResult) => void\n let rejectResult: (error: Error) => void\n const promise = new Promise<OAuthCallbackResult>((resolve, reject) => {\n resolveResult = resolve\n rejectResult = reject\n })\n // Attach a default no-op catch so a rejection without a consumer doesn't\n // raise an unhandled-rejection warning. Callers that DO `await promise`\n // still receive the rejection — Promises fan out to all listeners.\n promise.catch(() => {})\n\n let settled = false\n const resolveOnce = (value: OAuthCallbackResult) => {\n if (settled)\n return\n settled = true\n resolveResult(value)\n }\n const rejectOnce = (error: Error) => {\n if (settled)\n return\n settled = true\n rejectResult(error)\n }\n\n const server = createServer((req, res) => {\n const url = new URL(req.url ?? '/', `http://${host}`)\n if (url.pathname !== path) {\n res.writeHead(404, { 'content-type': 'text/plain' })\n res.end('Not Found')\n return\n }\n\n // `no-store` prevents the browser from replaying the page (with the\n // now-spent code in its URL bar) when the user hits back, refreshes,\n // or restores the tab from history. The code is a one-time secret.\n const htmlHeaders = {\n 'content-type': 'text/html; charset=utf-8',\n 'cache-control': 'no-store',\n }\n\n const error = url.searchParams.get('error')\n if (error) {\n const desc = url.searchParams.get('error_description') ?? error\n res.writeHead(400, htmlHeaders)\n res.end(errorHtml(desc))\n rejectOnce(new Error(`OAuth authorization failed: ${desc}`))\n return\n }\n\n const code = url.searchParams.get('code')\n if (!code) {\n // No code, no error — likely a stray probe. Serve a minimal page,\n // don't resolve. The browser will eventually arrive with the real\n // redirect, or the caller will time out via signal.\n res.writeHead(400, { 'content-type': 'text/plain' })\n res.end('Missing \"code\" query parameter.')\n return\n }\n\n const state = url.searchParams.get('state') ?? undefined\n res.writeHead(200, htmlHeaders)\n res.end(SUCCESS_HTML)\n resolveOnce({ code, state })\n })\n\n // Surface socket errors that arrive before the request handler — e.g. EADDRINUSE\n // on a pinned port, or an early TLS-style probe that the parser rejects.\n server.on('error', (err) => {\n rejectOnce(err instanceof Error ? err : new Error(String(err)))\n })\n\n await new Promise<void>((resolve, reject) => {\n server.once('error', reject)\n server.listen(port, host, () => {\n server.off('error', reject)\n resolve()\n })\n })\n\n const addr = server.address() as AddressInfo | null\n if (!addr || typeof addr === 'string') {\n server.close()\n throw new Error('OAuth callback server did not bind to a TCP port')\n }\n\n // `closeServer` and `onAbort` reference each other — pre-declare both so\n // either ordering reads cleanly and the lint doesn't trip on the cycle.\n let closing: Promise<void> | undefined\n let closeServer: () => Promise<void>\n const onAbort = () => {\n rejectOnce(new Error('OAuth callback aborted'))\n void closeServer()\n }\n closeServer = (): Promise<void> => {\n if (closing)\n return closing\n closing = new Promise<void>((resolve) => {\n // `close()` on a Node http server waits for in-flight requests to drain.\n // The flow has already settled (or is being aborted), so we also call\n // `closeAllConnections()` to drop keep-alive sockets immediately — a\n // browser keeps the connection open after the success page, which would\n // otherwise hang the close for the keep-alive timeout (~5s).\n const anyServer = server as { closeAllConnections?: () => void }\n anyServer.closeAllConnections?.()\n server.close(() => {\n opts.signal?.removeEventListener('abort', onAbort)\n // If neither a callback nor an abort happened before close(), reject\n // pending awaits so callers don't hang. Tag the rejection as\n // 'aborted' when the signal already fired — Bun fires the abort\n // listener via a microtask, so server.close() may resolve first\n // even though semantically the abort came first.\n if (opts.signal?.aborted)\n rejectOnce(new Error('OAuth callback aborted'))\n else\n rejectOnce(new Error('OAuth callback server closed'))\n resolve()\n })\n })\n return closing\n }\n opts.signal?.addEventListener('abort', onAbort, { once: true })\n\n return {\n redirectUri: `http://${host}:${addr.port}${path}`,\n promise,\n close: closeServer,\n }\n}\n","/**\n * Interactive OAuth login orchestrator for one MCP server.\n *\n * Composes the three pieces shipped separately:\n * - `startOAuthCallback` (`./oauth-callback`) — local loopback HTTP\n * listener that catches the `?code=...` redirect.\n * - `McpOAuthProvider` (`./oauth-provider`) — SDK adapter that persists\n * tokens / client info via an injected `McpCredentialStore`.\n * - The MCP SDK's transport `authProvider` + `finishAuth` plumbing —\n * `client.connect()` triggers the SDK to call `redirectToAuthorization`\n * (which opens the browser); we wait for the loopback callback to\n * deliver the code, hand it to `transport.finishAuth`, and reconnect.\n *\n * Returns once tokens are persisted. Caller decides whether to immediately\n * reconnect the server (re-run `connectMcpServers` / `agent.warmup()`) or\n * defer to the next session activation. The returned `tools` array is\n * provided as a convenience — callers that want the connection live in\n * this call rather than rebuilding can wire them in directly.\n */\n\nimport type { OAuthClientProvider } from '@modelcontextprotocol/sdk/client/auth.js'\nimport type { Hookable } from 'hookable'\nimport type { AgentHooks } from '../agent'\nimport type { McpServerConfig } from '../types'\nimport type { OAuthCallbackHandle } from './oauth-callback'\nimport type { McpCredentialStore } from './oauth-provider'\nimport { startOAuthCallback } from './oauth-callback'\nimport { McpOAuthProvider } from './oauth-provider'\nimport { sseToJsonFetchIfNeeded } from './sse-to-json-fetch'\nimport { createTolerantClient } from './tolerant-client'\n\nexport interface LoginMcpServerOptions {\n /** Persistence — same store the bootstrap path reads from. */\n store: McpCredentialStore\n /**\n * Invoked with the authorization URL once it's ready. Hosts typically\n * (a) emit `mcp:auth:url` for the TUI, and (b) call `tryOpenBrowser`.\n * The URL is identical to the one passed to the `mcp:auth:url` hook\n * fired automatically — this callback is a synchronous hook for callers\n * that don't want to wire the agent hook machinery.\n */\n onAuthorizationUrl?: (url: URL) => void | Promise<void>\n /** Cancels the flow (esc / close modal / SIGINT). */\n signal?: AbortSignal\n /** Agent hooks. The flow emits `mcp:auth:url`/`success`/`error` when wired. */\n hooks?: Hookable<AgentHooks>\n /** Override `client_name` shown on consent screens. Default: 'zidane'. */\n clientName?: string\n /** Override the requested OAuth scope. */\n scope?: string\n /**\n * Override the loopback callback path. Default: `/callback`. Useful only\n * for servers that pinned a different path during registration.\n */\n callbackPath?: string\n /**\n * Maximum time to wait for the user to complete the browser flow, in ms.\n * The user can also cancel via `signal`. Default: 5 minutes.\n */\n timeoutMs?: number\n}\n\nexport interface LoginMcpServerResult {\n /** Stored OAuth tokens after a successful exchange. */\n tokens: NonNullable<ReturnType<McpOAuthProvider['tokens']>>\n /**\n * Upstream tool descriptors discovered after re-connecting with the new\n * tokens. Already filtered by the server's `enabledTools` / `disabledTools`\n * is NOT applied here — that's a bootstrap concern. Hosts that want filtering\n * should pass the result through `connectMcpServers` rebuild on the next\n * session activation rather than reusing this list verbatim.\n */\n tools: Array<{ name: string, description?: string | null, inputSchema?: unknown }>\n}\n\nconst DEFAULT_LOGIN_TIMEOUT_MS = 5 * 60_000\n\n/**\n * Run the full interactive OAuth flow for `config`. Only supports `sse` and\n * `streamable-http` transports — `stdio` MCP servers don't speak OAuth.\n *\n * Throws on:\n * - Wrong transport.\n * - Abort signal.\n * - Browser-side error (user denied, server rejected, etc.).\n * - Code exchange failure.\n * - Post-exchange connect failure.\n *\n * Always closes the loopback callback server before returning, success or\n * failure.\n */\nexport async function loginMcpServer(\n config: McpServerConfig,\n options: LoginMcpServerOptions,\n): Promise<LoginMcpServerResult> {\n if (config.transport !== 'sse' && config.transport !== 'streamable-http')\n throw new Error(`MCP OAuth: cannot login for transport \"${config.transport}\" — only sse / streamable-http are supported`)\n if (!config.url)\n throw new Error(`MCP OAuth: server \"${config.name}\" is missing a url`)\n\n const hooks = options.hooks\n\n let callback: OAuthCallbackHandle | undefined\n try {\n callback = await startOAuthCallback({\n signal: options.signal,\n path: options.callbackPath,\n })\n\n const handle = callback\n const provider = new McpOAuthProvider({\n name: config.name,\n store: options.store,\n redirectUri: handle.redirectUri,\n clientName: options.clientName,\n scope: options.scope,\n onAuthorizationUrl: async (url) => {\n await hooks?.callHook('mcp:auth:url', { name: config.name, url: url.toString() })\n await options.onAuthorizationUrl?.(url)\n },\n })\n\n const transport = await createInteractiveTransport(config, provider)\n const client = await createTolerantClient({ name: 'zidane', version: '1.0.0' })\n\n // First connect → SDK has no tokens, so it calls\n // `provider.redirectToAuthorization(url)` (which routes to our\n // `onAuthorizationUrl`), then throws `UnauthorizedError`. This is the\n // expected path — we catch and wait on the loopback for the code.\n let needsAuth = false\n try {\n await client.connect(transport)\n }\n catch (err) {\n if (!isUnauthorizedError(err)) {\n await client.close().catch(() => {})\n throw err\n }\n needsAuth = true\n }\n\n if (needsAuth) {\n const { code } = await raceLoginCallback(handle, options.timeoutMs ?? DEFAULT_LOGIN_TIMEOUT_MS, options.signal)\n // SDK exchanges code → tokens; provider.saveTokens persists them.\n await (transport as { finishAuth: (code: string) => Promise<void> }).finishAuth(code)\n // Reconnect with a FRESH transport. The previous one is already in\n // the \"started\" state from the failed connect — reusing it throws\n // \"StreamableHTTPClientTransport already started!\" inside\n // `transport.start()`. The SDK's canonical example\n // (`simpleOAuthClient.js`) uses the same pattern: recurse with a\n // new transport, reuse the client. Close the stale transport\n // first so its abort controllers + sockets don't leak.\n await transport.close().catch(() => {})\n const freshTransport = await createInteractiveTransport(config, provider)\n await client.connect(freshTransport)\n }\n\n const { tools } = await client.listTools()\n await client.close()\n\n const tokens = provider.tokens()\n if (!tokens)\n throw new Error(`MCP OAuth: login for \"${config.name}\" returned no tokens (server may have rejected the exchange silently)`)\n\n await hooks?.callHook('mcp:auth:success', { name: config.name })\n return { tokens, tools }\n }\n catch (err) {\n const error = err instanceof Error ? err : new Error(String(err))\n await hooks?.callHook('mcp:auth:error', { name: config.name, error })\n throw error\n }\n finally {\n await callback?.close()\n }\n}\n\n/**\n * Build an `sse` / `streamable-http` transport pre-wired with `authProvider`.\n * Mirrors the bootstrap-side `createTransport` shape but inlined here so the\n * login flow doesn't depend on the bootstrap module's private export.\n */\nasync function createInteractiveTransport(config: McpServerConfig, authProvider: OAuthClientProvider) {\n if (config.transport === 'sse') {\n const { SSEClientTransport } = await import('@modelcontextprotocol/sdk/client/sse.js')\n return new SSEClientTransport(new URL(config.url!), {\n requestInit: config.headers ? { headers: config.headers } : undefined,\n authProvider,\n })\n }\n // streamable-http\n const { StreamableHTTPClientTransport } = await import('@modelcontextprotocol/sdk/client/streamableHttp.js')\n return new StreamableHTTPClientTransport(new URL(config.url!), {\n requestInit: config.headers ? { headers: config.headers } : undefined,\n fetch: sseToJsonFetchIfNeeded(),\n authProvider,\n })\n}\n\nfunction isUnauthorizedError(err: unknown): boolean {\n if (!err || typeof err !== 'object')\n return false\n const e = err as { name?: string, message?: string, constructor?: { name?: string } }\n if (e.name === 'UnauthorizedError' || e.constructor?.name === 'UnauthorizedError')\n return true\n return typeof e.message === 'string' && e.message.toLowerCase().startsWith('unauthorized')\n}\n\n/**\n * Wait on the callback handle, with a hard timeout AND honor the external\n * abort signal. Unlike `callback.promise` alone, this provides a deterministic\n * failure mode for \"user opened the browser then walked away\" — the modal\n * doesn't hang forever waiting for a redirect that may never come.\n */\nasync function raceLoginCallback(\n handle: OAuthCallbackHandle,\n timeoutMs: number,\n signal: AbortSignal | undefined,\n): Promise<{ code: string, state?: string }> {\n if (signal?.aborted)\n throw new Error('OAuth login aborted')\n let timer: ReturnType<typeof setTimeout> | undefined\n let onAbort: (() => void) | undefined\n try {\n return await new Promise<{ code: string, state?: string }>((resolve, reject) => {\n timer = setTimeout(\n () => reject(new Error(`OAuth login timed out after ${timeoutMs}ms`)),\n timeoutMs,\n )\n if (signal) {\n onAbort = () => reject(new Error('OAuth login aborted'))\n signal.addEventListener('abort', onAbort, { once: true })\n }\n handle.promise.then(resolve, reject)\n })\n }\n finally {\n if (timer)\n clearTimeout(timer)\n if (signal && onAbort)\n signal.removeEventListener('abort', onAbort)\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAkBA,IAAa,2BAAb,cAA8C,MAAM;CAClD,YAAY,SAAiB;EAC3B,MAAM,OAAO;EACb,KAAK,OAAO;CACd;AACF;;;;;;;AAQA,IAAa,4BAAb,cAA+C,MAAM;CACN;CAA7C,YAAY,SAAiB,YAAoC;EAC/D,MAAM,OAAO;EAD8B,KAAA,aAAA;EAE3C,KAAK,OAAO;CACd;AACF;;;;;;;;;;;;;;;;;ACeA,SAAgB,mBACd,OACA,OACA,WACiB;CACjB,IAAI,MAAM,WAAW,GACnB,MAAM,IAAI,yBAAyB,sBAAsB;CAE3D,IAAI;CACJ,IAAI;CAEJ,IAAI,UAAU,QAAQ;EACpB,cAAc;EACd,YAAY,CAAC;CACf,OACK,IAAI,UAAU,QAAQ;EACzB,MAAM,OAAO,KAAK,IAAI,GAAG,SAAS;EAClC,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AK;EACpB,IAAI,UAAU,SACZ,OAAO,oBAAoB,SAAS,OAAO;CAC/C;AACF"}
|
package/dist/mcp-Cy9mgCcr.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"mcp-Cy9mgCcr.js","names":[],"sources":["../src/mcp/oauth-provider.ts","../src/mcp/sse-to-json-fetch.ts","../src/mcp/tolerant-client.ts","../src/mcp/index.ts"],"sourcesContent":["/**\n * OAuth 2.1 client provider for MCP servers.\n *\n * Implements the MCP SDK's `OAuthClientProvider` interface — the SDK handles\n * the protocol (PKCE, RFC 9728 protected-resource discovery, RFC 8414 / OIDC\n * authorization-server metadata, RFC 7591 dynamic client registration, token\n * refresh). This class provides the two halves the SDK delegates to consumers:\n *\n * - **Persistence** — tokens, registered client info, optional discovery\n * state cache, and PKCE code verifier. Storage is injected via the\n * `McpCredentialStore` interface so the SDK adapter has no FS dependency;\n * the chat layer wires a file-backed store, tests use the in-memory one.\n *\n * - **Redirect** — when the SDK wants the user agent to navigate to the\n * authorization URL, we hand the URL to the host (the TUI opens a\n * browser + surfaces the URL in a status row). The PKCE code verifier\n * stays in process memory for the lifetime of the flow; persisting it\n * across process restarts would expose it on disk for no real benefit\n * since the loopback callback server is also process-local.\n *\n * Two modes by `redirectUri`:\n *\n * - **Non-interactive** (`redirectUri: undefined`) — used during bootstrap.\n * The SDK will use stored tokens, auto-refresh on expiry, but cannot\n * trigger a browser flow. Missing tokens cause the SDK to throw\n * `UnauthorizedError`, which the caller catches and converts to a\n * `mcp:auth:required` signal.\n *\n * - **Interactive** (`redirectUri: 'http://127.0.0.1:<port>/callback'`) —\n * used when the user explicitly triggers login. The caller has already\n * stood up a loopback callback server (see `oauth-callback.ts`) and\n * orchestrates: `client.connect()` → catches `UnauthorizedError` →\n * waits on the callback for the `code` → `transport.finishAuth(code)`\n * → retry `client.connect()`.\n */\n\nimport type {\n OAuthClientProvider,\n OAuthDiscoveryState,\n} from '@modelcontextprotocol/sdk/client/auth.js'\nimport type {\n OAuthClientInformationMixed,\n OAuthClientMetadata,\n OAuthTokens,\n} from '@modelcontextprotocol/sdk/shared/auth.js'\n\n/**\n * Per-server persisted state. Subfields are optional so a partial save\n * (e.g. `saveCodeVerifier` arriving before `saveTokens`) doesn't blow away\n * earlier subfields — the provider always patches, never replaces.\n */\nexport interface McpCredentialEntry {\n tokens?: OAuthTokens\n clientInformation?: OAuthClientInformationMixed\n discoveryState?: OAuthDiscoveryState\n}\n\nexport interface McpCredentialStore {\n load: (name: string) => McpCredentialEntry | undefined\n save: (name: string, entry: McpCredentialEntry) => void\n delete: (name: string) => void\n}\n\n/**\n * In-memory store — primarily for tests, but valid as a no-persistence option\n * (tokens evaporate on process exit, the user re-auths every cold start).\n */\nexport function createMemoryMcpCredentialStore(seed?: Record<string, McpCredentialEntry>): McpCredentialStore {\n const state = new Map<string, McpCredentialEntry>(Object.entries(seed ?? {}))\n return {\n load: name => state.get(name),\n save: (name, entry) => { state.set(name, entry) },\n delete: (name) => { state.delete(name) },\n }\n}\n\nexport interface McpOAuthProviderOptions {\n /** Server name — used as the storage key. */\n name: string\n /** Persistence backend. */\n store: McpCredentialStore\n /**\n * Loopback callback URI. Pass `undefined` for bootstrap (non-interactive\n * mode — stored tokens + refresh only, never opens a browser).\n */\n redirectUri?: string\n /**\n * Invoked when the SDK wants the user agent to navigate to the authorization\n * URL. Typically the host opens the browser AND emits a hook so the TUI can\n * render the URL in a status row. No-op in non-interactive mode (the SDK\n * still calls this before throwing `UnauthorizedError` from connect).\n */\n onAuthorizationUrl?: (url: URL) => void | Promise<void>\n /**\n * `client_name` used in dynamic client registration. Defaults to `'zidane'`.\n * Some servers display this string to the user on the consent screen.\n */\n clientName?: string\n /**\n * Override the requested OAuth scope. Default: unset (the SDK negotiates\n * via the server's metadata).\n */\n scope?: string\n}\n\nconst DEFAULT_CLIENT_NAME = 'zidane'\n\nexport class McpOAuthProvider implements OAuthClientProvider {\n private readonly name: string\n private readonly store: McpCredentialStore\n private readonly _redirectUri?: string\n private readonly onAuthorizationUrl?: (url: URL) => void | Promise<void>\n private readonly clientName: string\n private readonly _scope?: string\n // PKCE verifier is process-local — see file header. Module-scoped Map would\n // leak across concurrent flows for different servers; per-instance is the\n // right granularity.\n private codeVerifierValue: string | undefined\n\n constructor(opts: McpOAuthProviderOptions) {\n this.name = opts.name\n this.store = opts.store\n this._redirectUri = opts.redirectUri\n this.onAuthorizationUrl = opts.onAuthorizationUrl\n this.clientName = opts.clientName ?? DEFAULT_CLIENT_NAME\n this._scope = opts.scope\n }\n\n get redirectUrl(): string | URL | undefined {\n return this._redirectUri\n }\n\n get clientMetadata(): OAuthClientMetadata {\n return {\n // The SDK validates `redirect_uris` non-empty; we use a placeholder\n // when running non-interactively (no callback server is up). The\n // server won't actually redirect anywhere because the SDK throws\n // `UnauthorizedError` before any browser navigation in that mode.\n redirect_uris: [this._redirectUri ?? 'http://127.0.0.1:0/callback'],\n client_name: this.clientName,\n // PKCE-only public client — no client_secret. RFC 8252 native-app pattern.\n token_endpoint_auth_method: 'none',\n grant_types: ['authorization_code', 'refresh_token'],\n response_types: ['code'],\n ...(this._scope ? { scope: this._scope } : {}),\n }\n }\n\n tokens(): OAuthTokens | undefined {\n return this.store.load(this.name)?.tokens\n }\n\n saveTokens(tokens: OAuthTokens): void {\n this.patch({ tokens })\n }\n\n clientInformation(): OAuthClientInformationMixed | undefined {\n return this.store.load(this.name)?.clientInformation\n }\n\n saveClientInformation(info: OAuthClientInformationMixed): void {\n this.patch({ clientInformation: info })\n }\n\n discoveryState(): OAuthDiscoveryState | undefined {\n return this.store.load(this.name)?.discoveryState\n }\n\n saveDiscoveryState(state: OAuthDiscoveryState): void {\n this.patch({ discoveryState: state })\n }\n\n saveCodeVerifier(verifier: string): void {\n this.codeVerifierValue = verifier\n }\n\n codeVerifier(): string {\n if (!this.codeVerifierValue) {\n // Spec-correct error — the SDK would otherwise complete the code\n // exchange with an empty verifier and the auth server would 400.\n throw new Error(\n `MCP OAuth: code verifier missing for \"${this.name}\" — `\n + 'the flow must call saveCodeVerifier() before the redirect',\n )\n }\n return this.codeVerifierValue\n }\n\n async redirectToAuthorization(url: URL): Promise<void> {\n await this.onAuthorizationUrl?.(url)\n }\n\n /**\n * Wipe stored credentials when the server reports the cached state is no\n * longer valid. The SDK calls this with a scope hint:\n * - `'tokens'` → access/refresh revoked, keep client registration\n * - `'client'` → client registration invalidated, reset everything\n * - `'verifier'`→ PKCE state stale (e.g. mismatched state param)\n * - `'discovery'` → discovery metadata stale (servers re-keyed)\n * - `'all'` → full reset\n */\n async invalidateCredentials(scope: 'all' | 'client' | 'tokens' | 'verifier' | 'discovery'): Promise<void> {\n if (scope === 'verifier') {\n this.codeVerifierValue = undefined\n return\n }\n const current = this.store.load(this.name)\n if (!current)\n return\n if (scope === 'all') {\n this.codeVerifierValue = undefined\n this.store.delete(this.name)\n return\n }\n const next: McpCredentialEntry = { ...current }\n if (scope === 'tokens')\n delete next.tokens\n if (scope === 'client') {\n delete next.clientInformation\n // Client identity changed; previously-issued tokens and discovery\n // are no longer valid against the new registration.\n delete next.tokens\n delete next.discoveryState\n }\n if (scope === 'discovery')\n delete next.discoveryState\n this.store.save(this.name, next)\n }\n\n private patch(updates: Partial<McpCredentialEntry>): void {\n const existing = this.store.load(this.name) ?? {}\n this.store.save(this.name, { ...existing, ...updates })\n }\n}\n\n/**\n * True when an HTTP transport's auth headers already include an explicit\n * Authorization. Used by the bootstrap escape-hatch: a user who provided\n * their own bearer token shouldn't be auto-promoted to OAuth on a 401.\n *\n * Case-insensitive — Node normalizes outgoing headers to lowercase but\n * users hand-write `Authorization` in configs.\n */\nexport function hasAuthorizationHeader(headers: Record<string, string> | undefined): boolean {\n if (!headers)\n return false\n for (const key of Object.keys(headers)) {\n if (key.toLowerCase() === 'authorization')\n return true\n }\n return false\n}\n","/**\n * `fetch` shim that converts streamable-http POST responses with\n * `Content-Type: text/event-stream` into synthetic `application/json`\n * responses, preserving every original header (notably `mcp-session-id`).\n *\n * Why this exists\n * ----------------\n * The MCP SDK's streamable-http transport handles POST responses two ways\n * depending on `content-type`:\n *\n * - `application/json` → `await response.json()` (works on every runtime).\n * - `text/event-stream` → `body.pipeThrough(TextDecoderStream)\n * .pipeThrough(EventSourceParserStream)`\n * and forward each parsed event to `onmessage`.\n *\n * On Bun + MCP SDK 1.29.x the second pipeline silently drops the parsed\n * event: the full `event: message\\ndata: {...}\\n\\n` arrives intact, the\n * stream closes, but `onmessage` is never called. Bootstrap then waits the\n * full `bootstrapTimeout` (10s default) and the agent loses every tool the\n * server would have exposed.\n *\n * The MCP spec lets clients accept either content type\n * (`Accept: application/json, text/event-stream`), so flipping the stream\n * to JSON at the boundary is a transparent, server-agnostic workaround.\n *\n * Scope of the shim\n * -----------------\n * - POST + `text/event-stream` → drain, parse SSE message events, return\n * a synthetic `application/json` response. Single event becomes a JSON\n * object (matching the shape the SDK expects from non-streaming\n * servers); multiple events become a JSON array (the SDK already\n * iterates `Array.isArray(data)` from a JSON response).\n * - GET (long-lived `_startOrAuthSse` listener) → untouched. Per-event\n * latency matters there and the SSE pipeline isn't always broken on\n * GET in the same way (different code path inside Bun's stream impl).\n * - 202 / non-SSE / malformed SSE / no body → passthrough.\n *\n * Runtime gating\n * --------------\n * `sseToJsonFetchIfNeeded()` only returns a wrapper on Bun. Node + browser\n * runtimes get `undefined` and the SDK uses global `fetch` directly — no\n * extra buffer-and-redrain on the happy path, and no risk of collapsing a\n * future progress-streaming response into a single batched array. If you\n * need to apply the shim unconditionally (testing, custom hosts), call\n * `sseToJsonFetch()` directly.\n *\n * Cleanup\n * -------\n * Remove this file and its `fetch:` injection in `createTransport` once\n * either Bun fixes the `pipeThrough` chain or the SDK switches off the\n * streaming pipeline by default.\n */\n\n/**\n * Detect whether we're running on Bun. The `Bun` global is set by the\n * runtime itself and isn't faked by Bun's Node-compat layer.\n */\nfunction isBunRuntime(): boolean {\n return typeof (globalThis as { Bun?: unknown }).Bun !== 'undefined'\n}\n\n/**\n * Returns the `sseToJsonFetch` wrapper only on runtimes that need it\n * (currently Bun). Returns `undefined` everywhere else, which makes the\n * SDK fall back to the global `fetch` and keeps the streaming pipeline\n * intact on Node where it works correctly.\n *\n * Designed as the value to pass directly to `StreamableHTTPClientTransport`'s\n * `fetch` option:\n *\n * new StreamableHTTPClientTransport(url, {\n * fetch: sseToJsonFetchIfNeeded(),\n * })\n */\nexport function sseToJsonFetchIfNeeded(): typeof fetch | undefined {\n return isBunRuntime() ? sseToJsonFetch() : undefined\n}\n\n/**\n * Wrap a `fetch` implementation so streamable-http POST responses that come\n * back as `text/event-stream` are converted to JSON. Pass the result as\n * `opts.fetch` to `StreamableHTTPClientTransport`.\n *\n * Always-on (i.e. unconditional) — for the runtime-gated entry point,\n * use {@link sseToJsonFetchIfNeeded} instead.\n */\nexport function sseToJsonFetch(baseFetch: typeof fetch = fetch): typeof fetch {\n return async function sseToJsonWrappedFetch(input, init) {\n const response = await baseFetch(input, init)\n\n // Only intercept POSTs. The SDK's GET path opens a long-lived SSE\n // listener (`_startOrAuthSse`) that we must not buffer-drain.\n const method = (init?.method ?? 'GET').toString().toUpperCase()\n if (method !== 'POST')\n return response\n\n const contentType = response.headers.get('content-type')\n if (!contentType || !contentType.includes('text/event-stream'))\n return response\n\n if (!response.body)\n return response\n\n let raw: string\n try {\n raw = await response.text()\n }\n catch {\n // Bun edge case: if even .text() fails, surrender — the original\n // response is already drained, so we can't recover. Return it; the\n // SDK will surface the underlying read error.\n return response\n }\n\n const events = parseSseDataEvents(raw)\n // Single event → bare object; multi-event or zero → array. The SDK's\n // JSON branch handles both shapes (`Array.isArray(data) ? data.map(…) : …`).\n const payload = events.length === 1 ? events[0] : events\n return synthesizeJsonResponse(response, payload)\n } as typeof fetch\n}\n\n/**\n * Parse a buffered SSE body into the JSON payloads of its `message` events.\n *\n * Skips:\n * - SSE comments (lines starting with `:`).\n * - Non-default event types (`event: foo` ≠ `message`). The MCP server\n * only ever emits `message` events for JSON-RPC; anything else is out of\n * band and the SDK wouldn't have surfaced it to `onmessage` either.\n * - Malformed `data:` payloads (anything that fails `JSON.parse`).\n */\nfunction parseSseDataEvents(raw: string): unknown[] {\n const events: unknown[] = []\n for (const block of raw.split(/\\r?\\n\\r?\\n/)) {\n if (!block.trim())\n continue\n\n const dataLines: string[] = []\n let isMessageEvent = true\n for (const line of block.split(/\\r?\\n/)) {\n if (line.startsWith(':'))\n continue\n if (line.startsWith('event:')) {\n const eventType = line.slice('event:'.length).trim()\n if (eventType && eventType !== 'message')\n isMessageEvent = false\n }\n else if (line.startsWith('data:')) {\n // Per SSE spec: a single leading space after `data:` is part of the\n // separator, not the payload. Anything beyond it is.\n const value = line.slice('data:'.length)\n dataLines.push(value.startsWith(' ') ? value.slice(1) : value)\n }\n }\n\n if (!isMessageEvent || dataLines.length === 0)\n continue\n\n try {\n events.push(JSON.parse(dataLines.join('\\n')))\n }\n catch {\n // Drop malformed events. The SDK's broken pipe would have dropped\n // them too — staying silent here matches that baseline.\n }\n }\n return events\n}\n\n/**\n * Build a `Response` mirroring the original's status / statusText / headers\n * but with a JSON body and `content-type: application/json`. Header\n * preservation is the whole point — `mcp-session-id` is set by the server\n * on the initialize POST and must round-trip into the SDK's\n * `_sessionId` capture (`response.headers.get('mcp-session-id')`).\n */\nfunction synthesizeJsonResponse(original: Response, payload: unknown): Response {\n const headers = new Headers(original.headers)\n headers.set('content-type', 'application/json')\n return new Response(JSON.stringify(payload), {\n status: original.status,\n statusText: original.statusText,\n headers,\n })\n}\n","/**\n * Drop-in `Client` subclass whose `connect()` mirrors the upstream MCP SDK\n * sequence but treats `notifications/initialized` as best-effort.\n *\n * The MCP spec marks that notification fire-and-forget, but the SDK awaits\n * the underlying transport `send()` and rethrows on any HTTP 4xx. Several\n * real-world streamable-http servers (e.g. browser-codemode, custom MCPs\n * that gate non-initialize routes on a session id that didn't yet exist when\n * the notification posted) reject the notification with a 4xx and still\n * accept every subsequent request. The base `Client.connect()` then closes\n * the transport, the bootstrap fails, and the agent silently loses every\n * tool the server would have exposed.\n *\n * This module changes only that single step — log + continue if the\n * notification throws. Initialize, capability/version capture,\n * `setProtocolVersion`, listChanged handler wiring, and the close-on-failure\n * path for everything else are preserved verbatim from the SDK.\n *\n * Lazy SDK load: `@modelcontextprotocol/sdk` is an *optional* peer dep, so\n * the class is built inside `createTolerantClient()` via dynamic imports.\n * Importing this module does NOT trigger SDK resolution — callers only\n * incur the cost when they actually instantiate a client.\n *\n * Should be removed when the SDK lands an opt-in tolerance flag upstream.\n */\nimport type { Client } from '@modelcontextprotocol/sdk/client/index.js'\nimport { errorMessage } from '../errors'\n\n// Private fields the override needs to read/write to mirror upstream behavior.\n// Cast surface is isolated to this one type so the rest of the file stays clean.\ninterface ClientPrivates {\n _capabilities: unknown\n _clientInfo: unknown\n _serverCapabilities: unknown\n _serverVersion: unknown\n _instructions: unknown\n _pendingListChangedConfig?: unknown\n _setupListChangedHandlers: (config: unknown) => void\n}\n\ninterface MaybeSessionedTransport {\n sessionId?: unknown\n setProtocolVersion?: (v: string) => void\n}\n\n/**\n * Options forwarded to the upstream `Client` constructor. Mirrors the\n * publicly-observable shape so we don't have to re-export `Client`'s\n * private `ClientOptions` type (which would force consumers of the lazy\n * factory below to install the SDK at type-check time too).\n */\nexport interface TolerantClientOptions {\n /** Capabilities advertised to the server on `initialize`. */\n capabilities?: Record<string, unknown>\n /** When true, the SDK rejects requests for methods the server didn't advertise. */\n enforceStrictCapabilities?: boolean\n /** Per-request timeout in ms; the SDK falls back to its own default when unset. */\n defaultRequestTimeout?: number\n /**\n * Best-effort notification sink. Fires once if the server rejected\n * `notifications/initialized` with a 4xx — the connection survived but\n * the client wants to know it deviated from the spec. Default: no-op.\n *\n * Routing this through a callback (instead of a hard-coded\n * `console.warn`) is what lets `connectMcpServers` collect the warning,\n * include it on the `mcp:bootstrap:end` payload, and keep hosts that\n * don't care from seeing stray stderr.\n */\n onWarning?: (message: string) => void\n}\n\n/**\n * Async factory — builds an instance of the tolerant MCP client. The\n * actual `Client` subclass definition lives inside the factory so the\n * `@modelcontextprotocol/sdk` import only resolves when MCP is in use.\n *\n * Second `options` arg forwards directly into the upstream `Client`\n * constructor so tests / advanced consumers can supply capabilities,\n * `enforceStrictCapabilities`, etc.\n */\nexport async function createTolerantClient(\n info: { name: string, version: string },\n options?: TolerantClientOptions,\n): Promise<Client> {\n const { Client } = await import('@modelcontextprotocol/sdk/client/index.js')\n const { Protocol } = await import('@modelcontextprotocol/sdk/shared/protocol.js')\n const { InitializeResultSchema, LATEST_PROTOCOL_VERSION, SUPPORTED_PROTOCOL_VERSIONS } = await import('@modelcontextprotocol/sdk/types.js')\n\n // Capture once; the override below mustn't re-read `options.onWarning` per\n // connect because hosts may not consider `options` long-lived.\n const onWarning = options?.onWarning\n\n class TolerantMcpClient extends Client {\n async connect(\n transport: Parameters<Client['connect']>[0],\n options?: Parameters<Client['connect']>[1],\n ): Promise<void> {\n // Wire the transport (Protocol.connect — onmessage/onclose/onerror hooks\n // and transport.start) without sending any messages. We MUST NOT call\n // `super.connect()` here — that's `Client.connect`, which would\n // re-execute the very initialize+notification dance we're overriding.\n await Protocol.prototype.connect.call(this as never, transport)\n\n // Reconnect path: the upstream client short-circuits on a transport that\n // already carries a session id, so do we.\n if ((transport as MaybeSessionedTransport).sessionId !== undefined)\n return\n\n const self = this as unknown as ClientPrivates\n\n try {\n const result = await this.request(\n {\n method: 'initialize',\n params: {\n protocolVersion: LATEST_PROTOCOL_VERSION,\n capabilities: self._capabilities,\n clientInfo: self._clientInfo,\n },\n },\n InitializeResultSchema,\n options,\n )\n\n if (result === undefined)\n throw new Error(`Server sent invalid initialize result: ${result}`)\n if (!SUPPORTED_PROTOCOL_VERSIONS.includes(result.protocolVersion))\n throw new Error(`Server's protocol version is not supported: ${result.protocolVersion}`)\n\n self._serverCapabilities = result.capabilities\n self._serverVersion = result.serverInfo\n\n const setProtocolVersion = (transport as MaybeSessionedTransport).setProtocolVersion\n if (setProtocolVersion)\n setProtocolVersion.call(transport, result.protocolVersion)\n\n self._instructions = result.instructions\n\n // Best-effort. The session id (if any) is already captured by the\n // transport from the `initialize` response headers, so subsequent\n // requests like `tools/list` still authenticate. Don't bring down the\n // whole connection on a 4xx for a fire-and-forget notification.\n try {\n await this.notification({ method: 'notifications/initialized' })\n }\n catch (notifyError) {\n // Hosts opt in to the signal via `onWarning`; default is silent\n // so a plain `new Client()` doesn't leak stderr into every host.\n onWarning?.(`server rejected notifications/initialized (continuing): ${errorMessage(notifyError)}`)\n }\n\n if (self._pendingListChangedConfig) {\n self._setupListChangedHandlers(self._pendingListChangedConfig)\n self._pendingListChangedConfig = undefined\n }\n }\n catch (error) {\n // Same failure path as upstream Client — anything *other* than the\n // tolerated notification failure tears the transport down so the\n // bootstrap surfaces a clear error.\n void this.close().catch(() => {})\n throw error\n }\n }\n }\n\n return new TolerantMcpClient(info, options)\n}\n","/**\n * MCP (Model Context Protocol) server support.\n *\n * Connects to one or more MCP servers, discovers their tools,\n * and wraps them as zidane ToolDefs for use in agent loops.\n */\n\nimport type { OAuthClientProvider } from '@modelcontextprotocol/sdk/client/auth.js'\nimport type { Client } from '@modelcontextprotocol/sdk/client/index.js'\nimport type { Hookable } from 'hookable'\nimport type { AgentHooks } from '../agent'\nimport type { ToolContext, ToolDef } from '../tools/types'\nimport type { McpServerConfig, McpToolHookContext, McpToolSchema, ToolResultContent } from '../types'\nimport { Buffer } from 'node:buffer'\nimport { errorMessage } from '../errors'\nimport { reconcileImageMediaType } from '../tools/image-sniff'\nimport { toolOutputByteLength, toolResultToText } from '../types'\nimport { hasAuthorizationHeader } from './oauth-provider'\nimport { sseToJsonFetchIfNeeded } from './sse-to-json-fetch'\nimport { createTolerantClient } from './tolerant-client'\n\n/**\n * Subset of the MCP SDK's `Transport` interface that `bootstrapServer` needs\n * to clean up. Typed structurally so a failed `createTransport()` doesn't\n * force us to pull the SDK's full transport type into our static graph.\n */\ninterface ClosableTransport {\n close: () => void | Promise<void>\n}\n\n// NOTE: `@modelcontextprotocol/sdk` is an *optional* peer dependency\n// (see package.json). Static imports here would force every consumer of\n// `zidane` to install the SDK even if they never wire any MCP servers —\n// which fails on install with `--no-optional` or in restricted CI\n// environments. All runtime imports are inside `createTransport` /\n// `loadTolerantClient` below so the SDK is only resolved when MCP is\n// actually used. Type-only imports above are erased at compile time.\n\nexport type { McpServerConfig, McpToolSchema } from '../types'\n\nexport interface McpConnection {\n tools: Record<string, ToolDef>\n /**\n * Per-server `instructions` payload from the MCP `initialize` handshake,\n * keyed by server name. Each server may return free-form guidance the\n * model is meant to read alongside the tool catalog — typical content\n * is \"the database is already provisioned, use `apply_migration`\n * directly\" or \"always wrap SELECTs in a transaction\". The agent\n * renders these into a system-prompt section when\n * `behavior.surfaceMcpInstructions` is on (default).\n *\n * Servers that omit the field, return an empty string, or fail to\n * connect contribute no entry. Lazy-connected servers contribute an\n * entry only once their first connect resolves — until then the model\n * never sees their instructions, matching the schema-loading semantics\n * of `lazyConnect`.\n *\n * Map (not Record) so insertion order matches server config order and\n * the rendered section is byte-stable for the cache breakpoint.\n *\n * Optional so custom `mcpConnector` implementations from before this\n * field existed still type-check; the framework's own\n * `connectMcpServers` always returns a Map (possibly empty).\n */\n instructions?: Map<string, string>\n close: () => Promise<void>\n}\n\n// ---------------------------------------------------------------------------\n// Shape normalization\n// ---------------------------------------------------------------------------\n\ninterface RawServerShape {\n name?: string\n transport?: string\n type?: string\n command?: string\n args?: string[]\n env?: Record<string, string>\n strictEnv?: boolean\n cwd?: string\n url?: string\n httpUrl?: string\n sseUrl?: string\n headers?: Record<string, string>\n bootstrapTimeout?: number\n toolTimeout?: number\n closeTimeout?: number\n enabledTools?: string[]\n disabledTools?: string[]\n toolFilter?: McpServerConfig['toolFilter']\n disclosure?: McpServerConfig['disclosure']\n cachedTools?: McpServerConfig['cachedTools']\n lazyConnect?: boolean\n /** Canonical OAuth flag. */\n auth?: McpServerConfig['auth']\n /** Cursor runtime-state alias for `auth: 'oauth'`. */\n authMethod?: string\n [key: string]: unknown\n}\n\nconst DEFAULT_MCP_BOOTSTRAP_TIMEOUT_MS = 10_000\nconst DEFAULT_MCP_CLOSE_TIMEOUT_MS = 5_000\n\n// ---------------------------------------------------------------------------\n// Bootstrap-internal helpers (hook safety + cleanup + error hints)\n//\n// These keep `bootstrapServer` and `connectMcpServers` readable while\n// providing three guarantees the bootstrap contract relies on:\n//\n// 1. A misbehaving hook handler MUST NOT take down the whole batch — the\n// partial-failure tolerance (`Promise.allSettled` in the caller plus\n// this safe-fire helper) means one bad listener degrades to a debug\n// log instead of \"no MCP tools at all\".\n// 2. A timed-out / failed bootstrap MUST close its transport explicitly,\n// not just the SDK client. Until `Protocol.connect(transport)` has\n// wired the transport into the client, `client.close()` doesn't know\n// about it and the underlying stdio subprocess / HTTP connection\n// lingers until GC.\n// 3. Transport-mismatch failures (the common `type: 'http'` vs SSE-only\n// server case from downstream reports) MUST surface an actionable\n// hint in the error message, not just `HTTP 405`.\n// ---------------------------------------------------------------------------\n\n/**\n * Fire a hook without letting a listener's rejection escape. A throwing\n * listener used to bring down the whole `Promise.all` in\n * `connectMcpServers`, defeating the batch's partial-failure tolerance.\n *\n * Failures are swallowed; under `ZIDANE_DEBUG` they're traced to stderr\n * so the misbehaving listener is still discoverable when a host opts in.\n *\n * `args` is variadic to match `Hookable.callHook`'s spread shape; in\n * practice every `AgentHooks` event takes a single context object.\n */\nasync function safeCallHook<K extends keyof AgentHooks & string>(\n hooks: Hookable<AgentHooks> | undefined,\n event: K,\n ...args: Parameters<AgentHooks[K]>\n): Promise<void> {\n if (!hooks)\n return\n try {\n await hooks.callHook(event, ...args)\n }\n catch (err) {\n if (process.env.ZIDANE_DEBUG)\n process.stderr.write(`[zidane/mcp] hook \"${event}\" listener rejected: ${errorMessage(err)}\\n`)\n }\n}\n\n/**\n * Close a transport best-effort. The catch swallows everything — by the\n * time we're closing, the bootstrap has already failed and the only thing\n * worse than a leaked transport is a leaked transport AND a new error\n * masking the original cause.\n */\nasync function closeTransportQuietly(transport: ClosableTransport | null): Promise<void> {\n if (!transport)\n return\n try {\n await transport.close()\n }\n catch {\n // Original bootstrap error is more actionable.\n }\n}\n\n/**\n * Read the child PID off a stdio transport. Structural — only\n * `StdioClientTransport` exposes `pid`; HTTP/SSE transports return `null`.\n * Returns `null` once the child has exited (the SDK clears its process\n * reference on the child's `close` event), so a kill based on this value\n * never targets a recycled PID.\n */\nfunction stdioPidOf(transport: unknown): number | null {\n const pid = (transport as { pid?: unknown } | null | undefined)?.pid\n return typeof pid === 'number' ? pid : null\n}\n\n/**\n * Last-resort reaper for a stdio child whose transport close wedged. By the\n * time this runs, the graceful path (stdin EOF → SIGTERM escalation inside\n * the SDK, where the installed version implements it) has been abandoned —\n * SIGKILL is the only signal a hung child can't ignore. ESRCH (already\n * exited) is swallowed.\n */\nfunction killStdioChild(pid: number | null): void {\n if (pid == null)\n return\n try {\n process.kill(pid, 'SIGKILL')\n }\n catch {\n // Child already exited between PID capture and the kill.\n }\n}\n\n/**\n * Detect failure signatures that point at a transport mismatch and return\n * an actionable hint string. Conservative — only matches the signals we've\n * actually observed in the wild against MCP servers, so a real outage on\n * the right transport doesn't get misdiagnosed.\n *\n * Streamable-HTTP against an SSE-only endpoint:\n * - The transport opens an `initialize` POST. SSE-only servers respond\n * with `405 Method Not Allowed` or `404 Not Found` (their `/sse` GET\n * endpoint is the only handler). The SDK wraps these as\n * `Error POSTing to endpoint (HTTP 40X): ...`.\n * - Some proxies return `400` with a `text/event-stream` mismatch on\n * POST; same root cause, same fix.\n */\nfunction buildTransportMismatchHint(transport: McpServerConfig['transport'], err: Error): string | null {\n const msg = err.message\n if (transport === 'streamable-http') {\n if (/\\bHTTP\\s*40[45]\\b/i.test(msg) || /\\(40[45]\\)/.test(msg))\n return 'This server may not implement the streamable-HTTP transport. Try `transport: \\'sse\\'` (or `sseUrl` instead of `url`) on this server\\'s config.'\n }\n return null\n}\n\n/**\n * Wrap a bootstrap failure so the surfaced `Error` carries an actionable\n * hint while preserving the original message + stack + cause. Returns the\n * input error verbatim when no hint applies.\n *\n * `cause` (ES2022) gives hosts that introspect a way back to the raw SDK\n * error; the human-readable hint is the new message body that lands in\n * logs / hook payloads.\n */\nfunction withTransportMismatchHint(transport: McpServerConfig['transport'], err: Error): Error {\n const hint = buildTransportMismatchHint(transport, err)\n if (!hint)\n return err\n const augmented = new Error(`${err.message}\\nHint: ${hint}`, { cause: err })\n if (err.stack)\n augmented.stack = err.stack\n return augmented\n}\n\nfunction inferTransport(raw: RawServerShape): McpServerConfig['transport'] {\n if (raw.transport === 'stdio' || raw.transport === 'sse' || raw.transport === 'streamable-http')\n return raw.transport\n if (raw.type === 'stdio' || raw.type === 'sse' || raw.type === 'streamable-http' || raw.type === 'http')\n return raw.type === 'http' ? 'streamable-http' : raw.type\n if (raw.command)\n return 'stdio'\n if (raw.httpUrl)\n return 'streamable-http'\n if (raw.sseUrl)\n return 'sse'\n if (raw.url)\n return 'streamable-http'\n throw new Error(`Cannot infer MCP transport from config: ${JSON.stringify(raw)}`)\n}\n\nfunction normalizeOne(name: string, raw: RawServerShape): McpServerConfig {\n const transport = inferTransport(raw)\n const url = raw.url ?? raw.httpUrl ?? raw.sseUrl\n\n const config: McpServerConfig = { name, transport }\n if (raw.command)\n config.command = raw.command\n if (raw.args)\n config.args = raw.args\n if (raw.env)\n config.env = raw.env\n if (raw.strictEnv === true)\n config.strictEnv = true\n if (typeof raw.cwd === 'string' && raw.cwd.length > 0)\n config.cwd = raw.cwd\n if (url)\n config.url = url\n if (raw.headers)\n config.headers = raw.headers\n if (typeof raw.bootstrapTimeout === 'number')\n config.bootstrapTimeout = raw.bootstrapTimeout\n if (typeof raw.toolTimeout === 'number')\n config.toolTimeout = raw.toolTimeout\n if (typeof raw.closeTimeout === 'number')\n config.closeTimeout = raw.closeTimeout\n if (Array.isArray(raw.enabledTools))\n config.enabledTools = raw.enabledTools\n if (Array.isArray(raw.disabledTools))\n config.disabledTools = raw.disabledTools\n if (typeof raw.toolFilter === 'function')\n config.toolFilter = raw.toolFilter\n if (raw.disclosure === 'eager' || raw.disclosure === 'lazy')\n config.disclosure = raw.disclosure\n if (Array.isArray(raw.cachedTools))\n config.cachedTools = raw.cachedTools\n if (raw.lazyConnect === true)\n config.lazyConnect = true\n // Canonical `auth` field; also recognize Cursor's `authMethod: 'mcpOAuth'`\n // runtime-state shape so pasted ~/.cursor/mcp.json entries Just Work.\n if (raw.auth === 'oauth' || raw.authMethod === 'mcpOAuth')\n config.auth = 'oauth'\n\n return config\n}\n\n/**\n * True when the input looks like a single config object (flat fields like `command`,\n * `transport`, `url`) rather than a record whose values are configs.\n */\nfunction looksLikeSingleConfig(obj: Record<string, unknown>): boolean {\n const singleConfigKeys = ['transport', 'type', 'command', 'url', 'httpUrl', 'sseUrl']\n return singleConfigKeys.some(key => typeof obj[key] === 'string')\n}\n\n/**\n * Normalize MCP server configs from any common shape to `McpServerConfig[]`.\n *\n * Accepts:\n * - `McpServerConfig[]` — zidane native (pass-through).\n * - `McpServerConfig` — a single config object (wrapped to a 1-element array).\n * - `Record<string, RawShape>` — name-keyed map (common in host-SDK configs), where the key is the server name.\n * - Mixed shapes with `type` vs `transport`, `httpUrl`/`sseUrl` vs `url`.\n *\n * Returns `[]` when `input` is nullish. Throws a descriptive error when the transport\n * cannot be inferred from a given entry, or when the input shape is unsupported.\n */\nexport function normalizeMcpServers(input: unknown): McpServerConfig[] {\n if (input == null)\n return []\n\n if (Array.isArray(input)) {\n return input.map((raw, idx) => {\n const obj = raw as RawServerShape\n const name = obj.name ?? `mcp_${idx}`\n return normalizeOne(name, obj)\n })\n }\n\n if (typeof input === 'object') {\n const obj = input as Record<string, unknown>\n // Single-config heuristic: flat fields like `transport`/`command`/`url` at the top\n // level indicate a single McpServerConfig, not a record of configs.\n if (looksLikeSingleConfig(obj)) {\n const raw = obj as RawServerShape\n const name = raw.name ?? 'mcp_0'\n return [normalizeOne(name, raw)]\n }\n return Object.entries(obj as Record<string, RawServerShape>).map(\n ([name, raw]) => normalizeOne(name, raw ?? {}),\n )\n }\n\n throw new Error(`Unsupported MCP server config shape: ${typeof input}`)\n}\n\n/**\n * Lossy flattener — converts MCP `CallToolResult.content` blocks to a single\n * string. Text blocks are extracted; non-text blocks are JSON-stringified.\n *\n * Use this only at UI / log boundaries that require a string. The agent\n * loop itself routes through {@link normalizeMcpBlocks} so image blocks\n * survive into provider-native tool_result content (Anthropic blocks,\n * OpenAI companion-user-message).\n */\nexport function resultToString(content: unknown): string {\n if (!content || !Array.isArray(content))\n return ''\n return content\n .map((block) => {\n if (block && typeof block === 'object' && (block as { type?: unknown }).type === 'text') {\n const text = (block as { text?: unknown }).text\n if (typeof text === 'string')\n return text\n }\n return JSON.stringify(block)\n })\n .join('\\n')\n}\n\n/**\n * Normalize MCP `CallToolResult.content` to zidane's {@link ToolResultContent[]} shape.\n *\n * Handles the four MCP content block types:\n * - `text` → preserved as `{type:'text', text}`\n * - `image` → preserved as `{type:'image', mediaType, data}` (MCP uses `mimeType`)\n * - `resource` with embedded text → flattened to a text block\n * - `resource` with embedded blob whose `mimeType` is `image/*` → flattened to an image block\n * - Any unrecognized block → JSON-stringified fallback text block (lossy but safe)\n *\n * Returns `null` when the input is not an array — callers should fall back to an empty\n * result in that case.\n */\nexport function normalizeMcpBlocks(content: unknown): ToolResultContent[] | null {\n if (!Array.isArray(content))\n return null\n\n const out: ToolResultContent[] = []\n for (const raw of content) {\n if (!raw || typeof raw !== 'object')\n continue\n const block = raw as Record<string, unknown>\n\n if (block.type === 'text' && typeof block.text === 'string') {\n out.push({ type: 'text', text: block.text })\n continue\n }\n\n if (block.type === 'image' && typeof block.data === 'string') {\n const declared = typeof block.mimeType === 'string'\n ? block.mimeType\n : (typeof block.mediaType === 'string' ? block.mediaType : 'image/png')\n // Anthropic rejects the request when `media_type` disagrees with the\n // magic bytes. Some MCP servers (browser/screenshot bridges) declare\n // `image/webp` while actually serving JPEG; reconcile here so the\n // wrong label never reaches the provider.\n const mediaType = reconcileImageMediaType(declared, block.data)\n out.push({ type: 'image', mediaType, data: block.data })\n continue\n }\n\n if (block.type === 'resource' && block.resource && typeof block.resource === 'object') {\n const res = block.resource as Record<string, unknown>\n if (typeof res.text === 'string') {\n out.push({ type: 'text', text: res.text })\n continue\n }\n if (typeof res.blob === 'string' && typeof res.mimeType === 'string' && res.mimeType.startsWith('image/')) {\n const mediaType = reconcileImageMediaType(res.mimeType, res.blob)\n out.push({ type: 'image', mediaType, data: res.blob })\n continue\n }\n }\n\n // Audio, resource_link, and unknown block shapes — fall back to a JSON-stringified\n // text block. Lossy but keeps the information addressable by the model.\n out.push({ type: 'text', text: JSON.stringify(block) })\n }\n\n return out\n}\n\n/**\n * Route the MCP result content through the narrowest appropriate zidane shape:\n *\n * - All blocks are `text` → return a joined string (smaller wire payload,\n * string-friendly for hook consumers that don't need structured access).\n * - Any block is non-text → return a structured `ToolResultContent[]`.\n * - Empty / non-array input → return `''`.\n */\nfunction packMcpResult(content: unknown): string | ToolResultContent[] {\n const normalized = normalizeMcpBlocks(content)\n if (!normalized || normalized.length === 0)\n return ''\n\n // Single pass: build the joined string as we go. Bail to the structured array\n // the moment we hit a non-text block.\n const parts: string[] = []\n for (const block of normalized) {\n if (block.type !== 'text')\n return normalized\n parts.push(block.text)\n }\n return parts.join('\\n')\n}\n\n/**\n * Create the appropriate MCP transport for a server config.\n *\n * For stdio: when `config.env` is provided, it is merged on top of the MCP SDK's\n * `getDefaultEnvironment()` whitelist (`PATH`, `HOME`, `LANG`, `SHELL`, `USER` on\n * POSIX; `APPDATA`, `PATH`, ... on Win32). Without this defensive merge, older\n * MCP SDK versions strip `PATH` the moment a consumer sets any env, breaking\n * `spawn('node', ...)` with ENOENT. Pass `strictEnv: true` to opt out and send\n * `env` verbatim.\n *\n * `config.cwd` is forwarded to `StdioClientTransport` (→ `spawn`'s `cwd`).\n * Without this, the child inherits the agent's cwd, breaking servers that\n * resolve paths relative to their own working directory (e.g. project-root\n * MCPs reading `<root>/db/migrations/...`).\n */\nasync function createTransport(\n config: McpServerConfig,\n authProvider?: OAuthClientProvider,\n hooks?: Hookable<AgentHooks>,\n) {\n switch (config.transport) {\n case 'stdio': {\n // Routed through `./stdio-loader.ts` so the SDK's stdio transport\n // lives in zidane's static module graph at build time. That lets the\n // `cross-spawn` → shim alias in `tsdown.config.ts` fire and removes\n // the bare `require('child_process')` from the chunk that downstream\n // consumers re-bundle. See `./stdio-loader.ts` for the full story.\n const { StdioClientTransport, getDefaultEnvironment } = await import('./stdio-loader')\n const mergedEnv = config.env && !config.strictEnv\n ? { ...getDefaultEnvironment(), ...config.env }\n : config.env\n const transport = new StdioClientTransport({\n command: config.command!,\n args: config.args,\n env: mergedEnv,\n // By default `inherit` prints stdio from server into TUI framebuffer\n // and breaks UI, we'll pipe output to `mcp:warn` hook\n stderr: 'pipe',\n ...(config.cwd ? { cwd: config.cwd } : {}),\n })\n attachStderrWarnPump(transport, config.name, hooks)\n return transport\n }\n case 'sse': {\n const { SSEClientTransport } = await import('@modelcontextprotocol/sdk/client/sse.js')\n return new SSEClientTransport(new URL(config.url!), {\n requestInit: config.headers ? { headers: config.headers } : undefined,\n authProvider,\n })\n }\n case 'streamable-http': {\n // `fetch: sseToJsonFetchIfNeeded()` works around a Bun + MCP SDK\n // 1.29.x bug where `text/event-stream` POST responses are buffered\n // correctly but never surface to `onmessage`, dead-locking bootstrap.\n // The helper returns the wrapper only on Bun and `undefined` on Node\n // (so the SDK's streaming pipeline stays intact where it works).\n // See ./sse-to-json-fetch.ts.\n const { StreamableHTTPClientTransport } = await import('@modelcontextprotocol/sdk/client/streamableHttp.js')\n return new StreamableHTTPClientTransport(new URL(config.url!), {\n requestInit: config.headers ? { headers: config.headers } : undefined,\n fetch: sseToJsonFetchIfNeeded(),\n authProvider,\n })\n }\n default:\n throw new Error(`Unknown MCP transport: ${config.transport}`)\n }\n}\n\n/**\n * Split a stdio-transport child's stderr into lines and surface them via `mcp:warn`.\n */\nconst MAX_MCP_STDERR_LINE_CHARS = 64 * 1024\n\nexport function attachStderrWarnPump(\n // Structural: SDK types `stderr` as Node's generic `Stream`, but at runtime\n // it's a `Readable`. Loose-typed listener so this matches the SDK shape.\n transport: { stderr?: { on: (event: string, listener: (...args: unknown[]) => void) => unknown } | null },\n serverName: string,\n hooks: Hookable<AgentHooks> | undefined,\n): void {\n const stream = transport.stderr\n if (!stream)\n return\n let buffer = ''\n const emitLine = (line: string): void => {\n const trimmed = line.replace(/\\r$/, '').trim()\n if (trimmed.length === 0)\n return\n // Don't await — a slow handler must not backpressure the child.\n // Also swallow listener failures; stderr capture is diagnostic-only.\n const pending = hooks?.callHook('mcp:warn', { name: serverName, message: trimmed })\n void pending?.catch(() => {})\n }\n const flush = (): void => {\n if (buffer.length === 0)\n return\n emitLine(buffer)\n buffer = ''\n }\n // Must consume — leaving the stream unread hangs the child once the\n // OS pipe (~64 KiB) fills.\n stream.on('data', (chunk: unknown) => {\n // Node stream `data` events deliver Buffer | string (non-objectMode).\n buffer += typeof chunk === 'string' ? chunk : (chunk as Buffer).toString('utf8')\n let nl = buffer.indexOf('\\n')\n while (nl !== -1) {\n const line = buffer.slice(0, nl)\n buffer = buffer.slice(nl + 1)\n emitLine(line)\n nl = buffer.indexOf('\\n')\n }\n while (buffer.length >= MAX_MCP_STDERR_LINE_CHARS) {\n emitLine(buffer.slice(0, MAX_MCP_STDERR_LINE_CHARS))\n buffer = buffer.slice(MAX_MCP_STDERR_LINE_CHARS)\n }\n })\n // Connection liveness flows through the SDK's own onclose/onerror → `mcp:error`.\n stream.on('end', flush)\n stream.on('close', flush)\n stream.on('error', flush)\n}\n\n/**\n * True when an error from `client.connect()` indicates the user needs to\n * (re-)authenticate against the MCP server. Two signal families:\n *\n * 1. **SDK-formal** — the MCP SDK confirmed the resource is OAuth-protected\n * (RFC 9728 metadata advertised) and threw `UnauthorizedError`. Detected\n * by class name + \"Unauthorized\" message prefix, since the SDK's\n * `UnauthorizedError` class isn't a stable export across versions.\n *\n * 2. **Raw HTTP 401 with token-rejection signal** — some real-world servers\n * (Linear is one) skip the RFC 9728 dance and just return `HTTP 401`\n * with an OAuth-style JSON body (`{\"error\":\"invalid_token\"}` /\n * `\"invalid_grant\"` / `\"insufficient_scope\"`). The MCP SDK wraps that\n * into `\"Error POSTing to endpoint (HTTP 401): { ... }\"`. Without\n * promotion, a stale Linear token reads as a generic `mcp:error`\n * bootstrap failure instead of \"needs login\" — the user gets a noisy\n * red banner instead of a re-login affordance.\n *\n * Promotion to the skip+auth-required path is still gated on\n * `explicitAuth || eligibleAutoPromote` in the caller, so a server that\n * legitimately requires a static `Authorization: Bearer …` header (no\n * `auth: 'oauth'` flag, host-provided header) won't be mis-promoted into\n * a doomed OAuth flow.\n */\nfunction isUnauthorizedError(err: unknown): boolean {\n if (!err || typeof err !== 'object')\n return false\n const e = err as { name?: string, message?: string, constructor?: { name?: string } }\n if (e.name === 'UnauthorizedError' || e.constructor?.name === 'UnauthorizedError')\n return true\n const message = typeof e.message === 'string' ? e.message : ''\n if (message.toLowerCase().startsWith('unauthorized'))\n return true\n // Raw HTTP 401 with an OAuth-style token-rejection body — the\n // streamable-http transport surfaces these as `Error POSTing to endpoint\n // (HTTP 401): {\"error\":\"invalid_token\",\"error_description\":\"…\"}`.\n if (/\\bHTTP\\s*401\\b/i.test(message) || /\\b\\(401\\)/.test(message)) {\n if (/\"error\"\\s*:\\s*\"(?:invalid_token|invalid_grant|insufficient_scope|invalid_client)\"/i.test(message))\n return true\n }\n return false\n}\n\n/**\n * Optional knobs threaded into per-server bootstrap. Reserved for runtime\n * concerns the agent shell injects (credential storage, OAuth providers);\n * the `McpServerConfig` shape stays pure data.\n */\nexport interface ConnectMcpServersOptions {\n /**\n * Build a non-interactive `OAuthClientProvider` for a given server. Called\n * lazily — only invoked when the bootstrap actually needs auth (explicit\n * `auth: 'oauth'` flag, or a UnauthorizedError that meets the auto-detect\n * criteria). Return `undefined` to opt this server out of OAuth even if it\n * was requested (e.g. the host doesn't have a credential store for it).\n *\n * \"Non-interactive\" — the provider must NOT trigger a browser flow during\n * bootstrap. Use a `redirectUri` of `undefined` so the SDK reads stored\n * tokens, refreshes on expiry, and throws `UnauthorizedError` if tokens\n * are missing or refresh fails. The host then surfaces an\n * `mcp:auth:required` event so the user can opt in to interactive login.\n */\n buildAuthProvider?: (config: McpServerConfig) => OAuthClientProvider | undefined\n}\n\n/**\n * Internal: shape received by the `_clientFactory` test-injection seam.\n * Mirrors the subset of `TolerantClientOptions` the bootstrap path forwards\n * so a mock client can opt into the same lifecycle wiring (warning sink,\n * future seams) instead of silently bypassing it.\n */\ninterface McpClientFactoryOptions {\n /** See `TolerantClientOptions.onWarning`. */\n onWarning?: (message: string) => void\n}\n\ntype McpClientFactory = (opts?: McpClientFactoryOptions) => Client\n\n/**\n * Connect to MCP servers and discover their tools.\n *\n * Each tool is namespaced as `mcp_{serverName}_{toolName}` to avoid\n * collisions with agent tools or tools from other servers.\n *\n * @param configs - Array of MCP server configurations\n * @param _clientFactory - Internal: override client construction for testing\n * @param hooks - Optional agent hooks for firing mcp:connect, mcp:error, mcp:close events\n * @param options - Optional bootstrap knobs (e.g. OAuth provider injection)\n */\nexport async function connectMcpServers(\n configs: McpServerConfig[],\n _clientFactory?: McpClientFactory,\n hooks?: Hookable<AgentHooks>,\n options?: ConnectMcpServersOptions,\n): Promise<McpConnection> {\n // Per-server cleanup closure. Eager entries always have a live client; lazy\n // entries may or may not have connected — `closeIfConnected` short-circuits\n // when nothing was opened, and awaits any in-flight connect to avoid\n // leaking the resulting client past `close()`.\n //\n // `stdioPid` exposes the live stdio child's PID so the bounded `close()`\n // below can SIGKILL a child whose transport close wedged. It must be read\n // BEFORE initiating the close — the SDK's stdio transport clears its\n // process reference the moment `close()` starts, so there is nothing left\n // to read once the close has hung.\n const connections: {\n name: string\n closeTimeout: number\n closeIfConnected: () => Promise<void>\n stdioPid?: () => number | null\n }[] = []\n const tools: Record<string, ToolDef> = {}\n // Insertion-ordered (config order) so the rendered system-prompt section is\n // byte-stable across runs. Only populated from eager-connect successes;\n // `lazyConnect: true` servers do NOT contribute here because pulling their\n // instructions would force the connection the host asked to defer.\n const instructions = new Map<string, string>()\n const errors: { name: string, error: Error }[] = []\n let closed = false\n\n // Bootstrap every server in parallel. Previously this was a sequential for-loop,\n // which meant a single slow server (GitHub MCP, cold streamable-http endpoints,\n // anything on a flaky network) blocked the whole first `agent.run()` for up to\n // N × bootstrapTimeout.\n //\n // `Promise.allSettled` (not `Promise.all`) is the final safety net for the\n // partial-failure tolerance: `bootstrapServer` already internalises every\n // expected throw, but a future regression (or a misbehaving hook that\n // sneaks past `safeCallHook`) would otherwise reject the whole batch and\n // wipe out every other server's connection. Converting a rejection back\n // into an `{ ok: false }` result keeps the batch alive.\n const settled = await Promise.allSettled(configs.map(config => bootstrapServer(config, _clientFactory, hooks, options)))\n const bootstrapResults: BootstrapResult[] = settled.map((result, idx) => {\n if (result.status === 'fulfilled')\n return result.value\n const error = result.reason instanceof Error ? result.reason : new Error(String(result.reason))\n return { ok: false, name: configs[idx]!.name, error, durationMs: 0 }\n })\n\n // Deterministic batch aggregate. The per-server `mcp:bootstrap:end`\n // events above fire in network-completion order inside the parallel\n // bootstrap — fine for live dashboards, hostile to durable-execution\n // hosts that journal hook payloads. One name-sorted event per batch\n // gives them a replay-stable summary without buffer-and-sort glue.\n if (configs.length > 0) {\n const settledResults = bootstrapResults\n .map((result, idx) => {\n const transport = configs[idx]!.transport\n if (result.ok) {\n return {\n name: result.name,\n transport,\n durationMs: result.durationMs,\n ok: true as const,\n toolCount: result.tools.length,\n ...(result.lazy ? { lazy: true } : {}),\n }\n }\n return {\n name: result.name,\n transport,\n durationMs: result.durationMs,\n ok: false as const,\n ...(result.skipped ? { skipped: true } : {}),\n ...('error' in result && result.error ? { error: result.error } : {}),\n }\n })\n .sort((a, b) => a.name.localeCompare(b.name))\n await safeCallHook(hooks, 'mcp:bootstrap:settled', { results: settledResults })\n }\n\n for (const result of bootstrapResults) {\n if (result.skipped) {\n // Auth required but no tokens — already emitted `mcp:auth:required`.\n // Bootstrap continues with the other servers; the user opts in via\n // the host's interactive login path (`loginMcpServer`).\n continue\n }\n if (!result.ok) {\n errors.push({ name: result.name, error: result.error })\n await safeCallHook(hooks, 'mcp:error', { name: result.name, error: result.error })\n continue\n }\n\n const toolNames: string[] = result.tools.map(t => `mcp_${result.config.name}_${t.name}`)\n // Build a per-server `getClient` resolver before assembling the tool\n // defs. Lazy servers wrap it with a one-shot `mcp:connect` emitter so\n // the hook fires on the first successful connect (concurrent first\n // calls converge on the same connector — the wrapper guards against\n // double-fires). Eager servers fire `mcp:connect` synchronously below.\n const getClient = result.lazy\n ? wrapConnectWithHookFire(\n () => result.handle.connect(),\n onceFireConnect(result.name, result.config.transport, toolNames, hooks),\n )\n : (() => {\n const client = result.client\n return () => Promise.resolve(client)\n })()\n\n for (let i = 0; i < result.tools.length; i++) {\n tools[toolNames[i]!] = buildMcpToolDef(result.config, getClient, result.tools[i]!, toolNames[i]!, hooks)\n }\n\n const closeTimeout = result.config.closeTimeout ?? DEFAULT_MCP_CLOSE_TIMEOUT_MS\n if (result.lazy) {\n // No `mcp:connect` here — fires on first call via the wrapper above.\n const handle = result.handle\n const name = result.name\n connections.push({\n name,\n closeTimeout,\n closeIfConnected: async () => {\n // Probe lazy state BEFORE emitting `mcp:close` so a server that\n // was never used produces no close-noise. The handle's\n // `closeIfConnected` is a no-op when nothing was started; we\n // emit the hook only when a real close is about to happen.\n await safeCallHook(hooks, 'mcp:close', { name })\n await handle.closeIfConnected()\n },\n ...(result.config.transport === 'stdio' ? { stdioPid: handle.stdioPid } : {}),\n })\n }\n else {\n const eagerClient = result.client\n const eagerTransport = result.transport\n connections.push({\n name: result.name,\n closeTimeout,\n closeIfConnected: async () => {\n await safeCallHook(hooks, 'mcp:close', { name: result.name })\n await eagerClient.close()\n },\n ...(result.config.transport === 'stdio' ? { stdioPid: () => stdioPidOf(eagerTransport) } : {}),\n })\n // Eager-only — see `instructions` declaration above for the lazy\n // exclusion rationale.\n if (result.instructions !== undefined)\n instructions.set(result.name, result.instructions)\n await safeCallHook(hooks, 'mcp:connect', {\n name: result.name,\n transport: result.config.transport,\n tools: toolNames,\n })\n }\n }\n\n // Total failure is tolerated the same way partial failure is — every server\n // already fired `mcp:error` (and `mcp:bootstrap:end` with `ok: false`), so\n // the host can render status / re-auth UI from the hook stream. Throwing\n // here used to wedge `agent.run()` on the user's first message whenever a\n // single misconfigured / expired-token server was the only MCP configured\n // (e.g. Linear's stored token expired → HTTP 401 doesn't match our\n // \"Unauthorized\" prefix check → all-failed → throw → chat shows red bubble\n // instead of the assistant reply). Returning an empty toolset lets the\n // run proceed with no MCP tools; the next bootstrap retries automatically\n // (see `mcpWarmupPromise` reset in `agent.ts`).\n\n return {\n tools,\n instructions,\n close: async () => {\n // Idempotent — double-close on `agent.destroy()` retry should not\n // re-fire the hook or call `client.close()` twice (the stdio transport\n // treats a second close as a no-op but some transports throw).\n if (closed)\n return\n closed = true\n // Bounded per server (`closeTimeout`, default 5s; servers close in\n // parallel so the whole batch is bounded by the slowest one). An\n // unbounded close can wedge `agent.destroy()` indefinitely: a hung\n // `mcp:close` hook listener, a stdio child ignoring stdin EOF, or an\n // SDK version whose transport close awaits child exit forever.\n await Promise.allSettled(connections.map(async (c) => {\n // Capture before closing — see `stdioPid` doc on `connections`.\n const pid = c.stdioPid?.() ?? null\n try {\n await raceWithTimeout(\n () => c.closeIfConnected(),\n c.closeTimeout,\n `MCP server \"${c.name}\" close timed out after ${c.closeTimeout}ms (configurable via \\`closeTimeout\\`).`,\n )\n }\n catch (err) {\n // The abandoned close promise may still be holding a hung stdio\n // child — reap it so destroy() never leaks a subprocess into a\n // long-lived host process.\n killStdioChild(pid)\n if (process.env.ZIDANE_DEBUG)\n process.stderr.write(`[zidane/mcp] close \"${c.name}\" failed: ${errorMessage(err)}\\n`)\n }\n }))\n },\n }\n}\n\n/**\n * Build a one-shot `mcp:connect` emitter for a lazy server. The wrapper\n * around the connector calls this on the first successful connect;\n * concurrent first calls converge on the connector's in-flight promise\n * so the hook still fires exactly once.\n */\nfunction onceFireConnect(\n name: string,\n transport: McpServerConfig['transport'],\n toolNames: string[],\n hooks: Hookable<AgentHooks> | undefined,\n): () => Promise<void> {\n let fired = false\n return async () => {\n if (fired)\n return\n fired = true\n await safeCallHook(hooks, 'mcp:connect', { name, transport, tools: toolNames, lazy: true })\n }\n}\n\n/**\n * Wrap a lazy connector so the first successful resolution fires\n * `mcp:connect` exactly once. Failures don't fire the hook — the\n * connection isn't live yet and a retry will eventually run through the\n * wrapper again. Both the eager and lazy paths reach the tool builder\n * through this resolver shape so `buildMcpToolDef` stays mode-agnostic.\n */\nfunction wrapConnectWithHookFire(\n connect: () => Promise<Client>,\n fire: () => Promise<void>,\n): () => Promise<Client> {\n return async () => {\n const client = await connect()\n await fire()\n return client\n }\n}\n\n/**\n * Discriminated result returned by `bootstrapServer` so `connectMcpServers` can\n * aggregate successes + failures without letting a rejected promise tear down\n * the whole parallel batch.\n *\n * `skipped` is the third state: server needs OAuth login but has no tokens.\n * Not an error (we don't want a noisy `mcp:error`), not a success (no\n * connection). Caller already heard `mcp:auth:required` and decides whether\n * to surface a status row, prompt for login, etc.\n */\n/**\n * Discriminated result returned by `bootstrapServer`. Success carries either\n * an already-connected `client` (the eager path) or a `connect` thunk that\n * builds + connects a fresh client on first call (the `lazyConnect: true`\n * path). The collector translates both into a uniform `getClient` closure\n * for the tool builder so the call-site stays branch-free.\n */\ntype BootstrapResult\n = | { ok: true, skipped?: false, name: string, config: McpServerConfig, lazy: false, client: Client, transport: ClosableTransport, tools: Array<{ name: string, description?: string | null, inputSchema?: unknown }>, instructions?: string, durationMs: number }\n | { ok: true, skipped?: false, name: string, config: McpServerConfig, lazy: true, handle: LazyConnectHandle, tools: Array<{ name: string, description?: string | null, inputSchema?: unknown }>, durationMs: number }\n | { ok: false, skipped?: false, name: string, error: Error, durationMs: number }\n | { ok: false, skipped: true, name: string, durationMs: number }\n\n/**\n * Connect one MCP server and list its tools, wrapped in a single race against\n * `config.bootstrapTimeout` (default 10s). Always emits `mcp:bootstrap:start`\n * before network I/O and `mcp:bootstrap:end` with `durationMs` + outcome, so a\n * host can build a timing view even when every server succeeds.\n *\n * Errors are captured into the `{ ok: false }` result rather than thrown — the\n * parent uses `Promise.all` across every server and we can't let one rejection\n * short-circuit the batch.\n */\nasync function bootstrapServer(\n config: McpServerConfig,\n _clientFactory: McpClientFactory | undefined,\n hooks: Hookable<AgentHooks> | undefined,\n options: ConnectMcpServersOptions | undefined,\n): Promise<BootstrapResult> {\n const start = Date.now()\n // Validate mutually-exclusive filter fields up-front so the consumer gets a\n // typed `mcp:bootstrap:end` failure rather than a silent later-stage drop.\n if (config.enabledTools && config.disabledTools) {\n return emitConfigFailure(\n config,\n hooks,\n `MCP server \"${config.name}\": enabledTools and disabledTools are mutually exclusive — set one or the other, not both.`,\n )\n }\n // Lazy connect requires schemas in hand — without them there's nothing to\n // advertise to the model, so deferring the connection has no purpose.\n if (config.lazyConnect && (!config.cachedTools || config.cachedTools.length === 0)) {\n return emitConfigFailure(\n config,\n hooks,\n `MCP server \"${config.name}\": lazyConnect requires cachedTools (the host must pre-populate the tool schemas so they can be advertised without a connection).`,\n )\n }\n // OAuth + lazy is rejected — see `McpServerConfig.lazyConnect` for the\n // rationale. Surfacing this at bootstrap (rather than at first-call) is\n // load-bearing: the host needs the failure proximate to the misconfig\n // so it can render an auth affordance before the model commits to a\n // tool call that would fail mid-run.\n if (config.lazyConnect && config.auth === 'oauth') {\n return emitConfigFailure(\n config,\n hooks,\n `MCP server \"${config.name}\": lazyConnect is not compatible with auth: 'oauth' — OAuth servers must complete the handshake at bootstrap so a login affordance can surface before the model commits to a tool call.`,\n )\n }\n\n await safeCallHook(hooks, 'mcp:bootstrap:start', { name: config.name, transport: config.transport })\n\n // Build an auth provider when the config asks for OAuth and the host\n // wired a factory. Non-interactive — the SDK uses stored tokens / refresh,\n // never opens a browser during bootstrap. Missing tokens surface as\n // `UnauthorizedError` from `connect()`, which we convert to a skip below.\n const explicitAuth = config.auth === 'oauth'\n const authProvider = explicitAuth ? options?.buildAuthProvider?.(config) : undefined\n\n // Soft warnings emitted by the tolerant client (e.g. a 4xx on\n // `notifications/initialized`). Surfaced on the success arm of\n // `mcp:bootstrap:end` so hosts can render a \"connected with caveats\"\n // status instead of having to subscribe to stderr.\n const warnings: string[] = []\n const onWarning = (message: string) => { warnings.push(message) }\n\n // Lazy fast-path — defer transport + connect to `getClient()`. Schemas\n // come from `cachedTools`; the host owns invalidation. We still emit\n // `mcp:bootstrap:end ok: true, lazy: true` so timing dashboards stay\n // honest (the row appears, just with a near-zero `durationMs`).\n if (config.lazyConnect) {\n const cachedSchemas = config.cachedTools!\n const filteredTools = await applyMcpToolFilters(config, cachedSchemas, hooks)\n const handle = makeLazyConnector(config, _clientFactory, authProvider, hooks)\n const durationMs = Date.now() - start\n await safeCallHook(hooks, 'mcp:bootstrap:end', {\n name: config.name,\n transport: config.transport,\n durationMs,\n ok: true,\n toolCount: filteredTools.length,\n lazy: true,\n cached: true,\n })\n return { ok: true, name: config.name, config, lazy: true, handle, tools: filteredTools, durationMs }\n }\n\n let client: Client | null = null\n let transport: ClosableTransport | null = null\n try {\n // TolerantMcpClient is a drop-in subclass that survives a 4xx on the\n // `notifications/initialized` post (some real-world servers reject it\n // without that breaking subsequent `tools/list` etc.). See\n // ./tolerant-client.ts for the exact semantics.\n client = _clientFactory\n ? _clientFactory({ onWarning })\n : await createTolerantClient({ name: 'zidane', version: '1.0.0' }, { onWarning })\n const currentClient = client\n\n transport = await createTransport(config, authProvider, hooks) as ClosableTransport\n const bootstrapTimeout = config.bootstrapTimeout ?? DEFAULT_MCP_BOOTSTRAP_TIMEOUT_MS\n // When `cachedTools` is provided we still establish the connection\n // (the SDK needs it for `tools/call`) but skip the `tools/list`\n // round-trip — that's the cheaper of the two networking costs on\n // a fast server, but on a slow / chatty one it's the dominant one.\n const cachedSchemas = config.cachedTools\n const mcpTools = await raceWithTimeout(\n async () => {\n await currentClient.connect(transport as Parameters<Client['connect']>[0])\n return cachedSchemas ?? (await currentClient.listTools()).tools\n },\n bootstrapTimeout,\n `MCP server \"${config.name}\" bootstrap timed out after ${bootstrapTimeout}ms (configurable via \\`bootstrapTimeout\\`).`,\n )\n\n // Expose the raw, pre-filter tool list to hosts so a settings UI or\n // a tool-catalog persistence layer can capture every advertised\n // tool — including ones the config's `disabledTools` / `toolFilter`\n // is about to drop. Read-only; mutation goes through\n // `mcp:tools:filter` below. See AgentHooks['mcp:tools:list'] doc.\n await safeCallHook(hooks, 'mcp:tools:list', {\n name: config.name,\n transport: config.transport,\n tools: mcpTools as McpToolSchema[],\n })\n\n // Per-tool filtering — config-side first (static host policy), then the\n // `mcp:tools:filter` hook (runtime / dynamic decisions). Both compose AND-\n // style: a tool needs to pass every stage to land in the agent registry.\n const filteredTools = await applyMcpToolFilters(config, mcpTools, hooks)\n\n // Capture the server-issued `instructions` from the initialize handshake.\n // The SDK stored them on the client during `connect()` (see\n // `tolerant-client.ts`); the public `getInstructions()` returns the\n // string or `undefined`. We trim + nullable-coerce here so the agent's\n // system-prompt builder gets one normalized shape, and a server that\n // sends `\"\"` (empty string) is treated identically to \"no instructions\"\n // rather than emitting a section with an empty body.\n //\n // Defensive: older `_clientFactory` test mocks don't implement\n // `getInstructions` (the method was added to the SDK after the mocks\n // were written). Guard so an unknown function shape degrades to\n // \"no instructions\" instead of crashing bootstrap.\n let rawInstructions: unknown\n if (typeof (currentClient as { getInstructions?: unknown }).getInstructions === 'function') {\n try {\n rawInstructions = (currentClient as { getInstructions: () => unknown }).getInstructions()\n }\n catch {\n rawInstructions = undefined\n }\n }\n const instructions = typeof rawInstructions === 'string' && rawInstructions.trim().length > 0\n ? rawInstructions\n : undefined\n\n const durationMs = Date.now() - start\n await safeCallHook(hooks, 'mcp:bootstrap:end', {\n name: config.name,\n transport: config.transport,\n durationMs,\n ok: true,\n toolCount: filteredTools.length,\n ...(cachedSchemas ? { cached: true } : {}),\n ...(warnings.length > 0 ? { warnings } : {}),\n })\n return {\n ok: true,\n name: config.name,\n config,\n lazy: false,\n client: currentClient,\n transport,\n tools: filteredTools,\n ...(instructions !== undefined ? { instructions } : {}),\n durationMs,\n }\n }\n catch (err) {\n // Close in order: transport first (may not yet be wired into the\n // client when connect() hadn't completed), then client (close()\n // is a no-op on transports that already closed).\n await closeTransportQuietly(transport)\n await closeClientQuietly(client)\n const rawError = err instanceof Error ? err : new Error(String(err))\n const error = withTransportMismatchHint(config.transport, rawError)\n const durationMs = Date.now() - start\n\n // OAuth path — convert \"needs login\" into a skip instead of an error.\n // Two cases reach here:\n // 1. Explicit `auth: 'oauth'` with no stored tokens (provider built,\n // SDK threw UnauthorizedError when none could be refreshed).\n // 2. No auth flag, but the server returned 401 + RFC 9728 metadata\n // AND the user did not provide a static Authorization header\n // (the headers escape hatch — see McpServerConfig.auth docs).\n // The auto-promote path requires `buildAuthProvider` so the host\n // can complete the login. Without it, this falls through as a\n // regular bootstrap error.\n if (isUnauthorizedError(err)) {\n const eligibleAutoPromote = !explicitAuth\n && !hasAuthorizationHeader(config.headers)\n && !!options?.buildAuthProvider\n if (explicitAuth || eligibleAutoPromote) {\n await safeCallHook(hooks, 'mcp:bootstrap:end', {\n name: config.name,\n transport: config.transport,\n durationMs,\n ok: false,\n error,\n })\n await safeCallHook(hooks, 'mcp:auth:required', {\n name: config.name,\n transport: config.transport,\n reason: explicitAuth ? 'no-tokens' : 'auto-promoted',\n })\n return { ok: false, skipped: true, name: config.name, durationMs }\n }\n }\n\n await safeCallHook(hooks, 'mcp:bootstrap:end', {\n name: config.name,\n transport: config.transport,\n durationMs,\n ok: false,\n error,\n })\n return { ok: false, name: config.name, error, durationMs }\n }\n}\n\n/**\n * Emit the start/end hooks for a config-level failure and return the error\n * result. Centralised so the various validation branches (enabled/disabled\n * mutex, lazyConnect-without-cachedTools, lazyConnect+OAuth) stay readable.\n */\nasync function emitConfigFailure(\n config: McpServerConfig,\n hooks: Hookable<AgentHooks> | undefined,\n message: string,\n): Promise<BootstrapResult> {\n const error = new Error(message)\n await safeCallHook(hooks, 'mcp:bootstrap:start', { name: config.name, transport: config.transport })\n await safeCallHook(hooks, 'mcp:bootstrap:end', {\n name: config.name,\n transport: config.transport,\n durationMs: 0,\n ok: false,\n error,\n })\n return { ok: false, name: config.name, error, durationMs: 0 }\n}\n\n/**\n * Per-server lazy bootstrap handle returned by {@link makeLazyConnector}.\n *\n * - `connect()` triggers (or reuses the in-flight) transport build +\n * `client.connect()`. Concurrent callers converge on a single promise;\n * a rejection drops the cached promise so the next call retries.\n * - `closeIfConnected()` closes the live client when one exists and\n * short-circuits when the server was never used. Always awaits a\n * pending connect first so a `destroy()` racing with a first\n * `tools/call` cannot leak the freshly-built client.\n */\ninterface LazyConnectHandle {\n connect: () => Promise<Client>\n closeIfConnected: () => Promise<void>\n /**\n * PID of the live stdio child, or `null` when not connected / not stdio /\n * already exited. Read by the bounded `close()` before initiating the\n * close (the SDK's stdio transport drops its process reference the moment\n * its own close starts).\n */\n stdioPid: () => number | null\n}\n\n/**\n * Build a memoized lazy connector + matching close handle for a\n * `lazyConnect: true` server. State lives in the closure: the connector\n * tracks its own in-flight and resolved-client state so the close path\n * can reason about \"never connected\" vs \"in flight\" vs \"live\" without\n * forcing a connect from inside `close()`.\n *\n * Concurrency contract:\n * - First `connect()` builds + connects a fresh client. Concurrent\n * callers converge on the same in-flight promise.\n * - On rejection, the cached promise is dropped so the next caller\n * retries with a fresh client instance.\n * - `closeIfConnected()` awaits any pending connect, closes the\n * resulting client, and marks the handle destroyed. Any subsequent\n * `connect()` rejects synchronously so a tool call racing the\n * shutdown can't leak a freshly-built client past the close.\n */\nfunction makeLazyConnector(\n config: McpServerConfig,\n _clientFactory: McpClientFactory | undefined,\n authProvider: OAuthClientProvider | undefined,\n hooks: Hookable<AgentHooks> | undefined,\n): LazyConnectHandle {\n let inFlight: Promise<Client> | null = null\n let live: Client | null = null\n let liveTransport: ClosableTransport | null = null\n let destroyed = false\n\n const connect = (): Promise<Client> => {\n if (destroyed) {\n return Promise.reject(new Error(\n `MCP server \"${config.name}\": cannot connect after close()`,\n ))\n }\n if (live)\n return Promise.resolve(live)\n if (inFlight)\n return inFlight\n const pending = (async () => {\n // Match the eager path's timeout — a lazy connect that hangs\n // would wedge a tool call indefinitely without it.\n const bootstrapTimeout = config.bootstrapTimeout ?? DEFAULT_MCP_BOOTSTRAP_TIMEOUT_MS\n const client = _clientFactory\n ? _clientFactory()\n : await createTolerantClient({ name: 'zidane', version: '1.0.0' })\n let transport: ClosableTransport | null = null\n try {\n transport = await createTransport(config, authProvider, hooks) as ClosableTransport\n await raceWithTimeout(\n () => client.connect(transport as Parameters<Client['connect']>[0]),\n bootstrapTimeout,\n `MCP server \"${config.name}\" lazy connect timed out after ${bootstrapTimeout}ms (configurable via \\`bootstrapTimeout\\`).`,\n )\n // Shutdown ran while we were connecting — close the freshly\n // built client right here so it doesn't leak past destroy().\n if (destroyed) {\n await closeTransportQuietly(transport)\n await closeClientQuietly(client)\n throw new Error(`MCP server \"${config.name}\": closed during connect`)\n }\n live = client\n liveTransport = transport\n return client\n }\n catch (err) {\n // Close in order: transport first (may not be wired into the\n // client yet on a connect failure), then client.\n await closeTransportQuietly(transport)\n await closeClientQuietly(client)\n throw err instanceof Error\n ? withTransportMismatchHint(config.transport, err)\n : err\n }\n })()\n inFlight = pending\n // Clear the in-flight cache on either outcome — success leaves the\n // resolved client cached on `live`; failure drops the slot so the\n // next caller retries against a fresh client instance, matching the\n // bootstrap-level retry behavior in `agent.ts`'s `mcpWarmupPromise`.\n pending.then(\n () => { inFlight = null },\n () => { inFlight = null },\n )\n return pending\n }\n\n const closeIfConnected = async (): Promise<void> => {\n destroyed = true\n // Race-safe drain: if a connect is in flight, wait for it to settle\n // before deciding whether to close. Successful resolution promotes\n // the client to `live`; failure leaves both `live` and `inFlight`\n // null. Either way, after the await the state is terminal and the\n // close decision is unambiguous.\n if (inFlight) {\n try { await inFlight }\n catch { /* connect failed; nothing to close */ }\n }\n if (live) {\n const client = live\n live = null\n await client.close()\n }\n }\n\n return {\n connect,\n closeIfConnected,\n stdioPid: () => stdioPidOf(liveTransport),\n }\n}\n\n/**\n * Apply config-side filters (`enabledTools` / `disabledTools` / `toolFilter`)\n * and then the `mcp:tools:filter` hook to the upstream tool list.\n *\n * Composition order — narrowest-to-widest:\n * 1. Allow-list (`enabledTools`) — keep only listed tools.\n * 2. Deny-list (`disabledTools`) — drop listed tools.\n * 3. Predicate (`toolFilter`) — fine-grained metadata filtering.\n * 4. Hook (`mcp:tools:filter`) — runtime / per-host decisions.\n *\n * The mutual exclusion of `enabledTools` and `disabledTools` is checked in\n * `bootstrapServer` so this stays a pure data transform.\n */\nasync function applyMcpToolFilters(\n config: McpServerConfig,\n tools: Array<{ name: string, description?: string | null, inputSchema?: unknown }>,\n hooks: Hookable<AgentHooks> | undefined,\n): Promise<Array<{ name: string, description?: string | null, inputSchema?: unknown }>> {\n let filtered = tools\n\n if (config.enabledTools && config.enabledTools.length > 0) {\n const allow = new Set(config.enabledTools)\n filtered = filtered.filter(t => allow.has(t.name))\n }\n\n if (config.disabledTools && config.disabledTools.length > 0) {\n const deny = new Set(config.disabledTools)\n filtered = filtered.filter(t => !deny.has(t.name))\n }\n\n if (config.toolFilter) {\n const predicate = config.toolFilter\n filtered = filtered.filter(t => predicate(t))\n }\n\n if (hooks) {\n // Pass a fresh array so handlers cannot retroactively mutate the caller's\n // upstream list; mutations they make to `ctx.tools` are scoped to this turn.\n const ctx = { server: config.name, transport: config.transport, tools: [...filtered] }\n await hooks.callHook('mcp:tools:filter', ctx)\n filtered = ctx.tools\n }\n\n return filtered\n}\n\n/**\n * Build the zidane `ToolDef` that wraps a single MCP tool. Extracted so the\n * parallel bootstrap path can assemble tools from a results array without\n * inlining a 70-line closure inside the collector loop.\n *\n * `getClient` resolves to a live `Client` ready for `tools/call`. For\n * eagerly-bootstrapped servers it's a constant resolved promise; for\n * `lazyConnect: true` servers it's the memoized connector built by\n * {@link makeLazyConnector} — the first invocation pays the connect cost,\n * concurrent / subsequent calls observe the cached client. Reconnects /\n * swap-outs go through a fresh `connectMcpServers` call rather than\n * rewiring the tool in place.\n */\n/**\n * Coerce an MCP-advertised `inputSchema` to a plain object schema. The value is\n * `unknown` (live `listTools` is zod-checked upstream, but `cachedTools` and the\n * tolerant client bypass that), so a server/cache that serialized a string,\n * array or null would otherwise be cast verbatim and poison the whole provider\n * request (`tools[n].input_schema: expected object`). Degrade that one tool to\n * a fresh empty object schema instead of failing the run. (Fresh per call so a\n * downstream in-place mutation can't bleed across degraded tools.)\n */\nfunction toObjectSchema(schema: unknown): Record<string, unknown> {\n if (typeof schema === 'object' && schema !== null && !Array.isArray(schema))\n return schema as Record<string, unknown>\n return { type: 'object', properties: {} }\n}\n\nfunction buildMcpToolDef(\n config: McpServerConfig,\n getClient: () => Promise<Client>,\n tool: { name: string, description?: string | null, inputSchema?: unknown },\n namespacedName: string,\n hooks: Hookable<AgentHooks> | undefined,\n): ToolDef {\n return {\n spec: {\n name: namespacedName,\n description: tool.description || '',\n inputSchema: toObjectSchema(tool.inputSchema),\n },\n execute: async (input: Record<string, unknown>, ctx: ToolContext) => {\n const { turnId, callId, signal } = ctx\n const displayName = ctx.toolAliases?.[namespacedName] ?? namespacedName\n\n // Shared identity fields for every `mcp:tool:*` hook ctx the\n // wrapper fires. Mirrors `runId / parentRunId / depth` from the\n // tool context so subagent observability stays consistent\n // between `tool:*` and `mcp:tool:*` events.\n const identity = {\n ...(ctx.runId !== undefined ? { runId: ctx.runId } : {}),\n ...(ctx.parentRunId !== undefined ? { parentRunId: ctx.parentRunId } : {}),\n ...(typeof ctx.depth === 'number' ? { depth: ctx.depth } : {}),\n }\n\n // Gate — block MCP tool execution or substitute a synthetic result.\n const gateCtx: McpToolHookContext & {\n block: boolean\n reason: string\n result?: string | ToolResultContent[]\n } = {\n turnId,\n callId,\n server: config.name,\n tool: tool.name,\n displayName,\n input,\n ...identity,\n block: false,\n reason: 'MCP tool execution was blocked',\n }\n await hooks?.callHook('mcp:tool:gate', gateCtx)\n\n // Conflict resolution mirrors the loop's `tool:gate`: when both `block`\n // and `result` are set (e.g. a policy gate refuses on top of a consumer\n // cache substitute), `block` wins.\n if (gateCtx.block)\n return `Blocked: ${gateCtx.reason}`\n\n const effectiveInput = gateCtx.input\n\n // MCP gate `result` substitute — skip the upstream callTool, fire the\n // transform + after hooks so consumers see the substitute consistently\n // with normally-executed calls.\n if (gateCtx.result !== undefined) {\n let substitute: string | ToolResultContent[] = gateCtx.result\n const transformCtx = {\n turnId,\n callId,\n server: config.name,\n tool: tool.name,\n displayName,\n input: effectiveInput,\n ...identity,\n result: substitute,\n outputBytes: toolOutputByteLength(substitute),\n }\n await hooks?.callHook('mcp:tool:transform', transformCtx)\n substitute = transformCtx.result\n await hooks?.callHook('mcp:tool:after', {\n turnId,\n callId,\n server: config.name,\n tool: tool.name,\n displayName,\n input: effectiveInput,\n ...identity,\n result: substitute,\n outputBytes: toolOutputByteLength(substitute),\n })\n return substitute\n }\n await hooks?.callHook('mcp:tool:before', {\n turnId,\n callId,\n server: config.name,\n tool: tool.name,\n displayName,\n input: effectiveInput,\n ...identity,\n })\n // Per-tool override > per-server default > 30s. Slow-but-legitimate\n // operations (DDL migrations, deploys) need more headroom than fast\n // metadata calls on the same server.\n const timeout = config.toolTimeouts?.[tool.name] ?? config.toolTimeout ?? 30_000\n try {\n // Resolve the live client. For eager servers this is a no-op\n // (constant resolved promise); for `lazyConnect: true` servers\n // this triggers the deferred connect on first call and reuses\n // the cached client afterward.\n const client = await getClient()\n // Race the call against the configured timeout AND the run-level\n // abort signal. Aborting the run has to propagate into MCP calls\n // — otherwise a hung stdio server keeps the tool call alive\n // until `destroy()` eventually closes the client, which can be\n // minutes if the agent is waiting on `waitForIdle()`.\n const result = await raceWithTimeoutAndSignal(\n // The SDK's `RequestOptions.signal` sends a `notifications/cancelled`\n // to the server and rejects the in-flight RPC — so a timeout / run\n // abort actually cancels the remote call instead of orphaning it\n // (matters for streamable-http transports, where there is no\n // subprocess to kill on `close()`).\n rpcSignal => client.callTool({ name: tool.name, arguments: effectiveInput }, undefined, { signal: rpcSignal }),\n timeout,\n // The steering tail matters: a bare \"timed out\" reads as a transient\n // failure and models immediately retry the identical call — which\n // for non-idempotent operations (migrations, deploys) compounds the\n // damage and feeds retry loops.\n `MCP tool \"${tool.name}\" on server \"${config.name}\" timed out after ${timeout}ms. `\n + `The server may still be processing the request — verify its effect before retrying, `\n + `and do not immediately retry the identical call.`,\n signal,\n )\n let output: string | ToolResultContent[] = packMcpResult(result.content)\n\n // In-band MCP failure: per spec, servers report tool-level errors via\n // `CallToolResult.isError` with the diagnostic in `content`. Throw so\n // the failure routes through the same `mcp:tool:error` + loop error\n // paths as a transport-level failure — otherwise the model sees the\n // error text as a *successful* tool result.\n if (result.isError === true) {\n const detail = (typeof output === 'string' ? output : toolResultToText(output)).trim()\n throw new Error(detail || `MCP tool \"${tool.name}\" on server \"${config.name}\" reported an error with no diagnostic content`)\n }\n\n // Transform — mutate result before returning. `outputBytes` reflects\n // the size before any consumer mutation so a truncation hook can size-budget.\n const transformCtx = {\n turnId,\n callId,\n server: config.name,\n tool: tool.name,\n displayName,\n input: effectiveInput,\n ...identity,\n result: output,\n outputBytes: toolOutputByteLength(output),\n }\n await hooks?.callHook('mcp:tool:transform', transformCtx)\n output = transformCtx.result\n\n await hooks?.callHook('mcp:tool:after', {\n turnId,\n callId,\n server: config.name,\n tool: tool.name,\n displayName,\n input: effectiveInput,\n ...identity,\n result: output,\n outputBytes: toolOutputByteLength(output),\n })\n return output\n }\n catch (err) {\n const error = err instanceof Error ? err : new Error(String(err))\n // Route through safeCallHook so a throwing listener can't mask the\n // original tool error (which we rethrow below).\n await safeCallHook(hooks, 'mcp:tool:error', {\n turnId,\n callId,\n server: config.name,\n tool: tool.name,\n displayName,\n input: effectiveInput,\n ...identity,\n error,\n })\n await safeCallHook(hooks, 'mcp:tool:after', {\n turnId,\n callId,\n server: config.name,\n tool: tool.name,\n displayName,\n input: effectiveInput,\n ...identity,\n result: error.message,\n outputBytes: Buffer.byteLength(error.message),\n })\n throw error\n }\n },\n }\n}\n\nasync function closeClientQuietly(client: Pick<Client, 'close'> | null | undefined): Promise<void> {\n if (!client)\n return\n try {\n await client.close()\n }\n catch {\n // Best-effort cleanup — original bootstrap error is more actionable.\n }\n}\n\nasync function raceWithTimeout<T>(\n task: () => Promise<T>,\n timeoutMs: number,\n timeoutMessage: string,\n): Promise<T> {\n let timer: ReturnType<typeof setTimeout> | undefined\n try {\n return await new Promise<T>((resolvePromise, rejectPromise) => {\n timer = setTimeout(() => rejectPromise(new Error(timeoutMessage)), timeoutMs)\n task().then(resolvePromise, rejectPromise)\n })\n }\n finally {\n if (timer)\n clearTimeout(timer)\n }\n}\n\n/**\n * Race a promise-returning task against (a) a timeout and (b) an optional\n * abort signal. Cleans up its timer and abort listener on every exit path.\n *\n * The task receives a dedicated `AbortSignal` that fires on either trigger.\n * Callers thread it into the MCP SDK's `RequestOptions.signal` so a timeout\n * or run-abort cancels the underlying RPC (the SDK sends\n * `notifications/cancelled`) instead of leaving it running server-side —\n * which matters for streamable-http transports, where `connection.close()`\n * has no subprocess to kill.\n */\nasync function raceWithTimeoutAndSignal<T>(\n task: (signal: AbortSignal) => Promise<T>,\n timeoutMs: number,\n timeoutMessage: string,\n signal: AbortSignal | undefined,\n): Promise<T> {\n if (signal?.aborted)\n throw new Error('MCP tool call aborted')\n\n const rpcAbort = new AbortController()\n let timer: ReturnType<typeof setTimeout> | undefined\n let onAbort: (() => void) | undefined\n try {\n return await new Promise<T>((resolvePromise, rejectPromise) => {\n timer = setTimeout(() => {\n rejectPromise(new Error(timeoutMessage))\n rpcAbort.abort()\n }, timeoutMs)\n if (signal) {\n onAbort = () => {\n rejectPromise(new Error('MCP tool call aborted'))\n rpcAbort.abort()\n }\n signal.addEventListener('abort', onAbort, { once: true })\n }\n task(rpcAbort.signal).then(resolvePromise, rejectPromise)\n })\n }\n finally {\n if (timer)\n clearTimeout(timer)\n if (signal && onAbort)\n signal.removeEventListener('abort', onAbort)\n }\n}\n"],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