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(permissions): serialize authorization watermark writes

Maxd7948497cbe1
diff --git a/tests/test_remote_authorization_revision.py b/tests/test_remote_authorization_revision.py
index 8fcb6643b..a331c38e9 100644
--- a/tests/test_remote_authorization_revision.py
+++ b/tests/test_remote_authorization_revision.py
@@ -846,6 +846,63 @@ def test_remote_authorization_failure_serves_spa_shell_but_rejects_api(
assert protected.get_json()["error"] == error
+def _delayed_authorization_revision_sync_writer(
+ home: str,
+ entered: Any,
+ release: Any,
+ result_queue: Any,
+) -> None:
+ import os as _os
+
+ _os.environ["AVIBE_HOME"] = home
+ from config.v2_config import V2Config as _V2Config
+ from vibe import remote_access as _remote_access
+
+ config = _V2Config.load()
+ state_path = _remote_access._authorization_revision_state_path()
+ original_read_json = _remote_access.runtime.read_json
+ paused = False
+
+ def delayed_read_json(path):
+ nonlocal paused
+
+ payload = original_read_json(path)
+ if path == state_path and not paused:
+ paused = True
+ entered.set()
+ if not release.wait(timeout=15):
+ raise AssertionError("delayed revision writer was never released")
+ return payload
+
+ _remote_access.runtime.read_json = delayed_read_json
+ try:
+ result_queue.put(("ok", _remote_access._replace_authorization_revision(config, 42)))
+ except Exception as exc:
+ result_queue.put(("error", repr(exc)))
+
+
+def _authorization_revision_acknowledgement_writer(
+ home: str,
+ attempted: Any,
+ done: Any,
+ result_queue: Any,
+) -> None:
+ import os as _os
+
+ _os.environ["AVIBE_HOME"] = home
+ from config.v2_config import V2Config as _V2Config
+ from vibe import remote_access as _remote_access
+
+ config = _V2Config.load()
+ attempted.set()
+ try:
+ result_queue.put(("ok", _remote_access.acknowledge_authorization_revision(config, 43)))
+ except Exception as exc:
+ result_queue.put(("error", repr(exc)))
+ finally:
+ done.set()
+
+
def test_authorization_revision_device_contract_is_monotonic(monkeypatch, tmp_path):
"""I1057-AC2/AC3: one paired-device watermark drives every hostname."""
@@ -878,6 +935,60 @@ def test_authorization_revision_device_contract_is_monotonic(monkeypatch, tmp_pa
assert remote_access.current_authorization_revision(config) == 42
+def test_authorization_revision_writes_are_monotonic_across_processes(
+ monkeypatch,
+ tmp_path,
+):
+ """A delayed synchronization write cannot replace a newer acknowledgement."""
+
+ import multiprocessing as mp
+
+ monkeypatch.setenv("AVIBE_HOME", str(tmp_path))
+ _paired_config(tmp_path, revision=41)
+ ctx = mp.get_context("spawn")
+ sync_entered = ctx.Event()
+ release_sync = ctx.Event()
+ acknowledgement_attempted = ctx.Event()
+ acknowledgement_done = ctx.Event()
+ sync_result = ctx.Queue()
+ acknowledgement_result = ctx.Queue()
+ sync_writer = ctx.Process(
+ target=_delayed_authorization_revision_sync_writer,
+ args=(str(tmp_path), sync_entered, release_sync, sync_result),
+ )
+ acknowledgement_writer = ctx.Process(
+ target=_authorization_revision_acknowledgement_writer,
+ args=(
+ str(tmp_path),
+ acknowledgement_attempted,
+ acknowledgement_done,
+ acknowledgement_result,
+ ),
+ )
+
+ sync_writer.start()
+ assert sync_entered.wait(timeout=15), "sync writer never entered its transaction"
+ acknowledgement_writer.start()
+ assert acknowledgement_attempted.wait(timeout=15), "acknowledgement writer never attempted"
+ assert not acknowledgement_done.wait(timeout=1), (
+ "acknowledgement writer bypassed the synchronization transaction"
+ )
+ release_sync.set()
+ sync_writer.join(timeout=15)
+ acknowledgement_writer.join(timeout=15)
+
+ assert sync_writer.exitcode == 0, f"sync writer failed: {sync_writer.exitcode}"
+ assert acknowledgement_writer.exitcode == 0, (
+ f"acknowledgement writer failed: {acknowledgement_writer.exitcode}"
+ )
+ assert sync_result.get(timeout=5) == ("ok", 42)
+ assert acknowledgement_result.get(timeout=5) == ("ok", 43)
+ persisted = remote_access.runtime.read_json(
+ remote_access._authorization_revision_state_path()
+ )
+ assert persisted["authorization_revision"] == 43
+
+
def test_authorization_revision_cache_observes_another_process_watermark(
monkeypatch,
tmp_path,
diff --git a/vibe/remote_access.py b/vibe/remote_access.py
index fd89557f6..96121af96 100644
--- a/vibe/remote_access.py
+++ b/vibe/remote_access.py
@@ -243,6 +243,15 @@ def _authorization_revision_state_path() -> Path:
return paths.get_state_dir() / "remote-access-authorization-revision.json"
+def _authorization_revision_file_lock(path: Path):
+ """Return the cross-process lock for the authorization watermark."""
+
+ # Import lazily because storage's package initializer imports V2Config.
+ from storage.lock import MigrationFileLock
+
+ return MigrationFileLock(path.with_name(f".{path.name}.lock"))
+
+
def _quality_state_path() -> Path:
return paths.get_runtime_dir() / "remote-access-tunnel-quality.json"
@@ -767,41 +776,54 @@ def _install_authorization_revision(
source_updated_at = time.time()
with _AUTHORIZATION_REVISION_LOCK:
cached = _AUTHORIZATION_REVISION_CACHE
- if (
+ cache_matches = (
cached is not None
and cached.state_path == state_path
and cached.instance_id == instance_id
- ):
- previous_revision = cached.revision
- else:
- payload = runtime.read_json(state_path)
- previous_revision = None
- if (
- isinstance(payload, dict)
- and payload.get("schema_version") == 1
- and str(payload.get("instance_id") or "").strip() == instance_id
- ):
- try:
- previous_revision = _normalize_authorization_revision(
- payload.get("authorization_revision")
- )
- except ValueError:
- previous_revision = None
- if previous_revision is not None and revision < previous_revision:
- raise ValueError("authorization_revision_regressed")
- changed = previous_revision != revision
- payload = {
- "schema_version": 1,
- "instance_id": instance_id,
- "authorization_revision": revision,
- "source_updated_at": source_updated_at,
- }
+ )
+ cached_revision = cached.revision if cache_matches else None
+ previous_revision = cached_revision
+ changed = cached_revision != revision
+ file_signature = None
try:
state_path.parent.mkdir(parents=True, exist_ok=True)
- runtime.write_json(state_path, payload)
+ with _authorization_revision_file_lock(state_path):
+ persisted = runtime.read_json(state_path)
+ persisted_revision = None
+ if (
+ isinstance(persisted, dict)
+ and persisted.get("schema_version") == 1
+ and str(persisted.get("instance_id") or "").strip() == instance_id
+ ):
+ try:
+ persisted_revision = _normalize_authorization_revision(
+ persisted.get("authorization_revision")
+ )
+ except ValueError:
+ persisted_revision = None
+ if persisted_revision is not None and (
+ previous_revision is None or persisted_revision > previous_revision
+ ):
+ previous_revision = persisted_revision
+ if cached_revision is None:
+ changed = persisted_revision != revision
+ if previous_revision is not None and revision < previous_revision:
+ raise ValueError("authorization_revision_regressed")
+ runtime.write_json(
+ state_path,
+ {
+ "schema_version": 1,
+ "instance_id": instance_id,
+ "authorization_revision": revision,
+ "source_updated_at": source_updated_at,
+ },
+ )
+ file_signature = _authorization_revision_file_signature(state_path)
except OSError:
if persistence_required:
raise
+ if previous_revision is not None and revision < previous_revision:
+ raise ValueError("authorization_revision_regressed") from None
logger.warning(
"Authorization revision acknowledgement could not be persisted",
exc_info=True,
@@ -811,7 +833,7 @@ def _install_authorization_revision(
instance_id=instance_id,
revision=revision,
source_updated_at=source_updated_at,
- file_signature=_authorization_revision_file_signature(state_path),
+ file_signature=file_signature,
)
if changed:
try: