Skip to content

Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.

gateway: Codex on Copilot is relayed with its web_search, and a translated call's arguments stream (echo_ts)

yetoneec7f47432315
diff --git a/docs/subsystems/gateway-routing.md b/docs/subsystems/gateway-routing.md
index ccea32f78..90600cf95 100644
--- a/docs/subsystems/gateway-routing.md
+++ b/docs/subsystems/gateway-routing.md
@@ -172,6 +172,7 @@ recorded as a prediction failure (`noPrediction`).
- A model that refuses a conversation ending with the assistant's message (`refusesPrefill`: OpenCode Go's Claude models over Messages, "This model does not support assistant message prefill"; Volcengine's Agent Plan, "…for a model that does not support prefill"; Gemini's, "Requests ending with a model turn are not supported") is marked for that provider, model and API (`prefillRefused`, #1447); the provider's other models keep their prefill ([`prefill.go`](../../internal/gateway/prefill.go)). A Chat or Responses client's conversation ending with the assistant's message, which those APIs read as a turn already said (`Request.LastIsTurn`), is asked again with a user turn after it (`continueText`), translated (`forwardTranslated`) or relayed (`passthrough`, `userLastBody`), and so from then on. An Anthropic client's prefill is its own ask and gets the refusal as sent. magpie's own continuation of a cut reply (`continuation.go`) isn't asked of that model again; the try the model refused ends the reply with what cut it, not with the refusal. A request magpie builds in Gemini's own shape (`buildCodeAssistSent`: Vertex AI, Gemini CLI's and Antigravity's Code Assist, Factory) never ends with the model's turn, which all of them turn away: a user turn goes after it, `continueText`, or `compactLastText` when an agent is compacting with its ask in the system prompt (`modelLastAsk`), as one goes before a history that opens with the model's. Nor does it send a user turn that mixes a tool's `functionResponse` with text, which Vertex AI answers with the same "ending with a model turn" (#1533: Codex's compaction prompt after a tool's output): what the user said goes in a user turn of its own after the tools' responses (`hasResponse`), except for a Claude model on Antigravity, whose tool_result turn holds both.
- A tool's `strict` that an upstream turns away (`toolStrictRefusal` in [`strict_refused.go`](../../internal/gateway/strict_refused.go): "Unsupported Chat Completions API field: tools[0].function.strict", OpenAI-style unknown_parameter, "Extra inputs are not permitted" and pydantic's extra_forbidden on it) is asked once more without it, in Chat's `tools[].function` and Responses' `tools[]` alike, and marked for that provider and API (`toolStrictRefused`): its next requests go without it at once. OpenCode 2 sends `"strict": false` on every tool through `@ai-sdk/openai-compatible`, and a custom provider that refused it ended the session (#1980). An upstream that hasn't refused it still gets it, as the ChatGPT backend needs it (#383). `TestToolStrictLeftOutWhereRefused` (real OpenCode 2.0.25 requests), `TestToolStrictRefusalWordings`.
- A Codex custom tool (exec, apply_patch) that a Responses upstream turns away (`customToolRefusal` in [`custom_tool.go`](../../internal/gateway/custom_tool.go): DeepSeek's 400 "Unsupported custom tool: 'exec'. Only 'apply_patch' is supported.", #2656) ends the relay: `passthrough` marks the model (`customToolRefused`) and returns not done, and `attempt` translates the request on the same API (`ownAPI`), where each custom tool is offered as a function taking its `input` and a call comes back as the `custom_tool_call` Codex runs, its name kept. A marked model's next requests with a custom tool are translated at once; one without stays relayed, and an upstream that never refused them still gets them as sent. A compaction (`keepsCompaction`) isn't translated, and gets the refusal. Where the refusal reaches a group untranslated it is a shape refusal (`shapeRefused`): the next member is asked. `TestCustomToolTranslatedWhereRefused`, `TestCustomToolRefusalInGroup`, `TestCustomToolRefusalWordings`.
+- A tool call's arguments reach the client as the vendor writes them, translated too (echo_ts on Discord: Codex on Copilot about 10x slower, an apply_patch shown only once whole). The Responses decoder hands each `function_call_arguments.delta` and `custom_tool_call_input.delta` on as a `KToolArgs` delta (`argsDelta`), holding back only the one that would make the JSON whole until `.done`, which `endCall` checks against the final arguments; the Responses encoder streams a custom tool's input as `custom_tool_call_input.delta` (`customStream` in [`custom_tool.go`](../../internal/gateway/custom_tool.go)) before its `.done`. A Copilot account's Responses request is relayed with a `web_search` tool in it (`searchesItself`; checked on a real account 2026-10-17), and Copilot is never picked to search for another provider's model (`searchRank`). It is translated instead when a tool marks a parameter `"encrypted": true` (`asksSealing`, MultiAgentV2's `spawn_agent`): Copilot seals such an argument and can't read its own seal on the subagent's turn. `TestCallArgsStreamWhenTranslated`, `TestCustomInputStreams`, `TestResponsesCallArgsStream`, `TestCopilotRelayedUnlessAskedToSeal`, `TestCopilotUltraSubagentWithWebSearch`.
- A model's reply limit is published within the window beside it (#1438, `provider.OutputWithin`, `Entry.PublishedOutput`): `/v1/models`' `max_output_tokens` (`modelObject`), Cursor Private Inference's `capabilities.max_output_tokens` (`cursorLocalModel`), Muse Code's `limit.output` (`museModels`), the GUI's model and group rows (`providerInfo`, `providersState`), `magpie model output`, and every agent's file (`agent.maxTokens`, #338). The window and the output can come from different sources: Grok's backend lists grok-4.7's 256000 window, and models.dev fills its 500000 output (`catalog.Decorate` and `Provider.replyLimit` fill an output only where the vendor gives none). An unknown window or output is published as it is. The catalog keeps the unclamped `Entry.Output`, and a group's member is still asked for no more than that (`outputLimit`, `withMaxOutput`), so a request isn't lowered by the clamp. A limit the user set stays in their settings as they set it (`TestModelListsKeepOutputWithinTheWindow`). Outside a group, only a Max output the user set for the model (`Entry.OutputSet`) lowers a request (`userOutput`): Claude Code asks a claude-* model for its own full limit (128000 on Opus 5.5) and is told no figure of magpie's for it (`claudeOutput`), and a key whose quota is held per request as max_tokens times the price turned every turn away (YaCo on Discord, `TestUserMaxOutputLowersAPlainProvidersRequest`). A vendor's or models.dev's figure doesn't lower it, since it may be stale.
- A request that names no reply limit, translated to Anthropic's Messages API, which needs `max_tokens` (`forwardTranslated`), is sent the model's published limit (`askedOutput` in [`maxoutput.go`](../../internal/gateway/maxoutput.go)): the user's Max output, else the vendor's list or models.dev, within the window. `anthropicBody`'s `defaultMaxTokens` (16384) is left only for a model whose limit isn't known. Every Codex turn on a Claude went at 16384 before, and a long thinking and tool turn ran out of it: `stop_reason: max_tokens` reaches a Responses client as `incomplete_details.reason: "max_output_tokens"` (#1792, tensornull). A limit the client names goes as it is. The thinking budget fits under the filled limit as under one the client named (`TestAnthropicMaxTokensIsTheModels`).
diff --git a/internal/gateway/call_args_stream_test.go b/internal/gateway/call_args_stream_test.go
new file mode 100644
index 000000000..e3da10263
--- /dev/null
+++ b/internal/gateway/call_args_stream_test.go
@@ -0,0 +1,217 @@
+package gateway
+
+import (
+ "bufio"
+ "encoding/json"
+ "io"
+ "net/http"
+ "net/http/httptest"
+ "strings"
+ "testing"
+ "time"
+
+ "github.com/yetone/magpie/internal/provider"
+)
+
+// gatedCall is a Responses upstream that streams a call's first argument
+// delta and then waits, before the rest of the call, until the test has
+// seen that delta reach the client (or gives up).
+type gatedCall struct {
+ name string // the function the upstream calls
+ first string // its first argument delta
+ rest string // the rest of its arguments
+ seen chan struct{} // closed by the test once the client has the first delta
+ wrote chan struct{} // closed by the upstream as it goes on to the rest
+}
+
+func (g *gatedCall) ServeHTTP(w http.ResponseWriter, r *http.Request) {
+ io.ReadAll(r.Body)
+ w.Header().Set("Content-Type", "text/event-stream")
+ f := w.(http.Flusher)
+ q := func(s string) string { b, _ := json.Marshal(s); return string(b) }
+ write := func(lines ...string) {
+ io.WriteString(w, sse(lines...))
+ f.Flush()
+ }
+ write(`data: {"type":"response.created","response":{"id":"r1","model":"m"}}`,
+ `data: {"type":"response.output_item.added","output_index":0,"item":{"type":"function_call","id":"fc_1","call_id":"call_1","name":"`+g.name+`","arguments":""}}`,
+ `data: {"type":"response.function_call_arguments.delta","output_index":0,"item_id":"fc_1","delta":`+q(g.first)+`}`)
+ select {
+ case <-g.seen:
+ case <-time.After(20 * time.Second):
+ case <-r.Context().Done():
+ return
+ }
+ close(g.wrote)
+ args := q(g.first + g.rest)
+ write(`data: {"type":"response.function_call_arguments.delta","output_index":0,"item_id":"fc_1","delta":`+q(g.rest)+`}`,
+ `data: {"type":"response.function_call_arguments.done","output_index":0,"item_id":"fc_1","arguments":`+args+`}`,
+ `data: {"type":"response.output_item.done","output_index":0,"item":{"type":"function_call","id":"fc_1","call_id":"call_1","name":"`+g.name+`","arguments":`+args+`,"status":"completed"}}`,
+ `data: {"type":"response.completed","response":{"id":"r1","status":"completed","output":[],"usage":{"input_tokens":5,"output_tokens":5}}}`)
+}
+
+// A call's arguments reach Codex as the upstream writes them, on the
+// translated path its web_search sends a request down (echo_ts on Discord:
+// on Copilot, Codex saw nothing for the minutes the model wrote a patch or
+// a command, then all of it at once). A function's arguments come as
+// function_call_arguments deltas, a custom tool's input (apply_patch) as
+// custom_tool_call_input deltas, each before the upstream's call is done;
+// the finished items are what they were.
+func TestCallArgsStreamWhenTranslated(t *testing.T) {
+ const patch = "*** Begin Patch\n*** Add File: a.go\n+package a\n+// \"quoted\" \\ é 😀\n*** End Patch\n"
+ whole, _ := json.Marshal(map[string]string{"input": patch})
+ split := 24 // inside the patch: {"input":"*** Begin Pat
+ for _, c := range []struct {
+ name, fn string
+ first, rest string
+ delta string // the event the client is given the call's text by
+ wantFirst string // what that event's first delta says
+ wantItemType string
+ }{
+ {"function", "exec_command", `{"cmd":"go test ./...",`, `"yield_time_ms":1000}`,
+ "response.function_call_arguments.delta", `{"cmd":"go test ./...",`, "function_call"},
+ {"custom", "apply_patch", string(whole[:split]), string(whole[split:]),
+ "response.custom_tool_call_input.delta", patch[:split-len(`{"input":"`)], "custom_tool_call"},
+ } {
+ t.Run(c.name, func(t *testing.T) {
+ fresh(t)
+ up := &gatedCall{name: c.fn, first: c.first, rest: c.rest, seen: make(chan struct{}), wrote: make(chan struct{})}
+ vendor := httptest.NewServer(up)
+ t.Cleanup(vendor.Close)
+ if err := provider.Save(provider.Provider{ID: "rel", Name: "Rel", Key: "k", Responses: vendor.URL + "/v1", Models: []string{"m"}}); err != nil {
+ t.Fatal(err)
+ }
+ srv := httptest.NewServer(New().Handler())
+ t.Cleanup(srv.Close)
+ body := `{"model":"rel/m","stream":true,"input":[{"role":"user","content":"add a.go"}],
+ "tools":[{"type":"function","name":"exec_command","description":"Runs a command.","strict":false,
+ "parameters":{"type":"object","properties":{"cmd":{"type":"string"},"yield_time_ms":{"type":"number"}},"required":["cmd"]}},
+ {"type":"custom","name":"apply_patch","description":"Edits files.","format":{"type":"grammar","syntax":"lark","definition":"start: /.+/"}},
+ {"type":"web_search"}],"tool_choice":"auto"}`
+ res, err := http.Post(srv.URL+"/v1/responses", "application/json", strings.NewReader(body))
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer res.Body.Close()
+ if res.StatusCode != 200 {
+ b, _ := io.ReadAll(res.Body)
+ t.Fatalf("%d %s", res.StatusCode, b)
+ }
+ var deltas []string
+ var done map[string]any
+ early := false
+ sc := bufio.NewScanner(res.Body)
+ sc.Buffer(make([]byte, 1<<20), 1<<20)
+ for sc.Scan() {
+ data, ok := strings.CutPrefix(sc.Text(), "data: ")
+ if !ok {
+ continue
+ }
+ var ev struct {
+ Type string `json:"type"`
+ Delta string `json:"delta"`
+ Item map[string]any `json:"item"`
+ }
+ if json.Unmarshal([]byte(data), &ev) != nil {
+ continue
+ }
+ switch {
+ case ev.Type == c.delta:
+ if len(deltas) == 0 {
+ select {
+ case <-up.wrote:
+ default:
+ early = true // the upstream is still holding the rest
+ }
+ close(up.seen)
+ }
+ deltas = append(deltas, ev.Delta)
+ case ev.Type == "response.output_item.done" && ev.Item["type"] == c.wantItemType:
+ done = ev.Item
+ case ev.Type == "response.output_item.done" && done == nil:
+ // another kind of item: what the call came back as
+ done = ev.Item
+ }
+ }
+ if !early {
+ t.Fatalf("no %s before the upstream's call was done: deltas %q", c.delta, deltas)
+ }
+ if deltas[0] != c.wantFirst {
+ t.Errorf("first delta %q, want %q", deltas[0], c.wantFirst)
+ }
+ if done == nil || done["type"] != c.wantItemType {
+ t.Fatalf("finished item %v", done)
+ }
+ if c.wantItemType == "custom_tool_call" {
+ if done["input"] != patch || strings.Join(deltas, "") != patch {
+ t.Errorf("input %q, deltas %q, want %q", done["input"], strings.Join(deltas, ""), patch)
+ }
+ } else if done["arguments"] != c.first+c.rest || strings.Join(deltas, "") != c.first+c.rest {
+ t.Errorf("arguments %q, deltas %q", done["arguments"], strings.Join(deltas, ""))
+ }
+ })
+ }
+}
+
+// A custom tool's input read out of its arguments a piece at a time is
+// the input the whole arguments hold, wherever the pieces are cut: in an
+// escape, between a surrogate pair's halves, inside a character.
+func TestCustomInputStreams(t *testing.T) {
+ patch := "*** Begin Patch\n+\"q\" \\ é 😀 <a>&\t\n*** End Patch\n"
+ marshalled, _ := json.Marshal(map[string]string{"input": patch})
+ for _, c := range []struct{ args, want string }{
+ {string(marshalled), patch},
+ {`{"input":"\ud83d\ude00 \u00e9\u0041\/\n"}`, "😀 éA/\n"},
+ {`{"path":"a.go","input":"x\"y"}`, `x"y`},
+ {`{"meta":{"input":"no","list":["input"]},"input":"yes"}`, "yes"},
+ {`{ "input" : "spaced" }`, "spaced"},
+ {`"a lone string"`, "a lone string"},
+ {`{"input":"lone \ud83d half"}`, "lone \ufffd half"},
+ {`{"input":5}`, ""},
+ {`{"other":"x"}`, ""},
+ {`not json`, ""},
+ } {
+ for _, size := range []int{1, 2, 3, 5, 7, len(c.args)} {
+ var s customStream
+ var got strings.Builder
+ for i := 0; i < len(c.args); i += size {
+ got.WriteString(s.feed(c.args[i:min(i+size, len(c.args))]))
+ }
+ if got.String() != c.want {
+ t.Errorf("%s in pieces of %d: %q, want %q", c.args, size, got.String(), c.want)
+ }
+ if c.want != "" && customInput(c.args) != c.want {
+ t.Errorf("%s: customInput %q", c.args, customInput(c.args))
+ }
+ }
+ }
+}
+
+// A Responses upstream's argument deltas are given as they come, except
+// those that make the arguments whole JSON, which wait for the call's done
+// events: what was given and what follows add up to the fuller arguments.
+func TestResponsesCallArgsStream(t *testing.T) {
+ var d responsesDecoder
+ var got []string
+ emit := func(ev Event) {
+ if ev.Kind == KToolArgs {
+ got = append(got, ev.Text)
+ }
+ }
+ d.decode(`{"type":"response.output_item.added","item":{"type":"function_call","call_id":"c1","name":"f"}}`, emit)
+ for _, x := range []string{`{"cmd":"a}`, `\"]{",`, `"n":[1,{}]`} {
+ q, _ := json.Marshal(x)
+ d.decode(`{"type":"response.function_call_arguments.delta","delta":`+string(q)+`}`, emit)
+ }
+ if want := []string{`{"cmd":"a}`, `\"]{",`, `"n":[1,{}]`}; strings.Join(got, "|") != strings.Join(want, "|") {
+ t.Fatalf("before the end %q, want %q", got, want)
+ }
+ d.decode(`{"type":"response.function_call_arguments.delta","delta":"}"}`, emit)
+ if len(got) != 3 {
+ t.Fatalf("the delta that makes it whole was given before the call's done: %q", got)
+ }
+ d.decode(`{"type":"response.output_item.done","item":{"type":"function_call","call_id":"c1","name":"f","arguments":"{\"cmd\":\"a}\\\"]{\",\"n\":[1,{}],\"more\":true}"}}`, emit)
+ if all := strings.Join(got, ""); all != `{"cmd":"a}\"]{","n":[1,{}],"more":true}` {
+ t.Fatalf("arguments %s", all)
+ }
+}
diff --git a/internal/gateway/codex_ultra_test.go b/internal/gateway/codex_ultra_test.go
index b6e13dbc2..787f57e2f 100644
--- a/internal/gateway/codex_ultra_test.go
+++ b/internal/gateway/codex_ultra_test.go
@@ -177,12 +177,13 @@ func TestCopilotSolOffersUltra(t *testing.T) {
}
}
-// Ultra's subagents with Codex's web_search kept (#656): a request offering
-// the search is made again for Copilot rather than relayed, and on that
-// path a Copilot lead's spawn_agent comes back to Codex unsealed, with the
-// task as written (Copilot not asked to seal it), and the subagent's
-// request — the task as MultiAgentV2's agent_message — reaches Copilot as
-// the task's text.
+// Ultra's subagents with Codex's web_search kept (#656): a lead whose
+// spawn_agent marks its message "encrypted" is made again for Copilot
+// rather than relayed, as Copilot can't open what it seals, and on that
+// path its spawn_agent comes back to Codex unsealed, with the task as
+// written (Copilot not asked to seal it). The subagent's request, which
+// marks nothing, is relayed with the search (Copilot runs web_search
+// itself), the task as MultiAgentV2's agent_message as Codex sent it.
func TestCopilotUltraSubagentWithWebSearch(t *testing.T) {
c := copilotSol(t)
s := New()
@@ -240,10 +241,52 @@ func TestCopilotUltraSubagentWithWebSearch(t *testing.T) {
if code != 200 {
t.Fatalf("subagent: %d %s", code, out)
}
- sent, _ := json.Marshal(c.last(t)["input"])
- if !strings.Contains(string(sent), task) || strings.Contains(string(sent), "encrypted_content") {
+ sub := c.last(t)
+ sent, _ := json.Marshal(sub["input"])
+ if !strings.Contains(string(sent), task) || !strings.Contains(string(sent), `"agent_message"`) || strings.Contains(string(sent), "encrypted_content") {
t.Fatalf("subagent's input to Copilot: %s", sent)
}
+ if offered, _ := json.Marshal(sub["tools"]); string(offered) != `[{"type":"web_search"}]` {
+ t.Fatalf("subagent's tools to Copilot: %s", offered)
+ }
+}
+
+// A Codex turn on Copilot offering web_search is relayed as Codex sent it,
+// as Copilot runs the search itself (echo_ts on Discord: translated for
+// magpie's search, every turn lost the relay); one whose tools mark an
+// argument "encrypted" is made again unmarked, with the search or without,
+// since Copilot seals that argument and can't open it on the next turn.
+func TestCopilotRelayedUnlessAskedToSeal(t *testing.T) {
+ c := copilotSol(t)
+ s := New()
+ spawn := `{"type":"namespace","name":"collaboration","description":"Sub-agents.","tools":[
+ {"type":"function","name":"spawn_agent","strict":false,"description":"Spawns an agent.",
+ "parameters":{"type":"object","properties":{"message":{"type":"string","encrypted":true}},"required":["message"]}}]}`
+ exec := `{"type":"function","name":"exec_command","description":"Runs a command, \"encrypted\" or not.","strict":false,
+ "parameters":{"type":"object","properties":{"cmd":{"type":"string"},"encrypted":{"type":"boolean"}},"required":["cmd"]}}`
+ for _, tc := range []struct {
+ name, tools string
+ relayed bool
+ }{
+ {"search", exec + `,{"type":"web_search"}`, true},
+ {"search and a sealed argument", spawn + `,` + exec + `,{"type":"web_search"}`, false},
+ {"a sealed argument alone", spawn, false},
+ } {
+ code, out := postTo(t, s, "/v1/responses", `{"model":"copilot/gpt-6-luna","stream":true,"input":[{"role":"user","content":"hi"}],"probe":"as sent","tools":[`+tc.tools+`]}`)
+ if code != 200 {
+ t.Fatalf("%s: %d %s", tc.name, code, out)
+ }
+ // a field magpie doesn't know goes on only as Codex sent it
+ body := c.last(t)
+ sent, _ := json.Marshal(body["tools"])
+ relayed := body["probe"] == "as sent"
+ if relayed != tc.relayed {
+ t.Errorf("%s: relayed %v, tools to Copilot %s", tc.name, relayed, sent)
+ }
+ if !tc.relayed && strings.Contains(string(sent), `"encrypted":true`) {
+ t.Errorf("%s: Copilot was asked to seal: %s", tc.name, sent)
+ }
+ }
}
// Ultra is Codex's, no API's: a model whose list says "ultra" (ChatGPT's
diff --git a/internal/gateway/custom_tool.go b/internal/gateway/custom_tool.go
index da54f83a6..715395018 100644
--- a/internal/gateway/custom_tool.go
+++ b/internal/gateway/custom_tool.go
@@ -4,6 +4,8 @@ import (
"bytes"
"encoding/json"
"regexp"
+ "strconv"
+ "unicode/utf8"
)
// Codex offers custom tools on Responses (exec in code mode, apply_patch):
@@ -64,3 +66,144 @@ func customIn(v any) bool {
}
return false
}
+
+// customStream gives a custom tool's input as its call's arguments come,
+// a piece at a time: the text of the input string in {"input":"…"} (or of
+// a lone JSON string), unescaped, as the custom_tool_call_input deltas a
+// client reads while the model writes a patch. What can't be read yet (an
+// escape or a character cut off) waits for the next piece. The finished
+// item's input is still customInput's of the whole arguments; arguments
+// of another shape stream nothing.
+type customStream struct {
+ pend []byte // of the input string, read and not given yet
+ depth int
+ str bool // in a string that isn't the input
+ esc bool // after its backslash
+ key []byte // the depth-1 key being read, raw
+ inKey bool // the string being read is that key
+ want bool // a key is next, at depth 1
+ last string // the last depth-1 key
+ value bool // the depth-1 value next is the input's
+ in bool // in the input string
+ over bool // the input is given, or there is none
+}
+
+// feed reads the next piece of the arguments and gives what it adds to
+// the input.
+func (c *customStream) feed(s string) string {
+ if c.over {
+ return ""
+ }
+ if c.in {
+ c.pend = append(c.pend, s...)
+ return c.take()
+ }
+ for i := 0; i < len(s); i++ {
+ b := s[i]
+ switch {
+ case c.str:
+ if c.inKey {
+ c.key = append(c.key, b)
+ }
+ switch {
+ case c.esc:
+ c.esc = false
+ case b == '\\':
+ c.esc = true
+ case b == '"':
+ c.str = false
+ if c.inKey {
+ c.inKey = false
+ c.last = ""
+ json.Unmarshal(append([]byte{'"'}, c.key...), &c.last)
+ }
+ }
+ case b == '"' && (c.depth == 0 || c.depth == 1 && c.value):
+ c.in = true
+ c.pend = append(c.pend, s[i+1:]...)
+ return c.take()
+ case b == '"':
+ c.str = true
+ if c.depth == 1 && c.want {
+ c.inKey, c.key, c.want = true, c.key[:0], false
+ }
+ case b == '{' || b == '[':
+ c.depth++
+ if c.depth == 1 && b == '{' {
+ c.want = true
+ }
+ case b == '}' || b == ']':
+ if c.depth--; c.depth <= 0 {
+ c.over = true
+ return ""
+ }
+ case b == ':' && c.depth == 1:
+ c.value = c.last == "input"
+ case b == ',' && c.depth == 1:
+ c.want, c.value = true, false
+ case b == ' ' || b == '\t' || b == '\n' || b == '\r':
+ case c.depth == 0:
+ c.over = true // not JSON this reads
+ return ""
+ case c.depth == 1:
+ c.value = false // the input isn't a string
+ }
+ }
+ return ""
+}
+
+// take unescapes what can be read of the input string so far, up to its
+// closing quote.
+func (c *customStream) take() string {
+ n, end := 0, false
+scan:
+ for n < len(c.pend) {
+ switch c.pend[n] {
+ case '"':
+ end = true
+ break scan
+ case '\\':
+ switch {
+ case n+1 >= len(c.pend):
+ break scan
+ case c.pend[n+1] != 'u':
+ n += 2
+ case n+6 > len(c.pend):
+ break scan
+ case !highSurrogate(c.pend[n+2 : n+6]):
+ n += 6
+ case n+8 > len(c.pend):
+ break scan
+ case c.pend[n+6] != '\\' || c.pend[n+7] != 'u':
+ n += 6 // a lone half, read as JSON reads it
+ case n+12 > len(c.pend):
+ break scan // the low half is cut off
+ default:
+ n += 12
+ }
+ default:
+ n++
+ }
+ }
+ // a character cut off waits too
+ for k := 0; k < utf8.UTFMax-1 && n > 0 && !end && !utf8.Valid(c.pend[:n]); k++ {
+ n--
+ }
+ var text string
+ if json.Unmarshal(append(append([]byte{'"'}, c.pend[:n]...), '"'), &text) != nil {
+ c.over = true // not a string JSON reads: the finished item says it
+ return ""
+ }
+ c.pend = c.pend[n:]
+ if end {
+ c.over, c.pend = true, nil
+ }
+ return text
+}
+
+// highSurrogate is whether the four hex digits of a \u escape are the
+// first half of a pair.
+func highSurrogate(hex []byte) bool {
+ r, err := strconv.ParseUint(string(hex), 16, 16)
+ return err == nil && r >= 0xD800 && r < 0xDC00
+}
diff --git a/internal/gateway/gateway.go b/internal/gateway/gateway.go
index b389f5abe..30b879463 100644
--- a/internal/gateway/gateway.go
+++ b/internal/gateway/gateway.go
@@ -3540,6 +3540,14 @@ func (s *Server) attempt(w http.ResponseWriter, r *http.Request, from provider.P
if relay && searchAsked(from, body) && (from == provider.Chat && !p.IsRemoteMagpie() || !searchesModel(p, from, model)) && !(from == provider.Responses && hasSealedAgentMessage(body)) {
relay, ownAPI = false, true
}
+ // Copilot seals an argument the tools mark "encrypted" (spawn_agent's
+ // message, MultiAgentV2) and then can't open what it sealed: the
+ // subagent's turn came back "Encrypted function output content could
+ // not be decrypted or decoded" (2026-10-17, a real account). Translated,
+ // the marks are left out and the call goes back to Codex unsealed.
+ if relay && from == provider.Responses && p.Account != nil && p.Account.Agent == "copilot" && asksSealing(body) {
+ relay, ownAPI = false, true
+ }
// Zen's free models are asked as OpenCode asks them (zenfree.go)
if p.OpenCodeFree(model) {
relay = false
diff --git a/internal/gateway/responses.go b/internal/gateway/responses.go
index 7419263f2..fe494c915 100644
--- a/internal/gateway/responses.go
+++ b/internal/gateway/responses.go
@@ -196,6 +196,62 @@ func unsealed(schema json.RawMessage) json.RawMessage {
return out
}
+// asksSealing is a Responses request whose tools mark a parameter
+// "encrypted" (MultiAgentV2's spawn_agent message): an upstream that
+// honours the mark answers the call with that argument sealed.
+func asksSealing(body []byte) bool {
+ if !bytes.Contains(body, []byte(`"encrypted"`)) {
+ return false
+ }
+ var q struct {
+ Tools []struct {
+ Parameters json.RawMessage `json:"parameters"`
+ Tools []struct {
+ Parameters json.RawMessage `json:"parameters"`
+ } `json:"tools"`
+ } `json:"tools"`
+ }
+ if decodeJSON(body, &q) != nil {
+ return false
+ }
+ var mark func(any) bool
+ mark = func(v any) bool {
+ switch x := v.(type) {
+ case map[string]any:
+ if b, ok := x["encrypted"].(bool); ok && b {
+ return true
+ }
+ for _, c := range x {
+ if mark(c) {
+ return true
+ }
+ }
+ case []any:
+ for _, c := range x {
+ if mark(c) {
+ return true
+ }
+ }
+ }
+ return false
+ }
+ marked := func(schema json.RawMessage) bool {
+ var v any
+ return bytes.Contains(schema, []byte(`"encrypted"`)) && decodeJSON(schema, &v) == nil && mark(v)
+ }
+ for _, t := range q.Tools {
+ if marked(t.Parameters) {
+ return true
+ }
+ for _, nt := range t.Tools {
+ if marked(nt.Parameters) {
+ return true
+ }
+ }
+ }
+ return false
+}
+
func flatName(namespace, name string) string {
return provider.FlatName(namespace, name)
}
@@ -1270,32 +1326,95 @@ func (u Usage) responses() map[string]any {
// responsesDecoder turns a Responses stream into events.
//
-// A function call's arguments are given once the call is done, not as
-// their deltas come: the deltas can leave out what the finished call holds
-// (#613: Codex's spawn_agent came back with arguments {} where the request
-// was translated, though the upstream's finished call had them), and what
-// was sent of a call's arguments can't be taken back.
+// A function call's arguments are given as their deltas come: a model
+// writing a patch or a command for minutes is seen writing it (echo_ts on
+// Discord: Codex on Copilot, translated for its web_search, saw nothing
+// until the call was done). The deltas can leave out what the finished
+// call holds (#613: Codex's spawn_agent came back with arguments {} where
+// the request was translated, though the upstream's finished call had
+// them), and what was sent of a call's arguments can't be taken back. So
+// the deltas that would make the arguments whole JSON wait for the call's
+// done events (jsonWhole), and the call is given what they add to it.
type responsesDecoder struct {
started bool
called bool // a function call was streamed
calling bool // a function call is open
- args strings.Builder // the open call's argument deltas
+ sent strings.Builder // the open call's argument deltas given so far
+ held string // its deltas since they made whole JSON, not given yet
+ whole jsonWhole // where its deltas stand in their JSON
full string // the open call's arguments as its done events give them
}
-// endCall gives the open call's arguments: the deltas, or what its done
-// events give where that holds more.
+// argsDelta gives an open call's argument delta, or holds it when with
+// it the deltas are whole JSON, which the done events may yet replace.
+func (d *responsesDecoder) argsDelta(delta string, emit func(Event)) {
+ if d.whole.feed(delta) {
+ d.held += delta
+ return
+ }
+ text := d.held + delta
+ d.held = ""
+ d.sent.WriteString(text)
+ if text != "" {
+ emit(Event{Kind: KToolArgs, Text: text})
+ }
+}
+
+// endCall gives the rest of the open call's arguments: what was held, or
+// what its done events give past what was sent where they hold more.
func (d *responsesDecoder) endCall(emit func(Event)) {
if !d.calling {
return
}
d.calling = false
- args := pickArgs(d.args.String(), d.full)
- d.args.Reset()
- d.full = ""
- if args != "" {
- emit(Event{Kind: KToolArgs, Text: args})
+ sent := d.sent.String()
+ rest := d.held
+ if args := pickArgs(sent+d.held, d.full); strings.HasPrefix(args, sent) {
+ rest = args[len(sent):]
}
+ d.sent.Reset()
+ d.held, d.full, d.whole = "", "", jsonWhole{}
+ if rest != "" {
+ emit(Event{Kind: KToolArgs, Text: rest})
+ }
+}
+
+// jsonWhole follows a stream of JSON text, a piece at a time, for whether
+// what came so far is a whole object or array.
+type jsonWhole struct {
+ depth int
+ str, esc bool
+ began bool
+}
+
+// feed reads the next piece and says whether the text is whole after it.
+func (j *jsonWhole) feed(s string) bool {
+ for i := 0; i < len(s); i++ {
+ c := s[i]
+ switch {
+ case j.esc:
+ j.esc = false
+ case j.str:
+ if c == '\\' {
+ j.esc = true
+ } else if c == '"' {
+ j.str = false
+ }
+ case c == '"':
+ j.str = true
+ if j.depth == 0 {
+ j.began = false
+ }
+ case c == '{' || c == '[':
+ j.depth++
+ j.began = true
+ case c == '}' || c == ']':
+ j.depth--
+ case c != ' ' && c != '\t' && c != '\n' && c != '\r' && j.depth == 0:
+ j.began = false // a value that isn't one, or after one
+ }
+ }
+ return j.began && j.depth == 0 && !j.str
}
// pickArgs is the fuller of a call's arguments as streamed and as its done
@@ -1369,7 +1488,7 @@ func (d *responsesDecoder) decode(data string, emit func(Event)) error {
emit(Event{Kind: KThink, Text: ev.Delta})
case "response.function_call_arguments.delta":
if d.calling {
- d.args.WriteString(ev.Delta)
+ d.argsDelta(ev.Delta, emit)
} else {
emit(Event{Kind: KToolArgs, Text: ev.Delta})
}
@@ -1502,6 +1621,9 @@ type responsesEncoder struct {
// collector's last part isn't it once a hidden thinking block (signed
// with no text, or redacted) has come after the call (#1786)
call Part
+ // input reads a custom tool's input out of its call's arguments as
+ // they come
+ input customStream
// a message's output_item.done, and the events after it, wait here
// until its phase is known (phase.go, #2437)
heldMsg map[string]any
@@ -1661,10 +1783,15 @@ func (e *responsesEncoder) closeAs(sealed json.RawMessage) {
args = "{}"
}
p := e.call
- if q := e.named[p.Name]; !q.Search && !q.Custom {
+ item = callTo(map[string]any{"id": e.itemID, "type": "function_call", "status": "completed", "call_id": p.ID, "arguments": args}, p.Name, e.named)
+ switch q := e.named[p.Name]; {
+ case q.Custom:
+ // as OpenAI ends a custom tool's input deltas
+ input, _ := item.(map[string]any)["input"].(string)
+ e.send("response.custom_tool_call_input.done", map[string]any{"item_id": e.itemID, "output_index": e.item, "input": input})
+ case !q.Search:
e.send("response.function_call_arguments.done", callTo(map[string]any{"item_id": e.itemID, "output_index": e.item, "call_id": p.ID, "arguments": args}, p.Name, e.named))
}
- item = callTo(map[string]any{"id": e.itemID, "type": "function_call", "status": "completed", "call_id": p.ID, "arguments": args}, p.Name, e.named)
}
e.send("response.output_item.done", map[string]any{"output_index": e.item, "item": item})
e.output = append(e.output, item)
@@ -1759,14 +1886,21 @@ func (e *responsesEncoder) event(ev Event) {
item := callTo(map[string]any{"type": "function_call", "call_id": ev.ID, "arguments": ""}, ev.Name, e.named)
e.openItem(ToolCall, itemPrefix(item), item)
e.call = Part{Kind: ToolCall, ID: ev.ID, Name: ev.Name}
+ e.input = customStream{}
e.col.add(ev)
return
case KToolArgs:
if e.open == ToolCall && ev.Text != "" {
e.text.WriteString(ev.Text)
- // a custom tool's input is whole only once its arguments are:
- // it goes in the item when that is done
- if !e.named[e.call.Name].Custom {
+ switch q := e.named[e.call.Name]; {
+ case q.Custom:
+ // a custom tool's input, as it is read out of the
+ // arguments: what the model writes of a patch is seen as
+ // it writes it, as from OpenAI
+ if in := e.input.feed(ev.Text); in != "" {
+ e.send("response.custom_tool_call_input.delta", map[string]any{"item_id": e.itemID, "output_index": e.item, "delta": in})
+ }
+ default:
e.send("response.function_call_arguments.delta", map[string]any{"item_id": e.itemID, "output_index": e.item, "delta": ev.Text})
}
}
diff --git a/internal/gateway/search.go b/internal/gateway/search.go
index 666646aa5..c5c53389e 100644
--- a/internal/gateway/search.go
+++ b/internal/gateway/search.go
@@ -105,8 +105,14 @@ func searchesItself(p provider.Provider, proto provider.Protocol) bool {
saidTo := p.Searches && (proto == provider.Anthropic || proto == provider.Responses) && p.Base(proto) != ""
if p.Account != nil {
// Grok by its id: moved to its plugin, its account is the plugin's;
- // its plugin beside the built-in (grok-plugin) by the plugin's id
- return (p.Account.Agent == "codex" || p.ID == "grok" || p.PluginProvider() == "grok") && proto == provider.Responses ||
+ // its plugin beside the built-in (grok-plugin) by the plugin's id.
+ // Copilot's /responses runs web_search itself for every model it
+ // serves there: gpt-5.3-codex, gpt-5.4-mini, gpt-6-luna, grok-4.7
+ // and mai-code-1.1-flash each answered a request offering it with
+ // web_search_call items and cited pages (2026-10-17, a real
+ // account). Translated for magpie's search instead, Codex's every
+ // turn on Copilot lost the relay (echo_ts on Discord).
+ return (p.Account.Agent == "codex" || p.Account.Agent == "copilot" || p.ID == "grok" || p.PluginProvider() == "grok") && proto == provider.Responses ||
p.IsPlugin() && saidTo
}
if saidTo {
@@ -242,6 +248,10 @@ func searchRank(p provider.Provider) int {
return 0
case p.Account != nil && p.Account.Agent == "codex":
return 1
+ case p.Account != nil && p.Account.Agent == "copilot":
+ // it searches for its own models only: another's searches would
+ // spend its premium requests, which no one asked for
+ return -1
case searchesItself(p, provider.Anthropic):
return 2
case searchesItself(p, provider.Responses):