The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(desktop): bound a stalled native-gateway download body with an idle deadline
Teknium583d19dc330d
diff --git a/apps/desktop/electron/gateway-download-transport.test.ts b/apps/desktop/electron/gateway-download-transport.test.tsindex d18de3a7875..009b0702afe 100644--- a/apps/desktop/electron/gateway-download-transport.test.ts+++ b/apps/desktop/electron/gateway-download-transport.test.ts@@ -75,6 +75,42 @@ test('the connect timeout is dropped once headers arrive so a slow body streams}})+test('a body that stalls mid-stream is destroyed after the idle deadline, but the save dialog is not timed', async () => {+ // R4: dropping the connect timeout at headers must not leave a stalled body hanging the+ // save forever. The idle deadline runs only while the body is being read: an unread body+ // waiting on the native save dialog is the user's time, not a stall.+ const server = http.createServer((req, res) => {+ res.writeHead(200, { 'Content-Type': 'application/octet-stream' })++ if (req.url === '/whole') { res.end('bytes');++ return }++ res.write('head-')+ })++ await new Promise<void>(resolve => server.listen(0, '127.0.0.1', resolve))+ const url = `http://127.0.0.1:${(server.address() as net.AddressInfo).port}`++ const finish = async (res: http.IncomingMessage) => {+ await new Promise(resolve => setTimeout(resolve, 150))+ const chunks: Buffer[] = []++ for await (const chunk of res) { chunks.push(Buffer.from(chunk)) }++ return Buffer.concat(chunks).toString()+ }++ try {+ await expect(downloadViaTokenToFile(url + '/whole', 'remote-static', {}, finish, { bodyIdleTimeoutMs: 50 })).resolves.toBe('bytes')+ await expect(downloadViaTokenToFile(url + '/stall', 'remote-static', {}, finish, { bodyIdleTimeoutMs: 50 })).rejects.toThrow('stalled')+ } finally {+ destroyKeepaliveAgents()+ server.closeAllConnections()+ await new Promise<void>(resolve => server.close(() => resolve()))+ }+}, 3_000)+test.skipIf(process.platform === 'win32')('download retries mint a new private grant for every wire attempt and preserve remote auth', async () => {const home = await fs.mkdtemp(path.join(os.tmpdir(), 'download-auth-'))const grants: string[] = []diff --git a/apps/desktop/electron/gateway-download-transport.ts b/apps/desktop/electron/gateway-download-transport.tsindex 05da48b066b..2f250dd8359 100644--- a/apps/desktop/electron/gateway-download-transport.ts+++ b/apps/desktop/electron/gateway-download-transport.ts@@ -5,6 +5,36 @@ import { downloadAgentFor, withRetry } from './api-transport'import { DEFAULT_FETCH_TIMEOUT_MS, resolveTimeoutMs } from './hardening'import { nativeGatewayHttpHeaders } from './local-gateway'+// Bound a body that stops arriving once the consumer is reading it. The watchdog arms only+// when a 'data'/'readable' consumer attaches, so the unread body waiting on the native save+// dialog stays the user's time; it pauses under write backpressure and resets on every chunk.+function destroyStalledBody(res: http.IncomingMessage, idleMs: number) {+ let timer: ReturnType<typeof setTimeout> | undefined+ let watching = false+ const stop = () => clearTimeout(timer)++ const arm = () => {+ stop()+ timer = setTimeout(() => res.destroy(new Error(`Hermes backend download stalled: no data for ${idleMs}ms`)), idleMs)+ }++ const watch = () => {+ if (watching || res.destroyed) { return }+ watching = true+ // Added after the consumer's own listener, so this never switches a reader's mode.+ res.on('data', arm)+ arm()+ }++ res.on('newListener', event => {+ if (event === 'data' || event === 'readable') { queueMicrotask(watch) }+ })+ res.on('pause', stop)+ res.on('resume', () => { if (watching) { arm() } })+ res.once('end', stop)+ res.once('close', stop)+}+// Retry only the connection phase, never a save dialog or a partially saved body.export async function downloadViaTokenToFile(url, token, ctx, finalizeGatewayDownload, options: any = {}) {const parsed = new URL(url)@@ -41,6 +71,8 @@ export async function downloadViaTokenToFile(url, token, ctx, finalizeGatewayDowthrow new Error(`Unexpected download redirect (${res.statusCode})`)}+ destroyStalledBody(res, resolveTimeoutMs(options.bodyIdleTimeoutMs, DEFAULT_FETCH_TIMEOUT_MS))+return finalizeGatewayDownload(res, res.statusCode || 500, res.headers || {}, {...ctx, abort: () => res.destroy()})