From dc8fac4dfc34693707df58252dac4c2f7f8b1194 Mon Sep 17 00:00:00 2001 From: Rudy Celekli Date: Mon, 26 Oct 2026 10:24:51 -0400 Subject: [PATCH] fix(config): stop chat-settings read from corrupting shipped defaults _build_chat_block seeded its merge buffer by deep-merging DEFAULT_CHAT_PARAMS into an empty dict with the in-place _deep_merge helper. With an empty buffer, the first merge aliased the buffer's nested stage dicts (exploring/responding) to the module-level DEFAULT_CHAT_PARAMS objects; the second merge of the user's capabilities.chat override then wrote that value into DEFAULT_CHAT_PARAMS in place. Opening Settings -> Capabilities once with a chat stage override thus corrupted the shipped default process-wide, and a later read without an override returned the stale value (which also feeds loader.get_chat_params on the chat hot path). Seed the buffer from a deepcopy instead. Co-Authored-By: Claude Opus 4.8 --- .../services/config/capabilities_settings.py | 7 ++- .../test_chat_block_defaults_immutable.py | 45 +++++++++++++++++++ 2 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 tests/services/config/test_chat_block_defaults_immutable.py diff --git a/deeptutor/services/config/capabilities_settings.py b/deeptutor/services/config/capabilities_settings.py index 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 copy from pathlib import Path from 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.py new file mode 100644 index 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.`` 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 + + +@pytest.fixture(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