feat: cache generated Playwright scripts so repeat runs skip the LLM loop
Summary
Webwright now supports an opt-in script cache that lets repeat runs of the same task skip the agent loop entirely and execute the cached Playwright script directly. Cache hits cost zero LLM tokens.
Why this matters
Webwright's pitch in the Microsoft Research blog post is that the agent's persistent artifact is code: a final_script.py per task that "can be rerun, adapted, and shared across tasks rather than rediscovered from scratch." Today that artifact is saved but never reused — running the same task twice regenerates final_script.py from scratch and burns the full LLM budget twice.
Stagehand built its v3 wedge against browser-use on exactly this property: auto-caching combined with self-healing remembers previous actions and runs without LLM inference. Independent reviews note that "for repeated workflows (same sites, same forms, many times a day), Stagehand's caching means your costs approach zero after the first run." This change brings webwright the same property for full-script replay.
Changes
- New
src/webwright/cache/script_cache.pyaddsmake_fingerprint(config)(SHA-256 overtask,start_url,model.model_name,environment.environment_classby default; fields configurable) and aScriptCacheclass that reads and writes entries under~/.cache/webwright/<fingerprint>/. Each entry holdsmetadata.json,final_script.py, andtrajectory.json. CacheConfigis added toconfig/__init__.pywithenabled: falseas the default. The feature is fully opt-in.run/cli.pychecks the cache before constructing the model + agent. On hit, it runs the cached script via the local workspace environment and returns a result shaped like a normal run (withcached: true). On any failure during replay (selector drift, exception, 4xx/5xx onstart_urlwhenvalidate_url: true), the entry is invalidated and the agent loop runs normally.agents/default.pywrites a new cache entry after every successful run whencache.enabledis set.
No new runtime dependencies — uses stdlib hashlib/json/pathlib and the existing httpx for the URL precheck.
Testing
tests/unit/test_script_cache.py covers fingerprint stability (same inputs → same hash, one input change → different hash), script-error invalidation (cached script raises → entry is removed), and start-URL invalidation (HEAD returns 5xx → entry is removed). 3 tests, all passing.
A meaningful end-to-end test requires a real LLM run on both ends of the cache window, so a follow-up integration test is left out of this PR; the unit-level invariants are what would block re-merges.
Usage
Opt in via config:
# base.yaml
cache:
enabled: true
directory: ~/.cache/webwright
ttl_seconds: 604800
validate_url: true
Or via CLI: webwright -t "..." --start-url "..." --cache.enabled=true.
A cache hit prints Cache hit: skipping model loop to the console; a miss prints Cache miss and runs normally.