Commit Graph
8 Commits
Author SHA1 Message Date
yetone 06324e99e8 automode: say that Claude Code may have no local check behind a gateway
chenxiang0010 saw "Auto mode can't check actions in this session ...
your requests go through 127.0.0.1:3425, which doesn't pass it
through". magpie's comments and the gateway-routing and agent-wiring
pages said that Claude Code, finding no safeguard_results, classifies
locally. Claude Code 2.1.312 to 2.1.316 (read from their bundles) turn
that local classifier off behind a non-Anthropic base URL when
Anthropic's per-account flag tengu_bright_dragonfly is "all"
(PA(), bbt(): auto_mode_side_calls_retired, localClassifierSwitchedOff).
A call with no server verdict then gets "No classifier runs for this
session", and each action the check would judge asks for approval.
2.1.314 adds the text for CLAUDE_CODE_AUTO_MODE_SERVER=0: "Claude Code
no longer has a check of its own to use instead".

So on a route only Anthropic doesn't answer (another vendor's model,
another subscription's Claude, a translated route, a relay that drops
the fields), auto mode has no check at all for such an account. magpie
can't answer one on Anthropic's behalf. The reviewed routes (Anthropic's
API, a relay passing the fields, the Claude subscription bridge) still
carry the beta, safeguards and safeguard_results as before. Comments and
docs only, no change in behaviour.

Not changed: answering 400 naming safeguards on an unreviewed route, as
Claude Code's gateway spec allows. It sets a refused latch for the
session, so a later /model to a reviewed Claude stays unchecked, while
the strip loses nothing.

Run: gofmt -l internal (clean); go vet -tags nogui ./internal/gateway
./internal/agent on mote (ok).
Not tried: a real Claude Code run, since the flag is Anthropic's per
account and the reporter's route is unknown.

