Spaces:
Running
Running
File size: 10,026 Bytes
fcc9c2f 6354eae fcc9c2f 87e7318 fcc9c2f | 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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 | """Every recorded failure, replayed, so none of them can happen twice.
`dee/data/golden/*.json` holds real incidents β things Turing did to a real
user that were wrong. The mining rollup (dee/core/learn_signal.py) is what
finds them; this is what makes finding them once sufficient. Adding a newly
observed failure is a JSON file, not a new test, which is the only version of
this that survives contact with a busy week.
The model is scripted, so these do not test tool CHOICE β that needs a live
call and is non-deterministic. They test everything downstream of the choice,
which is where the harm actually landed every time: a fabricated sequence
reaching the user, a tool that should have run not running, the engine
treating "described it" as "did it".
Plus one static assertion that does move the live model: the descriptions of
the two tools users confuse must actually disambiguate. The pCAMBIA mis-pick
was fixed by what `fetch_sequence` says about vectors, not by anything at
runtime.
"""
import json
import time
import pytest
from dee.core import agent_tools as _tools
from dee.core import golden
from dee.core import llm as _llm
from dee.core import orchestrator as orch
SCENARIOS = golden.load_all()
@pytest.fixture(autouse=True)
def _configured(monkeypatch):
monkeypatch.setenv("OPENROUTER_API_KEY", "test-key")
monkeypatch.setenv("AGENT_MAX_COST_USD", "0.5")
def _run_to_rest(run, timeout=5.0):
deadline = time.time() + timeout
while time.time() < deadline:
if run.status in ("done", "error", "awaiting_input", "stopped"):
return run.status
time.sleep(0.01)
raise AssertionError(f"run never settled (status={run.status})")
def _replay(scenario, monkeypatch):
"""Drive a real run with the scenario's scripted model and tool results."""
msgs = golden.to_messages(scenario)
seq = list(msgs)
def fake_call(config, body):
# After the script runs out the model just stops talking, which is
# what a real final turn looks like.
return (seq.pop(0) if seq else {"content": "", "tool_calls": []}), 0.001, 100, 0
monkeypatch.setattr(_llm, "call", fake_call)
called = []
results = scenario.get("tool_results") or {}
def fake_exec(name, args, anon, **kw):
called.append((name, args))
return results.get(name, {"ok": True})
monkeypatch.setattr(orch._tools, "execute_tool", fake_exec)
run = orch.create_run(owner="golden-user", anonymous=False)
if scenario.get("workspace"):
run.workspace = scenario["workspace"]
orch.start(run, scenario["user"])
_run_to_rest(run)
# A gated tool (edit_sequence, log_outcome, blast_sequence) parks the run
# on a confirm card instead of executing. That is correct and is itself
# one of the protections β but a scenario about what happens AFTER the
# user approves has to actually approve, or it silently tests the gate
# and nothing else.
if scenario.get("approve") and run.status == "awaiting_input":
orch.reply(run, "yes")
_run_to_rest(run)
return run, [n for n, _a in called]
def _final_text(run):
return "\n".join(e.get("text") or "" for e in run.events if e["kind"] == "text")
def _ids(scenarios):
return [s["name"] for s in scenarios]
# --------------------------------------------------------------------------- #
# the corpus itself
# --------------------------------------------------------------------------- #
def test_there_are_scenarios_and_they_are_well_formed():
"""A validation error is raised, not skipped: an unloadable fixture reads
as coverage while asserting nothing."""
assert SCENARIOS, "no golden scenarios on disk"
for s in SCENARIOS:
golden.validate(s, source=s["name"])
assert s["incident"].strip(), f"{s['name']}: no incident recorded"
def test_a_scenario_that_asserts_nothing_is_rejected():
"""The failure mode this format is most prone to: a note pretending to be
a test."""
with pytest.raises(golden.GoldenError, match="asserts nothing"):
golden.validate({"name": "x", "incident": "i", "first_seen": "2026-01-01",
"user": "u", "model_script": [{"text": "hi"}]})
def test_a_typo_in_an_expectation_is_an_error_not_a_silent_pass():
with pytest.raises(golden.GoldenError, match="unknown expect"):
golden.validate({"name": "x", "incident": "i", "first_seen": "2026-01-01",
"user": "u", "model_script": [{"text": "hi"}],
"expect": {"must_kall": ["a"]}})
# --------------------------------------------------------------------------- #
# replay
# --------------------------------------------------------------------------- #
@pytest.mark.parametrize("scenario", SCENARIOS, ids=_ids(SCENARIOS))
def test_recorded_incident_does_not_recur(scenario, monkeypatch):
run, called = _replay(scenario, monkeypatch)
expect = scenario["expect"]
text = _final_text(run)
why = scenario["incident"][:120]
for name in expect.get("must_call", []):
assert name in called, f"{scenario['name']}: {name} never ran β {why}"
for name in expect.get("must_not_call", []):
assert name not in called, f"{scenario['name']}: {name} ran β {why}"
for name in expect.get("must_confirm", []):
confirms = [e for e in run.events if e["kind"] == "confirm"]
assert any(name in json.dumps(e) for e in confirms), (
f"{scenario['name']}: {name} ran without stopping to ask β {why}")
for needle in expect.get("final_text_contains", []):
assert needle in text, f"{scenario['name']}: missing {needle!r} β {why}"
for needle in expect.get("final_text_lacks", []):
assert needle not in text, \
f"{scenario['name']}: leaked {needle!r} to the user β {why}"
if expect.get("status"):
assert run.status == expect["status"]
if expect.get("no_unsourced_sequence"):
from dee.core import provenance as prov
pool = orch._sourced_pool(run)
leaked = prov.unsourced(text, pool)
assert not leaked, (
f"{scenario['name']}: {len(leaked)} sequence(s) reached the user "
f"without coming from a tool result, the user, or the bench β {why}")
@pytest.mark.parametrize("scenario", SCENARIOS, ids=_ids(SCENARIOS))
def test_tool_descriptions_still_disambiguate(scenario):
"""Static, and the one assertion that actually steers the live model.
Runtime guards stop the harm; the description is what stops the wrong
call being made in the first place.
"""
wanted = (scenario["expect"].get("tool_descriptions_say") or {})
if not wanted:
pytest.skip("scenario makes no description claim")
specs = {s["function"]["name"]: s["function"]["description"]
for s in orch.TOOL_SPECS}
for tool, needles in wanted.items():
assert tool in specs, f"{tool} is not a registered tool"
desc = specs[tool].lower()
for needle in needles:
assert needle.lower() in desc, (
f"{tool}'s description no longer mentions {needle!r}; "
f"the {scenario['name']} incident becomes possible again")
# --------------------------------------------------------------------------- #
# the guard the scenarios lean on
# --------------------------------------------------------------------------- #
def test_the_fabrication_scenario_actually_exercises_the_guard(monkeypatch):
"""Guards against the worst outcome here: the scenario passing because
the model was scripted to behave, rather than because the engine caught
it. This one scripts a fabrication and asserts it was CAUGHT."""
scenario = next(s for s in SCENARIOS if s["name"] == "pcambia-fabricated-sequence")
run, _called = _replay(scenario, monkeypatch)
events = [e for e in run.events if e["kind"] == "provenance"]
assert events, "the guard never fired β this scenario proves nothing"
assert events[0]["withheld"], "fired but withheld nothing"
def test_a_fabrication_sends_the_run_back_to_fetch_it_exactly_once(monkeypatch):
"""Ending on a redacted reply leaves the user holding a hole. So the run
gets one more turn to go and look the sequence up β one, not a loop: if
the model writes an unsourced sequence twice, the redaction stands and
the run ends rather than burning steps."""
fake = "GGGGCCCCGGGGCCCCGGGGCCCCGGGGCCCCTTTTAAAA"
scenario = {
"name": "retry", "incident": "n/a", "first_seen": "2026-08-05",
"user": "what is pUC19",
"model_script": [{"text": f"It is {fake}"},
{"text": f"Actually it is {fake}"}],
"expect": {"no_unsourced_sequence": True},
}
run, _ = _replay(scenario, monkeypatch)
assert run.provenance_retried is True
# Fired on both replies, and the run still terminated.
assert len([e for e in run.events if e["kind"] == "provenance"]) == 2
assert run.status == "done"
assert fake not in _final_text(run)
def test_a_sourced_sequence_is_left_alone(monkeypatch):
"""The other half. A guard that redacts real retrieved sequences would be
turned off within a day, and then nothing is protected."""
real = ("ATGACCATGATTACGCCAAGCTTGCATGCCTGCAGGTCGACTCTAGAGG"
"ATCCCCGGGTACCGAGCTCGAATTCACTGGC")
scenario = {
"name": "sourced", "incident": "n/a", "first_seen": "2026-08-05",
"user": "get pUC19",
"tool_results": {"lookup_vector": {"ok": True, "sequence": real}},
"model_script": [
{"tool": "lookup_vector", "args": {"name": "pUC19"}},
{"text": f"Here is the region you asked for: {real}"},
],
"expect": {"no_unsourced_sequence": True},
}
run, _ = _replay(scenario, monkeypatch)
text = _final_text(run)
assert real in text, "a legitimately retrieved sequence was redacted"
assert not [e for e in run.events if e["kind"] == "provenance"]
|