# Copyright 2026 SZL Holdings # SPDX-License-Identifier: Apache-2.0 """yarqa Space — FastAPI backend serving a sovereign static app + JSON endpoints. Architecture (doctrine v11 honest): * yarqa is an ENGINEERING METHOD (CFD) tier capability — never a locked theorem, never folded into the locked-8, never "proven". Receipts assert integrity / reproducibility, NOT correctness. * Every data tab has ONE code path with an explicit LIVE vs SAMPLE badge based on REAL reachability of license-clean public feeds (see feeds.py). Endpoints: GET /healthz — source-of-truth health + feed states GET /api/compartments — 3D field from live/sample feeds + receipt GET /api/agentic — one sense->route->gate->receipt loop, AgentSteps GET /api/chain — append-only hash-linked receipt chain POST /api/chain/verify — replay-verify a (possibly tampered) chain GET /api/forecast — honest short-horizon PROJECTED trend GET /api/feeds — raw per-source LIVE/SAMPLE readings (cited) """ from __future__ import annotations import os import sys import time from pathlib import Path from typing import Any import numpy as np from fastapi import FastAPI, Request from fastapi.middleware.cors import CORSMiddleware from fastapi.responses import JSONResponse, FileResponse from fastapi.staticfiles import StaticFiles from pydantic import BaseModel # Make the parent yarqa package importable when run from the space/ dir or HF. _HERE = Path(__file__).resolve().parent _REPO = _HERE.parent for p in (str(_REPO), str(_HERE)): if p not in sys.path: sys.path.insert(0, p) import yarqa # noqa: E402 from yarqa import ( # noqa: E402 Mesh, compartmentalize_with_receipt, compartmentalize, verify, build_experts, AgenticYarqa, ReceiptChain, ) from yarqa.core import compartment_summary # noqa: E402 import feeds as feeds_mod # noqa: E402 app = FastAPI(title="yarqa Space", version="0.4.0") # --------------------------------------------------------------------------- # SAFE-NOW hardening (R2) — real response headers on a real (FastAPI) server. # Unlike a pure static HF Space, here we CAN emit honest, browser-honored # headers, so we set the full set: CSP, HSTS, nosniff, Referrer-Policy, and an # enforced frame-ancestors that allows the legitimate HF embed but nothing else. # --------------------------------------------------------------------------- # CORS: restrict cross-origin *browser* access to the SZL/HF embed origins. This # does NOT block public reads — same-origin fetches and direct (curl/server) # GETs carry no Origin and are unaffected; only cross-site browser JS is gated, # and only to this allow-list (no '*'). Read-only methods, no credentials. _ALLOWED_ORIGINS = [ "https://huggingface.co", "https://a-11-oy.com", "https://www.a-11-oy.com", ] app.add_middleware( CORSMiddleware, allow_origins=_ALLOWED_ORIGINS, allow_origin_regex=r"https://[a-z0-9-]+\.hf\.space", allow_credentials=False, allow_methods=["GET", "POST", "OPTIONS"], allow_headers=["Content-Type"], max_age=600, ) # CSP: scripts are all vendored same-origin files (no inline