Skip to content

"Vibe-Trading: Your Personal Trading Agent"

feat(channels): explain degraded hot-apply resets to users

shadowinlifed71b28c743f7
diff --git a/agent/src/api/channels_config_routes.py b/agent/src/api/channels_config_routes.py
index b974c3eb3..f48566aeb 100644
--- a/agent/src/api/channels_config_routes.py
+++ b/agent/src/api/channels_config_routes.py
@@ -21,6 +21,7 @@ from __future__ import annotations
import asyncio
import logging
+import re
import sys as _sys
from collections import defaultdict
from pathlib import Path
@@ -31,6 +32,7 @@ from pydantic import BaseModel, ConfigDict, Field, ValidationError
from src.api import state as _state
from src.channels import config as _channels_config
+from src.channels.bus.events import OutboundMessage
from src.channels.bus.queue import MessageBus
from src.channels.config_meta import (
channel_field_hints,
@@ -54,6 +56,10 @@ _VERIFY_BLOCK_CODES = frozenset({"invalid_credentials", "network"})
# Bound the fallback stop so a wedged adapter cannot stall the reset + restart.
_FALLBACK_STOP_TIMEOUT_S = 10.0
+# Sanitized reset_reason bounds: exception text may embed credentials in URLs.
+_RESET_REASON_MAX_LEN = 200
+_URL_USERINFO_RE = re.compile(r"(\w[\w+.-]*://)[^\s/@]+@")
+
# Per-channel lifecycle keys the manager consumes from the section; they are
# not adapter model fields but are legitimate config content (#341 lineage).
_MANAGER_OVERRIDE_KEYS = frozenset(
@@ -318,31 +324,68 @@ def _build_ephemeral(name: str, section: dict[str, Any]) -> tuple[type, Any]:
raise _reject_validation([type(exc).__name__]) from None
-async def _hot_apply(name: str, section: dict[str, Any] | None) -> str:
- """Apply a written section to the cached runtime; return the apply mode.
+def format_reset_notice() -> str:
+ """Return the fixed IM reset-notice text (plain English, ``format_pairing_reply`` style)."""
+ return "Channel configuration updated; connections were reset."
+
+
+def _sanitize_reset_reason(exc: BaseException) -> str:
+ """Return a bounded, credential-free one-line reason for a degraded reset (#1625)."""
+ return _URL_USERINFO_RE.sub(r"\1", f"{type(exc).__name__}: {exc}")[:_RESET_REASON_MAX_LEN]
+
+
+async def _publish_reset_notice(runtime: Any, name: str, chat_id: str) -> None:
+ """Best-effort reset notice on the fresh runtime's bus (#1625).
+
+ A bus publish is a queue put, so the HTTP response never waits on the
+ adapter; the dispatcher's normal retry applies, and a notice lost while
+ the adapter reconnects is accepted (nanobot's /restart notice has the
+ same property across execv).
+ """
+ bus = getattr(runtime, "bus", None)
+ if bus is None:
+ return
+ try:
+ await bus.publish_outbound(OutboundMessage(
+ channel=name, chat_id=chat_id,
+ content=format_reset_notice(), metadata={"_config_notice": True},
+ ))
+ except Exception: # noqa: BLE001 - a failed notice must not fail the PUT
+ logger.warning("Reset notice for %s was not published", name, exc_info=True)
+
+
+async def _hot_apply(name: str, section: dict[str, Any] | None) -> tuple[str, str | None]:
+ """Apply a written section to the cached runtime; return ``(applied, reason)``.
Running runtime → per-channel hot swap; built-but-stopped → drop the stale
singleton so the next explicit Start reads fresh config; never built →
no-op (the lazy build reads disk). A failed swap falls back to a full
- reset + restart through the same path ``/channels/start`` uses.
+ reset + restart through the same path ``/channels/start`` uses; that
+ degraded path returns a sanitized ``reset_reason`` and pushes a
+ best-effort notice to the channel's last chat, so a disruption that was
+ previously log-only becomes an explained one (#1625).
"""
host = _host()
runtime = getattr(host, "_channel_runtime", None)
if runtime is None:
- return "deferred"
+ return "deferred", None
if not runtime.status().get("running"):
async with _runtime_lock:
_state.reset_channel_runtime()
- return "reset"
+ return "reset", None
try:
await runtime.manager.reload_channel(name, section)
- return "hot_swapped"
- except Exception: # noqa: BLE001 - a failed swap must not strand the runtime
+ return "hot_swapped", None
+ except Exception as exc: # noqa: BLE001 - a failed swap must not strand the runtime
logger.warning(
"Hot reload failed for channel %s; falling back to a runtime reset",
name,
exc_info=True,
)
+ # The reset destroys the old manager, so capture the notify target —
+ # the last chat we sent to — before its state is gone.
+ get_last = getattr(runtime.manager, "last_chat_id", None)
+ last_chat_id = get_last(name) if callable(get_last) else None
async with _runtime_lock:
try:
await asyncio.wait_for(runtime.stop(), timeout=_FALLBACK_STOP_TIMEOUT_S)
@@ -352,8 +395,11 @@ async def _hot_apply(name: str, section: dict[str, Any] | None) -> str:
exc_info=True,
)
_state.reset_channel_runtime()
- await host._get_channel_runtime().start(start_manager=True)
- return "reset"
+ fresh = host._get_channel_runtime()
+ await fresh.start(start_manager=True)
+ if last_chat_id is not None:
+ await _publish_reset_notice(fresh, name, last_chat_id)
+ return "reset", _sanitize_reset_reason(exc)
async def _apply_update(name: str, payload: ChannelConfigUpdateRequest) -> dict[str, Any]:
@@ -362,7 +408,10 @@ async def _apply_update(name: str, payload: ChannelConfigUpdateRequest) -> dict[
The response ``applied`` field reports how the edit reached the runtime:
``refreshed`` (a noop-key edit applied in place, no reconnect),
``hot_swapped`` (adapter stop+start), ``reset`` (full runtime rebuild), or
- ``deferred`` (no runtime yet; the next start reads disk).
+ ``deferred`` (no runtime yet; the next start reads disk). A ``reset`` that
+ degraded from a failed hot swap additionally carries ``reset_reason`` — a
+ sanitized one-line explanation (bounded, URL userinfo stripped); the key
+ is absent on every other path.
"""
stored = _stored_section(name)
patch = _normalize_nullable_blanks(name, _patch_of(name, payload.config))
@@ -422,8 +471,11 @@ async def _apply_update(name: str, payload: ChannelConfigUpdateRequest) -> dict[
and await runtime.manager.refresh_channel_config(name, fresh)
):
return {"channel": _fresh_entry(name), "applied": "refreshed"}
- applied = await _hot_apply(name, fresh or None)
- return {"channel": _fresh_entry(name), "applied": applied}
+ applied, reset_reason = await _hot_apply(name, fresh or None)
+ response: dict[str, Any] = {"channel": _fresh_entry(name), "applied": applied}
+ if reset_reason is not None:
+ response["reset_reason"] = reset_reason
+ return response
async def _run_test(name: str, body: dict[str, Any] | None, clears: list[str]) -> dict[str, Any]:
diff --git a/agent/src/channels/manager.py b/agent/src/channels/manager.py
index cf1f937bd..239b531fd 100644
--- a/agent/src/channels/manager.py
+++ b/agent/src/channels/manager.py
@@ -68,6 +68,9 @@ class ChannelManager:
# would each stop the adapter they saw and start their own.
self._reload_locks: defaultdict[str, asyncio.Lock] = defaultdict(asyncio.Lock)
self._origin_reply_fingerprints: dict[tuple[str, str, str], str] = {}
+ # Last outbound chat per channel: where a degraded config reset sends
+ # its user-visible notice (#1625). One entry per channel — bounded.
+ self._last_chat_ids: dict[str, str] = {}
self._status: dict[str, dict[str, Any]] = {}
self._init_channels()
@@ -467,6 +470,11 @@ class ChannelManager:
msg.channel, msg.chat_id,
)
continue
+ # Record the chat right before delivery: a chat we just
+ # sent to is a chat a reset notice can reach (#1625). Only
+ # this main path records — the reasoning-routing branch
+ # above carries transient fragments, not conversations.
+ self._last_chat_ids[msg.channel] = msg.chat_id
await self._send_with_retry(channel, msg)
else:
logger.warning("Unknown channel: %s", msg.channel)
@@ -586,6 +594,10 @@ class ChannelManager:
"""Get a channel by name."""
return self.channels.get(name)
+ def last_chat_id(self, name: str) -> str | None:
+ """Return the most recent outbound chat id for *name* (None if never sent)."""
+ return self._last_chat_ids.get(name)
+
def get_status(self) -> dict[str, Any]:
"""Get status of all channels."""
status = {name: dict(item) for name, item in self._status.items()}
diff --git a/agent/tests/test_channel_reset_notice.py b/agent/tests/test_channel_reset_notice.py
new file mode 100644
index 000000000..f1694ebde
--- /dev/null
+++ b/agent/tests/test_channel_reset_notice.py
@@ -0,0 +1,288 @@
+"""Contracts for the user-visible notice when a hot apply degrades (F3, #1625).
+
+A PUT whose ``reload_channel`` fails degrades to a full runtime reset: every
+connection drops. The route now explains the disruption — the response carries
+a sanitized ``reset_reason`` and the changed channel's last chat receives one
+best-effort ``OutboundMessage`` (``_config_notice``) published on the FRESH
+runtime's bus after the restart.
+
+Duplicate-suppression interaction (verified against manager.py):
+``ChannelManager._should_suppress_outbound`` builds its fingerprint key from
+``metadata["message_id"]`` and returns ``False`` early when that key is
+absent — a reset notice carries only ``_config_notice``, so it can never be
+suppressed as a duplicate reply.
+"""
+
+from __future__ import annotations
+
+import asyncio
+import json
+from pathlib import Path
+from typing import Any
+
+import pytest
+from fastapi.testclient import TestClient
+
+import api_server
+import src.channels.manager as manager_module
+from src.api import channels_config_routes as routes
+from src.channels.base import BaseChannel
+from src.channels.bus.events import OutboundMessage
+from src.channels.bus.queue import MessageBus
+from src.channels.manager import ChannelManager
+
+CLIENT_ID = "ding-client-id-1234567890"
+STORED_SECRET = "stored-secret-abcdefghij"
+LAST_CHAT = "chat-42"
+
+
+class _FakeAdapter(BaseChannel):
+ """Concrete adapter stand-in; sends are no-ops, no platform SDK is touched."""
+
+ name = "dingtalk"
+ display_name = "DingTalk"
+
+ def __init__(self, config: Any, bus: MessageBus, **kwargs: Any) -> None:
+ del kwargs
+ super().__init__(config, bus)
+
+ async def start(self) -> None:
+ self._running = True
+
+ async def stop(self) -> None:
+ self._running = False
+
+ async def send(self, msg: Any) -> None:
+ del msg
+
+
+def _fake_inspect_channels(config: Any) -> dict[str, dict[str, Any]]:
+ statuses: dict[str, dict[str, Any]] = {}
+ if not isinstance(config, dict):
+ return statuses
+ for name, section in config.items():
+ if not isinstance(section, dict):
+ continue
+ statuses[name] = {
+ "name": name,
+ "available": True,
+ "display_name": name.title(),
+ "install_hint": "",
+ "error": "",
+ "configured": True,
+ "enabled": bool(section.get("enabled")),
+ "loaded": False,
+ "running": False,
+ }
+ return statuses
+
+
+def _seeded_manager(
+ monkeypatch: pytest.MonkeyPatch, *, last_chat: str | None
+) -> ChannelManager:
+ """Build a real manager and record a last chat through the real dispatcher.
+
+ Driving an actual outbound message through ``_dispatch_outbound`` is what
+ populates ``_last_chat_ids`` in production, so the seeding doubles as
+ coverage of the dispatch hook. The dispatcher is stopped before return;
+ only the recorded state is consumed by the route.
+ """
+ monkeypatch.setattr(manager_module, "discover_channel_names", lambda: ["dingtalk"])
+ monkeypatch.setattr(manager_module, "load_channel_class", lambda name: _FakeAdapter)
+ monkeypatch.setattr(manager_module, "inspect_channels", _fake_inspect_channels)
+
+ async def setup() -> ChannelManager:
+ manager = ChannelManager({"dingtalk": {"enabled": True}}, MessageBus())
+ await manager.start_all()
+ if last_chat is not None:
+ await manager.bus.publish_outbound(
+ OutboundMessage(
+ channel="dingtalk", chat_id=last_chat, content="earlier reply"
+ )
+ )
+ for _ in range(200):
+ if manager.last_chat_id("dingtalk") is not None:
+ break
+ await asyncio.sleep(0.01)
+ await manager.stop_all()
+ return manager
+
+ return asyncio.run(setup())
+
+
+class _FakeRuntime:
+ """Stands in for ChannelRuntime: running flag + manager + bus."""
+
+ def __init__(self, *, running: bool, manager: Any = None) -> None:
+ self._running = running
+ self.manager = manager
+ self.bus = MessageBus()
+ self.start_calls: list[bool] = []
+ self.stop_calls: list[bool] = []
+
+ def status(self) -> dict[str, Any]:
+ return {"running": self._running, "channels": {}}
+
+ async def start(self, *, start_manager: bool = True) -> None:
+ self.start_calls.append(start_manager)
+
+ async def stop(self) -> None:
+ self.stop_calls.append(True)
+ self._running = False
+
+
+def _dingtalk_section() -> dict[str, Any]:
+ return {
+ "enabled": True,
+ "client_id": CLIENT_ID,
+ "client_secret": STORED_SECRET,
+ "allow_from": ["*"],
+ }
+
+
+def _arrange(
+ tmp_path: Path,
+ monkeypatch: pytest.MonkeyPatch,
+ *,
+ last_chat: str | None,
+ fail_reload: bool,
+) -> tuple[TestClient, _FakeRuntime, _FakeRuntime]:
+ """Wire a seeded old runtime + a fresh rebuild target into the test app."""
+ manager = _seeded_manager(monkeypatch, last_chat=last_chat)
+ if fail_reload:
+ async def boom(name: str, section: dict | None) -> dict[str, Any]:
+ raise RuntimeError("reload boom via https://user:[email protected]")
+
+ manager.reload_channel = boom # type: ignore[method-assign]
+ old_runtime = _FakeRuntime(running=True, manager=manager)
+ new_runtime = _FakeRuntime(running=False)
+
+ (tmp_path / "agent.json").write_text(
+ json.dumps({"channels": {"dingtalk": _dingtalk_section()}}), encoding="utf-8"
+ )
+ monkeypatch.setenv("VIBE_TRADING_HOME", str(tmp_path))
+ monkeypatch.setattr(api_server, "_channel_runtime", old_runtime)
+ monkeypatch.setattr(api_server, "_channel_bus", None)
+ monkeypatch.setattr(api_server, "_channel_manager", None)
+ monkeypatch.setattr(api_server, "_get_session_service", lambda: object())
+
+ def _stub_get_channel_runtime() -> _FakeRuntime:
+ # Mirror the real accessor's caching contract: the rebuilt singleton
+ # becomes observable on the host module.
+ monkeypatch.setattr(api_server, "_channel_runtime", new_runtime)
+ return new_runtime
+
+ monkeypatch.setattr(api_server, "_get_channel_runtime", _stub_get_channel_runtime)
+ client = TestClient(api_server.app, client=("127.0.0.1", 50000))
+ return client, old_runtime, new_runtime
+
+
[email protected](autouse=True)
+def _fresh_locks():
+ """Give every test unbound locks (asyncio.Lock binds to a loop on contention)."""
+ routes._channel_locks.clear()
+ routes._runtime_lock = asyncio.Lock()
+ yield
+ routes._channel_locks.clear()
+ routes._runtime_lock = asyncio.Lock()
+
+
+def _put_rotated_id(client: TestClient) -> dict[str, Any]:
+ """PUT a connection-key edit (never noop) and return the JSON response."""
+ response = client.put(
+ "/channels/config/dingtalk", json={"config": {"client_id": "rotated-id"}}
+ )
+ assert response.status_code == 200
+ return response.json()
+
+
+def test_degraded_reset_publishes_one_notice_to_last_chat(
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ """Planbook test 1: reload failure → reset + reason + exactly one notice.
+
+ The notice is exempt from duplicate suppression: it carries no inbound
+ ``message_id``, so ``_should_suppress_outbound`` returns False early.
+ """
+ client, old_runtime, new_runtime = _arrange(
+ tmp_path, monkeypatch, last_chat=LAST_CHAT, fail_reload=True
+ )
+
+ body = _put_rotated_id(client)
+
+ assert body["applied"] == "reset"
+ assert "RuntimeError" in body["reset_reason"]
+ assert old_runtime.stop_calls == [True]
+ assert new_runtime.start_calls == [True]
+ assert new_runtime.bus.outbound.qsize() == 1
+ notice = new_runtime.bus.outbound.get_nowait()
+ assert (notice.channel, notice.chat_id) == ("dingtalk", LAST_CHAT)
+ assert notice.content == routes.format_reset_notice()
+ assert notice.metadata == {"_config_notice": True}
+
+
+def test_degraded_reset_without_prior_outbound_publishes_no_notice(
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ """Planbook test 2: no recorded last chat → the reset still happens, silently."""
+ client, _, new_runtime = _arrange(
+ tmp_path, monkeypatch, last_chat=None, fail_reload=True
+ )
+
+ body = _put_rotated_id(client)
+
+ assert body["applied"] == "reset"
+ assert "reload boom" in body["reset_reason"]
+ assert new_runtime.start_calls == [True]
+ assert new_runtime.bus.outbound.qsize() == 0
+
+
+def test_successful_hot_swap_publishes_no_notice(
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ """Planbook test 3: a clean per-channel swap is not disruptive → no notice."""
+ client, _, new_runtime = _arrange(
+ tmp_path, monkeypatch, last_chat=LAST_CHAT, fail_reload=False
+ )
+
+ body = _put_rotated_id(client)
+
+ assert body["applied"] == "hot_swapped"
+ assert "reset_reason" not in body
+ assert new_runtime.start_calls == []
+ assert new_runtime.bus.outbound.qsize() == 0
+
+
+def test_reset_reason_scrubs_userinfo_and_truncates() -> None:
+ """Planbook test 4: credentials stripped, length bounded (≤200 chars)."""
+ reason = routes._sanitize_reset_reason(
+ RuntimeError("connect failed via https://user:[email protected]:8443/v1")
+ )
+ assert reason.startswith("RuntimeError:")
+ assert "secret" not in reason
+ assert "user:" not in reason
+ assert "api.example.com:8443/v1" in reason
+
+ long = routes._sanitize_reset_reason(RuntimeError("x" * 500))
+ assert len(long) <= 200
+
+
+def test_notice_content_never_echoes_config_values(
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ """Planbook test 5: the notice is a fixed string — no config value appears."""
+ client, _, new_runtime = _arrange(
+ tmp_path, monkeypatch, last_chat=LAST_CHAT, fail_reload=True
+ )
+
+ body = _put_rotated_id(client)
+
+ assert body["applied"] == "reset"
+ notice = new_runtime.bus.outbound.get_nowait()
+ assert notice.content == routes.format_reset_notice()
+ section = dict(_dingtalk_section(), client_id="rotated-id")
+ for value in section.values():
+ items = value if isinstance(value, list) else [value]
+ for item in items:
+ if isinstance(item, str) and item:
+ assert item not in notice.content