fix(desktop): keep the boot-time draft when the cold-start resume remounts the composer

Text typed on the fresh chat while the app boots must follow the chat that
resume-last-session restores (a4b48064b1, #114122). The handoff only ran
when one composer instance saw its scope flip from the fresh key to the
restored id. The restored session's loading view mounts no composer, so the
fresh chat's composer unmounts and a new one mounts already scoped to the
restored id: its first run never saw a fresh scope, never consumed the
announcement, and the typed text vanished from the composer.

The swap effect's first run now also adopts the fresh draft when the
pending announcement names its own key; a first mount on any other session
leaves the announcement for the restored chat's composer.

0.23.2 release blocker: install-e2e linux installer-script+desktop
(v2026.8.13 -> HEAD), "The composer must hold the typed prompt before
Enter", Received "".
This commit is contained in:
teknium1
2026-11-16 09:24:32 -08:00
committed by Teknium
parent 477d72105b
commit b8dd611d4a
3 changed files with 66 additions and 3 deletions

View File

@@ -6,6 +6,7 @@ import { PaneVisibleContext } from '@/components/pane-shell/pane-visibility'
import {
$freshDraftKey,
$restoredDraftNotice,
adoptNewSessionDraft,
announceGoneSessionDraft,
announceNewSessionDraftKey,
clearSessionDraft,
@@ -208,6 +209,46 @@ describe('useComposerDraft — attachment scope stays coherent with the committe
clearSessionDraft('session-created')
})
it('carries a pre-session draft onto the restored session when the composer remounts across the cold-start resume', () => {
// Typed on the fresh chat while the backend was still coming up.
const fresh = render(<ProbeHarness activeQueueSessionKey={null} onLayoutSnapshot={() => undefined} sessionId="" />)
stashSessionDraft(null, 'typed while booting', [])
// Cold-start resume-last-session announces the restored id and navigates;
// the routed session's loading view mounts no composer, so the fresh chat's
// composer unmounts and a NEW one mounts already scoped to the restored id.
announceNewSessionDraftKey('session-created')
fresh.unmount()
mockComposerApi.setText.mockClear()
render(
<ProbeHarness
activeQueueSessionKey="session-created"
onLayoutSnapshot={() => undefined}
sessionId="session-created"
/>
)
expect(takeSessionDraft('session-created').text).toBe('typed while booting')
expect(takeSessionDraft(null).text).toBe('')
expect(mockComposerApi.setText).toHaveBeenCalledWith('typed while booting')
})
it('leaves the pre-session draft alone when a composer mounts on a session nobody announced', () => {
stashSessionDraft(null, 'still composing a new chat', [])
// Another chat's restore is pending; a composer mounting elsewhere must not eat it.
announceNewSessionDraftKey('session-created')
render(<ProbeHarness activeQueueSessionKey="session-A" onLayoutSnapshot={() => undefined} sessionId="session-A" />)
expect(takeSessionDraft('session-A').text).toBe('')
expect(takeSessionDraft(null).text).toBe('still composing a new chat')
expect(adoptNewSessionDraft('session-created')).toBe(true)
clearSessionDraft('session-created')
clearSessionDraft(null)
})
it('carries the unsent draft of a GONE session into the fresh chat once, with an undoable notice (#111868)', () => {
stashSessionDraft('session-gone', 'typed into a session that no longer exists', [])
@@ -685,7 +726,9 @@ describe('useComposerDraft — a hidden keep-alive tab never auto-focuses its co
return (
<>
<div ref={element => element?.focus()} role="tab" tabIndex={0}>Session tab</div>
<div ref={element => element?.focus()} role="tab" tabIndex={0}>
Session tab
</div>
<div contentEditable data-slot="composer-rich-input" ref={draft.editorRef} role="textbox" tabIndex={0} />
</>
)

View File

@@ -14,6 +14,7 @@ import {
type ComposerDraftSyncMode,
freshDraftScope,
isFreshDraftScope,
newSessionDraftAnnouncedFor,
onComposerDraftSyncRequest,
reloadPersistedDrafts,
savedDraftAt,
@@ -181,6 +182,8 @@ export function useComposerDraft({
// session switch mid-flight files one session's draft under another's key
// (#54527).
const draftScopeRef = useRef(activeQueueSessionKey)
// Latch for the swap effect's first run (mount); see the remount handoff there.
const scopeSwapRanRef = useRef(false)
// The draft (text + attachment occurrences) this window last loaded from or
// stashed for draftScopeRef's scope. Another window can send (clear) or
// rewrite that draft while this editor still shows it, so every save path
@@ -710,8 +713,21 @@ export function useComposerDraft({
// is restored — so the text the user kept typing follows the chat instead
// of vanishing. Keyed on the scope alone: the runtime id can land a resume
// later than the route flips the scope, so it is not a usable signal here.
if ((isFreshDraftScope(draftScopeRef.current) || !draftScopeRef.current) && activeQueueSessionKey) {
const freshKey = draftScopeRef.current || freshDraftScope()
//
// The handoff can also span a remount: the cold-start resume navigates to
// a session whose loading view mounts no composer, so the fresh chat's
// composer unmounts (stashing the typed text under the fresh key) and a
// new one mounts already scoped to the restored id. That first run has no
// fresh previous scope, yet the announcement names its key; without this
// the text typed while the app was booting vanished from the composer.
const firstScopeRun = !scopeSwapRanRef.current
scopeSwapRanRef.current = true
const fromFresh = isFreshDraftScope(draftScopeRef.current) || !draftScopeRef.current
// A first run on any other session (a split pane, a preview tile) must not
// consume an announcement meant for the restored chat's composer.
if ((fromFresh || (firstScopeRun && newSessionDraftAnnouncedFor(activeQueueSessionKey))) && activeQueueSessionKey) {
const freshKey = fromFresh ? draftScopeRef.current || freshDraftScope() : freshDraftScope()
// A follow-up queued while session.create was in flight sits under the
// fresh key; it belongs to this chat too. The queue re-key never moves

View File

@@ -632,6 +632,10 @@ export function announceNewSessionDraftKey(toKey: string | null | undefined): vo
announcedNewSessionDraftKey = toKey?.trim() || null
}
/** Whether the pending announcement names `toKey` (does not consume it). */
export const newSessionDraftAnnouncedFor = (toKey: string | null | undefined): boolean =>
!!announcedNewSessionDraftKey && announcedNewSessionDraftKey === toKey?.trim()
/** Consume the announcement; move the `__new__` draft when it names `toKey`.
* True when the announcement named `toKey` (the caller hands the rest of the
* pre-session chat over), even if there was no draft text to move. */