The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(cli): skip Unix sockets and other special files during profile export
Camil Blanaru9a82c44e38fe
diff --git a/hermes_cli/profiles.py b/hermes_cli/profiles.pyindex d3ad434d594..8f8d7729d28 100644--- a/hermes_cli/profiles.py+++ b/hermes_cli/profiles.py@@ -1518,6 +1518,27 @@ def get_profile_export_path(name: str, *, timestamp: Optional[str] = None) -> Pareturn export_dir / f"{canon}-{stamp}.tar.gz"+def _non_exportable_entries(directory: str, contents: list) -> set:+ """Entries under *directory* that must never reach an export archive: ``__pycache__``,+ ``*.sock``/``*.tmp`` names, and anything that is not a regular file, directory, or symlink.+ :func:`shutil.copytree` cannot copy special files, so a single live Unix socket without a+ ``.sock`` name (or a FIFO, or a device node) would abort the whole export with+ ``[Errno 6] No such device or address``. Symlinks survive — copytree recreates them."""+ ignored: set = set()+ for entry in contents:+ if entry == "__pycache__" or entry.endswith((".sock", ".tmp")):+ ignored.add(entry)+ continue+ try:+ mode = os.lstat(os.path.join(directory, entry)).st_mode+ except OSError:+ ignored.add(entry) # vanished mid-walk — copytree would fail on it anyway+ continue+ if not (stat.S_ISREG(mode) or stat.S_ISDIR(mode) or stat.S_ISLNK(mode)):+ ignored.add(entry)+ return ignored++def _default_export_ignore(root_dir: Path):"""copytree ignore for the default-profile export: root-level allow-list(``_DEFAULT_EXPORT_INCLUDE_ROOT``) plus universal exclusions. Surviving text files are@@ -1527,17 +1548,14 @@ def _default_export_ignore(root_dir: Path):survive. Everything else (such as an unrelated ``x11-dev/`` directory in a Docker deployment whereHERMES_HOME equals the cwd) is excluded. Blacklisting was tried first and proved unable to anticipateevery non-Hermes file the user may have lying alongside HERMES_HOME (#58394). * **Universal exclusions- at any depth** — ``__pycache__``, sockets, temp files; plus npm lockfiles, which may appear at the root.+ at any depth** — ``__pycache__``, sockets and other special files, temp files+ (:func:`_non_exportable_entries`); plus npm lockfiles, which may appear at the root."""def _ignore(directory: str, contents: list) -> set:# Universal exclusions (any depth) plus npm lockfiles that can appear at root.- ignored: set = {- entry for entry in contents- if entry == "__pycache__"- or entry.endswith((".sock", ".tmp"))- or entry in {"package.json", "package-lock.json"}- }+ ignored = _non_exportable_entries(directory, contents)+ ignored.update({"package.json", "package-lock.json"} & set(contents))if Path(directory) == root_dir:ignored.update(entry for entry in contents if entry not in _DEFAULT_EXPORT_INCLUDE_ROOT)return ignored@@ -1602,9 +1620,14 @@ def export_profile(name: str, output_path: str, extra_files: Optional[Dict[str,# The default profile IS ~/.hermes (dir name ".hermes"), so both paths stage a filtered# copy under a temp dir named after the canonical id: root allow-list for default,- # credential exclusion for named profiles.+ # credential exclusion for named profiles. The universal exclusions apply to both:+ # profiles accumulate Unix sockets in normal operation (e.g. an agent-browser control+ # socket under ``home/.agent-browser/``), and a single one aborts copytree — and with+ # it the whole export.def _ignore_credentials(directory: str, contents: list) -> set:- return _EXPORT_CREDENTIAL_FILES & set(contents)+ ignored = _non_exportable_entries(directory, contents)+ ignored.update(_EXPORT_CREDENTIAL_FILES & set(contents))+ return ignoredignore = _default_export_ignore(profile_dir) if canon == "default" else _ignore_credentialswith tempfile.TemporaryDirectory() as tmpdir:diff --git a/tests/hermes_cli/test_profile_export_special_files.py b/tests/hermes_cli/test_profile_export_special_files.pynew file mode 100644index 00000000000..524c117c158--- /dev/null+++ b/tests/hermes_cli/test_profile_export_special_files.py@@ -0,0 +1,97 @@+"""Tests for special-file handling during profile export.++:func:`shutil.copytree` cannot copy Unix sockets, FIFOs, or device nodes —+it collects "[Errno 6] No such device or address" and raises+``shutil.Error`` at the end of the walk. Profiles accumulate such files in+normal operation (e.g. a stale agent-browser control socket under+``home/.agent-browser/``), so exports must skip them instead of failing+the whole archive.+"""++import os+import socket+import sys+import tarfile++import pytest++from hermes_cli.profiles import export_profile++pytestmark = pytest.mark.skipif(+ sys.platform == "win32",+ reason="Unix sockets and FIFOs are not available on Windows",+)+++def _patch_named_profile(monkeypatch, profiles_root, profile_dir):+ monkeypatch.setattr("hermes_cli.profiles._get_profiles_root", lambda: profiles_root)+ monkeypatch.setattr("hermes_cli.profiles.get_profile_dir", lambda n: profile_dir)+ monkeypatch.setattr("hermes_cli.profiles.validate_profile_name", lambda n: None)+++def _bind_unix_socket(path):+ """Bind an AF_UNIX socket at *path*.++ Binds via a cwd-relative name so pytest's long tmp paths stay under the+ ~104-byte ``sun_path`` limit on macOS. The filesystem entry outlives the+ close — exactly the stale-socket state a real profile ends up with.+ """+ sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)+ cwd = os.getcwd()+ os.chdir(path.parent)+ try:+ sock.bind(path.name)+ finally:+ os.chdir(cwd)+ sock.close()+++def test_named_profile_export_survives_unix_socket(tmp_path, monkeypatch):+ """Sockets and FIFOs in a named profile are skipped, not fatal."""+ profiles_root = tmp_path / "profiles"+ profile_dir = profiles_root / "sockety"+ browser_dir = profile_dir / "home" / ".agent-browser"+ browser_dir.mkdir(parents=True)++ (profile_dir / "config.yaml").write_text("model: gpt-4\n")+ (browser_dir / "state.json").write_text("{}\n")++ # One socket the *.sock suffix rule catches, one only lstat can catch,+ # and a FIFO — none of them may fail or enter the export.+ _bind_unix_socket(browser_dir / "dev-12345.sock")+ _bind_unix_socket(browser_dir / "control")+ os.mkfifo(browser_dir / "events")++ _patch_named_profile(monkeypatch, profiles_root, profile_dir)++ result = export_profile("sockety", str(tmp_path / "sockety.tar.gz"))++ with tarfile.open(result, "r:gz") as tf:+ names = tf.getnames()++ assert any("config.yaml" in n for n in names)+ assert any("state.json" in n for n in names)+ assert not any(".sock" in n for n in names)+ assert not any(n.endswith(("control", "events")) for n in names)+++def test_default_profile_export_survives_unix_socket(tmp_path, monkeypatch):+ """The default-profile export skips suffixless sockets in allowed dirs."""+ profile_dir = tmp_path / "hermes_home"+ sessions_dir = profile_dir / "sessions"+ sessions_dir.mkdir(parents=True)++ (profile_dir / "config.yaml").write_text("model: gpt-4\n")+ (sessions_dir / "log.jsonl").write_text("{}\n")+ _bind_unix_socket(sessions_dir / "ipc")++ _patch_named_profile(monkeypatch, tmp_path / "profiles", profile_dir)++ result = export_profile("default", str(tmp_path / "default.tar.gz"))++ with tarfile.open(result, "r:gz") as tf:+ names = tf.getnames()++ assert any("config.yaml" in n for n in names)+ assert any("log.jsonl" in n for n in names)+ assert not any(n.endswith("ipc") for n in names)