Skip to content

The agent that grows with you

fix(desktop): preserve legacy attachment replacements

Jakub Wolniewiczd0642e61e52e
diff --git a/apps/desktop/src/app/chat/session-tile-attachments.test.tsx b/apps/desktop/src/app/chat/session-tile-attachments.test.tsx
index 206d973961a..b9a97a86113 100644
--- a/apps/desktop/src/app/chat/session-tile-attachments.test.tsx
+++ b/apps/desktop/src/app/chat/session-tile-attachments.test.tsx
@@ -65,6 +65,26 @@ function makeAttachment(occurrenceId = createComposerAttachmentOccurrenceId()):
}
}
+function makeUrlAttachment(label: string): ComposerAttachment {
+ return {
+ id: 'url:https://example.com',
+ kind: 'url',
+ label,
+ refText: '@url:https://example.com'
+ }
+}
+
+function makeFileAttachment(): ComposerAttachment {
+ return {
+ detail: 'C:\\Users\\alice\\Documents\\report.txt',
+ id: 'file:report.txt',
+ kind: 'file',
+ label: 'report.txt',
+ path: 'C:\\Users\\alice\\Documents\\report.txt',
+ refText: '@file:`C:\\Users\\alice\\Documents\\report.txt`'
+ }
+}
+
function installDelegate(): void {
const updateSession: SessionTileDelegate['updateSession'] = (runtimeId, updater) => {
const current = $sessionStates.get()[runtimeId] ?? createClientSessionState(STORED_ID)
@@ -243,4 +263,72 @@ describe('session tile attachment occurrence ownership', () => {
expect(scope.attachments.$attachments.get()).toEqual([replacement])
})
+
+ it('preserves a newer same-id URL added while a successful tile submit is in flight', async () => {
+ const scope = createScope()
+ const original = makeUrlAttachment('old')
+ const replacement = makeUrlAttachment('new')
+ const submit = deferred<Record<string, never>>()
+
+ requestGateway.mockImplementation(async (method: string) => {
+ if (method === 'prompt.submit') {
+ return submit.promise
+ }
+
+ return {}
+ })
+
+ scope.attachments.add(original)
+
+ const { result } = renderHook(() =>
+ useSessionTileActions({ runtimeId: RUNTIME_ID, scope, storedSessionId: STORED_ID })
+ )
+
+ let submitted!: Promise<boolean>
+ act(() => {
+ submitted = result.current.submitText('read this')
+ })
+
+ await waitFor(() =>
+ expect(requestGateway).toHaveBeenCalledWith('prompt.submit', expect.anything(), expect.anything())
+ )
+ scope.attachments.remove(original.id)
+ scope.attachments.add(replacement)
+ submit.resolve({})
+
+ await act(async () => {
+ await expect(submitted).resolves.toBe(true)
+ })
+
+ expect(scope.attachments.$attachments.get()).toEqual([replacement])
+ })
+
+ it('removes a successfully staged legacy file from the tile composer', async () => {
+ const scope = createScope()
+ const original = makeFileAttachment()
+
+ requestGateway.mockImplementation(async (method: string) => {
+ if (method === 'file.attach') {
+ return {
+ attached: true,
+ ref_text: '@file:.hermes/desktop-attachments/report.txt',
+ uploaded: true
+ }
+ }
+
+ return {}
+ })
+
+ scope.attachments.add(original)
+
+ const { result } = renderHook(() =>
+ useSessionTileActions({ runtimeId: RUNTIME_ID, scope, storedSessionId: STORED_ID })
+ )
+
+ await act(async () => {
+ await expect(result.current.submitText('read this')).resolves.toBe(true)
+ })
+
+ expect(scope.attachments.$attachments.get()).toEqual([])
+ })
})
diff --git a/apps/desktop/src/app/session/hooks/use-prompt-actions/index.test.tsx b/apps/desktop/src/app/session/hooks/use-prompt-actions/index.test.tsx
index 37fc959b805..12ae168265b 100644
--- a/apps/desktop/src/app/session/hooks/use-prompt-actions/index.test.tsx
+++ b/apps/desktop/src/app/session/hooks/use-prompt-actions/index.test.tsx
@@ -2788,6 +2788,90 @@ describe('usePromptActions file attachment sync', () => {
expect($composerAttachments.get()).toEqual([replacement])
})
+ it('preserves a newer same-id URL added while a successful main submit is in flight', async () => {
+ const original: ComposerAttachment = {
+ id: 'url:https://example.com',
+ kind: 'url',
+ label: 'old',
+ refText: '@url:https://example.com'
+ }
+
+ const replacement: ComposerAttachment = {
+ ...original,
+ label: 'new'
+ }
+
+ let resolveSubmit!: (value: Record<string, never>) => void
+
+ const submitResult = new Promise<Record<string, never>>(resolve => {
+ resolveSubmit = resolve
+ })
+
+ const requestGateway = vi.fn(async (method: string) => {
+ if (method === 'prompt.submit') {
+ return (await submitResult) as never
+ }
+
+ return {} as never
+ })
+
+ $composerAttachments.set([original])
+
+ let handle: HarnessHandle | null = null
+ await actRender(
+ <Harness onReady={h => (handle = h)} refreshSessions={async () => undefined} requestGateway={requestGateway} />
+ )
+
+ let submitted!: Promise<boolean>
+ act(() => {
+ submitted = handle!.submitTextRaw('read this')
+ })
+
+ await waitFor(() =>
+ expect(requestGateway).toHaveBeenCalledWith('prompt.submit', expect.anything(), expect.anything())
+ )
+ $composerAttachments.set([replacement])
+ resolveSubmit({})
+
+ await act(async () => {
+ await expect(submitted).resolves.toBe(true)
+ })
+
+ expect($composerAttachments.get()).toEqual([replacement])
+ })
+
+ it('removes a successfully staged legacy file from the main composer', async () => {
+ $connection.set({ mode: 'remote' } as never)
+ const original = fileAttachment()
+
+ Object.defineProperty(window, 'hermesDesktop', {
+ configurable: true,
+ value: { readFileDataUrl: vi.fn(async () => 'data:text/plain;base64,aGVsbG8=') }
+ })
+
+ const requestGateway = vi.fn(async (method: string) => {
+ if (method === 'file.attach') {
+ return {
+ attached: true,
+ ref_text: '@file:.hermes/desktop-attachments/report.txt',
+ uploaded: true
+ } as never
+ }
+
+ return {} as never
+ })
+
+ $composerAttachments.set([original])
+
+ let handle: HarnessHandle | null = null
+ await actRender(
+ <Harness onReady={h => (handle = h)} refreshSessions={async () => undefined} requestGateway={requestGateway} />
+ )
+
+ await expect(handle!.submitTextRaw('read this')).resolves.toBe(true)
+ expect($composerAttachments.get()).toEqual([])
+ })
+
it('uploads file bytes when the terminal backend is a container (docker)', async () => {
// Container backends have their own filesystem: the host drop path would
// dangle inside the sandbox, so the bytes must cross via file.attach's
diff --git a/apps/desktop/src/app/session/hooks/use-prompt-actions/submit.ts b/apps/desktop/src/app/session/hooks/use-prompt-actions/submit.ts
index 68e8c8bcd22..e0f86e64961 100644
--- a/apps/desktop/src/app/session/hooks/use-prompt-actions/submit.ts
+++ b/apps/desktop/src/app/session/hooks/use-prompt-actions/submit.ts
@@ -714,10 +714,11 @@ export function useSubmitPrompt(deps: SubmitPromptDeps) {
}
if (usingComposerAttachments) {
- // A submit owns only the occurrences captured before its first await.
- // Preserve chips added or replaced while staging / prompt.submit was
- // in flight instead of clearing the composer's newer draft state.
- scope.removeAttachments(attachments)
+ // A submit owns only the occurrences that actually reached the
+ // gateway. Tokenized chips match across staging clones; legacy chips
+ // match by exact object identity, so a newer same-id replacement is
+ // preserved while the staged object for a submitted file is removed.
+ scope.removeAttachments(syncedAttachments)
}
// Submit landed — the turn now runs (busy stays true), but the submit
diff --git a/apps/desktop/src/store/composer.test.ts b/apps/desktop/src/store/composer.test.ts
index e1f040742f5..0fc034b1d9b 100644
--- a/apps/desktop/src/store/composer.test.ts
+++ b/apps/desktop/src/store/composer.test.ts
@@ -1,4 +1,4 @@
-import { afterEach, describe, expect, it } from 'vitest'
+import { afterEach, describe, expect, it, vi } from 'vitest'
import {
$composerAttachments,
@@ -175,6 +175,35 @@ describe('updateComposerAttachment', () => {
expect(scope.$attachments.get()).toEqual([replacement])
})
+
+ it('preserves a newer same-id legacy attachment and still emits on successful cleanup', () => {
+ const scope = createComposerAttachmentScope()
+ const submitted = attachment({ id: 'url:https://example.com', kind: 'url', label: 'old' })
+ const replacement = attachment({ id: submitted.id, kind: 'url', label: 'new' })
+ const listener = vi.fn()
+ const unlisten = scope.$attachments.listen(listener)
+
+ scope.add(submitted)
+ scope.remove(submitted.id)
+ scope.add(replacement)
+ listener.mockClear()
+
+ scope.removeOccurrences([submitted])
+
+ expect(scope.$attachments.get()).toEqual([replacement])
+ expect(listener).toHaveBeenCalledTimes(1)
+ unlisten()
+ })
+
+ it('removes the exact submitted legacy attachment', () => {
+ const scope = createComposerAttachmentScope()
+ const submitted = attachment({ id: 'url:https://example.com', kind: 'url' })
+
+ scope.add(submitted)
+ scope.removeOccurrences([submitted])
+
+ expect(scope.$attachments.get()).toEqual([])
+ })
})
describe('session drafts', () => {
diff --git a/apps/desktop/src/store/composer.ts b/apps/desktop/src/store/composer.ts
index 69be0df4174..81cb6e51293 100644
--- a/apps/desktop/src/store/composer.ts
+++ b/apps/desktop/src/store/composer.ts
@@ -103,10 +103,18 @@ export function createComposerAttachmentScope($attachments = atom<ComposerAttach
removeOccurrences(attachments) {
const current = $attachments.get()
- const submitted = new Set(attachments.map(attachment => `${attachment.id}\u0000${attachment.occurrenceId ?? ''}`))
+ const submittedOccurrences = new Set(
+ attachments
+ .filter(attachment => attachment.occurrenceId !== undefined)
+ .map(attachment => `${attachment.id}\u0000${attachment.occurrenceId}`)
+ )
+
+ const submittedLegacy = new Set(attachments.filter(attachment => attachment.occurrenceId === undefined))
- const next = current.filter(
- attachment => !submitted.has(`${attachment.id}\u0000${attachment.occurrenceId ?? ''}`)
+ const next = current.filter(attachment =>
+ attachment.occurrenceId === undefined
+ ? !submittedLegacy.has(attachment)
+ : !submittedOccurrences.has(`${attachment.id}\u0000${attachment.occurrenceId}`)
)
// Preserve clear()'s notification semantics even when no captured