From 4ca55c6b62eeac30351b17ae37ad4f4dad9fd8e8 Mon Sep 17 00:00:00 2001 From: yetone Date: Wed, 4 Nov 2026 12:00:01 +0800 Subject: [PATCH] gateway: a guard test that Only switch Claude Code's sign-in keeps every route off the Claude accounts (zhangxiaoxiaoxx on X) zhangxiaoxiaoxx on X asked for a switch so that, with Claude signed in, magpie never forwards or routes another agent's request onto it. That switch exists: Providers > Claude, "Only switch Claude Code's sign-in" (magpie accounts switch-only claude on, ec3dad75a). On it, KeptToAgent makes On() false and magpie serves nothing on the Claude accounts. Its only test asked by naming claude/... directly. TestClaudeKeptToClaudeCodeOnEveryRoute asks as OpenCode through the other routes: a routing group with a Claude member first (goes on to its other member), a provider whose fallback is a Claude model, and a group whose classifier is a Claude model. None runs a Claude Code through the bridge. Unticked, the group reaches the Claude account again. No behaviour change. Run on mote: - go test -count=1 -run TestClaudeKeptToClaudeCode ./internal/gateway: ok at 759b4d1d7, and again after rebasing on f4e52659a. - wsl-run, the same -run: ok. - go test -count=1 -timeout 45m ./... at 759b4d1d7: 86 packages ok, internal/gui red in TestCoreScriptBudget (#5139) and TestMainWindowCorners/macOS_26_SDK (#3949), both filed already; this commit touches neither. Broken by hand: On() without !KeptToAgent(): group member, fallback and classifier each red (a Claude Code ran through the bridge), and TestClaudeKeptToClaudeCodeServesNothing red. Not tried: a group magpie found (auto/claude-*): in the test sandbox the Claude account lists no catalog model, so no such group formed with it. It plans through the same per-member path as a user's group. --- internal/gateway/claude_kept_routes_test.go | 84 +++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 internal/gateway/claude_kept_routes_test.go diff --git a/internal/gateway/claude_kept_routes_test.go b/internal/gateway/claude_kept_routes_test.go new file mode 100644 index 000000000..cc5233311 --- /dev/null +++ b/internal/gateway/claude_kept_routes_test.go @@ -0,0 +1,84 @@ +package gateway + +import ( + "net/http/httptest" + "os" + "path/filepath" + "strings" + "testing" + + "github.com/yetone/magpie/internal/provider" +) + +// zhangxiaoxiaoxx on X: with Claude signed in, a switch so that no other +// agent's request is forwarded or routed onto it. Only switch Claude +// Code's sign-in is that switch, and it holds on every route that reaches +// the Claude accounts, not only a request naming them: a routing group +// with a Claude member first, a provider's fallback to one and a group's +// classifier on one, each asked by another agent. None runs a Claude +// Code through the bridge; the group goes on to its other member. +func TestClaudeKeptToClaudeCodeOnEveryRoute(t *testing.T) { + claudeMadeFirst(t, false) + calls := filepath.Join(t.TempDir(), "calls") + claudeRefuses(t, calls, "tok-none") // every account answers + serveOn(t, "b", "kb", []string{"m"}, &keyed{}) + serveOn(t, "f", "kf", []string{"m"}, &keyed{fail: map[string]int{"kf": 500}}) + f, err := provider.Find("f") + if err != nil { + t.Fatal(err) + } + f.Fallback = []string{"claude/claude-sonnet-5"} + if err := provider.Save(*f); err != nil { + t.Fatal(err) + } + if err := provider.SaveGroup(provider.Group{ID: "first", Name: "First", Members: []string{"claude/claude-sonnet-5", "b/m"}, Routing: provider.Ordered}); err != nil { + t.Fatal(err) + } + if err := provider.SaveGroup(provider.Group{ID: "judged", Name: "Judged", Members: []string{"b/m"}, Routing: provider.Ordered, + Classifier: "claude/claude-sonnet-5", Rules: []provider.Rule{{Use: "b/m", Intent: "refactoring"}}}); err != nil { + t.Fatal(err) + } + if err := provider.SetSwitchOnly("claude", true); err != nil { + t.Fatal(err) + } + s := New() + t.Cleanup(s.subscription.abortAll) + ask := func(model string) (int, string) { + body := `{"model":"` + model + `","messages":[{"role":"user","content":"rename this package"}]}` + rec := httptest.NewRecorder() + req := httptest.NewRequest("POST", "/v1/chat/completions", strings.NewReader(body)) + req.Header.Set("User-Agent", "opencode/1.2.3") + s.Handler().ServeHTTP(rec, req) + return rec.Code, rec.Body.String() + } + ran := func(route string) { + t.Helper() + if b, _ := os.ReadFile(calls); len(b) != 0 { + t.Errorf("%s: a Claude Code ran through the bridge: %q", route, b) + } + os.Remove(calls) + } + if code, out := ask("group/first"); code != 200 || !strings.Contains(out, "from kb") { + t.Errorf("group with a Claude member first: %d %s", code, out) + } + ran("group member") + ask("f/m") // f fails, its fallback is a Claude account + ran("fallback") + if code, out := ask("group/judged"); code != 200 || !strings.Contains(out, "from kb") { + t.Errorf("group judged by a Claude model: %d %s", code, out) + } + ran("classifier") + + // unticked, the same routes reach the Claude accounts again: the + // test sees the bridge when it is used + os.Remove(calls) + if err := provider.SetSwitchOnly("claude", false); err != nil { + t.Fatal(err) + } + if code, out := ask("group/first"); code != 200 || !strings.Contains(out, "healthy account") { + t.Fatalf("unticked: %d %s", code, out) + } + if b, _ := os.ReadFile(calls); len(b) == 0 { + t.Fatal("unticked: no Claude Code ran") + } +}