Files
magpie/internal/gui/gatewaymode.go
T
yetone 17345f684f agent: Spotlight is asked once for every desktop app, and a stale answer never waits
After the Sessions caches dropped (five idle minutes), the next
/api/sessions/stats on `magpie web` took 2.3–4.1s on the real account
on mote while its handler ran ~220ms of CPU. Two goroutine dumps taken
1.2s and 1.8s into the ask both sat in ownSessionsOff → agentsHere →
agent.Detected → HasApp → desktopApp.version → spotlightBundles: one
mdfind per bundle id (13 of them, ~50ms each idle, far more under
load), serial under one lock, asked again whenever the 5-minute answer
expired. Every other agent.Detected caller (Providers, quit ask, gateway
pause, Sessions) paid the same.

- spotlightBundles asks one `mdfind -attr kMDItemCFBundleIdentifier`
  for every id desktopApps names, and keeps the answer by id. Past
  spotlightFresh a caller gets the kept answer at once while one
  goroutine asks again; only the first ask after start waits, once for
  every caller. A failed mdfind keeps the answer before it.
- agentsHere stops at the first detected agent instead of checking all.

Measured with the real account's data on mote, the Sessions stats ask
at 370s, after the drops (`magpie web`, sandbox HOME):
  main (w27)  2.3–4.1s in three runs earlier today; 1.34s in this run,
              while mote's load fell from ~180 to ~10
  this (w28)  0.77s at load ~170, then 0.046s
The rest of the 0.77s is the stats themselves being read again.
Tests: TestSpotlightParse; TestSpotlightOneAskStaleNeverWaits (10 reads
are one ask; a stale read returns while the refresh is held; two stale
reads are one refresh). On mica: go vet ./internal/agent ./internal/gui,
go test -count=1 -timeout 45m ./internal/agent ./internal/gui ok
(280s, 186s).
Not tried: Spotlight off on the Mac (mdfind fails; the answer is none,
as before); Intel Mac.
2026-11-12 19:36:52 +08:00

161 lines
4.9 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 (
"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.Load().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.
// A test's own.
var agentsHere = func() bool { return slices.ContainsFunc(agent.All(), (*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)
}