Skip to content

fix: report rejected spark exit data as not restored - #1979

Merged
ovitrif merged 4 commits into
fix/spark-follow-upsfrom
fix/restore-rejected-exit-data
Nov 19, 2026
Merged

ovitrif merged 4 commits into
fix/spark-follow-upsfrom
fix/restore-rejected-exit-data

Conversation

@ovitrif

@ovitrif ovitrif commented Nov 18, 2026 •

Copy link
Copy Markdown
Collaborator

Closes #1976

This PR stacks on bitkit-android#1957, which adds the handling of exit data Spark rejects. Its base moves to master once that PR merges.

A wallet restore reported Spark's exit data as restored as soon as it was staged. Spark imported it only later, at its start, so onboarding showed a complete restore even when Spark then rejected the data.

Description

  • A restore connects Spark to import its exit data. The wallet's start waits for the restore, so Spark wasn't connected when the restore staged the data. The restore now connects Spark itself and imports the data at once. When Spark rejects it, the restore lists the exit data as not restored. A restore that can't connect Spark keeps the data staged, as before, for Spark's start to import.
  • Spark's events are collected before the restore connects it. Spark's listener drops events no one collects, and the wallet's start collects them only after the restore, so the restore starts the collection first. Both the restore and Spark's start wait until it has subscribed, so no event of a later connect is lost. The restore first takes back the sends held before it, so an earlier attempt's failure Spark reports meanwhile leaves a held retry alone.
  • The restore and Spark's start never import the staged data at once. Staging and importing share one lock, so an import can't delete data a restore staged after it.

Out of Scope

  • A restore that can't connect Spark still reports staged exit data as restored, as the import then waits for Spark's start.

Design

N/A — no UI changes. The restore screen already lists categories that were not restored.

Preview

N/A

QA Notes

Setup

N/A — no device testing required: the case needs a VSS backup holding exit data Spark rejects, which neither a journey nor the app can stage.

Journeys

N/A — not drivable; see Manual Tests.

Manual Tests

N/A

Automated Checks

  • added SparkRepoTest.kt — a restore made before Spark connects connects it and fails when Spark rejects its exit data, imports data Spark accepts once, keeps the data staged when Spark can't connect, after it tried, handles Spark's events after a restore's connect, keeps a held retry past an earlier attempt's failure during the restore, and keeps and imports a restore's data staged while the start imports older data

Evidence

  • Both connect cases fail without the new connect and pass with it, the events case fails without the early collection, and the held-retry and lock cases fail without their fixes.
  • just compile, just test and just lint pass.

Gaps

  • No device run: staging a backup with exit data Spark rejects needs a crafted VSS object.

Models used

  • Planning/scoping: Claude Opus 5.5 (reasoning: medium)
  • Implementation: Claude Opus 5.5 (reasoning: medium)
  • Review: Claude Sonnet 5.5 (reasoning: high)

The wallet's start waits for the restore, so Spark wasn't connected when the restore staged its exit data and the restore reported it restored before Spark could reject it. The restore now connects Spark and imports the data at once, and staging and importing share a lock.

Verified: the new SparkRepoTest connect cases fail without the connect and pass with it; just compile, just test, detekt and lintDevDebug pass.
@ovitrif ovitrif self-assigned this Nov 18, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 18, 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-11-18T20:06:12.567034Z d1ecf28 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.

@ovitrif
ovitrif requested review from a team, ben-kaufman and coreyphillips and removed request for a team November 18, 2026 20:05
@greptile-apps

greptile-apps Bot commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 4/5

[Medium impact] The changes affect Spark exit-data handling during wallet backup restore.

Start collecting Spark events before the restore connects it before merging.

Findings

  1. P1 Restore drops Spark events ▶
  2. P2 Import races lack coverage ▶

Summary

The restore now connects Spark before reporting whether its exit data was imported, and shares a lock with startup imports.

  • Wallet restores connect Spark and check its exit-data import result.

Reviews (1) · Last reviewed commit: "fix: report rejected spark exit data as ..." · Reviewed by Greptile

@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. Has the restore connect Spark itself and import the staged exit data right away, so a rejection shows up as SPARK_EXIT not restored, and puts staging and import behind one lock. Reviewed d1ecf28, full tier, stacked on #1957 (fix/spark-follow-ups), so only the delta against that base was reviewed.

What I checked, and 5 candidates I ruled out

