Skip to content

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

TypeScript

refactor(grok-llm-proxy): drop the depth check the raw-body scan already makes dead

Alfredo Lopez901fb8b51a69
diff --git a/docs/architecture/grok-subscription-transport-contract.md b/docs/architecture/grok-subscription-transport-contract.md
index def07ab63..16658e403 100644
--- a/docs/architecture/grok-subscription-transport-contract.md
+++ b/docs/architecture/grok-subscription-transport-contract.md
@@ -134,10 +134,10 @@ redeem. Once a slot is granted, the body must be read and parsed within
`BODY_READ_DEADLINE_MS` (10 s); otherwise the proxy answers 408
`request_timeout` with `connection: close` and frees the slot. After the grant,
the slot is also freed when the response closes for any other reason, which
-covers every refusal before the stream starts. The handler bounds the nesting
-depth of the parsed envelope (`LIMITS.maxNestingDepth + 6`, the control-api
-formula) before serializing it, and answers a deeper body 400
-`invalid_request`. A visual-gate refusal (queue full, wait exceeded) is
+covers every refusal before the stream starts. The raw-body scan
+refuses a body nested deeper than 70 (`BODY_STRUCTURE_LIMITS.maxDepth`) with
+400 `invalid_request` before `JSON.parse`, so `JSON.stringify` and the byte
+measurement never see a deeper tree. A visual-gate refusal (queue full, wait exceeded) is
answered 503 `provider_unavailable` and logged as
`grok_proxy_admission_refused` with `reason: visual_gate`.
diff --git a/grok-llm-proxy/src/server.ts b/grok-llm-proxy/src/server.ts
index f9f3da16b..c60a64901 100644
--- a/grok-llm-proxy/src/server.ts
+++ b/grok-llm-proxy/src/server.ts
@@ -108,33 +108,6 @@ function isRecord(value: unknown): value is Record<string, unknown> {
return value !== null && typeof value === 'object' && !Array.isArray(value)
}
-// body > request > messages[] > message > toolCalls[] > call > arguments: the
-// deepest free-form tree the contract accepts sits six containers below the
-// body root, and may itself nest maxNestingDepth containers. control-api
-// bounds its authorize body the same way (MAX_AUTHORIZE_BODY_DEPTH).
-const MAX_COMPLETION_BODY_DEPTH = LIMITS.maxNestingDepth + 6
-
-/**
- * True when the parsed body nests deeper than any envelope the contract
- * accepts. JSON.parse accepts far deeper input than JSON.stringify and the
- * contract's byte measurement can walk: both throw RangeError on it, before
- * the handler has released its admission. Iterative, so the depth cannot
- * overflow the stack here.
- */
-function exceedsCompletionBodyDepth(body: unknown): boolean {
- if (body === null || typeof body !== 'object') return false
- const stack: Array<[object, number]> = [[body, 1]]
- for (let entry = stack.pop(); entry; entry = stack.pop()) {
- const [node, depth] = entry
- if (depth > MAX_COMPLETION_BODY_DEPTH) return true
- const children: unknown[] = Array.isArray(node) ? node : Object.values(node)
- for (const child of children) {
- if (child !== null && typeof child === 'object') stack.push([child, depth + 1])
- }
- }
- return false
-}
-
function reject(res: Response, status: number, code: string): void {
if (res.headersSent) return
logger.warn({ event: 'grok_proxy_denied', code }, 'request denied')
@@ -144,7 +117,9 @@ function reject(res: Response, status: number, code: string): void {
// A8: every JSON parser scans the raw body before JSON.parse. JSON.parse
// allocates one heap object per container, so a body within the byte limit
// can still exhaust the heap; the scan refuses a body denser, more nested or
-// with more containers than any request the contract accepts.
+// with more containers than any request the contract accepts. Its depth bound
+// (LIMITS.maxNestingDepth + 6) also keeps JSON.stringify and the contract's
+// byte measurement from throwing RangeError on a deep body after parse.
const verifyBodyStructure = createBodyStructureVerify(BODY_STRUCTURE_LIMITS)
function boundedErrorHandler(err: unknown, _req: Request, res: Response, _next: () => void): void {
@@ -531,11 +506,6 @@ export function createProxyApps(
reject(res, 415, 'unsupported_media_type')
return
}
- if (exceedsCompletionBodyDepth(req.body)) {
- releaseAdmission()
- reject(res, 400, 'invalid_request')
- return
- }
const request = isRecord(req.body) ? req.body.request : undefined
const visualDeclared = isRecord(request) && request.schemaVersion === SCHEMA_VERSION_V2
const configuredLimit = visualDeclared ? config.maxVisualBodyBytes : config.maxBodyBytes
diff --git a/grok-llm-proxy/test/streamGate.handoff.test.ts b/grok-llm-proxy/test/streamGate.handoff.test.ts
index 9b5e33968..1d07d805e 100644
--- a/grok-llm-proxy/test/streamGate.handoff.test.ts
+++ b/grok-llm-proxy/test/streamGate.handoff.test.ts
@@ -575,8 +575,9 @@ describe('grok visual stream-gate handoff', () => {
const port = listen(
createProxyApps(config({ maxBodyBytes, maxVisualBodyBytes: 1024 * 1024 }))
)
- // JSON.parse accepts this depth; JSON.stringify and the contract's byte
- // measurement throw RangeError on it.
+ // JSON.parse accepts this depth. The raw-body scan refuses it (400
+ // body.structure.too.deep) before parsing; the visual slot must still be
+ // released.
const depth = 20_000
const body =
`{"executionTicket":"invalid-ticket","requestHash":"${'a'.repeat(64)}",` +