Skip to content

The agent that grows with you

fix(telemetry): record the CLI switch after the swap; pin count_switch in config-sync test

teknium1b1d206d2e016
diff --git a/hermes_cli/cli_model_switch_mixin.py b/hermes_cli/cli_model_switch_mixin.py
index f479f5c4b7b..4c7350b7e54 100644
--- a/hermes_cli/cli_model_switch_mixin.py
+++ b/hermes_cli/cli_model_switch_mixin.py
@@ -239,13 +239,13 @@ def _commit_model_switch(
typed path additionally records the one-turn restore snapshot. ``reasoning_effort`` (from
``--reasoning`` or the picker's effort step) is applied after the swap."""
from cli import HermesCLI, _cprint
- from hermes_cli.observability.shared_metrics_events import record_model_switch
old_model, old_provider = cli.model, getattr(cli, "provider", None)
snapshot = cli._snapshot_model_runtime() if one_turn else None
if not cli._stage_and_swap_model(result, old_model):
return
# A TUI slash worker replays /model on its shadow CLI; the tui_gateway mirror counts the real switch.
if not getattr(cli, "is_slash_worker", False):
+ from hermes_cli.observability.shared_metrics_events import record_model_switch
record_model_switch(from_provider=old_provider, to_provider=result.target_provider, surface="cli")
if not picker:
cli._pending_one_turn_model_restore = snapshot
diff --git a/tests/tui_gateway/test_tui_gateway_server.py b/tests/tui_gateway/test_tui_gateway_server.py
index 906065fc88f..66deae5652d 100644
--- a/tests/tui_gateway/test_tui_gateway_server.py
+++ b/tests/tui_gateway/test_tui_gateway_server.py
@@ -4262,6 +4262,7 @@ def test_config_sync_switches_unpinned_session(monkeypatch):
"confirm_expensive_model": True,
"pin_session_override": False,
"persist_override": False,
+ "count_switch": False,
},
)
]
@@ -8475,7 +8476,7 @@ def test_config_set_yolo_global_scope_writes_approvals_mode(tmp_path, monkeypatc
import hermes_yaml as yaml
cfg_path = tmp_path / "config.yaml"
- cfg_path.write_text(yaml.safe_dump({"approvals": {"mode": "manual"}}))
+ cfg_path.write_text(yaml.safe_dump({"approvals": {"mode": "manual"}}), encoding="utf-8")
monkeypatch.setattr(server, "_hermes_home", tmp_path)
resp_on = server.handle_request(
@@ -8487,7 +8488,7 @@ def test_config_set_yolo_global_scope_writes_approvals_mode(tmp_path, monkeypatc
)
assert resp_on["result"]["value"] == "1"
assert resp_on["result"]["scope"] == "global"
- assert yaml.safe_load(cfg_path.read_text())["approvals"]["mode"] == "off"
+ assert yaml.safe_load(cfg_path.read_text(encoding="utf-8-sig"))["approvals"]["mode"] == "off"
resp_off = server.handle_request(
{
@@ -8497,7 +8498,7 @@ def test_config_set_yolo_global_scope_writes_approvals_mode(tmp_path, monkeypatc
}
)
assert resp_off["result"]["value"] == "0"
- assert yaml.safe_load(cfg_path.read_text())["approvals"]["mode"] == "manual"
+ assert yaml.safe_load(cfg_path.read_text(encoding="utf-8-sig"))["approvals"]["mode"] == "manual"
def test_config_get_approval_mode_uses_smart_default_when_key_is_missing(
@@ -8584,7 +8585,7 @@ def test_config_set_approval_mode_persists_three_way_value_and_emits_live_status
server._sessions.clear()
assert resp["result"] == {"key": "approvals.mode", "value": "manual"}
- assert yaml.safe_load((tmp_path / "config.yaml").read_text())["approvals"]["mode"] == "manual"
+ assert yaml.safe_load((tmp_path / "config.yaml").read_text(encoding="utf-8-sig"))["approvals"]["mode"] == "manual"
assert emitted and emitted[0][0:2] == ("session.info", "sid")
assert emitted[0][2]["approval_mode"] == "manual"
@@ -8678,7 +8679,7 @@ def test_config_set_yolo_global_scope_honors_explicit_value(tmp_path, monkeypatc
import hermes_yaml as yaml
cfg_path = tmp_path / "config.yaml"
- cfg_path.write_text(yaml.safe_dump({"approvals": {"mode": "manual"}}))
+ cfg_path.write_text(yaml.safe_dump({"approvals": {"mode": "manual"}}), encoding="utf-8")
monkeypatch.setattr(server, "_hermes_home", tmp_path)
resp = server.handle_request(
@@ -8689,7 +8690,7 @@ def test_config_set_yolo_global_scope_honors_explicit_value(tmp_path, monkeypatc
}
)
assert resp["result"]["value"] == "1"
- assert yaml.safe_load(cfg_path.read_text())["approvals"]["mode"] == "off"
+ assert yaml.safe_load(cfg_path.read_text(encoding="utf-8-sig"))["approvals"]["mode"] == "off"
# Setting it on again is idempotent — stays off.
resp_again = server.handle_request(
@@ -8700,7 +8701,7 @@ def test_config_set_yolo_global_scope_honors_explicit_value(tmp_path, monkeypatc
}
)
assert resp_again["result"]["value"] == "1"
- assert yaml.safe_load(cfg_path.read_text())["approvals"]["mode"] == "off"
+ assert yaml.safe_load(cfg_path.read_text(encoding="utf-8-sig"))["approvals"]["mode"] == "off"
def test_config_set_fast_updates_live_agent_session_scoped(monkeypatch):
@@ -8920,7 +8921,7 @@ def test_config_set_statusbar_survives_non_dict_display(tmp_path, monkeypatch):
import hermes_yaml as yaml
cfg_path = tmp_path / "config.yaml"
- cfg_path.write_text(yaml.safe_dump({"display": "broken"}))
+ cfg_path.write_text(yaml.safe_dump({"display": "broken"}), encoding="utf-8")
monkeypatch.setattr(server, "_hermes_home", tmp_path)
resp = server.handle_request(
@@ -8932,7 +8933,7 @@ def test_config_set_statusbar_survives_non_dict_display(tmp_path, monkeypatch):
)
assert resp["result"]["value"] == "bottom"
- saved = yaml.safe_load(cfg_path.read_text())
+ saved = yaml.safe_load(cfg_path.read_text(encoding="utf-8-sig"))
assert saved["display"]["tui_statusbar"] == "bottom"
@@ -8956,7 +8957,7 @@ def test_config_set_details_mode_pins_all_sections(tmp_path, monkeypatch):
)
assert resp["result"] == {"key": "details_mode", "value": "collapsed"}
- saved = yaml.safe_load(cfg_path.read_text())
+ saved = yaml.safe_load(cfg_path.read_text(encoding="utf-8-sig"))
assert saved["display"]["details_mode"] == "collapsed"
assert saved["display"]["sections"] == {
"thinking": "collapsed",
@@ -8981,7 +8982,7 @@ def test_config_set_section_writes_per_section_override(tmp_path, monkeypatch):
)
assert resp["result"] == {"key": "details_mode.activity", "value": "hidden"}
- saved = yaml.safe_load(cfg_path.read_text())
+ saved = yaml.safe_load(cfg_path.read_text(encoding="utf-8-sig"))
assert saved["display"]["sections"] == {"activity": "hidden"}
@@ -9005,7 +9006,7 @@ def test_config_set_section_clears_override_on_empty_value(tmp_path, monkeypatch
)
assert resp["result"] == {"key": "details_mode.activity", "value": ""}
- saved = yaml.safe_load(cfg_path.read_text())
+ saved = yaml.safe_load(cfg_path.read_text(encoding="utf-8-sig"))
assert saved["display"]["sections"] == {"tools": "expanded"}
@@ -9380,7 +9381,7 @@ def test_setup_readiness_scopes_to_requested_profile(monkeypatch, tmp_path):
)
assert status["result"] == {"provider_configured": False, "profile": "bot"}
- (bot_home / ".env").write_text("OPENROUTER_API_KEY=sk-or-bot-profile-secret-00001\n")
+ (bot_home / ".env").write_text("OPENROUTER_API_KEY=sk-or-bot-profile-secret-00001\n", encoding="utf-8")
status = server.handle_request(
{"id": "2", "method": "setup.status", "params": {"profile": "bot"}}
)
@@ -11422,7 +11423,7 @@ def test_file_attach_uploads_remote_file_into_session_workspace(monkeypatch, tmp
assert resp["result"]["uploaded"] is True
assert resp["result"]["path"] == str(stored)
assert resp["result"]["ref_text"] == f"@file:{stored}"
- assert stored.read_text(encoding="utf-8") == "hello world"
+ assert stored.read_text(encoding="utf-8-sig") == "hello world"
finally:
server._sessions.pop("sid", None)
@@ -11455,7 +11456,7 @@ def test_file_attach_copies_gateway_visible_file_outside_workspace(monkeypatch,
assert resp["result"]["attached"] is True
assert resp["result"]["uploaded"] is True
assert resp["result"]["ref_text"] == f"@file:{stored}"
- assert stored.read_text(encoding="utf-8") == "outside workspace"
+ assert stored.read_text(encoding="utf-8-sig") == "outside workspace"
finally:
server._sessions.pop("sid", None)
@@ -11548,7 +11549,7 @@ def test_file_attach_quotes_ref_with_spaces(monkeypatch, tmp_path):
stored = tmp_path / "home" / "attachments" / "my exam schedule.csv"
assert resp["result"]["attached"] is True
assert resp["result"]["ref_text"] == f"@file:`{stored}`"
- assert stored.read_text(encoding="utf-8") == "a,b\n"
+ assert stored.read_text(encoding="utf-8-sig") == "a,b\n"
finally:
server._sessions.pop("sid", None)
@@ -18859,7 +18860,7 @@ def test_session_save_writes_under_hermes_home_with_system_prompt(monkeypatch, t
assert saved_file.parent == saved_dir
assert saved_file.exists()
- payload = json.loads(saved_file.read_text())
+ payload = json.loads(saved_file.read_text(encoding="utf-8-sig"))
assert payload["model"] == "hermes-test"
assert payload["session_id"] == "20260101_120000_abc123"
assert payload["session_start"] == "2026-01-01T12:00:00"
@@ -21421,7 +21422,7 @@ def test_save_cfg_preserves_user_comments(tmp_path, monkeypatch):
}
)
- text = cfg_path.read_text(encoding="utf-8")
+ text = cfg_path.read_text(encoding="utf-8-sig")
assert "# top of file note" in text
assert "# provider rationale" in text
assert "# trailing skin note" in text
@@ -21460,7 +21461,7 @@ def test_save_cfg_preserves_top_level_key_order(tmp_path, monkeypatch):
}
)
- text = cfg_path.read_text(encoding="utf-8")
+ text = cfg_path.read_text(encoding="utf-8-sig")
top_keys = [
line.split(":", 1)[0]
for line in text.splitlines()
@@ -21493,7 +21494,7 @@ def test_save_cfg_keeps_unicode_personalities_readable(tmp_path, monkeypatch):
}
)
- text = cfg_path.read_text(encoding="utf-8")
+ text = cfg_path.read_text(encoding="utf-8-sig")
assert "你好" in text
assert "(=^・ω・^=)" in text
assert "\\u4f60" not in text
@@ -22513,7 +22514,7 @@ def test_persist_live_session_system_prompt_uses_profile_home(monkeypatch, tmp_p
home = get_hermes_home()
built_homes.append(str(home))
soul = (
- (home / "SOUL.md").read_text(encoding="utf-8")
+ (home / "SOUL.md").read_text(encoding="utf-8-sig")
if (home / "SOUL.md").exists()
else ""
)