Skip to content

mission control for agent skills

fix: ignore Darwin system path aliases

Alvin Unrealb3f61c850c77
diff --git a/internal/agents/agents.go b/internal/agents/agents.go
index d033df3..3ab164c 100644
--- a/internal/agents/agents.go
+++ b/internal/agents/agents.go
@@ -4,6 +4,7 @@ import (
"fmt"
"os"
"path/filepath"
+ "runtime"
"sort"
"strings"
@@ -347,9 +348,11 @@ type sharedRootResult struct {
// component. anchor is cwd for project-scope locations and the user's home
// directory for global-scope locations.
//
-// When root sits under anchor, each path component of root strictly below
-// anchor is Lstat-ed in turn; the first symlinked component means everything
-// under it (including root) is shared with wherever the link resolves to.
+// When root sits under a non-empty anchor, each path component of root
+// strictly below anchor is Lstat-ed in turn; the first symlinked component
+// means everything under it (including root) is shared with wherever the
+// link resolves to. Restricting the walk to below anchor ignores system
+// symlinks above the scope boundary when home itself lives under them.
//
// When root is NOT under anchor (e.g. CODEX_HOME=/alias pointing outside the
// home directory), every path component needed to reach root is walked from
@@ -357,10 +360,15 @@ type sharedRootResult struct {
// detected even when /alias/skills itself is an ordinary directory. A normal
// non-symlink external root remains usable.
//
-// A missing component after an earlier symlink is never reached: the walk
-// returns shared at the first symlink. A missing component with no prior
-// symlink means the path does not exist yet and is reported not-shared.
-// Does not use EvalSymlinks.
+// On Darwin only, the exact volume-root OS aliases /var -> /private/var,
+// /tmp -> /private/tmp, and /etc -> /private/etc are skipped while walking
+// so macOS temp paths under /var/folders are not false-positive shared roots.
+// No basename heuristic and no user-created link is trusted.
+//
+// A missing component after an earlier (non-skipped) symlink is never
+// reached: the walk returns shared at the first such symlink. A missing
+// component with no prior shared symlink means the path does not exist yet
+// and is reported not-shared. Does not use EvalSymlinks.
func sharedRootInfo(root, anchor string) (link, target string, shared bool) {
root = filepath.Clean(root)
anchor = filepath.Clean(anchor)
@@ -374,8 +382,8 @@ func sharedRootInfo(root, anchor string) (link, target string, shared bool) {
start := pathWalkStart(root)
if pathInside(root, anchor) && anchor != "" && anchor != "." {
// Restrict the walk to components strictly below anchor so system
- // symlinks above the scope boundary (e.g. macOS /var -> /private/var
- // when home lives under /var/...) are not false-positives.
+ // symlinks above the scope boundary are not false-positives when the
+ // anchor itself lives under them (e.g. home under /var/folders).
start = anchor
}
if root == start {
@@ -391,9 +399,10 @@ func sharedRootInfo(root, anchor string) (link, target string, shared bool) {
}
// walkSymlinkComponents Lstats each path component from start down to root
-// (exclusive of start, inclusive of root). Returns at the first symlink.
-// Missing components (ErrNotExist) with no earlier symlink mean the path is
-// not a live shared root. Any other inspection error fails closed as shared.
+// (exclusive of start, inclusive of root). Returns at the first symlink that
+// is not a Darwin system path alias. Missing components (ErrNotExist) with
+// no earlier shared symlink mean the path is not a live shared root. Any
+// other inspection error fails closed as shared.
func walkSymlinkComponents(root, start string) (link, target string, shared bool) {
root = filepath.Clean(root)
start = filepath.Clean(start)
@@ -421,7 +430,12 @@ func walkSymlinkComponents(root, start string) (link, target string, shared bool
}
if info.Mode()&os.ModeSymlink != 0 {
// Reuse the Lstat result; only Readlink is needed for the target.
- return readlinkDetails(current)
+ link, target, shared = readlinkDetails(current)
+ if shared && isDarwinSystemPathAlias(link, target) {
+ // Exact Darwin OS prefix alias: keep walking.
+ continue
+ }
+ return link, target, shared
}
}
return "", "", false
@@ -444,6 +458,31 @@ func pathWalkStart(path string) string {
return ""
}
+// darwinSystemPathAliases is the exact Darwin volume-root symlink set that
+// rewrites legacy paths to /private/... Without skipping these, every walk
+// from "/" through /var/folders temp dirs false-positives as shared.
+// Keys and values are filepath.Clean absolute paths only.
+var darwinSystemPathAliases = map[string]string{
+ "/var": "/private/var",
+ "/tmp": "/private/tmp",
+ "/etc": "/private/etc",
+}
+
+// isDarwinSystemPathAlias reports whether link -> target is one of the exact
+// Darwin OS volume-root path aliases. Non-Darwin platforms always return
+// false. Same-basename user links (e.g. /alias -> /somewhere/alias) and
+// near-miss targets are never trusted.
+func isDarwinSystemPathAlias(link, target string) bool {
+ if runtime.GOOS != "darwin" {
+ return false
+ }
+ if link == "" || target == "" {
+ return false
+ }
+ want, ok := darwinSystemPathAliases[filepath.Clean(link)]
+ return ok && want == filepath.Clean(target)
+}
+
// pathInside reports whether path is equal to root or a descendant of root
// using filepath-aware containment (not raw string-prefix matching), so
// siblings like root+"-foo" are not treated as inside root.
diff --git a/internal/agents/shared_root_test.go b/internal/agents/shared_root_test.go
index 92dfea0..30d5183 100644
--- a/internal/agents/shared_root_test.go
+++ b/internal/agents/shared_root_test.go
@@ -3,6 +3,7 @@ package agents
import (
"os"
"path/filepath"
+ "runtime"
"testing"
"github.com/alvinunreal/lazyskills/internal/model"
@@ -90,6 +91,187 @@ func TestSharedRootInfoOutsideAnchorRealDirectoryNotShared(t *testing.T) {
}
}
+// TestSharedRootInfoIgnoresDarwinSystemVarAlias covers the macOS release-
+// validation failure mode: walking an external root under /var/folders from
+// the volume root must not treat the OS /var -> /private/var alias as shared.
+func TestSharedRootInfoIgnoresDarwinSystemVarAlias(t *testing.T) {
+ if runtime.GOOS != "darwin" {
+ t.Skip("Darwin system path aliases only apply on macOS")
+ }
+ info, err := os.Lstat("/var")
+ if err != nil || info.Mode()&os.ModeSymlink == 0 {
+ t.Skip("/var is not a symlink on this system")
+ }
+ external := t.TempDir()
+ if !pathInside(external, "/var") {
+ t.Skip("temp dir is not under /var; system-alias case does not apply")
+ }
+ realRoot := filepath.Join(external, "codex", "skills")
+ if err := os.MkdirAll(realRoot, 0o755); err != nil {
+ t.Fatal(err)
+ }
+ // Anchor on another top-level hierarchy so the walk starts at "/".
+ anchor := filepath.Join(string(filepath.Separator), "Users", "NonexistentLazySkillsTestHome")
+ if pathInside(realRoot, anchor) {
+ t.Fatalf("precondition: root must be outside anchor, root=%q anchor=%q", realRoot, anchor)
+ }
+ if link, target, shared := sharedRootInfo(realRoot, anchor); shared {
+ t.Fatalf("expected Darwin /var system alias not to flag real external root as shared, link=%q target=%q", link, target)
+ }
+}
+
+// TestDarwinSystemPathAliasWhitelist is a portable unit check of the exact
+// Darwin allow-list: only the three OS aliases match, and only on Darwin.
+func TestDarwinSystemPathAliasWhitelist(t *testing.T) {
+ cases := []struct {
+ link, target string
+ wantDarwin bool
+ }{
+ {"/var", "/private/var", true},
+ {"/tmp", "/private/tmp", true},
+ {"/etc", "/private/etc", true},
+ // Same-basename user link must never be trusted.
+ {"/alias", "/somewhere/alias", false},
+ {"/var", "/private/var-evil", false},
+ {"/var", "/private/tmp", false},
+ {"/var/folders", "/private/var/folders", false},
+ {"/tmp/foo", "/private/tmp/foo", false},
+ {"/etc", "/private/etx", false},
+ {"", "/private/var", false},
+ {"/var", "", false},
+ }
+ for _, tc := range cases {
+ got := isDarwinSystemPathAlias(tc.link, tc.target)
+ want := tc.wantDarwin && runtime.GOOS == "darwin"
+ if got != want {
+ t.Fatalf("isDarwinSystemPathAlias(%q, %q) = %v, want %v (GOOS=%s)",
+ tc.link, tc.target, got, want, runtime.GOOS)
+ }
+ }
+}
+
+// TestSharedRootInfoDetectsSameBasenameAncestorSymlink ensures a user-created
+// alias -> somewhere/alias redirect (same basename) remains shared. The old
+// basename heuristic would have skipped this unsafely.
+func TestSharedRootInfoDetectsSameBasenameAncestorSymlink(t *testing.T) {
+ home := t.TempDir()
+ external := t.TempDir()
+
+ targetHome := filepath.Join(external, "somewhere", "alias")
+ targetSkills := filepath.Join(targetHome, "skills")
+ if err := os.MkdirAll(targetSkills, 0o755); err != nil {
+ t.Fatal(err)
+ }
+ alias := filepath.Join(external, "alias")
+ if err := os.Symlink(targetHome, alias); err != nil {
+ t.Fatal(err)
+ }
+ root := filepath.Join(alias, "skills")
+ link, gotTarget, shared := sharedRootInfo(root, home)
+ if !shared {
+ t.Fatal("expected same-basename ancestor symlink to be flagged as shared")
+ }
+ if link != alias {
+ t.Fatalf("expected link %q, got %q", alias, link)
+ }
+ if gotTarget != targetHome {
+ t.Fatalf("expected target %q, got %q", targetHome, gotTarget)
+ }
+}
+
+// TestSharedRootInfoDetectsNearMissSystemAliasShape ensures a user-created
+// symlink that merely resembles a Darwin OS alias (var -> private/var under a
+// temp tree) is still treated as shared.
+func TestSharedRootInfoDetectsNearMissSystemAliasShape(t *testing.T) {
+ home := t.TempDir()
+ external := t.TempDir()
+
+ privateVar := filepath.Join(external, "private", "var")
+ if err := os.MkdirAll(filepath.Join(privateVar, "skills"), 0o755); err != nil {
+ t.Fatal(err)
+ }
+ fakeVar := filepath.Join(external, "var")
+ if err := os.Symlink(privateVar, fakeVar); err != nil {
+ t.Fatal(err)
+ }
+ root := filepath.Join(fakeVar, "skills")
+ link, gotTarget, shared := sharedRootInfo(root, home)
+ if !shared {
+ t.Fatal("expected near-miss var -> private/var user symlink to be flagged as shared")
+ }
+ if link != fakeVar {
+ t.Fatalf("expected link %q, got %q", fakeVar, link)
+ }
+ if gotTarget != privateVar {
+ t.Fatalf("expected target %q, got %q", privateVar, gotTarget)
+ }
+}
+
+// TestSharedRootInfoDetectsDeepSameBasenameSymlink ensures deeper same-basename
+// redirects (skills -> ../other/skills) remain shared under the anchor walk.
+func TestSharedRootInfoDetectsDeepSameBasenameSymlink(t *testing.T) {
+ home := t.TempDir()
+ other := filepath.Join(home, "other", "skills")
+ if err := os.MkdirAll(other, 0o755); err != nil {
+ t.Fatal(err)
+ }
+ codexHome := filepath.Join(home, ".codex")
+ if err := os.MkdirAll(codexHome, 0o755); err != nil {
+ t.Fatal(err)
+ }
+ linked := filepath.Join(codexHome, "skills")
+ if err := os.Symlink(other, linked); err != nil {
+ t.Fatal(err)
+ }
+ link, target, shared := sharedRootInfo(linked, home)
+ if !shared {
+ t.Fatal("expected deep same-basename symlink to be flagged as shared")
+ }
+ if link != linked {
+ t.Fatalf("expected link %q, got %q", linked, link)
+ }
+ if target != other {
+ t.Fatalf("expected target %q, got %q", other, target)
+ }
+}
+
+// TestCheckDestructivePathRefusesOutsideAnchorAncestorSymlink is the end-to-
+// end guard: when a skills root sits outside anchor beneath an ancestor
+// symlink, live destructive validation must refuse. Walking only below a
+// common prefix with anchor would miss ancestor redirects on the root side.
+func TestCheckDestructivePathRefusesOutsideAnchorAncestorSymlink(t *testing.T) {
+ home := t.TempDir()
+ cwd := t.TempDir()
+ external := t.TempDir()
+
+ canonical := filepath.Join(external, "canonical-home")
+ if err := os.MkdirAll(filepath.Join(canonical, "skills", "demo"), 0o755); err != nil {
+ t.Fatal(err)
+ }
+ alias := filepath.Join(external, "alias")
+ if err := os.Symlink(canonical, alias); err != nil {
+ t.Fatal(err)
+ }
+ // skills is a real directory reached only through the ancestor alias.
+ skillPath := filepath.Join(alias, "skills", "demo")
+ info, err := os.Lstat(filepath.Join(alias, "skills"))
+ if err != nil {
+ t.Fatal(err)
+ }
+ if info.Mode()&os.ModeSymlink != 0 {
+ t.Fatal("precondition: skills dir itself must not be a symlink")
+ }
+
+ e := Env{
+ Home: home,
+ Vars: map[string]string{"CODEX_HOME": alias},
+ ExistsFunc: pathExists,
+ }
+ if err := CheckDestructivePathWithEnv(skillPath, cwd, e); err == nil {
+ t.Fatal("expected destructive check to refuse path under outside-anchor ancestor symlink")
+ }
+}
+
func TestSharedRootInfoOutsideAnchorDetectsRootSymlink(t *testing.T) {
home := t.TempDir()
external := t.TempDir()