fix(skills): accept UTF-8 BOM in skill manifests

main
wizardchen 2026-09-09 14:27:56 +08:00 committed by lyingbug
parent 85b76d1a30
commit 918c20342c
6 changed files with 57 additions and 12 deletions

View File

@ -160,6 +160,8 @@ func (s *Skill) ToMetadata() *SkillMetadata {
// It handles YAML frontmatter enclosed in --- delimiters
func ParseSkillFile(content string) (*Skill, error) {
skill := &Skill{}
// Some editors emit a UTF-8 BOM, which strings.TrimSpace does not remove.
content = strings.TrimPrefix(content, "\ufeff")
// Check for YAML frontmatter
if !strings.HasPrefix(strings.TrimSpace(content), "---") {

View File

@ -2,6 +2,7 @@ package skills
import (
"context"
"fmt"
"os"
"path/filepath"
"strings"
@ -50,6 +51,30 @@ Use this skill when testing.
skill.Name, skill.Description, len(skill.Instructions))
}
func TestParseSkillFileUTF8BOM(t *testing.T) {
for _, newline := range []string{"\n", "\r\n"} {
t.Run(fmt.Sprintf("newline=%q", newline), func(t *testing.T) {
content := "\ufeff---\nname: test-skill\ndescription: A test skill.\n---\nKeep this \ufeff character.\n"
content = strings.ReplaceAll(content, "\n", newline)
skill, err := ParseSkillFile(content)
if err != nil {
t.Fatal(err)
}
if skill.Name != "test-skill" || skill.Description != "A test skill." ||
skill.Instructions != "Keep this \ufeff character." {
t.Fatalf("unexpected parsed skill: %+v", skill)
}
metadata, err := ParseSkillMetadata(content)
if err != nil || metadata.Name != "test-skill" {
t.Fatalf("unexpected metadata: %+v, error: %v", metadata, err)
}
})
}
if _, err := ParseSkillFile("\ufeff# Missing frontmatter"); err == nil {
t.Fatal("BOM must not bypass frontmatter validation")
}
}
func TestSkillValidation(t *testing.T) {
tests := []struct {
name string

View File

@ -391,6 +391,7 @@ func validateSkillEntryName(name string) error {
}
func parseSkillBundleVersion(manifest string) (string, error) {
manifest = strings.TrimPrefix(manifest, "\ufeff")
lines := strings.Split(manifest, "\n")
frontmatterStart := -1
for i, line := range lines {

View File

@ -242,6 +242,18 @@ Use scripts/extract.py to pull text out of a PDF.
require.Equal(t, "1.2.3", bundle.Version)
})
t.Run("reads UTF-8 BOM frontmatter including version", func(t *testing.T) {
manifest := "\ufeff---\nname: pdf-tools\nversion: 1.2.3\n" +
"description: Extract text from PDF files\n---\nUse PDF tools.\n"
data := zipBundle(t, map[string]string{"SKILL.md": manifest})
bundle, err := ParseSkillBundle(data)
require.NoError(t, err)
require.Equal(t, "pdf-tools", bundle.Name)
require.Equal(t, "1.2.3", bundle.Version)
require.Equal(t, "Use PDF tools.", bundle.Instructions)
require.Equal(t, []byte(manifest), bundle.Files["SKILL.md"])
})
t.Run("uses slug when name is a display title", func(t *testing.T) {
data := zipBundle(t, map[string]string{
"SKILL.md": `---

View File

@ -914,7 +914,7 @@ func looksLikeJSON(contentType string, body []byte) bool {
}
func looksLikeSkillMarkdown(body []byte) bool {
trimmed := bytes.TrimSpace(body)
trimmed := bytes.TrimSpace(bytes.TrimPrefix(body, []byte("\ufeff")))
if !bytes.HasPrefix(trimmed, []byte("---")) {
return false
}

View File

@ -515,18 +515,23 @@ func TestFetchSkillArchiveFollowsGitHubHandoff(t *testing.T) {
}
func TestFetchSkillArchiveFromSkillMarkdown(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "text/markdown")
_, _ = w.Write([]byte(validSkillMD))
}))
t.Cleanup(server.Close)
allowLoopbackSkillFetch(t)
for _, prefix := range []string{"", "\ufeff"} {
t.Run(fmt.Sprintf("prefix=%q", prefix), func(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "text/markdown")
_, _ = w.Write([]byte(prefix + validSkillMD))
}))
t.Cleanup(server.Close)
allowLoopbackSkillFetch(t)
got, err := fetchSkillArchive(t.Context(), server.URL+"/SKILL.md", server.Client())
require.NoError(t, err)
bundle, err := ParseSkillBundle(got)
require.NoError(t, err)
require.Equal(t, "pdf-tools", bundle.Name)
got, err := fetchSkillArchive(t.Context(), server.URL+"/SKILL.md", server.Client())
require.NoError(t, err)
bundle, err := ParseSkillBundle(got)
require.NoError(t, err)
require.Equal(t, "pdf-tools", bundle.Name)
require.Equal(t, []byte(prefix+validSkillMD), bundle.Files["SKILL.md"])
})
}
}
func TestParseSkillBundleNestedRemoteArchive(t *testing.T) {