Skip to content

bug: atomic compensation + consistent settlement rounding in RFQ execution saga (COG-GTM Execution Saga)#22

Open
devin-ai-integration[bot] wants to merge 1 commit into
masterfrom
devin/1780595472-fix-rfq-saga-compensation-rounding
Open

bug: atomic compensation + consistent settlement rounding in RFQ execution saga (COG-GTM Execution Saga)#22
devin-ai-integration[bot] wants to merge 1 commit into
masterfrom
devin/1780595472-fix-rfq-saga-compensation-rounding

Conversation

@devin-ai-integration

@devin-ai-integration devin-ai-integration Bot commented Jun 4, 2026

Copy link
Copy Markdown

Summary

Fixes a bug where a counterparty credit-check failure mid-saga left the bond's available notional understated, and a separate inconsistent-rounding bug in settlement math.

Root cause (compensation). RFQExecutionSaga.executeRfq deducts bond notional, then deducts counterparty credit. When deductCredit throws InsufficientCreditException, the saga relied solely on @Transactional rollback and never reversed the in-flight bond.deductNotional(...) mutation. When the saga's @Transactional joins an outer/shared persistence context (the production path), the managed Bond is left dirty with the deduction, so the bond's availableNotional stays reduced and subsequent quotes are wrongly rejected. The old code comment ("No need for saga compensation…") was incorrect.

Root cause (rounding). executionPrice was persisted and debited verbatim, with no currency-scale normalization → off-by-a-cent settlement values.

Fix:

settlementAmount = executionPrice.setScale(2, HALF_EVEN)   // consistent currency scale
bond.deductNotional(notional)
try {
    counterparty.deductCredit(settlementAmount)
} catch (InsufficientCreditException e) {
    bond.addNotional(notional)   // explicit compensation
    throw e
}
save Rfq(..., settlementAmount)

Change is scoped to RFQExecutionSaga; no unrelated refactors.

Reproduction & verification

Added a failing repro first (creditCheckFailure_rollsBackBondNotionalDeduction): before the fix it observed availableNotional understated by the trade notional (50000000.00 → 49000000.00) after the credit check failed. It now passes.

New JUnit 5 tests in RFQExecutionSagaTest:

  • (a) credit-check fails after notional deduction → bond notional restored, credit unchanged
  • (b) successful execution still debits bond notional and counterparty credit correctly
  • (c) HALF_EVEN rounding boundary cases (.005→.00, .015→.02, .025→.02)
  • notional-check failure leaves both aggregates unchanged

Test output

Tests run: 6, Failures: 0, Errors: 0, Skipped: 0 - RFQExecutionSagaTest
Tests run: 7, Failures: 0, Errors: 0, Skipped: 0 - IntegrationTest
Tests run: 13, Failures: 0, Errors: 0, Skipped: 0
BUILD SUCCESS

(mvn test, Java 11)

Link to Devin session: https://app.devin.ai/sessions/78af6ddcdc804c8088d3d9a1e22b213a
Requested by: @achalc


Devin Review

Status Commit
⚪ Not started

Run Devin Review

💡 Connect your GitHub account to enable automatic code reviews.

Open in Devin Review (Staging)
Open in Devin Review

Root cause: on credit-check failure the saga deducts bond notional but
never compensates the in-flight bond.deductNotional() mutation, leaving
the bond's available notional understated whenever the saga's
@transactional joins an outer/shared persistence context (the production
scenario). Settlement amounts were also persisted without a consistent
rounding policy, producing off-by-a-cent values.

Fix: explicitly compensate (bond.addNotional) when deductCredit throws
InsufficientCreditException, and normalize the settlement amount to
currency scale (2dp, HALF_EVEN) before deducting credit and persisting.

Adds JUnit 5 coverage: credit-fail rollback, notional-fail no-op,
successful debit, and HALF_EVEN rounding boundary cases.

Co-Authored-By: Achal Channarasappa <achal.channarasappa@cognition.ai>
@devin-ai-integration

Copy link
Copy Markdown
Author

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment, CI, and merge conflict monitoring

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no potential bugs to report.

View in Devin Review to see 2 additional findings.

Open in Devin Review

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.

1 participant