codex-codes 0.101.1

A tightly typed Rust interface for the OpenAI Codex CLI JSON protocol
Documentation
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
use codex_codes::{
    CommandExecutionStatus, FileChangeItem, PatchApplyStatus, PatchChangeKind, ThreadEvent,
    ThreadItem,
};

/// Parse every line from a JSONL capture file into ThreadEvents,
/// panicking on any deserialization failure.
fn parse_capture(jsonl: &str) -> Vec<ThreadEvent> {
    jsonl
        .lines()
        .filter(|line| !line.trim().is_empty())
        .enumerate()
        .map(|(i, line)| {
            serde_json::from_str::<ThreadEvent>(line)
                .unwrap_or_else(|e| panic!("Failed to parse line {}: {}\n  JSON: {}", i, e, line))
        })
        .collect()
}

/// Extract all ThreadItems from ItemCompleted events.
fn completed_items(events: &[ThreadEvent]) -> Vec<&ThreadItem> {
    events
        .iter()
        .filter_map(|e| match e {
            ThreadEvent::ItemCompleted(ic) => Some(&ic.item),
            _ => None,
        })
        .collect()
}

/// Every capture must start with thread.started and end with turn.completed.
fn assert_standard_envelope(events: &[ThreadEvent]) {
    assert!(
        events.len() >= 3,
        "Expected at least 3 events, got {}",
        events.len()
    );
    assert_eq!(events[0].event_type(), "thread.started");
    assert_eq!(events[1].event_type(), "turn.started");
    assert_eq!(
        events.last().unwrap().event_type(),
        "turn.completed",
        "Last event should be turn.completed"
    );

    // thread.started always carries a thread_id
    if let ThreadEvent::ThreadStarted(e) = &events[0] {
        assert!(!e.thread_id.is_empty(), "thread_id must not be empty");
    }

    // turn.completed always carries usage with non-zero tokens
    if let ThreadEvent::TurnCompleted(e) = events.last().unwrap() {
        assert!(e.usage.input_tokens > 0, "input_tokens should be > 0");
        assert!(e.usage.output_tokens > 0, "output_tokens should be > 0");
    }
}

// ── hello_world: simplest possible session ──────────────────────────

#[test]
fn test_hello_world_parses_all_lines() {
    let events = parse_capture(include_str!("../test_cases/captures/hello_world.jsonl"));
    assert_standard_envelope(&events);
    assert_eq!(events.len(), 5);
}

#[test]
fn test_hello_world_contains_reasoning_and_message() {
    let events = parse_capture(include_str!("../test_cases/captures/hello_world.jsonl"));
    let items = completed_items(&events);

    let reasoning_count = items
        .iter()
        .filter(|i| matches!(i, ThreadItem::Reasoning(_)))
        .count();
    let message_count = items
        .iter()
        .filter(|i| matches!(i, ThreadItem::AgentMessage(_)))
        .count();

    assert!(reasoning_count >= 1, "Expected at least one reasoning item");
    assert!(message_count >= 1, "Expected at least one agent message");

    // The final agent message should contain "hello world"
    let last_msg = items
        .iter()
        .rev()
        .find_map(|i| match i {
            ThreadItem::AgentMessage(m) => Some(&m.text),
            _ => None,
        })
        .expect("Should have an agent message");
    assert_eq!(last_msg, "hello world");
}

// ── list_files: command execution with item.started lifecycle ────────

#[test]
fn test_list_files_parses_all_lines() {
    let events = parse_capture(include_str!("../test_cases/captures/list_files.jsonl"));
    assert_standard_envelope(&events);
    assert_eq!(events.len(), 8);
}

#[test]
fn test_list_files_command_lifecycle() {
    let events = parse_capture(include_str!("../test_cases/captures/list_files.jsonl"));

    // Find the item.started / item.completed pair for the command
    let started_cmd = events.iter().find_map(|e| match e {
        ThreadEvent::ItemStarted(is) => match &is.item {
            ThreadItem::CommandExecution(c) => Some(c),
            _ => None,
        },
        _ => None,
    });
    let completed_cmd = completed_items(&events).into_iter().find_map(|i| match i {
        ThreadItem::CommandExecution(c) => Some(c),
        _ => None,
    });

    let started = started_cmd.expect("Should have an item.started command");
    let completed = completed_cmd.expect("Should have an item.completed command");

    // Same item id
    assert_eq!(started.id, completed.id);

    // Started has in_progress status, no exit code, empty output
    assert_eq!(started.status, CommandExecutionStatus::InProgress);
    assert_eq!(started.exit_code, None);
    assert!(started.aggregated_output.is_empty());

    // Completed has exit code 0, non-empty output
    assert_eq!(completed.status, CommandExecutionStatus::Completed);
    assert_eq!(completed.exit_code, Some(0));
    assert!(!completed.aggregated_output.is_empty());
    assert!(completed.command.contains("ls"));
}

