mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-06 11:04:06 +01:00
- Require the exact config-group and weights key sets the pinned assembler writes, with exact JSON types, so per-group format overrides, block_structure, and unknown nested keys fail closed. - Restore and back up prepared files via staged temp files and os.replace with the recorded mode, so read-only originals roll back cleanly and retries are not blocked by retained backups. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
279 lines
11 KiB
Python
279 lines
11 KiB
Python
"""CPU tests for permission-bit preservation in GSQ ``prepare``.
|
|
|
|
Atomic prepare used to install ``mkstemp`` files (mode ``0600``) over
|
|
the originals, discarding permission bits (``0755`` became ``0600``).
|
|
Staged replacements must keep the original file's mode, backups and
|
|
restores must carry it, and new files must get ``0o644 & ~umask``.
|
|
Restores and backups must also go through staged replacement rather
|
|
than writing through the file: with ``0444`` originals in a writable
|
|
directory, a mid-promotion failure (or interrupt) still rolls back to
|
|
byte- and mode-identical files, and the retry succeeds.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
import stat
|
|
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"
|
|
)
|
|
extra = root / "tests" / "eval_humming_model.py"
|
|
extra.parent.mkdir(parents=True, exist_ok=True)
|
|
extra.write_text("value = load(name, trust_remote_code=True)\n", encoding="utf-8")
|
|
|
|
|
|
def _make_tree(tmp_path: Path, monkeypatch, *, mode: int) -> Path:
|
|
root = tmp_path / "GSQ"
|
|
_write_fake_tree(root)
|
|
for path in root.rglob("*.py"):
|
|
os.chmod(path, mode)
|
|
monkeypatch.setattr(source, "_git", lambda _root, *args: source.GSQ_REVISION)
|
|
return root
|
|
|
|
|
|
def _mode(path: Path) -> int:
|
|
return stat.S_IMODE(path.stat().st_mode)
|
|
|
|
|
|
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 test_prepare_preserves_executable_modes(tmp_path, monkeypatch):
|
|
"""0755 patch targets stay 0755; the new guard honors umask."""
|
|
old_umask = os.umask(0o022)
|
|
try:
|
|
root = _make_tree(tmp_path, monkeypatch, mode=0o755)
|
|
changed = source.prepare(root)
|
|
finally:
|
|
os.umask(old_umask)
|
|
assert changed
|
|
for relative, _, _, _ in source.TEXT_PATCHES:
|
|
assert _mode(root / relative) == 0o755
|
|
assert _mode(root / "tests" / "eval_humming_model.py") == 0o755
|
|
assert _mode(root / source.GUARD_FILENAME) == 0o644
|
|
# Patched contents still land.
|
|
assert "trust_remote_code=True" not in (root / "main.py").read_text(
|
|
encoding="utf-8"
|
|
)
|
|
|
|
|
|
def test_prepare_preserves_plain_modes(tmp_path, monkeypatch):
|
|
"""0644 patch targets stay 0644 after prepare."""
|
|
root = _make_tree(tmp_path, monkeypatch, mode=0o644)
|
|
source.prepare(root)
|
|
for relative, _, _, _ in source.TEXT_PATCHES:
|
|
assert _mode(root / relative) == 0o644
|
|
assert _mode(root / source.GUARD_FILENAME) == (
|
|
0o644 & ~source._current_umask()
|
|
)
|
|
|
|
|
|
def test_prepare_new_guard_honors_restrictive_umask(tmp_path, monkeypatch):
|
|
"""A new guard file under umask 077 is 0600, not world-readable."""
|
|
old_umask = os.umask(0o077)
|
|
try:
|
|
root = _make_tree(tmp_path, monkeypatch, mode=0o755)
|
|
source.prepare(root)
|
|
finally:
|
|
os.umask(old_umask)
|
|
assert _mode(root / source.GUARD_FILENAME) == 0o600
|
|
# Existing files keep their own mode regardless of the umask.
|
|
assert _mode(root / "main.py") == 0o755
|
|
|
|
|
|
def test_prepare_existing_guard_keeps_its_mode(tmp_path, monkeypatch):
|
|
"""A stale guard already on disk is a replacement, not a new file."""
|
|
root = _make_tree(tmp_path, monkeypatch, mode=0o644)
|
|
source.prepare(root)
|
|
guard = root / source.GUARD_FILENAME
|
|
guard.write_bytes(b"# stale guard\n")
|
|
os.chmod(guard, 0o700)
|
|
assert source.prepare(root) == [source.GUARD_FILENAME]
|
|
packaged = (Path(source.__file__).with_name("numerics_guard.py")).read_bytes()
|
|
assert guard.read_bytes() == packaged
|
|
assert _mode(guard) == 0o700
|
|
|
|
|
|
def test_prepare_rollback_preserves_modes(tmp_path, monkeypatch):
|
|
"""A mid-promotion OSError restores bytes and modes (0755)."""
|
|
root = _make_tree(tmp_path, monkeypatch, mode=0o755)
|
|
before = _snapshot(root)
|
|
real_replace = os.replace
|
|
calls = {"n": 0}
|
|
|
|
def _failing_replace(src, dst):
|
|
# Backup replaces are never failed: the failure must land
|
|
# mid-promotion, after files were already promoted.
|
|
if not str(dst).endswith(source._PREPARE_BACKUP_SUFFIX):
|
|
calls["n"] += 1
|
|
if calls["n"] == 2:
|
|
raise OSError("simulated promotion failure")
|
|
return real_replace(src, dst)
|
|
|
|
monkeypatch.setattr(os, "replace", _failing_replace)
|
|
with pytest.raises(RuntimeError, match="cannot promote GSQ files"):
|
|
source.prepare(root)
|
|
# Bytes are byte-identical (backups kept for inspection on OSError).
|
|
current = {
|
|
key: value
|
|
for key, value in _snapshot(root).items()
|
|
if not key.endswith(source._PREPARE_BACKUP_SUFFIX)
|
|
and source._PREPARE_TMP_SUFFIX not in key
|
|
}
|
|
assert current == before
|
|
for relative, _, _, _ in source.TEXT_PATCHES:
|
|
assert _mode(root / relative) == 0o755
|
|
|
|
|
|
def test_tree_digest_ignores_modes(tmp_path, monkeypatch):
|
|
"""The recorded digest covers paths and bytes only, never modes."""
|
|
root = _make_tree(tmp_path, monkeypatch, mode=0o644)
|
|
source.prepare(root)
|
|
digest = source.tree_digest(root)
|
|
assert source.prospective_prepared_digest(root) == digest
|
|
for path in list(root.rglob("*.py"))[:5]:
|
|
os.chmod(path, 0o755)
|
|
assert source.tree_digest(root) == digest
|
|
assert source.prospective_prepared_digest(root) == digest
|
|
|
|
|
|
def test_tree_digest_hashes_posix_relative_paths(tmp_path, monkeypatch):
|
|
"""Digest path inputs are canonical POSIX on every platform.
|
|
|
|
Both digest implementations must feed ``as_posix`` relative paths,
|
|
so a Windows checkout of the same tree hashes byte-identical path
|
|
inputs: a ``PureWindowsPath`` spelling of a nested relative path
|
|
must produce the same digest input as the POSIX one. The recorded
|
|
digest itself is unchanged by the canonicalization.
|
|
"""
|
|
import hashlib
|
|
from pathlib import PurePosixPath, PureWindowsPath
|
|
|
|
assert source.EXPECTED_PREPARED_TREE_SHA256 == (
|
|
"6f56d133aa95116f9ec7ab76154d8af5ab830187bbb37ceed5d0d73d84d0d1d9"
|
|
)
|
|
nested = PurePosixPath("src/models/qwen35.py")
|
|
windows = PureWindowsPath("src\\models\\qwen35.py")
|
|
assert windows.as_posix() == nested.as_posix() == "src/models/qwen35.py"
|
|
# Pin the exact digest-input encoding on a nested tree: sorted
|
|
# POSIX relative path, NUL, file bytes.
|
|
root = tmp_path / "tree"
|
|
(root / "src" / "models").mkdir(parents=True)
|
|
(root / "src" / "models" / "qwen35.py").write_bytes(b"nested = 1\n")
|
|
(root / "top.py").write_bytes(b"top = 2\n")
|
|
expected = hashlib.sha256()
|
|
for name in ("src/models/qwen35.py", "top.py"):
|
|
expected.update(name.encode())
|
|
expected.update(b"\0")
|
|
expected.update((root / name).read_bytes())
|
|
assert source.tree_digest(root) == expected.hexdigest()
|
|
# On POSIX as_posix is byte-identical to str, so the recorded digest
|
|
# cannot move: the prepared fake tree digests the same before/after.
|
|
prepared = _make_tree(tmp_path / "prep", monkeypatch, mode=0o644)
|
|
source.prepare(prepared)
|
|
before = source.tree_digest(prepared)
|
|
assert source.prospective_prepared_digest(prepared) == before
|
|
assert before == source.tree_digest(prepared)
|
|
|
|
|
|
def _snapshot_with_modes(root: Path) -> dict[str, tuple[bytes, int]]:
|
|
return {
|
|
str(path.relative_to(root)): (path.read_bytes(), _mode(path))
|
|
for path in sorted(root.rglob("*"))
|
|
if path.is_file()
|
|
}
|
|
|
|
|
|
def _fail_second_promotion(monkeypatch, exc):
|
|
"""Fail the second promotion replace with ``exc``; let the rest pass.
|
|
|
|
Backup replaces (whose destination carries the backup suffix) are
|
|
never failed, so the failure lands mid-promotion after one file was
|
|
already promoted -- and the rollback's own staged replaces succeed.
|
|
"""
|
|
real_replace = os.replace
|
|
calls = {"n": 0}
|
|
|
|
def _failing_replace(src, dst):
|
|
if not str(dst).endswith(source._PREPARE_BACKUP_SUFFIX):
|
|
calls["n"] += 1
|
|
if calls["n"] == 2:
|
|
raise exc
|
|
return real_replace(src, dst)
|
|
|
|
monkeypatch.setattr(os, "replace", _failing_replace)
|
|
|
|
|
|
def _assert_tree_restored(root: Path, before: dict[str, tuple[bytes, int]]):
|
|
current = {
|
|
key: value
|
|
for key, value in _snapshot_with_modes(root).items()
|
|
if not key.endswith(source._PREPARE_BACKUP_SUFFIX)
|
|
and source._PREPARE_TMP_SUFFIX not in key
|
|
}
|
|
assert current == before
|
|
|
|
|
|
def test_prepare_rollback_restores_read_only_originals(tmp_path, monkeypatch):
|
|
"""A mid-promotion OSError with 0444 originals restores bytes+modes."""
|
|
root = _make_tree(tmp_path, monkeypatch, mode=0o444)
|
|
before = _snapshot_with_modes(root)
|
|
_fail_second_promotion(monkeypatch, OSError("simulated promotion failure"))
|
|
with pytest.raises(RuntimeError, match="cannot promote GSQ files"):
|
|
source.prepare(root)
|
|
# Every file is back byte- and mode-identical (backups, themselves
|
|
# read-only, are kept for inspection on OSError).
|
|
_assert_tree_restored(root, before)
|
|
assert list(root.rglob(f"*{source._PREPARE_TMP_SUFFIX}")) == []
|
|
backups = sorted(root.rglob(f"*{source._PREPARE_BACKUP_SUFFIX}"))
|
|
assert backups
|
|
# The retry replaces the retained read-only backups atomically and
|
|
# completes: contents land, modes stay 0444, no debris remains.
|
|
assert source.prepare(root)
|
|
assert "trust_remote_code=True" not in (root / "main.py").read_text(
|
|
encoding="utf-8"
|
|
)
|
|
for relative, _, _, _ in source.TEXT_PATCHES:
|
|
assert _mode(root / relative) == 0o444
|
|
assert list(root.rglob(f"*{source._PREPARE_TMP_SUFFIX}")) == []
|
|
assert list(root.rglob(f"*{source._PREPARE_BACKUP_SUFFIX}")) == []
|
|
|
|
|
|
def test_prepare_interrupt_restores_read_only_originals(tmp_path, monkeypatch):
|
|
"""A KeyboardInterrupt mid-promotion with 0444 originals restores all."""
|
|
root = _make_tree(tmp_path, monkeypatch, mode=0o444)
|
|
before = _snapshot_with_modes(root)
|
|
_fail_second_promotion(monkeypatch, KeyboardInterrupt())
|
|
with pytest.raises(KeyboardInterrupt):
|
|
source.prepare(root)
|
|
# Interrupts restore originals and remove temps and backups alike.
|
|
_assert_tree_restored(root, before)
|
|
assert list(root.rglob(f"*{source._PREPARE_TMP_SUFFIX}")) == []
|
|
assert list(root.rglob(f"*{source._PREPARE_BACKUP_SUFFIX}")) == []
|
|
# The retry after the interrupt succeeds on the read-only tree.
|
|
assert source.prepare(root)
|
|
assert "trust_remote_code=True" not in (root / "main.py").read_text(
|
|
encoding="utf-8"
|
|
)
|
|
for relative, _, _, _ in source.TEXT_PATCHES:
|
|
assert _mode(root / relative) == 0o444
|