From a7edeeb1d868fb552d299cd147e5e206a1ddc2cb Mon Sep 17 00:00:00 2001 From: ethernet Date: Sun, 6 Sep 2026 16:37:34 -0400 Subject: [PATCH] fix(browser): accept BOM-prefixed surviving Chrome metadata The surviving-profile lookup rejected a valid DevToolsActivePort after a BOM. Extend the existing identity-match regression across BOM and BOM-less files; the BOM case fails before the read fix. --- tests/tools/test_browser_real_profile.py | 5 +++-- tools/browser_tool_real_profile.py | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/tests/tools/test_browser_real_profile.py b/tests/tools/test_browser_real_profile.py index 2fb47b9f12..2aa870c676 100644 --- a/tests/tools/test_browser_real_profile.py +++ b/tests/tools/test_browser_real_profile.py @@ -355,15 +355,16 @@ class TestRealProfileCdpLaunch: assert cdp == "http://127.0.0.1:41000" self._reset() + @pytest.mark.parametrize("encoding", ["utf-8", "utf-8-sig"]) @pytest.mark.parametrize("live_browser_id", ["/devtools/browser/x", "/devtools/browser/other"]) - def test_reattaches_to_surviving_chrome_instead_of_overlaying_its_profile(self, tmp_path, live_browser_id): + def test_reattaches_to_surviving_chrome_instead_of_overlaying_its_profile(self, tmp_path, live_browser_id, encoding): """The attach daemon of a crashed owner gets reaped, but its Chrome (Hermes-launched, own session) survives holding the copy dir: re-attach, never re-run the snapshot. A DevToolsActivePort left by a crash whose port was recycled by ANOTHER CDP server (browser id mismatch) must not be attached to; the normal launch path runs.""" import tools.browser_tool as bt self._reset() - (tmp_path / "DevToolsActivePort").write_text("41000\n/devtools/browser/x\n") + (tmp_path / "DevToolsActivePort").write_text("41000\n/devtools/browser/x\n", encoding=encoding) version = Mock() version.json.return_value = {"webSocketDebuggerUrl": f"ws://127.0.0.1:41000{live_browser_id}"} with patch.object(bt_cloud, "_use_real_profile", return_value=True), \ diff --git a/tools/browser_tool_real_profile.py b/tools/browser_tool_real_profile.py index 51155c444a..45815fa22f 100644 --- a/tools/browser_tool_real_profile.py +++ b/tools/browser_tool_real_profile.py @@ -85,7 +85,7 @@ def _surviving_chrome_cdp(data_dir: str) -> Optional[str]: outlives a crashed Chrome and its port can be recycled by another local CDP server, so the file's browser id (line 2) must match what ``/json/version`` reports before it is trusted.""" try: - with open(os.path.join(data_dir, "DevToolsActivePort"), encoding="utf-8") as fh: + with open(os.path.join(data_dir, "DevToolsActivePort"), encoding="utf-8-sig") as fh: port, browser_path = fh.readline().strip(), fh.readline().strip() except OSError: return None