@ai-sdk/harness-codex 1.0.0-canary.9 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,121 @@
1
1
  # @ai-sdk/harness-codex
2
2
 
3
+ ## 1.0.0
4
+
5
+ ### Major Changes
6
+
7
+ - 3d9a50c: feat(harness): implement harness adapters for Claude Code, Codex, Pi
8
+
9
+ ### Patch Changes
10
+
11
+ - e5d4a24: chore(harness): update ws package
12
+ - bc77265: chore(harness): enforce more structural conventions in harness adapters
13
+ - aae0138: fix(harness): make listening for sandbox bridge readiness compatible with Bun
14
+ - 1ea15a3: fix(harness): fix various bugs with harness skills not being correctly processed by the harness adapters
15
+ - b8396f0: trigger initial beta release
16
+ - e551763: fix(harness): avoid using peer dependencies for underlying harness and sandbox SDKs
17
+ - 534dac6: fix(harness): fix incomplete OIDC token support for AI Gateway auth in harness adapters
18
+
19
+ ## 1.0.0-beta.24
20
+
21
+ ### Patch Changes
22
+
23
+ - @ai-sdk/harness@1.0.0-beta.27
24
+
25
+ ## 1.0.0-beta.23
26
+
27
+ ### Patch Changes
28
+
29
+ - Updated dependencies [a83a367]
30
+ - @ai-sdk/harness@1.0.0-beta.26
31
+
32
+ ## 1.0.0-beta.22
33
+
34
+ ### Patch Changes
35
+
36
+ - @ai-sdk/harness@1.0.0-beta.25
37
+
38
+ ## 1.0.0-beta.21
39
+
40
+ ### Patch Changes
41
+
42
+ - @ai-sdk/harness@1.0.0-beta.24
43
+
44
+ ## 1.0.0-beta.20
45
+
46
+ ### Patch Changes
47
+
48
+ - Updated dependencies [57e0a59]
49
+ - @ai-sdk/harness@1.0.0-beta.23
50
+
51
+ ## 1.0.0-beta.19
52
+
53
+ ### Patch Changes
54
+
55
+ - @ai-sdk/harness@1.0.0-beta.22
56
+ - @ai-sdk/provider-utils@5.0.0-beta.50
57
+
58
+ ## 1.0.0-beta.18
59
+
60
+ ### Patch Changes
61
+
62
+ - bc77265: chore(harness): enforce more structural conventions in harness adapters
63
+
64
+ ## 1.0.0-beta.17
65
+
66
+ ### Patch Changes
67
+
68
+ - @ai-sdk/harness@1.0.0-beta.21
69
+
70
+ ## 1.0.0-beta.16
71
+
72
+ ### Patch Changes
73
+
74
+ - e5d4a24: chore(harness): update ws package
75
+ - Updated dependencies [e5d4a24]
76
+ - @ai-sdk/harness@1.0.0-beta.20
77
+
78
+ ## 1.0.0-beta.15
79
+
80
+ ### Patch Changes
81
+
82
+ - @ai-sdk/harness@1.0.0-beta.19
83
+
84
+ ## 1.0.0-beta.14
85
+
86
+ ### Patch Changes
87
+
88
+ - @ai-sdk/harness@1.0.0-beta.18
89
+
90
+ ## 1.0.0-beta.13
91
+
92
+ ### Patch Changes
93
+
94
+ - 534dac6: fix(harness): fix incomplete OIDC token support for AI Gateway auth in harness adapters
95
+ - Updated dependencies [534dac6]
96
+ - @ai-sdk/harness@1.0.0-beta.17
97
+
98
+ ## 1.0.0-beta.12
99
+
100
+ ### Patch Changes
101
+
102
+ - @ai-sdk/harness@1.0.0-beta.16
103
+
104
+ ## 1.0.0-beta.11
105
+
106
+ ### Patch Changes
107
+
108
+ - @ai-sdk/harness@1.0.0-beta.15
109
+
110
+ ## 1.0.0-beta.10
111
+
112
+ ### Patch Changes
113
+
114
+ - b8396f0: trigger initial beta release
115
+ - Updated dependencies [b8396f0]
116
+ - @ai-sdk/harness@1.0.0-beta.14
117
+ - @ai-sdk/provider-utils@5.0.0-beta.49
118
+
3
119
  ## 1.0.0-canary.9
4
120
 
5
121
  ### Patch Changes
@@ -555,7 +555,14 @@ async function runTurn(start, turn) {
555
555
  }
556
556
  const codexConfig = {};
557
557
  if (Object.keys(mcpServers).length > 0) codexConfig.mcp_servers = mcpServers;
