Commit Graph
3 Commits
Author SHA1 Message Date
vincentand321sssrt-bit b630304322 gateway: an upstream naming the type an unknown variant custom turns custom tools to functions (#3392, kongkongklng)
xAI's Responses API answers a custom tool with 422 "Failed to deserialize
the JSON body into the target type: tools[N].type: unknown variant `custom`,
expected one of …" — Rust's own wording of a type it doesn't know, which
customToolRefusal didn't match, so the request wasn't offered again with
its custom tools as functions (the #2656 path) and Codex saw the 422.

customToolRefusal now also takes "unknown variant `custom`". The refusal
is tested with the reporter's wording, bare and in an error body, and a
variant that isn't the custom type itself stays no refusal.

Verified: go test ./internal/gateway -run Custom -count=1 (21 tests, green;
the two new wordings red without the fix). gofmt, go vet clean.

This is the #2656 path (321sssrt-bit) meeting a new wording.

From skygunner's branch fix/issue-batch-1024pm; kongkongklng confirmed it on 10-25.

Co-authored-by: 321sssrt-bit <298505031+321sssrt-bit@users.noreply.github.com>
2026-10-28 16:52:55 +08:00
yetone ec7f474323 gateway: Codex on Copilot is relayed with its web_search, and a translated call's arguments stream (echo_ts)
echo_ts (magpie 0.1.1279, macOS): Codex on a GitHub Copilot subscription
through magpie was about 10x slower than Copilot directly. Two causes,
both in the gateway:

- Codex offers web_search on every turn, and magpie counted Copilot as a
  provider that can't search, so every Codex turn on Copilot was
  translated for magpie's search instead of relayed.
- On that translated path responsesDecoder held a call's argument deltas
  until the call was done, so Codex saw nothing while the model wrote a
  command or a patch, then all of it at once. A custom tool (apply_patch)
  got no custom_tool_call_input deltas at all.

Fixes:

- search.go: searchesItself counts a Copilot account on Responses. Real
  check on 2026-10-17 with the owner's account: 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 web_search with web_search_call items and cited
  pages. searchRank never picks Copilot to search for another provider's
  models (its premium requests).
- gateway.go: a Copilot Responses request whose tools mark an argument
  "encrypted" (MultiAgentV2's spawn_agent message) is still translated,
  marks left out (asksSealing in responses.go). Checked on the real
  account: relayed, Copilot seals the message ("UlQC...", no
  encrypted_function_args), and the subagent's turn carrying it came back
  "Encrypted function output content could not be decrypted or
  decoded". This was already broken for a Copilot turn without
  web_search; it is fixed for that too. An unsealed agent_message relayed
  with web_search answers normally (checked).
- responses.go: responsesDecoder gives argument deltas as they come. It
  holds only the deltas that make the arguments whole JSON, until the
  done events, so #613's case (deltas that disagree with done) still
  ends with the fuller arguments. The encoder sends a custom tool's input
  as custom_tool_call_input.delta, read out of the {"input": ...}
  arguments as they stream (customStream in custom_tool.go, which waits
  for cut escapes, surrogate pairs and UTF-8), and closes it with
  custom_tool_call_input.done.
- docs/subsystems/gateway-routing.md says the above.

Not changed: the HTTP/2 connection. Head-of-line blocking from large
uploads on the one connection to Copilot was measured on the real
account with a Go client: a 4MB request and a small one 300ms later,
5 rounds x 2, same connection vs a new one. The small request's first
byte took 0.93-1.68s on the shared connection, 1.26-4.77s on its own
(handshake included), 0.66-1.08s alone. No sign of blocking.

Small requests slower while a long session runs (echo_ts, 42.8s vs
17-29s first text, xhigh): nothing in magpie serializes Copilot requests
by default (lanes need MaxConcurrency, the RPM gate MaxRPM; Go's h2
windows are 1GB per connection, 4MB per stream). Measured: small
requests beside a 412s xhigh stream (16,617 output tokens) were as fast
as idle, via magpie 0.8-1.1s first text, direct 1.3-2.0s, sent_ms
1-10ms. One global lock found: copilotMu is held across the token
refresh's HTTP call, which stalls every Copilot request only around a
refresh, filed as #2762, not changed here. usage.jsonl's own_ms,
queued_ms and sent_ms against ttft_ms tell magpie's time from Copilot's.

Tests:
- TestCallArgsStreamWhenTranslated (function and custom), through the
  real handler with an upstream that holds the rest of the call until
  the client has the first delta.
- TestResponsesCallArgsStream, TestCustomInputStreams (pieces of 1, 2, 3,
  5, 7 bytes and whole).
- TestCopilotRelayedUnlessAskedToSeal; TestCopilotUltraSubagentWithWebSearch
  now expects the subagent relayed with the search.

Broken by hand at this tree, each went red:
- decoder holding every delta: TestCallArgsStreamWhenTranslated,
  TestResponsesCallArgsStream;
- no custom input delta: TestCallArgsStreamWhenTranslated/custom;
- Copilot out of searchesItself: TestCopilotRelayedUnlessAskedToSeal,
  TestCopilotUltraSubagentWithWebSearch;
- the asksSealing check off: the same two.

Real use: a built magpie serve in a sandbox HOME (port 3461) with the
owner's Copilot account, Codex-shaped requests offering exec_command,
apply_patch and web_search to copilot/gpt-5.3-codex:
- unmarked: relayed (Copilot's own response ids), apply_patch gave 93
  custom_tool_call_input deltas from 3.55s to 11.69s;
- with a sealed spawn_agent: translated, apply_patch 117 deltas from
  1.45s to 11.73s, exec_command 92 function_call_arguments deltas from
  1.55s to 8.16s.
Not tried with the Codex binary itself.

Run at d783df46c plus this change (load 160-330 from other sessions): gofmt -l clean; go vet ./... for darwin,
linux (-tags nogui) and windows ok; go test -timeout 45m ./...:
60 packages ok; internal/agent, internal/gateway and internal/library
timed out at 45m (#2544/#2723, #2108, filed #2853), internal/gui's
TestSetAnswersWhileDevinAndProvidersHang (#1375) and TestMainWindowCorners
(#2645) red. Rerun alone: internal/library ok 1184s; the two gui tests
ok; internal/gateway timed out at 45m again (load ~300), then at
-timeout 3h ran 2887s with one red, TestClaudeAccountsSpentFallBackToCodex
"stream=true: no answer" at its 30s wall wait (filed #2858; -count=3
alone ok). The tests above, after the rebase: ok.
2026-10-19 00:33:29 +08:00
yetone 5b12407e8e gateway: a custom tool a Responses upstream turns away is translated, not given back as a 400 (#2656)
DeepSeek's Responses API answers Codex's exec, a custom tool, with 400
"Unsupported custom tool: 'exec'. Only 'apply_patch' is supported."
The relay (attempt -> passthrough) sent the tools as they came, so the
translation that offers a custom tool as a function and gives its call
back as a custom_tool_call never ran, and the 400 matched no word list,
so a group's next member wasn't asked either.

- passthrough: a 400/422 matching customToolRefusal, on a Responses
  body that offers a custom tool (tools, a namespace's, an input item's
  tools), marks the model (customToolRefused) and returns not done.
- attempt: then, and on a marked model's later requests with a custom
  tool, the request is translated on the client's own API (ownAPI), so
  exec keeps its name and comes back as custom_tool_call. A Codex
  compaction (keepsCompaction) isn't translated and gets the refusal.
- shapeRefused also takes customToolRefusal, so where the refusal does
  reach a group, the next member is asked and nobody rests.

Not scoped to DeepSeek: any Responses relay (key, plugin) that says its
custom tools are unsupported is handled the same; an upstream that never
refused them still gets them as sent (tested).

Tests: custom_tool_test.go, fixture from the reporter's literal error
text and request. TestCustomToolTranslatedWhereRefused (stream and
not), TestCustomToolRefusalInGroup (reporter's ordered group, next
member asked 0 times), TestCustomToolRefusalWordings. Broken by hand:
passthrough branch off -> both request tests 400 with the reporter's
exact message; attempt pre-skip off -> next turn asked 4 times, not 3;
shapeRefused addition off -> Wordings red.

Run:
- gofmt -l clean; go vet ./... (darwin), GOOS=linux and GOOS=windows
  go vet -tags nogui ./... clean, at the pushed head.
- Full go test -tags nogui -timeout 45m ./... on e085d6e92 + this
  change: every package ok except internal/agent, timed out at 2706s
  (TestPortMovesEveryAgent running 30s at the deadline; load ~100-150;
  same as #2544, commented there). internal/agent alone -timeout 60m:
  ok 3031s.
- After rebasing onto cb05dc2d1 (3 commits, none in this path):
  internal/gateway -timeout 60m run twice. First: FAIL at 2559s; I
  piped it through tail and lost which test, so it is not named or
  filed here. Second, full log kept: ok 2509s (load ~150).
- Rebased again onto 9f9bfc160 (3 more commits: agent, provider's
  Codex window; none in this path): TestCustomTool*, TestCodexCustom*,
  TestToolStrict*, *Shape* ok; gofmt and vet (darwin, linux, windows)
  clean. Then onto 85eef5d87 (GUI quota refresh only): build and
  TestCustomTool* ok. The full suite was not rerun at this head.
- Not tried against the real DeepSeek API; the fake upstream refuses as
  the reporter's logs show.
2026-10-17 17:44:23 +08:00