Read in full: SparkRepo.restoreExitBackup, connectForRestore, importRestoredExitState, start, importStagedExitState, discardStagedExitState, SparkService.connect/disconnect, the SPARK_EXIT step of BackupRepo.performFullRestoreFromLatestBackup, performRestore, uploadSparkExitBackup, and the exitStateGeneration listener
Call sites traced: restoreExitBackup (BackupRepo only), importRestoredExitState (start, restoreExitBackup, importStagedExitState), sparkRepo.start (LightningRepo, SparkExitRepo)
Build: not built here. On this repo the review is reasoned from the code and CI, without running the branch's Gradle build
CI: lint, build-local, detect-changes green; build-and-test and ui-tests still running at review time

Ruled out

  • Deadlock on exitImportMutex: Kotlin's Mutex is not reentrant, but restoreExitBackup releases it after the write and before calling importRestoredExitState, and no other path holds it around that call.
  • Start and restore both connecting: SparkService.connect runs under its own mutex and returns false once sdk is set, so the later start() neither reconnects nor bumps _exitStateGeneration a second time; connectForRestore bumped it once, and the StateFlow replays it when BackupRepo starts observing.
  • Start's import deleting data staged after it: the write now waits on the lock until the import's file.delete() is done, and a rejection that start() swallows is hit again by the restore's own import, which then reports it.
  • Connect failures escaping the restore: connectForRestore catches WipeInProgress, MnemonicNotFound and SDK errors, so the restore falls back to staging, covered by the new can't-connect test.
  • Events emitted between the restore's connect and start(): the collector only starts in start() and SparkService.events has no replay. Already raised inline by Greptile, so not repeated here. It looks reachable to me: leaf optimization can start on that connect, and start() runs only after the remaining restore categories.

Merge confidence: 4/5, the dropped-events point Greptile raised is still open and build-and-test had not finished.

Reviewer model: claude-opus-5-5
Reasoning effort: Not exposed (the review runtime does not report a setting)

@github-actions

github-actions Bot commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Regtest APK

Built from ae27cfd (run).

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

@ovitrif

ovitrif commented Nov 18, 2026

Copy link
Copy Markdown
Collaborator Author

Triage: this PR closes #1976, in the 3.2.0 milestone. It fixes the restore result for Spark exit data, which ships in 3.2.0 with #1957.

@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

  • Spark connected by the restore drops SDK events until start() runs (app/src/main/java/to/bitkit/repositories/SparkRepo.kt:1372-1376). connectForRestore is now the only path that connects Spark before start() has launched the event collector, so every SDK event in between is lost. SparkService.connect (SparkService.kt:174-199) attaches the listener that emits into _events, a MutableSharedFlow with no replay. Its only subscriber is launched by start() (SparkRepo.kt:282-284), and the wallet start runs that only after the restore finishes the WALLET, BLOCKTANK and ACTIVITY categories. With no subscriber, emit drops the value. The restored wallet's first sync emits payment, deposit-claim, AutoOptimization and UnilateralExitStateChanged events in that window. Most of the loss is recovered: start() syncs, refreshes and syncs activities, and the restore's own bump of _exitStateGeneration still triggers the exit backup. Optimization state is not recovered. A leaf optimization that started in the gap leaves isOptimizing false and sparkOptimizationActivityAt unstamped, so awaitOptimizationIdle() and needsSettling() won't wait for it before an exit quote. Before this change, every connect (start, setExiting, reconnectForExit) ran after the collector existed. I found this by reading the code and did not reproduce it on a device. One fix is to move the isCollectingEvents.compareAndSet launch into a helper and call it from connectForRestore as well.
  • Can't-connect test would pass even if no connect was attempted (app/src/test/java/to/bitkit/repositories/SparkRepoTest.kt:2677-2687). a restore spark can't connect for keeps its exit data staged for spark's start checks only that the import never ran and the file stayed staged. If the new isSupported && !isConnected branch were removed or never reached, both assertions would still hold, so the test doesn't show that the restore tried to connect and recovered from the failure. Adding verify(sparkService).connect(0, false) would close that gap. The lock that the description says stops a restore and Spark's start from importing at once also has no test.

nits

  • Changelog covers the out-of-scope case it doesn't fix (changelog.d/next/1976.fixed.md:1). The fragment reads 'when Spark can't import it'. A restore that can't connect Spark also can't import, and it still reports the exit data as restored, as the PR's Out of Scope says. 'when Spark rejects it' would match what changed.

