diff --git a/backend/main.py b/backend/main.py index 586754b..5418003 100644 --- a/backend/main.py +++ b/backend/main.py @@ -1458,6 +1458,46 @@ async def _wait_for_agent_loop_ready( return False +async def _record_agent_loop_ready_warning( + bot_id: str, + timeout_seconds: float = 12.0, + poll_interval_seconds: float = 0.5, +) -> None: + try: + agent_loop_ready = await _wait_for_agent_loop_ready( + bot_id, + timeout_seconds=timeout_seconds, + poll_interval_seconds=poll_interval_seconds, + ) + if agent_loop_ready: + return + if docker_manager.get_bot_status(bot_id) != "RUNNING": + return + detail = ( + "Bot container started, but ready marker was not found in logs within " + f"{int(timeout_seconds)}s. Check bot logs or MCP config if the bot stays unavailable." + ) + logger.warning("bot_id=%s agent loop ready marker not found within %ss", bot_id, timeout_seconds) + with Session(engine) as background_session: + if not background_session.get(BotInstance, bot_id): + return + record_activity_event( + background_session, + bot_id, + "bot_warning", + channel="system", + detail=detail, + metadata={ + "kind": "agent_loop_ready_timeout", + "marker": _AGENT_LOOP_READY_MARKER, + "timeout_seconds": timeout_seconds, + }, + ) + background_session.commit() + _invalidate_bot_detail_cache(bot_id) + except Exception: + logger.exception("Failed to record agent loop readiness warning for bot_id=%s", bot_id) + def _sync_workspace_channels( session: Session, bot_id: str, @@ -2668,20 +2708,7 @@ async def start_bot(bot_id: str, session: Session = Depends(get_session)): status_code=500, detail="Bot container failed shortly after startup. Check bot logs/config.", ) - agent_loop_ready = await _wait_for_agent_loop_ready(bot_id) - if not agent_loop_ready: - docker_manager.stop_bot(bot_id) - bot.docker_status = "STOPPED" - if str(bot.current_state or "").upper() not in {"ERROR"}: - bot.current_state = "IDLE" - bot.updated_at = datetime.utcnow() - session.add(bot) - session.commit() - _invalidate_bot_detail_cache(bot_id) - raise HTTPException( - status_code=500, - detail="Bot启动异常:容器已启动,但 Agent loop 未正常就绪,请检查 Bot 日志或 MCP 配置。", - ) + asyncio.create_task(_record_agent_loop_ready_warning(bot_id)) session.add(bot) record_activity_event(session, bot_id, "bot_started", channel="system", detail=f"Container started for {bot_id}") session.commit() diff --git a/backend/services/platform_service.py b/backend/services/platform_service.py index 38ed15b..1b28806 100644 --- a/backend/services/platform_service.py +++ b/backend/services/platform_service.py @@ -48,6 +48,7 @@ OPERATIONAL_ACTIVITY_EVENT_TYPES = { "bot_created", "bot_started", "bot_stopped", + "bot_warning", "bot_enabled", "bot_disabled", "bot_deactivated",