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): protect cache freshness and pairing

Maxa057f67c291b
diff --git a/tests/test_permissions.py b/tests/test_permissions.py
index d8ecb1a1b..bf055b795 100644
--- a/tests/test_permissions.py
+++ b/tests/test_permissions.py
@@ -757,6 +757,55 @@ def test_permissions_cache_equal_revision_retains_the_newest_complete_projection
assert cached.projection["policy_sync"] == fresh["policy_sync"]
[email protected]("terminal_status", ["error", "offline"])
+def test_permissions_cache_equal_revision_preserves_terminal_sync_failures(
+ terminal_status: str,
+) -> None:
+ applying = _complete_projection()
+ applying["policy_sync"]["status"] = "applying"
+ applying["projects"][0]["sync"].update(
+ {
+ "status": "pending",
+ "desired_access_revision": 3,
+ "applied_access_revision": 2,
+ }
+ )
+ terminal = deepcopy(applying)
+ terminal["policy_sync"]["status"] = terminal_status
+ terminal["projects"][0]["sync"].update(
+ {
+ "status": terminal_status,
+ "last_sync_error": f"{terminal_status} while applying",
+ }
+ )
+
+ permissions._cache_projection( # noqa: SLF001
+ "inst-123",
+ applying,
+ request_order=10,
+ )
+ permissions._cache_projection( # noqa: SLF001
+ "inst-123",
+ terminal,
+ request_order=11,
+ )
+ # A delayed response from the older request must not roll the terminal
+ # state back to the in-progress snapshot.
+ permissions._cache_projection( # noqa: SLF001
+ "inst-123",
+ applying,
+ request_order=10,
+ )
+
+ cached = permissions._read_cache("inst-123") # noqa: SLF001
+ assert cached is not None
+ assert cached.projection["policy_sync"]["status"] == terminal_status
+ assert cached.projection["projects"][0]["sync"]["status"] == terminal_status
+ assert cached.projection["projects"][0]["sync"]["last_sync_error"] == (
+ f"{terminal_status} while applying"
+ )
+
+
def test_permissions_cache_mutation_rebase_reads_complete_equal_revision_projection() -> None:
base = _complete_projection()
complete = deepcopy(base)
@@ -1108,6 +1157,105 @@ def test_permissions_rejects_cached_fallback_after_inflight_pairing_change(
permissions.get_current_permissions(config)
[email protected]("operation", ["get", "authorized_users", "project_access"])
+def test_permissions_cache_write_rechecks_pairing_for_every_projection_writer(
+ monkeypatch,
+ operation: str,
+) -> None:
+ config = _config()
+ old_projection = _complete_projection()
+ new_projection = _complete_projection("inst-new")
+ permissions._cache_projection("inst-123", old_projection) # noqa: SLF001
+ switched = False
+
+ def switch_pairing() -> None:
+ nonlocal switched
+ if switched:
+ return
+ switched = True
+ replacement = V2Config.load()
+ replacement.remote_access.vibe_cloud.instance_id = "inst-new"
+ replacement.save()
+ permissions._cache_projection( # noqa: SLF001
+ "inst-new",
+ new_projection,
+ )
+
+ if operation == "get":
+ original_guard = permissions._guard_current_pairing # noqa: SLF001
+
+ def switch_after_response_guard(credentials, load_current_config):
+ original_guard(credentials, load_current_config)
+ switch_pairing()
+
+ monkeypatch.setattr(
+ permissions,
+ "_guard_current_pairing",
+ switch_after_response_guard,
+ )
+ else:
+ monkeypatch.setattr(
+ permissions,
+ "_acknowledge_authorization_revision",
+ lambda *_args: switch_pairing(),
+ )
+
+ def request(_method, _url, **_kwargs):
+ if operation == "get":
+ return _Response(200, old_projection)
+ if operation == "authorized_users":
+ return _Response(
+ 200,
+ {
+ "ok": True,
+ "entries": old_projection["access"]["entries"],
+ "authorization_revision": 3,
+ },
+ )
+ return _Response(
+ 200,
+ {
+ "ok": True,
+ "project": old_projection["projects"][0],
+ "authorization_revision": 3,
+ },
+ )
+
+ monkeypatch.setattr(permissions.requests, "request", request)
+
+ with pytest.raises(
+ permissions.PermissionsPairingChangedError,
+ match="permissions_pairing_changed",
+ ):
+ if operation == "get":
+ permissions.get_current_permissions(config)
+ elif operation == "authorized_users":
+ permissions.replace_authorized_users(
+ {
+ "entries": old_projection["access"]["entries"],
+ "if_match_revision": 3,
+ "if_match_instance_id": "inst-123",
+ },
+ config,
+ )
+ else:
+ permissions.update_project_access(
+ "project-1",
+ {
+ "mode": old_projection["projects"][0]["access"]["mode"],
+ "bindings": old_projection["projects"][0]["access"]["bindings"],
+ "if_match_revision": 2,
+ "if_match_instance_id": "inst-123",
+ },
+ config,
+ )
+
+ cached = permissions._read_cache("inst-new") # noqa: SLF001
+ assert cached is not None
+ assert cached.projection == new_projection
+ assert permissions._read_cache("inst-123") is None # noqa: SLF001
+
+
@pytest.mark.parametrize(
"load_error",
[
diff --git a/vibe/permissions.py b/vibe/permissions.py
index 72ae6ff66..9918dd5a7 100644
--- a/vibe/permissions.py
+++ b/vibe/permissions.py
@@ -40,20 +40,6 @@ _POLICY_SYNC_STATUSES = frozenset({"none", "in_sync", "applying", "offline", "er
_SYNC_COUNT_KEYS = ("active", "error", "offline", "applying", "in_sync")
_PROJECT_ID_PATH_SEPARATORS = frozenset({"/", "\\"})
_PROJECT_ID_DOT_SEGMENTS = frozenset({".", ".."})
-_POLICY_SYNC_PROGRESS = {
- "none": 0,
- "offline": 1,
- "error": 1,
- "applying": 2,
- "in_sync": 3,
-}
-_PROJECT_SYNC_PROGRESS = {
- "offline": 0,
- "error": 0,
- "pending": 1,
- "in_sync": 2,
- "deleted": 3,
-}
logger = logging.getLogger(__name__)
_CACHE_LOCK = threading.RLock()
@@ -680,31 +666,20 @@ def _prefer_mapping(
return merged
-def _project_progress(project: Mapping[str, Any]) -> tuple[int, int, int, int, str]:
- sync = project["sync"]
- access = project["access"]
- return (
- int(access["revision"]),
- int(sync["desired_access_revision"]),
- int(sync["applied_access_revision"]),
- _PROJECT_SYNC_PROGRESS[str(sync["status"])],
- str(sync.get("last_synced_at") or ""),
- )
-
-
def _merge_project_projection(
preferred: Mapping[str, Any],
fallback: Mapping[str, Any],
) -> dict[str, Any]:
- merged = _prefer_mapping(preferred, fallback)
- advanced, other = (
- (preferred, fallback)
- if _project_progress(preferred) >= _project_progress(fallback)
- else (fallback, preferred)
- )
- merged["access"] = _prefer_mapping(advanced["access"], other["access"])
- merged["sync"] = _prefer_mapping(advanced["sync"], other["sync"])
- return merged
+ """Merge one Project while preserving the caller-selected snapshot winner.
+
+ Equal-revision snapshots are ordered by their request-start epoch at the
+ outer projection boundary. A status ranking here would let an older
+ ``applying``/``pending`` response overwrite a newer terminal failure.
+ Mutation acknowledgements also use this helper so their authoritative
+ Project fields are retained while additive unknown fields survive.
+ """
+
+ return _prefer_mapping(preferred, fallback)
def _merge_projects(
@@ -724,13 +699,9 @@ def _merge_policy_sync(
preferred: Mapping[str, Any],
fallback: Mapping[str, Any],
) -> dict[str, Any]:
- advanced, other = (
- (preferred, fallback)
- if _POLICY_SYNC_PROGRESS[str(preferred["status"])]
- >= _POLICY_SYNC_PROGRESS[str(fallback["status"])]
- else (fallback, preferred)
- )
- return _prefer_mapping(advanced, other)
+ """Preserve the request-order winner for aggregate sync state."""
+
+ return _prefer_mapping(preferred, fallback)
def _merge_equal_revision_projection(
@@ -760,6 +731,7 @@ def _cache_read_merge_write(
*,
request_order: int,
mutation_rebase: bool = False,
+ pairing_guard: Callable[[], None] | None = None,
) -> None:
"""Read, merge, and atomically replace one instance cache under one lock."""
@@ -768,6 +740,8 @@ def _cache_read_merge_write(
cache_path = _cache_path()
cache_path.parent.mkdir(parents=True, exist_ok=True)
with _cache_file_lock(cache_path):
+ if pairing_guard is not None:
+ pairing_guard()
cached = _read_cache(instance_id)
candidate = merge(cached.projection if cached is not None else None)
if candidate is None:
@@ -795,6 +769,8 @@ def _cache_read_merge_write(
),
)
validated = _validated_projection(validated, instance_id)
+ if pairing_guard is not None:
+ pairing_guard()
_write_cache(instance_id, validated, cache_order=cache_order)
except (OSError, TimeoutError):
logger.warning("Unable to cache the current Permissions projection", exc_info=True)
@@ -805,12 +781,14 @@ def _cache_projection(
projection: Any,
*,
request_order: int | None = None,
+ pairing_guard: Callable[[], None] | None = None,
) -> None:
order = _cache_allocate_order(instance_id) if request_order is None else request_order
_cache_read_merge_write(
instance_id,
lambda _cached: projection,
request_order=order,
+ pairing_guard=pairing_guard,
)
@@ -821,6 +799,7 @@ def _cache_mutation_result(
access_entries: list[Any] | None = None,
project: Mapping[str, Any] | None = None,
request_order: int | None = None,
+ pairing_guard: Callable[[], None] | None = None,
) -> None:
def merge(cached: dict[str, Any] | None) -> dict[str, Any] | None:
if cached is None:
@@ -870,6 +849,7 @@ def _cache_mutation_result(
merge,
request_order=order,
mutation_rebase=True,
+ pairing_guard=pairing_guard,
)
@@ -936,24 +916,33 @@ def _backend_request(
def get_current_permissions(config: V2Config | None = None) -> PermissionsProjectionResult:
config, load_current_config = _request_config(config)
- _, instance_id, _ = _runtime_credentials(config)
+ credentials = _runtime_credentials(config)
+ _, instance_id, _ = credentials
request_order = _cache_allocate_order(instance_id)
+ pairing_guard = lambda: _guard_current_pairing(credentials, load_current_config)
try:
payload, _ = _backend_request(config, load_current_config, "GET", "")
projection = _validated_projection(payload, instance_id)
- _cache_projection(instance_id, projection, request_order=request_order)
+ _cache_projection(
+ instance_id,
+ projection,
+ request_order=request_order,
+ pairing_guard=pairing_guard,
+ )
return PermissionsProjectionResult(
projection=projection,
source="live",
cache_order=request_order,
)
except PermissionsUnavailableError:
+ pairing_guard()
cached = _read_cache(instance_id)
if cached is not None:
return cached
raise
except PermissionsBackendError as exc:
if _is_backend_unavailable_status(exc.status):
+ pairing_guard()
cached = _read_cache(instance_id)
if cached is not None:
return cached
@@ -966,8 +955,10 @@ def replace_authorized_users(
) -> dict[str, Any]:
config, load_current_config = _request_config(config)
expected_instance_id, backend_payload = _mutation_payload(payload)
- _, request_instance_id, _ = _runtime_credentials(config)
+ credentials = _runtime_credentials(config)
+ _, request_instance_id, _ = credentials
request_order = _cache_allocate_order(request_instance_id)
+ pairing_guard = lambda: _guard_current_pairing(credentials, load_current_config)
payload_result, instance_id = _backend_request(
config,
load_current_config,
@@ -977,12 +968,14 @@ def replace_authorized_users(
expected_instance_id=expected_instance_id,
)
result = _validated_authorized_users_result(payload_result)
+ pairing_guard()
_acknowledge_authorization_revision(config, result["authorization_revision"])
_cache_mutation_result(
instance_id,
result["authorization_revision"],
access_entries=result["entries"],
request_order=request_order,
+ pairing_guard=pairing_guard,
)
return {**result, "instance_id": instance_id}
@@ -996,8 +989,10 @@ def update_project_access(
raise PermissionsInvalidResponseError("invalid_project_id")
config, load_current_config = _request_config(config)
expected_instance_id, backend_payload = _mutation_payload(payload)
- _, request_instance_id, _ = _runtime_credentials(config)
+ credentials = _runtime_credentials(config)
+ _, request_instance_id, _ = credentials
request_order = _cache_allocate_order(request_instance_id)
+ pairing_guard = lambda: _guard_current_pairing(credentials, load_current_config)
payload_result, instance_id = _backend_request(
config,
load_current_config,
@@ -1007,12 +1002,14 @@ def update_project_access(
expected_instance_id=expected_instance_id,
)
result = _validated_project_result(payload_result, project_id)
+ pairing_guard()
_acknowledge_authorization_revision(config, result["authorization_revision"])
_cache_mutation_result(
instance_id,
result["authorization_revision"],
project=result["project"],
request_order=request_order,
+ pairing_guard=pairing_guard,
)
return {**result, "instance_id": instance_id}