diff --git a/src/shared/native-chat/transcript/folding/native-chat-turn-fold.test.ts b/src/shared/native-chat/transcript/folding/native-chat-turn-fold.test.ts index c1fd989fdfd..a687d7ab1f3 100644 --- a/src/shared/native-chat/transcript/folding/native-chat-turn-fold.test.ts +++ b/src/shared/native-chat/transcript/folding/native-chat-turn-fold.test.ts @@ -17,6 +17,7 @@ function row(overrides: Partial = {}): NativeChatTurnFold reportsFailure: false, explainsTurn: false, sendsBack: false, + failedHook: false, ...overrides } } @@ -24,6 +25,27 @@ function row(overrides: Partial = {}): NativeChatTurnFold /** An error-toned system row: the agent or host reporting the turn failed. */ const FAILURE = row({ role: 'system', reportsFailure: true }) +function hookFailure(id: string, event: string): NativeChatMessage { + return { + id, + role: 'system', + timestamp: 1, + source: 'transcript', + blocks: [ + { + type: 'text', + text: `Codex ${event} hook failed: hook exited with code 1`, + tone: 'error', + providerFrame: { + provider: 'codex', + kind: 'notification:hook/completed', + payload: { head: '{}', byteLength: 2, digest: 'fixture', truncated: false } + } + } + ] + } +} + const SETTLED = new Set(['turn-1']) const NONE = new Set() @@ -96,6 +118,26 @@ describe('rendered plans in collapsed turns', () => { }) }) +describe('failed hooks in a settled turn', () => { + it('keeps the answer and the hook failures after it visible, folding earlier ones as work', () => { + const messages = [ + PROMPT, + WORK, + hookFailure('post-tool', 'PostToolUse'), + ANSWER, + hookFailure('post-model', 'PostModelRequest'), + hookFailure('stop', 'Stop') + ] + const rows = messages.map((item) => nativeChatTurnFoldRow(item, 'turn-1', false)) + const { foldedRows } = nativeChatTurnFold({ + rows, + settledTurnKeys: SETTLED, + expandedTurnKeys: NONE + }) + expect([...foldedRows].sort()).toEqual([1, 2]) + }) +}) + describe('nativeChatTurnFold', () => { // A stored-only provider event draws nothing: a disclosure over it would open onto nothing. diff --git a/src/shared/native-chat/transcript/folding/native-chat-turn-fold.ts b/src/shared/native-chat/transcript/folding/native-chat-turn-fold.ts index 5122a87ce4e..4bcd42b3f2d 100644 --- a/src/shared/native-chat/transcript/folding/native-chat-turn-fold.ts +++ b/src/shared/native-chat/transcript/folding/native-chat-turn-fold.ts @@ -1,5 +1,6 @@ // Derive settled-turn answers from the last assistant prose or system failure, plus the prose a Stop hook sent back; the journal has no answer marker. // Earlier work/recovered errors fold behind that answer, while failed turns end visibly on their error. +// Failed hooks never fail the turn: those that end it stay visible below the answer, earlier ones fold as work. import { AGENT_SESSION_COMPACTION_SKIPPED_PRESENTATION } from '../../../agent-session/agent-session-compaction' import { AGENT_SESSION_HOOK_SENT_BACK_PRESENTATION } from '../../../agent-session/journal/agent-session-hook-outcome' @@ -12,6 +13,7 @@ import { isGeneratedImageOperation } from '../../native-chat-generated-image' import { isBackgroundTaskBlock, isSubagentGroupBlock, + type NativeChatBlock, type NativeChatMessage, type NativeChatRole } from '../../native-chat-types' @@ -33,10 +35,21 @@ export type NativeChatTurnFoldRow = { explainsTurn: boolean /** A Stop hook sent the agent back after it answered, so the prose before this row is an answer too. */ sendsBack: boolean + /** A hook failed; it reports on the turn without ending it. */ + failedHook: boolean } const rowFacts = new WeakMap() +/** Hosts store a failed hook as a red row carrying the hook's frame; the turn itself completed. */ +function isFailedHookBlock(block: NativeChatBlock): boolean { + const frame = block.type === 'text' && block.tone === 'error' ? block.providerFrame : undefined + return ( + (frame?.provider === 'codex' && frame.kind === 'notification:hook/completed') || + (frame?.provider === 'claude' && frame.kind === 'message:system:hook_response') + ) +} + /** How the fold reads a transcript row, with the same derivation the row renders from. */ export function nativeChatTurnFoldRow( message: NativeChatMessage, @@ -76,7 +89,8 @@ function deriveTurnFoldRow( (block) => block.type === 'text' && (block.tone === 'error' || block.failure?.kind === 'usageLimitReached') && - block.presentation !== AGENT_SESSION_ORCA_STOP_PRESENTATION + block.presentation !== AGENT_SESSION_ORCA_STOP_PRESENTATION && + !isFailedHookBlock(block) ), explainsTurn: message.blocks.some( @@ -94,7 +108,8 @@ function deriveTurnFoldRow( sendsBack: message.blocks.some( (block) => block.type === 'text' && block.presentation === AGENT_SESSION_HOOK_SENT_BACK_PRESENTATION - ) + ), + failedHook: message.blocks.some(isFailedHookBlock) } } @@ -149,6 +164,12 @@ export function nativeChatTurnFold({ expandedTurnKeys: ReadonlySet }): NativeChatTurnFold { const answers = nativeChatTurnAnswerRows(rows) + const lastWorkByTurn = new Map() + for (const [index, row] of rows.entries()) { + if (row.turnKey !== undefined && row.draws && !row.failedHook) { + lastWorkByTurn.set(row.turnKey, index) + } + } const foldedRows = new Set() const foldableTurnKeys = new Set() for (const [index, row] of rows.entries()) { @@ -165,7 +186,7 @@ export function nativeChatTurnFold({ continue } // No-prose turns fold wholly behind the status anchor instead of leaving every command expanded. - if (answers.has(index)) { + if (answers.has(index) || (row.failedHook && index > (lastWorkByTurn.get(turnKey) ?? -1))) { continue } foldableTurnKeys.add(turnKey)