Files
DeepTutor/tests/agents/chat/test_mastery_source_request_boundary.py
zszz3 246aa1426b fix(chat): keep the loop's tools in the forced final round
Prompt caches key on tools, then system, then messages. Every round of a
chat-loop turn sends the loop's tool schemas, but the forced finish (round
budget spent) sent the request with tool_schemas=None. That changed the
very start of the request, so the whole turn history (often 10k-60k
tokens) missed the cache on the final round. tool_choice="none" is no
fix: on DeepSeek it caches exactly like a request without tools.

The forced finish now sends the same tool list the last normal round
sent (taken from the request snapshot, since the deferred tool loader
appends to the live list after a request goes out) with tool_choice left
at auto; the finish instruction already tells the model to stop calling
tools. A tool call it returns anyway is never dispatched, and no ask_user
draft card is published for it. The round is kept in the history as text
only (a call without a result is rejected by the next request; Responses
function_call items are dropped too), a repair note asks for a plain-text
answer, and one more attempt runs. If that also calls a tool, the turn
ends with the existing empty-response fallback. The forced finish still
counts as one loop round. The persisted model_turn tools and the context
budget now reflect what the final request really carried.

The context-exploration pre-pass had the same pattern on its last round
(tools dropped); it now keeps the tools, does not run a tool call there,
repairs once, and otherwise degrades to the existing single pass.

The finish instructions (chat, mastery, explore_context; en/zh) now say
the tools are still listed but no call will run. Same approach as the
staged pipelines' forced finish (#3080).

Refs #3182
2026-12-02 16:58:58 +08:00

131 lines
5.1 KiB
Python

"""Source preparation must actually run at the native model-request boundary."""
import json
import pytest
from deeptutor.agents.chat.agentic_pipeline import AgenticChatPipeline
from deeptutor.agents.loop import agent_loop as loop_module
from deeptutor.core.capability_protocol import CapabilityPrerequisiteError
from deeptutor.core.context import UnifiedContext
from deeptutor.learning.lesson_evidence import LessonUnavailable
from deeptutor.services.session.model_history import normalize_model_turn
from tests.agents.chat.test_agent_loop import _isolated_runtime as _isolated_runtime
from tests.agents.chat.test_agent_loop import _llm_chunk, _Registry, _run, _ScriptedChatClient
def pixels(label):
return {
"role": "user",
"content": [{"type": "image_url", "image_url": {"url": "data:image/png;base64," + label}}],
}
@pytest.mark.asyncio
@pytest.mark.parametrize("forced_finish", [False, True])
async def test_native_boundary_refreshes_each_round_and_keeps_tool_pixels_ephemeral(
monkeypatch, forced_finish
):
class Registry(_Registry):
async def execute(self, name, **kwargs):
result = await super().execute(name, **kwargs)
result.model_message = pixels("fresh-tool-image")
return result
client = _ScriptedChatClient(
[
[
_llm_chunk(
tool_calls=[
{"id": "inspect", "name": "web_search", "arguments": '{"query":"figure"}'}
]
)
],
[_llm_chunk(content="A supported explanation.")],
]
)
pipeline = AgenticChatPipeline(language="en")
pipeline.registry = Registry()
monkeypatch.setattr(pipeline, "_compose_enabled_tools", lambda _: ["web_search"])
monkeypatch.setattr(pipeline, "_build_openai_client", lambda: client)
if forced_finish:
pipeline._max_rounds = 1
monkeypatch.setattr(loop_module, "MAX_SETTLEMENT_ROUNDS", 0)
checks = []
async def prepare(context, stream, messages):
checks.append(True)
messages[:] = [m for m in messages if not m.get("_lesson_packet")]
messages.append(
{"role": "user", "content": "current authorized source packet", "_lesson_packet": True}
)
return [pixels("cached-page-image")]
monkeypatch.setattr(pipeline, "prepare_model_request", prepare, raising=False)
context = UnifiedContext(session_id="synthetic-source", user_message="Inspect the figure")
await _run(pipeline, context)
assert len(checks) == client.call_count == 2
assert "cached-page-image" in json.dumps(client.calls[0]["messages"])
assert "fresh-tool-image" in json.dumps(client.calls[1]["messages"])
assert "cached-page-image" not in json.dumps(client.calls[1]["messages"])
assert all("_lesson_packet" not in m for call in client.calls for m in call["messages"])
assert "base64" not in json.dumps(context.runtime.model_turn)
messages = client.calls[1]["messages"]
call_index = next(i for i, m in enumerate(messages) if m.get("tool_calls"))
assert messages[call_index + 1]["tool_call_id"] == "inspect"
# A hard finish keeps the previous round's tools (prompt-cache prefix).
assert client.calls[1]["tools"] == client.calls[0]["tools"]
@pytest.mark.asyncio
@pytest.mark.parametrize("forced_finish", [False, True])
@pytest.mark.parametrize("error_type", [LessonUnavailable, CapabilityPrerequisiteError])
async def test_source_check_failure_cannot_be_bypassed_by_finishing(
monkeypatch, forced_finish, error_type
):
client = _ScriptedChatClient(
[
[
_llm_chunk(
tool_calls=[
{"id": "change", "name": "web_search", "arguments": '{"query":"change"}'}
]
)
],
[_llm_chunk(content="Must not run.")],
]
)
pipeline = AgenticChatPipeline(language="en")
pipeline.registry = _Registry()
monkeypatch.setattr(pipeline, "_compose_enabled_tools", lambda _: ["web_search"])
monkeypatch.setattr(pipeline, "_build_openai_client", lambda: client)
if forced_finish:
pipeline._max_rounds = 1
monkeypatch.setattr(loop_module, "MAX_SETTLEMENT_ROUNDS", 0)
checks = []
async def prepare(context, stream, messages):
checks.append(True)
if len(checks) > 1:
raise error_type("Selected source access changed")
return []
monkeypatch.setattr(pipeline, "prepare_model_request", prepare, raising=False)
with pytest.raises(error_type):
await _run(pipeline, UnifiedContext(user_message="Teach"))
assert client.call_count == 1
def test_durable_packet_marker_survives_normalization_for_later_refresh():
record = normalize_model_turn(
{
"version": 1,
"messages": [
{"role": "user", "content": "Teach"},
{"role": "user", "content": "old source packet", "_lesson_packet": True},
{"role": "assistant", "content": "Explanation"},
],
}
)
assert record["messages"][1]["_lesson_packet"] is True