diff --git a/internal/gateway/history.go b/internal/gateway/history.go index 38f37f943..bdde77662 100644 --- a/internal/gateway/history.go +++ b/internal/gateway/history.go @@ -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 diff --git a/internal/gateway/history_test.go b/internal/gateway/history_test.go index 0630478c0..826a6aff0 100644 --- a/internal/gateway/history_test.go +++ b/internal/gateway/history_test.go @@ -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() + }) +} diff --git a/internal/gateway/trace.go b/internal/gateway/trace.go index 29f65cd78..03b9823f9 100644 --- a/internal/gateway/trace.go +++ b/internal/gateway/trace.go @@ -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...)