fix(teams): keep backfilled members consistent with the default they join

Members that keep their own row after a backfill now take the default's
reset time, so their spend resets when the default's does. A model list
always stays the member's own, even when it matches the default, so
clearing the default later cannot widen what they may call.

Writing the default member budget now evicts every roster member's cached
membership, including when its cap is cleared, and /team/member_update reads
the member's budget again once it holds the team lock, so an edit racing a
backfill lands on the row the member actually uses.
This commit is contained in:
shrey kharbanda 2026-10-23 16:13:58 -07:00
parent 0270b6bc5c
commit ce46561555
5 changed files with 131 additions and 15 deletions

View file

@ -501,6 +501,10 @@ def _team_membership_db(prisma_client: PrismaClient | None) -> "TableActions[pri
return TeamMembershipRepository(prisma_client).table
def _team_membership_tx_db(tx: "Prisma") -> "TableActions[prisma_models.LiteLLM_TeamMembership]":
return tx.litellm_teammembership # pyright: ignore[reportReturnType] # TableActions widens the generated inputs to Mapping, as the repositories do
def _user_db(prisma_client: PrismaClient | None) -> "TableActions[prisma_models.LiteLLM_UserTable]":
return UserRepository(prisma_client).table
@ -1242,7 +1246,7 @@ async def _write_team_update(
@dataclass(frozen=True, slots=True)
class _TeamWrite:
team_row: "prisma_models.LiteLLM_TeamTable | None"
linked_member_user_ids: tuple[str, ...]
roster_user_ids: tuple[str, ...]
moved_member_budgets: MovedMemberBudgets | None
def backfill_response(self) -> Mapping[str, TeamMemberBudgetBackfillResult]:
@ -1255,7 +1259,7 @@ class _TeamWrite:
def changed_member_user_ids(self) -> tuple[str, ...]:
moved: Final = () if self.moved_member_budgets is None else self.moved_member_budgets.user_ids
return tuple(dict.fromkeys((*self.linked_member_user_ids, *moved)))
return tuple(dict.fromkeys((*self.roster_user_ids, *moved)))
async def _write_team_with_member_default_budget(
@ -1304,7 +1308,7 @@ async def _write_team_with_member_default_budget(
team_row: Final = await _write_team_update(_team_tx_db(tx), team_id, team_update_data, max_budget_guard)
return _TeamWrite(
team_row=team_row,
linked_member_user_ids=member_user_ids if default_budget_write.links_members else (),
roster_user_ids=member_user_ids,
moved_member_budgets=moved if backfill != "none" else None,
)
@ -2602,7 +2606,7 @@ async def update_team(
team_row=await _write_team_update(
_team_db(prisma_client), data.team_id, team_update_data, max_budget_guard
),
linked_member_user_ids=(),
roster_user_ids=(),
moved_member_budgets=None,
)
if default_budget_write is None
@ -3958,13 +3962,6 @@ async def team_member_update(
status_code=400,
detail={"error": f"User id doesn't exist in team table. Data={data}"},
)
## find the relevant team membership
identified_budget_id: str | None = None
for tm in returned_team_info["team_memberships"]:
if tm.user_id == received_user_id:
identified_budget_id = tm.budget_id
break
# If this membership still points at the team's shared default member
# budget, _upsert_budget_and_membership will clone-on-write so that the
# update only touches this user (not every member sharing the default).
@ -3990,11 +3987,14 @@ async def team_member_update(
if data.role is not None
else None
)
locked_membership: Final = await _team_membership_tx_db(tx).find_unique(
where={"user_id_team_id": {"user_id": received_user_id, "team_id": data.team_id}}
)
await upsert_budget_and_membership(
tx=tx,
team_id=data.team_id,
user_id=received_user_id,
existing_budget_id=identified_budget_id,
existing_budget_id=locked_membership.budget_id if locked_membership is not None else None,
user_api_key_dict=user_api_key_dict,
budget_patch=budget_patch,
team_default_budget_id=team_default_budget_id,

View file

@ -28,6 +28,8 @@ if TYPE_CHECKING:
MAX_MEMBER_BUDGETS_MOVED_PER_UPDATE: Final = 50_000
_ALWAYS_OWN_LIMITS: Final = frozenset({"allowed_models"})
_DEFAULT_BUDGET_COLUMNS: Final = MappingProxyType(
{
"team_member_budget": "max_budget",
@ -53,6 +55,12 @@ _CLEAR_MAX_BUDGET_SQL: Final = """
UPDATE "LiteLLM_BudgetTable" SET max_budget = NULL, updated_by = $2, updated_at = (NOW() AT TIME ZONE 'UTC')
WHERE budget_id = ANY($1::text[])
"""
_ALIGN_RESET_TO_DEFAULT_SQL: Final = """
UPDATE "LiteLLM_BudgetTable" b
SET budget_reset_at = (SELECT d.budget_reset_at FROM "LiteLLM_BudgetTable" d WHERE d.budget_id = $2)
FROM "LiteLLM_TeamMembership" m
WHERE m.team_id = $1 AND m.user_id = ANY($3::text[]) AND m.budget_id = b.budget_id AND b.budget_id <> $2
"""
_SHARED_BUDGET_IDS_SQL: Final = """
SELECT budget_id FROM "LiteLLM_TeamMembership" WHERE budget_id = ANY($1::text[])
GROUP BY budget_id HAVING COUNT(*) > 1
@ -129,7 +137,9 @@ class _BudgetLimits(BaseModel):
def has_own_limits_beyond(self, default: "_BudgetLimits") -> bool:
own: Final = _JSON_OBJECT.validate_python(self.model_dump(mode="json"))
inherited: Final = _JSON_OBJECT.validate_python(default.model_dump(mode="json"))
return has_meaningful_budget_limit({field: value for field, value in own.items() if value != inherited[field]})
return has_meaningful_budget_limit(
{field: value for field, value in own.items() if field in _ALWAYS_OWN_LIMITS or value != inherited[field]}
)
class _MemberBudgetRef(BaseModel):
@ -218,8 +228,8 @@ async def move_member_budgets_to_default(
changed_by: UserAPIKeyAuth,
) -> MovedMemberBudgets | TooManyMemberBudgetsToMove:
"""Members compare on their permanent ``max_budget``, and only those resetting on the default's schedule move.
A member whose other limits all match the default is linked to the default row, while one holding a limit of its
own keeps its row with ``max_budget`` cleared."""
A member whose other limits all match the default is linked to the default row. One holding a limit of its own, or
any model list, keeps its row with ``max_budget`` cleared and its reset moved onto the default's."""
if backfill == "none":
return MovedMemberBudgets(user_ids=(), skipped_other_reset_period=0)
default: Final = await _budget_tx_db(tx).find_unique(where={"budget_id": default_budget_id})
@ -292,6 +302,7 @@ async def _move_to_default(
team_default_budget_id=default_budget_id,
shared_budget_ids=shared,
)
await tx.execute_raw(_ALIGN_RESET_TO_DEFAULT_SQL, team_id, default_budget_id, [ref.user_id for ref in keeping])
def _prisma_columns(columns: Mapping[str, object]) -> Mapping[str, object]:

View file

@ -1,5 +1,6 @@
from collections.abc import Mapping
from dataclasses import dataclass
from datetime import datetime, timezone
import pytest
from prisma import Json
@ -188,6 +189,50 @@ async def test_backfill_links_members_sharing_a_plain_custom_budget_to_the_defau
assert shared.max_budget == 20.0
async def test_a_moved_member_keeping_their_own_limits_resets_with_the_default(db, resources):
default_budget_id = await _seed_team(db, resources, {"own_rpm": _Budget(20.0, rpm_limit=5)})
default_reset = datetime(2030, 1, 1, tzinfo=timezone.utc)
await db.db.litellm_budgettable.update(
where={"budget_id": default_budget_id}, data={"budget_reset_at": default_reset}
)
await db.db.litellm_budgettable.update(
where={"budget_id": resources.tag("own-own_rpm")},
data={"budget_reset_at": datetime(2030, 1, 15, tzinfo=timezone.utc)},
)
response = _update_team(
db,
resources.prefix,
{"team_member_budget": _NEW_DEFAULT, "team_member_budget_backfill": "raise_to_default"},
)
assert response.status_code == 200, response.text
kept = (await _member_budget(db, resources, "own_rpm")).litellm_budget_table
assert (kept.max_budget, kept.rpm_limit, kept.budget_reset_at) == (None, 5, default_reset)
async def test_a_member_with_a_model_list_keeps_it_even_when_it_matches_the_default(db, resources):
default_budget_id = await _seed_team(db, resources, {"scoped": _Budget(20.0)})
for budget_id in (default_budget_id, resources.tag("own-scoped")):
await db.db.litellm_budgettable.update(
where={"budget_id": budget_id}, data={"allowed_models": ["gpt-5.4-mini"]}
)
response = _update_team(
db,
resources.prefix,
{"team_member_budget": _NEW_DEFAULT, "team_member_budget_backfill": "raise_to_default"},
)
assert response.status_code == 200, response.text
scoped = await _member_budget(db, resources, "scoped")
assert scoped.budget_id == resources.tag("own-scoped")
assert (scoped.litellm_budget_table.max_budget, scoped.litellm_budget_table.allowed_models) == (
None,
["gpt-5.4-mini"],
)
async def test_backfill_over_the_member_cap_rolls_back_the_new_default(db, resources):
default_budget_id = await _seed_team(db, resources, {})
over_cap = MAX_MEMBER_BUDGETS_MOVED_PER_UPDATE + 1

View file

@ -10565,6 +10565,50 @@ async def test_update_team_clears_explicitly_null_member_model_budget_and_evicts
assert cache.async_delete_cache.await_args_list == [call(key="team_member_default_budget:member-budget-1")]
@pytest.mark.asyncio
async def test_clearing_the_member_default_evicts_every_roster_members_cached_membership(
disable_audit_logging_for_mocked_team,
):
"""A member on the default caches the default row inside their membership, so clearing its cap must drop
that cached copy, or a member already at the old cap stays blocked until the cache expires."""
from fastapi import Request
from litellm.proxy._types import UpdateTeamRequest
from litellm.proxy.management_endpoints.team_endpoints import update_team
existing: Final = _existing_team_with_model_caps({})
existing.metadata = {"team_member_budget_id": "member-budget-1"}
existing.model_dump.return_value["metadata"] = existing.metadata
cache: Final = MagicMock()
cache.async_get_cache = AsyncMock(return_value=None)
cache.async_set_cache = AsyncMock()
cache.async_delete_cache = AsyncMock()
updated: Final = _existing_team_with_model_caps({})
updated.metadata = existing.metadata
updated.litellm_model_table = None
prisma: Final = MagicMock()
with (
patch("litellm.proxy.proxy_server.prisma_client", prisma),
patch("litellm.proxy.proxy_server.user_api_key_cache", cache),
patch("litellm.proxy.proxy_server.litellm_proxy_admin_name", "admin"),
):
prisma.db.litellm_teamtable.find_unique = AsyncMock(return_value=existing)
prisma.jsonify_team_object = lambda db_data: db_data
prisma.db.litellm_teamtable.update = AsyncMock(return_value=updated)
tx: Final = _transaction_over(prisma)
tx.query_raw = AsyncMock(return_value=[{"members_with_roles": [{"user_id": "at-old-cap", "role": "user"}]}])
await update_team(
data=UpdateTeamRequest(team_id="standalone-team-123", team_member_budget=None),
http_request=MagicMock(spec=Request),
user_api_key_dict=UserAPIKeyAuth(user_role=LitellmUserRoles.PROXY_ADMIN, user_id="admin"),
)
evicted: Final = {c.args[0] for c in cache.in_memory_cache_for.return_value.delete_cache.call_args_list}
assert "standalone-team-123_at-old-cap" in evicted
@pytest.mark.asyncio
async def test_update_team_persists_member_model_budget_to_default_budget_row(
disable_audit_logging_for_mocked_team,
@ -13427,6 +13471,7 @@ async def test_team_member_update_role_change_emits_a_roster_audit_event(monkeyp
mock_prisma_client.db.litellm_auditlog.create = AsyncMock()
monkeypatch.setattr("litellm.proxy.proxy_server.prisma_client", mock_prisma_client)
_wire_member_delete_tx(mock_prisma_client)
mock_prisma_client.db.litellm_teammembership.find_unique = AsyncMock(return_value=None)
with (
patch( # test-quality-ok: no live DB here; matches this file's established convention for endpoint-logic unit tests
@ -13522,6 +13567,7 @@ async def test_team_member_update_role_change_rewrites_the_roster_it_read_under_
mock_prisma_client.db.litellm_auditlog.create = AsyncMock()
monkeypatch.setattr("litellm.proxy.proxy_server.prisma_client", mock_prisma_client)
tx = _wire_member_delete_tx(mock_prisma_client)
mock_prisma_client.db.litellm_teammembership.find_unique = AsyncMock(return_value=None)
team_info_patch, upsert_patch = _member_update_patches(stale_snapshot)
with team_info_patch, upsert_patch:

View file

@ -69,6 +69,9 @@ def happy_path_upsert(monkeypatch):
class _FakeTx:
litellm_teamtable = prisma_client.db.litellm_teamtable
litellm_teammembership = types.SimpleNamespace(
find_unique=AsyncMock(return_value=types.SimpleNamespace(user_id="user-1", budget_id="bud-under-lock"))
)
async def __aenter__(self):
return self
@ -123,6 +126,17 @@ async def test_team_member_update_sends_provided_fields_as_patch(happy_path_upse
assert response.budget_duration == "30d"
@pytest.mark.asyncio
async def test_team_member_update_writes_the_budget_the_member_has_once_the_team_lock_is_held(happy_path_upsert):
"""A team default backfill that commits while this update waits for the lock can relink the member. The
update must then write the budget the member points at now, not the one read before the lock."""
data, request, auth = _member_update_request(max_budget_in_team=30.0)
await team_member_update(data, request, auth)
assert happy_path_upsert.await_args.kwargs["existing_budget_id"] == "bud-under-lock"
@pytest.mark.asyncio
async def test_team_member_update_explicit_null_clears_field(happy_path_upsert):
"""An explicitly-null field must be forwarded as None so the column is