Repository navigation
fix(notifications): key episode notification state by account and profile - #2602
Conversation
…file A profile ID is unique only within its account, and every account from before profiles has a profile named "default". Interest rows, notification preferences, the episode delivery dedupe keys, and inbox reads were keyed by profile ID alone, so two accounts' "default" profiles that followed the same series shared one interest row and only one of them was notified, shared one set of switches, and each could see the other's inbox. Key all of it by (user_id, profile_id): a migration widens the interest primary key, adds user_id to notification_preferences (copying a shared row to every account that has the profile), and re-keys the delivery dedupe and inbox indexes, and every read, mark-read, purge, fanout lookup, and the email channel now carry the account. The migration restarts the interest rebuild so its startup pass restores rows the old key collapsed. Closes #2597 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Silo Kody — review completeReview finished. Check the inline comments for findings and verify each suggestion against the code and tests. Reviewing changes in Silo
Review settingsReview OptionsThe following review options are enabled or disabled:
|
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info
📝 Walkthrough
Merge Risk: ⚪ Minimal · up to
|
| SELECT user_id, id AS profile_id FROM public.user_profiles | ||
| UNION SELECT user_id, profile_id FROM public.profile_series_interest | ||
| UNION SELECT user_id, profile_id FROM public.notification_deliveries | ||
| UNION SELECT user_id, profile_id FROM public.notification_email_prefs | ||
| ) owner ON owner.profile_id = np.profile_id | ||
| JOIN public.users u ON u.id = owner.user_id | ||
| WHERE np.user_id IS NULL; | ||
| DELETE FROM public.notification_preferences WHERE user_id IS NULL; |
There was a problem hiding this comment.
Preference loss occurs in migrations/sql/20261016161203_notification_state_account_key.sql because the migration deletes live SQLite profiles' settings when their account/profile pair is absent from the PostgreSQL ownership-source tables; a SQLite-only profile with notifications disabled and no interest, delivery, or email rows loses its preference row and subsequently receives all-enabled defaults. Preserve unresolved preferences for backfill against the configured user store instead of treating absent PostgreSQL ownership as proof of profile deletion.
Prompt for LLM
File migrations/sql/20261016161203_notification_state_account_key.sql:
Line 117 to 124:
Preference loss occurs in `migrations/sql/20261016161203_notification_state_account_key.sql` because the migration deletes live SQLite profiles' settings when their account/profile pair is absent from the PostgreSQL ownership-source tables; a SQLite-only profile with notifications disabled and no interest, delivery, or email rows loses its preference row and subsequently receives all-enabled defaults. Preserve unresolved preferences for backfill against the configured user store instead of treating absent PostgreSQL ownership as proof of profile deletion.
Talk to Kody by mentioning @kody
Was this suggestion helpful? React with 👍 or 👎 to help Kody learn from this interaction.
There was a problem hiding this comment.
Not changing this. The SQLite user store is not a live configuration (maintainer decision for this change). On the Postgres user store, user_profiles lists every profile, so a preference row whose profile_id matches no account in user_profiles, profile_series_interest, notification_deliveries, or notification_email_prefs belongs to a deleted profile. The new primary key needs an account, so such a row can't be kept as it is. Leaving this open for a maintainer in case SQLite support should block the migration instead.
There was a problem hiding this comment.
Understood. My critical warning assumed SQLite was a supported live user store. Given the stated PostgreSQL-only scope and your clarification that user_profiles contains every live profile, that failure scenario does not apply. A preference row with no matching live owner can be dropped rather than assigned an invented account.
This finding does not justify a required code change under that scope. Leaving the discussion open for a maintainer to decide whether SQLite support should block the migration is reasonable; the original warning should not be treated as an unconditional critical bug.
Resolve the fanout conflicts with #2599, reuse its profileKeyArrays for GuardedSetLastNotified and PreferencesRepository.GetMany, and add a fanout test where both accounts' "default" profiles follow one series and each delivery queues only its own account's webhook, web push subscription, and push device. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Evidence is incomplete for these visible changes:
Automated check: Macroscope check run agent (gpt-6-luna). Evidence was not reviewed for correctness. Posted via Macroscope — Visible change evidence |
|
The
Automated check: Macroscope check run agent (gpt-6-luna). No validation was performed. Posted via Macroscope — v1 validation impact |
Problem
Closes #2597
Closes #2603
Related issue: #2598
Validation tasks: none
On a server upgraded from before profiles, every account has a profile named
default. When two of those accounts follow the same series, only one of them gets new-episode notifications, they share one set of notification switches, and each account'sdefaultprofile sees the other's inbox (titles, unread count, mark-read). This change keys episode notification state by account and profile, so each account's profile is notified and has an inbox of its own.A profile ID is unique only within its account (
user_profilesis keyed by(user_id, id)), but these tables and queries treated it as unique on the server:profile_series_interesthas the primary key(profile_id, library_id, series_id), andUpsertRowssetuser_id = EXCLUDED.user_idon conflict, so both accounts' interest collapsed into one row owned by one account. Fanout delivers to the row's account only.(profile_id, release_event_id)and(profile_id, episode_id)would drop the second account's delivery even with separate interest rows.profile_idalone.notification_preferenceshad nouser_idat all.Approach
One migration plus the code that reads and writes these tables:
profile_series_interestgets the primary key(user_id, profile_id, library_id, series_id). Rows already carryuser_id.notification_deliveriesgets account-leading versions of the episode dedupe keys and the inbox indexes.notification_preferencesgainsuser_id, with the primary key(user_id, profile_id). A row two accounts' profiles share is copied to each account, so every profile keeps the switches it has today. Accounts come fromuser_profilesand, for profiles stored outside Postgres, from the account-keyed notification tables. A row no account can be found for belongs to a deleted profile and is dropped.Every delivery read, mark-read, purge, preference read and write, interest recompute and cleanup, and fanout's recipient bookkeeping now carries the account. The account comes from the request's claims in v1, v2, and the websocket. The email channel reads a profile's deliveries on the account that owns its email settings. API response shapes don't change (
Preferences.UserIDis not serialized), so no client changes are needed. Jellycompat doesn't serve these notifications.notification_inbox_clocksstays keyed by profile ID. It only orders inserts, so two accounts' same-ID profiles serialize their inserts without changing either inbox's order.docs/architecture/notifications.mdnow states the account-and-profile rule and the tables still keyed by profile ID alone.Validation
main: after both accounts'defaultprofiles favorited one series, there was one interest row, and a new episode reached one account.user_profiles, and an orphan row.defaultprofiles follow one series checks that each delivery queues only its own account's webhook, web push subscription, and push device.defaultprofiles on one series: interest rows, deliveries, inbox, preferences, recompute, and profile purge. A reproduction test failed onmainbefore the fix. The notifications, API v2, and notification handler suites pass against the migrated database.make lint-changed,migrate-validate,verify-migrations, the API v2 OpenAPI, contract, and fixture checks, and the other Go gate checks pass.make test-gopasses exceptTestRunStatsWithRealFFmpeg, which fails onmainon the same host. With a database configured, five unrelated handler tests (plugins, subtitles, catalog transfer, local login) also fail onmain.Evidence: https://evidence.siloserver.org/r/silo-server/issue-2597/
Evidence
Evidence: https://evidence.siloserver.org/r/silo-server/issue-2597/
Surface and build:
/api/v2/notifications,/api/v2/notifications/unread-count, and/api/v2/notifications/preferences. Each was requested as two accounts, each with a legacydefaultprofile, on one dev deployment: first onmain(21cdfa1), then on this PR (817ab22).main)GET /notifications(account 2 holds the only delivery)GET /notificationsenabledwithPUT /notifications/preferencesenabledchanges with itenabledstays as it wasThe evidence page has the full request output, the fanout recipients before and after, and the migration checks.
Risks
CONCURRENTLY. The primary-key swap onprofile_series_interestand the rewrite ofnotification_preferencestake brief exclusive locks.maintoday.Follow-ups:
defaultprofiles share one email row, address included: [bug] Two accounts' same-ID profiles share one email notification setting, address included #2600. This PR narrows the leak (only the owning account's deliveries now reach that address). The fix should follow this PR, since both touchemail_prefs_repo.go.Checklist
AI Disclosure
idtiebreak on the unread index. The rolling-deploy findings are listed under Risks, and the email-settings finding became [bug] Two accounts' same-ID profiles share one email notification setting, address included #2600. A background adversarial review (Codex, gpt-6.1-sol) of 4e73952 found no issues.🤖 Generated with Claude Code
Note
Key notification state by account and profile instead of profile ID only
profileKeyidentity type in interest_updater.go and threads it through the fanout worker, delivery and preference repositories, email digest channel, inbox pagination, and the API handlers in notifications.go.System.PurgeProfileremain profile-ID scoped.DeliveryRepository,PreferencesRepository,InterestRepository, and handler methods now require a user ID argument; in-tree callers are updated.Macroscope summarized 817ab22.