Files
magpie/internal/gateway/claude_effort_test.go
T
ggbdpq 6ac6760086 gateway: the Claude bridge passes the request's effort to the CLI it spawns (#2899)
A request for a Claude Code subscription could name its effort in
output_config.effort and the bridge still ran Claude Code at the CLI's own
level: parseAnthropic reads output_config.effort only when the request asks
thinking with it, so a request carrying an effort and no thinking ask
reached serveSubscription with no effort, and the --effort its
claudeCLIArgs appends for one was never appended. The reporter sampled the
spawned claude -p and found no --effort on its command line and no effort
variable in its environment, while their A/B table has the CLI's own
--effort low and --effort max at 325 and 1002 output tokens and the same
efforts through the gateway indistinguishable (~9s either way).

serveSubscription now takes the effort an Anthropic request's
output_config names even when its parse left none (outputConfigEffort),
unless the request turned thinking off. The gate that keeps a thinking-less
effort from turning thinking on upstream still stands for every other
backend: through the bridge the level is --effort, which works whatever
the turn thinks of. A request with no effort spawns as it did, and a turn
resumed in a run waiting for it is told the level as before (setEffort).

TestClaudeBridgePassesTheRequestsEffort stands in for Claude Code with a
script that records the arguments it is spawned with, and fails on the old
code: output_config.effort max came through with no --effort on the
recorded command line. With the change it reads --effort max, a request
that asks its effort with thinking reads it as it always did, and a
request that asks no effort spawns with no --effort.

go test ./internal/gateway/ -count=1 (full package, 341.9s), -run TestClaude
(33.3s), -race -run TestClaude (40.5s), go vet (darwin, GOOS=linux,
GOOS=windows), gofmt and go build -tags nogui (darwin, GOOS=linux,
GOOS=windows) pass.

Generated-by: GLM-5.3-Flash (ZCode)

Landed from ggbdpq's branch (patch posted on the issue; sha256/patch-id
checked against the issue) by yetone, rebased on origin/main 2026-11-03.
Run at the pushed tree, on mote (macOS): go test -tags nogui -count=1 of
internal/gateway with -run over this batch's tests and their families
(PlainSchema, Antigravity, AutoMode, WebP, ClaudeTUI, NativeRelay,
Namespace, Grok, Zed, Codex, ClinePass, rewrite) ok; -race -count=3 of
the new tests ok; GOOS=linux and GOOS=windows go vet of internal/gateway
and gofmt -l clean. The full gateway-and-importers run, started at an
earlier base, was still running at the push; its reds get issues.
Changed in review: the effort taken from output_config no longer
outranks #250's forced low for Claude Code's auto mode classifier
(the injection ran before start's req.Effort == "" check); the test
has a classifier case that fails without the guard.
Broken by hand: the product change reverted, the effort test red;
the classifier guard alone removed, its case red (--effort high).
Shell-script fake Claude Code: skipped on Windows, not run there. Not
tried with a real Claude Code sign-in.
2026-11-03 02:27:57 +08:00

82 lines
3.7 KiB
Go

package gateway
import (
"net/http/httptest"
"os"
"path/filepath"
"runtime"
"strings"
"testing"
"github.com/yetone/magpie/internal/provider"
"github.com/yetone/magpie/internal/testenv"
)
// TestClaudeBridgePassesTheRequestsEffort: the effort a request carries is
// the level the spawned Claude Code thinks at (--effort), also when the
// request asks no thinking with it (#2899: output_config.effort low and max
// reached the subscription as the same turn, with no --effort on the spawned
// command line).
func TestClaudeBridgePassesTheRequestsEffort(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("a shell script stands in for Claude Code")
}
testenv.SetHome(t, t.TempDir())
t.Setenv("TMPDIR", t.TempDir())
dir := t.TempDir()
script := `#!/bin/sh
printf '%s\n' "$@" > ` + dir + `/args
while read -r line; do
echo '{"type":"stream_event","event":{"type":"message_start","message":{"id":"m","model":"claude-opus-5-5","usage":{"input_tokens":1}}}}'
echo '{"type":"stream_event","event":{"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"ok"}}}'
echo '{"type":"stream_event","event":{"type":"message_delta","delta":{"stop_reason":"end_turn"},"usage":{"output_tokens":1}}}'
echo '{"type":"stream_event","event":{"type":"message_stop"}}'
echo '{"type":"result","subtype":"success","is_error":false,"result":""}'
done
`
testenv.Program(t, filepath.Join(dir, "claude"), script)
t.Setenv("PATH", dir+string(os.PathListSeparator)+os.Getenv("PATH"))
s := New()
t.Cleanup(s.subscription.abortAll)
p := provider.Provider{ID: "claude", Account: &provider.Account{Agent: "claude", User: "u"}}
post := func(body string) string {
t.Helper()
rec := httptest.NewRecorder()
var u Usage
if code, msg := s.serveClaudeSubscription(rec, httptest.NewRequest("POST", "/v1/messages", strings.NewReader(body)), provider.Anthropic, p, "claude-opus-5-5", []byte(body), &u); code != 200 {
t.Fatalf("%d %s", code, msg)
}
b, err := os.ReadFile(filepath.Join(dir, "args"))
if err != nil {
t.Fatal(err)
}
// the fake writes one argument a line; the command line is them
// joined, which is what the assertions below read
return strings.Join(strings.Fields(string(b)), " ")
}
// the effort an output_config names, thinking asked or not, is the
// spawned Claude Code's
args := post(`{"model":"claude-opus-5-5","max_tokens":100,"output_config":{"effort":"max"},"messages":[{"role":"user","content":"How many positive integers below 100000 have digits that sum to 23?"}]}`)
if !strings.Contains(args, "--effort max") {
t.Fatalf("output_config.effort max: no --effort max in %q", args)
}
// with the thinking ask, as it always was
args = post(`{"model":"claude-opus-5-5","max_tokens":100,"thinking":{"type":"enabled","budget_tokens":1024},"output_config":{"effort":"high"},"messages":[{"role":"user","content":"and now?"}]}`)
if !strings.Contains(args, "--effort high") {
t.Fatalf("output_config.effort high with thinking: no --effort high in %q", args)
}
// Claude Code's auto mode classifier is asked at low whatever effort
// it carries: its verdict is due within a minute (#250)
args = post(`{"model":"claude-opus-5-5","max_tokens":100,"system":"Judge the action. Answer <block>yes</block> or <block>no</block>.","output_config":{"effort":"high"},"messages":[{"role":"user","content":"<transcript>rm -rf build</transcript>"}]}`)
if !strings.Contains(args, "--effort low") {
t.Fatalf("the classifier with output_config.effort high: no --effort low in %q", args)
}
// no effort asked, no flag: the CLI's own level stands
args = post(`{"model":"claude-opus-5-5","max_tokens":100,"messages":[{"role":"user","content":"one more"}]}`)
if strings.Contains(args, "--effort") {
t.Fatalf("no effort asked: --effort in %q", args)
}
}