fix(grpc): preserve oversized integer tool-call arguments digit-exact - #2115
fix(grpc): preserve oversized integer tool-call arguments digit-exact#2115pallasathena92 wants to merge 1 commit into
Conversation
History normalization parses each assistant tool call's arguments string into a JSON value before template rendering. Integers outside the i64/u64 range fall back to f64 during that parse, so a large id like 123456789012345678901234567890 silently re-renders with different digits and the model echoes a corrupted value back to the tool. Guard the parse with a minimal string-state pre-scan that wraps only canonical integer literals neither i64 nor u64 can hold in quotes, preserving every digit as a JSON string. In-range integers and floats stay native numbers, digits inside string values are untouched, and malformed input still fails in the parser with the original text in the error. Rendering such an argument quoted rather than bare is the accepted trade: values at that magnitude are invariably identifiers that must round-trip verbatim. Signed-off-by: yifeng liu <31553858+pallasathena92@users.noreply.github.com>
|
Important Review skippedDraft detected. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Plus Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Clean, well-tested fix. The lexer correctly handles string escaping, float-shaped tokens, canonical-form validation, and the zero-copy borrowed path. Test coverage is thorough across boundaries (u64::MAX, i64::MIN), nested containers, escaped-quote edge cases, and malformed input fallthrough. No issues found.
Description
Problem
process_tool_call_argumentsparses each assistant tool call'sargumentsstring into a JSON value before chat-template rendering. serde_json represents integers outside the i64/u64 range as f64, so an argument like{"id": 123456789012345678901234567890}silently re-renders as1.2345678901234568e29. The model sees the corrupted spelling in its rendered history and echoes a wrong id back to the tool on the next call.Solution
Pre-scan the arguments string with a minimal string-state lexer and wrap only the integer literals neither i64 nor u64 can hold in quotes, so they survive the parse digit-exact as JSON strings. The guard is deliberately narrow:
Rendering such an argument quoted rather than bare is the accepted trade-off: values at that magnitude are invariably identifiers that must round-trip verbatim, and exact-but-quoted beats bare-but-corrupted.
Changes
model_gateway/src/routers/grpc/utils/chat_utils.rs: addquote_unrepresentable_integers(Cow-returning pre-scan; borrows when no rewrite is needed) andis_canonical_json_integer; apply the guard inprocess_tool_call_arguments.Test Plan
Six new unit tests in
chat_utils:>u64::MAXpositive and<i64::MINnegative integers survive digit-exact (top level and nested containers);{bad,[1-2], leading-zero integers) still error, citing the original text;Cow::Borrowedpath.cargo clippy -p smg --all-targetsclean;cargo test -p smg --lib chat_utils30/30.Checklist
make fmtcargo clippy -p smg --all-targets -- -D warnings