558
- const apiBaseUrl = procEnv2.AI_GATEWAY_API_KEY ? procEnv2.AI_GATEWAY_BASE_URL || "https://ai-gateway.vercel.sh/v1" : procEnv2.OPENAI_BASE_URL;
558
+ const gatewayBaseUrl = procEnv2.AI_GATEWAY_BASE_URL;
559
+ const hasGatewayAuth = Boolean(procEnv2.AI_GATEWAY_API_KEY || gatewayBaseUrl);
560
+ if (hasGatewayAuth && !gatewayBaseUrl) {
561
+ throw new Error(
562
+ "AI Gateway auth was selected but AI_GATEWAY_BASE_URL is missing from the Codex bridge environment."
563
+ );
564
+ }
565
+ const apiBaseUrl = hasGatewayAuth ? gatewayBaseUrl : procEnv2.OPENAI_BASE_URL;
559
566
  if (apiBaseUrl) {
560
567
  codexConfig.preferred_auth_method = "apikey";
561
568
  codexConfig.model_provider = "agent_bridge_openai";
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../harness/src/bridge/index.ts","../../src/bridge/index.ts","../../src/bridge/cli-relay.ts"],"sourcesContent":["// Shared in-sandbox bridge runtime. Adapter `bridge.mjs` bundles re-bundle\n// this module (tsup inlines it; `ws` stays external and resolves from the\n// sandbox-installed node_modules). It owns everything generic to the bridge\n// transport — the WebSocket server, token auth, single-flight connection\n// replacement, the in-memory event log + monotonic `seq`, resume replay, and\n// the lifecycle/meta files. The adapter supplies only `onStart` (drive its\n// CLI/SDK and translate to wire events) and `onDetach` (its resume payload).\n\nimport { appendFile, mkdir, writeFile } from 'node:fs/promises';\nimport { existsSync, readFileSync } from 'node:fs';\nimport { env as procEnv, pid, stdout } from 'node:process';\nimport { WebSocketServer, type WebSocket } from 'ws';\n\nexport type BridgeState = 'init' | 'waiting' | 'running' | 'draining' | 'done';\n\n/** Outbound turn event the adapter emits. `seq` is added by the runtime. */\nexport type BridgeEvent = Record<string, unknown> & { type: string };\n\nexport type BridgeDebugLevel = 'error' | 'warn' | 'info' | 'debug' | 'trace';\n\n/**\n * Per-session diagnostics config. The host resolves it from settings +\n * env and sends it on `start.debug`; the bridge gates console capture and\n * structured `debug-event`s on it. When disabled, nothing is captured or\n * emitted and no `seq` is consumed.\n */\nexport interface BridgeDebugConfig {\n enabled?: boolean;\n level?: BridgeDebugLevel;\n subsystems?: string[];\n}\n\nconst DEBUG_LEVEL_WEIGHT: Record<BridgeDebugLevel, number> = {\n error: 0,\n warn: 1,\n info: 2,\n debug: 3,\n trace: 4,\n};\n\n/** Exact-or-dotted-prefix subsystem match (`'bridge'` matches `'bridge.turn'`). */\nfunction subsystemMatches(\n filters: string[] | undefined,\n subsystem: string,\n): boolean {\n if (!filters || filters.length === 0) return true;\n return filters.some(\n filter => subsystem === filter || subsystem.startsWith(`${filter}.`),\n );\n}\n\nfunction formatBridgeError(err: unknown): {\n name?: string;\n message: string;\n stack?: string;\n} {\n if (err instanceof Error) {\n return { name: err.name, message: err.message, stack: err.stack };\n }\n return { message: String(err) };\n}\n\nfunction parseEnvList(value: string | undefined): string[] | undefined {\n if (!value) return undefined;\n const items = value\n .split(',')\n .map(item => item.trim())\n .filter(Boolean);\n return items.length > 0 ? items : undefined;\n}\n\nconst ENV_TRUTHY = new Set(['1', 'true', 'yes', 'on']);\n\n/**\n * Per-turn surface handed to {@link RunBridgeOptions.onStart}. The adapter\n * drives its runtime against these primitives; the runtime owns the transport.\n */\nexport interface BridgeTurn {\n /**\n * Emit a turn event to the host. Stamps a monotonic `seq`, appends to the\n * in-memory replay log, and sends to the live socket (best-effort — if the\n * host is mid-reconnect the event waits in the log and is replayed on\n * resume).\n */\n emit(event: BridgeEvent): void;\n\n /**\n * Register interest in a host-executed tool result and resolve when the\n * matching `tool-result` arrives. The adapter emits the `tool-call` event\n * itself (via {@link emit}) using the same `toolCallId`.\n */\n requestToolResult(\n toolCallId: string,\n ): Promise<{ output: unknown; isError?: boolean }>;\n\n /**\n * Register interest in a host approval decision and resolve when the matching\n * `tool-approval-response` arrives. The adapter emits the\n * `tool-approval-request` event itself using the same `approvalId`.\n */\n requestToolApproval(\n approvalId: string,\n ): Promise<{ approved: boolean; reason?: string }>;\n\n /**\n * Live queue of mid-turn user messages. The runtime pushes inbound\n * `user-message` text here; the adapter drains it as its runtime accepts\n * interactive input.\n */\n readonly pendingUserMessages: string[];\n\n /** Aborts when the host sends `abort`. */\n readonly abortSignal: AbortSignal;\n\n /** True for the first turn since this bridge process started. */\n readonly firstTurn: boolean;\n\n /**\n * Emit a structured diagnostic. Gated by the session's debug level +\n * subsystem filter; a no-op when diagnostics are disabled. Adapters use this\n * for runtime-level instrumentation; raw `console.*` output is captured and\n * forwarded automatically.\n */\n bridgeLog(input: {\n level?: BridgeDebugLevel;\n subsystem: string;\n message: string;\n attrs?: Record<string, unknown>;\n error?: unknown;\n }): void;\n}\n\nexport interface RunBridgeOptions<TStart extends { type: 'start' }> {\n /** Identifier written into `bridge-meta.json` (`'claude-code'` / `'codex'`). */\n bridgeType: string;\n /** Directory for `bridge-meta.json` / `start-config.json`. Created if absent. */\n bridgeStateDir: string;\n /** Drive one prompt turn. Rejections surface to the host as an `error` event. */\n onStart(start: TStart, turn: BridgeTurn): Promise<void>;\n /** Produce the adapter-defined resume payload for a `detach`. Defaults to `{}`. */\n onDetach?(): unknown | Promise<unknown>;\n /** WS port. Defaults to `BRIDGE_WS_PORT` env (0 = OS-assigned). */\n port?: number;\n /** Auth token. Defaults to `BRIDGE_CHANNEL_TOKEN` env. */\n token?: string;\n /** Called with the bound port once the server is listening. */\n onListening?(port: number): void;\n /**\n * Tear the process down after a `shutdown` / `detach`. Defaults to closing\n * the server and calling `process.exit(0)`. Overridable for tests.\n */\n onExit?(): void;\n}\n\ntype InboundControl =\n | {\n type: 'tool-result';\n toolCallId: string;\n output: unknown;\n isError?: boolean;\n }\n | {\n type: 'tool-approval-response';\n approvalId: string;\n approved: boolean;\n reason?: string;\n }\n | { type: 'user-message'; text: string }\n | { type: 'abort' }\n | { type: 'shutdown' }\n | { type: 'detach' }\n | { type: 'resume'; lastSeenEventId: number };\n\nconst WS_OPEN = 1;\n\n/**\n * Boot the bridge: bind the WebSocket server, announce `bridge-ready`, and\n * service host connections for the lifetime of the process. Resolves once the\n * server is listening; the process then stays alive on the server until a\n * `shutdown` / `detach` exits it.\n */\nexport interface BridgeHandle {\n /** The port the WebSocket server bound to. */\n readonly port: number;\n /** Close the WebSocket server. Does not call `process.exit`. */\n close(): Promise<void>;\n}\n\nexport async function runBridge<TStart extends { type: 'start' }>(\n options: RunBridgeOptions<TStart>,\n): Promise<BridgeHandle> {\n const { bridgeType, bridgeStateDir, onStart, onDetach } = options;\n const expectedToken = options.token ?? procEnv.BRIDGE_CHANNEL_TOKEN ?? '';\n const bridgeWsPort =\n options.port ?? parseInt(procEnv.BRIDGE_WS_PORT ?? '0', 10);\n\n const bridgeMetaPath = `${bridgeStateDir}/bridge-meta.json`;\n const startConfigPath = `${bridgeStateDir}/start-config.json`;\n const rerunStartConfigPath = `${bridgeStateDir}/rerun-start-config.json`;\n const eventLogPath = `${bridgeStateDir}/event-log.ndjson`;\n\n try {\n await mkdir(bridgeStateDir, { recursive: true });\n } catch {\n // Best-effort; the bridge still runs without its state files.\n }\n\n // ─── mutable runtime state ──────────────────────────────────────────\n let currentBoundPort = 0;\n let currentTurnState: BridgeState = 'init';\n let activeSocket: WebSocket | undefined;\n let isFirstTurn = true;\n let turnAbort: AbortController | undefined;\n let currentUserMessages: string[] | undefined;\n\n // Diagnostics. Resolved per turn from `start.debug` with a sandbox-side\n // env fallback; gates console capture + structured `debug-event`s.\n let debugConfig: BridgeDebugConfig | undefined;\n let consoleCaptureInstalled = false;\n const envDebugEnabled = ENV_TRUTHY.has(\n (procEnv.HARNESS_DEBUG ?? '').toLowerCase(),\n );\n\n // Replay log. `seq` is monotonic across the whole process — never reset —\n // because the host's `SandboxChannel` cursor (`lastSeenEventId`) lives across\n // turns. The log *contents* are cleared at the start of each turn to bound\n // memory; the just-finished turn stays replayable until the next `start`.\n let seqCounter = 0;\n let eventLog: Array<{ seq: number; line: string }> = [];\n\n /*\n * Disk mirror of the in-memory replay log. The in-memory log is lost when the\n * bridge process dies; the on-disk `event-log.ndjson` survives in the sandbox\n * filesystem so a respawned bridge (started with `BRIDGE_REPLAY_FROM_DISK=1`)\n * can reload the just-interrupted turn and serve a host's resume cursor —\n * `replay` recovery. Writes are batched on `setImmediate` (single-flight via\n * `flushPromise`) to keep `emit` off the disk hot path.\n */\n let diskBuffer = '';\n let flushPromise: Promise<void> | null = null;\n\n const flushEventsToDisk = async (): Promise<void> => {\n while (diskBuffer.length > 0) {\n const buf = diskBuffer;\n diskBuffer = '';\n await appendFile(eventLogPath, buf).catch(() => {\n // Best-effort crash-recovery mirror; the in-memory log is the source of\n // truth for the live connection.\n });\n }\n };\n\n const scheduleEventFlush = (): void => {\n if (flushPromise) return;\n flushPromise = new Promise<void>(resolve => {\n setImmediate(() => {\n void flushEventsToDisk().finally(resolve);\n });\n }).finally(() => {\n flushPromise = null;\n if (diskBuffer.length > 0) {\n scheduleEventFlush();\n }\n });\n };\n\n const flushPendingEventsToDisk = async (): Promise<void> => {\n if (diskBuffer.length > 0 && !flushPromise) {\n scheduleEventFlush();\n }\n // Await each in-flight flush, re-reading `flushPromise` after every await\n // since a fresh flush may have been scheduled for buffer that arrived while\n // we waited.\n let inFlight = flushPromise;\n while (inFlight) {\n await inFlight;\n inFlight = flushPromise;\n }\n };\n\n /*\n * When respawned for `replay`, reload the previous turn's log from disk before\n * accepting any connection so the very first `resume{lastSeenEventId}` can be\n * served the tail (including the terminal `finish`). The seq counter is\n * restored to the last persisted seq so it stays aligned with the host's\n * long-lived cursor. The file is NOT truncated in this mode — only a fresh\n * `start` (next turn) clears it.\n */\n const replayFromDisk = procEnv.BRIDGE_REPLAY_FROM_DISK === '1';\n if (replayFromDisk && existsSync(eventLogPath)) {\n try {\n const lines = readFileSync(eventLogPath, 'utf8')\n .split('\\n')\n .map(line => line.trim())\n .filter(Boolean);\n eventLog = lines.map(line => ({\n seq: (JSON.parse(line) as { seq: number }).seq,\n line,\n }));\n seqCounter = eventLog.at(-1)?.seq ?? 0;\n } catch {\n // Corrupt/partial log: fall back to an empty log; the host then degrades\n // to `rerun` instead of replaying a malformed tail.\n eventLog = [];\n seqCounter = 0;\n }\n }\n\n const pendingToolResults = new Map<\n string,\n (output: { output: unknown; isError?: boolean }) => void\n >();\n const pendingToolApprovals = new Map<\n string,\n (response: { approved: boolean; reason?: string }) => void\n >();\n\n // ─── persistence (best-effort meta + start config) ──────────────────\n const writeBridgeMeta = async (state: BridgeState): Promise<void> => {\n try {\n await writeFile(\n bridgeMetaPath,\n JSON.stringify({\n type: bridgeType,\n port: currentBoundPort,\n state,\n pid,\n }),\n );\n } catch {\n // Best-effort resilience metadata; not load-bearing for the active turn.\n }\n };\n\n const writeStartConfig = async (start: unknown): Promise<void> => {\n try {\n const serialized = JSON.stringify(start);\n await writeFile(startConfigPath, serialized);\n // Frozen copy: written once, restored over start-config.json by future\n // rerun-mode recovery to re-run the original turn from scratch.\n if (!existsSync(rerunStartConfigPath)) {\n await writeFile(rerunStartConfigPath, serialized);\n }\n } catch {\n // Best-effort.\n }\n };\n\n // ─── wire send + replay ─────────────────────────────────────────────\n const sendControl = (msg: Record<string, unknown>): void => {\n if (activeSocket?.readyState === WS_OPEN) {\n try {\n activeSocket.send(JSON.stringify(msg));\n } catch {\n // best-effort\n }\n }\n };\n\n const emit = (event: BridgeEvent): void => {\n const seq = ++seqCounter;\n const line = JSON.stringify({ ...event, seq });\n eventLog.push({ seq, line });\n diskBuffer += `${line}\\n`;\n scheduleEventFlush();\n if (activeSocket?.readyState === WS_OPEN) {\n try {\n activeSocket.send(line);\n } catch {\n // Send is best-effort: a dropped socket leaves the event in the log,\n // replayed once the host reconnects and sends `resume`.\n }\n }\n };\n\n const replay = (ws: WebSocket, afterSeq: number): void => {\n for (const entry of eventLog) {\n if (entry.seq > afterSeq && ws.readyState === WS_OPEN) {\n ws.send(entry.line);\n }\n }\n };\n\n // ─── diagnostics ──────────────────────────────────────────────\n const shouldEmitDebugEvent = (\n level: BridgeDebugLevel,\n subsystem: string,\n ): boolean => {\n if (!debugConfig?.enabled) return false;\n const threshold = debugConfig.level ?? 'debug';\n if (DEBUG_LEVEL_WEIGHT[level] > DEBUG_LEVEL_WEIGHT[threshold]) return false;\n return subsystemMatches(debugConfig.subsystems, subsystem);\n };\n\n /*\n * Forward sandbox console output. We line-buffer the original writers (kept so\n * output still reaches the real fds) and emit one `sandbox-log` per complete\n * line. `emit` never writes to stdout/stderr, so there is no recursion.\n * Installed lazily the first time a turn enables diagnostics; once installed,\n * capture is gated per-write on `debugConfig.enabled` so a later turn can\n * disable it. Console capture is independent of the subsystem/level filter.\n */\n const rawStdoutWrite = process.stdout.write.bind(process.stdout);\n const rawStderrWrite = process.stderr.write.bind(process.stderr);\n const installConsoleCapture = (): void => {\n if (consoleCaptureInstalled) return;\n consoleCaptureInstalled = true;\n const buffers: { stdout: string; stderr: string } = {\n stdout: '',\n stderr: '',\n };\n const patch =\n (stream: 'stdout' | 'stderr', raw: typeof process.stdout.write) =>\n (chunk: unknown, encoding?: unknown, cb?: unknown): boolean => {\n if (debugConfig?.enabled) {\n try {\n const enc = typeof encoding === 'string' ? encoding : 'utf8';\n const text =\n typeof chunk === 'string'\n ? chunk\n : Buffer.from(chunk as Uint8Array).toString(\n enc as BufferEncoding,\n );\n const combined = buffers[stream] + text.replace(/\\r\\n/g, '\\n');\n const parts = combined.split('\\n');\n buffers[stream] = parts.pop() ?? '';\n for (const line of parts) {\n const trimmed = line.replace(/\\s+$/, '');\n if (trimmed) {\n emit({\n type: 'sandbox-log',\n source: bridgeType,\n stream,\n line: trimmed,\n });\n }\n }\n } catch {\n // Never let capture break real output.\n }\n }\n return (raw as (c: unknown, e?: unknown, cb?: unknown) => boolean)(\n chunk,\n encoding,\n cb,\n );\n };\n process.stdout.write = patch(\n 'stdout',\n rawStdoutWrite,\n ) as typeof process.stdout.write;\n process.stderr.write = patch(\n 'stderr',\n rawStderrWrite,\n ) as typeof process.stderr.write;\n };\n\n // ─── inbound routing ────────────────────────────────────────────────\n const handleInbound = async (\n msg: TStart | InboundControl,\n ws: WebSocket,\n ): Promise<void> => {\n switch (msg.type) {\n case 'start': {\n const firstTurn = isFirstTurn;\n isFirstTurn = false;\n eventLog = []; // clear previous turn; keep seqCounter monotonic\n // Mirror the in-memory clear to disk: the log tracks only the current\n // turn. Discard any unflushed tail from the prior turn first.\n diskBuffer = '';\n void writeFile(eventLogPath, '').catch(() => {});\n turnAbort = new AbortController();\n currentTurnState = 'running';\n void writeStartConfig(msg);\n void writeBridgeMeta('running');\n const startDebug = (msg as { debug?: BridgeDebugConfig }).debug;\n debugConfig = {\n enabled: startDebug?.enabled ?? envDebugEnabled,\n level:\n startDebug?.level ??\n (procEnv.HARNESS_DEBUG_LEVEL as BridgeDebugLevel | undefined),\n subsystems:\n startDebug?.subsystems ??\n parseEnvList(procEnv.HARNESS_DEBUG_SUBSYSTEMS),\n };\n if (debugConfig.enabled) {\n installConsoleCapture();\n }\n const turn: BridgeTurn = {\n emit,\n requestToolResult: toolCallId =>\n new Promise(resolve => {\n pendingToolResults.set(toolCallId, resolve);\n }),\n requestToolApproval: approvalId =>\n new Promise(resolve => {\n pendingToolApprovals.set(approvalId, resolve);\n }),\n pendingUserMessages: [],\n abortSignal: turnAbort.signal,\n firstTurn,\n bridgeLog: input => {\n const level = input.level ?? 'debug';\n if (!shouldEmitDebugEvent(level, input.subsystem)) return;\n emit({\n type: 'debug-event',\n level,\n subsystem: input.subsystem,\n message: input.message,\n ...(input.attrs ? { attrs: input.attrs } : {}),\n ...(input.error !== undefined\n ? { error: formatBridgeError(input.error) }\n : {}),\n });\n },\n };\n currentUserMessages = turn.pendingUserMessages;\n try {\n await onStart(msg as TStart, turn);\n } catch (err) {\n emit({ type: 'error', error: serialiseError(err) });\n } finally {\n currentTurnState = 'waiting';\n void writeBridgeMeta('waiting');\n }\n return;\n }\n case 'tool-result': {\n const resolver = pendingToolResults.get(msg.toolCallId);\n if (resolver) {\n pendingToolResults.delete(msg.toolCallId);\n resolver({ output: msg.output, isError: msg.isError });\n }\n return;\n }\n case 'tool-approval-response': {\n const resolver = pendingToolApprovals.get(msg.approvalId);\n if (resolver) {\n pendingToolApprovals.delete(msg.approvalId);\n resolver({ approved: msg.approved, reason: msg.reason });\n }\n return;\n }\n case 'user-message':\n currentUserMessages?.push(msg.text);\n return;\n case 'abort':\n turnAbort?.abort();\n return;\n case 'resume':\n replay(ws, msg.lastSeenEventId);\n return;\n case 'shutdown':\n currentTurnState = 'done';\n void writeBridgeMeta('done');\n drainThenExit(ws, 1000, 'shutdown');\n return;\n case 'detach': {\n currentTurnState = 'done';\n void writeBridgeMeta('done');\n const data = (await onDetach?.()) ?? {};\n sendControl({ type: 'bridge-detach', data });\n drainThenExit(ws, 1000, 'detach');\n return;\n }\n }\n };\n\n // ─── server ─────────────────────────────────────────────────────────\n void writeBridgeMeta('init');\n\n const wss = new WebSocketServer({ port: bridgeWsPort, host: '0.0.0.0' });\n\n const exit = (): void => {\n if (options.onExit) {\n options.onExit();\n return;\n }\n wss.close(() => process.exit(0));\n setTimeout(() => process.exit(0), 1000).unref();\n };\n\n const drainThenExit = (ws: WebSocket, code: number, reason: string): void => {\n const start = Date.now();\n const tick = (): void => {\n const drained = ws.bufferedAmount === 0 || ws.readyState !== WS_OPEN;\n if (drained || Date.now() - start >= 5_000) {\n // Flush the on-disk log so a clean shutdown/detach leaves a complete\n // event-log.ndjson for any later replay recovery.\n void flushPendingEventsToDisk().finally(() => {\n try {\n ws.close(code, reason);\n } finally {\n exit();\n }\n });\n return;\n }\n setTimeout(tick, 10).unref();\n };\n tick();\n };\n\n wss.on('listening', () => {\n const addr = wss.address();\n currentBoundPort = typeof addr === 'object' && addr ? addr.port : 0;\n currentTurnState = 'waiting';\n void writeBridgeMeta('waiting');\n stdout.write(\n JSON.stringify({\n type: 'bridge-ready',\n port: currentBoundPort,\n }) + '\\n',\n );\n options.onListening?.(currentBoundPort);\n });\n\n wss.on('connection', (ws: WebSocket, req: { url?: string }) => {\n const url = new URL(req.url ?? '/', 'http://localhost');\n if (url.searchParams.get('agent_bridge_token') !== expectedToken) {\n ws.close(1008, 'unauthorized');\n return;\n }\n\n // Single-flight: a fresh authorized connection *replaces* the active one\n // (the host reconnecting after a drop). The previous socket's close is a\n // no-op below because it is no longer `activeSocket`.\n activeSocket = ws;\n\n // Announce liveness the instant we accept. Some sandbox runtimes complete\n // the host-side WS handshake before the connection is forwarded here; the\n // host waits for this frame before sending `start`/`resume`.\n sendControl({\n type: 'bridge-hello',\n state: currentTurnState,\n lastSeq: seqCounter,\n });\n\n ws.on('message', (raw: ArrayBufferLike | string) => {\n let parsed: TStart | InboundControl;\n try {\n const text =\n typeof raw === 'string' ? raw : Buffer.from(raw).toString('utf8');\n parsed = JSON.parse(text) as TStart | InboundControl;\n } catch (err) {\n sendControl({\n type: 'error',\n error: `protocol parse error: ${(err as Error).message}`,\n });\n return;\n }\n void handleInbound(parsed, ws);\n });\n\n ws.on('close', () => {\n // Only the *current* socket's close matters. A stale socket (already\n // replaced by a reconnect) closing is a no-op. Crucially we do NOT abort\n // the in-flight turn — it keeps running and its events accumulate in the\n // log for replay when the host reconnects.\n if (activeSocket === ws) {\n activeSocket = undefined;\n }\n });\n\n ws.on('error', () => {\n // 'close' follows; nothing to do beyond keeping the process alive.\n });\n });\n\n // Surface bridge-internal crashes to the host instead of dying silently.\n process.on('uncaughtException', err => {\n emit({ type: 'error', error: serialiseError(err) });\n });\n process.on('unhandledRejection', err => {\n emit({ type: 'error', error: serialiseError(err) });\n });\n\n await new Promise<void>((resolve, reject) => {\n if (wss.address() != null) {\n resolve();\n return;\n }\n\n wss.once('listening', resolve);\n wss.once('error', reject);\n });\n\n return {\n port: currentBoundPort,\n close: () =>\n new Promise<void>(resolve => {\n wss.close(() => resolve());\n }),\n };\n}\n\nfunction serialiseError(err: unknown): unknown {\n if (err instanceof Error) {\n return { name: err.name, message: err.message, stack: err.stack };\n }\n return err;\n}\n","// Long-running process that runs alongside the `codex` CLI in the sandbox.\n// The generic transport — WebSocket server, token auth, single-flight\n// reconnect, the in-memory event log + `seq`, resume replay, and the\n// lifecycle/meta files — lives in the shared `@ai-sdk/harness/bridge` runtime.\n// This file supplies only the Codex-specific turn driver.\n//\n// Host-defined tools are routed through an HTTP relay bound to\n// `127.0.0.1:0` with bearer-token auth. The codex CLI spawns\n// `host-tool-mcp.mjs` (shipped alongside this file) as a stdio MCP server;\n// the shim POSTs each tool call to the relay, which emits `tool-call` to\n// the host and waits for the matching `tool-result`.\n\nimport {\n runBridge,\n type BridgeEvent,\n type BridgeTurn,\n} from '@ai-sdk/harness/bridge';\nimport type { HarnessV1BuiltinToolName } from '@ai-sdk/harness';\nimport type { StartMessage } from '../codex-bridge-protocol';\nimport { randomUUID } from 'node:crypto';\nimport { writeFile } from 'node:fs/promises';\nimport { createServer, type Server } from 'node:http';\n// Temporary workaround for upstream codex MCP-tool bug — see ./cli-relay.ts\nimport {\n CLI_SHIM_FILENAME,\n buildCliShimScript,\n composeToolUsageInstructions,\n isToolRelayCommand,\n} from './cli-relay';\nimport { argv, env as procEnv, stdout } from 'node:process';\n\n/*\n * CONSTRAINT — the third-party imports below are NEVER bundled into the\n * compiled `bridge/index.mjs`. They are declared `external` in\n * tsup.config.ts and resolved at runtime from the node_modules that this\n * bridge installs *inside the sandbox* from `src/bridge/package.json` (and\n * its pinned `pnpm-lock.yaml`). That bridge package.json — NOT this host\n * package — is the single source of truth for these packages and their\n * versions; the published `@ai-sdk/harness-codex` package does not provide\n * them at runtime.\n *\n * When adding or changing a third-party import here you MUST keep all three\n * in sync, or the bridge will either get the dependency bundled in or fail\n * to resolve it in the sandbox:\n * 1. the import statement below,\n * 2. the `external` array in tsup.config.ts, and\n * 3. the dependency entry in `src/bridge/package.json`.\n */\nimport * as codexSdkModule from '@openai/codex-sdk';\n\n/*\n * Native Codex tool name → cross-harness common name. Tools outside this map\n * (e.g. MCP tools the model invokes by name) have no common equivalent; their\n * native name is forwarded as-is on `tool-call` events.\n */\nconst NATIVE_TO_COMMON: Readonly<Record<string, HarnessV1BuiltinToolName>> = {\n shell: 'bash',\n web_search: 'webSearch',\n};\n\nfunction toCommonName(nativeName: string): HarnessV1BuiltinToolName | string {\n return NATIVE_TO_COMMON[nativeName] ?? nativeName;\n}\n\nconst args = parseArgs(argv.slice(2));\nconst workdir = args.workdir;\nconst bridgeStateDir = args.bridgeStateDir;\nif (!workdir) {\n emitFatal('Missing --workdir argument.');\n}\nif (!bridgeStateDir) {\n emitFatal('Missing --bridge-state-dir argument.');\n}\nconst bootstrapDir = args.bootstrapDir ?? workdir;\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nconst codexSdk = codexSdkModule as any;\n\n// Codex thread id — survives across turns within this bridge process and is\n// returned to the host on `detach` so a future process can resume the thread.\nconst threadState: { id: string | undefined } = { id: undefined };\n\nawait runBridge<StartMessage>({\n bridgeType: 'codex',\n bridgeStateDir,\n onStart: runTurn,\n onDetach: () => (threadState.id ? { threadId: threadState.id } : {}),\n});\n\ntype Emit = (msg: Record<string, unknown>) => void;\n\nasync function runTurn(start: StartMessage, turn: BridgeTurn): Promise<void> {\n const emit: Emit = msg => turn.emit(msg as BridgeEvent);\n\n // Cross-process resume: the host carries the threadId we returned on detach.\n // Seed `threadState.id` so the codex SDK call below takes the `resumeThread`\n // branch.\n if (\n typeof start.resumeThreadId === 'string' &&\n start.resumeThreadId.length > 0\n ) {\n threadState.id = start.resumeThreadId;\n }\n\n /*\n * Known limitation: codex CLI does not currently surface MCP tools to the\n * model in `codex exec --experimental-json` mode (the path the\n * `@openai/codex-sdk` uses). The MCP handshake completes and `tools/list`\n * returns the host tool, but codex never registers it as a model-callable\n * function. Built-in MCP-resource accessors (`list_mcp_resources` etc.) are\n * exposed; tools are not. Tracked upstream at\n * https://github.com/openai/codex/issues/19425.\n *\n * Until that's fixed, host tools are made available to the model via a\n * separate CLI-relay workaround (see `./cli-relay.ts`). The MCP server\n * config below is kept so that the day codex starts exposing MCP tools\n * properly, host tools work both ways. Three hookpoints in this file\n * (writeFile for the shim, composeUserMessage's toolUsageBlock, and the\n * isToolRelayCommand filter in the event loop) implement the workaround\n * and can be removed once the upstream bug is fixed.\n */\n const mcpServers: Record<string, unknown> = {};\n let relay: { port: number; close(): void } | undefined;\n let cliShimPath: string | undefined;\n if (start.tools && start.tools.length > 0) {\n const relayToken = randomUUID();\n relay = await startToolRelay({\n relayToken,\n tools: start.tools,\n emit,\n requestToolResult: turn.requestToolResult,\n });\n mcpServers['harness-tools'] = {\n enabled: true,\n command: 'node',\n args: [`${bootstrapDir}/host-tool-mcp.mjs`],\n env: {\n TOOL_SCHEMAS: JSON.stringify(\n start.tools.map(t => ({\n name: t.name,\n description: t.description,\n inputSchema: t.inputSchema,\n })),\n ),\n TOOL_RELAY_URL: `http://127.0.0.1:${relay.port}`,\n TOOL_RELAY_TOKEN: relayToken,\n },\n };\n // Temporary workaround for upstream codex MCP-tool bug — see ./cli-relay.ts\n cliShimPath = `${workdir}/${CLI_SHIM_FILENAME}`;\n await writeFile(\n cliShimPath,\n buildCliShimScript({ relayPort: relay.port, relayToken }),\n 'utf8',\n );\n }\n\n const codexConfig: Record<string, unknown> = {};\n if (Object.keys(mcpServers).length > 0) codexConfig.mcp_servers = mcpServers;\n\n const apiBaseUrl = procEnv.AI_GATEWAY_API_KEY\n ? procEnv.AI_GATEWAY_BASE_URL || 'https://ai-gateway.vercel.sh/v1'\n : procEnv.OPENAI_BASE_URL;\n if (apiBaseUrl) {\n codexConfig.preferred_auth_method = 'apikey';\n codexConfig.model_provider = 'agent_bridge_openai';\n codexConfig.model_providers = {\n agent_bridge_openai: {\n name: procEnv.CODEX_MODEL_PROVIDER_NAME || 'Agent Bridge OpenAI',\n base_url: apiBaseUrl,\n env_key: 'CODEX_API_KEY',\n wire_api: 'responses',\n supports_websockets: false,\n },\n };\n }\n const usesConfiguredModelProvider =\n typeof codexConfig.model_provider === 'string';\n\n const codex = new codexSdk.Codex({\n ...(procEnv.CODEX_API_KEY ? { apiKey: procEnv.CODEX_API_KEY } : {}),\n ...(!usesConfiguredModelProvider && apiBaseUrl\n ? { baseUrl: apiBaseUrl }\n : {}),\n env: Object.fromEntries(\n Object.entries(procEnv).filter(\n (entry): entry is [string, string] => typeof entry[1] === 'string',\n ),\n ),\n ...(Object.keys(codexConfig).length > 0 ? { config: codexConfig } : {}),\n });\n\n const threadOptions = {\n ...(start.model ? { model: start.model } : {}),\n sandboxMode: 'danger-full-access',\n approvalPolicy: 'never',\n workingDirectory: workdir,\n skipGitRepoCheck: true,\n ...(start.reasoningEffort\n ? { modelReasoningEffort: start.reasoningEffort }\n : {}),\n webSearchMode: start.webSearch ? 'live' : 'disabled',\n };\n const thread = threadState.id\n ? codex.resumeThread(threadState.id, threadOptions)\n : codex.startThread(threadOptions);\n\n emit({ type: 'stream-start' });\n\n const userMessage = composeUserMessage({\n text: start.prompt,\n instructions: start.instructions,\n // Temporary workaround for upstream codex MCP-tool bug — see ./cli-relay.ts\n toolUsageBlock:\n cliShimPath && start.tools && start.tools.length > 0\n ? composeToolUsageInstructions({\n tools: start.tools,\n cliShimPath,\n })\n : undefined,\n });\n let turnUsage: Record<string, unknown> | undefined;\n const textByItem = new Map<string, string>();\n const reasoningByItem = new Map<string, string>();\n\n try {\n const { events } = await thread.runStreamed(userMessage, {\n signal: turn.abortSignal,\n });\n for await (const event of events as AsyncIterable<CodexEvent>) {\n if (turn.abortSignal.aborted) break;\n if (\n event.type === 'thread.started' &&\n typeof event.thread_id === 'string'\n ) {\n threadState.id = event.thread_id;\n // Announce to the host so it can include the id in resume state.\n emit({ type: 'bridge-thread', threadId: event.thread_id });\n }\n // Temporary workaround for upstream codex MCP-tool bug — see ./cli-relay.ts\n if (\n cliShimPath &&\n event.item?.type === 'command_execution' &&\n typeof event.item.command === 'string' &&\n isToolRelayCommand({ command: event.item.command, cliShimPath })\n ) {\n continue;\n }\n translateAndEmit(event, {\n send: emit,\n textByItem,\n reasoningByItem,\n setTurnUsage: u => (turnUsage = u),\n });\n }\n } catch (err) {\n emit({ type: 'error', error: serialiseError(err) });\n return;\n } finally {\n relay?.close();\n }\n\n emit({\n type: 'finish',\n finishReason: { unified: 'stop', raw: 'stop' },\n totalUsage: turnUsage ?? defaultUsage(),\n });\n\n void turn.pendingUserMessages; // accepted but only consumed when codex supports streamed user input\n}\n\ntype CodexItem = {\n type: string;\n id?: string;\n text?: string;\n command?: string;\n exit_code?: number;\n aggregated_output?: string;\n status?: 'in_progress' | 'completed' | 'failed';\n server?: string;\n tool?: string;\n arguments?: unknown;\n result?: { content?: unknown; structured_content?: unknown } | unknown;\n error?: { message?: string };\n query?: string;\n message?: string;\n changes?: ReadonlyArray<{\n path: string;\n kind: 'add' | 'delete' | 'update';\n }>;\n};\n\nfunction extractMcpToolCallResult(item: CodexItem): unknown {\n if (\n item.result === undefined ||\n item.result === null ||\n typeof item.result !== 'object'\n ) {\n return item.error?.message ? { error: item.error.message } : null;\n }\n const result = item.result as {\n content?: unknown;\n structured_content?: unknown;\n };\n if (\n result.structured_content !== undefined &&\n result.structured_content !== null\n ) {\n return result.structured_content;\n }\n return result.content ?? null;\n}\n\ntype CodexEvent = {\n type:\n | 'thread.started'\n | 'turn.completed'\n | 'turn.failed'\n | 'error'\n | 'item.started'\n | 'item.updated'\n | 'item.completed';\n item?: CodexItem;\n usage?: Record<string, number>;\n error?: { message: string };\n message?: string;\n thread_id?: string;\n};\n\nfunction translateAndEmit(\n event: CodexEvent,\n ctx: {\n send: Emit;\n textByItem: Map<string, string>;\n reasoningByItem: Map<string, string>;\n setTurnUsage: (u: Record<string, unknown>) => void;\n },\n): void {\n if (event.type === 'turn.completed') {\n if (event.usage) ctx.setTurnUsage(mapUsage(event.usage));\n ctx.send({\n type: 'finish-step',\n finishReason: { unified: 'stop', raw: 'stop' },\n usage: event.usage ? mapUsage(event.usage) : defaultUsage(),\n });\n return;\n }\n if (event.type === 'turn.failed') {\n ctx.send({\n type: 'error',\n error: event.error?.message ?? 'codex turn failed',\n });\n return;\n }\n if (event.type === 'error') {\n ctx.send({ type: 'error', error: event.message ?? 'codex error' });\n return;\n }\n if (!event.item) return;\n const item = event.item;\n const id = item.id ?? randomUUID();\n\n if (item.type === 'agent_message' && typeof item.text === 'string') {\n /*\n * The presence of `id` in `textByItem` — not the `item.started` event —\n * marks the text part as opened. Codex does not guarantee an\n * `item.started` event carrying text precedes the first `item.updated`\n * with text, so keying the `text-start` off the event type can emit a\n * `text-delta` for a part that was never opened. Opening lazily on the\n * first event with text keeps `text-start` before any `text-delta`.\n */\n if (!ctx.textByItem.has(id)) {\n ctx.send({ type: 'text-start', id });\n ctx.textByItem.set(id, '');\n }\n const last = ctx.textByItem.get(id) ?? '';\n const next = item.text;\n if (next.length > last.length) {\n ctx.send({ type: 'text-delta', id, delta: next.slice(last.length) });\n ctx.textByItem.set(id, next);\n }\n if (event.type === 'item.completed') ctx.send({ type: 'text-end', id });\n return;\n }\n\n if (item.type === 'reasoning' && typeof item.text === 'string') {\n if (!ctx.reasoningByItem.has(id)) {\n ctx.send({ type: 'reasoning-start', id });\n ctx.reasoningByItem.set(id, '');\n }\n const last = ctx.reasoningByItem.get(id) ?? '';\n const next = item.text;\n if (next.length > last.length) {\n ctx.send({ type: 'reasoning-delta', id, delta: next.slice(last.length) });\n ctx.reasoningByItem.set(id, next);\n }\n if (event.type === 'item.completed')\n ctx.send({ type: 'reasoning-end', id });\n return;\n }\n\n if (item.type === 'command_execution') {\n const nativeName = 'shell';\n if (event.type === 'item.started') {\n ctx.send({\n type: 'tool-call',\n toolCallId: id,\n toolName: toCommonName(nativeName),\n nativeName,\n input: JSON.stringify({ command: item.command ?? '' }),\n providerExecuted: true,\n });\n } else if (event.type === 'item.completed') {\n ctx.send({\n type: 'tool-result',\n toolCallId: id,\n toolName: toCommonName(nativeName),\n result: {\n exitCode: item.exit_code ?? null,\n output: item.aggregated_output ?? '',\n status: item.status ?? 'completed',\n },\n });\n }\n return;\n }\n\n if (item.type === 'mcp_tool_call') {\n const isHostTool = item.server === 'harness-tools';\n if (event.type === 'item.started') {\n ctx.send({\n type: 'tool-call',\n toolCallId: id,\n toolName: item.tool ?? 'unknown',\n ...(isHostTool ? {} : { nativeName: item.tool ?? 'unknown' }),\n input: JSON.stringify(item.arguments ?? {}),\n providerExecuted: !isHostTool,\n });\n } else if (event.type === 'item.completed') {\n ctx.send({\n type: 'tool-result',\n toolCallId: id,\n toolName: item.tool ?? 'unknown',\n result: extractMcpToolCallResult(item),\n });\n }\n return;\n }\n\n if (item.type === 'web_search') {\n const nativeName = 'web_search';\n if (event.type === 'item.started') {\n ctx.send({\n type: 'tool-call',\n toolCallId: id,\n toolName: toCommonName(nativeName),\n nativeName,\n input: JSON.stringify({ query: item.query ?? '' }),\n providerExecuted: true,\n });\n } else if (event.type === 'item.completed') {\n ctx.send({\n type: 'tool-result',\n toolCallId: id,\n toolName: toCommonName(nativeName),\n result: item.result ?? null,\n });\n }\n return;\n }\n\n if (item.type === 'file_change' && event.type === 'item.completed') {\n for (const change of item.changes ?? []) {\n ctx.send({\n type: 'file-change',\n event:\n change.kind === 'add'\n ? 'create'\n : change.kind === 'delete'\n ? 'delete'\n : 'modify',\n path: change.path,\n });\n }\n return;\n }\n\n if (item.type === 'error' && event.type === 'item.completed') {\n ctx.send({\n type: 'error',\n error: (item as { message?: string }).message ?? 'codex item error',\n });\n return;\n }\n}\n\nfunction mapUsage(usage: Record<string, number>): Record<string, unknown> {\n const input = usage.input_tokens ?? 0;\n const cacheRead = usage.cached_input_tokens ?? 0;\n return {\n inputTokens: {\n total: input,\n noCache: Math.max(0, input - cacheRead),\n cacheRead,\n cacheWrite: 0,\n },\n outputTokens: {\n total: usage.output_tokens ?? 0,\n text: usage.output_tokens ?? 0,\n },\n };\n}\n\nfunction defaultUsage(): Record<string, unknown> {\n return {\n inputTokens: { total: 0, noCache: 0, cacheRead: 0, cacheWrite: 0 },\n outputTokens: { total: 0, text: 0 },\n };\n}\n\nfunction composeUserMessage({\n text,\n instructions,\n toolUsageBlock,\n}: {\n text: string;\n instructions: string | undefined;\n toolUsageBlock: string | undefined;\n}): string {\n const blocks: string[] = [];\n /*\n * Frame instructions as system-provided operating guidance, not something\n * the user wrote, so the agent does not echo the prepended text back as if\n * the user had asked for it. Only present on the first user message of a\n * fresh session (the host gates it), so the matching `<user-message>` fence\n * is added only when instructions are present too.\n */\n if (instructions) {\n blocks.push(\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 if (toolUsageBlock) blocks.push(toolUsageBlock);\n blocks.push(instructions ? `<user-message>\\n${text}\\n</user-message>` : text);\n return blocks.join('\\n\\n');\n}\n\n/**\n * Tool relay — HTTP server on 127.0.0.1:0 with bearer-token auth. The MCP\n * stdio shim spawned by codex POSTs each tool invocation here; the relay\n * forwards the call to the host (via the shared runtime's `emit`), awaits the\n * matching `tool-result` (via `requestToolResult`), and responds with\n * `{ result }`.\n */\nasync function startToolRelay({\n relayToken,\n tools,\n emit,\n requestToolResult,\n}: {\n relayToken: string;\n tools: ReadonlyArray<{ name: string }>;\n emit: Emit;\n requestToolResult: (\n toolCallId: string,\n ) => Promise<{ output: unknown; isError?: boolean }>;\n}): Promise<{ port: number; close(): void }> {\n const toolNames = new Set(tools.map(t => t.name));\n\n const server = createServer(async (req, res) => {\n try {\n if (\n req.method !== 'POST' ||\n req.url !== '/' ||\n req.headers.authorization !== `Bearer ${relayToken}`\n ) {\n res.writeHead(401, { 'Content-Type': 'application/json' });\n res.end(JSON.stringify({ error: 'unauthorized tool relay request' }));\n return;\n }\n const chunks: Buffer[] = [];\n for await (const chunk of req) {\n chunks.push(chunk as Buffer);\n }\n const body = Buffer.concat(chunks).toString('utf8');\n const { requestId, toolName, input } = JSON.parse(body) as {\n requestId: string;\n toolName: string;\n input: unknown;\n };\n\n if (!toolNames.has(toolName)) {\n res.writeHead(403, { 'Content-Type': 'application/json' });\n res.end(\n JSON.stringify({ error: `Tool \"${toolName}\" is not available` }),\n );\n return;\n }\n\n emit({\n type: 'tool-call',\n toolCallId: requestId,\n toolName,\n input: JSON.stringify(input ?? {}),\n providerExecuted: false,\n });\n\n const { output, isError } = await requestToolResult(requestId);\n emit({\n type: 'tool-result',\n toolCallId: requestId,\n toolName,\n result: output ?? null,\n isError: !!isError,\n });\n\n res.writeHead(200, { 'Content-Type': 'application/json' });\n res.end(JSON.stringify({ result: output }));\n } catch (error) {\n res.writeHead(500, { 'Content-Type': 'application/json' });\n res.end(\n JSON.stringify({\n error: error instanceof Error ? error.message : String(error),\n }),\n );\n }\n });\n\n await new Promise<void>(resolve =>\n server.listen(0, '127.0.0.1', () => resolve()),\n );\n const address = server.address();\n if (!address || typeof address === 'string') {\n throw new Error('tool relay did not expose a numeric port');\n }\n return {\n port: address.port,\n close: () => closeServer(server),\n };\n}\n\nfunction closeServer(server: Server): void {\n try {\n server.close();\n } catch {}\n}\n\nfunction parseArgs(args: string[]): {\n workdir?: string;\n bridgeStateDir?: string;\n bootstrapDir?: string;\n} {\n const out: {\n workdir?: string;\n bridgeStateDir?: string;\n bootstrapDir?: string;\n } = {};\n for (let i = 0; i < args.length; i++) {\n if (args[i] === '--workdir' && i + 1 < args.length) {\n out.workdir = args[++i];\n } else if (args[i] === '--bridge-state-dir' && i + 1 < args.length) {\n out.bridgeStateDir = args[++i];\n } else if (args[i] === '--bootstrap-dir' && i + 1 < args.length) {\n out.bootstrapDir = args[++i];\n }\n }\n return out;\n}\n\nfunction serialiseError(err: unknown): unknown {\n if (err instanceof Error) {\n return { name: err.name, message: err.message, stack: err.stack };\n }\n return err;\n}\n\nfunction emitFatal(message: string): never {\n stdout.write(JSON.stringify({ type: 'bridge-fatal', message }) + '\\n');\n process.exit(1);\n}\n","/*\n * Temporary workaround for upstream codex CLI bug\n * https://github.com/openai/codex/issues/19425 — MCP tools registered via\n * `mcp_servers.*` are not exposed to the model in `codex exec --experimental-json`\n * mode (which is what `@openai/codex-sdk` uses). The MCP handshake completes\n * and `tools/list` succeeds, but codex never registers the tools as\n * model-callable functions.\n *\n * Until that's fixed upstream, this file implements a CLI-based relay:\n *\n * 1. A small Node script is written into the workdir at turn start\n * (`buildCliShimScript`). It accepts `<toolName> <jsonInput>` as argv,\n * POSTs to the same HTTP relay the MCP shim uses, and prints the\n * result to stdout.\n *\n * 2. Tool descriptions and invocation instructions are injected into the\n * user prompt (`composeToolUsageInstructions`), telling the model to\n * call host tools by running `node <shim-path> <toolName> '<jsonInput>'`\n * via its built-in `bash` tool.\n *\n * 3. The bridge's event loop suppresses the matching `command_execution`\n * events (`isToolRelayCommand`) so callers receive clean `tool-call` /\n * `tool-result` events from the HTTP relay rather than seeing the\n * relay invocations as raw bash commands.\n *\n * Once #19425 is fixed and MCP tools are properly exposed in exec mode, the\n * three hookpoints in `bridge/index.ts` can be removed along with this file.\n */\n\nexport const CLI_SHIM_FILENAME = 'harness-tool.mjs';\n\nexport function buildCliShimScript({\n relayPort,\n relayToken,\n}: {\n relayPort: number;\n relayToken: string;\n}): string {\n return `#!/usr/bin/env node\nconst [toolName, inputJson = '{}'] = process.argv.slice(2);\nif (!toolName) {\n console.error('Usage: harness-tool <tool_name> <json_input>');\n process.exit(64);\n}\nlet input;\ntry {\n input = JSON.parse(inputJson);\n} catch (error) {\n console.error('Invalid JSON input: ' + (error instanceof Error ? error.message : String(error)));\n process.exit(64);\n}\nconst requestId = 'cli-' + Date.now() + '-' + Math.random().toString(16).slice(2);\nconst response = await fetch('http://127.0.0.1:${relayPort}', {\n method: 'POST',\n headers: { 'Content-Type': 'application/json', Authorization: 'Bearer ${relayToken}' },\n body: JSON.stringify({ requestId, toolName, input }),\n});\nconst text = await response.text();\nlet payload;\ntry {\n payload = text ? JSON.parse(text) : {};\n} catch {\n payload = { error: text };\n}\nif (!response.ok || payload.error) {\n console.error(String(payload.error ?? ('tool relay failed with HTTP ' + response.status)));\n process.exit(1);\n}\nconsole.log(JSON.stringify(payload.result ?? payload, null, 2));\n`;\n}\n\nexport function composeToolUsageInstructions({\n tools,\n cliShimPath,\n}: {\n tools: ReadonlyArray<{\n name: string;\n description?: string;\n inputSchema?: unknown;\n }>;\n cliShimPath: string;\n}): string {\n const lines: string[] = [\n '## Host tools',\n '',\n 'You have access to the following host-provided tools. To use one, run the following command via your built-in `bash` tool:',\n '',\n ` node ${cliShimPath} <toolName> '<jsonInput>'`,\n '',\n 'The script prints the JSON result to stdout. Do not invent another way to call these tools — only this CLI invocation will work. Pass the JSON input as a single-quoted argument.',\n 'For every user request that depends on a host-provided tool, run a separate CLI invocation for each needed tool call in the current turn before answering. Do not reuse previous tool results, and do not say you used a host tool unless the command has completed in the current turn.',\n '',\n ];\n for (const tool of tools) {\n lines.push(`### ${tool.name}`);\n if (tool.description) lines.push(tool.description);\n lines.push(\n `Input schema: \\`${JSON.stringify(tool.inputSchema ?? {})}\\``,\n '',\n );\n }\n return lines.join('\\n');\n}\n\nexport function isToolRelayCommand({\n command,\n cliShimPath,\n}: {\n command: string;\n cliShimPath: string;\n}): boolean {\n return command.includes(cliShimPath);\n}\n"],"mappings":";AAQA,SAAS,YAAY,OAAO,iBAAiB;AAC7C,SAAS,YAAY,oBAAoB;AACzC,SAAS,OAAO,SAAS,KAAK,cAAc;AAC5C,SAAS,uBAAuC;AAqBhD,IAAM,qBAAuD;EAC3D,OAAO;EACP,MAAM;EACN,MAAM;EACN,OAAO;EACP,OAAO;AACT;AAGA,SAAS,iBACP,SACA,WACS;AACT,MAAI,CAAC,WAAW,QAAQ,WAAW,EAAG,QAAO;AAC7C,SAAO,QAAQ;IACb,CAAA,WAAU,cAAc,UAAU,UAAU,WAAW,GAAG,MAAM,GAAG;EACrE;AACF;AAEA,SAAS,kBAAkB,KAIzB;AACA,MAAI,eAAe,OAAO;AACxB,WAAO,EAAE,MAAM,IAAI,MAAM,SAAS,IAAI,SAAS,OAAO,IAAI,MAAM;EAClE;AACA,SAAO,EAAE,SAAS,OAAO,GAAG,EAAE;AAChC;AAEA,SAAS,aAAa,OAAiD;AACrE,MAAI,CAAC,MAAO,QAAO;AACnB,QAAM,QAAQ,MACX,MAAM,GAAG,EACT,IAAI,CAAA,SAAQ,KAAK,KAAK,CAAC,EACvB,OAAO,OAAO;AACjB,SAAO,MAAM,SAAS,IAAI,Q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AO,GAAG,aAAa,MAAM,QAAQ,CAAC;AAAA,EAC/C;AACA,QAAM,UAAU,OAAO,QAAQ;AAC/B,MAAI,CAAC,WAAW,OAAO,YAAY,UAAU;AAC3C,UAAM,IAAI,MAAM,0CAA0C;AAAA,EAC5D;AACA,SAAO;AAAA,IACL,MAAM,QAAQ;AAAA,IACd,OAAO,MAAM,YAAY,MAAM;AAAA,EACjC;AACF;AAEA,SAAS,YAAY,QAAsB;AACzC,MAAI;AACF,WAAO,MAAM;AAAA,EACf,QAAQ;AAAA,EAAC;AACX;AAEA,SAAS,UAAUC,OAIjB;AACA,QAAM,MAIF,CAAC;AACL,WAAS,IAAI,GAAG,IAAIA,MAAK,QAAQ,KAAK;AACpC,QAAIA,MAAK,CAAC,MAAM,eAAe,IAAI,IAAIA,MAAK,QAAQ;AAClD,UAAI,UAAUA,MAAK,EAAE,CAAC;AAAA,IACxB,WAAWA,MAAK,CAAC,MAAM,wBAAwB,IAAI,IAAIA,MAAK,QAAQ;AAClE,UAAI,iBAAiBA,MAAK,EAAE,CAAC;AAAA,IAC/B,WAAWA,MAAK,CAAC,MAAM,qBAAqB,IAAI,IAAIA,MAAK,QAAQ;AAC/D,UAAI,eAAeA,MAAK,EAAE,CAAC;AAAA,IAC7B;AAAA,EACF;AACA,SAAO;AACT;AAEA,SAASD,gBAAe,KAAuB;AAC7C,MAAI,eAAe,OAAO;AACxB,WAAO,EAAE,MAAM,IAAI,MAAM,SAAS,IAAI,SAAS,OAAO,IAAI,MAAM;AAAA,EAClE;AACA,SAAO;AACT;AAEA,SAAS,UAAU,SAAwB;AACzC,EAAAF,QAAO,MAAM,KAAK,UAAU,EAAE,MAAM,gBAAgB,QAAQ,CAAC,IAAI,IAAI;AACrE,UAAQ,KAAK,CAAC;AAChB;","names":["bridgeStateDir","writeFile","procEnv","stdout","writeFile","serialiseError","args"]}
1
+ {"version":3,"sources":["../../../harness/src/bridge/index.ts","../../src/bridge/index.ts","../../src/bridge/cli-relay.ts"],"sourcesContent":["// Shared in-sandbox bridge runtime. Adapter `bridge.mjs` bundles re-bundle\n// this module (tsup inlines it; `ws` stays external and resolves from the\n// sandbox-installed node_modules). It owns everything generic to the bridge\n// transport — the WebSocket server, token auth, single-flight connection\n// replacement, the in-memory event log + monotonic `seq`, resume replay, and\n// the lifecycle/meta files. The adapter supplies only `onStart` (drive its\n// CLI/SDK and translate to wire events) and `onDetach` (its resume payload).\n\nimport { appendFile, mkdir, writeFile } from 'node:fs/promises';\nimport { existsSync, readFileSync } from 'node:fs';\nimport { env as procEnv, pid, stdout } from 'node:process';\nimport { WebSocketServer, type WebSocket } from 'ws';\n\nexport type BridgeState = 'init' | 'waiting' | 'running' | 'draining' | 'done';\n\n/** Outbound turn event the adapter emits. `seq` is added by the runtime. */\nexport type BridgeEvent = Record<string, unknown> & { type: string };\n\nexport type BridgeDebugLevel = 'error' | 'warn' | 'info' | 'debug' | 'trace';\n\n/**\n * Per-session diagnostics config. The host resolves it from settings +\n * env and sends it on `start.debug`; the bridge gates console capture and\n * structured `debug-event`s on it. When disabled, nothing is captured or\n * emitted and no `seq` is consumed.\n */\nexport interface BridgeDebugConfig {\n enabled?: boolean;\n level?: BridgeDebugLevel;\n subsystems?: string[];\n}\n\nconst DEBUG_LEVEL_WEIGHT: Record<BridgeDebugLevel, number> = {\n error: 0,\n warn: 1,\n info: 2,\n debug: 3,\n trace: 4,\n};\n\n/** Exact-or-dotted-prefix subsystem match (`'bridge'` matches `'bridge.turn'`). */\nfunction subsystemMatches(\n filters: string[] | undefined,\n subsystem: string,\n): boolean {\n if (!filters || filters.length === 0) return true;\n return filters.some(\n filter => subsystem === filter || subsystem.startsWith(`${filter}.`),\n );\n}\n\nfunction formatBridgeError(err: unknown): {\n name?: string;\n message: string;\n stack?: string;\n} {\n if (err instanceof Error) {\n return { name: err.name, message: err.message, stack: err.stack };\n }\n return { message: String(err) };\n}\n\nfunction parseEnvList(value: string | undefined): string[] | undefined {\n if (!value) return undefined;\n const items = value\n .split(',')\n .map(item => item.trim())\n .filter(Boolean);\n return items.length > 0 ? items : undefined;\n}\n\nconst ENV_TRUTHY = new Set(['1', 'true', 'yes', 'on']);\n\n/**\n * Per-turn surface handed to {@link RunBridgeOptions.onStart}. The adapter\n * drives its runtime against these primitives; the runtime owns the transport.\n */\nexport interface BridgeTurn {\n /**\n * Emit a turn event to the host. Stamps a monotonic `seq`, appends to the\n * in-memory replay log, and sends to the live socket (best-effort — if the\n * host is mid-reconnect the event waits in the log and is replayed on\n * resume).\n */\n emit(event: BridgeEvent): void;\n\n /**\n * Register interest in a host-executed tool result and resolve when the\n * matching `tool-result` arrives. The adapter emits the `tool-call` event\n * itself (via {@link emit}) using the same `toolCallId`.\n */\n requestToolResult(\n toolCallId: string,\n ): Promise<{ output: unknown; isError?: boolean }>;\n\n /**\n * Register interest in a host approval decision and resolve when the matching\n * `tool-approval-response` arrives. The adapter emits the\n * `tool-approval-request` event itself using the same `approvalId`.\n */\n requestToolApproval(\n approvalId: string,\n ): Promise<{ approved: boolean; reason?: string }>;\n\n /**\n * Live queue of mid-turn user messages. The runtime pushes inbound\n * `user-message` text here; the adapter drains it as its runtime accepts\n * interactive input.\n */\n readonly pendingUserMessages: string[];\n\n /** Aborts when the host sends `abort`. */\n readonly abortSignal: AbortSignal;\n\n /** True for the first turn since this bridge process started. */\n readonly firstTurn: boolean;\n\n /**\n * Emit a structured diagnostic. Gated by the session's debug level +\n * subsystem filter; a no-op when diagnostics are disabled. Adapters use this\n * for runtime-level instrumentation; raw `console.*` output is captured and\n * forwarded automatically.\n */\n bridgeLog(input: {\n level?: BridgeDebugLevel;\n subsystem: string;\n message: string;\n attrs?: Record<string, unknown>;\n error?: unknown;\n }): void;\n}\n\nexport interface RunBridgeOptions<TStart extends { type: 'start' }> {\n /** Identifier written into `bridge-meta.json` (`'claude-code'` / `'codex'`). */\n bridgeType: string;\n /** Directory for `bridge-meta.json` / `start-config.json`. Created if absent. */\n bridgeStateDir: string;\n /** Drive one prompt turn. Rejections surface to the host as an `error` event. */\n onStart(start: TStart, turn: BridgeTurn): Promise<void>;\n /** Produce the adapter-defined resume payload for a `detach`. Defaults to `{}`. */\n onDetach?(): unknown | Promise<unknown>;\n /** WS port. Defaults to `BRIDGE_WS_PORT` env (0 = OS-assigned). */\n port?: number;\n /** Auth token. Defaults to `BRIDGE_CHANNEL_TOKEN` env. */\n token?: string;\n /** Called with the bound port once the server is listening. */\n onListening?(port: number): void;\n /**\n * Tear the process down after a `shutdown` / `detach`. Defaults to closing\n * the server and calling `process.exit(0)`. Overridable for tests.\n */\n onExit?(): void;\n}\n\ntype InboundControl =\n | {\n type: 'tool-result';\n toolCallId: string;\n output: unknown;\n isError?: boolean;\n }\n | {\n type: 'tool-approval-response';\n approvalId: string;\n approved: boolean;\n reason?: string;\n }\n | { type: 'user-message'; text: string }\n | { type: 'abort' }\n | { type: 'shutdown' }\n | { type: 'detach' }\n | { type: 'resume'; lastSeenEventId: number };\n\nconst WS_OPEN = 1;\n\n/**\n * Boot the bridge: bind the WebSocket server, announce `bridge-ready`, and\n * service host connections for the lifetime of the process. Resolves once the\n * server is listening; the process then stays alive on the server until a\n * `shutdown` / `detach` exits it.\n */\nexport interface BridgeHandle {\n /** The port the WebSocket server bound to. */\n readonly port: number;\n /** Close the WebSocket server. Does not call `process.exit`. */\n close(): Promise<void>;\n}\n\nexport async function runBridge<TStart extends { type: 'start' }>(\n options: RunBridgeOptions<TStart>,\n): Promise<BridgeHandle> {\n const { bridgeType, bridgeStateDir, onStart, onDetach } = options;\n const expectedToken = options.token ?? procEnv.BRIDGE_CHANNEL_TOKEN ?? '';\n const bridgeWsPort =\n options.port ?? parseInt(procEnv.BRIDGE_WS_PORT ?? '0', 10);\n\n const bridgeMetaPath = `${bridgeStateDir}/bridge-meta.json`;\n const startConfigPath = `${bridgeStateDir}/start-config.json`;\n const rerunStartConfigPath = `${bridgeStateDir}/rerun-start-config.json`;\n const eventLogPath = `${bridgeStateDir}/event-log.ndjson`;\n\n try {\n await mkdir(bridgeStateDir, { recursive: true });\n } catch {\n // Best-effort; the bridge still runs without its state files.\n }\n\n // ─── mutable runtime state ──────────────────────────────────────────\n let currentBoundPort = 0;\n let currentTurnState: BridgeState = 'init';\n let activeSocket: WebSocket | undefined;\n let isFirstTurn = true;\n let turnAbort: AbortController | undefined;\n let currentUserMessages: string[] | undefined;\n\n // Diagnostics. Resolved per turn from `start.debug` with a sandbox-side\n // env fallback; gates console capture + structured `debug-event`s.\n let debugConfig: BridgeDebugConfig | undefined;\n let consoleCaptureInstalled = false;\n const envDebugEnabled = ENV_TRUTHY.has(\n (procEnv.HARNESS_DEBUG ?? '').toLowerCase(),\n );\n\n // Replay log. `seq` is monotonic across the whole process — never reset —\n // because the host's `SandboxChannel` cursor (`lastSeenEventId`) lives across\n // turns. The log *contents* are cleared at the start of each turn to bound\n // memory; the just-finished turn stays replayable until the next `start`.\n let seqCounter = 0;\n let eventLog: Array<{ seq: number; line: string }> = [];\n\n /*\n * Disk mirror of the in-memory replay log. The in-memory log is lost when the\n * bridge process dies; the on-disk `event-log.ndjson` survives in the sandbox\n * filesystem so a respawned bridge (started with `BRIDGE_REPLAY_FROM_DISK=1`)\n * can reload the just-interrupted turn and serve a host's resume cursor —\n * `replay` recovery. Writes are batched on `setImmediate` (single-flight via\n * `flushPromise`) to keep `emit` off the disk hot path.\n */\n let diskBuffer = '';\n let flushPromise: Promise<void> | null = null;\n\n const flushEventsToDisk = async (): Promise<void> => {\n while (diskBuffer.length > 0) {\n const buf = diskBuffer;\n diskBuffer = '';\n await appendFile(eventLogPath, buf).catch(() => {\n // Best-effort crash-recovery mirror; the in-memory log is the source of\n // truth for the live connection.\n });\n }\n };\n\n const scheduleEventFlush = (): void => {\n if (flushPromise) return;\n flushPromise = new Promise<void>(resolve => {\n setImmediate(() => {\n void flushEventsToDisk().finally(resolve);\n });\n }).finally(() => {\n flushPromise = null;\n if (diskBuffer.length > 0) {\n scheduleEventFlush();\n }\n });\n };\n\n const flushPendingEventsToDisk = async (): Promise<void> => {\n if (diskBuffer.length > 0 && !flushPromise) {\n scheduleEventFlush();\n }\n // Await each in-flight flush, re-reading `flushPromise` after every await\n // since a fresh flush may have been scheduled for buffer that arrived while\n // we waited.\n let inFlight = flushPromise;\n while (inFlight) {\n await inFlight;\n inFlight = flushPromise;\n }\n };\n\n /*\n * When respawned for `replay`, reload the previous turn's log from disk before\n * accepting any connection so the very first `resume{lastSeenEventId}` can be\n * served the tail (including the terminal `finish`). The seq counter is\n * restored to the last persisted seq so it stays aligned with the host's\n * long-lived cursor. The file is NOT truncated in this mode — only a fresh\n * `start` (next turn) clears it.\n */\n const replayFromDisk = procEnv.BRIDGE_REPLAY_FROM_DISK === '1';\n if (replayFromDisk && existsSync(eventLogPath)) {\n try {\n const lines = readFileSync(eventLogPath, 'utf8')\n .split('\\n')\n .map(line => line.trim())\n .filter(Boolean);\n eventLog = lines.map(line => ({\n seq: (JSON.parse(line) as { seq: number }).seq,\n line,\n }));\n seqCounter = eventLog.at(-1)?.seq ?? 0;\n } catch {\n // Corrupt/partial log: fall back to an empty log; the host then degrades\n // to `rerun` instead of replaying a malformed tail.\n eventLog = [];\n seqCounter = 0;\n }\n }\n\n const pendingToolResults = new Map<\n string,\n (output: { output: unknown; isError?: boolean }) => void\n >();\n const pendingToolApprovals = new Map<\n string,\n (response: { approved: boolean; reason?: string }) => void\n >();\n\n // ─── persistence (best-effort meta + start config) ──────────────────\n const writeBridgeMeta = async (state: BridgeState): Promise<void> => {\n try {\n await writeFile(\n bridgeMetaPath,\n JSON.stringify({\n type: bridgeType,\n port: currentBoundPort,\n state,\n pid,\n }),\n );\n } catch {\n // Best-effort resilience metadata; not load-bearing for the active turn.\n }\n };\n\n const writeStartConfig = async (start: unknown): Promise<void> => {\n try {\n const serialized = JSON.stringify(start);\n await writeFile(startConfigPath, serialized);\n // Frozen copy: written once, restored over start-config.json by future\n // rerun-mode recovery to re-run the original turn from scratch.\n if (!existsSync(rerunStartConfigPath)) {\n await writeFile(rerunStartConfigPath, serialized);\n }\n } catch {\n // Best-effort.\n }\n };\n\n // ─── wire send + replay ─────────────────────────────────────────────\n const sendControl = (msg: Record<string, unknown>): void => {\n if (activeSocket?.readyState === WS_OPEN) {\n try {\n activeSocket.send(JSON.stringify(msg));\n } catch {\n // best-effort\n }\n }\n };\n\n const emit = (event: BridgeEvent): void => {\n const seq = ++seqCounter;\n const line = JSON.stringify({ ...event, seq });\n eventLog.push({ seq, line });\n diskBuffer += `${line}\\n`;\n scheduleEventFlush();\n if (activeSocket?.readyState === WS_OPEN) {\n try {\n activeSocket.send(line);\n } catch {\n // Send is best-effort: a dropped socket leaves the event in the log,\n // replayed once the host reconnects and sends `resume`.\n }\n }\n };\n\n const replay = (ws: WebSocket, afterSeq: number): void => {\n for (const entry of eventLog) {\n if (entry.seq > afterSeq && ws.readyState === WS_OPEN) {\n ws.send(entry.line);\n }\n }\n };\n\n // ─── diagnostics ──────────────────────────────────────────────\n const shouldEmitDebugEvent = (\n level: BridgeDebugLevel,\n subsystem: string,\n ): boolean => {\n if (!debugConfig?.enabled) return false;\n const threshold = debugConfig.level ?? 'debug';\n if (DEBUG_LEVEL_WEIGHT[level] > DEBUG_LEVEL_WEIGHT[threshold]) return false;\n return subsystemMatches(debugConfig.subsystems, subsystem);\n };\n\n /*\n * Forward sandbox console output. We line-buffer the original writers (kept so\n * output still reaches the real fds) and emit one `sandbox-log` per complete\n * line. `emit` never writes to stdout/stderr, so there is no recursion.\n * Installed lazily the first time a turn enables diagnostics; once installed,\n * capture is gated per-write on `debugConfig.enabled` so a later turn can\n * disable it. Console capture is independent of the subsystem/level filter.\n */\n const rawStdoutWrite = process.stdout.write.bind(process.stdout);\n const rawStderrWrite = process.stderr.write.bind(process.stderr);\n const installConsoleCapture = (): void => {\n if (consoleCaptureInstalled) return;\n consoleCaptureInstalled = true;\n const buffers: { stdout: string; stderr: string } = {\n stdout: '',\n stderr: '',\n };\n const patch =\n (stream: 'stdout' | 'stderr', raw: typeof process.stdout.write) =>\n (chunk: unknown, encoding?: unknown, cb?: unknown): boolean => {\n if (debugConfig?.enabled) {\n try {\n const enc = typeof encoding === 'string' ? encoding : 'utf8';\n const text =\n typeof chunk === 'string'\n ? chunk\n : Buffer.from(chunk as Uint8Array).toString(\n enc as BufferEncoding,\n );\n const combined = buffers[stream] + text.replace(/\\r\\n/g, '\\n');\n const parts = combined.split('\\n');\n buffers[stream] = parts.pop() ?? '';\n for (const line of parts) {\n const trimmed = line.replace(/\\s+$/, '');\n if (trimmed) {\n emit({\n type: 'sandbox-log',\n source: bridgeType,\n stream,\n line: trimmed,\n });\n }\n }\n } catch {\n // Never let capture break real output.\n }\n }\n return (raw as (c: unknown, e?: unknown, cb?: unknown) => boolean)(\n chunk,\n encoding,\n cb,\n );\n };\n process.stdout.write = patch(\n 'stdout',\n rawStdoutWrite,\n ) as typeof process.stdout.write;\n process.stderr.write = patch(\n 'stderr',\n rawStderrWrite,\n ) as typeof process.stderr.write;\n };\n\n // ─── inbound routing ────────────────────────────────────────────────\n const handleInbound = async (\n msg: TStart | InboundControl,\n ws: WebSocket,\n ): Promise<void> => {\n switch (msg.type) {\n case 'start': {\n const firstTurn = isFirstTurn;\n isFirstTurn = false;\n eventLog = []; // clear previous turn; keep seqCounter monotonic\n // Mirror the in-memory clear to disk: the log tracks only the current\n // turn. Discard any unflushed tail from the prior turn first.\n diskBuffer = '';\n void writeFile(eventLogPath, '').catch(() => {});\n turnAbort = new AbortController();\n currentTurnState = 'running';\n void writeStartConfig(msg);\n void writeBridgeMeta('running');\n const startDebug = (msg as { debug?: BridgeDebugConfig }).debug;\n debugConfig = {\n enabled: startDebug?.enabled ?? envDebugEnabled,\n level:\n startDebug?.level ??\n (procEnv.HARNESS_DEBUG_LEVEL as BridgeDebugLevel | undefined),\n subsystems:\n startDebug?.subsystems ??\n parseEnvList(procEnv.HARNESS_DEBUG_SUBSYSTEMS),\n };\n if (debugConfig.enabled) {\n installConsoleCapture();\n }\n const turn: BridgeTurn = {\n emit,\n requestToolResult: toolCallId =>\n new Promise(resolve => {\n pendingToolResults.set(toolCallId, resolve);\n }),\n requestToolApproval: approvalId =>\n new Promise(resolve => {\n pendingToolApprovals.set(approvalId, resolve);\n }),\n pendingUserMessages: [],\n abortSignal: turnAbort.signal,\n firstTurn,\n bridgeLog: input => {\n const level = input.level ?? 'debug';\n if (!shouldEmitDebugEvent(level, input.subsystem)) return;\n emit({\n type: 'debug-event',\n level,\n subsystem: input.subsystem,\n message: input.message,\n ...(input.attrs ? { attrs: input.attrs } : {}),\n ...(input.error !== undefined\n ? { error: formatBridgeError(input.error) }\n : {}),\n });\n },\n };\n currentUserMessages = turn.pendingUserMessages;\n try {\n await onStart(msg as TStart, turn);\n } catch (err) {\n emit({ type: 'error', error: serialiseError(err) });\n } finally {\n currentTurnState = 'waiting';\n void writeBridgeMeta('waiting');\n }\n return;\n }\n case 'tool-result': {\n const resolver = pendingToolResults.get(msg.toolCallId);\n if (resolver) {\n pendingToolResults.delete(msg.toolCallId);\n resolver({ output: msg.output, isError: msg.isError });\n }\n return;\n }\n case 'tool-approval-response': {\n const resolver = pendingToolApprovals.get(msg.approvalId);\n if (resolver) {\n pendingToolApprovals.delete(msg.approvalId);\n resolver({ approved: msg.approved, reason: msg.reason });\n }\n return;\n }\n case 'user-message':\n currentUserMessages?.push(msg.text);\n return;\n case 'abort':\n turnAbort?.abort();\n return;\n case 'resume':\n replay(ws, msg.lastSeenEventId);\n return;\n case 'shutdown':\n currentTurnState = 'done';\n void writeBridgeMeta('done');\n drainThenExit(ws, 1000, 'shutdown');\n return;\n case 'detach': {\n currentTurnState = 'done';\n void writeBridgeMeta('done');\n const data = (await onDetach?.()) ?? {};\n sendControl({ type: 'bridge-detach', data });\n drainThenExit(ws, 1000, 'detach');\n return;\n }\n }\n };\n\n // ─── server ─────────────────────────────────────────────────────────\n void writeBridgeMeta('init');\n\n const wss = new WebSocketServer({ port: bridgeWsPort, host: '0.0.0.0' });\n\n const exit = (): void => {\n if (options.onExit) {\n options.onExit();\n return;\n }\n wss.close(() => process.exit(0));\n setTimeout(() => process.exit(0), 1000).unref();\n };\n\n const drainThenExit = (ws: WebSocket, code: number, reason: string): void => {\n const start = Date.now();\n const tick = (): void => {\n const drained = ws.bufferedAmount === 0 || ws.readyState !== WS_OPEN;\n if (drained || Date.now() - start >= 5_000) {\n // Flush the on-disk log so a clean shutdown/detach leaves a complete\n // event-log.ndjson for any later replay recovery.\n void flushPendingEventsToDisk().finally(() => {\n try {\n ws.close(code, reason);\n } finally {\n exit();\n }\n });\n return;\n }\n setTimeout(tick, 10).unref();\n };\n tick();\n };\n\n wss.on('listening', () => {\n const addr = wss.address();\n currentBoundPort = typeof addr === 'object' && addr ? addr.port : 0;\n currentTurnState = 'waiting';\n void writeBridgeMeta('waiting');\n stdout.write(\n JSON.stringify({\n type: 'bridge-ready',\n port: currentBoundPort,\n }) + '\\n',\n );\n options.onListening?.(currentBoundPort);\n });\n\n wss.on('connection', (ws: WebSocket, req: { url?: string }) => {\n const url = new URL(req.url ?? '/', 'http://localhost');\n if (url.searchParams.get('agent_bridge_token') !== expectedToken) {\n ws.close(1008, 'unauthorized');\n return;\n }\n\n // Single-flight: a fresh authorized connection *replaces* the active one\n // (the host reconnecting after a drop). The previous socket's close is a\n // no-op below because it is no longer `activeSocket`.\n activeSocket = ws;\n\n // Announce liveness the instant we accept. Some sandbox runtimes complete\n // the host-side WS handshake before the connection is forwarded here; the\n // host waits for this frame before sending `start`/`resume`.\n sendControl({\n type: 'bridge-hello',\n state: currentTurnState,\n lastSeq: seqCounter,\n });\n\n ws.on('message', (raw: ArrayBufferLike | string) => {\n let parsed: TStart | InboundControl;\n try {\n const text =\n typeof raw === 'string' ? raw : Buffer.from(raw).toString('utf8');\n parsed = JSON.parse(text) as TStart | InboundControl;\n } catch (err) {\n sendControl({\n type: 'error',\n error: `protocol parse error: ${(err as Error).message}`,\n });\n return;\n }\n void handleInbound(parsed, ws);\n });\n\n ws.on('close', () => {\n // Only the *current* socket's close matters. A stale socket (already\n // replaced by a reconnect) closing is a no-op. Crucially we do NOT abort\n // the in-flight turn — it keeps running and its events accumulate in the\n // log for replay when the host reconnects.\n if (activeSocket === ws) {\n activeSocket = undefined;\n }\n });\n\n ws.on('error', () => {\n // 'close' follows; nothing to do beyond keeping the process alive.\n });\n });\n\n // Surface bridge-internal crashes to the host instead of dying silently.\n process.on('uncaughtException', err => {\n emit({ type: 'error', error: serialiseError(err) });\n });\n process.on('unhandledRejection', err => {\n emit({ type: 'error', error: serialiseError(err) });\n });\n\n await new Promise<void>((resolve, reject) => {\n if (wss.address() != null) {\n resolve();\n return;\n }\n\n wss.once('listening', resolve);\n wss.once('error', reject);\n });\n\n return {\n port: currentBoundPort,\n close: () =>\n new Promise<void>(resolve => {\n wss.close(() => resolve());\n }),\n };\n}\n\nfunction serialiseError(err: unknown): unknown {\n if (err instanceof Error) {\n return { name: err.name, message: err.message, stack: err.stack };\n }\n return err;\n}\n","// Long-running process that runs alongside the `codex` CLI in the sandbox.\n// The generic transport — WebSocket server, token auth, single-flight\n// reconnect, the in-memory event log + `seq`, resume replay, and the\n// lifecycle/meta files — lives in the shared `@ai-sdk/harness/bridge` runtime.\n// This file supplies only the Codex-specific turn driver.\n//\n// Host-defined tools are routed through an HTTP relay bound to\n// `127.0.0.1:0` with bearer-token auth. The codex CLI spawns\n// `host-tool-mcp.mjs` (shipped alongside this file) as a stdio MCP server;\n// the shim POSTs each tool call to the relay, which emits `tool-call` to\n// the host and waits for the matching `tool-result`.\n\nimport {\n runBridge,\n type BridgeEvent,\n type BridgeTurn,\n} from '@ai-sdk/harness/bridge';\nimport type { HarnessV1BuiltinToolName } from '@ai-sdk/harness';\nimport type { StartMessage } from '../codex-bridge-protocol';\nimport { randomUUID } from 'node:crypto';\nimport { writeFile } from 'node:fs/promises';\nimport { createServer, type Server } from 'node:http';\n// Temporary workaround for upstream codex MCP-tool bug — see ./cli-relay.ts\nimport {\n CLI_SHIM_FILENAME,\n buildCliShimScript,\n composeToolUsageInstructions,\n isToolRelayCommand,\n} from './cli-relay';\nimport { argv, env as procEnv, stdout } from 'node:process';\n\n/*\n * CONSTRAINT — the third-party imports below are NEVER bundled into the\n * compiled `bridge/index.mjs`. They are declared `external` in\n * tsup.config.ts and resolved at runtime from the node_modules that this\n * bridge installs *inside the sandbox* from `src/bridge/package.json` (and\n * its pinned `pnpm-lock.yaml`). That bridge package.json — NOT this host\n * package — is the single source of truth for these packages and their\n * versions; the published `@ai-sdk/harness-codex` package does not provide\n * them at runtime.\n *\n * When adding or changing a third-party import here you MUST keep all three\n * in sync, or the bridge will either get the dependency bundled in or fail\n * to resolve it in the sandbox:\n * 1. the import statement below,\n * 2. the `external` array in tsup.config.ts, and\n * 3. the dependency entry in `src/bridge/package.json`.\n */\nimport * as codexSdkModule from '@openai/codex-sdk';\n\n/*\n * Native Codex tool name → cross-harness common name. Tools outside this map\n * (e.g. MCP tools the model invokes by name) have no common equivalent; their\n * native name is forwarded as-is on `tool-call` events.\n */\nconst NATIVE_TO_COMMON: Readonly<Record<string, HarnessV1BuiltinToolName>> = {\n shell: 'bash',\n web_search: 'webSearch',\n};\n\nfunction toCommonName(nativeName: string): HarnessV1BuiltinToolName | string {\n return NATIVE_TO_COMMON[nativeName] ?? nativeName;\n}\n\nconst args = parseArgs(argv.slice(2));\nconst workdir = args.workdir;\nconst bridgeStateDir = args.bridgeStateDir;\nif (!workdir) {\n emitFatal('Missing --workdir argument.');\n}\nif (!bridgeStateDir) {\n emitFatal('Missing --bridge-state-dir argument.');\n}\nconst bootstrapDir = args.bootstrapDir ?? workdir;\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nconst codexSdk = codexSdkModule as any;\n\n// Codex thread id — survives across turns within this bridge process and is\n// returned to the host on `detach` so a future process can resume the thread.\nconst threadState: { id: string | undefined } = { id: undefined };\n\nawait runBridge<StartMessage>({\n bridgeType: 'codex',\n bridgeStateDir,\n onStart: runTurn,\n onDetach: () => (threadState.id ? { threadId: threadState.id } : {}),\n});\n\ntype Emit = (msg: Record<string, unknown>) => void;\n\nasync function runTurn(start: StartMessage, turn: BridgeTurn): Promise<void> {\n const emit: Emit = msg => turn.emit(msg as BridgeEvent);\n\n // Cross-process resume: the host carries the threadId we returned on detach.\n // Seed `threadState.id` so the codex SDK call below takes the `resumeThread`\n // branch.\n if (\n typeof start.resumeThreadId === 'string' &&\n start.resumeThreadId.length > 0\n ) {\n threadState.id = start.resumeThreadId;\n }\n\n /*\n * Known limitation: codex CLI does not currently surface MCP tools to the\n * model in `codex exec --experimental-json` mode (the path the\n * `@openai/codex-sdk` uses). The MCP handshake completes and `tools/list`\n * returns the host tool, but codex never registers it as a model-callable\n * function. Built-in MCP-resource accessors (`list_mcp_resources` etc.) are\n * exposed; tools are not. Tracked upstream at\n * https://github.com/openai/codex/issues/19425.\n *\n * Until that's fixed, host tools are made available to the model via a\n * separate CLI-relay workaround (see `./cli-relay.ts`). The MCP server\n * config below is kept so that the day codex starts exposing MCP tools\n * properly, host tools work both ways. Three hookpoints in this file\n * (writeFile for the shim, composeUserMessage's toolUsageBlock, and the\n * isToolRelayCommand filter in the event loop) implement the workaround\n * and can be removed once the upstream bug is fixed.\n */\n const mcpServers: Record<string, unknown> = {};\n let relay: { port: number; close(): void } | undefined;\n let cliShimPath: string | undefined;\n if (start.tools && start.tools.length > 0) {\n const relayToken = randomUUID();\n relay = await startToolRelay({\n relayToken,\n tools: start.tools,\n emit,\n requestToolResult: turn.requestToolResult,\n });\n mcpServers['harness-tools'] = {\n enabled: true,\n command: 'node',\n args: [`${bootstrapDir}/host-tool-mcp.mjs`],\n env: {\n TOOL_SCHEMAS: JSON.stringify(\n start.tools.map(t => ({\n name: t.name,\n description: t.description,\n inputSchema: t.inputSchema,\n })),\n ),\n TOOL_RELAY_URL: `http://127.0.0.1:${relay.port}`,\n TOOL_RELAY_TOKEN: relayToken,\n },\n };\n // Temporary workaround for upstream codex MCP-tool bug — see ./cli-relay.ts\n cliShimPath = `${workdir}/${CLI_SHIM_FILENAME}`;\n await writeFile(\n cliShimPath,\n buildCliShimScript({ relayPort: relay.port, relayToken }),\n 'utf8',\n );\n }\n\n const codexConfig: Record<string, unknown> = {};\n if (Object.keys(mcpServers).length > 0) codexConfig.mcp_servers = mcpServers;\n\n const gatewayBaseUrl = procEnv.AI_GATEWAY_BASE_URL;\n const hasGatewayAuth = Boolean(procEnv.AI_GATEWAY_API_KEY || gatewayBaseUrl);\n if (hasGatewayAuth && !gatewayBaseUrl) {\n throw new Error(\n 'AI Gateway auth was selected but AI_GATEWAY_BASE_URL is missing from the Codex bridge environment.',\n );\n }\n const apiBaseUrl = hasGatewayAuth ? gatewayBaseUrl : procEnv.OPENAI_BASE_URL;\n if (apiBaseUrl) {\n codexConfig.preferred_auth_method = 'apikey';\n codexConfig.model_provider = 'agent_bridge_openai';\n codexConfig.model_providers = {\n agent_bridge_openai: {\n name: procEnv.CODEX_MODEL_PROVIDER_NAME || 'Agent Bridge OpenAI',\n base_url: apiBaseUrl,\n env_key: 'CODEX_API_KEY',\n wire_api: 'responses',\n supports_websockets: false,\n },\n };\n }\n const usesConfiguredModelProvider =\n typeof codexConfig.model_provider === 'string';\n\n const codex = new codexSdk.Codex({\n ...(procEnv.CODEX_API_KEY ? { apiKey: procEnv.CODEX_API_KEY } : {}),\n ...(!usesConfiguredModelProvider && apiBaseUrl\n ? { baseUrl: apiBaseUrl }\n : {}),\n env: Object.fromEntries(\n Object.entries(procEnv).filter(\n (entry): entry is [string, string] => typeof entry[1] === 'string',\n ),\n ),\n ...(Object.keys(codexConfig).length > 0 ? { config: codexConfig } : {}),\n });\n\n const threadOptions = {\n ...(start.model ? { model: start.model } : {}),\n sandboxMode: 'danger-full-access',\n approvalPolicy: 'never',\n workingDirectory: workdir,\n skipGitRepoCheck: true,\n ...(start.reasoningEffort\n ? { modelReasoningEffort: start.reasoningEffort }\n : {}),\n webSearchMode: start.webSearch ? 'live' : 'disabled',\n };\n const thread = threadState.id\n ? codex.resumeThread(threadState.id, threadOptions)\n : codex.startThread(threadOptions);\n\n emit({ type: 'stream-start' });\n\n const userMessage = composeUserMessage({\n text: start.prompt,\n instructions: start.instructions,\n // Temporary workaround for upstream codex MCP-tool bug — see ./cli-relay.ts\n toolUsageBlock:\n cliShimPath && start.tools && start.tools.length > 0\n ? composeToolUsageInstructions({\n tools: start.tools,\n cliShimPath,\n })\n : undefined,\n });\n let turnUsage: Record<string, unknown> | undefined;\n const textByItem = new Map<string, string>();\n const reasoningByItem = new Map<string, string>();\n\n try {\n const { events } = await thread.runStreamed(userMessage, {\n signal: turn.abortSignal,\n });\n for await (const event of events as AsyncIterable<CodexEvent>) {\n if (turn.abortSignal.aborted) break;\n if (\n event.type === 'thread.started' &&\n typeof event.thread_id === 'string'\n ) {\n threadState.id = event.thread_id;\n // Announce to the host so it can include the id in resume state.\n emit({ type: 'bridge-thread', threadId: event.thread_id });\n }\n // Temporary workaround for upstream codex MCP-tool bug — see ./cli-relay.ts\n if (\n cliShimPath &&\n event.item?.type === 'command_execution' &&\n typeof event.item.command === 'string' &&\n isToolRelayCommand({ command: event.item.command, cliShimPath })\n ) {\n continue;\n }\n translateAndEmit(event, {\n send: emit,\n textByItem,\n reasoningByItem,\n setTurnUsage: u => (turnUsage = u),\n });\n }\n } catch (err) {\n emit({ type: 'error', error: serialiseError(err) });\n return;\n } finally {\n relay?.close();\n }\n\n emit({\n type: 'finish',\n finishReason: { unified: 'stop', raw: 'stop' },\n totalUsage: turnUsage ?? defaultUsage(),\n });\n\n void turn.pendingUserMessages; // accepted but only consumed when codex supports streamed user input\n}\n\ntype CodexItem = {\n type: string;\n id?: string;\n text?: string;\n command?: string;\n exit_code?: number;\n aggregated_output?: string;\n status?: 'in_progress' | 'completed' | 'failed';\n server?: string;\n tool?: string;\n arguments?: unknown;\n result?: { content?: unknown; structured_content?: unknown } | unknown;\n error?: { message?: string };\n query?: string;\n message?: string;\n changes?: ReadonlyArray<{\n path: string;\n kind: 'add' | 'delete' | 'update';\n }>;\n};\n\nfunction extractMcpToolCallResult(item: CodexItem): unknown {\n if (\n item.result === undefined ||\n item.result === null ||\n typeof item.result !== 'object'\n ) {\n return item.error?.message ? { error: item.error.message } : null;\n }\n const result = item.result as {\n content?: unknown;\n structured_content?: unknown;\n };\n if (\n result.structured_content !== undefined &&\n result.structured_content !== null\n ) {\n return result.structured_content;\n }\n return result.content ?? null;\n}\n\ntype CodexEvent = {\n type:\n | 'thread.started'\n | 'turn.completed'\n | 'turn.failed'\n | 'error'\n | 'item.started'\n | 'item.updated'\n | 'item.completed';\n item?: CodexItem;\n usage?: Record<string, number>;\n error?: { message: string };\n message?: string;\n thread_id?: string;\n};\n\nfunction translateAndEmit(\n event: CodexEvent,\n ctx: {\n send: Emit;\n textByItem: Map<string, string>;\n reasoningByItem: Map<string, string>;\n setTurnUsage: (u: Record<string, unknown>) => void;\n },\n): void {\n if (event.type === 'turn.completed') {\n if (event.usage) ctx.setTurnUsage(mapUsage(event.usage));\n ctx.send({\n type: 'finish-step',\n finishReason: { unified: 'stop', raw: 'stop' },\n usage: event.usage ? mapUsage(event.usage) : defaultUsage(),\n });\n return;\n }\n if (event.type === 'turn.failed') {\n ctx.send({\n type: 'error',\n error: event.error?.message ?? 'codex turn failed',\n });\n return;\n }\n if (event.type === 'error') {\n ctx.send({ type: 'error', error: event.message ?? 'codex error' });\n return;\n }\n if (!event.item) return;\n const item = event.item;\n const id = item.id ?? randomUUID();\n\n if (item.type === 'agent_message' && typeof item.text === 'string') {\n /*\n * The presence of `id` in `textByItem` — not the `item.started` event —\n * marks the text part as opened. Codex does not guarantee an\n * `item.started` event carrying text precedes the first `item.updated`\n * with text, so keying the `text-start` off the event type can emit a\n * `text-delta` for a part that was never opened. Opening lazily on the\n * first event with text keeps `text-start` before any `text-delta`.\n */\n if (!ctx.textByItem.has(id)) {\n ctx.send({ type: 'text-start', id });\n ctx.textByItem.set(id, '');\n }\n const last = ctx.textByItem.get(id) ?? '';\n const next = item.text;\n if (next.length > last.length) {\n ctx.send({ type: 'text-delta', id, delta: next.slice(last.length) });\n ctx.textByItem.set(id, next);\n }\n if (event.type === 'item.completed') ctx.send({ type: 'text-end', id });\n return;\n }\n\n if (item.type === 'reasoning' && typeof item.text === 'string') {\n if (!ctx.reasoningByItem.has(id)) {\n ctx.send({ type: 'reasoning-start', id });\n ctx.reasoningByItem.set(id, '');\n }\n const last = ctx.reasoningByItem.get(id) ?? '';\n const next = item.text;\n if (next.length > last.length) {\n ctx.send({ type: 'reasoning-delta', id, delta: next.slice(last.length) });\n ctx.reasoningByItem.set(id, next);\n }\n if (event.type === 'item.completed')\n ctx.send({ type: 'reasoning-end', id });\n return;\n }\n\n if (item.type === 'command_execution') {\n const nativeName = 'shell';\n if (event.type === 'item.started') {\n ctx.send({\n type: 'tool-call',\n toolCallId: id,\n toolName: toCommonName(nativeName),\n nativeName,\n input: JSON.stringify({ command: item.command ?? '' }),\n providerExecuted: true,\n });\n } else if (event.type === 'item.completed') {\n ctx.send({\n type: 'tool-result',\n toolCallId: id,\n toolName: toCommonName(nativeName),\n result: {\n exitCode: item.exit_code ?? null,\n output: item.aggregated_output ?? '',\n status: item.status ?? 'completed',\n },\n });\n }\n return;\n }\n\n if (item.type === 'mcp_tool_call') {\n const isHostTool = item.server === 'harness-tools';\n if (event.type === 'item.started') {\n ctx.send({\n type: 'tool-call',\n toolCallId: id,\n toolName: item.tool ?? 'unknown',\n ...(isHostTool ? {} : { nativeName: item.tool ?? 'unknown' }),\n input: JSON.stringify(item.arguments ?? {}),\n providerExecuted: !isHostTool,\n });\n } else if (event.type === 'item.completed') {\n ctx.send({\n type: 'tool-result',\n toolCallId: id,\n toolName: item.tool ?? 'unknown',\n result: extractMcpToolCallResult(item),\n });\n }\n return;\n }\n\n if (item.type === 'web_search') {\n const nativeName = 'web_search';\n if (event.type === 'item.started') {\n ctx.send({\n type: 'tool-call',\n toolCallId: id,\n toolName: toCommonName(nativeName),\n nativeName,\n input: JSON.stringify({ query: item.query ?? '' }),\n providerExecuted: true,\n });\n } else if (event.type === 'item.completed') {\n ctx.send({\n type: 'tool-result',\n toolCallId: id,\n toolName: toCommonName(nativeName),\n result: item.result ?? null,\n });\n }\n return;\n }\n\n if (item.type === 'file_change' && event.type === 'item.completed') {\n for (const change of item.changes ?? []) {\n ctx.send({\n type: 'file-change',\n event:\n change.kind === 'add'\n ? 'create'\n : change.kind === 'delete'\n ? 'delete'\n : 'modify',\n path: change.path,\n });\n }\n return;\n }\n\n if (item.type === 'error' && event.type === 'item.completed') {\n ctx.send({\n type: 'error',\n error: (item as { message?: string }).message ?? 'codex item error',\n });\n return;\n }\n}\n\nfunction mapUsage(usage: Record<string, number>): Record<string, unknown> {\n const input = usage.input_tokens ?? 0;\n const cacheRead = usage.cached_input_tokens ?? 0;\n return {\n inputTokens: {\n total: input,\n noCache: Math.max(0, input - cacheRead),\n cacheRead,\n cacheWrite: 0,\n },\n outputTokens: {\n total: usage.output_tokens ?? 0,\n text: usage.output_tokens ?? 0,\n },\n };\n}\n\nfunction defaultUsage(): Record<string, unknown> {\n return {\n inputTokens: { total: 0, noCache: 0, cacheRead: 0, cacheWrite: 0 },\n outputTokens: { total: 0, text: 0 },\n };\n}\n\nfunction composeUserMessage({\n text,\n instructions,\n toolUsageBlock,\n}: {\n text: string;\n instructions: string | undefined;\n toolUsageBlock: string | undefined;\n}): string {\n const blocks: string[] = [];\n /*\n * Frame instructions as system-provided operating guidance, not something\n * the user wrote, so the agent does not echo the prepended text back as if\n * the user had asked for it. Only present on the first user message of a\n * fresh session (the host gates it), so the matching `<user-message>` fence\n * is added only when instructions are present too.\n */\n if (instructions) {\n blocks.push(\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 if (toolUsageBlock) blocks.push(toolUsageBlock);\n blocks.push(instructions ? `<user-message>\\n${text}\\n</user-message>` : text);\n return blocks.join('\\n\\n');\n}\n\n/**\n * Tool relay — HTTP server on 127.0.0.1:0 with bearer-token auth. The MCP\n * stdio shim spawned by codex POSTs each tool invocation here; the relay\n * forwards the call to the host (via the shared runtime's `emit`), awaits the\n * matching `tool-result` (via `requestToolResult`), and responds with\n * `{ result }`.\n */\nasync function startToolRelay({\n relayToken,\n tools,\n emit,\n requestToolResult,\n}: {\n relayToken: string;\n tools: ReadonlyArray<{ name: string }>;\n emit: Emit;\n requestToolResult: (\n toolCallId: string,\n ) => Promise<{ output: unknown; isError?: boolean }>;\n}): Promise<{ port: number; close(): void }> {\n const toolNames = new Set(tools.map(t => t.name));\n\n const server = createServer(async (req, res) => {\n try {\n if (\n req.method !== 'POST' ||\n req.url !== '/' ||\n req.headers.authorization !== `Bearer ${relayToken}`\n ) {\n res.writeHead(401, { 'Content-Type': 'application/json' });\n res.end(JSON.stringify({ error: 'unauthorized tool relay request' }));\n return;\n }\n const chunks: Buffer[] = [];\n for await (const chunk of req) {\n chunks.push(chunk as Buffer);\n }\n const body = Buffer.concat(chunks).toString('utf8');\n const { requestId, toolName, input } = JSON.parse(body) as {\n requestId: string;\n toolName: string;\n input: unknown;\n };\n\n if (!toolNames.has(toolName)) {\n res.writeHead(403, { 'Content-Type': 'application/json' });\n res.end(\n JSON.stringify({ error: `Tool \"${toolName}\" is not available` }),\n );\n return;\n }\n\n emit({\n type: 'tool-call',\n toolCallId: requestId,\n toolName,\n input: JSON.stringify(input ?? {}),\n providerExecuted: false,\n });\n\n const { output, isError } = await requestToolResult(requestId);\n emit({\n type: 'tool-result',\n toolCallId: requestId,\n toolName,\n result: output ?? null,\n isError: !!isError,\n });\n\n res.writeHead(200, { 'Content-Type': 'application/json' });\n res.end(JSON.stringify({ result: output }));\n } catch (error) {\n res.writeHead(500, { 'Content-Type': 'application/json' });\n res.end(\n JSON.stringify({\n error: error instanceof Error ? error.message : String(error),\n }),\n );\n }\n });\n\n await new Promise<void>(resolve =>\n server.listen(0, '127.0.0.1', () => resolve()),\n );\n const address = server.address();\n if (!address || typeof address === 'string') {\n throw new Error('tool relay did not expose a numeric port');\n }\n return {\n port: address.port,\n close: () => closeServer(server),\n };\n}\n\nfunction closeServer(server: Server): void {\n try {\n server.close();\n } catch {}\n}\n\nfunction parseArgs(args: string[]): {\n workdir?: string;\n bridgeStateDir?: string;\n bootstrapDir?: string;\n} {\n const out: {\n workdir?: string;\n bridgeStateDir?: string;\n bootstrapDir?: string;\n } = {};\n for (let i = 0; i < args.length; i++) {\n if (args[i] === '--workdir' && i + 1 < args.length) {\n out.workdir = args[++i];\n } else if (args[i] === '--bridge-state-dir' && i + 1 < args.length) {\n out.bridgeStateDir = args[++i];\n } else if (args[i] === '--bootstrap-dir' && i + 1 < args.length) {\n out.bootstrapDir = args[++i];\n }\n }\n return out;\n}\n\nfunction serialiseError(err: unknown): unknown {\n if (err instanceof Error) {\n return { name: err.name, message: err.message, stack: err.stack };\n }\n return err;\n}\n\nfunction emitFatal(message: string): never {\n stdout.write(JSON.stringify({ type: 'bridge-fatal', message }) + '\\n');\n process.exit(1);\n}\n","/*\n * Temporary workaround for upstream codex CLI bug\n * https://github.com/openai/codex/issues/19425 — MCP tools registered via\n * `mcp_servers.*` are not exposed to the model in `codex exec --experimental-json`\n * mode (which is what `@openai/codex-sdk` uses). The MCP handshake completes\n * and `tools/list` succeeds, but codex never registers the tools as\n * model-callable functions.\n *\n * Until that's fixed upstream, this file implements a CLI-based relay:\n *\n * 1. A small Node script is written into the workdir at turn start\n * (`buildCliShimScript`). It accepts `<toolName> <jsonInput>` as argv,\n * POSTs to the same HTTP relay the MCP shim uses, and prints the\n * result to stdout.\n *\n * 2. Tool descriptions and invocation instructions are injected into the\n * user prompt (`composeToolUsageInstructions`), telling the model to\n * call host tools by running `node <shim-path> <toolName> '<jsonInput>'`\n * via its built-in `bash` tool.\n *\n * 3. The bridge's event loop suppresses the matching `command_execution`\n * events (`isToolRelayCommand`) so callers receive clean `tool-call` /\n * `tool-result` events from the HTTP relay rather than seeing the\n * relay invocations as raw bash commands.\n *\n * Once #19425 is fixed and MCP tools are properly exposed in exec mode, the\n * three hookpoints in `bridge/index.ts` can be removed along with this file.\n */\n\nexport const CLI_SHIM_FILENAME = 'harness-tool.mjs';\n\nexport function buildCliShimScript({\n relayPort,\n relayToken,\n}: {\n relayPort: number;\n relayToken: string;\n}): string {\n return `#!/usr/bin/env node\nconst [toolName, inputJson = '{}'] = process.argv.slice(2);\nif (!toolName) {\n console.error('Usage: harness-tool <tool_name> <json_input>');\n process.exit(64);\n}\nlet input;\ntry {\n input = JSON.parse(inputJson);\n} catch (error) {\n console.error('Invalid JSON input: ' + (error instanceof Error ? error.message : String(error)));\n process.exit(64);\n}\nconst requestId = 'cli-' + Date.now() + '-' + Math.random().toString(16).slice(2);\nconst response = await fetch('http://127.0.0.1:${relayPort}', {\n method: 'POST',\n headers: { 'Content-Type': 'application/json', Authorization: 'Bearer ${relayToken}' },\n body: JSON.stringify({ requestId, toolName, input }),\n});\nconst text = await response.text();\nlet payload;\ntry {\n payload = text ? JSON.parse(text) : {};\n} catch {\n payload = { error: text };\n}\nif (!response.ok || payload.error) {\n console.error(String(payload.error ?? ('tool relay failed with HTTP ' + response.status)));\n process.exit(1);\n}\nconsole.log(JSON.stringify(payload.result ?? payload, null, 2));\n`;\n}\n\nexport function composeToolUsageInstructions({\n tools,\n cliShimPath,\n}: {\n tools: ReadonlyArray<{\n name: string;\n description?: string;\n inputSchema?: unknown;\n }>;\n cliShimPath: string;\n}): string {\n const lines: string[] = [\n '## Host tools',\n '',\n 'You have access to the following host-provided tools. To use one, run the following command via your built-in `bash` tool:',\n '',\n ` node ${cliShimPath} <toolName> '<jsonInput>'`,\n '',\n 'The script prints the JSON result to stdout. Do not invent another way to call these tools — only this CLI invocation will work. Pass the JSON input as a single-quoted argument.',\n 'For every user request that depends on a host-provided tool, run a separate CLI invocation for each needed tool call in the current turn before answering. Do not reuse previous tool results, and do not say you used a host tool unless the command has completed in the current turn.',\n '',\n ];\n for (const tool of tools) {\n lines.push(`### ${tool.name}`);\n if (tool.description) lines.push(tool.description);\n lines.push(\n `Input schema: \\`${JSON.stringify(tool.inputSchema ?? {})}\\``,\n '',\n );\n }\n return lines.join('\\n');\n}\n\nexport function isToolRelayCommand({\n command,\n cliShimPath,\n}: {\n command: string;\n cliShimPath: string;\n}): boolean {\n return command.includes(cliShimPath);\n}\n"],"mappings":";AAQA,SAAS,YAAY,OAAO,iBAAiB;AAC7C,SAAS,YAAY,oBAAoB;AACzC,SAAS,OAAO,SAAS,KAAK,cAAc;AAC5C,SAAS,uBAAuC;AAqBhD,IAAM,qBAAuD;EAC3D,OAAO;EACP,MAAM;EACN,MAAM;EACN,OAAO;EACP,OAAO;AACT;AAGA,SAAS,iBACP,SACA,WACS;AACT,MAAI,CAAC,WAAW,QAAQ,WAAW,EAAG,QAAO;AAC7C,SAAO,QAAQ;IACb,CAAA,WAAU,cAAc,UAAU,UAAU,WAAW,GAAG,MAAM,GAAG;EACrE;AACF;AAEA,SAAS,kBAAkB,KAIzB;AACA,MAAI,eAAe,OAAO;AACxB,WAAO,EAAE,MAAM,IAAI,MAAM,SAAS,IAAI,SAAS,OAAO,IAAI,MAAM;EAClE;AACA,SAAO,EAAE,SAAS,OAAO,GAAG,EAAE;AAChC;AAEA,SAAS,aAAa,OAAiD;AACrE,MAAI,CAAC,MAAO,QAAO;AACnB,QAAM,QAAQ,MACX,MAAM,GAAG,EACT,IAAI,CAAA,SAAQ,KAAK,KAAK,CAAC,EACvB,OAAO,OAAO;AACjB,SAAO,MAAM,SAAS,IAAI,QAAQ;AACpC;AAEA,IAAM,aAAa,oBAAI,IAAI,CAAC,KAAK,QAAQ,OAAO,IAAI,CAAC;AAsGrD,IAAM,UAAU;AAehB,eAAsB,UACpB,SACuB;AACvB,QAAM,EAAE,YAAY,gBAAAA,iBAAgB,SAAS,SAAS,IAAI;AAC1D,QAAM,gBAAgB,QAAQ,SAAS,QAAQ,wBAAwB;AACvE,QAAM,eACJ,QAAQ,QAAQ,SAAS,QAAQ,kBAAkB,KAAK,EAAE;AAE5D,QAAM,iBAAiB,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AA,IACL,MAAM,QAAQ;AAAA,IACd,OAAO,MAAM,YAAY,MAAM;AAAA,EACjC;AACF;AAEA,SAAS,YAAY,QAAsB;AACzC,MAAI;AACF,WAAO,MAAM;AAAA,EACf,QAAQ;AAAA,EAAC;AACX;AAEA,SAAS,UAAUC,OAIjB;AACA,QAAM,MAIF,CAAC;AACL,WAAS,IAAI,GAAG,IAAIA,MAAK,QAAQ,KAAK;AACpC,QAAIA,MAAK,CAAC,MAAM,eAAe,IAAI,IAAIA,MAAK,QAAQ;AAClD,UAAI,UAAUA,MAAK,EAAE,CAAC;AAAA,IACxB,WAAWA,MAAK,CAAC,MAAM,wBAAwB,IAAI,IAAIA,MAAK,QAAQ;AAClE,UAAI,iBAAiBA,MAAK,EAAE,CAAC;AAAA,IAC/B,WAAWA,MAAK,CAAC,MAAM,qBAAqB,IAAI,IAAIA,MAAK,QAAQ;AAC/D,UAAI,eAAeA,MAAK,EAAE,CAAC;AAAA,IAC7B;AAAA,EACF;AACA,SAAO;AACT;AAEA,SAASD,gBAAe,KAAuB;AAC7C,MAAI,eAAe,OAAO;AACxB,WAAO,EAAE,MAAM,IAAI,MAAM,SAAS,IAAI,SAAS,OAAO,IAAI,MAAM;AAAA,EAClE;AACA,SAAO;AACT;AAEA,SAAS,UAAU,SAAwB;AACzC,EAAAF,QAAO,MAAM,KAAK,UAAU,EAAE,MAAM,gBAAgB,QAAQ,CAAC,IAAI,IAAI;AACrE,UAAQ,KAAK,CAAC;AAChB;","names":["bridgeStateDir","writeFile","procEnv","stdout","writeFile","serialiseError","args"]}
@@ -6,7 +6,7 @@
6
6
  "dependencies": {
7
7
  "@openai/codex-sdk": "0.130.0",
8
8
  "@modelcontextprotocol/sdk": "1.29.0",
9
- "ws": "8.20.1",
9
+ "ws": "8.21.0",
10
10
  "zod": "3.25.76"
11
11
  }
12
12
  }
