File size: 35,062 Bytes
de745bf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6c516b6
de745bf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
import asyncio
import re
import time
import uuid
from datetime import datetime, timezone
from typing import Any, Optional
import structlog

from ..models import (
    ValidationResult,
    ValidationStatus,
    ScoringResult,
    CatchAllResult,
    DNSResult,
    DisposableResult,
    SMTPResult,
    SyntaxResult,
)
from ..config import settings
from .syntax import validate_syntax
from .dns_check import check_dns
from .disposable import check_disposable, domain_in_blocklist, domain_is_known
from .mailbox_provider import MailboxProvider, resolve_provider
from .i18n import EAI_REASON, eai_gate
from ..ml.risk_scorer import AIRiskScorer, ai_scorer, label_for_score, score_many_by_domain
from ..ml.runtime import get_model_async, predict_async
from .mx_intel import mx_is_trusted
from .retry_queue import is_greylist_result
from .scoring import calculate_score

logger = structlog.get_logger()


def _mask_email_for_log(email: str) -> str:
    local_part, separator, domain = email.rpartition("@")
    if not separator or not domain:
        return "[invalid-email]"
    return f"{local_part[:1]}***@{domain}"


def _build_checks_run(
    syntax: SyntaxResult | None,
    dns: DNSResult | None,
    disposable: DisposableResult | None,
    catch_all: CatchAllResult | None,
    smtp: SMTPResult | None,
    scoring: ScoringResult | None,
) -> list[str]:
    """Which of the 7-layer taxonomy keys actually executed for this result.

    Mirrors the frontend fallback in src/lib/verifier.ts classify() so a layer
    the engine did not run is never reported as a pass. DNS without an error
    claims both `dns` and `mx`; a DNS error claims only `dns`.
    """
    checks: list[str] = []
    if syntax is not None:
        checks.append("syntax")
    if dns is not None:
        if dns.error:
            checks.append("dns")
        else:
            checks.extend(["dns", "mx"])
    if disposable is not None:
        checks.append("disposable")
    if catch_all is not None:
        checks.append("catch_all")
    if smtp is not None:
        checks.append("smtp")
    if scoring is not None:
        checks.append("scoring")
    return checks


def _build_reason_code(
    dns: DNSResult | None,
    disposable: DisposableResult | None,
    catch_all: CatchAllResult | None,
    smtp: SMTPResult | None,
    *,
    early: str | None = None,
) -> str | None:
    """Machine cause for the verdict. Ordered most-specific/actionable first.

    disposable outranks everything (frontend classifies it as Disposable). A
    definitive SMTP rejection (verified False + code) outranks the domain-level
    catch-all flag: reporting "catch_all_detected" beside status=invalid is
    incoherent — the explicit "mailbox does not exist" is the actionable fact.
    Catch-all then outranks a merely inconclusive probe.
    """
    if early:
        return early
    if disposable is not None and disposable.is_disposable:
        return "disposable"
    if dns is not None and dns.a_fallback and not dns.has_mx_records:
        return "a_fallback_only"
    if smtp is not None and smtp.verified is False and smtp.smtp_code:
        if smtp.smtp_code >= 500:
            return "rcpt_5xx"
        if smtp.smtp_code >= 400:
            return "rcpt_4xx"
        if smtp.smtp_code >= 200:
            return "rcpt_2xx"
    if catch_all is not None and catch_all.is_catch_all:
        return "catch_all_detected"
    if smtp is not None and smtp.verified is None and smtp.error:
        return (
            "smtp_timeout"
            if re.search(r"timed?\s*out", smtp.error, re.IGNORECASE)
            else "smtp_unavailable"
        )
    return None


def _deterministic_verdict(
    disposable: DisposableResult | None,
    catch_all: CatchAllResult | None,
    smtp: SMTPResult | None,
) -> str:
    """Pre-AI verdict of the deterministic gauntlet (spec Phase 5).

    Order matters: an explicit mailbox rejection outranks the domain-level
    catch-all flag (mirrors _build_reason_code), and a catch-all NEVER reads
    "valid" even when the probe was accepted — it only proves the domain
    accepts everything.
    """
    if disposable is not None and disposable.is_disposable:
        return "disposable"
    if (
        smtp is not None
        and smtp.verified is False
        and smtp.smtp_code is not None
        and smtp.smtp_code >= 500
    ):
        return "invalid"
    if catch_all is not None and catch_all.is_catch_all:
        return "catch_all"
    if smtp is not None and smtp.verified is True:
        return "valid"
    return "unknown"


