{"record":{"id":"e93067f0cdff2b79","repo":"JuliusBrussee/caveman","slug":"cursor-sort-value-is-required","errorCode":null,"errorMessage":"cursor.sort_value is required","messagePattern":"cursor\\.sort_value is required","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/cli/src/agent-mcp.ts","lineNumber":532,"sourceCode":"  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\": {\n        const [identity, projects, selectedProjectId] = await Promise.all([","sourceCodeStart":514,"sourceCodeEnd":550,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/766dce6b1394ebb56a3090748d5a0240a5aefb36/packages/cli/src/agent-mcp.ts#L514-L550","documentation":"Thrown by validateTraceSearch() in agent-mcp.ts when a cursor object is supplied for caveman_trace_search but its sort_value key is missing, not a string, or an empty string. sort_value is the only required cursor field — it anchors keyset pagination at the last row of the previous page.","triggerScenarios":"cursor: { request_id: \"req_9\" } (sort_value omitted), cursor: { sort_value: \"\" }, or a hand-built cursor where sort_value was taken from a null field of the last row. Note sort_value is compared as a string — numeric sort values must be stringified by whatever produced the cursor.","commonSituations":"An agent reconstructs a cursor manually from a row it inspected instead of using next_cursor; the previous page was the last page and next_cursor was null, which the caller wraps as an empty object; a serialization step drops empty-string values.","solutions":["Use the server-provided next_cursor object from the previous response instead of building one","Treat a null/absent next_cursor as 'no more pages' and stop paging","If constructing manually, always include a non-empty string sort_value"],"exampleFix":"// before\nif (resp.next_cursor !== null) args.cursor = {};\n\n// after\nif (resp.next_cursor !== null) args.cursor = resp.next_cursor;\nelse done = true;","handlingStrategy":"validation","validationCode":"const okCursor = (c: unknown) =>\n  c === undefined ||\n  (typeof c === \"object\" && c !== null && typeof (c as any).sort_value === \"string\" && (c as any).sort_value !== \"\");\nif (!okCursor(args.cursor)) throw new Error(\"cursor.sort_value missing — request the first page without a cursor\");","typeGuard":"const isKeysetCursor = (v: unknown): v is { sort_value: string; request_id?: string; group_key?: string } =>\n  typeof v === \"object\" && v !== null && typeof (v as any).sort_value === \"string\" && (v as any).sort_value !== \"\";","tryCatchPattern":"if (res.isError && res.content?.[0]?.text?.includes(\"cursor.sort_value is required\")) {\n  done = true; // treat as end-of-results instead of retrying\n}","preventionTips":["Only ever send the next_cursor object returned by the previous response","Treat a null next_cursor as 'no more pages'","Never hand-build cursors from row data"],"tags":["mcp","validation","pagination","traces","caveman"],"backgroundTag":"mcp-invalid-params","analyzedSha":"766dce6b1394ebb56a3090748d5a0240a5aefb36","analyzedAt":"2026-08-18T03:14:35.516Z","contentChangedAt":"2026-08-18T03:14:35.516Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}