Skip to content

fix(profiles): recover from access changes without failed loads - #440

Merged
Quick104 merged 26 commits into
Silo-Server:mainfrom
Rhainland:fix/profile-verification-recovery
Oct 10, 2026
Merged

Quick104 merged 26 commits into
Silo-Server:mainfrom
Rhainland:fix/profile-verification-recovery

Conversation

@Rhainland

@Rhainland Rhainland commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

Merge order: this change is backwards compatible and safe to merge before the server changes. Its access-change behavior takes effect once Silo-Server/silo-server#1780 ships (policy changes stop signing users out; the events socket sends an access_changed signal) and Silo-Server/silo-server#1781 ships (role changes force a token refresh instead of a sign-out). Against a server without them, the new frame, close code, and 401 type never arrive and that code does nothing.

Problem

Related issue: Silo-Server/silo-server#1404
Validation tasks: changes #313 C3, #314 C3, #317 C5, #318 C5

Calendar and person detail are touched only in minor ways, but a re-test of those cases is worthwhile. A remembered PIN profile still opens without a prompt while the server accepts its token; the picker appears only after the server rejects it.

Silo is about to stop signing users out when an administrator moves them to another access group or changes their permissions or playback-quality override. Today those changes sign the user out, and Android handles that by returning to Login. After the server change:

  • A PIN-protected profile keeps its session, but the server rejects its profile token on the next request with 403 profile_verification_required. Android has no general handling for that response, so every screen shows a failed load and the user has no way back except switching profile by hand. On phone, even that fails: the profile picker loads with the stale token still attached and gets the same 403.
  • The events socket sends {"type":"access_changed"} and closes with code 4001. Android reconnects, but nothing outside Home refetches, so libraries, search, details, and collections keep showing what the old policy allowed.
  • The notifications socket gives up for good after a 401 or 403 on its ticket, so the unread badge stops updating live for the rest of the session even after the user enters the PIN again.
  • A role change answers 401 token_refresh_required. Android already refreshes on any 401; this needed confirming, not changing.

The same 403 can already happen today, for example when another device changes a profile's restrictions or PIN, so the first fix also helps against current servers.

Approach

  • Stale PIN profile. The v2 error decoder reports a 403 profile_verification_required along with the profile id and token the refused request sent. A recovery clears the profile selection only if that is still the active profile and token on the active server; the account stays signed in. Phone and TV then open the profile picker. Reports for an identity that has since changed (another profile, a re-entered PIN, a server switch, a pinned background request from an earlier identity, a remote-playback overlay) are ignored, so repeated refusals cannot loop. Household-management calls (editing, creating, or deleting profiles) are excluded because the server uses the same problem type there for a missing permission.
  • Background work. Downloads and outbox replay can trigger the recovery when they ran on the active identity, since that identity is just as stale. They never navigate: the picker waits while the player, audiobook player, reader, a sign-in screen, or a remote-playback overlay is in front, and the prompt is dropped if the user signs out, switches server, or picks a profile first. If the picker is already open, it reloads its list in place so a PIN being typed is kept.
  • Access change on the socket. The events socket reports the access_changed frame and the 4001 close once per connection. Home, the library lists and tabs (including the TV library page and its Collections pill), search, item details, and collections refetch on phone and TV; a detail page whose title the server now refuses shows the same error as a fresh load, and reloads the title if a later change restores access, coalesced so both sockets reporting the same change, up to a server check interval apart, cause one refresh. The socket then reconnects with a new ticket; if the profile token went stale, that ticket request triggers the profile recovery above.
  • Change made while in the background. Both sockets close when the app stops, and the server compares each new connection with the policy its own ticket was minted under, so a change made in that time never reaches the socket. When the app returns to the foreground, it fetches the library list and reports an access change if the set of libraries differs from the cached list the screens were built from.
  • Notifications loop. After an auth-class close the loop now waits for the next identity change (PIN re-entry, profile switch, account or server change) and then reconnects, instead of stopping for the session. A 4001 close is an ordinary reconnect.
  • Role change. No code change; a test now pins that 401 token_refresh_required refreshes and retries once on both the active and the pinned background path without ending the session.

The refreshed screens use their existing quiet refresh paths. Offline caches are keyed by the full profile identity, including its token, so a re-verified profile does not read entries written under the old token.

Validation

Commands assume the repository root is the cwd, with JDK 21.

./gradlew :shared:testDebugUnitTest :android-shared:testDebugUnitTest :androidApp:testDebugUnitTest :androidTvApp:testDebugUnitTest --continue
  • shared: 1484 tests, 0 failures. android-shared: 1617, 0 failures. androidTvApp: 1159, 0 failures.
  • androidApp: 694 tests, 1 failure: ReflowStyleTest > line height flows from settings. It fails on this machine because its locale formats 1.9 as 1,9; it touches no code in this change.

New tests cover: detection of the 403 with the presented identity, and no report for other problem types, household-management calls, or requests without a profile; the recovery clearing only the active identity, never signing out, ignoring stale, foreign, pinned-earlier, and overlay reports, clearing once for repeated refusals, surviving a failing clear, deferring behind playback and a remote-playback overlay, keeping the prompt through unrelated identity transitions, consuming it on the picker, and dropping it after a profile pick or sign-out; the notifications loop parking on an auth close, resuming after a reset, and reconnecting after a 4001-style close; Home refreshing once per burst of access changes and reconnecting after the close; the events socket against a MockWebServer that sends the frame and a 4001 close, only a 4001 close, or an ordinary close; and the 401 token_refresh_required refresh on the active and pinned paths.

