can1357/oh-my-pi · error

Invalid Content-Length header: ${value}

Error message

Invalid Content-Length header: ${value}

What it means

contentLength parses the Content-Length HTTP header from captured trace headers. It returns 0 when the header is absent, but if present the value must parse as a non-negative safe integer; anything else (negative, non-numeric, overflow) throws this error rather than returning a corrupt length.

Source

Thrown at packages/coding-agent/src/cli/claude-trace-cli.ts:184

	return undefined;
}

function hasChunkedTransfer(headers: readonly HeaderEntry[]): boolean {
	const value = headerValue(headers, "transfer-encoding");
	return (
		value
			?.toLowerCase()
			.split(",")
			.some(part => part.trim() === "chunked") === true
	);
}

function contentLength(headers: readonly HeaderEntry[]): number {
	const value = headerValue(headers, "content-length");
	if (!value) return 0;
	const parsed = Number.parseInt(value, 10);
	if (!Number.isSafeInteger(parsed) || parsed < 0) {
		throw new Error(`Invalid Content-Length header: ${value}`);
	}
	return parsed;
}
interface PendingCapturedRequest {
	target: string;
	request: CapturedRequest;
}

function parseHeaders(headText: string): { startLine: string; headers: HeaderEntry[] } {
	const lines = headText.split("\r\n");
	const startLine = lines[0] ?? "";
	const headers: HeaderEntry[] = [];
	for (let i = 1; i < lines.length; i++) {
		const line = lines[i]!;
		const colon = line.indexOf(":");
		if (colon <= 0) continue;
		headers.push({ name: line.slice(0, colon), value: line.slice(colon + 1).trim() });
	}

View on GitHub (pinned to 9690622007)

Solutions

  1. Open the trace file and fix or remove the malformed content-length header value
  2. Re-capture the trace from the source request/response
  3. If the response was chunked, remove the bogus Content-Length header so contentLength() returns 0 and framing falls back to chunk handling

Example fix

// before (trace header)
content-length: -1
// after
content-length: 1024   // or delete the header entirely
Defensive patterns

Strategy: type-guard

Validate before calling

const raw = headerValue(headers, "content-length");
if (raw !== undefined && (!/^\d+$/.test(raw) || !Number.isSafeInteger(Number(raw))))
  throw new Error(`Trace has malformed Content-Length: ${raw}`);

Type guard

function isValidContentLength(value: string | undefined): value is string {
  return value !== undefined && /^\d+$/.test(value) && Number.isSafeInteger(Number(value));
}

Try / catch

try {
  const len = contentLength(headers);
} catch (err) {
  if (err instanceof Error && err.message.startsWith("Invalid Content-Length")) {
    // fall back to chunked framing or skip this captured entry
    const len = 0;
  } else throw err;
}

Prevention

When it happens

Trigger: Replaying/analyzing a Claude trace whose captured request/response headers contain a malformed Content-Length, e.g. "abc", "-5", or a value above Number.MAX_SAFE_INTEGER.

Common situations: Hand-edited or corrupted trace files; proxies injecting chunked-transfer responses with a bogus Content-Length; traces recorded by non-conforming tools; extremely large bodies exceeding safe-integer range.

Related errors


AI-assisted analysis of can1357/oh-my-pi@9690622007 (2026-08-31). Data as JSON: /api/errors/af993ef826b82072. Report an issue: GitHub.