debug_ita_calendar_gate.py 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374
  1. #!/usr/bin/env python3
  2. """Run a bounded Italy Calendar discovery or production-Query safety gate.
  3. This tool is intentionally interactive. It uses an authorized team Query
  4. account, sends exactly one booking OTP, submits the pre-calendar form exactly
  5. once, then either probes the discovery horizon or calls the real production
  6. Query twice in the same prepared session. Both modes compare non-sensitive
  7. Reservation counts before and after. This tool never explicitly selects a
  8. date or time slot and never clicks the final booking button. The site's own
  9. Calendar initialization may automatically select a date when it renders.
  10. """
  11. from __future__ import annotations
  12. import hashlib
  13. import json
  14. import os
  15. import sys
  16. import time
  17. from dataclasses import dataclass
  18. from datetime import date, datetime
  19. from enum import Enum
  20. from pathlib import Path
  21. from threading import Lock
  22. from typing import Any, Callable, Mapping
  23. from urllib.parse import parse_qs, urljoin, urlparse
  24. REPO_ROOT = Path(__file__).resolve().parents[1]
  25. if str(REPO_ROOT) not in sys.path:
  26. sys.path.insert(0, str(REPO_ROOT))
  27. from plugins.ita_plugin import (
  28. CALENDAR_QUERY_MODE,
  29. SERVICES_PROBE_QUERY_MODE,
  30. ItaPlugin,
  31. )
  32. from plugins import ita_plugin as calendar_session
  33. from plugins.ita_plugin import (
  34. AVAILABILITY_PATH,
  35. CALENDAR_PATH,
  36. NEXT_MONTH_CONTROL_SCRIPT,
  37. CalendarGateError,
  38. CalendarNextMonthUnavailableError,
  39. CalendarProbeResult,
  40. CalendarQueryScanResult,
  41. click_next_month,
  42. guard_calendar,
  43. header_value as _header,
  44. listen_once,
  45. month_text as _month_text,
  46. next_month_value as _next_month_value,
  47. parse_availability_packet,
  48. refresh_calendar,
  49. require_plain_nonnegative_int as _require_plain_nonnegative_int,
  50. same_origin_path as _same_origin_path,
  51. validate_next_month_snapshot,
  52. )
  53. from plugins.ita_plugin import (
  54. ItaProtocolError,
  55. ItaRouteConfig,
  56. resolve_route_config,
  57. )
  58. from tools.debug_ita_query import (
  59. ItaDebugRoute,
  60. _get_query_credentials,
  61. _prepare_query_profile,
  62. build_query_config,
  63. resolve_debug_route,
  64. )
  65. from tools.debug_ita_session import (
  66. DebugConfigurationError,
  67. _env_text,
  68. _text_for_log,
  69. resolve_chrome_path,
  70. )
  71. from vs_types import AppointmentType, AvailabilityStatus, VSPlgConfig, VSQueryResult
  72. RESERVATION_URL = "https://prenotami.esteri.it/Reservation"
  73. OTP_PATH = "/BookingCalendar/GenerateOTP"
  74. QCAL_TOOL_CONTRACT_VERSION = "qcal1-calendar-soak-v11"
  75. QCAL_TOOL_SOURCE_PATHS = (
  76. Path(__file__),
  77. REPO_ROOT / "plugins" / "ita_plugin.py",
  78. REPO_ROOT / "plugins" / "ita_plugin.py",
  79. REPO_ROOT / "plugins" / "ita_plugin.py",
  80. REPO_ROOT / "plugins" / "ita_plugin.py",
  81. REPO_ROOT / "plugins" / "ita_plugin.py",
  82. REPO_ROOT / "tools" / "debug_ita_soak.py",
  83. REPO_ROOT / "toolkit" / "backoff.py",
  84. )
  85. QCAL_TOOL_SOURCE_FINGERPRINT = hashlib.sha256(
  86. b"".join(path.read_bytes() for path in QCAL_TOOL_SOURCE_PATHS)
  87. ).hexdigest()[:12]
  88. QCAL_MODE_ENV = "ITA_QCAL_MODE"
  89. QCAL_PRODUCTION_QUERY_ATTEMPTS = 2
  90. QCAL_QUERY_INTERVAL_SECONDS = 3
  91. QCAL_FORWARD_MONTHS_ENV = "ITA_QCAL_FORWARD_MONTHS"
  92. QCAL_FORWARD_MONTHS_DEFAULT = 1
  93. QCAL_FORWARD_MONTHS_MAX = 12
  94. QCAL_MONTH_CLICK_INTERVAL_SECONDS = 1.5
  95. SAFE_PREFLIGHT_DIAGNOSTIC_OUTCOMES = frozenset({"ready", "waiting", "rejected", "timeout", "read_failed"})
  96. FORM_REQUIRED_ENV_NAMES = (
  97. "ITA_CALENDAR_LOOKAHEAD_MONTHS",
  98. "ITA_BOOKING_TYPE_CONTROL_ID",
  99. "ITA_BOOKING_TYPE_VALUE",
  100. "ITA_BOOKING_TYPE_LABELS",
  101. "ITA_PURPOSE_CONTROL_ID",
  102. "ITA_PURPOSE_VALUE",
  103. "ITA_PURPOSE_LABELS",
  104. )
  105. RESERVATION_SNAPSHOT_SCRIPT = r"""
  106. /* coordinate:ita-qcal-reservation-counts */
  107. const ids = ['dataTableReservations', 'dataTableReservationsWL'];
  108. const hasJQuery = Boolean(window.jQuery);
  109. const hasDataTables = Boolean(
  110. hasJQuery && jQuery.fn && jQuery.fn.dataTable
  111. );
  112. const tableState = (id) => {
  113. const table = document.getElementById(id);
  114. if (!table) return {ready: false, count: null};
  115. const initialized = hasDataTables &&
  116. jQuery.fn.dataTable.isDataTable(table);
  117. if (initialized) {
  118. return {
  119. ready: true,
  120. count: jQuery('#' + id).DataTable().rows().count(),
  121. };
  122. }
  123. const structurallyEmpty =
  124. table.querySelectorAll('tbody tr').length === 0 &&
  125. table.querySelectorAll('thead th').length === 0 &&
  126. !table.classList.contains('dataTable') &&
  127. !table.closest('.dataTables_wrapper');
  128. return {
  129. ready: structurallyEmpty,
  130. count: structurallyEmpty ? 0 : null,
  131. };
  132. };
  133. const reservations = tableState(ids[0]);
  134. const waitlist = tableState(ids[1]);
  135. return {
  136. path: window.location.pathname,
  137. authenticated: Boolean(document.querySelector('.app-menu')),
  138. tableCount: ids.filter((id) => document.getElementById(id)).length,
  139. ready: reservations.ready && waitlist.ready,
  140. reservations: reservations.count,
  141. waitlist: waitlist.count,
  142. };
  143. """
  144. AVANTI_FAILURE_DIAGNOSTIC_SCRIPT = r"""
  145. /* coordinate:ita-qcal-avanti-failure-diagnostic */
  146. const isVisible = (element) => Boolean(
  147. element &&
  148. element.getClientRects().length > 0 &&
  149. window.getComputedStyle(element).visibility !== 'hidden'
  150. );
  151. const otpInputs = document.querySelectorAll('#otp-input');
  152. const otpInvalid = document.getElementById('IdOtpInvalid');
  153. const bookingForm = document.getElementById('bookingForm');
  154. const avantiButtons = document.querySelectorAll('#btnAvanti');
  155. const turnstile = document.querySelectorAll(
  156. 'input[name="cf-turnstile-response"]'
  157. );
  158. const validationNodes = new Set([
  159. ...document.querySelectorAll('.field-validation-error'),
  160. ...document.querySelectorAll('.validation-summary-errors li'),
  161. ...document.querySelectorAll('[aria-invalid="true"]'),
  162. ]);
  163. const visibleValidationCount = Array.from(validationNodes).filter(
  164. isVisible
  165. ).length;
  166. const visibleConfirmCount = Array.from(
  167. document.querySelectorAll('.jconfirm-content')
  168. ).filter(isVisible).length;
  169. const resources = performance.getEntriesByType('resource');
  170. return {
  171. path: window.location.pathname,
  172. bookingFormPresent: Boolean(bookingForm),
  173. otpInputCount: otpInputs.length,
  174. otpValuePresent: Boolean(
  175. otpInputs.length === 1 && String(otpInputs[0].value || '').trim()
  176. ),
  177. otpInvalidVisible: isVisible(otpInvalid),
  178. avantiButtonCount: avantiButtons.length,
  179. avantiDisabled:
  180. avantiButtons.length === 1 ? avantiButtons[0].disabled : null,
  181. turnstileCount: turnstile.length,
  182. turnstileTokenPresent: Boolean(
  183. turnstile.length === 1 && turnstile[0].value
  184. ),
  185. visibleValidationCount,
  186. visibleConfirmCount,
  187. finalRequestCount: resources.filter(
  188. (entry) => entry.name.includes(
  189. '/BookingCalendar/InsertNewBooking'
  190. )
  191. ).length,
  192. };
  193. """
  194. from plugins.ita_plugin import (
  195. BookingPreflightStage,
  196. CalendarRequestAudit,
  197. validate_otp_packet,
  198. validate_avanti_packet,
  199. validate_booking_preflight,
  200. _booking_preflight_script,
  201. _validate_preflight_except_dynamic_token,
  202. _wait_for_calendar,
  203. _element_or_fail,
  204. _read_manual_otp,
  205. _enter_otp,
  206. )
  207. class CalendarGateMode(str, Enum):
  208. DISCOVERY_HORIZON = "discovery_horizon"
  209. PRODUCTION_QUERY = "production_query"
  210. CALENDAR_SOAK = "calendar_soak"
  211. class PositiveQueryEvidenceUnavailable(CalendarGateError):
  212. """Natural inventory did not provide evidence for the positive gate."""
  213. def resolve_qcal_mode(env: Mapping[str, str]) -> CalendarGateMode:
  214. raw = _env_text(env, QCAL_MODE_ENV) or CalendarGateMode.DISCOVERY_HORIZON.value
  215. try:
  216. return CalendarGateMode(raw)
  217. except ValueError as exc:
  218. raise CalendarGateError(
  219. "ITA_QCAL_MODE must be discovery_horizon, production_query, or calendar_soak"
  220. ) from exc
  221. def _safe_boolean_shape(value: object) -> dict[str, object]:
  222. """Describe one boolean without serializing an unexpected value."""
  223. is_boolean = isinstance(value, bool)
  224. return {
  225. "type": type(value).__name__,
  226. "value": value if is_boolean else None,
  227. }
  228. def _safe_nonnegative_int_shape(value: object) -> dict[str, object]:
  229. """Describe a count without serializing an unexpected value."""
  230. is_count = (
  231. isinstance(value, int)
  232. and not isinstance(value, bool)
  233. and value >= 0
  234. )
  235. return {
  236. "type": type(value).__name__,
  237. "value": value if is_count else None,
  238. }
  239. def _safe_http_status_shape(value: object) -> dict[str, object]:
  240. """Describe a conventional HTTP status without leaking response data."""
  241. is_status = (
  242. isinstance(value, int)
  243. and not isinstance(value, bool)
  244. and 100 <= value <= 599
  245. )
  246. return {
  247. "type": type(value).__name__,
  248. "value": value if is_status else None,
  249. }
  250. def _safe_path_category(
  251. raw_url: object,
  252. *,
  253. booking_path: str,
  254. ) -> str:
  255. """Map a URL to one fixed category without returning its raw value."""
  256. if not str(raw_url or "").strip():
  257. return "missing"
  258. normalized = urljoin("https://prenotami.esteri.it", str(raw_url))
  259. try:
  260. path, _ = _same_origin_path(normalized)
  261. except CalendarGateError:
  262. return "unexpected_origin"
  263. if path == booking_path:
  264. return "booking"
  265. if path == CALENDAR_PATH:
  266. return "calendar"
  267. if path == "/Services":
  268. return "services"
  269. if path == "/UserArea":
  270. return "user_area"
  271. return "other_same_origin"
  272. def safe_avanti_failure_diagnostic(
  273. packet: object,
  274. raw: object,
  275. *,
  276. booking_path: str,
  277. ) -> dict[str, object]:
  278. """Return allowlisted AVANTI evidence without body, query, or form values."""
  279. snapshot = raw if isinstance(raw, dict) else {}
  280. response = getattr(packet, "response", None)
  281. packet_url = getattr(packet, "url", "")
  282. request_matches = False
  283. try:
  284. request_path, parsed_request = _same_origin_path(packet_url)
  285. request_matches = (
  286. str(getattr(packet, "method", "")).upper() == "POST"
  287. and request_path == booking_path
  288. and not parsed_request.query
  289. and not parsed_request.fragment
  290. )
  291. except CalendarGateError:
  292. pass
  293. response_path = _safe_path_category(
  294. getattr(response, "url", ""),
  295. booking_path=booking_path,
  296. )
  297. location_path = _safe_path_category(
  298. _header(getattr(response, "headers", {}), "location"),
  299. booking_path=booking_path,
  300. )
  301. browser_path = _safe_path_category(
  302. snapshot.get("path"),
  303. booking_path=booking_path,
  304. )
  305. diagnostic = {
  306. "contract": QCAL_TOOL_CONTRACT_VERSION,
  307. "sourceFingerprint": QCAL_TOOL_SOURCE_FINGERPRINT,
  308. "snapshotType": type(raw).__name__,
  309. "bookingPostObserved": packet is not None,
  310. "requestMatches": request_matches,
  311. "responseStatus": _safe_http_status_shape(
  312. getattr(response, "status", None)
  313. ),
  314. "responsePath": response_path,
  315. "locationPath": location_path,
  316. "browserPath": browser_path,
  317. "bookingFormPresent": _safe_boolean_shape(
  318. snapshot.get("bookingFormPresent")
  319. ),
  320. "otpValuePresent": _safe_boolean_shape(
  321. snapshot.get("otpValuePresent")
  322. ),
  323. "otpInvalidVisible": _safe_boolean_shape(
  324. snapshot.get("otpInvalidVisible")
  325. ),
  326. "avantiDisabled": _safe_boolean_shape(
  327. snapshot.get("avantiDisabled")
  328. ),
  329. "turnstileReady": _safe_boolean_shape(
  330. snapshot.get("turnstileTokenPresent")
  331. ),
  332. "visibleValidationCount": _safe_nonnegative_int_shape(
  333. snapshot.get("visibleValidationCount")
  334. ),
  335. "visibleConfirmCount": _safe_nonnegative_int_shape(
  336. snapshot.get("visibleConfirmCount")
  337. ),
  338. "finalRequestCount": _safe_nonnegative_int_shape(
  339. snapshot.get("finalRequestCount")
  340. ),
  341. }
  342. if packet is None:
  343. outcome = "booking_post_not_observed"
  344. elif browser_path == "booking":
  345. outcome = "booking_form_returned"
  346. elif response_path == "calendar" and browser_path != "calendar":
  347. outcome = "calendar_response_not_rendered"
  348. else:
  349. outcome = "unexpected_transition"
  350. return {"outcome": outcome, **diagnostic}
  351. def safe_preflight_diagnostic(
  352. raw: object,
  353. *,
  354. stage: BookingPreflightStage,
  355. outcome: str,
  356. ) -> dict[str, object]:
  357. """Return an allowlisted diagnostic with no token or form values."""
  358. snapshot = raw if isinstance(raw, dict) else {}
  359. return {
  360. "contract": QCAL_TOOL_CONTRACT_VERSION,
  361. "sourceFingerprint": QCAL_TOOL_SOURCE_FINGERPRINT,
  362. "outcome": (
  363. outcome
  364. if outcome in SAFE_PREFLIGHT_DIAGNOSTIC_OUTCOMES
  365. else "invalid"
  366. ),
  367. "stage": (
  368. stage.value
  369. if isinstance(stage, BookingPreflightStage)
  370. else "invalid"
  371. ),
  372. "snapshotType": type(raw).__name__,
  373. "otpDisabled": _safe_boolean_shape(
  374. snapshot.get("otpDisabled")
  375. ),
  376. "otpValuePresent": _safe_boolean_shape(
  377. snapshot.get("otpValuePresent")
  378. ),
  379. "avantiDisabled": _safe_boolean_shape(
  380. snapshot.get("avantiDisabled")
  381. ),
  382. "turnstileReady": _safe_boolean_shape(
  383. snapshot.get("turnstileTokenPresent")
  384. ),
  385. }
  386. def safe_next_month_diagnostic(raw: object) -> dict[str, object]:
  387. """Return only fixed-shape evidence for the Calendar month control."""
  388. snapshot = raw if isinstance(raw, dict) else {}
  389. return {
  390. "contract": QCAL_TOOL_CONTRACT_VERSION,
  391. "sourceFingerprint": QCAL_TOOL_SOURCE_FINGERPRINT,
  392. "snapshotType": type(raw).__name__,
  393. "pathMatches": _safe_boolean_shape(
  394. snapshot.get("pathMatches")
  395. ),
  396. "candidateCount": _safe_nonnegative_int_shape(
  397. snapshot.get("candidateCount")
  398. ),
  399. "actionableCount": _safe_nonnegative_int_shape(
  400. snapshot.get("actionableCount")
  401. ),
  402. "clicked": _safe_boolean_shape(snapshot.get("clicked")),
  403. }
  404. def _print_preflight_diagnostic(
  405. raw: object,
  406. *,
  407. stage: BookingPreflightStage,
  408. outcome: str,
  409. ) -> None:
  410. print(
  411. "ITA_QCAL_PREFLIGHT_DIAGNOSTIC="
  412. + json.dumps(
  413. safe_preflight_diagnostic(
  414. raw,
  415. stage=stage,
  416. outcome=outcome,
  417. ),
  418. sort_keys=True,
  419. ),
  420. flush=True,
  421. )
  422. def _print_next_month_diagnostic(raw: object) -> None:
  423. print(
  424. "ITA_QCAL_NEXT_MONTH_DIAGNOSTIC="
  425. + json.dumps(
  426. safe_next_month_diagnostic(raw),
  427. sort_keys=True,
  428. ),
  429. flush=True,
  430. )
  431. def _print_avanti_failure_diagnostic(
  432. page: object,
  433. packet: object,
  434. *,
  435. booking_path: str,
  436. ) -> dict[str, object]:
  437. """Print one bounded allowlist even when the page snapshot itself fails."""
  438. try:
  439. raw = page.run_js(AVANTI_FAILURE_DIAGNOSTIC_SCRIPT)
  440. except Exception:
  441. raw = None
  442. diagnostic = safe_avanti_failure_diagnostic(
  443. packet,
  444. raw,
  445. booking_path=booking_path,
  446. )
  447. print(
  448. "ITA_QCAL_AVANTI_FAILURE_DIAGNOSTIC="
  449. + json.dumps(diagnostic, sort_keys=True),
  450. flush=True,
  451. )
  452. return diagnostic
  453. @dataclass(frozen=True, slots=True)
  454. class ReservationSnapshot:
  455. reservations: int
  456. waitlist: int
  457. def __post_init__(self) -> None:
  458. _require_plain_nonnegative_int(self.reservations, "reservations")
  459. _require_plain_nonnegative_int(self.waitlist, "waitlist")
  460. @property
  461. def total(self) -> int:
  462. return self.reservations + self.waitlist
  463. @dataclass(frozen=True, slots=True)
  464. class CalendarMonthScanResult:
  465. probes: tuple[CalendarProbeResult, ...]
  466. requested_forward_months: int
  467. stop_reason: str
  468. def __post_init__(self) -> None:
  469. if (
  470. isinstance(self.requested_forward_months, bool)
  471. or not isinstance(self.requested_forward_months, int)
  472. or not 1 <= self.requested_forward_months <= QCAL_FORWARD_MONTHS_MAX
  473. ):
  474. raise CalendarGateError(
  475. "requested_forward_months must be between 1 and 12"
  476. )
  477. if not self.probes or any(
  478. not isinstance(probe, CalendarProbeResult)
  479. for probe in self.probes
  480. ):
  481. raise CalendarGateError(
  482. "Calendar month scan requires parsed probe results"
  483. )
  484. if len(self.probes) > self.requested_forward_months + 1:
  485. raise CalendarGateError(
  486. "Calendar month scan exceeded its forward-month limit"
  487. )
  488. for previous, current in zip(self.probes, self.probes[1:]):
  489. if current.selected_month != _next_month_value(
  490. previous.selected_month
  491. ):
  492. raise CalendarGateError(
  493. "Calendar request did not advance exactly one month"
  494. )
  495. allowed_stop_reasons = {
  496. "forward_limit_reached",
  497. "next_control_unavailable",
  498. }
  499. if self.stop_reason not in allowed_stop_reasons:
  500. raise CalendarGateError(
  501. "Calendar month scan used an unknown stop reason"
  502. )
  503. if (
  504. self.stop_reason == "forward_limit_reached"
  505. and len(self.probes) != self.requested_forward_months + 1
  506. ):
  507. raise CalendarGateError(
  508. "Calendar month scan ended before its forward limit"
  509. )
  510. @property
  511. def availability_requests(self) -> int:
  512. return len(self.probes)
  513. @property
  514. def forward_clicks(self) -> int:
  515. return len(self.probes) - 1
  516. @property
  517. def earliest_available_date(self) -> date | None:
  518. return min(
  519. (
  520. day.day
  521. for probe in self.probes
  522. for day in probe.days
  523. if day.is_available
  524. ),
  525. default=None,
  526. )
  527. def safe_dict(self) -> dict[str, object]:
  528. earliest = self.earliest_available_date
  529. return {
  530. "requestedForwardMonths": self.requested_forward_months,
  531. "forwardClicks": self.forward_clicks,
  532. "availabilityRequests": self.availability_requests,
  533. "startMonth": _month_text(self.probes[0].selected_month),
  534. "furthestMonth": _month_text(self.probes[-1].selected_month),
  535. "monthsWithAvailability": sum(
  536. probe.available_count > 0 for probe in self.probes
  537. ),
  538. "earliestAvailableDate": (
  539. earliest.isoformat() if earliest is not None else None
  540. ),
  541. "stopReason": self.stop_reason,
  542. "nextControlUnavailable": (
  543. self.stop_reason == "next_control_unavailable"
  544. ),
  545. }
  546. @dataclass(frozen=True, slots=True)
  547. class CalendarGateEvidence:
  548. before: ReservationSnapshot
  549. after: ReservationSnapshot
  550. otp_requests: int
  551. avanti_posts: int
  552. availability_requests: int
  553. final_requests: int
  554. calendar_ready: bool
  555. def __post_init__(self) -> None:
  556. for field_name in (
  557. "otp_requests",
  558. "avanti_posts",
  559. "availability_requests",
  560. "final_requests",
  561. ):
  562. _require_plain_nonnegative_int(
  563. getattr(self, field_name),
  564. field_name,
  565. )
  566. if not isinstance(self.calendar_ready, bool):
  567. raise CalendarGateError("calendar_ready must be a boolean")
  568. @property
  569. def passed(self) -> bool:
  570. return (
  571. self.before == self.after
  572. and self.otp_requests == 1
  573. and self.avanti_posts == 1
  574. and self.availability_requests >= 2
  575. and self.final_requests == 0
  576. and self.calendar_ready
  577. )
  578. def safe_dict(self) -> dict[str, object]:
  579. return {
  580. "reservationCountBefore": self.before.reservations,
  581. "waitlistCountBefore": self.before.waitlist,
  582. "reservationCountAfter": self.after.reservations,
  583. "waitlistCountAfter": self.after.waitlist,
  584. "otpRequests": self.otp_requests,
  585. "avantiPosts": self.avanti_posts,
  586. "availabilityRequests": self.availability_requests,
  587. "finalRequests": self.final_requests,
  588. "calendarReady": self.calendar_ready,
  589. "passed": self.passed,
  590. }
  591. class OneShotAction:
  592. """An in-memory at-most-once guard that burns before the action starts."""
  593. def __init__(self, name: str) -> None:
  594. self.name = name
  595. self.attempted = False
  596. def run(self, action: Callable[[], Any]) -> Any:
  597. if self.attempted:
  598. raise CalendarGateError(f"{self.name} was already attempted")
  599. self.attempted = True
  600. return action()
  601. def _required_env_text(env: Mapping[str, str], name: str) -> str:
  602. value = _env_text(env, name)
  603. if not value:
  604. raise CalendarGateError(f"{name} is required for Q-CAL-1")
  605. return value
  606. def resolve_qcal_forward_months(env: Mapping[str, str]) -> int:
  607. """Resolve the debug-only bound without changing route semantics."""
  608. raw = _env_text(env, QCAL_FORWARD_MONTHS_ENV)
  609. if not raw:
  610. return QCAL_FORWARD_MONTHS_DEFAULT
  611. try:
  612. value = int(raw)
  613. except ValueError as exc:
  614. raise CalendarGateError(
  615. f"{QCAL_FORWARD_MONTHS_ENV} must be an integer between 1 and 12"
  616. ) from exc
  617. if not 1 <= value <= QCAL_FORWARD_MONTHS_MAX:
  618. raise CalendarGateError(
  619. f"{QCAL_FORWARD_MONTHS_ENV} must be between 1 and 12"
  620. )
  621. return value
  622. def _split_labels(raw: str, name: str) -> list[str]:
  623. labels = [label.strip() for label in raw.split("|") if label.strip()]
  624. if not labels:
  625. raise CalendarGateError(f"{name} must contain at least one label")
  626. return labels
  627. def build_calendar_gate_config(
  628. env: Mapping[str, str],
  629. *,
  630. username: str,
  631. password: str,
  632. persistent_profile_path: Path | None = None,
  633. route: ItaDebugRoute | None = None,
  634. ) -> VSPlgConfig:
  635. """Build one explicit live-route config while keeping production mode off."""
  636. resolved_debug_route = route or resolve_debug_route(env)
  637. values = {
  638. name: _required_env_text(env, name)
  639. for name in FORM_REQUIRED_ENV_NAMES
  640. }
  641. try:
  642. lookahead_months = int(values["ITA_CALENDAR_LOOKAHEAD_MONTHS"])
  643. except ValueError as exc:
  644. raise CalendarGateError(
  645. "ITA_CALENDAR_LOOKAHEAD_MONTHS must be a positive integer"
  646. ) from exc
  647. config = build_query_config(
  648. env,
  649. username=username,
  650. password=password,
  651. persistent_profile_path=persistent_profile_path,
  652. route=resolved_debug_route,
  653. )
  654. live_route = {
  655. "service_id": resolved_debug_route.service_id,
  656. "capability": "single_schengen_tourism",
  657. "calendar_scan": {"lookahead_months": lookahead_months},
  658. "form": {
  659. "booking_type_control_id": values[
  660. "ITA_BOOKING_TYPE_CONTROL_ID"
  661. ],
  662. "booking_type_value": values["ITA_BOOKING_TYPE_VALUE"],
  663. "booking_type_labels": _split_labels(
  664. values["ITA_BOOKING_TYPE_LABELS"],
  665. "ITA_BOOKING_TYPE_LABELS",
  666. ),
  667. "purpose_control_id": values["ITA_PURPOSE_CONTROL_ID"],
  668. "purpose_value": values["ITA_PURPOSE_VALUE"],
  669. "purpose_labels": _split_labels(
  670. values["ITA_PURPOSE_LABELS"],
  671. "ITA_PURPOSE_LABELS",
  672. ),
  673. },
  674. }
  675. config.free_config["apt_configs"] = {
  676. resolved_debug_route.routing_key: live_route,
  677. }
  678. try:
  679. resolve_route_config(
  680. config.free_config["apt_configs"],
  681. resolved_debug_route.routing_key,
  682. )
  683. except ItaProtocolError as exc:
  684. raise CalendarGateError(
  685. f"Italy Q-CAL route configuration rejected ({exc.code})"
  686. ) from exc
  687. return config
  688. def wait_for_booking_preflight(page, *, script, stage=BookingPreflightStage.BEFORE_OTP_SEND, timeout=120):
  689. return calendar_session.wait_for_booking_preflight(
  690. page, script=script, stage=stage, timeout=timeout,
  691. diagnostic=_print_preflight_diagnostic,
  692. )
  693. def read_reservation_snapshot(
  694. page: object,
  695. *,
  696. timeout: float = 20,
  697. settle_seconds: float = 5,
  698. ) -> ReservationSnapshot:
  699. """Read both row counts after the page has had time to initialize tables."""
  700. tab = page.new_tab(RESERVATION_URL)
  701. try:
  702. tab.wait.doc_loaded()
  703. deadline = time.monotonic() + timeout
  704. settle_deadline = time.monotonic() + settle_seconds
  705. last_raw: object = None
  706. last_snapshot: ReservationSnapshot | None = None
  707. stable_samples = 0
  708. while time.monotonic() < deadline:
  709. last_raw = tab.run_js(RESERVATION_SNAPSHOT_SCRIPT)
  710. if (
  711. isinstance(last_raw, dict)
  712. and last_raw.get("path") == "/Reservation"
  713. and last_raw.get("authenticated") is True
  714. and last_raw.get("tableCount") == 2
  715. and last_raw.get("ready") is True
  716. and time.monotonic() >= settle_deadline
  717. ):
  718. snapshot = ReservationSnapshot(
  719. reservations=last_raw.get("reservations"),
  720. waitlist=last_raw.get("waitlist"),
  721. )
  722. if snapshot == last_snapshot:
  723. stable_samples += 1
  724. else:
  725. last_snapshot = snapshot
  726. stable_samples = 1
  727. if stable_samples >= 2:
  728. return snapshot
  729. time.sleep(0.25)
  730. raise CalendarGateError(
  731. "Reservation DataTables did not become ready before timeout"
  732. )
  733. finally:
  734. tab.close()
  735. def _calendar_guard(page: object) -> dict[str, object]:
  736. return guard_calendar(page)
  737. def _click_next_month(page: object, *, timeout: float = 10) -> None:
  738. click_next_month(
  739. page,
  740. timeout=timeout,
  741. diagnostic=_print_next_month_diagnostic,
  742. )
  743. def scan_calendar_horizon(
  744. page: object,
  745. *,
  746. forward_months: int,
  747. ) -> CalendarMonthScanResult:
  748. """Probe the initial month and a bounded number of native next-month pages."""
  749. if (
  750. isinstance(forward_months, bool)
  751. or not isinstance(forward_months, int)
  752. or not 1 <= forward_months <= QCAL_FORWARD_MONTHS_MAX
  753. ):
  754. raise CalendarGateError("forward_months must be between 1 and 12")
  755. initial_packet = listen_once(
  756. page,
  757. target=AVAILABILITY_PATH,
  758. method="POST",
  759. action=lambda: _refresh_calendar(page),
  760. timeout=30,
  761. )
  762. probes = [parse_availability_packet(initial_packet)]
  763. _calendar_guard(page)
  764. print(
  765. "ITA_QCAL_PROBE index=0 "
  766. f"items={probes[0].item_count} "
  767. f"available={probes[0].available_count}"
  768. )
  769. stop_reason = "forward_limit_reached"
  770. for index in range(1, forward_months + 1):
  771. time.sleep(QCAL_MONTH_CLICK_INTERVAL_SECONDS)
  772. try:
  773. packet = listen_once(
  774. page,
  775. target=AVAILABILITY_PATH,
  776. method="POST",
  777. action=lambda: _click_next_month(page),
  778. timeout=30,
  779. )
  780. except CalendarNextMonthUnavailableError:
  781. stop_reason = "next_control_unavailable"
  782. break
  783. probe = parse_availability_packet(packet)
  784. if probe.selected_month != _next_month_value(
  785. probes[-1].selected_month
  786. ):
  787. raise CalendarGateError(
  788. "Calendar request did not advance exactly one month"
  789. )
  790. probes.append(probe)
  791. _calendar_guard(page)
  792. print(
  793. f"ITA_QCAL_PROBE index={index} "
  794. f"items={probe.item_count} available={probe.available_count}"
  795. )
  796. result = CalendarMonthScanResult(
  797. probes=tuple(probes),
  798. requested_forward_months=forward_months,
  799. stop_reason=stop_reason,
  800. )
  801. print(
  802. "ITA_QCAL_MONTH_SCAN_RESULT="
  803. + json.dumps(result.safe_dict(), sort_keys=True)
  804. )
  805. return result
  806. def _refresh_calendar(page: object) -> None:
  807. refresh_calendar(page)
  808. def validate_production_query_result(
  809. result: object,
  810. scan: object,
  811. *,
  812. apt_type: AppointmentType,
  813. lookahead_months: int,
  814. ) -> dict[str, object]:
  815. """Compare the public VFS-style result with this call's real scan evidence."""
  816. if not isinstance(result, VSQueryResult):
  817. raise CalendarGateError("Production Query did not return VSQueryResult")
  818. if not isinstance(scan, CalendarQueryScanResult):
  819. raise CalendarGateError("Production Query did not expose fresh scan evidence")
  820. if scan.lookahead_months != lookahead_months:
  821. raise CalendarGateError("Production Query used a different scan horizon")
  822. if result.apt_type != apt_type:
  823. raise CalendarGateError("Production Query returned a different route")
  824. earliest = scan.earliest_available_date
  825. positive = earliest is not None
  826. expected_date = (
  827. datetime.combine(earliest, datetime.min.time())
  828. if positive else None
  829. )
  830. expected_status = (
  831. AvailabilityStatus.Available if positive
  832. else AvailabilityStatus.NoneAvailable
  833. )
  834. if (
  835. result.success is not positive
  836. or result.availability_status is not expected_status
  837. or result.earliest_date != expected_date
  838. or len(result.availability) != int(positive)
  839. ):
  840. raise CalendarGateError("Production Query result disagrees with scan evidence")
  841. if positive and (
  842. result.availability[0].date != expected_date
  843. or result.availability[0].times != []
  844. ):
  845. raise CalendarGateError("Production Query must publish one earliest date without times")
  846. # Exercise the existing Sentinel serialization, without publishing to Redis.
  847. snapshot = result.to_snapshot_payload()
  848. expected_snapshot = {
  849. "routing_key": apt_type.routing_key,
  850. "country": apt_type.country,
  851. "city": apt_type.city,
  852. "visa_type": apt_type.visa_type,
  853. "availability_status": expected_status.value,
  854. "earliest_date": earliest.isoformat() if positive else None,
  855. "availability": [{"date": earliest.isoformat()}] if positive else [],
  856. "snapshot_source": "worker",
  857. }
  858. if (
  859. set(snapshot) != set(expected_snapshot) | {"snapshot_at"}
  860. or any(snapshot.get(key) != value for key, value in expected_snapshot.items())
  861. or not isinstance(snapshot.get("snapshot_at"), str)
  862. ):
  863. raise CalendarGateError("Production Query snapshot contract changed")
  864. return {
  865. "lookaheadMonths": lookahead_months,
  866. "scannedMonths": scan.scanned_months,
  867. "months": [
  868. {"month": _month_text(probe.selected_month), "positiveDays": probe.available_count}
  869. for probe in scan.probes
  870. ],
  871. "stopReason": scan.stop_reason,
  872. "success": result.success,
  873. "availabilityStatus": result.availability_status.value,
  874. "earliestDate": earliest.isoformat() if positive else None,
  875. "availabilityCount": len(result.availability),
  876. "timesCount": 0,
  877. "snapshotVerified": True,
  878. }
  879. def _query_request_delta(
  880. before: Mapping[str, int],
  881. after: Mapping[str, int],
  882. *,
  883. expected_availability: int,
  884. ) -> dict[str, int]:
  885. keys = {"otpRequests", "avantiPosts", "availabilityRequests", "finalRequests"}
  886. if set(before) != keys or set(after) != keys:
  887. raise CalendarGateError("Production Query request counters changed shape")
  888. delta = {}
  889. for key in keys:
  890. previous = _require_plain_nonnegative_int(before[key], key)
  891. current = _require_plain_nonnegative_int(after[key], key)
  892. delta[key] = _require_plain_nonnegative_int(current - previous, key)
  893. if (
  894. delta["otpRequests"] != 0
  895. or delta["avantiPosts"] != 0
  896. or delta["finalRequests"] != 0
  897. or delta["availabilityRequests"] != expected_availability
  898. ):
  899. raise CalendarGateError("Production Query exceeded its read-only request contract")
  900. return delta
  901. def run_production_query_acceptance(
  902. plugin: ItaPlugin,
  903. route: ItaDebugRoute,
  904. request_audit: CalendarRequestAudit,
  905. ) -> tuple[dict[str, object], ...]:
  906. """Call the real Query twice; never replace its scanner or recreate Session."""
  907. free_config = plugin.free_config
  908. if (
  909. free_config.get("query_mode") != SERVICES_PROBE_QUERY_MODE
  910. or free_config.get("final_submit_enabled") is not False
  911. ):
  912. raise CalendarGateError("Production Query acceptance requires the safe debug config")
  913. protocol_route = resolve_route_config(free_config["apt_configs"], route.routing_key)
  914. if protocol_route.calendar_scan is None:
  915. raise CalendarGateError("Production Query acceptance requires a Calendar route")
  916. page = plugin.page
  917. reports = []
  918. start_month = None
  919. free_config["query_mode"] = CALENDAR_QUERY_MODE
  920. try:
  921. for attempt in range(1, QCAL_PRODUCTION_QUERY_ATTEMPTS + 1):
  922. if attempt > 1:
  923. time.sleep(QCAL_QUERY_INTERVAL_SECONDS)
  924. print(
  925. "ITA_QCAL_PRODUCTION_QUERY_START "
  926. f"attempt={attempt} total={QCAL_PRODUCTION_QUERY_ATTEMPTS}",
  927. flush=True,
  928. )
  929. before = request_audit.settled_snapshot()
  930. previous_scan = getattr(plugin, "last_calendar_query_scan", None)
  931. apt_type = route.appointment_type()
  932. result = plugin.query(apt_type)
  933. scan = getattr(plugin, "last_calendar_query_scan", None)
  934. if scan is previous_scan:
  935. raise CalendarGateError("Production Query reused stale scan evidence")
  936. if plugin.page is not page or plugin.free_config is not free_config:
  937. raise CalendarGateError("Production Query changed its prepared Session")
  938. report = validate_production_query_result(
  939. result,
  940. scan,
  941. apt_type=apt_type,
  942. lookahead_months=protocol_route.calendar_scan.lookahead_months,
  943. )
  944. current_start = scan.probes[0].selected_month
  945. if start_month is not None and current_start != start_month:
  946. raise CalendarGateError("Repeated Query did not restart from the same initial month")
  947. start_month = current_start
  948. after = request_audit.settled_snapshot()
  949. report["queryRequestDelta"] = _query_request_delta(
  950. before,
  951. after,
  952. expected_availability=scan.scanned_months,
  953. )
  954. _calendar_guard(page)
  955. report["attempt"] = attempt
  956. reports.append(report)
  957. print(
  958. "ITA_QCAL_PRODUCTION_QUERY_RESULT="
  959. + json.dumps(report, sort_keys=True),
  960. flush=True,
  961. )
  962. finally:
  963. # Do not call set_config(): it would erase the prepared-route proof.
  964. # This is an in-memory mode switch, never a production config write.
  965. free_config["query_mode"] = SERVICES_PROBE_QUERY_MODE
  966. return tuple(reports)
  967. def run_calendar_gate(
  968. env: Mapping[str, str],
  969. *,
  970. otp_reader: Callable[[], str] = _read_manual_otp,
  971. ) -> int:
  972. print(
  973. "ITA_QCAL_TOOL_CONTRACT "
  974. f"version={QCAL_TOOL_CONTRACT_VERSION} "
  975. f"source_sha256={QCAL_TOOL_SOURCE_FINGERPRINT}",
  976. flush=True,
  977. )
  978. mode = resolve_qcal_mode(env)
  979. if mode is CalendarGateMode.CALENDAR_SOAK:
  980. # Validate limits before any credentials, browser, OTP, or form action.
  981. from tools.debug_ita_soak import calendar_soak_limits
  982. calendar_soak_limits(env)
  983. forward_months = (
  984. resolve_qcal_forward_months(env)
  985. if mode is CalendarGateMode.DISCOVERY_HORIZON else None
  986. )
  987. route = resolve_debug_route(env)
  988. chrome_path = resolve_chrome_path(env)
  989. os.environ["CHROME_BIN"] = str(chrome_path)
  990. username, password = _get_query_credentials(env, non_interactive=False)
  991. profile_path = _prepare_query_profile(username)
  992. config = build_calendar_gate_config(
  993. env,
  994. username=username,
  995. password=password,
  996. persistent_profile_path=profile_path,
  997. route=route,
  998. )
  999. protocol_route = resolve_route_config(
  1000. config.free_config["apt_configs"],
  1001. route.routing_key,
  1002. )
  1003. booking_path = f"/Services/Booking/{protocol_route.service_id}"
  1004. print(
  1005. f"ITA_QCAL_MODE mode={mode.value} "
  1006. f"lookahead_months={protocol_route.calendar_scan.lookahead_months} "
  1007. f"query_attempts={QCAL_PRODUCTION_QUERY_ATTEMPTS if mode is CalendarGateMode.PRODUCTION_QUERY else 0}",
  1008. flush=True,
  1009. )
  1010. plugin = ItaPlugin(group_id="ita-qcal-gate")
  1011. plugin.set_log(lambda message: print(message, flush=True))
  1012. plugin.set_config(config)
  1013. otp_guard = OneShotAction("OTP")
  1014. avanti_guard = OneShotAction("AVANTI")
  1015. cleanup_error: Exception | None = None
  1016. request_audit: CalendarRequestAudit | None = None
  1017. exit_code = 0
  1018. try:
  1019. plugin.create_session()
  1020. if mode in {CalendarGateMode.PRODUCTION_QUERY, CalendarGateMode.CALENDAR_SOAK}:
  1021. request_audit = CalendarRequestAudit(
  1022. plugin.page, include_time_slots=mode is CalendarGateMode.CALENDAR_SOAK,
  1023. )
  1024. request_audit.start()
  1025. before = read_reservation_snapshot(plugin.page)
  1026. print(
  1027. "ITA_QCAL_RESERVATION_BEFORE "
  1028. f"reservations={before.reservations} waitlist={before.waitlist}"
  1029. )
  1030. entry = plugin.query(route.appointment_type())
  1031. if not entry.success:
  1032. if mode is CalendarGateMode.PRODUCTION_QUERY:
  1033. raise PositiveQueryEvidenceUnavailable(
  1034. "Service currently has no entry; positive Query gate was not exercised"
  1035. )
  1036. raise CalendarGateError(
  1037. "Configured service did not reach the booking form"
  1038. )
  1039. preflight_script = _booking_preflight_script(
  1040. protocol_route,
  1041. booking_path,
  1042. )
  1043. wait_for_booking_preflight(
  1044. plugin.page,
  1045. script=preflight_script,
  1046. stage=BookingPreflightStage.BEFORE_OTP_SEND,
  1047. )
  1048. otp_packet = otp_guard.run(
  1049. lambda: listen_once(
  1050. plugin.page,
  1051. target=OTP_PATH,
  1052. method="GET",
  1053. action=lambda: plugin.mouse.human_click_ele(
  1054. _element_or_fail(plugin.page, "#otp-send")
  1055. ),
  1056. timeout=30,
  1057. )
  1058. )
  1059. validate_otp_packet(
  1060. otp_packet,
  1061. service_id=protocol_route.service_id,
  1062. )
  1063. print("ITA_QCAL_OTP_SENT requests=1")
  1064. otp = otp_reader()
  1065. _enter_otp(plugin, otp)
  1066. del otp
  1067. wait_for_booking_preflight(
  1068. plugin.page,
  1069. script=preflight_script,
  1070. stage=BookingPreflightStage.AFTER_OTP_SEND,
  1071. )
  1072. plugin.page.set.auto_handle_alert(accept=True)
  1073. avanti_packet = None
  1074. try:
  1075. avanti_packet = avanti_guard.run(
  1076. lambda: listen_once(
  1077. plugin.page,
  1078. target=booking_path,
  1079. method="POST",
  1080. action=lambda: plugin.mouse.human_click_ele(
  1081. _element_or_fail(plugin.page, "#btnAvanti")
  1082. ),
  1083. timeout=30,
  1084. )
  1085. )
  1086. print("ITA_QCAL_AVANTI_POST_OBSERVED requests=1")
  1087. _wait_for_calendar(plugin.page)
  1088. transition_mode = validate_avanti_packet(
  1089. avanti_packet,
  1090. booking_path=booking_path,
  1091. final_page_url=str(plugin.page.url or ""),
  1092. )
  1093. plugin._record_calendar_query_prepared_route(protocol_route)
  1094. except Exception:
  1095. _print_avanti_failure_diagnostic(
  1096. plugin.page,
  1097. avanti_packet,
  1098. booking_path=booking_path,
  1099. )
  1100. raise
  1101. finally:
  1102. if mode is CalendarGateMode.CALENDAR_SOAK:
  1103. plugin.page.set.auto_handle_alert(accept=False)
  1104. print(f"ITA_QCAL_AVANTI_TRANSITION mode={transition_mode}")
  1105. first_guard = _calendar_guard(plugin.page)
  1106. print("ITA_QCAL_CALENDAR_READY avanti_posts=1 final_requests=0")
  1107. if mode is CalendarGateMode.CALENDAR_SOAK:
  1108. # Transfer ownership, including cleanup, to the existing soak loop.
  1109. # Never leave alert auto-accept enabled during monitoring/recovery.
  1110. from tools.debug_ita_soak import run_prepared_calendar_soak
  1111. owned_plugin, owned_audit = plugin, request_audit
  1112. plugin, request_audit = None, None
  1113. return run_prepared_calendar_soak(
  1114. env, plugin=owned_plugin, route=route,
  1115. request_audit=owned_audit, before=before,
  1116. )
  1117. production_reports = ()
  1118. if mode is CalendarGateMode.PRODUCTION_QUERY:
  1119. preparation_counts = request_audit.settled_snapshot()
  1120. if (
  1121. preparation_counts["otpRequests"] != 1
  1122. or preparation_counts["avantiPosts"] != 1
  1123. or preparation_counts["finalRequests"] != 0
  1124. ):
  1125. raise CalendarGateError("Calendar preparation exceeded its one-shot request contract")
  1126. production_reports = run_production_query_acceptance(plugin, route, request_audit)
  1127. availability_requests = sum(
  1128. report["queryRequestDelta"]["availabilityRequests"]
  1129. for report in production_reports
  1130. )
  1131. else:
  1132. scan_result = scan_calendar_horizon(
  1133. plugin.page,
  1134. forward_months=forward_months,
  1135. )
  1136. availability_requests = scan_result.availability_requests
  1137. print(
  1138. "ITA_QCAL_MONTH_NAVIGATION_VERIFIED "
  1139. f"forward_clicks={scan_result.forward_clicks}"
  1140. )
  1141. last_guard = _calendar_guard(plugin.page)
  1142. after = read_reservation_snapshot(plugin.page)
  1143. print(
  1144. "ITA_QCAL_RESERVATION_AFTER "
  1145. f"reservations={after.reservations} waitlist={after.waitlist}"
  1146. )
  1147. final_requests = max(
  1148. first_guard["finalRequestCount"],
  1149. last_guard["finalRequestCount"],
  1150. )
  1151. observed_counts = request_audit.settled_snapshot() if request_audit is not None else None
  1152. if observed_counts is not None:
  1153. final_requests = max(final_requests, observed_counts["finalRequests"])
  1154. evidence = CalendarGateEvidence(
  1155. before=before,
  1156. after=after,
  1157. otp_requests=observed_counts["otpRequests"] if observed_counts is not None else 1,
  1158. avanti_posts=observed_counts["avantiPosts"] if observed_counts is not None else 1,
  1159. availability_requests=availability_requests,
  1160. final_requests=final_requests,
  1161. calendar_ready=True,
  1162. )
  1163. gate_result = evidence.safe_dict()
  1164. prefix = "ITA_QCAL_GATE_RESULT="
  1165. if mode is CalendarGateMode.PRODUCTION_QUERY:
  1166. positive_queries = sum(report["success"] for report in production_reports)
  1167. gate_result.update({
  1168. "mode": mode.value,
  1169. "queryAttempts": len(production_reports),
  1170. "positiveQueries": positive_queries,
  1171. "passed": (
  1172. evidence.passed
  1173. and len(production_reports) == QCAL_PRODUCTION_QUERY_ATTEMPTS
  1174. and positive_queries > 0
  1175. ),
  1176. })
  1177. prefix = "ITA_QCAL_PRODUCTION_QUERY_GATE_RESULT="
  1178. print(prefix + json.dumps(gate_result, sort_keys=True), flush=True)
  1179. if (
  1180. mode is CalendarGateMode.PRODUCTION_QUERY
  1181. and len(production_reports) != QCAL_PRODUCTION_QUERY_ATTEMPTS
  1182. ):
  1183. raise CalendarGateError("Production gate requires exactly two Query calls")
  1184. if not evidence.passed:
  1185. raise CalendarGateError("Q-CAL-1 evidence did not pass")
  1186. if mode is CalendarGateMode.PRODUCTION_QUERY and not positive_queries:
  1187. raise PositiveQueryEvidenceUnavailable(
  1188. "Both Query calls completed without positive inventory; positive gate remains open"
  1189. )
  1190. except PositiveQueryEvidenceUnavailable as exc:
  1191. print(f"ITA_QCAL_PRODUCTION_QUERY_NOT_READY message={_text_for_log(exc)}", flush=True)
  1192. exit_code = 2
  1193. except (CalendarGateError, DebugConfigurationError) as exc:
  1194. print(
  1195. f"ITA_QCAL_GATE_FAILED type={type(exc).__name__} "
  1196. f"message={_text_for_log(exc)}"
  1197. )
  1198. exit_code = 1
  1199. except Exception as exc:
  1200. print(
  1201. f"ITA_QCAL_GATE_FAILED type={type(exc).__name__} "
  1202. f"message={_text_for_log(exc)}"
  1203. )
  1204. exit_code = 1
  1205. finally:
  1206. if request_audit is not None:
  1207. try:
  1208. request_audit.stop()
  1209. except Exception as exc:
  1210. cleanup_error = exc
  1211. print(
  1212. "ITA_QCAL_AUDIT_CLEANUP_FAILED "
  1213. f"type={type(exc).__name__} message={_text_for_log(exc)}",
  1214. file=sys.stderr,
  1215. )
  1216. try:
  1217. if plugin is not None:
  1218. plugin.cleanup()
  1219. print("ITA_QCAL_GATE_CLEANUP_OK")
  1220. except Exception as exc:
  1221. cleanup_error = exc
  1222. print(
  1223. f"ITA_QCAL_GATE_CLEANUP_FAILED type={type(exc).__name__} "
  1224. f"message={_text_for_log(exc)}",
  1225. file=sys.stderr,
  1226. )
  1227. return 3 if cleanup_error is not None else exit_code
  1228. def main() -> int:
  1229. return run_calendar_gate(os.environ)
  1230. if __name__ == "__main__":
  1231. raise SystemExit(main())