"""The multiplex secondary-profile reconnect lane for GatewayRunner (mixin bound via the MRO).

A secondary profile's platform lands here when its connect failed, it went fatal mid-run, or its
adapter was never built because no plugin registered it in that profile (its load failed or overran
its deadline, #137268). One task per (profile, platform) rebuilds it under that profile's own runtime
scope until it connects or can no longer heal.

Split out of ``gateway/run_adapters.py`` (its size ratchet is at its target). ``gateway.run``
internals are imported lazily inside method bodies (import cycle), so ``patch("gateway.run.X")``
keeps intercepting them at call time.
"""
from __future__ import annotations

import asyncio
from dataclasses import dataclass, field
import functools
import logging
from pathlib import Path
import time
from typing import Callable, Iterable, Optional

from agent.async_utils import consume_detached_task_result
from gateway.config import Platform
from gateway.platforms.base import BasePlatformAdapter
from gateway.platforms.helpers import carry_inbound_dedup, hand_over_held_inbound
from gateway.restart import is_global_startup_conflict
from gateway.run_shutdown import _log_suppressed

logger = logging.getLogger("gateway.run")
# One adapter of each of these platforms per connect wave, in profile order, as the serial sweep connected
# them. A WhatsApp secondary allocates its bridge port by probing which ports are bound, so a sibling's
# configured port it has not bound yet would read as free and be recorded for this profile too.
_ONE_PER_WAVE_PLATFORMS = frozenset({Platform.WHATSAPP})
_MAX_PLUGIN_LOAD_REARMS = 3  # per queued platform: transient load failures heal, a broken plugin stops re-importing


def _adapter_unavailable_message(platform: Platform, *, retrying: bool = True) -> str:
    """Actionable ``adapter_unavailable`` status text, shared by startup and the reconnect watchers so
    ``hermes status`` keeps the plugin/deps/credentials hint after the first retry."""
    message = (
        f"No adapter available for enabled {platform.value}; check the plugin, dependencies, and credentials."
    )
    return f"{message} Retrying in the background." if retrying else message


# Runner reconnects after an adapter exhausted its own polling-conflict budget (``*_polling_conflict``): usually our
# previous long poll still open on the platform's side, which it expires. A holder still there after the last one is
# another live poller (another machine, a leftover process) the gateway must not fight forever (#146909).
_TOKEN_CONTENTION_RECONNECT_DELAYS = (120.0, 300.0, 600.0)
# A contention fatal this long after the previous one starts a new episode (the reconnect healed it meanwhile); a
# persistent holder re-fatals within one delay plus the adapter's own ~200 s ladder.
_TOKEN_CONTENTION_EPISODE_SECS = 1800.0


# A reconnect wait that overran its planned length by more than this was a suspended host, not an outage the
# gateway watched: Windows' ``time.monotonic()`` keeps counting through Modern Standby (#126825).
_RECONNECT_SUSPEND_GAP_SECS = 60.0


def discount_suspended_wait(entries, waited_from: float, planned: float) -> None:
    """Move each reconnect entry's ``queued_at`` forward by a wait's suspend overrun, so NEEDS_ATTENTION and its
    backdated ``retrying_since`` count only time the host was awake. An entry queued before the planned wait
    elapsed may have slept through the overrun; one queued after resume keeps its own clock (the clock is then
    already past ``waited_from + planned + overrun``). Only idle waits are measured: time spent in connect
    attempts is the gateway failing and stays counted.

    Any overrun above ``_RECONNECT_SUSPEND_GAP_SECS`` reads as a suspend, a starved event loop included. That
    is deliberate: a loop blocked for over a minute per idle step is already failing in louder ways, and
    Linux's ``time.monotonic()`` stops during suspend, so there only such stalls ever reach this branch."""
    overrun = time.monotonic() - waited_from - planned
    if overrun <= _RECONNECT_SUSPEND_GAP_SECS:
        return
    for info in entries:
        queued_at = info.get("queued_at")
        if queued_at is not None and queued_at <= waited_from + planned:
            info["queued_at"] = queued_at + overrun


def is_token_contention(error_code: Optional[str]) -> bool:
    """True for a fatal whose cause is another poller holding the token (matched by code, never text)."""
    return (error_code or "").strip().lower().endswith("_polling_conflict")


@dataclass
class _StagedProfileAdapter:
    """One prepared secondary-profile adapter awaiting concurrent connection."""

    profile_name: str
    profile_home: Path
    platform: Platform
    adapter: BasePlatformAdapter
    claims: tuple[Optional[tuple], Optional[tuple]] = (None, None)

    @property
    def non_none_claims(self) -> tuple[tuple, ...]:
        return tuple(c for c in self.claims if c is not None)

    @property
    def wave_keys(self) -> tuple[tuple, ...]:
        """Claims plus a wave-only key: a pending match defers the adapter to a later wave, never refuses it."""
        one_per_wave = (("wave", self.platform),) if self.platform in _ONE_PER_WAVE_PLATFORMS else ()
        return self.non_none_claims + one_per_wave


