Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

Recap Codex's final answer, not its commentary

0xdiego25943394ae1137
diff --git a/cli/src/engines/codex/normalizer.spec.ts b/cli/src/engines/codex/normalizer.spec.ts
index 47be58033..8651a5cb2 100644
--- a/cli/src/engines/codex/normalizer.spec.ts
+++ b/cli/src/engines/codex/normalizer.spec.ts
@@ -121,6 +121,25 @@ describe('Codex rollout normalizer', () => {
])
})
+ it('keeps only the final answer of a turn that also carried commentary', () => {
+ // Codex 0.149: each AgentMessage item carries `phase` — `commentary` on the way, `final_answer` at
+ // the end. The recap read both and opened on "I'm about to ask you to choose a shape."
+ const line = (obj: unknown): string => JSON.stringify(obj)
+ const turn = [
+ line({ type: 'event_msg', payload: { type: 'item_completed', item: { type: 'UserMessage', content: [{ type: 'Text', text: 'Pick for me' }] } } }),
+ line({ type: 'event_msg', payload: { type: 'item_completed', item: { type: 'AgentMessage', phase: 'commentary', content: [{ type: 'Text', text: 'I’m about to ask you to choose a shape.' }] } } }),
+ line({ type: 'event_msg', payload: { type: 'item_completed', item: { type: 'AgentMessage', phase: 'final_answer', content: [{ type: 'Text', text: 'Square selected.' }] } } }),
+ ]
+ expect(lastCodexTurnText(turn)).toEqual({ userMessage: 'Pick for me', assistantText: 'Square selected.' })
+ // No phases at all (an older rollout): every message is the answer, as before.
+ const old = [
+ line({ type: 'event_msg', payload: { type: 'item_completed', item: { type: 'UserMessage', content: [{ type: 'Text', text: 'Pick for me' }] } } }),
+ line({ type: 'event_msg', payload: { type: 'item_completed', item: { type: 'AgentMessage', content: [{ type: 'Text', text: 'On it.' }] } } }),
+ line({ type: 'event_msg', payload: { type: 'item_completed', item: { type: 'AgentMessage', content: [{ type: 'Text', text: 'Square selected.' }] } } }),
+ ]
+ expect(lastCodexTurnText(old)).toEqual({ userMessage: 'Pick for me', assistantText: 'On it.\n\nSquare selected.' })
+ })
+
it('extracts the final user/assistant turn and uses stable line cursors', () => {
expect(lastCodexTurnText(fixture)).toEqual({ userMessage: 'Change the API', assistantText: 'The API is updated.' })
const window = windowCodexLines([...fixture, ...fixture], { limit: 4 })
diff --git a/cli/src/engines/codex/normalizer.ts b/cli/src/engines/codex/normalizer.ts
index 32f10c29d..94673217b 100644
--- a/cli/src/engines/codex/normalizer.ts
+++ b/cli/src/engines/codex/normalizer.ts
@@ -576,6 +576,20 @@ export function codexMessagesToEvents(
export function lastCodexTurnText(rawLines: string[]): LastTurnText | null {
let userMessage = ''
let assistantText = ''
+ // Codex marks each of its messages with a `phase`: `commentary` is what it says on the way — "I'll
+ // check the official page", "I'm about to ask you to choose" — and `final_answer` is the answer. A
+ // recap built from both opened on the announcement rather than the outcome (measured 2026-09-15: a
+ // turn that asked a question recapped as "I'll ask for the colour choice."). Only the final answer is
+ // the turn's text; commentary counts only when a rollout carries no phases at all (an older Codex).
+ let finalText = ''
+ let sawPhase = false
+ const take = (item: JsonObject, message: string): void => {
+ const phase = string(item.phase)
+ if (phase) sawPhase = true
+ if (phase === 'commentary') return
+ if (phase === 'final_answer') finalText += `${finalText ? '\n\n' : ''}${message}`
+ assistantText += `${assistantText ? '\n\n' : ''}${message}`
+ }
for (const line of rawLines) {
const raw = parse(line)
if (!raw) continue
@@ -591,13 +605,14 @@ export function lastCodexTurnText(rawLines: string[]): LastTurnText | null {
const itemType = string(item.type)
if (USER_TURN_TYPES.has(itemType)) {
const message = messageText(item)
- if (message) { userMessage = message; assistantText = '' }
+ if (message) { userMessage = message; assistantText = ''; finalText = ''; sawPhase = false }
} else if (AGENT_TEXT_TYPES.has(itemType)) {
const message = messageText(item)
- if (message) assistantText += `${assistantText ? '\n\n' : ''}${message}`
+ if (message) take(item, message)
}
}
- return assistantText ? { userMessage, assistantText } : null
+ const text = sawPhase && finalText ? finalText : assistantText
+ return text ? { userMessage, assistantText: text } : null
}
export function windowCodexLines(
diff --git a/cli/src/lib/summarize.spec.ts b/cli/src/lib/summarize.spec.ts
index 8347c649d..18b088e47 100644
--- a/cli/src/lib/summarize.spec.ts
+++ b/cli/src/lib/summarize.spec.ts
@@ -332,20 +332,13 @@ describe('the body under the headline', () => {
})
describe('deriveTurnSummary (the local recap)', () => {
- it('headlines the END of the answer, not its opening line', () => {
- // A Codex turn that asked a question: the engine's first message announces the question, its last
- // states the outcome a minute later. The terminal is showing the outcome when the recap lands.
- const text = 'I’ll ask for the colour choice.\n\nBlue selected.'
+ it('headlines the opening of the answer', () => {
+ const text = 'Blue selected.\n\nAnything else?'
expect(deriveTurnSummary(text)?.split('\n')[0]).toBe('Blue selected.')
})
- it('still skips a label at the top of the closing paragraph', () => {
- const text = 'Working on it.\n\nKết quả:\nĐã sửa xong file cấu hình.'
+ it('skips a label at the top', () => {
+ const text = 'Kết quả:\nĐã sửa xong file cấu hình.'
expect(deriveTurnSummary(text)?.split('\n')[0]).toBe('Đã sửa xong file cấu hình.')
})
-
- it('keeps the whole answer in the body', () => {
- const text = 'I’ll ask for the colour choice.\n\nBlue selected.'
- expect(deriveTurnSummary(text)?.split('\n\n')[1]).toBe('I’ll ask for the colour choice. Blue selected.')
- })
})
diff --git a/cli/src/lib/summarize.ts b/cli/src/lib/summarize.ts
index d9438e93d..1d3494c36 100644
--- a/cli/src/lib/summarize.ts
+++ b/cli/src/lib/summarize.ts
@@ -224,21 +224,13 @@ export function deriveTurnSummary(text: string): string | null {
const stripped = stripMarkdown(text)
const body = stripped.replace(/\s+/g, ' ').trim()
if (!body) return null
- // The LAST part of the answer, not the first. A turn's assistant text is every message the engine
- // wrote in it, oldest first (normalize.ts joins them with a blank line) — and an engine that asks a
- // question opens with "I'll ask for the colour choice." and closes, a minute later, with "Blue
- // selected." The headline is the conclusion, which is what the terminal is showing when the recap
- // lands; the body below still carries the whole answer.
- const recap = clip(firstProseLine(lastParagraph(stripped)) || body, RECAP_MAX_CHARS)
+ // The answer's OPENING is the headline. The engine-specific turn readers hand this the answer alone —
+ // Codex's `commentary` messages ("I'll check the page", "I'm about to ask") are dropped there, since
+ // a headline taken from those announced the work instead of stating the result.
+ const recap = clip(firstProseLine(stripped) || body, RECAP_MAX_CHARS)
return `${recap}\n\n${deriveTurnBody(text)}`
}
-/** The final blank-line-separated block of an answer that has any prose in it. */
-function lastParagraph(stripped: string): string {
- const paragraphs = stripped.split(/\n\s*\n/).map((p) => p.trim()).filter((p) => /[\p{L}\p{N}]/u.test(p))
- return paragraphs.at(-1) ?? stripped
-}
-
/** The `text` under a recap: the answer flattened to one line and clipped — a glance, never a
* paraphrase. Shared by both recap writers so the body reads the same whoever wrote the headline. */
export function deriveTurnBody(text: string): string {