Skip to content

A desktop coding agent with free access to frontier models.

TypeScript

fix: sync box runner helpers

ogsamratce75500b0e72
diff --git a/apps/desktop/box/go/cmd/boxrun/init.go b/apps/desktop/box/go/cmd/boxrun/init.go
index 2e4d043e..2f3148b3 100644
--- a/apps/desktop/box/go/cmd/boxrun/init.go
+++ b/apps/desktop/box/go/cmd/boxrun/init.go
@@ -18,9 +18,18 @@ import (
// user namespace: 5.11+, with userxattr), else fuse-overlayfs when given.
// Returns "kernel" or "fuse".
func mountOverlay(lower, upper, work, merged, fuse string) (string, error) {
+ return mountOverlayMode(lower, upper, work, merged, fuse, false)
+}
+
+func mountOverlayMode(lower, upper, work, merged, fuse string, noKernel bool) (string, error) {
base := fmt.Sprintf("lowerdir=%s,upperdir=%s,workdir=%s", lower, upper, work)
var errs []string
- for _, opts := range []string{base + ",userxattr", base} {
+ kernel := []string{base + ",userxattr", base}
+ if noKernel {
+ kernel = nil
+ errs = append(errs, "kernel overlay skipped")
+ }
+ for _, opts := range kernel {
err := syscall.Mount("overlay", merged, "overlay", 0, opts)
if err == nil {
return "kernel", nil
@@ -34,7 +43,7 @@ func mountOverlay(lower, upper, work, merged, fuse string) (string, error) {
if err == nil {
return "fuse", nil
}
- errs = append(errs, "fuse-overlayfs: "+strings.TrimSpace(string(out)))
+ errs = append(errs, fmt.Sprintf("fuse-overlayfs: %v %s", err, strings.TrimSpace(string(out))))
} else {
errs = append(errs, "no /dev/fuse")
}
@@ -117,7 +126,7 @@ func stageInit(args []string) error {
if err := ensureDir(m); err != nil {
return err
}
- overlay, err := mountOverlay(sp.Lower, sp.Upper, sp.Work, m, sp.Fuse)
+ overlay, err := mountOverlayMode(sp.Lower, sp.Upper, sp.Work, m, sp.Fuse, sp.NoKernel)
if err != nil {
return err
}
diff --git a/apps/desktop/box/go/cmd/boxrun/main.go b/apps/desktop/box/go/cmd/boxrun/main.go
index 53817697..655f1b70 100644
--- a/apps/desktop/box/go/cmd/boxrun/main.go
+++ b/apps/desktop/box/go/cmd/boxrun/main.go
@@ -557,7 +557,8 @@ type spec struct {
Env []string `json:"env"`
Hostname string `json:"hostname"`
InitOut string `json:"init_out"`
- Root bool `json:"root"` // unshare as real root (WSL distro): mount /dev and /sys from the host
+ 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"`
}
@@ -611,8 +612,11 @@ func alive(s *state) bool {
if err != nil {
return false
}
- c := string(cmdline)
- return strings.Contains(c, "bwrap") || strings.Contains(c, "unshare") || strings.Contains(c, "boxrun")
+ switch filepath.Base(strings.SplitN(string(cmdline), "\x00", 2)[0]) {
+ case "bwrap", "unshare", "boxrun":
+ return true
+ }
+ return false
}
func portOpen(port int) bool {
@@ -658,6 +662,7 @@ func cmdUp(args []string) error {
idmapAsked := fs.String("idmap", "auto", "auto|single|subuid")
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
fs.Var(&binds, "bind", "SRC=DST (repeat)")
fs.Var(&roBinds, "ro-bind", "SRC=DST read-only (repeat)")
@@ -721,7 +726,17 @@ func cmdUp(args []string) error {
env = append(env, "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin")
}
env = append(env, envs...)
- sp := spec{Hostname: "omnirush-box", Env: env, AptRoot: ids.Mode == "single" && !root}
+ sp := spec{Hostname: "omnirush-box", Env: env, AptRoot: ids.Mode == "single" && !root, NoKernel: *overlayMode == "fuse"}
+ if sp.NoKernel && fuseBin == "" {
+ return errors.New("--overlay fuse: fuse-overlayfs is not installed")
+ }
+ // /etc/hosts: this machine's (the network is shared) plus the box's own name, as a
+ // container runtime writes it (sudo looks its host name up).
+ hostsData, _ := os.ReadFile("/etc/hosts")
+ hostsText := strings.TrimRight(string(hostsData), "\n") + "\n127.0.1.1\tomnirush-box\n"
+ if err := os.WriteFile(filepath.Join(*box, "hosts"), []byte(hostsText), 0o644); err != nil {
+ return err
+ }
var cmd *exec.Cmd
if mode == "unshare" {
un := findTool("", "unshare")
@@ -731,7 +746,7 @@ func cmdUp(args []string) error {
sp.Lower, sp.Upper, sp.Work = st.RootFS(*image), filepath.Join(*box, "upper"), filepath.Join(*box, "work")
sp.Merged = filepath.Join(*box, "merged")
_ = os.MkdirAll(sp.Merged, 0o755)
- sp.Fuse, sp.Boxd, sp.Resolv, sp.Hosts = fuseBin, *boxd, resolvFile(), "/etc/hosts"
+ 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 err := writeJSONFile(filepath.Join(*box, "spec.json"), sp); err != nil {
return err
@@ -760,10 +775,14 @@ func cmdUp(args []string) error {
bargs = append(bargs, "--ro-bind-try", r, "/.omni/resolv.conf")
sp.Resolv = "/.omni/resolv.conf"
}
- bargs = append(bargs, "--ro-bind-try", "/etc/hosts", "/.omni/hosts")
- sp.Hosts = "/.omni/hosts"
+ sp.Hosts = "/.omni/box/hosts"
if fuseBin != "" {
+ // fuse-overlayfs is a dynamic program: the host's libraries go in for the setup only
+ // (the root switch leaves them behind; the daemon keeps what it opened).
bargs = append(bargs, "--dev-bind-try", "/dev/fuse", "/dev/fuse", "--ro-bind", fuseBin, "/.omni/fuse-overlayfs")
+ for _, d := range []string{"/usr", "/lib", "/lib64", "/lib32", "/etc/ld.so.cache"} {
+ bargs = append(bargs, "--ro-bind-try", d, d)
+ }
sp.Fuse = "/.omni/fuse-overlayfs"
}
for i, b := range bs {
diff --git a/apps/desktop/box/go/cmd/boxrun/main_test.go b/apps/desktop/box/go/cmd/boxrun/main_test.go
new file mode 100644
index 00000000..c3f8a6f9
--- /dev/null
+++ b/apps/desktop/box/go/cmd/boxrun/main_test.go
@@ -0,0 +1,90 @@
+//go:build linux
+
+package main
+
+import (
+ "os"
+ "path/filepath"
+ "strings"
+ "testing"
+)
+
+func TestKernelAtLeast(t *testing.T) {
+ cases := map[string]bool{"6.8.0-139-generic": true, "5.11.0": true, "5.10.220-1": false, "4.19.0": false, "6.6.87.2-microsoft-standard-WSL2": true, "": false}
+ for rel, want := range cases {
+ if got := kernelAtLeast(rel, 5, 11); got != want {
+ t.Errorf("%q: %v", rel, got)
+ }
+ }
+}
+
+func TestParseBind(t *testing.T) {
+ b, err := parseBind("/mnt/c/Users/Sam/proj=/c/Users/Sam/proj")
+ if err != nil || b.Src != "/mnt/c/Users/Sam/proj" || b.Dst != "/c/Users/Sam/proj" {
+ t.Fatalf("%+v %v", b, err)
+ }
+ if _, err := parseBind("relative-only"); err == nil {
+ t.Fatal("a bind without a target was accepted")
+ }
+ b, _ = parseBind("/a=b=/x/y/")
+ if b.Src != "/a=b" || b.Dst != "/x/y" {
+ t.Fatalf("an = in the source: %+v", b)
+ }
+}
+
+func TestArchiveRef(t *testing.T) {
+ if f, ok := archiveRef("docker-archive:/tmp/x.tar"); !ok || f != "/tmp/x.tar" {
+ t.Fatal(f, ok)
+ }
+ if f, ok := archiveRef("oci-archive:/mnt/c/x.tar"); !ok || f != "/mnt/c/x.tar" {
+ t.Fatal(f, ok)
+ }
+ if _, ok := archiveRef("ghcr.io/omnirush-ai/box-base:1"); ok {
+ t.Fatal("a registry reference read as an archive")
+ }
+ if _, ok := archiveRef("docker-archive:"); ok {
+ t.Fatal("an empty archive path")
+ }
+}
+
+func TestSubIDs(t *testing.T) {
+ file := filepath.Join(t.TempDir(), "subuid")
+ os.WriteFile(file, []byte("root:100000:65536\nsam:165536:65536\n1001:231072:1000\n"), 0o644)
+ if s, c := subIDs(file, "sam", 1000); s != 165536 || c != 65536 {
+ t.Fatal(s, c)
+ }
+ if s, c := subIDs(file, "nobody-here", 1001); s != 231072 || c != 1000 {
+ t.Fatal("by uid:", s, c)
+ }
+ if _, c := subIDs(file, "x", 5); c != 0 {
+ t.Fatal("no entry")
+ }
+}
+
+func TestClassify(t *testing.T) {
+ one := 1
+ zero := 0
+ fail := func(code, reason, detail string) probeResult { return probeResult{Code: code, Reason: reason} }
+ r := classify(probeResult{AppArmor: &one}, "bwrap: setting up uid map: Permission denied", fail)
+ if r.Code != "apparmor" || !strings.Contains(r.Reason, "Ubuntu 23.10+") {
+ t.Fatal(r)
+ }
+ r = classify(probeResult{AppArmor: &zero, UsernsClone: &zero}, "bwrap: No permissions to create new namespace", fail)
+ if r.Code != "userns_off" {
+ t.Fatal(r)
+ }
+ r = classify(probeResult{AppArmor: &zero}, "bwrap: setting up uid map: Permission denied", fail)
+ if r.Code != "userns_denied" {
+ t.Fatal(r)
+ }
+}
+
+func TestAlive(t *testing.T) {
+ if alive(nil) || alive(&state{Pid: 0}) {
+ t.Fatal("no pid is not alive")
+ }
+ // The go tool (this test's parent) is not a launcher: a reused pid is not taken for the box.
+ if alive(&state{Pid: os.Getppid()}) {
+ t.Fatal("a pid that is not bwrap/unshare/boxrun counted as the box")
+ }
+}
diff --git a/apps/server/src/box/backends/bwrap.ts b/apps/server/src/box/backends/bwrap.ts
index 3bec757b..125cafe8 100644
--- a/apps/server/src/box/backends/bwrap.ts
+++ b/apps/server/src/box/backends/bwrap.ts
@@ -179,6 +179,8 @@ async function open(ctx: OpenContext): Promise<Opened> {
const portFile = path.join(runFolder, "boxd.port");
const idle = Number(env.OMNIRUSH_BOX_IDLE_MIN) > 0 ? Number(env.OMNIRUSH_BOX_IDLE_MIN) : IDLE_MINUTES;
const args = upArgs({ box: boxDir(home, key), store: storeDir(home), image: meta.image, boxd, portFile, root, croot, runFolder, shims, pkglog, token: meta.token, bwrap: runtime.bwrap ?? null, idleMinutes: idle });
+ // OMNIRUSH_BOX_OVERLAY=fuse: fuse-overlayfs only (a kernel without overlayfs in user namespaces, or a test of one).
+ if (String(env.OMNIRUSH_BOX_OVERLAY || "").toLowerCase() === "fuse") args.push("--overlay", "fuse");
const res = await run(runtime.bin, args, { timeoutMs: 45_000, env: { PATH: env.PATH || "/usr/bin:/bin", USER: env.USER || "" } });
const out = parseOut(res.stdout);
const port = num(out?.port);