fix: preserve optional Codex tool arguments for mastery quizzes

This commit is contained in:
我也有友人账
2026-10-29 11:42:59 -07:00
parent a4fa63b6e2
commit d24648d5ed
4 changed files with 131 additions and 0 deletions

View File

@@ -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:

View File

@@ -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():

View File

@@ -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

View File

@@ -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