Skip to content

The agent that grows with you

fix(browser): an interrupted profile switch never vouches for a mixed snapshot

Teknium2e83e2a935ee
diff --git a/hermes_cli/browser_connect.py b/hermes_cli/browser_connect.py
index d9788c9aebe..8c5c3664872 100644
--- a/hermes_cli/browser_connect.py
+++ b/hermes_cli/browser_connect.py
@@ -756,14 +756,17 @@ def snapshot_real_profile(browser: str, src: str | None = None) -> tuple[str | N
# Only a copy that previously COMPLETED counts as populated; a half-written tree is
# rebuilt — otherwise a torn first copy poisons freshness forever.
populated = os.path.isfile(marker)
+ same_source = populated and _snapshot_source(marker) == source_profile
# Fast lock probe BEFORE any copy: a blocking file op on a Windows-locked cookie DB can
# hang the launch for minutes. Never trips on POSIX, where a running browser only locks the
# password/autofill DBs, which ``_mirror_profile_auth`` skips.
if _profile_is_locked(src, source_profile):
- if populated and _snapshot_source(marker) == source_profile:
- return _reuse_snapshot(browser, dst)
- return None, _locked_profile_error(browser)
+ return _reuse_snapshot(browser, dst) if same_source else (None, _locked_profile_error(browser))
try:
+ if populated and not same_source:
+ # Drop another profile's marker BEFORE re-syncing: an interrupted sync must never leave
+ # it vouching for this profile's cookies on a later locked reuse.
+ os.unlink(marker)
os.makedirs(dst, exist_ok=True)
# Secure the snapshot dir AND its browser-profile parent on EVERY launch so a failed
# first attempt or an older-build dir still converges to owner-only perms.
diff --git a/tests/tools/test_browser_real_profile.py b/tests/tools/test_browser_real_profile.py
index 59703605d11..615859193f7 100644
--- a/tests/tools/test_browser_real_profile.py
+++ b/tests/tools/test_browser_real_profile.py
@@ -974,7 +974,7 @@ class TestReviewRound3:
monkeypatch.setattr(bc, "get_hermes_home", lambda: home)
first, err = bc.snapshot_real_profile("chrome", src=str(src))
assert err is None
- with open(os.path.join(first, bc._SNAPSHOT_DONE_MARKER), "w", encoding="utf-8") as fh:
+ with open(os.path.join(first, bc._SNAPSHOT_DONE_MARKER), "w", encoding="utf-8") as fh: # windows-footgun: ok — write mode, not a read
fh.write("Default")
monkeypatch.setattr(bc, "_profile_is_locked", lambda s, p: True)
monkeypatch.setattr(bc, "_real_profile_autoclose", lambda: False)
@@ -982,6 +982,25 @@ class TestReviewRound3:
assert dst is None
assert err and err.startswith(bc._PROFILE_LOCKED_PREFIX)
+ def test_interrupted_profile_switch_never_reuses_stale_marker(self, tmp_path, monkeypatch):
+ """A sync from another source profile that dies mid-way must not leave the old marker
+ vouching for the mixed copy: a later locked launch refuses instead of reusing it."""
+ import hermes_cli.browser_connect as bc
+ src = self._multi(tmp_path / "real")
+ home = tmp_path / "hh"
+ monkeypatch.setattr(bc, "get_hermes_home", lambda: home)
+ _first, err = bc.snapshot_real_profile("chrome", src=str(src))
+ assert err is None # completed copy of Profile 6
+ monkeypatch.setattr(bc, "_resolve_source_profile", lambda s: ("Default", None))
+ monkeypatch.setattr(bc, "_mirror_profile_auth", lambda *a: (_ for _ in ()).throw(OSError("killed")))
+ assert bc.snapshot_real_profile("chrome", src=str(src))[0] is None # interrupted switch
+ monkeypatch.undo()
+ monkeypatch.setattr(bc, "get_hermes_home", lambda: home)
+ monkeypatch.setattr(bc, "_profile_is_locked", lambda s, p: True)
+ monkeypatch.setattr(bc, "_real_profile_autoclose", lambda: False)
+ dst, err = bc.snapshot_real_profile("chrome", src=str(src))
+ assert dst is None and err.startswith(bc._PROFILE_LOCKED_PREFIX)
+
def test_processes_holding_profile_identity_binding(self, tmp_path, monkeypatch):
"""The process matcher requires BOTH a browser binary AND this exact
user-data-dir in the cmdline — never a same-name process on another dir."""