"""Infrastructure observations survive control-plane loss; commands stay fenced.""" import http.client from core_client import CoreRequestRejected EXPIRED_SESSION = "Сессия Worker истекла; требуется переподключение." def unavailable(exc): return isinstance(exc, (OSError, http.client.HTTPException)) or ( isinstance(exc, CoreRequestRejected) and 500 <= exc.status <= 599 ) def registered_request(client, endpoint, body=None, *, hello=None, **kwargs): """Retry only an explicit pre-execution session rejection, with the same owner. Callers use this for reads and idempotent observations/receipts, never dispatch. A cancellation, changed owner or invalid result is not a transport outage. """ try: return client.request(endpoint, body, **kwargs) except CoreRequestRejected as exc: if exc.status != 409 or exc.detail != EXPIRED_SESSION or hello is None: raise if hello["instance_id"] != client.instance: raise ValueError("Infrastructure reconnect owner changed") from exc client.request("/worker/register", hello) return client.request(endpoint, body, **kwargs) def report_progress(client, endpoint, phase, *, hello=None): # A missed observation must never unwind run_container's cleanup guard. # The next pulse sends the latest local progress, without a growing queue. try: return registered_request(client, endpoint, dict(action="progress", **phase), hello=hello) except Exception as exc: if not unavailable(exc): raise return None