Files
magpie/internal/gateway/grok_integral.go
T
yetoneandggbdpq c272e55825 gateway: any model's 8.0 in a call's arguments reaches the agent as 8, not only Grok's (#3152)
1024MrL's Codex, on opencode-go/muse-spark-1.3-contributor through the
gateway, had every tool_search turned away before it ran ("failed to
parse tool_search arguments: invalid number" over {"limit": 8.0}), and
exec_command the same ("floating point `2000.0`, expected usize").
grok_integral.go's whole-number rewrite (#1431) already fixes exactly
this, but it ran only when the model was Grok's.

Decision (delegated by the owner): the rewrite runs for every model,
not narrowed to Grok. Why:
- The defect is the model's, not a vendor's: Grok (#1431) and muse-spark
  (#3152) both write it, and no list of models would stay complete.
- The rewrite changes only a number written with a fraction of zeros and
  no exponent, to the same JSON number. Strict parsers break on 8.0
  (Codex's serde u64/usize/i32, Go's json into an int), while every
  float-typed field takes 8 (serde f64, Go float64, JS). The one visible
  difference is a Python client's json.loads giving int 8 for float 8.0,
  which any `number` schema allows.
- Strings, keys, other fractions (2.5, 2.50) and exponents (1.0e3) stay
  as written, held by integralCases' edges case.
- The reporter ran it end to end on Windows with real Codex 0.163 and
  muse-spark: 6 of 7 and 20 of 21 calls refused without it, 0 with it in
  two runs (thread, 10-23).
Gemini routes are left as they were: their call arguments are an object
the client decodes itself, and no report shows the shape there.

What changed:
- passthrough installs intTidy on every Responses, Chat and Messages
  reply; translate's Request.Grok and grokServed are gone, encoder wraps
  every reply in wholeEncoder and render runs wholeResult. grokModel
  stays for grok_schema.go.
- crTidy (glm-5.3-flash*'s stitched CR, #4256) now runs before intTidy,
  in the stream, the held-tail chain and the drain. With intTidy on every
  route, a whole reply it held went from its flush past crArgs, so the
  CR cure never saw it (added in this port: the patch's comment gave a
  different reason).
- intTidy's flush says a whole reply with no call marker as it came,
  without decoding it (added in this port), since every model's whole
  reply now reaches it.
- TestOnlyGrokCallsGetIntegers (which held the old scope) became
  TestOtherModelsCallsGetCodexIntegers; TestGrokCallsByUpstreamName is
  gone; TestToolSearchWholeFloatLimitGivesCodexIntegers is the report's
  shape; TestStitchedCRAndWholeFloatsBothGo (added in this port) holds
  both cures on a glm-5.3-flash route, whole and streamed. Its whole
  case reads the input's fields as written, not through a decode and
  encode, which would print 8.0 as 8 and pass on the old code.

Run at this head (rebased on 2c5d9101b, after #2580's held line in
passthrough's write), on mote:
- go test -tags nogui -count=1 -run 'TestWholeNums|TestGrok|
  TestOtherModelsCallsGetCodexIntegers|TestToolSearchWholeFloat|
  TestStitchedCRAndWholeFloatsBothGo|StitchedCR|TestCRLeavesHealthy|
  TestCRDropper|TestPluginStreamLargeReplies|TestQuietWholeReplyStreamed|
  TestTextWritten|TestChatStreamCut|TestPassthroughStream|TestNameless'
  ./internal/gateway: ok (61s).
- go vet -tags nogui ./internal/gateway on darwin, GOOS=linux and
  GOOS=windows: clean. gofmt -l: clean.
- At the review tree: the -run family above (without the last three)
  with -race -count=2: ok.
- Not run before the push: the whole gateway package and ./... (the
  package runs in the background after).

Broken by hand:
- gateway.go, grok_integral.go and ir.go back at origin/main, tests
  kept: TestToolSearchWholeFloatLimitGivesCodexIntegers fails "deltas:
  limit is 8.0 in {"query":"exa web search","limit":8.0}, want 8";
  TestOtherModelsCallsGetCodexIntegers fails "deltas: yield_time_ms is
  2500.0 ..., want 2500"; TestStitchedCRAndWholeFloatsBothGo fails in
  both subtests ("limit":8.0).
- the rewrite on every route but the old order (ints before crArgs):
  TestStitchedCRAndWholeFloatsBothGo/whole, TestChatReplyToolArgsLoseStitchedCR
  and TestAnthropicWholeRelayedToolArgsLoseStitchedCR fail with the
  stitched \r kept.

Not tried: a real muse-spark or Codex run by this port (the reporter's
runs stand in); a Gemini client. Risk not covered: a vendor that streams
SSE labelled Content-Type application/json. eventStream reads that as a
whole reply, and intTidy, now on every Responses, Chat and Messages
route, holds a whole reply to its flush, so such a stream would reach
the client only at its end, on every route rather than Grok's alone
(crTidy and chatWhole already hold such bodies on theirs). No vendor was
checked for it; a report of a stream arriving all at once points here.

Co-authored-by: ggbdpq <ggbdpq@gmail.com>
2026-10-31 09:38:21 +08:00

417 lines
11 KiB
Go

package gateway
import (
"bytes"
"encoding/json"
"strconv"
"strings"
"github.com/yetone/magpie/internal/provider"
)
// Models write a whole number in a tool call's arguments with a fraction
// of zeros ("yield_time_ms":2500.0, "session_id":17277.0), whoever serves
// them: Grok's do it on every turn (#1431, 11 of 11 turns on grok-4.6 and
// grok-4.7 through a relay), and muse-spark wrote tool_search's limit as
// 8.0 through the gateway (#3152). Codex reads such fields as u64, usize,
// i32 or i64 (exec_command, write_stdin, shell's timeout_ms, sleep,
// tool_search's limit, wait), though their schemas say "number", and serde
// turns the whole call away: "failed to parse function arguments: invalid
// type: floating point `2500.0`, expected u64". Any client that types its
// arguments strictly does the same.
//
// In a reply, a number in a call's arguments written with a fraction of
// zeros and no exponent loses that fraction: 2500.0 is written 2500. JSON
// has one kind of number, so 2500 is the same value to every client, and
// one that took 2500.0 takes 2500; a tool's schema isn't asked, since
// Codex's own say "number" for the integers it reads. Strings, keys, other
// fractions (2.5) and exponents (2.5e3) stay as written.
//
// The rule is applied to the arguments as a stream of text, so it gives
// the same arguments whether they come whole or in deltas cut anywhere:
// what the deltas of a call add up to is what its done events say.
// grokModel is whether id, the model as magpie or its upstream names it, is
// one of Grok's, under whatever vendor prefix a relay or router gives it
// (xai/grok-4.5, x-ai/grok-4). Grok's schema quirks (grok_schema.go) are
// still Grok's alone; whole floats in calls' arguments are every model's.
func grokModel(id string) bool {
if i := strings.LastIndexByte(id, '/'); i >= 0 {
id = id[i+1:]
}
return strings.HasPrefix(strings.ToLower(id), "grok")
}
// wholeNums drops the zero fraction of each number in a stream of JSON
// text, fed to it a piece at a time. It holds only a fraction of zeros
// (".0", ".00") until it knows whether the number ends there, in which
// case the fraction is dropped, or goes on (2500.05, 2500.0e3), in which
// case it is written as it came; so a piece never waits on the next one
// for anything but such a fraction, and nothing is held at the end.
type wholeNums struct {
str, esc bool // in a string, after its backslash
num bool // in a number
asIs bool // the rest of this number goes as written
zeros int // a fraction of zeros held: its dot, then its zeros
}
func (n *wholeNums) feed(s string) string {
var b strings.Builder
for i := 0; i < len(s); i++ {
c := s[i]
if n.num {
switch {
case n.zeros > 0 && c == '0':
n.zeros++
continue
case n.zeros > 0 && (c >= '1' && c <= '9' || c == 'e' || c == 'E'):
// not a fraction of zeros: what was held goes as it came
b.WriteByte('.')
b.WriteString(strings.Repeat("0", n.zeros-1))
n.zeros, n.asIs = 0, true
b.WriteByte(c)
continue
case n.zeros == 0 && c == '.' && !n.asIs:
n.zeros = 1
continue
case n.zeros == 0 && (c >= '0' && c <= '9' || c == '.' || c == 'e' || c == 'E' || c == '+' || c == '-'):
if c == 'e' || c == 'E' || c == '.' {
n.asIs = true
}
b.WriteByte(c)
continue
}
// the number ended: a fraction of zeros held is dropped
n.num, n.asIs, n.zeros = false, false, 0
}
switch {
case n.str:
if n.esc {
n.esc = false
} else if c == '\\' {
n.esc = true
} else if c == '"' {
n.str = false
}
case c == '"':
n.str = true
case c == '-' || c >= '0' && c <= '9':
n.num = true
}
b.WriteByte(c)
}
return b.String()
}
// wholeArgs is a call's whole arguments with the zero fraction of each of
// their numbers dropped.
func wholeArgs(s string) string {
if !strings.Contains(s, ".") {
return s
}
var n wholeNums
return n.feed(s)
}
// wholeEncoder hands its encoder a reply with the zero fractions dropped
// from its calls' arguments, streamed in pieces.
type wholeEncoder struct {
streamEncoder
args wholeNums
}
func (e *wholeEncoder) event(ev Event) {
switch ev.Kind {
case KToolStart:
e.args = wholeNums{}
case KToolArgs:
if ev.Text = e.args.feed(ev.Text); ev.Text == "" {
return // held: a fraction of zeros the next piece may end
}
}
e.streamEncoder.event(ev)
}
// wholeResult is a whole reply with its calls' arguments' zero fractions
// dropped.
func wholeResult(res Result) Result {
var parts []Part
for i, p := range res.Parts {
if p.Kind != ToolCall || len(p.Args) == 0 {
continue
}
if a := wholeArgs(string(p.Args)); a != string(p.Args) {
if parts == nil {
parts = append([]Part(nil), res.Parts...)
}
parts[i].Args = json.RawMessage(a)
}
}
if parts != nil {
res.Parts = parts
}
return res
}
// intTidy drops, in a reply relayed as it came, the zero fractions of its
// calls' arguments: Responses' function_call items and argument deltas,
// Chat's tool_calls, and Messages' tool_use input. A stream's lines pass
// as they came unless one carries arguments that change; a reply that
// isn't one is held whole and gone through at the end.
type intTidy struct {
proto provider.Protocol
sse bool
buf []byte
calls map[string]*wholeNums // a streamed call's arguments so far, by its index or item id
}
func (t *intTidy) write(b []byte) []byte {
t.buf = append(t.buf, b...)
if !t.sse {
return nil
}
i := bytes.LastIndexByte(t.buf, '\n')
if i < 0 {
return nil
}
out := t.lines(t.buf[:i+1])
t.buf = append(t.buf[:0], t.buf[i+1:]...)
return out
}
func (t *intTidy) flush() []byte {
b := t.buf
t.buf = nil
if !t.sse {
// every model's whole reply comes here now: one with no call in
// it is said as it came, without being decoded
if !t.carries(b) {
return b
}
if out, ok := t.event(b); ok {
return out
}
return b
}
return t.lines(b)
}
// carries is whether b may carry a call's arguments.
func (t *intTidy) carries(b []byte) bool {
switch t.proto {
case provider.Responses:
return bytes.Contains(b, []byte(`function_call`))
case provider.Chat:
return bytes.Contains(b, []byte(`"tool_calls"`))
case provider.Anthropic:
return bytes.Contains(b, []byte(`"tool_use"`)) || bytes.Contains(b, []byte(`"input_json_delta"`))
}
return false
}
func (t *intTidy) lines(b []byte) []byte {
if !t.carries(b) {
return append([]byte(nil), b...)
}
var out []byte
for len(b) > 0 {
line := b
if i := bytes.IndexByte(b, '\n'); i >= 0 {
line, b = b[:i+1], b[i+1:]
} else {
b = nil
}
body := bytes.TrimRight(line, "\r\n")
data, ok := bytes.CutPrefix(body, []byte("data:"))
if !ok || !t.carries(data) {
out = append(out, line...)
continue
}
nb, ok := t.event(data)
if !ok {
out = append(out, line...)
continue
}
out = append(append(append(out, "data: "...), nb...), line[len(body):]...)
}
return out
}
// event is an event, or a whole reply, with its calls' arguments' zero
// fractions dropped, and whether that changed it.
func (t *intTidy) event(data []byte) ([]byte, bool) {
dec := json.NewDecoder(bytes.NewReader(data))
dec.UseNumber() // the event's own numbers as they came
var ev map[string]any
if dec.Decode(&ev) != nil || ev == nil {
return nil, false
}
var changed bool
switch t.proto {
case provider.Responses:
changed = t.responses(ev)
case provider.Chat:
changed = t.chat(ev)
case provider.Anthropic:
changed = t.anthropic(ev)
}
if !changed {
return nil, false
}
nb, err := marshalPlain(ev)
if err != nil {
return nil, false
}
return nb, true
}
// stream is the arguments so far of the streamed call at key.
func (t *intTidy) stream(key string) *wholeNums {
if t.calls == nil {
t.calls = map[string]*wholeNums{}
}
n := t.calls[key]
if n == nil {
n = &wholeNums{}
t.calls[key] = n
}
return n
}
// set writes m[k], a string of arguments, through f, and says whether it
// changed.
func set(m map[string]any, k string, f func(string) string) bool {
s, ok := m[k].(string)
if !ok {
return false
}
if out := f(s); out != s {
m[k] = out
return true
}
return false
}
func (t *intTidy) responses(ev map[string]any) bool {
changed := false
item := func(it map[string]any) {
if it != nil && it["type"] == "function_call" && set(it, "arguments", wholeArgs) {
changed = true
}
}
it, _ := ev["item"].(map[string]any)
item(it)
switch ev["type"] {
case "response.function_call_arguments.delta":
key := callKeyOf(ev)
if set(ev, "delta", t.stream(key).feed) {
changed = true
}
case "response.function_call_arguments.done":
delete(t.calls, callKeyOf(ev))
if set(ev, "arguments", wholeArgs) {
changed = true
}
}
res, _ := ev["response"].(map[string]any)
if res == nil && ev["object"] == "response" {
res = ev // a whole reply
}
if res != nil {
out, _ := res["output"].([]any)
for _, o := range out {
im, _ := o.(map[string]any)
item(im)
}
}
return changed
}
// callKeyOf is the key a Responses argument event's call is streamed under:
// its item id, or where it is in the output.
func callKeyOf(ev map[string]any) string {
if id, _ := ev["item_id"].(string); id != "" {
return id
}
return "#" + jsonText(ev["output_index"])
}
func jsonText(v any) string {
b, _ := json.Marshal(v)
return string(b)
}
func (t *intTidy) chat(ev map[string]any) bool {
changed := false
choices, _ := ev["choices"].([]any)
for _, c := range choices {
cm, _ := c.(map[string]any)
if cm == nil {
continue
}
// a stream's delta, its arguments in pieces by the call's index
if d, _ := cm["delta"].(map[string]any); d != nil {
calls, _ := d["tool_calls"].([]any)
for j, tc := range calls {
tm, _ := tc.(map[string]any)
fn, _ := tm["function"].(map[string]any)
if fn == nil {
continue
}
idx := jsonText(tm["index"])
if tm["index"] == nil {
idx = strconv.Itoa(j)
}
if set(fn, "arguments", t.stream(jsonText(cm["index"])+"/"+idx).feed) {
changed = true
}
}
}
// a whole reply's message
if m, _ := cm["message"].(map[string]any); m != nil {
calls, _ := m["tool_calls"].([]any)
for _, tc := range calls {
tm, _ := tc.(map[string]any)
if fn, _ := tm["function"].(map[string]any); fn != nil && set(fn, "arguments", wholeArgs) {
changed = true
}
}
}
}
return changed
}
func (t *intTidy) anthropic(ev map[string]any) bool {
changed := false
// a whole reply's tool_use blocks, their input an object
block := func(b map[string]any) {
if b == nil || b["type"] != "tool_use" || b["input"] == nil {
return
}
in, err := marshalPlain(b["input"])
if err != nil {
return
}
if out := wholeArgs(string(in)); out != string(in) {
b["input"] = json.RawMessage(out)
changed = true
}
}
content, _ := ev["content"].([]any)
for _, c := range content {
cm, _ := c.(map[string]any)
block(cm)
}
cb, _ := ev["content_block"].(map[string]any)
block(cb)
// a stream's input, in pieces by the block's index
if ev["type"] == "content_block_delta" {
if d, _ := ev["delta"].(map[string]any); d != nil && d["type"] == "input_json_delta" {
if set(d, "partial_json", t.stream(jsonText(ev["index"])).feed) {
changed = true
}
}
}
return changed
}