Skip to content

The agent that grows with you

fix(bot-mode): review follow-ups for the turn lock

kshitijc460e87d10de
diff --git a/tests/tools/test_bot_turn_lock.py b/tests/tools/test_bot_turn_lock.py
index 0f55a8beddb..6332d3eaf4d 100644
--- a/tests/tools/test_bot_turn_lock.py
+++ b/tests/tools/test_bot_turn_lock.py
@@ -12,6 +12,7 @@ from __future__ import annotations
import fcntl
import json
import os
+import re
import threading
import time
@@ -74,7 +75,7 @@ def test_timeout_is_structured_target_busy(root):
assert err.profile == "ops"
assert err.waited_seconds >= 0.3
assert "target_busy" in str(err)
- assert "0s" in str(err) or "1s" in str(err) # rough wait duration surfaced
+ assert re.search(r"~\d+s", str(err)) # rough wait duration surfaced
finally:
release.set()
t.join(timeout=5)
@@ -90,9 +91,11 @@ def test_different_profiles_do_not_contend(root):
assert held.wait(timeout=5)
try:
start = time.monotonic()
- with acquire_turn_lock(root, "scout", timeout_seconds=2):
+ with acquire_turn_lock(root, "scout", timeout_seconds=5):
pass
- assert time.monotonic() - start < 1.0
+ # Upper bound generous for loaded CI runners — the point is only
+ # that 'scout' never waited the busy 'ops' budget out.
+ assert time.monotonic() - start < 2.5
finally:
release.set()
t.join(timeout=5)
@@ -240,6 +243,19 @@ def test_peer_stdin_delivery_skips_local_lock(root, tmp_path, monkeypatch):
# ── wiring: relay deliver RPC (tui_gateway/methods_bot_relay.py) ─────────────
+def test_local_delivery_command_never_reenters_the_lock():
+ """The gateway deliver handler runs local_delivery_command ALREADY holding
+ the profile lock. That argv must stay a raw `hermes -p … chat` invocation:
+ routing it through the --run-delivery wrapper would make the child hit
+ _delivery_lock (argv[0]=='hermes' and argv[1]=='-p'), burn the full wait
+ budget against its parent's flock, and fail every relay delivery with
+ target_busy."""
+ argv = bot_relay.local_delivery_command("ops", "/tmp/q.txt")
+ assert argv[:3] == ["hermes", "-p", "ops"]
+ assert "--run-delivery" not in argv
+ assert not any("bot_mode_dm" in part for part in argv)
+
+
def test_relay_deliver_returns_target_busy_error(tmp_path, monkeypatch):
import tui_gateway.server as srv
diff --git a/tools/bot_relay.py b/tools/bot_relay.py
index 25e6104951f..04b023eb16e 100644
--- a/tools/bot_relay.py
+++ b/tools/bot_relay.py
@@ -588,15 +588,17 @@ def acquire_turn_lock(
"""Hold ``profile``'s cross-process turn lock for the ``with`` body.
Non-blocking flock probe + short-sleep retry loop up to the budget
- (``bot_mode.turn_wait_seconds`` unless ``timeout_seconds`` is given), so
- waiters acquire roughly in arrival order without ever deadlocking.
- Raises :class:`TurnBusyError` when the budget is exhausted. On platforms
- without ``fcntl`` (Windows) the lock degrades to a no-op — those
- installs never had this race path in production.
+ (``bot_mode.turn_wait_seconds`` unless ``timeout_seconds`` is given).
+ No ordering guarantee among waiters — whichever probe lands first after
+ release wins — but every waiter is bounded by the budget, so no
+ deadlock. Raises :class:`TurnBusyError` when the budget is exhausted.
+ On platforms without ``fcntl`` (Windows) the lock degrades to a no-op —
+ those installs never had this race path in production.
"""
try:
import fcntl
except ImportError: # pragma: no cover — Windows
+ logger.debug("bot turn lock disabled: fcntl unavailable on this platform")
yield turn_lock_path(root, profile)
return
diff --git a/tui_gateway/methods_bot_relay.py b/tui_gateway/methods_bot_relay.py
index 68fb77961ea..a3cf7b86058 100644
--- a/tui_gateway/methods_bot_relay.py
+++ b/tui_gateway/methods_bot_relay.py
@@ -115,7 +115,10 @@ def _(rid, params: dict) -> dict:
f.write(message)
# Per-profile turn lock (#93091): serialize with any other
# delivery turn into this profile (relay or local message_agent).
- # The lock covers only the turn execution window.
+ # The lock covers only the turn execution window. Worst-case
+ # handler hold is lock wait (bot_mode.turn_wait_seconds, default
+ # 120s) + the 600s turn timeout below — clients calling
+ # bot_relay.deliver must tolerate ~720s before assuming failure.
with acquire_turn_lock(root, resolved):
proc = subprocess.run(
local_delivery_command(resolved, tmp),