Files
magpie/internal/library/wsl_asleep.go
T
yetone c1da9ccda3 library: a stopped WSL distro's agents are listed, and a tick for one is written there once it runs (cybercaius on Discord)
cybercaius: with WSL not started, the Library's MCP page didn't list the
agents in the distro at all, and nothing ticked for them could be kept.
A stopped distro's agents are no targets (#3896: opening their files
starts the distro), so they fell off the page.

While a distro runs, each agent's places and what its MCP file takes are
kept in wsl.json (keepShape, agent.WSLKeep, key library:<kind>). While it
is stopped, Read lists its agents from that (asleepView, asleep: <distro>)
with the library's ticks; one magpie never saw is listed only on rows that
name it, as unseen, its chip disabled and kept by every click (keptOf). A
tick on a stopped agent is kept in library.json (Takes accepts a known
shape, serversAgents judges it by shape.reaches); nothing is opened or run
in the distro. Woken, from KeepLinked's 30s loop, syncs once a distro the
library names goes from stopped to running (wsl.exe -l --running starts
nothing). The page greys a stopped agent's chip and says why in its
tooltip and one line per distro; the MCP hub leaves them out. The CLI says
the same on stderr (AsleepIn). en, zh, zh-TW, ja, de.

Run at this tree (20695ad8b before this message; on adc618bbf):
- mote: gofmt -l clean; go vet -tags nogui for darwin, GOOS=windows and
  GOOS=linux on internal/library, internal/agent, internal/cli; go test
  -tags nogui -timeout 45m ./internal/library ./internal/agent
  ./internal/cli: ok (internal/gui ok too at the 96a703a59 base).
- WSL magpie-desktop: library-wsl-asleep, i18n-keys, library-gone-agents,
  library-items and library-mcp-hub, Chromium and WebKit: 88/88. At the
  96a703a59 base every library-*, mcp-*, skill-* file and i18n-keys:
  1148 of 1164; the reds were library-chip-fan (WebKit),
  library-mcp-tools (chromium ja 420), library-items and
  skill-group-shared (WebKit) beside another session's full GUI suite.
  Alone, library-items and skill-group-shared pass, and chip-fan and
  mcp-tools are red the same on an origin/main 96a703a59 copy (#4120,
  commented).

