-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcodex.test.mjs
More file actions
237 lines (219 loc) · 8.45 KB
/
Copy pathcodex.test.mjs
File metadata and controls
237 lines (219 loc) · 8.45 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
/**
* test/adapters/codex.test.mjs -> hooks/adapters/codex.mjs
*/
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { recordsToNormalized } from '../../hooks/adapters/codex.mjs';
import { reconstructTraceFromNRs } from '../../hooks/trace-tree.mjs';
test('Codex adapter maps messages, tools, results, and token counts', () => {
const nrs = recordsToNormalized([
{
timestamp: '2026-08-21T19:00:00.000Z',
type: 'session_meta',
payload: {
id: '01abc',
cwd: '/Users/vinayakarora/Documents/GitHub/kaaroSessions',
cli_version: '0.148.0-alpha.9',
git: { branch: 'main' },
},
},
{
timestamp: '2026-08-21T19:00:01.000Z',
type: 'response_item',
payload: {
type: 'message',
role: 'user',
content: [{ type: 'input_text', text: 'Please make the sessions easier to hear' }],
},
},
{
timestamp: '2026-08-21T19:00:02.000Z',
type: 'response_item',
payload: {
type: 'message',
role: 'assistant',
content: [{ type: 'output_text', text: 'I will inspect the audio path.' }],
},
},
{
timestamp: '2026-08-21T19:00:03.000Z',
type: 'response_item',
payload: {
type: 'function_call',
name: 'exec_command',
call_id: 'call_1',
arguments: '{"cmd":"node --test","workdir":"/tmp/app"}',
},
},
{
timestamp: '2026-08-21T19:00:04.000Z',
type: 'response_item',
payload: {
type: 'function_call_output',
call_id: 'call_1',
output: 'Process exited with code 1\nError: boom',
},
},
{
timestamp: '2026-08-21T19:00:05.000Z',
type: 'event_msg',
payload: {
type: 'token_count',
info: {
last_token_usage: {
input_tokens: 10,
output_tokens: 4,
cached_input_tokens: 2,
cache_write_input_tokens: 1,
},
},
},
},
]);
assert.ok(nrs.some(r => r.kind === 'session_meta' && r.cwd?.endsWith('kaaroSessions')));
assert.ok(nrs.some(r => r.kind === 'user_turn' && r.text === 'Please make the sessions easier to hear'));
assert.ok(nrs.some(r => r.kind === 'assistant_turn' && r.model === null));
assert.ok(nrs.some(r => r.kind === 'content_block' && r.block_type === 'text' && r.text.includes('audio path')));
assert.ok(nrs.some(r => r.kind === 'tool_use' && r.tool === 'exec_command' && r.input.command === 'node --test'));
assert.ok(nrs.some(r => r.kind === 'tool_result' && r.tool_id === 'call_1' && r.error === true));
assert.ok(nrs.some(r => r.kind === 'tokens' && r.tokens.input === 0 && r.tokens.output === 4 && r.tokens.cache_read === 0));
const tree = reconstructTraceFromNRs(nrs);
assert.equal(tree.segments[0].turns.some(t => t.role === 'assistant' && t.text.includes('audio path')), true);
});
test('Codex adapter ignores cumulative token totals as per-event signal', () => {
const nrs = recordsToNormalized([{
timestamp: '2026-08-21T19:00:05.000Z',
type: 'event_msg',
payload: {
type: 'token_count',
info: {
total_token_usage: {
input_tokens: 999999,
output_tokens: 999999,
cached_input_tokens: 999999,
cache_write_input_tokens: 999999,
},
},
},
}]);
assert.equal(nrs.some(r => r.kind === 'tokens'), false);
});
test('Codex adapter categorizes real CLI shell_command tool name as bash (git)', () => {
// Real local Codex CLI (v0.147.0+) emits function_call name "shell_command",
// not "exec_command" — verified against live ~/.codex rollout transcripts.
const nrs = recordsToNormalized([
{
timestamp: '2026-08-29T10:49:30.000Z',
type: 'response_item',
payload: {
type: 'function_call',
name: 'shell_command',
call_id: 'call_1',
arguments: '{"command":"git status --short --branch","workdir":"D:\\\\src\\\\x"}',
},
},
]);
const toolUse = nrs.find(r => r.kind === 'tool_use');
assert.equal(toolUse.tool, 'shell_command');
assert.equal(toolUse.category, 'git');
});
test('Codex adapter populates user_turn text on every qualifying turn, not just the first', () => {
const nrs = recordsToNormalized([
{
timestamp: '2026-08-29T10:49:21.000Z',
type: 'response_item',
payload: { type: 'message', role: 'user', content: [{ type: 'input_text', text: 'First prompt here' }] },
},
{
timestamp: '2026-08-29T10:55:59.000Z',
type: 'response_item',
payload: { type: 'message', role: 'user', content: [{ type: 'input_text', text: 'Second prompt here too' }] },
},
]);
const userTurns = nrs.filter(r => r.kind === 'user_turn');
assert.equal(userTurns.length, 2);
assert.equal(userTurns[0].text, 'First prompt here');
assert.equal(userTurns[1].text, 'Second prompt here too');
});
test('Codex adapter does not double-emit assistant text (event_msg/agent_message duplicates response_item/message)', () => {
// Verified against live ~/.codex rollouts: every assistant turn writes
// BOTH an event_msg/agent_message AND a response_item/message with the
// same text (1ms apart) — a real session showed exactly 2x as many
// content_block:text NRs as actual assistant turns before this fix.
const nrs = recordsToNormalized([
{
timestamp: '2026-08-29T10:00:00.000Z',
type: 'event_msg',
payload: { type: 'agent_message', message: 'I will inspect the repo shape first.' },
},
{
timestamp: '2026-08-29T10:00:00.001Z',
type: 'response_item',
payload: { type: 'message', role: 'assistant', content: [{ type: 'output_text', text: 'I will inspect the repo shape first.' }] },
},
]);
const textBlocks = nrs.filter(r => r.kind === 'content_block' && r.block_type === 'text');
assert.equal(textBlocks.length, 1, 'assistant text must be emitted exactly once, not once per channel');
});
test('Codex adapter maps custom_tool_call (apply_patch) to tool_use/tool_result — real file-edit channel', () => {
// Verified against live ~/.codex rollouts: file edits never go through
// function_call — they're a separate custom_tool_call/custom_tool_call_output
// pair. Without this, file_ops stayed empty for every real codex session,
// even ones with 100+ tool calls.
const nrs = recordsToNormalized([
{
timestamp: '2026-08-29T10:00:00.000Z',
type: 'response_item',
payload: {
type: 'custom_tool_call', status: 'completed', call_id: 'call_1', name: 'apply_patch',
input: '*** Begin Patch\n*** Update File: src/app.mjs\n@@\n-old\n+new\n*** End Patch',
},
},
{
timestamp: '2026-08-29T10:00:01.000Z',
type: 'response_item',
payload: {
type: 'custom_tool_call_output', call_id: 'call_1',
output: '{"output":"Success. Updated the following files:\\nM src/app.mjs\\n","metadata":{"exit_code":0}}',
},
},
]);
const toolUse = nrs.find(r => r.kind === 'tool_use');
assert.equal(toolUse.tool, 'apply_patch');
assert.equal(toolUse.tool_id, 'call_1');
assert.deepEqual(toolUse.input.paths, ['src/app.mjs']);
const toolResult = nrs.find(r => r.kind === 'tool_result');
assert.equal(toolResult.tool_id, 'call_1');
assert.equal(toolResult.error, false);
});
test('Codex adapter extracts every path from a multi-file apply_patch call', () => {
const nrs = recordsToNormalized([{
timestamp: '2026-08-29T10:00:00.000Z',
type: 'response_item',
payload: {
type: 'custom_tool_call', status: 'completed', call_id: 'call_2', name: 'apply_patch',
input: '*** Begin Patch\n*** Update File: a.mjs\n@@\n-x\n+y\n*** Add File: b.mjs\n+z\n*** End Patch',
},
}]);
const toolUse = nrs.find(r => r.kind === 'tool_use');
assert.deepEqual(toolUse.input.paths, ['a.mjs', 'b.mjs']);
});
test('Codex adapter marks a failed apply_patch as a tool_result error', () => {
const nrs = recordsToNormalized([
{
timestamp: '2026-08-29T10:00:00.000Z',
type: 'response_item',
payload: { type: 'custom_tool_call', status: 'completed', call_id: 'call_3', name: 'apply_patch', input: '*** Begin Patch\n*** End Patch' },
},
{
timestamp: '2026-08-29T10:00:01.000Z',
type: 'response_item',
payload: {
type: 'custom_tool_call_output', call_id: 'call_3',
output: '{"output":"Error: context mismatch","metadata":{"exit_code":1}}',
},
},
]);
const toolResult = nrs.find(r => r.kind === 'tool_result');
assert.equal(toolResult.error, true);
});