Your lightweight open-source personal agent.
Python agent-frameworkai-agentai-agentschatbotchatopsdiscord-botllm-agentsllmslocal-llmmcpmodel-context-protocolmulti-agentopenai-compatibleopenclawpersonal-ai-assistantpythonself-hostedtelegram-bot-ai-assistantwebuiworkflow-automation
fix(restart): send completion notice after channel is ready and unify runtime keys
imfondofba7c07ccf2e8
diff --git a/nanobot/cli/commands.py b/nanobot/cli/commands.pyindex d611c2772..b1e4f056a 100644--- a/nanobot/cli/commands.py+++ b/nanobot/cli/commands.py@@ -206,6 +206,57 @@ def _is_exit_command(command: str) -> bool:return command.lower() in EXIT_COMMANDS+def _parse_cli_session(session_id: str) -> tuple[str, str]:+ """Split session id into (channel, chat_id)."""+ if ":" in session_id:+ return session_id.split(":", 1)+ return "cli", session_id+++def _should_show_cli_restart_notice(+ restart_notify_channel: str,+ restart_notify_chat_id: str,+ session_id: str,+) -> bool:+ """Return True when CLI should display restart-complete notice."""+ _, cli_chat_id = _parse_cli_session(session_id)+ return restart_notify_channel == "cli" and (+ not restart_notify_chat_id or restart_notify_chat_id == cli_chat_id+ )+++async def _notify_restart_done_when_channel_ready(+ *,+ bus,+ channels,+ channel: str,+ chat_id: str,+ timeout_s: float = 30.0,+ poll_s: float = 0.25,+) -> bool:+ """Wait for target channel readiness, then publish restart completion."""+ from nanobot.bus.events import OutboundMessage++ if not channel or not chat_id:+ return False+ if channel not in channels.enabled_channels:+ return False++ waited = 0.0+ while waited <= timeout_s:+ target = channels.get_channel(channel)+ if target and target.is_running:+ await bus.publish_outbound(OutboundMessage(+ channel=channel,+ chat_id=chat_id,+ content="Restart completed.",+ ))+ return True+ await asyncio.sleep(poll_s)+ waited += poll_s+ return False++async def _read_interactive_input_async() -> str:"""Read user input using prompt_toolkit (handles paste, history, display).@@ -598,6 +649,7 @@ def gateway(from nanobot.agent.loop import AgentLoopfrom nanobot.bus.queue import MessageBusfrom nanobot.channels.manager import ChannelManager+ from nanobot.config.runtime_keys import RESTART_NOTIFY_CHANNEL_ENV, RESTART_NOTIFY_CHAT_ID_ENVfrom nanobot.cron.service import CronServicefrom nanobot.cron.types import CronJobfrom nanobot.heartbeat.service import HeartbeatService@@ -696,6 +748,8 @@ def gateway(# Create channel managerchannels = ChannelManager(config, bus)+ restart_notify_channel = os.environ.pop(RESTART_NOTIFY_CHANNEL_ENV, "").strip()+ restart_notify_chat_id = os.environ.pop(RESTART_NOTIFY_CHAT_ID_ENV, "").strip()def _pick_heartbeat_target() -> tuple[str, str]:"""Pick a routable channel/chat target for heartbeat-triggered messages."""@@ -772,6 +826,13 @@ def gateway(try:await cron.start()await heartbeat.start()+ if restart_notify_channel and restart_notify_chat_id:+ asyncio.create_task(_notify_restart_done_when_channel_ready(+ bus=bus,+ channels=channels,+ channel=restart_notify_channel,+ chat_id=restart_notify_chat_id,+ ))await asyncio.gather(agent.run(),channels.start_all(),@@ -813,6 +874,7 @@ def agent(from nanobot.agent.loop import AgentLoopfrom nanobot.bus.queue import MessageBus+ from nanobot.config.runtime_keys import RESTART_NOTIFY_CHANNEL_ENV, RESTART_NOTIFY_CHAT_ID_ENVfrom nanobot.cron.service import CronServiceconfig = _load_runtime_config(config, workspace)@@ -853,6 +915,13 @@ def agent(channels_config=config.channels,timezone=config.agents.defaults.timezone,)+ restart_notify_channel = os.environ.pop(RESTART_NOTIFY_CHANNEL_ENV, "").strip()+ restart_notify_chat_id = os.environ.pop(RESTART_NOTIFY_CHAT_ID_ENV, "").strip()++ cli_channel, cli_chat_id = _parse_cli_session(session_id)++ if _should_show_cli_restart_notice(restart_notify_channel, restart_notify_chat_id, session_id):+ _print_agent_response("Restart completed.", render_markdown=False)# Shared reference for progress callbacks_thinking: ThinkingSpinner | None = None@@ -891,11 +960,6 @@ def agent(_init_prompt_session()console.print(f"{__logo__} Interactive mode (type [bold]exit[/bold] or [bold]Ctrl+C[/bold] to quit)\n")- if ":" in session_id:- cli_channel, cli_chat_id = session_id.split(":", 1)- else:- cli_channel, cli_chat_id = "cli", session_id-def _handle_signal(signum, frame):sig_name = signal.Signals(signum).name_restore_terminal()diff --git a/nanobot/command/builtin.py b/nanobot/command/builtin.pyindex 05d4fc163..f63a1e357 100644--- a/nanobot/command/builtin.py+++ b/nanobot/command/builtin.py@@ -9,6 +9,7 @@ import sysfrom nanobot import __version__from nanobot.bus.events import OutboundMessagefrom nanobot.command.router import CommandContext, CommandRouter+from nanobot.config.runtime_keys import RESTART_NOTIFY_CHANNEL_ENV, RESTART_NOTIFY_CHAT_ID_ENVfrom nanobot.utils.helpers import build_status_content@@ -35,6 +36,8 @@ async def cmd_stop(ctx: CommandContext) -> OutboundMessage:async def cmd_restart(ctx: CommandContext) -> OutboundMessage:"""Restart the process in-place via os.execv."""msg = ctx.msg+ os.environ[RESTART_NOTIFY_CHANNEL_ENV] = msg.channel+ os.environ[RESTART_NOTIFY_CHAT_ID_ENV] = msg.chat_idasync def _do_restart():await asyncio.sleep(1)diff --git a/nanobot/config/runtime_keys.py b/nanobot/config/runtime_keys.pynew file mode 100644index 000000000..2dc6c9234--- /dev/null+++ b/nanobot/config/runtime_keys.py@@ -0,0 +1,4 @@+"""Runtime environment variable keys shared across components."""++RESTART_NOTIFY_CHANNEL_ENV = "NANOBOT_RESTART_NOTIFY_CHANNEL"+RESTART_NOTIFY_CHAT_ID_ENV = "NANOBOT_RESTART_NOTIFY_CHAT_ID"diff --git a/tests/cli/test_restart_command.py b/tests/cli/test_restart_command.pyindex 6efcdad0d..16b3aaa48 100644--- a/tests/cli/test_restart_command.py+++ b/tests/cli/test_restart_command.py@@ -3,7 +3,9 @@from __future__ import annotationsimport asyncio+import osimport time+from types import SimpleNamespacefrom unittest.mock import AsyncMock, MagicMock, patchimport pytest@@ -35,15 +37,19 @@ class TestRestartCommand:@pytest.mark.asyncioasync def test_restart_sends_message_and_calls_execv(self):from nanobot.command.builtin import cmd_restart+ from nanobot.config.runtime_keys import RESTART_NOTIFY_CHANNEL_ENV, RESTART_NOTIFY_CHAT_ID_ENVfrom nanobot.command.router import CommandContextloop, bus = _make_loop()msg = InboundMessage(channel="cli", sender_id="user", chat_id="direct", content="/restart")ctx = CommandContext(msg=msg, session=None, key=msg.session_key, raw="/restart", loop=loop)- with patch("nanobot.command.builtin.os.execv") as mock_execv:+ with patch.dict(os.environ, {}, clear=False), \+ patch("nanobot.command.builtin.os.execv") as mock_execv:out = await cmd_restart(ctx)assert "Restarting" in out.content+ assert os.environ.get(RESTART_NOTIFY_CHANNEL_ENV) == "cli"+ assert os.environ.get(RESTART_NOTIFY_CHAT_ID_ENV) == "direct"await asyncio.sleep(1.5)mock_execv.assert_called_once()@@ -190,3 +196,76 @@ class TestRestartCommand:assert response is not Noneassert response.metadata == {"render_as": "text"}+++async def test_notify_restart_done_waits_until_channel_running() -> None:+ from nanobot.bus.queue import MessageBus+ from nanobot.cli.commands import _notify_restart_done_when_channel_ready++ bus = MessageBus()+ channel = SimpleNamespace(is_running=False)++ class DummyChannels:+ enabled_channels = ["feishu"]++ @staticmethod+ def get_channel(name: str):+ return channel if name == "feishu" else None++ async def _mark_running() -> None:+ await asyncio.sleep(0.02)+ channel.is_running = True++ marker = asyncio.create_task(_mark_running())+ sent = await _notify_restart_done_when_channel_ready(+ bus=bus,+ channels=DummyChannels(),+ channel="feishu",+ chat_id="oc_123",+ timeout_s=0.2,+ poll_s=0.01,+ )+ await marker++ assert sent is True+ out = await asyncio.wait_for(bus.consume_outbound(), timeout=0.1)+ assert out.channel == "feishu"+ assert out.chat_id == "oc_123"+ assert out.content == "Restart completed."+++async def test_notify_restart_done_times_out_when_channel_not_running() -> None:+ from nanobot.bus.queue import MessageBus+ from nanobot.cli.commands import _notify_restart_done_when_channel_ready++ bus = MessageBus()+ channel = SimpleNamespace(is_running=False)++ class DummyChannels:+ enabled_channels = ["feishu"]++ @staticmethod+ def get_channel(name: str):+ return channel if name == "feishu" else None++ sent = await _notify_restart_done_when_channel_ready(+ bus=bus,+ channels=DummyChannels(),+ channel="feishu",+ chat_id="oc_123",+ timeout_s=0.05,+ poll_s=0.01,+ )+ assert sent is False+ assert bus.outbound_size == 0+++def test_should_show_cli_restart_notice() -> None:+ from nanobot.cli.commands import _should_show_cli_restart_notice++ assert _should_show_cli_restart_notice("cli", "direct", "cli:direct") is True+ assert _should_show_cli_restart_notice("cli", "", "cli:direct") is True+ assert _should_show_cli_restart_notice("cli", "other", "cli:direct") is False+ assert _should_show_cli_restart_notice("feishu", "oc_123", "cli:direct") is False