Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion api/AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ RBAC / access enforcement:

Project membership and access:

- Membership is decided only by `MembershipsService.resolve` (`oss/src/core/memberships/`).
- Membership is decided only by `MembershipsService.resolve` (`oss/src/core/memberships/`) and its uncached form `read_membership` (same policy and DAO read; use it only where another cache keeps the outcome, today the Bearer workspace-only and no-parameter branches). No other membership reader or policy.
OSS: every organization member is a member of every project, with the organization role.
EE: a member needs an organization row and a `project_members` row; the project row's
role applies. The edition policy is picked only in `entrypoints/access.py`.
Expand Down
10 changes: 6 additions & 4 deletions api/ee/src/core/meters/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ class Meters(str, Enum):
class MeterScope(BaseModel):
"""Scope dimensions for a meter row.

`organization_id` is the tenant root — when set, every finer dimension
(`workspace -> project -> user`) requires the next one above it.
`organization_id` is the tenant root. A user requires a project, a project requires
the organization, and a workspace (legacy, optional) requires the organization.
`None` on any dimension means "this dimension does not apply" — never
"default".
"""
Expand All @@ -50,8 +50,10 @@ class MeterScope(BaseModel):
def _validate_hierarchy(self) -> "MeterScope":
if self.user_id is not None and self.project_id is None:
raise ValueError("user_id requires project_id")
if self.project_id is not None and self.workspace_id is None:
raise ValueError("project_id requires workspace_id")
# A project needs its organization, not a workspace: rows written after
# workspace removal step 2 carry none.
if self.project_id is not None and self.organization_id is None:
raise ValueError("project_id requires organization_id")
if self.workspace_id is not None and self.organization_id is None:
raise ValueError("workspace_id requires organization_id")

Expand Down
6 changes: 3 additions & 3 deletions api/ee/tests/pytest/unit/test_compute_meter_id.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,9 @@ def test_meter_scope_user_without_project_raises():
MeterScope(organization_id=ORG, workspace_id=WS, user_id=USR)


def test_meter_scope_project_without_workspace_raises():
with pytest.raises(ValueError, match="project_id requires workspace_id"):
MeterScope(organization_id=ORG, project_id=PRJ)
def test_meter_scope_project_without_organization_raises():
with pytest.raises(ValueError, match="project_id requires organization_id"):
MeterScope(project_id=PRJ)


def test_meter_scope_workspace_without_organization_raises():
Expand Down
88 changes: 88 additions & 0 deletions api/ee/tests/pytest/unit/test_meter_scope_switch_rows.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
"""Meter rows without a workspace (written and re-keyed by workspace removal step 2).

A step-1 pod must read them: the usage report converts every row it reports, and a row
that fails conversion is skipped and never synced, which stalls the report run during a
step-2 rollout or rollback. Their `meter_id` is the step-2 id: `compute_meter_id` omits
the missing workspace part.
"""

from types import SimpleNamespace
from uuid import UUID, uuid5

from ee.src.core.access.entitlements.types import Counter
from ee.src.core.meters.types import (
AGENTA_METERS_NAMESPACE_UUID,
MeterPeriod,
MeterScope,
compute_meter_id,
)
from ee.src.dbs.postgres.meters.dao import _dbe_to_dto

ORG = UUID("11111111-1111-1111-1111-111111111111")
WS = UUID("22222222-2222-2222-2222-222222222222")
PRJ = UUID("33333333-3333-3333-3333-333333333333")
USR = UUID("44444444-4444-4444-4444-444444444444")
PERIOD = MeterPeriod(year=2026, month=10, day=25)


def test_meter_scope_accepts_a_project_without_a_workspace():
scope = MeterScope(organization_id=ORG, project_id=PRJ, user_id=USR)
assert scope.workspace_id is None


def test_compute_meter_id_of_a_switch_row_equals_the_step2_id():
# Frozen copy of the step-2 canonical string: no `wrk` part.
step2 = uuid5(
AGENTA_METERS_NAMESPACE_UUID,
f"d=25|key=traces_retrieved|m=10|org={ORG}|prj={PRJ}|usr={USR}|y=2026",
)
assert (
compute_meter_id(
scope=MeterScope(organization_id=ORG, project_id=PRJ, user_id=USR),
period=PERIOD,
key=Counter.TRACES_RETRIEVED,
)
== step2
)


