Files
coder/codersdk/workspacesdk/frontmatter.go
T
7c7e2e73cf feat: honor disable-model-invocation in uploaded skill content (#31159)
Addresses @ethanndickson's [review comment on
#30882](https://github.com/coder/coder/pull/30882#discussion_r4344815088):
the same `SKILL.md` file could be manual-only in Claude Code and the
project dialog but automatic in Coder, because the server and the
browser each handled `disable-model-invocation` in their own way.

The server now owns the key for personal, organization, and project
skills:

- When a create or update request leaves `model_invocable` unset,
`disable-model-invocation: true` in the content turns automatic use off,
and `false` turns it back on. The key is read with the function
workspace skills use, so a file gets the same setting in both places.
Only YAML booleans count, so a quoted `"true"` is ignored.
- An explicit `model_invocable` in the request always wins.
- New content without the key leaves the setting unchanged, so editing
text never turns it back on.
- Content is stored as uploaded. Every API response returns content
whose key agrees with `model_invocable`: when the stored file says
otherwise, the server replaces the value of the key or adds the key
before the first one, and leaves every other byte alone.
- **Download** and **Export all** save the content the API returns, so
the browser no longer edits the file. `withDisabledModelInvocation` is
removed.
- An upload is rejected with a 400 when the key holds a value the server
can't replace with a boolean, such as a list. A later change to
`model_invocable` can then always reach the file.

Two edges:

- Adding the key makes the returned file about 30 bytes larger, so a
file at the 64 KB upload limit can be too large to upload again.
- A skill stored before the upload check with a value the server can't
replace is returned as stored.

Workspace skills already honor the key since #31147.

#31160, the frontend PR on top of this one, adds the switch to the
personal and organization skill editors.

> Generated by an agent on behalf of the user

---------

Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Nick Vigilante <nickvigilante@users.noreply.github.com>
2026-11-18 14:04:49 +07:00

162 lines
5.4 KiB
Go

package workspacesdk
import (
"regexp"
"strings"
"golang.org/x/xerrors"
"gopkg.in/yaml.v3"
)
// SkillNameRegex is the regular expression used to validate kebab-case skill names.
const SkillNameRegex = "^[a-z0-9]+(-[a-z0-9]+)*$"
// MaxSkillMetaBytes is the maximum raw Markdown size accepted for a skill meta file.
const MaxSkillMetaBytes = 64 * 1024
// SkillNamePattern is the compiled pattern used to validate kebab-case skill names.
var SkillNamePattern = regexp.MustCompile(SkillNameRegex)
// MaxPluginNameLength is the maximum length in bytes of an Agent Plugin name.
const MaxPluginNameLength = 64
// ValidatePluginName checks name against the Agent Plugins manifest name
// grammar: 1 to MaxPluginNameLength bytes from [a-z0-9.-], starting and
// ending with an alphanumeric character, with no "--" or ".." runs.
func ValidatePluginName(name string) error {
if name == "" {
return xerrors.New("plugin name is required")
}
if len(name) > MaxPluginNameLength {
return xerrors.Errorf("plugin name is %d bytes, maximum is %d", len(name), MaxPluginNameLength)
}
for i, c := range name {
isAlnum := (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9')
if isAlnum {
continue
}
if c != '.' && c != '-' {
return xerrors.Errorf("plugin name %q contains invalid character %q; allowed are a-z, 0-9, '.', and '-'", name, c)
}
if i == 0 || i == len(name)-1 {
return xerrors.Errorf("plugin name %q must start and end with a letter or digit", name)
}
if rune(name[i-1]) == c {
return xerrors.Errorf("plugin name %q must not contain %q", name, string([]rune{c, c}))
}
}
return nil
}
// markdownCommentRe strips HTML comments from skill file bodies so
// they don't leak into the LLM prompt.
var markdownCommentRe = regexp.MustCompile(`<!--[\s\S]*?-->`)
// ErrFrontmatterNameRequired is returned by ParseSkillFrontmatter when
// the frontmatter is missing a required name field.
var ErrFrontmatterNameRequired = xerrors.New("frontmatter missing required 'name' field")
func frontmatterStringField(frontmatter map[string]any, key string) (string, bool, error) {
value, ok := frontmatter[key]
if !ok {
return "", false, nil
}
stringValue, ok := value.(string)
if !ok {
return "", true, xerrors.Errorf("frontmatter field %q must be a string", key)
}
return strings.TrimRight(stringValue, "\r\n"), true, nil
}
// ParseSkillFrontmatter extracts name, description, and the
// remaining body from a skill meta file. The expected format is
// YAML frontmatter delimited by "---" lines:
//
// ---
// name: my-skill
// description: Does a thing
// ---
// Body text here...
func ParseSkillFrontmatter(content string) (name, description, body string, err error) {
frontmatter, body, err := splitSkillFrontmatter(content)
if err != nil {
return "", "", "", err
}
name, ok, err := frontmatterStringField(frontmatter, "name")
if err != nil {
return "", "", "", xerrors.Errorf("%w: %v", ErrFrontmatterNameRequired, err)
}
if !ok || name == "" {
return "", "", "", xerrors.Errorf("%w", ErrFrontmatterNameRequired)
}
description, _, err = frontmatterStringField(frontmatter, "description")
if err != nil {
return "", "", "", err
}
return name, description, body, nil
}
// SkillDisablesModelInvocation reports whether a skill meta file's
// frontmatter sets disable-model-invocation to the YAML boolean true.
// Agents must not discover or load such a skill on their own; only a
// person can invoke it. Any other value, and content whose frontmatter
// does not parse, leaves the skill model-invocable.
func SkillDisablesModelInvocation(content string) bool {
disabled, _ := SkillDisableModelInvocationValue(content)
return disabled
}
// SkillDisableModelInvocationValue returns the value of
// disable-model-invocation in a skill meta file's frontmatter. ok is false
// when the key is absent or is not a YAML boolean, and when the frontmatter
// does not parse. Stored skills use ok to tell false, which turns automatic
// use back on, from a file that says nothing.
func SkillDisableModelInvocationValue(content string) (disabled, ok bool) {
frontmatter, _, err := splitSkillFrontmatter(content)
if err != nil {
return false, false
}
disabled, ok = frontmatter["disable-model-invocation"].(bool)
return disabled, ok
}
// splitSkillFrontmatter decodes the YAML frontmatter of a skill meta file
// and returns it with the body that follows, with HTML comments stripped
// and surrounding whitespace trimmed.
func splitSkillFrontmatter(content string) (frontmatter map[string]any, body string, err error) {
content = strings.TrimPrefix(content, "\xef\xbb\xbf")
lines := strings.Split(content, "\n")
if len(lines) == 0 || strings.TrimSpace(lines[0]) != "---" {
return nil, "", xerrors.New(
"missing opening frontmatter delimiter",
)
}
closingIdx := -1
for i := 1; i < len(lines); i++ {
if strings.TrimSpace(lines[i]) == "---" {
closingIdx = i
break
}
}
if closingIdx < 0 {
return nil, "", xerrors.New(
"missing closing frontmatter delimiter",
)
}
frontmatterContent := strings.Join(lines[1:closingIdx], "\n")
if err := yaml.Unmarshal([]byte(frontmatterContent), &frontmatter); err != nil {
return nil, "", xerrors.Errorf("parse frontmatter YAML: %w", err)
}
// Everything after the closing delimiter is the body.
body = strings.Join(lines[closingIdx+1:], "\n")
body = markdownCommentRe.ReplaceAllString(body, "")
body = strings.TrimSpace(body)
return frontmatter, body, nil
}