mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-05 10:34:23 +01:00
Bound git file reads, validate symlinks and ref names, isolate the git environment, never fail a run on provenance errors, and preserve the resolved source commit through local snapshots and dequantization. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
402 lines
16 KiB
Python
402 lines
16 KiB
Python
"""Build provenance in checkpoint metadata: tool version, git commit/dirty, source revision."""
|
|
|
|
import os
|
|
from types import SimpleNamespace
|
|
|
|
import pytest
|
|
|
|
from obliteratus import __version__, provenance
|
|
from obliteratus.abliterate import AbliterationPipeline
|
|
|
|
OID = "0123456789abcdef0123456789abcdef01234567"
|
|
|
|
|
|
def _repo(tmp_path, head, refs=None, packed=None):
|
|
git = tmp_path / ".git"
|
|
git.mkdir()
|
|
(git / "HEAD").write_text(head + "\n", encoding="utf-8")
|
|
for ref, oid in (refs or {}).items():
|
|
path = git / ref
|
|
path.parent.mkdir(parents=True, exist_ok=True)
|
|
path.write_text(oid + "\n", encoding="utf-8")
|
|
if packed is not None:
|
|
(git / "packed-refs").write_text(packed, encoding="utf-8")
|
|
return tmp_path
|
|
|
|
|
|
def _no_git_status(monkeypatch, stdout=b"", returncode=0):
|
|
calls = []
|
|
|
|
def run(argv, **kwargs):
|
|
calls.append((argv, kwargs))
|
|
return SimpleNamespace(returncode=returncode, stdout=stdout)
|
|
|
|
monkeypatch.setattr(provenance.subprocess, "run", run)
|
|
return calls
|
|
|
|
|
|
def test_commit_from_loose_branch_ref(tmp_path, monkeypatch):
|
|
root = _repo(tmp_path, "ref: refs/heads/main", refs={"refs/heads/main": OID})
|
|
calls = _no_git_status(monkeypatch)
|
|
got = provenance.tool_provenance(root)
|
|
assert got == {"obliteratus_version": __version__, "obliteratus_git_commit": OID,
|
|
"obliteratus_git_dirty": False}
|
|
argv, kwargs = calls[0]
|
|
assert argv == ["git", "--no-optional-locks", "-c", "core.fsmonitor=false",
|
|
"status", "--porcelain", "--untracked-files=no"]
|
|
assert kwargs["cwd"] == root and kwargs["timeout"] == 5 and "shell" not in kwargs
|
|
|
|
|
|
def test_commit_from_packed_refs_and_detached_head(tmp_path, monkeypatch):
|
|
_no_git_status(monkeypatch)
|
|
packed_root, detached_root = tmp_path / "packed", tmp_path / "detached"
|
|
packed_root.mkdir()
|
|
detached_root.mkdir()
|
|
packed = f"# pack-refs with: peeled\n{OID} refs/heads/feature\n"
|
|
_repo(packed_root, "ref: refs/heads/feature", packed=packed)
|
|
_repo(detached_root, OID)
|
|
assert provenance.tool_provenance(packed_root)["obliteratus_git_commit"] == OID
|
|
assert provenance.tool_provenance(detached_root)["obliteratus_git_commit"] == OID
|
|
|
|
|
|
def test_dirty_tree_is_reported(tmp_path, monkeypatch):
|
|
root = _repo(tmp_path, OID)
|
|
_no_git_status(monkeypatch, stdout=" M obliteratus/abliterate.py\n")
|
|
assert provenance.tool_provenance(root)["obliteratus_git_dirty"] is True
|
|
|
|
|
|
@pytest.mark.parametrize("failure", ["oserror", "timeout", "nonzero"])
|
|
def test_dirty_is_unknown_when_git_cannot_answer(tmp_path, monkeypatch, failure):
|
|
root = _repo(tmp_path, OID)
|
|
if failure == "nonzero":
|
|
_no_git_status(monkeypatch, returncode=128)
|
|
else:
|
|
error = OSError("git missing") if failure == "oserror" else \
|
|
provenance.subprocess.TimeoutExpired(["git"], 5)
|
|
|
|
def run(*_args, **_kwargs):
|
|
raise error
|
|
|
|
monkeypatch.setattr(provenance.subprocess, "run", run)
|
|
got = provenance.tool_provenance(root)
|
|
assert got["obliteratus_git_commit"] == OID
|
|
assert got["obliteratus_git_dirty"] is None
|
|
|
|
|
|
@pytest.mark.parametrize("head", ["ref: refs/heads/missing", "not-an-oid", ""])
|
|
def test_unresolvable_head_records_no_commit_and_skips_git(tmp_path, monkeypatch, head):
|
|
root = _repo(tmp_path, head)
|
|
calls = _no_git_status(monkeypatch)
|
|
got = provenance.tool_provenance(root)
|
|
assert got["obliteratus_git_commit"] is None
|
|
assert got["obliteratus_git_dirty"] is None
|
|
assert calls == []
|
|
|
|
|
|
def test_installed_package_without_checkout(tmp_path, monkeypatch):
|
|
calls = _no_git_status(monkeypatch)
|
|
assert provenance.tool_provenance(tmp_path) == {
|
|
"obliteratus_version": __version__,
|
|
"obliteratus_git_commit": None,
|
|
"obliteratus_git_dirty": None,
|
|
}
|
|
assert calls == []
|
|
|
|
|
|
def test_metadata_carries_provenance_and_source_revision(monkeypatch):
|
|
monkeypatch.setattr(provenance, "tool_provenance", lambda: {
|
|
"obliteratus_version": "9.9.9", "obliteratus_git_commit": OID,
|
|
"obliteratus_git_dirty": False,
|
|
})
|
|
pipeline = AbliterationPipeline("org/model", method="basic")
|
|
pipeline.handle = SimpleNamespace(model=SimpleNamespace(config=SimpleNamespace(
|
|
_commit_hash="fedcba9876543210fedcba9876543210fedcba98")))
|
|
got = pipeline._build_metadata()["provenance"]
|
|
assert got == {
|
|
"obliteratus_version": "9.9.9",
|
|
"obliteratus_git_commit": OID,
|
|
"obliteratus_git_dirty": False,
|
|
"source_model_revision": "fedcba9876543210fedcba9876543210fedcba98",
|
|
}
|
|
|
|
|
|
def test_source_revision_is_none_without_a_loaded_model(monkeypatch):
|
|
monkeypatch.setattr(provenance, "tool_provenance", lambda: {})
|
|
pipeline = AbliterationPipeline("org/model", method="basic")
|
|
assert pipeline._build_provenance() == {"source_model_revision": None}
|
|
|
|
|
|
def test_commit_from_a_linked_worktree(tmp_path, monkeypatch):
|
|
"""A worktree's .git is a file; HEAD is per-worktree, branch refs are in the common dir."""
|
|
calls = _no_git_status(monkeypatch)
|
|
common = tmp_path / "main" / ".git"
|
|
(common / "refs" / "heads").mkdir(parents=True)
|
|
(common / "refs" / "heads" / "feature").write_text(OID + "\n", encoding="utf-8")
|
|
linked = common / "worktrees" / "feature"
|
|
linked.mkdir(parents=True)
|
|
(linked / "HEAD").write_text("ref: refs/heads/feature\n", encoding="utf-8")
|
|
(linked / "commondir").write_text("../..\n", encoding="utf-8")
|
|
# Shared branches must resolve in commondir, not a stale worktree-local ref.
|
|
(linked / "refs/heads").mkdir(parents=True)
|
|
(linked / "refs/heads/feature").write_text("a" * 40, encoding="utf-8")
|
|
checkout = tmp_path / "feature"
|
|
checkout.mkdir()
|
|
(checkout / ".git").write_text(f"gitdir: {linked}\n", encoding="utf-8")
|
|
|
|
got = provenance.tool_provenance(checkout)
|
|
|
|
assert got["obliteratus_git_commit"] == OID
|
|
assert got["obliteratus_git_dirty"] is False
|
|
assert calls[0][1]["cwd"] == checkout
|
|
|
|
|
|
def test_commit_from_a_relative_gitdir_pointer_with_packed_refs(tmp_path, monkeypatch):
|
|
"""Submodules point at their git dir with a relative path."""
|
|
_no_git_status(monkeypatch)
|
|
module = tmp_path / ".git" / "modules" / "obliteratus"
|
|
module.mkdir(parents=True)
|
|
(module / "HEAD").write_text("ref: refs/heads/main\n", encoding="utf-8")
|
|
(module / "packed-refs").write_text(f"{OID} refs/heads/main\n", encoding="utf-8")
|
|
checkout = tmp_path / "obliteratus"
|
|
checkout.mkdir()
|
|
(checkout / ".git").write_text("gitdir: ../.git/modules/obliteratus\n", encoding="utf-8")
|
|
|
|
assert provenance.tool_provenance(checkout)["obliteratus_git_commit"] == OID
|
|
|
|
|
|
@pytest.mark.parametrize("pointer", ["", "not a pointer", "gitdir: does/not/exist"])
|
|
def test_unusable_git_file_records_no_commit_and_skips_git(tmp_path, monkeypatch, pointer):
|
|
calls = _no_git_status(monkeypatch)
|
|
(tmp_path / ".git").write_text(pointer, encoding="utf-8")
|
|
got = provenance.tool_provenance(tmp_path)
|
|
assert got["obliteratus_git_commit"] is None
|
|
assert got["obliteratus_git_dirty"] is None
|
|
assert calls == []
|
|
|
|
|
|
def test_git_dir_without_head_records_no_commit_and_skips_git(tmp_path, monkeypatch):
|
|
calls = _no_git_status(monkeypatch)
|
|
(tmp_path / ".git").mkdir()
|
|
got = provenance.tool_provenance(tmp_path)
|
|
assert got["obliteratus_git_commit"] is None
|
|
assert got["obliteratus_git_dirty"] is None
|
|
assert calls == []
|
|
|
|
|
|
def test_unreadable_git_file_records_no_commit_and_skips_git(tmp_path, monkeypatch):
|
|
calls = _no_git_status(monkeypatch)
|
|
dot_git = tmp_path / ".git"
|
|
dot_git.write_text(f"gitdir: {tmp_path}\n", encoding="utf-8")
|
|
read_file = provenance._read_git_file
|
|
|
|
def unreadable(self, *args, **kwargs):
|
|
if self == dot_git:
|
|
raise PermissionError(13, "Permission denied", str(self))
|
|
return read_file(self, *args, **kwargs)
|
|
|
|
monkeypatch.setattr(provenance, "_read_git_file", unreadable)
|
|
got = provenance.tool_provenance(tmp_path)
|
|
assert got["obliteratus_git_commit"] is None
|
|
assert calls == []
|
|
|
|
|
|
def test_dirty_state_is_unknown_outside_a_checkout(tmp_path, monkeypatch):
|
|
calls = _no_git_status(monkeypatch)
|
|
assert provenance._git_dirty(tmp_path) is None
|
|
assert calls == []
|
|
|
|
|
|
def test_source_revision_ignores_a_non_string_commit_hash(monkeypatch):
|
|
# Metadata is JSON-serialized; a non-string ``_commit_hash`` (e.g. a mocked
|
|
# config) must not leak into it.
|
|
monkeypatch.setattr(provenance, "tool_provenance", lambda: {})
|
|
pipeline = AbliterationPipeline("org/model", method="basic")
|
|
pipeline.handle = SimpleNamespace(model=SimpleNamespace(config=SimpleNamespace(
|
|
_commit_hash=object())))
|
|
assert pipeline._build_provenance() == {"source_model_revision": None}
|
|
|
|
|
|
@pytest.mark.parametrize("name", ["HEAD", "refs/heads/main", "packed-refs", "commondir"])
|
|
@pytest.mark.parametrize("data", [b"\xff", b"x" * 4097])
|
|
def test_malformed_git_metadata_is_optional(tmp_path, monkeypatch, name, data):
|
|
root = _repo(tmp_path, "ref: refs/heads/main")
|
|
path = root / ".git" / name
|
|
path.parent.mkdir(parents=True, exist_ok=True)
|
|
path.write_bytes(data)
|
|
calls = _no_git_status(monkeypatch)
|
|
|
|
assert provenance.tool_provenance(root)["obliteratus_git_commit"] is None
|
|
assert calls == []
|
|
|
|
|
|
@pytest.mark.parametrize("pointer", [b"\xff", b"gitdir: \x00", b"gitdir: \n", b"x" * 4097])
|
|
def test_malformed_git_pointer_is_optional(tmp_path, monkeypatch, pointer):
|
|
(tmp_path / ".git").write_bytes(pointer)
|
|
calls = _no_git_status(monkeypatch)
|
|
assert provenance.tool_provenance(tmp_path)["obliteratus_git_commit"] is None
|
|
assert calls == []
|
|
|
|
|
|
@pytest.mark.parametrize("ref", ["../outside", "/outside", "refs/../../outside",
|
|
"refs/heads/../main", "refs//main", "refs\\heads\\main"])
|
|
def test_ref_paths_cannot_escape_git_directory(tmp_path, monkeypatch, ref):
|
|
root = _repo(tmp_path, "ref: " + ref)
|
|
(tmp_path / "outside").write_text(OID)
|
|
calls = _no_git_status(monkeypatch)
|
|
assert provenance.tool_provenance(root)["obliteratus_git_commit"] is None
|
|
assert calls == []
|
|
|
|
|
|
def _symlink(path, target, *, directory=False):
|
|
try:
|
|
path.symlink_to(target, target_is_directory=directory)
|
|
except (OSError, NotImplementedError):
|
|
pytest.skip("symlinks unavailable")
|
|
|
|
|
|
@pytest.mark.parametrize("target", ["outside", "loop"])
|
|
def test_ref_symlinks_cannot_escape_or_loop(tmp_path, monkeypatch, target):
|
|
root = _repo(tmp_path, "ref: refs/heads/main")
|
|
ref = root / ".git/refs/heads/main"
|
|
ref.parent.mkdir(parents=True)
|
|
outside = tmp_path / "outside"
|
|
outside.write_text(OID)
|
|
_symlink(ref, outside if target == "outside" else ref)
|
|
calls = _no_git_status(monkeypatch)
|
|
assert provenance.tool_provenance(root)["obliteratus_git_commit"] is None
|
|
assert calls == []
|
|
|
|
|
|
def test_symlinked_git_directory_and_internal_head(tmp_path, monkeypatch):
|
|
root = _repo(tmp_path, OID)
|
|
git = root / ".git"
|
|
git.rename(root / "git-metadata")
|
|
_symlink(git, root / "git-metadata", directory=True)
|
|
(git / "HEAD").rename(git / "detached")
|
|
_symlink(git / "HEAD", "detached")
|
|
_no_git_status(monkeypatch)
|
|
assert provenance.tool_provenance(root)["obliteratus_git_commit"] == OID
|
|
|
|
|
|
def test_symlinked_package_uses_source_checkout(tmp_path, monkeypatch):
|
|
root = _repo(tmp_path, OID)
|
|
package = root / "obliteratus"
|
|
package.mkdir()
|
|
installed = tmp_path / "site-packages/obliteratus"
|
|
installed.parent.mkdir()
|
|
_symlink(installed, package, directory=True)
|
|
monkeypatch.setattr(provenance, "_PACKAGE_DIR", installed)
|
|
calls = _no_git_status(monkeypatch)
|
|
assert provenance.tool_provenance()["obliteratus_git_commit"] == OID
|
|
assert calls[0][1]["cwd"] == root
|
|
|
|
|
|
@pytest.mark.skipif(not hasattr(os, "mkfifo"), reason="requires POSIX FIFO")
|
|
def test_fifo_head_cannot_block_export(tmp_path, monkeypatch):
|
|
root = _repo(tmp_path, OID)
|
|
head = root / ".git/HEAD"
|
|
head.unlink()
|
|
os.mkfifo(head)
|
|
calls = _no_git_status(monkeypatch)
|
|
assert provenance.tool_provenance(root)["obliteratus_git_commit"] is None
|
|
assert calls == []
|
|
|
|
|
|
def test_oversized_packed_refs_are_optional(tmp_path, monkeypatch):
|
|
root = _repo(tmp_path, "ref: refs/heads/main")
|
|
(root / ".git/packed-refs").write_bytes(b"x" * (8 * 1024 * 1024 + 1))
|
|
calls = _no_git_status(monkeypatch)
|
|
assert provenance.tool_provenance(root)["obliteratus_git_commit"] is None
|
|
assert calls == []
|
|
|
|
|
|
def test_unreadable_loose_ref_cannot_fall_back_to_stale_packed_ref(tmp_path, monkeypatch):
|
|
root = _repo(tmp_path, "ref: refs/heads/main", packed=f"{OID} refs/heads/main\n")
|
|
read_file = provenance._read_git_file
|
|
|
|
def unreadable(path, *args, **kwargs):
|
|
if path == root / ".git/refs/heads/main":
|
|
raise PermissionError("ref unreadable")
|
|
return read_file(path, *args, **kwargs)
|
|
|
|
monkeypatch.setattr(provenance, "_read_git_file", unreadable)
|
|
calls = _no_git_status(monkeypatch)
|
|
assert provenance.tool_provenance(root)["obliteratus_git_commit"] is None
|
|
assert calls == []
|
|
|
|
|
|
def test_sha256_detached_head(tmp_path, monkeypatch):
|
|
root = _repo(tmp_path, "a" * 64)
|
|
_no_git_status(monkeypatch)
|
|
assert provenance.tool_provenance(root)["obliteratus_git_commit"] == "a" * 64
|
|
|
|
|
|
def test_dirty_status_ignores_git_environment_and_does_not_decode(tmp_path, monkeypatch):
|
|
root = _repo(tmp_path, OID)
|
|
monkeypatch.setenv("GIT_DIR", "/other/repo/.git")
|
|
monkeypatch.setenv("GIT_WORK_TREE", "/other/repo")
|
|
monkeypatch.setenv("GIT_INDEX_FILE", "/other/index")
|
|
monkeypatch.setenv("GIT_CONFIG_COUNT", "1")
|
|
calls = _no_git_status(monkeypatch, stdout=b" M \xff\n")
|
|
assert provenance.tool_provenance(root)["obliteratus_git_dirty"] is True
|
|
kwargs = calls[0][1]
|
|
assert not kwargs.get("text")
|
|
assert not any(key.startswith("GIT_") for key in kwargs["env"])
|
|
assert kwargs["env"]["PATH"] == os.environ["PATH"]
|
|
|
|
|
|
@pytest.mark.parametrize("stage", ["_read_git_commit", "_git_dirty"])
|
|
def test_unexpected_provenance_failure_cannot_abort_export(tmp_path, monkeypatch, stage):
|
|
root = _repo(tmp_path, OID)
|
|
|
|
def fail(*args):
|
|
raise RuntimeError("metadata unavailable")
|
|
|
|
monkeypatch.setattr(provenance, stage, fail)
|
|
got = provenance.tool_provenance(root)
|
|
assert got["obliteratus_version"] == __version__
|
|
assert got["obliteratus_git_commit"] == (OID if stage == "_git_dirty" else None)
|
|
assert got["obliteratus_git_dirty"] is None
|
|
|
|
|
|
@pytest.mark.parametrize("revision", [None, "", "main", "v1.0", "abc123", "a" * 64, object()])
|
|
def test_source_revision_requires_a_resolved_hub_commit(revision):
|
|
assert provenance.source_model_revision("org/model", SimpleNamespace(_commit_hash=revision)) is None
|
|
|
|
|
|
def test_local_snapshot_revision_survives_symlink_and_config_loss(tmp_path):
|
|
snapshot = tmp_path / "models--org--model/snapshots" / OID
|
|
snapshot.mkdir(parents=True)
|
|
assert provenance.source_model_revision(str(snapshot)) == OID
|
|
alias = tmp_path / "model"
|
|
_symlink(alias, snapshot, directory=True)
|
|
assert provenance.source_model_revision(str(alias), SimpleNamespace(_commit_hash=None)) == OID
|
|
|
|
|
|
def test_ordinary_local_model_does_not_claim_a_stale_commit(tmp_path):
|
|
assert provenance.source_model_revision(str(tmp_path), SimpleNamespace(_commit_hash=OID)) is None
|
|
|
|
|
|
def test_metadata_prefers_revision_captured_by_loader(monkeypatch):
|
|
monkeypatch.setattr(provenance, "tool_provenance", lambda: {})
|
|
pipeline = AbliterationPipeline("org/model", method="basic", fold_post_norms=False)
|
|
pipeline.handle = SimpleNamespace(
|
|
source_model_revision=OID, model=SimpleNamespace(config=SimpleNamespace(_commit_hash=None)),
|
|
)
|
|
metadata = pipeline._build_metadata()
|
|
assert metadata["provenance"] == {"source_model_revision": OID}
|
|
assert metadata["method_config"]["fold_post_norms"] is False
|
|
assert metadata["concept"]["id"] == "refusal"
|
|
|
|
|
|
def test_source_config_failure_cannot_abort_metadata(monkeypatch):
|
|
class BrokenModel:
|
|
@property
|
|
def config(self):
|
|
raise RuntimeError("config unavailable")
|
|
|
|
monkeypatch.setattr(provenance, "tool_provenance", lambda: {})
|
|
pipeline = AbliterationPipeline("org/model", method="basic")
|
|
pipeline.handle = SimpleNamespace(model=BrokenModel())
|
|
assert pipeline._build_metadata()["provenance"] == {"source_model_revision": None}
|