Files
magpie/internal/sessions/wsl.go
T
yetone ac4a4cf849 sessions: opening Sessions waits on no WSL listing it already has (shangyuan0818 on X)
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 (base b0674b9ec): exit 0, 102 packages ok, no FAIL. Rebased onto
  2ec8e6ad0 after 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/main 2ec8e6ad0 too (#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.
2026-11-17 00:21:23 +08:00

512 lines
16 KiB
Go

package sessions
import (
"bytes"
"encoding/json"
"log"
"os"
"path/filepath"
"slices"
"sort"
"strings"
"sync"
"time"
"github.com/yetone/magpie/internal/lastgood"
)
// Sessions in WSL. On Windows an agent installed in a WSL distro keeps its
// sessions in the distro's home, which magpie opens through
// \\wsl.localhost\<distro> — the distros internal/agent's wsl.go already
// finds and probes for the agents it sets up (WSLHomes, which that package
// sets; this one can't import it). Claude Code's, Codex's, Pi's and omp's
// session files there are read as this computer's are, each session marked
// with its distro (Session.WSL) and resumed through wsl.exe (wslResume).
// So are agy's, and the Antigravity app's when it is connected to the
// distro: its language server runs there and keeps the conversations in
// the distro's ~/.gemini/antigravity (@guyng35955 on X).
//
// Reading over \\wsl.localhost is slow, and looking at a stopped distro's
// files starts it. So the distros' files are listed behind, at most every
// wslRelist, and the listing is kept on disk (sessions-wsl.json beside the
// parses): a listing reads what was last found at once, a stopped
// distro's files stay listed as they were found and are never opened
// (file.cold), and their parses, kept by path as every file's are, stand.
// WSLHome is a WSL distro magpie found agents in: its name, its user's home
// as magpie opens it (\\wsl.localhost\Ubuntu\home\me), and whether it runs.
type WSLHome struct {
Distro string
Home string
Running bool
// Open maps a path under Home to where it can be read from Windows:
// a folder that is a link in the distro (~/.codex → /mnt/c/…) can't
// be followed through \\wsl.localhost (#4502), so what is below it is
// read where the link leads. nil reads every path as it is.
Open func(string) string
}
// WSLHomes are the WSL distros to read sessions in; nil (none) unless
// internal/agent sets it, which it does where there is WSL.
var WSLHomes func() []WSLHome
// WSLRunning is whether a distro runs now, as wsl.exe said a few seconds
// ago at most; internal/agent sets it. A listing's Running is as old as
// the listing (up to wslRelist and more): a distro the user stopped since
// (wsl --shutdown, to repair WSL) would be started again by reading its
// files, so a file is read only while this says it runs.
var WSLRunning func(distro string) bool
// WSLOff is whether Settings' Detect agents in WSL is off (#1264); set by
// internal/agent. Off, no distro's sessions are listed, not even those
// listed before.
var WSLOff func() bool
// WSLListed is whether wsl.exe answered the last list of distros, so that
// WSLHomes naming none means "no distro has an agent" (the user took the
// last one away), not "wsl.exe didn't answer"; internal/agent sets it. Nil
// is not known.
var WSLListed func() bool
// wslLooking is whether the distros' sessions are listed.
func wslLooking() bool { return WSLHomes != nil && (WSLOff == nil || !WSLOff()) }
// wslUp is WSLRunning's answer; true when nothing set it (the listing
// alone decides).
func wslUp(distro string) bool { return WSLRunning == nil || WSLRunning(distro) }
// wslAgents are the agents whose sessions are read in a distro, and the
// folder under its home each keeps them in (the default: an agent's own
// variables there can't be read from Windows).
var wslAgents = []struct{ agent, dir string }{
{"claude", ".claude"},
{"codex", ".codex"},
{"pi", filepath.Join(".pi", "agent")},
{"raft", ".slock"},
{"omp", filepath.Join(".omp", "agent")},
{"kimi", ".kimi-code"},
{"antigravity", filepath.Join(".gemini", "antigravity")},
{"antigravity", filepath.Join(".gemini", "antigravity-insiders")},
{"agy", filepath.Join(".gemini", "antigravity-cli")},
}
// wslScan lists the session files under a distro's home.
func wslScan(h WSLHome) []file {
var out []file
for _, a := range wslAgents {
dir := filepath.Join(h.Home, a.dir)
if h.Open != nil {
dir = h.Open(dir)
}
switch a.agent {
case "claude":
out = append(out, ccFiles("claude", dir)...)
case "codex":
out = append(out, codexFilesIn(dir)...)
case "pi":
out = append(out, piFilesIn(dir, "")...)
case "raft":
out = append(out, raftFilesIn(dir)...)
case "omp":
out = append(out, ompFilesIn(dir)...)
case "kimi":
out = append(out, kimiFilesIn(filepath.Join(dir, "sessions"))...)
case "antigravity":
// the rows name the distro's own folders (file:///home/…)
conv := filepath.Join(dir, "conversations")
if filepath.Base(a.dir) == "antigravity-insiders" {
if !sameDir(dir, AntigravityInsidersDir()) {
out = append(out, antigravityInsiders(conv, "linux")...)
}
} else if !sameDir(dir, AntigravityAppDir()) {
out = append(out, antigravityFilesIn(conv, "linux")...)
}
case "agy":
if !sameDir(dir, AgyDir()) {
out = append(out, agyFilesIn(dir, "linux")...)
}
}
}
return out
}
// wslEntry is a file of a distro's listing, as kept on disk.
type wslEntry struct {
Agent string `json:"agent"`
Key string `json:"key"`
Path string `json:"path"`
Main bool `json:"main,omitempty"`
Size int64 `json:"size"`
Mod time.Time `json:"mod"`
// ReadOnly: the Antigravity app's conversations, which magpie lists
// and never moves or deletes
ReadOnly bool `json:"readOnly,omitempty"`
// Agy and Rev: an agy or Antigravity conversation's summary row and
// what tells it changed (file.more's agy and rev)
Agy *agySummary `json:"agy,omitempty"`
Rev string `json:"rev,omitempty"`
// Calls is an agy transcript's size and time as a call source, its
// conversation's database taken in (agyCallStat); nil for a file that
// isn't one (a conversation listed from its row alone)
Calls *wslStat `json:"calls,omitempty"`
}
// wslStat is a file's size and time.
type wslStat struct {
Size int64 `json:"size"`
Mod time.Time `json:"mod"`
}
// wslEntryOf is f as a listing keeps it.
func wslEntryOf(f file) wslEntry {
e := wslEntry{Agent: f.agent, Key: f.key, Path: f.path, Main: f.main, Size: f.size, Mod: f.mod.Time(), ReadOnly: f.readOnly, Agy: f.m().agy, Rev: f.m().rev}
if f.agent == "agy" && !agyRowOnly(f.path) {
c := file{path: f.path, size: f.size, mod: f.mod}
agyCallStat(&c)
e.Calls = &wslStat{Size: c.size, Mod: c.mod.Time()}
}
return e
}
// wslListing is a distro's files as last listed.
type wslListing struct {
Home string `json:"home"`
Files []wslEntry `json:"files"`
Running bool `json:"-"` // as the last listing found it; false until one has
}
var wslSess struct {
sync.Mutex
gen uint64 // Reset's: a listing started before one is dropped
loaded bool
lists map[string]*wslListing // by distro
at time.Time // when the last listing started
first time.Time // when the first listing started: reads wait wslFirstWait from it, in all
done chan struct{} // the listing under way, nil when none
kept []byte // what sessions-wsl.json holds
}
var (
// wslRelist is how often the distros' files are listed again
wslRelist = 30 * time.Second
// wslFirstWait is how long reads wait for the very first listing,
// when nothing was kept of one, all told from when it started; after
// it they go on without. Each read waiting it afresh held the Sessions
// page's first open, whose list, stats and overview each read, ~10s
// on a hung wsl.exe (shangyuan0818 on X).
wslFirstWait = 3 * time.Second
)
func wslListPath() string { return filepath.Join(filepath.Dir(CachePath()), "sessions-wsl.json") }
// wslFiles are the session files of the given agents in the WSL distros,
// as last listed; a listing is started behind when the last is older than
// wslRelist.
func wslFiles(agents ...string) []file { return wslFilesAs(false, agents...) }
// wslCallFiles are wslFiles as call sources: an agy transcript with its
// conversation's database taken into its size and time, as agyCallFiles
// has it, and none of agy's conversations listed from their row alone.
func wslCallFiles(agents ...string) []file { return wslFilesAs(true, agents...) }
func wslFilesAs(calls bool, agents ...string) []file {
agents = slices.DeleteFunc(slices.Clone(agents), func(a string) bool { return !Reads(a) })
if len(agents) == 0 || !wslLooking() {
return nil
}
wslSess.Lock()
if !wslSess.loaded {
wslSess.loaded = true
wslSess.lists = map[string]*wslListing{}
if b, err := os.ReadFile(wslListPath()); err == nil && json.Unmarshal(b, &wslSess.lists) == nil {
wslSess.kept = b
}
for n, l := range wslSess.lists {
if l == nil {
delete(wslSess.lists, n)
}
}
}
if wslSess.done == nil && time.Since(wslSess.at) > wslRelist {
done := make(chan struct{})
wslSess.done, wslSess.at = done, time.Now()
if wslSess.first.IsZero() {
wslSess.first = wslSess.at
}
go wslList(wslSess.gen, done)
}
wait := wslSess.done
if len(wslSess.lists) > 0 {
wait = nil // what was found before is read now; the new listing comes next time
}
left := wslFirstWait - time.Since(wslSess.first)
wslSess.Unlock()
if wait != nil && left > 0 {
t := time.NewTimer(left)
select {
case <-wait:
case <-t.C:
}
t.Stop()
}
want := map[string]bool{}
for _, a := range agents {
want[a] = true
}
// which run is asked outside the lock: wsl.exe may take a while
wslSess.Lock()
up := map[string]bool{}
for n := range wslSess.lists {
up[n] = false
}
wslSess.Unlock()
for n := range up {
up[n] = wslUp(n)
}
wslSess.Lock()
defer wslSess.Unlock()
names := make([]string, 0, len(wslSess.lists))
for n := range wslSess.lists {
names = append(names, n)
}
sort.Strings(names)
var out []file
for _, n := range names {
l := wslSess.lists[n]
cold := !l.Running || !up[n]
in := &fileMore{wsl: n} // the distro's files share it
for _, e := range l.Files {
if !want[e.Agent] {
continue
}
f := file{agent: e.Agent, key: e.Key, path: e.Path, main: e.Main, size: e.Size, mod: modOf(e.Mod), more: in, cold: cold, readOnly: e.ReadOnly}
if calls && e.Agent == "agy" {
if e.Calls == nil {
continue
}
f.size, f.mod = e.Calls.Size, modOf(e.Calls.Mod)
}
if e.Agy != nil || e.Rev != "" {
m := f.w()
m.agy, m.rev = e.Agy, e.Rev
}
out = append(out, f)
}
}
return out
}
// wslList lists the distros' files: a running one's afresh, a stopped
// one's as they were. With no distros said at all (wsl.exe didn't answer)
// every listing is kept, cold. A wsl.exe that answered with no distro an
// agent is in (WSLListed) lets them all go.
func wslList(gen uint64, done chan struct{}) {
defer close(done)
homes := WSLHomes()
wslSess.Lock()
old := wslSess.lists
wslSess.Unlock()
lists := map[string]*wslListing{}
for _, h := range homes {
if h.Distro == "" || h.Home == "" {
continue
}
if !h.Running || !wslUp(h.Distro) {
if o := old[h.Distro]; o != nil && o.Home == h.Home {
c := *o
c.Running = false
lists[h.Distro] = &c
}
continue
}
l := &wslListing{Home: h.Home, Running: true, Files: []wslEntry{}}
for _, f := range wslScan(h) {
l.Files = append(l.Files, wslEntryOf(f))
}
lists[h.Distro] = l
}
// with no distros said and no answer from wsl.exe every listing is
// kept; one that answered and found no agent anywhere has none left to
// list, and the rows of files that are gone with them
if len(homes) == 0 && (WSLListed == nil || !WSLListed()) {
for n, o := range old {
c := *o
c.Running = false
lists[n] = &c
}
}
b, _ := json.Marshal(lists)
wslSess.Lock()
defer wslSess.Unlock()
if gen != wslSess.gen {
return
}
wslSess.lists = lists
if wslSess.done == done {
wslSess.done = nil
}
if b != nil && !bytes.Equal(b, wslSess.kept) {
p := wslListPath()
// wslFiles reads a file that doesn't parse as no listing: it is
// copied aside before this one goes over it, and one that can't be
// copied isn't written over (#2263)
if err := lastgood.KeepUnparsed(p, lastgood.Into[map[string]*wslListing]); err != nil {
log.Printf("sessions-wsl.json wasn't written over: %v", err)
} else if os.MkdirAll(filepath.Dir(p), 0o755) == nil {
tmp := p + ".tmp"
if os.WriteFile(tmp, b, 0o644) == nil && os.Rename(tmp, p) == nil {
wslSess.kept = b
} else {
os.Remove(tmp)
}
}
}
}
// wslCold reports whether path is in a WSL distro's home that isn't
// running (or not yet known to run): such a file is not opened.
func wslCold(path string) bool {
if WSLHomes == nil {
return false
}
wslSess.Lock()
var in string
for n, l := range wslSess.lists {
if under(path, l.Home) {
if !l.Running {
wslSess.Unlock()
return true
}
in = n
}
}
wslSess.Unlock()
return in != "" && !wslUp(in)
}
// under reports whether path is dir or in it, its case aside (Windows').
func under(path, dir string) bool {
if len(path) < len(dir) || !strings.EqualFold(path[:len(dir)], dir) {
return false
}
return len(path) == len(dir) || os.IsPathSeparator(path[len(dir)]) || os.IsPathSeparator(dir[len(dir)-1])
}
// wslDirs are the agents' folders in the distros sessions were found in.
func wslDirs() []string {
if !wslLooking() {
return nil
}
wslSess.Lock()
defer wslSess.Unlock()
var out []string
seen := map[string]bool{}
for _, l := range wslSess.lists {
for _, e := range l.Files {
for _, a := range wslAgents {
if d := filepath.Join(l.Home, a.dir); a.agent == e.Agent && !seen[d] {
seen[d] = true
out = append(out, d)
}
}
}
}
sort.Strings(out)
return out
}
// wslHomeOf is the home of a listed distro, "" when it isn't.
func wslHomeOf(distro string) string {
wslSess.Lock()
defer wslSess.Unlock()
if l := wslSess.lists[distro]; l != nil {
return l.Home
}
return ""
}
// wslForget drops a deleted session's files from their distro's listing,
// and has the next read list the distros again (which keeps that on disk).
func wslForget(fs []file) {
gone := map[string]bool{}
for _, f := range fs {
if f.m().wsl != "" {
gone[f.path] = true
}
}
if len(gone) == 0 {
return
}
wslSess.Lock()
defer wslSess.Unlock()
for _, l := range wslSess.lists {
var kept []wslEntry
for _, e := range l.Files {
if !gone[e.Path] {
kept = append(kept, e)
}
}
l.Files = kept
}
wslSess.at = time.Time{}
}
// wslRelistNow lists the distros again when a restored session's files
// went back into one, after any listing under way (which may have missed
// them), waiting for it as a first listing is waited for: the session is
// listed when the page asks next.
func wslRelistNow(items []moved) {
if WSLHomes == nil {
return
}
wslSess.Lock()
in := false
for _, l := range wslSess.lists {
for _, m := range items {
in = in || under(m.From, l.Home)
}
}
busy := wslSess.done
wslSess.Unlock()
if !in {
return
}
if busy != nil {
<-busy
}
done := make(chan struct{})
wslSess.Lock()
wslSess.done, wslSess.at = done, time.Now()
go wslList(wslSess.gen, done)
wslSess.Unlock()
select {
case <-done:
case <-time.After(wslFirstWait):
}
}
// wslReset forgets the listings, in memory.
func wslReset() {
wslSess.Lock()
defer wslSess.Unlock()
wslSess.gen++
wslSess.loaded, wslSess.lists, wslSess.at, wslSess.first, wslSess.done, wslSess.kept = false, nil, time.Time{}, time.Time{}, nil, nil
}
// wslResume is the PowerShell line that resumes a session in a WSL distro
// (run is the agent's own command): wsl.exe starts the user's own shell
// there as a login and interactive one, so the agent is on the PATH it has
// in a terminal there (~/.local/bin, nvm's), in the session's folder.
// Nothing in it is in double quotes, which Windows PowerShell 5.1 passes
// to wsl.exe mangled.
//
// wsl.exe -d 'Ubuntu' --cd '/home/me/app' -e sh -lc 'exec ${SHELL:-sh} -lic ''claude --resume <id>'''
func wslResume(distro, cwd, run string) string {
ps := func(s string) string { return "'" + strings.ReplaceAll(s, "'", "''") + "'" }
line := "wsl.exe -d " + ps(distro)
if strings.HasPrefix(cwd, "/") {
line += " --cd " + ps(cwd)
}
return line + " -e sh -lc " + ps("exec ${SHELL:-sh} -lic '"+run+"'")
}