Repository navigation
fix(native-chat): place mid-turn and ! prompts where they were sent
#26582
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
Closed
pawel-kotowski
wants to merge
1
commit into
stablyai:main
from
pawel-kotowski:fix/native-chat-mid-turn-order
Closed
Changes from all commits
Commits
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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
88 changes: 88 additions & 0 deletions
88
src/main/native-chat/transcript-line-decoders-claude-queued-prompt.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,88 @@ | ||
| import { describe, expect, it } from 'vitest' | ||
| import { decodeClaudeTranscriptLine } from './transcript-line-decoders-claude' | ||
|
|
||
| // Shape captured from Claude Code 2.1.292 for a prompt absorbed mid-turn. | ||
| function queuedCommand(attachment: Record<string, unknown>): string { | ||
| return JSON.stringify({ | ||
| parentUuid: 'tool-result-row', | ||
| isSidechain: false, | ||
| attachment: { | ||
| type: 'queued_command', | ||
| source_uuid: 'cmd-1', | ||
| delivery_id: 'delivery-1', | ||
| timestamp: '2026-10-06T21:49:54.910Z', | ||
| ...attachment | ||
| }, | ||
| type: 'attachment', | ||
| uuid: 'queued-row', | ||
| timestamp: '2026-10-06T21:49:54.910Z', | ||
| rendered: [{ content: '<system-reminder>\nThe user sent a new message…</system-reminder>' }] | ||
| }) | ||
| } | ||
|
|
||
| describe('Claude queued_command attachment', () => { | ||
| it('decodes a prompt absorbed mid-turn as the user row Claude never writes', () => { | ||
| const line = queuedCommand({ | ||
| prompt: 'or you know what, use fable as orchestrator', | ||
| commandMode: 'prompt', | ||
| origin: { kind: 'human' }, | ||
| humanTurn: true | ||
| }) | ||
| expect(decodeClaudeTranscriptLine(line, 'fallback')).toEqual({ | ||
| id: 'queued-row', | ||
| role: 'user', | ||
| blocks: [{ type: 'text', text: 'or you know what, use fable as orchestrator' }], | ||
| timestamp: Date.parse('2026-10-06T21:49:54.910Z'), | ||
| source: 'transcript', | ||
| parentId: 'tool-result-row' | ||
| }) | ||
| }) | ||
|
|
||
| it('decodes legacy entries without an origin and content-block prompts', () => { | ||
| const decoded = decodeClaudeTranscriptLine( | ||
| queuedCommand({ | ||
| prompt: [{ type: 'text', text: 'is this true?' }], | ||
| commandMode: 'prompt' | ||
| }), | ||
| 'fallback' | ||
| ) | ||
| expect(decoded?.role).toBe('user') | ||
| expect(decoded?.blocks).toEqual([{ type: 'text', text: 'is this true?' }]) | ||
| }) | ||
|
|
||
| it.each([ | ||
| { prompt: '<task-notification>done</task-notification>', commandMode: 'task-notification' }, | ||
| { | ||
| prompt: '<task-notification>done</task-notification>', | ||
| commandMode: 'task-notification', | ||
| origin: { kind: 'task-notification' } | ||
| }, | ||
| { prompt: 'from another agent', commandMode: 'prompt', origin: { kind: 'peer' } }, | ||
| { commandMode: 'prompt', origin: { kind: 'human' } } | ||
| ])('drops queue entries the user did not type (%o)', (attachment) => { | ||
| expect(decodeClaudeTranscriptLine(queuedCommand(attachment), 'fallback')).toBeNull() | ||
| }) | ||
|
|
||
| it('still drops other attachment records', () => { | ||
| const line = JSON.stringify({ | ||
| type: 'attachment', | ||
| uuid: 'a', | ||
| attachment: { type: 'edited_text_file', filename: 'x.ts' } | ||
| }) | ||
| expect(decodeClaudeTranscriptLine(line, 'fallback')).toBeNull() | ||
| }) | ||
| }) | ||
|
|
||
| describe('Claude `!` shell command row', () => { | ||
| it('decodes as the line the user typed so its echo can match', () => { | ||
| const line = JSON.stringify({ | ||
| type: 'user', | ||
| uuid: 'bash-row', | ||
| timestamp: '2026-10-06T10:00:00.000Z', | ||
| message: { role: 'user', content: '<bash-input> gcloud auth login</bash-input>' } | ||
| }) | ||
| expect(decodeClaudeTranscriptLine(line, 'fallback')?.blocks).toEqual([ | ||
| { type: 'text', text: '! gcloud auth login' } | ||
| ]) | ||
| }) | ||
| }) |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| import { describe, expect, it } from 'vitest' | ||
| import { claudeBashInputAsTypedCommand } from './claude-bash-input' | ||
|
|
||
| describe('claudeBashInputAsTypedCommand', () => { | ||
| it('restores the line as typed, keeping the space after `!`', () => { | ||
| expect(claudeBashInputAsTypedCommand('<bash-input> gcloud auth login</bash-input>')).toBe( | ||
| '! gcloud auth login' | ||
| ) | ||
| expect(claudeBashInputAsTypedCommand('<bash-input>ls -la</bash-input>')).toBe('!ls -la') | ||
| }) | ||
| it.each([ | ||
| 'run <bash-input>ls</bash-input>', | ||
| '<bash-input>ls</bash-input> and more', | ||
| '<bash-stdout>ok</bash-stdout>', | ||
| '<bash-input>a</bash-input><bash-input>b</bash-input>' | ||
| ])('leaves anything else alone (%s)', (text) => { | ||
| expect(claudeBashInputAsTypedCommand(text)).toBeNull() | ||
| }) | ||
| }) |
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,18 @@ | ||
| import type { NativeChatBlock } from './native-chat-types' | ||
|
|
||
| const BASH_INPUT = /^\s*<bash-input>([\s\S]*)<\/bash-input>\s*$/ | ||
|
|
||
| /** Claude records a `!` shell command as `<bash-input>cmd</bash-input>`, with the `!` | ||
| * stripped. Returns the line as typed, or null when the text is anything else. */ | ||
| export function claudeBashInputAsTypedCommand(text: string): string | null { | ||
| const command = BASH_INPUT.exec(text)?.[1] | ||
| return command === undefined || command.includes('<bash-input>') ? null : `!${command}` | ||
| } | ||
|
|
||
| export function unwrapClaudeBashInputBlock(block: NativeChatBlock): NativeChatBlock { | ||
| if (block.type !== 'text') { | ||
| return block | ||
| } | ||
| const typed = claudeBashInputAsTypedCommand(block.text) | ||
| return typed === null ? block : { ...block, text: typed } | ||
| } |
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.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Require a single Bash-input block before converting the user row.
If a user row contains a
<bash-input>text block and another text block, this map converts the first block to a!command.isClaudeBashInputRecorddoes not recognize that multi-block row and emits a working turn. Apply the single-envelope condition to the complete row so display and lifecycle decoding use the same boundary.