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.
This commit is contained in:
@@ -471,6 +471,10 @@ func buildAnthropic(r *Request, model string) []byte {
|
||||
if len(r.Metadata) > 0 {
|
||||
out["metadata"] = r.Metadata
|
||||
}
|
||||
if len(r.Safeguards) > 0 && anthropicModel.MatchString(model) {
|
||||
// auto mode's review, which Claude alone does (automode.go)
|
||||
out["safeguards"] = r.Safeguards
|
||||
}
|
||||
if len(r.Tools) > 0 || r.WebSearch {
|
||||
var tools []map[string]any
|
||||
for _, t := range r.Tools {
|
||||
@@ -591,6 +595,8 @@ func decodeAnthropic(data string, emit func(Event)) error {
|
||||
Thinking string `json:"thinking"`
|
||||
Signature string `json:"signature"`
|
||||
StopReason string `json:"stop_reason"`
|
||||
// auto mode's review of the reply's calls (automode.go)
|
||||
SafeguardResults json.RawMessage `json:"safeguard_results"`
|
||||
} `json:"delta"`
|
||||
Usage aUsage `json:"usage"`
|
||||
Error struct {
|
||||
@@ -628,7 +634,11 @@ func decodeAnthropic(data string, emit func(Event)) error {
|
||||
if ev.Delta.StopReason != "" {
|
||||
emit(Event{Kind: KStop, Stop: stopFromAnthropic(ev.Delta.StopReason)})
|
||||
}
|
||||
emit(Event{Kind: KUsage, Usage: ev.Usage.usage()})
|
||||
var review json.RawMessage
|
||||
if r := ev.Delta.SafeguardResults; len(r) > 0 && string(r) != "null" {
|
||||
review = r
|
||||
}
|
||||
emit(Event{Kind: KUsage, Usage: ev.Usage.usage(), SafeguardResults: review})
|
||||
case "error":
|
||||
emit(Event{Kind: KError, Text: ev.Error.Message, Code: refusedCode(data)})
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package gateway
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"slices"
|
||||
"strings"
|
||||
|
||||
"github.com/tidwall/gjson"
|
||||
@@ -12,12 +13,16 @@ import (
|
||||
// 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.
|
||||
// `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, and doesn't turn the review off
|
||||
// in Claude Code's settings: /model can take the same session to a Claude
|
||||
// model behind the gateway, 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
|
||||
@@ -26,6 +31,37 @@ import (
|
||||
// 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 == "" || anthropicModel.MatchString(model) || provider.HostOf(p.Base(provider.Anthropic)) == "api.anthropic.com"
|
||||
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
|
||||
}
|
||||
|
||||
// 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.
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"runtime"
|
||||
"slices"
|
||||
"strings"
|
||||
@@ -150,19 +151,69 @@ func TestAutoModeClassifierAnthropicVendor(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// Claude Code 2.1.295's ask for the review, as it sent it in auto mode
|
||||
// (paths shortened): the beta beside its others, and the safeguards with
|
||||
// their classifier_context, an object.
|
||||
const (
|
||||
reviewBetas = "claude-code-20250219,interleaved-thinking-2025-05-14,thinking-token-count-2026-05-13,context-management-2025-06-27,prompt-caching-scope-2026-01-05,mid-conversation-system-2026-04-07,effort-2025-11-24,dangerous-tool-use-2026-09-03,afk-mode-2026-01-31"
|
||||
reviewAsked = `[{"type":"dangerous_tool_use","classifier_context":{"v":1,"permission_mode":"auto","platform":"macos","live_cwd":"/Users/lc/work","home_dir":"/Users/lc","rule_roots":{"userSettings":"/Users/lc/.claude","projectSettings":"/Users/lc/work"},"trusted_directories":{"primary":{"path":"/Users/lc/work","resolved":["/Users/lc/work"]},"additional":[],"network":[],"block_reads_outside_working_directories":false},"rules":{"allow":[],"deny":[],"ask":[]},"auto_mode":{"allow":[],"soft_deny":[],"hard_deny":[],"environment":[]},"restricted":false,"is_remote_mode":false,"user_identity":null,"git_state":{"cwd":"/Users/lc/work","root":null,"branch":null,"error":"not_a_repo"},"prior_turn_context":[{"tool_use_ids":["toolu_01Capture001"],"context":{"live_cwd":"/Users/lc/work","platform":"macos"}}]}}]`
|
||||
// the verdict, as Claude Code 2.1.295 parses it
|
||||
reviewed = `[{"type":"dangerous_tool_use","status":{"type":"available","tool_uses":{"toolu_01Abc":{"type":"evaluated","outcome":"not_flagged"}}}}]`
|
||||
)
|
||||
|
||||
// reviewAsk is a turn of Claude Code's in auto mode, asking the review.
|
||||
func reviewAsk(model string) string {
|
||||
return `{"model":"` + model + `","max_tokens":64000,"stream":true,
|
||||
"safeguards":` + reviewAsked + `,
|
||||
"tools":[{"name":"Bash","description":"Run a shell command","input_schema":{"type":"object","properties":{"command":{"type":"string"}},"required":["command"]}}],
|
||||
"messages":[{"role":"user","content":[{"type":"text","text":"Run the shell command: echo hi"}]}]}`
|
||||
}
|
||||
|
||||
// reviewReply is a streamed call to Bash, with the review's verdict on it.
|
||||
func reviewReply(model string) string {
|
||||
return sse(
|
||||
`event: message_start`+"\n"+`data: {"type":"message_start","message":{"id":"msg_1","type":"message","role":"assistant","model":"`+model+`","content":[],"stop_reason":null,"stop_sequence":null,"usage":{"input_tokens":7,"output_tokens":1}}}`,
|
||||
`event: content_block_start`+"\n"+`data: {"type":"content_block_start","index":0,"content_block":{"type":"tool_use","id":"toolu_01Abc","name":"Bash","input":{}}}`,
|
||||
`event: content_block_delta`+"\n"+`data: {"type":"content_block_delta","index":0,"delta":{"type":"input_json_delta","partial_json":"{\"command\":\"echo hi\"}"}}`,
|
||||
`event: content_block_stop`+"\n"+`data: {"type":"content_block_stop","index":0}`,
|
||||
`event: message_delta`+"\n"+`data: {"type":"message_delta","delta":{"stop_reason":"tool_use","stop_sequence":null,"safeguard_results":`+reviewed+`},"usage":{"output_tokens":4}}`,
|
||||
`event: message_stop`+"\n"+`data: {"type":"message_stop"}`)
|
||||
}
|
||||
|
||||
// askReview sends reviewAsk(model) to s as Claude Code does.
|
||||
func askReview(t *testing.T, s *Server, path, body string) *httptest.ResponseRecorder {
|
||||
t.Helper()
|
||||
rec := httptest.NewRecorder()
|
||||
req := httptest.NewRequest("POST", path, strings.NewReader(body))
|
||||
req.Header.Set("User-Agent", "claude-cli/2.1.295 (external, sdk-cli)")
|
||||
req.Header.Set("anthropic-beta", reviewBetas)
|
||||
req.Header.Set("anthropic-version", "2023-06-01")
|
||||
s.Handler().ServeHTTP(rec, req)
|
||||
return rec
|
||||
}
|
||||
|
||||
// reviewIn is the safeguard_results of a streamed reply's message_delta,
|
||||
// where Claude Code reads them.
|
||||
func reviewIn(t *testing.T, stream string) string {
|
||||
t.Helper()
|
||||
for _, e := range events(stream) {
|
||||
if e["type"] == "message_delta" {
|
||||
if d, _ := e["delta"].(map[string]any); d["safeguard_results"] != nil {
|
||||
b, _ := json.Marshal(d["safeguard_results"])
|
||||
return string(b)
|
||||
}
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// Anthropic's API reviews the actions itself when asked: the
|
||||
// dangerous-tool-use beta and the safeguards field go to it as Claude Code
|
||||
// sent them, and safeguard_results come back as it answered them. (This
|
||||
// passes on main too; it guards the classifier's changes from reaching a
|
||||
// Claude model.)
|
||||
// sent them, and safeguard_results come back as it answered them, the call's
|
||||
// id with them as it was. (This passes on main too; it guards the
|
||||
// classifier's changes from reaching a Claude model.)
|
||||
func TestAutoModeServerSideReviewRelayed(t *testing.T) {
|
||||
f := &fake{t: t, reply: sse(
|
||||
`event: message_start`+"\n"+`data: {"type":"message_start","message":{"id":"msg_1","model":"claude-sonnet-5","usage":{"input_tokens":7}}}`,
|
||||
`event: content_block_start`+"\n"+`data: {"type":"content_block_start","index":0,"content_block":{"type":"tool_use","id":"toolu_01Abc","name":"Bash","input":{}}}`,
|
||||
`event: content_block_delta`+"\n"+`data: {"type":"content_block_delta","index":0,"delta":{"type":"input_json_delta","partial_json":"{\"command\":\"ls\"}"}}`,
|
||||
`event: content_block_stop`+"\n"+`data: {"type":"content_block_stop","index":0}`,
|
||||
`event: message_delta`+"\n"+`data: {"type":"message_delta","delta":{"stop_reason":"tool_use","safeguard_results":[{"type":"dangerous_tool_use","tool_uses":[{"tool_use_id":"toolu_01Abc","status":"evaluated","result":"not_flagged"}]}]},"usage":{"output_tokens":4}}`,
|
||||
`event: message_stop`+"\n"+`data: {"type":"message_stop"}`)}
|
||||
f := &fake{t: t, reply: reviewReply("claude-sonnet-5")}
|
||||
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
|
||||
t.Setenv("XDG_CACHE_HOME", t.TempDir())
|
||||
up := httptest.NewServer(f)
|
||||
@@ -170,37 +221,169 @@ func TestAutoModeServerSideReviewRelayed(t *testing.T) {
|
||||
if err := provider.Save(provider.Provider{ID: "ant", Name: "Ant", Key: "k", Anthropic: up.URL, Models: []string{"claude-sonnet-5"}}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
body := `{"model":"ant/claude-sonnet-5","max_tokens":64,"stream":true,
|
||||
"safeguards":[{"type":"dangerous_tool_use","classifier_context":"The user allows file edits in the project."}],
|
||||
"tools":[{"name":"Bash","input_schema":{"type":"object"}}],
|
||||
"messages":[{"role":"user","content":[{"type":"text","text":"<transcript>\n"},{"type":"text","text":"list the files"}]}]}`
|
||||
rec := httptest.NewRecorder()
|
||||
req := httptest.NewRequest("POST", "/v1/messages?beta=true", strings.NewReader(body))
|
||||
req.Header.Set("User-Agent", "claude-cli/2.1.285 (external, cli)")
|
||||
req.Header.Set("anthropic-beta", "claude-code-20250219,dangerous-tool-use-2026-09-03")
|
||||
req.Header.Set("anthropic-version", "2023-06-01")
|
||||
New().Handler().ServeHTTP(rec, req)
|
||||
rec := askReview(t, New(), "/v1/messages?beta=true", reviewAsk("ant/claude-sonnet-5"))
|
||||
if rec.Code != 200 {
|
||||
t.Fatalf("%d %s", rec.Code, rec.Body)
|
||||
}
|
||||
if !strings.Contains(f.head.Get("anthropic-beta"), "dangerous-tool-use-2026-09-03") {
|
||||
t.Errorf("beta not forwarded: %q", f.head.Get("anthropic-beta"))
|
||||
}
|
||||
var asked map[string]any
|
||||
var asked struct {
|
||||
Safeguards json.RawMessage `json:"safeguards"`
|
||||
MaxTokens int `json:"max_tokens"`
|
||||
}
|
||||
json.Unmarshal(f.got, &asked)
|
||||
sg, _ := asked["safeguards"].([]any)
|
||||
if len(sg) != 1 || sg[0].(map[string]any)["classifier_context"] != "The user allows file edits in the project." || asked["max_tokens"] != float64(64) {
|
||||
if !sameReview(asked.Safeguards, []byte(reviewAsked)) || asked.MaxTokens != 64000 {
|
||||
t.Errorf("request changed: %s", f.got)
|
||||
}
|
||||
var delta map[string]any
|
||||
for _, e := range events(rec.Body.String()) {
|
||||
if e["type"] == "message_delta" {
|
||||
delta, _ = e["delta"].(map[string]any)
|
||||
if got := reviewIn(t, rec.Body.String()); !sameReview([]byte(got), []byte(reviewed)) || !strings.Contains(rec.Body.String(), `"id":"toolu_01Abc"`) {
|
||||
t.Errorf("safeguard_results not relayed: %s", rec.Body)
|
||||
}
|
||||
}
|
||||
|
||||
// sameReview reports whether a and b are the same JSON value.
|
||||
func sameReview(a, b []byte) bool {
|
||||
var x, y any
|
||||
return json.Unmarshal(a, &x) == nil && json.Unmarshal(b, &y) == nil && reflect.DeepEqual(x, y)
|
||||
}
|
||||
|
||||
// Only Claude reviews actions: another vendor's model behind its Anthropic
|
||||
// API (DeepSeek's, Kimi's) is sent neither the beta nor the safeguards,
|
||||
// which a vendor checking what it is sent turns the request away over
|
||||
// (AiHubMix: 400 "safeguards: Extra inputs are not permitted"). Claude
|
||||
// Code, given no verdict, classifies locally. The agent's other betas go on,
|
||||
// and its count_tokens likewise goes without the beta.
|
||||
func TestAutoModeReviewOnlyForClaude(t *testing.T) {
|
||||
f := &fake{t: t}
|
||||
f.refuse = func(body []byte) (int, string) {
|
||||
if strings.Contains(f.head.Get("anthropic-beta"), "dangerous-tool-use") || strings.Contains(string(body), `"safeguards"`) {
|
||||
return 400, `{"type":"error","error":{"type":"invalid_request_error","message":"safeguards: Extra inputs are not permitted"}}`
|
||||
}
|
||||
return 0, ""
|
||||
}
|
||||
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
|
||||
t.Setenv("XDG_CACHE_HOME", t.TempDir())
|
||||
up := httptest.NewServer(f)
|
||||
t.Cleanup(up.Close)
|
||||
if err := provider.Save(provider.Provider{ID: "ds", Name: "DeepSeek", Key: "k", Anthropic: up.URL, Models: []string{"deepseek-flash"}}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
s := New()
|
||||
f.reply = sse(
|
||||
`event: message_start`+"\n"+`data: {"type":"message_start","message":{"id":"msg_1","type":"message","role":"assistant","model":"deepseek-flash","content":[],"usage":{"input_tokens":7}}}`,
|
||||
`event: content_block_start`+"\n"+`data: {"type":"content_block_start","index":0,"content_block":{"type":"text","text":""}}`,
|
||||
`event: content_block_delta`+"\n"+`data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"hi"}}`,
|
||||
`event: content_block_stop`+"\n"+`data: {"type":"content_block_stop","index":0}`,
|
||||
`event: message_delta`+"\n"+`data: {"type":"message_delta","delta":{"stop_reason":"end_turn"},"usage":{"output_tokens":1}}`,
|
||||
`event: message_stop`+"\n"+`data: {"type":"message_stop"}`)
|
||||
if rec := askReview(t, s, "/v1/messages?beta=true", reviewAsk("ds/deepseek-flash")); rec.Code != 200 {
|
||||
t.Fatalf("%d %s", rec.Code, rec.Body)
|
||||
}
|
||||
if strings.Contains(string(f.got), `"safeguards"`) {
|
||||
t.Errorf("safeguards sent to DeepSeek: %s", f.got)
|
||||
}
|
||||
beta := f.head.Get("anthropic-beta")
|
||||
if strings.Contains(beta, "dangerous-tool-use") || !strings.Contains(beta, "claude-code-20250219") || !strings.Contains(beta, "afk-mode-2026-01-31") {
|
||||
t.Errorf("anthropic-beta to DeepSeek: %q", beta)
|
||||
}
|
||||
|
||||
f.ctype, f.reply = "application/json", `{"input_tokens":12}`
|
||||
if rec := askReview(t, s, "/v1/messages/count_tokens?beta=true", `{"model":"ds/deepseek-flash","messages":[{"role":"user","content":"hi"}]}`); rec.Code != 200 {
|
||||
t.Fatalf("count_tokens: %d %s", rec.Code, rec.Body)
|
||||
}
|
||||
if f.path != "/v1/messages/count_tokens" || strings.Contains(f.head.Get("anthropic-beta"), "dangerous-tool-use") {
|
||||
t.Errorf("count_tokens to DeepSeek: %s %q", f.path, f.head.Get("anthropic-beta"))
|
||||
}
|
||||
}
|
||||
|
||||
// A provider that turned the beta away (Bedrock's runtime: Unexpected
|
||||
// value(s) for the anthropic-beta header) is asked again without it, and
|
||||
// without the safeguards it turns on, which it would turn away next.
|
||||
func TestAutoModeReviewGoesWithItsBeta(t *testing.T) {
|
||||
f := &fake{t: t, reply: reviewReply("claude-sonnet-5")}
|
||||
f.refuse = func(body []byte) (int, string) {
|
||||
if strings.Contains(f.head.Get("anthropic-beta"), "dangerous-tool-use") {
|
||||
return 400, `{"message":"Unexpected value(s) ` + "`dangerous-tool-use-2026-09-03`" + ` for the ` + "`anthropic-beta`" + ` header. Please consult our documentation at docs.anthropic.com or try again without the header."}`
|
||||
}
|
||||
if strings.Contains(string(body), `"safeguards"`) {
|
||||
return 400, `{"message":"safeguards: Extra inputs are not permitted"}`
|
||||
}
|
||||
return 0, ""
|
||||
}
|
||||
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
|
||||
t.Setenv("XDG_CACHE_HOME", t.TempDir())
|
||||
up := httptest.NewServer(f)
|
||||
t.Cleanup(up.Close)
|
||||
if err := provider.Save(provider.Provider{ID: "ant", Name: "Ant", Key: "k", Anthropic: up.URL, Models: []string{"claude-sonnet-5"}}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
s := New()
|
||||
for i := range 2 {
|
||||
f.calls = 0
|
||||
rec := askReview(t, s, "/v1/messages?beta=true", reviewAsk("ant/claude-sonnet-5"))
|
||||
if rec.Code != 200 {
|
||||
t.Fatalf("turn %d: %d %s", i, rec.Code, rec.Body)
|
||||
}
|
||||
if strings.Contains(string(f.got), `"safeguards"`) || !strings.Contains(f.head.Get("anthropic-beta"), "claude-code-20250219") {
|
||||
t.Errorf("turn %d went as %q: %s", i, f.head.Get("anthropic-beta"), f.got)
|
||||
}
|
||||
if i == 1 && f.calls != 1 {
|
||||
t.Errorf("the next turn took %d requests, want 1", f.calls)
|
||||
}
|
||||
}
|
||||
res, _ := delta["safeguard_results"].([]any)
|
||||
if len(res) != 1 || !strings.Contains(rec.Body.String(), `"tool_use_id":"toolu_01Abc"`) {
|
||||
t.Errorf("safeguard_results not relayed: %s", rec.Body)
|
||||
}
|
||||
|
||||
// A reply its vendor gave whole goes to Claude Code, which asked a stream,
|
||||
// as the stream would have said it: the verdict in the message_delta,
|
||||
// where Claude Code reads it, not the message_start, where it finds no
|
||||
// result and leaves the review for the session.
|
||||
func TestAutoModeReviewOfAWholeReplyStreamed(t *testing.T) {
|
||||
whole := `{"id":"msg_1","type":"message","role":"assistant","model":"claude-sonnet-5","content":[{"type":"tool_use","id":"toolu_01Abc","name":"Bash","input":{"command":"echo hi"}}],"stop_reason":"tool_use","stop_sequence":null,"usage":{"input_tokens":7,"output_tokens":4},"safeguard_results":` + reviewed + `}`
|
||||
rec := httptest.NewRecorder()
|
||||
if !wholeAsStream(rec, provider.Anthropic, []byte(whole)) {
|
||||
t.Fatal("not a reply")
|
||||
}
|
||||
if got := reviewIn(t, rec.Body.String()); !sameReview([]byte(got), []byte(reviewed)) {
|
||||
t.Errorf("verdict not in message_delta: %s", rec.Body)
|
||||
}
|
||||
for _, e := range events(rec.Body.String()) {
|
||||
if m, _ := e["message"].(map[string]any); e["type"] == "message_start" && m["safeguard_results"] != nil {
|
||||
t.Errorf("verdict in message_start: %v", e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// A turn of Claude Code's translated to an Anthropic endpoint (the
|
||||
// candidates' loop, not relayed) asks Claude the review and gives Claude
|
||||
// Code its verdict; another vendor's model isn't asked it.
|
||||
func TestAutoModeReviewTranslated(t *testing.T) {
|
||||
req, err := parseAnthropic([]byte(reviewAsk("claude-sonnet-5")))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var out struct {
|
||||
Safeguards json.RawMessage `json:"safeguards"`
|
||||
}
|
||||
json.Unmarshal(buildAnthropic(req, "claude-sonnet-5"), &out)
|
||||
if !sameReview(out.Safeguards, []byte(reviewAsked)) {
|
||||
t.Errorf("safeguards to Claude: %s", out.Safeguards)
|
||||
}
|
||||
if b := buildAnthropic(req, "deepseek-flash"); strings.Contains(string(b), `"safeguards"`) {
|
||||
t.Errorf("safeguards to DeepSeek: %s", b)
|
||||
}
|
||||
|
||||
rec := httptest.NewRecorder()
|
||||
enc := &anthropicEncoder{w: newSSEWriter(rec), model: "claude-sonnet-5"}
|
||||
dec := decoder(provider.Anthropic)
|
||||
for _, e := range events(reviewReply("claude-sonnet-5")) {
|
||||
b, _ := json.Marshal(e)
|
||||
if err := dec(string(b), enc.event); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
enc.finish()
|
||||
if got := reviewIn(t, rec.Body.String()); !sameReview([]byte(got), []byte(reviewed)) || !strings.Contains(rec.Body.String(), `"id":"toolu_01Abc"`) {
|
||||
t.Errorf("verdict not given back: %s", rec.Body)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2871,12 +2871,19 @@ func (s *Server) forwardOnce(ctx context.Context, p provider.Provider, to provid
|
||||
// generate route reads it from the body
|
||||
body = withoutFields(body, "model")
|
||||
}
|
||||
var betas []string
|
||||
if to == provider.Anthropic {
|
||||
body = s.bodyBetas(p, body)
|
||||
body = s.withoutRefusedShapes(p, body)
|
||||
// what every path to an Anthropic endpoint sends, relayed or
|
||||
// built, with the model named as the vendor names it
|
||||
body = adaptiveThinking(body)
|
||||
asked := askedBetas(in)
|
||||
if gjson.GetBytes(body, "speed").String() == "fast" && provider.HostOf(p.Base(to)) == "api.anthropic.com" {
|
||||
asked = append(slices.Clone(asked), claudeFastBeta) // a group's member sent fast
|
||||
}
|
||||
// auto mode's review goes to Claude alone (automode.go)
|
||||
betas, body = s.serverReview(p, asked, body)
|
||||
}
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, p.Base(to)+path, bytes.NewReader(body))
|
||||
if err != nil {
|
||||
@@ -2902,12 +2909,8 @@ func (s *Server) forwardOnce(ctx context.Context, p provider.Provider, to provid
|
||||
}
|
||||
}
|
||||
}
|
||||
asked := askedBetas(in)
|
||||
if gjson.GetBytes(body, "speed").String() == "fast" && provider.HostOf(p.Base(to)) == "api.anthropic.com" {
|
||||
asked = append(slices.Clone(asked), claudeFastBeta) // a group's member sent fast
|
||||
}
|
||||
if bs := s.betas(p, asked); len(bs) > 0 {
|
||||
req.Header.Set("anthropic-beta", strings.Join(bs, ","))
|
||||
if len(betas) > 0 {
|
||||
req.Header.Set("anthropic-beta", strings.Join(betas, ","))
|
||||
} else {
|
||||
req.Header.Del("anthropic-beta")
|
||||
}
|
||||
|
||||
@@ -255,6 +255,9 @@ func anthropicWholeEvents(b []byte) []wholeEvent {
|
||||
}
|
||||
start := maps.Clone(v)
|
||||
start["content"], start["stop_reason"], start["stop_sequence"] = raw([]any{}), raw(nil), raw(nil)
|
||||
// auto mode's review of the reply's calls is the last thing a stream
|
||||
// says, in its message_delta, where Claude Code reads it (automode.go)
|
||||
delete(start, "safeguard_results")
|
||||
var usage obj
|
||||
if json.Unmarshal(v["usage"], &usage) == nil && usage != nil {
|
||||
// the output is counted as it is said, in message_delta
|
||||
@@ -307,6 +310,9 @@ func anthropicWholeEvents(b []byte) []wholeEvent {
|
||||
if c, ok := v["container"]; ok {
|
||||
stop["container"] = c
|
||||
}
|
||||
if r, ok := v["safeguard_results"]; ok {
|
||||
stop["safeguard_results"] = r
|
||||
}
|
||||
end := obj{"delta": raw(stop)}
|
||||
if usage != nil {
|
||||
end["usage"] = v["usage"]
|
||||
|
||||
Reference in New Issue
Block a user