After 647071f2b the real account's /api/state was still 346,210 bytes:
17 lists, 327KB, most of them each agent's 231-255-option catalog with a
few models of its own first, so no two were alike. The state now sends
each option once, in "options", and each list as the indexes of its
options ("optionLists"); api() in app.js looks each one up as it puts a
field's list back, a fastFor of "*" still given the agent's id.
shareOptions tells alike options apart by sorting pointers to them by
Value (slices.SortStableFunc on int32 indexes), not in a map, and each
list is a run of one []int32: a map sized for the options made
TestStateWriteGrowsNoBufferItsSize allocate 977KB for its 1.6MB state
(12,000 options unlike each other), the bar is half the state; now it
passes. writeStateJSON writes the indexes through one 512-byte buffer
and each option alone, as each agent is.
Run, at this tree on mote:
- go vet ./internal/gui; go test ./internal/gui: ok (TestCoreScriptBudget,
TestStateWriteGrowsNoBufferItsSize, TestStateSendsEachOptionListOnce,
which now also counts the 7 options its 4 lists name once each).
- state-option-lists.test.cjs: 16/16, Chromium and WebKit, 5 languages,
1000px and 420px; crush's list now starts with one model of its own,
an index past the shared catalog's. Broken by hand: without the
options lookup in app.js, red (8 of 8 Chromium).
- Real account (sandbox HOME with its data), magpie-w19: GET /api/state
159,767 bytes (689,949 before 57443ac5f, 346,210 at v0.1.3848),
14,765 gzipped, 7-9ms a GET; ?last=1 159,779. Its agents, lists put
back, are equal to w18's.
Not tried: the whole GUI suite; a real window drawing this state.
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.
With real accounts /api/state was 689,949 bytes, and agents[].fields
carried 707,970 of them before escaping: the model field's options, the
whole ~51KB catalog, went once per agent, 13 of 16 agents alike (crush
and dsh twice). Each window shown, each click that answers with a state
and every start's kept state (agents-state.json) encoded, sent, parsed
and held it again per agent.
writeStateJSON now sends each list once, in the state's last field
optionLists, and a field names its list by "list" (from 1) in place of
"options" (shareOptions). The lists are encoded one by one, as the
agents are, so no buffer grows to their size. The kept file is written
the same way, so its sum still matches. api() in app.js puts each
field's list back before anything reads the state, so every reader sees
the state as it was; the agents now share one array, which setFast
already treated as one model in every agent's lists. POST answers that
write the state with writeJSON are unchanged.
Run:
- go vet ./internal/gui; go test ./internal/gui (mote) ok.
- TestStateSendsEachOptionListOnce: red with shareOptions returning s
unchanged (0 lists sent), green with it.
- state-option-lists.test.cjs, Chromium and WebKit, en zh zh-TW ja de
at 1000px and en zh at 420px: 16 of 16. Red with api()'s line taken
out.
Each /api/state poll json.Marshal'd the state without its agents twice
(to send it and for keepState's sum) into fresh slices, and made a new
32KB writer; ownCodex cloned Codex's whole model list for a caller that
only reads it. One set of buffers and the writer is now kept between
polls (an atomic slot, not a sync.Pool, which a GC between polls
empties; a poll that finds it taken makes its own, and buffers over
4MB are dropped), and ownCodex reads catalog.CodexShared.
Run on mote at this tree: go vet ./internal/gui ./internal/agent;
go test -run "State|Settings|Allow" ./internal/gui, the same -race for
StateWritten, -run "Codex|OwnCodex|Pick" ./internal/agent: ok.
TestStateWrittenAsEncoded now writes big after small and small after
big; with encodeTo's Reset taken out by hand it is red (a stale tail).
Real accounts, 40 polls of /api/state, alloc_space: 27.8 and 26.1 MB
before, 18.4 and 22.7 MB after.
Not tried: Windows and Linux runs (no OS-specific code).
Each poll of the Agents page encoded the whole state (1.3 MB with real
accounts) into json's buffer, and the GC between polls empties the pool
that buffer comes from, so it grew to the state's size again each poll:
48 MB of what twenty rounds allocated (v0.1.2477 build, real-account
sandbox), and 68 MB at v0.1.2463.
writeStateJSON encodes the state without its agents twice (as sent, and
as keepState keeps it, without the settings), and each agent once,
straight to the response and to keepState's sha256, through one encoder.
keepState is split so the handler hands it that sum (keepStateSummed).
When the splice can't be made (no agents, or a field put before
"agents"), it writes and sums the state whole, as before.
Measured on this Mac with the real-account sandbox (abr.sh, 20 rounds of
drift + state + providers):
- v0.1.2477: writeJSON under /api/state 48 MB, 511 MB in all.
- this change: writeStateJSON 15 MB, 477 MB in all. The /api/state
bytes are identical to v0.1.2477's (cmp).
Run on mote (mote-run, Go 1.26.8):
- gofmt clean; go vet ok, also GOOS=windows.
- TestStateWrittenAsEncoded (same bytes as Encode, same sum as keepState,
for a large state, an empty agent list and none) and
TestStateWriteGrowsNoBufferItsSize, with TestKeptState* and TestLast*:
ok 3 of 3.
- Broken by hand (the whole-state path always taken): red, "writing a
1614586-byte state allocated 4207139 bytes, want under half its size".
- Not run before this push: the full internal/gui package; it runs next.