Repository navigation
fix(finops): round vendor balance to the table's NUMERIC contract (SCA-1961) - #25217
Conversation
…A-1961) The first real daily run after #25188 reached the Soniox balance puller and failed loading used_usd: the vendor's decimal has more than nine fractional digits and BigQuery rejected it as an invalid NUMERIC (job exit 1). parse_balance now renders balances through numeric_usd: half-up to nine decimals (the policy the derived daily spend already uses), plain decimal notation, short values unchanged, and a value outside the NUMERIC range is an invalid_response skip. The loader re-renders the artifact values too, so an artifact written by an older image cannot reach the table unnormalized. All money fields in the snapshot path are covered (used_usd, available_usd); the derived usd already quantized. Tests: parametrized renderings (more than nine digits, half rounding, exponent notation, range limit) and a loader regression that checks every money string sent to BigQuery against the NUMERIC text pattern. Failure-Class: none Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 76203c9a25
ℹ️ 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".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| def test_parse_balance_renders_numeric_safe_strings(vb, raw, expected): | ||
| parsed = vb.parse_balance({**BALANCE, "used_balance_usd": raw, "available_balance_usd": raw}) | ||
| assert parsed["used_usd"] == expected and parsed["available_usd"] == expected | ||
| import re |
There was a problem hiding this comment.
Move the new regular-expression imports to module scope
The new tests import re inside their function bodies, contrary to the explicit requirement in backend/AGENTS.md that all imports be at module top level. Move import re alongside the existing standard-library imports and remove both function-local imports.
AGENTS.md reference: backend/AGENTS.md:L168-L168
Useful? React with 👍 / 👎.
| amount = amount.quantize(NUMERIC_SCALE, rounding=ROUND_HALF_UP) | ||
| return format(amount, "f") |
There was a problem hiding this comment.
Check the NUMERIC range again after rounding
For a balance of 99999999999999999999999999999.9999999995, the initial range check passes, but half-up quantization produces 100000000000000000000000000000.000000000, which exceeds NUMERIC's 29-integer-digit limit. The puller consequently writes a ready artifact rather than an invalid_response skip; the loader then rejects it and fails the daily job. Validate the rounded amount against NUMERIC_LIMIT before returning it, and add a regression covering this carry across the upper boundary.
Useful? React with 👍 / 👎.
- numeric_usd range-checks after rounding, so a value just under 10**29 that rounds up to 10**29 is an invalid_response skip, not an invalid NUMERIC. - The derived daily spend renders through numeric_usd(pad=True): a zero delta is "0.000000000" (it was "0E-9"), and the share is divided and rounded at 60 digits so a 29-integer-digit delta cannot raise InvalidOperation under the default 28-digit context. Output shape for normal values is unchanged. - load_vendor_balance treats an out-of-range artifact as a bounded skip (stderr reason, nothing created or loaded), the same rule as the puller, instead of failing the daily job. Tests cover each case. Failure-Class: none Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
… exact (SCA-1961) Round-2 review finding: daily_deltas subtracted under the default 28-digit Decimal context, which rounds a one-day delta of 99999999999999999999999999999.999999999 to 1E29; numeric_usd then rejected it after the snapshot was already written. The difference now runs at 60 digits, like the share and the rounding. Test: a one-day delta at the NUMERIC maximum stays exact. Failure-Class: none Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
backend/AGENTS.md requires all imports at module top level; the new vendor-balance tests imported re inside three function bodies. No behavior change. Refs SCA-1961
Git-on-my-level
left a comment
There was a problem hiding this comment.
Independent review — exact head 2912ba078b
Reviewed the full diff against main (f3902ec54358) at this head. No blocking findings.
What I verified
numeric_usdholds the NUMERIC contract: half-up to 9 fractional digits, plain decimal (never exponent), and the range check runs after rounding, so...999.9999999995cannot carry into10**29. Short values keep their digits (12.5stays12.5), which BigQuery accepts.daily_deltasnow does subtraction, division and rounding at 60 digits, so a 29-integer-digit balance does not round at the default 28-digit context. The derivedusdis padded to 9 places, matching its historical shape.load_vendor_balancere-renders an artifact written by an older image and turns an out-of-range value into a bounded skip (invalid_response) instead of failing the daily job — the same rule the puller already uses.- I checked the other NUMERIC producers for the same defect: Stripe fees are
round(..., 6), Mercury and Modulate quantize to 9 places. The vendor balance was the only field outside the contract, so no further change is needed for "inspect all numeric money fields". - No credentials, raw vendor response or account value is added to any log or artifact path.
Tests
cd backend && python -m pytest tests/unit/test_finops_*.py -q → 282 passed at this head. The regression covers >9 fractional digits through parse_balance, daily_deltas, the loader row, and the exact newline-delimited JSON bq load validates.
Note on the last commit
2912ba078b is mine: it hoists the re import to module scope, closing the P1 finding from the earlier Codex review. Test-only, no behavior change.
Not verified here: a live BigQuery load. This issue does not authorize manual production job execution; the first real snapshot is the release owner's post-deploy check.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 2912ba078b
ℹ️ 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".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| if pad or amount.as_tuple().exponent < -9: | ||
| amount = amount.quantize(NUMERIC_SCALE, rounding=ROUND_HALF_UP) | ||
| if amount >= NUMERIC_LIMIT: | ||
| raise BalanceError("invalid_response", "balance value out of range") |
There was a problem hiding this comment.
Reject oversized amounts before quantizing them
If a vendor response or older artifact contains an amount with at least 52 integer digits and more than nine fractional digits, quantize() raises decimal.InvalidOperation because the rounded coefficient exceeds the 60-digit context precision. This happens before the range check, so the value never becomes a BalanceError; neither the puller nor the loader catches it, and the daily job fails instead of recording the intended invalid_response skip. Check the unrounded range before quantizing as well as afterward, or translate quantization failures into BalanceError.
Useful? React with 👍 / 👎.
What
The Soniox balance snapshot now renders its money fields to BigQuery's NUMERIC contract before the load, so the daily finops job's first real snapshot no longer fails.
Why
After #25188 gave
pull_vendor_balance.pya real entry point, the first production run (finops-unit-cost-job-8fqt6, 2026-11-13 15:39Z) reached the puller and failed loadingused_usd: the vendor's decimal has more than nine fractional digits and BigQuery rejected it withInvalid NUMERIC value. The job exited 1 after one retry;omi_finops.vendor_balance_snapshotwas created but holds 0 rows. Tracked as SCA-1961; the Soniox line on Omi TV needs two good snapshots.Change
numeric_usd()inpull_vendor_balance.py: half-up to nine decimals (the policy the derived daily spend already uses and the README documents), plain decimal notation (never exponent), short values keep their digits (12.5stays12.5), and a value outside NUMERIC's 29 integer digits becomes aninvalid_responseskip instead of a load error.parse_balanceuses it for bothused_usdandavailable_usd. The derivedusdcolumn already quantized to nine places.load_vendor_balancere-renders the artifact's values through the same function, so an artifact written by an older image cannot reach BigQuery unnormalized.No schema, schedule, secret, IAM or job-config change.
Tests
pytest backend/tests/unit/test_finops*.py: 279 passed, including new parametrized renderings (more than nine digits, half rounding, exponent notation, 29-digit limit, short values unchanged) and a loader regression that checks every money string sent to BigQuery against the NUMERIC text pattern, with 16-digit vendor-like decimals.Rollout
Needs the finops job image rebuilt through its existing workflow (release request SCA-1895 / SCA-1961). The release owner verifies the first scheduled snapshot; the measured Soniox line needs a second one the next day.
Failure-Class: none
🤖 Generated with Claude Code