Skip to content

The agent that grows with you

fix(desktop): an edited queued message drains as a new message

Tekniume22c1213e4da
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 8834be58e2b..0df53ffa903 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
@@ -19,6 +19,7 @@ import {
migrateQueuedPrompts,
promoteQueuedPrompt,
type QueuedPromptEntry,
+ queuedSubmissionId,
removeQueuedPrompt,
resolveQueuedPromptTransport,
serverOwnsComposerQueue,
@@ -525,7 +526,7 @@ export function useComposerQueue({
...(resolved.displayText ? { displayText: resolved.displayText } : {}),
...(entry.displayKind ? { displayKind: entry.displayKind } : {}),
fromQueue: true,
- submission_id: entry.id,
+ submission_id: queuedSubmissionId(entry),
sessionId: drainRuntimeSessionId,
storedSessionId: drainQueueSessionKey
})
diff --git a/apps/desktop/src/app/session/hooks/use-background-queue-drain.ts b/apps/desktop/src/app/session/hooks/use-background-queue-drain.ts
index c5b0e14e707..de5b6a66f89 100644
--- a/apps/desktop/src/app/session/hooks/use-background-queue-drain.ts
+++ b/apps/desktop/src/app/session/hooks/use-background-queue-drain.ts
@@ -10,6 +10,7 @@ import {
MAX_AUTO_DRAIN_ATTEMPTS,
noteQueuedPromptDrainFailure,
type QueuedPromptEntry,
+ queuedSubmissionId,
removeQueuedPrompt,
resolveQueuedPromptTransport,
shouldAutoDrain,
@@ -194,7 +195,7 @@ export function useBackgroundQueueDrain({
attachments: liveEntry.attachments,
...(resolved.displayText ? { displayText: resolved.displayText } : {}),
fromQueue: true,
- submission_id: liveEntry.id,
+ submission_id: queuedSubmissionId(liveEntry),
sessionId: runtimeSessionId,
storedSessionId: sessionKey
})
diff --git a/apps/desktop/src/app/session/hooks/use-prompt-actions/submit-admitted-conflict.test.tsx b/apps/desktop/src/app/session/hooks/use-prompt-actions/submit-admitted-conflict.test.tsx
index c4444652a3d..c8345200dbf 100644
--- a/apps/desktop/src/app/session/hooks/use-prompt-actions/submit-admitted-conflict.test.tsx
+++ b/apps/desktop/src/app/session/hooks/use-prompt-actions/submit-admitted-conflict.test.tsx
@@ -4,6 +4,7 @@ import { afterEach, beforeEach, expect, it, vi } from 'vitest'
import { en } from '@/i18n/en'
import { createClientSessionState } from '@/lib/chat-runtime'
+import { enqueueQueuedPrompt, getQueuedPrompts, queuedSubmissionId, updateQueuedPrompt } from '@/store/composer-queue'
import { $activeGatewayProfile } from '@/store/profile'
import { $connection, $sessions } from '@/store/session'
import { $sessionStates } from '@/store/session-states'
@@ -46,26 +47,7 @@ it('a retry refused because its id is already admitted settles as delivered; oth
return {} as never
})
- const deps = {
- activeSessionIdRef: { current: 'runtime-a' as string | null },
- selectedStoredSessionIdRef: { current: 'stored-a' as string | null },
- busyRef: { current: false },
- copy: en.desktop,
- createBackendSessionForSend: vi.fn(async () => null),
- getRoutedStoredSessionId: () => null,
- getRuntimeIdForStoredSession: () => 'runtime-a',
- getRouteToken: () => 'same-route',
- requestGateway: requestGateway as GatewayRequest,
- runtimeIdByStoredSessionIdRef: { current: new Map([['stored-a', 'runtime-a']]) },
- resumeStoredSession: vi.fn(),
- syncAttachmentsForSubmit: vi.fn(async (sessionId: string) => ({ sessionId, attachments: [] })),
- updateSessionState: vi.fn((_sid: string, updater: (s: ClientSessionState) => ClientSessionState) => {
- state = updater(state)
-
- return state
- })
- }
-
+ const deps = submitDeps(requestGateway as GatewayRequest, updater => (state = updater(state)))
const { result } = renderHook(() => useSubmitPrompt(deps as never))
refusal = {
@@ -90,3 +72,66 @@ it('a retry refused because its id is already admitted settles as delivered; oth
expect(state.messages.some(message => message.error)).toBe(true)
})
+
+// A local queue entry drains under its id. Its first drain can be admitted with the ack lost (the entry
+// stays queued); an edit after that is a new message, so it drains under a new id and runs, instead
+// of reading as that admitted original's retry and vanishing as "already delivered".
+it('an edited queued entry drains as a new message, not as its admitted original', async () => {
+ let state: ClientSessionState = createClientSessionState()
+ const submitted: Record<string, unknown>[] = []
+
+ const requestGateway = vi.fn(async (method: string, params?: Record<string, unknown>) => {
+ if (method === 'prompt.submit') {
+ if (params?.submission_id === 'X-orig') {
+ throw new JsonRpcGatewayError('admission_conflict', {
+ code: 4001,
+ data: { reason: 'admission_conflict', field: 'request_id' }
+ })
+ }
+
+ submitted.push(params ?? {})
+
+ return { submission_id: params?.submission_id, session_id: 'runtime-a', status: 'started' } as never
+ }
+
+ return {} as never
+ })
+
+ enqueueQueuedPrompt('stored-a', { id: 'X-orig', text: 'yes', attachments: [] })
+ expect(updateQueuedPrompt('stored-a', 'X-orig', { text: 'yes, and also run the tests', attachments: [] })).toBe(true)
+ const [entry] = getQueuedPrompts('stored-a')
+
+ const deps = submitDeps(requestGateway as GatewayRequest, updater => (state = updater(state)))
+ const { result } = renderHook(() => useSubmitPrompt(deps as never))
+ await act(async () => {
+ expect(
+ await result.current(entry!.text, { fromQueue: true, submission_id: queuedSubmissionId(entry!), storedSessionId: 'stored-a' })
+ ).toBe(true)
+ })
+
+ expect(submitted).toHaveLength(1)
+ expect(submitted[0]).toMatchObject({ text: 'yes, and also run the tests' })
+ expect(state.messages.filter(message => message.role === 'user')).toHaveLength(1)
+ expect(state.messages.some(message => message.error)).toBe(false)
+})
+
+function submitDeps(
+ requestGateway: GatewayRequest,
+ update: (updater: (s: ClientSessionState) => ClientSessionState) => ClientSessionState
+) {
+ return {
+ activeSessionIdRef: { current: 'runtime-a' as string | null },
+ selectedStoredSessionIdRef: { current: 'stored-a' as string | null },
+ busyRef: { current: false },
+ copy: en.desktop,
+ createBackendSessionForSend: vi.fn(async () => null),
+ getRoutedStoredSessionId: () => null,
+ getRuntimeIdForStoredSession: () => 'runtime-a',
+ getRouteToken: () => 'same-route',
+ requestGateway,
+ runtimeIdByStoredSessionIdRef: { current: new Map([['stored-a', 'runtime-a']]) },
+ resumeStoredSession: vi.fn(),
+ syncAttachmentsForSubmit: vi.fn(async (sessionId: string) => ({ sessionId, attachments: [] })),
+ updateSessionState: vi.fn((_sid: string, updater: (s: ClientSessionState) => ClientSessionState) => update(updater))
+ }
+}
diff --git a/apps/desktop/src/store/composer-queue.ts b/apps/desktop/src/store/composer-queue.ts
index aa5f21c7785..51f39e3f137 100644
--- a/apps/desktop/src/store/composer-queue.ts
+++ b/apps/desktop/src/store/composer-queue.ts
@@ -57,10 +57,18 @@ export interface QueuedPromptEntry {
* A user gesture — manual send, redirect, queueing a fresh prompt — clears
* it, exactly like the composer's in-process counter. */
drainFailures?: number
+ /** The submission id a local entry drains under once its words or attachments were edited (else
+ * `id`). The first drain may already be admitted with its ack lost: under that id the edit would
+ * read as the original's retry and settle as delivered without ever running. */
+ submissionId?: string
attachments: ComposerAttachment[]
queuedAt: number
}
+/** The `submission_id` a queued entry is sent (and matched against the owner's admissions) under. */
+export const queuedSubmissionId = (entry: Pick<QueuedPromptEntry, 'id' | 'submissionId'>): string =>
+ entry.submissionId ?? entry.id
+
export interface EnqueueQueuedPromptPayload {
/** Stable id to reuse (a canonical submission id), else one is minted. */
id?: string
@@ -327,6 +335,14 @@ const nextId = () => `queued-${Date.now()}-${Math.random().toString(36).slice(2,
const cloneAttachments = (attachments: ComposerAttachment[]) => attachments.map(a => ({ ...a }))
+const attachmentKeys = (attachments: ComposerAttachment[]) => JSON.stringify(attachments.map(a => a.id))
+
+// An edited local entry is a new message: it drains under a fresh submission id (see `submissionId`).
+const editedSubmission = (entry: QueuedPromptEntry, text: string, attachments: ComposerAttachment[]) =>
+ !entry.serverStatus && (entry.text !== text || attachmentKeys(entry.attachments) !== attachmentKeys(attachments))
+ ? { submissionId: crypto.randomUUID() }
+ : {}
+
export const getQueuedPrompts = (key: string | null | undefined): QueuedPromptEntry[] => {
const sid = sidOf(key)
@@ -561,6 +577,7 @@ export const updateQueuedPrompt = (
return {
...rest,
+ ...editedSubmission(entry, update.text, attachments),
text: update.text,
attachments,
...(nextDisplay ? { displayText: nextDisplay } : {})
diff --git a/apps/desktop/src/store/pending-submissions.ts b/apps/desktop/src/store/pending-submissions.ts
index e3c242e2504..444411f6927 100644
--- a/apps/desktop/src/store/pending-submissions.ts
+++ b/apps/desktop/src/store/pending-submissions.ts
@@ -1,5 +1,5 @@
import { noteListedSubmissions } from './admitted-submissions'
-import { getQueuedPrompts, type QueuedPromptEntry, writeSessionQueue } from './composer-queue'
+import { getQueuedPrompts, type QueuedPromptEntry, queuedSubmissionId, writeSessionQueue } from './composer-queue'
import { $sessions, resolveComposerSessionKey } from './session'
// One localStorage key per (session, admission). Every Desktop window of the origin shares this
@@ -246,7 +246,8 @@ function projectQueue(
const next: QueuedPromptEntry[] = []
for (const entry of current) {
- const receipt = receipts.get(admissionByInput.get(entry.id) ?? entry.id)
+ const submissionId = queuedSubmissionId(entry)
+ const receipt = receipts.get(admissionByInput.get(submissionId) ?? submissionId)
if (receipt) {
if (receipt.status !== 'started') {