// ── file_create: command that creates a file ────────────────────────

#[test]
fn test_file_create_parses_all_lines() {
    let events = parse_capture(include_str!("../test_cases/captures/file_create.jsonl"));
    assert_standard_envelope(&events);
    assert_eq!(events.len(), 8);
}

#[test]
fn test_file_create_command_output() {
    let events = parse_capture(include_str!("../test_cases/captures/file_create.jsonl"));
    let cmd = completed_items(&events)
        .into_iter()
        .find_map(|i| match i {
            ThreadItem::CommandExecution(c) => Some(c),
            _ => None,
        })
        .expect("Should have a completed command");

    assert_eq!(cmd.exit_code, Some(0));
    assert_eq!(cmd.aggregated_output, "hello from codex");
}

// ── failed_command: non-zero exit code ──────────────────────────────

#[test]
fn test_failed_command_parses_all_lines() {
    let events = parse_capture(include_str!("../test_cases/captures/failed_command.jsonl"));
    assert_standard_envelope(&events);
    assert_eq!(events.len(), 8);
}

#[test]
fn test_failed_command_status_and_exit_code() {
    let events = parse_capture(include_str!("../test_cases/captures/failed_command.jsonl"));

    let started_cmd = events.iter().find_map(|e| match e {
        ThreadEvent::ItemStarted(is) => match &is.item {
            ThreadItem::CommandExecution(c) => Some(c),
            _ => None,
        },
        _ => None,
    });
    let completed_cmd = completed_items(&events).into_iter().find_map(|i| match i {
        ThreadItem::CommandExecution(c) => Some(c),
        _ => None,
    });

    let started = started_cmd.expect("Should have started command");
    let completed = completed_cmd.expect("Should have completed command");

    assert_eq!(started.status, CommandExecutionStatus::InProgress);
    assert_eq!(started.exit_code, None);

    assert_eq!(completed.status, CommandExecutionStatus::Failed);
    assert_eq!(completed.exit_code, Some(42));
    assert!(completed.aggregated_output.is_empty());
}

// ── file_change: patch-based file modification ──────────────────────

#[test]
fn test_file_change_parses_all_lines() {
    let events = parse_capture(include_str!("../test_cases/captures/file_change.jsonl"));
    assert_standard_envelope(&events);
    assert_eq!(events.len(), 12);
}

#[test]
fn test_file_change_item_fields() {
    let events = parse_capture(include_str!("../test_cases/captures/file_change.jsonl"));

    let fc: &FileChangeItem = completed_items(&events)
        .into_iter()
        .find_map(|i| match i {
            ThreadItem::FileChange(f) => Some(f),
            _ => None,
        })
        .expect("Should have a file_change item");

    assert_eq!(fc.status, PatchApplyStatus::Completed);
    assert_eq!(fc.changes.len(), 1);
    assert_eq!(fc.changes[0].kind, PatchChangeKind::Update);
    assert!(fc.changes[0].path.contains("test.txt"));
}

#[test]
fn test_file_change_also_has_command_verification() {
    let events = parse_capture(include_str!("../test_cases/captures/file_change.jsonl"));
    let cmds: Vec<_> = completed_items(&events)
        .into_iter()
        .filter_map(|i| match i {
            ThreadItem::CommandExecution(c) => Some(c),
            _ => None,
        })
        .collect();

    assert!(!cmds.is_empty());
    assert!(cmds.iter().any(|c| c.command.contains("cat")));
    assert!(cmds
        .iter()
        .any(|c| c.aggregated_output.contains("new content")));
}

// ── multi_command: multiple sequential commands ─────────────────────

#[test]
fn test_multi_command_parses_all_lines() {
    let events = parse_capture(include_str!("../test_cases/captures/multi_command.jsonl"));
    assert_standard_envelope(&events);
    assert_eq!(events.len(), 12);
}

#[test]
fn test_multi_command_three_commands_executed() {
    let events = parse_capture(include_str!("../test_cases/captures/multi_command.jsonl"));

    let cmds: Vec<_> = completed_items(&events)
        .into_iter()
        .filter_map(|i| match i {
            ThreadItem::CommandExecution(c) => Some(c),
            _ => None,
        })
        .collect();

    assert_eq!(cmds.len(), 3, "Expected exactly 3 completed commands");

    for (i, cmd) in cmds.iter().enumerate() {
        let step = format!("step{}", i + 1);
        assert!(
            cmd.command.contains(&step),
            "Command {} should contain '{}'",
            i,
            step
        );
        assert_eq!(cmd.exit_code, Some(0));
        assert_eq!(cmd.status, CommandExecutionStatus::Completed);
        assert!(
            cmd.aggregated_output.contains(&step),
            "Output of command {} should contain '{}'",
            i,
            step
        );
    }
}

