DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.
Python ai-agentsai-tutorclawbotcli-tooldeepresearchinteractive-learninglearningmulti-agent-systemsrag
fix(config): stop chat-settings read from corrupting shipped defaults
Rudy Celeklidc8fac4dfc34
diff --git a/deeptutor/services/config/capabilities_settings.py b/deeptutor/services/config/capabilities_settings.pyindex ae67a0776..f12f3b592 100644--- a/deeptutor/services/config/capabilities_settings.py+++ b/deeptutor/services/config/capabilities_settings.py@@ -22,6 +22,7 @@ constants into settings, capabilities can be added here.from __future__ import annotations+import copyfrom pathlib import Pathfrom typing import Any@@ -205,8 +206,10 @@ _CHAT_LEGACY_KEYS: tuple[str, ...] = (def _build_chat_block(agents_cfg: dict[str, Any]) -> dict[str, Any]:"""Read agents.yaml.capabilities.chat into the UI schema with defaults."""chat_cfg: dict[str, Any] = _get_at(agents_cfg, ("capabilities", "chat"))- merged: dict[str, Any] = {}- _deep_merge(merged, DEFAULT_CHAT_PARAMS)+ # Deep-copy the shipped defaults: ``_deep_merge`` mutates its first argument+ # in place, so seeding from the module-level ``DEFAULT_CHAT_PARAMS`` would+ # alias (and then overwrite) its nested stage dicts with the user's values.+ merged: dict[str, Any] = copy.deepcopy(DEFAULT_CHAT_PARAMS)_deep_merge(merged, chat_cfg)return {"temperature": _coerce_float(merged.get("temperature"), DEFAULT_CHAT_PARAMS["temperature"]),diff --git a/tests/services/config/test_chat_block_defaults_immutable.py b/tests/services/config/test_chat_block_defaults_immutable.pynew file mode 100644index 000000000..15b9341c8--- /dev/null+++ b/tests/services/config/test_chat_block_defaults_immutable.py@@ -0,0 +1,45 @@+"""Reading the chat capability settings must not corrupt the shipped defaults.++``_build_chat_block`` seeded its merge buffer by deep-merging+``DEFAULT_CHAT_PARAMS`` into an empty dict with a mutating merge helper. That+aliased the buffer's nested stage dicts to the module-level default objects, so+merging a user's ``capabilities.chat.<stage>`` override then wrote that value+*into* ``DEFAULT_CHAT_PARAMS`` in place. The shipped default was corrupted+process-wide, and a later read without an override bled the previous user's+value (also into the chat hot path via ``loader.get_chat_params``).+"""++from __future__ import annotations++import copy++import pytest++from deeptutor.services.config import capabilities_settings as module+from deeptutor.services.config.loader import DEFAULT_CHAT_PARAMS++[email protected](autouse=True)+def _restore_chat_defaults() -> None:+ """Isolate each test from the very corruption under test (and each other)."""+ pristine = copy.deepcopy(DEFAULT_CHAT_PARAMS)+ yield+ DEFAULT_CHAT_PARAMS.clear()+ DEFAULT_CHAT_PARAMS.update(pristine)+++def test_build_chat_block_does_not_mutate_shipped_defaults() -> None:+ pristine = copy.deepcopy(DEFAULT_CHAT_PARAMS)++ module._build_chat_block({"capabilities": {"chat": {"exploring": {"max_tokens": 9999}}}})++ assert DEFAULT_CHAT_PARAMS == pristine+++def test_override_does_not_bleed_into_a_later_default_read() -> None:+ shipped_exploring = copy.deepcopy(DEFAULT_CHAT_PARAMS)["exploring"]["max_tokens"]++ module._build_chat_block({"capabilities": {"chat": {"exploring": {"max_tokens": 9999}}}})+ later = module._build_chat_block({"capabilities": {"chat": {}}})++ assert later["stage_budgets"]["exploring"] == shipped_exploring