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.
Python agentagent-osaiai-agentsai-partnerchatopsclaudeclaude-codecodexdevtoolsdiscord-botlark-botllmlocal-firstlong-term-memoryopencodeslack-botvibe-codingvibeywechat
feat(storage): add resource ACL policy foundation
Maxvintb626e46c2a3a
diff --git a/docs/plans/resource-acl-sync.md b/docs/plans/resource-acl-sync.mdnew file mode 100644index 000000000..9f121e3cf--- /dev/null+++ b/docs/plans/resource-acl-sync.md@@ -0,0 +1,33 @@+# Resource ACL Sync Foundation++## Background++Organization membership arrives at the local runtime in a signed OIDC ID token,+while resource content remains local. The runtime therefore needs a local ACL+projection that can be updated only by versioned control-plane intents.++## Goal++Add the SQLite policy state, signed-session organization context, and paired+device sync needed by later resource-specific enforcement work. This change+does not add enforcement to agents, vaults, skills, or Show Pages.++## Design++- Store one policy per `(resource_kind, resource_id)` and group bindings in a+ separate table with a cascading composite foreign key.+- Keep OIDC organization claims in the signed local remote-access cookie. A+ membership-bearing cookie re-enters OIDC at its refresh boundary rather than+ extending stale group claims indefinitely.+- Publish safe resource metadata and the currently applied ACL revision over+ the paired-instance device channel. Pull a newer intent, apply it in one+ SQLite transaction, then acknowledge that exact revision.+- Do not mutate an organization policy through a local standalone revision.+ The hosted organization resource API remains the writer of desired intents.++## Verification++- Unit test private, public, scoped, and missing-group policy evaluation.+- Unit test newer-only intent application, exact ACK behavior, and offline+ retention of the prior local policy.+- Run the repository's unit, static syntax, and lint commands before commit.diff --git a/storage/alembic/versions/20260725_0035_resource_access_policies.py b/storage/alembic/versions/20260725_0035_resource_access_policies.pynew file mode 100644index 000000000..9c2df6496--- /dev/null+++ b/storage/alembic/versions/20260725_0035_resource_access_policies.py@@ -0,0 +1,97 @@+"""add local resource access policies++Revision ID: 20260725_0035+Revises: 20260724_0034+Create Date: 2026-07-25+"""++from __future__ import annotations++import sqlalchemy as sa+from alembic import op+++revision = "20260725_0035"+down_revision = "20260724_0034"+branch_labels = None+depends_on = None+++def _table_exists(name: str) -> bool:+ bind = op.get_bind()+ return bind.exec_driver_sql(+ "select 1 from sqlite_master where type = 'table' and name = ?",+ (name,),+ ).first() is not None+++def upgrade() -> None:+ if not _table_exists("resource_access_policies"):+ op.create_table(+ "resource_access_policies",+ sa.Column("resource_kind", sa.String(), nullable=False),+ sa.Column("resource_id", sa.String(), nullable=False),+ sa.Column("organization_id", sa.String(), nullable=True),+ sa.Column("owner_user_id", sa.String(), nullable=True),+ sa.Column("owner_email", sa.String(), nullable=True),+ sa.Column("access_level", sa.String(), nullable=False, server_default="private"),+ sa.Column("created_by_user_id", sa.String(), nullable=True),+ sa.Column("updated_by_user_id", sa.String(), nullable=True),+ sa.Column("policy_revision", sa.Integer(), nullable=False, server_default="0"),+ sa.Column("last_applied_control_plane_revision", sa.Integer(), nullable=True),+ sa.Column("created_at", sa.String(), nullable=False),+ sa.Column("updated_at", sa.String(), nullable=False),+ sa.CheckConstraint(+ "resource_kind in ('agent', 'vault_secret', 'skill', 'show_page')",+ name="ck_resource_access_policies_kind",+ ),+ sa.CheckConstraint(+ "access_level in ('public', 'scope', 'private')",+ name="ck_resource_access_policies_access_level",+ ),+ sa.PrimaryKeyConstraint("resource_kind", "resource_id"),+ )+ op.create_index(+ "ix_resource_access_policies_org_level",+ "resource_access_policies",+ ["organization_id", "access_level", "resource_kind"],+ if_not_exists=True,+ )+ op.create_index(+ "ix_resource_access_policies_owner",+ "resource_access_policies",+ ["owner_user_id", "resource_kind"],+ if_not_exists=True,+ )++ if not _table_exists("resource_access_groups"):+ op.create_table(+ "resource_access_groups",+ sa.Column("resource_kind", sa.String(), nullable=False),+ sa.Column("resource_id", sa.String(), nullable=False),+ sa.Column("group_id", sa.String(), nullable=False),+ sa.Column("organization_id", sa.String(), nullable=False),+ sa.Column("created_at", sa.String(), nullable=False),+ sa.ForeignKeyConstraint(+ ["resource_kind", "resource_id"],+ ["resource_access_policies.resource_kind", "resource_access_policies.resource_id"],+ ondelete="CASCADE",+ ),+ sa.PrimaryKeyConstraint("resource_kind", "resource_id", "group_id"),+ )+ op.create_index(+ "ix_resource_access_groups_group",+ "resource_access_groups",+ ["organization_id", "group_id", "resource_kind"],+ if_not_exists=True,+ )+++def downgrade() -> None:+ if _table_exists("resource_access_groups"):+ op.drop_index("ix_resource_access_groups_group", table_name="resource_access_groups", if_exists=True)+ op.drop_table("resource_access_groups")+ op.drop_index("ix_resource_access_policies_owner", table_name="resource_access_policies", if_exists=True)+ op.drop_index("ix_resource_access_policies_org_level", table_name="resource_access_policies", if_exists=True)+ if _table_exists("resource_access_policies"):+ op.drop_table("resource_access_policies")diff --git a/storage/migrations.py b/storage/migrations.pyindex 13967bea9..938b1f536 100644--- a/storage/migrations.py+++ b/storage/migrations.py@@ -25,7 +25,7 @@ logger = logging.getLogger(__name__)_MIGRATION_LOCK = threading.RLock()INITIAL_REVISION = "20260501_0001"-LATEST_SCHEMA_REVISION = "20260622_0023"+LATEST_SCHEMA_REVISION = "20260725_0035"REMOVE_LEGACY_DEFAULT_AGENT_REVISION = "20260530_0008"ALLOW_DEV_STATE_MIGRATION_ENV = "AVIBE_ALLOW_DEV_STATE_MIGRATION"INITIAL_TABLES = {@@ -58,6 +58,8 @@ HEAD_TABLES = INITIAL_TABLES | {"vault_audit","vault_auth_factors","vault_operation_challenges",+ "resource_access_policies",+ "resource_access_groups",}PRE_SHOW_SESSION_EVENTS_HEAD_TABLES = INITIAL_TABLES | {"run_definitions",@@ -110,6 +112,17 @@ HEAD_ONLY_REQUIRED_COLUMNS = {"vault_grants": {"agent_ready", "agent_ready_at"},"vault_auth_factors": {"credential_id", "public_key", "alg", "sign_count"},"vault_operation_challenges": {"challenge_hash", "rp_id", "origin", "expires_at", "consumed_at"},+ "resource_access_policies": {+ "organization_id",+ "owner_user_id",+ "owner_email",+ "access_level",+ "policy_revision",+ "last_applied_control_plane_revision",+ "created_by_user_id",+ "updated_by_user_id",+ },+ "resource_access_groups": {"organization_id", "group_id"},}UNRELEASED_OLD_INITIAL_TABLES = ["session_messages",@@ -714,6 +727,23 @@ def _ensure_vault_authz_indexes(conn: sqlite3.Connection, tables: set[str]) -> N)+def _ensure_resource_access_indexes(conn: sqlite3.Connection, tables: set[str]) -> None:+ if "resource_access_policies" in tables:+ conn.execute(+ "create index if not exists ix_resource_access_policies_org_level "+ "on resource_access_policies (organization_id, access_level, resource_kind)"+ )+ conn.execute(+ "create index if not exists ix_resource_access_policies_owner "+ "on resource_access_policies (owner_user_id, resource_kind)"+ )+ if "resource_access_groups" in tables:+ conn.execute(+ "create index if not exists ix_resource_access_groups_group "+ "on resource_access_groups (organization_id, group_id, resource_kind)"+ )++def _delete_historical_message_tool_calls(conn: sqlite3.Connection, tables: set[str]) -> None:if "messages" not in tables:return@@ -742,6 +772,7 @@ def _ensure_head_indexes(conn: sqlite3.Connection, tables: set[str]) -> None:_ensure_messages_query_indexes(conn, tables)_ensure_agent_events_indexes(conn, tables)_ensure_vault_authz_indexes(conn, tables)+ _ensure_resource_access_indexes(conn, tables)def _missing_head_schema_description(conn: sqlite3.Connection, tables: set[str]) -> str:diff --git a/storage/models.py b/storage/models.pyindex 8d7d936d2..e86a2f4be 100644--- a/storage/models.py+++ b/storage/models.py@@ -1,10 +1,12 @@from __future__ import annotationsfrom sqlalchemy import (+ CheckConstraint,Column,DDL,Float,ForeignKey,+ ForeignKeyConstraint,Index,Integer,MetaData,@@ -476,6 +478,57 @@ web_push_subscriptions = Table(Index("ix_web_push_subscriptions_user_device", "user_key", "device_id"),)+# Resource ACLs are local enforcement state. The hosted control plane receives+# only safe metadata and desired revisions; it never stores local resource+# content or secret values.+resource_access_policies = Table(+ "resource_access_policies",+ metadata,+ Column("resource_kind", String, primary_key=True),+ Column("resource_id", String, primary_key=True),+ Column("organization_id", String, nullable=True),+ Column("owner_user_id", String, nullable=True),+ Column("owner_email", String, nullable=True),+ Column("access_level", String, nullable=False, server_default=text("'private'")),+ Column("created_by_user_id", String, nullable=True),+ Column("updated_by_user_id", String, nullable=True),+ Column("policy_revision", Integer, nullable=False, server_default=text("0")),+ Column("last_applied_control_plane_revision", Integer, nullable=True),+ Column("created_at", String, nullable=False),+ Column("updated_at", String, nullable=False),+ CheckConstraint(+ "resource_kind in ('agent', 'vault_secret', 'skill', 'show_page')",+ name="ck_resource_access_policies_kind",+ ),+ CheckConstraint(+ "access_level in ('public', 'scope', 'private')",+ name="ck_resource_access_policies_access_level",+ ),+ Index(+ "ix_resource_access_policies_org_level",+ "organization_id",+ "access_level",+ "resource_kind",+ ),+ Index("ix_resource_access_policies_owner", "owner_user_id", "resource_kind"),+)++resource_access_groups = Table(+ "resource_access_groups",+ metadata,+ Column("resource_kind", String, primary_key=True),+ Column("resource_id", String, primary_key=True),+ Column("group_id", String, primary_key=True),+ Column("organization_id", String, nullable=False),+ Column("created_at", String, nullable=False),+ ForeignKeyConstraint(+ ["resource_kind", "resource_id"],+ ["resource_access_policies.resource_kind", "resource_access_policies.resource_id"],+ ondelete="CASCADE",+ ),+ Index("ix_resource_access_groups_group", "organization_id", "group_id", "resource_kind"),+)+# Vaults — secret management for agents.# A named secret is referenced by a globally-unique env-style ``name``. Tags are# the only grouping selector; skill association is stored as reserveddiff --git a/storage/resource_access_service.py b/storage/resource_access_service.pynew file mode 100644index 000000000..070574ac3--- /dev/null+++ b/storage/resource_access_service.py@@ -0,0 +1,609 @@+"""Local organization resource-policy state and evaluation helpers.++The control plane owns desired ACL intents. This module owns the local applied+policy used by future resource services, and deliberately stores no resource+content, prompts, paths, outputs, or secret values.+"""++from __future__ import annotations++from collections.abc import Mapping, Sequence+from contextlib import contextmanager+from dataclasses import dataclass+from datetime import datetime, timezone+from typing import Any, Iterator++from sqlalchemy import select+from sqlalchemy.engine import Connection++from storage.db import get_cached_sqlite_engine+from storage.models import resource_access_groups, resource_access_policies+++RESOURCE_KINDS = frozenset({"agent", "vault_secret", "skill", "show_page"})+ACCESS_LEVELS = frozenset({"public", "scope", "private"})+ORGANIZATION_ROLES = frozenset({"owner", "admin", "member"})+++class ResourceAccessError(ValueError):+ """A stable, non-sensitive policy or intent validation error."""++ def __init__(self, code: str):+ super().__init__(code)+ self.code = code+++@dataclass(frozen=True)+class ResourceUserContext:+ subject: str | None = None+ email: str | None = None+ organization_id: str | None = None+ organization_member_id: str | None = None+ organization_role: str | None = None+ group_ids: frozenset[str] | None = None+ membership_version: str | None = None+ instance_access_source: str | None = None+ is_remote: bool = False+ is_trusted_local: bool = False++ @property+ def is_active_organization_member(self) -> bool:+ return bool(+ self.organization_id+ and self.organization_member_id+ and self.organization_role in ORGANIZATION_ROLES+ )++ @property+ def is_instance_owner(self) -> bool:+ return self.instance_access_source == "owner" and bool(self.subject)+++def _utc_now_iso() -> str:+ return datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ")+++def _clean_optional_string(value: Any, *, limit: int = 200) -> str | None:+ if not isinstance(value, str):+ return None+ cleaned = value.strip()+ if not cleaned or len(cleaned) > limit or any(ord(char) < 32 or ord(char) == 127 for char in cleaned):+ return None+ return cleaned+++def _required_identifier(value: Any, *, code: str) -> str:+ cleaned = _clean_optional_string(value)+ if cleaned is None:+ raise ResourceAccessError(code)+ return cleaned+++def _validate_resource_kind(resource_kind: Any) -> str:+ if resource_kind not in RESOURCE_KINDS:+ raise ResourceAccessError("invalid_resource_kind")+ return str(resource_kind)+++def _validate_access_level(access_level: Any) -> str:+ if access_level not in ACCESS_LEVELS:+ raise ResourceAccessError("invalid_resource_acl_intent")+ return str(access_level)+++def _normalize_group_ids(value: Any) -> list[str]:+ if value is None:+ return []+ if not isinstance(value, (list, tuple, set, frozenset)):+ raise ResourceAccessError("invalid_resource_acl_intent")+ result: list[str] = []+ seen: set[str] = set()+ for item in value:+ group_id = _clean_optional_string(item)+ if group_id is None:+ raise ResourceAccessError("invalid_resource_acl_intent")+ if group_id not in seen:+ seen.add(group_id)+ result.append(group_id)+ return result+++def _validate_policy_values(access_level: Any, group_ids: Any, organization_id: str | None) -> tuple[str, list[str]]:+ normalized_level = _validate_access_level(access_level)+ normalized_groups = _normalize_group_ids(group_ids)+ if normalized_level == "scope":+ if not organization_id or not normalized_groups:+ raise ResourceAccessError("invalid_resource_acl_intent")+ elif normalized_groups:+ raise ResourceAccessError("invalid_resource_acl_intent")+ return normalized_level, normalized_groups+++def normalize_policy_request(+ access_level: Any,+ group_ids: Any,+ organization_id: str | None,+) -> tuple[str, list[str]]:+ """Validate one policy request and normalize ignored non-scope groups."""++ if access_level in {"private", "public"}:+ group_ids = []+ return _validate_policy_values(access_level, group_ids, _clean_optional_string(organization_id))+++def _context_from_mapping(+ payload: Mapping[str, Any] | None,+ *,+ is_remote: bool,+ is_trusted_local: bool,+) -> ResourceUserContext:+ data = payload or {}+ organization_id = _clean_optional_string(data.get("vibe_organization_id", data.get("organization_id")))+ raw_groups = data.get("vibe_group_ids", data.get("group_ids"))+ group_ids: frozenset[str] | None = None+ if organization_id and isinstance(raw_groups, (list, tuple, set, frozenset)):+ cleaned_groups = [_clean_optional_string(value) for value in raw_groups]+ if all(value is not None for value in cleaned_groups):+ group_ids = frozenset(value for value in cleaned_groups if value is not None)+ role = _clean_optional_string(data.get("vibe_organization_role", data.get("organization_role")))+ if role not in ORGANIZATION_ROLES:+ role = None+ return ResourceUserContext(+ subject=_clean_optional_string(data.get("sub")),+ email=_clean_optional_string(data.get("email"), limit=320),+ organization_id=organization_id,+ organization_member_id=_clean_optional_string(+ data.get("vibe_organization_member_id", data.get("organization_member_id"))+ ),+ organization_role=role,+ group_ids=group_ids,+ membership_version=_clean_optional_string(+ data.get("vibe_membership_version", data.get("membership_version"))+ ),+ instance_access_source=_clean_optional_string(+ data.get("vibe_instance_access_source", data.get("instance_access_source"))+ ),+ is_remote=is_remote,+ is_trusted_local=is_trusted_local,+ )+++def current_resource_context(+ session_payload: Mapping[str, Any] | None = None,+ *,+ is_remote: bool | None = None,+ is_trusted_local: bool | None = None,+) -> ResourceUserContext:+ """Return the request's local resource-access context.++ Callers that already parsed the signed session should pass it explicitly.+ The no-argument form is intentionally best-effort for future service-layer+ callers running inside the UI request context; outside a request it returns+ an untrusted anonymous context rather than guessing at an identity.+ """++ if session_payload is not None or is_remote is not None or is_trusted_local is not None:+ return _context_from_mapping(+ session_payload,+ is_remote=bool(is_remote if is_remote is not None else session_payload is not None),+ is_trusted_local=bool(is_trusted_local),+ )++ try:+ from vibe import remote_access, ui_server++ config = ui_server._load_remote_access_config()+ remote_request = bool(+ config is not None+ and ui_server._is_remote_access_request(config)+ and not ui_server._is_local_request(config)+ )+ if remote_request and config is not None:+ payload = remote_access.parse_session_cookie(+ config,+ ui_server.request.cookies.get(remote_access.SESSION_COOKIE_NAME),+ )+ return _context_from_mapping(payload, is_remote=True, is_trusted_local=False)+ return _context_from_mapping(+ None,+ is_remote=False,+ is_trusted_local=bool(ui_server._is_local_request(config)),+ )+ except Exception:+ return ResourceUserContext()+++def _as_context(user_context: ResourceUserContext | Mapping[str, Any] | None) -> ResourceUserContext:+ if isinstance(user_context, ResourceUserContext):+ return user_context+ if isinstance(user_context, Mapping):+ return _context_from_mapping(user_context, is_remote=True, is_trusted_local=False)+ return ResourceUserContext()+++@contextmanager+def _connection(connection: Connection | None) -> Iterator[Connection]:+ if connection is not None:+ yield connection+ return+ engine = get_cached_sqlite_engine()+ with engine.connect() as active_connection:+ yield active_connection+++def _policy_row(connection: Connection, resource_kind: str, resource_id: str) -> dict[str, Any] | None:+ row = connection.execute(+ select(resource_access_policies)+ .where(resource_access_policies.c.resource_kind == resource_kind)+ .where(resource_access_policies.c.resource_id == resource_id)+ .limit(1)+ ).mappings().first()+ return dict(row) if row else None+++def _policy_groups(connection: Connection, resource_kind: str, resource_id: str) -> list[str]:+ return [+ str(row["group_id"])+ for row in connection.execute(+ select(resource_access_groups.c.group_id)+ .where(resource_access_groups.c.resource_kind == resource_kind)+ .where(resource_access_groups.c.resource_id == resource_id)+ .order_by(resource_access_groups.c.group_id)+ ).mappings()+ ]+++def _serialize_policy(connection: Connection, policy: Mapping[str, Any]) -> dict[str, Any]:+ data = dict(policy)+ data["group_ids"] = _policy_groups(connection, str(data["resource_kind"]), str(data["resource_id"]))+ return data+++def get_resource_policy(+ resource_kind: str,+ resource_id: str,+ *,+ connection: Connection | None = None,+) -> dict[str, Any] | None:+ kind = _validate_resource_kind(resource_kind)+ identifier = _required_identifier(resource_id, code="invalid_resource_id")+ with _connection(connection) as conn:+ policy = _policy_row(conn, kind, identifier)+ return _serialize_policy(conn, policy) if policy else None+++def list_resource_policies(+ *,+ resource_kind: str | None = None,+ organization_id: str | None = None,+ owner_user_id: str | None = None,+ connection: Connection | None = None,+) -> list[dict[str, Any]]:+ kind = _validate_resource_kind(resource_kind) if resource_kind is not None else None+ organization = _clean_optional_string(organization_id) if organization_id is not None else None+ owner = _clean_optional_string(owner_user_id) if owner_user_id is not None else None+ statement = select(resource_access_policies).order_by(+ resource_access_policies.c.resource_kind,+ resource_access_policies.c.resource_id,+ )+ if kind is not None:+ statement = statement.where(resource_access_policies.c.resource_kind == kind)+ if organization is not None:+ statement = statement.where(resource_access_policies.c.organization_id == organization)+ if owner is not None:+ statement = statement.where(resource_access_policies.c.owner_user_id == owner)+ with _connection(connection) as conn:+ return [_serialize_policy(conn, row) for row in conn.execute(statement).mappings()]+++def ensure_resource_policy(+ connection: Connection,+ *,+ resource_kind: str,+ resource_id: str,+ organization_id: str | None,+ owner_user_id: str | None,+ owner_email: str | None = None,+ access_level: str = "private",+ group_ids: Sequence[str] | None = None,+ created_by_user_id: str | None = None,+ updated_by_user_id: str | None = None,+ policy_revision: int = 0,+ last_applied_control_plane_revision: int | None = None,+) -> dict[str, Any]:+ """Create a resource policy if it does not already exist.++ Resource-specific services should use this at creation time. It never+ overwrites an existing policy, which prevents a late resource registration+ from replacing a control-plane intent already applied locally.+ """++ kind = _validate_resource_kind(resource_kind)+ identifier = _required_identifier(resource_id, code="invalid_resource_id")+ organization = _clean_optional_string(organization_id)+ owner = _clean_optional_string(owner_user_id)+ normalized_level, normalized_groups = _validate_policy_values(access_level, group_ids, organization)+ if policy_revision < 0 or last_applied_control_plane_revision is not None and last_applied_control_plane_revision < 0:+ raise ResourceAccessError("invalid_resource_acl_intent")+ existing = _policy_row(connection, kind, identifier)+ if existing:+ return _serialize_policy(connection, existing)+ now = _utc_now_iso()+ connection.execute(+ resource_access_policies.insert().values(+ resource_kind=kind,+ resource_id=identifier,+ organization_id=organization,+ owner_user_id=owner,+ owner_email=_clean_optional_string(owner_email, limit=320),+ access_level=normalized_level,+ created_by_user_id=_clean_optional_string(created_by_user_id),+ updated_by_user_id=_clean_optional_string(updated_by_user_id),+ policy_revision=policy_revision,+ last_applied_control_plane_revision=last_applied_control_plane_revision,+ created_at=now,+ updated_at=now,+ )+ )+ _replace_policy_groups(connection, kind, identifier, organization, normalized_groups, now)+ policy = _policy_row(connection, kind, identifier)+ assert policy is not None+ return _serialize_policy(connection, policy)+++def _replace_policy_groups(+ connection: Connection,+ resource_kind: str,+ resource_id: str,+ organization_id: str | None,+ group_ids: Sequence[str],+ now: str,+) -> None:+ connection.execute(+ resource_access_groups.delete()+ .where(resource_access_groups.c.resource_kind == resource_kind)+ .where(resource_access_groups.c.resource_id == resource_id)+ )+ if not group_ids:+ return+ if organization_id is None:+ raise ResourceAccessError("invalid_resource_acl_intent")+ connection.execute(+ resource_access_groups.insert(),+ [+ {+ "resource_kind": resource_kind,+ "resource_id": resource_id,+ "group_id": group_id,+ "organization_id": organization_id,+ "created_at": now,+ }+ for group_id in group_ids+ ],+ )+++def _policy_allows(context: ResourceUserContext, policy: Mapping[str, Any] | None, group_ids: Sequence[str]) -> bool:+ if context.is_trusted_local:+ return True+ if policy is None:+ # A legacy no-policy resource is local-private. The paired instance's+ # owner retains legacy access, while remote organization members do not.+ return context.is_instance_owner++ owner_user_id = _clean_optional_string(policy.get("owner_user_id"))+ if policy.get("access_level") == "private":+ return bool(owner_user_id and context.subject and owner_user_id == context.subject)++ organization_id = _clean_optional_string(policy.get("organization_id"))+ if not organization_id or context.organization_id != organization_id or not context.is_active_organization_member:+ return False+ if policy.get("access_level") == "public":+ return True+ if policy.get("access_level") == "scope":+ # Missing and empty are intentionally distinct: a missing group claim+ # must fail closed, and an active member with no matching group also+ # cannot use a scoped resource.+ return bool(context.group_ids and set(group_ids).intersection(context.group_ids))+ return False+++def can_use_resource(+ user_context: ResourceUserContext | Mapping[str, Any] | None,+ resource_kind: str,+ resource_id: str,+ *,+ connection: Connection | None = None,+) -> bool:+ context = _as_context(user_context)+ kind = _validate_resource_kind(resource_kind)+ identifier = _required_identifier(resource_id, code="invalid_resource_id")+ with _connection(connection) as conn:+ policy = _policy_row(conn, kind, identifier)+ groups = _policy_groups(conn, kind, identifier) if policy else []+ return _policy_allows(context, policy, groups)+++def can_manage_resource_acl(+ user_context: ResourceUserContext | Mapping[str, Any] | None,+ resource_kind: str,+ resource_id: str,+ *,+ connection: Connection | None = None,+) -> bool:+ context = _as_context(user_context)+ kind = _validate_resource_kind(resource_kind)+ identifier = _required_identifier(resource_id, code="invalid_resource_id")+ if context.is_trusted_local:+ return True+ with _connection(connection) as conn:+ policy = _policy_row(conn, kind, identifier)+ if policy is None:+ return context.is_instance_owner+ owner_user_id = _clean_optional_string(policy.get("owner_user_id"))+ if owner_user_id and context.subject and owner_user_id == context.subject:+ return True+ return bool(+ context.is_active_organization_member+ and context.organization_id == _clean_optional_string(policy.get("organization_id"))+ and context.organization_role in {"owner", "admin"}+ )+++def _row_resource_id(row: Any) -> str | None:+ keys = ("resource_id", "id", "session_id", "name", "key")+ if isinstance(row, Mapping):+ for key in keys:+ value = _clean_optional_string(row.get(key))+ if value is not None:+ return value+ return None+ for key in keys:+ value = _clean_optional_string(getattr(row, key, None))+ if value is not None:+ return value+ return None+++def filter_accessible_resources(+ user_context: ResourceUserContext | Mapping[str, Any] | None,+ resource_kind: str,+ rows: Sequence[Any],+ *,+ connection: Connection | None = None,+) -> list[Any]:+ """Filter generic resource rows without coupling to any resource domain.++ Rows may be mappings or objects and should expose one of `resource_id`,+ `id`, `session_id`, `name`, or `key`. Unknown rows are excluded rather than+ accidentally exposed.+ """++ context = _as_context(user_context)+ kind = _validate_resource_kind(resource_kind)+ candidates = [(row, _row_resource_id(row)) for row in rows]+ identifiers = [identifier for _, identifier in candidates if identifier is not None]+ if not identifiers:+ return []+ with _connection(connection) as conn:+ policy_rows = conn.execute(+ select(resource_access_policies)+ .where(resource_access_policies.c.resource_kind == kind)+ .where(resource_access_policies.c.resource_id.in_(identifiers))+ ).mappings()+ policies = {str(row["resource_id"]): dict(row) for row in policy_rows}+ groups = {+ identifier: _policy_groups(conn, kind, identifier)+ for identifier in policies+ }+ return [+ row+ for row, identifier in candidates+ if identifier is not None and _policy_allows(context, policies.get(identifier), groups.get(identifier, []))+ ]+++def apply_control_plane_intent(+ connection: Connection,+ *,+ organization_id: str,+ resource_kind: str,+ resource_id: str,+ revision: int,+ access_level: str,+ group_ids: Sequence[str],+ updated_by_user_id: str = "control_plane",+) -> dict[str, Any]:+ """Atomically apply a newer hosted ACL intent to one local policy.++ The caller owns the surrounding transaction. A stale or already-applied+ revision never rewrites the current policy, which makes retrying a device+ poll safe.+ """++ organization = _required_identifier(organization_id, code="invalid_organization_id")+ kind = _validate_resource_kind(resource_kind)+ identifier = _required_identifier(resource_id, code="invalid_resource_id")+ if not isinstance(revision, int) or isinstance(revision, bool) or revision < 0:+ raise ResourceAccessError("invalid_resource_acl_intent")+ normalized_level, normalized_groups = _validate_policy_values(access_level, group_ids, organization)+ policy = _policy_row(connection, kind, identifier)+ if policy is None:+ raise ResourceAccessError("resource_not_found")+ if _clean_optional_string(policy.get("organization_id")) != organization:+ raise ResourceAccessError("resource_organization_mismatch")++ last_applied = policy.get("last_applied_control_plane_revision")+ last_revision = int(last_applied) if isinstance(last_applied, int) else -1+ policy_revision = int(policy.get("policy_revision") or 0)+ if revision < last_revision or revision < policy_revision:+ return {"status": "stale", "policy": _serialize_policy(connection, policy)}+ if revision == last_revision:+ return {"status": "already_applied", "policy": _serialize_policy(connection, policy)}++ if normalized_groups:+ conflicting_group = connection.execute(+ select(resource_access_groups.c.group_id)+ .where(resource_access_groups.c.group_id.in_(normalized_groups))+ .where(resource_access_groups.c.organization_id != organization)+ .limit(1)+ ).first()+ if conflicting_group is not None:+ raise ResourceAccessError("resource_group_organization_mismatch")++ now = _utc_now_iso()+ connection.execute(+ resource_access_policies.update()+ .where(resource_access_policies.c.resource_kind == kind)+ .where(resource_access_policies.c.resource_id == identifier)+ .values(+ access_level=normalized_level,+ policy_revision=revision,+ last_applied_control_plane_revision=revision,+ updated_by_user_id=_clean_optional_string(updated_by_user_id) or "control_plane",+ updated_at=now,+ )+ )+ _replace_policy_groups(connection, kind, identifier, organization, normalized_groups, now)+ updated = _policy_row(connection, kind, identifier)+ assert updated is not None+ return {"status": "applied", "policy": _serialize_policy(connection, updated)}+++def update_local_non_organization_policy(+ connection: Connection,+ *,+ resource_kind: str,+ resource_id: str,+ access_level: str,+ group_ids: Sequence[str] | None,+ updated_by_user_id: str | None,+) -> dict[str, Any]:+ """Update a personal/local policy without touching control-plane revisions."""++ kind = _validate_resource_kind(resource_kind)+ identifier = _required_identifier(resource_id, code="invalid_resource_id")+ policy = _policy_row(connection, kind, identifier)+ if policy is None:+ raise ResourceAccessError("resource_not_found")+ organization = _clean_optional_string(policy.get("organization_id"))+ if organization is not None:+ raise ResourceAccessError("resource_acl_control_plane_required")+ normalized_level, normalized_groups = _validate_policy_values(access_level, group_ids, None)+ now = _utc_now_iso()+ next_revision = int(policy.get("policy_revision") or 0) + 1+ connection.execute(+ resource_access_policies.update()+ .where(resource_access_policies.c.resource_kind == kind)+ .where(resource_access_policies.c.resource_id == identifier)+ .values(+ access_level=normalized_level,+ policy_revision=next_revision,+ updated_by_user_id=_clean_optional_string(updated_by_user_id),+ updated_at=now,+ )+ )+ _replace_policy_groups(connection, kind, identifier, None, normalized_groups, now)+ updated = _policy_row(connection, kind, identifier)+ assert updated is not None+ return _serialize_policy(connection, updated)diff --git a/tests/test_resource_access_service.py b/tests/test_resource_access_service.pynew file mode 100644index 000000000..054811a6b--- /dev/null+++ b/tests/test_resource_access_service.py@@ -0,0 +1,106 @@+from __future__ import annotations++from storage import resource_access_service+from storage.db import create_sqlite_engine+from storage.migrations import run_migrations+++def _context(+ subject: str,+ *,+ organization_id: str | None = "org-1",+ group_ids: frozenset[str] | None = frozenset(),+ role: str | None = "member",+ access_source: str = "organization_group",+) -> resource_access_service.ResourceUserContext:+ return resource_access_service.ResourceUserContext(+ subject=subject,+ organization_id=organization_id,+ organization_member_id="member-1" if organization_id else None,+ organization_role=role,+ group_ids=group_ids,+ instance_access_source=access_source,+ is_remote=True,+ )+++def _seed_policies(connection) -> None:+ resource_access_service.ensure_resource_policy(+ connection,+ resource_kind="agent",+ resource_id="private-agent",+ organization_id="org-1",+ owner_user_id="owner-1",+ access_level="private",+ )+ resource_access_service.ensure_resource_policy(+ connection,+ resource_kind="agent",+ resource_id="public-agent",+ organization_id="org-1",+ owner_user_id="owner-1",+ access_level="public",+ )+ resource_access_service.ensure_resource_policy(+ connection,+ resource_kind="agent",+ resource_id="scoped-agent",+ organization_id="org-1",+ owner_user_id="owner-1",+ access_level="scope",+ group_ids=["group-engineering"],+ )+++def test_policy_evaluation_private_public_scope_and_missing_group_context(tmp_path) -> None:+ db = tmp_path / "vibe.sqlite"+ run_migrations(db)+ engine = create_sqlite_engine(db)+ try:+ with engine.begin() as connection:+ _seed_policies(connection)++ owner = _context("owner-1")+ engineering_member = _context("member-2", group_ids=frozenset({"group-engineering"}))+ member_without_groups = _context("member-3", group_ids=None)+ member_other_group = _context("member-4", group_ids=frozenset({"group-sales"}))+ outside_org = _context("member-5", organization_id="org-2", group_ids=frozenset({"group-engineering"}))++ assert resource_access_service.can_use_resource(owner, "agent", "private-agent", connection=connection)+ assert not resource_access_service.can_use_resource(+ engineering_member, "agent", "private-agent", connection=connection+ )++ assert resource_access_service.can_use_resource(+ engineering_member, "agent", "public-agent", connection=connection+ )+ assert not resource_access_service.can_use_resource(outside_org, "agent", "public-agent", connection=connection)++ assert resource_access_service.can_use_resource(+ engineering_member, "agent", "scoped-agent", connection=connection+ )+ assert not resource_access_service.can_use_resource(+ member_without_groups, "agent", "scoped-agent", connection=connection+ )+ assert not resource_access_service.can_use_resource(+ member_other_group, "agent", "scoped-agent", connection=connection+ )+ finally:+ engine.dispose()+++def test_no_policy_is_local_private_but_instance_owner_keeps_legacy_access(tmp_path) -> None:+ db = tmp_path / "vibe.sqlite"+ run_migrations(db)+ engine = create_sqlite_engine(db)+ try:+ with engine.connect() as connection:+ member = _context("member-1", group_ids=frozenset({"group-engineering"}))+ owner = _context("owner-1", access_source="owner")+ local = resource_access_service.ResourceUserContext(is_trusted_local=True)++ assert not resource_access_service.can_use_resource(member, "agent", "legacy-agent", connection=connection)+ assert resource_access_service.can_use_resource(owner, "agent", "legacy-agent", connection=connection)+ assert resource_access_service.can_use_resource(local, "agent", "legacy-agent", connection=connection)+ finally:+ engine.dispose()diff --git a/tests/test_sqlite_state_migration.py b/tests/test_sqlite_state_migration.pyindex e021cc304..e9ad8321e 100644--- a/tests/test_sqlite_state_migration.py+++ b/tests/test_sqlite_state_migration.py@@ -19,7 +19,7 @@ from storage.models import metadatafrom storage.settings_service import SQLiteSettingsService-HEAD_REVISION = "20260724_0034"+HEAD_REVISION = "20260725_0035"def _index_sql(conn: sqlite3.Connection, name: str) -> str:@@ -54,6 +54,8 @@ def test_run_migrations_creates_initial_schema(tmp_path: Path) -> None:assert "web_push_subscriptions" in tablesassert "vault_auth_factors" in tablesassert "vault_operation_challenges" in tables+ assert "resource_access_policies" in tables+ assert "resource_access_groups" in tablesagent_event_indexes = {row[1]for row in conn.execute(@@ -94,6 +96,18 @@ def test_run_migrations_creates_initial_schema(tmp_path: Path) -> None:"select seq, name from pragma_index_list('vault_operation_challenges')",)}+ resource_policy_indexes = {+ row[1]+ for row in conn.execute(+ "select seq, name from pragma_index_list('resource_access_policies')",+ )+ }+ resource_group_indexes = {+ row[1]+ for row in conn.execute(+ "select seq, name from pragma_index_list('resource_access_groups')",+ )+ }assert "ix_messages_session_created_id" in message_indexesassert "ix_messages_session_type_created_id" in message_indexesassert "ix_messages_platform_session_created_id" in message_indexes@@ -109,6 +123,9 @@ def test_run_migrations_creates_initial_schema(tmp_path: Path) -> None:assert "ix_vault_auth_factors_kind_rp" in vault_auth_factor_indexesassert "ix_vault_operation_challenges_lookup" in vault_challenge_indexesassert "ix_vault_operation_challenges_consumed" in vault_challenge_indexes+ assert "ix_resource_access_policies_org_level" in resource_policy_indexes+ assert "ix_resource_access_policies_owner" in resource_policy_indexes+ assert "ix_resource_access_groups_group" in resource_group_indexesassert "trg_vault_requests_pending_provision_name_case_insert" in vault_request_triggersassert "trg_vault_requests_pending_provision_name_case_update" in vault_request_triggersagent_session_indexes = {