From 6f155313ff169c9111a064f5bc6bb3ad17ae2364 Mon Sep 17 00:00:00 2001 From: Ananovo Date: Tue, 20 Oct 2026 07:21:21 +0800 Subject: [PATCH] fix(codex): keep status summaries out of intermediate messages (#2041) --- cli/src/codex/shared/projection.test.ts | 24 ++++++++++++ .../utils/appServerEventConverter.test.ts | 37 +++++++++++++++++++ .../codex/utils/appServerEventConverter.ts | 4 +- cli/src/codex/utils/assistantText.ts | 10 +++++ .../codex/utils/codexEventConverter.test.ts | 36 ++++++++++++++++++ cli/src/codex/utils/codexEventConverter.ts | 12 ++++-- .../common/sessionSummaryInstruction.test.ts | 25 +++---------- .../common/sessionSummaryInstruction.ts | 7 ++-- 8 files changed, 128 insertions(+), 27 deletions(-) create mode 100644 cli/src/codex/utils/assistantText.ts diff --git a/cli/src/codex/shared/projection.test.ts b/cli/src/codex/shared/projection.test.ts index 6695db40..0c0153b1 100644 --- a/cli/src/codex/shared/projection.test.ts +++ b/cli/src/codex/shared/projection.test.ts @@ -2,8 +2,32 @@ import { describe, expect, it, vi } from 'vitest'; import type { ApiSessionClient } from '@/api/apiSession'; import { SharedCodexProjection, inputText } from './projection'; import { codexPlanProposalId } from './plan'; +import { extractNotifySummary } from '@hapi/protocol/messages'; describe('shared history projection', () => { + it.each([undefined, 'root'])('publishes only final summaries in live output and history replay (parent: %s)', async parentThreadId => { + const send = vi.fn(); + const session = { getMetadata: () => ({}), sendAgentMessage: send } as unknown as ApiSessionClient; + const projection = new SharedCodexProjection(session, 'thread', async () => {}, parentThreadId); + const footer = 'AGENT_NOTIFY_SUMMARY {"status":"done","summary":"Finished"}'; + const items = [ + { id: 'progress', type: 'agentMessage', phase: 'commentary', text: `Still working.\n${footer}` }, + { id: 'partial', type: 'agentMessage', phase: 'partial_answer', text: `More work.\n${footer}` }, + { id: 'footer-only', type: 'agentMessage', phase: 'commentary', text: footer }, + { id: 'final', type: 'agentMessage', phase: 'final_answer', text: `Finished.\n${footer}` } + ]; + for (const item of items) { + await projection.notification('item/completed', { threadId: 'thread', turnId: 'turn', item }); + } + const live = send.mock.calls.slice(); + const bodies = live.map(([body]) => parentThreadId ? body.message : body); + expect(bodies.map(body => body.message)).toEqual(['Still working.', 'More work.', `Finished.\n${footer}`]); + expect(bodies.filter(body => extractNotifySummary(body.message))).toHaveLength(1); + projection.reset(); send.mockClear(); + await projection.history({ turns: [{ id: 'turn', status: 'completed', items }] }); + expect(send.mock.calls).toEqual(live); + }); + it.each([undefined, 'root'])('persists proposals without approval and replays the same IDs (parent: %s)', async parentThreadId => { const send = vi.fn(); const session = { getMetadata: () => ({}), sendAgentMessage: send } as unknown as ApiSessionClient; diff --git a/cli/src/codex/utils/appServerEventConverter.test.ts b/cli/src/codex/utils/appServerEventConverter.test.ts index 6898e58a..3252fa7a 100644 --- a/cli/src/codex/utils/appServerEventConverter.test.ts +++ b/cli/src/codex/utils/appServerEventConverter.test.ts @@ -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' } }); diff --git a/cli/src/codex/utils/appServerEventConverter.ts b/cli/src/codex/utils/appServerEventConverter.ts index 4ac506c1..0e33467b 100644 --- a/cli/src/codex/utils/appServerEventConverter.ts +++ b/cli/src/codex/utils/appServerEventConverter.ts @@ -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); } diff --git a/cli/src/codex/utils/assistantText.ts b/cli/src/codex/utils/assistantText.ts new file mode 100644 index 00000000..31784a1a --- /dev/null +++ b/cli/src/codex/utils/assistantText.ts @@ -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; +} diff --git a/cli/src/codex/utils/codexEventConverter.test.ts b/cli/src/codex/utils/codexEventConverter.test.ts index de94af45..dca55536 100644 --- a/cli/src/codex/utils/codexEventConverter.test.ts +++ b/cli/src/codex/utils/codexEventConverter.test.ts @@ -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', diff --git a/cli/src/codex/utils/codexEventConverter.ts b/cli/src/codex/utils/codexEventConverter.ts index 04df3cb6..c291f5b0 100644 --- a/cli/src/codex/utils/codexEventConverter.ts +++ b/cli/src/codex/utils/codexEventConverter.ts @@ -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; } diff --git a/cli/src/modules/common/sessionSummaryInstruction.test.ts b/cli/src/modules/common/sessionSummaryInstruction.test.ts index 5c8dd8b5..f7744708 100644 --- a/cli/src/modules/common/sessionSummaryInstruction.test.ts +++ b/cli/src/modules/common/sessionSummaryInstruction.test.ts @@ -9,16 +9,6 @@ import { withSessionSummaryInstruction } from './sessionSummaryInstruction' -const PREVIOUS_SESSION_SUMMARY_INSTRUCTION = [ - 'Session status summary:', - 'End every response with a single machine-readable status line (no backticks)', - 'so this workspace\'s session tracking can record progress. Put it on its own', - 'final line after all other content:', - 'AGENT_NOTIFY_SUMMARY {"version":1,"agent":"","project":"","status":"done|blocked|needs_review|needs_decision|failed|stalled","action":"<=12 words","summary":"one-line triage"}', - 'Use status "blocked" if unsure. Keep action to 12 words or fewer when status', - 'is "done" and follow-up remains.' -].join('\n') - const USER_LANGUAGE_INSTRUCTION = [ 'Use the language used by the user in the current conversation for the', 'human-readable "action" and "summary" values.' @@ -66,17 +56,14 @@ describe('sessionSummaryInstruction', () => { expect(body.toLowerCase()).not.toContain('surveillance') }) - it('adds user-language guidance without changing the existing prompt contract', () => { + it('limits summaries to the final answer while preserving language and footer guidance', () => { applyHubSessionSummaryContract(true) const body = sessionSummaryInstructionOrEmpty({}) - const previousLines = PREVIOUS_SESSION_SUMMARY_INSTRUCTION.split('\n') - const expected = [ - ...previousLines.slice(0, 5), - USER_LANGUAGE_INSTRUCTION, - ...previousLines.slice(5) - ].join('\n') - - expect(body).toBe(expected) + expect(body).toContain('At the end of each turn, append exactly one') + expect(body).toContain('final answer only') + expect(body).toContain('Do not emit this line in commentary, progress updates, or before tool calls.') + expect(body).toContain(USER_LANGUAGE_INSTRUCTION) + expect(body).not.toContain('End every response') }) it('preserves the exact machine-readable footer format', () => { diff --git a/cli/src/modules/common/sessionSummaryInstruction.ts b/cli/src/modules/common/sessionSummaryInstruction.ts index 244f53a4..f787ac4b 100644 --- a/cli/src/modules/common/sessionSummaryInstruction.ts +++ b/cli/src/modules/common/sessionSummaryInstruction.ts @@ -49,9 +49,10 @@ export const SESSION_SUMMARY_CONTRACT_LINE = export function buildSessionSummaryInstruction(): string { return [ 'Session status summary:', - 'End every response with a single machine-readable status line (no backticks)', - 'so this workspace\'s session tracking can record progress. Put it on its own', - 'final line after all other content:', + 'At the end of each turn, append exactly one machine-readable status line', + '(no backticks) to the final answer only, so this workspace\'s session tracking', + 'can record progress. Do not emit this line in commentary, progress updates, or before tool calls.', + 'Put it on its own final line after all other content:', SESSION_SUMMARY_CONTRACT_LINE, 'Use the language used by the user in the current conversation for the', 'human-readable "action" and "summary" values.',