Repository navigation
Expand file tree
/
Copy pathtest_canary.py
More file actions
69 lines (58 loc) · 2.8 KB
/
Copy pathtest_canary.py
File metadata and controls
69 lines (58 loc) · 2.8 KB
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
"""Log canary: a full run with canary strings in the message and the data leaves none of them in any log record."""
import logging
from collections.abc import Iterator
import httpx
import pytest
from agents.anyplot.dev_fixture import snapshot_from_repo
from agents.anyplot.services import Services
from agents.main import Runtime, app, get_runtime
from .conftest import CASES
from .fakes import default_script
MESSAGE_CANARY = "CANARY-MSG-91f2"
CELL_CANARY = "CANARY-CELL-5b7d"
USER = "adm_canary0000000001"
HEADERS = {"X-Anyplot-User": USER, "X-Request-Id": "req-canary"}
@pytest.fixture
def runtime() -> Iterator[Runtime]:
fresh = Runtime()
app.dependency_overrides[get_runtime] = lambda: fresh
yield fresh
app.dependency_overrides.clear()
async def test_no_content_in_logs(
runtime: Runtime, services: Services, swap_models, caplog: pytest.LogCaptureFixture
) -> None:
caplog.set_level(logging.DEBUG)
for name in ("anyplot", "agents", "anyplot.agents.attribution"):
logging.getLogger(name).setLevel(logging.DEBUG)
swap_models("gemini", default_script())
data = (CASES / "scatter-basic-matplotlib" / "data.csv").read_text().replace("S07", CELL_CANARY)
transport = httpx.ASGITransport(app=app)
async with httpx.AsyncClient(transport=transport, base_url="http://agents") as client:
created = await client.post(
"/v1/sessions",
headers=HEADERS,
json={
"user": USER,
"spec_id": "scatter-basic",
"library": "matplotlib",
"locale": "en",
"snapshot": snapshot_from_repo("scatter-basic", "matplotlib").model_dump(),
},
)
sid = created.json()["session_id"]
assert (
await client.post(f"/v1/sessions/{sid}/dataset", headers=HEADERS, json={"text": data})
).status_code == 200
# Confirmed, so the message is no summary turn (which refuses the plot before the run) and the run goes
# through the adapter, the render and the review with the canaries in the request and the cells.
confirmed = await client.post(f"/v1/sessions/{sid}/dataset/confirm", headers=HEADERS)
assert confirmed.status_code == 200, confirmed.text
response = await client.post(
f"/v1/sessions/{sid}/messages", headers=HEADERS, json={"text": f"Please plot it {MESSAGE_CANARY}"}
)
assert '"plot"' in response.text or "event: plot" in response.text
records = [record.getMessage() for record in caplog.records]
assert any('"hook": "model"' in message for message in records), "the attribution log ran"
leaked = [message for message in records if MESSAGE_CANARY in message or CELL_CANARY in message]
assert leaked == []
assert not any("Exam Score" in message for message in records)