The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(desktop): stop model_context_length edits from being dropped or wiped
chelsealong6d407ca1a4fa
diff --git a/hermes_cli/web_server.py b/hermes_cli/web_server.pyindex e99a90a98ea..050e0f3815f 100644--- a/hermes_cli/web_server.py+++ b/hermes_cli/web_server.py@@ -8026,15 +8026,22 @@ def _denormalize_config_from_web(config: Dict[str, Any]) -> Dict[str, Any]:string; the rest is preserved transparently.Also handles ``model_context_length`` — writes it back into the model dict- as ``context_length``. A value of 0 or absent means "auto-detect" (omitted- from the dict so get_model_context_length() uses its normal resolution).+ as ``context_length``. A value of 0 means "auto-detect" (omitted from the+ dict so get_model_context_length() uses its normal resolution). ``config``+ may be a partial update (e.g. the Settings autosave diff) that omits+ ``model_context_length`` entirely when the user didn't touch it — that+ must leave the on-disk override untouched, not get treated the same as an+ explicit 0 and cleared."""config = dict(config)# Remove any _model_meta that might have leaked in (shouldn't happen# with the stripped GET response, but be defensive)config.pop("_model_meta", None)- # Extract and remove model_context_length before processing model+ # Extract and remove model_context_length before processing model, but+ # remember whether it was actually present: a partial update omitting the+ # key means "unchanged", which is different from an explicit 0.+ ctx_sent = "model_context_length" in configctx_override = config.pop("model_context_length", 0)if not isinstance(ctx_override, int):try:@@ -8043,50 +8050,59 @@ def _denormalize_config_from_web(config: Dict[str, Any]) -> Dict[str, Any]:ctx_override = 0model_val = config.get("model")- if isinstance(model_val, str) and model_val:+ if (isinstance(model_val, str) and model_val) or ctx_sent:# Read the current disk config to recover model subkeystry:disk_config = load_config()disk_model = disk_config.get("model")if isinstance(disk_model, dict):- prev_default = str(disk_model.get("default") or "").strip()- prev_provider = str(disk_model.get("provider") or "").strip()- # When the model name actually changed, re-detect which- # provider serves it. The Config-page Model field is a flat- # string with no provider info, so without this a user who- # picks an OpenRouter model while their default provider is- # ollama-local keeps the stale provider and 404s. Only fires- # on a real model change so saving unrelated config fields- # never overwrites an explicit provider.- if model_val != prev_default and prev_provider:- new_provider, resolved_model = _infer_provider_on_model_change(- model_val, prev_provider- )- if new_provider and new_provider.strip().lower() != prev_provider.lower():- # Route through the canonical assignment chokepoints so- # the model is normalized for the new provider and stale- # base_url/api_mode/api_key are cleared on the switch- # (and preserved on a same-provider re-pick).- norm_provider, norm_model = _normalize_main_model_assignment(- new_provider, resolved_model- )- disk_model = _apply_main_model_assignment(- disk_model, norm_provider, norm_model+ if isinstance(model_val, str) and model_val:+ prev_default = str(disk_model.get("default") or "").strip()+ prev_provider = str(disk_model.get("provider") or "").strip()+ # When the model name actually changed, re-detect which+ # provider serves it. The Config-page Model field is a flat+ # string with no provider info, so without this a user who+ # picks an OpenRouter model while their default provider is+ # ollama-local keeps the stale provider and 404s. Only fires+ # on a real model change so saving unrelated config fields+ # never overwrites an explicit provider.+ if model_val != prev_default and prev_provider:+ new_provider, resolved_model = _infer_provider_on_model_change(+ model_val, prev_provider)- model_val = norm_model- # Preserve all subkeys, update default with the new value- disk_model["default"] = model_val- # Write context_length into the model dict (0 = remove/auto)- if ctx_override > 0:- disk_model["context_length"] = ctx_override- else:- disk_model.pop("context_length", None)+ if new_provider and new_provider.strip().lower() != prev_provider.lower():+ # Route through the canonical assignment chokepoints so+ # the model is normalized for the new provider and stale+ # base_url/api_mode/api_key are cleared on the switch+ # (and preserved on a same-provider re-pick).+ norm_provider, norm_model = _normalize_main_model_assignment(+ new_provider, resolved_model+ )+ disk_model = _apply_main_model_assignment(+ disk_model, norm_provider, norm_model+ )+ model_val = norm_model+ # Preserve all subkeys, update default with the new value+ disk_model["default"] = model_val+ # Write context_length into the model dict (0 = remove/auto),+ # but only when the payload actually carried the key.+ if ctx_sent:+ if ctx_override > 0:+ disk_model["context_length"] = ctx_override+ else:+ disk_model.pop("context_length", None)config["model"] = disk_model- # Model was previously a bare string — upgrade to dict if- # user is setting a context_length override- elif ctx_override > 0:+ # Model was previously a bare string (or absent) — upgrade to a+ # dict if the user is setting a context_length override.+ elif ctx_sent and ctx_override > 0:+ if isinstance(model_val, str) and model_val:+ default = model_val+ elif isinstance(disk_model, str) and disk_model:+ default = disk_model+ else:+ default = ""config["model"] = {- "default": model_val,+ "default": default,"context_length": ctx_override,}except Exception:diff --git a/tests/hermes_cli/test_web_server.py b/tests/hermes_cli/test_web_server.pyindex f0591b241d2..a4a135dbe21 100644--- a/tests/hermes_cli/test_web_server.py+++ b/tests/hermes_cli/test_web_server.py@@ -3094,6 +3094,43 @@ class TestModelContextLength:assert result["model"]["context_length"] == 100000assert "model_context_length" not in result # virtual field removed+ def test_denormalize_context_length_alone_is_applied(self):+ """The Settings autosave now sends a diff, not the full draft: editing+ only the Context Window control must not omit ``model`` and thereby+ drop the context_length edit on the floor (#89597 review)."""+ from hermes_cli.web_server import _denormalize_config_from_web+ from hermes_cli.config import save_config++ save_config({+ "model": {"default": "anthropic/claude-sonnet-4", "provider": "anthropic",+ "context_length": 100000}+ })++ result = _denormalize_config_from_web({"model_context_length": 200000})+ assert isinstance(result["model"], dict)+ assert result["model"]["context_length"] == 200000+ assert result["model"]["default"] == "anthropic/claude-sonnet-4"++ def test_denormalize_model_alone_preserves_context_length(self):+ """The mirror case: editing only the Model field must not silently+ wipe an existing context_length override just because the diff omits+ the unrelated model_context_length key (#89597 review).++ No ``provider`` on disk here on purpose: that keeps this test isolated+ to the diff-omission bug rather than the separate, pre-existing (and+ intentional, see ``_apply_main_model_assignment``) behavior where a+ real provider switch drops the context_length override."""+ from hermes_cli.web_server import _denormalize_config_from_web+ from hermes_cli.config import save_config++ save_config({+ "model": {"default": "anthropic/claude-sonnet-4", "context_length": 150000}+ })++ result = _denormalize_config_from_web({"model": "anthropic/claude-opus-4.6"})+ assert result["model"]["context_length"] == 150000+ assert result["model"]["default"] == "anthropic/claude-opus-4.6"+class TestDenormalizeProviderSwitch:"""The flat Config-page Model field carries no provider info. When the