def _smtp_class(smtp: SMTPResult | None) -> str:
    """Coarse SMTP response class for the AI feature string (no PII)."""
    if smtp is None:
        return "skipped"
    if smtp.verified is True:
        return "accept"
    if smtp.verified is False:
        if smtp.smtp_code and smtp.smtp_code >= 500:
            return "5xx"
        if smtp.smtp_code and smtp.smtp_code >= 400:
            return "4xx"
        return "reject"
    err = (smtp.error or "").lower()
    if "worker" in err or "unreachable" in err:
        return "transport_error"
    return "unavailable"


def _ambiguous(verdict: str) -> bool:
    """Only these two reach the AI scorer (spec gate steps 6 and 8)."""
    return verdict in {"catch_all", "unknown"}


class EmailValidationEngine:
    """
    Master 7-Layer Email Validation Orchestrator.

    Layers:
      1. Syntax / RFC 5321 check
      2. DNS domain existence
      3. MX record verification
      4. Disposable / Role-based / Free provider detection
      5. Catch-all domain detection
      6. SMTP mailbox verification
      7. Deterministic heuristic deliverability scoring
    """

    def __init__(
        self,
        mailbox: MailboxProvider | None = None,
        cache=None,
        retry_queue=None,
        scorer: AIRiskScorer | None = None,
    ):
        self.use_proxies = settings.use_proxies
        # Layers 5 (catch-all) and 6 (SMTP) run through the autonomous mailbox
        # layer: the built-in SMTP engine on this host, or the remote SMTP
        # probe worker when SMTP_WORKER_URL is configured (spec §3.3).
        self.mailbox = mailbox if mailbox is not None else resolve_provider()
        # DNS/MX and definitive mailbox verdicts are cached across runs — the
        # same domain (and often the same address) recurs constantly in bulk
        # lists. Transient failures are never cached; see validation_cache.py.
        if cache is None:
            from .validation_cache import validation_cache as cache

        self.cache = cache
        # Greylisted (4xx) addresses get a bounded second chance. Without this a
        # valid mailbox on a greylisting domain stays permanently "risky".
        if retry_queue is None:
            from .retry_queue import retry_queue as retry_queue

        self.retry_queue = retry_queue
        # AI risk scorer (spec §3.4): pattern-based classification for the
        # ambiguous rows only; circuit breaker + heuristic fallback inside.
        self.scorer = scorer if scorer is not None else ai_scorer

    async def validate(self, email: str, deep: bool = True) -> ValidationResult:
        start_time = time.monotonic()
        run_id = uuid.uuid4().hex[:12]

        log = logger.bind(email=_mask_email_for_log(email), run_id=run_id)
        log.info("validation_started")

        # ---- Layer 1: Syntax -----------------------------------------
        syntax = validate_syntax(email)

        if not syntax.passed:
            elapsed = (time.monotonic() - start_time) * 1000
            scoring = calculate_score(syntax, None, None, None, None)
            log.warning("syntax_failed", error=syntax.error)
            return ValidationResult(
                email=email,
                status=ValidationStatus.INVALID,
                syntax=syntax,
                scoring=scoring,
                processing_time_ms=round(elapsed, 2),
                validated_at=datetime.now(timezone.utc).isoformat(),
                checks_run=_build_checks_run(syntax, None, None, None, None, scoring),
                reason_code=_build_reason_code(None, None, None, None),
                run_id=run_id,
                validator_version=settings.app_version,
                deterministic_verdict="invalid",
            )

        domain = syntax.domain
        normalized_email = syntax.normalized_email or email

        # ---- Layers 2 & 3: DNS + MX ---------------------------------
        dns_result = await check_dns(domain, settings.dns_timeout)

        if dns_result.error_code in {"timeout", "resolver_error"}:
            elapsed = (time.monotonic() - start_time) * 1000
            scoring = calculate_score(syntax, dns_result, None, None, None)
            log.warning(
                "dns_inconclusive", domain=domain, error_code=dns_result.error_code
            )
            return ValidationResult(
                email=normalized_email,
                status=self._determine_status(scoring),
                syntax=syntax,
                dns=dns_result,
                scoring=scoring,
                processing_time_ms=round(elapsed, 2),
                validated_at=datetime.now(timezone.utc).isoformat(),
                checks_run=_build_checks_run(syntax, dns_result, None, None, None, scoring),
                reason_code=_build_reason_code(dns_result, None, None, None, early="dns_timeout"),
                run_id=run_id,
                validator_version=settings.app_version,
                deterministic_verdict="unknown",
            )

        if (
            not dns_result.domain_exists
            or dns_result.error_code == "nxdomain"
            or (not dns_result.has_mx_records and not dns_result.a_fallback)
        ):
            elapsed = (time.monotonic() - start_time) * 1000
            scoring = calculate_score(syntax, dns_result, None, None, None)
            log.warning("dns_failed", domain=domain)
            return ValidationResult(
                email=normalized_email,
                status=ValidationStatus.INVALID,
                syntax=syntax,
                dns=dns_result,
                scoring=scoring,
                processing_time_ms=round(elapsed, 2),
                validated_at=datetime.now(timezone.utc).isoformat(),
                checks_run=_build_checks_run(syntax, dns_result, None, None, None, scoring),
                reason_code=_build_reason_code(
                    dns_result, None, None, None, early="nxdomain"
                ),
                run_id=run_id,
                validator_version=settings.app_version,
                deterministic_verdict="invalid",
            )

        # ---- Layer 4: Disposable Detection ---------------------------
        disposable = check_disposable(normalized_email, domain)

        # ML advisory (single path): unknown domains only, fail-open. Mirrors
        # the batch pipeline so both surfaces agree on the same address.
        # Skipped when the MX runs on a trusted enterprise platform: the model
        # learned 'unfamiliar domain string = disposable' from an imbalanced
        # dataset and flags legitimate small businesses on Google Workspace /
        # M365 — the deterministic MX intelligence is the better signal there.
        ml_model = await get_model_async()
        mx_host = (
            dns_result.mx_records[0]["host"]
            if dns_result.mx_records
            else None
        )
        if (
            ml_model is not None
            and not domain_is_known(domain)
            and deep
            and not mx_is_trusted(mx_host)
        ):
            # Off the event loop: CPU inference inline would stall every other
            # concurrent request (measured 42.6s cold start before this).
            preds = await predict_async(ml_model, [f"john.smith [SEP] {domain}"])
            if preds:
                disposable = disposable.model_copy(
                    update={"ml_label": preds[0][0], "ml_confidence": preds[0][1]}
                )

        if not deep:
            # Fast path: skip SMTP for quick validation
            elapsed = (time.monotonic() - start_time) * 1000
            scoring = calculate_score(
                syntax,
                dns_result,
                disposable,
                None,
                None,
                mailbox_verification_skipped=True,
            )
            status = self._determine_status(scoring)
            return ValidationResult(
                email=normalized_email,
                status=status,
                syntax=syntax,
                dns=dns_result,
                disposable=disposable,
                scoring=scoring,
                processing_time_ms=round(elapsed, 2),
                validated_at=datetime.now(timezone.utc).isoformat(),
                checks_run=_build_checks_run(
                    syntax, dns_result, disposable, None, None, scoring
                ),
                reason_code=_build_reason_code(dns_result, disposable, None, None),
                run_id=run_id,
                validator_version=settings.app_version,
                # Fast path: the mailbox question was never asked, so there is
                # no deterministic mailbox verdict to report.
                deterministic_verdict=None,
            )

        # ---- Layers 5 & 6: Catch-All + SMTP --------------------------
        mx_host = dns_result.mx_records[0]["host"]

        # EAI/SMTPUTF8 gate (spec §7.2): a non-ASCII local part can only be
        # delivered when the receiving MX advertises SMTPUTF8. Without an SMTP
        # dialogue the engine refuses the per-address probe deterministically
        # instead of faking a verdict. The domain-level catch-all probe (a
        # random ASCII local part) still runs — it is independent of the
        # address's own local part.
        eai_reason = eai_gate(syntax.local_part or "")

        catch_all_task = asyncio.create_task(
            self.mailbox.catch_all(domain, mx_host)
        )
        if eai_reason:
            smtp_result = SMTPResult(
                verified=None,
                error=(
                    f"{EAI_REASON}: mailbox probe requires SMTPUTF8 support "
                    "(deterministic refusal)"
                ),
            )
            try:
                catch_all_raw = await catch_all_task
            except Exception as exc:
                # Same contract as the gather path: a failed probe is UNKNOWN.
                catch_all_raw = exc
        else:
            smtp_task = asyncio.create_task(
                self.mailbox.verify(normalized_email, mx_host)
            )
            catch_all_raw, smtp_result = await asyncio.gather(
                catch_all_task, smtp_task, return_exceptions=True
            )

        # Handle catch-all result. An exception means the probe could not run
        # (e.g. port-25 egress blocked) — that is UNKNOWN, not "not catch-all".
        if isinstance(catch_all_raw, Exception):
            catch_all = CatchAllResult(
                is_catch_all=None, confidence=0.0, error=str(catch_all_raw)
            )
        else:
            is_catch_all, confidence = catch_all_raw
            catch_all = CatchAllResult(is_catch_all=is_catch_all, confidence=confidence)

        # Handle SMTP result
        if isinstance(smtp_result, Exception):
            smtp_result = SMTPResult(verified=None, error=str(smtp_result))

        # ---- Layer 7: Scoring ----------------------------------------
        scoring = calculate_score(
            syntax, dns_result, disposable, catch_all, smtp_result
        )

        status = self._determine_status(scoring)
        elapsed = (time.monotonic() - start_time) * 1000

        # ---- Normalized verdict (spec Phase 5): deterministic first, then AI --
        deterministic = _deterministic_verdict(disposable, catch_all, smtp_result)
        ai_score: float | None = None
        ai_label: str | None = None
        fallback_used = False
        if deep and _ambiguous(deterministic):
            features = AIRiskScorer.build_features(
                domain=domain,
                mx_host=mx_host,
                smtp_class=_smtp_class(smtp_result),
                catch_all=catch_all.is_catch_all if catch_all else None,
                local_part=syntax.local_part or "",
                dns_inconsistent=dns_result.dns_inconsistent,
            )
            scored = await self.scorer.score(features)
            ai_score, ai_label, fallback_used = (
                scored.score,
                scored.label,
                scored.fallback_used,
            )
            if scored.label == "high_risk":
                scoring.warnings.append("ai_high_risk")
        if dns_result.dns_inconsistent:
            scoring.warnings.append("dns_inconsistent: resolvers disagree on MX")

        reason = _build_reason_code(dns_result, disposable, catch_all, smtp_result)
        if eai_reason:
            reason = EAI_REASON

        log.info(
            "validation_complete",
            status=status,
            score=scoring.score,
            time_ms=round(elapsed, 2),
        )

        return ValidationResult(
            email=normalized_email,
            status=status,
            syntax=syntax,
            dns=dns_result,
            disposable=disposable,
            catch_all=catch_all,
            smtp=smtp_result,
            scoring=scoring,
            processing_time_ms=round(elapsed, 2),
            validated_at=datetime.now(timezone.utc).isoformat(),
            checks_run=_build_checks_run(
                syntax, dns_result, disposable, catch_all, smtp_result, scoring
            ),
            reason_code=reason,
            run_id=run_id,
            validator_version=settings.app_version,
            deterministic_verdict=deterministic,
            ai_risk_score=ai_score,
            ai_label=ai_label,
            fallback_used=fallback_used,
        )

    async def validate_batch(
        self, emails: list[str], deep: bool = True
    ) -> list[ValidationResult]:
        """Validate many addresses in one pass, reusing per-domain work.

        Bulk throughput comes from doing DNS/MX **once per domain** and handing
        every (email, mx_host) pair to the mailbox provider in a **single** call,
        so the SMTP engine can reuse one session per receiving host instead of a
        fresh TCP handshake per address. Providers that cannot batch fall back to
        the per-address path, so behaviour is never worse than before.

        Results are returned in the same order as `emails`.
        """
        if not emails:
            return []

        run_id = uuid.uuid4().hex[:12]
        out: list[ValidationResult | None] = [None] * len(emails)

        def _stamp(**kw) -> ValidationResult:
            return ValidationResult(
                processing_time_ms=0.0,
                validated_at=datetime.now(timezone.utc).isoformat(),
                run_id=run_id,
                validator_version=settings.app_version,
                **kw,
            )

        # ---- Layer 1: syntax (per address) ---------------------------
        parsed: list[tuple[str, SyntaxResult]] = []
        domain_index: dict[str, list[int]] = {}
        for idx, raw in enumerate(emails):
            syntax = validate_syntax(raw)
            parsed.append((raw, syntax))
            if not syntax.passed:
                scoring = calculate_score(syntax, None, None, None, None)
                out[idx] = _stamp(
                    email=raw,
                    status=ValidationStatus.INVALID,
                    syntax=syntax,
                    scoring=scoring,
                    checks_run=_build_checks_run(syntax, None, None, None, None, scoring),
                    reason_code=_build_reason_code(None, None, None, None),
                )
                continue
            domain_index.setdefault(syntax.domain, []).append(idx)

        # ---- Layers 2 & 3: DNS + MX once per domain (cached) ---------
        domains = list(domain_index.keys())
        domain_dns: dict[str, DNSResult] = {}
        misses: list[str] = []
        for domain in domains:
            cached_dns = await self.cache.get_dns(domain)
            if cached_dns is not None:
                domain_dns[domain] = cached_dns
            else:
                misses.append(domain)

        if misses:
            resolved = await asyncio.gather(
                *[check_dns(d, settings.dns_timeout) for d in misses],
                return_exceptions=True,
            )
            for domain, dns in zip(misses, resolved):
                result = (
                    dns
                    if isinstance(dns, DNSResult)
                    else DNSResult(
                        domain_exists=False,
                        has_mx_records=False,
                        error=str(dns),
                        error_code="resolver_error",
                    )
                )
                domain_dns[domain] = result
                # put_dns ignores transient failures internally.
                await self.cache.put_dns(domain, result)

        pending: list[tuple[int, str, SyntaxResult, str, DNSResult]] = []
        for domain, idxs in domain_index.items():
            dns = domain_dns[domain]
            inconclusive = dns.error_code in {"timeout", "resolver_error"}
            dead = (
                not dns.domain_exists
                or dns.error_code == "nxdomain"
                or (not dns.has_mx_records and not dns.a_fallback)
            )
            for idx in idxs:
                raw, syntax = parsed[idx]
                normalized = syntax.normalized_email or raw
                if inconclusive:
                    scoring = calculate_score(syntax, dns, None, None, None)
                    out[idx] = _stamp(
                        email=normalized,
                        status=self._determine_status(scoring),
                        syntax=syntax,
                        dns=dns,
                        scoring=scoring,
                        checks_run=_build_checks_run(
                            syntax, dns, None, None, None, scoring
                        ),
                        reason_code=_build_reason_code(
                            dns, None, None, None, early="dns_timeout"
                        ),
                        deterministic_verdict="unknown",
                    )
                elif dead:
                    scoring = calculate_score(syntax, dns, None, None, None)
                    out[idx] = _stamp(
                        email=normalized,
                        status=ValidationStatus.INVALID,
                        syntax=syntax,
                        dns=dns,
                        scoring=scoring,
                        checks_run=_build_checks_run(
                            syntax, dns, None, None, None, scoring
                        ),
                        reason_code=_build_reason_code(
                            dns, None, None, None, early="nxdomain"
                        ),
                        deterministic_verdict="invalid",
                    )
                else:
                    pending.append((idx, normalized, syntax, domain, dns))

        # ---- Layer 4 fast path (no mailbox probe) --------------------
        if pending and not deep:
            for idx, normalized, syntax, domain, dns in pending:
                disposable = check_disposable(normalized, domain)
                scoring = calculate_score(
                    syntax, dns, disposable, None, None,
                    mailbox_verification_skipped=True,
                )
                out[idx] = _stamp(
                    email=normalized,
                    status=self._determine_status(scoring),
                    syntax=syntax,
                    dns=dns,
                    disposable=disposable,
                    scoring=scoring,
                    checks_run=_build_checks_run(
                        syntax, dns, disposable, None, None, scoring
                    ),
                    reason_code=_build_reason_code(dns, disposable, None, None),
                    deterministic_verdict=None,
                )
            pending = []

        # ---- Layers 5 & 6: ONE batched mailbox call ------------------
        cached_verdicts: dict[str, tuple[SMTPResult, Optional[bool]]] = {}
        catch_all_map: dict[str, Optional[bool]] = {}
        if pending:
            # Serve anything already answered (definitive verdicts only) so we
            # never re-probe an address we already know the answer to.
            to_probe: list[tuple[int, str, SyntaxResult, str, DNSResult]] = []
            for item in pending:
                _idx, normalized, _syntax, _domain, _dns = item
                hit = await self.cache.get_verdict(normalized, deep)
                if hit is None:
                    to_probe.append(item)
                else:
                    cached_verdicts[normalized] = hit
                    if hit[1] is not None:
                        catch_all_map[item[3]] = hit[1]

            smtp_map: dict[str, SMTPResult] = {}
            pairs: list[tuple[str, str]] = []
            for _idx, normalized, _syntax, _domain, _dns in to_probe:
                if eai_gate(_syntax.local_part or ""):
                    # Deterministic EAI refusal (spec §7.2): the probe requires
                    # an SMTPUTF8 advertisement we cannot observe, so refuse
                    # instead of guessing. Cache/check semantics unchanged.
                    smtp_map[normalized] = SMTPResult(
                        verified=None,
                        error=(
                            f"{EAI_REASON}: mailbox probe requires SMTPUTF8 "
                            "(deterministic refusal)"
                        ),
                    )
                    continue
                if _dns.mx_records:
                    pairs.append((normalized, _dns.mx_records[0]["host"]))
                else:
                    smtp_map[normalized] = SMTPResult(
                        verified=None, error="no MX host available for probe"
                    )
            if pairs:
                try:
                    if hasattr(self.mailbox, "verify_many"):
                        smtp_map, caught = await self.mailbox.verify_many(pairs)
                        for domain, value in (caught or {}).items():
                            catch_all_map.setdefault(domain, value)
                    else:
                        # Provider cannot batch: per-address fallback.
                        for email, mx in pairs:
                            smtp_map[email] = await self.mailbox.verify(email, mx)
                        for domain in {e.rsplit("@", 1)[-1].lower() for e, _ in pairs}:
                            mx = next(
                                m for e, m in pairs
                                if e.rsplit("@", 1)[-1].lower() == domain
                            )
                            value, _conf = await self.mailbox.catch_all(domain, mx)
                            catch_all_map.setdefault(domain, value)
                except Exception as exc:  # one bad stage must not abort the batch
                    smtp_map = {
                        e: SMTPResult(verified=None, error=f"mailbox_stage_error: {exc}")
                        for e, _ in pairs
                    }

            # ---- ML advisory signal (unknown domains, ONE batched call) ----
            # Runs only where the curated lists have no answer - the exact gap
            # a static blocklist cannot close. Any failure here is silently
            # ignored: the deterministic verdict stands.
            # Domains on trusted enterprise MX (Google Workspace / M365 /
            # gateways) are excluded: disposable domains essentially never run
            # there, and the model's 'unfamiliar string' bias would otherwise
            # flag legitimate businesses as disposable.
            ml_map: dict[str, tuple[str, float]] = {}
            ml_model = await get_model_async()
            if ml_model is not None:
                unknown_domains = sorted(
                    {
                        domain
                        for _, _, _, domain, dns in pending
                        if not domain_is_known(domain)
                        and dns.mx_records
                        and not mx_is_trusted(dns.mx_records[0]["host"])
                    }
                )
                if unknown_domains:
                    # One batched call, off the event loop (CPU inference inline
                    # stalls concurrent requests), and fail-open by construction:
                    # predict_async never raises.
                    preds = await predict_async(
                        ml_model, [f"john.smith [SEP] {d}" for d in unknown_domains]
                    )
                    if preds:
                        ml_map = dict(zip(unknown_domains, preds))

            # ---- Layer 7: score each row -----------------------------
            ai_cache: dict[str, Any] = {}  # feature string -> AIScore (dedupe)
            for idx, normalized, syntax, domain, dns in pending:
                cached = cached_verdicts.get(normalized)
                if cached is not None:
                    smtp, ca_cached = cached
                else:
                    smtp = smtp_map.get(normalized) or SMTPResult(
                        verified=None, error="engine returned no result"
                    )
                    ca_cached = None
                    # Persist only definitive answers (verified verdict or a
                    # confirmed catch-all); transient failures are never cached.
                    await self.cache.put_verdict(
                        normalized, deep, smtp, catch_all_map.get(domain)
                    )

                ca_value = (
                    ca_cached if ca_cached is not None else catch_all_map.get(domain)
                )
                catch_all = CatchAllResult(
                    is_catch_all=ca_value,
                    confidence=0.9 if ca_value is not None else 0.0,
                )
                disposable = check_disposable(normalized, domain)
                ml = ml_map.get(domain)
                if ml is not None:
                    disposable = disposable.model_copy(
                        update={"ml_label": ml[0], "ml_confidence": ml[1]}
                    )

                # A 4xx means "come back later", not a verdict. Schedule one
                # bounded retry so a real mailbox on a greylisting domain is not
                # stranded as permanently risky. The queue never raises.
                if cached is None:
                    try:
                        if is_greylist_result(smtp):
                            await self.retry_queue.enqueue(
                                normalized, dns.mx_records[0]["host"], smtp.smtp_code
                            )
                        elif smtp.verified is not None:
                            # Definitive answer — cancel any pending retry.
                            await self.retry_queue.resolve_email(normalized)
                    except Exception:
                        pass

                scoring = calculate_score(
                    syntax, dns, disposable, catch_all, smtp
                )

                # ---- Normalized verdict (spec Phase 5) -------------------
                deterministic = _deterministic_verdict(disposable, catch_all, smtp)
                ai_score: float | None = None
                ai_label: str | None = None
                fallback_used = False
                if deep and _ambiguous(deterministic):
                    feats = AIRiskScorer.build_features(
                        domain=domain,
                        mx_host=dns.mx_records[0]["host"] if dns.mx_records else None,
                        smtp_class=_smtp_class(smtp),
                        catch_all=ca_value,
                        local_part=syntax.local_part or "",
                        dns_inconsistent=dns.dns_inconsistent,
                    )
                    if feats not in ai_cache:
                        ai_cache[feats] = await self.scorer.score(feats)
                    scored = ai_cache[feats]
                    ai_score, ai_label, fallback_used = (
                        scored.score,
                        scored.label,
                        scored.fallback_used,
                    )
                    if scored.label == "high_risk":
                        scoring.warnings.append("ai_high_risk")
                if dns.dns_inconsistent:
                    scoring.warnings.append("dns_inconsistent: resolvers disagree on MX")
                reason = _build_reason_code(dns, disposable, catch_all, smtp)
                if (syntax.local_part or "") and eai_gate(syntax.local_part or ""):
                    reason = EAI_REASON

                out[idx] = _stamp(
                    email=normalized,
                    status=self._determine_status(scoring),
                    syntax=syntax,
                    dns=dns,
                    disposable=disposable,
                    catch_all=catch_all,
                    smtp=smtp,
                    scoring=scoring,
                    checks_run=_build_checks_run(
                        syntax, dns, disposable, catch_all, smtp, scoring
                    ),
                    reason_code=reason,
                    deterministic_verdict=deterministic,
                    ai_risk_score=ai_score,
                    ai_label=ai_label,
                    fallback_used=fallback_used,
                    cache_hit=cached is not None,
                )

        # Guarantee one result per input, in order.
        for idx, result in enumerate(out):
            if result is None:
                raw = emails[idx]
                syntax = parsed[idx][1] if idx < len(parsed) else validate_syntax(raw)
                scoring = calculate_score(syntax, None, None, None, None)
                out[idx] = _stamp(
                    email=raw,
                    status=ValidationStatus.UNKNOWN,
                    syntax=syntax,
                    scoring=scoring,
                    checks_run=_build_checks_run(
                        syntax, None, None, None, None, scoring
                    ),
                    reason_code="engine_missing_result",
                    deterministic_verdict="unknown",
                )
        return [r for r in out if r is not None]

    def _determine_status(self, scoring: ScoringResult) -> ValidationStatus:
        if scoring.score >= 75:
            return ValidationStatus.VALID
        elif scoring.score >= 40:
            return ValidationStatus.RISKY
        else:
            return ValidationStatus.INVALID


# Singleton engine
engine = EmailValidationEngine()