| """ |
| 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 |
|
|
|
|
| |
| |
| |
| 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"}) |
|
|
|
|
| |
|
|
|
|
| @dataclass(frozen=True) |
| class BackupTree: |
| """A directory tree to mirror into each snapshot.""" |
|
|
| src: str |
| dest: str |
|
|
|
|
| @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" |
| |
| |
| name_format: str = "{timestamp}" |
| timestamp_format: str = "%Y%m%dT%H%M%SZ" |
| |
| |
| |
| |
| |
| scope: str = "full" |
| max_file_bytes: int = 5 * 1024 * 1024 |
| top_files: tuple[str, ...] = ( |
| "settings.json", |
| "skill-manifest.json", |
| "pending-skills.json", |
| "CLAUDE.md", |
| "AGENTS.md", |
| "user-profile.json", |
| "skill-system-config.json", |
| "skill-registry.json", |
| ) |
| trees: tuple[BackupTree, ...] = ( |
| BackupTree(src="agents", dest="agents"), |
| BackupTree(src="skills", dest="skills"), |
| ) |
| |
| |
| |
| memory_glob: bool = True |
| excludes: tuple[str, ...] = () |
| retention: BackupRetention = field(default_factory=BackupRetention) |
|
|
| def __post_init__(self) -> None: |
| if self.scope not in _ALLOWED_SCOPES: |
| raise ValueError( |
| f"scope must be one of {sorted(_ALLOWED_SCOPES)}, " |
| f"got {self.scope!r}" |
| ) |
| if self.max_file_bytes < 0: |
| raise ValueError( |
| f"max_file_bytes must be >= 0, got {self.max_file_bytes}" |
| ) |
| if self.retention.keep_latest < 0: |
| raise ValueError( |
| f"retention.keep_latest must be >= 0, " |
| f"got {self.retention.keep_latest}" |
| ) |
| if self.retention.keep_daily < 0: |
| raise ValueError( |
| f"retention.keep_daily must be >= 0, " |
| f"got {self.retention.keep_daily}" |
| ) |
| if "{timestamp}" not in self.name_format: |
| raise ValueError( |
| f"name_format must contain '{{timestamp}}', " |
| f"got {self.name_format!r}" |
| ) |
| |
| |
| filtered_top = tuple( |
| name for name in self.top_files |
| if Path(name).name not in ALWAYS_EXCLUDE |
| ) |
| if filtered_top != self.top_files: |
| object.__setattr__(self, "top_files", filtered_top) |
|
|
| |
|
|
| def snapshot_dir_resolved(self) -> Path: |
| """Return snapshot_dir with ``~`` and ``$VARS`` expanded.""" |
| return Path(os.path.expanduser(os.path.expandvars(self.snapshot_dir))) |
|
|
| def is_excluded(self, rel_path: str) -> bool: |
| """True if ``rel_path`` is in excludes or ALWAYS_EXCLUDE.""" |
| name = Path(rel_path).name |
| if name in ALWAYS_EXCLUDE: |
| return True |
| for pattern in self.excludes: |
| if pattern == rel_path or Path(pattern).name == name: |
| return True |
| return False |
|
|
|
|
| |
|
|
|
|
| def _validate_tree_path(value: str, *, field: str) -> str: |
| """Reject tree src/dest values that would escape their trust boundary. |
| |
| Strix finding vuln-0002 (Backup Configuration Path Traversal): the |
| user-owned ``~/.claude/backup-config.json`` could supply |
| ``trees[].src = "../../etc"`` (reads outside ~/.claude) or |
| ``trees[].dest = "../../tmp/evil"`` (writes outside the snapshot |
| root). We reject absolute paths and any path containing ``..`` at |
| any segment, and forbid drive letters / UNC prefixes on Windows. |
| """ |
| if not value: |
| raise ValueError(f"{field} must be a non-empty string") |
| parts = Path(value).parts |
| if ".." in parts: |
| raise ValueError(f"{field}: {value!r} contains '..' — path traversal denied") |
| if value.startswith(("/", "\\")): |
| raise ValueError(f"{field}: {value!r} is absolute — only repo-relative paths allowed") |
| |
| if len(value) >= 2 and value[1] == ":": |
| raise ValueError(f"{field}: {value!r} contains a drive letter — rejected") |
| if value.startswith("\\\\"): |
| raise ValueError(f"{field}: {value!r} is a UNC path — rejected") |
| return value |
|
|
|
|
| def _coerce_trees(raw: Any, default: tuple[BackupTree, ...]) -> tuple[BackupTree, ...]: |
| if raw is None: |
| return default |
| if not isinstance(raw, list): |
| return default |
| out: list[BackupTree] = [] |
| for entry in raw: |
| if not isinstance(entry, dict): |
| continue |
| src = str(entry.get("src") or "").strip() |
| if not src: |
| continue |
| dest = str(entry.get("dest") or src).strip() |
| |
| |
| try: |
| _validate_tree_path(src, field="trees[].src") |
| _validate_tree_path(dest, field="trees[].dest") |
| except ValueError as exc: |
| |
| |
| |
| import sys as _sys |
| print(f"[backup-config] ignoring tree entry: {exc}", file=_sys.stderr) |
| continue |
| out.append(BackupTree(src=src, dest=dest)) |
| return tuple(out) |
|
|
|
|
| def _coerce_retention(raw: Any) -> BackupRetention: |
| if not isinstance(raw, dict): |
| return BackupRetention() |
| return BackupRetention( |
| keep_latest=int(raw.get("keep_latest", 50)), |
| keep_daily=int(raw.get("keep_daily", 14)), |
| ) |
|
|
|
|
| def load_backup_config(raw: dict[str, Any] | None = None) -> BackupConfig: |
| """Build a BackupConfig from a raw dict, applying defaults. |
| |
| Missing keys fall back to dataclass defaults. Invalid values raise |
| :class:`ValueError` via the dataclass ``__post_init__`` validator. |
| Pass ``None`` or ``{}`` to get the full default config. |
| """ |
| if not raw: |
| return BackupConfig() |
|
|
| defaults = BackupConfig() |
|
|
| top_raw = raw.get("top_files") |
| if top_raw is None: |
| top_files: tuple[str, ...] = defaults.top_files |
| else: |
| top_files = tuple(str(x) for x in top_raw) |
|
|
| return BackupConfig( |
| snapshot_dir=str(raw.get("snapshot_dir", defaults.snapshot_dir)), |
| name_format=str(raw.get("name_format", defaults.name_format)), |
| timestamp_format=str( |
| raw.get("timestamp_format", defaults.timestamp_format) |
| ), |
| scope=str(raw.get("scope", defaults.scope)), |
| max_file_bytes=int(raw.get("max_file_bytes", defaults.max_file_bytes)), |
| top_files=top_files, |
| trees=_coerce_trees(raw.get("trees"), defaults.trees), |
| memory_glob=bool(raw.get("memory_glob", defaults.memory_glob)), |
| excludes=tuple(str(x) for x in raw.get("excludes", ())), |
| retention=_coerce_retention(raw.get("retention")), |
| ) |
|
|
|
|
| def _deep_merge(base: dict[str, Any], override: dict[str, Any]) -> None: |
| """Merge override into base in-place, recursive for nested dicts.""" |
| for key, value in override.items(): |
| if ( |
| key in base |
| and isinstance(base[key], dict) |
| and isinstance(value, dict) |
| ): |
| _deep_merge(base[key], value) |
| else: |
| base[key] = value |
|
|
|
|
| def from_ctx_config() -> BackupConfig: |
| """Assemble the effective BackupConfig from ctx_config + user overlay. |
| |
| Order of precedence (later overrides earlier): |
| 1. dataclass defaults |
| 2. ``src/config.json::backup`` (via ``ctx_config.cfg``) |
| 3. ``~/.claude/backup-config.json`` (user override, optional) |
| |
| Never raises: a missing or malformed user file is silently ignored |
| so a backup is always possible even when the user's JSON is broken. |
| """ |
| try: |
| from ctx_config import cfg |
| base_raw = cfg.get("backup") or {} |
| except Exception: |
| base_raw = {} |
|
|
| merged: dict[str, Any] = dict(base_raw) if isinstance(base_raw, dict) else {} |
|
|
| user_override = Path(os.path.expanduser("~/.claude/backup-config.json")) |
| if user_override.is_file(): |
| try: |
| user_raw = json.loads(user_override.read_text(encoding="utf-8")) |
| if isinstance(user_raw, dict): |
| _deep_merge(merged, user_raw) |
| except (OSError, json.JSONDecodeError): |
| pass |
|
|
| return load_backup_config(merged if merged else None) |
|
|