Skip to content

The agent that grows with you

fix(gateway): don't refuse /reasoning when another viewer's mutation moved the fence

Teknium9e53841f0182
diff --git a/gateway/session_setting_commands.py b/gateway/session_setting_commands.py
index 6aa937ac385..21425551d61 100644
--- a/gateway/session_setting_commands.py
+++ b/gateway/session_setting_commands.py
@@ -68,6 +68,9 @@ def local_authority_live(runner, source):
return None, None
+_FENCE_ATTEMPTS = 3
+
+
async def commit_reasoning_command(authority, actor, ref, value, persist_global):
"""``/reasoning <level>`` in either scope, or ``reset``, on a LOCAL session (``config.set`` and
``slash.exec``): the owner's fenced ``reasoning`` mutation, before the handler replies or writes
@@ -91,18 +94,27 @@ async def commit_reasoning_command(authority, actor, ref, value, persist_global)
live = authority.sessions[ref.session_id]
if live.source.platform != Platform.LOCAL:
return
- row = await asyncio.to_thread(authority.db.get_session, ref.session_id)
- if row is None:
- raise RuntimeStoreError('not_found')
- try:
- await mutate_session(authority, actor, ref, {
- 'session_id': ref.session_id, 'request_id': 'reasoning-command:' + uuid.uuid4().hex,
- 'expected_revision': row['runtime_revision'], 'expected_generation': row['runtime_generation'],
- 'operation': 'reasoning', 'payload': {'effort': effort}})
- except (RuntimeStoreError, *MUTATION_GUARD_REFUSALS) as exc:
- if getattr(exc, 'reason', 'session_busy') not in ('session_busy', 'unknown_execution'):
- raise
- live.pending_reasoning = (actor.subject, effort)
+ # The fences are read here, not sent by a client: another viewer's mutation landing in between
+ # (``revision_conflict``) is no verdict on this command, so it re-reads them. A fence that keeps
+ # moving defers to the turn boundary like a busy session, as main always applied the command.
+ for _attempt in range(_FENCE_ATTEMPTS):
+ row = await asyncio.to_thread(authority.db.get_session, ref.session_id)
+ if row is None:
+ raise RuntimeStoreError('not_found')
+ try:
+ await mutate_session(authority, actor, ref, {
+ 'session_id': ref.session_id, 'request_id': 'reasoning-command:' + uuid.uuid4().hex,
+ 'expected_revision': row['runtime_revision'], 'expected_generation': row['runtime_generation'],
+ 'operation': 'reasoning', 'payload': {'effort': effort}})
+ return
+ except (RuntimeStoreError, *MUTATION_GUARD_REFUSALS) as exc:
+ reason = getattr(exc, 'reason', 'session_busy')
+ if reason == 'revision_conflict':
+ continue
+ if reason not in ('session_busy', 'unknown_execution'):
+ raise
+ break
+ live.pending_reasoning = (actor.subject, effort)
async def apply_pending_reasoning(authority, ref, live):
diff --git a/tests/gateway/test_session_mutation_model_busy.py b/tests/gateway/test_session_mutation_model_busy.py
index b30cb6353f7..2e4514d8732 100644
--- a/tests/gateway/test_session_mutation_model_busy.py
+++ b/tests/gateway/test_session_mutation_model_busy.py
@@ -462,3 +462,42 @@ async def test_a_reasoning_command_during_a_running_turn_is_committed_for_the_ne
release.set()
await owner.close()
store.close_all_db_handles()
+
+
[email protected]('conflicts', [1, 99], ids=['moved-once', 'keeps-moving'])
+async def test_a_reasoning_command_raced_by_another_mutation_is_not_refused(tmp_path, monkeypatch, conflicts):
+ """Review of #140969: ``/reasoning high`` on an idle session whose fence another viewer's mutation just
+ moved (``revision_conflict``) failed the command, where main always applied it. The fence moving is not
+ a verdict on this command: it re-reads the fences and commits; a fence that keeps moving defers it to the
+ next boundary like a busy session."""
+ from gateway import session_mutations
+ from gateway.session_policy import policy_for_source
+ from hermes_state_runtime import RuntimeStoreError
+
+ async def handle(event):
+ return 'ok'
+ monkeypatch.chdir(tmp_path)
+ store, _db, authority, owner, ref = _owner(tmp_path, monkeypatch, handle)
+ authority.runner._set_session_reasoning_override = lambda *a: None
+ authority.runner._apply_reasoning_selection = lambda *a, **k: 'ok'
+ real, raced = session_mutations.mutate_session, []
+
+ async def racing(authority_, actor, ref_, params, *a, **k):
+ if params.get('operation') == 'reasoning' and len(raced) < conflicts:
+ raced.append(params['expected_revision'])
+ raise RuntimeStoreError('revision_conflict')
+ return await real(authority_, actor, ref_, params, *a, **k)
+ monkeypatch.setattr(session_mutations, 'mutate_session', racing)
+ try:
+ reply = await owner.dispatch({'id': 1, 'method': 'config.set', 'params': {
+ 'session_id': ref.session_id, 'key': 'reasoning', 'value': 'high', 'scope': 'session'}})
+ assert reply.get('result', {}).get('value') == 'high', reply
+ live = authority.sessions[ref.session_id]
+ if conflicts == 1:
+ assert (policy_for_source(authority.runner, live.source).reasoning_config or {}).get('effort') == 'high'
+ else:
+ assert live.pending_reasoning == (owner.actor.subject, 'high')
+ finally:
+ await owner.close()
+ store.close_all_db_handles()