"""Public command and archive identity validation fails before side effects.""" import sys from types import SimpleNamespace from unittest.mock import Mock import pytest from obliteratus import cli from obliteratus.checkpoint_provenance import ArtifactIdentity, ToolIdentity, build_provenance from obliteratus.run_archive import RunArchive pytestmark = pytest.mark.cpu @pytest.mark.parametrize("argv", [ ["preflight", "distributed", "private-config"], ["run", "private-config", "distributed", "preflight"], ]) def test_misplaced_distributed_preflight_command_never_dispatches(monkeypatch, capsys, argv): dispatch = Mock() monkeypatch.setattr(cli, "_cmd_distributed", dispatch) monkeypatch.setattr(cli, "_cmd_run", dispatch) with pytest.raises(SystemExit) as caught: cli.main(argv) assert caught.value.code == 2 captured = capsys.readouterr() assert captured.out == "" assert "command shape is invalid" in captured.err dispatch.assert_not_called() def test_archive_refuses_mismatched_provenance_attribute_without_mutation(tmp_path): archive = RunArchive(tmp_path) run_id = archive.begin(["org/model"]) directory = archive._run_dir(run_id) before = {path.name: path.read_bytes() for path in directory.iterdir() if path.is_file()} record = build_provenance( sources=(ArtifactIdentity("generated", "boundary-test", "v1", "sha256:" + "a" * 64),), converter=ToolIdentity("test", "1", "c" * 40), obliteratus_commit="c" * 40, configuration_digest=None, tokenizer=None, base_model=None, command=("checkpoint", "attach"), environment={"python": "test", "platform": "cpu", "packages": {}}, source_topology={}, lineage=(), input_digests=("sha256:" + "a" * 64,), output_digests=("sha256:" + "b" * 64,), transformations=(), observed_scopes=("model_weights",), lost_state=(), ) inconsistent = SimpleNamespace(to_json=record.to_json, artifact_id="substituted-identity") with pytest.raises(ValueError, match="checkpoint provenance contract is invalid"): archive.attach_checkpoint_provenance(run_id, inconsistent) assert {path.name: path.read_bytes() for path in directory.iterdir() if path.is_file()} == before @pytest.mark.parametrize("tail", [ ["list"], ["launch", "--", "org/model", "--prompt-pairs-file", "{loop}/pairs.json"], ]) def test_runs_symlink_loop_paths_fail_with_one_line_and_no_traceback( tmp_path, capsys, monkeypatch, tail, ): """Symlink loops in the archive root or a dataset input are archive errors (#668).""" spawn = Mock(side_effect=AssertionError("a worker must not be spawned")) monkeypatch.setattr("obliteratus.run_archive.subprocess.Popen", spawn) loop = tmp_path / "loop-a" loop.symlink_to(tmp_path / "loop-b") (tmp_path / "loop-b").symlink_to(loop) archive_root = loop / "archive" if tail == ["list"] else tmp_path / "archive" argv = [part.replace("{loop}", str(loop)) for part in tail] with pytest.raises(SystemExit) as caught: cli.main(["runs", "--archive-root", str(archive_root), *argv]) captured = capsys.readouterr() assert caught.value.code == 1 assert captured.out == "" assert captured.err.startswith(f"obliteratus runs {tail[0]}: ") assert captured.err.count("\n") == 1 if tail[0] == "launch": assert list(archive_root.iterdir()) == [] _UNKNOWN_HOME = "~obliteratus-no-such-user-668" _UNKNOWN_USER_EXPANDS = pytest.mark.skipif( sys.platform == "win32", reason="Windows expanduser does not validate users: ~nosuchuser expands to a " "sibling of the current home directory (C:\\Users\\nosuchuser) instead of raising", ) @_UNKNOWN_USER_EXPANDS @pytest.mark.parametrize("tail", [ ["list"], ["launch", "--", "org/model", "--prompt-pairs-file", f"{_UNKNOWN_HOME}/pairs.json"], ]) def test_runs_unknown_home_directory_fails_with_one_line(tmp_path, capsys, monkeypatch, tail): """An unknown ``~user`` in the archive root or a dataset input is an archive error.""" spawn = Mock(side_effect=AssertionError("a worker must not be spawned")) monkeypatch.setattr("obliteratus.run_archive.subprocess.Popen", spawn) archive_root = f"{_UNKNOWN_HOME}/archive" if tail == ["list"] else str(tmp_path / "archive") with pytest.raises(SystemExit) as caught: cli.main(["runs", "--archive-root", archive_root, *tail]) captured = capsys.readouterr() assert caught.value.code == 1 assert captured.out == "" assert captured.err.startswith(f"obliteratus runs {tail[0]}: ") assert captured.err.count("\n") == 1 if tail[0] == "launch": assert list((tmp_path / "archive").iterdir()) == [] spawn.assert_not_called() @_UNKNOWN_USER_EXPANDS def test_runs_unknown_home_in_environment_root_fails_with_one_line(capsys, monkeypatch): """``OBLITERATUS_RUN_ARCHIVE='~nosuchuser/x'`` is an archive error, not a traceback (#675).""" spawn = Mock(side_effect=AssertionError("a worker must not be spawned")) monkeypatch.setattr("obliteratus.run_archive.subprocess.Popen", spawn) monkeypatch.setenv("OBLITERATUS_RUN_ARCHIVE", f"{_UNKNOWN_HOME}/x") with pytest.raises(SystemExit) as caught: cli.main(["runs", "list"]) captured = capsys.readouterr() assert caught.value.code == 1 assert captured.out == "" assert captured.err.startswith("obliteratus runs list: cannot inspect run archive root: ") assert captured.err.count("\n") == 1 spawn.assert_not_called()