Skip to content

Republish channel discovery events a failed emission left missing - #9805

Merged
bradseiler merged 6 commits into
mainfrom
mbot/discovery-heal
Dec 1, 2026
Merged

bradseiler merged 6 commits into
mainfrom
mbot/discovery-heal

Conversation

@baxen

@baxen baxen commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Summary

The relay publishes a channel's discovery events (kind:39000 metadata, kind:39001 admins, kind:39002 members) after the channel's write commits, and only best-effort. During a relay outage some channels got their rows but not all of these events. The database was healthy, but clients could not read the roster. A 1.x client confirms a DM against its signed kind:39002 roster, so opening one of these DMs failed with "participants could not be confirmed" every time. Retrying didn't help: reopening an existing DM published nothing. Users can't repair it either, because the relay refuses kind:9000 on DMs, which rules out the no-op re-add.

This PR adds two repair paths and one shared check.

Shared check (buzz_db::group_discovery). A channel is missing discovery when, for any of 39000/39001/39002, it has no live relay-signed event. Each kind is one probe on idx_events_addressable, bounded below by the channel's created_at (less an hour of clock slack), so Postgres prunes the monthly events partitions to the ones since the channel was created. That bound is what keeps the check cheap: without it, every probe visits every partition.

1. A DM reopen heals the DM. When kind:41010 returns an existing DM and adds nobody, the relay runs the check and republishes all three events only if one is missing. A healthy reopen costs one indexed read and no writes. When agents are added, the additions already republish discovery. So a client retry now repairs the DM.

2. Startup sweep. In the background after startup, the relay scans live channels in active communities created in the last BUZZ_DISCOVERY_REPAIR_LOOKBACK_HOURS (default 72; 0 turns it off). It skips channels created in the last minute, whose creation may still be publishing, and republishes each one missing a kind. Its probes also carry a constant floor (the window start, less the slack), so the partitions prune whichever join the planner picks. Each query returns up to 200 channels that need repair and uses a (created_at, id) cursor. PostgreSQL may examine more than 200 channels in the window. The sweep moves past a channel whose repair fails, so it ends after one pass. Replicas racing on one channel are fenced by the existing replacement locks.

Measured cost: on a production database with ~90k channels (~25k created in the past week), EXPLAIN ANALYZE of one 3-day page took 310 ms. That's ~48k index probes, all from cache, and run-time pruning skipped every earlier partition. An ad-hoc NOT EXISTS scan without the created_at bound hit the 30 s statement timeout on the same data.

The dev/CI reconciler (BUZZ_RECONCILE_CHANNELS) now uses the same check, so it also catches a missing 39001 or 39002, not just a missing 39000.

Startup logs the effective lookback and the outcome, including disabled runs and runs that repair zero channels. A malformed setting such as 336h logs a warning and falls back to 72 hours.

Related issue

N/A. Related DM membership repair proposal.

Rollout

For known channels older than 72 hours, set BUZZ_DISCOVERY_REPAIR_LOOKBACK_HOURS to a whole number that covers the oldest affected channel at rollout. For example, 336 covers channels created within the preceding 14 days. Before widening the window, run the read-only sweep query to verify the affected channels and measure the query cost. After rollout, check the startup logs for the effective window and repair results, confirm the affected channels have signed discovery events, and restore the default lookback after the one-time sweep.

Testing

handlers::discovery_repair_postgres_tests:

  • reopening_a_dm_republishes_its_missing_roster: a DM keeps 39000 and loses 39002, and a reopen republishes the roster with both people. A healthy reopen first leaves every live event untouched.
  • the_sweep_republishes_recent_channels_missing_any_discovery_kind: the sweep runs with one channel per page. It repairs a channel left with only 39000 (its 39001 and 39002 soft-deleted) and one left with nothing, and leaves alone a healthy channel, one older than the lookback, and one inside the in-flight grace.

A read-only run of the sweep's query against the production data above, over the past 3 days, returned exactly the 11 channels known to be broken by the outage, and no others.

Mutation checks, each of which turns a test red:

  • drop the reopen repair
  • republish without the check
  • check only 39000
  • count deleted events as live
  • stop after one page
  • start the scan before the lookback
  • drop the grace bound

