credits: close each cycle so a pack cannot be spent twice (#12)
ci/woodpecker/push/woodpecker Pipeline failed
ci/woodpecker/push/woodpecker Pipeline failed
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.
This commit is contained in:
1 parent
bcdc0416e2
commit
fe898905b4
5 files changed
+465
-18
No files matched your search
@@ -0,0 +1,42 @@
|
||||
#!/bin/sh
|
||||
# Credit cycle close — run by the Cloudron scheduler addon every 10 minutes.
|
||||
#
|
||||
# Calls the portal's own /admin/credit-cycle-close endpoint (localhost), which
|
||||
# walks each active member's completed allowance cycles and draws the credits
|
||||
# those cycles spent (issue #12: a monthly reset used to restore every credit
|
||||
# pack, because nothing ever recorded what usage consumed).
|
||||
#
|
||||
# Idempotent: each closed cycle is keyed by the ledger reference
|
||||
# `cycle-close:<email>:<cycle>`, so an overlapping tick or a restart never
|
||||
# charges a cycle twice. Cheap: one SELECT when nobody has credits.
|
||||
#
|
||||
# The ADMIN_TOKEN env var is shared with the main app (scheduler runs in the
|
||||
# same launch environment), so we use it to authenticate. Admin auth is a
|
||||
# header, never a URL token.
|
||||
|
||||
set -e
|
||||
|
||||
if [ -z "$ADMIN_TOKEN" ]; then
|
||||
echo "credit-cycle-close: ADMIN_TOKEN is not set; aborting" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
python3 - "$ADMIN_TOKEN" <<'PY'
|
||||
import sys, urllib.request, urllib.error
|
||||
|
||||
token = sys.argv[1]
|
||||
url = "http://127.0.0.1:8000/admin/credit-cycle-close"
|
||||
req = urllib.request.Request(url, method="POST", data=b"", headers={
|
||||
"Content-Type": "application/json",
|
||||
"X-Admin-Token": token,
|
||||
})
|
||||
try:
|
||||
with urllib.request.urlopen(req, timeout=300) as resp:
|
||||
print("credit-cycle-close:", resp.read().decode())
|
||||
except urllib.error.HTTPError as e:
|
||||
print(f"credit-cycle-close: HTTP {e.code}: {e.read().decode()}", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
except Exception as e:
|
||||
print(f"credit-cycle-close: request failed: {e}", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
PY
|
||||
Reference in new issue
Block a user