diff --git a/sage/gateway/being_gate_client.py b/sage/gateway/being_gate_client.py index c2622a4f1..d18b7f54f 100644 --- a/sage/gateway/being_gate_client.py +++ b/sage/gateway/being_gate_client.py @@ -1488,6 +1488,17 @@ def _reach_refusal(verb: str, path, reach: str) -> str: RUN_TMP_BYTES = 1 << 30 # the sandbox's /tmp RUN_WORK_BYTES = 1 << 30 # /work: the staged inputs (read-only) and whatever it writes RUN_FSIZE_BYTES = 512 << 20 # any one file the run writes, its output files included +# SYSTEM CONFIG A /usr PACKAGE NEEDS, read-only and -try (absent on a seat = no mount). The box +# binds /usr but not /etc, and Debian's python3-matplotlib patches matplotlib_fname() to look for +# its default rc at /etc/matplotlibrc INSTEAD of mpl-data, so with /etc unbound `import +# matplotlib` raised "Could not find matplotlibrc file; your Matplotlib install is broken" in +# every run. cbp-being read that as its own fault and spent 13 days (2026-11-06..11-20) hunting +# the file: scripts to locate it, searches of the workspace and home, then a write_note to the +# host home's config dir (scope-b0972998b89d, refused). No file the being could write fixes a +# mount, and the census listed matplotlib as missing from the box while the host had it. +# Measured 2026-11-20 on cbp: the same bwrap line exits 1 with that error, and with this bind it +# imports pyplot and saves a PNG. A default config file, not private state: nothing else in /etc. +RUN_ETC_BINDS = " --ro-bind-try /etc/matplotlibrc /etc/matplotlibrc" RUN_MIN_S = 30 # a beat with less than this left starts no run (Nova) @@ -1713,7 +1724,8 @@ def sandbox_reach(effector: str, args: dict, ctx: Optional[dict] = None) -> List """What a sandboxed verb's box exposes to the program, which is the being's reach for that act. The rest of the line is the box's own plumbing, fixed by this module and not chosen by the - being: the wrapper (prlimit, bwrap), the system mounts (/usr, /lib, /bin, /etc/alternatives), + being: the wrapper (prlimit, bwrap), the system mounts (/usr, /lib, /bin, /etc/alternatives, + RUN_ETC_BINDS), the interpreter, /proc, /dev and the tmpfs. Judging those as the being's reach refused every run on Legion (SAGE #441 replay, 2026-10-25: 271/271 on /usr/bin/prlimit) and, before that, every check and game (miniforge3, 2026-10-05). What the law must still see is what a grant @@ -1974,6 +1986,7 @@ def run_command(args: dict, ctx: Optional[dict] = None) -> str: # `run` read as unavailable there (legion-claude, #470) " --ro-bind /usr /usr --ro-bind /lib /lib --ro-bind-try /lib64 /lib64 --ro-bind /bin /bin" " --ro-bind /etc/alternatives /etc/alternatives" + f"{RUN_ETC_BINDS}" f" --ro-bind {interp} {interp}" f" --proc /proc --dev /dev --size {RUN_TMP_BYTES} --tmpfs /tmp" f"{wt_bind}" diff --git a/sage/gateway/run_census.py b/sage/gateway/run_census.py index 4acf923b4..c2a82051d 100644 --- a/sage/gateway/run_census.py +++ b/sage/gateway/run_census.py @@ -135,6 +135,7 @@ def box_command(stage: str) -> List[str]: f"{bgc.SANDBOX} --clearenv" " --ro-bind /usr /usr --ro-bind /lib /lib --ro-bind-try /lib64 /lib64 --ro-bind /bin /bin" " --ro-bind /etc/alternatives /etc/alternatives" + f"{bgc.RUN_ETC_BINDS}" f" --ro-bind {shlex.quote(interp)} {shlex.quote(interp)}" " --proc /proc --dev /dev --tmpfs /tmp" f" --ro-bind {shlex.quote(stage)} /work" diff --git a/sage/gateway/tests/test_being_gate_client.py b/sage/gateway/tests/test_being_gate_client.py index 4f90ab259..1b4c197ad 100644 --- a/sage/gateway/tests/test_being_gate_client.py +++ b/sage/gateway/tests/test_being_gate_client.py @@ -1138,7 +1138,9 @@ def _assert_every_bind_is_judged_or_plumbing(calls): line = next(e for e in events if e.get("egress"))["egress"][-1] judged = {p for e in events for p in (e["tool_input"].get("paths") or [])} interp = os.path.dirname(os.path.dirname(sys.executable)) + from sage.gateway.being_gate_client import RUN_ETC_BINDS plumbing = {"/usr", "/lib", "/lib64", "/bin", "/etc/alternatives", interp} + plumbing |= {t for t in RUN_ETC_BINDS.split() if t.startswith("/")} # fixed system config for src in _line_bind_sources(line): if src in plumbing or src.startswith(STAGE_ROOT + "-"): continue diff --git a/sage/gateway/tests/test_run_verb.py b/sage/gateway/tests/test_run_verb.py index 3f80b9c8f..eba0a4a46 100644 --- a/sage/gateway/tests/test_run_verb.py +++ b/sage/gateway/tests/test_run_verb.py @@ -1280,3 +1280,51 @@ def test_a_value_passed_as_data_is_told_data_only_stages_files(tmp_path): assert "base name with no directory" in g.error and "hand values" not in g.error s = d._do_run(BeingIntent("run", {"path": "nope"})) # the script itself: no value hint assert s.ok is False and "hand values" not in s.error + + +def _box_sees_debian_matplotlib(): + """True only where this test exercises RUN_ETC_BINDS: the box's interpreter imports matplotlib + from a tree the box mounts, and that matplotlib is Debian's (default rc read from + /etc/matplotlibrc, which exists). The probe runs the box's interpreter with the box's cleared + environment (HOME=/tmp, no PYTHONPATH), so a matplotlib only in the host user site or on an + inherited PYTHONPATH skips instead of failing on a ModuleNotFoundError the bind cannot fix + (codex, SAGE #795 review), and an upstream wheel with mpl-data skips instead of passing + without the bind (it never reads /etc).""" + import subprocess + interp = os.path.dirname(os.path.dirname(sys.executable)) + probe = ("import inspect, os, matplotlib as m\n" + "src = inspect.getsource(m.matplotlib_fname)\n" + "print(os.path.realpath(m.__file__)); print('/etc/matplotlibrc' in src)\n") + try: + p = subprocess.run([f"{interp}/bin/python3", "-c", probe], capture_output=True, text=True, + env={"HOME": "/tmp", "PATH": f"{interp}/bin:/usr/bin:/bin"}, timeout=60) + except (OSError, subprocess.TimeoutExpired): + return False + lines = p.stdout.split() + if p.returncode != 0 or len(lines) != 2: + return False + roots = [os.path.realpath(r) + os.sep for r in (interp, "/usr", "/lib")] + return (any(lines[0].startswith(r) for r in roots) and lines[1] == "True" + and os.path.isfile("/etc/matplotlibrc")) + + +@needs_bwrap +@pytest.mark.skipif(not _box_sees_debian_matplotlib(), + reason="no Debian matplotlib on a path the run box mounts") +def test_a_library_the_seat_python_imports_also_plots_inside_run(tmp_path): + """cbp-being, 2026-11-06..11-20: every `import matplotlib.pyplot` in `run` raised "Could not + find matplotlibrc file; your Matplotlib install is broken", because Debian's matplotlib reads + its default rc from /etc and the box binds /usr only. The being hunted the file for 13 days. + The assertion is on what the script printed and wrote, so a box that drops RUN_ETC_BINDS (or + a seat whose matplotlib needs another config file) turns this red.""" + d, home = _run_dispatcher(tmp_path) + (home / "scratch" / "plot.py").write_text( + "import os, matplotlib\n" + "matplotlib.use('Agg')\n" + "import matplotlib.pyplot as plt\n" + "fig, ax = plt.subplots(); ax.plot([0, 1], [1, 0]); fig.savefig('out.png')\n" + "print('saved', os.path.getsize('out.png') > 0)\n") + r = d._do_run(BeingIntent("run", {"path": "scratch/plot.py"})) + assert r.ok, r.error + assert "Could not find matplotlibrc" not in r.result["stderr"], r.result["stderr"] + assert r.result["exit"] == 0 and "saved True" in r.result["stdout"], r.result