Merge Workstream K: hardware anomalies feed the summon pipeline

This commit is contained in:
Indiana
2026-07-24 14:57:14 +00:00
3 changed files with 581 additions and 0 deletions

View File

@@ -91,6 +91,36 @@ class SeanceState:
last_wire_anomaly_at: float = 0.0
# Active-session registry (spec: ESP32 sensor node, Workstream K): maps a
# user to their live SeanceState so hardware ingestion (a plain HTTP
# request, not a WS connection) can find "does this user have a séance
# open right now" and push a hardware anomaly into it. Single
# most-recent-session mapping — if a user somehow has two `/ws/session`
# tabs open concurrently, the newer one wins the registry slot; that's a
# reasonable v1 (see spec's Contract section) since a user's attention is
# realistically in one tab at a time. Plain in-process dict, same reasoning
# as everywhere else in this file: no external store needed at this scale,
# doesn't need to survive a restart.
_active_sessions: dict[uuid.UUID, "SeanceState"] = {}
def register_active_session(user_id: uuid.UUID, state: "SeanceState") -> None:
_active_sessions[user_id] = state
def unregister_active_session(user_id: uuid.UUID, state: "SeanceState") -> None:
# Only remove if it's still *this* state — guards against a rare
# overlap where an older session's disconnect cleanup runs after a
# newer session for the same user has already registered, which would
# otherwise wipe out the newer (still-live) registry entry.
if _active_sessions.get(user_id) is state:
del _active_sessions[user_id]
def get_active_session(user_id: uuid.UUID) -> "SeanceState | None":
return _active_sessions.get(user_id)
def serialize_entity(entity: Entity) -> dict:
return {
"id": str(entity.id),
@@ -475,6 +505,7 @@ async def session_socket(websocket: WebSocket) -> None:
session_id=contact_session.id,
client_ip=_client_ip(websocket),
)
register_active_session(user_id, state)
sender = asyncio.create_task(_sender(state, websocket))
await state.send_queue.put({"type": "session", "id": str(contact_session.id)})
@@ -511,6 +542,7 @@ async def session_socket(websocket: WebSocket) -> None:
except WebSocketDisconnect:
pass
finally:
unregister_active_session(user_id, state)
if state.ambient_task is not None:
state.ambient_task.cancel()
sender.cancel()