Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix(rpc-proxy): pass a Host 413 through on the messages route
Alfredo Lopez8ffae916779e
diff --git a/external-rest-api/src/releaseManifest.ts b/external-rest-api/src/releaseManifest.tsindex e3307d2e2..6e3776060 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.85',- rpcProxyVersion: '0.1.113',+ rpcProxyVersion: '0.1.114',desktopVersion: '0.10.0',minimumDesktopVersion: '0.1.252',}diff --git a/rpc-proxy/package-lock.json b/rpc-proxy/package-lock.jsonindex c4fee10b4..92c4a09c0 100644--- a/rpc-proxy/package-lock.json+++ b/rpc-proxy/package-lock.json@@ -1,12 +1,12 @@{"name": "clerum-rpc-proxy",- "version": "0.1.113",+ "version": "0.1.114","lockfileVersion": 3,"requires": true,"packages": {"": {"name": "clerum-rpc-proxy",- "version": "0.1.113",+ "version": "0.1.114","dependencies": {"cors": "^2.8.5","express": "^4.21.2",diff --git a/rpc-proxy/package.json b/rpc-proxy/package.jsonindex b0d3ec855..1c954d2ce 100644--- a/rpc-proxy/package.json+++ b/rpc-proxy/package.json@@ -1,6 +1,6 @@{"name": "clerum-rpc-proxy",- "version": "0.1.113",+ "version": "0.1.114","description": "External-facing RPC proxy for tenant-scoped MCP access","main": "dist/main.js","engines": {diff --git a/rpc-proxy/src/__tests__/wake-and-hold-route.test.ts b/rpc-proxy/src/__tests__/wake-and-hold-route.test.tsindex 08fea4551..297239393 100644--- a/rpc-proxy/src/__tests__/wake-and-hold-route.test.ts+++ b/rpc-proxy/src/__tests__/wake-and-hold-route.test.ts@@ -1,5 +1,6 @@import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'import express from 'express'+import { randomUUID } from 'node:crypto'import { createServer } from 'node:http'import type { AddressInfo } from 'node:net'import request from 'supertest'@@ -67,6 +68,11 @@ const HOST_CONNECTION = {url: 'http://chatllm.mcp-host.svc.cluster.local:8080',headers: {},}+// Mirrors desktop-app/ui/src/constants/attachments.ts COMPOSER_FORWARDED_FIELDS_BYTES:+// the bytes rpc-proxy may add to a Desktop body before forwarding it to the Host.+const DESKTOP_FORWARDED_FIELDS_HEADROOM_BYTES = 2048+// traceContext.ts caps each correlation ref at 256 characters; `edge-request:` is 13.+const LONGEST_RETAINED_REQUEST_ID_LENGTH = 256 - 'edge-request:'.lengthconst originalUpstreamTimeoutMs = config.upstreamTimeoutMsconst originalFetch = globalThis.fetch@@ -345,6 +351,95 @@ describe('POST /rpc/hosts/:hostRef/messages wake-and-hold triggers', () => {expect(controlApiMock.requestHostWakeFromControlApi).not.toHaveBeenCalled()})+ it('passes a real Host 413 through as 413 Payload Too Large without entering the wake hold', async () => {+ let hostHits = 0+ const server = createServer((req, res) => {+ hostHits += 1+ req.resume()+ req.on('end', () => {+ res.writeHead(413, { 'content-type': 'application/json' })+ res.end(JSON.stringify({ error: 'Payload Too Large' }))+ })+ })+ 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 {+ const response = await postMessage(makeApp())++ expect(response.status).toBe(413)+ expect(response.body).toEqual({ error: 'Payload Too Large' })+ expect(hostHits).toBe(1)+ expect(controlApiMock.requestHostWakeFromControlApi).not.toHaveBeenCalled()+ } finally {+ server.closeAllConnections()+ await new Promise<void>(resolve => server.close(() => resolve()))+ }+ })++ it('forwards at most the Desktop headroom of bytes beyond the inbound Desktop body', async () => {+ let forwardedRaw = ''+ const server = createServer((req, res) => {+ const chunks: Buffer[] = []+ req.on('data', chunk => chunks.push(chunk as Buffer))+ req.on('end', () => {+ forwardedRaw = Buffer.concat(chunks).toString('utf8')+ res.writeHead(200, { 'content-type': 'application/json' })+ res.end(JSON.stringify({ success: true, taskId: 't-growth' }))+ })+ })+ await new Promise<void>((resolve, reject) => {+ server.once('error', reject)+ server.listen(0, '127.0.0.1', resolve)+ })+ const { port } = server.address() as AddressInfo+ authTokenMock.verifyRpcToken.mockReturnValue({+ ...VALID_CLAIMS,+ sub: randomUUID(),+ teamId: randomUUID(),+ })+ serviceMock.resolveHostConnectionForUser.mockResolvedValue({+ ...HOST_CONNECTION,+ url: `http://127.0.0.1:${port}`,+ })+ serviceMock.forwardHostMessageToHost.mockImplementation(+ forwardHostMessageToHost as typeof serviceMock.forwardHostMessageToHost+ )+ const inboundRaw = JSON.stringify({ content: 'hello', threadId: randomUUID() })++ try {+ await request(makeApp())+ .post('/rpc/hosts/chatllm/messages')+ .set('authorization', 'Bearer token')+ .set('content-type', 'application/json')+ .set('x-request-id', 'r'.repeat(LONGEST_RETAINED_REQUEST_ID_LENGTH))+ .send(inboundRaw)+ .expect(200)++ const forwarded = JSON.parse(forwardedRaw) as {+ traceContext: { correlationRefs: string[] }+ }+ // Witness: the longest retained x-request-id is among the forwarded refs.+ expect(forwarded.traceContext.correlationRefs.length).toBe(3)+ expect(Buffer.byteLength(forwardedRaw) - Buffer.byteLength(inboundRaw)).toBeLessThanOrEqual(+ DESKTOP_FORWARDED_FIELDS_HEADROOM_BYTES+ )+ } finally {+ server.closeAllConnections()+ await new Promise<void>(resolve => server.close(() => resolve()))+ }+ })+it('an upstream AbortError keeps the 504 path without entering the wake hold', async () => {const abort = new Error('aborted')abort.name = 'AbortError'diff --git a/rpc-proxy/src/routes/rpc.ts b/rpc-proxy/src/routes/rpc.tsindex 99efa5c80..7a4bc99cb 100644--- a/rpc-proxy/src/routes/rpc.ts+++ b/rpc-proxy/src/routes/rpc.ts@@ -39,6 +39,7 @@ import {} from '../services/mcpProxyService.js'import { readMutatingResponseBody } from '../services/upstreamBody.js'import {+ isHostPayloadTooLargeError,isUpstreamTimeoutError,isWakeEligibleHostError,respondWithWakeAndHold,@@ -539,6 +540,12 @@ export function createRpcRouter(): Router {})return}+ if (isHostPayloadTooLargeError(error)) {+ // Same body as chatJsonBody's own 413, so the Desktop maps a Host+ // refusal and a proxy refusal identically.+ res.status(413).json({ error: 'Payload Too Large' })+ return+ }respondUpstreamUnavailable(res, error)return}diff --git a/rpc-proxy/src/services/wakeAndHold.ts b/rpc-proxy/src/services/wakeAndHold.tsindex 914dde3db..56bee3b57 100644--- a/rpc-proxy/src/services/wakeAndHold.ts+++ b/rpc-proxy/src/services/wakeAndHold.ts@@ -527,6 +527,16 @@ export function isHostDrainingError(error: unknown): boolean {return upstream.status === 503 && String(upstream.bodySnippet || '').includes('host_draining')}+/**+ * True for an upstream 413 from mcp-host: the Host refused the body as too+ * large, which is the client's error to fix, not a sign the Host is+ * unavailable. Matched structurally (name + status) like isHostDrainingError.+ */+export function isHostPayloadTooLargeError(error: unknown): boolean {+ if (!(error instanceof Error) || error.name !== 'UpstreamHostError') return false+ return (error as Error & { status?: unknown }).status === 413+}+/*** undici's own header/body timers (300 s defaults). They fire on a connection* the host accepted, so they prove a slow host, not a down one: a timeout, and