Skip to content

Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.

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

yetonedf6bd3efecc7
diff --git a/internal/gateway/trace.go b/internal/gateway/trace.go
index b923dcfc5..c651dabe2 100644
--- a/internal/gateway/trace.go
+++ b/internal/gateway/trace.go
@@ -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) }
diff --git a/internal/gateway/trace_gather_test.go b/internal/gateway/trace_gather_test.go
new file mode 100644
index 000000000..a1578c683
--- /dev/null
+++ b/internal/gateway/trace_gather_test.go
@@ -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)
+ }
+}
diff --git a/internal/gui/assets/plugins/routing/client.js b/internal/gui/assets/plugins/routing/client.js
index 82a7b68d4..18ad29e5c 100644
--- a/internal/gui/assets/plugins/routing/client.js
+++ b/internal/gui/assets/plugins/routing/client.js
@@ -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
diff --git a/internal/gui/tests/panel-routing-hidden.test.cjs b/internal/gui/tests/panel-routing-hidden.test.cjs
index c5b3b6e72..acbec784b 100644
--- a/internal/gui/tests/panel-routing-hidden.test.cjs
+++ b/internal/gui/tests/panel-routing-hidden.test.cjs
@@ -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, []);
});
}
diff --git a/internal/gui/tests/routing-busy.test.cjs b/internal/gui/tests/routing-busy.test.cjs
index 65bf81cf9..a89888862 100644
--- a/internal/gui/tests/routing-busy.test.cjs
+++ b/internal/gui/tests/routing-busy.test.cjs
@@ -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: [] });
diff --git a/internal/gui/tests/routing-flood.test.cjs b/internal/gui/tests/routing-flood.test.cjs
index d72349f7a..9925039a7 100644
--- a/internal/gui/tests/routing-flood.test.cjs
+++ b/internal/gui/tests/routing-flood.test.cjs
@@ -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: [] });
diff --git a/internal/gui/tests/routing-hidden-stage.test.cjs b/internal/gui/tests/routing-hidden-stage.test.cjs
index a68d6e18c..db3666d27 100644
--- a/internal/gui/tests/routing-hidden-stage.test.cjs
+++ b/internal/gui/tests/routing-hidden-stage.test.cjs
@@ -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);
diff --git a/internal/gui/tests/routing-purpose-state.test.cjs b/internal/gui/tests/routing-purpose-state.test.cjs
index 8e2e29e37..969de952a 100644
--- a/internal/gui/tests/routing-purpose-state.test.cjs
+++ b/internal/gui/tests/routing-purpose-state.test.cjs
@@ -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")]);