Spaces:
Running
Running
chore(sync): mirror backend .py + Dockerfile to Space (hf-sync-backend)
Browse filesAutomated backend sync from szl-holdings/a11oy main via hf-sync-backend.
Updated (differed from the Space): Dockerfile, serve.py, szl_corpus_publish.py, szl_energy_operator.py
Deleted (gone from the repo + Dockerfile COPY set): (none)
Keeps the Space-built backend (serve.py + the Dockerfile-COPY'd .py
modules) identical to GitHub main so the Space never rebuilds from a
stale backend, new endpoints don't 404 there, and orphaned modules
removed from the repo don't linger in the Space tree.
- Dockerfile +8 -0
- serve.py +21 -1
- szl_corpus_publish.py +92 -2
- szl_energy_operator.py +145 -4
Dockerfile
CHANGED
|
@@ -406,6 +406,14 @@ COPY szl3d_holographic.py ./szl3d_holographic.py
|
|
| 406 |
# sovereign in-image. Reuses the vendor3d Three.js r160 above — 0 CDN.
|
| 407 |
COPY cathedral_genius.html ./cathedral_genius.html
|
| 408 |
COPY static/cathedral_app.js ./static/cathedral_app.js
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 409 |
# ADDITIVE: batch-2 sovereign security data module (imported by serve.py; try/except-guarded).
|
| 410 |
# ADDITIVE: a11oy.code conversational orchestrator module (imported by serve.py).
|
| 411 |
# ADDITIVE (a11oy Code agentic core, 2026-06-10): the GENUINELY-agentic loop + agentic
|
|
|
|
| 406 |
# sovereign in-image. Reuses the vendor3d Three.js r160 above — 0 CDN.
|
| 407 |
COPY cathedral_genius.html ./cathedral_genius.html
|
| 408 |
COPY static/cathedral_app.js ./static/cathedral_app.js
|
| 409 |
+
# ADDITIVE (holographic front-door landing, Dev1): the governed-inference-field
|
| 410 |
+
# hero served at "/" by serve.py.spa_root (cathedral one click in at /cathedral,
|
| 411 |
+
# console at /console). a11oy_landing.html is the page; static/a11oy_landing.js is
|
| 412 |
+
# the ES module served at /landing/app.js. Reuses the vendor3d Three.js r160 above
|
| 413 |
+
# (MIT) via the page importmap — 0 runtime CDN. MUST be per-file COPY'd or "/"
|
| 414 |
+
# falls back to the cathedral/console. Doctrine v11 LOCKED; Λ = Conjecture 1.
|
| 415 |
+
COPY a11oy_landing.html ./a11oy_landing.html
|
| 416 |
+
COPY static/a11oy_landing.js ./static/a11oy_landing.js
|
| 417 |
# ADDITIVE: batch-2 sovereign security data module (imported by serve.py; try/except-guarded).
|
| 418 |
# ADDITIVE: a11oy.code conversational orchestrator module (imported by serve.py).
|
| 419 |
# ADDITIVE (a11oy Code agentic core, 2026-06-10): the GENUINELY-agentic loop + agentic
|
serve.py
CHANGED
|
@@ -8002,7 +8002,12 @@ async def spa_root():
|
|
| 8002 |
from starlette.responses import Response as _SPA_Resp
|
| 8003 |
_SPA_TAG = (b'<script src="/vendor/a11oy-operator-widget.js" '
|
| 8004 |
b'data-surface="a11oy" defer></script>')
|
| 8005 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8006 |
try:
|
| 8007 |
_cp = Path(_cand)
|
| 8008 |
if not _cp.is_file():
|
|
@@ -8079,6 +8084,21 @@ async def _cathedral_app_js() -> Response:
|
|
| 8079 |
# === end /cathedral canonical genius unification ===
|
| 8080 |
|
| 8081 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8082 |
|
| 8083 |
# --- Doctrine v13 organ page routes (ADDITIVE; explicit, win over SPA catch-all) ---
|
| 8084 |
PAGES_DIR = Path("/app/pages")
|
|
|
|
| 8002 |
from starlette.responses import Response as _SPA_Resp
|
| 8003 |
_SPA_TAG = (b'<script src="/vendor/a11oy-operator-widget.js" '
|
| 8004 |
b'data-surface="a11oy" defer></script>')
|
| 8005 |
+
# Front door order: the holographic landing first (governed-inference field,
|
| 8006 |
+
# vendored Three.js r160, live receipt/mesh weave-ins + in-browser WebCrypto
|
| 8007 |
+
# verify), then the cathedral hero, then the console SPA, then the SPA index.
|
| 8008 |
+
# All fallbacks preserved so a missing file never white-screens "/".
|
| 8009 |
+
for _cand in (Path("/app/a11oy_landing.html"), Path("/app/cathedral.html"),
|
| 8010 |
+
PAGES_DIR / "console.html", INDEX_HTML):
|
| 8011 |
try:
|
| 8012 |
_cp = Path(_cand)
|
| 8013 |
if not _cp.is_file():
|
|
|
|
| 8084 |
# === end /cathedral canonical genius unification ===
|
| 8085 |
|
| 8086 |
|
| 8087 |
+
# === ADDITIVE: holographic front-door landing ES module (Dev1) ===
|
| 8088 |
+
# /landing/app.js serves the governed-inference-field hero (instanced particles +
|
| 8089 |
+
# fresnel governance core). ES module, imports "three" via the importmap in
|
| 8090 |
+
# a11oy_landing.html -> /hero/vendor3d/three.module.min.js (vendored r160, MIT, 0 CDN).
|
| 8091 |
+
# Registered BEFORE the SPA /{full_path:path} catch-all so it wins the ordered match.
|
| 8092 |
+
# Signed-off-by: Stephen P. Lutar Jr. <stephenlutar2@gmail.com>
|
| 8093 |
+
@app.get("/landing/app.js")
|
| 8094 |
+
async def _landing_app_js() -> Response:
|
| 8095 |
+
f = Path("/app/static/a11oy_landing.js")
|
| 8096 |
+
if f.is_file():
|
| 8097 |
+
return FileResponse(str(f), media_type="application/javascript; charset=utf-8",
|
| 8098 |
+
headers={"Cache-Control": "public, max-age=3600"})
|
| 8099 |
+
return JSONResponse({"error": "landing app.js missing"}, status_code=404)
|
| 8100 |
+
|
| 8101 |
+
|
| 8102 |
|
| 8103 |
# --- Doctrine v13 organ page routes (ADDITIVE; explicit, win over SPA catch-all) ---
|
| 8104 |
PAGES_DIR = Path("/app/pages")
|
szl_corpus_publish.py
CHANGED
|
@@ -115,6 +115,84 @@ def _canon(obj: Any) -> bytes:
|
|
| 115 |
return json.dumps(obj, sort_keys=True, separators=(",", ":"), ensure_ascii=False).encode("utf-8")
|
| 116 |
|
| 117 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
# --------------------------------------------------------------------------- #
|
| 119 |
# Receipts
|
| 120 |
# --------------------------------------------------------------------------- #
|
|
@@ -235,6 +313,13 @@ def on_new_receipt(env: Dict[str, Any], *, extra: Optional[Dict[str, Any]] = Non
|
|
| 235 |
return {"ok": False, "skipped": "not-an-envelope"}
|
| 236 |
if not _is_real_signed(env):
|
| 237 |
return {"ok": True, "skipped": "unsigned-or-placeholder", "published": 0}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 238 |
bucket = _get_bucket(PREFIX_RECEIPT)
|
| 239 |
if bucket is None:
|
| 240 |
return {"ok": False, "skipped": "bucket-unavailable", "published": 0}
|
|
@@ -257,16 +342,21 @@ def backfill_receipts(envelopes: Iterable[Dict[str, Any]], *, flush: bool = True
|
|
| 257 |
bucket = _get_bucket(PREFIX_RECEIPT, start=False)
|
| 258 |
if bucket is None:
|
| 259 |
return {"ok": False, "error": "bucket-unavailable"}
|
| 260 |
-
queued = skipped = 0
|
| 261 |
for env in envelopes:
|
| 262 |
if not (isinstance(env, dict) and _is_real_signed(env)):
|
| 263 |
skipped += 1
|
| 264 |
continue
|
|
|
|
|
|
|
|
|
|
|
|
|
| 265 |
rec = make_receipt_record(env)
|
| 266 |
wrapped = bucket.make_record(rec, kind="receipt", source=SOURCE, dedup_key=rec["receipt_uid"])
|
| 267 |
res = bucket.append(wrapped, kind="receipt", source=SOURCE, auto_flush=False)
|
| 268 |
queued += res.get("queued", 0)
|
| 269 |
-
out: Dict[str, Any] = {"ok": True, "queued": queued, "skipped_unsigned": skipped
|
|
|
|
| 270 |
if flush:
|
| 271 |
out["flush"] = bucket.flush_queue(force=True)
|
| 272 |
return out
|
|
|
|
| 115 |
return json.dumps(obj, sort_keys=True, separators=(",", ":"), ensure_ascii=False).encode("utf-8")
|
| 116 |
|
| 117 |
|
| 118 |
+
# --------------------------------------------------------------------------- #
|
| 119 |
+
# Verify-before-publish gate
|
| 120 |
+
# --------------------------------------------------------------------------- #
|
| 121 |
+
# The published corpus advertises that every ecdsa-p256-dsse-pae receipt verifies
|
| 122 |
+
# against ONE pinned cosign.pub — the same key the CI re-verify guard checks
|
| 123 |
+
# (.github/hf-corpus-guards.json -> cosign_pub_pem). Incident #325: two receipts
|
| 124 |
+
# signed by a transient/rotated key (matching the live org cosign.pub, not the
|
| 125 |
+
# pinned one) were published and could no longer re-verify. To make that
|
| 126 |
+
# impossible going forward, the producer now re-verifies each signed envelope
|
| 127 |
+
# against the SAME pinned key before publishing; an envelope that does not verify
|
| 128 |
+
# is skipped (honestly, like an UNSIGNED one) — never published.
|
| 129 |
+
#
|
| 130 |
+
# The pinned PEM is read from the guard config so producer and guard share one
|
| 131 |
+
# source of truth; the embedded copy is only a fallback for a runtime that does
|
| 132 |
+
# not ship the .github config. Keep it in sync with that file's cosign_pub_pem.
|
| 133 |
+
_CORPUS_COSIGN_PUB_PEM_FALLBACK = (
|
| 134 |
+
"-----BEGIN PUBLIC KEY-----\n"
|
| 135 |
+
"MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE/Jlv9FnwJ13l4QIZpr4IbTBUtVZ2\n"
|
| 136 |
+
"i+O7Jai/s7xsdXvOjmZGYhd36VxNQQahTSjWoYpPrSNhXbt/n7lsgi61xA==\n"
|
| 137 |
+
"-----END PUBLIC KEY-----\n"
|
| 138 |
+
)
|
| 139 |
+
|
| 140 |
+
|
| 141 |
+
def _corpus_verify_pub_pem() -> str:
|
| 142 |
+
"""Return the pinned cosign.pub the corpus re-verifies against. Reads the CI
|
| 143 |
+
guard config (single source of truth) and falls back to the embedded copy."""
|
| 144 |
+
cfg_path = os.path.join(
|
| 145 |
+
os.path.dirname(os.path.abspath(__file__)),
|
| 146 |
+
".github", "hf-corpus-guards.json")
|
| 147 |
+
try:
|
| 148 |
+
with open(cfg_path, "r", encoding="utf-8") as fh:
|
| 149 |
+
pem = json.load(fh).get("cosign_pub_pem")
|
| 150 |
+
if isinstance(pem, str) and "BEGIN PUBLIC KEY" in pem:
|
| 151 |
+
return pem
|
| 152 |
+
except Exception:
|
| 153 |
+
pass
|
| 154 |
+
return _CORPUS_COSIGN_PUB_PEM_FALLBACK
|
| 155 |
+
|
| 156 |
+
|
| 157 |
+
def _ecdsa_envelope_verifies(env: Dict[str, Any], pub_pem: str) -> bool:
|
| 158 |
+
"""True iff at least one of the envelope's signatures verifies over its DSSE
|
| 159 |
+
PAE against pub_pem. Conservative: any error (incl. missing cryptography)
|
| 160 |
+
returns False so an envelope we cannot vouch for is NOT published."""
|
| 161 |
+
try:
|
| 162 |
+
import base64
|
| 163 |
+
from cryptography.hazmat.primitives.serialization import load_pem_public_key
|
| 164 |
+
from cryptography.hazmat.primitives import hashes
|
| 165 |
+
from cryptography.hazmat.primitives.asymmetric import ec
|
| 166 |
+
from cryptography.exceptions import InvalidSignature
|
| 167 |
+
except Exception:
|
| 168 |
+
return False
|
| 169 |
+
try:
|
| 170 |
+
body = base64.b64decode(env.get("payload", "") or b"")
|
| 171 |
+
pt = str(env.get("payloadType", ""))
|
| 172 |
+
pae = b"DSSEv1 %d %s %d %s" % (len(pt.encode()), pt.encode(), len(body), body)
|
| 173 |
+
pub = load_pem_public_key(pub_pem.encode("utf-8"))
|
| 174 |
+
for s in env.get("signatures") or []:
|
| 175 |
+
try:
|
| 176 |
+
pub.verify(base64.b64decode(s.get("sig", "") or ""), pae,
|
| 177 |
+
ec.ECDSA(hashes.SHA256()))
|
| 178 |
+
return True
|
| 179 |
+
except InvalidSignature:
|
| 180 |
+
continue
|
| 181 |
+
return False
|
| 182 |
+
except Exception:
|
| 183 |
+
return False
|
| 184 |
+
|
| 185 |
+
|
| 186 |
+
def _publishable_against_corpus_key(env: Dict[str, Any], scheme: str) -> bool:
|
| 187 |
+
"""Verify-before-publish gate. ecdsa-p256-dsse-pae receipts are published only
|
| 188 |
+
if they verify against the pinned corpus cosign.pub, so a transient/rotated-key
|
| 189 |
+
envelope can never enter the corpus and later fail re-verify. sigstore-keyless
|
| 190 |
+
receipts are not gated here (verified via their Fulcio cert at re-verify time)."""
|
| 191 |
+
if scheme != "ecdsa-p256-dsse-pae":
|
| 192 |
+
return True
|
| 193 |
+
return _ecdsa_envelope_verifies(env, _corpus_verify_pub_pem())
|
| 194 |
+
|
| 195 |
+
|
| 196 |
# --------------------------------------------------------------------------- #
|
| 197 |
# Receipts
|
| 198 |
# --------------------------------------------------------------------------- #
|
|
|
|
| 313 |
return {"ok": False, "skipped": "not-an-envelope"}
|
| 314 |
if not _is_real_signed(env):
|
| 315 |
return {"ok": True, "skipped": "unsigned-or-placeholder", "published": 0}
|
| 316 |
+
scheme = _detect_scheme(env)
|
| 317 |
+
if not _publishable_against_corpus_key(env, scheme):
|
| 318 |
+
# Genuinely signed but NOT against the pinned corpus key (e.g. a
|
| 319 |
+
# transient/rotated key). Refuse to publish rather than poison the
|
| 320 |
+
# corpus with an envelope that would later fail re-verify (#325).
|
| 321 |
+
return {"ok": True, "skipped": "signature-not-verifiable-against-corpus-key",
|
| 322 |
+
"published": 0, "scheme": scheme}
|
| 323 |
bucket = _get_bucket(PREFIX_RECEIPT)
|
| 324 |
if bucket is None:
|
| 325 |
return {"ok": False, "skipped": "bucket-unavailable", "published": 0}
|
|
|
|
| 342 |
bucket = _get_bucket(PREFIX_RECEIPT, start=False)
|
| 343 |
if bucket is None:
|
| 344 |
return {"ok": False, "error": "bucket-unavailable"}
|
| 345 |
+
queued = skipped = skipped_unverifiable = 0
|
| 346 |
for env in envelopes:
|
| 347 |
if not (isinstance(env, dict) and _is_real_signed(env)):
|
| 348 |
skipped += 1
|
| 349 |
continue
|
| 350 |
+
if not _publishable_against_corpus_key(env, _detect_scheme(env)):
|
| 351 |
+
# signed, but not against the pinned corpus key -> never publish (#325)
|
| 352 |
+
skipped_unverifiable += 1
|
| 353 |
+
continue
|
| 354 |
rec = make_receipt_record(env)
|
| 355 |
wrapped = bucket.make_record(rec, kind="receipt", source=SOURCE, dedup_key=rec["receipt_uid"])
|
| 356 |
res = bucket.append(wrapped, kind="receipt", source=SOURCE, auto_flush=False)
|
| 357 |
queued += res.get("queued", 0)
|
| 358 |
+
out: Dict[str, Any] = {"ok": True, "queued": queued, "skipped_unsigned": skipped,
|
| 359 |
+
"skipped_unverifiable": skipped_unverifiable}
|
| 360 |
if flush:
|
| 361 |
out["flush"] = bucket.flush_queue(force=True)
|
| 362 |
return out
|
szl_energy_operator.py
CHANGED
|
@@ -130,6 +130,14 @@ THROTTLE_SLEEP_MULT_ENV = "A11OY_ENERGY_THROTTLE_SLEEP_MULT"
|
|
| 130 |
_THROTTLE_SLEEP_MULT_DEFAULT = 4.0
|
| 131 |
# Forced posture override for demos/tests (soak|baseline|throttle). Empty => live.
|
| 132 |
FORCE_POSTURE_ENV = "A11OY_ENERGY_FORCE_POSTURE"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
# The harvest posture feed (a11oy_harvest_endpoints.handle_posture) can hit live
|
| 134 |
# external energy feeds, so it is refreshed at most once per this TTL — NEVER on
|
| 135 |
# every sweep. Keeps the hot loop gentle (a fast inter-job interval must not turn
|
|
@@ -148,6 +156,116 @@ def _useful_work_enabled() -> bool:
|
|
| 148 |
not in ("0", "false", "no", "off", ""))
|
| 149 |
|
| 150 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 151 |
def _price_expensive_threshold() -> float:
|
| 152 |
try:
|
| 153 |
return float(os.environ.get(PRICE_EXPENSIVE_ENV, _PRICE_EXPENSIVE_DEFAULT))
|
|
@@ -509,6 +627,15 @@ class JobRecord:
|
|
| 509 |
# contract is unchanged — these are extra optional fields with safe defaults.
|
| 510 |
useful_work: bool = False
|
| 511 |
rag_chunk_id: Optional[str] = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 512 |
|
| 513 |
def to_dict(self) -> dict:
|
| 514 |
return {
|
|
@@ -519,6 +646,7 @@ class JobRecord:
|
|
| 519 |
"joules_label": self.joules_label, "joules_evidence": self.joules_evidence,
|
| 520 |
"ts": self.ts, "seq": self.seq,
|
| 521 |
"useful_work": self.useful_work, "rag_chunk_id": self.rag_chunk_id,
|
|
|
|
| 522 |
}
|
| 523 |
|
| 524 |
|
|
@@ -1052,7 +1180,8 @@ class OperatorDaemon:
|
|
| 1052 |
wall_s: float, exporter_sample: Optional[dict],
|
| 1053 |
joules_measured: Optional[float], *,
|
| 1054 |
useful_work: bool = False,
|
| 1055 |
-
rag_chunk_id: Optional[str] = None
|
|
|
|
| 1056 |
now = time.time()
|
| 1057 |
label = _label_upper(exporter_sample, now=now)
|
| 1058 |
evidence = _J.joules_evidence(exporter_sample, now=now) if label == LABEL_MEASURED else {}
|
|
@@ -1084,7 +1213,7 @@ class OperatorDaemon:
|
|
| 1084 |
node=node_name, model=model, kind=kind, tokens=int(tokens), wall_s=wall_s,
|
| 1085 |
joules_measured=billable_j,
|
| 1086 |
joules_label=label, joules_evidence=evidence, ts=_now_iso(), seq=seq,
|
| 1087 |
-
useful_work=useful_work, rag_chunk_id=rag_chunk_id)
|
| 1088 |
self._emit(rec)
|
| 1089 |
return rec
|
| 1090 |
|
|
@@ -1113,12 +1242,15 @@ class OperatorDaemon:
|
|
| 1113 |
j_before = (sample_before or {}).get("joules_measured_total")
|
| 1114 |
rag_chunk_id: Optional[str] = None
|
| 1115 |
embed_vec: list[float] = []
|
|
|
|
| 1116 |
t0 = time.time()
|
| 1117 |
try:
|
| 1118 |
if kind == "generate":
|
| 1119 |
prompt = _GEN_PROMPTS[self._state.seq % len(_GEN_PROMPTS)]
|
| 1120 |
-
tokens,
|
| 1121 |
model = node.gen_model
|
|
|
|
|
|
|
| 1122 |
else:
|
| 1123 |
# USEFUL WORK: embed a REAL un-embedded corpus chunk when available
|
| 1124 |
# (honest fallback to canned text otherwise). The joule meter window
|
|
@@ -1127,6 +1259,7 @@ class OperatorDaemon:
|
|
| 1127 |
text, rag_chunk_id = self._pick_corpus_embed_job()
|
| 1128 |
tokens, embed_vec = _ollama_embed(node.base_url, node.embed_model, text)
|
| 1129 |
model = node.embed_model
|
|
|
|
| 1130 |
except Exception: # noqa: BLE001 — node failed mid-job: DEGRADED, never faked
|
| 1131 |
with self._lock:
|
| 1132 |
self._node_status[node.name] = "DEGRADED"
|
|
@@ -1149,11 +1282,18 @@ class OperatorDaemon:
|
|
| 1149 |
stored = False
|
| 1150 |
if rag_chunk_id is not None and embed_vec:
|
| 1151 |
stored = self._store_rag_vector(rag_chunk_id, embed_vec)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1152 |
# The label is decided off the AFTER sample (the fresh reading at job end).
|
| 1153 |
rec = self._commit(node.name, model, kind, tokens, wall_s,
|
| 1154 |
sample_after, joules_measured,
|
| 1155 |
useful_work=stored,
|
| 1156 |
-
rag_chunk_id=(rag_chunk_id if stored else None)
|
|
|
|
| 1157 |
return rec.to_dict()
|
| 1158 |
|
| 1159 |
def _run_corpus_embed_batch(self, node: NodeCfg, meter_before: Optional[dict],
|
|
@@ -1264,6 +1404,7 @@ class OperatorDaemon:
|
|
| 1264 |
"live RAG dense index. Useful work changes WHAT is computed, "
|
| 1265 |
"never HOW joules are measured — the MEASURED gate is unchanged."
|
| 1266 |
),
|
|
|
|
| 1267 |
"recent_jobs": [_public_job(j) for j in self._last_records[-10:]],
|
| 1268 |
"exporter": _JOULE_METER_PUBLIC,
|
| 1269 |
"honesty": (
|
|
|
|
| 130 |
_THROTTLE_SLEEP_MULT_DEFAULT = 4.0
|
| 131 |
# Forced posture override for demos/tests (soak|baseline|throttle). Empty => live.
|
| 132 |
FORCE_POSTURE_ENV = "A11OY_ENERGY_FORCE_POSTURE"
|
| 133 |
+
# Governed-compute switch (default ON). When ON, each completed REAL GPU job is run
|
| 134 |
+
# through the EXISTING governed turn (a11oy_vertical_feeds.governed_turn: Λ aggregator
|
| 135 |
+
# + locked formulas + deny-by-default gates, sealing a Khipu/DSSE receipt into the Lake)
|
| 136 |
+
# and the honest result is attached to the JobRecord as ADDITIVE governance metadata.
|
| 137 |
+
# This NEVER touches how joules are MEASURED. Off => the prior byte-identical job path.
|
| 138 |
+
GOVERN_COMPUTE_ENV = "A11OY_GOVERN_COMPUTE"
|
| 139 |
+
# Governance vertical/organ the GPU jobs are sealed under (its own Lake DAG).
|
| 140 |
+
GOVERN_VERTICAL = "sovereign-compute"
|
| 141 |
# The harvest posture feed (a11oy_harvest_endpoints.handle_posture) can hit live
|
| 142 |
# external energy feeds, so it is refreshed at most once per this TTL — NEVER on
|
| 143 |
# every sweep. Keeps the hot loop gentle (a fast inter-job interval must not turn
|
|
|
|
| 156 |
not in ("0", "false", "no", "off", ""))
|
| 157 |
|
| 158 |
|
| 159 |
+
def _govern_enabled() -> bool:
|
| 160 |
+
"""True unless A11OY_GOVERN_COMPUTE is explicitly falsey. Default ON."""
|
| 161 |
+
return (os.environ.get(GOVERN_COMPUTE_ENV, "1").strip().lower()
|
| 162 |
+
not in ("0", "false", "no", "off", ""))
|
| 163 |
+
|
| 164 |
+
|
| 165 |
+
def _ungoverned(reason: str) -> dict:
|
| 166 |
+
"""Honest ungoverned governance record — STABLE schema, NO fabricated Λ/receipt."""
|
| 167 |
+
return {
|
| 168 |
+
"governed": False,
|
| 169 |
+
"lambda_score": None,
|
| 170 |
+
"lambda_pass": None,
|
| 171 |
+
"decision": None,
|
| 172 |
+
"receipt_id": None,
|
| 173 |
+
"dsse_keyid": None,
|
| 174 |
+
"dsse_signed": False,
|
| 175 |
+
"doctrine_lambda": None,
|
| 176 |
+
"vertical": GOVERN_VERTICAL,
|
| 177 |
+
"reason": reason,
|
| 178 |
+
}
|
| 179 |
+
|
| 180 |
+
|
| 181 |
+
def _govern_turn(kind: str, text: str, model: str,
|
| 182 |
+
node_name: str) -> dict:
|
| 183 |
+
"""Run a COMPLETED GPU job through the EXISTING governed turn as ADDITIVE metadata.
|
| 184 |
+
|
| 185 |
+
This is the governed-compute boundary: it imports + calls the real
|
| 186 |
+
`a11oy_vertical_feeds.governed_turn` (which scores the turn through the Λ aggregator
|
| 187 |
+
+ locked formulas + deny-by-default gates, then seals a Khipu receipt into the Lake
|
| 188 |
+
and signs a DSSE envelope) and distils the honest result.
|
| 189 |
+
|
| 190 |
+
Doctrine v11 — NEVER fabricate: any failure (disabled / module absent / call error /
|
| 191 |
+
receipt not sealed) returns an honest ungoverned record (governed=False, lambda_score
|
| 192 |
+
None, receipt_id None) with a reason. A job that could not be governed says so; it is
|
| 193 |
+
never faked as governed. The return schema is STABLE (same keys whether governed or
|
| 194 |
+
not). This function does NOT touch energy measurement and never raises."""
|
| 195 |
+
if not _govern_enabled():
|
| 196 |
+
return _ungoverned(f"disabled ({GOVERN_COMPUTE_ENV}=0)")
|
| 197 |
+
try:
|
| 198 |
+
import a11oy_vertical_feeds as _VF # type: ignore
|
| 199 |
+
except Exception as e: # noqa: BLE001 — governance optional; absence stays honest
|
| 200 |
+
return _ungoverned(f"governance modules unavailable: {type(e).__name__}")
|
| 201 |
+
try:
|
| 202 |
+
res = _VF.governed_turn(
|
| 203 |
+
vertical=GOVERN_VERTICAL,
|
| 204 |
+
text=text or "",
|
| 205 |
+
action_kind=f"gpu-{kind}",
|
| 206 |
+
context={"task": "energy-operator", "model": model, "node": node_name},
|
| 207 |
+
)
|
| 208 |
+
except Exception as e: # noqa: BLE001 — never let governance break the job path
|
| 209 |
+
return _ungoverned(f"governed_turn failed: {type(e).__name__}")
|
| 210 |
+
if not isinstance(res, dict):
|
| 211 |
+
return _ungoverned("governed_turn returned non-dict")
|
| 212 |
+
lam = res.get("lambda")
|
| 213 |
+
receipt = res.get("receipt") if isinstance(res.get("receipt"), dict) else {}
|
| 214 |
+
receipt_id = receipt.get("digest")
|
| 215 |
+
dsse = res.get("dsse") if isinstance(res.get("dsse"), dict) else {}
|
| 216 |
+
keyid = None
|
| 217 |
+
sigs = dsse.get("signatures")
|
| 218 |
+
if isinstance(sigs, list) and sigs and isinstance(sigs[0], dict):
|
| 219 |
+
keyid = sigs[0].get("keyid")
|
| 220 |
+
doctrine = res.get("doctrine") if isinstance(res.get("doctrine"), dict) else {}
|
| 221 |
+
# Fully governed ONLY when a real Λ score AND a real sealed receipt id are present.
|
| 222 |
+
fully = (isinstance(lam, (int, float)) and bool(receipt_id))
|
| 223 |
+
out = {
|
| 224 |
+
"governed": fully,
|
| 225 |
+
"lambda_score": lam if isinstance(lam, (int, float)) else None,
|
| 226 |
+
"lambda_pass": res.get("lambda_pass"),
|
| 227 |
+
"decision": res.get("decision"),
|
| 228 |
+
"receipt_id": receipt_id,
|
| 229 |
+
"dsse_keyid": keyid,
|
| 230 |
+
"dsse_signed": bool(dsse.get("signed")),
|
| 231 |
+
"doctrine_lambda": doctrine.get("lambda"),
|
| 232 |
+
"vertical": res.get("vertical"),
|
| 233 |
+
}
|
| 234 |
+
if not fully:
|
| 235 |
+
out["reason"] = ("Λ scored but receipt not sealed"
|
| 236 |
+
if isinstance(lam, (int, float))
|
| 237 |
+
else "governed_turn returned no Λ score")
|
| 238 |
+
return out
|
| 239 |
+
|
| 240 |
+
|
| 241 |
+
def _governed_compute_summary(recent_records: list[dict]) -> dict:
|
| 242 |
+
"""HONEST, observable governed-compute block for status(). Derived from the rolling
|
| 243 |
+
tail of recent JobRecords (no fabricated aggregate). Reports whether governance is
|
| 244 |
+
enabled, how many of the recent jobs were FULLY governed (real Λ + sealed receipt),
|
| 245 |
+
and the most recent governance result (Λ score, receipt id, dsse keyid) so a reader
|
| 246 |
+
can see a GPU job was a governed turn — or honestly was not."""
|
| 247 |
+
gov_records = [r for r in recent_records
|
| 248 |
+
if isinstance(r, dict) and isinstance(r.get("governance"), dict)]
|
| 249 |
+
fully = [g for g in gov_records if g["governance"].get("governed")]
|
| 250 |
+
last = gov_records[-1]["governance"] if gov_records else None
|
| 251 |
+
return {
|
| 252 |
+
"enabled": _govern_enabled(),
|
| 253 |
+
"vertical": GOVERN_VERTICAL,
|
| 254 |
+
"recent_window": len(recent_records),
|
| 255 |
+
"recent_governed": len(fully),
|
| 256 |
+
"recent_attempted": len(gov_records),
|
| 257 |
+
"last": last,
|
| 258 |
+
"note": (
|
| 259 |
+
"Each completed GPU job is ADDITIONALLY run through the EXISTING governed "
|
| 260 |
+
"turn (a11oy_vertical_feeds.governed_turn: Λ aggregator + locked formulas + "
|
| 261 |
+
"deny-by-default gates, sealing a Khipu/DSSE receipt into the Lake). Λ is "
|
| 262 |
+
"Conjecture 1 — never a theorem. A job that could not be governed is honestly "
|
| 263 |
+
"labeled governed=False with NO fabricated Λ score or receipt. Governance is "
|
| 264 |
+
"metadata only; it NEVER changes how joules are MEASURED."
|
| 265 |
+
),
|
| 266 |
+
}
|
| 267 |
+
|
| 268 |
+
|
| 269 |
def _price_expensive_threshold() -> float:
|
| 270 |
try:
|
| 271 |
return float(os.environ.get(PRICE_EXPENSIVE_ENV, _PRICE_EXPENSIVE_DEFAULT))
|
|
|
|
| 627 |
# contract is unchanged — these are extra optional fields with safe defaults.
|
| 628 |
useful_work: bool = False
|
| 629 |
rag_chunk_id: Optional[str] = None
|
| 630 |
+
# ADDITIVE (governed-compute): the honest result of running THIS completed GPU job
|
| 631 |
+
# through the existing governed turn (Λ aggregator + locked formulas + Khipu/DSSE
|
| 632 |
+
# receipt sealed into the Lake). None when governance was not attempted; when
|
| 633 |
+
# attempted it is a dict carrying governed(bool), lambda_score, decision, receipt_id,
|
| 634 |
+
# dsse_keyid/dsse_signed and the Conjecture-1 doctrine label — or governed=False + a
|
| 635 |
+
# reason when it could not be governed (NEVER a fabricated score/receipt). Energy
|
| 636 |
+
# measurement is unaffected; this is metadata only. Dev2/3/4 contract is unchanged
|
| 637 |
+
# (extra optional field, safe default).
|
| 638 |
+
governance: Optional[dict] = None
|
| 639 |
|
| 640 |
def to_dict(self) -> dict:
|
| 641 |
return {
|
|
|
|
| 646 |
"joules_label": self.joules_label, "joules_evidence": self.joules_evidence,
|
| 647 |
"ts": self.ts, "seq": self.seq,
|
| 648 |
"useful_work": self.useful_work, "rag_chunk_id": self.rag_chunk_id,
|
| 649 |
+
"governance": self.governance,
|
| 650 |
}
|
| 651 |
|
| 652 |
|
|
|
|
| 1180 |
wall_s: float, exporter_sample: Optional[dict],
|
| 1181 |
joules_measured: Optional[float], *,
|
| 1182 |
useful_work: bool = False,
|
| 1183 |
+
rag_chunk_id: Optional[str] = None,
|
| 1184 |
+
governance: Optional[dict] = None) -> JobRecord:
|
| 1185 |
now = time.time()
|
| 1186 |
label = _label_upper(exporter_sample, now=now)
|
| 1187 |
evidence = _J.joules_evidence(exporter_sample, now=now) if label == LABEL_MEASURED else {}
|
|
|
|
| 1213 |
node=node_name, model=model, kind=kind, tokens=int(tokens), wall_s=wall_s,
|
| 1214 |
joules_measured=billable_j,
|
| 1215 |
joules_label=label, joules_evidence=evidence, ts=_now_iso(), seq=seq,
|
| 1216 |
+
useful_work=useful_work, rag_chunk_id=rag_chunk_id, governance=governance)
|
| 1217 |
self._emit(rec)
|
| 1218 |
return rec
|
| 1219 |
|
|
|
|
| 1242 |
j_before = (sample_before or {}).get("joules_measured_total")
|
| 1243 |
rag_chunk_id: Optional[str] = None
|
| 1244 |
embed_vec: list[float] = []
|
| 1245 |
+
governed_text = "" # the turn text handed to the governed turn (post-meter)
|
| 1246 |
t0 = time.time()
|
| 1247 |
try:
|
| 1248 |
if kind == "generate":
|
| 1249 |
prompt = _GEN_PROMPTS[self._state.seq % len(_GEN_PROMPTS)]
|
| 1250 |
+
tokens, completion = _ollama_generate(node.base_url, node.gen_model, prompt)
|
| 1251 |
model = node.gen_model
|
| 1252 |
+
# The governed turn scores the full turn (prompt + completion).
|
| 1253 |
+
governed_text = f"{prompt}\n{completion}".strip()
|
| 1254 |
else:
|
| 1255 |
# USEFUL WORK: embed a REAL un-embedded corpus chunk when available
|
| 1256 |
# (honest fallback to canned text otherwise). The joule meter window
|
|
|
|
| 1259 |
text, rag_chunk_id = self._pick_corpus_embed_job()
|
| 1260 |
tokens, embed_vec = _ollama_embed(node.base_url, node.embed_model, text)
|
| 1261 |
model = node.embed_model
|
| 1262 |
+
governed_text = (text or "").strip()
|
| 1263 |
except Exception: # noqa: BLE001 — node failed mid-job: DEGRADED, never faked
|
| 1264 |
with self._lock:
|
| 1265 |
self._node_status[node.name] = "DEGRADED"
|
|
|
|
| 1282 |
stored = False
|
| 1283 |
if rag_chunk_id is not None and embed_vec:
|
| 1284 |
stored = self._store_rag_vector(rag_chunk_id, embed_vec)
|
| 1285 |
+
# GOVERNED COMPUTE (ADDITIVE): run the completed turn through the EXISTING governed
|
| 1286 |
+
# turn (Λ aggregator + locked formulas + Khipu/DSSE receipt sealed into the Lake).
|
| 1287 |
+
# This is OUTSIDE the metered window (after meter_after) and CANNOT change the joule
|
| 1288 |
+
# label/billable energy — governance is metadata only. Honest: a failure yields
|
| 1289 |
+
# governed=False with no fabricated Λ/receipt; the job still records exactly as before.
|
| 1290 |
+
governance = _govern_turn(kind, governed_text, model, node.name)
|
| 1291 |
# The label is decided off the AFTER sample (the fresh reading at job end).
|
| 1292 |
rec = self._commit(node.name, model, kind, tokens, wall_s,
|
| 1293 |
sample_after, joules_measured,
|
| 1294 |
useful_work=stored,
|
| 1295 |
+
rag_chunk_id=(rag_chunk_id if stored else None),
|
| 1296 |
+
governance=governance)
|
| 1297 |
return rec.to_dict()
|
| 1298 |
|
| 1299 |
def _run_corpus_embed_batch(self, node: NodeCfg, meter_before: Optional[dict],
|
|
|
|
| 1404 |
"live RAG dense index. Useful work changes WHAT is computed, "
|
| 1405 |
"never HOW joules are measured — the MEASURED gate is unchanged."
|
| 1406 |
),
|
| 1407 |
+
"governed_compute": _governed_compute_summary(self._last_records),
|
| 1408 |
"recent_jobs": [_public_job(j) for j in self._last_records[-10:]],
|
| 1409 |
"exporter": _JOULE_METER_PUBLIC,
|
| 1410 |
"honesty": (
|