Files
Willie 12f8c52a11 feat: add .agentignore support and agent file validation
Add ReadAgentIgnoreMatcher() in skillignore package to read .agentignore
and .agentignore.local files using the same gitignore-style pattern
format as .skillignore.

Integrate .agentignore filtering into AgentKind.Discover() with both
directory-level CanSkipDir() and file-level Match() checks.

Add agent validation in internal/validate/:
- AgentFile(): extension check, filename restrictions (no spaces/special
  chars), file size warning (>100KB), existence and type checks
- AgentName(): name format validation (alphanumeric + hyphens/underscores)

9 new tests across resource and validate packages.
2026-04-07 18:05:40 +08:00

85 lines
2.3 KiB
Go

package validate
import (
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
)
// agentFileNameRegex allows letters, numbers, underscores, hyphens, and dots.
// Must end with .md (case-insensitive checked separately).
var agentFileNameRegex = regexp.MustCompile(`^[a-zA-Z0-9][a-zA-Z0-9_.-]*$`)
// AgentFileSizeWarningThreshold is the size above which a warning is issued.
const AgentFileSizeWarningThreshold = 100 * 1024 // 100KB
// AgentValidationResult holds the result of validating an agent file.
type AgentValidationResult struct {
Valid bool
Errors []string
Warnings []string
}
// AgentFile validates a single agent .md file.
func AgentFile(filePath string) AgentValidationResult {
result := AgentValidationResult{Valid: true}
fileName := filepath.Base(filePath)
// Must be .md extension
if !strings.HasSuffix(strings.ToLower(fileName), ".md") {
result.Valid = false
result.Errors = append(result.Errors, fmt.Sprintf("agent file must have .md extension, got %q", fileName))
return result
}
// Filename restrictions: no spaces or special chars
if !agentFileNameRegex.MatchString(fileName) {
result.Valid = false
result.Errors = append(result.Errors, fmt.Sprintf("agent filename %q contains invalid characters (spaces, special chars not allowed)", fileName))
}
// Check file exists and size
info, err := os.Stat(filePath)
if err != nil {
if os.IsNotExist(err) {
result.Valid = false
result.Errors = append(result.Errors, "agent file does not exist")
}
return result
}
if info.IsDir() {
result.Valid = false
result.Errors = append(result.Errors, "path is a directory, not a file")
return result
}
if info.Size() > int64(AgentFileSizeWarningThreshold) {
result.Warnings = append(result.Warnings,
fmt.Sprintf("agent file is %dKB (>100KB) — large agents may slow down AI tools", info.Size()/1024))
}
return result
}
// AgentName validates an agent name (derived from filename).
func AgentName(name string) error {
if name == "" {
return fmt.Errorf("agent name cannot be empty")
}
if len(name) > 128 {
return fmt.Errorf("agent name too long (max 128 characters)")
}
nameRegex := regexp.MustCompile(`^[a-zA-Z0-9][a-zA-Z0-9_-]*$`)
if !nameRegex.MatchString(name) {
return fmt.Errorf("agent name must start with a letter or number and contain only letters, numbers, underscores, and hyphens")
}
return nil
}