fix(agent): run explicit recovery continuations
This commit is contained in:
1 parent
78cdf53ac3
commit
078e35f317
4 files changed
+79
-1
No files matched your search
@@ -1493,7 +1493,7 @@ class AgentLoop:
|
|||||||
async with lock, gate:
|
async with lock, gate:
|
||||||
# A preceding user turn may have completed or blocked the goal
|
# A preceding user turn may have completed or blocked the goal
|
||||||
# while its older continuation was still waiting in the inbox.
|
# while its older continuation was still waiting in the inbox.
|
||||||
if turn_continuation.internal_continuation_inbound(msg.metadata) and not (
|
if turn_continuation.sustained_goal_continuation_inbound(msg.metadata) and not (
|
||||||
sustained_goal_active(self.sessions.get_or_create(session_key).metadata)
|
sustained_goal_active(self.sessions.get_or_create(session_key).metadata)
|
||||||
):
|
):
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -43,6 +43,15 @@ def internal_continuation_inbound(metadata: Mapping[str, Any] | None) -> bool:
|
|||||||
return bool(metadata and metadata.get(INTERNAL_CONTINUATION_META) is True)
|
return bool(metadata and metadata.get(INTERNAL_CONTINUATION_META) is True)
|
||||||
|
|
||||||
|
|
||||||
|
def sustained_goal_continuation_inbound(metadata: Mapping[str, Any] | None) -> bool:
|
||||||
|
"""True only for an internal continuation owned by sustained-goal policy."""
|
||||||
|
return bool(
|
||||||
|
internal_continuation_inbound(metadata)
|
||||||
|
and metadata
|
||||||
|
and metadata.get(INTERNAL_CONTINUATION_KIND_META) == _GOAL_CONTINUATION_KIND
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def internal_continuation_pending(metadata: Mapping[str, Any] | None) -> bool:
|
def internal_continuation_pending(metadata: Mapping[str, Any] | None) -> bool:
|
||||||
"""True when the current turn scheduled an invisible continuation slice."""
|
"""True when the current turn scheduled an invisible continuation slice."""
|
||||||
return bool(metadata and metadata.get(INTERNAL_CONTINUATION_PENDING_META) is True)
|
return bool(metadata and metadata.get(INTERNAL_CONTINUATION_PENDING_META) is True)
|
||||||
|
|||||||
@@ -0,0 +1,63 @@
|
|||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import asyncio
|
||||||
|
from pathlib import Path
|
||||||
|
from unittest.mock import AsyncMock
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from nanobot.agent.loop import AgentLoop
|
||||||
|
from nanobot.agent.turn_delivery import TurnDeliveryFactory
|
||||||
|
from nanobot.bus.events import OutboundMessage
|
||||||
|
from nanobot.bus.queue import MessageBus
|
||||||
|
from nanobot.session.manager import SessionManager
|
||||||
|
from nanobot.session.recovery import RECOVERY_METADATA_KEY, RecoveryCoordinator
|
||||||
|
from nanobot.session.turn_continuation import INTERNAL_CONTINUATION_META
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_recovery_continuation_runs_without_a_sustained_goal(
|
||||||
|
tmp_path: Path,
|
||||||
|
monkeypatch: pytest.MonkeyPatch,
|
||||||
|
) -> None:
|
||||||
|
bus = MessageBus()
|
||||||
|
sessions = SessionManager(tmp_path)
|
||||||
|
session = sessions.get_or_create("websocket:chat")
|
||||||
|
session.metadata.update({
|
||||||
|
"webui": True,
|
||||||
|
RECOVERY_METADATA_KEY: {
|
||||||
|
"status": "awaiting_user",
|
||||||
|
"recovery_id": "recovery-1",
|
||||||
|
"attempts": 0,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
sessions.save(session)
|
||||||
|
recovery = RecoveryCoordinator(sessions, bus)
|
||||||
|
|
||||||
|
loop = AgentLoop.__new__(AgentLoop)
|
||||||
|
loop.sessions = sessions
|
||||||
|
loop._unified_session = False
|
||||||
|
loop._recovery_admission = recovery
|
||||||
|
loop._session_locks = {}
|
||||||
|
loop._concurrency_gate = None
|
||||||
|
loop._automation_turn_coordinators = []
|
||||||
|
loop._discarding_sessions = set()
|
||||||
|
loop._preserve_inflight_turns_on_shutdown = False
|
||||||
|
loop.turn_delivery_factory = TurnDeliveryFactory(bus)
|
||||||
|
process_message = AsyncMock(return_value=OutboundMessage(
|
||||||
|
channel="websocket",
|
||||||
|
chat_id="chat",
|
||||||
|
content="done",
|
||||||
|
))
|
||||||
|
monkeypatch.setattr(loop, "_process_message", process_message)
|
||||||
|
|
||||||
|
await recovery.handle_action(
|
||||||
|
"continue",
|
||||||
|
{"chat_id": "chat", "recovery_id": "recovery-1"},
|
||||||
|
)
|
||||||
|
continuation = bus.inbound.get_nowait()
|
||||||
|
|
||||||
|
assert continuation.metadata[INTERNAL_CONTINUATION_META] is True
|
||||||
|
await loop._dispatch_one(continuation, asyncio.Queue())
|
||||||
|
|
||||||
|
process_message.assert_awaited_once()
|
||||||
@@ -24,6 +24,7 @@ from nanobot.session.turn_continuation import (
|
|||||||
maybe_continue_turn,
|
maybe_continue_turn,
|
||||||
should_finalize_on_max_iterations,
|
should_finalize_on_max_iterations,
|
||||||
should_stream_budget_response,
|
should_stream_budget_response,
|
||||||
|
sustained_goal_continuation_inbound,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@@ -73,6 +74,7 @@ async def test_maybe_continue_turn_queues_internal_message():
|
|||||||
assert queued.sender_id == "system:continuation"
|
assert queued.sender_id == "system:continuation"
|
||||||
assert queued.metadata[INTERNAL_CONTINUATION_META] is True
|
assert queued.metadata[INTERNAL_CONTINUATION_META] is True
|
||||||
assert queued.metadata[INTERNAL_CONTINUATION_KIND_META] == "sustained_goal"
|
assert queued.metadata[INTERNAL_CONTINUATION_KIND_META] == "sustained_goal"
|
||||||
|
assert sustained_goal_continuation_inbound(queued.metadata) is True
|
||||||
assert queued.metadata[INTERNAL_CONTINUATION_RUN_STARTED_AT_META] == 1234.5
|
assert queued.metadata[INTERNAL_CONTINUATION_RUN_STARTED_AT_META] == 1234.5
|
||||||
assert internal_continuation_run_started_at(queued.metadata) == 1234.5
|
assert internal_continuation_run_started_at(queued.metadata) == 1234.5
|
||||||
assert internal_continuation_pending(ctx.msg.metadata)
|
assert internal_continuation_pending(ctx.msg.metadata)
|
||||||
@@ -90,6 +92,10 @@ async def test_maybe_continue_turn_queues_internal_message():
|
|||||||
assert meta["_sustained_goal_continuation_rounds"] == 1
|
assert meta["_sustained_goal_continuation_rounds"] == 1
|
||||||
|
|
||||||
|
|
||||||
|
def test_generic_internal_continuation_is_not_a_sustained_goal_continuation() -> None:
|
||||||
|
assert sustained_goal_continuation_inbound({INTERNAL_CONTINUATION_META: True}) is False
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_internal_continuation_respects_round_limit():
|
async def test_internal_continuation_respects_round_limit():
|
||||||
meta = {
|
meta = {
|
||||||
|
|||||||
Reference in new issue
Block a user