JuliusBrussee/caveman · error

recent stats was not a JSON array

Error message

recent stats was not a JSON array

What it means

`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.

Solutions

  1. Run the underlying command manually (`<bin> stats --recent <n> --json`) to see the raw non-JSON output and diagnose.
  2. Reinstall the companion binary with `caveman setup --install` to match the CLI's expected output shape.
  3. Check whether the proxy's stats store is corrupted and reset/reinitialize recent stats data.

Example fix

// before (shell)
my-proxy-bin stats --recent 20 --json
// prints: {"error":"store locked"}
// after (shell)
caveman setup --install
my-proxy-bin stats --recent 20 --json
// prints: [{"ts":"...","model":"..."}, ...]
Defensive patterns

Strategy: type-guard

Validate before calling

// js
const out = execFileSync(bin, ["stats", "--recent", "20", "--json"], { encoding: "utf8" });
const parsed = JSON.parse(out);
if (!Array.isArray(parsed)) throw new Error("proxy returned non-array recent stats; reinstall binary");

Type guard

function isRecentProxyRows(v: unknown): v is RecentProxyRow[] {
  return Array.isArray(v) && v.every((r) => r !== null && typeof r === "object");
}

Try / catch

// js
try {
  rows = readRecentProxyRows(bin, recent);
} catch (e) {
  if (String(e.message).includes("not a JSON array")) {
    warn("companion binary output shape mismatch; run caveman setup --install");
    rows = [];
  } else throw e;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of JuliusBrussee/caveman@3ee70a1026 (2026-09-20). Data as JSON: /api/errors/b6e90e6d30e508d1. Report an issue: GitHub.

Appendix: source

Thrown at packages/cli/src/index.ts:15550

  const seconds = Math.ceil(timeoutMs / 1000);
  console.error(`no request seen through the proxy in ${seconds}s`);
  console.error("hint: check base URL points at the Caveman gateway and run `caveman start`");
  process.exit(1);
}

function verifyTimeoutMs(values: string[]): number {
  const ms = Number(flagFrom(values, "--timeout-ms", ""));
  if (Number.isFinite(ms) && ms > 0) return ms;
  const seconds = Number(flagFrom(values, "--timeout", "60"));
  if (Number.isFinite(seconds) && seconds > 0) return Math.round(seconds * 1000);
  return 60_000;
}

function readRecentProxyRows(bin: string, recent: string): RecentProxyRow[] {
  try {
    const out = execFileSync(bin, ["stats", "--recent", recent, "--json"], { encoding: "utf8", env: process.env });
    const parsed = JSON.parse(out);
    if (!Array.isArray(parsed)) throw new Error("recent stats was not a JSON array");
    return parsed.map((row) => ({
      ts: String(row?.ts ?? ""),
      agent_slug: String(row?.agent_slug ?? ""),
      provider: String(row?.provider ?? ""),
      model: String(row?.model ?? ""),
      endpoint: String(row?.endpoint ?? ""),
      input_tokens: Number(row?.input_tokens ?? 0),
      output_tokens: Number(row?.output_tokens ?? 0),
      basis: String(row?.basis ?? "inferred"),
    }));
  } catch (error) {
    const e = error as { stdout?: string; stderr?: string; status?: number; message?: string };
    if (e.stdout) process.stdout.write(e.stdout);
    if (e.stderr) process.stderr.write(e.stderr);
    if (!e.stdout && !e.stderr) console.error(`failed to read recent proxy requests via ${bin}: ${e.message ?? error}`);
    process.exit(e.status ?? 1);
  }
}

View on GitHub (pinned to 3ee70a1026)