Files
OBLITERATUS/tests/test_conditional_gate_scripts.py
T
Joseph MaglyandClaude Opus 5.5 2539cdb2b7 feat(experiments): chat templates, seeded sampling, batching and CUDA observation
Remove the observation runner's execution limits.

- Chat templates: an optional prompt section renders each prompt
  through the tokenizer's chat template (optional system message,
  generation prompt), tokenized without extra special tokens. The
  rendered-prompt digest joins the generation step identity and each
  record; capture uses the same rendered prompts.
- Decoding: seeded sampling (temperature, top-p, top-k) alongside
  greedy, with a per-example seed derived from the spec seed and example
  id. Comparison treats sampled outcomes as exact only when strategy,
  device, dtype and seed match, and says why otherwise.
- Batching: optional left-padded greedy batches with an explicit,
  recorded pad token; rows are cut at their first eos so batched and
  unbatched outcomes agree. Sampling with batches is refused because
  per-example seeds cannot hold.
- Devices: dense CUDA for the runner and capture, with shards written
  from CPU copies and device/dtype recorded. Quantized, offloaded,
  dispatched, meta, split-device and MPS models are refused with
  explicit reasons.

The schema stays additive within version 1; options at their defaults
are dropped from the filled spec, so existing spec digests are
unchanged (pinned in tests).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-13 16:33:51 -05:00

1711 lines
66 KiB
Python

