panel state: a reminder is history, not a state — renewals clear grace, grace expires
ci/woodpecker/push/woodpecker Pipeline was canceled
ci/woodpecker/push/woodpecker Pipeline was canceled
mark@ofosos.org showed as in grace while his contribution was live. The sweep's own trail says it plainly: reminded 2026-11-04 for the cycle that looked unpaid, grace elapsed 2026-11-11, and `renewed` logged at 19:02 on 2026-11-13 (last charge 2026-11-11, next reset 2026-11-26). The panel said grace anyway, because member_membership_state() asked only "is there a row in payment_reminders?". That table is a HISTORY — kept so the sweep does not re-remind inside a cycle. It is not a state, and treating it as one broke two things: a reminder never expired (grace is PAYMENT_GRACE_DAYS long), and a renewal never cleared it. Now the LATEST reconciliation decision decides: renewed / would_renew -> active (they are paying) reminder inside the window -> in_grace reminder past the window -> awaiting_contribution, saying grace elapsed would_deactivate / deactivated -> awaiting_contribution Ordered by decided_at, not id: the same member had id 2296 dated 2026-11-11 next to id 2286 dated 2026-11-13, so "latest by id" reads the wrong decision. Reads still fail safe: an unreadable DB reports active, never a payment problem. tests/test_membership_state.py pins all of it, including the ordering trap.
This commit is contained in:
1 parent
b1f5a30272
commit
241ac800c6
2 files changed
+159
-11
No files matched your search
+37
-11
@@ -7064,34 +7064,60 @@ def member_membership_state(email: str, active: bool) -> tuple[str, str]:
|
||||
the portal rather than inventing policy of its own:
|
||||
|
||||
inactive — membership deactivated (members.active = 0).
|
||||
in_grace — a payment reminder has been sent for this member's
|
||||
current cycle and no contribution has arrived yet;
|
||||
access continues for PAYMENT_GRACE_DAYS.
|
||||
awaiting_contribution — the reconciliation flagged the member unpaid but no
|
||||
reminder has been sent yet.
|
||||
active — current / no open reminder.
|
||||
in_grace — a payment reminder went out and we are still inside
|
||||
the PAYMENT_GRACE_DAYS window.
|
||||
awaiting_contribution — the member is unpaid: either flagged with no
|
||||
reminder yet, or the grace window has elapsed
|
||||
(deactivation pending).
|
||||
active — current.
|
||||
|
||||
THE REMINDER TABLE IS A HISTORY, NOT A STATE. A row stays after the member
|
||||
pays, so "a reminder exists" is not "they are in grace". On 2026-11-13
|
||||
mark@ofosos.org showed as in grace while his contribution was live: reminded
|
||||
2026-11-04, grace elapsed 2026-11-11, and the reconciliation logged `renewed`
|
||||
for him at 19:02 that afternoon. The old version returned grace from the
|
||||
reminder row alone, forever.
|
||||
|
||||
So the LATEST RECONCILIATION DECISION decides, and a past reminder only
|
||||
matters when nothing since says they paid. Ordered by decided_at, not id:
|
||||
ids come from insertion order and a later-inserted row can carry an earlier
|
||||
timestamp (the same member had id 2296 dated 11-11 next to id 2286 dated
|
||||
11-13).
|
||||
"""
|
||||
if not active:
|
||||
return "inactive", "deactivated"
|
||||
try:
|
||||
conn = get_db()
|
||||
latest = conn.execute(
|
||||
"SELECT decision, decided_at FROM allowance_log WHERE email = ? "
|
||||
"ORDER BY decided_at DESC, id DESC LIMIT 1",
|
||||
(email,),
|
||||
).fetchone()
|
||||
rem = conn.execute(
|
||||
"SELECT sent_at FROM payment_reminders WHERE email = ? "
|
||||
"ORDER BY sent_at DESC LIMIT 1",
|
||||
(email,),
|
||||
).fetchone()
|
||||
log = conn.execute(
|
||||
"SELECT decision FROM allowance_log WHERE email = ? ORDER BY id DESC LIMIT 1",
|
||||
(email,),
|
||||
).fetchone()
|
||||
conn.close()
|
||||
except Exception: # tables absent / unreadable: fail safe
|
||||
return "active", ""
|
||||
decision = (latest[0] if latest else "") or ""
|
||||
if decision in ("renewed", "would_renew"):
|
||||
# They are paying. Any reminder belongs to an earlier cycle.
|
||||
return "active", ""
|
||||
if rem:
|
||||
sent = _parse_oc_dt(str(rem[0])) if rem[0] else None
|
||||
within = sent is not None and (datetime.now(timezone.utc) - sent) <= timedelta(days=PAYMENT_GRACE_DAYS)
|
||||
if within:
|
||||
return "in_grace", (f"awaiting contribution — reminder sent "
|
||||
f"{str(rem[0])[:10]}, grace {int(PAYMENT_GRACE_DAYS)}d")
|
||||
if log and log[0] in ("reminded", "would_remind", "remind_failed"):
|
||||
return "awaiting_contribution", (
|
||||
f"unpaid — reminder sent {str(rem[0])[:10]}, grace of "
|
||||
f"{int(PAYMENT_GRACE_DAYS)}d elapsed, deactivation pending")
|
||||
if decision in ("reminded", "would_remind", "remind_failed"):
|
||||
return "awaiting_contribution", "no current contribution at Open Collective"
|
||||
if decision in ("would_deactivate", "deactivated"):
|
||||
return "awaiting_contribution", f"grace elapsed ({decision})"
|
||||
return "active", ""
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,122 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Regression test: the panel's membership state must not call a paying member "grace".
|
||||
|
||||
WHY THIS EXISTS
|
||||
On 2026-11-13 mark@ofosos.org showed as in grace. He was current: the reminder
|
||||
went out 2026-11-04 for the cycle that looked unpaid, his grace window elapsed
|
||||
2026-11-11, and the reconciliation logged `renewed` for him at 19:02 that
|
||||
afternoon — his next charge is 2026-11-26, which had not arrived and was not
|
||||
supposed to.
|
||||
|
||||
The state function asked "is there a row in payment_reminders?" and stopped
|
||||
there. That table is a HISTORY, kept so the sweep does not re-remind inside a
|
||||
cycle; it is not a state. Two bugs followed: a reminder never expired (grace is
|
||||
PAYMENT_GRACE_DAYS long), and a renewal never cleared it.
|
||||
|
||||
Ordering matters too: the same member had allowance_log id 2296 dated 2026-11-11
|
||||
next to id 2286 dated 2026-11-13, so "latest by id" reads the WRONG decision.
|
||||
"""
|
||||
import os
|
||||
import sys
|
||||
import tempfile
|
||||
import pathlib
|
||||
from datetime import datetime, timedelta, timezone
|
||||
|
||||
ROOT = pathlib.Path(__file__).resolve().parent.parent
|
||||
sys.path.insert(0, str(ROOT))
|
||||
os.environ["DB_PATH"] = os.path.join(tempfile.mkdtemp(), "state-fixture.db")
|
||||
|
||||
import app.main as m # noqa: E402
|
||||
|
||||
m.DB_PATH = os.environ["DB_PATH"]
|
||||
|
||||
FAILS = []
|
||||
def check(cond, msg):
|
||||
print((" ok " if cond else " FAIL ") + msg)
|
||||
if not cond:
|
||||
FAILS.append(msg)
|
||||
|
||||
NOW = datetime.now(timezone.utc)
|
||||
def stamp(dt):
|
||||
return dt.strftime("%Y-%m-%d %H:%M:%S")
|
||||
|
||||
conn = m.get_db()
|
||||
m._ensure_allowance_tables(conn)
|
||||
conn.execute("DELETE FROM payment_reminders")
|
||||
conn.execute("DELETE FROM allowance_log")
|
||||
|
||||
EMAIL = "mark@ofosos.org"
|
||||
conn.execute("INSERT OR REPLACE INTO members (email, active) VALUES (?, 1)", (EMAIL,))
|
||||
|
||||
print("1. a RENEWAL after a reminder means active — the actual bug")
|
||||
conn.execute("INSERT INTO payment_reminders (email, cycle, sent_at) VALUES (?,?,?)",
|
||||
(EMAIL, "2026-10-26", stamp(NOW - timedelta(days=9)))) # reminded 9d ago
|
||||
conn.execute("INSERT INTO allowance_log (email, cycle, decision, decided_at) VALUES (?,?,?,?)",
|
||||
(EMAIL, "2026-10-26", "in_grace", stamp(NOW - timedelta(days=9))))
|
||||
conn.execute("INSERT INTO allowance_log (email, cycle, decision, decided_at) VALUES (?,?,?,?)",
|
||||
(EMAIL, "2026-10-26", "renewed", stamp(NOW - timedelta(hours=2))))
|
||||
conn.commit()
|
||||
state, why = m.member_membership_state(EMAIL, True)
|
||||
check(state == "active", f"paid member reads active, not grace (got {state!r}: {why})")
|
||||
|
||||
print("2. the LATEST decision wins, even when an older row has a higher id")
|
||||
conn.execute("DELETE FROM allowance_log")
|
||||
conn.execute("INSERT INTO allowance_log (email, cycle, decision, decided_at) VALUES (?,?,?,?)",
|
||||
(EMAIL, "2026-10-26", "renewed", stamp(NOW - timedelta(hours=2)))) # id 1
|
||||
conn.execute("INSERT INTO allowance_log (email, cycle, decision, decided_at) VALUES (?,?,?,?)",
|
||||
(EMAIL, "2026-10-26", "in_grace", stamp(NOW - timedelta(days=9)))) # id 2, OLDER date
|
||||
conn.commit()
|
||||
state, why = m.member_membership_state(EMAIL, True)
|
||||
check(state == "active",
|
||||
f"an older-dated row inserted later does not override the renewal (got {state!r}: {why})")
|
||||
|
||||
print("3. a reminder INSIDE the grace window still reads in_grace")
|
||||
conn.execute("DELETE FROM allowance_log")
|
||||
conn.execute("UPDATE payment_reminders SET sent_at = ? WHERE email = ?",
|
||||
(stamp(NOW - timedelta(days=2)), EMAIL))
|
||||
conn.commit()
|
||||
state, why = m.member_membership_state(EMAIL, True)
|
||||
check(state == "in_grace", f"two days into a {int(m.PAYMENT_GRACE_DAYS)}-day grace reads in_grace (got {state!r})")
|
||||
|
||||
print("4. grace EXPIRES — no reminder row may mean grace forever")
|
||||
conn.execute("UPDATE payment_reminders SET sent_at = ? WHERE email = ?",
|
||||
(stamp(NOW - timedelta(days=int(m.PAYMENT_GRACE_DAYS) + 3)), EMAIL))
|
||||
conn.commit()
|
||||
state, why = m.member_membership_state(EMAIL, True)
|
||||
check(state == "awaiting_contribution", f"past the window reads awaiting, not grace (got {state!r})")
|
||||
check("elapsed" in why, f"and says the grace elapsed (got {why!r})")
|
||||
|
||||
print("5. a previewed deactivation is not grace either")
|
||||
conn.execute("INSERT INTO allowance_log (email, cycle, decision, decided_at) VALUES (?,?,?,?)",
|
||||
(EMAIL, "2026-10-26", "would_deactivate", stamp(NOW - timedelta(hours=1))))
|
||||
conn.commit()
|
||||
state, why = m.member_membership_state(EMAIL, True)
|
||||
check(state == "awaiting_contribution", f"would_deactivate reads awaiting (got {state!r})")
|
||||
|
||||
print("6. nobody flagged is simply active")
|
||||
conn.execute("DELETE FROM payment_reminders")
|
||||
conn.execute("DELETE FROM allowance_log")
|
||||
conn.commit()
|
||||
state, why = m.member_membership_state(EMAIL, True)
|
||||
check(state == "active" and why == "", f"clean member reads active with no reason (got {state!r}, {why!r})")
|
||||
|
||||
print("7. inactive is still inactive")
|
||||
check(m.member_membership_state(EMAIL, False)[0] == "inactive", "active=0 reads inactive")
|
||||
|
||||
print("8. an unreadable database fails SAFE, not into a grace badge")
|
||||
real = m.get_db
|
||||
try:
|
||||
def boom():
|
||||
raise RuntimeError("db locked")
|
||||
m.get_db = boom
|
||||
state, why = m.member_membership_state(EMAIL, True)
|
||||
check(state == "active", f"a failed read does not invent a payment problem (got {state!r})")
|
||||
finally:
|
||||
m.get_db = real
|
||||
|
||||
conn.close()
|
||||
print()
|
||||
if FAILS:
|
||||
print(f"membership state: {len(FAILS)} FAILURE(S)")
|
||||
sys.exit(1)
|
||||
print("membership state: OK — renewals clear grace, grace expires, reads fail safe")
|
||||
Reference in new issue
Block a user