Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/main/claude/__fixtures__/claude-hook-outcomes.jsonl
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"}}
10 changes: 10 additions & 0 deletions src/main/claude/claude-message-journaling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
readClaudeMessageEnvelope,
type ClaudeToolUse
} from './structured/journal/claude-structured-item-translation'
import { claudeEnvelopeStopHookFeedbackRow } from './claude-stop-hook-feedback'
import {
appendUnmodeledContent,
type ClaudeProviderFrameFallback
Expand Down Expand Up @@ -125,6 +126,15 @@ export function journalClaudeMessage(
}
changed = true
}
const stopHookFeedback = claudeEnvelopeStopHookFeedbackRow(envelope)
if (stopHookFeedback) {
ctx.turn.ensureOpen(message, source, observedAt)
ctx.sink.appendItem(identity, stopHookFeedback, {
...stamp(identity, stopHookFeedback),
lifecycle: true
})
changed = true
}
for (const tool of claudeToolUses(outputEnvelope)) {
ctx.turn.ensureOpen(message, source, observedAt)
ctx.tools.set(tool.id, tool)
Expand Down
33 changes: 33 additions & 0 deletions src/main/claude/claude-stop-hook-feedback.ts
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])
}
Comment on lines +8 to +19

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor · Functional Correctness · Quick win

Supported by static analysis

Script executed:

sed -n '1,100p' src/main/claude/claude-stop-hook-feedback.ts
sed -n '1,110p' src/main/native-chat/transcript-line-decoders-claude-stop-hook.test.ts
rg -n 'Stop hook feedback:' src/main/claude src/main/native-chat

Repository: stablyai/orca

Length of output: 5837


Script executed:

set -o pipefail
printf '%s\n' '--- changed file and comparable parser ---'
nl -ba src/main/claude/claude-stop-hook-feedback.ts | sed -n '1,80p'
nl -ba src/main/claude/claude-informational-row.ts | sed -n '1,80p'
printf '%s\n' '--- feedback callers and tests ---'
rg -n -F --glob '*.ts' -e 'claudeStopHookFeedbackRow' -e 'claudeEnvelopeStopHookFeedbackRow' -e 'STOP_HOOK_FEEDBACK' -e 'Stop hook feedback:' src/main/claude src/main/native-chat
printf '%s\n' '--- all fixture feedback payloads ---'
rg -n -F --glob '*.{jsonl,ts}' 'Stop hook feedback:' src/main/claude src/main/native-chat
printf '%s\n' '--- relevant transcript decoder blocks ---'
nl -ba src/main/native-chat/transcript-line-decoders-claude.ts | sed -n '90,185p'
nl -ba src/main/native-chat/transcript-line-decoders-claude-stop-hook.test.ts | sed -n '1,90p'

Repository: stablyai/orca

Length of output: 15685


Preserve literal Stop-hook reason text.

claudeStopHookFeedbackRow receives 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.


