KoiLx0 runs several computers on one remote magpie and had to set every agent's models again on each of them, and again whenever the remote changed. Routing groups already reach a client (a group is a model of the remote's list, TestRemoteMagpieNewGroupServedBesidePicks); the agents' picks did not. - The remote tells, per connected agent, each model field's catalog ref at GET /v1/magpie/agent-models (gateway.SharedAgentModels, agent.SharedModels). Guarded as the Library is: this computer, or a full-access key of a magpie shared on the network; a held key is refused (readonly_test), and another machine without a key gets 401 (TestNetworkGatewayGuardsEveryRoute). - An agent connected here starts on those picks where this magpie lists them as <remote id>/<ref> (How "remote", after "again" and "joined"; the Agents page says "as on the remote magpie", in all five languages). - A field taken from the remote, or found on its pick, is recorded in the stash and follows the remote's change every 5 minutes (FollowRemotePicks from library.KeepLinked). A model picked here is the user's: the record goes, and the field is left alone until it is on the remote's pick again. A pick this magpie doesn't list is skipped. - app.js budget: no raise. The new line's 76 bytes come from same and alike sharing one string (CONN_WAS) and the native joined case returning plain instead of the same t() again. Run at this head (rebased onc38753a7e, i18n-keys ok there; before that onb010fba05, whose budget raise left 2 bytes; CONN_WAS's short name gives them): - mote: go test -count=1 ./internal/agent ./internal/gui ./internal/library ./internal/provider: all ok. - mica: go test ./internal/gateway (whole package, at the pre-rebase tree, ok 920s); at this head go build ./... and the gateway's guard, read-only and Library tests: ok. - mica: GOOS=linux and GOOS=windows go vet of agent, provider, gateway, library: ok. gofmt -l: clean. - mica at this head: go test ./internal/gui (budget ok), i18n-keys, agent-leave-magpie, agent-connect in chromium + webkit: ok. - mica, chromium + webkit, before the last rebase: i18n-keys, i18n-en-no-cjk, agent-leave-magpie, agent-connect, agent-connect-no-models, agent-connected-unfolded: 0 fail. Broken by hand: with connectRemote cut out of connect(), TestConnectFollowsTheRemoteMagpiesPicks fails (connected on aaa/claude-opus-5-5 (alike)); with the follow's "still the recorded value" check made always true, it fails on the user's pick written over. Not tried: two real computers; only a fake remote in the tests. Left for #6824: a headless `magpie serve` client doesn't follow later changes (the loop runs in the GUI), an empty field the remote fills later isn't filled, and WSL / claude#name agent ids don't match across machines.
165 lines
5.3 KiB
Go
165 lines
5.3 KiB
Go
package agent
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"path/filepath"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// remotePicksFake is a remote magpie that tells its agents' picks.
|
|
type remotePicksFake struct {
|
|
mu sync.Mutex
|
|
picks map[string]map[string]string
|
|
asked int
|
|
}
|
|
|
|
func (f *remotePicksFake) set(agent, key, ref string) {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
if f.picks[agent] == nil {
|
|
f.picks[agent] = map[string]string{}
|
|
}
|
|
f.picks[agent][key] = ref
|
|
}
|
|
|
|
func (f *remotePicksFake) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
if r.Header.Get("x-api-key") != "secret" && r.Header.Get("Authorization") != "Bearer secret" {
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
return
|
|
}
|
|
if r.URL.Path != provider.RemoteAgentModelsPath {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
f.asked++
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(provider.AgentModels{Agents: f.picks})
|
|
}
|
|
|
|
func resetRemoteCache() {
|
|
remoteCache.Lock()
|
|
remoteCache.at, remoteCache.picks = time.Time{}, nil
|
|
remoteCache.Unlock()
|
|
}
|
|
|
|
// A client of a remote magpie (KoiLx0 on X) connects Claude Code on the
|
|
// model the remote's Claude Code is on, follows the remote's change, and
|
|
// keeps a model the user picks here as theirs.
|
|
func TestConnectFollowsTheRemoteMagpiesPicks(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: "aaa", Name: "AAA", Chat: "https://a.example/v1", Key: "k", Models: []string{"claude-opus-5-5"}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := provider.Save(provider.Provider{ID: "office", Name: "Office", Preset: provider.RemoteMagpiePreset, Chat: srv.URL, Key: "secret",
|
|
Models: []string{"group/smart", "group/fast", "fake/claude-opus-9"}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
path := filepath.Join(home, ".claude", "settings.json")
|
|
writeFile(t, path, `{"model": "opus"}`)
|
|
|
|
c := claude(home)
|
|
conn, err := c.ConnectHow()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if m := writtenModel(path); m != "office/group/fast" || conn.How != "remote" {
|
|
t.Fatalf("connected on %q (%s), not the remote's group/fast:\n%s", m, conn.How, readFile(path))
|
|
}
|
|
|
|
// the remote's user moves its Claude Code to group/smart: followed here
|
|
fake.set("claude", "model", "group/smart")
|
|
ctx := context.Background()
|
|
c.followRemote(remotePicks(ctx, 0))
|
|
if m := writtenModel(path); m != "office/group/smart" {
|
|
t.Fatalf("the remote's change isn't followed: on %q", m)
|
|
}
|
|
|
|
// the user picks a model here: theirs, whatever the remote does after
|
|
if err := c.Pick("model", "aaa/claude-opus-5-5"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
c.followRemote(remotePicks(ctx, 0))
|
|
fake.set("claude", "model", "group/fast")
|
|
c.followRemote(remotePicks(ctx, 0))
|
|
if m := writtenModel(path); m != "aaa/claude-opus-5-5" {
|
|
t.Fatalf("the user's pick was written over: on %q", m)
|
|
}
|
|
if rec := stashShared()[remoteKey("claude", "model")]; rec != "" {
|
|
t.Fatalf("still followed after the user's pick: %q", rec)
|
|
}
|
|
|
|
// a pick the remote doesn't serve this computer is left alone
|
|
if err := c.Pick("model", "office/group/smart"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
fake.set("claude", "model", "group/smart")
|
|
c.followRemote(remotePicks(ctx, 0)) // on the remote's pick again: followed
|
|
fake.set("claude", "model", "group/not-listed-here")
|
|
c.followRemote(remotePicks(ctx, 0))
|
|
if m := writtenModel(path); m != "office/group/smart" {
|
|
t.Fatalf("moved to a model this magpie doesn't list: on %q", m)
|
|
}
|
|
fake.set("claude", "model", "fake/claude-opus-9")
|
|
c.followRemote(remotePicks(ctx, 0))
|
|
if m := writtenModel(path); m != "office/fake/claude-opus-9" {
|
|
t.Fatalf("followed no more after a pick this magpie didn't list: on %q", m)
|
|
}
|
|
}
|
|
|
|
// With no Remote magpie, connect asks no one and picks as before.
|
|
func TestConnectWithoutRemoteAsksNoOne(t *testing.T) {
|
|
home, _ := codexHome(t, "", "")
|
|
resetRemoteCache()
|
|
t.Cleanup(resetRemoteCache)
|
|
if err := provider.Save(provider.Provider{ID: "aaa", Name: "AAA", Chat: "https://a.example/v1", Key: "k", Models: []string{"first", "claude-opus-5-5"}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
path := filepath.Join(home, ".claude", "settings.json")
|
|
writeFile(t, path, `{"model": "opus"}`)
|
|
conn, err := claude(home).ConnectHow()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if m := writtenModel(path); m != "aaa/claude-opus-5-5" || conn.How == "remote" {
|
|
t.Fatalf("connected on %q (%s)", m, conn.How)
|
|
}
|
|
}
|
|
|
|
// The remote side: a connected agent tells the catalog ref of its model,
|
|
// one not connected tells nothing.
|
|
func TestSharedModelsTellsTheRefs(t *testing.T) {
|
|
home, _ := codexHome(t, "", "")
|
|
resetRemoteCache()
|
|
t.Cleanup(resetRemoteCache)
|
|
if err := provider.Save(provider.Provider{ID: "aaa", Name: "AAA", Chat: "https://a.example/v1", Key: "k", Models: []string{"m1", "claude-opus-5-5"}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
path := filepath.Join(home, ".claude", "settings.json")
|
|
writeFile(t, path, `{"model": "opus"}`)
|
|
c := claude(home)
|
|
if m := c.sharedModels(); len(m) != 0 {
|
|
t.Fatalf("not connected, told %v", m)
|
|
}
|
|
if err := c.Connect(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if m := c.sharedModels(); m["model"] != "aaa/claude-opus-5-5" {
|
|
t.Fatalf("told %v", m)
|
|
}
|
|
}
|