def test_existing_meter_ids_are_unchanged():
legacy = uuid5(
AGENTA_METERS_NAMESPACE_UUID,
f"d=25|key=traces_retrieved|m=10|org={ORG}|prj={PRJ}|usr={USR}|wrk={WS}|y=2026",
)
assert (
compute_meter_id(
scope=MeterScope(
organization_id=ORG, workspace_id=WS, project_id=PRJ, user_id=USR
),
period=PERIOD,
key=Counter.TRACES_RETRIEVED,
)
== legacy
)


def test_dump_converts_a_row_without_a_workspace():
stored = compute_meter_id(
scope=MeterScope(organization_id=ORG, project_id=PRJ, user_id=USR),
period=PERIOD,
key=Counter.TRACES_RETRIEVED,
)
row = SimpleNamespace(
organization_id=ORG,
workspace_id=None,
project_id=PRJ,
user_id=USR,
year=2026,
month=10,
day=25,
key=Counter.TRACES_RETRIEVED,
value=5,
synced=3,
meter_id=stored,
subscription=None,
)
dto = _dbe_to_dto(row)
assert dto.meter_id == stored
assert dto.workspace_id is None
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
"""drop the workspace cascade from projects and allow projects without a workspace

Step 1 of workspace removal. Projects stop depending on `workspaces`: every foreign key
from `projects.workspace_id` to `workspaces` goes (found by its definition, not by its
name, so a deployment whose constraint has another name is covered too), and the column
becomes nullable so the next step can stop writing it. Code of this release still writes
a workspace for every project; it also serves projects without one, which the next step
writes. A workspace row deleted directly in the database no longer deletes its projects;
the admin deletes now delete them explicitly.

The statements change only the catalog, but they take an ACCESS EXCLUSIVE lock on
`projects` and `workspaces`. The short lock timeout bounds how long requests queue behind
a waiting lock request; on a timeout the hook fails cleanly and the deploy is retried.
The migration gate reports one warning (`drop_constraint`) and passes.

Revision ID: oss000000052
Revises: oss000000051
Create Date: 2026-10-24 00:00:00.000000

"""

from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

revision: str = "oss000000052"
down_revision: Union[str, None] = "oss000000051"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None

_FIND_WORKSPACE_FOREIGN_KEYS_SQL = """
SELECT c.conname
FROM pg_constraint c
JOIN pg_attribute a ON a.attrelid = c.conrelid AND a.attnum = ANY (c.conkey)
WHERE c.contype = 'f'
AND c.conrelid = 'projects'::regclass
AND c.confrelid = 'workspaces'::regclass
AND a.attname = 'workspace_id'
"""


def upgrade() -> None:
op.execute("SET LOCAL lock_timeout = '2s'")

names = [
row.conname
for row in op.get_bind().execute(sa.text(_FIND_WORKSPACE_FOREIGN_KEYS_SQL))
]
for name in names:
op.drop_constraint(name, "projects", type_="foreignkey")

Check warning on line 52 in api/oss/databases/postgres/migrations/core_oss/versions/oss000000052_drop_projects_workspace_cascade.py

View workflow job for this annotation

GitHub Actions / Migration gate

Migration gate: drop_constraint

running code may rely on this constraint, for example in an upsert

Check warning on line 52 in api/oss/databases/postgres/migrations/core_oss/versions/oss000000052_drop_projects_workspace_cascade.py

View workflow job for this annotation

GitHub Actions / Migration gate

Migration gate: drop_constraint

running code may rely on this constraint, for example in an upsert

Check warning on line 52 in api/oss/databases/postgres/migrations/core_oss/versions/oss000000052_drop_projects_workspace_cascade.py

View workflow job for this annotation

GitHub Actions / Migration gate

Migration gate: drop_constraint

running code may rely on this constraint, for example in an upsert

op.alter_column(
"projects",
"workspace_id",
existing_type=postgresql.UUID(as_uuid=True),
nullable=True,
)


def downgrade() -> None:
"""Fails when projects without a workspace exist (written by the next step): correct,
because such rows cannot satisfy the old constraint."""
op.execute("SET LOCAL lock_timeout = '2s'")

