Skip to content

perf: load watch-only accounts after node start - #2248

Open
ovitrif wants to merge 4 commits into
masterfrom
perf/watch-only-accounts-after-start
Open

ovitrif wants to merge 4 commits into
masterfrom
perf/watch-only-accounts-after-start

Conversation

@ovitrif

@ovitrif ovitrif commented Dec 3, 2026 •

Copy link
Copy Markdown
Collaborator

Refs: #2238
Twin: synonymdev/bitkit-ios#1584
Source: self, #2238

Description

  • Builds the node without the Paykit Server watch-only accounts, because each one added about 1.9 s to the build (22.5 s with 5 accounts vs 13.4 s without on 3.2.0 RC3)
  • Adds WatchOnlyAccountsWorker, plain unique work with a network constraint (no foreground notification), queued by LightningRepo as soon as the node runs (and when adopting a running node) whenever the wallet tracks accounts or has removals pending, or the read fails; it reads under the account lock, so an approval in flight at start counts. The accounts load into the running node and keep loading when the app closes right after start; a transient failure retries on a linear 15 s backoff, a rejected xpub does not
  • Loads each account in its own LDK queue hop and lock hold, so sends and Pubky auth approvals are not held behind all of them; tracking is re-checked before each load
  • Removes the account reconcile from sync() and the in-process retry loop, which the worker replaces
  • Adds an AGENTS.md rule: network work that must survive the app closing runs in WorkManager workers
  • Updates docs/pubky-auth-companion-claims.md, which said the accounts load before the node starts

Out of Scope

Design

N/A — no UI changes.

Preview

N/A — no user-visible changes.

QA Notes

Setup

  • Mainnet or staging wallet on this PR's build, with 2 or more Paykit Server accounts approved (Pubky auth sheet with the watch-only account claim) and tracking on
  • adb logcat filtered on the app to read node start and account load lines

Journeys

  • J1: journeys/pubky-marketplace/approval-while-node-restarts.xml — route changed: an account approved while the node is down now loads through WatchOnlyAccountsWorker; step 21 now expects only "Registered derived on-chain wallet account"

Manual Tests

  • 1. cold start → Building node… to LDK node setup no longer grows per account; Loaded Paykit Server accounts logs shortly after Node started
  • 2. close the app right after the node starts, reopen later → watch-only account balances show
  • 3. toggle tracking of a Paykit Server account off and on → balance hides and returns

Automated Checks

  • added WatchOnlyAccountsWorkerTest.kt
  • updated LightningRepoTest.kt, LightningServiceTest.kt, WatchOnlyAccountLifecycleCoordinatorTest.kt, BackupRepoTest.kt
  • Local verification: ./gradlew testDevDebugUnitTest detekt (the suites above)

Evidence

Gaps

  • None

Models used

  • Planning/scoping: Claude Opus 5.5 (reasoning: medium)
  • Implementation: Claude Opus 5.5 (reasoning: medium)
  • Review: Claude Opus 5.5 (reasoning: medium), Gemini 4 Neon (reasoning: high)
  • Review rounds: 2

Build the node without the Paykit Server accounts, each of which added about
2 s to the build, and load them into the running node from a WorkManager
worker queued after every start that tracks accounts, so the load survives
the app closing.

- Reconcile unloads and completes pending removals in one lock hold, then
  loads each account in its own LDK queue hop and lock hold, re-checking
  tracking before each load.
- Wallet sync no longer reconciles accounts; the worker retries a transient
  failure with WorkManager backoff instead of the in-process retry loop.

Verified: ./gradlew testDevDebugUnitTest detekt
@ovitrif ovitrif added this to the 3.3.0 milestone Dec 3, 2026
@ovitrif ovitrif self-assigned this Dec 3, 2026
@ovitrif
ovitrif requested review from a team, ben-kaufman and coreyphillips and removed request for a team December 3, 2026 15:52
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Dec 3, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-12-03T15:56:42.662551Z a84d871 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@greptile-apps

greptile-apps Bot commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

Confidence Score: 3/5RetriggerView in Greptile

[Medium impact] The change affects startup and account loading for wallets that track Paykit Server accounts.

Fix node ownership during background loading and the startup tracking race before merging.

