allowances: resolve OC slug from the member's email, not the stored column

The first report run matched orders on members.slug, which holds a stale
guest-… slug from first provisioning for 18 members — every one of them read
as unpaid while actually being current payers. Resolve the live slug from the
member's email via the OC member list, refresh the stored slug, and treat
members absent from the OC member list as unverifiable (skip, never punish).
This commit is contained in:
inference-bot committed 2026-11-04 05:16:32 +00:00
1 parent 985b55f6d9
commit f8b3eaacd6
1 file changed
+101 -180
+101 -180
View File
@@ -4135,22 +4135,70 @@ def cycle_start_for_anchor_day(anchor_day: int, now: datetime | None = None) ->
async def fetch_member_index() -> dict[str, dict]:
"""email -> {slug, joined, role, tier} from the live OC member list.
Paginated (the members connection caps at 100/page and hides the rest).
The slug here is the member's CURRENT Open Collective slug — the members
table can hold a stale `guest-…` slug from first provisioning, so orders
must be matched on the slug resolved from the member's EMAIL, never on the
stored column (matching on the stored slug falsely read 18 current payers
as unpaid in the first report run).
"""
if not OC_PERSONAL_TOKEN:
raise RuntimeError("OC_PERSONAL_TOKEN not set; cannot fetch the member index")
out: dict[str, dict] = {}
offset = 0
async with httpx.AsyncClient(timeout=30.0) as client:
for _ in range(20):
query = (
'{ collective(slug: "%s") { members(limit: 100, offset: %d) { totalCount '
"nodes { role createdAt tier { name } account { name slug type ... on Individual { email } } } } } }"
% (OC_COLLECTIVE_SLUG, offset)
)
r = await client.post(
OC_GRAPHQL_URL,
headers={"Content-Type": "application/json",
"Personal-Token": OC_PERSONAL_TOKEN,
"Api-Version": "2022-01-01",
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64)"},
json={"query": query},
)
if r.status_code != 200:
raise RuntimeError(f"OC member index fetch failed: HTTP {r.status_code}")
members = (((r.json().get("data") or {}).get("collective") or {}).get("members") or {})
page = members.get("nodes") or []
for n in page:
a = n.get("account") or {}
e = (a.get("email") or "").strip().lower()
if e:
out[e] = {
"slug": a.get("slug") or "",
"joined": n.get("createdAt") or "",
"role": n.get("role") or "",
"tier": ((n.get("tier") or {}).get("name") or ""),
}
offset += len(page)
if not page or offset >= (members.get("totalCount") or 0):
break
return out
async def anchor_day_for_member(email: str, slug: str = "") -> int:
"""The member's allowance anchor day (the day of month they joined).
Stored on the members row; derived from the OC join date on first use and
persisted so later decisions are stable. Clamped to 28.
Stored on the members row; derived from the OC join date (resolved by email)
on first use and persisted so later decisions are stable. Clamped to 28.
"""
conn = get_db()
row = conn.execute("SELECT anchor_day FROM members WHERE email = ?", (email,)).fetchone()
conn.close()
if row and row[0]:
return _clamp_anchor_day(row[0])
if slug:
try:
for n in await fetch_all_member_nodes():
if (n.get("account") or {}).get("slug") == slug:
dt = _parse_oc_dt(n.get("createdAt"))
rec = (await fetch_member_index()).get(email.strip().lower())
if rec:
dt = _parse_oc_dt(rec.get("joined"))
if dt:
d = _clamp_anchor_day(dt.day)
conn = get_db()
@@ -4158,155 +4206,30 @@ async def anchor_day_for_member(email: str, slug: str = "") -> int:
conn.commit()
conn.close()
return d
break
except Exception as e:
logger.warning("anchor_day_for_member: OC join-date lookup failed for %s: %s", email, e)
return _clamp_anchor_day(datetime.now(timezone.utc).day)
async def fetch_member_orders() -> dict[str, list[dict]]:
"""Every OC order for the collective, grouped by the payer's slug.
One query for the whole collective (orders(limit: 200)) rather than a call
per member. Raises on failure so the caller can skip rather than mis-decide.
"""
if not OC_PERSONAL_TOKEN:
raise RuntimeError("OC_PERSONAL_TOKEN not set; cannot fetch orders")
query = (
'{ collective(slug: "%s") { orders(limit: 200) { totalCount nodes { '
"frequency status createdAt lastChargedAt nextChargeDate "
"fromAccount { slug } totalAmount { value currency } } } } }"
% OC_COLLECTIVE_SLUG
)
async with httpx.AsyncClient(timeout=30.0) as client:
r = await client.post(
OC_GRAPHQL_URL,
headers={"Content-Type": "application/json",
"Authorization": f"Bearer {OC_PERSONAL_TOKEN}",
"Api-Version": "2022-01-01",
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64)"},
json={"query": query},
)
if r.status_code != 200:
raise RuntimeError(f"OC order fetch failed: HTTP {r.status_code}")
body = r.json()
if body.get("errors"):
raise RuntimeError(f"OC order fetch errors: {str(body['errors'])[:200]}")
orders = (((body.get("data") or {}).get("collective") or {}).get("orders") or {})
by_slug: dict[str, list[dict]] = {}
for o in orders.get("nodes") or []:
slug = ((o.get("fromAccount") or {}).get("slug") or "").strip()
if slug:
by_slug.setdefault(slug, []).append(o)
return by_slug
def _payment_verdict(orders: list[dict], cycle_start: datetime) -> tuple[bool, str]:
"""Did this member's recurring contribution pay for the current cycle?
Returns (paid, evidence). A PROCESSING order counts as PAID (a payment in
flight is not a missed payment). A live ACTIVE recurring contribution is
treated as paid even if its next charge is still ahead — otherwise a member
whose charge falls due tomorrow reads as unpaid on every sweep.
"""
monthly = [o for o in orders if (o.get("frequency") or "").upper() == "MONTHLY"]
if not monthly:
return False, "no recurring (monthly) contribution on Open Collective"
live = [o for o in monthly if (o.get("status") or "").upper() in ("ACTIVE", "PROCESSING")]
if live:
charges = [c for c in (_parse_oc_dt(o.get("lastChargedAt")) for o in live) if c]
last = max(charges).date().isoformat() if charges else "unknown"
return True, f"live recurring contribution (last charged {last})"
charged = [(_parse_oc_dt(o.get("lastChargedAt")), o.get("status")) for o in monthly]
charged = [(c, s) for c, s in charged if c]
if charged:
last_dt, _ = max(charged, key=lambda t: t[0])
if last_dt >= cycle_start - timedelta(days=7):
return True, f"charged {last_dt.date().isoformat()} within the current cycle"
return False, f"last charge {last_dt.date().isoformat()} is before this cycle"
statuses = ",".join(sorted({(o.get("status") or "?").upper() for o in monthly}))
return False, f"no live recurring contribution (order status: {statuses})"
def payment_reminder_email(name: str, contribute_url: str) -> tuple[str, str, str]:
"""Build the one-per-cycle 'we haven't seen your contribution' reminder.
Same shell as welcome_email()/budget_notice_email(): cream background, white
card, forest-green button, inline pine-tree mark, table-based, no external
assets. Plain and direct — it names the action, not the plumbing.
"""
subject = "Your Inference Cooperative membership needs attention"
text = (
f"Hi {name},\n\n"
"Our records at Open Collective don't show a current monthly contribution "
"for your account, so your monthly allowance won't renew.\n\n"
"To keep your access, restart or update your contribution here:\n\n"
f"{contribute_url}\n\n"
"If your payment has already gone through, ignore this — your allowance "
"will renew on its own. If we don't see a contribution after a short grace "
"period, your account stays in place but loses access until your "
"contribution resumes.\n\n"
"Questions? Reply to this email or write to info@inference.coop.\n\n"
"— The Inference Cooperative\n"
)
html = f"""<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"></head>
<body style="margin:0;padding:0;background:#faf8f5;font-family:-apple-system,'Segoe UI',Roboto,Helvetica,Arial,sans-serif;">
<table role="presentation" width="100%" cellpadding="0" cellspacing="0" style="background:#faf8f5;padding:40px 16px;">
<tr>
<td align="center">
<table role="presentation" width="100%" cellpadding="0" cellspacing="0" style="max-width:520px;">
<tr>
<td align="center" style="padding-bottom:24px;">
{PINE_LOGO_SVG}
</td>
</tr>
<tr>
<td align="center" style="background:#ffffff;border:1px solid #e8e2d8;border-radius:16px;padding:40px 32px;">
<h1 style="margin:0 0 12px;font-size:24px;font-weight:700;color:#2d3327;">Your membership needs attention</h1>
<p style="margin:0 0 8px;font-size:16px;color:#6b7a62;line-height:1.5;">Our records at Open Collective don't show a current monthly contribution for your account, so your monthly allowance won't renew.</p>
<p style="margin:0 0 28px;font-size:15px;color:#6b7a62;line-height:1.5;">To keep your access, restart or update your contribution. If your payment has already gone through, ignore this.</p>
<a href="{contribute_url}" style="display:inline-block;background:#5b8c5a;color:#ffffff;text-decoration:none;padding:14px 36px;border-radius:999px;font-size:16px;font-weight:600;">Update your contribution</a>
<p style="margin:28px 0 0;font-size:13px;color:#94a08c;line-height:1.5;">If we don't see a contribution after a short grace period, your account stays in place but loses access until your contribution resumes.</p>
</td>
</tr>
<tr>
<td align="center" style="padding-top:24px;font-size:13px;color:#94a08c;line-height:1.6;">
Questions? Reply to this email or write to <a href="mailto:info@inference.coop" style="color:#6b7a62;">info@inference.coop</a>.<br>
<span style="color:#2d3327;">The Inference Cooperative</span> &middot; inference.coop
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>"""
return subject, text, html
async def reconcile_allowances(send: bool = False) -> dict:
"""Decide every active member's allowance renewal from OC payment evidence.
`send=False` is a REPORT: it computes and records every decision (renew /
remind / deactivate) and changes NOTHING — no resets, no emails, no group
changes. `send=True` performs renewals and sends the single reminder.
Deactivation still additionally requires ALLOWANCE_DEACTIVATE=on.
`send=False` is a REPORT: it computes and records every decision (would
renew / would remind / would deactivate) and changes NOTHING — no resets,
no emails, no group changes. `send=True` performs renewals and sends the
single reminder. Deactivation still additionally requires
ALLOWANCE_DEACTIVATE=on.
"""
try:
by_slug = await fetch_member_orders()
except Exception as e:
logger.warning("Allowance reconcile: order fetch failed; skipping (fail-safe): %s", e)
return {"status": "skipped", "reason": "order_fetch_failed", "detail": str(e)[:200]}
# Join dates from OC (authoritative anchor source) — best-effort.
joined_by_slug: dict[str, str] = {}
try:
for n in await fetch_all_member_nodes():
acct = n.get("account") or {}
joined_by_slug[acct.get("slug") or ""] = n.get("createdAt") or ""
index = await fetch_member_index()
except Exception as e:
logger.warning("Allowance reconcile: could not read OC join dates (%s); using stored anchor", e)
logger.warning("Allowance reconcile: member index fetch failed; skipping (fail-safe): %s", e)
return {"status": "skipped", "reason": "member_index_failed", "detail": str(e)[:200]}
conn = get_db()
_ensure_allowance_tables(conn)
@@ -4316,19 +4239,14 @@ async def reconcile_allowances(send: bool = False) -> dict:
f"SELECT email, slug, anchor_day, org_slug{extra} FROM members WHERE active = 1"
).fetchall()
# The collective's own ADMIN accounts (the co-op) are not members to reconcile.
admin_emails: set[str] = set()
try:
admin_emails = {m["email"] for m in await fetch_members() if m.get("role") == "ADMIN"}
except Exception as e:
logger.warning("Allowance reconcile: could not read ADMIN accounts (%s)", e)
now = datetime.now(timezone.utc)
contribute = credit_pack_url()
verified = "on" if (send and ALLOWANCE_POLICY == "on") else "report"
label = (lambda a: a) if verified == "on" else (lambda a: "would_" + a)
decisions: list[dict] = []
counts = {"renewed": 0, "reminded": 0, "deactivated": 0,
"would_renew": 0, "would_remind": 0, "would_deactivate": 0, "skipped": 0}
"would_renew": 0, "would_remind": 0, "would_deactivate": 0, "in_grace": 0,
"skipped": 0}
for row in rows:
email = row[0]
@@ -4340,25 +4258,28 @@ async def reconcile_allowances(send: bool = False) -> dict:
if email.lower() in MANUAL_MEMBERS:
counts["skipped"] += 1
continue
if email in admin_emails:
# The co-op's own account — never reconciled against payment.
counts["skipped"] += 1
continue
if org_slug:
# Dues are paid by an OC Organization; handled by the org branch.
counts["skipped"] += 1
counts["skipped"] += 1 # dues paid by an OC Organization
continue
if not slug:
# No OC slug on file → we cannot check payment. Never punish that.
decisions.append({"email": email, "slug": "", "anchor_day": anchor_day,
"cycle": "", "decision": "skipped", "evidence": "no OC slug on file"})
rec = index.get(email.strip().lower())
if rec and rec.get("role") == "ADMIN":
counts["skipped"] += 1 # the co-op's own account
continue
oc_slug = (rec or {}).get("slug") or ""
if not oc_slug:
decisions.append({"email": email, "slug": slug, "anchor_day": anchor_day,
"cycle": "", "decision": "skipped",
"evidence": "not in the live Open Collective member list — cannot verify payment"})
counts["skipped"] += 1
continue
# Resolve and persist the anchor day (join day).
# Keep the stored slug in step with Open Collective (authoritative).
if send and oc_slug != slug:
conn.execute("UPDATE members SET slug = ? WHERE email = ?", (oc_slug, email))
conn.commit()
if not anchor_day:
src = "oc_join_date" if joined_by_slug.get(slug) else ("welcome_sent" if welcome_sent else "today")
dt = _parse_oc_dt(joined_by_slug.get(slug)) or _parse_oc_dt(welcome_sent) or now
dt = _parse_oc_dt(rec.get("joined")) or _parse_oc_dt(welcome_sent) or now
anchor_day = _clamp_anchor_day(dt.day)
if send:
conn.execute("UPDATE members SET anchor_day = ? WHERE email = ?", (anchor_day, email))
@@ -4367,14 +4288,14 @@ async def reconcile_allowances(send: bool = False) -> dict:
cycle_start = cycle_start_for_anchor_day(anchor_day, now)
cycle_key = cycle_start.date().isoformat()
orders = by_slug.get(slug, [])
orders = by_slug.get(oc_slug, [])
paid, evidence = _payment_verdict(orders, cycle_start)
if paid:
decision, act = "renewed", "renew"
next_reset = next_reset_for_anchor_day(anchor_day, now)
evidence = f"{evidence}; reset anchored to day {anchor_day} → next reset {next_reset.date().isoformat()}"
counts["renewed" if verified == "on" else "would_renew"] += 1
evidence = f"{evidence}; reset anchored to join-day {anchor_day} → next reset {next_reset.date().isoformat()}"
decision = label("renewed")
counts[decision] += 1
if verified == "on":
try:
team_id = await litellm_team_id_for_email(email)
@@ -4383,16 +4304,15 @@ async def reconcile_allowances(send: bool = False) -> dict:
except Exception as e:
logger.warning("Allowance renew failed for %s: %s", email, e)
else:
reminded_row = conn.execute(
reminded = conn.execute(
"SELECT sent_at FROM payment_reminders WHERE email = ? AND cycle = ?",
(email, cycle_key),
).fetchone()
if not reminded_row:
decision, act = "reminded", "remind"
counts["reminded" if verified == "on" else "would_remind"] += 1
if not reminded:
decision = label("reminded")
counts[decision] += 1
if verified == "on":
name = slug or "there"
subject, text_body, html_body = payment_reminder_email(name, contribute)
subject, text_body, html_body = payment_reminder_email(oc_slug or slug or "there", contribute)
if send_email(email, subject, text_body, html_body):
conn.execute(
"INSERT OR REPLACE INTO payment_reminders (email, cycle, sent_at)"
@@ -4403,11 +4323,12 @@ async def reconcile_allowances(send: bool = False) -> dict:
else:
decision = "remind_failed"
else:
sent_at = _parse_oc_dt(str(reminded_row[0]))
sent_at = _parse_oc_dt(str(reminded[0]))
elapsed = (now - sent_at).days if sent_at else None
if elapsed is not None and elapsed >= PAYMENT_GRACE_DAYS and ALLOWANCE_DEACTIVATE == "on":
decision, act = "deactivated", "deactivate"
counts["deactivated" if verified == "on" else "would_deactivate"] += 1
decision = label("deactivated")
counts[decision] += 1
evidence = f"{evidence}; reminder sent {str(reminded[0])[:10]}, grace {int(PAYMENT_GRACE_DAYS)}d elapsed"
if verified == "on":
urow = conn.execute(
"SELECT cloudron_user_id FROM members WHERE email = ?", (email,)
@@ -4417,20 +4338,20 @@ async def reconcile_allowances(send: bool = False) -> dict:
deactivate_member(email)
await revoke_member_litellm_access(email)
await listmonk_sync(email, subscribe=False)
evidence = f"{evidence}; reminder sent {reminded_row[0][:10]}, grace {int(PAYMENT_GRACE_DAYS)}d elapsed"
else:
decision, act = "in_grace", "wait"
evidence = (f"{evidence}; reminder sent " +
(str(reminded_row[0])[:10] if reminded_row[0] else "?") +
f" (grace {int(PAYMENT_GRACE_DAYS)}d)")
decision = "in_grace"
counts["in_grace"] += 1
evidence = (f"{evidence}; reminder sent "
f"{str(reminded[0])[:10] if reminded[0] else '?'} "
f"(grace {int(PAYMENT_GRACE_DAYS)}d)")
conn.execute(
"INSERT INTO allowance_log (email, cycle, decision, evidence, anchor_day)"
" VALUES (?, ?, ?, ?, ?)",
(email, cycle_key, f"{decision}", evidence, anchor_day),
(email, cycle_key, decision, evidence, anchor_day),
)
conn.commit()
decisions.append({"email": email, "slug": slug, "anchor_day": anchor_day,
decisions.append({"email": email, "slug": oc_slug, "anchor_day": anchor_day,
"cycle": cycle_key, "decision": decision, "evidence": evidence})
conn.close()