Files
magpie/internal/gateway/claude_ratelimit_headers_test.go
T
yetone 5214d5114f claude: a run on a new account's first turn keeps its tool results
On the first request on a newly seen Claude sign-in, claude auth status
has no email yet, so the account is named from its plan ("Claude Max").
The bridged run's Claude Code then writes the profile, the account is
named by its email, and the turn's tool results came under another owner:
"tool results account changed", the run let go, and a new one told the
whole conversation (~8k more cache writes). Seen in a real run as
"claude\x00Claude Max\x00own" -> "claude\x00<email>\x00own".

- provider: Account.Unnamed marks the placeholder name;
  ClaudeOwnName gives the own sign-in's name once it has one.
- gateway: the run's owner is an atomic pointer (owned()); an own,
  unnamed owner ends in "\x00unnamed". takeNamedOwner lets such a run go
  on as the same provider's own sign-in, named; every other owner change
  still lets the run go. A turn that ends without a tool call takes the
  name before its keys are made (nameOwner), so the next turn finds the
  idle run.
- docs/subsystems/claude-subscription-bridge.md says so.

Run:
- TestClaudeToolResultsKeepTheSelectedAccount gains "named since" (kept)
  and "unnamed, another account" (let go);
  TestClaudeRunTakesOnlyItsOwnSignInsName; TestClaudeIdleRunKeptUnder-
  ItsOwnSignInsName (named since / still unnamed); provider asserts
  Unnamed on the plan-named account and not on an email-named one.
  Broken by hand: without takeNamedOwner in serveSubscription "named
  since" goes red (a new run replies); renaming any own owner goes red
  (a@ -> b@ taken); without nameOwner the idle case goes red; with
  unnamed never set the provider test goes red.
- Real run: sandbox HOME, dev build of this tree on 127.0.0.1:3741, the
  owner's Claude Max sign-in with .claude.json and logins.json removed
  first (auth status: no email), a real Claude Code 2.1.310 client in a
  sandbox config asked to write, wc and read pelican.html. One Claude
  Code pid for the whole turn, the log says the run "goes on as" the
  email, no "account changed" line. Before this change the same setup
  logged the owner change and "tool results account changed".
- gofmt; go vet darwin, linux -tags nogui, windows for gateway and
  provider; go test ./internal/gateway ok 392.7s; go test
  ./internal/provider ok 89.3s with -skip TestUnreadLoginsWriteErrors-
  KeepCredentials, which crashes the package on main alone (#3632,
  filememo synctest, not this change).
