fix(agent): preserve length-recovered output

This commit is contained in:
chengyongru
2026-07-27 01:39:46 +08:00
committed by Xubin Ren
parent c1899e2cb4
commit b19039f9d0
4 changed files with 180 additions and 1 deletions
+36
View File
@@ -352,6 +352,42 @@ async def test_checkpoint2_injects_after_final_response_with_resuming_stream():
assert stream_end_calls[-1] is False
@pytest.mark.asyncio
async def test_injected_followup_starts_new_length_recovery_chain():
"""Recovered content from the prior answer must not prefix a follow-up reply."""
from nanobot.agent.runner import AgentRunner
from nanobot.bus.events import InboundMessage
provider = MagicMock()
provider.chat_with_retry = AsyncMock(side_effect=[
LLMResponse(content="first", finish_reason="length"),
LLMResponse(content="second", finish_reason="stop"),
LLMResponse(content="follow-up answer", finish_reason="stop"),
])
tools = MagicMock()
tools.get_definitions.return_value = []
injection_queue = asyncio.Queue()
inject_cb = _make_injection_callback(injection_queue)
await injection_queue.put(
InboundMessage(channel="cli", sender_id="u", chat_id="c", content="follow-up question")
)
runner = AgentRunner()
result = await runner.run(make_run_spec(provider,
initial_messages=[{"role": "user", "content": "give a long answer"}],
tools=tools,
model="test-model",
max_iterations=5,
max_tool_result_chars=_MAX_TOOL_RESULT_CHARS,
injection_callback=inject_cb,
))
assert result.had_injections is True
assert result.final_content == "follow-up answer"
assert provider.chat_with_retry.await_count == 3
@pytest.mark.asyncio
async def test_checkpoint2_preserves_final_response_in_history_before_followup():
"""A follow-up injected after a final answer must still see that answer in history."""