package workspace import ( "context" "errors" "os" "path/filepath" "slices" "strings" ) // These are the root files created by Claude 2.1.312's subprocess scrub // initializer, including .env.local even when a repository ignores it. var sandboxMaskPaths = []string{ ".env", ".env.local", ".env.development", ".env.development.local", ".env.production", ".env.production.local", ".env.test", ".env.test.local", ".gitmodules", ".npmrc", ".yarnrc", ".yarnrc.yml", "bunfig.toml", "package-lock.json", "package.json", "pnpm-lock.yaml", "yarn.lock", } // Record absence before the specialist starts, after checkout or rebase. func (t *TaskTree) recordSandboxPaths() error { t.absentSandboxPaths = nil for _, name := range sandboxMaskPaths { _, err := os.Lstat(filepath.Join(t.Dir, name)) switch { case errors.Is(err, os.ErrNotExist): t.absentSandboxPaths = append(t.absentSandboxPaths, name) case err != nil: return err } } return nil } // The caller has proved the namespace exited. Existing files, symlinks, // directories and new files with contents belong to the step, not cleanup. func (t TaskTree) removeSandboxMasks() error { for _, name := range t.absentSandboxPaths { path := filepath.Join(t.Dir, name) info, err := os.Lstat(path) if errors.Is(err, os.ErrNotExist) { continue } if err != nil { return err } if info.Mode().IsRegular() && info.Size() == 0 { if err := os.Remove(path); err != nil { return err } } } return nil } // AddedSandboxMasks reads the exact root entries in committed trees. It does // not descend into directories with these names. Empty base means an unborn // default branch; existing paths of any type are left to normal review. func (t TaskTree) AddedSandboxMasks(ctx context.Context, base, candidate string) ([]string, error) { if !objectID.MatchString(candidate) || base != "" && !objectID.MatchString(base) { return nil, ErrTreeRefused } read := func(sha string) (map[string]bool, error) { entries := map[string]bool{} if sha == "" { return entries, nil } args := []string{"ls-tree", "-z", "-l", "--full-tree", sha, "--"} raw, err := t.git(ctx, nil, nil, append(args, sandboxMaskPaths...)...) if err != nil { return nil, err } for _, entry := range strings.Split(raw, "\x00") { if entry == "" { continue } meta, name, ok := strings.Cut(entry, "\t") f := strings.Fields(meta) if !ok || len(f) != 4 || !slices.Contains(sandboxMaskPaths, name) { return nil, ErrTreeRefused } entries[name] = (f[0] == "100644" || f[0] == "100755") && f[1] == "blob" && f[3] == "0" } return entries, nil } before, err := read(base) if err != nil { return nil, err } after, err := read(candidate) if err != nil { return nil, err } var paths []string for _, name := range sandboxMaskPaths { if _, existed := before[name]; !existed && after[name] { paths = append(paths, name) } } return paths, nil }