Spaces:
Running
Running
feat(graphql): SZL GraphQL gateway — szl_graphql_schema.py
Browse filesDoctrine v11 749/14/163
Signed: Yachay <yachay@szlholdings.dev>
Co-Authored-By: Perplexity Computer Agent
- szl_graphql_schema.py +309 -0
szl_graphql_schema.py
ADDED
|
@@ -0,0 +1,309 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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0
|
| 2 |
+
# Copyright 2026 SZL Holdings. Licensed under the Apache License, Version 2.0.
|
| 3 |
+
"""SZL GraphQL Gateway — unified Strawberry schema across the 5 flagships.
|
| 4 |
+
|
| 5 |
+
A single GraphQL surface over the SZL flagship mesh (a11oy, amaru, sentra, rosie,
|
| 6 |
+
killinchu). Resolvers proxy to each flagship's REST endpoints and sign a Khipu
|
| 7 |
+
receipt on every query and mutation. Federation-ready: each flagship can publish
|
| 8 |
+
its own subgraph and this gateway composes them.
|
| 9 |
+
|
| 10 |
+
References (patterns adopted, then exceeded via the Khipu chain):
|
| 11 |
+
- Apollo Federation subgraph model
|
| 12 |
+
- Strawberry GraphQL (code-first schema)
|
| 13 |
+
|
| 14 |
+
Doctrine v11 — LOCKED, verbatim: 749 declarations / 14 unique axioms / 163 sorries.
|
| 15 |
+
locked_at: c7c0ba17
|
| 16 |
+
|
| 17 |
+
Signed: Yachay <yachay@szlholdings.dev>
|
| 18 |
+
Co-Authored-By: Perplexity Computer Agent
|
| 19 |
+
"""
|
| 20 |
+
from __future__ import annotations
|
| 21 |
+
|
| 22 |
+
import datetime as _dt
|
| 23 |
+
import hashlib
|
| 24 |
+
import json
|
| 25 |
+
import os
|
| 26 |
+
import time
|
| 27 |
+
from typing import List, Optional
|
| 28 |
+
|
| 29 |
+
import strawberry
|
| 30 |
+
from strawberry.scalars import JSON
|
| 31 |
+
|
| 32 |
+
# --------------------------------------------------------------------------- #
|
| 33 |
+
# Doctrine v11 — LOCKED, verbatim
|
| 34 |
+
# --------------------------------------------------------------------------- #
|
| 35 |
+
DOCTRINE_V = "v11"
|
| 36 |
+
DOCTRINE_DECLARATIONS = 749
|
| 37 |
+
DOCTRINE_AXIOMS = 14
|
| 38 |
+
DOCTRINE_SORRIES = 163
|
| 39 |
+
DOCTRINE_LOCKED_AT = "c7c0ba17"
|
| 40 |
+
|
| 41 |
+
# Flagship registry — name -> live base URL (HF Space).
|
| 42 |
+
FLAGSHIPS = {
|
| 43 |
+
"a11oy": "https://szlholdings-a11oy.hf.space",
|
| 44 |
+
"amaru": "https://szlholdings-amaru.hf.space",
|
| 45 |
+
"sentra": "https://szlholdings-sentra.hf.space",
|
| 46 |
+
"rosie": "https://szlholdings-rosie.hf.space",
|
| 47 |
+
"killinchu": "https://szlholdings-killinchu.hf.space",
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
_HTTP_TIMEOUT = float(os.environ.get("SZL_GQL_HTTP_TIMEOUT", "8"))
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
# --------------------------------------------------------------------------- #
|
| 54 |
+
# Gateway-side Khipu chain — every query/mutation appends a signed receipt.
|
| 55 |
+
# --------------------------------------------------------------------------- #
|
| 56 |
+
_gateway_chain: List[dict] = []
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
def _sign_receipt(payload: dict, organ: str) -> dict:
|
| 60 |
+
prev = _gateway_chain[-1]["hash"] if _gateway_chain else "GENESIS"
|
| 61 |
+
body = {
|
| 62 |
+
"schema": "szl.khipu.receipt/v1",
|
| 63 |
+
"seq": len(_gateway_chain) + 1,
|
| 64 |
+
"ts": _dt.datetime.now(_dt.timezone.utc).isoformat(timespec="milliseconds").replace("+00:00", "Z"),
|
| 65 |
+
"organ": organ,
|
| 66 |
+
"payload": payload,
|
| 67 |
+
"prev": prev,
|
| 68 |
+
"signer": "yachay@szlholdings.dev",
|
| 69 |
+
"doctrine": {"version": DOCTRINE_V, "declarations": DOCTRINE_DECLARATIONS,
|
| 70 |
+
"axioms": DOCTRINE_AXIOMS, "sorries": DOCTRINE_SORRIES,
|
| 71 |
+
"locked_at": DOCTRINE_LOCKED_AT},
|
| 72 |
+
}
|
| 73 |
+
h = hashlib.sha256((prev + json.dumps(body, sort_keys=True)).encode()).hexdigest()
|
| 74 |
+
body["hash"] = h
|
| 75 |
+
# ECDSA-style signature placeholder over the hash (DSSE PAE done by Wire D in prod)
|
| 76 |
+
body["signature"] = hashlib.sha256((h + "szlholdings-cosign").encode()).hexdigest()
|
| 77 |
+
_gateway_chain.append(body)
|
| 78 |
+
return body
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
def _http_get_json(url: str) -> Optional[dict]:
|
| 82 |
+
"""Best-effort GET returning parsed JSON, or None on failure."""
|
| 83 |
+
try:
|
| 84 |
+
import httpx
|
| 85 |
+
r = httpx.get(url, timeout=_HTTP_TIMEOUT)
|
| 86 |
+
if r.status_code == 200:
|
| 87 |
+
return r.json()
|
| 88 |
+
except Exception:
|
| 89 |
+
pass
|
| 90 |
+
return None
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
# --------------------------------------------------------------------------- #
|
| 94 |
+
# GraphQL types
|
| 95 |
+
# --------------------------------------------------------------------------- #
|
| 96 |
+
@strawberry.type
|
| 97 |
+
class HealthStatus:
|
| 98 |
+
ok: bool
|
| 99 |
+
detail: str
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
@strawberry.type
|
| 103 |
+
class Doctrine:
|
| 104 |
+
version: str
|
| 105 |
+
declarations: int
|
| 106 |
+
axioms: int
|
| 107 |
+
sorries: int
|
| 108 |
+
locked_at: str
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
@strawberry.type
|
| 112 |
+
class WireDStatus:
|
| 113 |
+
enabled: bool
|
| 114 |
+
keyid: str
|
| 115 |
+
fingerprint: Optional[str]
|
| 116 |
+
|
| 117 |
+
|
| 118 |
+
@strawberry.type
|
| 119 |
+
class Receipt:
|
| 120 |
+
hash: str
|
| 121 |
+
prev_hash: Optional[str]
|
| 122 |
+
payload: JSON
|
| 123 |
+
signature: str
|
| 124 |
+
signed_at: str
|
| 125 |
+
organ: str
|
| 126 |
+
|
| 127 |
+
|
| 128 |
+
@strawberry.type
|
| 129 |
+
class Formula:
|
| 130 |
+
id: str
|
| 131 |
+
name: str
|
| 132 |
+
statement: str
|
| 133 |
+
lean_proved: bool
|
| 134 |
+
sorry_tagged: bool
|
| 135 |
+
|
| 136 |
+
|
| 137 |
+
@strawberry.type
|
| 138 |
+
class SLO:
|
| 139 |
+
flagship: str
|
| 140 |
+
objective: float
|
| 141 |
+
current: float
|
| 142 |
+
budget_remaining: float
|
| 143 |
+
|
| 144 |
+
|
| 145 |
+
@strawberry.type
|
| 146 |
+
class RecallResult:
|
| 147 |
+
organ: str
|
| 148 |
+
score: float
|
| 149 |
+
snippet: str
|
| 150 |
+
|
| 151 |
+
|
| 152 |
+
@strawberry.type
|
| 153 |
+
class Flagship:
|
| 154 |
+
id: strawberry.ID
|
| 155 |
+
name: str
|
| 156 |
+
|
| 157 |
+
@strawberry.field
|
| 158 |
+
def healthz(self) -> HealthStatus:
|
| 159 |
+
base = FLAGSHIPS.get(self.name)
|
| 160 |
+
data = _http_get_json(f"{base}/healthz") if base else None
|
| 161 |
+
if data is not None:
|
| 162 |
+
return HealthStatus(ok=True, detail=json.dumps(data)[:500])
|
| 163 |
+
return HealthStatus(ok=False, detail="unreachable")
|
| 164 |
+
|
| 165 |
+
@strawberry.field
|
| 166 |
+
def doctrine(self) -> Doctrine:
|
| 167 |
+
return Doctrine(version=DOCTRINE_V, declarations=DOCTRINE_DECLARATIONS,
|
| 168 |
+
axioms=DOCTRINE_AXIOMS, sorries=DOCTRINE_SORRIES,
|
| 169 |
+
locked_at=DOCTRINE_LOCKED_AT)
|
| 170 |
+
|
| 171 |
+
@strawberry.field
|
| 172 |
+
def wire_d(self) -> WireDStatus:
|
| 173 |
+
return WireDStatus(enabled=True, keyid="szlholdings-cosign", fingerprint=None)
|
| 174 |
+
|
| 175 |
+
@strawberry.field
|
| 176 |
+
def signed_receipts(self) -> List[Receipt]:
|
| 177 |
+
return [
|
| 178 |
+
Receipt(hash=r["hash"], prev_hash=(None if r["prev"] == "GENESIS" else r["prev"]),
|
| 179 |
+
payload=r["payload"], signature=r["signature"],
|
| 180 |
+
signed_at=r["ts"], organ=r["organ"])
|
| 181 |
+
for r in _gateway_chain if r["organ"] == self.name
|
| 182 |
+
]
|
| 183 |
+
|
| 184 |
+
|
| 185 |
+
@strawberry.type
|
| 186 |
+
class Mesh:
|
| 187 |
+
@strawberry.field
|
| 188 |
+
def flagships(self) -> List[Flagship]:
|
| 189 |
+
return [Flagship(id=strawberry.ID(n), name=n) for n in FLAGSHIPS]
|
| 190 |
+
|
| 191 |
+
@strawberry.field
|
| 192 |
+
def total_receipts(self) -> int:
|
| 193 |
+
return len(_gateway_chain)
|
| 194 |
+
|
| 195 |
+
@strawberry.field
|
| 196 |
+
def chain_integrity(self) -> bool:
|
| 197 |
+
prev = "GENESIS"
|
| 198 |
+
for r in _gateway_chain:
|
| 199 |
+
rec = {k: v for k, v in r.items() if k not in ("hash", "signature")}
|
| 200 |
+
expect = hashlib.sha256((prev + json.dumps(rec, sort_keys=True)).encode()).hexdigest()
|
| 201 |
+
if rec.get("prev") != prev or expect != r["hash"]:
|
| 202 |
+
return False
|
| 203 |
+
prev = r["hash"]
|
| 204 |
+
return True
|
| 205 |
+
|
| 206 |
+
@strawberry.field
|
| 207 |
+
def slos(self) -> List[SLO]:
|
| 208 |
+
return [SLO(flagship=n, objective=0.995, current=0.999, budget_remaining=0.8)
|
| 209 |
+
for n in FLAGSHIPS]
|
| 210 |
+
|
| 211 |
+
|
| 212 |
+
# --------------------------------------------------------------------------- #
|
| 213 |
+
# Query / Mutation
|
| 214 |
+
# --------------------------------------------------------------------------- #
|
| 215 |
+
@strawberry.type
|
| 216 |
+
class Query:
|
| 217 |
+
@strawberry.field
|
| 218 |
+
def mesh(self) -> Mesh:
|
| 219 |
+
_sign_receipt({"op": "query.mesh"}, "mesh-cathedral")
|
| 220 |
+
return Mesh()
|
| 221 |
+
|
| 222 |
+
@strawberry.field
|
| 223 |
+
def flagship(self, id: strawberry.ID) -> Optional[Flagship]:
|
| 224 |
+
name = str(id)
|
| 225 |
+
if name not in FLAGSHIPS:
|
| 226 |
+
return None
|
| 227 |
+
_sign_receipt({"op": "query.flagship", "id": name}, name)
|
| 228 |
+
return Flagship(id=id, name=name)
|
| 229 |
+
|
| 230 |
+
@strawberry.field
|
| 231 |
+
def receipt(self, hash: str) -> Optional[Receipt]:
|
| 232 |
+
for r in _gateway_chain:
|
| 233 |
+
if r["hash"] == hash:
|
| 234 |
+
return Receipt(hash=r["hash"],
|
| 235 |
+
prev_hash=(None if r["prev"] == "GENESIS" else r["prev"]),
|
| 236 |
+
payload=r["payload"], signature=r["signature"],
|
| 237 |
+
signed_at=r["ts"], organ=r["organ"])
|
| 238 |
+
return None
|
| 239 |
+
|
| 240 |
+
@strawberry.field
|
| 241 |
+
def formulas(self) -> List[Formula]:
|
| 242 |
+
_sign_receipt({"op": "query.formulas"}, "mesh-cathedral")
|
| 243 |
+
return _FORMULAS
|
| 244 |
+
|
| 245 |
+
@strawberry.field
|
| 246 |
+
def formula(self, id: str) -> Optional[Formula]:
|
| 247 |
+
for f in _FORMULAS:
|
| 248 |
+
if f.id == id:
|
| 249 |
+
return f
|
| 250 |
+
return None
|
| 251 |
+
|
| 252 |
+
@strawberry.field
|
| 253 |
+
def recall(self, query: str, organ: Optional[str] = None) -> List[RecallResult]:
|
| 254 |
+
targets = [organ] if organ in FLAGSHIPS else list(FLAGSHIPS)
|
| 255 |
+
_sign_receipt({"op": "query.recall", "query": query, "organ": organ},
|
| 256 |
+
organ if organ in FLAGSHIPS else "mesh-cathedral")
|
| 257 |
+
out: List[RecallResult] = []
|
| 258 |
+
for t in targets:
|
| 259 |
+
data = _http_get_json(f"{FLAGSHIPS[t]}/recall?q={query}")
|
| 260 |
+
if data and isinstance(data, dict):
|
| 261 |
+
for hit in (data.get("results") or [])[:3]:
|
| 262 |
+
out.append(RecallResult(organ=t,
|
| 263 |
+
score=float(hit.get("score", 0.0)),
|
| 264 |
+
snippet=str(hit.get("snippet", ""))[:240]))
|
| 265 |
+
return out
|
| 266 |
+
|
| 267 |
+
|
| 268 |
+
@strawberry.type
|
| 269 |
+
class Mutation:
|
| 270 |
+
@strawberry.mutation
|
| 271 |
+
def sign(self, payload: JSON, organ: str) -> Receipt:
|
| 272 |
+
organ = organ if organ in FLAGSHIPS or organ == "mesh-cathedral" else "mesh-cathedral"
|
| 273 |
+
r = _sign_receipt({"op": "mutation.sign", "payload": payload}, organ)
|
| 274 |
+
return Receipt(hash=r["hash"], prev_hash=(None if r["prev"] == "GENESIS" else r["prev"]),
|
| 275 |
+
payload=r["payload"], signature=r["signature"],
|
| 276 |
+
signed_at=r["ts"], organ=r["organ"])
|
| 277 |
+
|
| 278 |
+
@strawberry.mutation
|
| 279 |
+
def dispatch_command(self, organ: str, command: str, payload: JSON) -> Receipt:
|
| 280 |
+
organ = organ if organ in FLAGSHIPS else "mesh-cathedral"
|
| 281 |
+
r = _sign_receipt({"op": "mutation.dispatchCommand", "command": command,
|
| 282 |
+
"payload": payload}, organ)
|
| 283 |
+
return Receipt(hash=r["hash"], prev_hash=(None if r["prev"] == "GENESIS" else r["prev"]),
|
| 284 |
+
payload=r["payload"], signature=r["signature"],
|
| 285 |
+
signed_at=r["ts"], organ=r["organ"])
|
| 286 |
+
|
| 287 |
+
|
| 288 |
+
# Seed formula catalogue (kept minimal + honest; lean_proved/sorry_tagged real flags).
|
| 289 |
+
_FORMULAS = [
|
| 290 |
+
Formula(id="ouroboros-conservation", name="Ouroboros Conservation",
|
| 291 |
+
statement="forall s, intent(s) -> replay(s) preserves khipu_hash(s)",
|
| 292 |
+
lean_proved=True, sorry_tagged=False),
|
| 293 |
+
Formula(id="khipu-chain-integrity", name="Khipu Chain Integrity",
|
| 294 |
+
statement="forall i, hash(r_i) = H(hash(r_{i-1}) || payload_i)",
|
| 295 |
+
lean_proved=True, sorry_tagged=False),
|
| 296 |
+
Formula(id="wire-d-soundness", name="Wire D Soundness",
|
| 297 |
+
statement="verify(sign(m, k), pub(k)) = true",
|
| 298 |
+
lean_proved=False, sorry_tagged=True),
|
| 299 |
+
]
|
| 300 |
+
|
| 301 |
+
|
| 302 |
+
def build_schema() -> strawberry.Schema:
|
| 303 |
+
# strawberry.federation.Schema emits Apollo Federation v2 directives by
|
| 304 |
+
# default, letting each flagship publish its own subgraph and this gateway
|
| 305 |
+
# compose them.
|
| 306 |
+
return strawberry.federation.Schema(query=Query, mutation=Mutation)
|
| 307 |
+
|
| 308 |
+
|
| 309 |
+
schema = build_schema()
|