credits: defer a clawback cap that would fall under recorded spend
ci/woodpecker/push/woodpecker Pipeline was canceled
ci/woodpecker/push/woodpecker Pipeline was canceled
Applying the draw mid-cycle used to push max_budget below the member's already-recorded spend, so LiteLLM refused every call until their next reset (weeks for some anchors). The draw is still recorded immediately — books are correct — but the cap is only pushed once the member's spend has room for it, which happens right after LiteLLM's reset. The tick retries every 10 minutes, so the cap lands at the first tick after the boundary; deferred members are reported in close_credit_cycles().deferred. tests/test_credit_cycle_close.py covers draw-recorded/cap-deferred and the cap landing after spend resets, with no second draw.
This commit is contained in:
1 parent
1070fc5dd0
commit
3e01f727c2
2 files changed
+68
-6
No files matched your search
@@ -147,6 +147,48 @@ check_near("no overage, no draw",
|
||||
apply_cycle_draw("under@example.com", "2026-10-01", spend=9.0, allowance=15.0), 0.0)
|
||||
check_near("balance untouched", get_credits_drawn("under@example.com"), 0.0)
|
||||
|
||||
# --- a clawback that would land under recorded spend is DEFERRED ------------
|
||||
import asyncio # noqa: E402
|
||||
import app.main as m # noqa: E402
|
||||
|
||||
now = m.datetime.now(m.timezone.utc)
|
||||
anchor = now.day if now.day <= 28 else 28
|
||||
conn = get_db()
|
||||
m._ensure_allowance_tables(conn) # adds anchor_day
|
||||
conn.execute(
|
||||
"INSERT INTO members (email, key_token, active, account_type, balance, credit_balance, anchor_day) "
|
||||
"VALUES ('defer@example.com', '', 1, 'member', 15.0, 20.0, ?)", (anchor,))
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
state = {"spend": 40.0, "cap": 35.0}
|
||||
applied = []
|
||||
|
||||
async def fake_team(email):
|
||||
return "team:defer" if email == "defer@example.com" else None
|
||||
|
||||
async def fake_update(team_id, budget):
|
||||
applied.append(budget)
|
||||
state["cap"] = budget
|
||||
|
||||
m.litellm_team_id_for_email = fake_team
|
||||
m.litellm_update_team_budget = fake_update
|
||||
m.get_team_budget_from_db = lambda tid: {"spend": state["spend"], "max_budget": state["cap"],
|
||||
"budget_reset_at": None}
|
||||
m.cycle_spend_from_ledger = lambda tid, a, b: 40.0
|
||||
m.CREDIT_CLOSE_FROM = "2026-01-01"
|
||||
|
||||
r1 = asyncio.run(m.close_credit_cycles())
|
||||
check_near("draw recorded even while the cap is deferred", get_credits_drawn("defer@example.com"), 20.0)
|
||||
check("cap NOT lowered under recorded spend",
|
||||
any(d["email"] == "defer@example.com" for d in r1["deferred"]), True)
|
||||
check("no cap push while spend > new cap", applied, [])
|
||||
|
||||
state["spend"] = 0.0 # what LiteLLM does at the member's reset
|
||||
r2 = asyncio.run(m.close_credit_cycles())
|
||||
check("cap applied once spend resets", applied, [15.0])
|
||||
check("no second draw on the same cycle", r2["credits_drawn"], 0.0)
|
||||
|
||||
print()
|
||||
if failures:
|
||||
print(f"{failures} FAILURE(S)")
|
||||
|
||||
Reference in new issue
Block a user