Spaces:
Build error
Build error
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): szl_be_hardening.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.
- szl_be_hardening.py +62 -0
szl_be_hardening.py
CHANGED
|
@@ -773,6 +773,68 @@ def harden(app: Any, organ: str, ns: Optional[str] = None,
|
|
| 773 |
"energy_ledger is null when no ledger is reachable."),
|
| 774 |
}
|
| 775 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 776 |
report["registered"].append(
|
| 777 |
"assurance(artifact,credential,compliance,attest)+forge/ledger")
|
| 778 |
report["ok"] = True
|
|
|
|
| 773 |
"energy_ledger is null when no ledger is reachable."),
|
| 774 |
}
|
| 775 |
|
| 776 |
+
# ---- 11: cheapest-watt placement (carbon/cost-aware routing) ----------
|
| 777 |
+
# Reads the LIVE energy-operator status (per-node MEASURED joules + tokens +
|
| 778 |
+
# power_w + live grid €/MWh) and runs the cheapest-watt placement policy:
|
| 779 |
+
# pick the sovereign node minimizing energy-cost-per-token, record the decision
|
| 780 |
+
# (chosen node, €/MWh at decision time, MEASURED J/token, cheaper-than-alternative
|
| 781 |
+
# delta) into a re-hashable, hash-chained receipt, optionally DSSE-signed. Honest:
|
| 782 |
+
# with <2 comparable MEASURED nodes -> "no placement choice this tick"; a saving is
|
| 783 |
+
# MEASURED only when both legs are MEASURED; never fabricates a price or a saving.
|
| 784 |
+
def _operator_status_for_cw() -> Tuple[Optional[Dict[str, Any]], str]:
|
| 785 |
+
"""Live in-process operator status (MEASURED), else persisted ledger, else
|
| 786 |
+
(None, unavailable). Read-only; reuses the same source as /forge/ledger."""
|
| 787 |
+
return _energy_ledger()
|
| 788 |
+
|
| 789 |
+
@app.get(f"{base}/energy/cheapest-watt", tags=["energy"])
|
| 790 |
+
async def _cheapest_watt():
|
| 791 |
+
try:
|
| 792 |
+
import szl_cheapest_watt as _cw
|
| 793 |
+
except Exception as exc: # module not in image: STRUCTURAL-ONLY, never faked
|
| 794 |
+
return {"organ": organ, "data_kind": "structural",
|
| 795 |
+
"honesty": (f"szl_cheapest_watt unavailable ({type(exc).__name__}); "
|
| 796 |
+
"cheapest-watt is STRUCTURAL-ONLY — never fabricated.")}
|
| 797 |
+
status, src = _operator_status_for_cw()
|
| 798 |
+
led = _cw.get_ledger()
|
| 799 |
+
decision_receipt: Optional[Dict[str, Any]] = None
|
| 800 |
+
if status is not None:
|
| 801 |
+
# Record ONE fresh placement decision against the live status this read.
|
| 802 |
+
decision_receipt = led.record(status)
|
| 803 |
+
# Layer a REAL DSSE signature over the placement receipt when a cosign key
|
| 804 |
+
# is present; absent a key the receipt is honest-but-UNSIGNED (never faked).
|
| 805 |
+
try:
|
| 806 |
+
import szl_dsse as _dsse
|
| 807 |
+
env = _dsse.sign_payload(
|
| 808 |
+
decision_receipt,
|
| 809 |
+
payload_type="https://szl-holdings.dev/attestations/cheapest-watt-placement/v1",
|
| 810 |
+
)
|
| 811 |
+
decision_receipt = {**decision_receipt, "dsse": env,
|
| 812 |
+
"signed": True}
|
| 813 |
+
except Exception:
|
| 814 |
+
decision_receipt = {**decision_receipt, "dsse": None,
|
| 815 |
+
"signed": False,
|
| 816 |
+
"sign_note": ("no cosign private key in this runtime; "
|
| 817 |
+
"receipt is REAL + re-hashable but UNSIGNED "
|
| 818 |
+
"— never faked")}
|
| 819 |
+
body = led.status()
|
| 820 |
+
body.update({
|
| 821 |
+
"organ": organ,
|
| 822 |
+
"git_sha": os.getenv("SZL_GIT_SHA", "unknown"),
|
| 823 |
+
"operator_source": src,
|
| 824 |
+
"data_kind": "live" if status is not None else "structural",
|
| 825 |
+
"latest_decision": decision_receipt,
|
| 826 |
+
"reads": "GET re-evaluates against the live operator status and appends one decision",
|
| 827 |
+
})
|
| 828 |
+
if status is None:
|
| 829 |
+
body["honesty_no_operator"] = (
|
| 830 |
+
f"no live operator status reachable ({src}); no placement decision this "
|
| 831 |
+
"read — never fabricated.")
|
| 832 |
+
return body
|
| 833 |
+
|
| 834 |
+
# canonical path /api/<organ>/v1/energy/cheapest-watt registered above (same
|
| 835 |
+
# /api/<organ>/v1/energy prefix the operator's status/ledger/projection use).
|
| 836 |
+
report["registered"].append("energy/cheapest-watt(placement+signed-receipt)")
|
| 837 |
+
|
| 838 |
report["registered"].append(
|
| 839 |
"assurance(artifact,credential,compliance,attest)+forge/ledger")
|
| 840 |
report["ok"] = True
|