Skip to content

The agent that grows with you

fix(desktop): download Kanban attachments through authenticated gateway

Johan Roestd9a44a0ed496
diff --git a/apps/desktop/src/api/file-download.test.ts b/apps/desktop/src/api/file-download.test.ts
new file mode 100644
index 00000000000..f455b0299cc
--- /dev/null
+++ b/apps/desktop/src/api/file-download.test.ts
@@ -0,0 +1,58 @@
+import { afterEach, describe, expect, it, vi } from 'vitest'
+
+import { setApiRequestConnection, setApiRequestProfile } from './client'
+import { captureGatewayFileDownload } from './file-download'
+
+afterEach(() => {
+ setApiRequestConnection(null)
+ setApiRequestProfile(null)
+ vi.unstubAllGlobals()
+})
+
+describe('captured gateway file download', () => {
+ it('reports an unavailable desktop bridge', async () => {
+ vi.stubGlobal('hermesDesktop', {})
+ await expect(captureGatewayFileDownload()('/persisted/file.md', 'file.md')).rejects.toThrow(
+ 'Desktop file download bridge is unavailable'
+ )
+ })
+
+ it('rejects an absent stored path before invoking the bridge', async () => {
+ const saveGatewayFile = vi.fn()
+ vi.stubGlobal('hermesDesktop', { saveGatewayFile })
+ await expect(captureGatewayFileDownload()(' ', 'file.md')).rejects.toThrow('Missing gateway file path')
+ expect(saveGatewayFile).not.toHaveBeenCalled()
+ })
+
+ it('rejects an unsuccessful non-canceled save', async () => {
+ vi.stubGlobal('hermesDesktop', { saveGatewayFile: vi.fn().mockResolvedValue({ saved: false }) })
+ await expect(captureGatewayFileDownload()('/persisted/file.md', 'file.md')).rejects.toThrow('File download failed')
+ })
+
+ it('preserves legacy primary routing instead of inventing a default profile', async () => {
+ const saveGatewayFile = vi.fn().mockResolvedValue({ saved: true })
+ vi.stubGlobal('hermesDesktop', { saveGatewayFile })
+ const download = captureGatewayFileDownload()
+ setApiRequestConnection('remote')
+ setApiRequestProfile('work')
+ await download('/persisted/file.md', 'file.md')
+ expect(saveGatewayFile).toHaveBeenCalledWith({ path: '/persisted/file.md', suggestedName: 'file.md' })
+ })
+
+ it('keeps explicit local ownership even after switching to a remote', async () => {
+ const saveGatewayFile = vi.fn().mockResolvedValue({ canceled: true, saved: false })
+ vi.stubGlobal('hermesDesktop', { saveGatewayFile })
+ setApiRequestConnection('local')
+ setApiRequestProfile('personal')
+ const download = captureGatewayFileDownload()
+ setApiRequestConnection('remote')
+ setApiRequestProfile('work')
+ await expect(download('/persisted/file.md', 'file.md')).resolves.toEqual({ canceled: true, saved: false })
+ expect(saveGatewayFile).toHaveBeenCalledWith({
+ connectionId: 'local',
+ profile: 'personal',
+ path: '/persisted/file.md',
+ suggestedName: 'file.md'
+ })
+ })
+})
diff --git a/apps/desktop/src/api/file-download.ts b/apps/desktop/src/api/file-download.ts
new file mode 100644
index 00000000000..299f61f7c0c
--- /dev/null
+++ b/apps/desktop/src/api/file-download.ts
@@ -0,0 +1,29 @@
+import { capabilityScoped } from './client'
+
+/** Capture alongside a REST read, never at click time: cached resources belong
+ * to the connection/profile that returned them, not the currently focused chat.
+ * Paths stay on the gateway; only Electron's authenticated save bridge sees them.
+ * An absent connection id preserves legacy profile-pool routing; an explicit
+ * `local` id must not be dropped (the registry primary may be remote).
+ */
+export function captureGatewayFileDownload() {
+ const scope = capabilityScoped()
+
+ return async (path: string, suggestedName: string) => {
+ if (!path?.trim()) {
+ throw new Error('Missing gateway file path')
+ }
+
+ if (!window.hermesDesktop?.saveGatewayFile) {
+ throw new Error('Desktop file download bridge is unavailable')
+ }
+
+ const result = await window.hermesDesktop.saveGatewayFile({ ...scope, path, suggestedName })
+
+ if (!result.saved && !result.canceled) {
+ throw new Error('File download failed')
+ }
+
+ return result
+ }
+}
diff --git a/apps/desktop/src/plugins/kanban/api.ts b/apps/desktop/src/plugins/kanban/api.ts
index 6fd0d997cc6..5917ef70efc 100644
--- a/apps/desktop/src/plugins/kanban/api.ts
+++ b/apps/desktop/src/plugins/kanban/api.ts
@@ -17,6 +17,7 @@
import {
atom,
+ captureGatewayFileDownload,
host,
type PluginOs,
type PluginRestOptions,
@@ -250,7 +251,12 @@ export const orchestrationKey = (scope: string) => ['kanban', 'orchestration', s
export const fetchBoard = (archived: boolean) =>
call<KanbanBoard>(withBoard('/board', archived ? { include_archived: 'true' } : {}))
-export const fetchTask = (id: string) => call<KanbanTaskDetail>(withBoard(`/tasks/${id}`))
+export const fetchTask = async (id: string) => {
+ const downloadAttachment = captureGatewayFileDownload()
+ const detail = await call<KanbanTaskDetail>(withBoard(`/tasks/${id}`))
+
+ return { ...detail, downloadAttachment }
+}
/** Worker stdout/stderr tail (last 16 KiB — plenty for the drawer). */
export const fetchLog = (id: string) => call<WorkerLog>(withBoard(`/tasks/${id}/log`, { tail: '16384' }))
diff --git a/apps/desktop/src/plugins/kanban/drawer.test.tsx b/apps/desktop/src/plugins/kanban/drawer.test.tsx
index e2dd19216c9..70854217229 100644
--- a/apps/desktop/src/plugins/kanban/drawer.test.tsx
+++ b/apps/desktop/src/plugins/kanban/drawer.test.tsx
@@ -67,7 +67,12 @@ beforeEach(() => {
)
})
-afterEach(() => {
+afterEach(async () => {
+ const { setApiRequestConnection, setApiRequestProfile } = await import('@/api/client')
+ setApiRequestConnection(null)
+ setApiRequestProfile(null)
+ vi.unstubAllGlobals()
+ vi.restoreAllMocks()
cleanup()
client.clear()
disposeApi()
@@ -84,6 +89,83 @@ function openDrawer() {
}
describe('task attachment compatibility', () => {
+ it('downloads the persisted attachment through its original remote owner', async () => {
+ const { setApiRequestConnection, setApiRequestProfile } = await import('@/api/client')
+ const save = vi.fn().mockResolvedValue({ saved: true })
+ vi.stubGlobal('hermesDesktop', { saveGatewayFile: save })
+ setApiRequestConnection('remote-owner')
+ setApiRequestProfile('research')
+ detail = {
+ ...legacyDetail,
+ attachments: [{ id: 42, filename: 'report.md', stored_path: '/persisted/attachments/report.md' }]
+ }
+ openDrawer()
+ const download = await screen.findByRole('button', { name: 'Download report.md' })
+ setApiRequestConnection('other-host')
+ setApiRequestProfile('other-profile')
+ fireEvent.click(download)
+ await waitFor(() =>
+ expect(save).toHaveBeenCalledWith({
+ connectionId: 'remote-owner',
+ profile: 'research',
+ path: '/persisted/attachments/report.md',
+ suggestedName: 'report.md'
+ })
+ )
+ setApiRequestConnection(null)
+ setApiRequestProfile(null)
+ })
+
+ it('disables downloads with no persisted path instead of guessing a workspace path', async () => {
+ detail = { ...legacyDetail, attachments: [{ id: 1, filename: 'gone.md' }] }
+ openDrawer()
+ const button = await screen.findByRole('button', { name: 'Download gone.md' })
+ expect((button as HTMLButtonElement).disabled).toBe(true)
+ })
+
+ it('reports a failed download and allows retry', async () => {
+ const { host } = await import('@hermes/plugin-sdk')
+ const notify = vi.spyOn(host, 'notify')
+ const save = vi.fn().mockRejectedValue(new Error('File not found'))
+ vi.stubGlobal('hermesDesktop', { saveGatewayFile: save })
+ detail = { ...legacyDetail, attachments: [{ id: 1, filename: 'gone.md', stored_path: '/persisted/gone.md' }] }
+ openDrawer()
+ const button = await screen.findByRole('button', { name: 'Download gone.md' })
+ fireEvent.click(button)
+ await waitFor(() => expect(notify).toHaveBeenCalledWith({ kind: 'error', message: 'File not found' }))
+ await waitFor(() => expect((button as HTMLButtonElement).disabled).toBe(false))
+ save.mockResolvedValueOnce({ saved: true })
+ fireEvent.click(button)
+ await waitFor(() => expect(save).toHaveBeenCalledTimes(2))
+ notify.mockRestore()
+ })
+
+ it('disables a pending download and treats save-dialog cancellation quietly', async () => {
+ const { host } = await import('@hermes/plugin-sdk')
+ const notify = vi.spyOn(host, 'notify')
+ let finish!: (value: { saved: boolean; canceled: boolean }) => void
+
+ const save = vi.fn(
+ () =>
+ new Promise(resolve => {
+ finish = resolve
+ })
+ )
+
+ vi.stubGlobal('hermesDesktop', { saveGatewayFile: save })
+ detail = { ...legacyDetail, attachments: [{ id: 1, filename: 'report.md', stored_path: '/persisted/report.md' }] }
+ openDrawer()
+ const button = await screen.findByRole('button', { name: 'Download report.md' })
+ fireEvent.click(button)
+ await waitFor(() => expect((button as HTMLButtonElement).disabled).toBe(true))
+ fireEvent.click(button)
+ expect(save).toHaveBeenCalledOnce()
+ await act(async () => finish({ saved: false, canceled: true }))
+ await waitFor(() => expect((button as HTMLButtonElement).disabled).toBe(false))
+ expect(notify).not.toHaveBeenCalled()
+ notify.mockRestore()
+ })
+
it.each([{}, { attachments: null }])(
'keeps older task details usable without attachment controls (%j)',
async extra => {
diff --git a/apps/desktop/src/plugins/kanban/drawer.tsx b/apps/desktop/src/plugins/kanban/drawer.tsx
index febcd8f848d..966ff906bb6 100644
--- a/apps/desktop/src/plugins/kanban/drawer.tsx
+++ b/apps/desktop/src/plugins/kanban/drawer.tsx
@@ -29,6 +29,7 @@ import {
SegmentedControl,
Textarea,
Tip,
+ useI18n,
useMutation,
useQuery,
useQueryClient,
@@ -468,11 +469,41 @@ function DescriptionSection({ body, onSave }: { body: null | string | undefined;
// administrative note into that slot; hide those (Runs still shows them).
const isAdminSummary = (summary: string) => /^status changed to \w+ \(dashboard\/direct\)$/.test(summary)
+function AttachmentDownload({
+ attachment,
+ onDownload
+}: {
+ attachment: KanbanAttachment
+ onDownload: (path: string, suggestedName: string) => Promise<unknown>
+}) {
+ const { t } = useI18n()
+
+ const download = useMutation({
+ mutationFn: () => onDownload(attachment.stored_path!, attachment.filename),
+ onError: err => host.notify({ kind: 'error', message: errText(err) })
+ })
+
+ return (
+ <Button
+ aria-label={`${t.fileMenu.download} ${attachment.filename}`}
+ disabled={!attachment.stored_path?.trim() || download.isPending}
+ onClick={() => download.mutate()}
+ size="xs"
+ variant="ghost"
+ >
+ <Codicon name={download.isPending ? 'sync' : 'cloud-download'} size="0.75rem" spinning={download.isPending} />
+ {attachment.filename}
+ </Button>
+ )
+}
+
function AttachmentsSection({
attachments,
onUpload,
+ onDownload,
pending
}: {
+ onDownload: (path: string, suggestedName: string) => Promise<unknown>
attachments: KanbanAttachment[]
onUpload: (file: File) => void
pending: boolean
@@ -515,8 +546,7 @@ function AttachmentsSection({
<ul className="flex flex-col gap-1">
{attachments.map(attachment => (
<li className="flex items-center gap-1.5 text-[0.75rem] text-(--ui-text-tertiary)" key={attachment.id}>
- <Codicon name="file" size="0.75rem" />
- {attachment.filename}
+ <AttachmentDownload attachment={attachment} onDownload={onDownload} />
</li>
))}
</ul>
@@ -1086,6 +1116,7 @@ export function TaskDrawer({
{Array.isArray(detail.attachments) && (
<AttachmentsSection
attachments={detail.attachments}
+ onDownload={detail.downloadAttachment}
onUpload={file => uploadMut.mutate(file)}
pending={uploadMut.isPending}
/>
diff --git a/apps/desktop/src/plugins/kanban/types.ts b/apps/desktop/src/plugins/kanban/types.ts
index b629e7679ea..4d6e1b5dd94 100644
--- a/apps/desktop/src/plugins/kanban/types.ts
+++ b/apps/desktop/src/plugins/kanban/types.ts
@@ -88,6 +88,7 @@ export interface KanbanEvent {
export interface KanbanAttachment {
id: number | string
filename: string
+ stored_path?: null | string
size?: null | number
}
diff --git a/apps/desktop/src/sdk/index.ts b/apps/desktop/src/sdk/index.ts
index b148b7e80bd..2045809eb3c 100644
--- a/apps/desktop/src/sdk/index.ts
+++ b/apps/desktop/src/sdk/index.ts
@@ -22,6 +22,8 @@ import { atom, computed, type ReadableAtom } from 'nanostores'
import type { ReactNode } from 'react'
import { capabilityScoped } from '@/api/client'
+
+export { captureGatewayFileDownload } from '@/api/file-download'
import { PRIMARY_SESSION_VIEW } from '@/app/chat/session-view'
import { openSession, type OpenSessionIntent } from '@/app/open-session'
import { syncWorkspaceRoute } from '@/app/routes'
diff --git a/website/docs/developer-guide/desktop-plugin-sdk.md b/website/docs/developer-guide/desktop-plugin-sdk.md
index dfdb3846554..e2c5506076f 100644
--- a/website/docs/developer-guide/desktop-plugin-sdk.md
+++ b/website/docs/developer-guide/desktop-plugin-sdk.md
@@ -45,6 +45,15 @@ plugin, and fail to resolve in a disk plugin). Capability comes in tiers:
restart the gateway, subscribe to the gateway event stream.
- **`host.request`** — the gateway JSON-RPC door: sessions, config, skills,
cron — everything the app itself calls.
+- **`captureGatewayFileDownload()`** — capture a gateway file-save action
+ immediately before starting a REST read, and retain it alongside the returned
+ data. The action `(storedPath, suggestedName) => Promise<{ saved, canceled?, path? }>`
+ keeps that read's connection/profile scope even if the user switches hosts
+ before clicking. Invoke only on an explicit user download gesture, using the
+ backend's persisted file path, never a guessed workspace path. Electron handles
+ authenticated streaming, the native save dialog, and older-gateway fallback;
+ plugins never receive credentials or open remote paths with `file://`.
+ Cancellation resolves quietly; missing bridge/path and failed saves reject.
- **`ctx.rest` / `ctx.socket`** — your plugin's own backend namespace
(`/api/plugins/<id>`) if you ship a `plugin_api.py`.
- **`ui.*`** — the design language: the app's real components, theme variables,