fix(opencode): drop a stale extra-model override of a built-in id

`_drop_stale_managed_models` skipped an id that was still advertised by the
current wrap, so an extra model overriding a built-in (`--extra-model
gpt-4o=Override=8000=4000`, then a re-wrap with no extra flag under
`--openai-api-url`) kept its override and limits: `gpt-4o` stayed
advertised, but not with the entry Headroom now writes for it.

An entry is stale when it still matches what Headroom injected earlier and
differs from what the current wrap writes for that id, whether or not the
id is still in the injected set.
This commit is contained in:
Parideboy
2026-11-13 15:11:16 +01:00
parent 6919fc9166
commit 6be8489420
2 changed files with 28 additions and 4 deletions
+7 -4
View File
@@ -534,17 +534,20 @@ def _drop_stale_managed_models(
prior_state: OpencodeManagedState | None,
injected_models: dict[str, Any],
) -> dict[str, Any]:
"""Drop models Headroom injected earlier but no longer advertises.
"""Drop entries Headroom injected earlier but no longer advertises.
Only unmodified entries are dropped: a model the user edited after the
wrap is their content, not Headroom's.
That is an extra model dropped from the flags, and an extra that overrode
a built-in id and is dropped again: the id stays advertised, but what
Headroom writes for it differs, so the old override must not win the
merge. Only unmodified entries are dropped: a model the user edited after
the wrap is their content, not Headroom's.
"""
if prior_state is None:
return existing_models
stale = {
model_id
for model_id, injected in prior_state.managed_models.items()
if model_id not in injected_models and existing_models.get(model_id) == injected
if existing_models.get(model_id) == injected and injected_models.get(model_id) != injected
}
if not stale:
return existing_models
+21
View File
@@ -10,6 +10,7 @@ import pytest
from headroom.providers.opencode.config import (
HEADROOM_OPENCODE_EXTRA_MODELS_ENV,
HEADROOM_OPENCODE_MODELS,
HEADROOM_OPENCODE_PLUGIN,
_inject_key_into_json,
_parse_json_loose,
@@ -996,6 +997,26 @@ def test_reinject_prunes_a_dropped_extra_model(
assert set(models) == {"gpt-4o", "gpt-4.1"}
@pytest.mark.parametrize("keep_user_entries", [False, True])
def test_reinject_prunes_a_dropped_override_of_a_builtin_id(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, keep_user_entries: bool
) -> None:
"""A dropped --extra-model override of a built-in restores the defaults."""
_set_test_home(monkeypatch, tmp_path)
config_file = tmp_path / ".config" / "opencode" / "opencode.json"
override = {"name": "Override", "limit": {"context": 8000, "output": 4000}}
inject_opencode_provider_config(
port=8787, extra_models={"gpt-4o": override}, keep_user_entries=keep_user_entries
)
inject_opencode_provider_config(port=8787, keep_user_entries=keep_user_entries)
models = _parse_json_loose(config_file.read_text(encoding="utf-8"))["provider"]["headroom"][
"models"
]
assert models["gpt-4o"] == HEADROOM_OPENCODE_MODELS["gpt-4o"]
def test_reinject_keeps_a_user_model_and_a_edited_extra(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None: