Files
magpie/internal/agent/dsh_acp_test.go
T
yetone 49b8c64559 dsh: deepseek-acp gets magpie's route in settings.yaml, and the key store is owner-only
liby233 on Discord: dsh in an editor over ACP (deepseek-acp, CodeG, the
desktop app) listed only DeepSeek's own models. Checked against the real
packages from npm (deepseek-acp 0.9.0, dsh 0.2.1-rc.5): deepseek-acp
mounts FileSettings on $DSH_HOME/settings.yaml alone, never profiles/,
and loads the pi-ai plugin only when that file mentions "llm-pi-ai". Its
credentials-local store throws when .credentials.yaml is readable beyond
its owner ("run chmod 600"), so a 0644 store stops it from starting.

- settings.yaml gets llm-pi-ai.providers.magpie, built by the same code
  as the profiles' route (dshPutProviders), at 0600 when magpie creates
  it. It gets no agent-default-model of magpie's. The user's routes,
  other sections, comments, mode and the api they chose for magpie's
  route are kept. Disconnect takes only magpie's route out, and the
  section too when nothing else is left in it.
- dsh 0.2 renames settings.yaml to settings.yaml.imported (over the one
  before) at each full start and imports it into the profile. The
  serving round, Sync, Set and Unwire write it again. The user's own
  import from before magpie's first write is copied once to
  settings.yaml.imported.kept, so magpie's import never overwrites it.
- .credentials.yaml is made owner-only (go-rwx) on every write and every
  Sync, whoever made it 0644. In a WSL distro, a file written through
  \\wsl.localhost comes out 0644, so the chmod runs inside the distro:
  wsl.exe -d <distro> -e chmod go-rwx <path>. A failure is logged and
  doesn't fail the write.

Run:
- internal/agent on mote (ok) and its Dsh/WSL tests on Windows
  (xiaomi-gpu, ok); gofmt; go vet on darwin, GOOS=linux -tags nogui
  and GOOS=windows.
- The new tests (dsh_acp_test.go) were broken by hand, each turning red:
  dshSettingsAgain disabled, ownerOnly disabled, dshKeepImported
  disabled, and the round's call removed.
- Real runs on mote in a sandbox HOME (~/runs/realdsh-home), never a
  live ~/.dsh. After magpie's Pick, real deepseek-acp's session/new
  listed magpie::deepseek/pro and magpie::deepseek/flash; without
  settings.yaml it listed only DeepSeek's. With a 0644 store its boot
  failed with the credentials-local error, and it started once magpie
  had made the store 0600. `dsh --profile web --dump-config` shows
  agent-default-model magpie and the route. `dsh headless` ate
  settings.yaml into the headless profile, and magpie's round
  (dshWiredOnce) wrote it again, after which ACP listed magpie's models
  again.
- On magpie-desktop: `wsl.exe -d magpie-desktop -e chmod go-rwx` takes
  a file written through UNC from 644 to 600.

Not tried:
- magpie's own write into a real WSL distro end to end; only the
  command form above, and fakeWSL in tests.
- A WSL distro has no serving round, so after dsh eats settings.yaml
  there, it comes back only on magpie's next Sync or model pick.
- A dsh from before profiles (config.yaml only) is left as before.

Also: fakeWSL answers the chmod without recording it, since it is neither
a start nor a probe (TestWSLMoreAgentsStopped counts starts).
2026-10-31 22:00:28 +08:00

197 lines
7.6 KiB
Go

