Files
OBLITERATUS/tests/test_gsq_workflow_edges.py
Joseph MaglyandClaude Opus 5.5 3010f7c2c5 test(gsq): cover fail-closed edges of source, workflow, and coverage (#304)
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>
2026-10-14 16:43:16 -04:00

1016 lines
37 KiB
Python

"""CPU tests for fail-closed edges of the portable GSQ workflow runner.
The end-to-end workflow tests drive the happy paths and the headline
refusals; these pin the remaining guard rails: header parsing of base
safetensors, config-contract refusals, launch-binding and manifest
tampering, post-assembly drift, launch-lock/record I/O failures, and the
pre-spawn source re-verification of ``run`` and ``assemble``. GSQ itself
is never executed: subprocess launches are replaced by fakes, and the
checkout is the fake adapter tree from ``test_gsq_workflow``.
"""
from __future__ import annotations
import copy
import json
import os
import struct
import subprocess
from pathlib import Path
import pytest
import torch
import yaml
from safetensors.torch import save_file
from obliteratus.gsq import source, workflow
from tests.test_gsq_workflow import (
_validate_bound_run,
_write_fake_tree,
_write_run_config,
_write_tiny_run,
)
@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 _rewrite_json(path: Path, mutate) -> None:
payload = json.loads(path.read_text(encoding="utf-8"))
mutate(payload)
path.write_text(json.dumps(payload), encoding="utf-8")
# --- prepare_checkout -----------------------------------------------------
def test_prepare_checkout_rechecks_digest_after_writing(fake_tree, monkeypatch):
"""A plan that matched the record but a result that does not refuses."""
monkeypatch.setattr(
source,
"prospective_prepared_digest",
lambda _root: source.EXPECTED_PREPARED_TREE_SHA256,
)
with pytest.raises(RuntimeError, match="does not match recorded"):
workflow.prepare_checkout(fake_tree)
# --- safetensors header parsing / dtype detection -------------------------
def _raw_safetensors(path: Path, header: bytes, *, declared: int | None = None):
length = len(header) if declared is None else declared
path.write_bytes(struct.pack("<Q", length) + header)
@pytest.mark.parametrize(
("payload", "message"),
[
(b"\x01\x02", "is truncated"),
(struct.pack("<Q", 512 * 1024**2), "implausible header length"),
(struct.pack("<Q", 100) + b"{}", "is truncated"),
(struct.pack("<Q", 2) + b"\xff\xfe", "unreadable header"),
(struct.pack("<Q", 4) + b"nope", "unreadable header"),
(struct.pack("<Q", 2) + b"[]", "must be a JSON object"),
],
)
def test_safetensors_header_fails_closed(tmp_path, payload, message):
path = tmp_path / "w.safetensors"
path.write_bytes(payload)
with pytest.raises(ValueError, match=message):
workflow._read_safetensors_header(path)
def test_safetensors_header_unreadable_path(tmp_path):
with pytest.raises(ValueError, match="cannot read safetensors header"):
workflow._read_safetensors_header(tmp_path)
def test_safetensors_header_drops_metadata(tmp_path):
path = tmp_path / "w.safetensors"
save_file({"a": torch.zeros(2, 2)}, str(path), metadata={"k": "v"})
header = workflow._read_safetensors_header(path)
assert set(header) == {"a"}
assert header["a"]["dtype"] == "F32"
def test_detect_dtype_missing_dir(tmp_path):
with pytest.raises(ValueError, match="cannot list base model dir"):
workflow.detect_base_float_dtype(tmp_path / "missing")
def test_detect_dtype_rejects_malformed_entries(tmp_path):
shard = tmp_path / "model.safetensors"
_raw_safetensors(shard, json.dumps({"w": [1, 2]}).encode())
with pytest.raises(ValueError, match="entry 'w' is malformed"):
workflow.detect_base_float_dtype(tmp_path)
_raw_safetensors(shard, json.dumps({"w": {"shape": [2, 2]}}).encode())
with pytest.raises(ValueError, match="has no dtype tag"):
workflow.detect_base_float_dtype(tmp_path)
def test_detect_dtype_ignores_vectors_and_integer_tensors(tmp_path):
save_file(
{
"norm": torch.zeros(4, dtype=torch.float64),
"ids": torch.zeros((2, 2), dtype=torch.int64),
"w": torch.zeros((2, 2), dtype=torch.float16),
},
str(tmp_path / "model.safetensors"),
)
assert workflow.detect_base_float_dtype(tmp_path) == "float16"
def test_dtype_override_must_be_string():
with pytest.raises(ValueError, match="must be a string"):
workflow._normalize_run_dtype_override(16)
def test_config_logits_dtype_must_match_base(tmp_path):
save_file({"w": torch.zeros((2, 2), dtype=torch.float16)},
str(tmp_path / "model.safetensors"))
config = {
"model": {"dtype": "float16"},
"quantization": {"logits_dtype": "bfloat16"},
}
with pytest.raises(ValueError, match="logits_dtype"):
workflow._require_config_dtype_matches_base(
config, config_path=tmp_path / "g.yaml", base_model_dir=tmp_path
)
for ok in ("float16", "float32"):
config["quantization"]["logits_dtype"] = ok
workflow._require_config_dtype_matches_base(
config, config_path=tmp_path / "g.yaml", base_model_dir=tmp_path
)
def test_render_config_resolves_model_file_to_its_directory(tmp_path):
model_dir = tmp_path / "model"
model_dir.mkdir()
config_json = model_dir / "config.json"
config_json.write_text("{}", encoding="utf-8")
rendered = yaml.safe_load(
workflow.render_gsq_config(
str(config_json), checkpoint_dir=str(tmp_path / "ckpt")
)
)
assert rendered["model"]["name"] == str(model_dir.resolve())
# --- small validators -----------------------------------------------------
@pytest.mark.parametrize("value", [True, "1", 1.0])
def test_world_size_must_be_int(value):
with pytest.raises(ValueError, match="world_size must be an int"):
workflow._require_single_gpu(value)
@pytest.mark.parametrize("value", [True, "4", 4.0])
def test_pack_bits_must_be_int(value):
with pytest.raises(ValueError, match="pack_bits must be an int"):
workflow._require_assembler_pack_bits(value)
@pytest.mark.parametrize(
("progress", "expected"),
[
(None, None),
("not json", None),
([1], None),
({"last_completed_layer": True}, None),
({"last_completed_layer": "3"}, None),
({"last_completed_layer": 3}, 3),
],
)
def test_read_last_completed_layer(tmp_path, progress, expected):
if progress is not None:
text = progress if isinstance(progress, str) else json.dumps(progress)
(tmp_path / "progress.json").write_text(text, encoding="utf-8")
assert workflow._read_last_completed_layer(tmp_path) == expected
def test_run_dtype_itemsize_fails_closed(tmp_path):
cfg = tmp_path / "g.yaml"
with pytest.raises(ValueError, match="expected a dtype name"):
workflow._run_dtype_itemsize(None, config_path=cfg)
with pytest.raises(ValueError, match="unknown item size"):
workflow._run_dtype_itemsize("complex64", config_path=cfg)
assert workflow._run_dtype_itemsize(" BF16 ", config_path=cfg) == 2
def test_activation_hidden_size_sources(tmp_path):
cfg = tmp_path / "g.yaml"
with pytest.raises(ValueError, match="no base model dir or model config"):
workflow._read_activation_hidden_size(
config_path=cfg, base_model_dir=None, model_config_path=None
)
with pytest.raises(ValueError, match="cannot load base model config"):
workflow._read_activation_hidden_size(
config_path=cfg, base_model_dir=tmp_path, model_config_path=None
)
model_config = tmp_path / "m.json"
model_config.write_text("[]", encoding="utf-8")
with pytest.raises(ValueError, match="must be a JSON object"):
workflow._read_activation_hidden_size(
config_path=cfg, base_model_dir=None, model_config_path=model_config
)
model_config.write_text(json.dumps({"hidden_size": 0}), encoding="utf-8")
with pytest.raises(ValueError, match="sets hidden_size to 0"):
workflow._read_activation_hidden_size(
config_path=cfg, base_model_dir=None, model_config_path=model_config
)
model_config.write_text(
json.dumps({"hidden_size": 7, "text_config": {"hidden_size": 64}}),
encoding="utf-8",
)
assert (
workflow._read_activation_hidden_size(
config_path=cfg, base_model_dir=None, model_config_path=model_config
)
== 64
)
def _rendered_config(tmp_path) -> tuple[dict, Path]:
config_path = _write_run_config(tmp_path)
return yaml.safe_load(config_path.read_text(encoding="utf-8")), config_path
@pytest.mark.parametrize(
("section", "key", "value", "message"),
[
("data", "num_samples", -1, "data.num_samples"),
("gptq", "nsamples", "many", "gptq.nsamples"),
("data", "max_length", 0, "data.max_length to 0"),
("training", "act_cache_mmap_threshold_gb", 0, "positive number"),
("training", "act_cache_mmap_threshold_gb", True, "positive number"),
],
)
def test_activation_buffer_sizing_refuses_bad_counts(
tmp_path, section, key, value, message
):
config, config_path = _rendered_config(tmp_path)
config[section][key] = value
with pytest.raises(ValueError, match=message):
workflow._require_ram_resident_activation_buffers(
config, config_path=config_path, base_model_dir=tmp_path / "model"
)
@pytest.mark.parametrize(
("section", "key", "value", "message"),
[
("quantization", "init_method", "rtn", "init_method"),
("quantization", "gsq_enabled", False, "gsq_enabled"),
("gptq", "wbits", 4, "gptq.wbits"),
("gptq", "sym", False, "gptq.sym"),
("gptq", "groupsize", 0, "positive int"),
("quantization", "groupsize", True, "positive int"),
("data", "num_workers", 2, "num_workers"),
("training", "act_cache_mmap_threshold_gb", 2.0, "at least"),
],
)
def test_method_contract_refusals(tmp_path, section, key, value, message):
config, config_path = _rendered_config(tmp_path)
workflow._require_gsq_method_contract(
config, config_path=config_path, group_size=None
)
broken = copy.deepcopy(config)
broken[section][key] = value
with pytest.raises(ValueError, match=message):
workflow._require_gsq_method_contract(
broken, config_path=config_path, group_size=None
)
def test_method_contract_group_sizes(tmp_path):
config, config_path = _rendered_config(tmp_path)
groupsize = config["gptq"]["groupsize"]
with pytest.raises(ValueError, match="validation uses group_size"):
workflow._require_gsq_method_contract(
config, config_path=config_path, group_size=groupsize * 2
)
config["gptq"]["groupsize"] = groupsize * 2
with pytest.raises(ValueError, match="the two must agree"):
workflow._require_gsq_method_contract(
config, config_path=config_path, group_size=None
)
def test_config_settings_require_self_attn(tmp_path):
config, config_path = _rendered_config(tmp_path)
del config["quantization"]["self_attn"]
with pytest.raises(ValueError, match="no quantization.self_attn"):
workflow._check_gsq_config_settings(
config,
config_path=config_path,
quantize_attention=True,
quantize_mlp=True,
group_size=128,
)
def test_load_gsq_yaml_config_fails_closed(tmp_path):
path = tmp_path / "g.yaml"
with pytest.raises(ValueError, match="cannot load GSQ config"):
workflow._load_gsq_yaml_config(path)
path.write_text("a: [unterminated", encoding="utf-8")
with pytest.raises(ValueError, match="cannot load GSQ config"):
workflow._load_gsq_yaml_config(path)
path.write_text("- 1\n", encoding="utf-8")
with pytest.raises(ValueError, match="must be a YAML mapping"):
workflow._load_gsq_yaml_config(path)
def test_require_local_model_dir(tmp_path):
cfg = tmp_path / "g.yaml"
with pytest.raises(ValueError, match="no usable model.name"):
workflow._require_local_model_dir({"model": {}}, config_path=cfg)
with pytest.raises(ValueError, match="not an absolute"):
workflow._require_local_model_dir(
{"model": {"name": "org/model"}}, config_path=cfg
)
with pytest.raises(ValueError, match="no such local"):
workflow._require_local_model_dir(
{"model": {"name": str(tmp_path / "gone")}}, config_path=cfg
)
weights = tmp_path / "model.safetensors"
weights.write_bytes(b"")
assert workflow._require_local_model_dir(
{"model": {"name": str(weights)}}, config_path=cfg
) == str(tmp_path.resolve())
def test_bound_load_dtype_falls_back_to_base_weights(tmp_path):
save_file({"w": torch.zeros((2, 2), dtype=torch.float16)},
str(tmp_path / "model.safetensors"))
for gsq_yaml in ({"model": {}}, {"model": {"dtype": ""}}, {"model": "x"}):
assert (
workflow._bound_validation_load_dtype(
gsq_yaml=gsq_yaml,
base_model_dir=tmp_path,
model_config_path=tmp_path / "config.json",
)
== "float16"
)
def test_has_base_weights_on_missing_dir(tmp_path):
assert workflow._has_base_weights(tmp_path / "missing") is False
def test_fresh_out_dir_rules(tmp_path):
workflow._require_fresh_or_empty_out_dir(tmp_path / "new")
empty = tmp_path / "empty"
empty.mkdir()
workflow._require_fresh_or_empty_out_dir(empty)
as_file = tmp_path / "file"
as_file.write_text("x", encoding="utf-8")
with pytest.raises(ValueError, match="is not a directory"):
workflow._require_fresh_or_empty_out_dir(as_file)
# --- base hashing ---------------------------------------------------------
def test_base_hashes_select_assembler_inputs(tmp_path):
base = tmp_path / "model"
base.mkdir()
save_file({"w": torch.zeros(2, 2)}, str(base / "model.safetensors"))
(base / "config.json").write_text("{}", encoding="utf-8")
(base / "modeling_x.py").write_text("x = 1\n", encoding="utf-8")
(base / "README.md").write_text("ignored", encoding="utf-8")
(base / "nested").mkdir()
hashes = workflow._base_model_file_hashes(base)
assert set(hashes) == {"model.safetensors", "config.json", "modeling_x.py"}
with pytest.raises(ValueError, match="cannot list base model dir"):
workflow._base_model_file_hashes(tmp_path / "missing")
def test_consumed_hashes_require_progress(tmp_path):
run_dir = tmp_path / "run"
_write_tiny_run(run_dir, tmp_path / "model")
hashes = workflow._consumed_file_hashes(run_dir)
assert "progress.json" in hashes
assert all(name.endswith((".safetensors", ".json")) for name in hashes)
(run_dir / "progress.json").unlink()
with pytest.raises(ValueError, match="no progress.json"):
workflow._consumed_file_hashes(run_dir)
# --- launch records -------------------------------------------------------
def _launch_inputs(tmp_path):
base = tmp_path / "model"
base.mkdir()
(base / "config.json").write_text(json.dumps({"hidden_size": 8}),
encoding="utf-8")
config = tmp_path / "gsq.yaml"
config.write_text("model: {}\n", encoding="utf-8")
record = workflow._launch_record(
launch_id="abc",
config_path=config,
base_model_dir=str(base),
run_id=None,
checkpoint_dir=tmp_path,
)
return base, config, record
def test_post_launch_input_drift_detection(tmp_path):
base, config, record = _launch_inputs(tmp_path)
assert workflow._post_launch_input_drift(record, config, str(base)) is None
no_hashes = dict(record, base_model_sha256=None)
assert "no base-model hashes" in workflow._post_launch_input_drift(
no_hashes, config, str(base)
)
(base / "tokenizer.json").write_text("{}", encoding="utf-8")
assert "file set changed" in workflow._post_launch_input_drift(
record, config, str(base)
)
(base / "tokenizer.json").unlink()
(base / "config.json").write_text("{}", encoding="utf-8")
assert "files changed during" in workflow._post_launch_input_drift(
record, config, str(base)
)
assert "unreadable after training" in workflow._post_launch_input_drift(
record, config, str(tmp_path / "gone")
)
config.write_text("model: {x: 1}\n", encoding="utf-8")
assert "changed during training" in workflow._post_launch_input_drift(
record, config, str(base)
)
config.unlink()
assert "unreadable after training" in workflow._post_launch_input_drift(
record, config, str(base)
)
def test_fsync_parent_dir_tolerates_missing_dir_and_fsync_errors(
tmp_path, monkeypatch
):
workflow._fsync_parent_dir(tmp_path / "missing")
closed: list[int] = []
real_close = os.close
def _close(fd):
closed.append(fd)
real_close(fd)
def _fsync(_fd):
raise OSError("fsync unsupported (injected)")
monkeypatch.setattr(workflow.os, "fsync", _fsync)
monkeypatch.setattr(workflow.os, "close", _close)
workflow._fsync_parent_dir(tmp_path)
assert len(closed) == 1
def test_write_json_record_cleans_temp_when_replace_fails(tmp_path, monkeypatch):
dest = tmp_path / "record.json"
def _replace(_src, _dst):
raise OSError("replace failed (injected)")
monkeypatch.setattr(workflow.os, "replace", _replace)
with pytest.raises(OSError, match="replace failed"):
workflow._write_json_record(dest, {"a": 1})
assert list(tmp_path.iterdir()) == []
def test_child_dir_names(tmp_path):
assert workflow._child_dir_names(tmp_path / "missing") == set()
(tmp_path / "run").mkdir()
(tmp_path / "file").write_text("x", encoding="utf-8")
assert workflow._child_dir_names(tmp_path) == {"run"}
with pytest.raises(ValueError, match="cannot list checkpoint directory"):
workflow._child_dir_names(tmp_path / "file")
def test_write_launch_binding_requires_run_dir_and_honors_explicit_values(
tmp_path,
):
base, config, _ = _launch_inputs(tmp_path)
with pytest.raises(ValueError, match="run directory not found"):
workflow.write_launch_binding(
tmp_path / "ckpt" / "missing", config_path=config, base_model_dir=base
)
run = tmp_path / "ckpt" / "run-a"
run.mkdir(parents=True)
(run / "progress.json").write_text(
json.dumps({"last_completed_layer": 9}), encoding="utf-8"
)
path = workflow.write_launch_binding(
run,
config_path=config,
base_model_dir=base,
launch_id="fixed",
last_completed_layer=2,
gsq_revision="deadbeef",
prepared_tree_sha256="0" * 64,
prepared_tree_verified=False,
allow_unrecorded_digest=True,
)
record = json.loads(path.read_text(encoding="utf-8"))
assert record["launch_id"] == "fixed"
assert record["last_completed_layer"] == 2
assert record["gsq_revision"] == "deadbeef"
assert record["prepared_tree_verified"] is False
assert record["allow_unrecorded_digest"] is True
@pytest.fixture
def bound_launch(tmp_path):
base, config, _ = _launch_inputs(tmp_path)
run = (tmp_path / "ckpt" / "run-a").resolve()
run.mkdir(parents=True)
(run / "progress.json").write_text("{}", encoding="utf-8")
workflow.write_launch_binding(run, config_path=config, base_model_dir=base)
return run, config, base
def test_launch_binding_accepts_matching_launch(bound_launch):
run, config, base = bound_launch
binding = workflow._require_launch_binding(run, config, base)
assert binding["run_id"] == "run-a"
@pytest.mark.parametrize(
("mutate", "message"),
[
(lambda b: b.update(schema_version=0), "schema_version"),
(lambda b: b.update(run_id="run-b"), "records run_id"),
(lambda b: b.update(checkpoint_dir="/elsewhere"), "records checkpoint_dir"),
(lambda b: b.update(checkpoint_dir=None), "records checkpoint_dir"),
(lambda b: b.update(base_model_dir="/other"), "launched with base model"),
(lambda b: b.update(base_model_sha256=None), "no base-model hashes"),
(lambda b: b["base_model_sha256"].update(extra="0"), "file set changed"),
(
lambda b: b["base_model_sha256"].update({"config.json": "0"}),
"files changed since launch",
),
],
)
def test_launch_binding_refusals(bound_launch, mutate, message):
run, config, base = bound_launch
_rewrite_json(run / workflow.LAUNCH_FILENAME, mutate)
with pytest.raises(ValueError, match=message):
workflow._require_launch_binding(run, config, base)
def test_launch_binding_refuses_unreadable_or_non_object(bound_launch):
run, config, base = bound_launch
binding = run / workflow.LAUNCH_FILENAME
binding.write_text("{", encoding="utf-8")
with pytest.raises(ValueError, match="is unreadable"):
workflow._require_launch_binding(run, config, base)
binding.write_text("[]", encoding="utf-8")
with pytest.raises(ValueError, match="must be a JSON object"):
workflow._require_launch_binding(run, config, base)
def test_launch_binding_input_failures(bound_launch, tmp_path):
run, config, base = bound_launch
with pytest.raises(ValueError, match="no base model dir"):
workflow._require_launch_binding(run, config, None)
# Duplicate tensors across base shards make the base unhashable.
save_file({"w": torch.zeros(2, 2)}, str(base / "a.safetensors"))
save_file({"w": torch.zeros(2, 2)}, str(base / "b.safetensors"))
with pytest.raises(ValueError, match="changed since launch"):
workflow._require_launch_binding(run, config, base)
config.unlink()
with pytest.raises(ValueError, match="cannot hash GSQ config"):
workflow._require_launch_binding(run, config, base)
def test_launch_lock_and_release_edges(tmp_path):
with pytest.raises(RuntimeError, match="cannot create launch lock"):
workflow._acquire_launch_lock(tmp_path / "missing", "id")
lock = workflow._acquire_launch_lock(tmp_path, "id-1")
assert lock.read_text(encoding="utf-8").endswith(":id-1\n")
with pytest.raises(RuntimeError, match="holds launch lock"):
workflow._acquire_launch_lock(tmp_path, "id-2")
workflow._release_launch_lock(lock)
assert not lock.exists()
# Releasing twice, or a path that cannot be unlinked, never raises.
workflow._release_launch_lock(lock)
stuck = tmp_path / "dir.lock"
stuck.mkdir()
workflow._release_launch_lock(stuck)
assert stuck.is_dir()
def test_run_dir_fresh_requires_progress(tmp_path):
with pytest.raises(RuntimeError, match="cannot stat"):
workflow._require_run_dir_fresh(
tmp_path, launch_start_ns=0, launch_id="id"
)
def test_write_run_binding_link_race_and_failure(tmp_path, monkeypatch):
def _exists(_src, _dst):
raise FileExistsError("raced (injected)")
monkeypatch.setattr(workflow.os, "link", _exists)
with pytest.raises(RuntimeError, match="already holds"):
workflow._write_run_binding(tmp_path, {"a": 1})
assert list(tmp_path.iterdir()) == []
def _fails(_src, _dst):
raise OSError("no hard links (injected)")
monkeypatch.setattr(workflow.os, "link", _fails)
with pytest.raises(OSError, match="no hard links"):
workflow._write_run_binding(tmp_path, {"a": 1})
assert list(tmp_path.iterdir()) == []
def test_unverified_source_record_keeps_unknown_values():
record = {"gsq_revision": "r", "prepared_tree_sha256": "d",
"prepared_tree_verified": True}
updated = workflow._unverified_source_record(record, head=None, digest=None)
assert updated == {"gsq_revision": "r", "prepared_tree_sha256": "d",
"prepared_tree_verified": False}
assert record["prepared_tree_verified"] is True
def test_post_launch_source_record_never_raises(tmp_path, monkeypatch):
pre = {"gsq_revision": source.GSQ_REVISION, "prepared_tree_sha256": "d",
"prepared_tree_verified": True}
def _unreadable(_root):
raise OSError("io error (injected)")
monkeypatch.setattr(source, "tree_digest", _unreadable)
record, drift = workflow._post_launch_source_record(pre, tmp_path,
launch_id="L")
assert "unreadable after launch L" in drift
assert record["prepared_tree_verified"] is False
assert record["gsq_revision"] == source.GSQ_REVISION
monkeypatch.setattr(source, "tree_digest", lambda _root: "d2")
def _repinned(_root, *args):
return "f" * 40
monkeypatch.setattr(source, "_git", _repinned)
record, drift = workflow._post_launch_source_record(pre, tmp_path,
launch_id="L")
assert "revision check failed" in drift
assert record["gsq_revision"] is None
assert record["prepared_tree_sha256"] == "d2"
def test_bind_partial_run_dir_skips_dir_without_progress(tmp_path):
(tmp_path / "new-run").mkdir()
assert (
workflow._bind_partial_run_dir(
tmp_path, set(), {}, 1, launch_start_ns=0, launch_id="L"
)
is None
)
assert not (tmp_path / "new-run" / workflow.LAUNCH_FILENAME).exists()
# --- run_quantization / assemble pre-spawn checks -------------------------
def _git_failing_after(monkeypatch, calls_ok: int) -> list[int]:
seen: list[int] = []
def _git(_root, *args):
seen.append(1)
if len(seen) > calls_ok:
raise subprocess.CalledProcessError(128, ["git"], "gone")
return source.GSQ_REVISION
monkeypatch.setattr(source, "_git", _git)
return seen
def test_run_refuses_when_source_cannot_be_reverified(
fake_tree, tmp_path, monkeypatch
):
config_path = _write_run_config(tmp_path)
_git_failing_after(monkeypatch, calls_ok=1)
launched: list = []
monkeypatch.setattr(
subprocess, "run", lambda *a, **k: launched.append(a[0])
)
with pytest.raises(RuntimeError, match="cannot re-verify the training source"):
workflow.run_quantization(
fake_tree, config_path, allow_unrecorded_digest=True
)
assert launched == []
checkpoint = tmp_path / "checkpoints"
assert not (checkpoint / workflow.LAUNCH_LOCK_FILENAME).exists()
(record_path,) = checkpoint.glob(f"{workflow.LAUNCH_RECORD_PREFIX}*")
record = json.loads(record_path.read_text(encoding="utf-8"))
assert record["prepared_tree_verified"] is False
def _fake_launch(*, progress: bool, returncode: int):
def _run(command, **kwargs):
cmd = list(command)
config_path = Path(cmd[cmd.index("--config") + 1])
checkpoint = Path(
yaml.safe_load(config_path.read_text(encoding="utf-8"))["training"][
"checkpoint_dir"
]
)
run_dir = checkpoint / "run-new"
run_dir.mkdir(parents=True)
if progress:
(run_dir / "progress.json").write_text("{}", encoding="utf-8")
return subprocess.CompletedProcess(command, returncode)
return _run
def test_run_refuses_success_without_progress(fake_tree, tmp_path, monkeypatch):
config_path = _write_run_config(tmp_path)
monkeypatch.setattr(
subprocess, "run", _fake_launch(progress=False, returncode=0)
)
with pytest.raises(RuntimeError, match="without progress.json"):
workflow.run_quantization(
fake_tree, config_path, allow_unrecorded_digest=True
)
run_dir = tmp_path / "checkpoints" / "run-new"
assert not (run_dir / workflow.LAUNCH_FILENAME).exists()
assert not (tmp_path / "checkpoints" / workflow.LAUNCH_LOCK_FILENAME).exists()
def test_run_failure_without_progress_returns_code_unbound(
fake_tree, tmp_path, monkeypatch
):
config_path = _write_run_config(tmp_path)
monkeypatch.setattr(
subprocess, "run", _fake_launch(progress=False, returncode=3)
)
assert (
workflow.run_quantization(
fake_tree, config_path, allow_unrecorded_digest=True
)
== 3
)
assert not (
tmp_path / "checkpoints" / "run-new" / workflow.LAUNCH_FILENAME
).exists()
def test_assemble_refuses_when_source_cannot_be_reverified(
fake_tree, tmp_path, monkeypatch
):
_validate_bound_run(tmp_path)
config_path = tmp_path / "gsq.yaml"
_git_failing_after(monkeypatch, calls_ok=1)
launched: list = []
monkeypatch.setattr(
subprocess, "run", lambda *a, **k: launched.append(a[0])
)
with pytest.raises(RuntimeError, match="cannot re-verify"):
workflow.assemble_checkpoint(
fake_tree,
config_path,
run_id="run-a",
python="py",
allow_unrecorded_digest=True,
)
assert launched == []
# --- manifest binding (_require_validated_manifest) -----------------------
@pytest.fixture
def validated(tmp_path):
run_dir, config_path, model_config = _validate_bound_run(tmp_path)
return run_dir.resolve(), config_path.resolve(), model_config.resolve()
def _manifest_path(run_dir: Path) -> Path:
return run_dir / workflow.MANIFEST_FILENAME
def test_validated_manifest_accepts_bound_run(validated):
run_dir, config_path, _ = validated
manifest = workflow._require_validated_manifest(run_dir, config_path)
assert manifest["run_id"] == run_dir.name
@pytest.mark.parametrize(
("mutate", "message"),
[
(lambda m: m.update(schema_version=1), "schema_version"),
(lambda m: m.update(run_id="other"), "records run_id"),
(lambda m: m.update(checkpoint_dir="/elsewhere"), "records checkpoint_dir"),
(lambda m: m.update(validation=None), "no validation settings"),
(lambda m: m["validation"].pop("pack_bits"), "missing validation setting"),
(lambda m: m.update(shard_sha256=None), "no shard hashes"),
(lambda m: m.update(base_model_sha256=None), "no base-model hashes"),
(lambda m: m["base_model_sha256"].pop("config.json"), "file set changed"),
(
lambda m: m["base_model_sha256"].update({"config.json": "0"}),
"files changed since validation",
),
(lambda m: m.update(coverage=None), "does not record coverage"),
],
)
def test_validated_manifest_refusals(validated, mutate, message):
run_dir, config_path, _ = validated
_rewrite_json(_manifest_path(run_dir), mutate)
with pytest.raises(ValueError, match=message):
workflow._require_validated_manifest(run_dir, config_path)
def test_validated_manifest_unreadable_or_non_object(validated):
run_dir, config_path, _ = validated
_manifest_path(run_dir).write_text("{", encoding="utf-8")
with pytest.raises(ValueError, match="is unreadable"):
workflow._require_validated_manifest(run_dir, config_path)
_manifest_path(run_dir).write_text("[]", encoding="utf-8")
with pytest.raises(ValueError, match="must be a JSON object"):
workflow._require_validated_manifest(run_dir, config_path)
def test_validated_manifest_missing_config_and_progress(validated):
run_dir, config_path, _ = validated
(run_dir / "progress.json").unlink()
with pytest.raises(ValueError, match="changed since validation"):
workflow._require_validated_manifest(run_dir, config_path)
config_path.unlink()
with pytest.raises(ValueError, match="cannot hash GSQ config"):
workflow._require_validated_manifest(run_dir, config_path)
def test_validated_manifest_refuses_unhashable_base(validated):
run_dir, config_path, model_config = validated
tensors = {"dup.weight": torch.zeros((2, 2), dtype=torch.float32)}
save_file(tensors, str(model_config.parent / "extra-a.safetensors"))
save_file(tensors, str(model_config.parent / "extra-b.safetensors"))
with pytest.raises(ValueError, match="changed since validation"):
workflow._require_validated_manifest(run_dir, config_path)
def test_validated_manifest_partial_records(validated):
run_dir, config_path, _ = validated
path = _manifest_path(run_dir)
def _partial(layers, packed):
def _mutate(m):
m["validation"]["allow_partial"] = True
m["validation"]["partial_layers"] = layers
m["coverage"]["packed_modules"] = packed
return _mutate
_rewrite_json(path, _partial([], 1))
with pytest.raises(ValueError, match="no partial layer set"):
workflow._require_validated_manifest(
run_dir, config_path, allow_partial=True
)
_rewrite_json(path, _partial([0], "seven"))
with pytest.raises(ValueError, match="no packed coverage"):
workflow._require_validated_manifest(
run_dir, config_path, allow_partial=True
)
_rewrite_json(path, _partial([0], 0))
with pytest.raises(ValueError, match="no packed modules"):
workflow._require_validated_manifest(
run_dir, config_path, allow_partial=True
)
_rewrite_json(path, _partial([0], 3))
manifest = workflow._require_validated_manifest(
run_dir, config_path, allow_partial=True
)
assert manifest["validation"]["partial_layers"] == [0]
# --- post-assembly drift --------------------------------------------------
def _drift_args(validated):
run_dir, config_path, model_config = validated
manifest = json.loads(_manifest_path(run_dir).read_text(encoding="utf-8"))
sha = workflow._sha256_file(model_config)
return run_dir, config_path, manifest, sha
def test_post_assemble_drift_accepts_unchanged_inputs(validated):
run_dir, config_path, manifest, sha = _drift_args(validated)
workflow._require_no_post_assemble_drift(
run_dir, config_path, manifest, model_config_sha256=sha
)
# Without a recorded base dir, the model config directory is used.
manifest["base_model_dir"] = None
workflow._require_no_post_assemble_drift(
run_dir, config_path, manifest, model_config_sha256=sha
)
@pytest.mark.parametrize(
("mutate", "message"),
[
(lambda m: m.update(shard_sha256=None), "records no shard hashes"),
(lambda m: m["shard_sha256"].pop("progress.json"), "shard set changed"),
(lambda m: m.update(base_model_sha256=None), "records no hashes"),
(lambda m: m["base_model_sha256"].pop("config.json"), "file set changed"),
(
lambda m: m["base_model_sha256"].update({"config.json": "0"}),
"files changed during assembly",
),
(
lambda m: m.update(model_config_path="/nonexistent/config.json"),
"cannot hash model config",
),
],
)
def test_post_assemble_drift_refusals(validated, mutate, message):
run_dir, config_path, manifest, sha = _drift_args(validated)
mutate(manifest)
with pytest.raises(ValueError, match=message):
workflow._require_no_post_assemble_drift(
run_dir, config_path, manifest, model_config_sha256=sha
)
def test_post_assemble_drift_detects_changed_files(validated):
run_dir, config_path, manifest, sha = _drift_args(validated)
with pytest.raises(ValueError, match="model config .* changed during"):
workflow._require_no_post_assemble_drift(
run_dir, config_path, manifest, model_config_sha256="0" * 64
)
model_dir = Path(manifest["model_config_path"]).parent
tensors = {"dup.weight": torch.zeros((2, 2), dtype=torch.float32)}
save_file(tensors, str(model_dir / "extra-a.safetensors"))
save_file(tensors, str(model_dir / "extra-b.safetensors"))
with pytest.raises(ValueError, match="changed during assembly"):
workflow._require_no_post_assemble_drift(
run_dir, config_path, manifest, model_config_sha256=sha
)
(run_dir / "progress.json").unlink()
with pytest.raises(ValueError, match="changed during assembly"):
workflow._require_no_post_assemble_drift(
run_dir, config_path, manifest, model_config_sha256=sha
)
config_path.unlink()
with pytest.raises(ValueError, match="cannot hash GSQ config"):
workflow._require_no_post_assemble_drift(
run_dir, config_path, manifest, model_config_sha256=sha
)
# --- interrupted-run subset -----------------------------------------------
def test_interrupted_run_subset_edges(tmp_path):
run_dir = tmp_path / "run"
_write_tiny_run(run_dir, tmp_path / "model")
expected = ("model.layers.5.mlp.down_proj",)
with pytest.raises(ValueError, match="no expected projections"):
workflow._interrupted_run_subset(run_dir, expected, 6, 5)
(run_dir / "progress.json").write_text("[]", encoding="utf-8")
assert workflow._interrupted_run_subset(run_dir, expected, 6, 5) is None
(run_dir / "progress.json").unlink()
assert workflow._interrupted_run_subset(run_dir, expected, 6, 5) is None
# --- CLI ------------------------------------------------------------------
def test_cli_fetch_reports_destination(tmp_path, monkeypatch, capsys):
monkeypatch.setattr(workflow, "fetch", lambda checkout: Path(checkout))
assert workflow.main(["fetch", str(tmp_path / "GSQ")]) == 0
out = capsys.readouterr().out
assert f"fetched {source.GSQ_REVISION} into {tmp_path / 'GSQ'}" in out
def test_cli_validate_allow_partial_reports_layers(tmp_path, capsys):
run_dir = tmp_path / "run"
model_config, expected = _write_tiny_run(run_dir, tmp_path / "model")
assert (
workflow.main(
[
"validate",
"--model-config",
str(model_config),
"--checkpoint-dir",
str(run_dir),
"--no-manifest",
"--allow-partial",
]
)
== 0
)
out = capsys.readouterr().out
assert "partial coverage recorded" in out
assert f"{len(expected)}/{len(expected)} packed, layers [0]" in out