@@ -15,8 +15,8 @@ importers:
15
15
  specifier: 0.130.0
16
16
  version: 0.130.0
17
17
  ws:
18
- specifier: 8.20.1
19
- version: 8.20.1
18
+ specifier: 8.21.0
19
+ version: 8.21.0
20
20
  zod:
21
21
  specifier: 3.25.76
22
22
  version: 3.25.76
@@ -430,8 +430,8 @@ packages:
430
430
  wrappy@1.0.2:
431
431
  resolution: {integrity: sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ==}
432
432
 
433
- ws@8.20.1:
434
- resolution: {integrity: sha512-It4dO0K5v//JtTXuPkfEOaI3uUN87iYPnqo/ZzqCoG3g8uhA66QUMs/SrM0YK7/NAu+r4LMh/9dq2A7k+rHs+w==}
433
+ ws@8.21.0:
434
+ resolution: {integrity: sha512-Vsp28b7DRcimFQvrqu2Wek3z1iYxDCWqHYB8Qsnk/S4RfaCQzPGPyBNuVjJV3cd6UiKtUtp6sNM77gWvzcCH+g==}
435
435
  engines: {node: '>=10.0.0'}
436
436
  peerDependencies:
437
437
  bufferutil: ^4.0.1
@@ -870,7 +870,7 @@ snapshots:
870
870
 
871
871
  wrappy@1.0.2: {}
872
872
 
873
- ws@8.20.1: {}
873
+ ws@8.21.0: {}
874
874
 
875
875
  zod-to-json-schema@3.25.2(zod@3.25.76):
876
876
  dependencies:
package/dist/index.js CHANGED
@@ -18,6 +18,7 @@ import { WebSocket } from "ws";
18
18
  import { z as z2 } from "zod";
19
19
 
20
20
  // src/codex-auth.ts
21
+ import { getAiGatewayAuthFromEnv } from "@ai-sdk/harness/utils";
21
22
  function resolveCodexEnv(auth, processEnv = process.env) {
22
23
  if (auth?.openaiCompatible) {
23
24
  return pickOpenAICompatible(auth.openaiCompatible, processEnv);
@@ -25,11 +26,20 @@ function resolveCodexEnv(auth, processEnv = process.env) {
25
26
  if (auth?.openai) {
26
27
  return pickOpenAI({ explicit: auth.openai, processEnv });
27
28
  }
29
+ const gatewayAuthFromEnv = getAiGatewayAuthFromEnv({
30
+ env: processEnv
31
+ });
28
32
  if (auth?.gateway) {
29
- return pickGateway(auth.gateway, processEnv);
33
+ return pickGateway({
34
+ explicit: auth.gateway,
35
+ gatewayAuthFromEnv
36
+ });
30
37
  }
31
- if (processEnv.AI_GATEWAY_API_KEY) {
32
- return pickGateway({}, processEnv);
38
+ if (gatewayAuthFromEnv.apiKey) {
39
+ return pickGateway({
40
+ explicit: {},
41
+ gatewayAuthFromEnv
42
+ });
33
43
  }
34
44
  return pickOpenAI({ processEnv });
35
45
  }
@@ -59,9 +69,14 @@ function pickOpenAI({
59
69
  if (project) env.OPENAI_PROJECT = project;
60
70
  return env;
61
71
  }
62
- function pickGateway(explicit, processEnv) {
63
- const apiKey = explicit.apiKey ?? processEnv.AI_GATEWAY_API_KEY;
64
- const baseUrl = explicit.baseUrl ?? processEnv.AI_GATEWAY_BASE_URL ?? "https://ai-gateway.vercel.sh/v1";
72
+ function pickGateway({
73
+ explicit,
74
+ gatewayAuthFromEnv
75
+ }) {
76
+ const apiKey = explicit.apiKey ?? gatewayAuthFromEnv.apiKey;
77
+ const baseUrl = toCodexGatewayBaseUrl(
78
+ explicit.baseUrl ?? gatewayAuthFromEnv.baseUrl
79
+ );
65
80
  const env = {};
66
81
  if (apiKey) {
67
82
  env.AI_GATEWAY_API_KEY = apiKey;
@@ -70,6 +85,10 @@ function pickGateway(explicit, processEnv) {
70
85
  env.AI_GATEWAY_BASE_URL = baseUrl;
71
86
  return env;
72
87
  }
88
+ function toCodexGatewayBaseUrl(baseUrl) {
89
+ const trimmed = baseUrl.replace(/\/+$/, "");
90
+ return trimmed.endsWith("/v1") ? trimmed : `${trimmed}/v1`;
91
+ }
73
92
 
74
93
  // src/codex-bridge-protocol.ts
75
94
  import {
@@ -112,7 +131,7 @@ var CODEX_BUILTIN_TOOLS = {
112
131
  })
113
132
  };
114
133
  var BOOTSTRAP_DIR = "/tmp/harness/codex";
115
- var bridgeCoordsSchema = z2.object({
134
+ var codexBridgeCoordsSchema = z2.object({
116
135
  port: z2.number(),
117
136
  token: z2.string(),
118
137
  lastSeenEventId: z2.number(),
@@ -120,7 +139,7 @@ var bridgeCoordsSchema = z2.object({
120
139
  });
121
140
  var codexResumeStateSchema = z2.object({
122
141
  threadId: z2.string().optional(),
123
- bridge: bridgeCoordsSchema.optional()
142
+ bridge: codexBridgeCoordsSchema.optional()
124
143
  });
125
144
  function createCodex(settings = {}) {
126
145
  let cachedBootstrap;
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/codex-harness.ts","../src/codex-auth.ts","../src/codex-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 HarnessCapabilityUnsupportedError,\n harnessV1DiagnosticFromBridgeFrame,\n type HarnessV1,\n type HarnessV1Bootstrap,\n type HarnessV1DebugConfig,\n type HarnessV1BuiltinTool,\n type HarnessV1ContinueTurnState,\n type HarnessV1Prompt,\n type HarnessV1PromptControl,\n type HarnessV1ResumeSessionState,\n type HarnessV1NetworkSandboxSession,\n type HarnessV1PermissionMode,\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 type Experimental_SandboxProcess,\n type Experimental_SandboxSession,\n} from '@ai-sdk/provider-utils';\nimport { WebSocket } from 'ws';\nimport { z } from 'zod';\nimport { resolveCodexEnv, type CodexAuthOptions } from './codex-auth';\nimport {\n outboundMessageSchema,\n type InboundMessage,\n type OutboundMessage,\n} from './codex-bridge-protocol';\n\ntype CodexChannel = SandboxChannel<OutboundMessage, InboundMessage>;\ntype CodexRespawnStrategy = 'replay' | 'rerun';\n\ntype WriteSkillsResult = {\n readonly homeDir: string;\n readonly codexHomeDir: string;\n};\n\n/*\n * The model the adapter pins when the consumer configures none. The Codex SDK\n * does not report the model it resolves to at runtime (no model field on any\n * event), and exposes no default-model constant, so we pin the latest\n * codex-specialized model available for the bundled `@openai/codex@0.130.0`\n * (published 2026-05-08): `gpt-5.3-codex` (released 2026-02). Keep this in sync\n * when bumping the codex SDK/binary. Passing it explicitly makes the resolved\n * model deterministic and the telemetry (`gen_ai.request.model`) accurate.\n */\nconst DEFAULT_CODEX_MODEL = 'gpt-5.3-codex';\n\nexport type CodexHarnessSettings = {\n readonly auth?: CodexAuthOptions;\n /**\n * OpenAI model id the underlying `codex` CLI should use. Leaving this unset\n * pins the adapter default (`DEFAULT_CODEX_MODEL`).\n */\n readonly model?: string;\n /**\n * Reasoning effort for reasoning-capable models. Leaving this unset\n * defers to the CLI's default.\n */\n readonly reasoningEffort?: 'low' | 'medium' | 'high';\n /**\n * When `true`, allow the underlying runtime to use live web search.\n */\n readonly webSearch?: boolean;\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 Codex CLI can invoke as a model-callable tool,\n * declared as a `ToolSet` keyed by what the bridge emits as `toolName` on\n * the wire (`commonName ?? nativeName`). Schemas reflect the `ThreadItem`\n * union in `@openai/codex-sdk`'s `dist/index.d.ts`.\n *\n * Codex's other native operations (`apply_patch`, todo planning) surface\n * only as side-effect events (`file_change`, `todo_list`) and are not\n * model-callable tools — they don't appear here.\n */\nconst CODEX_BUILTIN_TOOLS = {\n bash: commonTool('bash', {\n nativeName: 'shell',\n toolUseKind: 'bash',\n description: 'Execute a shell command',\n inputSchema: z.object({ command: z.string() }),\n }),\n webSearch: commonTool('webSearch', {\n nativeName: 'web_search',\n toolUseKind: 'readonly',\n description: 'Search the web',\n inputSchema: z.object({ query: z.string() }),\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 contents (the codex CLI shim and any files the agent edits) and\n * the bridge state files survive both detach -> attach and\n * stop -> snapshot -> resume cycles.\n */\nconst BOOTSTRAP_DIR = '/tmp/harness/codex';\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 bridgeCoordsSchema = 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 lifecycle `data` payload Codex produces.\n * `threadId` is what `codex.resumeThread(...)` requires for the replay/rerun\n * rungs; the sandbox lookup is handled separately via\n * `provider.resumeSession({ sessionId })`. `bridge` carries live coordinates\n * for cross-process `attach` (present on `doDetach()` and `doSuspendTurn()`\n * payloads).\n */\nconst codexResumeStateSchema = z.object({\n threadId: z.string().optional(),\n bridge: bridgeCoordsSchema.optional(),\n});\n\ntype CodexBridgeCoords = z.infer<typeof bridgeCoordsSchema>;\n\nexport function createCodex(\n settings: CodexHarnessSettings = {},\n): HarnessV1<typeof CODEX_BUILTIN_TOOLS> {\n let cachedBootstrap: HarnessV1Bootstrap | undefined;\n\n return {\n specificationVersion: 'harness-v1',\n harnessId: 'codex',\n builtinTools: CODEX_BUILTIN_TOOLS,\n supportsBuiltinToolApprovals: false,\n lifecycleStateSchema: codexResumeStateSchema,\n getBootstrap: async () => {\n if (cachedBootstrap != null) return cachedBootstrap;\n const [pkg, lock, bridge, hostToolMcp] = await Promise.all([\n readBridgeAsset('package.json'),\n readBridgeAsset('pnpm-lock.yaml'),\n readBridgeAsset('index.mjs'),\n readBridgeAsset('host-tool-mcp.mjs'),\n ]);\n cachedBootstrap = {\n harnessId: 'codex',\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 path: `${BOOTSTRAP_DIR}/host-tool-mcp.mjs`,\n content: hostToolMcp,\n },\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 };\n return cachedBootstrap;\n },\n doStart: async startOpts => {\n if (\n startOpts.permissionMode != null &&\n startOpts.permissionMode !== 'allow-all'\n ) {\n throw new HarnessCapabilityUnsupportedError({\n message:\n \"Harness 'codex' does not support built-in tool approval requests; use permissionMode: 'allow-all'.\",\n harnessId: 'codex',\n });\n }\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 resumeData =\n isResume && typeof lifecycleState?.data === 'object'\n ? (lifecycleState.data as {\n threadId?: unknown;\n bridge?: CodexBridgeCoords;\n })\n : undefined;\n const resumeThreadId = resumeData?.threadId;\n const resumeThreadIdString =\n typeof resumeThreadId === 'string' && resumeThreadId.length > 0\n ? resumeThreadId\n : undefined;\n const coords = resumeData?.bridge;\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 /*\n * Rung 1 — ATTACH. With live coordinates, reopen a socket to the\n * still-running bridge. Parked between-turn sessions just attach and wait\n * for the next `start`; suspended in-flight turns request replay of\n * everything past the persisted cursor. No spawn, no fresh token. If the\n * bridge is gone the open throws and we fall through to a spawn-based\n * 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: CodexChannel = new SandboxChannel({\n connect: () => openWebSocket(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; no process handle.\n proc: undefined,\n model: settings.model ?? DEFAULT_CODEX_MODEL,\n reasoningEffort: settings.reasoningEffort,\n webSearch: settings.webSearch,\n resumeThreadId: resumeThreadIdString,\n isResume: true,\n seedResumeThreadOnFirstPrompt: false,\n rerunContinue: false,\n bridgePort: coords.port,\n bridgeToken: coords.token,\n sandboxId,\n debug: startOpts.observability?.debug,\n permissionMode: startOpts.permissionMode,\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 * via `codex.resumeThread(threadId)` when attach is unavailable.\n */\n let respawnStrategy: CodexRespawnStrategy | 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 port = resolveBridgePort(sandboxSession, settings.port);\n const token = randomBytes(32).toString('hex');\n const codexSkillSetup =\n startOpts.skills && startOpts.skills.length > 0\n ? await writeSkills({\n sandbox: session,\n skills: startOpts.skills,\n abortSignal: startOpts.abortSignal,\n })\n : undefined;\n const env = {\n ...resolveCodexEnv(settings.auth),\n BRIDGE_CHANNEL_TOKEN: token,\n BRIDGE_WS_PORT: String(port),\n ...(codexSkillSetup\n ? {\n HOME: codexSkillSetup.homeDir,\n CODEX_HOME: codexSkillSetup.codexHomeDir,\n }\n : {}),\n ...(respawnStrategy === 'replay'\n ? { BRIDGE_REPLAY_FROM_DISK: '1' }\n : {}),\n };\n\n if (respawnStrategy === undefined) {\n await session.run({\n command: `mkdir -p ${workDir} ${bridgeStateDir}`,\n abortSignal: startOpts.abortSignal,\n });\n }\n\n await markBridgeStarting({\n sandbox: session,\n bridgeStateDir,\n bridgeType: 'codex',\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} --bootstrap-dir ${BOOTSTRAP_DIR}`,\n env,\n abortSignal: startOpts.abortSignal,\n });\n\n const { port: boundPort } = await waitForBridgeReady({\n proc,\n sandbox: session,\n bridgeStateDir,\n bridgeType: 'codex',\n timeoutMs,\n abortSignal: startOpts.abortSignal,\n createTimeoutError: () =>\n new Error('codex bridge did not become ready in time.'),\n createExitError: () =>\n new Error('codex bridge exited before becoming ready.'),\n });\n void drainRest(proc.stdout);\n /*\n * Bridge stderr is the only diagnostic channel for what happens\n * inside the sandbox once the bridge is running (uncaught\n * exceptions, Codex SDK errors, network failures). Forward it\n * line-by-line to the host console so a mid-turn bridge crash can\n * be inspected from `pnpm dev` logs without redeploying. The\n * bridge itself writes nothing to stderr in steady state, so this\n * is silent on the happy path.\n */\n void forwardBridgeStderr(proc.stderr);\n\n const wsUrl =\n (await sandboxSession.getPortUrl({\n port: boundPort,\n protocol: 'ws',\n })) + `?agent_bridge_token=${encodeURIComponent(token)}`;\n\n const channel: CodexChannel = new SandboxChannel({\n connect: () => openWebSocket(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`).\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 ?? DEFAULT_CODEX_MODEL,\n reasoningEffort: settings.reasoningEffort,\n webSearch: settings.webSearch,\n resumeThreadId: resumeThreadIdString,\n isResume: respawnStrategy !== undefined,\n seedResumeThreadOnFirstPrompt: respawnStrategy !== undefined,\n rerunContinue: respawnStrategy === 'rerun',\n bridgePort: boundPort,\n bridgeToken: token,\n sandboxId,\n debug: startOpts.observability?.debug,\n permissionMode: startOpts.permissionMode,\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: 'codex',\n message:\n 'The codex harness needs a TCP port exposed by the sandbox. ' +\n 'Create the sandbox with `ports: [<port>]` or pass `createCodex({ port })`.',\n });\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 writeSkills({\n sandbox,\n skills,\n abortSignal,\n}: {\n sandbox: Experimental_SandboxSession;\n skills: ReadonlyArray<HarnessV1Skill>;\n abortSignal?: AbortSignal;\n}): Promise<WriteSkillsResult> {\n for (const skill of skills) {\n safeCodexSkillName(skill.name);\n for (const file of skill.files ?? []) {\n safeCodexSkillFilePath({\n skillName: skill.name,\n filePath: file.path,\n });\n }\n }\n\n const homeDir = await resolveSandboxHomeDir({ sandbox, abortSignal });\n const codexHomeDir = path.posix.join(homeDir, '.codex');\n await sandbox.run({\n command: `mkdir -p ${shellQuote(codexHomeDir)}`,\n abortSignal,\n });\n\n const rootDir = path.posix.join(homeDir, '.agents', 'skills');\n await sandbox.run({\n command: `mkdir -p ${shellQuote(rootDir)}`,\n abortSignal,\n });\n\n for (const skill of skills) {\n const name = safeCodexSkillName(skill.name);\n const skillDir = path.posix.join(rootDir, name);\n const content = `---\\nname: ${skill.name}\\ndescription: ${skill.description}\\n---\\n\\n${skill.content}`;\n\n await sandbox.writeTextFile({\n path: path.posix.join(skillDir, 'SKILL.md'),\n content,\n abortSignal,\n });\n\n for (const file of skill.files ?? []) {\n const filePath = safeCodexSkillFilePath({\n skillName: skill.name,\n filePath: file.path,\n });\n await sandbox.writeTextFile({\n path: path.posix.join(skillDir, filePath),\n content: file.content,\n abortSignal,\n });\n }\n }\n\n return {\n homeDir,\n codexHomeDir,\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 safeCodexSkillName(name: string): string {\n if (!/^[A-Za-z0-9._-]+$/.test(name) || name === '.' || name === '..') {\n throw new Error(`Invalid Codex skill name: ${name}`);\n }\n return name;\n}\n\nfunction safeCodexSkillFilePath({\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 Codex 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 forwardBridgeStderr(\n stream: ReadableStream<Uint8Array>,\n): Promise<void> {\n try {\n const reader = stream.pipeThrough(new TextDecoderStream()).getReader();\n while (true) {\n const { value, done } = await reader.read();\n if (done) return;\n if (value) {\n const trimmed = value.endsWith('\\n') ? value.slice(0, -1) : value;\n if (trimmed.length > 0) {\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\nfunction openWebSocket(url: string): Promise<WebSocket> {\n return new Promise((resolve, reject) => {\n const ws = new WebSocket(url);\n const onOpen = () => {\n ws.off('error', onError);\n resolve(ws);\n };\n const onError = (err: Error) => {\n ws.off('open', onOpen);\n reject(err);\n };\n ws.once('open', onOpen);\n ws.once('error', onError);\n });\n}\n\nfunction createSession({\n sessionId,\n channel,\n proc,\n model,\n reasoningEffort,\n webSearch,\n resumeThreadId,\n isResume,\n seedResumeThreadOnFirstPrompt,\n rerunContinue,\n bridgePort,\n bridgeToken,\n sandboxId,\n debug,\n permissionMode,\n}: {\n sessionId: string;\n channel: CodexChannel;\n /** Undefined on `attach` — the live bridge was spawned by another process. */\n proc: Experimental_SandboxProcess | undefined;\n model: string | undefined;\n reasoningEffort: 'low' | 'medium' | 'high' | undefined;\n webSearch: boolean | undefined;\n resumeThreadId: string | undefined;\n isResume: boolean;\n seedResumeThreadOnFirstPrompt: boolean;\n rerunContinue: boolean;\n bridgePort: number;\n bridgeToken: string;\n sandboxId: string;\n debug: HarnessV1DebugConfig | undefined;\n permissionMode: HarnessV1PermissionMode | undefined;\n}): HarnessV1Session {\n let stopped = false;\n let stopPromise: Promise<void> | undefined;\n /*\n * Send the persisted threadId on the first prompt only when the bridge was\n * respawned (rerun/replay) so it takes the `codex.resumeThread(...)` branch.\n * An `attach`ed bridge already holds its threadState in memory and continues\n * on its own, so it needs no seed.\n */\n let pendingResumeThreadId = seedResumeThreadOnFirstPrompt\n ? resumeThreadId\n : undefined;\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 persisted thread), so it starts\n * \"applied\".\n */\n let instructionsApplied = isResume;\n\n /*\n * Latest codex thread id, cached from the bridge's `bridge-thread`\n * announcements. Seeded from lifecycle state so `doDetach()` and `doStop()`\n * can include a thread id even before this process has run a turn.\n */\n let latestThreadId = resumeThreadId;\n channel.on('bridge-thread', msg => {\n latestThreadId = msg.threadId;\n });\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 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 'file-change',\n 'finish-step',\n 'raw',\n ] as const;\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 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(new Error('codex bridge closed before the turn finished.'));\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 const applyInstructions =\n !instructionsApplied && !!promptOpts.instructions;\n instructionsApplied = true;\n\n const startMessage = {\n type: 'start' as const,\n prompt: extractUserText(promptOpts.prompt),\n ...(applyInstructions ? { instructions: promptOpts.instructions } : {}),\n tools: (promptOpts.tools ?? []).map(t => ({\n name: t.name,\n description: t.description,\n inputSchema: t.inputSchema,\n })),\n model,\n reasoningEffort,\n webSearch,\n ...(permissionMode ? { permissionMode } : {}),\n ...(pendingResumeThreadId\n ? { resumeThreadId: pendingResumeThreadId }\n : {}),\n ...(debug ? { debug } : {}),\n };\n pendingResumeThreadId = undefined;\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 codex's own thread via `resumeThreadId`. Lossy — work in flight\n * at the interruption is recomputed. This is the rare bridge-died\n * fallback; the common slice path is `attach`.\n */\n if (rerunContinue) {\n const threadId = pendingResumeThreadId ?? latestThreadId;\n pendingResumeThreadId = undefined;\n channel.send({\n type: 'start' as const,\n /*\n * A continuation nudge rather than an empty prompt: `resumeThreadId`\n * rehydrates the prior thread, and this is the new user turn that\n * drives it forward. Keeping it non-empty avoids handing the runtime\n * an empty user message (and mirrors the claude-code adapter, where an\n * empty text block trips the Anthropic API's `cache_control` rule).\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 reasoningEffort,\n webSearch,\n ...(permissionMode ? { permissionMode } : {}),\n ...(threadId ? { resumeThreadId: threadId } : {}),\n ...(debug ? { debug } : {}),\n });\n }\n\n return control;\n },\n doCompact: async () => {\n /*\n * Codex compacts its context automatically inside the core turn loop\n * (~90% of the model context window), but the `codex exec` transport this\n * adapter drives exposes no manual compaction trigger and emits no\n * compaction event. Manual `compact()` is therefore unsupported; Codex's\n * own auto-compaction continues to run regardless.\n */\n throw new HarnessCapabilityUnsupportedError({\n message:\n \"Harness 'codex' does not support manual compaction; Codex auto-compacts its context internally.\",\n harnessId: 'codex',\n });\n },\n doDetach: async () => {\n if (stopped) {\n throw new Error(\n `codex 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: 'codex',\n specificationVersion: 'harness-v1',\n data: {\n ...(latestThreadId ? { threadId: latestThreadId } : {}),\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 `codex 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 — the workdir is still captured by the sandbox snapshot\n * during the subsequent `sandboxSession.stop()`, so the next turn can\n * resume the filesystem state. The trade-off: we lose\n * `threadId`, so the codex CLI starts a fresh thread on the\n * preserved workdir rather than resuming the prior conversation\n * inside Codex's runtime. Ability to continue beats throwing.\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 `codex 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 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: 'codex',\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 `codex 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: 'codex',\n specificationVersion: 'harness-v1',\n data: {\n ...(latestThreadId ? { threadId: latestThreadId } : {}),\n bridge: {\n port: bridgePort,\n token: bridgeToken,\n lastSeenEventId,\n sandboxId,\n },\n },\n };\n return payload;\n },\n };\n}\n\n/*\n * Reduce a `HarnessV1Prompt` to the plain user text the bridge forwards\n * to the Codex 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: 'codex',\n message: `The codex 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","export type CodexAuthOptions = {\n readonly openaiCompatible?: {\n readonly apiKey?: string;\n readonly baseUrl?: string;\n readonly modelProviderName?: string;\n readonly queryParamsJson?: string;\n };\n readonly openai?: {\n readonly apiKey?: string;\n readonly baseUrl?: string;\n readonly organization?: string;\n readonly project?: string;\n };\n readonly gateway?: {\n readonly apiKey?: string;\n readonly baseUrl?: string;\n };\n};\n\n/**\n * Resolve the environment-variable blob the codex bridge needs. Precedence:\n *\n * 1. Explicit `auth.openaiCompatible` — pin to a custom OpenAI-compatible endpoint.\n * 2. Explicit `auth.openai` — pin to direct OpenAI auth.\n * 3. Explicit `auth.gateway` — pin to Vercel AI Gateway.\n * 4. Auto-detect from the host process env: gateway first\n * (`AI_GATEWAY_API_KEY`), then `CODEX_API_KEY` / `OPENAI_API_KEY`.\n */\nexport function resolveCodexEnv(\n auth: CodexAuthOptions | undefined,\n processEnv: Record<string, string | undefined> = process.env,\n): Record<string, string> {\n if (auth?.openaiCompatible) {\n return pickOpenAICompatible(auth.openaiCompatible, processEnv);\n }\n if (auth?.openai) {\n return pickOpenAI({ explicit: auth.openai, processEnv });\n }\n if (auth?.gateway) {\n return pickGateway(auth.gateway, processEnv);\n }\n if (processEnv.AI_GATEWAY_API_KEY) {\n return pickGateway({}, processEnv);\n }\n return pickOpenAI({ processEnv });\n}\n\nfunction pickOpenAICompatible(\n explicit: NonNullable<CodexAuthOptions['openaiCompatible']>,\n processEnv: Record<string, string | undefined>,\n): Record<string, string> {\n const env: Record<string, string> = {};\n const apiKey =\n explicit.apiKey ?? processEnv.OPENAI_API_KEY ?? processEnv.CODEX_API_KEY;\n if (apiKey) env.CODEX_API_KEY = apiKey;\n if (explicit.baseUrl) env.OPENAI_BASE_URL = explicit.baseUrl;\n if (explicit.modelProviderName)\n env.CODEX_MODEL_PROVIDER_NAME = explicit.modelProviderName;\n if (explicit.queryParamsJson)\n env.OPENAI_QUERY_PARAMS_JSON = explicit.queryParamsJson;\n return env;\n}\n\nfunction pickOpenAI({\n explicit,\n processEnv,\n}: {\n explicit?: NonNullable<CodexAuthOptions['openai']>;\n processEnv: Record<string, string | undefined>;\n}): Record<string, string> {\n const env: Record<string, string> = {};\n const apiKey =\n explicit?.apiKey ?? processEnv.OPENAI_API_KEY ?? processEnv.CODEX_API_KEY;\n if (apiKey) env.CODEX_API_KEY = apiKey;\n const baseUrl = explicit?.baseUrl ?? processEnv.OPENAI_BASE_URL;\n if (baseUrl) env.OPENAI_BASE_URL = baseUrl;\n const organization = explicit?.organization ?? processEnv.OPENAI_ORGANIZATION;\n if (organization) env.OPENAI_ORGANIZATION = organization;\n const project = explicit?.project ?? processEnv.OPENAI_PROJECT;\n if (project) env.OPENAI_PROJECT = project;\n return env;\n}\n\nfunction pickGateway(\n explicit: NonNullable<CodexAuthOptions['gateway']>,\n processEnv: Record<string, string | undefined>,\n): Record<string, string> {\n const apiKey = explicit.apiKey ?? processEnv.AI_GATEWAY_API_KEY;\n const baseUrl =\n explicit.baseUrl ??\n processEnv.AI_GATEWAY_BASE_URL ??\n 'https://ai-gateway.vercel.sh/v1';\n const env: Record<string, string> = {};\n if (apiKey) {\n env.AI_GATEWAY_API_KEY = apiKey;\n env.CODEX_API_KEY = apiKey;\n }\n env.AI_GATEWAY_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 * Codex's bridge wire protocol. The outbound events (including `file-change`\n * and the `bridge-thread` resume coordinate), transport frames, shared inbound\n * commands, and `bridge-ready` line all come from the shared `@ai-sdk/harness`\n * protocol — the only Codex-specific piece is the `start` payload.\n */\n\nexport const outboundMessageSchema = harnessV1BridgeOutboundMessageSchema;\nexport type OutboundMessage = z.infer<typeof outboundMessageSchema>;\n\nexport const startMessageSchema = harnessV1BridgeStartBaseSchema.extend({\n instructions: z.string().optional(),\n reasoningEffort: z.enum(['low', 'medium', 'high']).optional(),\n webSearch: z.boolean().optional(),\n // Resume signal. When supplied, the bridge calls\n // `codex.resumeThread(resumeThreadId, …)` instead of starting a fresh thread.\n // The host sources the id from lifecycle state `data` cached from a prior\n // `agent.detach`.\n resumeThreadId: z.string().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 { createCodex } from './codex-harness';\n\n/**\n * Default `codex` harness instance with no overrides — suitable for the\n * common case where the underlying `codex` CLI's defaults are fine.\n * Equivalent to `createCodex()`.\n */\nexport const codex = createCodex();\n\nexport { createCodex } from './codex-harness';\nexport type { CodexHarnessSettings } from './codex-harness';\nexport type { CodexAuthOptions } from './codex-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;AAKP,SAAS,iBAAiB;AAC1B,SAAS,KAAAA,UAAS;;;ACLX,SAAS,gBACd,MACA,aAAiD,QAAQ,KACjC;AACxB,MAAI,MAAM,kBAAkB;AAC1B,WAAO,qBAAqB,KAAK,kBAAkB,UAAU;AAAA,EAC/D;AACA,MAAI,MAAM,QAAQ;AAChB,WAAO,WAAW,EAAE,UAAU,KAAK,QAAQ,WAAW,CAAC;AAAA,EACzD;AACA,MAAI,MAAM,SAAS;AACjB,WAAO,YAAY,KAAK,SAAS,UAAU;AAAA,EAC7C;AACA,MAAI,WAAW,oBAAoB;AACjC,WAAO,YAAY,CAAC,GAAG,UAAU;AAAA,EACnC;AACA,SAAO,WAAW,EAAE,WAAW,CAAC;AAClC;AAEA,SAAS,qBACP,UACA,YACwB;AACxB,QAAM,MAA8B,CAAC;AACrC,QAAM,SACJ,SAAS,UAAU,WAAW,kBAAkB,WAAW;AAC7D,MAAI,OAAQ,KAAI,gBAAgB;AAChC,MAAI,SAAS,QAAS,KAAI,kBAAkB,SAAS;AACrD,MAAI,SAAS;AACX,QAAI,4BAA4B,SAAS;AAC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Ae,IAAI,CAAC;AAAA,UACrD,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,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,sEAAsE,KAAK,IAAI;AAAA,MAC1F,CAAC;AAAA,IACH;AACA,UAAM,KAAK,KAAK,IAAI;AAAA,EACtB;AACA,SAAO,MAAM,KAAK,MAAM;AAC1B;;;AGlkCO,IAAM,QAAQ,YAAY;","names":["z","z"]}
1
+ {"version":3,"sources":["../src/codex-harness.ts","../src/codex-auth.ts","../src/codex-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 HarnessCapabilityUnsupportedError,\n harnessV1DiagnosticFromBridgeFrame,\n type HarnessV1,\n type HarnessV1Bootstrap,\n type HarnessV1DebugConfig,\n type HarnessV1BuiltinTool,\n type HarnessV1ContinueTurnState,\n type HarnessV1Prompt,\n type HarnessV1PromptControl,\n type HarnessV1ResumeSessionState,\n type HarnessV1NetworkSandboxSession,\n type HarnessV1PermissionMode,\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 type Experimental_SandboxProcess,\n type Experimental_SandboxSession,\n} from '@ai-sdk/provider-utils';\nimport { WebSocket } from 'ws';\nimport { z } from 'zod';\nimport { resolveCodexEnv, type CodexAuthOptions } from './codex-auth';\nimport {\n outboundMessageSchema,\n type InboundMessage,\n type OutboundMessage,\n} from './codex-bridge-protocol';\n\ntype CodexChannel = SandboxChannel<OutboundMessage, InboundMessage>;\ntype CodexRespawnStrategy = 'replay' | 'rerun';\n\ntype WriteSkillsResult = {\n readonly homeDir: string;\n readonly codexHomeDir: string;\n};\n\n/*\n * The model the adapter pins when the consumer configures none. The Codex SDK\n * does not report the model it resolves to at runtime (no model field on any\n * event), and exposes no default-model constant, so we pin the latest\n * codex-specialized model available for the bundled `@openai/codex@0.130.0`\n * (published 2026-05-08): `gpt-5.3-codex` (released 2026-02). Keep this in sync\n * when bumping the codex SDK/binary. Passing it explicitly makes the resolved\n * model deterministic and the telemetry (`gen_ai.request.model`) accurate.\n */\nconst DEFAULT_CODEX_MODEL = 'gpt-5.3-codex';\n\nexport type CodexHarnessSettings = {\n readonly auth?: CodexAuthOptions;\n /**\n * OpenAI model id the underlying `codex` CLI should use. Leaving this unset\n * pins the adapter default (`DEFAULT_CODEX_MODEL`).\n */\n readonly model?: string;\n /**\n * Reasoning effort for reasoning-capable models. Leaving this unset\n * defers to the CLI's default.\n */\n readonly reasoningEffort?: 'low' | 'medium' | 'high';\n /**\n * When `true`, allow the underlying runtime to use live web search.\n */\n readonly webSearch?: boolean;\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 Codex CLI can invoke as a model-callable tool,\n * declared as a `ToolSet` keyed by what the bridge emits as `toolName` on\n * the wire (`commonName ?? nativeName`). Schemas reflect the `ThreadItem`\n * union in `@openai/codex-sdk`'s `dist/index.d.ts`.\n *\n * Codex's other native operations (`apply_patch`, todo planning) surface\n * only as side-effect events (`file_change`, `todo_list`) and are not\n * model-callable tools — they don't appear here.\n */\nconst CODEX_BUILTIN_TOOLS = {\n bash: commonTool('bash', {\n nativeName: 'shell',\n toolUseKind: 'bash',\n description: 'Execute a shell command',\n inputSchema: z.object({ command: z.string() }),\n }),\n webSearch: commonTool('webSearch', {\n nativeName: 'web_search',\n toolUseKind: 'readonly',\n description: 'Search the web',\n inputSchema: z.object({ query: z.string() }),\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 contents (the codex CLI shim and any files the agent edits) and\n * the bridge state files survive both detach -> attach and\n * stop -> snapshot -> resume cycles.\n */\nconst BOOTSTRAP_DIR = '/tmp/harness/codex';\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 codexBridgeCoordsSchema = 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 lifecycle `data` payload Codex produces.\n * `threadId` is what `codex.resumeThread(...)` requires for the replay/rerun\n * rungs; the sandbox lookup is handled separately via\n * `provider.resumeSession({ sessionId })`. `bridge` carries live coordinates\n * for cross-process `attach` (present on `doDetach()` and `doSuspendTurn()`\n * payloads).\n */\nconst codexResumeStateSchema = z.object({\n threadId: z.string().optional(),\n bridge: codexBridgeCoordsSchema.optional(),\n});\n\ntype CodexBridgeCoords = z.infer<typeof codexBridgeCoordsSchema>;\n\nexport function createCodex(\n settings: CodexHarnessSettings = {},\n): HarnessV1<typeof CODEX_BUILTIN_TOOLS> {\n let cachedBootstrap: HarnessV1Bootstrap | undefined;\n\n return {\n specificationVersion: 'harness-v1',\n harnessId: 'codex',\n builtinTools: CODEX_BUILTIN_TOOLS,\n supportsBuiltinToolApprovals: false,\n lifecycleStateSchema: codexResumeStateSchema,\n getBootstrap: async () => {\n if (cachedBootstrap != null) return cachedBootstrap;\n const [pkg, lock, bridge, hostToolMcp] = await Promise.all([\n readBridgeAsset('package.json'),\n readBridgeAsset('pnpm-lock.yaml'),\n readBridgeAsset('index.mjs'),\n readBridgeAsset('host-tool-mcp.mjs'),\n ]);\n cachedBootstrap = {\n harnessId: 'codex',\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 path: `${BOOTSTRAP_DIR}/host-tool-mcp.mjs`,\n content: hostToolMcp,\n },\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 };\n return cachedBootstrap;\n },\n doStart: async startOpts => {\n if (\n startOpts.permissionMode != null &&\n startOpts.permissionMode !== 'allow-all'\n ) {\n throw new HarnessCapabilityUnsupportedError({\n message:\n \"Harness 'codex' does not support built-in tool approval requests; use permissionMode: 'allow-all'.\",\n harnessId: 'codex',\n });\n }\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 resumeData =\n isResume && typeof lifecycleState?.data === 'object'\n ? (lifecycleState.data as {\n threadId?: unknown;\n bridge?: CodexBridgeCoords;\n })\n : undefined;\n const resumeThreadId = resumeData?.threadId;\n const resumeThreadIdString =\n typeof resumeThreadId === 'string' && resumeThreadId.length > 0\n ? resumeThreadId\n : undefined;\n const coords = resumeData?.bridge;\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 /*\n * Rung 1 — ATTACH. With live coordinates, reopen a socket to the\n * still-running bridge. Parked between-turn sessions just attach and wait\n * for the next `start`; suspended in-flight turns request replay of\n * everything past the persisted cursor. No spawn, no fresh token. If the\n * bridge is gone the open throws and we fall through to a spawn-based\n * 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: CodexChannel = new SandboxChannel({\n connect: () => openWebSocket(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; no process handle.\n proc: undefined,\n model: settings.model ?? DEFAULT_CODEX_MODEL,\n reasoningEffort: settings.reasoningEffort,\n webSearch: settings.webSearch,\n resumeThreadId: resumeThreadIdString,\n isResume: true,\n seedResumeThreadOnFirstPrompt: false,\n rerunContinue: false,\n bridgePort: coords.port,\n bridgeToken: coords.token,\n sandboxId,\n debug: startOpts.observability?.debug,\n permissionMode: startOpts.permissionMode,\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 * via `codex.resumeThread(threadId)` when attach is unavailable.\n */\n let respawnStrategy: CodexRespawnStrategy | 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 port = resolveBridgePort(sandboxSession, settings.port);\n const token = randomBytes(32).toString('hex');\n const codexSkillSetup =\n startOpts.skills && startOpts.skills.length > 0\n ? await writeSkills({\n sandbox: session,\n skills: startOpts.skills,\n abortSignal: startOpts.abortSignal,\n })\n : undefined;\n const env = {\n ...resolveCodexEnv(settings.auth),\n BRIDGE_CHANNEL_TOKEN: token,\n BRIDGE_WS_PORT: String(port),\n ...(codexSkillSetup\n ? {\n HOME: codexSkillSetup.homeDir,\n CODEX_HOME: codexSkillSetup.codexHomeDir,\n }\n : {}),\n ...(respawnStrategy === 'replay'\n ? { BRIDGE_REPLAY_FROM_DISK: '1' }\n : {}),\n };\n\n if (respawnStrategy === undefined) {\n await session.run({\n command: `mkdir -p ${workDir} ${bridgeStateDir}`,\n abortSignal: startOpts.abortSignal,\n });\n }\n\n await markBridgeStarting({\n sandbox: session,\n bridgeStateDir,\n bridgeType: 'codex',\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} --bootstrap-dir ${BOOTSTRAP_DIR}`,\n env,\n abortSignal: startOpts.abortSignal,\n });\n\n const { port: boundPort } = await waitForBridgeReady({\n proc,\n sandbox: session,\n bridgeStateDir,\n bridgeType: 'codex',\n timeoutMs,\n abortSignal: startOpts.abortSignal,\n createTimeoutError: () =>\n new Error('codex bridge did not become ready in time.'),\n createExitError: () =>\n new Error('codex bridge exited before becoming ready.'),\n });\n void drainRest(proc.stdout);\n /*\n * Bridge stderr is the only diagnostic channel for what happens\n * inside the sandbox once the bridge is running (uncaught\n * exceptions, Codex SDK errors, network failures). Forward it\n * line-by-line to the host console so a mid-turn bridge crash can\n * be inspected from `pnpm dev` logs without redeploying. The\n * bridge itself writes nothing to stderr in steady state, so this\n * is silent on the happy path.\n */\n void forwardBridgeStderr(proc.stderr);\n\n const wsUrl =\n (await sandboxSession.getPortUrl({\n port: boundPort,\n protocol: 'ws',\n })) + `?agent_bridge_token=${encodeURIComponent(token)}`;\n\n const channel: CodexChannel = new SandboxChannel({\n connect: () => openWebSocket(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`).\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 ?? DEFAULT_CODEX_MODEL,\n reasoningEffort: settings.reasoningEffort,\n webSearch: settings.webSearch,\n resumeThreadId: resumeThreadIdString,\n isResume: respawnStrategy !== undefined,\n seedResumeThreadOnFirstPrompt: respawnStrategy !== undefined,\n rerunContinue: respawnStrategy === 'rerun',\n bridgePort: boundPort,\n bridgeToken: token,\n sandboxId,\n debug: startOpts.observability?.debug,\n permissionMode: startOpts.permissionMode,\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: 'codex',\n message:\n 'The codex harness needs a TCP port exposed by the sandbox. ' +\n 'Create the sandbox with `ports: [<port>]` or pass `createCodex({ port })`.',\n });\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 writeSkills({\n sandbox,\n skills,\n abortSignal,\n}: {\n sandbox: Experimental_SandboxSession;\n skills: ReadonlyArray<HarnessV1Skill>;\n abortSignal?: AbortSignal;\n}): Promise<WriteSkillsResult> {\n for (const skill of skills) {\n safeCodexSkillName(skill.name);\n for (const file of skill.files ?? []) {\n safeCodexSkillFilePath({\n skillName: skill.name,\n filePath: file.path,\n });\n }\n }\n\n const homeDir = await resolveSandboxHomeDir({ sandbox, abortSignal });\n const codexHomeDir = path.posix.join(homeDir, '.codex');\n await sandbox.run({\n command: `mkdir -p ${shellQuote(codexHomeDir)}`,\n abortSignal,\n });\n\n const rootDir = path.posix.join(homeDir, '.agents', 'skills');\n await sandbox.run({\n command: `mkdir -p ${shellQuote(rootDir)}`,\n abortSignal,\n });\n\n for (const skill of skills) {\n const name = safeCodexSkillName(skill.name);\n const skillDir = path.posix.join(rootDir, name);\n const content = `---\\nname: ${skill.name}\\ndescription: ${skill.description}\\n---\\n\\n${skill.content}`;\n\n await sandbox.writeTextFile({\n path: path.posix.join(skillDir, 'SKILL.md'),\n content,\n abortSignal,\n });\n\n for (const file of skill.files ?? []) {\n const filePath = safeCodexSkillFilePath({\n skillName: skill.name,\n filePath: file.path,\n });\n await sandbox.writeTextFile({\n path: path.posix.join(skillDir, filePath),\n content: file.content,\n abortSignal,\n });\n }\n }\n\n return {\n homeDir,\n codexHomeDir,\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 safeCodexSkillName(name: string): string {\n if (!/^[A-Za-z0-9._-]+$/.test(name) || name === '.' || name === '..') {\n throw new Error(`Invalid Codex skill name: ${name}`);\n }\n return name;\n}\n\nfunction safeCodexSkillFilePath({\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 Codex 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 forwardBridgeStderr(\n stream: ReadableStream<Uint8Array>,\n): Promise<void> {\n try {\n const reader = stream.pipeThrough(new TextDecoderStream()).getReader();\n while (true) {\n const { value, done } = await reader.read();\n if (done) return;\n if (value) {\n const trimmed = value.endsWith('\\n') ? value.slice(0, -1) : value;\n if (trimmed.length > 0) {\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\nfunction openWebSocket(url: string): Promise<WebSocket> {\n return new Promise((resolve, reject) => {\n const ws = new WebSocket(url);\n const onOpen = () => {\n ws.off('error', onError);\n resolve(ws);\n };\n const onError = (err: Error) => {\n ws.off('open', onOpen);\n reject(err);\n };\n ws.once('open', onOpen);\n ws.once('error', onError);\n });\n}\n\nfunction createSession({\n sessionId,\n channel,\n proc,\n model,\n reasoningEffort,\n webSearch,\n resumeThreadId,\n isResume,\n seedResumeThreadOnFirstPrompt,\n rerunContinue,\n bridgePort,\n bridgeToken,\n sandboxId,\n debug,\n permissionMode,\n}: {\n sessionId: string;\n channel: CodexChannel;\n /** Undefined on `attach` — the live bridge was spawned by another process. */\n proc: Experimental_SandboxProcess | undefined;\n model: string | undefined;\n reasoningEffort: 'low' | 'medium' | 'high' | undefined;\n webSearch: boolean | undefined;\n resumeThreadId: string | undefined;\n isResume: boolean;\n seedResumeThreadOnFirstPrompt: boolean;\n rerunContinue: boolean;\n bridgePort: number;\n bridgeToken: string;\n sandboxId: string;\n debug: HarnessV1DebugConfig | undefined;\n permissionMode: HarnessV1PermissionMode | undefined;\n}): HarnessV1Session {\n let stopped = false;\n let stopPromise: Promise<void> | undefined;\n /*\n * Send the persisted threadId on the first prompt only when the bridge was\n * respawned (rerun/replay) so it takes the `codex.resumeThread(...)` branch.\n * An `attach`ed bridge already holds its threadState in memory and continues\n * on its own, so it needs no seed.\n */\n let pendingResumeThreadId = seedResumeThreadOnFirstPrompt\n ? resumeThreadId\n : undefined;\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 persisted thread), so it starts\n * \"applied\".\n */\n let instructionsApplied = isResume;\n\n /*\n * Latest codex thread id, cached from the bridge's `bridge-thread`\n * announcements. Seeded from lifecycle state so `doDetach()` and `doStop()`\n * can include a thread id even before this process has run a turn.\n */\n let latestThreadId = resumeThreadId;\n channel.on('bridge-thread', msg => {\n latestThreadId = msg.threadId;\n });\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 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 'file-change',\n 'finish-step',\n 'raw',\n ] as const;\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 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(new Error('codex bridge closed before the turn finished.'));\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 const applyInstructions =\n !instructionsApplied && !!promptOpts.instructions;\n instructionsApplied = true;\n\n const startMessage = {\n type: 'start' as const,\n prompt: extractUserText(promptOpts.prompt),\n ...(applyInstructions ? { instructions: promptOpts.instructions } : {}),\n tools: (promptOpts.tools ?? []).map(t => ({\n name: t.name,\n description: t.description,\n inputSchema: t.inputSchema,\n })),\n model,\n reasoningEffort,\n webSearch,\n ...(permissionMode ? { permissionMode } : {}),\n ...(pendingResumeThreadId\n ? { resumeThreadId: pendingResumeThreadId }\n : {}),\n ...(debug ? { debug } : {}),\n };\n pendingResumeThreadId = undefined;\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 codex's own thread via `resumeThreadId`. Lossy — work in flight\n * at the interruption is recomputed. This is the rare bridge-died\n * fallback; the common slice path is `attach`.\n */\n if (rerunContinue) {\n const threadId = pendingResumeThreadId ?? latestThreadId;\n pendingResumeThreadId = undefined;\n channel.send({\n type: 'start' as const,\n /*\n * A continuation nudge rather than an empty prompt: `resumeThreadId`\n * rehydrates the prior thread, and this is the new user turn that\n * drives it forward. Keeping it non-empty avoids handing the runtime\n * an empty user message (and mirrors the claude-code adapter, where an\n * empty text block trips the Anthropic API's `cache_control` rule).\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 reasoningEffort,\n webSearch,\n ...(permissionMode ? { permissionMode } : {}),\n ...(threadId ? { resumeThreadId: threadId } : {}),\n ...(debug ? { debug } : {}),\n });\n }\n\n return control;\n },\n doCompact: async () => {\n /*\n * Codex compacts its context automatically inside the core turn loop\n * (~90% of the model context window), but the `codex exec` transport this\n * adapter drives exposes no manual compaction trigger and emits no\n * compaction event. Manual `compact()` is therefore unsupported; Codex's\n * own auto-compaction continues to run regardless.\n */\n throw new HarnessCapabilityUnsupportedError({\n message:\n \"Harness 'codex' does not support manual compaction; Codex auto-compacts its context internally.\",\n harnessId: 'codex',\n });\n },\n doDetach: async () => {\n if (stopped) {\n throw new Error(\n `codex 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: 'codex',\n specificationVersion: 'harness-v1',\n data: {\n ...(latestThreadId ? { threadId: latestThreadId } : {}),\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 `codex 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 — the workdir is still captured by the sandbox snapshot\n * during the subsequent `sandboxSession.stop()`, so the next turn can\n * resume the filesystem state. The trade-off: we lose\n * `threadId`, so the codex CLI starts a fresh thread on the\n * preserved workdir rather than resuming the prior conversation\n * inside Codex's runtime. Ability to continue beats throwing.\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 `codex 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 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: 'codex',\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 `codex 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: 'codex',\n specificationVersion: 'harness-v1',\n data: {\n ...(latestThreadId ? { threadId: latestThreadId } : {}),\n bridge: {\n port: bridgePort,\n token: bridgeToken,\n lastSeenEventId,\n sandboxId,\n },\n },\n };\n return payload;\n },\n };\n}\n\n/*\n * Reduce a `HarnessV1Prompt` to the plain user text the bridge forwards\n * to the Codex 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: 'codex',\n message: `The codex 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 { getAiGatewayAuthFromEnv } from '@ai-sdk/harness/utils';\n\nexport type CodexAuthOptions = {\n readonly openaiCompatible?: {\n readonly apiKey?: string;\n readonly baseUrl?: string;\n readonly modelProviderName?: string;\n readonly queryParamsJson?: string;\n };\n readonly openai?: {\n readonly apiKey?: string;\n readonly baseUrl?: string;\n readonly organization?: string;\n readonly project?: string;\n };\n readonly gateway?: {\n readonly apiKey?: string;\n readonly baseUrl?: string;\n };\n};\n\n/**\n * Resolve the environment-variable blob the codex bridge needs. Precedence:\n *\n * 1. Explicit `auth.openaiCompatible` — pin to a custom OpenAI-compatible endpoint.\n * 2. Explicit `auth.openai` — pin to direct OpenAI auth.\n * 3. Explicit `auth.gateway` — pin to Vercel AI Gateway.\n * 4. Auto-detect from the host process env: gateway first\n * (`AI_GATEWAY_API_KEY` / `VERCEL_OIDC_TOKEN`), then `CODEX_API_KEY` /\n * `OPENAI_API_KEY`.\n */\nexport function resolveCodexEnv(\n auth: CodexAuthOptions | undefined,\n processEnv: Record<string, string | undefined> = process.env,\n): Record<string, string> {\n if (auth?.openaiCompatible) {\n return pickOpenAICompatible(auth.openaiCompatible, processEnv);\n }\n if (auth?.openai) {\n return pickOpenAI({ explicit: auth.openai, processEnv });\n }\n const gatewayAuthFromEnv = getAiGatewayAuthFromEnv({\n env: processEnv,\n });\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 return pickOpenAI({ processEnv });\n}\n\nfunction pickOpenAICompatible(\n explicit: NonNullable<CodexAuthOptions['openaiCompatible']>,\n processEnv: Record<string, string | undefined>,\n): Record<string, string> {\n const env: Record<string, string> = {};\n const apiKey =\n explicit.apiKey ?? processEnv.OPENAI_API_KEY ?? processEnv.CODEX_API_KEY;\n if (apiKey) env.CODEX_API_KEY = apiKey;\n if (explicit.baseUrl) env.OPENAI_BASE_URL = explicit.baseUrl;\n if (explicit.modelProviderName)\n env.CODEX_MODEL_PROVIDER_NAME = explicit.modelProviderName;\n if (explicit.queryParamsJson)\n env.OPENAI_QUERY_PARAMS_JSON = explicit.queryParamsJson;\n return env;\n}\n\nfunction pickOpenAI({\n explicit,\n processEnv,\n}: {\n explicit?: NonNullable<CodexAuthOptions['openai']>;\n processEnv: Record<string, string | undefined>;\n}): Record<string, string> {\n const env: Record<string, string> = {};\n const apiKey =\n explicit?.apiKey ?? processEnv.OPENAI_API_KEY ?? processEnv.CODEX_API_KEY;\n if (apiKey) env.CODEX_API_KEY = apiKey;\n const baseUrl = explicit?.baseUrl ?? processEnv.OPENAI_BASE_URL;\n if (baseUrl) env.OPENAI_BASE_URL = baseUrl;\n const organization = explicit?.organization ?? processEnv.OPENAI_ORGANIZATION;\n if (organization) env.OPENAI_ORGANIZATION = organization;\n const project = explicit?.project ?? processEnv.OPENAI_PROJECT;\n if (project) env.OPENAI_PROJECT = project;\n return env;\n}\n\nfunction pickGateway({\n explicit,\n gatewayAuthFromEnv,\n}: {\n explicit: NonNullable<CodexAuthOptions['gateway']>;\n gatewayAuthFromEnv: ReturnType<typeof getAiGatewayAuthFromEnv>;\n}): Record<string, string> {\n const apiKey = explicit.apiKey ?? gatewayAuthFromEnv.apiKey;\n const baseUrl = toCodexGatewayBaseUrl(\n explicit.baseUrl ?? gatewayAuthFromEnv.baseUrl,\n );\n const env: Record<string, string> = {};\n if (apiKey) {\n env.AI_GATEWAY_API_KEY = apiKey;\n env.CODEX_API_KEY = apiKey;\n }\n env.AI_GATEWAY_BASE_URL = baseUrl;\n return env;\n}\n\nfunction toCodexGatewayBaseUrl(baseUrl: string): string {\n const trimmed = baseUrl.replace(/\\/+$/, '');\n return trimmed.endsWith('/v1') ? trimmed : `${trimmed}/v1`;\n}\n","import {\n harnessV1BridgeInboundCommandSchemas,\n harnessV1BridgeOutboundMessageSchema,\n harnessV1BridgeReadySchema,\n harnessV1BridgeStartBaseSchema,\n} from '@ai-sdk/harness';\nimport { z } from 'zod/v4';\n\n/*\n * Codex's bridge wire protocol. The outbound events (including `file-change`\n * and the `bridge-thread` resume coordinate), transport frames, shared inbound\n * commands, and `bridge-ready` line all come from the shared `@ai-sdk/harness`\n * protocol — the only Codex-specific piece is the `start` payload.\n */\n\nexport const outboundMessageSchema = harnessV1BridgeOutboundMessageSchema;\nexport type OutboundMessage = z.infer<typeof outboundMessageSchema>;\n\nexport const startMessageSchema = harnessV1BridgeStartBaseSchema.extend({\n instructions: z.string().optional(),\n reasoningEffort: z.enum(['low', 'medium', 'high']).optional(),\n webSearch: z.boolean().optional(),\n // Resume signal. When supplied, the bridge calls\n // `codex.resumeThread(resumeThreadId, …)` instead of starting a fresh thread.\n // The host sources the id from lifecycle state `data` cached from a prior\n // `agent.detach`.\n resumeThreadId: z.string().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 { createCodex } from './codex-harness';\n\n/**\n * Default `codex` harness instance with no overrides — suitable for the\n * common case where the underlying `codex` CLI's defaults are fine.\n * Equivalent to `createCodex()`.\n */\nexport const codex = createCodex();\n\nexport { createCodex } from './codex-harness';\nexport type { CodexHarnessSettings } from './codex-harness';\nexport type { CodexAuthOptions } from './codex-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,OAC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,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,sEAAsE,KAAK,IAAI;AAAA,MAC1F,CAAC;AAAA,IACH;AACA,UAAM,KAAK,KAAK,IAAI;AAAA,EACtB;AACA,SAAO,MAAM,KAAK,MAAM;AAC1B;;;AGlkCO,IAAM,QAAQ,YAAY;","names":["z","z"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ai-sdk/harness-codex",
3
- "version": "1.0.0-canary.9",
3
+ "version": "1.0.0",
4
4
  "type": "module",
5
5
  "license": "Apache-2.0",
6
6
  "sideEffects": false,
@@ -26,10 +26,10 @@
26
26
  }
27
27
  },
28
28
  "dependencies": {
29
- "ws": "^8.20.1",
29
+ "ws": "^8.21.0",
30
30
  "zod": "3.25.76",
31
- "@ai-sdk/harness": "1.0.0-canary.13",
32
- "@ai-sdk/provider-utils": "5.0.0-canary.48"
31
+ "@ai-sdk/provider-utils": "5.0.0",
32
+ "@ai-sdk/harness": "1.0.0"
33
33
  },
34
34
  "devDependencies": {
35
35
  "@modelcontextprotocol/sdk": "1.29.0",
@@ -158,9 +158,14 @@ async function runTurn(start: StartMessage, turn: BridgeTurn): Promise<void> {
158
158
  const codexConfig: Record<string, unknown> = {};
159
159
  if (Object.keys(mcpServers).length > 0) codexConfig.mcp_servers = mcpServers;
160
160
 
161
- const apiBaseUrl = procEnv.AI_GATEWAY_API_KEY
162
- ? procEnv.AI_GATEWAY_BASE_URL || 'https://ai-gateway.vercel.sh/v1'
163
- : procEnv.OPENAI_BASE_URL;
161
+ const gatewayBaseUrl = procEnv.AI_GATEWAY_BASE_URL;
162
+ const hasGatewayAuth = Boolean(procEnv.AI_GATEWAY_API_KEY || gatewayBaseUrl);
163
+ if (hasGatewayAuth && !gatewayBaseUrl) {
164
+ throw new Error(
165
+ 'AI Gateway auth was selected but AI_GATEWAY_BASE_URL is missing from the Codex bridge environment.',
166
+ );
167
+ }
168
+ const apiBaseUrl = hasGatewayAuth ? gatewayBaseUrl : procEnv.OPENAI_BASE_URL;
164
169
  if (apiBaseUrl) {
165
170
  codexConfig.preferred_auth_method = 'apikey';
166
171
  codexConfig.model_provider = 'agent_bridge_openai';
@@ -6,7 +6,7 @@
6
6
  "dependencies": {
7
7
  "@openai/codex-sdk": "0.130.0",
8
8
  "@modelcontextprotocol/sdk": "1.29.0",
9
- "ws": "8.20.1",
9
+ "ws": "8.21.0",
10
10
  "zod": "3.25.76"
11
11
  }
12
12
  }
@@ -15,8 +15,8 @@ importers:
15
15
  specifier: 0.130.0
16
16
  version: 0.130.0
17
17
  ws:
18
- specifier: 8.20.1
19
- version: 8.20.1
18
+ specifier: 8.21.0
19
+ version: 8.21.0
20
20
  zod:
21
21
  specifier: 3.25.76
22
22
  version: 3.25.76
@@ -430,8 +430,8 @@ packages:
430
430
  wrappy@1.0.2:
431
431
  resolution: {integrity: sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ==}
432
432
 
433
- ws@8.20.1:
434
- resolution: {integrity: sha512-It4dO0K5v//JtTXuPkfEOaI3uUN87iYPnqo/ZzqCoG3g8uhA66QUMs/SrM0YK7/NAu+r4LMh/9dq2A7k+rHs+w==}
433
+ ws@8.21.0:
434
+ resolution: {integrity: sha512-Vsp28b7DRcimFQvrqu2Wek3z1iYxDCWqHYB8Qsnk/S4RfaCQzPGPyBNuVjJV3cd6UiKtUtp6sNM77gWvzcCH+g==}
435
435
  engines: {node: '>=10.0.0'}
436
436
  peerDependencies:
437
437
  bufferutil: ^4.0.1
@@ -870,7 +870,7 @@ snapshots:
870
870
 
871
871
  wrappy@1.0.2: {}
872
872
 
873
- ws@8.20.1: {}
873
+ ws@8.21.0: {}
874
874
 
875
875
  zod-to-json-schema@3.25.2(zod@3.25.76):
876
876
  dependencies:
package/src/codex-auth.ts CHANGED
@@ -1,3 +1,5 @@
1
+ import { getAiGatewayAuthFromEnv } from '@ai-sdk/harness/utils';
2
+
1
3
  export type CodexAuthOptions = {
2
4
  readonly openaiCompatible?: {
3
5
  readonly apiKey?: string;
@@ -24,7 +26,8 @@ export type CodexAuthOptions = {
24
26
  * 2. Explicit `auth.openai` — pin to direct OpenAI auth.
25
27
  * 3. Explicit `auth.gateway` — pin to Vercel AI Gateway.
26
28
  * 4. Auto-detect from the host process env: gateway first
27
- * (`AI_GATEWAY_API_KEY`), then `CODEX_API_KEY` / `OPENAI_API_KEY`.
29
+ * (`AI_GATEWAY_API_KEY` / `VERCEL_OIDC_TOKEN`), then `CODEX_API_KEY` /
30
+ * `OPENAI_API_KEY`.
28
31
  */
29
32
  export function resolveCodexEnv(
30
33
  auth: CodexAuthOptions | undefined,
@@ -36,11 +39,20 @@ export function resolveCodexEnv(
36
39
  if (auth?.openai) {
37
40
  return pickOpenAI({ explicit: auth.openai, processEnv });
38
41
  }
42
+ const gatewayAuthFromEnv = getAiGatewayAuthFromEnv({
43
+ env: processEnv,
44
+ });
39
45
  if (auth?.gateway) {
40
- return pickGateway(auth.gateway, processEnv);
46
+ return pickGateway({
47
+ explicit: auth.gateway,
48
+ gatewayAuthFromEnv,
49
+ });
41
50
  }
42
- if (processEnv.AI_GATEWAY_API_KEY) {
43
- return pickGateway({}, processEnv);
51
+ if (gatewayAuthFromEnv.apiKey) {
52
+ return pickGateway({
53
+ explicit: {},
54
+ gatewayAuthFromEnv,
55
+ });
44
56
  }
45
57
  return pickOpenAI({ processEnv });
46
58
  }
@@ -81,15 +93,17 @@ function pickOpenAI({
81
93
  return env;
82
94
  }
83
95
 
84
- function pickGateway(
85
- explicit: NonNullable<CodexAuthOptions['gateway']>,
86
- processEnv: Record<string, string | undefined>,
87
- ): Record<string, string> {
88
- const apiKey = explicit.apiKey ?? processEnv.AI_GATEWAY_API_KEY;
89
- const baseUrl =
90
- explicit.baseUrl ??
91
- processEnv.AI_GATEWAY_BASE_URL ??
92
- 'https://ai-gateway.vercel.sh/v1';
96
+ function pickGateway({
97
+ explicit,
98
+ gatewayAuthFromEnv,
99
+ }: {
100
+ explicit: NonNullable<CodexAuthOptions['gateway']>;
101
+ gatewayAuthFromEnv: ReturnType<typeof getAiGatewayAuthFromEnv>;
102
+ }): Record<string, string> {
103
+ const apiKey = explicit.apiKey ?? gatewayAuthFromEnv.apiKey;
104
+ const baseUrl = toCodexGatewayBaseUrl(
105
+ explicit.baseUrl ?? gatewayAuthFromEnv.baseUrl,
106
+ );
93
107
  const env: Record<string, string> = {};
94
108
  if (apiKey) {
95
109
  env.AI_GATEWAY_API_KEY = apiKey;
@@ -98,3 +112,8 @@ function pickGateway(
98
112
  env.AI_GATEWAY_BASE_URL = baseUrl;
99
113
  return env;
100
114
  }
115
+
116
+ function toCodexGatewayBaseUrl(baseUrl: string): string {
117
+ const trimmed = baseUrl.replace(/\/+$/, '');
118
+ return trimmed.endsWith('/v1') ? trimmed : `${trimmed}/v1`;
119
+ }
@@ -129,7 +129,7 @@ const BOOTSTRAP_DIR = '/tmp/harness/codex';
129
129
  * future process uses them to reopen a socket to the still-running bridge
130
130
  * (`attach`) instead of re-spawning it. Absent on a `doStop()` payload.
131
131
  */
132
- const bridgeCoordsSchema = z.object({
132
+ const codexBridgeCoordsSchema = z.object({
133
133
  port: z.number(),
134
134
  token: z.string(),
135
135
  lastSeenEventId: z.number(),
@@ -146,10 +146,10 @@ const bridgeCoordsSchema = z.object({
146
146
  */
147
147
  const codexResumeStateSchema = z.object({
148
148
  threadId: z.string().optional(),
149
- bridge: bridgeCoordsSchema.optional(),
149
+ bridge: codexBridgeCoordsSchema.optional(),
150
150
  });
151
151
 
152
- type CodexBridgeCoords = z.infer<typeof bridgeCoordsSchema>;
152
+ type CodexBridgeCoords = z.infer<typeof codexBridgeCoordsSchema>;
153
153
 
154
154
  export function createCodex(
155
155
  settings: CodexHarnessSettings = {},