fatcutegg's patch (fatcutegg/magpie 40ceb22df). The routing trace is held
by the process that serves the gateway. A second magpie on this computer
(`magpie web`, or the app while a launchd `magpie serve` holds the port)
showed none of it. Now its console reads the serving magpie's
GET /v1/magpie/trace (and /v1/magpie/concurrency for the lanes) over
loopback, and plays that trace as its own. The heading names which magpie
is routing ("serve (pid N) is routing").
Reviewed as a community change. A trace names accounts and emails, session
ids, a folder's last two parts and error text. It holds no prompt text and
no key. The endpoint answers local(r) alone: a request from another address
gets 403, and so does a loopback request carrying a proxy or Tailscale
header, or a LAN request while the gateway is shared, whatever key it holds.
lanGuard and corsGuard stop rebinding Hosts, Origin: null and *.localhost
first, as on every route.
Changed on top of the patch:
- A gateway key held to a budget, models or accounts is refused with
KeyHeldCode (refuseHeld), as concurrency and quotas refuse it.
- The console reads loopback alone (ownerURL). A non-loopback MAGPIE_ADDR is
never asked, and 0.0.0.0, :port, [::] and localhost are read at
127.0.0.1.
- OwnerTrace lets a hung magpie go at wait+5s (was +20s). OwnerLanes waits
700ms like ServedBy (was 2s) and is asked only when a magpie answers.
- `/` tells pid and serves to a loopback request alone. A plain `magpie`
that opens the TUI reports itself as "tui".
- Takeover: in the real two-instance run, after B took the gateway over the
hub still read "serve (pid A) is routing", from the providers state. Now
the trace carries `by` (pid, serves), and the page names the router from
it. When `by` changes, the page drops its routes and reads the new
magpie's whole trace, so route ids from two magpies never mix. The
label's full text is in its title, because the node cuts it with an
ellipsis.
- Trace's doc comment, which the patch had orphaned, is back on it. Docs
(gateway-routing.md, gui-shell.md) say all of the above. Issue
references are #2606 (were #110).
- Tests: a proxy-header subtest, TestOwnerReadOnLoopbackAlone, the trace
added to TestHeldKeyToldNoAccounts, and `by` / pid / serves asserted in
both packages' peer tests.
Run:
- Each guarantee broken by hand, and its test went red: refuseHeld removed,
ownerURL returning MAGPIE_ADDR, local(r) replaced by a RemoteAddr check,
pid/serves dropped from the answer, `by` dropped in OwnerTrace, and the
console's OwnerTrace fallback turned off.
- A real sandbox (own HOME, ports 3591-3593, fake upstream on 3564):
- A `magpie serve`, B `magpie web`. B's Routing page played A's routes
live (the long poll counted 3 requests through A).
- Probes of A's /v1/magpie/trace. 200 from plain loopback. 403 for
evil Origin, Origin: null, evil.localhost, a rebound Host with and
without Origin, x.localhost Host, X-Forwarded-For and
Tailscale-User-Login.
- A LAN-shared instance answered 403 from 10.0.0.111 even with a valid
key, and gave no pid on `/`.
- Killed A: B took over in ~11s. The hub dropped the "is routing" label,
and the next request through B showed as a fresh trace (1 request).
- Chromium and WebKit screenshots at 1280 and 390 wide, in en, zh and ja.
No horizontal scroll.
- gofmt -l clean. go vet darwin, GOOS=linux -tags nogui and GOOS=windows
pass, and all three builds pass.
- go test -timeout 45m ./... at this tree (sandbox HOME, load 250-400):
every package ok except four.
- agent, gateway and library hit 45m in TestClaudeTierPickedAsTheMainModelStays,
TestQueuedStreamKeptAliveOnceSentToItsVendor and TestWellKnownKitGrouped.
- plugin failed TestGatewayHookMixedPiPackage (#2865).
- Rerun alone with -p 1: plugin ok (464s). gateway finished in 45m with
one red, TestRequestBuildsProvidersOnce, a 900ms/1s timing window under
load. It is 8/8 green alone here and on origin/main f73378365. Filed as
#2965.
- agent ok (2162s) and library ok (377s), each run alone.
- GUI suite, whole, Chromium (playwright 1.62.0, 8 at a time, load
~300): 5608 of 5661 pass. The 15 red files were run alone at this tree
and at origin/main f73378365. The fails are the same set, give or take
load (136 of 162 pass here, 137 of 162 on main), and each already has
an issue: routing-bodies #2559, segs-glide #2683, lightweight #2638,
codex-predictions #2624, routing-model-rest #2115, routing-keys-fold
#1612/#2658, quota-count-wrap #2228/#2399, sidebar-overlay-scrollbar
#1863. routing-keys-fold's live fold test, which plays a live trace, is
3 of 3 green alone both here and on main.
- WebKit: the whole pass was cut short at ~1576 tests by load (nothing
routing-related red in it). The 9 files that drive the hub or the live
trace (routing-js, routing-keys-fold, routing-group-fold,
routing-held-dot, routing-held-member, routing-answered-still,
routing-weight-chip, routing-mode-goes, claude-subagent-tag) are 108 of
108 in WebKit here.
Co-authored-by: fatcutegg <29221285+fatcutegg@users.noreply.github.com>
125 lines
4.1 KiB
Go
125 lines
4.1 KiB
Go
package gui
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
// A magpie here that does not serve the gateway shows the routing of the one
|
|
// that does (#2606), and names it: the console reads the serving magpie's
|
|
// trace over loopback and plays it as its own, and its state carries how that
|
|
// magpie was started and its pid.
|
|
func TestConsoleShowsTheServingMagpiesRouting(t *testing.T) {
|
|
h := t.TempDir()
|
|
t.Setenv("HOME", h)
|
|
t.Setenv("USERPROFILE", h)
|
|
t.Setenv("XDG_CONFIG_HOME", filepath.Join(h, ".config"))
|
|
t.Setenv("XDG_CACHE_HOME", filepath.Join(h, ".cache"))
|
|
t.Setenv("XDG_DATA_HOME", filepath.Join(h, ".local", "share"))
|
|
ln, err := net.Listen("tcp", "127.0.0.1:0")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Setenv("MAGPIE_ADDR", ln.Addr().String())
|
|
other := &http.Server{Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
switch r.URL.Path {
|
|
case "/":
|
|
w.Write([]byte(`{"name":"magpie","version":"0.1.1257","models":3,"window":true,"serves":"serve","pid":4321,"apis":[]}`))
|
|
case "/v1/magpie/trace":
|
|
w.Write([]byte(`{"object":"trace","seq":7,"pid":4321,"serves":"serve","routes":[{"id":1,"model":"ollama/gemma4:e4b","provider":"ollama","status":200}]}`))
|
|
default:
|
|
http.NotFound(w, r)
|
|
}
|
|
})}
|
|
go other.Serve(ln)
|
|
defer other.Close()
|
|
served.Store(nil)
|
|
|
|
mux := http.NewServeMux()
|
|
traceRoutes(mux)
|
|
w := httptest.NewRecorder()
|
|
mux.ServeHTTP(w, httptest.NewRequest("GET", "/api/gateway/trace", nil))
|
|
if w.Code != 200 {
|
|
t.Fatalf("%d %s", w.Code, w.Body)
|
|
}
|
|
var got struct {
|
|
Mine bool `json:"mine"`
|
|
By *struct {
|
|
Pid int `json:"pid"`
|
|
Serves string `json:"serves"`
|
|
} `json:"by"`
|
|
Routes []struct {
|
|
Model string `json:"model"`
|
|
Status int `json:"status"`
|
|
} `json:"routes"`
|
|
}
|
|
if err := json.Unmarshal(w.Body.Bytes(), &got); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !got.Mine || len(got.Routes) != 1 || got.Routes[0].Model != "ollama/gemma4:e4b" || got.Routes[0].Status != 200 {
|
|
t.Fatalf("the serving magpie's routing was not shown: mine %v routes %+v", got.Mine, got.Routes)
|
|
}
|
|
// the trace names the magpie it came from, so the page's name for it
|
|
// changes with the trace when another magpie takes the gateway over
|
|
if got.By == nil || got.By.Pid != 4321 || got.By.Serves != "serve" {
|
|
t.Errorf("the trace doesn't name the magpie it came from: %+v", got.By)
|
|
}
|
|
|
|
// the state names it, for the page's heading to say which one is routing
|
|
g := providersState().Gateway
|
|
if !g.Running || g.Mine || g.Serves != "serve" || g.Pid != 4321 {
|
|
t.Errorf("state: running %v mine %v serves %q pid %d", g.Running, g.Mine, g.Serves, g.Pid)
|
|
}
|
|
}
|
|
|
|
// One that doesn't tell its trace (older than the endpoint, or a refusal) is
|
|
// unknown, not empty: the page keeps the notice it had, and never plays
|
|
// another magpie's routing as its own.
|
|
func TestConsoleKeepsTheNoticeFromAnOlderGateway(t *testing.T) {
|
|
h := t.TempDir()
|
|
t.Setenv("HOME", h)
|
|
t.Setenv("USERPROFILE", h)
|
|
t.Setenv("XDG_CONFIG_HOME", filepath.Join(h, ".config"))
|
|
t.Setenv("XDG_CACHE_HOME", filepath.Join(h, ".cache"))
|
|
t.Setenv("XDG_DATA_HOME", filepath.Join(h, ".local", "share"))
|
|
ln, err := net.Listen("tcp", "127.0.0.1:0")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Setenv("MAGPIE_ADDR", ln.Addr().String())
|
|
other := &http.Server{Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
if r.URL.Path == "/" {
|
|
w.Write([]byte(`{"name":"magpie","version":"0.1.550","models":3,"window":true,"apis":[]}`))
|
|
return
|
|
}
|
|
http.NotFound(w, r)
|
|
})}
|
|
go other.Serve(ln)
|
|
defer other.Close()
|
|
served.Store(nil)
|
|
|
|
mux := http.NewServeMux()
|
|
traceRoutes(mux)
|
|
w := httptest.NewRecorder()
|
|
mux.ServeHTTP(w, httptest.NewRequest("GET", "/api/gateway/trace", nil))
|
|
if w.Code != 200 {
|
|
t.Fatalf("%d %s", w.Code, w.Body)
|
|
}
|
|
var got struct {
|
|
Mine bool `json:"mine"`
|
|
Routes json.RawMessage `json:"routes"`
|
|
}
|
|
if err := json.Unmarshal(w.Body.Bytes(), &got); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got.Mine {
|
|
t.Fatalf("an older gateway's routing was played as this console's own: %s", w.Body)
|
|
}
|
|
}
|