diff --git a/internal/sandbox/learning.go b/internal/sandbox/learning.go index 61678d7..18b15e9 100644 --- a/internal/sandbox/learning.go +++ b/internal/sandbox/learning.go @@ -220,9 +220,15 @@ func CollapsePaths(paths []string) []string { } } - // For standalone paths, use their parent directory + // For standalone paths, use their parent directory — but never collapse to $HOME for _, p := range standalone { - result = append(result, filepath.Dir(p)) + parent := filepath.Dir(p) + if parent == home { + // Keep exact file path to avoid opening entire home directory + result = append(result, p) + } else { + result = append(result, parent) + } } // Sort and deduplicate (remove sub-paths of other paths) diff --git a/internal/sandbox/learning_test.go b/internal/sandbox/learning_test.go index 1f5d3a3..90deed4 100644 --- a/internal/sandbox/learning_test.go +++ b/internal/sandbox/learning_test.go @@ -75,9 +75,10 @@ func TestCollapsePaths(t *testing.T) { defer os.Setenv("HOME", origHome) tests := []struct { - name string - paths []string - contains []string // paths that should be in the result + name string + paths []string + contains []string // paths that should be in the result + notContains []string // paths that must NOT be in the result }{ { name: "multiple paths under same app dir", @@ -111,6 +112,33 @@ func TestCollapsePaths(t *testing.T) { "/home/testuser/.config/opencode", }, }, + { + name: "files directly under home stay as exact paths", + paths: []string{ + "/home/testuser/.gitignore", + "/home/testuser/.npmrc", + }, + contains: []string{ + "/home/testuser/.gitignore", + "/home/testuser/.npmrc", + }, + notContains: []string{"/home/testuser"}, + }, + { + name: "mix of home files and app dir paths", + paths: []string{ + "/home/testuser/.gitignore", + "/home/testuser/.cache/opencode/db/main.sqlite", + "/home/testuser/.cache/opencode/version", + "/home/testuser/.npmrc", + }, + contains: []string{ + "/home/testuser/.gitignore", + "/home/testuser/.npmrc", + "/home/testuser/.cache/opencode", + }, + notContains: []string{"/home/testuser"}, + }, } for _, tt := range tests { @@ -134,6 +162,13 @@ func TestCollapsePaths(t *testing.T) { t.Errorf("CollapsePaths() = %v, missing expected path %q", got, want) } } + for _, bad := range tt.notContains { + for _, g := range got { + if g == bad { + t.Errorf("CollapsePaths() = %v, should NOT contain %q", got, bad) + } + } + } }) } }