Skip to content

Build and run agents you can see, understand and trust.

fix(skill): read SKILL.md as utf-8-sig so a BOM keeps front matter (#2735)

lihongyuan9978c4ec47f5f7
diff --git a/src/agentscope/skill/_local_loader.py b/src/agentscope/skill/_local_loader.py
index 917ba5eaa..cff7aacfb 100644
--- a/src/agentscope/skill/_local_loader.py
+++ b/src/agentscope/skill/_local_loader.py
@@ -60,7 +60,10 @@ class LocalSkillLoader(SkillLoaderBase):
async with aiofiles.open(
skill_md_path,
"r",
- encoding="utf-8",
+ # ``utf-8-sig`` also accepts a byte order mark. Windows
+ # editors commonly write one, and it would otherwise hide
+ # the opening front matter delimiter.
+ encoding="utf-8-sig",
) as f:
content_str = await f.read()
content = frontmatter.loads(content_str)
diff --git a/tests/skill_loader_test.py b/tests/skill_loader_test.py
index 84e332921..2e889cafb 100644
--- a/tests/skill_loader_test.py
+++ b/tests/skill_loader_test.py
@@ -5,9 +5,12 @@ import os
import tempfile
import shutil
import time
+from dataclasses import asdict
from unittest.async_case import IsolatedAsyncioTestCase
from unittest.mock import patch
+from utils import AnyValue
+
from agentscope.skill import LocalSkillLoader
@@ -86,6 +89,45 @@ This is the subdir2 skill content.
finally:
shutil.rmtree(empty_dir)
+ async def test_skill_md_with_utf8_bom(self) -> None:
+ """A byte order mark does not hide the front matter.
+
+ Windows editors such as PowerShell's ``Out-File`` write UTF-8 with
+ a leading BOM, which used to make the opening ``---`` invisible so
+ the skill was dropped with a "missing required fields" warning.
+ """
+ body = """---
+name: bom_skill
+description: A skill saved with a byte order mark
+---
+
+This skill is loaded despite the BOM.
+"""
+ with tempfile.TemporaryDirectory() as skill_dir:
+ with open(
+ os.path.join(skill_dir, "SKILL.md"),
+ "wb",
+ ) as f:
+ f.write(b"\xef\xbb\xbf" + body.encode("utf-8"))
+
+ skills = await LocalSkillLoader(
+ skill_dir,
+ scan_subdir=False,
+ ).list_skills()
+
+ self.assertEqual(
+ [asdict(skill) for skill in skills],
+ [
+ {
+ "name": "bom_skill",
+ "description": "A skill saved with a byte order mark",
+ "dir": skill_dir,
+ "markdown": "This skill is loaded despite the BOM.",
+ "updated_at": AnyValue(),
+ },
+ ],
+ )
+
async def test_expanduser_directory(self) -> None:
"""Test that ``~`` is expanded before loading skills."""
with tempfile.TemporaryDirectory() as home_dir: