File size: 12,166 Bytes
ae07f06
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73a5f3a
ae07f06
 
 
e9c0356
ae07f06
e9c0356
 
ae07f06
 
a75cabf
 
 
 
ae07f06
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e9c0356
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
be68cd2
 
 
ae07f06
e9c0356
 
ae07f06
e9c0356
ae07f06
be68cd2
ae07f06
 
e9c0356
 
 
 
 
 
 
be68cd2
e9c0356
ae07f06
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e9c0356
 
ae07f06
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
"""Pydantic models for ST-WebAgentBench leaderboard submissions.

Defines the complete submission bundle schema including metadata,
per-task evidence, computed metrics, and integrity manifest.

Task/policy counts and safety dimensions are computed dynamically
from test.raw.json so the Space auto-adapts when the benchmark grows.
"""

import json
import logging
import re
from datetime import datetime, timezone
from pathlib import Path
from typing import List, Optional

from pydantic import BaseModel, Field, field_validator

from validation.integrity import BENCHMARK_VERSION

logger = logging.getLogger(__name__)

# ---------------------------------------------------------------------------
# Dynamic benchmark config — computed from test.raw.json at startup
# ---------------------------------------------------------------------------

_TASKS_DATA_PATH = Path(__file__).resolve().parent.parent / "data" / "test.raw.json"


def _load_benchmark_config() -> tuple:
    """Load task/policy counts, safety dimensions, web apps, and tiers from test.raw.json.

    Returns (task_count, policy_count, safety_dimensions, dimension_display,
             web_applications, tier_config).
    """
    if not _TASKS_DATA_PATH.exists():
        raise FileNotFoundError(
            f"test.raw.json not found at {_TASKS_DATA_PATH}. "
            "This file must be included in the Space deployment."
        )

    with open(_TASKS_DATA_PATH) as f:
        tasks = json.load(f)

    task_count = len(tasks)
    policy_count = sum(len(t.get("policies", [])) for t in tasks)

    # Extract unique safety dimensions and build display names from task data
    dim_set = set()
    for t in tasks:
        for p in t.get("policies", []):
            cat = p.get("policy_category", "")
            if cat:
                dim_set.add(cat)

    safety_dims = sorted(dim_set)

    # Auto-generate display names: "user_consent" -> "User Consent"
    dim_display = {}
    for d in safety_dims:
        dim_display[d] = d.replace("_", " ").title().replace("And ", "& ")

    # Extract unique web applications
    web_apps = set()
    for t in tasks:
        for s in t.get("sites", []):
            web_apps.add(s)
    web_applications = sorted(web_apps)

    # Extract tier configuration from task_metadata
    # e.g. {"crm_policy_complexity": {"easy": [235, 236, ...], "medium": [...], ...}}
    tier_config: dict[str, dict[str, list[int]]] = {}
    for t in tasks:
        meta = t.get("task_metadata", {})
        if not isinstance(meta, dict):
            continue
        tier = meta.get("difficulty_tier")
        group = meta.get("tier_group")
        if tier and group:
            tier_config.setdefault(group, {}).setdefault(tier, []).append(t["task_id"])

    # Extract actual task IDs (don't assume 0..N-1)
    task_ids = sorted(t["task_id"] for t in tasks)

    logger.info(
        "Loaded benchmark config: %d tasks, %d policies, %d dimensions, "
        "%d web apps, %d tier groups",
        task_count, policy_count, len(safety_dims),
        len(web_applications), len(tier_config),
    )
    return task_count, policy_count, safety_dims, dim_display, web_applications, tier_config, task_ids


(
    EXPECTED_TASK_COUNT,
    EXPECTED_POLICY_COUNT,
    SAFETY_DIMENSIONS,
    DIMENSION_DISPLAY,
    WEB_APPLICATIONS,
    TIER_CONFIG,
    EXPECTED_TASK_IDS,
) = _load_benchmark_config()


# ---------------------------------------------------------------------------
# Per-step evidence
# ---------------------------------------------------------------------------


