sitcod3 commited on
Commit
af42101
·
verified ·
1 Parent(s): 5bb5035

fix: keep inherited Playwright browser cache when it ships revisions

Browse files
src/browser_automation_agent/browser/manager.py CHANGED
@@ -272,17 +272,39 @@ class BrowserManager:
272
  self._runtime_dir = runtime_root
273
  self._browser_user_data_dir = profile_root
274
  self._profile_root_notice = notice or None
275
- self._original_runtime_env = {
276
- key: os.environ.get(key)
277
- for key in ("TEMP", "TMP", "TMPDIR", "PLAYWRIGHT_BROWSERS_PATH")
278
- }
279
  # Keep Playwright's browser cache beside the app. The inherited global
280
  # cache can be writable to the shell but denied to the Python runtime.
281
- browser_cache = _resolve_writable_dir(
282
- runtime_root / "playwright-browsers",
283
- runtime_root / "playwright-browsers",
284
- )
285
- os.environ["PLAYWRIGHT_BROWSERS_PATH"] = str(browser_cache)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
286
  for key in ("TEMP", "TMP", "TMPDIR"):
287
  os.environ[key] = str(temp_dir)
288
 
 
272
  self._runtime_dir = runtime_root
273
  self._browser_user_data_dir = profile_root
274
  self._profile_root_notice = notice or None
 
 
 
 
275
  # Keep Playwright's browser cache beside the app. The inherited global
276
  # cache can be writable to the shell but denied to the Python runtime.
277
+ # EXCEPTION: when the inherited PLAYWRIGHT_BROWSERS_PATH already ships
278
+ # browser payloads (Playwright base images bake /ms-playwright with the
279
+ # exact revisions the pinned driver expects — HF Spaces, CI containers),
280
+ # keep it: redirecting to an app-local empty cache made the launch die
281
+ # with BROWSER_NOT_INSTALLED even though the browsers were on disk.
282
+ inherited = os.environ.get("PLAYWRIGHT_BROWSERS_PATH")
283
+ inherited_has_browsers = False
284
+ if inherited:
285
+ try:
286
+ inherited_has_browsers = any(
287
+ entry.is_dir()
288
+ and (entry.name.startswith("chromium-") or entry.name.startswith("firefox-"))
289
+ for entry in Path(inherited).iterdir()
290
+ )
291
+ except OSError:
292
+ inherited_has_browsers = False
293
+
294
+ env_keys = ("TEMP", "TMP", "TMPDIR")
295
+ if inherited_has_browsers:
296
+ self._original_runtime_env = {key: os.environ.get(key) for key in env_keys}
297
+ logger.info("Using inherited Playwright browser cache at %s", inherited)
298
+ else:
299
+ self._original_runtime_env = {
300
+ key: os.environ.get(key)
301
+ for key in ("TEMP", "TMP", "TMPDIR", "PLAYWRIGHT_BROWSERS_PATH")
302
+ }
303
+ browser_cache = _resolve_writable_dir(
304
+ runtime_root / "playwright-browsers",
305
+ runtime_root / "playwright-browsers",
306
+ )
307
+ os.environ["PLAYWRIGHT_BROWSERS_PATH"] = str(browser_cache)
308
  for key in ("TEMP", "TMP", "TMPDIR"):
309
  os.environ[key] = str(temp_dir)
310