diff --git a/docs/subsystems/claude-subscription-bridge.md b/docs/subsystems/claude-subscription-bridge.md index d2742a6f6..876b44cf0 100644 --- a/docs/subsystems/claude-subscription-bridge.md +++ b/docs/subsystems/claude-subscription-bridge.md @@ -43,6 +43,21 @@ FrierenF on Discord). A run as anyone else has its env as before. Only the bridge's run gets it; the warm-up and `/usage` runs don't skip permissions. +A run asks Claude Code for the model by `claudeRunName`. A Claude model +of the 5 generation or later that Claude Code doesn't know (Fable 5.2 on +2.1.316) is asked for by its `[1m]` name. By its own name Claude Code +assumed a 200K window for it and turned a longer conversation away before +Anthropic was asked ("Prompt is too long", `blocking_limit`), while magpie +tells agents the model's 1M window: Codex, compacting at 272K, never +compacted and failed every turn past 200K (magicalaciw18 on X). +`CLAUDE_CODE_MAX_CONTEXT_TOKENS` is not read for a Claude name. The models +Claude Code knows at 1M (`claudeKnownLong`) keep their names, since a new +name misses the prompt cache once. Every 4 generation model keeps its name +too: `claude-sonnet-4-6[1m]` is refused even a short turn on an account +without usage credits ("Usage credits required for 1M context"). The +reply, the saved session and the logs keep the model as asked; only +Claude Code's `--model` has the mark. + Every Claude Code magpie starts for the subscription (a turn's run, the warm-up and model test `askClaude`, `/usage`, and the sign-in's `auth login` and `auth status`) is given its proxy through one helper, diff --git a/internal/gateway/claude_long_name_test.go b/internal/gateway/claude_long_name_test.go new file mode 100644 index 000000000..17ce1ed23 --- /dev/null +++ b/internal/gateway/claude_long_name_test.go @@ -0,0 +1,100 @@ +package gateway + +import ( + "encoding/json" + "net/http/httptest" + "os" + "path/filepath" + "runtime" + "strings" + "testing" + + "github.com/yetone/magpie/internal/provider" + "github.com/yetone/magpie/internal/testenv" +) + +// A Claude model of the 5 generation on that Claude Code doesn't know +// (Fable 5.2 on 2.1.316) is asked for by its "[1m]" name, which Claude +// Code runs to 1M: by its own it held the model to 200K and turned a +// longer conversation away before Anthropic was asked, so Codex, told the +// model's 1M window and compacting at 272K, never compacted (magicalaciw18 +// on X). One it knows, and every 4 generation one, whose 1M a subscription +// without usage credits is refused a short turn on, keep their names. +func TestClaudeRunName(t *testing.T) { + for model, want := range map[string]string{ + "claude-fable-5-2": "claude-fable-5-2[1m]", + "claude-opus-6": "claude-opus-6[1m]", + "claude-sonnet-5-7": "claude-sonnet-5-7[1m]", + "claude-fable-5-2[1m]": "claude-fable-5-2[1m]", + "claude-fable-5-1": "claude-fable-5-1", + "claude-fable-5": "claude-fable-5", + "claude-opus-5-5": "claude-opus-5-5", + "claude-sonnet-5-5": "claude-sonnet-5-5", + "claude-haiku-5-5": "claude-haiku-5-5", + "claude-mythos-5-1": "claude-mythos-5-1", + "claude-sonnet-4-6": "claude-sonnet-4-6", + "claude-opus-4-8": "claude-opus-4-8", + "claude-haiku-4-5": "claude-haiku-4-5", + "claude-3-7-sonnet": "claude-3-7-sonnet", + "haiku": "haiku", + "m": "m", + } { + if got := claudeRunName(model); got != want { + t.Errorf("claudeRunName(%q) = %q, want %q", model, got, want) + } + } +} + +// The run of a Codex turn on claude/claude-fable-5-2 asks Claude Code for +// claude-fable-5-2[1m], and the reply names the model as asked. +func TestClaudeRunAsksUnknownModelLong(t *testing.T) { + if runtime.GOOS == "windows" { + t.Skip("a shell script stands in for Claude Code") + } + dir := t.TempDir() + argsFile := filepath.Join(dir, "args") + script := `#!/bin/sh +printf '%s\n' "$@" >> '` + argsFile + `' +while read -r line; do + echo '{"type":"stream_event","event":{"type":"message_start","message":{"id":"m","model":"x","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")) + runsEndWithTest(t, dir) + s := New() + t.Cleanup(s.subscription.abortAll) + p := provider.Provider{ID: "claude", Account: &provider.Account{Agent: "claude", User: "u"}} + for model, want := range map[string]string{"claude-fable-5-2": "claude-fable-5-2[1m]", "claude-sonnet-4-6": "claude-sonnet-4-6"} { + _ = os.Remove(argsFile) + body := `{"model":"` + model + `","max_tokens":100,"messages":[{"role":"user","content":"hi"}]}` + rec := httptest.NewRecorder() + var u Usage + if code, msg := s.serveClaudeSubscription(rec, httptest.NewRequest("POST", "/v1/messages", strings.NewReader(body)), provider.Anthropic, p, model, []byte(body), &u); code != 200 { + t.Fatalf("%s: %d %s", model, code, msg) + } + b, err := os.ReadFile(argsFile) + if err != nil { + t.Fatal(err) + } + args := strings.Split(string(b), "\n") + got := "" + for i, a := range args { + if a == "--model" && i+1 < len(args) { + got = args[i+1] + } + } + if got != want { + t.Errorf("%s: Claude Code was asked for %q, want %q", model, got, want) + } + var res struct{ Model string } + json.Unmarshal(rec.Body.Bytes(), &res) + if strings.Contains(res.Model, "[1m]") { + t.Errorf("%s: the reply names %q", model, res.Model) + } + } +} diff --git a/internal/gateway/claude_subscription.go b/internal/gateway/claude_subscription.go index 56d7cb96e..e9f81f741 100644 --- a/internal/gateway/claude_subscription.go +++ b/internal/gateway/claude_subscription.go @@ -61,6 +61,7 @@ import ( "github.com/santhosh-tekuri/jsonschema/v6" "github.com/yetone/magpie/internal/appdir" + "github.com/yetone/magpie/internal/catalog" "github.com/yetone/magpie/internal/claudecode" "github.com/yetone/magpie/internal/claudetui" "github.com/yetone/magpie/internal/edit" @@ -1012,7 +1013,7 @@ func (b *subscriptionBridge) startRun(ctx context.Context, req *Request, model, mcpConfig, _ := json.Marshal(map[string]any{"mcpServers": map[string]any{ "magpie": map[string]any{"command": exe, "args": []string{claudetui.MCPCommand, callbackPath, toolsPath, filepath.Join(tmp, "listed")}}, }}) - args := claudeCLIArgs(model, string(mcpConfig), req.Effort, req.WebSearch) + args := claudeCLIArgs(claudeRunName(model), string(mcpConfig), req.Effort, req.WebSearch) if req.ThinkOff { // the client turned thinking off, as Claude Code's auto mode // classifier and its small asks do: a run at Claude Code's own @@ -3521,6 +3522,43 @@ func messageWords(msgs []Message) []string { return out } +// claudeKnownLong are the Claude models Claude Code (2.1.316) knows, and +// runs to their 1M window by their own ids. +var claudeKnownLong = []string{ + "claude-fable-5", "claude-fable-5-1", "claude-mythos-5", "claude-mythos-5-1", + "claude-opus-5", "claude-opus-5-5", "claude-sonnet-5", "claude-sonnet-5-5", "claude-haiku-5-5", +} + +// claudeGenRe is a Claude model's id, its generation in the first group. +var claudeGenRe = lazyre.New(`^claude-(?:opus|sonnet|haiku|fable|mythos)-([0-9]+)(?:-[0-9]+)?$`) + +// claudeRunName is the name a run asks Claude Code for model by. A Claude +// model of the 5 generation or later that Claude Code doesn't know (Fable +// 5.2 on 2.1.316) it holds to 200K, and turned away a longer conversation +// before Anthropic was asked ("Prompt is too long"), while magpie tells +// the agent the model's 1M window: Codex, which compacts at 272K, never +// compacted and failed every turn past 200K instead (magicalaciw18 on X). +// CLAUDE_CODE_MAX_CONTEXT_TOKENS isn't read for a Claude name; "[1m]" is, +// and Anthropic serves these models to 1M on a subscription as they are. +// One Claude Code knows keeps its id, as does every 4 generation one, whose +// 1M a subscription without usage credits is refused even a short turn +// on (claude-sonnet-4-6[1m]: "Usage credits required for 1M context"), +// and one whose window magpie knows to be less. +func claudeRunName(model string) string { + id := strings.ToLower(model) + m := claudeGenRe.Regexp().FindStringSubmatch(id) + if m == nil || slices.Contains(claudeKnownLong, id) { + return model + } + if gen, _ := strconv.Atoi(m[1]); gen < 5 { + return model + } + if w := catalog.ContextOf(model); w > 0 && w < 1000000 { + return model + } + return model + "[1m]" +} + // claudeCLIArgs runs Claude Code with none of its own tools but, when the // client offered a web search (Claude Code's own WebSearch tool asks for // Anthropic's), its WebSearch, which searches as the subscription does.