Skip to content

The agent that grows with you

fix(desktop): group Send waits for a selected attachment's upload

Teknium22185112dbfa
diff --git a/apps/desktop/src/plugins/hermes-bots/canonical-group-attachments.tsx b/apps/desktop/src/plugins/hermes-bots/canonical-group-attachments.tsx
index ee131c93150..0d0f6afbbaf 100644
--- a/apps/desktop/src/plugins/hermes-bots/canonical-group-attachments.tsx
+++ b/apps/desktop/src/plugins/hermes-bots/canonical-group-attachments.tsx
@@ -17,10 +17,12 @@ function kindFor(file: File): string {
return 'file'
}
-export function CanonicalGroupAttachments({ binding, attachments, onChange, disabled, readOnly = false }: {
+export function CanonicalGroupAttachments({ binding, attachments, onChange, onUploadingChange, disabled, readOnly = false }: {
binding: CanonicalGroupBinding
attachments: Attachment[]
disabled: boolean
+ // C21: the form holds Send while a selected file's manifest is not committed yet.
+ onUploadingChange?: (uploading: boolean) => void
} & ({ readOnly: true; onChange?: never } | { readOnly?: false; onChange: (attachments: Attachment[]) => void })) {
const labels = useCanonicalGroupLabels()
const input = useRef<HTMLInputElement>(null)
@@ -40,7 +42,7 @@ export function CanonicalGroupAttachments({ binding, attachments, onChange, disa
async function upload(file: File) {
if (readOnly) {return}
- setBusy(true); setError('')
+ setBusy(true); setError(''); onUploadingChange?.(true)
try {
const data = await new Promise<string>((resolve, reject) => {
@@ -59,7 +61,7 @@ export function CanonicalGroupAttachments({ binding, attachments, onChange, disa
const { attachment_id, kind, name, mime, size } = result
onChange?.([...attachments, { attachment_id, kind, name, mime, size }])
} catch (e) { setError(e instanceof Error ? e.message : String(e)) }
- finally { setBusy(false) }
+ finally { setBusy(false); onUploadingChange?.(false) }
}
async function download(attachment: Attachment) {
diff --git a/apps/desktop/src/plugins/hermes-bots/canonical-group-upload-race.test.tsx b/apps/desktop/src/plugins/hermes-bots/canonical-group-upload-race.test.tsx
new file mode 100644
index 00000000000..9d9c7064213
--- /dev/null
+++ b/apps/desktop/src/plugins/hermes-bots/canonical-group-upload-race.test.tsx
@@ -0,0 +1,59 @@
+import { useStore } from '@nanostores/react'
+import { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react'
+import { atom } from 'nanostores'
+import type { ComponentProps } from 'react'
+import { afterEach, beforeEach, expect, it, vi } from 'vitest'
+
+const request = vi.hoisted(() => vi.fn())
+vi.mock('@hermes/plugin-sdk', async () => {
+ const { pluginSdkMock, createGroupGateway } = await import('./group-test-utils')
+ const gateway = createGroupGateway()
+ const { en } = await import('@/i18n/en')
+ const { CANONICAL_GROUP_LOCALES } = await import('./canonical-group-locales')
+
+ return { ...await pluginSdkMock(gateway.host), atom, useValue: useStore,
+ useI18n: () => ({ t: en }),
+ usePluginI18n: () => (key: string) => CANONICAL_GROUP_LOCALES.en[key.replace('canonical.', '') as keyof typeof CANONICAL_GROUP_LOCALES.en] ?? key,
+ Button: (p: ComponentProps<'button'>) => <button {...p} />,
+ host: { ...gateway.host, requestProfile: request } }
+})
+import { CanonicalGroupWorkspace } from './canonical-group-workspace'
+const originalDesktop = window.hermesDesktop
+beforeEach(() => { Object.defineProperty(window, 'hermesDesktop', { configurable: true, writable: true, value: undefined }) })
+afterEach(() => { cleanup(); request.mockReset(); localStorage.clear(); window.hermesDesktop = originalDesktop })
+
+// C21: while a selected file's upload acknowledgement is outstanding, Send cannot dispatch the text
+// without it, and the late manifest never lands on the cleared next draft.
+it('holds Send until the selected attachment is committed, then sends it with the text', async () => {
+ let ack!: () => void
+ const uploaded = { attachment_id: 'file-one', kind: 'file', name: 'report.txt', mime: 'text/plain', size: 2 }
+ request.mockImplementation(async (_route, method) => {
+ if (method === 'groups.state') {return { room: { name: 'Room' }, driver_status: {} }}
+
+ if (method === 'groups.log') {return { events: [] }}
+
+ if (method === 'groups.attachment.upload') {return new Promise(resolve => { ack = () => resolve(uploaded) })}
+
+ return {}
+ })
+ const view = render(<CanonicalGroupWorkspace binding={{ connectionId: 'remote', profile: 'team', roomId: 'upload-race' }} />)
+ await screen.findByText('Room')
+ await waitFor(() => expect((screen.getByRole('textbox') as HTMLTextAreaElement).disabled).toBe(false))
+ fireEvent.change(view.container.querySelector('input[type=file]')!, { target: { files: [new File(['hi'], 'report.txt', { type: 'text/plain' })] } })
+ await waitFor(() => expect(request.mock.calls.some(c => c[1] === 'groups.attachment.upload')).toBe(true))
+ fireEvent.change(screen.getByRole('textbox'), { target: { value: 'see file' } })
+ const sendButton = screen.getByRole('button', { name: 'Send' }) as HTMLButtonElement
+ expect(sendButton.disabled).toBe(true)
+ fireEvent.submit(sendButton.closest('form')!)
+ expect(request.mock.calls.some(c => c[1] === 'groups.send')).toBe(false)
+ ack()
+ await screen.findByText('report.txt')
+ await waitFor(() => expect(sendButton.disabled).toBe(false))
+ fireEvent.click(sendButton)
+ await waitFor(() => expect(request.mock.calls.some(c => c[1] === 'groups.send')).toBe(true))
+ const sent = request.mock.calls.filter(c => c[1] === 'groups.send')
+ expect(sent).toHaveLength(1)
+ expect(sent[0][2].payload).toMatchObject({ text: 'see file', attachments: [uploaded] })
+ await waitFor(() => expect((screen.getByRole('textbox') as HTMLTextAreaElement).value).toBe(''))
+ expect(screen.queryByText('report.txt')).toBeNull()
+})
diff --git a/apps/desktop/src/plugins/hermes-bots/canonical-group-workspace.tsx b/apps/desktop/src/plugins/hermes-bots/canonical-group-workspace.tsx
index 6eea0972504..7538c52314a 100644
--- a/apps/desktop/src/plugins/hermes-bots/canonical-group-workspace.tsx
+++ b/apps/desktop/src/plugins/hermes-bots/canonical-group-workspace.tsx
@@ -34,6 +34,7 @@ function CanonicalRoomView({ binding: initialBinding, visible, onBack }: {
const [readError, setReadError] = useState('')
const [draft, setDraft] = useState('')
const [attachments, setAttachments] = useState<Attachment[]>([])
+ const [uploading, setUploading] = useState(false)
const [restored, setRestored] = useState(false)
const [pending, setPending] = useState<PreparedCanonicalGroupSend | null>(null)
const [busy, setBusy] = useState(false)
@@ -123,7 +124,7 @@ function CanonicalRoomView({ binding: initialBinding, visible, onBack }: {
}
const send = () => {
- if (!restored || busyRef.current || !state?.driver_status || (!pending && !draft.trim() && !attachments.length)) {return}
+ if (!restored || busyRef.current || uploading || !state?.driver_status || (!pending && !draft.trim() && !attachments.length)) {return}
void mutate(async () => {
const exact = pending ?? await prepareCanonicalGroupSend(binding, { text: draft, attachments })
@@ -180,9 +181,9 @@ function CanonicalRoomView({ binding: initialBinding, visible, onBack }: {
</div>}
{pending && <p role="status">{labels.restoredPendingSend}</p>}
<form className="flex gap-2" onSubmit={event => { event.preventDefault(); send() }}>
- <CanonicalGroupAttachments attachments={attachments} binding={binding} disabled={composerLocked} onChange={setAttachments} />
+ <CanonicalGroupAttachments attachments={attachments} binding={binding} disabled={composerLocked} onChange={setAttachments} onUploadingChange={setUploading} />
<textarea aria-label={labels.groupMessage} className="min-w-0 flex-1" disabled={composerLocked} onChange={e => setDraft(e.target.value)} value={draft} />
- <Button disabled={!restored || busy || (!pending && !draft.trim() && !attachments.length) || !state?.driver_status} type="submit">{pending ? labels.retry : labels.send}</Button>
+ <Button disabled={!restored || busy || uploading || (!pending && !draft.trim() && !attachments.length) || !state?.driver_status} type="submit">{pending ? labels.retry : labels.send}</Button>
</form>
</section>
}