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.
This commit is contained in:
yetone
2026-11-10 19:55:26 +08:00
parent 5e3f14f21f
commit 18c932385e
3 changed files with 154 additions and 1 deletions
@@ -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,
+100
View File
@@ -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)
}
}
}
+39 -1
View File
@@ -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.