ita_query_book_local.py 34 KB

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