Files
magpie/internal/gateway/claude_long_name_test.go
T
yetone 18c932385e claude: a Claude model Claude Code doesn't know is run to 1M, so Codex compacts on Fable 5.2 (magicalaciw18 on X)
Codex on claude/claude-fable-5-2 (the Claude subscription bridge) never
auto-compacted. magpie tells Codex the model's 1M window and Codex
compacts at 272K, but Claude Code 2.1.316 doesn't know claude-fable-5-2,
so it assumes a 200K window. It turned every conversation past 200K away
before Anthropic was asked ("Prompt is too long", blocking_limit, 0
tokens). Codex got that refusal as a failed turn, not a compaction.

claudeRunName now asks Claude Code for such a model by its "[1m]" name.
That applies to a Claude model of generation 5 or later that Claude Code
doesn't know (claudeKnownLong) and whose window magpie doesn't know to be
under 1M. Models it knows keep their names, since a new name misses the
prompt cache once. Every generation 4 model keeps its name too:
claude-sonnet-4-6[1m] is refused even a short turn on an account without
usage credits. Only Claude Code's --model changes. The reply, the saved
session and the logs keep the model as asked.

Real runs (sandbox CLAUDE_CONFIG_DIR, real accounts, claude -p 2.1.316,
~275K-token prompt):
- claude-fable-5-2: Prompt is too long / blocking_limit.
- claude-fable-5-2 with CLAUDE_CODE_MAX_CONTEXT_TOKENS=1000000, or with
  CLAUDE_CODE_DISABLE_UNKNOWN_MODEL_WINDOW_ENFORCEMENT=1: still blocked.
- claude-fable-5-2[1m]: OK.
- fable-5-1, sonnet-5-5, opus-5-5, haiku-5-5 plain, and fable-5-1[1m],
  sonnet-5-5[1m]: all OK.
- Short turn: sonnet-4-6[1m] got 429 "Usage credits required for 1M
  context" on one account; opus-4-6[1m], opus-4-8[1m] and fable-5-2[1m]
  were OK.
- The live gateway gave the same refusal: 230K to claude/claude-fable-5-2
  was refused, while claude/claude-fable-5-1 took 269K.

Siblings: Copilot lists max_prompt_tokens 200000 for claude-sonnet-5.5,
while magpie says 1M. A real 265K turn on copilot/claude-sonnet-5.5 (and
on copilot/claude-fable-5.2) was served, so that limit isn't enforced and
was left alone.

Tests: TestClaudeRunName, and TestClaudeRunAsksUnknownModelLong, which
uses a fake claude that records its args.
Run on mote:
- the new tests with TestClaudeCLI* and TestClaudeRunKept*: ok;
- go test -count=1 -timeout 45m ./internal/gateway/: ok (900s);
- go vet for darwin, linux -tags nogui and windows on ./internal/gateway/;
- gofmt -l: clean.

Broken by hand:
- passing the bare model to claudeCLIArgs made
  TestClaudeRunAsksUnknownModelLong red (asked for "claude-fable-5-2");
- dropping the generation 4 floor made both tests red (sonnet-4-6[1m],
  haiku-4-5[1m], opus-4-8[1m]).

Not tried:
- a real Codex session compacting through a sandbox build end to end;
- Claude Code versions other than 2.1.316, where the list of models it
  knows differs;
- Windows (the fake claude is a shell script, so the test skips there).
The full suite runs after the tag.

Rebased on 9c39edb92 (32 commits on, incl. 401187300 catalog fallback and 543eb39b9 interactive runs): the Claude run tests in internal/gateway, go vet darwin/linux nogui/windows ok again at this head on mote. The interactive mode takes the run's args (interactiveArgs), so it asks the [1m] name too.
2026-11-10 19:55:26 +08:00

101 lines
3.9 KiB
Go

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)
}
}
}