Skip to content

The agent that grows with you

fix(gateway): a direct reasoning commit supersedes the deferred level

unsupportedpastelsa2657c345ca3
diff --git a/gateway/session_deferred_pick.py b/gateway/session_deferred_pick.py
index f538e3e2981..e4ae51e3a21 100644
--- a/gateway/session_deferred_pick.py
+++ b/gateway/session_deferred_pick.py
@@ -47,7 +47,10 @@ def defer_pick(authority, actor, ref, params):
current.get('payload', {}).pop('reasoning', None) # this level is newer than the one picked with the model
value, picked = {**current, 'reasoning': pick}, {'effort': params['payload']['effort']}
else:
- value = {**pick, **({'reasoning': current['reasoning']} if 'reasoning' in current else {})}
+ # A level picked with this model is newer than one deferred before it, which it drops: the
+ # slot never holds both, so committing the slot's level never strips the model's own.
+ older = 'reasoning' in current and 'reasoning' not in params['payload']
+ value = {**pick, **({'reasoning': current['reasoning']} if older else {})}
picked = {'model': params['payload']['model']}
result = {'session_id': ref.session_id, 'operation': operation, 'status': 'deferred', **picked,
'revision': row[0], 'execution_generation': row[1]}
diff --git a/hermes_state_mutation_prepared.py b/hermes_state_mutation_prepared.py
index af4c2fa31af..089c2c64bba 100644
--- a/hermes_state_mutation_prepared.py
+++ b/hermes_state_mutation_prepared.py
@@ -60,7 +60,19 @@ def model_in_transaction(db, conn, session_id, payload, prepared):
def reasoning_in_transaction(db, conn, session_id, payload, prepared):
+ from hermes_state_runtime import _json
snapshot, generation = _commit_policy(db, conn, session_id, prepared)
+ # This level is newer than any still waiting for a turn boundary, beside or inside a deferred model pick.
+ key = DEFERRED_PICK_PREFIX + session_id
+ row = conn.execute('SELECT value FROM state_meta WHERE key=?', (key,)).fetchone()
+ if row is not None:
+ pick = json.loads(row[0])
+ pick.pop('reasoning', None)
+ pick.get('payload', {}).pop('reasoning', None)
+ if 'application_id' in pick:
+ conn.execute('UPDATE state_meta SET value=? WHERE key=?', (_json(pick), key))
+ else:
+ conn.execute('DELETE FROM state_meta WHERE key=?', (key,))
return {session_id, snapshot['target']}, {'effort': payload['effort'], 'execution_generation': generation}
diff --git a/tests/gateway/test_session_deferred_reasoning.py b/tests/gateway/test_session_deferred_reasoning.py
index 633ad97b286..b4c235d5fb1 100644
--- a/tests/gateway/test_session_deferred_reasoning.py
+++ b/tests/gateway/test_session_deferred_reasoning.py
@@ -49,3 +49,52 @@ async def test_levels_picked_during_a_running_turn_apply_to_the_next_turn_in_pic
release.set()
await owner.close()
store.close_all_db_handles()
+
+
+async def test_a_level_committed_after_the_turn_settles_supersedes_the_deferred_one(tmp_path, monkeypatch):
+ from gateway import session_deferred_pick
+ from gateway.session_contract import Submission
+ from gateway.session_policy import policy_for_source
+ import hermes_state_runtime as rt
+ started, release = threading.Event(), asyncio.Event()
+ seen, state = [], {}
+
+ async def handle(event):
+ live = state['authority'].sessions[state['ref'].session_id]
+ seen.append((event.text, (policy_for_source(state['authority'].runner, live.source).reasoning_config
+ or {}).get('effort')))
+ if event.text == 'RUNNING':
+ started.set()
+ await release.wait()
+ return 'ok'
+
+ async def pick(n, effort):
+ snap = db.get_session(ref.session_id)
+ reply = await owner.dispatch({'id': n, 'method': 'session.mutate', 'params': {
+ 'session_id': ref.session_id, 'request_id': f'pick-{n}', 'expected_revision': snap['runtime_revision'],
+ 'expected_generation': snap['runtime_generation'], 'operation': 'reasoning',
+ 'payload': {'effort': effort}}})
+ return reply['result'].get('status')
+ store, db, authority, owner, ref = _owner(tmp_path, monkeypatch, handle)
+ state.update(authority=authority, ref=ref)
+ authority.runner._set_session_reasoning_override = lambda *a: None
+ apply = session_deferred_pick.apply_deferred_pick
+ try:
+ first = await authority.submit(owner.actor, Submission('running', ref, {'text': 'RUNNING'}, 'queue'))
+ await async_wait_until(started.is_set, timeout=10, what='the first turn is running')
+ assert await pick(0, 'low') == 'deferred'
+ # The newer pick takes the mutation lock before the drain reaches the boundary.
+ monkeypatch.setattr(session_deferred_pick, 'apply_deferred_pick', lambda *a: asyncio.sleep(0))
+ release.set()
+ await async_wait_until(
+ lambda: rt.get_session_admission(db, admission_id=first.admission_id)['status'] == 'terminal',
+ timeout=10, what='the running turn settled')
+ assert await pick(1, 'high') is None # committed now, not deferred
+ monkeypatch.setattr(session_deferred_pick, 'apply_deferred_pick', apply)
+ await _run_turn(authority, owner, db, ref, 'NEXT')
+ assert seen[-1] == ('NEXT', 'high'), seen
+ finally:
+ release.set()
+ await owner.close()
+ store.close_all_db_handles()