class ActionSummary(BaseModel):
    """Lightweight action record for server-side re-evaluation.

    Excludes screenshots, full DOM, and LLM reasoning (too large).
    Includes everything needed for offline evaluators.
    """

    step: int = Field(..., ge=0)
    action_type: str = Field(..., description="e.g. click, fill, goto, send_msg_to_user, answer")
    action_args: List[str] = Field(default_factory=list)
    element_text: str = Field("", description="From state_info, for ActionEvaluator matching")
    element_bid: str = Field("", description="From state_info, for CountAction/Sequence matching")
    observed_url: str = Field("", description="Page URL after this action")
    open_pages_urls: List[str] = Field(default_factory=list)
    timestamp: float = Field(..., description="Epoch seconds")


# ---------------------------------------------------------------------------
# Per-policy evidence
# ---------------------------------------------------------------------------


class PolicyReport(BaseModel):
    """Per-policy evaluation result within a task."""

    policy_index: int = Field(..., ge=0)
    policy_template_id: str
    policy_category: str
    source: str = Field(..., description="organization, user, or task")
    violated: bool
    dormant: bool
    violating_step: Optional[int] = None
    violating_action: Optional[str] = None
    eval_type: Optional[str] = None


# ---------------------------------------------------------------------------
# Per-task evidence
# ---------------------------------------------------------------------------


class TaskEvidence(BaseModel):
    """Evidence for a single task execution."""

    task_id: int = Field(..., ge=0)

    # Core results
    total_reward: float = Field(..., ge=0, le=1)
    satisfied_requirements: int = Field(0, ge=0)
    total_requirements: int = Field(1, ge=1)
    safety_report: List[PolicyReport]

    # Trajectory summary
    action_sequence: List[ActionSummary]
    visited_urls: List[str] = Field(default_factory=list)

    # Timing
    start_time: str
    end_time: str
    num_steps: int = Field(..., ge=0)

    # Hash binding (must match integrity manifest)
    trajectory_hash: str


# ---------------------------------------------------------------------------
# Aggregate metrics
# ---------------------------------------------------------------------------


class DimensionMetrics(BaseModel):
    """Per-safety-dimension aggregated metrics."""

    dimension: str
    failures: int = Field(..., ge=0)
    total_instances: int = Field(..., ge=0)
    active_instances: int = Field(..., ge=0)
    dormant_count: int = Field(..., ge=0)
    risk_ratio: float = Field(..., ge=0, le=1)
    active_risk_ratio: float = Field(..., ge=0, le=1)
    risk_tier: str  # low, medium, high
    active_risk_tier: str


class TierMetrics(BaseModel):
    """Per-difficulty-tier metrics (Easy/Medium/Hard CRM tasks)."""

    tier: str
    CR: float = Field(..., ge=0, le=1)
    CuP: float = Field(..., ge=0, le=1)
    semi_CR: float = Field(..., ge=0, le=1)
    semi_CuP: float = Field(..., ge=0, le=1)


class PerAppMetrics(BaseModel):
    """Per-application metrics."""

    app: str
    CR: float = Field(..., ge=0, le=1)
    CuP: float = Field(..., ge=0, le=1)
    semi_CR: float = Field(0, ge=0, le=1)
    semi_CuP: float = Field(0, ge=0, le=1)
    task_count: int = Field(..., ge=0)


class ClaimedMetrics(BaseModel):
    """Aggregate metrics claimed by the submitter.

    These are independently recomputed server-side from task_results.
    Any discrepancy flags the submission for review.
    """

    CR: float = Field(..., ge=0, le=1, description="Completion Rate")
    CuP: float = Field(..., ge=0, le=1, description="Completion under Policy")
    semi_CR: float = Field(..., ge=0, le=1, description="Partial Completion Rate")
    semi_CuP: float = Field(..., ge=0, le=1, description="Partial CuP")
    all_pass_at_k: Optional[float] = Field(None, ge=0, le=1)
    k: Optional[int] = Field(None, ge=1)


# ---------------------------------------------------------------------------
# Submission results (wraps all metric types)
# ---------------------------------------------------------------------------


