Files
DeepTutor/tests/services/prompt/test_language_directive.py
Gaurav Dubey d5637d5ba3 fix(mastery): keep language-course content in its target language (#2934)
* fix(mastery): keep language-course content in its target language

The reply-language directive is the last thing in every system prompt and
tells the model to write quiz stems and options in the reply language and
never switch. For a course whose subject is itself a language, the model
obeyed literally: an "Antonyms" objective in an English course, studied with
Chinese as the reply language, was drafted as 反义词 and taught in a later
session as Chinese antonyms.

- language_directive now says the reply language governs the explanation,
  not the subject: a studied language keeps its own words, sentences and
  exercise items.
- Route generation and the tutor's outline drafting name the target
  language in every module and knowledge point ("English antonyms").
- The tutor playbook keeps an existing language course on its target
  language even when an objective name does not repeat it.

Closes #2933

Co-Authored-By: Claude <noreply@anthropic.com>

* test(mastery): check the composed prompts keep a language course's target language

The strict reply-language directive is appended last, after the route and
tutor instructions. Assert the carve-out survives in the prompts the model
actually receives: zh route generation, and the zh tutor playbook rendered
with a fixed reply language.

Related to #2933

Co-Authored-By: Claude <noreply@anthropic.com>

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-11-28 18:24:23 +08:00

110 lines
4.4 KiB
Python

"""The language directive, and the one surface allowed to bend it."""
from __future__ import annotations
import pytest
from deeptutor.services.prompt.language import (
append_language_directive,
language_directive,
)
@pytest.mark.parametrize("language", ["en", "zh", "ja"])
def test_the_default_directive_forbids_switching(language: str) -> None:
"""Books, quizzes and research have no user in the loop to ask."""
directive = language_directive(language)
assert "Do NOT switch languages" in directive or "不得切换语言" in directive
assert "explicitly asks" not in directive
assert "明确要求" not in directive
@pytest.mark.parametrize("language", ["en", "zh", "ja"])
def test_the_conversational_directive_yields_to_an_explicit_request(language: str) -> None:
"""Why this exists: a user asked 「请用中文回复我」 and was told no.
The strict directive is the last thing in the system prompt, and the
runtime policy just above it says user text is not authority over these
instructions — so the model answered that it was *required* to reply in
English. It was reading the prompt correctly. The prompt was wrong.
"""
directive = language_directive(language, allow_user_override=True)
assert "explicitly asks" in directive or "明确要求" in directive
def test_the_override_rides_on_the_appended_prompt() -> None:
prompt = append_language_directive("## Base\nbody", "zh", allow_user_override=True)
assert prompt.startswith("## Base")
assert "明确要求" in prompt
@pytest.mark.parametrize(
("language", "marker"),
[("en", "explicitly asks"), ("zh", "明确要求")],
)
def test_chat_renders_the_overridable_directive(language: str, marker: str) -> None:
"""Chat is the surface that carries the carve-out — rendered, not asserted
on source text, so a refactor that drops the flag fails here."""
from deeptutor.agents.loop.prompt_blocks import LoopPromptAssembler, PromptBlock
assembler = LoopPromptAssembler(prompts={}, language=language)
rendered = assembler.render([PromptBlock(name="Base", content="body")])
assert marker in rendered
def test_fixed_session_selector_disables_conversational_override() -> None:
from deeptutor.agents.loop.prompt_blocks import LoopPromptAssembler, PromptBlock
assembler = LoopPromptAssembler(prompts={}, language="fr")
rendered = assembler.render(
[PromptBlock(name="Base", content="body")], allow_user_override=False
)
assert "Français" in rendered
assert "explicitly asks" not in rendered
@pytest.mark.parametrize(
("language", "label"),
[
("fr", "Français"),
("uk", "Українська"),
("ms", "Bahasa Melayu"),
("vi", "Tiếng Việt"),
("hi", "हिन्दी"),
],
)
def test_loop_preserves_requested_output_language_with_english_prompt_assets(
language: str, label: str
) -> None:
from deeptutor.agents.loop.pipeline import AgenticLoopPipeline
from deeptutor.agents.loop.prompt_blocks import PromptBlock
pipeline = AgenticLoopPipeline(language=language)
assert pipeline.language == language
assert pipeline._prompt_assembler.language == "en"
rendered = pipeline._prompt_assembler.render([PromptBlock(name="Base", content="body")])
assert label in rendered
@pytest.mark.parametrize("language", ["en", "zh", "ja", "fr"])
@pytest.mark.parametrize("allow_user_override", [False, True])
def test_the_directive_keeps_a_studied_language_in_its_own_language(
language: str, allow_user_override: bool
) -> None:
"""#2933: an English antonyms lesson taught to a learner whose reply
language is Chinese came back as a lesson on Chinese antonyms.
The directive tells the model to write quiz stems and options in the reply
language and never switch, so without a carve-out it is right to translate
the subject matter itself. The reply language is the language of the
explanation; a language being studied keeps its own words and exercises.
"""
directive = language_directive(language, allow_user_override=allow_user_override)
assert "Do NOT switch languages" in directive or "不得切换语言" in directive
if language == "zh":
assert "所学的科目本身就是一门语言" in directive
assert "保留原语言" in directive
else:
assert "the subject being studied is itself a language" in directive
assert "never substitute the equivalent material" in directive