the reviewers disagree, your call

  • Cold restore drops Spark events before startup (app/src/main/java/to/bitkit/repositories/SparkRepo.kt:1366-1375). Objection: Real, but non-blocking rather than blocking: This is a real regression. connectForRestore (new in this PR) attaches the SDK listener during the SPARK_EXIT restore step, but sparkService.events has replay 0 and no collector until SparkRepo.start() subscribes, and WalletViewModel.start waits for the whole restore (waitForRestoreIfNeeded), so any AutoOptimization.Started in that gap is dropped and isOptimizing/needsSettling() stay false. The impact is narrow: an exit has to be quoted during the first optimization round right after a cold restore, and a later RoundCompleted/Cancelled event corrects the state. The fix is cheap: start collecting events before connectForRestore connects.
  • Spark can suppress a confirmed failure for the current send (app/src/main/java/to/bitkit/repositories/SparkRepo.kt:596-602). Objection: Real, but non-blocking rather than blocking: Confirmed. When earlierAttemptIds returns null (before the first full sync, since canListPayments() needs hasSynced, or when the list read fails), the taken attempt stores only sentAt. The id from the PENDING payment is never kept, so the send's own failure dated within RAIL_CLOCK_SKEW_SECONDS matches isEarlierFailureOfUncertainSend. That makes onPayment return early, lightningSend report PENDING, and retries on both rails stay blocked until dropExpiredTakenAttempts runs at invoice expiry. No funds are lost and it clears itself at expiry, but it is a reachable correctness bug. Fix: record the returned payment id and never treat a failure carrying that id as an earlier attempt's.

Spark's listener drops events no one collects, and the wallet's start collected them only after the restore, so events after the restore's connect were lost. The restore now starts the collection first, which subscribes before it returns. The can't-connect test also checks the connect, and the changelog says rejects.

Verified: the new SparkRepoTest events case fails without the early collection and passes with it; just compile, just test and just lint pass.
@ovitrif

ovitrif commented Nov 18, 2026

Copy link
Copy Markdown
Collaborator Author

@coreyphillips all four from your review:

Spark connected by the restore drops SDK events until start() runs

Addressed in a90f5e7. The event collection moved into one helper that start() and the restore's connect both call. It now subscribes before it returns (CoroutineStart.UNDISPATCHED), so no event of a later connect is lost. A new test emits a payment event after a restore's connect and checks it is handled; it fails without the change.

Can't-connect test would pass even if no connect was attempted

Addressed in a90f5e7: the test now verifies connect(0, false). The lock has no test, as the race needs two imports to overlap, which a single test thread can't force.

Changelog covers the out-of-scope case it doesn't fix

Addressed in a90f5e7: it now says "when Spark rejects it".

Spark can suppress a confirmed failure for the current send

Addressed in b34fa0d on #1957, where that code lives. A taken attempt now keeps the id of the payment Spark returned or listed as pending, and a failure with that id is never read as an earlier attempt's. A new test fails without it.

@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.

Rechecked after 1 new commit (a90f5e7). 2 blocking findings the reviewers disagree about.

addressed

  • Changelog covers the out-of-scope case it doesn't fix (changelog.d/next/1976.fixed.md:1). Fixed: changelog.d/next/1976.fixed.md:1 now reads "when Spark rejects it", which matches the change and leaves out the can't-connect case.

