Files
coder/codersdk/workspacesdk/frontmatter_test.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

282 lines
8.1 KiB
Go

package workspacesdk_test
import (
"strings"
"testing"
"github.com/stretchr/testify/require"
"github.com/coder/coder/v2/codersdk/workspacesdk"
)
func TestParseSkillFrontmatter(t *testing.T) {
t.Parallel()
t.Run("Basic", func(t *testing.T) {
t.Parallel()
name, desc, body, err := workspacesdk.ParseSkillFrontmatter(
"---\nname: my-skill\ndescription: Does a thing\n---\nBody text here.\n",
)
require.NoError(t, err)
require.Equal(t, "my-skill", name)
require.Equal(t, "Does a thing", desc)
require.Equal(t, "Body text here.", body)
})
t.Run("QuotedValues", func(t *testing.T) {
t.Parallel()
name, desc, _, err := workspacesdk.ParseSkillFrontmatter(
"---\nname: \"quoted-name\"\ndescription: 'single-quoted'\n---\n",
)
require.NoError(t, err)
require.Equal(t, "quoted-name", name)
require.Equal(t, "single-quoted", desc)
})
t.Run("EscapedDoubleQuotedValue", func(t *testing.T) {
t.Parallel()
_, desc, _, err := workspacesdk.ParseSkillFrontmatter(
"---\nname: escaped\ndescription: \"Review \\\"critical\\\" C:\\\\paths.\"\n---\nBody\n",
)
require.NoError(t, err)
require.Equal(t, "Review \"critical\" C:\\paths.", desc)
})
t.Run("FoldedDescription", func(t *testing.T) {
t.Parallel()
name, desc, body, err := workspacesdk.ParseSkillFrontmatter(
strings.Join([]string{
"---",
"name: brainstorming",
"description: >",
" Use before any creative work: features, components, functionality changes,",
" or behavior modifications. Turns ideas into approved designs through",
" collaborative dialog. Hard gate: no implementation action until the",
" design is presented and approved.",
"",
"---",
"Use this skill.",
}, "\n"),
)
require.NoError(t, err)
require.Equal(t, "brainstorming", name)
require.Equal(t, strings.Join([]string{
"Use before any creative work: features, components, functionality changes,",
"or behavior modifications. Turns ideas into approved designs through",
"collaborative dialog. Hard gate: no implementation action until the",
"design is presented and approved.",
}, " "), desc)
require.Equal(t, "Use this skill.", body)
})
t.Run("YAMLComments", func(t *testing.T) {
t.Parallel()
_, desc, _, err := workspacesdk.ParseSkillFrontmatter(
"---\nname: plain-hash\ndescription: Build # test\n---\nBody\n",
)
require.NoError(t, err)
require.Equal(t, "Build", desc)
})
t.Run("ErrorNullDescription", func(t *testing.T) {
t.Parallel()
_, _, _, err := workspacesdk.ParseSkillFrontmatter(
"---\nname: null-description\ndescription: null\n---\nBody\n",
)
require.ErrorContains(t, err, `frontmatter field "description" must be a string`)
})
t.Run("NoDescription", func(t *testing.T) {
t.Parallel()
name, desc, body, err := workspacesdk.ParseSkillFrontmatter(
"---\nname: minimal\n---\nSome body.\n",
)
require.NoError(t, err)
require.Equal(t, "minimal", name)
require.Empty(t, desc)
require.Equal(t, "Some body.", body)
})
t.Run("HTMLCommentsStripped", func(t *testing.T) {
t.Parallel()
_, _, body, err := workspacesdk.ParseSkillFrontmatter(
"---\nname: strip-test\n---\nBefore <!-- hidden --> after.\n",
)
require.NoError(t, err)
require.Equal(t, "Before after.", body)
})
t.Run("MultilineHTMLComment", func(t *testing.T) {
t.Parallel()
_, _, body, err := workspacesdk.ParseSkillFrontmatter(
"---\nname: multi\n---\nKeep this.\n<!--\nRemove\nall of this.\n-->\nAnd this.\n",
)
require.NoError(t, err)
require.Contains(t, body, "Keep this.")
require.Contains(t, body, "And this.")
require.NotContains(t, body, "Remove")
})
t.Run("BOMPrefix", func(t *testing.T) {
t.Parallel()
name, _, _, err := workspacesdk.ParseSkillFrontmatter(
"\xef\xbb\xbf---\nname: bom-skill\n---\n",
)
require.NoError(t, err)
require.Equal(t, "bom-skill", name)
})
t.Run("EmptyBody", func(t *testing.T) {
t.Parallel()
_, _, body, err := workspacesdk.ParseSkillFrontmatter(
"---\nname: nobody\ndescription: has no body\n---\n",
)
require.NoError(t, err)
require.Empty(t, body)
})
t.Run("YAMLKeysAreCaseSensitive", func(t *testing.T) {
t.Parallel()
_, _, _, err := workspacesdk.ParseSkillFrontmatter(
"---\nName: upper\nDescription: Also upper\n---\n",
)
require.ErrorIs(t, err, workspacesdk.ErrFrontmatterNameRequired)
})
t.Run("UnknownKeysIgnored", func(t *testing.T) {
t.Parallel()
name, _, _, err := workspacesdk.ParseSkillFrontmatter(
"---\nname: test\nauthor: someone\nversion: 1.0\n---\n",
)
require.NoError(t, err)
require.Equal(t, "test", name)
})
t.Run("ErrorMissingOpenDelimiter", func(t *testing.T) {
t.Parallel()
_, _, _, err := workspacesdk.ParseSkillFrontmatter("no frontmatter here")
require.ErrorContains(t, err, "missing opening frontmatter delimiter")
})
t.Run("ErrorMissingCloseDelimiter", func(t *testing.T) {
t.Parallel()
_, _, _, err := workspacesdk.ParseSkillFrontmatter("---\nname: oops\n")
require.ErrorContains(t, err, "missing closing frontmatter delimiter")
})
t.Run("ErrorMissingName", func(t *testing.T) {
t.Parallel()
_, _, _, err := workspacesdk.ParseSkillFrontmatter(
"---\ndescription: no name\n---\n",
)
require.ErrorIs(t, err, workspacesdk.ErrFrontmatterNameRequired)
require.ErrorContains(t, err, "frontmatter missing required 'name' field")
})
t.Run("ErrorNullName", func(t *testing.T) {
t.Parallel()
_, _, _, err := workspacesdk.ParseSkillFrontmatter(
"---\nname: null\n---\nBody\n",
)
require.ErrorIs(t, err, workspacesdk.ErrFrontmatterNameRequired)
require.ErrorContains(t, err, `frontmatter field "name" must be a string`)
})
t.Run("WhitespaceAroundDelimiters", func(t *testing.T) {
t.Parallel()
name, _, _, err := workspacesdk.ParseSkillFrontmatter(
" --- \nname: spaced\n --- \n",
)
require.NoError(t, err)
require.Equal(t, "spaced", name)
})
}
func TestValidatePluginName(t *testing.T) {
t.Parallel()
valid := []string{
"a",
"acme",
"acme-tools",
"acme.tools",
"a1.b2-c3",
"a.-b",
"a-.b",
"deployment.tools",
"lint3r",
"0",
strings.Repeat("a", workspacesdk.MaxPluginNameLength),
}
for _, name := range valid {
require.NoError(t, workspacesdk.ValidatePluginName(name), name)
}
invalid := []string{
"",
"Acme",
"acme tools",
"acme_tools",
"-acme",
"acme-",
".acme",
"acme.",
"acme--tools",
"acme..tools",
"acme/tools",
"ac\u00e9me",
"My-Plugin",
"has--double",
"too.many..dots",
strings.Repeat("a", workspacesdk.MaxPluginNameLength+1),
}
for _, name := range invalid {
require.Error(t, workspacesdk.ValidatePluginName(name), name)
}
err := workspacesdk.ValidatePluginName("ac\u00e9me")
require.ErrorContains(t, err, "invalid character '\u00e9'")
}
func TestSkillDisablesModelInvocation(t *testing.T) {
t.Parallel()
tests := []struct {
name string
value string
want bool
// wantBoolean is whether the key holds a YAML boolean at all.
wantBoolean bool
}{
{name: "True", value: "true", want: true, wantBoolean: true},
{name: "TitleCaseTrue", value: "True", want: true, wantBoolean: true},
{name: "Absent", value: "", want: false},
{name: "False", value: "false", want: false, wantBoolean: true},
{name: "QuotedTrue", value: `"true"`, want: false},
{name: "Yes", value: "yes", want: false},
{name: "One", value: "1", want: false},
{name: "Null", value: "null", want: false},
{name: "List", value: "[true]", want: false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
key := ""
if tt.value != "" {
key = "disable-model-invocation: " + tt.value + "\n"
}
content := "---\nname: manual-only\ndescription: Runs on request\n" + key + "---\nBody text.\n"
require.Equal(t, tt.want, workspacesdk.SkillDisablesModelInvocation(content))
disabled, ok := workspacesdk.SkillDisableModelInvocationValue(content)
require.Equal(t, tt.want, disabled)
require.Equal(t, tt.wantBoolean, ok)
name, description, body, err := workspacesdk.ParseSkillFrontmatter(content)
require.NoError(t, err)
require.Equal(t, "manual-only", name)
require.Equal(t, "Runs on request", description)
require.Equal(t, "Body text.", body)
})
}
}