routing: out of sight the page stops asking for the trace, and a waiting trace read gathers a burst of changes into one answer

The Routing page (and the tray panel's Routing tab) read
/api/gateway/trace in a loop for as long as magpie ran, in sight or not.
The read was already a long-poll, but each change woke it: a request's
way is told in a burst (begun, a try sent, answered, its usage), so
under traffic every open page was answered 4-8 times a second, hidden
pages included, for nothing they drew.

Server (internal/gateway/trace.go): once a change wakes a waiting
Trace, it gathers for traceGather (250ms) before answering, so a burst
is one answer; a read with nothing new still blocks until a change or
the 25s wait. Every answer now also lists the ids of all the routes
the trace keeps (TraceState.IDs, "ids"), changed or not; OwnerTrace
passes them through from another magpie serving the gateway.

Client (plugins/routing/client.js): after its first answer, poll()
asks only while the page or the panel's Routing tab is in sight
(document not hidden, and the view shown or the panel's tab picked).
Out of sight it parks; start() (visibilitychange, focus, the view's
hidden attribute) and panelRoutingShown resume it. The first answer
after a resume is a catch-up: what came meanwhile is listed, and only
the requests still under way fly (stage and panel), as #302 asks. A
route last heard of as under way that is missing from "ids" (it ended
and was trimmed while the page was away) is dropped, so it can't show
as live forever. An answer with no "ids" (an older magpie) leaves the
routes as before.

Measured:
  - Hidden page, 2s with a request every 80ms (routing-hidden-stage,
    panel-routing-hidden): before, the trace was read 24-25 times in
    2s by each of the main window (another page picked, or the window
    hidden) and the hidden tray panel; after, 0 times.
  - A burst of 4 changes 20ms apart (TestTraceWaitHearsABurstOnce):
    before, read in 4 answers; after, 1.

Tests:
  - TestTraceWaitHearsABurstOnce (gateway, new): a waiting read
    answers a burst once, with all of it and the route's id in IDs.
    Broken by hand: no gather -> "a burst of 4 changes was read in 4
    answers, want 1"; no ids -> "ids [], want [<id>]".
  - routing-hidden-stage and panel-routing-hidden now assert the page
    reads the trace at most once out of sight (was: at least 20), and
    shows the newest request once back. The mock is a clock-driven
    gateway (every change since `after`, at once). With the old
    client.js all 12 went red: "the trace was read 24/25 times out of
    sight", "... while hidden".
  - routing-busy and routing-flood's mocks answered one route per
    read; the gateway answers every change since `after`, so they do
    too now. routing-purpose-state sent one update while another page
    was shown and required a new read to start out there; that send
    no longer waits for one.

Run at this commit's tree (go1.26.8 darwin/arm64, sandbox HOME):
  gofmt -l .: nothing
  GOOS=linux go vet -tags nogui ./...: ok
  GOOS=windows go vet -tags nogui ./...: ok
  go test -tags nogui ./internal/gateway ./internal/gui: ok
  go test -tags nogui -race -timeout 45m ./internal/gateway
    ./internal/gui: ok (gateway 1598s, gui 348s)
  set -o pipefail; go test -tags nogui -timeout 45m ./...: ok, every
    package (66 ok, 20 with no test files)
  GUI, playwright 1.62.0, chromium and webkit, every routing*.test.cjs
    and panel-routing*.test.cjs (97 files, 1204 tests, concurrency 4):
    1203 pass, 1 red: routing-rule-remove "chromium en 390: a rule's
    ... is there to remove it" (routing-rule-remove.test.cjs:158,
    "1 !== 0", the rule count read right after a keyboard Enter). Its
    trace mock never answers a wait, so its polling is the same before
    and after this change; alone it passed 3 runs of 3 (60/60).
  The whole GUI suite (822 files) was still running when this was
  written.
This commit is contained in:
yetone
2026-10-21 07:32:11 +08:00
parent 6a050eebe6
commit df6bd3efec
8 changed files with 172 additions and 41 deletions
+23 -3
View File
@@ -492,6 +492,10 @@ type TraceState struct {
Seq int64 `json:"seq"`
Routes []Route `json:"routes"`
Totals Totals `json:"totals"`
// IDs are every route the trace keeps now, changed or not: a page that
// stopped reading while out of sight drops a request it last heard of
// under way that has since ended and gone from the trace
IDs []int64 `json:"ids,omitempty"`
// By is the magpie whose routing this is, when it is another magpie's
// (OwnerTrace): the console names it from here, so the name follows the
// trace when another magpie takes the gateway over (#2606)
@@ -690,7 +694,7 @@ func (s *Server) gatewayTrace(w http.ResponseWriter, r *http.Request) {
wait = 25 * time.Second
}
st := s.Trace(r.Context(), after, wait)
writeJSON(w, 200, map[string]any{"object": "trace", "now": time.Now(), "seq": st.Seq, "routes": st.Routes, "totals": st.Totals, "pid": os.Getpid(), "serves": Serves})
writeJSON(w, 200, map[string]any{"object": "trace", "now": time.Now(), "seq": st.Seq, "routes": st.Routes, "totals": st.Totals, "ids": st.IDs, "pid": os.Getpid(), "serves": Serves})
}
// ownerURL is the gateway's address on loopback, where the magpie serving
@@ -754,13 +758,14 @@ func OwnerTrace(ctx context.Context, after int64, wait time.Duration) (TraceStat
Seq int64 `json:"seq"`
Routes []Route `json:"routes"`
Totals Totals `json:"totals"`
IDs []int64 `json:"ids"`
Pid int `json:"pid"`
Serves string `json:"serves"`
}
if decodeJSON(b, &out) != nil || out.Object != "trace" || out.Routes == nil {
return TraceState{}, false
}
return TraceState{Seq: out.Seq, Routes: out.Routes, Totals: out.Totals, By: &TraceBy{Pid: out.Pid, Serves: out.Serves}}, true
return TraceState{Seq: out.Seq, Routes: out.Routes, Totals: out.Totals, IDs: out.IDs, By: &TraceBy{Pid: out.Pid, Serves: out.Serves}}, true
}
// OwnerLanes reads the lanes of the magpie serving the gateway's address, for
@@ -805,8 +810,9 @@ func (s *Server) Trace(ctx context.Context, after int64, wait time.Duration) Tra
if t.seq < after {
after = 0 // the gateway started over since
}
st := TraceState{Seq: t.seq, Routes: []Route{}, Totals: t.totals}
st := TraceState{Seq: t.seq, Routes: []Route{}, Totals: t.totals, IDs: make([]int64, 0, len(t.routes))}
for _, r := range t.routes {
st.IDs = append(st.IDs, r.ID)
if r.Seq > after {
c := *r
c.Order = append([]Weighed{}, r.Order...) // [] for none: the GUI reads it as a list
@@ -827,6 +833,16 @@ func (s *Server) Trace(ctx context.Context, after int64, wait time.Duration) Tra
t.mu.Unlock()
select {
case <-wake:
// a request's way is told in a burst (begun, its prompt read,
// a try sent, answered, its usage), each change waking every
// page waiting: gathered a moment, a page hears of the burst
// once, not 4-8 times a second while agents are busy
gather := time.NewTimer(traceGather)
select {
case <-gather.C:
case <-ctx.Done():
gather.Stop()
}
case <-deadline.C:
return st
case <-ctx.Done():
@@ -835,6 +851,10 @@ func (s *Server) Trace(ctx context.Context, after int64, wait time.Duration) Tra
}
}
// traceGather is how long a waiting Trace gathers changes after the first
// one wakes it.
var traceGather = 250 * time.Millisecond
// swapped reports whether served is another model than sent: not the same
// name, however dated, pinned or prefixed (usage.Swapped).
func swapped(sent, served string) bool { return usage.Swapped(sent, served) }
+66
View File
@@ -0,0 +1,66 @@
package gateway
import (
"context"
"slices"
"testing"
"time"
)
// A request's way is told to the trace in a burst of changes (begun, its
// prompt read, a try sent, answered), and each one woke the Routing page's
// long poll on its own: with agents busy, the page read the trace 4-8
// times a second. A waiting read gathers the burst and answers once. It
// also names every route the trace keeps (IDs), so a page that stopped
// reading while out of sight drops one it last heard of under way that has
// ended and gone since.
func TestTraceWaitHearsABurstOnce(t *testing.T) {
defer func(d time.Duration) { traceGather = d }(traceGather)
traceGather = 2 * time.Second // the burst's spacing, however loaded the machine, is well inside it
var s Server
r := s.trace.begin(Route{Agent: "codex", Model: "m", Tries: []Try{{ID: "relay", Model: "m", Start: time.Now()}}})
after := s.Trace(context.Background(), 0, 0).Seq
reads := 0
heard := make(chan TraceState, 1)
go func() {
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
defer cancel()
for at := after; ; {
st := s.Trace(ctx, at, 10*time.Second)
reads++
at = st.Seq
if i := slices.IndexFunc(st.Routes, func(x Route) bool { return x.ID == r.ID && x.Done }); i >= 0 || ctx.Err() != nil {
heard <- st
return
}
}
}()
// the read is waiting before the burst begins
for waiting := false; !waiting; {
s.trace.mu.Lock()
waiting = s.trace.wake != nil
s.trace.mu.Unlock()
time.Sleep(time.Millisecond)
}
steps := []func(*Route){
func(x *Route) { x.Prompt = &Prompt{} },
func(x *Route) { x.Tries[0].Status = 200 },
func(x *Route) { x.Tries[0].Done, x.Tries[0].Millis = true, 900 },
func(x *Route) { x.Done, x.Status, x.Millis = true, 200, 900 },
}
for _, f := range steps {
s.trace.update(r, f)
time.Sleep(20 * time.Millisecond)
}
st := <-heard
if reads != 1 {
t.Fatalf("a burst of %d changes was read in %d answers, want 1", len(steps), reads)
}
if len(st.Routes) != 1 || st.Routes[0].Prompt == nil || !st.Routes[0].Tries[0].Done {
t.Fatalf("the answer has %+v, want the route with the whole burst", st.Routes)
}
if !slices.Equal(st.IDs, []int64{r.ID}) {
t.Fatalf("ids %v, want [%d]", st.IDs, r.ID)
}
}
+30 -2
View File
@@ -3592,6 +3592,7 @@ function routingPage(part) {
// Visibility events can arrive after the browser has suspended frames, so
// finish hidden work here too. The first visible frame alone owns resume.
function start() {
inSightAgain();
if (!shown()) { pause(); return; }
kick();
}
@@ -3647,11 +3648,26 @@ function routingPage(part) {
for (const d of document.querySelectorAll(".rt-livedot")) d.classList.toggle("on", on);
}
// the trace is asked for only while the page or the tray panel's Routing
// tab is in sight: a hidden page held a long-poll open and was answered
// on every change, 4 to 8 times a second under traffic, for nothing it
// drew. Out of sight it asks nothing; in sight again it asks once for all
// that changed meanwhile, and plays only the requests still under way
const inSight = () => !document.hidden && (shown() || !!(pBox && pShown()));
let heard = false, sightWait = null, caught = false;
function inSightAgain() {
if (sightWait && inSight()) { const go = sightWait; sightWait = null; go(); }
}
async function poll() {
for (;;) {
if (heard && !inSight()) {
await new Promise((r) => { sightWait = r; });
caught = true;
}
try {
const res = await fetch(`/api/gateway/trace?after=${seq}${loaded ? "&wait=1" : ""}`);
const d = await res.json();
heard = true;
noteAccounts(d);
skew = at(d.now) - Date.now();
mine = d.mine;
@@ -3683,7 +3699,16 @@ function routingPage(part) {
if (!routes.has(r.id) && !first) fresh.push(r.id);
routes.set(r.id, whole(r));
}
// one heard of as under way that the gateway has let go of since
// (finished and trimmed while the page was out of sight) is not
// under way forever: the gateway lists every id it still keeps
if (Array.isArray(d.ids)) {
const kept = new Set(d.ids);
for (const [id, r] of routes) if (!r.done && !kept.has(id)) routes.delete(id);
}
for (const id of [...routes.keys()].sort((a, b) => a - b).slice(0, -60)) routes.delete(id);
const back = caught;
caught = false;
loaded = true;
offMsg = "";
renderPanel(first || d.routes.some((r) => r.done));
@@ -3723,11 +3748,13 @@ function routingPage(part) {
}
// played only in sight: coming back into it plays those still
// under way, not all that came meanwhile (#302)
const go = !pinned && !rp && !day && shown() ? fresh.filter((id) => matchesPurpose(routes.get(id))) : [];
const go = !back && !pinned && !rp && !day && shown() ? fresh.filter((id) => matchesPurpose(routes.get(id))) : [];
for (const id of go) playing.set(id, gen);
if (go.length) sync(); // the stage once for them all (#308)
for (const id of go) play(id, true);
for (const id of fresh.slice(-4)) pPlay(id);
// back in sight, those still under way fly, not all that came
for (const id of (back ? fresh.filter((id) => !routes.get(id).done) : fresh).slice(-4)) pPlay(id);
if (back && seen && shown()) for (const u of underWay()) if (!playing.has(u.id)) play(u.id);
wake();
renderAll();
}
@@ -5846,6 +5873,7 @@ function routingPage(part) {
renderPanel();
}
window.panelRoutingShown = () => {
inSightAgain();
if (pShown() && performance.now() - todayAt > 5e3) loadToday();
// a group switched on the Routing page or by `magpie group pick`
// since: the tab shows the model it goes to now
@@ -6,8 +6,10 @@
// tab. On Windows the hidden panel grew to 265k nodes in 11 minutes and GBs
// over a day. Hidden, the tab is drawn again only when its shape (what the
// panel's height is fitted to) changes, no request's dot flies, and once
// the panel shows it is drawn in full, newest request first. The page's
// hidden state is faked here, as headless pages are never hidden.
// the panel shows it is drawn in full, newest request first. Hidden, it
// doesn't ask for the trace either: shown again, it asks once for all that
// came meanwhile. The page's hidden state is faked here, as headless pages
// are never hidden.
const assert = require("node:assert/strict");
const fs = require("node:fs/promises");
const path = require("node:path");
@@ -23,6 +25,9 @@ const route = (id, done) => ({
});
function serve(lang) {
// a request every 80 ms, under way, whether or not the panel asks
const t0 = Date.now();
const seqNow = () => 2 + Math.floor((Date.now() - t0) / 80);
const state = { agents: [{ id: "codex", name: "Codex", path: "/test/config.toml", fields: [] }], profiles: [], settings: { lang, theme: "light" } };
return async (r) => {
const req = r.request(), url = new URL(req.url());
@@ -31,12 +36,15 @@ function serve(lang) {
if (url.pathname === "/wails/runtime.js") return r.fulfill({ contentType: "text/javascript", body: "export const Window = {};" });
if (url.pathname === "/api/state") return json(state);
if (url.pathname === "/api/gateway/trace") {
// a request every 80 ms, under way: each is a tab drawn again
// as the gateway does: what changed since after, at once when
// something did, or once the next request comes. Each is a tab drawn
// again
const after = Number(url.searchParams.get("after"));
const now = new Date().toISOString();
if (!after) return json({ mine: true, now, seq: 2, totals: { requests: 2 }, routes: [route(1, true), route(2, true)] });
await new Promise((res) => setTimeout(res, 80));
return json({ mine: true, now, seq: after + 1, totals: { requests: after + 1 }, routes: [route(after + 1, false)] });
if (!after) return json({ mine: true, now: new Date().toISOString(), seq: 2, totals: { requests: 2 }, routes: [route(1, true), route(2, true)] });
while (seqNow() <= after) await new Promise((res) => setTimeout(res, 20));
const seq = seqNow(), routes = [];
for (let id = Math.max(after + 1, seq - 9); id <= seq; id++) routes.push(route(id, false));
return json({ mine: true, now: new Date().toISOString(), seq, totals: { requests: seq }, routes });
}
if (url.pathname === "/api/gateway/history") return json({ cut: false, days: [], routes: [] });
if (url.pathname === "/api/usage" && url.searchParams.get("period") === "today") return json({ calls: 2, input: 1000, output: 200, series: [] });
@@ -92,26 +100,20 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium",
});
let heard = 0;
page.on("request", (q) => { if (q.url().includes("/api/gateway/trace")) heard++; });
for (let i = 0; i < 40 && heard < 20; i++) await page.waitForTimeout(100);
assert(heard >= 20, `the trace was read ${heard} times while hidden`);
// 2 s hidden: 25 requests come, and the panel asks for none
await page.waitForTimeout(2000);
assert(heard <= 1, `the trace was read ${heard} times while hidden`);
const hidden = await page.evaluate(() => ({ draws: window.__draws, dots: window.__dots, shown: Number(document.querySelector("#panelRouting .pr-req .pr-a .m").textContent.replace(/\D+/g, "")) }));
assert.equal(hidden.draws, 0, `the hidden tab was drawn ${hidden.draws} times over ${heard} requests`);
assert.equal(hidden.dots, 0, "no request's dot flies in a hidden panel");
assert.equal(hidden.shown, seqAt, "what the tab shows is left as it was");
// shown again: drawn at once, the newest request first
const after = await page.evaluate(async () => {
window.__hide(false);
await Promise.resolve(); // the observer's records
return {
draws: window.__draws,
first: Number(document.querySelector("#panelRouting .pr-req .pr-a .m").textContent.replace(/\D+/g, "")),
rows: document.querySelectorAll("#panelRouting .pr-req").length,
};
});
assert.equal(after.draws, 1, "drawn once as the panel shows");
assert(after.first >= seqAt + 20, `the newest request is shown (${after.first}, was ${seqAt})`);
assert.equal(after.rows, 6);
// shown again: it asks once for what came meanwhile, and draws the
// newest request first
await page.evaluate(() => window.__hide(false));
await page.waitForFunction((at) => Number(document.querySelector("#panelRouting .pr-req .pr-a .m").textContent.replace(/\D+/g, "")) >= at + 20, seqAt);
assert(heard >= 1, "shown again, the trace is asked for");
assert.equal(await page.locator("#panelRouting .pr-req").count(), 6);
assert.deepEqual(errors, []);
});
}
+1 -1
View File
@@ -38,7 +38,7 @@ function serve(lang, feed) {
const trace = (routes) => json({ mine: true, now: new Date().toISOString(), seq: routes.length ? routes[routes.length - 1].seq : Number(url.searchParams.get("after")), totals: { requests: 0, rerouted: 0, errors: 0 }, routes });
if (!url.searchParams.get("wait")) return trace(first);
while (!feed.q.length && !feed.closed) await new Promise((r) => setTimeout(r, 20));
return trace(feed.q.length ? [feed.q.shift()] : []).catch(() => {});
return trace(feed.q.splice(0)).catch(() => {}); // every change since, as the gateway answers
}
if (url.pathname === "/api/gateway/history") return json({ cut: false, days: [], routes: [] });
if (url.pathname === "/api/groups") return json({ models: [], groups: [], pools: [] });
+1 -1
View File
@@ -46,7 +46,7 @@ function serve(lang, feed) {
if (!url.searchParams.get("wait")) return trace(first);
// the long poll: answered with the next request the test feeds
while (!feed.q.length && !feed.closed) await new Promise((r) => setTimeout(r, 20));
return trace(feed.q.length ? [feed.q.shift()] : []).catch(() => {});
return trace(feed.q.splice(0)).catch(() => {}); // every change since, as the gateway answers
}
if (url.pathname === "/api/gateway/history") return json({ cut: false, days: [], routes: [] });
if (url.pathname === "/api/groups") return json({ models: [], groups: [], pools: [] });
@@ -6,8 +6,11 @@
// hidden window no frame is drawn, so the entrance and live animations on
// the nodes it replaced stayed on the document's timeline and held each one:
// WebView2 grew to GBs over a day. Out of sight the page is left as it was,
// and back in sight it is drawn at once with the newest request. The
// window's hidden state is faked, as headless pages are never hidden.
// and back in sight it is drawn at once with the newest request. Out of
// sight it doesn't ask for the trace either: a long-poll held open there was
// answered on every change, 4 to 8 times a second under traffic, for
// nothing it drew. The window's hidden state is faked, as headless pages
// are never hidden.
const assert = require("node:assert/strict");
const fs = require("node:fs/promises");
const path = require("node:path");
@@ -26,6 +29,10 @@ const route = (id, done) => ({
});
function serve(lang) {
// a request every 80 ms, and the one before it done, whether or not the
// page asks: the gateway's trace goes on without it
const t0 = Date.now();
const seqNow = () => 2 + Math.floor((Date.now() - t0) / 80);
const state = {
agents: [{ id: "codex", name: "Codex", path: "/test/config.toml", fields: [] }, { id: "claude", name: "Claude Code", path: "/test/settings.json", fields: [] }],
profiles: [], settings: { lang, theme: "light" },
@@ -37,12 +44,16 @@ function serve(lang) {
if (url.pathname === "/wails/runtime.js") return r.fulfill({ contentType: "text/javascript", body: "export const Window = {};" });
if (url.pathname === "/api/state") return json(state);
if (url.pathname === "/api/gateway/trace") {
// a request every 80 ms, and the one before it done
// as the gateway does: what changed since after, at once when
// something did, or once the next request comes
const after = Number(url.searchParams.get("after"));
const now = new Date().toISOString();
if (!after) return json({ mine: true, now, seq: 2, totals: { requests: 2 }, routes: [route(1, true), route(2, true)] });
await new Promise((res) => setTimeout(res, 80));
return json({ mine: true, now, seq: after + 1, totals: { requests: after + 1 }, routes: [route(after, true), route(after + 1, false)] });
if (after) while (seqNow() <= after) await new Promise((res) => setTimeout(res, 20));
const seq = seqNow(), now = new Date().toISOString();
const routes = [];
for (let id = Math.max(1, after, seq - 4); id <= seq; id++) routes.push(route(id, id < seq));
const ids = [];
for (let id = Math.max(1, seq - 59); id <= seq; id++) ids.push(id);
return json({ mine: true, now, seq, totals: { requests: seq }, routes, ids });
}
if (url.pathname === "/api/gateway/history") return json({ cut: false, days: [], routes: [] });
if (url.pathname === "/api/usage" && url.searchParams.get("period") === "today") return json({ calls: 2, input: 1000, output: 200, series: [] });
@@ -110,11 +121,12 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium",
});
let heard = 0;
page.on("request", (q) => { if (q.url().includes("/api/gateway/trace")) heard++; });
for (let i = 0; i < 50 && heard < 20; i++) await page.waitForTimeout(100);
assert(heard >= 20, `the trace was read ${heard} times out of sight`);
// 2 s out of sight: 25 requests come, and the page asks for none
await page.waitForTimeout(2000);
assert(heard <= 1, `the trace was read ${heard} times out of sight`);
// a stage's node, a row, a wire, a list's request: none of them put up
const added = await page.evaluate(() => window.__added);
assert.deepEqual(added, {}, `out of sight over ${heard} requests, the page added ${JSON.stringify(added)}`);
assert.deepEqual(added, {}, `out of sight, the page added ${JSON.stringify(added)}`);
// back in sight: drawn at once, with the newest request
await how.show(page);
@@ -85,10 +85,13 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium",
await page.waitForFunction((v) => document.querySelector(".rt-purpose-tools").classList.contains("set") === !!v, value);
if (await page.locator("#rtPurpose").getAttribute("aria-expanded") === "true") await page.keyboard.press("Escape");
};
const send = async (rs) => {
// away: sent while another page is shown, where the page asks for no
// more until it is back in sight
const send = async (rs, away) => {
for (let i = 0; i < 100 && !feed.next; i++) await page.waitForTimeout(20);
assert(feed.next, "trace poll is waiting");
feed.next(rs);
if (away) return;
// The following poll begins after all of this response was applied.
for (let i = 0; i < 100 && !feed.next; i++) await page.waitForTimeout(20);
assert(feed.next, "trace update was applied");
@@ -118,7 +121,7 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium",
assert.deepEqual(await counts(), ["4", "1", "2"]);
// Resume after another page, then a matching new call, both keep the scope.
await page.evaluate(() => window.show("providers"));
await send([req(104, "review")]);
await send([req(104, "review")], true);
await page.evaluate(() => window.show("routing"));
await story(102);
await send([req(105, "thread_title")]);