package agent
import (
"os"
"path/filepath"
"runtime"
"strings"
"testing"
"time"
"github.com/yetone/magpie/internal/gateway"
)
// liby233 on Discord: deepseek-acp (dsh in an editor over ACP, CodeG)
// reads $DSH_HOME/settings.yaml and nothing else, and loads the pi-ai plugin
// only when that file names llm-pi-ai; with magpie's route in the profiles
// alone an editor listed DeepSeek's own models. A pick puts magpie's route
// there too, beside the user's own routes and sections, and never a start
// of magpie's (dsh 0.1 kept the user's pick there); Disconnect takes the
// route out and leaves the rest.
func TestDshACPSettingsGetTheRoute(t *testing.T) {
home, dir, web := dshRouteHome(t)
os.WriteFile(web, []byte("# Your patch layer for this dsh profile.\n"), 0o644)
settings := filepath.Join(dir, "settings.yaml")
read := func() string { b, _ := os.ReadFile(settings); return string(b) }
a := dsh(home)
if err := a.Pick("model", "magpie/deepseek/pro"); err != nil {
t.Fatal(err)
}
s := read()
for _, want := range []string{"llm-pi-ai:\n providers:\n magpie:\n", "displayName: Magpie", "apiKeyEnv: " + dshKeyRef, "api: openai-completions", "baseURL: " + gatewayV1(), "- id: deepseek/pro"} {
if !strings.Contains(s, want) {
t.Fatalf("settings.yaml lacks %q:\n%s", want, s)
}
}
if strings.Contains(s, "agent-default-model") {
t.Fatalf("a start of magpie's in settings.yaml:\n%s", s)
}
if st, _ := os.Stat(settings); runtime.GOOS != "windows" && st.Mode().Perm() != 0o600 {
t.Fatalf("settings.yaml made %v", st.Mode().Perm())
}
// the user's own: a route of theirs, a section of another plugin's, the
// protocol they picked for magpie's route in dsh's Models page
own := "# mine\nllm-deepseek:\n thinking: enabled\nllm-pi-ai:\n providers:\n mine:\n displayName: Mine\n apiKeyEnv: MINE_API_KEY\n api: openai-completions\n baseURL: https://mine.example/v1\n models:\n - id: m1\n magpie:\n api: openai-responses\n"
os.Remove(settings)
os.WriteFile(settings, []byte(own), 0o644)
if err := a.Sync(); err != nil {
t.Fatal(err)
}
s = read()
for _, want := range []string{"# mine\n", "llm-deepseek:\n thinking: enabled\n", " mine:\n", "baseURL: https://mine.example/v1", " magpie:\n", "api: openai-responses", "- id: deepseek/flash"} {
if !strings.Contains(s, want) {
t.Fatalf("after the sync settings.yaml lacks %q:\n%s", want, s)
}
}
if st, _ := os.Stat(settings); runtime.GOOS != "windows" && st.Mode().Perm() != 0o644 {
t.Fatalf("the user's settings.yaml went %v", st.Mode().Perm())
}
if err := a.Disconnect(); err != nil {
t.Fatal(err)
}
s = read()
if strings.Contains(s, "magpie") || strings.Contains(s, dshKeyRef) {
t.Fatalf("magpie's route stayed:\n%s", s)
}
for _, want := range []string{"# mine\n", "llm-deepseek:\n thinking: enabled\n", " mine:\n", "baseURL: https://mine.example/v1"} {
if !strings.Contains(s, want) {
t.Fatalf("Disconnect took %q:\n%s", want, s)
}
}
// only magpie's there: the section goes with it
if err := a.Pick("model", "magpie/deepseek/pro"); err != nil {
t.Fatal(err)
}
os.WriteFile(settings, []byte("llm-deepseek:\n thinking: enabled\n"), 0o644)
if err := a.Sync(); err != nil {
t.Fatal(err)
}
if err := a.Disconnect(); err != nil {
t.Fatal(err)
}
if s := read(); s != "llm-deepseek:\n thinking: enabled\n" {
t.Fatalf("after Disconnect:\n%s", s)
}
}
// dsh 0.2 eats settings.yaml at each start: it renames it to
// settings.yaml.imported, over the one before, and imports it into the
// profile. The round magpie does while it serves writes the route there
// again, and an import of the user's own from before magpie's first is
// kept aside rather than lost under magpie's.
func TestDshACPSettingsWrittenAgain(t *testing.T) {
_, dir, web := dshRouteHome(t)
t.Setenv("DSH_HOME", dir)
os.WriteFile(web, []byte("# Your patch layer for this dsh profile.\n"), 0o644)
settings := filepath.Join(dir, "settings.yaml")
imported := settings + ".imported"
mine := "llm-deepseek:\n thinking: enabled\nagent-default-model:\n model: deepseek-v4-pro\n"
os.WriteFile(imported, []byte(mine), 0o644)
if err := dsh(filepath.Dir(dir)).Field("model").Set("magpie/deepseek/pro"); err != nil {
t.Fatal(err)
}
if b, _ := os.ReadFile(imported + ".kept"); string(b) != mine {
t.Fatalf("the user's import wasn't kept: %q", b)
}
// dsh starts
os.Rename(settings, imported)
if trouble := dshWiredOnce(); trouble != "" {
t.Fatal(trouble)
}
if b, _ := os.ReadFile(settings); !strings.Contains(string(b), " magpie:\n") {
t.Fatalf("the round didn't write settings.yaml again:\n%s", b)
}
// magpie's own import isn't kept over the user's
os.Rename(settings, imported)
os.Remove(imported + ".kept")
if trouble := dshWiredOnce(); trouble != "" {
t.Fatal(trouble)
}
if _, err := os.Stat(imported + ".kept"); err == nil {
t.Fatal("magpie's own import was kept")
}
}
// deepseek-acp and dsh refuse a key store any other user can read ("run
// chmod 600"), and dsh won't start then: one there already at 0644, the
// user's or an older magpie's, is made owner-only when magpie puts its key
// in, and when the key was there already.
func TestDshStoreMadeOwnerOnly(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("no such bits; dsh doesn't ask on Windows")
}
home, dir, web := dshRouteHome(t)
os.WriteFile(web, []byte("# Your patch layer for this dsh profile.\n"), 0o644)
creds := filepath.Join(dir, ".credentials.yaml")
os.WriteFile(creds, []byte("version: 1\nrefs:\n DEEPSEEK_API_KEY: sk-mine\n"), 0o644)
a := dsh(home)
if err := a.Field("model").Set("magpie/deepseek/pro"); err != nil {
t.Fatal(err)
}
if st, _ := os.Stat(creds); st.Mode().Perm() != 0o600 {
t.Fatalf("store left %v", st.Mode().Perm())
}
if b, _ := os.ReadFile(creds); !strings.Contains(string(b), "DEEPSEEK_API_KEY: sk-mine") {
t.Fatalf("the user's key went:\n%s", b)
}
os.Chmod(creds, 0o644)
if err := a.Sync(); err != nil {
t.Fatal(err)
}
if st, _ := os.Stat(creds); st.Mode().Perm() != 0o600 {
t.Fatalf("store with the key already left %v", st.Mode().Perm())
}
}
// In a WSL distro a file written through \\wsl.localhost is 0644 whatever
// mode magpie asks for (tried on xiaomi-gpu), so the key store is chmodded
// from inside the distro, at the path the distro has it; settings.yaml
// gets the route at the gateway as the distro reaches it.
func TestWSLDshStoreOwnerOnly(t *testing.T) {
home := syncHome(t)
moveHostDirs(t)
root := t.TempDir()
dir := filepath.Join(root, "home", "me", ".dsh")
web := filepath.Join(dir, "profiles", "web", "cordis.patch.yml")
os.MkdirAll(filepath.Dir(web), 0o755)
os.WriteFile(web, []byte("# Your patch layer for this dsh profile.\n[]\n"), 0o644)
var ran []string
run := wslRun
wslRun = func(_ time.Duration, args ...string) ([]byte, error) {
ran = append(ran, strings.Join(args, " "))
return nil, nil
}
t.Cleanup(func() { wslRun = run })
d := distro{Name: "Ubuntu", Root: root, Home: "/home/me", Has: map[string]bool{"bin:dsh": true},
Gateway: "172.20.0.1", Running: true}
a := wslAgent(wslKindOf("dsh"), d)
if err := a.Field("model").Set("magpie/relay/glm-4.6"); err != nil {
t.Fatal(err)
}
want := "-d Ubuntu -e chmod go-rwx /home/me/.dsh/.credentials.yaml"
if !strings.Contains(strings.Join(ran, "\n"), want) {
t.Fatalf("wsl.exe ran %q, not %q", ran, want)
}
gw := "http://172.20.0.1:" + gateway.Port() + "/v1"
if s := readFile(filepath.Join(dir, "settings.yaml")); !strings.Contains(s, "baseURL: "+gw) || !strings.Contains(s, "- id: relay/glm-4.6") {
t.Fatalf("settings.yaml lacks the route at %s:\n%s", gw, s)
}
if es, _ := os.ReadDir(filepath.Join(home, ".dsh")); len(es) > 0 {
t.Fatal("this machine's ~/.dsh was touched")
}
}