Findings

  1. P1 Closing the app abandons loading ▶
  2. P1 Startup can miss enabled tracking ▶

Summary

Moves Paykit Server account loading out of node construction and into WatchOnlyAccountsWorker.

  • The node starts first, then a worker loads tracked Paykit Server accounts.
  • Account loading gives other node work a turn between accounts.

Verification: Source review only; no builds or tests were run. GitHub draft-status checks were blocked by missing authentication.

Reviewer model: gpt-6.1-sol
Reasoning effort: Unknown
Metadata note: The supplied execution context identifies the model but does not provide a reasoning-effort setting.

Diagram

sequenceDiagram
  participant R as LightningRepo
  participant N as LightningService
  participant W as WorkManager worker
  participant U as App lifecycle
  R->>N: Start node without server accounts
  R->>W: Queue account loading
  W->>N: Load accounts one at a time
  U->>R: App closes without background service
  R->>N: Stop node after five seconds
  W->>N: Continue loading
  N-->>W: Skip released node
  W-->>W: Finish successfully
Loading

Reviews (1) · Last reviewed commit: "docs: run app-close-proof network work i..." · Reviewed by Greptile

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: a84d871370

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@github-actions

github-actions Bot commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

Regtest APK

Built from 6f2d58d (run).

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

@coreyphillips coreyphillips left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Two independent reviews.

worth doing, does not block

  • Expedited worker shows a foreground notification on Android 9 to 11 at every start (app/src/main/java/to/bitkit/repositories/WatchOnlyAccountsWorker.kt:43)
  • Transient load failures now back off exponentially, up to 5 hours (app/src/main/java/to/bitkit/repositories/WatchOnlyAccountsWorker.kt:71)
  • Journey for the reworked load path is not listed, and one step is now stale (journeys/pubky-marketplace/approval-while-node-restarts.xml:21). approval-while-node-restarts.xml is the existing end-to-end check that an account approved while the node is down is tracked once the node starts. That load now goes through the worker instead of the build, so this PR changes the journey's route. Step 21 still accepts "Loaded configured wallet for OnchainWalletAccount" as evidence, and nothing can log that line now that the build loads no accounts. The repo's journey rules ask for a route-changed journey to be listed under Journeys and updated. Step 21 should expect only "Registered derived on-chain wallet account".

nits

  • Account load failures are logged twice, and the success log can mislead (app/src/main/java/to/bitkit/repositories/WatchOnlyAccountsWorker.kt:77)

the reviewers disagree, your call

  • PR description is missing the Source line, QA case IDs and Setup (general). Objection: The repository requires all three items in this situation. I could not retrieve the PR description to confirm they were missing. Answer: I fetched the body with gh pr view 2248 at head a84d871. The reference block is only Refs: #2238, which closes nothing, and there is no Source: line, which pr.md:127 requires in that case. The three Manual Tests are - [ ] mainnet wallet... with no 1./2./3. IDs (pr.md:198), and QA Notes has no #### Setup, #### Evidence or #### Gaps (pr.md:203-212), even though the tests need a mainnet wallet with several approved Paykit servers.
  • Startup can miss the first Paykit account approved during a rebuild (app/src/main/java/to/bitkit/repositories/LightningRepo.kt:672-675)
  • Post-load sync makes sends and approvals wait for account scans (app/src/main/java/to/bitkit/repositories/WatchOnlyAccountsWorker.kt:79-81)

@talosmachina talosmachina left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

No findings. Builds the node with no Paykit Server accounts and loads them afterwards in a WorkManager worker queued on each start and on adopting a running node, one account per LDK queue hop. Reviewed a84d871, full tier, static: not built here, CI is the gate.

What I checked, and 5 candidates I ruled out

Read in full: WatchOnlyAccountsWorker.kt, the LightningService.kt and LightningRepo.kt diffs, WatchOnlyAccountsWorkerTest.kt
Call sites traced: reconcileWatchOnlyAccounts (worker, BackupRepo.restoreWalletBackup), hasTrackedWatchOnlyAccounts, setAccountTracking in WatchOnlyAccountRepo.kt, listOnchainWalletAccounts in CoreService.kt
Upstream checked: ldk-node add_onchain_wallet_account / remove_onchain_wallet_account at v0.7.0-rc.85
CI: build, build-and-test, lint, detekt, compose-ui green; e2e shards still running at review time

