-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodex.ts
More file actions
311 lines (283 loc) · 10.5 KB
/
Copy pathcodex.ts
File metadata and controls
311 lines (283 loc) · 10.5 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
// src/executor/codex.ts — Codex CLI-subprocess executor + pure event mapper.
//
// Drives the installed `codex` CLI (v0.139.0) via `codex exec --json` and normalizes
// its line-delimited JSON (JSONL) ThreadEvent stream into the canonical ActionEvent
// union (src/shared/events.ts). The mapper is PURE so it can be unit-tested against a
// captured fixture without spawning a process.
//
// Spawn invariants (see BUILD_GUIDE Executor Adapter, steps 3 + gotchas):
// - stdin MUST be /dev/null (stdio[0]='ignore') or `codex exec` can block on a TTY.
// - Parse STDOUT ONLY as JSONL; stderr carries logs/skill-load errors/telemetry — drain it.
// - Tolerant parse: skip lines not starting with '{', wrap JSON.parse in try/catch.
// - Unknown event/item types -> map to null (skip), never throw.
import { spawn } from 'node:child_process';
import { createInterface } from 'node:readline';
import {
ActionEvent,
ExecutorRunOptions,
Executor,
newRunId,
} from '../shared/events';
// Absolute path to the installed codex binary. Packaged Electron strips PATH
// (/opt/homebrew/bin is gone), so resolve absolutely. Falls back to bare 'codex'
// for dev shells where PATH is intact.
const CODEX_BIN = process.env.COMPANION_CODEX_BIN ?? '/opt/homebrew/bin/codex';
/**
* Pure mapper: one Codex ThreadEvent object -> at most one canonical ActionEvent.
*
* Maps the CURRENT codex v0.139.0 JSONL shapes (verified live, see __fixtures__):
* thread.started{thread_id} -> run.started(threadId)
* turn.started -> turn.started
* item.started|completed / type=reasoning -> reasoning (only on completed; carries text)
* item.started|completed / type=command_execution -> command(status, command, output, exitCode)
* item.started|completed / type=file_change -> file_change(status, files[])
* item.started|completed / type=agent_message -> message (only on completed; final text)
* item.started|completed / type=mcp_tool_call -> tool(status, server, tool)
* item.started|completed / type=web_search -> tool(status, tool='web_search', summary=query)
* item.* / type=error -> run.failed
* turn.completed{usage} -> run.completed
* turn.failed{error} -> run.failed
* error{message} -> run.failed
* Anything else (todo_list, unknown item/event types) -> null (skip; forward-compat).
*
* Item status note (v0.139.0): item.started carries status:'in_progress'; item.completed
* carries 'completed' | 'failed'. We derive the canonical ActionEvent status from the
* envelope event type (started -> 'started') plus the item's own failure signal on completion.
*/
export function mapCodexThreadEvent(
obj: unknown,
runId: string,
): ActionEvent | null {
const ts = Date.now();
if (!obj || typeof obj !== 'object') return null;
const ev = obj as Record<string, unknown>;
if (typeof ev.type !== 'string') return null;
switch (ev.type) {
case 'thread.started': {
const threadId =
typeof ev.thread_id === 'string' ? ev.thread_id : undefined;
return { kind: 'run.started', agent: 'codex', runId, threadId, ts };
}
case 'turn.started':
return { kind: 'turn.started', runId, ts };
case 'item.started':
case 'item.completed': {
const completed = ev.type === 'item.completed';
const it = ev.item;
if (!it || typeof it !== 'object') return null;
const item = it as Record<string, unknown>;
const itemType =
typeof item.type === 'string' ? item.type : undefined;
const itemId = typeof item.id === 'string' ? item.id : '';
switch (itemType) {
case 'reasoning': {
// Reasoning carries meaningful text only on completion.
if (!completed) return null;
const text = typeof item.text === 'string' ? item.text : '';
return { kind: 'reasoning', runId, itemId, text, ts };
}
case 'command_execution': {
const command =
typeof item.command === 'string' ? item.command : '';
const output =
typeof item.aggregated_output === 'string'
? item.aggregated_output
: undefined;
const exitCode =
typeof item.exit_code === 'number' ? item.exit_code : undefined;
// Failure derivation: trust the item's own status, and treat any
// non-zero exit code as a failure. exit_code is null/absent on start.
const itemStatus =
typeof item.status === 'string' ? item.status : undefined;
const failed =
itemStatus === 'failed' || (exitCode != null && exitCode !== 0);
const status: 'started' | 'completed' | 'failed' = !completed
? 'started'
: failed
? 'failed'
: 'completed';
return {
kind: 'command',
runId,
itemId,
status,
command,
output,
exitCode,
ts,
};
}
case 'file_change': {
const rawChanges = Array.isArray(item.changes) ? item.changes : [];
const files = rawChanges
.filter(
(c): c is Record<string, unknown> =>
!!c && typeof c === 'object',
)
.map((c) => ({
path: typeof c.path === 'string' ? c.path : '',
op: normalizeFileOp(c.kind),
}));
const itemStatus =
typeof item.status === 'string' ? item.status : undefined;
const status: 'started' | 'completed' | 'failed' = !completed
? 'started'
: itemStatus === 'failed'
? 'failed'
: 'completed';
return { kind: 'file_change', runId, itemId, status, files, ts };
}
case 'mcp_tool_call': {
const status: 'started' | 'completed' | 'failed' = !completed
? 'started'
: item.status === 'failed' || item.error != null
? 'failed'
: 'completed';
return {
kind: 'tool',
runId,
itemId,
status,
server: typeof item.server === 'string' ? item.server : undefined,
tool: typeof item.tool === 'string' ? item.tool : 'mcp',
ts,
};
}
case 'web_search': {
const status: 'started' | 'completed' | 'failed' = completed
? 'completed'
: 'started';
return {
kind: 'tool',
runId,
itemId,
status,
tool: 'web_search',
summary: typeof item.query === 'string' ? item.query : undefined,
ts,
};
}
case 'agent_message': {
// Final assistant text only arrives on completion.
if (!completed) return null;
const text = typeof item.text === 'string' ? item.text : '';
return { kind: 'message', runId, text, ts };
}
case 'error': {
const error =
typeof item.message === 'string' ? item.message : 'item error';
return { kind: 'run.failed', runId, ok: false, error, ts };
}
// todo_list and any unknown item type -> skip (forward-compat).
default:
return null;
}
}
case 'turn.completed':
return { kind: 'run.completed', runId, ok: true, usage: ev.usage, ts };
case 'turn.failed': {
const err = ev.error;
const error =
err && typeof err === 'object' && typeof (err as Record<string, unknown>).message === 'string'
? ((err as Record<string, unknown>).message as string)
: 'turn.failed';
return { kind: 'run.failed', runId, ok: false, error, ts };
}
case 'error': {
const error =
typeof ev.message === 'string' ? ev.message : 'codex error';
return { kind: 'run.failed', runId, ok: false, error, ts };
}
// Unknown top-level event type -> skip.
default:
return null;
}
}
function normalizeFileOp(kind: unknown): 'add' | 'update' | 'delete' {
return kind === 'add' || kind === 'delete' ? kind : 'update';
}
/**
* Spawn the codex CLI and yield normalized ActionEvents.
*
* `codex exec --json --skip-git-repo-check -s workspace-write -C <repo> "<prompt>" </dev/null`
* stdin = 'ignore' (== /dev/null), stdout = JSONL parsed via readline, stderr drained.
*/
export async function* runCodex(
opts: ExecutorRunOptions,
): AsyncIterable<ActionEvent> {
const runId = newRunId();
if (opts.signal?.aborted) {
yield { kind: 'run.failed', runId, ok: false, error: 'aborted', ts: Date.now() };
return;
}
const args = [
'exec',
'--json',
'--skip-git-repo-check',
'-s',
'workspace-write',
'-C',
opts.repo,
opts.prompt,
];
const child = spawn(CODEX_BIN, args, {
stdio: ['ignore', 'pipe', 'pipe'], // stdin=/dev/null; no TTY hang
env: { ...process.env },
signal: opts.signal,
});
// Drain stderr; it is plain logs (skill-load errors, telemetry), NOT JSONL.
child.stderr?.on('data', () => {});
// Surface a clean run.failed if the binary cannot be spawned (ENOENT) or the
// AbortSignal kills it — instead of letting the process error escape the loop.
let spawnError: Error | null = null;
child.on('error', (err) => {
spawnError = err;
});
const rl = createInterface({ input: child.stdout!, crlfDelay: Infinity });
try {
for await (const line of rl) {
if (opts.signal?.aborted) break;
const s = line.trim();
if (!s || s[0] !== '{') continue; // skip blanks / non-JSON banner lines
let obj: unknown;
try {
obj = JSON.parse(s);
} catch {
continue; // tolerate partial/garbage lines
}
const mapped = mapCodexThreadEvent(obj, runId);
if (mapped) yield mapped;
}
} catch (e) {
yield {
kind: 'run.failed',
runId,
ok: false,
error: messageOf(e),
ts: Date.now(),
};
return;
}
if (opts.signal?.aborted) {
yield { kind: 'run.failed', runId, ok: false, error: 'aborted', ts: Date.now() };
return;
}
if (spawnError) {
yield {
kind: 'run.failed',
runId,
ok: false,
error: messageOf(spawnError),
ts: Date.now(),
};
}
}
function messageOf(e: unknown): string {
if (e instanceof Error) return e.message;
return String(e);
}
export const CodexExecutor: Executor = {
run(opts: ExecutorRunOptions): AsyncIterable<ActionEvent> {
return runCodex(opts);
},
};