fix(api): don't re-persist user turn on empty-response retry (#4079)

The non-streaming retry called process_direct again with the same
content, persisting a duplicate user turn. Pass persist_user_message=False
so the retry recovers a response without re-recording the user message.
This commit is contained in:
04cb
2026-06-16 21:31:26 +08:00
committed by Xubin Ren
parent 25a55fe1c7
commit d75f80437c
4 changed files with 35 additions and 1 deletions
+26
View File
@@ -400,6 +400,32 @@ async def test_empty_response_retry_then_success(aiohttp_client) -> None:
assert call_count == 2
@pytest.mark.skipif(not HAS_AIOHTTP, reason="aiohttp not installed")
@pytest.mark.asyncio
async def test_empty_response_retry_does_not_duplicate_user_turn(aiohttp_client) -> None:
persist_flags = []
async def record(content, session_key="", channel="", chat_id="", **kwargs):
persist_flags.append(kwargs.get("persist_user_message", True))
return "" if len(persist_flags) == 1 else "recovered response"
agent = MagicMock()
agent.process_direct = record
agent._connect_mcp = AsyncMock()
agent.close_mcp = AsyncMock()
agent._last_usage = {}
app = create_app(agent, model_name="m")
client = await aiohttp_client(app)
resp = await client.post(
"/v1/chat/completions",
json={"messages": [{"role": "user", "content": "hello"}]},
)
assert resp.status == 200
# first call persists the user turn; the retry must not persist it again
assert persist_flags == [True, False]
@pytest.mark.skipif(not HAS_AIOHTTP, reason="aiohttp not installed")
@pytest.mark.asyncio
async def test_empty_response_falls_back(aiohttp_client) -> None: