unmapped backers: the paid-but-unprovisioned net moves into the portal
ci/woodpecker/push/woodpecker Pipeline was canceled

A contributor who pays and never gets an account is invisible to the sweep —
fetch_members() drops every member node with no email, and an OC Organization
never exposes one — so they wait until they ask (the mmoya case). The net for them
was a daily script on the bot's host driving `cloudron exec` from outside. The
portal holds both sides of that comparison, so it now owns it.

reconcile_unmapped_backers() runs in the sweep and reports three classes, because
each needs a different human action: an org account (link it), a VISIBLE email
(the sweep should have provisioned this — check the token/sweep), and a hidden
email (fetch it out-of-band). A node with no account object is attributed through
same-day current orders, and stays silent when every same-day payer is mapped.

MAIL POLICY (ntnsndr, 2026-11-13): only when there is a problem — and only when
the finding SET changes, fingerprinted in portal_state (slug+class only, never an
email), so a stuck finding does not mail three times a day while a fresh one
always speaks. A clean run clears the marker. A failed read returns skipped,
never a clean bill.

POST /admin/unmapped-check (X-Admin-Token, ?notify=0 to stay quiet). The
credit-pack reconcile now mails on the same rule. 25 checks; all 19 suites green.
This commit is contained in:
inference-admin committed 2026-11-13 20:30:46 +00:00
1 parent d117da3dc6
commit eaaabf3763
2 files changed
+506 -3

No files matched your search

