The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): a cold /refine leaves the session's terminal alone
Tekniume1f987caf975
diff --git a/gateway/session_refine.py b/gateway/session_refine.pyindex 82cb085e6df..e1103dbb7d8 100644--- a/gateway/session_refine.py+++ b/gateway/session_refine.py@@ -58,7 +58,8 @@ async def refine_local_session(runner, source, focus):from agent.turn_finalizer import _clone_background_review_messageswith _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.+ # runs a turn of its own, so it writes no row. close() would tear down that id's terminal,+ # browser and memory provider (the session's), so only the agent's own clients are released.agent = holder[0] = _side_agent(authority, policy, live, task_id, session_id=target)agent._end_session_on_close = Falseagent.background_review_callback = publish@@ -73,7 +74,7 @@ async def refine_local_session(runner, source, focus):finally:finish_background_review_run(agent, run)with contextlib.suppress(Exception):- agent.close()+ agent.release_clients()return '' # the summary (if any) was published above; an empty review.summary draws nothingtry:diff --git a/tests/gateway/test_local_refine_keeps_session_resources.py b/tests/gateway/test_local_refine_keeps_session_resources.pynew file mode 100644index 00000000000..f422c024f16--- /dev/null+++ b/tests/gateway/test_local_refine_keeps_session_resources.py@@ -0,0 +1,66 @@+"""A cold ``/refine`` on a local session leaves the session's tool resources alone (T7).++The review side agent borrows the session's id so its usage is attributed to the session. Closing it+would run the session-id-keyed task cleanup (terminal sandbox, browser) of the foreground session, so+it releases only its own clients. Real refine body, AIAgent cleanup, terminal registry and+LocalEnvironment; review inference and agent construction are stubbed.+"""+from contextlib import nullcontext+from types import SimpleNamespace++import pytest+++async def test_cold_refine_keeps_the_sessions_terminal(tmp_path, monkeypatch):+ refine = pytest.importorskip('gateway.session_refine')+ from gateway import session_tui_side, session_policy, session_local_recovery+ from agent import background_review+ from run_agent import AIAgent+ from tools.environments.local import LocalEnvironment+ from tools import terminal_tool+ monkeypatch.setenv('HERMES_HOME', str(tmp_path))+ monkeypatch.setenv('HOME', str(tmp_path))+ monkeypatch.setenv('TMPDIR', str(tmp_path))+ monkeypatch.setenv('TERMINAL_TEMP_DIR', str(tmp_path))+ sid = 'independent-refine-parent'+ env = LocalEnvironment(cwd=str(tmp_path), persistent=True)+ first = env.execute('export INDEPENDENT_REFINE_SENTINEL=kept; printf ready')+ assert first['returncode'] == 0, first+ before = env.execute('printf %s "$INDEPENDENT_REFINE_SENTINEL"')['output']+ assert 'kept' in before, before+ # Per-task isolation uses the session id (as nonpersistent Docker also does),+ # unlike ordinary local environments whose key is session:<route>.+ terminal_tool.register_task_env_overrides(sid, {'env_type': 'local'})+ key = terminal_tool._resolve_container_task_id(sid)+ assert key == sid, key+ terminal_tool._active_environments[key] = env+ source = SimpleNamespace(chat_id=sid)+ live = SimpleNamespace(source=source)+ authority = SimpleNamespace(profile_id=str(tmp_path), sessions={sid: live},+ db=SimpleNamespace(get_messages_as_conversation=lambda *a, **k: [{'role': 'user', 'content': 'history'}]))+ policy = object()+ # Isolate inference/authority construction only. The production refine body,+ # AIAgent.close, task-resource cleanup, registry and LocalEnvironment are real.+ monkeypatch.setattr(refine, '_local_session', lambda r, s: (authority, live))+ monkeypatch.setattr(session_policy, 'policy_for_source', lambda r, s: policy)+ monkeypatch.setattr(session_local_recovery, 'transcript_target', lambda a, r: sid)+ monkeypatch.setattr(session_tui_side, '_side_scope', lambda *a: nullcontext())+ def side_agent(*args, **kwargs):+ agent = object.__new__(AIAgent)+ agent.session_id = kwargs.get('session_id', args[3])+ agent.valid_tool_names = set()+ return agent+ monkeypatch.setattr(session_tui_side, '_side_agent', side_agent)+ monkeypatch.setattr(background_review, 'spawn_background_review_thread', lambda *a, **k: (lambda: None, ''))+ monkeypatch.setattr(session_tui_side, '_spawn', lambda a, r, t, e, body, **kw: body())+ try:+ reply = await refine.refine_local_session(object(), source, 'preserve my terminal')+ retained = sid in terminal_tool._active_environments+ after = env.execute('printf %s "${INDEPENDENT_REFINE_SENTINEL-missing}"')['output']+ print('REFINE_EVIDENCE', {'reply': reply, 'before': before, 'after': after, 'registered': retained})+ assert retained and 'kept' in after, 'Refine closed the foreground terminal and deleted its shell snapshot'+ finally:+ terminal_tool._active_environments.pop(sid, None)+ terminal_tool.clear_task_env_overrides(sid)+ env.cleanup()