diff --git a/server-go/modules/config/store.go b/server-go/modules/config/store.go index 946e799..0f3d550 100644 --- a/server-go/modules/config/store.go +++ b/server-go/modules/config/store.go @@ -1096,10 +1096,17 @@ func (s *Store) WorkspaceAdd(change WorkspaceMutation) error { return errors.New("workspace already exists") } if len(paths) >= maxWorkspaces { + // Make room by dropping server-local workspaces whose directory is gone. + // Only a shared workspace (no provider) lives on this filesystem: a + // detached or mirror workspace's path is the client's, so it never + // exists here, and judging it by a stat here dropped every one of them + // whenever the registry filled. keptPaths, keptProviders, keptRemotes, keptHeads := []string{}, []string{}, []string{}, []string{} for i, registered := range paths { - if _, statErr := os.Stat(registered); errors.Is(statErr, os.ErrNotExist) { - continue + if providers[i] == "" { + if _, statErr := os.Stat(registered); errors.Is(statErr, os.ErrNotExist) { + continue + } } keptPaths = append(keptPaths, registered) keptProviders = append(keptProviders, providers[i]) diff --git a/server-go/modules/config/store_test.go b/server-go/modules/config/store_test.go index 0cace8c..1e5f63a 100644 --- a/server-go/modules/config/store_test.go +++ b/server-go/modules/config/store_test.go @@ -2,6 +2,7 @@ package config import ( "encoding/json" + "fmt" "os" "path/filepath" "strings" @@ -743,3 +744,45 @@ func TestMalformedReloadKeepsLastGoodSnapshotAndRejectsMutations(t *testing.T) { t.Fatalf("recovered snapshot: value=%#v err=%v", value, err) } } + +// A full registry makes room only from server-local workspaces that are gone. +// A detached workspace's path is the client's and never exists on the server, +// so pruning it by a local stat dropped every detached workspace at once. +func TestAFullRegistryKeepsClientWorkspaces(t *testing.T) { + dir := t.TempDir() + store, err := NewStore(filepath.Join(dir, "aimee.yaml")) + if err != nil { + t.Fatal(err) + } + for i := 0; i < maxWorkspaces-1; i++ { + change := WorkspaceMutation{Path: fmt.Sprintf("/client/repo-%d", i), Provider: "detached"} + if err := store.WorkspaceAdd(change); err != nil { + t.Fatal(err) + } + } + gone := filepath.Join(dir, "gone") + if err := store.WorkspaceAdd(WorkspaceMutation{Path: gone}); err != nil { + t.Fatal(err) + } + // Full: the next add may drop the vanished server-local one, and only it. + if err := store.WorkspaceAdd(WorkspaceMutation{Path: "/client/new", Provider: "detached"}); err != nil { + t.Fatal(err) + } + values, _, err := store.Snapshot() + if err != nil { + t.Fatal(err) + } + paths, _ := values["workspaces"].([]any) + if len(paths) != maxWorkspaces { + t.Fatalf("%d workspaces after the add, want %d", len(paths), maxWorkspaces) + } + for _, path := range paths { + if path == gone { + t.Fatal("the vanished server-local workspace was kept") + } + } + // Full of client workspaces only: refuse rather than drop one. + if err := store.WorkspaceAdd(WorkspaceMutation{Path: "/client/one-more", Provider: "detached"}); err == nil { + t.Fatal("a registry full of client workspaces accepted another by dropping one") + } +}