gateway: a done route isn't written to disk in a test binary testenv isolated, so a late write can't refill a test's folder as it is removed (#1543)
trace.update runs `go saveRoute(c)` when a route ends. That write runs behind the request that ended the route, under XDG_CONFIG_HOME/magpie. When a test returns, its cleanups run in reverse: the env is put back, then the TempDir is removed. A saveRoute that had already taken its folder was still writing routing/<day>.jsonl into it while RemoveAll ran, which is CI's "TempDir RemoveAll cleanup: .../magpie: directory not empty" for internal/gui TestContextTextAPI (ubuntu 23:15Z and macOS 19:38Z on 10-12). Every package whose tests send a request through gateway.New() has the same writer: gui, tui, agent, davsync. Only the gateway's own TestMain turned it off (keepRoutes = false). The fix is in one shared place: keepsRoutes() is false when MAGPIE_TEST_SANDBOX (testenv.Marker) is set, which testenv.Run/Isolate and the gateway's TestMain set for every test binary. No test outside the gateway reads the routing history back; the gateway already had keepRoutes = false, and only the new test below turns it on. The gateway's two sightings (TestRequestTailAfterOldLimit at9c377231, TestLargeRequestEndpoints at2c75d898, both 10-10) are beforea349de82, which set the gateway's marker and stopped the late CLI-identity write (#1524); there keepRoutes was already false. Found by an injected delay, not by reruns: a sleep in saveRoute after `dir := HistoryDir()`, with a probe in TestContextTextAPI that checked for the folder after its cleanup, put routing/ back every run on the old code and never with the fix. TestNoRouteWrittenInATestSandbox: under the marker no route reaches disk; with the marker cleared one does (so the test fails if the writer moves and nothing is written either way). Broken by hand: keepsRoutes() returning keepRoutes alone makes it fail ("a route was written to .../magpie/routing"). Run (sandbox HOME, GOMODCACHE/GOCACHE pinned, -tags=nogui, load 25-40 from other sessions' suites, disk at times full): - go test ./... -timeout 45m -p 6: 46 packages ok; agent and gateway hit the 45m timeout in TestStashKeepsChangesMadeAtOnce and TestStandaloneNativeAccounts, and TestCodexHeardWithinItsIdleTimeout failed once. All three pass by name, -race -count=3. - go test ./internal/gateway/ ./internal/gui/ ./internal/usage/... -race -count=3 -timeout 60m: gui and the gateway failures were "no space left on device", a 6s bound at load 40 (TestProviderListReadsSettingsOnce) and timing asserts. Each failed test re-run by name with -race -count=3 passes: gateway 373s ok, gui (with TestContextTextAPI) ok. usage's TestRecordAlwaysCarriesCacheTokens fails on any -count>1 on main as well (it reads the log its previous run wrote): filed #1897, not fixed here. - gofmt -l internal/: clean. GOOS=linux and GOOS=windows go vet ./...: clean.
This commit is contained in:
@@ -40,6 +40,19 @@ func HistoryDir() string { return filepath.Join(filepath.Dir(provider.Path()), "
|
||||
// routing directory back into its temporary HOME as that is removed.
|
||||
var keepRoutes = true
|
||||
|
||||
// testSandbox is testenv.Marker, which gateway can't import outside its
|
||||
// tests: set in a test binary testenv isolated.
|
||||
const testSandbox = "MAGPIE_TEST_SANDBOX"
|
||||
|
||||
// keepsRoutes says whether a done route is written to disk: never in a test
|
||||
// binary testenv isolated. The write runs behind the request that ended the
|
||||
// route, so one still going when its test ended put routing/ back into the
|
||||
// test's config folder as that was removed, failing its TempDir cleanup
|
||||
// with "directory not empty": internal/gui's TestContextTextAPI (#1543),
|
||||
// and any package whose tests send a request through a gateway (gui, tui,
|
||||
// agent, davsync). No test outside this package reads the history back.
|
||||
func keepsRoutes() bool { return keepRoutes && os.Getenv(testSandbox) == "" }
|
||||
|
||||
var history struct {
|
||||
mu sync.Mutex
|
||||
pruned time.Time
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"compress/gzip"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
@@ -12,6 +13,8 @@ import (
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/yetone/magpie/internal/provider"
|
||||
)
|
||||
|
||||
// historyNoon holds the history's clock at noon of a past day and returns
|
||||
@@ -429,3 +432,49 @@ func TestHistoryRouteDayAndNoLock(t *testing.T) {
|
||||
t.Fatal("searched outside the three-day window")
|
||||
}
|
||||
}
|
||||
|
||||
// A done route isn't written to disk in a test binary testenv isolated,
|
||||
// where the write, run behind the request, outlived the test that sent it
|
||||
// and put routing/ back into its config folder as that was removed
|
||||
// (#1543); outside one it is.
|
||||
func TestNoRouteWrittenInATestSandbox(t *testing.T) {
|
||||
keepRoutes = true
|
||||
t.Cleanup(func() { keepRoutes = false })
|
||||
send := func() string {
|
||||
f := &fake{t: t, ctype: "application/json", reply: `{"choices":[{"message":{"role":"assistant","content":"ok"},"finish_reason":"stop"}]}`}
|
||||
setup(t, provider.Chat, f)
|
||||
rec := httptest.NewRecorder()
|
||||
New().Handler().ServeHTTP(rec, httptest.NewRequest("POST", "/v1/chat/completions", strings.NewReader(`{"model":"fake/m1","messages":[{"role":"user","content":"hi"}]}`)))
|
||||
if rec.Code != 200 {
|
||||
t.Fatal(rec.Code, rec.Body)
|
||||
}
|
||||
return HistoryDir()
|
||||
}
|
||||
t.Run("sandbox", func(t *testing.T) {
|
||||
if os.Getenv(testSandbox) == "" {
|
||||
t.Fatal("the package's TestMain left the sandbox marker unset")
|
||||
}
|
||||
dir := send()
|
||||
time.Sleep(200 * time.Millisecond) // a write begun would be on disk by now
|
||||
if _, err := os.Stat(dir); err == nil {
|
||||
t.Fatalf("a route was written to %s", dir)
|
||||
}
|
||||
})
|
||||
t.Run("outside", func(t *testing.T) {
|
||||
t.Setenv(testSandbox, "")
|
||||
dir := send()
|
||||
deadline := time.Now().Add(5 * time.Second)
|
||||
for {
|
||||
if es, _ := os.ReadDir(dir); len(es) > 0 {
|
||||
break
|
||||
}
|
||||
if time.Now().After(deadline) {
|
||||
t.Fatalf("no route written to %s", dir)
|
||||
}
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
}
|
||||
// saveRoute holds the lock until its write and prune are done
|
||||
history.mu.Lock()
|
||||
history.mu.Unlock()
|
||||
})
|
||||
}
|
||||
|
||||
@@ -474,7 +474,7 @@ func (t *trace) update(r *Route, f func(r *Route)) {
|
||||
t.totals.Errors++
|
||||
}
|
||||
t.turns.end(r, r.Status < 400 && r.Error == "" && !r.tools.called(), time.Now())
|
||||
if keepRoutes {
|
||||
if keepsRoutes() {
|
||||
c := *r
|
||||
c.Order = append([]Weighed{}, r.Order...) // [] for none: the GUI reads it as a list
|
||||
c.Left = append([]Weighed(nil), r.Left...)
|
||||
|
||||
Reference in New Issue
Block a user