Skip to content

A desktop coding agent with free access to frontier models.

TypeScript

feat: sync box helpers with the cli

ogsamratbad35e2bdf31
diff --git a/apps/desktop/box/go/cmd/boxd/main.go b/apps/desktop/box/go/cmd/boxd/main.go
index 3632ad14..a8c4560a 100644
--- a/apps/desktop/box/go/cmd/boxd/main.go
+++ b/apps/desktop/box/go/cmd/boxd/main.go
@@ -19,6 +19,7 @@
// BOXD_PORT_FILE where to write the port it listens on (BOXD_LISTEN may end in :0)
// BOXD_IDLE_EXIT minutes with no request after which it exits (0: never; the
// bubblewrap and WSL boxes, which have no engine to keep them)
+// BOXD_SHADOWS the build folders boxrun init shadowed (JSON list; shadows.go)
package main
import (
@@ -63,6 +64,7 @@ type config struct {
portFile string
idle time.Duration
baseEnv []string
+ shadows string
}
// snapshotGate lets a snapshot read the files with no new command starting
@@ -138,6 +140,7 @@ func main() {
os.Exit(runInit())
}
cfg := loadConfig()
+ initShadows(cfg, cfg.shadows)
log.SetFlags(log.LstdFlags | log.Lmicroseconds)
log.SetPrefix("boxd ")
// The command user first, then the image's file index (once per
@@ -154,6 +157,7 @@ func main() {
if err != nil {
log.Fatalf("listen: %v", err)
}
+ ownPort.Store(int64(ln.Addr().(*net.TCPAddr).Port))
if cfg.portFile != "" {
port := ln.Addr().(*net.TCPAddr).Port
tmp := cfg.portFile + ".tmp"
@@ -168,6 +172,8 @@ func main() {
signal.Notify(sigs, syscall.SIGTERM, syscall.SIGINT)
go func() {
<-sigs
+ // Before the listener closes: main returns as soon as it does.
+ removeMade()
_ = ln.Close()
os.Exit(0)
}()
@@ -208,6 +214,7 @@ func idleExit(idle time.Duration, ln net.Listener) {
time.Sleep(idle / 20)
if activity.Load() == 0 && time.Since(time.Unix(0, lastActive.Load())) > idle {
log.Printf("idle for %v: exiting", idle)
+ removeMade()
_ = ln.Close()
os.Exit(0)
}
@@ -238,6 +245,7 @@ func loadConfig() config {
shell: os.Getenv("BOXD_SHELL"),
portFile: os.Getenv("BOXD_PORT_FILE"),
idle: time.Duration(atoi("BOXD_IDLE_EXIT")) * time.Minute,
+ shadows: os.Getenv("BOXD_SHADOWS"),
}
if cfg.shell == "" {
cfg.shell = "/bin/bash"
@@ -251,7 +259,7 @@ func loadConfig() config {
}
cfg.baseEnv = append(cfg.baseEnv, kv)
}
- for _, k := range []string{"BOXD_TOKEN", "BOXD_LISTEN", "BOXD_UID", "BOXD_GID", "BOXD_HOME", "BOXD_ROOT", "BOXD_PATH", "BOXD_ENV_FILE", "BOXD_SHELL", "BOXD_PORT_FILE", "BOXD_IDLE_EXIT"} {
+ for _, k := range []string{"BOXD_TOKEN", "BOXD_LISTEN", "BOXD_UID", "BOXD_GID", "BOXD_HOME", "BOXD_ROOT", "BOXD_PATH", "BOXD_ENV_FILE", "BOXD_SHELL", "BOXD_PORT_FILE", "BOXD_IDLE_EXIT", "BOXD_SHADOWS"} {
_ = os.Unsetenv(k)
}
if cfg.token == "" {
@@ -400,6 +408,14 @@ func serve(cfg config, raw net.Conn) {
c.exit(proto.ExitInfo{Ms: ms(start)})
case "exec":
runExec(cfg, c, req, start)
+ case "ports":
+ // The box's listening TCP ports (ports.go), for the launcher's forwards.
+ out, _ := json.Marshal(listeners())
+ _ = c.frame(proto.Data, out)
+ c.exit(proto.ExitInfo{Ms: ms(start)})
+ case "connect":
+ // A TCP stream to a port inside the box (ports.go): raw bytes after one Data frame.
+ connectPort(c, req, start)
case "stat":
out, _ := json.Marshal(statPaths(req.Paths))
_ = c.frame(proto.Data, out)
@@ -553,6 +569,8 @@ func runExec(cfg config, c *conn, req proto.Request, start time.Time) {
defer gate.leave()
waited := ms(waitStart)
+ // A package made since the last command gets its own node_modules before this one runs (shadows.go).
+ checkShadows(cfg)
argv := req.Argv
if argv[0] == "" || argv[0] == "$SHELL" {
argv = append([]string{cfg.shell}, argv[1:]...)
@@ -739,6 +757,13 @@ func walkPaths(roots []string, max int) ([]proto.StatEntry, bool) {
truncated = true
return filepath.SkipAll
}
+ // The box's own build folders are never in a snapshot (a rebuild makes them from the manifests).
+ if shadowed(p) {
+ if d != nil && d.IsDir() {
+ return filepath.SkipDir
+ }
+ return nil
+ }
paths = append(paths, p)
return nil
})
diff --git a/apps/desktop/box/go/cmd/boxd/ports.go b/apps/desktop/box/go/cmd/boxd/ports.go
new file mode 100644
index 00000000..da29d0fd
--- /dev/null
+++ b/apps/desktop/box/go/cmd/boxd/ports.go
@@ -0,0 +1,231 @@
+//go:build linux
+
+// Ports the box listens on, and TCP streams to them, for backends whose box
+// has its own network (Docker, Podman): the launcher opens the same port on
+// this machine's 127.0.0.1 and carries each connection through boxd, so a
+// dev server the agent starts in the box answers in the user's browser.
+//
+// op "ports": one Data frame, the JSON list of listening TCP sockets
+// (/proc/net/tcp and tcp6), boxd's own port left out
+// op "connect": dial Port inside the box; on success one Data frame
+// {"ok":true}, then raw bytes both ways until either side
+// closes; on failure a Fail frame and an Exit frame
+package main
+
+import (
+ "bufio"
+ "encoding/hex"
+ "encoding/json"
+ "errors"
+ "fmt"
+ "io"
+ "net"
+ "os"
+ "sort"
+ "strconv"
+ "strings"
+ "sync"
+ "sync/atomic"
+ "time"
+
+ "omnirush.ai/box/proto"
+)
+
+// ownPort is the port boxd listens on (never forwarded).
+var ownPort atomic.Int64
+
+// procNetFiles are read for listeners (a missing one, no IPv6, is skipped).
+var procNetFiles = []string{"/proc/net/tcp", "/proc/net/tcp6"}
+
+// tcpListen is the socket state LISTEN in /proc/net/tcp.
+const tcpListen = "0A"
+
+// listeners lists the box's listening TCP sockets, one per port (the widest
+// address when a port is bound more than once), sorted by port.
+func listeners() []proto.Listener {
+ byPort := map[int]proto.Listener{}
+ for _, file := range procNetFiles {
+ f, err := os.Open(file)
+ if err != nil {
+ continue
+ }
+ for _, l := range parseProcNet(f) {
+ if int64(l.Port) == ownPort.Load() {
+ continue
+ }
+ if old, ok := byPort[l.Port]; !ok || addrRank(l.Addr) < addrRank(old.Addr) {
+ byPort[l.Port] = l
+ }
+ }
+ f.Close()
+ }
+ out := make([]proto.Listener, 0, len(byPort))
+ for _, l := range byPort {
+ out = append(out, l)
+ }
+ sort.Slice(out, func(i, j int) bool { return out[i].Port < out[j].Port })
+ return out
+}
+
+// addrRank: any-address first, then loopback, then the rest.
+func addrRank(addr string) int {
+ ip := net.ParseIP(addr)
+ switch {
+ case ip == nil:
+ return 3
+ case ip.IsUnspecified():
+ return 0
+ case ip.IsLoopback():
+ return 1
+ default:
+ return 2
+ }
+}
+
+// parseProcNet reads the LISTEN rows of /proc/net/tcp or tcp6.
+func parseProcNet(r io.Reader) []proto.Listener {
+ var out []proto.Listener
+ sc := bufio.NewScanner(r)
+ sc.Buffer(make([]byte, 64<<10), 1<<20)
+ first := true
+ for sc.Scan() {
+ if first {
+ first = false // the header row
+ continue
+ }
+ fields := strings.Fields(sc.Text())
+ if len(fields) < 4 || fields[3] != tcpListen {
+ continue
+ }
+ host, portHex, ok := strings.Cut(fields[1], ":")
+ if !ok {
+ continue
+ }
+ port, err := strconv.ParseUint(portHex, 16, 16)
+ if err != nil || port == 0 {
+ continue
+ }
+ ip, err := procIP(host)
+ if err != nil {
+ continue
+ }
+ out = append(out, proto.Listener{Port: int(port), Addr: ip.String()})
+ }
+ return out
+}
+
+// procIP decodes /proc/net's address: 32-bit words in host (little-endian) order.
+func procIP(text string) (net.IP, error) {
+ raw, err := hex.DecodeString(text)
+ if err != nil || (len(raw) != 4 && len(raw) != 16) {
+ return nil, errors.New("bad address")
+ }
+ ip := make(net.IP, len(raw))
+ for w := 0; w < len(raw); w += 4 {
+ ip[w], ip[w+1], ip[w+2], ip[w+3] = raw[w+3], raw[w+2], raw[w+1], raw[w]
+ }
+ if v4 := ip.To4(); v4 != nil {
+ return v4, nil
+ }
+ return ip, nil
+}
+
+// dialTargets are the addresses inside the box a connect to `port` tries, in order.
+func dialTargets(port int, ls []proto.Listener) []string {
+ seen := map[string]bool{}
+ var out []string
+ add := func(host string) {
+ addr := net.JoinHostPort(host, strconv.Itoa(port))
+ if !seen[addr] {
+ seen[addr] = true
+ out = append(out, addr)
+ }
+ }
+ for _, l := range ls {
+ if l.Port != port {
+ continue
+ }
+ ip := net.ParseIP(l.Addr)
+ switch {
+ case ip == nil:
+ case ip.IsUnspecified() && ip.To4() != nil:
+ add("127.0.0.1")
+ case ip.IsUnspecified():
+ add("127.0.0.1")
+ add("::1")
+ default:
+ add(ip.String())
+ }
+ }
+ add("127.0.0.1")
+ add("::1")
+ return out
+}
+
+// connectPort dials the port inside the box and joins it to the client's connection.
+func connectPort(c *conn, req proto.Request, start time.Time) {
+ if req.Port <= 0 || req.Port > 65535 {
+ c.fail(start, fmt.Errorf("bad port %d", req.Port))
+ return
+ }
+ if int64(req.Port) == ownPort.Load() {
+ c.fail(start, errors.New("that is the sandbox helper's own port"))
+ return
+ }
+ var target net.Conn
+ var lastErr error
+ for _, addr := range dialTargets(req.Port, listeners()) {
+ target, lastErr = net.DialTimeout("tcp", addr, 3*time.Second)
+ if lastErr == nil {
+ break
+ }
+ }
+ if target == nil {
+ c.fail(start, fmt.Errorf("nothing answers on port %d in the sandbox: %v", req.Port, lastErr))
+ return
+ }
+ defer target.Close()
+ if tcp, ok := target.(*net.TCPConn); ok {
+ _ = tcp.SetNoDelay(true)
+ }
+ ok, _ := json.Marshal(map[string]any{"ok": true})
+ if err := c.frame(proto.Data, ok); err != nil {
+ return
+ }
+ pipeBoth(c, target)
+}
+
+// pipeBoth copies both ways; a side that ends its writes half-closes the
+// other, and the stream is over when both directions are.
+func pipeBoth(c *conn, target net.Conn) {
+ var wg sync.WaitGroup
+ wg.Add(2)
+ go func() {
+ defer wg.Done()
+ // c.r first: bytes the client sent right after its request line are buffered there.
+ if _, err := io.Copy(target, c.r); err != nil {
+ _ = target.Close()
+ _ = c.Conn.Close()
+ return
+ }
+ closeWrite(target)
+ }()
+ go func() {
+ defer wg.Done()
+ if _, err := io.Copy(c.Conn, target); err != nil {
+ _ = target.Close()
+ _ = c.Conn.Close()
+ return
+ }
+ closeWrite(c.Conn)
+ }()
+ wg.Wait()
+}
+
+func closeWrite(c net.Conn) {
+ if tcp, ok := c.(*net.TCPConn); ok {
+ _ = tcp.CloseWrite()
+ return
+ }
+ _ = c.Close()
+}
diff --git a/apps/desktop/box/go/cmd/boxd/ports_test.go b/apps/desktop/box/go/cmd/boxd/ports_test.go
new file mode 100644
index 00000000..610064ae
--- /dev/null
+++ b/apps/desktop/box/go/cmd/boxd/ports_test.go
@@ -0,0 +1,169 @@
+//go:build linux
+
+package main
+
+import (
+ "bufio"
+ "encoding/json"
+ "io"
+ "net"
+ "strconv"
+ "strings"
+ "testing"
+ "time"
+
+ "omnirush.ai/box/proto"
+)
+
+const procTCP = ` sl local_address rem_address st tx_queue rx_queue tr tm->when retrnsmt uid timeout inode
+ 0: 00000000:2238 00000000:0000 0A 00000000:00000000 00:00000000 00000000 1000 0 1 1 0000000000000000 100 0 0 10 0
+ 1: 0100007F:1F90 00000000:0000 0A 00000000:00000000 00:00000000 00000000 1000 0 2 1 0000000000000000 100 0 0 10 0
+ 2: 0100007F:1F90 0100007F:D2F0 01 00000000:00000000 00:00000000 00000000 1000 0 3 1 0000000000000000 20 4 30 10 -1
+ 3: 1600640A:1E61 00000000:0000 0A 00000000:00000000 00:00000000 00000000 0 0 4 1 0000000000000000 100 0 0 10 0
+`
+
+const procTCP6 = ` sl local_address remote_address st tx_queue rx_queue tr tm->when retrnsmt uid timeout inode
+ 0: 00000000000000000000000000000000:0BB8 00000000000000000000000000000000:0000 0A 00000000:00000000 00:00000000 00000000 1000 0 5 1 0000000000000000 100 0 0 10 0
+ 1: 00000000000000000000000001000000:13BA 00000000000000000000000000000000:0000 0A 00000000:00000000 00:00000000 00000000 1000 0 6 1 0000000000000000 100 0 0 10 0
+ 2: 0000000000000000FFFF00000100007F:0050 00000000000000000000000000000000:0000 0A 00000000:00000000 00:00000000 00000000 1000 0 7 1 0000000000000000 100 0 0 10 0
+`
+
+func TestParseProcNet(t *testing.T) {
+ got := parseProcNet(strings.NewReader(procTCP))
+ want := []proto.Listener{{Port: 8760, Addr: "0.0.0.0"}, {Port: 8080, Addr: "127.0.0.1"}, {Port: 7777, Addr: "10.100.0.22"}}
+ if len(got) != len(want) {
+ t.Fatalf("%v", got)
+ }
+ for i := range want {
+ if got[i] != want[i] {
+ t.Fatalf("%d: %v want %v", i, got[i], want[i])
+ }
+ }
+ got6 := parseProcNet(strings.NewReader(procTCP6))
+ want6 := []proto.Listener{{Port: 3000, Addr: "::"}, {Port: 5050, Addr: "::1"}, {Port: 80, Addr: "127.0.0.1"}}
+ if len(got6) != len(want6) {
+ t.Fatalf("%v", got6)
+ }
+ for i := range want6 {
+ if got6[i] != want6[i] {
+ t.Fatalf("%d: %v want %v", i, got6[i], want6[i])
+ }
+ }
+}
+
+func TestDialTargets(t *testing.T) {
+ ls := []proto.Listener{{Port: 3000, Addr: "::"}, {Port: 5050, Addr: "::1"}, {Port: 9000, Addr: "10.1.2.3"}}
+ if got := strings.Join(dialTargets(3000, ls), " "); got != "127.0.0.1:3000 [::1]:3000" {
+ t.Fatal(got)
+ }
+ if got := strings.Join(dialTargets(5050, ls), " "); got != "[::1]:5050 127.0.0.1:5050" {
+ t.Fatal(got)
+ }
+ if got := strings.Join(dialTargets(9000, ls), " "); got != "10.1.2.3:9000 127.0.0.1:9000 [::1]:9000" {
+ t.Fatal(got)
+ }
+ if got := strings.Join(dialTargets(1, nil), " "); got != "127.0.0.1:1 [::1]:1" {
+ t.Fatal(got)
+ }
+}
+
+// An echo server that upper-cases, and closes after the client half-closes.
+func upperServer(t *testing.T) (int, net.Listener) {
+ t.Helper()
+ ln, err := net.Listen("tcp", "127.0.0.1:0")
+ if err != nil {
+ t.Fatal(err)
+ }
+ t.Cleanup(func() { ln.Close() })
+ go func() {
+ for {
+ c, err := ln.Accept()
+ if err != nil {
+ return
+ }
+ go func(c net.Conn) {
+ defer c.Close()
+ data, _ := io.ReadAll(c)
+ _, _ = c.Write([]byte(strings.ToUpper(string(data))))
+ }(c)
+ }
+ }()
+ return ln.Addr().(*net.TCPAddr).Port, ln
+}
+
+func TestPortsListsListener(t *testing.T) {
+ _, addr := startDaemon(t)
+ port, _ := upperServer(t)
+ res := call(t, addr, proto.Request{Op: "ports"}, nil)
+ var ls []proto.Listener
+ if err := json.Unmarshal(res.data, &ls); err != nil {
+ t.Fatal(err, string(res.data))
+ }
+ found := false
+ for _, l := range ls {
+ if l.Port == port && l.Addr == "127.0.0.1" {
+ found = true
+ }
+ }
+ if !found {
+ t.Fatalf("port %d not in %v", port, ls)
+ }
+}
+
+func TestConnectStreams(t *testing.T) {
+ _, addr := startDaemon(t)
+ port, _ := upperServer(t)
+ c, err := net.Dial("tcp", addr)
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer c.Close()
+ // The request and the first bytes in one write: boxd keeps what it buffered past the line.
+ line, _ := json.Marshal(proto.Request{V: proto.Version, Token: "tok", Op: "connect", Port: port})
+ if _, err := c.Write(append(append(line, '\n'), []byte("hello ")...)); err != nil {
+ t.Fatal(err)
+ }
+ r := bufio.NewReader(c)
+ k, p, err := proto.ReadFrame(r, nil)
+ if err != nil || k != proto.Data || !strings.Contains(string(p), `"ok":true`) {
+ t.Fatal(k, string(p), err)
+ }
+ _, _ = c.Write([]byte("box"))
+ _ = c.(*net.TCPConn).CloseWrite()
+ _ = c.SetReadDeadline(time.Now().Add(5 * time.Second))
+ got, err := io.ReadAll(r)
+ if err != nil || string(got) != "HELLO BOX" {
+ t.Fatalf("%q %v", got, err)
+ }
+}
+
+func TestConnectNothingThere(t *testing.T) {
+ _, addr := startDaemon(t)
+ ln, _ := net.Listen("tcp", "127.0.0.1:0")
+ port := ln.Addr().(*net.TCPAddr).Port
+ ln.Close()
+ res := call(t, addr, proto.Request{Op: "connect", Port: port}, nil)
+ if res.exit.Code == 0 || !strings.Contains(res.err, "nothing answers") {
+ t.Fatalf("%d %q", res.exit.Code, res.err)
+ }
+ res = call(t, addr, proto.Request{Op: "connect", Port: 0}, nil)
+ if res.exit.Code == 0 || !strings.Contains(res.err, "bad port") {
+ t.Fatalf("%d %q", res.exit.Code, res.err)
+ }
+}
+
+func TestConnectNotOwnPort(t *testing.T) {
+ _, addr := startDaemon(t)
+ _, p, _ := net.SplitHostPort(addr)
+ own, _ := strconv.Atoi(p)
+ ownPort.Store(int64(own))
+ t.Cleanup(func() { ownPort.Store(0) })
+ res := call(t, addr, proto.Request{Op: "connect", Port: own}, nil)
+ if res.exit.Code == 0 || !strings.Contains(res.err, "own port") {
+ t.Fatalf("%d %q", res.exit.Code, res.err)
+ }
+ res = call(t, addr, proto.Request{Op: "ports"}, nil)
+ if strings.Contains(string(res.data), `"port":`+p+`,`) {
+ t.Fatalf("own port listed: %s", res.data)
+ }
+}
diff --git a/apps/desktop/box/go/cmd/boxd/shadows.go b/apps/desktop/box/go/cmd/boxd/shadows.go
new file mode 100644
index 00000000..6a6c1e3b
--- /dev/null
+++ b/apps/desktop/box/go/cmd/boxd/shadows.go
@@ -0,0 +1,296 @@
+//go:build linux
+
+package main
+
+// Build-artefact shadows (src/box/required.js; macOS/Windows hosts, boxes
+// that boxrun starts as root: the WSL distro, the Lima VM). boxrun init binds
+// the box's own copy of each build folder found at the start (node_modules,
+// .venv, target, ...) over the project's, from the box-local store mounted
+// at shadowStore. Here, before each command, a cheap bounded look for a new
+// package (a package.json whose node_modules is not shadowed yet) binds one
+// for it too, so `npm install` in a package made mid-chat never writes this
+// machine's folder. Shadowed folders are never walked for a snapshot. The
+// folders made on the host only to be mount points are removed when boxd
+// ends (and by `boxrun stop` / the next `boxrun up` if it did not end cleanly).
+
+import (
+ "crypto/sha256"
+ "encoding/hex"
+ "encoding/json"
+ "os"
+ "path/filepath"
+ "strings"
+ "sync"
+ "syscall"
+ "time"
+)
+
+// shadowStore: the box's own store inside the box (required.json shadows.store).
+const shadowStore = "/var/lib/omnirush/shadows"
+
+// madeFile lists the mount points made on the host (inside paths), for their removal.
+const madeFile = shadowStore + "/.made.json"
+
+// How often the look for new packages runs at most.
+const shadowCheckEvery = 2 * time.Second
+
+type shadowPlan struct {
+ V int `json:"v"`
+ Root string `json:"root"`
+ Store string `json:"store"`
+ Active []string `json:"active"`
+ Lazy []struct {
+ Dir string `json:"dir"`
+ WhenAny []string `json:"when_any"`
+ } `json:"lazy"`
+ Limits struct {
+ Entries int `json:"entries"`
+ Ms int `json:"ms"`
+ Depth int `json:"depth"`
+ Max int `json:"max"`
+ } `json:"limits"`
+ SkipDirs []string `json:"skip_dirs"`
+}
+
+type lazyBind struct {
+ Path string `json:"path"`
+}
+
+type shadowState struct {
+ mu sync.Mutex
+ active map[string]bool
+ lazy []lazyBind
+ lastCheck time.Time
+ planFile string
+ lazyFile string
+}
+
+var shadows = shadowState{active: map[string]bool{}}
+
+// shadowID is required.js shadowId: sha256 of the project-relative POSIX path ("." for the root), 12 hex digits.
+func shadowID(rel string) string {
+ parts := []string{}
+ for _, p := range strings.Split(filepath.ToSlash(rel), "/") {
+ if p != "" && p != "." {
+ parts = append(parts, p)
+ }
+ }
+ clean := strings.Join(parts, "/")
+ if clean == "" {
+ clean = "."
+ }
+ sum := sha256.Sum256([]byte(clean))
+ return hex.EncodeToString(sum[:])[:12]
+}
+
+// initShadows takes the start's shadows (BOXD_SHADOWS, JSON list of inside paths, set by boxrun init)
+// and where the launcher's plan is (shadows.json next to the env file).
+func initShadows(cfg config, start string) {
+ shadows.mu.Lock()
+ defer shadows.mu.Unlock()
+ if start != "" {
+ var list []string
+ if json.Unmarshal([]byte(start), &list) == nil {
+ for _, p := range list {
+ shadows.active[filepath.Clean(p)] = true
+ }
+ }
+ }
+ if cfg.envFile != "" {
+ dir := filepath.Dir(cfg.envFile)
+ shadows.planFile = filepath.Join(dir, "shadows.json")
+ shadows.lazyFile = filepath.Join(dir, "shadows-lazy.json")
+ }
+}
+
+// shadowed: whether p is a shadowed folder or in one.
+func shadowed(p string) bool {
+ shadows.mu.Lock()
+ defer shadows.mu.Unlock()
+ for dir := range shadows.active {
+ if within(p, dir) {
+ return true
+ }
+ }
+ return false
+}
+
+func readMade() []string {
+ var list []string
+ if data, err := os.ReadFile(madeFile); err == nil {
+ _ = json.Unmarshal(data, &list)
+ }
+ return list
+}
+
+func writeMade(list []string) {
+ if list == nil {
+ list = []string{}
+ }
+ data, _ := json.Marshal(list)
+ tmp := madeFile + ".tmp"
+ if os.WriteFile(tmp, data, 0o600) == nil {
+ _ = os.Rename(tmp, madeFile)
+ }
+}
+
+// checkShadows: before a command, at most every shadowCheckEvery, bind a shadow for each new
+// package (lazy rules: a folder with one of when_any whose `dir` is not shadowed). Needs root in
+// the box's mount namespace; without a plan (other backends, Linux hosts) it does nothing.
+func checkShadows(cfg config) {
+ shadows.mu.Lock()
+ if shadows.planFile == "" || time.Since(shadows.lastCheck) < shadowCheckEvery {
+ shadows.mu.Unlock()
+ return
+ }
+ shadows.lastCheck = time.Now()
+ planFile := shadows.planFile
+ shadows.mu.Unlock()
+ data, err := os.ReadFile(planFile)
+ if err != nil {
+ return
+ }
+ var plan shadowPlan
+ if json.Unmarshal(data, &plan) != nil || plan.Root == "" || len(plan.Lazy) == 0 {
+ return
+ }
+ if _, err := os.Stat(shadowStore); err != nil {
+ return // no store in this box (started without shadows)
+ }
+ shadows.mu.Lock()
+ for _, p := range plan.Active {
+ shadows.active[filepath.Clean(p)] = true
+ }
+ count := len(shadows.active)
+ shadows.mu.Unlock()
+ if plan.Limits.Max > 0 && count >= plan.Limits.Max {
+ return
+ }
+ for _, target := range findNew(plan) {
+ bindNew(cfg, plan, target)
+ }
+}
+
+// findNew walks the project (bounded like the start's scan) for lazy-rule folders not shadowed yet.
+func findNew(plan shadowPlan) []string {
+ entries, ms, depth := plan.Limits.Entries, plan.Limits.Ms, plan.Limits.Depth
+ if entries <= 0 {
+ entries = 4000
+ }
+ if ms <= 0 {
+ ms = 150
+ }
+ if depth <= 0 {
+ depth = 4
+ }
+ skip := map[string]bool{}
+ for _, d := range plan.SkipDirs {
+ skip[d] = true
+ }
+ deadline := time.Now().Add(time.Duration(ms) * time.Millisecond)
+ seen := 0
+ var out []string
+ var visit func(dir string, level int)
+ visit = func(dir string, level int) {
+ if seen > entries || time.Now().After(deadline) {
+ return
+ }
+ f, err := os.Open(dir)
+ if err != nil {
+ return
+ }
+ list, _ := f.Readdir(-1)
+ f.Close()
+ seen += len(list) + 1
+ names := map[string]bool{}
+ for _, fi := range list {
+ if fi.Mode().IsRegular() {
+ names[fi.Name()] = true
+ }
+ }
+ for _, rule := range plan.Lazy {
+ for _, m := range rule.WhenAny {
+ if names[m] {
+ target := filepath.Join(dir, filepath.FromSlash(rule.Dir))
+ if !shadowed(target) {
+ out = append(out, target)
+ }
+ break
+ }
+ }
+ }
+ if level >= depth {
+ return
+ }
+ for _, fi := range list {
+ name := fi.Name()
+ if !fi.IsDir() || strings.HasPrefix(name, ".") || skip[name] {
+ continue
+ }
+ child := filepath.Join(dir, name)
+ if shadowed(child) {
+ continue
+ }
+ visit(child, level+1)
+ }
+ }
+ visit(plan.Root, 0)
+ return out
+}
+
+func bindNew(cfg config, plan shadowPlan, target string) {
+ rel, err := filepath.Rel(plan.Root, target)
+ if err != nil || strings.HasPrefix(rel, "..") {
+ return
+ }
+ src := filepath.Join(shadowStore, shadowID(rel))
+ if err := os.MkdirAll(src, 0o755); err != nil {
+ return
+ }
+ _ = os.Lchown(src, cfg.uid, cfg.gid)
+ made := false
+ if fi, err := os.Lstat(target); err != nil {
+ // The parent is the project's (on this machine): only the leaf is made, as a mount point.
+ if os.Mkdir(target, 0o755) != nil {
+ return
+ }
+ made = true
+ } else if !fi.IsDir() {
+ return
+ }
+ if err := syscall.Mount(src, target, "", syscall.MS_BIND, ""); err != nil {
+ if made {
+ _ = os.Remove(target)
+ }
+ return
+ }
+ shadows.mu.Lock()
+ shadows.active[filepath.Clean(target)] = true
+ shadows.lazy = append(shadows.lazy, lazyBind{Path: filepath.ToSlash(rel)})
+ lazy := append([]lazyBind(nil), shadows.lazy...)
+ lazyFile := shadows.lazyFile
+ shadows.mu.Unlock()
+ if made {
+ writeMade(append(readMade(), target))
+ }
+ if lazyFile != "" {
+ data, _ := json.Marshal(map[string]any{"binds": lazy})
+ _ = os.WriteFile(lazyFile, data, 0o644)
+ }
+}
+
+// removeMade unmounts and removes the mount points made on the host (empty folders only), at boxd's end.
+func removeMade() {
+ list := readMade()
+ if len(list) == 0 {
+ return
+ }
+ var left []string
+ for _, p := range list {
+ _ = syscall.Unmount(p, syscall.MNT_DETACH)
+ if err := os.Remove(p); err != nil && !os.IsNotExist(err) {
+ left = append(left, p)
+ }
+ }
+ writeMade(left)
+}
diff --git a/apps/desktop/box/go/cmd/boxd/shadows_test.go b/apps/desktop/box/go/cmd/boxd/shadows_test.go
new file mode 100644
index 00000000..168edc78
--- /dev/null
+++ b/apps/desktop/box/go/cmd/boxd/shadows_test.go
@@ -0,0 +1,78 @@
+//go:build linux
+
+package main
+
+import (
+ "encoding/json"
+ "os"
+ "path/filepath"
+ "sort"
+ "testing"
+ "time"
+)
+
+// The same ids as src/box/required.js shadowId (test/box-required.test.js checks the JS side).
+func TestShadowIDMatchesTheLauncher(t *testing.T) {
+ for rel, want := range map[string]string{".": "cdb4ee2aea69", "": "cdb4ee2aea69", "node_modules": "dba27c31aad9", "packages/web/node_modules": "093be456e0e4", "./packages//web/node_modules": "093be456e0e4"} {
+ if got := shadowID(rel); got != want {
+ t.Errorf("shadowID(%q) = %s, want %s", rel, got, want)
+ }
+ }
+}
+
+func TestFindNewLooksForUnshadowedPackagesOnly(t *testing.T) {
+ root := t.TempDir()
+ for _, d := range []string{"packages/a", "packages/b", "node_modules/x", ".git/y", "deep/1/2/3/4/5"} {
+ if err := os.MkdirAll(filepath.Join(root, d), 0o755); err != nil {
+ t.Fatal(err)
+ }
+ }
+ for _, f := range []string{"package.json", "packages/a/package.json", "packages/b/package.json", "node_modules/x/package.json", ".git/y/package.json", "deep/1/2/3/4/5/package.json"} {
+ _ = os.WriteFile(filepath.Join(root, f), []byte("{}"), 0o644)
+ }
+ shadows = shadowState{active: map[string]bool{filepath.Join(root, "node_modules"): true}}
+ var plan shadowPlan
+ _ = json.Unmarshal([]byte(`{"v":1,"lazy":[{"dir":"node_modules","when_any":["package.json"]}],"limits":{"entries":4000,"ms":150,"depth":4,"max":64},"skip_dirs":[".git","node_modules"]}`), &plan)
+ plan.Root = root
+ got := findNew(plan)
+ sort.Strings(got)
+ want := []string{filepath.Join(root, "packages/a/node_modules"), filepath.Join(root, "packages/b/node_modules")}
+ if len(got) != len(want) || got[0] != want[0] || got[1] != want[1] {
+ t.Fatalf("findNew = %v, want %v (the root's is shadowed; .git, node_modules and past depth 4 are not looked in)", got, want)
+ }
+ if !shadowed(filepath.Join(root, "node_modules", "x", "index.js")) || shadowed(filepath.Join(root, "packages")) {
+ t.Fatal("shadowed() is the shadowed folders and what is in them")
+ }
+}
+
+func TestCheckShadowsWithoutAPlanDoesNothing(t *testing.T) {
+ dir := t.TempDir()
+ shadows = shadowState{active: map[string]bool{}}
+ initShadows(config{envFile: filepath.Join(dir, "env")}, `["/p/node_modules"]`)
+ if !shadows.active["/p/node_modules"] || shadows.planFile != filepath.Join(dir, "shadows.json") {
+ t.Fatalf("initShadows: %v %s", shadows.active, shadows.planFile)
+ }
+ shadows.lastCheck = time.Time{}
+ checkShadows(config{envFile: filepath.Join(dir, "env")}) // no shadows.json: no walk, no mounts
+ if len(shadows.lazy) != 0 {
+ t.Fatal("bound something with no plan")
+ }
+}
+
+func TestWalkSkipsShadowedFolders(t *testing.T) {
+ root := t.TempDir()
+ _ = os.MkdirAll(filepath.Join(root, "node_modules", "pkg"), 0o755)
+ _ = os.WriteFile(filepath.Join(root, "node_modules", "pkg", "index.js"), []byte("x"), 0o644)
+ _ = os.MkdirAll(filepath.Join(root, ".venv", "lib"), 0o755)
+ shadows = shadowState{active: map[string]bool{filepath.Join(root, "node_modules"): true}}
+ entries, _ := walkPaths([]string{filepath.Join(root, "node_modules"), filepath.Join(root, ".venv")}, 1000)
+ for _, e := range entries {
+ if within(e.Path, filepath.Join(root, "node_modules")) {
+ t.Fatalf("a shadowed folder was walked: %s", e.Path)
+ }
+ }
+ if len(entries) != 2 {
+ t.Fatalf("the .venv walk: %d entries", len(entries))
+ }
+ shadows = shadowState{active: map[string]bool{}}
+}
diff --git a/apps/desktop/box/go/cmd/boxrun/init.go b/apps/desktop/box/go/cmd/boxrun/init.go
index 2f3148b3..a788fc89 100644
--- a/apps/desktop/box/go/cmd/boxrun/init.go
+++ b/apps/desktop/box/go/cmd/boxrun/init.go
@@ -194,6 +194,12 @@ func stageInit(args []string) error {
return err
}
}
+ // The box's own build folders, over the project's (after its bind: they are inside it).
+ shadowsOn := bindShadows(sp, in)
+ if len(shadowsOn) > 0 {
+ list, _ := json.Marshal(shadowsOn)
+ sp.Env = append(sp.Env, "BOXD_SHADOWS="+string(list))
+ }
if sp.AptRoot {
// One uid is mapped (the user's, as root): apt cannot drop to _apt.
conf := in("etc/apt/apt.conf.d")
@@ -205,7 +211,7 @@ func stageInit(args []string) error {
}
}
if sp.InitOut != "" {
- info, _ := json.Marshal(map[string]any{"overlay": overlay, "ms": time.Since(t0).Milliseconds()})
+ info, _ := json.Marshal(map[string]any{"overlay": overlay, "ms": time.Since(t0).Milliseconds(), "shadows": nonNil(shadowsOn)})
_ = 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.go
index b00db49d..e7ac51dd 100644
--- a/apps/desktop/box/go/cmd/boxrun/main.go
+++ b/apps/desktop/box/go/cmd/boxrun/main.go
@@ -600,6 +600,9 @@ type spec struct {
Root bool `json:"root"` // unshare as real root (WSL distro): mount /dev and /sys from the host
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"`
}
type bindSpec struct {
@@ -609,14 +612,15 @@ type bindSpec struct {
}
type state struct {
- Pid int `json:"pid"`
- Port int `json:"port"`
- Launcher string `json:"launcher"`
- IDMap string `json:"idmap"`
- Overlay string `json:"overlay"`
- Image string `json:"image"`
- Started int64 `json:"started"`
- Version string `json:"version"`
+ Pid int `json:"pid"`
+ Port int `json:"port"`
+ Launcher string `json:"launcher"`
+ IDMap string `json:"idmap"`
+ Overlay string `json:"overlay"`
+ Image string `json:"image"`
+ Started int64 `json:"started"`
+ Version string `json:"version"`
+ Shadows []string `json:"shadows,omitempty"`
}
func readState(box string) *state {
@@ -705,7 +709,9 @@ 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 multi
+ var binds, roBinds, envs, shadowArgs multi
+ shadowStoreDir := 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(&binds, "bind", "SRC=DST (repeat)")
fs.Var(&roBinds, "ro-bind", "SRC=DST read-only (repeat)")
fs.Var(&envs, "env", "K=V for boxd (repeat)")
@@ -728,7 +734,7 @@ func cmdUp(args []string) error {
unlock()
}
}
- emit(map[string]any{"ok": true, "reused": true, "pid": s.Pid, "port": port, "launcher": s.Launcher, "idmap": s.IDMap, "overlay": s.Overlay, "ms": time.Since(start).Milliseconds()})
+ emit(map[string]any{"ok": true, "reused": true, "pid": s.Pid, "port": port, "launcher": s.Launcher, "idmap": s.IDMap, "overlay": s.Overlay, "shadows": nonNil(s.Shadows), "ms": time.Since(start).Milliseconds()})
if *foreground {
keep(nil)
}
@@ -736,6 +742,8 @@ func cmdUp(args []string) error {
}
killTree(s.Pid)
}
+ // Mount points an earlier run of this box made on the host and did not remove (it was killed).
+ removeShadowMade(*box)
st := oci.Store{Dir: *store}
unlockImage, err := st.Lock(*image)
if err != nil {
@@ -808,6 +816,13 @@ func cmdUp(args []string) error {
_ = os.MkdirAll(sp.Merged, 0o755)
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"), true
+ if *shadowStoreDir != "" && len(shadowArgs) > 0 {
+ shadowSpec, err := planShadows(*box, *shadowStoreDir, shadowArgs, bs)
+ if err != nil {
+ return err
+ }
+ sp.ShadowStore, sp.Shadows = shadowSpec.store, shadowSpec.binds
+ }
if err := writeJSONFile(filepath.Join(*box, "spec.json"), sp); err != nil {
return err
}
@@ -917,14 +932,15 @@ func cmdUp(args []string) error {
return fmt.Errorf("boxd did not answer in %v: %s", *timeout, tail(filepath.Join(*box, "boxd.log"), 600))
}
var initInfo struct {
- Overlay string `json:"overlay"`
+ Overlay string `json:"overlay"`
+ Shadows []string `json:"shadows"`
}
if data, err := os.ReadFile(filepath.Join(*box, "init.json")); err == nil {
_ = json.Unmarshal(data, &initInfo)
}
- s := state{Pid: cmd.Process.Pid, Port: port, Launcher: mode, IDMap: ids.Mode, Overlay: initInfo.Overlay, Image: *image, Started: time.Now().Unix(), Version: version}
+ 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, "ms": time.Since(start).Milliseconds()})
+ 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()})
if *foreground {
keep(func() { killTree(cmd.Process.Pid) })
return nil
@@ -1074,6 +1090,8 @@ func cmdStop(args []string, remove bool) error {
killTree(s.Pid)
}
_ = os.Remove(filepath.Join(*box, "state.json"))
+ // Killed: boxd did not remove the shadows' mount points it made on the host; done here.
+ removeShadowMade(*box)
if remove {
if err := removeTree(*box); err != nil {
// Files owned by subordinate ids: delete them from inside a namespace that maps them.
diff --git a/apps/desktop/box/go/cmd/boxrun/shadows.go b/apps/desktop/box/go/cmd/boxrun/shadows.go
new file mode 100644
index 00000000..e843e84c
--- /dev/null
+++ b/apps/desktop/box/go/cmd/boxrun/shadows.go
@@ -0,0 +1,175 @@
+//go:build linux
+
+package main
+
+// Build-artefact shadows (src/box/required.js; macOS/Windows hosts): the
+// box's own copy of each build folder of the project (node_modules, .venv,
+// target, ...), kept in a store on the backend's own disk (the WSL distro,
+// 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.
+
+import (
+ "encoding/json"
+ "fmt"
+ "os"
+ "path/filepath"
+ "regexp"
+ "strings"
+)
+
+// shadowStore: where the store is inside the box (required.json shadows.store; boxd's shadows.go).
+const shadowStore = "/var/lib/omnirush/shadows"
+
+var shadowIDPattern = regexp.MustCompile(`^[0-9a-f]{12}$`)
+
+type shadowsFile struct {
+ Store string `json:"store"`
+ Binds []bindSpec `json:"binds"` // the box's other binds: how an inside path maps to the host
+}
+
+type shadowSpec struct {
+ store string
+ binds []bindSpec
+}
+
+func nonNil(list []string) []string {
+ if list == nil {
+ return []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) {
+ store, _ = filepath.Abs(store)
+ if err := os.MkdirAll(store, 0o755); err != nil {
+ return shadowSpec{}, err
+ }
+ out := shadowSpec{store: store}
+ for _, v := range args {
+ i := strings.Index(v, "=/")
+ if i <= 0 || !shadowIDPattern.MatchString(v[:i]) {
+ return shadowSpec{}, fmt.Errorf("bad shadow %q (ID=DST)", v)
+ }
+ src := filepath.Join(store, v[:i])
+ if err := os.MkdirAll(src, 0o755); err != nil {
+ return shadowSpec{}, err
+ }
+ out.binds = append(out.binds, bindSpec{Src: src, Dst: filepath.Clean(v[i+1:])})
+ }
+ if err := writeJSONFile(filepath.Join(box, "shadows.json"), shadowsFile{Store: store, Binds: binds}); err != nil {
+ return shadowSpec{}, err
+ }
+ return out, nil
+}
+
+// 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
+ }
+ if err := ensureDir(in(shadowStore)); err != nil {
+ fmt.Fprintf(os.Stderr, "boxrun: shadows: %v\n", err)
+ return nil
+ }
+ if err := bind(sp.ShadowStore, in(shadowStore), false, false); err != nil {
+ fmt.Fprintf(os.Stderr, "boxrun: shadows: %v\n", err)
+ return nil
+ }
+ madeFile := filepath.Join(sp.ShadowStore, ".made.json")
+ var made []string
+ if data, err := os.ReadFile(madeFile); err == nil {
+ _ = json.Unmarshal(data, &made)
+ }
+ var on []string
+ for _, b := range sp.Shadows {
+ target := in(b.Dst)
+ madeHere := false
+ if 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() {
+ continue
+ }
+ if os.Mkdir(target, 0o755) != nil {
+ continue
+ }
+ madeHere = true
+ } else if !fi.IsDir() {
+ continue
+ }
+ if err := bind(b.Src, target, false, false); err != nil {
+ fmt.Fprintf(os.Stderr, "boxrun: shadow %s: %v\n", b.Dst, err)
+ if madeHere {
+ _ = os.Remove(target)
+ }
+ continue
+ }
+ if madeHere {
+ made = append(made, b.Dst)
+ }
+ on = append(on, b.Dst)
+ }
+ if data, err := json.Marshal(nonNil(made)); err == nil {
+ _ = os.WriteFile(madeFile, data, 0o600)
+ }
+ return on
+}
+
+// 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
+ }
+ }
+ if best < 0 {
+ return ""
+ }
+ rest := strings.TrimPrefix(inside, binds[best].Dst)
+ return filepath.Join(binds[best].Src, rest)
+}
+
+func within(p, dir string) bool {
+ return p == dir || strings.HasPrefix(p, strings.TrimSuffix(dir, "/")+"/")
+}
+
+// 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"))
+ if err != nil {
+ return
+ }
+ var f shadowsFile
+ if json.Unmarshal(data, &f) != nil || f.Store == "" {
+ return
+ }
+ madeFile := filepath.Join(f.Store, ".made.json")
+ raw, err := os.ReadFile(madeFile)
+ if err != nil {
+ return
+ }
+ var made []string
+ if json.Unmarshal(raw, &made) != nil {
+ return
+ }
+ var left []string
+ for _, inside := range made {
+ host := hostPath(inside, f.Binds)
+ if host == "" {
+ continue
+ }
+ if err := os.Remove(host); err != nil && !os.IsNotExist(err) {
+ left = append(left, inside)
+ }
+ }
+ out, _ := json.Marshal(nonNil(left))
+ _ = os.WriteFile(madeFile, out, 0o600)
+}
diff --git a/apps/desktop/box/go/cmd/boxrun/shadows_test.go b/apps/desktop/box/go/cmd/boxrun/shadows_test.go
new file mode 100644
index 00000000..bb8f3f42
--- /dev/null
+++ b/apps/desktop/box/go/cmd/boxrun/shadows_test.go
@@ -0,0 +1,61 @@
+//go:build linux
+
+package main
+
+import (
+ "encoding/json"
+ "os"
+ "path/filepath"
+ "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": "",
+ }
+ for inside, want := range cases {
+ if got := hostPath(inside, binds); got != want {
+ t.Errorf("hostPath(%s) = %q, want %q", inside, got, want)
+ }
+ }
+}
+
+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)
+ 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" {
+ 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 {
+ t.Fatal("a shadow id is 12 hex digits")
+ }
+ // 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)
+ removeShadowMade(box)
+ if _, err := os.Stat(filepath.Join(host, "node_modules")); !os.IsNotExist(err) {
+ t.Fatal("the empty mount point is removed")
+ }
+ 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)
+ }
+ removeShadowMade(t.TempDir()) // no shadows.json: nothing
+}
diff --git a/apps/desktop/box/go/proto/proto.go b/apps/desktop/box/go/proto/proto.go
index 545d12ba..fdf4917f 100644
--- a/apps/desktop/box/go/proto/proto.go
+++ b/apps/desktop/box/go/proto/proto.go
@@ -43,7 +43,7 @@ const MaxRequestLine = 16 << 20
type Request struct {
V int `json:"v"`
Token string `json:"token"`
- Op string `json:"op"` // exec | stat | tar | ping
+ Op string `json:"op"` // exec | stat | tar | ping | ports | connect
Argv []string `json:"argv,omitempty"`
Cwd string `json:"cwd,omitempty"`
Env map[string]string `json:"env,omitempty"`
@@ -53,6 +53,14 @@ type Request struct {
// Root: exec as root instead of the box's command user (the launcher's
// own setup steps; the shell client never sets it).
Root bool `json:"root,omitempty"`
+ // Port: for connect, the TCP port inside the box (on its loopback).
+ Port int `json:"port,omitempty"`
+}
+
+// Listener is one listening TCP socket inside the box (op "ports").
+type Listener struct {
+ Port int `json:"port"`
+ Addr string `json:"addr"` // the bound address: 0.0.0.0, 127.0.0.1, ::, ::1
}
// ExitInfo ends an operation.