-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Expand file tree
/
Copy pathcodex-app-server.ts
More file actions
393 lines (344 loc) · 13.2 KB
/
Copy pathcodex-app-server.ts
File metadata and controls
393 lines (344 loc) · 13.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/**
* Codex app-server JSON-RPC transport primitives.
*
* Communicates with `codex app-server` over stdio. This module is just the
* plumbing — spawn the process, send requests, dispatch responses and
* notifications. Higher-level semantics (threads, turns, event translation)
* live in codex.ts.
*
* Kept separate so the transport can be unit-tested without pulling in the
* full provider and so any future Codex tooling (e.g. a CLI for manual
* debugging) can reuse the same primitives.
*/
import fs from 'fs';
import path from 'path';
import { spawn, type ChildProcess } from 'child_process';
import { createInterface, type Interface as ReadlineInterface } from 'readline';
function log(msg: string): void {
console.error(`[codex-app-server] ${msg}`);
}
const INIT_TIMEOUT_MS = 30_000;
/**
* Errors from `thread/resume` that indicate the thread ID is unusable —
* typically because the app-server has no memory of it (thread transcript
* was deleted, server was wiped, ID is from a different codex version).
* Only errors matching this pattern trigger silent fallback to a fresh
* thread; everything else bubbles up so the caller can decide what to do.
*
* Shared with `codex.ts`'s `isSessionInvalid` to keep the two detection
* paths in sync.
*/
export const STALE_THREAD_RE = /thread\s+not\s+found|unknown\s+thread|thread[_\s]id|no such thread/i;
/**
* Escape a string for emission inside a TOML basic string (double-quoted).
* Handles `"` and `\`. Rejects newlines: basic strings can't contain raw
* newlines, and silently converting them to `\n` would mask misconfiguration
* (e.g. a secret pasted with a trailing newline). Multiline strings are
* unsupported for `config.toml` use here.
*/
export function tomlBasicString(value: string): string {
if (value.includes('\n') || value.includes('\r')) {
throw new Error(
`MCP config value contains newline (not supported in config.toml): ${JSON.stringify(value.slice(0, 40))}${value.length > 40 ? '…' : ''}`,
);
}
return `"${value.replace(/\\/g, '\\\\').replace(/"/g, '\\"')}"`;
}
// ── JSON-RPC types ──────────────────────────────────────────────────────────
let nextRequestId = 1;
interface JsonRpcRequest {
id: number;
method: string;
params: Record<string, unknown>;
}
export interface JsonRpcResponse {
id: number;
result?: unknown;
error?: { code: number; message: string; data?: unknown };
}
export interface JsonRpcNotification {
method: string;
params: Record<string, unknown>;
}
export interface JsonRpcServerRequest {
id: number;
method: string;
params: Record<string, unknown>;
}
type JsonRpcMessage = JsonRpcResponse | JsonRpcNotification | JsonRpcServerRequest;
function makeRequest(method: string, params: Record<string, unknown>): JsonRpcRequest {
return { id: nextRequestId++, method, params };
}
function isResponse(msg: JsonRpcMessage): msg is JsonRpcResponse {
return 'id' in msg && ('result' in msg || 'error' in msg) && !('method' in msg);
}
function isServerRequest(msg: JsonRpcMessage): msg is JsonRpcServerRequest {
return 'id' in msg && 'method' in msg;
}
// ── App-server handle ───────────────────────────────────────────────────────
export interface AppServer {
process: ChildProcess;
readline: ReadlineInterface;
pending: Map<number, { resolve: (r: JsonRpcResponse) => void; reject: (e: Error) => void }>;
notificationHandlers: ((n: JsonRpcNotification) => void)[];
serverRequestHandlers: ((r: JsonRpcServerRequest) => void)[];
}
export function spawnCodexAppServer(configOverrides: string[] = []): AppServer {
const args = ['app-server', '--listen', 'stdio://'];
for (const override of configOverrides) args.push('-c', override);
log(`Spawning: codex ${args.join(' ')}`);
const proc = spawn('codex', args, {
stdio: ['pipe', 'pipe', 'pipe'],
env: { ...process.env },
});
const rl = createInterface({ input: proc.stdout! });
const server: AppServer = {
process: proc,
readline: rl,
pending: new Map(),
notificationHandlers: [],
serverRequestHandlers: [],
};
proc.stderr?.on('data', (chunk: Buffer) => {
const text = chunk.toString().trim();
if (text) log(`[stderr] ${text}`);
});
rl.on('line', (line: string) => {
if (!line.trim()) return;
let msg: JsonRpcMessage;
try {
msg = JSON.parse(line);
} catch {
log(`[parse-error] ${line.slice(0, 200)}`);
return;
}
if (isResponse(msg)) {
const handler = server.pending.get(msg.id);
if (handler) {
server.pending.delete(msg.id);
handler.resolve(msg);
}
} else if (isServerRequest(msg)) {
for (const h of server.serverRequestHandlers) h(msg);
} else if ('method' in msg) {
for (const h of server.notificationHandlers) h(msg as JsonRpcNotification);
}
});
proc.on('error', (err) => {
log(`[process-error] ${err.message}`);
for (const [, handler] of server.pending) handler.reject(err);
server.pending.clear();
});
proc.on('exit', (code, signal) => {
log(`[exit] code=${code} signal=${signal}`);
const err = new Error(`Codex app-server exited: code=${code} signal=${signal}`);
for (const [, handler] of server.pending) handler.reject(err);
server.pending.clear();
});
return server;
}
export function sendCodexRequest(
server: AppServer,
method: string,
params: Record<string, unknown>,
timeoutMs = 60_000,
): Promise<JsonRpcResponse> {
const req = makeRequest(method, params);
const line = JSON.stringify(req) + '\n';
return new Promise<JsonRpcResponse>((resolve, reject) => {
const timer = setTimeout(() => {
server.pending.delete(req.id);
reject(new Error(`Timeout waiting for ${method} response (${timeoutMs}ms)`));
}, timeoutMs);
server.pending.set(req.id, {
resolve: (r) => {
clearTimeout(timer);
resolve(r);
},
reject: (e) => {
clearTimeout(timer);
reject(e);
},
});
try {
server.process.stdin!.write(line);
} catch (err) {
clearTimeout(timer);
server.pending.delete(req.id);
reject(err instanceof Error ? err : new Error(String(err)));
}
});
}
export function sendCodexResponse(server: AppServer, id: number, result: unknown): void {
const line = JSON.stringify({ id, result }) + '\n';
try {
server.process.stdin!.write(line);
} catch (err) {
log(`[send-error] Failed to send response for id=${id}: ${err instanceof Error ? err.message : String(err)}`);
}
}
export function killCodexAppServer(server: AppServer): void {
try {
server.readline.close();
server.process.kill('SIGTERM');
} catch {
/* ignore */
}
}
// ── Auto-approval ───────────────────────────────────────────────────────────
// The container sandbox is already the security boundary; inside it, Codex's
// own approval prompts would just block every tool call on a user that isn't
// watching. Accept everything and let sandbox limits do the enforcement.
export function attachCodexAutoApproval(server: AppServer): void {
server.serverRequestHandlers.push((req) => {
const method = req.method;
log(`[approval] ${method}`);
switch (method) {
case 'item/commandExecution/requestApproval':
case 'item/fileChange/requestApproval':
sendCodexResponse(server, req.id, { decision: 'accept' });
break;
case 'item/permissions/requestApproval':
sendCodexResponse(server, req.id, {
permissions: { fileSystem: { read: ['/'], write: ['/'] }, network: { enabled: true } },
scope: 'session',
});
break;
case 'applyPatchApproval':
case 'execCommandApproval':
sendCodexResponse(server, req.id, { decision: 'approved' });
break;
case 'item/tool/call': {
const toolName = (req.params as { tool?: string }).tool || 'unknown';
log(`[approval] Unexpected dynamic tool call: ${toolName}`);
sendCodexResponse(server, req.id, {
success: false,
contentItems: [{ type: 'inputText', text: `Tool "${toolName}" is not available. Use MCP tools instead.` }],
});
break;
}
case 'item/tool/requestUserInput':
case 'mcpServer/elicitation/request':
sendCodexResponse(server, req.id, { input: null });
break;
default:
log(`[approval] Unknown method ${method}, generic accept`);
sendCodexResponse(server, req.id, { decision: 'accept' });
break;
}
});
}
// ── High-level helpers ──────────────────────────────────────────────────────
export async function initializeCodexAppServer(server: AppServer): Promise<void> {
log('Sending initialize…');
const resp = await sendCodexRequest(
server,
'initialize',
{
clientInfo: { name: 'nanoclaw', version: '1.0.0' },
capabilities: { experimentalApi: false },
},
INIT_TIMEOUT_MS,
);
if (resp.error) throw new Error(`Initialize failed: ${resp.error.message}`);
log('Initialize successful');
}
export interface ThreadParams {
model?: string;
cwd: string;
sandbox?: string;
approvalPolicy?: string;
personality?: string;
baseInstructions?: string;
}
/**
* Start or resume a Codex thread. If `threadId` is provided, attempts
* `thread/resume` first and falls back to a fresh `thread/start` on failure
* (stale thread IDs commonly outlive containers). Returns the active thread
* ID either way.
*/
export async function startOrResumeCodexThread(
server: AppServer,
threadId: string | undefined,
params: ThreadParams,
): Promise<string> {
if (threadId) {
log(`Resuming thread: ${threadId}`);
const resp = await sendCodexRequest(server, 'thread/resume', {
threadId,
...(params as unknown as Record<string, unknown>),
});
if (!resp.error) {
log(`Thread resumed: ${threadId}`);
return threadId;
}
// Only fall through to fresh-thread on recognized stale-thread errors.
// Auth, version, or transient failures would otherwise silently discard
// session state — fail loud instead so the caller can retry or surface.
if (!STALE_THREAD_RE.test(resp.error.message)) {
throw new Error(`thread/resume failed: ${resp.error.message}`);
}
log(`Stale thread ${threadId}; starting fresh thread.`);
}
log('Starting new thread…');
const resp = await sendCodexRequest(server, 'thread/start', {
...(params as unknown as Record<string, unknown>),
});
if (resp.error) throw new Error(`thread/start failed: ${resp.error.message}`);
const result = resp.result as { thread?: { id?: string } } | undefined;
const newThreadId = result?.thread?.id;
if (!newThreadId) throw new Error('thread/start response missing thread ID');
log(`New thread: ${newThreadId}`);
return newThreadId;
}
export interface TurnParams {
threadId: string;
inputText: string;
model?: string;
cwd?: string;
}
export async function startCodexTurn(server: AppServer, params: TurnParams): Promise<void> {
const resp = await sendCodexRequest(server, 'turn/start', {
threadId: params.threadId,
input: [{ type: 'text', text: params.inputText }],
model: params.model,
cwd: params.cwd,
});
if (resp.error) throw new Error(`turn/start failed: ${resp.error.message}`);
}
// ── MCP config.toml ─────────────────────────────────────────────────────────
// Codex discovers MCP servers by reading ~/.codex/config.toml at startup.
// We rewrite it on every spawn from whatever mcpServers the agent-runner
// passes in, so the container's config reflects the current host wiring.
export interface CodexMcpServer {
command: string;
args?: string[];
env?: Record<string, string>;
}
export function writeCodexMcpConfigToml(servers: Record<string, CodexMcpServer>): void {
const codexConfigDir = path.join(process.env.HOME || '/home/node', '.codex');
fs.mkdirSync(codexConfigDir, { recursive: true });
const configTomlPath = path.join(codexConfigDir, 'config.toml');
const lines: string[] = [];
for (const [name, config] of Object.entries(servers)) {
lines.push(`[mcp_servers.${name}]`);
lines.push('type = "stdio"');
lines.push(`command = ${tomlBasicString(config.command)}`);
if (config.args && config.args.length > 0) {
const argsStr = config.args.map(tomlBasicString).join(', ');
lines.push(`args = [${argsStr}]`);
}
if (config.env && Object.keys(config.env).length > 0) {
lines.push(`[mcp_servers.${name}.env]`);
for (const [key, value] of Object.entries(config.env)) {
lines.push(`${key} = ${tomlBasicString(value)}`);
}
}
lines.push('');
}
fs.writeFileSync(configTomlPath, lines.join('\n'));
log(`Wrote MCP config.toml (${Object.keys(servers).length} server(s))`);
}
export function createCodexConfigOverrides(): string[] {
return ['features.use_linux_sandbox_bwrap=false'];
}