{"record":{"id":"67711d96a6468d26","repo":"JuliusBrussee/caveman","slug":"cave-tool-search-result-limit","errorCode":"cave_tool_search_result_limit","errorMessage":"cave_tool_search_result_limit","messagePattern":"cave_tool_search_result_limit","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/agent/src/runtime.ts","lineNumber":3093,"sourceCode":"        } catch {\n          continue;\n        }\n        if (!isRecord(parsed) || typeof parsed.name !== \"string\" ||\n            typeof parsed.description !== \"string\" || !isRecord(parsed.input)) continue;\n        const haystack = `${parsed.name} ${parsed.description} ${JSON.stringify(parsed.input)}`.toLowerCase();\n        const score = terms.reduce((total, term) => total + (haystack.includes(term) ? 1 : 0), 0);\n        if (score > 0) hits.push({ score, value: parsed, handle });\n      }\n      hits.sort((first, second) => second.score - first.score ||\n        String(first.value.name).localeCompare(String(second.value.name)));\n      const selected = hits.slice(0, 4);\n      for (const hit of selected) {\n        for (const item of trace ?? []) {\n          if (item.recoveryHandle === hit.handle) item.recoveryUsed = true;\n        }\n      }\n      const text = JSON.stringify(selected.map((hit) => hit.value));\n      if (text.length > 8_192) throw new Error(\"cave_tool_search_result_limit\");\n      return {\n        content: [{ type: \"text\", text }],\n        details: { recovery: \"tool_schema_search\", hits: selected.length },\n      };\n    },\n  };\n}\n\nfunction validEngineTokenCount(value: unknown): number | undefined {\n  return Number.isSafeInteger(value) && Number(value) >= 0 ? Number(value) : undefined;\n}\n\nfunction localMemoryTools(\n  definition: NonNullable<AgentDefinition[\"memory\"]>,\n  agentId: string,\n  config: MemoryStoreConfig | undefined,\n): AgentTool<TSchema>[] {\n  // Defense in depth: memory() already rejects a non-local provenance/consent at","sourceCodeStart":3075,"sourceCodeEnd":3111,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/packages/agent/src/runtime.ts#L3075-L3111","documentation":"The `cave_search_tools` recovery tool serializes its top-scored (max 4) original tool definitions to JSON and refuses to return payloads over 8,192 characters, throwing `cave_tool_search_result_limit`. This bounds the context cost of the recovery path so a search meant to save context cannot blow the budget by returning huge tool schemas.","triggerScenarios":"Calling `cave_search_tools` when the combined JSON of the top 4 matching original tool definitions exceeds 8,192 characters — e.g. matching tools with very large `input` parameter schemas.","commonSituations":"Agents registered with many large MCP/tool schemas whose original definitions are huge; a broad query matching 4 giant tools; annotation compaction making the model search for tools whose definitions are individually large.","solutions":["Refine the `query` to a more specific term so fewer/smaller tool definitions match and the top 4 fit under the limit.","Split tool registration so no single tool's schema is enormous; move rarely-used parameters out of the JSON schema.","Wrap the call, catch the error, and retry with a narrower query instead of treating it as fatal.","If you control the tool set, shorten descriptions and schema titles/defaults that inflate the serialized definition."],"exampleFix":"// before\nconst r = await tools.cave_search_tools.execute(id, { query: \"file\" }); // matches 4 huge schemas > 8192 chars\n// after\nconst r = await tools.cave_search_tools.execute(id, { query: \"read file content\" }); // narrower match, smaller payload","handlingStrategy":"try-catch","validationCode":"null","typeGuard":"null","tryCatchPattern":"try {\n  return await tools.cave_search_tools.execute(id, { query });\n} catch (e) {\n  if ((e as Error).message === \"cave_tool_search_result_limit\") {\n    return await tools.cave_search_tools.execute(id, { query: narrower(query) });\n  }\n  throw e;\n}","preventionTips":["Use specific multi-word queries so fewer/smaller tool definitions match.","Keep tool JSON schemas lean — avoid embedding large enums, defaults, and examples.","Treat this error as 'retry with narrower query', not as a hard failure."],"tags":["tool-call","size-limit","payload-too-large"],"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"}