Commit Graph
3 Commits
Author SHA1 Message Date
yetone db9a5ec7c4 gateway: Claude Code's advisor runs in magpie for a model whose upstream can't (Awad_Yoo on X)
Claude Code 2.1.316 sends its advisor (/advisor, settings.json's
advisorModel) for any model when CLAUDE_CODE_ENABLE_EXPERIMENTAL_ADVISOR_TOOL
is set: an advisor_20260301 server tool, which only Anthropic's server
runs. Until now magpie left it out for every other upstream (b6fae8efd).

Now, when the advisor model is one magpie serves and the upstream can't
run it (not a Claude model on Anthropic's API with a same-provider Claude
advisor, which keeps the native passthrough), magpie runs it:
- the model is offered an "advisor" function tool with no parameters;
- a call is answered by asking the advisor model, through magpie's own
  routing, with the conversation so far and a short ask after it (up to 3
  calls a reply, then max_uses_exceeded, as Anthropic answers);
- the client gets the server_tool_use and advisor_tool_result blocks
  Anthropic writes (advisor_tool_result_error with its error_code on a
  failure), and the history Claude Code echoes back is read as a call and
  its result.
Claude Code's system prompt, identity text and its own advisor
instructions go upstream as it wrote them; nothing about the advisor is
added to the prompt beside the tool's neutral description.

