mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-02 19:39:42 +08:00
Local dev repairs for #2539, #2686. Regression checks cover reported symptoms and compatibility boundaries.
777 lines
25 KiB
Python
777 lines
25 KiB
Python
from __future__ import annotations
|
|
|
|
import asyncio
|
|
from pathlib import Path
|
|
import sys
|
|
from types import SimpleNamespace
|
|
|
|
import pytest
|
|
|
|
from deeptutor.services.llm.config import LLMConfig
|
|
from deeptutor.services.llm.provider_core import ClaudeCodeProvider
|
|
from deeptutor.services.llm.provider_core.claude_code_provider import (
|
|
_INLINE_SYSTEM_PROMPT_LIMIT,
|
|
_align_advertised_tools,
|
|
)
|
|
from deeptutor.services.llm.provider_factory import get_runtime_provider
|
|
from deeptutor.services.provider_registry import find_by_name
|
|
|
|
|
|
class FakeOptions:
|
|
def __init__(self, **kwargs):
|
|
self.kwargs = kwargs
|
|
|
|
|
|
class FakeTextBlock:
|
|
def __init__(self, text: str):
|
|
self.text = text
|
|
|
|
|
|
class FakeToolUseBlock:
|
|
def __init__(self, call_id: str, name: str, arguments: dict[str, object]):
|
|
self.id = call_id
|
|
self.name = name
|
|
self.input = arguments
|
|
|
|
|
|
class FakeAssistantMessage:
|
|
def __init__(self, content):
|
|
self.content = content
|
|
|
|
|
|
class FakeResultMessage:
|
|
def __init__(
|
|
self,
|
|
result: str = "",
|
|
is_error: bool = False,
|
|
subtype: str = "",
|
|
errors: list[str] | None = None,
|
|
):
|
|
self.result = result
|
|
self.is_error = is_error
|
|
self.subtype = subtype
|
|
self.errors = errors
|
|
|
|
|
|
def fake_tool(name, description, parameters):
|
|
def decorate(func):
|
|
func.tool_definition = (name, description, parameters)
|
|
return func
|
|
|
|
return decorate
|
|
|
|
|
|
def fake_mcp_server(name, tools):
|
|
return {"name": name, "tools": tools}
|
|
|
|
|
|
class FakeClient:
|
|
"""Replays one scripted response per query, like ``ClaudeSDKClient``."""
|
|
|
|
instances: list["FakeClient"] = []
|
|
script: list[list[object]] = []
|
|
|
|
def __init__(self, options):
|
|
self.options = options
|
|
self.prompts: list[str] = []
|
|
self.interrupts = 0
|
|
self.disconnected = False
|
|
FakeClient.instances.append(self)
|
|
|
|
async def connect(self):
|
|
self.owner = asyncio.current_task()
|
|
|
|
async def __aenter__(self):
|
|
await self.connect()
|
|
return self
|
|
|
|
async def __aexit__(self, *args):
|
|
await self.disconnect()
|
|
|
|
async def query(self, prompt):
|
|
if not isinstance(prompt, str):
|
|
prompt = [message async for message in prompt]
|
|
self.prompts.append(prompt)
|
|
|
|
async def receive_response(self):
|
|
for message in FakeClient.script.pop(0):
|
|
yield message
|
|
|
|
async def interrupt(self):
|
|
self.interrupts += 1
|
|
|
|
async def disconnect(self):
|
|
assert self.owner is asyncio.current_task()
|
|
self.disconnected = True
|
|
|
|
|
|
def _install_fake_sdk(monkeypatch, **extra):
|
|
FakeClient.instances = []
|
|
sdk = SimpleNamespace(
|
|
ClaudeAgentOptions=FakeOptions,
|
|
ClaudeSDKClient=FakeClient,
|
|
tool=fake_tool,
|
|
create_sdk_mcp_server=fake_mcp_server,
|
|
**extra,
|
|
)
|
|
monkeypatch.setitem(sys.modules, "claude_agent_sdk", sdk)
|
|
return sdk
|
|
|
|
|
|
WEATHER_TOOL = {
|
|
"type": "function",
|
|
"function": {
|
|
"name": "get_weather",
|
|
"description": "Weather lookup",
|
|
"parameters": {"type": "object", "properties": {"city": {"type": "string"}}},
|
|
},
|
|
}
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_long_system_prompt_goes_to_a_file_not_the_command_line(monkeypatch) -> None:
|
|
"""Windows rejects a spawn over 32,767 characters, so long prompts use a file."""
|
|
captured: dict[str, object] = {}
|
|
|
|
async def fake_query(**kwargs):
|
|
captured.update(kwargs)
|
|
yield FakeResultMessage("ok")
|
|
|
|
_install_fake_sdk(monkeypatch, query=fake_query)
|
|
long_prompt = "Plan the outline. " * 3_000
|
|
|
|
provider = ClaudeCodeProvider(default_model="claude-code/sonnet")
|
|
await provider.chat(
|
|
messages=[
|
|
{"role": "system", "content": long_prompt},
|
|
{"role": "user", "content": "Go"},
|
|
]
|
|
)
|
|
|
|
option = captured["options"].kwargs["system_prompt"]
|
|
assert option["type"] == "file"
|
|
path = Path(option["path"])
|
|
assert path.read_text(encoding="utf-8") == _align_advertised_tools(long_prompt, None)
|
|
assert path.parent == Path(provider._workspace.name)
|
|
assert not list(path.parent.glob("*.tmp"))
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_system_prompt_at_the_inline_limit_stays_inline(monkeypatch) -> None:
|
|
captured: dict[str, object] = {}
|
|
|
|
async def fake_query(**kwargs):
|
|
captured.update(kwargs)
|
|
yield FakeResultMessage("ok")
|
|
|
|
_install_fake_sdk(monkeypatch, query=fake_query)
|
|
# The provider appends its tool-access note before measuring, so size the
|
|
# base prompt for the aligned result to sit exactly at the limit.
|
|
note_suffix = _align_advertised_tools("x", None)[1:]
|
|
prompt = "x" * (_INLINE_SYSTEM_PROMPT_LIMIT - len(note_suffix))
|
|
|
|
provider = ClaudeCodeProvider(default_model="claude-code/sonnet")
|
|
await provider.chat(
|
|
messages=[{"role": "system", "content": prompt}, {"role": "user", "content": "Go"}]
|
|
)
|
|
|
|
assert captured["options"].kwargs["system_prompt"] == prompt + note_suffix
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_one_shot_lifts_system_prompt_and_isolates_claude_code(monkeypatch) -> None:
|
|
captured: dict[str, object] = {}
|
|
|
|
async def fake_query(**kwargs):
|
|
captured.update(kwargs)
|
|
yield FakeAssistantMessage([FakeTextBlock("hel"), FakeTextBlock("lo")])
|
|
yield FakeResultMessage("hello")
|
|
|
|
_install_fake_sdk(monkeypatch, query=fake_query)
|
|
|
|
provider = ClaudeCodeProvider(default_model="claude-code/sonnet")
|
|
response = await provider.chat(
|
|
messages=[
|
|
{"role": "system", "content": "Be concise."},
|
|
{"role": "user", "content": "Say hello"},
|
|
]
|
|
)
|
|
|
|
assert response.content == "hello"
|
|
options = captured["options"].kwargs
|
|
assert options["system_prompt"] == _align_advertised_tools("Be concise.", None)
|
|
assert options["system_prompt"].startswith("Be concise.")
|
|
assert options["model"] == "sonnet"
|
|
assert options["tools"] == []
|
|
assert options["setting_sources"] == []
|
|
assert options["max_turns"] == 1
|
|
assert "mcp_servers" not in options
|
|
assert "Be concise." not in captured["prompt"]
|
|
assert "User:\nSay hello" in captured["prompt"]
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_default_model_alias_is_left_to_the_account(monkeypatch) -> None:
|
|
captured: dict[str, object] = {}
|
|
|
|
async def fake_query(**kwargs):
|
|
captured.update(kwargs)
|
|
yield FakeResultMessage("ok")
|
|
|
|
_install_fake_sdk(monkeypatch, query=fake_query)
|
|
|
|
await ClaudeCodeProvider().chat(messages=[{"role": "user", "content": "hi"}])
|
|
|
|
assert "model" not in captured["options"].kwargs
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_session_tool_call_drains_without_interrupt_and_sends_only_new_tail(
|
|
monkeypatch,
|
|
) -> None:
|
|
_install_fake_sdk(monkeypatch)
|
|
FakeClient.script = [
|
|
[
|
|
FakeAssistantMessage(
|
|
[FakeToolUseBlock("toolu_1", "mcp__deeptutor__get_weather", {"city": "Hanoi"})]
|
|
),
|
|
FakeResultMessage("max turns", is_error=True),
|
|
],
|
|
[FakeAssistantMessage([FakeTextBlock("31C and rainy")]), FakeResultMessage("done")],
|
|
]
|
|
|
|
provider = ClaudeCodeProvider()
|
|
messages = [
|
|
{"role": "system", "content": "Use tools."},
|
|
{"role": "user", "content": "Weather in Hanoi?"},
|
|
]
|
|
first = await provider.chat(messages, tools=[WEATHER_TOOL], deeptutor_session_id="s1")
|
|
|
|
assert first.finish_reason == "tool_calls"
|
|
assert [(c.id, c.name, c.arguments) for c in first.tool_calls] == [
|
|
("toolu_1", "get_weather", {"city": "Hanoi"})
|
|
]
|
|
client = FakeClient.instances[0]
|
|
assert client.interrupts == 0
|
|
assert client.options.kwargs["allowed_tools"] == ["mcp__deeptutor__get_weather"]
|
|
|
|
call = first.tool_calls[0]
|
|
messages += [
|
|
{"role": "assistant", "content": "", "tool_calls": [call.to_openai_tool_call()]},
|
|
{"role": "tool", "tool_call_id": call.id, "name": call.name, "content": "31C rain"},
|
|
]
|
|
second = await provider.chat(messages, tools=[WEATHER_TOOL], deeptutor_session_id="s1")
|
|
|
|
assert second.content == "31C and rainy"
|
|
assert len(FakeClient.instances) == 1
|
|
assert client.prompts[1] == "Tool result (get_weather):\n31C rain"
|
|
|
|
await provider.aclose()
|
|
assert client.disconnected
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_bare_name_tool_call_matching_an_offered_tool_is_executed(monkeypatch) -> None:
|
|
_install_fake_sdk(monkeypatch)
|
|
FakeClient.script = [
|
|
[
|
|
FakeAssistantMessage([FakeToolUseBlock("toolu_1", "get_weather", {"city": "Hanoi"})]),
|
|
FakeResultMessage("max turns", is_error=True),
|
|
],
|
|
]
|
|
|
|
provider = ClaudeCodeProvider()
|
|
messages = [
|
|
{"role": "system", "content": "Use tools."},
|
|
{"role": "user", "content": "Weather in Hanoi?"},
|
|
]
|
|
response = await provider.chat(messages, tools=[WEATHER_TOOL], deeptutor_session_id="s1")
|
|
|
|
assert response.finish_reason == "tool_calls"
|
|
assert [(c.name, c.arguments) for c in response.tool_calls] == [
|
|
("get_weather", {"city": "Hanoi"})
|
|
]
|
|
await provider.aclose()
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_bare_name_that_was_not_offered_is_still_ignored(monkeypatch) -> None:
|
|
_install_fake_sdk(monkeypatch)
|
|
FakeClient.script = [
|
|
[
|
|
FakeAssistantMessage([FakeToolUseBlock("toolu_1", "Bash", {"command": "ls"})]),
|
|
FakeResultMessage("max turns", is_error=True),
|
|
],
|
|
]
|
|
|
|
provider = ClaudeCodeProvider()
|
|
messages = [{"role": "user", "content": "Weather in Hanoi?"}]
|
|
response = await provider.chat(messages, tools=[WEATHER_TOOL], deeptutor_session_id="s1")
|
|
|
|
assert not response.tool_calls
|
|
assert response.finish_reason == "error"
|
|
await provider.aclose()
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_error_result_without_text_names_the_subtype(monkeypatch) -> None:
|
|
_install_fake_sdk(monkeypatch)
|
|
result = FakeResultMessage(None, is_error=True)
|
|
result.result = None
|
|
result.subtype = "error_max_turns"
|
|
FakeClient.script = [[result]]
|
|
|
|
provider = ClaudeCodeProvider()
|
|
response = await provider.chat(
|
|
[{"role": "user", "content": "hi"}], tools=[WEATHER_TOOL], deeptutor_session_id="s1"
|
|
)
|
|
|
|
assert response.finish_reason == "error"
|
|
assert "Error calling Claude Code:" in response.content
|
|
assert "maximum number of turns (error_max_turns)" in response.content
|
|
await provider.aclose()
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_missing_login_is_reported_as_error_response(monkeypatch) -> None:
|
|
async def fake_query(**kwargs):
|
|
raise RuntimeError("Invalid API key · Please run /login")
|
|
yield # pragma: no cover
|
|
|
|
_install_fake_sdk(monkeypatch, query=fake_query)
|
|
|
|
response = await ClaudeCodeProvider().chat(messages=[{"role": "user", "content": "hi"}])
|
|
|
|
assert response.finish_reason == "error"
|
|
assert "/login" in (response.content or "")
|
|
|
|
|
|
def test_claude_code_registry_alias_and_factory() -> None:
|
|
spec = find_by_name("claude-code")
|
|
|
|
assert spec is not None
|
|
assert spec.name == "claude_code"
|
|
assert spec.is_oauth
|
|
|
|
provider = get_runtime_provider(
|
|
LLMConfig(
|
|
model="claude-code/default",
|
|
api_key="",
|
|
binding="claude_code",
|
|
provider_name="claude_code",
|
|
)
|
|
)
|
|
|
|
assert isinstance(provider, ClaudeCodeProvider)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_model_catalog_comes_from_cli_handshake(monkeypatch) -> None:
|
|
from deeptutor.services.llm.provider_core.claude_code_provider import (
|
|
fetch_claude_code_catalog,
|
|
fetch_claude_code_models,
|
|
)
|
|
|
|
async def server_info(self):
|
|
return {
|
|
"models": [
|
|
{"value": "default", "displayName": "Default (recommended)"},
|
|
{"value": "opus", "displayName": "Opus 5.5"},
|
|
{"value": "", "displayName": "ignored"},
|
|
]
|
|
}
|
|
|
|
monkeypatch.setattr(FakeClient, "get_server_info", server_info, raising=False)
|
|
_install_fake_sdk(monkeypatch)
|
|
|
|
assert await fetch_claude_code_catalog() == [
|
|
{"id": "claude-code/default", "name": "Default (account setting)"},
|
|
{"id": "claude-code/opus", "name": "Opus 5.5"},
|
|
]
|
|
assert await fetch_claude_code_models() == ["claude-code/default", "claude-code/opus"]
|
|
assert FakeClient.instances[0].disconnected
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_model_catalog_falls_back_to_aliases_when_cli_fails(monkeypatch) -> None:
|
|
from deeptutor.services.llm.provider_core.claude_code_provider import (
|
|
fetch_claude_code_catalog,
|
|
)
|
|
|
|
async def broken_connect(self):
|
|
raise RuntimeError("CLI not found")
|
|
|
|
monkeypatch.setattr(FakeClient, "connect", broken_connect)
|
|
_install_fake_sdk(monkeypatch)
|
|
|
|
catalog = await fetch_claude_code_catalog()
|
|
|
|
assert [model["id"] for model in catalog] == [
|
|
"claude-code/default",
|
|
"claude-code/sonnet",
|
|
"claude-code/opus",
|
|
"claude-code/haiku",
|
|
]
|
|
|
|
|
|
IMAGE_URL = "data:image/png;base64,iVBORw0KGgo="
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_text_only_turns_stay_plain_strings(monkeypatch) -> None:
|
|
captured: dict[str, object] = {}
|
|
|
|
async def fake_query(**kwargs):
|
|
captured.update(kwargs)
|
|
yield FakeResultMessage("ok")
|
|
|
|
_install_fake_sdk(monkeypatch, query=fake_query)
|
|
|
|
await ClaudeCodeProvider().chat(messages=[{"role": "user", "content": "hi"}])
|
|
|
|
assert captured["prompt"] == "User:\nhi"
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_one_shot_sends_images_as_anthropic_blocks(monkeypatch) -> None:
|
|
captured: dict[str, object] = {}
|
|
|
|
async def fake_query(**kwargs):
|
|
captured["prompt"] = [message async for message in kwargs["prompt"]]
|
|
yield FakeAssistantMessage([FakeTextBlock("a red square")])
|
|
yield FakeResultMessage("a red square")
|
|
|
|
_install_fake_sdk(monkeypatch, query=fake_query)
|
|
|
|
response = await ClaudeCodeProvider().chat(
|
|
messages=[
|
|
{"role": "system", "content": "Be terse."},
|
|
{
|
|
"role": "user",
|
|
"content": [
|
|
{"type": "text", "text": "Source evidence: fig-1."},
|
|
{"type": "image_url", "image_url": {"url": IMAGE_URL}},
|
|
{"type": "text", "text": "What is shown?"},
|
|
],
|
|
},
|
|
]
|
|
)
|
|
|
|
assert response.content == "a red square"
|
|
[message] = captured["prompt"]
|
|
assert message["type"] == "user"
|
|
assert message["message"]["content"] == [
|
|
{"type": "text", "text": "User:\nSource evidence: fig-1."},
|
|
{
|
|
"type": "image",
|
|
"source": {"type": "base64", "media_type": "image/png", "data": "iVBORw0KGgo="},
|
|
},
|
|
{"type": "text", "text": "What is shown?"},
|
|
]
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_session_sends_only_new_image_message_as_blocks(monkeypatch) -> None:
|
|
_install_fake_sdk(monkeypatch)
|
|
FakeClient.script = [
|
|
[FakeAssistantMessage([FakeTextBlock("first")]), FakeResultMessage("first")],
|
|
[FakeAssistantMessage([FakeTextBlock("it is red")]), FakeResultMessage("it is red")],
|
|
]
|
|
|
|
provider = ClaudeCodeProvider()
|
|
messages = [{"role": "user", "content": "Look at figure 1."}]
|
|
await provider.chat(messages, deeptutor_session_id="s1")
|
|
|
|
messages += [
|
|
{"role": "assistant", "content": "first"},
|
|
{"role": "user", "content": [{"type": "image_url", "image_url": {"url": IMAGE_URL}}]},
|
|
]
|
|
response = await provider.chat(messages, deeptutor_session_id="s1")
|
|
|
|
assert response.content == "it is red"
|
|
client = FakeClient.instances[0]
|
|
assert client.prompts[0] == "User:\nLook at figure 1."
|
|
[message] = client.prompts[1]
|
|
assert message["message"]["content"] == [
|
|
{"type": "text", "text": "User:"},
|
|
{
|
|
"type": "image",
|
|
"source": {"type": "base64", "media_type": "image/png", "data": "iVBORw0KGgo="},
|
|
},
|
|
]
|
|
await provider.aclose()
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_cancel_active_stream_closes_in_owner_task(monkeypatch):
|
|
entered = asyncio.Event()
|
|
|
|
async def blocked_response(self):
|
|
entered.set()
|
|
await asyncio.Event().wait()
|
|
yield FakeResultMessage("unreachable")
|
|
|
|
monkeypatch.setattr(FakeClient, "receive_response", blocked_response)
|
|
_install_fake_sdk(monkeypatch)
|
|
provider = ClaudeCodeProvider()
|
|
task = asyncio.create_task(
|
|
provider.chat(
|
|
[{"role": "user", "content": "Wait"}],
|
|
deeptutor_session_id="cancel",
|
|
)
|
|
)
|
|
await asyncio.wait_for(entered.wait(), 1)
|
|
task.cancel()
|
|
with pytest.raises(asyncio.CancelledError):
|
|
await asyncio.wait_for(task, 1)
|
|
assert FakeClient.instances[0].disconnected
|
|
assert not provider._sessions
|
|
await provider.aclose()
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_regeneration_replaces_old_cli_transcript(monkeypatch):
|
|
_install_fake_sdk(monkeypatch)
|
|
FakeClient.script = [[FakeResultMessage("one")], [FakeResultMessage("two")]]
|
|
provider = ClaudeCodeProvider()
|
|
await provider.chat([{"role": "user", "content": "Original"}], deeptutor_session_id="branch")
|
|
await provider.chat([{"role": "user", "content": "Edited"}], deeptutor_session_id="branch")
|
|
assert len(FakeClient.instances) == 2
|
|
assert FakeClient.instances[0].disconnected
|
|
assert FakeClient.instances[1].prompts == ["User:\nEdited"]
|
|
options = FakeClient.instances[1].options.kwargs
|
|
assert options["permission_mode"] == "dontAsk"
|
|
assert options["extra_args"] == {"strict-mcp-config": None}
|
|
await provider.aclose()
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_cancel_during_sdk_connect_settles_owner(monkeypatch):
|
|
import anyio
|
|
|
|
from deeptutor.services.llm.provider_core.claude_code_provider import _ClaudeCodeSession
|
|
|
|
entered = asyncio.Event()
|
|
|
|
async def connect(self):
|
|
self.owner = asyncio.current_task()
|
|
self.group = anyio.create_task_group()
|
|
await self.group.__aenter__()
|
|
entered.set()
|
|
await asyncio.Event().wait()
|
|
|
|
async def disconnect(self):
|
|
assert self.owner is asyncio.current_task()
|
|
await self.group.__aexit__(None, None, None)
|
|
self.disconnected = True
|
|
|
|
monkeypatch.setattr(FakeClient, "connect", connect)
|
|
monkeypatch.setattr(FakeClient, "disconnect", disconnect)
|
|
sdk = _install_fake_sdk(monkeypatch)
|
|
task = asyncio.create_task(_ClaudeCodeSession.start(sdk=sdk, signature="test", options=None))
|
|
await asyncio.wait_for(entered.wait(), 1)
|
|
task.cancel()
|
|
with pytest.raises(asyncio.CancelledError):
|
|
await asyncio.wait_for(task, 1)
|
|
assert FakeClient.instances[0].disconnected
|
|
|
|
|
|
def test_an_empty_sdk_exception_still_names_a_reason() -> None:
|
|
"""#2539: a blank exception must not render as a bare "Error calling …:"."""
|
|
from deeptutor.services.llm.provider_core.claude_code_provider import _friendly_error
|
|
|
|
for exc in (Exception(""), RuntimeError(), ValueError(" ")):
|
|
message = _friendly_error(exc)
|
|
assert message != "Error calling Claude Code: "
|
|
assert type(exc).__name__ in message
|
|
|
|
|
|
def test_a_non_empty_sdk_exception_is_reported_verbatim() -> None:
|
|
from deeptutor.services.llm.provider_core.claude_code_provider import _friendly_error
|
|
|
|
assert _friendly_error(RuntimeError("socket closed")) == (
|
|
"Error calling Claude Code: socket closed"
|
|
)
|
|
|
|
|
|
# ---- #2539: advertised tools must match the mounted MCP surface ----------
|
|
|
|
TOOL_MANIFEST = (
|
|
"## tools\n"
|
|
"- `get_weather` — Weather lookup\n"
|
|
" When to use: the user asks about weather.\n"
|
|
"- `write_note` — Create an authored notebook page."
|
|
)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_system_prompt_advertises_mounted_tools_by_reachable_name(monkeypatch) -> None:
|
|
"""The manifest's bare names do not exist on the CLI; mount-aligned ones do."""
|
|
captured: dict[str, object] = {}
|
|
|
|
async def fake_query(**kwargs):
|
|
captured.update(kwargs)
|
|
yield FakeResultMessage("ok")
|
|
|
|
_install_fake_sdk(monkeypatch, query=fake_query)
|
|
|
|
await ClaudeCodeProvider().chat(
|
|
messages=[
|
|
{"role": "system", "content": TOOL_MANIFEST},
|
|
{"role": "user", "content": "Weather?"},
|
|
],
|
|
tools=[WEATHER_TOOL],
|
|
)
|
|
|
|
prompt = captured["options"].kwargs["system_prompt"]
|
|
assert "`mcp__deeptutor__get_weather`" in prompt
|
|
assert "`get_weather`" not in prompt
|
|
assert "mcp__deeptutor__" in prompt # the naming rule names the exact prefix
|
|
assert "the user asks about weather" in prompt # the mounted entry keeps its lines
|
|
# A tool that is advertised but NOT mounted must not be advertised at all.
|
|
assert "`write_note`" not in prompt
|
|
assert "Create an authored notebook page" not in prompt
|
|
assert "mcp__deeptutor__write_note" not in prompt
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_system_prompt_without_mounted_tools_says_no_tools(monkeypatch) -> None:
|
|
"""Forced-finish calls send the manifest with no tools mounted; say that."""
|
|
captured: dict[str, object] = {}
|
|
|
|
async def fake_query(**kwargs):
|
|
captured.update(kwargs)
|
|
yield FakeResultMessage("ok")
|
|
|
|
_install_fake_sdk(monkeypatch, query=fake_query)
|
|
|
|
await ClaudeCodeProvider().chat(
|
|
messages=[
|
|
{"role": "system", "content": TOOL_MANIFEST},
|
|
{"role": "user", "content": "Finish now."},
|
|
],
|
|
tools=None,
|
|
)
|
|
|
|
options = captured["options"].kwargs
|
|
prompt = options["system_prompt"]
|
|
assert prompt.startswith(TOOL_MANIFEST)
|
|
assert "No tools are mounted" in prompt
|
|
# Nothing is mounted, so no tool is renamed into a reachable one.
|
|
assert "mcp__deeptutor__" not in prompt
|
|
assert "mcp_servers" not in options
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_empty_system_prompt_is_left_empty(monkeypatch) -> None:
|
|
captured: dict[str, object] = {}
|
|
|
|
async def fake_query(**kwargs):
|
|
captured.update(kwargs)
|
|
yield FakeResultMessage("ok")
|
|
|
|
_install_fake_sdk(monkeypatch, query=fake_query)
|
|
|
|
await ClaudeCodeProvider().chat(messages=[{"role": "user", "content": "hi"}])
|
|
|
|
assert captured["options"].kwargs["system_prompt"] == ""
|
|
|
|
|
|
# ---- #2539: a rejected tool call must survive as turn-failure evidence ----
|
|
|
|
REJECTED_TOOL_RESULT_MESSAGE = {
|
|
"type": "user",
|
|
"message": {
|
|
"role": "user",
|
|
"content": [
|
|
{
|
|
"type": "tool_result",
|
|
"tool_use_id": "toolu_1",
|
|
"is_error": True,
|
|
"content": "Error: No such tool available: write_note",
|
|
},
|
|
{
|
|
"type": "tool_result",
|
|
"tool_use_id": "toolu_2",
|
|
"content": "<tool_use_error>No such tool available: search_notes</tool_use_error>",
|
|
},
|
|
],
|
|
},
|
|
}
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_rejected_tool_call_ends_turn_with_actionable_error(monkeypatch) -> None:
|
|
"""A turn killed by max_turns right after a rejection keeps every cause.
|
|
|
|
Real CLI frames mark this terminal result ``is_error=True`` with the
|
|
prose in ``errors[]`` and ``result=None``; when the frame carries only
|
|
the subtype, the rejections must still be named (#2539).
|
|
"""
|
|
_install_fake_sdk(monkeypatch)
|
|
FakeClient.script = [
|
|
[
|
|
REJECTED_TOOL_RESULT_MESSAGE,
|
|
FakeResultMessage("", is_error=True, subtype="error_max_turns"),
|
|
]
|
|
]
|
|
|
|
response = await ClaudeCodeProvider().chat(
|
|
messages=[{"role": "user", "content": "Make me a study guide."}],
|
|
tools=[WEATHER_TOOL],
|
|
deeptutor_session_id="s1",
|
|
)
|
|
|
|
assert response.finish_reason == "error"
|
|
assert response.content.strip()
|
|
assert "error_max_turns" in response.content
|
|
assert "No such tool available: write_note" in response.content
|
|
assert "No such tool available: search_notes" in response.content
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_cli_errors_prose_stays_ahead_of_rejected_tool_evidence(monkeypatch) -> None:
|
|
"""The CLI's own ``errors[]`` sentence leads; rejections are appended."""
|
|
_install_fake_sdk(monkeypatch)
|
|
FakeClient.script = [
|
|
[
|
|
REJECTED_TOOL_RESULT_MESSAGE,
|
|
FakeResultMessage(
|
|
"",
|
|
is_error=True,
|
|
subtype="error_max_turns",
|
|
errors=["Reached maximum number of turns (1)"],
|
|
),
|
|
]
|
|
]
|
|
|
|
response = await ClaudeCodeProvider().chat(
|
|
messages=[{"role": "user", "content": "hi"}], deeptutor_session_id="s1"
|
|
)
|
|
|
|
assert response.finish_reason == "error"
|
|
assert response.content.startswith(
|
|
"Error calling Claude Code: Reached maximum number of turns (1)"
|
|
)
|
|
assert "rejected tool call: Error: No such tool available: write_note" in response.content
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_text_after_a_rejected_tool_call_is_still_a_normal_answer(monkeypatch) -> None:
|
|
"""A rejection the model recovered from must not fail the whole turn."""
|
|
_install_fake_sdk(monkeypatch)
|
|
FakeClient.script = [
|
|
[
|
|
REJECTED_TOOL_RESULT_MESSAGE,
|
|
FakeAssistantMessage([FakeTextBlock("here is the answer")]),
|
|
FakeResultMessage("here is the answer"),
|
|
]
|
|
]
|
|
|
|
response = await ClaudeCodeProvider().chat(
|
|
messages=[{"role": "user", "content": "hi"}], deeptutor_session_id="s1"
|
|
)
|
|
|
|
assert response.finish_reason == "stop"
|
|
assert response.content == "here is the answer"
|