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 reason8c21a4184didn'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/main94aa15397(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.
151 lines
6.2 KiB
Go
151 lines
6.2 KiB
Go
package gateway
|
|
|
|
import (
|
|
"bytes"
|
|
"slices"
|
|
"strings"
|
|
|
|
"github.com/tidwall/gjson"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// Claude Code's auto mode checks each action with a classifier (#250).
|
|
// Behind a gateway, its first requests ask the API to do it: the
|
|
// dangerous-tool-use beta and a `safeguards` field, answered by
|
|
// `safeguard_results` in the reply: a whole reply's own, a stream's in its
|
|
// message_delta. Anthropic's own API (and a relay in front of it) gets them
|
|
// as they were sent, relayed or built (buildAnthropic), and the results
|
|
// come back as it gave them, on a stream made of a whole reply too
|
|
// (anthropicWholeEvents). Any other model neither sees them nor answers
|
|
// them (serverReview), and Claude Code, finding no result, classifies
|
|
// locally for the rest of the session, naming the gateway in a notice.
|
|
// magpie says nothing on the API's behalf. Where no model Claude Code can
|
|
// reach through magpie is reviewed (AutoModeReviewed), the agent's wiring
|
|
// turns the review off in Claude Code's settings instead
|
|
// (CLAUDE_CODE_AUTO_MODE_SERVER=0, as Anthropic's page on the notice says to
|
|
// for a gateway that can't run the checks), so it classifies locally from
|
|
// the start and says nothing about the gateway (1EFLOPS on X). Where one
|
|
// is, it is left on: /model can take the same session to that model,
|
|
// which reviews it.
|
|
//
|
|
// Locally, the classifier is a request of its own on the session's model:
|
|
// the transcript in <transcript> blocks, a system prompt asking for
|
|
// <block>yes</block> or <block>no</block>, thinking turned off, and for its
|
|
// first stage 64 tokens to answer in (stopping at </block>). A model that
|
|
// reasons whatever it is told spends those 64 tokens reasoning and answers
|
|
// nothing, which Claude Code reads as no verdict and blocks the action.
|
|
|
|
// serverReviewBeta begins the beta Claude Code asks the review by
|
|
// (dangerous-tool-use-2026-09-03).
|
|
const serverReviewBeta = "dangerous-tool-use-"
|
|
|
|
func isServerReviewBeta(b string) bool { return strings.HasPrefix(b, serverReviewBeta) }
|
|
|
|
// serverReview fits auto mode's review to what a request to an Anthropic
|
|
// endpoint reaches: the betas asked, fitted to p (betas), and the body.
|
|
// Only Claude reviews actions, so a body naming another vendor's model
|
|
// (DeepSeek's, Kimi's, behind their Anthropic APIs) goes without the beta
|
|
// and the safeguards: a vendor that checks the fields it is sent turns the
|
|
// whole request away over them. Neither does a body p is no longer asked
|
|
// the beta for, having turned it away (Bedrock's "Unexpected value(s)"):
|
|
// the safeguards ask for a review that the beta turns on. A body that names
|
|
// no model (the model in Bedrock's path), or goes to Anthropic's own API,
|
|
// is Claude's.
|
|
func (s *Server) serverReview(p provider.Provider, asked []string, body []byte) ([]string, []byte) {
|
|
model := gjson.GetBytes(body, "model").String()
|
|
claude := model == "" || AutoModeReviewed(p, model)
|
|
if !claude {
|
|
asked = slices.DeleteFunc(slices.Clone(asked), isServerReviewBeta)
|
|
}
|
|
betas := s.betas(p, asked)
|
|
if !claude || slices.ContainsFunc(asked, isServerReviewBeta) && !slices.ContainsFunc(betas, isServerReviewBeta) {
|
|
if gjson.GetBytes(body, "safeguards").Exists() {
|
|
body = withoutFields(body, "safeguards")
|
|
}
|
|
}
|
|
return betas, body
|
|
}
|
|
|
|
// AutoModeReviewed reports whether a request for model on p can have its
|
|
// actions reviewed by Anthropic for Claude Code's auto mode: the Claude
|
|
// subscription (the bridge hands the review to Claude Code's own binary),
|
|
// a Claude model (Anthropic's API, a relay in front of it, Bedrock's or
|
|
// Vertex's names for it), or anything on Anthropic's own API. No other
|
|
// vendor runs Anthropic's classifier, and magpie answers none on its
|
|
// behalf.
|
|
func AutoModeReviewed(p provider.Provider, model string) bool {
|
|
return p.Account != nil && p.Account.Agent == "claude" || anthropicModel.MatchString(model) || provider.HostOf(p.Base(provider.Anthropic)) == "api.anthropic.com"
|
|
}
|
|
|
|
// classifierRoom is what a model that reasons is given on top of the
|
|
// classifier's own budget, as Claude Code gives a Claude model that can't
|
|
// turn its thinking off.
|
|
const classifierRoom = 2048
|
|
|
|
// autoModeClassifier reports whether req is Claude Code's auto mode
|
|
// classifier asking about an action.
|
|
func autoModeClassifier(req *Request) bool {
|
|
if len(req.Tools) > 0 || !strings.Contains(req.System, "<block>") && !strings.Contains(req.System, "<severity>") {
|
|
return false
|
|
}
|
|
for _, m := range req.Messages {
|
|
if m.Role != "user" {
|
|
continue
|
|
}
|
|
for _, p := range m.Parts {
|
|
if p.Kind == Text && strings.HasPrefix(strings.TrimSpace(p.Text), "<transcript>") {
|
|
return true
|
|
}
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// hasReply reports whether msgs hold a reply: without one, a request is
|
|
// a one-off ask however many user messages it comes in.
|
|
func hasReply(msgs []Message) bool {
|
|
for _, m := range msgs {
|
|
if m.Role == "assistant" {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// fitAutoModeClassifier asks a model that isn't Claude, for Claude Code's
|
|
// auto mode classifier, to reason least — none when it can go without, its
|
|
// lowest level otherwise, left to the vendor when its levels aren't known —
|
|
// with room for that reasoning and the verdict.
|
|
func fitAutoModeClassifier(p provider.Provider, model string, req *Request) {
|
|
if anthropicModel.MatchString(model) || !autoModeClassifier(req) {
|
|
return
|
|
}
|
|
if req.MaxTokens > 0 && req.MaxTokens < classifierRoom {
|
|
req.MaxTokens += classifierRoom
|
|
}
|
|
if req.Effort == "" {
|
|
if levels := p.Efforts(model); len(levels) > 0 {
|
|
req.Effort = fitEffort("none", levels)
|
|
}
|
|
}
|
|
}
|
|
|
|
// autoModeClassifierBody is fitAutoModeClassifier for a request relayed to
|
|
// an Anthropic endpoint as it was sent: a model that isn't Claude gets the
|
|
// room to reason in. The rest of the body is left alone.
|
|
func autoModeClassifierBody(model string, body []byte) []byte {
|
|
if anthropicModel.MatchString(model) || !bytes.Contains(body, []byte("<transcript>")) || !bytes.Contains(body, []byte("<block>")) && !bytes.Contains(body, []byte("<severity>")) {
|
|
return body
|
|
}
|
|
req, err := parseAnthropic(body)
|
|
if err != nil || !autoModeClassifier(req) {
|
|
return body
|
|
}
|
|
asked := gjson.GetBytes(body, "max_tokens").Int()
|
|
if asked <= 0 || asked >= classifierRoom {
|
|
return body
|
|
}
|
|
return withFields(body, map[string]any{"max_tokens": asked + classifierRoom})
|
|
}
|