"""Run pytest and assemble the W1-03 acceptance report from measured evidence.""" import argparse import hashlib import json import os from pathlib import Path import subprocess import sys import tempfile from xml.etree import ElementTree from .query_tools import TOOLS from .snapshot import DATASETS def read_evidence(folder, name): path = folder / f"{name}.json" return json.loads(path.read_bytes()) if path.is_file() else {} def run(args): with tempfile.TemporaryDirectory(prefix="faisi-w103-") as folder: temp = Path(folder) junit = temp / "pytest.xml" env = os.environ.copy() env.update( FAISI_W103_SNAPSHOT_ROOT=str(args.snapshot_root.resolve()), FAISI_W103_WINDOW_INDEX=str(args.window_index.resolve()), FAISI_W103_TOKENIZER=str(args.tokenizer.resolve()), FAISI_W103_TEST_RESULTS=str(temp), TOKENIZERS_PARALLELISM="false", OPENBLAS_NUM_THREADS="1", OMP_NUM_THREADS="1", MKL_NUM_THREADS="1", ARROW_DEFAULT_MEMORY_POOL="system", ) result = subprocess.run([sys.executable, "-m", "pytest", "-q", str(args.tests), "--junitxml", str(junit)], env=env, check=False) tests = errors = skipped = 0 if junit.is_file(): xml = ElementTree.parse(junit).getroot() for suite in xml.iter("testsuite"): tests += int(suite.attrib.get("tests", 0)) errors += int(suite.attrib.get("failures", 0)) + int(suite.attrib.get("errors", 0)) skipped += int(suite.attrib.get("skipped", 0)) leakage = read_evidence(temp, "leakage") permutation = read_evidence(temp, "permutation") concurrency = read_evidence(temp, "concurrency") passed = tests - errors - skipped coverage = sorted(concurrency.get("dataset_counts", {})) return { "overall_pass": bool( result.returncode == 0 and tests > 0 and not errors and not skipped and set(coverage) == set(DATASETS) and leakage.get("raw_entity_name_leakage_instances") == 0 and leakage.get("cases") == 50 and permutation.get("verified", False) and concurrency.get("active_processes") == 32 and concurrency.get("calls") == 10000 and concurrency.get("error_rate") == 0 and concurrency.get("mean_latency_ms", float("inf")) < 3 and concurrency.get("p99_latency_ms", float("inf")) < 10 and max(leakage.get("max_response_tokens", 0), concurrency.get("max_response_tokens", 0)) <= 400 ), "datasets_covered": [dataset for dataset in DATASETS if dataset in coverage], "pytest_passed_count": passed, "pytest_failed_count": errors, "pytest_skipped_count": skipped, "raw_entity_name_leakage_instances": leakage.get("raw_entity_name_leakage_instances"), "permutation_invariance_verified": permutation.get("verified", False), "concurrency_32proc_10k_calls": concurrency, "leakage_audit": leakage, "permutation_audit": permutation, "max_response_tokens": max(leakage.get("max_response_tokens", 0), concurrency.get("max_response_tokens", 0)), "tokenizer": {"name": "Qwen2.5-14B-Instruct", "sha256": hashlib.sha256(args.tokenizer.read_bytes()).hexdigest()}, } def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--snapshot-root", required=True, type=Path) parser.add_argument("--window-index", required=True, type=Path) parser.add_argument("--tokenizer", required=True, type=Path) parser.add_argument("--tests", type=Path, default=Path("tests/test_sandbox.py")) parser.add_argument("--report", type=Path, required=True) parser.add_argument("--tools-schema", type=Path) args = parser.parse_args() report = run(args) args.report.parent.mkdir(parents=True, exist_ok=True) temp = args.report.with_suffix(".tmp") temp.write_text(json.dumps(report, indent=2) + "\n") temp.replace(args.report) if args.tools_schema: args.tools_schema.parent.mkdir(parents=True, exist_ok=True) args.tools_schema.write_text(json.dumps(TOOLS, indent=2) + "\n") print(json.dumps(report, indent=2)) return 0 if report["overall_pass"] else 1 if __name__ == "__main__": raise SystemExit(main())