Skip to content

The agent that grows with you

fix: WS session.mutate answers live write-guard refusals as session_busy

Teknium4309ccf5f55c
diff --git a/gateway/session_controls.py b/gateway/session_controls.py
index 5e1c01b9d63..919b438cd82 100644
--- a/gateway/session_controls.py
+++ b/gateway/session_controls.py
@@ -382,7 +382,13 @@ class AuthorityConnection:
async def mutate(self, ref, params):
from gateway.session_mutations import mutate_session
- result = await mutate_session(self.authority, self.actor, ref, params)
+ from hermes_state_mutation_guards import MUTATION_GUARD_REFUSALS
+ try:
+ result = await mutate_session(self.authority, self.actor, ref, params)
+ except MUTATION_GUARD_REFUSALS as exc:
+ # A live turn lease / compression lock is the same retryable busy verdict a
+ # running admission gets (REST maps it to 409), not an internal error.
+ raise RuntimeStoreError('session_busy') from exc
# The branching viewer navigates straight into its new child; attach it
# here (create parity) so the first submit is not refused as a stranger.
child = result.get('branched_session_id') if isinstance(result, dict) else None
diff --git a/tests/gateway/test_session_mutation_rpc.py b/tests/gateway/test_session_mutation_rpc.py
index 40f694f6dd6..0c77ee4f6a7 100644
--- a/tests/gateway/test_session_mutation_rpc.py
+++ b/tests/gateway/test_session_mutation_rpc.py
@@ -63,3 +63,31 @@ async def test_pending_fanout_carries_the_revision_a_later_mutation_must_present
authority._publish_pending(SessionRef('owned', 's'))
assert published and published[-1][0] == 'session.info'
assert published[-1][1]['revision'] == db.get_session('s')['runtime_revision']
+
+
[email protected]('guard', ['turn_lease', 'compression_lock'])
+async def test_ws_mutation_refused_by_live_write_guard_is_session_busy(tmp_path, guard):
+ """The WS path answers a live turn lease / compression lock like REST's 409: a retryable
+ session_busy error frame with the row intact, never an exception that becomes an internal error."""
+ with SessionDB(db_path=tmp_path / 'state.db') as db:
+ db.create_session('s', source='test')
+ epoch = begin_runtime_epoch(db, instance_id='current')
+ authority = SessionAuthority(SimpleNamespace(_draining=False), profile_id='owned',
+ instance_id='current', db=db, epoch=epoch)
+ authority.sessions['s'] = LiveSession(None, 'route')
+ if guard == 'turn_lease':
+ assert db.try_acquire_session_turn_lease('s', 'foreign-turn')
+ else:
+ assert db.try_acquire_compression_lock('s', 'foreign-compressor')
+ owner = AuthorityConnection(authority, object(), {'user_id': 'owner'})
+ try:
+ refused = await owner.dispatch({'id': 7, 'method': 'session.mutate', 'params': {
+ 'session_id': 's', 'request_id': 'delete-1',
+ 'expected_revision': db.get_session('s')['runtime_revision'],
+ 'expected_generation': db.get_session('s')['runtime_generation'],
+ 'operation': 'delete', 'payload': {}}})
+ assert refused['error']['message'] == 'session_busy', refused
+ assert db.get_session('s') is not None
+ finally:
+ await owner.close()