mirror of
https://github.com/wu736139669/hapi.git
synced 2026-11-29 06:13:46 +00:00
fix(codex): keep status summaries out of intermediate messages (#2041)
This commit is contained in:
@@ -3,6 +3,43 @@ import { logger } from '@/ui/logger';
|
||||
import { AppServerEventConverter } from './appServerEventConverter';
|
||||
|
||||
describe('AppServerEventConverter', () => {
|
||||
const footer = 'AGENT_NOTIFY_SUMMARY {"status":"done","summary":"Finished"}';
|
||||
|
||||
it.each(['commentary', 'partial_answer'])('removes a summary footer from %s while preserving progress text', phase => {
|
||||
const converter = new AppServerEventConverter();
|
||||
converter.handleNotification('item/agentMessage/delta', { itemId: 'progress', delta: `Still working.\n${footer}` });
|
||||
expect(converter.handleNotification('item/completed', {
|
||||
item: { id: 'progress', type: 'agentMessage', phase }
|
||||
})).toEqual([{ type: 'agent_message', message: 'Still working.' }]);
|
||||
});
|
||||
|
||||
it.each(['final_answer', undefined, null, 'unknown'])('preserves summary footers when phase is %s', phase => {
|
||||
const converter = new AppServerEventConverter();
|
||||
const message = `Finished.\n${footer}`;
|
||||
expect(converter.handleNotification('item/completed', {
|
||||
item: { id: 'answer', type: 'agentMessage', text: message, phase }
|
||||
})).toEqual([{ type: 'agent_message', message }]);
|
||||
});
|
||||
|
||||
it('suppresses and deduplicates footer-only commentary without suppressing the final answer', () => {
|
||||
const converter = new AppServerEventConverter();
|
||||
const item = { id: 'progress', type: 'agentMessage', text: footer, phase: 'commentary' };
|
||||
expect(converter.handleNotification('item/completed', { item })).toEqual([]);
|
||||
expect(converter.handleNotification('item/completed', { item: { ...item, phase: undefined } })).toEqual([]);
|
||||
expect(converter.handleNotification('item/completed', {
|
||||
item: { ...item, id: 'final', phase: 'final_answer' }
|
||||
})).toEqual([{ type: 'agent_message', message: footer }]);
|
||||
});
|
||||
|
||||
it('leaves literal examples and malformed footers in commentary unchanged', () => {
|
||||
const converter = new AppServerEventConverter();
|
||||
for (const text of [`Example: ${footer}`, `${footer}\nMore explanation.`, 'AGENT_NOTIFY_SUMMARY {invalid}']) {
|
||||
expect(converter.handleNotification('item/completed', {
|
||||
item: { id: text, type: 'agentMessage', text, phase: 'commentary' }
|
||||
})).toEqual([{ type: 'agent_message', message: text }]);
|
||||
}
|
||||
});
|
||||
|
||||
it('maps thread/started', () => {
|
||||
const converter = new AppServerEventConverter();
|
||||
const events = converter.handleNotification('thread/started', { thread: { id: 'thread-1' } });
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { INCLUSIVE_INPUT_TOKEN_USAGE_MARKER } from '@hapi/protocol/usage';
|
||||
import { logger } from '@/ui/logger';
|
||||
import { codexAssistantText } from './assistantText';
|
||||
|
||||
type ConvertedEvent = {
|
||||
type: string;
|
||||
@@ -1003,7 +1004,8 @@ export class AppServerEventConverter {
|
||||
}
|
||||
const text = extractItemText(item) ?? this.agentMessageBuffers.get(itemId);
|
||||
if (text) {
|
||||
events.push(scoped({ type: 'agent_message', message: text }));
|
||||
const message = codexAssistantText(text, item.phase);
|
||||
if (message) events.push(scoped({ type: 'agent_message', message }));
|
||||
this.completedAgentMessageItems.add(itemId);
|
||||
this.agentMessageBuffers.delete(itemId);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
import { stripNotifySummaryFooter } from '@hapi/protocol/messages';
|
||||
|
||||
/** Keep turn summaries out of explicitly intermediate Codex output. */
|
||||
export function codexAssistantText(text: string, phase: unknown): string {
|
||||
if (phase === 'commentary' || phase === 'partial_answer') {
|
||||
return stripNotifySummaryFooter(text);
|
||||
}
|
||||
// Missing/unknown phases provide no evidence that a message is interim.
|
||||
return text;
|
||||
}
|
||||
@@ -11,6 +11,42 @@ function getAgentMessages(actions: CodexConversionAction[]): CodexMessage[] {
|
||||
}
|
||||
|
||||
describe('convertCodexEvent', () => {
|
||||
const footer = 'AGENT_NOTIFY_SUMMARY {"status":"done","summary":"Finished"}';
|
||||
const assistantEvents = (text: string, phase: string | null | undefined) => [
|
||||
{ type: 'event_msg', payload: { type: 'agent_message', message: text, phase } },
|
||||
{ type: 'event_msg', payload: { type: 'item_completed', item: { type: 'AgentMessage', id: 'item', text, phase } } },
|
||||
{ type: 'response_item', payload: { type: 'message', role: 'assistant', id: 'item', content: [{ type: 'output_text', text }], phase } }
|
||||
];
|
||||
|
||||
it.each(['commentary', 'partial_answer'])('removes intermediate summaries from every transcript message form (%s)', phase => {
|
||||
for (const event of assistantEvents(`Still working.\n${footer}`, phase)) {
|
||||
expect(convertCodexEvent(event)?.messages).toEqual([
|
||||
expect.objectContaining({ type: 'message', message: 'Still working.' })
|
||||
]);
|
||||
}
|
||||
});
|
||||
|
||||
it.each(['final_answer', undefined, null, 'unknown'])('preserves transcript summaries with phase %s', phase => {
|
||||
for (const event of assistantEvents(`Finished.\n${footer}`, phase)) {
|
||||
expect(convertCodexEvent(event)?.messages?.[0]).toMatchObject({ message: `Finished.\n${footer}` });
|
||||
}
|
||||
});
|
||||
|
||||
it('suppresses footer-only intermediate transcript messages', () => {
|
||||
for (const event of assistantEvents(footer, 'commentary')) {
|
||||
expect(convertCodexEvent(event)).toBeNull();
|
||||
}
|
||||
});
|
||||
|
||||
it.each([false, true])('deduplicates raw semantic/response mirrors after summary filtering (response first: %s)', responseFirst => {
|
||||
const converter = createCodexEventConverter();
|
||||
const events = assistantEvents(`Still working.\n${footer}`, 'commentary');
|
||||
const pair = [events[1], events[2]];
|
||||
if (responseFirst) pair.reverse();
|
||||
const messages = pair.flatMap(event => getAgentMessages(converter(event)));
|
||||
expect(messages).toEqual([expect.objectContaining({ type: 'message', message: 'Still working.' })]);
|
||||
});
|
||||
|
||||
it('extracts session_meta id', () => {
|
||||
const result = convertCodexEvent({
|
||||
type: 'session_meta',
|
||||
|
||||
@@ -2,6 +2,7 @@ import { randomUUID } from 'node:crypto';
|
||||
import { INCLUSIVE_INPUT_TOKEN_USAGE_MARKER, type InclusiveInputTokenUsageMarker } from '@hapi/protocol/usage';
|
||||
import { z } from 'zod';
|
||||
import { logger } from '@/ui/logger';
|
||||
import { codexAssistantText } from './assistantText';
|
||||
|
||||
const CodexSessionEventSchema = z.object({
|
||||
timestamp: z.string().optional(),
|
||||
@@ -502,8 +503,9 @@ export function convertCodexEvent(rawEvent: unknown): CodexEventProjection | nul
|
||||
}
|
||||
|
||||
if (eventType === 'agent_message') {
|
||||
const message = extractVisibleAssistantText(
|
||||
payloadRecord.message ?? payloadRecord.text ?? payloadRecord.content
|
||||
const message = codexAssistantText(
|
||||
extractVisibleAssistantText(payloadRecord.message ?? payloadRecord.text ?? payloadRecord.content),
|
||||
payloadRecord.phase
|
||||
);
|
||||
if (!message) {
|
||||
return null;
|
||||
@@ -532,7 +534,9 @@ export function convertCodexEvent(rawEvent: unknown): CodexEventProjection | nul
|
||||
}
|
||||
|
||||
if (itemType === 'agentmessage') {
|
||||
const message = extractVisibleAssistantText(item?.content ?? item?.message ?? item?.text);
|
||||
const message = codexAssistantText(
|
||||
extractVisibleAssistantText(item?.content ?? item?.message ?? item?.text), item?.phase
|
||||
);
|
||||
if (!message) return null;
|
||||
return {
|
||||
...(turnId ? { turnId } : {}),
|
||||
@@ -620,7 +624,7 @@ export function convertCodexEvent(rawEvent: unknown): CodexEventProjection | nul
|
||||
// semantic user events represent visible chat input.
|
||||
return null;
|
||||
}
|
||||
const message = extractVisibleAssistantText(payloadRecord.content);
|
||||
const message = codexAssistantText(extractVisibleAssistantText(payloadRecord.content), payloadRecord.phase);
|
||||
if (!message) {
|
||||
return null;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user