Skip to content

fix: offer the savings retry after a refused exit start - #2056

Merged
ovitrif merged 15 commits into
masterfrom
fix/spark-exit-refused-start-retry
Nov 26, 2026
Merged

ovitrif merged 15 commits into
masterfrom
fix/spark-exit-refused-start-retry

Conversation

@ovitrif

@ovitrif ovitrif commented Nov 26, 2026 •

Copy link
Copy Markdown
Collaborator

Closes #1856
Twin: synonymdev/bitkit-ios#1449

Description

  • Fixes Progress offering no savings retry after an emergency exit start whose savings payment was refused before it went out, because its fee rose above the one confirmed. The refusal (amount and refused fee) is now saved with the exit, so the offer survives a relaunch.
  • Progress prices that payment again at the fast rate, never below the refused fee, and shows what leaves savings under the emergency fees ("Deducted from your Savings balance"). The existing red note and Pay from savings button pay it at exactly that fee. A fee that rose again is refused, saved and shown for another confirm, and the older price is never sent.
  • The payment re-reads the funding address under the tick lock and pays nothing once funding arrived there, the exit holds funding, or a savings send to the address is unresolved or refused by a node (that one keeps its own retry). A payment that may have gone out forgets the refusal.
  • A savings payment the node accepted is never paid again, also when a stop or a failed save lost its txid: each tick, cancel and the retry keep it as the exit's payment while the refusal is on record, so it survives a later send in the send journal. Pay from savings pays the price on screen when tapped, never one published after. Progress never offers it over funding seen before, also when a later address read fails.
  • Progress prices again every 15 s after a failed price (for example before the node runs) while it is open, and on pull to refresh.

Out of Scope

Design

N/A — no design available. Reuses the existing Progress note and button of the savings retry and the funding screen's savings row; no new strings.

Preview

N/A — the fee mismatch hasn't been reproduced on a device.

QA Notes

Journeys

N/A

Manual Tests

  • start an emergency exit paying from savings while the savings fee rises between confirm and dispatch → Progress shows the red note, the new savings total and Pay from savings, which pays it — no journey or device run can force a fee change between confirm and dispatch, so none covers this

Automated Checks

  • updated SparkExitRepoTest.kt
  • updated SparkExitFundingTest.kt
  • updated SparkExitViewModelTest.kt
  • ran ./gradlew testDevDebugUnitTest --tests 'to.bitkit.repositories.SparkExit*' --tests 'to.bitkit.ui.settings.lightning.SparkExit*'
  • ran ./gradlew detekt

Models used

  • Planning/scoping: Claude Opus 5.5 (reasoning: medium)
  • Implementation: Claude Opus 5.5 (reasoning: medium)
  • Review: Not performed

An exit start whose savings payment was refused before it went out, for a
fee above the one confirmed, saves that payment with the exit. Progress
prices it again at no less than the refused fee, shows what leaves savings
and pays it at that fee from Pay from savings.

Verified: ./gradlew testDevDebugUnitTest --tests 'to.bitkit.repositories.SparkExit*' --tests 'to.bitkit.ui.settings.lightning.SparkExit*'
The refused payment is paid only while the exit holds no funding and no
savings send to its address went out, checked again under the tick lock.
Progress drops a price below the refused fee, re-prices on refresh, and
a send that went out and failed shows with its own retry.

Verified: ./gradlew testDevDebugUnitTest --tests 'to.bitkit.repositories.SparkExit*' --tests 'to.bitkit.ui.settings.lightning.SparkExit*' :app:detekt
A refused start payment is offered only while no savings send to the
address is unresolved or refused, read from the send attempt itself, and
is forgotten once a savings payment went out. A fee refused again is never
paid at the older price.

Verified: ./gradlew testDevDebugUnitTest --tests 'to.bitkit.repositories.SparkExit*' --tests 'to.bitkit.ui.settings.lightning.SparkExit*' :app:detekt
The refused start payment is paid only over a fresh read of the funding
address, on a chain Spark reads. Progress prices it again until a price
succeeds, as on a launch before the node runs.

Verified: ./gradlew testDevDebugUnitTest --tests 'to.bitkit.repositories.SparkExit*' --tests 'to.bitkit.ui.settings.lightning.SparkExit*' :app:detekt
Progress stops offering savings once the payment's read of the funding
address found funding, keeps its last price when a re-price fails, and
stops pricing once it closes.

Verified: ./gradlew testDevDebugUnitTest --tests 'to.bitkit.repositories.SparkExit*' --tests 'to.bitkit.ui.settings.lightning.SparkExit*' :app:detekt
The pay-time read now sets funding seen through the same path as a tick,
and Progress prices the refused payment only while it is open.

