Files
magpie/internal/proc/listing.go
T
vincentandJoeyWu-tech 62f612fd44 proc: recheck tools installed within a directory clock tick
Adapt JoeyWu-tech f9d7eac9: do not retain lookup answers or directory listings while their timestamps are fresh. Freeze the clock in the same-tick regression so scheduler delays cannot age the fixture. Keep the settled/future directory cache and document the 100ms resolution limit.

Validation: the same-tick regression fails with both old layers, only the stamp fixed, and only the listing fixed. Restored proc lookup and four affected agent tests pass five race runs.

Batch run, at tree 104d75f0a (the twelve coffemon ports together, on
origin/main 7d022bdb6), mote, macOS, go1.26.8:
- go test -tags nogui -count=1 -timeout 45m on the packages the batch
  changes (agent, autostart, daemon, gateway, library, proc, provider,
  sessions, testenv, tui, gui): ok each.
- GUI, node --test with Playwright 1.62.0, Chromium and WebKit:
  library-*.test.cjs and i18n-keys.test.cjs, 1044 of 1044 pass.
- gofmt -l: nothing. go vet ./..., GOOS=linux go vet -tags nogui ./...,
  GOOS=windows go vet ./... and go build ./...: ok at cdd7c8e46, the same
  batch on origin/main 00098f12b one rebase before.
- The whole go test ./... at cdd7c8e46: 86 packages ok, one red,
  internal/cli TestEveryRouteHasACommandOrIsNamed (GET
  /api/context/reply has no command), red on main too (#5026).
- Linux (wsl-run, Ubuntu 24.04, at 339055896, the batch on f4c664a09):
  proc, autostart, testenv, tui ok; agent: only TestGrokInstalled red
  (#3548); #4033's proc.Look reds (Zed, Copilot, Qoder, Cursor, Omp)
  pass.
- Windows (win-run, Windows 11, at 339055896): proc, autostart, testenv,
  tui ok; library and agent red in the same tests as on origin/main
  f4c664a09 (#4115, #1827, #4786; #5068, #5069, #5070 filed now).
- Rebased onto origin/main fb1f4856a (three commits, no file in
  common with the batch) and run again at the pushed tree: gofmt -l
  nothing, go build ./... and go vet ./... ok; go test -tags nogui
  sessions, agent, autostart, tui, proc, testenv ok; i18n-keys and
  library-* 1044 of 1044 pass.
- Not run before the push: the whole GUI suite (a run at cdd7c8e46 was
  cut off at 1170 of ~1174 tests when mote rebooted; it runs again after
  the tag and any red is filed).

Co-authored-by: JoeyWu-tech <joey110551567@gmail.com>
2026-11-03 20:38:11 +08:00

171 lines
4.5 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 proc
import (
"errors"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"sync"
"time"
)
// A command that isn't installed was looked for with a stat of every
// name it could have (11 PATHEXT extensions on Windows) in every folder
// of PATH and of findDirs: hundreds of stats a command, and the Library
// and Agents pages ask for about a hundred agents' commands. The answers
// are kept (looked), but every one of them is asked again when any of
// those folders changes, and at the app's start, so the first change on
// the Library page after a start, or after some folder on PATH changed
// while it sat idle, took 5–8s on a Windows machine with 11 agents and an
// antivirus that slows each stat (#1624, Hipple). Each folder's listing is
// read once instead and kept while its modification time is unchanged: a
// command none of them has a name for isn't there, without a stat of its
// own; one that may be is looked for as before.
// listingRead is how many folders were read, and execLookPath what looks
// for a command that may be there; the tests count both.
var (
listingRead int
execLookPath = exec.LookPath
)
var listings struct {
sync.Mutex
m map[string]*listing
}
type listing struct {
mod time.Time
checked time.Time
// names are the folder's entries, lower-cased, each also as it is up
// to each dot in it: claude.exe is there as "claude" and "claude.exe"
names map[string]bool
}
// mayBeIn is whether name could be a program in one of dirs: false only
// when every folder was read and none has an entry name could be (name,
// or name and an extension, in any case). A folder that can't be read
// may have it.
func mayBeIn(name string, dirs []string) bool {
if name == "" || strings.ContainsAny(name, `/\:`) {
return true
}
key := strings.ToLower(name)
for _, d := range dirs {
if d == "" {
continue
}
if !filepath.IsAbs(d) {
abs, err := filepath.Abs(d)
if err != nil {
return true
}
d = abs
}
names, ok := listed(d)
if !ok || names[key] {
return true
}
}
return false
}
// listed is the names in dir, read again when its modification time
// changed (checked once a lookWindow); false when it can't be read. A
// folder changed within lookSettle is read and not kept: what is
// installed in the same clock tick leaves its time as it was (lookStamp).
func listed(dir string) (map[string]bool, bool) {
now := lookNow()
listings.Lock()
l := listings.m[dir]
if l != nil && now.Sub(l.checked) < lookWindow() {
listings.Unlock()
return l.names, true
}
listings.Unlock()
st, err := os.Stat(dir)
if errors.Is(err, os.ErrNotExist) {
// nothing is in a folder that isn't there
listings.Lock()
if listings.m == nil {
listings.m = map[string]*listing{}
}
listings.m[dir] = &listing{checked: now}
listings.Unlock()
return nil, true
}
if err != nil || !st.IsDir() {
return nil, false
}
if l != nil && l.names != nil && l.mod.Equal(st.ModTime()) {
listings.Lock()
l.checked = now
listings.Unlock()
return l.names, true
}
es, err := os.ReadDir(dir)
if err != nil {
return nil, false
}
names := make(map[string]bool, len(es))
for _, e := range es {
n := strings.ToLower(e.Name())
names[n] = true
for i := 1; i < len(n); i++ {
if n[i] == '.' {
names[n[:i]] = true
}
}
}
listings.Lock()
listingRead++
if d := now.Sub(st.ModTime()); d >= 0 && d < lookSettle {
delete(listings.m, dir)
} else {
if listings.m == nil {
listings.m = map[string]*listing{}
}
listings.m[dir] = &listing{mod: st.ModTime(), checked: now, names: names}
}
listings.Unlock()
return names, true
}
// pathDirs are the folders exec.LookPath looks in: PATH's (an empty
// entry the current folder, but on Windows), and on Windows the current
// folder first unless NoDefaultCurrentDirectoryInExePath is set.
func pathDirs() []string {
dirs := filepath.SplitList(os.Getenv("PATH"))
if runtime.GOOS != "windows" {
// an empty entry is the current folder there
for i, d := range dirs {
if d == "" {
dirs[i] = "."
}
}
} else {
if _, off := os.LookupEnv("NoDefaultCurrentDirectoryInExePath"); !off {
dirs = append([]string{"."}, dirs...)
}
}
return dirs
}
// lookPath is exec.LookPath, with a command no folder of PATH has a name
// for answered as not found without looking.
func lookPath(name string) (string, error) {
if !mayBeIn(name, pathDirs()) {
return "", &exec.Error{Name: name, Err: exec.ErrNotFound}
}
return execLookPath(name)
}
// forgetListings drops the folders read.
func forgetListings() {
listings.Lock()
listings.m = nil
listings.Unlock()
}