still open

  • Spark connected by the restore drops SDK events until start() runs (app/src/main/java/to/bitkit/repositories/SparkRepo.kt:1372-1376). connectForRestore is now the only path that connects Spark before start() has launched the event collector, so every SDK event in between is lost. SparkService.connect (SparkService.kt:174-199) attaches the listener that emits into _events, a MutableSharedFlow with no replay. Its only subscriber is launched by start() (SparkRepo.kt:282-284), and the wallet start runs that only after the restore finishes the WALLET, BLOCKTANK and ACTIVITY categories. With no subscriber, emit drops the value. The restored wallet's first sync emits payment, deposit-claim, AutoOptimization and UnilateralExitStateChanged events in that window. Most of the loss is recovered: start() syncs, refreshes and syncs activities, and the restore's own bump of _exitStateGeneration still triggers the exit backup. Optimization state is not recovered. A leaf optimization that started in the gap leaves isOptimizing false and sparkOptimizationActivityAt unstamped, so awaitOptimizationIdle() and needsSettling() won't wait for it before an exit quote. Before this change, every connect (start, setExiting, reconnectForExit) ran after the collector existed. I found this by reading the code and did not reproduce it on a device. One fix is to move the isCollectingEvents.compareAndSet launch into a helper and call it from connectForRestore as well. Codex still sees it: The sequential restore receives connect-time events. With overlapping calls, I reproduced start() setting the flag at SparkRepo.kt:1373 before subscribing, then the restore connecting with no subscriber.
  • Can't-connect test would pass even if no connect was attempted (app/src/test/java/to/bitkit/repositories/SparkRepoTest.kt:2677-2687). a restore spark can't connect for keeps its exit data staged for spark's start checks only that the import never ran and the file stayed staged. If the new isSupported && !isConnected branch were removed or never reached, both assertions would still hold, so the test doesn't show that the restore tried to connect and recovered from the failure. Adding verify(sparkService).connect(0, false) would close that gap. The lock that the description says stops a restore and Spark's start from importing at once also has no test. The connect half is fixed: SparkRepoTest.kt:2685 now verifies connect(0, false). The lock is still untested. The reply says one test thread can't force two imports to overlap, but that doesn't hold. A suspending importExitState stub that awaits a CompletableDeferred can hold start()'s import inside exitImportMutex while a launched restoreExitBackup waits to write. Completing the deferred then shows whether the restore's data survives and gets imported.
  • Cold restore drops Spark events before startup (app/src/main/java/to/bitkit/repositories/SparkRepo.kt:1366-1375). A cold restore can drop Spark’s leaf optimization start event, so an emergency exit can quote while a leaf swap is still active. connectForRestore() connects before start() subscribes to the SDK event flow, which has no replay. I ran the base and branch connection paths with a service that emits AutoOptimization.Started during connection. The base recorded the event. On HEAD, needsSettling() stayed false. Codex still sees it: I reproduced a connect-time optimization start reaching the collector on the sequential path. When startup paused between setting the flag and subscribing at SparkRepo.kt:1373-1374, the restore dropped that event and left the optimizer flag false.
  • Spark can suppress a confirmed failure for the current send (app/src/main/java/to/bitkit/repositories/SparkRepo.kt:596-602). A failed Spark send can stay pending until its invoice expires even when Spark reports the failure for that exact payment. If the pre-send history read fails, payInvoice() keeps the start time and discards the payment ID from the PENDING response. A failure with that same ID can then be treated as an older attempt. I ran the event and lookup code from both revisions with a matching failure timestamped one second after submission. On HEAD, the failure event was suppressed, Activity received no update, and the settled lookup returned null. Codex still sees it: The fix cited in b34fa0dda is absent from this checkout. SparkRepo.kt:599-603 still discards the pending payment ID, so the timestamp check at line 1986 can suppress a failure for that payment.

new since the last review

worth doing, does not block

  • Restore handles Spark events before held sends are restored (app/src/main/java/to/bitkit/repositories/SparkRepo.kt:1377-1379)
  • Early Spark failure events can clear a restored retry (app/src/main/java/to/bitkit/repositories/SparkRepo.kt:1377-1380)

A restore's connect now takes back the sends held before it and waits until Spark's events are subscribed, so a start on another thread can't leave a connect without a collector and an earlier attempt's failure can't end a restored retry. Adds a test for the import lock.

Verified: the new lock and held-retry SparkRepoTest cases fail without their fixes and pass with them; just compile, just test and just lint pass.
@ovitrif

ovitrif commented Nov 18, 2026

Copy link
Copy Markdown
Collaborator Author

@coreyphillips on your recheck:

When startup paused between setting the flag and subscribing at SparkRepo.kt:1373-1374, the restore dropped that event and left the optimizer flag false.

Addressed in ae27cfd. collectEvents() now suspends until the collector has subscribed (onSubscription), and both start() and the restore's connect wait for it. A connect from either caller therefore always has a subscriber, also when the other caller set the flag first.

The lock is still untested.

Addressed in ae27cfd: a new test holds the start's import of older staged data inside the lock while a restore stages its own, and checks that the restore's data is kept and imported. Without the lock around the write it fails.

The fix cited in b34fa0dda is absent from this checkout.

This branch now merges #1957's branch, so b34fa0dda is here, with 76a7355ed, which keeps the id across restarts.