Verified: ./gradlew :app:testDevDebugUnitTest --tests '*SparkExit*' detekt
Progress screens are counted, so one closing after another opened never
stops the open one's pricing. A test covers pricing only while open.

Verified: ./gradlew :app:testDevDebugUnitTest --tests '*SparkExit*' detekt
The pricing test keeps a failing price retrying, so it fails if one screen
closing stops another's pricing or the last one leaves it running.

Verified: ./gradlew :app:testDevDebugUnitTest --tests '*SparkExit*' detekt; the test fails with the screen count guard removed
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 26, 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-26T03:42:22.587335Z ea06210 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 Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Confidence Score: 5/5 · The PR appears safe to merge; no actionable issues were found.RetriggerView in GreptileConfidence Score: 5/5

[High impact] The PR appears safe to merge; no actionable issues were found.

Summary

Adds a saved retry for emergency-exit funding refused before dispatch because its savings fee increased.

  • Progress can retry a refused savings payment at its updated fee.

No actionable issues found. Review was based on source inspection; tests and device checks were not run.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  A[Funding fee exceeds approval] --> B[Save amount and refused fee]
  B --> C[Price retry at fast rate]
  C --> D[Show savings total]
  D --> E[User taps Pay from savings]
  E --> F{Funding still missing and no unresolved send?}
  F -->|No| G[Send nothing]
  F -->|Yes| H{Prepared fee within approval?}
  H -->|No| B
  H -->|Yes| I[Send and clear refusal]
Loading

Reviews (1) · Last reviewed commit: "test: check a closing progress screen st..." · Reviewed by Greptile

@github-actions

github-actions Bot commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Regtest APK

Built from 90b8e37 (run).

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

@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. Saves a start's savings payment refused for a fee above the confirmed one on the exit record, prices it again on Progress at the fast rate (never below the refused fee) and pays it under the tick lock after re-reading the funding address. Reviewed ea06210, full tier, static: on the Bitkit track an auto review does not build the PR's branch, so this is reasoned from the code and from CI.

What I checked, and 5 candidates I ruled out

Read in full: the diff of SparkExitRepo.kt, SparkExitFunding.kt, SparkExitRecord.kt (record field and JSON round-trip), SparkExitViewModel.kt, SparkExitProgressScreen.kt, SparkExitComponents.kt; payFunding, sendFunding, fundingTxid, retrySavingsPayment, cancel, canCancel, SparkExitMath.fundingState at the head
Call sites traced: LightningRepo.sendOnChain down to checkApprovedSpend (where OnchainSendChangedError(miningFee) is wrapped in OnchainSendNotDispatchedError), and the three places that clear savingsRefusal on a paid txid
CI at review time: build-and-test and build-local green; lint, compose-ui / ui-tests and the local e2e shards still running

Ruled out

  • A refusal that went out is offered again: a node rejection surfaces as SparkExitError from fundingTxid and an unknown dispatch as OnchainSendPendingError; both clear the refusal in payFunding's onFailure, and savingsRefusal() also returns null while the send attempt to the address is unresolved or rejected.
  • Paying after funding arrived: the isDue check runs under tickMutex, reads the address, feeds noteFundingSeen, and SparkExitMath.savingsRefusal refuses on any receivedSats > 0 or a non-Ask state; a failed read throws before sendFunding.
  • Paying an older, lower price: isDue requires refusal.feeSats <= retry.feeSats, and savingsRetryOffer hides a price below the current refusal until it is priced again.
  • Two payments at once: paymentMutex.tryLock() guards the repo path, and the view model returns early while isRetryingSavingsPayment.
  • Pricing that keeps running after Progress closes: the screen counter in start/stopSavingsRetryPricing cancels the job on the last dispose, and loadSavingsRetry is a no-op with no screen open.

Merge confidence: 4/5, no findings and the payment paths are covered by SparkExitRepoTest.kt, but lint and the UI tests had not finished when this was posted and the branch was not built here.

Reviewer model: Claude Opus 5.5 (claude-opus-5-5)
Reasoning effort: Not exposed (the run metadata names the model but no reasoning level)

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

Reviewed ea06210.

The exit's funding state can lag behind the savings send journal. When the funding address still appears empty, Progress can offer another payment even though a savings transaction was accepted or may still confirm.

Medium candidate: SparkExitRepo.kt:1042 ignores a matching accepted send. The savings retry marks ordinary follow-up complete before saving the exit's funding txid (SparkExitRepo.kt:952, SparkExitRepo.kt:1135). If the app stops between those writes or the exit save fails, Progress can restore Pay from savings while the address still appears empty. That tap can start a fresh send with different inputs, potentially paying the funding amount twice and incurring another mining fee.

