π OpenHands: AI-Driven Development
refactor(settings): delegate agent-settings merge to SDK apply_agent_settings_diff (#14677)
simonrosenberg0af395afbc3b
diff --git a/enterprise/storage/org_store.py b/enterprise/storage/org_store.pyindex 8c81b8403b..b93ad3ecb9 100644--- a/enterprise/storage/org_store.py+++ b/enterprise/storage/org_store.py@@ -44,7 +44,7 @@ from openhands.sdk.settings import (AgentSettingsConfig,ConversationSettings,OpenHandsAgentSettings,- validate_agent_settings,+ apply_agent_settings_diff,)_ORG_SETTINGS_EXCLUDED_FIELDS = {@@ -341,30 +341,15 @@ class OrgStore:settings_diff: dict[str, Any],settings_type: type[OpenHandsAgentSettings] | type[ConversationSettings],) -> AgentSettingsConfig | ConversationSettings:- """Deep-merge a sparse settings diff and validate the merged result.+ """Apply a sparse settings diff to the persisted base and validate it.- The persisted base is routed through the SDK loader first so any- registered schema migrations are applied before the diff is merged.- Agent settings are validated against the discriminated union, so the- result is the correct variant (OpenHands or ACP) rather than a coerced- OpenHands shape.+ Agent settings delegate to the SDK's :func:`apply_agent_settings_diff`,+ which owns the discriminated-union merge (replace on ``agent_kind``+ change, deep-merge within a variant) and returns the correct variant+ (OpenHands or ACP) rather than a coerced OpenHands shape."""if settings_type is OpenHandsAgentSettings:- base_settings = _load_persisted_agent_settings(current_settings or {})- new_kind = settings_diff.get('agent_kind')- if new_kind and new_kind != base_settings.agent_kind:- # Variant switch: deep-merging the new kind's fields onto the- # outgoing kind's dump yields an invalid mongrel. Start from a- # fresh base and let the diff populate it.- merged_settings = {'agent_kind': new_kind, **settings_diff}- else:- merged_settings = deep_merge(- base_settings.model_dump(- mode='json', context={'expose_secrets': True}- ),- settings_diff,- )- return validate_agent_settings(merged_settings)+ return apply_agent_settings_diff(current_settings or {}, settings_diff)base_settings = _load_persisted_conversation_settings(current_settings) # type: ignore[assignment]merged_settings = deep_merge(diff --git a/openhands/app_server/settings/settings_models.py b/openhands/app_server/settings/settings_models.pyindex d5386e86dd..e47155ca82 100644--- a/openhands/app_server/settings/settings_models.py+++ b/openhands/app_server/settings/settings_models.py@@ -34,6 +34,7 @@ from openhands.sdk.settings import (AgentSettingsConfig,ConversationSettings,OpenHandsAgentSettings,+ apply_agent_settings_diff,default_agent_settings,validate_agent_settings,)@@ -64,22 +65,12 @@ def _load_persisted_agent_settings() -> OpenHandsAgentSettings | ACPAgentSettings:"""Load persisted agent settings via the SDK loader.- Routes the raw payload through :func:`validate_agent_settings` so any- schema migrations registered with the SDK are applied before validation- against the discriminated :data:`AgentSettingsConfig` union.-- The legacy ``agent_kind: 'llm'`` tag (pre-rename, field-compatible with- ``openhands``) is normalized to ``'openhands'`` first. The SDK migration- only rewrites it while advancing ``schema_version``, so an ``'llm'`` payload- already at the current version would otherwise validate as the deprecated- ``LLMAgentSettings``. Doing it here keeps every read on the canonical- ``{openhands, acp}`` variants, without the cross-variant coercion that 500'd- ACP settings (``agent_kind: 'acp'`` is left untouched).+ Routes the raw payload through :func:`validate_agent_settings`, which+ applies registered schema migrations, canonicalizes the legacy+ ``agent_kind: 'llm'`` tag to ``'openhands'``, and validates against the+ discriminated :data:`AgentSettingsConfig` union."""- payload = data or {}- if isinstance(payload, dict) and payload.get('agent_kind') == 'llm':- payload = {**payload, 'agent_kind': 'openhands'}- return validate_agent_settings(payload)+ return validate_agent_settings(data or {})def _load_persisted_conversation_settings(data: Any) -> ConversationSettings:@@ -216,33 +207,25 @@ class Settings(BaseModel):_coerce_value(value) if not isinstance(value, dict) else value)+ # ``mcp_config`` replaces wholesale rather than deep-merging, so+ # hold it back from the variant-aware merge and apply it after.replace_mcp_config = 'mcp_config' in agent_updatemcp_config = coerced.pop('mcp_config', None) if replace_mcp_config else None- new_kind = coerced.get('agent_kind')- current_kind = self.agent_settings.agent_kind-- if new_kind and new_kind != current_kind:- # ``agent_settings`` is a discriminated union over- # ``OpenHandsAgentSettings | ACPAgentSettings``. Deep-merging- # the incoming kind's fields onto the outgoing kind's dump- # produces a mongrel (``llm`` plus ``acp_command``) that- # fails validation. Start from a fresh base for the new- # kind. Cross-kind config preservation tracked in- # OpenHands/OpenHands#14370.- base: dict[str, Any] = {'agent_kind': new_kind}- else:- base = self.agent_settings.model_dump(+ # The SDK owns the discriminated-union merge: replace on+ # ``agent_kind`` change, deep-merge within a variant. Cross-kind+ # config preservation tracked in OpenHands/OpenHands#14370.+ new_settings = apply_agent_settings_diff(self.agent_settings, coerced)+ if replace_mcp_config:+ dumped = new_settings.model_dump(mode='json', context={'expose_secrets': True})-- merged = deep_merge(base, coerced)- if replace_mcp_config:- merged['mcp_config'] = mcp_config+ dumped['mcp_config'] = mcp_config+ new_settings = validate_agent_settings(dumped)# Use object.__setattr__ to avoid validate_assignment# side-effects on other fields.- object.__setattr__(self, 'agent_settings', validate_agent_settings(merged))+ object.__setattr__(self, 'agent_settings', new_settings)conv_update = payload.get('conversation_settings_diff')if isinstance(conv_update, dict):