The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
refactor(gateway): share preview-restart prompt, cwd check and reply text with the sidecar
Tekniumb4fbb145917e
diff --git a/gateway/session_tui_side.py b/gateway/session_tui_side.pyindex 22e2a0dfcf9..a012b87f248 100644--- a/gateway/session_tui_side.py+++ b/gateway/session_tui_side.py@@ -135,6 +135,7 @@ async def background(connection, ref, params):authority = connection.authoritydef body():+ from tui_gateway.methods_prompt import _final_response_textwith _side_scope(authority, policy, task_id):agent = _side_agent(authority, policy, live, task_id)try:@@ -143,7 +144,7 @@ async def background(connection, ref, params):# AIAgent.close() is the owner boundary (memory, tool subprocesses, clients).with contextlib.suppress(Exception):agent.close()- return result.get('final_response', str(result)) if isinstance(result, dict) else str(result)+ return _final_response_text(result)return _spawn(authority, ref, task_id, 'background.complete', body)@@ -180,8 +181,6 @@ async def preview_restart(connection, ref, params):live, policy, url, task_id = _admit(connection, ref, params, 'preview', required='url',optional=('cwd', 'context'))authority = connection.authority- import os- from tui_gateway.methods_prompt import _PREVIEW_RESTART_HISTORY_NOTE, _PREVIEW_RESTART_RULEScwd, context = (str(params.get(key) or '').strip() for key in ('cwd', 'context'))from gateway.session_local_recovery import transcript_targettarget = transcript_target(authority, ref)@@ -193,17 +192,11 @@ async def preview_restart(connection, ref, params):def body():from tui_gateway.agent_callbacks import _preview_restart_history- try: # A malformed client path (embedded NUL, a missing dir) is "no validated cwd".- preview_cwd = os.path.abspath(os.path.expanduser(cwd)) if cwd else ''- preview_cwd = preview_cwd if preview_cwd and os.path.isdir(preview_cwd) else ''- except (OSError, ValueError):- preview_cwd = ''+ from tui_gateway.methods_prompt import (_final_response_text, _preview_restart_prompt,+ _validated_preview_cwd)+ preview_cwd = _validated_preview_cwd(cwd)history = _preview_restart_history(authority.db.get_messages_as_conversation(target) if target else [])- prompt = '\n'.join(line for line in [- 'The desktop preview pane cannot load a local server URL.', f'Preview URL: {url.strip()}',- f"Current working directory: {cwd or '(unknown)'}",- f'Preview console:\n{context}' if context else '',- _PREVIEW_RESTART_HISTORY_NOTE if history else '', *_PREVIEW_RESTART_RULES] if line)+ prompt = _preview_restart_prompt(url, cwd, context, bool(history))with _side_scope(authority, policy, task_id):if preview_cwd:from tools.terminal_tool import register_task_env_overrides@@ -221,6 +214,6 @@ async def preview_restart(connection, ref, params):with contextlib.suppress(Exception):from tools.terminal_tool import clear_task_env_overridesclear_task_env_overrides(task_id)- return result.get('final_response', str(result)) if isinstance(result, dict) else str(result)+ return _final_response_text(result)return _spawn(authority, ref, task_id, 'preview.restart.complete', body)diff --git a/tui_gateway/methods_prompt.py b/tui_gateway/methods_prompt.pyindex ac8c35f571f..6e892ce8514 100644--- a/tui_gateway/methods_prompt.py+++ b/tui_gateway/methods_prompt.py@@ -1151,23 +1151,8 @@ def _(rid, params: dict) -> dict:with session.get("history_lock") or contextlib.nullcontext():snapshot = list(session.get("history") or [])parent_history = _preview_restart_history(snapshot)- prompt = "\n".join(- line- for line in [- "The desktop preview pane cannot load a local server URL.",- f"Preview URL: {url}",- f"Current working directory: {cwd or '(unknown)'}",- f"Preview console:\n{context}" if context else "",- _PREVIEW_RESTART_HISTORY_NOTE if parent_history else None,- *_PREVIEW_RESTART_RULES]- if line)- # A malformed client path (embedded NUL, etc.) is "no validated cwd".- try:- preview_cwd = os.path.abspath(os.path.expanduser(cwd)) if cwd else ""- if preview_cwd and not os.path.isdir(preview_cwd):- preview_cwd = ""- except Exception:- preview_cwd = ""+ prompt = _preview_restart_prompt(url, cwd, context, bool(parent_history))+ preview_cwd = _validated_preview_cwd(cwd)def body():from run_agent import AIAgent@@ -1337,6 +1322,28 @@ def _final_response_text(result) -> str:return (result.get("final_response", str(result)) if isinstance(result, dict) else str(result))+def _preview_restart_prompt(url: str, cwd: str, context: str, has_history: bool) -> str:+ """The hidden restart agent's task, shared by this transport and the gateway's+ (``gateway/session_tui_side.py``) so the two cannot drift."""+ return "\n".join(line for line in [+ "The desktop preview pane cannot load a local server URL.", f"Preview URL: {url.strip()}",+ f"Current working directory: {cwd or '(unknown)'}",+ f"Preview console:\n{context}" if context else "",+ _PREVIEW_RESTART_HISTORY_NOTE if has_history else "", *_PREVIEW_RESTART_RULES] if line)+++def _validated_preview_cwd(cwd: str) -> str:+ """The client's cwd as an existing absolute directory, else ``""``. A malformed client path+ (embedded NUL, a missing dir) is "no validated cwd", never an error."""+ import os # this module's globals are bound by tui_gateway.server; the gateway calls it unbound++ try:+ preview_cwd = os.path.abspath(os.path.expanduser(cwd)) if cwd else ""+ return preview_cwd if preview_cwd and os.path.isdir(preview_cwd) else ""+ except (OSError, ValueError):+ return ""++def _spawn_side_agent(rid, session, task_id, parent, event, body, *, cwd="", extra=None, cleanup=None):"""Run ``body()`` on a daemon thread under the session's profile home (the ContextVar