Files
magpie/internal/gateway/claude_trust_test.go
T
yetone 82403f9f6d claude: /usage is read through -p, never the interactive mode, which opened idle accounts' 5-hour window (#5582)
Since v0.1.2635 (ccd746e03), when the interactive mode is on (the
default), magpie read the plan's allowance by starting the interactive
Claude Code in its helper and typing /usage there (claudeUsageTUI). The
interactive Claude Code 2.1.316 sends a model request as it starts,
before anything is typed. That is probeQuotaStatus: haiku, max_tokens 1,
content "quota". It sends it unless CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC
is set, and that variable also keeps /usage from loading. The request
counts, so on an account with no 5-hour window it started one. The
reporter's reads came at magpie's start and when a window ended, and
each opened a window with warm-up off. Their counterexample (11-01,
v0.1.2433, before ccd746e03) read /usage through -p and opened nothing.

Recorded (sandbox HOME and CLAUDE_CONFIG_DIR, a fake sign-in, the real
Claude Code 2.1.316 behind a recording proxy that answers everything
itself, so no account was spent):
- magpie's interactive args and env, in a pty. At start, before /usage
  was typed:
  POST api.anthropic.com /v1/messages?beta=true
  {"model":"claude-haiku-4-5-20251001","max_tokens":1,
   "messages":[{"role":"user","content":"quota"}],...}
  then GET /api/oauth/usage once /usage was typed.
- The same with CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC=1: no POST, but
  no /api/oauth/usage either, and the panel says "Failed to load usage
  data". This can't be the fix.
- `claude -p /usage` with claudeUsageArgs and claudeUsageEnv: no
  /v1/messages at all, only GET /api/oauth/usage (plus profile and
  bootstrap GETs). It printed "You are currently using your subscription
  to power your Claude Code usage" / "Current session: 13% used · resets
  Nov 7 at 4pm (Asia/Shanghai)" / "Current week (all models): 4% used ·
  ...". ccd746e03 said -p's /usage tells only the session's cost. In
  2.1.316's source that is only for a sign-in without a subscription or
  the user:profile scope, such as a setup-token, which provider never
  reads (errClaudeSetupToken), or an API key.

Fix: claudeUsage always runs `claude -p /usage`, with the interactive
mode on or off. claudeUsageTUI and claudeTUIUsageArgs are gone. This is
the one choke point (provider.UsageClaudeVia -> runClaudeUsage) for
every magpie read of Claude usage: the signed-in account's card,
saved accounts (readSavedClaudeUsage), the 8-day keep-alive, the
used-up switch's spare reads and refreshes. The other interactive
starts are the bridge's turns, the warm-up and the model test. Each one
sends a model request the user asked for, and each runs with
cleanClaudeEnv's CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC=1, so the
startup probe isn't added there. Not changed: claudetui still takes a
typed /usage, and magpie no longer sends it.

Docs: docs/subsystems/claude-subscription-bridge.md says why and
removes the wrong -p claim. The comments in claude_warmup.go, tui.go
and claude_usage_cli.go are updated to match.

Tests: TestClaudeUsageStartsNoInteractiveRun replaces
TestClaudeUsageTUIOn, TestClaudeUsageTUIWorksInMagpiesFolder and
TestClaudeUsageTUISaysItsDeadline. With the interactive mode on, it
checks that a read of the signed-in account and of a saved one starts
no helper, runs -p /usage, and leaves the traffic variable off. The
"usage" rows of TestClaudeOneOffTrustsMagpiesFolder and
TestClaudeOneOffFailureLogged are removed, since a /usage read is no
longer an interactive one-off run. Their "test" rows stay.

Run (mote, at this tree on origin/main 665b9aff1; after the rebase on 9e6687e3a, vet and claudetui plus gateway's ClaudeUsage|OneOff|Trust|TUI tests again: ok):
- gofmt -l internal/gateway internal/provider internal/claudetui: clean.
- go vet for darwin, and for linux and windows with -tags nogui:
  ./internal/gateway ./internal/provider ./internal/claudetui ok.
- On mote: go test -count=1 -timeout 45m ./internal/provider ./internal/claudetui: ok (114s, 110s);
  go test -run 'Claude|Usage|Warm|Trust|OneOff|TUI|Shim|Guard|Loop|Budget' ./internal/gateway: ok (89s).

Broken by hand: with HEAD's claude_warmup.go (its claudeTUIOn branch
back) and the new test, TestClaudeUsageStartsNoInteractiveRun went red:
"a /usage read started the interactive Claude Code: claude-tui-helper
.../claude -p --output-format stream-json --input-format stream-json ...".

