Skip to content

fix: keep spark settlement going on cache errors - #2015

Merged
ovitrif merged 4 commits into
masterfrom
fix/spark-cache-best-effort
Nov 25, 2026
Merged

ovitrif merged 4 commits into
masterfrom
fix/spark-cache-best-effort

Conversation

@ovitrif

@ovitrif ovitrif commented Nov 24, 2026 •

Copy link
Copy Markdown
Collaborator

Closes #2016

Refs: #2014

Description

Follow-up to the review on #2014: #2014 (review)

  • Fixes a retry of an unexpired pre-restore invoice being refused when Spark's first sync lands during the send's checks, because readiness was read before the listing. payInvoice reads it once, after the listing, as before fix: close spark retry hold gaps #2014. A listing that couldn't be read is read again before the send, so a synced list that failed once no longer refuses outright, and a first sync landing after the listing doesn't either.
  • Fixes cache I/O interrupting Spark settlement, because the own-failure marker writes could throw: they are best-effort now, so a failed write never stops the failure report, the hold release or the SDK callback, and a failed cleanup during a sync no longer skips reconciling other holds or the withdrawal journal.
  • Fixes Spark attempt ids reaching the restore log, because the logged cache copy kept them: it now leaves out sparkUnsavedOwnFailures and sparkSendAttemptIds.
  • Fixes a node send recorded during a Spark send's checks losing its hash, because earlierNodeSend and the listing trim released it from a snapshot taken before the write. Both now check in-flight and newly recorded node sends inside the cache write, and earlierNodeSend again after it.

Out of Scope

Design

N/A — no UI changes.

Preview

N/A

QA Notes

Setup

N/A — no device testing required: each fix is a race or storage-failure path covered by unit tests.

Journeys

N/A — no user-visible behaviour change.

Manual Tests

N/A

Automated Checks

  • added SparkRepoTest.kt — a pre-restore retry is sent when Spark's first sync lands during its listing and the operators show it failed
  • added SparkRepoTest.kt — a send that returns failed still reports it when the cache can't keep its unsaved failure
  • added SparkRepoTest.kt — a sync whose own-failure cleanup fails still ends a paid hold
  • added LightningRepoTest.kt — a node send recorded while a Spark check releases the invoice keeps its hash
  • added LightningRepoTest.kt — a node send recorded while the listing trims its failed attempt keeps its hash
  • added CacheStoreTest.kt — a node send listed failed stays kept when recorded again before the trim writes

Evidence

  • just compile, just test and just lint pass; the last commit, which only adds a test, ran LightningRepoTest.kt and detekt.
  • Each new SparkRepoTest.kt and LightningRepoTest.kt test fails with its fix reverted.

Gaps

None.

Models used

  • Planning/scoping: Claude Opus 5.5 (reasoning: medium)
  • Implementation: Claude Opus 5.5 (reasoning: medium)
  • Review: gemini-4-neon-high (reasoning: Not exposed)
  • Review rounds: 2

The review runner records only the model id, whose high suffix names its tier; it exposes no separate reasoning setting.

Verified: SparkRepoTest, LightningRepoTest and BackupRepoTest pass; each new test fails without its fix.
Verified: just compile, just test and just lint pass.
Verified: LightningRepoTest and detekt pass; the new test fails without the trim guard.
@ovitrif ovitrif added this to the 3.2.0 milestone Nov 24, 2026
@ovitrif ovitrif self-assigned this Nov 24, 2026
@ovitrif
ovitrif requested review from jvsena42 and pwltr November 24, 2026 17:51
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 24, 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-24T17:52:02.065434Z b190c19 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.

Verified: none (changelog fragment only).
@ovitrif ovitrif removed this from the 3.2.0 milestone Nov 24, 2026
@ovitrif

ovitrif commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator Author

Linked #2016 as the 3.2.0 tracking issue for this PR and moved the milestone from this PR to that issue, since the release milestone tracks issues.

@ovitrif ovitrif modified the milestone: 3.2.0 Nov 24, 2026

@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. Follow-up to #2014: makes the Spark own-failure marker writes best-effort, reads Spark's listing readiness after the listing and re-reads an unreadable list before sending, keeps a node send recorded during a Spark check or the listing trim by re-checking inside the cache write, and drops attempt ids from the restore log. Reviewed eb022e3, full tier, static: per the Bitkit track rule this branch was not built here, so the review rests on the code and CI.

What I checked, and 5 candidates I ruled out

Read in full: SparkRepo.payInvoice, lightningSend, listedLightningSend, canListPayments, refuseUnverifiedRestoredInvoice, isUnsentPerOperators, recordActivity, the sync refresh around markUnsavedOwnFailure; LightningRepo.earlierNodeSend, listPaymentsKeepingSendHashes, keepLdkSendHashes, payInvoiceWithLdk's record/add order; CacheStore.trimLdkSendHashes, removeLdkSendHash, addLdkSendHash; runSuspendCatching (rethrows CancellationException).

