Skip to content

The agent that grows with you

fix(desktop): a queued-row Save reply settles only its own save and keeps newer text

unsupportedpastels44c3fd1e2bb2
diff --git a/apps/desktop/src/app/chat/composer/hooks/use-composer-queue.test.tsx b/apps/desktop/src/app/chat/composer/hooks/use-composer-queue.test.tsx
index 433dace7f50..324fa415731 100644
--- a/apps/desktop/src/app/chat/composer/hooks/use-composer-queue.test.tsx
+++ b/apps/desktop/src/app/chat/composer/hooks/use-composer-queue.test.tsx
@@ -32,6 +32,19 @@ import { useComposerQueue } from './use-composer-queue'
const SESSION_KEY = 'stored-session-queue-hook'
+/** A reply the test settles by hand. */
+function deferred() {
+ let resolve!: (value: unknown) => void
+ let reject!: (reason: unknown) => void
+
+ const promise = new Promise<unknown>((onResolve, onReject) => {
+ resolve = onResolve
+ reject = onReject
+ })
+
+ return { promise, reject, resolve }
+}
+
function renderQueueHook(
overrides: {
busy?: boolean
@@ -589,6 +602,53 @@ describe('useComposerQueue scope', () => {
expect($notifications.get().some(n => n.kind === 'warning')).toBe(true)
})
+ // Review: the composer stays editable while the owner answers a Save. That answer settles only the
+ // save it belongs to and never overwrites text typed since.
+ it.each([
+ ['accepted', true],
+ ['refused', false]
+ ])('a %s Save reply keeps the text typed after Save', async (_label, accepted) => {
+ reconcilePendingSubmissions(SESSION_KEY, [
+ { admission_id: 'adm-1', status: 'queued', user: 'old words', controls: ['steer', 'replace'] }
+ ])
+ const reply = deferred()
+ const { draftRef, hook } = renderQueueHook({ busy: true, draft: 'my draft', replaceServerQueued: () => reply.promise })
+
+ act(() => hook.result.current.beginQueuedEdit(getQueuedPrompts(SESSION_KEY)[0]!))
+ draftRef.current = 'new words'
+ act(() => {
+ hook.result.current.exitQueuedEdit('save')
+ })
+ draftRef.current = 'typed after save'
+ await act(async () => (accepted ? reply.resolve({}) : reply.reject({ data: { reason: 'admission_started' } })))
+
+ expect(hook.result.current.queueEdit).toBeNull()
+ expect(draftRef.current).toBe('typed after save\n\nmy draft')
+ })
+
+ it('a Save reply for an edit that was cancelled and reopened leaves the reopened edit alone', async () => {
+ reconcilePendingSubmissions(SESSION_KEY, [
+ { admission_id: 'adm-1', status: 'queued', user: 'old words', controls: ['steer', 'replace'] }
+ ])
+ const reply = deferred()
+ const { draftRef, hook } = renderQueueHook({ busy: true, draft: 'my draft', replaceServerQueued: () => reply.promise })
+
+ act(() => hook.result.current.beginQueuedEdit(getQueuedPrompts(SESSION_KEY)[0]!))
+ draftRef.current = 'new words'
+ act(() => {
+ hook.result.current.exitQueuedEdit('save')
+ })
+ act(() => {
+ hook.result.current.exitQueuedEdit('cancel')
+ })
+ act(() => hook.result.current.beginQueuedEdit(getQueuedPrompts(SESSION_KEY)[0]!))
+ draftRef.current = 'reopened edit'
+ await act(async () => reply.resolve({}))
+
+ expect(hook.result.current.queueEdit?.entryId).toBe('adm-1')
+ expect(draftRef.current).toBe('reopened edit')
+ })
+
it('a started-row snapshot arriving mid-edit keeps the edit as a draft', () => {
reconcilePendingSubmissions(SESSION_KEY, [
{ admission_id: 'adm-1', status: 'queued', user: 'old words', controls: ['steer', 'replace'] }
diff --git a/apps/desktop/src/app/chat/composer/hooks/use-composer-queue.ts b/apps/desktop/src/app/chat/composer/hooks/use-composer-queue.ts
index c8c2546832e..80bf565688b 100644
--- a/apps/desktop/src/app/chat/composer/hooks/use-composer-queue.ts
+++ b/apps/desktop/src/app/chat/composer/hooks/use-composer-queue.ts
@@ -91,6 +91,9 @@ interface UseComposerQueueArgs {
sessionId: string | null | undefined
}
+/** Text kept in the composer, followed by the draft a queued-row edit interrupted (when it differs). */
+const withPreEditDraft = (text: string, draft: string) => (draft.trim() && draft !== text ? `${text}\n\n${draft}` : text)
+
/**
* The composer's queue engine — everything about queued turns: the per-session
* queue store binding, in-place queued-prompt editing (begin/step/exit), the
@@ -159,15 +162,16 @@ export function useComposerQueue({
// only: the owner's FIFO keeps running (it may claim the row mid-edit), so the composer keeps
// the edited text until the owner accepts the replacement.
const serverEditRef = useRef<string | null>(null)
- const savingServerEditRef = useRef<string | null>(null)
+ // The one save the owner is answering: the exact edit it was made in, its own token and the
+ // composer text at Save. Its answer settles only that save and never overwrites newer text.
+ const serverSaveRef = useRef<{ edit: QueueEditState; text: string; token: symbol } | null>(null)
// The owner refused the replacement or started the row: nothing typed is lost. The edit stays
// in the composer as an unsent draft (followed by the draft it interrupted), with the reason.
const keepServerEditAsDraft = (edit: QueueEditState, text: string) => {
serverEditRef.current = null
setQueueEditSnapshot(null)
- const previous = edit.draft.trim() && edit.draft !== text ? edit.draft : ''
- loadIntoComposer(previous ? `${text}\n\n${previous}` : text, edit.attachments)
+ loadIntoComposer(withPreEditDraft(text, edit.draft), edit.attachments)
notify({
kind: 'warning',
title: t.promptNotices.sessionCommands.queueEditKeptTitle,
@@ -295,36 +299,43 @@ export function useComposerQueue({
const edited = frozen.frozenTransport ?? frozen.text
const entryId = editingQueuedPrompt.id
- if (savingServerEditRef.current === entryId) {
+ if (serverSaveRef.current?.edit === queueEdit) {
return true // its save is already with the owner
}
if (edited !== editingQueuedPrompt.text) {
// The edit stays open until the owner answers: only an accepted replacement restores the
// pre-edit draft; a refusal (started, queue full, transport) keeps the edited text.
- const edit = queueEdit
- savingServerEditRef.current = entryId
+ const save = { edit: queueEdit, text, token: Symbol('queued-edit-save') }
+ serverSaveRef.current = save
const settle = (accepted: boolean) => {
- if (savingServerEditRef.current !== entryId) {
+ if (serverSaveRef.current?.token !== save.token) {
return
}
- savingServerEditRef.current = null
+ serverSaveRef.current = null
- if (queueEditRef.current?.entryId !== entryId) {
+ // Cancelled, or cancelled and reopened: that later edit is not this save's to settle.
+ if (queueEditRef.current !== save.edit) {
return
}
+ // What the composer holds now, typed after Save included.
+ const current = draftRef.current
+
if (!accepted) {
- keepServerEditAsDraft(edit, text)
+ keepServerEditAsDraft(save.edit, current)
return
}
serverEditRef.current = null
setQueueEditSnapshot(null)
- loadIntoComposer(edit.draft, edit.attachments)
+ loadIntoComposer(
+ current === save.text ? save.edit.draft : withPreEditDraft(current, save.edit.draft),
+ save.edit.attachments
+ )
focusInput()
}
@@ -745,7 +756,7 @@ export function useComposerQueue({
return
}
- if (savingServerEditRef.current === queueEdit.entryId) {
+ if (serverSaveRef.current?.edit === queueEdit) {
return // our own replacement retired it: the owner's answer settles the edit
}