buzz icon indicating copy to clipboard operation
buzz copied to clipboard

Desktop: relay-scoped local read-state keys don't canonicalize equivalent relay URLs, producing a permanent phantom unread badge

Open astromara opened this issue 1 month ago • 1 comments

Describe the bug

Desktop's local read-state stores (including the workspace/community unread badge) key their scope by <pubkey>:<relayUrl>, but the client-side relay-URL normalizer only trims whitespace, strips trailing slashes, and lowercases — it does not canonicalize scheme, loopback host spelling (localhost / 127.0.0.1 / [::1]), or default ports. When the same relay is reached under two URL spellings that are equivalent identities (a tailnet hostname change, ws vs wss, with/without an explicit default port, etc.), a read marker recorded under one spelling never covers events recorded under the other. The result is a permanent phantom unread count on the workspace badge: the channels holding the "unread" events never highlight in the sidebar, so there is nothing to click to clear it, and restarting the app does not help.

Steps to reproduce

  1. Connect Desktop to a self-hosted relay and generate activity (mentions/messages) while connected under one URL spelling, e.g. wss://hermes-macbook-pro.tail1234.ts.net.
  2. Reconnect to the same relay under a differently-but-equivalently-spelled URL, e.g. the same host with a casing/typo change, ws://127.0.0.1:3000 vs wss://127.0.0.1:3000, or with vs without an explicit default port.
  3. Open and read everything, including under the new spelling; restart Desktop.
  4. The workspace badge still shows an unread count. No channel in the sidebar is highlighted, so there is no affordance to clear it.

Expected behavior

Equivalent relay URLs (same canonical host identity) should resolve to a single read-state scope, the same way the server already treats them as one identity. A read marker written on one connection should cover events observed on another connection to the same relay.

Version and platform

  • Buzz version: 0.5.14
  • OS: macOS 26.5.2 (Apple Silicon)

Logs / additional context

Local DB evidence (~/Library/Application Support/xyz.block.buzz.app/observed-unread.db, queried directly): for 2-3 real relay connections, scope_state/observed_events/read_markers carry 11 distinct scope strings, including ws://127.0.0.1:3000, wss://127.0.0.1:3000, wss:///127.0.0.1:3000 (stray extra slash), wss://127.0.0.1:8080, two differently-spelled tailnet hostnames for the same machine, a malformed wss://https://<host> scope, and that host with/without :3000/:8080. Cross-checking observed_events (counts_badge=1) against read_markers directly returns 7 rows with no covering marker, spanning 3 channels across 2 of those scope strings, event timestamps 2026-10-20 through 2026-10-24 — none of which show as unread anywhere in the sidebar.

Root cause, from source:

// desktop/src/shared/lib/normalizeRelayUrl.ts
export function normalizeRelayUrl(relayUrl: string): string {
  return relayUrl.trim().replace(/\/+$/, "").toLowerCase();
}

This is the function used to build the unread scope key in observedUnreadStorageKey() (desktop/src/features/channels/observedUnreadStorage.ts:52), and it's re-exported/imported by at least a dozen other relay-scoped local stores (sidebar sections, sync watermark, sort preference, message/channel snapshots, thread activity, self profile, user labels, community theme preference, legacy community storage, onboarding welcome guide/kickoff) — so the same collision risk applies anywhere read/UI state is scoped by relay URL, not just the unread badge.

By contrast, the backend already has a correct canonicalizer for exactly this problem — buzz-core's normalize_relay_url (crates/buzz-core/src/relay.rs:37-78) — which folds wss://[::1], wss://127.0.0.1, and wss://localhost to one identity and strips default ports (covered by its own test, relay.rs:84-91). It is never used for the desktop's local-storage/sqlite scope keys.

Checked for duplicates first: closest are #7930 (DM unread badge never clears) and #8189 (home inbox "mark all as read" doesn't stick), but both describe per-row read-state recompute issues within a single scope. #6428 and #8308 are also unread-adjacent but describe unrelated mechanisms. None of them describe cross-scope key mismatch from unnormalized relay URLs.

Suggested fix direction: either (a) have the desktop local-storage/sqlite scoping reuse buzz-core::normalize_relay_url's canonicalization instead of the ad-hoc trim/lowercase, or (b) give desktop/src/shared/lib/normalizeRelayUrl.ts equivalent scheme/host/port folding — plus a one-time migration to merge existing scopes that normalize to the same identity, and pruning of scopes whose URL never successfully connects.

astromara avatar Oct 25 '26 14:10 astromara

Confirming evidence from a live 0.5.14 self-hosted deployment (loopback relay exposed over Tailscale, plus a separate hosted community domain).

Querying ~/Library/Application Support/<bundle-id>/observed-unread.db directly for one pubkey turned up the same pattern this issue already catalogs: 11 distinct scope strings for what is really one relay/pubkey pair — loopback variants, two differently-spelled tailnet hostnames for the same machine, a malformed wss://https://<host> entry, and the current production community domain.

For one specific channel/thread I was able to trace the failure end-to-end:

  • Its channel-level read_markers row exists only under two stale scopes — the dev loopback URL (last read ~4 weeks ago) and an old tailnet-hostname spelling (last read ~1 week ago).
  • The scope the client is actually connected on right now has zero rows in channel_latest and zero cached observed_events for that channel at all — despite clearly being the live connection (hundreds of recent events logged there for other channels in the same scope).
  • A leftover thread:<id> marker does exist under the live scope, but it's frozen at the timestamp of the last reply before the URL spelling changed, so anything re-ingested into that channel afterward is unprotected.

Net effect: every time the resolved relay URL spelling shifts, that channel's local read state silently restarts on a blank scope, and old threads re-surface as unread with nothing durable to "mark as read" against — they just come back. Matches the root cause already identified here: normalizeRelayUrl.ts only trims/lowercases and never folds equivalent relay identities, unlike the backend's normalize_relay_url in buzz-core.

Happy to share the raw sqlite3 query output if useful for repro.

astromara avatar Nov 03 '26 15:11 astromara