Files
magpie/internal/gateway/claude_warmup_test.go
T
yetone 1e88598384 gateway: a Claude Code test or /usage run that magpie gives up on ends with what it started, npm's node on Windows too (#1894, heng91)
What changed: magpie runs Claude Code one-off for an account's model test
(askClaude) and for /usage (claudeUsage, read in the background for each
Claude account). Both ran through CommandContext, whose kill on the
timeout ends only the process magpie started. On Windows an npm install's
claude is claude.cmd: cmd.exe running node, and only cmd.exe was killed,
so node went on running with nothing to end it. Elsewhere a CLI that is a
wrapper script left its child the same way.

Now both run in a proc.Tree (a job on Windows, a session elsewhere), as
the Claude subscription's runs do: what is left once the head ends is
ended with it, and on Windows the job ends it if magpie exits mid-run.

Run:
- go test -tags nogui -count=3 -run 'ClaudeRunEnds|WarmClaude|ClaudeUsage|ClaudeTest'
  ./internal/gateway: ok. New TestClaudeRunEndsWhatItStarted: a script
  claude that starts a child and hangs; once it has started the child the
  run is given up on, and the child must be gone. With cmd.Run() put back
  by hand, both subtests (test, usage) go red with the child running.
- gofmt -l; GOOS=linux and GOOS=windows go vet -tags nogui: clean.

Not run on the Windows box: the job path is the one subscription runs
already use. Not found to be the reporter's processes: magpie's other
Claude Code processes are the subscription runs kept for a conversation's
next turn (at most 6, at most an hour, by design).

Reported-by: heng91
2026-10-13 13:03:26 +08:00

271 lines
10 KiB
Go

