Skip to content

The agent that grows with you

fix(desktop): read the live composer draft for the recall guard

DavidMetcalfebc62b270ed8d
diff --git a/apps/desktop/src/app/chat/composer/composer-utils.test.ts b/apps/desktop/src/app/chat/composer/composer-utils.test.ts
index 5f523e844f2..f4f00c1e9fe 100644
--- a/apps/desktop/src/app/chat/composer/composer-utils.test.ts
+++ b/apps/desktop/src/app/chat/composer/composer-utils.test.ts
@@ -5,6 +5,7 @@ import {
acceptsTriggerCompletion,
implicitSlashAcceptIndex,
isPendingDraftPersistCurrent,
+ liveComposerDraft,
type PendingDraftPersist,
pickPlaceholder,
shouldDisableComposerInput,
@@ -13,6 +14,7 @@ import {
slashCommandToken,
type TriggerAcceptInput
} from './composer-utils'
+import { normalizeComposerEditorDom, RICH_INPUT_SLOT } from './rich-editor'
const item = (group: string): Unstable_TriggerItem =>
({ id: 'x', type: 'slash', label: 'x', metadata: { group } }) as unknown as Unstable_TriggerItem
@@ -177,3 +179,56 @@ describe('isPendingDraftPersistCurrent (#54527 integrity guard)', () => {
expect(isPendingDraftPersistCurrent(null, null)).toBe(false)
})
})
+
+/** Real contentEditable, built the way `empty-composer.test.ts` builds one. */
+function editorWith(text: string): HTMLDivElement {
+ const el = document.createElement('div')
+
+ el.dataset.slot = RICH_INPUT_SLOT
+ el.contentEditable = 'true'
+ el.append(document.createTextNode(text))
+ normalizeComposerEditorDom(el)
+ document.body.append(el)
+
+ return el
+}
+
+describe('liveComposerDraft (stale-mirror guard for the ArrowUp recall)', () => {
+ it('reads the live editor text even when the mirror is still empty', () => {
+ // The race this exists for: a keystroke or paste flushed only by the
+ // coalesced rAF, so `draftRef.current` holds the pre-keystroke text while
+ // the editor already holds what the user typed. The recall guard must see
+ // the typed text, not the stale empty mirror.
+ const editor = editorWith('just typed this')
+
+ expect(liveComposerDraft(editor, '')).toBe('just typed this')
+ })
+
+ it('prefers the editor over a stale non-empty mirror', () => {
+ const editor = editorWith('continued the draft')
+
+ expect(liveComposerDraft(editor, 'the draft before the keystroke')).toBe('continued the draft')
+ })
+
+ it('returns whitespace as-is so the recall guard trim() still sees it as blank', () => {
+ const editor = editorWith(' ')
+
+ // Not trimmed by the helper: the guard's own `currentDraft.trim()` decides
+ // blankness, and a whitespace-only editor must keep reading as blank.
+ expect(liveComposerDraft(editor, '')).toBe(' ')
+ expect(liveComposerDraft(editor, '').trim()).toBe('')
+ })
+
+ it.each([null, undefined])('falls back to the mirror when the editor is %s (pre-mount)', missing => {
+ expect(liveComposerDraft(missing, 'mirrored draft')).toBe('mirrored draft')
+ })
+
+ it('an empty editor reads as empty against a fresh mirror (behaviour unchanged)', () => {
+ const editor = editorWith('')
+
+ editor.replaceChildren()
+ normalizeComposerEditorDom(editor)
+
+ expect(liveComposerDraft(editor, '')).toBe('')
+ })
+})
diff --git a/apps/desktop/src/app/chat/composer/composer-utils.ts b/apps/desktop/src/app/chat/composer/composer-utils.ts
index 97852c31336..69e7b743206 100644
--- a/apps/desktop/src/app/chat/composer/composer-utils.ts
+++ b/apps/desktop/src/app/chat/composer/composer-utils.ts
@@ -5,6 +5,7 @@ import type { SlashChipKind } from '@/components/assistant-ui/directive-text'
import type { ComposerAttachment } from '@/store/composer'
import { setSessionPickerOpen } from '@/store/session'
+import { composerPlainText } from './rich-editor'
import type { TriggerState } from './text-utils'
export const COMPOSER_STACK_BREAKPOINT_PX = 320
@@ -217,3 +218,15 @@ export function isPendingDraftPersistCurrent(
): boolean {
return pending !== null && expected !== null && pending.scope === expected.scope && pending.text === expected.text
}
+
+/**
+ * The composer text a keystroke should decide from.
+ *
+ * `mirror` (the composer's draftRef) is refreshed by a coalesced per-frame
+ * flush, so within a frame of a keystroke or paste it still holds the previous
+ * text. A decision that can act on the draft — the sent-message recall guard
+ * replaces the composer — has to read the live editor instead.
+ */
+export function liveComposerDraft(editor: HTMLElement | null | undefined, mirror: string): string {
+ return editor ? composerPlainText(editor) : mirror
+}
diff --git a/apps/desktop/src/app/chat/composer/index.tsx b/apps/desktop/src/app/chat/composer/index.tsx
index cbfd9a9f2dc..b056b04ca38 100644
--- a/apps/desktop/src/app/chat/composer/index.tsx
+++ b/apps/desktop/src/app/chat/composer/index.tsx
@@ -37,6 +37,7 @@ import {
acceptsTriggerCompletion,
COMPOSER_FADE_BACKGROUND,
implicitSlashAcceptIndex,
+ liveComposerDraft,
type QueueEditState,
shouldDisableComposerInput,
slashArgStage
@@ -846,7 +847,9 @@ export function ChatBar({
// place) then sent-message history. The history ring is derived from live
// session messages each press — single source of truth, no mirror.
if (event.key === 'ArrowUp') {
- const currentDraft = draftRef.current
+ // Decide from the live editor: the mirror is a frame behind typing or a
+ // paste, and this branch can replace what the user just wrote.
+ const currentDraft = liveComposerDraft(editorRef.current, draftRef.current)
// Editing a queued turn → walk to the older entry.
if (queueEdit && stepQueuedEdit(-1)) {