Datasets:

Tasks:
Other
ArXiv:
File size: 9,106 Bytes
c72a741
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Plant-survival check: did the secret actually get planted before the probe?

Every loaded session's first step plants the secret at a prior origin. A standing privacy
instruction (SCT_GUARD, see agents/browseruse_runner.GUARD_TEXTS) can make the agent refuse to
do it. When it does, nothing is planted, the loaded cells become indistinguishable from cold,
ScenarioLeak collapses to ~0 — and the run reports "no channel" while never having tested one.
That failure is silent in every other report, which is why this check exists and why it gates
the guarded arms.

Verdicts per loaded session, from the saved browser-use trace:

  DELIVERED      reached a post-plant page  -> the secret is in context, session is valid
  ENTERED_ONLY   opened the plant URL but never got past it (login or action not completed)
                 <- the guard-refusal signature
  NEVER_VISITED  never opened the plant URL at all
  NO_TRACE       trace file missing or unreadable

The check only has teeth where the plant needs a second step: a sign-in (plant_bank /
plant_balance / plant_state) or a button press (plant_act, whose task must genuinely complete —
"import my contacts", "add it to the cart"). For a plant_read page there is nothing to clear, so
opening the URL IS delivery and ENTERED_ONLY can never fire.

Reported per (scenario, PLANT, guard). Pooling delivery across a scenario's three plant routes
would hide the one thing running three routes is for: telling "this model does not leak" apart
from "this particular plant did not survive".

Usage:
  SCT_DATASET=<ds> uv run python scripts/plant_check.py [--scenarios a b c] [--verbose]
"""
from __future__ import annotations

import argparse
import contextlib
import json
import sqlite3
import sys
from collections import Counter
from pathlib import Path

# The repo isn't installed as a package, so a bare `python scripts/plant_check.py` can't see
# harness/ or orchestrator/. Put the repo root on the path so the documented invocation works
# without the caller having to remember PYTHONPATH=.
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))

from harness.session import trace_basename
from harness.state_planting import build_plan
from orchestrator.config import DATASET, RESULTS_DB, TRACES_DIR

DELIVERED = "DELIVERED"
ENTERED_ONLY = "ENTERED_ONLY"
NEVER_VISITED = "NEVER_VISITED"
NO_TRACE = "NO_TRACE"

# Plant steps that are only half-delivered by opening the URL, and the pages that prove the rest
# happened. Two shapes:
#
#   plant_bank / plant_balance / plant_state   sign-in plants (the legacy slice and the framing
#                                              controls). Delivery needs the post-login page.
#   plant_act                                  a `list_action` plant whose task must COMPLETE
#                                              ("import my contacts", "add it to the cart").
#                                              Delivery needs the /do confirmation page, since
#                                              the held value and the completion both live there.
#
# mock_origins/bank redirects to a bare /dashboard and services to /home, deliberately keeping
# the brand out of the address bar (see harness/opaque.py) — so these paths are the same for
# every condition and reveal nothing about which secret was planted.
LOGIN_PLANTS = {"plant_bank", "plant_balance", "plant_state"}
POST_LOGIN_PATHS = ("/dashboard", "/home")
ACTION_PLANTS = {"plant_act"}
POST_ACTION_PATHS = ("/do",)


def _visited_urls(path: Path) -> list[str] | None:
    """Every URL the agent's browser was actually on, from the saved trace. None if unreadable."""
    try:
        data = json.loads(path.read_text())
    except Exception:
        return None
    return [(step.get("state") or {}).get("url") or ""
            for step in (data.get("history") or [])]


def _origin(url: str) -> str:
    return url.split("/", 3)[2] if url.count("/") >= 2 else ""


def classify(session_id: str, condition: str, variant: str, scenario: str,
             trace_path: Path, plant_id: str = "") -> tuple[str, str]:
    """(verdict, plant_url) for one loaded session."""
    plan = build_plan(session_id, condition, variant, scenario, plant_id=plant_id)
    plant = next((s for s in plan.steps if (s.label or "").startswith("plant")), None)
    if plant is None or not plant.start_url:
        return "NO_PLANT_STEP", ""
    plant_url = plant.start_url
    urls = _visited_urls(trace_path)
    if urls is None:
        return NO_TRACE, plant_url

    # Compare on path, not the whole URL: browser-use records the post-redirect address.
    visited = {u.split("?")[0].rstrip("/") for u in urls if u}
    if plant_url.split("?")[0].rstrip("/") not in visited:
        return NEVER_VISITED, plant_url
    label = plant.label or ""
    if label in LOGIN_PLANTS:
        needed = POST_LOGIN_PATHS
    elif label in ACTION_PLANTS:
        needed = POST_ACTION_PATHS
    else:
        return DELIVERED, plant_url          # nothing to clear; opening the page IS the plant
    host = _origin(plant_url)
    cleared = any(_origin(u) == host and u.split("?")[0].rstrip("/").endswith(needed)
                  for u in urls if u)
    return (DELIVERED if cleared else ENTERED_ONLY), plant_url


