{"record":{"id":"c45f8499972d3c1f","repo":"JuliusBrussee/caveman","slug":"tool-search-response-contained-an-unknown-or-dupli","errorCode":null,"errorMessage":"tool search response contained an unknown or duplicate tool","messagePattern":"tool search response contained an unknown or duplicate tool","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/sdk/typescript/src/index.ts","lineNumber":1637,"sourceCode":"  const fullSchemaTokens = validCounts ? rawFull : 0;\n  const savedTokens = fullSchemaTokens - sentSchemaTokens;\n  const reductionPct = fullSchemaTokens === 0 ? 0 : Math.round((savedTokens / fullSchemaTokens) * 1000) / 10;\n\n  const catalogByName = new Map<string, CaveTool>();\n  for (const tool of catalog) {\n    if (typeof tool.name !== \"string\" || tool.name.trim() === \"\" || catalogByName.has(tool.name)) {\n      throw new Error(\"tool catalog names must be non-empty and unique\");\n    }\n    catalogByName.set(tool.name, tool);\n  }\n  if (!Array.isArray(data.tools)) throw new Error(\"tool search response tools must be an array\");\n  const returnedNames = new Set<string>();\n  const selectedTools = data.tools.map((wire: unknown) => {\n    const name = wire !== null && typeof wire === \"object\" && typeof (wire as Record<string, unknown>)[\"name\"] === \"string\"\n      ? (wire as Record<string, unknown>)[\"name\"] as string\n      : \"\";\n    const local = catalogByName.get(name);\n    if (!local || returnedNames.has(name)) throw new Error(\"tool search response contained an unknown or duplicate tool\");\n    returnedNames.add(name);\n    return local;\n  });\n\n  return {\n    sessionId: typeof data.session_id === \"string\" ? data.session_id : undefined,\n    tools: selectedTools,\n    sentSchemaTokens,\n    fullSchemaTokens,\n    deferredCount: strictNonNegativeInt(data.deferred_count) ?? 0,\n    method: data.method ?? \"unknown\",\n    tokenBasis: typeof data.token_basis === \"string\" && data.token_basis.trim() ? data.token_basis : \"unavailable\",\n    basis: \"inferred\",\n    savedTokens,\n    reductionPct\n  };\n}\n","sourceCodeStart":1619,"sourceCodeEnd":1655,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/packages/sdk/typescript/src/index.ts#L1619-L1655","documentation":"After mapping the returned tools back to the local catalog, any returned name that is not in the catalog or appears more than once throws this error. It guarantees the server only selected tools the client offered and never duplicates.","triggerScenarios":"Server returns a tool name absent from the request catalog, an empty/malformed name entry, or the same tool name twice in data.tools.","commonSituations":"Server/session state referencing tools from a previous catalog (stale toolSessionId); server bug or version drift; cached session returning older tool set.","solutions":["Drop the stale toolSessionId and start a fresh search session","Ensure the catalog passed to searchTools is identical across calls in a session","Check for SDK/server version mismatch and upgrade both","Log data.tools to identify the unknown/duplicate name and fix the catalog or report the server bug"],"exampleFix":"// before\nawait searchTools(cave, newCatalog, query, { toolSessionId: oldSessionId }); // session knows old tool names\n// after\nawait searchTools(cave, newCatalog, query); // fresh session matching this catalog","handlingStrategy":"try-catch","validationCode":"if (toolSessionId) {\n  // only reuse sessions whose catalog matches exactly\n  if (!sessions.get(toolSessionId)?.catalogEquals(catalog)) toolSessionId = undefined;\n}","typeGuard":"null","tryCatchPattern":"try {\n  return await searchTools(cave, catalog, query, { toolSessionId });\n} catch (e) {\n  if (/unknown or duplicate tool/.test(e.message)) {\n    // stale session: retry once without the session id\n    return await searchTools(cave, catalog, query);\n  }\n  throw e;\n}","preventionTips":["Start a new session whenever the catalog changes","Pass an identical catalog array throughout a session","Keep SDK and server versions in sync","Log returned tool names when this fires to spot server drift"],"tags":["api","response-validation","tool-search","session"],"backgroundTag":"unexpected-response-shape","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"}