Skip to content

test(mcp_impl): pin parseBalancesJson float64 truncation bug (failing)#142

Open
gfyrag wants to merge 1 commit into
mainfrom
test/mcp-float-truncation-bug
Open

test(mcp_impl): pin parseBalancesJson float64 truncation bug (failing)#142
gfyrag wants to merge 1 commit into
mainfrom
test/mcp-float-truncation-bug

Conversation

@gfyrag
Copy link
Copy Markdown
Contributor

@gfyrag gfyrag commented Jun 3, 2026

Summary

Failing regression tests demonstrating a silent corruption bug in
internal/mcp_impl/handlers.go::parseBalancesJson. CI is intentionally
red
on this branch — see the stacked fix PR below.

JSON numeric values reach the MCP evaluate tool as float64 (Go's
default when unmarshalling into any). The handler converts them with
big.NewFloat(amount).Int(new(big.Int)), which:

  • Silently truncates fractional parts. 100.9 becomes 100.
  • Silently rounds large values. Anything past ±(2^53 - 1) cannot be
    losslessly represented as a float64, so the integer the caller meant
    is gone before this handler ever sees it.

For a financial DSL these are not acceptable behaviors. The handler
should reject any value that isn't representable as an exact integer
in float64 precision.

Stack

PR What it does Target
this PR Adds failing regression tests main
Fix PR (TBD) Validates the amount before conversion this branch

Merging the fix flips the failing tests green.

Test plan

  • TestParseBalancesJsonRejectsNonIntegerAmounts fails today (100.9 is silently truncated to 100).
  • TestParseBalancesJsonRejectsUnsafelyLargeAmounts fails today (1e18 is silently accepted).
  • TestParseBalancesJsonAcceptsExactIntegerAmounts passes (sanity).

🤖 Generated with Claude Code

JSON numeric values reach the MCP `evaluate` tool as float64 (Go's
default for unmarshalling into `any`), and parseBalancesJson currently
funnels them through `big.NewFloat(amount).Int(new(big.Int))` — which
silently truncates fractional parts and rounds values past the exact-
integer range of float64 (±(2^53 - 1)).

For a financial DSL, silent corruption of balance amounts is not
acceptable: the handler should reject any non-integer or unsafely-
large value so the caller is forced to switch to a safer encoding.

This commit introduces a failing white-box test
`TestParseBalancesJsonRejectsNonIntegerAmounts` and
`TestParseBalancesJsonRejectsUnsafelyLargeAmounts` that pin the bug.
The third test (`TestParseBalancesJsonAcceptsExactIntegerAmounts`)
documents the expected happy path and passes today. CI is
intentionally red on this branch.

The fix is in a stacked PR that targets this branch — merging it
flips the failing tests green.

Flagged by CodeRabbit during review of #139.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Jun 3, 2026

Review Change Stack

Walkthrough

Added unit test file internal/mcp_impl/handlers_test.go containing three tests for parseBalancesJson. Tests verify the parser rejects JSON numeric balances when they are fractional or unsafe-large floats, and accepts safe exact integers by returning the correct *big.Int value.

Changes

parseBalancesJson Test Coverage

Layer / File(s) Summary
Exact-integer validation tests
internal/mcp_impl/handlers_test.go
Three unit tests validate that parseBalancesJson rejects fractional amounts, rejects unsafe-large float64 values beyond 2^53 - 1, and accepts safe exact-integer amounts with correct *big.Int conversion.

🎯 1 (Trivial) | ⏱️ ~3 minutes

🐰 Three tests now guard the balance parser,
Fractional floats shall not pass the marker,
Big integers bloom where exactness reigns,
Safe math prevails through careful constraints! ✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly identifies the main change: adding tests that demonstrate a float64 truncation bug in parseBalancesJson. It accurately reflects the changeset's purpose.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description check ✅ Passed The pull request description clearly relates to the changeset by explaining the bug being tested, why the tests fail, and the overall intent to add regression tests.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch test/mcp-float-truncation-bug

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

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