Skip to content

fix(finops): round vendor balance to the table's NUMERIC contract (SCA-1961) - #25217

Merged
Git-on-my-level merged 4 commits into
mainfrom
lane/finops-balance-numeric-1113
Nov 13, 2026
Merged

Git-on-my-level merged 4 commits into
mainfrom
lane/finops-balance-numeric-1113

Conversation

@Git-on-my-level

@Git-on-my-level Git-on-my-level commented Nov 13, 2026 •

Copy link
Copy Markdown
Member

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.py a real entry point, the first production run (finops-unit-cost-job-8fqt6, 2026-11-13 15:39Z) reached the puller and failed loading used_usd: the vendor's decimal has more than nine fractional digits and BigQuery rejected it with Invalid NUMERIC value. The job exited 1 after one retry; omi_finops.vendor_balance_snapshot was created but holds 0 rows. Tracked as SCA-1961; the Soniox line on Omi TV needs two good snapshots.

Change

  • numeric_usd() in pull_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.5 stays 12.5), and a value outside NUMERIC's 29 integer digits becomes an invalid_response skip instead of a load error.
  • parse_balance uses it for both used_usd and available_usd. The derived usd column already quantized to nine places.
  • load_vendor_balance re-renders the artifact's values through the same function, so an artifact written by an older image cannot reach BigQuery unnormalized.
  • README: one sentence on the policy.

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

View guided diff Turn on auto-fix

…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]>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-13T16:47:21.855893Z 2912ba0 New commits
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge 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 👍 / 👎.

Comment on lines +102 to +103
amount = amount.quantize(NUMERIC_SCALE, rounding=ROUND_HALF_UP)
return format(amount, "f")

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge 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 👍 / 👎.

Git-on-my-level and others added 3 commits November 13, 2026 23:30
- 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 Git-on-my-level left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Independent review — exact head 2912ba078b

Reviewed the full diff against main (f3902ec54358) at this head. No blocking findings.

What I verified

  • numeric_usd holds the NUMERIC contract: half-up to 9 fractional digits, plain decimal (never exponent), and the range check runs after rounding, so ...999.9999999995 cannot carry into 10**29. Short values keep their digits (12.5 stays 12.5), which BigQuery accepts.
  • daily_deltas now does subtraction, division and rounding at 60 digits, so a 29-integer-digit balance does not round at the default 28-digit context. The derived usd is padded to 9 places, matching its historical shape.
  • load_vendor_balance re-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.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 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".

Comment on lines +102 to +105
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")

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge 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 👍 / 👎.

@Git-on-my-level
Git-on-my-level merged commit 5f3bc66 into main Nov 13, 2026
61 checks passed
@Git-on-my-level
Git-on-my-level deleted the lane/finops-balance-numeric-1113 branch November 13, 2026 16:57
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