{"record":{"id":"f458515172a1edad","repo":"JuliusBrussee/caveman","slug":"tool-schema-transform-invalid-segmentid","errorCode":null,"errorMessage":"tool_schema_transform_invalid:${segmentID}","messagePattern":"tool_schema_transform_invalid:(.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/agent/src/runtime.ts","lineNumber":3804,"sourceCode":"              : \"Use cave_retrieve for omitted detail.\",\n          ].join(\"\\n\"),\n        }],\n        details: {\n          effect: definition.effect,\n          resultPolicy: definition.result,\n          recoveryHandle: transformed.handle,\n          recoveryVerified: true,\n        },\n      };\n    },\n  };\n}\n\nfunction validatedToolSchemaBytes(output: Uint8Array, segmentID: string): Uint8Array {\n  const parsed = JSON.parse(new TextDecoder().decode(output)) as unknown;\n  if (!isRecord(parsed) || typeof parsed.name !== \"string\" ||\n      typeof parsed.description !== \"string\" || !isRecord(parsed.input)) {\n    throw new Error(`tool_schema_transform_invalid:${segmentID}`);\n  }\n  return output;\n}\n\nfunction providerToolDefinition(\n  definition: ToolDefinition,\n  lowered: LoweredContext,\n  appliedPlan: AppliedPlan,\n): { description: string; input: TSchema } {\n  const segment = lowered.ir.segments.find((item) => item.id === `tool.${definition.name}`);\n  if (!segment) throw new Error(`cave_context_segment_missing:tool.${definition.name}`);\n  const body = appliedPlan.bodies.get(segment.bodyHandle);\n  if (!body) throw new Error(`cave_context_body_missing:tool.${definition.name}`);\n  const parsed = JSON.parse(new TextDecoder().decode(body)) as unknown;\n  if (!isRecord(parsed) || parsed.name !== definition.name ||\n      typeof parsed.description !== \"string\" || !isRecord(parsed.input)) {\n    throw new Error(`cave_tool_schema_invalid:${definition.name}`);\n  }","sourceCodeStart":3786,"sourceCodeEnd":3822,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/766dce6b1394ebb56a3090748d5a0240a5aefb36/packages/agent/src/runtime.ts#L3786-L3822","documentation":"validatedToolSchemaBytes checks that a lowered/transformed tool-schema segment still parses as { name: string, description: string, input: object }. When a schema transform mangles the JSON — dropping a field, changing a type, or emitting a non-object — the segment's id is attached and the transform output is rejected.","triggerScenarios":"A context transform rewriting the tool.<name> schema segment so the output loses name/description/input or makes name/description non-strings or input a non-object; a transform emitting concatenated JSON or prose instead of one object; the segmentID in the message identifies which tool.schema segment broke.","commonSituations":"Custom schema-compression transforms that summarize description into a non-string or replace the object with an array; renames that drop required fields; transforms applied too broadly across segment kinds.","solutions":["Fix the transform so its output keeps name (string), description (string), and input (object)","Exclude tool-schema segments from the transform (treat them as read-only)","Use the segmentID from the message to reproduce with exactly that segment and inspect the bytes it produced"],"exampleFix":"// before: transform replaces the schema with a summary object\nconst out = JSON.stringify({ summary: describe(schema) });\n\n// after: preserve the required shape\nconst out = JSON.stringify({ ...schema, description: shorten(schema.description) });","handlingStrategy":"validation","validationCode":"function isValidToolSchemaJson(bytes: Uint8Array): boolean {\n  try {\n    const parsed = JSON.parse(new TextDecoder().decode(bytes)) as unknown;\n    return isToolSchema(parsed);\n  } catch {\n    return false;\n  }\n}","typeGuard":"function isToolSchema(value: unknown): value is { name: string; description: string; input: Record<string, unknown> } {\n  return typeof value === \"object\" && value !== null && !Array.isArray(value) &&\n    typeof (value as { name?: unknown }).name === \"string\" &&\n    typeof (value as { description?: unknown }).description === \"string\" &&\n    typeof (value as { input?: unknown }).input === \"object\" &&\n    (value as { input?: unknown }).input !== null;\n}","tryCatchPattern":"try {\n  bytes = validatedToolSchemaBytes(output, segmentID);\n} catch (error) {\n  if (error instanceof Error && error.message.startsWith(\"tool_schema_transform_invalid:\")) {\n    return originalSchemaBytes; // bypass the bad transform, keep the pre-transform schema\n  }\n  throw error;\n}","preventionTips":["Unit-test every schema transform against the {name, description, input} contract","Mark tool-schema segments read-only in transform pipelines","Log the segmentID when validation fails so the offending transform is identifiable"],"tags":["schema","transforms","validation"],"backgroundTag":"schema-validation-failed","analyzedSha":"766dce6b1394ebb56a3090748d5a0240a5aefb36","analyzedAt":"2026-08-18T03:14:35.516Z","contentChangedAt":"2026-08-18T03:14:35.516Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}