The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): a refused deferred /model is no pick when an open adopts a frozen Bot Chat
David Dudok de Wita1370c6f5d2d
diff --git a/hermes_state_mutation_prepared.py b/hermes_state_mutation_prepared.pyindex 68c9352c6a9..bde37bdc418 100644--- a/hermes_state_mutation_prepared.py+++ b/hermes_state_mutation_prepared.py@@ -95,12 +95,15 @@ _RECEIPTS = "FROM state_meta WHERE key GLOB 'gateway.mutation.v1.*' AND json_valdef picks_on(conn, session_id, before=None):"""The policy edits (``model``, ``reasoning``, ``workspace``) a ``session.mutate`` committed for *session_id* (belowrevision *before*): their receipts (kept for exact retries, also past deletion) are the durable record of- the user's pick. A branch copied its parent's policy, picks included, under its own id: its local- receipt's ``picks``; a branch stored before that field has its parent's as they stood at its branch."""+ the user's pick. A deferred ``/model`` (``status: deferred``) is only a request until a turn boundary+ applies it under a receipt of its own, so it is no pick. A branch copied its parent's policy, picks+ included, under its own id: its local receipt's ``picks``; a branch stored before that field has its+ parent's as they stood at its branch."""bound = '' if before is None else " AND json_extract(value, '$.result.revision')<?"picked = {row[0] for row in conn.execute("SELECT DISTINCT json_extract(value, '$.result.operation') " + _RECEIPTS +"AND json_extract(value, '$.result.operation') IN ('model', 'reasoning', 'workspace') "+ "AND COALESCE(json_extract(value, '$.result.status'), '') != 'deferred' ""AND json_extract(value, '$.result.session_id')=?" + bound, (session_id, *(() if before is None else (before,))))}saved = conn.execute('SELECT value FROM state_meta WHERE key=?', (POLICY_PREFIX + session_id,)).fetchone()if saved is None:diff --git a/tests/gateway/test_bot_chat_follows_profile.py b/tests/gateway/test_bot_chat_follows_profile.pyindex fd159c04b36..bbc240e33d9 100644--- a/tests/gateway/test_bot_chat_follows_profile.py+++ b/tests/gateway/test_bot_chat_follows_profile.py@@ -673,3 +673,53 @@ async def test_an_mcp_reload_keeps_a_busy_first_opens_adoption_pending(home, monawait desktop.close()await owner.close()store.close_all_db_handles()+++async def test_a_deferred_model_pick_that_was_refused_is_no_pick_when_an_open_adopts_the_chat(home, monkeypatch):+ """A ``/model`` sent while a turn ran is deferred to the turn boundary, where it no longer resolved and+ was dropped. Its receipt stays (exact retries), but no selection was committed: the open follows the+ profile's current model."""+ import asyncio+ from gateway.run_turn_prepare import GatewayTurnPrepareMixin+ from gateway.session_contract import Submission+ from hermes_cli import model_switch+ from tests.waiting import async_wait_until+ monkeypatch.setattr(model_switch, 'switch_model', lambda **k: model_switch.ModelSwitchResult(+ success=False, new_model='', target_provider=''))+ _profile(home, 'old-model', 1, 'profile-key')+ store, runner, authority, owner = _owner(home)+ runner._peek_session_state = lambda key: None+ runner._set_session_reasoning_override = lambda route, value: None+ runner._resolve_session_agent_runtime = (+ lambda **k: GatewayTurnPrepareMixin._resolve_session_agent_runtime(runner, **k))+ entered, release, published = asyncio.Event(), asyncio.Event(), []++ async def turn(event):+ entered.set()+ await release.wait()+ return 'ok'+ runner._handle_message = turn+ try:+ ref, _params = _frozen_bot_chat(authority, owner.actor, home)+ live = authority.sessions[ref.session_id]+ publish = live.event_stream.publish+ monkeypatch.setattr(live.event_stream, 'publish', lambda sid, payload, **kw: (+ published.append(payload), publish(sid, payload, **kw))[1])+ await authority.submit(owner.actor, Submission('running', ref, {'text': 'RUNNING'}, 'queue'))+ await asyncio.wait_for(entered.wait(), 10)+ snap = authority.db.get_session(ref.session_id)+ response = await owner.dispatch({'id': 'pick', 'method': 'session.mutate', 'params': dict(+ session_id=ref.session_id, request_id='pick', operation='model', payload={'model': 'gone-model'},+ expected_revision=snap['runtime_revision'], expected_generation=snap['runtime_generation'])})+ assert response['result']['status'] == 'deferred', response+ release.set()+ await async_wait_until(lambda: any(p.get('status') == 'refused' for p in published), timeout=10,+ what='the deferred pick to be refused at the turn boundary')+ _profile(home, 'new-model', 2, 'new-key')+ await _open(authority, ref)+ assert _follows(runner, live) and _route(runner, live) == ('new-model', 'http://127.0.0.1:2/v1', 'new-key')+ finally:+ release.set()+ await owner.close()+ store.close_all_db_handles()