fix(skills): load categories independently and support non-git checkouts

SkillsManager refused to start when the local skills directory was not a
git repository (cloning into a non-empty path fails), and reload_skills()
returned early when injection/ was missing, so device and feature skills
never loaded. reload_packet_analysis_protocols() had the opposite bug:
an empty result wiped the registry while still reporting success.

- initialize(): accept a non-empty directory without .git as a local-only
  source (skip automatic updates, keep loading files); degrade to local
  files when GitPython is unavailable.
- _update_if_needed(): re-create/clone when the local directory vanished
  or moved, instead of reusing a stale handle forever.
- reload_skills(): injection, device and feature load independently; a
  missing category keeps its previously loaded entries.
- reload_packet_analysis_protocols(): an empty/missing directory keeps
  the existing registry and reports failure.
- reload_skills_repository(): drop the console guard cache so
  config/console_guard.txt applies on reload.

MCP device_skill_get now exposes the category filter, documents the
single-file (topic-less) layout, and returns a sections list for
index/summary of skills that have no topics/issues. An uninitialized
skills repository fails with a clear error instead of an empty list.
This commit is contained in:
yueguobin committed 2026-11-01 23:09:38 +08:00
1 parent 5ed252455d
commit 21fb84e91c
8 files changed
+338 -64

No files matched your search

