Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.
Python agentagentic-aiagentic-frameworkagentic-workflowaiautomationclaudedeepseekgenaigptllmlow-codemcpnextjsno-codeopenaipythonskillsworkflow
fix(ops): bind tracing service accounts to the app workspace (#44208)
Liu Zimingebb270613908
diff --git a/api/core/ops/base_trace_instance.py b/api/core/ops/base_trace_instance.pyindex c0095858d46..d0f27ad0c5f 100644--- a/api/core/ops/base_trace_instance.py+++ b/api/core/ops/base_trace_instance.py@@ -6,7 +6,7 @@ from sqlalchemy.orm import Sessionfrom core.ops.entities.config_entity import BaseTracingConfigfrom core.ops.entities.trace_entity import BaseTraceInfofrom extensions.ext_database import db-from models import Account, App, TenantAccountJoin+from models import Account, Appclass BaseTraceInstance(ABC):@@ -31,16 +31,19 @@ class BaseTraceInstance(ABC):def get_service_account_with_tenant(self, app_id: str) -> Account:"""- Get service account for an app and set up its tenant.+ Get the app creator with the app's tenant and membership role.++ Trace tasks may run after the creator switches workspaces. Bind only the+ returned account's context without changing the creator's current workspace.Args:app_id: The ID of the appReturns:- Account: The service account with tenant set up+ Account: The service account with the app's tenant set upRaises:- ValueError: If app, creator account or tenant cannot be found+ ValueError: If the app, creator account, app tenant or membership cannot be found"""with Session(db.engine, expire_on_commit=False) as session:# Get the app to find its creator@@ -56,13 +59,8 @@ class BaseTraceInstance(ABC):if not service_account:raise ValueError(f"Creator account with id {app.created_by} not found for app {app_id}")- current_tenant = session.scalar(- select(TenantAccountJoin)- .where(TenantAccountJoin.account_id == service_account.id, TenantAccountJoin.current.is_(True))- .limit(1)- )- if not current_tenant:- raise ValueError(f"Current tenant not found for account {service_account.id}")- service_account.set_tenant_id_with_session(current_tenant.tenant_id, session=session)+ service_account.set_tenant_id_with_session(app.tenant_id, session=session)+ if service_account.current_tenant_id != app.tenant_id:+ raise ValueError(f"App tenant {app.tenant_id} not found for account {service_account.id}")return service_accountdiff --git a/api/tests/unit_tests/core/ops/test_base_trace_instance.py b/api/tests/unit_tests/core/ops/test_base_trace_instance.pyindex c64b8ce27c1..c09883aded0 100644--- a/api/tests/unit_tests/core/ops/test_base_trace_instance.py+++ b/api/tests/unit_tests/core/ops/test_base_trace_instance.py@@ -1,7 +1,7 @@from types import SimpleNamespaceimport pytest-from sqlalchemy import Engine+from sqlalchemy import Engine, selectfrom sqlalchemy.orm import Sessionfrom core.ops.base_trace_instance import BaseTraceInstance@@ -90,12 +90,13 @@ def test_get_service_account_with_tenant_tenant_not_found(sqlite_session: Sessio_persist_app(sqlite_session, created_by="creator_id")_persist_account(sqlite_session)- with pytest.raises(ValueError, match="Current tenant not found for account creator_id"):+ with pytest.raises(ValueError, match="App tenant tenant_id not found for account creator_id"):_trace_instance().get_service_account_with_tenant("some_app_id")[email protected]("current", [True, False])@pytest.mark.parametrize("sqlite_session", [TABLES], indirect=True)-def test_get_service_account_with_tenant_success(sqlite_session: Session):+def test_get_service_account_with_tenant_success(sqlite_session: Session, current: bool):_persist_app(sqlite_session, created_by="creator_id")_persist_account(sqlite_session)tenant = Tenant(name="Workspace")@@ -106,7 +107,7 @@ def test_get_service_account_with_tenant_success(sqlite_session: Session):TenantAccountJoin(tenant_id=tenant.id,account_id="creator_id",- current=True,+ current=current,role=TenantAccountRole.OWNER,),]@@ -118,3 +119,90 @@ def test_get_service_account_with_tenant_success(sqlite_session: Session):assert result.id == "creator_id"assert result.current_tenant_id == "tenant_id"assert result.current_role == TenantAccountRole.OWNER++[email protected]("sqlite_session", [TABLES], indirect=True)+def test_get_service_account_with_tenant_uses_app_workspace(sqlite_session: Session):+ _persist_app(sqlite_session, created_by="creator_id")+ _persist_account(sqlite_session)+ app_tenant = Tenant(name="App workspace")+ app_tenant.id = "tenant_id"+ current_tenant = Tenant(name="Current workspace")+ current_tenant.id = "other_tenant_id"+ sqlite_session.add_all(+ [+ app_tenant,+ current_tenant,+ TenantAccountJoin(+ tenant_id="tenant_id",+ account_id="creator_id",+ current=False,+ role=TenantAccountRole.EDITOR,+ ),+ TenantAccountJoin(+ tenant_id="other_tenant_id",+ account_id="creator_id",+ current=True,+ role=TenantAccountRole.OWNER,+ ),+ ]+ )+ sqlite_session.commit()++ result = _trace_instance().get_service_account_with_tenant("some_app_id")++ assert result.id == "creator_id"+ assert result.current_tenant_id == "tenant_id"+ assert result.current_role == TenantAccountRole.EDITOR+ sqlite_session.expire_all()+ memberships = sqlite_session.scalars(+ select(TenantAccountJoin).where(TenantAccountJoin.account_id == "creator_id")+ ).all()+ assert {membership.tenant_id: membership.current for membership in memberships} == {+ "tenant_id": False,+ "other_tenant_id": True,+ }++[email protected]("sqlite_session", [TABLES], indirect=True)+def test_get_service_account_with_tenant_requires_app_membership(sqlite_session: Session):+ _persist_app(sqlite_session, created_by="creator_id")+ _persist_account(sqlite_session)+ app_tenant = Tenant(name="App workspace")+ app_tenant.id = "tenant_id"+ current_tenant = Tenant(name="Current workspace")+ current_tenant.id = "other_tenant_id"+ sqlite_session.add_all(+ [+ app_tenant,+ current_tenant,+ TenantAccountJoin(+ tenant_id="other_tenant_id",+ account_id="creator_id",+ current=True,+ role=TenantAccountRole.OWNER,+ ),+ ]+ )+ sqlite_session.commit()++ with pytest.raises(ValueError, match="App tenant tenant_id not found for account creator_id"):+ _trace_instance().get_service_account_with_tenant("some_app_id")++[email protected]("sqlite_session", [TABLES], indirect=True)+def test_get_service_account_with_tenant_requires_existing_app_tenant(sqlite_session: Session):+ _persist_app(sqlite_session, created_by="creator_id")+ _persist_account(sqlite_session)+ sqlite_session.add(+ TenantAccountJoin(+ tenant_id="tenant_id",+ account_id="creator_id",+ current=True,+ role=TenantAccountRole.OWNER,+ )+ )+ sqlite_session.commit()++ with pytest.raises(ValueError, match="App tenant tenant_id not found for account creator_id"):+ _trace_instance().get_service_account_with_tenant("some_app_id")