Skip to content

armgit / avibe

Public Imported from GitHub
0

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.

fix(storage): keep the ends of a retry window, not its newest copies

cyhd76a578247f6
diff --git a/storage/backups.py b/storage/backups.py
index d76b2e94b..80720ece7 100644
--- a/storage/backups.py
+++ b/storage/backups.py
@@ -37,11 +37,18 @@ class _BackupCandidate:
kind: str
timestamp: datetime
suffix: int
+ transition: tuple[tuple[str, ...], tuple[str, ...]] | None = None
@property
def order_key(self) -> tuple[datetime, int, str]:
return self.timestamp, self.suffix, self.root.name
+ @property
+ def group_key(self) -> object:
+ # A backup with no recorded transition cannot be shown to be an attempt
+ # at the same rollback point as any other, so it stands alone.
+ return self.transition or self.root.name
+
def _parse_timestamp(value: str) -> datetime | None:
try:
@@ -105,6 +112,26 @@ def _directory_candidate(path: Path) -> _BackupCandidate | None:
kind=kind,
timestamp=timestamp,
suffix=int(match.group("suffix") or 0),
+ transition=_manifest_transition(manifest),
+ )
+
+
+def _manifest_transition(manifest: dict) -> tuple[tuple[str, ...], tuple[str, ...]] | None:
+ """The schema move a backup was taken for, when the manifest records one.
+
+ A failed upgrade leaves the recorded revisions untouched, so every retry of
+ it reports the same move. That is what makes the move usable as the identity
+ of one rollback point across attempts. Manifests written before these fields
+ existed simply have no transition.
+ """
+
+ from_revisions = manifest.get("from_revisions")
+ to_revisions = manifest.get("to_revisions")
+ if not isinstance(from_revisions, list) or not isinstance(to_revisions, list):
+ return None
+ return (
+ tuple(sorted(str(revision) for revision in from_revisions)),
+ tuple(sorted(str(revision) for revision in to_revisions)),
)
@@ -161,16 +188,56 @@ def _remove_candidate(candidate: _BackupCandidate) -> bool:
return True
+def _keep_priority(candidates: Iterable[_BackupCandidate]) -> 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
+ rather than one rollback point each, and the two worth keeping are the ends.
+ The first predates any partially applied schema change -- an upgrade can
+ commit part of its work before raising, after which every later copy is of
+ the half-migrated database. The last carries every write made since. Copies
+ in between are worse than both on both counts, which is exactly why plain
+ newest-first is the wrong order here: it fills the window with the newest
+ two attempts and discards the only clean one.
+
+ Backups that stand alone are their own group, so for a healthy machine --
+ where each backup marks a different completed migration -- this stays the
+ familiar newest-first.
+ """
+
+ groups: dict[object, list[_BackupCandidate]] = {}
+ for candidate in candidates:
+ groups.setdefault(candidate.group_key, []).append(candidate)
+
+ 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):
+ members = sorted(group, key=lambda item: item.order_key)
+ ranked.append(members[-1])
+ if len(members) > 1:
+ ranked.append(members[0])
+ superseded.extend(members[1:-1])
+ return ranked + superseded
+
+
def prune_state_backups(
backups_dir: Path,
*,
json_retention: int | None = JSON_STATE_BACKUP_RETENTION,
sqlite_retention: int | None = SQLITE_BACKUP_RETENTION,
+ protect: Path | None = None,
) -> list[Path]:
"""Keep a bounded rollback window of backups created by Avibe.
Unknown files, symlinks, incomplete backups, and directories without a
recognized 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.
"""
limits = {
@@ -179,15 +246,45 @@ def prune_state_backups(
if retention is not None
}
candidates = _managed_candidates(backups_dir)
+ protected = protect.expanduser().resolve() if protect is not None else None
removed: list[Path] = []
for kind, limit in limits.items():
- matching = sorted((candidate for candidate in candidates if candidate.kind == kind), key=lambda item: item.order_key)
- for candidate in matching[: max(0, len(matching) - limit)]:
- if _remove_candidate(candidate):
+ 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]}
+ 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.
+
+ 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.
+ """
+
+ flags = os.O_RDONLY
+ if path.is_dir():
+ flags |= getattr(os, "O_DIRECTORY", 0)
+ try:
+ fd = os.open(path, flags)
+ except OSError:
+ logger.debug("Cannot open %s to fsync it on this platform", path, exc_info=True)
+ return
+ try:
+ os.fsync(fd)
+ except OSError:
+ logger.debug("fsync is unavailable for %s on this platform", path, exc_info=True)
+ finally:
+ os.close(fd)
+
+
def _unique_backup_dir(backups_dir: Path, *, now: datetime) -> Path:
timestamp = now.astimezone(timezone.utc).strftime("%Y%m%dT%H%M%SZ")
suffixes = [
@@ -222,6 +319,11 @@ def create_sqlite_migration_backup(
forever. Owning it at the one function that grows the window is what makes
the bound true for callers not yet written, including the ones that opt out
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.
"""
source_path = db_path.expanduser().resolve()
@@ -242,6 +344,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)
temp_db.replace(backup_db)
manifest = {
"schema_version": BACKUP_MANIFEST_VERSION,
@@ -252,16 +355,22 @@ def create_sqlite_migration_backup(
"from_revisions": sorted(set(from_revisions)),
"to_revisions": sorted(set(to_revisions)),
}
- (backup_dir / "manifest.json").write_text(json.dumps(manifest, indent=2) + "\n", encoding="utf-8")
+ 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)
except Exception:
shutil.rmtree(backup_dir, ignore_errors=True)
raise
- # After the try, deliberately: the new backup is durable and complete here,
- # so a failure while pruning can never reach the cleanup path above and
- # delete the rollback point this call just made. Pruning also has to follow
- # the manifest write, because a directory without one is not yet a
- # recognized candidate and would not count itself against the retention
- # bound.
- prune_state_backups(target_root, json_retention=None)
+ # 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
+ # 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)
return backup_dir
diff --git a/tests/test_state_backups.py b/tests/test_state_backups.py
index b9716838d..5e2134edd 100644
--- a/tests/test_state_backups.py
+++ b/tests/test_state_backups.py
@@ -7,6 +7,7 @@ from pathlib import Path
import pytest
+from storage import backups
from storage.backups import (
SQLITE_BACKUP_RETENTION,
_JSON_BACKUP_RE,
@@ -36,6 +37,13 @@ def _legacy_sqlite_backup(backups_dir: Path, name: str) -> Path:
return path
+def _table_exists(db_path: Path, table: str) -> bool:
+ with sqlite3.connect(db_path) as conn:
+ return conn.execute(
+ "select 1 from sqlite_master where type = 'table' and name = ?", (table,)
+ ).fetchone() is not None
+
+
def _sqlite_backup_roots(backups_dir: Path) -> list[str]:
"""Names in the sqlite rollback window.
@@ -236,21 +244,123 @@ def test_repeated_migration_failures_keep_the_rollback_window_bounded(monkeypatc
lambda *args, **kwargs: (_ for _ in ()).throw(RuntimeError("migration boom")),
)
- created: list[Path] = []
+ created: list[str] = []
for _ in range(SQLITE_BACKUP_RETENTION + 3):
with pytest.raises(RuntimeError, match="migration boom"):
ensure_sqlite_state(db_path=db_path, state_dir=state_dir)
- created.append(_sqlite_backup_roots(state_dir / "backups")[-1])
+ created.append(max(_sqlite_backup_roots(state_dir / "backups")))
surviving = _sqlite_backup_roots(state_dir / "backups")
assert len(surviving) == SQLITE_BACKUP_RETENTION
- assert surviving == sorted(created[-SQLITE_BACKUP_RETENTION:])
+ assert surviving == sorted({created[0], created[-1]})
# A bounded window is only worth keeping if it is still restorable.
for name in surviving:
with sqlite3.connect(state_dir / "backups" / name / "vibe.sqlite") as backup:
assert backup.execute("PRAGMA quick_check").fetchone() == ("ok",)
+def test_retry_window_keeps_the_snapshot_taken_before_a_partial_upgrade(monkeypatch, tmp_path: Path) -> None:
+ # An upgrade can commit part of its work and then raise -- a SQLite table
+ # rebuild inside an autocommit block that fails a later check is exactly
+ # that shape. From the next attempt on, every copy is of the half-migrated
+ # database, so keeping the newest copies fills the window with damage and
+ # discards the only snapshot that can undo it. What each attempt shares is
+ # the schema move it was taken for, since a failed upgrade leaves the
+ # recorded revisions alone; the window keeps the ends of that move.
+ state_dir = tmp_path / "state"
+ state_dir.mkdir()
+ db_path = state_dir / "vibe.sqlite"
+ run_migrations(db_path, revision="20260627_0025")
+
+ 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")
+
+ monkeypatch.setattr("storage.migrations.command.upgrade", _partially_commit_then_fail)
+
+ for _ in range(SQLITE_BACKUP_RETENTION + 2):
+ with pytest.raises(RuntimeError, match="upgrade failed after committing"):
+ run_migrations(db_path)
+
+ surviving = _sqlite_backup_roots(state_dir / "backups")
+ assert len(surviving) == SQLITE_BACKUP_RETENTION
+ intact = [
+ name
+ for name in surviving
+ if not _table_exists(state_dir / "backups" / name / "vibe.sqlite", "half_migrated")
+ ]
+ assert 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:
+ # 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.
+ 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')")
+ 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)
+ ]
+
+ backup_dir = create_sqlite_migration_backup(
+ db_path,
+ from_revisions={"old"},
+ to_revisions={"new"},
+ now=datetime(2026, 7, 10, 3, 0, tzinfo=timezone.utc),
+ )
+
+ 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
+
+
+def test_new_backup_reaches_stable_storage_before_older_ones_are_deleted(monkeypatch, tmp_path: Path) -> None:
+ # A backup that replaces durable copies has to be durable first. If the
+ # filesystem is free to persist the deletions while still holding the new
+ # rename and manifest in cache, a power loss here leaves no rollback point
+ # at all -- the one outcome this whole window exists to prevent.
+ 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()
+ for day in (7, 8, 9):
+ _legacy_sqlite_backup(backups_dir, f"vibe-pre-0026-repair-2026070{day}T020000Z.sqlite")
+
+ journal: list[tuple[str, Path]] = []
+ real_fsync = backups._fsync
+ real_remove = backups._remove_candidate
+ monkeypatch.setattr(backups, "_fsync", lambda path: (journal.append(("fsync", path)), real_fsync(path))[1])
+ monkeypatch.setattr(
+ backups,
+ "_remove_candidate",
+ lambda candidate: (journal.append(("remove", candidate.root)), real_remove(candidate))[1],
+ )
+
+ backup_dir = create_sqlite_migration_backup(db_path, now=datetime(2026, 7, 10, 3, 0, tzinfo=timezone.utc))
+
+ removals = [index for index, (action, _) in enumerate(journal) if action == "remove"]
+ assert removals, "the prune must have had something to delete for this ordering to mean anything"
+ synced = {path for action, path in journal[: removals[0]] if action == "fsync"}
+ assert backup_dir / "manifest.json" in synced
+ assert backup_dir in synced
+ assert backup_dir.parent in synced
+
+
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"