Skip to content

fix(python): align local score rounding with the reference - #672

Open
yousizaitianqiong wants to merge 1 commit into
FindAny:mainfrom
yousizaitianqiong:fix/python-rounding-parity
Open

yousizaitianqiong wants to merge 1 commit into
FindAny:mainfrom
yousizaitianqiong:fix/python-rounding-parity

Conversation

@yousizaitianqiong

@yousizaitianqiong yousizaitianqiong commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Problem

The Python SDK can report a mismatch for a scan produced by the TypeScript scorer because Python's built-in round and the reference's scaled half-even rule differ at some boundaries. With synthetic metrics and no active contribution years, an account age of 0.9 produces maturity 1.1 in the current Python scorer and 1.0 in the reference; at 2.7, Python produces 3.1 and the reference produces 3.2. The corrected Python scorer matches the reference in both cases.

Change

Use a shared Python helper for the existing reference rule in the scoring and risk paths, and add cross-runtime fixtures for score and risk boundaries. Preserve the previous Python behavior when scaling a finite value overflows. The tested finite inputs also retain the reference's signed-zero behavior. This follows up the rounding discussion in #231, authored by 4evour; that prior work is not part of this contribution.

The TypeScript and Go scoring implementations and the server's scoring weights are unchanged. The separate _math_round helpers and the fields compared by verify_scan are also unchanged. The implementation does not claim complete cross-language equivalence for all floats: for example, finite values whose scaling overflows retain the existing Python result while the TypeScript and Go helpers can return infinity.

Impact and versioning

The examples above are synthetic. I recomputed the full Python score and risk output for 13 public, anonymized historical fixture snapshots representing 11 fixture aliases; baseline and candidate outputs were identical for those snapshots. This is not a live scan and does not establish how many real accounts are affected.

I also replayed my own public cached scan (scanned_at=1795385705753) offline. The baseline and candidate returned identical full Python scoring outputs, including the risk assessment, with a final score of 67.4. This single snapshot does not establish the wider impact.

scoring/README.md requires a backend score-version bump when outputs change. This patch changes the Python mirror at demonstrated boundaries while leaving the canonical TypeScript scorer unchanged. Could maintainers confirm whether this mirror-only correction needs a coordinated backend version change?

Validation

  • Python 3.14.9: 307 tests and 369 subtests passed; Python 3.8.10: 10 selected tests passed in an independently exported candidate tree.
  • TypeScript: typecheck and 391 tests passed on Node 22.21.1. An independent Node 24.17/Python 3.8 comparison found no numeric or zero-sign mismatch among 9,354 finite-scaled helper samples; 36 overflow-scaled samples were treated separately.
  • Go 1.23.12: go test ./... and go vet ./... passed; the independent review also ran 11 shared parity subtests and a separate boundary probe. Existing Go/TypeScript differences at extreme values and signed zero remain outside this Python patch.
  • Linux pnpm scoring:boundary passed; pnpm reported an existing node_modules/lockfile setting warning.
  • Built wheel and source distribution from the fixed candidate using a hash-locked offline builder. All 16 packaged Python modules match the candidate; rebuilding a wheel from the source distribution produced identical wheel bytes. An installed Python 3.9 console script passed five synthetic verification cases and returned the expected mismatch for a sixth; Python 3.8 imported the wheel directly and passed four selected cases. These are targeted checks, not a full Python 3.8 suite.

The first Windows pytest collection failed because the default GBK encoding could not decode a UTF-8 fixture; the full run passed with UTF-8 enabled. The initial offline pnpm installation lacked package metadata; the successful run reused previously fetched packages. AI assistance was used to investigate, prepare, and test this change.

This branch has not been deployed

No deployments
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