switchback 0.6.2__tar.gz → 0.7.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (54) hide show
  1. {switchback-0.6.2 → switchback-0.7.0}/CHANGELOG.md +20 -0
  2. {switchback-0.6.2 → switchback-0.7.0}/PKG-INFO +2 -1
  3. {switchback-0.6.2 → switchback-0.7.0}/README.md +1 -0
  4. {switchback-0.6.2 → switchback-0.7.0}/pyproject.toml +1 -1
  5. {switchback-0.6.2 → switchback-0.7.0}/switchback/policy/botwall.py +63 -6
  6. {switchback-0.6.2 → switchback-0.7.0}/switchback/tiers/_browser.py +8 -0
  7. {switchback-0.6.2 → switchback-0.7.0}/switchback/tiers/tier_4.py +1 -1
  8. {switchback-0.6.2 → switchback-0.7.0}/switchback/tiers/tier_5.py +1 -1
  9. {switchback-0.6.2 → switchback-0.7.0}/switchback.egg-info/PKG-INFO +2 -1
  10. {switchback-0.6.2 → switchback-0.7.0}/.env.example +0 -0
  11. {switchback-0.6.2 → switchback-0.7.0}/CONTRIBUTING.md +0 -0
  12. {switchback-0.6.2 → switchback-0.7.0}/LICENSE +0 -0
  13. {switchback-0.6.2 → switchback-0.7.0}/MANIFEST.in +0 -0
  14. {switchback-0.6.2 → switchback-0.7.0}/NOTICE +0 -0
  15. {switchback-0.6.2 → switchback-0.7.0}/SECURITY.md +0 -0
  16. {switchback-0.6.2 → switchback-0.7.0}/clients/node_bridge.md +0 -0
  17. {switchback-0.6.2 → switchback-0.7.0}/clients/python_client.py +0 -0
  18. {switchback-0.6.2 → switchback-0.7.0}/config/botwall_skip_urls.txt +0 -0
  19. {switchback-0.6.2 → switchback-0.7.0}/config/extraction.example.json +0 -0
  20. {switchback-0.6.2 → switchback-0.7.0}/setup.cfg +0 -0
  21. {switchback-0.6.2 → switchback-0.7.0}/switchback/__init__.py +0 -0
  22. {switchback-0.6.2 → switchback-0.7.0}/switchback/__main__.py +0 -0
  23. {switchback-0.6.2 → switchback-0.7.0}/switchback/api.py +0 -0
  24. {switchback-0.6.2 → switchback-0.7.0}/switchback/concurrency.py +0 -0
  25. {switchback-0.6.2 → switchback-0.7.0}/switchback/content_cache.py +0 -0
  26. {switchback-0.6.2 → switchback-0.7.0}/switchback/doctor.py +0 -0
  27. {switchback-0.6.2 → switchback-0.7.0}/switchback/egress.py +0 -0
  28. {switchback-0.6.2 → switchback-0.7.0}/switchback/extract.py +0 -0
  29. {switchback-0.6.2 → switchback-0.7.0}/switchback/flags.py +0 -0
  30. {switchback-0.6.2 → switchback-0.7.0}/switchback/highlights.py +0 -0
  31. {switchback-0.6.2 → switchback-0.7.0}/switchback/mcp_server.py +0 -0
  32. {switchback-0.6.2 → switchback-0.7.0}/switchback/normalize.py +0 -0
  33. {switchback-0.6.2 → switchback-0.7.0}/switchback/orchestrator.py +0 -0
  34. {switchback-0.6.2 → switchback-0.7.0}/switchback/policy/__init__.py +0 -0
  35. {switchback-0.6.2 → switchback-0.7.0}/switchback/policy/gates.py +0 -0
  36. {switchback-0.6.2 → switchback-0.7.0}/switchback/py.typed +0 -0
  37. {switchback-0.6.2 → switchback-0.7.0}/switchback/reporting.py +0 -0
  38. {switchback-0.6.2 → switchback-0.7.0}/switchback/search.py +0 -0
  39. {switchback-0.6.2 → switchback-0.7.0}/switchback/server.py +0 -0
  40. {switchback-0.6.2 → switchback-0.7.0}/switchback/session_cache.py +0 -0
  41. {switchback-0.6.2 → switchback-0.7.0}/switchback/session_trace.py +0 -0
  42. {switchback-0.6.2 → switchback-0.7.0}/switchback/state.py +0 -0
  43. {switchback-0.6.2 → switchback-0.7.0}/switchback/tiers/__init__.py +0 -0
  44. {switchback-0.6.2 → switchback-0.7.0}/switchback/tiers/tier_1.py +0 -0
  45. {switchback-0.6.2 → switchback-0.7.0}/switchback/tiers/tier_2.py +0 -0
  46. {switchback-0.6.2 → switchback-0.7.0}/switchback/tiers/tier_3.py +0 -0
  47. {switchback-0.6.2 → switchback-0.7.0}/switchback/tiers/tier_6.py +0 -0
  48. {switchback-0.6.2 → switchback-0.7.0}/switchback/tiers/tier_7.py +0 -0
  49. {switchback-0.6.2 → switchback-0.7.0}/switchback/tracing.py +0 -0
  50. {switchback-0.6.2 → switchback-0.7.0}/switchback.egg-info/SOURCES.txt +0 -0
  51. {switchback-0.6.2 → switchback-0.7.0}/switchback.egg-info/dependency_links.txt +0 -0
  52. {switchback-0.6.2 → switchback-0.7.0}/switchback.egg-info/entry_points.txt +0 -0
  53. {switchback-0.6.2 → switchback-0.7.0}/switchback.egg-info/requires.txt +0 -0
  54. {switchback-0.6.2 → switchback-0.7.0}/switchback.egg-info/top_level.txt +0 -0
