From 3918ece17f4d4c00d8db28e29261b9df8c9ec8ab Mon Sep 17 00:00:00 2001 From: Xubin Ren <52506698+Re-bin@users.noreply.github.com> Date: Fri, 6 Nov 2026 09:35:41 +0800 Subject: [PATCH] test: separate preparation responsiveness from persistence timing --- tests/agent/test_prompt_responsiveness.py | 7 +++- tests/webui/test_subagent_persistence.py | 47 +++++++++++++---------- 2 files changed, 31 insertions(+), 23 deletions(-) diff --git a/tests/agent/test_prompt_responsiveness.py b/tests/agent/test_prompt_responsiveness.py index fe076f90b..3cf9f1933 100644 --- a/tests/agent/test_prompt_responsiveness.py +++ b/tests/agent/test_prompt_responsiveness.py @@ -11,7 +11,7 @@ from nanobot.agent.tools.context import current_request_context from nanobot.providers.base import LLMProvider, LLMResponse, LLMUsage from nanobot.runtime_context import public_history_message from nanobot.security.workspace_access import current_workspace_scope -from nanobot.session.manager import Session +from nanobot.session.manager import Session, SessionPolicy class ControlledProvider(LLMProvider): @@ -126,7 +126,10 @@ async def test_preparation_allows_status_heartbeat_and_cancellation( tasks = [task, pulse] try: await asyncio.wait_for(entered.wait(), timeout=5) - status = await asyncio.wait_for(loop.process_direct("/status", session_key="cli:status"), 5) + # Measure preparation responsiveness, not command-history disk writes. + status = await asyncio.wait_for(loop.process_direct( + "/status", session_key="cli:status", _session_policy=SessionPolicy(persist=False), + ), 5) assert status is not None and "test-model" in status.content heartbeat_requested.set() await asyncio.wait_for(heartbeat_progress.wait(), 5) diff --git a/tests/webui/test_subagent_persistence.py b/tests/webui/test_subagent_persistence.py index c3396c104..0b67362a4 100644 --- a/tests/webui/test_subagent_persistence.py +++ b/tests/webui/test_subagent_persistence.py @@ -62,6 +62,27 @@ def gateway_for(manager, sessions, workspace): return gateway, SimpleNamespace(remote_address=("127.0.0.1", 12345)), Headers({"Authorization": f"Bearer {token}"}) +async def wait_for_model_requests(manager, task_id, requests, count): + task = manager._running_tasks[task_id] + for _ in range(count): + request = asyncio.create_task(requests.get()) + try: + done, _ = await asyncio.wait( + (request, task), timeout=5, return_when=asyncio.FIRST_COMPLETED, + ) + if task in done: + result = task.result() + status = manager.check(task_id, "websocket:parent") + pytest.fail( + "subagent ended before its activity was ready: " + f"state={status.state}, error={status.error!r}, result={result!r}" + ) + assert request in done, "subagent made no progress to its next model request" + finally: + request.cancel() + await asyncio.gather(request, return_exceptions=True) + + @pytest.mark.asyncio @pytest.mark.parametrize("wait", [False, True]) async def test_cancelled_task_creation_removes_committed_child(tmp_path, monkeypatch, wait): @@ -119,18 +140,18 @@ async def test_child_thread_reads_real_session_through_parent_without_new_persis workspace.mkdir() (workspace / "config.txt").write_text("configured value", encoding="utf-8") manager, sessions, runtime = manager_with_storage(workspace, root) - entered, release = asyncio.Event(), asyncio.Event() + model_requests, release = asyncio.Queue(), asyncio.Event() final_text = ("## Findings\n\n" + "Verified configuration.\n" * 1000).rstrip() calls = 0 async def respond(**_kwargs): nonlocal calls calls += 1 + model_requests.put_nowait(None) if calls == 1: return LLMResponse(content="Inspecting configuration", tool_calls=[ ToolCallRequest("read-config", "read_file", {"path": "config.txt"}), ]) - entered.set() await release.wait() return LLMResponse(content=final_text) @@ -138,8 +159,9 @@ async def test_child_thread_reads_real_session_through_parent_without_new_persis gateway, connection, headers = gateway_for(manager, sessions, workspace) try: await manager.spawn("Inspect configuration", session_key="websocket:parent", runtime=runtime) - await asyncio.wait_for(entered.wait(), 5) task_id, = manager.statuses_for_session("websocket:parent") + # Bound each round, rather than the combined startup/tool/persistence work. + await wait_for_model_requests(manager, task_id, model_requests, 2) child_key = SubagentSessions.key(task_id) path = f"/api/sessions/websocket%3Aparent/subagents/{task_id}/webui-thread" files_before = set(root.rglob("*")) @@ -244,24 +266,7 @@ async def test_child_activity_replays_real_tool_errors_and_file_diffs(tmp_path): try: await manager.spawn("Edit and inspect files", session_key="websocket:parent", runtime=runtime) task_id, = manager.statuses_for_session("websocket:parent") - task = manager._running_tasks[task_id] - for _ in range(4): - request = asyncio.create_task(model_requests.get()) - try: - done, _ = await asyncio.wait( - (request, task), timeout=5, return_when=asyncio.FIRST_COMPLETED, - ) - if task in done: - result = task.result() - status = manager.check(task_id, "websocket:parent") - pytest.fail( - "subagent ended before its activity was ready: " - f"state={status.state}, error={status.error!r}, result={result!r}" - ) - assert request in done, "subagent made no progress to its next model request" - finally: - request.cancel() - await asyncio.gather(request, return_exceptions=True) + await wait_for_model_requests(manager, task_id, model_requests, 4) path = f"/api/sessions/websocket%3Aparent/subagents/{task_id}/webui-thread" async def assert_activity(gateway, connection, headers):