The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): /review works on a session left idle, without a message first
Teknium328f7bd8096d
diff --git a/gateway/session_cold_review.py b/gateway/session_cold_review.pynew file mode 100644index 00000000000..72c50670270--- /dev/null+++ b/gateway/session_cold_review.py@@ -0,0 +1,70 @@+"""``/review`` for a session whose cached agent is gone.++The gateway drops a session's cached agent after an idle hour (``agent.agent_cache.idle_ttl_secs``),+under LRU or memory pressure, and around ``/moa``. The conversation outlives it in the session's+stored transcript, but ``/review`` read only the cached agent, so a session left alone for an hour+answered "Nothing to review yet" until the next message rebuilt one. A cold ``/review`` reviews the+stored transcript and dispatches the reviewer from a fresh agent on the route, toolsets and session+id the session's next turn would use, so its completion comes back exactly as a resident agent's.+"""+import asyncio+import contextlib+++async def cold_review_history(runner, source):+ """``(session_id, transcript)`` of the session ``source`` belongs to: a local session's current+ physical transcript (``gateway/session_refine.py`` reads the same one), else the messaging entry's."""+ from gateway.session_refine import _local_session+ authority, live = _local_session(runner, source)+ if live is not None:+ from gateway.session_contract import SessionRef+ from gateway.session_local_recovery import transcript_target++ def load():+ target = transcript_target(authority, SessionRef(authority.profile_id, source.chat_id))+ return target, authority.db.get_messages_as_conversation(target, include_ancestors=True)+ return await asyncio.to_thread(load)+ entry = await runner.async_session_store.get_or_create_session(source)+ return entry.session_id, await runner.async_session_store.load_transcript(entry.session_id)+++def cold_review_parent(runner, source, session_key, session_id):+ """A fresh agent on the session's next-turn route (built as ``session_execution._build_fresh_agent``+ builds one, minus the turn-only prompt pieces), inside the session's policy scope. On exit only its+ own LLM clients are released: ``close()`` would tear down the session's terminal and browser, and the+ reviewer it dispatched is detached and builds its own clients."""+ from gateway.run import _load_gateway_config, _platform_config_key+ from gateway.session_authorities import active_authority+ from gateway.session_policy import policy_for_source, policy_scope, policy_turn_toolsets+ from run_agent import AIAgent+ policy = policy_for_source(runner, source)+ authority = active_authority(runner) if policy else None+ user_config = policy.config(authority, secrets=False) if policy else _load_gateway_config()+ platform_key = policy.platform if policy else _platform_config_key(source.platform)+ enabled, disabled = runner._resolve_turn_toolsets(user_config, source, platform_key)+ if policy:+ enabled = policy_turn_toolsets(authority, policy, user_config, source.chat_id)+ with policy_scope(policy, authority=authority):+ model, runtime = runner._resolve_session_agent_runtime(+ source=source, session_key=session_key, user_config=user_config)+ pr = runner._provider_routing+ agent = AIAgent(+ model=model, **runtime, quiet_mode=True, verbose_logging=False,+ enabled_toolsets=enabled, disabled_toolsets=disabled,+ # Launch-preloaded skills (``hermes -s``): the reviewer is told to load them too.+ ephemeral_system_prompt=(policy.skills_prompt if policy else None) or None,+ reasoning_config=runner._resolve_session_reasoning_config(source=source, model=model),+ providers_allowed=pr.get("only"), providers_ignored=pr.get("ignore"), providers_order=pr.get("order"),+ provider_sort=pr.get("sort"), provider_require_parameters=pr.get("require_parameters", False),+ provider_data_collection=pr.get("data_collection"),+ session_id=session_id, platform=platform_key, gateway_session_key=session_key,+ session_db=getattr(runner._session_db, "_db", runner._session_db),+ # The reviewer reads the transcript excerpt; this agent never runs a turn of its own.+ skip_memory=True)+ agent._end_session_on_close = False+ try:+ yield agent+ finally:+ with contextlib.suppress(Exception):+ agent.release_clients()diff --git a/gateway/slash_commands_goals.py b/gateway/slash_commands_goals.pyindex 7578f0740ac..55c6f1c8b1f 100644--- a/gateway/slash_commands_goals.py+++ b/gateway/slash_commands_goals.py@@ -206,16 +206,31 @@ class GatewayGoalCommandsMixin:event carries no gateway route and never re-enters this chat."""args = (event.get_command_args() or "").strip()quick_key, agent, error = self._idle_cached_agent_or_error(event, "review")+ snapshot = list(getattr(agent, "_session_messages", None) or [])+ cold_session_id = None+ if agent is None and quick_key and quick_key not in self._running_agents:+ # The cached agent was evicted (idle TTL, pressure, /moa): review the stored transcript.+ from gateway.session_cold_review import cold_review_history+ from gateway.session_transcript import TranscriptReadError+ from gateway.slash_commands_status import history_unreadable+ try:+ cold_session_id, snapshot = await cold_review_history(self, event.source)+ except TranscriptReadError:+ return history_unreadable()+ error = Noneif error:return error- snapshot = list(getattr(agent, "_session_messages", None) or [])from tools.approval_context import reset_current_session_key, set_current_session_keydef _dispatch():+ from agent.review_engine import start_review+ from gateway.session_cold_review import cold_review_parenttoken = set_current_session_key(quick_key)try:- from agent.review_engine import start_review- return start_review(agent, snapshot, args)+ if cold_session_id is None:+ return start_review(agent, snapshot, args)+ with cold_review_parent(self, event.source, quick_key, cold_session_id) as parent:+ return start_review(parent, snapshot, args)finally:reset_current_session_key(token)diff --git a/tests/gateway/test_local_review_without_cached_agent.py b/tests/gateway/test_local_review_without_cached_agent.pynew file mode 100644index 00000000000..4d757248fc5--- /dev/null+++ b/tests/gateway/test_local_review_without_cached_agent.py@@ -0,0 +1,91 @@+"""``/review`` on a local session whose cached agent is gone.++The gateway drops a session's cached agent after an idle hour, under memory pressure, and around a+``/moa`` turn. ``/review`` read only the cached agent, so a session left alone for an hour answered+"Nothing to review yet — send a message first" until the next message rebuilt one. The conversation+is the session's stored transcript; the reviewer reviews it and its completion re-enters the session.++Real daemon (``python -m gateway.run``), ``slash.exec`` (``session_commands.execute_command``),+loopback model. ``/moa`` drops the cached agent here because the idle sweep's first pass is a minute in.+"""+import asyncio+import json+import signal+import sqlite3+import threading+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+from tests.waiting import async_wait_until++[email protected]("linux") # the daemon is stopped with SIGINT, which raises on Windows+def test_local_review_reviews_the_stored_conversation_once_the_cached_agent_is_gone(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': 'review-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='cold-review', source='cli', toolsets=[],+ cwd=str(work))+ sid = created['result']['session_id']++ def rows():+ with sqlite3.connect(f'file:{home / "state.db"}?mode=ro', uri=True) as db:+ return dict(db.execute('SELECT request_id,status FROM session_admissions'))++ async def settle(input_id):+ await async_wait_until(lambda: rows().get(input_id) == 'terminal', timeout=45,+ what=f'{input_id} turn terminal')++ await rpc(ws, 'prompt.submit', session_id=sid, input_id='warm', text='OPENED_PR_42')+ await settle('warm')+ moa = (await rpc(ws, 'slash.exec', session_id=sid, command='moa compare the options'))['result']+ await rpc(ws, 'prompt.submit', session_id=sid, input_id='moa', text=moa['message'])+ await settle('moa')+ before = len(peer.requests)+ reply = await rpc(ws, 'slash.exec', session_id=sid, command='review check the naming')+ out['review'] = review = reply.get('result') or reply.get('error')+ assert 'Nothing to review' not in json.dumps(review), ('/review lost the conversation with its agent', out)+ await async_wait_until(lambda: any('check the naming' in json.dumps(r) for r in peer.requests[before:]),+ timeout=30, what='the reviewer reaching the model')+ request = next(r for r in peer.requests[before:] if 'check the naming' in json.dumps(r))+ out['reviewer_saw_conversation'] = 'OPENED_PR_42' in json.dumps(request['messages'])+ # The reviewer's completion comes back to this session as a delegation-completion turn.+ await async_wait_until(lambda: any('ASYNC DELEGATION' in json.dumps(r) for r in peer.requests[before:]),+ timeout=60, what='the review completion re-entering the session')++ 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)++ review = out['review']+ assert review.get('type') == 'exec' and 'Review started' in review['output'], out+ assert out['reviewer_saw_conversation'], outdiff --git a/website/docs/user-guide/features/delegation.md b/website/docs/user-guide/features/delegation.mdindex 8040ba38c30..dfe081aee77 100644--- a/website/docs/user-guide/features/delegation.md+++ b/website/docs/user-guide/features/delegation.md@@ -348,7 +348,7 @@ Listing a profile in `delegation.backends.profile_routes` lets this profile's suWhat happens:-1. The last 10 user/assistant messages are snapshotted as the reviewer's starting evidence (tool output and system messages are excluded).+1. The last 10 user/assistant messages are snapshotted as the reviewer's starting evidence (tool output and system messages are excluded). On the gateway they come from the session's stored transcript when its agent has been released (after `agent.agent_cache.idle_ttl_secs` of idling, one hour by default), so `/review` works on a session you left alone without sending a message first.2. A reviewer subagent is dispatched on the same background delegation rail as `delegate_task` — it gets the full normal subagent toolset (terminal, web, files, browser...), so it actually opens the PR, reads the diff, and runs code rather than judging from the excerpt.3. The reviewer inherits the primary agent's working context: any skills the primary agent had loaded (launch-preloaded or via `skill_view` during the session) are named in its briefing with an instruction to load them and judge the work against their conventions. Like every subagent, its system prompt also embeds the workspace's project context files (AGENTS.md / CLAUDE.md / .cursorrules) as binding conventions.4. When it finishes, its full review re-enters the same session as a normal background-subagent completion — your primary agent sees it and can act on it (fix the findings, push follow-ups, reply to you).