Files
deerflow-code/offline-backend-20260512/backend/tests/test_agent_skill_migration.py
T

757 lines
26 KiB
Python

from __future__ import annotations
import threading
import time
from pathlib import Path
from types import SimpleNamespace
import pytest
from sqlalchemy.ext.asyncio import async_sessionmaker, create_async_engine
from app.gateway import agent_skill_migration as migration_module
from app.gateway.agent_skill_migration import (
AgentManifestItem,
MigrationManifest,
OwnerReference,
SkillManifestItem,
SupportDirectoryManifestItem,
apply_migration_manifest,
build_agent_migration_manifest,
build_skill_migration_manifest,
compute_tree_hash,
preflight_manifest,
prepare_manifest_for_apply,
)
from deerflow.persistence.agents.memory import MemoryAgentStore
from deerflow.persistence.agents.sql import AgentRepository
from deerflow.persistence.base import Base
from deerflow.persistence.skills.memory import MemorySkillStore
from deerflow.persistence.skills.sql import SkillRepository
from deerflow.persistence.tags.memory import MemoryTagStore
from deerflow.skills import usage as skill_usage
def _write(path: Path, content: str) -> None:
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(content, encoding="utf-8")
def _manifest(agents_root: Path, skills_root: Path) -> MigrationManifest:
agent_dir = agents_root / "agent-one"
skill_dir = skills_root / "custom" / "folder-does-not-match-name"
return MigrationManifest(
bundle_id="bundle-1",
agents=[
AgentManifestItem(
id="agent-one",
relative_dir="agent-one",
tree_hash=compute_tree_hash(agent_dir),
name="智能体一",
owner=OwnerReference(source_user_id="source-u1", email="owner@example.com"),
skills=["skill-one"],
)
],
skills=[
SkillManifestItem(
name="skill-one",
category="custom",
relative_dir="custom/folder-does-not-match-name",
tree_hash=compute_tree_hash(skill_dir),
owner=OwnerReference(source_user_id="source-u1", email="owner@example.com"),
)
],
)
def test_preflight_matches_live_directories_by_agent_id_and_skill_manifest_name(tmp_path: Path) -> None:
agents_root = tmp_path / "agents"
skills_root = tmp_path / "skills"
_write(agents_root / "agent-one" / "config.yaml", "id: agent-one\nname: 智能体一\nskills:\n - skill-one\n")
_write(
skills_root / "custom" / "folder-does-not-match-name" / "SKILL.md",
"---\nname: skill-one\ndescription: test\n---\nbody\n",
)
result = preflight_manifest(
_manifest(agents_root, skills_root),
agents_root=agents_root,
skills_root=skills_root,
users_by_email={"owner@example.com": "target-u9"},
)
assert result.ready is True
assert result.agents[0].status == "matched"
assert result.skills[0].status == "matched"
assert result.owner_mappings[0].target_user_id == "target-u9"
def test_preflight_blocks_missing_or_changed_live_content(tmp_path: Path) -> None:
agents_root = tmp_path / "agents"
skills_root = tmp_path / "skills"
_write(agents_root / "agent-one" / "config.yaml", "id: agent-one\nname: 智能体一\n")
_write(
skills_root / "custom" / "folder-does-not-match-name" / "SKILL.md",
"---\nname: skill-one\ndescription: test\n---\nbody\n",
)
manifest = _manifest(agents_root, skills_root)
_write(agents_root / "agent-one" / "SOUL.md", "changed after export")
(skills_root / "custom" / "folder-does-not-match-name" / "SKILL.md").unlink()
result = preflight_manifest(
manifest,
agents_root=agents_root,
skills_root=skills_root,
users_by_email={"owner@example.com": "target-u9"},
)
assert result.ready is False
assert result.agents[0].status == "hash_mismatch"
assert result.skills[0].status == "missing"
def test_preflight_blocks_unresolved_owner_and_identity_mismatch(tmp_path: Path) -> None:
agents_root = tmp_path / "agents"
skills_root = tmp_path / "skills"
_write(agents_root / "agent-one" / "config.yaml", "id: another-agent\nname: wrong\n")
_write(
skills_root / "custom" / "folder-does-not-match-name" / "SKILL.md",
"---\nname: another-skill\ndescription: wrong\n---\nbody\n",
)
manifest = _manifest(agents_root, skills_root)
# Re-sign the mismatching content so identity validation, rather than the
# checksum, is what rejects it.
manifest.agents[0].tree_hash = compute_tree_hash(agents_root / "agent-one")
manifest.skills[0].tree_hash = compute_tree_hash(skills_root / "custom" / "folder-does-not-match-name")
result = preflight_manifest(
manifest,
agents_root=agents_root,
skills_root=skills_root,
users_by_email={},
)
assert result.ready is False
assert result.agents[0].status == "identity_mismatch"
assert result.skills[0].status == "identity_mismatch"
assert result.owner_mappings[0].status == "unresolved"
def test_preflight_checks_many_skill_trees_with_bounded_parallelism(
tmp_path: Path,
monkeypatch: pytest.MonkeyPatch,
) -> None:
active = 0
peak_active = 0
lock = threading.Lock()
def fake_resource_preflight(**kwargs):
nonlocal active, peak_active
with lock:
active += 1
peak_active = max(peak_active, active)
time.sleep(0.02)
with lock:
active -= 1
return migration_module.ResourcePreflightItem(
kind=kwargs["kind"],
key=kwargs["key"],
relative_dir=kwargs["relative_dir"],
status="matched",
)
monkeypatch.setattr(
migration_module,
"_resource_preflight",
fake_resource_preflight,
)
manifest = MigrationManifest(
bundle_id="parallel-preflight",
skills=[
SkillManifestItem(
name=f"skill-{index}",
category="custom",
relative_dir=f"custom/skill-{index}",
tree_hash="0" * 64,
)
for index in range(12)
],
)
result = preflight_manifest(
manifest,
agents_root=tmp_path / "agents",
skills_root=tmp_path / "skills",
users_by_email={},
)
assert result.ready is True
assert peak_active > 1
def test_prepare_manifest_can_skip_invalid_skills_and_their_support_dependants(
tmp_path: Path,
) -> None:
agents_root = tmp_path / "agents"
skills_root = tmp_path / "skills"
good_dir = skills_root / "custom" / "good"
missing_dir = skills_root / "custom" / "missing"
support_dir = skills_root / "public" / "shared-support"
_write(good_dir / "SKILL.md", "---\nname: good\n---\nbody\n")
_write(missing_dir / "SKILL.md", "---\nname: missing\n---\nbody\n")
_write(support_dir / "helper.py", "VALUE = 1\n")
manifest = MigrationManifest(
bundle_id="skills-with-one-bad-entry",
skills=[
SkillManifestItem(
name="good",
category="custom",
relative_dir="custom/good",
tree_hash=compute_tree_hash(good_dir),
),
SkillManifestItem(
name="missing",
category="custom",
relative_dir="custom/missing",
tree_hash=compute_tree_hash(missing_dir),
),
],
support_directories=[
SupportDirectoryManifestItem(
relative_dir="public/shared-support",
tree_hash=compute_tree_hash(support_dir),
required_by=["missing"],
)
],
)
(missing_dir / "SKILL.md").unlink()
(support_dir / "helper.py").unlink()
preflight = preflight_manifest(
manifest,
agents_root=agents_root,
skills_root=skills_root,
users_by_email={},
)
effective_manifest, effective_preflight, skipped = prepare_manifest_for_apply(
manifest,
preflight,
skip_invalid_skills=True,
)
assert [item.name for item in effective_manifest.skills] == ["good"]
assert effective_manifest.support_directories == []
assert skipped == ["missing"]
assert effective_preflight.ready is True
assert [item.key for item in effective_preflight.skills] == ["custom:good"]
def test_prepare_manifest_does_not_skip_failures_without_explicit_opt_in(
tmp_path: Path,
) -> None:
agents_root = tmp_path / "agents"
skills_root = tmp_path / "skills"
skill_dir = skills_root / "custom" / "missing"
_write(skill_dir / "SKILL.md", "---\nname: missing\n---\nbody\n")
manifest = MigrationManifest(
bundle_id="strict-skill-import",
skills=[
SkillManifestItem(
name="missing",
category="custom",
relative_dir="custom/missing",
tree_hash=compute_tree_hash(skill_dir),
)
],
)
(skill_dir / "SKILL.md").unlink()
preflight = preflight_manifest(
manifest,
agents_root=agents_root,
skills_root=skills_root,
users_by_email={},
)
effective_manifest, effective_preflight, skipped = prepare_manifest_for_apply(
manifest,
preflight,
skip_invalid_skills=False,
)
assert effective_manifest is manifest
assert effective_preflight is preflight
assert skipped == []
assert effective_preflight.ready is False
@pytest.mark.asyncio
async def test_apply_manifest_skips_invalid_skill_and_applies_matched_skill(
tmp_path: Path,
monkeypatch: pytest.MonkeyPatch,
) -> None:
agents_root = tmp_path / "agents"
skills_root = tmp_path / "skills"
good_dir = skills_root / "custom" / "good"
missing_dir = skills_root / "custom" / "missing"
_write(good_dir / "SKILL.md", "---\nname: good\n---\nbody\n")
_write(missing_dir / "SKILL.md", "---\nname: missing\n---\nbody\n")
manifest = MigrationManifest(
bundle_id="partial-skill-import",
skills=[
SkillManifestItem(
name="good",
category="custom",
relative_dir="custom/good",
tree_hash=compute_tree_hash(good_dir),
),
SkillManifestItem(
name="missing",
category="custom",
relative_dir="custom/missing",
tree_hash=compute_tree_hash(missing_dir),
),
],
)
(missing_dir / "SKILL.md").unlink()
preflight = preflight_manifest(
manifest,
agents_root=agents_root,
skills_root=skills_root,
users_by_email={},
)
extensions_path = tmp_path / "extensions_config.json"
extensions_path.write_text("{}", encoding="utf-8")
monkeypatch.setattr(
"app.gateway.agent_skill_migration._apply_usage_metadata",
lambda _name, _metadata: None,
)
skills = MemorySkillStore()
result = await apply_migration_manifest(
manifest,
preflight=preflight,
agent_store=MemoryAgentStore(),
skill_store=skills,
tag_store=MemoryTagStore(),
agents_root=agents_root,
users_by_email={},
fallback_owner_user_id=None,
extensions_config_path=extensions_path,
skip_invalid_skills=True,
)
assert result.skills_applied == 1
assert result.skills_skipped == 1
assert result.skipped_skill_names == ["missing"]
assert await skills.get_any("good") is not None
assert await skills.get_any("missing") is None
@pytest.mark.asyncio
async def test_apply_many_skills_uses_batch_persistence_tags_and_usage(
tmp_path: Path,
monkeypatch: pytest.MonkeyPatch,
) -> None:
class CountingSkillStore(MemorySkillStore):
def __init__(self) -> None:
super().__init__()
self.single_calls = 0
self.batch_calls = 0
async def migration_upsert(self, data):
self.single_calls += 1
return await super().migration_upsert(data)
async def migration_upsert_many(self, items):
self.batch_calls += 1
return [
await MemorySkillStore.migration_upsert(self, item)
for item in items
]
class CountingTagStore(MemoryTagStore):
def __init__(self) -> None:
super().__init__()
self.list_calls = 0
self.single_assignment_calls = 0
self.batch_assignment_calls = 0
async def list_tags(self, tag_type=None):
self.list_calls += 1
return await super().list_tags(tag_type)
async def set_target_tags(self, target_type, target_id, tag_ids):
self.single_assignment_calls += 1
return await super().set_target_tags(target_type, target_id, tag_ids)
async def set_target_tags_bulk(self, target_type, assignments):
self.batch_assignment_calls += 1
return {
target_id: await MemoryTagStore.set_target_tags(
self,
target_type,
target_id,
tag_ids,
)
for target_id, tag_ids in assignments.items()
}
agents_root = tmp_path / "agents"
skills_root = tmp_path / "skills"
manifest_skills: list[SkillManifestItem] = []
for index in range(8):
name = f"skill-{index}"
skill_dir = skills_root / "custom" / name
_write(skill_dir / "SKILL.md", f"---\nname: {name}\n---\nbody\n")
manifest_skills.append(
SkillManifestItem(
name=name,
category="custom",
relative_dir=f"custom/{name}",
tree_hash=compute_tree_hash(skill_dir),
)
)
manifest = MigrationManifest(
bundle_id="batch-skill-apply",
skills=manifest_skills,
)
preflight = preflight_manifest(
manifest,
agents_root=agents_root,
skills_root=skills_root,
users_by_email={},
)
extensions_path = tmp_path / "extensions_config.json"
extensions_path.write_text("{}", encoding="utf-8")
usage_batches: list[dict[str, dict]] = []
monkeypatch.setattr(
migration_module,
"_apply_usage_metadata",
lambda _name, _metadata: None,
raising=False,
)
monkeypatch.setattr(
migration_module,
"_apply_usage_metadata_batch",
lambda entries: usage_batches.append(entries),
raising=False,
)
skills = CountingSkillStore()
tags = CountingTagStore()
result = await apply_migration_manifest(
manifest,
preflight=preflight,
agent_store=MemoryAgentStore(),
skill_store=skills,
tag_store=tags,
agents_root=agents_root,
users_by_email={},
fallback_owner_user_id=None,
extensions_config_path=extensions_path,
)
assert result.skills_applied == 8
assert skills.batch_calls == 1
assert skills.single_calls == 0
assert tags.list_calls == 1
assert tags.batch_assignment_calls == 1
assert tags.single_assignment_calls == 0
assert len(usage_batches) == 1
assert set(usage_batches[0]) == {f"skill-{index}" for index in range(8)}
def test_skill_usage_migration_metadata_is_written_once_for_a_batch(
monkeypatch: pytest.MonkeyPatch,
) -> None:
writes: list[dict] = []
monkeypatch.setattr(skill_usage, "_read_all", lambda: {})
monkeypatch.setattr(skill_usage, "_write_all", lambda data: writes.append(data))
skill_usage.set_migration_metadata_batch(
{
f"skill-{index}": {
"state": "stale",
"pinned": index % 2 == 0,
"featured": False,
"source": "upload",
}
for index in range(100)
}
)
assert len(writes) == 1
assert len(writes[0]) == 100
assert writes[0]["skill-0"]["state"] == "stale"
assert writes[0]["skill-0"]["pinned"] is True
def test_manifest_rejects_absolute_and_parent_relative_paths() -> None:
with pytest.raises(ValueError):
AgentManifestItem(
id="agent-one",
relative_dir="../agent-one",
tree_hash="0" * 64,
name="bad",
)
with pytest.raises(ValueError):
SkillManifestItem(
name="skill-one",
category="custom",
relative_dir="C:/skills/custom/skill-one",
tree_hash="0" * 64,
)
with pytest.raises(ValueError):
SkillManifestItem(
name="skill-one",
category="custom",
relative_dir="public/skill-one",
tree_hash="0" * 64,
)
@pytest.mark.asyncio
async def test_migration_upsert_converts_startup_placeholder_rows() -> None:
agents = MemoryAgentStore()
skills = MemorySkillStore()
await agents.ensure_builtin({"id": "agent-one", "name": "placeholder", "description": "", "published": True})
await skills.ensure_legacy({"name": "skill-one", "owner_user_id": None, "published": True})
agent = await agents.migration_upsert(
{
"id": "agent-one",
"name": "source name",
"description": "source description",
"user_id": "target-u9",
"published": False,
"is_private": True,
"square_id": "private",
"featured_order": None,
"created_at": "2026-01-01T00:00:00+00:00",
"updated_at": "2026-01-02T00:00:00+00:00",
}
)
skill = await skills.migration_upsert(
{
"name": "skill-one",
"owner_user_id": "target-u9",
"published": False,
"detail": "source detail",
"name_zh": "技能一",
"always_on": True,
"square_id": "private",
"created_at": "2026-01-01T00:00:00+00:00",
"updated_at": "2026-01-02T00:00:00+00:00",
}
)
assert agent["user_id"] == "target-u9"
assert agent["published"] is False
assert agent["is_private"] is True
assert agent["created_at"] == "2026-01-01T00:00:00+00:00"
assert skill["owner_user_id"] == "target-u9"
assert skill["published"] is False
assert skill["always_on"] is True
assert skill["created_at"] == "2026-01-01T00:00:00+00:00"
@pytest.mark.asyncio
async def test_build_and_apply_manifest_round_trip_preserves_metadata_and_unknown_extension_fields(
tmp_path: Path,
monkeypatch: pytest.MonkeyPatch,
) -> None:
agents_root = tmp_path / "agents"
skills_root = tmp_path / "skills"
agent_dir = agents_root / "agent-one"
skill_dir = skills_root / "custom" / "folder-does-not-match-name"
support_dir = skills_root / "public" / "support-lib"
_write(
agent_dir / "config.yaml",
"id: agent-one\nname: 智能体一\nskills:\n - skill-one\n - agent_orchestration\nmodel: m1\ntool_groups:\n - web\nllmwiki_knowledge_base_ids:\n - wiki-1\n",
)
_write(agent_dir / "SOUL.md", "soul")
_write(skill_dir / "SKILL.md", "---\nname: skill-one\ndescription: test\n---\nbody\n")
_write(support_dir / ".deerflow-skill-support.json", '{"required_by":["skill-one"]}')
_write(support_dir / "helper.py", "VALUE = 1\n")
source_agents = MemoryAgentStore()
source_skills = MemorySkillStore()
source_tags = MemoryTagStore()
await source_agents.migration_upsert(
{
"id": "agent-one",
"name": "智能体一",
"description": "desc",
"user_id": "source-u1",
"published": False,
"is_private": True,
"square_id": "private",
"featured_order": None,
}
)
await source_skills.migration_upsert(
{
"name": "skill-one",
"owner_user_id": "source-u1",
"published": False,
"detail": "detail",
"name_zh": "技能一",
"always_on": True,
"square_id": "private",
}
)
await source_tags.create_tag({"id": "agent-tag", "name": "研判", "tag_type": "agent", "color": "blue"})
await source_tags.set_target_tags("agent", "agent-one", ["agent-tag"])
discovered = [
SimpleNamespace(
name="skill-one",
category="custom",
relative_path=Path("folder-does-not-match-name"),
skill_dir=skill_dir,
enabled=False,
)
]
agent_manifest = await build_agent_migration_manifest(
["agent-one"],
agent_store=source_agents,
tag_store=source_tags,
agents_root=agents_root,
users_by_id={"source-u1": "owner@example.com"},
)
assert [item.id for item in agent_manifest.agents] == ["agent-one"]
assert agent_manifest.agents[0].skills == ["skill-one", "agent_orchestration"]
assert agent_manifest.skills == []
assert agent_manifest.support_directories == []
assert any("知识库数据不在本迁移清单范围内" in warning for warning in agent_manifest.warnings)
skill_manifest = await build_skill_migration_manifest(
["skill-one"],
skill_store=source_skills,
tag_store=source_tags,
skills_root=skills_root,
discovered_skills=discovered,
users_by_id={"source-u1": "owner@example.com"},
usage_entries={"skill-one": {"state": "stale", "pinned": True, "source": "agent"}},
)
assert skill_manifest.agents == []
assert skill_manifest.skills[0].relative_dir == "custom/folder-does-not-match-name"
assert skill_manifest.skills[0].enabled is False
assert skill_manifest.skills[0].state == "stale"
assert skill_manifest.support_directories[0].relative_dir == "public/support-lib"
target_agents = MemoryAgentStore()
target_skills = MemorySkillStore()
target_tags = MemoryTagStore()
await target_agents.ensure_builtin({"id": "agent-one", "name": "startup placeholder", "published": True})
await target_skills.ensure_legacy({"name": "skill-one", "owner_user_id": None, "published": True})
extensions_path = tmp_path / "extensions_config.json"
extensions_path.write_text(
'{"mcpServers":{"keep":{"command":"secret-placeholder"}},"skills":{"unrelated":{"enabled":true}}}',
encoding="utf-8",
)
usage_batches: list[dict[str, dict]] = []
monkeypatch.setattr(
"app.gateway.agent_skill_migration._apply_usage_metadata",
lambda _name, _metadata: None,
raising=False,
)
monkeypatch.setattr(
"app.gateway.agent_skill_migration._apply_usage_metadata_batch",
lambda entries: usage_batches.append(entries),
)
skill_preflight = preflight_manifest(
skill_manifest,
agents_root=agents_root,
skills_root=skills_root,
users_by_email={"owner@example.com": "target-u9"},
)
skill_result = await apply_migration_manifest(
skill_manifest,
preflight=skill_preflight,
agent_store=target_agents,
skill_store=target_skills,
tag_store=target_tags,
agents_root=agents_root,
users_by_email={"owner@example.com": "target-u9"},
fallback_owner_user_id=None,
extensions_config_path=extensions_path,
)
agent_preflight = preflight_manifest(
agent_manifest,
agents_root=agents_root,
skills_root=skills_root,
users_by_email={"owner@example.com": "target-u9"},
)
agent_result = await apply_migration_manifest(
agent_manifest,
preflight=agent_preflight,
agent_store=target_agents,
skill_store=target_skills,
tag_store=target_tags,
agents_root=agents_root,
users_by_email={"owner@example.com": "target-u9"},
fallback_owner_user_id=None,
extensions_config_path=None,
)
assert agent_result.agents_applied == 1
assert agent_result.skills_applied == 0
assert skill_result.agents_applied == 0
assert skill_result.skills_applied == 1
assert (await target_agents.get_any("agent-one"))["user_id"] == "target-u9"
assert (await target_agents.get_extras_for(["agent-one"]))["agent-one"] == {
"skills": ["skill-one", "agent_orchestration"],
"tool_groups": ["web"],
"model": "m1",
}
assert (await target_skills.get_any("skill-one"))["owner_user_id"] == "target-u9"
assert [tag["name"] for tag in await target_tags.get_target_tags("agent", "agent-one")] == ["研判"]
raw_extensions = extensions_path.read_text(encoding="utf-8")
assert "secret-placeholder" in raw_extensions
assert '"unrelated"' in raw_extensions
assert '"skill-one"' in raw_extensions
assert usage_batches == [{"skill-one": {"state": "stale", "pinned": True, "featured": False, "source": "agent"}}]
@pytest.mark.asyncio
async def test_sql_migration_upsert_can_replace_placeholder_ownership(tmp_path: Path) -> None:
engine = create_async_engine(f"sqlite+aiosqlite:///{tmp_path / 'migration.db'}")
async with engine.begin() as connection:
await connection.run_sync(Base.metadata.create_all)
sessions = async_sessionmaker(engine, expire_on_commit=False)
agents = AgentRepository(sessions)
skills = SkillRepository(sessions)
try:
await agents.ensure_builtin({"id": "agent-one", "name": "placeholder", "published": True})
await skills.ensure_legacy({"name": "skill-one", "owner_user_id": None, "published": True})
agent = await agents.migration_upsert(
{
"id": "agent-one",
"name": "source",
"user_id": "target-u9",
"published": False,
"is_private": True,
"featured_order": 7,
}
)
skill = await skills.migration_upsert(
{
"name": "skill-one",
"owner_user_id": "target-u9",
"published": False,
"always_on": True,
}
)
assert agent["user_id"] == "target-u9"
assert agent["featured_order"] == 7
assert skill["owner_user_id"] == "target-u9"
assert skill["always_on"] is True
finally:
await engine.dispose()
(apply_migration_manifest,)
(build_agent_migration_manifest, build_skill_migration_manifest)