Skip to content

The agent that grows with you

fix(bot-desktop): lease no longer needs fcntl at import time

teknium11f9810aed80b
diff --git a/tests/tools/test_bot_desktop_lease.py b/tests/tools/test_bot_desktop_lease.py
index 3b9da1a2cfe..e67d664af63 100644
--- a/tests/tools/test_bot_desktop_lease.py
+++ b/tests/tools/test_bot_desktop_lease.py
@@ -122,3 +122,24 @@ def test_unreadable_lease_file_fails_closed_and_takeover_keeps_the_agents_reason
held = lease.acquire("desk-1", profile_key=home)
assert held.pending_handoff is None and held.reason == "log in to the bank, 2FA on your phone"
assert hermes_home_key(home) # sanity: the key derivation used by the bridge is available
+
+
+def test_lease_works_without_fcntl(tmp_path):
+ """Windows and fcntl-less hosts: ``computer_use`` imports the lease (via handoff) on EVERY call, so a
+ module-level fcntl dependency turns every desktop action into ModuleNotFoundError there. The file
+ semantics must still work; only the cross-process lock degrades. Subprocess so the module cache is clean."""
+ import os
+ import subprocess
+ import sys
+
+ probe = ("import sys; sys.modules['fcntl'] = None; sys.path.insert(0, %r)\n"
+ "from tools.bot_desktop import lease\n"
+ "import tools.computer_use.handoff\n"
+ "assert lease.get().holder == lease.AGENT\n"
+ "assert lease.acquire('v1').holder == lease.HUMAN\n"
+ "assert lease.get().holder == lease.HUMAN\n"
+ "assert lease.release('v1').holder == lease.AGENT\n"
+ "print('OK')\n") % os.getcwd()
+ out = subprocess.run([sys.executable, "-c", probe], capture_output=True, text=True, encoding="utf-8", timeout=60,
+ stdin=subprocess.DEVNULL, env={**os.environ, "HERMES_HOME": str(tmp_path)})
+ assert out.stdout.strip() == "OK", out.stderr
diff --git a/tools/bot_desktop/lease.py b/tools/bot_desktop/lease.py
index 2a3b84d5e5d..f890677dc38 100644
--- a/tools/bot_desktop/lease.py
+++ b/tools/bot_desktop/lease.py
@@ -14,7 +14,6 @@ an action admitted under one lease can tell that control changed underneath it.
from __future__ import annotations
-import fcntl
import json
import os
import threading
@@ -25,6 +24,11 @@ from typing import Callable, Dict, List, Optional
from hermes_constants import get_hermes_home, hermes_home_key
+try:
+ import fcntl
+except ImportError: # Windows/macOS without fcntl: computer_use imports this module on every call, and no
+ fcntl = None # multi-process Bot Desktop exists there, so the cross-process lock degrades to a no-op.
+
AGENT = "agent"
HUMAN = "human"
_POLL_SECONDS = 0.25
@@ -90,14 +94,18 @@ class _locked:
self._fh = None
def __enter__(self):
+ if fcntl is None:
+ return self
self._lockfile.parent.mkdir(parents=True, exist_ok=True)
self._fh = open(self._lockfile, "a+", encoding="utf-8") # noqa: SIM115 — closed in __exit__
fcntl.flock(self._fh.fileno(), fcntl.LOCK_EX)
return self
def __exit__(self, *exc):
- fcntl.flock(self._fh.fileno(), fcntl.LOCK_UN) # type: ignore[union-attr]
- self._fh.close() # type: ignore[union-attr]
+ if self._fh is None:
+ return
+ fcntl.flock(self._fh.fileno(), fcntl.LOCK_UN)
+ self._fh.close()
def get(profile_key: Optional[str] = None) -> Lease: