def admit_attempt(self, request: Any) -> None:
"""Admit a complete tenant/session execution before any turn effects."""
# A raw-ID artifact observer could mix simultaneous attempts. Reject
# unsupported compositions before either admission or observer writes.
for sink in self._sinks:
for name, args, kwargs in (
("bind_run", (request.run_id,), dict(
root_run_id=request.root_run_id, parent_run_id=request.parent_run_id,
tenant_id=self._tenant, session_id=request.session_id,
attempt_id=request.attempt_id)),
("capture_model_round", (request.run_id, None, ""),
{"attempt_id": request.attempt_id}),
):
method = getattr(sink, name, None)
if callable(method):
try:
inspect.signature(method).bind(*args, **kwargs)
except (TypeError, ValueError) as exc:
raise TypeError(f"native {name} requires attempt_id support") from exc
if callable(getattr(sink, "bind_run", None)) and not callable(
getattr(sink, "observe_run", None)
):
abort = getattr(sink, "abort_attempt", None)
if not callable(abort):
raise TypeError("native identity binder requires abort_attempt rollback")
inspect.signature(abort).bind(request.attempt_id)
admit = getattr(self._collector, "admit_attempt", None)
abort_collector = getattr(self._collector, "abort_attempt", None)
if not callable(admit) or not callable(abort_collector):
raise TypeError("native telemetry collector must support attempt admission")
rollback = []
try:
for sink in self._sinks:
if callable(getattr(sink, "observe_run", None)):
continue
bind = getattr(sink, "bind_run", None)
if callable(bind):
# Register first: even a binder that mutates then raises
# must release its partially admitted execution.
rollback.append(sink.abort_attempt)
bind(request.run_id, root_run_id=request.root_run_id,
parent_run_id=request.parent_run_id, tenant_id=self._tenant,
session_id=request.session_id, attempt_id=request.attempt_id)
admit(request.attempt_id, run_id=request.run_id, tenant_id=self._tenant,
session_id=request.session_id, root_run_id=request.root_run_id,
parent_run_id=request.parent_run_id)
rollback.append(abort_collector)
for sink in self._sinks:
observe = getattr(sink, "observe_run", None)
if callable(observe):
try:
observe(request.run_id, root_run_id=request.root_run_id,
parent_run_id=request.parent_run_id, tenant_id=self._tenant,
session_id=request.session_id, attempt_id=request.attempt_id)
except Exception: # noqa: BLE001 - best-effort observation only
continue
except BaseException as failure:
errors = []
for abort in reversed(rollback):
try:
abort(request.attempt_id)
except BaseException as cleanup_error:
errors.append(cleanup_error)
if errors:
raise BaseExceptionGroup(
"native admission rollback failed", [failure, *errors]
) from None
raise