diff --git a/internal/agent/claudedesktop.go b/internal/agent/claudedesktop.go index 8f9947c67..6d64408b6 100644 --- a/internal/agent/claudedesktop.go +++ b/internal/agent/claudedesktop.go @@ -295,6 +295,9 @@ func desktopObject(path string) error { // The stash keeps what Desktop had before magpie: each file's // deploymentMode ("" when it had none), the profile applied, and the // files and folders magpie made, so switching off puts all of it back. +// It also keeps magpie's profile as it was when switched off, the +// settings the user made in it from Desktop's window among them, so +// switching on again brings them back (#2035). // A package's folders keep theirs under the same keys ending in ".package". const ( desktopStashed = "claude-desktop.on" @@ -302,9 +305,10 @@ const ( desktopMode3p = "claude-desktop.mode3p" desktopApplied = "claude-desktop.applied" desktopMade = "claude-desktop.made" + desktopProfile = "claude-desktop.profile" ) -var desktopStashKeys = []string{desktopStashed, desktopMode, desktopMode3p, desktopApplied, desktopMade} +var desktopStashKeys = []string{desktopStashed, desktopMode, desktopMode3p, desktopApplied, desktopMade, desktopProfile} // desktopAdoptStash moves the stash a packaged Desktop's folders got // before they had keys of their own (when magpie wrote only one set) onto @@ -365,12 +369,17 @@ func desktopOn(p desktopPaths) error { } // the profile: the gateway's address and key set, Desktop's own - // settings in it (the user's, from its window) kept + // settings in it (the user's, from its window) kept, and those it had + // when switched off put back (#2035) if b, _ := edit.Read(p.prof); len(bytes.TrimSpace(b)) == 0 { if err := os.MkdirAll(p.library, 0o755); err != nil { return err } - if err := os.WriteFile(p.prof, []byte("{}\n"), 0o600); err != nil { + was := []byte("{}\n") + if kept := unstash(desktopProfile + p.key); kept != "" && desktopObjectBytes([]byte(kept)) { + was = []byte(kept) + } + if err := os.WriteFile(p.prof, was, 0o600); err != nil { return err } } @@ -478,6 +487,11 @@ func desktopOff(p desktopPaths) error { return err } } + // the profile goes, and what is in it is kept for the next on: the + // settings the user made in it are theirs (#2035) + if b, err := os.ReadFile(p.prof); err == nil && desktopObjectBytes(b) && !desktopEmpty(b) { + stash(map[string]string{desktopProfile + k: string(b)}) + } if err := os.Remove(p.prof); err != nil && !errors.Is(err, os.ErrNotExist) { return err } @@ -500,6 +514,12 @@ func desktopOff(p desktopPaths) error { return nil } +// desktopObjectBytes: b is a JSON object. +func desktopObjectBytes(b []byte) bool { + var m map[string]json.RawMessage + return json.Unmarshal(b, &m) == nil && m != nil +} + func desktopEmpty(b []byte) bool { var m map[string]json.RawMessage return json.Unmarshal(b, &m) == nil && len(m) == 0 diff --git a/internal/agent/claudedesktop_test.go b/internal/agent/claudedesktop_test.go index a9dcd4ebb..0604a1c9c 100644 --- a/internal/agent/claudedesktop_test.go +++ b/internal/agent/claudedesktop_test.go @@ -290,6 +290,43 @@ func TestClaudeDesktopChangedSince(t *testing.T) { } } +// #2035 (capiton0): settings the user made in Desktop's third-party window +// live in magpie's profile. Switching Desktop off magpie and on again +// brings them back, the gateway's address and key set anew. +func TestClaudeDesktopOffOnKeepsTheUsersSettings(t *testing.T) { + home, p := desktopSandbox(t) + os.MkdirAll(p.dir, 0o755) + a := claudeDesktop(home) + f := a.Field("provider") + if err := f.Set("magpie"); err != nil { + t.Fatal(err) + } + desktopWrite(t, p.prof, `{"inferenceProvider":"gateway","inferenceGatewayBaseUrl":"http://old:1","inferenceGatewayApiKey":"old","inferenceGatewayAuthScheme":"bearer",`+ + `"disableDeploymentModeChooser":false,"coworkEgressAllowedHosts":["corp.example"],"inferenceModels":["claude-opus-4-7"],"isClaudeCodeForDesktopEnabled":true}`) + if err := f.Set(""); err != nil { + t.Fatal(err) + } + if _, err := os.Stat(p.prof); !os.IsNotExist(err) { + t.Fatal("magpie's profile is still there after off") + } + if err := f.Set("magpie"); err != nil { + t.Fatal(err) + } + prof := desktopJSON(t, p.prof) + want := map[string]any{ + "inferenceProvider": "gateway", "inferenceGatewayBaseUrl": gateway.URL(), + "inferenceGatewayApiKey": "magpie-claude-desktop", "inferenceGatewayAuthScheme": "bearer", + "disableDeploymentModeChooser": false, "coworkEgressAllowedHosts": []any{"corp.example"}, + "inferenceModels": []any{"claude-opus-4-7"}, "isClaudeCodeForDesktopEnabled": true, + } + if !reflect.DeepEqual(prof, want) { + t.Errorf("profile after off and on:\n%v\nwant\n%v", prof, want) + } + if f.Get() != "magpie" || a.Check() != "" { + t.Errorf("get %q, check %q", f.Get(), a.Check()) + } +} + // A file magpie can't read as Desktop writes it is left alone, and so is // everything else. func TestClaudeDesktopRefusesOddFiles(t *testing.T) {