fix(web): reject a negative-zero literal, which loses its sign when serialized

Copilot's twelfth pass. The suppressed comment is right and is fixed here; the
inline one is real too, but needs a core API change and is filed as #2171.

`-0` is represented exactly, so the digit comparison had nothing to say about
it — but `JSON.stringify` writes it as `0`, so the loss happens on the way
*out* rather than on the way in. The editor showed `-0` and the wire carried
`0`, and the viewer rewrote a received `-0` while pretty-printing.

`hasImpreciseIntegerLiteral` becomes `findUnsendableNumberLiteral`, checking
`Object.is(Number(literal), -0)` for every numeric spelling — `-0`, `-0.0`,
`-0e1` — before the integer-only digit check. Returning the offending literal
rather than a boolean lets the message name it, which is more use than either
of the two rules behind it and matches how the duplicate-key message reads.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UL9retfvAXRgvi6EWk4SY4
Signed-off-by: cliffhall <cliff@futurescale.com>
This commit is contained in:
cliffhall 2026-08-27 06:11:23 -04:00
commit 8aa6f01505
5 changed files with 85 additions and 58 deletions

View file

@ -9,7 +9,7 @@
import { isSerializableJson } from "@inspector/core/json/jsonUtils.js";
import {
findDuplicateObjectKey,
hasImpreciseIntegerLiteral,
findUnsendableNumberLiteral,
} from "../../../utils/jsonObjectDraft";
/**
@ -169,7 +169,7 @@ export function formatJson(content: string): string {
function reformatsFaithfully(text: string, parsed: unknown): boolean {
return (
isSerializableJson(parsed) &&
!hasImpreciseIntegerLiteral(text) &&
findUnsendableNumberLiteral(text) === null &&
// A duplicate member name is the same class and the starkest instance:
// `{"role":"user","role":"admin"}` would be *shown* as only the second,
// which is the viewer hiding what the server sent.

View file

@ -218,9 +218,7 @@ describe("EditReplayModal", () => {
renderWithMantine(<EditReplayModal {...baseProps} method="tools/call" />);
await setAceText('{"name":"echo","arguments":{"id":9007199254740993}}');
expect(screen.getByRole("button", { name: "Send" })).toBeDisabled();
expect(
screen.getByText(/cannot be represented exactly/),
).toBeInTheDocument();
expect(screen.getByText(/would not arrive as written/)).toBeInTheDocument();
});
it("disables Send for a non-string prompt argument", async () => {

View file

@ -5,9 +5,9 @@ import { JsonEditor } from "../../elements/JsonEditor/JsonEditor";
import {
duplicateKeyError,
findDuplicateObjectKey,
hasImpreciseIntegerLiteral,
findUnsendableNumberLiteral,
isJsonObject,
IMPRECISE_INTEGER_ERROR,
unsendableNumberError,
} from "../../../utils/jsonObjectDraft";
import {
replayableParams,
@ -113,8 +113,9 @@ function parseParamsDraft(
"Numbers must be finite — a value like `1e400` overflows and would be sent as null",
};
}
if (hasImpreciseIntegerLiteral(text)) {
return { ok: false, error: IMPRECISE_INTEGER_ERROR };
const unsendable = findUnsendableNumberLiteral(text);
if (unsendable !== null) {
return { ok: false, error: unsendableNumberError(unsendable) };
}
const duplicate = findDuplicateObjectKey(text);
if (duplicate !== null) {

View file

@ -789,7 +789,7 @@ describe("SchemaForm", () => {
await setAceTextByLabel(/Config/, "[9007199254740993]");
expect(
screen.getByText(/^This whole number cannot be represented exactly/),
screen.getByText(/^`9007199254740993` would not arrive as written/),
).toBeInTheDocument();
await setAceTextByLabel(/Config/, '[{"a":1,"a":2}]');
@ -1976,9 +1976,7 @@ describe("SchemaForm raw JSON (#2151)", () => {
await setAceTextByLabel(/Arguments JSON/, '{"id":9007199254740993}');
expect(onValidityChange).toHaveBeenLastCalledWith(true);
expect(
screen.getByText(/cannot be represented exactly/),
).toBeInTheDocument();
expect(screen.getByText(/would not arrive as written/)).toBeInTheDocument();
});
// `JSON.parse` keeps only the last occurrence, so the editor would show a

View file

@ -27,9 +27,9 @@ import { JsonEditor } from "../../elements/JsonEditor/JsonEditor";
import {
duplicateKeyError,
findDuplicateObjectKey,
hasImpreciseIntegerLiteral,
findUnsendableNumberLiteral,
isJsonObject,
IMPRECISE_INTEGER_ERROR,
unsendableNumberError,
} from "../../../utils/jsonObjectDraft";
import { isSerializableJson } from "@inspector/core/json/jsonUtils.js";
import { useValueChange } from "../../../hooks/useValueChange";
@ -331,8 +331,9 @@ function describeJsonDraftProblem(text: string): string | null {
if (!isSerializableJson(parsed)) {
return "Numbers must be finite — this field will be omitted";
}
if (hasImpreciseIntegerLiteral(text)) {
return `${IMPRECISE_INTEGER_ERROR} — this field will be omitted`;
const unsendable = findUnsendableNumberLiteral(text);
if (unsendable !== null) {
return `${unsendableNumberError(unsendable)} — this field will be omitted`;
}
const duplicate = findDuplicateObjectKey(text);
if (duplicate !== null) {
@ -652,8 +653,9 @@ function parseRawArgumentsDraft(
}
// The quieter half of the same defect: a whole number past 2^53−1 parses to
// the nearest double, so the draft shows digits the wire will not carry.
if (hasImpreciseIntegerLiteral(text)) {
return { ok: false, error: IMPRECISE_INTEGER_ERROR };
const unsendable = findUnsendableNumberLiteral(text);
if (unsendable !== null) {
return { ok: false, error: unsendableNumberError(unsendable) };
}
const duplicate = findDuplicateObjectKey(text);
if (duplicate !== null) {