package gateway
import (
"context"
"encoding/json"
"os"
"os/exec"
"path/filepath"
"runtime"
"strconv"
"strings"
"testing"
"time"
"github.com/yetone/magpie/internal/provider"
"github.com/yetone/magpie/internal/testenv"
)
// fakeWarmClaude is a claude that writes what it was run with to log and
// answers as out says.
func fakeWarmClaude(t *testing.T, out string, code int) string {
t.Helper()
if runtime.GOOS == "windows" {
t.Skip("a shell script stands in for Claude Code")
}
dir := t.TempDir()
log := filepath.Join(dir, "log")
script := "#!/bin/sh\n" +
"{ printf 'args:'; for a in \"$@\"; do printf '[%s]' \"$a\"; done; echo; " +
"echo \"dir:$CLAUDE_CONFIG_DIR\"; echo \"base:$ANTHROPIC_BASE_URL\"; echo \"pwd:$(pwd)\"; printf 'stdin:'; cat; echo; " +
"echo \"traffic:$CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC\"; echo \"telemetry:$DISABLE_TELEMETRY\"; " +
"echo \"errors:$DISABLE_ERROR_REPORTING\"; echo \"updater:$DISABLE_AUTOUPDATER\"; } > " + log + "\n" +
"echo '" + out + "'\nexit " + string(rune('0'+code)) + "\n"
testenv.Program(t, filepath.Join(dir, "claude"), script)
t.Setenv("PATH", dir+string(os.PathListSeparator)+os.Getenv("PATH"))
t.Setenv("ANTHROPIC_BASE_URL", "http://127.0.0.1:3425")
return log
}
func TestWarmClaudeAsksClaudeCodeOnce(t *testing.T) {
log := fakeWarmClaude(t, `{"type":"result","is_error":false,"result":"Hi!"}`, 0)
t.Setenv("CLAUDE_CONFIG_DIR", "/the/users/own")
if err := warmClaude(context.Background(), "/saved/account"); err != nil {
t.Fatal(err)
}
b, _ := os.ReadFile(log)
got := string(b)
for _, want := range []string{"[-p]", "[--model][haiku]", "[--tools][]", "[--setting-sources][]", "[--no-session-persistence]",
"dir:/saved/account\n", "base:\n", "stdin:hi", "magpie-claude-"} {
if !strings.Contains(got, want) {
t.Errorf("run lacks %q:\n%s", want, got)
}
}
// its folder goes with it
for _, l := range strings.Split(got, "\n") {
if d, ok := strings.CutPrefix(l, "pwd:"); ok {
if _, err := os.Stat(d); !os.IsNotExist(err) {
t.Errorf("%s left behind", d)
}
}
}
// the account Claude Code is signed in to is its own, in its own
// config directory
if err := warmClaude(context.Background(), ""); err != nil {
t.Fatal(err)
}
if b, _ := os.ReadFile(log); !strings.Contains(string(b), "dir:/the/users/own\n") {
t.Errorf("another config directory given for Claude Code's own account:\n%s", b)
}
}
func TestWarmClaudeSaysWhyNot(t *testing.T) {
fakeWarmClaude(t, `{"type":"result","is_error":true,"result":"Invalid API key · Please run /login"}`, 1)
err := warmClaude(context.Background(), "")
if err == nil || !strings.Contains(err.Error(), "Please run /login") {
t.Fatalf("got %v", err)
}
fakeWarmClaude(t, `not json`, 2)
if err := warmClaude(context.Background(), ""); err == nil || !strings.Contains(err.Error(), "not json") {
t.Fatalf("got %v", err)
}
}
// A Claude account's model test runs Claude Code at the model tested.
func TestClaudeTestRunsClaudeCode(t *testing.T) {
log := fakeWarmClaude(t, `{"type":"result","is_error":false,"result":"Hi!"}`, 0)
p := provider.Provider{Name: "Claude", Account: &provider.Account{Agent: "claude"}}
r := p.TestModels(context.Background(), []string{"claude-sonnet-4-5"})
if len(r) != 1 || !r[0].OK || r[0].Model != "claude-sonnet-4-5" {
t.Fatalf("result %+v", r)
}
b, _ := os.ReadFile(log)
if got := string(b); !strings.Contains(got, "[--model][claude-sonnet-4-5]") || !strings.Contains(got, "stdin:hi") {
t.Fatalf("run:\n%s", got)
}
}
// Claude's usage is Claude Code's own /usage, run with nothing of the
// user's settings and nothing kept, as the account it is signed in to.
func TestClaudeUsageRunsClaudeCode(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("a shell script stands in for Claude Code")
}
dir := t.TempDir()
log := filepath.Join(dir, "log")
out := filepath.Join(dir, "out")
text := "You are currently using your subscription\n\nCurrent session: 13% used · resets Oct 1 at 3:30pm (Asia/Shanghai)\n"
b, _ := json.Marshal(map[string]any{"type": "result", "is_error": false, "num_turns": 0, "result": text})
os.WriteFile(out, b, 0o644)
script := "#!/bin/sh\n" +
"{ printf 'args:'; for a in \"$@\"; do printf '[%s]' \"$a\"; done; echo; echo \"token:$CLAUDE_CODE_OAUTH_TOKEN\"; } > " + log + "\n" +
"cat " + out + "\n"
testenv.Program(t, filepath.Join(dir, "claude"), script)
t.Setenv("PATH", dir+string(os.PathListSeparator)+os.Getenv("PATH"))
t.Setenv("CLAUDE_CODE_OAUTH_TOKEN", "leaked")
got, err := claudeUsage(context.Background(), "")
if err != nil || got != text {
t.Fatalf("usage %q %v", got, err)
}
b, _ = os.ReadFile(log)
for _, want := range []string{"[-p][/usage]", "[--setting-sources][]", "[--strict-mcp-config]", "[--no-session-persistence]", "token:\n"} {
if !strings.Contains(string(b), want) {
t.Errorf("run lacks %q:\n%s", want, b)
}
}
}
// /usage runs without CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC, which keeps
// Claude Code from asking for the usage, unless the user set it; telemetry,
// error reporting and autoupdate stay off. The warm-up keeps it.
func TestClaudeUsageSendsItsRequest(t *testing.T) {
log := fakeWarmClaude(t, `{"type":"result","is_error":false,"result":"Current session: 13% used"}`, 0)
for _, k := range []string{"CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC", "DISABLE_TELEMETRY", "DISABLE_ERROR_REPORTING", "DISABLE_AUTOUPDATER"} {
t.Setenv(k, "")
}
if _, err := claudeUsage(context.Background(), ""); err != nil {
t.Fatal(err)
}
b, _ := os.ReadFile(log)
for _, want := range []string{"traffic:\n", "telemetry:1\n", "errors:1\n", "updater:1\n"} {
if !strings.Contains(string(b), want) {
t.Errorf("/usage run lacks %q:\n%s", want, b)
}
}
if err := warmClaude(context.Background(), ""); err != nil {
t.Fatal(err)
}
if b, _ := os.ReadFile(log); !strings.Contains(string(b), "traffic:1\n") {
t.Errorf("warm-up lacks CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC:\n%s", b)
}
t.Setenv("CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC", "1")
if _, err := claudeUsage(context.Background(), ""); err != nil {
t.Fatal(err)
}
if b, _ := os.ReadFile(log); !strings.Contains(string(b), "traffic:1\n") {
t.Errorf("/usage run dropped the user's CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC:\n%s", b)
}
}
func TestClaudeUsageRunsInSavedAccountDirectory(t *testing.T) {
log := fakeWarmClaude(t, `{"type":"result","is_error":false,"result":"Current session: 17% used"}`, 0)
t.Setenv("CLAUDE_CONFIG_DIR", "/the/users/own")
t.Setenv("CLAUDE_SECURESTORAGE_CONFIG_DIR", "/another/account")
got, err := claudeUsage(context.Background(), "/saved/account")
if err != nil || got != "Current session: 17% used" {
t.Fatalf("usage %q %v", got, err)
}
b, err := os.ReadFile(log)
if err != nil {
t.Fatal(err)
}
if !strings.Contains(string(b), "dir:/saved/account\n") || !strings.Contains(string(b), "[-p][/usage]") {
t.Fatalf("saved account's /usage ran in the wrong directory:\n%s", b)
}
if os.Getenv("CLAUDE_CONFIG_DIR") != "/the/users/own" || os.Getenv("CLAUDE_SECURESTORAGE_CONFIG_DIR") != "/another/account" {
t.Fatal("reading saved usage changed the caller's account directory")
}
}
// With Claude Code's verbose output on (its /config, kept in .claude.json),
// claude -p --output-format json prints every message as an array, the
// system init first and the result last (KevinXC on Discord: usage
// couldn't be read; the tooltip began [{"type":"system","subtype":"init")
func TestClaudeVerboseOutput(t *testing.T) {
text := "Current session: 13% used"
b, _ := json.Marshal([]map[string]any{
{"type": "system", "subtype": "init", "tools": []string{}},
{"type": "result", "subtype": "success", "is_error": false, "result": text},
})
fakeWarmClaude(t, string(b), 0)
got, err := claudeUsage(context.Background(), "")
if err != nil || got != text {
t.Fatalf("usage %q %v", got, err)
}
b, _ = json.Marshal([]map[string]any{
{"type": "system", "subtype": "init"},
{"type": "result", "is_error": true, "result": "Invalid API key · Please run /login"},
})
fakeWarmClaude(t, string(b), 1)
if err := warmClaude(context.Background(), ""); err == nil || !strings.Contains(err.Error(), "Please run /login") || strings.Contains(err.Error(), "init") {
t.Fatalf("got %v", err)
}
}
// heng91 on #1894: claude processes in Task Manager with no Claude session
// open. A test or /usage run of Claude Code that outlives its time is
// killed, and what it started goes with it: npm's claude.cmd is cmd.exe
// running node, and only cmd.exe was killed. A script stands in, which
// starts a child and hangs; once the run is given up on, the child is gone.
func TestClaudeRunEndsWhatItStarted(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("a shell script stands in for Claude Code")
}
for _, run := range []struct {
name string
f func(ctx context.Context) error
}{
{"test", func(ctx context.Context) error { return askClaude(ctx, "", "haiku") }},
{"usage", func(ctx context.Context) error { _, err := claudeUsage(ctx, ""); return err }},
} {
t.Run(run.name, func(t *testing.T) {
dir := t.TempDir()
pidFile := filepath.Join(dir, "child")
testenv.Program(t, filepath.Join(dir, "claude"), "#!/bin/sh\nsleep 300 </dev/null >/dev/null 2>&1 &\necho $! > "+pidFile+"\nsleep 300\n")
t.Setenv("PATH", dir+string(os.PathListSeparator)+os.Getenv("PATH"))
var pid int
t.Cleanup(func() {
if pid > 0 {
if p, err := os.FindProcess(pid); err == nil {
p.Kill()
}
}
})
// given up on once it has started its child, however busy the
// machine: a timeout that came first had nothing to leave behind
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
go func() {
for ctx.Err() == nil {
if b, _ := os.ReadFile(pidFile); strings.HasSuffix(string(b), "\n") {
cancel()
return
}
time.Sleep(10 * time.Millisecond)
}
}()
if err := run.f(ctx); err == nil {
t.Fatal("a run that never answered passed")
}
b, err := os.ReadFile(pidFile)
if err != nil {
t.Fatal(err)
}
pid, _ = strconv.Atoi(strings.TrimSpace(string(b)))
if pid <= 0 {
t.Fatalf("pid %q", b)
}
// gone, or a zombie waiting for init to reap it
for end := time.Now().Add(5 * time.Second); ; time.Sleep(20 * time.Millisecond) {
out, _ := exec.Command("ps", "-o", "stat=", "-p", strconv.Itoa(pid)).Output()
if st := strings.TrimSpace(string(out)); st == "" || strings.HasPrefix(st, "Z") {
break
}
if time.Now().After(end) {
t.Fatalf("what Claude Code started is still running (%d)", pid)
}
}
})
}
}