Files
magpie/internal/gui/laststate.go
T
yetone c97b4247a7 gui: the Agents page draws the agents magpie read last at once after a start or a light-mode reopen, and the fresh read replaces them (#1519, fatkun)
After a start, or after light mode let the window go, the Agents page
waited for state(): every agent's config read again, and with WSL agents
on each distro's through \\wsl.localhost. Measured on the Windows test box
(realistic config, one running distro with 9 agents):

  /api/state, WSL agents on:  456-620 ms per read (WSL rows 20-160 ms each)
  /api/state, WSL agents off: 117-253 ms
  /api/providers:             83-125 ms, already loaded on its own
                              (show() at start), not behind the state

Now GET /api/state keeps what it built (memory, and
<cache>/magpie/agents-state.json for the next start, written only when it
changed). GET /api/state?last=1 answers with it at once, marked last, with
the settings as they are now (the kept copy leaves the settings out: they
hold the GitHub token and the LAN key) and only for the version that kept
it. The page's first load asks for both, draws the last-known rows if they
come first, and the fresh read replaces them: an agent leaves the list
only once that read says it is gone. A change made on the early rows
answers with a newer state, which stands over the fresh read asked before
it. With nothing kept the page waits as before.

Windows box, new binary, cold start: /api/state?last=1 7-25 ms (436 KB,
all 20 agents incl. the WSL ones), fresh /api/state 456-550 ms beside it.

Run: TestLastKnownState (fails on the old handler: ?last=1 gave the full
fresh state); agents-last-known.test.cjs in Chromium and WebKit (the
at-once case fails on the old app.js); the 21 GUI tests that count or
stub /api/state; go test ./internal/gui; gofmt; vet darwin, linux,
windows; real page in Chromium against the built binary (second start
drawn from agents-state.json).

Not changed: the ~1.15 s of start-up moves before the window on Windows,
and state()'s own cost.
2026-10-11 01:49:47 +08:00

85 lines
2.6 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package gui
import (
"bytes"
"encoding/json"
"os"
"path/filepath"
"sync"
"github.com/yetone/magpie/internal/appdir"
"github.com/yetone/magpie/internal/edit"
"github.com/yetone/magpie/internal/settings"
)
// The last-known state (#1519, fatkun). state() reads every agent's config
// anew, and with WSL agents on, each distro's through \\wsl.localhost, so
// the Agents page waited 0.4–0.6 s for it on the Windows test box after a
// start (fatkun: about 2 s, and seconds more with WSL on), and the
// Providers page waited behind it. The page now draws from the state
// built last, kept here and in the cache folder for the next start, and
// the fresh one replaces it the moment it is in: an agent leaves the list
// only once a fresh read says it is gone.
//
// What is kept leaves the settings out (they hold the GitHub token and the
// LAN key): the last-known state is served with the settings as they are
// now, so it never brings back one the user changed since.
var lastState struct {
sync.Mutex
kept []byte // the last state built, as stateFile holds it
read bool // the file was looked at (or kept was set)
}
// stateFile is the last-known state, with the version that wrote it: an
// older magpie's is not drawn by a newer page.
type stateFile struct {
Version string `json:"version"`
State stateJSON `json:"state"`
}
func lastStatePath() string { return filepath.Join(appdir.Cache(), "agents-state.json") }
// keepState remembers s as the last-known state. The file is written only
// when what is kept changed.
func keepState(s stateJSON) {
s.Settings = settings.Settings{}
// and what is told once: a notice, a file recovered after a crash
s.Notice, s.Connected, s.Recovered = "", nil, nil
b, err := json.Marshal(stateFile{Version: Version, State: s})
if err != nil {
return
}
lastState.Lock()
defer lastState.Unlock()
if !lastState.read {
lastState.kept, _ = os.ReadFile(lastStatePath())
lastState.read = true
}
if bytes.Equal(b, lastState.kept) {
return
}
lastState.kept = b
_ = edit.WriteAtomic(lastStatePath(), b)
}
// lastKnown is the last state built, by this run or the one before it,
// with the settings as they are now; false when none is kept, or it was
// kept by another version.
func lastKnown() (stateJSON, bool) {
lastState.Lock()
if !lastState.read {
lastState.kept, _ = os.ReadFile(lastStatePath())
lastState.read = true
}
b := lastState.kept
lastState.Unlock()
var f stateFile
if len(b) == 0 || json.Unmarshal(b, &f) != nil || f.Version != Version || f.State.Agents == nil {
return stateJSON{}, false
}
f.State.Settings = settings.Load()
f.State.Last = true
return f.State, true
}