refactor(cli): replace channel module globals with ChannelRuntime (#197)

* refactor(cli): replace channel module globals with ChannelRuntime

Removes _cli_agent / _cli_thread_id from EvoScientist/cli/channel.py
and threads a ChannelRuntime via CommandContext.channel_runtime so
/model and /channel rebind without poking module-level state.

* fix(cli): address coderabbit review

- _auto_start_channel: bind ChannelRuntime only after
  _start_channels_bus_mode succeeds, so a startup failure no longer
  leaves a stale binding pointing at channels that never started.
- _sync_tui_command_completion (TUI) and the Rich CLI command-completion
  paths: rebind the runtime on thread rotation, not just agent swap, so
  /new and /resume keep ChannelRuntime in sync with the running thread
  (matches the serve-mode hook contract).
- test_hook_syncs_channel_runtime: pin ctx.thread_id explicitly so a
  bare MagicMock attribute can't silently mutate runtime.thread_id.
- New regression test covering the rebind-on-thread-rotation contract.
This commit is contained in:
dinos
2026-04-30 19:06:22 +02:00
committed by GitHub
parent 7cfec02416
commit 5c829942d7
12 changed files with 228 additions and 156 deletions
+32 -5
View File
@@ -4,7 +4,7 @@ from types import SimpleNamespace
import pytest
from EvoScientist.commands.base import CommandContext
from EvoScientist.commands.base import ChannelRuntime, CommandContext
from tests.conftest import run_async as _run
pytest.importorskip("textual")
@@ -13,15 +13,18 @@ pytest.importorskip("textual")
class _Loader:
def __init__(self) -> None:
self.adopt_calls: list[object] = []
self.agent: object | None = None
def adopt(self, agent: object) -> None:
self.adopt_calls.append(agent)
self.agent = agent
class _StubApp:
def __init__(self) -> None:
self._agent_loader = _Loader()
self._conversation_tid = "thread-1"
self._channel_runtime = ChannelRuntime()
self.model_updates: list[tuple[str, str | None]] = []
self.refresh_calls: list[bool] = []
@@ -58,16 +61,15 @@ def test_sync_tui_command_completion_adopts_agent_swap(monkeypatch):
lambda: SimpleNamespace(model="gpt-5.5", provider="openai"),
)
monkeypatch.setattr(tui_mod, "_channels_is_running", lambda: True)
monkeypatch.setattr(tui_mod._ch_mod, "_cli_agent", "old-agent", raising=False)
monkeypatch.setattr(tui_mod._ch_mod, "_cli_thread_id", "old-thread", raising=False)
app._channel_runtime.bind("old-agent", "old-thread")
_run(tui_mod._sync_tui_command_completion(app, ctx, "old-agent", cmd))
assert app._agent_loader.adopt_calls == ["new-agent"]
assert app.model_updates == [("gpt-5.5", "openai")]
assert app.refresh_calls == [True]
assert tui_mod._ch_mod._cli_agent == "new-agent"
assert tui_mod._ch_mod._cli_thread_id == "thread-1"
assert app._channel_runtime.agent == "new-agent"
assert app._channel_runtime.thread_id == "thread-1"
def test_sync_tui_command_completion_refreshes_without_agent_swap(monkeypatch):
@@ -88,3 +90,28 @@ def test_sync_tui_command_completion_refreshes_without_agent_swap(monkeypatch):
assert app._agent_loader.adopt_calls == []
assert app.model_updates == []
assert app.refresh_calls == [True]
def test_sync_tui_rebinds_runtime_on_thread_rotation_without_agent_swap(monkeypatch):
"""Regression: ``/new`` and ``/resume`` rotate ``app._conversation_tid``
without swapping the agent. The runtime must still pick up the new
thread id so the bus contract stays consistent with serve mode."""
import EvoScientist.cli.tui_interactive as tui_mod
app = _StubApp()
app._conversation_tid = "rotated-thread"
app._agent_loader.agent = "same-agent"
app._channel_runtime.bind("same-agent", "old-thread")
ctx = CommandContext(
agent="same-agent",
thread_id="rotated-thread",
ui=SimpleNamespace(),
)
cmd = SimpleNamespace(name="/new")
monkeypatch.setattr(tui_mod, "_channels_is_running", lambda: True)
_run(tui_mod._sync_tui_command_completion(app, ctx, "same-agent", cmd))
assert app._channel_runtime.agent == "same-agent"
assert app._channel_runtime.thread_id == "rotated-thread"