/** 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
}
32 changes: 32 additions & 0 deletions src/main/claude/claude-transcript-catch-up.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,38 @@ describe('Claude chat catch-up', () => {
expect(c.warn).not.toHaveBeenCalled()
})

it('keeps an outside Stop hook row inside the turn it sent back', async () => {
const c = await chat()
await c.live()
await c.write([
...chatTurn,
prompt('p2', 'a1', 'ask 2', 3),
reply('a2', 'p2', 'msg_b', { type: 'text', text: 'answer 2' }, 4),
{
...prompt('h2', 'a2', 'Stop hook feedback:\n[/hook.sh]: Cite evidence.\n', 5),
isMeta: true
},
reply('a3', 'h2', 'msg_c', { type: 'text', text: 'evidence' }, 6),
...bookkeeping
])
await c.open('a1')
expect(c.rows()).toEqual([
'user:ask 1',
'assistant:answer 1',
'(continued-outside)',
'user:ask 2',
'assistant:answer 2',
'(hook-sent-back)',
'assistant:evidence'
])
const scopes = c.journal
.snapshot()
.items.filter((item) => item.body.kind !== 'turn')
.slice(-3)
.map((item) => JSON.stringify(item.turnScope))
expect(new Set(scopes).size).toBe(1)
})

it('imports a later outside batch from the retained leaf without reusing its old divider', async () => {
const c = await chat()
await c.live()
Expand Down
11 changes: 5 additions & 6 deletions src/main/claude/claude-transcript-import-plan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,14 @@ import {
claudeText,
claudeThinkingIdentity,
claudeToolIdentity,
claudeToolResults,
claudeToolUses,
readClaudeMessageEnvelope
} from './structured/journal/claude-structured-item-translation'
import { createClaudeJournalTranslator } from './structured/journal/claude-structured-journal-translation'
import { createClaudeTranscriptItemReader } from './claude-transcript-item-reader'
import { idleClaudeJournalTurn } from './claude-session-state-turn-over'
import { claudeEnvelopeStopHookFeedbackRow } from './claude-stop-hook-feedback'

/** An isolated projection of final source items, compared with saved historical content. */
export function createClaudeTranscriptImportPlan(input: {
Expand Down Expand Up @@ -189,17 +191,14 @@ export function createClaudeTranscriptImportPlan(input: {
for (const tool of claudeToolUses(envelope)) {
matchSource([claudeToolIdentity(envelope.sessionId, tool.id)])
}
for (const part of envelope.content) {
const result = claudeRecord(part)
const toolUseId = result?.type === 'tool_result' ? claudeText(result.tool_use_id) : null
if (toolUseId) {
matchSource([claudeToolIdentity(envelope.sessionId, toolUseId)])
}
for (const result of claudeToolResults(envelope)) {
matchSource([claudeToolIdentity(envelope.sessionId, result.toolUseId)])
}
}
const prompt =
queued ||
(envelope?.role === 'user' &&
!claudeEnvelopeStopHookFeedbackRow(envelope) &&
envelope.content.some((part) => claudeRecord(part)?.type !== 'tool_result'))
const decoded = prompt
? decodeClaudeTranscriptLine(JSON.stringify(row), `${input.providerSessionId}:${uuid}`)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ describe('grouping a Codex thread restored from full history', () => {
draws: true,
outlivesTurn: false,
reportsFailure: false,
explainsTurn: false
explainsTurn: false,
sendsBack: false
})),
settledTurnKeys: new Set(bars.settledTurns.keys()),
expandedTurnKeys: new Set()
Expand Down
34 changes: 30 additions & 4 deletions src/main/native-chat/agent-session-wire/hook-outcome-rows.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ async function captured<T>(agent: 'codex' | 'claude', scenario: string): Promise
return lines.map((line) => JSON.parse(line)).filter((line) => line.case === scenario)
}

function settledRows(items: readonly AgentJournalRenderItem[]) {
function visibleRows(items: readonly AgentJournalRenderItem[]) {
expect(withNativeChatCutTurnNotices(items, { agentName: 'Agent' })).toBe(items)
const messages = projectStructuredItemsToNativeChat(items)
const turnKeys = nativeChatRowTurnKeys(messages)
Expand All @@ -70,9 +70,11 @@ function settledRows(items: readonly AgentJournalRenderItem[]) {
settledTurnKeys: new Set(turnKeys.flatMap((key) => (key ? [key] : []))),
expandedTurnKeys: new Set()
})
return messages.flatMap((message, index) =>
message.role === 'system' && !foldedRows.has(index) ? message.blocks : []
)
return messages.filter((_, index) => !foldedRows.has(index))
}

function settledRows(items: readonly AgentJournalRenderItem[]) {
return visibleRows(items).flatMap((message) => (message.role === 'system' ? message.blocks : []))
}

async function blockedChat(
Expand Down Expand Up @@ -211,3 +213,27 @@ describe('a prompt hook that blocked the message', () => {
expect(settledRows(items)).toEqual([row(`Blocked by hook: ${QA_POLICY}`)])
})
})

describe('a Stop hook that sent Claude back', () => {
it('keeps the reply before the hook row once the turn settles', async () => {
const frames = await captured<Captured>('claude', 'stop-hook-sent-back')
const { items } = await blockedChat(CLAUDE_CHAT, (rig) => {
const lane = createClaudeJournalTranslator({ sink: rig.eventSink, fallbackIdPrefix: '1' })
for (const { frame } of frames) {
lane.handle({ type: 'message', sessionId: 's', message: frame })
}
})
expect(visibleRows(items).map((message) => [message.role, message.blocks[0]])).toEqual([
['user', GO[0]],
['assistant', { type: 'text', text: 'Grass is green.' }],
[
'system',
expect.objectContaining({
tone: 'warning',
text: 'Stop hook feedback: Verify your statement with evidence before finishing.'
})
],
['assistant', { type: 'text', text: 'I checked: grass is green.' }]
])
})
})
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.' }]
])
})
})
15 changes: 15 additions & 0 deletions src/main/native-chat/transcript-line-decoders-claude.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import { claudeContentBlocks } from './transcript-record-blocks'
import { unwrapClaudePastedContentBlock } from '../../shared/claude-pasted-content'
import { unwrapClaudeBashInputBlock } from '../../shared/claude-bash-input'
import { claudeInterruptedMessageId } from './transcript-turn-markers'
import { claudeStopHookFeedbackRow } from '../claude/claude-stop-hook-feedback'
import { structuredAgentSessionStatusBlock } from '../../shared/agent-session/view/structured-agent-session-status-block'

