diff --git a/internal/gateway/main_test.go b/internal/gateway/main_test.go index cb6e2ab24..c4b0fcf9a 100644 --- a/internal/gateway/main_test.go +++ b/internal/gateway/main_test.go @@ -7,6 +7,7 @@ import ( "testing" "github.com/yetone/magpie/internal/agentenv" + "github.com/yetone/magpie/internal/provider" "github.com/yetone/magpie/internal/testenv" ) @@ -76,9 +77,22 @@ 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 ec78f71d8..eff99964d 100644 --- a/internal/provider/cli_identity.go +++ b/internal/provider/cli_identity.go @@ -46,6 +46,14 @@ 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 + func identityPath() string { return filepath.Join(filepath.Dir(Path()), "cli-identity.json") } func readIdentities() map[string]keptIdentity { @@ -57,6 +65,9 @@ func readIdentities() map[string]keptIdentity { } func (c *cliIdentity) keep() { + if !KeepCLIIdentities { + return + } identityFile.Lock() defer identityFile.Unlock() m := readIdentities() diff --git a/internal/provider/cli_identity_test.go b/internal/provider/cli_identity_test.go index 8397d9430..937eb185c 100644 --- a/internal/provider/cli_identity_test.go +++ b/internal/provider/cli_identity_test.go @@ -194,3 +194,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) { + 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) + } +}