A desktop coding agent with free access to frontier models.
TypeScript
fix: box shadows only under required
ogsamrat0a3f2c9ce476
diff --git a/apps/desktop/box/go/cmd/boxrun/init.go b/apps/desktop/box/go/cmd/boxrun/init.goindex a788fc89..02aa0fe8 100644--- a/apps/desktop/box/go/cmd/boxrun/init.go+++ b/apps/desktop/box/go/cmd/boxrun/init.go@@ -195,7 +195,7 @@ func stageInit(args []string) error {}}// The box's own build folders, over the project's (after its bind: they are inside it).- shadowsOn := bindShadows(sp, in)+ shadowsOn, shadowsMade := bindShadows(sp, in)if len(shadowsOn) > 0 {list, _ := json.Marshal(shadowsOn)sp.Env = append(sp.Env, "BOXD_SHADOWS="+string(list))@@ -211,7 +211,7 @@ func stageInit(args []string) error {}}if sp.InitOut != "" {- info, _ := json.Marshal(map[string]any{"overlay": overlay, "ms": time.Since(t0).Milliseconds(), "shadows": nonNil(shadowsOn)})+ info, _ := json.Marshal(map[string]any{"overlay": overlay, "ms": time.Since(t0).Milliseconds(), "shadows": nonNil(shadowsOn), "made": nonNil(shadowsMade)})_ = os.WriteFile(sp.InitOut, info, 0o600)}// Switch root: pivot_root(".", ".") stacks the old root on the new one;diff --git a/apps/desktop/box/go/cmd/boxrun/main.go b/apps/desktop/box/go/cmd/boxrun/main.goindex e7ac51dd..1557d17d 100644--- a/apps/desktop/box/go/cmd/boxrun/main.go+++ b/apps/desktop/box/go/cmd/boxrun/main.go@@ -601,8 +601,9 @@ type spec struct {NoKernel bool `json:"no_kernel_overlay,omitempty"` // fuse-overlayfs only (a kernel without overlayfs in user namespaces, or a test of one)AptRoot bool `json:"apt_root"`// The box's own build folders (shadows.go): the store mounted at shadowStore, each shadow bound from it.- ShadowStore string `json:"shadow_store,omitempty"`- Shadows []bindSpec `json:"shadows,omitempty"`+ ShadowStore string `json:"shadow_store,omitempty"`+ Shadows []bindSpec `json:"shadows,omitempty"`+ ShadowProject string `json:"shadow_project,omitempty"` // the project's inside path: shadows are made in it only}type bindSpec struct {@@ -709,9 +710,10 @@ func cmdUp(args []string) error {foreground := fs.Bool("foreground", false, "stay attached (the WSL keeper): exit when stdin closes")timeout := fs.Duration("timeout", 30*time.Second, "wait this long for boxd")overlayMode := fs.String("overlay", "auto", "auto|fuse (fuse-overlayfs only)")- var binds, roBinds, envs, shadowArgs multi+ var binds, roBinds, envs, shadowArgs, lazyArgs multishadowStoreDir := fs.String("shadow-store", "", "the box's own store of build folders (shadows; as root only)")fs.Var(&shadowArgs, "shadow", "ID=DST: a build folder of the project bound from <shadow-store>/ID (repeat)")+ fs.Var(&lazyArgs, "shadow-lazy", "DIR=MARKER[,MARKER]: a folder boxd may shadow mid-chat (repeat; default: required.json's)")fs.Var(&binds, "bind", "SRC=DST (repeat)")fs.Var(&roBinds, "ro-bind", "SRC=DST read-only (repeat)")fs.Var(&envs, "env", "K=V for boxd (repeat)")@@ -817,11 +819,19 @@ func cmdUp(args []string) error {sp.Fuse, sp.Boxd, sp.Resolv, sp.Hosts = fuseBin, *boxd, resolvFile(), filepath.Join(*box, "hosts")sp.Binds, sp.InitOut, sp.Root = bs, filepath.Join(*box, "init.json"), trueif *shadowStoreDir != "" && len(shadowArgs) > 0 {- shadowSpec, err := planShadows(*box, *shadowStoreDir, shadowArgs, bs)+ var lazy []lazyRule+ for _, v := range lazyArgs {+ rule, err := parseLazyRule(v)+ if err != nil {+ return err+ }+ lazy = append(lazy, rule)+ }+ shadowSpec, err := planShadows(*box, *shadowStoreDir, shadowArgs, bs, envValue(envs, "BOXD_ROOT"), lazy)if err != nil {return err}- sp.ShadowStore, sp.Shadows = shadowSpec.store, shadowSpec.binds+ sp.ShadowStore, sp.Shadows, sp.ShadowProject = shadowSpec.store, shadowSpec.binds, shadowSpec.project}if err := writeJSONFile(filepath.Join(*box, "spec.json"), sp); err != nil {return err@@ -934,10 +944,13 @@ func cmdUp(args []string) error {var initInfo struct {Overlay string `json:"overlay"`Shadows []string `json:"shadows"`+ Made []string `json:"made"`}if data, err := os.ReadFile(filepath.Join(*box, "init.json")); err == nil {_ = json.Unmarshal(data, &initInfo)}+ // The mount points init made on the host, kept in the box folder (the box never sees it).+ recordShadowMade(*box, initInfo.Made)s := state{Pid: cmd.Process.Pid, Port: port, Launcher: mode, IDMap: ids.Mode, Overlay: initInfo.Overlay, Image: *image, Started: time.Now().Unix(), Version: version, Shadows: initInfo.Shadows}_ = writeJSONFile(filepath.Join(*box, "state.json"), s)emit(map[string]any{"ok": true, "reused": false, "pid": s.Pid, "port": port, "launcher": mode, "idmap": ids.Mode, "overlay": s.Overlay, "shadows": nonNil(s.Shadows), "ms": time.Since(start).Milliseconds()})@@ -974,6 +987,17 @@ func keep(onEnd func()) {_ = onEnd}+// envValue: the value of the last KEY=V in env, or "".+func envValue(env []string, key string) string {+ v := ""+ for _, kv := range env {+ if strings.HasPrefix(kv, key+"=") {+ v = kv[len(key)+1:]+ }+ }+ return v+}+func hasKey(env []string, key string) bool {for _, kv := range env {if strings.HasPrefix(kv, key+"=") {diff --git a/apps/desktop/box/go/cmd/boxrun/shadows.go b/apps/desktop/box/go/cmd/boxrun/shadows.goindex e843e84c..55323e81 100644--- a/apps/desktop/box/go/cmd/boxrun/shadows.go+++ b/apps/desktop/box/go/cmd/boxrun/shadows.go@@ -8,31 +8,92 @@ package main// the Lima VM) and bound over the project's folder in the box, so the host's// builds are never written and never run in the box. Only `up` as root// (unshare) does it. A folder the host did not have is made there as an-// empty mount point; those are listed in <store>/.made.json (the same file-// boxd adds the ones it makes mid-chat to) and removed when the box stops.+// empty mount point and removed when the box stops.+//+// The store is the box's (mounted at shadowStore inside, writable there), so+// nothing in it is trusted on this side: <store>/.made.json is only for+// boxd's own clean-up in the box. What this side removes comes from+// <box>/shadows.json (the box folder is never bound into the box): the mount+// points init made, recorded from init.json, and the ones boxd made mid-chat+// only when they still match the lazy rules recorded there at plan time.+// Each is removed through the project's bind only, walking from the+// project's real folder without following a link, and only if it is an+// empty folder.import ("encoding/json"+ "errors""fmt"+ "io""os""path/filepath""regexp""strings"+ "syscall"+ "unsafe")// shadowStore: where the store is inside the box (required.json shadows.store; boxd's shadows.go).const shadowStore = "/var/lib/omnirush/shadows"+// madeFileName: in the store, boxd's list of the mount points made on the host (box-writable).+const madeFileName = ".made.json"+var shadowIDPattern = regexp.MustCompile(`^[0-9a-f]{12}$`)+// lazyRule is a folder boxd may make mid-chat (required.json shadows.binds with even_if_absent):+// `dir` next to one of `when_any`.+type lazyRule struct {+ Dir string `json:"dir"`+ WhenAny []string `json:"when_any"`+}++// The lazy rules, depth and skipped folders of required.json shadows (boxd's findNew), used+// when `up` is not given --shadow-lazy.+var (+ defaultLazyRules = []lazyRule{{Dir: "node_modules", WhenAny: []string{"package.json"}}}+ defaultLazyDepth = 4+ defaultSkipDirs = []string{".git", "node_modules", ".venv", "venv", "target", ".hg", ".svn"}+)++// parseLazyRule: DIR=MARKER[,MARKER...] (--shadow-lazy).+func parseLazyRule(v string) (lazyRule, error) {+ i := strings.Index(v, "=")+ if i <= 0 || i == len(v)-1 {+ return lazyRule{}, fmt.Errorf("bad lazy shadow %q (DIR=MARKER[,MARKER])", v)+ }+ dir := filepath.Clean(v[:i])+ if filepath.IsAbs(dir) || dir == "." || hasDotDot(dir) {+ return lazyRule{}, fmt.Errorf("bad lazy shadow %q (DIR=MARKER[,MARKER])", v)+ }+ var markers []string+ for _, m := range strings.Split(v[i+1:], ",") {+ if m = strings.TrimSpace(m); m != "" {+ markers = append(markers, m)+ }+ }+ return lazyRule{Dir: dir, WhenAny: markers}, nil+}+type shadowsFile struct {Store string `json:"store"`- Binds []bindSpec `json:"binds"` // the box's other binds: how an inside path maps to the host+ Binds []bindSpec `json:"binds"` // all the box's binds (the project's and the others)+ // The project's bind: mount points are removed through it only.+ Project *bindSpec `json:"project,omitempty"`+ // The start's shadows (inside paths): never removed unless init made them.+ Shadows []string `json:"shadows,omitempty"`+ // The mount points init made (inside paths), from init.json, still to remove.+ Made []string `json:"made,omitempty"`+ // What boxd may make mid-chat (a .made.json entry is removed only when it matches).+ Lazy []lazyRule `json:"lazy,omitempty"`+ Depth int `json:"depth,omitempty"`+ SkipDirs []string `json:"skip_dirs,omitempty"`}type shadowSpec struct {- store string- binds []bindSpec+ store string+ project string+ binds []bindSpec}func nonNil(list []string) []string {@@ -42,58 +103,149 @@ func nonNil(list []string) []string {return list}-// planShadows makes the store and a folder per shadow (ID=DST), and remembers the store-// and the other binds in <box>/shadows.json for the clean-up after a kill.-func planShadows(box, store string, args []string, binds []bindSpec) (shadowSpec, error) {+func hasDotDot(p string) bool {+ for _, c := range strings.Split(p, "/") {+ if c == ".." {+ return true+ }+ }+ return false+}++func readShadowsFile(box string) (shadowsFile, bool) {+ var f shadowsFile+ data, err := os.ReadFile(filepath.Join(box, "shadows.json"))+ if err != nil || json.Unmarshal(data, &f) != nil || f.Store == "" {+ return shadowsFile{}, false+ }+ return f, true+}++// planShadows makes the store and a folder per shadow (ID=DST), and remembers in+// <box>/shadows.json the store, the binds, the project's bind (the one whose inside path is+// `project`, BOXD_ROOT), the shadows and the lazy rules, for the clean-up after a kill.+func planShadows(box, store string, args []string, binds []bindSpec, project string, lazy []lazyRule) (shadowSpec, error) {store, _ = filepath.Abs(store)+ var proj *bindSpec+ if project != "" {+ project = filepath.Clean(project)+ for i := range binds {+ if !binds[i].RO && binds[i].Dst == project {+ b := binds[i]+ proj = &b+ break+ }+ }+ }+ if proj == nil || !filepath.IsAbs(proj.Dst) || proj.Dst == "/" {+ return shadowSpec{}, errors.New("shadows: no writable bind for the project (BOXD_ROOT)")+ }if err := os.MkdirAll(store, 0o755); err != nil {return shadowSpec{}, err}- out := shadowSpec{store: store}+ out := shadowSpec{store: store, project: proj.Dst}+ var dsts []stringfor _, v := range args {i := strings.Index(v, "=/")if i <= 0 || !shadowIDPattern.MatchString(v[:i]) {return shadowSpec{}, fmt.Errorf("bad shadow %q (ID=DST)", v)}+ dst := filepath.Clean(v[i+1:])+ if hasDotDot(v[i+1:]) || dst == proj.Dst || !within(dst, proj.Dst) {+ return shadowSpec{}, fmt.Errorf("bad shadow %q (not in the project)", v)+ }src := filepath.Join(store, v[:i])- if err := os.MkdirAll(src, 0o755); err != nil {+ // The store is the box's: a link left there is never followed (bound over the project).+ if fi, err := os.Lstat(src); err == nil && !fi.IsDir() {+ if err := os.Remove(src); err != nil {+ return shadowSpec{}, err+ }+ }+ if err := os.Mkdir(src, 0o755); err != nil && !os.IsExist(err) {return shadowSpec{}, err}- out.binds = append(out.binds, bindSpec{Src: src, Dst: filepath.Clean(v[i+1:])})+ if fi, err := os.Lstat(src); err != nil || !fi.IsDir() {+ return shadowSpec{}, fmt.Errorf("shadow store %s is not a folder", src)+ }+ out.binds = append(out.binds, bindSpec{Src: src, Dst: dst})+ dsts = append(dsts, dst)+ }+ if lazy == nil {+ lazy = defaultLazyRules+ }+ f := shadowsFile{Store: store, Binds: binds, Project: proj, Shadows: dsts, Lazy: lazy, Depth: defaultLazyDepth, SkipDirs: defaultSkipDirs}+ // Mount points an earlier run made and could not remove (not empty) are kept on the list.+ if old, ok := readShadowsFile(box); ok && old.Project != nil && *old.Project == *proj {+ f.Made = old.Made}- if err := writeJSONFile(filepath.Join(box, "shadows.json"), shadowsFile{Store: store, Binds: binds}); err != nil {+ if err := writeJSONFile(filepath.Join(box, "shadows.json"), f); err != nil {return shadowSpec{}, err}return out, nil}+// recordShadowMade adds the mount points init made (init.json "made") to <box>/shadows.json.+func recordShadowMade(box string, made []string) {+ if len(made) == 0 {+ return+ }+ f, ok := readShadowsFile(box)+ if !ok {+ return+ }+ f.Made = union(f.Made, made)+ _ = writeJSONFile(filepath.Join(box, "shadows.json"), f)+}++func union(a, b []string) []string {+ seen := map[string]bool{}+ var out []string+ for _, list := range [][]string{a, b} {+ for _, p := range list {+ if !seen[p] {+ seen[p] = true+ out = append(out, p)+ }+ }+ }+ return out+}+// bindShadows (init, as root, before the root switch): the store at shadowStore, then each// shadow over its folder. A folder that cannot be shadowed is left as it is (never fails the box).-// Returns the inside paths shadowed.-func bindShadows(sp spec, in func(string) string) []string {- if sp.ShadowStore == "" || len(sp.Shadows) == 0 {- return nil+// Returns the inside paths shadowed and the ones made here as mount points.+func bindShadows(sp spec, in func(string) string) (on, made []string) {+ if sp.ShadowStore == "" || len(sp.Shadows) == 0 || sp.ShadowProject == "" {+ return nil, nil}if err := ensureDir(in(shadowStore)); err != nil {fmt.Fprintf(os.Stderr, "boxrun: shadows: %v\n", err)- return nil+ return nil, nil}if err := bind(sp.ShadowStore, in(shadowStore), false, false); err != nil {fmt.Fprintf(os.Stderr, "boxrun: shadows: %v\n", err)- return nil+ return nil, nil}- madeFile := filepath.Join(sp.ShadowStore, ".made.json")- var made []string- if data, err := os.ReadFile(madeFile); err == nil {- _ = json.Unmarshal(data, &made)+ projReal, err := filepath.EvalSymlinks(in(sp.ShadowProject))+ if err != nil {+ fmt.Fprintf(os.Stderr, "boxrun: shadows: %v\n", err)+ return nil, nil}- var on []stringfor _, b := range sp.Shadows {+ if b.Dst == sp.ShadowProject || !within(b.Dst, sp.ShadowProject) {+ continue+ }target := in(b.Dst)+ // A link in the project (written by the box) must not take the folder elsewhere.+ parentReal, err := filepath.EvalSymlinks(filepath.Dir(target))+ if err != nil || !within(parentReal, projReal) {+ continue+ }+ target = filepath.Join(parentReal, filepath.Base(target))madeHere := falseif fi, err := os.Lstat(target); err != nil {// Only the leaf is made (its parent is the project's folder, e.g. next to package.json).- if parent, err := os.Stat(filepath.Dir(target)); err != nil || !parent.IsDir() {+ if parent, err := os.Lstat(parentReal); err != nil || !parent.IsDir() {continue}if os.Mkdir(target, 0o755) != nil {@@ -111,65 +263,285 @@ func bindShadows(sp spec, in func(string) string) []string {continue}if madeHere {- made = append(made, b.Dst)+ // Recorded where it really is (a link inside the project may have led there).+ if rel, err := filepath.Rel(projReal, target); err == nil && rel != "." && !hasDotDot(rel) {+ made = append(made, filepath.Join(sp.ShadowProject, rel))+ }}on = append(on, b.Dst)}+ // For boxd's own clean-up in the box. The store is the box's: never write through a link left there.+ madeFile := filepath.Join(sp.ShadowStore, madeFileName)+ _ = os.Remove(madeFile)if data, err := json.Marshal(nonNil(made)); err == nil {- _ = os.WriteFile(madeFile, data, 0o600)+ if fh, err := os.OpenFile(madeFile, os.O_WRONLY|os.O_CREATE|os.O_EXCL|syscall.O_NOFOLLOW, 0o600); err == nil {+ _, _ = fh.Write(data)+ _ = fh.Close()+ }}- return on+ return on, made}-// hostPath: an inside path as this side sees it, through the bind that holds it (longest first).-func hostPath(inside string, binds []bindSpec) string {- best := -1- for i, b := range binds {- if within(inside, b.Dst) && (best < 0 || len(b.Dst) > len(binds[best].Dst)) {- best = i+func within(p, dir string) bool {+ return p == dir || strings.HasPrefix(p, strings.TrimSuffix(dir, "/")+"/")+}++// projectRel: an inside path below the project's bind as its components, or false when it is+// not absolute, has a `..`, is not strictly in the project, or is in another bind inside it.+func projectRel(inside string, f shadowsFile) ([]string, bool) {+ if f.Project == nil || inside == "" || !filepath.IsAbs(inside) || hasDotDot(inside) || strings.ContainsRune(inside, 0) {+ return nil, false+ }+ c := filepath.Clean(inside)+ proj := f.Project.Dst+ if c == proj || !within(c, proj) {+ return nil, false+ }+ for _, b := range f.Binds {+ if b.Dst != proj && within(b.Dst, proj) && within(c, b.Dst) {+ return nil, false}}- if best < 0 {- return ""+ rest := strings.TrimPrefix(c, strings.TrimSuffix(proj, "/")+"/")+ parts := strings.Split(rest, "/")+ for _, p := range parts {+ if p == "" || p == "." || p == ".." {+ return nil, false+ }}- rest := strings.TrimPrefix(inside, binds[best].Dst)- return filepath.Join(binds[best].Src, rest)+ return parts, true}-func within(p, dir string) bool {- return p == dir || strings.HasPrefix(p, strings.TrimSuffix(dir, "/")+"/")+// lazyMatch: whether rel is a folder boxd may make mid-chat (the lazy rules: `dir` in a folder no+// deeper than Depth, not under a skipped or hidden one); returns how deep the rule's folder is and+// its markers (plain file names; one must be a file there).+func lazyMatch(rel []string, f shadowsFile) (int, []string, bool) {+ skip := map[string]bool{}+ for _, d := range f.SkipDirs {+ skip[d] = true+ }+ depth := f.Depth+ if depth <= 0 {+ depth = defaultLazyDepth+ }+ for _, rule := range f.Lazy {+ dir := filepath.Clean(rule.Dir)+ if dir == "." || filepath.IsAbs(dir) || hasDotDot(dir) {+ continue+ }+ dirParts := strings.Split(dir, "/")+ if len(rel) < len(dirParts) {+ continue+ }+ base := len(rel) - len(dirParts)+ if strings.Join(rel[base:], "/") != dir || base > depth {+ continue+ }+ ok := true+ for _, p := range rel[:base] {+ if strings.HasPrefix(p, ".") || skip[p] {+ ok = false+ break+ }+ }+ if !ok {+ continue+ }+ var markers []string+ for _, m := range rule.WhenAny {+ if m != "" && !strings.ContainsAny(m, "/@*?[") && m != "." && m != ".." {+ markers = append(markers, m)+ }+ }+ if len(markers) > 0 {+ return base, markers, true+ }+ }+ return 0, nil, false}-// removeShadowMade removes the mount points this box made on the host (empty folders only:-// anything written there since stays), after the box stopped without boxd's own clean-up.-func removeShadowMade(box string) {- data, err := os.ReadFile(filepath.Join(box, "shadows.json"))+type removal int++const (+ removedDir removal = iota // removed (or already gone)+ keptDir // a folder that could not be removed (not empty): try again later+ refusedDir // not an empty real folder in the project (a link, a file, a link on the way, no marker)+)++const (+ atRemoveDir = 0x200 // AT_REMOVEDIR+ oPath = 0x200000 // O_PATH+)++func rmdirAt(dirfd int, name string) error {+ p, err := syscall.BytePtrFromString(name)if err != nil {- return+ return err}- var f shadowsFile- if json.Unmarshal(data, &f) != nil || f.Store == "" {- return+ _, _, e := syscall.Syscall(syscall.SYS_UNLINKAT, uintptr(dirfd), uintptr(unsafe.Pointer(p)), atRemoveDir)+ if e != 0 {+ return e}- madeFile := filepath.Join(f.Store, ".made.json")- raw, err := os.ReadFile(madeFile)+ return nil+}++// statAt: the mode of name in dirfd, the name itself (a link is not followed).+func statAt(dirfd int, name string) (uint32, error) {+ fd, err := syscall.Openat(dirfd, name, oPath|syscall.O_NOFOLLOW|syscall.O_CLOEXEC, 0)if err != nil {- return+ return 0, err+ }+ defer syscall.Close(fd)+ var st syscall.Stat_t+ if err := syscall.Fstat(fd, &st); err != nil {+ return 0, err+ }+ return st.Mode & syscall.S_IFMT, nil+}++// removeEmptyDir removes root/rel... if it is an empty real folder. Every folder on the way is+// opened from the project's real folder without following a link, so nothing outside it is+// reached; rmdir never removes a file or a link and fails on a folder with anything in it.+// markers (when given): one must be a regular file in the folder markerAt components down.+func removeEmptyDir(root string, rel []string, markerAt int, markers []string) removal {+ if len(rel) == 0 {+ return refusedDir+ }+ real, err := filepath.EvalSymlinks(root)+ if err != nil {+ if os.IsNotExist(err) {+ return removedDir+ }+ return keptDir+ }+ fd, err := syscall.Open(real, syscall.O_RDONLY|syscall.O_DIRECTORY|syscall.O_CLOEXEC, 0)+ if err != nil {+ return keptDir+ }+ for i := 0; ; i++ {+ if markers != nil && i == markerAt {+ found := false+ for _, m := range markers {+ if mode, err := statAt(fd, m); err == nil && mode == syscall.S_IFREG {+ found = true+ break+ }+ }+ if !found {+ syscall.Close(fd)+ return refusedDir+ }+ }+ name := rel[i]+ if i == len(rel)-1 {+ defer syscall.Close(fd)+ mode, err := statAt(fd, name)+ if err == syscall.ENOENT {+ return removedDir+ }+ if err != nil {+ return keptDir+ }+ if mode != syscall.S_IFDIR {+ return refusedDir+ }+ switch err := rmdirAt(fd, name); err {+ case nil, syscall.ENOENT:+ return removedDir+ case syscall.ENOTDIR:+ return refusedDir+ default:+ return keptDir+ }+ }+ next, err := syscall.Openat(fd, name, syscall.O_RDONLY|syscall.O_DIRECTORY|syscall.O_NOFOLLOW|syscall.O_CLOEXEC, 0)+ syscall.Close(fd)+ switch err {+ case nil:+ fd = next+ case syscall.ENOENT:+ return removedDir+ case syscall.ELOOP, syscall.ENOTDIR:+ return refusedDir+ default:+ return keptDir+ }+ }+}++// readBoxMadeFile reads the store's .made.json (box-written: a link is not followed, the size is capped).+func readBoxMadeFile(store string) []string {+ fh, err := os.OpenFile(filepath.Join(store, madeFileName), os.O_RDONLY|syscall.O_NOFOLLOW|syscall.O_NONBLOCK, 0)+ if err != nil {+ return nil+ }+ defer fh.Close()+ if fi, err := fh.Stat(); err != nil || !fi.Mode().IsRegular() {+ return nil}- var made []string- if json.Unmarshal(raw, &made) != nil {+ data, err := io.ReadAll(io.LimitReader(fh, 1<<20))+ if err != nil {+ return nil+ }+ var list []string+ if json.Unmarshal(data, &list) != nil {+ return nil+ }+ return list+}++// readInitMade: the mount points init made at the last start (<box>/init.json, written by init+// before the root switch), in case `up` did not get to record them.+func readInitMade(box string) []string {+ var info struct {+ Made []string `json:"made"`+ }+ if data, err := os.ReadFile(filepath.Join(box, "init.json")); err == nil {+ _ = json.Unmarshal(data, &info)+ }+ return info.Made+}++// removeShadowMade removes the mount points this box made on the host (empty folders only:+// anything written there since stays), after the box stopped without boxd's own clean-up.+func removeShadowMade(box string) {+ f, ok := readShadowsFile(box)+ if !ok || f.Project == nil {return}+ trusted := union(f.Made, readInitMade(box))+ isTrusted := map[string]bool{}var left []string- for _, inside := range made {- host := hostPath(inside, f.Binds)- if host == "" {+ for _, inside := range trusted {+ isTrusted[inside] = true+ rel, ok := projectRel(inside, f)+ if !ok {continue}- if err := os.Remove(host); err != nil && !os.IsNotExist(err) {+ if removeEmptyDir(f.Project.Src, rel, 0, nil) == keptDir {left = append(left, inside)}}- out, _ := json.Marshal(nonNil(left))- _ = os.WriteFile(madeFile, out, 0o600)+ start := map[string]bool{}+ for _, s := range f.Shadows {+ start[s] = true+ }+ // boxd's mid-chat ones: the list is the box's, so only what the lazy rules allow.+ for _, inside := range readBoxMadeFile(f.Store) {+ if isTrusted[inside] {+ continue+ }+ rel, ok := projectRel(inside, f)+ if !ok || start[filepath.Clean(inside)] {+ continue+ }+ at, markers, ok := lazyMatch(rel, f)+ if !ok {+ continue+ }+ _ = removeEmptyDir(f.Project.Src, rel, at, markers)+ }+ _ = os.Remove(filepath.Join(f.Store, madeFileName))+ f.Made = left+ _ = writeJSONFile(filepath.Join(box, "shadows.json"), f)}diff --git a/apps/desktop/box/go/cmd/boxrun/shadows_test.go b/apps/desktop/box/go/cmd/boxrun/shadows_test.goindex bb8f3f42..ae66b85f 100644--- a/apps/desktop/box/go/cmd/boxrun/shadows_test.go+++ b/apps/desktop/box/go/cmd/boxrun/shadows_test.go@@ -6,20 +6,66 @@ import ("encoding/json""os""path/filepath"+ "reflect"+ "runtime"+ "syscall""testing")-func TestHostPathThroughTheLongestBind(t *testing.T) {- binds := []bindSpec{{Src: "/mnt/c/Users/u/proj", Dst: "/c/Users/u/proj"}, {Src: "/home/run", Dst: "/run/omnirush"}, {Src: "/mnt/c/Users/u/proj/sub", Dst: "/c/Users/u/proj/sub"}}- cases := map[string]string{- "/c/Users/u/proj/node_modules": "/mnt/c/Users/u/proj/node_modules",- "/c/Users/u/proj/sub/node_modules": "/mnt/c/Users/u/proj/sub/node_modules",- "/c/Users/u/projector/node_modules": "",- "/elsewhere/node_modules": "",+func TestProjectRelOnlyThroughTheProjectBind(t *testing.T) {+ f := shadowsFile{+ Project: &bindSpec{Src: "/mnt/c/Users/u/proj", Dst: "/c/Users/u/proj"},+ Binds: []bindSpec{{Src: "/mnt/c/Users/u/proj", Dst: "/c/Users/u/proj"}, {Src: "/home/run", Dst: "/run/omnirush"}, {Src: "/mnt/c/Users/u/other", Dst: "/c/Users/u/proj/sub"}},+ }+ cases := map[string][]string{+ "/c/Users/u/proj/node_modules": {"node_modules"},+ "/c/Users/u/proj/packages/web/node_modules": {"packages", "web", "node_modules"},+ "/c/Users/u/proj//a/./node_modules": {"a", "node_modules"},+ "/c/Users/u/proj": nil, // the project itself+ "/c/Users/u/proj/sub/node_modules": nil, // another bind inside the project+ "/c/Users/u/proj/sub": nil,+ "/c/Users/u/projector/node_modules": nil,+ "/run/omnirush/node_modules": nil,+ "/elsewhere/node_modules": nil,+ "/c/Users/u/proj/../../../etc/passwd": nil,+ "/c/Users/u/proj/a/../node_modules": nil,+ "c/Users/u/proj/node_modules": nil,+ "": nil,}for inside, want := range cases {- if got := hostPath(inside, binds); got != want {- t.Errorf("hostPath(%s) = %q, want %q", inside, got, want)+ got, ok := projectRel(inside, f)+ if ok != (want != nil) || (ok && !reflect.DeepEqual(got, want)) {+ t.Errorf("projectRel(%q) = %v %v, want %v", inside, got, ok, want)+ }+ }+ if _, ok := projectRel("/c/Users/u/proj/node_modules", shadowsFile{}); ok {+ t.Error("no project bind: nothing")+ }+}++func TestLazyMatch(t *testing.T) {+ f := shadowsFile{Lazy: defaultLazyRules, Depth: 4, SkipDirs: defaultSkipDirs}+ ok := [][]string{{"node_modules"}, {"packages", "web", "node_modules"}, {"a", "b", "c", "d", "node_modules"}}+ bad := [][]string{{"build"}, {"pkg", "node_modules", "x"}, {".hidden", "node_modules"}, {"vendor", "target", "node_modules"}, {"a", "b", "c", "d", "e", "node_modules"}, {"node_modules", "x", "node_modules"}}+ for _, rel := range ok {+ if at, m, yes := lazyMatch(rel, f); !yes || at != len(rel)-1 || !reflect.DeepEqual(m, []string{"package.json"}) {+ t.Errorf("lazyMatch(%v) = %d %v %v", rel, at, m, yes)+ }+ }+ for _, rel := range bad {+ if _, _, yes := lazyMatch(rel, f); yes {+ t.Errorf("lazyMatch(%v) matched", rel)+ }+ }+ if _, _, yes := lazyMatch([]string{"node_modules"}, shadowsFile{Lazy: []lazyRule{{Dir: "node_modules", WhenAny: []string{"@host-binaries", "*.csproj"}}}}); yes {+ t.Error("a rule with no plain marker never matches")+ }+ if r, err := parseLazyRule("vendor/bundle=Gemfile, gems.rb"); err != nil || r.Dir != "vendor/bundle" || !reflect.DeepEqual(r.WhenAny, []string{"Gemfile", "gems.rb"}) {+ t.Errorf("parseLazyRule: %+v %v", r, err)+ }+ for _, v := range []string{"../x=a", "/abs=a", "x=", "=a", "."} {+ if _, err := parseLazyRule(v); err == nil {+ t.Errorf("parseLazyRule(%q) accepted", v)}}}@@ -27,25 +73,51 @@ func TestHostPathThroughTheLongestBind(t *testing.T) {func TestPlanShadowsAndTheCleanUpOfMadeMountPoints(t *testing.T) {box, store, host := t.TempDir(), filepath.Join(t.TempDir(), "k"), t.TempDir()binds := []bindSpec{{Src: host, Dst: "/c/p"}}- sp, err := planShadows(box, store, []string{"dba27c31aad9=/c/p/node_modules", "093be456e0e4=/c/p/packages/web/node_modules"}, binds)+ sp, err := planShadows(box, store, []string{"dba27c31aad9=/c/p/node_modules", "093be456e0e4=/c/p/packages/web/node_modules"}, binds, "/c/p", nil)if err != nil {t.Fatal(err)}- if len(sp.binds) != 2 || sp.binds[0].Src != filepath.Join(store, "dba27c31aad9") || sp.binds[0].Dst != "/c/p/node_modules" {+ if len(sp.binds) != 2 || sp.binds[0].Src != filepath.Join(store, "dba27c31aad9") || sp.binds[0].Dst != "/c/p/node_modules" || sp.project != "/c/p" {t.Fatalf("planShadows: %+v", sp)}if fi, err := os.Stat(filepath.Join(store, "093be456e0e4")); err != nil || !fi.IsDir() {t.Fatal("the store folders are made")}- if _, err := planShadows(box, store, []string{"../../etc=/c/p/x"}, binds); err == nil {+ if _, err := planShadows(box, store, []string{"../../etc=/c/p/x"}, binds, "/c/p", nil); err == nil {t.Fatal("a shadow id is 12 hex digits")}+ if _, err := planShadows(box, store, []string{"dba27c31aad9=/etc"}, binds, "/c/p", nil); err == nil {+ t.Fatal("a shadow is in the project")+ }+ if _, err := planShadows(box, store, []string{"dba27c31aad9=/c/p/../etc"}, binds, "/c/p", nil); err == nil {+ t.Fatal("a shadow has no ..")+ }+ if _, err := planShadows(box, store, []string{"dba27c31aad9=/c/p/node_modules"}, binds, "", nil); err == nil {+ t.Fatal("shadows need the project's bind")+ }+ if _, err := planShadows(box, store, []string{"dba27c31aad9=/c/p/node_modules"}, []bindSpec{{Src: host, Dst: "/c/p", RO: true}}, "/c/p", nil); err == nil {+ t.Fatal("a read-only bind is not the project's")+ }+ // A link the box left in the store is replaced, never followed.+ outside := t.TempDir()+ _ = os.RemoveAll(filepath.Join(store, "dba27c31aad9"))+ if err := os.Symlink(outside, filepath.Join(store, "dba27c31aad9")); err != nil {+ t.Fatal(err)+ }+ if _, err := planShadows(box, store, []string{"dba27c31aad9=/c/p/node_modules", "093be456e0e4=/c/p/packages/web/node_modules"}, binds, "/c/p", nil); err != nil {+ t.Fatal(err)+ }+ if fi, err := os.Lstat(filepath.Join(store, "dba27c31aad9")); err != nil || !fi.IsDir() {+ t.Fatal("the store folder is a real folder again")+ }+ if _, err := os.Stat(outside); err != nil {+ t.Fatal("the link's target is left alone")+ }// What init made on the host: an empty node_modules (removed), one written to since (kept)._ = os.MkdirAll(filepath.Join(host, "node_modules"), 0o755)_ = os.MkdirAll(filepath.Join(host, "packages", "web", "node_modules"), 0o755)_ = os.WriteFile(filepath.Join(host, "packages", "web", "node_modules", "kept"), []byte("x"), 0o644)- made, _ := json.Marshal([]string{"/c/p/node_modules", "/c/p/packages/web/node_modules"})- _ = os.WriteFile(filepath.Join(store, ".made.json"), made, 0o600)+ recordShadowMade(box, []string{"/c/p/node_modules", "/c/p/packages/web/node_modules"})removeShadowMade(box)if _, err := os.Stat(filepath.Join(host, "node_modules")); !os.IsNotExist(err) {t.Fatal("the empty mount point is removed")@@ -53,9 +125,211 @@ func TestPlanShadowsAndTheCleanUpOfMadeMountPoints(t *testing.T) {if _, err := os.Stat(filepath.Join(host, "packages", "web", "node_modules", "kept")); err != nil {t.Fatal("a folder with files is never removed")}- data, _ := os.ReadFile(filepath.Join(store, ".made.json"))- if string(data) != `["/c/p/packages/web/node_modules"]` {- t.Fatalf(".made.json after: %s", data)+ f, _ := readShadowsFile(box)+ if !reflect.DeepEqual(f.Made, []string{"/c/p/packages/web/node_modules"}) {+ t.Fatalf("shadows.json made after: %v", f.Made)+ }+ // The next plan keeps the list of the ones not removed yet.+ if _, err := planShadows(box, store, []string{"dba27c31aad9=/c/p/node_modules"}, binds, "/c/p", nil); err != nil {+ t.Fatal(err)+ }+ if f, _ := readShadowsFile(box); !reflect.DeepEqual(f.Made, []string{"/c/p/packages/web/node_modules"}) {+ t.Fatalf("shadows.json made after a new plan: %v", f.Made)+ }+ // init.json's list counts too (up did not get to record it).+ _ = os.MkdirAll(filepath.Join(host, "x", "node_modules"), 0o755)+ _ = os.WriteFile(filepath.Join(box, "init.json"), []byte(`{"made":["/c/p/x/node_modules","/c/p/../../etc"]}`), 0o600)+ removeShadowMade(box)+ if _, err := os.Stat(filepath.Join(host, "x", "node_modules")); !os.IsNotExist(err) {+ t.Fatal("init.json's made folder is removed")}removeShadowMade(t.TempDir()) // no shadows.json: nothing}++func TestCleanUpNeverTrustsTheBoxList(t *testing.T) {+ box, store, host, run, nested := t.TempDir(), filepath.Join(t.TempDir(), "k"), t.TempDir(), t.TempDir(), t.TempDir()+ victim := t.TempDir() // outside the project: nothing in it may go+ mk := func(p string) {+ if err := os.MkdirAll(p, 0o755); err != nil {+ t.Fatal(err)+ }+ }+ file := func(p string) {+ mk(filepath.Dir(p))+ if err := os.WriteFile(p, []byte("{}"), 0o644); err != nil {+ t.Fatal(err)+ }+ }+ binds := []bindSpec{{Src: host, Dst: "/c/p"}, {Src: run, Dst: "/run/omnirush"}, {Src: nested, Dst: "/c/p/sub"}}+ if _, err := planShadows(box, store, []string{"dba27c31aad9=/c/p/node_modules"}, binds, "/c/p", nil); err != nil {+ t.Fatal(err)+ }+ // Outside the project.+ file(filepath.Join(victim, "passwd"))+ mk(filepath.Join(victim, "empty"))+ file(filepath.Join(victim, "package.json"))+ mk(filepath.Join(victim, "node_modules"))+ // Through another bind (the run folder) and a bind inside the project.+ file(filepath.Join(run, "package.json"))+ mk(filepath.Join(run, "node_modules"))+ file(filepath.Join(nested, "package.json"))+ mk(filepath.Join(nested, "node_modules"))+ // In the project: a link, a file, a full folder, a linked parent, rule misses.+ file(filepath.Join(host, "a", "package.json"))+ _ = os.Symlink(filepath.Join(victim, "empty"), filepath.Join(host, "a", "node_modules"))+ file(filepath.Join(host, "b", "package.json"))+ file(filepath.Join(host, "b", "node_modules"))+ file(filepath.Join(host, "c", "package.json"))+ file(filepath.Join(host, "c", "node_modules", "left-pad", "index.js"))+ _ = os.Symlink(victim, filepath.Join(host, "evil"))+ mk(filepath.Join(host, "nopkg", "node_modules"))+ file(filepath.Join(host, "f", "package.json"))+ mk(filepath.Join(host, "f", "build"))+ file(filepath.Join(host, ".hidden", "package.json"))+ mk(filepath.Join(host, ".hidden", "node_modules"))+ file(filepath.Join(host, "a1", "a2", "a3", "a4", "a5", "package.json"))+ mk(filepath.Join(host, "a1", "a2", "a3", "a4", "a5", "node_modules"))+ mk(filepath.Join(host, "dirpkg", "package.json"))+ mk(filepath.Join(host, "dirpkg", "node_modules"))+ // The start's shadow over the host's own (empty) node_modules: not made, never removed.+ file(filepath.Join(host, "package.json"))+ mk(filepath.Join(host, "node_modules"))+ // Legit: a package made mid-chat and its empty mount point.+ file(filepath.Join(host, "packages", "api", "package.json"))+ mk(filepath.Join(host, "packages", "api", "node_modules"))++ rel, _ := filepath.Rel("/", victim)+ list := []string{+ "/c/p/../../" + rel + "/passwd",+ "/c/p/../../../../../../../../" + rel + "/empty",+ "../../etc/passwd",+ filepath.Join(victim, "empty"),+ filepath.Join(victim, "node_modules"),+ "/run/omnirush/node_modules",+ "/c/p/sub/node_modules",+ "/c/p/a/node_modules",+ "/c/p/b/node_modules",+ "/c/p/c/node_modules",+ "/c/p/evil/node_modules",+ "/c/p/nopkg/node_modules",+ "/c/p/f/build",+ "/c/p/.hidden/node_modules",+ "/c/p/a1/a2/a3/a4/a5/node_modules",+ "/c/p/dirpkg/node_modules",+ "/c/p/node_modules",+ "/c/p",+ "/c/p/packages/api/node_modules",+ }+ data, _ := json.Marshal(list)+ if err := os.WriteFile(filepath.Join(store, ".made.json"), data, 0o600); err != nil {+ t.Fatal(err)+ }+ removeShadowMade(box)++ for _, p := range []string{+ filepath.Join(victim, "passwd"), filepath.Join(victim, "empty"), filepath.Join(victim, "node_modules"),+ filepath.Join(run, "node_modules"), filepath.Join(nested, "node_modules"),+ filepath.Join(host, "b", "node_modules"), filepath.Join(host, "c", "node_modules", "left-pad", "index.js"),+ filepath.Join(host, "nopkg", "node_modules"), filepath.Join(host, "f", "build"), filepath.Join(host, ".hidden", "node_modules"),+ filepath.Join(host, "a1", "a2", "a3", "a4", "a5", "node_modules"), filepath.Join(host, "dirpkg", "node_modules"),+ filepath.Join(host, "node_modules"), host,+ } {+ if _, err := os.Lstat(p); err != nil {+ t.Errorf("%s was removed: %v", p, err)+ }+ }+ if fi, err := os.Lstat(filepath.Join(host, "a", "node_modules")); err != nil || fi.Mode()&os.ModeSymlink == 0 {+ t.Error("a link entry is left as it is")+ }+ if _, err := os.Stat(filepath.Join(host, "packages", "api", "node_modules")); !os.IsNotExist(err) {+ t.Error("a mid-chat mount point the lazy rules allow is removed")+ }+ if _, err := os.Lstat(filepath.Join(store, ".made.json")); !os.IsNotExist(err) {+ t.Error("the box's list is done with")+ }++ // A link as the box's list: neither read nor written through.+ file(filepath.Join(host, "packages", "web", "package.json"))+ mk(filepath.Join(host, "packages", "web", "node_modules"))+ target := filepath.Join(victim, "list.json")+ _ = os.WriteFile(target, []byte(`["/c/p/packages/web/node_modules"]`), 0o644)+ _ = os.Symlink(target, filepath.Join(store, ".made.json"))+ removeShadowMade(box)+ if _, err := os.Stat(filepath.Join(host, "packages", "web", "node_modules")); err != nil {+ t.Error("a list behind a link is not read")+ }+ if data, err := os.ReadFile(target); err != nil || string(data) != `["/c/p/packages/web/node_modules"]` {+ t.Errorf("the link's target is left alone: %s %v", data, err)+ }+}++func TestRemoveEmptyDirNeverFollowsALink(t *testing.T) {+ root, victim := t.TempDir(), t.TempDir()+ _ = os.MkdirAll(filepath.Join(victim, "x", "node_modules"), 0o755)+ _ = os.Symlink(victim, filepath.Join(root, "l"))+ if r := removeEmptyDir(root, []string{"l", "x", "node_modules"}, 0, nil); r != refusedDir {+ t.Errorf("through a link: %v", r)+ }+ if _, err := os.Stat(filepath.Join(victim, "x", "node_modules")); err != nil {+ t.Error("removed through a link")+ }+ if r := removeEmptyDir(root, []string{"gone", "node_modules"}, 0, nil); r != removedDir {+ t.Errorf("a missing one is done: %v", r)+ }+}++// bindShadows binds as root: run in a mount namespace of its own (one locked thread), with sudo.+func TestBindShadowsStaysInTheProject(t *testing.T) {+ if os.Geteuid() != 0 {+ t.Skip("needs root (mounts)")+ }+ rootfs, store, outside := t.TempDir(), t.TempDir(), t.TempDir()+ proj := filepath.Join(rootfs, "c", "p")+ for _, d := range []string{filepath.Join(proj, "pkg"), filepath.Join(store, "aaaaaaaaaaaa"), filepath.Join(store, "bbbbbbbbbbbb"), filepath.Join(store, "cccccccccccc")} {+ _ = os.MkdirAll(d, 0o755)+ }+ _ = os.Symlink(outside, filepath.Join(proj, "evil"))+ victim := filepath.Join(outside, "victim")+ _ = os.WriteFile(victim, []byte("keep"), 0o644)+ _ = os.Symlink(victim, filepath.Join(store, ".made.json"))+ sp := spec{ShadowStore: store, ShadowProject: "/c/p", Shadows: []bindSpec{+ {Src: filepath.Join(store, "aaaaaaaaaaaa"), Dst: "/c/p/pkg/node_modules"},+ {Src: filepath.Join(store, "bbbbbbbbbbbb"), Dst: "/c/p/evil/node_modules"},+ {Src: filepath.Join(store, "cccccccccccc"), Dst: "/elsewhere/node_modules"},+ }}+ in := func(p string) string { return filepath.Join(rootfs, p) }+ type result struct {+ on, made []string+ err error+ }+ done := make(chan result)+ go func() {+ runtime.LockOSThread() // never unlocked: the thread ends with its namespace+ if err := syscall.Unshare(syscall.CLONE_NEWNS); err != nil {+ done <- result{err: err}+ return+ }+ if err := syscall.Mount("", "/", "", syscall.MS_REC|syscall.MS_PRIVATE, ""); err != nil {+ done <- result{err: err}+ return+ }+ on, made := bindShadows(sp, in)+ done <- result{on: on, made: made}+ }()+ r := <-done+ if r.err != nil {+ t.Skipf("no mount namespace: %v", r.err)+ }+ if !reflect.DeepEqual(r.on, []string{"/c/p/pkg/node_modules"}) || !reflect.DeepEqual(r.made, []string{"/c/p/pkg/node_modules"}) {+ t.Fatalf("bindShadows: on %v made %v", r.on, r.made)+ }+ if _, err := os.Lstat(filepath.Join(outside, "node_modules")); !os.IsNotExist(err) {+ t.Error("a folder was made through a link out of the project")+ }+ if data, _ := os.ReadFile(victim); string(data) != "keep" {+ t.Errorf("the store's list was written through a link: %q", data)+ }+ if data, err := os.ReadFile(filepath.Join(store, ".made.json")); err != nil || string(data) != `["/c/p/pkg/node_modules"]` {+ t.Errorf("the store's list for boxd: %s %v", data, err)+ }+}diff --git a/apps/server/src/box/backends/bwrap.ts b/apps/server/src/box/backends/bwrap.tsindex 6db43b91..6c6dc3b5 100644--- a/apps/server/src/box/backends/bwrap.ts+++ b/apps/server/src/box/backends/bwrap.ts@@ -22,7 +22,7 @@ import os from "node:os";import path from "node:path";import { GOARCH, RUN_DIR, SHIMS_DIR, containerName, containerRoot, helperPath, newToken, readJson, writeJson, type BoxInstallLog, type BoxRuntime } from "../common.js";-import { shadowId, type ShadowPlan } from "../required.js";+import { REQUIRED_RULES, shadowId, type ShadowPlan } from "../required.js";import { runCommand, type RunCommand, type RunOptions } from "../run.js";import type { RuntimeResult } from "../runtime.js";import { YOUNG_IMAGE_MS, removableImage, removeTree, staleEntries } from "../retention.mjs";@@ -166,6 +166,14 @@ export function boxdEnv({ token, croot, idleMinutes = IDLE_MINUTES, listen = "12];}+/** The folders boxd may shadow mid-chat (required.json shadows.binds, even_if_absent), with their plain file markers. CLI: the same. */+export function lazyShadowRules(): Array<{ dir: string; markers: string[] }> {+ return REQUIRED_RULES.shadows.binds+ .filter((rule) => rule.even_if_absent)+ .map((rule) => ({ dir: rule.dir, markers: rule.when_any.filter((m) => !/[/@*?[,]/.test(m)) }))+ .filter((rule) => rule.markers.length > 0);+}+/** `boxrun up` arguments for a project's box. */export function upArgs({ box, store, image, boxd, portFile, root, croot, runFolder, shims = null, pkglog = null, token, bwrap = null, idleMinutes = IDLE_MINUTES, listen, platform = "linux", mapPath = (p: string) => p, shadows = null, shadowStore = null }: { box: string; store: string; image: string; boxd: string; portFile: string; root: string; croot: string; runFolder: string; shims?: string | null; pkglog?: Pick<BoxInstallLog, "root"> | null; token: string; bwrap?: string | null; idleMinutes?: number; listen?: string; platform?: NodeJS.Platform; mapPath?: (p: string) => string; shadows?: Pick<ShadowPlan, "binds"> | null; shadowStore?: string | null }): string[] {const args = ["up", "--box", box, "--store", store, "--image", image, "--boxd", boxd, "--port-file", portFile,@@ -179,6 +187,8 @@ export function upArgs({ box, store, image, boxd, portFile, root, croot, runFoldif (shadows && shadowStore) {args.push("--shadow-store", shadowStore);for (const bind of shadows.binds ?? []) args.push("--shadow", `${bind.id ?? shadowId(bind.path)}=${bind.target}`);+ // What boxd may shadow mid-chat (required.json): boxrun removes such a folder after a kill only when it fits these.+ if (shadows.binds?.length) for (const rule of lazyShadowRules()) args.push("--shadow-lazy", `${rule.dir}=${rule.markers.join(",")}`);}return args;}diff --git a/apps/server/src/box/box-shadows.test.ts b/apps/server/src/box/box-shadows.test.tsindex 1dbbe8e7..cbfb6725 100644--- a/apps/server/src/box/box-shadows.test.ts+++ b/apps/server/src/box/box-shadows.test.ts@@ -13,6 +13,7 @@ import path from "node:path";import { detectBackend, driverFor } from "./backends/index.js";import { upArgs } from "./backends/bwrap.js";import { Box, dependencyFolders, recordFile } from "./box.js";+import { boxShadows } from "./launch.js";import { BOX_NOTE } from "./plugin-lib.js";import { REQUIRED_RULES, shadowId, shadowPlan, shadowRecord, shadowsApply, shadowsNotice } from "./required.js";import type { RunCommand } from "./run.js";@@ -94,7 +95,7 @@ describe("build-artefact shadows: the backends", () => {const base = { box: "/b", store: "/s", image: "i", boxd: "/d", portFile: "/p", root: "/r", croot: "/r", runFolder: "/run", token: "t" };expect(upArgs(base).includes("--shadow-store")).toBe(false);const args = upArgs({ ...base, shadows: { binds: [{ path: "node_modules", existed: false, id: "0123456789ab", target: "/r/node_modules" }] }, shadowStore: "/var/lib/omnirush/shadows/k" });- expect(args.slice(args.indexOf("--shadow-store"))).toEqual(["--shadow-store", "/var/lib/omnirush/shadows/k", "--shadow", "0123456789ab=/r/node_modules"]);+ expect(args.slice(args.indexOf("--shadow-store"))).toEqual(["--shadow-store", "/var/lib/omnirush/shadows/k", "--shadow", "0123456789ab=/r/node_modules", "--shadow-lazy", "node_modules=package.json"]);});test("only the WSL distro applies shadows; under required on Windows it comes first", async () => {@@ -149,4 +150,42 @@ describe("build-artefact shadows: a box", () => {expect("shadows" in plain.describe()).toBe(false);expect(JSON.parse(readFileSync(recordFile(home, "p"), "utf8")).shadows_note).toBeUndefined();});++ test("shadows only under required: a Windows WSL box (or the Mac VM) that is not required gets none, no line, no record key (CLI: the same)", () => {+ const root = tmp("noshadow");+ writeFileSync(path.join(root, "package.json"), "{}");+ mkdirSync(path.join(root, "node_modules"));+ for (const [platform, runtime] of [["win32", { name: "wsl", backend: "wsl", bin: "wsl.exe" }], ["darwin", { name: "lima", backend: "lima", bin: "limactl" }]] as const) {+ const plain = boxShadows({ required: false, runtime, root, platform, env: {} });+ expect(plain).toBeNull();+ expect(shadowsNotice(plain, platform)).toBeNull();+ const req = boxShadows({ required: true, runtime, root, platform, env: {} });+ expect(req?.binds.some((b) => b.path === "node_modules")).toBe(true);+ }+ const home = tmp("noshadow-home");+ const runFolder = path.join(home, "run", "k");+ mkdirSync(runFolder, { recursive: true });+ const box = new Box({ key: "k", name: "omnirush-box-k", root, croot: root, runtime: { name: "wsl", backend: "wsl", bin: "wsl.exe" }, home, runFolder, run: down, log: () => {}, platform: "win32", env: {} });+ expect("shadows" in box.describe()).toBe(false);+ });++ test("what a checkpoint leaves out follows the shadows the box really has (boxrun's answer), not this run's plan (CLI: the same)", () => {+ const root = tmp("opened");+ writeFileSync(path.join(root, "package.json"), "{}");+ mkdirSync(path.join(root, "node_modules"));+ mkdirSync(path.join(root, "web", "node_modules"), { recursive: true });+ const home = tmp("opened-home");+ const runFolder = path.join(home, "run", "k");+ mkdirSync(runFolder, { recursive: true });+ const box = new Box({ key: "k", name: "omnirush-box-k", root, croot: root, runtime: { name: "wsl", backend: "wsl", bin: "wsl.exe" }, home, runFolder, run: down, log: () => {}, platform: "win32", env: {} });+ const plan = shadowPlan(root, { platform: "win32", croot: root });+ box.useShadows({ ...plan, binds: plan.binds.filter((b) => b.path === "node_modules") }, [`${root}/node_modules`, `${root}/web/node_modules`, "/elsewhere/node_modules"]);+ const skip = new Set(box.currentShadows()!.binds.map((b) => b.path));+ expect([...skip].sort()).toEqual(["node_modules", "web/node_modules"]);+ expect(dependencyFolders(root, { skip })).toEqual([]);+ const later = new Box({ key: "k", name: "omnirush-box-k", root, croot: root, runtime: { name: "wsl", backend: "wsl", bin: "wsl.exe" }, home, runFolder, run: down, log: () => {}, platform: "win32", env: {} });+ later.useShadows({ binds: [], env: {}, dirs: [], scanned: 0, truncated: false, ms: 0 }, [`${root}/web/node_modules`], { lazy: false });+ expect(later.currentShadows()!.binds.map((b) => b.path)).toEqual(["web/node_modules"]);+ expect(JSON.parse(readFileSync(path.join(runFolder, "shadows.json"), "utf8")).lazy).toEqual([]);+ });});diff --git a/apps/server/src/box/box.ts b/apps/server/src/box/box.tsindex 22c9398d..7b99171c 100644--- a/apps/server/src/box/box.ts+++ b/apps/server/src/box/box.ts@@ -259,8 +259,11 @@ export async function openBox({ root, runtime, env = process.env, platform = proelse log("info", "box made before the install log: its wrappers are not in this box (Reset sandbox adds them)", { key });}try { box.useGitIdentity(); } catch (error) { log("warn", "box git identity not set", { error: error instanceof Error ? error.message : String(error) }); }- // The shadows boxrun really bound (a box another start made before keeps its own set until it stops).- if (wanted) box.useShadows(wanted, opened.shadows ?? null);+ // The shadows boxrun really bound (a box another start made before keeps its own set until it stops):+ // what a checkpoint leaves out follows those, whatever this plan said.+ const bound = Array.isArray(opened.shadows) ? opened.shadows.filter((t): t is string => typeof t === "string") : null;+ if (wanted || bound?.length) box.useShadows(wanted ?? { binds: [], env: {}, dirs: [], scanned: 0, truncated: false, ms: 0 }, bound, { lazy: Boolean(wanted) });+ else rmSync(path.join(runFolder, "shadows.json"), { force: true });box.setState("ready", null, onState);return box;} catch (error) {@@ -509,12 +512,15 @@ export class Box {* command; shadows.json in the run folder tells boxd which folders are* shadowed and what to bind when a new package appears mid-chat. CLI: the same.*/- useShadows(plan: ShadowPlan, applied: string[] | null = null): void {- const set = Array.isArray(applied) ? new Set(applied) : null;- const binds = plan.binds.filter((b) => !set || set.has(b.target));+ useShadows(plan: ShadowPlan, applied: string[] | null = null, { lazy: mid = true }: { lazy?: boolean } = {}): void {+ // `applied` (boxrun's answer) wins over the plan: a box started before keeps the set it was made with.+ const binds = Array.isArray(applied)+ ? applied.map((target) => plan.binds.find((b) => b.target === target) ?? this.shadowOf(target)).filter((b): b is ShadowBind => b !== null)+ : plan.binds;this.shadows = { ...plan, binds, dirs: [...new Set(binds.map((b) => b.path.split("/").pop()!))] };const rules = REQUIRED_RULES.shadows;- const lazy = rules.binds.filter((rule) => rule.even_if_absent).map((rule) => ({ dir: rule.dir, when_any: rule.when_any }));+ // Folders shadowed mid-chat only under the plan's rules (the required sandbox); none otherwise.+ const lazy = mid ? rules.binds.filter((rule) => rule.even_if_absent).map((rule) => ({ dir: rule.dir, when_any: rule.when_any })) : [];writeJson(path.join(this.runFolder, "shadows.json"), { v: 1, root: this.croot, store: rules.store, active: binds.map((b) => b.target), lazy, limits: rules.limits, skip_dirs: rules.skip_dirs }, 0o644);// The env file keeps what the wrappers and the git identity wrote; the redirections are set once.const names = Object.keys(plan.env ?? {});@@ -526,6 +532,13 @@ export class Box {this.writeRecord();}+ /** A bound folder (inside path) the plan does not list: its project-relative path, or null outside the project. */+ shadowOf(target: string): ShadowBind | null {+ const rel = path.posix.relative(this.croot, path.posix.normalize(String(target)));+ if (!rel || rel.startsWith("..") || path.posix.isAbsolute(rel)) return null;+ return { path: rel, existed: false, target: `${this.croot}/${rel}` };+ }+/** The shadows in use now: the start's, plus the folders boxd bound mid-chat (shadows-lazy.json in the run folder). */currentShadows(): ShadowPlan | null {if (!this.shadows) return null;diff --git a/apps/server/src/box/launch.ts b/apps/server/src/box/launch.tsindex 9e8ba0bb..d88f4d39 100644--- a/apps/server/src/box/launch.ts+++ b/apps/server/src/box/launch.ts@@ -334,6 +334,20 @@ export type BoxPlan = ReadyPlan | NativePlan;/** How long a plan under required waits for the backend to answer before it is "cannot run" (cold WSL / VM start). */export const REQUIRED_REDETECT_MS = 45_000;+/**+ * The box's own build folders (required.ts shadowPlan), found now (bounded)+ * so the project's one line can say so: under the required sandbox only, on+ * macOS / Windows, with a backend that runs boxrun as root (the WSL distro,+ * the Lima VM). Else null (as before the required sandbox).+ */+export function boxShadows({ required, runtime, root, platform = process.platform, env = process.env, stateDir = null, log = () => {} }: { required: boolean; runtime: BoxRuntime; root: string; platform?: NodeJS.Platform; env?: NodeJS.ProcessEnv; stateDir?: string | null; log?: BoxLog }): ShadowPlan | null {+ if (!required || !driverFor(runtime).shadows || !shadowsApply(platform, env)) return null;+ let shadows: ShadowPlan | null = null;+ try { shadows = shadowPlan(root, { platform, croot: containerRoot(root, platform), stateDir, env }); } catch (error) { log("warn", "box shadows scan failed", { error: error instanceof Error ? error.message : String(error) }); }+ if (shadows) log("info", "box shadows", { binds: shadows.binds.length, env: Object.keys(shadows.env), ms: shadows.ms, truncated: shadows.truncated });+ return shadows;+}+/*** Plan the box of the project at `root` (the project's box is on). Returns* { box: null, notice } when its chats run on this machine for now (no@@ -344,7 +358,7 @@ export async function planBox({ env = process.env, home, root, log = () => {}, rconst bounded = <T>(promise: Promise<T>, ms: number, fallback: T): Promise<T> => Promise.race([promise, new Promise<T>((resolve) => setTimeout(() => resolve(fallback), ms))]);// Under required the chat waits for the box anyway: a backend slow to answer (a WSL VM booting) is not "cannot run".const detectMs = required ? Math.max(probeMs, REQUIRED_REDETECT_MS) : probeMs;- const runtime = await bounded<RuntimeResult>(detectRuntime({ boxHome: home, env, run, ...(asked ? { asked } : {}), allow, ...(preferShadows ? { preferShadows } : {}) }), detectMs, { ok: false, reason: "the sandbox backend did not answer in time" });+ const runtime = await bounded<RuntimeResult>(detectRuntime({ boxHome: home, env, run, ...(asked ? { asked } : {}), allow, ...(preferShadows ? { preferShadows } : {}), ...(required ? { required } : {}) }), detectMs, { ok: false, reason: "the sandbox backend did not answer in time" });if (!runtime.ok) {log("info", "box unavailable", { reason: runtime.reason });return { box: null, notice: `Sandbox off for now: ${runtime.reason}.`, runtime };@@ -372,13 +386,7 @@ export async function planBox({ env = process.env, home, root, log = () => {}, rlet opening: Promise<Box> | null = null;let phase: string | null = null;let startedWith: BoxInstallLog | null = null;- // macOS / Windows with a backend that runs boxrun as root (the WSL distro): the box's own build folders- // (required.ts shadowPlan), found now (bounded) so the project's one line can say so.- let shadows: ShadowPlan | null = null;- if (driver.shadows && shadowsApply(platform, env)) {- try { shadows = shadowPlan(root, { platform, croot: containerRoot(root, platform), stateDir, env }); } catch (error) { log("warn", "box shadows scan failed", { error: error instanceof Error ? error.message : String(error) }); }- if (shadows) log("info", "box shadows", { binds: shadows.binds.length, env: Object.keys(shadows.env), ms: shadows.ms, truncated: shadows.truncated });- }+ const shadows = boxShadows({ required, runtime, root, platform, env, stateDir, log });const start = ({ pkglog = null }: { pkglog?: BoxInstallLog | null } = {}): Promise<Box> => {if (!opening) startedWith = pkglog;opening ??= openBox({ root, runtime, env, home, log, pull: false, shims, pkglog, revive: true, shadows, platform, onState: (state) => { phase = state; } });@@ -498,7 +506,7 @@ export type BoxRun = {* "off"; the shell runs on this machine, its chats are not labelled); later* failures are logged and that turn has no checkpoint.*/-export function boxRun(plan: ReadyPlan, { log = () => {}, readyWaitMs = 30_000, onNative = () => {}, onRevive = () => {}, onPort = () => {}, tmpRoot, pkglog = null, pollMs = REVIVE_POLL_MS, ping = client.ping, env = process.env, forwards: makeForwards = portForwards }: { log?: BoxLog; readyWaitMs?: number; onNative?: (reason: string) => void; onRevive?: (event: { ok: boolean; why: string; error?: string }) => void; onPort?: (event: { port: number; open: boolean; line?: string; why?: string }) => void; tmpRoot?: string; pkglog?: BoxInstallLog | null; pollMs?: number; ping?: typeof client.ping; env?: NodeJS.ProcessEnv; forwards?: (options: PortForwardOptions) => PortForwards } = {}): BoxRun {+export function boxRun(plan: ReadyPlan, { log = () => {}, readyWaitMs = 30_000, onNative = () => {}, onRevive = () => {}, onPort = () => {}, tmpRoot, pkglog = null, pollMs = REVIVE_POLL_MS, ping = client.ping, env = process.env, forwards: makeForwards = portForwards, required = false }: { required?: boolean; log?: BoxLog; readyWaitMs?: number; onNative?: (reason: string) => void; onRevive?: (event: { ok: boolean; why: string; error?: string }) => void; onPort?: (event: { port: number; open: boolean; line?: string; why?: string }) => void; tmpRoot?: string; pkglog?: BoxInstallLog | null; pollMs?: number; ping?: typeof client.ping; env?: NodeJS.ProcessEnv; forwards?: (options: PortForwardOptions) => PortForwards } = {}): BoxRun {const { key, home, runtime, root } = plan.box;let box: Box | null = null;let native = false;@@ -510,7 +518,8 @@ export function boxRun(plan: ReadyPlan, { log = () => {}, readyWaitMs = 30_000,const opening = plan.start({ pkglog }).then((opened) => { box = opened; return opened; });// A box with its own network (Docker, Podman): its servers at the same port on this computer's 127.0.0.1// (ports.ts), polled while the box is usable (none before it is ready, after a failure, once let go).- const wantsForwards = driverFor(runtime).forwardsPorts === false && String(env.OMNIRUSH_BOX_PORTS ?? "").trim().toLowerCase() !== "off";+ // Under the required sandbox only (a chat that would have run here keeps its servers reachable); OMNIRUSH_BOX_PORTS=off: never.+ const wantsForwards = required && driverFor(runtime).forwardsPorts === false && String(env.OMNIRUSH_BOX_PORTS ?? "").trim().toLowerCase() !== "off";const forwards = wantsForwards ? makeForwards({endpoint: () => { const current: Box | null = box; return current?.ready && !native && !closed ? current.endpoint : null; },log,