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.
181 lines
7.4 KiB
Go
181 lines
7.4 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: another subscription's or a plugin's Claude model (Copilot,
|
|
// Kiro, Cursor…) is that vendor's service, which turns the safeguards
|
|
// away (#1756) or drops them, so it isn't reviewed by its name alone.
|
|
func AutoModeReviewed(p provider.Provider, model string) bool {
|
|
if provider.HostOf(p.Base(provider.Anthropic)) == "api.anthropic.com" {
|
|
return true
|
|
}
|
|
if p.Account != nil {
|
|
return p.Account.Agent == "claude"
|
|
}
|
|
return anthropicModel.MatchString(model)
|
|
}
|
|
|
|
// AutoModeReviewable reports whether Claude Code, asking for model at p
|
|
// through magpie, can have its actions reviewed: AutoModeReviewed, on a
|
|
// route that reaches the Messages API. A Claude model p serves only on
|
|
// Chat or Responses (a relay with no Anthropic endpoint, or one the user
|
|
// set to be asked on another API) is asked translated, and a translation
|
|
// carries neither the safeguards nor their results (nexnull on Discord).
|
|
// The agent's wiring asks this before a request is made; serverReview,
|
|
// on the Messages route already, asks AutoModeReviewed.
|
|
func AutoModeReviewable(p provider.Provider, model string) bool {
|
|
if !AutoModeReviewed(p, model) {
|
|
return false
|
|
}
|
|
if p.Account != nil {
|
|
return true // the Claude subscription, through Claude Code's own binary
|
|
}
|
|
if p.Base(provider.Anthropic) == "" {
|
|
return false
|
|
}
|
|
apis := p.APIs(model)
|
|
return apis == nil || slices.Contains(apis, provider.Anthropic)
|
|
}
|
|
|
|
// 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})
|
|
}
|