fix(exec): isolate exec session managers

This commit is contained in:
yu-xin-c
2026-07-18 17:35:56 +08:00
committed by Xubin Ren
parent 7ac9a46978
commit 995cc44e89
8 changed files with 132 additions and 12 deletions
+51
View File
@@ -7,6 +7,7 @@ import subprocess
import sys
import time
from nanobot.agent.tools.context import RequestContext, bind_request_context, reset_request_context
from nanobot.agent.tools.exec_session import (
ExecSessionManager,
ListExecSessionsTool,
@@ -398,6 +399,56 @@ def test_list_exec_sessions_reports_running_commands(tmp_path):
assert "Session terminated." in cleanup
def test_exec_sessions_are_scoped_to_request_session_key(tmp_path):
async def run() -> tuple[str, str, str, str, str, str]:
manager = ExecSessionManager()
exec_tool = ExecTool(working_dir=str(tmp_path), timeout=5, session_manager=manager)
list_tool = ListExecSessionsTool(manager=manager)
stdin_tool = WriteStdinTool(manager=manager)
command = _python_command(
"import time; print('ready', flush=True); time.sleep(5)"
)
token_a = bind_request_context(
RequestContext(channel="cli", chat_id="a", session_key="cli:a")
)
try:
initial = await exec_tool.execute(command=command, yield_time_ms=100)
sid = _session_id(initial)
owner_listing = await list_tool.execute()
finally:
reset_request_context(token_a)
unbound_listing = await list_tool.execute()
token_b = bind_request_context(
RequestContext(channel="cli", chat_id="b", session_key="cli:b")
)
try:
other_listing = await list_tool.execute()
other_write = await stdin_tool.execute(session_id=sid, yield_time_ms=0)
finally:
reset_request_context(token_b)
token_a = bind_request_context(
RequestContext(channel="cli", chat_id="a", session_key="cli:a")
)
try:
cleanup = await stdin_tool.execute(session_id=sid, terminate=True, yield_time_ms=0)
finally:
reset_request_context(token_a)
return sid, owner_listing, unbound_listing, other_listing, other_write, cleanup
sid, owner_listing, unbound_listing, other_listing, other_write, cleanup = asyncio.run(run())
assert sid in owner_listing
assert unbound_listing == "No active exec sessions."
assert other_listing == "No active exec sessions."
assert other_write == f"Error: exec session not found: {sid!r}"
assert "Session terminated." in cleanup
def test_list_exec_sessions_reports_empty_state():
result = asyncio.run(ListExecSessionsTool(manager=ExecSessionManager()).execute())