credits: an adjustment below the drawn amount must not eat the next pack
ci/woodpecker/push/woodpecker Pipeline was canceled

credits_drawn could exceed credit_balance after an admin set/correction (or a
negative adjustment), so a later pack was only partly usable: available =
max(0, purchased - drawn) swallowed the difference. Cap credits_drawn at the
row's balance on every add_credits movement; ledger history is unchanged.
Tests cover reduction-then-repurchase.
This commit is contained in:
inference-bot committed 2026-11-27 22:05:19 +00:00
1 parent 066a6fc2cf
commit d10b36ed34
2 files changed
+20 -2

No files matched your search

+9
View File
@@ -1708,6 +1708,15 @@ def add_credits(email: str, delta: float, reason: str, reference: str | None = N
"INSERT INTO credit_ledger (email, delta, reason, reference) VALUES (?, ?, ?, ?)", "INSERT INTO credit_ledger (email, delta, reason, reference) VALUES (?, ?, ?, ?)",
(email, delta, reason, reference), (email, delta, reason, reference),
) )
# Keep `credits_drawn <= credit_balance`. An adjustment that lowers the pack
# below what was already drawn must not swallow FUTURE credits: a new pack
# adds headroom, while consumption beyond the reduced pack is simply
# unbacked. The ledger history itself is never rewritten.
conn.execute(
"UPDATE members SET credits_drawn = MIN(COALESCE(credits_drawn, 0), "
"MAX(0, COALESCE(credit_balance, 0))) WHERE email = ?",
(email,),
)
row = conn.execute( row = conn.execute(
"SELECT credit_balance FROM members WHERE email = ?", (email,) "SELECT credit_balance FROM members WHERE email = ?", (email,)
).fetchone() ).fetchone()
+11 -2
View File
@@ -32,8 +32,8 @@ for _cand in (os.environ.get("PORTAL_CODE_DIR"), "/app/code",
break break
from app.main import ( # noqa: E402 from app.main import ( # noqa: E402
_prev_month, apply_cycle_draw, compute_max_budget, cycle_start_for_anchor_day, _prev_month, add_credits, apply_cycle_draw, compute_max_budget,
get_credit_balance, get_credits_drawn, get_db, cycle_start_for_anchor_day, get_credit_balance, get_credits_drawn, get_db,
) )
failures = 0 failures = 0
@@ -189,6 +189,15 @@ r2 = asyncio.run(m.close_credit_cycles())
check("cap applied once spend resets", applied, [15.0]) check("cap applied once spend resets", applied, [15.0])
check("no second draw on the same cycle", r2["credits_drawn"], 0.0) check("no second draw on the same cycle", r2["credits_drawn"], 0.0)
# --- lowering a pack below what was drawn must not swallow NEW credits ----
add_credits("small@example.com", -10.0, reason="admin correction (test)")
check_near("drawn follows the pack down when it is reduced", get_credits_drawn("small@example.com"), 0.0)
check_near("balance reduced to zero", get_credit_balance("small@example.com"), 0.0)
add_credits("small@example.com", 5.0, reason="new pack (test)")
check_near("a new pack is fully available after the correction",
compute_max_budget(15, get_credit_balance("small@example.com"), 0.0,
get_credits_drawn("small@example.com"))["credits_remaining"], 5.0)
print() print()
if failures: if failures:
print(f"{failures} FAILURE(S)") print(f"{failures} FAILURE(S)")