- Full suite at this tree (3ea0dd908, committed first as 3a201180b on 42ec2700b): go test
  -tags nogui -count=1 -timeout 45m ./... -skip TestUnreadLoginsWrite-
  ErrorsKeepCredentials (#3632): exit 0, 71 packages ok, no FAIL in the
  kept log. No other suite ran on the machine (no suite-lock script
  here; ps checked before the start).
Not tried: a sign-in switch mid-turn from one named account to another
(still lets the run go, by the test).
2026-10-26 01:59:38 +08:00

152 lines
7.4 KiB
Go

package gateway
import (
"context"
"io"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"runtime"
"strconv"
"strings"
"testing"
"time"
"github.com/yetone/magpie/internal/provider"
"github.com/yetone/magpie/internal/testenv"
)
// A reply a Claude subscription account answered says that account's
// allowance as Anthropic says it to Claude Code, in its
// anthropic-ratelimit-unified-* headers: a Claude Code signed in to
// claude.ai reads them into its status line's rate_limits, where
// claude-hud shows the 5-hour and weekly figures (#1257). The run's agent
// tells them in its rate_limit_event, which comes before the reply's first
// event, so the reply says this turn's.
func TestClaudeSubscriptionReplySaysTheAllowance(t *testing.T) {
five := time.Now().Add(3 * time.Hour).Unix()
week := time.Now().Add(4 * 24 * time.Hour).Unix()
user := "hud-" + strconv.FormatInt(time.Now().UnixNano(), 36) + "@example.com"
limits := `{"type":"rate_limit_event","rate_limit_info":{"status":"allowed","rateLimitType":"five_hour","utilization":0.42,"resetsAt":` + strconv.FormatInt(five, 10) +
`,"unifiedWindows":{"five_hour":{"utilization":0.42,"resetsAt":` + strconv.FormatInt(five, 10) + `},"seven_day":{"utilization":0.17,"resetsAt":` + strconv.FormatInt(week, 10) + `}}}}`
answer := []string{
`{"type":"stream_event","event":{"type":"message_start","message":{"id":"m1","model":"claude-sonnet-5","usage":{"input_tokens":3,"output_tokens":1}}}}`,
`{"type":"stream_event","event":{"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"OK"}}}`,
`{"type":"stream_event","event":{"type":"message_delta","delta":{"stop_reason":"end_turn"},"usage":{"output_tokens":2}}}`,
`{"type":"stream_event","event":{"type":"message_stop"}}`,
}
ask := func(name, owner string, stream bool, lines []string) *httptest.ResponseRecorder {
t.Helper()
s := New()
start := func(_ context.Context, req *Request) (*subscriptionRun, <-chan Event, error) {
run := &subscriptionRun{bridge: s.subscription, token: "hud", pending: map[string]chan mcpToolResult{}}
run.owner.Store(&owner)
events := run.attach()
go run.readOutput(strings.NewReader(strings.Join(lines, "\n") + "\n"))
return run, events, nil
}
body := `{"model":"claude-sonnet-5","max_tokens":100,"stream":` + strconv.FormatBool(stream) + `,"messages":[{"role":"user","content":"hi"}]}`
rec := httptest.NewRecorder()
var u Usage
if code, msg := s.serveSubscription(rec, httptest.NewRequest("POST", "/v1/messages", strings.NewReader(body)), provider.Anthropic, name, "claude-sonnet-5", owner, []byte(body), &u, start); code != 200 {
t.Fatalf("%d %s", code, msg)
}
return rec
}
says := func(rec *httptest.ResponseRecorder, want map[string]string) {
t.Helper()
for k, v := range want {
if got := rec.Header().Get(k); got != v {
t.Errorf("%s: %q, want %q (headers %v)", k, got, v, rec.Header())
}
}
}
owner := "claude\x00" + user
told := map[string]string{
"anthropic-ratelimit-unified-5h-utilization": "0.42",
"anthropic-ratelimit-unified-5h-reset": strconv.FormatInt(five, 10),
"anthropic-ratelimit-unified-7d-utilization": "0.17",
"anthropic-ratelimit-unified-7d-reset": strconv.FormatInt(week, 10),
}
for _, stream := range []bool{true, false} {
t.Run("stream="+strconv.FormatBool(stream), func(t *testing.T) {
// the turn tells it
says(ask("Claude Code", owner, stream, append([]string{limits}, answer...)), told)
// a turn that tells nothing says what is kept
says(ask("Claude Code", owner, stream, answer), told)
})
}
// another account's reply says nothing of this one's
if rec := ask("Claude Code", "claude\x00other-"+user, true, answer); rec.Header().Get("anthropic-ratelimit-unified-5h-utilization") != "" {
t.Errorf("another account's reply says %v", rec.Header())
}
// another agent's run (Codex's, Cursor's) has no Claude allowance to say
if rec := ask("Agent", owner, true, answer); rec.Header().Get("anthropic-ratelimit-unified-5h-utilization") != "" {
t.Errorf("another agent's reply says %v", rec.Header())
}
}
// Through the gateway as it runs, to a Claude Code signed in to the
// account and answering as Claude Code does, streamed or not: the reply's
// headers carry what its rate_limit_event said, past the writer that holds
// a reply while another account may still take over.
func TestClaudeAllowanceHeadersThroughTheGateway(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("a shell script stands in for Claude Code")
}
home := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("USERPROFILE", home)
t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config"))
t.Setenv("XDG_CACHE_HOME", filepath.Join(home, ".cache"))
os.MkdirAll(filepath.Join(home, ".claude"), 0o755)
os.WriteFile(filepath.Join(home, ".claude", ".credentials.json"), mustJSON(map[string]any{"claudeAiOauth": map[string]any{
"accessToken": "tok-me", "refreshToken": "r-me", "expiresAt": time.Now().Add(24 * time.Hour).UnixMilli(), "subscriptionType": "pro"}}), 0o600)
os.WriteFile(filepath.Join(home, ".claude.json"), mustJSON(map[string]any{
"oauthAccount": map[string]any{"emailAddress": "hud@example.com", "accountUuid": "u-hud"}}), 0o600)
five := strconv.FormatInt(time.Now().Add(2*time.Hour).Unix(), 10)
week := strconv.FormatInt(time.Now().Add(5*24*time.Hour).Unix(), 10)
dir := t.TempDir()
testenv.Program(t, filepath.Join(dir, "claude"), `#!/bin/sh
if [ "$1" = auth ]; then echo '{"loggedIn":true,"authMethod":"claude.ai","apiProvider":"firstParty","email":"hud@example.com","subscriptionType":"pro"}'; exit 0; fi
while read -r line; do
echo '{"type":"rate_limit_event","rate_limit_info":{"status":"allowed","isUsingOverage":false,"unifiedWindows":{"five_hour":{"utilization":0.63,"resetsAt":`+five+`},"seven_day":{"utilization":0.08,"resetsAt":`+week+`}}}}'
echo '{"type":"stream_event","event":{"type":"message_start","message":{"id":"m","model":"claude-sonnet-5","usage":{"input_tokens":1}}}}'
echo '{"type":"stream_event","event":{"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"pong"}}}'
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":"pong"}'
done
`)
t.Setenv("PATH", dir+string(os.PathListSeparator)+os.Getenv("PATH"))
provider.ForgetAccounts()
t.Cleanup(provider.ForgetAccounts)
s := New()
t.Cleanup(s.subscription.abortAll)
srv := httptest.NewServer(s.Handler())
defer srv.Close()
for _, stream := range []string{"true", "false"} {
res, err := http.Post(srv.URL+"/v1/messages", "application/json", strings.NewReader(
`{"model":"claude/claude-sonnet-5","max_tokens":50,"stream":`+stream+`,"messages":[{"role":"user","content":"ping `+stream+`"}]}`))
if err != nil {
t.Fatal(err)
}
b, _ := io.ReadAll(res.Body)
res.Body.Close()
if res.StatusCode != 200 || !strings.Contains(string(b), "pong") {
t.Fatalf("stream=%s: %d %s", stream, res.StatusCode, b)
}
for k, v := range map[string]string{
"anthropic-ratelimit-unified-5h-utilization": "0.63",
"anthropic-ratelimit-unified-5h-reset": five,
"anthropic-ratelimit-unified-7d-utilization": "0.08",
"anthropic-ratelimit-unified-7d-reset": week,
} {
if got := res.Header.Get(k); got != v {
t.Errorf("stream=%s: %s %q, want %q (headers %v)", stream, k, got, v, res.Header)
}
}
}
}