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>
100 lines
3.3 KiB
Go
100 lines
3.3 KiB
Go
package gateway
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http/httptest"
|
|
"os"
|
|
"testing"
|
|
)
|
|
|
|
// The routing is this process's own, so a magpie on this computer that does
|
|
// not serve the gateway reads it here to show the same Routing page (#2606).
|
|
// It answers this computer alone: the trace names accounts and what they
|
|
// served, and the magpie that reads it is on the same machine.
|
|
func TestGatewayTraceIsLoopbackOnly(t *testing.T) {
|
|
h := New().Handler()
|
|
|
|
t.Run("this computer", func(t *testing.T) {
|
|
w := httptest.NewRecorder()
|
|
r := httptest.NewRequest("GET", "/v1/magpie/trace", nil)
|
|
r.RemoteAddr = "127.0.0.1:50123"
|
|
h.ServeHTTP(w, r)
|
|
if w.Code != 200 {
|
|
t.Fatalf("%d %s", w.Code, w.Body)
|
|
}
|
|
var got struct {
|
|
Object string `json:"object"`
|
|
Seq int64 `json:"seq"`
|
|
Routes []json.RawMessage `json:"routes"`
|
|
Pid int `json:"pid"`
|
|
Serves string `json:"serves"`
|
|
}
|
|
if err := json.Unmarshal(w.Body.Bytes(), &got); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got.Object != "trace" || got.Routes == nil {
|
|
t.Fatalf("object %q routes %v", got.Object, got.Routes)
|
|
}
|
|
// the console names the magpie routing from the trace it plays
|
|
if got.Pid != os.Getpid() || got.Serves != Serves {
|
|
t.Errorf("the trace doesn't name its magpie: pid %d serves %q", got.Pid, got.Serves)
|
|
}
|
|
})
|
|
|
|
// a proxy or tunnel on this computer (cloudflared, Tailscale serve)
|
|
// hands on someone else's request from loopback
|
|
t.Run("through a proxy here", func(t *testing.T) {
|
|
for _, hdr := range []string{"X-Forwarded-For", "Cf-Connecting-IP", "Tailscale-User-Login"} {
|
|
w := httptest.NewRecorder()
|
|
r := httptest.NewRequest("GET", "/v1/magpie/trace", nil)
|
|
r.RemoteAddr = "127.0.0.1:50123"
|
|
r.Header.Set(hdr, "203.0.113.7")
|
|
h.ServeHTTP(w, r)
|
|
if w.Code != 403 {
|
|
t.Fatalf("a request a proxy handed on (%s) reached the trace: %d %s", hdr, w.Code, w.Body)
|
|
}
|
|
}
|
|
})
|
|
|
|
t.Run("another machine", func(t *testing.T) {
|
|
w := httptest.NewRecorder()
|
|
r := httptest.NewRequest("GET", "/v1/magpie/trace", nil)
|
|
r.RemoteAddr = "192.168.0.9:50123"
|
|
h.ServeHTTP(w, r)
|
|
if w.Code != 403 {
|
|
t.Fatalf("another machine reached the trace: %d %s", w.Code, w.Body)
|
|
}
|
|
})
|
|
}
|
|
|
|
// Another magpie's trace is read from this computer alone (#2606): an
|
|
// address on another interface or host is never asked, whatever
|
|
// MAGPIE_ADDR says, and one listening everywhere is read on loopback.
|
|
func TestOwnerReadOnLoopbackAlone(t *testing.T) {
|
|
for addr, want := range map[string]string{
|
|
"127.0.0.1:3561": "http://127.0.0.1:3561",
|
|
"0.0.0.0:3561": "http://127.0.0.1:3561",
|
|
":3561": "http://127.0.0.1:3561",
|
|
"[::]:3561": "http://127.0.0.1:3561",
|
|
"localhost:3561": "http://127.0.0.1:3561",
|
|
"[::1]:3561": "http://[::1]:3561",
|
|
"127.0.0.2:3561": "http://127.0.0.2:3561",
|
|
"192.168.1.9:3561": "",
|
|
"10.0.0.5:3561": "",
|
|
"example.com:3561": "",
|
|
"magpie.tail:3561": "",
|
|
} {
|
|
t.Setenv("MAGPIE_ADDR", addr)
|
|
if got := ownerURL(); got != want {
|
|
t.Errorf("MAGPIE_ADDR %s: read at %q, want %q", addr, got, want)
|
|
}
|
|
}
|
|
t.Setenv("MAGPIE_ADDR", "192.0.2.1:3561")
|
|
if _, ok := OwnerTrace(t.Context(), 0, 0); ok {
|
|
t.Error("a trace read from another host")
|
|
}
|
|
if _, ok := OwnerLanes(); ok {
|
|
t.Error("lanes read from another host")
|
|
}
|
|
}
|