Files
magpie/internal/gui/connect_agents_test.go
T
yetone e97271f027 agents: a client of a Remote magpie can connect new agents the remote has connected (KoiLx0 on X)
KoiLx0 runs one magpie as a client of another, through a gateway key, and
had to switch every agent on by hand on the client before it took the
remote's models. A Remote magpie's editor now has "Connect new agents to
this remote" (自动接入新 agent) under Advanced, off by default. It is kept
per computer in the stash (remote.<id>.connectAgents), not in
providers.json, so a synced or shared providers list doesn't turn it on
anywhere else. No server-side key permission.

With it on, the follow loop (FollowRemotePicks, every 5 minutes, and at
once when the switch is turned on) connects an agent installed here that
the remote has connected, on the remote's pick (connectFromRemote), and
records it as <agent>.remote.auto = on. An agent the user disconnected
here is never connected again:
- one with the Agents page's Disconnect mark, connected or not before;
- one recorded on that is no longer connected (taken out by hand in its
  file, or by any other route) is recorded off.
An agent magpie put back on quit (quit.restored) is left to RewireOnStart
and not taken for a disconnect. Connecting it by hand records on again.
The follow throttle's time is now under a mutex, since the save handler
resets it from another goroutine.

The switch shows in /api/providers as connectAgents and is saved as
connectAgents (a save that leaves it out keeps it; a provider that isn't
a Remote magpie is refused it). Advanced opens on its own while it is on.
docs/subsystems/providers-accounts.md says all of this.

app.js stays within its budget (TestCoreScriptBudget): the Remote
magpie's three ticks (MCP servers, skills and prompts, agents) and their
save are one loop, which the new switch costs less than it saves.

Run:
- mote, go test -tags nogui -count=1 -timeout 45m ./... with the first
  draft of app.js: every package ok but internal/gui, where only
  TestCoreScriptBudget failed (1087333 > 1086971). After the loop,
  rebased on origin/main 187d06724 (budget passes there alone), at this
  head: TestCoreScriptBudget, internal/gui, internal/agent and
  internal/cli ok.
- mica: TestRemoteConnectsNewAgents, TestConnectFollowsTheRemoteMagpiesPicks,
  TestProviderSaveConnectAgents ok.
- mote: go vet (darwin), GOOS=linux go vet -tags nogui, GOOS=windows go
  vet on internal/agent and internal/gui ok; gofmt -l clean.
