fix(agent): preserve goal continuation lifecycle
This commit is contained in:
1 parent
3c022ada6a
commit
ce885fa31e
5 files changed
+75
-22
No files matched your search
@@ -1514,8 +1514,9 @@ async def test_internal_continuation_preserves_streaming_route_metadata(
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize("content", ["start the goal", "/goal start the goal"])
|
||||
async def test_websocket_internal_continuation_keeps_single_visible_run(
|
||||
tmp_path: Path,
|
||||
tmp_path: Path, content: str,
|
||||
) -> None:
|
||||
loop = _make_full_loop(tmp_path)
|
||||
session = loop.sessions.get_or_create("websocket:c-auto")
|
||||
@@ -1548,7 +1549,7 @@ async def test_websocket_internal_continuation_keeps_single_visible_run(
|
||||
channel="websocket",
|
||||
sender_id="u1",
|
||||
chat_id="c-auto",
|
||||
content="start the goal",
|
||||
content=content,
|
||||
metadata={"webui": True},
|
||||
))
|
||||
|
||||
@@ -1567,6 +1568,71 @@ async def test_websocket_internal_continuation_keeps_single_visible_run(
|
||||
assert calls == 2
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize("content", ["start the goal", "/goal start the goal"])
|
||||
async def test_stop_after_continuation_queued_ends_visible_run(
|
||||
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, content: str,
|
||||
) -> None:
|
||||
from nanobot.command.builtin import cmd_stop
|
||||
from nanobot.command.router import CommandContext
|
||||
|
||||
loop = _make_full_loop(tmp_path)
|
||||
session = loop.sessions.get_or_create("websocket:c-stop-continuation")
|
||||
session.metadata[GOAL_STATE_KEY] = {
|
||||
"status": "active",
|
||||
"objective": "Finish the long goal.",
|
||||
}
|
||||
loop.sessions.save(session)
|
||||
calls = 0
|
||||
|
||||
async def fake_run_agent_loop(transcript_input, **_kwargs):
|
||||
nonlocal calls
|
||||
calls += 1
|
||||
initial_messages = _assembled_messages(loop.context, transcript_input)
|
||||
return _agent_run_result(
|
||||
"paused",
|
||||
[*initial_messages, {"role": "assistant", "content": "paused"}],
|
||||
stop_reason="max_iterations",
|
||||
)
|
||||
|
||||
loop._run_agent_loop = fake_run_agent_loop # type: ignore[method-assign]
|
||||
saving = asyncio.Event()
|
||||
finish_turn = loop.sessions.state.finish_turn
|
||||
|
||||
async def hold_first_save(session):
|
||||
if not saving.is_set():
|
||||
saving.set()
|
||||
await asyncio.Event().wait()
|
||||
await finish_turn(session)
|
||||
|
||||
monkeypatch.setattr(loop.sessions.state, "finish_turn", hold_first_save)
|
||||
msg = InboundMessage(
|
||||
channel="websocket", sender_id="u1", chat_id="c-stop-continuation",
|
||||
content=content, metadata={"webui": True},
|
||||
)
|
||||
await loop._enqueue_session_message(msg)
|
||||
try:
|
||||
await asyncio.wait_for(saving.wait(), timeout=5)
|
||||
assert loop._pending_queues[msg.session_key].qsize() == 1
|
||||
stop_msg = InboundMessage(
|
||||
channel="websocket", sender_id="u1", chat_id=msg.chat_id, content="/stop",
|
||||
)
|
||||
result = await cmd_stop(CommandContext(
|
||||
msg=stop_msg, session=None, key=msg.session_key, raw="/stop", loop=loop,
|
||||
))
|
||||
assert "Stopped 1 task(s)" in result.content
|
||||
finally:
|
||||
await loop._cancel_active_tasks(msg.session_key)
|
||||
|
||||
outbound = []
|
||||
while loop.bus.outbound_size:
|
||||
outbound.append(await loop.bus.consume_outbound())
|
||||
statuses = [m.event.status for m in outbound if isinstance(m.event, GoalStatusEvent)]
|
||||
assert statuses == ["running", "idle"]
|
||||
assert calls == 1
|
||||
assert msg.session_key not in loop._pending_queues
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_process_message_keeps_delivery_chat_for_thread_session(tmp_path: Path) -> None:
|
||||
loop = _make_full_loop(tmp_path)
|
||||
|
||||
@@ -18,10 +18,8 @@ from nanobot.session.recovery import REQUEST_ORIGIN_METADATA_KEY
|
||||
from nanobot.session.turn_continuation import (
|
||||
INTERNAL_CONTINUATION_KIND_META,
|
||||
INTERNAL_CONTINUATION_META,
|
||||
INTERNAL_CONTINUATION_PENDING_META,
|
||||
INTERNAL_CONTINUATION_RUN_STARTED_AT_META,
|
||||
_save_skip_for_turn,
|
||||
internal_continuation_pending,
|
||||
internal_continuation_run_started_at,
|
||||
maybe_continue_turn,
|
||||
should_finalize_on_max_iterations,
|
||||
@@ -82,7 +80,6 @@ async def test_maybe_continue_turn_queues_internal_message():
|
||||
assert sustained_goal_continuation_inbound(queued.metadata) is True
|
||||
assert queued.metadata[INTERNAL_CONTINUATION_RUN_STARTED_AT_META] == 1234.5
|
||||
assert internal_continuation_run_started_at(queued.metadata) == 1234.5
|
||||
assert internal_continuation_pending(ctx.msg.metadata)
|
||||
assert queued.metadata["webui"] is True
|
||||
assert queued.metadata["message_id"] == "msg-1"
|
||||
assert queued.metadata[REQUEST_ORIGIN_METADATA_KEY] == {
|
||||
@@ -97,7 +94,6 @@ async def test_maybe_continue_turn_queues_internal_message():
|
||||
assert ctx.all_messages == messages[:-1]
|
||||
assert ctx.final_content == ""
|
||||
assert ctx.suppress_response is True
|
||||
assert ctx.msg.metadata[INTERNAL_CONTINUATION_PENDING_META] is True
|
||||
assert meta["_sustained_goal_continuation_rounds"] == 1
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user