From a3c90c64ca2fb7694dee8e935266caa56206fb32 Mon Sep 17 00:00:00 2001 From: jxxghp Date: Wed, 22 Jul 2026 08:15:19 +0800 Subject: [PATCH] refactor: remove unused result handling and related test for empty task results --- app/agent/__init__.py | 15 ++----------- tests/test_agent_scheduled_tasks.py | 34 ----------------------------- 2 files changed, 2 insertions(+), 47 deletions(-) diff --git a/app/agent/__init__.py b/app/agent/__init__.py index b6ec4ebeb..3d03af773 100644 --- a/app/agent/__init__.py +++ b/app/agent/__init__.py @@ -2145,7 +2145,7 @@ class AgentManager: f"也不要重复创建同一个定时任务。\n\n" f"任务名称:{task.name}\n" f"任务内容:{task.content}\n\n" - "完成后请直接向用户报告本次执行结果;如果无法完成,请说明原因。" + "完成后请直接向用户发送消息报告本次执行结果;如果无法完成,也需发送消息说明原因。" ) success = True result = "" @@ -2164,20 +2164,9 @@ class AgentManager: wait_for_completion=True, ) result_text = str(result or "").strip() - success = bool(result_text) and not result_text.startswith( + success = not result_text.startswith( (AGENT_EXECUTION_ERROR_PREFIX, "处理消息时发生错误") ) - if not result_text: - result = "定时任务已执行,但 Agent 未返回结果" - await AgentChain().async_post_message( - Notification( - mtype=NotificationType.Agent, - username=notification_username, - title=f"定时任务:{task.name}", - text=result, - save_history=False, - ) - ) except Exception as err: success = False result = f"Agent 定时任务执行失败:{str(err)}" diff --git a/tests/test_agent_scheduled_tasks.py b/tests/test_agent_scheduled_tasks.py index eb87a9392..1a12d7e0a 100644 --- a/tests/test_agent_scheduled_tasks.py +++ b/tests/test_agent_scheduled_tasks.py @@ -591,40 +591,6 @@ async def test_agent_manager_runs_contextless_task_in_broadcast_mode( post_message.assert_not_awaited() -@pytest.mark.anyio -async def test_agent_manager_broadcasts_empty_task_result(monkeypatch) -> None: - """Agent 未返回内容时,调度器应广播一次兜底消息。""" - user_id = f"empty-{uuid4().hex}" - task = AgentTaskOper().add( - name="空结果检查", - content="执行检查", - trigger_type="cron", - cron_expression="0 * * * *", - run_at=None, - user_id=user_id, - username="admin", - session_id=f"session-{user_id}", - channel="Telegram", - source="telegram-test", - original_chat_id="chat-123", - ) - manager = AgentManager() - manager.process_message = AsyncMock(return_value="") - post_message = AsyncMock() - monkeypatch.setattr(AgentChain, "async_post_message", post_message) - - success, result = await manager.execute_scheduled_task(task.id) - - assert success is False - assert result == "定时任务已执行,但 Agent 未返回结果" - notification = post_message.await_args.args[0] - assert notification.channel is None - assert notification.source is None - assert notification.userid is None - assert notification.original_chat_id is None - assert notification.username == "admin" - - @pytest.mark.anyio async def test_cached_agent_clears_channel_for_background_task() -> None: """复用会话 Agent 时,后台任务必须覆盖上一轮保留的渠道信息。"""