From 317de37029aaa3e6bbf8c729c35c2bfd38284853 Mon Sep 17 00:00:00 2001 From: Dennis Palatov Date: Mon, 16 Nov 2026 01:09:17 -0800 Subject: [PATCH] run box reads the host user site, read-only (#701 option 2) dp, 2026-11-16: option 2. `run` (bwrap box) and `request_run` (seat on the host) imported different packages: on cbp, sklearn and torch live only in the user site. The census showed the box's answer every beat, and cbp-being spent 11-14..11-16 filing installer scripts and scope asks for the host's .local/bin to close a gap no install could close. - being_gate_client: host_user_site(), box_user_site(), user_site_bind(). run_command binds the host user site --ro-bind at /tmp/.local/lib/python3.N/site-packages (the box's HOME is /tmp), after --tmpfs /tmp, so site.py treats it as the user site (.pth included). SAGE_RUN_USER_SITE=0 turns it off. Seatbelt form unchanged. - run_census: same bind; probe runs without -I (which disables the user site; run's own line has no -I); site_dirs() includes the user site so a `pip install --user` re-measures. - tests: the user site is box plumbing (like /usr) in the every-bind guard; three new rows (ro, after /tmp, off switch, census). Measured on cbp: census box imports sklearn 1.8.0 and torch 2.6.0+cu124; a real run_command line (prlimit --as 12.6 GB) trained a torch XOR MLP to 0.996 and an sklearn MLPClassifier to 0.994, exit 0. Gateway suite: 2488 passed; one order-dependent failure in test_body_and_gaze passes alone, with and without this change. Co-Authored-By: Claude Opus 5.5 --- sage/gateway/being_gate_client.py | 46 ++++++++++++++++++++ sage/gateway/run_census.py | 11 ++++- sage/gateway/tests/test_being_gate_client.py | 40 +++++++++++++++++ 3 files changed, 96 insertions(+), 1 deletion(-) diff --git a/sage/gateway/being_gate_client.py b/sage/gateway/being_gate_client.py index c23b74c88a..45768162b5 100644 --- a/sage/gateway/being_gate_client.py +++ b/sage/gateway/being_gate_client.py @@ -1509,6 +1509,50 @@ def run_budget_s(now: Optional[float] = None) -> tuple: return int(left), "the time left in this beat" +def host_user_site() -> str: + """The seat user's Python user site (e.g. ~/.local/lib/python3.12/site-packages), realpath'd, + or "" when there is none. + + THE BOX READS IT, READ-ONLY (dp, 2026-11-16, SAGE #701 option 2). `run` and `request_run` + imported different packages: on cbp, sklearn and torch live only in the user site, so the seat's + host run imported them and the box could not. The being saw the box's answer every beat and + spent two days filing installer scripts and scope asks for ~/.local/bin to close a gap no + install could close. Binding the user site read-only into the box makes the two places agree, + so the cheap path (run) and the correct path import the same packages. It is box plumbing + like /usr: package code, read-only, no secrets, nothing the being can write. + + SAGE_RUN_USER_SITE=0 turns it off on a seat that does not want it.""" + import os + import site + if os.environ.get("SAGE_RUN_USER_SITE", "1") == "0": + return "" + try: + p = site.getusersitepackages() + except Exception: + return "" + p = os.path.realpath(p) if p else "" + return p if p and os.path.isdir(p) else "" + + +def box_user_site(interp_minor: Optional[int] = None) -> str: + """Where Python inside the box looks for its user site: HOME is /tmp there, so + /tmp/.local/lib/python3.N/site-packages. Binding the host's user site AT THIS PATH (not at its + own path plus PYTHONPATH) lets site.py treat it as the user site it is, .pth files included.""" + import sys + n = sys.version_info.minor if interp_minor is None else interp_minor + return f"/tmp/.local/lib/python3.{n}/site-packages" + + +def user_site_bind() -> str: + """The bwrap fragment that shows the host user site to the box, or "" when there is none. + Must come AFTER `--tmpfs /tmp` (a later mount masks an earlier one).""" + import shlex + p = host_user_site() + if not p: + return "" + return f" --ro-bind {shlex.quote(p)} {shlex.quote(box_user_site())}" + + def run_mem_cap_bytes() -> int: import os try: @@ -1914,6 +1958,8 @@ def run_command(args: dict, ctx: Optional[dict] = None) -> str: " --ro-bind /etc/alternatives /etc/alternatives" f" --ro-bind {interp} {interp}" f" --proc /proc --dev /dev --size {RUN_TMP_BYTES} --tmpfs /tmp" + # the host user site, read-only, where the box's python looks for it (SAGE #701) + f"{user_site_bind()}" f"{wt_bind}" f"{grant_binds}" # Writable in there: these two size-limited tmpfs, and what the being's grants reach. diff --git a/sage/gateway/run_census.py b/sage/gateway/run_census.py index 4acf923b4b..52d87fe45a 100644 --- a/sage/gateway/run_census.py +++ b/sage/gateway/run_census.py @@ -112,6 +112,11 @@ def site_dirs() -> List[str]: f"/usr/lib/{ver}/dist-packages"): if p not in out: out.append(p) + # the user site is in the box too now (SAGE #701), so a `pip install --user` re-measures + from sage.gateway import being_gate_client as bgc + us = bgc.host_user_site() + if us and us not in out: + out.append(us) return [p for p in out if os.path.isdir(p)] @@ -137,12 +142,16 @@ def box_command(stage: str) -> List[str]: " --ro-bind /etc/alternatives /etc/alternatives" f" --ro-bind {shlex.quote(interp)} {shlex.quote(interp)}" " --proc /proc --dev /dev --tmpfs /tmp" + # the host user site, as run binds it (SAGE #701) + f"{bgc.user_site_bind()}" f" --ro-bind {shlex.quote(stage)} /work" " --unshare-pid --unshare-net --unshare-ipc --unshare-uts" " --new-session --die-with-parent" " --setenv HOME /tmp --setenv PYTHONUTF8 1 --setenv PYTHONDONTWRITEBYTECODE 1" f" --setenv PATH {shlex.quote(interp)}/bin:/usr/bin:/bin" - f" --chdir /work {shlex.quote(interp)}/bin/python3 -I /work/probe.py") + # No -I: it disables the user site, and run's own interpreter line has no -I, so a probe + # under -I measured a box `run` is not (SAGE #701). /work holds only probe.py. + f" --chdir /work {shlex.quote(interp)}/bin/python3 /work/probe.py") def measure(modules=DEFAULT_MODULES, tools=TOOLS, runner=subprocess.run) -> Dict: diff --git a/sage/gateway/tests/test_being_gate_client.py b/sage/gateway/tests/test_being_gate_client.py index c5b27a14a5..f0356fd178 100644 --- a/sage/gateway/tests/test_being_gate_client.py +++ b/sage/gateway/tests/test_being_gate_client.py @@ -1105,6 +1105,11 @@ def _assert_every_bind_is_judged_or_plumbing(calls): judged = {p for e in events for p in (e["tool_input"].get("paths") or [])} interp = os.path.dirname(os.path.dirname(sys.executable)) plumbing = {"/usr", "/lib", "/lib64", "/bin", "/etc/alternatives", interp} + # the host user site is box plumbing by operator decision (dp, 2026-11-16, SAGE #701 option 2): + # package code, read-only, like /usr. Judging it as the being's read would refuse every run. + from sage.gateway.being_gate_client import host_user_site + if host_user_site(): + plumbing.add(host_user_site()) for src in _line_bind_sources(line): if src in plumbing or src.startswith(STAGE_ROOT + "-"): continue @@ -1180,3 +1185,38 @@ def test_a_run_name_that_could_add_a_bind_never_reaches_the_law(tmp_path, monkey v = c.gate(BeingIntent("run", {"path": "scratch/s.py", "data": data})) assert v.blocks, v assert not calls, "refused at composition, before any decide()" + + +# --- SAGE #701 option 2 (dp, 2026-11-16): the host user site is in the run box, read-only, where the +# box's python looks for it, so `run` and `request_run` import the same packages. + +def test_the_user_site_is_bound_read_only_after_tmp_where_the_box_looks(tmp_path, monkeypatch): + import shlex + import sage.gateway.being_gate_client as bgc + site_dir = tmp_path / "site-packages"; site_dir.mkdir() + monkeypatch.setattr(bgc, "host_user_site", lambda: str(site_dir)) + monkeypatch.setattr(bgc, "run_sandbox_kind", lambda: "bwrap") + home = tmp_path / "home"; (home / "scratch").mkdir(parents=True) + (home / "scratch" / "s.py").write_text("print(1)\n") + line = bgc.run_command({"path": "scratch/s.py"}, {"memory_root": str(home), "member": "t"}) + toks = shlex.split(line) + i = toks.index(str(site_dir)) + assert toks[i - 1] == "--ro-bind", "the user site must never be writable from the box" + assert toks[i + 1] == bgc.box_user_site() + assert toks.index("/tmp") < i, "bound after --tmpfs /tmp, or the tmpfs masks it" + + +def test_the_user_site_bind_can_be_switched_off(monkeypatch): + import sage.gateway.being_gate_client as bgc + monkeypatch.setenv("SAGE_RUN_USER_SITE", "0") + assert bgc.host_user_site() == "" and bgc.user_site_bind() == "" + + +def test_the_census_box_binds_the_user_site_and_does_not_isolate_it_away(tmp_path, monkeypatch): + import sage.gateway.being_gate_client as bgc + from sage.gateway import run_census + site_dir = tmp_path / "site-packages"; site_dir.mkdir() + monkeypatch.setattr(bgc, "host_user_site", lambda: str(site_dir)) + cmd = run_census.box_command(str(tmp_path)) + assert str(site_dir) in cmd and bgc.box_user_site() in cmd + assert "-I" not in cmd, "-I disables the user site; run's own line has no -I"