class SubmissionResults(BaseModel):
    """All computed metrics for the submission."""

    metrics: ClaimedMetrics
    dimensions: List[DimensionMetrics]
    tiers: Optional[List[TierMetrics]] = None
    apps: Optional[List[PerAppMetrics]] = None
    tasks_evaluated: int = Field(..., ge=0)
    tasks_total: int = EXPECTED_TASK_COUNT
    policies_evaluated: int = Field(..., ge=0)


# ---------------------------------------------------------------------------
# Metadata
# ---------------------------------------------------------------------------


class SubmissionMetadata(BaseModel):
    """Agent and team metadata for a leaderboard submission."""

    # Required
    agent_id: str = Field(..., min_length=1, max_length=128)
    model_name: str = Field(..., min_length=1, max_length=256)
    team: str = Field(..., min_length=1, max_length=256)
    code_repository_url: str = Field(
        ...,
        min_length=1,
        description="Public GitHub/GitLab/HuggingFace repository URL",
    )
    contact_email: str = Field(
        ...,
        min_length=1,
        description="Contact email for verification (not displayed publicly)",
    )

    # Optional
    paper_url: Optional[str] = None
    agent_framework: Optional[str] = None
    model_family: Optional[str] = None
    is_open_source: Optional[bool] = None
    is_open_weights: Optional[bool] = None
    cost_per_task_usd: Optional[float] = Field(None, ge=0)
    total_cost_usd: Optional[float] = Field(None, ge=0)
    hardware: Optional[str] = None
    num_runs: int = Field(1, ge=1)
    uses_vision: Optional[bool] = None
    max_steps: Optional[int] = Field(None, ge=1)
    description: Optional[str] = Field(None, max_length=1000)

    @field_validator("agent_id")
    @classmethod
    def validate_agent_id(cls, v: str) -> str:
        if not re.match(r"^[a-zA-Z0-9_\-\.]+$", v):
            raise ValueError(
                "agent_id must contain only alphanumeric characters, "
                "hyphens, underscores, and dots"
            )
        return v

    @field_validator("code_repository_url")
    @classmethod
    def validate_repo_url(cls, v: str) -> str:
        valid_prefixes = (
            "https://github.com/",
            "https://gitlab.com/",
            "https://huggingface.co/",
            "https://bitbucket.org/",
        )
        if not any(v.startswith(p) for p in valid_prefixes):
            raise ValueError(
                "code_repository_url must be a public GitHub, GitLab, "
                "HuggingFace, or Bitbucket URL"
            )
        return v


# ---------------------------------------------------------------------------
# Integrity section
# ---------------------------------------------------------------------------


class IntegritySection(BaseModel):
    """Cryptographic integrity data from the evaluation run."""

    run_id: str
    benchmark_version: str = BENCHMARK_VERSION
    timestamp_start: float
    timestamp_end: Optional[float] = None
    evaluators_sha256: str
    task_config_sha256: str
    custom_env_sha256: str
    helper_functions_sha256: str
    task_hashes: dict  # task_id (str key in JSON) -> SHA256
    manifest_hash: str
    hmac_signature: Optional[str] = Field(
        None,
        description="HMAC-SHA256 signature (requires ST_BENCH_SIGNING_KEY)",
    )


# ---------------------------------------------------------------------------
# Top-level submission
# ---------------------------------------------------------------------------


class Submission(BaseModel):
    """Complete leaderboard submission bundle.

    Contains metadata, per-task evidence, computed metrics, and
    cryptographic integrity data.
    """

    schema_version: str = Field("1.0", description="Submission schema version")
    benchmark_version: str = BENCHMARK_VERSION
    submission_date: str = Field(
        default_factory=lambda: datetime.now(timezone.utc).isoformat(),
    )
    metadata: SubmissionMetadata
    results: SubmissionResults
    task_evidence: List[TaskEvidence]
    integrity: IntegritySection

    @field_validator("submission_date")
    @classmethod
    def validate_date(cls, v: str) -> str:
        # Ensure the date can be parsed
        try:
            datetime.fromisoformat(v)
        except ValueError as e:
            raise ValueError(f"submission_date must be ISO 8601 format: {e}") from e
        return v