Files
beaver_project/app-instance/backend/tests/unit/test_session_archive.py
steven_li 4b0bf65ace ```
feat(engine): 优化智能体循环中的助手消息处理逻辑

- 在没有工具调用时才添加助手消息到上下文
- 确保工具调用响应正确添加到消息上下文中
- 修复了消息构建的条件逻辑

fix(cron): 改进定时任务调度的时间解析功能

- 添加正则表达式导入用于时间显示解析
- 实现从显示文本中提取毫秒间隔的功能
- 增强整数转换的安全性,避免类型错误
- 优化定时任务配置的解析逻辑

feat(outlook): 增强Outlook集成的功能和稳定性

- 将默认超时时间从10秒增加到180秒
- 为状态检查函数添加可选的验证参数
- 串行执行邮件概览获取操作而非并行
- 改进连接状态验证逻辑

feat(channel): 添加设备名称作为会话标识的选项

- 为终端WebSocket适配器添加新的配置选项
- 实现基于设备名称生成会话对等ID的功能
- 记录原始对等ID和设备名称的元数据
- 支持从设备名称创建会话对等ID

feat(skills): 完善技能学习评估系统和进度跟踪

- 在应用启动时自动调度待评估的技能草稿
- 为技能评估工作创建独立的循环工厂
- 实现异步技能评估任务的取消和清理机制
- 添加技能评估进度报告和状态跟踪功能
- 扩展会话列表API以包含更多详细信息
- 防止对不存在的会话进行操作
- 优化技能草稿提交和评估的业务逻辑

perf(skills): 提升技能评估的并发性能

- 实现并行技能案例评估以提高效率
- 添加最大并行案例数的环境变量控制
- 实现实时评估进度更新和回调机制
- 优化评估过程中的资源管理和同步

refactor(services): 创建隔离的智能体循环实例

- 添加创建独立智能体循环的工厂方法
- 确保新循环继承运行时服务配置
- 支持技能评估等需要隔离环境的场景
```
2026-06-15 14:48:16 +08:00

134 lines
5.0 KiB
Python

from __future__ import annotations
from pathlib import Path
from fastapi.testclient import TestClient
from beaver.engine.session import SessionManager
from beaver.interfaces.web.app import create_app
from beaver.services.agent_service import AgentService
def test_archived_sessions_can_be_hidden_from_default_web_list(tmp_path: Path) -> None:
manager = SessionManager(tmp_path)
manager.ensure_session("web:keep", source="web")
manager.ensure_session("web:archived", source="web")
manager.end_session("web:archived", "archived")
visible = manager.list_sessions_rich(exclude_end_reasons=["archived"])
visible_ids = {row["id"] for row in visible}
assert "web:keep" in visible_ids
assert "web:archived" not in visible_ids
assert manager.get_session("web:archived")["end_reason"] == "archived"
def test_archived_sessions_remain_available_to_history_search(tmp_path: Path) -> None:
manager = SessionManager(tmp_path)
manager.ensure_session("web:archived", source="web")
manager.end_session("web:archived", "archived")
all_sessions = manager.list_sessions_rich()
assert {row["id"] for row in all_sessions} == {"web:archived"}
def test_visible_history_excludes_error_and_incomplete_runs(tmp_path: Path) -> None:
manager = SessionManager(tmp_path)
manager.ensure_session("web:history", source="web")
manager.append_message("web:history", run_id="ok-run", role="user", content="hello")
manager.append_message("web:history", run_id="ok-run", role="assistant", content="hi", finish_reason="stop")
manager.append_message(
"web:history",
run_id="ok-run",
role="assistant",
content=None,
tool_calls=[{"id": "call-1", "type": "function", "function": {"name": "echo", "arguments": "{}"}}],
)
manager.append_message(
"web:history",
run_id="ok-run",
role="tool",
content="tool result",
tool_call_id="call-1",
)
manager.append_message(
"web:history",
run_id="ok-run",
role="system",
event_type="run_completed",
content="hi",
context_visible=False,
)
manager.append_message("web:history", run_id="error-run", role="user", content="bad")
manager.append_message(
"web:history",
run_id="error-run",
role="assistant",
content="Error: provider failed",
finish_reason="error",
)
manager.append_message(
"web:history",
run_id="error-run",
role="system",
event_type="run_completed",
content="Error: provider failed",
finish_reason="error",
context_visible=False,
)
manager.append_message("web:history", run_id="pending-run", role="user", content="pending")
history = manager.get_visible_history("web:history")
assert [(message["role"], message["content"]) for message in history] == [
("user", "hello"),
("assistant", "hi"),
]
def test_web_archive_route_does_not_create_archive_suffix_session(tmp_path: Path) -> None:
service = AgentService(workspace=tmp_path)
app = create_app(service=service, manage_service_lifecycle=False)
with TestClient(app) as client:
create_response = client.post("/api/sessions/web:alpha")
archive_response = client.post("/api/sessions/web:alpha/archive")
sessions_response = client.get("/api/sessions")
assert create_response.status_code == 200
assert archive_response.status_code == 200
assert archive_response.json() == {"ok": True, "archived": True}
assert sessions_response.status_code == 200
loaded = service.create_loop().boot()
assert loaded.session_manager.get_session("web:alpha")["end_reason"] == "archived" # type: ignore[union-attr]
assert loaded.session_manager.get_session("web:alpha/archive") is None # type: ignore[union-attr]
assert sessions_response.json() == []
def test_web_session_list_hides_skill_replay_evaluation_sessions(tmp_path: Path) -> None:
service = AgentService(workspace=tmp_path)
loaded = service.create_loop().boot()
loaded.session_manager.ensure_session("eval-session", source="skill_replay_eval") # type: ignore[union-attr]
loaded.session_manager.ensure_session("web:visible", source="web") # type: ignore[union-attr]
app = create_app(service=service, manage_service_lifecycle=False)
with TestClient(app) as client:
response = client.get("/api/sessions")
assert response.status_code == 200
assert [item["key"] for item in response.json()] == ["web:visible"]
def test_get_missing_session_returns_404_without_creating_it(tmp_path: Path) -> None:
service = AgentService(workspace=tmp_path)
app = create_app(service=service, manage_service_lifecycle=False)
with TestClient(app) as client:
response = client.get("/api/sessions/missing-session")
assert response.status_code == 404
loaded = service.create_loop().boot()
assert loaded.session_manager.get_session("missing-session") is None # type: ignore[union-attr]