"""Bundle integrity and dependency boundaries, without compiling a model.""" import json import subprocess import sys from pathlib import Path import pytest from timestral_coreai import TimestralConfig from timestral_coreai.loading import BUNDLE_FORMAT, GRAPH_INPUTS, STAGE_INPUTS, STAGE_OUTPUTS, load_model, sha256, verify_bundle @pytest.fixture def bundle(tmp_path): config = TimestralConfig(patch_size=4, d_model=16, num_heads=2, mlp_hidden_dim=32, block_schedule=("time",), quantile_knots=(.1, .5, .9), block_size=2, context_length=16) stages = {"embedding": "embedding.aimodel", "blocks": ["block_000.aimodel"], "head": "head.aimodel"} for name in ("embedding.aimodel", "block_000.aimodel", "head.aimodel"): (tmp_path / name).mkdir() contents = {"config.json": json.dumps(config.to_dict()), "LICENSE": "test license", **{name + "/program": "test graph" for name in ("embedding.aimodel", "block_000.aimodel", "head.aimodel")}} for name, content in contents.items(): (tmp_path / name).write_text(content) manifest = dict(format=BUNDLE_FORMAT, dtype="float32", variant="5M", function="main", inputs=GRAPH_INPUTS, outputs=["quantiles"], stages=stages, stage_inputs=STAGE_INPUTS, stage_outputs=STAGE_OUTPUTS, shape_limits=dict(max_rows=256, max_context_patches=1056, max_forecast_patches=1056), files={ name: dict(bytes=(tmp_path / name).stat().st_size, sha256=sha256(tmp_path / name)) for name in contents}) (tmp_path / "manifest.json").write_text(json.dumps(manifest)) return tmp_path def test_manifest_checks_actual_bytes(bundle): path, manifest = verify_bundle(bundle, variant="5M") assert path == bundle.resolve() assert manifest["variant"] == "5M" (bundle / "block_000.aimodel/program").write_text("fake graph") # Same byte length. with pytest.raises(ValueError, match="integrity"): verify_bundle(bundle) def test_rejects_unverified_model_files(bundle): (bundle / "head.aimodel/extra").write_text("unlisted") with pytest.raises(ValueError, match="unverified"): verify_bundle(bundle) def test_rejects_escaping_manifest_path(bundle): manifest = json.loads((bundle / "manifest.json").read_text()) manifest["files"]["../outside"] = dict(bytes=0, sha256="") (bundle / "manifest.json").write_text(json.dumps(manifest)) with pytest.raises(ValueError, match="Unsafe"): verify_bundle(bundle) def test_rejects_variant_mismatch(bundle): with pytest.raises(ValueError, match="variant"): verify_bundle(bundle, variant="28M") @pytest.mark.parametrize("mutation,match", [ (lambda m: m.update(format="timestral-coreai-fp32-v1"), "Unsupported"), (lambda m: m["stages"]["blocks"].append("block_000.aimodel"), "unique"), (lambda m: m["stage_inputs"]["embedding"].append("context_positions"), "schema"), (lambda m: m["shape_limits"].update(max_rows=True), "shape limits"), (lambda m: m["stages"].update(embedding="../outside.aimodel"), "Unsafe"), ]) def test_staged_manifest_contract(bundle, mutation, match): manifest = json.loads((bundle / "manifest.json").read_text()) mutation(manifest) (bundle / "manifest.json").write_text(json.dumps(manifest)) with pytest.raises(ValueError, match=match): verify_bundle(bundle) def test_rejects_symlinked_asset_directory_even_inside_bundle(bundle): (bundle / "head.aimodel/alias").symlink_to(bundle / "embedding.aimodel", target_is_directory=True) with pytest.raises(ValueError, match="symlinks"): verify_bundle(bundle) def test_stage_count_must_match_verified_config(bundle): config = json.loads((bundle / "config.json").read_text()) config["block_schedule"].append("time") (bundle / "config.json").write_text(json.dumps(config)) manifest = json.loads((bundle / "manifest.json").read_text()) manifest["files"]["config.json"] = dict(bytes=(bundle / "config.json").stat().st_size, sha256=sha256(bundle / "config.json")) (bundle / "manifest.json").write_text(json.dumps(manifest)) with pytest.raises(ValueError, match="stage count"): verify_bundle(bundle) def test_missing_local_bundle_is_not_downloaded(tmp_path): with pytest.raises(FileNotFoundError, match="Local"): load_model(tmp_path / "not-a-bundle") def test_package_import_does_not_import_training_or_mlx_runtimes(): root = Path(__file__).resolve().parents[1] result = subprocess.run( [sys.executable, "-c", "import sys; import timestral_coreai; " "assert 'torch' not in sys.modules; assert 'mlx.core' not in sys.modules; " "assert 'coreai_torch' not in sys.modules; assert 'coreai' not in sys.modules"], cwd=root, capture_output=True, text=True, ) assert result.returncode == 0, result.stdout + result.stderr