feat: improve skill uploads and agent runtime stability
This commit is contained in:
1 parent
0980eea975
commit
1b43205c1b
14 files changed
+655
-142
No files matched your search
@@ -1,5 +1,7 @@
|
||||
import logging
|
||||
from types import SimpleNamespace
|
||||
|
||||
from deerflow.agents.lead_agent import agent as agent_module
|
||||
from deerflow.agents.lead_agent import prompt as prompt_module
|
||||
from deerflow.config.skills_config import EsQueryRoutingConfig, SkillsConfig
|
||||
|
||||
@@ -68,3 +70,41 @@ def test_es_query_routing_is_injected_into_lead_and_allowed_custom_agent_prompts
|
||||
assert "自定义 ES 路由规则" in lead_prompt
|
||||
assert '<skill_routing skill="es_query">' in allowed_custom_prompt
|
||||
assert '<skill_routing skill="es_query">' not in denied_custom_prompt
|
||||
|
||||
|
||||
def test_es_query_routing_registration_log_confirms_exact_final_prompt(caplog) -> None:
|
||||
config = _app_config(enabled=True, prompt="自定义 ES 路由规则")
|
||||
system_prompt = '<skill_routing skill="es_query">\n自定义 ES 路由规则\n</skill_routing>'
|
||||
|
||||
with caplog.at_level(logging.INFO, logger=agent_module.__name__):
|
||||
agent_module._log_es_query_routing_registration(
|
||||
system_prompt=system_prompt,
|
||||
app_config=config,
|
||||
agent_id=None,
|
||||
available_skills=None,
|
||||
)
|
||||
|
||||
assert "ES query routing prompt registration" in caplog.text
|
||||
assert "agent_id=default" in caplog.text
|
||||
assert "enabled=True" in caplog.text
|
||||
assert "eligible=True" in caplog.text
|
||||
assert "registered=True" in caplog.text
|
||||
assert "prompt_chars=11" in caplog.text
|
||||
assert "prompt_sha256=" in caplog.text
|
||||
|
||||
|
||||
def test_es_query_routing_registration_log_explains_custom_agent_exclusion(caplog) -> None:
|
||||
config = _app_config(enabled=True, prompt="自定义 ES 路由规则")
|
||||
|
||||
with caplog.at_level(logging.INFO, logger=agent_module.__name__):
|
||||
agent_module._log_es_query_routing_registration(
|
||||
system_prompt="custom agent prompt",
|
||||
app_config=config,
|
||||
agent_id="agent-without-es",
|
||||
available_skills={"other_skill"},
|
||||
)
|
||||
|
||||
assert "agent_id=agent-without-es" in caplog.text
|
||||
assert "enabled=True" in caplog.text
|
||||
assert "eligible=False" in caplog.text
|
||||
assert "registered=False" in caplog.text
|
||||
@@ -5,11 +5,16 @@ from __future__ import annotations
|
||||
import io
|
||||
import zipfile
|
||||
from pathlib import Path
|
||||
from types import SimpleNamespace
|
||||
|
||||
import pytest
|
||||
from fastapi import HTTPException
|
||||
|
||||
from app.gateway.routers import skills as skills_router
|
||||
from deerflow.persistence.skills import MemorySkillStore
|
||||
from deerflow.skills.installer import SkillAlreadyExistsError
|
||||
from deerflow.skills.storage.local_skill_storage import LocalSkillStorage
|
||||
from deerflow.skills.types import SkillCategory
|
||||
|
||||
|
||||
def _make_skill_zip(
|
||||
@@ -239,3 +244,218 @@ async def test_install_refuses_to_overwrite_public_skill(
|
||||
await storage.ainstall_skill_from_archive(
|
||||
archive, overwrite=True, require_skill_suffix=False
|
||||
)
|
||||
|
||||
|
||||
# ---------- ownership-aware direct upload conflicts ----------
|
||||
|
||||
|
||||
def _request(user_id: str, *, is_admin: bool = False) -> SimpleNamespace:
|
||||
return SimpleNamespace(
|
||||
state=SimpleNamespace(
|
||||
user=SimpleNamespace(
|
||||
id=user_id,
|
||||
system_role="admin" if is_admin else "user",
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
async def _attach_test_owner_names(responses):
|
||||
for response in responses:
|
||||
if response.owner_user_id == "user-b":
|
||||
response.owner_username = "bob"
|
||||
return responses
|
||||
|
||||
|
||||
class _UploadStorage:
|
||||
def __init__(self) -> None:
|
||||
self.install_calls = 0
|
||||
|
||||
def validate_skill_archive(self, _path: Path) -> dict:
|
||||
return {
|
||||
"ok": True,
|
||||
"skill_name": "demo-skill",
|
||||
"description": "demo",
|
||||
"license": None,
|
||||
"allowed_tools": [],
|
||||
"conflict": True,
|
||||
"conflict_kind": "custom",
|
||||
"warnings": [],
|
||||
}
|
||||
|
||||
async def ainstall_skill_from_archive(self, *_args, **_kwargs) -> dict:
|
||||
self.install_calls += 1
|
||||
return {
|
||||
"success": True,
|
||||
"skill_name": "demo-skill",
|
||||
"message": "installed",
|
||||
"overwritten": True,
|
||||
"category": "custom",
|
||||
}
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_upload_preflight_allows_owner_to_overwrite(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
store = MemorySkillStore()
|
||||
await store.create({"name": "demo-skill", "owner_user_id": "user-a"})
|
||||
monkeypatch.setattr(skills_router, "_attach_owner_usernames", _attach_test_owner_names)
|
||||
|
||||
result = await skills_router._preview_install_target(
|
||||
request=_request("user-a"),
|
||||
skill_store=store,
|
||||
skill_name="demo-skill",
|
||||
target_category=SkillCategory.CUSTOM,
|
||||
conflict_kind="custom",
|
||||
)
|
||||
|
||||
can_install_as_is, can_overwrite, owned_by_me, owned_by_other, *_rest = result
|
||||
assert can_install_as_is is False
|
||||
assert can_overwrite is True
|
||||
assert owned_by_me is True
|
||||
assert owned_by_other is False
|
||||
assert "upload it again and overwrite" in result[-1]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_upload_preflight_names_the_other_owner(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
store = MemorySkillStore()
|
||||
await store.create({"name": "demo-skill", "owner_user_id": "user-b"})
|
||||
monkeypatch.setattr(skills_router, "_attach_owner_usernames", _attach_test_owner_names)
|
||||
|
||||
result = await skills_router._preview_install_target(
|
||||
request=_request("user-a"),
|
||||
skill_store=store,
|
||||
skill_name="demo-skill",
|
||||
target_category=SkillCategory.CUSTOM,
|
||||
conflict_kind="custom",
|
||||
)
|
||||
|
||||
can_install_as_is, can_overwrite, owned_by_me, owned_by_other = result[:4]
|
||||
assert can_install_as_is is False
|
||||
assert can_overwrite is False
|
||||
assert owned_by_me is False
|
||||
assert owned_by_other is True
|
||||
assert result[6] == "bob"
|
||||
assert "user 'bob'" in result[-1]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_validate_upload_surfaces_the_other_owner(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
tmp_path: Path,
|
||||
) -> None:
|
||||
store = MemorySkillStore()
|
||||
await store.create({"name": "demo-skill", "owner_user_id": "user-b"})
|
||||
storage = _UploadStorage()
|
||||
upload_path = tmp_path / "validate.skill"
|
||||
upload_path.write_bytes(b"placeholder")
|
||||
|
||||
async def _persist(_file) -> Path:
|
||||
return upload_path
|
||||
|
||||
monkeypatch.setattr(skills_router, "_persist_uploaded_skill", _persist)
|
||||
monkeypatch.setattr(
|
||||
skills_router,
|
||||
"get_or_new_skill_storage",
|
||||
lambda **_kwargs: storage,
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
skills_router,
|
||||
"_attach_owner_usernames",
|
||||
_attach_test_owner_names,
|
||||
)
|
||||
|
||||
response = await skills_router.validate_skill_upload(
|
||||
request=_request("user-a"),
|
||||
file=object(),
|
||||
target_category=SkillCategory.CUSTOM,
|
||||
config=object(),
|
||||
skill_store=store,
|
||||
)
|
||||
|
||||
assert response.conflict is True
|
||||
assert response.conflict_kind == "custom"
|
||||
assert response.can_overwrite is False
|
||||
assert response.owned_by_other is True
|
||||
assert response.owner_username == "bob"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_direct_upload_owner_can_overwrite_own_skill(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
tmp_path: Path,
|
||||
) -> None:
|
||||
store = MemorySkillStore()
|
||||
await store.create({"name": "demo-skill", "owner_user_id": "user-a"})
|
||||
storage = _UploadStorage()
|
||||
upload_path = tmp_path / "owner-upload.skill"
|
||||
upload_path.write_bytes(b"placeholder")
|
||||
|
||||
async def _persist(_file) -> Path:
|
||||
return upload_path
|
||||
|
||||
async def _refresh() -> None:
|
||||
return None
|
||||
|
||||
monkeypatch.setattr(skills_router, "_persist_uploaded_skill", _persist)
|
||||
monkeypatch.setattr(
|
||||
skills_router,
|
||||
"get_or_new_skill_storage",
|
||||
lambda **_kwargs: storage,
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
skills_router,
|
||||
"refresh_skills_system_prompt_cache_async",
|
||||
_refresh,
|
||||
)
|
||||
monkeypatch.setattr("deerflow.skills.usage.mark_uploaded", lambda _name: None)
|
||||
|
||||
response = await skills_router.install_skill_upload(
|
||||
request=_request("user-a"),
|
||||
file=object(),
|
||||
overwrite=True,
|
||||
target_category=SkillCategory.CUSTOM,
|
||||
config=object(),
|
||||
skill_store=store,
|
||||
)
|
||||
|
||||
assert response.success is True
|
||||
assert response.overwritten is True
|
||||
assert storage.install_calls == 1
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_direct_upload_rechecks_owner_before_overwrite(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
tmp_path: Path,
|
||||
) -> None:
|
||||
store = MemorySkillStore()
|
||||
await store.create({"name": "demo-skill", "owner_user_id": "user-b"})
|
||||
storage = _UploadStorage()
|
||||
upload_path = tmp_path / "upload.skill"
|
||||
upload_path.write_bytes(b"placeholder")
|
||||
|
||||
async def _persist(_file) -> Path:
|
||||
return upload_path
|
||||
|
||||
monkeypatch.setattr(skills_router, "_persist_uploaded_skill", _persist)
|
||||
monkeypatch.setattr(skills_router, "get_or_new_skill_storage", lambda **_kwargs: storage)
|
||||
monkeypatch.setattr(skills_router, "_attach_owner_usernames", _attach_test_owner_names)
|
||||
|
||||
with pytest.raises(HTTPException) as exc_info:
|
||||
await skills_router.install_skill_upload(
|
||||
request=_request("user-a", is_admin=True),
|
||||
file=object(),
|
||||
overwrite=True,
|
||||
target_category=SkillCategory.CUSTOM,
|
||||
config=object(),
|
||||
skill_store=store,
|
||||
)
|
||||
|
||||
assert exc_info.value.status_code == 409
|
||||
assert "user 'bob'" in str(exc_info.value.detail)
|
||||
assert storage.install_calls == 0
|
||||
Reference in new issue
Block a user