File size: 2,258 Bytes
fd5366a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# Copyright (c) 2026 CLSA contributors
# SPDX-License-Identifier: MIT
"""Trained artefacts: presence, shapes, calibration contract.

Skipped (not failed) when the artefacts have not been produced yet: a missing
artefact is a missing run, and the suite must say so rather than pass silently.
"""
from __future__ import annotations

import sys
from pathlib import Path

sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "src"))

import numpy as np  # noqa: E402
import pytest  # noqa: E402

from clsa.transcoder.dataset import DERIVED  # noqa: E402
from clsa.util import load_config  # noqa: E402


@pytest.fixture(scope="module")
def artefacts():
    name = load_config("transcoder")["backbones"]["primary"]
    d = DERIVED / name.replace("/", "__")
    if not (d / "artifacts.json").exists():
        pytest.skip(f"no trained artefacts at {d}; run `make train`")
    import json

    return d, json.loads((d / "artifacts.json").read_text())


def test_artefact_files_exist(artefacts):
    d, _ = artefacts
    for f in ("sae.pt", "model.pt", "probe_dense.joblib", "probe_sparse.joblib"):
        assert (d / f).exists(), f"missing {f}"


def test_thresholds_are_probabilities(artefacts):
    _, blob = artefacts
    thr = blob["thresholds"]
    assert thr, "no thresholds recorded"
    for k, v in thr.items():
        assert 0.0 <= float(v) <= 1.0, f"threshold {k}={v} out of range"


def test_admission_report_has_three_arms(artefacts):
    _, blob = artefacts
    adm = blob.get("admission", {})
    assert adm, "no admission report"
    assert set(adm) >= {"shared", "probe_only", "random"} or any(
        "shared" in str(k) for k in adm
    ), f"unexpected admission keys: {list(adm)}"


def test_probe_scores_are_bounded():
    from clsa.world_model.train import load_model, probe_predict

    name = load_config("transcoder")["backbones"]["primary"]
    try:
        _wm, _rh, _blob, ds = load_model(name)
    except FileNotFoundError:
        pytest.skip("no trained world model")
    import joblib

    p = joblib.load(DERIVED / name.replace("/", "__") / "probe_dense.joblib")
    X = np.concatenate([ds["dense"], ds["action"]], axis=1)[:50]
    s = probe_predict(p, X)
    assert s.shape == (50,)
    assert np.all((s >= 0) & (s <= 1))