perf(agent): prepare prompts outside the event loop (#6224)
* perf(agent): prepare prompts outside the event loop * fix(agent): preserve cancellation during run cleanup * refactor(agent): clarify prompt preparation boundaries * revert(agent): restore original prompt preparation structure This reverts commit 484cb96a08bf3a805f5828d5a65316be7decaaa5.
This commit is contained in:
1 parent
fdeda8a468
commit
3abcfe70c5
6 files changed
+521
-90
No files matched your search
@@ -3,7 +3,7 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from pathlib import Path
|
||||
from unittest.mock import AsyncMock, MagicMock, patch
|
||||
from unittest.mock import AsyncMock, MagicMock
|
||||
|
||||
import pytest
|
||||
|
||||
@@ -74,13 +74,16 @@ async def test_process_message_hands_complete_replay_to_runner(tmp_path: Path) -
|
||||
loop.tools.get_definitions = MagicMock(return_value=[])
|
||||
|
||||
session = loop.sessions.get_or_create("cli:test")
|
||||
with patch.object(session, "get_history", wraps=session.get_history) as get_history:
|
||||
result = await loop._process_message(
|
||||
InboundMessage(channel="cli", sender_id="user", chat_id="test", content="hello")
|
||||
)
|
||||
session.messages = _populated_session(30).messages
|
||||
expected_history = session.get_history()
|
||||
loop.provider.aclose = AsyncMock()
|
||||
result = await loop.process_direct("hello", session_key="cli:test")
|
||||
|
||||
assert result is not None
|
||||
assert get_history.call_args.kwargs == {"extend_to_user": False}
|
||||
sent = loop.provider.chat_stream_with_retry.await_args.kwargs["messages"]
|
||||
assert sent[1:-1] == expected_history
|
||||
assert sent[-1]["content"].startswith("hello\n\n")
|
||||
await loop.aclose()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
|
||||
@@ -0,0 +1,296 @@
|
||||
"""Prompt preparation must leave the public runtime responsive."""
|
||||
|
||||
import asyncio
|
||||
import threading
|
||||
from copy import deepcopy
|
||||
|
||||
import pytest
|
||||
|
||||
from nanobot.agent.hook import AgentHook
|
||||
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
|
||||
|
||||
|
||||
class ControlledProvider(LLMProvider):
|
||||
def __init__(self, *, report_usage=True):
|
||||
super().__init__(provider_name="prompt-test")
|
||||
self.requests = []
|
||||
self.report_usage = report_usage
|
||||
|
||||
def get_default_model(self):
|
||||
return "test-model"
|
||||
|
||||
def estimate_prompt_tokens(self, messages, tools, model):
|
||||
return 10, "controlled-counter"
|
||||
|
||||
async def chat(self, messages, **kwargs):
|
||||
self.requests.append(deepcopy(messages))
|
||||
return LLMResponse(
|
||||
content="Done",
|
||||
usage=LLMUsage.reported(input_tokens=10, output_tokens=1) if self.report_usage else None,
|
||||
)
|
||||
|
||||
async def chat_stream_with_retry(self, **kwargs):
|
||||
return await self.chat(**kwargs)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("phase", ["prompt", "history", "request", "usage"])
|
||||
@pytest.mark.parametrize("cancel", [False, True])
|
||||
async def test_preparation_allows_status_heartbeat_and_cancellation(
|
||||
tmp_path, monkeypatch, loop_factory, phase, cancel,
|
||||
):
|
||||
provider = ControlledProvider(report_usage=phase != "usage")
|
||||
loop = loop_factory(provider=provider)
|
||||
runtime_loop = asyncio.get_running_loop()
|
||||
key = f"cli:prepare-{phase}"
|
||||
live = loop.sessions.get_or_create(key)
|
||||
live.messages = [
|
||||
{"role": "user", "content": [{"type": "text", "text": "Earlier question"}]},
|
||||
{"role": "assistant", "content": "Earlier answer"},
|
||||
*[
|
||||
{"role": role, "content": "long history " * 256}
|
||||
for _ in range(128)
|
||||
for role in ("user", "assistant")
|
||||
],
|
||||
]
|
||||
loop.sessions.save(live)
|
||||
entered = asyncio.Event()
|
||||
finished = asyncio.Event()
|
||||
release = threading.Event()
|
||||
captured = []
|
||||
finalized = []
|
||||
ticks = []
|
||||
ticking = True
|
||||
|
||||
class Lifecycle(AgentHook):
|
||||
async def on_finally(self, context):
|
||||
finalized.append((
|
||||
context.stop_reason, current_request_context(),
|
||||
asyncio.get_running_loop(), context.messages,
|
||||
))
|
||||
if context.messages:
|
||||
context.messages[0]["content"] = "hook-only"
|
||||
|
||||
async def heartbeat():
|
||||
while ticking:
|
||||
ticks.append(runtime_loop.time())
|
||||
await asyncio.sleep(0.01)
|
||||
|
||||
def blocked(operation, *args, **kwargs):
|
||||
captured.append((current_request_context(), current_workspace_scope(), threading.get_ident()))
|
||||
runtime_loop.call_soon_threadsafe(entered.set)
|
||||
try:
|
||||
assert release.wait(5), "test did not release prompt preparation"
|
||||
return operation(*args, **kwargs)
|
||||
finally:
|
||||
runtime_loop.call_soon_threadsafe(finished.set)
|
||||
|
||||
if phase == "prompt":
|
||||
original = loop.context.build_system_prompt
|
||||
|
||||
def build(**kwargs):
|
||||
request = current_request_context()
|
||||
if request is not None and request.session_key == key:
|
||||
return blocked(original, **kwargs)
|
||||
return original(**kwargs)
|
||||
|
||||
monkeypatch.setattr(loop.context, "build_system_prompt", build)
|
||||
elif phase == "history":
|
||||
original = Session.get_history
|
||||
|
||||
def history(session, *args, **kwargs):
|
||||
if session.key == key:
|
||||
assert session is not live
|
||||
return blocked(original, session, *args, **kwargs)
|
||||
return original(session, *args, **kwargs)
|
||||
|
||||
monkeypatch.setattr(Session, "get_history", history)
|
||||
else:
|
||||
module = "context_governance" if phase == "request" else "runner"
|
||||
from nanobot.utils.helpers import estimate_prompt_tokens_chain
|
||||
|
||||
def count(*args, **kwargs):
|
||||
request = current_request_context()
|
||||
if request is not None and request.session_key == key:
|
||||
return blocked(estimate_prompt_tokens_chain, *args, **kwargs)
|
||||
return estimate_prompt_tokens_chain(*args, **kwargs)
|
||||
|
||||
monkeypatch.setattr(f"nanobot.agent.{module}.estimate_prompt_tokens_chain", count)
|
||||
|
||||
task = asyncio.create_task(loop.process_direct("Hello", session_key=key, hooks=[Lifecycle()]))
|
||||
pulse = asyncio.create_task(heartbeat())
|
||||
tasks = [task, pulse]
|
||||
try:
|
||||
await asyncio.wait_for(entered.wait(), timeout=5)
|
||||
before = len(ticks)
|
||||
status = await asyncio.wait_for(loop.process_direct("/status", session_key="cli:status"), 0.5)
|
||||
assert status is not None and "test-model" in status.content
|
||||
await asyncio.sleep(0.04)
|
||||
assert len(ticks) >= before + 2
|
||||
assert not finished.is_set()
|
||||
assert captured[0][2] != threading.get_ident()
|
||||
if phase != "history":
|
||||
request, scope, _ = captured[0]
|
||||
assert request.session_key == key
|
||||
assert scope.project_path == tmp_path.resolve()
|
||||
assert current_request_context() is None
|
||||
assert current_workspace_scope() is None
|
||||
expected_requests = 1 if phase == "usage" else 0
|
||||
assert len(provider.requests) == expected_requests
|
||||
if cancel:
|
||||
task.cancel()
|
||||
with pytest.raises(asyncio.CancelledError):
|
||||
await asyncio.wait_for(task, 0.5)
|
||||
assert not finished.is_set()
|
||||
if phase != "history":
|
||||
assert len(finalized) == 1
|
||||
reason, request, final_loop, snapshot = finalized[0]
|
||||
assert reason == "cancelled"
|
||||
assert request.session_key == key
|
||||
assert final_loop is runtime_loop
|
||||
if phase != "prompt":
|
||||
assert snapshot
|
||||
# A new session queues behind the physical worker, even after its caller exits.
|
||||
waiting = asyncio.create_task(loop.process_direct("Queued", session_key="cli:waiting"))
|
||||
tasks.append(waiting)
|
||||
await asyncio.sleep(0.04)
|
||||
assert len(provider.requests) == expected_requests
|
||||
assert not waiting.done()
|
||||
waiting.cancel()
|
||||
with pytest.raises(asyncio.CancelledError):
|
||||
await asyncio.wait_for(waiting, 0.5)
|
||||
elif phase == "history":
|
||||
# The live nested payload may change after capture; the worker owns its copy.
|
||||
live.messages[0]["content"][0]["text"] = "Changed after capture"
|
||||
release.set()
|
||||
await asyncio.wait_for(finished.wait(), 5)
|
||||
if cancel:
|
||||
result = await asyncio.wait_for(loop.process_direct("After", session_key="cli:after"), 5)
|
||||
assert result is not None and result.content == "Done"
|
||||
assert len(provider.requests) == expected_requests + 1
|
||||
assert provider.requests[-1][-1]["content"].startswith("After")
|
||||
else:
|
||||
result = await asyncio.wait_for(task, 5)
|
||||
assert result is not None and result.content == "Done"
|
||||
assert len(provider.requests) == 1
|
||||
assert provider.requests[0][1]["content"][0]["text"] == "Earlier question"
|
||||
saved = loop.sessions.get_or_create(key)
|
||||
assert [public_history_message(message)["content"] for message in saved.messages[-2:]] == ["Hello", "Done"]
|
||||
assert current_request_context() is None
|
||||
assert current_workspace_scope() is None
|
||||
finally:
|
||||
ticking = False
|
||||
release.set()
|
||||
for pending in tasks:
|
||||
if not pending.done():
|
||||
pending.cancel()
|
||||
await asyncio.gather(*tasks, return_exceptions=True)
|
||||
if captured:
|
||||
await asyncio.wait_for(finished.wait(), 5)
|
||||
await loop.aclose()
|
||||
|
||||
|
||||
@pytest.mark.parametrize("phase", ["success_snapshot", "finally_snapshot"])
|
||||
async def test_cancellation_during_run_cleanup_delivers_finally_once(
|
||||
tmp_path, monkeypatch, loop_factory, phase,
|
||||
):
|
||||
provider = ControlledProvider()
|
||||
loop = loop_factory(provider=provider)
|
||||
prompt_file = tmp_path / "AGENTS.md"
|
||||
prompt_file.write_text("Lifecycle test instructions", encoding="utf-8")
|
||||
session = loop.sessions.get_or_create("cli:first")
|
||||
session.messages = [
|
||||
{"role": "user", "content": [{"type": "text", "text": "Earlier question"}]},
|
||||
{"role": "assistant", "content": "Earlier answer"},
|
||||
]
|
||||
loop.sessions.save(session)
|
||||
runtime_loop = asyncio.get_running_loop()
|
||||
entered = asyncio.Event()
|
||||
hook_returned = asyncio.Event()
|
||||
worker_finished = asyncio.Event()
|
||||
release = threading.Event()
|
||||
events = []
|
||||
finals = []
|
||||
raw_messages = []
|
||||
blockers = []
|
||||
|
||||
class Lifecycle(AgentHook):
|
||||
async def start_other_session(self):
|
||||
blockers.append(asyncio.create_task(
|
||||
loop.process_direct("Blocked", session_key="cli:blocker"),
|
||||
))
|
||||
await asyncio.wait_for(entered.wait(), 5)
|
||||
hook_returned.set()
|
||||
|
||||
async def before_run(self, context):
|
||||
events.append("before_run")
|
||||
|
||||
async def after_iteration(self, context):
|
||||
raw_messages.append(context.messages)
|
||||
events.append("after_iteration")
|
||||
if phase == "success_snapshot":
|
||||
await self.start_other_session()
|
||||
|
||||
async def after_run(self, context):
|
||||
events.append("after_run")
|
||||
if phase == "finally_snapshot":
|
||||
await self.start_other_session()
|
||||
|
||||
async def on_error(self, context):
|
||||
events.append("on_error")
|
||||
|
||||
async def on_finally(self, context):
|
||||
events.append("on_finally")
|
||||
finals.append(context)
|
||||
assert context.messages is not raw_messages[0]
|
||||
context.messages[1]["content"][0]["text"] = "hook-only"
|
||||
|
||||
original_read = type(prompt_file).read_text
|
||||
|
||||
def read_text(path, *args, **kwargs):
|
||||
request = current_request_context()
|
||||
if path == prompt_file and request is not None and request.session_key == "cli:blocker":
|
||||
runtime_loop.call_soon_threadsafe(entered.set)
|
||||
try:
|
||||
assert release.wait(5), "test did not release the other session's prompt read"
|
||||
return original_read(path, *args, **kwargs)
|
||||
finally:
|
||||
runtime_loop.call_soon_threadsafe(worker_finished.set)
|
||||
return original_read(path, *args, **kwargs)
|
||||
|
||||
monkeypatch.setattr(type(prompt_file), "read_text", read_text)
|
||||
first = asyncio.create_task(loop.process_direct("First", session_key="cli:first", hooks=[Lifecycle()]))
|
||||
try:
|
||||
await asyncio.wait_for(hook_returned.wait(), 5)
|
||||
await asyncio.sleep(0)
|
||||
first.cancel()
|
||||
with pytest.raises(asyncio.CancelledError):
|
||||
await asyncio.wait_for(first, 0.5)
|
||||
assert not worker_finished.is_set()
|
||||
assert len(provider.requests) == 1
|
||||
assert provider.requests[0][-1]["content"].startswith("First")
|
||||
assert len(finals) == 1
|
||||
assert finals[0].stop_reason == "cancelled"
|
||||
assert isinstance(finals[0].exception, asyncio.CancelledError)
|
||||
assert finals[0].error is None
|
||||
assert len(finals[0].messages) == 5
|
||||
assert raw_messages[0][1]["content"][0]["text"] == "Earlier question"
|
||||
assert events == [
|
||||
"before_run", "after_iteration",
|
||||
*(["after_run"] if phase == "finally_snapshot" else []),
|
||||
"on_finally",
|
||||
]
|
||||
assert current_request_context() is None
|
||||
assert current_workspace_scope() is None
|
||||
finally:
|
||||
for task in [first, *blockers]:
|
||||
if not task.done():
|
||||
task.cancel()
|
||||
release.set()
|
||||
await asyncio.gather(first, *blockers, return_exceptions=True)
|
||||
if entered.is_set():
|
||||
await asyncio.wait_for(worker_finished.wait(), 5)
|
||||
await loop.aclose()
|
||||
@@ -1,7 +1,6 @@
|
||||
"""Tests for subagent tool registration and wiring."""
|
||||
|
||||
import asyncio
|
||||
from types import SimpleNamespace
|
||||
from unittest.mock import AsyncMock, MagicMock, patch
|
||||
|
||||
import pytest
|
||||
@@ -768,7 +767,8 @@ async def test_terminal_drain_reuses_one_timeout_budget(tmp_path):
|
||||
content="result",
|
||||
)
|
||||
|
||||
fake_loop = SimpleNamespace(time=clock)
|
||||
fake_loop = MagicMock(wraps=asyncio.get_running_loop())
|
||||
fake_loop.time = clock
|
||||
with (
|
||||
patch("nanobot.agent.loop.asyncio.get_running_loop", return_value=fake_loop),
|
||||
patch("nanobot.agent.loop.asyncio.wait_for", side_effect=_deliver),
|
||||
|
||||
Reference in new issue
Block a user