Conversation
When LLMs pass tool arguments with string-typed numbers (e.g. offset: "1",
limit: "2000"), JavaScript evaluates `("1" + "2000") - 1` as `11999`
instead of `2000`, causing bizarre line ranges like "lines 1–11999".
Explicitly parse numeric arguments with Number() before calculating the range.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When an LLM provides tool call arguments with string-typed numbers (e.g.
offset: "1",limit: "2000", as observed with models likeglm-5.3-flash), thereadtool summary displays bizarre, inflated line ranges such as:lines 1–11999(foroffset: "1",limit: "2000")lines 1046–1046399(foroffset: "1046",limit: "400")Root Cause
In
src/summary.ts,readCallcalculates the end of the line range as follows:While
argsis typed as{ offset?: number; limit?: number }, TypeScript types are erased at runtime. Tool call arguments deserialized from LLM responses may carry string values.In JavaScript:
(args.offset ?? 1)evaluates to"1"(string).("1") + args.limitevaluates to"1" + "2000"="12000"(string concatenation due to+associativity)."12000" - 1coerces to a number via subtraction, resulting in11999.Solution
Explicitly coerce
args.offsetandargs.limitto numbers usingNumber()before calculating the range:This prevents string concatenation while preserving the exact original logic and public type definition.
Verification
test/pretty.test.tsverifying string-typed arguments foroffsetandlimit.tsc --noEmit: clean (0 errors).node --test test/pretty.test.ts: all 32 tests passed (0 failed).