Skip to content

[SCR-383] Auto-Mode (--mode auto / --max-cost) support#26

Open
kostas-jakeliunas-sb wants to merge 4 commits into
mainfrom
SCR-383/auto-mode
Open

[SCR-383] Auto-Mode (--mode auto / --max-cost) support#26
kostas-jakeliunas-sb wants to merge 4 commits into
mainfrom
SCR-383/auto-mode

Conversation

@kostas-jakeliunas-sb

@kostas-jakeliunas-sb kostas-jakeliunas-sb commented Jun 30, 2026

Copy link
Copy Markdown
Contributor

Merge order: part of the [SCR-383] Auto-Mode client rollout — merge once the server PR ScrapingBee/scrapingbee-API#1242 is merged + the flag is enabled.

tl;dr

Adds Auto-Mode to the scrapingbee scrape command: --mode auto lets the API pick the cheapest scraping config that succeeds and charges only for the winning config; --max-cost N caps the credits a request may spend. Threaded through all four layers (command → cli_utils → client → tests), mirroring the SCR-426 --tag pattern. Versioned 1.4.5. Server feature is beta + flag-gated — do not merge until it ships.

Summary

  • src/scrapingbee_cli/commands/scrape.py — new --mode (click.Choice(["auto"])) and --max-cost (int) options in the option group; added to scrape_cmd signature; passed into build_scrape_kwargs(...). Three validation guards in the try: block (matching existing guard style):
    • --mode auto + any of --render-js / --premium-proxy / --stealth-proxy / --transparent-status-code (presence, even false) → error + exit 1 (Auto-Mode picks these itself; the server 400s the combo).
    • --max-cost without --mode auto → error + exit 1.
    • --max-cost < 1 → error via the existing _validate_range pattern.
  • src/scrapingbee_cli/cli_utils.pybuild_scrape_kwargs() gains mode / max_cost (signature + returned dict; scrape_kwargs_to_api_params auto-passes through). write_output() verbose block now surfaces the Spb-auto-cost response header as Auto Credit Cost, alongside Spb-cost.
  • src/scrapingbee_cli/client.pyClient.scrape() gains mode / max_cost; added to the params list. _clean_params() drops None, so an omitted --max-cost = uncapped for free.
  • tests/unit/test_client.pyTestModeParam + TestMaxCostParam (sent-when-set / omitted-when-unset), mirroring TestTagParam via patched client._get.
  • tests/unit/test_cli.pyTestModeAutoGuards: command-level guard tests (CliRunner + patched get_api_key) for all conflicting-option combos, --max-cost requiring --mode auto, --max-cost < 1, and the valid pass-through cases.
  • Docsskills/scrapingbee-cli/reference/scrape/options.md gains a dedicated Auto-Mode section + --mode / --max-cost rows; README.md key-features bullet; CHANGELOG.md [1.4.5] entry. Propagated to all mirror dirs via ./sync-skills.sh.
  • Version — bumped 1.4.4 → 1.4.5 across all version-bearing files (pyproject.toml, __init__.py ×2, plugin.json, AGENTS.md upgrade line, canonical + guard SKILL.md frontmatter, synced mirrors).

Test plan

  • pytest -m "not integration"760 passed, 9 deselected.
  • ruff check src tests → clean. ruff format --check src tests → clean (after format).
  • check-version consistency: pyproject.toml == __init__.py == 1.4.5.
  • scrapingbee --version1.4.5; scrape --help shows --mode [auto] and --max-cost INTEGER.
  • End-to-end guard smoke (real CLI binary): all three guards exit 1 with the exact messages.
  • Live request against the server once Auto-Mode ships on prod (verify Spb-auto-cost header rendering + actual cheapest-config charging).

Versioning note

Versioned 1.4.5 off main v1.4.4. The open REPL PR #19 (not a draft, no CI) self-bumped to 1.5.0. If #19 merges first, rebase and re-bump (likely 1.5.1 or 1.6.0) to keep the check-version job green and avoid a duplicate/back-numbered tag — version/CHANGELOG may need amending depending on merge order.

