diff --git a/README.md b/README.md index 0fbed74..b82b75e 100644 --- a/README.md +++ b/README.md @@ -102,7 +102,28 @@ when it lists an entry that is not an object with a known status (`initializing`, `running`, `idle`, `error`, or `closed`), or when a running or initializing agent has no working directory. -Before each deletion, Plonge checks that the path is still a directory, +Plonge also refuses a cache that a running process uses, +such as a `.venv` that a service runs from, +because the HEAD log and transcripts do not show a long-running service. +A process uses a cache when it has a file open or a library mapped under it, +works in it, runs an executable from it, or names a path in it on its command line. +`--include-active` does not override this check. +The report names the process: + +```console +Refused ~/code/app/.venv: in use by python3.12 (PID 4312), whose command line names bin/python +``` + +On macOS, Plonge runs `lsof` and `ps` once for each scan; +on Linux, it reads `/proc`. +If it cannot list the processes, every project counts as busy. +The check does not see a service that is stopped when Plonge runs, +a timer or cron job, or a process that starts after the last check. +The [rule reference](docs/rules.md#caches-that-a-running-process-uses) +describes what the check can miss. + +Before each deletion, Plonge checks again that no process uses the cache, +and that the path is still a directory, is not a symlink, is inside its root, is ignored by Git, and contains no tracked files. It also refuses a directory that holds an entry that its rule protects; diff --git a/cmd/plonge/main_test.go b/cmd/plonge/main_test.go index 4c85459..ebc2553 100644 --- a/cmd/plonge/main_test.go +++ b/cmd/plonge/main_test.go @@ -7,6 +7,7 @@ import ( "os" "os/exec" "path/filepath" + "runtime" "strings" "testing" "time" @@ -15,7 +16,9 @@ import ( ) // binDir is the only directory on PATH during a test. -// It holds Git and fake tools, so that no test can run a real tool. +// It holds Git and fake tools, so that no test can run a real tool +// other than lsof and ps, which the process check finds in the system +// directories on macOS, and which only read the process list. func binDir(t *testing.T) string { t.Helper() git, err := exec.LookPath("git") @@ -305,7 +308,7 @@ func TestCleanKeepsBusyRootAndReportsSources(t *testing.T) { t.Fatalf("exit %d: %s", code, stderr.String()) } out := stdout.String() - for _, want := range []string{"busy project (Paseo)", "1 cache", "Activity source skipped: claude", "Estimated freed: 0 B"} { + for _, want := range []string{"busy project:", "1 cache", "Activity source skipped: claude", "Estimated freed: 0 B"} { if !strings.Contains(out, want) { t.Errorf("output lacks %q:\n%s", want, out) } @@ -332,7 +335,7 @@ func TestCleanKeepsBusyRootAndReportsSources(t *testing.T) { if len(rep.Caches) != 1 || !rep.Caches[0].Busy || rep.Caches[0].Quiet || rep.Caches[0].ActivitySource != "paseo" { t.Errorf("caches = %+v", rep.Caches) } - if len(rep.Sources) != 2 { + if len(rep.Sources) != 3 || rep.Sources[2].Name != "processes" { t.Errorf("sources = %+v", rep.Sources) } } @@ -356,7 +359,7 @@ func TestCleanKeepsEveryRootWhenPaseoFails(t *testing.T) { } out := stdout.String() for _, want := range []string{ - "busy project (Paseo)", "1 cache", + "busy project:", "1 cache", "Activity source failed: paseo (paseo ls failed: no daemon)", "Every project counts as busy. Fix paseo, or remove it from PATH for this run.", } { @@ -706,3 +709,35 @@ func TestCleanTextListsSkippedRoots(t *testing.T) { t.Errorf("output lacks %q:\n%s", want, stdout.String()) } } + +// TestCleanRefusesCacheThatAProcessUses starts a process that works +// in a cache, and checks that clean keeps the cache and names the process. +func TestCleanRefusesCacheThatAProcessUses(t *testing.T) { + if runtime.GOOS != "linux" && runtime.GOOS != "darwin" { + t.Skip("no process inspector for " + runtime.GOOS) + } + home := setup(t) + t.Setenv("GIT_CEILING_DIRECTORIES", home) + project := filepath.Join(home, "app") + quietRepo(t, project) + cache := filepath.Join(project, "node_modules") + sleep := exec.Command("/bin/sleep", "60") + sleep.Dir = cache + if err := sleep.Start(); err != nil { + t.Fatal(err) + } + t.Cleanup(func() { _ = sleep.Process.Kill(); _ = sleep.Wait() }) + + var stdout, stderr bytes.Buffer + args := []string{"clean", "--no-mise", "--root", project, "--apply"} + if code := run(args, &stdout, &stderr); code != 0 { + t.Fatalf("exit %d: %s", code, stderr.String()) + } + want := fmt.Sprintf("Refused ~/app/node_modules: in use by sleep (PID %d), which works in it\n", sleep.Process.Pid) + if out := stdout.String(); !strings.Contains(out, want) { + t.Errorf("output lacks %q:\n%s", want, out) + } + if _, err := os.Stat(cache); err != nil { + t.Errorf("a cache that a process uses was deleted: %v", err) + } +} diff --git a/docs/coverage.md b/docs/coverage.md index 3162982..df2e193 100644 --- a/docs/coverage.md +++ b/docs/coverage.md @@ -338,6 +338,9 @@ For every row: Busy projects are excluded; `--include-active` adds active projects. If `paseo` is on PATH but fails, or lists an agent that Plonge cannot read, every project is busy. + A cache that a running process uses is refused, + and if Plonge cannot list the processes, every project is busy; + see [Caches that a running process uses](rules.md#caches-that-a-running-process-uses). - `clean --global` shows global cleanup actions instead of project caches. Add `--apply` to run eligible prune actions. A prune rule's size marked `(whole cache)` is the size of its whole cache, diff --git a/docs/rules.md b/docs/rules.md index 0782dd2..708faa9 100644 --- a/docs/rules.md +++ b/docs/rules.md @@ -137,6 +137,77 @@ Encoded names can also match a sibling such as `app-two` for a root named `app`; this can only make the root appear more active. A missing or unreadable transcript directory is reported as a skipped source. +## Caches that a running process uses + +A service or a long-running command does not update the Git HEAD log +or a transcript, so its project can look quiet while it runs. +Plonge refuses a project cache that a running process uses, +whatever the project's activity, and `--include-active` does not override it. +A process uses a cache when one of these is at or below the cache's path: + +- a file that the process has open, or a library that it maps +- its working or root directory +- its executable +- a path on its command line + +A command-line word counts when it is an absolute path, +or a relative path with a `/`, which Plonge joins to the process's working directory. +In a word such as `--config=path`, the text after the first `=` counts. +Plonge reads command lines because the executable alone can miss a cache: +a virtual environment's `bin/python` is a symlink to the base interpreter, +so a process started from `.venv/bin/python` runs the interpreter's file, +and only its command line names `.venv`. +Command-line paths are not resolved through symlinks for the same reason. +A cache path that is or is inside a symlink +also matches the paths at its resolved location. + +On macOS, Plonge takes one list of processes for each scan, +with one `lsof` call for the open files, working directories, and executables +of the current user's processes, and one `ps` call for the command lines of all processes. +On Linux, it reads `exe`, `cwd`, `root`, `fd`, `maps`, `cmdline`, and `comm` +in `/proc//` and does not need `lsof`. +`clean --apply` takes another list before the first deletion, +and takes a new one before a later deletion +only when the last one is more than 10 seconds old. +A cache that a process uses at that check is skipped. + +The report lists each refused cache and the first process that uses it, +with the number of other processes: + +```console +Refused ~/code/web/node_modules: in use by node (PID 911), which has esbuild/bin/esbuild open, and 2 more +``` + +With `--json`, the entry in `refused` also has `processes`, +a list of objects with `pid`, `name`, `user`, `use`, and `path`. +`use` is `runs`, `open`, `cwd`, `root`, or `args`, +and `path` is the path that the process uses. + +If Plonge cannot list the processes, +because `lsof` or `ps` is missing, fails, or takes more than 30 seconds, +or because `/proc` cannot be read, +the activity source `processes` fails and every project counts as busy. +Before a deletion, a failed list skips the cache. +No option overrides this, +because a missing tool does not show that no process uses a cache. + +The check has these limits: + +- It does not see a service that is stopped when Plonge runs, + a timer or cron job that starts later, + or a process that starts after the last check. +- It does not see a process that names no path in the cache and holds nothing open there, + such as `python` run from an activated environment, + or a script that Node.js read and closed. +- Without root, Plonge cannot read another user's open files, + working directory, or executable, + so it sees another user's process only through its command line. + On Linux, `/proc` mounted with `hidepid` hides those processes. +- On macOS, `ps` joins the words of a command line with spaces, + so a path with a space in it can be missed. + +Global rules do not use this check. + ## Caches that hold a Git repository An ignored cache can contain a dependency cloned from Git, diff --git a/internal/clean/clean.go b/internal/clean/clean.go index ed2b677..c3ecb14 100644 --- a/internal/clean/clean.go +++ b/internal/clean/clean.go @@ -12,6 +12,7 @@ import ( "time" "github.com/mattt/plonge/internal/disk" + "github.com/mattt/plonge/internal/procs" "github.com/mattt/plonge/internal/scan" ) @@ -34,6 +35,10 @@ type Options struct { // It returns why the root is busy, or "" when it is not. // Run calls it once per root, before the first deletion there. StillBusy func(root string) string + // InUse, when set, asks whether a running process uses a cache. + // It returns why the cache is in use, or "" when it is not. + // Run calls it before it checks and deletes each cache. + InUse func(path string) string } // Item is one selected cache and what happened to it. @@ -80,7 +85,7 @@ type Result struct { Roots int `json:"roots"` SkippedRoots []scan.SkippedRoot `json:"skipped_roots"` // Refused lists the caches that the scan refused - // because they are or hold a Git repository. + // because a running process uses them, or they are or hold a Git repository. Refused []scan.Refusal `json:"refused"` ActivitySources []scan.Source `json:"activity_sources"` @@ -146,6 +151,8 @@ func Run(ctx context.Context, roots []scan.Root, sizes [][]int64, opts Options) } if reason := busy[p.root]; reason != "" { p.item.Status, p.item.Reason = Skipped, reason + } else if reason := inUse(opts, p.item.Path); reason != "" { + p.item.Status, p.item.Reason = Skipped, reason } else { apply(ctx, &p) } @@ -162,6 +169,40 @@ func Run(ctx context.Context, roots []scan.Root, sizes [][]int64, opts Options) return res } +func inUse(opts Options, path string) string { + if opts.InUse == nil { + return "" + } + return opts.InUse(path) +} + +// SnapshotAge is how old a process snapshot can be before InUseChecker takes another. +const SnapshotAge = 10 * time.Second + +// InUseChecker returns a function for Options.InUse that asks insp. +// It takes a snapshot at its first call, and another at a later call +// only when the last one is older than SnapshotAge. +// When a snapshot fails, every call until the next one returns the error, +// so the caches are skipped. +func InUseChecker(ctx context.Context, insp procs.Inspector, now func() time.Time) func(path string) string { + var snap *procs.Snapshot + var err error + var taken time.Time + return func(path string) string { + if t := now(); taken.IsZero() || t.Sub(taken) > SnapshotAge { + snap, err = insp.Snapshot(ctx) + taken = t + } + if err != nil { + return "cannot list processes: " + err.Error() + } + if ms := snap.Find([]string{path})[filepath.Clean(path)]; len(ms) > 0 { + return procs.Reason(path, ms) + } + return "" + } +} + func apply(ctx context.Context, p *planned) { reason, err := scan.Verify(ctx, p.root, p.cand) if err != nil { diff --git a/internal/clean/inuse_test.go b/internal/clean/inuse_test.go new file mode 100644 index 0000000..36e7277 --- /dev/null +++ b/internal/clean/inuse_test.go @@ -0,0 +1,101 @@ +package clean + +import ( + "context" + "errors" + "path/filepath" + "strings" + "testing" + "time" + + "github.com/mattt/plonge/internal/procs" +) + +// fakeInspector returns a fixed snapshot or error and counts its calls. +type fakeInspector struct { + snap *procs.Snapshot + err error + calls int +} + +func (f *fakeInspector) Snapshot(context.Context) (*procs.Snapshot, error) { + f.calls++ + return f.snap, f.err +} + +// TestInUseStopsDeletion starts a process in .venv after the scan. +func TestInUseStopsDeletion(t *testing.T) { + root := repoWith(t) + roots, sizes := scanOne(t, root) + venv := filepath.Join(root, ".venv") + insp := &fakeInspector{snap: &procs.Snapshot{Processes: []procs.Process{ + {PID: 4312, Name: "python3.12", Files: []procs.File{{Use: procs.UseArgs, Path: filepath.Join(venv, "bin", "python")}}}, + }}} + opts := quietOpts(true) + opts.InUse = InUseChecker(context.Background(), insp, time.Now) + res := Run(context.Background(), roots, sizes, opts) + if res.Skipped != 1 || res.Failed != 0 || insp.calls != 1 { + t.Fatalf("%d calls, result = %+v", insp.calls, res) + } + for _, it := range res.Items { + switch it.Path { + case venv: + if it.Status != Skipped || it.Reason != "in use by python3.12 (PID 4312), whose command line names bin/python" { + t.Errorf(".venv: %+v", it) + } + default: + if it.Status != Deleted { + t.Errorf("%s: %+v", it.Path, it) + } + } + } + if !exists(venv) || exists(filepath.Join(root, "node_modules")) { + t.Errorf(".venv exists %v, node_modules exists %v", exists(venv), exists(filepath.Join(root, "node_modules"))) + } +} + +func TestInUseErrorSkipsEveryCache(t *testing.T) { + root := repoWith(t) + roots, sizes := scanOne(t, root) + opts := quietOpts(true) + opts.InUse = InUseChecker(context.Background(), &fakeInspector{err: errors.New("lsof failed: exit status 1")}, time.Now) + res := Run(context.Background(), roots, sizes, opts) + if res.Skipped != 2 { + t.Fatalf("result = %+v", res) + } + for _, it := range res.Items { + if it.Reason != "cannot list processes: lsof failed: exit status 1" || !exists(it.Path) { + t.Errorf("%+v, exists %v", it, exists(it.Path)) + } + } +} + +func TestInUseCheckerRefreshes(t *testing.T) { + insp := &fakeInspector{snap: &procs.Snapshot{}} + now := time.Date(2026, 11, 17, 9, 0, 0, 0, time.UTC) + check := InUseChecker(context.Background(), insp, func() time.Time { return now }) + steps := []struct { + after time.Duration + calls int + }{ + {0, 1}, + {5 * time.Second, 1}, + {5 * time.Second, 1}, // exactly SnapshotAge old + {time.Second, 2}, + {9 * time.Second, 2}, + } + for i, s := range steps { + now = now.Add(s.after) + if reason := check("/code/app/.venv"); reason != "" { + t.Errorf("step %d: reason %q", i, reason) + } + if insp.calls != s.calls { + t.Errorf("step %d: %d calls, want %d", i, insp.calls, s.calls) + } + } + insp.err = errors.New("ps is not installed") + now = now.Add(time.Minute) + if reason := check("/code/app/.venv"); !strings.Contains(reason, "ps is not installed") { + t.Errorf("reason after a failed snapshot: %q", reason) + } +} diff --git a/internal/engine/engine.go b/internal/engine/engine.go index 183c744..ee9528d 100644 --- a/internal/engine/engine.go +++ b/internal/engine/engine.go @@ -10,6 +10,7 @@ import ( "github.com/mattt/plonge/internal/disk" "github.com/mattt/plonge/internal/global" "github.com/mattt/plonge/internal/mise" + "github.com/mattt/plonge/internal/procs" "github.com/mattt/plonge/internal/report" "github.com/mattt/plonge/internal/rules" "github.com/mattt/plonge/internal/scan" @@ -30,7 +31,7 @@ type Inventory struct { Sizes [][]int64 Report report.Report Now time.Time - // Sources reports each source of agent activity. + // Sources reports each source of activity: agents and running processes. Sources []scan.Source } @@ -64,6 +65,7 @@ func Inspect(ctx context.Context, sel Selection) (Inventory, error) { paths = mise.DropNested(paths, func(root string) bool { return inTree[root] }) inv.Roots = scan.Run(ctx, paths, inv.Rules) inv.Sources = agents.Apply(ctx, inv.Roots, env.Home, inv.Now) + inv.Sources = append(inv.Sources, scan.RefuseUsed(ctx, inv.Roots, procs.System())) inv.Sizes = scan.Measure(ctx, inv.Roots) inv.Report = report.Build(inv.Roots, inv.Sizes, inv.Rules.QuietHours, broken, inv.Now) inv.Report.ActivitySources = inv.Sources @@ -96,6 +98,7 @@ func Clean(ctx context.Context, opts CleanOptions) (clean.Result, Inventory, err QuietHours: inv.Rules.QuietHours, Now: inv.Now, StillBusy: func(root string) string { return agents.Busy(ctx, root, inv.Env.Home) }, + InUse: clean.InUseChecker(ctx, procs.System(), time.Now), }) } if opts.Apply { diff --git a/internal/mcpserver/server_test.go b/internal/mcpserver/server_test.go index 083f701..5a0ef09 100644 --- a/internal/mcpserver/server_test.go +++ b/internal/mcpserver/server_test.go @@ -213,7 +213,7 @@ func TestCleanToolKeepsCachesWhenPaseoFails(t *testing.T) { if len(res.Items) != 0 || res.NotSelected.Busy.Caches != 1 { t.Errorf("%v: result = %+v", args, res) } - if len(res.Sources) != 2 || res.Sources[1].Status != "failed" || !strings.Contains(res.Sources[1].Reason, "no daemon") { + if len(res.Sources) != 3 || res.Sources[1].Status != "failed" || !strings.Contains(res.Sources[1].Reason, "no daemon") { t.Errorf("%v: sources = %+v", args, res.Sources) } } diff --git a/internal/procs/procs.go b/internal/procs/procs.go new file mode 100644 index 0000000..708bd85 --- /dev/null +++ b/internal/procs/procs.go @@ -0,0 +1,202 @@ +// Package procs finds the running processes that use a path. +// +// A process uses a path when it has a file open under it, +// maps a library from it, works in it, runs an executable from it, +// or names it on its command line. +// A virtual environment's bin/python is a symlink to the base interpreter, +// so a process started from it shows the interpreter's path as its executable; +// only its command line names the environment. +package procs + +import ( + "context" + "fmt" + "path/filepath" + "sort" + "strings" + "time" +) + +// The uses of a path, from the most to the least specific. +const ( + UseRuns = "runs" // the process runs an executable from the path + UseOpen = "open" // the process has a file open or a library mapped + UseCwd = "cwd" // the process works in the path + UseRoot = "root" // the process has the path as its root directory + UseArgs = "args" // the process names the path on its command line +) + +var useRank = map[string]int{UseRuns: 0, UseOpen: 1, UseCwd: 2, UseRoot: 3, UseArgs: 4} + +// File is a path that a process uses, and how. +type File struct { + Use string + Path string +} + +// Process is one running process and the paths that it uses. +type Process struct { + PID int + Name string + User string + Files []File +} + +// Snapshot is the list of processes at one time. +type Snapshot struct { + Processes []Process +} + +// Inspector takes a snapshot of the running processes. +type Inspector interface { + Snapshot(ctx context.Context) (*Snapshot, error) +} + +// Match is a process that uses a path. +type Match struct { + PID int `json:"pid"` + Name string `json:"name"` + User string `json:"user,omitempty"` + Use string `json:"use"` + // Path is the path that the process uses, at or below the matched path. + Path string `json:"path"` +} + +// Find returns, for each of paths, the processes that use it, ordered by PID. +// Each process appears once per path, with its most specific use. +// A path matches its own entries and, when it is or is inside a symlink, +// the entries at its resolved path. +// A path with no users is not in the result. +func (s *Snapshot) Find(paths []string) map[string][]Match { + // Two paths can share a key, such as a path and a symlink to it. + keys := map[string][]string{} + minLen := -1 + add := func(key, path string) { + keys[key] = append(keys[key], path) + if minLen < 0 || len(key) < minLen { + minLen = len(key) + } + } + for _, p := range paths { + p = filepath.Clean(p) + add(p, p) + if real, err := filepath.EvalSymlinks(p); err == nil && real != p { + add(real, p) + } + } + out := map[string][]Match{} + if len(keys) == 0 { + return out + } + for _, proc := range s.Processes { + best := map[string]Match{} + for _, f := range proc.Files { + if !filepath.IsAbs(f.Path) { + continue + } + for p := filepath.Clean(f.Path); len(p) >= minLen; p = filepath.Dir(p) { + for _, path := range keys[p] { + m, seen := best[path] + if !seen || useRank[f.Use] < useRank[m.Use] { + best[path] = Match{PID: proc.PID, Name: proc.Name, User: proc.User, Use: f.Use, Path: f.Path} + } + } + if p == "/" { + break + } + } + } + for path, m := range best { + out[path] = append(out[path], m) + } + } + for _, ms := range out { + sort.Slice(ms, func(a, b int) bool { return ms[a].PID < ms[b].PID }) + } + return out +} + +// Reason says why a process in ms keeps path from deletion, +// such as "in use by node (PID 911), which has esbuild/bin/esbuild open, and 2 more". +// ms must not be empty. +func Reason(path string, ms []Match) string { + m := ms[0] + what := relative(path, m.Path) + var how string + switch m.Use { + case UseRuns: + how = "which runs " + what + case UseOpen: + how = "which has " + what + " open" + case UseCwd: + how = "which works in " + what + case UseRoot: + how = "whose root directory is " + what + default: + how = "whose command line names " + what + } + s := fmt.Sprintf("in use by %s (PID %d), %s", m.Name, m.PID, how) + if n := len(ms) - 1; n > 0 { + s += fmt.Sprintf(", and %d more", n) + } + return s +} + +// relative returns used relative to path, or "it" when they are the same. +// When used is under path's resolved location, it is relative to that. +func relative(path, used string) string { + for _, base := range []string{path, resolved(path)} { + if rel, err := filepath.Rel(base, used); err == nil && !strings.HasPrefix(rel, "..") { + if rel == "." { + return "it" + } + return rel + } + } + return used +} + +func resolved(path string) string { + if p, err := filepath.EvalSymlinks(path); err == nil { + return p + } + return path +} + +// argPaths returns the paths that the words of a command line name. +// A word counts when it is an absolute path, or a relative path with a slash, +// which is joined to cwd; with no cwd, a relative path does not count. +// In a word with an =, as in --flag=path, the text after the first = counts, +// and the whole word counts only when it is an absolute path. +// Paths are not resolved, because a symlink such as bin/python +// leads out of the directory that it names. +func argPaths(words []string, cwd string) []string { + var out []string + for _, w := range words { + forms := []string{w} + if _, after, ok := strings.Cut(w, "="); ok { + forms = []string{after} + if filepath.IsAbs(w) { + forms = append([]string{w}, after) + } + } + for _, f := range forms { + switch { + case filepath.IsAbs(f): + out = append(out, filepath.Clean(f)) + case strings.Contains(f, "/") && cwd != "": + out = append(out, filepath.Join(cwd, f)) + } + } + } + return out +} + +// trimDeleted removes the " (deleted)" that Linux and lsof add +// to the name of a file that was removed while open. +func trimDeleted(name string) string { + return strings.TrimSuffix(name, " (deleted)") +} + +// Timeout limits each command that takes a snapshot. Tests shorten it. +var Timeout = 30 * time.Second diff --git a/internal/procs/procs_test.go b/internal/procs/procs_test.go new file mode 100644 index 0000000..132bd89 --- /dev/null +++ b/internal/procs/procs_test.go @@ -0,0 +1,303 @@ +package procs + +import ( + "context" + "os" + "os/exec" + "path/filepath" + "reflect" + "runtime" + "strings" + "testing" + "time" +) + +func TestFind(t *testing.T) { + // Resolve the temporary directory, which is under a symlink on macOS, + // so that the only symlink is the one that the test makes. + dir, err := filepath.EvalSymlinks(t.TempDir()) + if err != nil { + t.Fatal(err) + } + app := filepath.Join(dir, "app") + venv := filepath.Join(app, ".venv") + nm := filepath.Join(app, "node_modules") + if err := os.MkdirAll(venv, 0o755); err != nil { + t.Fatal(err) + } + // link/.venv reaches app/.venv through a symlinked parent, + // so it has the users of app/.venv. + link := filepath.Join(dir, "link") + if err := os.Symlink(app, link); err != nil { + t.Fatal(err) + } + s := &Snapshot{Processes: []Process{ + {PID: 30, Name: "node", Files: []File{ + {UseArgs, filepath.Join(nm, ".bin", "vite")}, + {UseOpen, filepath.Join(nm, "esbuild", "bin", "esbuild")}, + }}, + {PID: 10, Name: "python3.12", Files: []File{ + {UseRuns, "/usr/bin/python3.12"}, + {UseCwd, app}, + {UseArgs, filepath.Join(venv, "bin", "python")}, + }}, + {PID: 20, Name: "sh", Files: []File{{UseCwd, filepath.Join(venv, "lib")}}}, + {PID: 40, Name: "python", Files: []File{{UseRuns, filepath.Join(app, ".venv2", "bin", "python")}}}, + {PID: 50, Name: "relative", Files: []File{{UseOpen, ".venv/x"}}}, + }} + linked := filepath.Join(link, ".venv") + got := s.Find([]string{venv, nm, linked, filepath.Join(app, "build")}) + wantVenv := []Match{ + {PID: 10, Name: "python3.12", Use: UseArgs, Path: filepath.Join(venv, "bin", "python")}, + {PID: 20, Name: "sh", Use: UseCwd, Path: filepath.Join(venv, "lib")}, + } + if !reflect.DeepEqual(got[venv], wantVenv) { + t.Errorf(".venv: %+v", got[venv]) + } + // The open file ranks above the command line word. + wantNM := []Match{{PID: 30, Name: "node", Use: UseOpen, Path: filepath.Join(nm, "esbuild", "bin", "esbuild")}} + if !reflect.DeepEqual(got[nm], wantNM) { + t.Errorf("node_modules: %+v", got[nm]) + } + if !reflect.DeepEqual(got[linked], wantVenv) { + t.Errorf("symlinked .venv: %+v", got[linked]) + } + if len(got) != 3 { + t.Errorf("got %d paths: %+v", len(got), got) + } +} + +func TestReason(t *testing.T) { + path := "/code/app/.venv" + tests := []struct { + m Match + more int + want string + }{ + {Match{PID: 4312, Name: "python3.12", Use: UseArgs, Path: path + "/bin/python"}, 0, + "in use by python3.12 (PID 4312), whose command line names bin/python"}, + {Match{PID: 911, Name: "node", Use: UseOpen, Path: path + "/esbuild/bin/esbuild"}, 2, + "in use by node (PID 911), which has esbuild/bin/esbuild open, and 2 more"}, + {Match{PID: 7, Name: "zsh", Use: UseCwd, Path: path}, 0, "in use by zsh (PID 7), which works in it"}, + {Match{PID: 8, Name: "zsh", Use: UseCwd, Path: path + "/lib"}, 0, "in use by zsh (PID 8), which works in lib"}, + {Match{PID: 9, Name: "ruff", Use: UseRuns, Path: path + "/bin/ruff"}, 0, "in use by ruff (PID 9), which runs bin/ruff"}, + {Match{PID: 10, Name: "x", Use: UseRoot, Path: path}, 0, "in use by x (PID 10), whose root directory is it"}, + } + for _, tt := range tests { + ms := []Match{tt.m} + for range tt.more { + ms = append(ms, Match{PID: 1, Name: "other", Use: UseOpen, Path: path}) + } + if got := Reason(path, ms); got != tt.want { + t.Errorf("Reason = %q, want %q", got, tt.want) + } + } +} + +func TestArgPaths(t *testing.T) { + words := []string{"python", "-m", "app", ".venv/bin/gunicorn", "--config=/etc/app.conf", "/opt/x/../y", "--venv=env/bin", "plain=word"} + want := []string{"/srv/app/.venv/bin/gunicorn", "/etc/app.conf", "/opt/y", "/srv/app/env/bin"} + if got := argPaths(words, "/srv/app"); !reflect.DeepEqual(got, want) { + t.Errorf("with cwd: %q", got) + } + want = []string{"/etc/app.conf", "/opt/y"} + if got := argPaths(words, ""); !reflect.DeepEqual(got, want) { + t.Errorf("without cwd: %q", got) + } +} + +const lsofOutput = `p100 +cnode +Lmattt +fcwd +n/code/web +frtd +n/ +ftxt +n/opt/homebrew/bin/node +ftxt +n/code/web/node_modules/fsevents/fsevents.node +f3 +n/code/web/node_modules/esbuild/bin/esbuild +f4 +n->0x1234 +f5 +n/code/web/tmp/old.log (deleted) +p200 +cpython3.12 +Lmattt +fcwd +n/code/app +ftxt +n/Users/mattt/.local/share/uv/python/bin/python3.12 +pbad +cskipped +p300 +cplonge +Lmattt +fcwd +n/code/app/.venv +` + +const psOutput = ` 100 mattt node node_modules/.bin/vite --port=5173 + 200 mattt /code/app/.venv/bin/python -m app + 300 mattt plonge clean + 400 root /srv/api/.venv/bin/python /srv/api/main.py --config=conf/api.toml + 500 _www +` + +func TestLsofAndPS(t *testing.T) { + s := mergePS(parseLsof(strings.NewReader(lsofOutput)), strings.NewReader(psOutput), 300) + want := []Process{ + {PID: 100, Name: "node", User: "mattt", Files: []File{ + {UseCwd, "/code/web"}, + {UseRuns, "/opt/homebrew/bin/node"}, + {UseOpen, "/code/web/node_modules/fsevents/fsevents.node"}, + {UseOpen, "/code/web/node_modules/esbuild/bin/esbuild"}, + {UseOpen, "/code/web/tmp/old.log"}, + {UseArgs, "/code/web/node_modules/.bin/vite"}, + }}, + {PID: 200, Name: "python3.12", User: "mattt", Files: []File{ + {UseCwd, "/code/app"}, + {UseRuns, "/Users/mattt/.local/share/uv/python/bin/python3.12"}, + {UseArgs, "/code/app/.venv/bin/python"}, + }}, + // Another user's process has no lsof entry and no known cwd. + {PID: 400, Name: "python", User: "root", Files: []File{ + {UseArgs, "/srv/api/.venv/bin/python"}, + {UseArgs, "/srv/api/main.py"}, + }}, + } + if !reflect.DeepEqual(s.Processes, want) { + t.Errorf("processes:\n%+v\nwant\n%+v", s.Processes, want) + } +} + +// fakeProc makes a process directory in a fake /proc tree. +func fakeProc(t *testing.T, root, pid string, files map[string]string, links map[string]string) string { + t.Helper() + dir := filepath.Join(root, pid) + for name, body := range files { + path := filepath.Join(dir, name) + if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(path, []byte(body), 0o644); err != nil { + t.Fatal(err) + } + } + for name, target := range links { + path := filepath.Join(dir, name) + if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil { + t.Fatal(err) + } + if err := os.Symlink(target, path); err != nil { + t.Fatal(err) + } + } + return dir +} + +func TestReadProcFS(t *testing.T) { + root := t.TempDir() + maps := "55d0-55d1 r--p 00000000 08:01 1234 /usr/bin/python3.12\n" + + "7f00-7f01 r-xp 00000000 08:01 99 /srv/app/.venv/lib/python3.12/site-packages/_cffi.so\n" + + "7f02-7f03 r-xp 00000000 08:01 99 /srv/app/.venv/lib/python3.12/site-packages/_cffi.so\n" + + "7f04-7f05 rw-p 00000000 00:00 0 [heap]\n" + + "7f06-7f07 r--p 00000000 08:01 7 /srv/app/data file.bin (deleted)\n" + fakeProc(t, root, "42", map[string]string{ + "comm": "python3.12\n", + "cmdline": "/srv/app/.venv/bin/python\x00-m\x00app\x00.venv/bin/gunicorn\x00", + "status": "Name:\tpython3.12\nUid:\t4242424\t4242424\t4242424\t4242424\n", + "maps": maps, + }, map[string]string{ + "exe": "/usr/bin/python3.12", + "cwd": "/srv/app", + "root": "/", + "fd/0": "/dev/null", + "fd/3": "socket:[1234]", + "fd/4": "/srv/app/.venv/lib/log.txt (deleted)", + "task/x": "/ignored", + "ns/mnt": "mnt:[1]", + "environ": "/ignored", + }) + fakeProc(t, root, "7", map[string]string{"comm": "self\n"}, map[string]string{"cwd": "/srv/app/.venv"}) + fakeProc(t, root, "9", map[string]string{"cmdline": "exited before comm was read"}, nil) + fakeProc(t, root, "self", map[string]string{"comm": "not a pid\n"}, nil) + // Another user's process: only comm and cmdline are readable. + other := fakeProc(t, root, "50", map[string]string{ + "comm": "gunicorn\n", + "cmdline": "/opt/svc/.venv/bin/gunicorn\x00", + "fd/.keep": "", + }, nil) + if os.Geteuid() != 0 { + if err := os.Chmod(filepath.Join(other, "fd"), 0); err != nil { + t.Fatal(err) + } + t.Cleanup(func() { _ = os.Chmod(filepath.Join(other, "fd"), 0o755) }) + } + + s, err := readProcFS(root, 7) + if err != nil { + t.Fatal(err) + } + want := []Process{ + {PID: 42, Name: "python3.12", User: "4242424", Files: []File{ + {UseRuns, "/usr/bin/python3.12"}, + {UseCwd, "/srv/app"}, + {UseOpen, "/dev/null"}, + {UseOpen, "/srv/app/.venv/lib/log.txt"}, + {UseOpen, "/usr/bin/python3.12"}, + {UseOpen, "/srv/app/.venv/lib/python3.12/site-packages/_cffi.so"}, + {UseOpen, "/srv/app/data file.bin"}, + {UseArgs, "/srv/app/.venv/bin/python"}, + {UseArgs, "/srv/app/.venv/bin/gunicorn"}, + }}, + {PID: 50, Name: "gunicorn", Files: []File{{UseArgs, "/opt/svc/.venv/bin/gunicorn"}}}, + } + if !reflect.DeepEqual(s.Processes, want) { + t.Errorf("processes:\n%+v\nwant\n%+v", s.Processes, want) + } + + if _, err := readProcFS(filepath.Join(root, "missing"), 0); err == nil { + t.Error("a missing /proc did not fail") + } +} + +// TestSystem starts a process that works in a directory +// and checks that the system inspector finds it. +func TestSystem(t *testing.T) { + if runtime.GOOS != "linux" && runtime.GOOS != "darwin" { + t.Skip("no inspector for " + runtime.GOOS) + } + dir := filepath.Join(t.TempDir(), ".venv") + if err := os.Mkdir(dir, 0o755); err != nil { + t.Fatal(err) + } + cmd := exec.Command("sleep", "30") + cmd.Dir = dir + if err := cmd.Start(); err != nil { + t.Fatal(err) + } + t.Cleanup(func() { _ = cmd.Process.Kill(); _ = cmd.Wait() }) + + ctx, cancel := context.WithTimeout(context.Background(), time.Minute) + defer cancel() + s, err := System().Snapshot(ctx) + if err != nil { + t.Fatal(err) + } + ms := s.Find([]string{dir})[dir] + found := false + for _, m := range ms { + if m.PID == cmd.Process.Pid && m.Use == UseCwd { + found = true + } + if m.PID == os.Getpid() { + t.Errorf("the snapshot holds Plonge's own process: %+v", m) + } + } + if !found { + t.Errorf("sleep (PID %d) not found: %+v", cmd.Process.Pid, ms) + } +} diff --git a/internal/procs/system.go b/internal/procs/system.go new file mode 100644 index 0000000..7a9af71 --- /dev/null +++ b/internal/procs/system.go @@ -0,0 +1,321 @@ +package procs + +import ( + "bufio" + "bytes" + "context" + "fmt" + "io" + "os" + "os/exec" + "os/user" + "path/filepath" + "runtime" + "strconv" + "strings" + "time" +) + +// System returns the inspector for this system. +// On Linux, it reads /proc. Elsewhere, it runs lsof and ps. +// It leaves out Plonge's own process. +func System() Inspector { + return system{self: os.Getpid()} +} + +type system struct{ self int } + +func (s system) Snapshot(ctx context.Context) (*Snapshot, error) { + if runtime.GOOS == "linux" { + return readProcFS("/proc", s.self) + } + return runLsofPS(ctx, s.self) +} + +// toolDirs are where systems install lsof and ps, for a PATH that leaves them out. +var toolDirs = []string{"/usr/sbin", "/usr/bin", "/sbin", "/bin"} + +func lookTool(name string) (string, error) { + if p, err := exec.LookPath(name); err == nil { + return p, nil + } + for _, dir := range toolDirs { + if p, err := exec.LookPath(filepath.Join(dir, name)); err == nil { + return p, nil + } + } + return "", fmt.Errorf("%s is not installed", name) +} + +// run runs a tool and returns its output; a nonzero exit is an error. +func run(ctx context.Context, name string, args ...string) ([]byte, error) { + path, err := lookTool(name) + if err != nil { + return nil, err + } + ctx, cancel := context.WithTimeout(ctx, Timeout) + defer cancel() + cmd := exec.CommandContext(ctx, path, args...) + // A child process that keeps the output open must not outlast the timeout. + cmd.WaitDelay = time.Second + var stderr bytes.Buffer + cmd.Stderr = &stderr + out, err := cmd.Output() + if err != nil { + if ctx.Err() == context.DeadlineExceeded { + return nil, fmt.Errorf("%s did not finish in %v", name, Timeout) + } + if msg := lastLine(stderr.String()); msg != "" { + return nil, fmt.Errorf("%s failed: %w: %s", name, err, msg) + } + return nil, fmt.Errorf("%s failed: %w", name, err) + } + return out, nil +} + +func lastLine(s string) string { + s = strings.TrimSpace(s) + if i := strings.LastIndexByte(s, '\n'); i >= 0 { + return s[i+1:] + } + return s +} + +// runLsofPS takes a snapshot with one lsof call and one ps call. +// Without root, lsof lists only the current user's processes; +// ps gives the command lines of all of them. +func runLsofPS(ctx context.Context, self int) (*Snapshot, error) { + // -w leaves out warnings, -n and -P leave out name lookups, + // +c 0 prints whole command names, + // and -F prints the process ID, command, login name, and each file's + // descriptor and name, one field to a line. + lsof, err := run(ctx, "lsof", "-w", "-n", "-P", "+c", "0", "-F", "pcLfn") + if err != nil { + return nil, err + } + ps, err := run(ctx, "ps", "-axww", "-o", "pid=,user=,args=") + if err != nil { + return nil, err + } + return mergePS(parseLsof(bytes.NewReader(lsof)), bytes.NewReader(ps), self), nil +} + +// lsofProcess is a process that lsof listed, with its working directory. +type lsofProcess struct { + Process + cwd string +} + +// parseLsof reads the output of lsof -F pcLfn. +// A cwd descriptor is the working directory, rtd the root directory, +// and the first txt the executable; later txt entries are mapped files. +// Every other descriptor with an absolute name is an open file. +func parseLsof(r io.Reader) []*lsofProcess { + var procs []*lsofProcess + var cur *lsofProcess + fd := "" + ranTxt := false + sc := bufio.NewScanner(r) + sc.Buffer(make([]byte, 64*1024), 1024*1024) + for sc.Scan() { + line := sc.Text() + if line == "" { + continue + } + tag, val := line[0], line[1:] + if tag == 'p' { + pid, err := strconv.Atoi(val) + if err != nil { + cur = nil + continue + } + cur = &lsofProcess{Process: Process{PID: pid}} + procs = append(procs, cur) + fd, ranTxt = "", false + continue + } + if cur == nil { + continue + } + switch tag { + case 'c': + cur.Name = val + case 'L': + cur.User = val + case 'f': + fd = val + case 'n': + if !filepath.IsAbs(val) { + continue + } + name := trimDeleted(val) + use := UseOpen + switch fd { + case "cwd": + use = UseCwd + cur.cwd = name + case "rtd": + use = UseRoot + case "txt": + if !ranTxt { + use, ranTxt = UseRuns, true + } + } + if use == UseRoot && name == "/" { + continue + } + cur.Files = append(cur.Files, File{Use: use, Path: name}) + } + } + return procs +} + +// mergePS adds the command lines from ps -o pid=,user=,args= to procs. +// A process that lsof did not list gets its name from its first word. +// ps joins the words of a command line with spaces, +// so a path with a space in it reads as two words. +func mergePS(procs []*lsofProcess, r io.Reader, self int) *Snapshot { + byPID := map[int]*lsofProcess{} + for _, p := range procs { + byPID[p.PID] = p + } + sc := bufio.NewScanner(r) + sc.Buffer(make([]byte, 64*1024), 1024*1024) + for sc.Scan() { + f := strings.Fields(sc.Text()) + if len(f) < 3 { + continue + } + pid, err := strconv.Atoi(f[0]) + if err != nil { + continue + } + p, ok := byPID[pid] + if !ok { + p = &lsofProcess{Process: Process{PID: pid, Name: filepath.Base(f[2]), User: f[1]}} + byPID[pid] = p + procs = append(procs, p) + } + for _, path := range argPaths(f[2:], p.cwd) { + p.Files = append(p.Files, File{Use: UseArgs, Path: path}) + } + } + s := &Snapshot{} + for _, p := range procs { + if p.PID != self { + s.Processes = append(s.Processes, p.Process) + } + } + return s +} + +// readProcFS reads each process in a Linux /proc tree at root: +// its executable, working and root directories, open files, mapped files, +// command line, name, and user. +// A process that exits during the read, or a file that Plonge may not read, +// such as another user's open files, is left out without an error. +func readProcFS(root string, self int) (*Snapshot, error) { + ents, err := os.ReadDir(root) + if err != nil { + return nil, fmt.Errorf("cannot read %s: %w", root, err) + } + users := map[string]string{} + s := &Snapshot{} + for _, ent := range ents { + pid, err := strconv.Atoi(ent.Name()) + if err != nil || pid == self { + continue + } + dir := filepath.Join(root, ent.Name()) + p := Process{PID: pid} + comm, err := os.ReadFile(filepath.Join(dir, "comm")) + if err != nil { + continue // the process exited + } + p.Name = strings.TrimSpace(string(comm)) + p.User = procUser(dir, users) + link := func(name, use string) string { + target, err := os.Readlink(filepath.Join(dir, name)) + if err != nil || !filepath.IsAbs(target) { + return "" + } + target = trimDeleted(target) + if !(use == UseRoot && target == "/") { + p.Files = append(p.Files, File{Use: use, Path: target}) + } + return target + } + link("exe", UseRuns) + cwd := link("cwd", UseCwd) + link("root", UseRoot) + if fds, err := os.ReadDir(filepath.Join(dir, "fd")); err == nil { + for _, fd := range fds { + link(filepath.Join("fd", fd.Name()), UseOpen) + } + } + for _, path := range mappedFiles(filepath.Join(dir, "maps")) { + p.Files = append(p.Files, File{Use: UseOpen, Path: path}) + } + if cmdline, err := os.ReadFile(filepath.Join(dir, "cmdline")); err == nil { + words := strings.Split(strings.TrimRight(string(cmdline), "\x00"), "\x00") + for _, path := range argPaths(words, cwd) { + p.Files = append(p.Files, File{Use: UseArgs, Path: path}) + } + } + s.Processes = append(s.Processes, p) + } + return s, nil +} + +// mappedFiles returns the distinct files in a /proc//maps file. +func mappedFiles(path string) []string { + data, err := os.ReadFile(path) + if err != nil { + return nil + } + seen := map[string]bool{} + var out []string + for line := range strings.SplitSeq(string(data), "\n") { + // The path is the last field, after the inode; it can hold spaces. + i := strings.Index(line, " /") + if i < 0 { + continue + } + name := trimDeleted(strings.TrimSpace(line[i+1:])) + if !seen[name] { + seen[name] = true + out = append(out, name) + } + } + return out +} + +// procUser returns the name of the user who owns the process in dir, +// or the user ID when it has no name. +func procUser(dir string, cache map[string]string) string { + data, err := os.ReadFile(filepath.Join(dir, "status")) + if err != nil { + return "" + } + for line := range strings.SplitSeq(string(data), "\n") { + rest, ok := strings.CutPrefix(line, "Uid:") + if !ok { + continue + } + f := strings.Fields(rest) + if len(f) == 0 { + return "" + } + uid := f[0] + if name, ok := cache[uid]; ok { + return name + } + name := uid + if u, err := user.LookupId(uid); err == nil { + name = u.Username + } + cache[uid] = name + return name + } + return "" +} diff --git a/internal/report/report.go b/internal/report/report.go index 30c27e1..3c684aa 100644 --- a/internal/report/report.go +++ b/internal/report/report.go @@ -51,7 +51,8 @@ type Report struct { SkippedRoots []SkippedRoot `json:"skipped_roots"` BrokenLinks int `json:"broken_links"` Errors []string `json:"errors"` - // Refused lists the caches that are or hold a Git repository. + // Refused lists the caches that a running process uses + // or that are or hold a Git repository. Refused []scan.Refusal `json:"refused"` // ActivitySources reports each source of agent activity. ActivitySources []scan.Source `json:"activity_sources"` @@ -196,13 +197,18 @@ func writeSources(w io.Writer, sources []scan.Source) { fmt.Fprintf(w, "Activity source skipped: %s (%s)\n", s.Name, s.Reason) case scan.SourceFailed: fmt.Fprintf(w, "Activity source failed: %s (%s)\n", s.Name, s.Reason) - fmt.Fprintf(w, "Every project counts as busy. Fix %s, or remove it from PATH for this run.\n", s.Name) + if s.Name == scan.SourceProcesses { + fmt.Fprintln(w, "Every project counts as busy until Plonge can list the running processes.") + } else { + fmt.Fprintf(w, "Every project counts as busy. Fix %s, or remove it from PATH for this run.\n", s.Name) + } } } } // WriteRefusals lists the caches, or the folders of a global rule, -// that plonge does not report or delete because they are or hold a Git repository. +// that plonge does not report or delete, such as because a running process uses them +// or they are or hold a Git repository. func WriteRefusals(w io.Writer, refused []scan.Refusal, home string) { for _, r := range refused { fmt.Fprintf(w, "Refused %s: %s\n", Tilde(r.Path, home), r.Reason) @@ -339,7 +345,7 @@ func WriteClean(w io.Writer, r clean.Result, home string) error { } n := r.NotSelected if n.Busy.Caches > 0 { - fmt.Fprintf(w, "Not selected, busy project (Paseo): %s in %s\n", + fmt.Fprintf(w, "Not selected, busy project: %s in %s\n", HumanBytes(n.Busy.Bytes), plural(n.Busy.Caches, "cache")) } if n.Active.Caches > 0 { diff --git a/internal/report/report_test.go b/internal/report/report_test.go index 06a39aa..22f2960 100644 --- a/internal/report/report_test.go +++ b/internal/report/report_test.go @@ -3,6 +3,7 @@ package report import ( "bytes" "encoding/json" + "reflect" "strings" "testing" "time" @@ -10,6 +11,7 @@ import ( "github.com/mattt/plonge/internal/clean" "github.com/mattt/plonge/internal/disk" "github.com/mattt/plonge/internal/global" + "github.com/mattt/plonge/internal/procs" "github.com/mattt/plonge/internal/scan" ) @@ -229,7 +231,7 @@ func TestWriteGlobalRefusedFolders(t *testing.T) { if err := json.Unmarshal(buf.Bytes(), &got); err != nil { t.Fatal(err) } - if len(got.Outcomes) != 1 || len(got.Outcomes[0].RefusedFolders) != 1 || got.Outcomes[0].RefusedFolders[0] != e.RefusedFolders[0] { + if len(got.Outcomes) != 1 || len(got.Outcomes[0].RefusedFolders) != 1 || !reflect.DeepEqual(got.Outcomes[0].RefusedFolders[0], e.RefusedFolders[0]) { t.Errorf("JSON = %s", buf.String()) } } @@ -325,3 +327,49 @@ func TestWriteFreedReportsHomeVolume(t *testing.T) { } } } + +// TestWriteCleanProcesses reports a cache that a process uses, +// and a failed process check, in text and JSON. +func TestWriteCleanProcesses(t *testing.T) { + res := clean.Result{ + Roots: 1, + NotSelected: clean.NotSelected{Busy: clean.Group{Caches: 1, Bytes: 2048}}, + Refused: []scan.Refusal{{ + Path: "/home/u/code/app/.venv", Project: "/home/u/code/app", Rule: ".venv", + Reason: "in use by python3.12 (PID 4312), whose command line names bin/python", + Processes: []procs.Match{{PID: 4312, Name: "python3.12", User: "u", Use: procs.UseArgs, Path: "/home/u/code/app/.venv/bin/python"}}, + }}, + ActivitySources: []scan.Source{{Name: scan.SourceProcesses, Status: scan.SourceFailed, Reason: "lsof is not installed"}}, + } + var buf bytes.Buffer + if err := WriteClean(&buf, res, "/home/u"); err != nil { + t.Fatal(err) + } + for _, want := range []string{ + "Not selected, busy project: 2.0 KiB in 1 cache\n", + "Refused ~/code/app/.venv: in use by python3.12 (PID 4312), whose command line names bin/python\n", + "Activity source failed: processes (lsof is not installed)\n", + "Every project counts as busy until Plonge can list the running processes.\n", + } { + if !strings.Contains(buf.String(), want) { + t.Errorf("clean output lacks %q:\n%s", want, buf.String()) + } + } + + buf.Reset() + if err := WriteJSON(&buf, res); err != nil { + t.Fatal(err) + } + var got struct { + Refused []map[string]any `json:"refused"` + } + if err := json.Unmarshal(buf.Bytes(), &got); err != nil { + t.Fatal(err) + } + want := []any{map[string]any{ + "pid": float64(4312), "name": "python3.12", "user": "u", "use": "args", "path": "/home/u/code/app/.venv/bin/python", + }} + if len(got.Refused) != 1 || !reflect.DeepEqual(got.Refused[0]["processes"], want) { + t.Errorf("JSON refused = %v", got.Refused) + } +} diff --git a/internal/scan/detect.go b/internal/scan/detect.go index 00088b5..4b4236e 100644 --- a/internal/scan/detect.go +++ b/internal/scan/detect.go @@ -11,6 +11,7 @@ import ( "strings" "time" + "github.com/mattt/plonge/internal/procs" "github.com/mattt/plonge/internal/rules" ) @@ -31,6 +32,9 @@ type Refusal struct { Project string `json:"project"` Rule string `json:"rule"` Reason string `json:"reason"` + // Processes lists the processes that use the cache, + // when that is why plonge refused it. + Processes []procs.Match `json:"processes,omitempty"` } // SkippedRoot is a root that plonge did not scan, and why. @@ -45,7 +49,9 @@ type Root struct { Skipped string // why the root was skipped, or empty Candidates []Candidate Errors []string // Git failures; the candidates they affect are left out - // Refused lists the candidates that Measure left out + // Refused lists the candidates that RefuseUsed left out + // because a running process uses them, + // and the ones that Measure left out // because they are or hold a Git repository, // or hold an entry at a refuse_inside path. Refused []Refusal @@ -54,7 +60,8 @@ type Root struct { LastActivity time.Time // ActivitySource names the source that set LastActivity. ActivitySource string - // Busy is true when a coding agent is working in the root right now. + // Busy is true when a coding agent is working in the root right now, + // or when an activity source failed. Busy bool } diff --git a/internal/scan/processes.go b/internal/scan/processes.go new file mode 100644 index 0000000..68fb208 --- /dev/null +++ b/internal/scan/processes.go @@ -0,0 +1,49 @@ +package scan + +import ( + "context" + + "github.com/mattt/plonge/internal/procs" +) + +// SourceProcesses is the activity source for running processes. +const SourceProcesses = "processes" + +// RefuseUsed moves each candidate that a running process uses +// from Candidates to Refused, with the processes in the refusal. +// When the inspector fails, every root that was scanned is busy, +// because a process might use any cache. +func RefuseUsed(ctx context.Context, roots []Root, insp procs.Inspector) Source { + snap, err := insp.Snapshot(ctx) + if err != nil { + for i := range roots { + if roots[i].Skipped == "" { + roots[i].Busy = true + } + } + return Source{Name: SourceProcesses, Status: SourceFailed, Reason: err.Error()} + } + var paths []string + for _, r := range roots { + for _, c := range r.Candidates { + paths = append(paths, c.Path) + } + } + used := snap.Find(paths) + for i := range roots { + r := &roots[i] + var kept []Candidate + for _, c := range r.Candidates { + if ms := used[c.Path]; len(ms) > 0 { + r.Refused = append(r.Refused, Refusal{ + Path: c.Path, Project: r.Path, Rule: c.Rule, + Reason: procs.Reason(c.Path, ms), Processes: ms, + }) + continue + } + kept = append(kept, c) + } + r.Candidates = kept + } + return Source{Name: SourceProcesses, Status: SourceUsed} +} diff --git a/internal/scan/processes_test.go b/internal/scan/processes_test.go new file mode 100644 index 0000000..ee605e2 --- /dev/null +++ b/internal/scan/processes_test.go @@ -0,0 +1,66 @@ +package scan + +import ( + "context" + "errors" + "reflect" + "testing" + + "github.com/mattt/plonge/internal/procs" +) + +// fakeInspector returns a fixed snapshot or error and counts its calls. +type fakeInspector struct { + snap *procs.Snapshot + err error + calls int +} + +func (f *fakeInspector) Snapshot(context.Context) (*procs.Snapshot, error) { + f.calls++ + return f.snap, f.err +} + +func TestRefuseUsed(t *testing.T) { + roots := []Root{ + {Path: "/code/app", Candidates: []Candidate{ + {Path: "/code/app/.venv", Rule: ".venv"}, + {Path: "/code/app/.venv2", Rule: ".venv"}, + }}, + {Path: "/code/gone", Skipped: "not a directory"}, + } + insp := &fakeInspector{snap: &procs.Snapshot{Processes: []procs.Process{ + {PID: 4312, Name: "python3.12", User: "u", Files: []procs.File{ + {Use: procs.UseArgs, Path: "/code/app/.venv/bin/python"}, + }}, + }}} + src := RefuseUsed(context.Background(), roots, insp) + if src != (Source{Name: SourceProcesses, Status: SourceUsed}) || insp.calls != 1 { + t.Errorf("source = %+v, %d calls", src, insp.calls) + } + want := []Refusal{{ + Path: "/code/app/.venv", Project: "/code/app", Rule: ".venv", + Reason: "in use by python3.12 (PID 4312), whose command line names bin/python", + Processes: []procs.Match{{PID: 4312, Name: "python3.12", User: "u", Use: procs.UseArgs, Path: "/code/app/.venv/bin/python"}}, + }} + if !reflect.DeepEqual(roots[0].Refused, want) { + t.Errorf("refused = %+v", roots[0].Refused) + } + if len(roots[0].Candidates) != 1 || roots[0].Candidates[0].Path != "/code/app/.venv2" || roots[0].Busy { + t.Errorf("root = %+v", roots[0]) + } +} + +func TestRefuseUsedFailsClosed(t *testing.T) { + roots := []Root{ + {Path: "/code/app", Candidates: []Candidate{{Path: "/code/app/.venv"}}}, + {Path: "/code/gone", Skipped: "not a directory"}, + } + src := RefuseUsed(context.Background(), roots, &fakeInspector{err: errors.New("lsof is not installed")}) + if src != (Source{Name: SourceProcesses, Status: SourceFailed, Reason: "lsof is not installed"}) { + t.Errorf("source = %+v", src) + } + if !roots[0].Busy || roots[1].Busy || len(roots[0].Candidates) != 1 { + t.Errorf("roots = %+v", roots) + } +} diff --git a/skills/plonge/SKILL.md b/skills/plonge/SKILL.md index 5253906..8b607d8 100644 --- a/skills/plonge/SKILL.md +++ b/skills/plonge/SKILL.md @@ -32,6 +32,8 @@ but MCP does not support `--root` or `--no-mise`. (24 hours by default). An active project has more recent or unknown activity. A busy project has a running or initializing Paseo agent. + Plonge refuses a project cache that a running process uses + and names the process in the report. Project cleanup excludes busy projects and, by default, active projects. Say which global rules are `prune` and which are `clear`. A `prune` rule uses the tool's policy to select unused or stale data. @@ -80,6 +82,9 @@ and Paseo status, not on uncommitted edits or running builds. If `paseo` is not on PATH, the busy check cannot see Paseo agents and gives no protection. If `paseo` is on PATH but its status cannot be read, every project counts as busy, so `clean` deletes no project caches until Paseo works again. +The running-process check sees only processes that run when Plonge checks; +it does not see a stopped service or a later cron job. +If Plonge cannot list the processes, every project counts as busy. Do not clean caches in a project that you know is busy. Do not use another deletion command to bypass a skipped path or rule.