mirror of
https://github.com/NousResearch/hermes-agent.git
synced 2026-11-16 19:04:08 +03:00
f5dc3f632b
Outside a served (multiplexed) turn _base_subprocess_env left BH_RUNTIME_DIR unset, so every hermes -p CLI, cron and one-shot process of one OS user shared <harness home>/runtime/bu-default.* and the harness reused whichever live daemon it found there: one profile's browser_exec drove the browser (real profile, logins) another profile's daemon was attached to. The harness runtime/tmp dirs are now namespaced by hermes_home_key() always (served profiles already got exactly this key). That always sets BH_RUNTIME_DIR, so the deep-HOME default-socket fallback (fit_default_socket_dir) is gone: socket_fit_runtime_dir already moves an over-budget profile runtime dir to a private short dir. Fixes #149829
144 lines
6.5 KiB
Python
144 lines
6.5 KiB
Python
"""browser_exec outside a served profile: each profile's own harness daemon socket binds under a deep HOME.
|
|
|
|
browser-harness binds ``<home>/.config/browser-harness/runtime/bu-<BU_NAME>.sock`` unless
|
|
``BH_RUNTIME_DIR`` says otherwise. A container's profile home (``$HERMES_HOME/home``) or a long
|
|
account home overflowed sun_path and every call died with ``AF_UNIX path too long``.
|
|
"""
|
|
import json
|
|
import os
|
|
import socket
|
|
import stat
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
import tools.browser_use_cli as bu_cli
|
|
from tools import browser_harness_dirs as harness_dirs
|
|
from tools.browser_harness_dirs import claim_private_dir as _REAL_CLAIM # conftest stubs it per test
|
|
from tools import browser_session as bs
|
|
|
|
|
|
@pytest.fixture
|
|
def harness_env(tmp_path, monkeypatch):
|
|
"""A fake harness CLI that echoes the env it was launched with; returns a driver."""
|
|
for key in ("BU_NAME", "BH_HOME", "BROWSER_HARNESS_HOME", "XDG_CONFIG_HOME", "BH_RUNTIME_DIR",
|
|
"BH_TMP_DIR", "BROWSER_USE_API_KEY", "BROWSER_CDP_URL"):
|
|
monkeypatch.delenv(key, raising=False)
|
|
monkeypatch.setattr(bs, "get_session_info", lambda key: {
|
|
"cdp_url": "ws://127.0.0.1:47000/devtools/browser/x", "features": {"local": True}})
|
|
from tools import browser_supervisor
|
|
|
|
class _Registry:
|
|
def get_or_start(self, *a, **kw):
|
|
return None
|
|
|
|
def get(self, task_id):
|
|
return None
|
|
|
|
monkeypatch.setattr(browser_supervisor, "SUPERVISOR_REGISTRY", _Registry())
|
|
monkeypatch.setattr("hermes_cli.config_effective.load_user_config_effective", lambda: {"browser": {}})
|
|
monkeypatch.setattr("tools.browser_session_cdp.resolve_cdp_override", lambda url: url)
|
|
cli = tmp_path / "browser-harness"
|
|
cli.write_text('#!/bin/sh\ncat > /dev/null\necho "{\\"home\\": \\"$HOME\\", \\"runtime\\": \\"$BH_RUNTIME_DIR\\", '
|
|
'\\"shared\\": \\"$BH_RUNTIME_DIR_SHARED\\", \\"name\\": \\"${BU_NAME:-default}\\"}"\n',
|
|
encoding="utf-8")
|
|
cli.chmod(cli.stat().st_mode | stat.S_IXUSR)
|
|
monkeypatch.setattr(bu_cli, "_find_cli", lambda: [str(cli)])
|
|
claimed = [] # conftest keeps unit tests out of the shared temp root; record the claim instead
|
|
monkeypatch.setattr(harness_dirs, "claim_private_dir", lambda path: claimed.append(path) or True)
|
|
|
|
def drive(home: str, session: str = "") -> tuple[dict, list]:
|
|
monkeypatch.setenv("HOME", home)
|
|
result = json.loads(bu_cli.browser_exec("print(1)", session=session, task_id="t-sock"))
|
|
assert result["success"] is True, result
|
|
return json.loads(result["output"]), claimed
|
|
|
|
return drive
|
|
|
|
|
|
def _bound_socket(seen: dict) -> str:
|
|
"""The socket browser_harness/_ipc.py binds for that env: ``bu-<NAME>.sock`` under the resolved
|
|
BH_RUNTIME_DIR (shared stems), else under ``<home>/.config/browser-harness/runtime``."""
|
|
runtime = seen["runtime"] or f"{seen['home']}/.config/browser-harness/runtime"
|
|
return f"{Path(runtime).resolve()}/bu-{seen['name']}.sock"
|
|
|
|
|
|
def _kernel_accepts(path: str) -> bool:
|
|
"""An unbound dir answers ENOENT; an overlong path answers "AF_UNIX path too long" first."""
|
|
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:
|
|
os.unlink(path)
|
|
return True
|
|
finally:
|
|
s.close()
|
|
|
|
|
|
@pytest.mark.platforms("linux")
|
|
def test_a_deep_home_gets_a_daemon_socket_the_kernel_accepts(harness_env, tmp_path):
|
|
deep_home = str(tmp_path / "profiles" / ("research-assistant-" * 3) / "home")
|
|
seen, claimed = harness_env(deep_home)
|
|
assert seen["shared"] == "1" and claimed == [seen["runtime"]] # keeps bu-<name> stems, private dir
|
|
assert _kernel_accepts(_bound_socket(seen)), _bound_socket(seen)
|
|
|
|
|
|
@pytest.mark.platforms("linux")
|
|
def test_a_64_char_session_under_an_ordinary_home_gets_a_socket_the_kernel_accepts(harness_env):
|
|
seen, _claimed = harness_env("/home/alexander.smith", session="n" * 64)
|
|
assert seen["runtime"] and _kernel_accepts(_bound_socket(seen)), _bound_socket(seen)
|
|
|
|
|
|
def test_each_cli_profile_drives_its_own_harness_daemon(tmp_path, monkeypatch):
|
|
"""#149829: outside a served turn two ``hermes -p`` profiles resolved the same
|
|
``<harness home>/runtime/bu-default.pid``, so one profile's browser_exec drove the daemon the other
|
|
left attached to its (real-profile, logged-in) browser."""
|
|
monkeypatch.setattr(bs, "build_browser_env", lambda: {"HOME": str(tmp_path / "u")})
|
|
seen = {}
|
|
for name in ("work", "personal"):
|
|
home = tmp_path / "profiles" / name
|
|
home.mkdir(parents=True)
|
|
monkeypatch.setenv("HERMES_HOME", str(home))
|
|
env = bu_cli._base_subprocess_env()
|
|
seen[name] = (harness_dirs.daemon_identity(env), harness_dirs.harness_pid_file(env),
|
|
env.get("BH_TMP_DIR"))
|
|
(work_id, work_pid, work_tmp), (personal_id, personal_pid, personal_tmp) = seen.values()
|
|
assert work_id != personal_id and work_pid != personal_pid
|
|
assert work_tmp and personal_tmp and work_tmp != personal_tmp # screenshots and daemon logs too
|
|
|
|
|
|
@pytest.mark.platforms("posix")
|
|
def test_runtime_root_is_the_users_private_xdg_dir_else_none(tmp_path, monkeypatch):
|
|
monkeypatch.setattr(harness_dirs, "claim_private_dir", _REAL_CLAIM)
|
|
private, open_dir, link = tmp_path / "rt-private", tmp_path / "rt-open", tmp_path / "rt-link"
|
|
private.mkdir(mode=0o700)
|
|
open_dir.mkdir()
|
|
open_dir.chmod(0o755)
|
|
link.symlink_to(private)
|
|
for raw, expected in ((private, str(private)), (open_dir, None), (link, None),
|
|
("relative/rt", None), (tmp_path / "missing", None)):
|
|
monkeypatch.setenv("XDG_RUNTIME_DIR", str(raw))
|
|
assert harness_dirs.private_user_runtime_dir() == expected, raw
|
|
# The short dir lands under the private runtime root when one exists.
|
|
monkeypatch.setenv("XDG_RUNTIME_DIR", str(private))
|
|
short = harness_dirs._claim_short_dir("deep-home\0tag", None)
|
|
assert short and os.path.dirname(short) == str(private)
|
|
assert stat.S_IMODE(os.lstat(short).st_mode) == 0o700
|
|
|
|
|
|
@pytest.mark.platforms("posix")
|
|
def test_an_existing_short_dir_is_claimed_only_when_private_and_real(tmp_path):
|
|
claim = _REAL_CLAIM
|
|
ok, loose, target = tmp_path / "hbh-ok", tmp_path / "hbh-loose", tmp_path / "elsewhere"
|
|
ok.mkdir(mode=0o700)
|
|
loose.mkdir()
|
|
loose.chmod(0o755)
|
|
target.mkdir(mode=0o700)
|
|
(tmp_path / "hbh-link").symlink_to(target)
|
|
assert claim(str(tmp_path / "hbh-new"))
|
|
assert claim(str(ok))
|
|
assert not claim(str(loose))
|
|
assert not claim(str(tmp_path / "hbh-link"))
|