op.alter_column(
"projects",
"workspace_id",
existing_type=postgresql.UUID(as_uuid=True),
nullable=False,
)
op.create_foreign_key(
"projects_workspace_id_fkey",
"projects",
"workspaces",
["workspace_id"],
["id"],
ondelete="CASCADE",
)
6 changes: 5 additions & 1 deletion api/oss/src/apis/fastapi/access/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,11 @@ async def _check_scope_access(
if scope_type == "project":
allow_scope = str(auth_scope.project_id) == str(scope_id)
elif scope_type == "workspace":
allow_scope = str(auth_scope.workspace_id) == str(scope_id)
# A workspace id, or the organization id where a workspace id is expected.
allow_scope = str(scope_id) in {
str(auth_scope.workspace_id),
str(auth_scope.organization_id),
}
elif not scope_type and not scope_id:
allow_scope = True

Expand Down
7 changes: 6 additions & 1 deletion api/oss/src/core/accounts/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from oss.src.utils.common import is_ee
from oss.src.utils.logging import get_module_logger
from oss.src.core.memberships.service import invalidate_access_caches
from oss.src.core.memberships.workspaces import legacy_workspace_id
from oss.src.core.access.dtos import PrincipalKind, PrincipalRef
from oss.src.core.access.service import (
invalidate_principals_after_write,
Expand Down Expand Up @@ -278,7 +279,11 @@ def _proj_db_to_read_dto(proj: ProjectDB) -> AdminProjectRead:
id=str(proj.id),
name=proj.project_name or "",
organization_id=str(proj.organization_id),
workspace_id=str(proj.workspace_id),
workspace_id=str(
legacy_workspace_id(
workspace_id=proj.workspace_id, organization_id=proj.organization_id
)
),
is_default=proj.is_default,
created_at=proj.created_at.isoformat() if proj.created_at else None,
updated_at=proj.updated_at.isoformat() if proj.updated_at else None,
Expand Down
15 changes: 15 additions & 0 deletions api/oss/src/core/memberships/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,21 @@ async def resolve(
)
return membership

async def read_membership(
self,
*,
user_id: UUID,
project_id: UUID,
) -> Optional[ProjectMembership]:
"""The membership `resolve` answers, read without the `resolve_membership` cache.

Same edition policy and the same one-statement read. Only for a caller whose
outcome another cache already keeps (the Bearer workspace-only and no-parameter
branches cache their state), so it adds no Redis call.
"""

return await self._read_membership(user_id=user_id, project_id=project_id)

async def is_member(
self,
*,
Expand Down
19 changes: 19 additions & 0 deletions api/oss/src/core/memberships/workspaces.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
"""The workspace id the API answers for a project, until the classic app is retired.

Projects are walls inside an organization; workspaces are being removed (CONTRACT section
1). Until then every response, token and link still carries a `workspace_id`. A project
that has a workspace answers it; a project without one (written by workspace removal step
2) answers its organization id, which every reader accepts wherever a workspace id goes.
"""

from typing import Optional
from uuid import UUID


def legacy_workspace_id(
*,
workspace_id: Optional[UUID],
organization_id: UUID,
) -> UUID:
"""The project's workspace id, or its organization id when it has none."""
return workspace_id or organization_id
8 changes: 7 additions & 1 deletion api/oss/src/core/workflows/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from oss.src.core.access.dtos import RunEnvelope
from oss.src.core.access.service import get_access_service
from oss.src.core.access.types import MissingPrincipal
from oss.src.core.memberships.workspaces import legacy_workspace_id
from oss.src.core.git.dtos import RevisionGrouping
from urllib.parse import urlparse
from uuid import UUID, uuid4
Expand Down Expand Up @@ -3605,7 +3606,12 @@ async def _prepare_invoke(
secret_token = await sign_secret_token(
user_id=str(acting_user_id),
project_id=str(project_id),
workspace_id=str(project.workspace_id),
workspace_id=str(
legacy_workspace_id(
workspace_id=project.workspace_id,
organization_id=project.organization_id,
)
),
organization_id=str(project.organization_id),
gateway_run_id=gateway_run_id,
grants=self._service_grants(service_url),
Expand Down
6 changes: 6 additions & 0 deletions api/oss/src/dbs/postgres/access/dao.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ def _cache_key(principal: PrincipalRef) -> dict:
WHERE p.id = ANY(CAST(:projects AS uuid[]))
OR p.organization_id = ANY(CAST(:organizations AS uuid[]))
OR p.workspace_id = ANY(CAST(:workspaces AS uuid[]))
-- A legacy project can sit in a workspace of a deleted organization; deletes of
-- that organization remove it too, so the snapshot covers it.
OR p.workspace_id IN (
SELECT w.id FROM workspaces w
WHERE w.organization_id = ANY(CAST(:organizations AS uuid[]))
)
)
SELECT
ARRAY(SELECT id FROM scoped) AS project_ids,
Expand Down
Loading
Loading