From 9c377231107575ec6e06fb7558ea6c5c4c95bab4 Mon Sep 17 00:00:00 2001 From: yetone Date: Sun, 11 Oct 2026 03:28:45 +0800 Subject: [PATCH] provider: a test binary keeps no CLI identity on disk, so a late ask can't write into the next test's folder (#1538, sibling of #1524) TestDshHomePickSurvivesSyncAndEffort failed under full-suite load and passed alone. The cause is cliIdentity: a look starts an ask of the CLI (a failing testenv stand-in) and, once the answer is a minute old, a refresh in a goroutine. keep() writes cli-identity.json under the XDG_CONFIG_HOME current when it finishes, so an ask slow under load, still going when its test ended, wrote into the config folder of the test running then while t.TempDir's cleanup was removing it: "TempDir RemoveAll cleanup: ... directory not empty". 4ee3a76b fixed this for internal/gateway alone (KeepCLIIdentities=false in its TestMain). internal/agent, and every other package whose tests read the accounts (., access, backup, davsync, gui, profile, sessions, stats, tui, usage), still could. keep() now skips in any binary testenv isolated (MAGPIE_TEST_SANDBOX, as internal/proc's sandboxVar reads it), and the provider tests of the file itself turn keeping back on with keepIdentities(t). The gateway's own switch and its test are gone; TestCLIIdentityNotKeptInATestBinary covers every package. Ran: - Reproduced by injection (get refreshes on every look, 15ms sleep after c.ask()), agent Dsh tests -count=3: old code red 2 of 6 runs with the exact TempDir cleanup error; new code 0 of 6. - Broke by hand (keepsIdentities() returns true): TestCLIIdentityNotKeptInATestBinary red, "written in a test binary". - go test -count=1 ./internal/provider ./internal/agent ./internal/gateway: ok - gofmt -l clean; go vet provider, agent, gateway on darwin, GOOS=linux -tags nogui, GOOS=windows: clean. --- internal/gateway/main_test.go | 14 ------------- internal/provider/cli_identity.go | 25 ++++++++++++++++-------- internal/provider/cli_identity_test.go | 26 ++++++++++++++++++------- internal/provider/cursor_status_test.go | 1 + internal/provider/devin_test.go | 1 + 5 files changed, 38 insertions(+), 29 deletions(-) diff --git a/internal/gateway/main_test.go b/internal/gateway/main_test.go index c4b0fcf9a..cb6e2ab24 100644 --- a/internal/gateway/main_test.go +++ b/internal/gateway/main_test.go @@ -7,7 +7,6 @@ import ( "testing" "github.com/yetone/magpie/internal/agentenv" - "github.com/yetone/magpie/internal/provider" "github.com/yetone/magpie/internal/testenv" ) @@ -77,22 +76,9 @@ func isolatedTests(m *testing.M) (int, error) { // no route is written to disk behind a test's back; the history's own // tests call saveRoute themselves keepRoutes = false - // nor a CLI's identity: a request's look at Cursor's or Devin's account - // asks the stand-in behind it, and the answer was kept in the config - // folder of whichever test was running then, as it was removed (#1524) - provider.KeepCLIIdentities = false // no test asks a vendor: a provider with a real base URL and a made-up // key would, in the background (a plan key's windows, #1016), and a // plugin host's first start fetched models.dev testenv.Offline() return m.Run(), nil } - -// The package's tests keep no CLI identity on disk: an ask runs behind the -// request that started it, and one still going when its test ended wrote -// cli-identity.json into the config folder being removed (#1524). -func TestNoCLIIdentityKeptInTests(t *testing.T) { - if provider.KeepCLIIdentities { - t.Fatal("TestMain left provider.KeepCLIIdentities on") - } -} diff --git a/internal/provider/cli_identity.go b/internal/provider/cli_identity.go index eff99964d..07280819f 100644 --- a/internal/provider/cli_identity.go +++ b/internal/provider/cli_identity.go @@ -46,13 +46,22 @@ type keptIdentity struct { var identityFile sync.Mutex -// KeepCLIIdentities is whether the answers are kept on disk. Off in the -// gateway's tests: an ask runs behind the look that started it, and one -// still going when its test ended (a stand-in CLI slow under load) wrote -// cli-identity.json into the test's config folder as that was being -// removed, failing the test's TempDir cleanup (#1524). Set before the -// tests start, never while asks run. -var KeepCLIIdentities = true +// testSandbox is testenv.Marker, which provider can't import: set in a test +// binary testenv isolated. +const testSandbox = "MAGPIE_TEST_SANDBOX" + +// keepInSandbox has the answers kept on disk in a test binary too, for the +// tests of that file itself. +var keepInSandbox = false + +// keepsIdentities says whether the answers are kept on disk: always, except +// in a test binary testenv isolated. There an ask runs behind the look that +// started it, and one still going when its test ended (a stand-in CLI slow +// under load) wrote cli-identity.json into the config folder of whichever +// test was running then, as that was being removed, failing its TempDir +// cleanup: internal/gateway's (#1524), then internal/agent's (#1538), and +// any package whose tests read the accounts could. +func keepsIdentities() bool { return keepInSandbox || os.Getenv(testSandbox) == "" } func identityPath() string { return filepath.Join(filepath.Dir(Path()), "cli-identity.json") } @@ -65,7 +74,7 @@ func readIdentities() map[string]keptIdentity { } func (c *cliIdentity) keep() { - if !KeepCLIIdentities { + if !keepsIdentities() { return } identityFile.Lock() diff --git a/internal/provider/cli_identity_test.go b/internal/provider/cli_identity_test.go index 937eb185c..d90e4fd4d 100644 --- a/internal/provider/cli_identity_test.go +++ b/internal/provider/cli_identity_test.go @@ -8,8 +8,16 @@ import ( "time" ) +// keepIdentities has this test keep the answers on disk, as magpie does +// outside a test binary. +func keepIdentities(t *testing.T) { + keepInSandbox = true + t.Cleanup(func() { keepInSandbox = false }) +} + func TestCLIIdentityKeptAcrossStarts(t *testing.T) { t.Setenv("XDG_CONFIG_HOME", t.TempDir()) + keepIdentities(t) var asked atomic.Int32 answer := func() (string, string, bool, error) { asked.Add(1); return "me@example.com", "Pro", true, nil } exe := func() string { return "/bin/sh" } @@ -68,6 +76,7 @@ func TestCLIIdentityKeptAcrossStarts(t *testing.T) { func TestCLIIdentityKeptIgnoredWithoutTheCLI(t *testing.T) { t.Setenv("XDG_CONFIG_HOME", t.TempDir()) + keepIdentities(t) (&cliIdentity{name: "x", exe: func() string { return "/bin/sh" }, ask: func() (string, string, bool, error) { return "me@example.com", "", true, nil }}).get() gone := &cliIdentity{name: "x", exe: func() string { return "" }, ask: func() (string, string, bool, error) { return "", "", false, nil }} if u, _, ok := gone.get(); ok || u != "" { @@ -79,6 +88,7 @@ func TestCLIIdentityKeptIgnoredWithoutTheCLI(t *testing.T) { // nobody is signed in until it answers, and then its answer is served func TestCLIIdentityFirstAskBounded(t *testing.T) { t.Setenv("XDG_CONFIG_HOME", t.TempDir()) + keepIdentities(t) old := firstAsk firstAsk = 100 * time.Millisecond t.Cleanup(func() { firstAsk = old }) @@ -117,6 +127,7 @@ func TestCLIIdentityFirstAskBounded(t *testing.T) { // rather than waiting for it again func TestCLIIdentitySignedOutKept(t *testing.T) { t.Setenv("XDG_CONFIG_HOME", t.TempDir()) + keepIdentities(t) (&cliIdentity{name: "x", exe: func() string { return "/bin/sh" }, ask: func() (string, string, bool, error) { return "", "", false, nil }}).get() if _, found := readIdentities()["x"]; !found { t.Fatal("a signed-out answer wasn't kept") @@ -195,19 +206,20 @@ func TestCLIIdentityFailingAskedLessOften(t *testing.T) { } } -// With KeepCLIIdentities off, an answer is served but nothing is written: -// the gateway's tests turn it off, as an ask still going when a test ended -// wrote into the config folder being removed (#1524). -func TestCLIIdentityNotKeptWhenOff(t *testing.T) { +// In a test binary testenv isolated an answer is served but nothing is +// written: an ask still going when a test ended wrote into the config +// folder being removed (#1524, #1538). +func TestCLIIdentityNotKeptInATestBinary(t *testing.T) { + if os.Getenv(testSandbox) == "" { + t.Fatal("provider's tests run without testenv's sandbox") + } dir := t.TempDir() t.Setenv("XDG_CONFIG_HOME", dir) - KeepCLIIdentities = false - t.Cleanup(func() { KeepCLIIdentities = true }) c := &cliIdentity{name: "x", exe: func() string { return "/bin/sh" }, ask: func() (string, string, bool, error) { return "me@example.com", "Pro", true, nil }} if u, _, ok := c.get(); !ok || u != "me@example.com" { t.Fatalf("get: %q %v", u, ok) } if es, _ := os.ReadDir(dir); len(es) != 0 { - t.Fatalf("written with KeepCLIIdentities off: %v", es) + t.Fatalf("written in a test binary: %v", es) } } diff --git a/internal/provider/cursor_status_test.go b/internal/provider/cursor_status_test.go index 920b295e1..cfc514e11 100644 --- a/internal/provider/cursor_status_test.go +++ b/internal/provider/cursor_status_test.go @@ -15,6 +15,7 @@ import ( // sure nobody is signed in still signs it out func TestCLIIdentityUnsureKeepsAnswer(t *testing.T) { t.Setenv("XDG_CONFIG_HOME", t.TempDir()) + keepIdentities(t) c := &cliIdentity{name: "x", exe: func() string { return "/bin/sh" }, ask: func() (string, string, bool, error) { return "me@example.com", "Pro", true, nil }} if _, _, ok := c.get(); !ok { diff --git a/internal/provider/devin_test.go b/internal/provider/devin_test.go index 2ba0d6cd7..8218dfe20 100644 --- a/internal/provider/devin_test.go +++ b/internal/provider/devin_test.go @@ -440,6 +440,7 @@ func TestAskDevinStatus(t *testing.T) { // that says nobody (or keeps no key) drops it, on disk too func TestDevinStatusKeepsTheAccount(t *testing.T) { home := claudeHome(t) + keepIdentities(t) t.Setenv("XDG_DATA_HOME", filepath.Join(home, "data")) exe := filepath.Join(home, "devin") fakeDevin(t, exe)