Skip to content

The agent that grows with you

fix(gateway): /refine on a local session reads the session, so /moa no longer empties it

Tekniuma55423ed760f
diff --git a/gateway/session_refine.py b/gateway/session_refine.py
new file mode 100644
index 00000000000..82cb085e6df
--- /dev/null
+++ b/gateway/session_refine.py
@@ -0,0 +1,84 @@
+"""``/refine`` for a local session whose cached agent is gone (T7).
+
+``/moa`` arms a one-shot model swap that evicts the session's cached agent, and its settle evicts
+the MoA-built one, so the cached agent is not where a local session's conversation lives. The
+session's stored transcript is: the review runs over it on a side agent built from the session's
+frozen launch policy (the route ``/bg`` and ``/btw`` use, ``gateway/session_tui_side.py``), and its
+summary lands on the session's event stream as ``review.summary``, as a turn-time review does.
+Messaging sessions keep the cached-agent path unchanged.
+"""
+import asyncio
+import contextlib
+import uuid
+
+from agent.i18n import t
+
+
+def _local_session(runner, source):
+ from gateway.config import Platform
+ from gateway.session_setting_commands import local_authority_live
+ if source is None or getattr(source, 'platform', None) != Platform.LOCAL:
+ return None, None
+ return local_authority_live(runner, source)
+
+
+async def refine_local_session(runner, source, focus):
+ """The ``/refine`` reply for a local session, or None when ``source`` is not one."""
+ authority, live = _local_session(runner, source)
+ if live is None:
+ return None
+ from gateway.session_contract import SessionRef
+ from gateway.session_local_recovery import transcript_target
+ from gateway.session_policy import policy_for_source
+ policy = policy_for_source(runner, live.source)
+ if policy is None:
+ return None
+ ref = SessionRef(authority.profile_id, source.chat_id)
+
+ def load():
+ target = transcript_target(authority, ref)
+ return target, authority.db.get_messages_as_conversation(target, include_ancestors=True)
+
+ target, history = await asyncio.to_thread(load)
+ if not history:
+ return t('gateway.refine.nothing_yet')
+ from gateway.session_tui_side import _side_agent, _side_scope, _spawn
+ task_id = f'refine_{uuid.uuid4().hex[:6]}'
+ holder = [None]
+
+ def publish(message):
+ current = authority.sessions.get(ref.session_id)
+ if current is not None:
+ current.event_stream.publish(ref.session_id, {'text': str(message)}, event_type='review.summary')
+
+ def body():
+ from agent.background_review import (
+ finish_background_review_run, prepare_background_review_run, spawn_background_review_thread,
+ )
+ from agent.turn_finalizer import _clone_background_review_messages
+ with _side_scope(authority, policy, task_id):
+ # The session's own id: the review's usage is recorded against it, and the agent never
+ # runs a turn of its own, so it writes no row and must not end the session on close.
+ agent = holder[0] = _side_agent(authority, policy, live, task_id, session_id=target)
+ agent._end_session_on_close = False
+ agent.background_review_callback = publish
+ agent._print_fn = lambda *_a, **_k: None
+ run = prepare_background_review_run(agent)
+ try:
+ review, _prompt = spawn_background_review_thread(
+ agent, _clone_background_review_messages(history), review_memory=True,
+ review_skills='skill_manage' in getattr(agent, 'valid_tool_names', set()),
+ focus=focus or None, review_run=run, explicit=True)
+ review()
+ finally:
+ finish_background_review_run(agent, run)
+ with contextlib.suppress(Exception):
+ agent.close()
+ return '' # the summary (if any) was published above; an empty review.summary draws nothing
+
+ try:
+ _spawn(authority, ref, task_id, 'review.summary', body, holder=holder)
+ except RuntimeError as exc: # the review thread could not start
+ return t('gateway.refine.start_failed', error=exc)
+ tail = t('gateway.refine.focus_suffix', focus=focus) if focus else ''
+ return t('gateway.refine.started', focus_suffix=tail)
diff --git a/gateway/slash_commands_goals.py b/gateway/slash_commands_goals.py
index 61d58976179..2a1794a2be9 100644
--- a/gateway/slash_commands_goals.py
+++ b/gateway/slash_commands_goals.py
@@ -172,9 +172,16 @@ class GatewayGoalCommandsMixin:
snapshot of the cached AIAgent's conversation (live session and prompt cache untouched)."""
args = (event.get_command_args() or "").strip()
_quick_key, agent, error = self._idle_cached_agent_or_error(event, "refine")
+ snapshot = list(getattr(agent, "_session_messages", None) or [])
+ if not snapshot and _quick_key and _quick_key not in self._running_agents:
+ # A local session's conversation is its stored transcript, not the cached agent: /moa
+ # (and its settle) drop that agent, so /refine reads the session itself (T7).
+ from gateway.session_refine import refine_local_session
+ local = await refine_local_session(self, event.source, args)
+ if local is not None:
+ return local
if error:
return error
- snapshot = list(getattr(agent, "_session_messages", None) or [])
if not snapshot:
return t("gateway.refine.empty")
try:
diff --git a/tests/gateway/test_local_moa_then_refine.py b/tests/gateway/test_local_moa_then_refine.py
new file mode 100644
index 00000000000..e5a31d851fd
--- /dev/null
+++ b/tests/gateway/test_local_moa_then_refine.py
@@ -0,0 +1,95 @@
+"""``/moa`` then ``/refine`` on a local session (T7).
+
+``/moa`` arms a one-shot model swap and drops the session's cached agent; when its turn settles the
+restore drops the MoA-built agent too. ``/refine`` used to read the conversation off the cached
+agent, so straight after a ``/moa`` turn it answered "Nothing to refine yet". The conversation
+belongs to the session (its stored transcript), so ``/refine`` still finds it.
+
+Real daemon (``python -m gateway.run``), ``slash.exec`` (``session_commands.execute_command``),
+loopback model.
+"""
+import asyncio
+import json
+import signal
+import threading
+import time
+from http.server import ThreadingHTTPServer
+from pathlib import Path
+
+import pytest
+
+from tests.gateway.fixtures.local_recovery_probe import Model, child_env, daemon, rpc, websocket
+
+
[email protected]("linux") # the daemon is stopped with SIGINT, which raises on Windows
+def test_local_refine_finds_the_conversation_after_a_moa_turn(tmp_path):
+ root = Path(__file__).resolve().parents[2]
+ home, user, work = tmp_path / 'state', tmp_path / 'user', tmp_path / 'work'
+ home.mkdir(mode=0o700)
+ user.mkdir()
+ work.mkdir()
+ peer = ThreadingHTTPServer(('127.0.0.1', 0), Model)
+ peer.requests = []
+ peer.blocked, peer.release = threading.Event(), threading.Event()
+ thread = threading.Thread(target=peer.serve_forever, daemon=True)
+ thread.start()
+ url = f'http://127.0.0.1:{peer.server_port}/v1'
+ (home / 'config.yaml').write_text(json.dumps({
+ 'gateway': {'multiplex_profiles': False},
+ 'model': {'provider': 'custom', 'default': 'refine-model', 'base_url': url},
+ 'auxiliary': {'title_generation': {'enabled': False}},
+ }))
+ env = child_env()
+ env.update(HOME=str(user), USERPROFILE=str(user), HERMES_HOME=str(home), PYTHONPATH=str(root),
+ OPENAI_API_KEY='loopback-only', OPENAI_BASE_URL=url, PYTHONUNBUFFERED='1')
+ out = {}
+
+ async def probe(desc):
+ async with websocket(home, desc) as ws:
+ created = await rpc(ws, 'session.create', request_id='moa-refine', source='cli', toolsets=[],
+ cwd=str(work))
+ sid = created['result']['session_id']
+
+ async def settle(marker):
+ deadline = time.monotonic() + 45
+ while True:
+ snap = (await rpc(ws, 'session.resume', session_id=sid))['result']
+ if not snap['running'] and not snap['pending'] and (
+ marker is None or marker in json.dumps(snap['messages'])):
+ return snap
+ assert time.monotonic() < deadline, ('turn never settled', snap)
+ await asyncio.sleep(.1)
+
+ await rpc(ws, 'prompt.submit', session_id=sid, input_id='warm', text='WARM_REFINE')
+ await settle('RECOVERY_ACK_WARM_REFINE')
+ moa = (await rpc(ws, 'slash.exec', session_id=sid, command='moa compare the options'))['result']
+ out['moa'] = moa
+ # The client submits /moa's rewritten turn with its own input id; it settles either way.
+ await rpc(ws, 'prompt.submit', session_id=sid, input_id='moa', text=moa['message'])
+ await settle(None)
+ reply = await rpc(ws, 'slash.exec', session_id=sid, command='refine keep the naming')
+ out['refine'] = reply.get('result') or reply.get('error')
+ # The review fork reaches the model with the session's conversation and the focus.
+ deadline = time.monotonic() + 30
+ while not any('keep the naming' in json.dumps(r) for r in peer.requests):
+ assert time.monotonic() < deadline, ('the review never ran', out)
+ await asyncio.sleep(.1)
+ review = next(r for r in peer.requests if 'keep the naming' in json.dumps(r))
+ out['review_saw_conversation'] = 'WARM_REFINE' in json.dumps(review['messages'])
+
+ try:
+ with daemon(root, home, env, barrier=False) as (proc, desc):
+ asyncio.run(probe(desc))
+ proc.send_signal(signal.SIGINT)
+ proc.wait(timeout=20)
+ finally:
+ peer.shutdown()
+ peer.server_close()
+ thread.join(timeout=5)
+
+ assert out['moa']['type'] == 'send', out
+ refine = out['refine']
+ assert refine.get('type') == 'exec', out
+ assert 'Nothing to refine' not in refine['output'], ('/refine lost the conversation after /moa', out)
+ assert 'Reviewing this conversation' in refine['output'] and 'keep the naming' in refine['output'], out
+ assert out['review_saw_conversation'], out