fix(native-chat): keep the answer visible when a Stop hook fails (#42508)

* fix(native-chat): keep the answer visible when a Stop hook fails

A failed Stop hook follows a completed answer, but the turn fold treated
its red row as the turn's final answer, hiding the reply behind the
Worked-for disclosure. Mark failed Stop-hook rows with a dedicated
presentation so they stay visible beside the answer instead of
displacing it; turns that genuinely fail still end on their error.

Fixes #42487

* fix(native-chat): identify Claude Stop hooks from hook_event

hook_name carries the matcher-suffixed display name (for example
Stop:guard), so comparing it to 'stop' dropped the hook-failed
presentation for Stop hooks with a matcher. Read the bare event name
from hook_event and keep hook_name for display; a matcher-suffixed
hook_name without hook_event still counts so hook_name-only frames
keep working.

* fix(native-chat): read failed hooks from their frame and fold only mid-turn ones

Every failed hook, not just Stop, completes the turn, and a failing
PostModelRequest hook also lands after the answer. The fold now spots a
failed-hook row by the hook frame every such row already carries, so it
also works for rows from older hosts and rows already stored. Failures
that end a turn stay visible below the answer; earlier ones fold as work.

---------

Co-authored-by: Kelvin Amoaba <97001695+AmoabaKelvin@users.noreply.github.com>
This commit is contained in:
Benson LUO
2026-11-15 01:54:25 +00:00
committed by GitHub
co-authored by Kelvin Amoaba
parent 8f6a6262e1
commit e4e342e700
2 changed files with 66 additions and 3 deletions
@@ -17,6 +17,7 @@ function row(overrides: Partial<NativeChatTurnFoldRow> = {}): NativeChatTurnFold
reportsFailure: false,
explainsTurn: false,
sendsBack: false,
failedHook: false,
...overrides
}
}
@@ -24,6 +25,27 @@ function row(overrides: Partial<NativeChatTurnFoldRow> = {}): 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<string>()
@@ -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.
@@ -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<NativeChatMessage, NativeChatTurnFoldRow>()
/** 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<string>
}): NativeChatTurnFold {
const answers = nativeChatTurnAnswerRows(rows)
const lastWorkByTurn = new Map<string, number>()
for (const [index, row] of rows.entries()) {
if (row.turnKey !== undefined && row.draws && !row.failedHook) {
lastWorkByTurn.set(row.turnKey, index)
}
}
const foldedRows = new Set<number>()
const foldableTurnKeys = new Set<string>()
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)