./gradlew :android-shared:lintDebug :androidApp:lintDebug :androidTvApp:lintDebug :androidApp:assembleDebug :androidTvApp:assembleDebug --continue

Lint passed on all three modules with 0 errors and no warnings on changed lines (existing findings stay in each module's baseline). Both debug APKs built.

Review fixes (7ddd8524, b120a00c): the phone detail tests (:androidApp:testDebugUnitTest --tests 'org.siloserver.silo.android.ui.screens.detail.*', 33 tests) and TV detail tests (:androidTvApp:testDebugUnitTest --tests 'org.siloserver.silo.tv.ui.screens.detail.*', 202 tests) pass on repeated runs. The new tests cover a refused title reloading once access returns and a still-refused title staying hidden instead of repainting its cached detail. Lint and the full module suites were not re-run for these two commits.

Review fixes (0b89049d, c921bf05): :shared:testDebugUnitTest --tests 'org.siloserver.silo.repository.PersonalDataRepositoryCacheTest' covers the foreground library-list comparison (a changed set, the same set in another order, and no cached list); the TV detail access-change and next-up tests pass; both apps compile.

Maintainer review (9025078f to 5805bc62): merged main. The foreground library check now goes through recheckUserLibraries, so hidden libraries no longer count as an access change. The commits also fix Back from a picker opened while adding a server, older phone search and Collections loads overwriting the access-change refresh, and a pending facet load refilling Browse facets. The full unit suites pass on 5805bc62 (shared 1886, android-shared 1715, androidApp 830, androidTvApp 1255 tests, 0 failures), along with lint on all three modules and both debug builds.

Not run: lintVitalRelease, the supply-chain scripts, and a manual check on a device against a server running Silo-Server/silo-server#1780 and #1781 (they are not merged yet).

Risks

  • A user on the phone's edit-profile screen whose own token went stale sees the server's error there; the next read on any other screen opens the picker.
  • Some screens refresh only while visible; a screen opened later loads fresh data anyway.
  • A change made while the app was in the background is detected only when it changes which libraries the viewer can see. Other policy changes (maturity limits, playback quality) show up when the affected screen next loads.
  • For You, Calendar, Browse, collection detail, library collection detail, and person detail do not listen for access changes yet; they are left for a follow-up.
  • Nothing changes for servers that never send the new signals.

Checklist

  • I read and can explain the complete diff.
  • This pull request addresses one concern.

AI Disclosure

  • Harness: Claude Code
  • Tool(s): Claude Code
  • Model(s): claude-opus-5-5
  • Involvement: Fully AI-generated, human verified
  • Adversarial review: a separate Claude Code agent (claude-opus-5-5) reviewed the full diff read-only for loops, wrong-identity clears, navigation, coroutine, DI, websocket, and compatibility bugs. It found that a deferred prompt could be dropped by a remote-playback overlay or an unrelated identity transition, that a prompt on the open picker reset PIN entry, that the recovery coroutine lacked an exception guard, and that an eager DI binding could fail cold start. All four are fixed and covered by tests; it found no compatibility break. A second review of the finished branch confirmed the wire formats against the server and found one more issue: the Home and notifications sockets report the same change up to 15 seconds apart, so the 750 ms coalescing let both through and screens such as search refetched twice. Fixed: the first report refreshes at once and reports within 20 seconds count as the same change; a test covers it. Bot review on this pull request found that a deferred profile prompt was not re-checked when remote playback ended, that detail pages kept a title the viewer lost access to, that the TV library page and Collections probe did not refresh, that a startup failure logged the raw exception, and that a detail page left unavailable did not reload when access returned; all are fixed. A maintainer review by Claude Code (claude-opus-5-5), with a cross-provider round by Codex (gpt-6.1-sol), merged main and fixed five more findings. One finding is still open: the profile prompt does not survive process death.

Note

Recover from viewer access changes without failed loads

  • Adds a shared AccessChangeSignals coordinator in AccessChangeSignals.kt. It collects stale-profile refusals from the v2 error decoder, access-change websocket frames, and close code 4001, and exposes cursor-based flows that coalesce duplicate reports
  • Adds ProfileVerificationRecovery in ProfileVerificationRecovery.kt. A stale active profile is cleared while the account session is kept, and a pending prompt routes the user to profile selection (deferred on player, sign-in, and setup routes) in AppNavigation.kt and TvAppNavigation.kt
  • Auth-class realtime closures now park the reconnect loop instead of ending it. NotificationsRepository.reset wakes the parked loop via a StateFlow-backed identity epoch and clears realtimeFatal
  • Mobile and TV screens (detail, search, libraries, collections) subscribe via the new OnViewerAccessChanged composable and refresh visible data when an access change is reported. Several ViewModels (e.g. HomeViewModel, CollectionsViewModel, TV library/search ViewModels) now track load Jobs and generations so stale responses from superseded or pre-change loads cannot overwrite newer results
  • Risk: NotificationsRepository.connectRealtime changes auth-failure behavior from permanent termination to a parked wait on the identity epoch — check that path and realtimeFatal consumers. Also, ApiResult.isAccessRefusal classifies 403 as a refusal only when the problem code is not PROFILE_VERIFICATION_REQUIRED, so any code branching on refusal status should be reviewed

Macroscope summarized 32e759c.

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants