ClawRouter icon indicating copy to clipboard operation
ClawRouter copied to clipboard

JSON compaction changes big numbers in tool calls and tool results

Open goranai opened this issue 2 weeks ago • 0 comments

When a request is over 180 KB, the proxy runs the compression layers by default. The jsonCompact layer minifies tool_calls[].function.arguments and tool message content with JSON.stringify(JSON.parse(s)). That round trip changes numbers:

  • 9007199254740993 becomes 9007199254740992 (most integers above 2^53 lose digits)
  • 1234567890123456789012 becomes 1.2345678901234568e+21
  • 1e400 becomes null

So in a long agent session the model gets a different ID than the one the tool returned. Snowflake IDs and database bigints can be above 2^53. The layer comment says "JSON semantics unchanged", but here the data changes.

To reproduce, from a checkout of main after npm ci, save this as repro.mts and run npx tsx repro.mts:

import { compressContext } from "./src/compression/index.ts";

const messages: any[] = [
  { role: "system", content: "x".repeat(6000) },
  {
    role: "assistant",
    content: null,
    tool_calls: [
      {
        id: "c1",
        type: "function",
        function: { name: "close", arguments: '{ "ticket_id": 9007199254740993, "ratio": 1e400 }' },
      },
    ],
  },
  { role: "tool", tool_call_id: "c1", content: '{ "ticket_id": 9007199254740993, "owner": 1234567890123456789012 }' },
];

// Same layers the proxy uses for requests over 180 KB
const r = await compressContext(messages, {
  enabled: true,
  preserveRaw: false,
  layers: { deduplication: true, whitespace: true, dictionary: false, paths: false, jsonCompact: true, observation: false, dynamicCodebook: false },
  dictionary: { maxEntries: 50, minPhraseLength: 15, includeCodebookHeader: false },
});
console.log(r.messages[1].tool_calls[0].function.arguments);
console.log(r.messages[2].content);

Output on main:

{"ticket_id":9007199254740992,"ratio":null}
{"ticket_id":9007199254740992,"owner":1.2345678901234568e+21}

Expected: the same numbers with only the whitespace removed.

I also checked it through the real proxy. I started startProxy with default options against a local mock upstream and sent a 196 KB chat request. The body the upstream received had "ticket_id":9007199254740992 in both the tool call and the tool result.

The published 0.12.312 package has the same code (compactJson in dist/index.js).

Validated with Claude Opus 5.5.

goranai avatar Nov 12 '26 07:11 goranai