Download tasks/llbean/tests/oracle-runtime/harbor_entry.py from TIGER-Lab/WebsiteBench: direct link, hf CLI and curl.
- Browser
- Download file 6.12 kB
-
https://huggingface.co/datasets/TIGER-Lab/WebsiteBench/resolve/main/tasks/llbean/tests/oracle-runtime/harbor_entry.py
- Command line
-
hf download hf://datasets/TIGER-Lab/WebsiteBench/tasks/llbean/tests/oracle-runtime/harbor_entry.py
-
curl -L -o harbor_entry.py https://huggingface.co/datasets/TIGER-Lab/WebsiteBench/resolve/main/tasks/llbean/tests/oracle-runtime/harbor_entry.py
6.12 kB
| #!/usr/bin/env python3 | |
| """Single-process Harbor adapter for the frozen L.L.Bean clone. | |
| The frozen clone exposes site-local session and business-state behavior but no | |
| reachable mail-producing flow. This adapter does not add a private registration, | |
| password-reset, outbox, or mailbox surface. | |
| """ | |
| from __future__ import annotations | |
| import hmac | |
| import os | |
| import random | |
| import sys | |
| import time | |
| from pathlib import Path | |
| ROOT = Path(__file__).resolve().parent | |
| SITE_ID = "llbean" | |
| DATABASE_NAME = "llbean.sqlite3" | |
| DISPLAY_NAME = "L.L.Bean" | |
| health_contract = "/healthz" | |
| migration_version = 1 | |
| required_invariants = ('frozen-route-closure', 'loopback-runtime', 'site-session') | |
| PAYMENT_ADAPTER = "local-sandbox" | |
| payment_profile = "local-sandbox" | |
| for _name in ("HOST", "PORT", "DATA_DIR", "SEED", "TZ"): | |
| if not os.environ.get(_name): | |
| raise SystemExit(f"{_name} is required") | |
| data_dir = Path(os.environ["DATA_DIR"]) | |
| if not data_dir.is_absolute(): | |
| raise SystemExit("DATA_DIR must be absolute") | |
| data_dir.mkdir(parents=True, exist_ok=True) | |
| try: | |
| random.seed(int(os.environ["SEED"])) | |
| except ValueError: | |
| raise SystemExit("SEED must be an integer") from None | |
| if hasattr(time, "tzset"): | |
| time.tzset() | |
| os.environ["WEBSITEBENCH_SITE_BACKEND_DATABASE"] = str(data_dir / DATABASE_NAME) | |
| os.environ["WEBSITEBENCH_SITE_BACKEND_RUNTIME"] = str(ROOT / "backend" / "runtime.json") | |
| os.environ["WEBSITEBENCH_DEPLOYMENT_PROFILE"] = "offline-harbor" | |
| os.environ["WEBSITEBENCH_PAYMENT_PROFILE"] = PAYMENT_ADAPTER | |
| os.environ["WEBSITEBENCH_LOCAL_HTTP_COOKIE"] = "1" | |
| os.environ["WEBSITEBENCH_HARBOR_MIGRATION_VERSION"] = str(migration_version) | |
| os.environ["PYTHONDONTWRITEBYTECODE"] = "1" | |
| for _name in ( | |
| "STRIPE_SECRET_KEY", | |
| "STRIPE_WEBHOOK_SECRET", | |
| "STRIPE_TEST_SECRET_KEY", | |
| "STRIPE_TEST_WEBHOOK_SECRET", | |
| ): | |
| os.environ.pop(_name, None) | |
| sys.dont_write_bytecode = True | |
| sys.path.insert(0, str(ROOT / "vendor")) | |
| sys.path.insert(0, str(ROOT / "clone")) | |
| # The frozen clone resolves its own static mounts relative to the process | |
| # working directory, so the adapter enters the clone tree rather than | |
| # rewriting a pinned path. | |
| os.chdir(ROOT / "clone") | |
| from app import app as app # noqa: E402 | |
| from backend.site_backend_integration import ( # noqa: E402 | |
| open_site_services, | |
| ) | |
| def _reset_site_state() -> None: | |
| """Reset exactly the rows the vendored backend and auth own.""" | |
| backend, auth = open_site_services() | |
| def _site_reset(connection) -> None: | |
| backend.lifecycle.reset_embedded( | |
| connection, confirm_site_id=SITE_ID | |
| ) | |
| auth.reset_site_state(site_reset=_site_reset, seed_accounts=[]) | |
| from fastapi import Request # noqa: E402 | |
| from fastapi.responses import JSONResponse, PlainTextResponse # noqa: E402 | |
| import uvicorn # noqa: E402 | |
| async def harbor_site_header(request: Request, call_next): | |
| if request.method == "GET" and request.url.path in { | |
| "/healthz", | |
| "/__websitebench/health", | |
| }: | |
| response = JSONResponse({"status": "ok"}) | |
| else: | |
| response = await call_next(request) | |
| response.headers["X-WebsiteBench-Site"] = SITE_ID | |
| return response | |
| async def harbor_lifecycle_health() -> JSONResponse: | |
| """Expose the exact lifecycle body while preserving clone `/healthz`.""" | |
| return JSONResponse({"status": "ok"}) | |
| _health_routes = [ | |
| route | |
| for route in app.router.routes | |
| if getattr(route, "endpoint", None) is harbor_lifecycle_health | |
| ] | |
| for route in _health_routes: | |
| app.router.routes.remove(route) | |
| app.router.routes[:0] = _health_routes | |
| _ADMIN_TOKEN = os.environ.get("WEBSITEBENCH_ADMIN_TOKEN", "") | |
| _RESET_PATH = "/__admin/reset" | |
| def _declared_routes(router): | |
| """Yield (path, methods) including lazily included sub-routers. | |
| FastAPI defers ``include_router`` into an ``_IncludedRouter`` placeholder | |
| that carries no path until the first request, so a plain scan of | |
| ``app.router.routes`` reports a clone as owning far fewer routes than it | |
| does. Follow ``original_router`` so detection sees the real table. | |
| """ | |
| for route in getattr(router, "routes", ()): | |
| included = getattr(route, "original_router", None) | |
| if included is not None: | |
| yield from _declared_routes(included) | |
| continue | |
| path = getattr(route, "path", None) | |
| if isinstance(path, str): | |
| yield path, set(getattr(route, "methods", None) or ()) | |
| _clone_owns_reset = any( | |
| path == _RESET_PATH and "POST" in methods | |
| for path, methods in _declared_routes(app.router) | |
| ) | |
| if not _clone_owns_reset: | |
| async def harbor_reset(request: Request) -> JSONResponse: | |
| """Deterministic reset, admin-token guarded like the paired clones. | |
| Reset authority is never granted to an unauthenticated caller; the | |
| trial harness isolates state with a fresh ``DATA_DIR`` instead. | |
| """ | |
| supplied = request.headers.get("X-WebsiteBench-Admin-Token", "") | |
| if not _ADMIN_TOKEN or not hmac.compare_digest(supplied, _ADMIN_TOKEN): | |
| return JSONResponse({"error": "forbidden"}, status_code=403) | |
| _reset_site_state() | |
| return JSONResponse({"reset": True, "site_id": SITE_ID}) | |
| async def harbor_reset_get() -> PlainTextResponse: | |
| return PlainTextResponse("Page not found", status_code=404) | |
| # A frozen catch-all would otherwise shadow the harness ABI. Move only the | |
| # two adapter routes to the front; reordering the whole table would also | |
| # move the clone's own routes relative to each other. | |
| _added = [ | |
| route | |
| for route in app.router.routes | |
| if getattr(route, "path", None) == _RESET_PATH | |
| ] | |
| for route in _added: | |
| app.router.routes.remove(route) | |
| app.router.routes[:0] = _added | |
| if __name__ == "__main__": | |
| uvicorn.run( | |
| app, | |
| host=os.environ["HOST"], | |
| port=int(os.environ["PORT"]), | |
| log_level="warning", | |
| ) | |