Skip to content

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

library: InUse answers while a sync holds the library's lock

yetone4ef718651fd8
diff --git a/internal/library/inuse_unlocked_test.go b/internal/library/inuse_unlocked_test.go
new file mode 100644
index 000000000..a3d2cf3d6
--- /dev/null
+++ b/internal/library/inuse_unlocked_test.go
@@ -0,0 +1,44 @@
+package library
+
+import (
+ "os"
+ "path/filepath"
+ "testing"
+ "time"
+)
+
+// #2774: boot.js, which the window waits on before it paints, asks InUse
+// through the built-in plugins, and a library sync holds mu for as long as
+// it takes (13–16s on the reporter's Mac): the window stayed white that
+// long. InUse answers while a sync holds the lock, from the file as it is.
+func TestInUseAnswersWhileASyncHoldsTheLock(t *testing.T) {
+ sandbox(t)
+ os.MkdirAll(filepath.Dir(path()), 0o700)
+ write(t, path(), `{"skills":[{"name":"a"},{"name":"b"}],"mcp":[{"name":"s"}]}`)
+
+ mu.Lock() // a sync, still running
+ defer mu.Unlock()
+ type res struct {
+ n int
+ err error
+ }
+ got := make(chan res, 1)
+ go func() {
+ n, err := InUse()
+ got <- res{n, err}
+ }()
+ select {
+ case r := <-got:
+ if r.err != nil || r.n != 3 {
+ t.Fatalf("InUse = %d, %v; want 3", r.n, r.err)
+ }
+ case <-time.After(5 * time.Second):
+ t.Fatal("InUse waited on the sync's lock")
+ }
+
+ // a file that can't be read is still an error, not 0
+ write(t, path(), `{"skills":`)
+ if n, err := InUse(); err == nil {
+ t.Fatalf("a broken library.json read as %d", n)
+ }
+}
diff --git a/internal/library/setup.go b/internal/library/setup.go
index 9ad9471a8..0662a4064 100644
--- a/internal/library/setup.go
+++ b/internal/library/setup.go
@@ -253,9 +253,14 @@ func Restore(s *Setup) (*Result, error) {
// and project folder it keeps, and the shared instructions while an agent
// gets them. The library built-in plugin stays on while it is
// more than 0. A library.json that can't be read is an error, never 0.
+//
+// It reads without mu: a sync holds mu for as long as it takes (13s and
+// more with many agents and skills), and boot.js, which the window waits
+// on before it paints, asks this through the built-in plugins (#2774: a
+// white window at start on macOS). library.json is only ever replaced
+// whole (save's edit.WriteAtomic), so a read beside a sync sees the file
+// before or after one of its writes, never half of it.
func InUse() (int, error) {
- mu.Lock()
- defer mu.Unlock()
l, err := load()
if err != nil {
return 0, err