guests: only UNSPENT credit carries into membership
ci/woodpecker/push/woodpecker Pipeline was canceled

A guest has no monthly cycle, so nothing ever drew their spend against the $2
pack. Conversion kept the full credit_balance, handing back credits already
used — guest-access.md promises only the unspent credit carries over.

record_guest_conversion_draw() writes the consumed part as a negative ledger
row (reference guest-convert-draw:<email>) and bumps credits_drawn, capped by
the row's credit and idempotent on replay. Tests cover draw, replay, cap, and
the resulting remaining figure.
This commit is contained in:
inference-bot committed 2026-11-27 22:03:43 +00:00
1 parent cfb767dd4f
commit a70dd6ab4c
2 files changed
+140

No files matched your search

+54
View File
@@ -3729,6 +3729,42 @@ async def provision_guest_member(
return user_id
def record_guest_conversion_draw(email: str, spend: float) -> float:
"""Record guest usage consumed before conversion; only the unspent part carries.
A guest has no monthly cycle, so nothing ever drew their spend against the
$2 pack. Converting without this would hand back credits already used
(guest-access.md promises only the UNSPENT credit carries over). The draw
is capped by the row's current credit and deduped on a ledger reference.
Returns the amount recorded (0.0 when nothing owed or already recorded).
"""
purchased = get_credit_balance(email)
used = round(min(max(0.0, float(spend or 0.0)), purchased), 6)
if used <= 0:
return 0.0
ref = f"guest-convert-draw:{email}"
if credit_reference_exists(ref):
return 0.0
conn = get_db()
try:
conn.execute(
"INSERT INTO credit_ledger (email, delta, reason, reference) "
"VALUES (?, ?, ?, ?)",
(email, -used,
"guest credit used before conversion — only unspent credit carries over",
ref),
)
conn.execute(
"UPDATE members SET credits_drawn = COALESCE(credits_drawn, 0) + ? "
"WHERE email = ?",
(used, email),
)
conn.commit()
finally:
conn.close()
return used
async def convert_guest_to_member(email: str, name: str, slug: str = "") -> str:
"""Convert a GUEST account into a full member (the automatic transition).
@@ -3775,6 +3811,24 @@ async def convert_guest_to_member(email: str, name: str, slug: str = "") -> str:
conn.commit()
conn.close()
# Guest usage has already consumed part of the $2; only the UNSPENT credit
# may carry into membership (guest-access.md). record_guest_conversion_draw
# writes the consumed part as a draw so it cannot be spent twice.
try:
guest_spend = None
team_id = await litellm_team_id_for_email(email)
if team_id:
team = get_team_budget_from_db(team_id)
if team:
guest_spend = float(team.get("spend") or 0.0)
if guest_spend is not None:
used = record_guest_conversion_draw(email, guest_spend)
if used > 0:
logger.info("Guest convert: %.2f of guest credit already used for %s; "
"the rest carries over", used, email)
except Exception as e: # noqa: BLE001
logger.warning("Guest convert: guest-draw accounting failed for %s: %s", email, e)
# 3. Lift the LiteLLM cap to allowance + credits and pin the reset to
# their anchor day, exactly as the webhook does for any member.
await sync_team_budget_from_balance(email)