@dataclass
class _SecondaryConnectBatch:
    """Mutable staging, deferral, claim-tracking, and rollback state for secondary connects."""

    staged: list[_StagedProfileAdapter] = field(default_factory=list)
    deferred: list[_StagedProfileAdapter] = field(default_factory=list)
    staged_owners: set[int] = field(default_factory=set)
    pending_claims: set[tuple] = field(default_factory=set)

    def checkpoint(self) -> tuple[int, int]:
        return len(self.staged), len(self.deferred)

    def has_work(self) -> bool:
        return bool(self.staged or self.deferred)

    def has_pending_conflict(self, claims: Iterable[Optional[tuple]]) -> bool:
        return any(c in self.pending_claims for c in claims if c is not None)

    def stage(self, item: _StagedProfileAdapter) -> None:
        self.staged.append(item)
        self.staged_owners.add(id(item.adapter))
        self.pending_claims.update(item.wave_keys)

    def defer(self, item: _StagedProfileAdapter) -> None:
        self.deferred.append(item)
        self.pending_claims.update(item.wave_keys)

    def staged_platforms(self) -> list[Platform]:
        return [item.platform for item in self.staged]

    def take_wave(self) -> list[_StagedProfileAdapter]:
        wave = self.staged
        self.staged = []
        self.staged_owners.clear()
        return wave

    def take_deferred(self) -> list[_StagedProfileAdapter]:
        pending = self.deferred
        self.deferred = []
        self.staged_owners.clear()
        self.pending_claims.clear()
        return pending

    def rollback(
        self,
        release_claim: Callable[[str, Platform, BasePlatformAdapter], None],
        checkpoint: tuple[int, int] = (0, 0),
    ) -> None:
        staged_start, deferred_start = checkpoint
        for item in self.staged[staged_start:]:
            release_claim(item.profile_name, item.platform, item.adapter)
            self.staged_owners.discard(id(item.adapter))
        del self.staged[staged_start:]
        del self.deferred[deferred_start:]
        self.pending_claims.clear()
        for item in self.staged:
            self.pending_claims.update(item.wave_keys)
        for item in self.deferred:
            self.pending_claims.update(item.wave_keys)


