Repository navigation
fix(native-chat): keep Claude's reply visible when a Stop hook sends it back #36944
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
91e428b
fix(native-chat): keep a Claude reply visible when a Stop hook sends …
AmoabaKelvin 1ebc859
fix(native-chat): keep the Stop hook row when catching up a Claude ch…
AmoabaKelvin ca3aac3
refactor(native-chat): one row table for hook outcomes
AmoabaKelvin 167b35a
fix(native-chat): import journal types from their new home
AmoabaKelvin 5c98f16
fix(native-chat): import moved translation and status-block modules f…
AmoabaKelvin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,6 @@ | ||
| {"case": "blocked-prompt", "frame": {"type": "system", "subtype": "informational", "content": "UserPromptSubmit operation blocked by hook:\n[/tmp/hook-capture/repo/block.sh]: Deploys are frozen\n\n\nOriginal prompt: BLOCKME please say hi", "level": "warning", "prevent_continuation": true, "prompt_submit_outcome": {"kind": "blocked_by_hook", "prompt_kept": false, "reason": "Deploys are frozen"}, "uuid": "00000000-0000-4000-8000-000000000001", "session_id": "00000000-0000-4000-8000-000000000002"}} | ||
| {"case": "blocked-prompt", "frame": {"type": "result", "subtype": "success", "is_error": false, "num_turns": 0, "result": "UserPromptSubmit operation blocked by hook:\n[/tmp/hook-capture/repo/block.sh]: Deploys are frozen\n\n\nOriginal prompt: BLOCKME please say hi", "stop_reason": null, "session_id": "00000000-0000-4000-8000-000000000002", "uuid": "00000000-0000-4000-8000-000000000003", "duration_ms": 33, "duration_api_ms": 0}} | ||
| {"case": "stop-hook-sent-back", "frame": {"type": "assistant", "message": {"id": "msg_10", "type": "message", "role": "assistant", "model": "claude-haiku-5-5", "content": [{"type": "text", "text": "Grass is green."}], "stop_reason": null, "usage": {"input_tokens": 6, "output_tokens": 10}}, "parent_tool_use_id": null, "session_id": "00000000-0000-4000-8000-000000000002", "uuid": "00000000-0000-4000-8000-000000000010"}} | ||
| {"case": "stop-hook-sent-back", "frame": {"type": "user", "message": {"role": "user", "content": [{"type": "text", "text": "Stop hook feedback:\nVerify your statement with evidence before finishing."}]}, "parent_tool_use_id": null, "timestamp": "2026-11-01T05:34:54.630Z", "isSynthetic": true, "session_id": "00000000-0000-4000-8000-000000000002", "uuid": "00000000-0000-4000-8000-000000000011"}} | ||
| {"case": "stop-hook-sent-back", "frame": {"type": "assistant", "message": {"id": "msg_12", "type": "message", "role": "assistant", "model": "claude-haiku-5-5", "content": [{"type": "text", "text": "I checked: grass is green."}], "stop_reason": null, "usage": {"input_tokens": 6, "output_tokens": 10}}, "parent_tool_use_id": null, "session_id": "00000000-0000-4000-8000-000000000002", "uuid": "00000000-0000-4000-8000-000000000012"}} | ||
| {"case": "stop-hook-sent-back", "frame": {"type": "result", "subtype": "success", "is_error": false, "num_turns": 3, "result": "I checked: grass is green.", "stop_reason": "end_turn", "duration_ms": 12518, "duration_api_ms": 13517, "session_id": "00000000-0000-4000-8000-000000000002", "uuid": "00000000-0000-4000-8000-000000000013"}} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| import { hookOutcomeRowBody } from '../../shared/agent-session-hook-outcome' | ||
| import type { AgentJournalPlainStatusItem } from '../../shared/agent-session/journal/agent-session-journal-types' | ||
| import { | ||
| claudeRecord, | ||
| claudeText, | ||
| type ClaudeMessageEnvelope | ||
| } from './structured/journal/claude-structured-item-translation' | ||
|
|
||
| // Claude injects a blocking Stop hook's reason as a meta user turn; exit-2 hooks prefix it with their command. | ||
| const STOP_HOOK_FEEDBACK = /^Stop hook feedback:\n/ | ||
|
|
||
| /** The row for Claude's injected Stop-hook feedback text, or null for any other text. */ | ||
| export function claudeStopHookFeedbackRow(text: string): AgentJournalPlainStatusItem | null { | ||
| if (!STOP_HOOK_FEEDBACK.test(text)) { | ||
| return null | ||
| } | ||
| const reason = text.replace(STOP_HOOK_FEEDBACK, '').replace(/^\[[^\]\n]+\]: /gm, '') | ||
| return hookOutcomeRowBody('sent-back', [reason]) | ||
| } | ||
|
|
||
| /** The same row from a root-agent envelope; it continues the turn, so importers must not treat it as a prompt. */ | ||
| export function claudeEnvelopeStopHookFeedbackRow( | ||
| envelope: ClaudeMessageEnvelope | ||
| ): AgentJournalPlainStatusItem | null { | ||
| const [part] = envelope.content | ||
| const text = claudeText(claudeRecord(part)?.text) | ||
| return envelope.isInjectedUserTurn && | ||
| envelope.parentToolUseId === null && | ||
| envelope.content.length === 1 && | ||
| text | ||
| ? claudeStopHookFeedbackRow(text) | ||
| : null | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
src/main/native-chat/transcript-line-decoders-claude-stop-hook.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| import { describe, expect, it } from 'vitest' | ||
| import { nativeChatRowTurnKeys } from '../../shared/native-chat/transcript/native-chat-turn-grouping' | ||
| import { | ||
| nativeChatTurnFold, | ||
| nativeChatTurnFoldRow | ||
| } from '../../shared/native-chat/transcript/native-chat-turn-fold' | ||
| import { decodeClaudeTranscriptLine } from './transcript-line-decoders-claude' | ||
|
|
||
| // Text rows of a Claude Code 2.1.316 turn whose Stop hook blocked once. | ||
| function stopHookTurn(feedback: string): string[] { | ||
| return [ | ||
| { type: 'user', uuid: 'prompt', message: { role: 'user', content: 'Say: the sky is blue' } }, | ||
| { | ||
| type: 'assistant', | ||
| uuid: 'reply', | ||
| message: { role: 'assistant', content: [{ type: 'text', text: 'The sky is blue.' }] } | ||
| }, | ||
| { | ||
| type: 'user', | ||
| uuid: 'feedback', | ||
| isMeta: true, | ||
| message: { role: 'user', content: `Stop hook feedback:\n${feedback}` } | ||
| }, | ||
| { | ||
| type: 'assistant', | ||
| uuid: 'continuation', | ||
| message: { role: 'assistant', content: [{ type: 'text', text: 'Evidence: I checked.' }] } | ||
| } | ||
| ].map((record) => JSON.stringify({ timestamp: '2026-11-01T05:33:22.035Z', ...record })) | ||
| } | ||
|
|
||
| describe('a Claude turn its Stop hook sent back', () => { | ||
| it.each([ | ||
| ['a JSON block decision', 'Verify your statement with evidence.'], | ||
| ['an exit-2 hook', '[/tmp/hook.sh]: Verify your statement with evidence.\n'] | ||
| ])('keeps the reply and the hook row visible once settled (%s)', (_, feedback) => { | ||
| const messages = stopHookTurn(feedback).flatMap( | ||
| (line) => decodeClaudeTranscriptLine(line, 'fallback') ?? [] | ||
| ) | ||
| const turnKeys = nativeChatRowTurnKeys(messages) | ||
| const { foldedRows } = nativeChatTurnFold({ | ||
| rows: messages.map((message, index) => | ||
| nativeChatTurnFoldRow(message, turnKeys[index], false) | ||
| ), | ||
| settledTurnKeys: new Set(['prompt']), | ||
| expandedTurnKeys: new Set() | ||
| }) | ||
| expect( | ||
| messages.flatMap((message, index) => | ||
| foldedRows.has(index) ? [] : [[message.role, message.blocks[0]]] | ||
| ) | ||
| ).toEqual([ | ||
| ['user', { type: 'text', text: 'Say: the sky is blue' }], | ||
| ['assistant', { type: 'text', text: 'The sky is blue.' }], | ||
| [ | ||
| 'system', | ||
| expect.objectContaining({ | ||
| text: 'Stop hook feedback: Verify your statement with evidence.', | ||
| tone: 'warning' | ||
| }) | ||
| ], | ||
| ['assistant', { type: 'text', text: 'Evidence: I checked.' }] | ||
| ]) | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Script executed:
Repository: stablyai/orca
Length of output: 5837
Script executed:
Repository: stablyai/orca
Length of output: 15685
Preserve literal Stop-hook reason text.
claudeStopHookFeedbackRowreceives no exit-status or wrapper-provenance field. Therefore a first-line-only regex is not safe: it also removes a literal JSON-block reason such as[the user has typed the word banana in a later message]: .... Keep literal reason prefixes intact and strip a generated command wrapper only when its provenance is available from the producer or an explicit marker. The current global replacement must not remain.