2026-04-06 18:38:13 -05:00
|
|
|
import json
|
2026-04-21 12:23:17 -05:00
|
|
|
import os
|
2026-04-11 14:02:36 -05:00
|
|
|
import sys
|
2026-04-06 18:38:13 -05:00
|
|
|
import threading
|
|
|
|
|
import time
|
2026-04-11 14:02:36 -05:00
|
|
|
import types
|
|
|
|
|
from pathlib import Path
|
2026-04-06 18:38:13 -05:00
|
|
|
from unittest.mock import patch
|
|
|
|
|
|
|
|
|
|
from tui_gateway import server
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class _ChunkyStdout:
|
|
|
|
|
def __init__(self):
|
|
|
|
|
self.parts: list[str] = []
|
|
|
|
|
|
|
|
|
|
def write(self, text: str) -> int:
|
|
|
|
|
for ch in text:
|
|
|
|
|
self.parts.append(ch)
|
|
|
|
|
time.sleep(0.0001)
|
|
|
|
|
return len(text)
|
|
|
|
|
|
|
|
|
|
def flush(self) -> None:
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class _BrokenStdout:
|
|
|
|
|
def write(self, text: str) -> int:
|
|
|
|
|
raise BrokenPipeError
|
|
|
|
|
|
|
|
|
|
def flush(self) -> None:
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_write_json_serializes_concurrent_writes(monkeypatch):
|
|
|
|
|
out = _ChunkyStdout()
|
2026-04-11 14:02:36 -05:00
|
|
|
monkeypatch.setattr(server, "_real_stdout", out)
|
2026-04-06 18:38:13 -05:00
|
|
|
|
|
|
|
|
threads = [
|
|
|
|
|
threading.Thread(target=server.write_json, args=({"seq": i, "text": "x" * 24},))
|
|
|
|
|
for i in range(8)
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
for t in threads:
|
|
|
|
|
t.start()
|
|
|
|
|
|
|
|
|
|
for t in threads:
|
|
|
|
|
t.join()
|
|
|
|
|
|
|
|
|
|
lines = "".join(out.parts).splitlines()
|
|
|
|
|
|
|
|
|
|
assert len(lines) == 8
|
|
|
|
|
assert {json.loads(line)["seq"] for line in lines} == set(range(8))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_write_json_returns_false_on_broken_pipe(monkeypatch):
|
2026-04-11 14:02:36 -05:00
|
|
|
monkeypatch.setattr(server, "_real_stdout", _BrokenStdout())
|
2026-04-06 18:38:13 -05:00
|
|
|
|
|
|
|
|
assert server.write_json({"ok": True}) is False
|
|
|
|
|
|
|
|
|
|
|
2026-04-26 15:16:12 -05:00
|
|
|
def test_history_to_messages_preserves_tool_calls_for_resume_display():
|
|
|
|
|
history = [
|
|
|
|
|
{"role": "user", "content": "first prompt"},
|
|
|
|
|
{
|
|
|
|
|
"role": "assistant",
|
|
|
|
|
"content": "",
|
|
|
|
|
"tool_calls": [
|
|
|
|
|
{
|
|
|
|
|
"id": "call_1",
|
|
|
|
|
"function": {
|
|
|
|
|
"name": "search_files",
|
|
|
|
|
"arguments": json.dumps({"pattern": "resume"}),
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
{"role": "tool", "content": "{}", "tool_call_id": "call_1"},
|
|
|
|
|
{"role": "assistant", "content": "first answer"},
|
|
|
|
|
{"role": "user", "content": "second prompt"},
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
assert server._history_to_messages(history) == [
|
|
|
|
|
{"role": "user", "text": "first prompt"},
|
|
|
|
|
{"context": "resume", "name": "search_files", "role": "tool"},
|
|
|
|
|
{"role": "assistant", "text": "first answer"},
|
|
|
|
|
{"role": "user", "text": "second prompt"},
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_session_resume_uses_parent_lineage_for_display(monkeypatch):
|
|
|
|
|
captured = {}
|
|
|
|
|
|
|
|
|
|
class FakeDB:
|
|
|
|
|
def get_session(self, target):
|
|
|
|
|
return {"id": target}
|
|
|
|
|
|
|
|
|
|
def reopen_session(self, target):
|
|
|
|
|
captured["reopened"] = target
|
|
|
|
|
|
|
|
|
|
def get_messages_as_conversation(self, target, include_ancestors=False):
|
|
|
|
|
captured.setdefault("history_calls", []).append((target, include_ancestors))
|
2026-04-27 11:49:02 -05:00
|
|
|
return (
|
|
|
|
|
[
|
|
|
|
|
{"role": "user", "content": "root prompt"},
|
|
|
|
|
{"role": "assistant", "content": "root answer"},
|
|
|
|
|
]
|
|
|
|
|
if include_ancestors
|
|
|
|
|
else [{"role": "user", "content": "tip prompt"}]
|
|
|
|
|
)
|
2026-04-26 15:16:12 -05:00
|
|
|
|
|
|
|
|
monkeypatch.setattr(server, "_get_db", lambda: FakeDB())
|
|
|
|
|
monkeypatch.setattr(server, "_enable_gateway_prompts", lambda: None)
|
|
|
|
|
monkeypatch.setattr(server, "_set_session_context", lambda target: [])
|
|
|
|
|
monkeypatch.setattr(server, "_clear_session_context", lambda tokens: None)
|
2026-04-27 11:49:02 -05:00
|
|
|
monkeypatch.setattr(
|
|
|
|
|
server,
|
|
|
|
|
"_make_agent",
|
|
|
|
|
lambda *args, **kwargs: types.SimpleNamespace(model="test"),
|
|
|
|
|
)
|
|
|
|
|
monkeypatch.setattr(
|
|
|
|
|
server,
|
|
|
|
|
"_session_info",
|
|
|
|
|
lambda agent: {"model": "test", "tools": {}, "skills": {}},
|
|
|
|
|
)
|
|
|
|
|
monkeypatch.setattr(
|
|
|
|
|
server, "_init_session", lambda sid, key, agent, history, cols=80: None
|
|
|
|
|
)
|
2026-04-26 15:16:12 -05:00
|
|
|
|
2026-04-27 11:49:02 -05:00
|
|
|
resp = server.handle_request(
|
|
|
|
|
{"id": "1", "method": "session.resume", "params": {"session_id": "tip"}}
|
|
|
|
|
)
|
2026-04-26 15:16:12 -05:00
|
|
|
|
|
|
|
|
assert resp["result"]["messages"] == [
|
|
|
|
|
{"role": "user", "text": "root prompt"},
|
|
|
|
|
{"role": "assistant", "text": "root answer"},
|
|
|
|
|
]
|
|
|
|
|
assert captured["history_calls"] == [("tip", False), ("tip", True)]
|
|
|
|
|
|
|
|
|
|
|
2026-04-06 18:38:13 -05:00
|
|
|
def test_status_callback_emits_kind_and_text():
|
|
|
|
|
with patch("tui_gateway.server._emit") as emit:
|
|
|
|
|
cb = server._agent_cbs("sid")["status_callback"]
|
|
|
|
|
cb("context_pressure", "85% to compaction")
|
|
|
|
|
|
|
|
|
|
emit.assert_called_once_with(
|
|
|
|
|
"status.update",
|
|
|
|
|
"sid",
|
|
|
|
|
{"kind": "context_pressure", "text": "85% to compaction"},
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_status_callback_accepts_single_message_argument():
|
|
|
|
|
with patch("tui_gateway.server._emit") as emit:
|
|
|
|
|
cb = server._agent_cbs("sid")["status_callback"]
|
|
|
|
|
cb("thinking...")
|
|
|
|
|
|
|
|
|
|
emit.assert_called_once_with(
|
|
|
|
|
"status.update",
|
|
|
|
|
"sid",
|
|
|
|
|
{"kind": "status", "text": "thinking..."},
|
|
|
|
|
)
|
2026-04-11 14:02:36 -05:00
|
|
|
|
|
|
|
|
|
2026-04-25 13:21:59 -05:00
|
|
|
def test_resolve_model_uses_inference_model_env(monkeypatch):
|
|
|
|
|
monkeypatch.delenv("HERMES_MODEL", raising=False)
|
2026-04-25 13:29:15 -05:00
|
|
|
monkeypatch.setenv("HERMES_INFERENCE_MODEL", " anthropic/claude-sonnet-4.6\n")
|
2026-04-25 13:21:59 -05:00
|
|
|
|
|
|
|
|
assert server._resolve_model() == "anthropic/claude-sonnet-4.6"
|
|
|
|
|
|
|
|
|
|
|
2026-04-25 13:29:15 -05:00
|
|
|
def test_resolve_model_strips_config_model(monkeypatch):
|
|
|
|
|
monkeypatch.delenv("HERMES_MODEL", raising=False)
|
|
|
|
|
monkeypatch.delenv("HERMES_INFERENCE_MODEL", raising=False)
|
|
|
|
|
monkeypatch.setattr(
|
|
|
|
|
server, "_load_cfg", lambda: {"model": {"default": " nous/hermes-test "}}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert server._resolve_model() == "nous/hermes-test"
|
|
|
|
|
|
|
|
|
|
|
2026-04-25 13:21:59 -05:00
|
|
|
def test_startup_runtime_uses_tui_provider_env(monkeypatch):
|
|
|
|
|
monkeypatch.setenv("HERMES_MODEL", "nous/hermes-test")
|
|
|
|
|
monkeypatch.setenv("HERMES_TUI_PROVIDER", "nous")
|
|
|
|
|
monkeypatch.delenv("HERMES_INFERENCE_PROVIDER", raising=False)
|
|
|
|
|
|
|
|
|
|
assert server._resolve_startup_runtime() == ("nous/hermes-test", "nous")
|
|
|
|
|
|
|
|
|
|
|
2026-04-25 13:25:43 -05:00
|
|
|
def test_startup_runtime_does_not_treat_inference_provider_as_explicit(monkeypatch):
|
|
|
|
|
monkeypatch.setenv("HERMES_MODEL", "nous/hermes-test")
|
|
|
|
|
monkeypatch.delenv("HERMES_TUI_PROVIDER", raising=False)
|
|
|
|
|
monkeypatch.setenv("HERMES_INFERENCE_PROVIDER", "nous")
|
2026-04-25 13:47:18 -05:00
|
|
|
monkeypatch.setattr(
|
2026-04-25 13:56:16 -05:00
|
|
|
"hermes_cli.models.detect_static_provider_for_model",
|
|
|
|
|
lambda model, provider: None,
|
2026-04-25 13:47:18 -05:00
|
|
|
)
|
2026-04-25 13:25:43 -05:00
|
|
|
|
|
|
|
|
assert server._resolve_startup_runtime() == ("nous/hermes-test", None)
|
|
|
|
|
|
|
|
|
|
|
2026-04-25 13:21:59 -05:00
|
|
|
def test_startup_runtime_detects_provider_for_model_env(monkeypatch):
|
|
|
|
|
monkeypatch.setenv("HERMES_MODEL", "sonnet")
|
|
|
|
|
monkeypatch.delenv("HERMES_TUI_PROVIDER", raising=False)
|
|
|
|
|
monkeypatch.delenv("HERMES_INFERENCE_PROVIDER", raising=False)
|
|
|
|
|
monkeypatch.setattr(server, "_load_cfg", lambda: {"model": {"provider": "auto"}})
|
|
|
|
|
|
|
|
|
|
def fake_detect(model, current_provider):
|
|
|
|
|
assert model == "sonnet"
|
|
|
|
|
assert current_provider == "auto"
|
|
|
|
|
return "anthropic", "anthropic/claude-sonnet-4.6"
|
|
|
|
|
|
2026-04-25 13:56:16 -05:00
|
|
|
monkeypatch.setattr(
|
|
|
|
|
"hermes_cli.models.detect_static_provider_for_model", fake_detect
|
|
|
|
|
)
|
2026-04-25 13:21:59 -05:00
|
|
|
|
|
|
|
|
assert server._resolve_startup_runtime() == (
|
|
|
|
|
"anthropic/claude-sonnet-4.6",
|
|
|
|
|
"anthropic",
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2026-04-25 14:13:02 -05:00
|
|
|
def test_startup_runtime_resolves_short_alias_without_network(monkeypatch):
|
|
|
|
|
monkeypatch.setenv("HERMES_MODEL", "sonnet")
|
|
|
|
|
monkeypatch.delenv("HERMES_TUI_PROVIDER", raising=False)
|
|
|
|
|
monkeypatch.delenv("HERMES_INFERENCE_PROVIDER", raising=False)
|
|
|
|
|
monkeypatch.setattr(server, "_load_cfg", lambda: {"model": {"provider": "auto"}})
|
|
|
|
|
monkeypatch.setattr(
|
|
|
|
|
"hermes_cli.models.fetch_openrouter_models",
|
|
|
|
|
lambda *_args, **_kwargs: (_ for _ in ()).throw(
|
|
|
|
|
AssertionError("network lookup should not run")
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
model, provider = server._resolve_startup_runtime()
|
|
|
|
|
|
|
|
|
|
assert provider == "anthropic"
|
|
|
|
|
assert model.startswith("claude-sonnet")
|
|
|
|
|
|
|
|
|
|
|
2026-04-25 13:47:18 -05:00
|
|
|
def test_startup_runtime_does_not_call_network_detector(monkeypatch):
|
|
|
|
|
monkeypatch.setenv("HERMES_MODEL", "sonnet")
|
|
|
|
|
monkeypatch.delenv("HERMES_TUI_PROVIDER", raising=False)
|
|
|
|
|
monkeypatch.delenv("HERMES_INFERENCE_PROVIDER", raising=False)
|
|
|
|
|
monkeypatch.setattr(server, "_load_cfg", lambda: {"model": {"provider": "auto"}})
|
|
|
|
|
monkeypatch.setattr(
|
|
|
|
|
"hermes_cli.models.detect_provider_for_model",
|
2026-04-25 13:56:16 -05:00
|
|
|
lambda *_args, **_kwargs: (_ for _ in ()).throw(
|
|
|
|
|
AssertionError("network detector called")
|
|
|
|
|
),
|
2026-04-25 13:47:18 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
model, provider = server._resolve_startup_runtime()
|
|
|
|
|
|
|
|
|
|
assert model
|
|
|
|
|
assert provider in {None, "anthropic"}
|
|
|
|
|
|
|
|
|
|
|
2026-04-11 14:02:36 -05:00
|
|
|
def _session(agent=None, **extra):
|
|
|
|
|
return {
|
|
|
|
|
"agent": agent if agent is not None else types.SimpleNamespace(),
|
|
|
|
|
"session_key": "session-key",
|
|
|
|
|
"history": [],
|
|
|
|
|
"history_lock": threading.Lock(),
|
|
|
|
|
"history_version": 0,
|
|
|
|
|
"running": False,
|
|
|
|
|
"attached_images": [],
|
|
|
|
|
"image_counter": 0,
|
|
|
|
|
"cols": 80,
|
|
|
|
|
"slash_worker": None,
|
|
|
|
|
"show_reasoning": False,
|
|
|
|
|
"tool_progress_mode": "all",
|
|
|
|
|
**extra,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2026-04-27 12:20:08 -05:00
|
|
|
def test_session_close_commits_memory_and_fires_finalize_hook(monkeypatch):
|
|
|
|
|
calls = {"hooks": []}
|
|
|
|
|
|
|
|
|
|
agent = types.SimpleNamespace(session_id="session-key")
|
|
|
|
|
agent.commit_memory_session = lambda history: calls.setdefault("history", history)
|
|
|
|
|
server._sessions["sid"] = _session(
|
|
|
|
|
agent=agent, history=[{"role": "user", "content": "hello"}]
|
|
|
|
|
)
|
|
|
|
|
monkeypatch.setattr(
|
|
|
|
|
server,
|
|
|
|
|
"_notify_session_boundary",
|
|
|
|
|
lambda event, session_id: calls["hooks"].append((event, session_id)),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
resp = server.handle_request(
|
|
|
|
|
{"id": "1", "method": "session.close", "params": {"session_id": "sid"}}
|
|
|
|
|
)
|
|
|
|
|
assert resp["result"]["closed"] is True
|
|
|
|
|
assert calls["history"] == [{"role": "user", "content": "hello"}]
|
|
|
|
|
assert ("on_session_finalize", "session-key") in calls["hooks"]
|
|
|
|
|
finally:
|
|
|
|
|
server._sessions.pop("sid", None)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_init_session_fires_reset_hook(monkeypatch):
|
|
|
|
|
hooks = []
|
|
|
|
|
|
|
|
|
|
class _FakeWorker:
|
|
|
|
|
def __init__(self, key, model):
|
|
|
|
|
self.key = key
|
|
|
|
|
|
|
|
|
|
def close(self):
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
monkeypatch.setattr(server, "_SlashWorker", _FakeWorker)
|
|
|
|
|
monkeypatch.setattr(server, "_wire_callbacks", lambda _sid: None)
|
|
|
|
|
monkeypatch.setattr(server, "_emit", lambda *args, **kwargs: None)
|
|
|
|
|
monkeypatch.setattr(
|
|
|
|
|
server,
|
|
|
|
|
"_notify_session_boundary",
|
|
|
|
|
lambda event, session_id: hooks.append((event, session_id)),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
import tools.approval as _approval
|
|
|
|
|
|
|
|
|
|
monkeypatch.setattr(_approval, "register_gateway_notify", lambda key, cb: None)
|
|
|
|
|
monkeypatch.setattr(_approval, "load_permanent_allowlist", lambda: None)
|
|
|
|
|
|
|
|
|
|
sid = "sid"
|
|
|
|
|
try:
|
|
|
|
|
server._init_session(
|
|
|
|
|
sid,
|
|
|
|
|
"session-key",
|
|
|
|
|
types.SimpleNamespace(model="x"),
|
|
|
|
|
history=[],
|
|
|
|
|
cols=80,
|
|
|
|
|
)
|
|
|
|
|
assert ("on_session_reset", "session-key") in hooks
|
|
|
|
|
finally:
|
|
|
|
|
server._sessions.pop(sid, None)
|
|
|
|
|
|
|
|
|
|
|
2026-04-27 10:51:14 -05:00
|
|
|
def test_session_title_queues_when_db_row_not_ready(monkeypatch):
|
|
|
|
|
class _FakeDB:
|
|
|
|
|
def get_session_title(self, _key):
|
|
|
|
|
return None
|
|
|
|
|
|
2026-04-27 11:05:10 -05:00
|
|
|
def get_session(self, _key):
|
|
|
|
|
return None
|
|
|
|
|
|
2026-04-27 10:51:14 -05:00
|
|
|
def set_session_title(self, _key, _title):
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
server._sessions["sid"] = _session(pending_title=None)
|
|
|
|
|
monkeypatch.setattr(server, "_get_db", lambda: _FakeDB())
|
|
|
|
|
try:
|
|
|
|
|
set_resp = server.handle_request(
|
|
|
|
|
{
|
|
|
|
|
"id": "1",
|
|
|
|
|
"method": "session.title",
|
|
|
|
|
"params": {"session_id": "sid", "title": "queued title"},
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert set_resp["result"]["pending"] is True
|
|
|
|
|
assert set_resp["result"]["title"] == "queued title"
|
|
|
|
|
assert server._sessions["sid"]["pending_title"] == "queued title"
|
|
|
|
|
|
|
|
|
|
get_resp = server.handle_request(
|
|
|
|
|
{"id": "2", "method": "session.title", "params": {"session_id": "sid"}}
|
|
|
|
|
)
|
|
|
|
|
assert get_resp["result"]["title"] == "queued title"
|
|
|
|
|
finally:
|
|
|
|
|
server._sessions.pop("sid", None)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_session_title_clears_pending_after_persist(monkeypatch):
|
|
|
|
|
class _FakeDB:
|
|
|
|
|
def __init__(self):
|
|
|
|
|
self.title = "old"
|
|
|
|
|
|
|
|
|
|
def get_session_title(self, _key):
|
|
|
|
|
return self.title
|
|
|
|
|
|
2026-04-27 11:05:10 -05:00
|
|
|
def get_session(self, _key):
|
|
|
|
|
return {"id": _key, "title": self.title}
|
|
|
|
|
|
2026-04-27 10:51:14 -05:00
|
|
|
def set_session_title(self, _key, title):
|
|
|
|
|
self.title = title
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
db = _FakeDB()
|
|
|
|
|
server._sessions["sid"] = _session(pending_title="stale")
|
|
|
|
|
monkeypatch.setattr(server, "_get_db", lambda: db)
|
|
|
|
|
try:
|
|
|
|
|
resp = server.handle_request(
|
|
|
|
|
{
|
|
|
|
|
"id": "1",
|
|
|
|
|
"method": "session.title",
|
|
|
|
|
"params": {"session_id": "sid", "title": "fresh"},
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert resp["result"]["pending"] is False
|
|
|
|
|
assert resp["result"]["title"] == "fresh"
|
|
|
|
|
assert server._sessions["sid"]["pending_title"] is None
|
|
|
|
|
finally:
|
|
|
|
|
server._sessions.pop("sid", None)
|
|
|
|
|
|
|
|
|
|
|
2026-04-27 11:05:10 -05:00
|
|
|
def test_session_title_does_not_queue_noop_when_row_exists(monkeypatch):
|
|
|
|
|
class _FakeDB:
|
|
|
|
|
def __init__(self):
|
|
|
|
|
self.title = "same title"
|
|
|
|
|
|
|
|
|
|
def get_session_title(self, _key):
|
|
|
|
|
return self.title
|
|
|
|
|
|
|
|
|
|
def get_session(self, _key):
|
|
|
|
|
return {"id": _key, "title": self.title}
|
|
|
|
|
|
|
|
|
|
def set_session_title(self, _key, _title):
|
|
|
|
|
# Simulate sqlite UPDATE rowcount==0 for no-op update.
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
server._sessions["sid"] = _session(pending_title="stale")
|
|
|
|
|
monkeypatch.setattr(server, "_get_db", lambda: _FakeDB())
|
|
|
|
|
try:
|
|
|
|
|
resp = server.handle_request(
|
|
|
|
|
{
|
|
|
|
|
"id": "1",
|
|
|
|
|
"method": "session.title",
|
|
|
|
|
"params": {"session_id": "sid", "title": "same title"},
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert resp["result"]["pending"] is False
|
|
|
|
|
assert resp["result"]["title"] == "same title"
|
|
|
|
|
assert server._sessions["sid"]["pending_title"] is None
|
|
|
|
|
finally:
|
|
|
|
|
server._sessions.pop("sid", None)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_session_title_get_falls_back_to_pending_when_db_read_throws(monkeypatch):
|
|
|
|
|
class _FakeDB:
|
|
|
|
|
def get_session_title(self, _key):
|
|
|
|
|
raise RuntimeError("db temporarily locked")
|
|
|
|
|
|
|
|
|
|
server._sessions["sid"] = _session(pending_title="queued title")
|
|
|
|
|
monkeypatch.setattr(server, "_get_db", lambda: _FakeDB())
|
|
|
|
|
try:
|
|
|
|
|
resp = server.handle_request(
|
|
|
|
|
{"id": "1", "method": "session.title", "params": {"session_id": "sid"}}
|
|
|
|
|
)
|
|
|
|
|
assert resp["result"]["title"] == "queued title"
|
|
|
|
|
finally:
|
|
|
|
|
server._sessions.pop("sid", None)
|
|
|
|
|
|
|
|
|
|
|
2026-04-27 11:27:51 -05:00
|
|
|
def test_session_title_get_retries_persist_for_pending_title(monkeypatch):
|
|
|
|
|
class _FakeDB:
|
|
|
|
|
def __init__(self):
|
|
|
|
|
self.title = ""
|
|
|
|
|
|
|
|
|
|
def get_session_title(self, _key):
|
|
|
|
|
return self.title
|
|
|
|
|
|
|
|
|
|
def set_session_title(self, _key, title):
|
|
|
|
|
self.title = title
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
def get_session(self, _key):
|
|
|
|
|
return {"id": _key, "title": self.title}
|
2026-04-27 11:31:49 -05:00
|
|
|
|
|
|
|
|
db = _FakeDB()
|
|
|
|
|
server._sessions["sid"] = _session(pending_title="queued title")
|
|
|
|
|
monkeypatch.setattr(server, "_get_db", lambda: db)
|
|
|
|
|
try:
|
|
|
|
|
resp = server.handle_request(
|
|
|
|
|
{"id": "1", "method": "session.title", "params": {"session_id": "sid"}}
|
|
|
|
|
)
|
|
|
|
|
assert resp["result"]["title"] == "queued title"
|
|
|
|
|
assert server._sessions["sid"]["pending_title"] is None
|
|
|
|
|
finally:
|
|
|
|
|
server._sessions.pop("sid", None)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_session_title_get_retries_pending_even_when_db_has_title(monkeypatch):
|
|
|
|
|
class _FakeDB:
|
|
|
|
|
def __init__(self):
|
|
|
|
|
self.title = "auto title"
|
|
|
|
|
|
|
|
|
|
def get_session_title(self, _key):
|
|
|
|
|
return self.title
|
|
|
|
|
|
|
|
|
|
def set_session_title(self, _key, title):
|
|
|
|
|
self.title = title
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
def get_session(self, _key):
|
|
|
|
|
return {"id": _key, "title": self.title}
|
2026-04-27 11:27:51 -05:00
|
|
|
|
|
|
|
|
db = _FakeDB()
|
|
|
|
|
server._sessions["sid"] = _session(pending_title="queued title")
|
|
|
|
|
monkeypatch.setattr(server, "_get_db", lambda: db)
|
|
|
|
|
try:
|
|
|
|
|
resp = server.handle_request(
|
|
|
|
|
{"id": "1", "method": "session.title", "params": {"session_id": "sid"}}
|
|
|
|
|
)
|
|
|
|
|
assert resp["result"]["title"] == "queued title"
|
|
|
|
|
assert server._sessions["sid"]["pending_title"] is None
|
|
|
|
|
finally:
|
|
|
|
|
server._sessions.pop("sid", None)
|
|
|
|
|
|
|
|
|
|
|
2026-04-27 11:05:10 -05:00
|
|
|
def test_session_title_rejects_empty_title_with_specific_error_code(monkeypatch):
|
|
|
|
|
class _FakeDB:
|
|
|
|
|
def get_session_title(self, _key):
|
|
|
|
|
return ""
|
|
|
|
|
|
|
|
|
|
server._sessions["sid"] = _session()
|
|
|
|
|
monkeypatch.setattr(server, "_get_db", lambda: _FakeDB())
|
|
|
|
|
try:
|
|
|
|
|
resp = server.handle_request(
|
|
|
|
|
{
|
|
|
|
|
"id": "1",
|
|
|
|
|
"method": "session.title",
|
|
|
|
|
"params": {"session_id": "sid", "title": " "},
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
assert "error" in resp
|
|
|
|
|
assert resp["error"]["code"] == 4021
|
|
|
|
|
finally:
|
|
|
|
|
server._sessions.pop("sid", None)
|
|
|
|
|
|
|
|
|
|
|
2026-04-27 11:27:51 -05:00
|
|
|
def test_session_title_set_maps_valueerror_to_user_error(monkeypatch):
|
|
|
|
|
class _FakeDB:
|
|
|
|
|
def get_session_title(self, _key):
|
|
|
|
|
return ""
|
|
|
|
|
|
|
|
|
|
def get_session(self, _key):
|
|
|
|
|
return {"id": _key}
|
|
|
|
|
|
|
|
|
|
def set_session_title(self, _key, _title):
|
|
|
|
|
raise ValueError("Title already in use")
|
|
|
|
|
|
|
|
|
|
server._sessions["sid"] = _session()
|
|
|
|
|
monkeypatch.setattr(server, "_get_db", lambda: _FakeDB())
|
|
|
|
|
try:
|
|
|
|
|
resp = server.handle_request(
|
|
|
|
|
{
|
|
|
|
|
"id": "1",
|
|
|
|
|
"method": "session.title",
|
|
|
|
|
"params": {"session_id": "sid", "title": "dup"},
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
assert "error" in resp
|
|
|
|
|
assert resp["error"]["code"] == 4022
|
|
|
|
|
assert "already in use" in resp["error"]["message"]
|
|
|
|
|
finally:
|
|
|
|
|
server._sessions.pop("sid", None)
|
|
|
|
|
|
|
|
|
|
|
2026-04-27 11:15:37 -05:00
|
|
|
def test_session_title_set_errors_when_row_lookup_fails_after_noop(monkeypatch):
|
|
|
|
|
class _FakeDB:
|
|
|
|
|
def get_session_title(self, _key):
|
|
|
|
|
return ""
|
|
|
|
|
|
|
|
|
|
def get_session(self, _key):
|
|
|
|
|
raise RuntimeError("row lookup failed")
|
|
|
|
|
|
|
|
|
|
def set_session_title(self, _key, _title):
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
server._sessions["sid"] = _session()
|
|
|
|
|
monkeypatch.setattr(server, "_get_db", lambda: _FakeDB())
|
|
|
|
|
try:
|
|
|
|
|
resp = server.handle_request(
|
|
|
|
|
{
|
|
|
|
|
"id": "1",
|
|
|
|
|
"method": "session.title",
|
|
|
|
|
"params": {"session_id": "sid", "title": "fresh"},
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
assert "error" in resp
|
|
|
|
|
assert resp["error"]["code"] == 5007
|
|
|
|
|
assert "row lookup failed" in resp["error"]["message"]
|
|
|
|
|
finally:
|
|
|
|
|
server._sessions.pop("sid", None)
|
|
|
|
|
|
|
|
|
|
|
2026-04-27 11:49:02 -05:00
|
|
|
def test_session_create_drops_pending_title_on_valueerror(monkeypatch):
|
|
|
|
|
unblock_agent = threading.Event()
|
|
|
|
|
|
|
|
|
|
class _FakeWorker:
|
|
|
|
|
def __init__(self, key, model):
|
|
|
|
|
self.key = key
|
|
|
|
|
|
|
|
|
|
def close(self):
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
class _FakeAgent:
|
|
|
|
|
model = "x"
|
|
|
|
|
provider = "openrouter"
|
|
|
|
|
base_url = ""
|
|
|
|
|
api_key = ""
|
|
|
|
|
|
|
|
|
|
class _FakeDB:
|
|
|
|
|
def create_session(self, _key, source="tui", model=None):
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
def set_session_title(self, _key, _title):
|
|
|
|
|
raise ValueError("Title already in use")
|
|
|
|
|
|
|
|
|
|
def _make_agent(_sid, _key):
|
|
|
|
|
unblock_agent.wait(timeout=2.0)
|
|
|
|
|
return _FakeAgent()
|
|
|
|
|
|
|
|
|
|
monkeypatch.setattr(server, "_make_agent", _make_agent)
|
|
|
|
|
monkeypatch.setattr(server, "_SlashWorker", _FakeWorker)
|
|
|
|
|
monkeypatch.setattr(server, "_get_db", lambda: _FakeDB())
|
|
|
|
|
monkeypatch.setattr(server, "_session_info", lambda _a: {"model": "x"})
|
|
|
|
|
monkeypatch.setattr(server, "_probe_credentials", lambda _a: None)
|
|
|
|
|
monkeypatch.setattr(server, "_wire_callbacks", lambda _sid: None)
|
|
|
|
|
monkeypatch.setattr(server, "_emit", lambda *a, **kw: None)
|
|
|
|
|
|
|
|
|
|
import tools.approval as _approval
|
|
|
|
|
|
|
|
|
|
monkeypatch.setattr(_approval, "register_gateway_notify", lambda key, cb: None)
|
|
|
|
|
monkeypatch.setattr(_approval, "load_permanent_allowlist", lambda: None)
|
|
|
|
|
|
2026-04-27 12:47:42 -05:00
|
|
|
resp = server.handle_request(
|
|
|
|
|
{"id": "1", "method": "session.create", "params": {"cols": 80}}
|
|
|
|
|
)
|
2026-04-27 11:49:02 -05:00
|
|
|
sid = resp["result"]["session_id"]
|
|
|
|
|
session = server._sessions[sid]
|
|
|
|
|
session["pending_title"] = "duplicate title"
|
|
|
|
|
unblock_agent.set()
|
|
|
|
|
session["agent_ready"].wait(timeout=2.0)
|
|
|
|
|
|
|
|
|
|
assert session["pending_title"] is None
|
|
|
|
|
server._sessions.pop(sid, None)
|
|
|
|
|
|
|
|
|
|
|
2026-04-11 14:02:36 -05:00
|
|
|
def test_config_set_yolo_toggles_session_scope():
|
|
|
|
|
from tools.approval import clear_session, is_session_yolo_enabled
|
|
|
|
|
|
|
|
|
|
server._sessions["sid"] = _session()
|
|
|
|
|
try:
|
2026-04-22 15:19:50 -05:00
|
|
|
resp_on = server.handle_request(
|
|
|
|
|
{
|
|
|
|
|
"id": "1",
|
|
|
|
|
"method": "config.set",
|
|
|
|
|
"params": {"session_id": "sid", "key": "yolo"},
|
|
|
|
|
}
|
|
|
|
|
)
|
2026-04-11 14:02:36 -05:00
|
|
|
assert resp_on["result"]["value"] == "1"
|
|
|
|
|
assert is_session_yolo_enabled("session-key") is True
|
|
|
|
|
|
2026-04-22 15:19:50 -05:00
|
|
|
resp_off = server.handle_request(
|
|
|
|
|
{
|
|
|
|
|
"id": "2",
|
|
|
|
|
"method": "config.set",
|
|
|
|
|
"params": {"session_id": "sid", "key": "yolo"},
|
|
|
|
|
}
|
|
|
|
|
)
|
2026-04-11 14:02:36 -05:00
|
|
|
assert resp_off["result"]["value"] == "0"
|
|
|
|
|
assert is_session_yolo_enabled("session-key") is False
|
|
|
|
|
finally:
|
|
|
|
|
clear_session("session-key")
|
|
|
|
|
server._sessions.clear()
|
|
|
|
|
|
|
|
|
|
|
2026-04-27 12:20:08 -05:00
|
|
|
def test_config_set_fast_updates_live_agent_and_config(monkeypatch):
|
|
|
|
|
writes = []
|
|
|
|
|
emits = []
|
2026-04-27 12:47:42 -05:00
|
|
|
agent = types.SimpleNamespace(
|
|
|
|
|
model="openai/gpt-5.4",
|
|
|
|
|
request_overrides={"foo": "bar", "speed": "slow"},
|
|
|
|
|
service_tier=None,
|
|
|
|
|
)
|
2026-04-27 12:20:08 -05:00
|
|
|
server._sessions["sid"] = _session(agent=agent)
|
|
|
|
|
|
2026-04-27 12:47:42 -05:00
|
|
|
monkeypatch.setattr(
|
|
|
|
|
server, "_write_config_key", lambda path, value: writes.append((path, value))
|
|
|
|
|
)
|
2026-04-27 12:20:08 -05:00
|
|
|
monkeypatch.setattr(server, "_session_info", lambda _agent: {"model": "x"})
|
|
|
|
|
monkeypatch.setattr(server, "_emit", lambda *args: emits.append(args))
|
2026-04-27 12:47:42 -05:00
|
|
|
monkeypatch.setattr(
|
|
|
|
|
"hermes_cli.models.resolve_fast_mode_overrides",
|
|
|
|
|
lambda _model_id: {"service_tier": "priority"},
|
|
|
|
|
)
|
2026-04-27 12:20:08 -05:00
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
resp = server.handle_request(
|
|
|
|
|
{
|
|
|
|
|
"id": "1",
|
|
|
|
|
"method": "config.set",
|
|
|
|
|
"params": {"session_id": "sid", "key": "fast", "value": "fast"},
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
assert resp["result"]["value"] == "fast"
|
|
|
|
|
assert agent.service_tier == "priority"
|
2026-04-27 12:47:42 -05:00
|
|
|
assert agent.request_overrides == {
|
|
|
|
|
"foo": "bar",
|
|
|
|
|
"service_tier": "priority",
|
|
|
|
|
}
|
2026-04-27 12:20:08 -05:00
|
|
|
assert ("agent.service_tier", "fast") in writes
|
|
|
|
|
assert ("session.info", "sid", {"model": "x"}) in emits
|
2026-04-27 12:47:42 -05:00
|
|
|
|
|
|
|
|
resp_normal = server.handle_request(
|
|
|
|
|
{
|
|
|
|
|
"id": "2",
|
|
|
|
|
"method": "config.set",
|
|
|
|
|
"params": {"session_id": "sid", "key": "fast", "value": "normal"},
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
assert resp_normal["result"]["value"] == "normal"
|
|
|
|
|
assert agent.service_tier is None
|
|
|
|
|
assert agent.request_overrides == {"foo": "bar"}
|
|
|
|
|
assert ("agent.service_tier", "normal") in writes
|
|
|
|
|
finally:
|
|
|
|
|
server._sessions.pop("sid", None)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_config_set_fast_status_is_non_mutating(monkeypatch):
|
|
|
|
|
writes = []
|
|
|
|
|
emits = []
|
|
|
|
|
agent = types.SimpleNamespace(service_tier="priority")
|
|
|
|
|
server._sessions["sid"] = _session(agent=agent)
|
|
|
|
|
|
|
|
|
|
monkeypatch.setattr(
|
|
|
|
|
server, "_write_config_key", lambda path, value: writes.append((path, value))
|
|
|
|
|
)
|
|
|
|
|
monkeypatch.setattr(server, "_emit", lambda *args: emits.append(args))
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
resp = server.handle_request(
|
|
|
|
|
{
|
|
|
|
|
"id": "1",
|
|
|
|
|
"method": "config.set",
|
|
|
|
|
"params": {"session_id": "sid", "key": "fast", "value": "status"},
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
assert resp["result"]["value"] == "fast"
|
|
|
|
|
assert writes == []
|
|
|
|
|
assert emits == []
|
2026-04-27 12:20:08 -05:00
|
|
|
finally:
|
|
|
|
|
server._sessions.pop("sid", None)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_config_busy_get_and_set(monkeypatch):
|
|
|
|
|
writes = []
|
|
|
|
|
|
|
|
|
|
monkeypatch.setattr(
|
|
|
|
|
server,
|
|
|
|
|
"_load_cfg",
|
|
|
|
|
lambda: {"display": {"busy_input_mode": "steer"}},
|
|
|
|
|
)
|
2026-04-27 12:47:42 -05:00
|
|
|
monkeypatch.setattr(
|
|
|
|
|
server, "_write_config_key", lambda path, value: writes.append((path, value))
|
|
|
|
|
)
|
2026-04-27 12:20:08 -05:00
|
|
|
|
|
|
|
|
get_resp = server.handle_request(
|
|
|
|
|
{"id": "1", "method": "config.get", "params": {"key": "busy"}}
|
|
|
|
|
)
|
|
|
|
|
assert get_resp["result"]["value"] == "steer"
|
|
|
|
|
|
|
|
|
|
set_resp = server.handle_request(
|
|
|
|
|
{
|
|
|
|
|
"id": "2",
|
|
|
|
|
"method": "config.set",
|
|
|
|
|
"params": {"key": "busy", "value": "interrupt"},
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
assert set_resp["result"]["value"] == "interrupt"
|
|
|
|
|
assert ("display.busy_input_mode", "interrupt") in writes
|
|
|
|
|
|
|
|
|
|
|
2026-04-22 15:19:50 -05:00
|
|
|
def test_config_get_statusbar_survives_non_dict_display(monkeypatch):
|
|
|
|
|
monkeypatch.setattr(server, "_load_cfg", lambda: {"display": "broken"})
|
|
|
|
|
|
|
|
|
|
resp = server.handle_request(
|
|
|
|
|
{"id": "1", "method": "config.get", "params": {"key": "statusbar"}}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert resp["result"]["value"] == "top"
|
|
|
|
|
|
|
|
|
|
|
2026-04-27 12:30:30 -05:00
|
|
|
def test_config_get_busy_survives_non_dict_display(monkeypatch):
|
|
|
|
|
monkeypatch.setattr(server, "_load_cfg", lambda: {"display": "broken"})
|
|
|
|
|
|
|
|
|
|
resp = server.handle_request(
|
|
|
|
|
{"id": "1", "method": "config.get", "params": {"key": "busy"}}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert resp["result"]["value"] == "interrupt"
|
|
|
|
|
|
|
|
|
|
|
2026-04-22 15:19:50 -05:00
|
|
|
def test_config_set_statusbar_survives_non_dict_display(tmp_path, monkeypatch):
|
|
|
|
|
import yaml
|
|
|
|
|
|
|
|
|
|
cfg_path = tmp_path / "config.yaml"
|
|
|
|
|
cfg_path.write_text(yaml.safe_dump({"display": "broken"}))
|
|
|
|
|
monkeypatch.setattr(server, "_hermes_home", tmp_path)
|
|
|
|
|
|
|
|
|
|
resp = server.handle_request(
|
|
|
|
|
{
|
|
|
|
|
"id": "1",
|
|
|
|
|
"method": "config.set",
|
|
|
|
|
"params": {"key": "statusbar", "value": "bottom"},
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert resp["result"]["value"] == "bottom"
|
|
|
|
|
saved = yaml.safe_load(cfg_path.read_text())
|
|
|
|
|
assert saved["display"]["tui_statusbar"] == "bottom"
|
|
|
|
|
|
|
|
|
|
|
2026-04-24 02:34:32 -05:00
|
|
|
def test_config_set_section_writes_per_section_override(tmp_path, monkeypatch):
|
|
|
|
|
import yaml
|
|
|
|
|
|
|
|
|
|
cfg_path = tmp_path / "config.yaml"
|
|
|
|
|
monkeypatch.setattr(server, "_hermes_home", tmp_path)
|
|
|
|
|
|
|
|
|
|
resp = server.handle_request(
|
|
|
|
|
{
|
|
|
|
|
"id": "1",
|
|
|
|
|
"method": "config.set",
|
|
|
|
|
"params": {"key": "details_mode.activity", "value": "hidden"},
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert resp["result"] == {"key": "details_mode.activity", "value": "hidden"}
|
|
|
|
|
saved = yaml.safe_load(cfg_path.read_text())
|
|
|
|
|
assert saved["display"]["sections"] == {"activity": "hidden"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_config_set_section_clears_override_on_empty_value(tmp_path, monkeypatch):
|
|
|
|
|
import yaml
|
|
|
|
|
|
|
|
|
|
cfg_path = tmp_path / "config.yaml"
|
|
|
|
|
cfg_path.write_text(
|
|
|
|
|
yaml.safe_dump(
|
|
|
|
|
{"display": {"sections": {"activity": "hidden", "tools": "expanded"}}}
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
monkeypatch.setattr(server, "_hermes_home", tmp_path)
|
|
|
|
|
|
|
|
|
|
resp = server.handle_request(
|
|
|
|
|
{
|
|
|
|
|
"id": "1",
|
|
|
|
|
"method": "config.set",
|
|
|
|
|
"params": {"key": "details_mode.activity", "value": ""},
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert resp["result"] == {"key": "details_mode.activity", "value": ""}
|
|
|
|
|
saved = yaml.safe_load(cfg_path.read_text())
|
|
|
|
|
assert saved["display"]["sections"] == {"tools": "expanded"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_config_set_section_rejects_unknown_section_or_mode(tmp_path, monkeypatch):
|
|
|
|
|
monkeypatch.setattr(server, "_hermes_home", tmp_path)
|
|
|
|
|
|
|
|
|
|
bad_section = server.handle_request(
|
|
|
|
|
{
|
|
|
|
|
"id": "1",
|
|
|
|
|
"method": "config.set",
|
|
|
|
|
"params": {"key": "details_mode.bogus", "value": "hidden"},
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
assert bad_section["error"]["code"] == 4002
|
|
|
|
|
|
|
|
|
|
bad_mode = server.handle_request(
|
|
|
|
|
{
|
|
|
|
|
"id": "2",
|
|
|
|
|
"method": "config.set",
|
|
|
|
|
"params": {"key": "details_mode.tools", "value": "maximised"},
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
assert bad_mode["error"]["code"] == 4002
|
|
|
|
|
|
|
|
|
|
|
2026-04-17 10:37:48 -05:00
|
|
|
def test_enable_gateway_prompts_sets_gateway_env(monkeypatch):
|
|
|
|
|
monkeypatch.delenv("HERMES_EXEC_ASK", raising=False)
|
|
|
|
|
monkeypatch.delenv("HERMES_GATEWAY_SESSION", raising=False)
|
|
|
|
|
monkeypatch.delenv("HERMES_INTERACTIVE", raising=False)
|
|
|
|
|
|
|
|
|
|
server._enable_gateway_prompts()
|
|
|
|
|
|
|
|
|
|
assert server.os.environ["HERMES_GATEWAY_SESSION"] == "1"
|
|
|
|
|
assert server.os.environ["HERMES_EXEC_ASK"] == "1"
|
|
|
|
|
assert server.os.environ["HERMES_INTERACTIVE"] == "1"
|
|
|
|
|
|
|
|
|
|
|
2026-04-17 10:58:01 -05:00
|
|
|
def test_setup_status_reports_provider_config(monkeypatch):
|
|
|
|
|
monkeypatch.setattr("hermes_cli.main._has_any_provider_configured", lambda: False)
|
|
|
|
|
|
|
|
|
|
resp = server.handle_request({"id": "1", "method": "setup.status", "params": {}})
|
|
|
|
|
|
|
|
|
|
assert resp["result"]["provider_configured"] is False
|
|
|
|
|
|
|
|
|
|
|
2026-04-25 13:58:59 -05:00
|
|
|
def test_complete_slash_includes_provider_alias():
|
|
|
|
|
resp = server.handle_request(
|
|
|
|
|
{"id": "1", "method": "complete.slash", "params": {"text": "/pro"}}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert any(item["text"] == "provider" for item in resp["result"]["items"])
|
|
|
|
|
|
|
|
|
|
|
2026-04-26 02:15:10 -05:00
|
|
|
def test_complete_slash_includes_tui_details_command():
|
|
|
|
|
resp = server.handle_request(
|
|
|
|
|
{"id": "1", "method": "complete.slash", "params": {"text": "/det"}}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert any(item["text"] == "/details" for item in resp["result"]["items"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_complete_slash_details_args():
|
2026-04-26 04:28:55 -05:00
|
|
|
resp_root = server.handle_request(
|
|
|
|
|
{"id": "0", "method": "complete.slash", "params": {"text": "/details"}}
|
|
|
|
|
)
|
2026-04-26 02:15:10 -05:00
|
|
|
resp_section = server.handle_request(
|
|
|
|
|
{"id": "1", "method": "complete.slash", "params": {"text": "/details t"}}
|
|
|
|
|
)
|
|
|
|
|
resp_mode = server.handle_request(
|
|
|
|
|
{
|
|
|
|
|
"id": "2",
|
|
|
|
|
"method": "complete.slash",
|
|
|
|
|
"params": {"text": "/details thinking e"},
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
2026-04-26 04:28:55 -05:00
|
|
|
assert resp_root["result"]["replace_from"] == len("/details")
|
2026-04-26 13:43:08 -05:00
|
|
|
assert any(item["text"] == " thinking" for item in resp_root["result"]["items"])
|
2026-04-26 02:15:10 -05:00
|
|
|
assert any(item["text"] == "thinking" for item in resp_section["result"]["items"])
|
|
|
|
|
assert any(item["text"] == "expanded" for item in resp_mode["result"]["items"])
|
|
|
|
|
|
|
|
|
|
|
2026-04-11 14:02:36 -05:00
|
|
|
def test_config_set_reasoning_updates_live_session_and_agent(tmp_path, monkeypatch):
|
|
|
|
|
monkeypatch.setattr(server, "_hermes_home", tmp_path)
|
|
|
|
|
agent = types.SimpleNamespace(reasoning_config=None)
|
|
|
|
|
server._sessions["sid"] = _session(agent=agent)
|
|
|
|
|
|
|
|
|
|
resp_effort = server.handle_request(
|
2026-04-22 15:19:50 -05:00
|
|
|
{
|
|
|
|
|
"id": "1",
|
|
|
|
|
"method": "config.set",
|
|
|
|
|
"params": {"session_id": "sid", "key": "reasoning", "value": "low"},
|
|
|
|
|
}
|
2026-04-11 14:02:36 -05:00
|
|
|
)
|
|
|
|
|
assert resp_effort["result"]["value"] == "low"
|
|
|
|
|
assert agent.reasoning_config == {"enabled": True, "effort": "low"}
|
|
|
|
|
|
|
|
|
|
resp_show = server.handle_request(
|
2026-04-22 15:19:50 -05:00
|
|
|
{
|
|
|
|
|
"id": "2",
|
|
|
|
|
"method": "config.set",
|
|
|
|
|
"params": {"session_id": "sid", "key": "reasoning", "value": "show"},
|
|
|
|
|
}
|
2026-04-11 14:02:36 -05:00
|
|
|
)
|
|
|
|
|
assert resp_show["result"]["value"] == "show"
|
|
|
|
|
assert server._sessions["sid"]["show_reasoning"] is True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_config_set_verbose_updates_session_mode_and_agent(tmp_path, monkeypatch):
|
|
|
|
|
monkeypatch.setattr(server, "_hermes_home", tmp_path)
|
|
|
|
|
agent = types.SimpleNamespace(verbose_logging=False)
|
|
|
|
|
server._sessions["sid"] = _session(agent=agent)
|
|
|
|
|
|
|
|
|
|
resp = server.handle_request(
|
2026-04-22 15:19:50 -05:00
|
|
|
{
|
|
|
|
|
"id": "1",
|
|
|
|
|
"method": "config.set",
|
|
|
|
|
"params": {"session_id": "sid", "key": "verbose", "value": "cycle"},
|
|
|
|
|
}
|
2026-04-11 14:02:36 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert resp["result"]["value"] == "verbose"
|
|
|
|
|
assert server._sessions["sid"]["tool_progress_mode"] == "verbose"
|
|
|
|
|
assert agent.verbose_logging is True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_config_set_model_uses_live_switch_path(monkeypatch):
|
|
|
|
|
server._sessions["sid"] = _session()
|
|
|
|
|
seen = {}
|
|
|
|
|
|
|
|
|
|
def _fake_apply(sid, session, raw):
|
|
|
|
|
seen["args"] = (sid, session["session_key"], raw)
|
2026-04-13 14:57:42 -05:00
|
|
|
return {"value": "new/model", "warning": "catalog unreachable"}
|
2026-04-11 14:02:36 -05:00
|
|
|
|
|
|
|
|
monkeypatch.setattr(server, "_apply_model_switch", _fake_apply)
|
|
|
|
|
resp = server.handle_request(
|
2026-04-22 15:19:50 -05:00
|
|
|
{
|
|
|
|
|
"id": "1",
|
|
|
|
|
"method": "config.set",
|
|
|
|
|
"params": {"session_id": "sid", "key": "model", "value": "new/model"},
|
|
|
|
|
}
|
2026-04-11 14:02:36 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert resp["result"]["value"] == "new/model"
|
2026-04-13 14:57:42 -05:00
|
|
|
assert resp["result"]["warning"] == "catalog unreachable"
|
2026-04-11 14:02:36 -05:00
|
|
|
assert seen["args"] == ("sid", "session-key", "new/model")
|
|
|
|
|
|
|
|
|
|
|
2026-04-13 18:29:24 -05:00
|
|
|
def test_config_set_model_global_persists(monkeypatch):
|
|
|
|
|
class _Agent:
|
|
|
|
|
provider = "openrouter"
|
|
|
|
|
model = "old/model"
|
|
|
|
|
base_url = ""
|
|
|
|
|
api_key = "sk-old"
|
|
|
|
|
|
|
|
|
|
def switch_model(self, **kwargs):
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
result = types.SimpleNamespace(
|
|
|
|
|
success=True,
|
|
|
|
|
new_model="anthropic/claude-sonnet-4.6",
|
|
|
|
|
target_provider="anthropic",
|
|
|
|
|
api_key="sk-new",
|
|
|
|
|
base_url="https://api.anthropic.com",
|
|
|
|
|
api_mode="anthropic_messages",
|
|
|
|
|
warning_message="",
|
|
|
|
|
)
|
|
|
|
|
seen = {}
|
|
|
|
|
saved = {}
|
|
|
|
|
|
|
|
|
|
def _switch_model(**kwargs):
|
|
|
|
|
seen.update(kwargs)
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
server._sessions["sid"] = _session(agent=_Agent())
|
|
|
|
|
monkeypatch.setattr("hermes_cli.model_switch.switch_model", _switch_model)
|
|
|
|
|
monkeypatch.setattr(server, "_restart_slash_worker", lambda session: None)
|
|
|
|
|
monkeypatch.setattr(server, "_emit", lambda *args, **kwargs: None)
|
|
|
|
|
monkeypatch.setattr("hermes_cli.config.save_config", lambda cfg: saved.update(cfg))
|
|
|
|
|
|
|
|
|
|
resp = server.handle_request(
|
2026-04-22 15:19:50 -05:00
|
|
|
{
|
|
|
|
|
"id": "1",
|
|
|
|
|
"method": "config.set",
|
|
|
|
|
"params": {
|
|
|
|
|
"session_id": "sid",
|
|
|
|
|
"key": "model",
|
|
|
|
|
"value": "anthropic/claude-sonnet-4.6 --global",
|
|
|
|
|
},
|
|
|
|
|
}
|
2026-04-13 18:29:24 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert resp["result"]["value"] == "anthropic/claude-sonnet-4.6"
|
|
|
|
|
assert seen["is_global"] is True
|
|
|
|
|
assert saved["model"]["default"] == "anthropic/claude-sonnet-4.6"
|
|
|
|
|
assert saved["model"]["provider"] == "anthropic"
|
|
|
|
|
assert saved["model"]["base_url"] == "https://api.anthropic.com"
|
|
|
|
|
|
|
|
|
|
|
2026-04-21 12:23:17 -05:00
|
|
|
def test_config_set_model_syncs_inference_provider_env(monkeypatch):
|
|
|
|
|
"""After an explicit provider switch, HERMES_INFERENCE_PROVIDER must
|
|
|
|
|
reflect the user's choice so ambient re-resolution (credential pool
|
|
|
|
|
refresh, aux clients) picks up the new provider instead of the original
|
|
|
|
|
one persisted in config or shell env.
|
|
|
|
|
|
|
|
|
|
Regression: a TUI user switched openrouter → anthropic and the TUI kept
|
|
|
|
|
trying openrouter because the env-var-backed resolvers still saw the old
|
|
|
|
|
provider.
|
|
|
|
|
"""
|
2026-04-22 15:19:50 -05:00
|
|
|
|
2026-04-21 12:23:17 -05:00
|
|
|
class _Agent:
|
|
|
|
|
provider = "openrouter"
|
|
|
|
|
model = "old/model"
|
|
|
|
|
base_url = ""
|
|
|
|
|
api_key = "sk-or"
|
|
|
|
|
|
|
|
|
|
def switch_model(self, **_kwargs):
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
result = types.SimpleNamespace(
|
|
|
|
|
success=True,
|
|
|
|
|
new_model="claude-sonnet-4.6",
|
|
|
|
|
target_provider="anthropic",
|
|
|
|
|
api_key="sk-ant",
|
|
|
|
|
base_url="https://api.anthropic.com",
|
|
|
|
|
api_mode="anthropic_messages",
|
|
|
|
|
warning_message="",
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
server._sessions["sid"] = _session(agent=_Agent())
|
|
|
|
|
monkeypatch.setenv("HERMES_INFERENCE_PROVIDER", "openrouter")
|
2026-04-22 15:19:50 -05:00
|
|
|
monkeypatch.setattr(
|
|
|
|
|
"hermes_cli.model_switch.switch_model", lambda **_kwargs: result
|
|
|
|
|
)
|
2026-04-21 12:23:17 -05:00
|
|
|
monkeypatch.setattr(server, "_restart_slash_worker", lambda session: None)
|
|
|
|
|
monkeypatch.setattr(server, "_emit", lambda *args, **kwargs: None)
|
|
|
|
|
|
|
|
|
|
server.handle_request(
|
2026-04-22 15:19:50 -05:00
|
|
|
{
|
|
|
|
|
"id": "1",
|
|
|
|
|
"method": "config.set",
|
|
|
|
|
"params": {
|
|
|
|
|
"session_id": "sid",
|
|
|
|
|
"key": "model",
|
|
|
|
|
"value": "claude-sonnet-4.6 --provider anthropic",
|
|
|
|
|
},
|
|
|
|
|
}
|
2026-04-21 12:23:17 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert os.environ["HERMES_INFERENCE_PROVIDER"] == "anthropic"
|
|
|
|
|
|
|
|
|
|
|
2026-04-25 13:29:15 -05:00
|
|
|
def test_config_set_model_syncs_tui_provider_env(monkeypatch):
|
|
|
|
|
class Agent:
|
|
|
|
|
model = "gpt-5.3-codex"
|
|
|
|
|
provider = "openai-codex"
|
|
|
|
|
base_url = ""
|
|
|
|
|
api_key = ""
|
|
|
|
|
|
|
|
|
|
def switch_model(self, **kwargs):
|
|
|
|
|
self.model = kwargs["new_model"]
|
|
|
|
|
self.provider = kwargs["new_provider"]
|
|
|
|
|
|
|
|
|
|
agent = Agent()
|
|
|
|
|
server._sessions["sid"] = _session(agent=agent)
|
|
|
|
|
monkeypatch.setenv("HERMES_TUI_PROVIDER", "openai-codex")
|
|
|
|
|
monkeypatch.setattr(server, "_restart_slash_worker", lambda session: None)
|
|
|
|
|
monkeypatch.setattr(server, "_emit", lambda *args, **kwargs: None)
|
|
|
|
|
|
|
|
|
|
def fake_switch_model(**kwargs):
|
|
|
|
|
return types.SimpleNamespace(
|
|
|
|
|
success=True,
|
|
|
|
|
new_model="anthropic/claude-sonnet-4.6",
|
|
|
|
|
target_provider="anthropic",
|
|
|
|
|
api_key="key",
|
|
|
|
|
base_url="https://api.anthropic.com",
|
|
|
|
|
api_mode="anthropic_messages",
|
|
|
|
|
warning_message="",
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
monkeypatch.setattr("hermes_cli.model_switch.switch_model", fake_switch_model)
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
resp = server.handle_request(
|
|
|
|
|
{
|
|
|
|
|
"id": "1",
|
|
|
|
|
"method": "config.set",
|
|
|
|
|
"params": {
|
|
|
|
|
"session_id": "sid",
|
|
|
|
|
"key": "model",
|
|
|
|
|
"value": "anthropic/claude-sonnet-4.6 --provider anthropic",
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert resp["result"]["value"] == "anthropic/claude-sonnet-4.6"
|
|
|
|
|
assert os.environ["HERMES_TUI_PROVIDER"] == "anthropic"
|
2026-04-25 14:17:57 -05:00
|
|
|
assert os.environ["HERMES_MODEL"] == "anthropic/claude-sonnet-4.6"
|
|
|
|
|
assert os.environ["HERMES_INFERENCE_MODEL"] == "anthropic/claude-sonnet-4.6"
|
2026-04-25 13:29:15 -05:00
|
|
|
finally:
|
|
|
|
|
server._sessions.clear()
|
|
|
|
|
|
|
|
|
|
|
2026-04-13 18:29:24 -05:00
|
|
|
def test_config_set_personality_rejects_unknown_name(monkeypatch):
|
2026-04-22 15:19:50 -05:00
|
|
|
monkeypatch.setattr(
|
|
|
|
|
server,
|
|
|
|
|
"_available_personalities",
|
|
|
|
|
lambda cfg=None: {"helpful": "You are helpful."},
|
|
|
|
|
)
|
2026-04-13 18:29:24 -05:00
|
|
|
resp = server.handle_request(
|
2026-04-22 15:19:50 -05:00
|
|
|
{
|
|
|
|
|
"id": "1",
|
|
|
|
|
"method": "config.set",
|
|
|
|
|
"params": {"key": "personality", "value": "bogus"},
|
|
|
|
|
}
|
2026-04-13 18:29:24 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert "error" in resp
|
|
|
|
|
assert "Unknown personality" in resp["error"]["message"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_config_set_personality_resets_history_and_returns_info(monkeypatch):
|
2026-04-22 15:19:50 -05:00
|
|
|
session = _session(
|
|
|
|
|
agent=types.SimpleNamespace(),
|
|
|
|
|
history=[{"role": "user", "text": "hi"}],
|
|
|
|
|
history_version=4,
|
|
|
|
|
)
|
2026-04-13 18:29:24 -05:00
|
|
|
new_agent = types.SimpleNamespace(model="x")
|
|
|
|
|
emits = []
|
|
|
|
|
|
|
|
|
|
server._sessions["sid"] = session
|
2026-04-22 15:19:50 -05:00
|
|
|
monkeypatch.setattr(
|
|
|
|
|
server,
|
|
|
|
|
"_available_personalities",
|
|
|
|
|
lambda cfg=None: {"helpful": "You are helpful."},
|
|
|
|
|
)
|
|
|
|
|
monkeypatch.setattr(
|
|
|
|
|
server, "_make_agent", lambda sid, key, session_id=None: new_agent
|
|
|
|
|
)
|
|
|
|
|
monkeypatch.setattr(
|
|
|
|
|
server, "_session_info", lambda agent: {"model": getattr(agent, "model", "?")}
|
|
|
|
|
)
|
2026-04-13 18:29:24 -05:00
|
|
|
monkeypatch.setattr(server, "_restart_slash_worker", lambda session: None)
|
|
|
|
|
monkeypatch.setattr(server, "_emit", lambda *args: emits.append(args))
|
2026-04-16 19:07:49 -05:00
|
|
|
monkeypatch.setattr(server, "_write_config_key", lambda path, value: None)
|
2026-04-13 18:29:24 -05:00
|
|
|
|
|
|
|
|
resp = server.handle_request(
|
2026-04-22 15:19:50 -05:00
|
|
|
{
|
|
|
|
|
"id": "1",
|
|
|
|
|
"method": "config.set",
|
|
|
|
|
"params": {"session_id": "sid", "key": "personality", "value": "helpful"},
|
|
|
|
|
}
|
2026-04-13 18:29:24 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert resp["result"]["history_reset"] is True
|
|
|
|
|
assert resp["result"]["info"] == {"model": "x"}
|
|
|
|
|
assert session["history"] == []
|
|
|
|
|
assert session["history_version"] == 5
|
|
|
|
|
assert ("session.info", "sid", {"model": "x"}) in emits
|
|
|
|
|
|
|
|
|
|
|
2026-04-11 14:02:36 -05:00
|
|
|
def test_session_compress_uses_compress_helper(monkeypatch):
|
|
|
|
|
agent = types.SimpleNamespace()
|
|
|
|
|
server._sessions["sid"] = _session(agent=agent)
|
|
|
|
|
|
2026-04-22 15:19:50 -05:00
|
|
|
monkeypatch.setattr(
|
|
|
|
|
server,
|
|
|
|
|
"_compress_session_history",
|
|
|
|
|
lambda session, focus_topic=None: (2, {"total": 42}),
|
|
|
|
|
)
|
2026-04-11 14:02:36 -05:00
|
|
|
monkeypatch.setattr(server, "_session_info", lambda _agent: {"model": "x"})
|
|
|
|
|
|
|
|
|
|
with patch("tui_gateway.server._emit") as emit:
|
2026-04-22 15:19:50 -05:00
|
|
|
resp = server.handle_request(
|
|
|
|
|
{"id": "1", "method": "session.compress", "params": {"session_id": "sid"}}
|
|
|
|
|
)
|
2026-04-11 14:02:36 -05:00
|
|
|
|
|
|
|
|
assert resp["result"]["removed"] == 2
|
|
|
|
|
assert resp["result"]["usage"]["total"] == 42
|
|
|
|
|
emit.assert_called_once_with("session.info", "sid", {"model": "x"})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_prompt_submit_sets_approval_session_key(monkeypatch):
|
|
|
|
|
from tools.approval import get_current_session_key
|
|
|
|
|
|
|
|
|
|
captured = {}
|
|
|
|
|
|
|
|
|
|
class _Agent:
|
2026-04-22 15:19:50 -05:00
|
|
|
def run_conversation(
|
|
|
|
|
self, prompt, conversation_history=None, stream_callback=None
|
|
|
|
|
):
|
2026-04-11 14:02:36 -05:00
|
|
|
captured["session_key"] = get_current_session_key(default="")
|
2026-04-22 15:19:50 -05:00
|
|
|
return {
|
|
|
|
|
"final_response": "ok",
|
|
|
|
|
"messages": [{"role": "assistant", "content": "ok"}],
|
|
|
|
|
}
|
2026-04-11 14:02:36 -05:00
|
|
|
|
|
|
|
|
class _ImmediateThread:
|
|
|
|
|
def __init__(self, target=None, daemon=None):
|
|
|
|
|
self._target = target
|
|
|
|
|
|
|
|
|
|
def start(self):
|
|
|
|
|
self._target()
|
|
|
|
|
|
|
|
|
|
server._sessions["sid"] = _session(agent=_Agent())
|
|
|
|
|
monkeypatch.setattr(server.threading, "Thread", _ImmediateThread)
|
|
|
|
|
monkeypatch.setattr(server, "_emit", lambda *args, **kwargs: None)
|
|
|
|
|
monkeypatch.setattr(server, "make_stream_renderer", lambda cols: None)
|
|
|
|
|
monkeypatch.setattr(server, "render_message", lambda raw, cols: None)
|
|
|
|
|
|
2026-04-22 15:19:50 -05:00
|
|
|
resp = server.handle_request(
|
|
|
|
|
{
|
|
|
|
|
"id": "1",
|
|
|
|
|
"method": "prompt.submit",
|
|
|
|
|
"params": {"session_id": "sid", "text": "ping"},
|
|
|
|
|
}
|
|
|
|
|
)
|
2026-04-11 14:02:36 -05:00
|
|
|
|
|
|
|
|
assert resp["result"]["status"] == "streaming"
|
|
|
|
|
assert captured["session_key"] == "session-key"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_prompt_submit_expands_context_refs(monkeypatch):
|
|
|
|
|
captured = {}
|
|
|
|
|
|
|
|
|
|
class _Agent:
|
|
|
|
|
model = "test/model"
|
|
|
|
|
base_url = ""
|
|
|
|
|
api_key = ""
|
|
|
|
|
|
2026-04-22 15:19:50 -05:00
|
|
|
def run_conversation(
|
|
|
|
|
self, prompt, conversation_history=None, stream_callback=None
|
|
|
|
|
):
|
2026-04-11 14:02:36 -05:00
|
|
|
captured["prompt"] = prompt
|
2026-04-22 15:19:50 -05:00
|
|
|
return {
|
|
|
|
|
"final_response": "ok",
|
|
|
|
|
"messages": [{"role": "assistant", "content": "ok"}],
|
|
|
|
|
}
|
2026-04-11 14:02:36 -05:00
|
|
|
|
|
|
|
|
class _ImmediateThread:
|
|
|
|
|
def __init__(self, target=None, daemon=None):
|
|
|
|
|
self._target = target
|
|
|
|
|
|
|
|
|
|
def start(self):
|
|
|
|
|
self._target()
|
|
|
|
|
|
|
|
|
|
fake_ctx = types.ModuleType("agent.context_references")
|
2026-04-22 15:19:50 -05:00
|
|
|
fake_ctx.preprocess_context_references = (
|
|
|
|
|
lambda message, **kwargs: types.SimpleNamespace(
|
|
|
|
|
blocked=False,
|
|
|
|
|
message="expanded prompt",
|
|
|
|
|
warnings=[],
|
|
|
|
|
references=[],
|
|
|
|
|
injected_tokens=0,
|
|
|
|
|
)
|
2026-04-11 14:02:36 -05:00
|
|
|
)
|
|
|
|
|
fake_meta = types.ModuleType("agent.model_metadata")
|
|
|
|
|
fake_meta.get_model_context_length = lambda *args, **kwargs: 100000
|
|
|
|
|
|
|
|
|
|
server._sessions["sid"] = _session(agent=_Agent())
|
|
|
|
|
monkeypatch.setattr(server.threading, "Thread", _ImmediateThread)
|
|
|
|
|
monkeypatch.setattr(server, "_emit", lambda *args, **kwargs: None)
|
|
|
|
|
monkeypatch.setattr(server, "make_stream_renderer", lambda cols: None)
|
|
|
|
|
monkeypatch.setattr(server, "render_message", lambda raw, cols: None)
|
|
|
|
|
monkeypatch.setitem(sys.modules, "agent.context_references", fake_ctx)
|
|
|
|
|
monkeypatch.setitem(sys.modules, "agent.model_metadata", fake_meta)
|
|
|
|
|
|
2026-04-22 15:19:50 -05:00
|
|
|
server.handle_request(
|
|
|
|
|
{
|
|
|
|
|
"id": "1",
|
|
|
|
|
"method": "prompt.submit",
|
|
|
|
|
"params": {"session_id": "sid", "text": "@diff"},
|
|
|
|
|
}
|
|
|
|
|
)
|
2026-04-11 14:02:36 -05:00
|
|
|
|
|
|
|
|
assert captured["prompt"] == "expanded prompt"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_image_attach_appends_local_image(monkeypatch):
|
|
|
|
|
fake_cli = types.ModuleType("cli")
|
|
|
|
|
fake_cli._IMAGE_EXTENSIONS = {".png"}
|
2026-04-21 14:27:28 +05:30
|
|
|
fake_cli._detect_file_drop = lambda raw: {
|
|
|
|
|
"path": Path("/tmp/cat.png"),
|
|
|
|
|
"is_image": True,
|
|
|
|
|
"remainder": "",
|
|
|
|
|
}
|
2026-04-11 14:02:36 -05:00
|
|
|
fake_cli._split_path_input = lambda raw: (raw, "")
|
|
|
|
|
fake_cli._resolve_attachment_path = lambda raw: Path("/tmp/cat.png")
|
|
|
|
|
|
|
|
|
|
server._sessions["sid"] = _session()
|
|
|
|
|
monkeypatch.setitem(sys.modules, "cli", fake_cli)
|
|
|
|
|
|
2026-04-22 15:19:50 -05:00
|
|
|
resp = server.handle_request(
|
|
|
|
|
{
|
|
|
|
|
"id": "1",
|
|
|
|
|
"method": "image.attach",
|
|
|
|
|
"params": {"session_id": "sid", "path": "/tmp/cat.png"},
|
|
|
|
|
}
|
|
|
|
|
)
|
2026-04-11 14:02:36 -05:00
|
|
|
|
|
|
|
|
assert resp["result"]["attached"] is True
|
|
|
|
|
assert resp["result"]["name"] == "cat.png"
|
|
|
|
|
assert len(server._sessions["sid"]["attached_images"]) == 1
|
|
|
|
|
|
|
|
|
|
|
2026-04-21 14:27:28 +05:30
|
|
|
def test_image_attach_accepts_unquoted_screenshot_path_with_spaces(monkeypatch):
|
|
|
|
|
screenshot = Path("/tmp/Screenshot 2026-04-21 at 1.04.43 PM.png")
|
|
|
|
|
fake_cli = types.ModuleType("cli")
|
|
|
|
|
fake_cli._IMAGE_EXTENSIONS = {".png"}
|
|
|
|
|
fake_cli._detect_file_drop = lambda raw: {
|
|
|
|
|
"path": screenshot,
|
|
|
|
|
"is_image": True,
|
|
|
|
|
"remainder": "",
|
|
|
|
|
}
|
2026-04-22 15:19:50 -05:00
|
|
|
fake_cli._split_path_input = lambda raw: (
|
|
|
|
|
"/tmp/Screenshot",
|
|
|
|
|
"2026-04-21 at 1.04.43 PM.png",
|
|
|
|
|
)
|
2026-04-21 14:27:28 +05:30
|
|
|
fake_cli._resolve_attachment_path = lambda raw: None
|
|
|
|
|
|
|
|
|
|
server._sessions["sid"] = _session()
|
|
|
|
|
monkeypatch.setitem(sys.modules, "cli", fake_cli)
|
|
|
|
|
|
|
|
|
|
resp = server.handle_request(
|
2026-04-22 15:19:50 -05:00
|
|
|
{
|
|
|
|
|
"id": "1",
|
|
|
|
|
"method": "image.attach",
|
|
|
|
|
"params": {"session_id": "sid", "path": str(screenshot)},
|
|
|
|
|
}
|
2026-04-21 14:27:28 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert resp["result"]["attached"] is True
|
|
|
|
|
assert resp["result"]["path"] == str(screenshot)
|
|
|
|
|
assert resp["result"]["remainder"] == ""
|
|
|
|
|
assert len(server._sessions["sid"]["attached_images"]) == 1
|
|
|
|
|
|
|
|
|
|
|
2026-04-18 09:16:39 -05:00
|
|
|
def test_commands_catalog_surfaces_quick_commands(monkeypatch):
|
2026-04-22 15:19:50 -05:00
|
|
|
monkeypatch.setattr(
|
|
|
|
|
server,
|
|
|
|
|
"_load_cfg",
|
|
|
|
|
lambda: {
|
|
|
|
|
"quick_commands": {
|
|
|
|
|
"build": {"type": "exec", "command": "npm run build"},
|
|
|
|
|
"git": {"type": "alias", "target": "/shell git"},
|
|
|
|
|
"notes": {
|
|
|
|
|
"type": "exec",
|
|
|
|
|
"command": "cat NOTES.md",
|
|
|
|
|
"description": "Open design notes",
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
)
|
2026-04-18 09:16:39 -05:00
|
|
|
|
2026-04-22 15:19:50 -05:00
|
|
|
resp = server.handle_request(
|
|
|
|
|
{"id": "1", "method": "commands.catalog", "params": {}}
|
|
|
|
|
)
|
2026-04-18 09:16:39 -05:00
|
|
|
|
|
|
|
|
pairs = dict(resp["result"]["pairs"])
|
|
|
|
|
assert "npm run build" in pairs["/build"]
|
|
|
|
|
assert pairs["/git"].startswith("alias →")
|
|
|
|
|
assert pairs["/notes"] == "Open design notes"
|
|
|
|
|
|
2026-04-22 15:19:50 -05:00
|
|
|
user_cat = next(
|
|
|
|
|
c for c in resp["result"]["categories"] if c["name"] == "User commands"
|
|
|
|
|
)
|
2026-04-18 09:16:39 -05:00
|
|
|
user_pairs = dict(user_cat["pairs"])
|
|
|
|
|
assert set(user_pairs) == {"/build", "/git", "/notes"}
|
|
|
|
|
|
|
|
|
|
assert resp["result"]["canon"]["/build"] == "/build"
|
|
|
|
|
assert resp["result"]["canon"]["/notes"] == "/notes"
|
|
|
|
|
|
|
|
|
|
|
2026-04-13 18:29:24 -05:00
|
|
|
def test_command_dispatch_exec_nonzero_surfaces_error(monkeypatch):
|
2026-04-22 15:19:50 -05:00
|
|
|
monkeypatch.setattr(
|
|
|
|
|
server,
|
|
|
|
|
"_load_cfg",
|
|
|
|
|
lambda: {"quick_commands": {"boom": {"type": "exec", "command": "boom"}}},
|
|
|
|
|
)
|
2026-04-13 18:29:24 -05:00
|
|
|
monkeypatch.setattr(
|
|
|
|
|
server.subprocess,
|
|
|
|
|
"run",
|
2026-04-22 15:19:50 -05:00
|
|
|
lambda *args, **kwargs: types.SimpleNamespace(
|
|
|
|
|
returncode=1, stdout="", stderr="failed"
|
|
|
|
|
),
|
2026-04-13 18:29:24 -05:00
|
|
|
)
|
|
|
|
|
|
2026-04-22 15:19:50 -05:00
|
|
|
resp = server.handle_request(
|
|
|
|
|
{"id": "1", "method": "command.dispatch", "params": {"name": "boom"}}
|
|
|
|
|
)
|
2026-04-13 18:29:24 -05:00
|
|
|
|
|
|
|
|
assert "error" in resp
|
|
|
|
|
assert "failed" in resp["error"]["message"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_plugins_list_surfaces_loader_error(monkeypatch):
|
|
|
|
|
with patch("hermes_cli.plugins.get_plugin_manager", side_effect=Exception("boom")):
|
2026-04-22 15:19:50 -05:00
|
|
|
resp = server.handle_request(
|
|
|
|
|
{"id": "1", "method": "plugins.list", "params": {}}
|
|
|
|
|
)
|
2026-04-13 18:29:24 -05:00
|
|
|
|
|
|
|
|
assert "error" in resp
|
|
|
|
|
assert "boom" in resp["error"]["message"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_complete_slash_surfaces_completer_error(monkeypatch):
|
2026-04-22 15:19:50 -05:00
|
|
|
with patch(
|
|
|
|
|
"hermes_cli.commands.SlashCommandCompleter",
|
|
|
|
|
side_effect=Exception("no completer"),
|
|
|
|
|
):
|
|
|
|
|
resp = server.handle_request(
|
|
|
|
|
{"id": "1", "method": "complete.slash", "params": {"text": "/mo"}}
|
|
|
|
|
)
|
2026-04-13 18:29:24 -05:00
|
|
|
|
|
|
|
|
assert "error" in resp
|
|
|
|
|
assert "no completer" in resp["error"]["message"]
|
|
|
|
|
|
|
|
|
|
|
2026-04-11 14:02:36 -05:00
|
|
|
def test_input_detect_drop_attaches_image(monkeypatch):
|
|
|
|
|
fake_cli = types.ModuleType("cli")
|
|
|
|
|
fake_cli._detect_file_drop = lambda raw: {
|
|
|
|
|
"path": Path("/tmp/cat.png"),
|
|
|
|
|
"is_image": True,
|
|
|
|
|
"remainder": "",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
server._sessions["sid"] = _session()
|
|
|
|
|
monkeypatch.setitem(sys.modules, "cli", fake_cli)
|
|
|
|
|
|
|
|
|
|
resp = server.handle_request(
|
2026-04-22 15:19:50 -05:00
|
|
|
{
|
|
|
|
|
"id": "1",
|
|
|
|
|
"method": "input.detect_drop",
|
|
|
|
|
"params": {"session_id": "sid", "text": "/tmp/cat.png"},
|
|
|
|
|
}
|
2026-04-11 14:02:36 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert resp["result"]["matched"] is True
|
|
|
|
|
assert resp["result"]["is_image"] is True
|
|
|
|
|
assert resp["result"]["text"] == "[User attached image: cat.png]"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_rollback_restore_resolves_number_and_file_path():
|
|
|
|
|
calls = {}
|
|
|
|
|
|
|
|
|
|
class _Mgr:
|
|
|
|
|
enabled = True
|
|
|
|
|
|
|
|
|
|
def list_checkpoints(self, cwd):
|
|
|
|
|
return [{"hash": "aaa111"}, {"hash": "bbb222"}]
|
|
|
|
|
|
|
|
|
|
def restore(self, cwd, target, file_path=None):
|
|
|
|
|
calls["args"] = (cwd, target, file_path)
|
|
|
|
|
return {"success": True, "message": "done"}
|
|
|
|
|
|
2026-04-22 15:19:50 -05:00
|
|
|
server._sessions["sid"] = _session(
|
|
|
|
|
agent=types.SimpleNamespace(_checkpoint_mgr=_Mgr()), history=[]
|
|
|
|
|
)
|
2026-04-11 14:02:36 -05:00
|
|
|
resp = server.handle_request(
|
|
|
|
|
{
|
|
|
|
|
"id": "1",
|
|
|
|
|
"method": "rollback.restore",
|
|
|
|
|
"params": {"session_id": "sid", "hash": "2", "file_path": "src/app.tsx"},
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert resp["result"]["success"] is True
|
|
|
|
|
assert calls["args"][1] == "bbb222"
|
|
|
|
|
assert calls["args"][2] == "src/app.tsx"
|
feat(steer): /steer <prompt> injects a mid-run note after the next tool call (#12116)
* feat(steer): /steer <prompt> injects a mid-run note after the next tool call
Adds a new slash command that sits between /queue (turn boundary) and
interrupt. /steer <text> stashes the message on the running agent and
the agent loop appends it to the LAST tool result's content once the
current tool batch finishes. The model sees it as part of the tool
output on its next iteration.
No interrupt is fired, no new user turn is inserted, and no prompt
cache invalidation happens beyond the normal per-turn tool-result
churn. Message-role alternation is preserved — we only modify an
existing role:"tool" message's content.
Wiring
------
- hermes_cli/commands.py: register /steer + add to ACTIVE_SESSION_BYPASS_COMMANDS.
- run_agent.py: add _pending_steer state, AIAgent.steer(), _drain_pending_steer(),
_apply_pending_steer_to_tool_results(); drain at end of both parallel and
sequential tool executors; clear on interrupt; return leftover as
result['pending_steer'] if the agent exits before another tool batch.
- cli.py: /steer handler — route to agent.steer() when running, fall back to
the regular queue otherwise; deliver result['pending_steer'] as next turn.
- gateway/run.py: running-agent intercept calls running_agent.steer(); idle-agent
path strips the prefix and forwards as a regular user message.
- tui_gateway/server.py: new session.steer JSON-RPC method.
- ui-tui: SessionSteerResponse type + local /steer slash command that calls
session.steer when ui.busy, otherwise enqueues for the next turn.
Fallbacks
---------
- Agent exits mid-steer → surfaces in run_conversation result as pending_steer
so CLI/gateway deliver it as the next user turn instead of silently dropping it.
- All tools skipped after interrupt → re-stashes pending_steer for the caller.
- No active agent → /steer reduces to sending the text as a normal message.
Tests
-----
- tests/run_agent/test_steer.py — accept/reject, concatenation, drain,
last-tool-result injection, multimodal list content, thread safety,
cleared-on-interrupt, registry membership, bypass-set membership.
- tests/gateway/test_steer_command.py — running agent, pending sentinel,
missing steer() method, rejected payload, empty payload.
- tests/gateway/test_command_bypass_active_session.py — /steer bypasses
the Level-1 base adapter guard.
- tests/test_tui_gateway_server.py — session.steer RPC paths.
72/72 targeted tests pass under scripts/run_tests.sh.
* feat(steer): register /steer in Discord's native slash tree
Discord's app_commands tree is a curated subset of slash commands (not
derived from COMMAND_REGISTRY like Telegram/Slack). /steer already
works there as plain text (routes through handle_message → base
adapter bypass → runner), but registering it here adds Discord's
native autocomplete + argument hint UI so users can discover and
type it like any other first-class command.
2026-04-18 04:17:18 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
# ── session.steer ────────────────────────────────────────────────────
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_session_steer_calls_agent_steer_when_agent_supports_it():
|
|
|
|
|
"""The TUI RPC method must call agent.steer(text) and return a
|
|
|
|
|
queued status without touching interrupt state.
|
|
|
|
|
"""
|
|
|
|
|
calls = {}
|
|
|
|
|
|
|
|
|
|
class _Agent:
|
|
|
|
|
def steer(self, text):
|
|
|
|
|
calls["steer_text"] = text
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
def interrupt(self, *args, **kwargs):
|
|
|
|
|
calls["interrupt_called"] = True
|
|
|
|
|
|
|
|
|
|
server._sessions["sid"] = _session(agent=_Agent())
|
|
|
|
|
try:
|
|
|
|
|
resp = server.handle_request(
|
|
|
|
|
{
|
|
|
|
|
"id": "1",
|
|
|
|
|
"method": "session.steer",
|
|
|
|
|
"params": {"session_id": "sid", "text": "also check auth.log"},
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
finally:
|
|
|
|
|
server._sessions.pop("sid", None)
|
|
|
|
|
|
|
|
|
|
assert "result" in resp, resp
|
|
|
|
|
assert resp["result"]["status"] == "queued"
|
|
|
|
|
assert resp["result"]["text"] == "also check auth.log"
|
|
|
|
|
assert calls["steer_text"] == "also check auth.log"
|
|
|
|
|
assert "interrupt_called" not in calls # must NOT interrupt
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_session_steer_rejects_empty_text():
|
2026-04-22 15:19:50 -05:00
|
|
|
server._sessions["sid"] = _session(
|
|
|
|
|
agent=types.SimpleNamespace(steer=lambda t: True)
|
|
|
|
|
)
|
feat(steer): /steer <prompt> injects a mid-run note after the next tool call (#12116)
* feat(steer): /steer <prompt> injects a mid-run note after the next tool call
Adds a new slash command that sits between /queue (turn boundary) and
interrupt. /steer <text> stashes the message on the running agent and
the agent loop appends it to the LAST tool result's content once the
current tool batch finishes. The model sees it as part of the tool
output on its next iteration.
No interrupt is fired, no new user turn is inserted, and no prompt
cache invalidation happens beyond the normal per-turn tool-result
churn. Message-role alternation is preserved — we only modify an
existing role:"tool" message's content.
Wiring
------
- hermes_cli/commands.py: register /steer + add to ACTIVE_SESSION_BYPASS_COMMANDS.
- run_agent.py: add _pending_steer state, AIAgent.steer(), _drain_pending_steer(),
_apply_pending_steer_to_tool_results(); drain at end of both parallel and
sequential tool executors; clear on interrupt; return leftover as
result['pending_steer'] if the agent exits before another tool batch.
- cli.py: /steer handler — route to agent.steer() when running, fall back to
the regular queue otherwise; deliver result['pending_steer'] as next turn.
- gateway/run.py: running-agent intercept calls running_agent.steer(); idle-agent
path strips the prefix and forwards as a regular user message.
- tui_gateway/server.py: new session.steer JSON-RPC method.
- ui-tui: SessionSteerResponse type + local /steer slash command that calls
session.steer when ui.busy, otherwise enqueues for the next turn.
Fallbacks
---------
- Agent exits mid-steer → surfaces in run_conversation result as pending_steer
so CLI/gateway deliver it as the next user turn instead of silently dropping it.
- All tools skipped after interrupt → re-stashes pending_steer for the caller.
- No active agent → /steer reduces to sending the text as a normal message.
Tests
-----
- tests/run_agent/test_steer.py — accept/reject, concatenation, drain,
last-tool-result injection, multimodal list content, thread safety,
cleared-on-interrupt, registry membership, bypass-set membership.
- tests/gateway/test_steer_command.py — running agent, pending sentinel,
missing steer() method, rejected payload, empty payload.
- tests/gateway/test_command_bypass_active_session.py — /steer bypasses
the Level-1 base adapter guard.
- tests/test_tui_gateway_server.py — session.steer RPC paths.
72/72 targeted tests pass under scripts/run_tests.sh.
* feat(steer): register /steer in Discord's native slash tree
Discord's app_commands tree is a curated subset of slash commands (not
derived from COMMAND_REGISTRY like Telegram/Slack). /steer already
works there as plain text (routes through handle_message → base
adapter bypass → runner), but registering it here adds Discord's
native autocomplete + argument hint UI so users can discover and
type it like any other first-class command.
2026-04-18 04:17:18 -07:00
|
|
|
try:
|
|
|
|
|
resp = server.handle_request(
|
|
|
|
|
{
|
|
|
|
|
"id": "1",
|
|
|
|
|
"method": "session.steer",
|
|
|
|
|
"params": {"session_id": "sid", "text": " "},
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
finally:
|
|
|
|
|
server._sessions.pop("sid", None)
|
|
|
|
|
|
|
|
|
|
assert "error" in resp, resp
|
|
|
|
|
assert resp["error"]["code"] == 4002
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_session_steer_errors_when_agent_has_no_steer_method():
|
|
|
|
|
server._sessions["sid"] = _session(agent=types.SimpleNamespace()) # no steer()
|
|
|
|
|
try:
|
|
|
|
|
resp = server.handle_request(
|
|
|
|
|
{
|
|
|
|
|
"id": "1",
|
|
|
|
|
"method": "session.steer",
|
|
|
|
|
"params": {"session_id": "sid", "text": "hi"},
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
finally:
|
|
|
|
|
server._sessions.pop("sid", None)
|
|
|
|
|
|
|
|
|
|
assert "error" in resp, resp
|
|
|
|
|
assert resp["error"]["code"] == 4010
|
|
|
|
|
|
2026-04-18 09:23:47 -05:00
|
|
|
|
|
|
|
|
def test_session_info_includes_mcp_servers(monkeypatch):
|
|
|
|
|
fake_status = [
|
|
|
|
|
{"name": "github", "transport": "http", "tools": 12, "connected": True},
|
|
|
|
|
{"name": "filesystem", "transport": "stdio", "tools": 4, "connected": True},
|
|
|
|
|
{"name": "broken", "transport": "stdio", "tools": 0, "connected": False},
|
|
|
|
|
]
|
|
|
|
|
fake_mod = types.ModuleType("tools.mcp_tool")
|
|
|
|
|
fake_mod.get_mcp_status = lambda: fake_status
|
|
|
|
|
monkeypatch.setitem(sys.modules, "tools.mcp_tool", fake_mod)
|
|
|
|
|
|
|
|
|
|
info = server._session_info(types.SimpleNamespace(tools=[], model=""))
|
|
|
|
|
|
|
|
|
|
assert info["mcp_servers"] == fake_status
|
|
|
|
|
|
fix(tui): reject history-mutating commands while session is running (#12416)
Fixes silent data loss in the TUI when /undo, /compress, /retry, or
rollback.restore runs during an in-flight agent turn. The version-
guard at prompt.submit:1449 would fail the version check and silently
skip writing the agent's result — UI showed the assistant reply but
DB / backend history never received it, causing UI↔backend desync
that persisted across session resume.
Changes (tui_gateway/server.py):
- session.undo, session.compress, /retry, rollback.restore (full-history
only — file-scoped rollbacks still allowed): reject with 4009 when
session.running is True. Users can /interrupt first.
- prompt.submit: on history_version mismatch (defensive backstop),
attach a 'warning' field to message.complete and log to stderr
instead of silently dropping the agent's output. The UI can surface
the warning to the user; the operator can spot it in logs.
Tests (tests/test_tui_gateway_server.py): 6 new cases.
- test_session_undo_rejects_while_running
- test_session_undo_allowed_when_idle (regression guard)
- test_session_compress_rejects_while_running
- test_rollback_restore_rejects_full_history_while_running
- test_prompt_submit_history_version_mismatch_surfaces_warning
- test_prompt_submit_history_version_match_persists_normally (regression)
Validated: against unpatched server.py the three 'rejects_while_running'
tests fail and the version-mismatch test fails (no 'warning' field).
With the fix, all 6 pass, all 33 tests in the file pass, 74 TUI tests
in total pass. Live E2E against the live Python environment confirmed
all 5 patches present and guards enforce 4009 exactly as designed.
2026-04-18 22:30:10 -07:00
|
|
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
# History-mutating commands must reject while session.running is True.
|
|
|
|
|
# Without these guards, prompt.submit's post-run history write either
|
|
|
|
|
# clobbers the mutation (version matches) or silently drops the agent's
|
|
|
|
|
# output (version mismatch) — both produce UI<->backend state desync.
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_session_undo_rejects_while_running():
|
|
|
|
|
"""Fix for TUI silent-drop #1: /undo must not mutate history
|
|
|
|
|
while the agent is mid-turn — would either clobber the undo or
|
|
|
|
|
cause prompt.submit to silently drop the agent's response."""
|
2026-04-22 15:19:50 -05:00
|
|
|
server._sessions["sid"] = _session(
|
|
|
|
|
running=True,
|
|
|
|
|
history=[
|
|
|
|
|
{"role": "user", "content": "hi"},
|
|
|
|
|
{"role": "assistant", "content": "hello"},
|
|
|
|
|
],
|
|
|
|
|
)
|
fix(tui): reject history-mutating commands while session is running (#12416)
Fixes silent data loss in the TUI when /undo, /compress, /retry, or
rollback.restore runs during an in-flight agent turn. The version-
guard at prompt.submit:1449 would fail the version check and silently
skip writing the agent's result — UI showed the assistant reply but
DB / backend history never received it, causing UI↔backend desync
that persisted across session resume.
Changes (tui_gateway/server.py):
- session.undo, session.compress, /retry, rollback.restore (full-history
only — file-scoped rollbacks still allowed): reject with 4009 when
session.running is True. Users can /interrupt first.
- prompt.submit: on history_version mismatch (defensive backstop),
attach a 'warning' field to message.complete and log to stderr
instead of silently dropping the agent's output. The UI can surface
the warning to the user; the operator can spot it in logs.
Tests (tests/test_tui_gateway_server.py): 6 new cases.
- test_session_undo_rejects_while_running
- test_session_undo_allowed_when_idle (regression guard)
- test_session_compress_rejects_while_running
- test_rollback_restore_rejects_full_history_while_running
- test_prompt_submit_history_version_mismatch_surfaces_warning
- test_prompt_submit_history_version_match_persists_normally (regression)
Validated: against unpatched server.py the three 'rejects_while_running'
tests fail and the version-mismatch test fails (no 'warning' field).
With the fix, all 6 pass, all 33 tests in the file pass, 74 TUI tests
in total pass. Live E2E against the live Python environment confirmed
all 5 patches present and guards enforce 4009 exactly as designed.
2026-04-18 22:30:10 -07:00
|
|
|
try:
|
|
|
|
|
resp = server.handle_request(
|
|
|
|
|
{"id": "1", "method": "session.undo", "params": {"session_id": "sid"}}
|
|
|
|
|
)
|
|
|
|
|
assert resp.get("error"), "session.undo should reject while running"
|
|
|
|
|
assert resp["error"]["code"] == 4009
|
|
|
|
|
assert "session busy" in resp["error"]["message"]
|
|
|
|
|
# History must be unchanged
|
|
|
|
|
assert len(server._sessions["sid"]["history"]) == 2
|
|
|
|
|
finally:
|
|
|
|
|
server._sessions.pop("sid", None)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_session_undo_allowed_when_idle():
|
|
|
|
|
"""Regression guard: when not running, /undo still works."""
|
2026-04-22 15:19:50 -05:00
|
|
|
server._sessions["sid"] = _session(
|
|
|
|
|
running=False,
|
|
|
|
|
history=[
|
|
|
|
|
{"role": "user", "content": "hi"},
|
|
|
|
|
{"role": "assistant", "content": "hello"},
|
|
|
|
|
],
|
|
|
|
|
)
|
fix(tui): reject history-mutating commands while session is running (#12416)
Fixes silent data loss in the TUI when /undo, /compress, /retry, or
rollback.restore runs during an in-flight agent turn. The version-
guard at prompt.submit:1449 would fail the version check and silently
skip writing the agent's result — UI showed the assistant reply but
DB / backend history never received it, causing UI↔backend desync
that persisted across session resume.
Changes (tui_gateway/server.py):
- session.undo, session.compress, /retry, rollback.restore (full-history
only — file-scoped rollbacks still allowed): reject with 4009 when
session.running is True. Users can /interrupt first.
- prompt.submit: on history_version mismatch (defensive backstop),
attach a 'warning' field to message.complete and log to stderr
instead of silently dropping the agent's output. The UI can surface
the warning to the user; the operator can spot it in logs.
Tests (tests/test_tui_gateway_server.py): 6 new cases.
- test_session_undo_rejects_while_running
- test_session_undo_allowed_when_idle (regression guard)
- test_session_compress_rejects_while_running
- test_rollback_restore_rejects_full_history_while_running
- test_prompt_submit_history_version_mismatch_surfaces_warning
- test_prompt_submit_history_version_match_persists_normally (regression)
Validated: against unpatched server.py the three 'rejects_while_running'
tests fail and the version-mismatch test fails (no 'warning' field).
With the fix, all 6 pass, all 33 tests in the file pass, 74 TUI tests
in total pass. Live E2E against the live Python environment confirmed
all 5 patches present and guards enforce 4009 exactly as designed.
2026-04-18 22:30:10 -07:00
|
|
|
try:
|
|
|
|
|
resp = server.handle_request(
|
|
|
|
|
{"id": "1", "method": "session.undo", "params": {"session_id": "sid"}}
|
|
|
|
|
)
|
|
|
|
|
assert resp.get("result"), f"got error: {resp.get('error')}"
|
|
|
|
|
assert resp["result"]["removed"] == 2
|
|
|
|
|
assert server._sessions["sid"]["history"] == []
|
|
|
|
|
finally:
|
|
|
|
|
server._sessions.pop("sid", None)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_session_compress_rejects_while_running(monkeypatch):
|
|
|
|
|
server._sessions["sid"] = _session(running=True)
|
|
|
|
|
try:
|
|
|
|
|
resp = server.handle_request(
|
|
|
|
|
{"id": "1", "method": "session.compress", "params": {"session_id": "sid"}}
|
|
|
|
|
)
|
|
|
|
|
assert resp.get("error")
|
|
|
|
|
assert resp["error"]["code"] == 4009
|
|
|
|
|
finally:
|
|
|
|
|
server._sessions.pop("sid", None)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_rollback_restore_rejects_full_history_while_running(monkeypatch):
|
|
|
|
|
"""Full-history rollback must reject; file-scoped rollback still allowed."""
|
|
|
|
|
server._sessions["sid"] = _session(running=True)
|
|
|
|
|
try:
|
|
|
|
|
resp = server.handle_request(
|
2026-04-22 15:19:50 -05:00
|
|
|
{
|
|
|
|
|
"id": "1",
|
|
|
|
|
"method": "rollback.restore",
|
|
|
|
|
"params": {"session_id": "sid", "hash": "abc"},
|
|
|
|
|
}
|
fix(tui): reject history-mutating commands while session is running (#12416)
Fixes silent data loss in the TUI when /undo, /compress, /retry, or
rollback.restore runs during an in-flight agent turn. The version-
guard at prompt.submit:1449 would fail the version check and silently
skip writing the agent's result — UI showed the assistant reply but
DB / backend history never received it, causing UI↔backend desync
that persisted across session resume.
Changes (tui_gateway/server.py):
- session.undo, session.compress, /retry, rollback.restore (full-history
only — file-scoped rollbacks still allowed): reject with 4009 when
session.running is True. Users can /interrupt first.
- prompt.submit: on history_version mismatch (defensive backstop),
attach a 'warning' field to message.complete and log to stderr
instead of silently dropping the agent's output. The UI can surface
the warning to the user; the operator can spot it in logs.
Tests (tests/test_tui_gateway_server.py): 6 new cases.
- test_session_undo_rejects_while_running
- test_session_undo_allowed_when_idle (regression guard)
- test_session_compress_rejects_while_running
- test_rollback_restore_rejects_full_history_while_running
- test_prompt_submit_history_version_mismatch_surfaces_warning
- test_prompt_submit_history_version_match_persists_normally (regression)
Validated: against unpatched server.py the three 'rejects_while_running'
tests fail and the version-mismatch test fails (no 'warning' field).
With the fix, all 6 pass, all 33 tests in the file pass, 74 TUI tests
in total pass. Live E2E against the live Python environment confirmed
all 5 patches present and guards enforce 4009 exactly as designed.
2026-04-18 22:30:10 -07:00
|
|
|
)
|
|
|
|
|
assert resp.get("error"), "full-history rollback should reject while running"
|
|
|
|
|
assert resp["error"]["code"] == 4009
|
|
|
|
|
finally:
|
|
|
|
|
server._sessions.pop("sid", None)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_prompt_submit_history_version_mismatch_surfaces_warning(monkeypatch):
|
|
|
|
|
"""Fix for TUI silent-drop #2: the defensive backstop at prompt.submit
|
|
|
|
|
must attach a 'warning' to message.complete when history was
|
|
|
|
|
mutated externally during the turn (instead of silently dropping
|
|
|
|
|
the agent's output)."""
|
|
|
|
|
# Agent bumps history_version itself mid-run to simulate an external
|
|
|
|
|
# mutation slipping past the guards.
|
|
|
|
|
session_ref = {"s": None}
|
|
|
|
|
|
|
|
|
|
class _RacyAgent:
|
2026-04-22 15:19:50 -05:00
|
|
|
def run_conversation(
|
|
|
|
|
self, prompt, conversation_history=None, stream_callback=None
|
|
|
|
|
):
|
fix(tui): reject history-mutating commands while session is running (#12416)
Fixes silent data loss in the TUI when /undo, /compress, /retry, or
rollback.restore runs during an in-flight agent turn. The version-
guard at prompt.submit:1449 would fail the version check and silently
skip writing the agent's result — UI showed the assistant reply but
DB / backend history never received it, causing UI↔backend desync
that persisted across session resume.
Changes (tui_gateway/server.py):
- session.undo, session.compress, /retry, rollback.restore (full-history
only — file-scoped rollbacks still allowed): reject with 4009 when
session.running is True. Users can /interrupt first.
- prompt.submit: on history_version mismatch (defensive backstop),
attach a 'warning' field to message.complete and log to stderr
instead of silently dropping the agent's output. The UI can surface
the warning to the user; the operator can spot it in logs.
Tests (tests/test_tui_gateway_server.py): 6 new cases.
- test_session_undo_rejects_while_running
- test_session_undo_allowed_when_idle (regression guard)
- test_session_compress_rejects_while_running
- test_rollback_restore_rejects_full_history_while_running
- test_prompt_submit_history_version_mismatch_surfaces_warning
- test_prompt_submit_history_version_match_persists_normally (regression)
Validated: against unpatched server.py the three 'rejects_while_running'
tests fail and the version-mismatch test fails (no 'warning' field).
With the fix, all 6 pass, all 33 tests in the file pass, 74 TUI tests
in total pass. Live E2E against the live Python environment confirmed
all 5 patches present and guards enforce 4009 exactly as designed.
2026-04-18 22:30:10 -07:00
|
|
|
# Simulate: something external bumped history_version
|
|
|
|
|
# while we were running.
|
|
|
|
|
with session_ref["s"]["history_lock"]:
|
|
|
|
|
session_ref["s"]["history_version"] += 1
|
2026-04-22 15:19:50 -05:00
|
|
|
return {
|
|
|
|
|
"final_response": "agent reply",
|
|
|
|
|
"messages": [{"role": "assistant", "content": "agent reply"}],
|
|
|
|
|
}
|
fix(tui): reject history-mutating commands while session is running (#12416)
Fixes silent data loss in the TUI when /undo, /compress, /retry, or
rollback.restore runs during an in-flight agent turn. The version-
guard at prompt.submit:1449 would fail the version check and silently
skip writing the agent's result — UI showed the assistant reply but
DB / backend history never received it, causing UI↔backend desync
that persisted across session resume.
Changes (tui_gateway/server.py):
- session.undo, session.compress, /retry, rollback.restore (full-history
only — file-scoped rollbacks still allowed): reject with 4009 when
session.running is True. Users can /interrupt first.
- prompt.submit: on history_version mismatch (defensive backstop),
attach a 'warning' field to message.complete and log to stderr
instead of silently dropping the agent's output. The UI can surface
the warning to the user; the operator can spot it in logs.
Tests (tests/test_tui_gateway_server.py): 6 new cases.
- test_session_undo_rejects_while_running
- test_session_undo_allowed_when_idle (regression guard)
- test_session_compress_rejects_while_running
- test_rollback_restore_rejects_full_history_while_running
- test_prompt_submit_history_version_mismatch_surfaces_warning
- test_prompt_submit_history_version_match_persists_normally (regression)
Validated: against unpatched server.py the three 'rejects_while_running'
tests fail and the version-mismatch test fails (no 'warning' field).
With the fix, all 6 pass, all 33 tests in the file pass, 74 TUI tests
in total pass. Live E2E against the live Python environment confirmed
all 5 patches present and guards enforce 4009 exactly as designed.
2026-04-18 22:30:10 -07:00
|
|
|
|
|
|
|
|
class _ImmediateThread:
|
|
|
|
|
def __init__(self, target=None, daemon=None):
|
|
|
|
|
self._target = target
|
|
|
|
|
|
|
|
|
|
def start(self):
|
|
|
|
|
self._target()
|
|
|
|
|
|
|
|
|
|
server._sessions["sid"] = _session(agent=_RacyAgent())
|
|
|
|
|
session_ref["s"] = server._sessions["sid"]
|
|
|
|
|
emits: list[tuple] = []
|
|
|
|
|
try:
|
|
|
|
|
monkeypatch.setattr(server.threading, "Thread", _ImmediateThread)
|
|
|
|
|
monkeypatch.setattr(server, "_get_usage", lambda _a: {})
|
|
|
|
|
monkeypatch.setattr(server, "render_message", lambda _t, _c: "")
|
|
|
|
|
monkeypatch.setattr(server, "_emit", lambda *a: emits.append(a))
|
|
|
|
|
|
|
|
|
|
resp = server.handle_request(
|
2026-04-22 15:19:50 -05:00
|
|
|
{
|
|
|
|
|
"id": "1",
|
|
|
|
|
"method": "prompt.submit",
|
|
|
|
|
"params": {"session_id": "sid", "text": "hi"},
|
|
|
|
|
}
|
fix(tui): reject history-mutating commands while session is running (#12416)
Fixes silent data loss in the TUI when /undo, /compress, /retry, or
rollback.restore runs during an in-flight agent turn. The version-
guard at prompt.submit:1449 would fail the version check and silently
skip writing the agent's result — UI showed the assistant reply but
DB / backend history never received it, causing UI↔backend desync
that persisted across session resume.
Changes (tui_gateway/server.py):
- session.undo, session.compress, /retry, rollback.restore (full-history
only — file-scoped rollbacks still allowed): reject with 4009 when
session.running is True. Users can /interrupt first.
- prompt.submit: on history_version mismatch (defensive backstop),
attach a 'warning' field to message.complete and log to stderr
instead of silently dropping the agent's output. The UI can surface
the warning to the user; the operator can spot it in logs.
Tests (tests/test_tui_gateway_server.py): 6 new cases.
- test_session_undo_rejects_while_running
- test_session_undo_allowed_when_idle (regression guard)
- test_session_compress_rejects_while_running
- test_rollback_restore_rejects_full_history_while_running
- test_prompt_submit_history_version_mismatch_surfaces_warning
- test_prompt_submit_history_version_match_persists_normally (regression)
Validated: against unpatched server.py the three 'rejects_while_running'
tests fail and the version-mismatch test fails (no 'warning' field).
With the fix, all 6 pass, all 33 tests in the file pass, 74 TUI tests
in total pass. Live E2E against the live Python environment confirmed
all 5 patches present and guards enforce 4009 exactly as designed.
2026-04-18 22:30:10 -07:00
|
|
|
)
|
|
|
|
|
assert resp.get("result"), f"got error: {resp.get('error')}"
|
|
|
|
|
|
|
|
|
|
# History should NOT contain the agent's output (version mismatch)
|
|
|
|
|
assert server._sessions["sid"]["history"] == []
|
|
|
|
|
|
|
|
|
|
# message.complete must carry a 'warning' so the UI / operator
|
|
|
|
|
# knows the output was not persisted.
|
|
|
|
|
complete_calls = [a for a in emits if a[0] == "message.complete"]
|
|
|
|
|
assert len(complete_calls) == 1
|
|
|
|
|
_, _, payload = complete_calls[0]
|
|
|
|
|
assert "warning" in payload, (
|
|
|
|
|
"message.complete must include a 'warning' field on "
|
|
|
|
|
"history_version mismatch — otherwise the UI silently "
|
|
|
|
|
"shows output that was never persisted"
|
|
|
|
|
)
|
2026-04-22 15:19:50 -05:00
|
|
|
assert (
|
|
|
|
|
"not saved" in payload["warning"].lower()
|
|
|
|
|
or "changed" in payload["warning"].lower()
|
|
|
|
|
)
|
fix(tui): reject history-mutating commands while session is running (#12416)
Fixes silent data loss in the TUI when /undo, /compress, /retry, or
rollback.restore runs during an in-flight agent turn. The version-
guard at prompt.submit:1449 would fail the version check and silently
skip writing the agent's result — UI showed the assistant reply but
DB / backend history never received it, causing UI↔backend desync
that persisted across session resume.
Changes (tui_gateway/server.py):
- session.undo, session.compress, /retry, rollback.restore (full-history
only — file-scoped rollbacks still allowed): reject with 4009 when
session.running is True. Users can /interrupt first.
- prompt.submit: on history_version mismatch (defensive backstop),
attach a 'warning' field to message.complete and log to stderr
instead of silently dropping the agent's output. The UI can surface
the warning to the user; the operator can spot it in logs.
Tests (tests/test_tui_gateway_server.py): 6 new cases.
- test_session_undo_rejects_while_running
- test_session_undo_allowed_when_idle (regression guard)
- test_session_compress_rejects_while_running
- test_rollback_restore_rejects_full_history_while_running
- test_prompt_submit_history_version_mismatch_surfaces_warning
- test_prompt_submit_history_version_match_persists_normally (regression)
Validated: against unpatched server.py the three 'rejects_while_running'
tests fail and the version-mismatch test fails (no 'warning' field).
With the fix, all 6 pass, all 33 tests in the file pass, 74 TUI tests
in total pass. Live E2E against the live Python environment confirmed
all 5 patches present and guards enforce 4009 exactly as designed.
2026-04-18 22:30:10 -07:00
|
|
|
finally:
|
|
|
|
|
server._sessions.pop("sid", None)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_prompt_submit_history_version_match_persists_normally(monkeypatch):
|
|
|
|
|
"""Regression guard: the backstop does not affect the happy path."""
|
2026-04-22 15:19:50 -05:00
|
|
|
|
fix(tui): reject history-mutating commands while session is running (#12416)
Fixes silent data loss in the TUI when /undo, /compress, /retry, or
rollback.restore runs during an in-flight agent turn. The version-
guard at prompt.submit:1449 would fail the version check and silently
skip writing the agent's result — UI showed the assistant reply but
DB / backend history never received it, causing UI↔backend desync
that persisted across session resume.
Changes (tui_gateway/server.py):
- session.undo, session.compress, /retry, rollback.restore (full-history
only — file-scoped rollbacks still allowed): reject with 4009 when
session.running is True. Users can /interrupt first.
- prompt.submit: on history_version mismatch (defensive backstop),
attach a 'warning' field to message.complete and log to stderr
instead of silently dropping the agent's output. The UI can surface
the warning to the user; the operator can spot it in logs.
Tests (tests/test_tui_gateway_server.py): 6 new cases.
- test_session_undo_rejects_while_running
- test_session_undo_allowed_when_idle (regression guard)
- test_session_compress_rejects_while_running
- test_rollback_restore_rejects_full_history_while_running
- test_prompt_submit_history_version_mismatch_surfaces_warning
- test_prompt_submit_history_version_match_persists_normally (regression)
Validated: against unpatched server.py the three 'rejects_while_running'
tests fail and the version-mismatch test fails (no 'warning' field).
With the fix, all 6 pass, all 33 tests in the file pass, 74 TUI tests
in total pass. Live E2E against the live Python environment confirmed
all 5 patches present and guards enforce 4009 exactly as designed.
2026-04-18 22:30:10 -07:00
|
|
|
class _Agent:
|
2026-04-22 15:19:50 -05:00
|
|
|
def run_conversation(
|
|
|
|
|
self, prompt, conversation_history=None, stream_callback=None
|
|
|
|
|
):
|
|
|
|
|
return {
|
|
|
|
|
"final_response": "reply",
|
|
|
|
|
"messages": [{"role": "assistant", "content": "reply"}],
|
|
|
|
|
}
|
fix(tui): reject history-mutating commands while session is running (#12416)
Fixes silent data loss in the TUI when /undo, /compress, /retry, or
rollback.restore runs during an in-flight agent turn. The version-
guard at prompt.submit:1449 would fail the version check and silently
skip writing the agent's result — UI showed the assistant reply but
DB / backend history never received it, causing UI↔backend desync
that persisted across session resume.
Changes (tui_gateway/server.py):
- session.undo, session.compress, /retry, rollback.restore (full-history
only — file-scoped rollbacks still allowed): reject with 4009 when
session.running is True. Users can /interrupt first.
- prompt.submit: on history_version mismatch (defensive backstop),
attach a 'warning' field to message.complete and log to stderr
instead of silently dropping the agent's output. The UI can surface
the warning to the user; the operator can spot it in logs.
Tests (tests/test_tui_gateway_server.py): 6 new cases.
- test_session_undo_rejects_while_running
- test_session_undo_allowed_when_idle (regression guard)
- test_session_compress_rejects_while_running
- test_rollback_restore_rejects_full_history_while_running
- test_prompt_submit_history_version_mismatch_surfaces_warning
- test_prompt_submit_history_version_match_persists_normally (regression)
Validated: against unpatched server.py the three 'rejects_while_running'
tests fail and the version-mismatch test fails (no 'warning' field).
With the fix, all 6 pass, all 33 tests in the file pass, 74 TUI tests
in total pass. Live E2E against the live Python environment confirmed
all 5 patches present and guards enforce 4009 exactly as designed.
2026-04-18 22:30:10 -07:00
|
|
|
|
|
|
|
|
class _ImmediateThread:
|
|
|
|
|
def __init__(self, target=None, daemon=None):
|
|
|
|
|
self._target = target
|
|
|
|
|
|
|
|
|
|
def start(self):
|
|
|
|
|
self._target()
|
|
|
|
|
|
|
|
|
|
server._sessions["sid"] = _session(agent=_Agent())
|
|
|
|
|
emits: list[tuple] = []
|
|
|
|
|
try:
|
|
|
|
|
monkeypatch.setattr(server.threading, "Thread", _ImmediateThread)
|
|
|
|
|
monkeypatch.setattr(server, "_get_usage", lambda _a: {})
|
|
|
|
|
monkeypatch.setattr(server, "render_message", lambda _t, _c: "")
|
|
|
|
|
monkeypatch.setattr(server, "_emit", lambda *a: emits.append(a))
|
|
|
|
|
|
|
|
|
|
resp = server.handle_request(
|
2026-04-22 15:19:50 -05:00
|
|
|
{
|
|
|
|
|
"id": "1",
|
|
|
|
|
"method": "prompt.submit",
|
|
|
|
|
"params": {"session_id": "sid", "text": "hi"},
|
|
|
|
|
}
|
fix(tui): reject history-mutating commands while session is running (#12416)
Fixes silent data loss in the TUI when /undo, /compress, /retry, or
rollback.restore runs during an in-flight agent turn. The version-
guard at prompt.submit:1449 would fail the version check and silently
skip writing the agent's result — UI showed the assistant reply but
DB / backend history never received it, causing UI↔backend desync
that persisted across session resume.
Changes (tui_gateway/server.py):
- session.undo, session.compress, /retry, rollback.restore (full-history
only — file-scoped rollbacks still allowed): reject with 4009 when
session.running is True. Users can /interrupt first.
- prompt.submit: on history_version mismatch (defensive backstop),
attach a 'warning' field to message.complete and log to stderr
instead of silently dropping the agent's output. The UI can surface
the warning to the user; the operator can spot it in logs.
Tests (tests/test_tui_gateway_server.py): 6 new cases.
- test_session_undo_rejects_while_running
- test_session_undo_allowed_when_idle (regression guard)
- test_session_compress_rejects_while_running
- test_rollback_restore_rejects_full_history_while_running
- test_prompt_submit_history_version_mismatch_surfaces_warning
- test_prompt_submit_history_version_match_persists_normally (regression)
Validated: against unpatched server.py the three 'rejects_while_running'
tests fail and the version-mismatch test fails (no 'warning' field).
With the fix, all 6 pass, all 33 tests in the file pass, 74 TUI tests
in total pass. Live E2E against the live Python environment confirmed
all 5 patches present and guards enforce 4009 exactly as designed.
2026-04-18 22:30:10 -07:00
|
|
|
)
|
|
|
|
|
assert resp.get("result")
|
|
|
|
|
|
|
|
|
|
# History was written
|
2026-04-22 15:19:50 -05:00
|
|
|
assert server._sessions["sid"]["history"] == [
|
|
|
|
|
{"role": "assistant", "content": "reply"}
|
|
|
|
|
]
|
fix(tui): reject history-mutating commands while session is running (#12416)
Fixes silent data loss in the TUI when /undo, /compress, /retry, or
rollback.restore runs during an in-flight agent turn. The version-
guard at prompt.submit:1449 would fail the version check and silently
skip writing the agent's result — UI showed the assistant reply but
DB / backend history never received it, causing UI↔backend desync
that persisted across session resume.
Changes (tui_gateway/server.py):
- session.undo, session.compress, /retry, rollback.restore (full-history
only — file-scoped rollbacks still allowed): reject with 4009 when
session.running is True. Users can /interrupt first.
- prompt.submit: on history_version mismatch (defensive backstop),
attach a 'warning' field to message.complete and log to stderr
instead of silently dropping the agent's output. The UI can surface
the warning to the user; the operator can spot it in logs.
Tests (tests/test_tui_gateway_server.py): 6 new cases.
- test_session_undo_rejects_while_running
- test_session_undo_allowed_when_idle (regression guard)
- test_session_compress_rejects_while_running
- test_rollback_restore_rejects_full_history_while_running
- test_prompt_submit_history_version_mismatch_surfaces_warning
- test_prompt_submit_history_version_match_persists_normally (regression)
Validated: against unpatched server.py the three 'rejects_while_running'
tests fail and the version-mismatch test fails (no 'warning' field).
With the fix, all 6 pass, all 33 tests in the file pass, 74 TUI tests
in total pass. Live E2E against the live Python environment confirmed
all 5 patches present and guards enforce 4009 exactly as designed.
2026-04-18 22:30:10 -07:00
|
|
|
assert server._sessions["sid"]["history_version"] == 1
|
|
|
|
|
|
|
|
|
|
# No warning should be attached
|
|
|
|
|
complete_calls = [a for a in emits if a[0] == "message.complete"]
|
|
|
|
|
assert len(complete_calls) == 1
|
|
|
|
|
_, _, payload = complete_calls[0]
|
|
|
|
|
assert "warning" not in payload
|
|
|
|
|
finally:
|
|
|
|
|
server._sessions.pop("sid", None)
|
|
|
|
|
|
2026-04-19 00:03:58 -07:00
|
|
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
# session.interrupt must only cancel pending prompts owned by the calling
|
|
|
|
|
# session — it must not blast-resolve clarify/sudo/secret prompts on
|
|
|
|
|
# unrelated sessions sharing the same tui_gateway process. Without
|
|
|
|
|
# session scoping the other sessions' prompts silently resolve to empty
|
|
|
|
|
# strings, unblocking their agent threads as if the user cancelled.
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_interrupt_only_clears_own_session_pending():
|
|
|
|
|
"""session.interrupt on session A must NOT release pending prompts
|
|
|
|
|
that belong to session B."""
|
|
|
|
|
import types
|
|
|
|
|
|
|
|
|
|
session_a = _session()
|
|
|
|
|
session_a["agent"] = types.SimpleNamespace(interrupt=lambda: None)
|
|
|
|
|
session_b = _session()
|
|
|
|
|
session_b["agent"] = types.SimpleNamespace(interrupt=lambda: None)
|
|
|
|
|
server._sessions["sid_a"] = session_a
|
|
|
|
|
server._sessions["sid_b"] = session_b
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
# Simulate pending prompts on both sessions (what _block creates
|
|
|
|
|
# while a clarify/sudo/secret request is outstanding).
|
|
|
|
|
ev_a = threading.Event()
|
|
|
|
|
ev_b = threading.Event()
|
|
|
|
|
server._pending["rid-a"] = ("sid_a", ev_a)
|
|
|
|
|
server._pending["rid-b"] = ("sid_b", ev_b)
|
|
|
|
|
server._answers.clear()
|
|
|
|
|
|
|
|
|
|
# Interrupt session A.
|
|
|
|
|
resp = server.handle_request(
|
2026-04-22 15:19:50 -05:00
|
|
|
{
|
|
|
|
|
"id": "1",
|
|
|
|
|
"method": "session.interrupt",
|
|
|
|
|
"params": {"session_id": "sid_a"},
|
|
|
|
|
}
|
2026-04-19 00:03:58 -07:00
|
|
|
)
|
|
|
|
|
assert resp.get("result"), f"got error: {resp.get('error')}"
|
|
|
|
|
|
|
|
|
|
# Session A's pending must be released to empty.
|
|
|
|
|
assert ev_a.is_set(), "sid_a pending Event should be set after interrupt"
|
|
|
|
|
assert server._answers.get("rid-a") == ""
|
|
|
|
|
|
|
|
|
|
# Session B's pending MUST remain untouched — no cross-session blast.
|
|
|
|
|
assert not ev_b.is_set(), (
|
|
|
|
|
"CRITICAL: session.interrupt on sid_a released a pending prompt "
|
|
|
|
|
"belonging to sid_b — other sessions' clarify/sudo/secret "
|
|
|
|
|
"prompts are being silently cancelled"
|
|
|
|
|
)
|
|
|
|
|
assert "rid-b" not in server._answers
|
|
|
|
|
finally:
|
|
|
|
|
server._sessions.pop("sid_a", None)
|
|
|
|
|
server._sessions.pop("sid_b", None)
|
|
|
|
|
server._pending.pop("rid-a", None)
|
|
|
|
|
server._pending.pop("rid-b", None)
|
|
|
|
|
server._answers.pop("rid-a", None)
|
|
|
|
|
server._answers.pop("rid-b", None)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_interrupt_clears_multiple_own_pending():
|
|
|
|
|
"""When a single session has multiple pending prompts (uncommon but
|
|
|
|
|
possible via nested tool calls), interrupt must release all of them."""
|
|
|
|
|
import types
|
|
|
|
|
|
|
|
|
|
sess = _session()
|
|
|
|
|
sess["agent"] = types.SimpleNamespace(interrupt=lambda: None)
|
|
|
|
|
server._sessions["sid"] = sess
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
ev1, ev2 = threading.Event(), threading.Event()
|
|
|
|
|
server._pending["r1"] = ("sid", ev1)
|
|
|
|
|
server._pending["r2"] = ("sid", ev2)
|
|
|
|
|
|
|
|
|
|
resp = server.handle_request(
|
|
|
|
|
{"id": "1", "method": "session.interrupt", "params": {"session_id": "sid"}}
|
|
|
|
|
)
|
|
|
|
|
assert resp.get("result")
|
|
|
|
|
assert ev1.is_set() and ev2.is_set()
|
|
|
|
|
assert server._answers.get("r1") == "" and server._answers.get("r2") == ""
|
|
|
|
|
finally:
|
|
|
|
|
server._sessions.pop("sid", None)
|
|
|
|
|
for key in ("r1", "r2"):
|
|
|
|
|
server._pending.pop(key, None)
|
|
|
|
|
server._answers.pop(key, None)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_clear_pending_without_sid_clears_all():
|
|
|
|
|
"""_clear_pending(None) is the shutdown path — must still release
|
|
|
|
|
every pending prompt regardless of owning session."""
|
|
|
|
|
ev1, ev2, ev3 = threading.Event(), threading.Event(), threading.Event()
|
|
|
|
|
server._pending["a"] = ("sid_x", ev1)
|
|
|
|
|
server._pending["b"] = ("sid_y", ev2)
|
|
|
|
|
server._pending["c"] = ("sid_z", ev3)
|
|
|
|
|
try:
|
|
|
|
|
server._clear_pending(None)
|
|
|
|
|
assert ev1.is_set() and ev2.is_set() and ev3.is_set()
|
|
|
|
|
finally:
|
|
|
|
|
for key in ("a", "b", "c"):
|
|
|
|
|
server._pending.pop(key, None)
|
|
|
|
|
server._answers.pop(key, None)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_respond_unpacks_sid_tuple_correctly():
|
|
|
|
|
"""After the (sid, Event) tuple change, _respond must still work."""
|
|
|
|
|
ev = threading.Event()
|
|
|
|
|
server._pending["rid-x"] = ("sid_x", ev)
|
|
|
|
|
try:
|
|
|
|
|
resp = server.handle_request(
|
2026-04-22 15:19:50 -05:00
|
|
|
{
|
|
|
|
|
"id": "1",
|
|
|
|
|
"method": "clarify.respond",
|
|
|
|
|
"params": {"request_id": "rid-x", "answer": "the answer"},
|
|
|
|
|
}
|
2026-04-19 00:03:58 -07:00
|
|
|
)
|
|
|
|
|
assert resp.get("result")
|
|
|
|
|
assert ev.is_set()
|
|
|
|
|
assert server._answers.get("rid-x") == "the answer"
|
|
|
|
|
finally:
|
|
|
|
|
server._pending.pop("rid-x", None)
|
|
|
|
|
server._answers.pop("rid-x", None)
|
|
|
|
|
|
2026-04-19 05:19:57 -07:00
|
|
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
# /model switch and other agent-mutating commands must reject while the
|
|
|
|
|
# session is running. agent.switch_model() mutates self.model, self.provider,
|
|
|
|
|
# self.base_url, self.client etc. in place — the worker thread running
|
|
|
|
|
# agent.run_conversation is reading those on every iteration. Same class of
|
|
|
|
|
# bug as the session.undo / session.compress mid-run silent-drop; same fix
|
|
|
|
|
# pattern: reject with 4009 while running.
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_config_set_model_rejects_while_running(monkeypatch):
|
|
|
|
|
"""/model via config.set must reject during an in-flight turn."""
|
|
|
|
|
seen = {"called": False}
|
|
|
|
|
|
|
|
|
|
def _fake_apply(sid, session, raw):
|
|
|
|
|
seen["called"] = True
|
|
|
|
|
return {"value": raw, "warning": ""}
|
|
|
|
|
|
|
|
|
|
monkeypatch.setattr(server, "_apply_model_switch", _fake_apply)
|
|
|
|
|
|
|
|
|
|
server._sessions["sid"] = _session(running=True)
|
|
|
|
|
try:
|
2026-04-22 15:19:50 -05:00
|
|
|
resp = server.handle_request(
|
|
|
|
|
{
|
|
|
|
|
"id": "1",
|
|
|
|
|
"method": "config.set",
|
|
|
|
|
"params": {
|
|
|
|
|
"session_id": "sid",
|
|
|
|
|
"key": "model",
|
|
|
|
|
"value": "anthropic/claude-sonnet-4.6",
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
)
|
2026-04-19 05:19:57 -07:00
|
|
|
assert resp.get("error")
|
|
|
|
|
assert resp["error"]["code"] == 4009
|
|
|
|
|
assert "session busy" in resp["error"]["message"]
|
|
|
|
|
assert not seen["called"], (
|
|
|
|
|
"_apply_model_switch was called mid-turn — would race with "
|
|
|
|
|
"the worker thread reading agent.model / agent.client"
|
|
|
|
|
)
|
|
|
|
|
finally:
|
|
|
|
|
server._sessions.pop("sid", None)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_config_set_model_allowed_when_idle(monkeypatch):
|
|
|
|
|
"""Regression guard: idle sessions can still switch models."""
|
|
|
|
|
seen = {"called": False}
|
|
|
|
|
|
|
|
|
|
def _fake_apply(sid, session, raw):
|
|
|
|
|
seen["called"] = True
|
|
|
|
|
return {"value": "newmodel", "warning": ""}
|
|
|
|
|
|
|
|
|
|
monkeypatch.setattr(server, "_apply_model_switch", _fake_apply)
|
|
|
|
|
|
|
|
|
|
server._sessions["sid"] = _session(running=False)
|
|
|
|
|
try:
|
2026-04-22 15:19:50 -05:00
|
|
|
resp = server.handle_request(
|
|
|
|
|
{
|
|
|
|
|
"id": "1",
|
|
|
|
|
"method": "config.set",
|
|
|
|
|
"params": {"session_id": "sid", "key": "model", "value": "newmodel"},
|
|
|
|
|
}
|
|
|
|
|
)
|
2026-04-19 05:19:57 -07:00
|
|
|
assert resp.get("result")
|
|
|
|
|
assert resp["result"]["value"] == "newmodel"
|
|
|
|
|
assert seen["called"]
|
|
|
|
|
finally:
|
|
|
|
|
server._sessions.pop("sid", None)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_mirror_slash_side_effects_rejects_mutating_commands_while_running(monkeypatch):
|
|
|
|
|
"""Slash worker passthrough (e.g. /model, /personality, /prompt,
|
|
|
|
|
/compress) must reject during an in-flight turn. Same race as
|
|
|
|
|
config.set — mutates live agent state while run_conversation is
|
|
|
|
|
reading it."""
|
|
|
|
|
import types
|
|
|
|
|
|
|
|
|
|
applied = {"model": False, "compress": False}
|
|
|
|
|
|
|
|
|
|
def _fake_apply_model(sid, session, arg):
|
|
|
|
|
applied["model"] = True
|
|
|
|
|
return {"value": arg, "warning": ""}
|
|
|
|
|
|
|
|
|
|
def _fake_compress(session, focus):
|
|
|
|
|
applied["compress"] = True
|
|
|
|
|
return (0, {})
|
|
|
|
|
|
|
|
|
|
monkeypatch.setattr(server, "_apply_model_switch", _fake_apply_model)
|
|
|
|
|
monkeypatch.setattr(server, "_compress_session_history", _fake_compress)
|
|
|
|
|
|
|
|
|
|
session = _session(running=True)
|
|
|
|
|
session["agent"] = types.SimpleNamespace(model="x")
|
|
|
|
|
|
|
|
|
|
for cmd, expected_name in [
|
|
|
|
|
("/model new/model", "model"),
|
|
|
|
|
("/personality default", "personality"),
|
|
|
|
|
("/prompt", "prompt"),
|
|
|
|
|
("/compress", "compress"),
|
|
|
|
|
]:
|
|
|
|
|
warning = server._mirror_slash_side_effects("sid", session, cmd)
|
2026-04-22 15:19:50 -05:00
|
|
|
assert (
|
|
|
|
|
"session busy" in warning
|
|
|
|
|
), f"{cmd} should have returned busy warning, got: {warning!r}"
|
2026-04-19 05:19:57 -07:00
|
|
|
assert f"/{expected_name}" in warning
|
|
|
|
|
|
|
|
|
|
# None of the mutating side-effect helpers should have fired.
|
|
|
|
|
assert not applied["model"], "model switch fired despite running session"
|
|
|
|
|
assert not applied["compress"], "compress fired despite running session"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_mirror_slash_side_effects_allowed_when_idle(monkeypatch):
|
|
|
|
|
"""Regression guard: idle session still runs the side effects."""
|
|
|
|
|
import types
|
|
|
|
|
|
|
|
|
|
applied = {"model": False}
|
|
|
|
|
|
|
|
|
|
def _fake_apply_model(sid, session, arg):
|
|
|
|
|
applied["model"] = True
|
|
|
|
|
return {"value": arg, "warning": ""}
|
|
|
|
|
|
|
|
|
|
monkeypatch.setattr(server, "_apply_model_switch", _fake_apply_model)
|
|
|
|
|
|
|
|
|
|
session = _session(running=False)
|
|
|
|
|
session["agent"] = types.SimpleNamespace(model="x")
|
|
|
|
|
|
|
|
|
|
warning = server._mirror_slash_side_effects("sid", session, "/model foo")
|
|
|
|
|
# Should NOT contain "session busy" — the switch went through.
|
|
|
|
|
assert "session busy" not in warning
|
|
|
|
|
assert applied["model"]
|
2026-04-19 05:35:45 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
# session.create / session.close race: fast /new churn must not orphan the
|
|
|
|
|
# slash_worker subprocess or the global approval-notify registration.
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_session_create_close_race_does_not_orphan_worker(monkeypatch):
|
|
|
|
|
"""Regression guard: if session.close runs while session.create's
|
|
|
|
|
_build thread is still constructing the agent, the build thread
|
|
|
|
|
must detect the orphan and clean up the slash_worker + notify
|
|
|
|
|
registration it's about to install. Without the cleanup those
|
|
|
|
|
resources leak — the subprocess stays alive until atexit and the
|
|
|
|
|
notify callback lingers in the global registry."""
|
|
|
|
|
import threading
|
|
|
|
|
|
|
|
|
|
closed_workers: list[str] = []
|
|
|
|
|
unregistered_keys: list[str] = []
|
|
|
|
|
|
|
|
|
|
class _FakeWorker:
|
|
|
|
|
def __init__(self, key, model):
|
|
|
|
|
self.key = key
|
|
|
|
|
self._closed = False
|
|
|
|
|
|
|
|
|
|
def close(self):
|
|
|
|
|
self._closed = True
|
|
|
|
|
closed_workers.append(self.key)
|
|
|
|
|
|
|
|
|
|
class _FakeAgent:
|
|
|
|
|
def __init__(self):
|
|
|
|
|
self.model = "x"
|
|
|
|
|
self.provider = "openrouter"
|
|
|
|
|
self.base_url = ""
|
|
|
|
|
self.api_key = ""
|
|
|
|
|
|
|
|
|
|
# Make _build block until we release it — simulates slow agent init
|
|
|
|
|
release_build = threading.Event()
|
|
|
|
|
|
|
|
|
|
def _slow_make_agent(sid, key):
|
|
|
|
|
release_build.wait(timeout=3.0)
|
|
|
|
|
return _FakeAgent()
|
|
|
|
|
|
|
|
|
|
# Stub everything _build touches
|
|
|
|
|
monkeypatch.setattr(server, "_make_agent", _slow_make_agent)
|
|
|
|
|
monkeypatch.setattr(server, "_SlashWorker", _FakeWorker)
|
2026-04-22 15:19:50 -05:00
|
|
|
monkeypatch.setattr(
|
|
|
|
|
server,
|
|
|
|
|
"_get_db",
|
|
|
|
|
lambda: types.SimpleNamespace(create_session=lambda *a, **kw: None),
|
|
|
|
|
)
|
2026-04-19 05:35:45 -07:00
|
|
|
monkeypatch.setattr(server, "_session_info", lambda _a: {"model": "x"})
|
|
|
|
|
monkeypatch.setattr(server, "_probe_credentials", lambda _a: None)
|
|
|
|
|
monkeypatch.setattr(server, "_wire_callbacks", lambda _sid: None)
|
|
|
|
|
monkeypatch.setattr(server, "_emit", lambda *a, **kw: None)
|
|
|
|
|
|
|
|
|
|
# Shim register/unregister to observe leaks
|
|
|
|
|
import tools.approval as _approval
|
2026-04-22 15:19:50 -05:00
|
|
|
|
|
|
|
|
monkeypatch.setattr(_approval, "register_gateway_notify", lambda key, cb: None)
|
|
|
|
|
monkeypatch.setattr(
|
|
|
|
|
_approval,
|
|
|
|
|
"unregister_gateway_notify",
|
|
|
|
|
lambda key: unregistered_keys.append(key),
|
|
|
|
|
)
|
2026-04-19 05:35:45 -07:00
|
|
|
monkeypatch.setattr(_approval, "load_permanent_allowlist", lambda: None)
|
|
|
|
|
|
|
|
|
|
# Start: session.create spawns _build thread, returns synchronously
|
2026-04-22 15:19:50 -05:00
|
|
|
resp = server.handle_request(
|
|
|
|
|
{
|
|
|
|
|
"id": "1",
|
|
|
|
|
"method": "session.create",
|
|
|
|
|
"params": {"cols": 80},
|
|
|
|
|
}
|
|
|
|
|
)
|
2026-04-19 05:35:45 -07:00
|
|
|
assert resp.get("result"), f"got error: {resp.get('error')}"
|
|
|
|
|
sid = resp["result"]["session_id"]
|
|
|
|
|
|
|
|
|
|
# Build thread is blocked in _slow_make_agent. Close the session
|
|
|
|
|
# NOW — this pops _sessions[sid] before _build can install the
|
|
|
|
|
# worker/notify.
|
2026-04-22 15:19:50 -05:00
|
|
|
close_resp = server.handle_request(
|
|
|
|
|
{
|
|
|
|
|
"id": "2",
|
|
|
|
|
"method": "session.close",
|
|
|
|
|
"params": {"session_id": sid},
|
|
|
|
|
}
|
|
|
|
|
)
|
2026-04-19 05:35:45 -07:00
|
|
|
assert close_resp.get("result", {}).get("closed") is True
|
|
|
|
|
|
|
|
|
|
# At this point session.close saw slash_worker=None (not yet
|
|
|
|
|
# installed) so it didn't close anything. Release the build thread
|
|
|
|
|
# and let it finish — it should detect the orphan and clean up the
|
|
|
|
|
# worker it just allocated + unregister the notify.
|
|
|
|
|
release_build.set()
|
|
|
|
|
|
|
|
|
|
# Give the build thread a moment to run through its finally.
|
|
|
|
|
for _ in range(100):
|
|
|
|
|
if closed_workers:
|
|
|
|
|
break
|
|
|
|
|
import time
|
2026-04-22 15:19:50 -05:00
|
|
|
|
2026-04-19 05:35:45 -07:00
|
|
|
time.sleep(0.02)
|
|
|
|
|
|
2026-04-22 15:19:50 -05:00
|
|
|
assert (
|
|
|
|
|
len(closed_workers) == 1
|
|
|
|
|
), f"orphan worker was not cleaned up — closed_workers={closed_workers}"
|
2026-04-19 05:35:45 -07:00
|
|
|
# Notify may be unregistered by both session.close (unconditional)
|
|
|
|
|
# and the orphan-cleanup path; the key guarantee is that the build
|
|
|
|
|
# thread does at least one unregister call (any prior close
|
|
|
|
|
# already popped the callback; the duplicate is a no-op).
|
|
|
|
|
assert len(unregistered_keys) >= 1, (
|
|
|
|
|
f"orphan notify registration was not unregistered — "
|
|
|
|
|
f"unregistered_keys={unregistered_keys}"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_session_create_no_race_keeps_worker_alive(monkeypatch):
|
|
|
|
|
"""Regression guard: when session.close does NOT race, the build
|
|
|
|
|
thread must install the worker + notify normally and leave them
|
|
|
|
|
alone (no over-eager cleanup)."""
|
|
|
|
|
closed_workers: list[str] = []
|
|
|
|
|
unregistered_keys: list[str] = []
|
|
|
|
|
|
|
|
|
|
class _FakeWorker:
|
|
|
|
|
def __init__(self, key, model):
|
|
|
|
|
self.key = key
|
|
|
|
|
|
|
|
|
|
def close(self):
|
|
|
|
|
closed_workers.append(self.key)
|
|
|
|
|
|
|
|
|
|
class _FakeAgent:
|
|
|
|
|
def __init__(self):
|
|
|
|
|
self.model = "x"
|
|
|
|
|
self.provider = "openrouter"
|
|
|
|
|
self.base_url = ""
|
|
|
|
|
self.api_key = ""
|
|
|
|
|
|
|
|
|
|
monkeypatch.setattr(server, "_make_agent", lambda sid, key: _FakeAgent())
|
|
|
|
|
monkeypatch.setattr(server, "_SlashWorker", _FakeWorker)
|
2026-04-22 15:19:50 -05:00
|
|
|
monkeypatch.setattr(
|
|
|
|
|
server,
|
|
|
|
|
"_get_db",
|
|
|
|
|
lambda: types.SimpleNamespace(create_session=lambda *a, **kw: None),
|
|
|
|
|
)
|
2026-04-19 05:35:45 -07:00
|
|
|
monkeypatch.setattr(server, "_session_info", lambda _a: {"model": "x"})
|
|
|
|
|
monkeypatch.setattr(server, "_probe_credentials", lambda _a: None)
|
|
|
|
|
monkeypatch.setattr(server, "_wire_callbacks", lambda _sid: None)
|
|
|
|
|
monkeypatch.setattr(server, "_emit", lambda *a, **kw: None)
|
|
|
|
|
|
|
|
|
|
import tools.approval as _approval
|
2026-04-22 15:19:50 -05:00
|
|
|
|
2026-04-19 05:35:45 -07:00
|
|
|
monkeypatch.setattr(_approval, "register_gateway_notify", lambda key, cb: None)
|
2026-04-22 15:19:50 -05:00
|
|
|
monkeypatch.setattr(
|
|
|
|
|
_approval,
|
|
|
|
|
"unregister_gateway_notify",
|
|
|
|
|
lambda key: unregistered_keys.append(key),
|
|
|
|
|
)
|
2026-04-19 05:35:45 -07:00
|
|
|
monkeypatch.setattr(_approval, "load_permanent_allowlist", lambda: None)
|
|
|
|
|
|
2026-04-22 15:19:50 -05:00
|
|
|
resp = server.handle_request(
|
|
|
|
|
{
|
|
|
|
|
"id": "1",
|
|
|
|
|
"method": "session.create",
|
|
|
|
|
"params": {"cols": 80},
|
|
|
|
|
}
|
|
|
|
|
)
|
2026-04-19 05:35:45 -07:00
|
|
|
sid = resp["result"]["session_id"]
|
|
|
|
|
|
|
|
|
|
# Wait for the build to finish (ready event inside session dict).
|
|
|
|
|
session = server._sessions[sid]
|
|
|
|
|
session["agent_ready"].wait(timeout=2.0)
|
|
|
|
|
|
|
|
|
|
# Build finished without a close race — nothing should have been
|
|
|
|
|
# cleaned up by the orphan check.
|
2026-04-22 15:19:50 -05:00
|
|
|
assert (
|
|
|
|
|
closed_workers == []
|
|
|
|
|
), f"build thread closed its own worker despite no race: {closed_workers}"
|
|
|
|
|
assert (
|
|
|
|
|
unregistered_keys == []
|
|
|
|
|
), f"build thread unregistered its own notify despite no race: {unregistered_keys}"
|
2026-04-19 05:35:45 -07:00
|
|
|
|
|
|
|
|
# Session should have the live worker installed.
|
|
|
|
|
assert session.get("slash_worker") is not None
|
|
|
|
|
|
|
|
|
|
# Cleanup
|
|
|
|
|
server._sessions.pop(sid, None)
|
2026-04-19 16:15:22 -07:00
|
|
|
|
|
|
|
|
|
2026-04-22 13:49:33 -06:00
|
|
|
def test_get_db_degrades_cleanly_when_sessiondb_init_fails(monkeypatch):
|
|
|
|
|
fake_mod = types.ModuleType("hermes_state")
|
|
|
|
|
|
|
|
|
|
class _BrokenSessionDB:
|
|
|
|
|
def __init__(self):
|
|
|
|
|
raise RuntimeError("locking protocol")
|
|
|
|
|
|
|
|
|
|
fake_mod.SessionDB = _BrokenSessionDB
|
|
|
|
|
monkeypatch.setitem(sys.modules, "hermes_state", fake_mod)
|
|
|
|
|
monkeypatch.setattr(server, "_db", None)
|
|
|
|
|
monkeypatch.setattr(server, "_db_error", None)
|
|
|
|
|
|
|
|
|
|
assert server._get_db() is None
|
|
|
|
|
assert server._db_error == "locking protocol"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_session_create_continues_when_state_db_is_unavailable(monkeypatch):
|
|
|
|
|
class _FakeWorker:
|
|
|
|
|
def __init__(self, key, model):
|
|
|
|
|
self.key = key
|
|
|
|
|
|
|
|
|
|
def close(self):
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
class _FakeAgent:
|
|
|
|
|
def __init__(self):
|
|
|
|
|
self.model = "x"
|
|
|
|
|
self.provider = "openrouter"
|
|
|
|
|
self.base_url = ""
|
|
|
|
|
self.api_key = ""
|
|
|
|
|
|
|
|
|
|
emits = []
|
|
|
|
|
|
|
|
|
|
monkeypatch.setattr(server, "_make_agent", lambda sid, key: _FakeAgent())
|
|
|
|
|
monkeypatch.setattr(server, "_SlashWorker", _FakeWorker)
|
|
|
|
|
monkeypatch.setattr(server, "_get_db", lambda: None)
|
|
|
|
|
monkeypatch.setattr(server, "_session_info", lambda _a: {"model": "x"})
|
|
|
|
|
monkeypatch.setattr(server, "_probe_credentials", lambda _a: None)
|
|
|
|
|
monkeypatch.setattr(server, "_wire_callbacks", lambda _sid: None)
|
|
|
|
|
monkeypatch.setattr(server, "_emit", lambda *a, **kw: emits.append(a))
|
|
|
|
|
|
|
|
|
|
import tools.approval as _approval
|
2026-04-27 11:49:02 -05:00
|
|
|
|
2026-04-22 13:49:33 -06:00
|
|
|
monkeypatch.setattr(_approval, "register_gateway_notify", lambda key, cb: None)
|
|
|
|
|
monkeypatch.setattr(_approval, "load_permanent_allowlist", lambda: None)
|
|
|
|
|
|
|
|
|
|
resp = server.handle_request(
|
|
|
|
|
{"id": "1", "method": "session.create", "params": {"cols": 80}}
|
|
|
|
|
)
|
|
|
|
|
sid = resp["result"]["session_id"]
|
|
|
|
|
session = server._sessions[sid]
|
|
|
|
|
session["agent_ready"].wait(timeout=2.0)
|
|
|
|
|
|
|
|
|
|
assert session["agent_error"] is None
|
|
|
|
|
assert session["agent"] is not None
|
|
|
|
|
assert not any(args and args[0] == "error" for args in emits)
|
|
|
|
|
|
|
|
|
|
server._sessions.pop(sid, None)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_session_list_returns_clean_error_when_state_db_is_unavailable(monkeypatch):
|
|
|
|
|
monkeypatch.setattr(server, "_get_db", lambda: None)
|
|
|
|
|
monkeypatch.setattr(server, "_db_error", "locking protocol")
|
|
|
|
|
|
|
|
|
|
resp = server.handle_request({"id": "1", "method": "session.list", "params": {}})
|
|
|
|
|
|
|
|
|
|
assert "error" in resp
|
|
|
|
|
assert "state.db unavailable: locking protocol" in resp["error"]["message"]
|
|
|
|
|
|
|
|
|
|
|
2026-04-19 16:15:22 -07:00
|
|
|
# --------------------------------------------------------------------------
|
|
|
|
|
# model.options — curated-list parity with `hermes model` and classic /model
|
|
|
|
|
# --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_model_options_does_not_overwrite_curated_models(monkeypatch):
|
|
|
|
|
"""The TUI model.options handler must surface the same curated model
|
|
|
|
|
list as `hermes model` and the classic CLI /model picker.
|
|
|
|
|
|
|
|
|
|
Regression: earlier versions of this handler unconditionally replaced
|
|
|
|
|
each provider's curated ``models`` field with ``provider_model_ids()``
|
|
|
|
|
(live /models catalog). That pulled in hundreds of non-agentic models
|
|
|
|
|
for providers like Nous whose /models endpoint returns image/video
|
|
|
|
|
generators, rerankers, embeddings, and TTS models alongside chat models.
|
|
|
|
|
"""
|
|
|
|
|
curated_providers = [
|
|
|
|
|
{
|
|
|
|
|
"slug": "nous",
|
|
|
|
|
"name": "Nous",
|
|
|
|
|
"models": ["moonshotai/kimi-k2.5", "anthropic/claude-opus-4.7"],
|
|
|
|
|
"total_models": 30,
|
|
|
|
|
"source": "built-in",
|
|
|
|
|
"is_current": False,
|
|
|
|
|
"is_user_defined": False,
|
|
|
|
|
},
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
monkeypatch.setattr(
|
|
|
|
|
server,
|
|
|
|
|
"_load_cfg",
|
|
|
|
|
lambda: {"providers": {}, "custom_providers": []},
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
with patch(
|
|
|
|
|
"hermes_cli.model_switch.list_authenticated_providers",
|
|
|
|
|
return_value=curated_providers,
|
|
|
|
|
) as listing:
|
|
|
|
|
# If provider_model_ids gets called at all, the handler is still
|
|
|
|
|
# overwriting curated with live — that's the regression we're
|
|
|
|
|
# guarding against.
|
|
|
|
|
with patch("hermes_cli.models.provider_model_ids") as live_fetch:
|
|
|
|
|
resp = server._methods["model.options"](99, {"session_id": ""})
|
|
|
|
|
|
|
|
|
|
assert "result" in resp, resp
|
|
|
|
|
providers = resp["result"]["providers"]
|
|
|
|
|
nous = next((p for p in providers if p.get("slug") == "nous"), None)
|
|
|
|
|
assert nous is not None
|
|
|
|
|
assert nous["models"] == [
|
|
|
|
|
"moonshotai/kimi-k2.5",
|
|
|
|
|
"anthropic/claude-opus-4.7",
|
|
|
|
|
]
|
|
|
|
|
assert nous["total_models"] == 30
|
|
|
|
|
# Handler must not consult the live catalog — curated is the truth.
|
|
|
|
|
live_fetch.assert_not_called()
|
|
|
|
|
# list_authenticated_providers is the single source.
|
|
|
|
|
assert listing.call_count == 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_model_options_propagates_list_exception(monkeypatch):
|
|
|
|
|
"""If list_authenticated_providers itself raises, surface as an RPC
|
|
|
|
|
error rather than swallowing to a blank picker."""
|
|
|
|
|
monkeypatch.setattr(
|
|
|
|
|
server,
|
|
|
|
|
"_load_cfg",
|
|
|
|
|
lambda: {"providers": {}, "custom_providers": []},
|
|
|
|
|
)
|
|
|
|
|
with patch(
|
|
|
|
|
"hermes_cli.model_switch.list_authenticated_providers",
|
|
|
|
|
side_effect=RuntimeError("catalog blew up"),
|
|
|
|
|
):
|
|
|
|
|
resp = server._methods["model.options"](77, {"session_id": ""})
|
|
|
|
|
assert "error" in resp
|
|
|
|
|
assert resp["error"]["code"] == 5033
|
|
|
|
|
assert "catalog blew up" in resp["error"]["message"]
|
2026-04-26 10:44:22 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
# prompt.submit — auto-title
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
2026-04-27 11:49:02 -05:00
|
|
|
|
2026-04-26 10:44:22 -07:00
|
|
|
class _ImmediateThread:
|
|
|
|
|
"""Runs the target callable synchronously so assertions can follow."""
|
|
|
|
|
|
|
|
|
|
def __init__(self, target=None, daemon=None):
|
|
|
|
|
self._target = target
|
|
|
|
|
|
|
|
|
|
def start(self):
|
|
|
|
|
self._target()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_prompt_submit_auto_titles_session_on_complete(monkeypatch):
|
|
|
|
|
"""maybe_auto_title is called after a successful (complete) prompt."""
|
|
|
|
|
|
|
|
|
|
class _Agent:
|
2026-04-27 11:49:02 -05:00
|
|
|
def run_conversation(
|
|
|
|
|
self, prompt, conversation_history=None, stream_callback=None
|
|
|
|
|
):
|
2026-04-26 10:44:22 -07:00
|
|
|
return {
|
|
|
|
|
"final_response": "Rome was founded in 753 BC.",
|
|
|
|
|
"messages": [
|
|
|
|
|
{"role": "user", "content": "Tell me about Rome"},
|
|
|
|
|
{"role": "assistant", "content": "Rome was founded in 753 BC."},
|
|
|
|
|
],
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
server._sessions["sid"] = _session(agent=_Agent())
|
|
|
|
|
monkeypatch.setattr(server.threading, "Thread", _ImmediateThread)
|
|
|
|
|
monkeypatch.setattr(server, "_emit", lambda *args, **kwargs: None)
|
|
|
|
|
monkeypatch.setattr(server, "make_stream_renderer", lambda cols: None)
|
|
|
|
|
monkeypatch.setattr(server, "render_message", lambda raw, cols: None)
|
|
|
|
|
monkeypatch.setattr(server, "_get_db", lambda: None)
|
|
|
|
|
|
|
|
|
|
with patch("agent.title_generator.maybe_auto_title") as mock_title:
|
|
|
|
|
server.handle_request(
|
|
|
|
|
{
|
|
|
|
|
"id": "1",
|
|
|
|
|
"method": "prompt.submit",
|
|
|
|
|
"params": {"session_id": "sid", "text": "Tell me about Rome"},
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
mock_title.assert_called_once()
|
|
|
|
|
args = mock_title.call_args.args
|
|
|
|
|
assert args[1] == "session-key"
|
|
|
|
|
assert args[2] == "Tell me about Rome"
|
|
|
|
|
assert args[3] == "Rome was founded in 753 BC."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_prompt_submit_skips_auto_title_when_interrupted(monkeypatch):
|
|
|
|
|
"""maybe_auto_title must NOT be called when the agent was interrupted."""
|
|
|
|
|
|
|
|
|
|
class _Agent:
|
2026-04-27 11:49:02 -05:00
|
|
|
def run_conversation(
|
|
|
|
|
self, prompt, conversation_history=None, stream_callback=None
|
|
|
|
|
):
|
2026-04-26 10:44:22 -07:00
|
|
|
return {
|
|
|
|
|
"final_response": "partial answer",
|
|
|
|
|
"interrupted": True,
|
|
|
|
|
"messages": [],
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
server._sessions["sid"] = _session(agent=_Agent())
|
|
|
|
|
monkeypatch.setattr(server.threading, "Thread", _ImmediateThread)
|
|
|
|
|
monkeypatch.setattr(server, "_emit", lambda *args, **kwargs: None)
|
|
|
|
|
monkeypatch.setattr(server, "make_stream_renderer", lambda cols: None)
|
|
|
|
|
monkeypatch.setattr(server, "render_message", lambda raw, cols: None)
|
|
|
|
|
monkeypatch.setattr(server, "_get_db", lambda: None)
|
|
|
|
|
|
|
|
|
|
with patch("agent.title_generator.maybe_auto_title") as mock_title:
|
|
|
|
|
server.handle_request(
|
|
|
|
|
{
|
|
|
|
|
"id": "1",
|
|
|
|
|
"method": "prompt.submit",
|
|
|
|
|
"params": {"session_id": "sid", "text": "Tell me about Rome"},
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
mock_title.assert_not_called()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_prompt_submit_skips_auto_title_when_response_empty(monkeypatch):
|
|
|
|
|
"""maybe_auto_title must NOT be called when the agent returns an empty reply."""
|
|
|
|
|
|
|
|
|
|
class _Agent:
|
2026-04-27 11:49:02 -05:00
|
|
|
def run_conversation(
|
|
|
|
|
self, prompt, conversation_history=None, stream_callback=None
|
|
|
|
|
):
|
2026-04-26 10:44:22 -07:00
|
|
|
return {
|
|
|
|
|
"final_response": "",
|
|
|
|
|
"messages": [],
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
server._sessions["sid"] = _session(agent=_Agent())
|
|
|
|
|
monkeypatch.setattr(server.threading, "Thread", _ImmediateThread)
|
|
|
|
|
monkeypatch.setattr(server, "_emit", lambda *args, **kwargs: None)
|
|
|
|
|
monkeypatch.setattr(server, "make_stream_renderer", lambda cols: None)
|
|
|
|
|
monkeypatch.setattr(server, "render_message", lambda raw, cols: None)
|
|
|
|
|
monkeypatch.setattr(server, "_get_db", lambda: None)
|
|
|
|
|
|
|
|
|
|
with patch("agent.title_generator.maybe_auto_title") as mock_title:
|
|
|
|
|
server.handle_request(
|
|
|
|
|
{
|
|
|
|
|
"id": "1",
|
|
|
|
|
"method": "prompt.submit",
|
|
|
|
|
"params": {"session_id": "sid", "text": "Tell me about Rome"},
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
mock_title.assert_not_called()
|