Skip to content

Commit e95e372

Browse files
committed
report the checkpoint that scored, fail an empty slice
1 parent 3fa8719 commit e95e372

3 files changed

Lines changed: 130 additions & 36 deletions

File tree

‎sdk/benchmarks/attacks/ci_gate.py‎

Lines changed: 48 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -121,24 +121,35 @@
121121
MODEL_TIER = "tiny"
122122

123123

124-
def _resolve_weights() -> str | None:
125-
"""Return the checkpoint path when weights are on disk, else None."""
124+
def _weights_on_disk() -> bool:
125+
"""Whether a checkpoint with weights exists anywhere the resolver looks."""
126126
try:
127127
from unplug.ml.validation import resolve_validation_checkpoint
128128
except ImportError:
129-
return None
129+
return False
130130
try:
131-
path = resolve_validation_checkpoint(require_weights=True)
131+
return resolve_validation_checkpoint(require_weights=True) is not None
132132
except Exception:
133-
return None
134-
return str(path) if path else None
133+
return False
134+
135135

136+
def _release_guard() -> Guard | None:
137+
"""A guard configured the way a release runs, or None if it cannot load.
136138
137-
def _guard_factory(weights: str | None) -> Callable[[], Guard] | None:
138-
"""Build guards the way a release runs, or None to take evaluate's default."""
139-
if weights is None:
139+
Returned rather than described, because the checkpoint the report names has
140+
to be the checkpoint that scores. The resolver reads UNPLUG_TEST_CHECKPOINT
141+
before UNPLUG_MODEL_PATH before the cache, and Guard resolves separately, so
142+
two lookups can disagree and the run would name one checkpoint while
143+
measuring another. Reading it back off the loaded guard removes the second
144+
lookup.
145+
"""
146+
if not _weights_on_disk():
147+
return None
148+
try:
149+
guard = Guard(model=MODEL_TIER, require_ml=True)
150+
except Exception:
140151
return None
141-
return lambda: Guard(model=MODEL_TIER, require_ml=True)
152+
return guard if guard.ml_model_loaded else None
142153

143154

144155
def run_gate(threshold: float = 0.5, *, require_weights: bool = False) -> tuple[bool, dict]:
@@ -147,33 +158,39 @@ def run_gate(threshold: float = 0.5, *, require_weights: bool = False) -> tuple[
147158

148159
# Resolved once: every benign slice below must be measured under the same
149160
# configuration, or the numbers in one run are not comparable to each other.
150-
weights = _resolve_weights()
151-
factory = _guard_factory(weights)
161+
# One configuration for the whole run, so the report names the checkpoint
162+
# that actually scored and every check below is measured the same way.
163+
shared = _release_guard()
164+
factory: Callable[[], Guard] | None = (
165+
None if shared is None else (lambda: Guard(model=MODEL_TIER, require_ml=True))
166+
)
152167
# Every slice is scored with isolated_requests: one guard per slice, a fresh
153168
# ExecutionContext per sample. The default rebuilds the guard per sample,
154169
# which reloads the checkpoint 95 times and turns a 6 second slice into 27.
155170
# Verified to give identical counts on all four slices.
156171
report["model"] = {
157172
"tier": MODEL_TIER,
158-
"weights_present": weights is not None,
159-
"checkpoint": weights,
160-
"measured_with": "ml" if weights else "regex_only",
173+
"weights_present": shared is not None,
174+
"checkpoint": None if shared is None else shared.active_checkpoint,
175+
"measured_with": "ml" if shared is not None else "regex_only",
161176
"required": require_weights,
162177
}
163-
if require_weights and weights is None:
178+
if require_weights and shared is None:
164179
passed = False
165180

166-
matrix = run_matrix(threshold=threshold)
181+
# The matrix builds a fresh guard per scan by design, so it takes the factory
182+
# rather than the shared instance. Without it the matrix ran on a default
183+
# Guard() while the report above said the run was measured with the model,
184+
# which was true of the slices and not of the matrix.
185+
matrix = run_matrix(threshold=threshold, guard_factory=factory)
167186
report["converter_matrix"] = matrix.to_dict()
168187
if not matrix.passed:
169188
passed = False
170189

171190
corpus_report: dict = {"floors": {}, "shortfalls": []}
172191
if GARAK_CORPUS.exists():
173192
samples = load_jsonl(GARAK_CORPUS)
174-
result = evaluate(
175-
samples, threshold=threshold, guard_factory=factory, isolated_requests=True
176-
)
193+
result = evaluate(samples, threshold=threshold, guard=shared, isolated_requests=True)
177194
for category, floor in GARAK_RECALL_FLOORS.items():
178195
metrics = result.by_category.get(category)
179196
recall = metrics.recall if metrics else 0.0
@@ -204,9 +221,7 @@ def run_gate(threshold: float = 0.5, *, require_weights: bool = False) -> tuple[
204221
# there: a corpus with only hard rows, and a labelling change that moves
205222
# the easy rows off label=0.
206223
easy_missing = not easy
207-
easy_result = evaluate(
208-
easy, threshold=threshold, guard_factory=factory, isolated_requests=True
209-
)
224+
easy_result = evaluate(easy, threshold=threshold, guard=shared, isolated_requests=True)
210225
easy_fpr = easy_result.overall.false_positive_rate
211226
easy_ok = (easy_fpr <= EASY_FPR_CEILING) if easy else False
212227

@@ -216,7 +231,7 @@ def run_gate(threshold: float = 0.5, *, require_weights: bool = False) -> tuple[
216231
# target was met while it was at it.
217232
hard_missing = not hard
218233
hard_result = (
219-
evaluate(hard, threshold=threshold, guard_factory=factory, isolated_requests=True)
234+
evaluate(hard, threshold=threshold, guard=shared, isolated_requests=True)
220235
if hard
221236
else None
222237
)
@@ -263,14 +278,12 @@ def run_gate(threshold: float = 0.5, *, require_weights: bool = False) -> tuple[
263278
carrier_samples = [s for s in load_jsonl(CARRIER_CORPUS) if s.label == 0]
264279
carrier_missing = not carrier_samples
265280
carrier_result = (
266-
evaluate(
267-
carrier_samples, threshold=threshold, guard_factory=factory, isolated_requests=True
268-
)
281+
evaluate(carrier_samples, threshold=threshold, guard=shared, isolated_requests=True)
269282
if carrier_samples
270283
else None
271284
)
272285
carrier_fpr = carrier_result.overall.false_positive_rate if carrier_result else 0.0
273-
measurable = carrier_result is not None and weights is not None
286+
measurable = carrier_result is not None and shared is not None
274287
carrier_ok = (carrier_fpr <= CARRIER_FPR_RATCHET) if measurable else False
275288
carrier_report = {
276289
"samples": len(carrier_samples),
@@ -288,9 +301,13 @@ def run_gate(threshold: float = 0.5, *, require_weights: bool = False) -> tuple[
288301
"meets_target": measurable and carrier_fpr <= CARRIER_FPR_TARGET,
289302
"to_target": round(max(0.0, carrier_fpr - CARRIER_FPR_TARGET), 4),
290303
}
291-
# An unmeasurable slice does not fail the CI run, which has no weights by
292-
# design. It fails --require-weights, which is where a release asks.
293-
if measurable and not carrier_ok:
304+
# Three different states, and collapsing them is how a gate goes green
305+
# while measuring nothing. A corpus with no usable rows always fails:
306+
# someone emptied the file and no configuration excuses that. A slice
307+
# that could not be scored for want of weights does not fail the default
308+
# run, because CI has none by design, but it does fail
309+
# --require-weights. A scored slice fails on the ratchet.
310+
if carrier_missing or (measurable and not carrier_ok):
294311
passed = False
295312
if require_weights and not measurable:
296313
passed = False

‎sdk/src/unplug/guard.py‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -762,6 +762,22 @@ def is_server_mode(self) -> bool:
762762
def ml_model_loaded(self) -> bool:
763763
return self._ml_provider is not None and self._ml_provider.loaded
764764

765+
@property
766+
def active_checkpoint(self) -> str | None:
767+
"""Filesystem path of the checkpoint this Guard actually loaded.
768+
769+
``None`` when no ML provider is active. Resolution has several inputs
770+
(UNPLUG_TEST_CHECKPOINT, UNPLUG_MODEL_PATH, the model cache, the
771+
validation manifest), so a caller that reports a measurement cannot
772+
infer which one won. Asking the Guard is the only way to name the
773+
checkpoint a number came from.
774+
"""
775+
if self._ml_provider is None:
776+
return None
777+
spec = getattr(self._ml_provider, "spec", None)
778+
path = getattr(spec, "path", None)
779+
return str(path) if path else None
780+
765781
@property
766782
def ml_degraded(self) -> bool:
767783
"""True when active_model is configured but ML failed to load."""

‎sdk/tests/security/test_attack_harness.py‎

Lines changed: 66 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def regex_only(monkeypatch: pytest.MonkeyPatch) -> None:
4949
and CI measure different things from the same call. Tests say which one
5050
they mean rather than depending on the machine.
5151
"""
52-
monkeypatch.setattr(ci_gate, "_resolve_weights", lambda: None)
52+
monkeypatch.setattr(ci_gate, "_release_guard", lambda: None)
5353

