Skip to content

Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.

plugin: an install takes the versions no longer installed out of Bun's cache (#1988)

yetonea25731996383
diff --git a/internal/plugin/bun.go b/internal/plugin/bun.go
index c22f909f5..a309879ad 100644
--- a/internal/plugin/bun.go
+++ b/internal/plugin/bun.go
@@ -22,7 +22,6 @@ import (
"sync"
"time"
- "github.com/yetone/magpie/internal/catalog"
"github.com/yetone/magpie/internal/ghtoken"
"github.com/yetone/magpie/internal/libc"
"github.com/yetone/magpie/internal/netproxy"
@@ -318,7 +317,7 @@ func getURLFrom(ctx context.Context, url string, limit int64, mirror bool) ([]by
var bunCommand = func(ctx context.Context, bun, dir string, args ...string) *exec.Cmd {
cmd := command(ctx, bun, args...)
cmd.Dir = dir
- cmd.Env = append(netproxy.EnvForBun(env()), "BUN_INSTALL_CACHE_DIR="+filepath.Join(filepath.Dir(catalog.CachePath()), "bun", "install-cache"))
+ cmd.Env = append(netproxy.EnvForBun(env()), "BUN_INSTALL_CACHE_DIR="+installCacheDir())
cmd.Env = append(cmd.Env, caEnv(cmd.Env, dir)...)
cmd.Env = append(cmd.Env, registryEnv(cmd.Env)...)
return cmd
diff --git a/internal/plugin/installcache.go b/internal/plugin/installcache.go
new file mode 100644
index 000000000..86e4bbe32
--- /dev/null
+++ b/internal/plugin/installcache.go
@@ -0,0 +1,97 @@
+package plugin
+
+import (
+ "encoding/json"
+ "os"
+ "path/filepath"
+ "strings"
+
+ "github.com/yetone/magpie/internal/catalog"
+)
+
+// installCacheDir is Bun's install cache for plugins/: each version of a
+// package it has fetched, unpacked.
+func installCacheDir() string {
+ return filepath.Join(filepath.Dir(catalog.CachePath()), "bun", "install-cache")
+}
+
+// pruneInstallCache takes out of Bun's install cache the versions
+// plugins/ no longer has installed. Bun never does: every version of every
+// plugin an update ever fetched stayed, 131 of them and growing by one
+// with each release of a moved subscription's plugin (#1988). What is
+// installed is a copy, so a version taken out is only fetched again if it
+// is asked for again. installMu held.
+func pruneInstallCache() {
+ root := installCacheDir()
+ tops, err := os.ReadDir(root)
+ if err != nil {
+ return
+ }
+ pruneCached(root, "", tops)
+ for _, top := range tops {
+ if top.IsDir() && strings.HasPrefix(top.Name(), "@") && !strings.Contains(top.Name(), "@@") {
+ // a scope: its packages are in it
+ dir := filepath.Join(root, top.Name())
+ if ents, err := os.ReadDir(dir); err == nil {
+ pruneCached(dir, top.Name()+"/", ents)
+ }
+ }
+ }
+}
+
+// pruneCached takes out of ents, dir's entries in the install cache, the
+// versions (name@version@@registry…) plugins/ hasn't installed; then, in
+// each package's folder of links to its versions, the links left to
+// nothing.
+func pruneCached(dir, scope string, ents []os.DirEntry) {
+ var folders []string
+ for _, e := range ents {
+ if !e.IsDir() {
+ continue
+ }
+ name, rest, ok := strings.Cut(e.Name(), "@")
+ if !ok {
+ folders = append(folders, filepath.Join(dir, e.Name()))
+ continue
+ }
+ version, _, ok := strings.Cut(rest, "@@")
+ if !ok || name == "" || version == "" {
+ continue
+ }
+ if v, known := installedVersion(scope + name); known && v != version {
+ os.RemoveAll(filepath.Join(dir, e.Name()))
+ }
+ }
+ for _, f := range folders {
+ links, _ := os.ReadDir(f)
+ for _, l := range links {
+ p := filepath.Join(f, l.Name())
+ if l.Type()&os.ModeSymlink == 0 {
+ continue
+ }
+ if _, err := os.Stat(p); os.IsNotExist(err) {
+ os.Remove(p)
+ }
+ }
+ }
+}
+
+// installedVersion is the version of pkg in plugins/node_modules, "" when
+// it isn't there. known is false when that can't be told (a read or parse
+// that failed): its versions stay then.
+func installedVersion(pkg string) (version string, known bool) {
+ b, err := os.ReadFile(filepath.Join(Dir(), "node_modules", filepath.FromSlash(pkg), "package.json"))
+ if os.IsNotExist(err) {
+ return "", true
+ }
+ if err != nil {
+ return "", false
+ }
+ var pj struct {
+ Version string `json:"version"`
+ }
+ if json.Unmarshal(b, &pj) != nil || pj.Version == "" {
+ return "", false
+ }
+ return pj.Version, true
+}
diff --git a/internal/plugin/installcache_test.go b/internal/plugin/installcache_test.go
new file mode 100644
index 000000000..f42eaa390
--- /dev/null
+++ b/internal/plugin/installcache_test.go
@@ -0,0 +1,78 @@
+package plugin
+
+import (
+ "context"
+ "os"
+ "path/filepath"
+ "runtime"
+ "testing"
+ "time"
+)
+
+// Bun keeps every version it ever fetched in its install cache, and an
+// install takes none out: 131 versions of the community plugins on the
+// owner's Mac, one more with each release of a moved subscription's plugin
+// (#1988). An install leaves the versions installed and takes out the rest.
+// The layout is Bun's: scope/name@version@@registry@@@1 folders, a
+// scope/name folder of links to them, @T@ tarball entries and .npm
+// manifests.
+func TestInstallPrunesBunsCache(t *testing.T) {
+ offSandbox(t)
+ if err := os.MkdirAll(Dir(), 0o755); err != nil {
+ t.Fatal(err)
+ }
+ os.WriteFile(filepath.Join(Dir(), "package.json"), []byte(`{"name":"magpie-plugins","private":true}`), 0o644)
+ cache := installCacheDir()
+ scope := filepath.Join(cache, "@magpie-community")
+ ver := func(name, v string) string {
+ return filepath.Join(scope, name+"@"+v+"@@registry.npmjs.org@@@1")
+ }
+ mk := func(dir string) {
+ if err := os.MkdirAll(dir, 0o755); err != nil {
+ t.Fatal(err)
+ }
+ os.WriteFile(filepath.Join(dir, "package.json"), []byte(`{}`), 0o644)
+ }
+ old, kept := ver("opencode-grok-auth", "0.1.16"), ver("opencode-grok-auth", "0.1.17")
+ gone := ver("opencode-kiro-auth", "0.1.5") // a plugin no longer installed
+ unsure := ver("opencode-zed-auth", "0.1.7") // installed, its package.json unreadable
+ tarball := filepath.Join(cache, "@T@2773b89432f28711@@@1")
+ for _, d := range []string{old, kept, gone, unsure, tarball} {
+ mk(d)
+ }
+ manifest := filepath.Join(cache, "48634a9ce3e2fcc1-02200d3777602379.npm")
+ os.WriteFile(manifest, []byte("x"), 0o644)
+ links := filepath.Join(scope, "opencode-grok-auth")
+ os.MkdirAll(links, 0o755)
+ if runtime.GOOS != "windows" {
+ os.Symlink(old, filepath.Join(links, "0.1.16@@registry.npmjs.org@@@1"))
+ os.Symlink(kept, filepath.Join(links, "0.1.17@@registry.npmjs.org@@@1"))
+ }
+ zed := filepath.Join(Dir(), "node_modules", "@magpie-community", "opencode-zed-auth")
+ os.MkdirAll(zed, 0o755)
+ os.WriteFile(filepath.Join(zed, "package.json"), []byte(`{"version":`), 0o644)
+
+ ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
+ defer cancel()
+ if err := install(ctx, "@magpie-community/[email protected]"); err != nil {
+ t.Fatal(err)
+ }
+ for _, p := range []string{old, gone} {
+ if _, err := os.Stat(p); !os.IsNotExist(err) {
+ t.Errorf("%s left in Bun's cache, not installed: %v", filepath.Base(p), err)
+ }
+ }
+ for _, p := range []string{kept, unsure, tarball, manifest} {
+ if _, err := os.Stat(p); err != nil {
+ t.Errorf("%s taken out of Bun's cache: %v", filepath.Base(p), err)
+ }
+ }
+ if runtime.GOOS != "windows" {
+ if _, err := os.Lstat(filepath.Join(links, "0.1.16@@registry.npmjs.org@@@1")); !os.IsNotExist(err) {
+ t.Errorf("the link to the version taken out left: %v", err)
+ }
+ if _, err := os.Stat(filepath.Join(links, "0.1.17@@registry.npmjs.org@@@1")); err != nil {
+ t.Errorf("the link to the version installed taken out: %v", err)
+ }
+ }
+}
diff --git a/internal/plugin/store.go b/internal/plugin/store.go
index 28f660f8a..8c231c4fd 100644
--- a/internal/plugin/store.go
+++ b/internal/plugin/store.go
@@ -720,6 +720,7 @@ func install(ctx context.Context, spec string) error {
}
if err == nil {
pruneDist()
+ pruneInstallCache()
}
return err
}