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 cache rebases

Max2fc03e508674
diff --git a/tests/test_permissions.py b/tests/test_permissions.py
index 97709d93b..3ee2443cf 100644
--- a/tests/test_permissions.py
+++ b/tests/test_permissions.py
@@ -65,6 +65,27 @@ def _cache_projection_process(
permissions._cache_projection("inst-123", projection) # noqa: SLF001
+def _cache_mutation_rebase_process(
+ access_entries: list[dict[str, str]],
+ entered_lock,
+ release_lock,
+) -> None:
+ original_lock = permissions._cache_file_lock # noqa: SLF001
+
+ def delayed_lock(path):
+ entered_lock.set()
+ if not release_lock.wait(10):
+ raise TimeoutError("cache lock was not released")
+ return original_lock(path)
+
+ permissions._cache_file_lock = delayed_lock # noqa: SLF001
+ permissions._cache_mutation_result( # noqa: SLF001
+ "inst-123",
+ 3,
+ access_entries=access_entries,
+ )
+
+
def _projection(instance_id: str = "inst-123") -> dict:
return {
"schema_version": 1,
@@ -624,6 +645,46 @@ def test_permissions_cache_revision_is_monotonic_across_processes() -> None:
assert cached.projection["instance"]["authorization_revision"] == 4
+def test_permissions_cache_mutation_rebase_reads_complete_equal_revision_projection() -> None:
+ base = _complete_projection()
+ complete = deepcopy(base)
+ complete["directory"]["members"][0]["email"] = "[email protected]"
+ complete["projects"][0]["display_name"] = "Newest Project"
+ complete["policy_sync"]["projects"]["active"] = 7
+ updated_entries = [
+ {"kind": "email", "value": "[email protected]", "role": "editor"}
+ ]
+ permissions._cache_projection("inst-123", base) # noqa: SLF001
+
+ context = mp.get_context("spawn")
+ entered_lock = context.Event()
+ release_lock = context.Event()
+ mutation = context.Process(
+ target=_cache_mutation_rebase_process,
+ args=(updated_entries, entered_lock, release_lock),
+ )
+
+ try:
+ mutation.start()
+ assert entered_lock.wait(10)
+ permissions._cache_projection("inst-123", complete) # noqa: SLF001
+ release_lock.set()
+ mutation.join(10)
+ assert mutation.exitcode == 0
+ finally:
+ release_lock.set()
+ if mutation.is_alive():
+ mutation.terminate()
+ mutation.join(5)
+
+ cached = permissions._read_cache("inst-123") # noqa: SLF001
+ assert cached is not None
+ assert cached.projection["directory"] == complete["directory"]
+ assert cached.projection["projects"] == complete["projects"]
+ assert cached.projection["policy_sync"] == complete["policy_sync"]
+ assert cached.projection["access"]["entries"] == updated_entries
+
+
@pytest.mark.parametrize(
"status",
[*sorted(permissions._CACHE_FALLBACK_HTTP_STATUSES), 503], # noqa: SLF001
diff --git a/vibe/permissions.py b/vibe/permissions.py
index 479e6eaa4..bda5902c8 100644
--- a/vibe/permissions.py
+++ b/vibe/permissions.py
@@ -594,14 +594,22 @@ def _read_cache(instance_id: str) -> PermissionsProjectionResult | None:
)
-def _cache_projection(instance_id: str, projection: Any) -> None:
- validated = _validated_projection(projection, instance_id)
+def _cache_read_merge_write(
+ instance_id: str,
+ merge: Callable[[dict[str, Any] | None], Any],
+) -> None:
+ """Read, merge, and atomically replace one instance cache under one lock."""
+
with _CACHE_LOCK:
try:
cache_path = _cache_path()
cache_path.parent.mkdir(parents=True, exist_ok=True)
with _cache_file_lock(cache_path):
cached = _read_cache(instance_id)
+ candidate = merge(cached.projection if cached is not None else None)
+ if candidate is None:
+ return
+ validated = _validated_projection(candidate, instance_id)
if (
cached is not None
and cached.projection["instance"]["authorization_revision"]
@@ -613,6 +621,10 @@ def _cache_projection(instance_id: str, projection: Any) -> None:
logger.warning("Unable to cache the current Permissions projection", exc_info=True)
+def _cache_projection(instance_id: str, projection: Any) -> None:
+ _cache_read_merge_write(instance_id, lambda _cached: projection)
+
+
def _cache_mutation_result(
instance_id: str,
authorization_revision: int,
@@ -620,36 +632,36 @@ def _cache_mutation_result(
access_entries: list[Any] | None = None,
project: Mapping[str, Any] | None = None,
) -> None:
- with _CACHE_LOCK:
- cached = _read_cache(instance_id)
+ def merge(cached: dict[str, Any] | None) -> dict[str, Any] | None:
if cached is None:
- return
- projection = cached.projection
- current_revision = projection["instance"]["authorization_revision"]
+ return None
+ current_revision = cached["instance"]["authorization_revision"]
if authorization_revision < current_revision:
- return
+ return None
projection = {
- **projection,
+ **cached,
"instance": {
- **projection["instance"],
+ **cached["instance"],
"authorization_revision": authorization_revision,
},
}
if access_entries is not None:
projection["access"] = {
- **projection["access"],
+ **cached["access"],
"entries": access_entries,
}
if project is not None:
project_id = project.get("project_id")
projects = [
project if current.get("project_id") == project_id else current
- for current in projection["projects"]
+ for current in cached["projects"]
]
if not any(current.get("project_id") == project_id for current in projects):
projects.append(project)
projection["projects"] = projects
- _cache_projection(instance_id, _validated_projection(projection, instance_id))
+ return projection
+
+ _cache_read_merge_write(instance_id, merge)
def _acknowledge_authorization_revision(config: V2Config, revision: int) -> None: