Commit Graph
3 Commits
Author SHA1 Message Date
yetone fd70def825 gui: a Providers poll sums what it keeps while writing the answer
Each poll of /api/providers encoded the answer for the page and then
encoded the kept last-known list again only to sum it (keepProviders,
encodedSum): 32-34 MB of what forty polls allocated with real accounts.

writeProvidersJSON now returns the encodedSum of keptProviders(s), taken
from the same encoding, as writeStateJSON does for /api/state. A row
kept as it is (no headers, proxy, extra body or account proxies) is
encoded once into the answer and the sum; only a row with those is
encoded again without them. The head and tail of the kept file come
from marshaling it with no rows. A nil row list, or a head that doesn't
match, falls back to the old writeJSON and encodedSum. The other
keepProviders callers (after a save) are unchanged.

Run on mote at this tree:
- gofmt, go vet ./internal/gui, GOOS=windows go vet -tags nogui: ok.
- go test -run 'Providers|Kept' ./internal/gui 3 times alone: ok.
- New TestProvidersWriteSumsWhatIsKept: the sum equals
  encodedSum(keptProviders(s)) for bigProviders, rows with each secret
  field, gateway calls, moved models, and empty and nil lists. Broken
  by hand twice: keepsAllOf always true (secrets summed) and the kept
  tail left out of the sum. Red both times.
- go test ./internal/gui: only the known reds,
  TestSettingsSaveKeepsWhatItDoesNotSend (#4514) and
  TestMainWindowCorners (#3949).
- -race -run 'Providers|Kept': TestProvidersWriteGrowsNoBufferItsSize
  red, and red the same way at the base (c76bcaea7, twice): an
  allocation count under -race, where sync.Pool drops items. Fixed in
  its own commit next.

Real accounts, sandbox HOME, 40 polls of state, providers and drift,
v0.1.2509 (msteady) vs this (mpsum), two rounds each:
- alloc_space shown: 365.6/364.3 MB -> 341.5/329.8 MB; keepProviders
  32.9/32.1 MB -> gone; writeProvidersJSON 15.3/13.6 -> 9.9/14.9 MB.
- poll CPU 4.21/4.27 s -> 4.18/4.10 s; footprint peak 111/116 ->
  112/108 MB.
- /api/state and drift answers byte-identical; /api/providers differs
  only in the providers' "fetched" time.

Not run: the Linux and Windows apps; a save's keepProviders path is
unchanged and was not exercised.
2026-11-01 09:04:26 +08:00
yetone 6a23d9d755 gui: the kept state and providers are marshaled only when they changed
keepState and keepProviders ran json.Marshal on every /api/state and
/api/providers poll to compare the answer with the one kept on disk.
They now keep a sha256 of what was written and hash the new answer
into it with an Encoder; only a changed answer is marshaled and
written. json.Marshal there was 38.6 MB (keepState 25.9 MB,
keepProviders 12.7 MB) of what twenty window polls allocated with
real accounts (m2463 profile).

Test: TestKeptStateMarshaledOnlyWhenChanged. Twenty identical keeps
marshal nothing, nor does a keep after the file is read again; a
changed state is marshaled once and read back.

Run on mote, at this tree on dd7442f28:
- gofmt, go vet (darwin, windows, linux): ok
- the new test alone 3 times: ok
- broken by hand (the sum check made never true): red, "20 polls of
  the same state and list marshaled them 20 times, want 0"
- go test ./internal/gui: every test ok except the known
  TestSettingsSaveKeepsWhatItDoesNotSend (#4514) and
  TestMainWindowCorners (#3949)

Not run: an A/B with the real-account sandbox for this change alone;
it goes with the next batch's profile.
2026-11-01 04:46:38 +08:00
yetone 92fbabb47e gui: the Providers page draws the last list at once and the fresh one behind it
#1519 (fatkun): the Providers page asked /api/providers each time it
opened, and after a start that read takes seconds (every account's
sign-in, every agent's model), so the page showed skeletons that long.

Now, as the Agents page draws the last state:
- the app reads /api/providers once in the background after the start
  (prefetchProviders), and the page's first opening uses that read, or
  waits on it, instead of asking again. The sidebar's gateway line
  reuses the same read (sideGwRead), so a start still makes one read.
- the server keeps the list it last built in the cache folder
  (providers-state.json, 0600; lastproviders.go) without the gateway's
  recent calls and lanes or any provider's headers, proxy or extra body.
  /api/providers?last=1 serves it at once; the page draws it while the
  fresh read is out, and the fresh one replaces it.
- a row clicked on a kept list waits for the fresh one rather than
  opening an editor that would be drawn again under the reader's hands.

Tests:
- TestLastKnownProviders (internal/gui): red when the stripping of
  Headers/Proxy was broken by hand ("the kept list holds hdr-secret-1519
  / proxy-secret-1519"), green restored.
- providers-last-known.test.cjs (new): the kept list drawn before the
  fresh read answers, then replaced; a row click waits. With
  kept.last = false by hand the row-click case was red in all 5
  languages.
- sidebar.test.cjs: the narrow case now guards one /api/providers read
  in total, still one after widening to 1200px (the sidebar reuses it).
  Red on the old assets (0 reads), green on the new, both engines.

Run:
  go test -timeout 45m ./internal/gui
  ok  	github.com/yetone/magpie/internal/gui	528.667s
  gofmt -l internal/: nothing; go vet ./internal/gui, GOOS=linux and
  GOOS=windows go vet -tags nogui ./...: exit 0
  GUI, chromium and webkit, every language, wide and narrow:
  providers-last-known 20/20, sidebar 8/8, provider-proxy 4/4,
  agents-last-known 3/3, last-page 1/1 in each engine.
  provider-advanced-fold: 1-3 of 25 red per run, the same on the old
  assets (#2552).
  Chromium full GUI suite (before the kept.last and sidebar-test edits),
  under load ~200: 5681 tests, 55 red in 23 files; each was rerun alone
  and either passed or was as red on the old assets. routing-model-rest
  runs, old vs new: old 4, 2, 7 red, new 3, 3 red; routing-request-
  metrics two runs: old 1, 0 red, new 0, 2 red, each red at 23-29s
  (load ~240; the same test red on old).

Not verified: a real cold start on Windows; the webkit full suite (59
provider/sidebar/start files run: 516 tests, the reds there pass alone or
are #1863).
2026-10-20 20:23:56 +08:00