The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): messaging /refine refines a session left idle, in the same chat
Teknium406ea875e151
diff --git a/gateway/session_cold_review.py b/gateway/session_cold_review.pyindex 009a618de46..a633741ac2b 100644--- a/gateway/session_cold_review.py+++ b/gateway/session_cold_review.py@@ -1,4 +1,4 @@-"""``/review`` for a session whose cached agent is gone.+"""``/review`` and messaging ``/refine`` 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@@ -8,7 +8,9 @@ stored transcript and dispatches the reviewer from a fresh agent on the route, tid the session's next turn would use, so its completion comes back exactly as a resident agent's."""import asyncio+import concurrent.futuresimport contextlib+import threadingasync def cold_review_history(runner, source):@@ -28,6 +30,16 @@ async def cold_review_history(runner, source):return entry.session_id, await runner.async_session_store.load_transcript(entry.session_id)+async def stored_transcript(runner, session_key):+ """``(session_id, transcript)`` of a messaging session; the transcript is empty when the chat has no+ session or no user/assistant turn. Read-only: ``/refine`` in a new chat must not create a session."""+ session_id = await runner.async_session_store.peek_session_id(session_key)+ history = await runner.async_session_store.load_transcript(session_id) if session_id else []+ if not any(m.get("role") in {"user", "assistant"} for m in history):+ return session_id, []+ return session_id, list(history)++@contextlib.contextmanagerdef 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``@@ -80,3 +92,41 @@ def cold_review_parent(runner, source, session_key, session_id):finally:with contextlib.suppress(Exception):agent.release_clients()+++def start_cold_refine(runner, source, session_key, session_id, history, focus):+ """``/refine`` for a messaging session whose cached agent is gone: the resident path's own+ ``_spawn_background_review`` call, made from the cold parent. A resident agent sends its review+ summary into the chat through its last turn's callback (``run_turn_runner._make_bg_review_callbacks``);+ this one sends it to the same chat and thread directly. Returns a future that resolves once the review+ is spawned (or raises its start error); the parent's clients are released when the review's request ends,+ on the thread that built it."""+ from gateway.run import _interim_metadata, _non_conversational_metadata+ from tools.thread_context import propagate_context_to_thread+ loop = asyncio.get_running_loop()+ adapter = runner._delivery_adapter_for(source)+ metadata = _interim_metadata(_non_conversational_metadata(+ runner._thread_metadata_for_source(source), platform=source.platform))+ started = concurrent.futures.Future()++ def deliver(message):+ if adapter is not None:+ asyncio.run_coroutine_threadsafe(adapter.send(source.chat_id, str(message), metadata=metadata), loop)++ def body():+ try:+ with cold_review_parent(runner, source, session_key, session_id) as parent:+ parent.background_review_callback = deliver+ parent._spawn_background_review(+ messages_snapshot=history, review_memory=True,+ review_skills="skill_manage" in getattr(parent, "valid_tool_names", set()), focus=focus or None)+ started.set_result(None)+ run = getattr(parent, "_background_review_run", None)+ if run is not None:+ run.request_done.wait()+ except Exception as exc:+ if not started.done():+ started.set_exception(exc)++ threading.Thread(target=propagate_context_to_thread(body), daemon=True, name="cold-refine").start()+ return asyncio.wrap_future(started)diff --git a/gateway/slash_commands_goals.py b/gateway/slash_commands_goals.pyindex 58963007314..f0f7a6ac4d7 100644--- a/gateway/slash_commands_goals.py+++ b/gateway/slash_commands_goals.py@@ -177,39 +177,39 @@ class GatewayGoalCommandsMixin:"""Handle /refine — run the memory/skill review fork on demand, in a daemon thread against asnapshot 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")+ 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:+ session_id = None+ 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_sessionlocal = await refine_local_session(self, event.source, args)if local is not None:return local- # A cold cache (or an agent holding no messages) is not an empty conversation: read the- # durable transcript without creating a session for an unknown key.+ # A released cached agent (idle TTL, pressure, /moa), or one holding no messages, is not+ # an empty conversation: refine the stored transcript.+ from gateway.session_cold_review import stored_transcriptfrom gateway.session_transcript import TranscriptReadErrorfrom gateway.slash_commands_status import history_unreadable- session_id = await self.async_session_store.peek_session_id(_quick_key)try:- history = await self.async_session_store.load_transcript(session_id) if session_id else []+ session_id, snapshot = await stored_transcript(self, quick_key)except TranscriptReadError:return history_unreadable()- turns = sum(1 for m in history if m.get("role") in {"user", "assistant"})- if agent is None and turns:- return (f"This session has {turns} persisted user/assistant messages, but the live agent "- "isn't cached. Send a message (or /resume) to wake the session first, then /refine.")- if turns:- snapshot = list(history)+ error = None if snapshot else errorif error:return errorif not snapshot:return t("gateway.refine.empty")try:- agent._spawn_background_review(- messages_snapshot=snapshot, review_memory=True,- review_skills="skill_manage" in getattr(agent, "valid_tool_names", set()), focus=args or None,- )+ if agent is None:+ from gateway.session_cold_review import start_cold_refine+ await start_cold_refine(self, event.source, quick_key, session_id, snapshot, args)+ else:+ agent._spawn_background_review(+ messages_snapshot=snapshot, review_memory=True,+ review_skills="skill_manage" in getattr(agent, "valid_tool_names", set()), focus=args or None,+ )except Exception as exc:return t("gateway.refine.start_failed", error=exc)tail = t("gateway.refine.focus_suffix", focus=args) if args else ""