fix(ai): garantir une reponse finale quand la boucle d'agent epuise son budget (BUG-052)
This commit is contained in:
@@ -182,6 +182,40 @@ class TestConfirmationAndLimits:
|
||||
assert result.iterations == 2
|
||||
assert len(result.tool_calls) == 2
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_max_iterations_synthesizes_final_answer(self, monkeypatch):
|
||||
"""BUG-052: exhausting the budget must still produce an answer."""
|
||||
_register(monkeypatch, "_echo", lambda ctx, params: {"value": 1})
|
||||
llm = ScriptedLLM([
|
||||
LLMResponse(tool_calls=[ToolCall(id="1", name="_echo", arguments={})]),
|
||||
LLMResponse(tool_calls=[ToolCall(id="2", name="_echo", arguments={})]),
|
||||
LLMResponse(content="synthèse finale"),
|
||||
])
|
||||
result = await run_agent(
|
||||
[{"role": "user", "content": "loop"}], ctx=_ctx(), llm=llm, max_iterations=2
|
||||
)
|
||||
assert result.stopped == STOP_MAX_ITERATIONS
|
||||
assert result.content == "synthèse finale"
|
||||
# The last call is tool-less and carries the synthesis instruction.
|
||||
assert llm.calls[-1]["tools"] == []
|
||||
assert "N'appelle plus aucun outil" in llm.calls[-1]["messages"][-1]["content"]
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_max_iterations_falls_back_to_sources(self, monkeypatch):
|
||||
"""An empty/failed synthesis still returns the gathered sources."""
|
||||
_register(monkeypatch, "_search", lambda ctx, params: {
|
||||
"results": [{"title": "T", "url": "https://ex.dev/a"}]
|
||||
})
|
||||
llm = ScriptedLLM([
|
||||
LLMResponse(tool_calls=[ToolCall(id="1", name="_search", arguments={})]),
|
||||
LLMResponse(content=""),
|
||||
])
|
||||
result = await run_agent(
|
||||
[{"role": "user", "content": "loop"}], ctx=_ctx(), llm=llm, max_iterations=1
|
||||
)
|
||||
assert result.stopped == STOP_MAX_ITERATIONS
|
||||
assert "https://ex.dev/a" in result.content
|
||||
|
||||
|
||||
# ═══════════════════════════════════════════════════════════════════
|
||||
# Permissions (index-backed)
|
||||
|
||||
Reference in New Issue
Block a user