Claude Code's agent row gets an Advisor line under Tiers, after Auto
mode, default Off: picking a model writes advisorModel and the env switch
(only when the user hasn't set it themselves); Off removes both, and so
does disconnecting. CLI: magpie claude advisor <model|default>.

Run (on the Linux box, xiaomi-gpu magpie-desktop, at this head; mote was
over its load the whole time and mica's disk is full):
- go vet ./internal/gateway ./internal/agent, and with GOOS
  linux/windows/darwin (CGO_ENABLED=0); gofmt -l clean
- go test ./internal/gateway -run 'Advisor|Advised|Search|Anthropic': ok
- go test ./internal/agent -run Claude: ok
- node --test claude-advisor, claude-auto-mode, i18n-keys,
  i18n-en-no-cjk (Chromium + WebKit, en and zh, 1100 and 560px): 24/24
- TestCoreScriptBudget measured by its own rule: app.js 1,084,341 of
  1,086,267, core 1,267,726 of 1,267,762 (no raise)
- Real Claude Code 2.1.316 in a sandbox HOME/CLAUDE_CONFIG_DIR against a
  sandbox `magpie serve` (darwin nogui build of this tree) with only the
  DeepSeek provider: `magpie claude advisor deepseek/deepseek-v4-pro`
  wrote advisorModel + the env; `claude -p --model deepseek/deepseek-flash`
  asked to consult the advisor got text, server_tool_use (srvtoolu_…),
  advisor_tool_result with deepseek-v4-pro's advice (usage row: v4-pro
  11,205 in / 221 out), then its answer quoting it. A --resume turn sent
  that history back and completed.

Broken by hand:
- advisedHere returning false: TestAdvisorServedForAVendorModel,
  TestAdvisorHistoryGoesBackAsACall, TestAdvisorFailureIsAnErrorCode,
  TestAdvisedHere and TestAdvisorModelAsTheUpstreamNamesIt red.
- the env write left out of the advisor field's Set:
  TestClaudeAdvisorModel red.

Not tried: the interactive TUI's /advisor picker (only -p and the
settings.json route); an advisor on a Chat-only or Responses upstream
with a real account (the fake upstream in the tests is Chat); an advisor
call in the same round as a client tool call, which is dropped as a
search call is. Seen on the way, not changed: in the fresh sandbox,
Claude Code's own "sonnet[1m]" default resolved to sonnet-4-5-20250929
rather than ANTHROPIC_DEFAULT_SONNET_MODEL without --model.
2026-11-13 15:31:06 +08:00
yetone 39e8ced8f6 gateway: a translated ask read with its marks is finished like any parse (#4571)
parseTranslated (the commit before, #4571) reads an Anthropic ask to an
Anthropic upstream with parseAnthropicRequest(body, true), which skipped
what parseRequest does after its reader: unsignCalls, LastIsTurn and the
required-tool-choice check. A call id carrying a Gemini thought
signature, from a turn a Gemini member of a group answered, went to the
upstream signature and all whenever the ask carried cache_control. Both
parses now end in afterParse.

Also from review: translate's doc comment had been left above
cacheControlField, and ir.go still said clientCache is only the Claude
bridge's opt-in.

TestTranslatedWithMarksTakesSignaturesOffCallIDs is red with
afterParse's call taken out of parseTranslated (the id comes back
signed) and green with it.

Run for this batch (f600fb982..this commit), all on mote:
- Full suite (go test -tags nogui -count=1 -timeout 45m ./...) at
  6a900e0d0 plus these six commits: 81 packages ok, two red, both
  already filed on main and in packages this batch doesn't touch:
  internal/cli TestLibraryMCPHub (#4140, npm still writing into the
  test's HOME at TempDir cleanup) and internal/gui
  TestSettingsSaveKeepsWhatItDoesNotSend (#4514, SearchAll).
- After rebasing onto 0fe9f97c1: go vet -tags nogui ./... ok;
  internal/gateway, internal/agent, internal/library and
  internal/provider ok.
- gofmt -l clean. GOOS=linux (nogui) and GOOS=windows vet clean.

- Rebased again onto 80af3b86a (main's 20482d71f also changes
  skills.go and skillnew.go; the merge was clean and #4572's hunks are
  unchanged): gofmt -l clean, go vet -tags nogui ./internal/... ok,
  internal/library and internal/gateway ok at this head.

- Rebased onto 0565c0bba (main's e40675cc1 also changes gateway.go;
  the hunks merged clean): gofmt and go vet ./internal/... ok; gateway
  -run Translated|Cache|Effort|Gemini|Account|Middleware|Browser|
  Thinking|Plugin, agent -run OpenCode|Codex|Sync|Image|Group, and all
  of internal/library ok. TestBrowserExtensionOnlyFromItsOrigin is red
  on 0565c0bba alone, 3 of 3 (#4631), and was skipped.
2026-11-01 02:45:01 +08:00
ggbdpq 81245e6cf6 gateway: a translated Messages ask keeps the cache marks its client put on it (#4571)
A plugin account only streams, so a client's whole-body Anthropic
Messages ask is translated: parsed into the request, built again for the
upstream, asked streamed and its answer assembled into one Message JSON.
The parse read none of the cache_control the client had put on its
blocks, and the build set its own marks — the system and the last block
of the last message, five minutes long. A client that marked the end of
its history, as Claude Code's auto mode classifier does, found that
boundary gone from the rebuilt body: the next turn, its history grown by
a block or two, matched only the fixed prefix up to the system block and
wrote the whole transcript again, and every write went out at five
minutes where the client had asked for an hour. The same ask with
stream:true is relayed as it came and shows none of it.

A translation of Anthropic's own now reads the request with its marks
(#3835's path) and the rebuild keeps them: a block the client marked is
marked again, the tail and the system take the client's ttl, and none of
magpie's own are added, so the mark that moves on with the history
stands at the same block the next turn. A mark on thinking, which
Anthropic does not mark, and one on a block the rebuild drops, are left
out. A set that fails the checks any Messages request must pass goes the
default way, its marks dropped as before, not refused with a 400 the old
translation never gave.

Run (macOS):
- Red before, green after: TestPluginWholeAskedKeepsTheClientCacheMarks
  (the boundary unmarked and both marks ttl-less on main, the client's
  blocks otherwise kept 1:1), TestPluginWholeAskedServesBadMarksAsBefore
  green before and after.
- go test -tags nogui -count=1 ./internal/gateway: ok.

Issue #4571

Generated-by: GLM-5.3-Flash (ZCode)

Broken by hand: with the anthropic.go hunks reverted,
TestPluginWholeAskedKeepsTheClientCacheMarks is red: the boundary is
unmarked, and the tail and system are ttl-less. Matches devin plugin
0.1.29 (30cd39f19, plugins#566), which carries the client's
cache_control into its prompt cache options: the marks now reach the
plugin. The next commit fixes this one's parse skipping unsignCalls.
2026-11-01 02:45:01 +08:00