Repository navigation
fix(ts): repair overflowing JSON model output - #554
Yezheng Li (yezhengli-Mr9) wants to merge 3 commits into
Conversation
Reject numeric model output outside the finite JavaScript number range before serialization can validate it as null. Reuse the existing parse failure and bounded repair path, preserving the original response diagnostics.
Run the deterministic overflow regressions through both package formats and cover schema-error repair exhaustion plus rejection before null stripping and instance validation. Document the focused offline command.
|
Yezheng Li (@yezhengli-Mr9) please read the following Contributor License Agreement(CLA). If you agree with the CLA, please reply with the following information.
Contributor License AgreementContribution License AgreementThis Contribution License Agreement (“Agreement”) is agreed to by the party signing below (“You”),
|
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues were identified for the numeric-overflow hardening change.
No open issues 🎉
What changed in this PR
This PR hardens the TypeScript translator against overflowing JSON numbers while preserving its existing single-repair budget.
Changes:
- Reject non-finite numbers during parsing, before validation.
- Add offline CommonJS and ESM regressions for rejection, repair, and unchanged valid values.
- Document numeric behavior and focused test commands.
| File | Description |
|---|---|
typescript/tests/numeric-output.test.mjs |
Tests overflow handling and compatibility across both module formats. |
typescript/src/typechat.ts |
Rejects non-finite numbers through a JSON parse reviver. |
typescript/README.md |
Explains overflow rejection, repair behavior, and regression tests. |
Review effort: Balanced
Alternative-tool counterpart PRs
null. Its fix is in canonicalization/hashing, not model-output parsing, and does not add TypeChat's bounded model-repair behavior.Number.isFinite, preserving ordinary conversions and string values. Addresses structured-query casting of"Infinity"/"-Infinity", not overflowing JSON numeric literals such as1e400or parse-repair retries.These are related non-finite-number fixes, not exact equivalents. No verified alternative-tool PR was found combining rejection of overflowing JSON model-output numbers before schema validation with the existing single-repair budget. Counterpart bodies and diffs were checked; their existence is not evidence that they are merged or that this PR's behavior is already implemented elsewhere.
Review overview
Reject overflowing JSON numbers before schema validation and reuse the existing bounded parse-repair path.
Validation and evidence
Related issues, work items, and PRs
Original detailed description, reproduction, and repository checklist
Summary
Reject numeric model responses outside JavaScript's finite number range before schema validation, using the existing parse-failure/one-repair path. Provider-neutral; no model calls were made.
At main
d61044d51da50ec89a756b9ad5fb8a023b69762f, the real TypeScript validator and translator returned successful data containingInfinityfor{"value":1e400}againstvalue: number | null.JSON.parseproduces Infinity, and serialization silently validates null instead of the returned value. Negative and nested overflow behave the same way.Commit
d7d0856checks numbers in the JSON.parse reviver, retaining the existing bounded repair path, original response diagnostics and prompt roles. Finite numbers, actual nulls and strings are unchanged. This does not introduce arbitrary precision or change direct validator APIs. README documents the behavior.Follow-up
71e9a71runs the deterministic regressions through both CommonJS and ESM package entry points, additionally covering overflow after a schema-error repair and rejection before null stripping/application instance validation. No further production-code defect was found in the cross-platform experiments.Experiments and results
These are offline parser/repair experiments using the real TypeScript validator and mocked model responses, not model benchmarks. No Azure/provider calls, spending, token savings, quality gains or cost gains are claimed.
/etc/os-release: Ubuntu 24.04.5 LTS, x86_64Each environment executed the same eight assertions/cases per module format:
Changed-core strict typechecks passed in both CJS and ESM modes on both operating systems.
git diff --checkpassed.Earlier implementation iteration (before this follow-up): five of six new regressions failed before the reviver fix; all six then passed, with 181 tests passed across the selected prompt-layout, metadata, validator and numeric suites on Node 20.19.5. That earlier result is separate from the new 16-per-platform supported-Node result; the complete 181-test selection was not rerun here.
Reproduction
With normal development dependencies installed, from
typescript/, on native Windows or Ubuntu 24.04 with the repository-pinned Node:Constrained-environment procedure actually used
Normal build/test was attempted first, then dependency restoration after missing-dependency failures.
npm testinitially found the obsolete global compiler because the local executable/dependencies were missing. On supported Node,npm ci --ignore-scripts --fetch-retries=0 --fetch-timeout=30000failed E401; direct npm-registry access also failed a TLS handshake. No TLS verification, authentication, signing or hook checks were disabled. No manifests or lockfiles changed.Official downloads worked:
SHASUMS256.txt:https://nodejs.org/dist/v24.22.1/https://github.com/microsoft/TypeScript/releases/tag/v7.0.2Downloaded archive SHA-256 values were checked against Node's published sums / GitHub release asset digests:
Extract
typescript.tgzintotypescript/node_modules/typescriptand the matching platform package intotypescript/node_modules/@typescript/typescript-{win32,linux}-x64, using--strip-components=1. Put the extracted official Node on PATH.To reproduce the fallback without unavailable development dependencies, create these configurations in
.scratch/cross-platform/relative to the repository root:core-cjs.json:{ "compilerOptions": { "target": "es2021", "lib": ["es2021"], "module": "node20", "strict": true, "exactOptionalPropertyTypes": true, "types": [], "noEmit": true, "skipLibCheck": true }, "files": ["../../typescript/src/typechat.ts"] }core-esm.json:{ "extends": "./core-cjs.json", "compilerOptions": { "module": "es2022", "moduleResolution": "bundler" } }emit-cjs.json:{ "extends": "../../typescript/src/tsconfig.json", "compilerOptions": { "types": [], "rootDir": "../../typescript/src", "noCheck": true } }emit-esm.json:{ "extends": "../../typescript/src/tsconfig.esm.json", "compilerOptions": { "types": [], "rootDir": "../../typescript/src", "noCheck": true } }Then from the repository root (forward slashes below are portable Node CLI arguments; PowerShell also accepts backslashes):
For Ubuntu, use
docker run --rm ... -w /work ubuntu:24.04 sh <script>with the same source/configurations and linux-x64 packages copied/extracted inside/work, rather than running the Windows dependencies. The script runscat /etc/os-release,uname -m,node --version, compiler--version, then the six commands above. In this execution the Windows checkout was mounted read-only as/source; source and release archives were extracted into container-local/work. The resulting test summary wastests 16; suites 2; pass 16; fail 0; skipped 0.Environment iterations: the minimal Ubuntu image lacked
xz; an initial extraction onto the Windows bind mount was also prohibitively slow. Host-side decompression to an uncompressed tar followed by container-local extraction resolved that. The shell script's Windows CRLF was normalized before execution. These were harness/setup failures, not TypeChat failures.Verification limits: source emission used
--noCheckin the fallback; only the changed core and its imports received the separate strict checks above. Fullnpm test, full-source strict build and workspacebuild-allare still not claimed. Keep this PR draft pending normal dependency-backed CI and maintainer review.