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
Merge origin/main into feat/health-endpoint
Xubin Ren4999e2f7341a
diff --cc README.mdindex 828b56477,f593e26ec..a5ddf1368--- a/README.md+++ b/README.md@@@ -1508,6 -1739,6 +1739,12 @@@ nanobot gateway --config ~/.nanobot-telnanobot gateway --config ~/.nanobot-discord/config.json```++Each gateway instance also exposes a lightweight HTTP status endpoint on++`gateway.host:gateway.port`:++++- `GET /` returns `nanobot`++- `GET /health` returns JSON with service metadata, uptime, and enabled channels++Override workspace for one-off runs when needed:```bashdiff --cc nanobot/cli/commands.pyindex 9ddb46d74,48400bbd9..1f3f00c85--- a/nanobot/cli/commands.py+++ b/nanobot/cli/commands.py@@@ -674,66 -821,20 +821,80 @@@ def gatewayconsole.print(f"[green]✓[/green] Heartbeat: every {hb_cfg.interval_s}s")+ async def _health_server(host: str, health_port: int):+ """Lightweight HTTP health endpoint on the gateway port."""+ import json as _json+ import time++ start_time = time.monotonic()++ async def handle(reader, writer):+ try:+ data = await asyncio.wait_for(reader.read(4096), timeout=5)+ except (asyncio.TimeoutError, ConnectionError):+ writer.close()+ return++ request_line = data.split(b"\r\n", 1)[0].decode("utf-8", errors="replace")+ method, path = "", ""+ parts = request_line.split(" ")+ if len(parts) >= 2:+ method, path = parts[0], parts[1]++ if method == "GET" and path == "/health":+ uptime_s = int(time.monotonic() - start_time)+ body = _json.dumps({+ "service": "nanobot",+ "version": __version__,+ "status": "running",+ "uptime_seconds": uptime_s,+ "channels": channels.enabled_channels,+ })+ resp = (+ f"HTTP/1.0 200 OK\r\n"+ f"Content-Type: application/json\r\n"+ f"Content-Length: {len(body)}\r\n"+ f"\r\n{body}"+ )+ elif method == "GET" and path == "/":+ body = "nanobot"+ resp = (+ f"HTTP/1.0 200 OK\r\n"+ f"Content-Type: text/plain\r\n"+ f"Content-Length: {len(body)}\r\n"+ f"\r\n{body}"+ )+ else:+ body = "Not Found"+ resp = (+ f"HTTP/1.0 404 Not Found\r\n"+ f"Content-Type: text/plain\r\n"+ f"Content-Length: {len(body)}\r\n"+ f"\r\n{body}"+ )++ writer.write(resp.encode())+ await writer.drain()+ writer.close()++ server = await asyncio.start_server(handle, host, health_port)+ console.print(f"[green]✓[/green] Health endpoint: http://{host}:{health_port}/health")+ async with server:+ await server.serve_forever()+ # Register Dream system job (always-on, idempotent on restart)+ dream_cfg = config.agents.defaults.dream+ if dream_cfg.model_override:+ agent.dream.model = dream_cfg.model_override+ agent.dream.max_batch_size = dream_cfg.max_batch_size+ agent.dream.max_iterations = dream_cfg.max_iterations+ from nanobot.cron.types import CronJob, CronPayload+ cron.register_system_job(CronJob(+ id="dream",+ name="dream",+ schedule=dream_cfg.build_schedule(config.agents.defaults.timezone),+ payload=CronPayload(kind="system_event"),+ ))+ console.print(f"[green]✓[/green] Dream: {dream_cfg.describe_schedule()}")async def run():try:diff --cc tests/cli/test_commands.pyindex a8fcc4aa0,3a1e7145a..1ae2ffd87--- a/tests/cli/test_commands.py+++ b/tests/cli/test_commands.py@@@ -899,6 -1126,63 +1126,214 @@@ def test_gateway_cli_port_overrides_conassert "port 18792" in result.stdout++def test_gateway_health_endpoint_binds_and_serves_expected_responses(++ monkeypatch, tmp_path: Path++) -> None:++ config_file = _write_instance_config(tmp_path)++ config = Config()++ config.gateway.host = "127.0.0.9"++ config.gateway.port = 18791++ captured: dict[str, object] = {}++++ class _FakeDream:++ model = None++ max_batch_size = 0++ max_iterations = 0++++ async def run(self) -> None:++ return None++++ class _FakeAgentLoop:++ def __init__(self, **_kwargs) -> None:++ self.model = "test-model"++ self.dream = _FakeDream()++++ async def run(self) -> None:++ await asyncio.Event().wait()++++ async def close_mcp(self) -> None:++ return None++++ def stop(self) -> None:++ return None++++ class _FakeChannelManager:++ def __init__(self, _config, _bus) -> None:++ self.enabled_channels = ["telegram", "discord"]++++ async def start_all(self) -> None:++ await asyncio.Event().wait()++++ async def stop_all(self) -> None:++ return None++++ class _FakeCronService:++ def __init__(self, _store_path: Path) -> None:++ self.on_job = None++++ async def start(self) -> None:++ return None++++ def stop(self) -> None:++ return None++++ def status(self) -> dict[str, int]:++ return {"jobs": 0}++++ def register_system_job(self, _job) -> None:++ return None++++ class _FakeHeartbeatService:++ def __init__(self, **_kwargs) -> None:++ return None++++ async def start(self) -> None:++ return None++++ def stop(self) -> None:++ return None++++ class _FakeServer:++ async def __aenter__(self):++ return self++++ async def __aexit__(self, exc_type, exc, tb) -> bool:++ return False++++ async def serve_forever(self) -> None:++ raise _StopGatewayError("stop")++++ async def _fake_start_server(handler, host: str, port: int):++ captured["handler"] = handler++ captured["host"] = host++ captured["port"] = port++ return _FakeServer()++++ class _FakeReader:++ def __init__(self, payload: bytes) -> None:++ self.payload = payload++++ async def read(self, _size: int) -> bytes:++ return self.payload++++ class _FakeWriter:++ def __init__(self) -> None:++ self.output = b""++ self.closed = False++++ def write(self, data: bytes) -> None:++ self.output += data++++ async def drain(self) -> None:++ return None++++ def close(self) -> None:++ self.closed = True++++ _patch_cli_command_runtime(++ monkeypatch,++ config,++ message_bus=lambda: object(),++ session_manager=lambda _workspace: object(),++ )++ monkeypatch.setattr("nanobot.agent.loop.AgentLoop", _FakeAgentLoop)++ monkeypatch.setattr("nanobot.channels.manager.ChannelManager", _FakeChannelManager)++ monkeypatch.setattr("nanobot.cron.service.CronService", _FakeCronService)++ monkeypatch.setattr("nanobot.heartbeat.service.HeartbeatService", _FakeHeartbeatService)++ monkeypatch.setattr("asyncio.start_server", _fake_start_server)++++ result = runner.invoke(app, ["gateway", "--config", str(config_file)])++++ assert result.exit_code == 0++ assert captured["host"] == "127.0.0.9"++ assert captured["port"] == 18791++ assert "Health endpoint: http://127.0.0.9:18791/health" in result.stdout++++ def _call_handler(path: str) -> tuple[str, _FakeWriter]:++ request = f"GET {path} HTTP/1.1\r\nHost: localhost\r\n\r\n".encode()++ writer = _FakeWriter()++ handler = captured["handler"]++ assert callable(handler)++ asyncio.run(handler(_FakeReader(request), writer))++ return writer.output.decode(), writer++++ root_response, root_writer = _call_handler("/")++ assert root_writer.closed is True++ assert "HTTP/1.0 200 OK" in root_response++ assert root_response.endswith("\r\n\r\nnanobot")++++ health_response, health_writer = _call_handler("/health")++ assert health_writer.closed is True++ assert "HTTP/1.0 200 OK" in health_response++ health_body = json.loads(health_response.split("\r\n\r\n", 1)[1])++ assert health_body["service"] == "nanobot"++ assert health_body["status"] == "running"++ assert health_body["channels"] == ["telegram", "discord"]++ assert health_body["uptime_seconds"] >= 0++++ missing_response, missing_writer = _call_handler("/missing")++ assert missing_writer.closed is True++ assert "HTTP/1.0 404 Not Found" in missing_response++ assert missing_response.endswith("\r\n\r\nNot Found")+++++ def test_serve_uses_api_config_defaults_and_workspace_override(+ monkeypatch, tmp_path: Path+ ) -> None:+ config_file = _write_instance_config(tmp_path)+ config = Config()+ config.agents.defaults.workspace = str(tmp_path / "config-workspace")+ config.api.host = "127.0.0.2"+ config.api.port = 18900+ config.api.timeout = 45.0+ override_workspace = tmp_path / "override-workspace"+ seen: dict[str, object] = {}++ _patch_serve_runtime(monkeypatch, config, seen)++ result = runner.invoke(+ app,+ ["serve", "--config", str(config_file), "--workspace", str(override_workspace)],+ )++ assert result.exit_code == 0+ assert seen["workspace"] == override_workspace+ assert seen["host"] == "127.0.0.2"+ assert seen["port"] == 18900+ assert seen["request_timeout"] == 45.0+++ def test_serve_cli_options_override_api_config(monkeypatch, tmp_path: Path) -> None:+ config_file = _write_instance_config(tmp_path)+ config = Config()+ config.api.host = "127.0.0.2"+ config.api.port = 18900+ config.api.timeout = 45.0+ seen: dict[str, object] = {}++ _patch_serve_runtime(monkeypatch, config, seen)++ result = runner.invoke(+ app,+ [+ "serve",+ "--config",+ str(config_file),+ "--host",+ "127.0.0.1",+ "--port",+ "18901",+ "--timeout",+ "46",+ ],+ )++ assert result.exit_code == 0+ assert seen["host"] == "127.0.0.1"+ assert seen["port"] == 18901+ assert seen["request_timeout"] == 46.0++def test_channels_login_requires_channel_name() -> None:result = runner.invoke(app, ["channels", "login"])