ita_query_book_local.py 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  1. """Finite live acceptance wiring, not a second scheduler.
  2. Execute existing GCO spawn/monitor/trigger/stop methods. Only order API and
  3. telemetry are substituted; pool allocation and Italy OTP keep their SDK.
  4. Redis is an owned, non-persistent Unix-socket subprocess (never shared Redis).
  5. This module is loaded only by explicit --local-preflight / --local-run.
  6. """
  7. from __future__ import annotations
  8. from contextlib import ExitStack, contextmanager
  9. import copy
  10. import hashlib
  11. import json
  12. import os
  13. from pathlib import Path
  14. import shutil
  15. import socket
  16. import subprocess
  17. import tempfile
  18. import threading
  19. import time
  20. from types import SimpleNamespace
  21. from unittest.mock import patch
  22. from uuid import uuid4
  23. import redis
  24. import configure
  25. from booker import OrderBookerGCO
  26. from sentinel import SentinelGCO
  27. from plugins.ita_calendar_book import book_date_preferences, read_calendar_service, verify_precommit_selection
  28. from plugins.ita_calendar_session import CalendarRequestAudit
  29. from plugins.ita_otp import validate_cloud_otp_environment, validate_cloud_otp_logging
  30. from plugins.ita_plugin import (
  31. BOOKING_CALENDAR_PREFINAL_STOP_LABEL, BOOKING_RECHECK_NONE_AVAILABLE_LABEL,
  32. PERSISTENT_BROWSER_PROFILE_KEY,
  33. )
  34. from plugins.ita_protocol import ItaCapability, resolve_route_config
  35. from plugins.ita_booking_final import FinalBookingError, FinalOutcomeUnknown
  36. from plugins import ita_workflow
  37. from tools.ita_final_evidence import claim_once, require_unused_claim
  38. from tools.ita_local_handoff import ITA_LOCAL_HANDOFF_ROOT
  39. from toolkit.vs_cloud_api import VSCloudApi
  40. from tools.debug_ita_session import _get_credentials, resolve_chrome_path
  41. from tools.plugin_tester import PluginTester
  42. from utils.local_account import load_local_account
  43. from vs_types import AvailabilityStatus, GroupConfig, VSAccount
  44. CONTRACT = "existing-framework-local-prefinal-v1"
  45. FINAL_CONTRACT = "existing-framework-local-auto-submit-once-v4"
  46. ROOT = Path(__file__).resolve().parents[1]
  47. class LocalAcceptanceError(RuntimeError):
  48. """Only static diagnostic codes, never credentials or upstream bodies."""
  49. def emit(event, **fields):
  50. print("ITA_QUERY_BOOK_" + event + "=" + json.dumps(fields, sort_keys=True), flush=True)
  51. def load_settings(args):
  52. validate_cloud_otp_logging()
  53. if configure.TEST_ACCOUNT:
  54. raise LocalAcceptanceError("test_account_override_enabled")
  55. path = Path(args.config)
  56. groups = json.loads((path if path.is_absolute() else ROOT / path).read_text())
  57. matches = [g for g in groups if g.get("identifier") == args.group]
  58. if len(matches) != 1:
  59. raise LocalAcceptanceError("group_missing_or_ambiguous")
  60. source = GroupConfig.from_json(matches[0])
  61. if (source.plugin_config.plugin_name != "ita_plugin" or len(source.appointment_types) != 1
  62. or not source.sentinel.account_pool_id or source.sentinel.signal_ttl <= 0
  63. or source.free_config.get("query_mode") != "calendar"
  64. or source.free_config.get("otp_mode") != "cloud"
  65. or source.free_config.get("final_submit_enabled") is not False):
  66. raise LocalAcceptanceError("requires_single_calendar_query_group_with_cloud_otp_and_final_disabled")
  67. apt = source.appointment_types[0]
  68. route = resolve_route_config(source.free_config.get("apt_configs", {}), apt.routing_key)
  69. if (route.capability != ItaCapability.SINGLE_SCHENGEN_TOURISM
  70. or route.mail is None or route.mail.authorization_source != "account"):
  71. raise LocalAcceptanceError("requires_single_tourism_route_and_account_mailbox")
  72. preferences = {key: value for key, value in (
  73. ("expected_start_date", args.expected_start_date), ("expected_end_date", args.expected_end_date)) if value}
  74. book_date_preferences(preferences)
  75. proxy = PluginTester.parse_proxy_url(args.proxy)
  76. if proxy.ip not in {"127.0.0.1", "localhost", "::1"} or not 1 <= proxy.port <= 65535:
  77. raise LocalAcceptanceError("requires_explicit_loopback_proxy")
  78. return source, route, proxy, preferences
  79. def build_groups(source, run_id, *, submit_once=False):
  80. """Copy existing config; do not publish or edit production groups."""
  81. query = source.model_copy(deep=True)
  82. query.identifier = "ita-local-query-" + run_id
  83. query.enable, query.debug, query.need_account, query.need_proxy = True, False, True, True
  84. query.sentinel.target_instances = 1
  85. query.proxy_pool = ["local-test-proxy"]
  86. query.free_config.pop(PERSISTENT_BROWSER_PROFILE_KEY, None)
  87. query.appointment_types[0].weight = max(1, query.appointment_types[0].weight)
  88. book = query.model_copy(deep=True)
  89. book.identifier = "ita-local-book-" + run_id
  90. book.booker.account_source = "order"
  91. book.booker.target_instances = 1
  92. book.free_config.update(query_mode="services_probe", otp_mode="cloud", final_submit_enabled=submit_once)
  93. return query, book
  94. class LocalFinalSubmission:
  95. """Single-dispatch fence around the EXISTING production final workflow.
  96. No new receipt matcher, POST implementation or retry policy. Reuse the
  97. exclusive/fsynced claim helper, scoped to site + applicant + service (not
  98. run id, date or routing alias). A restart cannot silently submit again.
  99. Legacy final-evidence-once.claim is never read, removed or reset here;
  100. the production receipt baseline still rejects existing active/unknown rows.
  101. """
  102. def __init__(self, applicant, route, *, root=ITA_LOCAL_HANDOFF_ROOT):
  103. self.username = applicant.username.strip().casefold()
  104. self.route = route
  105. identity = json.dumps(["prenotami.esteri.it", self.username, str(route.service_id)])
  106. self.claim_path = Path(root) / "query-book-final" / (hashlib.sha256(identity.encode()).hexdigest() + ".claim")
  107. self.used = self.possibly_submitted = False
  108. self.selection = {}
  109. self.failure_reason = None
  110. require_unused_claim(self.claim_path) # Before browser/pool allocation.
  111. def submit(self, plugin, route, observed, production_submit):
  112. if (self.used or route != self.route or observed.get("book") is None
  113. or observed["book"].plugin is not plugin
  114. or plugin.config.account.username.strip().casefold() != self.username):
  115. raise LocalAcceptanceError("final_applicant_or_route_changed")
  116. self.used = True
  117. require_unused_claim(self.claim_path)
  118. approval = plugin.last_booking_precommit
  119. verify_precommit_selection(plugin.page, runtime_service=read_calendar_service(plugin.page), approval=approval)
  120. slot = approval.candidate
  121. self.selection = {"selectionVerified": True, "selectedDate": slot.day.isoformat(), "selectedTime": slot.time_label}
  122. emit("FINAL_TARGET", **self.selection, routingKey=route.routing_key, serviceId=route.service_id,
  123. remaining=slot.slots_free, createsRealAppointment=True, automaticSubmission=True)
  124. # Only wrap native dispatch. The production core FIRST reads a fresh
  125. # receipt baseline and rejects active/waitlist/unknown history.
  126. native_factory = ita_workflow.NativeFinalSubmitter
  127. owner = self
  128. class FencedNative:
  129. def __init__(self, page, target, current):
  130. if (page is not plugin.page or target.applicant != owner.username
  131. or target.service_id != str(route.service_id) or target.approval is not approval):
  132. raise LocalAcceptanceError("final_target_changed")
  133. self.native = native_factory(page, target, current)
  134. def __getattr__(self, name):
  135. return getattr(self.native, name)
  136. def __call__(self):
  137. if owner.possibly_submitted or plugin.last_booking_precommit is not approval:
  138. raise LocalAcceptanceError("final_already_attempted_do_not_rerun")
  139. for item in observed.values():
  140. if item.audit.settled_snapshot()["finalRequests"] != 0:
  141. raise LocalAcceptanceError("unexpected_final_request_do_not_rerun")
  142. verify_precommit_selection(plugin.page, runtime_service=read_calendar_service(plugin.page), approval=approval)
  143. claim_once(owner.claim_path)
  144. owner.possibly_submitted = True
  145. emit("FINAL_DISPATCH", automaticRetry=False, possiblySubmitted=True)
  146. return self.native()
  147. with patch.object(ita_workflow, "NativeFinalSubmitter", FencedNative):
  148. try:
  149. # This observer already owns the page's passive callback.
  150. # Lend it to production final; keep coverage through cleanup.
  151. return production_submit(plugin, route, request_audit=observed["book"].audit)
  152. except FinalBookingError:
  153. self.failure_reason = "receipt_baseline_or_final_context_rejected"
  154. raise
  155. def check_dependencies(proxy):
  156. executable = shutil.which("redis-server")
  157. if not executable:
  158. raise LocalAcceptanceError("redis_server_missing_run_brew_install_redis")
  159. chrome = resolve_chrome_path(os.environ)
  160. with socket.create_connection((proxy.ip, proxy.port), timeout=3):
  161. pass # Check before allocating a pool account. Not an internet test.
  162. return executable, chrome
  163. @contextmanager
  164. def private_redis(executable):
  165. # /tmp keeps the Unix socket below macOS's path-length limit. Only this
  166. # owned directory/process is removed; no FLUSHDB, shared service or port.
  167. with tempfile.TemporaryDirectory(prefix="ita-qb-", dir="/tmp") as directory:
  168. unix_socket = str(Path(directory) / "redis.sock")
  169. process = subprocess.Popen([
  170. executable, "--port", "0", "--unixsocket", unix_socket,
  171. "--unixsocketperm", "700", "--save", "", "--appendonly", "no",
  172. "--dir", directory, "--daemonize", "no",
  173. ], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
  174. config = {"unix_socket_path": unix_socket, "db": 0, "decode_responses": True,
  175. "socket_timeout": 2, "socket_connect_timeout": 2}
  176. client = redis.Redis(**config)
  177. try:
  178. deadline = time.monotonic() + 8
  179. while True:
  180. if process.poll() is not None:
  181. raise LocalAcceptanceError("private_redis_exited")
  182. try:
  183. if client.ping():
  184. break
  185. except redis.exceptions.RedisError:
  186. pass
  187. if time.monotonic() >= deadline:
  188. raise LocalAcceptanceError("private_redis_not_ready")
  189. time.sleep(0.1)
  190. yield config
  191. finally:
  192. try:
  193. client.close()
  194. finally:
  195. if process.poll() is None:
  196. process.terminate()
  197. try:
  198. process.wait(timeout=5)
  199. except subprocess.TimeoutExpired:
  200. process.kill()
  201. process.wait(timeout=5)
  202. class LocalCloudBoundary:
  203. """Only cloud order/telemetry are local. Pool selection is never reimplemented."""
  204. def __init__(self, api, group, route, proxy, applicant, preferences):
  205. self.api, self.group, self.route, self.proxy = api, group, route, proxy
  206. self.applicant = applicant
  207. self.order = {"id": -1, "order_id": "LOCAL-ACCEPTANCE", "status": "processing",
  208. "user_inputs": {**preferences, "username": applicant.username, "password": applicant.password}}
  209. self.pool_calls = self.order_calls = self.forbidden_calls = 0
  210. self.query_account = None
  211. self.error = None
  212. def get_next_account(self, pool_name, account_cd):
  213. self.pool_calls += 1
  214. if self.pool_calls != 1 or (pool_name, account_cd) != (
  215. self.group.sentinel.account_pool_id, self.group.sentinel.account_cd):
  216. raise LocalAcceptanceError("unexpected_pool_allocation")
  217. try:
  218. raw = self.api.get_next_account(pool_name, account_cd)
  219. if not isinstance(raw, dict) or raw.get("pool_name") != pool_name or raw.get("status") != "active":
  220. raise LocalAcceptanceError("pool_account_invalid")
  221. account = VSAccount.model_validate(raw)
  222. if not account.password:
  223. raise LocalAcceptanceError("pool_website_password_missing")
  224. validate_cloud_otp_environment(self.route.mail, account.username)
  225. if account.username.strip().casefold() == self.applicant.username.strip().casefold():
  226. raise LocalAcceptanceError("query_and_book_accounts_same_choose_other_applicant")
  227. self.query_account = account
  228. emit("POOL", accountId=account.id, pool=pool_name, accountsDistinct=True)
  229. return raw
  230. except Exception as exc:
  231. self.error = str(exc) if isinstance(exc, LocalAcceptanceError) else "pool_allocation_failed"
  232. raise LocalAcceptanceError(self.error) from None
  233. def get_next_proxy(self, *_args):
  234. return self.proxy.model_dump()
  235. def get_vas_task_pop(self, queue_name):
  236. self.order_calls += 1
  237. if queue_name != "auto." + self.route.routing_key or self.order_calls != 1:
  238. raise LocalAcceptanceError("unexpected_local_order_request")
  239. return copy.deepcopy(self.order)
  240. def slot_refresh_start(self, *_args, **_kwargs):
  241. pass
  242. def slot_refresh_success(self, *_args, **_kwargs):
  243. pass
  244. def slot_refresh_fail(self, *_args, **_kwargs):
  245. self.error = self.error or "query_failed"
  246. def slot_snapshot_report(self, *_args, **_kwargs):
  247. pass
  248. def __getattr__(self, name):
  249. # Reject cloud updates/notifications and any new dependency rather
  250. # than silently forwarding a synthetic order id to the backend.
  251. self.forbidden_calls += 1
  252. raise LocalAcceptanceError("unexpected_cloud_operation")
  253. class ObservedPlugin:
  254. """A test observer around a real factory-created plugin, not an algorithm."""
  255. def __init__(self, plugin, role):
  256. self.plugin, self.role = plugin, role
  257. self.audit = None
  258. self.calls = 0
  259. self.result = None
  260. self.error = None
  261. self.cleanup_ok = False
  262. self.last_counts = None
  263. def __getattr__(self, name):
  264. return getattr(self.plugin, name)
  265. def create_session(self):
  266. try:
  267. self.plugin.create_session()
  268. self.audit = CalendarRequestAudit(self.plugin.page, include_time_slots=True)
  269. self.audit.start()
  270. except Exception as exc:
  271. self.error = type(exc).__name__
  272. raise LocalAcceptanceError(self.role + "_session_failed") from None
  273. def query(self, apt):
  274. return self._call("query", apt)
  275. def book(self, slot_info, user_inputs):
  276. return self._call("book", slot_info, user_inputs)
  277. def _call(self, operation, *args):
  278. self.calls += 1
  279. if operation != self.role or self.calls != 1:
  280. raise LocalAcceptanceError("unexpected_plugin_invocation")
  281. try:
  282. self.result = getattr(self.plugin, operation)(*args)
  283. return self.result
  284. except Exception as exc:
  285. self.error = type(exc).__name__
  286. if isinstance(exc, FinalOutcomeUnknown):
  287. raise # Preserve the native Booker's pause-on-unknown branch.
  288. raise LocalAcceptanceError(self.role + "_operation_failed") from None
  289. def cleanup(self):
  290. # The GCO may remove a failed instance before the final audit. Keep
  291. # that failure visible; never turn retained counters into live proof.
  292. try:
  293. if self.audit is not None:
  294. try:
  295. self.last_counts = self.audit.settled_snapshot()
  296. finally:
  297. self.audit.stop()
  298. finally:
  299. self.plugin.cleanup()
  300. self.cleanup_ok = True
  301. def observe_factory(gco, role, stack, observed):
  302. factory_create = gco.m_factory.create
  303. cfg = gco.m_cfg.plugin_config
  304. gco.m_factory.register_plugin(cfg.plugin_name, str(ROOT / "plugins/ita_plugin.py"), "ItaPlugin")
  305. def create(*args):
  306. instance = ObservedPlugin(factory_create(*args), role)
  307. observed[role] = instance
  308. return instance
  309. stack.enter_context(patch.object(gco.m_factory, "create", side_effect=create))
  310. def framework_log(message):
  311. # GCO errors can include arbitrary provider exception strings. Keep only
  312. # lifecycle markers here; plugin observers produce safe failure codes.
  313. for marker in ("Triggering BOOK", "SLOT FOUND", "spawned", "Stopping", "Cleaned up",
  314. "Entering username", "Entering password", "Login form submitted",
  315. "Successfully reached authenticated /UserArea", "OTP sent",
  316. "automatic OTP DOM write verified", "Calendar found the first positive month"):
  317. if marker in message:
  318. emit("FRAMEWORK", marker=marker)
  319. break
  320. def run_framework_once(query_group, book_group, route, cloud, redis_config, *, final_control=None):
  321. observed, threads = {}, []
  322. report = {"contract": FINAL_CONTRACT if final_control else CONTRACT, "passed": False, "outcome": "failed",
  323. "cloudOrders": False, "finalSubmissionEnabled": final_control is not None, "liveAcceptance": True,
  324. "queryCalls": 0, "bookCalls": 0, "signalPublished": False,
  325. "signalReadByBooker": False, "selectionVerified": False, "cleanupVerified": False}
  326. gc_objects = []
  327. local_success_reports = []
  328. if final_control:
  329. report.update(bookingConfirmed=False, bookResultSuccess=False, automaticRetry=False)
  330. with ExitStack() as stack:
  331. def forbidden_cloud_write(*_args, **_kwargs):
  332. cloud.forbidden_calls += 1
  333. raise LocalAcceptanceError("unexpected_cloud_operation")
  334. boundary = SimpleNamespace(Instance=lambda: cloud, lock_account=forbidden_cloud_write)
  335. # Existing tests use these same dependency seams. Do not patch GCO
  336. # implementation methods, signal serialization, filtering or TTL.
  337. for cls in (SentinelGCO, OrderBookerGCO):
  338. stack.enter_context(patch.dict(cls.__init__.__globals__, {"VSCloudApi": boundary}))
  339. production_submit = ita_workflow.submit_final
  340. stack.enter_context(patch.object(ita_workflow, "submit_final", side_effect=(
  341. lambda plugin, actual_route: final_control.submit(plugin, actual_route, observed, production_submit)
  342. ) if final_control else LocalAcceptanceError("final_submission_forbidden")))
  343. sentinel = SentinelGCO(query_group, redis_config, framework_log)
  344. booker = OrderBookerGCO(book_group, redis_config, framework_log)
  345. # Block reporting BEFORE the shared asynchronous executor can enqueue
  346. # anything. Its callback could otherwise outlive this patch scope.
  347. def capture_success(task_id, order_id, result):
  348. if (final_control is None or task_id != -1 or order_id != cloud.order["order_id"]
  349. or result is not observed["book"].result or result.success is not True
  350. or local_success_reports):
  351. return forbidden_cloud_write()
  352. local_success_reports.append(result) # Standard callback; no cloud enqueue.
  353. stack.enter_context(patch.object(booker, "_push_success_notification", side_effect=capture_success))
  354. if final_control:
  355. def local_pause(task_id, data):
  356. if str(task_id) != "-1" or data != {"status": "pause"}:
  357. return forbidden_cloud_write()
  358. cloud.order["status"] = "pause"
  359. # Restore only this test seam, not the counters/state accumulated
  360. # during the run (patch.dict(__dict__) would restore those too).
  361. cloud.update_vas_task = local_pause
  362. stack.callback(cloud.__dict__.pop, "update_vas_task", None)
  363. gc_objects.extend((sentinel, booker))
  364. observe_factory(sentinel, "query", stack, observed)
  365. observe_factory(booker, "book", stack, observed)
  366. signal_key = sentinel._get_redis_key(route.routing_key)
  367. published_digest = None
  368. original_setex, original_get = sentinel.redis_client.setex, booker.redis_client.get
  369. def publish(key, ttl, payload):
  370. nonlocal published_digest
  371. ok = original_setex(key, ttl, payload)
  372. if key == signal_key and ok:
  373. published_digest = hashlib.sha256(payload.encode()).hexdigest()
  374. report["signalPublished"] = True
  375. emit("SIGNAL", published=True, ttl=ttl)
  376. return ok
  377. def read(key):
  378. payload = original_get(key)
  379. if key == signal_key and payload and published_digest:
  380. data = payload.encode() if isinstance(payload, str) else payload
  381. report["signalReadByBooker"] = hashlib.sha256(data).hexdigest() == published_digest
  382. return payload
  383. stack.enter_context(patch.object(sentinel.redis_client, "setex", side_effect=publish))
  384. stack.enter_context(patch.object(booker.redis_client, "get", side_effect=read))
  385. thread_class = threading.Thread
  386. def tracked_thread(*args, **kwargs):
  387. # Original trigger still starts and joins its own thread. Track
  388. # ownership for Ctrl+C cleanup; don't create a second job runner.
  389. finished = threading.Event()
  390. target = kwargs.pop("target")
  391. def observed_target(*target_args, **target_kwargs):
  392. try:
  393. return target(*target_args, **target_kwargs)
  394. finally:
  395. finished.set()
  396. thread = thread_class(*args, target=observed_target, **kwargs, daemon=True)
  397. threads.append((thread, finished))
  398. return thread
  399. stack.enter_context(patch.dict(booker._booking_trigger_loop.__globals__, {
  400. "threading": SimpleNamespace(Thread=tracked_thread, RLock=threading.RLock, Event=threading.Event)}))
  401. try:
  402. emit("STAGE", stage="applicant_login", orderSource="local_only")
  403. booker._spawn_worker(route.routing_key)
  404. if len(booker.m_tasks) != 1:
  405. raise LocalAcceptanceError("applicant_session_not_created")
  406. emit("STAGE", stage="pool_login")
  407. sentinel._spawn_sentinel_worker()
  408. if len(sentinel.m_tasks) != 1:
  409. raise LocalAcceptanceError(cloud.error or "query_session_not_created")
  410. if observed["query"].page is observed["book"].page:
  411. raise LocalAcceptanceError("browser_sessions_not_separate")
  412. emit("STAGE", stage="query_and_publish")
  413. # Exactly one iteration of the existing loops, as in the offline
  414. # fixture. No independent queue, TTL, retry or polling algorithm.
  415. with patch.object(sentinel.m_stop_event, "is_set", side_effect=[False, True]):
  416. sentinel._monitor_loop()
  417. query = observed["query"]
  418. if query.error or query.result is None or cloud.error:
  419. raise LocalAcceptanceError("query_did_not_complete")
  420. if query.result.success and (query.result.availability_status != AvailabilityStatus.Available
  421. or query.result.earliest_date is None):
  422. raise LocalAcceptanceError("query_did_not_return_date_availability")
  423. report["earliestDate"] = query.result.earliest_date.strftime("%Y-%m-%d") if query.result.earliest_date else None
  424. if not query.result.success:
  425. if query.result.availability_status != AvailabilityStatus.NoneAvailable:
  426. raise LocalAcceptanceError("unexpected_query_result")
  427. report["outcome"] = "no_availability"
  428. elif not report["signalPublished"]:
  429. raise LocalAcceptanceError("redis_publication_failed")
  430. else:
  431. emit("STAGE", stage="booker_receive_and_select")
  432. with patch.object(booker.m_stop_event, "is_set", side_effect=[False, True]):
  433. booker._booking_trigger_loop()
  434. book = observed["book"]
  435. if book.error:
  436. raise LocalAcceptanceError("book_failed")
  437. if book.calls == 0:
  438. report["outcome"] = "not_triggered_expired_or_date_filtered"
  439. elif (book.result is not None and book.result.success is False
  440. and book.result.label == BOOKING_CALENDAR_PREFINAL_STOP_LABEL):
  441. approval = book.last_booking_precommit
  442. verify_precommit_selection(book.page, runtime_service=read_calendar_service(book.page), approval=approval)
  443. report.update(selectionVerified=True, selectedDate=approval.candidate.day.isoformat(),
  444. selectedTime=approval.candidate.time_label, outcome="pre_final")
  445. elif book.result is not None and book.result.success:
  446. if final_control is None:
  447. raise LocalAcceptanceError("unexpected_book_success_before_final")
  448. attempt = getattr(book.plugin, "_production_final_attempt", None)
  449. if (not final_control.possibly_submitted or attempt is None or attempt.state != "confirmed"
  450. or book.plugin._production_book_result is not book.result
  451. or len(local_success_reports) != 1):
  452. raise LocalAcceptanceError("final_success_not_verified")
  453. report.update(**final_control.selection, outcome="confirmed", bookingConfirmed=True,
  454. bookResultSuccess=True, confirmedDate=book.result.book_date,
  455. confirmedTime=book.result.book_time, successHandledLocally=True)
  456. elif (book.result is not None and book.result.success is False
  457. and book.result.label == BOOKING_RECHECK_NONE_AVAILABLE_LABEL):
  458. report["outcome"] = "book_no_selection"
  459. elif final_control and final_control.possibly_submitted and book.result is not None and book.result.success is False:
  460. report["outcome"] = "rejected_do_not_retry"
  461. else:
  462. raise LocalAcceptanceError("unexpected_book_result")
  463. for role, item in observed.items():
  464. # The owner snapshots before stopping its audit. Browser
  465. # teardown may then fail; that does not invalidate these
  466. # connected counters or justify reading a stopped audit.
  467. counts = item.last_counts if item.last_counts is not None else item.audit.settled_snapshot()
  468. report[role + "Requests"] = counts
  469. expected_final = 1 if role == "book" and final_control and final_control.possibly_submitted else 0
  470. if counts["finalRequests"] != expected_final:
  471. raise LocalAcceptanceError("unexpected_final_request_do_not_rerun")
  472. if counts["otpRequests"] > 1 or counts["avantiPosts"] > 1:
  473. raise LocalAcceptanceError("unexpected_repeated_preparation")
  474. if cloud.forbidden_calls:
  475. raise LocalAcceptanceError("unexpected_cloud_operation")
  476. report["passed"] = (report["outcome"] == ("confirmed" if final_control else "pre_final") and report["signalPublished"]
  477. and report["signalReadByBooker"] and cloud.pool_calls == cloud.order_calls == 1)
  478. except KeyboardInterrupt:
  479. report.update(outcome="interrupted", passed=False)
  480. except Exception as exc:
  481. report.update(passed=False, outcome="failed", errorType=type(exc).__name__,
  482. reason=str(exc) if isinstance(exc, LocalAcceptanceError) else "local_acceptance_failed")
  483. finally:
  484. for role, item in observed.items():
  485. report[role + "Calls"] = item.calls
  486. if item.error:
  487. report[role + "ErrorType"] = item.error
  488. # Stop real GCOs and their owned pages before disposing Redis.
  489. for gco in gc_objects:
  490. gco.stop()
  491. for item in observed.values():
  492. if not item.cleanup_ok:
  493. try:
  494. item.cleanup()
  495. except Exception:
  496. report["passed"] = False
  497. for thread, finished in threads:
  498. if thread.ident is not None:
  499. # Keep cloud/final guards installed until the original
  500. # callback really exits, even if Ctrl+C interrupted join.
  501. # Outstanding SDK I/O follows its existing timeout.
  502. if not finished.is_set():
  503. emit("STAGE", stage="waiting_for_inflight_call_cleanup_no_retry")
  504. while not finished.is_set():
  505. try:
  506. finished.wait(timeout=0.5)
  507. except KeyboardInterrupt:
  508. report["passed"] = False
  509. thread.join(timeout=1)
  510. report["cleanupVerified"] = (all(item.cleanup_ok for item in observed.values())
  511. and not any(thread.is_alive() for thread, _ in threads))
  512. report["passed"] = report["passed"] and report["cleanupVerified"]
  513. if final_control:
  514. # Preserve confirmed business outcome even if cleanup failed;
  515. # never suggest a new booking to repair a diagnostic failure.
  516. report.update(final_control.selection)
  517. report.update(possiblySubmitted=final_control.possibly_submitted, automaticRetry=False)
  518. if final_control.failure_reason:
  519. report["finalReason"] = final_control.failure_reason
  520. if final_control.possibly_submitted and report["outcome"] not in {"confirmed", "rejected_do_not_retry"}:
  521. report.update(outcome="unknown_do_not_retry", passed=False)
  522. for role, item in observed.items():
  523. if item.last_counts is not None:
  524. report[role + "Requests"] = item.last_counts
  525. for gco in gc_objects:
  526. gco.redis_client._client.close()
  527. return report
  528. def main_local(args):
  529. stage = "preflight"
  530. try:
  531. group, route, proxy, preferences = load_settings(args)
  532. submit_once = getattr(args, "submit_once", False)
  533. if submit_once and not getattr(args, "local_run", False):
  534. raise LocalAcceptanceError("real_submission_requires_local_run")
  535. executable, chrome = check_dependencies(proxy)
  536. emit("PREFLIGHT", contract=FINAL_CONTRACT if submit_once else CONTRACT, ready=True, pool=group.sentinel.account_pool_id,
  537. routingKey=route.routing_key, cloudOrders=False, finalSubmissionEnabled=submit_once)
  538. if args.local_preflight:
  539. return 0
  540. stage = "applicant_credentials"
  541. if args.book_account_file:
  542. applicant = load_local_account(args.book_account_file)
  543. else:
  544. print("Book applicant account (not the Query pool account); OTP mailbox must already be authorized.", flush=True)
  545. username, password = _get_credentials({"ITA_USERNAME": os.environ.get("ITA_BOOK_USERNAME", ""),
  546. "ITA_PASSWORD": os.environ.get("ITA_BOOK_PASSWORD", "")}, non_interactive=False)
  547. applicant = VSAccount(username=username, password=password)
  548. if not applicant.password:
  549. raise LocalAcceptanceError("applicant_website_password_missing")
  550. validate_cloud_otp_environment(route.mail, applicant.username)
  551. final_control = LocalFinalSubmission(applicant, route) if submit_once else None
  552. groups = build_groups(group, uuid4().hex[:10], submit_once=submit_once)
  553. cloud = LocalCloudBoundary(VSCloudApi.Instance(), group, route, proxy, applicant, preferences)
  554. stage = "local_redis_and_framework"
  555. with private_redis(executable) as redis_config, patch.dict(os.environ, {"CHROME_BIN": str(chrome)}):
  556. report = run_framework_once(*groups, route, cloud, redis_config, final_control=final_control)
  557. emit("LOCAL_RESULT", **report)
  558. if report.get("possiblySubmitted") and not report["passed"]:
  559. return 4 # Receipt review, NEVER rerun to diagnose a possible write.
  560. return 0 if report["passed"] else (2 if report["outcome"] in {
  561. "no_availability", "book_no_selection", "not_triggered_expired_or_date_filtered"} else 1)
  562. except KeyboardInterrupt:
  563. emit("LOCAL_FAILED", stage=stage, reason="interrupted")
  564. return 130
  565. except Exception as exc:
  566. emit("LOCAL_FAILED", stage=stage, errorType=type(exc).__name__,
  567. reason=str(exc) if isinstance(exc, LocalAcceptanceError) else "check_local_configuration_or_dependency")
  568. return 1