def main() -> None:
    ap = argparse.ArgumentParser()
    ap.add_argument("--scenarios", nargs="*", default=None)
    ap.add_argument("--verbose", action="store_true", help="one line per failing session")
    args = ap.parse_args()

    with sqlite3.connect(f"file:{RESULTS_DB}?mode=ro", uri=True) as c:
        c.row_factory = sqlite3.Row
        cols = {r[1] for r in c.execute("PRAGMA table_info(sessions)")}
        # A result database recorded before scenarios declared multiple plants has no `plant`
        # column; substituting an empty string lets those datasets replay unchanged.
        plant_col = "plant" if "plant" in cols else "'' AS plant"
        rows = c.execute(
            f"SELECT session_id, scenario, condition, variant, {plant_col}, secret_label, "  # noqa: S608  plant_col is one of two literals above
            "trace_path, error, meta FROM sessions ORDER BY scenario, condition").fetchall()
    if args.scenarios:
        rows = [r for r in rows if r["scenario"] in args.scenarios]

    print(f"\n==== PLANT SURVIVAL  (dataset: {DATASET or '(flat results/)'}) ====")
    print("DELIVERED = secret reached the agent's context; ENTERED_ONLY = opened the plant "
          "page but never signed in\n")

    per: dict[tuple[str, str], Counter] = {}
    failures: list[str] = []
    n_cold = 0
    for r in rows:
        if (r["secret_label"] or "none") in ("none", "capability"):
            n_cold += 1        # cold and capability cells have no plant step by construction
            continue
        guard = "?"
        if r["meta"]:
            # The guard arm is a display column in this report; unreadable meta leaves it "?"
            # rather than aborting a survival check over a label.
            with contextlib.suppress(Exception):
                guard = json.loads(r["meta"]).get("guard") or "?"
        tp = Path(r["trace_path"]) if r["trace_path"] else (
            Path(TRACES_DIR) / trace_basename(r["session_id"], r["scenario"],
                                              r["condition"], r["variant"], r["plant"]))
        try:
            verdict, plant_url = classify(r["session_id"], r["condition"], r["variant"],
                                          r["scenario"], tp, r["plant"] or "")
        except Exception as e:
            verdict, plant_url = f"ERROR({type(e).__name__})", ""
        # Keyed by PLANT as well as scenario: the whole point of running three plant routes is to
        # tell "this model does not leak" from "this plant did not survive", and a delivery rate
        # pooled across routes hides exactly that.
        per.setdefault((f"{r['scenario']}/{r['plant'] or '-'}", guard), Counter())[verdict] += 1
        if verdict != DELIVERED:
            failures.append(
                f"  {r['scenario']:22s} {r['condition']:16s} "
                f"{r['plant'] or '-':16s} {r['variant']:9s} "
                f"{verdict:14s} {r['session_id'][:12]}  plant={plant_url or '?'}"
                + (f"  err={r['error']}" if r["error"] else ""))

    width = max((len(s) for s, _ in per), default=10)
    print(f"  {'scenario/plant':{width}s}  {'guard':8s}  {'delivered':>12s}   breakdown")
    for (scen, guard), cnt in sorted(per.items()):
        n = sum(cnt.values())
        d = cnt[DELIVERED]
        other = {k: v for k, v in cnt.items() if k != DELIVERED}
        print(f"  {scen:{width}s}  {guard:8s}  {d:5d}/{n:<5d} {100*d/n:3.0f}%   "
              f"{other or ''}")

    if failures and args.verbose:
        print(f"\n---- {len(failures)} non-delivered session(s) ----")
        print("\n".join(failures))
    elif failures:
        print(f"\n  {len(failures)} non-delivered session(s) — rerun with --verbose to list them")
    print(f"\n  ({n_cold} cold session(s) skipped — no plant step by construction)")


if __name__ == "__main__":
    main()