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): close legacy access races

Maxc83cc149645f
diff --git a/core/show_pages.py b/core/show_pages.py
index 0cebd9868..69a2f8865 100644
--- a/core/show_pages.py
+++ b/core/show_pages.py
@@ -779,8 +779,17 @@ class ShowPageStore:
session_id,
connection=connection,
)
- if current_policy is None and not user_context.can_use_show_page(session_id):
- cls._require_project_edit_access(connection, session_id, user_context)
+ if current_policy is None:
+ try:
+ cls._require_project_edit_access(connection, session_id, user_context)
+ except ShowPageError:
+ if not user_context.can_use_show_page(session_id):
+ raise
+ return {
+ "status": "unchanged",
+ "ownership": ownership,
+ "policy": None,
+ }
owner_user_id = (
user_context.subject
if user_context.subject and user_context.has_role("editor")
diff --git a/tests/test_resource_acl_show_pages.py b/tests/test_resource_acl_show_pages.py
index f37966a7b..51a174f82 100644
--- a/tests/test_resource_acl_show_pages.py
+++ b/tests/test_resource_acl_show_pages.py
@@ -609,7 +609,7 @@ def test_existing_show_page_is_adopted_idempotently_without_changing_link_access
store.close()
-def test_legacy_show_page_policy_reconciliation_requires_existing_page_or_project_access(
+def test_legacy_show_page_policy_reconciliation_keeps_page_grants_read_only(
monkeypatch,
tmp_path,
) -> None:
@@ -617,7 +617,8 @@ def test_legacy_show_page_policy_reconciliation_requires_existing_page_or_projec
store = ShowPageStore()
project_path = tmp_path / "project"
project_path.mkdir()
- editor = _organization_context("editor-1", instance_role="editor")
+ instance_editor = _organization_context("editor-1", instance_role="editor")
+ project_editor = _organization_context("project-editor", instance_role="editor")
try:
with store.engine.begin() as connection:
project = projects_service.create_project(connection, str(project_path))
@@ -644,8 +645,27 @@ def test_legacy_show_page_policy_reconciliation_requires_existing_page_or_projec
},
)
+ page_guest = resource_access_service.ResourceUserContext(
+ subject="guest-1",
+ instance_role="viewer",
+ instance_access_source="show_page_email",
+ show_page_id=session_id,
+ is_remote=True,
+ )
+ guest_response = api.get_show_page_access(session_id, user_context=page_guest)
+ assert guest_response["ownership_status"] == "unchanged"
+ assert guest_response["can_use"] is True
+ assert guest_response["can_manage"] is False
+ with store.engine.connect() as connection:
+ assert resource_access_service.get_resource_policy(
+ "show_page",
+ session_id,
+ connection=connection,
+ ) is None
+
with pytest.raises(ShowPageError, match="Show Page access is not permitted"):
- api.get_show_page_access(session_id, user_context=editor)
+ api.get_show_page_access(session_id, user_context=instance_editor)
with store.engine.connect() as connection:
assert resource_access_service.get_resource_policy(
"show_page",
@@ -662,13 +682,13 @@ def test_legacy_show_page_policy_reconciliation_requires_existing_page_or_projec
"mode": "restricted",
"bindings": [{
"principal_kind": "email",
- "principal_value": "[email protected]",
+ "principal_value": "[email protected]",
"access_role": "editor",
}],
},
)
- response = api.get_show_page_access(session_id, user_context=editor)
+ response = api.get_show_page_access(session_id, user_context=project_editor)
with store.engine.connect() as connection:
policy = resource_access_service.get_resource_policy(
"show_page",
@@ -679,7 +699,7 @@ def test_legacy_show_page_policy_reconciliation_requires_existing_page_or_projec
assert response["can_use"] is True
assert response["can_manage"] is True
assert policy is not None
- assert policy["owner_user_id"] == "editor-1"
+ assert policy["owner_user_id"] == "project-editor"
assert policy["organization_id"] == "org-1"
finally:
store.close()
diff --git a/ui/src/features/permissions/PermissionsPage.test.tsx b/ui/src/features/permissions/PermissionsPage.test.tsx
index c233ebf67..70853db6f 100644
--- a/ui/src/features/permissions/PermissionsPage.test.tsx
+++ b/ui/src/features/permissions/PermissionsPage.test.tsx
@@ -1123,6 +1123,63 @@ describe('PermissionsPage conflict handling', () => {
expect(api.replaceAuthorizedUsers).toHaveBeenCalledOnce();
});
+ it.each([
+ { draftRole: 'editor' as const, concurrentRole: 'viewer' as const, narrows: false },
+ { draftRole: 'viewer' as const, concurrentRole: 'editor' as const, narrows: true },
+ ])('rebases an access addition onto a concurrently created matching principal', async ({
+ draftRole,
+ concurrentRole,
+ narrows,
+ }) => {
+ const initial = response();
+ const latest = response();
+ latest.projection.instance.authorization_revision = 5;
+ latest.projection.access.entries = [
+ { kind: 'email', value: '[email protected]', role: concurrentRole },
+ ];
+ api.getPermissions
+ .mockResolvedValueOnce(initial)
+ .mockResolvedValueOnce(latest);
+ api.replaceAuthorizedUsers
+ .mockRejectedValueOnce(new PermissionsApiError(409, {
+ error: 'permission_revision_conflict',
+ current_revision: 5,
+ }))
+ .mockResolvedValueOnce({
+ ok: true,
+ instance_id: 'inst-123',
+ authorization_revision: 6,
+ entries: [{ kind: 'email', value: '[email protected]', role: draftRole }],
+ });
+ const user = userEvent.setup();
+ renderPage();
+
+ await user.click(await screen.findByRole('button', { name: 'permissions.actions.addAccess' }));
+ await user.type(screen.getByLabelText('permissions.fields.principal'), '[email protected]');
+ if (draftRole === 'editor') {
+ await user.click(screen.getByRole('radio', { name: 'permissions.roles.editor' }));
+ }
+ await user.click(screen.getByRole('button', { name: 'permissions.actions.save' }));
+
+ expect(await screen.findByText('permissions.states.conflictTitle')).toBeTruthy();
+ expect(screen.getByText('permissions.access.editTitle')).toBeTruthy();
+ await user.click(screen.getByRole('button', { name: 'permissions.actions.retrySave' }));
+ if (narrows) {
+ const narrowingDialog = (await screen.findByText('permissions.access.narrowTitle')).closest('[role="dialog"]');
+ await user.click(within(narrowingDialog as HTMLElement).getByRole('button', {
+ name: 'permissions.actions.save',
+ }));
+ }
+
+ await waitFor(() => expect(api.replaceAuthorizedUsers).toHaveBeenCalledTimes(2));
+ expect(api.replaceAuthorizedUsers.mock.calls[1]).toEqual([
+ [{ kind: 'email', value: '[email protected]', role: draftRole }],
+ 5,
+ 'inst-123',
+ ]);
+ expect(screen.queryByText('permissions.errors.duplicate_access_principal')).toBeNull();
+ });
+
it('closes a principal replacement when conflict refresh shows it was already applied', async () => {
const initial = response();
initial.projection.access.entries = [
diff --git a/ui/src/features/permissions/PermissionsPage.tsx b/ui/src/features/permissions/PermissionsPage.tsx
index db53088e3..a2939255d 100644
--- a/ui/src/features/permissions/PermissionsPage.tsx
+++ b/ui/src/features/permissions/PermissionsPage.tsx
@@ -354,7 +354,7 @@ function AccessEntryDialog({
const authoritativeIndex = originalKey === null
? -1
: entries.findIndex((entry) => accessEntryKey(entry) === originalKey);
- const nextEntries = editingKey === null || authoritativeIndex < 0
+ const nextEntries = originalKey === null || authoritativeIndex < 0
? [...entries, candidate]
: entries.map((entry, index) => (index === authoritativeIndex ? candidate : entry));
@@ -388,7 +388,12 @@ function AccessEntryDialog({
onOpenChange(false);
return true;
}
- baselineEntry.current = latestOriginal;
+ if (originalKey === null && latestCandidate) {
+ originalEntry.current = latestCandidate;
+ baselineEntry.current = latestCandidate;
+ } else {
+ baselineEntry.current = latestOriginal;
+ }
setRevision(latest.response.projection.instance.authorization_revision);
setRefreshRequired(false);
setError(undefined);
@@ -453,7 +458,7 @@ function AccessEntryDialog({
<Dialog open={open} onOpenChange={onOpenChange}>
<DialogContent>
<DialogHeader>
- <DialogTitle>{t(editingKey === null ? 'permissions.access.addTitle' : 'permissions.access.editTitle')}</DialogTitle>
+ <DialogTitle>{t(originalKey === null ? 'permissions.access.addTitle' : 'permissions.access.editTitle')}</DialogTitle>
<DialogDescription>{t('permissions.access.dialogBody')}</DialogDescription>
</DialogHeader>
{conflict ? (