AP2 icon indicating copy to clipboard operation
AP2 copied to clipboard

Inconsistent amount units: `payment.budget.max` read as dollars, `payment.amount_range.max` as cents

Open NagaYu opened this issue 1 month ago • 2 comments

Version: e1ea56db72a6385bce3e5c1112b3a56ce60acb43 (v0.2).

The inconsistency

Every amount in a closed Payment Mandate is an integer in minor units:

  • types/amount.json amount: "Amount in minor units, according to the ISO-4217 spec."

The two amount constraints disagree with this, and with each other, across schema, docs and SDK:

Schema (open_payment_mandate.json) Doc example (payment_mandate.md) SDK (constraints.py)
payment.amount_range.max integer, "Maximum allowed amount in minor (cents) unit of currency." "max": 100.50 compared directly with the minor-unit amount
payment.budget.max number, "Maximum amount for the budget." "max": 1000.00 budget_max_cents = int(self.constraint.max * 100)

So the SDK treats amount_range.max as cents and budget.max as dollars. The documentation writes both as dollars.

Effect

The same number means two different limits depending on the constraint:

  • Budget. {"type": "payment.budget", "max": 250, "currency": "USD"} with a 19900 (USD 199.00) payment: the SDK accepts it (25000 ≥ 19900). An implementation following the Amount type (max = 250 cents) rejects it.
  • Amount range. {"type": "payment.amount_range", "max": 250.0, ...} written in the style of the documentation example: the SDK rejects the same 19900 payment ("Amount 19900 exceeds maximum 250"). A reader of the documentation example expects a USD 250.00 limit, i.e. accept.

A user who approves "budget 250" on a Trusted Surface could get a USD 2.50 or a USD 250.00 limit, depending on the verifier.

Reproduction

These are KYA Bench conformance vectors, run against the SDK at the commit above (MandateClient.verify + PaymentMandateChain.verify):

  • ap2-budget-unit-ambiguous: SDK accepts; KYA Bench reports indeterminate (outcome depends on the unit).
  • ap2-amount-range-decimal-max: SDK rejects; KYA Bench reports indeterminate (decimal value against an integer schema).
  • ap2-budget-cumulative-exceeded: SDK and KYA Bench both reject; the outcome is the same under either reading.

Suggested resolution

State that every amount-like constraint field is an integer in ISO 4217 minor units. Make budget.max an integer in the schema, drop the * 100 in BudgetEvaluator, and update both documentation examples (e.g. "max": 10050, "max": 100000).

Also noted (lower priority)

checkout.allowed_merchants with no revealed entries is "invalid" in AP2 (checkout_mandate.md: "if the allowed contains no revealed elements, the constraint is invalid"). The equivalent VI constraint skips the check instead. This doesn't affect AP2 on its own, but anyone translating AP2 intents into VI-style constraints will get a different verdict for the same probe (KYA Bench vector xlate-undisclosed-allowlist).

NagaYu avatar Oct 31 '26 15:10 NagaYu

Two measurements on the SDK side at e1ea56d that bear on this, whichever unit is chosen (per CONTRIBUTING the core specification now lives at FIDO; the SDK and samples are here):

  1. The documentation example "max": 100.50 for payment.amount_range (docs/ap2/payment_mandate.md:186) does not load: AmountRange.model_validate raises a ValidationError on the int field. 250.0 loads and becomes 250, which is why ap2-amount-range-decimal-max reads as 250 minor units. The doc example and the model already disagree, before any unit decision.
  2. Budget.max is a float: Budget(max=10, currency="USD").model_dump_json() writes "max":10.0. If the schema moves to integer, whole-number budgets written this way still validate (JSON Schema Validation 2020-12 §6.1.1: any number with a zero fractional part), but Core §6.3 says integers SHOULD NOT carry a fractional part, and a reader that refuses that encoding rejects them (ap2-evidence-pack, which I maintain, does today); a fractional budget such as 0.29 no longer validates; and if the unit becomes minor units, a 10.0 written today as USD 10 reads as 10 cents. The type change, the model field (max: int) and a migration note belong together.

Related and not linked here yet: #339 (23 Aug) raised the undocumented unit of budget.max, and #368 / #377 fix the SDK's conversion in the other direction (major units with the ISO 4217 exponent). How an offline verifier grades the 250 / 19900 case while the unit is open is in x402-foundation/x402#2452, not repeated here.

robertolocatelli81-dev avatar Nov 02 '26 18:11 robertolocatelli81-dev

Thanks — both measurements are at the same pinned commit I used (e1ea56d), and they sharpen two points in the issue:

  1. The doc example doesn't just disagree with the model — it doesn't load. I hadn't separated the two cases: "max": 100.50 raises a ValidationError on the int field, while 250.0 silently coerces to 250. The KYA Bench vector ap2-amount-range-decimal-max uses the 250.0 form, which is why the SDK run reads it as 250 minor units and rejects the 19900 payment. I'll add the unloadable-example case to the spec-gap entry (AP2-04): a documentation example that the SDK's own model refuses is a stronger form of the same gap.

  2. Agreed that the type change, the model field, and a migration note belong together. The resolution suggested above (integer in the schema, drop the * 100, fix the examples) covers writers going forward, but not mandates already written with a float Budget.max: under minor units a 10.0 written today as USD 10 would read as 10 cents, and fractional budgets such as 0.29 would stop validating. The migration note should say both, and state the reader rule explicitly — integers SHOULD NOT carry a fractional part (Core §6.3), so a reader may reject 10.0 rather than coerce it, as ap2-evidence-pack already does.

On grading while the unit is open: KYA Bench reports the 250 / 19900 case as indeterminate, consistent with the grading described in x402-foundation/x402#2452 — no change needed there.

Linking the related threads for the record: #339 raised the undocumented unit of budget.max, and #368 / #377 fix the SDK's conversion in the major-unit direction.

NagaYu avatar Nov 02 '26 22:11 NagaYu