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
//! Pipe-mode Codex bindings: NDJSON parser + spawn builder.
use super::traits::{CliEvent, NdjsonParser};
use crate::utils::truncate_str;
use crate::transport::SpawnOptions;
/// Codex CLI JSONL parser.
///
/// Expects output from: `codex exec --json "prompt"`
///
/// Event types: "thread.started", "turn.started", "turn.completed", "item.started", "item.completed"
pub struct CodexNdjsonParser {
thread_id: Option<String>,
}
impl CodexNdjsonParser {
pub fn new() -> Self {
Self { thread_id: None }
}
}
impl Default for CodexNdjsonParser {
fn default() -> Self {
Self::new()
}
}
impl NdjsonParser for CodexNdjsonParser {
fn parse_line(&mut self, line: &str) -> Vec<CliEvent> {
let line = line.trim();
if line.is_empty() {
return vec![];
}
let v: serde_json::Value = match serde_json::from_str(line) {
Ok(v) => v,
Err(_) => {
return vec![CliEvent::Error {
message: format!("invalid JSON: {}", truncate_str(line, 100)),
}]
}
};
let mut events = Vec::new();
match v.get("type").and_then(|t| t.as_str()) {
Some("thread.started") => {
let tid = v
.get("thread_id")
.and_then(|s| s.as_str())
.unwrap_or("")
.to_string();
self.thread_id = Some(tid.clone());
events.push(CliEvent::SessionStart {
session_id: tid,
model: "codex".to_string(),
tools: vec![],
});
}
Some("turn.completed") => {
if let Some(usage) = v.get("usage") {
let input = usage
.get("input_tokens")
.and_then(|v| v.as_u64())
.unwrap_or(0);
let output = usage
.get("output_tokens")
.and_then(|v| v.as_u64())
.unwrap_or(0);
events.push(CliEvent::TurnComplete {
input_tokens: input,
output_tokens: output,
});
}
}
Some("turn.failed") => {
let msg = v
.get("error")
.and_then(|e| e.as_str())
.unwrap_or("turn failed")
.to_string();
events.push(CliEvent::Error { message: msg });
}
Some("item.started") => {
if let Some(item) = v.get("item") {
match item.get("type").and_then(|t| t.as_str()) {
Some("command_execution") => {
let id = item
.get("id")
.and_then(|s| s.as_str())
.unwrap_or("")
.to_string();
let cmd = item
.get("command")
.and_then(|s| s.as_str())
.unwrap_or("")
.to_string();
events.push(CliEvent::ToolCallStart {
id,
name: "Bash".to_string(),
input: serde_json::json!({"command": cmd}),
});
}
// Both "agent_message" and "assistant_message" are valid names
// depending on the Codex version. Treat them as aliases.
Some("agent_message") | Some("assistant_message") => {
// Message starting — will get content in item.completed
}
Some("file_change") => {
let id = item
.get("id")
.and_then(|s| s.as_str())
.unwrap_or("")
.to_string();
events.push(CliEvent::ToolCallStart {
id,
name: "FileChange".to_string(),
input: item.clone(),
});
}
Some("reasoning") => {
if let Some(text) =
item.get("text").and_then(|s| s.as_str())
{
events.push(CliEvent::Thinking {
text: text.to_string(),
});
}
}
_ => {}
}
}
}
Some("item.completed") => {
if let Some(item) = v.get("item") {
match item.get("type").and_then(|t| t.as_str()) {
Some("command_execution") => {
let id = item
.get("id")
.and_then(|s| s.as_str())
.unwrap_or("")
.to_string();
// [BUGFIX] Codex emits "aggregated_output", not "output".
// Reading "output" always returned an empty string for all
// shell command results in pipe mode.
let output = item
.get("aggregated_output")
.and_then(|s| s.as_str())
.unwrap_or("")
.to_string();
let status =
item.get("status").and_then(|s| s.as_str()).unwrap_or("");
events.push(CliEvent::ToolCallResult {
id,
output,
is_error: status == "failed",
duration_ms: None,
});
}
// Both "agent_message" and "assistant_message" are valid names
// depending on the Codex version. Treat them as aliases.
Some("agent_message") | Some("assistant_message") => {
let text = item
.get("text")
.and_then(|s| s.as_str())
.unwrap_or("")
.to_string();
if !text.is_empty() {
events.push(CliEvent::AssistantText {
text,
is_delta: false,
});
}
}
Some("file_change") => {
let id = item
.get("id")
.and_then(|s| s.as_str())
.unwrap_or("")
.to_string();
events.push(CliEvent::ToolCallResult {
id,
output: "file changed".to_string(),
is_error: false,
duration_ms: None,
});
}
_ => {}
}
}
}
Some("error") => {
let msg = v
.get("message")
.and_then(|s| s.as_str())
.or_else(|| v.get("error").and_then(|s| s.as_str()))
.unwrap_or("unknown error")
.to_string();
events.push(CliEvent::Error { message: msg });
}
_ => {}
}
events
}
fn session_id(&self) -> Option<&str> {
self.thread_id.as_deref()
}
}
/// Pipe-mode spawn builder for Codex.
///
/// Argv produced (fresh session):
/// `codex exec --json --ask-for-approval never --skip-git-repo-check <prompt>`
///
/// Argv produced (resumed session):
/// `codex exec resume <session_id> --json --ask-for-approval never --skip-git-repo-check <prompt>`
pub struct CodexPipeBuilder;
impl super::traits::CliCommandBuilder for CodexPipeBuilder {
fn build_command(&self, opts: &SpawnOptions) -> std::process::Command {
let mut cmd = std::process::Command::new("codex");
if let Some(ref session_id) = opts.resume_session_id {
// Resume shape: `codex exec resume <id> --json --ask-for-approval never ...`
cmd.arg("exec");
cmd.arg("resume");
cmd.arg(session_id);
} else {
// Fresh shape: `codex exec --json --ask-for-approval never ...`
cmd.arg("exec");
}
cmd.arg("--json");
cmd.arg("--ask-for-approval");
cmd.arg("never");
cmd.arg("--skip-git-repo-check");
for arg in &opts.extra_args {
cmd.arg(arg);
}
// Prompt is the final positional argument.
cmd.arg(&opts.prompt);
cmd
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn codex_parses_thread_started() {
let mut parser = CodexNdjsonParser::new();
let line = r#"{"type":"thread.started","thread_id":"thread_xyz"}"#;
let events = parser.parse_line(line);
assert_eq!(events.len(), 1);
match &events[0] {
CliEvent::SessionStart { session_id, .. } => {
assert_eq!(session_id, "thread_xyz");
}
_ => panic!("expected SessionStart"),
}
}
/// Regression test: Codex emits "aggregated_output" for command_execution results,
/// NOT "output". Reading the wrong field returns an empty string — this test guards
/// against regressing to that behavior.
#[test]
fn codex_aggregated_output_regression() {
let mut parser = CodexNdjsonParser::new();
let line = r#"{"type":"item.completed","item":{"id":"item_1","type":"command_execution","command":"bash -lc 'ls -la'","aggregated_output":"total 48\ndrwxr-xr-x 12 user user 4096 Apr 9 12:00 .","exit_code":0,"status":"completed"}}"#;
let events = parser.parse_line(line);
assert_eq!(events.len(), 1, "expected exactly one ToolCallResult event");
match &events[0] {
CliEvent::ToolCallResult { output, is_error, .. } => {
assert!(
!output.is_empty(),
"aggregated_output must not be empty — parser may be reading the wrong field"
);
assert!(output.contains("total 48"), "expected ls output in aggregated_output");
assert!(!is_error, "status=completed should not be an error");
}
other => panic!("expected ToolCallResult, got {:?}", other),
}
}
/// Test that "assistant_message" is accepted as an alias for "agent_message"
/// in item.completed. Some Codex versions use one or the other.
#[test]
fn codex_assistant_message_alias_completed() {
let mut parser = CodexNdjsonParser::new();
let line = r#"{"type":"item.completed","item":{"id":"item_3","type":"assistant_message","text":"Here is what I found: some result"}}"#;
let events = parser.parse_line(line);
assert_eq!(events.len(), 1, "expected exactly one AssistantText event");
match &events[0] {
CliEvent::AssistantText { text, is_delta } => {
assert_eq!(text, "Here is what I found: some result");
assert!(!is_delta, "completed message should not be a delta");
}
other => panic!("expected AssistantText, got {:?}", other),
}
}
/// Test that "assistant_message" in item.started is silently consumed (no events).
#[test]
fn codex_assistant_message_alias_started() {
let mut parser = CodexNdjsonParser::new();
let line = r#"{"type":"item.started","item":{"id":"item_3","type":"assistant_message","status":"in_progress"}}"#;
let events = parser.parse_line(line);
assert!(
events.is_empty(),
"item.started for assistant_message should produce no events"
);
}
/// Test that "agent_message" still works.
#[test]
fn codex_agent_message_original_still_works() {
let mut parser = CodexNdjsonParser::new();
let line = r#"{"type":"item.completed","item":{"id":"item_2","type":"agent_message","text":"Original agent_message format still works"}}"#;
let events = parser.parse_line(line);
assert_eq!(events.len(), 1);
match &events[0] {
CliEvent::AssistantText { text, .. } => {
assert_eq!(text, "Original agent_message format still works");
}
other => panic!("expected AssistantText, got {:?}", other),
}
}
}