diff --git a/internal/agent/dsh.go b/internal/agent/dsh.go index 115abb62a..cff60ff8e 100644 --- a/internal/agent/dsh.go +++ b/internal/agent/dsh.go @@ -95,6 +95,25 @@ func dshAt(at place) *Agent { } // DeepSeek Harness Desktop, on this machine (a distro has none) desktop := func() bool { return at.spell == nil && (&Agent{ID: "dsh"}).HasApp() } + // the patch list magpie makes for a dsh with no profile yet: the desktop + // app's, else the web profile's; "" for a dsh before profiles, which + // reads config.yaml (liby233: a dsh 0.2 in WSL that hadn't booted a + // profile yet was wired in config.yaml, which it never reads) + fresh := func() string { + if desktop() { + return dshDesktopFile(dir) + } + version := at.version + if at.spell == nil { + version = dshVersion() + } + if dshBeforeProfiles(dir, version, at.cold) { + return "" + } + return dshWebFile(dir) + } + // profiles' patch lists, or one magpie makes, are what dsh reads + modern := func() bool { return len(dshProfiles(dir)) > 0 || fresh() != "" } path := filepath.Join(dir, "config.yaml") // a stopped distro's folder is never listed: that starts it (#3896) var files []string @@ -103,8 +122,8 @@ func dshAt(at place) *Agent { } if len(files) > 0 { path = files[0] - } else if desktop() { - path = dshDesktopFile(dir) + } else if f := fresh(); f != "" { + path = f } return &Agent{ ID: "dsh", Name: "DeepSeek Harness", Icon: "deepseek-color", Aliases: []string{"deepseek-harness"}, Spelled: prefixed, @@ -112,7 +131,7 @@ func dshAt(at place) *Agent { Bin: "dsh", Dir: dir, Path: path, // the desktop app installed and not opened yet has no ~/.dsh (#2279) sign: desktop, - Sync: func() error { return dshSync(dir, gw()) }, + Sync: func() error { return dshSyncFresh(dir, gw(), fresh) }, Notice: func() string { var notes []string if desktop() && dshUnopened(dir) { @@ -130,7 +149,7 @@ func dshAt(at place) *Agent { notes = append(notes, noticeDsh3.String()) } // only before 0.1.5 does magpie go through llm-deepseek - if len(dshProfiles(dir)) == 0 && dshSettingsEndpoint(filepath.Join(dir, "settings.yaml")) { + if !modern() && dshSettingsEndpoint(filepath.Join(dir, "settings.yaml")) { notes = append(notes, noticeDsh4.String()) } return strings.Join(notes, " ") @@ -170,16 +189,24 @@ func dshAt(at place) *Agent { return dshEnv(dir, gw(), false, false) }, // what magpie made for a desktop app never opened goes (#2279) - Restore: func() error { return dshTidyUnopened(dir) }, + Restore: func() error { + if err := dshTidyUnopened(dir); err != nil { + return err + } + return dshTidyUnbooted(dir) + }, Fields: []Field{{ Key: "model", Label: "model", Get: func() string { return dshGet(dir) }, Set: func(v string) error { - // the desktop app not opened yet: its profile is made as - // it makes it, and it keeps the patch list it finds (#2279) - if v != "" && len(dshProfiles(dir)) == 0 && desktop() { - if err := dshMakeDesktop(dir); err != nil { - return err + // the desktop app not opened yet, or a dsh that hasn't + // booted a profile: the profile is made as dsh makes it, + // and dsh keeps the patch list it finds (#2279, liby233) + if v != "" && len(dshProfiles(dir)) == 0 { + if f := fresh(); f != "" { + if err := dshMakeProfile(f); err != nil { + return err + } } } return dshSet(dir, v, gw()) @@ -189,7 +216,7 @@ func dshAt(at place) *Agent { // since 0.1.5 dsh's own go to DeepSeek on its own key, its // llm-deepseek row left to it, and say so as every agent's // own do (EZN7L2C3, #955); before, magpie took that row over - if len(dshProfiles(dir)) > 0 { + if modern() { for i := range own { own[i].Direct = "DeepSeek" } @@ -208,7 +235,7 @@ func dshAt(at place) *Agent { // a model through magpie that lists no levels has none in // dsh (its route gives it no reasoningEfforts), so none is // offered: dsh's own four were, and each was turned away - if ref, ok := strings.CutPrefix(cur["model"], magpieID+"/"); ok && len(dshProfiles(dir)) > 0 { + if ref, ok := strings.CutPrefix(cur["model"], magpieID+"/"); ok && modern() { return static(dshLevels(ref)...) } return static(dshEfforts...) @@ -245,6 +272,42 @@ func dshDesktopFile(dir string) string { return filepath.Join(dir, "profiles", "desktop", "cordis.patch.yml") } +// dshWebFile is the web profile's patch list, $DSH_HOME/profiles/web: the +// profile `dsh web` boots, the first of dsh's shipped ones (web, headless, +// acp) that magpie makes for a dsh CLI that has booted none yet. A profile +// dsh makes later is given magpie's route by the sync (dshFillNewProfiles). +func dshWebFile(dir string) string { + return filepath.Join(dir, "profiles", "web", "cordis.patch.yml") +} + +// dshProfilesSince is the first dsh whose patch lists are its profiles'. +// That is every dsh on npm: 0.0.1-rc.1, the first, already boots +// $DSH_HOME/profiles/, and none of 0.0.1-rc.1, 0.0.1-rc.5, +// 0.1.0-rc.8, 0.1.1-rc.1/rc.2, 0.1.2-rc.1, 0.1.3-alpha.2 or 0.2.1-rc.5 +// reads a config.yaml. A dsh saying an older version is one from before. +const dshProfilesSince = "0.0.1-rc.1" + +// dshVersion is the version of the dsh CLI on this machine, "" when there is +// none or it didn't say; a var for tests. +var dshVersion = func() string { return (&Agent{ID: "dsh", Bin: "dsh"}).InstalledVersion() } + +// dshBeforeProfiles says a dsh with no profile at dir is one from before +// profiles, which reads ~/.dsh/config.yaml: by its version where that is +// known, else only where config.yaml holds an entry that isn't magpie's +// (what such a dsh, or its user, wrote there). A dsh since profiles makes +// none until one is first booted, and reads no config.yaml at all. cold is +// a stopped distro's, whose files are not read. +func dshBeforeProfiles(dir, version string, cold bool) bool { + if version != "" { + return Newer(dshProfilesSince, version) + } + if cold { + return false + } + _, items, err := dshRead(filepath.Join(dir, "config.yaml")) + return err == nil && slices.ContainsFunc(items, func(it dshItem) bool { return !it.magpie }) +} + // dshMakeDesktop makes the desktop app's patch list, as dsh's template, // for an app installed and never opened (#2279, liangjinyuan-sihan): with // no profile at all magpie had nowhere to write, and DeepSeek Harness @@ -253,8 +316,12 @@ func dshDesktopFile(dir string) string { // files are never touched"), so on its first start it adds its // package.json and pnpm-workspace.yaml beside this one and keeps // magpie's entries. The folder is owner-only, as the app makes it. -func dshMakeDesktop(dir string) error { - f := dshDesktopFile(dir) +func dshMakeDesktop(dir string) error { return dshMakeProfile(dshDesktopFile(dir)) } + +// dshMakeProfile makes the patch list f of a profile dsh hasn't made yet, as +// dsh's template; dsh's initProfile adds the profile's other files beside +// it on its first boot and keeps it. +func dshMakeProfile(f string) error { if err := os.MkdirAll(filepath.Dir(f), 0o700); err != nil { return err } @@ -267,8 +334,12 @@ func dshMakeDesktop(dir string) error { // dshUnopened says the desktop app's profile is one magpie made and the // app hasn't started on yet: it has no package.json, which the app's first // start writes. -func dshUnopened(dir string) bool { - f := dshDesktopFile(dir) +func dshUnopened(dir string) bool { return dshUnbooted(dshDesktopFile(dir)) } + +// dshUnbooted says the profile whose patch list is f is one magpie made and +// dsh hasn't booted yet: it has no package.json, which dsh's first boot of +// it writes (initProfile). +func dshUnbooted(f string) bool { if !isFile(f) { return false } @@ -280,9 +351,17 @@ func dshUnopened(dir string) bool { // app never opened since: the profile's patch list back to dsh's template // with nothing beside it, and the folders that leaves empty. A patch list // holding anything else, or a profile the app has started on, stays. -func dshTidyUnopened(dir string) error { - f := dshDesktopFile(dir) - if !dshUnopened(dir) { +func dshTidyUnopened(dir string) error { return dshTidyMade(dir, dshDesktopFile(dir)) } + +// dshTidyUnbooted is dshTidyUnopened for the web profile magpie made for a +// dsh CLI that has booted none (liby233). +func dshTidyUnbooted(dir string) error { return dshTidyMade(dir, dshWebFile(dir)) } + +// dshTidyMade takes out the profile patch list f magpie made, while dsh has +// not booted it and it is dsh's template alone, and the folders that leaves +// empty. +func dshTidyMade(dir, f string) error { + if !dshUnbooted(f) { return nil } b, err := os.ReadFile(f) @@ -953,6 +1032,11 @@ var dshWrites sync.Mutex func dshSet(dir, v, gw string) error { dshWrites.Lock() defer dshWrites.Unlock() + return dshSetLocked(dir, v, gw) +} + +// dshSetLocked is dshSet with dshWrites held. +func dshSetLocked(dir, v, gw string) error { ref, viaGateway := strings.CutPrefix(v, magpieID+"/") if viaGateway && !isMagpie(ref) { return fmt.Errorf("unknown model %q", v) @@ -1708,12 +1792,36 @@ func dshRouteLines(model string) []string { // profile still on dsh's DeepSeek row taken over moves to the route. // Nothing else in the patch lists changes. func dshSync(dir, gw string) error { + return dshSyncFresh(dir, gw, func() string { return "" }) +} + +// dshSyncFresh is dshSync for a dsh whose first profile, when it has none +// yet, is fresh's patch list ("" for a dsh before profiles). A model of +// magpie's set in config.yaml, which no dsh since profiles reads, goes to the +// profiles when none of them is wired: magpie wrote it there for a dsh that +// hadn't booted a profile yet (liby233), and dsh's first boot made the +// profile without it. +func dshSyncFresh(dir, gw string, fresh func() string) error { dshWrites.Lock() defer dshWrites.Unlock() models := magpieModels("dsh") files := dshProfiles(dir) + made := "" if len(files) == 0 { - return dshSyncLegacy(filepath.Join(dir, "config.yaml"), models, gw) + if made = fresh(); made == "" { + return dshSyncLegacy(filepath.Join(dir, "config.yaml"), models, gw) + } + } + if v := dshGetLegacy(filepath.Join(dir, "config.yaml")); dshCarried(v, files) && edit.GetYAMLMap(filepath.Join(dir, "settings.yaml"), "agent-default-model") == nil { + if made != "" { + if err := dshMakeProfile(made); err != nil { + return err + } + } + return dshSetLocked(dir, v, gw) + } + if len(files) == 0 { + return nil } defer func() { // the key under the name the route gives it, moved from the old one @@ -1745,6 +1853,26 @@ func dshSync(dir, gw string) error { return dshMirrorHome(dir, models, gw, false) } +// dshCarried says v, the model config.yaml starts on, is one of magpie's +// that profiles which have none of magpie's route take over. +func dshCarried(v string, files []string) bool { + ref, ok := strings.CutPrefix(v, magpieID+"/") + if !ok || !isMagpie(ref) { + return false + } + for _, f := range files { + _, items, err := dshRead(f) + if err != nil || dshWired(items) || dshOldWiring(items) { + return false + } + // a start of the user's own, picked in dsh since + if i := dshFindLast(items, "agent-default-model"); i >= 0 && !dshOurs(items[i]) { + return false + } + } + return true +} + // dshStartsOnMagpie reports whether the model a profile's sessions start on // is one of magpie's, read the way dsh starts sessions. func dshStartsOnMagpie(dir string, items []dshItem) bool { diff --git a/internal/agent/dsh_fresh_test.go b/internal/agent/dsh_fresh_test.go new file mode 100644 index 000000000..ecd6b3d6c --- /dev/null +++ b/internal/agent/dsh_fresh_test.go @@ -0,0 +1,144 @@ +package agent + +import ( + "os" + "path/filepath" + "strings" + "testing" +) + +// A dsh CLI on the machine running the tests never decides which file a +// test's dsh is wired in; a test that needs a version says it. +func init() { dshVersion = func() string { return "" } } + +// dshVersionIs holds the dsh CLI's version on this machine at v. +func dshVersionIs(t *testing.T, v string) { + t.Helper() + was := dshVersion + dshVersion = func() string { return v } + t.Cleanup(func() { dshVersion = was }) +} + +// dshNoApp holds DeepSeek Harness Desktop uninstalled. +func dshNoApp(t *testing.T) { + t.Helper() + was := appFolders + appFolders = func() []string { return nil } + t.Cleanup(func() { appFolders = was }) +} + +// liby233 on Discord: in WSL, a dsh 0.2 that had booted no profile yet was +// wired in ~/.dsh/config.yaml, which no dsh on npm reads (0.0.1-rc.1 on; +// 0.2.1-rc.5 makes $DSH_HOME/profiles/web on `dsh web`, as dsh's +// initProfile, and reads no config.yaml). magpie now makes the web +// profile's patch list as dsh's template with its route in it, which dsh +// keeps on its first boot; a dsh before profiles still gets config.yaml. +func TestDshCLIWithNoProfileYet(t *testing.T) { + home, dir, web := dshRouteHome(t) + if err := os.RemoveAll(dir); err != nil { + t.Fatal(err) + } + dshNoApp(t) + dshVersionIs(t, "0.2.1-rc.5") + legacy := filepath.Join(dir, "config.yaml") + read := func(p string) string { b, _ := os.ReadFile(p); return string(b) } + + a := dsh(home) + if a.Path != web { + t.Fatalf("names %s, not the web profile's patch list", a.Path) + } + if err := a.Pick("model", "magpie/deepseek/flash"); err != nil { + t.Fatal(err) + } + s := read(web) + if !strings.HasPrefix(s, strings.TrimSuffix(dshDesktopPatch, "[]\n")) || !strings.Contains(s, "apiKeyEnv: "+dshKeyRef) || !strings.Contains(s, "provider: "+dshRoute) || !strings.Contains(s, `model: "deepseek/flash"`) { + t.Fatalf("the web profile magpie made:\n%s", s) + } + if _, err := os.Stat(legacy); err == nil { + t.Fatalf("wrote config.yaml, which this dsh doesn't read:\n%s", read(legacy)) + } + a = dsh(home) + if !a.Wired() || a.Check() != "" || a.Drift() != nil { + t.Fatalf("not wired: wired %v, check %q, drift %+v", a.Wired(), a.Check(), a.Drift()) + } + // Disconnect before dsh booted it: what magpie made goes + if err := a.Disconnect(); err != nil { + t.Fatal(err) + } + if _, err := os.Stat(filepath.Join(dir, "profiles")); !os.IsNotExist(err) { + t.Fatalf("the profile magpie made outlived Disconnect: %v\n%s", err, read(web)) + } + + // a dsh before profiles keeps config.yaml + dshVersionIs(t, "0.0.0") + a = dsh(home) + if a.Path != legacy { + t.Fatalf("an old dsh names %s", a.Path) + } + if err := a.Pick("model", "magpie/deepseek/flash"); err != nil { + t.Fatal(err) + } + if s := read(legacy); !strings.Contains(s, "- id: llm-deepseek # magpie") { + t.Fatalf("an old dsh's config.yaml:\n%s", s) + } + if _, err := os.Stat(filepath.Join(dir, "profiles")); !os.IsNotExist(err) { + t.Fatal("made a profile for a dsh before profiles") + } + // its version not known: config.yaml with an entry of the user's says + // it is one before profiles; one with magpie's alone says nothing + dshVersionIs(t, "") + if dshBeforeProfiles(dir, "", false) { + t.Fatal("magpie's entries alone taken for an old dsh's config") + } + if err := os.WriteFile(legacy, []byte("- id: tools\n config:\n disabled: []\n"+read(legacy)), 0o644); err != nil { + t.Fatal(err) + } + if !dshBeforeProfiles(dir, "", false) || dsh(home).Path != legacy { + t.Fatal("an old dsh's own config.yaml not taken for one") + } +} + +// liby233's state: magpie wired a dsh 0.2 in config.yaml before it booted a +// profile, and its first boot made the web profile as its template. The +// sync carries magpie's model over to the profile, and config.yaml loses +// magpie's entries; with no profile yet, it makes the web one. +func TestDshConfigYamlCarriedToProfiles(t *testing.T) { + for _, booted := range []bool{true, false} { + home, dir, web := dshRouteHome(t) + if err := os.RemoveAll(dir); err != nil { + t.Fatal(err) + } + dshNoApp(t) + legacy := filepath.Join(dir, "config.yaml") + read := func(p string) string { b, _ := os.ReadFile(p); return string(b) } + + // what magpie wrote before, for a dsh it took for an old one + dshVersionIs(t, "0.0.0") + if err := dsh(home).Pick("model", "magpie/deepseek/pro"); err != nil { + t.Fatal(err) + } + if !strings.Contains(read(legacy), "- id: llm-deepseek # magpie") { + t.Fatalf("config.yaml:\n%s", read(legacy)) + } + dshVersionIs(t, "0.2.1-rc.5") + if booted { + os.MkdirAll(filepath.Dir(web), 0o755) + os.WriteFile(web, []byte(dshDesktopPatch), 0o644) + os.WriteFile(filepath.Join(filepath.Dir(web), "package.json"), []byte("{\n \"name\": \"dsh-profile-web\"\n}\n"), 0o644) + } + a := dsh(home) + if err := a.Sync(); err != nil { + t.Fatal(err) + } + s := read(web) + if !strings.Contains(s, "- id: llm-pi-ai # magpie") || !strings.Contains(s, "provider: "+dshRoute) || !strings.Contains(s, `model: "deepseek/pro"`) { + t.Fatalf("booted %v: the web profile after the sync:\n%s", booted, s) + } + if s := read(legacy); strings.Contains(s, "# magpie") { + t.Fatalf("booted %v: config.yaml kept magpie's entries:\n%s", booted, s) + } + if a = dsh(home); a.Field("model").Get() != "magpie/deepseek/pro" || a.Check() != "" { + t.Fatalf("booted %v: model %q, check %q", booted, a.Field("model").Get(), a.Check()) + } + } +} diff --git a/internal/agent/wsl.go b/internal/agent/wsl.go index 1a2938c26..460563c23 100644 --- a/internal/agent/wsl.go +++ b/internal/agent/wsl.go @@ -497,7 +497,9 @@ var wslKinds = []wslKind{ return append(miniMaxOwnOptions("", cur["model"]), viaMagpie("minimax-code", magpieID+"/")...) } }}, - {id: "dsh", name: "DeepSeek Harness", dir: ".dsh", bin: "dsh", in: dshIn, + // version: a dsh with no profile yet is told from one before profiles + // by it (liby233) + {id: "dsh", name: "DeepSeek Harness", dir: ".dsh", bin: "dsh", in: dshIn, version: true, restart: newNotice("{agent} in WSL {distro} reads its config at start-up — restart open dsh sessions to use this."), asleep: func(key string) func(map[string]string) []Option { // the thinking levels hang on which patch lists there are, the diff --git a/internal/agent/wsl_more_test.go b/internal/agent/wsl_more_test.go index c0391490e..007a359ed 100644 --- a/internal/agent/wsl_more_test.go +++ b/internal/agent/wsl_more_test.go @@ -30,7 +30,7 @@ var wslMore = []struct { {"fx", ".fx/settings.json", "/v1"}, {"commandcode", ".commandcode/providers.json", "/v1"}, {"minimax-code", ".minimax/config.yaml", ""}, - {"dsh", ".dsh/config.yaml", "/v1"}, + {"dsh", ".dsh/profiles/web/cordis.patch.yml", "/v1"}, // liby233: no dsh reads config.yaml {"muse", ".config/muse/settings.json", "/v1"}, {"qoder", ".qoder/settings.json", "/v1"}, {"qoder-cn", ".qoder-cn/settings.json", "/v1"},