Medium candidate: Progress shows the collected quote (SparkExitProgressScreen.kt:230), but the button reads the latest ViewModel quote at tap time (SparkExitViewModel.kt:356). If a refresh publishes a higher quote just before a tap on the old amount, the new cap can allow a fee above the one still visible. Pass the displayed quote with the tap so the prepared-fee check enforces the amount the user saw.

Low candidate: A savings retry that returns a pending error can clear the retry offer without saving the transaction ID (SparkExitRepo.kt:947). While the address read shows no funds, Progress still shows Amount to pay and a copyable address (SparkExitProgressScreen.kt:222). Someone returning to Progress can follow that prompt from another wallet while the savings transaction is still able to confirm.

Review details

Quick review
Model: Claude Opus 5.5
Reasoning effort: high

General review
Model: GPT-6.2 Sol
Reasoning effort: high

Combined review
Model: GPT-6.2 Sol
Reasoning effort: high

A savings send the node accepted holds back the refused payment's offer
and pay, also when the exit's txid was not saved; a failed save keeps the
payment as paid in memory. The tap pays the price on screen, never one
published after.

Verified: ./gradlew :app:testDevDebugUnitTest --tests '*SparkExit*' detekt; the accepted-send test fails without the gate
A tick saves the txid of a savings send the node accepted to the funding
address when the record lost it, before a later send can take its place in
the journal, and cancel holds back while such a send exists.

Verified: ./gradlew :app:testDevDebugUnitTest --tests '*SparkExit*' detekt

@jvsena42 jvsena42 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Code scan at ea06210: no findings.

Checked and clean:

  • Refused vs ambiguous starts: a refusal is written only for OnchainSendNotDispatchedError(cause = OnchainSendChangedError). checkApprovedSpend throws that before retainPreparedReceipt and before broadcast, and the attempt is released, so a refusal always means nothing went out. Pending and rejected outcomes clear the refusal; unknown and accepted outcomes save the txid and clear it.
  • Retry gate: isDue runs under tickMutex and re-reads the address and the live OnchainSendAttempt. It requires the same sats and a fee no lower than the refusal's. The approved cap is the shown retry.feeSats. sendOnChain still refuses while another attempt is unresolved. The remaining double-pay window is pwltr's first thread, not repeated here.
  • Persisted state: savingsRefusal is optional (objectOrNull) and CURRENT_VERSION is unchanged. Spark exit isn't in v3.1.1 (git grep -i sparkexit on v3.1.1^{commit} is empty), so there is no upgrade path to cover.
  • Cancellation and concurrency: check, send and save run NonCancellable under tickMutex, and cancel() takes the same lock. paymentMutex.tryLock is shared with start, fee bump and node retry. Every savingsRefusal writer holds tickMutex. The VM double-tap guard is set synchronously.
  • Cancel/Back: Cancel deletes the record with its refusal. Back/OK keep it and reprice on re-entry. A relaunch reloads from disk.
  • Pricing once on Progress (no reprice on resume) matches the start flow and fee bump on master, so I'm not raising it here.

perf: relevant (new 15 s pricing loop while Progress is open and pricing fails); measurement pending with the gate.

Device gate (partial): ea06210 — not run yet. It needs a refused exit start (prepared fee above the approved one) on a Spark regtest wallet. Device result to follow.

Reviewer model: Opus 5.5 (claude-opus-5-5) coordinating and verifying; Fable 5.2 (claude-fable-5-2) primary review
Reasoning effort: Not exposed

…cord

A payment the exit forgot since (dropped, or short) stays forgotten, as the
journal keeps an accepted send until the next one. Cancel keeps a lost
payment first instead of refusing on the journal alone.

Verified: ./gradlew :app:testDevDebugUnitTest --tests '*SparkExit*' detekt
The savings retry and cancel keep a refused start's accepted payment the
record lost under their lock before they check it, and keeping it shows the
funding as paid.

Verified: ./gradlew :app:testDevDebugUnitTest --tests '*SparkExit*' detekt
A retry that finds the lost accepted payment keeps it and succeeds instead
of failing, and the exit reads both savings sends from one journal read.

Verified: ./gradlew :app:testDevDebugUnitTest --tests '*SparkExit*' detekt
@ovitrif

ovitrif commented Nov 26, 2026

Copy link
Copy Markdown
Collaborator Author

@pwltr

Medium candidate: SparkExitRepo.kt:1042 ignores a matching accepted send. [...] Progress can restore Pay from savings while the address still appears empty.

Fixed in 7b6c93b, 5aaccbe, 52aa7d5 and 2222ede. A savings send the node accepted to the funding address now counts as a savings send, so the refused payment is neither offered nor paid again. Each tick, cancel and the retry itself keep its txid on the record (under the tick lock) while the refusal is still on record. That way the payment survives a later send taking its slot in the send journal, and a payment the exit later forgets (dropped, or short) is never brought back. A failed save keeps the payment as paid in memory.

