tbxark's request log: Claude Code's own requests went to its tier, while Python-urllib asked /v1/messages for claude-sonnet-4-6 and claude-opus-5-5 and got 404 "unknown model" in 5 ms. A hook, the statusline or a plugin's script that Claude Code starts inherits its ANTHROPIC_BASE_URL and token, so it reaches magpie with the plain gateway token and its own User-Agent. claudeTierStandIn mapped a Claude id to Claude Code's tier only for a request magpie took for Claude Code, by its User-Agent, so the script's ids went nowhere. Now a request that came with the plain token (not a magpie-<agent> token magpie gave an agent), whose User-Agent names no agent magpie knows (usage.IsAgent; claude-desktop and cursor-local are known too), and that names a Claude-family id magpie doesn't serve, gets Claude Code's stand-in for it, on /v1/messages and on count_tokens (the one choke point both use). Nothing changes when: - the id is served (a Claude subscription or an Anthropic key in the catalog lists it): it goes where it is served, as before; - the caller is an agent magpie knows, or uses an agent's own token; - Claude Code isn't routed through this gateway (StandIn says nothing), so the 404 stays. Run: - TestClaudeTierForAScriptClaudeCodeStarted (new) fails on the old gateway.go (404 for urllib, "", anthropic-python; count_tokens counted 0 without reaching the tier). Breaking the token check by hand or the served-id check by hand turns it red too. TestIsAgentByUserAgent (new, internal/agent) covers IsAgent with the real agent list. - Sandbox HOME, a fake Anthropic upstream, `magpie serve` on 127.0.0.1:3561 with Claude Code's settings.json routed there (sonnet/opus tiers set): python3 urllib POST /v1/messages claude-sonnet-4-6 with x-api-key magpie: old binary 404 "magpie knows no model", new binary 200, upstream got m-sonnet-tier; claude-opus-5-5 -> m-opus-tier; count_tokens reached the upstream on m-sonnet-tier. A codex_cli_rs User-Agent still gets 404. - gofmt -l; go vet ./...; GOOS=linux go vet -tags nogui ./...; GOOS=windows go vet ./...; go test of every package (gateway with -timeout 25m). A full ./... run on the loaded Mac timed the gateway package out at 10m (#1654); run alone it passed in 330s.
29 lines
884 B
Go
29 lines
884 B
Go
package agent
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/usage"
|
|
)
|
|
|
|
// The gateway takes a client that is no agent magpie knows, with the plain
|
|
// token, for a script Claude Code started (tbxark on X); every agent's own
|
|
// User-Agent must still read as an agent, and a script's must not.
|
|
func TestIsAgentByUserAgent(t *testing.T) {
|
|
for _, a := range Clients() {
|
|
if !usage.IsAgent(a.ID) {
|
|
t.Errorf("%s isn't an agent", a.ID)
|
|
}
|
|
}
|
|
for _, ua := range []string{"claude-cli/2.1.287 (external, cli)", "codex_cli_rs/0.160.0 (Mac OS 26.6.0; arm64)", "Cursor/3.2"} {
|
|
if got := usage.AgentOf(ua); !usage.IsAgent(got) {
|
|
t.Errorf("%q reads as %q, no agent", ua, got)
|
|
}
|
|
}
|
|
for _, ua := range []string{"Python-urllib/3.12", "anthropic-python/0.75.0", ""} {
|
|
if got := usage.AgentOf(ua); usage.IsAgent(got) {
|
|
t.Errorf("%q reads as the agent %q", ua, got)
|
|
}
|
|
}
|
|
}
|