From 1c84ff648c1ef6633ae5a1d1c5c5f17dfaa41750 Mon Sep 17 00:00:00 2001 From: Muse Sweep Date: Sun, 18 Oct 2026 14:39:46 -0500 Subject: [PATCH] fix(mcpx): honour MCPX_STATE_DIR in daemon discovery and mirroring (#305) Port of dezren39/nix PR #410 (fix for dezren39/nix#407) to this repo's pkgs/mcpx copy: DaemonSearchDirs now also searches the MCPX_STATE_DIR directory, and a daemon whose state directory is the MCPX_STATE_DIR directory no longer mirrors its record into the real XDG default directory. Adds the two regression tests from that PR, adapted to this repo's issue number; both failed on unmodified main (missing env dir in search dirs; env-default daemon leaked a mirror into the default dir) and pass with the fix. Full internal/daemon and internal/cli suites green. --- pkgs/mcpx/internal/daemon/paths.go | 38 +++++++++++- pkgs/mcpx/internal/daemon/paths_test.go | 81 +++++++++++++++++++++++++ 2 files changed, 116 insertions(+), 3 deletions(-) diff --git a/pkgs/mcpx/internal/daemon/paths.go b/pkgs/mcpx/internal/daemon/paths.go index 5107bc2985..c81945ca1b 100644 --- a/pkgs/mcpx/internal/daemon/paths.go +++ b/pkgs/mcpx/internal/daemon/paths.go @@ -240,15 +240,40 @@ func DefaultStateDir() string { ) } +// envStateDir is the state directory MCPX_STATE_DIR names, or "" when it +// is not set. ResolvePaths honours it before the XDG location, so a daemon +// whose state directory is this one is a default daemon -- the plain +// invocation's daemon -- not a custom-state one (#305). +func envStateDir() string { + return os.Getenv("MCPX_STATE_DIR") +} + // DaemonSearchDirs is where `mcpx daemons` looks for daemon records: the // caller's own state directory, plus the default one. A daemon started // with its own state directory mirrors its record into the default // directory (see WriteInfo), so the second entry is what makes the listing // cover the whole machine rather than only the caller's directory. +// +// The MCPX_STATE_DIR directory is searched too (#305): it is where a plain +// invocation's daemon lives when the variable is set, and a caller whose +// own state came from a config file or a flag would otherwise never see +// that daemon, mirror or no mirror. func DaemonSearchDirs(primary string) []string { out := []string{primary} - if d := DefaultStateDir(); d != "" && d != primary { - out = append(out, d) + for _, d := range []string{DefaultStateDir(), envStateDir()} { + if d == "" { + continue + } + seen := false + for _, have := range out { + if have == d { + seen = true + break + } + } + if !seen { + out = append(out, d) + } } return out } @@ -304,12 +329,19 @@ func (p Paths) PruneStale() []Info { // publishes its record: the default state directory, under a name keyed // by its own directory so two custom directories cannot collide and a // custom record cannot shadow the default directory's own daemon for the -// same configuration. Empty when this directory is the default one. +// same configuration. Empty when this directory is the default one -- +// the XDG location, or the MCPX_STATE_DIR location a plain invocation +// resolves to (#305). Mirroring the env-default daemon would leak a +// sandboxed daemon's record into the real default directory, where a +// non-sandboxed listing could see it and stop it. func (p Paths) mirrorInfoPath() string { def := DefaultStateDir() if def == "" || p.State == "" || p.State == def || p.Info == "" { return "" } + if env := envStateDir(); env != "" && p.State == env { + return "" + } sum := sha256.Sum256([]byte(p.State)) return filepath.Join(def, "daemon-x"+hex.EncodeToString(sum[:])[:12]+"-"+filepath.Base(p.Info)) } diff --git a/pkgs/mcpx/internal/daemon/paths_test.go b/pkgs/mcpx/internal/daemon/paths_test.go index 5aeedab61b..94b97acb69 100644 --- a/pkgs/mcpx/internal/daemon/paths_test.go +++ b/pkgs/mcpx/internal/daemon/paths_test.go @@ -250,6 +250,7 @@ func TestPruneStaleRemovesRecordsWhoseProcessIsGone(t *testing.T) { func TestWriteInfoMirrorsIntoTheDefaultStateDir(t *testing.T) { xdg := t.TempDir() t.Setenv("XDG_STATE_HOME", xdg) + t.Setenv("MCPX_STATE_DIR", "") def := daemon.DefaultStateDir() if def != filepath.Join(xdg, "mcpx") { t.Fatalf("DefaultStateDir = %q, want the XDG location", def) @@ -283,3 +284,83 @@ func TestWriteInfoMirrorsIntoTheDefaultStateDir(t *testing.T) { t.Errorf("mirror should be removed too, still listed: %+v", remaining) } } + +// #305: MCPX_STATE_DIR is the first override ResolvePaths honours, so the +// daemon a plain invocation talks to lives there -- but DaemonSearchDirs +// only ever searched the caller's directory and the XDG default, so a +// listing from a custom state directory missed it entirely. +func TestDaemonSearchDirsIncludesTheEnvStateDir(t *testing.T) { + xdg := t.TempDir() + t.Setenv("XDG_STATE_HOME", xdg) + envDir := t.TempDir() + t.Setenv("MCPX_STATE_DIR", envDir) + def := daemon.DefaultStateDir() + custom := t.TempDir() + + dirs := daemon.DaemonSearchDirs(custom) + want := map[string]bool{custom: true, def: true, envDir: true} + if len(dirs) != len(want) { + t.Fatalf("DaemonSearchDirs(custom) = %v, want exactly %v", dirs, want) + } + for _, d := range dirs { + if !want[d] { + t.Errorf("DaemonSearchDirs(custom) has unexpected %q in %v", d, dirs) + } + delete(want, d) + } + if len(want) != 0 { + t.Errorf("DaemonSearchDirs(custom) = %v, missing %v", dirs, want) + } + // The env directory as the primary is not duplicated. + if dirs := daemon.DaemonSearchDirs(envDir); len(dirs) != 2 { + t.Errorf("DaemonSearchDirs(env) = %v, want [env default] with no duplicate", dirs) + } +} + +// #305: a daemon whose state directory is MCPX_STATE_DIR is the default +// daemon, not a custom-state one, so it must not mirror its record into +// the XDG default directory -- that leak made a sandboxed daemon visible +// to (and stoppable by) a non-sandboxed `mcpx daemons` / `stop --all`. +// A genuinely custom directory still mirrors, env set or not. +func TestWriteInfoDoesNotMirrorTheEnvDefaultDaemon(t *testing.T) { + xdg := t.TempDir() + t.Setenv("XDG_STATE_HOME", xdg) + envDir := t.TempDir() + t.Setenv("MCPX_STATE_DIR", envDir) + def := daemon.DefaultStateDir() + + p := daemon.Paths{State: envDir, Info: filepath.Join(envDir, "daemon.json")} + info := daemon.Info{ + PID: os.Getpid(), Socket: filepath.Join(envDir, "daemon.sock"), + ConfigPath: "/proj/.mcpx.json", + } + if err := p.WriteInfo(info); err != nil { + t.Fatal(err) + } + envPaths := daemon.Paths{State: envDir} + if got := envPaths.ListDaemons(); len(got) != 1 { + t.Fatalf("env dir listing = %+v, want the primary record", got) + } + defPaths := daemon.Paths{State: def} + if got := defPaths.ListDaemons(); len(got) != 0 { + t.Fatalf("env-default daemon leaked a mirror into the default dir: %+v", got) + } + p.RemoveInfo() + if _, err := os.Stat(p.Info); !os.IsNotExist(err) { + t.Errorf("primary record should be removed: %v", err) + } + + custom := t.TempDir() + cp := daemon.Paths{State: custom, Info: filepath.Join(custom, "daemon-abc.json")} + cinfo := daemon.Info{ + PID: os.Getpid(), Socket: filepath.Join(custom, "daemon-abc.sock"), + ConfigPath: "/other/.mcpx.json", + } + if err := cp.WriteInfo(cinfo); err != nil { + t.Fatal(err) + } + if got := defPaths.ListDaemons(); len(got) != 1 || got[0].Socket != cinfo.Socket { + t.Errorf("custom daemon should still mirror with MCPX_STATE_DIR set, default listing = %+v", got) + } + cp.RemoveInfo() +}