gui: the sidebar's gateway line is drawn from boot.js, before the providers' read (#3429, chiperman)

After a refresh the sidebar's gateway line came in about 1.5s late: it
was drawn only by sideGwRead, which waits 1500ms before it reads
/api/providers. chiperman's commit (c1f457a1b) has boot.js carry where
this process's gateway listens, and app.js draw it at once. Ported with
changes:
- api.go puts sideGateway in boot.js only when this process serves the
  gateway (served.Load() != nil), with the URL from gateway.URL(), the
  one /api/providers reports, and marked boot. Another magpie serving
  the port: nothing is sent, and the providers read decides as before.
- app.js starts sideGwAt from boot.js, so the first layoutNav draws it;
  chiperman's separate renderSideGwBoot is folded into that. A value
  from boot.js is read again at 1500ms like before, so /api/providers
  has the last word: served is set before the listen succeeds, so a
  listen that failed, or a gateway stopped since, shows "Gateway
  stopped" then. Without the re-read the boot line could stay stale.
- app.js is 45 bytes under its budget, which is unchanged.
Not fixed here, and pre-existing on main: Settings' address Apply leaves
the line on the old port until a reload (#3531). Seen in real sandbox
apps on main (3483 -> 3486) and with this change (3481 -> 3485).

Tests: TestBootSaysWhereItsGatewayListens (gui) checks boot.js's
sideGateway for 127.0.0.1:3481 and for 0.0.0.0:3483 shown as 127.0.0.1,
and none when this process doesn't serve. sidebar.test.cjs gets the
line from boot.js before any providers read, and a new subtest: the
boot.js line for 3425 is read again and turns to "Gateway stopped" on
3490 once providers says so, with one read.

Run on origin/main 46c418832 at this commit:
- Real app at the pre-rebase c28576084 and origin/main beside it,
  sandbox HOME, `go build -tags dev`, MAGPIE_DEV_UI
  127.0.0.1:3482, gateway on 3481 (main's on 3483), both engines,
  load and two reloads each: main shows the line 1885-2015ms after
  load; this change 70-216ms. One providers read in each.
- gofmt -l: nothing. go vet (darwin) of gui/gateway/cli,
  GOOS=linux go vet -tags nogui ./..., GOOS=windows go vet ./...: clean.
- go test -count=1 -timeout 45m ./internal/gui/ ./internal/cli/
  ./internal/gateway/: ok (gui 133s, cli 39s, gateway 405s) (TestCoreScriptBudget among them).
- GUI, both engines: sidebar, refresh-page, gateway-port, mobile-web,
  compact-at, routing-kind: 64/76; the 12 reds are sidebar's page list
  missing Insights › Masking, red on origin/main too (#3498). With that
  list given "masking" for the run only, sidebar is 18/18.
- Broken by hand: sideGwAt = null fails 10 sidebar subtests ("the
  gateway's line from boot.js"); the old `!sideGwAt` guard fails the
  new subtest in both engines; `false &&` in api.go fails
  TestBootSaysWhereItsGatewayListens.
- Whole GUI suite at the pre-rebase pair c28576084: see the #3425
  commit before this one; every red file has an issue (#3557 and #3558
  filed for it, #3535 earlier).
- Not tried: Linux or Windows real app.

Co-authored-by: chiperman <34889778+chiperman@users.noreply.github.com>
This commit is contained in:
yetone
2026-10-25 12:06:13 +08:00
co-authored by chiperman
parent 00d44d6928
commit 6874095b15
4 changed files with 67 additions and 8 deletions
+7
View File
@@ -845,6 +845,13 @@ func Handler(w Windows, gw *gateway.Server) http.Handler {
// before the first paint rather than after the settings come
list := builtinsState()
boot["featuresOff"] = builtinsOff(list)
// and where the gateway listens, so the sidebar's line at its foot
// is drawn from the start, not held back until providers has come
// in (#1519's read waits for the agents); only this process's own,
// for one served by another magpie is the providers' read to find
if served.Load() != nil {
boot["sideGateway"] = map[string]any{"url": gateway.URL(), "running": true, "boot": true}
}
// and the settings of those the page draws (kernel.js)
boot["pluginConfig"] = builtinConfig(list)
// on Omarchy the page takes its theme's look before it paints
+3 -3
View File
@@ -24382,10 +24382,10 @@ function renderCrumb(items) {
if (on) parts.push(on.textContent.trim().split(" · ")[0]);
c.replaceChildren(...parts.flatMap((p, i) => i ? [el("i", "", "/"), el(i === parts.length - 1 ? "b" : "span", "", p)] : [el(parts.length === 1 ? "b" : "span", "", p)]));
}
// the gateway at the sidebar's foot: where it listens, or that it doesn't
var sideGwAt = null;
// the gateway at the sidebar's foot, boot.js's until providers says (#3429)
var sideGwAt = window.bootPrefs?.sideGateway;
function sideGwRead() {
if (!(sideGwRead.due && sideNav() && !sideGwAt)) return;
if (!(sideGwRead.due && sideNav() && (!sideGwAt || sideGwAt.boot))) return;
// the list is not read yet: the read the Providers page is made ready
// with (#1519) serves both, one read, not two
if (!providers && mode !== "panel") {
+32
View File
@@ -9,6 +9,7 @@ import (
"strings"
"testing"
"github.com/yetone/magpie/internal/gateway"
"github.com/yetone/magpie/internal/pluginrt"
"github.com/yetone/magpie/internal/settings"
_ "github.com/yetone/magpie/plugins/all"
@@ -137,6 +138,37 @@ func TestBootSaysBuiltinsOff(t *testing.T) {
}
}
// boot.js says where the gateway listens when this process serves it, so
// the sidebar's line is drawn before the first paint (#3429), and nothing
// when it doesn't: one served by another magpie is the providers' read to
// find, never this process's guess.
func TestBootSaysWhereItsGatewayListens(t *testing.T) {
h := builtinHome(t)
t.Setenv("MAGPIE_ADDR", "127.0.0.1:3481")
boot := func() string {
rec := httptest.NewRecorder()
h.ServeHTTP(rec, httptest.NewRequest("GET", "/boot.js", nil))
return rec.Body.String()
}
prev := served.Swap(nil)
t.Cleanup(func() { served.Store(prev) })
if b := boot(); strings.Contains(b, "sideGateway") {
t.Fatalf("no gateway served here: %s", b)
}
served.Store(gateway.New())
if b := boot(); !strings.Contains(b, `"sideGateway":{"boot":true,"running":true,"url":"http://127.0.0.1:3481"}`) {
t.Fatalf("served here: %s", b)
}
t.Setenv("MAGPIE_ADDR", "0.0.0.0:3483")
if b := boot(); !strings.Contains(b, `"sideGateway":{"boot":true,"running":true,"url":"http://127.0.0.1:3483"}`) {
t.Fatalf("every interface, reached on loopback: %s", b)
}
served.Store(nil)
if b := boot(); strings.Contains(b, "sideGateway") {
t.Fatalf("stopped: %s", b)
}
}
// A plugin's settings are set only as their schema allows, kept in
// plugins.yml, and handed to the page in boot.js; a request says on or
// config, not both.
+25 -5
View File
@@ -11,15 +11,15 @@ const { chromium, webkit } = require("playwright");
const assets = path.resolve(__dirname, "../assets");
function serve(lang, reads) {
function serve(lang, reads, gateway = { url: "http://127.0.0.1:3425", running: true, window: true }) {
const state = { agents: [], profiles: [], settings: { lang, theme: "light" } };
return async (route) => {
const url = new URL(route.request().url());
const json = (data) => route.fulfill({ json: data });
if (url.pathname === "/boot.js") return route.fulfill({ contentType: "text/javascript", body: `window.bootPrefs = {lang:${JSON.stringify(lang)},theme:"light",web:false};` });
if (url.pathname === "/boot.js") return route.fulfill({ contentType: "text/javascript", body: `window.bootPrefs = {lang:${JSON.stringify(lang)},theme:"light",web:false,sideGateway:{url:"http://127.0.0.1:3425",running:true,boot:true}};` });
if (url.pathname === "/wails/runtime.js") return route.fulfill({ contentType: "text/javascript", body: "export const Window = {};" });
if (url.pathname === "/api/state") return json(state);
if (url.pathname === "/api/providers") { reads.push(performance.now()); return json({ providers: [], presets: [], excluded: [], gateway: { url: "http://127.0.0.1:3425", running: true, window: true } }); }
if (url.pathname === "/api/providers") { reads.push(performance.now()); return json({ providers: [], presets: [], excluded: [], gateway }); }
if (url.pathname === "/api/groups") return json({ groups: [] });
if (url.pathname.startsWith("/api/")) return json({});
const file = path.join(assets, url.pathname === "/" ? "index.html" : url.pathname);
@@ -89,8 +89,9 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium",
if (sel === "#prefs") assert.notEqual(pressed, bg, `${sel} pressed darkens`);
}
// the gateway's address at the foot, read once the window has settled
await page.locator("#sideGw").waitFor();
// the gateway's address at the foot, drawn from boot.js ahead of the
// read, not held back until the providers have come in
assert.ok(await page.locator("#sideGw").isVisible(), "the gateway's line from boot.js");
assert.match(await page.locator("#sideGw").textContent(), /127\.0\.0\.1:3425/);
assert.ok(await page.locator("#sideGw span").evaluate((s) => s.scrollWidth <= s.clientWidth), "the address is read whole");
assert.equal(reads.length, 1, "read once for the sidebar");
@@ -173,6 +174,25 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium",
await context.close();
});
await t.test("the line boot.js drew is read again: a gateway gone since says so", async () => {
// boot.js is written once this process holds the gateway, before its
// listen is known to have worked; the read after the start has the
// last word on the line it drew (#3429)
const reads = [];
const context = await browser.newContext({ viewport: { width: 1200, height: 700 }, reducedMotion: "reduce" });
const page = await context.newPage();
page.setDefaultTimeout(5000);
await page.route("**/*", serve("en", reads, { url: "http://127.0.0.1:3490", running: false }));
await page.goto("http://magpie.test/");
await page.waitForSelector("#nav button.on");
assert.match(await page.locator("#sideGw").textContent(), /127\.0\.0\.1:3425/, "boot.js's line first");
await page.waitForFunction(() => document.querySelector("#sideGw span")?.textContent === "Gateway stopped");
assert.ok(await page.locator("#sideGw").evaluate((b) => b.classList.contains("off")));
assert.match(await page.locator("#sideGw").getAttribute("title"), /3490/);
assert.equal(reads.length, 1, "the read the Providers page is made ready with, no other");
await context.close();
});
await t.test("the list opens and folds as it plays", async () => {
const context = await browser.newContext({ viewport: { width: 1200, height: 760 } });
const page = await context.newPage();