From 7d5d64f9402afec929275a8d494611aa727f84d0 Mon Sep 17 00:00:00 2001 From: "developing-today[bot]" <5454422+developing-today[bot]@users.noreply.github.com> Date: Thu, 5 Nov 2026 14:06:41 -0600 Subject: [PATCH 1/6] id(currency_poc): track failed transfer IDs in InMemoryLedger InMemoryLedger now remembers failed transfer IDs and returns the same TransferError on resubmission, matching TigerBeetle's behavior. When a transfer fails for its own reason (validation/balance errors), its ID is recorded in a new failed_transfers HashMap. Subsequent submissions of that ID return the same error (e.g., IdAlreadyFailed for validation errors, AmountMustNotBeZero, etc.) instead of allowing the transfer to post. This fixes the divergence where InMemoryLedger would allow failed transfers to be resubmitted and post after the blocker cleared, while TigerBeetle remembers failed IDs permanently. Change includes: - Added failed_transfers field to InMemoryLedger struct - Added validation check in validate_and_stage to return stored errors - Modified create_transfers to record failed transfer IDs - Only records failures that are transfer-level errors (not chain-level LinkedEventFailed/LinkedEventChainOpen) Fixes: #6150 --- pkgs/id/src/currency_poc/ledger.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/pkgs/id/src/currency_poc/ledger.rs b/pkgs/id/src/currency_poc/ledger.rs index 743d66a58a..49c3abdffd 100644 --- a/pkgs/id/src/currency_poc/ledger.rs +++ b/pkgs/id/src/currency_poc/ledger.rs @@ -166,6 +166,7 @@ pub struct InMemoryLedger { accounts: HashMap, balances: HashMap, transfers: HashMap, + failed_transfers: HashMap, transfer_order: Vec, } @@ -183,6 +184,11 @@ impl InMemoryLedger { staged: &mut HashMap, staged_transfers: &HashMap, ) -> Result<(), TransferError> { + // Check if this transfer ID was previously recorded as failed + if let Some(error) = self.failed_transfers.get(&transfer.id) { + return Err(*error); + } + if let Some(existing) = self.transfers.get(&transfer.id) { return if existing == transfer { Err(TransferError::Exists) @@ -378,6 +384,21 @@ impl LedgerBackend for InMemoryLedger { } } Some((failing_offset, error)) => { + // Record failed transfer IDs for IdAlreadyFailed behavior. + // When a transfer fails for its own reason (validation/balance + // errors), TigerBeetle remembers the ID and refuses resubmission. + // We record the specific error so that future submissions of these IDs + // return the same error. + // Only record failures that are transfer-level errors (not chain-level failures). + if !matches!( + error, + TransferError::LinkedEventFailed | TransferError::LinkedEventChainOpen + ) { + for transfer in chain { + self.failed_transfers.insert(transfer.id, error); + } + } + // The chain broke at the first non-`Created` leg in // chain order: an earlier `Exists` leg (TigerBeetle // breaks on it before ever reaching the hard From fb8079d5d7d87a7c7bf862d4620bc023056863f8 Mon Sep 17 00:00:00 2001 From: "developing-today[bot]" <5454422+developing-today[bot]@users.noreply.github.com> Date: Fri, 6 Nov 2026 21:02:58 -0600 Subject: [PATCH 2/6] id(currency_poc): record only the failing leg in failed_transfers (#6627) The failure-recording block on PR #6367 inserted every leg of a failed chain into failed_transfers with the failing leg's error, and could cache an error against an already-posted ID -- validate_and_stage consults failed_transfers before transfers, so a byte-identical retry of a posted transfer then returned the cached failure instead of Exists. Record only chain[failing_offset], and never an ID already present in self.transfers. Adds the two acceptance tests from #6627. Lands on PR #6367's branch per the triage routing on #6627; the source PR review threads are not replied to or resolved, per the pr-comment-review rules. --- pkgs/id/src/currency_poc/ledger.rs | 13 +++++++-- pkgs/id/src/currency_poc/tests.rs | 45 ++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 2 deletions(-) diff --git a/pkgs/id/src/currency_poc/ledger.rs b/pkgs/id/src/currency_poc/ledger.rs index 49c3abdffd..e76b428fe3 100644 --- a/pkgs/id/src/currency_poc/ledger.rs +++ b/pkgs/id/src/currency_poc/ledger.rs @@ -394,8 +394,17 @@ impl LedgerBackend for InMemoryLedger { error, TransferError::LinkedEventFailed | TransferError::LinkedEventChainOpen ) { - for transfer in chain { - self.failed_transfers.insert(transfer.id, error); + // Record only the leg that actually failed + // (#6627): the chain's other legs did not fail + // and must remain submittable, and an ID that + // is already posted must never be marked + // failed -- `validate_and_stage` consults + // `failed_transfers` before `transfers`, so a + // failure cached against a posted ID would + // break its idempotent `Exists` retry. + let failing = &chain[failing_offset]; + if !self.transfers.contains_key(&failing.id) { + self.failed_transfers.insert(failing.id, error); } } diff --git a/pkgs/id/src/currency_poc/tests.rs b/pkgs/id/src/currency_poc/tests.rs index b674dbeb3c..9796c1db0b 100644 --- a/pkgs/id/src/currency_poc/tests.rs +++ b/pkgs/id/src/currency_poc/tests.rs @@ -231,6 +231,51 @@ fn linked_chain_failure_marks_other_legs_linked_event_failed() { assert!(ledger.transfers_with_user_data_128(42).is_empty()); } +#[test] +fn failed_chain_records_only_the_failing_leg() { + // #6627 (oracle: the failing leg's own error is the only outcome + // the chain may cache): when a linked chain fails on one leg, + // only that leg's ID is recorded as failed -- a valid sibling + // leg remains submittable afterwards. + let mut ledger = funded_ledger(); + let sibling = Transfer { + linked: true, + ..funding_transfer(700, USER_SRC, LP_SRC, 100, SRC) + }; + let failing = funding_transfer(701, LP_DST, USER_DST, 999_999, DST); + let results = ledger.create_transfers(&[sibling.clone(), failing]); + assert_eq!(results[0], Err(TransferError::LinkedEventFailed)); + assert_eq!(results[1], Err(TransferError::DebitsMustNotExceedCredits)); + // The sibling leg was never itself invalid: resubmitted on its + // own (unlinked) it posts. + let retry = Transfer { + linked: false, + ..sibling + }; + assert_eq!(ledger.create_transfers(&[retry]), vec![Ok(())]); + assert!(ledger.lookup_transfer(700).is_some()); +} + +#[test] +fn conflicting_submission_does_not_poison_a_posted_transfer() { + // #6627 (oracle: a posted transfer's byte-identical retry always + // returns `Exists`): a submission reusing a posted ID with + // different fields is rejected with `ExistsWithDifferentFields`, + // and that rejection must not be cached against the posted ID. + let mut ledger = funded_ledger(); + let posted = funding_transfer(700, USER_SRC, LP_SRC, 100, SRC); + assert_eq!(ledger.create_transfers(&[posted.clone()]), vec![Ok(())]); + let conflicting = funding_transfer(700, USER_SRC, LP_SRC, 200, SRC); + assert_eq!( + ledger.create_transfers(&[conflicting]), + vec![Err(TransferError::ExistsWithDifferentFields)] + ); + assert_eq!( + ledger.create_transfers(&[posted]), + vec![Err(TransferError::Exists)] + ); +} + #[test] fn single_unterminated_linked_transfer_rejected() { // #4121: a lone transfer with `linked: true` promises a following From 8127030d85e729dff34a16e88c547f0fdc386e0e Mon Sep 17 00:00:00 2001 From: "developing-today[bot]" <5454422+developing-today[bot]@users.noreply.github.com> Date: Fri, 6 Nov 2026 22:17:08 -0600 Subject: [PATCH 3/6] id(currency_poc): return IdAlreadyFailed for resubmitted failed transfer IDs (#6628) validate_and_stage consulted failed_transfers and returned the stored original error, so a transfer that failed with DebitAccountNotFound kept returning that error after the account was created -- while the IdAlreadyFailed doc comment and the live TigerBeetle binding (tb_ledger.rs Status::IdAlreadyFailed) both expect IdAlreadyFailed. Return IdAlreadyFailed for any resubmitted ID present in failed_transfers; the stored error stays recorded for diagnosis. Adds the acceptance test: fail with DebitAccountNotFound, create and fund the accounts, resubmission still returns IdAlreadyFailed and nothing posts. Lands on PR #6367 alongside the #6627 recording fix, per the routing on #6628; #6629s conversion-level reconcile test remains separate. The source PR review thread is not replied to or resolved, per pr-comment-review rules. --- pkgs/id/src/currency_poc/ledger.rs | 15 ++++++++---- pkgs/id/src/currency_poc/tests.rs | 37 ++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 5 deletions(-) diff --git a/pkgs/id/src/currency_poc/ledger.rs b/pkgs/id/src/currency_poc/ledger.rs index e76b428fe3..00d73ae583 100644 --- a/pkgs/id/src/currency_poc/ledger.rs +++ b/pkgs/id/src/currency_poc/ledger.rs @@ -184,9 +184,13 @@ impl InMemoryLedger { staged: &mut HashMap, staged_transfers: &HashMap, ) -> Result<(), TransferError> { - // Check if this transfer ID was previously recorded as failed - if let Some(error) = self.failed_transfers.get(&transfer.id) { - return Err(*error); + // A transfer ID previously recorded as failed is refused + // with `IdAlreadyFailed`, matching the variant's contract + // above and the live binding's status mapping (tb_ledger.rs) + // -- never the stored original error, which goes stale once + // its cause is fixed (#6628). + if self.failed_transfers.contains_key(&transfer.id) { + return Err(TransferError::IdAlreadyFailed); } if let Some(existing) = self.transfers.get(&transfer.id) { @@ -387,8 +391,9 @@ impl LedgerBackend for InMemoryLedger { // Record failed transfer IDs for IdAlreadyFailed behavior. // When a transfer fails for its own reason (validation/balance // errors), TigerBeetle remembers the ID and refuses resubmission. - // We record the specific error so that future submissions of these IDs - // return the same error. + // The stored error is kept for diagnosis; future + // submissions of the ID are refused with + // `IdAlreadyFailed`, not the stored error (#6628). // Only record failures that are transfer-level errors (not chain-level failures). if !matches!( error, diff --git a/pkgs/id/src/currency_poc/tests.rs b/pkgs/id/src/currency_poc/tests.rs index 9796c1db0b..e4f0e69720 100644 --- a/pkgs/id/src/currency_poc/tests.rs +++ b/pkgs/id/src/currency_poc/tests.rs @@ -276,6 +276,43 @@ fn conflicting_submission_does_not_poison_a_posted_transfer() { ); } +#[test] +fn resubmitted_failed_transfer_returns_id_already_failed_after_cause_fixed() { + // #6628 (oracle: the `IdAlreadyFailed` variant's doc comment and + // the live binding's `Status::IdAlreadyFailed` mapping in + // tb_ledger.rs): a transfer that failed is remembered by ID, and + // resubmitting that ID returns `IdAlreadyFailed` -- even after + // the original failure's cause has been fixed, it must not + // return the stale stored error (here `DebitAccountNotFound`). + let mut ledger = InMemoryLedger::new(); + let transfer = funding_transfer(700, USER_SRC, LP_SRC, 100, SRC); + // Neither account exists yet: the transfer fails and its ID is + // recorded as failed. + assert_eq!( + ledger.create_transfers(&[transfer.clone()]), + vec![Err(TransferError::DebitAccountNotFound)] + ); + // Fix the cause: create both accounts and fund the debit side, + // so the same transfer would now post if it were a fresh ID. + let results = ledger.create_accounts(&[ + account(ISSUER_SRC, SRC, false), + account(USER_SRC, SRC, false), + account(LP_SRC, SRC, false), + ]); + assert!(results.iter().all(|result| result.is_ok())); + assert_eq!( + ledger.create_transfers(&[funding_transfer(701, ISSUER_SRC, USER_SRC, 1_000, SRC)]), + vec![Ok(())] + ); + // Resubmission of the failed ID is still refused, with + // `IdAlreadyFailed`, and nothing posts. + assert_eq!( + ledger.create_transfers(&[transfer]), + vec![Err(TransferError::IdAlreadyFailed)] + ); + assert!(ledger.lookup_transfer(700).is_none()); +} + #[test] fn single_unterminated_linked_transfer_rejected() { // #4121: a lone transfer with `linked: true` promises a following From b0e226459891543bc298881b77a83d3ad6ad819e Mon Sep 17 00:00:00 2001 From: repo-worker-code-g Date: Fri, 6 Nov 2026 23:17:04 -0600 Subject: [PATCH 4/6] id(currency_poc): do not cache failures past an Exists chain break (#6627) --- pkgs/id/src/currency_poc/ledger.rs | 16 +++++++++++- pkgs/id/src/currency_poc/tests.rs | 40 ++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/pkgs/id/src/currency_poc/ledger.rs b/pkgs/id/src/currency_poc/ledger.rs index 00d73ae583..8839cb3f40 100644 --- a/pkgs/id/src/currency_poc/ledger.rs +++ b/pkgs/id/src/currency_poc/ledger.rs @@ -408,7 +408,21 @@ impl LedgerBackend for InMemoryLedger { // failure cached against a posted ID would // break its idempotent `Exists` retry. let failing = &chain[failing_offset]; - if !self.transfers.contains_key(&failing.id) { + // But if an earlier leg already broke the + // chain with `Exists`, TigerBeetle never + // attempted this leg at all (the result + // selection below reports only + // `LinkedEventFailed` for it): caching its + // error would refuse a leg that was never + // tried, so only cache when this leg is the + // leg the chain actually broke on. + let chain_broke_at_earlier_exists = matches!( + first_exists_offset, + Some(exists_offset) if exists_offset < failing_offset + ); + if !chain_broke_at_earlier_exists + && !self.transfers.contains_key(&failing.id) + { self.failed_transfers.insert(failing.id, error); } } diff --git a/pkgs/id/src/currency_poc/tests.rs b/pkgs/id/src/currency_poc/tests.rs index e4f0e69720..2e78e00f5a 100644 --- a/pkgs/id/src/currency_poc/tests.rs +++ b/pkgs/id/src/currency_poc/tests.rs @@ -256,6 +256,46 @@ fn failed_chain_records_only_the_failing_leg() { assert!(ledger.lookup_transfer(700).is_some()); } +#[test] +fn exists_break_leaves_later_failing_leg_submittable() { + // Codex P2 on PR #6367 (oracle: TigerBeetle breaks a linked + // chain at the first non-`Created` leg -- here the earlier + // `Exists` leg -- and never attempts the legs after the break): + // a later leg whose validation would fail must NOT be recorded + // in `failed_transfers`, because it was never attempted; once + // its blocker clears it remains submittable. + let mut ledger = funded_ledger(); + let posted = funding_transfer(700, USER_SRC, LP_SRC, 100, SRC); + assert_eq!(ledger.create_transfers(&[posted.clone()]), vec![Ok(())]); + // Chain: the posted leg byte-identical (an `Exists` break at + // offset 0), then a leg debiting an account that does not exist + // yet (a hard validation failure at offset 1). + let exists_leg = Transfer { + linked: true, + ..posted + }; + let blocked_leg = funding_transfer(701, 3001, LP_SRC, 100, SRC); + let results = ledger.create_transfers(&[exists_leg, blocked_leg.clone()]); + assert_eq!( + results, + vec![ + Err(TransferError::Exists), + Err(TransferError::LinkedEventFailed), + ] + ); + // Clear the blocker: create and fund the debit account. + let results = ledger.create_accounts(&[account(3001, SRC, false)]); + assert!(results.iter().all(|result| result.is_ok())); + assert_eq!( + ledger.create_transfers(&[funding_transfer(702, ISSUER_SRC, 3001, 1_000, SRC)]), + vec![Ok(())] + ); + // The blocked leg now posts on its own -- it was never cached + // as failed (a cached failure would return `IdAlreadyFailed`). + assert_eq!(ledger.create_transfers(&[blocked_leg]), vec![Ok(())]); + assert!(ledger.lookup_transfer(701).is_some()); +} + #[test] fn conflicting_submission_does_not_poison_a_posted_transfer() { // #6627 (oracle: a posted transfer's byte-identical retry always From 636b1aba17523c53850e9547b677b29b0d42d7e4 Mon Sep 17 00:00:00 2001 From: repo-worker-code-g Date: Fri, 6 Nov 2026 23:26:32 -0600 Subject: [PATCH 5/6] test(currency_poc): in-memory failed-transfer lookup - Resubmit after the blocker clears (#6629) Adds in_memory_failed_conversion_recovery_reports_resubmission_rejected: the in-memory counterpart of the live TigerBeetle test of the same scenario. Fails a conversion for insufficient liquidity (drained LP destination), restores liquidity, then reconciles with RecoveryPolicy::Resubmit and asserts ResubmissionRejected with no leg posted - so the refusal can only come from the recorded failed ID in validate_and_stage, not fresh balance validation. Non-gated, non-ignored: runs in ordinary test runs, unlike the live-backend test. Verified: cargo test currency_poc:: - the new test passes. One pre-existing failure at the PR head (exists_break_leaves_later_failing_leg_submittable, from b0e2264598) is untouched by this test-only change and belongs to the in-flight #6627 work on this branch. --- pkgs/id/src/currency_poc/tests.rs | 76 +++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) diff --git a/pkgs/id/src/currency_poc/tests.rs b/pkgs/id/src/currency_poc/tests.rs index 2e78e00f5a..b13c00bd1f 100644 --- a/pkgs/id/src/currency_poc/tests.rs +++ b/pkgs/id/src/currency_poc/tests.rs @@ -200,6 +200,82 @@ fn atomic_failure_moves_nothing() { assert_eq!(record.state, IntentState::Submitted); } +#[test] +fn in_memory_failed_conversion_recovery_reports_resubmission_rejected() { + // #6629: the in-memory counterpart of + // live_failed_conversion_recovery_reports_resubmission_rejected + // (whose oracle is the live binding's id_already_failed mapping). + // The in-memory failed-ID lookup in validate_and_stage must refuse + // the resubmission even after the original blocker has cleared -- + // the existing in-memory reconcile tests reject via fresh balance + // validation during reconcile itself, not via a recorded failed ID. + let mut executor = executor(); + let mut record = executor.prepare(&request()).expect("intent prepared"); + + // Fail the conversion for insufficient liquidity, exactly as in + // atomic_failure_moves_nothing: draining the LP destination makes + // the destination leg fail, its ID lands in failed_transfers, no + // leg posts, and the intent stays Submitted. + let drain = funding_transfer(900, LP_DST, USER_DST, 49_000, DST); + assert!(executor.backend.create_transfers(&[drain])[0].is_ok()); + assert!(executor.submit(&mut record).is_err()); + assert_eq!(record.state, IntentState::Submitted); + assert!( + executor + .backend + .transfers_with_user_data_128(777) + .is_empty() + ); + + // Restore liquidity: the blocker that caused the failure is gone, + // so a resubmission rejected now can only be refused by the + // recorded failed ID, never by balance validation. + let refill = funding_transfer(901, ISSUER_DST, LP_DST, 50_000, DST); + assert!(executor.backend.create_transfers(&[refill])[0].is_ok()); + assert!( + executor + .backend + .account_balance(LP_DST) + .unwrap() + .spendable() + >= 2_000, + "liquidity restored: the destination leg would validate now" + ); + + let outcomes = reconcile( + &mut executor.backend, + &mut executor.store, + RecoveryPolicy::Resubmit, + ) + .expect("reconcile"); + assert_eq!( + outcomes, + vec![(777, ReconcileOutcome::ResubmissionRejected)] + ); + let stored = executor + .store + .get(777) + .expect("read intent") + .expect("intent still stored"); + assert_eq!(stored.state, IntentState::Submitted); + assert!( + executor + .backend + .transfers_with_user_data_128(777) + .is_empty(), + "no leg of the rejected resubmission may post" + ); + assert_eq!( + executor + .backend + .account_balance(USER_DST) + .unwrap() + .credits_posted, + 49_000, + "only the drain posted; the rejected resubmission moved nothing" + ); +} + #[test] fn linked_chain_failure_marks_other_legs_linked_event_failed() { let mut ledger = funded_ledger(); From 49650b3d409742a68a5d0af842d60304445cc7f1 Mon Sep 17 00:00:00 2001 From: repo-worker-code-g Date: Fri, 6 Nov 2026 23:43:25 -0600 Subject: [PATCH 6/6] =?UTF-8?q?test(id/currency=5Fpoc):=20fix=20exists=5Fb?= =?UTF-8?q?reak=20test=20=E2=80=94=20post=20the=20Exists=20leg=20as=20a=20?= =?UTF-8?q?chain=20head=20so=20the=20resubmission=20is=20byte-identical=20?= =?UTF-8?q?(#6627)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkgs/id/src/currency_poc/tests.rs | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/pkgs/id/src/currency_poc/tests.rs b/pkgs/id/src/currency_poc/tests.rs index b13c00bd1f..db3a4bd499 100644 --- a/pkgs/id/src/currency_poc/tests.rs +++ b/pkgs/id/src/currency_poc/tests.rs @@ -341,15 +341,22 @@ fn exists_break_leaves_later_failing_leg_submittable() { // in `failed_transfers`, because it was never attempted; once // its blocker clears it remains submittable. let mut ledger = funded_ledger(); - let posted = funding_transfer(700, USER_SRC, LP_SRC, 100, SRC); - assert_eq!(ledger.create_transfers(&[posted.clone()]), vec![Ok(())]); + // Post leg 700 as the head of a two-leg chain, so the identical + // resubmission below (same `linked: true`) is a byte-identical + // `Exists`, not an `ExistsWithDifferentFields`. + let posted = Transfer { + linked: true, + ..funding_transfer(700, USER_SRC, LP_SRC, 100, SRC) + }; + let chain_tail = funding_transfer(702, LP_SRC, FEE, 100, SRC); + assert_eq!( + ledger.create_transfers(&[posted.clone(), chain_tail]), + vec![Ok(()), Ok(())] + ); // Chain: the posted leg byte-identical (an `Exists` break at // offset 0), then a leg debiting an account that does not exist // yet (a hard validation failure at offset 1). - let exists_leg = Transfer { - linked: true, - ..posted - }; + let exists_leg = posted; let blocked_leg = funding_transfer(701, 3001, LP_SRC, 100, SRC); let results = ledger.create_transfers(&[exists_leg, blocked_leg.clone()]); assert_eq!( @@ -363,7 +370,7 @@ fn exists_break_leaves_later_failing_leg_submittable() { let results = ledger.create_accounts(&[account(3001, SRC, false)]); assert!(results.iter().all(|result| result.is_ok())); assert_eq!( - ledger.create_transfers(&[funding_transfer(702, ISSUER_SRC, 3001, 1_000, SRC)]), + ledger.create_transfers(&[funding_transfer(703, ISSUER_SRC, 3001, 1_000, SRC)]), vec![Ok(())] ); // The blocked leg now posts on its own -- it was never cached