Skip to content

The agent that grows with you

fix(agent): do not interrupt a turn when lease refresh hits a SQLite lock

686f6c61bae3a0101212
diff --git a/agent/turn_facade_lease.py b/agent/turn_facade_lease.py
index 3347f05f382..68cee326e4f 100644
--- a/agent/turn_facade_lease.py
+++ b/agent/turn_facade_lease.py
@@ -182,8 +182,9 @@ class DurableTurnLease:
_set_interrupt(False, agent._execution_thread_id)
def refresh_tick(self):
- """One periodic renewal (every ``refresh_interval`` via the shared scheduler); a miss or
- error interrupts the turn. Returning False stops the timer.
+ """One periodic renewal (every ``refresh_interval`` via the shared scheduler). A real
+ miss (rowcount 0) or a non-lock error interrupts the turn. A SQLite lock is only a
+ missed tick: the timer stays armed. Returning False stops the timer.
The holder-qualified UPDATE fences a late refresher from a successor lease. The façade's
finally sets ``stop`` before releasing, so a holder-fenced miss observed after stop is not
@@ -201,9 +202,20 @@ class DurableTurnLease:
"Lost session turn lease while turn is active: %s", self._current_session_id()
)
self._interrupt_turn("Session turn lease lost; stopping to protect the transcript.")
- except Exception:
+ except Exception as exc:
if self.stop.is_set():
return False
+ # A lock is contention, not a lost holder. The row is still ours
+ # (release is holder-qualified), so a convoy must not kill the turn.
+ # Keep the timer: the next tick retries, and one miss fits inside the TTL.
+ from hermes_state_errors import is_sqlite_lock_error
+
+ if is_sqlite_lock_error(exc):
+ logger.warning(
+ "Session turn lease refresh hit a SQLite lock; will retry: %s",
+ self._current_session_id(),
+ )
+ return None
logger.warning(
"Failed to refresh session turn lease: %s", self._current_session_id(), exc_info=True,
)
diff --git a/tests/agent/test_turn_facade_lease.py b/tests/agent/test_turn_facade_lease.py
index 29df52d2c3a..99f5664c9f2 100644
--- a/tests/agent/test_turn_facade_lease.py
+++ b/tests/agent/test_turn_facade_lease.py
@@ -1,4 +1,5 @@
"""Unit tests for agent.turn_facade_lease (admission + lease bracket)."""
+import sqlite3
import threading
from types import SimpleNamespace
@@ -119,6 +120,55 @@ def test_timeout_and_interrupt_early_results():
assert admission.early_result["interrupt_message"] == "stop"
+def _active_lease(db):
+ agent = _agent(db)
+ calls = []
+ agent.interrupt = lambda msg, **kw: calls.append(msg)
+ lease = DurableTurnLease(agent, db, "s1", "h")
+ lease.turn_active = True
+ return lease, calls
+
+
+def test_refresh_tick_sqlite_lock_keeps_the_turn():
+ db = _Db()
+
+ def locked(session_id, holder, **kwargs):
+ raise sqlite3.OperationalError("database is locked")
+
+ db.refresh_session_turn_lease = locked
+ lease, calls = _active_lease(db)
+
+ # False would cancel the refresher; a lock is a missed tick, not a lost lease.
+ assert lease.refresh_tick() is None
+ assert calls == []
+ assert lease.interrupt_message is None
+ assert lease.stop.is_set() is False
+
+
+def test_refresh_tick_real_loss_still_interrupts():
+ db = _Db()
+ db.refresh_session_turn_lease = lambda session_id, holder, **kwargs: False
+ lease, calls = _active_lease(db)
+
+ assert lease.refresh_tick() is False
+ assert calls == ["Session turn lease lost; stopping to protect the transcript."]
+
+
+def test_refresh_tick_non_lock_error_still_interrupts():
+ db = _Db()
+
+ def broken(session_id, holder, **kwargs):
+ raise sqlite3.OperationalError("disk I/O error")
+
+ db.refresh_session_turn_lease = broken
+ lease, calls = _active_lease(db)
+
+ assert lease.refresh_tick() is False
+ assert calls == [
+ "Session turn lease could not be refreshed; stopping to protect the transcript."
+ ]
+
+
def test_interrupt_turn_only_while_active():
agent = _agent(_Db())
calls = []