Skip to content

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

agent: an import app's status magpie can't read is unknown, not "not added"

yetonec26a418d23d5
diff --git a/docs/subsystems/agent-wiring.md b/docs/subsystems/agent-wiring.md
index e134afaed..e7e69e9c0 100644
--- a/docs/subsystems/agent-wiring.md
+++ b/docs/subsystems/agent-wiring.md
@@ -195,5 +195,11 @@ read or write Lody's private document store, select models there, or claim
that opening the link completed the import. Lody exposes no public status
reader: `Agent.Added` remains nil and the JSON row omits `added`. The GUI
shows a confirmation hint for this unknown state; Cindy still reports its
-known true/false status. Reopening the link lets Lody handle duplicates.
+known true/false status. `Added` returns `(added, known)`: Cindy's
+databases are read without writing beside them (`sessions.OpenUntouched`,
+immutable with no WAL log, from a copy with one; a plain read-only open
+made `-wal` and `-shm` in Cindy's folder while it was closed), and one that
+can't be read, or a stopped WSL distro's, is unknown, never "not added"
+(`TestCindyHasClosedWALAndUnreadable`, `TestImportStatusUnreadableIsUnknown`).
+Reopening the link lets Lody handle duplicates.
Bub requires its separately installed ACP plugin.
diff --git a/internal/agent/agent.go b/internal/agent/agent.go
index 1b55ad146..37f464503 100644
--- a/internal/agent/agent.go
+++ b/internal/agent/agent.go
@@ -239,10 +239,12 @@ type Agent struct {
Gateway func() string
// Import, for an app that takes magpie only through an import link of
// its own, which the user confirms there (Cindy), is that link; the app
- // has no fields magpie sets. Added says whether it has magpie already;
- // nil means the app exposes no way to read that status.
+ // has no fields magpie sets. Added says whether it has magpie already,
+ // known false when what it is read from couldn't be read: unknown,
+ // never "not added". nil means the app exposes no way to read that
+ // status.
Import func() string
- Added func() bool
+ Added func() (added, known bool)
// Launch, for an agent that takes the gateway only from its
// environment (agy), is the command that starts it on magpie, while
// it is on one of magpie's models; "" otherwise.
diff --git a/internal/agent/cindy.go b/internal/agent/cindy.go
index 824e58205..8624b8002 100644
--- a/internal/agent/cindy.go
+++ b/internal/agent/cindy.go
@@ -12,15 +12,15 @@ package agent
// written.
import (
- "database/sql"
"encoding/base64"
"encoding/json"
- "net/url"
+ "errors"
+ "io/fs"
"os"
"path/filepath"
- "strings"
"github.com/yetone/magpie/internal/gateway"
+ "github.com/yetone/magpie/internal/sessions"
_ "github.com/yetone/magpie/internal/sqlitedb"
)
@@ -60,26 +60,27 @@ func CindyLink(url string) string {
// cindyHas reports whether Cindy's database at path has magpie among its
// custom providers: by the id or name the link gives it, or by the
-// gateway's address in its endpoints. Opened read-only; no database, or
-// one that can't be read, is no.
-func cindyHas(path, gw string) bool {
- if _, err := os.Stat(path); err != nil {
- return false
+// gateway's address in its endpoints. Read untouched, whether Cindy runs or
+// not (sessions.OpenUntouched); no database is no, and one that can't be
+// read is unknown (known false), never "not added".
+func cindyHas(path, gw string) (has, known bool) {
+ if _, err := os.Stat(path); errors.Is(err, fs.ErrNotExist) {
+ return false, true
+ } else if err != nil {
+ return false, false
}
- p := filepath.ToSlash(path)
- if !strings.HasPrefix(p, "/") {
- p = "/" + p // C:/… on Windows
- }
- u := url.URL{Scheme: "file", Path: p, RawQuery: "mode=ro&_pragma=busy_timeout(1000)"}
- db, err := sql.Open("sqlite", u.String())
+ db, done, err := sessions.OpenUntouched(path)
if err != nil {
- return false
+ return false, false
}
- defer db.Close()
+ defer done()
var n int
err = db.QueryRow(`SELECT count(*) FROM custom_providers
WHERE id = ? OR lower(name) = ? OR instr(runtimes, ?) > 0`, magpieID, magpieID, gw).Scan(&n)
- return err == nil && n > 0
+ if err != nil {
+ return false, false
+ }
+ return n > 0, true
}
// cindyDirs are where Cindy keeps its data (~/Library/Application
@@ -104,17 +105,24 @@ func cindyDirs() []string {
// cindyAdded reports whether any of Cindy's databases in dirs has magpie:
// one per account (cindy-<account>.db) once signed in, cindy-local-v1.db
-// before.
-func cindyAdded(dirs []string, gw string) bool {
+// before. One that has it is enough; else one that couldn't be read leaves
+// it unknown.
+func cindyAdded(dirs []string, gw string) (added, known bool) {
+ known = true
for _, d := range dirs {
- dbs, _ := filepath.Glob(filepath.Join(d, "cindy-*.db"))
+ dbs, err := filepath.Glob(filepath.Join(d, "cindy-*.db"))
+ if err != nil {
+ known = false
+ }
for _, db := range dbs {
- if cindyHas(db, gw) {
- return true
+ has, ok := cindyHas(db, gw)
+ if has {
+ return true, true
}
+ known = known && ok
}
}
- return false
+ return false, known
}
func cindy() *Agent {
@@ -136,6 +144,6 @@ func cindy() *Agent {
return false
},
Import: func() string { return CindyLink(gateway.URL()) },
- Added: func() bool { return cindyAdded(dirs, gateway.URL()) },
+ Added: func() (bool, bool) { return cindyAdded(dirs, gateway.URL()) },
}
}
diff --git a/internal/agent/cindy_test.go b/internal/agent/cindy_test.go
index 5408b5a04..ed5bf2e65 100644
--- a/internal/agent/cindy_test.go
+++ b/internal/agent/cindy_test.go
@@ -4,6 +4,7 @@ import (
"database/sql"
"encoding/base64"
"encoding/json"
+ "os"
"path/filepath"
"strings"
"testing"
@@ -41,8 +42,8 @@ func TestCindyLink(t *testing.T) {
func TestCindyHas(t *testing.T) {
path := filepath.Join(t.TempDir(), "cindy-local-v1.db")
gw := "http://127.0.0.1:3425"
- if cindyHas(path, gw) {
- t.Fatal("no database, yet has magpie")
+ if has, known := cindyHas(path, gw); has || !known {
+ t.Fatal("no database, yet has magpie or unknown")
}
db, err := sql.Open("sqlite", path)
if err != nil {
@@ -52,19 +53,19 @@ func TestCindyHas(t *testing.T) {
if _, err := db.Exec(`CREATE TABLE custom_providers (id text PRIMARY KEY, name text, runtimes text DEFAULT '{}')`); err != nil {
t.Fatal(err)
}
- if cindyHas(path, gw) {
- t.Fatal("empty, yet has magpie")
+ if has, known := cindyHas(path, gw); has || !known {
+ t.Fatal("empty, yet has magpie or unknown")
}
if _, err := db.Exec(`INSERT INTO custom_providers VALUES ('p1', 'Other', '{"codex":{"baseUrl":"https://x"}}')`); err != nil {
t.Fatal(err)
}
- if cindyHas(path, gw) {
+ if has, _ := cindyHas(path, gw); has {
t.Fatal("another provider taken for magpie")
}
if _, err := db.Exec(`INSERT INTO custom_providers VALUES ('p2', 'Mine', '{"codex":{"baseUrl":"http://127.0.0.1:3425/v1"}}')`); err != nil {
t.Fatal(err)
}
- if !cindyHas(path, gw) {
+ if has, known := cindyHas(path, gw); !has || !known {
t.Fatal("the gateway's provider not found")
}
}
@@ -74,8 +75,8 @@ func TestCindyHas(t *testing.T) {
func TestCindyAdded(t *testing.T) {
local, global := t.TempDir(), t.TempDir()
gw := "http://127.0.0.1:3425"
- if cindyAdded([]string{local, global}, gw) {
- t.Fatal("no databases, yet added")
+ if added, known := cindyAdded([]string{local, global}, gw); added || !known {
+ t.Fatal("no databases, yet added or unknown")
}
db, err := sql.Open("sqlite", filepath.Join(global, "cindy-cmubb4059023mzr0152yezm4r.db"))
if err != nil {
@@ -86,7 +87,61 @@ func TestCindyAdded(t *testing.T) {
INSERT INTO custom_providers VALUES ('magpie-73271b6d', 'Magpie', '{}')`); err != nil {
t.Fatal(err)
}
- if !cindyAdded([]string{local, global}, gw) {
+ if added, _ := cindyAdded([]string{local, global}, gw); !added {
t.Fatal("magpie in an account's database not found")
}
}
+
+// Cindy closed leaves its WAL-mode database with no -shm beside it, which
+// SQLite can't open read-only: magpie in it is still found, and nothing is
+// written in Cindy's folder. A file that is no database is unknown, never
+// "not added".
+func TestCindyHasClosedWALAndUnreadable(t *testing.T) {
+ dir := t.TempDir()
+ path := filepath.Join(dir, "cindy-local-v1.db")
+ gw := "http://127.0.0.1:3425"
+ db, err := sql.Open("sqlite", path)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if _, err := db.Exec(`PRAGMA journal_mode=WAL;
+ CREATE TABLE custom_providers (id text PRIMARY KEY, name text, runtimes text DEFAULT '{}');
+ INSERT INTO custom_providers VALUES ('magpie', 'Magpie', '{}')`); err != nil {
+ t.Fatal(err)
+ }
+ db.Close()
+ before := dirNames(t, dir)
+ if has, known := cindyHas(path, gw); !has || !known {
+ t.Fatalf("closed WAL database: has %v known %v", has, known)
+ }
+ if after := dirNames(t, dir); after != before {
+ t.Fatalf("Cindy's folder changed: %s -> %s", before, after)
+ }
+ bad := filepath.Join(dir, "cindy-bad.db")
+ if err := os.WriteFile(bad, []byte("not a database, a half-written one"), 0600); err != nil {
+ t.Fatal(err)
+ }
+ if has, known := cindyHas(bad, gw); has || known {
+ t.Fatalf("unreadable database: has %v known %v, want unknown", has, known)
+ }
+ if added, known := cindyAdded([]string{dir}, gw); !added || !known {
+ t.Fatal("magpie in one database, beside one unreadable, not found")
+ }
+ os.Remove(path)
+ if added, known := cindyAdded([]string{dir}, gw); added || known {
+ t.Fatalf("only an unreadable database: added %v known %v, want unknown", added, known)
+ }
+}
+
+func dirNames(t *testing.T, dir string) string {
+ t.Helper()
+ es, err := os.ReadDir(dir)
+ if err != nil {
+ t.Fatal(err)
+ }
+ var names []string
+ for _, e := range es {
+ names = append(names, e.Name())
+ }
+ return strings.Join(names, ",")
+}
diff --git a/internal/agent/wsl.go b/internal/agent/wsl.go
index 17aeb618d..aec24d4de 100644
--- a/internal/agent/wsl.go
+++ b/internal/agent/wsl.go
@@ -732,11 +732,20 @@ func wslAgent(k wslKind, d distro) *Agent {
return check()
}
}
- for _, p := range []*func() bool{&a.Routed, &a.Joined, &a.Beside, &a.Added} {
+ for _, p := range []*func() bool{&a.Routed, &a.Joined, &a.Beside} {
if f := *p; f != nil {
*p = func() bool { return d.live() && f() }
}
}
+ // a stopped distro's app can't be read: unknown, not "not added"
+ if f := a.Added; f != nil {
+ a.Added = func() (bool, bool) {
+ if !d.live() {
+ return false, false
+ }
+ return f()
+ }
+ }
// what a set leaves is kept at once, in case the distro stops before
// the next look
for i := range a.Fields {
diff --git a/internal/cli/main.go b/internal/cli/main.go
index dd5c993ff..bc8dc1dc3 100644
--- a/internal/cli/main.go
+++ b/internal/cli/main.go
@@ -725,7 +725,13 @@ func list(agents []*agent.Agent, detectedOnly bool, dimFrom int) error {
r.vals += label.Render(" · " + said)
}
if a.Import != nil {
- if a.Added != nil && a.Added() {
+ // unknown reads as not added here: the line only offers the
+ // command, never says magpie is missing
+ added := false
+ if a.Added != nil {
+ added, _ = a.Added()
+ }
+ if added {
r.vals = value.Render("magpie added")
} else {
r.vals = label.Render("magpie "+a.ID+" add") + faint.Render(" to add magpie")
diff --git a/internal/gui/api.go b/internal/gui/api.go
index 94443b630..99c215b8f 100644
--- a/internal/gui/api.go
+++ b/internal/gui/api.go
@@ -2430,8 +2430,9 @@ func state() stateJSON {
if a.Import != nil {
aj.Import = a.Import()
if a.Added != nil {
- added := a.Added()
- aj.Added = &added
+ if added, known := a.Added(); known {
+ aj.Added = &added
+ }
}
}
if a.Launch != nil {
diff --git a/internal/gui/lody_test.go b/internal/gui/lody_test.go
index 4225e405e..6b3d6c0de 100644
--- a/internal/gui/lody_test.go
+++ b/internal/gui/lody_test.go
@@ -144,3 +144,29 @@ func TestLodyImportRunsLodyOnLinux(t *testing.T) {
t.Fatalf("Lody ran with %q, want %q", b, want)
}
}
+
+// Cindy's database that can't be read leaves its row's added out, as
+// unknown: the window never says "Not added" for what it couldn't read.
+func TestImportStatusUnreadableIsUnknown(t *testing.T) {
+ sandboxHome(t)
+ cfg, err := os.UserConfigDir()
+ if err != nil {
+ t.Fatal(err)
+ }
+ dir := filepath.Join(cfg, "Cindy")
+ if err := os.MkdirAll(dir, 0700); err != nil {
+ t.Fatal(err)
+ }
+ if err := os.WriteFile(filepath.Join(dir, "cindy-local-v1.db"), []byte("half-written, no database"), 0600); err != nil {
+ t.Fatal(err)
+ }
+ for _, a := range state().Agents {
+ if a.ID == "cindy" {
+ if b, _ := json.Marshal(a); strings.Contains(string(b), `"added"`) {
+ t.Fatalf("unreadable Cindy database said: %s", b)
+ }
+ return
+ }
+ }
+ t.Fatal("no Cindy row")
+}
diff --git a/internal/sessions/cursor.go b/internal/sessions/cursor.go
index ca4358960..0b1da34e3 100644
--- a/internal/sessions/cursor.go
+++ b/internal/sessions/cursor.go
@@ -182,6 +182,16 @@ func cursorParentOf(f file) string {
// the two files, in a folder of magpie's own that close removes; a large
// store's copy is shared while it is unchanged (cursor_snapshot.go).
func openCursorStore(path string) (db *sql.DB, closeDB func(), err error) {
+ return OpenUntouched(path)
+}
+
+// OpenUntouched opens another app's SQLite database at path to read it,
+// as openCursorStore does, without writing a byte beside it: immutable
+// with no log to replay, from a copy of it and its log with one. A
+// database in WAL mode can't be opened read-only while its app is closed
+// (SQLite would make its -shm file), so a reader that needs it read
+// whether the app runs or not opens it this way. closeDB removes the copy.
+func OpenUntouched(path string) (db *sql.DB, closeDB func(), err error) {
open := func(p, query string) (*sql.DB, error) {
u := url.URL{Scheme: "file", Path: filepath.ToSlash(p), RawQuery: query}
if len(u.Path) >= 2 && u.Path[1] == ':' {