The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): refuse an unprovable foreign gateway.lock with the recovery command
Teknium4fd3300d58e8
diff --git a/gateway/runtime_ownership.py b/gateway/runtime_ownership.pyindex 2e035513590..723171a64f5 100644--- a/gateway/runtime_ownership.py+++ b/gateway/runtime_ownership.py@@ -80,7 +80,10 @@ def _unlink_stale_lock(path: Path) -> bool:try:probe = os.open(path, os.O_RDONLY | getattr(os, 'O_NOFOLLOW', 0) | os.O_NONBLOCK)except PermissionError:- return _inode_locked_per_proc(info.st_ino) is False and _unlink_if_same(path, info)+ held = _inode_locked_per_proc(info.st_ino)+ if held is None: # no /proc/locks (macOS/BSD): nothing can prove it unheld, so never guess+ raise PermissionError(foreign_lock_recovery(path)) from None+ return held is False and _unlink_if_same(path, info)import fcntltry:if not _same_inode(os.fstat(probe), info):@@ -95,6 +98,22 @@ def _unlink_stale_lock(path: Path) -> bool:os.close(probe)+def foreign_lock_recovery(path: Path) -> str:+ return (f'foreign_stale_lock: {path} is not readable by this user (left by a gateway run as root?) '+ f'and cannot be proven unheld. If `sudo lsof {path}` lists no process, run `sudo rm {path}`.')+++def unreadable_lock_recovery(path: Path) -> str | None:+ """The recovery a client prints for a ``gateway.lock`` it cannot even open read-only."""+ try:+ os.close(os.open(path, os.O_RDONLY | getattr(os, 'O_NOFOLLOW', 0) | os.O_NONBLOCK))+ except PermissionError:+ return foreign_lock_recovery(path)+ except OSError:+ pass+ return None++def _unlink_if_same(path: Path, info) -> bool:if not _same_inode(os.lstat(path), info):return Falsediff --git a/hermes_cli/gateway_runtime.py b/hermes_cli/gateway_runtime.pyindex 86a721f8e5a..99eafe1aa1f 100644--- a/hermes_cli/gateway_runtime.py+++ b/hermes_cli/gateway_runtime.py@@ -174,7 +174,12 @@ def discover_gateway_endpoint(profile_home: str | Path, *, timeout: float = 2.0)served = _served_by_multiplexer(home, timeout=timeout)if served is not None:return served- return GatewayDiscovery(missing_owner_state(home))+ state = missing_owner_state(home)+ if state == "inaccessible" and os.name != "nt":+ from gateway.runtime_ownership import unreadable_lock_recovery+ if detail := unreadable_lock_recovery(home / "gateway.lock"):+ return GatewayDiscovery(state, reason_code="foreign_stale_lock", detail=detail)+ return GatewayDiscovery(state)except TimeoutError:return GatewayDiscovery("inaccessible", reason_code="control_timeout")except DiscoveryError as exc:diff --git a/tests/gateway/test_foreign_stale_gateway_lock.py b/tests/gateway/test_foreign_stale_gateway_lock.pynew file mode 100644index 00000000000..621675ba94f--- /dev/null+++ b/tests/gateway/test_foreign_stale_gateway_lock.py@@ -0,0 +1,36 @@+"""A gateway.lock this user cannot read and cannot prove unheld (a root run on macOS, which has no+/proc/locks) is refused with a terminal reason and the exact recovery command, never guessed at."""+import os++import pytest++pytestmark = [pytest.mark.platforms("linux", "macos"),+ pytest.mark.skipif(hasattr(os, 'geteuid') and os.geteuid() == 0, reason="root reads mode 000")]+++def _unreadable_lock(tmp_path):+ home = tmp_path / 'home'+ home.mkdir(mode=0o700)+ lock = home / 'gateway.lock'+ lock.write_text('{"pid": 1}', encoding='utf-8')+ lock.chmod(0)+ return home, lock+++def test_reserve_without_proc_locks_refuses_with_recovery_command(tmp_path, monkeypatch):+ from gateway import runtime_ownership+ home, lock = _unreadable_lock(tmp_path)+ monkeypatch.setattr(runtime_ownership, '_inode_locked_per_proc', lambda ino: None) # macOS: no table+ inode = lock.stat().st_ino+ with pytest.raises(PermissionError) as raised:+ runtime_ownership.ProfileOwnership().reserve([home])+ assert 'foreign_stale_lock' in str(raised.value) and f'sudo rm {lock}' in str(raised.value)+ assert lock.stat().st_ino == inode+++def test_discovery_names_the_unreadable_lock_and_its_recovery(tmp_path):+ from hermes_cli.gateway_runtime import discover_gateway_endpoint+ home, lock = _unreadable_lock(tmp_path)+ result = discover_gateway_endpoint(home, timeout=1.0)+ assert (result.state, result.reason_code) == ('inaccessible', 'foreign_stale_lock')+ assert f'sudo lsof {lock}' in result.detail