{"record":{"id":"bd3fd8dc1c8eff79","repo":"musistudio/claude-code-router","slug":"the-ccr-artifact-response-length-did-not-match-its","errorCode":null,"errorMessage":"The CCR artifact response length did not match its headers.","messagePattern":"The CCR artifact response length did not match its headers\\.","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/core/src/agents/codex/media-preview-bridge.ts","lineNumber":333,"sourceCode":"    throw new Error(\"Compressed CCR media artifacts are not accepted for inline preview.\");\n  }\n  if (!response.body) throw new Error(\"The CCR artifact response had no body.\");\n  const reader = response.body.getReader();\n  const chunks: Buffer[] = [];\n  let total = 0;\n  while (true) {\n    const part = await reader.read();\n    if (part.done) break;\n    if (!part.value?.byteLength) continue;\n    total += part.value.byteLength;\n    if (total > maxBytes) {\n      await reader.cancel();\n      throw new Error(\"The CCR media artifact exceeds the inline preview size limit.\");\n    }\n    chunks.push(Buffer.from(part.value));\n  }\n  if (!total) throw new Error(\"The CCR artifact response was empty.\");\n  if (declaredLength && total !== declaredLength) throw new Error(\"The CCR artifact response length did not match its headers.\");\n  const bytes = Buffer.concat(chunks, total);\n  const detectedMimeType = detectMediaMimeType(bytes);\n  if (!detectedMimeType || mediaKind(detectedMimeType) !== declaredKind) {\n    throw new Error(\"The CCR artifact content did not match its declared media type.\");\n  }\n  return { bytes, mimeType: detectedMimeType };\n}\n\nfunction detectMediaMimeType(buffer: Buffer): string | undefined {\n  if (buffer.byteLength >= 8 && buffer.subarray(0, 8).equals(Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]))) return \"image/png\";\n  if (buffer.byteLength >= 3 && buffer[0] === 0xff && buffer[1] === 0xd8 && buffer[2] === 0xff) return \"image/jpeg\";\n  if (buffer.byteLength >= 12 && buffer.subarray(0, 4).toString(\"ascii\") === \"RIFF\" && buffer.subarray(8, 12).toString(\"ascii\") === \"WEBP\") return \"image/webp\";\n  if (buffer.byteLength >= 12 && buffer.subarray(4, 8).toString(\"ascii\") === \"ftyp\") {\n    const brand = buffer.subarray(8, 12).toString(\"ascii\");\n    if ([\"avif\", \"avis\", \"mif1\", \"msf1\"].includes(brand)) return \"image/avif\";\n    return \"video/mp4\";\n  }\n  if (buffer.byteLength >= 4 && buffer.subarray(0, 4).equals(Buffer.from([0x1a, 0x45, 0xdf, 0xa3]))) return \"video/webm\";","sourceCodeStart":315,"sourceCodeEnd":351,"githubUrl":"https://github.com/musistudio/claude-code-router/blob/99f24806c6a2c660b16e53e95211c517448a6c90/packages/core/src/agents/codex/media-preview-bridge.ts#L315-L351","documentation":"The number of bytes actually streamed did not equal the declared Content-Length. This guards against truncated or padded responses and against misleading size metadata before the bytes are sniffed and returned.","triggerScenarios":"Connection dropped mid-transfer and the stream ended early; a proxy rewrote the body (e.g. injected content, or decompressed a response after Content-Length was computed for the compressed size); server sends an inaccurate Content-Length.","commonSituations":"Intermediaries modifying bodies (ad injection, TLS-terminating middleboxes), decompression proxies, flaky networks truncating responses, or servers with off-by-one/broken length computation.","solutions":["Compare curl's reported downloaded size against the response Content-Length to see which side is wrong","If a proxy is altering the body, bypass it or make it pass responses through verbatim","If truncation is network-related, retry the artifact fetch — transient truncation resolves on a stable connection"],"exampleFix":null,"handlingStrategy":"retry","validationCode":null,"typeGuard":"function isLengthMismatchRejection(e: unknown): boolean {\n  return e instanceof Error && e.message.includes('length did not match');\n}","tryCatchPattern":"try {\n  const media = await bridge.artifact(ref);\n} catch (e) {\n  if (isLengthMismatchRejection(e)) {\n    await sleep(500);\n    return bridge.artifact(ref); // truncated transfers are often transient\n  }\n  throw e;\n}","preventionTips":["Avoid intermediaries that rewrite or decompress response bodies","Retry once on length mismatches — truncation is frequently network-related","If mismatches persist for one URL, treat that artifact as corrupt server-side"],"tags":["content-length","integrity","validation","network"],"backgroundTag":"content-length-mismatch","analyzedSha":"99f24806c6a2c660b16e53e95211c517448a6c90","analyzedAt":"2026-08-27T04:11:01.184Z","schemaVersion":2},"datasetVersion":"2026-08-27T08:17:20.692Z"}