Medium candidate: Progress shows the collected quote (SparkExitProgressScreen.kt:230), but the button reads the latest ViewModel quote at tap time (SparkExitViewModel.kt:356).

Fixed in 7b6c93b. The button passes the offer it rendered, and the payment is capped at that fee; a fee refused since is refused before it goes out.

Low candidate: A savings retry that returns a pending error can clear the retry offer without saving the transaction ID (SparkExitRepo.kt:947). While the address read shows no funds, Progress still shows Amount to pay and a copyable address

This predates this PR: a start whose savings payment ends pending shows the same ask on master. Filed as #2059 (iOS: synonymdev/bitkit-ios#1451) and listed under Out of Scope.

synonymdev/bitkit-ios#1449 got the same two fixes, so the twins stay equal.

@ovitrif
ovitrif requested review from jvsena42 and pwltr November 26, 2026 09:56
Funding seen at the address stays seen when a later read fails, and the
refused savings payment is no longer offered over it.

Verified: ./gradlew :app:testDevDebugUnitTest --tests '*SparkExit*' detekt
The funding-seen guard moves into the shared refusal check, where every
caller and its tests see it.

Verified: ./gradlew :app:testDevDebugUnitTest --tests '*SparkExit*' detekt

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

Reviewed 90b8e37.

Low candidate: A savings retry that returns Pending can lose its link to the exit. SparkExitRepo.kt:948 clears the saved refusal before recording a funding txid, and SparkExitRepo.kt:1074 requires that refusal to keep a matching accepted send. If the wallet records the transaction before the funding explorer sees it, Cancel can become available. An empty read at SparkExitRepo.kt:1229 then lets Cancel delete the active transfer, leaving its payment on the address watched for late funding. To verify this, extend the pending-retry test so the same journal entry becomes Observed while the address still returns no UTXOs, then check whether Cancel preserves the transfer.

Review details

Quick review
Model: Claude Opus 5.5
Reasoning effort: high

General review
Model: GPT-6.2 Sol
Reasoning effort: high

Combined review
Model: GPT-6.2 Sol
Reasoning effort: high

@ovitrif

ovitrif commented Nov 26, 2026

Copy link
Copy Markdown
Collaborator Author

@pwltr

Low candidate: A savings retry that returns Pending can lose its link to the exit.

Out of scope: tracked in #2067, because no funds are lost (the payment stays watched as late funding and is swept back to savings) and a push now would void this approval. Merging this PR now: approved, CI green.

@ovitrif
ovitrif merged commit 576ac14 into master Nov 26, 2026
23 checks passed
@ovitrif
ovitrif deleted the fix/spark-exit-refused-start-retry branch November 26, 2026 11:11
@jvsena42

Copy link
Copy Markdown
Member

Device gate (partial): ea06210 — refusal reproduced on the Android emulator against the local bitkit-docker Spark stack (one-line worktree patch forcing sparkSharesNodeChain).

Trigger: Emergency fees prices the funding with ldk-node's input choice (1 input, 422 sats); the start uses Coin Selection = Consolidate over six UTXOs (1 442 sats), so checkApprovedSpend refuses before broadcast.

  • Master 4a2adf9: "Fees changed" toast; Progress shows only Amount to pay, the address and Cancel transfer.
  • PR, 3 of 4 starts: the red note (SparkExitSavingsRefused), "Deducted from your Savings balance ₿ 12 237" (10 795 + 1 442) and SparkExitRetrySavings. The offer survives a force-stop. When the fee rose again (a seventh UTXO), the tap was refused again and the total went to 12 441 with nothing sent. Pay funded the exit once: one UTXO at the address, 7 inputs, fee 1 646.
  • perf: retry tap → funded 8.5 s (host load ~20), 3.2 s when refused again. While pricing fails, about 3.9 pricing calls/min (10 calls, 15.2–16.3 s apart, 10:43:38 → 10:45:57); none after leaving Progress (47 s); master makes none. A successful price runs once per open, refusal change, refresh or failed tap.
10:41:22.003 DEBUG [LightningRepo.kt:2618] Consolidating by spending all 6 UTXOs
10:41:22.116 ERROR [SparkExitRepo.kt:773] Failed to start the Spark exit [OnchainSendNotDispatchedError='On-chain send did not reach the backend']
10:32:04.901 INFO  [SparkExitRepo.kt:1137] Funded the Spark exit with '10795' sats in '25ac0da4…543d'

Still running: the full runbook drive (relaunch-before-price check, cancel/repeat, measurements on a second wallet).

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.

bug: no savings retry after a refused exit start

4 participants