lawsynth/tests/_engine.py
caelum0x c8700e78f8 feat: §8.5 parameter refinement + §8.6 causal hypothesis + executable conformance suites
Discovery: opt-in joint parameter refinement (refine.rs, via lawsynth-opt
coordinate_minimize, guaranteed non-worsening trajectory MSE) and dependency/
causal hypothesis discovery (causal.rs, via lawsynth-causal Granger/independence/
time-order, framed as a hypothesis with its assumption set). CLI --refine/--causal.

Verification: wire tests/{conformance,end-to-end,cross-language} to execute against
the real compiled CLI — valid worlds round-trip, every bad-* asserted-rejected,
unbuilt native/TS legs honestly skipped. 26 pytest cases.

Rust workspace 342->352 tests; +26 system-verification cases. clippy/fmt clean.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-08-17 19:05:23 +03:00

78 lines
2.5 KiB
Python

#!/usr/bin/env python3
"""Shared location and invocation helpers for the compiled LawSynth engine.
Every cross-language, end-to-end, and conformance suite drives the real
``lawsynth`` CLI through this module. The engine is built once with
``cargo build -p lawsynth-cli`` and reused from ``target/debug/lawsynth``.
When the prebuilt binary is missing we fall back to ``cargo run`` so a case is
still executed against the real engine rather than an emulation; when neither
the binary nor ``cargo`` is available the suites skip cleanly with an explicit
message instead of silently passing.
"""
from __future__ import annotations
import functools
import shutil
import subprocess
from pathlib import Path
# ``tests/_engine.py`` -> parents[1] is the repository root.
ROOT = Path(__file__).resolve().parents[1]
_PROFILES = ("debug", "release")
_BINARY_NAMES = ("lawsynth", "lawsynth.exe")
def prebuilt_binary() -> Path | None:
"""Return the compiled ``lawsynth`` binary if one is already on disk."""
for profile in _PROFILES:
for name in _BINARY_NAMES:
candidate = ROOT / "target" / profile / name
if candidate.exists():
return candidate
return None
@functools.lru_cache(maxsize=1)
def ensure_engine() -> Path | None:
"""Locate the engine binary, building it once when necessary.
Returns the path to the binary, or ``None`` when the engine cannot be
produced offline (missing ``cargo`` toolchain or a failed build). Callers
translate ``None`` into an explicit skip.
"""
existing = prebuilt_binary()
if existing is not None:
return existing
if shutil.which("cargo") is None:
return None
result = subprocess.run(
["cargo", "build", "-p", "lawsynth-cli"],
cwd=ROOT,
text=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
check=False,
)
if result.returncode != 0:
return None
return prebuilt_binary()
def cli(*arguments: str) -> subprocess.CompletedProcess[str]:
"""Invoke the real engine, preferring the prebuilt binary for speed."""
binary = prebuilt_binary()
if binary is not None:
command: list[str] = [str(binary), *arguments]
else:
command = ["cargo", "run", "--quiet", "-p", "lawsynth-cli", "--bin", "lawsynth", "--", *arguments]
return subprocess.run(
command,
cwd=ROOT,
text=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
check=False,
)