@@ -4,6 +4,26 @@ All notable changes to this project are documented here. Format loosely follows
4
4
  [Keep a Changelog](https://keepachangelog.com/); this project uses semantic-ish
5
5
  versioning while pre-1.0.
6
6
 
7
+ ## [0.7.0] - 2026-10-17
8
+
9
+ ### Added
10
+ - **Opt-in log retention: `SCRAPER_BOTWALL_LOG_RETENTION_DAYS`.** The botwall
11
+ audit logs (`botwall_events.jsonl`, `botwall_excluded.jsonl`,
12
+ `botwall_excluded.log`) only ever grew. Set this to N > 0 to keep the last N
13
+ days: the logging path trims them at most once an hour per process, streaming
14
+ through a temp file and `os.replace` that keeps the file's permissions. Lines without a
15
+ readable timestamp are kept, and a failed trim only logs a warning. The
16
+ default 0 keeps everything, as before; `botwall_db.json` is never trimmed.
17
+
18
+ ## [0.6.3] - 2026-10-14
19
+
20
+ ### Fixed
21
+ - **"Page not found" pages returned as articles by the browser tiers.** tier_4
22
+ and tier_5 ignored the HTTP status of the page they loaded, so a site answering
23
+ 404 (seen on bhaskarenglish.in, which serves its 404 page to some bot traffic)
24
+ had its "404 - Page not found" page returned as a success. A 404 or 410 now
25
+ fails the tier, as it already did in tier_2, and the cascade moves on.
26
+
7
27
  ## [0.6.2] - 2026-10-14
8
28
 
9
29
  ### Fixed
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: switchback
3
- Version: 0.6.2
3
+ Version: 0.7.0
4
4
  Summary: One cost-ordered scrape cascade (HTTP → stealth browser → paid), shared by every tool.
5
5
  Author-email: Akash Kodavuru <akash@theaklabs.com>
6
6
  License: MIT
@@ -414,6 +414,7 @@ export OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4317
414
414
  - `SCRAPER_BROWSER_CONCURRENCY` — max simultaneous headless browsers (default 1)
415
415
  - `SCRAPER_BOTWALL_URL_SKIP_COOLDOWN_H` — auto-skip re-test window (24h; 0 = never)
416
416
  - `SCRAPER_BOTWALL_EGRESS_AFTER` — local-tier failures before a host escalates to the residential tier (default 2)
417
+ - `SCRAPER_BOTWALL_LOG_RETENTION_DAYS` — keep only the last N days of `botwall_events.jsonl` / `botwall_excluded.jsonl` / `botwall_excluded.log`, trimmed at most hourly (**0 = keep everything**, the default)
417
418
  - `SCRAPER_SESSION_TTL_S` — cf_clearance reuse window (1800s)
418
419
  - `SCRAPER_DISABLE_SESSION_CACHE` — turn off cf_clearance reuse
419
420
  - `SCRAPER_CONTENT_TTL_S` — URL→result cache TTL (**0 = off**; set e.g. 86400 to skip re-scraping a page within a day)
@@ -349,6 +349,7 @@ export OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4317
349
349
  - `SCRAPER_BROWSER_CONCURRENCY` — max simultaneous headless browsers (default 1)
350
350
  - `SCRAPER_BOTWALL_URL_SKIP_COOLDOWN_H` — auto-skip re-test window (24h; 0 = never)
351
351
  - `SCRAPER_BOTWALL_EGRESS_AFTER` — local-tier failures before a host escalates to the residential tier (default 2)
352
+ - `SCRAPER_BOTWALL_LOG_RETENTION_DAYS` — keep only the last N days of `botwall_events.jsonl` / `botwall_excluded.jsonl` / `botwall_excluded.log`, trimmed at most hourly (**0 = keep everything**, the default)
352
353
  - `SCRAPER_SESSION_TTL_S` — cf_clearance reuse window (1800s)
353
354
  - `SCRAPER_DISABLE_SESSION_CACHE` — turn off cf_clearance reuse
354
355
  - `SCRAPER_CONTENT_TTL_S` — URL→result cache TTL (**0 = off**; set e.g. 86400 to skip re-scraping a page within a day)
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "switchback"
7
- version = "0.6.2"
7
+ version = "0.7.0"
8
8
  description = "One cost-ordered scrape cascade (HTTP → stealth browser → paid), shared by every tool."
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.10"
@@ -24,6 +24,7 @@ import json
24
24
  import logging
25
25
  import os
26
26
  import threading
27
+ import time
27
28
  from datetime import datetime, timezone
28
29
 
29
30
  from .gates import host_of
@@ -69,6 +70,11 @@ SEED_HOSTS = {
69
70
  # When true, each time Firecrawl is invoked for a host it counts as a failure
70
71
  # toward the domain skip threshold (original v1 behaviour). No effect if
71
72
  # SCRAPER_BOTWALL_DOMAIN_SKIP_AFTER is 0.
73
+ #
74
+ # SCRAPER_BOTWALL_LOG_RETENTION_DAYS int ≥0, default 0 (keep everything)
75
+ # When >0, botwall_events.jsonl, botwall_excluded.jsonl and botwall_excluded.log
76
+ # are trimmed to the last N days, at most once an hour per process. Lines
77
+ # without a readable timestamp are kept. botwall_db.json is never trimmed.
72
78
 
73
79
  def _int_env(name: str, default: int) -> int:
74
80
  try:
@@ -92,6 +98,10 @@ URL_SKIP_COOLDOWN_H = _int_env("SCRAPER_BOTWALL_URL_SKIP_COOLDOWN_H", 24)
92
98
  # residential tier. 0 disables escalation.
93
99
  PROMOTE_EGRESS_AFTER = _int_env("SCRAPER_BOTWALL_EGRESS_AFTER", 2)
94
100
 
101
+ LOG_RETENTION_DAYS = _int_env("SCRAPER_BOTWALL_LOG_RETENTION_DAYS", 0)
102
+ _LOG_LOCK = threading.Lock() # appends vs. the trim's rewrite, within this process
103
+ _last_trim = 0.0
104
+
95
105
  # Outcomes a *single URL* is excluded for (deterministic per-URL failures). A 429
96
106
  # is deliberately excluded — it's transient, so it escalates egress but never
97
107
  # permanently skips the URL.
@@ -247,12 +257,57 @@ def _log_event(url, tier, outcome, md_len=None, error=None, latency_ms=None,
247
257
  "outcome": outcome, "md_len": md_len, "latency_ms": latency_ms,
248
258
  "error": error, "status_code": status_code, "challenge": challenge}
249
259
  try:
250
- with open(EVENTS_PATH, "a") as f:
251
- f.write(json.dumps(ev) + "\n")
260
+ with _LOG_LOCK:
261
+ with open(EVENTS_PATH, "a") as f:
262
+ f.write(json.dumps(ev) + "\n")
263
+ _maybe_trim_logs()
252
264
  except Exception as e:
253
265
  logger.warning(f"botwall: event log failed: {e}")
254
266
 
255
267
 
268
+ def _line_ts(line: str):
269
+ """A log line's timestamp: JSON `ts`, else the first token (.log lines)."""
270
+ try:
271
+ return json.loads(line)["ts"]
272
+ except Exception:
273
+ parts = line.split(None, 1)
274
+ return parts[0] if parts else None
275
+
276
+
277
+ def _maybe_trim_logs() -> None:
278
+ """Drop log lines older than LOG_RETENTION_DAYS. Caller holds _LOG_LOCK.
279
+
280
+ shortcut: another process appending during the os.replace loses that line;
281
+ fine for an audit log, use a file lock if several processes share a state dir."""
282
+ global _last_trim
283
+ if LOG_RETENTION_DAYS <= 0 or time.time() - _last_trim < 3600:
284
+ return
285
+ _last_trim = time.time()
286
+ max_age = LOG_RETENTION_DAYS * 86400
287
+ for path in (EVENTS_PATH, EXCLUDED_JSONL, EXCLUDED_LOG):
288
+ tmp = f"{path}.tmp.{os.getpid()}"
289
+ try:
290
+ if not os.path.exists(path):
291
+ continue
292
+ dropped = 0
293
+ # Streamed line by line: a log that only ever grew can be far larger than memory.
294
+ with open(path) as src, open(tmp, "w") as dst:
295
+ for ln in src:
296
+ if (age := _age_seconds(_line_ts(ln))) is None or age < max_age:
297
+ dst.write(ln)
298
+ else:
299
+ dropped += 1
300
+ if not dropped:
301
+ os.unlink(tmp)
302
+ continue
303
+ os.chmod(tmp, os.stat(path).st_mode & 0o7777)
304
+ os.replace(tmp, path)
305
+ except Exception as e:
306
+ logger.warning(f"botwall: log trim failed for {path}: {e}")
307
+ if os.path.exists(tmp):
308
+ os.unlink(tmp)
309
+
310
+
256
311
  def log_final(url: str, outcome: str, latency_ms=None, error=None,
257
312
  status_code=None) -> None:
258
313
  """Write one aggregate event for a URL's final cascade result (all_failed /
@@ -283,10 +338,12 @@ def _log_exclusion(url: str, reason: str) -> None:
283
338
  """Write to both the structured JSONL and the human-readable log."""
284
339
  ev = {"ts": _now(), "url": url, "host": host_of(url), "reason": reason}
285
340
  try:
286
- with open(EXCLUDED_JSONL, "a") as f:
287
- f.write(json.dumps(ev) + "\n")
288
- with open(EXCLUDED_LOG, "a") as f:
289
- f.write(f"{ev['ts']} EXCLUDED {url} [{reason}]\n")
341
+ with _LOG_LOCK:
342
+ with open(EXCLUDED_JSONL, "a") as f:
343
+ f.write(json.dumps(ev) + "\n")
344
+ with open(EXCLUDED_LOG, "a") as f:
345
+ f.write(f"{ev['ts']} EXCLUDED {url} [{reason}]\n")
346
+ _maybe_trim_logs()
290
347
  except Exception as e:
291
348
  logger.warning(f"botwall: exclusion log failed: {e}")
292
349
 
@@ -34,6 +34,14 @@ def looks_blocked(html: str, url: str) -> bool:
34
34
  return _looks_like_botwall(html_to_markdown(html, base_url=url))
35
35
 
36
36
 
37
+ def raise_if_missing(response) -> None:
38
+ """Fail the tier on a 404/410 document, as tier_2 does; a browser otherwise
39
+ renders the site's "Page not found" page and returns it as the article."""
40
+ status = getattr(response, "status", None)
41
+ if status in (404, 410):
42
+ raise RuntimeError(f"HTTP Error {status}: page not found")
43
+
44
+
37
45
  def response_bytes(responses) -> int:
38
46
  """Total wire bytes a render pulled across every resource — the residential-cost
39
47
  basis. Reads only the Content-Length header: it's non-blocking. (We deliberately
@@ -80,7 +80,7 @@ def fetch(url: str, timeout_ms: int = int(_TIMEOUT_S * 1000)) -> str:
80
80
  page = ctx.new_page()
81
81
  responses: list = []
82
82
  page.on("response", lambda resp: responses.append(resp))
83
- page.goto(url, timeout=timeout_ms, wait_until="domcontentloaded")
83
+ _browser.raise_if_missing(page.goto(url, timeout=timeout_ms, wait_until="domcontentloaded"))
84
84
  html = page.content()
85
85
  if len(html) < 5000:
86
86
  try:
@@ -86,7 +86,7 @@ def fetch(url: str) -> str:
86
86
  auth = session_cache.browser_cookies(url)
87
87
  if auth:
88
88
  page.context.add_cookies(auth)
89
- page.goto(url, wait_until="networkidle", timeout=_TIMEOUT_MS)
89
+ _browser.raise_if_missing(page.goto(url, wait_until="networkidle", timeout=_TIMEOUT_MS))
90
90
  html = page.content()
91
91
  # JS bot-manager sensor interstitial → settle + reload once.
92
92
  if _browser.looks_blocked(html, url):
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: switchback
3
- Version: 0.6.2
3
+ Version: 0.7.0
4
4
  Summary: One cost-ordered scrape cascade (HTTP → stealth browser → paid), shared by every tool.
5
5
  Author-email: Akash Kodavuru <akash@theaklabs.com>
6
6
  License: MIT
@@ -414,6 +414,7 @@ export OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4317
414
414
  - `SCRAPER_BROWSER_CONCURRENCY` — max simultaneous headless browsers (default 1)
415
415
  - `SCRAPER_BOTWALL_URL_SKIP_COOLDOWN_H` — auto-skip re-test window (24h; 0 = never)
416
416
  - `SCRAPER_BOTWALL_EGRESS_AFTER` — local-tier failures before a host escalates to the residential tier (default 2)
417
+ - `SCRAPER_BOTWALL_LOG_RETENTION_DAYS` — keep only the last N days of `botwall_events.jsonl` / `botwall_excluded.jsonl` / `botwall_excluded.log`, trimmed at most hourly (**0 = keep everything**, the default)
417
418
  - `SCRAPER_SESSION_TTL_S` — cf_clearance reuse window (1800s)
418
419
  - `SCRAPER_DISABLE_SESSION_CACHE` — turn off cf_clearance reuse
419
420
  - `SCRAPER_CONTENT_TTL_S` — URL→result cache TTL (**0 = off**; set e.g. 86400 to skip re-scraping a page within a day)
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes