Files
magpie/internal/agent/dsh_creds_test.go
T
yetone 70a0c387cb agent: a provider named magpie written by hand is the user's, not connected (JFunfunfun on X)
Pi set up by hand on a provider of its own named magpie (magpie/<model>
at the gateway) read as connected in magpie: every catalog change wrote
magpie's list over that provider, RenameProvider rewrote its refs, Drift
offered Reconnect, and Disconnect took the provider out.

0911a65e7 (yyddbds, MiMo) already holds a ListOnly agent's sync until
it is Wired; that still let a hand-written config that names a magpie
model (JFunfunfun's Pi) read as Wired. Both gates now apply.
Its test's last case said a magpie model picked by hand on MiMo's own
magpie provider connects it; that is JFunfunfun's case, so it now
stays the user's until connected in magpie (listonly_test.go, and the
doc's ListOnly line says so).

An agent whose magpie is a provider of its own config (Spelled, not
Native) is now connected only when magpie set it up (setUp): a field in
applied.json, the new applied.Kept that Keep leaves instead of deleting
the record, or the <id>.connect.was stash Connect has written since
10-03. Otherwise (handWired) it isn't Wired, has no Drift, and
SyncCatalog, RenameProvider and renameRefs skip it; Disconnect is a
no-op as it is for any unwired agent. Connecting it in magpie takes it
on as before; Disconnect after that skips putting back "was" values
that were themselves magpie's (dsh errored on them). RenameLegacy
records the dial/ -> magpie/ values it writes, and moveEfforts (the
Cursor/Antigravity effort ids, at start-up) leaves a hand-written
magpie/<ref> alone. Aside keeps its own
key-based ownership. OpenCode's own-gateway-provider test now connects
through Apply as `magpie opencode` does.

Run (on mote, at this head, rebased on f2c6831a3): go test -tags nogui
-count=1 ./internal/agent ./internal/cli ./internal/library
./internal/gui; go vet darwin/linux/windows of internal/agent and
internal/cli; gofmt -l. internal/agent again after the ListOnly test
change: ok. Pushed on ceef9e943, whose five commits since touch usage,
sessions, library, provider movers and gui only.
New tests: TestPiHandWiredMagpieProviderIsTheirs (pi's literal
settings.json/models.json in a sandbox HOME, files byte-identical after
SyncCatalog, RenameProvider and Disconnect), TestPiConnectedBefore-
StaysConnected (applied record, gone model, connect.was, applied then
Keep), TestHandWiredMagpieProviderSiblings (29 Spelled agents). All fail
on the old code.
Tests that wrote a connected agent's files by hand (Pi, OpenCode,
Hermes, omp, dsh, the Kilo plugin, Cursor efforts, and the cli's
save-then-sync test) now also mark it as Connect leaves it
(setUpInMagpie, the connect.was stash); without it they read as
hand-written, which is what this change is about.

Broken by hand, each red: the Sync gate, the rename gates, the Drift
gate, Keep deleting the record, the was-skip in Disconnect, the
moveEfforts gate (TestMoveCursorEfforts' hand-wired case).

Not tried: a real pi binary against the gateway. Users connected
between 09-23 and 10-03 who pressed Keep (record deleted before this)
read as not connected; one Connect brings them back and pi keeps
working meanwhile. Agents without Spelled (Claude Code, Codex, Gemini
and others) decide by their own checks and are unchanged.
2026-10-30 14:45:56 +08:00

205 lines
7.6 KiB
Go

package agent
import (
"os"
"path/filepath"
"runtime"
"strings"
"testing"
"github.com/yetone/magpie/internal/access"
"github.com/yetone/magpie/internal/edit"
)
// #969 (pan17): the DeepSeek Harness desktop app, wired through magpie, failed
// every turn with `no credential for provider route "magpie"; its profile
// resolves MAGPIE_GATEWAY_KEY, which is not set — store MAGPIE_GATEWAY_KEY
// through the credentials service (the web Models page writes it) or export
// it` (MISSING_CREDENTIAL), while magpie said it was connected. The route was
// in its profile; the key was in ~/.dsh/.env, which only dsh's product CLI
// loads. The key now goes into dsh's own store, ~/.dsh/.credentials.yaml,
// which every dsh reads (dsh-credentials-local), the desktop app included.
func TestDshDesktopGetsTheKeyFromItsStore(t *testing.T) {
home, dir, web := dshRouteHome(t)
desktop := filepath.Join(dir, "profiles", "desktop", "cordis.patch.yml")
os.MkdirAll(filepath.Dir(desktop), 0o755)
os.WriteFile(web, []byte("# Your patch layer for this dsh profile.\n"), 0o644)
os.WriteFile(desktop, []byte("# Your patch layer for this dsh profile.\n"), 0o644)
a := dsh(home)
if err := a.Field("model").Set("magpie/deepseek/flash"); err != nil {
t.Fatal(err)
}
if b, _ := os.ReadFile(desktop); !strings.Contains(string(b), "apiKeyEnv: "+dshKeyRef) {
t.Fatalf("the desktop app's profile has no route:\n%s", b)
}
creds := filepath.Join(dir, ".credentials.yaml")
b, err := os.ReadFile(creds)
if err != nil {
t.Fatal(err)
}
if s := string(b); !strings.HasPrefix(s, "version: 1\n") || !strings.Contains(s, "refs:\n "+dshKeyRef+": magpie\n") {
t.Fatalf("the key is not in dsh's store:\n%s", s)
}
// dsh refuses to start on a store any other user can read
if st, _ := os.Stat(creds); runtime.GOOS != "windows" && st.Mode().Perm() != 0o600 {
t.Fatalf("store mode %v", st.Mode().Perm())
}
if d := a.Check(); d != "" {
t.Fatalf("check: %s", d)
}
// wired by an earlier magpie, the key in .env alone: the check says so,
// and the sync that runs as magpie starts writes it
os.Remove(creds)
if d := a.Check(); !strings.Contains(d, ".credentials.yaml") || !strings.Contains(d, "desktop") {
t.Fatalf("a store without the key is not named: %q", d)
}
if err := a.Sync(); err != nil {
t.Fatal(err)
}
if d := a.Check(); d != "" {
t.Fatalf("after the sync: %s", d)
}
// back to dsh's own models: magpie's key goes, the rest stays
os.WriteFile(creds, []byte("version: 1\n# mine\nrefs:\n DEEPSEEK_API_KEY: sk-mine\n "+dshKeyRef+": magpie\n"), 0o600)
if err := a.Field("model").Set("deepseek-v4-pro"); err != nil {
t.Fatal(err)
}
b, _ = os.ReadFile(creds)
if s := string(b); strings.Contains(s, dshKeyRef) || !strings.Contains(s, "DEEPSEEK_API_KEY: sk-mine") || !strings.Contains(s, "# mine") {
t.Fatalf("back to dsh's own:\n%s", s)
}
}
// #1332: a caller key this gateway issued, held in dsh's own store, takes dsh
// through magpie all the same — the store wins over .env (#969) and the
// gateway authenticates the key — so the check is quiet. A key the gateway
// doesn't take still is drift.
func TestDshStoreHoldingAGatewayCallerKey(t *testing.T) {
home, dir, web := dshRouteHome(t)
os.WriteFile(web, []byte("# Your patch layer for this dsh profile.\n"), 0o644)
a := dsh(home)
if err := a.Field("model").Set("magpie/deepseek/flash"); err != nil {
t.Fatal(err)
}
secret, err := access.Update("add-key", access.Change{Name: "dsh"})
if err != nil {
t.Fatal(err)
}
creds := filepath.Join(dir, ".credentials.yaml")
os.WriteFile(creds, []byte("version: 1\nrefs:\n "+dshKeyRef+": "+secret+"\n"), 0o600)
if d := a.Check(); d != "" {
t.Fatalf("a key the gateway issued is drift: %s", d)
}
os.WriteFile(creds, []byte("version: 1\nrefs:\n "+dshKeyRef+": sk-not-this-gateways\n"), 0o600)
if d := a.Check(); !strings.Contains(d, "holds another") {
t.Fatalf("a key the gateway doesn't take is not reported: %q", d)
}
}
// A store the user keeps is written into, not over; one holding a key of the
// user's under magpie's name, or in dsh's pre-release flat layout, is left to
// the user and to dsh.
func TestDshStoreOfTheUsersOwn(t *testing.T) {
home, dir, web := dshRouteHome(t)
os.WriteFile(web, []byte("# Your patch layer for this dsh profile.\n"), 0o644)
creds := filepath.Join(dir, ".credentials.yaml")
a := dsh(home)
own := "version: 1\nrefs:\n DEEPSEEK_API_KEY: sk-mine\nrecords:\n llm-pi-ai/amazon-bedrock:\n kind: api-key\n env:\n AWS_PROFILE: prod\n"
os.WriteFile(creds, []byte(own), 0o600)
if err := a.Field("model").Set("magpie/deepseek/pro"); err != nil {
t.Fatal(err)
}
b, _ := os.ReadFile(creds)
for _, want := range []string{"DEEPSEEK_API_KEY: sk-mine", "AWS_PROFILE: prod", dshKeyRef + ": magpie"} {
if !strings.Contains(string(b), want) {
t.Fatalf("missing %q:\n%s", want, b)
}
}
theirs := "version: 1\nrefs:\n " + dshKeyRef + ": sk-mine\n"
os.WriteFile(creds, []byte(theirs), 0o600)
if err := a.Sync(); err != nil {
t.Fatal(err)
}
if b, _ := os.ReadFile(creds); string(b) != theirs {
t.Fatalf("the user's key was written over:\n%s", b)
}
if d := a.Check(); !strings.Contains(d, "holds another") {
t.Fatalf("the user's key is not reported: %q", d)
}
flat := "DEEPSEEK_API_KEY: sk-mine\n"
os.WriteFile(creds, []byte(flat), 0o600)
if err := a.Sync(); err != nil {
t.Fatal(err)
}
if b, _ := os.ReadFile(creds); string(b) != flat {
t.Fatalf("a flat store was written:\n%s", b)
}
}
// #1419 (DannyJRu, dsh desktop 0.2.0-rc.2): dsh's own store held a
// MAGPIE_GATEWAY_KEY this gateway refuses. Reconnect (Apply again, which sets
// the model) said done and the row stayed red, telling the user to go and
// remove it in dsh. A model picked here now puts magpie's key there, the one
// it replaced stashed for Disconnect to put back; the sync still leaves it,
// and a key the gateway takes stays on a pick too.
func TestDshApplyReplacesAKeyTheGatewayRefuses(t *testing.T) {
home, dir, web := dshRouteHome(t)
os.WriteFile(web, []byte("# Your patch layer for this dsh profile.\n"), 0o644)
a := dsh(home)
setUpInMagpie(a.ID)
if err := a.Field("model").Set("magpie/deepseek/flash"); err != nil {
t.Fatal(err)
}
creds := filepath.Join(dir, ".credentials.yaml")
refused := "version: 1\nrefs:\n DEEPSEEK_API_KEY: sk-mine\n " + dshKeyRef + ": sk-refused\n"
os.WriteFile(creds, []byte(refused), 0o600)
if err := a.Sync(); err != nil {
t.Fatal(err)
}
if b, _ := os.ReadFile(creds); string(b) != refused {
t.Fatalf("the sync wrote over the store:\n%s", b)
}
if d := a.Check(); !strings.Contains(d, "holds another") || !strings.Contains(d, "Apply again") {
t.Fatalf("check: %q", d)
}
// Reconnect
if err := a.Field("model").Set("magpie/deepseek/flash"); err != nil {
t.Fatal(err)
}
if v, _ := edit.GetYAML(creds, "refs."+dshKeyRef); v != "magpie" {
t.Fatalf("the refused key is still there: %q", v)
}
if v, _ := edit.GetYAML(creds, "refs.DEEPSEEK_API_KEY"); v != "sk-mine" {
t.Fatalf("the user's other key: %q", v)
}
if d := a.Check(); strings.Contains(d, "holds another") {
t.Fatalf("still drift after Reconnect: %q", d)
}
if err := a.Disconnect(); err != nil {
t.Fatal(err)
}
if v, _ := edit.GetYAML(creds, "refs."+dshKeyRef); v != "sk-refused" {
t.Fatalf("Disconnect didn't put the user's key back: %q", v)
}
secret, err := access.Update("add-key", access.Change{Name: "dsh"})
if err != nil {
t.Fatal(err)
}
taken := "version: 1\nrefs:\n " + dshKeyRef + ": " + secret + "\n"
os.WriteFile(creds, []byte(taken), 0o600)
if err := a.Field("model").Set("magpie/deepseek/flash"); err != nil {
t.Fatal(err)
}
if b, _ := os.ReadFile(creds); string(b) != taken {
t.Fatalf("a caller key the gateway takes was replaced:\n%s", b)
}
}