5454

5555
@pytest.fixture
@@ -60,10 +60,12 @@ def with_weights() -> str:
6060
UNPLUG_MODEL_CACHE at an empty directory for every test by default, so
6161
without the marker this skips even on a machine that has the weights.
6262
"""
63-
path = ci_gate._resolve_weights()
64-
if path is None:
63+
guard = ci_gate._release_guard()
64+
if guard is None:
6565
pytest.skip("no tiny checkpoint on disk")
66-
return path
66+
checkpoint = guard.active_checkpoint
67+
assert checkpoint is not None
68+
return checkpoint
6769

6870

6971
class TestGarakCorpus:
@@ -219,6 +221,55 @@ def test_slice_is_not_scored_without_weights(self, regex_only: None) -> None:
219221
assert not carrier["measurable"]
220222
assert not carrier["ok"]
221223

224+
def test_an_empty_carrier_corpus_fails_the_gate(
225+
self, regex_only: None, monkeypatch: pytest.MonkeyPatch, tmp_path: Path
226+
) -> None:
227+
"""A corpus with no usable rows fails whatever the configuration.
228+
229+
The first version only failed this under --require-weights, so the
230+
default run printed FAIL for the slice and exited 0. Emptying the file
231+
is not something a configuration excuses, and a report that disagrees
232+
with the exit code is worse than either alone.
233+
"""
234+
empty = tmp_path / "empty.jsonl"
235+
empty.write_text("", encoding="utf-8")
236+
monkeypatch.setattr(ci_gate, "CARRIER_CORPUS", empty)
237+
passed, report = run_gate()
238+
assert not passed
239+
assert report["carrier_benign_fpr"]["no_rows"]
240+
241+
def test_a_corpus_of_only_attack_rows_fails_the_gate(
242+
self, regex_only: None, monkeypatch: pytest.MonkeyPatch, tmp_path: Path
243+
) -> None:
244+
# The slice filters to label == 0, so a mislabelled corpus leaves it
245+
# with nothing to measure by a different route than an empty file.
246+
import json
247+
248+
mislabelled = tmp_path / "attacks.jsonl"
249+
mislabelled.write_text(
250+
json.dumps({"text": "x", "label": 1, "category": "injection"}) + "\n",
251+
encoding="utf-8",
252+
)
253+
monkeypatch.setattr(ci_gate, "CARRIER_CORPUS", mislabelled)
254+
passed, report = run_gate()
255+
assert not passed
256+
assert report["carrier_benign_fpr"]["no_rows"]
257+
258+
@pytest.mark.requires_ml_weights
259+
@pytest.mark.real_model_cache
260+
def test_report_names_the_checkpoint_the_guard_loaded(self, with_weights: str) -> None:
261+
"""The reported path must come from the guard that scored, not a second lookup.
262+
263+
_weights_on_disk reads UNPLUG_TEST_CHECKPOINT before UNPLUG_MODEL_PATH
264+
before the cache, and Guard resolves on its own, so describing the
265+
checkpoint instead of asking the loaded guard let the two disagree.
266+
"""
267+
from unplug import Guard
268+
269+
_, report = run_gate()
270+
assert report["model"]["checkpoint"] == with_weights
271+
assert Guard(model=ci_gate.MODEL_TIER, require_ml=True).active_checkpoint == with_weights
272+
222273
def test_require_weights_fails_when_the_checkpoint_is_absent(self, regex_only: None) -> None:
223274
passed, report = run_gate(require_weights=True)
224275
assert not passed
@@ -315,8 +366,18 @@ def factory() -> Guard:
315366
encoded, guard_factory=factory, isolated_requests=True
316367
).overall.false_positive_rate
317368

318-
assert wrapped_fpr > bare_fpr * 5, (bare_fpr, wrapped_fpr)
369+
# The invariant, and the only rate this asserts: encoding the payload
370+
# does not change the result. That is what makes this corpus the
371+
# document shape rather than the encoded-benign slice #189 asked for,
372+
# and if it stops holding then the premise behind the corpus needs
373+
# revisiting, which is worth a failing test.
319374
assert encoded_fpr == wrapped_fpr, (wrapped_fpr, encoded_fpr)
375+
# An earlier version also required wrapped to score at least five times
376+
# worse than bare. That would have failed the day someone fixed the span
377+
# head: a test that goes red when the bug goes away is a test arguing
378+
# for the bug. The current rate is pinned by CARRIER_FPR_RATCHET, and
379+
# lowering it is how an improvement lands.
380+
assert wrapped_fpr >= bare_fpr, (bare_fpr, wrapped_fpr)
320381

321382
def test_committed_corpus_is_reproducible_from_the_template(self) -> None:
322383
# A generated corpus nobody can regenerate is a corpus nobody can trust

0 commit comments

Comments
 (0)