Commit Graph
2 Commits
Author SHA1 Message Date
bfb0d92ca8 gateway: a ToolSearch answer's definition goes to DeepSeek as the reference it takes (#5404)
Newer Claude Code builds put the loaded tool's definition itself in the
ToolSearch answer's tool_result.content[] ({"type":"tool_definition",
"definition":{...}}, the same block shape Claude Code's own bundle
handles beside tool_reference). DeepSeek's Anthropic endpoint refuses
it there: 422 "messages[1].content: unknown variant `tool_definition`,
expected `tool_reference`". withoutDeferredTools walked the messages'
top-level blocks only, so the nested definition survived every learned
retry, and a body with no top-level deferred block never learned the
refusal at all.

hasDeferredTools now sees a tool_definition in a tool_result's content,
and withoutDeferredTools hoists each such definition onto the request's
tools (once; one already listed wins) and sends the block as
{"type":"tool_reference","tool_name":...}, which the endpoint takes only
once the tool is declared. A result whose content isn't an array, or a
definition without a name, goes as it came. DeepSeek's own host
(isDeepSeekAnthropic, by host, not preset) now gets the deferred blocks
stripped up front like Kimi (#2042), since it takes a tool_addition at
no position (erlinerd's live check of 2026-11-05).

Review change on top of ggbdpq's patch: added
TestDeepSeekAnthropicIsStrippedUpFront, which drives a provider at
api.deepseek.com (dial redirected to httptest) through the server; the
patch had no test for the forwardOnce wiring.

Run (mote, -tags nogui, -count=1):
- TestToolResultDefinitionRefusedOnceBecomesReference,
  TestNestedToolResultDefinitionBecomesReference,
  TestDeepSeekAnthropicRecognized, TestDeepSeekAnthropicIsStrippedUpFront:
  PASS.
- -run 'Deferred|Kimi|DeepSeek|ToolSearch|ToolReference|RefsOnly|OpenCode|Shape'
  ./internal/gateway: ok 18.3s.
- gofmt -l clean; GOOS=linux go vet -tags nogui and GOOS=windows go vet
  of ./internal/gateway: clean.

Broken by hand:
- anthropic_shapes.go at the parent: the refused-once test 422s to the
  client, the unit test fails "a nested tool_definition was not seen as
  a deferred tool", the up-front test 422s.
- gateway.go at the parent: TestDeepSeekAnthropicIsStrippedUpFront fails
  "DeepSeek was asked 2 times, want once".

Not tried: a live api.deepseek.com request (no DeepSeek key here); the
endpoint's answers are the reporter's live checks. Not run: the whole
gateway package or ./... suite.

#5404, erlinerd.

Co-authored-by: ggbdpq <ggbdpq@gmail.com>
Co-authored-by: erlinerd <10431861+erlinerd@users.noreply.github.com>
2026-11-06 09:55:19 +08:00
yetoneandlu123bing c9af654b26 gateway: Kimi Code takes a Claude Code session that loaded a deferred tool, and DeepSeek one that dropped a tool (#2042, davisjiahao)
Claude Code's deferred tools (ToolSearch, MCP tools that come and go) put
blocks of their own in a message: tool_addition (its tool a
tool_reference naming the tool, or a tool_definition), tool_removal, and
tool_reference. magpie relays an Anthropic body as it is, so they reached
vendors' Anthropic endpoints.

Checked against the real endpoints on 2026-10-14 (a Kimi Code CN key, a
DeepSeek key; shapes read from Claude Code 2.1.296's own builder):
- Kimi Code refuses any of them, in any role and either tool shape, with a
  whole-request 400 "Invalid request Error" that names nothing; a
  tool_reference inside a tool_result passes.
- DeepSeek takes a tool_addition naming a tool, but refuses one defining
  it, a tool_removal and a message's tool_reference: 422 "messages[1].
  content: unknown variant `tool_removal`, expected one of ...".

What changed:
- withoutDeferredTools (anthropic_shapes.go) drops those message blocks
  and a message they leave empty. The tools stay in the request's tools,
  and ToolSearch's answer (a tool_reference inside a tool_result) stays.
- Kimi (isKimi) is never sent them: its 400 says nothing to learn from.
- Any provider that refuses one in DeepSeek's words is marked
  (shapeDeferred, refuseShapes) and asked again at once without them, and
  not sent them again while magpie runs. The shape-retry loop in forward
  now reads a 422 as well as a 400 (refuseShapes and refuseBetas only act
  on wording they recognise).
- Anthropic and every provider that takes them get them as Claude Code
  wrote them. Kimi's bare "Invalid request Error" is not added to
  shapeWords: it names no field, and with the blocks gone there is nothing
  left for it to cover.
- docs/subsystems/gateway-routing.md: a paragraph on the above.

Builds on lu123bing's fork branch (c4a0564), widened from Kimi to the
learned refusal after the DeepSeek check; this keeps a tool_addition
naming a tool for providers that take it.

Run:
- TestKimiIsSentNoDeferredToolBlocks (the reporter's literal body plus
  Claude Code's real shapes, through Handler) and
  TestDeferredToolBlocksRefusedOnceLeftOut (DeepSeek's literal 422,
  learned once; a provider that takes them keeps getting them).
- Break checks, each red: without the Kimi drop, 5 subtests "status 400:
  Kimi Code: Invalid request Error"; with the loop on 400 only, and
  without the mark, "status 422: DeepSeek: ... unknown variant
  `tool_removal`". Restored: green.
- Real use: a nogui build served from a sandbox HOME on 127.0.0.1:35211
  with copies of the Kimi Code CN and DeepSeek providers. The reporter's
  body to kimi-for-coding now reaches Kimi's account check (403, the test
  key's plan has lapsed) instead of the 400; a tool_removal to DeepSeek
  reaches its balance check (402) instead of the 422. A full successful
  Kimi turn wasn't possible on this key.
- gofmt -l clean; go vet, GOOS=linux go vet -tags nogui, GOOS=windows
  go vet on internal/gateway; go test -tags nogui ./internal/gateway/
  in a sandbox HOME: ok (1516s). An earlier run of the same suite, with
  five other gateway.test binaries running on the machine, ended FAIL
  after 1484s; its output was cut to the last 30 lines, which held no
  test name, so the failing test is not known.

Co-authored-by: lu123bing <12312702@mail.sustech.edu.cn>
2026-10-14 08:26:38 +08:00