Skip to content

The agent that grows with you

fix(browser): a served profile's harness socket fits Linux sun_path for any session name

Teknium403856e943e6
diff --git a/tests/tools/conftest.py b/tests/tools/conftest.py
index d4ab0ae3a6d..41afea411ca 100644
--- a/tests/tools/conftest.py
+++ b/tests/tools/conftest.py
@@ -35,6 +35,20 @@ def _no_host_browser_use_cli():
yield
[email protected](autouse=True)
+def _no_shared_temp_socket_dirs():
+ """A served profile's over-budget harness runtime dir is moved to a private ``hbh-<hash>`` dir
+ in the shared temp root (sun_path budget). Unit tests never claim one there: fake CLIs would
+ leave latch files behind. The dir keeps its long path; tests of the claim restore the real one."""
+ try:
+ import tools.browser_use_cli as bu_cli
+ except Exception:
+ yield
+ return
+ with patch.object(bu_cli, "_claim_private_dir", lambda path: False):
+ yield
+
+
@pytest.fixture(autouse=True)
def _no_host_bot_desktop_autostart():
"""Keep the host's TigerVNC/Xfce install out of tests.
diff --git a/tests/tools/test_browser_profile_scope_edges.py b/tests/tools/test_browser_profile_scope_edges.py
index ed96b8665cf..ce595637fd6 100644
--- a/tests/tools/test_browser_profile_scope_edges.py
+++ b/tests/tools/test_browser_profile_scope_edges.py
@@ -12,6 +12,7 @@ from agent import secret_scope
from gateway.run import _profile_runtime_scope
from hermes_constants import get_hermes_home, hermes_home_key
from tools import browser_tool_cdp as bt_cdp
+from tools.browser_use_cli import _claim_private_dir as _REAL_CLAIM # conftest stubs it per test
@pytest.fixture
@@ -100,12 +101,13 @@ def test_a_served_profiles_harness_socket_fits_macos_sun_path_on_a_default_home(
@pytest.mark.platforms("macos")
-def test_on_macos_a_served_profile_binds_a_64_char_session_socket(profiles, tmp_path):
+def test_on_macos_a_served_profile_binds_a_64_char_session_socket(profiles, tmp_path, monkeypatch):
"""Real branch: the dir is claimed under the real short root and a 64-character name binds."""
import os
import socket
from tools import browser_use_cli as bu_cli
a, _b = profiles
+ monkeypatch.setattr(bu_cli, "_claim_private_dir", _REAL_CLAIM) # the real claim, real short root
env = {"HOME": str(tmp_path / ("u" * 40))}
with _profile_runtime_scope(a, hydrate_secrets=False):
bu_cli._namespace_harness_daemons(env)
@@ -120,19 +122,73 @@ def test_on_macos_a_served_profile_binds_a_64_char_session_socket(profiles, tmp_
os.unlink(path)
+def _kernel_accepts_sun_path(path: str) -> bool:
+ """Ask the kernel ABI whether ``path`` fits ``sun_path``: an unbound dir gives ENOENT, an overlong
+ path gives "AF_UNIX path too long" before any filesystem access."""
+ import socket
+ s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
+ try:
+ s.bind(path)
+ except OSError as e:
+ return "too long" not in str(e)
+ else:
+ import os
+ os.unlink(path)
+ return True
+ finally:
+ s.close()
+
+
[email protected]("user_home", ["/home/teknium", "/home/alexander.smith"])
+def test_on_linux_a_served_profiles_64_char_session_socket_fits_sun_path(profiles, monkeypatch, user_home):
+ """Linux ``sun_path`` is 108 bytes (107 + NUL). ``~/.config/browser-harness/runtime/p-<hash>`` left
+ room for 38-character names at /home/teknium and 30 at /home/alexander.smith, while BU_NAME may be
+ 64. The served profile gets the same private short dir as on macOS (claim stubbed: no temp-root
+ write); the kernel judges the length of the exact path the installed harness binds."""
+ import os
+ from tools import browser_use_cli as bu_cli
+ a, _b = profiles
+ claimed = []
+ monkeypatch.setattr(bu_cli, "_claim_private_dir", lambda path: claimed.append(path) or True)
+ env = {"HOME": user_home}
+ with _profile_runtime_scope(a, hydrate_secrets=False):
+ bu_cli._namespace_harness_daemons(env)
+ sock = f"{env['BH_RUNTIME_DIR']}/bu-{_LONGEST_NAME}.sock"
+ assert claimed == [env["BH_RUNTIME_DIR"]]
+ assert _sock_len(env["BH_RUNTIME_DIR"]) <= 107 and _kernel_accepts_sun_path(sock), sock
+ assert os.path.basename(env["BH_RUNTIME_DIR"]).startswith("hbh-")
+
+
+def test_on_linux_a_runtime_dir_that_already_fits_is_kept_and_an_unclaimable_one_stays_long(profiles, monkeypatch):
+ from tools import browser_use_cli as bu_cli
+ a, _b = profiles
+ claimed = []
+ monkeypatch.setattr(bu_cli, "_claim_private_dir", lambda path: claimed.append(path) or True)
+ env = {"BH_HOME": "/b"}
+ with _profile_runtime_scope(a, hydrate_secrets=False):
+ bu_cli._namespace_harness_daemons(env)
+ assert env["BH_RUNTIME_DIR"].startswith("/b/runtime/p-") and claimed == []
+ monkeypatch.setattr(bu_cli, "_claim_private_dir", lambda path: False) # squatted / not ours
+ env = {"HOME": "/home/teknium"}
+ with _profile_runtime_scope(a, hydrate_secrets=False):
+ bu_cli._namespace_harness_daemons(env)
+ assert env["BH_RUNTIME_DIR"].startswith("/home/teknium/.config/browser-harness/runtime/p-")
+
+
def test_the_short_socket_dir_is_claimed_only_when_private_and_ours(tmp_path):
"""The short dir sits in a world-writable root: a symlink or another user's/open dir is refused."""
- from tools import browser_use_cli as bu_cli
fresh = tmp_path / "hbh-fresh"
- assert bu_cli._claim_private_dir(str(fresh)) and (fresh.stat().st_mode & 0o777) == 0o700
- assert bu_cli._claim_private_dir(str(fresh)) # ours, 0700: reused
+ assert _REAL_CLAIM(str(fresh)) and (fresh.stat().st_mode & 0o777) == 0o700
+ assert _REAL_CLAIM(str(fresh)) # ours, 0700: reused
open_dir = tmp_path / "hbh-open"
open_dir.mkdir(mode=0o755)
open_dir.chmod(0o755)
- assert not bu_cli._claim_private_dir(str(open_dir))
+ assert not _REAL_CLAIM(str(open_dir))
link = tmp_path / "hbh-link"
link.symlink_to(fresh)
- assert not bu_cli._claim_private_dir(str(link))
+ assert not _REAL_CLAIM(str(link))
# --- per-profile harness tmp dir (screenshots, debug overlays, daemon logs) ---------------------------
diff --git a/tools/browser_use_cli.py b/tools/browser_use_cli.py
index c3d629e258d..619d24bb5ba 100644
--- a/tools/browser_use_cli.py
+++ b/tools/browser_use_cli.py
@@ -207,23 +207,40 @@ def _namespace_harness_daemons(env: dict) -> None:
# browser_harness/_ipc.py: a shared runtime dir names a daemon socket ``<dir>/bu-<BU_NAME>.sock`` and
-# BU_NAME may be 64 characters, so the dir gets the macOS sun_path (104 bytes with the NUL) minus 72.
+# BU_NAME may be 64 characters, so the dir gets sun_path (macOS 104 bytes, Linux 108, with the NUL)
+# minus 72.
_MACOS_SOCKET_DIR_BUDGET = 103 - len("/bu-.sock") - 64
+_LINUX_SOCKET_DIR_BUDGET = 107 - len("/bu-.sock") - 64
+
+
+def _socket_dir_budget() -> Optional[int]:
+ """The runtime-dir byte budget on platforms where the harness binds AF_UNIX sockets, else None."""
+ if sys.platform == "darwin":
+ return _MACOS_SOCKET_DIR_BUDGET
+ if sys.platform.startswith("linux"):
+ return _LINUX_SOCKET_DIR_BUDGET
+ return None # Windows: the harness serves over loopback TCP (port files), no sun_path
def _socket_fit_runtime_dir(runtime: str, base: str, tag: str) -> str:
- """``runtime`` unless it is too long for a 64-character session's socket on macOS, where
- ``~/.config/browser-harness/runtime`` alone already exceeds the budget. There the served profile
- gets a private dir under the short temp root (``/private/tmp/hbh-<12 hex>``, 29 bytes), keyed by
- uid, root and profile; if it can't be claimed safely the long path stays (shorter names only)."""
- if sys.platform != "darwin" or len(runtime.encode()) <= _MACOS_SOCKET_DIR_BUDGET:
+ """``runtime`` unless it is too long for a 64-character session's socket (macOS: the default
+ ``~/.config/browser-harness/runtime`` alone exceeds it; Linux: anything past a 34-byte dir). Then
+ the served profile gets a private dir under the short socket temp root (``hbh-<12 hex>``), keyed by
+ uid, root and profile; if none fits or can be claimed safely the long path stays (shorter names
+ only)."""
+ budget = _socket_dir_budget()
+ if budget is None or len(runtime.encode()) <= budget:
return runtime
from hermes_constants import socket_safe_tmpdir
getuid = getattr(os, "getuid", lambda: 0)
digest = hashlib.sha256(f"{getuid()}\0{base}\0{tag}".encode()).hexdigest()[:12]
- short = os.path.join(os.path.realpath(socket_safe_tmpdir()), f"hbh-{digest}")
- if len(short.encode()) <= _MACOS_SOCKET_DIR_BUDGET and _claim_private_dir(short):
- return short
+ # socket_safe_tmpdir() caps the root at 50 bytes for Chrome's longer socket names; ours needs a
+ # root of at most 17, which a Hermes scratch TMPDIR exceeds, so the sticky system root follows.
+ roots = dict.fromkeys(os.path.realpath(r) for r in (socket_safe_tmpdir(), "/tmp")) # no-tmp: ok — AF_UNIX sun_path budget; dir claimed 0700 and ownership-checked
+ for root in roots:
+ short = os.path.join(root, f"hbh-{digest}")
+ if len(short.encode()) <= budget and _claim_private_dir(short):
+ return short
return runtime