"""Tests for conditional policy, runner, and evidence helpers."""
from __future__ import annotations
import hashlib
import json
import os
import shutil
import subprocess
import sys
from datetime import date, timedelta
from pathlib import Path
import pytest
from obliteratus.distributed.contracts import contract_digest
from obliteratus.distributed.model_topology import TP2DeploymentLayout, TP2TopologyBinding
from obliteratus.distributed.probe_contracts import MAX_PROBE_LAYERS, ProbeDistillConfig
from obliteratus.distributed.export_contracts import ExportConfig
from obliteratus.distributed.surgery_contracts import ExciseConfig
from obliteratus.distributed.verification_contracts import VerificationConfig
from scripts import check_conditional_policy
from scripts import check_llama_tp_hardware_evidence
from scripts import conditional_gate_metadata
from scripts import conditional_gate_summary
from scripts import run_conditional_gate
from scripts import run_readonly_mount_namespace
ROOT = Path(__file__).parents[1]
TODAY = date(2026, 8, 16)
def _waiver(gate: str, **overrides) -> dict:
value = {
"gate": gate,
"reason": "No runner is currently attached.",
"issue": "https://github.com/elder-plinius/OBLITERATUS/issues/110",
"opened": "2026-08-16",
"expires": "2026-09-15",
"blocked_claim": f"{gate} support is not claimed.",
}
value.update(overrides)
return value
def test_committed_conditional_policy_is_complete():
policy = json.loads((ROOT / "ci" / "conditional-test-policy.json").read_text())
# Structural tests use the declared review date. The CI policy command
# separately enforces expiry against the real clock.
reviewed = max(date.fromisoformat(waiver["opened"]) for waiver in policy["environment_waivers"])
assert check_conditional_policy.validate(
ROOT / "ci" / "conditional-test-policy.json",
ROOT / "ci" / "test-quality-policy.json",
ROOT / ".github" / "workflows" / "conditional-tests.yml",
today=reviewed,
) == []
def test_ordinary_module_qualification_is_manual_and_rejects_required_skips():
workflow = (ROOT / ".github/workflows/conditional-tests.yml").read_text()
job = workflow.split(" module_placement_gpu:\n", 1)[1].split(" jetson:\n", 1)[0]
assert "github.event_name == 'workflow_dispatch' && inputs.run_module_gpu" in job
assert "scripts/run_conditional_gate.py module-placement-gpu" in job
assert "inputs.module_gpu_count" in job
assert "retention-days: 30" in job
assert run_conditional_gate.GATES["module-placement-gpu"] == "tests/conditional/test_gpu_qualification.py"
def test_committed_waivers_fail_closed_after_expiry():
policy = json.loads((ROOT / "ci" / "conditional-test-policy.json").read_text())
expired = max(date.fromisoformat(waiver["expires"]) for waiver in policy["environment_waivers"])
errors = check_conditional_policy.validate(
ROOT / "ci" / "conditional-test-policy.json",
ROOT / "ci" / "test-quality-policy.json",
ROOT / ".github" / "workflows" / "conditional-tests.yml",
today=expired + timedelta(days=1),
)
assert errors == [
f"environment waiver {waiver['gate']} expired on {waiver['expires']}"
for waiver in policy["environment_waivers"]
]
def test_cuda_job_replaces_locked_cpu_torch_with_same_version_cuda_build():
workflow = (ROOT / ".github" / "workflows" / "conditional-tests.yml").read_text()
cuda_job = workflow.split(" cuda:\n", maxsplit=1)[1].split(" mps:\n", maxsplit=1)[0]
assert "torch.__version__.split" in cuda_job
assert "--extra recovery" in cuda_job
assert "--extra quantization" in cuda_job
assert "UV_TORCH_BACKEND=cu130 uv pip install" in cuda_job
assert "--reinstall-package torch" in cuda_job
assert '"torch==$CUDA_TORCH_VERSION"' in cuda_job
assert "assert torch.version.cuda is not None" in cuda_job
assert 'uv pip check --python "$CONDITIONAL_ENV/bin/python"' in cuda_job
def test_documented_cuda_setup_installs_recovery_and_quantization():
guide = (ROOT / "docs" / "conditional-testing.md").read_text()
cuda_section = guide.split("## CUDA and bitsandbytes\n", maxsplit=1)[1].split(
"\n### ", maxsplit=1,
)[0]
setup = next(line for line in cuda_section.splitlines() if line.startswith("uv sync "))
assert "--extra dev" in setup
assert "--extra recovery" in setup
assert "--extra quantization" in setup
def test_jetson_job_is_manual_physical_trusted_and_retains_sanitized_evidence():
workflow = (ROOT / ".github" / "workflows" / "conditional-tests.yml").read_text()
jetson_job = workflow.split(" jetson:\n", maxsplit=1)[1].split(
" mps:\n",
maxsplit=1,
)[0]
assert "github.event_name == 'workflow_dispatch' && inputs.run_jetson" in jetson_job
assert "runs-on: [self-hosted, linux, ARM64, jetson]" in jetson_job
assert "scripts/setup_jetson.py" in jetson_job
assert "scripts/run_conditional_gate.py jetson-runtime" in jetson_job
assert "scripts/jetson_support.py" in jetson_job
assert "conditional-jetson-${{ github.run_attempt }}" in jetson_job
assert "retention-days: 30" in jetson_job
assert "actions/setup-python" not in jetson_job
policy = json.loads((ROOT / "ci" / "conditional-test-policy.json").read_text())
gate = next(value for value in policy["gates"] if value["id"] == "jetson-runtime")
assert gate["job"] == "jetson"
assert gate["runner"] == "self-hosted, linux, ARM64, jetson"
def test_policy_rejects_unknown_cpu_exclusion_gate(tmp_path):
policy = json.loads((ROOT / "ci" / "conditional-test-policy.json").read_text())
policy["environment_waivers"] = []
quality = {
"mature_cpu_scope": {
"exclusions": [{"path": "obliteratus/device.py", "conditional_gate": "missing"}]
}
}
policy_path = tmp_path / "policy.json"
quality_path = tmp_path / "quality.json"
policy_path.write_text(json.dumps(policy))
quality_path.write_text(json.dumps(quality))
errors = check_conditional_policy.validate(
policy_path,
quality_path,
ROOT / ".github" / "workflows" / "conditional-tests.yml",
today=TODAY,
)
assert errors == ["CPU exclusion obliteratus/device.py references unknown gate missing"]
def test_junit_counts_and_report_are_machine_readable(tmp_path):
junit = tmp_path / "result.xml"
junit.write_text(
'<testsuites><testsuite tests="3" failures="1" errors="0" skipped="1"/>'
'<testsuite tests="2" failures="0" errors="1" skipped="0"/></testsuites>'
)
assert run_conditional_gate.counts(junit) == {
"tests": 5,
"failures": 1,
"errors": 1,
"skipped": 1,
}
report = tmp_path / "report.json"
run_conditional_gate.write_report(report, "network-services", "passed", counts={"tests": 1})
evidence = json.loads(report.read_text())
assert evidence["gate"] == "network-services"
assert evidence["status"] == "passed"
assert evidence["counts"] == {"tests": 1}
@pytest.mark.parametrize("sha", ["a" * 40, "a" * 64])
@pytest.mark.parametrize("git_status, dirty", [("", False), (" M tracked.py\n", True)])
def test_report_github_sha_wins_and_records_dirty_flag(tmp_path, monkeypatch, sha, git_status, dirty):
monkeypatch.setenv("GITHUB_SHA", sha)
commands = []
def run(command, **kwargs):
commands.append(command)
return subprocess.CompletedProcess(command, 0, stdout=git_status)
monkeypatch.setattr(conditional_gate_metadata.subprocess, "run", run)
report = tmp_path / "report.json"
run_conditional_gate.write_report(report, "network-services", "passed")
evidence = json.loads(report.read_text())
assert evidence["schema_version"] == 1
assert evidence["git_sha"] == sha
assert evidence["git_dirty"] is dirty
assert commands == [[
"git", "-C", str(ROOT.resolve()), "status", "--porcelain", "--untracked-files=no",
]]
@pytest.mark.parametrize("github_sha", [None, ""])
@pytest.mark.parametrize("sha", ["b" * 40, "b" * 64])
def test_report_uses_script_repository_head(tmp_path, monkeypatch, github_sha, sha):
if github_sha is None:
monkeypatch.delenv("GITHUB_SHA", raising=False)
else:
monkeypatch.setenv("GITHUB_SHA", github_sha)
repo = tmp_path / "repository"
monkeypatch.setattr(
conditional_gate_metadata, "__file__", str(repo / "scripts/conditional_gate_metadata.py"),
)
monkeypatch.chdir(tmp_path)
commands = []
def run(command, **kwargs):
commands.append(command)
assert command[:3] == ["git", "-C", str(repo)]
assert kwargs["capture_output"] is True
assert kwargs["text"] is True
assert 0 < kwargs["timeout"] <= 5
assert not kwargs.get("shell", False)
output = sha + "\n" if command[3:] == ["rev-parse", "HEAD"] else ""
return subprocess.CompletedProcess(command, 0, stdout=output)
monkeypatch.setattr(conditional_gate_metadata.subprocess, "run", run)
report = tmp_path / "report.json"
run_conditional_gate.write_report(report, "network-services", "passed")
evidence = json.loads(report.read_text())
assert evidence["git_sha"] == sha
assert evidence["git_dirty"] is False
assert commands == [
["git", "-C", str(repo), "rev-parse", "HEAD"],
["git", "-C", str(repo), "status", "--porcelain", "--untracked-files=no"],
]
@pytest.mark.parametrize("source", ["environment", "git", "both"])
@pytest.mark.parametrize("sha", [
"not-a-sha", "A" * 40, "g" * 40, "a" * 39, "a" * 41, "a" * 63, "a" * 65,
])
def test_report_invalid_sha_falls_back_to_head_or_local(tmp_path, monkeypatch, source, sha):
monkeypatch.delenv("GITHUB_SHA", raising=False)
if source in {"environment", "both"}:
monkeypatch.setenv("GITHUB_SHA", sha)
commands = []
def run(command, **kwargs):
commands.append(command)
output = ""
if command[3:] == ["rev-parse", "HEAD"]:
output = "b" * 40 if source == "environment" else sha
return subprocess.CompletedProcess(command, 0, stdout=output)
monkeypatch.setattr(conditional_gate_metadata.subprocess, "run", run)
report = tmp_path / "report.json"
run_conditional_gate.write_report(report, "network-services", "passed")
expected_sha = "b" * 40 if source == "environment" else "local"
assert json.loads(report.read_text())["git_sha"] == expected_sha
assert commands == [
["git", "-C", str(ROOT.resolve()), "rev-parse", "HEAD"],
["git", "-C", str(ROOT.resolve()), "status", "--porcelain", "--untracked-files=no"],
]
@pytest.mark.parametrize("github_sha", [None, "a" * 40])
@pytest.mark.parametrize("result", [
subprocess.CompletedProcess(["git"], 128, stdout="b" * 40),
FileNotFoundError("git unavailable"),
subprocess.TimeoutExpired(["git"], 5),
UnicodeError("invalid Git output"),
], ids=["nonzero", "missing", "timeout", "invalid-encoding"])
def test_report_git_failure_has_unknown_dirty_flag(tmp_path, monkeypatch, github_sha, result):
monkeypatch.delenv("GITHUB_SHA", raising=False)
if github_sha is not None:
monkeypatch.setenv("GITHUB_SHA", github_sha)
def run(command, **kwargs):
if isinstance(result, Exception):
raise result
return result
monkeypatch.setattr(conditional_gate_metadata.subprocess, "run", run)
report = tmp_path / "report.json"
run_conditional_gate.write_report(report, "network-services", "passed")
evidence = json.loads(report.read_text())
assert evidence["git_sha"] == (github_sha or "local")
assert evidence["git_dirty"] is None
def test_git_metadata_sanitizes_subprocess_environment(monkeypatch):
overrides = {
"GIT_DIR", "GIT_WORK_TREE", "GIT_INDEX_FILE", "GIT_COMMON_DIR",
"GIT_OBJECT_DIRECTORY", "GIT_ALTERNATE_OBJECT_DIRECTORIES",
"GIT_NAMESPACE", "GIT_CEILING_DIRECTORIES", "GIT_DISCOVERY_ACROSS_FILESYSTEM",
"GIT_PREFIX", "GIT_IMPLICIT_WORK_TREE", "GIT_INTERNAL_SUPER_PREFIX",
"GIT_SHALLOW_FILE", "GIT_GRAFT_FILE", "GIT_REPLACE_REF_BASE",
"GIT_NO_REPLACE_OBJECTS", "GIT_QUARANTINE_PATH", "GIT_ATTR_SOURCE",
"GIT_CONFIG", "GIT_CONFIG_SYSTEM", "GIT_CONFIG_GLOBAL", "GIT_CONFIG_NOSYSTEM",
"GIT_CONFIG_PARAMETERS", "GIT_CONFIG_COUNT", "GIT_CONFIG_KEY_0", "GIT_CONFIG_VALUE_0",
"GIT_CONFIG_KEY_42", "GIT_CONFIG_VALUE_42",
"GIT_REDIRECT_STDIN", "GIT_REDIRECT_STDOUT", "GIT_REDIRECT_STDERR",
"GIT_TRACE", "GIT_TRACE_SETUP", "GIT_TRACE2", "GIT_TRACE2_EVENT", "GIT_TRACE2_PERF",
"GIT_SOME_FUTURE_CONTROL",
}
for name in overrides:
monkeypatch.setenv(name, "redirected")
ordinary = {"PATH": "/fixture/bin", "HOME": "/fixture/home", "LANG": "C.UTF-8"}
for name, value in ordinary.items():
monkeypatch.setenv(name, value)
monkeypatch.setenv("GIT_OPTIONAL_LOCKS", "1")
monkeypatch.setenv("GIT_TERMINAL_PROMPT", "1")
monkeypatch.delenv("GIT_NO_LAZY_FETCH", raising=False)
monkeypatch.delenv("GITHUB_SHA", raising=False)
commands = []
def run(command, **kwargs):
commands.append(command[3:])
environment = kwargs["env"]
assert overrides.isdisjoint(environment)
assert {name for name in environment if name.startswith("GIT_")} == {
"GIT_OPTIONAL_LOCKS", "GIT_TERMINAL_PROMPT", "GIT_NO_LAZY_FETCH",
}
assert environment["GIT_OPTIONAL_LOCKS"] == "0"
assert environment["GIT_TERMINAL_PROMPT"] == "0"
assert environment["GIT_NO_LAZY_FETCH"] == "1"
assert kwargs["stdin"] is subprocess.DEVNULL
assert {name: environment[name] for name in ordinary} == ordinary
output = "b" * 40 if command[3:] == ["rev-parse", "HEAD"] else ""
return subprocess.CompletedProcess(command, 0, stdout=output)
monkeypatch.setattr(conditional_gate_metadata.subprocess, "run", run)
assert conditional_gate_metadata.git_metadata() == {"git_sha": "b" * 40, "git_dirty": False}
assert commands == [["rev-parse", "HEAD"], ["status", "--porcelain", "--untracked-files=no"]]
assert all(os.environ[name] == "redirected" for name in overrides)
assert os.environ["GIT_OPTIONAL_LOCKS"] == "1"
assert os.environ["GIT_TERMINAL_PROMPT"] == "1"
assert "GIT_NO_LAZY_FETCH" not in os.environ
@pytest.fixture
def git_repository(tmp_path, monkeypatch):
git = shutil.which("git")
if git is None:
pytest.skip("git is unavailable")
# Start from the production sanitizer so the two cannot drift, then also drop
# any other inherited GIT_* control: fixture writes must never be redirected.
environment = {
name: value
for name, value in conditional_gate_metadata._git_environment().items()
if not name.startswith("GIT_") or name == "GIT_OPTIONAL_LOCKS"
}
environment.pop("GITHUB_SHA", None)
environment.update({
"GIT_CONFIG_NOSYSTEM": "1",
"HOME": str(tmp_path / "home"),
"XDG_CONFIG_HOME": str(tmp_path / "config"),
})
# The helper gets the same isolated settings; keep the fixture's subprocess
# environment separate so later monkeypatch.setenv calls cannot redirect it.
monkeypatch.setattr(os, "environ", environment.copy())
def create_repository(name):
repo = tmp_path / name
repo.mkdir()
def run_git(*arguments):
return subprocess.run(
[git, "-C", str(repo), *arguments],
env=environment,
check=True, capture_output=True, text=True, timeout=5,
).stdout.strip()
run_git("-c", "init.defaultBranch=main", "-c", "init.templateDir=", "init")
(repo / "tracked.txt").write_text("clean\n")
run_git("add", "tracked.txt")
run_git(
"-c", "user.name=Conditional gate test",
"-c", "user.email=conditional-gate@example.invalid",
"-c", "commit.gpgSign=false", "commit", "-m", f"Initial tracked file in {name}",
)
return repo, run_git("rev-parse", "HEAD")
return create_repository
def test_report_real_repository_ignores_untracked_files(tmp_path, monkeypatch, git_repository):
repo, sha = git_repository("repository")
tracked = repo / "tracked.txt"
monkeypatch.setattr(
conditional_gate_metadata, "__file__", str(repo / "scripts/conditional_gate_metadata.py"),
)
monkeypatch.chdir(tmp_path)
report = repo / "report.json"
def assert_dirty(expected):
run_conditional_gate.write_report(report, "network-services", "passed")
evidence = json.loads(report.read_text())
assert evidence["git_sha"] == sha
assert evidence["git_dirty"] is expected
assert_dirty(False)
(repo / "untracked.txt").write_text("untracked\n")
assert_dirty(False)
tracked.write_text("dirty\n")
assert_dirty(True)
tracked.write_text("clean\n")
assert_dirty(False)
@pytest.mark.parametrize("github_sha", [None, "not-a-sha"])
@pytest.mark.parametrize("checkout_dirty", [False, True])
def test_git_metadata_ignores_inherited_repository_redirects(
tmp_path, monkeypatch, git_repository, checkout_dirty, github_sha,
):
checkout, sha = git_repository("checkout")
redirected, redirected_sha = git_repository("redirected")
assert sha != redirected_sha
dirty_repo = checkout if checkout_dirty else redirected
(dirty_repo / "tracked.txt").write_text("dirty\n")
monkeypatch.setenv("GIT_DIR", str(redirected / ".git"))
monkeypatch.setenv("GIT_WORK_TREE", str(redirected))
monkeypatch.setenv("GIT_INDEX_FILE", str(redirected / ".git/index"))
if github_sha is not None:
# An invalid GITHUB_SHA must still fall back to the sanitized checkout HEAD.
monkeypatch.setenv("GITHUB_SHA", github_sha)
monkeypatch.setattr(
conditional_gate_metadata, "__file__", str(checkout / "scripts/conditional_gate_metadata.py"),
)
monkeypatch.chdir(tmp_path)
assert conditional_gate_metadata.git_metadata() == {"git_sha": sha, "git_dirty": checkout_dirty}
@pytest.mark.skipif(not os.path.exists("/dev/stdout"), reason="needs /dev/stdout")
@pytest.mark.parametrize(
"variable", ["GIT_TRACE", "GIT_TRACE_SETUP", "GIT_TRACE2", "GIT_TRACE2_EVENT", "GIT_TRACE2_PERF"],
)
def test_git_metadata_ignores_inherited_trace_output(tmp_path, monkeypatch, git_repository, variable):
# Traces aimed at stdout would otherwise corrupt the captured SHA and status.
checkout, sha = git_repository("checkout")
monkeypatch.setenv(variable, "/dev/stdout")
monkeypatch.delenv("GITHUB_SHA", raising=False)
monkeypatch.setattr(
conditional_gate_metadata, "__file__", str(checkout / "scripts/conditional_gate_metadata.py"),
)
monkeypatch.chdir(tmp_path)
assert conditional_gate_metadata.git_metadata() == {"git_sha": sha, "git_dirty": False}
def test_remote_prerequisites_are_actionable(monkeypatch):
names = {
"OBLITERATUS_REMOTE_HOST",
"OBLITERATUS_REMOTE_USER",
"OBLITERATUS_REMOTE_KEY",
"OBLITERATUS_REMOTE_KNOWN_HOSTS",
}
for name in names:
monkeypatch.delenv(name, raising=False)
assert set(run_conditional_gate.missing_prerequisites("remote-execution")) == names
for name in names:
monkeypatch.setenv(name, "fixture")
assert run_conditional_gate.missing_prerequisites("remote-execution") == []
@pytest.mark.parametrize("allow_missing", [False, True])
@pytest.mark.parametrize("artifact_kind", ["directory", "file", "dangling-symlink"])
def test_llama_gate_rejects_source_metadata_before_probes(
tmp_path, monkeypatch, capsys, allow_missing, artifact_kind,
):
checkout = tmp_path / "checkout"
checkout.mkdir()
artifact = checkout / "obliteratus.egg-info"
if artifact_kind == "directory":
artifact.mkdir()
elif artifact_kind == "file":
artifact.write_text("stale metadata", encoding="utf-8")
else:
artifact.symlink_to(checkout / "missing")
monkeypatch.setattr(
run_conditional_gate, "__file__", str(checkout / "scripts" / "run_conditional_gate.py"),
)
monkeypatch.chdir(tmp_path) # Guard the checkout even when cwd is elsewhere.
evidence = tmp_path / "evidence"
monkeypatch.setattr(run_conditional_gate.sys, "argv", [
"run_conditional_gate", "llama-tp-local-gpu", "--evidence-dir", str(evidence),
*(["--allow-missing"] if allow_missing else []),
])
monkeypatch.setattr(conditional_gate_metadata, "_git_output", lambda *args: None)
monkeypatch.setattr(
run_conditional_gate, "missing_prerequisites",
lambda _gate: pytest.fail("stale metadata must be detected before hardware probes"),
)
monkeypatch.setattr(
run_conditional_gate.subprocess, "run",
lambda *_args, **_kwargs: pytest.fail("stale metadata must not launch pytest"),
)
assert run_conditional_gate.main() == 2
report = json.loads((evidence / "llama-tp-local-gpu.json").read_text())
assert report["status"] == "not_run"
assert str(artifact) in report["reason"]
assert "Remove this build artifact after the non-editable install" in report["reason"]
assert "Stale build metadata" in capsys.readouterr().err
assert not (evidence / "llama-tp-local-gpu.xml").exists()
def test_llama_gate_clean_checkout_still_launches_qualification(tmp_path, monkeypatch):
monkeypatch.setattr(
run_conditional_gate, "__file__", str(tmp_path / "scripts" / "run_conditional_gate.py"),
)
monkeypatch.setattr(run_conditional_gate.sys, "argv", [
"run_conditional_gate", "llama-tp-local-gpu", "--evidence-dir", str(tmp_path),
])
monkeypatch.setattr(run_conditional_gate, "missing_prerequisites", lambda _gate: [])
monkeypatch.setattr(conditional_gate_metadata, "_git_output", lambda *args: None)
commands = []
def run(command, *, check):
commands.append(command)
(tmp_path / "llama-tp-local-gpu.xml").write_text(
'<testsuite tests="1" failures="0" errors="0" skipped="0"/>', encoding="utf-8",
)
return subprocess.CompletedProcess(command, 0)
monkeypatch.setattr(run_conditional_gate.subprocess, "run", run)
assert run_conditional_gate.main() == 0
assert commands[0][:4] == [
sys.executable, "-m", "pytest", run_conditional_gate.GATES["llama-tp-local-gpu"],
]
assert json.loads((tmp_path / "llama-tp-local-gpu.json").read_text())["status"] == "passed"
def test_summary_rejects_selected_failure_and_marks_unselected_stale():
policy = {
"gates": [
{"id": "first", "job": "shared"},
{"id": "second", "job": "shared"},
{"id": "optional", "job": "optional"},
]
}
rows, failures = conditional_gate_summary.summarize(
policy,
{"shared": "failure", "optional": "skipped"},
{"shared": True, "optional": False},
)
assert failures == ["shared: selected but result was failure"]
assert rows[0]["status"] == "failure"
assert rows[1]["status"] == "failure"
assert rows[2]["status"] == "not_selected_no_fresh_evidence"
def test_summary_records_active_waiver_and_selected_success_overrides_it():
policy = {
"gates": [{"id": "cuda-runtime", "job": "cuda"}],
"environment_waivers": [_waiver("cuda-runtime")],
}
rows, failures = conditional_gate_summary.summarize(
policy,
{"cuda": "skipped"},
{"cuda": False},
today=TODAY,
)
assert failures == []
assert rows == [{
"gate": "cuda-runtime",
"job": "cuda",
"selected": False,
"status": "waived_no_support_claim",
"waiver": {
"issue": "https://github.com/elder-plinius/OBLITERATUS/issues/110",
"expires": "2026-09-15",
"blocked_claim": "cuda-runtime support is not claimed.",
},
}]
rows, failures = conditional_gate_summary.summarize(
policy,
{"cuda": "success"},
{"cuda": True},
today=TODAY,
)
assert failures == []
assert rows[0]["status"] == "success"
assert "waiver" not in rows[0]
@pytest.mark.parametrize("github_sha", [None, "", "not-a-sha", "a" * 40, "a" * 64])
@pytest.mark.parametrize("git_status, dirty", [("", False), (" M tracked.py\n", True), (None, None)])
def test_summary_producer_records_git_metadata(tmp_path, monkeypatch, github_sha, git_status, dirty):
if github_sha is None:
monkeypatch.delenv("GITHUB_SHA", raising=False)
else:
monkeypatch.setenv("GITHUB_SHA", github_sha)
monkeypatch.delenv("GITHUB_STEP_SUMMARY", raising=False)
sha = "b" * 40
commands = []
def git_output(*arguments):
commands.append(arguments)
if arguments == ("rev-parse", "HEAD"):
return sha
assert arguments == ("status", "--porcelain", "--untracked-files=no")
return git_status
monkeypatch.setattr(conditional_gate_metadata, "_git_output", git_output)
policy = tmp_path / "policy.json"
policy.write_text(json.dumps({
"gates": [{"id": "network-services", "job": "network_services"}],
"maximum_evidence_age_days": 30,
}))
output = tmp_path / "evidence" / "summary.json"
monkeypatch.setattr(sys, "argv", [
"conditional_gate_summary", "--policy", str(policy), "--output", str(output),
"--results-json", '{"network_services": "success"}',
"--selected-json", '{"network_services": true}',
])
assert conditional_gate_summary.main() == 0
evidence = json.loads(output.read_text())
valid_environment = github_sha in {"a" * 40, "a" * 64}
assert evidence["schema_version"] == 1
assert evidence["git_sha"] == (github_sha if valid_environment else sha)
assert evidence["git_dirty"] is dirty
assert evidence["gates"][0]["status"] == "success"
assert evidence["failures"] == []
expected_commands = [] if valid_environment else [("rev-parse", "HEAD")]
assert commands == [
*expected_commands, ("status", "--porcelain", "--untracked-files=no"),
]
@pytest.mark.parametrize("script", ["run_conditional_gate.py", "conditional_gate_summary.py"])
def test_conditional_gate_scripts_support_direct_execution(tmp_path, script):
result = subprocess.run(
[sys.executable, "-E", str(ROOT / "scripts" / script), "--help"],
cwd=tmp_path, capture_output=True, text=True, timeout=5, check=False,
)
assert result.returncode == 0, result.stderr
assert "usage:" in result.stdout
def test_environment_waiver_validation_rejects_duplicate_expired_and_overlong_records():
policy = {
"gates": [
{"id": "cuda-runtime"},
{"id": "mps-runtime"},
{"id": "mlx-runtime"},
],
"environment_waivers": [
_waiver("cuda-runtime"),
_waiver("cuda-runtime"),
_waiver("mps-runtime", opened="2026-07-01", expires="2026-07-31"),
_waiver("mlx-runtime", expires="2026-09-16"),
],
}
active, errors = check_conditional_policy.validate_environment_waivers(
policy,
today=TODAY,
)
assert active == {"cuda-runtime": policy["environment_waivers"][0]}
assert "duplicate environment waiver gate: cuda-runtime" in errors
assert "environment waiver mps-runtime expired on 2026-07-31" in errors
assert "environment waiver mlx-runtime exceeds 30 days" in errors
def test_environment_waiver_validation_rejects_malformed_or_software_records():
policy = {
"gates": [
{"id": "network-services"},
{"id": "remote-execution"},
],
"environment_waivers": [
_waiver("network-services"),
_waiver("remote-execution", issue="not-an-issue", blocked_claim=""),
"not-an-object",
],
}
active, errors = check_conditional_policy.validate_environment_waivers(
policy,
today=TODAY,
)
assert active == {}
assert "software-only gate may not use an environment waiver: network-services" in errors
assert "environment waiver remote-execution issue must be a canonical issue URL" in errors
assert "environment waiver remote-execution blocked_claim must be non-empty" in errors
assert "environment waiver 2 must be an object" in errors
def test_environment_waiver_validation_rejects_missing_future_and_reversed_dates():
missing = _waiver("remote-execution")
missing.pop("reason")
policy = {
"gates": [
{"id": "remote-execution"},
{"id": "mps-runtime"},
{"id": "mlx-runtime"},
],
"environment_waivers": [
missing,
_waiver("mps-runtime", opened="2026-08-17", expires="2026-09-15"),
_waiver("mlx-runtime", opened="2026-08-16", expires="2026-08-15"),
],
}
active, errors = check_conditional_policy.validate_environment_waivers(
policy,
today=TODAY,
)
assert active == {}
assert "environment waiver 0 is missing fields: ['reason']" in errors
assert "environment waiver mps-runtime opens in the future: 2026-08-17" in errors
assert "environment waiver mlx-runtime expires before it opens" in errors
assert "environment waiver mlx-runtime expired on 2026-08-15" in errors
def test_summary_fails_an_expired_waiver():
policy = {
"gates": [{"id": "cuda-runtime", "job": "cuda"}],
"environment_waivers": [
_waiver("cuda-runtime", opened="2026-07-01", expires="2026-07-31"),
],
}
rows, failures = conditional_gate_summary.summarize(
policy,
{"cuda": "skipped"},
{"cuda": False},
today=TODAY,
)
assert rows[0]["status"] == "not_selected_no_fresh_evidence"
assert failures == [
"waiver policy: environment waiver cuda-runtime expired on 2026-07-31",
]
@pytest.mark.parametrize("gate, marker", [
("cuda-runtime", "gpu and not network and not download"),
("bitsandbytes-runtime", "gpu and not network and not download"),
("model-download-runtime", "network and download and not gpu"),
])
def test_recovery_runner_selects_matching_runtime_contracts(tmp_path, monkeypatch, gate, marker):
from types import SimpleNamespace
monkeypatch.setattr(
run_conditional_gate.sys, "argv",
["run_conditional_gate", gate, "--evidence-dir", str(tmp_path)],
)
monkeypatch.setattr(run_conditional_gate, "missing_prerequisites", lambda gate: [])
monkeypatch.setattr(conditional_gate_metadata, "_git_output", lambda *args: None)
commands = []
def run(command, *, check):
commands.append(command)
(tmp_path / f"{gate}.xml").write_text(
'<testsuite tests="2" failures="0" errors="0" skipped="0"/>'
)
return SimpleNamespace(returncode=0)
monkeypatch.setattr(run_conditional_gate.subprocess, "run", run)
assert run_conditional_gate.main() == 0
assert run_conditional_gate.GATES[gate] in commands[0]
assert "tests/conditional/test_recovery_runtime.py" in commands[0]
assert commands[0][-2:] == ["-m", marker]
assert ("tests/conditional/test_phase_admission_gpu.py" in commands[0]) == (gate == "cuda-runtime")
assert ("tests/conditional/test_operator_ui_cuda.py" in commands[0]) == (gate == "cuda-runtime")
assert ("tests/conditional/test_observation_cuda.py" in commands[0]) == (gate == "cuda-runtime")
def test_operator_ui_runner_collects_marked_lifecycle_tests_across_suite(tmp_path, monkeypatch):
from types import SimpleNamespace
monkeypatch.setattr(run_conditional_gate.sys, "argv", [
"run_conditional_gate", "operator-ui", "--evidence-dir", str(tmp_path),
])
monkeypatch.setattr(run_conditional_gate, "missing_prerequisites", lambda gate: [])
monkeypatch.setattr(conditional_gate_metadata, "_git_output", lambda *args: None)
commands = []
def run(command, *, check):
commands.append(command)
(tmp_path / "operator-ui.xml").write_text(
'<testsuite tests="2" failures="0" errors="0" skipped="0"/>'
)
return SimpleNamespace(returncode=0)
monkeypatch.setattr(run_conditional_gate.subprocess, "run", run)
assert run_conditional_gate.main() == 0
assert commands[0][3] == "tests"
assert commands[0][-2:] == ["-m", "operator_ui"]
assert "tests/conditional/test_operator_ui.py" not in commands[0]
source = (ROOT / "tests/test_app_model_lifecycle.py").read_text()
assert "@pytest.mark.operator_ui" in source
def _llama_tp_physical_hardware_digest(host: str, devices: tuple[str, str]) -> str:
return check_llama_tp_hardware_evidence._record_digest(
[
{
"rank": rank,
"identity_digest": check_llama_tp_hardware_evidence._record_digest(
{
"candidate_sha": "b" * 40,
"host_identity": host,
"device_identity": device,
}
),
}
for rank, device in enumerate(devices)
]
)
def _llama_tp_deployment_topology(
topology_digest: str, host_digest: str, device_digests: tuple[str, str],
) -> tuple[dict, str]:
ranks = [
{
"rank": rank, "group_rank": 0, "local_rank": rank,
"host_digest": host_digest, "device_digest": device_digest,
}
for rank, device_digest in enumerate(device_digests)
]
binding = TP2TopologyBinding(
schema_version=1, layout=TP2DeploymentLayout.LOCAL_TP2,
world_size=2, local_world_size=2, host_count=1,
rank_mapping_digest=contract_digest(((0, 0, 0), (1, 0, 1))),
host_mapping_digest=contract_digest(tuple((item["rank"], item["host_digest"]) for item in ranks)),
device_mapping_digest=contract_digest(tuple((item["rank"], item["device_digest"]) for item in ranks)),
topology_plan_digest=topology_digest,
)
return {
"schema_version": 1, "layout": "local_tp2", "world_size": 2,
"local_world_size": 2, "host_count": 1, "ranks": ranks,
"topology_plan_digest": topology_digest,
}, binding.digest
def _llama_tp_evidence(
*, verification_host: str = "host", verification_devices: tuple[str, str] = ("gpu0", "gpu1")
) -> dict:
digest = "a" * 64
devices = [
{
"rank": rank,
"device_digest": chr(ord("c") + rank) * 64,
"device_config_digest": "1" * 64,
"device_profile_digest": "2" * 64,
"name_digest": "e" * 64,
"compute_capability": "8.9",
"total_memory_bytes": 24_000_000_000,
}
for rank in range(2)
]
source_mount_attestation_digest = "3" * 64
verification_source_mount_attestation_digest = "8" * 64
hardware_devices = [
{
"rank": item["rank"],
"device_config_digest": item["device_config_digest"],
"device_profile_digest": item["device_profile_digest"],
}
for item in devices
]
hardware_digest = hashlib.sha256(
json.dumps(
{
"devices": hardware_devices,
"source_mount_attestation_digest": source_mount_attestation_digest,
},
ensure_ascii=True,
allow_nan=False,
sort_keys=True,
separators=(",", ":"),
).encode("ascii")
).hexdigest()
verification_hardware_digest = check_llama_tp_hardware_evidence._record_digest(
{
"devices": hardware_devices,
"source_mount_attestation_digest": verification_source_mount_attestation_digest,
}
)
acquisition_digest = hashlib.sha256(
json.dumps(
{
"kind": "project_generated_fixture_v1",
"source_digest": digest,
"config_digest": digest,
"fixture_digest": digest,
"reference_digest": digest,
},
ensure_ascii=True,
allow_nan=False,
sort_keys=True,
separators=(",", ":"),
).encode("ascii")
).hexdigest()
control_group_digest = check_llama_tp_hardware_evidence._record_digest(
{"backend": "gloo", "ranks": [0, 1], "world_size": 2}
)
data_group_digest = check_llama_tp_hardware_evidence._record_digest(
{
"backend": "nccl",
"device_type": "cuda",
"mesh_dim_names": ["tp"],
"ranks": [0, 1],
"world_size": 2,
"deployment_layout": "local_tp2",
}
)
topology_digest = check_llama_tp_hardware_evidence._record_digest(
{
"backend": "gloo",
"coordinator_rank": 0,
"placement_plan_digest": digest,
"world_size": 2,
}
)
deployment_topology, deployment_layout_digest = _llama_tp_deployment_topology(
topology_digest, "5" * 64, ("c" * 64, "d" * 64),
)
verification_deployment_topology, verification_deployment_layout_digest = (
_llama_tp_deployment_topology(topology_digest, "6" * 64, ("7" * 64, "8" * 64))
)
session_bindings = {
"api_digest": digest,
"artifact_manifest_digest": acquisition_digest,
"code_digest": digest,
"config_digest": digest,
"control_group_digest": control_group_digest,
"data_group_digest": data_group_digest,
"deployment_layout_digest": deployment_layout_digest,
"hardware_digest": hardware_digest,
"identity_digest": "4" * 64,
"image_digest": digest,
"lock_digest": digest,
"logical_manifest_digest": digest,
"model_config_digest": digest,
"model_digest": digest,
"package_digest": digest,
"plan_digest": digest,
"profile_digest": digest,
"runtime_digest": digest,
"source_digest": digest,
"tokenizer_digest": digest,
"topology_digest": topology_digest,
"torch_wheel_digest": digest,
}
verification_bindings = {
**session_bindings,
"config_digest": "6" * 64,
"hardware_digest": verification_hardware_digest,
"deployment_layout_digest": verification_deployment_layout_digest,
"identity_digest": "7" * 64,
}
verification_bindings_digest = check_llama_tp_hardware_evidence._record_digest(
verification_bindings
)
verification_attempt_digest = check_llama_tp_hardware_evidence._record_digest(
{
"fixture_digest": digest,
"source_digest": digest,
"verification_bindings_digest": verification_bindings_digest,
"verification_summary_digest": digest,
}
)
export_attempt_digest = check_llama_tp_hardware_evidence._record_digest(
{
"source_digest": digest,
"verification_bindings_digest": verification_bindings_digest,
"verification_summary_digest": digest,
"export_summary_digest": digest,
}
)
return {
"schema_version": 1,
"gate": "llama-tp-local-gpu",
"status": "passed",
"candidate_sha": "b" * 40,
"generated_at": "2026-09-04T03:00:00+00:00",
"environment": {
"image_digest": f"sha256:{digest}",
"lock_digest": digest,
"torch_wheel_digest": digest,
"package_digest": digest,
"runtime_digest": digest,
"api_digest": digest,
"hardware_digest": hardware_digest,
"python": "3.12.11",
"torch": "2.13.0+cu130",
"transformers": "5.15.0",
"accelerate": "1.14.0",
"safetensors": "0.8.0",
"cuda": "13.0",
"nccl": "2.27.5",
"driver": "580.82.07",
},
"topology": {
"world_size": 2,
"local_world_size": 2,
"tensor_parallel_size": 2,
"host_count": 1,
"deployment_layout": "local_tp2",
"control_backend": "gloo",
"data_backend": "nccl",
},
"devices": devices,
"artifacts": {
"source_digest": digest,
"distributed_config_digest": digest,
"model_config_digest": digest,
"tokenizer_digest": digest,
"model_digest": digest,
"plan_digest": digest,
"manifest_digest": digest,
"fixture_digest": digest,
"reference_digest": digest,
"acquisition_manifest_digest": acquisition_digest,
"code_digest": digest,
"profile_digest": digest,
"verification_attempt_digest": verification_attempt_digest,
"export_attempt_digest": export_attempt_digest,
},
"session": {
"bindings": session_bindings,
"bindings_digest": check_llama_tp_hardware_evidence._record_digest(session_bindings),
"load_summary_set_digest": "5" * 64,
"source_mount_attestation_digest": source_mount_attestation_digest,
"deployment_topology": deployment_topology,
"physical_hardware_digest": _llama_tp_physical_hardware_digest(
"host", ("gpu0", "gpu1")
),
"verification_source_mount_attestation_digest": (
verification_source_mount_attestation_digest
),
"verification_physical_hardware_digest": _llama_tp_physical_hardware_digest(
verification_host, verification_devices
),
"verification_bindings": verification_bindings,
"verification_deployment_topology": verification_deployment_topology,
"verification_bindings_digest": verification_bindings_digest,
},
"qualification": {
"rtol": 0.01,
"atol": 0.02,
"max_abs_error": 0.001,
"max_rel_error": 0.002,
"finite": True,
"within_tolerance": True,
"greedy_match": True,
"activation_sites": [
{
"site_digest": "f" * 64,
"complete": True,
"replicated": True,
"within_tolerance": True,
"max_abs_error": 0.001,
"max_rel_error": 0.002,
},
],
"probe_distill": {
"summary_digest": digest,
"config_digest": ProbeDistillConfig(
n_directions=1,
selected_layer_count=1,
max_samples_per_class=2,
max_tokens=8,
batch_size=2,
max_activation_bytes=1024 * 1024,
activation_atol=0.02,
activation_rtol=0.01,
stage_timeout_seconds=120,
add_special_tokens=False,
).digest,
"tokenization_digest": digest,
"selected_site_digests": ["f" * 64],
"direction_method": "difference_of_means",
"direction_count": 1,
"harmful_count": 2,
"harmless_count": 2,
"finite": True,
"within_tolerance": True,
"rank_agreement": True,
"parameters_unchanged": True,
"max_mean_abs_error": 0.001,
"max_direction_abs_error": 0.002,
},
"excise": {
"summary_digest": digest,
"config_digest": ExciseConfig(
norm_preserve=True,
regularization=0.2,
projection_row_fraction=0.5,
max_norm_ratio=1.1,
max_journal_bytes=16 * 1024 * 1024,
max_update_bytes=4 * 1024 * 1024,
safety_margin_bytes=1024 * 1024,
stage_timeout_seconds=120,
).digest,
"operation_count": 7,
"finite": True,
"complete_tensor_match": True,
"postconditions_match": True,
"journal_private": True,
"rollback_restored": True,
"verified": False,
"journal_removed": True,
"rank_agreement": True,
"max_abs_error": 0.001,
"max_rel_error": 0.002,
},
"verification": {
"summary_digest": digest,
"baseline_digest": digest,
"config_digest": VerificationConfig(
max_samples=3,
max_tokens=8,
batch_size=2,
max_baseline_bytes=4 * 1024 * 1024,
safety_margin_bytes=1024 * 1024,
rank_logit_atol=0.02,
rank_logit_rtol=0.01,
max_refusal_rate=1.0,
min_coherence_retention=0.0,
max_forward_kl=100.0,
min_capability_retention=0.0,
stage_timeout_seconds=120,
add_special_tokens=False,
).digest,
"rank_evidence_set_digest": digest,
"quality_accepted": True,
"rank_agreement": True,
"refusal_rate": 0.0,
"coherence_retention": 1.0,
"forward_kl": 0.01,
"capability_retention": 0.99,
"nonfinite_count": 0,
"journal_removed": True,
"fresh_attempt_identity": True,
"max_excise_abs_error": 0.001,
"max_excise_rel_error": 0.002,
},
"export": {
"summary_digest": digest,
"config_digest": ExportConfig(
export_id="5" * 32,
output_name="canonical-output",
max_output_bytes=128 * 1024 * 1024,
max_staging_bytes=256 * 1024 * 1024,
max_host_bytes_per_rank=1024 * 1024 * 1024,
max_device_bytes_per_rank=128 * 1024 * 1024,
semantic_reload_host_bytes_per_rank=256 * 1024 * 1024,
semantic_reload_device_bytes_per_rank=1024 * 1024,
max_tensor_bytes=64 * 1024 * 1024,
max_metadata_bytes=16 * 1024 * 1024,
max_metadata_files=100,
safety_margin_bytes=1024 * 1024,
stage_timeout_seconds=120,
).digest,
"verification_digest": digest,
"assignment_set_digest": digest,
"tensor_record_set_digest": digest,
"rank_evidence_set_digest": digest,
"reload_evidence_set_digest": digest,
"hf_index_digest": digest,
"tensor_count": 7,
"logical_bytes": 1024,
"published": True,
"rank_agreement": True,
"semantic_reload": True,
"fresh_attempt_identity": True,
},
},
}
def test_llama_tp_hardware_evidence_accepts_only_the_exact_closed_envelope():
evidence = _llama_tp_evidence()
assert evidence["session"]["source_mount_attestation_digest"] != evidence["session"][
"verification_source_mount_attestation_digest"
]
assert evidence["session"]["bindings"]["hardware_digest"] != evidence["session"][
"verification_bindings"
]["hardware_digest"]
assert (
check_llama_tp_hardware_evidence.validate(
evidence,
candidate_sha="b" * 40,
lock_digest="a" * 64,
image_digest="sha256:" + "a" * 64,
torch_wheel_digest="a" * 64,
package_digest="a" * 64,
runtime_digest="a" * 64,
api_digest="a" * 64,
code_digest="a" * 64,
profile_digest="a" * 64,
)
== []
)
def test_llama_tp_evidence_accepts_streamed_verify_baseline_identities():
from dataclasses import asdict
import torch
from obliteratus.distributed.verification import _snapshot_digest, _state_roots_digest
from obliteratus.distributed.verification_contracts import (
VerificationBaselineSummary, prepare_verification_corpus,
)
from tests.conditional.test_distributed_llama_tp_cuda import (
_verification_config, _verification_samples,
)
from tests.test_distributed_verification_contracts import _Tokenizer
config = _verification_config()
corpus = prepare_verification_corpus(
_Tokenizer(), _verification_samples(), refusal_token_ids=(2,),
source_tokenizer_digest="a" * 64, config=config,
)
row = torch.zeros(1, corpus.vocab_size)
next_rows = torch.zeros(3, corpus.vocab_size)
baseline = VerificationBaselineSummary(
schema_version=1, run_id="1" * 32, distilled_digest="a" * 64,
manifest_digest="a" * 64, expected_excise_config_digest="a" * 64,
verification_config_digest=config.digest, corpus_digest=corpus.corpus_digest,
sample_ids_digest=corpus.sample_ids_digest,
tokenizer_contract_digest=corpus.tokenizer_contract_digest,
tokenization_digest=corpus.tokenization_digest,
pristine_state_digest=_state_roots_digest({f"parameter-{i}": "a" * 64 for i in range(1024)}),
baseline_logit_digest=_snapshot_digest((row,) * 3, next_rows),
sample_count=3, sequence_position_count=3, vocab_size=corpus.vocab_size,
baseline_bytes=6 * corpus.vocab_size * 4,
rank_max_abs_error_hex=(0.0).hex(), rank_max_rel_error_hex=(0.0).hex(),
)
# The qualifier calls production VERIFY; the checker hashes only the closed
# outer summaries, so streamed inner roots remain opaque 64-character ids.
assert baseline.digest == check_llama_tp_hardware_evidence._record_digest(asdict(baseline))
evidence = _llama_tp_evidence()
evidence["qualification"]["verification"]["baseline_digest"] = baseline.digest
assert evidence["qualification"]["verification"]["config_digest"] == config.digest
assert check_llama_tp_hardware_evidence.validate(
evidence, candidate_sha="b" * 40, lock_digest="a" * 64,
image_digest="sha256:" + "a" * 64, torch_wheel_digest="a" * 64,
package_digest="a" * 64, runtime_digest="a" * 64, api_digest="a" * 64,
code_digest="a" * 64, profile_digest="a" * 64,
) == []
@pytest.mark.parametrize(
("host", "devices"),
[("different-host", ("gpu0", "gpu1")), ("host", ("gpu0", "different-gpu"))],
)
def test_llama_tp_hardware_evidence_rejects_different_physical_host_or_gpu(host, devices):
evidence = _llama_tp_evidence(verification_host=host, verification_devices=devices)
errors = check_llama_tp_hardware_evidence.validate(
evidence,
candidate_sha="b" * 40,
lock_digest="a" * 64,
image_digest="sha256:" + "a" * 64,
torch_wheel_digest="a" * 64,
package_digest="a" * 64,
runtime_digest="a" * 64,
api_digest="a" * 64,
code_digest="a" * 64,
profile_digest="a" * 64,
)
assert errors == ["verification attempt does not use the same physical host and GPUs"]
def _validate_llama_tp_evidence(evidence):
return check_llama_tp_hardware_evidence.validate(
evidence, candidate_sha="b" * 40, lock_digest="a" * 64,
image_digest="sha256:" + "a" * 64, torch_wheel_digest="a" * 64,
package_digest="a" * 64, runtime_digest="a" * 64, api_digest="a" * 64,
code_digest="a" * 64, profile_digest="a" * 64,
)
@pytest.mark.parametrize("version", [True, False, 1.0, "1", None, 0, 2])
def test_llama_tp_evidence_requires_an_integer_schema_version(version):
evidence = _llama_tp_evidence()
evidence["schema_version"] = version
assert _validate_llama_tp_evidence(evidence) == [
"hardware evidence schema_version must be 1"
]
@pytest.mark.parametrize("count", [0, MAX_PROBE_LAYERS, MAX_PROBE_LAYERS + 1])
def test_llama_tp_evidence_bounds_activation_record_sets(count):
evidence = _llama_tp_evidence()
site = evidence["qualification"]["activation_sites"][0]
evidence["qualification"]["activation_sites"] = [
{**site, "site_digest": f"{index:064x}"} for index in range(count)
]
errors = _validate_llama_tp_evidence(evidence)
if count == MAX_PROBE_LAYERS:
assert errors == []
else:
assert errors == [
f"qualification requires 1 through {MAX_PROBE_LAYERS} activation-site results"
]
@pytest.mark.parametrize(
("path", "expected"),
[
(("devices",), "qualification requires exactly two device records"),
(
("qualification", "probe_distill", "selected_site_digests"),
"probe selected-site evidence must contain exactly one digest",
),
],
)
def test_llama_tp_evidence_rejects_expanded_fixed_record_sets(path, expected):
evidence = _llama_tp_evidence()
target = evidence
for field in path[:-1]:
target = target[field]
records = target[path[-1]]
records.append(records[0])
assert _validate_llama_tp_evidence(evidence) == [expected]
@pytest.mark.parametrize("attempt", ["", "verification_"])
@pytest.mark.parametrize(
("path", "value", "message"),
[
(("layout",), "two_host_tp2", "requires the local_tp2 deployment layout"),
(("local_world_size",), 1, "requires the local TP2 geometry"),
(("schema_version",), True, "schema_version is outside its integer boundary"),
(("topology_plan_digest",), "f" * 64, "disagrees with the placement topology"),
(("ranks",), [], "requires exactly two rank identities"),
(("ranks", 1, "local_rank"), 0, "has an invalid local TP2 rank mapping"),
(("ranks", 1, "host_digest"), "f" * 64, "requires one physical host identity"),
(("ranks", 0, "prompt"), "unretained", "rank 0 does not match its closed schema"),
],
)
def test_llama_tp_evidence_rejects_invalid_per_attempt_deployment(attempt, path, value, message):
evidence = _llama_tp_evidence()
target = evidence["session"][attempt + "deployment_topology"]
for field in path[:-1]:
target = target[field]
target[path[-1]] = value
name = "verification deployment topology" if attempt else "session deployment topology"
assert _validate_llama_tp_evidence(evidence) == [f"{name} {message}"]
@pytest.mark.parametrize("attempt", ["", "verification_"])
def test_llama_tp_evidence_rejects_opaque_deployment_binding(attempt):
evidence = _llama_tp_evidence()
bindings = evidence["session"][attempt + "bindings"]
bindings["deployment_layout_digest"] = "f" * 64
evidence["session"][attempt + "bindings_digest"] = contract_digest(bindings)
name = "verification deployment topology" if attempt else "session deployment topology"
assert _validate_llama_tp_evidence(evidence) == [f"{name} does not match its deployment binding"]
def test_llama_tp_evidence_rejects_device_drift_and_reused_deployment_identity():
evidence = _llama_tp_evidence()
evidence["session"]["deployment_topology"]["ranks"][0]["device_digest"] = "f" * 64
assert _validate_llama_tp_evidence(evidence) == [
"session deployment topology device identity disagrees with hardware evidence"
]
evidence = _llama_tp_evidence()
evidence["session"]["verification_deployment_topology"] = evidence["session"]["deployment_topology"]
bindings = evidence["session"]["verification_bindings"]
bindings["deployment_layout_digest"] = evidence["session"]["bindings"]["deployment_layout_digest"]
evidence["session"]["verification_bindings_digest"] = contract_digest(bindings)
assert _validate_llama_tp_evidence(evidence) == [
"verification session did not bind a fresh deployment identity"
]
def test_llama_tp_evidence_binds_verify_and_export_to_per_attempt_mount_and_topology():
evidence = _llama_tp_evidence()
session = evidence["session"]
topology, layout_digest = _llama_tp_deployment_topology(
session["bindings"]["topology_digest"], "f" * 64, ("7" * 64, "8" * 64),
)
session["verification_deployment_topology"] = topology
session["verification_bindings"]["deployment_layout_digest"] = layout_digest
session["verification_source_mount_attestation_digest"] = "9" * 64
session["verification_bindings"]["hardware_digest"] = contract_digest({
"devices": [
{field: device[field] for field in ("rank", "device_config_digest", "device_profile_digest")}
for device in evidence["devices"]
],
"source_mount_attestation_digest": session["verification_source_mount_attestation_digest"],
})
session["verification_bindings_digest"] = contract_digest(session["verification_bindings"])
assert _validate_llama_tp_evidence(evidence) == [
"verification attempt digest does not bind the retained evidence"
]
evidence["artifacts"]["verification_attempt_digest"] = contract_digest({
"fixture_digest": evidence["artifacts"]["fixture_digest"],
"source_digest": evidence["artifacts"]["source_digest"],
"verification_bindings_digest": session["verification_bindings_digest"],
"verification_summary_digest": evidence["qualification"]["verification"]["summary_digest"],
})
assert _validate_llama_tp_evidence(evidence) == [
"export attempt digest does not bind the retained evidence"
]
evidence["artifacts"]["export_attempt_digest"] = contract_digest({
"source_digest": evidence["artifacts"]["source_digest"],
"verification_bindings_digest": session["verification_bindings_digest"],
"verification_summary_digest": evidence["qualification"]["verification"]["summary_digest"],
"export_summary_digest": evidence["qualification"]["export"]["summary_digest"],
})
assert _validate_llama_tp_evidence(evidence) == []
def test_llama_tp_hardware_evidence_rejects_drift_and_payload_expansion():
evidence = _llama_tp_evidence()
evidence["candidate_sha"] = "c" * 40
errors = check_llama_tp_hardware_evidence.validate(
evidence,
candidate_sha="b" * 40,
lock_digest="a" * 64,
image_digest="sha256:" + "a" * 64,
torch_wheel_digest="a" * 64,
package_digest="a" * 64,
runtime_digest="a" * 64,
api_digest="a" * 64,
code_digest="a" * 64,
profile_digest="a" * 64,
)
assert errors == ["hardware evidence candidate does not match the exact requested head"]
evidence = _llama_tp_evidence()
evidence["qualification"]["prompt"] = "must not be retained"
errors = check_llama_tp_hardware_evidence.validate(
evidence,
candidate_sha="b" * 40,
lock_digest="a" * 64,
image_digest="sha256:" + "a" * 64,
torch_wheel_digest="a" * 64,
package_digest="a" * 64,
runtime_digest="a" * 64,
api_digest="a" * 64,
code_digest="a" * 64,
profile_digest="a" * 64,
)
assert errors == ["qualification does not match its closed schema"]
evidence = _llama_tp_evidence()
evidence["topology"]["host_count"] = 2
errors = check_llama_tp_hardware_evidence.validate(
evidence,
candidate_sha="b" * 40,
lock_digest="a" * 64,
image_digest="sha256:" + "a" * 64,
torch_wheel_digest="a" * 64,
package_digest="a" * 64,
runtime_digest="a" * 64,
api_digest="a" * 64,
code_digest="a" * 64,
profile_digest="a" * 64,
)
assert errors == ["upstream #244 local qualification is exactly one physical host"]
evidence = _llama_tp_evidence()
evidence["topology"]["deployment_layout"] = "two_host_tp2"
errors = check_llama_tp_hardware_evidence.validate(
evidence,
candidate_sha="b" * 40,
lock_digest="a" * 64,
image_digest="sha256:" + "a" * 64,
torch_wheel_digest="a" * 64,
package_digest="a" * 64,
runtime_digest="a" * 64,
api_digest="a" * 64,
code_digest="a" * 64,
profile_digest="a" * 64,
)
assert errors == ["upstream #244 local qualification requires the local_tp2 deployment layout"]
evidence = _llama_tp_evidence()
evidence["session"]["bindings"]["model_digest"] = "6" * 64
evidence["session"]["bindings_digest"] = check_llama_tp_hardware_evidence._record_digest(
evidence["session"]["bindings"]
)
errors = check_llama_tp_hardware_evidence.validate(
evidence,
candidate_sha="b" * 40,
lock_digest="a" * 64,
image_digest="sha256:" + "a" * 64,
torch_wheel_digest="a" * 64,
package_digest="a" * 64,
runtime_digest="a" * 64,
api_digest="a" * 64,
code_digest="a" * 64,
profile_digest="a" * 64,
)
assert errors == ["session binding model_digest disagrees with hardware evidence"]
evidence = _llama_tp_evidence()
evidence["artifacts"]["reference_digest"] = "7" * 64
errors = check_llama_tp_hardware_evidence.validate(
evidence,
candidate_sha="b" * 40,
lock_digest="a" * 64,
image_digest="sha256:" + "a" * 64,
torch_wheel_digest="a" * 64,
package_digest="a" * 64,
runtime_digest="a" * 64,
api_digest="a" * 64,
code_digest="a" * 64,
profile_digest="a" * 64,
)
assert errors == ["acquisition manifest digest does not bind the generated fixture"]
evidence = _llama_tp_evidence()
evidence["qualification"]["excise"]["rollback_restored"] = False
errors = check_llama_tp_hardware_evidence.validate(
evidence,
candidate_sha="b" * 40,
lock_digest="a" * 64,
image_digest="sha256:" + "a" * 64,
torch_wheel_digest="a" * 64,
package_digest="a" * 64,
runtime_digest="a" * 64,
api_digest="a" * 64,
code_digest="a" * 64,
profile_digest="a" * 64,
)
assert errors == ["excise rollback_restored must be true"]
@pytest.mark.parametrize(
("path", "value", "expected"),
[
(
("qualification", "verification", "nonfinite_count"),
1,
"verification nonfinite_count must equal zero",
),
(
("qualification", "verification", "fresh_attempt_identity"),
False,
"verification fresh_attempt_identity must be true",
),
(
("qualification", "verification", "config_digest"),
"9" * 64,
"verification config digest differs from preregistered profile",
),
(
("session", "verification_bindings_digest"),
None,
"verification session must use a fresh binding identity",
),
(
("session", "verification_source_mount_attestation_digest"),
"9" * 64,
"verification hardware digest does not bind its device and mount evidence",
),
],
)
def test_llama_tp_hardware_evidence_rejects_invalid_verification(path, value, expected):
evidence = _llama_tp_evidence()
if value is None:
evidence["session"]["verification_bindings"] = dict(evidence["session"]["bindings"])
value = evidence["session"]["bindings_digest"]
target = evidence
for name in path[:-1]:
target = target[name]
target[path[-1]] = value
errors = check_llama_tp_hardware_evidence.validate(
evidence,
candidate_sha="b" * 40,
lock_digest="a" * 64,
image_digest="sha256:" + "a" * 64,
torch_wheel_digest="a" * 64,
package_digest="a" * 64,
runtime_digest="a" * 64,
api_digest="a" * 64,
code_digest="a" * 64,
profile_digest="a" * 64,
)
assert errors == [expected]
@pytest.mark.parametrize(
("path", "value", "expected"),
[
(
("qualification", "export", "published"),
False,
"export published must be true",
),
(
("qualification", "export", "verification_digest"),
"9" * 64,
"export does not bind the accepted verification result",
),
(
("qualification", "export", "config_digest"),
"9" * 64,
"export config digest differs from the preregistered profile",
),
],
)
def test_llama_tp_hardware_evidence_rejects_invalid_export(path, value, expected):
evidence = _llama_tp_evidence()
target = evidence
for name in path[:-1]:
target = target[name]
target[path[-1]] = value
errors = check_llama_tp_hardware_evidence.validate(
evidence,
candidate_sha="b" * 40,
lock_digest="a" * 64,
image_digest="sha256:" + "a" * 64,
torch_wheel_digest="a" * 64,
package_digest="a" * 64,
runtime_digest="a" * 64,
api_digest="a" * 64,
code_digest="a" * 64,
profile_digest="a" * 64,
)
assert errors == [expected]
def test_readonly_mount_namespace_applies_closed_flags_before_exec(monkeypatch, tmp_path):
source = tmp_path / f"source-{'a' * 64}"
source.mkdir()
operations: list[tuple[str | None, str, int]] = []
class ExecCalled(BaseException):
pass
monkeypatch.setattr(
run_readonly_mount_namespace,
"_mount",
lambda source, target, flags: operations.append((source, target, flags)),
)
monkeypatch.setattr(
run_readonly_mount_namespace.os,
"execve",
lambda *_args: (_ for _ in ()).throw(ExecCalled()),
)
monkeypatch.setattr(
sys,
"argv",
[
"run_readonly_mount_namespace.py",
"--source",
str(source),
"--",
"/bin/true",
],
)
try:
run_readonly_mount_namespace.main()
except ExecCalled:
pass
else:
raise AssertionError("the wrapper did not exec the bounded child")
assert operations == [
(None, "/", (1 << 18) | 16384),
(str(source), str(source), 4096),
(None, str(source), 4096 | 32 | 1 | 2 | 4 | 8),
]
def test_readonly_mount_namespace_rejects_non_content_addressed_source(monkeypatch, tmp_path):
source = tmp_path / "source"
source.mkdir()
monkeypatch.setattr(
sys,
"argv",
[
"run_readonly_mount_namespace.py",
"--source",
str(source),
"--",
"/bin/true",
],
)
monkeypatch.setattr(
run_readonly_mount_namespace,
"_mount",
lambda *_args: (_ for _ in ()).throw(AssertionError("mount must not run")),
)
assert run_readonly_mount_namespace.main() == 70
@pytest.mark.parametrize(
("address", "expected"),
[
("10.1.2.3", True),
("172.31.255.254", True),
("192.168.4.5", True),
("127.0.0.1", False),
("169.254.1.2", False),
("203.0.113.5", False),
("not-an-address", False),
(None, False),
],
)
def test_llama_tp_private_interface_check_is_total(address, expected):
assert run_conditional_gate._is_private_rfc1918_ipv4(address) is expected