mirror of
https://github.com/wu736139669/hapi.git
synced 2026-11-29 06:13:46 +00:00
fix(codex): preserve transcript final answer ordering
This commit is contained in:
@@ -1,5 +1,14 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { convertCodexEvent, createCodexEventConverter } from './codexEventConverter';
|
||||
import {
|
||||
convertCodexEvent,
|
||||
createCodexEventConverter,
|
||||
type CodexConversionAction,
|
||||
type CodexMessage
|
||||
} from './codexEventConverter';
|
||||
|
||||
function getAgentMessages(actions: CodexConversionAction[]): CodexMessage[] {
|
||||
return actions.flatMap((action) => action.type === 'agent-message' ? [action.message] : []);
|
||||
}
|
||||
|
||||
describe('convertCodexEvent', () => {
|
||||
it('extracts session_meta id', () => {
|
||||
@@ -247,14 +256,14 @@ describe('convertCodexEvent', () => {
|
||||
it('deduplicates legacy and 0.147 semantic messages against response items', () => {
|
||||
const convert = createCodexEventConverter();
|
||||
|
||||
convert({
|
||||
expect(convert({
|
||||
type: 'turn_context',
|
||||
payload: { turn_id: 'turn-1' }
|
||||
});
|
||||
expect(convert({
|
||||
})).toEqual([]);
|
||||
expect(getAgentMessages(convert({
|
||||
type: 'event_msg',
|
||||
payload: { type: 'agent_message', phase: 'commentary', message: 'legacy commentary' }
|
||||
})?.messages?.[0]).toMatchObject({ message: 'legacy commentary' });
|
||||
}))[0]).toMatchObject({ message: 'legacy commentary' });
|
||||
expect(convert({
|
||||
type: 'response_item',
|
||||
payload: {
|
||||
@@ -265,9 +274,9 @@ describe('convertCodexEvent', () => {
|
||||
content: [{ type: 'output_text', text: 'legacy commentary' }],
|
||||
internal_chat_message_metadata_passthrough: { turn_id: 'turn-1' }
|
||||
}
|
||||
})).toBeNull();
|
||||
})).toEqual([]);
|
||||
|
||||
expect(convert({
|
||||
expect(getAgentMessages(convert({
|
||||
type: 'event_msg',
|
||||
payload: {
|
||||
type: 'item_completed',
|
||||
@@ -279,7 +288,7 @@ describe('convertCodexEvent', () => {
|
||||
content: [{ type: 'Text', text: 'new commentary' }]
|
||||
}
|
||||
}
|
||||
})?.messages?.[0]).toMatchObject({ message: 'new commentary' });
|
||||
}))[0]).toMatchObject({ message: 'new commentary' });
|
||||
expect(convert({
|
||||
type: 'response_item',
|
||||
payload: {
|
||||
@@ -290,7 +299,7 @@ describe('convertCodexEvent', () => {
|
||||
content: [{ type: 'output_text', text: 'new commentary' }],
|
||||
internal_chat_message_metadata_passthrough: { turn_id: 'turn-1' }
|
||||
}
|
||||
})).toBeNull();
|
||||
})).toEqual([]);
|
||||
|
||||
expect(convert({
|
||||
type: 'response_item',
|
||||
@@ -302,7 +311,364 @@ describe('convertCodexEvent', () => {
|
||||
content: [{ type: 'output_text', text: 'response-only final' }],
|
||||
internal_chat_message_metadata_passthrough: { turn_id: 'turn-1' }
|
||||
}
|
||||
})?.messages?.[0]).toMatchObject({ message: 'response-only final' });
|
||||
})).toEqual([]);
|
||||
const completed = convert({
|
||||
type: 'event_msg',
|
||||
payload: { type: 'task_complete', turn_id: 'turn-1' }
|
||||
});
|
||||
expect(getAgentMessages(completed)).toEqual([
|
||||
expect.objectContaining({ message: 'response-only final' })
|
||||
]);
|
||||
expect(completed.at(-1)).toEqual({ type: 'turn-finished', turnId: 'turn-1' });
|
||||
});
|
||||
|
||||
it('flushes older finals before emitting a later semantic mirror', () => {
|
||||
const convert = createCodexEventConverter();
|
||||
|
||||
convert({
|
||||
type: 'turn_context',
|
||||
payload: { turn_id: 'turn-1' }
|
||||
});
|
||||
expect(convert({
|
||||
type: 'response_item',
|
||||
payload: {
|
||||
type: 'message',
|
||||
id: 'final-a',
|
||||
role: 'assistant',
|
||||
phase: 'final_answer',
|
||||
content: [{ type: 'output_text', text: 'visible final A' }],
|
||||
internal_chat_message_metadata_passthrough: { turn_id: 'turn-1' }
|
||||
}
|
||||
})).toEqual([]);
|
||||
expect(convert({
|
||||
type: 'response_item',
|
||||
payload: {
|
||||
type: 'message',
|
||||
id: 'final-b',
|
||||
role: 'assistant',
|
||||
phase: 'final_answer',
|
||||
content: [{ type: 'output_text', text: 'visible final B' }],
|
||||
internal_chat_message_metadata_passthrough: { turn_id: 'turn-1' }
|
||||
}
|
||||
})).toEqual([]);
|
||||
|
||||
const mirrored = convert({
|
||||
type: 'event_msg',
|
||||
payload: {
|
||||
type: 'item_completed',
|
||||
turn_id: 'turn-1',
|
||||
item: {
|
||||
type: 'AgentMessage',
|
||||
id: 'final-b',
|
||||
phase: 'final_answer',
|
||||
content: [{ type: 'Text', text: 'visible final B' }]
|
||||
}
|
||||
}
|
||||
});
|
||||
expect(getAgentMessages(mirrored)).toEqual([{
|
||||
type: 'message',
|
||||
message: 'visible final A',
|
||||
id: 'final-a'
|
||||
}, {
|
||||
type: 'message',
|
||||
message: 'visible final B',
|
||||
id: 'final-b'
|
||||
}]);
|
||||
expect(convert({
|
||||
type: 'event_msg',
|
||||
payload: { type: 'task_complete', turn_id: 'turn-1' }
|
||||
})).toEqual([{ type: 'turn-finished', turnId: 'turn-1' }]);
|
||||
});
|
||||
|
||||
it('does not register a semantic message after pairing it with a buffered response', () => {
|
||||
const convert = createCodexEventConverter();
|
||||
|
||||
convert({ type: 'turn_context', payload: { turn_id: 'turn-1' } });
|
||||
convert({
|
||||
type: 'response_item',
|
||||
payload: {
|
||||
type: 'message',
|
||||
id: 'paired-final',
|
||||
role: 'assistant',
|
||||
phase: 'final_answer',
|
||||
content: [{ type: 'output_text', text: 'same visible text' }],
|
||||
internal_chat_message_metadata_passthrough: { turn_id: 'turn-1' }
|
||||
}
|
||||
});
|
||||
expect(getAgentMessages(convert({
|
||||
type: 'event_msg',
|
||||
payload: {
|
||||
type: 'item_completed',
|
||||
turn_id: 'turn-1',
|
||||
item: {
|
||||
type: 'AgentMessage',
|
||||
id: 'paired-final',
|
||||
phase: 'final_answer',
|
||||
content: [{ type: 'Text', text: 'same visible text' }]
|
||||
}
|
||||
}
|
||||
}))).toEqual([{
|
||||
type: 'message',
|
||||
message: 'same visible text',
|
||||
id: 'paired-final'
|
||||
}]);
|
||||
|
||||
expect(convert({
|
||||
type: 'response_item',
|
||||
payload: {
|
||||
type: 'message',
|
||||
id: 'response-only-final',
|
||||
role: 'assistant',
|
||||
phase: 'final_answer',
|
||||
content: [{ type: 'output_text', text: 'same visible text' }],
|
||||
internal_chat_message_metadata_passthrough: { turn_id: 'turn-1' }
|
||||
}
|
||||
})).toEqual([]);
|
||||
expect(getAgentMessages(convert({
|
||||
type: 'event_msg',
|
||||
payload: { type: 'task_complete', turn_id: 'turn-1' }
|
||||
}))).toEqual([{
|
||||
type: 'message',
|
||||
message: 'same visible text',
|
||||
id: 'response-only-final'
|
||||
}]);
|
||||
});
|
||||
|
||||
it('finalizes a response-only answer when the event stream ends', () => {
|
||||
const convert = createCodexEventConverter();
|
||||
|
||||
convert({ type: 'turn_context', payload: { turn_id: 'turn-1' } });
|
||||
expect(convert({
|
||||
type: 'response_item',
|
||||
payload: {
|
||||
type: 'message',
|
||||
id: 'final-at-eof',
|
||||
role: 'assistant',
|
||||
phase: 'final_answer',
|
||||
content: [{ type: 'output_text', text: 'visible answer at EOF' }],
|
||||
internal_chat_message_metadata_passthrough: { turn_id: 'turn-1' }
|
||||
}
|
||||
})).toEqual([]);
|
||||
|
||||
expect(getAgentMessages(convert.finalize())).toEqual([{
|
||||
type: 'message',
|
||||
message: 'visible answer at EOF',
|
||||
id: 'final-at-eof'
|
||||
}]);
|
||||
expect(convert.finalize()).toEqual([]);
|
||||
});
|
||||
|
||||
it('flushes a pending final before subsequent user activity', () => {
|
||||
const convert = createCodexEventConverter();
|
||||
|
||||
convert({ type: 'turn_context', payload: { turn_id: 'turn-1' } });
|
||||
expect(convert({
|
||||
type: 'response_item',
|
||||
payload: {
|
||||
type: 'message',
|
||||
id: 'visible-final',
|
||||
role: 'assistant',
|
||||
phase: 'final_answer',
|
||||
content: [{ type: 'output_text', text: 'visible final answer' }],
|
||||
internal_chat_message_metadata_passthrough: { turn_id: 'turn-1' }
|
||||
}
|
||||
})).toEqual([]);
|
||||
|
||||
expect(convert({
|
||||
type: 'event_msg',
|
||||
payload: { type: 'user_message', message: 'queued follow-up' }
|
||||
})).toEqual([{
|
||||
type: 'agent-message',
|
||||
turnId: 'turn-1',
|
||||
message: {
|
||||
type: 'message',
|
||||
message: 'visible final answer',
|
||||
id: 'visible-final'
|
||||
}
|
||||
}, {
|
||||
type: 'user-message',
|
||||
message: 'queued follow-up'
|
||||
}]);
|
||||
});
|
||||
|
||||
it('flushes a pending final before subsequent tool activity', () => {
|
||||
const convert = createCodexEventConverter();
|
||||
|
||||
convert({ type: 'turn_context', payload: { turn_id: 'turn-1' } });
|
||||
convert({
|
||||
type: 'response_item',
|
||||
payload: {
|
||||
type: 'message',
|
||||
id: 'visible-final',
|
||||
role: 'assistant',
|
||||
phase: 'final_answer',
|
||||
content: [{ type: 'output_text', text: 'visible final answer' }],
|
||||
internal_chat_message_metadata_passthrough: { turn_id: 'turn-1' }
|
||||
}
|
||||
});
|
||||
|
||||
expect(getAgentMessages(convert({
|
||||
type: 'response_item',
|
||||
payload: {
|
||||
type: 'function_call',
|
||||
name: 'ReadFile',
|
||||
call_id: 'call-read',
|
||||
arguments: '{"path":"README.md"}'
|
||||
}
|
||||
}))).toEqual([{
|
||||
type: 'message',
|
||||
message: 'visible final answer',
|
||||
id: 'visible-final'
|
||||
}, expect.objectContaining({
|
||||
type: 'tool-call',
|
||||
name: 'ReadFile',
|
||||
callId: 'call-read'
|
||||
})]);
|
||||
});
|
||||
|
||||
it('drops only a confirmed compaction summary and flushes earlier finals', () => {
|
||||
const convert = createCodexEventConverter();
|
||||
|
||||
convert({ type: 'turn_context', payload: { turn_id: 'turn-1' } });
|
||||
convert({
|
||||
type: 'response_item',
|
||||
payload: {
|
||||
type: 'message',
|
||||
id: 'earlier-visible-final',
|
||||
role: 'assistant',
|
||||
phase: 'final_answer',
|
||||
content: [{ type: 'output_text', text: 'earlier visible final answer' }],
|
||||
internal_chat_message_metadata_passthrough: { turn_id: 'turn-1' }
|
||||
}
|
||||
});
|
||||
convert({
|
||||
type: 'response_item',
|
||||
payload: {
|
||||
type: 'message',
|
||||
id: 'compaction-summary',
|
||||
role: 'assistant',
|
||||
phase: 'final_answer',
|
||||
content: [{ type: 'output_text', text: 'internal context summary for the next model' }],
|
||||
internal_chat_message_metadata_passthrough: { turn_id: 'turn-1' }
|
||||
}
|
||||
});
|
||||
|
||||
expect(getAgentMessages(convert({
|
||||
type: 'compacted',
|
||||
payload: {
|
||||
message: 'Compaction checkpoint prefix\ninternal context summary for the next model'
|
||||
}
|
||||
}))).toEqual([{
|
||||
type: 'message',
|
||||
message: 'earlier visible final answer',
|
||||
id: 'earlier-visible-final'
|
||||
}]);
|
||||
expect(convert({
|
||||
type: 'event_msg',
|
||||
payload: { type: 'task_complete', turn_id: 'turn-1' }
|
||||
})).toEqual([{ type: 'turn-finished', turnId: 'turn-1' }]);
|
||||
});
|
||||
|
||||
it('matches an indented compaction summary after the checkpoint prefix', () => {
|
||||
const convert = createCodexEventConverter();
|
||||
|
||||
convert({ type: 'turn_context', payload: { turn_id: 'turn-1' } });
|
||||
convert({
|
||||
type: 'response_item',
|
||||
payload: {
|
||||
type: 'message',
|
||||
id: 'indented-compaction-summary',
|
||||
role: 'assistant',
|
||||
phase: 'final_answer',
|
||||
content: [{ type: 'output_text', text: ' internal context summary' }],
|
||||
internal_chat_message_metadata_passthrough: { turn_id: 'turn-1' }
|
||||
}
|
||||
});
|
||||
|
||||
expect(convert({
|
||||
type: 'compacted',
|
||||
payload: { message: 'Compaction checkpoint prefix\n internal context summary' }
|
||||
})).toEqual([]);
|
||||
expect(convert({
|
||||
type: 'event_msg',
|
||||
payload: { type: 'task_complete', turn_id: 'turn-1' }
|
||||
})).toEqual([{ type: 'turn-finished', turnId: 'turn-1' }]);
|
||||
});
|
||||
|
||||
it('does not let token counts confirm a pending final', () => {
|
||||
const convert = createCodexEventConverter();
|
||||
|
||||
convert({ type: 'turn_context', payload: { turn_id: 'turn-1' } });
|
||||
convert({
|
||||
type: 'response_item',
|
||||
payload: {
|
||||
type: 'message',
|
||||
id: 'visible-final',
|
||||
role: 'assistant',
|
||||
phase: 'final_answer',
|
||||
content: [{ type: 'output_text', text: 'visible final answer' }],
|
||||
internal_chat_message_metadata_passthrough: { turn_id: 'turn-1' }
|
||||
}
|
||||
});
|
||||
|
||||
const tokenCount = convert({
|
||||
type: 'event_msg',
|
||||
payload: { type: 'token_count', info: { total_token_usage: {} } }
|
||||
});
|
||||
expect(getAgentMessages(tokenCount)).toEqual([
|
||||
expect.objectContaining({ type: 'token_count' })
|
||||
]);
|
||||
|
||||
const completed = convert({
|
||||
type: 'event_msg',
|
||||
payload: { type: 'task_complete', turn_id: 'turn-1' }
|
||||
});
|
||||
expect(getAgentMessages(completed)).toEqual([{
|
||||
type: 'message',
|
||||
message: 'visible final answer',
|
||||
id: 'visible-final'
|
||||
}]);
|
||||
});
|
||||
|
||||
it('keeps a pending visible final when remote compaction has no raw summary item', () => {
|
||||
const convert = createCodexEventConverter();
|
||||
|
||||
convert({
|
||||
type: 'turn_context',
|
||||
payload: { turn_id: 'turn-1' }
|
||||
});
|
||||
expect(convert({
|
||||
type: 'response_item',
|
||||
payload: {
|
||||
type: 'message',
|
||||
id: 'visible-before-remote-compaction',
|
||||
role: 'assistant',
|
||||
phase: 'final_answer',
|
||||
content: [{ type: 'output_text', text: 'visible before remote compaction' }],
|
||||
internal_chat_message_metadata_passthrough: { turn_id: 'turn-1' }
|
||||
}
|
||||
})).toEqual([]);
|
||||
expect(convert({
|
||||
type: 'compacted',
|
||||
payload: {
|
||||
message: '',
|
||||
replacement_history: [{
|
||||
type: 'message',
|
||||
role: 'user',
|
||||
content: [{ type: 'input_text', text: 'remote checkpoint summary' }]
|
||||
}]
|
||||
}
|
||||
})).toEqual([]);
|
||||
const completed = convert({
|
||||
type: 'event_msg',
|
||||
payload: { type: 'task_complete', turn_id: 'turn-1' }
|
||||
});
|
||||
expect(getAgentMessages(completed)).toEqual([{
|
||||
type: 'message',
|
||||
message: 'visible before remote compaction',
|
||||
id: 'visible-before-remote-compaction'
|
||||
}]);
|
||||
expect(completed.at(-1)).toEqual({ type: 'turn-finished', turnId: 'turn-1' });
|
||||
});
|
||||
|
||||
it('converts reasoning events', () => {
|
||||
|
||||
Reference in New Issue
Block a user