Skip to content

The agent that grows with you

fix(desktop): keep rejected submit restore out of newly focused composer

Wiktor Zając8fc6f60562e4
diff --git a/apps/desktop/src/app/chat/composer/hooks/use-composer-draft.ts b/apps/desktop/src/app/chat/composer/hooks/use-composer-draft.ts
index bf41c4ecb3c..c88a91c8910 100644
--- a/apps/desktop/src/app/chat/composer/hooks/use-composer-draft.ts
+++ b/apps/desktop/src/app/chat/composer/hooks/use-composer-draft.ts
@@ -612,6 +612,7 @@ export function useComposerDraft({
return {
activeQueueSessionKeyRef,
clearDraft,
+ draftScopeRef,
draftRef,
editorRef,
focusInput,
diff --git a/apps/desktop/src/app/chat/composer/hooks/use-composer-submit.test.tsx b/apps/desktop/src/app/chat/composer/hooks/use-composer-submit.test.tsx
index 14bb3c9cb36..cced81516aa 100644
--- a/apps/desktop/src/app/chat/composer/hooks/use-composer-submit.test.tsx
+++ b/apps/desktop/src/app/chat/composer/hooks/use-composer-submit.test.tsx
@@ -52,6 +52,7 @@ function renderSubmitHook({
}: SubmitHarnessOptions = {}) {
const resolvedSurfaceId = surfaceId === undefined ? `test-surface-${++surfaceSequence}` : surfaceId
const draftRef = { current: text }
+ const draftScopeRef = { current: sessionKey }
const editor = window.document.createElement('div')
editor.dataset.slot = 'composer-rich-input'
editor.textContent = text
@@ -107,6 +108,7 @@ function renderSubmitHook({
compacting,
clearDraft,
disabled: false,
+ draftScopeRef,
draftRef,
drainNextQueued: vi.fn(async () => false),
editorRef,
@@ -130,6 +132,7 @@ function renderSubmitHook({
return {
clearDraft,
+ draftScopeRef,
hook,
onCancel,
onSteer,
@@ -471,6 +474,58 @@ describe('useComposerSubmit busy-turn routing', () => {
expect(queueCurrentDraft).not.toHaveBeenCalled()
expect(onCancel).not.toHaveBeenCalled()
})
+
+ it('threads the loaded composer scope through onSubmit for the #59305 submit-time guard', async () => {
+ const { hook, onSubmit } = renderSubmitHook({ text: 'hello' })
+
+ act(() => {
+ hook.result.current.submitDraft()
+ })
+
+ await waitFor(() =>
+ expect(onSubmit).toHaveBeenCalledWith('hello', expect.objectContaining({ composerScope: 'stored-session' }))
+ )
+ })
+
+ it('keeps a rejected submit out of the composer after switching sessions', async () => {
+ let resolveSubmit!: (accepted: boolean) => void
+
+ const pendingSubmit = new Promise<boolean>(resolve => {
+ resolveSubmit = resolve
+ })
+
+ const { draftScopeRef, hook, loadIntoComposer, onSubmit, stashAt } = renderSubmitHook()
+ onSubmit.mockImplementationOnce(() => pendingSubmit)
+
+ act(() => {
+ hook.result.current.dispatchSubmit('draft from session A')
+ })
+
+ draftScopeRef.current = 'stored-session-b'
+
+ act(() => {
+ resolveSubmit(false)
+ })
+
+ await waitFor(() =>
+ expect(stashAt).toHaveBeenCalledWith('stored-session', 'draft from session A', [])
+ )
+ expect(loadIntoComposer).not.toHaveBeenCalled()
+ })
+
+ it('repaints a rejected submit while its composer remains loaded', async () => {
+ const { hook, loadIntoComposer, onSubmit, stashAt } = renderSubmitHook()
+ onSubmit.mockResolvedValueOnce(false)
+
+ act(() => {
+ hook.result.current.dispatchSubmit('draft from this session')
+ })
+
+ await waitFor(() =>
+ expect(stashAt).toHaveBeenCalledWith('stored-session', 'draft from this session', [])
+ )
+ expect(loadIntoComposer).toHaveBeenCalledWith('draft from this session', [])
+ })
})
describe('useComposerSubmit with a clarify parked on the session', () => {
diff --git a/apps/desktop/src/app/chat/composer/hooks/use-composer-submit.ts b/apps/desktop/src/app/chat/composer/hooks/use-composer-submit.ts
index e3512e7766c..ca376a39989 100644
--- a/apps/desktop/src/app/chat/composer/hooks/use-composer-submit.ts
+++ b/apps/desktop/src/app/chat/composer/hooks/use-composer-submit.ts
@@ -26,6 +26,7 @@ interface UseComposerSubmitArgs {
compacting: boolean
clearDraft: () => void
disabled: boolean
+ draftScopeRef: RefObject<string | null>
draftRef: RefObject<string>
drainNextQueued: () => Promise<boolean>
editorRef: RefObject<HTMLDivElement | null>
@@ -62,6 +63,7 @@ export function useComposerSubmit({
compacting,
clearDraft,
disabled,
+ draftScopeRef,
draftRef,
drainNextQueued,
editorRef,
@@ -85,18 +87,19 @@ export function useComposerSubmit({
const surfaceId = useComposerSurfaceId()
// Shared send primitive: fire onSubmit, and if the gateway rejects (accepted
- // === false) or throws, re-load + re-stash the draft so the words survive.
+ // === false) or throws, re-stash the draft so the words survive. Repaint it
+ // only while the same session still owns the visible composer; a late reject
+ // must not publish an old session's text into the newly focused one.
const dispatchSubmit = (text: string, attachments?: ComposerAttachment[], displayKind?: 'hidden') => {
- const submittedScope = activeQueueSessionKeyRef.current
+ const submittedScope = draftScopeRef.current
const submittedAttachments = attachments ?? []
const restore = () => {
- loadIntoComposer(text, submittedAttachments)
- // Use the scope captured at dispatch, not whatever session is focused
- // now — the gateway can reject well after the user has switched away,
- // and re-stashing into the currently-focused session would overwrite
- // its draft with the rejected text from a different session (#54527).
stashAt(submittedScope, text, submittedAttachments)
+
+ if (draftScopeRef.current === submittedScope) {
+ loadIntoComposer(text, submittedAttachments)
+ }
}
// A hidden submit is machine text (a setup note, never something the user
diff --git a/apps/desktop/src/app/chat/composer/index.tsx b/apps/desktop/src/app/chat/composer/index.tsx
index 7ebfb838275..aca7952e042 100644
--- a/apps/desktop/src/app/chat/composer/index.tsx
+++ b/apps/desktop/src/app/chat/composer/index.tsx
@@ -299,6 +299,7 @@ export function ChatBar({
const {
activeQueueSessionKeyRef,
clearDraft,
+ draftScopeRef,
draftRef,
editorRef,
focusInput,
@@ -440,6 +441,7 @@ export function ChatBar({
compacting,
clearDraft,
disabled,
+ draftScopeRef,
draftRef,
drainNextQueued,
editorRef,