| 1 | package workspace |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "os" |
| 7 | "path/filepath" |
| 8 | "slices" |
| 9 | "strings" |
| 10 | ) |
| 11 | |
| 12 | // These are the root files created by Claude 2.1.312's subprocess scrub |
| 13 | // initializer, including .env.local even when a repository ignores it. |
| 14 | var sandboxMaskPaths = []string{ |
| 15 | ".env", ".env.local", ".env.development", ".env.development.local", |
| 16 | ".env.production", ".env.production.local", ".env.test", ".env.test.local", |
| 17 | ".gitmodules", ".npmrc", ".yarnrc", ".yarnrc.yml", "bunfig.toml", |
| 18 | "package-lock.json", "package.json", "pnpm-lock.yaml", "yarn.lock", |
| 19 | } |
| 20 | |
| 21 | // Record absence before the specialist starts, after checkout or rebase. |
| 22 | func (t *TaskTree) recordSandboxPaths() error { |
| 23 | t.absentSandboxPaths = nil |
| 24 | for _, name := range sandboxMaskPaths { |
| 25 | _, err := os.Lstat(filepath.Join(t.Dir, name)) |
| 26 | switch { |
| 27 | case errors.Is(err, os.ErrNotExist): |
| 28 | t.absentSandboxPaths = append(t.absentSandboxPaths, name) |
| 29 | case err != nil: |
| 30 | return err |
| 31 | } |
| 32 | } |
| 33 | return nil |
| 34 | } |
| 35 | |
| 36 | // The caller has proved the namespace exited. Existing files, symlinks, |
| 37 | // directories and new files with contents belong to the step, not cleanup. |
| 38 | func (t TaskTree) removeSandboxMasks() error { |
| 39 | for _, name := range t.absentSandboxPaths { |
| 40 | path := filepath.Join(t.Dir, name) |
| 41 | info, err := os.Lstat(path) |
| 42 | if errors.Is(err, os.ErrNotExist) { |
| 43 | continue |
| 44 | } |
| 45 | if err != nil { |
| 46 | return err |
| 47 | } |
| 48 | if info.Mode().IsRegular() && info.Size() == 0 { |
| 49 | if err := os.Remove(path); err != nil { |
| 50 | return err |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | return nil |
| 55 | } |
| 56 | |
| 57 | // AddedSandboxMasks reads the exact root entries in committed trees. It does |
| 58 | // not descend into directories with these names. Empty base means an unborn |
| 59 | // default branch; existing paths of any type are left to normal review. |
| 60 | func (t TaskTree) AddedSandboxMasks(ctx context.Context, base, candidate string) ([]string, error) { |
| 61 | if !objectID.MatchString(candidate) || base != "" && !objectID.MatchString(base) { |
| 62 | return nil, ErrTreeRefused |
| 63 | } |
| 64 | read := func(sha string) (map[string]bool, error) { |
| 65 | entries := map[string]bool{} |
| 66 | if sha == "" { |
| 67 | return entries, nil |
| 68 | } |
| 69 | args := []string{"ls-tree", "-z", "-l", "--full-tree", sha, "--"} |
| 70 | raw, err := t.git(ctx, nil, nil, append(args, sandboxMaskPaths...)...) |
| 71 | if err != nil { |
| 72 | return nil, err |
| 73 | } |
| 74 | for _, entry := range strings.Split(raw, "\x00") { |
| 75 | if entry == "" { |
| 76 | continue |
| 77 | } |
| 78 | meta, name, ok := strings.Cut(entry, "\t") |
| 79 | f := strings.Fields(meta) |
| 80 | if !ok || len(f) != 4 || !slices.Contains(sandboxMaskPaths, name) { |
| 81 | return nil, ErrTreeRefused |
| 82 | } |
| 83 | entries[name] = (f[0] == "100644" || f[0] == "100755") && f[1] == "blob" && f[3] == "0" |
| 84 | } |
| 85 | return entries, nil |
| 86 | } |
| 87 | before, err := read(base) |
| 88 | if err != nil { |
| 89 | return nil, err |
| 90 | } |
| 91 | after, err := read(candidate) |
| 92 | if err != nil { |
| 93 | return nil, err |
| 94 | } |
| 95 | var paths []string |
| 96 | for _, name := range sandboxMaskPaths { |
| 97 | if _, existed := before[name]; !existed && after[name] { |
| 98 | paths = append(paths, name) |
| 99 | } |
| 100 | } |
| 101 | return paths, nil |
| 102 | } |
| 103 | |