gateway: a test's config folder isn't written into by a CLI identity asked behind its request (#1524)
TestEncodedBodyInflatedPastLimit failed under load at its TempDir cleanup
("unlinkat .../001/magpie: directory not empty"). The server wasn't the
writer: New() starts no goroutine, and httptest's Close waits for the
handlers. The writer is provider's cliIdentity. Reading the accounts
reads Cursor's and Devin's identity. In the gateway's tests, testenv's
stand-in cursor-agent and devin are on PATH, so their CLIs count as
installed. After the first answer, a look older than a minute asks the
CLI again in a goroutine and doesn't wait for it. When the stand-in
answers signed out, keep() writes cli-identity.json (MkdirAll, then a
temp file renamed in) under the XDG_CONFIG_HOME of whatever test is
running then. Under load the stand-in takes seconds, so the write lands
as that test's TempDir is removed.
Found by injecting a delay, not by rerunning. With get() refreshing at
every look and a 15ms sleep after the ask, a -count=3 run of
TestEncodedBodyInflatedPastLimit failed 3 subtests with the exact error
from the issue (/v1/responses/zstd, /v1/chat/completions/zstd,
/v1/videos/gzip). With this change and the same injection,
-count=5 of TestEncodedBody*, TestUnknownBodyEncodingRefused and
TestKeyLimitReservesDecodedBody passed.
provider.KeepCLIIdentities (default true) now decides whether the
answers are kept on disk. The gateway's TestMain turns it off, as it
already turns off keepRoutes for the same reason. Production doesn't
change. TestNoCLIIdentityKeptInTests goes red if TestMain leaves it on.
TestCLIIdentityNotKeptWhenOff checks that an answer is still served and
nothing is written.
The same pattern: every gateway test that calls New() and never stops it
was a suspect. Server has no Stop and New() starts nothing, so the pattern
itself is harmless; the background write was the cause wherever a
request looks at the accounts. provider's own tests still keep
identities on, since several of them test the file; they haven't failed
this way.
Run: go test -tags nogui -count=1 -timeout 40m ./internal/gateway
./internal/provider (ok); gofmt -l; GOOS=linux and GOOS=windows go vet
-tags nogui ./internal/gateway ./internal/provider.
This commit is contained in:
@@ -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")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user