""" backup_config.py -- Schema + loader for the backup mirror system. Defines what gets backed up, where snapshots go, how they're named, and retention policy. The schema is loaded from two sources (deep-merged): 1. src/config.json::backup — repo default (checked in) 2. ~/.claude/backup-config.json — user override (optional) User values take precedence. Missing keys fall back to dataclass defaults so the backup system works out-of-the-box even with zero configuration. ALWAYS_EXCLUDE is a hard-coded safety net: any name in this set is dropped from ``top_files`` regardless of what the config says. This is how we prevent ``.credentials.json`` or auth caches from ever ending up in a snapshot even if a user's config accidentally lists them. """ from __future__ import annotations import json import os from dataclasses import dataclass, field from pathlib import Path from typing import Any # Files we refuse to ever copy, even if the user lists them. These # contain live auth tokens or ephemeral machine state that would either # leak credentials or become stale the moment a snapshot is taken. ALWAYS_EXCLUDE: frozenset[str] = frozenset({ ".credentials.json", "mcp-needs-auth-cache.json", "stats-cache.json", "claude.json", ".claude.json", }) _ALLOWED_SCOPES: frozenset[str] = frozenset({"full", "incremental", "hybrid"}) # ── Schema ────────────────────────────────────────────────────────────────── @dataclass(frozen=True) class BackupTree: """A directory tree to mirror into each snapshot.""" src: str # relative to claude_home, e.g. "agents" dest: str # relative to snapshot root, e.g. "agents" @dataclass(frozen=True) class BackupRetention: """How many snapshots survive automatic pruning.""" keep_latest: int = 50 keep_daily: int = 14 @dataclass(frozen=True) class BackupConfig: """Full backup system configuration. Path fields store the raw string (``~`` and ``$VARS`` unexpanded) so the config round-trips through JSON without mutation. Use :meth:`snapshot_dir_resolved` to get a materialized ``Path``. """ snapshot_dir: str = "~/.claude/backups" # Two placeholders: ``{timestamp}`` required, ``{reason}`` optional. # The Phase 2 CLI will format reason from ``--reason