service invites: per-room Matrix gate, resumable rate limits, Loomio record heal, 5-min sync tick

A new member could end up with a Loomio account and a Matrix account and see
NOTHING: no forum group, no Space. Three defects:

* sync_matrix_space() gated each member on the SPACE's membership, so an
  account already joined/invited to the Space was skipped for EVERY room. A
  member whose invite pass was interrupted (or who predates a room added to
  MATRIX_INVITE_ROOMS later) never received that room — and a restricted child
  room is invisible until you are invited to it. The gate is now per room, from
  a single up-front membership snapshot; a room whose membership cannot be read
  is left alone rather than invited into blindly.
* a 429 slept for retry_after_ms (minutes) INSIDE the pass, so a scheduled run
  could outlive its own tick. A rate limit now ENDS the pass; the next run
  resumes (every invite is idempotent).
* a member sitting in the Loomio group with no loomio_invited_at stamp reads as
  'never invited' — the dedupe guard was only as good as the rows it had. The
  sync now heals the record from the live roster.

And the activation window itself: provisioning skips an unactivated account by
design, and activation happens later with nobody watching, so the 3x/day sweep
was the pickup. New /admin/service-sync + a 5-minute scheduler task fires the
same idempotent syncs while any member still awaits an invite (one SELECT when
none do), and deliberately is NOT /admin/sweep — it never touches Open
Collective, allowance logic or member mail.

tests/test_service_invites.py proves the per-room gate, resumable rate limits,
the unreadable-room skip, the record heal and the endpoint's idle/run paths.
This commit is contained in:
inference-admin committed 2026-11-13 17:37:57 +00:00
1 parent de0f5c13c4
commit 0e825e6d61
6 files changed
+596 -49

No files matched your search

+26 -12
View File
@@ -171,7 +171,7 @@ points. Remaining work before production:
## Scheduled tasks (scheduler addon)
Three jobs run inside the container via `CloudronManifest.json` → `addons.scheduler`:
Four jobs run inside the container via `CloudronManifest.json` → `addons.scheduler`:
- **membership_sweep** (`0 3,11,19 * * *` UTC — three times a day) — `sweep.sh`
calls the portal's own `/admin/sweep` (auth: the `X-Admin-Token` header with
@@ -182,16 +182,30 @@ Three jobs run inside the container via `CloudronManifest.json` → `addons.sche
self-heals within a day. The same sweep also runs the payment-driven
allowance reconciliation (below).
**First-login activation hook:** the sweep is also the backstop for service
invites, but it is no longer the only trigger. When a member first presents
themselves on the broker identity path (the dashboard's first login), the
portal checks — only if their Loomio invite is still outstanding
(`loomio_invited_at` is null) — that their Cloudron account is now activated
(`inviteAccepted`), and if so fires the same idempotent Loomio + Matrix syncs
as a background task. Established members cost one SELECT; the response never
waits on Loomio or Matrix; failures are logged and non-fatal; unactivated
accounts are never invited (the check fails closed). So a new member gets
forum + Space access at their first login instead of at the next sweep.
**Service-invite triggers:** the sweep is the backstop, not the primary path.
Two faster triggers exist, because a member who has just set up their account
should not wait hours for forum + Space access:
1. **First-login activation hook** — when a member first presents themselves
on the broker identity path (the dashboard's first login), the portal
checks — only if their Loomio invite is still outstanding
(`loomio_invited_at` is null) — that their Cloudron account is now
activated (`inviteAccepted`), and if so fires the same idempotent Loomio +
Matrix syncs as a background task. Established members cost one SELECT; the
response never waits on Loomio or Matrix; failures are logged and
non-fatal; unactivated accounts are never invited (the check fails closed).
2. **service_sync** (below) — the scheduled tick that covers members who never
open the dashboard.
- **service_sync** (every 5 minutes) — `service_sync.sh` calls the portal's own
`/admin/service-sync` (same `X-Admin-Token` auth as the sweep). WHY: a new
member is provisioned with an UNACTIVATED Cloudron account, and the syncs
deliberately skip an unactivated account (never invite a half-created one) —
so nobody on our side is watching at the moment the member actually activates.
This job fires the same idempotent Loomio + Matrix syncs the sweep runs, but
ONLY while some member is still awaiting their invites (one indexed SELECT
when nobody is, so it costs nothing in the steady state), and it deliberately
is NOT `/admin/sweep`: it never touches Open Collective, allowance logic or
member mail. An in-process guard drops an overlapping run.
Net effect: a member who has just set up their account waits minutes at most.
- **rag_refresh** (daily 3:30am UTC) — `rag_refresh.py` pulls this repo's
sibling docs from the Gitea raw API (public, no credentials), uploads changed
files to Open WebUI (`OWUI_API_KEY`), attaches them to the shared knowledge
@@ -217,7 +231,7 @@ Three jobs run inside the container via `CloudronManifest.json` → `addons.sche
Methodology: [Energy and carbon metrics](docs/energy-metrics.md).
Verify after any manifest change: the deploy log must show
`setupAddons: ... scheduler ...` listing all three tasks, and each script must
`setupAddons: ... scheduler ...` listing all four tasks, and each script must
succeed when run manually inside the container.
### Allowance renewal (payment-driven, join-day anchored)