Ruled out

  • History lost by loading at runtime instead of at build: ldk-node reloads a persisted account's BDK store and full-scans an account it has not synced this session, so a runtime add sees the same history a build-time add did.
  • Pending removals cleared while new ones are queued: unloadUnwantedWatchOnlyAccounts unloads and calls completeReconciliation in one lock hold, before any load.
  • Tracking toggled off mid-load: loadWatchOnlyAccount re-reads the store under the lock before each add; covered by reconciliation skips an account whose tracking was turned off while others loaded.
  • Worker loading into a replaced node: REPLACE on each start, plus the node !== check inside every queue hop.
  • Rejected xpub retried forever: isRejectedPaykitServerAccount returns success, and a transient failure is rethrown ahead of a rejected one so it retries; both are tested.

Merge confidence: 4/5, the behaviour hinges on WorkManager timing after app close, which no automated test covers; manual tests 2 and 3 on a device are the evidence.

Reviewer model: Claude Opus 5.5
Reasoning effort: Unknown (not exposed to the reviewing session)

The worker is plain unique work with a network constraint, so Android 9
to 11 show no foreground notification at each start, and a transient
failure retries on a linear 15 s backoff instead of an exponential one.
It is queued once the node runs and keeps its network graph.

The check before queuing reads the store under the lifecycle lock, so
an approval that began before the node ran counts, and it also counts
removals a restore left pending. The load stops once the node is
stopped or replaced, and the worker then skips the sync. Each failure
is logged once, and "Loaded Paykit Server accounts" logs only for a
current node. The approval-while-node-restarts journey no longer
expects the build log line, since the build loads no accounts.

Verified: ./gradlew compileDevDebugKotlin testDevDebugUnitTest (WatchOnlyAccountsWorkerTest, LightningServiceTest, LightningRepoTest, WatchOnlyAccount*, BackupRepoTest) detekt
The worker is queued right after the node reaches Running, so a later
start step that fails or is cancelled no longer leaves the Paykit
Server accounts unloaded, and it is also queued when the tracked read
fails. Adds tests for the tracked check, the work request and the sync
after a rejected account, and corrects the companion claims doc, which
still said the accounts load before the node starts.

Verified: ./gradlew compileDevDebugKotlin testDevDebugUnitTest (WatchOnlyAccountsWorkerTest, LightningServiceTest, LightningRepoTest, WatchOnlyAccount*, BackupRepoTest) detekt
@ovitrif

ovitrif commented Dec 3, 2026

Copy link
Copy Markdown
Collaborator Author

Description updated: Source: line, QA case IDs, Setup, Evidence and Gaps, and approval-while-node-restarts.xml listed as J1. Its step 21 now expects only "Registered derived on-chain wallet account" (c98fc07).

@ovitrif

ovitrif commented Dec 3, 2026 •

Copy link
Copy Markdown
Collaborator Author

QA for 6f2d58d:
Tested on Android 16 emulator (Pixel 9), regtest, against the bitkit-docker Pubky marketplace fixture (local Pubky testnet and Paykit Server).

🟢 J1, 1. Cold start, 2. Close right after start, 3. Toggle tracking
J1: approval-while-node-restarts

With the node stopped and VSS held, the setup completed on the first Authorize, with no toast, and the account's tracking was stored without a node. Once VSS was released, WatchOnlyAccountsWorker loaded account 1 half a second after the node started, and the list shows one "paykit server" account at m/84'/1'/1' with tracking on.

android-j12x.mp4

1. Cold start

With three tracked accounts, the node built in 1.2 s and started without them; the worker registered all three within 5 s of start.

android-m12x.mp4

2. Close right after start

The app was killed right after the node started, before any account loaded. On reopen the worker loaded all three accounts, and Savings shows the 50,000 sats held by account 1.

android-m2n2x.mp4

3. Toggle tracking

Tracking off unloaded account 1 and Savings dropped to 0 at the next balance refresh; tracking on loaded it again and the 50,000 sats returned.

android-m3b2x.mp4

This branch has not been deployed

No deployments
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.

3 participants