class GatewaySecondaryReconnectMixin:
    """Queue, rebuild and reconnect a secondary profile's platform, plus the capped plugin-load re-arm
    that both reconnect lanes (launch profile and secondaries) share."""

    def _token_contention_reconnect_delay(self, status_key: str, adapter: BasePlatformAdapter) -> float:
        """Delay before reconnecting a fatal adapter, shared by the launch profile and secondaries: 0 unless another
        poller holds its token. Past the delay table the adapter is re-marked non-retryable
        ``token_in_use_elsewhere`` so every reconnect gate parks it, and status flags it NEEDS_ATTENTION."""
        if not adapter.fatal_error_retryable or not is_token_contention(adapter.fatal_error_code):
            return 0.0
        strikes = getattr(self, "_token_contention_strikes", None)
        if strikes is None:
            strikes = self._token_contention_strikes = {}
        now = time.monotonic()
        count, last = strikes.get(status_key, (0, now))
        count = 1 if now - last > _TOKEN_CONTENTION_EPISODE_SECS else count + 1
        strikes[status_key] = (count, now)
        if count <= len(_TOKEN_CONTENTION_RECONNECT_DELAYS):
            delay = _TOKEN_CONTENTION_RECONNECT_DELAYS[count - 1]
            logger.warning("%s: bot token still polled elsewhere; reconnect %d/%d in %ds", status_key, count,
                           len(_TOKEN_CONTENTION_RECONNECT_DELAYS), delay)
            self._update_platform_runtime_status(status_key, platform_state="retrying",
                                                 error_code=adapter.fatal_error_code,
                                                 error_message=adapter.fatal_error_message)
            return delay
        strikes.pop(status_key, None)
        message = (f"{adapter.platform.value} bot token is in use elsewhere: another process (another machine or a "
                   f"leftover gateway) kept polling it through {len(_TOKEN_CONTENTION_RECONNECT_DELAYS)} reconnects. "
                   "Stop that poller, then run 'hermes gateway restart'.")
        logger.error("%s: %s", status_key, message)
        adapter._set_fatal_error("token_in_use_elsewhere", message, retryable=False)
        self._update_platform_runtime_status(status_key, platform_state="fatal", error_code="token_in_use_elsewhere",
                                             error_message=message, needs_attention=True)
        return 0.0

    def _mark_adapter_unavailable(
        self, status_key: str, platform: Platform, *, retrying: bool, needs_attention: Optional[bool] = None,
    ) -> None:
        """Stamp ``adapter_unavailable`` runtime status (``retrying`` or ``fatal``) with the actionable hint, for
        an enabled platform whose ``_create_adapter`` returned None. Shared by both lanes' startup and retries."""
        self._update_platform_runtime_status(
            status_key, platform_state="retrying" if retrying else "fatal", error_code="adapter_unavailable",
            error_message=_adapter_unavailable_message(platform, retrying=retrying), needs_attention=needs_attention,
        )

    def _stand_down_unbuilt_secondary(self, profile_name: str, platform: Platform, reason: str) -> None:
        """A never-built platform whose retry gave up for a config reason (disabled, credential removed): replace
        the ``retrying`` stamp startup gave it, or status keeps promising a retry nothing runs."""
        self._update_platform_runtime_status(
            f"{profile_name}:{platform.value}", platform_state="disabled", error_code=None, error_message=reason,
            needs_attention=False,
        )

    async def _rearm_failed_plugin_load(self, platform: Platform, info: dict) -> None:
        """Re-arm a failed plugin load for a queued platform, capped per queue entry, so the next attempt's
        import can succeed. The CALLER's runtime scope picks the plugin manager (one per profile home).
        Off the loop: the re-arm takes the manager's discovery lock, which a sweep or deferred load holds."""
        from hermes_cli.plugins import get_plugin_manager
        rearms = info.get("load_rearms", 0)
        if rearms < _MAX_PLUGIN_LOAD_REARMS and await self._run_in_executor_with_context(
                get_plugin_manager().rearm_failed_platform, platform.value):
            info["load_rearms"] = rearms + 1

    def _claim_secondary_platform(
        self, profile_name: str, profile_home, platform: Platform, adapter: BasePlatformAdapter,
        claimed: dict[tuple, str],
    ) -> Optional[tuple]:
        """Refuse a secondary adapter whose credential/listener another profile owns (a second poller on
        one bot, a second bind on one port); otherwise record the profile as the bot's owner and return
        its ``(credential_claim, listener_claim)``. None = refused (parked fatal)."""
        credential_claim, listener_claim = self._adapter_claims(platform, adapter)
        owner_name = claimed.get(credential_claim) if credential_claim is not None else None
        owner_origin, incoming_origin = (None, None)
        if owner_name:
            owner_origin, incoming_origin = self._duplicate_credential_origins(
                owner_name, profile_name, profile_home, platform, adapter,
            )
        if self._refuse_duplicate_claim(
            credential_claim, claimed, profile_name, platform, "credential",
            owner_origin=owner_origin, incoming_origin=incoming_origin,
        ) or self._refuse_duplicate_claim(listener_claim, claimed, profile_name, platform, "listener"):
            return None
        # The profile owns this bot from here on, connected or not: cron and the delivery drain read
        # ownership here from another thread, never from the live map a fatal error rewrites.
        bot_platforms = getattr(self, "_profile_bot_platforms", None)
        if bot_platforms is None:
            bot_platforms = self._profile_bot_platforms = {}
        bot_platforms.setdefault(profile_name, set()).add(platform)
        return credential_claim, listener_claim

    def _disown_secondary_platform(self, profile_name: str, platform: Platform) -> None:
        """Undo ``_claim_secondary_platform``'s ownership record for a never-built platform that gave up for good:
        cron and the delivery drain read it to decide that the profile owns the bot."""
        (getattr(self, "_profile_bot_platforms", None) or {}).get(profile_name, set()).discard(platform)

    def _adapter_claims(self, platform: Platform, adapter: BasePlatformAdapter) -> tuple[Optional[tuple], Optional[tuple]]:
        """``(credential_claim, listener_claim)`` of one adapter; either may be None. Never raises: unserve and a
        fatal teardown compute it after the adapter left the map."""
        return self._adapter_credential_claim(platform, adapter), self._adapter_listener_claim(platform, adapter)

    def _hold_secondary_claims(self, profile_name: str, platform: Platform, owner, claims: tuple) -> None:
        """Hold a secondary platform's credential/listener claims while it is not in ``_profile_adapters``
        (initial connect, failed boot connect awaiting the handoff, fatal teardown, unserve teardown, queued
        to reconnect, or mid-heal), so no other profile takes the same bot meanwhile. A task owner's hold is
        released when that task finishes, however it finishes (cancelled before it ever ran included); an
        adapter owner (being connected or torn down) releases its own, so it never drops a newer hold on the
        same platform. Once unserve has popped the profile's adapters, it drops the profile's holds however it
        exits, except the hold of an adapter it did not pop (a fatal teardown still running, say): that hold moves
        to ``_teardown_held_claims`` until its owner releases it."""
        held = getattr(self, "_secondary_held_claims", None)
        if held is None:
            held = self._secondary_held_claims = {}
        key = (profile_name, platform)
        previous = held.get(key)
        held[key] = (owner, claims)
        if isinstance(owner, asyncio.Future) and (previous is None or previous[0] is not owner):
            owner.add_done_callback(lambda done: self._release_secondary_claims(profile_name, platform, done))

    def _release_secondary_claims(self, profile_name: str, platform: Platform, owner) -> None:
        """Drop the hold on (profile, platform) if ``owner`` still owns it (a newer owner keeps its hold), and
        ``owner``'s teardown hold that outlived its profile's unserve."""
        held = getattr(self, "_secondary_held_claims", None) or {}
        entry = held.get((profile_name, platform))
        if entry is not None and entry[0] is owner:
            del held[(profile_name, platform)]
        teardown = getattr(self, "_teardown_held_claims", None) or {}
        if (entry := teardown.get(id(owner))) is not None and entry[0] is owner:
            del teardown[id(owner)]

    def _held_claim_owners(self, exclude: Optional[tuple] = None) -> dict[tuple, str]:
        """Claim -> profile for every held secondary claim, teardowns that outlived an unserve included, but
        the ``exclude`` (profile, platform) ones."""
        held = getattr(self, "_secondary_held_claims", None) or {}
        teardown = getattr(self, "_teardown_held_claims", None) or {}
        holds = [*((key, claims) for key, (_owner, claims) in held.items()),
                 *((key, claims) for _owner, key, claims in teardown.values())]
        return {claim: key[0] for key, claims in holds if key != exclude for claim in claims if claim is not None}

    def _current_claim_owners(self, exclude: Optional[tuple] = None) -> dict[tuple, str]:
        """Who owns each bot/listener right now: held claims (but ``exclude``'s) plus everything live. Read at
        the moment of a claim check, never from a snapshot taken before an await (an owner may connect or let
        go meanwhile)."""
        active = getattr(self, "_primary_profile_name", None) or "default"
        return {**self._held_claim_owners(exclude), **self._live_resource_claims(active)}

    def _external_claim_owners(self, profile_name: str, platform: Platform, staged_owners: set) -> dict[tuple, str]:
        """Claim owners excluding provisional holds owned by ``staged_owners``."""
        mine = (profile_name, platform) if any(key == (profile_name, platform) for _o, key, _c in (
            getattr(self, "_teardown_held_claims", None) or {}).values()) else None
        owners = self._current_claim_owners(mine) if getattr(self, "_running", False) else self._held_claim_owners(mine)
        if not staged_owners:
            return owners
        held = getattr(self, "_secondary_held_claims", None) or {}
        provisional = {
            c for key, (owner, cl) in held.items()
            if id(owner) in staged_owners and key != mine for c in cl if c is not None
        }
        return {c: who for c, who in owners.items() if c not in provisional} if provisional else owners

    def _stage_or_defer_adapter(
        self, candidate: _StagedProfileAdapter, claimed: dict[tuple, str], batch: _SecondaryConnectBatch,
    ) -> bool:
        """Stage candidate for current wave, defer on provisional conflict, or refuse external duplicate."""
        raw_claims = self._adapter_claims(candidate.platform, candidate.adapter)
        candidate.claims = raw_claims
        non_none = candidate.non_none_claims
        external = {
            **claimed,
            **self._external_claim_owners(candidate.profile_name, candidate.platform, batch.staged_owners),
        }
        if any(c in external for c in non_none):
            self._claim_secondary_platform(
                candidate.profile_name, candidate.profile_home, candidate.platform, candidate.adapter, external,
            )
            return False
        if batch.has_pending_conflict(candidate.wave_keys):
            batch.defer(candidate)
            return True
        claims = self._claim_secondary_platform(
            candidate.profile_name, candidate.profile_home, candidate.platform, candidate.adapter, external,
        )
        if claims is None:
            return False
        candidate.claims = claims
        self._configure_profile_adapter(candidate.adapter, candidate.profile_name, candidate.platform)
        self._hold_secondary_claims(candidate.profile_name, candidate.platform, candidate.adapter, claims)
        batch.stage(candidate)
        return True

    async def _cancel_and_drain_wave_tasks(self, tasks: list[asyncio.Task]) -> None:
        """Cancel unfinished wave tasks and wait (bounded) for them to unwind."""
        if not tasks:
            return
        for task in tasks:
            if not task.done():
                task.cancel()
        timeout = self._adapter_disconnect_timeout_secs()
        if timeout <= 0:
            await asyncio.gather(*tasks, return_exceptions=True)
            return
        done, pending = await asyncio.wait(tasks, timeout=timeout)
        for task in done:
            if not task.cancelled():
                task.exception()
        for task in pending:
            task.add_done_callback(consume_detached_task_result)

    async def _cleanup_abandoned_staged(
        self, wave: list[_StagedProfileAdapter], started: set[int], disconnected: set[int],
    ) -> None:
        """Release abandoned provisional holds and disconnect any started-but-untracked adapters."""
        from gateway.run import _profile_runtime_scope
        for item in wave:
            self._release_secondary_claims(item.profile_name, item.platform, item.adapter)
            if id(item.adapter) not in started or id(item.adapter) in disconnected:
                continue
            if (getattr(self, "_profile_adapters", None) or {}).get(item.profile_name, {}).get(item.platform) is item.adapter:
                continue
            disconnected.add(id(item.adapter))
            with _log_suppressed(
                logging.DEBUG, "Cancelled %s disconnect failed (profile: %s)",
                item.platform.value, item.profile_name, exc_info=True,
            ):
                with _profile_runtime_scope(item.profile_home, hydrate_secrets=False):
                    await self._safe_adapter_disconnect(item.adapter, item.platform)

    async def _connect_profile_adapter_wave(
        self, wave: list[_StagedProfileAdapter], claimed: dict[tuple, str],
        transient_failed: Optional[set[str]] = None,
    ) -> int:
        """Connect one conflict-free wave of staged secondary adapters concurrently."""
        if not wave:
            return 0
        started: set[int] = set()
        disconnected: set[int] = set()
        tasks: list[asyncio.Task] = []
        boot_platform: Optional[Platform] = None

        async def _run_one(item: _StagedProfileAdapter) -> int:
            try:
                return await self._connect_prepared_profile_adapter(
                    item, claimed, started=started, disconnected=disconnected, boot_platform=boot_platform,
                )
            except Exception as exc:
                if transient_failed is None:
                    raise
                logger.error("Failed to start adapters for profile '%s': %s", item.profile_name, exc, exc_info=True)
                transient_failed.add(item.profile_name)
                return 0

        try:
            boot_platform = self._max_boot_timeout_platform(item.platform for item in wave)
            if len(wave) == 1:
                return await _run_one(wave[0])
            tasks = [asyncio.create_task(_run_one(item)) for item in wave]
            results = await asyncio.gather(*tasks, return_exceptions=True)
        except BaseException:
            await self._cancel_and_drain_wave_tasks(tasks)
            await self._cleanup_abandoned_staged(wave, started, disconnected)
            raise
        connected = 0
        first_exc: Optional[BaseException] = None
        for outcome in results:
            if isinstance(outcome, BaseException):
                if first_exc is None:
                    first_exc = outcome
                continue
            connected += outcome
        if first_exc is not None:
            await self._cleanup_abandoned_staged(wave, started, disconnected)
            raise first_exc
        return connected

    async def _connect_staged_profile_adapters(
        self, batch: _SecondaryConnectBatch, claimed: dict[tuple, str],
        transient_failed: Optional[set[str]] = None,
    ) -> int:
        """Connect staged adapters in conflict-free waves, re-evaluating deferred siblings."""
        connected = 0
        while batch.has_work():
            wave = batch.take_wave()
            if wave:
                connected += await self._connect_profile_adapter_wave(wave, claimed, transient_failed)
            pending_deferred = batch.take_deferred()
            if not pending_deferred:
                break
            try:
                for candidate in pending_deferred:
                    p_map = self._profile_adapters.setdefault(candidate.profile_name, {})
                    if candidate.platform in p_map or candidate.platform in (
                            (getattr(self, "_profile_failed_platforms", None) or {}).get(candidate.profile_name) or {}) or (
                            (candidate.profile_name, candidate.platform) in (
                                getattr(self, "_secondary_held_claims", None) or {})):
                        continue
                    self._stage_or_defer_adapter(candidate, claimed, batch)
            except BaseException:
                batch.rollback(self._release_secondary_claims)
                raise
        return connected

    def _note_unbuilt_secondary_platform(self, profile_name: str, platform: Platform, heals: bool) -> None:
        """A secondary platform whose ``_create_adapter`` returned None at startup: say why, stamp its
        ``<profile>:<platform>`` status, and queue it when it can heal (no plugin registered it in this
        profile's scope, so a later import can still succeed). Anything else needs a config change:
        retrying it would only re-warn at the backoff cap (#5196)."""
        pv = platform.value
        if heals:
            logger.warning(
                "[MULTIPLEX] Profile '%s': no plugin registered platform '%s' - is the plugin installed? (a load "
                "that failed or overran its deadline also leaves it unregistered); retrying in the background",
                profile_name, pv,
            )
        else:
            logger.warning(
                "[MULTIPLEX] Profile '%s': skipping platform '%s' - adapter creation returned None",
                profile_name, pv,
            )
        self._mark_adapter_unavailable(f"{profile_name}:{pv}", platform, retrying=heals, needs_attention=True)
        if heals:
            self._schedule_secondary_profile_startup_reconnect(profile_name, platform, None)

    async def _secondary_reconnect_attempt(self, profile_name: str, platform: Platform, predecessor=None):
        """One scoped attempt to rebuild+connect a secondary adapter → ``(adapter, success)``;
        ``(None, None)`` = give up for good (disabled, credential removed, refused as a duplicate, or
        adapter unavailable for a reason only a config change fixes); ``(None, False)`` = no adapter yet
        but the platform can still heal (no plugin registered it, so a later import can succeed). Caller
        tears down a RETURNED adapter; one whose configure/connect raised is torn down here."""
        from gateway.platform_registry import platform_registry
        from gateway.run import _platform_has_bot_credential, _profile_runtime_scope
        # Lazy + per-attempt: keeps test monkeypatches on these modules live.
        from hermes_cli.profiles import get_profile_dir
        from hermes_cli.env_loader import hydrate_profile_secret_sources
        from gateway.config import load_gateway_config
        profile_home = get_profile_dir(profile_name)
        # Hydrate external secret sources off-loop so they cannot starve heartbeats.
        await asyncio.to_thread(hydrate_profile_secret_sources, profile_home)
        with _profile_runtime_scope(profile_home, hydrate_secrets=False):
            profile_config = load_gateway_config().platforms.get(platform)
            if profile_config is None or not profile_config.enabled:
                if predecessor is None:
                    self._stand_down_unbuilt_secondary(profile_name, platform, "disabled in config")
                return None, None
            # Startup credential gate mirror: a removed credential must not rebuild.
            # Mirrors the startup credential gate (#84079): a credential removed from this profile's scope
            # must not rebuild an adapter that would fan out turns.
            if not _platform_has_bot_credential(platform, profile_config):
                logger.info(
                    "Secondary %s reconnect skipped: no bot credential (profile: %s)",
                    platform.value, profile_name,
                )
                if predecessor is None:
                    self._stand_down_unbuilt_secondary(profile_name, platform, "no bot credential configured")
                return None, None
            # A re-armed plugin import + register() joins its load deadline: run it off the event loop,
            # in this profile's scope (the registry keeps deferred loaders per profile home).
            await self._run_in_executor_with_context(platform_registry.get, platform.value)
            adapter = await self._create_adapter_off_loop_deps(platform, profile_config)
            if adapter is None:
                if self._adapter_may_heal(platform, profile_config):
                    logger.info("Secondary %s: no adapter yet (profile: %s)", platform.value, profile_name)
                    return None, False
                logger.warning(
                    "Secondary %s reconnect skipped: adapter unavailable (profile: %s)",
                    platform.value, profile_name,
                )
                self._mark_adapter_unavailable(
                    f"{profile_name}:{platform.value}", platform, retrying=False, needs_attention=True)
                return None, None
            # Run the ownership check before connect() against live and held owners (but this platform's own
            # hold: the predecessor's, or an earlier tick's): a never-built platform never passed the startup
            # check, and a rebuilt one may claim a bot another profile took meanwhile. Hold the claim (no await
            # between the check and the hold) so a second healing profile with the same bot is refused meanwhile.
            claims = self._claim_secondary_platform(
                profile_name, profile_home, platform, adapter,
                self._current_claim_owners(exclude=(profile_name, platform)))
            if claims is None:
                return None, None
            self._hold_secondary_claims(profile_name, platform, asyncio.current_task(), claims)
            carry_inbound_dedup(predecessor, adapter)
            try:
                self._configure_profile_adapter(adapter, profile_name, platform)
                success = await self._connect_adapter_with_timeout(adapter, platform, is_reconnect=True)
            except BaseException:
                # Caller never sees this adapter; release its partial resources and return its held inbound.
                await self._safe_adapter_disconnect(adapter, platform)
                hand_over_held_inbound(adapter, predecessor)
                raise
            return adapter, success

    def _end_secondary_reconnect(self, profile_name: str, platform: Platform, predecessor, adapter, success) -> bool:
        """Decide whether a reconnect ends after an attempt that installed nothing, and settle ownership: it
        gave up for good (``(None, None)``: needs a config change), its connected adapter was not installed (a
        newer task won the slot, or shutdown began), or it hit a non-retryable fatal. A never-built platform
        that ends for good (not the connected case) drops its bot ownership, which cron and the delivery drain
        read."""
        if adapter is None:
            final = success is None
        elif success:
            return True  # connected but not installed: a newer task owns the platform, or shutdown began
        else:
            final = getattr(adapter, "has_fatal_error", False) and not getattr(adapter, "fatal_error_retryable", True)
        if final and predecessor is None:
            self._disown_secondary_platform(profile_name, platform)
        return final

    async def _run_secondary_profile_reconnect(
        self, profile_name: str, platform: Platform, predecessor=None
    ) -> None:
        """Reconnect a retryable secondary adapter under its own profile scope."""
        from gateway.run import _profile_runtime_scope, _reconnect_backoff
        attempts = 0
        # Same escalation shape as the primary queue entry; ``queued_at`` is this task's start.
        queue_info = {"queued_at": time.monotonic(), "attempts": 0}
        current_task = asyncio.current_task()
        try:
            while self._running:
                adapter = None
                rearm = False
                try:
                    adapter, success = await self._secondary_reconnect_attempt(
                        profile_name, platform, predecessor
                    )
                    if adapter is None:
                        if self._end_secondary_reconnect(profile_name, platform, predecessor, None, success):
                            return
                        rearm = True  # no adapter yet but it can heal: re-arm the failed load, then retry
                    elif success and self._running:
                        profile_map = self._profile_adapters.setdefault(profile_name, {})
                        # Never publish once superseded: a reconcile that retired this reconnect cancels
                        # it (an attempt that swallowed the cancel still sees it) or gives the slot away.
                        slot = ((self._profile_failed_platforms or {}).get(profile_name) or {}).get(platform)
                        superseded = bool(current_task and current_task.cancelling()) or slot not in (None, current_task)
                        if platform not in profile_map and not superseded:
                            profile_map[platform] = adapter
                            hand_over_held_inbound(predecessor, adapter)
                            self._sync_voice_mode_state_to_adapter(adapter)
                            logger.info("✓ %s reconnected (profile: %s)", platform.value, profile_name)
                            await self._redeliver_failed_obligations_for_platform(
                                platform, profile=profile_name
                            )
                            # What a primary reconnect replays too: the owed notice spans served profiles' home
                            # channels, and sessions boot skipped for this offline adapter wait for this call.
                            self._schedule_restart_notice_replay()
                            try:
                                self._schedule_resume_pending_sessions(platform=platform)
                            except Exception:
                                logger.debug("resume-pending reschedule after %s reconnect failed (profile: %s)",
                                             platform.value, profile_name, exc_info=True)
                            return
                    if adapter is not None:
                        # Not installed (newer reconnect won the slot, shutdown began, or connect failed):
                        # release partial resources and return its held inbound; stop only for a non-retryable fatal.
                        await self._safe_adapter_disconnect(adapter, platform)
                        hand_over_held_inbound(adapter, predecessor)
                        if self._end_secondary_reconnect(profile_name, platform, predecessor, adapter, success):
                            return
                except BaseException as exc:
                    if adapter is not None:
                        await self._safe_adapter_disconnect(adapter, platform)
                        if self._profile_adapters.get(profile_name, {}).get(platform) is not adapter:
                            hand_over_held_inbound(adapter, predecessor)  # an installed adapter owns its queue
                    if not isinstance(exc, Exception):
                        raise  # CancelledError (and other BaseExceptions) propagate after release
                    logger.debug(
                        "Secondary %s reconnect attempt failed (profile: %s)", platform.value,
                        profile_name, exc_info=True,
                    )
                if not self._running:
                    return
                attempts += 1
                queue_info["attempts"] = attempts
                profile_home = self._routed_profile_home(profile_name)
                # The attempt above already hydrated this profile's secret sources off-loop. The re-arm runs in
                # this scope too: the plugin manager whose load failed is this profile's, not the launch's.
                with self._scope_or_null(
                        functools.partial(_profile_runtime_scope, hydrate_secrets=False), profile_home):
                    if rearm:
                        with _log_suppressed(logging.DEBUG, "plugin load re-arm for %s failed (profile: %s): %s",
                                             platform.value, profile_name):
                            await self._rearm_failed_plugin_load(platform, queue_info)
                    flagged = self._flag_reconnect_needs_attention(
                        platform, queue_info, time.monotonic(), status_key=f"{profile_name}:{platform.value}")
                    if flagged and rearm:  # the flag rewrote the entry without an error code: put the hint back
                        self._mark_adapter_unavailable(f"{profile_name}:{platform.value}", platform, retrying=True)
                backoff = _reconnect_backoff(attempts)
                logger.info(
                    "Secondary %s reconnect retry in %ds (profile: %s)", platform.value, backoff, profile_name
                )
                slept_from = time.monotonic()
                await asyncio.sleep(backoff)
                discount_suspended_wait((queue_info,), slept_from, backoff)
        finally:
            # Release our slot unless a newer task already owns it.
            pending = self._profile_failed_platforms
            profile_pending = pending.get(profile_name) if isinstance(pending, dict) else None
            if isinstance(profile_pending, dict):
                task = profile_pending.get(platform)
                if not isinstance(task, asyncio.Task) or task is current_task:
                    profile_pending.pop(platform, None)
                    if not profile_pending:
                        pending.pop(profile_name, None)

    def _schedule_secondary_profile_startup_reconnect(
        self, profile_name: str, platform: Platform, adapter: Optional[BasePlatformAdapter] = None
    ) -> None:
        """Queue a cold-start reconnect: startup failures happen BEFORE ``_running`` flips True (the
        regular scheduler would drop them), so park a task and hand off once live. ``adapter`` is None
        when nothing was built at all (a platform whose creation may still heal)."""
        if not getattr(adapter, "fatal_error_retryable", True):
            return
        if adapter is not None and is_global_startup_conflict(getattr(adapter, "fatal_error_code", None)):
            # A live foreign token holder is an ownership conflict, not a blip: park it fatal.
            logger.error(
                # Park it fatal (like ``duplicate_credential``) instead of retry-storming the token every
                # backoff (#83183).
                "[MULTIPLEX] Profile '%s': %s credential is held by another "
                "gateway (%s) — parked, not retried. %s", profile_name, platform.value,
                adapter.fatal_error_code, adapter.fatal_error_message or "",
            )
            self._mark_platform_fatal(f"{profile_name}:{platform.value}", adapter)
            return

        # The bridge belongs to this serve of the profile: once it is unserved (its map popped) or served again
        # (a new map), a bridge still waiting for startup to finish hands nothing over.
        profile_map = (getattr(self, "_profile_adapters", None) or {}).get(profile_name)

        def _handoff() -> None:
            if (getattr(self, "_profile_adapters", None) or {}).get(profile_name) is not profile_map:
                return
            try:
                self._schedule_secondary_profile_reconnect(profile_name, platform, adapter)
            except Exception:
                # A raise here would die as an unretrieved-task exception logged only at GC; surface it.
                logger.exception(
                    "secondary-startup-reconnect handoff failed (profile=%s platform=%s)",
                    profile_name, platform.value,
                )

        async def _await_running_then_schedule() -> None:
            if self._running:  # fast path (also the only path for bare runners without _shutdown_event)
                _handoff()
                return
            # Poll (startup completion has no event); bounded so a wedged startup cannot spin.
            while not self._running and not self._shutdown_event.is_set():
                await asyncio.sleep(0.1)
            if self._running and not self._shutdown_event.is_set():
                _handoff()

        bridge = self._retain_background_task(asyncio.create_task(
            _await_running_then_schedule(),
            name=f"secondary-startup-reconnect:{profile_name}:{platform.value}",
        ))
        if adapter is not None:  # it owns its bot from the boot sweep on; the handoff passes the hold on
            self._hold_secondary_claims(profile_name, platform, bridge, self._adapter_claims(platform, adapter))

    def _schedule_secondary_profile_reconnect(
        self, profile_name: str, platform: Platform, adapter: Optional[BasePlatformAdapter] = None,
        delay: float = 0.0,
    ) -> None:
        """Schedule one runner-owned reconnect without sharing primary secrets. ``adapter`` is the
        failed predecessor, or None when the platform has no adapter yet (creation returned None for a
        platform that can still heal): the queued entry stops the runtime re-scan from building the same
        platform twice, and the attempt claims the bot before it connects. A profile being unserved
        (``_retiring_profiles``) gets none: its unserve has already popped the reconnects it cancels."""
        if (
            not self._running
            or not getattr(adapter, "fatal_error_retryable", True)
            or profile_name in (getattr(self, "_retiring_profiles", None) or ())
        ):
            return
        pending = self._profile_failed_platforms
        if not isinstance(pending, dict):
            pending = self._profile_failed_platforms = {}
        profile_pending = pending.setdefault(profile_name, {})
        if platform in profile_pending:
            return
        async def _after_delay() -> None:  # the token-contention back-off; the slot is held meanwhile
            try:
                await asyncio.sleep(delay)
            except asyncio.CancelledError:
                if profile_pending.get(platform) is asyncio.current_task():
                    profile_pending.pop(platform, None)
                raise
            await self._run_secondary_profile_reconnect(profile_name, platform, adapter)

        task = profile_pending[platform] = self._retain_background_task(asyncio.create_task(
            _after_delay() if delay > 0 else self._run_secondary_profile_reconnect(profile_name, platform, adapter),
            name=f"secondary-reconnect:{profile_name}:{platform.value}",
        ))
        if adapter is not None:  # the failed predecessor still owns its bot while it waits to reconnect
            self._hold_secondary_claims(profile_name, platform, task, self._adapter_claims(platform, adapter))
