{"record":{"id":"b6e90e6d30e508d1","repo":"JuliusBrussee/caveman","slug":"recent-stats-was-not-a-json-array","errorCode":null,"errorMessage":"recent stats was not a JSON array","messagePattern":"recent stats was not a JSON array","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/cli/src/index.ts","lineNumber":15550,"sourceCode":"  const seconds = Math.ceil(timeoutMs / 1000);\n  console.error(`no request seen through the proxy in ${seconds}s`);\n  console.error(\"hint: check base URL points at the Caveman gateway and run `caveman start`\");\n  process.exit(1);\n}\n\nfunction verifyTimeoutMs(values: string[]): number {\n  const ms = Number(flagFrom(values, \"--timeout-ms\", \"\"));\n  if (Number.isFinite(ms) && ms > 0) return ms;\n  const seconds = Number(flagFrom(values, \"--timeout\", \"60\"));\n  if (Number.isFinite(seconds) && seconds > 0) return Math.round(seconds * 1000);\n  return 60_000;\n}\n\nfunction readRecentProxyRows(bin: string, recent: string): RecentProxyRow[] {\n  try {\n    const out = execFileSync(bin, [\"stats\", \"--recent\", recent, \"--json\"], { encoding: \"utf8\", env: process.env });\n    const parsed = JSON.parse(out);\n    if (!Array.isArray(parsed)) throw new Error(\"recent stats was not a JSON array\");\n    return parsed.map((row) => ({\n      ts: String(row?.ts ?? \"\"),\n      agent_slug: String(row?.agent_slug ?? \"\"),\n      provider: String(row?.provider ?? \"\"),\n      model: String(row?.model ?? \"\"),\n      endpoint: String(row?.endpoint ?? \"\"),\n      input_tokens: Number(row?.input_tokens ?? 0),\n      output_tokens: Number(row?.output_tokens ?? 0),\n      basis: String(row?.basis ?? \"inferred\"),\n    }));\n  } catch (error) {\n    const e = error as { stdout?: string; stderr?: string; status?: number; message?: string };\n    if (e.stdout) process.stdout.write(e.stdout);\n    if (e.stderr) process.stderr.write(e.stderr);\n    if (!e.stdout && !e.stderr) console.error(`failed to read recent proxy requests via ${bin}: ${e.message ?? error}`);\n    process.exit(e.status ?? 1);\n  }\n}","sourceCodeStart":15532,"sourceCodeEnd":15568,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/packages/cli/src/index.ts#L15532-L15568","documentation":"`readRecentProxyRows` shells out to the companion binary with `stats --recent <n> --json` and JSON.parses the output, expecting an array of recent proxy rows. If the binary prints anything that is not a JSON array (e.g. an error message, non-JSON text, or a JSON object), the CLI throws this error.","triggerScenarios":"Calling the stats code path that reads recent proxy rows when the companion binary's `stats --recent --json` output parses to a non-array (string, object, null) or fails to parse as JSON at all.","commonSituations":"Stale or mismatched companion binary version emitting a different shape; binary crashing and printing an error string; corrupted proxy stats store causing the binary to emit an error object instead of rows.","solutions":["Run the underlying command manually (`<bin> stats --recent <n> --json`) to see the raw non-JSON output and diagnose.","Reinstall the companion binary with `caveman setup --install` to match the CLI's expected output shape.","Check whether the proxy's stats store is corrupted and reset/reinitialize recent stats data."],"exampleFix":"// before (shell)\nmy-proxy-bin stats --recent 20 --json\n// prints: {\"error\":\"store locked\"}\n// after (shell)\ncaveman setup --install\nmy-proxy-bin stats --recent 20 --json\n// prints: [{\"ts\":\"...\",\"model\":\"...\"}, ...]","handlingStrategy":"type-guard","validationCode":"// js\nconst out = execFileSync(bin, [\"stats\", \"--recent\", \"20\", \"--json\"], { encoding: \"utf8\" });\nconst parsed = JSON.parse(out);\nif (!Array.isArray(parsed)) throw new Error(\"proxy returned non-array recent stats; reinstall binary\");","typeGuard":"function isRecentProxyRows(v: unknown): v is RecentProxyRow[] {\n  return Array.isArray(v) && v.every((r) => r !== null && typeof r === \"object\");\n}","tryCatchPattern":"// js\ntry {\n  rows = readRecentProxyRows(bin, recent);\n} catch (e) {\n  if (String(e.message).includes(\"not a JSON array\")) {\n    warn(\"companion binary output shape mismatch; run caveman setup --install\");\n    rows = [];\n  } else throw e;\n}","preventionTips":["Keep the companion binary version in sync with the CLI.","Validate binary output shape before consuming it in scripts.","Capture raw binary stderr/stdout when debugging stats integrations."],"tags":["cli","json","stats","unexpected-shape"],"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"}