The discovery repair, DM growth, and group-state suites passed in a local PostgreSQL 16 instance on the rebased code. The published head is e452374a3. Four local relay starts with 0, -1, 336h, and 336 verified disabled runs, the malformed-value warning and fallback, and the zero-repair summary. The live startup runs exercised logging and configuration. The repair itself was verified in the PostgreSQL tests.

🤖 Generated with Claude Code. Updated with Codex.

baxen and others added 5 commits November 30, 2026 14:26
Co-authored-by: mbot <3754f8729004d95654c46dbab3129e4ab9ef05cc2534e2a3fbfc155983bd637b@buzz.block.builderlab.xyz>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Bradley Axen <baxen@squareup.com>
Signed-off-by: Brad Seiler <seiler@squareup.com>
… the lookback

Co-authored-by: mbot <3754f8729004d95654c46dbab3129e4ab9ef05cc2534e2a3fbfc155983bd637b@buzz.block.builderlab.xyz>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Bradley Axen <baxen@squareup.com>
Signed-off-by: Brad Seiler <seiler@squareup.com>
Co-authored-by: mbot <3754f8729004d95654c46dbab3129e4ab9ef05cc2534e2a3fbfc155983bd637b@buzz.block.builderlab.xyz>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Bradley Axen <baxen@squareup.com>
Signed-off-by: Brad Seiler <seiler@squareup.com>
Co-authored-by: mbot <3754f8729004d95654c46dbab3129e4ab9ef05cc2534e2a3fbfc155983bd637b@buzz.block.builderlab.xyz>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Bradley Axen <baxen@squareup.com>
Signed-off-by: Brad Seiler <seiler@squareup.com>
Co-authored-by: Harrington <91868d0cc0b8144564e350ce0a9e46447f654ad3fe93b23e39fe71314c911781@buzz.block.builderlab.xyz>
Signed-off-by: Brad Seiler <seiler@squareup.com>
@github-actions github-actions Bot added the codex-security-review-current The posted Codex security review matches its recorded range. label Dec 1, 2026
@github-actions

github-actions Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

🔐 Codex Security Review

Note: This is an automated, security-focused review generated by Codex.
Use it as a supplement to human review; false positives are possible.

Scope

  • Exact PR diff: a12a9995c458e45c4901763f029fd2ec006f610d...3f02d6579bd7b103ecab9798cfcf9c0b93726966
  • Model: gpt-6.1-sol

💡 Click "edited" above to see earlier reviews for this PR.


Review Summary

Overall Risk: MEDIUM

The one-shot startup repair can miss channels created immediately before a crash.

Findings

[MEDIUM] Revisit channels excluded by the startup grace period

  • Category: Reliability
  • Location: crates/buzz-relay/src/handlers/discovery_repair.rs:73 (source)
  • Description: If channel creation commits and the relay crashes before publishing discovery, a restart within one minute excludes that channel from this sweep. The cutoff remains fixed, and main invokes the sweep only once, so the channel is never reconsidered when the grace period expires. The on-demand repair covers only DM reopen.
  • Impact: Ordinary channels can remain without metadata or roster events, preventing client discovery until another mutation or restart triggers publication.
  • Recommendation: Delay the startup sweep until the grace period has elapsed, or schedule a follow-up pass covering channels excluded by the initial cutoff. Add coverage for a committed channel missing discovery immediately before a fast restart.

Notes

  • No additional limitations were reported.

Generated by Codex Security Review |
Requested by: @bradseiler |
Workflow run

@wpfleger96 wpfleger96 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 Reviewed at e452374a3. No blocking concerns. The missing-event check runs on the index's leading columns plus a created_at bound for partition pruning, every live channel always gets all three kinds from emit_group_discovery_events, and the only paths that soft-delete discovery events also delete the channel, which the check excludes, so a healthy channel won't be republished on every startup or reopen. One stale comment inline, non-blocking.

