{"record":{"id":"594fcf08a977c93d","repo":"JuliusBrussee/caveman","slug":"sort-has-unknown-key-s-unknown-sort-join","errorCode":null,"errorMessage":"sort has unknown key(s): ${unknown.sort().join(\", \")}","messagePattern":"sort has unknown key\\(s\\): (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/cli/src/agent-mcp.ts","lineNumber":523,"sourceCode":"}\n\nfunction validateTraceSearch(args: JSONObject): void {\n  validateTraceFilters(args);\n  for (const key of [\"from\", \"to\"]) {\n    const value = args[key];\n    if (value !== undefined && typeof value !== \"string\") throw new Error(`${key} must be a string`);\n  }\n  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\";","sourceCodeStart":505,"sourceCodeEnd":541,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/packages/cli/src/agent-mcp.ts#L505-L541","documentation":"When `sort` is provided as an object, only the keys `by` and `dir` are allowed. validateTraceSearch collects any other keys and throws this error listing them sorted, mirroring the monitor-filter whitelist pattern.","triggerScenarios":"trace_search with {\"sort\":{\"by\":\"latency_ms\",\"field\":\"x\"}}, {\"sort\":{\"key\":\"timestamp\"}}, or typos like {\"sort\":{\"order\":\"asc\"}} instead of \"dir\".","commonSituations":"Renamed fields across API versions (order vs dir); extra options like nulls-handling copied from other sort DSLs; typo of `dir`/`by`; accidental spread of a larger sort config object.","solutions":["Keep only `by` and `dir` in the sort object; delete other keys.","Fix typos: `by` for the field, `dir` for the direction.","Use allowed `by` values: timestamp, total_cost_usd, latency_ms, total_tokens (see related enum error).","Explicitly construct the sort object rather than spreading a generic config."],"exampleFix":"// before\n{ \"sort\": { \"by\": \"latency_ms\", \"dir\": \"desc\", \"nulls\": \"last\" } }\n// after\n{ \"sort\": { \"by\": \"latency_ms\", \"dir\": \"desc\" } }","handlingStrategy":"validation","validationCode":"const SORT_KEYS = new Set([\"by\", \"dir\"]);\nif (args.sort && Object.keys(args.sort).some(k => !SORT_KEYS.has(k)))\n  throw new Error(\"sort may only contain by and dir\");","typeGuard":"function isValidSort(s) {\n  return isPlainObject(s) && Object.keys(s).every(k => k === \"by\" || k === \"dir\");\n}","tryCatchPattern":"try {\n  await traceSearch(args);\n} catch (e) {\n  if (e.message.startsWith(\"sort has unknown key\")) {\n    args.sort = { by: args.sort.by, dir: args.sort.dir };\n    return traceSearch(args);\n  }\n  throw e;\n}","preventionTips":["Construct sort objects explicitly; avoid spreading generic sort configs.","Keep field names in sync with API version updates (dir vs order).","Type the sort shape in client code so extra keys fail at compile time."],"tags":["validation","mcp","sorting","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"}