{"record":{"id":"6bf93a9af9171e3f","repo":"nexu-io/open-design","slug":"file-relpath-size-bytes-exceeds-remaining","errorCode":null,"errorMessage":"file ${relPath} (${size} bytes) exceeds remaining budget","messagePattern":"file (.+?) \\((.+?) bytes\\) exceeds remaining budget","errorType":"exception","errorClass":"BudgetExceededError","httpStatus":null,"severity":"warning","filePath":"apps/daemon/src/mcp.ts","lineNumber":3270,"sourceCode":"    .split('/')\n    .filter((s) => s.length > 0)\n    .map(encodeURIComponent);\n  const url = `${baseUrl}/api/projects/${encodeURIComponent(projectId)}/raw/${segments.join('/')}`;\n  const resp = await fetch(url, headers ? { headers } : undefined);\n  if (!resp.ok) {\n    const body = await safeText(resp);\n    throw new Error(`daemon ${resp.status} on ${url}: ${body || resp.statusText}`);\n  }\n  const mime = ((resp.headers.get('content-type') || 'application/octet-stream').split(';')[0] ?? 'application/octet-stream').trim();\n  const headerSize = Number(resp.headers.get('content-length'));\n  const size = Number.isFinite(headerSize) && headerSize >= 0 ? headerSize : null;\n  if (!isTextualMime(mime)) {\n    return { name: relPath, mime, size, content: null, binary: true };\n  }\n  // If the server advertises a size that already exceeds our remaining\n  // budget, skip reading the body to avoid a large allocation.\n  if (size !== null && size > remainingBytes) {\n    throw new BudgetExceededError(`file ${relPath} (${size} bytes) exceeds remaining budget`);\n  }\n  const content = await resp.text();\n  const contentBytes = Buffer.byteLength(content, 'utf8');\n  if (contentBytes > remainingBytes) {\n    throw new BudgetExceededError(\n      `file ${relPath} (${contentBytes} bytes) exceeds remaining budget`,\n    );\n  }\n  return { name: relPath, mime, size: size ?? contentBytes, content, binary: false };\n}\n\n// Patterns common to HTML and CSS (also fine to run on plain markdown).\nconst HTML_REF_PATTERNS = [\n  /<script\\b[^>]*\\bsrc=[\"']([^\"']+)[\"']/gi,\n  /<link\\b[^>]*\\bhref=[\"']([^\"']+)[\"']/gi,\n  /<img\\b[^>]*\\bsrc=[\"']([^\"']+)[\"']/gi,\n  /<source\\b[^>]*\\bsrc=[\"']([^\"']+)[\"']/gi,\n  /<video\\b[^>]*\\bsrc=[\"']([^\"']+)[\"']/gi,","sourceCodeStart":3252,"sourceCodeEnd":3288,"githubUrl":"https://github.com/nexu-io/open-design/blob/5be4028344c2eb4c667c5a97bda8f750c5597ef7/apps/daemon/src/mcp.ts#L3252-L3288","documentation":"Thrown as a BudgetExceededError by the raw-file fetcher: the server's Content-Length header already exceeds the caller's remaining byte budget, so the body is never read. This optimization prevents a large allocation when the advertised size alone would overflow the MCP context bundle budget.","triggerScenarios":"Bundling project files into an MCP response with a tight maxBytes; fetching a very large source/log/data file when remainingBytes is nearly exhausted; a binary file mislabeled with a textual MIME and a huge Content-Length.","commonSituations":"Agent reads many files in sequence and the cumulative budget runs out; a generated/lockfile/minified file is huge; budget set too low for the repo's largest files.","solutions":["Skip or truncate large files before fetching (filter by size/name in your listing step).","Increase the maxBytes/remainingBytes budget passed to the fetcher if the caller controls it.","Read the file in offset/limit chunks using the ranged file API instead of whole-file fetch.","Exclude known large paths (dist/, node_modules/, lockfiles, minified bundles) from the bundle."],"exampleFix":"// before\nconst bundle = await getFile(baseUrl, project, \"dist/build.js\", active, resolved, 0, 2000, remainingBytes);\n// after: skip oversized generated files\nif (entry.size > remainingBytes) continue;","handlingStrategy":"validation","validationCode":"if (typeof size === 'number' && size > remainingBytes) {\n  throw new BudgetExceededError(`${relPath} too large; skip or raise budget`);\n}","typeGuard":null,"tryCatchPattern":"try {\n  return await fetchRawFile(baseUrl, projectId, relPath, remainingBytes, headers);\n} catch (e) {\n  if (e instanceof BudgetExceededError) return null; // skip oversized file\n  throw e;\n}","preventionTips":["Pre-filter listing entries by size against the remaining budget.","Read large files in offset/limit pages.","Exclude dist/, lockfiles, and minified bundles from bundles."],"tags":["mcp","files","budget","performance"],"backgroundTag":null,"analyzedSha":"5be4028344c2eb4c667c5a97bda8f750c5597ef7","analyzedAt":"2026-08-12T12:03:58.812Z","schemaVersion":2},"datasetVersion":"2026-08-12T18:17:37.767Z"}