Comment thread crates/buzz-relay/src/main.rs Resolved
Co-authored-by: Henke <b494f0778682c83a1faae59541cfd2c2d2060dc1d5b3c2dc819fdc529840b8a4@buzz.block.builderlab.xyz>
Signed-off-by: Brad Seiler <seiler@squareup.com>
@github-actions github-actions Bot removed the codex-security-review-current The posted Codex security review matches its recorded range. label Dec 1, 2026
@bradseiler
bradseiler deployed to codex-review December 1, 2026 20:09 — with GitHub Actions Active
@github-actions github-actions Bot added the codex-security-review-current The posted Codex security review matches its recorded range. label Dec 1, 2026
@bradseiler
bradseiler merged commit e9af5fe into main Dec 1, 2026
91 checks passed
@bradseiler
bradseiler deleted the mbot/discovery-heal branch December 1, 2026 21:21
wpfleger96 added a commit that referenced this pull request Dec 2, 2026
…xpires

* origin/main: (24 commits)
  fix(db): remove the duplicate tracing-subscriber dev-dependency (#10094)
  chore(lint): ban raw calls that review keeps catching (#10077)
  fix(deletion): log a request the drain blocks at claim (#10078)
  fix(deletion): skip unlisted tenant tables instead of blocking (#10074)
  fix(deletion): purge PostgreSQL in bounded batches (#10081)
  feat(db): record safe error and pool fields on datastore spans (#9431)
  perf(relay): share identical in-flight author lookups on POST /query (#10044)
  Republish channel discovery events a failed emission left missing (#9805)
  perf(db): jitter pool connection retirement; configurable reader floor and lifetime (#10047)
  feat(search): people-only search filter, and the agent owner on each indexed message (#10021)
  fix(search): drop the community label from indexer metrics (#10019)
  relay: advertise the build's source SHA in NIP-11 (#9827)
  feat(mobile): record a push baseline when Android enrolls (#10038)
  Show deletion evidence to the requester (#10076)
  Give single photos the stack card style in mobile chat (#10071)
  fix(search): do not return expired AWS credentials after a failed reload (#10042)
  feat(mobile): unify the Android conversation composer (#10015)
  feat(mobile): check the relay key on Android push channel titles (#10037)
  chore(devex): streamline local OpenSearch and Postgres test workflows (#10034)
  feat(mobile): record messages the app shows so Android wakes skip them (#10039)
  ...

Signed-off-by: Will Pfleger <wpfleger@block.xyz>
liching1 added a commit that referenced this pull request Dec 2, 2026
* origin/main: (47 commits)
  fix(db): remove the duplicate tracing-subscriber dev-dependency (#10094)
  chore(lint): ban raw calls that review keeps catching (#10077)
  fix(deletion): log a request the drain blocks at claim (#10078)
  fix(deletion): skip unlisted tenant tables instead of blocking (#10074)
  fix(deletion): purge PostgreSQL in bounded batches (#10081)
  feat(db): record safe error and pool fields on datastore spans (#9431)
  perf(relay): share identical in-flight author lookups on POST /query (#10044)
  Republish channel discovery events a failed emission left missing (#9805)
  perf(db): jitter pool connection retirement; configurable reader floor and lifetime (#10047)
  feat(search): people-only search filter, and the agent owner on each indexed message (#10021)
  fix(search): drop the community label from indexer metrics (#10019)
  relay: advertise the build's source SHA in NIP-11 (#9827)
  feat(mobile): record a push baseline when Android enrolls (#10038)
  Show deletion evidence to the requester (#10076)
  Give single photos the stack card style in mobile chat (#10071)
  fix(search): do not return expired AWS credentials after a failed reload (#10042)
  feat(mobile): unify the Android conversation composer (#10015)
  feat(mobile): check the relay key on Android push channel titles (#10037)
  chore(devex): streamline local OpenSearch and Postgres test workflows (#10034)
  feat(mobile): record messages the app shows so Android wakes skip them (#10039)
  ...

Signed-off-by: Liching Li <liching@squareup.com>

This branch was successfully deployed

1 active deployment
codex-review — 3f02d657 Deployed Dec 1, 2026 by bradseiler via Run Codex Security Review #19052
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

codex-security-review-current The posted Codex security review matches its recorded range.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants