@devries-cameron20 / t3code Mirror · read-only / commits / 3a87ca8c6a
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 13:30 UTC

fix(client-runtime): tool output no longer rounds integers beyond 2^53 (#30806)

Co-authored-by: Geezy <igor.licko@gloryck.com> Co-authored-by: Julius Marminge <julius0216@outlook.com>

TheGuyWithHAT committed Nov 15, 2026 at 01:12 UTC 3a87ca8c6abc3a13c2f1d5d3e018e433b6dca9c9
2 files changed +41 -2
packages/client-runtime/src/work-log/itemDetail.test.ts
+20
@@ -114,6 +114,26 @@ describe("tool image output parsing", () => {
114 );
115 });
116
117 +describe("tool text output formatting", () => {
118 + it.each([
119 + '{"id":1234567890123456789,"name":"x"}',
120 + '{"id":1}\n{"id":-9007199254740993}',
121 + '{"id":9.007199254740993e15}',
122 + '{"id":1e309}',
123 + ])("keeps %j as returned when parsing would change a number", (text) => {
124 + expect(turnItemOutputText(toolItem({ content: [{ type: "text", text }] }))).toBe(text);
125 + });
126 +
127 + it.each(['{"id":42,"note":"12345678901234567890"}', '{"ratio":0.1234567890123456789}'])(
128 + "still pretty-prints %j",
129 + (text) => {
130 + expect(turnItemOutputText(toolItem({ content: [{ type: "text", text }] }))).toBe(
131 + JSON.stringify(JSON.parse(text), null, 2),
132 + );
133 + },
134 + );
135 +});
136 +
137 describe("MCP app tool output", () => {
138 it("shows the tool result, not the app wrapper", () => {
139 const text = "Notice\nview\tcount\n/home\t42";
packages/client-runtime/src/work-log/itemDetail.ts
+21 -2
@@ -72,14 +72,33 @@ export function isJsonToolText(text: string): boolean {
72 return documents.length > 1 && documents.every((document) => parseJson(document) !== undefined);
73 }
74
75 +// Strings match whole so numbers inside them are skipped. Scan only parsed JSON: every string closes.
76 +const JSON_STRING_OR_NUMBER = /"(?:[^"\\]|\\.)*"|-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?/g;
77 +
78 +/** JSON.parse rounds integers beyond 2^53 and overflows to Infinity, so reformatting would change the value. */
79 +function hasLossyJsonNumber(json: string): boolean {
80 + for (const [token] of json.matchAll(JSON_STRING_OR_NUMBER)) {
81 + if (token.startsWith('"')) continue;
82 + const value = Number(token);
83 + if (!Number.isFinite(value) || (Number.isInteger(value) && !Number.isSafeInteger(value))) {
84 + return true;
85 + }
86 + }
87 + return false;
88 +}
89 +
90 function prettyJsonText(text: string): string {
91 const trimmed = text.trim();
92 if (!/^[[{]/.test(trimmed)) return text;
93 const whole = parseJson(trimmed);
79 - if (whole !== undefined) return JSON.stringify(whole, null, 2);
94 + if (whole !== undefined) {
95 + return hasLossyJsonNumber(trimmed) ? text : JSON.stringify(whole, null, 2);
96 + }
97 const lines = trimmed.split("\n").filter((line) => line.trim());
98 const documents = lines.map((line) => parseJson(line.trim()));
82 - if (documents.some((document) => document === undefined)) return text;
99 + if (documents.some((document) => document === undefined) || hasLossyJsonNumber(trimmed)) {
100 + return text;
101 + }
102 return documents.map((document) => JSON.stringify(document, null, 2)).join("\n\n");
103 }
104