From d24648d5ed517ff43789cda7e1826b32b6a00a95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=88=91=E4=B9=9F=E6=9C=89=E5=8F=8B=E4=BA=BA=E8=B4=A6?= <227647791+maxsheng215-cyber@users.noreply.github.com> Date: Thu, 29 Oct 2026 11:42:59 -0700 Subject: [PATCH] fix: preserve optional Codex tool arguments for mastery quizzes --- deeptutor/capabilities/mastery/tools.py | 7 ++ .../provider_core/openai_codex_provider.py | 5 ++ .../test_mastery_group_delivery.py | 77 +++++++++++++++++++ .../llm/test_openai_codex_oauth_provider.py | 42 ++++++++++ 4 files changed, 131 insertions(+) diff --git a/deeptutor/capabilities/mastery/tools.py b/deeptutor/capabilities/mastery/tools.py index ed142b4f8..637557c2e 100644 --- a/deeptutor/capabilities/mastery/tools.py +++ b/deeptutor/capabilities/mastery/tools.py @@ -1263,6 +1263,10 @@ class MasteryQuizTool(BaseTool): return _no_built_path_result("mastery_quiz") pending_action = kwargs.get("pending_action", "retain") resume_id = kwargs.get("resume_question_id") + # Model adapters may include blank optional strings in tool arguments. + # A blank ID is absent; a real ID still requires the resume action. + if isinstance(resume_id, str) and not resume_id.strip(): + resume_id = None if pending_action == "resume": contract_fields = { "knowledge_point_id", @@ -1304,6 +1308,9 @@ class MasteryQuizTool(BaseTool): success=False, ) raw_questions = kwargs.get("questions") + # An empty optional group carries no contracts; use the single question. + if isinstance(raw_questions, list) and not raw_questions: + raw_questions = None if pending_action == "resume": pending, echoed_options = None, False elif raw_questions is None: diff --git a/deeptutor/services/llm/provider_core/openai_codex_provider.py b/deeptutor/services/llm/provider_core/openai_codex_provider.py index 78fe5245b..e5f816b88 100644 --- a/deeptutor/services/llm/provider_core/openai_codex_provider.py +++ b/deeptutor/services/llm/provider_core/openai_codex_provider.py @@ -74,6 +74,11 @@ class OpenAICodexProvider(LLMProvider): body["reasoning"] = {"effort": reasoning_effort} if tools: body["tools"] = convert_tools(tools) + # Preserve optional parameters instead of letting Responses normalize + # every field into a required strict-schema argument. + for tool in body["tools"]: + if tool.get("type") == "function": + tool.setdefault("strict", False) service = get_codex_oauth_service() async with service.inference_guard(): diff --git a/tests/capabilities/test_mastery_group_delivery.py b/tests/capabilities/test_mastery_group_delivery.py index 2713dcf2e..dd25e436d 100644 --- a/tests/capabilities/test_mastery_group_delivery.py +++ b/tests/capabilities/test_mastery_group_delivery.py @@ -224,3 +224,80 @@ async def test_markdown_discussion_preserves_existing_card(service, monkeypatch) assert result.metadata["mastery_question"]["question_id"] == "parent" assert capability.finish_instruction(context, "The questions are on your card.") is None assert service.store.load("path").quiz_attempts == [] + + +@pytest.mark.asyncio +@pytest.mark.parametrize("resume_id", [None, "", " \t"]) +@pytest.mark.parametrize("questions", [None, []]) +async def test_blank_optional_arguments_allow_single_question( + service, monkeypatch, resume_id, questions +): + from deeptutor.capabilities.mastery import tools + + monkeypatch.setattr(tools, "_new_service", lambda: service) + progress = service.store.load("path").model_copy(deep=True) + progress.book_id = "new-path" + progress.version = 0 + progress.pending_question = None + service.save(progress) + ended = [] + result = await tools.MasteryQuizTool().execute( + _mastery_path_id="new-path", + knowledge_point_id="one", + question="What is one plus one?", + expected_answer="2", + question_type="open", + options=[], + questions=questions, + pending_action="retain", + resume_question_id=resume_id, + replace_question_id="", + replacement_request="", + _end_turn_on_card=lambda: ended.append(True), + ) + assert result.success, result.content + question_id = result.metadata["mastery_question"]["question_id"] + assert service.store.get_active_interaction("new-path").interaction_id == question_id + assert service.store.load("new-path").quiz_attempts == [] + assert ended == [True] + assert "expected_answer" not in json.dumps(result.metadata) + + +@pytest.mark.asyncio +@pytest.mark.parametrize("resume_id", ["parent", False, 0, [], {}]) +async def test_nonblank_or_malformed_resume_id_still_requires_resume( + service, monkeypatch, resume_id +): + from deeptutor.capabilities.mastery import tools + + monkeypatch.setattr(tools, "_new_service", lambda: service) + before = service.store.load("path").model_dump() + interaction = service.store.get_active_interaction("path").model_dump() + result = await tools.MasteryQuizTool().execute( + _mastery_path_id="path", pending_action="retain", resume_question_id=resume_id + ) + assert not result.success + assert "requires pending_action='resume'" in result.content + assert service.store.load("path").model_dump() == before + assert service.store.get_active_interaction("path").model_dump() == interaction + + +@pytest.mark.asyncio +@pytest.mark.parametrize("questions", [[{}], [{}, {}, {}, {}, {}], {}]) +async def test_nonempty_malformed_groups_are_not_treated_as_single_questions( + service, monkeypatch, questions +): + from deeptutor.capabilities.mastery import tools + + monkeypatch.setattr(tools, "_new_service", lambda: service) + before = service.store.load("path").model_dump() + result = await tools.MasteryQuizTool().execute( + _mastery_path_id="path", + knowledge_point_id="one", + question="What is one plus one?", + expected_answer="2", + questions=questions, + ) + assert not result.success + assert "two to four" in result.content + assert service.store.load("path").model_dump() == before diff --git a/tests/services/llm/test_openai_codex_oauth_provider.py b/tests/services/llm/test_openai_codex_oauth_provider.py index b6b77b7a4..c544fbfa3 100644 --- a/tests/services/llm/test_openai_codex_oauth_provider.py +++ b/tests/services/llm/test_openai_codex_oauth_provider.py @@ -1,6 +1,7 @@ from __future__ import annotations from contextlib import asynccontextmanager +from copy import deepcopy from typing import Any, AsyncIterator import httpcore @@ -406,3 +407,44 @@ def test_openai_codex_binding_threads_session_id() -> None: from deeptutor.services.llm.capabilities import threads_session_id assert threads_session_id("openai_codex") + + +@pytest.mark.asyncio +@pytest.mark.parametrize("strict", [None, False, True]) +async def test_codex_preserves_optional_tool_parameters(monkeypatch, strict): + service = FakeCodexService() + captured = [] + + async def request(url, headers, body, **kwargs): + captured.append(body) + return "ok", [], "stop" + + monkeypatch.setattr(module, "get_codex_oauth_service", lambda: service) + monkeypatch.setattr(module, "_request_codex", request) + function = { + "name": "mastery_quiz", + "parameters": { + "type": "object", + "properties": { + "question": {"type": "string"}, + "resume_question_id": {"type": "string"}, + "visual": {"type": "object", "properties": {"sources": {"type": "array"}}}, + }, + "required": ["question"], + }, + } + if strict is not None: + function["strict"] = strict + tools = [{"type": "function", "function": function}] + before = deepcopy(tools) + + result = await OpenAICodexProvider().chat( + [{"role": "user", "content": "Give me a text-only question."}], tools=tools + ) + + assert result.content == "ok" + outgoing = captured[0]["tools"][0] + assert outgoing["strict"] is (strict if strict is not None else False) + assert outgoing["parameters"]["required"] == ["question"] + assert outgoing["parameters"] == function["parameters"] + assert tools == before