π OpenHands: AI-Driven Development
fix: delete LiteLLM user on personal org reset (#14700)
ε―εΊι549421e30f00
diff --git a/enterprise/storage/org_store.py b/enterprise/storage/org_store.pyindex 676eaf56c2..0601be8e71 100644--- a/enterprise/storage/org_store.py+++ b/enterprise/storage/org_store.py@@ -61,6 +61,17 @@ _ORG_SETTINGS_FIELDS = {class OrgStore:"""Store for managing organizations."""+ @staticmethod+ async def _delete_litellm_user_best_effort(user_id: str, org_id: UUID) -> None:+ """Delete the LiteLLM user record without blocking org deletion."""+ try:+ await LiteLlmManager.delete_user(user_id)+ except Exception as exc:+ logger.warning(+ 'Failed to delete LiteLLM user during org cascade cleanup',+ extra={'org_id': str(org_id), 'user_id': user_id, 'error': str(exc)},+ )+@staticmethoddef get_agent_settings_from_org(org: Org) -> AgentSettingsConfig:# Route through the shared SDK loader: it applies persisted-settings@@ -755,6 +766,10 @@ class OrgStore:)await LiteLlmManager.delete_team(str(org_id))+ if requester_orphan_ids:+ for user_id in requester_orphan_ids:+ await OrgStore._delete_litellm_user_best_effort(user_id, org_id)+# 7. Commit all changes only if everything succeededawait session.commit()diff --git a/enterprise/tests/unit/test_org_store.py b/enterprise/tests/unit/test_org_store.pyindex 431bb9047d..6b7a526f6f 100644--- a/enterprise/tests/unit/test_org_store.py+++ b/enterprise/tests/unit/test_org_store.py@@ -663,7 +663,13 @@ async def test_delete_org_cascade_success(async_session_maker, mock_litellm_api)session.add(expected_org)await session.commit()- with patch('storage.org_store.a_session_maker', async_session_maker):+ with (+ patch('storage.org_store.a_session_maker', async_session_maker),+ patch(+ 'storage.org_store.OrgStore._delete_litellm_user_best_effort',+ new=AsyncMock(),+ ) as mock_delete_litellm_user,+ ):# Actresult = await OrgStore.delete_org_cascade(org_id)@@ -673,6 +679,38 @@ async def test_delete_org_cascade_success(async_session_maker, mock_litellm_api)assert result.name == 'Test Organization'assert result.contact_name == 'John Doe'assert result.contact_email == '[email protected]'+ mock_delete_litellm_user.assert_not_called()+++async def test_delete_litellm_user_best_effort_calls_litellm():+ user_id = str(uuid.uuid4())+ org_id = uuid.uuid4()++ with patch(+ 'storage.org_store.LiteLlmManager.delete_user', new=AsyncMock()+ ) as mock_delete_user:+ await OrgStore._delete_litellm_user_best_effort(user_id, org_id)++ mock_delete_user.assert_called_once_with(user_id)+++async def test_delete_litellm_user_best_effort_swallows_litellm_failure():+ user_id = str(uuid.uuid4())+ org_id = uuid.uuid4()++ with (+ patch(+ 'storage.org_store.LiteLlmManager.delete_user',+ new=AsyncMock(side_effect=Exception('LiteLLM API unavailable')),+ ) as mock_delete_user,+ patch('storage.org_store.logger.warning') as mock_warning,+ ):+ await OrgStore._delete_litellm_user_best_effort(user_id, org_id)++ mock_delete_user.assert_called_once_with(user_id)+ mock_warning.assert_called_once()@pytest.mark.asyncio@@ -1089,7 +1127,13 @@ async def test_delete_org_cascade_sole_org_requester_is_deleted(await session.commit()# Act- with patch('storage.org_store.a_session_maker', async_session_maker):+ with (+ patch('storage.org_store.a_session_maker', async_session_maker),+ patch(+ 'storage.org_store.OrgStore._delete_litellm_user_best_effort',+ new=AsyncMock(),+ ) as mock_delete_litellm_user,+ ):result = await OrgStore.delete_org_cascade(org_id, requester_user_id=str(user_id))@@ -1097,6 +1141,7 @@ async def test_delete_org_cascade_sole_org_requester_is_deleted(# Assert: the deleted org is returned, and the user/org/member rows are gone.assert result is not Noneassert result.id == org_id+ mock_delete_litellm_user.assert_called_once_with(str(user_id), org_id)async with async_session_maker() as session:assert await session.get(Org, org_id) is None