shangyuan0818 (X, third round, after v0.1.4091 and v0.1.4255): on Windows, Usage -> Sessions opened only after a fixed ~20s when sessions were long. The long sessions were not the cause. Goroutine dumps of the reporter's case on the Windows box showed four waits on WSL on the page's first asks: - agent.Looks -> Clients -> All -> wslDistros: a blocking wslRefresh, `wsl -l -q` (10s timeout) once its list is a minute old, then `wsl -l --running -q` (10s) every time. A WSL that answers slowly but answers is never marked hung, so each open after a minute idle waited for both: about 18s, every time. ClientsIn drops WSL's agents anyway, so Clients now reads only this machine's (local()). - gui agentsHere (the web-mode own-sessions gate) called agent.All the same way. It now calls agent.AllKnown: this machine's agents and WSL's as last listed (wslDistrosWithin(0)), never waiting on wsl.exe. - sessions.wslFilesAs waited wslFirstWait (3s) afresh on each read while the first listing was pending, so list, stats and overview each waited 3s in turn. The 3s is now counted once, from when the first listing started. - provider.copilotCLILogin waited copilotWSLWait (2s) on every re-read of a distro's Copilot sign-in, every copilotWSLAge (20s), though it had an answer. A re-read now answers the last read at once; only the first read waits. Timed on Windows 11 (xiaomi-gpu), sandbox HOME with a 90 MB Claude JSONL and a 32 MB Codex rollout, a fake wsl.exe whose -l -q answers "Ubuntu" after 8s and whose --running hangs; /api/sessions list+stats+overview together, opened 20s after start, then 70s apart:b0674b9ec(old): 0.39s, 18.11s, 18.12s this commit: 0.38s, 0.07s, 0.07s With wsl.exe hanging outright and Sessions opened right after start: old 10.3-10.5s, new 3s (the designed first wait, now waited once). With no WSL: 0.4s on both. Tests, each red on the old code by hand: - TestClientsAskNoWSL (agent): with Clients back on All(), red (waited past 5s on the hung wsl.exe); with AllKnown on wslDistros(), red. - TestWSLFirstWaitIsWaitedOnce (sessions): four reads with the first listing hung; with the wait counted per read, red at 2.007s. - TestCopilotWSLHangHoldsNoRequest (provider): a re-read on a hung WSL with copilotWSLWait 10s must answer within 2s; with the kept answer disabled, red at 2.002s. Run at this tree: - mote: go test -timeout 45m ./... at dac5095c3 (baseb0674b9ec): exit 0, 102 packages ok, no FAIL. Rebased onto2ec8e6ad0after it, which changes README files only. - mica: go vet ./..., GOOS=linux go vet -tags nogui ./..., GOOS=windows go vet ./... ok; gofmt -l clean. - WSL (magpie-desktop): internal/agent, sessions, provider ok. - Windows: internal/agent, sessions, provider, gui: 66 reds, 64 of them red at origin/main2ec8e6ad0too (#6068, #1827, #4115, #2808, #4446, #5957, #5343, #5344). The other two, TestHermesProfilesAreNotListedAsAgents and TestHermesProfilesFollowMagpieModel, call onMachine, not the code changed here, pass alone 3 of 3 on this tree, and follow other Hermes tests red at main in the package run (#1841: Windows tests share APPDATA). Not tried: a real WSL distro that is slow (the fake wsl.exe stands in for it), and the reporter's own machine.
162 lines
5.0 KiB
Go
162 lines
5.0 KiB
Go
package gui
|
||
|
||
import (
|
||
"encoding/json"
|
||
"fmt"
|
||
"net/http"
|
||
"os"
|
||
"slices"
|
||
"sync"
|
||
"sync/atomic"
|
||
"time"
|
||
|
||
"github.com/yetone/magpie/internal/agent"
|
||
"github.com/yetone/magpie/internal/sessions"
|
||
"github.com/yetone/magpie/internal/settings"
|
||
)
|
||
|
||
// Gateway mode (Player on Discord): `magpie web` on a server that is only
|
||
// the gateway for other machines' agents shows what such a gateway needs —
|
||
// Providers, Gateway, Routing, Usage, Sessions, Plugins, Settings and the
|
||
// Library's MCP servers with the router (treeman_26897) — and leaves out
|
||
// what is this machine's agents' (Agents, the Library's other tabs, the settings
|
||
// written into agents' files) and its desktop's (the tray, the Dock, open
|
||
// at login, notifications). It is the browser page's alone: the app's
|
||
// windows show everything whatever it says.
|
||
//
|
||
// Settings' choice (settings.GatewayMode, "on" or "off") wins; with none,
|
||
// `magpie web --gateway` turns it on, and so does a magpie web with no
|
||
// agent on its machine (a container, a NAS). Settings › General turns it
|
||
// off again, which brings the pages back.
|
||
|
||
// WebGateway is `magpie web --gateway`'s.
|
||
var WebGateway atomic.Bool
|
||
|
||
// webPage is set once the page is served by `magpie web`: settingsState,
|
||
// which has no Windows, tells the page its gateway mode by it.
|
||
var webPage atomic.Bool
|
||
|
||
// gatewayMode says whether the page is in gateway mode, and why: "on" or
|
||
// "off" (Settings' choice), "flag" (magpie web --gateway), "no-agents", or
|
||
// "" (the app's windows, or magpie web on a machine with agents).
|
||
func gatewayMode(web bool) (bool, string) {
|
||
if !web {
|
||
return false, ""
|
||
}
|
||
switch m := settings.Shared().GatewayMode; m {
|
||
case "on", "off":
|
||
return m == "on", m
|
||
}
|
||
if WebGateway.Load() {
|
||
return true, "flag"
|
||
}
|
||
if !agentsHere() {
|
||
return true, "no-agents"
|
||
}
|
||
return false, ""
|
||
}
|
||
|
||
// agentsHere says whether an agent is found on this computer, stopping at
|
||
// the first: each agent's check can read app bundles and ask Spotlight.
|
||
// WSL's agents are those last listed: a hung wsl.exe doesn't hold the
|
||
// Sessions page (sessions.Dirs asks this). A test's own.
|
||
var agentsHere = func() bool { return slices.ContainsFunc(agent.AllKnown(), (*agent.Agent).Detected) }
|
||
|
||
// gatewayModeRoutes: POST /api/settings/gateway-mode {"mode": "on" | "off"
|
||
// | ""} sets Settings' choice, "" leaving it to magpie again.
|
||
func gatewayModeRoutes(mux *http.ServeMux) {
|
||
mux.HandleFunc("POST /api/settings/gateway-mode", func(rw http.ResponseWriter, r *http.Request) {
|
||
var in struct {
|
||
Mode string `json:"mode"`
|
||
}
|
||
if err := json.NewDecoder(r.Body).Decode(&in); err != nil {
|
||
fail(rw, err)
|
||
return
|
||
}
|
||
if in.Mode != "" && in.Mode != "on" && in.Mode != "off" {
|
||
fail(rw, fmt.Errorf("gateway mode is on, off or \"\" (automatic), not %q", in.Mode))
|
||
return
|
||
}
|
||
s := settings.Load()
|
||
s.GatewayMode = in.Mode
|
||
if err := settings.Save(s); err != nil {
|
||
fail(rw, err)
|
||
return
|
||
}
|
||
// the Sessions page lists this computer's agents' sessions, or
|
||
// stops, at once (ownSessionsOff)
|
||
ownSessionsOff()
|
||
forgetOwnSessions()
|
||
writeJSON(rw, settingsState())
|
||
})
|
||
}
|
||
|
||
// ownSessionsOff is gatewayMode as the sessions' gate asks it
|
||
// (sessions.OwnOff; paultian on Discord): in gateway mode the Sessions page
|
||
// and Usage › Sessions list what came through this gateway, never this
|
||
// computer's own agents' session files, which a machine put in gateway
|
||
// mode with agents installed still listed. It is asked on every read of a
|
||
// session, so it reads the shared settings and an agents look kept a while
|
||
// (agentsHereKept) rather than gatewayMode's own. When its answer turns,
|
||
// what was read under the other answer is dropped (ownTurned).
|
||
func ownSessionsOff() bool {
|
||
on := false
|
||
if webPage.Load() {
|
||
switch settings.Shared().GatewayMode {
|
||
case "on":
|
||
on = true
|
||
case "off":
|
||
default:
|
||
on = WebGateway.Load() || !agentsHereKept()
|
||
}
|
||
}
|
||
ownTurned(on)
|
||
return on
|
||
}
|
||
|
||
// ownWas is ownSessionsOff's last answer: 0 none yet, 1 off, 2 on.
|
||
var ownWas atomic.Int32
|
||
|
||
// ownTurned drops the sessions read, and the stats made, under the other
|
||
// answer, off the caller's goroutine: the gate is asked under the
|
||
// sessions' own lock, which Reset takes.
|
||
func ownTurned(on bool) {
|
||
v := int32(1)
|
||
if on {
|
||
v = 2
|
||
}
|
||
if was := ownWas.Swap(v); was != 0 && was != v {
|
||
go forgetOwnSessions()
|
||
}
|
||
}
|
||
|
||
func forgetOwnSessions() {
|
||
sessions.Reset()
|
||
forgetStats()
|
||
}
|
||
|
||
// agentsHereKept is agentsHere looked at most every half minute: it looks
|
||
// for every agent's files and commands. Another HOME or PATH (a test's
|
||
// sandbox) is looked at afresh.
|
||
var agentsSeen struct {
|
||
sync.Mutex
|
||
at time.Time
|
||
on string // the HOME and PATH it was looked for in
|
||
here bool
|
||
}
|
||
|
||
func agentsHereKept() bool {
|
||
agentsSeen.Lock()
|
||
defer agentsSeen.Unlock()
|
||
on := os.Getenv("HOME") + "\x00" + os.Getenv("PATH")
|
||
if agentsSeen.at.IsZero() || agentsSeen.on != on || time.Since(agentsSeen.at) > 30*time.Second {
|
||
agentsSeen.here, agentsSeen.at, agentsSeen.on = agentsHere(), time.Now(), on
|
||
}
|
||
return agentsSeen.here
|
||
}
|
||
|
||
func init() {
|
||
f := ownSessionsOff
|
||
sessions.OwnOff.Store(&f)
|
||
}
|