"""路由取当前租户运行时——纯访问器(Task 7)。 TenantHostMiddleware(纯 ASGI,http 与 websocket 双覆盖)保证凡到达路由 的请求 ``state.tenant`` 必已就位;唯一例外是平台级豁免路径(/health), 而它不访问租户资源。本模块不再有任何兜底——state 无租户即 raise,把 "中间件没挂上/被绕过"这类装配错误炸在第一现场而不是静默串库。 """ from typing import TYPE_CHECKING from fastapi import Request, WebSocket if TYPE_CHECKING: # 仅类型注解用: engine 包运行期零 import platform(依赖方向保持 # platform → engine,反向只在类型层) from app.platform.registry import TenantRuntime def get_runtime(request_or_ws: Request | WebSocket) -> "TenantRuntime": """读 state.tenant(中间件写入);缺失即装配错误,清晰失败。""" runtime = getattr(request_or_ws.state, "tenant", None) if runtime is None: raise RuntimeError( "请求 state 里没有租户运行时——TenantHostMiddleware 未覆盖该" "请求(中间件未挂载,或豁免路径访问了租户资源)") return runtime