+43 -3
View File
@@ -33,7 +33,13 @@ FAKE_SKILLS = {
"topics": {"bgp": {"name": "BGP", "syntax": "router bgp <asn>"}},
},
"cisco_ios_telnet": {"name": "IOS", "description": "IOS quirks", "category": "device"},
"gns3_vpcs_telnet": {"name": "VPCS", "description": "VPCS syntax", "category": "device"},
"gns3_vpcs_telnet": {
"name": "VPCS",
"description": "VPCS syntax",
"category": "device",
"config_commands": {"ip": {"syntax": "ip <address>/<mask>"}},
},
"topology_planner": {"name": "Topology Planner", "description": "Planning", "category": "feature"},
}
@@ -48,18 +54,32 @@ class TestDeviceSkillsHandler:
def test_list(self):
result = device_skills_handler({"action": "list"}, {})
assert result["status"] == "ok"
assert result["count"] == 3
assert result["count"] == 4
assert {s["device_type"] for s in result["skills"]} == set(FAKE_SKILLS)
def test_no_args_defaults_to_list(self):
result = device_skills_handler({}, {})
assert result["status"] == "ok" and result["count"] == 3
assert result["status"] == "ok" and result["count"] == 4
def test_list_filtered_by_category(self):
result = device_skills_handler({"action": "list", "category": "feature"}, {})
assert result["status"] == "ok"
assert [s["device_type"] for s in result["skills"]] == ["topology_planner"]
def test_get_by_key(self):
result = device_skills_handler({"device_type": "frr_vtysh"}, {})
assert result["status"] == "ok"
assert result["skill"]["name"] == "FRR"
def test_get_with_matching_category(self):
result = device_skills_handler({"device_type": "frr_vtysh", "category": "device"}, {})
assert result["status"] == "ok"
def test_get_with_wrong_category(self):
result = device_skills_handler({"device_type": "frr_vtysh", "category": "feature"}, {})
assert result["status"] == "failed"
assert "not in category" in result["error"]
def test_topic_body_only_on_explicit_request(self):
indexed = device_skills_handler({"device_type": "frr_vtysh"}, {})["skill"]
assert indexed["topics"]["bgp"] == "BGP" # index: names only, body withheld
@@ -67,6 +87,13 @@ class TestDeviceSkillsHandler:
topic = device_skills_handler({"device_type": "frr_vtysh", "topic": "bgp"}, {})["skill"]
assert topic["topic"] == {"bgp": {"name": "BGP", "syntax": "router bgp <asn>"}}
def test_index_sections_for_topicless_skill(self):
# Single-file skills have no topics; index names their sections
# instead of returning an empty shell.
result = device_skills_handler({"device_type": "gns3_vpcs_telnet", "detail": "index"}, {})
assert result["status"] == "ok"
assert result["skill"]["sections"] == ["config_commands"]
def test_unknown_key_lists_available(self):
# Not a key the list would return — the error carries the loaded
# keys anyway, so a stale caller can see what step 1 offers.
@@ -79,3 +106,16 @@ class TestDeviceSkillsHandler:
result = device_skills_handler({"action": "get"}, {})
assert result["status"] == "failed"
assert "Provide device_type" in result["error"]
def test_uninitialized_repository_reports_failure(self, monkeypatch):
monkeypatch.setattr(skills_registry, "SKILLS_REGISTRY", {})
monkeypatch.setattr(skills_registry, "get_skills_manager", lambda: None)
result = device_skills_handler({"action": "list"}, {})
assert result["status"] == "failed"
assert "not initialized" in result["error"]
def test_empty_but_initialized_repository_lists_zero(self, monkeypatch):
monkeypatch.setattr(skills_registry, "SKILLS_REGISTRY", {})
monkeypatch.setattr(skills_registry, "get_skills_manager", object)
result = device_skills_handler({"action": "list"}, {})
assert result == {"status": "ok", "count": 0, "skills": []}
+10
View File
@@ -260,6 +260,16 @@ class TestGetSkillTopics:
result = get_skill("frr_vtysh", detail="index")
assert result["topics"]["bgp"] == "BGP (FRR 10.x)"
def test_index_sections_for_single_file_device(self, device_registry):
# No topics/issues to index: name the content sections instead of
# returning an empty body.
result = get_skill("gns3_vpcs_telnet", detail="index")
assert result["sections"] == ["config_commands"]
def test_summary_sections_for_single_file_device(self, device_registry):
result = get_skill("gns3_vpcs_telnet", detail="summary")
assert result["sections"] == ["config_commands"]
def test_summary_includes_topic_descriptions(self, device_registry):
result = get_skill("frr_vtysh", detail="summary")
assert result["topics"]["ospf"] == {
+145
View File
@@ -0,0 +1,145 @@
#
# Copyright (C) 2026 GNS3 Technologies Inc.
# Author: Yue Guobin
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
"""
Tests for SkillsManager repository robustness and per-category reloads.
"""
import shutil
import pytest
from gns3server.agent.gns3_copilot.skills import registry
from gns3server.agent.gns3_copilot.skills.manager import SkillsManager
from gns3server.config import Config
DEVICE_YAML = """
name: "VPCS"
description: "VPCS test device"
device_type: "gns3_vpcs_telnet"
category: "device"
config_commands:
ip_config:
syntax: "ip <address>/<mask>"
"""
FEATURE_YAML = """
name: "Topology Planner"
description: "Plan topologies"
device_type: "topology_planner"
category: "feature"
"""
@pytest.fixture
def skills_root(tmp_path, monkeypatch):
monkeypatch.setattr(Config, "config_dir", property(lambda self: str(tmp_path)))
root = tmp_path / "skills"
root.mkdir()
return root
@pytest.fixture(autouse=True)
def fake_registries(monkeypatch):
"""Isolate the module registries: each test starts from empty dicts."""
monkeypatch.setattr(registry, "SKILLS_REGISTRY", {})
monkeypatch.setattr(registry, "INJECTION_SKILLS_REGISTRY", {})
monkeypatch.setattr(registry, "PACKET_ANALYSIS_REGISTRY", {})
def write_skill(root, category, filename, content):
directory = root / category
directory.mkdir(exist_ok=True)
(directory / filename).write_text(content, encoding="utf-8")
class TestRepositoryInitialization:
def test_non_git_directory_is_used_as_is(self, skills_root):
# A manually copied/cloned checkout without .git must still load;
# cloning into a non-empty directory cannot work.
write_skill(skills_root, "device", "vpcs.yaml", DEVICE_YAML)
manager = SkillsManager()
assert manager.initialize() is True
assert not (skills_root / ".git").exists()
assert manager.reload_skills() is True
assert "gns3_vpcs_telnet" in registry.SKILLS_REGISTRY
def test_update_reinitializes_after_directory_disappears(self, skills_root, monkeypatch):
write_skill(skills_root, "device", "vpcs.yaml", DEVICE_YAML)
manager = SkillsManager()
assert manager.initialize() is True
shutil.rmtree(skills_root)
cloned = []
monkeypatch.setattr(manager, "_clone", lambda: cloned.append(True) or True)
manager._update_if_needed()
assert cloned == [True]
class TestReloadSkills:
def test_device_loads_when_injection_directory_missing(self, skills_root):
write_skill(skills_root, "device", "vpcs.yaml", DEVICE_YAML)
manager = SkillsManager()
assert manager.initialize() is True
assert manager.reload_skills() is True
assert "gns3_vpcs_telnet" in registry.SKILLS_REGISTRY
def test_missing_injection_keeps_existing_entries(self, skills_root):
write_skill(skills_root, "device", "vpcs.yaml", DEVICE_YAML)
registry.INJECTION_SKILLS_REGISTRY["injection_ospf"] = {
"name": "OSPF",
"description": "existing",
"issues": {},
}
manager = SkillsManager()
assert manager.initialize() is True
assert manager.reload_skills() is True
assert "injection_ospf" in registry.INJECTION_SKILLS_REGISTRY
def test_missing_device_directory_keeps_existing_device_skills(self, skills_root):
write_skill(skills_root, "feature", "topology_planner.yaml", FEATURE_YAML)
registry.SKILLS_REGISTRY["old_device"] = {
"name": "Old Device",
"description": "existing",
"category": "device",
}
manager = SkillsManager()
assert manager.initialize() is True
assert manager.reload_skills() is True
assert "old_device" in registry.SKILLS_REGISTRY
assert "topology_planner" in registry.SKILLS_REGISTRY
def test_no_categories_loaded_returns_false(self, skills_root):
manager = SkillsManager()
manager._repo = None
assert manager.reload_skills() is False
class TestReloadPacketAnalysis:
def test_missing_directory_keeps_existing_protocols(self, skills_root):
registry.PACKET_ANALYSIS_REGISTRY["ospf"] = {"name": "OSPF"}
manager = SkillsManager()
manager._repo = None
assert manager.reload_packet_analysis_protocols() is False
assert "ospf" in registry.PACKET_ANALYSIS_REGISTRY