{"record":{"id":"e1271753ec91a1ec","repo":"mastra-ai/mastra","slug":"buildhdrl-internal-error-wrote-p-bytes-expect","errorCode":null,"errorMessage":"buildHdrl internal error: wrote ${p} bytes, expected ${hdrlPayloadSize}","messagePattern":"buildHdrl internal error: wrote (.+?) bytes, expected (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/core/src/browser/recording/mjpeg-avi.ts","lineNumber":338,"sourceCode":"  p += 2;\n  // biCompression = \"MJPG\"\n  FOURCC_MJPG.copy(buf, p);\n  p += 4;\n  // biSizeImage\n  buf.writeUInt32LE(sizeImage, p);\n  p += 4;\n  // biXPelsPerMeter, biYPelsPerMeter, biClrUsed, biClrImportant\n  buf.writeInt32LE(0, p);\n  p += 4;\n  buf.writeInt32LE(0, p);\n  p += 4;\n  buf.writeUInt32LE(0, p);\n  p += 4;\n  buf.writeUInt32LE(0, p);\n  p += 4;\n\n  if (p !== hdrlPayloadSize) {\n    throw new Error(`buildHdrl internal error: wrote ${p} bytes, expected ${hdrlPayloadSize}`);\n  }\n  return buf;\n}\n\nfunction paddedLength(n: number): number {\n  return n + (n & 1);\n}\n\nfunction assertJpegFrame(bytes: Uint8Array, index: number): void {\n  if (bytes.length < 2 || bytes[0] !== 0xff || bytes[1] !== 0xd8) {\n    throw new Error(`writeMjpegAviFile: frame ${index} is not a JPEG frame (missing SOI marker)`);\n  }\n}\n\nfunction assertU32(value: number, field: string): void {\n  if (!Number.isSafeInteger(value) || value < 0 || value > MAX_U32) {\n    throw new Error(`writeMjpegAviFile: ${field} exceeds 32-bit AVI limit (${value})`);\n  }","sourceCodeStart":320,"sourceCodeEnd":356,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/core/src/browser/recording/mjpeg-avi.ts#L320-L356","documentation":"buildHdrl writes the AVI main + stream header ('hdrl') list and verifies the byte cursor matches the precomputed payload size. A mismatch is a pure internal bug in the writer's size accounting, not user input — it means the header builder and its size math diverged.","triggerScenarios":"Only reachable when the module's own hdrl layout code and hdrlPayloadSize constant disagree — typically after someone edited buildHdrl (added/removed fields) without updating hdrlPayloadSize.","commonSituations":"Encountered by contributors modifying the AVI muxer; end users hit it only as fallout of an untested fork/patch or corrupted build of mjpeg-avi.ts.","solutions":["File a bug against the mjpeg-avi muxer — this is an internal invariant violation","If you modified buildHdrl, recompute hdrlPayloadSize to match every writeUInt* call in the builder","Add/refresh unit tests that mux a minimal recording so the invariant is exercised in CI"],"exampleFix":"// before (added an extra field but kept the old size)\nbuf.writeUInt32LE(flags, p); p += 4; // hdrlPayloadSize not updated\n// after\nhdrlPayloadSize += 4; // keep size constant in sync with writes\n","handlingStrategy":"try-catch","validationCode":"null","typeGuard":null,"tryCatchPattern":"try {\n  const buf = buildHdrl(...);\n} catch (e) {\n  if (e.message.startsWith('buildHdrl internal error')) {\n    throw new Error('AVI muxer bug: header size accounting mismatch — report to library maintainers');\n  }\n  throw e;\n}","preventionTips":["Treat this as a library bug, not user error","Keep hdrlPayloadSize in sync when editing buildHdrl","Run muxer unit tests after any header-layout change"],"tags":["avi","internal-bug","header","invariant"],"backgroundTag":"internal-invariant-violation","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T08:17:16.595Z"}