File size: 8,221 Bytes
fad5c43 | 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 | """Validate declared case artifacts against their task specification."""
from __future__ import annotations
from typing import Any, Mapping
from .case_protocol import (
CaseProtocolError,
canonical_json,
validate_case_manifest_payload,
validate_case_references,
)
ARTIFACT_FILENAMES = {
"manifest": "case-manifest.json",
"task": "task-suite.json",
"visual": "visual-suite.json",
"cicd": "cicd-suite.json",
}
_PAIRED_GOLDEN_KEYS = {
"expected",
"expected_value",
"golden",
"golden_path",
"reference_image",
"reference_observations",
}
def _project_task(case: Mapping[str, Any]) -> dict[str, Any]:
task: dict[str, Any] = {
"id": case["id"],
"timeout_sec": case["timeout_sec"],
"actions": case["actions"],
"observations": case["observations"],
}
if "comparison_source" in case:
task["comparison_source"] = case["comparison_source"]
if case.get("reference_mutation_authorized") is True:
task["reference_mutation_authorized"] = True
return task
def _validate_comparison_sources(cases: list[Mapping[str, Any]]) -> None:
"""Validate the explicit protocol used by newly-authored case catalogs.
Historical catalogs have no marker and remain byte-compatible. Once a
catalog opts in by marking any case, every case must be marked so new tests
cannot silently fall back to persisted golden data.
"""
marked = [case for case in cases if "comparison_source" in case]
if not marked:
return
if len(marked) != len(cases):
missing = sorted(
str(case.get("id", ""))
for case in cases
if "comparison_source" not in case
)
raise CaseProtocolError(
"comparison_source must be explicit for every case in an opted-in "
f"catalog; missing={missing}"
)
for case in cases:
source = case["comparison_source"]
if case["tier"] == "T3":
if source != "fixed-assertion":
raise CaseProtocolError(
f"case {case['id']!r}: T3 obligations must use fixed-assertion"
)
continue
if source != "paired-runtime":
raise CaseProtocolError(
f"case {case['id']!r}: runtime cases must use paired-runtime"
)
stack: list[Any] = [case.get("actions", []), case.get("observations", [])]
if isinstance(case.get("visual"), Mapping):
stack.append(case["visual"])
visual = case["visual"]
if visual.get("comparator") != "ssim" or not isinstance(
visual.get("threshold"), (int, float)
):
raise CaseProtocolError(
f"case {case['id']!r}: paired visual capture requires ssim comparator and threshold"
)
while stack:
value = stack.pop()
if isinstance(value, Mapping):
forbidden = sorted(_PAIRED_GOLDEN_KEYS.intersection(value))
if forbidden:
raise CaseProtocolError(
f"case {case['id']!r}: paired-runtime embeds golden keys {forbidden}"
)
stack.extend(value.values())
elif isinstance(value, list):
stack.extend(value)
def project_case_design(value: Mapping[str, Any]) -> dict[str, dict[str, Any]]:
"""Sort and project explicit records into the four formal artifacts."""
site_id = str(value["site_id"])
artifact_ids = value["artifact_ids"]
cases = sorted(value["cases"], key=lambda item: str(item["id"]))
_validate_comparison_sources(cases)
manifest_cases: list[dict[str, Any]] = []
tasks: list[dict[str, Any]] = []
checkpoints: list[dict[str, Any]] = []
checks: list[dict[str, Any]] = []
for case in cases:
manifest_case: dict[str, Any] = {
"id": case["id"],
"tier": case["tier"],
"kind": case["kind"],
"timeout_sec": case["timeout_sec"],
}
if "comparison_source" in case:
manifest_case["comparison_source"] = case["comparison_source"]
# Lane-based profiles score against the lane, so it has to survive the
# projection. Legacy designs carry no lane and keep projecting
# byte-identically to what is already committed.
if "lane" in case:
manifest_case["lane"] = case["lane"]
if case["tier"] in {"T1", "T2"}:
manifest_case["task_id"] = case["id"]
tasks.append(_project_task(case))
if case["tier"] == "T2":
manifest_case["level"] = case["level"]
if "visual" in case:
manifest_case["visual_checkpoint_ids"] = [case["visual"]["id"]]
visual = dict(case["visual"])
if "comparison_source" in case:
visual["comparison_source"] = case["comparison_source"]
checkpoints.append(visual)
if case["tier"] == "T3":
manifest_case["cicd_check_id"] = case["cicd_check"]["id"]
check = dict(case["cicd_check"])
if "comparison_source" in case:
check["comparison_source"] = case["comparison_source"]
checks.append(check)
manifest_cases.append(manifest_case)
# The manifest is scored against the same profile the design was authored
# against, so it travels with the projection -- but only when the design
# names one. Designs written before the profile existed keep projecting
# byte-identically to what is already committed.
declared_profile = value.get("suite_profile")
projected = {
"manifest": {
"schema": "websitebench.harbor.case-manifest",
"manifest_id": artifact_ids["manifest"],
"site_id": site_id,
"status": "complete" if value["status"] == "complete" else "draft",
"dsl_version": "websitebench.harbor.neutral-dsl",
**({"suite_profile": declared_profile} if declared_profile else {}),
"cases": manifest_cases,
},
"task": {
"schema": "websitebench.harbor.task-suite",
"suite_id": artifact_ids["task_suite"],
"site_id": site_id,
"dsl_version": "websitebench.harbor.playwright-dsl",
"tasks": sorted(tasks, key=lambda item: str(item["id"])),
},
"visual": {
"schema": "websitebench.harbor.visual-suite",
"suite_id": artifact_ids["visual_suite"],
"site_id": site_id,
"checkpoints": sorted(checkpoints, key=lambda item: str(item["id"])),
},
"cicd": {
"schema": "websitebench.harbor.cicd-suite",
"suite_id": artifact_ids["cicd_suite"],
"site_id": site_id,
"checks": sorted(checks, key=lambda item: str(item["id"])),
},
}
validate_case_manifest_payload(
projected["manifest"],
allow_draft=value["status"] == "draft",
expected_site_id=site_id,
)
if value["status"] == "complete":
validate_case_references(
projected["manifest"],
task_suite=projected["task"],
visual_suite=projected["visual"],
cicd_suite=projected["cicd"],
)
return projected
def validate_projection(
design: Mapping[str, Any],
*,
case_manifest: Mapping[str, Any],
task_suite: Mapping[str, Any],
visual_suite: Mapping[str, Any],
cicd_suite: Mapping[str, Any],
) -> None:
"""Reject any authored artifact that drifted from its explicit catalog."""
expected = project_case_design(design)
observed = {
"manifest": case_manifest,
"task": task_suite,
"visual": visual_suite,
"cicd": cicd_suite,
}
drift = [
ARTIFACT_FILENAMES[kind]
for kind in ("manifest", "task", "visual", "cicd")
if canonical_json(expected[kind]) != canonical_json(observed[kind])
]
if drift:
raise CaseProtocolError(
"case-design projection drift: " + ", ".join(sorted(drift))
)
|