Not tried: a real account's -p /usage on this build. The windows output
above comes from the recorder's fake usage reply, and the reporter's
v0.1.2433 readings came from -p. Not tried: an Enterprise account whose
usage reply leaves the windows out. -p then prints only the
subscription line, which reads as "temporarily unavailable" and keeps
the last reading, where the interactive panel had said no window has
started. Not run on Windows or in WSL. The -p read there is the same code
(claudeUsage); the interactive read had run on Windows too.
Not run: the full ./... suite (mote's suite.lock had four full runs
queued).
2026-11-07 11:52:30 +08:00

220 lines
7.5 KiB
Go

package gateway
import (
"context"
"encoding/json"
"os"
"path/filepath"
"runtime"
"slices"
"strings"
"testing"
"time"
"github.com/yetone/magpie/internal/claudetui"
"github.com/yetone/magpie/internal/provider"
"github.com/yetone/magpie/internal/testenv"
"github.com/yetone/magpie/internal/wslrun"
)
// savedClaudeDir is a saved Claude account's config directory, as
// magpie keeps one (provider's claudeAccountDir), in a home of the
// test's own.
func savedClaudeDir(t *testing.T) string {
t.Helper()
home := t.TempDir()
testenv.SetHome(t, home)
t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config"))
dir := filepath.Join(filepath.Dir(provider.Path()), "claude-accounts", "0123456789abcdef")
if err := os.MkdirAll(dir, 0o700); err != nil {
t.Fatal(err)
}
// a saved account as magpie writes it: onboarded, no projects
if err := os.WriteFile(filepath.Join(dir, ".claude.json"), []byte(`{"hasCompletedOnboarding": true}`), 0o600); err != nil {
t.Fatal(err)
}
return dir
}
// trustedIn is the projects .claude.json b records as trusted.
func trustedIn(t *testing.T, b []byte) []string {
t.Helper()
var m struct {
Projects map[string]struct {
Trusted bool `json:"hasTrustDialogAccepted"`
} `json:"projects"`
}
if err := json.Unmarshal(b, &m); err != nil {
t.Fatalf("%v: %s", err, b)
}
var out []string
for k, p := range m.Projects {
if p.Trusted {
out = append(out, k)
}
}
slices.Sort(out)
return out
}
// claudeKey is the key Claude Code 2.1.316 looks folder work up by: the
// path its links lead to, with forward slashes on Windows.
func claudeKey(work string) string { return filepath.ToSlash(evalSymlinks(work)) }
// mydosu's saved account (#4830, Windows, Claude Code 2.1.316) was asked
// whether to trust magpie's folder on every /usage read and every test,
// and each waited out its caller's time on the question (a /usage read
// runs as -p now, #5582, which asks nothing about trust). Before a test
// runs, the account's .claude.json says the folder is trusted, under the
// path Claude Code runs in, which a Mac's getcwd gives with its links
// resolved (/private/var/… for /var/…).
func TestClaudeOneOffTrustsMagpiesFolder(t *testing.T) {
for _, c := range []struct {
name string
run func(ctx context.Context, cfg string) error
say string
}{
{"test", func(ctx context.Context, cfg string) error { return askClaude(ctx, cfg, "haiku") }, `"result":"Hi!"`},
} {
t.Run(c.name, func(t *testing.T) {
fakeWarmClaude(t, `{}`, 0)
claudeTUIRuns = true
t.Cleanup(func() { claudeTUIRuns = false })
t.Setenv("TMPDIR", t.TempDir())
cfg := savedClaudeDir(t)
seen := filepath.Join(t.TempDir(), "seen")
helper := filepath.Join(t.TempDir(), "magpie")
// what the account's file says as Claude Code starts
testenv.Program(t, helper, `#!/bin/sh
[ "$1" = "`+claudetui.Command+`" ] || exit 9
cat "$CLAUDE_CONFIG_DIR/.claude.json" > '`+seen+`'
printf '%s\n' '{"type":"result","subtype":"success","is_error":false,`+c.say+`}'
cat > /dev/null
`)
claudeTUIHelper = helper
t.Cleanup(func() { claudeTUIHelper = "" })
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
defer cancel()
if err := c.run(ctx, cfg); err != nil {
t.Fatal(err)
}
work, err := claudeWorkDir()
if err != nil {
t.Fatal(err)
}
b, err := os.ReadFile(seen)
if err != nil {
t.Fatal(err)
}
got := trustedIn(t, b)
if !slices.Contains(got, claudeKey(work)) {
t.Fatalf("Claude Code started with %v trusted, not magpie's folder %s: %s", got, claudeKey(work), b)
}
if !strings.Contains(string(b), `"hasCompletedOnboarding": true`) {
t.Fatalf("the account's other keys went: %s", b)
}
})
}
}
// The account Claude Code is signed in to (configDir "") keeps its own
// ~/.claude.json, which magpie never writes, and a folder of the run's
// own, made when magpie's can't be used, isn't recorded: each would
// leave one more trusted project behind.
func TestClaudeTrustWorkOnlyMagpiesFolderInSavedAccounts(t *testing.T) {
tmp := t.TempDir()
for _, v := range []string{"TMPDIR", "TMP", "TEMP"} {
t.Setenv(v, tmp)
}
cfg := savedClaudeDir(t)
home, _ := os.UserHomeDir()
t.Setenv("CLAUDE_CONFIG_DIR", filepath.Join(home, ".claude"))
work, err := claudeWorkDir()
if err != nil {
t.Fatal(err)
}
trustWork(claudeCLI{}, "", work)
for _, f := range []string{filepath.Join(home, ".claude.json"), filepath.Join(home, ".claude", ".claude.json")} {
if _, err := os.Stat(f); !os.IsNotExist(err) {
t.Errorf("%s was written", f)
}
}
own := t.TempDir()
trustWork(claudeCLI{}, cfg, own)
b, _ := os.ReadFile(filepath.Join(cfg, ".claude.json"))
if got := trustedIn(t, b); len(got) != 0 {
t.Errorf("a folder of the run's own was recorded: %v", got)
}
trustWork(claudeCLI{}, cfg, work)
b, _ = os.ReadFile(filepath.Join(cfg, ".claude.json"))
if got := trustedIn(t, b); !slices.Contains(got, claudeKey(work)) {
t.Errorf("magpie's folder wasn't recorded: %v", got)
}
}
// Claude Code 2.1.316 keys a project on Windows by its path with forward
// slashes (C:/Users/<me>/AppData/Local/Temp/magpie-claude-work): an entry
// under the backslashed path is never found, and the question is asked.
func TestClaudeTrustKeysAsClaudeCodeKeys(t *testing.T) {
old := trustGOOS
t.Cleanup(func() { trustGOOS = old })
trustGOOS = "windows"
// mydosu's folder, a name in NFD as a file system may give it
got := claudeTrustKeys(claudeCLI{}, "C:\\Users\\Me\u0301\\AppData\\Local\\Temp\\magpie-claude-work")
if want := "C:/Users/M\u00e9/AppData/Local/Temp/magpie-claude-work"; !slices.Contains(got, want) {
t.Errorf("keys %q, want %q among them", got, want)
}
for _, k := range got {
if strings.Contains(k, `\`) {
t.Errorf("key %q has a backslash", k)
}
}
trustGOOS = "linux"
if runtime.GOOS != "windows" {
if got := claudeTrustKeys(claudeCLI{}, `/tmp/a\b`); !slices.Contains(got, `/tmp/a\b`) {
t.Errorf("a backslash in a Unix name was changed: %v", got)
}
}
got = claudeTrustKeys(claudeCLI{wsl: &wslrun.Tool{Mount: "/mnt/"}}, `C:\Users\me\AppData\Local\Temp\magpie-claude-work`)
if len(got) != 1 || got[0] != "/mnt/c/Users/me/AppData/Local/Temp/magpie-claude-work" {
t.Errorf("Claude Code in WSL runs in its mount of the folder, keys %v", got)
}
}
// A saved account's bridge run in magpie's folder starts with the folder
// trusted too: the bridge's runs work there whenever the caller's own
// folder isn't one here.
func TestClaudeBridgeTrustsMagpiesFolderForSavedAccount(t *testing.T) {
h := tuiScriptHarness(t, `cat "$CLAUDE_CONFIG_DIR/.claude.json" > $log.seen`)
t.Setenv("XDG_CONFIG_HOME", filepath.Join(os.Getenv("HOME"), ".config"))
cfg := filepath.Join(filepath.Dir(provider.Path()), "claude-accounts", "0123456789abcdef")
if err := os.MkdirAll(cfg, 0o700); err != nil {
t.Fatal(err)
}
req := &Request{Messages: []Message{{Role: "user", Parts: []Part{{Kind: Text, Text: "hello"}}}}}
run, _, err := h.s.subscription.start(context.Background(), req, "claude-sonnet-5", cfg, "", nil)
if err != nil {
t.Fatal(err)
}
if !run.tui {
t.Fatal("the run wasn't interactive")
}
var b []byte
eventually(t, "the run read its account's file", func() bool {
entries, _ := os.ReadDir(h.logs)
for _, e := range entries {
if strings.HasSuffix(e.Name(), ".seen") {
b, _ = os.ReadFile(filepath.Join(h.logs, e.Name()))
}
}
return len(b) > 0
})
work, err := claudeWorkDir()
if err != nil {
t.Fatal(err)
}
if got := trustedIn(t, b); !slices.Contains(got, claudeKey(work)) {
t.Fatalf("the run started with %v trusted, not magpie's folder %s", got, claudeKey(work))
}
}