+264 -3
View File
@@ -7846,6 +7846,7 @@ CREDIT_RECONCILE = (os.environ.get("CREDIT_RECONCILE", "report") or "report").st
# whole morning, so a log-derived window cannot see the failures it exists for.
CREDIT_RECONCILE_DAYS = int(os.environ.get("CREDIT_RECONCILE_DAYS", "30") or "30")
CREDIT_RECONCILE_MAX_PAGES = int(os.environ.get("CREDIT_RECONCILE_MAX_PAGES", "3") or "3")
CREDIT_RECONCILE_MARKER = "credit_reconcile_fingerprint"
async def fetch_collective_orders(per_page: int = 100, max_pages: int = 3) -> list[dict]:
@@ -8006,7 +8007,202 @@ def _nearby_book_rows(slug: str, email: str, when: datetime | None, ledger_rows:
return out[:cap]
async def reconcile_credit_packs(apply: bool = False) -> dict:
# --- Unmapped-backer check (was a script on the bot host) ------------------- #
# The failure mode (the mmoya case, 2026-10/11): a contributor pays the
# collective, but their OC account hides their email or belongs to an
# ORGANIZATION, so reconcile_memberships() cannot map them to a portal row and
# they are never provisioned — they wait until they ask. The sweep can only
# provision what fetch_members() can SEE (it drops nodes with no email); this
# reads the RAW member nodes, including the ones the sweep must skip, and
# compares them with members.db.
#
# It used to be a daily script on the bot's host, driving `cloudron exec` from
# outside. It belongs here: the portal holds both sides of the comparison, and a
# member waiting for an account must not depend on the bot being up.
CURRENT_ORDER_STATUS = {"ACTIVE", "PAID"} # an order that is paying right now
UNMAPPED_BACKERS_MARKER = "unmapped_backers_fingerprint"
def _oc_memberish(node: dict) -> bool:
"""Mirror fetch_members()'s member definition (ADMIN, or Membership/legacy tier)."""
role = node.get("role") or ""
if role not in ("BACKER", "ADMIN"):
return False
tier = (((node.get("tier") or {}) or {}).get("name") or "").strip()
return role == "ADMIN" or not tier or "membership" in tier.lower()
async def reconcile_unmapped_backers(notify: bool = True) -> dict:
"""Paid OC contributors with no portal row. Emails info@ only when there are findings.
Silent when everything is mapped, and — once a finding has been reported — quiet
while the SAME set persists, because this runs three times a day and a stuck
finding would otherwise mail every time. The fingerprint changes when a new
finding appears or an old one is fixed, and a recurrence after a clean run
emails again (the marker is cleared on a clean run).
A read that fails reports `skipped`, never a clean result: "could not look" is
not the same as "nothing wrong".
"""
try:
nodes = await fetch_all_member_nodes()
except Exception as e: # noqa: BLE001
logger.warning("unmapped-backer check: member fetch failed; skipping: %s", e)
return {"status": "skipped", "reason": "member_fetch_failed", "detail": str(e)[:200]}
try:
orders = await fetch_collective_orders()
except Exception as e: # noqa: BLE001
# A finding without dues context is still a finding; never lose it over this.
logger.warning("unmapped-backer check: order fetch failed (%s); findings omit dues", e)
orders = []
conn = get_db()
rows = [(r[0], r[1], r[2]) for r in
conn.execute("SELECT email, slug, org_slug FROM members")]
conn.close()
portal_emails = {str(e).strip().lower() for e, _s, _o in rows if e and "@" in str(e)}
portal_slugs = {str(s).strip() for _e, s, _o in rows if s}
portal_org_slugs = {str(o).strip() for _e, _s, o in rows if o}
def mapped(email, slug) -> bool:
if email and str(email).strip().lower() in portal_emails:
return True
return bool(slug) and (slug in portal_slugs or slug in portal_org_slugs)
def order_line(slug: str) -> str:
cands = [o for o in orders
if ((o.get("fromAccount") or {}).get("slug") or "") == slug]
if not cands:
return "no order found in the order list"
cands.sort(key=lambda o: str(o.get("createdAt") or ""))
ranked = ([o for o in cands if (o.get("status") or "") == "ACTIVE"]
or [o for o in cands if (o.get("status") or "") == "PAID"] or cands)
o = ranked[-1]
return (f"${(o.get('totalAmount') or {}).get('value')} {o.get('frequency')} "
f"{o.get('status')} (last order {str(o.get('createdAt') or '')[:10]})")
findings: list[dict] = []
seen: set[str] = set()
# Pass 1: account-bearing nodes (rich findings). Pass 2: null-account nodes,
# attributed through same-day orders.
for with_account in (True, False):
for n in nodes:
acct = n.get("account")
if with_account != bool(acct):
continue
if not _oc_memberish(n):
continue # e.g. a Credit-pack-only BACKER: the held-credits flow, not a member
if acct:
slug = (acct.get("slug") or "").strip()
email = (acct.get("email") or "").strip()
if mapped(email, slug):
continue
kind = (acct.get("type") or "INDIVIDUAL").upper()
name = acct.get("name") or slug or "(no name)"
if kind == "ORGANIZATION":
cls, vis = "org", "org email never visible (OC design, even for admins)"
action = ("org-backed membership — OC never exposes an org email. Link it: "
'POST /admin/org-link {"org_slug": "<slug>", "email": '
'"<preferred email>", "name": "<name>"} (X-Admin-Token)')
elif email:
cls, vis = "visible", f"VISIBLE to the admin view ({email})"
action = ("the email IS visible, so the sweep should have provisioned this "
"within 24h — check the sweep and the OC token "
"(POST /admin/provision re-provisions by hand)")
else:
cls, vis = "hidden", "HIDDEN from the admin view (null email)"
action = ("email hidden, so reconcile cannot map it. Get it out-of-band "
"(contribution page as collective admin) and provision with "
"POST /admin/provision on the operator's instruction")
key = slug or f"no-slug:{n.get('role')}"
if key in seen:
continue
seen.add(key)
findings.append({
"slug": slug, "name": name, "type": kind, "role": n.get("role"),
"tier": (((n.get("tier") or {}) or {}).get("name") or "none"),
"email_visibility": vis, "dues": order_line(slug), "action": action,
"class": cls,
"incognito_alias": slug.startswith("incognito-"),
})
else:
# No account object even in the ADMIN view (deleted, or
# incognito-hidden). Attribute via same-day current orders; stay
# silent when every same-day payer is already mapped.
day = str(n.get("createdAt") or "")[:10]
current = [o for o in orders
if str(o.get("createdAt") or "")[:10] == day
and (o.get("status") or "") in CURRENT_ORDER_STATUS]
unmapped = sorted({
((o.get("fromAccount") or {}).get("slug") or "")
for o in current
if not mapped(None, ((o.get("fromAccount") or {}).get("slug") or ""))})
if current and not unmapped:
continue
key = f"null-account:{day}"
if key in seen:
continue
seen.add(key)
findings.append({
"slug": "", "name": "(no account object in the admin view)",
"type": "UNKNOWN", "role": n.get("role"), "tier": "none",
"email_visibility": "no account object returned",
"dues": ", ".join(s for s in unmapped if s) or "no same-day order identified",
"action": ("a member node with no account object — attribute it from the "
"same-day orders above and provision by hand"),
"class": "null-account",
"incognito_alias": False,
})
# Fingerprint the SET (class + slug only: never a member's email) so the same
# unresolved finding is reported once, and a fixed one is not remembered.
fingerprint = "|".join(sorted(f"{f['slug']}:{f['class']}" for f in findings))[:400]
conn = get_db()
previous = _state_get(conn, UNMAPPED_BACKERS_MARKER, "")
if fingerprint != previous:
_state_set(conn, UNMAPPED_BACKERS_MARKER, fingerprint)
conn.commit()
conn.close()
if findings:
logger.warning("unmapped backers: %d paid contributor(s) with no portal row "
"(set %s): %s", len(findings), fingerprint or "-",
"; ".join(f["slug"] or f["name"] for f in findings))
reported = False
if findings and fingerprint != previous and notify:
lines = []
for f in findings:
tag = " [incognito alias]" if f["incognito_alias"] else ""
lines.append(
f"- slug={f['slug'] or '?'}{tag} ({f['name']}) {f['type']} "
f"role={f['role']} tier={f['tier']}\n"
f" email: {f['email_visibility']}\n"
f" dues: {f['dues']}\n"
f" action: {f['action']}")
try:
send_email(
"info@inference.coop",
f"Inference Cooperative — {len(findings)} paid contributor(s) with no account",
"These Open Collective contributors look like members, but no portal row "
"matches them, so nothing has provisioned an account:\n\n"
+ "\n".join(lines)
+ "\n\nThis is sent when the SET changes, not on every sweep. A contributor "
"who has just joined may also be caught by the next membership sweep "
"(3x/day); if they are still listed after that, the sweep cannot see them "
"and someone has to act.\n",
)
reported = True
except Exception as e: # noqa: BLE001
logger.warning("unmapped-backer notice email failed: %s", e)
return {"status": "ok", "findings": len(findings), "classes":
sorted({f["class"] for f in findings}), "fingerprint": fingerprint,
"reported": reported, "detail": findings}
async def reconcile_credit_packs(apply: bool = False, notify: bool = True) -> dict:
"""Credit PAID credit packs that never reached our books (issue #7).
`apply=False` (report) is the default and changes NOTHING; granting also
@@ -8131,6 +8327,47 @@ async def reconcile_credit_packs(apply: bool = False) -> dict:
if c.get("nearby_book_rows") else "")
for c in candidates))
# Tell a human when money needed attention — and ONLY when the set changes, so
# a finding that stays put does not mail three times a day. Silence when there
# is nothing to say, which is the normal case.
fingerprint = "|".join(sorted(
[f"{c['order_id']}:grant" for c in candidates]
+ [f"{c['order_id']}:hold" for c in needs_hold]))[:400]
conn = get_db()
previous = _state_get(conn, CREDIT_RECONCILE_MARKER, "")
if fingerprint != previous:
_state_set(conn, CREDIT_RECONCILE_MARKER, fingerprint)
conn.commit()
conn.close()
notified = False
if fingerprint and fingerprint != previous and notify:
granted = "\n".join(
f" granted ${c['amount']:.2f} to {c['slug']} for order {c['order_id']} "
f"({c['created_at']}) — {c.get('result', 'not applied (report mode)')}"
for c in candidates) or " (nothing granted)"
held = "\n".join(
f" NOT granted: ${c['amount']:.2f} bought by {c['slug']} for order "
f"{c['order_id']} ({c['created_at']}) — {c['reason']}" for c in needs_hold)
try:
send_email(
"info@inference.coop",
f"Inference Cooperative — paid credit pack(s) missing from the ledger "
f"({len(candidates)}/{len(needs_hold)})",
"A credit pack that Open Collective recorded as PAID had no matching entry "
"in our books. Credits are normally granted by the Open Collective webhook; "
"this pass reads Open Collective's own order list every sweep, so a purchase "
"whose delivery failed is credited without anyone having to notice.\n\n"
+ granted
+ (("\n\n" + held + "\n\n(These buyers have no active membership, so they keep "
"the hold-and-email path — nothing was granted.)")
if needs_hold else "")
+ "\n\nThis notice is sent when the set changes, not on every sweep.\n",
)
notified = True
except Exception as e: # noqa: BLE001
logger.warning("Credit reconcile: notice email failed: %s", e)
return {
"status": "ok",
"dry_run": not acting,
@@ -8141,6 +8378,7 @@ async def reconcile_credit_packs(apply: bool = False) -> dict:
"candidates": candidates,
"needs_hold": needs_hold,
"applied": applied,
"notified": notified,
}
@@ -8351,6 +8589,15 @@ async def admin_sweep(request: Request):
except Exception as exc: # noqa: BLE001
result["credit_reconcile"] = {"error": str(exc)[:200]}
# Paid Open Collective contributors with no portal row — the net for members
# the sweep cannot see (hidden email, org account). Emails info@ only when it
# finds something, and only when the set changes. Was a daily script on the
# bot's host.
try:
result["unmapped_backers"] = await reconcile_unmapped_backers()
except Exception as exc: # noqa: BLE001
result["unmapped_backers"] = {"error": str(exc)[:200]}
# Deliveries the portal refused. The portal records these itself (see
# record_webhook_failure) and reports them here ONCE per batch, so the alert
# for a failed delivery belongs to the system rather than to a log scraper on
@@ -8365,7 +8612,7 @@ async def admin_sweep(request: Request):
@app.post("/admin/credit-pack-reconcile")
@limiter.limit("20/minute")
async def admin_credit_pack_reconcile(request: Request, apply: int = 0):
async def admin_credit_pack_reconcile(request: Request, apply: int = 0, notify: int = 1):
"""Reconcile PAID credit packs against our books — DRY RUN by default.
Authenticated by the X-Admin-Token header, like the other /admin/* routes.
@@ -8374,9 +8621,23 @@ async def admin_credit_pack_reconcile(request: Request, apply: int = 0):
this pass's output in production before any member's money is touched. Every
grant uses the same per-order reference the webhook writes, so running it
twice — or running it after a late delivery — credits a purchase once.
`?notify=0` suppresses the info@ notice (for a check that should not mail).
"""
_verify_admin_token(request)
return await reconcile_credit_packs(apply=bool(apply))
return await reconcile_credit_packs(apply=bool(apply), notify=bool(notify))
@app.post("/admin/unmapped-check")
@limiter.limit("20/minute")
async def admin_unmapped_check(request: Request, notify: int = 1):
"""Run the paid-but-unprovisioned check on demand (X-Admin-Token).
Emits info@ only when there are findings AND the set has changed, so this is
safe to call repeatedly; `?notify=0` suppresses the notice entirely.
"""
_verify_admin_token(request)
return await reconcile_unmapped_backers(notify=bool(notify))
def _service_sync_pending() -> list[str]: