refactor(goal): sustain one ReAct run until the goal closes (#6538)

* refactor(goal): sustain one ReAct run until the goal closes

Keep active goals in the same runner beyond the ordinary iteration limit, with a final answer after successful closure. Remove cross-turn goal handoffs, continuation allowances and rollback bookkeeping while preserving input authorization, recovery, cancellation and terminal lifecycle ownership.

Contributes to NAN-392

Co-authored-by: chengyongru <chengyongru.ai@gmail.com>

* test(goal): consolidate sustained-run regression coverage

Retain lifecycle, history, usage, provider-state privacy and checkpoint cancellation assertions in the existing long-run scenarios. Remove duplicate smoke cases and parameter combinations that do not exercise distinct behavior; keep real Gateway, authorization and generation regressions.
This commit is contained in:
chengyongruandchengyongru authored and GitHub committed 2026-11-03 22:47:00 +08:00
1 parent 3545539128
commit 62c2ed4e7e
68 files changed
+2683 -1421

No files matched your search

+25 -15
View File
@@ -2,7 +2,6 @@
from __future__ import annotations
import asyncio
import json
from copy import deepcopy
from typing import Any
@@ -11,8 +10,8 @@ from unittest.mock import AsyncMock
import pytest
from nanobot.agent.loop import AgentLoop
from nanobot.bus.events import InboundMessage
from nanobot.bus.queue import MessageBus
from nanobot.goal.assembly import install_goal
from nanobot.providers.base import LLMProvider, LLMResponse, ToolCallRequest
from nanobot.runtime_context import public_history_messages
from nanobot.session.goal_state import GOAL_STATE_KEY
@@ -55,6 +54,7 @@ def _make_loop(tmp_path, requests: list[list[dict[str, Any]]]) -> AgentLoop:
model="test-model",
context_window_tokens=128_000,
max_iterations=2,
builtin_assembly=install_goal,
)
exec_tool = loop.tools.get("exec")
assert exec_tool is not None
@@ -115,28 +115,38 @@ async def test_stop_survives_restart_and_keeps_history_prefix(tmp_path):
await loop.aclose()
async def test_goal_budget_handoff_is_not_an_abandoned_turn(tmp_path):
async def test_goal_completed_after_iteration_limit_is_not_an_abandoned_turn(tmp_path):
requests: list[list[dict[str, Any]]] = []
loop = _make_loop(tmp_path, requests)
pending: asyncio.Queue[InboundMessage] = asyncio.Queue()
chat = loop.provider.chat
async def complete_after_overrun(messages, tools=None, **kwargs):
if len(requests) == 3:
requests.append(deepcopy(messages))
return LLMResponse(content=None, tool_calls=[ToolCallRequest(
id="complete", name="update_goal", arguments={"action": "complete", "recap": "Verified."},
)])
return await chat(messages, tools=tools, **kwargs)
loop.provider.chat = complete_after_overrun
loop.runner.run = AsyncMock(wraps=loop.runner.run)
try:
await loop.sessions.state.get("cli:goal")
await loop.sessions.state.update_metadata("cli:goal", {
GOAL_STATE_KEY: {"status": "active", "objective": "run the tests"},
})
await loop._process_message(
InboundMessage(channel="cli", sender_id="user", chat_id="goal", content="run the tests"),
pending_queue=pending,
)
continuation = pending.get_nowait()
assert continuation.sender_id == "system:continuation"
result = await loop.process_direct("run the tests", session_key="cli:goal")
assert result.content == "done"
assert len(requests) == 5
loop.runner.run.assert_awaited_once()
session = await loop.sessions.state.read("cli:goal")
assert session.metadata[GOAL_STATE_KEY]["status"] == "completed"
assert [row["tool_call_id"] for row in session.messages if row["role"] == "tool"] == [
"call_1", "call_2", "call_3", "complete",
]
next_request = len(requests)
await loop.process_direct("give me a progress update", session_key="cli:goal")
assert _STOP_MARKER not in _last_user_content(requests[next_request])
next_request = len(requests)
await loop._process_message(continuation, pending_queue=pending)
assert _STOP_MARKER not in _last_user_content(requests[next_request])
assert _STOP_MARKER not in _last_user_content(requests[-1])
finally:
await loop.aclose()