JuliusBrussee/caveman · error · Error

local learn snapshot has no valid generated_at at

Error message

local learn snapshot has no valid generated_at at ${snapshotPath}

What it means

The CLI's local learn feature reads a snapshot JSON file from disk and validates its shape before using it. `generated_at` must be a string parseable as a valid Date; the throw means the field is missing, not a string, or an unparseable/invalid date string. The CLI refuses to process snapshots whose generation timestamp cannot be established, since learn output is timestamped against it.

Solutions

  1. Open the snapshot file and add or repair the top-level `generated_at` field with an ISO 8601 string (e.g. "2026-09-20T12:00:00.000Z")
  2. Regenerate the snapshot by re-running the local scan/learn command so a fresh valid snapshot is written
  3. If the snapshot came from an old CLI version, upgrade the CLI and rebuild the snapshot rather than editing it
  4. Validate the JSON parses and contains generated_at before feeding it to the CLI

Example fix

// before
{
  "sinks": [...],
  "sessions_scanned": 12
}
// after
{
  "sinks": [...],
  "sessions_scanned": 12,
  "generated_at": "2026-09-20T12:00:00.000Z"
}
Defensive patterns

Strategy: validation

Validate before calling

const raw = JSON.parse(fs.readFileSync(snapshotPath, "utf8"));
const t = typeof raw.generated_at === "string" ? new Date(raw.generated_at) : null;
if (!t || Number.isNaN(t.valueOf())) throw new Error(`snapshot ${snapshotPath} needs a valid generated_at`);

Type guard

function hasValidGeneratedAt(r: unknown): r is { generated_at: string } & Record<string, unknown> {
  const o = r as { generated_at?: unknown };
  return typeof o.generated_at === "string" && !Number.isNaN(new Date(o.generated_at).valueOf());
}

Try / catch

try {
  await localLearn(snapshotPath);
} catch (e) {
  if ((e as Error).message.includes("no valid generated_at")) {
    await regenerateSnapshot(); // rebuild the snapshot instead of failing
  } else throw e;
}

Prevention

When it happens

Trigger: Calling the local learn flow whose snapshot file at snapshotPath has: no `generated_at` key, a non-string `generated_at` (number/null), or a string that `new Date()` fails to parse (NaN), e.g. hand-edited or truncated snapshot JSON.

Common situations: Snapshots hand-edited or partially copied, older snapshot formats from previous CLI versions lacking the field, files corrupted by interrupted writes, or JSON produced by custom scripts that omit the timestamp.

Related errors


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

Appendix: source

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

// from the current learn snapshot. Titles, suggestions, evidence, file paths,
// and payload text never leave the machine. Empty snapshots are sent so stale
// rows for this user are removed server-side.
async function syncLocalPracticeFindings(cfg: Config): Promise<PracticeSyncOutcome> {
  const snapshotPath = join(cavemanHome(), "reports", "caveman-learn.json");
  let raw: Record<string, unknown>;
  try {
    raw = JSON.parse(readFileSync(snapshotPath, "utf8")) as Record<string, unknown>;
  } catch (error) {
    if ((error as NodeJS.ErrnoException).code === "ENOENT") return { kind: "no_snapshot" };
    throw new Error(`local learn snapshot is unreadable at ${snapshotPath}`);
  }

  if (!Array.isArray(raw.sinks)) {
    throw new Error(`local learn snapshot has no sinks array at ${snapshotPath}`);
  }
  const observed = typeof raw.generated_at === "string" ? new Date(raw.generated_at) : null;
  if (!observed || Number.isNaN(observed.valueOf())) {
    throw new Error(`local learn snapshot has no valid generated_at at ${snapshotPath}`);
  }
  const sessions = typeof raw.sessions_scanned === "number" && Number.isFinite(raw.sessions_scanned)
    ? Math.max(0, Math.trunc(raw.sessions_scanned))
    : 0;
  const counter = (value: unknown) =>
    typeof value === "number" && Number.isFinite(value) ? Math.max(0, Math.trunc(value)) : 0;

  const findings = raw.sinks
    .filter((sink): sink is Record<string, unknown> =>
      !!sink && typeof sink === "object" && !Array.isArray(sink))
    .filter((sink) => typeof sink.practice_id === "string" && sink.practice_id.length > 0)
    .map((sink) => ({
      sink_id: typeof sink.sink_id === "string" ? sink.sink_id : "",
      practice_id: sink.practice_id as string,
      basis: "inferred",
      tokens_per_turn: counter(sink.tokens_per_turn),
      tokens_per_day_rate: counter(sink.tokens_per_day_rate),
      sessions_scanned: sessions,

View on GitHub (pinned to 3ee70a1026)