From 5e0d7c058be28701cda2f353926ff070c8c48658 Mon Sep 17 00:00:00 2001 From: yetone Date: Sun, 25 Oct 2026 21:41:58 +0800 Subject: [PATCH] agent: WorkBuddy's own save of models.json after it starts no longer keeps its row saying restart (Sac2333o on X) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sac2333o on X: the WorkBuddy row kept saying "已连接 · 重启 WorkBuddy 后生效" after every restart of WorkBuddy. WorkBuddy rewrites ~/.workbuddy/models.json itself a few seconds after each start. 5.5.6 (Mac app.asar): registerLocalModelIpc checks whether the computer can run local models (only some NVIDIA RTX GPUs and Intel Core Ultra chips), and on "no" purgeLocalModelsOnGateFail saves the file through ModelsJsonStorage.update even when nothing was purged. The live Mac log says "Hardware eligible: false ... Purge complete: 0 models removed". 5.6.2 (Windows app.asar) does the same through the daemon's deleteLocalCustomModel. Both save it with JSON.stringify(x, null, 2), so magpie's trailing newline is gone. 5.6.2 also writes a bare list, and its models-json codec encrypts apiKey (WBEF1) when its credential policy says so. On the Windows box, WorkBuddy 5.6.2 started at 17:17:09 and models.json was last written at 17:17:13 with no trailing newline. AppBehind put magpie's last change at the time of the last write it hadn't seen, using a digest of the raw bytes. WorkBuddy's own save changes those bytes, so every start put "magpie's change" 4s after WorkBuddy started, and the row asked for a restart forever. appReads now digests workbuddyPart: magpie's entries (vendor magpie), each with its keys sorted and its apiKey left out. The same models written by magpie and saved again by WorkBuddy read the same. A model magpie adds or removes, or a changed URL, still asks for a restart. Upgrading users: the stash still holds the old raw digest, so the first look after the update may still say restart once. One restart of WorkBuddy then clears it for good. Run: - TestWorkBuddyOwnSaveAfterStartIsNoChange (darwin and windows subtests): connect, WorkBuddy restarted, WorkBuddy's save 4s after its start (Windows: apiKey encrypted). Red on the old appReads ("WorkBuddy's own save after its start read as magpie's change"), green now. Broken by hand (apiKey kept in the digest): the windows subtest goes red. TestWorkBuddyRestart*, TestChangedAt* still pass. - GUI: internal/gui/tests/agent-app-restart.test.cjs (every language, chromium and webkit, wide/narrow/panel), 40/40. app.js is unchanged. - gofmt -l; go vet darwin, GOOS=linux -tags nogui, GOOS=windows on internal/agent. - Full suite (go test -tags nogui -count=1 -timeout 45m ./..., one run on this machine, no other suite running): running at push. A red it finds gets a follow-up commit and an issue. ZCode (the same report: "zcode接入通信失败") is not changed here. I could not reproduce it. In a sandbox HOME on macOS, `magpie zcode provider magpie` (a build of 4b3bdbe28; this commit doesn't touch ZCode), then ZCode 3.14.3's own app-server (provider/testModelConnectivity, the call behind its settings' test button) against the gateway: {"success":true}. The same on the Windows box, with this tree's magpie.exe and ZCode 3.14.3 for Windows run with ELECTRON_RUN_AS_NODE in a sandbox USERPROFILE/HOME: {"success":true}, and the request reached the upstream. Not tried: the reporter's own machine, upstream and ZCode model (ZCode's probe streams with max_tokens 1 and the model's first reasoning level, so an upstream that refuses that shows as 连接失败); WorkBuddy's real app seeing a magpie sync in this build (its save was read from its code, its log and file times, then replayed in the test). --- internal/agent/apprestart.go | 7 +-- internal/agent/apprestart_test.go | 100 ++++++++++++++++++++++++++++++ internal/agent/workbuddy.go | 33 ++++++++++ 3 files changed, 135 insertions(+), 5 deletions(-) diff --git a/internal/agent/apprestart.go b/internal/agent/apprestart.go index de85b5d80..663fcad0d 100644 --- a/internal/agent/apprestart.go +++ b/internal/agent/apprestart.go @@ -217,7 +217,7 @@ func (a *Agent) AppBehind() *AppRun { // appReads are the files the agent's desktop app reads magpie's part from // at start, and that part. Claude Desktop: deploymentMode, the profile // applied and magpie's profile, with the models the gateway lists it. -// WorkBuddy: its models.json. +// WorkBuddy: magpie's models in its models.json (workbuddyPart). func (a *Agent) appReads() (files []string, reads func() string) { switch a.ID { case "claude-desktop": @@ -243,10 +243,7 @@ func (a *Agent) appReads() (files []string, reads func() string) { return b.String() } case "workbuddy", "workbuddy-ai": - return []string{a.Path}, func() string { - c, _ := edit.Read(a.Path) - return string(c) - } + return []string{a.Path}, func() string { return workbuddyPart(a.Path) } } return nil, nil } diff --git a/internal/agent/apprestart_test.go b/internal/agent/apprestart_test.go index 0d3bf5933..6f78f72d0 100644 --- a/internal/agent/apprestart_test.go +++ b/internal/agent/apprestart_test.go @@ -1,7 +1,9 @@ package agent import ( + "bytes" "context" + "encoding/json" "fmt" "os" "path/filepath" @@ -273,3 +275,101 @@ func TestChangedAtWithoutAWrite(t *testing.T) { t.Fatalf("with a write: %v, want %v", got, later) } } + +// WorkBuddy saves models.json itself seconds after every start (it purges +// local models the computer can't run, and writes the file even when none +// were): JSON.stringify's two-space indent with no trailing newline, and +// on Windows 5.6 the apiKey encrypted. That write is WorkBuddy's, not a +// change of magpie's, so after a restart the row says nothing more; a +// change of magpie's models still asks for one (Sac2333o on X: the row +// kept saying "已连接 · 重启 WorkBuddy 后生效" after every restart). +func TestWorkBuddyOwnSaveAfterStartIsNoChange(t *testing.T) { + for _, osName := range []string{"darwin", "windows"} { + t.Run(osName, func(t *testing.T) { + home := restartSandbox(t) + start := time.Now().Add(-time.Hour) + if osName == "windows" { + restartOnWindows = true + winProcs = func(name string) []winProc { + if !strings.EqualFold(name, "WorkBuddy.exe") { + return nil + } + return []winProc{{pid: 10, parent: 4, image: `C:\Program Files\WorkBuddy\WorkBuddy.exe`, start: start}} + } + } else { + apps := filepath.Join(home, "Applications") + bundle := macApp(t, apps, "WorkBuddy.app", "com.tencent.workbuddy.mac", "Electron") + restartOnMac = true + appFolders = func() []string { return []string{apps} } + running = func(pat string) []runningProc { + p := runningProc{time.Since(start), bundle + "/Contents/MacOS/Electron"} + if regexp.MustCompile(pat).MatchString(p.cmd) { + return []runningProc{p} + } + return nil + } + } + a := workbuddy(home) + if err := a.Field("provider").Set("magpie"); err != nil { + t.Fatal(err) + } + wrote := time.Now().Add(-30 * time.Minute) + os.Chtimes(a.Path, wrote, wrote) + if a.AppBehind() == nil { + t.Fatal("no Restart for WorkBuddy started half an hour before magpie wrote its models") + } + + // restarted: WorkBuddy starts, then saves models.json 4s later + start = time.Now().Add(-10 * time.Second) + workbuddySaves(t, a.Path, osName == "windows", start.Add(4*time.Second)) + if got := a.AppBehind(); got != nil { + t.Fatalf("WorkBuddy's own save after its start read as magpie's change: behind since %v", got.Since) + } + // magpie's next sync writes the same models again + if err := workbuddyWrite(a.Path, true); err != nil { + t.Fatal(err) + } + if got := a.AppBehind(); got != nil { + t.Fatalf("magpie's rewrite of the same models read as a change: behind since %v", got.Since) + } + // a model magpie adds is a change WorkBuddy hasn't read + if err := provider.Save(provider.Provider{ID: "deepseek", Name: "DeepSeek", Chat: "https://api.deepseek.com/v1", Key: "k", Models: []string{"pro", "flash"}}); err != nil { + t.Fatal(err) + } + if err := workbuddyWrite(a.Path, true); err != nil { + t.Fatal(err) + } + if a.AppBehind() == nil { + t.Fatal("no Restart after magpie added a model WorkBuddy started without") + } + }) + } +} + +// workbuddySaves writes models.json as WorkBuddy saves it, +// JSON.stringify(models, null, 2) with no trailing newline, the apiKey +// encrypted when encrypt, and dates the write at. +func workbuddySaves(t *testing.T, path string, encrypt bool, at time.Time) { + t.Helper() + b, err := os.ReadFile(path) + if err != nil { + t.Fatal(err) + } + var out bytes.Buffer + if err := json.Indent(&out, bytes.TrimSpace(b), "", " "); err != nil { + t.Fatal(err) + } + s := out.String() + if encrypt { + s = regexp.MustCompile(`"apiKey": "[^"]*"`).ReplaceAllString(s, `"apiKey": "WBEF1:bm90LXRoZS1rZXk="`) + } + if s == string(b) { + t.Fatal("WorkBuddy's save came out as magpie's bytes; the test proves nothing") + } + if err := os.WriteFile(path, []byte(s), 0o600); err != nil { + t.Fatal(err) + } + if err := os.Chtimes(path, at, at); err != nil { + t.Fatal(err) + } +} diff --git a/internal/agent/workbuddy.go b/internal/agent/workbuddy.go index 8132f616e..7ade53207 100644 --- a/internal/agent/workbuddy.go +++ b/internal/agent/workbuddy.go @@ -37,6 +37,7 @@ import ( "path/filepath" "runtime" "slices" + "strings" "github.com/yetone/magpie/internal/appdir" "github.com/yetone/magpie/internal/edit" @@ -168,6 +169,38 @@ func workbuddyMine(raw json.RawMessage) (workbuddyEntry, bool) { return e, json.Unmarshal(raw, &e) == nil && e.Vendor == magpieID } +// workbuddyPart is magpie's part of models.json, in one form whoever wrote +// the file: magpie's entries, each with its keys sorted and without its +// apiKey. WorkBuddy rewrites models.json itself seconds after every start +// (it purges local models a computer can't run, and saves the file again +// even when nothing was purged): without magpie's trailing newline, as a +// bare list on 5.6, and with the apiKey encrypted where its credential +// policy says so. Its own write is no change of magpie's, so its row is not +// told to restart WorkBuddy for it. The file as it is when it can't be +// read as models.json. +func workbuddyPart(path string) string { + d, err := workbuddyRead(path) + if err != nil { + c, _ := edit.Read(path) + return string(c) + } + var b strings.Builder + for _, raw := range d.models { + if _, ok := workbuddyMine(raw); !ok { + continue + } + var e map[string]any + if json.Unmarshal(raw, &e) != nil { + continue + } + delete(e, "apiKey") + c, _ := json.Marshal(e) + b.Write(c) + b.WriteByte('\n') + } + return b.String() +} + // workbuddyWired reports whether models.json has magpie's models. func workbuddyWired(path string) bool { d, _ := workbuddyRead(path)