Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript

fix(gfs): add timeout + fail-loud passthrough to /external/gfs proxy (#281)

Alfredo Lopez36e42e7686d8
diff --git a/control-api/package-lock.json b/control-api/package-lock.json
index 728b82da3..2534f3b9f 100644
--- a/control-api/package-lock.json
+++ b/control-api/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-control-api",
- "version": "0.1.426",
+ "version": "0.1.427",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-control-api",
- "version": "0.1.426",
+ "version": "0.1.427",
"dependencies": {
"@clerum/image-policy": "file:../packages/image-policy",
"@clerum/llm-providers": "file:../packages/llm-providers",
diff --git a/control-api/package.json b/control-api/package.json
index 0a02298e8..62fc2c282 100644
--- a/control-api/package.json
+++ b/control-api/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-control-api",
- "version": "0.1.426",
+ "version": "0.1.427",
"description": "Internal Clerum control plane REST API + UI for managing CRDs and secrets",
"main": "dist/main.js",
"engines": {
diff --git a/control-api/src/routes/external/gfs.ts b/control-api/src/routes/external/gfs.ts
index d3a655351..c1ca65ece 100644
--- a/control-api/src/routes/external/gfs.ts
+++ b/control-api/src/routes/external/gfs.ts
@@ -527,10 +527,40 @@ async function proxyReadToGfsc(
scopes: [GFS_READ_SCOPE],
})
const target = `${config.gfscBaseUrl.replace(/\/+$/, '')}${gfscPath}`
- const upstream = await fetch(target, {
- method: 'GET',
- headers: { authorization: `Bearer ${token}` },
- })
+ let upstream: Response
+ try {
+ upstream = await fetch(target, {
+ method: 'GET',
+ headers: { authorization: `Bearer ${token}` },
+ signal: AbortSignal.timeout(config.gfscProxyTimeoutMs),
+ })
+ } catch (err) {
+ const timedOut = err instanceof Error && err.name === 'TimeoutError'
+ rootLogger.error(
+ { err, gfscPath, timeoutMs: config.gfscProxyTimeoutMs },
+ timedOut
+ ? 'gfs external read proxy: gfsc fetch timed out'
+ : 'gfs external read proxy: gfsc fetch failed'
+ )
+ res.status(timedOut ? 504 : 502).json({ error: timedOut ? 'gfsc_timeout' : 'gfsc_unreachable' })
+ return
+ }
+ if (upstream.status >= 400) {
+ // Error envelopes are small JSON: buffer, LOG, forward verbatim (a gfsc 5xx on a
+ // read was invisible at this hop before). Success bodies keep streaming below.
+ const errorBody = await upstream.text()
+ if (upstream.status >= 500) {
+ rootLogger.error(
+ { gfscPath, status: upstream.status, body: errorBody.slice(0, 2048) },
+ 'gfs external read proxy: gfsc upstream error'
+ )
+ }
+ res.status(upstream.status)
+ const ct = upstream.headers.get('content-type')
+ if (ct) res.setHeader('content-type', ct)
+ res.send(errorBody)
+ return
+ }
res.status(upstream.status)
for (const h of ['content-type', 'content-disposition', 'content-length']) {
const v = upstream.headers.get(h)
@@ -575,14 +605,43 @@ async function proxyMutationToGfsc(
const target = `${config.gfscWriteBaseUrl.replace(/\/+$/, '')}${gfscPath}`
const headers: Record<string, string> = { 'content-type': 'application/json' }
headers['author' + 'ization'] = ['Bearer', token].join(' ')
- const upstream = await fetch(target, {
- method,
- headers,
- body: JSON.stringify(req.body ?? {}),
- })
+ let upstream: Response
+ try {
+ upstream = await fetch(target, {
+ method,
+ headers,
+ body: JSON.stringify(req.body ?? {}),
+ // Homologous with the operator proxy (routes/gfs/proxy.ts): without this the
+ // fetch had NO timeout — a hung gfsc pinned the desktop upload indefinitely.
+ signal: AbortSignal.timeout(config.gfscProxyTimeoutMs),
+ })
+ } catch (err) {
+ const timedOut = err instanceof Error && err.name === 'TimeoutError'
+ rootLogger.error(
+ { err, gfscPath, method, timeoutMs: config.gfscProxyTimeoutMs },
+ timedOut
+ ? 'gfs external mutation proxy: gfsc fetch timed out'
+ : 'gfs external mutation proxy: gfsc fetch failed'
+ )
+ res.status(timedOut ? 504 : 502).json({ error: timedOut ? 'gfsc_timeout' : 'gfsc_unreachable' })
+ return
+ }
res.status(upstream.status)
const contentType = upstream.headers.get('content-type')
if (contentType) res.setHeader('content-type', contentType)
const text = await upstream.text()
+ // Never silent: surface a gfsc 4xx/5xx at this hop (the base64 RangeError 500 was
+ // invisible here before) while forwarding the body verbatim.
+ if (upstream.status >= 500) {
+ rootLogger.error(
+ { gfscPath, method, status: upstream.status, body: text.slice(0, 2048) },
+ 'gfs external mutation proxy: gfsc upstream error'
+ )
+ } else if (upstream.status >= 400) {
+ rootLogger.warn(
+ { gfscPath, method, status: upstream.status, body: text.slice(0, 2048) },
+ 'gfs external mutation proxy: gfsc upstream client error'
+ )
+ }
res.send(text)
}
diff --git a/control-api/test/routes.externalGfs.test.ts b/control-api/test/routes.externalGfs.test.ts
index 3adcc6818..9b1a6153d 100644
--- a/control-api/test/routes.externalGfs.test.ts
+++ b/control-api/test/routes.externalGfs.test.ts
@@ -30,6 +30,7 @@ vi.mock('../src/config.js', () => ({
config: {
gfscBaseUrl: 'http://gfsc.gfs.svc:8087',
gfscWriteBaseUrl: 'http://gfsc-writer.gfs.svc:8087',
+ gfscProxyTimeoutMs: 300_000,
// The eligibility guard maps caller agent names to 1st:<hostsNamespace>/<name>.
hostsNamespace: 'mcp-host',
},
@@ -1028,6 +1029,7 @@ describe('user resource mutations via gfsc proxy', () => {
method: 'POST',
headers: expect.objectContaining({ 'content-type': 'application/json' }),
body: JSON.stringify({ name: 'docs', kind: 'directory' }),
+ signal: expect.any(AbortSignal),
}
)
})
@@ -1060,9 +1062,46 @@ describe('user resource mutations via gfsc proxy', () => {
method: 'DELETE',
headers: expect.objectContaining({ 'content-type': 'application/json' }),
body: JSON.stringify({ ifMatch: 3 }),
+ signal: expect.any(AbortSignal),
}
)
})
+
+ it('returns 504 gfsc_timeout when the gfsc mutation fetch times out', async () => {
+ auth()
+ const fetchMock = vi.fn(async () => {
+ const err = new Error('The operation was aborted due to timeout')
+ err.name = 'TimeoutError'
+ throw err
+ })
+ vi.stubGlobal('fetch', fetchMock)
+ const app = await buildApp()
+ const res = await request(app)
+ .post(`/external/gfs/resources/${R}/children`)
+ .set('x-user-session-token', 'sess')
+ .send({ name: 'docs', kind: 'file', contentBase64: 'AAAA' })
+ expect(res.status).toBe(504)
+ expect(res.body).toEqual({ error: 'gfsc_timeout' })
+ })
+
+ it('forwards a gfsc 5xx on a user mutation verbatim (never a silent 500)', async () => {
+ auth()
+ const fetchMock = vi.fn(
+ async () =>
+ new Response(JSON.stringify({ ok: false, error: { code: 'internal' } }), {
+ status: 500,
+ headers: { 'content-type': 'application/json' },
+ })
+ )
+ vi.stubGlobal('fetch', fetchMock)
+ const app = await buildApp()
+ const res = await request(app)
+ .post(`/external/gfs/resources/${R}/children`)
+ .set('x-user-session-token', 'sess')
+ .send({ name: 'docs', kind: 'file', contentBase64: 'AAAA' })
+ expect(res.status).toBe(500)
+ expect(res.body).toEqual({ ok: false, error: { code: 'internal' } })
+ })
})
describe('GET /external/gfs/resources', () => {