Cross-link: #19

🤖 Generated with Claude Code

Add Auto-Mode to the `scrape` command, mirroring the server-side beta feature.
The API picks the cheapest scraping config that succeeds (cheap -> expensive,
stops at first success) and charges only for the winning config.

- scrape.py: `--mode` (Choice[auto]) + `--max-cost` (int) options; signature;
  pass-through to build_scrape_kwargs; validation guards (mode-auto vs
  render_js/premium_proxy/stealth_proxy/transparent_status_code incompatibility,
  max_cost requires mode=auto, max_cost >= 1 via _validate_range).
- cli_utils.py: build_scrape_kwargs mode/max_cost; surface Spb-auto-cost header
  ("Auto Credit Cost") in write_output verbose block.
- client.py: Client.scrape() mode/max_cost params (omitted when None).
- Tests: TestModeParam + TestMaxCostParam (test_client.py) and
  TestModeAutoGuards command-level guard tests (test_cli.py).
- Docs: SKILL.md reference/scrape/options.md (Auto-Mode section), README,
  CHANGELOG [1.4.5]; synced via sync-skills.sh.
- Version bumped 1.4.4 -> 1.4.5 across all version-bearing files.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@kostas-jakeliunas-sb kostas-jakeliunas-sb marked this pull request as ready for review June 30, 2026 12:57
@kostas-jakeliunas-sb kostas-jakeliunas-sb changed the title [DRAFT — DO NOT MERGE] [SCR-383] Auto-Mode (--mode auto / --max-cost) support [SCR-383] Auto-Mode (--mode auto / --max-cost) support Jun 30, 2026
@kostas-jakeliunas-sb kostas-jakeliunas-sb self-assigned this Jun 30, 2026
kostas-jakeliunas-sb and others added 3 commits July 13, 2026 16:42
# Conflicts:
#	.agents/skills/scrapingbee-cli-guard/SKILL.md
#	.agents/skills/scrapingbee-cli/SKILL.md
#	.github/skills/scrapingbee-cli-guard/SKILL.md
#	.github/skills/scrapingbee-cli/SKILL.md
#	.kiro/skills/scrapingbee-cli-guard/SKILL.md
#	.kiro/skills/scrapingbee-cli/SKILL.md
#	.opencode/skills/scrapingbee-cli-guard/SKILL.md
#	.opencode/skills/scrapingbee-cli/SKILL.md
#	AGENTS.md
#	CHANGELOG.md
#	plugins/scrapingbee-cli/.claude-plugin/plugin.json
#	plugins/scrapingbee-cli/skills/scrapingbee-cli-guard/SKILL.md
#	plugins/scrapingbee-cli/skills/scrapingbee-cli/SKILL.md
#	pyproject.toml
#	src/scrapingbee_cli/__init__.py
#	src/scrapingbee_cli/cli_utils.py
#	tests/unit/test_cli.py
AGENTS.md is the self-contained reference for agent tools, but the
Auto-Mode commit only bumped its version line. Add --mode auto and
--max-cost to the scrape options block, an Auto-Mode note covering the
escalation ladder, GET-only limit, rejected flag combinations, and the
Spb-auto-cost header, plus a credit-cost table row.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
test_session_default_skip_warning_on_screen asserted against the visible
pyte screen, but the warning is a single line printed before the full
`google --help` output, which immediately scrolls it out of the 32-row
viewport. The test only passed when the pump captured an intermediate
frame before the scroll — a race that failed on random OS/Python matrix
cells (seen on main pushes for #27/#28 and on SCR-560 CI runs).

Match on the accumulated ANSI-stripped raw PTY stream instead, which is
scroll-immune: add _strip_ansi/_pump_until_raw and use them in both
session-default tests (the negative test asserts on everything ever
printed, which is also a stronger check than the final screen). Also
makes the positive test return immediately instead of waiting out the
20s pump timeout.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
return _ANSI_RE.sub("", s)


def _pump_until_raw(child, screen, stream, predicate, timeout=15.0):

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was 100% found by claude; I can move to separate PR if needed btw.

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.

2 participants