AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
fix(backend): prevent duplicate personal orgs — in-loop lock renewal, get-or-create backfill, partial unique index (#13552)
Nicholas Tindle8cd6afb2731a
diff --git a/autogpt_platform/backend/backend/api/features/orgs/db.py b/autogpt_platform/backend/backend/api/features/orgs/db.pyindex 14fe21c5b5..8e2d06e7e0 100644--- a/autogpt_platform/backend/backend/api/features/orgs/db.py+++ b/autogpt_platform/backend/backend/api/features/orgs/db.py@@ -7,7 +7,11 @@ from datetime import datetime, timezonefrom prisma.errors import UniqueViolationErrorfrom backend.data.db import prisma, transaction-from backend.data.org_migration import _sanitize_slug, create_personal_org+from backend.data.org_migration import (+ _sanitize_slug,+ _soft_delete_blocking_orphan,+ create_personal_org,+)from backend.util.exceptions import NotFoundErrorfrom .model import OrgAliasResponse, OrgMemberResponse, OrgResponse, UpdateOrgData@@ -21,12 +25,16 @@ logger = logging.getLogger(__name__)async def _find_personal_org_member(user_id: str):+ # Ordered oldest-first so this agrees with get_request_context (auth) and+ # _find_owned_personal_org (org_migration) on the canonical personal org+ # when a user briefly has more than one.return await prisma.orgmember.find_first(where={"userId": user_id,"isOwner": True,"Org": {"isPersonal": True, "deletedAt": None},},+ order={"createdAt": "asc"},)@@ -96,6 +104,31 @@ async def _bootstrap_personal_org(user_id: str) -> str | None:"using existing org")return member.orgId+ # No membership: a legacy orphan may be squatting on the user's+ # one-personal-per-user index slot — clear it and retry once so+ # a first-touch request self-heals instead of degrading.+ if await _soft_delete_blocking_orphan(user_id):+ try:+ org = await _create_personal_org_for_user(+ user_id, slug_base, display_name+ )+ logger.info(+ f"Bootstrapped personal org {org.id} for user {user_id} "+ "after clearing a blocking orphan"+ )+ return org.id+ except UniqueViolationError:+ # A concurrent creator can win between the orphan clear+ # and this retry — reconcile before reporting failure.+ member = await _find_personal_org_member(user_id)+ if member is not None:+ return member.orgId+ logger.error(+ f"Personal-org bootstrap for {user_id} still failing "+ "after orphan cleanup",+ exc_info=True,+ )+ return Nonelogger.error(f"Personal-org bootstrap for {user_id} hit a unique violation ""but no membership exists",diff --git a/autogpt_platform/backend/backend/api/features/orgs/routes_test.py b/autogpt_platform/backend/backend/api/features/orgs/routes_test.pyindex 07e02f7022..a64d8a17d4 100644--- a/autogpt_platform/backend/backend/api/features/orgs/routes_test.py+++ b/autogpt_platform/backend/backend/api/features/orgs/routes_test.py@@ -1623,6 +1623,7 @@ class TestMigrationSlugEdgeCases:return_value=MagicMock(id="org-new"))self.prisma.orgmember.create = AsyncMock()+ self.prisma.orgmember.find_first = AsyncMock(return_value=None)self.prisma.team.create = AsyncMock(return_value=MagicMock(id="ws-new"))self.prisma.teammember.create = AsyncMock()self.prisma.organizationprofile.create = AsyncMock()@@ -1631,6 +1632,17 @@ class TestMigrationSlugEdgeCases:self.prisma.execute_raw = AsyncMock(return_value=0)mocker.patch("backend.data.org_migration.prisma", self.prisma)+ prisma_mock = self.prisma++ class _TxCM:+ async def __aenter__(self):+ return prisma_mock++ async def __aexit__(self, *exc):+ return False++ mocker.patch("backend.data.org_migration.transaction", lambda *a, **k: _TxCM())+@pytest.mark.asyncioasync def test_user_with_name_but_no_profile_uses_name_slug(self):from backend.data.org_migration import create_orgs_for_existing_users@@ -2910,3 +2922,22 @@ class TestPersonalOrgBootstrapOnDemand:assert org_id == "org-from-winner"self.create_org.assert_not_called()+++class TestCanonicalPersonalOrgOrdering:+ """The personal-org lookup must agree with auth's oldest-first rule so+ every path resolves the same canonical org when a user briefly has more+ than one."""++ @pytest.mark.asyncio+ async def test_find_personal_org_member_is_ordered_oldest_first(self, mocker):+ from backend.api.features.orgs.db import _find_personal_org_member++ prisma = MagicMock()+ prisma.orgmember.find_first = AsyncMock(return_value=None)+ mocker.patch("backend.api.features.orgs.db.prisma", prisma)++ await _find_personal_org_member("user-1")++ order = prisma.orgmember.find_first.call_args.kwargs["order"]+ assert order == {"createdAt": "asc"}diff --git a/autogpt_platform/backend/backend/data/org_migration.py b/autogpt_platform/backend/backend/data/org_migration.pyindex 5d17aa3f6b..4d31f6c626 100644--- a/autogpt_platform/backend/backend/data/org_migration.py+++ b/autogpt_platform/backend/backend/data/org_migration.py@@ -17,6 +17,7 @@ import loggingimport reimport timefrom collections.abc import Awaitable, Callable+from datetime import datetime, timezonefrom typing import TYPE_CHECKING, LiteralStringfrom uuid import uuid4@@ -47,6 +48,12 @@ return 0_RenewLock = Callable[[], Awaitable[None]]+# Renew the distributed bootstrap lock every N users inside the long per-user+# loops (org creation, credential copy). Matches the batched-tenancy cadence:+# frequent enough that the 300s lock never lapses mid-loop, rare enough not to+# spam Redis. If the lock has been lost the renewal raises and the loop aborts.+_LOCK_RENEW_EVERY = 50+def _sanitize_slug(raw: str) -> str:"""Convert a string to a URL-safe slug: lowercase, alphanumeric + hyphens."""@@ -204,14 +211,51 @@ async def _derive_personal_org_identity(user_id: str) -> tuple[str, str]:return slug_base, display_name+async def _soft_delete_blocking_orphan(user_id: str) -> bool:+ """Clear a legacy ORPHAN blocking *user_id*'s one-personal-per-user slot.++ An orphan is a live personal org carrying this user's ``bootstrapUserId``+ with no owner membership (pre-transaction partial create): it occupies the+ partial unique index while staying invisible to every membership-based+ re-check, so creation retries can never succeed until it is cleared. The+ org is unusable by definition — no member can resolve it — so soft-deleting+ it loses nothing. Returns True if one was cleared.+ """+ orphan = await prisma.organization.find_first(+ where={+ "isPersonal": True,+ "deletedAt": None,+ "bootstrapUserId": user_id,+ "Members": {"none": {"isOwner": True}},+ }+ )+ if orphan is None:+ return False+ logger.warning(+ f"Soft-deleting orphan personal org {orphan.id} (no owner membership) "+ f"blocking user {user_id}"+ )+ await prisma.organization.update(+ where={"id": orphan.id},+ data={"deletedAt": datetime.now(timezone.utc)},+ )+ return True++async def _find_owned_personal_org(user_id: str):- """Return *user_id*'s owned, non-deleted personal-org membership, or None."""+ """Return *user_id*'s owned, non-deleted personal-org membership, or None.++ Ordered oldest-first so every path that resolves "the" personal org agrees+ on the same canonical one when a user briefly has more than one — matching+ ``get_request_context`` (auth) and ``_find_personal_org_member`` (orgs.db).+ """return await prisma.orgmember.find_first(where={"userId": user_id,"isOwner": True,"Org": {"isPersonal": True, "deletedAt": None},},+ order={"createdAt": "asc"},)@@ -246,7 +290,10 @@ async def ensure_personal_org(user_id: str) -> None:# A concurrent request bootstrapped this user first.logger.debug("Personal org for user %s created concurrently", user_id)return- # Slug collided with a *different* user — retry with a fresh suffix.+ # A legacy orphan holding this user's index slot blocks every+ # retry — clear it so the next attempt can succeed. Otherwise+ # the slug collided with a *different* user; retry either way.+ await _soft_delete_blocking_orphan(user_id)raise RuntimeError(f"Failed to bootstrap a personal organization for user {user_id} after "@@ -254,9 +301,26 @@ async def ensure_personal_org(user_id: str) -> None:)-async def create_orgs_for_existing_users() -> int:+async def create_orgs_for_existing_users(+ renew_lock: _RenewLock | None = None,+) -> int:"""Create a personal Organization for every user that lacks one.+ Idempotent and safe under concurrent multi-pod startup. The ``NOT EXISTS``+ query is only a snapshot taken before the loop begins; by the time a given+ user is reached another pod (or an interrupted earlier run) may already+ have bootstrapped them. So each user is re-checked immediately before+ creation, and a ``UniqueViolationError`` — including one raised by the+ ``Organization_one_personal_per_user`` partial index when another writer+ wins the race — is treated as a clean skip rather than crashing startup.+ This is what turns the double-run that produced duplicate personal orgs+ into a no-op on the second pod.++ When *renew_lock* is provided it is called every ``_LOCK_RENEW_EVERY``+ users so the distributed bootstrap lock never lapses across a long loop.+ If the lock has been lost ``renew_lock`` raises, aborting the loop+ immediately so the next pod's fresh run is the sole writer.+Returns the number of orgs created."""# Find users who are NOT yet an owner of any personal org@@ -286,47 +350,111 @@ async def create_orgs_for_existing_users() -> int:)created = 0- for row in users_without_org:+ for processed, row in enumerate(users_without_org, start=1):user_id: str = row["id"]- email: str = row["email"]- profile_username: str | None = row.get("profile_username")- profile_name: str | None = row.get("profile_name")- user_name: str | None = row.get("name")-- # Determine slug: Profile.username → sanitized User.name → email local part → user-{id[:8]}- if profile_username:- desired_slug = _sanitize_slug(profile_username)- elif user_name:- desired_slug = _sanitize_slug(user_name)- else:- local_part = email.split("@")[0] if email else ""- desired_slug = (- _sanitize_slug(local_part) if local_part else f"user-{user_id[:8]}"- )- slug = await _resolve_unique_slug(desired_slug)+ # Renew BEFORE the re-check so skip-heavy re-runs (most users already+ # bootstrapped — exactly the second-wave scenario) still keep the lock+ # alive; counting only creates would let a long skip streak expire it.+ if renew_lock is not None and processed % _LOCK_RENEW_EVERY == 0:+ await renew_lock()- display_name = profile_name or user_name or email.split("@")[0]+ # Re-check against the live table — the snapshot above can be stale by+ # the time we reach this user, so skip anyone bootstrapped since.+ if await _find_owned_personal_org(user_id) is not None:+ continue- # Create Organization — only include optional JSON fields when non-None- org_data: dict = {- "name": display_name,- "slug": slug,- "isPersonal": True,- "bootstrapUserId": user_id,- "settings": "{}",- }- if row.get("stripeCustomerId"):- org_data["stripeCustomerId"] = row["stripeCustomerId"]- if row.get("topUpConfig"):- # query_raw returns Json columns as parsed Python objects; prisma- # create() requires them re-wrapped as Json, not raw dict/list.- org_data["topUpConfig"] = SafeJson(row["topUpConfig"])+ if await _get_or_create_backfill_org(row):+ created += 1++ logger.info(f"Org migration: created {created} personal orgs")+ return created+++async def _get_or_create_backfill_org(row: dict) -> bool:+ """Bootstrap one backfill row's personal org; True if this call created it.++ Mirrors ``ensure_personal_org``'s race handling: on ``UniqueViolationError``+ re-check whether the user now owns a personal org — a concurrent writer won+ (skip, return False) — versus a slug clash with a *different* user (retry+ with a fresh suffix). Raises if creation keeps failing, so a genuinely+ stuck user fails loud rather than silently missing an org.+ """+ user_id: str = row["id"]+ for _ in range(3):+ try:+ await _create_backfill_org(row)+ return True+ except UniqueViolationError:+ if await _find_owned_personal_org(user_id) is not None:+ logger.debug(+ "Org migration: user %s bootstrapped concurrently, skipping",+ user_id,+ )+ return False+ # A legacy orphan holding this user's index slot blocks every+ # retry — clear it so the next attempt can succeed. Otherwise+ # the slug collided with a *different* user; retry either way.+ await _soft_delete_blocking_orphan(user_id)+ raise RuntimeError(+ f"Org migration: failed to bootstrap a personal organization for user "+ f"{user_id} after retries"+ )+++async def _create_backfill_org(row: dict) -> None:+ """Create the Organization + baseline records for one backfill row.++ Unlike ``create_personal_org`` this also carries the billing+ (``stripeCustomerId``/``topUpConfig``) and store-profile enrichment the+ backfill sources from its ``query_raw`` JOIN. Raises ``UniqueViolationError``+ if the resolved slug or the one-personal-per-user index is already taken.+ """+ user_id: str = row["id"]+ email: str = row["email"]+ profile_username: str | None = row.get("profile_username")+ profile_name: str | None = row.get("profile_name")+ user_name: str | None = row.get("name")++ # Determine slug: Profile.username → sanitized User.name → email local part → user-{id[:8]}+ if profile_username:+ desired_slug = _sanitize_slug(profile_username)+ elif user_name:+ desired_slug = _sanitize_slug(user_name)+ else:+ local_part = email.split("@")[0] if email else ""+ desired_slug = (+ _sanitize_slug(local_part) if local_part else f"user-{user_id[:8]}"+ )- org = await prisma.organization.create(data=org_data)+ slug = await _resolve_unique_slug(desired_slug)++ display_name = profile_name or user_name or email.split("@")[0]++ # Create Organization — only include optional JSON fields when non-None+ org_data: dict = {+ "name": display_name,+ "slug": slug,+ "isPersonal": True,+ "bootstrapUserId": user_id,+ "settings": "{}",+ }+ if row.get("stripeCustomerId"):+ org_data["stripeCustomerId"] = row["stripeCustomerId"]+ if row.get("topUpConfig"):+ # query_raw returns Json columns as parsed Python objects; prisma+ # create() requires them re-wrapped as Json, not raw dict/list.+ org_data["topUpConfig"] = SafeJson(row["topUpConfig"])++ # All-or-nothing like create_personal_org: a partial create (org row+ # without its owner membership) would be invisible to every re-check —+ # they look for the OrgMember — while the one-personal-per-user index+ # blocks re-creation, permanently wedging the user.+ async with transaction() as tx:+ org = await tx.organization.create(data=org_data)# Create OrgMember (owner)- await prisma.orgmember.create(+ await tx.orgmember.create(data={"Org": {"connect": {"id": org.id}},"User": {"connect": {"id": user_id}},@@ -337,7 +465,7 @@ async def create_orgs_for_existing_users() -> int:)# Create default Team- workspace = await prisma.team.create(+ workspace = await tx.team.create(data={"name": "Default","Org": {"connect": {"id": org.id}},@@ -348,7 +476,7 @@ async def create_orgs_for_existing_users() -> int:)# Create TeamMember- await prisma.teammember.create(+ await tx.teammember.create(data={"Team": {"connect": {"id": workspace.id}},"User": {"connect": {"id": user_id}},@@ -374,10 +502,10 @@ async def create_orgs_for_existing_users() -> int:# whole startup migration.profile_data["socialLinks"] = SafeJson(row["profile_links"])- await prisma.organizationprofile.create(data=profile_data)+ await tx.organizationprofile.create(data=profile_data)# Create seat assignment (FREE seat for personal org)- await prisma.organizationseatassignment.create(+ await tx.organizationseatassignment.create(data={"organizationId": org.id,"userId": user_id,@@ -387,17 +515,12 @@ async def create_orgs_for_existing_users() -> int:})- # Log if slug diverged from desired (collision resolution)- if slug != desired_slug:- logger.info(- f"Org migration: slug collision for user {user_id} — "- f"desired '{desired_slug}', assigned '{slug}'"- )-- created += 1-- logger.info(f"Org migration: created {created} personal orgs")- return created+ # Log if slug diverged from desired (collision resolution)+ if slug != desired_slug:+ logger.info(+ f"Org migration: slug collision for user {user_id} — "+ f"desired '{desired_slug}', assigned '{slug}'"+ )async def migrate_org_balances() -> int:@@ -764,7 +887,9 @@ async def create_store_listing_aliases() -> int:return result-async def migrate_credentials_to_table() -> int:+async def migrate_credentials_to_table(+ renew_lock: _RenewLock | None = None,+) -> int:"""Copy each user's UserIntegrations blob credentials intoIntegrationCredential rows (ownerType=USER, personal org).@@ -775,6 +900,10 @@ async def migrate_credentials_to_table() -> int:ids already present in the table are skipped, so re-runs (everystartup) only pick up blob credentials not yet copied.+ When *renew_lock* is provided it is called every ``_LOCK_RENEW_EVERY``+ users so the distributed bootstrap lock never lapses across this loop.+ If the lock has been lost ``renew_lock`` raises and the loop aborts.+Returns the number of credential rows created."""from backend.data.model import UserIntegrations@@ -784,7 +913,11 @@ async def migrate_credentials_to_table() -> int:created = 0users = await prisma.user.find_many(where={"integrations": {"not": ""}})- for user in users:+ for processed, user in enumerate(users, start=1):+ # Renew up front so an early `continue` (undecryptable blob, no+ # credentials, deferred user) can't skip the renewal.+ if renew_lock is not None and processed % _LOCK_RENEW_EVERY == 0:+ await renew_lock()try:integrations = UserIntegrations.model_validate(cryptor.decrypt(user.integrations)@@ -803,8 +936,15 @@ async def migrate_credentials_to_table() -> int:org_row = await prisma.organization.find_first(where={"isPersonal": True,+ "deletedAt": None,"Members": {"some": {"userId": user.id, "isOwner": True}},- }+ },+ # Oldest-first LIVE org so credentials attach to the same canonical+ # personal org every other path resolves (see+ # _find_owned_personal_org). deletedAt matters: after the dedupe+ # migration soft-deletes duplicate losers, the oldest org for a+ # user may be a dead one no request path ever resolves.+ order={"createdAt": "asc"},)if org_row is None:# Personal org not bootstrapped yet — the next startup sweep@@ -901,7 +1041,7 @@ async def run_migration() -> None:start = time.monotonic()logger.info("Org migration: starting personal org bootstrap")- orgs_created = await create_orgs_for_existing_users()+ orgs_created = await create_orgs_for_existing_users(renew_lock=renew_lock)await renew_lock()await migrate_org_balances()await renew_lock()@@ -913,7 +1053,7 @@ async def run_migration() -> None:await renew_lock()await create_store_listing_aliases()await renew_lock()- await migrate_credentials_to_table()+ await migrate_credentials_to_table(renew_lock=renew_lock)await renew_lock()total_resources = sum(resource_counts.values())diff --git a/autogpt_platform/backend/backend/data/org_migration_test.py b/autogpt_platform/backend/backend/data/org_migration_test.pyindex c2eb6db543..34fb87d683 100644--- a/autogpt_platform/backend/backend/data/org_migration_test.py+++ b/autogpt_platform/backend/backend/data/org_migration_test.py@@ -31,6 +31,11 @@ def mock_prisma(mocker):mock.organization.find_unique = AsyncMock(return_value=None)mock.organizationalias.find_unique = AsyncMock(return_value=None)mock.organization.create = AsyncMock(return_value=MagicMock(id="org-1"))+ # Default: the orphan-org lookup in the violation handler finds nothing.+ mock.organization.find_first = AsyncMock(return_value=None)+ mock.organization.update = AsyncMock()+ # Default: the per-user re-check in the backfill finds no existing org.+ mock.orgmember.find_first = AsyncMock(return_value=None)mock.orgmember.create = AsyncMock()mock.team.create = AsyncMock(return_value=MagicMock(id="ws-1"))mock.teammember.create = AsyncMock()@@ -39,6 +44,17 @@ def mock_prisma(mocker):mock.query_raw = AsyncMock(return_value=[])mock.execute_raw = AsyncMock(return_value=0)mocker.patch("backend.data.org_migration.prisma", mock)++ # _create_backfill_org now writes inside `async with transaction() as tx`;+ # yield the same mock so per-model create assertions keep working.+ class _TxCM:+ async def __aenter__(self):+ return mock++ async def __aexit__(self, *exc):+ return False++ mocker.patch("backend.data.org_migration.transaction", lambda *a, **k: _TxCM())return mock@@ -427,6 +443,205 @@ class TestCreateOrgsForExistingUsers:assert seat_data["status"] == "ACTIVE"+# ---------------------------------------------------------------------------+# create_orgs_for_existing_users — lock renewal + get-or-create hardening+# ---------------------------------------------------------------------------+++def _backfill_row(user_id: str) -> dict:+ """A minimal ``query_raw`` backfill row for a user with no profile."""+ return {+ "id": user_id,+ "email": f"{user_id}@example.com",+ "name": None,+ "stripeCustomerId": None,+ "topUpConfig": None,+ "profile_username": None,+ "profile_name": None,+ "profile_description": None,+ "profile_avatar_url": None,+ "profile_links": None,+ }+++class TestBackfillHardening:+ @pytest.mark.asyncio+ async def test_renews_lock_every_50_users(self, mock_prisma):+ mock_prisma.query_raw = AsyncMock(+ return_value=[_backfill_row(f"user-{i}") for i in range(120)]+ )+ renew_lock = AsyncMock()++ created = await create_orgs_for_existing_users(renew_lock=renew_lock)++ assert created == 120+ # Fires after the 50th and 100th user, not the trailing 120.+ assert renew_lock.await_count == 2++ @pytest.mark.asyncio+ async def test_lost_lock_mid_loop_aborts_creating(self, mock_prisma):+ mock_prisma.query_raw = AsyncMock(+ return_value=[_backfill_row(f"user-{i}") for i in range(120)]+ )+ # First renewal (after user 50) reports the lock was lost.+ renew_lock = AsyncMock(+ side_effect=RuntimeError(+ "Org migration lost the distributed bootstrap lock"+ )+ )++ with pytest.raises(RuntimeError, match="lost the distributed bootstrap lock"):+ await create_orgs_for_existing_users(renew_lock=renew_lock)++ # Renewal now fires BEFORE processing user 50, so exactly 49 were+ # created before the abort — the loop did NOT keep writing after+ # losing the lock (which is what let a second pod duplicate orgs).+ assert mock_prisma.organization.create.await_count == 49++ @pytest.mark.asyncio+ async def test_skip_heavy_rerun_still_renews_lock(self, mock_prisma):+ """A re-run where every user is already bootstrapped must still renew:+ a long skip streak that never renews is the same TTL-expiry hole the+ incident exploited, just on the second pass."""+ mock_prisma.query_raw = AsyncMock(+ return_value=[_backfill_row(f"user-{i}") for i in range(120)]+ )+ mock_prisma.orgmember.find_first = AsyncMock(return_value=MagicMock())+ renew_lock = AsyncMock()++ created = await create_orgs_for_existing_users(renew_lock=renew_lock)++ assert created == 0+ assert mock_prisma.organization.create.await_count == 0+ assert renew_lock.await_count == 2++ @pytest.mark.asyncio+ async def test_signup_path_heals_blocking_orphan(self, mock_prisma, mocker):+ """ensure_personal_org (signup) must clear a blocking orphan and+ succeed on retry, same as the backfill path — otherwise signup fails+ its retries on a state only the backfill could heal."""+ from prisma.errors import UniqueViolationError++ from backend.data.org_migration import ensure_personal_org++ mocker.patch(+ "backend.data.org_migration._derive_personal_org_identity",+ AsyncMock(return_value=("jane", "Jane")),+ )+ create = mocker.patch(+ "backend.data.org_migration.create_personal_org",+ AsyncMock(side_effect=[UniqueViolationError(MagicMock()), None]),+ )+ mock_prisma.orgmember.find_first = AsyncMock(return_value=None)+ orphan = MagicMock(id="orphan-org")+ mock_prisma.organization.find_first = AsyncMock(return_value=orphan)++ await ensure_personal_org("user-x")++ assert create.await_count == 2+ update_kwargs = mock_prisma.organization.update.call_args.kwargs+ assert update_kwargs["where"] == {"id": "orphan-org"}++ @pytest.mark.asyncio+ async def test_orphan_org_blocking_index_is_soft_deleted_and_retried(+ self, mock_prisma+ ):+ """A legacy orphan (bootstrapUserId set, no owner membership) blocks+ the unique index but is invisible to the membership re-check — the+ handler must soft-delete it and let the retry recreate cleanly."""+ from prisma.errors import UniqueViolationError++ mock_prisma.query_raw = AsyncMock(return_value=[_backfill_row("user-x")])+ mock_prisma.organization.create = AsyncMock(+ side_effect=[UniqueViolationError(MagicMock()), MagicMock(id="org-2")]+ )+ mock_prisma.orgmember.find_first = AsyncMock(return_value=None)+ orphan = MagicMock(id="orphan-org")+ mock_prisma.organization.find_first = AsyncMock(return_value=orphan)++ created = await create_orgs_for_existing_users()++ assert created == 1+ update_kwargs = mock_prisma.organization.update.call_args.kwargs+ assert update_kwargs["where"] == {"id": "orphan-org"}+ assert update_kwargs["data"]["deletedAt"] is not None+ assert mock_prisma.organization.create.await_count == 2++ @pytest.mark.asyncio+ async def test_user_already_bootstrapped_is_skipped(self, mock_prisma):+ """Second-run semantics: the snapshot listed this user as org-less, but+ the live re-check finds one (another pod won) — skip, don't create."""+ mock_prisma.query_raw = AsyncMock(return_value=[_backfill_row("user-x")])+ mock_prisma.orgmember.find_first = AsyncMock(return_value=MagicMock())++ created = await create_orgs_for_existing_users()++ assert created == 0+ mock_prisma.organization.create.assert_not_called()++ @pytest.mark.asyncio+ async def test_unique_violation_race_is_swallowed_as_skip(self, mock_prisma):+ """A concurrent writer creates the org between our re-check and our+ insert: the insert raises UniqueViolationError (slug or the+ one-personal-per-user index), we re-check, find the winner's org, and+ skip instead of crashing startup."""+ from prisma.errors import UniqueViolationError++ mock_prisma.query_raw = AsyncMock(return_value=[_backfill_row("user-race")])+ # Top-of-loop re-check: None. Post-violation re-check: winner's org.+ mock_prisma.orgmember.find_first = AsyncMock(side_effect=[None, MagicMock()])+ mock_prisma.organization.create = AsyncMock(+ side_effect=UniqueViolationError({})+ )++ created = await create_orgs_for_existing_users()++ assert created == 0+ # Only one insert attempt — the loser did not retry into a 2nd org.+ assert mock_prisma.organization.create.await_count == 1++ @pytest.mark.asyncio+ async def test_slug_collision_with_different_user_retries(self, mock_prisma):+ """A slug clash with a *different* user (re-check still finds no org for+ us) retries with a fresh slug rather than skipping or crashing."""+ from prisma.errors import UniqueViolationError++ mock_prisma.query_raw = AsyncMock(return_value=[_backfill_row("user-dup")])+ mock_prisma.orgmember.find_first = AsyncMock(return_value=None)+ mock_prisma.organization.create = AsyncMock(+ side_effect=[UniqueViolationError({}), MagicMock(id="org-2")]+ )++ created = await create_orgs_for_existing_users()++ assert created == 1+ assert mock_prisma.organization.create.await_count == 2++ @pytest.mark.asyncio+ async def test_persistent_failure_raises_loud(self, mock_prisma):+ """If creation keeps violating and the user still owns no org, fail loud+ rather than silently leaving the account without an organization."""+ from prisma.errors import UniqueViolationError++ mock_prisma.query_raw = AsyncMock(return_value=[_backfill_row("user-stuck")])+ mock_prisma.orgmember.find_first = AsyncMock(return_value=None)+ mock_prisma.organization.create = AsyncMock(+ side_effect=UniqueViolationError({})+ )++ with pytest.raises(RuntimeError, match="after retries"):+ await create_orgs_for_existing_users()++ @pytest.mark.asyncio+ async def test_find_owned_personal_org_is_ordered_oldest_first(self, mock_prisma):+ from backend.data.org_migration import _find_owned_personal_org++ await _find_owned_personal_org("user-1")++ order = mock_prisma.orgmember.find_first.call_args.kwargs["order"]+ assert order == {"createdAt": "asc"}++# ---------------------------------------------------------------------------# create_personal_org (single-user bootstrap)# ---------------------------------------------------------------------------@@ -888,7 +1103,7 @@ class TestRunMigration:mocker.patch("backend.data.org_migration.create_orgs_for_existing_users",- new_callable=lambda: lambda: _track(calls, "create_orgs", 1),+ new_callable=lambda: lambda **_kw: _track(calls, "create_orgs", 1),)mocker.patch("backend.data.org_migration.migrate_org_balances",@@ -918,7 +1133,7 @@ class TestRunMigration:)mocker.patch("backend.data.org_migration.migrate_credentials_to_table",- new_callable=lambda: lambda: _track(calls, "credentials", 0),+ new_callable=lambda: lambda **_kw: _track(calls, "credentials", 0),)await run_migration()diff --git a/autogpt_platform/backend/migrations/20260713120000_org_one_personal_per_user/migration.sql b/autogpt_platform/backend/migrations/20260713120000_org_one_personal_per_user/migration.sqlnew file mode 100644index 0000000000..43833f46a0--- /dev/null+++ b/autogpt_platform/backend/migrations/20260713120000_org_one_personal_per_user/migration.sql@@ -0,0 +1,79 @@+-- Enforce at most one live personal org per user.+--+-- The startup backfill (create_orgs_for_existing_users) held the Redis+-- bootstrap lock across its whole per-user loop with no mid-loop renewal.+-- When a wave exceeded the 300s lock TTL, a second pod acquired the lock and+-- re-ran from a fresh snapshot, creating a *second* personal org for every+-- user the first pod had already processed. Nothing at the storage layer+-- prevented it (Organization only has slug @unique, and _resolve_unique_slug+-- side-stepped that with -N suffixes). This migration closes the gap: it+-- de-dupes any orgs the incident already created, then adds a partial unique+-- index so a future double-run collides cleanly instead of duplicating.+--+-- Both steps are set-based and idempotent: on a clean DB (already one personal+-- org per user) the de-dupe UPDATE matches zero rows and the index builds with+-- nothing to reject. Runs inside Prisma's migration transaction, so plain+-- CREATE INDEX (not CONCURRENTLY); the Organization table is small at migrate+-- time in every environment because the backfill runs post-migrate.++-- Step 0: sweep legacy ORPHAN personal orgs — rows carrying a bootstrapUserId+-- with no owner membership (left by pre-transaction partial creates). They are+-- unusable (no member can ever resolve them) but would occupy the unique index+-- slot while staying invisible to the backfill's membership re-check, wedging+-- the user. Idempotent: matches nothing on healthy DBs.+UPDATE "Organization" o+SET "deletedAt" = NOW()+WHERE o."isPersonal" = true+ AND o."deletedAt" IS NULL+ AND o."bootstrapUserId" IS NOT NULL+ AND NOT EXISTS (+ SELECT 1 FROM "OrgMember" m+ WHERE m."orgId" = o."id" AND m."isOwner" = true+ );++-- Step 1: de-duplicate existing personal orgs.+--+-- Winner per bootstrapUserId group = the org that actually accumulated the+-- user's tenanted resources: most AgentGraph rows, then most ChatSession rows,+-- then oldest, then lowest id (final deterministic tiebreak). Losers are+-- soft-deleted (deletedAt = now()); we deliberately do NOT hard-delete or+-- touch balances/ledgers here — money/slug remediation for the affected orgs+-- is handled operationally, not in-migration.+WITH ranked AS (+ SELECT+ o."id" AS org_id,+ ROW_NUMBER() OVER (+ PARTITION BY o."bootstrapUserId"+ ORDER BY+ (+ SELECT COUNT(*) FROM "AgentGraph" ag+ WHERE ag."organizationId" = o."id"+ ) DESC,+ (+ SELECT COUNT(*) FROM "ChatSession" cs+ WHERE cs."organizationId" = o."id"+ ) DESC,+ o."createdAt" ASC,+ o."id" ASC+ ) AS rn+ FROM "Organization" o+ WHERE o."isPersonal" = true+ AND o."deletedAt" IS NULL+ AND o."bootstrapUserId" IS NOT NULL+)+UPDATE "Organization" o+SET "deletedAt" = NOW()+FROM ranked+WHERE o."id" = ranked.org_id+ AND ranked.rn > 1;++-- Step 2: partial unique index guaranteeing one live personal org per user.+-- Only live personal orgs are constrained; soft-deleted losers (Step 1) and+-- converted team orgs (isPersonal flipped false) are excluded, so the+-- personal->team conversion path — which flips isPersonal=false on the old org+-- BEFORE creating the replacement personal org — never trips it.+CREATE UNIQUE INDEX "Organization_one_personal_per_user"+ON "Organization" ("bootstrapUserId")+WHERE "isPersonal" = true+ AND "deletedAt" IS NULL+ AND "bootstrapUserId" IS NOT NULL;diff --git a/autogpt_platform/backend/schema.prisma b/autogpt_platform/backend/schema.prismaindex 36e63df77f..6f094492e8 100644--- a/autogpt_platform/backend/schema.prisma+++ b/autogpt_platform/backend/schema.prisma@@ -1762,6 +1762,10 @@ model Organization {TransfersTo TransferRequest[] @relation("TransferTarget")AuditLogs AuditLog[]// slug already has @unique which creates an index — no separate @@index needed+ // Partial unique index "Organization_one_personal_per_user" on (bootstrapUserId)+ // WHERE isPersonal AND deletedAt IS NULL AND bootstrapUserId IS NOT NULL enforces+ // at most one live personal org per user — created via SQL migration+ // 20260713120000_org_one_personal_per_user (Prisma can't model partial indexes).}model OrganizationAlias {