refactor(agent): clarify run-local input and checkpoint ownership
This commit is contained in:
1 parent
e6b23c7356
commit
f94f245dca
7 files changed
+327
-213
No files matched your search
@@ -2017,3 +2017,100 @@ async def test_error_path_is_not_stopped_by_an_arbitrary_injection_cap():
|
||||
|
||||
assert result.had_injections is True
|
||||
assert call_count["n"] == 8
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_cancelling_input_conversion_preserves_other_concurrent_run(tmp_path):
|
||||
from nanobot.agent.loop import AgentLoop
|
||||
from nanobot.agent.tools.context import current_request_context, request_context
|
||||
from nanobot.bus.events import InboundMessage
|
||||
from nanobot.bus.queue import MessageBus
|
||||
|
||||
provider = MagicMock(aclose=AsyncMock())
|
||||
provider.get_default_model.return_value = "test-model"
|
||||
requests = []
|
||||
|
||||
async def respond(*, messages, **kwargs):
|
||||
requests.append((current_request_context().session_key, [dict(row) for row in messages]))
|
||||
# Stop the first execution while the second is inside its model request.
|
||||
task.cancel()
|
||||
with pytest.raises(asyncio.CancelledError):
|
||||
await task
|
||||
assert current_request_context().session_key == "cli:second"
|
||||
return LLMResponse(content="second run finished")
|
||||
|
||||
provider.chat_stream_with_retry = respond
|
||||
loop = AgentLoop(bus=MessageBus(), provider=provider, workspace=tmp_path, model="test-model")
|
||||
loop.tools.get_definitions = MagicMock(return_value=[])
|
||||
conversion_started = asyncio.Event()
|
||||
never_release = asyncio.Event()
|
||||
prepared_for = []
|
||||
|
||||
async def prepare_context(request):
|
||||
prepared_for.append((request.session_key, request.original_user_text))
|
||||
assert current_request_context().session_key == request.session_key
|
||||
if request.original_user_text == "first private input":
|
||||
conversion_started.set()
|
||||
await never_release.wait()
|
||||
return None
|
||||
|
||||
loop.register_runtime_context_provider(prepare_context)
|
||||
runtime = loop.llm_runtime()
|
||||
first, second = asyncio.Queue(), asyncio.Queue()
|
||||
for content in ["first private input", "first tail"]:
|
||||
first.put_nowait(InboundMessage("cli", "u", "first", content))
|
||||
for content in ["second input", "second tail"]:
|
||||
second.put_nowait(InboundMessage("cli", "u", "second", content))
|
||||
outer = RequestContext(channel="test", chat_id="outer", session_key="test:outer")
|
||||
restored = []
|
||||
|
||||
async def first_run():
|
||||
try:
|
||||
return await loop._run_agent_loop(
|
||||
TranscriptInput(history=[{"role": "user", "content": "first root"}], current_message=None),
|
||||
runtime=runtime,
|
||||
request_context=RequestContext(channel="cli", chat_id="first", session_key="cli:first", runtime=runtime),
|
||||
pending_queue=first,
|
||||
)
|
||||
finally:
|
||||
restored.append(current_request_context())
|
||||
|
||||
task = None
|
||||
try:
|
||||
with request_context(outer):
|
||||
task = asyncio.create_task(first_run())
|
||||
await asyncio.wait_for(conversion_started.wait(), timeout=5)
|
||||
first.put_nowait(InboundMessage("cli", "u", "first", "first later arrival"))
|
||||
result = await loop._run_agent_loop(
|
||||
TranscriptInput(history=[{"role": "user", "content": "second root"}], current_message=None),
|
||||
runtime=runtime,
|
||||
request_context=RequestContext(channel="cli", chat_id="second", session_key="cli:second", runtime=runtime),
|
||||
pending_queue=second,
|
||||
)
|
||||
assert current_request_context() is outer
|
||||
|
||||
assert result.final_content == "second run finished"
|
||||
assert restored == [outer]
|
||||
assert [first.get_nowait().content for _ in range(first.qsize())] == [
|
||||
"first private input", "first tail", "first later arrival",
|
||||
]
|
||||
assert second.empty()
|
||||
assert prepared_for == [
|
||||
("cli:first", "first private input"),
|
||||
("cli:second", "second input"),
|
||||
("cli:second", "second tail"),
|
||||
]
|
||||
assert len(requests) == 1
|
||||
key, messages = requests[0]
|
||||
assert key == "cli:second"
|
||||
assert [visible_message(row)["content"] for row in messages if row["role"] == "user"] == [
|
||||
"second root\n\nsecond input\n\nsecond tail",
|
||||
]
|
||||
assert [visible_message(row)["content"] for row in result.messages if row["role"] == "user"] == [
|
||||
"second root", "second input", "second tail",
|
||||
]
|
||||
finally:
|
||||
if task is not None and not task.done():
|
||||
task.cancel()
|
||||
await asyncio.gather(task, return_exceptions=True)
|
||||
await loop.aclose()
|
||||
@@ -730,7 +730,7 @@ async def test_terminal_drain_timeout(tmp_path):
|
||||
awaitable.close()
|
||||
raise asyncio.TimeoutError
|
||||
|
||||
with patch("nanobot.agent.loop.asyncio.wait_for", side_effect=_timeout):
|
||||
with patch("nanobot.agent.pending_inputs.asyncio.wait_for", side_effect=_timeout):
|
||||
results = await terminal_injection_callback()
|
||||
assert results == []
|
||||
|
||||
@@ -800,10 +800,10 @@ async def test_terminal_drain_reuses_one_timeout_budget(tmp_path):
|
||||
loop.tools.get_runtime_context_providers = MagicMock(return_value=[])
|
||||
fake_loop = MagicMock(wraps=asyncio.get_running_loop())
|
||||
fake_loop.time = clock
|
||||
loop_asyncio = SimpleNamespace(**vars(asyncio))
|
||||
loop_asyncio.get_running_loop = lambda: fake_loop
|
||||
loop_asyncio.wait_for = _deliver
|
||||
with patch("nanobot.agent.loop.asyncio", loop_asyncio):
|
||||
pending_asyncio = SimpleNamespace(**vars(asyncio))
|
||||
pending_asyncio.get_running_loop = lambda: fake_loop
|
||||
pending_asyncio.wait_for = _deliver
|
||||
with patch("nanobot.agent.pending_inputs.asyncio", pending_asyncio):
|
||||
assert await terminal_injection_callback()
|
||||
assert await terminal_injection_callback()
|
||||
|
||||
|
||||
Reference in new issue
Block a user