const MAX_EDIT_PATCH_HUNKS = 40
const MAX_EDIT_PATCH_HUNK_LINES = 400
Expand Down Expand Up @@ -106,6 +108,10 @@ export function decodeClaudeTranscriptLine(
const isInjectedUserTurn =
role === 'user' &&
(record.isMeta === true || record.isSynthetic === true || record.isCompactSummary === true)
const stopHookFeedback = isInjectedUserTurn ? claudeStopHookFeedbackMessage(decodedBlocks) : null
if (stopHookFeedback) {
return { id: recordMessageId, timestamp, source: 'transcript', ...stopHookFeedback, ...parent }
}
// Keep image-source companions despite isMeta: prompt base64 has no path and cannot render attachments.
const blocks = isInjectedUserTurn
? isImageSourceRecord(decodedBlocks)
Expand Down Expand Up @@ -153,6 +159,15 @@ function claudeQueuedPromptAsUserRecord(
return { ...record, type: 'user', message: { role: 'user', content: prompt } }
}

function claudeStopHookFeedbackMessage(
blocks: NativeChatBlock[]
): Pick<NativeChatMessage, 'role' | 'blocks'> | null {
const [block] = blocks
const row =
blocks.length === 1 && block?.type === 'text' ? claudeStopHookFeedbackRow(block.text) : null
return row ? { role: 'system', blocks: [structuredAgentSessionStatusBlock(row)] } : null
}

// Mixed prose/markers are injected turns, never image-source companions.
function isImageSourceRecord(blocks: NativeChatBlock[]): boolean {
return (
Expand Down
21 changes: 17 additions & 4 deletions src/shared/agent-session-hook-outcome.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,33 @@
// Keep blocked/stopped prompt hooks as visible rows using the hook's reason, consistently across agents.
// Keep hook outcomes as visible rows using the hook's reason, consistently across agents.

import type { AgentJournalPlainStatusItem } from './agent-session/journal/agent-session-journal-types'
import { AGENT_SESSION_TURN_NOTICE_PRESENTATION } from './agent-session-turn-notice'

export type AgentSessionHookOutcome = 'blocked' | 'stopped'
/** A Stop hook sent the agent back to work, so the reply before this row was an answer. */
export const AGENT_SESSION_HOOK_SENT_BACK_PRESENTATION = 'hook-sent-back'

export type AgentSessionHookOutcome = 'blocked' | 'stopped' | 'sent-back'

const HOOK_OUTCOME_ROWS: Record<AgentSessionHookOutcome, { header: string; presentation: string }> =
{
blocked: { header: 'Blocked by hook', presentation: AGENT_SESSION_TURN_NOTICE_PRESENTATION },
stopped: { header: 'Hook stopped', presentation: AGENT_SESSION_TURN_NOTICE_PRESENTATION },
'sent-back': {
header: 'Stop hook feedback',
presentation: AGENT_SESSION_HOOK_SENT_BACK_PRESENTATION
}
}

export function hookOutcomeRowBody(
outcome: AgentSessionHookOutcome,
details: readonly string[]
): AgentJournalPlainStatusItem {
const header = outcome === 'blocked' ? 'Blocked by hook' : 'Hook stopped'
const { header, presentation } = HOOK_OUTCOME_ROWS[outcome]
const lines = details.map((detail) => detail.trim()).filter((detail) => detail.length > 0)
return {
kind: 'status',
tone: 'warning',
presentation: AGENT_SESSION_TURN_NOTICE_PRESENTATION,
presentation,
text:
lines.length === 0
? `${header}.`
Expand Down
15 changes: 8 additions & 7 deletions src/shared/native-chat/transcript/native-chat-turn-fold.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ function row(overrides: Partial<NativeChatTurnFoldRow> = {}): NativeChatTurnFold
outlivesTurn: false,
reportsFailure: false,
explainsTurn: false,
sendsBack: false,
...overrides
}
}
Expand All @@ -35,32 +36,32 @@ const NONE = new Set<string>()

describe('nativeChatTurnAnswerRows', () => {
it('names the last assistant row that renders prose, not the first', () => {
expect(nativeChatTurnAnswerRows(TURN).get('turn-1')).toBe(4)
expect([...nativeChatTurnAnswerRows(TURN)]).toEqual([4])
})

it('ignores rows that render no prose, so a trailing tool run is not the answer', () => {
const rows = [row({ role: 'user' }), row(), row({ rendersProse: false })]
expect(nativeChatTurnAnswerRows(rows).get('turn-1')).toBe(1)
expect([...nativeChatTurnAnswerRows(rows)]).toEqual([1])
})

it('ignores reasoning and system rows, which are never the agent answering', () => {
const rows = [row({ role: 'user' }), row(), row({ role: 'reasoning' }), row({ role: 'system' })]
expect(nativeChatTurnAnswerRows(rows).get('turn-1')).toBe(1)
expect([...nativeChatTurnAnswerRows(rows)]).toEqual([1])
})

it('names a failure report as the answer when nothing the agent said comes after it', () => {
const rows = [row({ role: 'user' }), row(), FAILURE]
expect(nativeChatTurnAnswerRows(rows).get('turn-1')).toBe(2)
expect([...nativeChatTurnAnswerRows(rows)]).toEqual([2])
})

it('names the later prose as the answer when the agent recovered from a failure', () => {
const rows = [row({ role: 'user' }), FAILURE, row()]
expect(nativeChatTurnAnswerRows(rows).get('turn-1')).toBe(2)
expect([...nativeChatTurnAnswerRows(rows)]).toEqual([2])
})

it('reports no answer for a turn that only ran tools', () => {
const rows = [row({ role: 'user' }), row({ rendersProse: false })]
expect(nativeChatTurnAnswerRows(rows).has('turn-1')).toBe(false)
expect(nativeChatTurnAnswerRows(rows).size).toBe(0)
})
})

Expand Down Expand Up @@ -215,6 +216,6 @@ describe('nativeChatTurnFold', () => {
expandedTurnKeys: NONE
})
expect([...foldedRows]).toEqual([1])
expect(nativeChatTurnAnswerRows(rows).get('turn-1')).toBe(3)
expect([...nativeChatTurnAnswerRows(rows)]).toEqual([3])
})
})
Loading
Loading