#!/usr/bin/env python3
"""Pre-flight check: fail (exit 1) if any sanctioned fallback has past retire_by.

Wired into the /bugfix skill via a step that runs this script and aborts the
skill if it exits non-zero. The skill emits a finding ('EXPIRED fallback: name
X past retire_by Y') that the spec author must address before the build can
be declared clean.

Boot-time guard in recoil.api.main is the structural primary; this script is
the dev-loop secondary, catching drift before the next LaunchAgent restart.
"""
from __future__ import annotations

import sys
from datetime import date

from recoil.pipeline._lib.sanctioned_fallbacks import list_sanctioned_fallbacks


def main() -> int:
    today = date.today().isoformat()
    expired = [
        r for r in list_sanctioned_fallbacks()
        if getattr(r, "retire_by", None) is not None and r.retire_by < today
    ]
    if expired:
        for r in expired:
            reason = getattr(r, "retire_reason", "") or "(no reason given)"
            print(
                f"EXPIRED: {r.name} (retire_by={r.retire_by}) — {reason}",
                file=sys.stderr,
            )
        return 1
    return 0


if __name__ == "__main__":
    sys.exit(main())
