The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(desktop): take back an offered draft once the owner lists its send
Teknium834bccc6623c
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.tsindex 505fdc01f3d..9ca800b0bf5 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@@ -706,6 +706,19 @@ export function useComposerDraft({loadIntoComposer(retry.text, [])return true+ }, retry => {+ // The owner listed it after all: take back the offered draft unless the user changed it.+ if (+ draftScopeRef.current !== activeQueueSessionKey ||+ syncDraftFromEditor().trim() !== retry.text.trim() ||+ readDraftRetry(activeQueueSessionKey)?.id !== retry.id+ ) {+ return+ }++ stashAt(activeQueueSessionKey, '', [])+ writeDraftRetry(activeQueueSessionKey, undefined)+ loadIntoComposer('', [])})}diff --git a/apps/desktop/src/app/chat/composer/hooks/use-composer-submit.send-then-close.test.tsx b/apps/desktop/src/app/chat/composer/hooks/use-composer-submit.send-then-close.test.tsxindex be663ccd3ef..2d086e6dafb 100644--- a/apps/desktop/src/app/chat/composer/hooks/use-composer-submit.send-then-close.test.tsx+++ b/apps/desktop/src/app/chat/composer/hooks/use-composer-submit.send-then-close.test.tsx@@ -168,14 +168,19 @@ it('a message sent right before the window closes is never lost and never sent t})it('a send the owner admitted before the window closed is never offered again as the draft', async () => {- // Admitted and run (its stored user row names it), or admitted and still queued (only the resume- // snapshot lists it): either way the chat already shows it.+ // Admitted and run (its stored user row names it), admitted and still queued (the resume snapshot+ // lists it), or listed only by a snapshot that came after the draft was offered.+ const queued = (id: string, text: string) => [{ admission_id: 'adm-1', input_id: id, status: 'queued', text }]+const landed = {- queued: (id: string) => ({ pending: [{ admission_id: 'adm-1', input_id: id, status: 'queued', text: 'queued one' }] }),+ late: (id: string) => ({ late: queued(id, 'late one') }),+ queued: (id: string) => ({ pending: queued(id, 'queued one') }),ran: (id: string) => ({ row: { content: 'ran one', display_metadata: { submission_id: id }, role: 'user' } })}- for (const [text, owner] of [['ran one', landed.ran], ['queued one', landed.queued]] as const) {+ const cases = [['ran one', landed.ran], ['queued one', landed.queued], ['late one', landed.late]] as const++ for (const [text, owner] of cases) {// Enter, the frame reaches the owner, and the window closes before the ack comes back.window.localStorage.setItem('hermes:composer-drafts:v3', JSON.stringify({ 'stored-B': text }))const unacked = vi.fn<ChatBarProps['onSubmit']>(() => new Promise<boolean>(() => {}))@@ -189,6 +194,13 @@ it('a send the owner admitted before the window closed is never offered again asawait openPage(reopened, 'pending' in held ? held.pending : [])await waitFor(() => expect(outboxKeys()).toEqual([]))await act(async () => {})++ if ('late' in held) {+ expect(handles.editor()).toBe(text)+ const { reconcilePendingSubmissions } = await import('@/store/pending-submissions')+ act(() => reconcilePendingSubmissions('stored-B', held.late))+ }+// Shown once (in the chat), never also back in the composer.expect(handles.editor()).toBe('')expect(reopened).not.toHaveBeenCalled()diff --git a/apps/desktop/src/store/admitted-submissions.ts b/apps/desktop/src/store/admitted-submissions.tsindex 66cbf316fec..4506ca25e98 100644--- a/apps/desktop/src/store/admitted-submissions.ts+++ b/apps/desktop/src/store/admitted-submissions.ts@@ -1,13 +1,22 @@-// The input ids (client submission ids) the owner's latest pending snapshot lists per composer-// queue key, as this page saw them. A send the owner admitted but has not run yet (queued behind a-// running turn or an unresolved one) has no stored row, so only this listing shows that it landed.+// The input ids (client submission ids) the owner's pending snapshots list per composer queue key,+// as this page saw them. A send the owner admitted but has not run yet (queued behind a running+// turn or an unresolved one) has no stored row, so only this listing shows that it landed.const listed = new Map<string, Set<string>>()-const waiters = new Set<() => void>()+const listeners = new Set<(key: string, ids: Set<string>) => void>()/** Record one pending snapshot of `key` (called by the pending-submissions reconcile). */export function noteListedSubmissions(key: string, inputIds: Iterable<string>): void {- listed.set(key, new Set(inputIds))- waiters.forEach(wake => wake())+ const ids = new Set(inputIds)+ listed.set(key, ids)+ listeners.forEach(listener => listener(key, ids))+}++/** Call `listener` with every later snapshot of `key`; returns the unsubscribe. */+export function onListedSubmissions(key: string, listener: (ids: Set<string>) => void): () => void {+ const scoped = (snapshotKey: string, ids: Set<string>) => void (snapshotKey === key && listener(ids))+ listeners.add(scoped)++ return () => void listeners.delete(scoped)}/** The ids `key`'s pending snapshot lists, waiting up to `timeoutMs` for this page's first one;@@ -20,14 +29,17 @@ export function listedSubmissions(key: string, timeoutMs: number): Promise<Set<s}return new Promise(resolve => {- const done = () => {- waiters.delete(wake)- clearTimeout(timer)- resolve(listed.get(key))- }+ let stop = () => {}++ const timer = setTimeout(() => {+ stop()+ resolve(undefined)+ }, timeoutMs)- const wake = () => void (listed.has(key) && done())- const timer = setTimeout(done, timeoutMs)- waiters.add(wake)+ stop = onListedSubmissions(key, ids => {+ stop()+ clearTimeout(timer)+ resolve(ids)+ })})}diff --git a/apps/desktop/src/store/composer-outbox.ts b/apps/desktop/src/store/composer-outbox.tsindex 278239606a7..dcd8c24418d 100644--- a/apps/desktop/src/store/composer-outbox.ts+++ b/apps/desktop/src/store/composer-outbox.ts@@ -1,3 +1,4 @@+import { onListedSubmissions } from './admitted-submissions'import { freshDraftScope } from './composer'import type { ComposerDraftRetry } from './composer-draft-retry'import { sendLanded } from './composer-outbox-landed'@@ -98,12 +99,31 @@ export function journalOutgoingSend(scope: string | null, retry: ComposerDraftRe}}+// A snapshot can list the admission only after the draft was offered (the owner committed the dead+// page's frame after this page's resume, or the snapshot came late). The owner's listing is the+// answer: `withdraw` takes the offered draft back if the composer still holds it untouched.+const WITHDRAW_WATCH_MS = 120_000++function withdrawOnceListed(entry: OutboxEntry, withdraw: (retry: ComposerDraftRetry) => void): void {+ let stop = () => {}+ const timer = setTimeout(() => stop(), WITHDRAW_WATCH_MS)++ stop = onListedSubmissions(entry.scope, ids => {+ if (ids.has(entry.retry.id)) {+ clearTimeout(timer)+ stop()+ withdraw(entry.retry)+ }+ })+}+/** Offer `scope`'s interrupted send (one no live page owns) to `apply`; the entry is removed when* its send already landed in the stored transcript (silently) or when `apply` took it, so a* composer the user already typed into leaves it for the next load. */export async function restoreInterruptedSend(scope: string | null | undefined,- apply: (retry: ComposerDraftRetry) => boolean+ apply: (retry: ComposerDraftRetry) => boolean,+ withdraw: (retry: ComposerDraftRetry) => void): Promise<void> {const target = scopeKey(scope)const keys: string[] = []@@ -127,6 +147,7 @@ export async function restoreInterruptedSend(write(key, undefined)} else if (read(key) && apply(entry.retry)) {write(key, undefined)+ withdrawOnceListed(entry, withdraw)}}