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
test: cover archived heartbeat target selection
Xubin Renc915e98c1555
diff --git a/nanobot/cli/commands.py b/nanobot/cli/commands.pyindex d11b5b676..2332fe2e4 100644--- a/nanobot/cli/commands.py+++ b/nanobot/cli/commands.py@@ -5,7 +5,7 @@ import osimport selectimport signalimport sys-from collections.abc import Callable+from collections.abc import Callable, Iterablefrom contextlib import nullcontext, suppressfrom pathlib import Pathfrom typing import Any@@ -61,6 +61,7 @@ from nanobot.utils.restart import ( # noqa: E402format_restart_completed_message,should_show_cli_restart_notice,)+from nanobot.webui.sidebar_state import read_webui_sidebar_state # noqa: E402def _sanitize_surrogates(text: str) -> str:@@ -210,6 +211,29 @@ def _heartbeat_has_active_tasks(content: str) -> bool:return Truereturn False++def _pick_heartbeat_target_from_sessions(+ *,+ enabled_channels: Iterable[str],+ sessions: Iterable[dict[str, Any]],+ archived_keys: Iterable[str],+) -> tuple[str, str]:+ enabled = set(enabled_channels)+ archived = set(archived_keys)+ for item in sessions:+ key = item.get("key") or ""+ if key in archived:+ continue+ if ":" not in key:+ continue+ channel, chat_id = key.split(":", 1)+ if channel in {"cli", "system"}:+ continue+ if channel in enabled and chat_id:+ return channel, chat_id+ return "cli", "direct"++# ---------------------------------------------------------------------------# CLI input: prompt_toolkit for editing, paste, history, and display# ---------------------------------------------------------------------------@@ -1064,24 +1088,12 @@ def _run_gateway(def _pick_heartbeat_target() -> tuple[str, str]:"""Pick a routable channel/chat target for heartbeat-triggered messages."""- enabled = set(channels.enabled_channels)-- from nanobot.webui.sidebar_state import read_webui_sidebar_statesidebar_state = read_webui_sidebar_state()- archived_keys = set(sidebar_state.get("archived_keys", []))-- for item in session_manager.list_sessions():- key = item.get("key") or ""- if key in archived_keys:- continue- if ":" not in key:- continue- channel, chat_id = key.split(":", 1)- if channel in {"cli", "system"}:- continue- if channel in enabled and chat_id:- return channel, chat_id- return "cli", "direct"+ return _pick_heartbeat_target_from_sessions(+ enabled_channels=channels.enabled_channels,+ sessions=session_manager.list_sessions(),+ archived_keys=sidebar_state.get("archived_keys", []),+ )if channels.enabled_channels:console.print(f"[green]✓[/green] Channels enabled: {', '.join(channels.enabled_channels)}")diff --git a/nanobot/session/manager.py b/nanobot/session/manager.pyindex da62f0f0d..331ccf959 100644--- a/nanobot/session/manager.py+++ b/nanobot/session/manager.py@@ -843,7 +843,6 @@ class SessionManager:if not fallback_preview and item.get("role") == "assistant":fallback_preview = textpreview = preview or fallback_preview- from datetime import datetimefallback_time = datetime.fromtimestamp(path.stat().st_mtime).isoformat()sessions.append({diff --git a/nanobot/webui/session_list_index.py b/nanobot/webui/session_list_index.pyindex fdd645755..8269500c8 100644--- a/nanobot/webui/session_list_index.py+++ b/nanobot/webui/session_list_index.py@@ -272,7 +272,6 @@ def _scan_session_row(session_manager: SessionManager, path: Path) -> dict[str,created_at_s = data.get("created_at")updated_at_s = data.get("updated_at")if not created_at_s or not updated_at_s:- from datetime import datetimefallback_time = datetime.fromtimestamp(signature["mtime_ns"] / 1e9).isoformat()created_at_s = created_at_s or fallback_timeupdated_at_s = updated_at_s or fallback_timediff --git a/tests/cli/test_commands.py b/tests/cli/test_commands.pyindex aeb67e8b5..3a6ad2864 100644--- a/tests/cli/test_commands.py+++ b/tests/cli/test_commands.py@@ -1247,6 +1247,21 @@ def test_heartbeat_skips_bundled_template():assert _heartbeat_has_active_tasks(load_bundled_template("HEARTBEAT.md")) is False+def test_heartbeat_target_skips_archived_webui_sessions():+ from nanobot.cli.commands import _pick_heartbeat_target_from_sessions++ target = _pick_heartbeat_target_from_sessions(+ enabled_channels=["websocket"],+ archived_keys=["websocket:archived"],+ sessions=[+ {"key": "websocket:archived"},+ {"key": "websocket:active"},+ ],+ )++ assert target == ("websocket", "active")++def _write_instance_config(tmp_path: Path) -> Path:config_file = tmp_path / "instance" / "config.json"config_file.parent.mkdir(parents=True)