Repository navigation
feat(relay): add shared mention groups with durable CAS commands - #9589
LordMelkor wants to merge 7 commits into
Conversation
🔐 Codex Security Review
|
joahg
left a comment
There was a problem hiding this comment.
🤖 Commenting rather than approving. This is a high-risk relay change covering authorization, fan-out and a migration, and I only read the code without building or running it. I found no P0 or P1.
What looks right:
- CAS: the community row lock serializes all commands and group rows are
FOR UPDATE. A name collision rolls the transaction back, and receipts are written in the same transaction as the state. - Tenant isolation: every query is scoped by
community_id. - Migration: additive, with bounded timeouts. Its table names match what #9587 registered.
- Channel joins: only open, listed channels are accepted.
- Replay and caps: exact retries return
duplicate:with the original summary, the age window is ±120 s, and the 20-member cap is advertised in NIP-11. - Tests: they cover concurrent CAS and name races, retries, expired receipts, fenced writes and purge.
P2:
- Authorization happens after the community lock.
store/mention_group.rstakeslock_community(FOR NO KEY UPDATE) beforeauthorize_command. Any member can send commands that end inForbidden, for example underadmins, and each still serializes against every group write on that community. I didn't find a per-actor command limiter. Authorizing first, under a shared lock, then escalating, would close that. - A channel turning private breaks group edits. The channel visibility check runs for every listed channel even when no members are being added. If a referenced channel later becomes private or unlisted, every put of that group returns
InvalidData, including a rename or a member removal, and only delete still works. Skipping or warning on stale channels when no members are added would keep groups editable. - Join fan-out is large and runs in the request. One put can join 20 members to 32 channels, which is 640 joins. After commit,
finishemits a system message, a membership notification and a type lookup for each join, plus a discovery event per channel, all serially. Someone who left a channel is re-added every time they're toggled out of and back into the group. Moving that work to a background task and rate-limiting per actor would help. - No growth bound. There's no cap on groups per community, and deleted ids and receipts are kept forever. A manager can create and delete groups in a loop, and clients read all of that state, tombstones included, on every reconnect.
- An open relay has no policy owner. With no stewards, any signer can set the management policy. That fits the open model, but the NIP should say so.
P3:
- Probing private channel ids. A private channel id returns an explicit error while a nonexistent one passes silently, which confirms that a given id exists and is private.
- Group names. Zero-width and bidi characters (U+200B, U+202E) are allowed, and there's no Unicode normalization. Uniqueness uses Postgres
lower(), which depends on collation. A name collision and a CAS conflict share 409 and differ only in message text.hereandeveryonearen't reserved. - Stale membership. Removed community members stay in groups, and channels added to a group later aren't backfilled for existing members.
- Timeouts.
SET LOCAL statement_timeout='5s'covers the whole transaction, join loop included, and a timeout surfaces as a 500.
|
🤖 Update, Nov 18: Closed for now. Mention Groups is being redesigned around community-scoped NIP-AR artifacts (#9638), which would replace these tables and commands. Thanks for the follow-up review. If this approach comes back, its channel-lock and test points come first. We'll reopen what's needed when the redesign is complete. The stale-default edit failure is fixed: a saved default that later becomes private or unlisted remains an inactive reference. Renames and member removals succeed, and adding someone to the group skips that channel with For the other observations:
Validation at merged head Normal push hooks passed and the published PR head was verified at |
joahg
left a comment
There was a problem hiding this comment.
🤖 Followup on 9148c337. 89ff687a fixes the stale-default P2. Previous channels come from the locked group row. A saved default that has since become private or unlisted is skipped as channel_closed rather than refused, so renames and member removals work again. Newly selected nonpublic defaults are still refused, and the new Postgres test covers both the private and the unlisted transitions. The NIP-MG text also now says who can set policy on an open community with no steward, which closes that P2. Apart from that, the merge from main brings in no changes of its own to this feature.
On your other replies: per-actor admission quotas on both transports do bound the forbidden-command traffic I raised, so I'm fine keeping authorization under the community lock for ordering. Fan-out latency, growth bounds and name normalization are reasonable as follow-ups. I'd still like an issue tracking the total-count or retention policy before groups see real use.
I'm still commenting rather than approving. This is a high-risk relay change (authorization, fan-out and a migration), and I've read it without building or running it. It needs a relay maintainer's approval.
brow
left a comment
There was a problem hiding this comment.
🤖 Tom's pre-review recommends a small rework. The feature itself is sound, but this head breaks three things outside it:
- Channel changes can fail. A group put takes the community row (
mention_group.rs:116) and then each default channel's membership lock, TTL lock and channel row (channel_members.rs:953-1015), while a channel visibility, TTL orunlistededit and a channel delete take those channel locks first (channel.rs:1265-1287) and thenUPDATE communities(channel.rs:1421-1427,1884). When the two overlap on one channel PostgreSQL aborts one with a deadlock and nothing retries it, so an admin's channel change or delete (or the group save) fails and has to be repeated. Taking the put's full sorted lock set (membership locks, then per channel the shared TTL lock and the channel row) beforelock_community, with the statement timeout set first and no eligibility checks or joins before the receipt check, removes the cycle. Please cover it with a PostgreSQL test in which a holder of a channel's production locks can still update the community row while a put waits. - A sleeping, clock-dependent test.
expired_exact_receipt_retry_survives_a_later_edit_but_unknown_expired_commands_failstamps its command 105 s old and sleeps about 16 s of real time on every run (16.8 s in CI), and its firstingest_event(...).unwrap()fails if the machine stalls more than 15 s. Only the handler checks freshness, so the test can seed an already expired command throughDb::accept_mention_groupand keep the retry and unknown-command cases oningest_event, with no sleep. - Mobile PRs run the Rust lanes.
mobile/lib/shared/push/push_subscription.dartis now in therustpath filter (ci.yml:53-54,lefthook.yml:103), so any PR that touches that 584-line file also runs the Rust, desktop, relay, security and PostgreSQL jobs and the pre-pushjust test-unit(this PR's run was 111 jobs and about 400 runner-minutes, against 54 jobs and 53 for a recent mobile-only PR). MovingbuzzPushHellthreadParticipantLimitinto its own small Dart file and pointing the parity test and the filters at that file keeps the guard without the cost.
The head also conflicts with main: migration numbers 0093 to 0095 are taken there, and #9587 is still open. The lock analysis was done on this head, so please recheck it after the rebase, since main changed channel.rs and channel_members.rs.
Co-authored-by: Sigil <06114f072cf21e50fcd4133ab21346cdb1f74ee16d853a9729995fa6c03a251c@buzz.block.builderlab.xyz> Signed-off-by: Krish Ray <kray@block.xyz> Signed-off-by: kray <kray@squareup.com>
Store relay-owned groups and management policy with atomic signed heads, accepted-command receipts and default-channel joins. Expose NIP-MG discovery and conflict responses, with persistence, admission and live transport coverage. Co-authored-by: Sigil <06114f072cf21e50fcd4133ab21346cdb1f74ee16d853a9729995fa6c03a251c@buzz.block.builderlab.xyz> Signed-off-by: Krish Ray <kray@block.xyz> Signed-off-by: kray <kray@squareup.com>
Use the shared event writer for relay-signed group heads. Limit groups to 20 distinct members, advertise the cap, and derive refusal text from the core constant. Check parity with the mobile push limit and run that check for Dart-only changes. Co-authored-by: Sigil <06114f072cf21e50fcd4133ab21346cdb1f74ee16d853a9729995fa6c03a251c@buzz.block.builderlab.xyz> Signed-off-by: Krish Ray <kray@block.xyz> Signed-off-by: kray <kray@squareup.com>
Retain saved defaults that become private or unlisted as inactive references, without granting channel access. Refuse newly selected nonpublic defaults. Clarify policy authority for open communities without stewards. Co-authored-by: Sigil <06114f072cf21e50fcd4133ab21346cdb1f74ee16d853a9729995fa6c03a251c@buzz.block.builderlab.xyz> Signed-off-by: Krish Ray <kray@block.xyz> Signed-off-by: kray <kray@squareup.com> Co-authored-by: Codex <noreply@openai.com> Signed-off-by: kray <kray@squareup.com>
Signed-off-by: Krish Ray <kray@block.xyz> Co-authored-by: Sigil <06114f072cf21e50fcd4133ab21346cdb1f74ee16d853a9729995fa6c03a251c@buzz.block.builderlab.xyz> Co-authored-by: Codex <noreply@openai.com>
Signed-off-by: Krish Ray <kray@block.xyz> Co-authored-by: Sigil <06114f072cf21e50fcd4133ab21346cdb1f74ee16d853a9729995fa6c03a251c@buzz.block.builderlab.xyz> Co-authored-by: Codex <noreply@openai.com>
Signed-off-by: Krish Ray <kray@block.xyz> Co-authored-by: Sigil <06114f072cf21e50fcd4133ab21346cdb1f74ee16d853a9729995fa6c03a251c@buzz.block.builderlab.xyz> Co-authored-by: Codex <noreply@openai.com>
9148c33 to
3df54db
Compare
joahg
left a comment
There was a problem hiding this comment.
🤖 Follow-up on 3df54db. This is a comment, not an approval. A human maintainer needs to review this PR.
I read the code and the range-diff against 9148c337 without building or running it. CI is green on this head, including the PostgreSQL lanes. The only red check is the Codex Security Review job, which failed with "No review output from action or output file" and needs a maintainer to re-authorize it.
What changed since 9148c337
- The first four commits differ only by rebase noise: the migration is renumbered to 0097 (main is at 0096), and there is a
deletion_state = 'active'guard on the policy UPDATE plus the shared test database-URL helper. - f7d5de2: a group put now takes its sorted default-channel locks (membership advisory lock, then shared TTL lock, then channel row
FOR NO KEY UPDATE) beforelock_community. This is the same order the channel settings and delete writers use.statement_timeoutis now set first, so lock waits are bounded. - 266ab62: the expiry test seeds the historical receipt through
accept_mention_groupand no longer sleeps. - 3df54db: the mobile limit moved into
push_contract.dart. The Rust path filter, the lefthook glob, the parity test andci-selection.test.mjsall point at that file. A change topush_subscription.dartnow selects only the mobile lane.
Earlier findings
- Settled: stale default channel (P2). Settled: NIP-MG policy text for an open community (P2).
- Settled, from the CHANGES_REQUESTED review: the channel/community lock-order deadlock, the sleeping test, mobile PRs running the Rust lanes, and the migration number clash.
- Accepted earlier: authorization under the community lock, given the per-actor quotas.
- Still open as follow-ups: join fan-out latency, name normalization and reservation, and the private-versus-missing channel error difference (all P2/P3). Group, tombstone and receipt growth is tracked in #9594.
Lock ordering check. I found no cycle.
- Group puts sort and dedupe channels in
parse_command, and the DB layer rejects a command that differs from the parse. So two puts lock in the same order. - Channel writers take membership, then TTL, then row, then
UPDATE communities. A put holds every channel it needs before it waits for the community row, and the put's later queries take no further channel locks. - The policy command and group delete take no channel locks.
- The three new
cluster_global_PostgreSQL tests (membership, exclusive TTL and row writers) hold the production channel locks, prove viapg_blocking_pidsthat the put is waiting, update the community row, and then expect both to commit. The replay-after-channel-removal assertions also confirm the lock phase makes no eligibility decision.
New findings
- P2: the channel locks are now taken before the receipt, authorization and CAS checks (inline comment). A forbidden, stale or duplicate put now locks up to 32 channels for as long as it waits on the community row. Before this change those commands never touched channel locks.
- P3: the lock-order tests use one channel. Nothing covers the multi-channel sorted-order guarantee, which is the part that stops put-versus-put deadlocks (inline comment). The channel delete path (
lock_channel_for_delete) is not covered either. - P3: the first commit is #9587, which is still open. The migration follows the "pending-catalog release first" rule in
docs/database.md, so #9587 must be merged and deployed everywhere before this migration runs. That is a merge and release gate for maintainers.
Status. P0: none. P1: none. The earlier P2 and P3 follow-ups remain as noted. This is a high-risk relay change covering authorization, locking and a migration, so it needs a relay maintainer's approval.
| // Match that order using the full normalized put set, locks only: | ||
| // mutable eligibility and authorization stay after the receipt check. | ||
| if let Operation::Put { channels, .. } = &command.operation { | ||
| crate::channel_members::lock_group_default_channels_in_transaction(&mut tx, channels) |
There was a problem hiding this comment.
🤖 P2: these locks now run before the receipt, authorization and CAS checks. A member who is refused (for example under admins), sends a stale prev, or retries an accepted command takes the membership advisory lock, the shared TTL lock and the row lock on up to 32 channels, and holds them while it waits for lock_community. Joins, leaves and settings edits on those channels then stall behind it, bounded only by the 5 s statement timeout and the per-actor quota. Before f7d5de2 these commands never reached channel locks. Cheap fix that keeps the ordering: do an unlocked read of the actor's role and the manage policy, and an unlocked receipt lookup, before this call, and return Forbidden or Duplicate early when they already decide the outcome. Keep the authoritative checks after lock_community, since the early reads only decide whether the locks are needed.
|
|
||
| // A settings writer already holding a channel lock must be able to finish | ||
| // its community invalidation while a group put waits for that channel. | ||
| async fn assert_channel_writer_precedes_group_put(updates: crate::channel::ChannelUpdate) { |
There was a problem hiding this comment.
🤖 P3: this helper locks one channel. The property that prevents put-versus-put deadlocks, sorted locking across several channels, is not exercised. Consider one test with two overlapping puts over two or more channels, plus one for the channel delete path (lock_channel_for_delete), so a later change to the normalization or to the lock order fails a test.
Summary
Community-wide mention groups need one authoritative revision that multiple authorized people can edit safely. The relay owns group state, checks revisions on signed put and delete commands, keeps live names unique, and records accepted commands. An exact retry returns the original acknowledgement without repeating the edit or joining members again.
Commands 47000–47002 manage groups and the community's
everyoneoradminspolicy. Relay-only kind 39010 publishes current groups, settings and deletion tombstones. Management defaults toeveryoneand uses effective community membership. Agents do not inherit their owner's admin role. Adding group members joins eligible targets to configured public channels in the same transaction under the existing channel-add policy. Saved defaults that become private or unlisted stay inactive. New nonpublic defaults are refused.Groups contain up to 20 distinct members, matching mobile's push recipient limit. NIP-11 advertises the limit, protocol version and the relay's
openorrostermembership rule. Clients reconcile current state after reconnecting. NIP-MG specificationRollout
Deploy the compatibility release from #9587 to every serving relay, deletion executor and operator tool before applying migration 0097. This branch includes the prerequisite changes for review. After #9587 merges, rebase this PR onto main to account for the repository's squash merge. Migration 0097 adds
mention_groupsandmention_group_commandsand extendscommunities. Compatibility PR · Migration 0097External plugins can use the relay contract after deployment. The app capabilities they need are tracked separately. Signing and publishing · Completion providers · Linked mention chips
This design supersedes #2791 and adds revision checks and durable receipts to relay-owned groups. #8819 describes the related author-signed alternative.
Review repairs
Known limits
Forbidden. PostgreSQL applies a five-second timeout per statement.Validation
Published head
3df54db74f01ebd8f47cc0ecdba50745b17cfefbhas the same Git tree as the tested candidateb9ee6ceb0a162c601742be53020c2ee7c951504b, based on main3876720c345bb6a9b709401707e35225e66bdb31.just testpre-push lane failedcomposition_log_does_not_leak_issuer_urlon the candidate and on exact main. Themobile-checklane flagged the same 296 files on both. Flutter test dependency resolution stopped becausepub.devcould not be resolved, andjust cistopped atmobile-check. GitHub CI succeeded on exact main, including the PostgreSQL domain job and all three mobile test shards. Main CI runTo repeat the live journey, run the test against an isolated open relay with this schema.
Human acceptance and fleet deployment are pending.