{"record":{"id":"0ad3e63237df2267","repo":"JuliusBrussee/caveman","slug":"cursor-has-unknown-key-s-unknown-sort-join","errorCode":null,"errorMessage":"cursor has unknown key(s): ${unknown.sort().join(\", \")}","messagePattern":"cursor has unknown key\\(s\\): (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/cli/src/agent-mcp.ts","lineNumber":531,"sourceCode":"  optionalEnum(args, \"date_field\", [\"occurred\", \"updated\"]);\n  optionalEnum(args, \"group_by\", [\"session\", \"workflow\", \"model\", \"member\"]);\n  const pageSize = args.page_size;\n  if (pageSize !== undefined && (!Number.isSafeInteger(pageSize) || (pageSize as number) < 1 || (pageSize as number) > 500)) {\n    throw new Error(\"page_size must be an integer from 1 to 500\");\n  }\n  const sort = args.sort;\n  if (sort !== undefined) {\n    if (!sort || typeof sort !== \"object\" || Array.isArray(sort)) throw new Error(\"sort must be an object\");\n    const unknown = Object.keys(sort).filter((key) => key !== \"by\" && key !== \"dir\");\n    if (unknown.length > 0) throw new Error(`sort has unknown key(s): ${unknown.sort().join(\", \")}`);\n    optionalEnum(sort, \"by\", [\"timestamp\", \"total_cost_usd\", \"latency_ms\", \"total_tokens\"]);\n    optionalEnum(sort, \"dir\", [\"asc\", \"desc\"]);\n  }\n  const cursor = args.cursor;\n  if (cursor !== undefined) {\n    if (!cursor || typeof cursor !== \"object\" || Array.isArray(cursor)) throw new Error(\"cursor must be an object\");\n    const unknown = Object.keys(cursor).filter((key) => key !== \"sort_value\" && key !== \"request_id\" && key !== \"group_key\");\n    if (unknown.length > 0) throw new Error(`cursor has unknown key(s): ${unknown.sort().join(\", \")}`);\n    if (typeof cursor.sort_value !== \"string\" || cursor.sort_value === \"\") throw new Error(\"cursor.sort_value is required\");\n    for (const key of [\"request_id\", \"group_key\"]) {\n      if (cursor[key] !== undefined && typeof cursor[key] !== \"string\") throw new Error(`cursor.${key} must be a string`);\n    }\n  }\n}\n\nasync function callTool(client: AgentMcpClient, name: string, rawArgs: unknown): Promise<ToolResult> {\n  const started = Date.now();\n  let outcome: \"ok\" | \"error\" = \"ok\";\n  try {\n    const args = objectArg(rawArgs);\n    const allowed = TOOL_ARGUMENT_KEYS[name];\n    const unknown = Object.keys(args).filter((key) => !allowed?.has(key));\n    if (unknown.length > 0) throw new Error(`unknown argument(s): ${unknown.sort().join(\", \")}`);\n    let value: JSONValue;\n    switch (name) {\n      case \"caveman_context\": {","sourceCodeStart":513,"sourceCodeEnd":549,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/packages/cli/src/agent-mcp.ts#L513-L549","documentation":"A `cursor` object supplied to trace_search may contain only `sort_value` (required string), and optionally `request_id` and `group_key` (strings). Any additional key triggers this error listing the disallowed keys sorted alphabetically.","triggerScenarios":"trace_search with {\"cursor\":{\"sort_value\":\"...\",\"offset\":40}}, extra fields like {\"page\":2} inside cursor, or stale cursors from a prior schema version carrying now-removed keys.","commonSituations":"Manually assembling cursors and adding convenience fields; older clients persisting cursor JSON with deprecated keys; copying the response object including envelope metadata (e.g. next_cursor itself) into the cursor field.","solutions":["Remove all keys from the cursor except sort_value, request_id, and group_key.","Echo back only the exact cursor object the API returned on the previous page.","Verify required cursor.sort_value is a non-empty string (see related error).","Regenerate the cursor from a fresh page response instead of hand-editing old ones."],"exampleFix":"// before\n{ \"cursor\": { \"sort_value\": \"2025-01-01T00:00:00Z\", \"offset\": 40 } }\n// after\n{ \"cursor\": { \"sort_value\": \"2025-01-01T00:00:00Z\" } }","handlingStrategy":"validation","validationCode":"const CURSOR_KEYS = new Set([\"sort_value\", \"request_id\", \"group_key\"]);\nif (args.cursor && Object.keys(args.cursor).some(k => !CURSOR_KEYS.has(k)))\n  throw new Error(\"cursor may only contain sort_value, request_id, and group_key\");","typeGuard":"function isValidCursor(c) {\n  return isPlainObject(c) && Object.keys(c).every(k => CURSOR_KEYS.has(k));\n}","tryCatchPattern":"try {\n  await traceSearch(args);\n} catch (e) {\n  if (e.message.startsWith(\"cursor has unknown key\")) {\n    const { sort_value, request_id, group_key } = args.cursor;\n    args.cursor = { sort_value, request_id, group_key };\n    return traceSearch(args);\n  }\n  throw e;\n}","preventionTips":["Store and replay the cursor object exactly as the API returned it.","Strip envelope/metadata fields before assigning to the cursor argument.","After API upgrades, refetch a fresh cursor rather than reusing persisted ones."],"tags":["validation","pagination","mcp","schema"],"backgroundTag":"schema-validation-failed","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"}