Skip to content

fix: flag session restore on pubky init fail - #1305

Merged
jvsena42 merged 3 commits into
masterfrom
fix/1093-flag-pubky-init-failure
Sep 18, 2026
Merged

jvsena42 merged 3 commits into
masterfrom
fix/1093-flag-pubky-init-failure

Conversation

@jvsena42

@jvsena42 jvsena42 commented Sep 17, 2026 •

Copy link
Copy Markdown
Member

Refs #1093
Related to #1275

This PR shows the "Session expired" toast when Paykit fails to start because the saved Pubky session is rejected.

Description

  • Flags a failed session restore when Paykit startup fails with an identity error and a Pubky session is saved. Before this, startup logged the error and returned early, so the user saw nothing.
  • Leaves the flag unset for other startup failures (for example storage errors) and for wallets with no saved session, so users who never enabled Paykit get no "Session expired" toast.
  • Keeps the saved session and secret key in the keychain, the single error log, and startup completion unchanged.
  • Adds a helper that checks a failure's cause chain for a Paykit identity error. It follows the existing Trezor and broadcast exception helpers.

Out of Scope

Design

N/A — no UI changes.

Preview

N/A. The toast and its text already exist; only the condition that shows it changed. No device run was done: a stale session cannot be forced on the shared emulator without tampering with its real Paykit session.

QA Notes

Manual Tests

  • 1. Wallet with a saved Paykit session that the SDK rejects with an identity error (e.g. missing required capabilities) → launch app: "Session expired" toast shows; Profile shows signed out.
  • 2. regression: Wallet that never enabled Paykit → force-stop → launch app: no "Session expired" toast.
  • 3. regression: Wallet with a valid Paykit session → force-stop → launch app: no toast; Profile and Contacts load.

Automated Checks

  • Unit tests added: PubkyRepoTest.kt checks that an identity error with a saved session sets the flag without calling session import or deleting keychain entries, and that a storage error with a saved session does not set it.
  • Unit tests modified: PubkyRepoTest.kt startup-failure test now also checks the flag stays unset when no session is saved.
  • Unit tests added: PaykitExceptionExtTest.kt covers wrapped identity, storage and generic errors.
  • Unit tests added: PubkyRepoTest.kt pins the saved-session half of the guard — an identity error with no saved session leaves the flag unset and does not import a session.
  • Local runs: just compile, just test, just lint, all green. Negative controls: with the fix removed, the identity-error test failed; with && hasSavedSession() dropped from the guard, exactly the new no-saved-session test failed (1 of 104 in PubkyRepoTest.kt).

jvsena42 and others added 2 commits September 17, 2026 08:44
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@jvsena42 jvsena42 self-assigned this Sep 17, 2026
@greptile-apps

greptile-apps Bot commented Sep 17, 2026

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

The PR appears safe to merge, with the new failure signal narrowly gated and covered by focused unit tests.

Summary

This PR reports a failed saved-session restoration when Paykit initialization encounters an identity error, while leaving unrelated initialization failures unchanged.

  • Adds cause-chain classification for Paykit identity exceptions.
  • Requires a nonblank saved session before publishing the restoration-failure state.
  • Adds focused tests for wrapped exceptions and startup failure classification.
  • Documents the corrected startup behavior in the changelog.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A[Start Pubky repository] --> B[Initialize Paykit service]
    B -->|Success| C[Continue saved-session restoration]
    B -->|Failure| D{Identity error in cause chain?}
    D -->|No| E[Log and finish initialization]
    D -->|Yes| F{Saved session exists?}
    F -->|No| E
    F -->|Yes| G[Flag session restoration failure]
    G --> H[Existing UI shows Session expired toast]
Loading

Reviews (1) · Last reviewed commit: "fix: flag session restore only on paykit..."

@github-actions

github-actions Bot commented Sep 17, 2026 •

Copy link
Copy Markdown
Contributor

Regtest APK

Built from 8f47f9f (run).

Download bitkit-dev-debug universal APK (expires in 30 days).

@jvsena42 jvsena42 left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Approved — no HIGH/MEDIUM findings at head d303825e4. (Posted as a comment because GitHub does not allow approving your own PR.)

Checked: Traced the new flag path in PubkyRepo.initialize (line 186) end to end: on an identity error the code only flips _sessionRestorationFailed; no keychain delete, no signOut, no clearLocalState runs on that path, and the only consumer (AppViewModel.kt:503-510) shows the toast and clears the flag, so nothing is discarded for transient failures. Offline/transport failures are a separate PaykitException.Transport class in the rc55 bindings and are excluded by isPaykitIdentityError, so a network outage cannot trigger the toast (the Rust-side mapping itself is not checked out locally and was not verified). The deferred stale-session branch in PaykitSdkService.initialize (lines 235-251) resumes stored session access in finally before rethrowing, so a non-deferrable identity error still leaves the saved session intact. The flag is set before initializationReady completes, but it is a StateFlow so a late AppViewModel subscriber still sees it; the second initialize caller (WalletViewModel.kt:243, restore path) hits isServiceInitialized=true when the SDK started, and when it did not the toast repeats only in that restore run. Re-auth after this toast still fails because startAuth awaits the poisoned isSetup, and the toast repeats every launch; both are stated as accepted limitations in the PR body and #1275 scopes the fix to setting the flag, so not raised. The path is only reachable for wallets that created a Pubky session through the dev-gated Paykit UI. No iOS twin PR found.

@jvsena42
jvsena42 requested a review from ovitrif September 18, 2026 13:26
@jvsena42
jvsena42 enabled auto-merge September 18, 2026 13:56

@ovi-reviewer ovi-reviewer Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Verdict: ✅ Approve


Review: diff 5 files.

Findings:
1 inline (non-blocking)

Audit:
Awaits QA.

Coverage:
QA: journeys and manual tests queued


Reviewed by claude-opus-5-xhigh via gh-pr-review-loop skill
Commands: @ovi-reviewer review · test · retest · audit (author or owner)

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

@jvsena42 jvsena42 left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Approved — no blocking HIGH/MEDIUM findings at head 8f47f9fc2. (Posted as a comment because GitHub does not allow approving your own PR.)

Checked: Follow-up: Follow-up on 8f47f9f (test only). The new case builds the repo with createSut(), whose init launches initialize(), and awaitInitialization waits for that job, so it runs the production path. It throws the same AppError(PaykitException.Identity(...)) that the sibling case uses to prove isPaykitIdentityError() is true, and it stubs PAYKIT_SESSION to null. hasSavedSession() (PubkyRepo.kt:231) is therefore false and the flag must stay false. Removing && hasSavedSession() from PubkyRepo.kt:186 sets _sessionRestorationFailed to true and fails the new assertFalse. The commit touches only PubkyRepoTest.kt.

@ovitrif ovitrif left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

utAck

@jvsena42
jvsena42 merged commit 4ffec09 into master Sep 18, 2026
19 checks passed
@jvsena42
jvsena42 deleted the fix/1093-flag-pubky-init-failure branch September 18, 2026 16:19
@piotr-iohk piotr-iohk mentioned this pull request Oct 16, 2026
3 of 5 tasks
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