Reported-by: chenxiang0010 (Discord)
2026-11-08 10:55:10 +08:00
yetone 137187a401 gateway: Claude Code's auto mode classifier follows a /model switch at once
CyberCaius on Discord: after switching models, the first auto mode
permission check went to the previous model. The model is Claude Code's
choice, not magpie's. Claude Code 2.1.310 ($Xe/BB) names
ANTHROPIC_DEFAULT_SONNET_MODEL for the classifier, not the session's
model. magpie's sonnet tier follows the main model only once Follow runs
(the GUI's state poll), and Claude Code reads env again only on its next
start. So right after /model the classifier named the old main model.

At the gateway, a classifier request (recognised from its body: a
<transcript> and a <block>/<severity> verdict, no tools) on a sonnet tier
that follows a main model settings.json no longer has goes to the tier
the new main model gives it (followAt, keeping the tier's effort). It is
left alone when the user set CLAUDE_CODE_AUTO_MODE_MODEL, when the sonnet
tier is their own, when the base URL isn't magpie's, or when the main
model hasn't moved. WSL distros are read the same way. Only the
classifier is rerouted: a turn naming the old model may be another open
session that is still on it. The haiku, opus and subagent siblings are
#3474.

Run (sandbox HOME; packages at 7edf5b0a3, then rebased on f35c721c4,
where the build, Windows build, Linux vet and the classifier, StandIn,
tier and Follow tests ran again: ok):
- TestAutoModeClassifierFollowsTheSwitchedModel (gateway) and
  TestClaudeClassifierFollowsTheSwitchedModel (agent): pass.
- go test -tags nogui -timeout 45m ./internal/agent ./internal/gateway
  ./internal/cli: ok (agent 787s, gateway 1191s, cli 129s, load ~450).
- gofmt -l clean; go vet, GOOS=linux go vet -tags nogui, GOOS=windows
  go vet on the three packages; go build, GOOS=linux -tags nogui,
  GOOS=windows: ok.

Broken by hand, each red then restored:
- classifierStandIn never rerouting: gateway test red ("went to m1").
- claudeClassifierIn returning "": agent test red at line 56.
- without the own-sonnet-tier check: red at line 84.
- without the CLAUDE_CODE_AUTO_MODE_MODEL check: red at line 67.

Not tried: real Claude Code doing a /model switch against this build. A
full ./... suite didn't run either: another session's full suite held the
machine, with several more queued behind it.
2026-10-24 19:47:12 +08:00
yetone c8901d4723 gateway: auto mode's classifier asks a model of unknown levels for no reasoning
CyberCaius on Discord (#help): in Claude Code's auto mode every
classifier request ended 499 in magpie, on a new-api relay's
gpt6.1-sol (an OpenAI-compatible provider the user added), v0.1.1364.

Cause (likely; not confirmed against the reporter's relay): Claude Code
2.1.310 sends the classifier as a non-stream sideQuery, thinking off or
left out, 64 (or 2112) tokens, and waits 60s an attempt for it (WWt in
the bundle; 120s cap on large prompts). fitAutoModeClassifier asked for
the least reasoning only when magpie knew the model's levels. For a
relay's own name it sent no reasoning_effort / reasoning.effort, so a
reasoning model reasoned at its vendor's default over the whole policy
and transcript. That ran past Claude Code's wait, and Claude Code
closed the request (499).

Change:
- automode.go: with the levels unknown, the classifier is asked for
  effort none, marked as magpie's own ask (Request.OwnEffort).
- gateway.go forwardTranslated: a 400 to that own effort
  (ownEffortRefused) is asked again. If it named the level, it is
  asked at low (offRefused). If it named the field ("Unrecognized
  request argument supplied: reasoning_effort", "Unsupported
  parameter: 'reasoning.effort'"), it is asked without the effort, as
  before. Either is remembered in unfit until restart. Only the
  classifier gets OwnEffort, and no other request changes.
- docs/subsystems/gateway-routing.md: a bullet on the local
  classifier.

Run:
- TestAutoModeClassifierReasonsLeastOnAnUnknownModel covers Chat and
  Responses, non-stream, thinking off/64 and left out/2112. It is red
  on the old code: no effort was sent in any of the four cases.
- TestAutoModeClassifierEffortRefusedGoesWithout covers a named level
  going to low, and the Chat and Responses field refusals going
  without. Each next ask takes one request.
- TestAutoModeClassifierOnlyTheClassifier now expects none for unknown
  levels (the 7a3337dd2 decision, re-derived). It still checks that a
  title ask is untouched.
- gofmt -l is clean. go vet ./... passes on darwin, GOOS=linux
  -tags nogui and GOOS=windows.
- go test -tags nogui -timeout 45m ./internal/gateway/ (sandbox HOME, this
  tree on 9d6436311): ok in 541s.

Broken by hand:
- With the refusal branch off, both field cases go red.
- With the remembered-field check off, the next ask takes 2 requests.

Not tried:
- the reporter's relay or a real reasoning model's timing. The 60s
  wait is read from Claude Code's bundle, and no fake measures time.
- Sibling side queries (agent_namer, hook_prompt,
  subagent_config_classifier, titles) that send thinking off to such a
  model still leave reasoning to the vendor. They aren't recognisable
  as the classifier is, and the main turn shares their shape.
- Responses still drops the classifier's stop sequence. Its verdict
  regex reads <block>no without it.
2026-10-24 17:25:01 +08:00
yetone ccbc727818 agent, gateway: a Claude model magpie can only ask translated no longer keeps auto mode's server review on (nexnull on Discord)
nexnull (Claude Code 2.1.308, magpie v0.1.1318, DeepSeek 4.1 flash) saw
Claude Code's notice that auto mode's classifier "isn't eligible because
your requests go through 127.0.0.1:3425". The wiring leaves
CLAUDE_CODE_AUTO_MODE_SERVER unset while any model Claude Code can reach
through magpie is reviewed (claudeReviewed), and it judged that by
AutoModeReviewed, which counts a Claude model by its name. A relay that
lists claude-* beside DeepSeek with only a Chat (or Responses) URL, or a
Claude model the user pinned to chat, is asked translated: a probe showed
such a request goes to /v1/chat/completions with no safeguards and no
safeguard_results. So the review stayed on and the notice showed.

AutoModeReviewable (gateway/automode.go) is AutoModeReviewed on a route
that reaches the Messages API: the Claude subscription, or a provider
with an Anthropic URL whose APIs for the model (a user pin first) include
Anthropic. claudeReviewed asks it for the provider and each fallback, so
Claude Code and T3 Code get CLAUDE_CODE_AUTO_MODE_SERVER=0 there. It is
still magpie's own stashed value (claude.auto_mode_server): a user's own
value is left alone, and disconnect or restore takes magpie's off.
serverReview, already on the Messages route, keeps AutoModeReviewed.

Chosen on purpose: any reachable model that is really reviewed keeps the
review on. Writing 0 by mistake would lose server review on Claude
silently, while leaving it on by mistake costs the notice, which Enter
dismisses for 24h. A relay with an Anthropic URL (OpenRouter, AiHubMix)
still counts as reviewable, since some pass the field through. No
client mimicry and no prompt or identity text changes.

Pass-through on Anthropic routes (asked in the same task): no defect.
Probes against a fake upstream showed the anthropic-beta (with
dangerous-tool-use-2026-09-03) and anthropic-version forwarded, the
safeguards body byte-identical, a stream:false reply relayed byte for
byte with safeguard_results and an unknown key intact, and the SSE relay
keeping an unknown key, an unknown event type and toolu ids. The probe
file was removed and is not committed.

Full run at 983425662, the same change one commit back:
- gofmt -l: clean. go vet ./..., GOOS=linux go vet -tags nogui ./...,
  GOOS=windows go vet ./...: ok.
- set -o pipefail; go test -timeout 45m ./...: EXIT 0, 68 packages ok,
  no FAIL (internal/agent 386s, internal/gateway 507s), run alone after
  another session's suite ended.
- TestAutoModeReviewableAsksOnMessages and
  TestClaudeReviewedOnlyWhereMessagesReachClaude are red on the old code
  ("cr/claude-sonnet-5: true, want false"; "a relay without an Anthropic
  endpoint: \"\", want 0").

Rebased then onto 61b92529e (#2932 sessions, #2942 budget; none of
this change's files): go vet ./internal/... ok, and go test for
internal/agent, internal/gateway, internal/budget and internal/sessions
ok at the pushed head.

Broken by hand: dropping the Anthropic-URL check made the gateway test
red; ignoring the user's pin (apis != nil || true) made the agent test
red ("Claude set to be asked on chat: \"\", want 0"). Restored after
each break.

Not tried: real Claude Code 2.1.308 against a sandbox gateway with a
real DeepSeek key, so the notice going away in a live auto-mode session
is not seen. No GUI files are touched, so the GUI suite was not run.
2026-10-22 18:19:00 +08:00
yetone 6690caaaec gateway: auto mode's review isn't asked of another subscription's Claude model, and Claude Code's set model is judged where it goes
Claude Code 2.1.302 tells a session behind magpie "this session isn't
eligible because your requests go through 127.0.0.1:3425" and points the
gateway at code.claude.com/docs/en/auto-mode-classifier-billing. That
page asks a gateway to forward the dangerous-tool-use-2026-09-03 beta and
the top-level `safeguards` field, and to return `safeguard_results`
(message_delta.delta in a stream) with tool-use ids unchanged; one that
can't should have clients set CLAUDE_CODE_AUTO_MODE_SERVER=0. magpie
already does this on every route Anthropic answers (Claude subscription
bridge, api.anthropic.com, relays, Bedrock, Vertex) and writes the 0
when no reachable model is reviewed (24350a372). Only Anthropic can
produce safeguard_results, so for any other model the 0 is the remedy;
magpie never answers one on Anthropic's behalf.

Gap fixed here: AutoModeReviewed counted any claude-* name as reviewed,
so a Claude model on another vendor's subscription or a plugin (Copilot,
Kiro, Cursor…) was sent the beta and safeguards (Copilot 400s on them,
#1756) and counted as reviewed by the agent wiring, which then left
CLAUDE_CODE_AUTO_MODE_SERVER unset and the notice showing. Now an
account provider counts only when it is the Claude subscription or sits
on api.anthropic.com; providers with a key keep the name rule. And
claudeReviewed resolves the models Claude Code is set to (group,
provider, fallback) before judging them, as the gateway would, instead
of by name alone.

Tests: TestAutoModeReviewedRoutes (copilot/kiro/plugin cases),
TestAutoModeReviewLeftOffOtherSubscriptions,
TestClaudeReviewedJudgesTheSetModelWhereItGoes. Broken by hand: with
automode.go reverted, the first two go red (copilot/kiro/x-plugin true;
copilot beta+safeguards sent); with claude.go reverted, the third goes
red (v/gpt-x falling back to a/claude-sonnet-5 not reviewed).

Run at 2042123f4 + this change, sandbox HOME, load ~350:
- gofmt -l: clean. go vet ./... (darwin), GOOS=linux and GOOS=windows
  go vet -tags nogui ./...: clean.
- go test -timeout 45m ./...: 63 ok, 3 red, all filed and none in code
  this touches: internal/agent hit 45m (#2723, #2544); gateway's
  TestPluginStreamLargeReplies/JSON_fast hit its client timeout (#2551);
  gui's TestMainWindowCorners was killed at its deadline (#1936, #2645).
- Reruns alone: internal/agent -timeout 90m ok (2637s);
  TestPluginStreamLargeReplies ok; TestMainWindowCorners/macOS_26_SDK
  killed at 40s again at load ~200 (#1936).
After rebasing onto origin/main c4aeaec74 (6 commits, none in these
files): go vet of gateway and agent clean; TestAutoMode*,
TestSafeguards*, TestClaudeBridgeSafeguard* ok; the agent review tests
printed one bare FAIL whose output was cut off by tail, then passed
alone and -count=3 -v (all PASS); cause of that one FAIL not known.
Not run: a real Copilot/Kiro account through Claude Code (the agent test
covers the resolution with key providers; the account rule is covered
in the gateway unit tests), the GUI node suite (no GUI change).
2026-10-17 22:43:54 +08:00
yetone 24350a3722 agent, gateway: Claude Code on a model Anthropic doesn't review gets auto mode's server review turned off, so it no longer says magpie isn't eligible (1EFLOPS on X)
1EFLOPS runs Claude Code through magpie on Codex's gpt-6.1-sol. In auto
mode, Claude Code 2.1.296 says "this session isn't eligible because your
requests go through 127.0.0.1:3425 ... ask your gateway to implement
https://code.claude.com/docs/en/auto-mode-classifier-billing".

What the page asks of a gateway: pass the anthropic-beta header
(dangerous-tool-use-2026-09-03), the request's `safeguards` field, and
`safeguard_results` in the reply through unchanged, and leave tool-use ids
alone. The results are verdicts from Anthropic's own safety classifier.
For a gateway that can't provide them, the page's remedy is
CLAUDE_CODE_AUTO_MODE_SERVER=0. Pro, Max and Team plans never see the
notice. In the bundle, Claude Code falls back on server_unsupported,
server_unavailable_disabled or server_no_result, and the notice shows
whenever there's no real verdict. Only that variable removes it.

Per route:
- Claude subscription bridge, Anthropic API key, relays in front of
  Anthropic, Bedrock and Vertex: already pass the beta, `safeguards` and
  `safeguard_results` through (e8cd4bac8, 8c21a4184). This commit doesn't
  change them. serverReview now uses the shared AutoModeReviewed rule
  (adding the subscription account, which never reaches serverReview).
- Codex/OpenAI and every other non-Claude model: magpie can't answer
  honestly. A verdict would be made up, and "unsupported" still shows the
  notice. So magpie answers nothing on Anthropic's behalf. Instead the
  Claude Code wiring writes env.CLAUDE_CODE_AUTO_MODE_SERVER="0" to
  settings.json. It does this only while Claude Code is routed through
  magpie and no model it can reach there is reviewed. That means checking
  the models shown to Claude Code (and Claude Desktop's while Desktop is on
  magpie), a shown group's members, those providers' fallbacks, and the
  models it is set to (claudeReviewed). If any one is reviewed, the
  variable stays unset, because /model can move the session onto it (the
  reason 8c21a4184 didn't write it at all). The value is rewritten on every
  model set and Sync, so adding a Claude provider turns the review back on.
  It is stash-tracked (claude.auto_mode_server): a user's own value is left
  alone, and magpie's goes on Disconnect, reset or a move off magpie.
- T3 Code (sibling): its magpie instance runs Claude Code on the gateway
  through its own environment, so it gets the same "0" by the models it
  lists.
Not covered: Claude Desktop's Code tab while Claude Code itself isn't
routed through magpie. Desktop users are on Pro/Max/Team, which never see
the notice.

Tests (each fails with its part of the change broken by hand):
- TestClaudeAutoModeReview: off on a non-Claude model; on with a Claude
  model shown, in a group or as a fallback; the user's "1" and "0" kept;
  dropped on reset and on a move to Claude Code's own model.
  A group that keeps its models out of the lists (#1783, Unlists) still
  routes to them, so its Claude member counts: members come from what is
  served, not from the catalog.
- TestT3CodeAutoModeReview.
- TestAutoModeReviewedRoutes: subscription, api.anthropic.com, relay,
  Bedrock and Vertex spellings yes; Codex, DeepSeek's Anthropic API,
  openai/gpt-6 and claudette no.
Mutations that went red: skipping group members, skipping fallbacks,
skipping an unlisting group, overwriting the user's value, not dropping it off magpie, always writing
it, the T3 env always or never, and the subscription not counted.

Real Claude Code 2.1.296, sandbox HOME/CLAUDE_CONFIG_DIR, magpie serve on
127.0.0.1:3591 under a scratch HOME, fake upstreams on 3593 (OpenAI Chat)
and 3594 (Anthropic):
- Before, on fakeai/fake-gpt, -p --permission-mode auto: the notice is on
  stderr (2 of 2 fresh config dirs). With the env in settings: none.
- With this build, `magpie claude fakeai/fake-gpt` wrote the "0", and a
  fresh run shows no notice. The same settings without it show the notice.
- `magpie provider add fakeanth anthropic=... models=claude-sonnet-5`:
  Sync took the "0" out. `magpie claude fakeanth/claude-sonnet-5` and a run
  in auto mode: the upstream got the dangerous-tool-use beta and
  `safeguards` on both turns, answered not_flagged in message_delta, and
  there was no notice.
Not tried: the Claude subscription bridge with a real account (its plans
don't see the notice), and a real Codex account (a fake Chat upstream
stood in for it).

Run: gofmt -l; go vet -tags nogui ./... for darwin, linux and windows;
go test -tags nogui ./... in a scratch HOME with the Go caches pinned.
The only red was TestOTelWaterfallIncludesUnbilledFallback, whose first
export was cut before the root span under load. That is #1794, filed
earlier today; this run is noted there. This change doesn't touch OTel.
internal/agent was timed against the unchanged base under the same load
and ran at the same pace.

Re-run after rebasing on origin/main 94aa15397 (load ~100, so not the
whole suite): gofmt -l on the touched files; go vet -tags nogui
./internal/agent ./internal/gateway for darwin, linux and windows;
go test -tags nogui -count=1 ./internal/agent (1381s) and
./internal/gateway (939s), green. Breaking writeReview to never write the
"0" turned TestClaudeAutoModeReview red, and dropping the subscription from
AutoModeReviewed turned TestAutoModeReviewedRoutes red.
2026-10-12 22:25:35 +08:00
yetone 8c21a41846 gateway: auto mode's server review reaches Claude alone, and its verdict comes back on every route (lc on Discord)
lc saw Claude Code's notice that the session isn't eligible for the free
server-side auto mode classifier "because your requests go through
127.0.0.1:3425". Claude Code 2.1.295 sends the dangerous-tool-use-2026-09-03
beta and a `safeguards` field, and reads `safeguard_results` from every reply
(a stream's message_delta, a whole reply's top level). A reply without them
latches the session to the local classifier and shows the notice.

What changed:
- serverReview (automode.go), called from forwardOnce, so passthrough,
  count_tokens and the translated path alike: a body naming a model that
  isn't Claude (DeepSeek's, Kimi's Anthropic APIs) goes without the beta and
  without safeguards. AiHubMix answers them with 400 "safeguards: Extra
  inputs are not permitted"; DeepSeek ignores them. A provider that turned the
  beta away (remembered by s.betas, e.g. Bedrock's "Unexpected value(s)")
  gets no safeguards either. Claude, a body with no model (Bedrock's path)
  and api.anthropic.com keep both as sent.
- anthropicWholeEvents: a whole reply streamed after keepalives put
  safeguard_results in message_start, where Claude Code doesn't read it. It
  now goes in message_delta.delta.
- buildAnthropic sends safeguards to a claude-* model; decodeAnthropic
  carries delta.safeguard_results to the encoder, which already wrote them.
  Tool ids are left as they were on every path.
- magpie does not write CLAUDE_CODE_AUTO_MODE_SERVER=0 for other vendors'
  models: /model can move the same session to a Claude model behind the
  gateway, and the setting would hide its eligibility there. Said in
  automode.go's header comment. How magpie presents itself is unchanged.

Tests use Claude Code 2.1.295's captured beta header, an object
classifier_context and the status.available.tool_uses verdict shape.
TestAutoModeReviewOnlyForClaude, TestAutoModeReviewGoesWithItsBeta,
TestAutoModeReviewOfAWholeReplyStreamed and TestAutoModeReviewTranslated
each fail with the source changes reverted (400 from the vendor, verdict in
message_start, safeguards dropped to Claude / verdict not given back).

Run: gofmt -l; go vet, GOOS=linux and GOOS=windows go vet -tags nogui
./internal/gateway; go test -tags nogui ./internal/gateway (ok, 191s)
under a scratchpad HOME. End to end: real Claude Code 2.1.295 -p
--permission-mode auto under a sandbox CLAUDE_CONFIG_DIR, through this
build's gateway on 127.0.0.1:3563, to a fake Anthropic API answering with
safeguard_results: on ant/claude-sonnet-5 the beta and safeguards reached
the fake, Bash ran on the server verdict and no notice was shown; on
ds/deepseek-flash neither reached the fake and Claude Code classified
locally with the notice, as designed. Not run against Anthropic's real API:
no first-party Anthropic key was available.
2026-10-10 03:56:01 +08:00
yetone 7a3337dd25 gateway: Claude Code's auto mode classifier gets an answer through magpie on models that reason, and never lingers as a kept Claude Code run (#250 netfishx: the classifier often fails; Claude Code notes the session "isn't eligible" for free classifier requests through 127.0.0.1:3425) — per code.claude.com/docs/en/auto-mode-classifier-billing and Claude Code 2.1.285, behind a gateway its first requests ask the API to classify (the dangerous-tool-use-2026-09-03 beta, a safeguards field, safeguard_results in the reply); keyed Anthropic already relays them as sent and returns the reply as it came (now guarded by a test), and no other model answers them, so Claude Code falls back to classifying locally: a request of its own on the session's model, the transcript in <transcript> blocks, a system prompt asking <block>yes|no</block>, thinking off, 64 tokens for the first stage stopping at </block>, 60s. A model that reasons whatever it is told (OpenAI Responses/Chat, DeepSeek, a vendor's model behind an Anthropic API) spent the 64 tokens reasoning and answered nothing, which Claude Code reads as no verdict and blocks. Now (automode.go) such a request to a model that isn't Claude gets 2048 tokens more when it asked for less, as Claude Code gives a Claude model that can't turn thinking off, and the least reasoning its levels allow (fitEffort "none"), left to the vendor when they aren't known; a Claude model's request goes byte for byte. Through the Claude subscription the classifier, whose transcript spans several user messages, was kept as an idle claude run (the one-off test was fewer than two messages), pushing out the conversations' own: a request with no tools and no reply is one-off now, and the classifier runs at --effort low. magpie never answers safeguard_results itself. Tested in automode_test.go against httptest fakes and a fake claude: Responses and Chat (budget raised, effort none/low), unknown levels (room given, effort left, a title ask untouched), a vendor on an Anthropic endpoint, the bridge (no idle runs, --effort low) fail on main; TestAutoModeServerSideReviewRelayed keeps the beta, safeguards, safeguard_results and tool-use ids as they are. go vet ./internal/..., go build -tags nogui (and GOOS=linux, GOOS=windows) and go test -tags nogui ./... pass. Not tried in a real auto-mode session nor with real vendors taking effort none. 2026-09-30 09:36:27 +08:00