- GUI, mica, both engines, at f261f7ba6 (before the loop and the rebase): remote-connect-agents.test.cjs (new: under
  Advanced and closed while off, sent as ticked, Advanced opens on it
  ticked, unticked sent so; en, zh, zh-TW, ja, de at 420px, no sideways
  scroll) passes, with i18n-keys, i18n-en-no-cjk and every file that
  reaches the provider editor's Advanced or a Remote magpie (64 files):
  1738 pass, 26 fail. The same files at origin/main 19794cc10: 1728 pass,
  24 fail, the same reds by name (editor-hints webkit #6156,
  account-arrange #6010, lan-join #6353, vertex-editor #4720,
  remote-magpie #5013, commented with this sha) except name-suffix-remote
  (#5147, red at base too on another language) and one count-free
  browser launch timeout; both files alone: 80 of 80 pass. At this
  head (rebased, with the loop): remote-connect-agents, library-mcp-on, provider-advanced-fold,
  i18n-keys, i18n-en-no-cjk pass; remote-magpie red as at base (#5013);
  editor-advanced-align red once in webkit, 1 of 4 alone (#6421, a
  Claude Code account's editor this change doesn't reach; it was red at
  base in the 64-file run).
- Real use on mica, sandbox HOMEs, this branch's binary: remote R
  (`magpie serve`, Claude Code on gpt-5.4-mini) and a fresh client C
  with Claude Code installed, not connected. Switch off: C's
  settings.json stayed {}. Switch on, C restarted: "agent: Claude Code
  connected as the remote magpie's", ANTHROPIC_DEFAULT_SONNET_MODEL
  office/openai/gpt-5.4-mini, recorded on. `magpie claude default`
  (disconnect) and C restarted: settings back to the user's, recorded
  off, not connected again.

Broken by hand, each red in TestRemoteConnectsNewAgents:
- connectFromRemote returning at once (the old code): "not connected on
  the remote's group/fast";
- without the on-then-unconnected record: "an agent the user
  disconnected by hand in its file was connected again";
- without the Disconnect mark check: "an agent marked disconnected,
  never recorded, was connected";
- without the quit.restored exception: "a quit-restored agent: wired
  false, recorded off".

Not tried: the switch turned on from the real app's window (the GUI test
fakes the API; the save handler is covered by TestProviderSaveConnectAgents
and real use wrote the stash key); agents other than Claude Code for real
(the code is the same ConnectHow for every agent with fields); Windows and
Linux runs.
2026-11-16 17:23:50 +08:00

68 lines
2.5 KiB
Go

package gui
import (
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
"github.com/yetone/magpie/internal/agent"
"github.com/yetone/magpie/internal/provider"
)
// KoiLx0 on X: a Remote magpie editor's "Connect new agents to this
// remote" is saved on this computer (agent.SetConnectAgents), shown as
// saved, kept by a save that leaves it out, and refused another provider.
func TestProviderSaveConnectAgents(t *testing.T) {
t.Setenv("HOME", t.TempDir())
t.Setenv("USERPROFILE", os.Getenv("HOME"))
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
t.Setenv("XDG_CACHE_HOME", t.TempDir())
mux := http.NewServeMux()
providerRoutes(mux, nil)
post := func(body string) {
t.Helper()
w := httptest.NewRecorder()
mux.ServeHTTP(w, httptest.NewRequest("POST", "/api/provider/save", strings.NewReader(body)))
if w.Code != 200 {
t.Fatalf("%d %s", w.Code, w.Body.String())
}
}
shown := func(id string) bool {
t.Helper()
for _, p := range providersState().Providers {
if p.ID == id {
return p.ConnectAgents
}
}
t.Fatalf("%s not listed", id)
return false
}
if err := provider.Save(provider.Provider{ID: "office", Name: "Office", Preset: provider.RemoteMagpiePreset, Chat: "http://192.168.1.20:3425/v1", Key: "secret"}); err != nil {
t.Fatal(err)
}
if err := provider.Save(provider.Provider{ID: "vllm", Name: "vLLM", Chat: "http://192.168.1.21:8000/v1", Key: "k"}); err != nil {
t.Fatal(err)
}
if shown("office") || agent.ConnectsAgents()["office"] {
t.Fatal("on by default")
}
post(`{"id":"office","from":"office","name":"Office","preset":"remote-magpie","chat":"http://192.168.1.20:3425/v1","models":[],"headers":{},"connectAgents":true}`)
if !agent.ConnectsAgents()["office"] || !shown("office") {
t.Fatal("the switch wasn't saved or isn't shown")
}
post(`{"id":"office","from":"office","name":"Office","preset":"remote-magpie","chat":"http://192.168.1.20:3425/v1","models":[],"headers":{}}`)
if !shown("office") {
t.Fatal("a save that left it out turned it off")
}
post(`{"id":"vllm","from":"vllm","name":"vLLM","chat":"http://192.168.1.21:8000/v1","models":[],"headers":{},"connectAgents":true}`)
if agent.ConnectsAgents()["vllm"] {
t.Fatal("a provider that isn't a Remote magpie took the switch")
}
post(`{"id":"office","from":"office","name":"Office","preset":"remote-magpie","chat":"http://192.168.1.20:3425/v1","models":[],"headers":{},"connectAgents":false}`)
if agent.ConnectsAgents()["office"] || shown("office") {
t.Fatal("turning it off didn't")
}
}