Files
magpie/internal/agent/preview_sweep_test.go
T
vincent e50d73aa36 agent: disconnect previews sweep sandboxes a killed magpie left (#3136)
DisconnectPreview builds its dry-run sandbox with os.MkdirTemp and
cleans it with a bare `defer os.RemoveAll(tmp)` (internal/agent/preview.go).
Two consequences reported by OldSuns: when the removal fails (on Windows
the dry run's bun host still holds magpie-bun.exe after cmd.Run returns)
the error is dropped and the sandbox - each with an 86 MB bun re-downloaded
into it - stays in the temporary directory for good; 12 dirs / 985 MB in
the report, 99 dirs in ggggggggggz's. And nothing ever reclaims sandboxes
left by a killed magpie, unlike the cursor snapshots, whose
sweepCursorSnaps removes stale magpie-cursor-* copies.

The change: DisconnectPreview sweeps magpie-preview-* folders older than
an hour (a live preview lasts seconds; the dry run is bounded at 20 s)
before creating its own, the way the cursor snapshots do; cleanup goes
through removePreview, which retries while processes are still letting go
of files (5 tries, 0.1 s-0.5 s apart) and logs the error instead of
dropping it when it still cannot remove the folder. Only the reported
prefix is swept; the other magpie-* temp prefixes have their own
lifecycles (#2321, cursor_snapshot.go).

Each sandbox also holds a copy of magpie's config folder's files
(logins.json, providers.json and the rest, copyTop), so a sandbox the
removal left behind kept the user's sign-ins in the temporary folder for
good. The sweep takes those too.

Reported by OldSuns, ggggggggggz and track23; the fix is skygunner's
branch fix/3136-agent-magpie-preview-86-mb (98e5e7bc7), ported as it is
apart from the test: it now sweeps a temp dir of its own (TMPDIR, TMP and
TEMP set to t.TempDir()) instead of the machine's, where it could take
another magpie's stale sandboxes.

What was run (on the ported tree):

- TestDisconnectPreviewSweepsStaleSandboxes with sweepPreviews() taken
  out by hand: red ("a stale sandbox is still there after a preview").
- go vet ./internal/agent/ for linux, windows and darwin: ok.
- go test ./internal/agent/ on mote: ok 170.4s.
- Windows (xiaomi-gpu, win-run): go test -run
  'TestDisconnectPreview|TestRemovePreview|TestPreview' ./internal/agent/:
  ok 7.8s.
- On mote, the built binary's agent-disconnect-dry-run for opencode,
  claude and codex under an empty sandbox HOME, then with a plugins.json
  naming an OpenCode plugin: nothing downloaded, the sandbox stayed at
  8-12K.

Not reproduced: the 86 MB bun and the pnpm install inside a sandbox. The
reports are from v0.1.1223; on main the dry run started no plugin host in
the cases above, so nothing here changes where bun comes from. The
sandboxes it left are what this removes.

Refs #3136
2026-10-28 18:21:38 +08:00

80 lines
2.2 KiB
Go

package agent
import (
"errors"
"os"
"path/filepath"
"testing"
"time"
)
// TestDisconnectPreviewSweepsStaleSandboxes: a sandbox an earlier magpie
// was killed holding (#3136) is gone after the next preview, and a sandbox
// a live preview could be using is left alone.
func TestDisconnectPreviewSweepsStaleSandboxes(t *testing.T) {
// a temp dir of the test's own: the sweep never reaches the real one
tmp, err := filepath.EvalSymlinks(t.TempDir())
if err != nil {
t.Fatal(err)
}
for _, k := range []string{"TMPDIR", "TMP", "TEMP"} {
t.Setenv(k, tmp)
}
stale := filepath.Join(tmp, "magpie-preview-sweeptest-stale")
fresh := filepath.Join(tmp, "magpie-preview-sweeptest-fresh")
for _, d := range []string{stale, fresh} {
if err := os.MkdirAll(d, 0o700); err != nil {
t.Fatal(err)
}
}
old := time.Now().Add(-2 * time.Hour)
if err := os.Chtimes(stale, old, old); err != nil {
t.Fatal(err)
}
home, _ := codexHome(t, "", "model = \"gpt-5.4\"\n")
a := codex(home)
if err := a.Apply("model", "fake/m1"); err != nil {
t.Fatal(err)
}
exe, err := os.Executable()
if err != nil {
t.Fatal(err)
}
if _, err := DisconnectPreview(a, exe); err != nil {
t.Fatal(err)
}
if _, err := os.Stat(stale); !os.IsNotExist(err) {
t.Fatalf("a stale sandbox is still there after a preview: %v", err)
}
if _, err := os.Stat(fresh); err != nil {
t.Fatalf("a fresh sandbox was swept: %v", err)
}
}
// TestRemovePreviewRetriesWhileFilesAreHeld: a removal that fails while the
// dry run's processes still hold files is retried until it succeeds, and a
// removal that never succeeds is reported, not dropped (#3136).
func TestRemovePreviewRetriesWhileFilesAreHeld(t *testing.T) {
dir := t.TempDir()
real := previewRemoveAll
var calls int
previewRemoveAll = func(string) error {
calls++
if calls < 3 {
return errors.New("file in use")
}
return real(dir)
}
t.Cleanup(func() { previewRemoveAll = real })
if err := removePreview(dir); err != nil {
t.Fatalf("removePreview gave up: %v", err)
}
if calls != 3 {
t.Fatalf("removePreview tried %d times, want 3", calls)
}
if _, err := os.Stat(dir); !os.IsNotExist(err) {
t.Fatalf("sandbox still there after removePreview: %v", err)
}
}