Files
magpie/internal/gateway/automode.go
T
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

99 lines
3.5 KiB
Go

package gateway
import (
"bytes"
"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. Anthropic's own API (and a relay in front
// of it) gets them through passthrough as they were sent and hands the
// reply back as it came. Any other model neither sees them nor answers
// them, and Claude Code, finding no result in a whole reply, classifies
// locally for the rest of the session. magpie says nothing on the API's
// behalf.
//
// 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.
// 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})
}