Make your computer a home for your AI partner. Avibe is the local-first Agent OS: Vibey, its built-in AI partner, works out of the box, and Claude Code, Codex & OpenCode run unchanged beside it — reach them from your browser, phone, or any chat app.
Python agentagent-osaiai-agentsai-partnerchatopsclaudeclaude-codecodexdevtoolsdiscord-botlark-botllmlocal-firstlong-term-memoryopencodeslack-botvibe-codingvibeywechat
fix(storage): let the migration in progress own the window, not a protect flag
cyh1df8a3b4f9c9
diff --git a/storage/backups.py b/storage/backups.pyindex 80720ece7..8c1e59f8e 100644--- a/storage/backups.py+++ b/storage/backups.py@@ -1,5 +1,6 @@from __future__ import annotations+import errnoimport jsonimport loggingimport os@@ -29,6 +30,18 @@ _LEGACY_SQLITE_REPAIR_RE = re.compile(r"(?P<timestamp>\d{8}T\d{6}Z)\.sqlite$")+# The schema move a backup was taken for: (from_revisions, to_revisions).+_Transition = tuple[tuple[str, ...], tuple[str, ...]]++# A platform or filesystem declining to sync a directory is an answer, not a+# fault. Anything else -- ENOSPC, EIO -- means the data may not be on the disk,+# so it must reach the caller instead of being logged and forgotten.+_UNSUPPORTED_SYNC_ERRNOS = frozenset(+ getattr(errno, name)+ for name in ("EACCES", "EPERM", "EINVAL", "EISDIR", "ENOSYS", "ENOTSUP", "EOPNOTSUPP")+ if hasattr(errno, name)+)+@dataclass(frozen=True)class _BackupCandidate:@@ -188,7 +201,11 @@ def _remove_candidate(candidate: _BackupCandidate) -> bool:return True-def _keep_priority(candidates: Iterable[_BackupCandidate]) -> list[_BackupCandidate]:+def _keep_priority(+ candidates: Iterable[_BackupCandidate],+ *,+ active: _Transition | None = None,+) -> list[_BackupCandidate]:"""Order backups by how much a rollback would want them, best first.Retries of one unfinished migration are attempts at a single rollback point@@ -200,6 +217,12 @@ def _keep_priority(candidates: Iterable[_BackupCandidate]) -> list[_BackupCandidnewest-first is the wrong order here: it fills the window with the newesttwo attempts and discards the only clean one.+ `active` is the migration being attempted right now, and its ends outrank+ everything else because being newest on disk is not the same as being+ current. A clock corrected backwards, or state carried from a machine that+ ran ahead, leaves unrelated copies dated later; spending the window on those+ would evict the snapshot taken before the upgrade that is failing.+Backups that stand alone are their own group, so for a healthy machine --where each backup marks a different completed migration -- this stays thefamiliar newest-first.@@ -209,9 +232,13 @@ def _keep_priority(candidates: Iterable[_BackupCandidate]) -> list[_BackupCandidfor candidate in candidates:groups.setdefault(candidate.group_key, []).append(candidate)+ def rank(group: list[_BackupCandidate]) -> tuple[bool, tuple[datetime, int, str]]:+ is_active = active is not None and group[0].transition == active+ return is_active, max(entry.order_key for entry in group)+ranked: list[_BackupCandidate] = []superseded: list[_BackupCandidate] = []- for group in sorted(groups.values(), key=lambda item: max(entry.order_key for entry in item), reverse=True):+ for group in sorted(groups.values(), key=rank, reverse=True):members = sorted(group, key=lambda item: item.order_key)ranked.append(members[-1])if len(members) > 1:@@ -225,19 +252,16 @@ def prune_state_backups(*,json_retention: int | None = JSON_STATE_BACKUP_RETENTION,sqlite_retention: int | None = SQLITE_BACKUP_RETENTION,- protect: Path | None = None,+ active: _Transition | None = None,) -> list[Path]:"""Keep a bounded rollback window of backups created by Avibe.Unknown files, symlinks, incomplete backups, and directories without arecognized manifest are intentionally left untouched.- `protect` names a backup that must survive this call. It takes the first- slot of its kind rather than an extra one, so the window stays bounded; it- exists because timestamp order is not proof of being newest. A clock- corrected backwards, or state moved from a machine whose clock ran ahead,- leaves future-dated copies that would otherwise rank above a backup taken- seconds ago and delete it.+ `active` names the migration in progress, whose rollback points get the+ slots first. Callers that are only reclaiming space leave it unset and get+ the same order by recency."""limits = {@@ -246,41 +270,53 @@ def prune_state_backups(if retention is not None}candidates = _managed_candidates(backups_dir)- protected = protect.expanduser().resolve() if protect is not None else Noneremoved: list[Path] = []for kind, limit in limits.items():matching = [candidate for candidate in candidates if candidate.kind == kind]- ranked = _keep_priority(matching)- if protected is not None:- ranked.sort(key=lambda candidate: candidate.root != protected)- keep = {candidate.root for candidate in ranked[:limit]}+ keep = {candidate.root for candidate in _keep_priority(matching, active=active)[:limit]}for candidate in sorted(matching, key=lambda item: item.order_key):if candidate.root not in keep and _remove_candidate(candidate):removed.append(candidate.root)return removed-def _fsync(path: Path) -> None:- """Push a file, or a directory's entries, to stable storage.+def _fsync_file(path: Path) -> None:+ """Push a file's contents to stable storage.- Syncing the directory is what makes a rename durable on POSIX, and a backup- is worth nothing if a crash can lose it. Windows has no equivalent and- refuses to open a directory at all, so a rejection here is the platform's- answer rather than a failure to report.+ A failure here is never tolerable: it says the copy may not survive the+ crash it was made for, and raising is what stops the caller from deleting+ the copies it was meant to replace."""- flags = os.O_RDONLY- if path.is_dir():- flags |= getattr(os, "O_DIRECTORY", 0)+ fd = os.open(path, os.O_RDONLY)try:- fd = os.open(path, flags)- except OSError:- logger.debug("Cannot open %s to fsync it on this platform", path, exc_info=True)+ os.fsync(fd)+ finally:+ os.close(fd)+++def _fsync_directory(path: Path) -> None:+ """Push a directory's own entries to stable storage, where that is a thing.++ Syncing the directory is how a rename becomes durable on POSIX. Windows has+ no equivalent and refuses to open a directory at all, and some filesystems+ reject the call; those refusals are the platform answering. A storage error+ is not an answer, so it propagates like any other.+ """++ try:+ fd = os.open(path, os.O_RDONLY | getattr(os, "O_DIRECTORY", 0))+ except OSError as error:+ if error.errno not in _UNSUPPORTED_SYNC_ERRNOS:+ raise+ logger.debug("Directory sync is unavailable for %s on this platform", path)returntry:os.fsync(fd)- except OSError:- logger.debug("fsync is unavailable for %s on this platform", path, exc_info=True)+ except OSError as error:+ if error.errno not in _UNSUPPORTED_SYNC_ERRNOS:+ raise+ logger.debug("Directory sync is unavailable for %s on this filesystem", path)finally:os.close(fd)@@ -321,9 +357,9 @@ def create_sqlite_migration_backup(of their own pruning.Owning it here also means this call is the only thing that can destroy what- it just produced, so it never does: the copy is on stable storage before any- older one is deleted, and it is named as protected rather than trusted to- rank newest by its own timestamp.+ it just produced, so it never does: the copy reaches stable storage before+ any older one is deleted, and the prune is told which migration is in+ progress rather than left to infer it from timestamps it cannot trust."""source_path = db_path.expanduser().resolve()@@ -344,7 +380,7 @@ def create_sqlite_migration_backup(if check != ("ok",):raise sqlite3.DatabaseError(f"SQLite backup quick_check failed: {check!r}")os.chmod(temp_db, 0o600)- _fsync(temp_db)+ _fsync_file(temp_db)temp_db.replace(backup_db)manifest = {"schema_version": BACKUP_MANIFEST_VERSION,@@ -357,20 +393,22 @@ def create_sqlite_migration_backup(}manifest_path = backup_dir / "manifest.json"manifest_path.write_text(json.dumps(manifest, indent=2) + "\n", encoding="utf-8")- _fsync(manifest_path)- _fsync(backup_dir)- _fsync(target_root)+ _fsync_file(manifest_path)+ _fsync_directory(backup_dir)+ _fsync_directory(target_root)except Exception:shutil.rmtree(backup_dir, ignore_errors=True)raise# Everything above has to happen before the prune, and each for its own- # reason. The fsyncs make the replacement survive a crash first, so the+ # reason. The fsyncs put the replacement on the disk first, so the# filesystem can never persist the deletion of durable copies while losing- # the one that replaced them. Being after the try means a failure while- # pruning cannot reach the cleanup path and delete the rollback point this- # call just made. And it has to follow the manifest write, because a- # directory without one is not yet a recognized candidate and would not- # count itself against the bound.- prune_state_backups(target_root, json_retention=None, protect=backup_dir)+ # the one that replaced them -- and if a sync reports a storage failure, the+ # cleanup above runs and nothing is pruned at all. Being after the try means+ # a failure while pruning cannot reach that cleanup and delete the rollback+ # point this call just made. And it has to follow the manifest write,+ # because a directory without one is not yet a recognized candidate: it+ # would neither count itself against the bound nor be found as the newest+ # attempt at the migration now in progress.+ prune_state_backups(target_root, json_retention=None, active=_manifest_transition(manifest))return backup_dirdiff --git a/tests/test_state_backups.py b/tests/test_state_backups.pyindex 5e2134edd..80c330f8a 100644--- a/tests/test_state_backups.py+++ b/tests/test_state_backups.py@@ -1,5 +1,6 @@from __future__ import annotations+import errnoimport jsonimport sqlite3from datetime import datetime, timezone@@ -293,37 +294,38 @@ def test_retry_window_keeps_the_snapshot_taken_before_a_partial_upgrade(monkeypaassert intact, "the snapshot taken before the first partial upgrade must survive the retries"-def test_new_backup_survives_future_dated_copies_and_outlives_them(tmp_path: Path) -> None:+def test_future_dated_copies_do_not_outrank_the_migration_in_progress(monkeypatch, tmp_path: Path) -> None:# A clock corrected backwards, or state carried from a machine running- # ahead, leaves copies dated after the one being taken right now. Ranking by- # timestamp alone would call the fresh backup the oldest and delete it, and- # create_sqlite_migration_backup would hand back a path to nothing.+ # ahead, leaves unrelated copies dated after everything the current+ # migration produces. Being newest on disk is not the same as being current:+ # ranked by timestamp those copies take the window, and both ends of the+ # failing migration -- the clean snapshot and the latest one -- are lost to+ # backups that have nothing to do with it.state_dir = tmp_path / "state"state_dir.mkdir()db_path = state_dir / "vibe.sqlite"- with sqlite3.connect(db_path) as writer:- writer.execute("create table records (value text not null)")- writer.execute("insert into records values ('written today')")+ run_migrations(db_path, revision="20260627_0025")backups_dir = state_dir / "backups"backups_dir.mkdir()- from_the_future = [- _legacy_sqlite_backup(backups_dir, f"vibe-pre-0026-repair-20990{day}01T020000Z.sqlite")- for day in (1, 2, 3)- ]+ from_the_future = _legacy_sqlite_backup(backups_dir, "vibe-pre-0026-repair-20990101T020000Z.sqlite")- backup_dir = create_sqlite_migration_backup(- db_path,- from_revisions={"old"},- to_revisions={"new"},- now=datetime(2026, 7, 10, 3, 0, tzinfo=timezone.utc),- )+ def _partially_commit_then_fail(*args, **kwargs):+ with sqlite3.connect(db_path) as conn:+ conn.execute("create table if not exists half_migrated (value text)")+ raise RuntimeError("upgrade failed after committing")- assert (backup_dir / "vibe.sqlite").is_file()- with sqlite3.connect(backup_dir / "vibe.sqlite") as backup:- assert backup.execute("select value from records").fetchone() == ("written today",)- # Protection takes a slot in the window rather than an extra one.- assert len(_sqlite_backup_roots(backups_dir)) == SQLITE_BACKUP_RETENTION- assert sum(path.exists() for path in from_the_future) == SQLITE_BACKUP_RETENTION - 1+ monkeypatch.setattr("storage.migrations.command.upgrade", _partially_commit_then_fail)++ for _ in range(SQLITE_BACKUP_RETENTION + 1):+ with pytest.raises(RuntimeError, match="upgrade failed after committing"):+ run_migrations(db_path)++ surviving = _sqlite_backup_roots(backups_dir)+ assert len(surviving) == SQLITE_BACKUP_RETENTION+ assert from_the_future.name not in surviving, "a future timestamp is not a claim on the window"+ assert any(+ not _table_exists(backups_dir / name / "vibe.sqlite", "half_migrated") for name in surviving+ ), "the snapshot from before the partial upgrade must keep its slot"def test_new_backup_reaches_stable_storage_before_older_ones_are_deleted(monkeypatch, tmp_path: Path) -> None:@@ -342,9 +344,10 @@ def test_new_backup_reaches_stable_storage_before_older_ones_are_deleted(monkeyp_legacy_sqlite_backup(backups_dir, f"vibe-pre-0026-repair-2026070{day}T020000Z.sqlite")journal: list[tuple[str, Path]] = []- real_fsync = backups._fsync+ for name in ("_fsync_file", "_fsync_directory"):+ real = getattr(backups, name)+ monkeypatch.setattr(backups, name, lambda path, _real=real: (journal.append(("fsync", path)), _real(path))[1])real_remove = backups._remove_candidate- monkeypatch.setattr(backups, "_fsync", lambda path: (journal.append(("fsync", path)), real_fsync(path))[1])monkeypatch.setattr(backups,"_remove_candidate",@@ -361,6 +364,37 @@ def test_new_backup_reaches_stable_storage_before_older_ones_are_deleted(monkeypassert backup_dir.parent in synced+def test_storage_failure_while_flushing_deletes_nothing(monkeypatch, tmp_path: Path) -> None:+ # A sync that fails because the disk is full or the device errored says the+ # new copy may not be on it. Swallowing that and pruning anyway deletes+ # durable rollback points in exchange for one that might not exist after the+ # next power loss -- the exact trade this window exists to refuse. A+ # platform that has no directory sync is a different answer and stays quiet.+ state_dir = tmp_path / "state"+ state_dir.mkdir()+ db_path = state_dir / "vibe.sqlite"+ with sqlite3.connect(db_path) as writer:+ writer.execute("create table records (value text not null)")+ backups_dir = state_dir / "backups"+ backups_dir.mkdir()+ existing = [+ _legacy_sqlite_backup(backups_dir, f"vibe-pre-0026-repair-2026070{day}T020000Z.sqlite")+ for day in (7, 8, 9)+ ]++ def _out_of_space(fd):+ raise OSError(errno.ENOSPC, "No space left on device")++ monkeypatch.setattr(backups.os, "fsync", _out_of_space)++ with pytest.raises(OSError) as failure:+ create_sqlite_migration_backup(db_path, now=datetime(2026, 7, 10, 3, 0, tzinfo=timezone.utc))++ assert failure.value.errno == errno.ENOSPC+ assert all(path.exists() for path in existing), "a backup that may not be durable prunes nothing"+ assert not list(backups_dir.glob("avibe-sqlite-migration-*")), "the incomplete backup is cleaned up"++def test_startup_keeps_json_rollbacks_when_new_snapshot_fails(monkeypatch, tmp_path: Path) -> None:state_dir = tmp_path / "state"backups_dir = state_dir / "backups"