fix(desktop): stop model_context_length edits from being dropped or wiped
_denormalize_config_from_web only wrote model_context_length into the on-disk model dict inside the branch gated on `model` also being present in the payload. That was harmless when the frontend always sent the full config, but the prior commit switched Settings autosave to send only the diff (diffConfig), so editing the Context Window control alone omits `model` from the payload and the context-length edit is silently thrown away. The mirror case regressed too: editing `model` alone now omits model_context_length from the diff, and the old code treated that missing key the same as an explicit 0, wiping an existing context_length override that the user never touched. Track whether model_context_length was actually present in the payload and only mutate context_length when it was, independent of whether `model` also changed.
This commit is contained in:
+55
-39
@@ -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 config
|
||||
ctx_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 = 0
|
||||
|
||||
model_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 subkeys
|
||||
try:
|
||||
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
|
||||
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
|
||||
)
|
||||
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)
|
||||
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:
|
||||
|
||||
@@ -3094,6 +3094,43 @@ class TestModelContextLength:
|
||||
assert result["model"]["context_length"] == 100000
|
||||
assert "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
|
||||
|
||||
Reference in New Issue
Block a user