{"record":{"id":"9ab72f5d8e3af189","repo":"JuliusBrussee/caveman","slug":"local-learn-snapshot-has-no-valid-generated-at-at","errorCode":null,"errorMessage":"local learn snapshot has no valid generated_at at ${snapshotPath}","messagePattern":"local learn snapshot has no valid generated_at at (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/cli/src/index.ts","lineNumber":10430,"sourceCode":"// from the current learn snapshot. Titles, suggestions, evidence, file paths,\n// and payload text never leave the machine. Empty snapshots are sent so stale\n// rows for this user are removed server-side.\nasync function syncLocalPracticeFindings(cfg: Config): Promise<PracticeSyncOutcome> {\n  const snapshotPath = join(cavemanHome(), \"reports\", \"caveman-learn.json\");\n  let raw: Record<string, unknown>;\n  try {\n    raw = JSON.parse(readFileSync(snapshotPath, \"utf8\")) as Record<string, unknown>;\n  } catch (error) {\n    if ((error as NodeJS.ErrnoException).code === \"ENOENT\") return { kind: \"no_snapshot\" };\n    throw new Error(`local learn snapshot is unreadable at ${snapshotPath}`);\n  }\n\n  if (!Array.isArray(raw.sinks)) {\n    throw new Error(`local learn snapshot has no sinks array at ${snapshotPath}`);\n  }\n  const observed = typeof raw.generated_at === \"string\" ? new Date(raw.generated_at) : null;\n  if (!observed || Number.isNaN(observed.valueOf())) {\n    throw new Error(`local learn snapshot has no valid generated_at at ${snapshotPath}`);\n  }\n  const sessions = typeof raw.sessions_scanned === \"number\" && Number.isFinite(raw.sessions_scanned)\n    ? Math.max(0, Math.trunc(raw.sessions_scanned))\n    : 0;\n  const counter = (value: unknown) =>\n    typeof value === \"number\" && Number.isFinite(value) ? Math.max(0, Math.trunc(value)) : 0;\n\n  const findings = raw.sinks\n    .filter((sink): sink is Record<string, unknown> =>\n      !!sink && typeof sink === \"object\" && !Array.isArray(sink))\n    .filter((sink) => typeof sink.practice_id === \"string\" && sink.practice_id.length > 0)\n    .map((sink) => ({\n      sink_id: typeof sink.sink_id === \"string\" ? sink.sink_id : \"\",\n      practice_id: sink.practice_id as string,\n      basis: \"inferred\",\n      tokens_per_turn: counter(sink.tokens_per_turn),\n      tokens_per_day_rate: counter(sink.tokens_per_day_rate),\n      sessions_scanned: sessions,","sourceCodeStart":10412,"sourceCodeEnd":10448,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/packages/cli/src/index.ts#L10412-L10448","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["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\")","Regenerate the snapshot by re-running the local scan/learn command so a fresh valid snapshot is written","If the snapshot came from an old CLI version, upgrade the CLI and rebuild the snapshot rather than editing it","Validate the JSON parses and contains generated_at before feeding it to the CLI"],"exampleFix":"// before\n{\n  \"sinks\": [...],\n  \"sessions_scanned\": 12\n}\n// after\n{\n  \"sinks\": [...],\n  \"sessions_scanned\": 12,\n  \"generated_at\": \"2026-09-20T12:00:00.000Z\"\n}","handlingStrategy":"validation","validationCode":"const raw = JSON.parse(fs.readFileSync(snapshotPath, \"utf8\"));\nconst t = typeof raw.generated_at === \"string\" ? new Date(raw.generated_at) : null;\nif (!t || Number.isNaN(t.valueOf())) throw new Error(`snapshot ${snapshotPath} needs a valid generated_at`);","typeGuard":"function hasValidGeneratedAt(r: unknown): r is { generated_at: string } & Record<string, unknown> {\n  const o = r as { generated_at?: unknown };\n  return typeof o.generated_at === \"string\" && !Number.isNaN(new Date(o.generated_at).valueOf());\n}","tryCatchPattern":"try {\n  await localLearn(snapshotPath);\n} catch (e) {\n  if ((e as Error).message.includes(\"no valid generated_at\")) {\n    await regenerateSnapshot(); // rebuild the snapshot instead of failing\n  } else throw e;\n}","preventionTips":["Never hand-edit snapshot JSON; always regenerate via the CLI","Validate generated_at is ISO 8601 before sharing snapshots between machines","Keep snapshot writes atomic so partial files never persist","Pin CLI versions so snapshot schemas stay compatible"],"tags":["validation","snapshot","date"],"backgroundTag":"invalid-date-format","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"}