Ruled out

  • canList read after the listing lets an unsynced list pass: when lightningSend returned Unavailable before the first sync and the sync lands before canList is read, the Unavailable branch now re-reads listPayments(), which is the synced list at that point, so a COMPLETED/PENDING row still skips or refuses. A sync landing after canList only makes refuseUnverifiedRestoredInvoice stricter.
  • Unavailable branch now sends after a synced list failed once: intended per the body; the re-read must succeed and its latestSend is checked before onBeforeSend, and a second failure still throws SendRefused.
  • removeLdkSendHash drops a hash recorded right after its transform: payInvoiceWithLdk sets ldkSendRecordedAt before addLdkSendHash, and DataStore serialises transforms, so either the transform sees the record and keeps the hash, or the add lands after the removal; the second isRetried() then returns Pending.
  • Swallowed marker write loses an own failure in-process: unsavedPayments still carries the id, so the next refresh retries the row; only the cross-restart detail is lost, as the KDoc states. A failed remove leaves a stale id whose replay hits the SUCCEEDED guard or re-saves the same failed row.
  • ldkSendRecordedAt kept for failed hashes: only entries newer than recordsBefore survive, i.e. sends recorded during the trim, which must keep it.

CI: build-and-test, build-local, lint and ui-tests still pending at review time.

Merge confidence: 4/5, no findings, but CI had not finished and the branch was not built here.

Reviewer model: claude-opus-5-5
Reasoning effort: Not exposed

@github-actions

Copy link
Copy Markdown
Contributor

Regtest APK

Built from eb022e3 (run).

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

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

Approving eb022e3. Code scan: no HIGH, MEDIUM or LOW. CI is green.

  • No ordering of a node send against the Spark check or the listing trim drops a hash while that node send is in flight or newly recorded.
  • The own-failure marker writes are now best-effort and use runSuspendCatching, and no hold or send decision reads the marker.
  • The second listing read before a send still refuses when it fails again.

Device gate: the PR's cases are unit-tested races and storage failures, so I ran ordinary Spark sends on the PR build as a regression check (local bitkit-docker spark stack, Gate_Throwaway).

Case Result
R1. Send that succeeds, ×3 ✅ one Sent Spark payment, one PaymentSuccessful, Sent sheet, no PaymentFailed
R2. Send that fails right away (receiver offline) ✅ one Failed sheet, one PaymentFailed, row Failed
R3. Hold invoice settled while open / failed while open ✅ / ✅, one event each, rows Sent / Failed
R4. Send right after a cold start, and from a lightning: URI after a cold start ✅ both paid. Spark's first sync lands about 2 s after launch, before Confirm can be reached, so the "before the first sync" branch can't be driven from the UI.

Device gate: R1–R4 ✅ at eb022e3. perf: swipe to SSP "payment initiated" median 1.34 s (3 samples), within the 1.3–2.7 s range measured on #2007 for base and PR; not performance-relevant otherwise.

Reviewer model: claude-fable-5-2 (primary review), claude-opus-5-5 (coordinator), claude-sonnet-5-5 (device driver)
Reasoning effort: Not exposed

@jvsena42

Copy link
Copy Markdown
Member

Two items that predate this PR, for a follow-up. Neither blocks it.

  1. BackupRepo.kt:1340-1346: the logged cache copy still includes sparkSendHashes, ldkSendHashes, the sparkUncertainSends keys, pubkySwapHoldHashes, pendingLightningMessages (LNURL comments by hash), bolt11, bolt11PaymentHash, onchainAddress, pendingSparkWithdrawal and sentSparkWithdrawals. The comment above it says payment history stays out of the log, and support requests attach that log. Nothing in AppCacheData is key material.
  2. SparkRepo.kt:656 (also 636/643 and settleUncertainSend): these cache writes on the settlement path can still throw. If removeSparkUncertainSend fails after Spark returned COMPLETED, payInvoice returns an IOException for a paid invoice and LightningRepo restores the previous rail. There is no double pay, because the uncertain entry stays, so a retry reads PENDING and the next sync settles it. Until then the user sees a failure for a paid send.

@ovitrif

ovitrif commented Nov 25, 2026

Copy link
Copy Markdown
Collaborator Author

Two items that predate this PR, for a follow-up. Neither blocks it.

@jvsena42 thanks, both are tracked for 3.2.0:

  1. Restore log fields: bug: restore log includes payment details #2024
  2. Settlement-path cache writes: bug: paid spark send shows as failed after a storage error #2025

@ovitrif

ovitrif commented Nov 25, 2026

Copy link
Copy Markdown
Collaborator Author

Merged: approved by @jvsena42 on eb022e3, checks green, the follow-up items tracked in #2024 and #2025.

@ovitrif
ovitrif merged commit 1e50dc8 into master Nov 25, 2026
23 checks passed
@ovitrif
ovitrif deleted the fix/spark-cache-best-effort branch November 25, 2026 09:01
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: spark settlement stops on cache errors

3 participants