Skip to content

fix: spark exit alert and pending overlapping send - #1903

Merged
ovitrif merged 3 commits into
masterfrom
fix/spark-exit-alert-and-pending-send
Nov 12, 2026
Merged

ovitrif merged 3 commits into
masterfrom
fix/spark-exit-alert-and-pending-send

Conversation

@ovitrif

@ovitrif ovitrif commented Nov 12, 2026

Copy link
Copy Markdown
Collaborator

Description

Fixes the two Spark points deferred from #1898:

  • Funding notification after a Redo (fixes bug: spark exit funding notification stays after a redo turns running #1899). When a Redo exit moves from NeedsFunding to Running on the funding it already holds, the posted funding notification is now withdrawn, whichever tick the move happens on. A top-up that arrives still leaves the notification up, as on the normal path.
  • Refused overlapping send stays pending (fixes bug: a refused overlapping spark send stays pending #1900). A Spark send refused because a competing send of its invoice was recorded now checks that send's outcome first. A completed send returns its hash, so the app shows it as paid. A failed one returns the refusal, so the error screen shows the existing payment-failed copy. Only a send still in flight stays pending.

It also adds the #1898 hold tests @coreyphillips asked for: a prepare whose funding the held inputs pay, the failed-only branch, and a later short build asking again.

No changelog fragment, since this changes Spark behaviour that hasn't shipped in a release yet.

QA Notes

  • New unit tests, each failing without its fix:
    • SparkExitAlertsTest: a Redo ask held through is withdrawn; a top-up and a non-Redo ask are not
    • SparkExitDriverTest: the held-funding hold uses the held input, drops the ask and asks again on a later short build; the failed-only branch moves to Running
    • LightningRepoTest: a refused overlapping send returns the competing send's completed or failed outcome; still pending otherwise
  • Ran just compile, just test and just lint; all pass.
  • No UI change, so no media.

A Redo exit that goes from NeedsFunding to Running on its held funding withdraws the funding notification. A Spark send refused for a competing send of its invoice returns that send's outcome instead of staying pending.
@ovitrif ovitrif self-assigned this Nov 12, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 12, 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-12T18:29:05.420391Z 27c2506 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 12, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] This PR appears safe to merge; no actionable issues were found.

Summary

Fixes two Spark cases:

  • Overlapping Spark sends now return the outcome already recorded.
  • Redo exits withdraw fee alerts when held funding gets them running.

Adds tests for completed, failed, and pending sends, plus held-funding builds, failed-only builds, and a later build that needs more funding.

Reviews (1) · Last reviewed commit: "fix: spark exit alert and pending overla..." · 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: 27c2506c30

ℹ️ 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 Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Regtest APK

Built from 3871825 (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

  • Same-tick Redo on held funding still leaves a non-unverified fee ask posted (app/src/main/java/to/bitkit/repositories/SparkExitWorker.kt:400-402)
  • Refused send can report a stale FAILED Spark row while another send is live (app/src/main/java/to/bitkit/repositories/LightningRepo.kt:1637-1644)

the reviewers disagree, your call

  • A live overlapping retry can be reported as failed (app/src/main/java/to/bitkit/repositories/LightningRepo.kt:1637-1642)

@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. Withdraws the funding notification when a Redo exit moves from NeedsFunding to Running on the funding it already holds, and settles a Spark send refused for an overlapping send from that send's listed outcome instead of leaving it pending. Reviewed 27c2506, full tier, static: per this repo's rule for auto reviews the branch was not built here, so this rests on the code and CI.

What I checked, and 4 candidates I ruled out

Read: the full diff, plus LightningRepo's Spark branch of payInvoice (authorizeSend, admitSend, the onFailure rail bookkeeping), SparkRepo.payInvoice / lightningSend / listedLightningSend / hasRecordedSendSince / uncertainSendHashes, SparkExitRepo's alert block and SparkExitDriver's Redo hold paths.
CI: build-and-test, build-local, compose-ui, detekt, lint and Greptile green; the e2e-tests-local shards were still running when I posted.

Ruled out

  • A refused send reported as paid while the competing send is still in flight: lightningSend returns Found(PENDING) for any hash in the uncertain set, and the competing send writes addSparkUncertainSend before sparkService.send, so an unsettled overlap maps to null and keeps the original PaymentPendingException.
  • An earlier attempt's FAILED row taken as the competing send's outcome: a FAILED row is reported only when the hash is not uncertain; the competing send stays uncertain until its call returns, so FAILED here is that send's own result. The failure is a SendRefused, which the service-leg path and QuickPayRepo already treat as nothing sent, true in that case.
  • Notification withdrawn while new funding is still needed: the new term requires record.funding == previous.funding and the move to Running, so a top-up (which appends to funding in SparkExitDriver) keeps the ask, as the new SparkExitAlertsTest cases pin.
  • Non-Redo ask withdrawn: gated on previous.isRedoRebuild; the unverified-ask branch is unchanged.

Merge confidence: 4/5, no findings and each change has a test, but the e2e shards were still running and the branch was reviewed without a local build.

Reviewer model: claude-opus-5-5
Reasoning effort: 15 (runtime setting of this run)

… is live

A Redo keeps a flag for its funding ask, so the notification is withdrawn on the Redo's own tick too. A refused overlapping send reads a failed Spark row as its outcome only when no send of the invoice is in flight or admitted on either rail and no node send was recorded since its checks.
@ovitrif

ovitrif commented Nov 12, 2026

Copy link
Copy Markdown
Collaborator Author

Fixed all three points from the last review in 8bf1a0d: the Redo funding ask is now withdrawn on the Redo tick too, and a refused overlapping send trusts a failed Spark row only when no other send of the invoice is live. just compile, just test and just lint pass, and each fix has a test that fails without it.

Re-review is needed only because of this push: https://github.com/synonymdev/bitkit-android/compare/27c2506c3..8bf1a0de3

@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 (8bf1a0d). 1 blocking finding the reviewers disagree about.

addressed

  • Same-tick Redo on held funding still leaves a non-unverified fee ask posted (app/src/main/java/to/bitkit/repositories/SparkExitWorker.kt:400-402). Fixed: driveWhileFunding's Redo branch now sets isRedoAsk (SparkExitDriver.kt:556), and isFundingAskWithdrawn reads it on either record (SparkExitWorker.kt:401-405). adopt and the holdingRedo copy both keep the flag, so the Redo's own tick sees record.isRedoAsk. Every ask that reaches that branch is covered, fee-bump asks and asks that came back included. I traced this by reading the code. The new test a redo on a fee ask built from the held funding that tick withdraws the ask starts from a plain NeedsFunding record. I could not run it because the private packages don't resolve here.

still open

  • Refused send can report a stale FAILED Spark row while another send is live (app/src/main/java/to/bitkit/repositories/LightningRepo.kt:1637-1644). settledSparkSendOutcome takes sparkRepo.lightningSend(hash)'s FAILED as the outcome of the invoice. lightningSend does not look at sendsInFlight or at node sends. earlierLightningSend does: it treats a send in flight or recorded since as pending even over a failed row (SparkRepo.kt:1399-1436). Two reachable cases: 1. Spark send B failed and was recorded after this call's mark, and a retry C over the node is now admitted. This send is refused because LDK holds the key in admitSend. isOverlapped is true because of B, so it returns SendRefused("Spark's earlier send of it failed") while C can still pay. 2. A Spark retry B is in flight over an older FAILED row. Between sendRecordedAt being set (SparkRepo.kt:546) and the addSparkUncertainSend write (550), lightningSend returns FAILED, not PENDING. The window is two DataStore writes. Callers read SendRefused as "nothing left the wallet": - failLightningPayment(hash, error) drops the Paykit proofs for the hash. - QuickPay classifies the send as PRE_DISPATCH_REJECTION. - The send screen shows payment failed. Before this change the refusal stayed pending, and the live send's event resolved it correctly. I found this by reading the code and did not reproduce it. Both cases need overlapping attempts, so it is unlikely in practice. A COMPLETED row is safe to return as is. A suggested guard for FAILED: trust it only when no send of the key is admitted on either rail (admittedSends[key] == null) and no node send was recorded since the checks (ldkSendRecordedAt[key] <= ldkRecordsBefore). Codex still sees it: LightningRepo.kt:1653-1655 checks current Spark activity only after reading the row, missing a retry that completes during that read. I paused a FAILED lookup, let the competing retry succeed and release admission, then resumed the lookup: the refused caller still received SendRefused.
  • A live overlapping retry can be reported as failed (app/src/main/java/to/bitkit/repositories/LightningRepo.kt:1637-1642). A stale failed attempt can produce a final failure while a competing retry is still live. SparkRepo.payInvoice records the retry before its suspending journal writes, but lightningSend ignores sendsInFlight and can still return the older FAILED row. This branch converts that row into SendRefused, so the caller shows failure even though the retry can pay. I confirmed this in a temporary coroutine reproduction using the unchanged methods: pause the competing retry at addSparkSendHash, resume the overlapping call after its checks, then release the retry. The overlapping call returned SendRefused and the retry succeeded. The same interleaving before this commit returned PaymentPendingException. Codex still sees it: The original journal-write pause now returns PaymentPendingException at LightningRepo.kt:1647. The reproduced variant where the retry completes during the lookup still reports failure for a paid invoice, leaving a narrower, non-blocking race.

new since the last review

worth doing, does not block

  • A Redo rebuild's own short-build fee ask is no longer withdrawn (app/src/main/java/to/bitkit/repositories/SparkExitWorker.kt:401-405)
  • A competing Spark send that ends during the row read can still read as failed (app/src/main/java/to/bitkit/repositories/LightningRepo.kt:1643-1655)
  • Later Redo funding asks no longer dismiss their notifications (app/src/main/java/to/bitkit/repositories/SparkExitWorker.kt:401-404)

@ovitrif

ovitrif commented Nov 12, 2026

Copy link
Copy Markdown
Collaborator Author

Pushed 3871825 for the re-review's three threads:

  • A fee ask raised inside a Redo hold, by the Redo's own short build or a later one, is withdrawn once a build on the held funding runs.
  • A refused Spark send ignores a FAILED row when a competing send was live before or after the row read, or was recorded during it.

just compile, just test and just lint pass; each new test fails without its fix.

Re-review is needed only because of this push: 8bf1a0d...3871825

@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 (3871825). Nothing blocking remains.

addressed

  • Refused send can report a stale FAILED Spark row while another send is live (app/src/main/java/to/bitkit/repositories/LightningRepo.kt:1637-1644). Fixed: Case 1: isSendLive (LightningRepo.kt:1658-1660) checks admittedSends[key] and ldkSendRecordedAt[key] > ldkRecordsBefore, and now runs both before and after the read (1644, 1647). An admitted or recorded node retry therefore keeps the FAILED row pending. Case 2: sendsInFlight.putIfAbsent (SparkRepo.kt:542) runs before sendRecordedAt is set (546), so a retry is already in flight during the DataStore window. I traced both cases; I could not run the tests because the private packages don't resolve offline.
  • A live overlapping retry can be reported as failed (app/src/main/java/to/bitkit/repositories/LightningRepo.kt:1637-1642). Fixed: A retry paused at addSparkSendHash (SparkRepo.kt:547) is already in sendsInFlight (542), so isSendLive returns true. settledSparkSendOutcome then returns null at LightningRepo.kt:1652, and the caller gets PaymentPendingException again. The in-flight test in LightningRepoTest covers this (isSendInFlight stubbed true over a FAILED row). Traced, not run.
  • A Redo rebuild's own short-build fee ask is no longer withdrawn (app/src/main/java/to/bitkit/repositories/SparkExitWorker.kt:401-405). Fixed: askForFunding now sets isRedoAsk from record.isRedoRebuild (SparkExitDriver.kt:763-766), and isFundingAskWithdrawn has the previous.isRedoRebuild term again (SparkExitWorker.kt:402). I traced the check path: rebuild(isRedoRebuild = true) (630), then a short build. tick keeps the flag while NeedsFunding (139). The later build at 208 then runs Running on the same funding, so the predicate is true. The 1931 assertion is back.
  • Later Redo funding asks no longer dismiss their notifications (app/src/main/java/to/bitkit/repositories/SparkExitWorker.kt:401-404). Fixed: holdingRedo returns a hold only while isRedoRebuild is true (SparkExitDriver.kt:387). A later short build in the hold therefore reaches askForFunding with that flag set, and the ask is marked as a Redo ask (765). The predicate also accepts previous.isRedoRebuild. The new test at SparkExitDriverTest.kt:1984-2011 takes the hold through a short build and then a build on the held funding, and asserts the withdrawal. Traced, not run.

still open

  • A competing Spark send that ends during the row read can still read as failed (app/src/main/java/to/bitkit/repositories/LightningRepo.kt:1643-1655). settledSparkSendOutcome checks isSendLive only after lightningSend returns, so a competing send that finishes during that read is not seen as live. I did not confirm this; it depends on whether the Breez SDK lists a send's row while its send call is still running. The interleaving, for a refused call R and a Spark send B that is in sparkService.send: 1. R's listPayments still shows an older FAILED row because B's row isn't listed yet. 2. B returns, which clears sendsInFlight (SparkRepo.kt:564) and the uncertain marker (584). 3. B's LightningRepo.payInvoice calls releaseSend. 4. R then reads uncertainSendHashes and isSendLive (LightningRepo.kt:1653), and both now say nothing is live. R returns SendRefused while B paid the invoice. The window is narrower than the earlier points: B has to finish its send, a DataStore write and its release between R's list read and R's DataStore read. The author's reply covers a retry that starts during the read, not one that ends during it. Elsewhere this file reads liveness before the listing as well as after (inFlightBefore in earlierNodeSend and keepLdkSendHashes). Doing the same here would close the gap: take isSendLive and a sendRecordMark() before lightningSend, and treat a Spark send recorded since that mark as live. Codex still sees it: LightningRepo.kt:1644-1645 still samples liveness before taking the record mark. With an earlier overlap already recorded, an extracted-method reproduction started another retry between those reads and ended it during the row read: the current ordering returned SendRefused, while taking the mark first kept it pending.

new since the last review

worth doing, does not block

  • Liveness is read before the Spark record mark, leaving a cross-thread gap (app/src/main/java/to/bitkit/repositories/LightningRepo.kt:1644-1645)

@ovitrif

ovitrif commented Nov 12, 2026

Copy link
Copy Markdown
Collaborator Author

Merging: approved on this head, required checks green, all threads replied and resolved; the remaining non-blocking point is tracked in #1908.

@ovitrif
ovitrif merged commit 52bc273 into master Nov 12, 2026
23 checks passed
@ovitrif
ovitrif deleted the fix/spark-exit-alert-and-pending-send branch November 12, 2026 20:22
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: a refused overlapping spark send stays pending bug: spark exit funding notification stays after a redo turns running

3 participants