Repository navigation
fix(sdk): convert payment.budget max to minor units per ISO 4217 - #377
Open
baaakgun4543 wants to merge 1 commit into
Open
baaakgun4543 wants to merge 1 commit into
baaakgun4543 wants to merge 1 commit into
Conversation
BudgetEvaluator converted the major-unit budget with int(max * 100). For zero-decimal currencies this raised the cap 100x (a 1000 JPY budget admitted 100000 JPY), and binary-float truncation rejected payments exactly at the cap (19.99 USD -> 1998 cents). Convert with exact decimal arithmetic and the ISO 4217 minor-unit exponent, flooring fractions so rounding can never raise the cap.
3 of 4 tasks
Author
|
Note on the failing #379 adds those terms to the dictionary. Once it is merged, re-running spellcheck here should pass. |
Author
|
After opening this PR I found that #368 already addresses the same budget conversion bug. I found it independently. The differences:
If maintainers prefer #368, I'm happy to close this one and help port those tests over. |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
payment.budgetis the cumulative cap for recurring agent payments.BudgetEvaluatorconverts the budget'smaxto minor units withint(self.constraint.max * 100). The spec example uses a major-unit value ("max": 1000.00), andpayment_amount.amountis in integer minor units per ISO 4217. That conversion fails in two ways.1. The cap is too loose for zero-decimal currencies (fail-open). JPY, KRW and others have no minor unit, so
* 100raises the cap 100x. ABudget(max=1000, currency='JPY')admits a payment of 100,000 JPY.2. Payments exactly at the cap are rejected (fail-closed).
int(19.99 * 100) == 1998, so a 19.99 USD budget rejects a 1999-cent payment. Likewiseint(0.29 * 100) == 28.Change
minor_unit_exponent(currency)returns the ISO 4217 exponent. The default is 2. Exceptions:major_to_minor_units(value, currency)usesDecimal(str(value))and the exponent. It floors fractions of a minor unit so rounding can never raise the cap.BudgetEvaluatoruses the helper. The violation message is unchanged apart from reporting the correct limit.Whole-number USD budgets behave exactly as before (e.g.
Budget(max=15.0, currency='USD')is 1500 cents), and all existing tests pass.Tests
New tests in
constraints_tests.py:test_payment_budget_decimal_boundary_usdtest_payment_budget_iso4217_minor_units, parametrized over JPY (at and over the cap), KRW (at the cap), KWD (at and over the cap) and EUR 0.29.4 of the 7 new cases fail on
main. The other 3 pass before and after, as regression guards.Full suite in a clean environment with the pinned dependencies (Python 3.12):
mainThe 2 failures are
kb_sd_jwt_intermediate_tests.py::test_verify_rejects_aud_mismatchand::test_verify_rejects_nonce_mismatch. They fail identically onmainand are unrelated to this change. The change adds noruff checkfindings beyond those already onmain.Notes for reviewers
The exponent table is inlined to avoid a new dependency. Happy to switch to a maintained source if preferred.
payment.amount_rangealready takes integer minor units (per its schema) and is unaffected.The spec could state explicitly that
payment.budget.maxis in major units, as its example implies.Follow the
CONTRIBUTINGGuide.Make your Pull Request title in the https://www.conventionalcommits.org/ specification.
Ensure the tests and linter pass (see the note above on the 2 failures already present on
main).Appropriate docs were updated (if necessary): no doc change. A spec clarification is suggested above.