From 64d1ea5093fe3352dd7ea4f901cfaaa225029090 Mon Sep 17 00:00:00 2001 From: Inferencebot Date: Fri, 13 Nov 2026 22:24:46 +0000 Subject: [PATCH] members: an explicit SOURCE, so 'oc-incognito' stops being 'manual' MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit mmoya@mmoya.org sat in MANUAL_MEMBERS because OC hides the address on their profile (incognito-5ad60182, email null since 2023), so email matching can never find them. But "manual" means hand-provisioned, and the sweep SKIPS manual members entirely: a paying subscriber had no lapse logic, no dues tracking and no order attribution. Two different facts were sharing one word. members.source now carries which one it is: oc — OC-backed, matched by email (the default; NULL means this) oc-incognito — OC-backed, profile hides the email, matched by the stored OC SLUG — which the reconciliation already uses, since fetch_member_orders() groups orders by the payer's slug and members.slug holds exactly that for these people manual — hand-provisioned; no contribution expected, nothing reconciles, nobody lapses So an oc-incognito member needs no exemption from reconciliation — they need to stop being skipped, and the existing slug path does the rest. Every decision that keyed on the raw MANUAL_MEMBERS list now keys on the source: the reconciliation skip, the sweep's deactivation guard, and the shortfall-credit exemption. An explicit source beats the list, so a member can be moved without editing the env — and the env list can shrink toward deprecation. An unrecognised or unreadable source falls back to 'oc', the direction that keeps reconciling someone rather than quietly exempting them. anchor_day_for_member also resolves by slug now: an incognito member is absent from the member index (it drops anyone without an email), and falling back to "today" would silently move their renewal day. tests/test_oc_incognito.py pins all of it, including that the column is optional (NULL = not stated) so this ships without a data migration. --- app/main.py | 102 ++++++++++++++++++++++++++++++-- tests/test_oc_incognito.py | 118 +++++++++++++++++++++++++++++++++++++ 2 files changed, 216 insertions(+), 4 deletions(-) create mode 100644 tests/test_oc_incognito.py diff --git a/app/main.py b/app/main.py index bfdfd3c..bfeeb90 100644 --- a/app/main.py +++ b/app/main.py @@ -408,6 +408,72 @@ INACTIVE_GROUP_ID = os.environ.get("INACTIVE_GROUP_ID", "") # Open Collective member list. Used for the free "founder" tier, which OC # cannot process as a $0 recurring subscription. These members are exempt from # the sweep's deactivation (they have no OC membership to lapse). +# How a member's membership is ACCOUNTED FOR. This exists because "manual" was +# doing two incompatible jobs (2026-11-13): +# oc — OC-backed, matched by email. The default. +# oc-incognito — OC-backed, but the OC profile HIDES its email (mmoya@mmoya.org), +# so email matching can never see them. The contribution is real +# and the reconciliation already matches orders by the payer's +# SLUG, which is what the members.slug column holds for these +# people — so they keep lapse logic, dues tracking and order +# attribution, and are NOT swept into the manual bucket. +# manual — hand-provisioned (MANUAL_MEMBERS): no OC contribution is +# expected, so nothing reconciles and nobody lapses. +# A row with no explicit source falls back to MANUAL_MEMBERS, so this changed +# nothing for anyone until a source was actually set. +MEMBER_SOURCES = ("oc", "oc-incognito", "manual") + + +def member_source(email: str, conn=None) -> str: + """This member's accounting source. Never raises: an unreadable row is 'oc' + (the default), which is the safe direction — it means we keep reconciling them + rather than quietly exempting them from it.""" + email = (email or "").strip().lower() + src = "" + try: + own = conn is None + conn = conn or get_db() + try: + cols = {r[1] for r in conn.execute("PRAGMA table_info(members)")} + if "source" in cols: + row = conn.execute("SELECT source FROM members WHERE lower(email) = ?", + (email,)).fetchone() + src = ((row[0] if row else "") or "").strip().lower() + finally: + if own: + conn.close() + except Exception as e: + logger.warning("member_source(%s): %s", email, e) + if src in MEMBER_SOURCES: + return src + return "manual" if email in MANUAL_MEMBERS else "oc" + + +def is_manual_member(email: str, conn=None) -> bool: + """Hand-provisioned: the set MANUAL_MEMBERS names, minus anyone given an + explicit source of their own (an oc-incognito member is OC-backed even though + email matching cannot see them).""" + return member_source(email, conn) == "manual" + + +def set_member_source(email: str, source: str) -> bool: + """Record a member's source. Idempotent; returns True when the row was written.""" + source = (source or "").strip().lower() + if source not in MEMBER_SOURCES: + raise ValueError(f"unknown source {source!r}; expected one of {MEMBER_SOURCES}") + conn = get_db() + try: + cols = {r[1] for r in conn.execute("PRAGMA table_info(members)")} + if "source" not in cols: + conn.execute("ALTER TABLE members ADD COLUMN source TEXT") + cur = conn.execute("UPDATE members SET source = ? WHERE lower(email) = ?", + (source, (email or "").strip().lower())) + conn.commit() + return cur.rowcount > 0 + finally: + conn.close() + + MANUAL_MEMBERS = { e.strip().lower() for e in os.environ.get("MANUAL_MEMBERS", "").split(",") @@ -1031,6 +1097,10 @@ def get_db() -> sqlite3.Connection: # only by their own rules (credit exhausted, or the GUEST_DAYS time box). if "account_type" not in cols: conn.execute("ALTER TABLE members ADD COLUMN account_type TEXT DEFAULT 'member'") + # Migration: source — how this membership is accounted for (see + # MEMBER_SOURCES). NULL means "not stated": fall back to MANUAL_MEMBERS. + if "source" not in cols: + conn.execute("ALTER TABLE members ADD COLUMN source TEXT") # Migration: oc_email — the Open Collective email a member's contribution # arrives under when it differs from their account email (set via # POST /admin/guest-convert when a invited friend joins under another @@ -5226,7 +5296,8 @@ async def admin_overview(request: Request): "active": bool(active), "state": mstate, "state_reason": mstate_reason, - "manual": email.lower() in MANUAL_MEMBERS, + "manual": member_source(email) == "manual", + "source": member_source(email), "org": org_slug or None, "matrix_mxid": matrix_mxid or None, "balance": float(balance) if balance is not None else MEMBER_BUDGET, @@ -6948,7 +7019,7 @@ async def reconcile_memberships() -> dict: if info["account_type"] == "guest": continue user_id = info["user_id"] - if email not in active_emails and email not in MANUAL_MEMBERS: + if email not in active_emails and not is_manual_member(email): try: if INACTIVE_GROUP_ID and user_id: await cloudron_set_group(user_id, INACTIVE_GROUP_ID) @@ -7516,6 +7587,25 @@ async def anchor_day_for_member(email: str, slug: str = "") -> int: return d except Exception as e: logger.warning("anchor_day_for_member: OC join-date lookup failed for %s: %s", email, e) + if slug: + # An oc-incognito member is absent from the member index (it drops anyone + # without an email), but their orders carry the slug we store. Their + # billing rhythm still anchors the cycle — falling back to "today" would + # silently move their renewal day. + try: + orders = (await fetch_member_orders()).get(slug) or [] + joined = min((_parse_oc_dt(str(o.get("createdAt"))) for o in orders + if _parse_oc_dt(str(o.get("createdAt")))), default=None) + if joined: + d = _clamp_anchor_day(joined.day) + conn = get_db() + conn.execute("UPDATE members SET anchor_day = ? WHERE email = ?", (d, email)) + conn.commit() + conn.close() + logger.info("anchor_day_for_member: %s anchored to %d from their OC order", email, d) + return d + except Exception as e: + logger.warning("anchor_day_for_member: order lookup failed for %s/%s: %s", email, slug, e) return _clamp_anchor_day(datetime.now(timezone.utc).day) @@ -7600,7 +7690,11 @@ async def reconcile_allowances(send: bool = False) -> dict: # renewal decision, no reminder, ever. counts["skipped"] += 1 continue - if email.lower() in MANUAL_MEMBERS: + if member_source(email, conn) == "manual": + # Hand-provisioned: no OC contribution is expected, so there is + # nothing to reconcile. An 'oc-incognito' member is NOT skipped — + # their contribution is matched by the slug in row[1] below, which is + # the only key OC gives us for them. counts["skipped"] += 1 continue if org_slug: @@ -8907,7 +9001,7 @@ async def reconcile_allowance_shortfalls(send: bool = False) -> dict: skipped.append({**c, "email": email, "why": "not an active portal member"}) continue m = meta.get(email) or {} - if email in MANUAL_MEMBERS or m.get("org_slug"): + if is_manual_member(email) or m.get("org_slug"): skipped.append({**c, "email": email, "why": "exempt member (manual / organization-backed)"}) continue rec = index.get(email) diff --git a/tests/test_oc_incognito.py b/tests/test_oc_incognito.py new file mode 100644 index 0000000..66f1c0f --- /dev/null +++ b/tests/test_oc_incognito.py @@ -0,0 +1,118 @@ +#!/usr/bin/env python3 +"""Regression test: 'oc-incognito' — an OC payer whose OC profile hides its email. + +WHY THIS EXISTS + mmoya@mmoya.org sat in MANUAL_MEMBERS because OC hides the address on their + profile (incognito-5ad60182, email null since 2023), so email matching can never + find them. But "manual" means hand-provisioned, and the sweep SKIPS manual + members entirely — so a paying subscriber had no lapse logic, no dues tracking + and no order attribution. Two different facts were sharing one word. + + An oc-incognito member needs no exemption from reconciliation: the partition + already matches OC orders by the PAYER'S SLUG, and members.slug holds exactly + that slug for these people. What they need is to stop being skipped. + + The invariants pinned here: + * an explicit source beats MANUAL_MEMBERS membership — that is the whole + point; mmoya must not read as manual just because their address is listed; + * only 'manual' is exempt (lapse guard, reconciliation skip, shortfall + credits). 'oc-incognito' is a payer and gets payer treatment; + * an unreadable or unexpected source falls back to 'oc' — the direction that + keeps reconciling someone rather than quietly exempting them; + * the source column is optional (every existing row is NULL, meaning + "not stated"), so this ships without a data migration. +""" +import os +import pathlib +import sys +import tempfile + +ROOT = pathlib.Path(__file__).resolve().parent.parent +sys.path.insert(0, str(ROOT)) +os.environ["DB_PATH"] = os.path.join(tempfile.mkdtemp(), "source-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) + +MMOYA = "mmoya@mmoya.org" +OTHER = "someone@example.org" + +conn = m.get_db() +# The column arrives with get_db()'s own migration — asserted, not created here, +# because "the migration ran" is part of what this test is for. +check_cols = {r[1] for r in conn.execute("PRAGMA table_info(members)")} +assert "source" in check_cols, "get_db() must migrate in members.source" +conn.execute("INSERT OR REPLACE INTO members (email, slug, active) VALUES (?,?,1)", + (MMOYA, "incognito-5ad60182")) +conn.execute("INSERT OR REPLACE INTO members (email, slug, active) VALUES (?,?,1)", + (OTHER, "guest-abc123")) +conn.commit() +conn.close() + +m.MANUAL_MEMBERS = {MMOYA, "founder@example.org"} + +print("1. the fallback: a listed address with no source of its own is manual") +check(m.member_source(MMOYA) == "manual", + "an address in MANUAL_MEMBERS and nothing else reads manual (before the flip)") +check(m.is_manual_member(MMOYA) is True, "…and is exempt as such") + +print("2. an explicit source BEATS the manual list — this is the mmoya case") +check(m.set_member_source(MMOYA, "oc-incognito") is True, "the source is written") +check(m.member_source(MMOYA) == "oc-incognito", + f"mmoya reads oc-incognito even while listed in MANUAL_MEMBERS (got {m.member_source(MMOYA)!r})") +check(m.is_manual_member(MMOYA) is False, + "and is therefore NOT exempt: a paying member gets payer treatment") + +print("3. everyone else is unchanged") +check(m.member_source(OTHER) == "oc", "an unlisted member is oc (the default)") +check(m.member_source("founder@example.org") == "manual", "an unlisted-in-DB manual address stays manual") +check(m.member_source("nobody@nowhere.test") == "oc", "an address with no row at all is oc, not manual") + +print("4. a bad source value never silently exempts anyone") +conn = m.get_db() +conn.execute("UPDATE members SET source = 'banana' WHERE lower(email) = ?", (OTHER,)) +conn.commit(); conn.close() +check(m.member_source(OTHER) == "oc", + f"an unrecognised source falls back to oc — the direction that keeps reconciling (got {m.member_source(OTHER)!r})") +try: + m.set_member_source(OTHER, "banana") + check(False, "set_member_source must reject an unknown source") +except ValueError: + check(True, "set_member_source rejects an unknown source") + +print("5. the three decisions that used to key on MANUAL_MEMBERS now key on the source") +src = (ROOT / "app" / "main.py").read_text() +recon = src[src.index("async def reconcile_allowances"):] +recon = recon[:recon.index("def ", recon.index("for row in rows"))] +check('member_source(email, conn) == "manual"' in recon, + "the reconciliation skips only source == manual") +check("in MANUAL_MEMBERS" not in recon, + "and no longer keys on the raw list (an oc-incognito member must not be skipped)") +check("not is_manual_member(email)" in src, + "the sweep's deactivation guard uses the source too") +check("is_manual_member(email) or m.get(\"org_slug\")" in src, + "shortfall credits exempt manual + org-backed, nothing else") +check('"source": member_source(email)' in src, + "the panel payload carries the source (so it can stop saying 'manual')") + +print("6. the column is optional — existing rows carry NULL and still work") +conn = m.get_db() +conn.execute("UPDATE members SET source = NULL WHERE lower(email) = ?", (MMOYA,)) +conn.commit(); conn.close() +m.MANUAL_MEMBERS = {"founder@example.org"} # the address leaves the list +check(m.member_source(MMOYA) == "oc", + "once mmoya leaves MANUAL_MEMBERS with no source set, they read oc — which is why the " + "source is set BEFORE the address is removed from the env") + +print() +if FAILS: + print(f"oc-incognito: {len(FAILS)} FAILURE(S)") + sys.exit(1) +print("oc-incognito: OK — an explicit source wins, only manual is exempt, bad values fail safe")