Skip to content

Commit 541aed1

Browse files
committed
fix: keep compaction spans and skip false empty spans in codex traces
1 parent d6c3972 commit 541aed1

2 files changed

Lines changed: 79 additions & 24 deletions

File tree

‎packages/core/src/agents/codex/parser.test.ts‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,28 @@ describe('enrichFromRollout', () => {
362362
['b', 't4', 'r3'],
363363
]);
364364
});
365+
366+
it('adds only itemless empty messages when messages do not pair', () => {
367+
const line = (timestamp: string, type: string, payload: object) =>
368+
JSON.stringify({ timestamp, type, payload });
369+
const usageRecord = (at: string, id: string) =>
370+
line(at, 'token_usage_record', { response_id: id, usage: {} });
371+
const rollout = [
372+
line('t1', 'response_item', { type: 'message', role: 'assistant' }),
373+
usageRecord('t2', 'r1'),
374+
usageRecord('t3', 'r2'),
375+
line('t4', 'response_item', { type: 'message', role: 'assistant' }),
376+
usageRecord('t5', 'r3'),
377+
].join('\n');
378+
const events: TranscriptEvent[] = [
379+
{ type: 'message', role: 'assistant', content: 'a' },
380+
];
381+
enrichFromRollout(events, rollout);
382+
expect(events.map((e) => [e.content, e.timestamp, e.requestId])).toEqual([
383+
['a', undefined, undefined],
384+
['', 't3', 'r2'],
385+
]);
386+
});
365387
});
366388

367389
describe('enrichFromRollout tool pairing', () => {
@@ -459,6 +481,32 @@ describe('enrichFromRollout tool pairing', () => {
459481
).toEqual([undefined, undefined, undefined, undefined]);
460482
});
461483

484+
it('adds only itemless empty messages when tools do not pair', () => {
485+
const events = [
486+
...call('item_1', '/bin/bash -lc pwd'),
487+
...call('item_2', '/bin/bash -lc ls'),
488+
];
489+
const usageRecord = (at: string, id: string) =>
490+
line(at, 'token_usage_record', { response_id: id, usage: {} });
491+
enrichFromRollout(
492+
events,
493+
[
494+
issued('c1', 't1'),
495+
completed('c1', 'pwd'),
496+
usageRecord('t2', 'r1'),
497+
finished('c1', 't3'),
498+
usageRecord('t4', 'r2'),
499+
].join('\n')
500+
);
501+
expect(events.map((e) => [e.type, e.requestId])).toEqual([
502+
['tool_call', undefined],
503+
['tool_result', undefined],
504+
['tool_call', undefined],
505+
['tool_result', undefined],
506+
['message', 'r2'],
507+
]);
508+
});
509+
462510
it('ignores non-tool rollout items', () => {
463511
expect(
464512
times(call('item_1', '/bin/bash -lc pwd'), [

‎packages/core/src/agents/codex/parser.ts‎

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,11 @@ const ROLLOUT_TOOL_ITEMS = new Set([
257257
* rollout, which the `--json` stream lacks. The two streams list tool items and
258258
* assistant messages in the same order but under different ids, so they're
259259
* paired by position. Tool pairs must also agree on the command or MCP tool,
260-
* and any count or content mismatch leaves that kind of event untouched.
261-
* A request no event came from, like a compaction call, gets an empty message.
260+
* and any count or content mismatch leaves those messages or tool calls
261+
* untouched. A request no event came from, like a compaction call, gets an
262+
* empty message. If the messages or tool calls didn't pair, only a request with
263+
* no `response_item` records gets one, since the others may just be missing
264+
* their requestId.
262265
*
263266
* rollout: reasoning → message → function_call(c1) → token_usage_record(r1)
264267
* → function_call_output(c1)
@@ -271,6 +274,8 @@ export function enrichFromRollout(
271274
interface Request {
272275
id?: string;
273276
usage?: RequestUsage;
277+
/** No response item before its usage record, like a compaction call. */
278+
itemless?: boolean;
274279
}
275280
const callStarts = new Map<string, { at?: string; request: Request }>();
276281
const callEnds = new Map<string, string | undefined>();
@@ -283,7 +288,7 @@ export function enrichFromRollout(
283288
const at = str(record.timestamp);
284289
const payload = isRecord(record.payload) ? record.payload : {};
285290
if (record.type === 'token_usage_record') {
286-
const request = open ?? {};
291+
const request = open ?? { itemless: true };
287292
request.id = str(payload.response_id);
288293
request.usage = rolloutUsage(payload.usage);
289294
requests.push({ at, request });
@@ -320,37 +325,39 @@ export function enrichFromRollout(
320325
const assistant = events.filter(
321326
(e) => e.type === 'message' && e.role === 'assistant'
322327
);
323-
if (assistant.length === messages.length) {
328+
const messagesPaired = assistant.length === messages.length;
329+
if (messagesPaired) {
324330
assistant.forEach((event, i) => {
325331
event.timestamp = messages[i].at ?? event.timestamp;
326332
tag(event, messages[i].request);
327333
});
328334
}
329335
const calls = events.filter((e) => e.type === 'tool_call');
330-
if (
331-
calls.length !== toolItems.length ||
332-
calls.some((event, i) => !sameCall(event, toolItems[i]))
333-
) {
334-
return;
335-
}
336-
const callIdByItem = new Map<string, string>();
337-
calls.forEach((event, i) => {
338-
const itemId = String(toolItems[i].id);
339-
const start = callStarts.get(itemId);
340-
if (event.tool?.id) callIdByItem.set(event.tool.id, itemId);
341-
if (!start) return;
342-
event.timestamp = start.at ?? event.timestamp;
343-
tag(event, start.request);
344-
});
345-
for (const event of events) {
346-
if (event.type !== 'tool_result' || !event.tool?.id) continue;
347-
const callId = callIdByItem.get(event.tool.id);
348-
const end = callId ? callEnds.get(callId) : undefined;
349-
if (end) event.timestamp = end;
336+
const toolsPaired =
337+
calls.length === toolItems.length &&
338+
calls.every((event, i) => sameCall(event, toolItems[i]));
339+
if (toolsPaired) {
340+
const callIdByItem = new Map<string, string>();
341+
calls.forEach((event, i) => {
342+
const itemId = String(toolItems[i].id);
343+
const start = callStarts.get(itemId);
344+
if (event.tool?.id) callIdByItem.set(event.tool.id, itemId);
345+
if (!start) return;
346+
event.timestamp = start.at ?? event.timestamp;
347+
tag(event, start.request);
348+
});
349+
for (const event of events) {
350+
if (event.type !== 'tool_result' || !event.tool?.id) continue;
351+
const callId = callIdByItem.get(event.tool.id);
352+
const end = callId ? callEnds.get(callId) : undefined;
353+
if (end) event.timestamp = end;
354+
}
350355
}
351356
const tagged = new Set(events.map((e) => e.requestId));
352357
for (const { at, request } of requests) {
353358
if (!request.id || tagged.has(request.id)) continue;
359+
// Unpaired events have no requestId, so their requests only look silent.
360+
if (!(messagesPaired && toolsPaired) && !request.itemless) continue;
354361
const silent: TranscriptEvent = {
355362
type: 'message',
356363
role: 'assistant',

0 commit comments

Comments
 (0)