{"record":{"id":"5634d7021a0fb957","repo":"NousResearch/hermes-agent","slug":"unreasonable-content-length-n","errorCode":null,"errorMessage":"unreasonable Content-Length: {n}","messagePattern":"unreasonable Content-Length: (.+?)","errorType":"exception","errorClass":"LSPProtocolError","httpStatus":null,"severity":"error","filePath":"agent/lsp/protocol.py","lineNumber":115,"sourceCode":"        if not line:\n            break  # blank line ends header block\n        try:\n            key, _, value = line.decode(\"ascii\").partition(\":\")\n        except UnicodeDecodeError as e:\n            raise LSPProtocolError(f\"non-ASCII LSP header: {line!r}\") from e\n        if not key:\n            raise LSPProtocolError(f\"malformed LSP header line: {line!r}\")\n        headers[key.strip().lower()] = value.strip()\n\n    cl = headers.get(\"content-length\")\n    if cl is None:\n        raise LSPProtocolError(f\"LSP message missing Content-Length: {headers!r}\")\n    try:\n        n = int(cl)\n    except ValueError as e:\n        raise LSPProtocolError(f\"non-integer Content-Length: {cl!r}\") from e\n    if n < 0 or n > 64 * 1024 * 1024:  # 64 MiB sanity cap\n        raise LSPProtocolError(f\"unreasonable Content-Length: {n}\")\n\n    try:\n        body = await reader.readexactly(n)\n    except asyncio.IncompleteReadError as e:\n        raise LSPProtocolError(\n            f\"truncated LSP body: expected {n} bytes, got {len(e.partial)}\"\n        ) from e\n\n    try:\n        return json.loads(body.decode(\"utf-8\"))\n    except json.JSONDecodeError as e:\n        raise LSPProtocolError(f\"invalid JSON in LSP body: {e}\") from e\n    except UnicodeDecodeError as e:\n        raise LSPProtocolError(f\"non-UTF-8 LSP body: {e}\") from e\n\n\ndef make_request(req_id: int, method: str, params: Any) -> dict:\n    \"\"\"Build a JSON-RPC 2.0 request envelope.\"\"\"","sourceCodeStart":97,"sourceCodeEnd":133,"githubUrl":"https://github.com/NousResearch/hermes-agent/blob/c896c09c42910c584c4c7d2325b58c14713ea42c/agent/lsp/protocol.py#L97-L133","documentation":"LSPProtocolError raised by the sanity cap on Content-Length: the parsed integer is negative or exceeds 64 MiB. Legitimate LSP messages are at most a few MB (huge diagnostics batches), so a value in that range means the length is corrupt — reading it would attempt a multi-gigabyte allocation or a negative read.","triggerScenarios":"Corrupted length from a crashed/desynced server; a server with an integer overflow computing length; the parser misaligned reading body bytes as a header after a previous message's length was wrong.","commonSituations":"Same family as other framing errors: stdout pollution, locale-formatted lengths, or a server bug in byte counting (using character count on non-ASCII JSON inflates nothing but using arbitrary offsets does).","solutions":["Restart the session — a desynced stream rarely recovers; a fresh client/server pair resets framing.","Update the server if the bug is known (check the server's issue tracker for Content-Length corruption).","Verify the message that triggers it: if a genuinely huge diagnostic batch exceeds 64 MiB (extremely unlikely), chunk server-side work; otherwise treat it as corruption."],"exampleFix":null,"handlingStrategy":"fallback","validationCode":"# server-side: sanity-check before writing the header\nMAX = 64 * 1024 * 1024\n\ndef frame(msg: dict) -> bytes:\n    body = json.dumps(msg).encode(\"utf-8\")\n    assert len(body) <= MAX, \"message exceeds LSP sanity cap; chunk it\"\n    return f\"Content-Length: {len(body)}\\r\\n\\r\\n\".encode() + body","typeGuard":null,"tryCatchPattern":"try:\n    msg = await read_message(reader)\nexcept LSPProtocolError as e:\n    if \"unreasonable Content-Length\" in str(e):\n        restart_session()  # desync/corruption; rebuild client+server\n    else:\n        raise","preventionTips":["Treat an out-of-cap length as corruption, never as a big message to allocate.","Chunk very large diagnostic batches server-side.","Restart on any framing sanity failure — the stream state is untrustworthy afterwards."],"tags":["lsp","protocol","framing","sanity-check"],"backgroundTag":null,"analyzedSha":"c896c09c42910c584c4c7d2325b58c14713ea42c","analyzedAt":"2026-08-14T17:18:01.089Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}