lingologos-physics / tests /test_isolation.py
tailot's picture
update physics bundle
875f067 verified
Raw History Blame Contribute Delete
2.9 kB
"""The physics bundle stays separate: it collaborates with the mathematics dataset as a process.
The rule the dataset is built on is that physics does not become a layer on top of the mathematics
code. The chain asks mathematics a question in the mathematics language and reads the verdict back as
text, the way any two LingoLOGOS instances do it across an orchestrator. These tests read the syntax
trees, so the property cannot quietly rot while the code grows.
"""
import ast
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
# The code that ships with the dataset. The tests are scanned for imports as well, but not for the
# words they forbid: a test has to name what it is keeping out.
SHIPPED = (list((ROOT / "lgphys").rglob("*.py")) + list((ROOT / "tools").rglob("*.py"))
+ [ROOT / "agents" / "physics_agent"])
SOURCES = SHIPPED + list((ROOT / "tests").rglob("*.py"))
def _tree(path: Path) -> ast.Module:
return ast.parse(path.read_text(encoding="utf-8"))
def _imports(path: Path):
for node in ast.walk(_tree(path)):
if isinstance(node, ast.Import):
for alias in node.names:
yield alias.name
elif isinstance(node, ast.ImportFrom) and node.module:
yield node.module
def test_no_module_of_the_mathematics_bundle_is_imported():
for path in SOURCES:
for name in _imports(path):
assert not name.startswith("lgmath"), "%s imports %s" % (path.name, name)
def test_no_float_arithmetic_anywhere_in_the_bundle():
# Every number in this dataset is an exact rational, so the float module and the float builtin
# have no business here: a rounded value would break the two roads agreeing on the same answer.
for path in SHIPPED:
assert "math" not in set(_imports(path)), "%s imports math" % path.name
tree = _tree(path)
for node in ast.walk(tree):
if isinstance(node, ast.Name) and node.id in ("float", "round"):
raise AssertionError("%s uses %s" % (path.name, node.id))
if isinstance(node, ast.Constant) and isinstance(node.value, float):
raise AssertionError("%s holds the float %r" % (path.name, node.value))
def test_the_mathematics_bundle_is_reached_as_a_process_only():
text = (ROOT / "lgphys" / "invoker.py").read_text(encoding="utf-8")
assert "subprocess" in text
assert "solve.py" in text
for path in SHIPPED:
assert "lgmath" not in path.read_text(encoding="utf-8"), path.name
def test_the_bundle_does_not_write_outside_itself():
# The tools copy their node folders into a temporary workspace; nothing in the bundle names a
# path above the bundle root, which is what keeps a run of this dataset inside this folder.
for path in SHIPPED:
text = path.read_text(encoding="utf-8")
assert ".." + "/" not in text, path.name