mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-05 10:34:23 +01:00
Add CPU-only offline tests for the GSQ adapter, workflow runner, and coverage certifier guard rails that the release lane did not reach: real git checkout verification in a temp repo, non-UTF-8/unreadable checkout files, plan-vs-write tree drift, the post-promotion trust_remote_code sweep, staging/backup/promotion rollback branches, launch-binding and manifest tampering, post-assembly drift, pre-spawn source re-verification for run/assemble, header-only safetensors parsing, weight-index agreement, explicit shape tables, assembled index/config/packed-content checks, and retained base tensors. GSQ is never executed: subprocess launches are faked and the checkout is the fake adapter tree. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
434 lines
15 KiB
Python
434 lines
15 KiB
Python
"""CPU tests for fail-closed edges of the GSQ source adapter.
|
|
|
|
These cover the paths the happy-path prepare tests do not reach: the
|
|
real ``git`` checkout verification, unreadable or non-UTF-8 checkout
|
|
files, a tree that changes between planning and writing, the
|
|
post-promotion ``trust_remote_code`` sweep, stale temp cleanup, and the
|
|
staging/backup/promotion rollback branches of ``_promote_atomically``.
|
|
Every failure must leave the tree byte-identical (or report exactly
|
|
what it could not restore) without leaking temps.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
import subprocess
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
from obliteratus.gsq import source
|
|
|
|
|
|
def _write_fake_tree(root: Path) -> None:
|
|
olds: dict[str, list[str]] = {}
|
|
for relative, old, _new, count in source.TEXT_PATCHES:
|
|
olds.setdefault(relative, []).extend([old] * count)
|
|
for relative, fragments in olds.items():
|
|
path = root / relative
|
|
path.parent.mkdir(parents=True, exist_ok=True)
|
|
path.write_text(
|
|
"".join(fragments) + "\ntrust_remote_code=True\n", encoding="utf-8"
|
|
)
|
|
|
|
|
|
@pytest.fixture
|
|
def fake_tree(tmp_path, monkeypatch):
|
|
root = tmp_path / "GSQ"
|
|
_write_fake_tree(root)
|
|
monkeypatch.setattr(source, "_git", lambda _root, *args: source.GSQ_REVISION)
|
|
return root
|
|
|
|
|
|
def _snapshot(root: Path) -> dict[str, bytes]:
|
|
return {
|
|
str(path.relative_to(root)): path.read_bytes()
|
|
for path in sorted(root.rglob("*"))
|
|
if path.is_file()
|
|
}
|
|
|
|
|
|
def _leftovers(root: Path, suffix: str) -> list[Path]:
|
|
return [path for path in root.rglob(f"*{suffix}") if path.is_file()]
|
|
|
|
|
|
# --- checkout verification through the real git boundary -----------------
|
|
|
|
|
|
def _isolate_git(monkeypatch, tmp_path: Path) -> None:
|
|
"""Run git against only the temp dir: no inherited repo, config, or hooks."""
|
|
for key in list(os.environ):
|
|
if key.startswith("GIT_"):
|
|
monkeypatch.delenv(key)
|
|
for key, value in {
|
|
"GIT_CONFIG_GLOBAL": os.devnull,
|
|
"GIT_CONFIG_NOSYSTEM": "1",
|
|
"GIT_CEILING_DIRECTORIES": str(tmp_path),
|
|
"GIT_AUTHOR_NAME": "t",
|
|
"GIT_AUTHOR_EMAIL": "t@example.invalid",
|
|
"GIT_COMMITTER_NAME": "t",
|
|
"GIT_COMMITTER_EMAIL": "t@example.invalid",
|
|
}.items():
|
|
monkeypatch.setenv(key, value)
|
|
|
|
|
|
def test_verify_checkout_rejects_non_git_directory(tmp_path, monkeypatch):
|
|
_isolate_git(monkeypatch, tmp_path)
|
|
plain = tmp_path / "not-a-checkout"
|
|
plain.mkdir()
|
|
with pytest.raises(RuntimeError, match="cannot verify GSQ checkout"):
|
|
source.verify_checkout(plain)
|
|
|
|
|
|
def test_verify_checkout_rejects_unpinned_head(tmp_path, monkeypatch):
|
|
_isolate_git(monkeypatch, tmp_path)
|
|
repo = tmp_path / "repo"
|
|
repo.mkdir()
|
|
subprocess.run(["git", "init", "-q", str(repo)], check=True)
|
|
subprocess.run(
|
|
["git", "-C", str(repo), "commit", "-q", "--allow-empty", "-m", "x"],
|
|
check=True,
|
|
)
|
|
head = source._git(repo, "rev-parse", "HEAD")
|
|
assert len(head) == 40 and head != source.GSQ_REVISION
|
|
with pytest.raises(RuntimeError, match=f"found {head}"):
|
|
source.verify_checkout(repo)
|
|
|
|
|
|
def test_guard_source_unreadable_fails_closed(tmp_path, monkeypatch):
|
|
monkeypatch.setattr(source, "__file__", str(tmp_path / "source.py"))
|
|
with pytest.raises(RuntimeError, match="cannot read numerics guard source"):
|
|
source._guard_source_bytes()
|
|
|
|
|
|
# --- phase 1 read failures ------------------------------------------------
|
|
|
|
|
|
def test_prepare_rejects_non_utf8_patch_target(fake_tree):
|
|
(fake_tree / "main.py").write_bytes(b"\xff\xfe not utf-8")
|
|
before = _snapshot(fake_tree)
|
|
with pytest.raises(RuntimeError, match="main.py is not UTF-8"):
|
|
source.prepare(fake_tree)
|
|
assert _snapshot(fake_tree) == before
|
|
|
|
|
|
def test_prepare_rejects_non_utf8_extra_python_file(fake_tree):
|
|
extra = fake_tree / "tools" / "binary.py"
|
|
extra.parent.mkdir()
|
|
extra.write_bytes(b"\x80\x81")
|
|
before = _snapshot(fake_tree)
|
|
with pytest.raises(RuntimeError, match="binary.py is not UTF-8"):
|
|
source.prepare(fake_tree)
|
|
assert _snapshot(fake_tree) == before
|
|
|
|
|
|
def test_prepare_rejects_unreadable_extra_python_path(fake_tree):
|
|
# A directory named like a module matches ``*.py`` but cannot be read.
|
|
(fake_tree / "pkg.py").mkdir()
|
|
before = _snapshot(fake_tree)
|
|
with pytest.raises(RuntimeError, match="cannot read GSQ file"):
|
|
source.prepare(fake_tree)
|
|
assert _snapshot(fake_tree) == before
|
|
|
|
|
|
def test_excluded_directories_are_not_swept(fake_tree):
|
|
venv_file = fake_tree / ".venv" / "lib" / "site.py"
|
|
venv_file.parent.mkdir(parents=True)
|
|
venv_file.write_text("x(trust_remote_code=True)\n", encoding="utf-8")
|
|
source.prepare(fake_tree)
|
|
assert "trust_remote_code=True" in venv_file.read_text(encoding="utf-8")
|
|
assert venv_file not in list(source._iter_python_files(fake_tree))
|
|
|
|
|
|
# --- planning vs writing --------------------------------------------------
|
|
|
|
|
|
def test_build_jobs_refuses_tree_changed_after_planning(fake_tree):
|
|
root = fake_tree.resolve()
|
|
current, originals = source._read_staged_texts(root)
|
|
(root / "main.py").write_text("tampered\n", encoding="utf-8")
|
|
with pytest.raises(RuntimeError, match="tree changed during prepare"):
|
|
source._build_prepare_jobs(root, current, originals, b"guard")
|
|
|
|
|
|
def test_build_jobs_refuses_target_removed_after_planning(fake_tree):
|
|
root = fake_tree.resolve()
|
|
current, originals = source._read_staged_texts(root)
|
|
(root / "main.py").unlink()
|
|
with pytest.raises(RuntimeError, match="cannot re-read GSQ file"):
|
|
source._build_prepare_jobs(root, current, originals, b"guard")
|
|
|
|
|
|
def test_prospective_digest_ignores_irrelevant_jobs(fake_tree, tmp_path):
|
|
root = fake_tree.resolve()
|
|
originals = {
|
|
path: path.read_bytes() for path in source._iter_python_files(root)
|
|
}
|
|
baseline = source._prospective_digest_from_plan(root, {}, originals)
|
|
assert baseline == source.tree_digest(root)
|
|
irrelevant = {
|
|
root / "README.md": b"docs",
|
|
tmp_path / "elsewhere.py": b"outside the checkout",
|
|
root / "__pycache__" / "cached.py": b"excluded",
|
|
}
|
|
assert (
|
|
source._prospective_digest_from_plan(root, irrelevant, originals)
|
|
== baseline
|
|
)
|
|
relevant = {root / "new_module.py": b"x = 1\n"}
|
|
assert (
|
|
source._prospective_digest_from_plan(root, relevant, originals)
|
|
!= baseline
|
|
)
|
|
|
|
|
|
def test_prospective_digest_matches_prepared_tree(fake_tree):
|
|
before = _snapshot(fake_tree)
|
|
digest = source.prospective_prepared_digest(fake_tree)
|
|
assert _snapshot(fake_tree) == before
|
|
source.prepare(fake_tree)
|
|
assert source.tree_digest(fake_tree) == digest
|
|
|
|
|
|
def test_prepare_post_sweep_detects_unpromoted_remote_code(fake_tree, monkeypatch):
|
|
"""If promotion silently writes nothing, the final sweep refuses."""
|
|
monkeypatch.setattr(source, "_promote_atomically", lambda root, jobs: None)
|
|
with pytest.raises(RuntimeError, match="remote-code trust remains enabled"):
|
|
source.prepare(fake_tree)
|
|
|
|
|
|
def test_noop_prepare_removes_stale_temps(fake_tree):
|
|
source.prepare(fake_tree)
|
|
stale = fake_tree / ("main.py.abc" + source._PREPARE_TMP_SUFFIX)
|
|
stale.write_bytes(b"killed run")
|
|
odd_dir = fake_tree / ("dir" + source._PREPARE_TMP_SUFFIX)
|
|
odd_dir.mkdir()
|
|
assert source.prepare(fake_tree) == []
|
|
assert not stale.exists()
|
|
# Only files are removed; a same-named directory is left alone.
|
|
assert odd_dir.is_dir()
|
|
|
|
|
|
# --- _stage_temp / _record_target_modes -----------------------------------
|
|
|
|
|
|
def test_stage_temp_removes_partial_file_on_failure(tmp_path, monkeypatch):
|
|
target = tmp_path / "file.py"
|
|
|
|
def _boom(_fd):
|
|
raise OSError("fsync failed (injected)")
|
|
|
|
monkeypatch.setattr(source.os, "fsync", _boom)
|
|
with pytest.raises(OSError, match="fsync failed"):
|
|
source._stage_temp(target, b"data")
|
|
assert list(tmp_path.iterdir()) == []
|
|
|
|
|
|
def test_record_modes_fails_closed_when_stat_fails(tmp_path):
|
|
class _Unstatable(type(tmp_path)):
|
|
def is_file(self, **_kwargs):
|
|
return True
|
|
|
|
def stat(self, **_kwargs):
|
|
raise OSError("stat denied (injected)")
|
|
|
|
target = _Unstatable(tmp_path / "x.py")
|
|
with pytest.raises(RuntimeError, match="cannot stat GSQ file"):
|
|
source._record_target_modes({target: b""})
|
|
|
|
|
|
# --- _promote_atomically rollback branches --------------------------------
|
|
|
|
|
|
@pytest.fixture
|
|
def two_files(tmp_path):
|
|
root = tmp_path / "tree"
|
|
root.mkdir()
|
|
first = root / "a.py"
|
|
second = root / "b.py"
|
|
first.write_bytes(b"a-old")
|
|
second.write_bytes(b"b-old")
|
|
return root, first, second
|
|
|
|
|
|
def _assert_clean(root: Path) -> None:
|
|
assert _leftovers(root, source._PREPARE_TMP_SUFFIX) == []
|
|
|
|
|
|
def test_promote_stage_failure_removes_earlier_temps(two_files, monkeypatch):
|
|
root, first, second = two_files
|
|
real_stage = source._stage_temp
|
|
|
|
def _stage(target, data):
|
|
if target == second:
|
|
raise OSError("no space (injected)")
|
|
return real_stage(target, data)
|
|
|
|
monkeypatch.setattr(source, "_stage_temp", _stage)
|
|
with pytest.raises(RuntimeError, match="cannot stage GSQ file"):
|
|
source._promote_atomically(root, {first: b"a-new", second: b"b-new"})
|
|
assert first.read_bytes() == b"a-old" and second.read_bytes() == b"b-old"
|
|
_assert_clean(root)
|
|
|
|
|
|
def test_promote_chmod_failure_removes_temps(two_files, monkeypatch):
|
|
root, first, second = two_files
|
|
|
|
def _chmod(_path, _mode):
|
|
raise OSError("read-only fs (injected)")
|
|
|
|
monkeypatch.setattr(source.os, "chmod", _chmod)
|
|
with pytest.raises(RuntimeError, match="cannot set mode on staged GSQ file"):
|
|
source._promote_atomically(root, {first: b"a-new", second: b"b-new"})
|
|
assert first.read_bytes() == b"a-old" and second.read_bytes() == b"b-old"
|
|
_assert_clean(root)
|
|
|
|
|
|
def test_promote_backup_stage_failure(two_files, monkeypatch):
|
|
root, first, second = two_files
|
|
real_stage = source._stage_temp
|
|
|
|
def _stage(target, data):
|
|
if target.name.endswith(source._PREPARE_BACKUP_SUFFIX):
|
|
raise OSError("backup disk full (injected)")
|
|
return real_stage(target, data)
|
|
|
|
monkeypatch.setattr(source, "_stage_temp", _stage)
|
|
with pytest.raises(RuntimeError, match="cannot back up GSQ file"):
|
|
source._promote_atomically(root, {first: b"a-new", second: b"b-new"})
|
|
assert first.read_bytes() == b"a-old" and second.read_bytes() == b"b-old"
|
|
_assert_clean(root)
|
|
|
|
|
|
def test_promote_backup_replace_failure(two_files, monkeypatch):
|
|
root, first, second = two_files
|
|
real_replace = os.replace
|
|
|
|
def _replace(src, dst, *args, **kwargs):
|
|
if str(dst).endswith(source._PREPARE_BACKUP_SUFFIX):
|
|
raise OSError("rename denied (injected)")
|
|
return real_replace(src, dst, *args, **kwargs)
|
|
|
|
monkeypatch.setattr(source.os, "replace", _replace)
|
|
with pytest.raises(RuntimeError, match="cannot back up GSQ file"):
|
|
source._promote_atomically(root, {first: b"a-new", second: b"b-new"})
|
|
assert first.read_bytes() == b"a-old" and second.read_bytes() == b"b-old"
|
|
_assert_clean(root)
|
|
|
|
|
|
def test_promote_backup_interrupt_cleans_temps_and_backups(two_files, monkeypatch):
|
|
root, first, second = two_files
|
|
real_replace = os.replace
|
|
seen: list[str] = []
|
|
|
|
def _replace(src, dst, *args, **kwargs):
|
|
if str(dst).endswith(source._PREPARE_BACKUP_SUFFIX):
|
|
seen.append(Path(dst).name)
|
|
if len(seen) == 2:
|
|
raise KeyboardInterrupt("ctrl-c (injected)")
|
|
return real_replace(src, dst, *args, **kwargs)
|
|
|
|
monkeypatch.setattr(source.os, "replace", _replace)
|
|
with pytest.raises(KeyboardInterrupt):
|
|
source._promote_atomically(root, {first: b"a-new", second: b"b-new"})
|
|
assert len(seen) == 2
|
|
assert first.read_bytes() == b"a-old" and second.read_bytes() == b"b-old"
|
|
_assert_clean(root)
|
|
assert _leftovers(root, source._PREPARE_BACKUP_SUFFIX) == []
|
|
|
|
|
|
def test_promote_oserror_removes_new_file_and_restores_existing(
|
|
two_files, monkeypatch
|
|
):
|
|
root, first, _second = two_files
|
|
new_file = root / "guard.py"
|
|
real_replace = os.replace
|
|
failed = []
|
|
|
|
def _replace(src, dst, *args, **kwargs):
|
|
if str(dst) == str(first) and not failed:
|
|
failed.append(dst)
|
|
raise OSError("promotion failed (injected)")
|
|
return real_replace(src, dst, *args, **kwargs)
|
|
|
|
monkeypatch.setattr(source.os, "replace", _replace)
|
|
with pytest.raises(RuntimeError, match="cannot promote GSQ files") as info:
|
|
source._promote_atomically(root, {new_file: b"guard", first: b"a-new"})
|
|
assert "cannot restore" not in str(info.value)
|
|
assert not new_file.exists()
|
|
assert first.read_bytes() == b"a-old"
|
|
_assert_clean(root)
|
|
|
|
|
|
def test_promote_oserror_reports_failed_restore(two_files, monkeypatch):
|
|
root, first, second = two_files
|
|
real_replace = os.replace
|
|
|
|
def _replace(src, dst, *args, **kwargs):
|
|
if str(dst) == str(second) and source._PREPARE_TMP_SUFFIX in str(src):
|
|
raise OSError("promotion failed (injected)")
|
|
return real_replace(src, dst, *args, **kwargs)
|
|
|
|
def _restore(_target, _backup, _mode):
|
|
raise OSError("restore failed (injected)")
|
|
|
|
monkeypatch.setattr(source.os, "replace", _replace)
|
|
monkeypatch.setattr(source, "_restore_from_backup", _restore)
|
|
with pytest.raises(RuntimeError) as info:
|
|
source._promote_atomically(root, {first: b"a-new", second: b"b-new"})
|
|
message = str(info.value)
|
|
assert "cannot promote GSQ files" in message
|
|
assert f"cannot restore {first}" in message
|
|
# The backup is kept for inspection when its restore failed.
|
|
backup = first.with_name(first.name + source._PREPARE_BACKUP_SUFFIX)
|
|
assert backup.read_bytes() == b"a-old"
|
|
_assert_clean(root)
|
|
|
|
|
|
def test_promote_interrupt_keeps_backup_whose_restore_failed(
|
|
two_files, monkeypatch
|
|
):
|
|
root, first, second = two_files
|
|
new_file = root / "guard.py"
|
|
real_replace = os.replace
|
|
|
|
def _replace(src, dst, *args, **kwargs):
|
|
if str(dst) == str(second) and source._PREPARE_TMP_SUFFIX in str(src):
|
|
raise KeyboardInterrupt("ctrl-c (injected)")
|
|
return real_replace(src, dst, *args, **kwargs)
|
|
|
|
def _restore(target, _backup, _mode):
|
|
if target == first:
|
|
raise OSError("restore failed (injected)")
|
|
|
|
monkeypatch.setattr(source.os, "replace", _replace)
|
|
monkeypatch.setattr(source, "_restore_from_backup", _restore)
|
|
with pytest.raises(KeyboardInterrupt):
|
|
source._promote_atomically(
|
|
root, {new_file: b"guard", first: b"a-new", second: b"b-new"}
|
|
)
|
|
# The new file (no backup) is removed; the failed restore keeps its
|
|
# backup while the other backup is cleaned up.
|
|
assert not new_file.exists()
|
|
first_backup = first.with_name(first.name + source._PREPARE_BACKUP_SUFFIX)
|
|
second_backup = second.with_name(second.name + source._PREPARE_BACKUP_SUFFIX)
|
|
assert first_backup.read_bytes() == b"a-old"
|
|
assert not second_backup.exists()
|
|
_assert_clean(root)
|
|
|
|
|
|
def test_restore_from_backup_cleans_staged_temp_on_failure(tmp_path, monkeypatch):
|
|
target = tmp_path / "x.py"
|
|
target.write_bytes(b"new")
|
|
backup = tmp_path / "x.py.bak"
|
|
backup.write_bytes(b"old")
|
|
|
|
def _replace(_src, _dst, *args, **kwargs):
|
|
raise OSError("replace failed (injected)")
|
|
|
|
monkeypatch.setattr(source.os, "replace", _replace)
|
|
with pytest.raises(OSError, match="replace failed"):
|
|
source._restore_from_backup(target, backup, 0o644)
|
|
assert target.read_bytes() == b"new"
|
|
assert _leftovers(tmp_path, source._PREPARE_TMP_SUFFIX) == []
|