"""Background task: expire stale delegations every 30 seconds.

Started by ``gsap_broker/app.py`` lifespan. Stopped on shutdown.
"""

import asyncio
import logging

logger = logging.getLogger(__name__)


async def cleanup_loop(manager) -> None:
    """Periodically expire stale delegations (30s interval)."""
    while True:
        try:
            await manager.cleanup_expired()
        except asyncio.CancelledError:
            raise
        except Exception as e:
            logger.warning("delegation cleanup error: %s", e)
        await asyncio.sleep(30)
