@ai-sdk/harness-claude-code 1.0.3 → 1.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +8 -0
- package/dist/bridge/index.mjs +1 -1
- package/dist/bridge/index.mjs.map +1 -1
- package/dist/index.d.ts +52 -64
- package/dist/index.js +5 -4
- package/dist/index.js.map +1 -1
- package/package.json +6 -3
- package/src/bridge/index.ts +1 -1
- package/src/claude-code-harness.ts +5 -4
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/claude-code-harness.ts","../src/claude-code-auth.ts","../src/claude-code-bridge-protocol.ts","../src/index.ts"],"sourcesContent":["import { randomBytes } from 'node:crypto';\nimport { readFile } from 'node:fs/promises';\nimport path from 'node:path';\nimport { fileURLToPath } from 'node:url';\nimport {\n commonTool,\n harnessV1DiagnosticFromBridgeFrame,\n HarnessCapabilityUnsupportedError,\n type HarnessV1,\n type HarnessV1Bootstrap,\n type HarnessV1DebugConfig,\n type HarnessV1BuiltinTool,\n type HarnessV1ContinueTurnState,\n type HarnessV1PermissionMode,\n type HarnessV1Prompt,\n type HarnessV1PromptControl,\n type HarnessV1ResumeSessionState,\n type HarnessV1NetworkSandboxSession,\n type HarnessV1Session,\n type HarnessV1Skill,\n type HarnessV1StreamPart,\n} from '@ai-sdk/harness';\nimport {\n classifyDiskLog,\n markBridgeStarting,\n SandboxChannel,\n waitForBridgeReady,\n} from '@ai-sdk/harness/utils';\nimport {\n tool,\n type Experimental_SandboxSession,\n type Experimental_SandboxProcess,\n} from '@ai-sdk/provider-utils';\nimport { WebSocket } from 'ws';\nimport { z } from 'zod';\nimport {\n resolveClaudeCodeEnv,\n type ClaudeCodeAuthOptions,\n} from './claude-code-auth';\nimport {\n outboundMessageSchema,\n type InboundMessage,\n type OutboundMessage,\n} from './claude-code-bridge-protocol';\n\ntype ClaudeCodeChannel = SandboxChannel<OutboundMessage, InboundMessage>;\ntype ClaudeCodeRespawnStrategy = 'replay' | 'rerun';\n\nexport type ClaudeCodeHarnessSettings = {\n readonly auth?: ClaudeCodeAuthOptions;\n /**\n * Anthropic model id the underlying `claude` CLI should use. Leaving this\n * unset defers to the CLI's default.\n */\n readonly model?: string;\n /**\n * Hard cap on how many internal turns the CLI can take before yielding\n * back to the caller. Unset means the CLI's default.\n */\n readonly maxTurns?: number;\n /**\n * Controls extended-thinking behavior. `'off'` disables thinking,\n * `'on'` forces it on, `'adaptive'` lets the runtime decide.\n */\n readonly thinking?: 'off' | 'on' | 'adaptive';\n /**\n * Override the port the bridge binds inside the sandbox. By default the\n * adapter uses the first port the sandbox declares via `sandbox.ports`.\n * Only set this if the sandbox declares multiple ports and the first one\n * is reserved for something else.\n */\n readonly port?: number;\n /** Maximum milliseconds to wait for the bridge to advertise its port. Defaults to 120000. */\n readonly startupTimeoutMs?: number;\n};\n\n/*\n * Every native tool the Claude Code CLI can invoke, declared as a `ToolSet`\n * keyed by what the bridge emits as `toolName` on the wire\n * (`commonName ?? nativeName`). Schemas transcribed from\n * `@anthropic-ai/claude-agent-sdk`'s `agentSdkTypes.d.ts`.\n *\n * `MCP` (the generic proxy tool inside the Claude Code SDK) is intentionally\n * omitted — the bridge filters out `mcp__harness-tools__*` tool names before\n * emitting them, and other MCP invocations come through with their own\n * server-tool names rather than the literal `'Mcp'` token.\n */\nconst CLAUDE_CODE_BUILTIN_TOOLS = {\n read: commonTool('read', {\n nativeName: 'Read',\n toolUseKind: 'readonly',\n description: 'Read file contents (text, image, PDF, notebook)',\n inputSchema: z.object({\n file_path: z.string(),\n offset: z.number().optional(),\n limit: z.number().optional(),\n pages: z.string().optional(),\n }),\n }),\n write: commonTool('write', {\n nativeName: 'Write',\n toolUseKind: 'edit',\n description: 'Overwrite or create a file at an absolute path',\n inputSchema: z.object({\n file_path: z.string(),\n content: z.string(),\n }),\n }),\n edit: commonTool('edit', {\n nativeName: 'Edit',\n toolUseKind: 'edit',\n description: 'Edit a file by exact string replacement',\n inputSchema: z.object({\n file_path: z.string(),\n old_string: z.string(),\n new_string: z.string(),\n replace_all: z.boolean().optional(),\n }),\n }),\n bash: commonTool('bash', {\n nativeName: 'Bash',\n toolUseKind: 'bash',\n description: 'Execute a shell command, optionally in background',\n inputSchema: z.object({\n command: z.string(),\n timeout: z.number().optional(),\n description: z.string().optional(),\n run_in_background: z.boolean().optional(),\n dangerouslyDisableSandbox: z.boolean().optional(),\n }),\n }),\n glob: commonTool('glob', {\n nativeName: 'Glob',\n toolUseKind: 'readonly',\n description: 'Fast file-pattern search using glob syntax',\n inputSchema: z.object({\n pattern: z.string(),\n path: z.string().optional(),\n }),\n }),\n grep: commonTool('grep', {\n nativeName: 'Grep',\n toolUseKind: 'readonly',\n description: 'Regex search over file contents via ripgrep',\n inputSchema: z.object({\n pattern: z.string(),\n path: z.string().optional(),\n glob: z.string().optional(),\n output_mode: z\n .enum(['content', 'files_with_matches', 'count'])\n .optional(),\n '-B': z.number().optional(),\n '-A': z.number().optional(),\n '-C': z.number().optional(),\n context: z.number().optional(),\n '-n': z.boolean().optional(),\n '-i': z.boolean().optional(),\n '-o': z.boolean().optional(),\n type: z.string().optional(),\n head_limit: z.number().optional(),\n offset: z.number().optional(),\n multiline: z.boolean().optional(),\n }),\n }),\n webSearch: commonTool('webSearch', {\n nativeName: 'WebSearch',\n toolUseKind: 'readonly',\n description: 'Issue web search queries with optional domain filters',\n inputSchema: z.object({\n query: z.string(),\n allowed_domains: z.array(z.string()).optional(),\n blocked_domains: z.array(z.string()).optional(),\n }),\n }),\n\n WebFetch: tool({\n description: 'Fetch a URL and run a prompt against its content',\n inputSchema: z.object({\n url: z.string(),\n prompt: z.string(),\n }),\n }),\n NotebookEdit: tool({\n description: 'Edit, insert, or delete a Jupyter notebook cell',\n inputSchema: z.object({\n notebook_path: z.string(),\n new_source: z.string(),\n cell_id: z.string().optional(),\n cell_type: z.enum(['code', 'markdown']).optional(),\n edit_mode: z.enum(['replace', 'insert', 'delete']).optional(),\n }),\n }),\n TodoWrite: tool({\n description: 'Replace the session todo list',\n inputSchema: z.object({\n todos: z.array(\n z.object({\n content: z.string(),\n status: z.enum(['pending', 'in_progress', 'completed']),\n activeForm: z.string(),\n }),\n ),\n }),\n }),\n Agent: tool({\n description: 'Spawn a subagent with a task',\n inputSchema: z.object({\n description: z.string(),\n prompt: z.string(),\n subagent_type: z.string().optional(),\n model: z.enum(['sonnet', 'opus', 'haiku']).optional(),\n run_in_background: z.boolean().optional(),\n name: z.string().optional(),\n team_name: z.string().optional(),\n mode: z\n .enum([\n 'acceptEdits',\n 'auto',\n 'bypassPermissions',\n 'default',\n 'dontAsk',\n 'plan',\n ])\n .optional(),\n isolation: z.literal('worktree').optional(),\n }),\n }),\n TaskCreate: tool({\n description: 'Create a task in the session-local task list',\n inputSchema: z.object({\n subject: z.string(),\n description: z.string(),\n activeForm: z.string().optional(),\n metadata: z.record(z.unknown()).optional(),\n }),\n }),\n TaskGet: tool({\n description: 'Retrieve a task by id',\n inputSchema: z.object({ taskId: z.string() }),\n }),\n TaskUpdate: tool({\n description: 'Update fields of an existing task',\n inputSchema: z.object({\n taskId: z.string(),\n subject: z.string().optional(),\n description: z.string().optional(),\n activeForm: z.string().optional(),\n status: z\n .union([\n z.enum(['pending', 'in_progress', 'completed']),\n z.literal('deleted'),\n ])\n .optional(),\n addBlocks: z.array(z.string()).optional(),\n addBlockedBy: z.array(z.string()).optional(),\n owner: z.string().optional(),\n metadata: z.record(z.unknown()).optional(),\n }),\n }),\n TaskList: tool({\n description: 'Return all tasks in the session-local task list',\n inputSchema: z.object({}),\n }),\n TaskStop: tool({\n description: 'Stop a running background task by id',\n inputSchema: z.object({\n task_id: z.string().optional(),\n shell_id: z.string().optional(),\n }),\n }),\n TaskOutput: tool({\n description: 'Poll for output from a background task',\n inputSchema: z.object({\n task_id: z.string(),\n block: z.boolean(),\n timeout: z.number(),\n }),\n }),\n ListMcpResources: tool({\n description: 'List resources available from MCP servers',\n inputSchema: z.object({ server: z.string().optional() }),\n }),\n ReadMcpResource: tool({\n description: 'Read a specific MCP resource by URI',\n inputSchema: z.object({ server: z.string(), uri: z.string() }),\n }),\n ExitPlanMode: tool({\n description: 'Exit plan mode with optional permission approvals',\n inputSchema: z\n .object({\n allowedPrompts: z\n .array(\n z.object({\n tool: z.literal('Bash'),\n prompt: z.string(),\n }),\n )\n .optional(),\n })\n .passthrough(),\n }),\n EnterWorktree: tool({\n description: 'Create or enter an isolated git worktree',\n inputSchema: z.object({\n name: z.string().optional(),\n path: z.string().optional(),\n }),\n }),\n ExitWorktree: tool({\n description: 'Exit the current worktree session',\n inputSchema: z.object({\n action: z.enum(['keep', 'remove']),\n discard_changes: z.boolean().optional(),\n }),\n }),\n AskUserQuestion: tool({\n description: 'Ask the user multiple-choice questions via a structured UI',\n inputSchema: z.object({\n questions: z\n .array(\n z.object({\n question: z.string(),\n header: z.string(),\n options: z.array(\n z.object({\n label: z.string(),\n description: z.string(),\n preview: z.string().optional(),\n }),\n ),\n multiSelect: z.boolean(),\n }),\n )\n .min(1)\n .max(4),\n answers: z.record(z.string()).optional(),\n annotations: z\n .record(\n z.object({\n preview: z.string().optional(),\n notes: z.string().optional(),\n }),\n )\n .optional(),\n metadata: z.object({ source: z.string().optional() }).optional(),\n }),\n }),\n Skill: tool({\n description: 'Activate a skill by name',\n inputSchema: z.object({\n skill: z.string(),\n args: z.string().optional(),\n }),\n }),\n} as const satisfies Record<string, HarnessV1BuiltinTool<any, any>>;\n\n/*\n * Bootstrap lives in /tmp because it's pure derived state — the harness can\n * reinstall the CLI and bridge files on any fresh sandbox from the recipe.\n * Persistence comes from the sandbox provider's snapshot, not the path.\n *\n * The session work dir (`startOpts.sessionWorkDir`) and the bridge-state dir\n * derived from `sandboxSession.defaultWorkingDirectory` both live under the sandbox's\n * default working directory — the provider's persistent mount — so the\n * workdir's CLI state (Claude's `~/.claude/projects/<dir>/*.jsonl` thread\n * history is keyed by working directory) and the bridge state files survive\n * both detach -> attach/replay and stop -> snapshot -> resume cycles.\n */\nconst BOOTSTRAP_DIR = '/tmp/harness/claude-code';\n\n/**\n * Live bridge coordinates returned by `doDetach()` and `doSuspendTurn()`. A\n * future process uses them to reopen a socket to the still-running bridge\n * (`attach`) instead of re-spawning it. Absent on a `doStop()` payload.\n */\nconst claudeCodeBridgeCoordsSchema = z.object({\n port: z.number(),\n token: z.string(),\n lastSeenEventId: z.number(),\n sandboxId: z.string().optional(),\n});\n\n/**\n * Schema for the adapter-specific portion of lifecycle state `data`.\n *\n * A `doStop()` payload is structurally empty (`{}`): the framework derives the\n * sandbox via `provider.resumeSession({ sessionId })`, and the Claude SDK's\n * `{ continue: true }` flag rehydrates the thread from the workdir. A\n * `doDetach()` payload additionally carries `bridge` coordinates for\n * cross-process `attach`. `.passthrough()` keeps both shapes valid.\n */\nconst claudeCodeResumeStateSchema = z\n .object({ bridge: claudeCodeBridgeCoordsSchema.optional() })\n .passthrough();\n\ntype ClaudeCodeBridgeCoords = z.infer<typeof claudeCodeBridgeCoordsSchema>;\n\nexport function createClaudeCode(\n settings: ClaudeCodeHarnessSettings = {},\n): HarnessV1<typeof CLAUDE_CODE_BUILTIN_TOOLS> {\n let cachedBootstrap: HarnessV1Bootstrap | undefined;\n\n return {\n specificationVersion: 'harness-v1',\n harnessId: 'claude-code',\n builtinTools: CLAUDE_CODE_BUILTIN_TOOLS,\n supportsBuiltinToolApprovals: true,\n lifecycleStateSchema: claudeCodeResumeStateSchema,\n getBootstrap: async () => {\n if (cachedBootstrap != null) return cachedBootstrap;\n const [pkg, lock, bridge] = await Promise.all([\n readBridgeAsset('package.json'),\n readBridgeAsset('pnpm-lock.yaml'),\n readBridgeAsset('index.mjs'),\n ]);\n cachedBootstrap = {\n harnessId: 'claude-code',\n bootstrapDir: BOOTSTRAP_DIR,\n files: [\n { path: `${BOOTSTRAP_DIR}/package.json`, content: pkg },\n { path: `${BOOTSTRAP_DIR}/pnpm-lock.yaml`, content: lock },\n { path: `${BOOTSTRAP_DIR}/bridge.mjs`, content: bridge },\n ],\n commands: [\n { command: `mkdir -p ${BOOTSTRAP_DIR}` },\n {\n command: `pnpm --dir ${BOOTSTRAP_DIR} install --frozen-lockfile --store-dir ${BOOTSTRAP_DIR}/.pnpm-store`,\n },\n {\n command: `cd ${BOOTSTRAP_DIR} && if [ -f node_modules/@anthropic-ai/claude-code/install.cjs ]; then node node_modules/@anthropic-ai/claude-code/install.cjs; fi && ./node_modules/.bin/claude --version`,\n },\n ],\n };\n return cachedBootstrap;\n },\n doStart: async startOpts => {\n const sandboxSession = startOpts.sandboxSession;\n const session = sandboxSession.restricted();\n const sandboxId = sandboxSession.id;\n const lifecycleState = startOpts.continueFrom ?? startOpts.resumeFrom;\n const isResume = lifecycleState != null;\n const isContinue = startOpts.continueFrom != null;\n const coords = isResume\n ? (lifecycleState?.data as { bridge?: ClaudeCodeBridgeCoords })?.bridge\n : undefined;\n\n const workDir = startOpts.sessionWorkDir;\n const sessionDataDir = `${sandboxSession.defaultWorkingDirectory}/.agent-runs/${startOpts.sessionId}`;\n const bridgeStateDir = `${sessionDataDir}/bridge`;\n const timeoutMs = settings.startupTimeoutMs ?? 120_000;\n\n // Normalize each forwarded bridge diagnostics frame into the general\n // `HarnessV1Diagnostic` and report it. The adapter does no telemetry work\n // beyond this transport→emission mapping.\n const report = startOpts.observability?.report;\n const onDiagnostic = report\n ? (frame: Parameters<typeof harnessV1DiagnosticFromBridgeFrame>[0]) =>\n report(\n harnessV1DiagnosticFromBridgeFrame(frame, {\n sessionId: startOpts.sessionId,\n timestamp: Date.now(),\n }),\n )\n : undefined;\n\n // Builds the `connect` thunk a `SandboxChannel` re-invokes on every\n // (re)connect: open the socket, then wait for `bridge-hello` so the\n // end-to-end link is proven live before any frame is sent.\n const buildConnect = (wsUrl: string) => async (): Promise<WebSocket> => {\n return openBridgeWebSocket({ wsUrl, timeoutMs });\n };\n\n /*\n * Rung 1 — ATTACH. When lifecycle state carries live bridge coordinates,\n * try to reopen a socket to the still-running bridge. Parked sessions wait\n * for the next `start`; suspended turns request replay of everything past\n * the persisted cursor. No spawn, no fresh token (the existing bridge\n * still authorises the persisted one). If the bridge is gone the open\n * throws and we fall through to a spawn-based recovery.\n */\n if (coords) {\n try {\n const attachUrl =\n (await sandboxSession.getPortUrl({\n port: coords.port,\n protocol: 'ws',\n })) + `?agent_bridge_token=${encodeURIComponent(coords.token)}`;\n const attachChannel: ClaudeCodeChannel = new SandboxChannel({\n connect: buildConnect(attachUrl),\n outboundSchema: outboundMessageSchema,\n initialLastSeenEventId: coords.lastSeenEventId,\n onDiagnostic,\n });\n await attachChannel.open(isContinue ? { resume: true } : undefined);\n return createSession({\n sessionId: startOpts.sessionId,\n channel: attachChannel,\n // The live bridge was spawned by another process; this one owns no\n // process handle. The session lifecycle method decides whether the\n // sandbox is left running, stopped, or destroyed.\n proc: undefined,\n model: settings.model,\n maxTurns: settings.maxTurns,\n thinking: settings.thinking,\n isResume: true,\n continueOnFirstPrompt: false,\n rerunContinue: false,\n bridgePort: coords.port,\n bridgeToken: coords.token,\n sandboxId,\n debug: startOpts.observability?.debug,\n permissionMode: startOpts.permissionMode,\n skills: startOpts.skills ?? [],\n });\n } catch {\n // Bridge no longer reachable — recover by respawning below.\n }\n }\n\n /*\n * Rungs 2/3 — REPLAY vs RERUN. Respawn the bridge. `replay` is only sound\n * for `continueFrom`: those coordinates include the cursor the on-disk\n * log is replayed *from*. `resumeFrom` is a between-turn resume; even when\n * it carries bridge coordinates, replaying the previous turn would\n * re-deliver stale events into the next turn. Those resumes always `rerun`\n * when attach is unavailable (the CLI continues its own thread from the\n * workdir snapshot via `continue: true`).\n */\n let respawnStrategy: ClaudeCodeRespawnStrategy | undefined = isResume\n ? 'rerun'\n : undefined;\n if (coords && isContinue) {\n const logRaw = await Promise.resolve(\n session.readTextFile({\n path: `${bridgeStateDir}/event-log.ndjson`,\n abortSignal: startOpts.abortSignal,\n }),\n ).catch(() => null);\n if ((await classifyDiskLog(logRaw)) === 'replay') {\n respawnStrategy = 'replay';\n }\n }\n\n const sandboxHomeDir =\n startOpts.skills && startOpts.skills.length > 0\n ? await resolveSandboxHomeDir({\n sandbox: session,\n abortSignal: startOpts.abortSignal,\n })\n : undefined;\n const port = resolveBridgePort(sandboxSession, settings.port);\n const token = randomBytes(32).toString('hex');\n const env = {\n ...resolveClaudeCodeEnv(settings.auth),\n BRIDGE_CHANNEL_TOKEN: token,\n BRIDGE_WS_PORT: String(port),\n ...(sandboxHomeDir ? { HOME: sandboxHomeDir } : {}),\n ...(respawnStrategy === 'replay'\n ? { BRIDGE_REPLAY_FROM_DISK: '1' }\n : {}),\n };\n\n /*\n * On a fresh start the workdir, skill files, and bridge-state directory\n * must be created. On any resume they already exist in the\n * sandbox snapshot, so skip the rewrite. The env is sent fresh on every\n * spawn — `BRIDGE_CHANNEL_TOKEN` rotates per start.\n */\n if (respawnStrategy === undefined) {\n await session.run({\n command: `mkdir -p ${workDir} ${bridgeStateDir}`,\n abortSignal: startOpts.abortSignal,\n });\n\n if (startOpts.skills && startOpts.skills.length > 0) {\n if (!sandboxHomeDir) {\n throw new Error('Unable to resolve sandbox HOME directory.');\n }\n await writeSkills({\n sandbox: session,\n homeDir: sandboxHomeDir,\n skills: startOpts.skills,\n abortSignal: startOpts.abortSignal,\n });\n }\n }\n\n await markBridgeStarting({\n sandbox: session,\n bridgeStateDir,\n bridgeType: 'claude-code',\n abortSignal: startOpts.abortSignal,\n });\n\n const proc = await session.spawn({\n command: `node ${BOOTSTRAP_DIR}/bridge.mjs --workdir ${workDir} --bridge-state-dir ${bridgeStateDir}`,\n env,\n abortSignal: startOpts.abortSignal,\n });\n\n const bridgeStartupStderr: string[] = [];\n /*\n * Bridge stderr is the only diagnostic channel for sandbox-side crashes\n * and startup failures. Start forwarding before `bridge-ready`, otherwise\n * module-resolution, auth, and syntax errors can be lost behind the\n * generic startup failure below.\n */\n const bridgeStartupStderrDone = forwardBridgeStderr({\n stream: proc.stderr,\n collectTail: bridgeStartupStderr,\n });\n void bridgeStartupStderrDone;\n const { port: boundPort } = await waitForBridgeReady({\n proc,\n sandbox: session,\n bridgeStateDir,\n bridgeType: 'claude-code',\n timeoutMs,\n abortSignal: startOpts.abortSignal,\n createTimeoutError: ({ proc, stdoutTail }) =>\n createBridgeStartupError({\n message: 'claude-code bridge did not become ready in time.',\n proc,\n stdoutTail,\n stderrTail: bridgeStartupStderr,\n stderrDone: bridgeStartupStderrDone,\n }),\n createExitError: ({ proc, stdoutTail }) =>\n createBridgeStartupError({\n message: 'claude-code bridge exited before becoming ready.',\n proc,\n stdoutTail,\n stderrTail: bridgeStartupStderr,\n stderrDone: bridgeStartupStderrDone,\n }),\n });\n void drainRest(proc.stdout);\n\n const wsUrl =\n (await sandboxSession.getPortUrl({\n port: boundPort,\n protocol: 'ws',\n })) + `?agent_bridge_token=${encodeURIComponent(token)}`;\n\n const channel: ClaudeCodeChannel = new SandboxChannel({\n connect: buildConnect(wsUrl),\n outboundSchema: outboundMessageSchema,\n onDiagnostic,\n // In replay mode the respawned bridge reloaded the finished turn from\n // disk; seed the cursor and resume so it streams the tail (incl.\n // `finish`) rather than starting empty.\n ...(respawnStrategy === 'replay'\n ? { initialLastSeenEventId: coords?.lastSeenEventId ?? 0 }\n : {}),\n });\n await channel.open(\n respawnStrategy === 'replay' ? { resume: true } : undefined,\n );\n\n return createSession({\n sessionId: startOpts.sessionId,\n channel,\n proc,\n model: settings.model,\n maxTurns: settings.maxTurns,\n thinking: settings.thinking,\n isResume: respawnStrategy !== undefined,\n continueOnFirstPrompt: respawnStrategy !== undefined,\n rerunContinue: respawnStrategy === 'rerun',\n bridgePort: boundPort,\n bridgeToken: token,\n sandboxId,\n debug: startOpts.observability?.debug,\n permissionMode: startOpts.permissionMode,\n skills: startOpts.skills ?? [],\n });\n },\n };\n}\n\nfunction resolveBridgePort(\n sandboxSession: HarnessV1NetworkSandboxSession,\n override: number | undefined,\n): number {\n if (override !== undefined) return override;\n if (sandboxSession.ports.length > 0) return sandboxSession.ports[0];\n throw new HarnessCapabilityUnsupportedError({\n harnessId: 'claude-code',\n message:\n 'The claude-code harness needs a TCP port exposed by the sandbox. ' +\n 'Create the sandbox with `ports: [<port>]` or pass `createClaudeCode({ port })`.',\n });\n}\n\n/**\n * Materialise skill files into\n * `$HOME/.claude/skills/<name>/SKILL.md`. The `claude` CLI\n * auto-discovers skills from that directory on startup, so the files have to\n * be in place before the bridge is spawned without mutating the session\n * workdir. Each file uses the YAML-frontmatter shape the CLI expects.\n */\nasync function writeSkills({\n sandbox,\n homeDir,\n skills,\n abortSignal,\n}: {\n sandbox: Experimental_SandboxSession;\n homeDir: string;\n skills: ReadonlyArray<HarnessV1Skill>;\n abortSignal?: AbortSignal;\n}): Promise<void> {\n for (const skill of skills) {\n safeClaudeSkillName(skill.name);\n for (const file of skill.files ?? []) {\n safeClaudeSkillFilePath({\n skillName: skill.name,\n filePath: file.path,\n });\n }\n }\n\n await sandbox.run({\n command: `mkdir -p ${shellQuote(homeDir)}/.claude/skills`,\n abortSignal,\n });\n for (const skill of skills) {\n const name = safeClaudeSkillName(skill.name);\n const skillDir = `${homeDir}/.claude/skills/${name}`;\n const path = `${skillDir}/SKILL.md`;\n const content = `---\\nname: ${skill.name}\\ndescription: ${skill.description}\\n---\\n\\n${skill.content}\\n`;\n await sandbox.writeTextFile({ path, content, abortSignal });\n for (const file of skill.files ?? []) {\n const filePath = safeClaudeSkillFilePath({\n skillName: skill.name,\n filePath: file.path,\n });\n await sandbox.writeTextFile({\n path: `${skillDir}/${filePath}`,\n content: file.content,\n abortSignal,\n });\n }\n }\n}\n\nasync function resolveSandboxHomeDir({\n sandbox,\n abortSignal,\n}: {\n sandbox: Experimental_SandboxSession;\n abortSignal?: AbortSignal;\n}): Promise<string> {\n const result = await sandbox.run({\n command: 'printf \"%s\" \"$HOME\"',\n abortSignal,\n });\n const homeDir = result.stdout.trim();\n if (result.exitCode !== 0 || !homeDir || !path.posix.isAbsolute(homeDir)) {\n throw new Error(\n `Unable to resolve sandbox HOME directory: ${result.stderr || result.stdout}`,\n );\n }\n return homeDir;\n}\n\nfunction safeClaudeSkillName(name: string): string {\n if (!/^[A-Za-z0-9._-]+$/.test(name) || name === '.' || name === '..') {\n throw new Error(`Invalid Claude Code skill name: ${name}`);\n }\n return name;\n}\n\nfunction safeClaudeSkillFilePath({\n skillName,\n filePath,\n}: {\n skillName: string;\n filePath: string;\n}): string {\n const normalized = path.posix.normalize(filePath);\n if (\n normalized === '.' ||\n normalized.startsWith('../') ||\n path.posix.isAbsolute(normalized)\n ) {\n throw new Error(\n `Invalid Claude Code skill file path for ${skillName}: ${filePath}`,\n );\n }\n return normalized;\n}\n\nfunction shellQuote(value: string): string {\n return `'${value.replace(/'/g, `'\\\\''`)}'`;\n}\n\nasync function readBridgeAsset(name: string): Promise<string> {\n const candidates = [\n new URL(`./bridge/${name}`, import.meta.url),\n new URL(`../bridge/${name}`, import.meta.url),\n ];\n let lastErr: unknown;\n for (const url of candidates) {\n try {\n return await readFile(fileURLToPath(url), 'utf8');\n } catch (err) {\n const code = (err as NodeJS.ErrnoException).code;\n if (code !== 'ENOENT') throw err;\n lastErr = err;\n }\n }\n throw lastErr ?? new Error(`bridge asset not found: ${name}`);\n}\n\nasync function createBridgeStartupError({\n message,\n proc,\n stdoutTail,\n stderrTail,\n stderrDone,\n}: {\n message: string;\n proc: Experimental_SandboxProcess;\n stdoutTail: string[];\n stderrTail: string[];\n stderrDone: Promise<void>;\n}): Promise<Error> {\n await Promise.race([\n stderrDone,\n new Promise<void>(resolve => setTimeout(resolve, 250)),\n ]).catch(() => {});\n\n let exitStatus = '';\n try {\n const result = (await Promise.race([\n proc.wait(),\n new Promise<undefined>(resolve => setTimeout(resolve, 250)),\n ])) as { exitCode?: number } | undefined;\n if (result?.exitCode !== undefined) {\n exitStatus = ` Exit code: ${result.exitCode}.`;\n }\n } catch {}\n\n const details: string[] = [];\n if (stdoutTail.length > 0) {\n details.push(`stdout:\\n${stdoutTail.join('\\n')}`);\n }\n if (stderrTail.length > 0) {\n details.push(`stderr:\\n${stderrTail.join('\\n')}`);\n }\n\n return new Error(\n `${message}${exitStatus}${\n details.length > 0 ? `\\n\\n${details.join('\\n\\n')}` : ''\n }`,\n );\n}\n\nfunction lineDecoder() {\n let buffer = '';\n return {\n push(chunk: string): string[] {\n buffer += chunk;\n const lines: string[] = [];\n let nl: number;\n while ((nl = buffer.indexOf('\\n')) !== -1) {\n const raw = buffer.slice(0, nl);\n buffer = buffer.slice(nl + 1);\n const line = raw.replace(/\\r$/, '').trim();\n if (line.length > 0) lines.push(line);\n }\n return lines;\n },\n flush(): string[] {\n const line = buffer.replace(/\\r$/, '').trim();\n buffer = '';\n return line.length > 0 ? [line] : [];\n },\n };\n}\n\nasync function forwardBridgeStderr({\n stream,\n collectTail,\n}: {\n stream: ReadableStream<Uint8Array>;\n collectTail?: string[];\n}): Promise<void> {\n try {\n const reader = stream.pipeThrough(new TextDecoderStream()).getReader();\n const decoder = lineDecoder();\n while (true) {\n const { value, done } = await reader.read();\n if (done) {\n for (const line of decoder.flush()) {\n const trimmed = line.trim();\n if (!trimmed) continue;\n if (collectTail) {\n collectTail.push(trimmed);\n if (collectTail.length > 20) collectTail.shift();\n }\n // eslint-disable-next-line no-console\n console.log(`[bridge stderr] ${trimmed}`);\n }\n return;\n }\n if (value) {\n for (const line of decoder.push(value)) {\n const trimmed = line.trim();\n if (!trimmed) continue;\n if (collectTail) {\n collectTail.push(trimmed);\n if (collectTail.length > 20) collectTail.shift();\n }\n // eslint-disable-next-line no-console\n console.log(`[bridge stderr] ${trimmed}`);\n }\n }\n }\n } catch {\n // Reader errors are non-fatal — best-effort diagnostic only.\n }\n}\n\nasync function drainRest(stream: ReadableStream<Uint8Array>): Promise<void> {\n try {\n const reader = stream.pipeThrough(new TextDecoderStream()).getReader();\n while (true) {\n const { done } = await reader.read();\n if (done) return;\n }\n } catch {}\n}\n\n/**\n * Wait for the bridge's `bridge-hello` message to arrive on the freshly\n * opened WebSocket before any other host-side code touches it.\n *\n * Some sandbox runtimes (Vercel in particular) complete the WS upgrade\n * with the host long before the connection is actually forwarded to the\n * sandbox-side bridge. Anything sent in that gap is silently dropped —\n * including the `start` message we send next. The bridge emits\n * `bridge-hello` the instant it accepts the connection, so receiving it\n * is the only reliable evidence that the end-to-end link is live.\n */\nasync function waitForBridgeHello({\n ws,\n timeoutMs,\n}: {\n ws: WebSocket;\n timeoutMs: number;\n}): Promise<void> {\n await new Promise<void>((resolve, reject) => {\n let settled = false;\n const cleanup = () => {\n ws.off('message', onMessage);\n ws.off('close', onClose);\n ws.off('error', onError);\n if (timer) clearTimeout(timer);\n };\n const settle = (err?: unknown) => {\n if (settled) return;\n settled = true;\n cleanup();\n if (err) reject(err);\n else resolve();\n };\n const onMessage = (raw: unknown) => {\n try {\n const text =\n typeof raw === 'string'\n ? raw\n : (raw as Buffer | ArrayBufferLike).toString\n ? (raw as Buffer).toString('utf8')\n : String(raw);\n const parsed = JSON.parse(text) as { type?: unknown };\n if (parsed?.type === 'bridge-hello') settle();\n } catch {\n // Ignore malformed frames while waiting.\n }\n };\n const onClose = () => {\n settle(\n new Error('claude-code bridge closed before sending bridge-hello'),\n );\n };\n const onError = (err: Error) => settle(err);\n const timer = setTimeout(\n () =>\n settle(\n new Error(\n `claude-code bridge did not send bridge-hello within ${timeoutMs}ms`,\n ),\n ),\n timeoutMs,\n );\n timer.unref?.();\n ws.on('message', onMessage);\n ws.on('close', onClose);\n ws.on('error', onError);\n });\n}\n\nasync function openBridgeWebSocket({\n wsUrl,\n timeoutMs,\n}: {\n wsUrl: string;\n timeoutMs: number;\n}): Promise<WebSocket> {\n const deadline = Date.now() + timeoutMs;\n let attempt = 0;\n let lastError: unknown;\n\n while (Date.now() < deadline) {\n attempt++;\n let ws: WebSocket | undefined;\n try {\n const remaining = Math.max(1, deadline - Date.now());\n ws = await openWebSocket({\n url: wsUrl,\n timeoutMs: Math.min(10_000, remaining),\n });\n await waitForBridgeHello({\n ws,\n timeoutMs: Math.min(5_000, Math.max(1, deadline - Date.now())),\n });\n return ws;\n } catch (err) {\n lastError = err;\n try {\n ws?.close();\n } catch {}\n const remaining = deadline - Date.now();\n if (remaining <= 0) break;\n await sleep(Math.min(250 * attempt, 1_000, remaining));\n }\n }\n\n throw new Error(\n `claude-code bridge did not complete WebSocket handshake within ${timeoutMs}ms after ${attempt} attempt(s). Last error: ${formatUnknownError(lastError)}`,\n );\n}\n\nfunction openWebSocket({\n url,\n timeoutMs,\n}: {\n url: string;\n timeoutMs: number;\n}): Promise<WebSocket> {\n return new Promise((resolve, reject) => {\n const ws = new WebSocket(url);\n const timer = setTimeout(() => {\n cleanup();\n try {\n ws.terminate();\n } catch {}\n reject(new Error(`WebSocket open timed out after ${timeoutMs}ms`));\n }, timeoutMs);\n timer.unref?.();\n const cleanup = () => {\n clearTimeout(timer);\n ws.off('open', onOpen);\n ws.off('error', onError);\n };\n const onOpen = () => {\n cleanup();\n resolve(ws);\n };\n const onError = (err: Error) => {\n cleanup();\n reject(err);\n };\n ws.once('open', onOpen);\n ws.once('error', onError);\n });\n}\n\nfunction sleep(ms: number): Promise<void> {\n return new Promise(resolve => {\n const timer = setTimeout(resolve, ms);\n timer.unref?.();\n });\n}\n\nfunction formatUnknownError(error: unknown): string {\n if (error instanceof Error) return error.message;\n return String(error);\n}\n\nfunction createSession({\n sessionId,\n channel,\n proc,\n model,\n maxTurns,\n thinking,\n isResume,\n continueOnFirstPrompt,\n rerunContinue,\n bridgePort,\n bridgeToken,\n sandboxId,\n debug,\n permissionMode,\n skills,\n}: {\n sessionId: string;\n channel: ClaudeCodeChannel;\n /** Undefined on `attach` — the live bridge was spawned by another process. */\n proc: Experimental_SandboxProcess | undefined;\n model: string | undefined;\n maxTurns: number | undefined;\n thinking: 'off' | 'on' | 'adaptive' | undefined;\n isResume: boolean;\n continueOnFirstPrompt: boolean;\n rerunContinue: boolean;\n bridgePort: number;\n bridgeToken: string;\n sandboxId: string;\n debug: HarnessV1DebugConfig | undefined;\n permissionMode: HarnessV1PermissionMode | undefined;\n skills: ReadonlyArray<HarnessV1Skill>;\n}): HarnessV1Session {\n let stopped = false;\n let stopPromise: Promise<void> | undefined;\n /*\n * Force the Claude SDK's `continue: true` on the first prompt only when the\n * bridge was respawned (rerun/replay): a fresh bridge process treats its\n * first turn as new, so it must be told to rehydrate the workdir thread. An\n * `attach`ed bridge is already past its first turn and continues on its own.\n */\n let pendingResumeFlag = continueOnFirstPrompt;\n /*\n * Instructions are prepended to the first user message of a fresh session\n * only. A resumed session (attach/replay/rerun) already carried them in its\n * original first message (preserved in the workdir snapshot), so it starts\n * \"applied\".\n */\n let instructionsApplied = isResume;\n\n /*\n * Wire the channel into one turn's worth of events and return the control\n * surface. Shared by `doPromptTurn` (which sends a `start` afterwards) and\n * `doContinueTurn` (which attaches to an already-running/replayed turn, or sends\n * a rerun `start`). The only difference between the two entry points is the\n * `start` message, not the listener/abort/settle plumbing.\n */\n const wireTurn = (turnOpts: {\n emit: (event: HarnessV1StreamPart) => void;\n abortSignal?: AbortSignal;\n }): HarnessV1PromptControl => {\n let pendingResolve: (() => void) | undefined;\n let pendingReject: ((err: unknown) => void) | undefined;\n const done = new Promise<void>((resolve, reject) => {\n pendingResolve = resolve;\n pendingReject = reject;\n });\n\n const unsubs: Array<() => void> = [];\n const forward = (event: HarnessV1StreamPart) => {\n try {\n turnOpts.emit(event);\n } catch {}\n };\n\n let isSettled = false;\n const settleSuccess = () => {\n if (isSettled) return;\n isSettled = true;\n for (const u of unsubs) u();\n pendingResolve!();\n };\n const settleError = (err: unknown) => {\n if (isSettled) return;\n isSettled = true;\n for (const u of unsubs) u();\n pendingReject!(err);\n };\n\n const eventTypes = [\n 'stream-start',\n 'text-start',\n 'text-delta',\n 'text-end',\n 'reasoning-start',\n 'reasoning-delta',\n 'reasoning-end',\n 'tool-call',\n 'tool-approval-request',\n 'tool-result',\n 'finish-step',\n 'raw',\n ] as const;\n for (const type of eventTypes) {\n unsubs.push(\n channel.on(type, msg => {\n forward(msg);\n }),\n );\n }\n unsubs.push(\n channel.on('finish', msg => {\n forward(msg);\n settleSuccess();\n }),\n );\n unsubs.push(\n channel.on('error', msg => {\n forward(msg);\n settleError(msg.error);\n }),\n );\n\n /*\n * A `'suspended'` close is a graceful slice-boundary freeze the host\n * initiated (`doSuspendTurn`): the turn keeps running in the bridge and its\n * tail is replayed to the next process, so wind this turn down cleanly\n * rather than failing it. Any other close mid-turn is an unexpected drop.\n */\n const onClose = (_code?: number, reason?: string) => {\n if (isSettled) return;\n if (reason === 'suspended') {\n settleSuccess();\n return;\n }\n settleError(\n new Error('claude-code bridge closed before the turn finished.'),\n );\n };\n channel.onClose(onClose);\n\n const onAbort = () => {\n if (isSettled) return;\n try {\n channel.send({ type: 'abort' });\n } catch {}\n settleError(\n turnOpts.abortSignal?.reason ??\n new DOMException('Aborted', 'AbortError'),\n );\n };\n if (turnOpts.abortSignal) {\n if (turnOpts.abortSignal.aborted) {\n onAbort();\n } else {\n turnOpts.abortSignal.addEventListener('abort', onAbort, {\n once: true,\n });\n }\n }\n\n return {\n submitToolResult: async input => {\n channel.send({\n type: 'tool-result',\n toolCallId: input.toolCallId,\n output: input.output,\n isError: input.isError,\n });\n },\n submitToolApproval: async input => {\n channel.send({\n type: 'tool-approval-response',\n approvalId: input.approvalId,\n approved: input.approved,\n reason: input.reason,\n });\n },\n submitUserMessage: async text => {\n channel.send({ type: 'user-message', text });\n },\n done,\n };\n };\n\n return {\n sessionId,\n isResume,\n modelId: model,\n doPromptTurn: async promptOpts => {\n const control = wireTurn({\n emit: promptOpts.emit,\n abortSignal: promptOpts.abortSignal,\n });\n\n let promptText = extractUserText(promptOpts.prompt);\n if (!instructionsApplied && promptOpts.instructions) {\n promptText = frameInstructions(promptOpts.instructions, promptText);\n }\n instructionsApplied = true;\n\n const startMessage = {\n type: 'start' as const,\n prompt: promptText,\n tools: (promptOpts.tools ?? []).map(t => ({\n name: t.name,\n description: t.description,\n inputSchema: t.inputSchema,\n })),\n model,\n maxTurns,\n thinking,\n ...(skills.length > 0\n ? { skills: skills.map(skill => skill.name) }\n : {}),\n ...(permissionMode ? { permissionMode } : {}),\n ...(debug ? { debug } : {}),\n ...(pendingResumeFlag ? { continue: true } : {}),\n };\n pendingResumeFlag = false;\n channel.send(startMessage);\n\n return control;\n },\n doContinueTurn: async continueOpts => {\n const control = wireTurn({\n emit: continueOpts.emit,\n abortSignal: continueOpts.abortSignal,\n });\n\n /*\n * attach / replay: the still-running (or disk-replayed) turn streams into\n * the wired listeners — `doStart` opened the channel with `{ resume: true }`\n * so the bridge replays everything past the persisted cursor (including a\n * `finish` if the turn ended during the gap). No `start` is sent: issuing\n * one would clear the bridge's replay log and begin a new turn. Lossless.\n *\n * rerun: the bridge was respawned with no in-flight turn to attach to, so\n * re-drive the runtime's own thread from the workdir snapshot via\n * `continue: true`. Lossy — work in flight at the interruption is\n * recomputed. This is the rare bridge-died fallback; the common slice path\n * is `attach`.\n */\n if (rerunContinue) {\n pendingResumeFlag = false;\n channel.send({\n type: 'start' as const,\n /*\n * A continuation nudge rather than an empty prompt: `continue: true`\n * rehydrates the prior thread, and this is the new user turn that\n * drives it forward. It must be non-empty — an empty text block is\n * rejected by the Anthropic API once the SDK stamps it with\n * `cache_control`.\n */\n prompt: 'Continue.',\n tools: (continueOpts.tools ?? []).map(t => ({\n name: t.name,\n description: t.description,\n inputSchema: t.inputSchema,\n })),\n model,\n maxTurns,\n thinking,\n ...(skills.length > 0\n ? { skills: skills.map(skill => skill.name) }\n : {}),\n ...(permissionMode ? { permissionMode } : {}),\n ...(debug ? { debug } : {}),\n continue: true,\n });\n }\n\n return control;\n },\n doCompact: async (customInstructions?: string) => {\n /*\n * Claude Code has no SDK/control method for compaction — the supported\n * trigger is the `/compact` slash command submitted as user input. Ride\n * the existing user-message rail; the bridge feeds it into the streaming\n * query input and Claude's native compaction handles the rest, emitting a\n * `compact_boundary` + `PostCompact` we observe as a `compaction` event.\n */\n const text =\n customInstructions && customInstructions.trim()\n ? `/compact ${customInstructions.trim()}`\n : '/compact';\n channel.send({ type: 'user-message', text });\n },\n doDetach: async () => {\n if (stopped) {\n throw new Error(\n `claude-code session ${sessionId} is already stopped; cannot detach.`,\n );\n }\n stopped = true;\n const lastSeenEventId = await channel.suspend();\n const payload: HarnessV1ResumeSessionState = {\n type: 'resume-session',\n harnessId: 'claude-code',\n specificationVersion: 'harness-v1',\n data: {\n bridge: {\n port: bridgePort,\n token: bridgeToken,\n lastSeenEventId,\n sandboxId,\n },\n },\n };\n return payload;\n },\n doDestroy: async () => {\n if (stopped) return stopPromise;\n stopped = true;\n stopPromise = (async () => {\n // Tell the channel we are tearing down so the bridge's post-shutdown\n // socket close finalises instead of triggering a reconnect.\n channel.beginClose();\n try {\n if (!channel.isClosed()) {\n channel.send({ type: 'shutdown' });\n }\n } catch {}\n let stopTimer: ReturnType<typeof setTimeout> | undefined;\n try {\n if (proc) {\n await Promise.race([\n proc.wait(),\n new Promise<void>(resolve => {\n stopTimer = setTimeout(resolve, 5000);\n stopTimer.unref?.();\n }),\n ]);\n }\n } finally {\n if (stopTimer) clearTimeout(stopTimer);\n try {\n await proc?.kill();\n } catch {}\n channel.close();\n }\n })();\n return stopPromise;\n },\n doStop: async () => {\n if (stopped) {\n throw new Error(\n `claude-code session ${sessionId} is already stopped; cannot stop.`,\n );\n }\n stopped = true;\n /*\n * If the bridge's channel already closed (e.g. mid-turn WS drop)\n * there is no one to ack a `detach` message. Synthesize an empty\n * payload — for Claude Code the resume state structurally is `{}`\n * (the conversation lives in the workdir, captured by the sandbox\n * snapshot during the subsequent `sandboxSession.stop()`), so we\n * lose nothing by skipping the round-trip.\n */\n // Tell the channel we are tearing down so the bridge's post-detach\n // socket close finalises instead of triggering a reconnect.\n channel.beginClose();\n const data: unknown = channel.isClosed()\n ? {}\n : await new Promise<unknown>((resolve, reject) => {\n const timer = setTimeout(() => {\n unsub();\n reject(\n new Error(\n `claude-code session ${sessionId} did not reply to detach within 5s.`,\n ),\n );\n }, 5000);\n timer.unref?.();\n const unsub = channel.on('bridge-detach', msg => {\n clearTimeout(timer);\n unsub();\n resolve(msg.data);\n });\n try {\n channel.send({ type: 'detach' });\n } catch (err) {\n clearTimeout(timer);\n unsub();\n reject(err);\n }\n });\n\n // The bridge exits itself ~50ms after sending bridge-detach. Give\n // it a moment, then ensure the process is reaped and the channel\n // closed.\n let stopTimer: ReturnType<typeof setTimeout> | undefined;\n try {\n if (proc) {\n await Promise.race([\n proc.wait(),\n new Promise<void>(resolve => {\n stopTimer = setTimeout(resolve, 5000);\n stopTimer.unref?.();\n }),\n ]);\n }\n } finally {\n if (stopTimer) clearTimeout(stopTimer);\n try {\n await proc?.kill();\n } catch {}\n channel.close();\n }\n\n const payload: HarnessV1ResumeSessionState = {\n type: 'resume-session',\n harnessId: 'claude-code',\n specificationVersion: 'harness-v1',\n data: (data ?? {}) as HarnessV1ResumeSessionState['data'],\n };\n return payload;\n },\n doSuspendTurn: async () => {\n if (stopped) {\n throw new Error(\n `claude-code session ${sessionId} is stopped; cannot suspend.`,\n );\n }\n stopped = true;\n /*\n * Gracefully freeze the active turn at a precise cursor. `channel.suspend`\n * stops processing inbound frames (the cursor stops advancing exactly at\n * the last delivered event), drains what was already dispatched, then\n * closes the host socket with reason `'suspended'` — which `wireTurn`'s\n * `onClose` treats as a clean turn end. The bridge keeps the turn running\n * and accumulates events past the cursor for the next slice to replay. The\n * sandbox process is deliberately left alive (no `shutdown`/`detach`).\n */\n const lastSeenEventId = await channel.suspend();\n const payload: HarnessV1ContinueTurnState = {\n type: 'continue-turn',\n harnessId: 'claude-code',\n specificationVersion: 'harness-v1',\n data: {\n bridge: {\n port: bridgePort,\n token: bridgeToken,\n lastSeenEventId,\n sandboxId,\n },\n },\n };\n return payload;\n },\n };\n}\n\n/*\n * Frame session instructions and the user's text so the runtime treats the\n * instructions as system-provided operating guidance, not something the user\n * wrote. Without the wrapper the agent can echo the prepended text back as if\n * the user had asked for it, which is confusing since the user never typed it.\n * Applied only to the first user message of a fresh session.\n */\nfunction frameInstructions(instructions: string, userText: string): string {\n return (\n '<session-instructions>\\n' +\n 'The block below is operating guidance from the system, not a message from the user — follow it, but do not mention it or attribute it to the user.\\n\\n' +\n `${instructions}\\n` +\n '</session-instructions>\\n\\n' +\n `<user-message>\\n${userText}\\n</user-message>`\n );\n}\n\n/*\n * Reduce a `HarnessV1Prompt` to the plain user text the bridge forwards\n * to the Claude SDK. File and image parts on the message are not yet\n * supported by the underlying runtime — throw rather than silently drop\n * them so callers learn about the gap instead of seeing mysteriously\n * truncated prompts.\n */\nfunction extractUserText(prompt: HarnessV1Prompt): string {\n if (typeof prompt === 'string') return prompt;\n const { content } = prompt;\n if (typeof content === 'string') return content;\n const parts: string[] = [];\n for (const part of content) {\n if (part.type !== 'text') {\n throw new HarnessCapabilityUnsupportedError({\n harnessId: 'claude-code',\n message: `The claude-code harness does not yet support user message parts of type '${part.type}'. Pass a string or a user message whose content contains only text parts.`,\n });\n }\n parts.push(part.text);\n }\n return parts.join('\\n\\n');\n}\n","import { execFileSync } from 'node:child_process';\nimport { readFileSync } from 'node:fs';\nimport { homedir } from 'node:os';\nimport { join } from 'node:path';\nimport { getAiGatewayAuthFromEnv } from '@ai-sdk/harness/utils';\n\nexport type ClaudeCodeAuthOptions = {\n readonly anthropic?: {\n readonly apiKey?: string;\n readonly authToken?: string;\n readonly baseUrl?: string;\n };\n readonly gateway?: {\n readonly apiKey?: string;\n readonly baseUrl?: string;\n };\n};\n\n/**\n * Resolve the environment-variable blob the bridge needs to authenticate\n * with Anthropic (directly or via the Vercel AI Gateway). Precedence:\n *\n * 1. Explicit `auth.anthropic` — pin to direct Anthropic auth.\n * 2. Explicit `auth.gateway` — pin to gateway-routed auth.\n * 3. Auto-detect from the host process env: gateway first\n * (`AI_GATEWAY_API_KEY` / `VERCEL_OIDC_TOKEN`), then direct\n * (`ANTHROPIC_API_KEY` / `ANTHROPIC_AUTH_TOKEN`).\n */\nexport type ResolveClaudeCodeEnvOptions = {\n /**\n * Returns an API key from a custom source (e.g. a password manager).\n * Used as the last-resort fallback in the auto-detect branch when no\n * static env vars or explicit auth are configured. Defaults to running\n * the `apiKeyHelper` command from `~/.claude/settings.json`, matching\n * the `claude` CLI's own behaviour.\n */\n readApiKeyHelper?: () => string | undefined;\n};\n\nexport function resolveClaudeCodeEnv(\n auth: ClaudeCodeAuthOptions | undefined,\n processEnv: Record<string, string | undefined> = process.env,\n options: ResolveClaudeCodeEnvOptions = {},\n): Record<string, string> {\n const readApiKey = options.readApiKeyHelper ?? readApiKeyHelper;\n if (auth?.anthropic) {\n return pickAnthropic({ explicit: auth.anthropic, processEnv, readApiKey });\n }\n\n const gatewayAuthFromEnv = getAiGatewayAuthFromEnv({ env: processEnv });\n if (auth?.gateway) {\n return pickGateway({\n explicit: auth.gateway,\n gatewayAuthFromEnv,\n });\n }\n if (gatewayAuthFromEnv.apiKey) {\n return pickGateway({\n explicit: {},\n gatewayAuthFromEnv,\n });\n }\n\n return pickAnthropic({ processEnv, readApiKey });\n}\n\nfunction pickAnthropic({\n explicit,\n processEnv,\n readApiKey,\n}: {\n explicit?: NonNullable<ClaudeCodeAuthOptions['anthropic']>;\n processEnv: Record<string, string | undefined>;\n readApiKey: () => string | undefined;\n}): Record<string, string> {\n const env: Record<string, string> = {};\n const helperKey = explicit ? undefined : readApiKey();\n const apiKey = explicit?.apiKey ?? processEnv.ANTHROPIC_API_KEY ?? helperKey;\n const authToken =\n explicit?.authToken ?? processEnv.ANTHROPIC_AUTH_TOKEN ?? helperKey;\n if (apiKey) env.ANTHROPIC_API_KEY = apiKey;\n if (authToken) env.ANTHROPIC_AUTH_TOKEN = authToken;\n const baseUrl = explicit?.baseUrl ?? processEnv.ANTHROPIC_BASE_URL;\n if (baseUrl) env.ANTHROPIC_BASE_URL = baseUrl;\n return env;\n}\n\n/**\n * Read the `apiKeyHelper` setting from `~/.claude/settings.json` and run\n * it. The `claude` CLI uses this hook to fetch credentials from password\n * managers and similar tools; mirroring it here lets users with that\n * setup run the harness without having to set `ANTHROPIC_API_KEY`\n * explicitly.\n */\nfunction readApiKeyHelper(): string | undefined {\n const home = homedir();\n if (!home) return undefined;\n let raw: string;\n try {\n raw = readFileSync(join(home, '.claude', 'settings.json'), 'utf8');\n } catch {\n return undefined;\n }\n let settings: { apiKeyHelper?: unknown };\n try {\n settings = JSON.parse(raw);\n } catch {\n return undefined;\n }\n const command = settings.apiKeyHelper;\n if (typeof command !== 'string' || command.length === 0) return undefined;\n try {\n const output = execFileSync('sh', ['-c', command], {\n encoding: 'utf8',\n stdio: ['ignore', 'pipe', 'ignore'],\n });\n const trimmed = output.trim();\n return trimmed.length > 0 ? trimmed : undefined;\n } catch {\n return undefined;\n }\n}\n\nfunction pickGateway({\n explicit,\n gatewayAuthFromEnv,\n}: {\n explicit: NonNullable<ClaudeCodeAuthOptions['gateway']>;\n gatewayAuthFromEnv: ReturnType<typeof getAiGatewayAuthFromEnv>;\n}): Record<string, string> {\n const apiKey = explicit.apiKey ?? gatewayAuthFromEnv.apiKey;\n const baseUrl = explicit.baseUrl ?? gatewayAuthFromEnv.baseUrl;\n const env: Record<string, string> = {};\n if (apiKey) {\n env.AI_GATEWAY_API_KEY = apiKey;\n env.ANTHROPIC_API_KEY = apiKey;\n }\n env.AI_GATEWAY_BASE_URL = baseUrl;\n env.ANTHROPIC_BASE_URL = baseUrl;\n return env;\n}\n","import {\n harnessV1BridgeInboundCommandSchemas,\n harnessV1BridgeOutboundMessageSchema,\n harnessV1BridgeReadySchema,\n harnessV1BridgeStartBaseSchema,\n} from '@ai-sdk/harness';\nimport { z } from 'zod/v4';\n\n/*\n * Claude Code's bridge wire protocol. The outbound events, transport frames,\n * shared inbound commands, and `bridge-ready` line all come from the shared\n * `@ai-sdk/harness` protocol — the only Claude-specific piece is the `start`\n * payload, which carries Claude SDK configuration.\n */\n\nexport const outboundMessageSchema = harnessV1BridgeOutboundMessageSchema;\nexport type OutboundMessage = z.infer<typeof outboundMessageSchema>;\n\nexport const startMessageSchema = harnessV1BridgeStartBaseSchema.extend({\n thinking: z.enum(['off', 'on', 'adaptive']).optional(),\n maxTurns: z.number().optional(),\n skills: z.array(z.string()).optional(),\n // Resume signal. When true, the bridge passes `{ continue: true }` to the\n // Claude SDK so the in-workdir thread state is rehydrated. The host sets this\n // on the first prompt after a cross-process resume.\n continue: z.boolean().optional(),\n});\n\nexport type StartMessage = z.infer<typeof startMessageSchema>;\n\nexport const inboundMessageSchema = z.discriminatedUnion('type', [\n startMessageSchema,\n ...harnessV1BridgeInboundCommandSchemas,\n]);\nexport type InboundMessage = z.infer<typeof inboundMessageSchema>;\n\nexport const bridgeReadySchema = harnessV1BridgeReadySchema;\nexport type BridgeReady = z.infer<typeof bridgeReadySchema>;\n","import { createClaudeCode } from './claude-code-harness';\n\n/**\n * Default `claude-code` harness instance with no overrides — suitable for the\n * common case where the underlying `claude` CLI's defaults are fine.\n * Equivalent to `createClaudeCode()`.\n */\nexport const claudeCode = createClaudeCode();\n\nexport { createClaudeCode } from './claude-code-harness';\nexport type { ClaudeCodeHarnessSettings } from './claude-code-harness';\nexport type { ClaudeCodeAuthOptions } from './claude-code-auth';\n"],"mappings":";AAAA,SAAS,mBAAmB;AAC5B,SAAS,gBAAgB;AACzB,OAAO,UAAU;AACjB,SAAS,qBAAqB;AAC9B;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OAcK;AACP;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP;AAAA,EACE;AAAA,OAGK;AACP,SAAS,iBAAiB;AAC1B,SAAS,KAAAA,UAAS;;;AClClB,SAAS,oBAAoB;AAC7B,SAAS,oBAAoB;AAC7B,SAAS,eAAe;AACxB,SAAS,YAAY;AACrB,SAAS,+BAA+B;AAmCjC,SAAS,qBACd,MACA,aAAiD,QAAQ,KACzD,UAAuC,CAAC,GAChB;AACxB,QAAM,aAAa,QAAQ,oBAAoB;AAC/C,MAAI,MAAM,WAAW;AACnB,WAAO,cAAc,EAAE,UAAU,KAAK,WAAW,YAAY,WAAW,CAAC;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AAO;AAAA,IACT;AAAA,EACF;AACF;AASA,SAAS,kBAAkB,cAAsB,UAA0B;AACzE,SACE;AAAA;AAAA;AAAA,EAEG,YAAY;AAAA;AAAA;AAAA;AAAA,EAEI,QAAQ;AAAA;AAE/B;AASA,SAAS,gBAAgB,QAAiC;AACxD,MAAI,OAAO,WAAW,SAAU,QAAO;AACvC,QAAM,EAAE,QAAQ,IAAI;AACpB,MAAI,OAAO,YAAY,SAAU,QAAO;AACxC,QAAM,QAAkB,CAAC;AACzB,aAAW,QAAQ,SAAS;AAC1B,QAAI,KAAK,SAAS,QAAQ;AACxB,YAAM,IAAI,kCAAkC;AAAA,QAC1C,WAAW;AAAA,QACX,SAAS,4EAA4E,KAAK,IAAI;AAAA,MAChG,CAAC;AAAA,IACH;AACA,UAAM,KAAK,KAAK,IAAI;AAAA,EACtB;AACA,SAAO,MAAM,KAAK,MAAM;AAC1B;;;AG1iDO,IAAM,aAAa,iBAAiB;","names":["z","z","wsUrl","proc","path"]}
|
|
1
|
+
{"version":3,"sources":["../src/claude-code-harness.ts","../src/claude-code-auth.ts","../src/claude-code-bridge-protocol.ts","../src/index.ts"],"sourcesContent":["import { randomBytes } from 'node:crypto';\nimport { readFile } from 'node:fs/promises';\nimport path from 'node:path';\nimport { fileURLToPath } from 'node:url';\nimport {\n commonTool,\n harnessV1DiagnosticFromBridgeFrame,\n HarnessCapabilityUnsupportedError,\n type HarnessV1,\n type HarnessV1Bootstrap,\n type HarnessV1DebugConfig,\n type HarnessV1BuiltinTool,\n type HarnessV1ContinueTurnState,\n type HarnessV1PermissionMode,\n type HarnessV1Prompt,\n type HarnessV1PromptControl,\n type HarnessV1ResumeSessionState,\n type HarnessV1NetworkSandboxSession,\n type HarnessV1Session,\n type HarnessV1Skill,\n type HarnessV1StreamPart,\n} from '@ai-sdk/harness';\nimport {\n classifyDiskLog,\n markBridgeStarting,\n SandboxChannel,\n waitForBridgeReady,\n} from '@ai-sdk/harness/utils';\nimport {\n tool,\n type Experimental_SandboxSession,\n type Experimental_SandboxProcess,\n} from '@ai-sdk/provider-utils';\nimport { WebSocket } from 'ws';\nimport { z } from 'zod/v4';\nimport {\n resolveClaudeCodeEnv,\n type ClaudeCodeAuthOptions,\n} from './claude-code-auth';\nimport {\n outboundMessageSchema,\n type InboundMessage,\n type OutboundMessage,\n} from './claude-code-bridge-protocol';\n\ntype ClaudeCodeChannel = SandboxChannel<OutboundMessage, InboundMessage>;\ntype ClaudeCodeRespawnStrategy = 'replay' | 'rerun';\n\nexport type ClaudeCodeHarnessSettings = {\n readonly auth?: ClaudeCodeAuthOptions;\n /**\n * Anthropic model id the underlying `claude` CLI should use. Leaving this\n * unset defers to the CLI's default.\n */\n readonly model?: string;\n /**\n * Hard cap on how many internal turns the CLI can take before yielding\n * back to the caller. Unset means the CLI's default.\n */\n readonly maxTurns?: number;\n /**\n * Controls extended-thinking behavior. `'off'` disables thinking,\n * `'on'` forces it on, `'adaptive'` lets the runtime decide.\n */\n readonly thinking?: 'off' | 'on' | 'adaptive';\n /**\n * Override the port the bridge binds inside the sandbox. By default the\n * adapter uses the first port the sandbox declares via `sandbox.ports`.\n * Only set this if the sandbox declares multiple ports and the first one\n * is reserved for something else.\n */\n readonly port?: number;\n /** Maximum milliseconds to wait for the bridge to advertise its port. Defaults to 120000. */\n readonly startupTimeoutMs?: number;\n};\n\n/*\n * Every native tool the Claude Code CLI can invoke, declared as a `ToolSet`\n * keyed by what the bridge emits as `toolName` on the wire\n * (`commonName ?? nativeName`). Schemas transcribed from\n * `@anthropic-ai/claude-agent-sdk`'s `agentSdkTypes.d.ts`.\n *\n * `MCP` (the generic proxy tool inside the Claude Code SDK) is intentionally\n * omitted — the bridge filters out `mcp__harness-tools__*` tool names before\n * emitting them, and other MCP invocations come through with their own\n * server-tool names rather than the literal `'Mcp'` token.\n */\nconst CLAUDE_CODE_BUILTIN_TOOLS = {\n read: commonTool('read', {\n nativeName: 'Read',\n toolUseKind: 'readonly',\n description: 'Read file contents (text, image, PDF, notebook)',\n inputSchema: z.object({\n file_path: z.string(),\n offset: z.number().optional(),\n limit: z.number().optional(),\n pages: z.string().optional(),\n }),\n }),\n write: commonTool('write', {\n nativeName: 'Write',\n toolUseKind: 'edit',\n description: 'Overwrite or create a file at an absolute path',\n inputSchema: z.object({\n file_path: z.string(),\n content: z.string(),\n }),\n }),\n edit: commonTool('edit', {\n nativeName: 'Edit',\n toolUseKind: 'edit',\n description: 'Edit a file by exact string replacement',\n inputSchema: z.object({\n file_path: z.string(),\n old_string: z.string(),\n new_string: z.string(),\n replace_all: z.boolean().optional(),\n }),\n }),\n bash: commonTool('bash', {\n nativeName: 'Bash',\n toolUseKind: 'bash',\n description: 'Execute a shell command, optionally in background',\n inputSchema: z.object({\n command: z.string(),\n timeout: z.number().optional(),\n description: z.string().optional(),\n run_in_background: z.boolean().optional(),\n dangerouslyDisableSandbox: z.boolean().optional(),\n }),\n }),\n glob: commonTool('glob', {\n nativeName: 'Glob',\n toolUseKind: 'readonly',\n description: 'Fast file-pattern search using glob syntax',\n inputSchema: z.object({\n pattern: z.string(),\n path: z.string().optional(),\n }),\n }),\n grep: commonTool('grep', {\n nativeName: 'Grep',\n toolUseKind: 'readonly',\n description: 'Regex search over file contents via ripgrep',\n inputSchema: z.object({\n pattern: z.string(),\n path: z.string().optional(),\n glob: z.string().optional(),\n output_mode: z\n .enum(['content', 'files_with_matches', 'count'])\n .optional(),\n '-B': z.number().optional(),\n '-A': z.number().optional(),\n '-C': z.number().optional(),\n context: z.number().optional(),\n '-n': z.boolean().optional(),\n '-i': z.boolean().optional(),\n '-o': z.boolean().optional(),\n type: z.string().optional(),\n head_limit: z.number().optional(),\n offset: z.number().optional(),\n multiline: z.boolean().optional(),\n }),\n }),\n webSearch: commonTool('webSearch', {\n nativeName: 'WebSearch',\n toolUseKind: 'readonly',\n description: 'Issue web search queries with optional domain filters',\n inputSchema: z.object({\n query: z.string(),\n allowed_domains: z.array(z.string()).optional(),\n blocked_domains: z.array(z.string()).optional(),\n }),\n }),\n\n WebFetch: tool({\n description: 'Fetch a URL and run a prompt against its content',\n inputSchema: z.object({\n url: z.string(),\n prompt: z.string(),\n }),\n }),\n NotebookEdit: tool({\n description: 'Edit, insert, or delete a Jupyter notebook cell',\n inputSchema: z.object({\n notebook_path: z.string(),\n new_source: z.string(),\n cell_id: z.string().optional(),\n cell_type: z.enum(['code', 'markdown']).optional(),\n edit_mode: z.enum(['replace', 'insert', 'delete']).optional(),\n }),\n }),\n TodoWrite: tool({\n description: 'Replace the session todo list',\n inputSchema: z.object({\n todos: z.array(\n z.object({\n content: z.string(),\n status: z.enum(['pending', 'in_progress', 'completed']),\n activeForm: z.string(),\n }),\n ),\n }),\n }),\n Agent: tool({\n description: 'Spawn a subagent with a task',\n inputSchema: z.object({\n description: z.string(),\n prompt: z.string(),\n subagent_type: z.string().optional(),\n model: z.enum(['sonnet', 'opus', 'haiku']).optional(),\n run_in_background: z.boolean().optional(),\n name: z.string().optional(),\n team_name: z.string().optional(),\n mode: z\n .enum([\n 'acceptEdits',\n 'auto',\n 'bypassPermissions',\n 'default',\n 'dontAsk',\n 'plan',\n ])\n .optional(),\n isolation: z.literal('worktree').optional(),\n }),\n }),\n TaskCreate: tool({\n description: 'Create a task in the session-local task list',\n inputSchema: z.object({\n subject: z.string(),\n description: z.string(),\n activeForm: z.string().optional(),\n metadata: z.record(z.string(), z.unknown()).optional(),\n }),\n }),\n TaskGet: tool({\n description: 'Retrieve a task by id',\n inputSchema: z.object({ taskId: z.string() }),\n }),\n TaskUpdate: tool({\n description: 'Update fields of an existing task',\n inputSchema: z.object({\n taskId: z.string(),\n subject: z.string().optional(),\n description: z.string().optional(),\n activeForm: z.string().optional(),\n status: z\n .union([\n z.enum(['pending', 'in_progress', 'completed']),\n z.literal('deleted'),\n ])\n .optional(),\n addBlocks: z.array(z.string()).optional(),\n addBlockedBy: z.array(z.string()).optional(),\n owner: z.string().optional(),\n metadata: z.record(z.string(), z.unknown()).optional(),\n }),\n }),\n TaskList: tool({\n description: 'Return all tasks in the session-local task list',\n inputSchema: z.object({}),\n }),\n TaskStop: tool({\n description: 'Stop a running background task by id',\n inputSchema: z.object({\n task_id: z.string().optional(),\n shell_id: z.string().optional(),\n }),\n }),\n TaskOutput: tool({\n description: 'Poll for output from a background task',\n inputSchema: z.object({\n task_id: z.string(),\n block: z.boolean(),\n timeout: z.number(),\n }),\n }),\n ListMcpResources: tool({\n description: 'List resources available from MCP servers',\n inputSchema: z.object({ server: z.string().optional() }),\n }),\n ReadMcpResource: tool({\n description: 'Read a specific MCP resource by URI',\n inputSchema: z.object({ server: z.string(), uri: z.string() }),\n }),\n ExitPlanMode: tool({\n description: 'Exit plan mode with optional permission approvals',\n inputSchema: z\n .object({\n allowedPrompts: z\n .array(\n z.object({\n tool: z.literal('Bash'),\n prompt: z.string(),\n }),\n )\n .optional(),\n })\n .passthrough(),\n }),\n EnterWorktree: tool({\n description: 'Create or enter an isolated git worktree',\n inputSchema: z.object({\n name: z.string().optional(),\n path: z.string().optional(),\n }),\n }),\n ExitWorktree: tool({\n description: 'Exit the current worktree session',\n inputSchema: z.object({\n action: z.enum(['keep', 'remove']),\n discard_changes: z.boolean().optional(),\n }),\n }),\n AskUserQuestion: tool({\n description: 'Ask the user multiple-choice questions via a structured UI',\n inputSchema: z.object({\n questions: z\n .array(\n z.object({\n question: z.string(),\n header: z.string(),\n options: z.array(\n z.object({\n label: z.string(),\n description: z.string(),\n preview: z.string().optional(),\n }),\n ),\n multiSelect: z.boolean(),\n }),\n )\n .min(1)\n .max(4),\n answers: z.record(z.string(), z.string()).optional(),\n annotations: z\n .record(\n z.string(),\n z.object({\n preview: z.string().optional(),\n notes: z.string().optional(),\n }),\n )\n .optional(),\n metadata: z.object({ source: z.string().optional() }).optional(),\n }),\n }),\n Skill: tool({\n description: 'Activate a skill by name',\n inputSchema: z.object({\n skill: z.string(),\n args: z.string().optional(),\n }),\n }),\n} as const satisfies Record<string, HarnessV1BuiltinTool<any, any>>;\n\n/*\n * Bootstrap lives in /tmp because it's pure derived state — the harness can\n * reinstall the CLI and bridge files on any fresh sandbox from the recipe.\n * Persistence comes from the sandbox provider's snapshot, not the path.\n *\n * The session work dir (`startOpts.sessionWorkDir`) and the bridge-state dir\n * derived from `sandboxSession.defaultWorkingDirectory` both live under the sandbox's\n * default working directory — the provider's persistent mount — so the\n * workdir's CLI state (Claude's `~/.claude/projects/<dir>/*.jsonl` thread\n * history is keyed by working directory) and the bridge state files survive\n * both detach -> attach/replay and stop -> snapshot -> resume cycles.\n */\nconst BOOTSTRAP_DIR = '/tmp/harness/claude-code';\n\n/**\n * Live bridge coordinates returned by `doDetach()` and `doSuspendTurn()`. A\n * future process uses them to reopen a socket to the still-running bridge\n * (`attach`) instead of re-spawning it. Absent on a `doStop()` payload.\n */\nconst claudeCodeBridgeCoordsSchema = z.object({\n port: z.number(),\n token: z.string(),\n lastSeenEventId: z.number(),\n sandboxId: z.string().optional(),\n});\n\n/**\n * Schema for the adapter-specific portion of lifecycle state `data`.\n *\n * A `doStop()` payload is structurally empty (`{}`): the framework derives the\n * sandbox via `provider.resumeSession({ sessionId })`, and the Claude SDK's\n * `{ continue: true }` flag rehydrates the thread from the workdir. A\n * `doDetach()` payload additionally carries `bridge` coordinates for\n * cross-process `attach`. `.passthrough()` keeps both shapes valid.\n */\nconst claudeCodeResumeStateSchema = z\n .object({ bridge: claudeCodeBridgeCoordsSchema.optional() })\n .passthrough();\n\ntype ClaudeCodeBridgeCoords = z.infer<typeof claudeCodeBridgeCoordsSchema>;\n\nexport function createClaudeCode(\n settings: ClaudeCodeHarnessSettings = {},\n): HarnessV1<typeof CLAUDE_CODE_BUILTIN_TOOLS> {\n let cachedBootstrap: HarnessV1Bootstrap | undefined;\n\n return {\n specificationVersion: 'harness-v1',\n harnessId: 'claude-code',\n builtinTools: CLAUDE_CODE_BUILTIN_TOOLS,\n supportsBuiltinToolApprovals: true,\n lifecycleStateSchema: claudeCodeResumeStateSchema,\n getBootstrap: async () => {\n if (cachedBootstrap != null) return cachedBootstrap;\n const [pkg, lock, bridge] = await Promise.all([\n readBridgeAsset('package.json'),\n readBridgeAsset('pnpm-lock.yaml'),\n readBridgeAsset('index.mjs'),\n ]);\n cachedBootstrap = {\n harnessId: 'claude-code',\n bootstrapDir: BOOTSTRAP_DIR,\n files: [\n { path: `${BOOTSTRAP_DIR}/package.json`, content: pkg },\n { path: `${BOOTSTRAP_DIR}/pnpm-lock.yaml`, content: lock },\n { path: `${BOOTSTRAP_DIR}/bridge.mjs`, content: bridge },\n ],\n commands: [\n { command: `mkdir -p ${BOOTSTRAP_DIR}` },\n {\n command: `pnpm --dir ${BOOTSTRAP_DIR} install --frozen-lockfile --store-dir ${BOOTSTRAP_DIR}/.pnpm-store`,\n },\n {\n command: `cd ${BOOTSTRAP_DIR} && if [ -f node_modules/@anthropic-ai/claude-code/install.cjs ]; then node node_modules/@anthropic-ai/claude-code/install.cjs; fi && ./node_modules/.bin/claude --version`,\n },\n ],\n };\n return cachedBootstrap;\n },\n doStart: async startOpts => {\n const sandboxSession = startOpts.sandboxSession;\n const session = sandboxSession.restricted();\n const sandboxId = sandboxSession.id;\n const lifecycleState = startOpts.continueFrom ?? startOpts.resumeFrom;\n const isResume = lifecycleState != null;\n const isContinue = startOpts.continueFrom != null;\n const coords = isResume\n ? (lifecycleState?.data as { bridge?: ClaudeCodeBridgeCoords })?.bridge\n : undefined;\n\n const workDir = startOpts.sessionWorkDir;\n const sessionDataDir = `${sandboxSession.defaultWorkingDirectory}/.agent-runs/${startOpts.sessionId}`;\n const bridgeStateDir = `${sessionDataDir}/bridge`;\n const timeoutMs = settings.startupTimeoutMs ?? 120_000;\n\n // Normalize each forwarded bridge diagnostics frame into the general\n // `HarnessV1Diagnostic` and report it. The adapter does no telemetry work\n // beyond this transport→emission mapping.\n const report = startOpts.observability?.report;\n const onDiagnostic = report\n ? (frame: Parameters<typeof harnessV1DiagnosticFromBridgeFrame>[0]) =>\n report(\n harnessV1DiagnosticFromBridgeFrame(frame, {\n sessionId: startOpts.sessionId,\n timestamp: Date.now(),\n }),\n )\n : undefined;\n\n // Builds the `connect` thunk a `SandboxChannel` re-invokes on every\n // (re)connect: open the socket, then wait for `bridge-hello` so the\n // end-to-end link is proven live before any frame is sent.\n const buildConnect = (wsUrl: string) => async (): Promise<WebSocket> => {\n return openBridgeWebSocket({ wsUrl, timeoutMs });\n };\n\n /*\n * Rung 1 — ATTACH. When lifecycle state carries live bridge coordinates,\n * try to reopen a socket to the still-running bridge. Parked sessions wait\n * for the next `start`; suspended turns request replay of everything past\n * the persisted cursor. No spawn, no fresh token (the existing bridge\n * still authorises the persisted one). If the bridge is gone the open\n * throws and we fall through to a spawn-based recovery.\n */\n if (coords) {\n try {\n const attachUrl =\n (await sandboxSession.getPortUrl({\n port: coords.port,\n protocol: 'ws',\n })) + `?agent_bridge_token=${encodeURIComponent(coords.token)}`;\n const attachChannel: ClaudeCodeChannel = new SandboxChannel({\n connect: buildConnect(attachUrl),\n outboundSchema: outboundMessageSchema,\n initialLastSeenEventId: coords.lastSeenEventId,\n onDiagnostic,\n });\n await attachChannel.open(isContinue ? { resume: true } : undefined);\n return createSession({\n sessionId: startOpts.sessionId,\n channel: attachChannel,\n // The live bridge was spawned by another process; this one owns no\n // process handle. The session lifecycle method decides whether the\n // sandbox is left running, stopped, or destroyed.\n proc: undefined,\n model: settings.model,\n maxTurns: settings.maxTurns,\n thinking: settings.thinking,\n isResume: true,\n continueOnFirstPrompt: false,\n rerunContinue: false,\n bridgePort: coords.port,\n bridgeToken: coords.token,\n sandboxId,\n debug: startOpts.observability?.debug,\n permissionMode: startOpts.permissionMode,\n skills: startOpts.skills ?? [],\n });\n } catch {\n // Bridge no longer reachable — recover by respawning below.\n }\n }\n\n /*\n * Rungs 2/3 — REPLAY vs RERUN. Respawn the bridge. `replay` is only sound\n * for `continueFrom`: those coordinates include the cursor the on-disk\n * log is replayed *from*. `resumeFrom` is a between-turn resume; even when\n * it carries bridge coordinates, replaying the previous turn would\n * re-deliver stale events into the next turn. Those resumes always `rerun`\n * when attach is unavailable (the CLI continues its own thread from the\n * workdir snapshot via `continue: true`).\n */\n let respawnStrategy: ClaudeCodeRespawnStrategy | undefined = isResume\n ? 'rerun'\n : undefined;\n if (coords && isContinue) {\n const logRaw = await Promise.resolve(\n session.readTextFile({\n path: `${bridgeStateDir}/event-log.ndjson`,\n abortSignal: startOpts.abortSignal,\n }),\n ).catch(() => null);\n if ((await classifyDiskLog(logRaw)) === 'replay') {\n respawnStrategy = 'replay';\n }\n }\n\n const sandboxHomeDir =\n startOpts.skills && startOpts.skills.length > 0\n ? await resolveSandboxHomeDir({\n sandbox: session,\n abortSignal: startOpts.abortSignal,\n })\n : undefined;\n const port = resolveBridgePort(sandboxSession, settings.port);\n const token = randomBytes(32).toString('hex');\n const env = {\n ...resolveClaudeCodeEnv(settings.auth),\n BRIDGE_CHANNEL_TOKEN: token,\n BRIDGE_WS_PORT: String(port),\n ...(sandboxHomeDir ? { HOME: sandboxHomeDir } : {}),\n ...(respawnStrategy === 'replay'\n ? { BRIDGE_REPLAY_FROM_DISK: '1' }\n : {}),\n };\n\n /*\n * On a fresh start the workdir, skill files, and bridge-state directory\n * must be created. On any resume they already exist in the\n * sandbox snapshot, so skip the rewrite. The env is sent fresh on every\n * spawn — `BRIDGE_CHANNEL_TOKEN` rotates per start.\n */\n if (respawnStrategy === undefined) {\n await session.run({\n command: `mkdir -p ${workDir} ${bridgeStateDir}`,\n abortSignal: startOpts.abortSignal,\n });\n\n if (startOpts.skills && startOpts.skills.length > 0) {\n if (!sandboxHomeDir) {\n throw new Error('Unable to resolve sandbox HOME directory.');\n }\n await writeSkills({\n sandbox: session,\n homeDir: sandboxHomeDir,\n skills: startOpts.skills,\n abortSignal: startOpts.abortSignal,\n });\n }\n }\n\n await markBridgeStarting({\n sandbox: session,\n bridgeStateDir,\n bridgeType: 'claude-code',\n abortSignal: startOpts.abortSignal,\n });\n\n const proc = await session.spawn({\n command: `node ${BOOTSTRAP_DIR}/bridge.mjs --workdir ${workDir} --bridge-state-dir ${bridgeStateDir}`,\n env,\n abortSignal: startOpts.abortSignal,\n });\n\n const bridgeStartupStderr: string[] = [];\n /*\n * Bridge stderr is the only diagnostic channel for sandbox-side crashes\n * and startup failures. Start forwarding before `bridge-ready`, otherwise\n * module-resolution, auth, and syntax errors can be lost behind the\n * generic startup failure below.\n */\n const bridgeStartupStderrDone = forwardBridgeStderr({\n stream: proc.stderr,\n collectTail: bridgeStartupStderr,\n });\n void bridgeStartupStderrDone;\n const { port: boundPort } = await waitForBridgeReady({\n proc,\n sandbox: session,\n bridgeStateDir,\n bridgeType: 'claude-code',\n timeoutMs,\n abortSignal: startOpts.abortSignal,\n createTimeoutError: ({ proc, stdoutTail }) =>\n createBridgeStartupError({\n message: 'claude-code bridge did not become ready in time.',\n proc,\n stdoutTail,\n stderrTail: bridgeStartupStderr,\n stderrDone: bridgeStartupStderrDone,\n }),\n createExitError: ({ proc, stdoutTail }) =>\n createBridgeStartupError({\n message: 'claude-code bridge exited before becoming ready.',\n proc,\n stdoutTail,\n stderrTail: bridgeStartupStderr,\n stderrDone: bridgeStartupStderrDone,\n }),\n });\n void drainRest(proc.stdout);\n\n const wsUrl =\n (await sandboxSession.getPortUrl({\n port: boundPort,\n protocol: 'ws',\n })) + `?agent_bridge_token=${encodeURIComponent(token)}`;\n\n const channel: ClaudeCodeChannel = new SandboxChannel({\n connect: buildConnect(wsUrl),\n outboundSchema: outboundMessageSchema,\n onDiagnostic,\n // In replay mode the respawned bridge reloaded the finished turn from\n // disk; seed the cursor and resume so it streams the tail (incl.\n // `finish`) rather than starting empty.\n ...(respawnStrategy === 'replay'\n ? { initialLastSeenEventId: coords?.lastSeenEventId ?? 0 }\n : {}),\n });\n await channel.open(\n respawnStrategy === 'replay' ? { resume: true } : undefined,\n );\n\n return createSession({\n sessionId: startOpts.sessionId,\n channel,\n proc,\n model: settings.model,\n maxTurns: settings.maxTurns,\n thinking: settings.thinking,\n isResume: respawnStrategy !== undefined,\n continueOnFirstPrompt: respawnStrategy !== undefined,\n rerunContinue: respawnStrategy === 'rerun',\n bridgePort: boundPort,\n bridgeToken: token,\n sandboxId,\n debug: startOpts.observability?.debug,\n permissionMode: startOpts.permissionMode,\n skills: startOpts.skills ?? [],\n });\n },\n };\n}\n\nfunction resolveBridgePort(\n sandboxSession: HarnessV1NetworkSandboxSession,\n override: number | undefined,\n): number {\n if (override !== undefined) return override;\n if (sandboxSession.ports.length > 0) return sandboxSession.ports[0];\n throw new HarnessCapabilityUnsupportedError({\n harnessId: 'claude-code',\n message:\n 'The claude-code harness needs a TCP port exposed by the sandbox. ' +\n 'Create the sandbox with `ports: [<port>]` or pass `createClaudeCode({ port })`.',\n });\n}\n\n/**\n * Materialise skill files into\n * `$HOME/.claude/skills/<name>/SKILL.md`. The `claude` CLI\n * auto-discovers skills from that directory on startup, so the files have to\n * be in place before the bridge is spawned without mutating the session\n * workdir. Each file uses the YAML-frontmatter shape the CLI expects.\n */\nasync function writeSkills({\n sandbox,\n homeDir,\n skills,\n abortSignal,\n}: {\n sandbox: Experimental_SandboxSession;\n homeDir: string;\n skills: ReadonlyArray<HarnessV1Skill>;\n abortSignal?: AbortSignal;\n}): Promise<void> {\n for (const skill of skills) {\n safeClaudeSkillName(skill.name);\n for (const file of skill.files ?? []) {\n safeClaudeSkillFilePath({\n skillName: skill.name,\n filePath: file.path,\n });\n }\n }\n\n await sandbox.run({\n command: `mkdir -p ${shellQuote(homeDir)}/.claude/skills`,\n abortSignal,\n });\n for (const skill of skills) {\n const name = safeClaudeSkillName(skill.name);\n const skillDir = `${homeDir}/.claude/skills/${name}`;\n const path = `${skillDir}/SKILL.md`;\n const content = `---\\nname: ${skill.name}\\ndescription: ${skill.description}\\n---\\n\\n${skill.content}\\n`;\n await sandbox.writeTextFile({ path, content, abortSignal });\n for (const file of skill.files ?? []) {\n const filePath = safeClaudeSkillFilePath({\n skillName: skill.name,\n filePath: file.path,\n });\n await sandbox.writeTextFile({\n path: `${skillDir}/${filePath}`,\n content: file.content,\n abortSignal,\n });\n }\n }\n}\n\nasync function resolveSandboxHomeDir({\n sandbox,\n abortSignal,\n}: {\n sandbox: Experimental_SandboxSession;\n abortSignal?: AbortSignal;\n}): Promise<string> {\n const result = await sandbox.run({\n command: 'printf \"%s\" \"$HOME\"',\n abortSignal,\n });\n const homeDir = result.stdout.trim();\n if (result.exitCode !== 0 || !homeDir || !path.posix.isAbsolute(homeDir)) {\n throw new Error(\n `Unable to resolve sandbox HOME directory: ${result.stderr || result.stdout}`,\n );\n }\n return homeDir;\n}\n\nfunction safeClaudeSkillName(name: string): string {\n if (!/^[A-Za-z0-9._-]+$/.test(name) || name === '.' || name === '..') {\n throw new Error(`Invalid Claude Code skill name: ${name}`);\n }\n return name;\n}\n\nfunction safeClaudeSkillFilePath({\n skillName,\n filePath,\n}: {\n skillName: string;\n filePath: string;\n}): string {\n const normalized = path.posix.normalize(filePath);\n if (\n normalized === '.' ||\n normalized.startsWith('../') ||\n path.posix.isAbsolute(normalized)\n ) {\n throw new Error(\n `Invalid Claude Code skill file path for ${skillName}: ${filePath}`,\n );\n }\n return normalized;\n}\n\nfunction shellQuote(value: string): string {\n return `'${value.replace(/'/g, `'\\\\''`)}'`;\n}\n\nasync function readBridgeAsset(name: string): Promise<string> {\n const candidates = [\n new URL(`./bridge/${name}`, import.meta.url),\n new URL(`../bridge/${name}`, import.meta.url),\n ];\n let lastErr: unknown;\n for (const url of candidates) {\n try {\n return await readFile(fileURLToPath(url), 'utf8');\n } catch (err) {\n const code = (err as NodeJS.ErrnoException).code;\n if (code !== 'ENOENT') throw err;\n lastErr = err;\n }\n }\n throw lastErr ?? new Error(`bridge asset not found: ${name}`);\n}\n\nasync function createBridgeStartupError({\n message,\n proc,\n stdoutTail,\n stderrTail,\n stderrDone,\n}: {\n message: string;\n proc: Experimental_SandboxProcess;\n stdoutTail: string[];\n stderrTail: string[];\n stderrDone: Promise<void>;\n}): Promise<Error> {\n await Promise.race([\n stderrDone,\n new Promise<void>(resolve => setTimeout(resolve, 250)),\n ]).catch(() => {});\n\n let exitStatus = '';\n try {\n const result = (await Promise.race([\n proc.wait(),\n new Promise<undefined>(resolve => setTimeout(resolve, 250)),\n ])) as { exitCode?: number } | undefined;\n if (result?.exitCode !== undefined) {\n exitStatus = ` Exit code: ${result.exitCode}.`;\n }\n } catch {}\n\n const details: string[] = [];\n if (stdoutTail.length > 0) {\n details.push(`stdout:\\n${stdoutTail.join('\\n')}`);\n }\n if (stderrTail.length > 0) {\n details.push(`stderr:\\n${stderrTail.join('\\n')}`);\n }\n\n return new Error(\n `${message}${exitStatus}${\n details.length > 0 ? `\\n\\n${details.join('\\n\\n')}` : ''\n }`,\n );\n}\n\nfunction lineDecoder() {\n let buffer = '';\n return {\n push(chunk: string): string[] {\n buffer += chunk;\n const lines: string[] = [];\n let nl: number;\n while ((nl = buffer.indexOf('\\n')) !== -1) {\n const raw = buffer.slice(0, nl);\n buffer = buffer.slice(nl + 1);\n const line = raw.replace(/\\r$/, '').trim();\n if (line.length > 0) lines.push(line);\n }\n return lines;\n },\n flush(): string[] {\n const line = buffer.replace(/\\r$/, '').trim();\n buffer = '';\n return line.length > 0 ? [line] : [];\n },\n };\n}\n\nasync function forwardBridgeStderr({\n stream,\n collectTail,\n}: {\n stream: ReadableStream<Uint8Array>;\n collectTail?: string[];\n}): Promise<void> {\n try {\n const reader = stream.pipeThrough(new TextDecoderStream()).getReader();\n const decoder = lineDecoder();\n while (true) {\n const { value, done } = await reader.read();\n if (done) {\n for (const line of decoder.flush()) {\n const trimmed = line.trim();\n if (!trimmed) continue;\n if (collectTail) {\n collectTail.push(trimmed);\n if (collectTail.length > 20) collectTail.shift();\n }\n // eslint-disable-next-line no-console\n console.log(`[bridge stderr] ${trimmed}`);\n }\n return;\n }\n if (value) {\n for (const line of decoder.push(value)) {\n const trimmed = line.trim();\n if (!trimmed) continue;\n if (collectTail) {\n collectTail.push(trimmed);\n if (collectTail.length > 20) collectTail.shift();\n }\n // eslint-disable-next-line no-console\n console.log(`[bridge stderr] ${trimmed}`);\n }\n }\n }\n } catch {\n // Reader errors are non-fatal — best-effort diagnostic only.\n }\n}\n\nasync function drainRest(stream: ReadableStream<Uint8Array>): Promise<void> {\n try {\n const reader = stream.pipeThrough(new TextDecoderStream()).getReader();\n while (true) {\n const { done } = await reader.read();\n if (done) return;\n }\n } catch {}\n}\n\n/**\n * Wait for the bridge's `bridge-hello` message to arrive on the freshly\n * opened WebSocket before any other host-side code touches it.\n *\n * Some sandbox runtimes (Vercel in particular) complete the WS upgrade\n * with the host long before the connection is actually forwarded to the\n * sandbox-side bridge. Anything sent in that gap is silently dropped —\n * including the `start` message we send next. The bridge emits\n * `bridge-hello` the instant it accepts the connection, so receiving it\n * is the only reliable evidence that the end-to-end link is live.\n */\nasync function waitForBridgeHello({\n ws,\n timeoutMs,\n}: {\n ws: WebSocket;\n timeoutMs: number;\n}): Promise<void> {\n await new Promise<void>((resolve, reject) => {\n let settled = false;\n const cleanup = () => {\n ws.off('message', onMessage);\n ws.off('close', onClose);\n ws.off('error', onError);\n if (timer) clearTimeout(timer);\n };\n const settle = (err?: unknown) => {\n if (settled) return;\n settled = true;\n cleanup();\n if (err) reject(err);\n else resolve();\n };\n const onMessage = (raw: unknown) => {\n try {\n const text =\n typeof raw === 'string'\n ? raw\n : (raw as Buffer | ArrayBufferLike).toString\n ? (raw as Buffer).toString('utf8')\n : String(raw);\n const parsed = JSON.parse(text) as { type?: unknown };\n if (parsed?.type === 'bridge-hello') settle();\n } catch {\n // Ignore malformed frames while waiting.\n }\n };\n const onClose = () => {\n settle(\n new Error('claude-code bridge closed before sending bridge-hello'),\n );\n };\n const onError = (err: Error) => settle(err);\n const timer = setTimeout(\n () =>\n settle(\n new Error(\n `claude-code bridge did not send bridge-hello within ${timeoutMs}ms`,\n ),\n ),\n timeoutMs,\n );\n timer.unref?.();\n ws.on('message', onMessage);\n ws.on('close', onClose);\n ws.on('error', onError);\n });\n}\n\nasync function openBridgeWebSocket({\n wsUrl,\n timeoutMs,\n}: {\n wsUrl: string;\n timeoutMs: number;\n}): Promise<WebSocket> {\n const deadline = Date.now() + timeoutMs;\n let attempt = 0;\n let lastError: unknown;\n\n while (Date.now() < deadline) {\n attempt++;\n let ws: WebSocket | undefined;\n try {\n const remaining = Math.max(1, deadline - Date.now());\n ws = await openWebSocket({\n url: wsUrl,\n timeoutMs: Math.min(10_000, remaining),\n });\n await waitForBridgeHello({\n ws,\n timeoutMs: Math.min(5_000, Math.max(1, deadline - Date.now())),\n });\n return ws;\n } catch (err) {\n lastError = err;\n try {\n ws?.close();\n } catch {}\n const remaining = deadline - Date.now();\n if (remaining <= 0) break;\n await sleep(Math.min(250 * attempt, 1_000, remaining));\n }\n }\n\n throw new Error(\n `claude-code bridge did not complete WebSocket handshake within ${timeoutMs}ms after ${attempt} attempt(s). Last error: ${formatUnknownError(lastError)}`,\n );\n}\n\nfunction openWebSocket({\n url,\n timeoutMs,\n}: {\n url: string;\n timeoutMs: number;\n}): Promise<WebSocket> {\n return new Promise((resolve, reject) => {\n const ws = new WebSocket(url);\n const timer = setTimeout(() => {\n cleanup();\n try {\n ws.terminate();\n } catch {}\n reject(new Error(`WebSocket open timed out after ${timeoutMs}ms`));\n }, timeoutMs);\n timer.unref?.();\n const cleanup = () => {\n clearTimeout(timer);\n ws.off('open', onOpen);\n ws.off('error', onError);\n };\n const onOpen = () => {\n cleanup();\n resolve(ws);\n };\n const onError = (err: Error) => {\n cleanup();\n reject(err);\n };\n ws.once('open', onOpen);\n ws.once('error', onError);\n });\n}\n\nfunction sleep(ms: number): Promise<void> {\n return new Promise(resolve => {\n const timer = setTimeout(resolve, ms);\n timer.unref?.();\n });\n}\n\nfunction formatUnknownError(error: unknown): string {\n if (error instanceof Error) return error.message;\n return String(error);\n}\n\nfunction createSession({\n sessionId,\n channel,\n proc,\n model,\n maxTurns,\n thinking,\n isResume,\n continueOnFirstPrompt,\n rerunContinue,\n bridgePort,\n bridgeToken,\n sandboxId,\n debug,\n permissionMode,\n skills,\n}: {\n sessionId: string;\n channel: ClaudeCodeChannel;\n /** Undefined on `attach` — the live bridge was spawned by another process. */\n proc: Experimental_SandboxProcess | undefined;\n model: string | undefined;\n maxTurns: number | undefined;\n thinking: 'off' | 'on' | 'adaptive' | undefined;\n isResume: boolean;\n continueOnFirstPrompt: boolean;\n rerunContinue: boolean;\n bridgePort: number;\n bridgeToken: string;\n sandboxId: string;\n debug: HarnessV1DebugConfig | undefined;\n permissionMode: HarnessV1PermissionMode | undefined;\n skills: ReadonlyArray<HarnessV1Skill>;\n}): HarnessV1Session {\n let stopped = false;\n let stopPromise: Promise<void> | undefined;\n /*\n * Force the Claude SDK's `continue: true` on the first prompt only when the\n * bridge was respawned (rerun/replay): a fresh bridge process treats its\n * first turn as new, so it must be told to rehydrate the workdir thread. An\n * `attach`ed bridge is already past its first turn and continues on its own.\n */\n let pendingResumeFlag = continueOnFirstPrompt;\n /*\n * Instructions are prepended to the first user message of a fresh session\n * only. A resumed session (attach/replay/rerun) already carried them in its\n * original first message (preserved in the workdir snapshot), so it starts\n * \"applied\".\n */\n let instructionsApplied = isResume;\n\n /*\n * Wire the channel into one turn's worth of events and return the control\n * surface. Shared by `doPromptTurn` (which sends a `start` afterwards) and\n * `doContinueTurn` (which attaches to an already-running/replayed turn, or sends\n * a rerun `start`). The only difference between the two entry points is the\n * `start` message, not the listener/abort/settle plumbing.\n */\n const wireTurn = (turnOpts: {\n emit: (event: HarnessV1StreamPart) => void;\n abortSignal?: AbortSignal;\n }): HarnessV1PromptControl => {\n let pendingResolve: (() => void) | undefined;\n let pendingReject: ((err: unknown) => void) | undefined;\n const done = new Promise<void>((resolve, reject) => {\n pendingResolve = resolve;\n pendingReject = reject;\n });\n\n const unsubs: Array<() => void> = [];\n const forward = (event: HarnessV1StreamPart) => {\n try {\n turnOpts.emit(event);\n } catch {}\n };\n\n let isSettled = false;\n const settleSuccess = () => {\n if (isSettled) return;\n isSettled = true;\n for (const u of unsubs) u();\n pendingResolve!();\n };\n const settleError = (err: unknown) => {\n if (isSettled) return;\n isSettled = true;\n for (const u of unsubs) u();\n pendingReject!(err);\n };\n\n const eventTypes = [\n 'stream-start',\n 'text-start',\n 'text-delta',\n 'text-end',\n 'reasoning-start',\n 'reasoning-delta',\n 'reasoning-end',\n 'tool-call',\n 'tool-approval-request',\n 'tool-result',\n 'finish-step',\n 'raw',\n ] as const;\n for (const type of eventTypes) {\n unsubs.push(\n channel.on(type, msg => {\n forward(msg);\n }),\n );\n }\n unsubs.push(\n channel.on('finish', msg => {\n forward(msg);\n settleSuccess();\n }),\n );\n unsubs.push(\n channel.on('error', msg => {\n forward(msg);\n settleError(msg.error);\n }),\n );\n\n /*\n * A `'suspended'` close is a graceful slice-boundary freeze the host\n * initiated (`doSuspendTurn`): the turn keeps running in the bridge and its\n * tail is replayed to the next process, so wind this turn down cleanly\n * rather than failing it. Any other close mid-turn is an unexpected drop.\n */\n const onClose = (_code?: number, reason?: string) => {\n if (isSettled) return;\n if (reason === 'suspended') {\n settleSuccess();\n return;\n }\n settleError(\n new Error('claude-code bridge closed before the turn finished.'),\n );\n };\n channel.onClose(onClose);\n\n const onAbort = () => {\n if (isSettled) return;\n try {\n channel.send({ type: 'abort' });\n } catch {}\n settleError(\n turnOpts.abortSignal?.reason ??\n new DOMException('Aborted', 'AbortError'),\n );\n };\n if (turnOpts.abortSignal) {\n if (turnOpts.abortSignal.aborted) {\n onAbort();\n } else {\n turnOpts.abortSignal.addEventListener('abort', onAbort, {\n once: true,\n });\n }\n }\n\n return {\n submitToolResult: async input => {\n channel.send({\n type: 'tool-result',\n toolCallId: input.toolCallId,\n output: input.output,\n isError: input.isError,\n });\n },\n submitToolApproval: async input => {\n channel.send({\n type: 'tool-approval-response',\n approvalId: input.approvalId,\n approved: input.approved,\n reason: input.reason,\n });\n },\n submitUserMessage: async text => {\n channel.send({ type: 'user-message', text });\n },\n done,\n };\n };\n\n return {\n sessionId,\n isResume,\n modelId: model,\n doPromptTurn: async promptOpts => {\n const control = wireTurn({\n emit: promptOpts.emit,\n abortSignal: promptOpts.abortSignal,\n });\n\n let promptText = extractUserText(promptOpts.prompt);\n if (!instructionsApplied && promptOpts.instructions) {\n promptText = frameInstructions(promptOpts.instructions, promptText);\n }\n instructionsApplied = true;\n\n const startMessage = {\n type: 'start' as const,\n prompt: promptText,\n tools: (promptOpts.tools ?? []).map(t => ({\n name: t.name,\n description: t.description,\n inputSchema: t.inputSchema,\n })),\n model,\n maxTurns,\n thinking,\n ...(skills.length > 0\n ? { skills: skills.map(skill => skill.name) }\n : {}),\n ...(permissionMode ? { permissionMode } : {}),\n ...(debug ? { debug } : {}),\n ...(pendingResumeFlag ? { continue: true } : {}),\n };\n pendingResumeFlag = false;\n channel.send(startMessage);\n\n return control;\n },\n doContinueTurn: async continueOpts => {\n const control = wireTurn({\n emit: continueOpts.emit,\n abortSignal: continueOpts.abortSignal,\n });\n\n /*\n * attach / replay: the still-running (or disk-replayed) turn streams into\n * the wired listeners — `doStart` opened the channel with `{ resume: true }`\n * so the bridge replays everything past the persisted cursor (including a\n * `finish` if the turn ended during the gap). No `start` is sent: issuing\n * one would clear the bridge's replay log and begin a new turn. Lossless.\n *\n * rerun: the bridge was respawned with no in-flight turn to attach to, so\n * re-drive the runtime's own thread from the workdir snapshot via\n * `continue: true`. Lossy — work in flight at the interruption is\n * recomputed. This is the rare bridge-died fallback; the common slice path\n * is `attach`.\n */\n if (rerunContinue) {\n pendingResumeFlag = false;\n channel.send({\n type: 'start' as const,\n /*\n * A continuation nudge rather than an empty prompt: `continue: true`\n * rehydrates the prior thread, and this is the new user turn that\n * drives it forward. It must be non-empty — an empty text block is\n * rejected by the Anthropic API once the SDK stamps it with\n * `cache_control`.\n */\n prompt: 'Continue.',\n tools: (continueOpts.tools ?? []).map(t => ({\n name: t.name,\n description: t.description,\n inputSchema: t.inputSchema,\n })),\n model,\n maxTurns,\n thinking,\n ...(skills.length > 0\n ? { skills: skills.map(skill => skill.name) }\n : {}),\n ...(permissionMode ? { permissionMode } : {}),\n ...(debug ? { debug } : {}),\n continue: true,\n });\n }\n\n return control;\n },\n doCompact: async (customInstructions?: string) => {\n /*\n * Claude Code has no SDK/control method for compaction — the supported\n * trigger is the `/compact` slash command submitted as user input. Ride\n * the existing user-message rail; the bridge feeds it into the streaming\n * query input and Claude's native compaction handles the rest, emitting a\n * `compact_boundary` + `PostCompact` we observe as a `compaction` event.\n */\n const text =\n customInstructions && customInstructions.trim()\n ? `/compact ${customInstructions.trim()}`\n : '/compact';\n channel.send({ type: 'user-message', text });\n },\n doDetach: async () => {\n if (stopped) {\n throw new Error(\n `claude-code session ${sessionId} is already stopped; cannot detach.`,\n );\n }\n stopped = true;\n const lastSeenEventId = await channel.suspend();\n const payload: HarnessV1ResumeSessionState = {\n type: 'resume-session',\n harnessId: 'claude-code',\n specificationVersion: 'harness-v1',\n data: {\n bridge: {\n port: bridgePort,\n token: bridgeToken,\n lastSeenEventId,\n sandboxId,\n },\n },\n };\n return payload;\n },\n doDestroy: async () => {\n if (stopped) return stopPromise;\n stopped = true;\n stopPromise = (async () => {\n // Tell the channel we are tearing down so the bridge's post-shutdown\n // socket close finalises instead of triggering a reconnect.\n channel.beginClose();\n try {\n if (!channel.isClosed()) {\n channel.send({ type: 'shutdown' });\n }\n } catch {}\n let stopTimer: ReturnType<typeof setTimeout> | undefined;\n try {\n if (proc) {\n await Promise.race([\n proc.wait(),\n new Promise<void>(resolve => {\n stopTimer = setTimeout(resolve, 5000);\n stopTimer.unref?.();\n }),\n ]);\n }\n } finally {\n if (stopTimer) clearTimeout(stopTimer);\n try {\n await proc?.kill();\n } catch {}\n channel.close();\n }\n })();\n return stopPromise;\n },\n doStop: async () => {\n if (stopped) {\n throw new Error(\n `claude-code session ${sessionId} is already stopped; cannot stop.`,\n );\n }\n stopped = true;\n /*\n * If the bridge's channel already closed (e.g. mid-turn WS drop)\n * there is no one to ack a `detach` message. Synthesize an empty\n * payload — for Claude Code the resume state structurally is `{}`\n * (the conversation lives in the workdir, captured by the sandbox\n * snapshot during the subsequent `sandboxSession.stop()`), so we\n * lose nothing by skipping the round-trip.\n */\n // Tell the channel we are tearing down so the bridge's post-detach\n // socket close finalises instead of triggering a reconnect.\n channel.beginClose();\n const data: unknown = channel.isClosed()\n ? {}\n : await new Promise<unknown>((resolve, reject) => {\n const timer = setTimeout(() => {\n unsub();\n reject(\n new Error(\n `claude-code session ${sessionId} did not reply to detach within 5s.`,\n ),\n );\n }, 5000);\n timer.unref?.();\n const unsub = channel.on('bridge-detach', msg => {\n clearTimeout(timer);\n unsub();\n resolve(msg.data);\n });\n try {\n channel.send({ type: 'detach' });\n } catch (err) {\n clearTimeout(timer);\n unsub();\n reject(err);\n }\n });\n\n // The bridge exits itself ~50ms after sending bridge-detach. Give\n // it a moment, then ensure the process is reaped and the channel\n // closed.\n let stopTimer: ReturnType<typeof setTimeout> | undefined;\n try {\n if (proc) {\n await Promise.race([\n proc.wait(),\n new Promise<void>(resolve => {\n stopTimer = setTimeout(resolve, 5000);\n stopTimer.unref?.();\n }),\n ]);\n }\n } finally {\n if (stopTimer) clearTimeout(stopTimer);\n try {\n await proc?.kill();\n } catch {}\n channel.close();\n }\n\n const payload: HarnessV1ResumeSessionState = {\n type: 'resume-session',\n harnessId: 'claude-code',\n specificationVersion: 'harness-v1',\n data: (data ?? {}) as HarnessV1ResumeSessionState['data'],\n };\n return payload;\n },\n doSuspendTurn: async () => {\n if (stopped) {\n throw new Error(\n `claude-code session ${sessionId} is stopped; cannot suspend.`,\n );\n }\n stopped = true;\n /*\n * Gracefully freeze the active turn at a precise cursor. `channel.suspend`\n * stops processing inbound frames (the cursor stops advancing exactly at\n * the last delivered event), drains what was already dispatched, then\n * closes the host socket with reason `'suspended'` — which `wireTurn`'s\n * `onClose` treats as a clean turn end. The bridge keeps the turn running\n * and accumulates events past the cursor for the next slice to replay. The\n * sandbox process is deliberately left alive (no `shutdown`/`detach`).\n */\n const lastSeenEventId = await channel.suspend();\n const payload: HarnessV1ContinueTurnState = {\n type: 'continue-turn',\n harnessId: 'claude-code',\n specificationVersion: 'harness-v1',\n data: {\n bridge: {\n port: bridgePort,\n token: bridgeToken,\n lastSeenEventId,\n sandboxId,\n },\n },\n };\n return payload;\n },\n };\n}\n\n/*\n * Frame session instructions and the user's text so the runtime treats the\n * instructions as system-provided operating guidance, not something the user\n * wrote. Without the wrapper the agent can echo the prepended text back as if\n * the user had asked for it, which is confusing since the user never typed it.\n * Applied only to the first user message of a fresh session.\n */\nfunction frameInstructions(instructions: string, userText: string): string {\n return (\n '<session-instructions>\\n' +\n 'The block below is operating guidance from the system, not a message from the user — follow it, but do not mention it or attribute it to the user.\\n\\n' +\n `${instructions}\\n` +\n '</session-instructions>\\n\\n' +\n `<user-message>\\n${userText}\\n</user-message>`\n );\n}\n\n/*\n * Reduce a `HarnessV1Prompt` to the plain user text the bridge forwards\n * to the Claude SDK. File and image parts on the message are not yet\n * supported by the underlying runtime — throw rather than silently drop\n * them so callers learn about the gap instead of seeing mysteriously\n * truncated prompts.\n */\nfunction extractUserText(prompt: HarnessV1Prompt): string {\n if (typeof prompt === 'string') return prompt;\n const { content } = prompt;\n if (typeof content === 'string') return content;\n const parts: string[] = [];\n for (const part of content) {\n if (part.type !== 'text') {\n throw new HarnessCapabilityUnsupportedError({\n harnessId: 'claude-code',\n message: `The claude-code harness does not yet support user message parts of type '${part.type}'. Pass a string or a user message whose content contains only text parts.`,\n });\n }\n parts.push(part.text);\n }\n return parts.join('\\n\\n');\n}\n","import { execFileSync } from 'node:child_process';\nimport { readFileSync } from 'node:fs';\nimport { homedir } from 'node:os';\nimport { join } from 'node:path';\nimport { getAiGatewayAuthFromEnv } from '@ai-sdk/harness/utils';\n\nexport type ClaudeCodeAuthOptions = {\n readonly anthropic?: {\n readonly apiKey?: string;\n readonly authToken?: string;\n readonly baseUrl?: string;\n };\n readonly gateway?: {\n readonly apiKey?: string;\n readonly baseUrl?: string;\n };\n};\n\n/**\n * Resolve the environment-variable blob the bridge needs to authenticate\n * with Anthropic (directly or via the Vercel AI Gateway). Precedence:\n *\n * 1. Explicit `auth.anthropic` — pin to direct Anthropic auth.\n * 2. Explicit `auth.gateway` — pin to gateway-routed auth.\n * 3. Auto-detect from the host process env: gateway first\n * (`AI_GATEWAY_API_KEY` / `VERCEL_OIDC_TOKEN`), then direct\n * (`ANTHROPIC_API_KEY` / `ANTHROPIC_AUTH_TOKEN`).\n */\nexport type ResolveClaudeCodeEnvOptions = {\n /**\n * Returns an API key from a custom source (e.g. a password manager).\n * Used as the last-resort fallback in the auto-detect branch when no\n * static env vars or explicit auth are configured. Defaults to running\n * the `apiKeyHelper` command from `~/.claude/settings.json`, matching\n * the `claude` CLI's own behaviour.\n */\n readApiKeyHelper?: () => string | undefined;\n};\n\nexport function resolveClaudeCodeEnv(\n auth: ClaudeCodeAuthOptions | undefined,\n processEnv: Record<string, string | undefined> = process.env,\n options: ResolveClaudeCodeEnvOptions = {},\n): Record<string, string> {\n const readApiKey = options.readApiKeyHelper ?? readApiKeyHelper;\n if (auth?.anthropic) {\n return pickAnthropic({ explicit: auth.anthropic, processEnv, readApiKey });\n }\n\n const gatewayAuthFromEnv = getAiGatewayAuthFromEnv({ env: processEnv });\n if (auth?.gateway) {\n return pickGateway({\n explicit: auth.gateway,\n gatewayAuthFromEnv,\n });\n }\n if (gatewayAuthFromEnv.apiKey) {\n return pickGateway({\n explicit: {},\n gatewayAuthFromEnv,\n });\n }\n\n return pickAnthropic({ processEnv, readApiKey });\n}\n\nfunction pickAnthropic({\n explicit,\n processEnv,\n readApiKey,\n}: {\n explicit?: NonNullable<ClaudeCodeAuthOptions['anthropic']>;\n processEnv: Record<string, string | undefined>;\n readApiKey: () => string | undefined;\n}): Record<string, string> {\n const env: Record<string, string> = {};\n const helperKey = explicit ? undefined : readApiKey();\n const apiKey = explicit?.apiKey ?? processEnv.ANTHROPIC_API_KEY ?? helperKey;\n const authToken =\n explicit?.authToken ?? processEnv.ANTHROPIC_AUTH_TOKEN ?? helperKey;\n if (apiKey) env.ANTHROPIC_API_KEY = apiKey;\n if (authToken) env.ANTHROPIC_AUTH_TOKEN = authToken;\n const baseUrl = explicit?.baseUrl ?? processEnv.ANTHROPIC_BASE_URL;\n if (baseUrl) env.ANTHROPIC_BASE_URL = baseUrl;\n return env;\n}\n\n/**\n * Read the `apiKeyHelper` setting from `~/.claude/settings.json` and run\n * it. The `claude` CLI uses this hook to fetch credentials from password\n * managers and similar tools; mirroring it here lets users with that\n * setup run the harness without having to set `ANTHROPIC_API_KEY`\n * explicitly.\n */\nfunction readApiKeyHelper(): string | undefined {\n const home = homedir();\n if (!home) return undefined;\n let raw: string;\n try {\n raw = readFileSync(join(home, '.claude', 'settings.json'), 'utf8');\n } catch {\n return undefined;\n }\n let settings: { apiKeyHelper?: unknown };\n try {\n settings = JSON.parse(raw);\n } catch {\n return undefined;\n }\n const command = settings.apiKeyHelper;\n if (typeof command !== 'string' || command.length === 0) return undefined;\n try {\n const output = execFileSync('sh', ['-c', command], {\n encoding: 'utf8',\n stdio: ['ignore', 'pipe', 'ignore'],\n });\n const trimmed = output.trim();\n return trimmed.length > 0 ? trimmed : undefined;\n } catch {\n return undefined;\n }\n}\n\nfunction pickGateway({\n explicit,\n gatewayAuthFromEnv,\n}: {\n explicit: NonNullable<ClaudeCodeAuthOptions['gateway']>;\n gatewayAuthFromEnv: ReturnType<typeof getAiGatewayAuthFromEnv>;\n}): Record<string, string> {\n const apiKey = explicit.apiKey ?? gatewayAuthFromEnv.apiKey;\n const baseUrl = explicit.baseUrl ?? gatewayAuthFromEnv.baseUrl;\n const env: Record<string, string> = {};\n if (apiKey) {\n env.AI_GATEWAY_API_KEY = apiKey;\n env.ANTHROPIC_API_KEY = apiKey;\n }\n env.AI_GATEWAY_BASE_URL = baseUrl;\n env.ANTHROPIC_BASE_URL = baseUrl;\n return env;\n}\n","import {\n harnessV1BridgeInboundCommandSchemas,\n harnessV1BridgeOutboundMessageSchema,\n harnessV1BridgeReadySchema,\n harnessV1BridgeStartBaseSchema,\n} from '@ai-sdk/harness';\nimport { z } from 'zod/v4';\n\n/*\n * Claude Code's bridge wire protocol. The outbound events, transport frames,\n * shared inbound commands, and `bridge-ready` line all come from the shared\n * `@ai-sdk/harness` protocol — the only Claude-specific piece is the `start`\n * payload, which carries Claude SDK configuration.\n */\n\nexport const outboundMessageSchema = harnessV1BridgeOutboundMessageSchema;\nexport type OutboundMessage = z.infer<typeof outboundMessageSchema>;\n\nexport const startMessageSchema = harnessV1BridgeStartBaseSchema.extend({\n thinking: z.enum(['off', 'on', 'adaptive']).optional(),\n maxTurns: z.number().optional(),\n skills: z.array(z.string()).optional(),\n // Resume signal. When true, the bridge passes `{ continue: true }` to the\n // Claude SDK so the in-workdir thread state is rehydrated. The host sets this\n // on the first prompt after a cross-process resume.\n continue: z.boolean().optional(),\n});\n\nexport type StartMessage = z.infer<typeof startMessageSchema>;\n\nexport const inboundMessageSchema = z.discriminatedUnion('type', [\n startMessageSchema,\n ...harnessV1BridgeInboundCommandSchemas,\n]);\nexport type InboundMessage = z.infer<typeof inboundMessageSchema>;\n\nexport const bridgeReadySchema = harnessV1BridgeReadySchema;\nexport type BridgeReady = z.infer<typeof bridgeReadySchema>;\n","import { createClaudeCode } from './claude-code-harness';\n\n/**\n * Default `claude-code` harness instance with no overrides — suitable for the\n * common case where the underlying `claude` CLI's defaults are fine.\n * Equivalent to `createClaudeCode()`.\n */\nexport const claudeCode = createClaudeCode();\n\nexport { createClaudeCode } from './claude-code-harness';\nexport type { ClaudeCodeHarnessSettings } from './claude-code-harness';\nexport type { ClaudeCodeAuthOptions } from './claude-code-auth';\n"],"mappings":";AAAA,SAAS,mBAAmB;AAC5B,SAAS,gBAAgB;AACzB,OAAO,UAAU;AACjB,SAAS,qBAAqB;AAC9B;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OAcK;AACP;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP;AAAA,EACE;AAAA,OAGK;AACP,SAAS,iBAAiB;AAC1B,SAAS,KAAAA,UA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ACT;AAAA,IACA,eAAe,YAAY;AACzB,UAAI,SAAS;AACX,cAAM,IAAI;AAAA,UACR,uBAAuB,SAAS;AAAA,QAClC;AAAA,MACF;AACA,gBAAU;AAUV,YAAM,kBAAkB,MAAM,QAAQ,QAAQ;AAC9C,YAAM,UAAsC;AAAA,QAC1C,MAAM;AAAA,QACN,WAAW;AAAA,QACX,sBAAsB;AAAA,QACtB,MAAM;AAAA,UACJ,QAAQ;AAAA,YACN,MAAM;AAAA,YACN,OAAO;AAAA,YACP;AAAA,YACA;AAAA,UACF;AAAA,QACF;AAAA,MACF;AACA,aAAO;AAAA,IACT;AAAA,EACF;AACF;AASA,SAAS,kBAAkB,cAAsB,UAA0B;AACzE,SACE;AAAA;AAAA;AAAA,EAEG,YAAY;AAAA;AAAA;AAAA;AAAA,EAEI,QAAQ;AAAA;AAE/B;AASA,SAAS,gBAAgB,QAAiC;AACxD,MAAI,OAAO,WAAW,SAAU,QAAO;AACvC,QAAM,EAAE,QAAQ,IAAI;AACpB,MAAI,OAAO,YAAY,SAAU,QAAO;AACxC,QAAM,QAAkB,CAAC;AACzB,aAAW,QAAQ,SAAS;AAC1B,QAAI,KAAK,SAAS,QAAQ;AACxB,YAAM,IAAI,kCAAkC;AAAA,QAC1C,WAAW;AAAA,QACX,SAAS,4EAA4E,KAAK,IAAI;AAAA,MAChG,CAAC;AAAA,IACH;AACA,UAAM,KAAK,KAAK,IAAI;AAAA,EACtB;AACA,SAAO,MAAM,KAAK,MAAM;AAC1B;;;AG3iDO,IAAM,aAAa,iBAAiB;","names":["z","z","wsUrl","proc","path"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ai-sdk/harness-claude-code",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.4",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -27,10 +27,12 @@
|
|
|
27
27
|
},
|
|
28
28
|
"dependencies": {
|
|
29
29
|
"ws": "^8.21.0",
|
|
30
|
-
"
|
|
31
|
-
"@ai-sdk/harness": "1.0.3",
|
|
30
|
+
"@ai-sdk/harness": "1.0.4",
|
|
32
31
|
"@ai-sdk/provider-utils": "5.0.0"
|
|
33
32
|
},
|
|
33
|
+
"peerDependencies": {
|
|
34
|
+
"zod": "^3.25.76 || ^4.1.8"
|
|
35
|
+
},
|
|
34
36
|
"devDependencies": {
|
|
35
37
|
"@anthropic-ai/claude-agent-sdk": "0.3.177",
|
|
36
38
|
"@modelcontextprotocol/sdk": "1.29.0",
|
|
@@ -38,6 +40,7 @@
|
|
|
38
40
|
"@types/ws": "^8.5.13",
|
|
39
41
|
"tsup": "^8.5.1",
|
|
40
42
|
"typescript": "5.8.3",
|
|
43
|
+
"zod": "3.25.76",
|
|
41
44
|
"@vercel/ai-tsconfig": "0.0.0"
|
|
42
45
|
},
|
|
43
46
|
"engines": {
|
package/src/bridge/index.ts
CHANGED
|
@@ -34,7 +34,7 @@ import { argv, stdout } from 'node:process';
|
|
|
34
34
|
*/
|
|
35
35
|
import * as claudeAgentSdk from '@anthropic-ai/claude-agent-sdk';
|
|
36
36
|
import * as mcpServerModule from '@modelcontextprotocol/sdk/server/mcp.js';
|
|
37
|
-
import { z } from 'zod';
|
|
37
|
+
import { z } from 'zod/v4';
|
|
38
38
|
import { toClaudeSkillsOption } from './claude-skills-option';
|
|
39
39
|
|
|
40
40
|
/*
|
|
@@ -32,7 +32,7 @@ import {
|
|
|
32
32
|
type Experimental_SandboxProcess,
|
|
33
33
|
} from '@ai-sdk/provider-utils';
|
|
34
34
|
import { WebSocket } from 'ws';
|
|
35
|
-
import { z } from 'zod';
|
|
35
|
+
import { z } from 'zod/v4';
|
|
36
36
|
import {
|
|
37
37
|
resolveClaudeCodeEnv,
|
|
38
38
|
type ClaudeCodeAuthOptions,
|
|
@@ -231,7 +231,7 @@ const CLAUDE_CODE_BUILTIN_TOOLS = {
|
|
|
231
231
|
subject: z.string(),
|
|
232
232
|
description: z.string(),
|
|
233
233
|
activeForm: z.string().optional(),
|
|
234
|
-
metadata: z.record(z.unknown()).optional(),
|
|
234
|
+
metadata: z.record(z.string(), z.unknown()).optional(),
|
|
235
235
|
}),
|
|
236
236
|
}),
|
|
237
237
|
TaskGet: tool({
|
|
@@ -254,7 +254,7 @@ const CLAUDE_CODE_BUILTIN_TOOLS = {
|
|
|
254
254
|
addBlocks: z.array(z.string()).optional(),
|
|
255
255
|
addBlockedBy: z.array(z.string()).optional(),
|
|
256
256
|
owner: z.string().optional(),
|
|
257
|
-
metadata: z.record(z.unknown()).optional(),
|
|
257
|
+
metadata: z.record(z.string(), z.unknown()).optional(),
|
|
258
258
|
}),
|
|
259
259
|
}),
|
|
260
260
|
TaskList: tool({
|
|
@@ -333,9 +333,10 @@ const CLAUDE_CODE_BUILTIN_TOOLS = {
|
|
|
333
333
|
)
|
|
334
334
|
.min(1)
|
|
335
335
|
.max(4),
|
|
336
|
-
answers: z.record(z.string()).optional(),
|
|
336
|
+
answers: z.record(z.string(), z.string()).optional(),
|
|
337
337
|
annotations: z
|
|
338
338
|
.record(
|
|
339
|
+
z.string(),
|
|
339
340
|
z.object({
|
|
340
341
|
preview: z.string().optional(),
|
|
341
342
|
notes: z.string().optional(),
|