From 647071f2b9021e1a33435dc269269d7616d83d10 Mon Sep 17 00:00:00 2001 From: yetone Date: Thu, 12 Nov 2026 14:09:09 +0800 Subject: [PATCH] gui: a state shares the option lists that differ only in their own agent's fastFor MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 9ed6a312b sent each fields' option list once, but the real account's /api/state only went from 689,949 to 627,061 bytes: each agent's catalog (231 options, ~51KB) marks ~34 of them fastFor its own id ("crush", "droid", …), so no two agents' lists were alike. shareOptions now sends a fastFor naming the field's own agent as "*" before it compares, and api() in app.js gives each agent its own copy of those options with its id back, so a fast pick for one agent still marks that agent's options only (setFast matches on fastFor). setFast's three loops are one mark() over every list, the picked option included, which pays for the expansion within the core script budget (1,263,333, not raised). Run, at this tree on mote: - go vet ./internal/gui; go test ./internal/gui: ok (TestCoreScriptBudget ok). - state-option-lists.test.cjs: 16/16 (Chromium and WebKit, en zh zh-TW ja de at 1000px, en zh at 420px), with a new check that codex's fast leaves crush's alone on a shared list. - Broken by hand: without fastForSelf, TestStateSendsEachOptionListOnce is red (5 lists, want 4); without the "*" map in app.js the GUI test is red ("the kept state's lists put back"). - Real account (sandbox HOME with its data), magpie-w18: GET /api/state 346,210 bytes (689,949 before 9ed6a312b, 627,061 after it), 30,390 gzipped; ?last=1 346,222. - fast-ultra.test.cjs and fast-pick are red with this app.js and with the one before 9ed6a312b alike: #6201 (an extra POST /api/update/using). With that one POST left out of their record (not committed), both pass 36/36 in Chromium and WebKit with this setFast. Not tried: the whole GUI suite (mote over its cores); a real window with this state. Lists of 231-255 options that differ in their first entries are still sent whole (17 lists, 327KB); an option table is next. --- internal/gui/assets/app.js | 13 ++++--- internal/gui/statewrite.go | 25 ++++++++++++- internal/gui/statewrite_test.go | 35 +++++++++++++++++-- .../gui/tests/state-option-lists.test.cjs | 19 +++++++--- 4 files changed, 77 insertions(+), 15 deletions(-) diff --git a/internal/gui/assets/app.js b/internal/gui/assets/app.js index dd38eb3eb..a7ed7c12b 100644 --- a/internal/gui/assets/app.js +++ b/internal/gui/assets/app.js @@ -150,8 +150,9 @@ async function api(path, body, heads, signal) { try { data = text ? JSON.parse(text) : null; } catch { throw new Error(notJSON(res, text)); } - // a state sends each fields' option list once (shareOptions, statewrite.go) - if (data?.optionLists) for (const a of data.agents) for (const f of a.fields) if (f.list) f.options = data.optionLists[f.list - 1]; + // a state sends each fields' option list once, "*" for a fastFor of + // the agent's own (shareOptions, statewrite.go) + if (data?.optionLists) for (const a of data.agents) for (const f of a.fields) if (f.list) f.options = data.optionLists[f.list - 1].map((o) => o.fastFor == "*" ? { ...o, fastFor: a.id } : o); if (!res.ok) { const err = new Error(data?.error || `${res.status} ${res.statusText}`); if (data?.code === "runtime_unavailable") { @@ -5443,11 +5444,9 @@ function boltIcon(ultra, size) { // setFast marks the model fast or not in every list it is in for the agent: // the open picker's, and the agent's fields (its tiers pick the same model) function setFast(o, fast, ultra = false) { - const same = (x) => x.ref === o.ref && x.fastFor === o.fastFor; - for (const a of state.agents) for (const f of a.fields) for (const x of f.options || []) if (same(x)) { x.fast = fast; x.ultra = ultra; } - for (const x of [...(pick?.options || []), ...(pick?.items || [])]) if (same(x)) { x.fast = fast; x.ultra = ultra; } - o.fast = fast; - o.ultra = ultra; + const mark = (x) => { if (x.ref === o.ref && x.fastFor === o.fastFor) { x.fast = fast; x.ultra = ultra; } }; + for (const a of state.agents) for (const f of a.fields) (f.options || []).forEach(mark); + [...(pick?.options || []), ...(pick?.items || []), o].forEach(mark); // the field's button says it of the model it has const anchor = pick?.anchor; if (anchor?.classList.contains("field") && optionFor(pick.field, pick.field.value)?.ref === o.ref) { diff --git a/internal/gui/statewrite.go b/internal/gui/statewrite.go index 81ee430f9..2bd58c623 100644 --- a/internal/gui/statewrite.go +++ b/internal/gui/statewrite.go @@ -142,7 +142,8 @@ func (l lineless) Write(b []byte) (int, error) { // shareOptions is s with each fields' option list sent once, in // OptionLists, and named by the field's List: a list two fields offer -// alike is one entry. s's own agents are left as they were. The page +// alike is one entry, a FastFor naming the field's own agent sent as +// fastSelf. s's own agents are left as they were. The page // puts each field's list back as it reads the state (api in app.js). func shareOptions(s stateJSON) stateJSON { if s.OptionLists != nil { @@ -158,6 +159,7 @@ func shareOptions(s stateJSON) stateJSON { if len(opts) < 2 { continue } + opts = fastForSelf(opts, agents[i].ID) k := slices.IndexFunc(lists, func(l []agent.Option) bool { return sameOptions(l, opts) }) if k < 0 { lists = append(lists, opts) @@ -177,6 +179,27 @@ func shareOptions(s stateJSON) stateJSON { return s } +// fastSelf is the FastFor an option list is sent with for the agent +// whose list it is: each agent's catalog differed only there, so none +// were alike. The page puts the agent's id back (api in app.js). +const fastSelf = "*" + +// fastForSelf is opts with each FastFor that names id as fastSelf; opts +// itself when none does, a copy otherwise. +func fastForSelf(opts []agent.Option, id string) []agent.Option { + copied := false + for k := range opts { + if opts[k].FastFor != id || id == "" { + continue + } + if !copied { + opts, copied = slices.Clone(opts), true + } + opts[k].FastFor = fastSelf + } + return opts +} + // sameOptions reports whether a and b are the same options, in the same // order. func sameOptions(a, b []agent.Option) bool { diff --git a/internal/gui/statewrite_test.go b/internal/gui/statewrite_test.go index 7a329304c..f75925d43 100644 --- a/internal/gui/statewrite_test.go +++ b/internal/gui/statewrite_test.go @@ -133,7 +133,12 @@ func TestStateSendsEachOptionListOnce(t *testing.T) { {ID: "c", Fields: []fieldJSON{{Key: "model", Options: other}}}, {ID: "d", Fields: []fieldJSON{{Key: "model", Options: icons}}}, {ID: "e", Fields: []fieldJSON{{Key: "model", Options: []agent.Option{}}}}, + // each agent's catalog names it as the one its fast models are + // for: alike all the same + {ID: "f", Fields: []fieldJSON{{Key: "model", Options: fastFor("f")}}}, + {ID: "g", Fields: []fieldJSON{{Key: "model", Options: fastFor("g")}}}, }} + fCatalog := s.Agents[5].Fields[0].Options var before bytes.Buffer if err := json.NewEncoder(&before).Encode(s); err != nil { t.Fatal(err) @@ -154,8 +159,8 @@ func TestStateSendsEachOptionListOnce(t *testing.T) { if err := json.Unmarshal(w.body.Bytes(), &sent); err != nil { t.Fatal(err) } - if len(sent.OptionLists) != 3 { - t.Fatalf("%d option lists sent, want 3 (the catalog, the fast one's, the other icons'): %s", len(sent.OptionLists), w.body.Bytes()) + if len(sent.OptionLists) != 4 { + t.Fatalf("%d option lists sent, want 4 (the catalog, codex's fast one's, the other icons', f's and g's): %s", len(sent.OptionLists), w.body.Bytes()) } lists := map[string]int{} for _, a := range sent.Agents { @@ -174,6 +179,12 @@ func TestStateSendsEachOptionListOnce(t *testing.T) { if lists["c/model"] == lists["a/model"] || lists["d/model"] == lists["a/model"] { t.Fatalf("a list unlike the catalog was sent as it: %v", lists) } + if lists["f/model"] == 0 || lists["f/model"] != lists["g/model"] || lists["f/model"] == lists["a/model"] { + t.Fatalf("catalogs differing only by their own agent's fastFor are not one list: %v", lists) + } + if fCatalog[1].FastFor != "f" { + t.Fatal("sharing the list changed the agent's own options") + } if lists["a/effort"] != 0 || lists["e/model"] != 0 { t.Fatalf("a list of one or none was moved: %v", lists) } @@ -187,7 +198,20 @@ func TestStateSendsEachOptionListOnce(t *testing.T) { for _, f := range a.(map[string]any)["fields"].([]any) { f := f.(map[string]any) if l, ok := f["list"].(float64); ok { - f["options"] = ol[int(l)-1] + var opts []any + for _, o := range ol[int(l)-1].([]any) { + o := o.(map[string]any) + if o["fastFor"] == "*" { + c := map[string]any{} + for k, v := range o { + c[k] = v + } + c["fastFor"] = a.(map[string]any)["id"] + o = c + } + opts = append(opts, o) + } + f["options"] = opts delete(f, "list") } } @@ -209,3 +233,8 @@ func TestStateSendsEachOptionListOnce(t *testing.T) { t.Fatal("writing the state changed the caller's agents") } } + +// fastFor is a catalog whose fast model is sent fast for id. +func fastFor(id string) []agent.Option { + return []agent.Option{{Value: "x/1"}, {Value: "x/2", FastFor: id}, {Value: "x/3", FastFor: "codex"}} +} diff --git a/internal/gui/tests/state-option-lists.test.cjs b/internal/gui/tests/state-option-lists.test.cjs index ec3a8d809..225cca44a 100644 --- a/internal/gui/tests/state-option-lists.test.cjs +++ b/internal/gui/tests/state-option-lists.test.cjs @@ -5,7 +5,8 @@ // from 1, and api() puts the list back before anything reads the state. // Two agents' model pickers sharing one list each show it whole, a field // with its own options keeps them, and the state kept from the last read -// (?last=1), shared the same way, draws too. Chromium and WebKit, wide +// (?last=1), shared the same way, draws too. A fastFor sent as "*" is +// the agent's own id again in each agent's list. Chromium and WebKit, wide // and narrow, in every language. const assert = require("node:assert/strict"); const fs = require("node:fs/promises"); @@ -18,6 +19,8 @@ const catalog = [ { value: "magpie/codex/gpt-5.5", ref: "codex/gpt-5.5", label: "GPT-5.5", note: "", group: "ChatGPT" }, { value: "magpie/zai/glm-5", ref: "zai/glm-5", label: "GLM-5", note: "", group: "Z.ai" }, { value: "magpie/kimi/k2", ref: "kimi/k2", label: "Kimi K2", note: "", group: "Kimi" }, + // sent fast for the agent whose list it is: "*" on the wire + { value: "magpie/codex/gpt-5.5-fast", ref: "codex/gpt-5.5-fast", label: "GPT-5.5 Fast", note: "", group: "ChatGPT", fastFor: "*" }, ]; const effort = [{ value: "low", note: "" }, { value: "high", note: "" }]; @@ -81,10 +84,11 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", await page.goto("http://magpie.test/?view=agents"); // drawn from the kept state, before the fresh one comes await page.locator('.row.agent[data-id="crush"]').waitFor(); - const read = () => page.evaluate(() => state.agents.map((a) => [a.id, a.fields.map((f) => [f.key, (f.options || []).map((o) => o.value)])])); + const read = () => page.evaluate(() => state.agents.map((a) => [a.id, a.fields.map((f) => [f.key, (f.options || []).map((o) => o.value + (o.fastFor ? " " + o.fastFor : ""))])])); + const own = (id) => catalog.map((o) => o.value + (o.fastFor ? " " + id : "")); const want = [ - ["codex", [["model", catalog.map((o) => o.value)], ["effort", ["low", "high"]]]], - ["crush", [["model", catalog.map((o) => o.value)]]], + ["codex", [["model", own("codex")], ["effort", ["low", "high"]]]], + ["crush", [["model", own("crush")]]], ]; assert.deepEqual(await read(), want, "the kept state's lists put back"); release(); @@ -99,6 +103,13 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", await page.keyboard.press("Escape"); await page.locator("#pop").waitFor({ state: "hidden" }); } + // fast for one agent is fast for that agent only, though the list is shared + const fast = await page.evaluate(() => { + const opt = (id) => state.agents.find((a) => a.id === id).fields[0].options.find((o) => o.fastFor); + setFast(opt("codex"), true); + return [opt("codex").fast, !!opt("crush").fast]; + }); + assert.deepEqual(fast, [true, false], "codex's fast leaves crush's alone"); assert.deepEqual(errors, []); await page.context().close(); });