Skip to content

The agent that grows with you

refactor(gateway): share the reasoning mutation payload and the profile read

Teknium7d7ef6ebe130
diff --git a/gateway/session_mutation_policy.py b/gateway/session_mutation_policy.py
index 3f99a255952..b75ea619960 100644
--- a/gateway/session_mutation_policy.py
+++ b/gateway/session_mutation_policy.py
@@ -22,18 +22,14 @@ def _rebind(authority, prepared, old, policy, **extra):
async def prepare_reasoning(authority, live, payload, prepared):
"""``/reasoning <level>`` for this session: the level ``build_policy`` freezes at creation. ``None``
(``/reasoning reset``) gives the level back to the profile."""
- from gateway.session_policy import pin_reasoning_effort, restore_policy, unpin_reasoning
+ from gateway.session_policy import pin_reasoning_effort, profile_config, restore_policy, unpin_reasoning
old = restore_policy(prepared['snapshot']['receipt']['policy'])
frozen = old.config()
if payload['effort'] is not None:
pin_reasoning_effort(frozen, payload['effort'], session=True)
else:
- from pathlib import Path
- from gateway.run import _load_gateway_config
- from gateway.session_policy import present_sections
# A bypass launch never reads the profile: its reset is the built-in default.
- profile = {} if old.ignore_user_config else await asyncio.to_thread(lambda: present_sections(
- _load_gateway_config(Path(authority.db.db_path).resolve().parent / 'config.yaml')))
+ profile = {} if old.ignore_user_config else await asyncio.to_thread(profile_config, authority)
unpin_reasoning(frozen, profile)
return _rebind(authority, prepared, old, replace(old, config_json=json.dumps(frozen)))
diff --git a/gateway/session_policy.py b/gateway/session_policy.py
index f7a41446367..738f8f365c1 100644
--- a/gateway/session_policy.py
+++ b/gateway/session_policy.py
@@ -43,16 +43,20 @@ def pin_reasoning_effort(config, effort, *, session=False):
config[SESSION_PINS] = {**(config.get(SESSION_PINS) or {}), 'reasoning': True}
+def profile_config(authority):
+ """The owning profile's current ``config.yaml`` (its present sections), read fresh."""
+ from gateway.run import _load_gateway_config
+ return present_sections(_load_gateway_config(Path(authority.db.db_path).resolve().parent / 'config.yaml'))
+
+
def unpin_reasoning(config, profile):
"""``/reasoning reset``: the level is *profile*'s (its current config) again, re-read every turn by a
following session and on each resume by any other; a create-time ``reasoning`` included."""
+ from gateway.session_policy_refresh import _follow
followed = profile.get('agent') if isinstance(profile.get('agent'), dict) else {}
agent = config.setdefault('agent', {})
for key in ('reasoning_effort', 'reasoning_overrides'):
- if key in followed:
- agent[key] = followed[key]
- else:
- agent.pop(key, None)
+ _follow(agent, followed, key)
config[SESSION_PINS] = {**(config.get(SESSION_PINS) or {}), 'reasoning': False}
@@ -196,8 +200,7 @@ class LocalSessionPolicy:
held = _TURN_PROFILE.get()
if held is not None and held[0] == self:
return held[1]
- from gateway.run import _load_gateway_config
- return present_sections(_load_gateway_config(Path(authority.db.db_path).resolve().parent / 'config.yaml'))
+ return profile_config(authority)
def current_toolsets(self, config):
"""The next turn's toolsets from its resolved *config*: a following session without an explicit
diff --git a/gateway/session_setting_commands.py b/gateway/session_setting_commands.py
index 21425551d61..6e24df98107 100644
--- a/gateway/session_setting_commands.py
+++ b/gateway/session_setting_commands.py
@@ -71,6 +71,14 @@ def local_authority_live(runner, source):
_FENCE_ATTEMPTS = 3
+def _reasoning_mutation(ref, row, effort):
+ """The owner's fenced ``reasoning`` mutation of a ``/reasoning`` command, on *row*'s current fences."""
+ import uuid
+ return {'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}}
+
+
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
@@ -79,7 +87,6 @@ async def commit_reasoning_command(authority, actor, ref, value, persist_global)
and so does a messaging chat (no session policy). While a turn runs, the handler applies it for the next
turn as on main and the drain commits it at that turn's end (``apply_pending_reasoning``)."""
import asyncio
- import uuid
from gateway.config import Platform
from gateway.session_mutations import mutate_session
from hermes_constants import parse_reasoning_effort
@@ -102,10 +109,7 @@ async def commit_reasoning_command(authority, actor, ref, value, persist_global)
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}})
+ await mutate_session(authority, actor, ref, _reasoning_mutation(ref, row, effort))
return
except (RuntimeStoreError, *MUTATION_GUARD_REFUSALS) as exc:
reason = getattr(exc, 'reason', 'session_busy')
@@ -121,7 +125,6 @@ async def apply_pending_reasoning(authority, ref, live):
"""Commit a reasoning command that arrived while a turn ran. The drain calls this at the turn boundary,
under the claim gate and mutation lock, before a deferred model pick (whose level wins, as on main)."""
import asyncio
- import uuid
from gateway.session_mutations import write_mutation
from hermes_state_mutation_guards import MUTATION_GUARD_REFUSALS
from hermes_state_runtime import RuntimeStoreError
@@ -130,10 +133,7 @@ async def apply_pending_reasoning(authority, ref, live):
return
try:
row = await asyncio.to_thread(authority.db.get_session, ref.session_id)
- await write_mutation(authority, 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': pending[1]}}, None, principal_id=pending[0])
+ await write_mutation(authority, ref, _reasoning_mutation(ref, row, pending[1]), None, principal_id=pending[0])
except (RuntimeStoreError, *MUTATION_GUARD_REFUSALS) as exc:
if getattr(exc, 'reason', 'session_busy') in ('session_busy', 'unknown_execution'):
live.pending_reasoning = live.pending_reasoning or pending # a worker still runs: the next boundary