{"record":{"id":"60d550718da825ad","repo":"JuliusBrussee/caveman","slug":"cave-tool-result-limit","errorCode":"cave_tool_result_limit","errorMessage":"cave_tool_result_limit","messagePattern":"cave_tool_result_limit","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/agent/src/runtime.ts","lineNumber":3747,"sourceCode":"      // The worker path is bounded by killing its process; the IN-PROCESS\n      // (host/fixture) path is a closure this process cannot preempt, so a\n      // closure that ignores `combined` would hang the run forever. Race it\n      // against a hard deadline so the run stops waiting at timeoutMs with\n      // cave_tool_timeout (the closure keeps running in the background — JS\n      // cannot preempt it — but it no longer blocks the run).\n      const value = sandboxExecute\n        ? await sandboxExecute(params, combined)\n        : await raceToolTimeout(\n          Promise.resolve(definition.execute(params, combined)),\n          definition.timeoutMs,\n        );\n      const text = typeof value === \"string\" ? value : JSON.stringify(value);\n      const raw = new TextEncoder().encode(text ?? \"null\");\n      const maxInlineBytes = definition.artifact === undefined\n        ? 32_768\n        : definition.artifact.maxInlineTokens * 4;\n      if (deferLockedToolResult) {\n        if (raw.byteLength > 16 * 1024 * 1024) throw new Error(\"cave_tool_result_limit\");\n        return {\n          content: [{ type: \"text\", text: text ?? \"null\" }],\n          details: { effect: definition.effect, resultPolicy: definition.result, lockedRoutePending: true },\n        };\n      }\n      if (definition.result === \"inline\") {\n        if (raw.byteLength > maxInlineBytes) throw new Error(\"cave_tool_result_inline_limit\");\n        return {\n          content: [{ type: \"text\", text: text ?? \"null\" }],\n          details: { effect: definition.effect, resultPolicy: definition.result },\n        };\n      }\n      const mustOffload = definition.result === \"page\" ||\n        definition.result === \"compress\" ||\n        definition.result === \"exact_ccr\" ||\n        raw.byteLength > maxInlineBytes;\n      if (!mustOffload) {\n        return {","sourceCodeStart":3729,"sourceCodeEnd":3765,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/packages/agent/src/runtime.ts#L3729-L3765","documentation":"In deferred/locked-route mode a tool result may be kept inline only if its UTF-8 encoding is at most 16 MiB; larger payloads throw this error instead of being silently passed. The library enforces an absolute ceiling on tool result bytes even in the deferred path where normal inline/artifact limits do not apply.","triggerScenarios":"A tool marked with lockedRoutePending/deferred results returns a string or JSON-serializable value whose TextEncoder byte length exceeds 16*1024*1024 at runtime.ts:3747.","commonSituations":"A tool dumps a huge file, log archive, or database dump as its result while the definition routes through the deferred lock path; accidental serialization of binary blobs into the result text.","solutions":["Make the tool return a handle/path/reference instead of embedding the full payload in the result text.","Truncate or paginate the tool's output before returning it.","Configure the tool definition with an artifact (maxInlineTokens) so large results go through the offload path rather than the deferred inline path.","If 16 MiB is genuinely needed, compress the payload before returning."],"exampleFix":"// before\nreturn { text: giantLogContents };\n// after\nreturn { text: `log archived at ${writeArtifact(giantLogContents)} (${giantLogContents.length} bytes)` };","handlingStrategy":"validation","validationCode":"const bytes = new TextEncoder().encode(JSON.stringify(result)).byteLength;\nif (bytes > 16 * 1024 * 1024) {\n  result = `payload too large (${bytes} bytes); see artifact at ${offload(result)}`;\n}","typeGuard":"function fitsDeferredLimit(value: unknown): boolean {\n  const text = typeof value === 'string' ? value : JSON.stringify(value);\n  return new TextEncoder().encode(text ?? 'null').byteLength <= 16 * 1024 * 1024;\n}","tryCatchPattern":null,"preventionTips":["Never return raw file dumps or DB dumps from tools; return references.","Truncate or summarize large outputs inside the tool itself.","Prefer the artifact/offload path for anything likely to exceed a few MB."],"tags":["limit-exceeded","tool-results","size-limit"],"backgroundTag":"payload-too-large","analyzedSha":"3ee70a102609e550bd2e68004bf5990a9341c851","analyzedAt":"2026-09-20T15:53:39.229Z","contentChangedAt":"2026-09-20T15:53:39.229Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}