credit_balance was never reduced and max_budget was never re-derived at a reset, so every cycle restored every pack. Live exposure measured read-only today: 38 members hold $986 of purchased credits; 15 of them are past their $15 allowance this cycle by $454.74 in total — each reset would hand that back.
Fix
members.credits_drawn — credits consumed in CLOSED cycles. credit_balance stays the lifetime purchased total, so the members' "bought $X all-time" figure keeps its meaning.
compute_max_budget(allowance, purchased, spend, drawn) caps at allowance + max(0, purchased − drawn). Spend inside the CURRENT cycle still does not shrink the cap, so a credit add keeps raising it 1:1 (the old inversion bug stays fixed).
apply_cycle_draw() records max(0, cycle_spend − allowance) as a NEGATIVE credit_ledger row and bumps credits_drawn in one transaction, deduped on reference cycle-close:<email>:<cycle>. Cycle spend is summed from LiteLLM_SpendLogs over the exact anchor-day window, so no snapshot job or race is needed.
close_credit_cycles() runs every 10 minutes from a new Cloudron scheduler task (credit_cycle_close.sh) and again as a catch-up in /admin/sweep. GET /admin/credits/{email} now reports credits_drawn.
Policy choice encoded here
CREDIT_CLOSE_FROM (default 2026-11-28) skips cycles that STARTED before the fix. Credits already regranted under the old code are forgiven rather than clawed back mid-cycle; charging starts with each member's next full cycle.
Verification
tests/test_credit_cycle_close.py — cap arithmetic, current-cycle non-shrink, the issue's $65 example end-to-end, replay draws nothing, draw capped by credits available. Ran in the portal image against this tree: 22/22 pass. The existing suite's DB/arithmetic tests also pass unchanged against the patched tree.
Not deployed yet: this changes member-visible caps and the money posture, so it needs the operator's go-ahead before build + cloudron update.
Fixes #12.
## The bug, verified
On the deployed image (`cd /app/code && DB_PATH=/tmp/x.db python3 -c 'from app.main import compute_max_budget as cap; print(cap(15,20,25)); print(cap(15,20,0))'`):
```
cycle 1: {'max_budget': 35, 'allowance_used': 15, 'credits_used': 10, 'credits_remaining': 10}
after reset: {'max_budget': 35, 'allowance_used': 0, 'credits_used': 0.0, 'credits_remaining': 20.0}
```
`credit_balance` was never reduced and `max_budget` was never re-derived at a reset, so every cycle restored every pack. Live exposure measured read-only today: 38 members hold $986 of purchased credits; 15 of them are past their $15 allowance this cycle by $454.74 in total — each reset would hand that back.
## Fix
- `members.credits_drawn` — credits consumed in CLOSED cycles. `credit_balance` stays the lifetime purchased total, so the members' "bought $X all-time" figure keeps its meaning.
- `compute_max_budget(allowance, purchased, spend, drawn)` caps at `allowance + max(0, purchased − drawn)`. Spend inside the CURRENT cycle still does not shrink the cap, so a credit add keeps raising it 1:1 (the old inversion bug stays fixed).
- `apply_cycle_draw()` records `max(0, cycle_spend − allowance)` as a NEGATIVE `credit_ledger` row and bumps `credits_drawn` in one transaction, deduped on reference `cycle-close:<email>:<cycle>`. Cycle spend is summed from `LiteLLM_SpendLogs` over the exact anchor-day window, so no snapshot job or race is needed.
- `close_credit_cycles()` runs every 10 minutes from a new Cloudron scheduler task (`credit_cycle_close.sh`) and again as a catch-up in `/admin/sweep`. `GET /admin/credits/{email}` now reports `credits_drawn`.
## Policy choice encoded here
`CREDIT_CLOSE_FROM` (default `2026-11-28`) skips cycles that STARTED before the fix. Credits already regranted under the old code are forgiven rather than clawed back mid-cycle; charging starts with each member's next full cycle.
## Verification
`tests/test_credit_cycle_close.py` — cap arithmetic, current-cycle non-shrink, the issue's $65 example end-to-end, replay draws nothing, draw capped by credits available. Ran in the portal image against this tree: 22/22 pass. The existing suite's DB/arithmetic tests also pass unchanged against the patched tree.
Not deployed yet: this changes member-visible caps and the money posture, so it needs the operator's go-ahead before build + `cloudron update`.
LiteLLM zeroes a team's `spend` at budget_reset_at but leaves max_budget
alone, and nothing in the portal ever reduced credit_balance. So every
monthly reset restored every credit pack already spent: a $15 member who
bought $50 and used all $65 got $65 again the next month.
Fix — record the draw when a cycle closes:
- new `members.credits_drawn` (credits consumed in CLOSED cycles);
credit_balance stays the lifetime purchased total so the "bought all-time"
display never changes meaning.
- `compute_max_budget` caps at allowance + credits STILL AVAILABLE
(purchased − drawn). Spend inside the current cycle still does not shrink
the cap, so a credit add keeps raising it 1:1.
- `apply_cycle_draw` writes a NEGATIVE credit_ledger row per closed cycle,
deduped on reference `cycle-close:<email>:<cycle>`; the cycle's spend is
summed from LiteLLM_SpendLogs over the exact anchor-day window.
- `close_credit_cycles` runs every 10 min via the new Cloudron scheduler task
(credit_cycle_close.sh) and again as a catch-up inside /admin/sweep.
- CREDIT_CLOSE_FROM (default 2026-11-28) stops it charging cycles that
started before the fix: credits already regranted are forgiven rather than
clawed back mid-cycle.
- /admin/credits/{email} now reports credits_drawn.
Tests: tests/test_credit_cycle_close.py covers the cap arithmetic, the
issue's worked example, draw idempotence and the available-credit cap.
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.
Fixes #12.
The bug, verified
On the deployed image (
cd /app/code && DB_PATH=/tmp/x.db python3 -c 'from app.main import compute_max_budget as cap; print(cap(15,20,25)); print(cap(15,20,0))'):credit_balancewas never reduced andmax_budgetwas never re-derived at a reset, so every cycle restored every pack. Live exposure measured read-only today: 38 members hold $986 of purchased credits; 15 of them are past their $15 allowance this cycle by $454.74 in total — each reset would hand that back.Fix
members.credits_drawn— credits consumed in CLOSED cycles.credit_balancestays the lifetime purchased total, so the members' "bought $X all-time" figure keeps its meaning.compute_max_budget(allowance, purchased, spend, drawn)caps atallowance + max(0, purchased − drawn). Spend inside the CURRENT cycle still does not shrink the cap, so a credit add keeps raising it 1:1 (the old inversion bug stays fixed).apply_cycle_draw()recordsmax(0, cycle_spend − allowance)as a NEGATIVEcredit_ledgerrow and bumpscredits_drawnin one transaction, deduped on referencecycle-close:<email>:<cycle>. Cycle spend is summed fromLiteLLM_SpendLogsover the exact anchor-day window, so no snapshot job or race is needed.close_credit_cycles()runs every 10 minutes from a new Cloudron scheduler task (credit_cycle_close.sh) and again as a catch-up in/admin/sweep.GET /admin/credits/{email}now reportscredits_drawn.Policy choice encoded here
CREDIT_CLOSE_FROM(default2026-11-28) skips cycles that STARTED before the fix. Credits already regranted under the old code are forgiven rather than clawed back mid-cycle; charging starts with each member's next full cycle.Verification
tests/test_credit_cycle_close.py— cap arithmetic, current-cycle non-shrink, the issue's $65 example end-to-end, replay draws nothing, draw capped by credits available. Ran in the portal image against this tree: 22/22 pass. The existing suite's DB/arithmetic tests also pass unchanged against the patched tree.Not deployed yet: this changes member-visible caps and the money posture, so it needs the operator's go-ahead before build +
cloudron update.LiteLLM zeroes a team's `spend` at budget_reset_at but leaves max_budget alone, and nothing in the portal ever reduced credit_balance. So every monthly reset restored every credit pack already spent: a $15 member who bought $50 and used all $65 got $65 again the next month. Fix — record the draw when a cycle closes: - new `members.credits_drawn` (credits consumed in CLOSED cycles); credit_balance stays the lifetime purchased total so the "bought all-time" display never changes meaning. - `compute_max_budget` caps at allowance + credits STILL AVAILABLE (purchased − drawn). Spend inside the current cycle still does not shrink the cap, so a credit add keeps raising it 1:1. - `apply_cycle_draw` writes a NEGATIVE credit_ledger row per closed cycle, deduped on reference `cycle-close:<email>:<cycle>`; the cycle's spend is summed from LiteLLM_SpendLogs over the exact anchor-day window. - `close_credit_cycles` runs every 10 min via the new Cloudron scheduler task (credit_cycle_close.sh) and again as a catch-up inside /admin/sweep. - CREDIT_CLOSE_FROM (default 2026-11-28) stops it charging cycles that started before the fix: credits already regranted are forgiven rather than clawed back mid-cycle. - /admin/credits/{email} now reports credits_drawn. Tests: tests/test_credit_cycle_close.py covers the cap arithmetic, the issue's worked example, draw idempotence and the available-credit cap.Merged to main by push (inference-bot is in the Owners push whitelist); commit
fe89890. Building20261127-fe89890from main.Pull request closed