@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.

Rechecked after 5 new commits (2979510, b34fa0d, 76a7355, and 2 more). Nothing blocking remains.

addressed

  • Spark connected by the restore drops SDK events until start() runs (app/src/main/java/to/bitkit/repositories/SparkRepo.kt:1372-1376). Fixed: connectForRestore (SparkRepo.kt:1398-1405) now calls collectEvents() before sparkService.connect. collectEvents (1389-1396) suspends on eventsSubscribed until onSubscription fires, and start() waits on the same signal (285), so the listener attached in SparkService.connect always has a subscriber. The test at SparkRepoTest.kt:2748 covers it, and CI unit tests pass at ae27cfd.
  • Can't-connect test would pass even if no connect was attempted (app/src/test/java/to/bitkit/repositories/SparkRepoTest.kt:2677-2687). Fixed: SparkRepoTest.kt:2685 verifies connect(0, false), and the new test at 2690-2712 holds the start's import of older data inside exitImportMutex while a restore stages its own. I traced that test without the lock on the write: the start's file.delete() removes the new data and verify(importExitState("new")) fails. CI unit tests pass at ae27cfd.
  • Cold restore drops Spark events before startup (app/src/main/java/to/bitkit/repositories/SparkRepo.kt:1366-1375). Fixed: The subscription is now awaited before the restore's connect (SparkRepo.kt:1402-1403), and _events buffers with a suspending emit (SparkService.kt:146, 191). An AutoOptimization.Started emitted during that connect therefore reaches onOptimization (SparkRepo.kt:451-456) and sets isOptimizing. I confirmed this by reading the code. No test emits during the connect itself.
  • Spark can suppress a confirmed failure for the current send (app/src/main/java/to/bitkit/repositories/SparkRepo.kt:596-602). Fixed: payInvoice keeps the PENDING payment's id as ownId (SparkRepo.kt:601) and persists it through keepSparkSendAttempt (648). restoreUncertainSends reads it back after a restart (794-801). isEarlierFailureOfUncertainSend returns false for that id before the earlier-id and clock-skew rules (2005), so onPayment and settledSendEvent no longer hide the send's own failure. The tests at SparkRepoTest.kt:3823-3862 cover the live and restart cases and pass in CI.
  • Restore handles Spark events before held sends are restored (app/src/main/java/to/bitkit/repositories/SparkRepo.kt:1377-1379). Fixed: connectForRestore calls restoreUncertainSends() at SparkRepo.kt:1401, before collectEvents(). METADATA is restored before SPARK_EXIT (BackupRepo.kt:1155, 1190), so isOutrunByRetry sees the restored attempts. The test at SparkRepoTest.kt:2714-2746 sends an earlier attempt's failure after the restore's connect, then checks the hold and its saved start survive. It passes in CI.
  • Early Spark failure events can clear a restored retry (app/src/main/java/to/bitkit/repositories/SparkRepo.kt:1377-1380). Fixed: Same change as point 6: SparkRepo.kt:1401 loads the saved attempts before the collector starts. onPayment therefore treats the earlier id as outrun and returns before removeSparkUncertainSend. SparkRepoTest.kt:2714-2746 checks that no PaymentFailed is emitted and the start is kept.

new since the last review

worth doing, does not block

  • A late pending write can recreate a settled Spark send hold (app/src/main/java/to/bitkit/data/CacheStore.kt:209-217). A late write of a pending payment ID can recreate a send hold after Spark has confirmed the failure. Let onPayment() clear the journal and expiry before the pending path reaches keepSparkSendAttempt() at SparkRepo.kt:648 or :721. I ran the cache and restore methods in isolation and confirmed that the write recreates the attempt on restart with no expiry. The stale entry then makes private Paykit treat the failed invoice as already attempted.

@ovitrif

ovitrif commented Nov 19, 2026

Copy link
Copy Markdown
Collaborator Author

Merging this PR into its base, fix/spark-follow-ups, with a merge commit: it is approved and green at ae27cfd, with every thread resolved. That keeps the approval: bringing it up to date with master would have needed a push here. #1957 now carries this change and merges it into master once approved.

@ovitrif
ovitrif merged commit 590373d into fix/spark-follow-ups Nov 19, 2026
23 checks passed
@ovitrif
ovitrif deleted the fix/restore-rejected-exit-data branch November 19, 2026 12:49
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.

4 participants