037 — Adaptive Scraping Escalation Policy (registry-driven, no host hardcode)¶
Status: ACCEPTED
Date: 2026-06-18
Extends: 022-cloakbrowser-advanced-bypass.md,
023-adaptive-scraping-escalation.md,
015-ingestion-mode-bypass-strategy.md
Closes: ADR-023 status (PROPOSED → ACCEPTED via this ADR).
Context¶
AdaptiveBypassManager (infrastructure/bypass/adaptive.py) already ships with
TIERS = ['noop', 'curl_stealth', 'stealth_browser', 'cloak'] and an
escalate() method. The wiring is in
infrastructure/sources/career_site_source.py:66-95 (_try_escalate_bypass).
But three problems remain:
- The escalation contract is implicit. "When should I escalate?" lives in
code that catches
Exceptionand increments a counter with no TTL or window. Two near-simultaneous failures look the same as two failures a week apart. - No failure classification. A Cloudflare 403, a DataDome 503, a 5xx
timeout, and a parser returning zero items are all the same exception. The
right response differs: 403 → escalate, 5xx → retry same tier with backoff,
zero items → re-try with
monitor: auto(ADR-025 territory). - Hardcoded
cloaktier dependency. ADR-022 documents CloakBrowser as the ultimate tier; the code unconditionally puts'cloak'in the tier list. Ifcloakbrowseris not installed (no[stealth]extra), the registry import fails atload_extensions()time. There is no graceful skip for dev / CI environments.
ADR-023 listed all three axes (tiered bypass, auto-sniffer, strategy caching) but
stayed PROPOSED and stopped at the design discussion. The implementation
adaptive.py only covers the first axis. The other two belong to other ADRs
(025, 026) and are out of scope here.
Decision¶
- New
FailureSignalProtocol inapplication/contracts.py:class FailureSignal(Protocol): def classify( self, *, status_code: int | None, body: bytes | None, error: BaseException | None ) -> FailureKind: ...FailureKind ∈ {"ok", "rate_limit", "captcha", "blocked", "timeout", "parse_empty", "unknown"}. - Default implementation
HeuristicFailureSignal(ininfrastructure/bypass/failure_signal.py) maps: - 401/403 →
blocked - 429 →
rate_limit - 5xx →
timeout - reCAPTCHA / Cloudflare / DataDome / Kasada strings in body →
captcha - Zero extracted items from a non-empty page →
parse_empty - everything else →
unknown AdaptiveBypassManagergains:_failure_window: dict[SourceId, RingBuffer[(ts, strategy, FailureKind)]]with TTL 1 hour.escalate_if_needed(source_id, signal) -> Strategy | None:- Window of last 30 min, ≥ 3 failures of
kind in {blocked, captcha, timeout}on the current strategy → callescalate(). - 1
captchafailure is enough to escalate (CAPTCHA is binary).
- Window of last 30 min, ≥ 3 failures of
handle_failure(source_id, *, status_code, body, error)— single entry point used bycareer_site_source.pyinstead of bareexcept Exception.- The tier list is built dynamically from registered bypasses at construction:
tiers = [name for name in DEFAULT_TIER_ORDER if resolve_bypass(name) is not None]. Ifcloakbrowseris not installed,cloakis silently absent. No import error.DEFAULT_TIER_ORDERis a const inbypass/__init__.py, not a hardcodedif/elifinadaptive.py. JOB_FTCH_ADAPTIVE_ENABLED(defaulttruein dev,falsein CI) gates the whole manager: when disabled,career_site_sourcealways uses the explicitbypassfrom YAML.- New Prometheus counter
bypass_escalations_total{source, from_tier, to_tier}. career_site_source.py:_try_escalate_bypassis rewritten to callawait self.bypass_strategy.handle_failure(source_id, status_code=..., body=..., error=...)when an extraction failure is observed.
Consequences¶
- (+) Escalation is data-driven: a
captchatriggers an immediate bump;parse_emptydoes not, because re-trying with a heavier browser does not fix an empty page. - (+)
cloakand any other optional tier are drop-in via registry; dev/CI without extras keeps working. - (+) The contract between sources and the bypass layer is explicit:
handle_failureinstead of bareexcept. Documented inBypassStrategyProtocol. - (+) ADR-023 finally gets
ACCEPTED. ADR-022 keeps itsACCEPTEDstatus and is now a concrete instance of the more general policy. - (-) Two new files (
failure_signal.py, plus the contract addition). Test surface forAdaptiveBypassManagergrows. - (-)
career_site_sourceloses the bare-exceptstyle. The diff is large but localised to_try_escalate_bypassand_fetch_with_bypass. - (-) Operators who relied on
cloakalways being last will see it gone from the list when the extra is missing. Mitigated by the dev/CI gate and the startup log that prints the active tier list.