Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

marimo: cover the verdict, the pane and the shell scripts; read marimo check as it prints

deecd87289311fe
diff --git a/store/agents/marimo/toolchain/test_scripts.py b/store/agents/marimo/toolchain/test_scripts.py
new file mode 100644
index 000000000..075d3dd15
--- /dev/null
+++ b/store/agents/marimo/toolchain/test_scripts.py
@@ -0,0 +1,169 @@
+"""setup.sh, doctor.sh, init-workspace.sh and viewer.sh, run for real against a scratch install whose
+PATH holds only stub commands (and the few coreutils the scripts use), so every ok / miss line is
+reached without a network, a venv or a marimo on the machine:
+
+ python3 -m unittest toolchain/test_scripts.py
+"""
+import shutil, subprocess, tempfile, unittest
+from pathlib import Path
+
+PACKAGE = Path(__file__).resolve().parent.parent
+BASH = "/bin/bash"
+COREUTILS = ("dirname", "cat", "mkdir", "tail")
+VERSION = (PACKAGE / "MARIMO_VERSION").read_text().strip()
+
+
+class Sandbox:
+ """An install dir with the package's scripts, a bin/ of stubs as the whole PATH, and a log of
+ every stub call."""
+
+ def __init__(self, test: unittest.TestCase):
+ tmp = tempfile.TemporaryDirectory()
+ test.addCleanup(tmp.cleanup)
+ self.root = Path(tmp.name)
+ self.install = self.root / "install"
+ (self.install / "toolchain").mkdir(parents=True)
+ for script in PACKAGE.glob("toolchain/*.sh"):
+ shutil.copy(script, self.install / "toolchain" / script.name)
+ for name in ("MARIMO_VERSION", "viewer.sh"):
+ shutil.copy(PACKAGE / name, self.install / name)
+ self.bin = self.root / "bin"
+ self.bin.mkdir()
+ self.calls = self.root / "calls.log"
+ self.calls.touch()
+ for name in COREUTILS:
+ real = next(p for p in (Path("/bin") / name, Path("/usr/bin") / name) if p.exists())
+ (self.bin / name).symlink_to(real)
+
+ def stub(self, name: str, body: str = "", where: Path | None = None) -> Path:
+ path = (where or self.bin) / name
+ path.parent.mkdir(parents=True, exist_ok=True)
+ path.write_text(f'#!/bin/bash\necho "{name} $*" >> "$CALLS"\n{body}\n')
+ path.chmod(0o755)
+ return path
+
+ def venv(self, root: Path | None = None) -> None:
+ """A .venv whose python logs pip and whose marimo prints a version the way marimo does."""
+ bin_ = (root or self.install) / ".venv" / "bin"
+ self.stub("python", "exit ${PIP_EXIT:-0}", where=bin_)
+ self.stub("marimo", f'echo "{VERSION}"', where=bin_)
+
+ def run(self, script: str, cwd: Path | None = None, **env: str) -> subprocess.CompletedProcess:
+ return subprocess.run([BASH, str(self.install / script)], cwd=cwd or self.install,
+ env={"PATH": str(self.bin), "CALLS": str(self.calls), **env},
+ capture_output=True, text=True, timeout=60)
+
+ def logged(self) -> list[str]:
+ return self.calls.read_text().splitlines()
+
+
+class Doctor(unittest.TestCase):
+ def test_ready(self):
+ box = Sandbox(self)
+ box.venv()
+ r = box.run("toolchain/doctor.sh")
+ self.assertEqual((r.returncode, r.stdout), (0, f"ok marimo {VERSION}\n"), r.stderr)
+
+ def test_no_venv_is_a_miss(self):
+ r = Sandbox(self).run("toolchain/doctor.sh")
+ self.assertEqual((r.returncode, r.stdout), (1, "miss .venv/bin/marimo — run toolchain/setup.sh\n"))
+
+
+def python(version: str, fits: bool) -> str:
+ """A pythonX.Y stub: the version probe passes or not; `-m venv .venv` makes a stub venv."""
+ return (f'case "$1" in\n'
+ f' -c) exit {0 if fits else 1} ;;\n'
+ f' --version) echo "Python {version}" ;;\n'
+ f' -m) mkdir -p .venv/bin; cp "$STUB_VENV/python" "$STUB_VENV/marimo" .venv/bin/ ;;\n'
+ f'esac')
+
+
+class Setup(unittest.TestCase):
+ def sandbox(self) -> Sandbox:
+ box = Sandbox(self)
+ box.venv(box.root / "template") # what `python -m venv` copies in
+ (box.bin / "cp").symlink_to("/bin/cp")
+ return box
+
+ def run_setup(self, box: Sandbox, **env: str) -> subprocess.CompletedProcess:
+ return box.run("toolchain/setup.sh", STUB_VENV=str(box.root / "template" / ".venv" / "bin"), **env)
+
+ def test_the_first_python_that_fits_makes_the_venv(self):
+ box = self.sandbox()
+ box.stub("python3.12", python("3.12.9", fits=False)) # present but refused
+ box.stub("python3.11", python("3.11.4", fits=True))
+ box.stub("python3", python("3.9.6", fits=True)) # never reached
+ r = self.run_setup(box)
+ self.assertEqual(r.returncode, 0, r.stderr)
+ self.assertEqual(r.stdout.splitlines(), ["ok Python 3.11.4",
+ f" installing marimo {VERSION} and the usual libraries (a minute or two)",
+ f"ok marimo {VERSION}"])
+ self.assertIn("python3.11 -m venv .venv", box.logged())
+ self.assertIn(f"python -m pip install --quiet marimo=={VERSION} numpy pandas polars altair matplotlib duckdb pyarrow", box.logged())
+ self.assertFalse(any(c.startswith("python3 ") for c in box.logged()))
+
+ def test_an_existing_venv_is_reused_and_a_failed_pip_upgrade_is_not_fatal(self):
+ box = self.sandbox()
+ box.stub("python3.13", python("3.13.1", fits=True))
+ box.venv()
+ # pip upgrade fails, the install itself succeeds
+ box.stub("python", 'case "$*" in *--upgrade*) exit 1 ;; esac', where=box.install / ".venv" / "bin")
+ r = self.run_setup(box)
+ self.assertEqual(r.returncode, 0, r.stderr)
+ self.assertNotIn("python3.13 -m venv .venv", box.logged())
+ self.assertIn("python -m pip install --quiet --upgrade pip", box.logged())
+ self.assertEqual(r.stdout.splitlines()[::2], ["ok Python 3.13.1", f"ok marimo {VERSION}"])
+
+ def test_no_python_that_fits_is_a_miss(self):
+ box = self.sandbox()
+ box.stub("python3", python("3.14.0", fits=False))
+ r = self.run_setup(box)
+ self.assertEqual((r.returncode, r.stdout), (1, "miss python 3.10–3.13 (brew install [email protected])\n"))
+
+ def test_a_failed_install_fails_setup(self):
+ box = self.sandbox()
+ box.stub("python3.12", python("3.12.9", fits=True))
+ box.stub("python", "exit 1", where=box.root / "template" / ".venv" / "bin")
+ r = self.run_setup(box)
+ self.assertNotEqual(r.returncode, 0)
+ self.assertNotIn("ok marimo", r.stdout)
+
+
+class InitWorkspace(unittest.TestCase):
+ def test_seeds_the_verdict_with_the_venv_python(self):
+ box = Sandbox(self)
+ box.venv()
+ ws = box.root / "ws"
+ ws.mkdir()
+ r = box.run("toolchain/init-workspace.sh", cwd=ws, HARNESS_DSH_DIR=str(box.install), PIP_EXIT="1") # a failing verdict does not stop init
+ self.assertEqual(r.returncode, 0, r.stderr)
+ self.assertTrue((ws / ".harness").is_dir())
+ self.assertEqual(box.logged(), [f"python {box.install}/toolchain/verdict.py"])
+
+ def test_needs_the_install_dir(self):
+ box = Sandbox(self)
+ r = box.run("toolchain/init-workspace.sh", cwd=box.root)
+ self.assertNotEqual(r.returncode, 0)
+ self.assertIn("HARNESS_DSH_DIR", r.stderr)
+
+
+class Viewer(unittest.TestCase):
+ def test_runs_viewer_py_with_the_venv_python(self):
+ box = Sandbox(self)
+ box.stub("python", 'echo "port=$HARNESS_VIEWER_PORT workspace=$HARNESS_WORKSPACE"', where=box.install / ".venv" / "bin")
+ r = box.run("viewer.sh", cwd=box.root, HARNESS_VIEWER_PORT="4123", HARNESS_WORKSPACE="/Users/example/ws")
+ self.assertEqual((r.returncode, r.stdout), (0, "port=4123 workspace=/Users/example/ws\n"), r.stderr)
+ self.assertEqual(box.logged(), [f"python {box.install}/viewer.py"])
+
+ def test_needs_a_port_and_a_workspace(self):
+ box = Sandbox(self)
+ for env, missing in (({"HARNESS_WORKSPACE": "/Users/example/ws"}, "HARNESS_VIEWER_PORT"), ({"HARNESS_VIEWER_PORT": "4123"}, "HARNESS_WORKSPACE")):
+ with self.subTest(missing=missing):
+ r = box.run("viewer.sh", **env)
+ self.assertNotEqual(r.returncode, 0)
+ self.assertIn(missing, r.stderr)
+ self.assertEqual(box.logged(), [])
+
+
+if __name__ == "__main__":
+ unittest.main()
diff --git a/store/agents/marimo/toolchain/test_verdict.py b/store/agents/marimo/toolchain/test_verdict.py
index a82b38d11..f32e9ce85 100644
--- a/store/agents/marimo/toolchain/test_verdict.py
+++ b/store/agents/marimo/toolchain/test_verdict.py
@@ -1,8 +1,12 @@
-import sys, unittest
+import contextlib, io, json, os, runpy, subprocess, sys, tempfile, unittest
from pathlib import Path
+from unittest import mock
sys.path.insert(0, str(Path(__file__).parent))
+import verdict
from verdict import count_cells, judge
+HERE = Path(__file__).resolve().parent
+
NB = '''import marimo
app = marimo.App()
@@ -17,17 +21,257 @@ def _(mo):
return
'''
+# What `marimo check` (the pinned 0.24.2) prints for a notebook with one breaking and one formatting
+# issue: a header per issue, where it is, an excerpt, a hint, a count.
+CHECK_OUT = """critical[multiple-definitions]: Variable 'x' is defined in multiple cells
+ --> /Users/example/ws/notebook.py:7:1
+ 7 | def _():
+ 8 | x = 1
+ | ^
+ ...
+ 13 | def _():
+hint: Variables must be unique across cells. Alternatively, they can be private with an underscore prefix (i.e. `_x`.)
+
+warning[general-formatting]: Expected `__generated_with` assignment for marimo version number.
+ --> /Users/example/ws/notebook.py:2:1
+ 2 |
+ | ^
+
+Found 2 issues.
+"""
+
+
class Verdict(unittest.TestCase):
def test_cells_are_counted(self):
self.assertEqual(count_cells(NB), 2); self.assertEqual(count_cells("def ("), -1)
+ def test_async_cells_functions_and_classes_are_cells(self):
+ source = NB + "\[email protected]\nasync def _():\n return\n\[email protected]\ndef helper():\n return 1\n\[email protected]_definition\nclass Thing:\n pass\n\n@staticmethod\ndef not_a_cell():\n pass\n"
+ self.assertEqual(count_cells(source), 5)
def test_runs(self):
v = judge(2, "All checks passed", 0, "", 0, "notebook.py")
self.assertTrue(v["ready"]); self.assertEqual(v["summary"], "notebook.py · 2 cells · runs")
+ self.assertEqual(v["findings"], [])
def test_check_failure(self):
v = judge(2, "notebook.py:12: error MB001 multiple definitions of x", 1, None, None, "notebook.py")
self.assertFalse(v["ready"]); self.assertEqual(v["phases"][1]["state"], "failed"); self.assertEqual(v["findings"][0]["severity"], "error")
def test_run_failure(self):
v = judge(2, "", 0, "Traceback\nZeroDivisionError: division by zero", 1, "notebook.py")
self.assertFalse(v["ready"]); self.assertEqual(v["phases"][2]["state"], "failed"); self.assertIn("ZeroDivisionError", v["findings"][0]["message"])
+ def test_a_run_that_failed_silently_says_how_it_exited(self):
+ v = judge(1, "", 0, None, -9, "notebook.py")
+ self.assertEqual(v["findings"][0]["message"], "the notebook exited -9")
+ self.assertEqual(v["summary"], "notebook.py · 1 cell · 1 error")
+
+ def test_one_finding_per_issue_with_its_place_not_one_per_line(self):
+ v = judge(3, CHECK_OUT, 1, None, None, "notebook.py")
+ self.assertEqual(v["findings"], [
+ {"severity": "error", "kind": "check", "message": "Variable 'x' is defined in multiple cells (multiple-definitions)", "ref": "notebook.py:7:1"},
+ {"severity": "warning", "kind": "check", "message": "Expected `__generated_with` assignment for marimo version number. (general-formatting)", "ref": "notebook.py:2:1"},
+ ])
+ self.assertEqual(v["summary"], "notebook.py · 3 cells · 1 error")
+ self.assertEqual([p["state"] for p in v["phases"]], ["done", "failed", "pending"])
+
+ def test_formatting_warnings_do_not_stop_a_notebook_that_runs(self):
+ out = "warning[markdown-indentation]: Markdown cell should be dedented\n --> notebook.py:16:1\n 16 | @app.cell\n\nFound 1 issue.\n"
+ v = judge(5, out, 0, "", 0, "notebook.py")
+ self.assertTrue(v["ready"])
+ self.assertEqual([(f["severity"], f.get("ref")) for f in v["findings"]], [("warning", "notebook.py:16:1")])
+
+ def test_a_runtime_error_fails_the_check_even_when_check_exits_0(self):
+ v = judge(2, "error[self-import]: Importing a module with the same name as the file\n", 0, "", 0, "notebook.py")
+ self.assertFalse(v["ready"]); self.assertEqual(v["phases"][1]["state"], "failed")
+ self.assertNotIn("ref", v["findings"][0], "a header with nothing after it has no place")
+
+ def test_info_stays_info(self):
+ v = judge(2, "info[wasm]: not available in WebAssembly\n --> notebook.py:3:1\n", 0, "", 0, "notebook.py")
+ self.assertTrue(v["ready"]); self.assertEqual(v["findings"][0]["severity"], "info")
+
+ def test_a_check_that_fails_without_an_issue_says_why(self):
+ v = judge(2, "Failed to parse: notebook.py (not a valid notebook)\n", 1, None, None, "notebook.py")
+ self.assertEqual(v["findings"], [{"severity": "error", "kind": "check", "message": "Failed to parse: notebook.py (not a valid notebook)"}])
+ v = judge(2, "", 2, None, None, "notebook.py")
+ self.assertEqual(v["findings"][0]["message"], "marimo check exited 2")
+
+ def test_a_notebook_that_does_not_parse(self):
+ v = judge(-1, "", None, None, None, "notebook.py")
+ self.assertFalse(v["ready"]); self.assertIsNone(v["artifact"])
+ self.assertEqual(v["summary"], "notebook.py · 1 error")
+ self.assertEqual(v["findings"][0]["kind"], "syntax")
+
+ def test_nothing_written_yet(self):
+ v = judge(0, "", None, None, None, "notebook.py")
+ self.assertEqual(v["summary"], "notebook.py · no cells yet")
+ self.assertEqual([p["state"] for p in v["phases"]], ["active", "pending", "pending"])
+
+ def test_checked_but_not_run(self):
+ self.assertEqual(judge(2, "", 0, None, None, "notebook.py")["summary"], "notebook.py · 2 cells · not run")
+
+
+def completed(args, code=0, stdout="", stderr=""):
+ return subprocess.CompletedProcess(args, code, stdout, stderr)
+
+
+class Main(unittest.TestCase):
+ """verdict.py on a workspace, with `marimo check` and the notebook run stubbed at subprocess.run."""
+
+ def setUp(self):
+ tmp = tempfile.TemporaryDirectory()
+ self.addCleanup(tmp.cleanup)
+ self.ws = Path(tmp.name)
+ for name, value in (("WS", self.ws), ("MARIMO", "/Users/example/.venv/bin/marimo"), ("PY", "/Users/example/.venv/bin/python")):
+ patcher = mock.patch.object(verdict, name, value)
+ patcher.start()
+ self.addCleanup(patcher.stop)
+
+ def main(self, *argv, run=None):
+ calls = []
+
+ def fake_run(args, **kwargs):
+ calls.append((args, kwargs))
+ return run(args) if run else completed(args)
+
+ out = io.StringIO()
+ with mock.patch.object(verdict.subprocess, "run", fake_run), contextlib.redirect_stdout(out):
+ code = verdict.main(["verdict.py", *argv])
+ return code, out.getvalue(), json.loads((self.ws / ".harness" / "verdict.json").read_text()), calls
+
+ def test_a_notebook_that_checks_and_runs(self):
+ (self.ws / "notebook.py").write_text(NB)
+ code, printed, v, calls = self.main()
+ self.assertEqual(code, 0)
+ self.assertEqual(printed.splitlines(), ["ready · notebook.py · 2 cells · runs"])
+ target = str(self.ws / "notebook.py")
+ self.assertEqual([c[0] for c in calls], [["/Users/example/.venv/bin/marimo", "check", target], ["/Users/example/.venv/bin/python", target]])
+ self.assertEqual({(c[1]["cwd"], c[1]["timeout"]) for c in calls}, {(self.ws, 120), (self.ws, 300)})
+
+ def test_a_named_notebook_and_a_failing_check_that_skips_the_run(self):
+ (self.ws / "analysis.py").write_text(NB)
+ code, printed, v, calls = self.main(str(self.ws / "analysis.py"), run=lambda args: completed(args, 1, CHECK_OUT))
+ self.assertEqual(code, 1)
+ self.assertEqual(len(calls), 1, "a notebook that fails its check is not run")
+ self.assertEqual(v["artifact"], "analysis.py")
+ self.assertEqual(printed.splitlines(), ["not ready · analysis.py · 2 cells · 1 error",
+ " error Variable 'x' is defined in multiple cells (multiple-definitions)",
+ " warning Expected `__generated_with` assignment for marimo version number. (general-formatting)"])
+
+ def test_check_output_on_stderr_counts_too(self):
+ (self.ws / "notebook.py").write_text(NB)
+ _, _, v, _ = self.main(run=lambda args: completed(args, 1, "", "Failed to parse: notebook.py (not a valid notebook)\n"))
+ self.assertEqual(v["findings"][0]["message"], "Failed to parse: notebook.py (not a valid notebook)")
+
+ def test_marimo_missing(self):
+ (self.ws / "notebook.py").write_text(NB)
+
+ def run(args):
+ raise FileNotFoundError(2, "No such file or directory", args[0])
+
+ _, _, v, calls = self.main(run=run)
+ self.assertEqual(len(calls), 1)
+ self.assertTrue(v["findings"][0]["message"].startswith("error: marimo check could not run ("), v["findings"])
+ self.assertEqual(v["phases"][1]["state"], "failed")
+
+ def test_a_check_that_hangs(self):
+ (self.ws / "notebook.py").write_text(NB)
+
+ def run(args):
+ raise subprocess.TimeoutExpired(args, 120)
+
+ _, _, v, _ = self.main(run=run)
+ self.assertIn("could not run", v["findings"][0]["message"])
+
+ def test_a_notebook_that_hangs(self):
+ (self.ws / "notebook.py").write_text(NB)
+
+ def run(args):
+ if args[1] == "check":
+ return completed(args)
+ raise subprocess.TimeoutExpired(args, 300)
+
+ code, _, v, _ = self.main(run=run)
+ self.assertEqual(code, 1)
+ self.assertEqual(v["findings"], [{"severity": "error", "kind": "run", "message": "the notebook did not finish within 5 minutes"}])
+ self.assertEqual(v["phases"][2]["state"], "failed")
+
+ def test_no_notebook_and_a_broken_notebook_run_nothing(self):
+ code, _, v, calls = self.main()
+ self.assertEqual((code, calls, v["summary"]), (1, [], "notebook.py · no cells yet"))
+ (self.ws / "notebook.py").write_text("def (")
+ code, _, v, calls = self.main()
+ self.assertEqual((code, calls, v["summary"]), (1, [], "notebook.py · 1 error"))
+
+ def test_run_as_a_script(self):
+ (self.ws / "notebook.py").write_text(NB)
+ env = {"HARNESS_WORKSPACE": str(self.ws), "MARIMO": "/Users/example/.venv/bin/marimo"}
+ with mock.patch.dict(os.environ, env), mock.patch.object(sys, "argv", ["verdict.py"]), \
+ mock.patch("subprocess.run", lambda args, **kw: completed(args)), contextlib.redirect_stdout(io.StringIO()):
+ with self.assertRaises(SystemExit) as exit_:
+ runpy.run_path(str(HERE / "verdict.py"), run_name="__main__")
+ self.assertEqual(exit_.exception.code, 0)
+ self.assertTrue(json.loads((self.ws / ".harness" / "verdict.json").read_text())["ready"])
+
+
+try:
+ import marimo # noqa: F401
+except ImportError: # the plain `python3 -m unittest` run, outside the venv
+ marimo = None
+
+GOOD = '''import marimo
+
+__generated_with = "0.24.2"
+app = marimo.App()
+
+
+def _():
+ x = 1
+ return (x,)
+
+
+async def _(x):
+ y = x + 1
+ return (y,)
+
+
+class Thing:
+ value = 3
+
+
+if __name__ == "__main__":
+ app.run()
+'''
+
+
[email protected](marimo is None, "marimo is not importable here; run with .venv/bin/python")
+class RealMarimo(unittest.TestCase):
+ """The pinned marimo's own check and the notebook run as a script, on notebooks small enough to read."""
+
+ def verdict_for(self, source: str) -> tuple[int, dict]:
+ with tempfile.TemporaryDirectory() as d:
+ (Path(d) / "notebook.py").write_text(source)
+ with mock.patch.object(verdict, "WS", Path(d)), mock.patch.object(verdict, "PY", sys.executable), \
+ mock.patch.object(verdict, "MARIMO", str(Path(sys.executable).parent / "marimo")), contextlib.redirect_stdout(io.StringIO()):
+ code = verdict.main(["verdict.py"])
+ return code, json.loads((Path(d) / ".harness" / "verdict.json").read_text())
+
+ def test_a_clean_notebook_with_async_cells_and_a_class_is_ready(self):
+ code, v = self.verdict_for(GOOD)
+ self.assertEqual(code, 0, v)
+ self.assertEqual(v["summary"], "notebook.py · 3 cells · runs")
+ self.assertEqual(v["findings"], [])
+
+ def test_a_breaking_issue_is_one_error_with_its_place(self):
+ code, v = self.verdict_for(GOOD.replace(" y = x + 1\n return (y,)", " x = 2\n return (x,)").replace("async def _(x)", "def _()"))
+ self.assertEqual(code, 1)
+ self.assertEqual(v["findings"], [{"severity": "error", "kind": "check", "message": "Variable 'x' is defined in multiple cells (multiple-definitions)", "ref": "notebook.py:8:1"}])
+ self.assertEqual([p["state"] for p in v["phases"]], ["done", "failed", "pending"])
+
+ def test_a_cell_that_raises_fails_the_run(self):
+ code, v = self.verdict_for(GOOD.replace("x = 1", "x = 1 / 0"))
+ self.assertEqual(code, 1)
+ self.assertEqual(v["findings"], [{"severity": "error", "kind": "run", "message": "ZeroDivisionError: division by zero"}])
+ self.assertEqual([p["state"] for p in v["phases"]], ["done", "done", "failed"])
+
if __name__ == "__main__": unittest.main()
diff --git a/store/agents/marimo/toolchain/test_viewer.py b/store/agents/marimo/toolchain/test_viewer.py
index e02297f04..061a4ea56 100644
--- a/store/agents/marimo/toolchain/test_viewer.py
+++ b/store/agents/marimo/toolchain/test_viewer.py
@@ -2,14 +2,23 @@
viewer.py layers three settings over marimo's config by wrapping the factory `marimo._server.start`
calls. If a marimo bump moves that factory, the pane still starts (with marimo's defaults) — this
-test is what notices.
+test is what notices. The tests with a stand-in marimo run anywhere; the real ones need the venv.
"""
+import contextlib
+import io
+import os
+import runpy
import sys
import tempfile
+import types
import unittest
from pathlib import Path
+from unittest import mock
-sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
+PACKAGE = Path(__file__).resolve().parent.parent
+sys.path.insert(0, str(PACKAGE))
+
+import viewer # noqa: E402
try:
import marimo._server.start as start
@@ -17,11 +26,83 @@ except ImportError: # the plain `python3 -m unittest` run, outside the venv
start = None
+def stand_in_marimo(test: unittest.TestCase, factory=True) -> types.SimpleNamespace:
+ """A marimo package tree in sys.modules for the length of the test: `_server.start` with a config
+ factory (or without one, as a moved marimo would be) and a `_cli.cli.main` that records its call."""
+ made = types.SimpleNamespace(calls=[], factory_calls=[])
+
+ class Manager:
+ def __init__(self, args, kwargs):
+ self.args, self.kwargs, self.overrides = args, kwargs, None
+
+ def with_overrides(self, overrides):
+ self.overrides = overrides
+ return self
+
+ def get_default_config_manager(*args, **kwargs):
+ made.factory_calls.append((args, kwargs))
+ return Manager(args, kwargs)
+
+ def cli_main(**kwargs):
+ made.calls.append({"kwargs": kwargs, "cwd": os.getcwd()})
+
+ modules = {name: types.ModuleType(name) for name in ("marimo", "marimo._server", "marimo._server.start", "marimo._cli", "marimo._cli.cli")}
+ modules["marimo"]._server, modules["marimo"]._cli = modules["marimo._server"], modules["marimo._cli"]
+ modules["marimo._server"].start = modules["marimo._server.start"]
+ modules["marimo._cli"].cli = modules["marimo._cli.cli"]
+ if factory:
+ modules["marimo._server.start"].get_default_config_manager = get_default_config_manager
+ modules["marimo._cli.cli"].main = cli_main
+ patcher = mock.patch.dict(sys.modules, modules)
+ patcher.start()
+ test.addCleanup(patcher.stop)
+ made.start = modules["marimo._server.start"]
+ return made
+
+
+class Pane(unittest.TestCase):
+ def setUp(self):
+ tmp = tempfile.TemporaryDirectory()
+ self.addCleanup(tmp.cleanup)
+ self.ws = str(Path(tmp.name).resolve())
+ self.addCleanup(os.chdir, os.getcwd())
+
+ def test_tune_layers_the_pane_settings_over_the_factory(self):
+ fake = stand_in_marimo(self)
+ viewer.tune()
+ manager = fake.start.get_default_config_manager(current_path="/Users/example/ws/notebook.py")
+ self.assertEqual(fake.factory_calls, [((), {"current_path": "/Users/example/ws/notebook.py"})])
+ self.assertEqual(manager.overrides, viewer.PANE_CONFIG)
+
+ def test_a_marimo_that_moved_the_factory_still_starts_and_says_so(self):
+ fake = stand_in_marimo(self, factory=False)
+ err = io.StringIO()
+ with contextlib.redirect_stderr(err):
+ viewer.tune()
+ self.assertRegex(err.getvalue(), r"^\[marimo pane\] running with marimo's defaults \(.*get_default_config_manager.*\)\n$")
+ self.assertFalse(hasattr(fake.start, "get_default_config_manager"))
+
+ def test_main_runs_marimo_edit_headless_on_the_workspace_notebook(self):
+ fake = stand_in_marimo(self)
+ with mock.patch.dict(os.environ, {"HARNESS_VIEWER_PORT": "4123", "HARNESS_WORKSPACE": self.ws}):
+ os.environ.pop("MARIMO_PANE_NOTEBOOK", None)
+ viewer.main()
+ self.assertEqual(fake.calls, [{"cwd": self.ws, "kwargs": {"prog_name": "marimo", "args": [
+ "edit", "--headless", "--host", "127.0.0.1", "--port", "4123", "--no-token", "--watch", "--skip-update-check", "notebook.py"]}}])
+ self.assertIsNotNone(fake.start.get_default_config_manager().overrides, "tune() ran before marimo started")
+
+ def test_the_notebook_can_be_named_and_the_file_runs_as_a_script(self):
+ fake = stand_in_marimo(self)
+ env = {"HARNESS_VIEWER_PORT": "4124", "HARNESS_WORKSPACE": self.ws, "MARIMO_PANE_NOTEBOOK": "analysis.py"}
+ with mock.patch.dict(os.environ, env):
+ runpy.run_path(str(PACKAGE / "viewer.py"), run_name="__main__")
+ self.assertEqual(fake.calls[0]["kwargs"]["args"][-1], "analysis.py")
+ self.assertEqual(fake.calls[0]["kwargs"]["args"][5], "4124")
+
+
@unittest.skipIf(start is None, "marimo is not importable here; run with .venv/bin/python")
class PaneConfig(unittest.TestCase):
def test_overrides_reach_the_server_config(self):
- import viewer
-
original = start.get_default_config_manager
try:
viewer.tune()
@@ -35,6 +116,27 @@ class PaneConfig(unittest.TestCase):
finally:
start.get_default_config_manager = original
+ def test_the_pinned_marimo_accepts_the_pane_command_line(self):
+ """marimo's own `edit` parses the flags and reaches the server start, which is stubbed."""
+ import marimo._cli.cli as cli
+
+ original = start.get_default_config_manager
+ self.addCleanup(setattr, start, "get_default_config_manager", original)
+ self.addCleanup(os.chdir, os.getcwd())
+ with tempfile.TemporaryDirectory() as ws, mock.patch.object(cli, "start") as server:
+ Path(ws, "notebook.py").write_text("import marimo\napp = marimo.App()\n\[email protected]\ndef _():\n return\n")
+ env = {"HARNESS_VIEWER_PORT": "4125", "HARNESS_WORKSPACE": ws, "MARIMO_SKIP_UPDATE_CHECK": "1"}
+ with mock.patch.dict(os.environ, env), contextlib.redirect_stdout(io.StringIO()):
+ os.environ.pop("MARIMO_PANE_NOTEBOOK", None)
+ with self.assertRaises(SystemExit) as exit_:
+ viewer.main()
+ self.assertEqual(os.path.realpath(os.getcwd()), os.path.realpath(ws))
+ self.assertEqual(exit_.exception.code, 0)
+ kwargs = server.call_args.kwargs
+ self.assertEqual((kwargs["host"], kwargs["port"], kwargs["headless"], kwargs["watch"], str(kwargs["auth_token"])),
+ ("127.0.0.1", 4125, True, True, ""), "loopback, the given port, no browser, following the file, no token")
+ self.assertIn("notebook.py", repr(vars(kwargs["workspace"])))
+
if __name__ == "__main__":
unittest.main()
diff --git a/store/agents/marimo/toolchain/verdict.py b/store/agents/marimo/toolchain/verdict.py
index cacecba3b..434f6223f 100755
--- a/store/agents/marimo/toolchain/verdict.py
+++ b/store/agents/marimo/toolchain/verdict.py
@@ -9,12 +9,17 @@ definitions), Run (the notebook runs top to bottom as a script without an except
runs. The notebook is the artifact; the pane is marimo's editor on it.
"""
from __future__ import annotations
-import ast, json, os, subprocess, sys, time
+import ast, json, os, re, subprocess, sys, time
from pathlib import Path
WS = Path(os.environ.get("HARNESS_WORKSPACE") or os.getcwd()).resolve()
PY = sys.executable
MARIMO = os.environ.get("MARIMO") or str(Path(PY).parent / "marimo")
+# `marimo check` prints each issue as `critical[multiple-definitions]: message`, then ` --> file:line:col`
+# and a code excerpt; critical (breaking) and error (runtime) are errors, warning is formatting.
+DIAGNOSTIC = re.compile(r"^(critical|error|warning|info)\[([^\]]+)\]:\s*(.+)$")
+WHERE = re.compile(r"^-->\s*.+:(\d+):(\d+)$")
+EXCERPT = re.compile(r"^(\d+\s*)?\||^\.\.\.$|^hint:|^Found \d+ issues?\.$|^Updated \d+ files?\.$")
def count_cells(source: str) -> int:
@@ -24,7 +29,8 @@ def count_cells(source: str) -> int:
return -1
n = 0
for node in ast.walk(tree):
- if isinstance(node, ast.FunctionDef):
+ # Cells may be async; @app.class_definition decorates a class.
+ if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef, ast.ClassDef)):
for d in node.decorator_list:
name = d.func if isinstance(d, ast.Call) else d
if isinstance(name, ast.Attribute) and name.attr in ("cell", "function", "class_definition"):
@@ -36,12 +42,18 @@ def judge(cells: int, check_out: str, check_code: int | None, run_err: str | Non
findings: list[dict] = []
if cells < 0:
findings.append({"severity": "error", "kind": "syntax", "message": f"{rel} does not parse"})
- for line in check_out.splitlines():
- line = line.strip()
- if not line or line.startswith(("Checking", "✓", "All checks")):
- continue
- sev = "error" if ("error" in line.lower() or "✗" in line or "MB" in line.split(":")[0]) else "warning"
- findings.append({"severity": sev, "kind": "check", "message": line[:300]})
+ lines = [line.strip() for line in check_out.splitlines() if line.strip()]
+ other = [] # what check printed besides issues: why it failed when there is no issue to show
+ for i, line in enumerate(lines):
+ issue = DIAGNOSTIC.match(line)
+ if issue:
+ sev = "error" if issue[1] in ("critical", "error") else issue[1]
+ where = WHERE.match(lines[i + 1]) if i + 1 < len(lines) else None
+ findings.append({"severity": sev, "kind": "check", "message": f"{issue[3]} ({issue[2]})"[:300], **({"ref": f"{rel}:{where[1]}:{where[2]}"} if where else {})})
+ elif not WHERE.match(line) and not EXCERPT.match(line):
+ other.append(line)
+ if check_code not in (None, 0) and not any(f["severity"] == "error" and f["kind"] == "check" for f in findings):
+ findings.append({"severity": "error", "kind": "check", "message": (other[-1] if other else f"marimo check exited {check_code}")[:300]})
if run_code not in (None, 0):
tail = (run_err or "").strip().splitlines()
findings.append({"severity": "error", "kind": "run", "message": (tail[-1] if tail else f"the notebook exited {run_code}")[:300]})
diff --git a/store/agents/marimo/viewer.py b/store/agents/marimo/viewer.py
index 24c77fd4a..c3a760b22 100644
--- a/store/agents/marimo/viewer.py
+++ b/store/agents/marimo/viewer.py
@@ -37,7 +37,7 @@ def tune() -> None:
return factory(*args, **kwargs).with_overrides(PANE_CONFIG)
start.get_default_config_manager = pane_config_manager
- except Exception as error: # pragma: no cover - only on a marimo that moved things
+ except Exception as error: # only on a marimo that moved things
print(f"[marimo pane] running with marimo's defaults ({error})", file=sys.stderr)