Files
magpie/internal/agent/remote_connect_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

123 lines
3.8 KiB
Go

package agent
import (
"context"
"net/http/httptest"
"path/filepath"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// KoiLx0 on X: every agent on a client magpie had to be switched on by
// hand before it took the remote's models. With the Remote magpie's
// switch on (off by default), an agent installed here that the remote has
// connected is connected on the follow loop, on the remote's pick; one
// the remote hasn't connected is left alone; and one the user
// disconnected here, by the Agents page or by hand in its file, is never
// connected again, until the user connects it themselves.
func TestRemoteConnectsNewAgents(t *testing.T) {
home, _ := codexHome(t, "", "")
resetRemoteCache()
t.Cleanup(resetRemoteCache)
fake := &remotePicksFake{picks: map[string]map[string]string{}}
fake.set("claude", "model", "group/fast")
srv := httptest.NewServer(fake)
defer srv.Close()
if err := provider.Save(provider.Provider{ID: "office", Name: "Office", Preset: provider.RemoteMagpiePreset, Chat: srv.URL, Key: "secret",
Models: []string{"group/smart", "group/fast"}}); err != nil {
t.Fatal(err)
}
path := filepath.Join(home, ".claude", "settings.json")
own := `{"model": "opus"}`
writeFile(t, path, own)
c := claude(home)
ctx := context.Background()
tick := func() { connectFromRemote([]*Agent{c}, remotePicks(ctx, 0)) }
// off by default: nothing connected
tick()
if c.Wired() || readFile(path) != own {
t.Fatalf("connected with the switch off:\n%s", readFile(path))
}
if ConnectsAgents()["office"] {
t.Fatal("the switch is on by default")
}
// on: connected on the remote's pick
SetConnectAgents("office", true)
if !ConnectsAgents()["office"] {
t.Fatal("the switch didn't turn on")
}
tick()
if !c.Wired() || writtenModel(path) != "office/group/fast" {
t.Fatalf("not connected on the remote's group/fast: on %q\n%s", writtenModel(path), readFile(path))
}
// disconnected on the Agents page: never again
if err := c.Disconnect(); err != nil {
t.Fatal(err)
}
tick()
tick()
if c.Wired() {
t.Fatal("an agent the user disconnected was connected again")
}
// connected by hand: the switch follows it again; then taken out by
// hand in its file (no Disconnect mark): never again either
if err := c.Connect(); err != nil {
t.Fatal(err)
}
tick()
if got := stashShared()[autoKey(c.ID)]; got != "on" {
t.Fatalf("connected by hand, recorded %q", got)
}
writeFile(t, path, own)
if c.Wired() || c.Disconnected() {
t.Fatal("the file written by hand should leave it unconnected, unmarked")
}
tick()
tick()
if c.Wired() {
t.Fatal("an agent the user disconnected by hand in its file was connected again")
}
// an agent the remote hasn't connected stays as it is
forget(autoKey(c.ID), c.ID+disconnectedKey)
fake.mu.Lock()
delete(fake.picks, "claude")
fake.mu.Unlock()
tick()
if c.Wired() {
t.Fatal("connected an agent the remote hasn't connected")
}
// disconnected on the Agents page before the switch was on (no record
// of its own): not connected
fake.set("claude", "model", "group/fast")
stash(map[string]string{c.ID + disconnectedKey: "1"})
tick()
if c.Wired() {
t.Fatal("an agent marked disconnected, never recorded, was connected")
}
forget(c.ID + disconnectedKey)
// put back by magpie on quit (RewireOnStart connects it again): not
// connected here, and not taken for a disconnect by hand
fake.set("claude", "model", "group/fast")
stash(map[string]string{autoKey(c.ID): "on", quitRestoredKey: `["claude"]`})
tick()
if c.Wired() || stashShared()[autoKey(c.ID)] != "on" {
t.Fatalf("a quit-restored agent: wired %v, recorded %q", c.Wired(), stashShared()[autoKey(c.ID)])
}
forget(quitRestoredKey)
// the switch off: nothing connected
SetConnectAgents("office", false)
tick()
if c.Wired() {
t.Fatal("connected with the switch turned off")
}
}