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 after a wake retry
Alfredo Lopez14a879e2b216
diff --git a/external-rest-api/src/releaseManifest.ts b/external-rest-api/src/releaseManifest.tsindex f13defdec..074e309d4 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.96',- rpcProxyVersion: '0.1.120',+ rpcProxyVersion: '0.1.121',desktopVersion: '0.10.0',minimumDesktopVersion: '0.1.252',}diff --git a/rpc-proxy/package-lock.json b/rpc-proxy/package-lock.jsonindex 173582f76..4aa1fb94d 100644--- a/rpc-proxy/package-lock.json+++ b/rpc-proxy/package-lock.json@@ -1,12 +1,12 @@{"name": "clerum-rpc-proxy",- "version": "0.1.120",+ "version": "0.1.121","lockfileVersion": 3,"requires": true,"packages": {"": {"name": "clerum-rpc-proxy",- "version": "0.1.120",+ "version": "0.1.121","dependencies": {"@clerum/jwt-key-policy": "file:../packages/jwt-key-policy","cors": "^2.8.5",diff --git a/rpc-proxy/package.json b/rpc-proxy/package.jsonindex cd84dce4f..b7a5367b0 100644--- a/rpc-proxy/package.json+++ b/rpc-proxy/package.json@@ -1,6 +1,6 @@{"name": "clerum-rpc-proxy",- "version": "0.1.120",+ "version": "0.1.121","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 297239393..b96b78ec6 100644--- a/rpc-proxy/src/__tests__/wake-and-hold-route.test.ts+++ b/rpc-proxy/src/__tests__/wake-and-hold-route.test.ts@@ -387,6 +387,84 @@ describe('POST /rpc/hosts/:hostRef/messages wake-and-hold triggers', () => {}})+ // A real Host that answers the first POST with a draining 503 and the+ // post-wake retry with `retryStatus`, so both errors come from the real+ // forwardHostMessageToHost rather than hand-built UpstreamHostErrors.+ async function withDrainingThenHost(+ retryStatus: number,+ retryBody: Record<string, unknown>,+ run: (hostHits: () => number) => Promise<void>+ ): Promise<void> {+ let hits = 0+ const server = createServer((req, res) => {+ hits += 1+ const hit = hits+ req.resume()+ req.on('end', () => {+ res.writeHead(hit === 1 ? 503 : retryStatus, { 'content-type': 'application/json' })+ res.end(+ JSON.stringify(hit === 1 ? { code: 'host_draining', retryAfterMs: 1000 } : retryBody)+ )+ })+ })+ 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+ )+ controlApiMock.requestHostWakeFromControlApi.mockResolvedValue({+ kind: 'active',+ wakeGeneration: 21,+ })+ try {+ await run(() => hits)+ } finally {+ server.closeAllConnections()+ await new Promise<void>(resolve => server.close(() => resolve()))+ }+ }++ it('passes a Host 413 on the post-wake retry through as the same 413 as the direct path', async () => {+ await withDrainingThenHost(413, { error: 'Payload Too Large' }, async hostHits => {+ const response = await postMessage(makeApp())++ // Witness: the 503 entered the wake hold and the retry reached the Host.+ expect(controlApiMock.requestHostWakeFromControlApi).toHaveBeenCalledTimes(1)+ expect(hostHits()).toBe(2)+ expect(response.status).toBe(413)+ expect(response.body).toEqual({ error: 'Payload Too Large' })+ })+ })++ it('still answers 200 when the post-wake retry is accepted by the Host', async () => {+ await withDrainingThenHost(200, { success: true, taskId: 't-after-wake' }, async hostHits => {+ const response = await postMessage(makeApp())++ expect(controlApiMock.requestHostWakeFromControlApi).toHaveBeenCalledTimes(1)+ expect(hostHits()).toBe(2)+ expect(response.status).toBe(200)+ expect(response.body).toEqual({ success: true, taskId: 't-after-wake' })+ })+ })++ it('keeps a non-413 Host failure on the post-wake retry on the 502 path', async () => {+ await withDrainingThenHost(500, { error: 'boom' }, async hostHits => {+ const response = await postMessage(makeApp())++ expect(controlApiMock.requestHostWakeFromControlApi).toHaveBeenCalledTimes(1)+ expect(hostHits()).toBe(2)+ expect(response.status).toBe(502)+ expect(response.body).toEqual({ error: 'Upstream host unavailable' })+ })+ })+it('forwards at most the Desktop headroom of bytes beyond the inbound Desktop body', async () => {let forwardedRaw = ''const server = createServer((req, res) => {diff --git a/rpc-proxy/src/middleware/chatJsonBody.ts b/rpc-proxy/src/middleware/chatJsonBody.tsindex 2c9625cdb..528b66573 100644--- a/rpc-proxy/src/middleware/chatJsonBody.ts+++ b/rpc-proxy/src/middleware/chatJsonBody.ts@@ -16,9 +16,11 @@ import express, { type NextFunction, type Request, type Response } from 'express* base64 and MINUS credited file base64. Without those subtractions the* attachment budgets would become a general 24MiB text budget.*- * `kind:'file'` has its own quota (issue #678): at most 11MiB decoded per file- * and 16MiB of base64 in total, credited exactly like images. The text, the- * JSON envelope and the non-base64 fields of each file entry stay in the 6MiB+ * `kind:'file'` has its own credited quota (issue #678), separate from the+ * image budget and with its own qualification rules (inspectChatFileBudget):+ * at most 20 files, 11MiB decoded per file and 16MiB of base64 (not decoded+ * bytes) in total, with no MIME or signature check. The text, the JSON+ * envelope and the non-base64 fields of each file entry stay in the 6MiB* share, so a text-only message keeps its 6MiB ceiling.** Mount `chatJsonBody` on `POST /rpc/hosts/:hostRef/messages` right afterdiff --git a/rpc-proxy/src/routes/rpc.ts b/rpc-proxy/src/routes/rpc.tsindex 7a4bc99cb..cac505d38 100644--- a/rpc-proxy/src/routes/rpc.ts+++ b/rpc-proxy/src/routes/rpc.ts@@ -99,6 +99,21 @@ export function respondUpstreamUnavailable(res: ExpressResponse, error: unknown)res.status(502).json({ error: 'Upstream host unavailable' })}+/**+ * Terminal mapping for a failed Host message forward, on the first attempt and+ * on the post-wake retry alike. A Host 413 is the client's body to fix, not an+ * unavailable Host: it gets the same body as chatJsonBody's own 413, so the+ * Desktop maps a Host refusal and a proxy refusal identically. Everything else+ * keeps the respondUpstreamUnavailable mapping, including its headersSent guard.+ */+function respondHostMessageForwardFailure(res: ExpressResponse, error: unknown): void {+ if (isHostPayloadTooLargeError(error) && !res.headersSent) {+ res.status(413).json({ error: 'Payload Too Large' })+ return+ }+ respondUpstreamUnavailable(res, error)+}+/*** Guards a `next(error)` handoff against a committed response (§11.5). Express* routes an error to the default handler which, when headers are already sent,@@ -536,17 +551,11 @@ export function createRpcRouter(): Router {})res.status(200).json(retried)},- respondLegacy: legacyError => respondUpstreamUnavailable(res, legacyError),+ respondLegacy: legacyError => respondHostMessageForwardFailure(res, legacyError),})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)+ respondHostMessageForwardFailure(res, error)return}// The upstream accepted the message: the request is resolved. Writingdiff --git a/rpc-proxy/src/services/wakeAndHold.ts b/rpc-proxy/src/services/wakeAndHold.tsindex 56bee3b57..a2209eda2 100644--- a/rpc-proxy/src/services/wakeAndHold.ts+++ b/rpc-proxy/src/services/wakeAndHold.ts@@ -658,7 +658,11 @@ export type RespondWithWakeAndHoldOptions = {* transfer) that `timeoutMs` alone does not bound.*/attemptUpstream: (timeoutMs: number, deadlineMs: number) => Promise<void>- /** Writes today's error response (502/504) — the pre-wake behavior. */+ /**+ * Writes the route's pre-wake error response (502/504, plus any route-owned+ * mapping such as the messages route's Host 413). Also receives the error of+ * a post-wake retry the Host answered with a non-availability failure.+ */respondLegacy: (error: unknown) => voidcoordinator?: WakeAndHoldCoordinator/** Absolute deadline captured before a route's initial availability probe. */