From 810b97a28ae36fe02cecbf414152efd30f2cd616 Mon Sep 17 00:00:00 2001 From: Dennis Palatov Date: Thu, 19 Nov 2026 16:51:58 -0800 Subject: [PATCH 1/2] run: bind the system matplotlibrc so matplotlib imports in the box Debian's python3-matplotlib patches matplotlib_fname() to read its default rc from /etc/matplotlibrc instead of mpl-data. The run box binds /usr but not /etc, so every `import matplotlib` in a run raised "Could not find matplotlibrc file; your Matplotlib install is broken". cbp-being read that as its own fault and hunted the file for 13 days (locator scripts, workspace and home searches, then a write_note to the host home config dir, scope-b0972998b89d, refused). RUN_ETC_BINDS adds one --ro-bind-try of that file to run_command and to the census box (so the census keeps measuring the box run uses). It is listed as box plumbing beside /etc/alternatives in sandbox_reach's docstring and in the bind-audit test's plumbing set, derived from the constant. Test: test_a_library_the_seat_python_imports_also_plots_inside_run saves a PNG through the real run verb; with the fix reverted it fails on the exact error the being saw. Gateway suite: 2710 passed, 44 skipped, 1 xfail (pre-existing). Co-Authored-By: Claude Opus 5.5 --- sage/gateway/being_gate_client.py | 15 ++++++++++- sage/gateway/run_census.py | 1 + sage/gateway/tests/test_being_gate_client.py | 2 ++ sage/gateway/tests/test_run_verb.py | 26 ++++++++++++++++++++ 4 files changed, 43 insertions(+), 1 deletion(-) diff --git a/sage/gateway/being_gate_client.py b/sage/gateway/being_gate_client.py index c2622a4f13..d18b7f54fa 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 4acf923b4b..c2a82051db 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 4f90ab2591..1b4c197ad3 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 3f80b9c8f2..ce5d5ccba7 100644 --- a/sage/gateway/tests/test_run_verb.py +++ b/sage/gateway/tests/test_run_verb.py @@ -1280,3 +1280,29 @@ 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 _host_has_matplotlib(): + import subprocess + return subprocess.run([sys.executable, "-c", "import matplotlib"], capture_output=True).returncode == 0 + + +@needs_bwrap +@pytest.mark.skipif(not _host_has_matplotlib(), reason="the seat's interpreter has no matplotlib") +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 From c72abedcab708732b175305d8e96c2d5b6c11dfe Mon Sep 17 00:00:00 2001 From: Dennis Palatov Date: Thu, 19 Nov 2026 22:11:18 -0800 Subject: [PATCH 2/2] run: gate the matplotlib box test on a Debian matplotlib the box mounts codex review (#795): the skip probe imported with the host env, so a matplotlib only in the user site or on PYTHONPATH ran the test and failed on a ModuleNotFoundError the bind cannot fix. The probe now runs the box's interpreter with the box's cleared env (HOME=/tmp), requires the module under a mounted root, and requires Debian's /etc rc lookup + the file, so an upstream wheel skips instead of passing without the bind. Control on cbp: RUN_ETC_BINDS='' turns the test red on 'Could not find matplotlibrc'. Co-Authored-By: Claude Opus 5.5 --- sage/gateway/tests/test_run_verb.py | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/sage/gateway/tests/test_run_verb.py b/sage/gateway/tests/test_run_verb.py index ce5d5ccba7..eba0a4a46c 100644 --- a/sage/gateway/tests/test_run_verb.py +++ b/sage/gateway/tests/test_run_verb.py @@ -1282,13 +1282,35 @@ def test_a_value_passed_as_data_is_told_data_only_stages_files(tmp_path): assert s.ok is False and "hand values" not in s.error -def _host_has_matplotlib(): +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 - return subprocess.run([sys.executable, "-c", "import matplotlib"], capture_output=True).returncode == 0 + 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 _host_has_matplotlib(), reason="the seat's interpreter has no matplotlib") +@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