Broken by hand: asleepView returning nothing made TestWSLStoppedListedAndQueued
("a stopped distro's Codex isn't listed") and TestWSLStoppedUnseen red;
Woken never syncing made TestWSLStoppedListedAndQueued red ("no sync once
the distro ran"); keptOf without unseen made library-wsl-asleep red in
all 10 Chromium cases (Pi's tick dropped by a click on Claude Code).

Rebased onto fa2332976 (gateway, provider and Codex effort commits, nothing
in internal/library, internal/agent/wsl or the Library page) before the
push: go vet of the three packages and go test -run TestWSL
./internal/library ok there; the full packages ran at adc618bbf.

Not tried: a real distro stopped and started. magpie-desktop was running
another session's full GUI suite, which stopping it would have killed, and
the other distros are other people's. The tests use FakeWSL, which counts
every wsl.exe -d command run in the stopped distro (none). Not changed:
the Instructions, Hooks and Subagents pages still leave a stopped
distro's agents out (#4182, #4237, #4238).
2026-10-29 18:34:44 +08:00

226 lines
6.8 KiB
Go

package library
import (
"encoding/json"
"log"
"maps"
"slices"
"strings"
"sync"
"github.com/yetone/magpie/internal/agent"
)
// A stopped WSL distro's agents are no targets (wslTargetOf): their files
// can't be opened without starting the distro (#3896). The page lists them
// all the same (cybercaius on Discord: the MCP page knew a distro's agents
// only once WSL had started), from what magpie learnt of them while the
// distro ran — the places it gives each one MCP servers and skills, kept
// in wsl.json beside the probe's memory (shape) — with the ticks the
// library has for them. A tick there is kept as any is, in library.json,
// and written into the distro the next time it runs (Woken): nothing is
// opened while it is stopped, and nothing starts it but the user.
// shape is what the library last knew of where a WSL agent keeps its MCP
// servers and skills, and what its MCP file can take: an AgentView's, so
// the page can show the agent's ticks while its distro is stopped.
type shape struct {
MCP string `json:"mcp,omitempty"`
Skills string `json:"skills,omitempty"`
SkillsAlso []string `json:"skillsAlso,omitempty"`
MCPVia string `json:"mcpVia,omitempty"`
NoSSE bool `json:"noSSE,omitempty"`
NoRemote bool `json:"noRemote,omitempty"`
NoEnvRefs bool `json:"noEnvRefs,omitempty"`
ToolsOff bool `json:"toolsOff,omitempty"`
Startup bool `json:"startup,omitempty"`
}
// shapeKey is the key a WSL agent's shape is kept under in its distro's
// memory: one for each kind of agent (codex, omp#work).
func shapeKey(id string) string {
kind, _, _ := strings.Cut(id, "@")
return "library:" + kind
}
// keepShape remembers t's shape, t being an agent in a running distro.
func keepShape(t *Target) {
a := t.Agent
sh := shape{Skills: t.Skills, SkillsAlso: t.SkillsAlso, MCPVia: t.MCPVia}
if t.MCP != nil {
sh.MCP = t.MCP.Path
sh.NoSSE = t.MCP.supports(&Server{Transport: "sse"}) != nil
sh.NoRemote = t.MCP.supports(&Server{Transport: "http"}) != nil
sh.NoEnvRefs = t.MCP.refsOf() == envSyntax{}
sh.ToolsOff = toolsOffOf(t) != toolsNone
sh.Startup = t.MCP.Format == fmtCodex
}
b, err := json.Marshal(sh)
if err != nil {
return
}
if string(b) != agent.WSLKept(a.WSL, shapeKey(a.ID)) {
agent.WSLKeep(a.WSL, shapeKey(a.ID), string(b))
}
}
// shapeOf is the shape kept for a, an agent in a stopped distro; nil when
// magpie never saw it with an MCP file or a skills folder.
func shapeOf(a *agent.Agent) *shape {
var sh shape
if json.Unmarshal([]byte(agent.WSLKept(a.WSL, shapeKey(a.ID))), &sh) != nil || sh.MCP == "" && sh.Skills == "" {
return nil
}
return &sh
}
// reaches says whether the agent can be given s, as far as its shape
// tells (the page's reaches): what its file was found to read when the
// distro last ran. The rest (a header Codex reads only whole) is said by
// the sync that writes it, as for any agent.
func (sh *shape) reaches(s *Server) bool {
switch {
case sh.MCP == "":
return false
case s.Transport == "sse" && sh.NoSSE, s.Remote() && sh.NoRemote:
return false
case sh.NoEnvRefs && (s.Remote() && hasRef(s.Headers) || !s.Remote() && hasRef(s.Env)):
return false
case !s.Remote() && windowsPath.MatchString(s.Command) && !isSelf(s):
return false
}
return true
}
// isAsleep says whether a is an agent in a WSL distro that isn't running
// now: wslTargetOf's nil.
func isAsleep(a *agent.Agent) bool {
return a.WSL != "" && (a.Home == "" || !agent.WSLRunning(a.WSL))
}
// asleepView is a stopped distro's agent as the page lists it: by its
// shape, or, with none known and the library naming it, as one whose
// ticks can't be shown (Unseen); false for one the page leaves out.
func (l *Library) asleepView(a *agent.Agent) (AgentView, bool) {
av := AgentView{ID: a.ID, Name: a.Name, Icon: a.Icon, Asleep: a.WSL}
sh := shapeOf(a)
if sh == nil {
av.Unseen = true
return av, l.names(a.ID)
}
av.MCP, av.Skills, av.SkillsAlso, av.MCPVia = sh.MCP, sh.Skills, sh.SkillsAlso, sh.MCPVia
av.NoSSE, av.NoRemote, av.NoEnvRefs, av.ToolsOff, av.Startup = sh.NoSSE, sh.NoRemote, sh.NoEnvRefs, sh.ToolsOff, sh.Startup
if sh.Skills != "" {
// a WSL agent takes copies only (Target.Copy)
_, av.HowOwn = l.SkillHow[a.ID]
av.How, av.MustCopy = HowCopy, true
}
return av, true
}
// names says whether the library gives the agent id anything, or holds
// something it gave it.
func (l *Library) names(id string) bool {
if a := l.Applied[id]; a != nil && (len(a.Skills) > 0 || len(a.MCP) > 0 || len(a.Hooks) > 0) {
return true
}
return slices.ContainsFunc(l.Skills, func(s *Skill) bool { return slices.Contains(s.Agents, id) }) ||
slices.ContainsFunc(l.MCP, func(s *Server) bool { return slices.Contains(s.Agents, id) })
}
// distrosNamed are the WSL distros the library names an agent in.
func (l *Library) distrosNamed() []string {
in := map[string]bool{}
add := func(id string) {
if _, d, ok := strings.Cut(id, "@wsl:"); ok && d != "" {
in[d] = true
}
}
for id := range l.Applied {
if l.names(id) {
add(id)
}
}
for _, s := range l.Skills {
for _, id := range s.Agents {
add(id)
}
}
for _, s := range l.MCP {
for _, id := range s.Agents {
add(id)
}
}
return slices.Sorted(maps.Keys(in))
}
// wake is which distros were last seen stopped (false) or running (true),
// by Woken and by the reads that left a stopped one's agents out.
var wake struct {
sync.Mutex
up map[string]bool
}
func noteUp(distro string, up bool) {
wake.Lock()
defer wake.Unlock()
if wake.up == nil {
wake.up = map[string]bool{}
}
wake.up[distro] = up
}
// Woken writes the library into the agents of each distro it names that
// was seen stopped and runs now: what was ticked for them while it was
// stopped, kept in library.json, reaches them once the user starts it.
// Asking whether a distro runs (wsl.exe -l --running) doesn't start it.
// nil, nil when no such distro started.
func Woken() (*Result, error) {
mu.Lock()
l, err := load()
mu.Unlock()
if err != nil {
return nil, err
}
started := false
for _, d := range l.distrosNamed() {
up := agent.WSLRunning(d)
wake.Lock()
was, seen := wake.up[d]
wake.Unlock()
noteUp(d, up)
if up && seen && !was {
started = true
}
}
if !started {
return nil, nil
}
return Sync()
}
// keepWoken is Woken for KeepLinked's loop, which logs what it couldn't do.
func keepWoken() {
res, err := Woken()
if err != nil {
log.Println("library: a WSL distro that started:", err)
return
}
if res != nil {
for _, p := range res.Problems {
log.Println("library: a WSL distro that started:", p.Agent, p.What, p.Error)
}
}
}
// AsleepIn is the WSL distro, not running now, of the agent with id; ""
// for any other. A change the library makes for it is written there the
// next time it runs.
func AsleepIn(id string) string {
_, d, ok := strings.Cut(id, "@wsl:")
if !ok || d == "" || agent.WSLRunning(d) {
return ""
}
return d
}