Skip to content

Count proxy traffic per run and stop at --proxy-budget-mb (default 10 MB) - #7

Merged
jehrr merged 1 commit into
mainfrom
feat/proxy-traffic-meter-2026-10-21
Oct 21, 2026
Merged

jehrr merged 1 commit into
mainfrom
feat/proxy-traffic-meter-2026-10-21

Conversation

@jehrr

@jehrr jehrr commented Oct 21, 2026

Copy link
Copy Markdown
Collaborator

Problem

A run through a residential proxy is billed per byte, and nothing measured or limited what a run sent through it. A run that kept retrying or rotating, or a site page that grew, could spend a lot before anyone looked.

Resulting behaviour

This applies only to a local browser behind --proxy / --proxy-file / AUTOTRADER_PROXY. Nothing changes over --cdp-endpoint, with no proxy, or in scraper_api_client.py.

  • Meter (proxy_meter.py). The count is an estimate of the tunnelled payload. It does not include TLS/CONNECT overhead, WebSocket frames or Chrome's own background requests.
    • Playwright: request.sizes() (request headers + body, response headers + body) on requestfinished / requestfailed, for every context _launch_local makes, including each concurrent worker's.
    • pyppeteer: a CDP session per page counts Network.loadingFinished.encodedDataLength of responses that came over the network, plus the request line, headers and body from Network.requestWillBeSent. Chrome's own error page and cache hits are not counted.
    • Selenium: the same CDP events, from the performance log. It is turned on only when metering and polled after each navigation, each document read and before quit().
    • The pre-check's HEAD is counted through a requests response hook.
  • Budget --proxy-budget-mb (default 10, 0 = unlimited). A negative or non-number value is a usage error that names the flag. It is a flag only, because ENV_KEYS maps only credentials and URLs. Once the total passes the budget:
    • sequential: no next page, no retry, no rotation or fresh-browser re-fetch;
    • --concurrency: no worker takes another page, pages already loading finish, and the queue's remainder is reported;
    • browser: Playwright aborts every further request through a route (registered after the media route, then fallback()). pyppeteer and Selenium set Network.setBlockedURLs ["*"];
    • outcome: the rows are kept, and the run ends with stop_reason: proxy_budget_exceeded, exit 6 with rows and 5 without. finish_run puts this above a block, so a spent budget on a refused page is 5, not 3.
  • Sidecar:
    • proxy_bytes, proxy_budget_bytes and proxy_budget_exceeded, which are null / null / false without a local proxy;
    • pages_unattempted;
    • a proxied run that writes no rows now records its outcome and bytes in <out>.latest_attempt.meta.json, and the data and .meta.json are left as they were;
    • one line at the end: Proxy traffic: X.X MB of Y MB budget.
  • Canary: both runs, listing and vehicle, pass --proxy-budget-mb 10. An if: always() step after each, .github/proxy_traffic_summary.py, appends **run listing** / **run vehicle**: proxy traffic X.X MB of 10 MB to $GITHUB_STEP_SUMMARY. If the budget was hit, it prints ::error::stopped: proxy budget 10 MB exceeded and fails.
  • CI: the engine jobs now install Playwright's Chromium and run tests/test_proxy_meter.py with PROXY_METER_REQUIRE=<engine>. A missing browser fails the job instead of skipping.

Evidence (offline; no proxy, no paid call, no canary dispatched)

  • tests/test_proxy_meter.py. A stub forward proxy serves a page of known size (about 152 KB) and counts the bytes on its sockets. Compared with that count:
    • requests: within 2%;
    • real Chromium via Playwright, pyppeteer and Selenium: within 10%, and 0.99–1.00 locally;
    • a 50 KB budget on Playwright: the second navigation gets ERR_BLOCKED_BY_CLIENT.
  • Real CLIs through the stub proxy. The stub refuses CONNECT, so autotrader.com was never reached.
    • Budget 10: rc 5 in all three engines, Proxy traffic: 0.0 MB of 10 MB budget, and an attempt sidecar with proxy_bytes 320–640.
    • --proxy-budget-mb 0.0001 --retries 3: rc 5 with stop_reason: proxy_budget_exceeded. Playwright stopped after ONE CONNECT, with no retries.
  • Suites. smoke_test.py: 555 checks in core, 568 in playwright, 560 in pyppeteer and 558 in selenium. All green; each engine venv runs its own engine's checks. pytest: 11 passed, 4 skipped in core.
  • New checks:
    • budget spent on page 2 of 5: exit 6, 2 navigations, 7 rows, sidecar (1200, 1000, True), pages_unattempted [3,4,5];
    • spent on a refused page 1: exit 5 (not 3), no fresh-browser re-fetch, attempt sidecar written, data untouched;
    • a timed-out navigation is not retried;
    • rows followed by a refusal give 6 with proxy_budget_exceeded;
    • no proxy: nothing metered, and the fields are null; budget 0 means unlimited;
    • concurrent workers stop, and attempted + queued pages = 29;
    • flag default and validation;
    • per-engine wiring: Playwright route order, Selenium tee, pyppeteer attach;
    • canary steps and their script.
  • Planted-fault controls. Each went red on the named check and back to green when restored:
    • A, meter add() disabled: 11 smoke failures and 8 test_proxy_meter failures;
    • B1, sequential page check removed: unattempted-pages check;
    • B2a, retry-loop check: "timed-out navigation is not retried";
    • B2b, block-retry check: "no re-fetch from a fresh browser";
    • B3, worker-loop check: concurrent dispatch check;
    • B4, finish_run override: 5 checks;
    • C, listing summary step replaced: canary check.

Not changed

  • No version bump, release or canary dispatch.
  • Chrome's own background traffic is not suppressed.

🤖 Generated with Claude Code

https://claude.ai/code/session_01CKsd7q25N19WzUZjrAdzx8

… MB)

A local browser run through --proxy / --proxy-file now counts the bytes
it sends through the proxy (Playwright: request.sizes(); pyppeteer and
Selenium: CDP Network.loadingFinished.encodedDataLength plus request
sizes; the pre-check HEAD too). Once the total passes --proxy-budget-mb
no new page, retry or re-fetch is started (concurrent workers take no
further page), the browser aborts what it still tries, the rows read so
far are kept, and the run exits 6 (5 with no rows) with stop_reason
proxy_budget_exceeded. The sidecar records proxy_bytes,
proxy_budget_bytes, proxy_budget_exceeded and pages_unattempted, and the
run ends with one "Proxy traffic: X.X MB of Y MB budget" line. Nothing
is metered over --cdp-endpoint or with no proxy.

The canary passes --proxy-budget-mb 10 to both runs, writes their
traffic to the job summary and fails when the budget was hit.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CKsd7q25N19WzUZjrAdzx8
@jehrr
jehrr merged commit 0e703ad into main Oct 21, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant