Expand description
Pre-migration snapshots — Increment B.2 lean cut from
docs/specs/SPEC_DATA_CHANNELS_2026_05_24.md §3.4.
When a launch detects an existing objects.db whose user_version is
behind the binary’s OBJECT_SCHEMA_VERSION, copy the SQLite DBs into
~/.agentmux/snapshots/<channel>-pre-v<code-version>-<ISO8601>.bak/
BEFORE any migration mutates them, then prune to the newest
MAX_SNAPSHOTS_PER_CHANNEL per channel.
Lean-cut scope decisions (vs. the full spec):
- SQLite files only — no copy of
agents/,config/,logs/, orcef-cache/. Most state worth recovering lives in the DBs; agent workspaces are typically git-managed; logs and cache are regenerable. Cuts snapshot size by ~10× vs. a full data-dir copy. VACUUM INTOinstead of file copy — produces an atomic, WAL-consistent single-file snapshot regardless of journal state. No need to coordinate.db-waland.db-shmfiles separately.- No restore CLI — manual restore is
cp snapshot/*.db <db_dir>/. Documented in the spec for now; a CLI can come later if used. - Snapshot failure is logged + non-fatal — the safety lock already prevents downgrade corruption, so a failed snapshot is a missing rollback aid, not a data-loss event. Refusing to boot would be worse.
Enums§
Constants§
- MAX_
SNAPSHOTS_ PER_ CHANNEL - Maximum number of snapshots retained per channel. Older ones are pruned after each new snapshot. Spec §3.4 budgets ~2 GB per channel at this retention level; the lean SQLite-only cut runs well under that.
- NAME_
PRE_ 🔒TOKEN - Token that separates
<channel>from the version segment in snapshot dir names:<channel>-pre-v<code_version>-<ts>.bak. Used by the prune step to extract the timestamp from the filename (more robust than mtime, which can be rewritten by backup tools). - NAME_
SUFFIX 🔒 - SNAPSHOT_
DB_ 🔒NAMES - DBs included in a snapshot. Lean-cut: SQLite files only.
Functions§
- create_
snapshot - Snapshot the SQLite DBs in
db_dirto a new directory undersnapshots_dir. The snapshot dir is named<channel>-pre-v<code_version>-<ISO8601>.bak. UsesVACUUM INTOfor atomic, WAL-consistent copies. Returns the new snapshot dir. - maybe_
snapshot_ pre_ migration - High-level entry: check, snapshot, prune. Returns the snapshot dir on
success, or
Noneif no snapshot was needed. Errors are surfaced to the caller; production call sites should log-and-continue rather than abort boot (see module docs). - needs_
snapshot - Inspect
objects.dbto decide whether a pre-migration snapshot is warranted. ReturnsSome(found_version)if an existing DB is behindcurrent;Nonefor fresh installs (no file) or same-version opens. A newer-than-current DB is left alone — the safety lock inwstore.rswill refuse to open it before any migration runs. - parse_
ts_ 🔒from_ name - Extract the timestamp segment from a snapshot dir name.
Format:
<channel>-pre-v<code_version>-<ISO8601>.bak. We already know the name starts with<channel>-pre-v(the caller checked); find the last-before.bakto split off the timestamp from<code_version>. - prune_
snapshots - Delete oldest snapshots for
channeluntil at mostMAX_SNAPSHOTS_PER_CHANNELremain. Returns the number pruned. Pruning errors on a specific entry are logged and skipped rather than aborting the whole sweep (one bad dir shouldn’t keep storage growing). - quote_
sql_ 🔒literal - snapshot_
dir_ 🔒name - validate_
channel 🔒 - Reject channel names that would break the filename layout or escape the
snapshots dir. Mirrors the reserved-name set in
agentmux-common’ssanitize_channel_name, but operates on the already-resolved channel string this module receives via env.