#[test]
fn test_multi_command_started_events_match_completed() {
    let events = parse_capture(include_str!("../test_cases/captures/multi_command.jsonl"));

    let started_ids: Vec<_> = events
        .iter()
        .filter_map(|e| match e {
            ThreadEvent::ItemStarted(is) => match &is.item {
                ThreadItem::CommandExecution(c) => Some(c.id.clone()),
                _ => None,
            },
            _ => None,
        })
        .collect();

    let completed_ids: Vec<_> = completed_items(&events)
        .into_iter()
        .filter_map(|i| match i {
            ThreadItem::CommandExecution(c) => Some(c.id.clone()),
            _ => None,
        })
        .collect();

    assert_eq!(started_ids.len(), 3);
    assert_eq!(completed_ids.len(), 3);
    assert_eq!(
        started_ids, completed_ids,
        "Every started command should have a matching completed event"
    );
}

// ── cross-capture: verify all captures share structural invariants ──

#[test]
fn test_all_captures_have_unique_thread_ids() {
    let captures = [
        include_str!("../test_cases/captures/hello_world.jsonl"),
        include_str!("../test_cases/captures/list_files.jsonl"),
        include_str!("../test_cases/captures/file_create.jsonl"),
        include_str!("../test_cases/captures/failed_command.jsonl"),
        include_str!("../test_cases/captures/file_change.jsonl"),
        include_str!("../test_cases/captures/multi_command.jsonl"),
    ];

    let thread_ids: Vec<String> = captures
        .iter()
        .map(|c| {
            let events = parse_capture(c);
            match &events[0] {
                ThreadEvent::ThreadStarted(e) => e.thread_id.clone(),
                _ => panic!("First event should be ThreadStarted"),
            }
        })
        .collect();

    let mut deduped = thread_ids.clone();
    deduped.sort();
    deduped.dedup();
    assert_eq!(
        thread_ids.len(),
        deduped.len(),
        "All captures should have unique thread IDs"
    );
}

#[test]
fn test_all_captures_have_cached_tokens() {
    let captures = [
        include_str!("../test_cases/captures/hello_world.jsonl"),
        include_str!("../test_cases/captures/list_files.jsonl"),
        include_str!("../test_cases/captures/file_create.jsonl"),
        include_str!("../test_cases/captures/failed_command.jsonl"),
        include_str!("../test_cases/captures/file_change.jsonl"),
        include_str!("../test_cases/captures/multi_command.jsonl"),
    ];

    for (i, capture) in captures.iter().enumerate() {
        let events = parse_capture(capture);
        if let ThreadEvent::TurnCompleted(tc) = events.last().unwrap() {
            assert!(
                tc.usage.cached_input_tokens > 0,
                "Capture {} should have cached_input_tokens > 0",
                i
            );
        }
    }
}

#[test]
fn test_all_item_ids_are_sequential_within_capture() {
    let captures = [
        include_str!("../test_cases/captures/hello_world.jsonl"),
        include_str!("../test_cases/captures/list_files.jsonl"),
        include_str!("../test_cases/captures/file_create.jsonl"),
        include_str!("../test_cases/captures/failed_command.jsonl"),
        include_str!("../test_cases/captures/file_change.jsonl"),
        include_str!("../test_cases/captures/multi_command.jsonl"),
    ];

    for capture in &captures {
        let events = parse_capture(capture);
        let ids: Vec<String> = completed_items(&events)
            .into_iter()
            .map(|item| match item {
                ThreadItem::AgentMessage(m) => m.id.clone(),
                ThreadItem::Reasoning(r) => r.id.clone(),
                ThreadItem::CommandExecution(c) => c.id.clone(),
                ThreadItem::FileChange(f) => f.id.clone(),
                ThreadItem::McpToolCall(m) => m.id.clone(),
                ThreadItem::WebSearch(w) => w.id.clone(),
                ThreadItem::TodoList(t) => t.id.clone(),
                ThreadItem::Error(e) => e.id.clone(),
            })
            .collect();

        // IDs follow the pattern "item_N" with increasing N
        let mut seen: Vec<usize> = Vec::new();
        for id in &ids {
            assert!(
                id.starts_with("item_"),
                "ID '{}' should start with 'item_'",
                id
            );
            let n: usize = id[5..]
                .parse()
                .unwrap_or_else(|_| panic!("ID '{}' should have a numeric suffix", id));
            if !seen.contains(&n) {
                seen.push(n);
            }
        }

        for window in seen.windows(2) {
            assert!(
                window[1] > window[0],
                "Item IDs should be monotonically increasing, got {} after {}",
                window[1],
                window[0]
            );
        }
    }
}