Files
magpie/internal/agent/zcode_restart_test.go
T
Flandern1211 5e933e153a agent: ZCode gets a Restart when magpie's models change while it runs (Flandern1211 on X, #5662)
ZCode reads its providers at start. Its Agents row now offers Restart for a
model list magpie changed while the app runs, as Claude Desktop's, WorkBuddy's
and OpenMausBot's do: restartApps has ZCode.app (dev.zcode.app) and
ZCode.exe, appReads covers config.json and provider_config.json, and
zcodePart hashes only magpie's provider, provider rule and model rules,
without the key or another provider's rules.

Patch by Flandern1211 (fork branch fix/zcode-restart-5662, 7b663fea9), read
line by line: three files in internal/agent, no network, credential, dependency
or .github change. Kept as written; this commit only adds the Restart line to
docs/subsystems/agent-wiring.md.

Run (mote, at this tree on origin/main 5a3d73039):
- TestZCodeRestartOnWindows red with the zcode restartApps entry renamed away
  by hand ("behind: <nil>"), green with it.
- go vet darwin, darwin nogui, linux nogui, windows on internal/agent: ok.
- GUI agent-app-restart, restart-app, agent-notice-lang, agents-last-known in
  chromium and webkit: 100 of 100 pass.
- internal/agent package: started, result follows in the tag's suite record.
- gofmt -l: clean.

Not tried: a real ZCode on macOS. dev.zcode.app is the patch's bundle id and
could not be checked here (no ZCode.app on the Macs); a wrong id leaves the
Mac row without a Restart, as before, and does not close anything. Real
Windows ZCode.exe was reproduced by the author on 3.17.2, not by me.

Co-authored-by: Flandern1211 <163982805+Flandern1211@users.noreply.github.com>
2026-11-12 18:59:53 +08:00

85 lines
2.8 KiB
Go

package agent
import (
"context"
"os"
"path/filepath"
"slices"
"strings"
"testing"
"time"
)
// ZCode reads its providers at start: while the app runs on a list older
// than magpie's last write, its row offers a Restart, and the Restart quits
// and opens it again (yetone/magpie#5662).
func TestZCodeRestartOnWindows(t *testing.T) {
home := restartSandbox(t)
restartOnWindows = true
appCloseWait, appBackWait = 50*time.Millisecond, time.Second
const exe = `C:\Users\me\AppData\Local\Programs\ZCode\ZCode.exe`
start := time.Now().Add(-time.Hour)
alive := true
winProcs = func(name string) []winProc {
if !alive || !strings.EqualFold(name, "ZCode.exe") {
return nil
}
return []winProc{{pid: 11, parent: 4, image: exe, start: start}}
}
var did []string
winClose = func(pids []int) error {
did = append(did, "close")
return nil
}
winEnd = func(pid int) error {
did = append(did, "end")
alive = false
return nil
}
winLaunch = func(p winProc) error {
did = append(did, "open")
alive, start = true, time.Now()
return nil
}
a := zcode(home)
if err := a.Field("provider").Set("magpie"); err != nil {
t.Fatal(err)
}
got := a.AppBehind()
if got == nil || !got.Since.Equal(start) {
t.Fatalf("behind: %+v, want since %v", got, start)
}
if err := a.RestartApp(context.Background()); err != nil {
t.Fatal(err)
}
if want := []string{"close", "end", "open"}; !slices.Equal(did, want) {
t.Fatalf("did %q, want %q", did, want)
}
if a.AppBehind() != nil {
t.Fatal("still offered after the restart")
}
}
// zcodePart is magpie's part of ZCode's two files only: another provider's
// rule, which ZCode writes itself, is no change of magpie's.
func TestZCodePartIsMagpiesOnly(t *testing.T) {
dir := t.TempDir()
cfg, rules := filepath.Join(dir, "config.json"), filepath.Join(dir, "provider_config.json")
if err := os.WriteFile(cfg, []byte(`{"provider":{"magpie":{"options":{"apiKey":"secret"},"models":{"a":{"limit":{"output":1}}}},"other":{"models":{"b":{}}}}}`), 0o600); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(rules, []byte(`{"config":{"providerConfigRules":{"providerRules":[{"providerId":"magpie","config":{"access":{"apiKey":"secret"},"x":1}},{"providerId":"other","config":{"x":2}}]},"modelConfigRules":{"providerModelRules":[{"providerId":"magpie","modelId":"a"},{"providerId":"other","modelId":"b"}],"manualProviderModelRules":[{"providerId":"magpie","modelId":"a"}]}}}`), 0o600); err != nil {
t.Fatal(err)
}
got := zcodePart(cfg, rules)
if strings.Contains(got, `"other"`) || strings.Contains(got, "secret") {
t.Fatalf("another provider's rule or magpie's key is in magpie's part:\n%s", got)
}
for _, want := range []string{`"models":{"a":{"limit":{"output":1}}}`, `"providerId":"magpie"`, `"modelId":"a"`, `"x":1`} {
if !strings.Contains(got, want) {
t.Fatalf("missing %q in\n%s", want, got)
}
}
}