Spaces:
Paused
Paused
Download src/coflow5/synapse/observer.py from REBELLION2002/traffix-live: direct link, hf CLI and curl.
- Browser
- Download file 2.14 kB
-
https://huggingface.co/spaces/REBELLION2002/traffix-live/resolve/main/src/coflow5/synapse/observer.py
- Command line
-
hf download hf://spaces/REBELLION2002/traffix-live/src/coflow5/synapse/observer.py
-
curl -L -o observer.py https://huggingface.co/spaces/REBELLION2002/traffix-live/resolve/main/src/coflow5/synapse/observer.py
2.14 kB
| from __future__ import annotations | |
| import hashlib | |
| import json | |
| from dataclasses import dataclass | |
| from typing import Protocol, Sequence | |
| class ImmutableDecisionView: | |
| run_id: str | |
| scenario_hash: str | |
| event_id: str | |
| simulation_time: float | |
| selected_action: str | |
| reason_code: str | |
| class ObservationReceipt: | |
| event_id: str | |
| observer_available: bool | |
| explanation_status: str | |
| failure_type: str | None = None | |
| failure_detail: str | None = None | |
| class ExplanationObserver(Protocol): | |
| def observe(self, decision: ImmutableDecisionView) -> str: ... | |
| class ExplanationObserverBoundary: | |
| """Dispatches an immutable post-decision copy; it has no control callback.""" | |
| def __init__(self, observer: ExplanationObserver | None) -> None: | |
| self._observer = observer | |
| def observe_after_decision(self, decision: ImmutableDecisionView) -> ObservationReceipt: | |
| if self._observer is None: | |
| return ObservationReceipt( | |
| event_id=decision.event_id, | |
| observer_available=False, | |
| explanation_status="unavailable", | |
| failure_type="ObserverUnavailable", | |
| failure_detail="Synapse observer is killed or not configured", | |
| ) | |
| try: | |
| status = self._observer.observe(decision) | |
| except Exception as exc: | |
| return ObservationReceipt( | |
| event_id=decision.event_id, | |
| observer_available=False, | |
| explanation_status="failed-visible", | |
| failure_type=type(exc).__name__, | |
| failure_detail=str(exc) or type(exc).__name__, | |
| ) | |
| return ObservationReceipt( | |
| event_id=decision.event_id, | |
| observer_available=True, | |
| explanation_status=status, | |
| ) | |
| def action_sequence_hash(actions: Sequence[str]) -> str: | |
| canonical = json.dumps(list(actions), separators=(",", ":"), ensure_ascii=True).encode() | |
| return "sha256:" + hashlib.sha256(canonical).hexdigest() | |