Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix(proxy): preserve bounded artifact transfers
Alfredo Lopezfdba1c5d4616
diff --git a/external-rest-api/src/releaseManifest.ts b/external-rest-api/src/releaseManifest.tsindex ae4b110f3..6e8d28411 100644--- a/external-rest-api/src/releaseManifest.ts+++ b/external-rest-api/src/releaseManifest.ts@@ -9,7 +9,7 @@ export type ReleaseManifest = {export const releaseManifest: ReleaseManifest = {releaseId: 'v0.10.0',externalRestApiVersion: '0.1.84',- rpcProxyVersion: '0.1.98',+ rpcProxyVersion: '0.1.99',desktopVersion: '0.10.0',minimumDesktopVersion: '0.1.252',}diff --git a/rpc-proxy/package-lock.json b/rpc-proxy/package-lock.jsonindex ff5167503..86f4f3b84 100644--- a/rpc-proxy/package-lock.json+++ b/rpc-proxy/package-lock.json@@ -1,12 +1,12 @@{"name": "clerum-rpc-proxy",- "version": "0.1.98",+ "version": "0.1.99","lockfileVersion": 3,"requires": true,"packages": {"": {"name": "clerum-rpc-proxy",- "version": "0.1.98",+ "version": "0.1.99","dependencies": {"cors": "^2.8.5","express": "^4.21.2",diff --git a/rpc-proxy/package.json b/rpc-proxy/package.jsonindex 10f1bbbd8..9d0c0b0f7 100644--- a/rpc-proxy/package.json+++ b/rpc-proxy/package.json@@ -1,6 +1,6 @@{"name": "clerum-rpc-proxy",- "version": "0.1.98",+ "version": "0.1.99","description": "External-facing RPC proxy for tenant-scoped MCP access","main": "dist/main.js","engines": {diff --git a/rpc-proxy/src/__tests__/approval-artifacts.test.ts b/rpc-proxy/src/__tests__/approval-artifacts.test.tsindex 24cd5d5c4..63b1f6f2e 100644--- a/rpc-proxy/src/__tests__/approval-artifacts.test.ts+++ b/rpc-proxy/src/__tests__/approval-artifacts.test.ts@@ -3,6 +3,7 @@ import express from 'express'import request from 'supertest'import { config } from '../config.js'import { createRpcRouter } from '../routes/rpc.js'+import { isUpstreamTimeoutError } from '../services/wakeAndHold.js'// ── Hoisted mocks ───────────────────────────────────────────────────const authTokenMock = vi.hoisted(() => ({@@ -79,6 +80,10 @@ function makeApp() {// Generic error handler to avoid unhandled rejections in testsapp.use((err: unknown, _req: express.Request, res: express.Response, _next: express.NextFunction) => {+ if (isUpstreamTimeoutError(err)) {+ res.status(504).json({ error: 'Gateway Timeout' })+ return+ }res.status(500).json({ error: err instanceof Error ? err.message : 'Internal error' })})@@ -104,6 +109,8 @@ function mockFetchResponse(// ── Setup / Teardown ────────────────────────────────────────────────const originalFetch = globalThis.fetchconst originalArtifactDownloadMaxBytes = config.artifactDownloadMaxBytes+const originalArtifactDownloadTimeoutMs = config.artifactDownloadTimeoutMs+const originalUpstreamTimeoutMs = config.upstreamTimeoutMsbeforeEach(() => {authTokenMock.verifyRpcToken.mockReset()@@ -123,12 +130,35 @@ beforeEach(() => {afterEach(() => {globalThis.fetch = originalFetchconfig.artifactDownloadMaxBytes = originalArtifactDownloadMaxBytes+ config.artifactDownloadTimeoutMs = originalArtifactDownloadTimeoutMs+ config.upstreamTimeoutMs = originalUpstreamTimeoutMs})// =====================================================================// Approval Routes// =====================================================================describe('POST /rpc/hosts/:hostRef/approvals/approve', () => {+ it('keeps the upstream timeout on the first approval attempt', async () => {+ config.upstreamTimeoutMs = 20+ let signal: AbortSignal | undefined+ globalThis.fetch = vi.fn().mockImplementation((_url: string, init: RequestInit) => {+ signal = init.signal as AbortSignal+ return new Promise((_resolve, reject) => {+ signal!.addEventListener('abort', () => reject(signal!.reason), { once: true })+ })+ })++ const app = makeApp()+ await request(app)+ .post('/rpc/hosts/chatllm/approvals/approve')+ .set('authorization', 'Bearer token')+ .send({ toolCallId: 'tc-timeout' })+ .expect(504)++ expect(signal?.aborted).toBe(true)+ expect(controlApiMock.requestHostWakeFromControlApi).not.toHaveBeenCalled()+ })+it('translates toolCallId to requestId in upstream body', async () => {let capturedBody: string | undefinedglobalThis.fetch = vi.fn().mockImplementation(async (_url: string, init: RequestInit) => {@@ -615,6 +645,34 @@ describe('POST /rpc/hosts/:hostRef/tasks/:taskId/cancel', () => {})describe('GET /rpc/hosts/:hostRef/artifacts/:filename/download', () => {+ it('allows the bounded body transfer to outlast the upstream header timeout', async () => {+ config.upstreamTimeoutMs = 5+ config.artifactDownloadTimeoutMs = 1_000+ let signal: AbortSignal | undefined+ globalThis.fetch = vi.fn().mockImplementation((_url: string, init: RequestInit) => {+ signal = init.signal as AbortSignal+ return Promise.resolve({+ ok: true,+ status: 200,+ headers: new Headers({ 'content-type': 'application/octet-stream' }),+ arrayBuffer: async () => {+ await new Promise(resolve => setTimeout(resolve, 25))+ expect(signal?.aborted).toBe(false)+ return new TextEncoder().encode('slow-artifact').buffer as ArrayBuffer+ },+ } as unknown as Response)+ })++ const app = makeApp()+ const response = await request(app)+ .get('/rpc/hosts/chatllm/artifacts/slow.bin/download')+ .set('authorization', 'Bearer token')+ .expect(200)++ expect(Buffer.from(response.body).toString()).toBe('slow-artifact')+ expect(signal?.aborted).toBe(false)+ })+it("rejects filename containing '..'", async () => {// Express normalizes "../" in paths, so use URL-encoded dots to test// the validation logic when the param actually contains ".."diff --git a/rpc-proxy/src/__tests__/wake-and-hold-route.test.ts b/rpc-proxy/src/__tests__/wake-and-hold-route.test.tsindex 4898ebdfd..120425472 100644--- a/rpc-proxy/src/__tests__/wake-and-hold-route.test.ts+++ b/rpc-proxy/src/__tests__/wake-and-hold-route.test.ts@@ -1,7 +1,11 @@import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'import express from 'express'+import { createServer } from 'node:http'+import type { AddressInfo } from 'node:net'import request from 'supertest'+import { config } from '../config.js'import { createRpcRouter } from '../routes/rpc.js'+import { forwardHostMessageToHost } from '../services/mcpHostRestService.js'const authTokenMock = vi.hoisted(() => ({verifyRpcToken: vi.fn(),@@ -62,6 +66,7 @@ const HOST_CONNECTION = {url: 'http://chatllm.mcp-host.svc.cluster.local:8080',headers: {},}+const originalUpstreamTimeoutMs = config.upstreamTimeoutMsfunction makeApp() {const app = express()@@ -98,6 +103,7 @@ beforeEach(() => {afterEach(() => {vi.useRealTimers()+ config.upstreamTimeoutMs = originalUpstreamTimeoutMs})describe('POST /rpc/hosts/:hostRef/messages wake-and-hold triggers', () => {@@ -351,6 +357,34 @@ describe('POST /rpc/hosts/:hostRef/messages wake-and-hold triggers', () => {expect(controlApiMock.requestHostWakeFromControlApi).not.toHaveBeenCalled()})+ it('returns 504 when the real host REST service times out a hanging HTTP request', async () => {+ config.upstreamTimeoutMs = 25+ const server = createServer(() => {+ // Leave the request unanswered so the service timeout must abort it.+ })+ await new Promise<void>((resolve, reject) => {+ server.once('error', reject)+ server.listen(0, '127.0.0.1', resolve)+ })+ const { port } = server.address() as AddressInfo+ serviceMock.resolveHostConnectionForUser.mockResolvedValue({+ ...HOST_CONNECTION,+ url: `http://127.0.0.1:${port}`,+ })+ serviceMock.forwardHostMessageToHost.mockImplementation(+ forwardHostMessageToHost as typeof serviceMock.forwardHostMessageToHost+ )++ try {+ await postMessage(makeApp()).expect(504, { error: 'Gateway Timeout' })+ expect(serviceMock.forwardHostMessageToHost).toHaveBeenCalledTimes(1)+ expect(controlApiMock.requestHostWakeFromControlApi).not.toHaveBeenCalled()+ } finally {+ server.closeAllConnections()+ await new Promise<void>(resolve => server.close(() => resolve()))+ }+ })+it('a wake retry that times out returns 504 instead of 502', async () => {const timeout = new Error('aborted')timeout.name = 'AbortError'diff --git a/rpc-proxy/src/config.ts b/rpc-proxy/src/config.tsindex 21ac2c62d..0f9c377b8 100644--- a/rpc-proxy/src/config.ts+++ b/rpc-proxy/src/config.ts@@ -12,6 +12,7 @@ type Config = {controlApiServiceName: stringcontrolApiCacheTtlMs: numberartifactDownloadMaxBytes: number+ artifactDownloadTimeoutMs: numberstreamMaxLifetimeMs: numberstreamIntervalMs: numberstreamKeepaliveMs: number@@ -183,6 +184,10 @@ export const config: Config = {artifactDownloadMaxBytes: parseArtifactDownloadMaxBytes(process.env.RPC_PROXY_ARTIFACT_DOWNLOAD_MAX_MB || '50'),+ artifactDownloadTimeoutMs: parsePositiveIntMs(+ 'RPC_PROXY_ARTIFACT_DOWNLOAD_TIMEOUT_MS',+ process.env.RPC_PROXY_ARTIFACT_DOWNLOAD_TIMEOUT_MS || '60000'+ ),streamMaxLifetimeMs: Number(process.env.RPC_PROXY_STREAM_MAX_LIFETIME_MS || 600000),streamIntervalMs: Number(process.env.RPC_PROXY_STREAM_INTERVAL_MS || 3000),streamKeepaliveMs: Number(process.env.RPC_PROXY_STREAM_KEEPALIVE_MS || 15000),diff --git a/rpc-proxy/src/routes/rpc.ts b/rpc-proxy/src/routes/rpc.tsindex 75e764723..09d2bfccd 100644--- a/rpc-proxy/src/routes/rpc.ts+++ b/rpc-proxy/src/routes/rpc.ts@@ -1433,37 +1433,49 @@ export function createRpcRouter(): Router {// The success path only commits (`res.send`) at the very end, so a// wake retry before that point is safe from duplicate delivery.const attemptDownload = async (timeoutMs = config.upstreamTimeoutMs) => {- const response = await fetch(- `${baseUrl}/v1/runtime/artifacts/${encodeURIComponent(filename)}/download`,- {- headers: { ...host.headers },- signal: AbortSignal.timeout(timeoutMs),- }- )- if (!response.ok) {- const errBody = await response.text()- const draining = sessionDrainingFence(response, errBody)- if (draining) throw draining- res.status(response.status).send(errBody)- return- }- const contentType = response.headers.get('content-type') || 'application/octet-stream'- const safeFilename = filename.replace(/[^a-zA-Z0-9_.-]/g, '_')- const redaction = response.headers.get('x-clerum-redaction')- let buffer: Buffer+ const controller = new AbortController()+ let timeout = setTimeout(() => controller.abort(), timeoutMs)try {- buffer = await readArtifactResponseBuffer(response)- } catch (error) {- if (error instanceof ProxyArtifactTooLargeError) {- res.status(413).json({ error: 'Artifact too large to download' })+ const response = await fetch(+ `${baseUrl}/v1/runtime/artifacts/${encodeURIComponent(filename)}/download`,+ {+ headers: { ...host.headers },+ signal: controller.signal,+ }+ )+ // The upstream timeout protects connection and header latency. Once+ // headers arrive, allow the separately bounded artifact transfer to+ // finish; the small JSON-operation deadline must not truncate a+ // valid large download while its body is streaming.+ clearTimeout(timeout)+ timeout = setTimeout(() => controller.abort(), config.artifactDownloadTimeoutMs)+ if (!response.ok) {+ const errBody = await response.text()+ const draining = sessionDrainingFence(response, errBody)+ if (draining) throw draining+ res.status(response.status).send(errBody)return}- throw error+ const contentType = response.headers.get('content-type') || 'application/octet-stream'+ const safeFilename = filename.replace(/[^a-zA-Z0-9_.-]/g, '_')+ const redaction = response.headers.get('x-clerum-redaction')+ let buffer: Buffer+ try {+ buffer = await readArtifactResponseBuffer(response)+ } catch (error) {+ if (error instanceof ProxyArtifactTooLargeError) {+ res.status(413).json({ error: 'Artifact too large to download' })+ return+ }+ throw error+ }+ res.setHeader('Content-Type', contentType)+ res.setHeader('Content-Disposition', `attachment; filename="${safeFilename}"`)+ if (redaction) res.setHeader('X-Clerum-Redaction', redaction)+ res.send(buffer)+ } finally {+ clearTimeout(timeout)}- res.setHeader('Content-Type', contentType)- res.setHeader('Content-Disposition', `attachment; filename="${safeFilename}"`)- if (redaction) res.setHeader('X-Clerum-Redaction', redaction)- res.send(buffer)}try {await attemptDownload()