diff --git a/acceptance/cmd/skills/list/output.txt b/acceptance/cmd/skills/list/output.txt index 31107bd777c..f2717d4489c 100644 --- a/acceptance/cmd/skills/list/output.txt +++ b/acceptance/cmd/skills/list/output.txt @@ -3,7 +3,6 @@ Pick the skill whose description matches the task. Read the skill before you act on it. --foo: A skill whose name starts with a hyphen. demo-skill: A skill for tests. future-skill: A skill that needs a newer CLI. guide-skill: A skill with a supporting file. @@ -13,7 +12,6 @@ To read a skill, run: databricks skills read Pick the skill whose description matches the task. Read the skill before you act on it. --foo: A skill whose name starts with a hyphen. demo-skill: A skill for tests. future-skill: A skill that needs a newer CLI. guide-skill: A skill with a supporting file. @@ -25,17 +23,6 @@ To read a skill, run: databricks skills read "source": "[TESTROOT]/cmd/skills/release", "release_version": "0.0.1", "skills": [ - { - "name": "-foo", - "description": "A skill whose name starts with a hyphen.", - "version": "0.0.1", - "files": [ - "SKILL.md" - ], - "commands": [ - "jobs" - ] - }, { "name": "demo-skill", "description": "A skill for tests.", @@ -76,17 +63,6 @@ To read a skill, run: databricks skills read "source": "[TESTROOT]/cmd/skills/release", "release_version": "0.0.1", "skills": [ - { - "name": "-foo", - "description": "A skill whose name starts with a hyphen.", - "version": "0.0.1", - "files": [ - "SKILL.md" - ], - "commands": [ - "jobs" - ] - }, { "name": "demo-skill", "description": "A skill for tests.", diff --git a/acceptance/cmd/skills/release/manifest.json b/acceptance/cmd/skills/release/manifest.json index 29b1db635b8..18f5e774b70 100644 --- a/acceptance/cmd/skills/release/manifest.json +++ b/acceptance/cmd/skills/release/manifest.json @@ -5,7 +5,6 @@ "demo-skill": {"version": "0.0.1", "description": "A skill for tests.", "files": ["SKILL.md"], "commands": ["fs", "air"]}, "guide-skill": {"version": "0.0.1", "description": "A skill with a supporting file.", "files": ["SKILL.md", "references/guide.md", "-notes.md"]}, "future-skill": {"version": "0.0.1", "description": "A skill that needs a newer CLI.", "files": ["SKILL.md"], "min_cli_version": "99.0.0"}, - "preview-skill": {"version": "0.0.2", "description": "An experimental skill for tests.", "files": ["SKILL.md"], "repo_dir": "experimental"}, - "-foo": {"version": "0.0.1", "description": "A skill whose name starts with a hyphen.", "files": ["SKILL.md"], "commands": ["jobs"]} + "preview-skill": {"version": "0.0.2", "description": "An experimental skill for tests.", "files": ["SKILL.md"], "repo_dir": "experimental"} } } diff --git a/acceptance/cmd/skills/release/skills/-foo/SKILL.md b/acceptance/cmd/skills/release/skills/-foo/SKILL.md deleted file mode 100644 index 9c36f6fb254..00000000000 --- a/acceptance/cmd/skills/release/skills/-foo/SKILL.md +++ /dev/null @@ -1,6 +0,0 @@ ---- -name: -foo -description: A skill whose name starts with a hyphen. ---- - -# Foo skill diff --git a/acceptance/help/skills/hints-invalid-name/manifest.json b/acceptance/help/skills/hints-invalid-name/manifest.json new file mode 100644 index 00000000000..dd07032606a --- /dev/null +++ b/acceptance/help/skills/hints-invalid-name/manifest.json @@ -0,0 +1,8 @@ +{ + "version": "0.0.1", + "skills": { + "-foo": {"version": "0.0.1", "description": "A skill whose name starts with a hyphen.", "files": ["SKILL.md"], "commands": ["jobs"]}, + "a b": {"version": "0.0.1", "description": "A skill whose name has a space.", "files": ["SKILL.md"], "commands": ["jobs"]}, + "alpha": {"version": "0.0.1", "description": "A valid skill.", "files": ["SKILL.md"], "commands": ["jobs"]} + } +} diff --git a/acceptance/help/skills/hints-invalid-name/out.test.toml b/acceptance/help/skills/hints-invalid-name/out.test.toml new file mode 100644 index 00000000000..18b1a88417e --- /dev/null +++ b/acceptance/help/skills/hints-invalid-name/out.test.toml @@ -0,0 +1 @@ +Cloud = false diff --git a/acceptance/help/skills/hints-invalid-name/output.txt b/acceptance/help/skills/hints-invalid-name/output.txt new file mode 100644 index 00000000000..61c44fd5a6e --- /dev/null +++ b/acceptance/help/skills/hints-invalid-name/output.txt @@ -0,0 +1,15 @@ + +>>> [CLI] jobs --help +Use "databricks jobs [command] --help" for more information about a command. + +Coding agents: read the skills for this command group before you use it. + databricks skills read alpha + +>>> [CLI] skills read -- -foo +Error: unknown skill "-foo": run "databricks skills list" to see the available skills + +>>> [CLI] skills read-file -- -foo SKILL.md +Error: unknown skill: "-foo" + +>>> [CLI] skills read a b +Error: unknown skill "a b": run "databricks skills list" to see the available skills diff --git a/acceptance/help/skills/hints-invalid-name/script b/acceptance/help/skills/hints-invalid-name/script new file mode 100644 index 00000000000..3207d3cb497 --- /dev/null +++ b/acceptance/help/skills/hints-invalid-name/script @@ -0,0 +1,7 @@ +# The footer lists only the skills whose names the installer accepts. "-foo" and "a b" are left out, so no printed command starts with a flag or needs quotes. +trace $CLI jobs --help | tail -n 4 + +# The catalog leaves those names out, so reading them fails as an unknown skill. +musterr trace $CLI skills read -- -foo +musterr trace $CLI skills read-file -- -foo SKILL.md +musterr trace $CLI skills read "a b" diff --git a/acceptance/help/skills/hints-invalid-name/test.toml b/acceptance/help/skills/hints-invalid-name/test.toml new file mode 100644 index 00000000000..1e3837584d3 --- /dev/null +++ b/acceptance/help/skills/hints-invalid-name/test.toml @@ -0,0 +1,2 @@ +Env.DATABRICKS_SKILLS_DIR = "." +Env.DATABRICKS_SKILLS_HINTS = "1" diff --git a/acceptance/help/skills/hints/output.txt b/acceptance/help/skills/hints/output.txt index 7aebeb19ae0..6cd1a7b5063 100644 --- a/acceptance/help/skills/hints/output.txt +++ b/acceptance/help/skills/hints/output.txt @@ -19,17 +19,3 @@ Coding agents: read the skills for this command group before you use it. >>> [CLI] auth --help >>> [CLI] fs ls --help - ->>> [CLI] jobs --help -Use "databricks jobs [command] --help" for more information about a command. - -Coding agents: read the skills for this command group before you use it. - databricks skills read -- -foo - ->>> eval databricks skills read -- -foo ---- -name: -foo -description: A skill whose name starts with a hyphen. ---- - -# Foo skill diff --git a/acceptance/help/skills/hints/script b/acceptance/help/skills/hints/script index e14d37d34cf..49612b7f29d 100644 --- a/acceptance/help/skills/hints/script +++ b/acceptance/help/skills/hints/script @@ -14,10 +14,3 @@ trace $CLI auth --help | contains.py "!databricks skills" > /dev/null # A leaf command under a mapped group gets no footer of its own. trace $CLI fs ls --help | contains.py "!databricks skills" > /dev/null - -# A skill whose name starts with "-" ends the flags before it, so the printed command runs as printed. -databricks() { "$CLI" "$@"; } -trace $CLI jobs --help | tail -n 4 -line="databricks skills read -- -foo" -$CLI jobs --help | contains.py " $line" > LOG.read -trace eval "$line" diff --git a/cmd/skills/read.go b/cmd/skills/read.go index 73339d2dd0d..d0fc8cb1f19 100644 --- a/cmd/skills/read.go +++ b/cmd/skills/read.go @@ -104,9 +104,10 @@ func otherFiles(files []string) []string { } // writeReadText writes SKILL.md, with a trailing newline added if it is missing, -// then the other files with the command that reads each one. The commands are -// quoted for the shell of goos. A name or file that no quoting keeps literal -// is listed without its command. Nothing else goes to stdout. +// then the other files with the command that reads each one. The name is a +// skill name the catalog lists, so it is printed as it is. The file is quoted +// for the shell of goos. A file that no quoting keeps literal is listed without +// its command. Nothing else goes to stdout. func writeReadText(w io.Writer, goos, name string, content []byte, files []string) error { var b strings.Builder b.Write(content) @@ -115,19 +116,18 @@ func writeReadText(w io.Writer, goos, name string, content []byte, files []strin } if len(files) > 0 { b.WriteString("\nOther files in this skill, each read with its command:\n") - quotedName, nameOK := shellquote.Arg(goos, name) for _, f := range files { quotedFile, fileOK := shellquote.Arg(goos, f) - if !nameOK || !fileOK { + if !fileOK { fmt.Fprintf(&b, " %s\n", f) continue } - // A leading "-" would make Cobra read the name or file as a flag, so "--" ends the flags first. + // A leading "-" would make Cobra read the file as a flag, so "--" ends the flags first. endOfFlags := "" - if strings.HasPrefix(name, "-") || strings.HasPrefix(f, "-") { + if strings.HasPrefix(f, "-") { endOfFlags = "-- " } - fmt.Fprintf(&b, " databricks skills read-file %s%s %s\n", endOfFlags, quotedName, quotedFile) + fmt.Fprintf(&b, " databricks skills read-file %s%s %s\n", endOfFlags, name, quotedFile) } } _, err := io.WriteString(w, b.String()) diff --git a/cmd/skills/read_test.go b/cmd/skills/read_test.go index 35036b94709..1e697564457 100644 --- a/cmd/skills/read_test.go +++ b/cmd/skills/read_test.go @@ -35,11 +35,6 @@ func TestWriteReadTextEndsFlagsBeforeLeadingHyphenFile(t *testing.T) { require.NoError(t, err) assert.Contains(t, out.String(), " databricks skills read-file -- guide-skill -notes.md\n databricks skills read-file guide-skill references/ok.md\n") - out.Reset() - err = writeReadText(&out, "linux", "-guide-skill", []byte("# Guide\n"), []string{"references/ok.md"}) - require.NoError(t, err) - assert.Contains(t, out.String(), " databricks skills read-file -- -guide-skill references/ok.md\n") - out.Reset() err = writeReadText(&out, "linux", "guide-skill", []byte("# Guide\n"), []string{"-my notes.md"}) require.NoError(t, err) diff --git a/libs/aitools/installer/source.go b/libs/aitools/installer/source.go index e899645f206..8feb0bb4f04 100644 --- a/libs/aitools/installer/source.go +++ b/libs/aitools/installer/source.go @@ -7,9 +7,7 @@ import ( "io" "net/http" "path" - "regexp" "strings" - "sync" "time" "github.com/databricks/cli/libs/clicompat" @@ -72,19 +70,6 @@ func (s *GitHubManifestSource) FetchManifest(ctx context.Context, ref string) (* return &manifest, nil } -// maxSkillNameLength is the Agent Skills limit on the length of a skill name. -const maxSkillNameLength = 64 - -// skillNamePattern is the Agent Skills name format -// (https://agentskills.io/specification#name-field): lowercase letters, digits -// and single hyphens. Every name is a directory under the skills dir, next to -// the installer's state file, so the format also keeps names from aliasing each -// other on case-insensitive filesystems or through Windows trailing dots and -// spaces. -var skillNamePattern = sync.OnceValue(func() *regexp.Regexp { - return regexp.MustCompile(`^[a-z0-9]+(-[a-z0-9]+)*$`) -}) - // unportableSkill returns why a skill that stays inside its directory still // cannot be installed the same way on every OS, or "" if it can. Install skips // such a skill and update keeps the installed copy, the way both treat a skill @@ -95,8 +80,8 @@ var skillNamePattern = sync.OnceValue(func() *regexp.Regexp { // may differ only in case, and no path may be both a file and a folder: either // would merge or collide on a case-insensitive filesystem. func unportableSkill(name string, files []string) string { - if len(name) > maxSkillNameLength || !skillNamePattern().MatchString(name) { - return fmt.Sprintf("the name is not at most %d lowercase letters, digits and single hyphens", maxSkillNameLength) + if !skills.IsSkillName(name) { + return fmt.Sprintf("the name is not at most %d lowercase letters, digits and single hyphens", skills.MaxSkillNameLength) } if isWindowsDeviceName(name) { return "the name is a reserved Windows device name" diff --git a/libs/skills/fs.go b/libs/skills/fs.go index 448bd5dcaac..20402ac88ce 100644 --- a/libs/skills/fs.go +++ b/libs/skills/fs.go @@ -10,6 +10,8 @@ import ( "path" "slices" "time" + + "github.com/databricks/cli/libs/log" ) const manifestFile = "manifest.json" @@ -112,7 +114,9 @@ func (s *fsSource) filePath(ctx context.Context, skill, file string) (string, er } // manifest reads and checks the release manifest. Every path it names, the -// skill files, repo_dir and catalog_rules, must stay inside the release. +// skill files, repo_dir and catalog_rules, must stay inside the release. It +// drops the skills whose name the installer would reject, so every reader sees +// the same skills. func (s *fsSource) manifest(ctx context.Context) (*Manifest, error) { data, err := s.read(ctx, manifestFile) if errors.Is(err, ErrManifestTooLarge) || errors.Is(err, errReleaseNotFound) { @@ -129,5 +133,11 @@ func (s *fsSource) manifest(ctx context.Context) (*Manifest, error) { if err := CheckManifest(&m); err != nil { return nil, err } + for _, name := range slices.Sorted(maps.Keys(m.Skills)) { + if !IsSkillName(name) { + log.Debugf(ctx, "skills catalog: skipping skill %q: the name is not a valid skill name", name) + delete(m.Skills, name) + } + } return &m, nil } diff --git a/libs/skills/fs_test.go b/libs/skills/fs_test.go index cb80a28bcd8..49ac600814a 100644 --- a/libs/skills/fs_test.go +++ b/libs/skills/fs_test.go @@ -1,6 +1,7 @@ package skills import ( + "io/fs" "os" "strings" "testing" @@ -52,6 +53,40 @@ func TestCatalogFromFixture(t *testing.T) { }, catalog.Skills[1]) } +func TestCatalogLeavesOutSkillNamesTheInstallerRejects(t *testing.T) { + src := mapSource(`{"version": "1.0.0", "skills": { + "alpha": {"version": "1.0.0", "files": ["SKILL.md"]}, + "-foo": {"version": "1.0.0", "files": ["SKILL.md"]}, + "a b": {"version": "1.0.0", "files": ["SKILL.md"]}}}`) + catalog, err := src.Catalog(t.Context()) + require.NoError(t, err) + + require.Len(t, catalog.Skills, 1) + assert.Equal(t, "alpha", catalog.Skills[0].Name) +} + +func TestReadFileRejectsSkillNameTheCatalogLeavesOut(t *testing.T) { + src := mapSource(`{"version": "1.0.0", "skills": { + "alpha": {"version": "1.0.0", "files": ["SKILL.md"]}, + "-foo": {"version": "1.0.0", "files": ["SKILL.md"]}}}`) + + _, err := src.ReadFile(t.Context(), "-foo", "SKILL.md") + assert.ErrorIs(t, err, ErrUnknownSkill) + + _, err = src.ReadFile(t.Context(), "alpha", "SKILL.md") + require.Error(t, err) + assert.ErrorIs(t, err, fs.ErrNotExist) +} + +func TestIsSkillName(t *testing.T) { + for _, name := range []string{"a", "alpha", "databricks-core", "skill2", strings.Repeat("a", MaxSkillNameLength)} { + assert.True(t, IsSkillName(name), name) + } + for _, name := range []string{"", "-foo", "a b", "Alpha", "a--b", "a_b", "a/b", "a.b", strings.Repeat("a", MaxSkillNameLength+1)} { + assert.False(t, IsSkillName(name), name) + } +} + func TestReadFiles(t *testing.T) { src := fixtureSource() diff --git a/libs/skills/hints.go b/libs/skills/hints.go index d8afd77bd18..ddeabb95118 100644 --- a/libs/skills/hints.go +++ b/libs/skills/hints.go @@ -5,7 +5,6 @@ import ( "fmt" "io" "slices" - "strings" "github.com/databricks/cli/libs/log" ) @@ -50,11 +49,6 @@ func WriteGroupHint(ctx context.Context, w io.Writer, group string) { fmt.Fprintln(w) fmt.Fprintln(w, "Coding agents: read the skills for this command group before you use it.") for _, name := range names { - // A leading "-" would make Cobra read the name as a flag, so "--" ends the flags first. - endOfFlags := "" - if strings.HasPrefix(name, "-") { - endOfFlags = "-- " - } - fmt.Fprintf(w, " databricks skills read %s%s\n", endOfFlags, name) + fmt.Fprintf(w, " databricks skills read %s\n", name) } } diff --git a/libs/skills/hints_test.go b/libs/skills/hints_test.go index dab4e2d86d4..6a97079b8d0 100644 --- a/libs/skills/hints_test.go +++ b/libs/skills/hints_test.go @@ -4,12 +4,10 @@ import ( "bytes" "os" "path/filepath" - "strings" "testing" "github.com/databricks/cli/internal/testutil" "github.com/databricks/cli/libs/env" - "github.com/spf13/pflag" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -43,9 +41,11 @@ func TestWriteGroupHint(t *testing.T) { } } -func TestWriteGroupHintRunsLeadingHyphenSkillAsPrinted(t *testing.T) { +func TestWriteGroupHintLeavesOutSkillNameTheInstallerRejects(t *testing.T) { dir := t.TempDir() - const manifest = `{"version": "1.0.0", "skills": {"-foo": {"version": "1.0.0", "description": "A skill whose name starts with a hyphen.", "files": ["SKILL.md"], "commands": ["jobs"]}}}` + const manifest = `{"version": "1.0.0", "skills": { + "-foo": {"version": "1.0.0", "description": "A skill whose name starts with a hyphen.", "files": ["SKILL.md"], "commands": ["jobs"]}, + "alpha": {"version": "1.0.0", "description": "A valid skill.", "files": ["SKILL.md"], "commands": ["jobs"]}}}` require.NoError(t, os.WriteFile(filepath.Join(dir, "manifest.json"), []byte(manifest), 0o644)) ctx := env.Set(t.Context(), "DATABRICKS_SKILLS_DIR", dir) @@ -54,15 +54,8 @@ func TestWriteGroupHintRunsLeadingHyphenSkillAsPrinted(t *testing.T) { var buf bytes.Buffer WriteGroupHint(ctx, &buf, "jobs") - assert.Equal(t, "\nCoding agents: read the skills for this command group before you use it.\n"+ - " databricks skills read -- -foo\n", buf.String()) - - // The printed command, minus "databricks skills read", must parse to the name, not to flags. - printed := strings.Fields(buf.String()) - args := printed[len(printed)-2:] - fs := pflag.NewFlagSet("read", pflag.ContinueOnError) - require.NoError(t, fs.Parse(args)) - assert.Equal(t, []string{"-foo"}, fs.Args()) + require.Equal(t, "\nCoding agents: read the skills for this command group before you use it.\n"+ + " databricks skills read alpha\n", buf.String()) } // With the preview unset, a non-empty bundled copy must not produce a footer, diff --git a/libs/skills/manifest.go b/libs/skills/manifest.go index 1f152eeb6a2..91877865126 100644 --- a/libs/skills/manifest.go +++ b/libs/skills/manifest.go @@ -5,10 +5,33 @@ import ( "fmt" "maps" "path" + "regexp" "slices" "strings" + "sync" ) +// MaxSkillNameLength is the Agent Skills limit on the length of a skill name. +const MaxSkillNameLength = 64 + +// skillNamePattern is the Agent Skills name format +// (https://agentskills.io/specification#name-field): lowercase letters, digits +// and single hyphens. Every name is a directory under the skills dir, next to +// the installer's state file, so the format also keeps names from aliasing each +// other on case-insensitive filesystems or through Windows trailing dots and +// spaces. +var skillNamePattern = sync.OnceValue(func() *regexp.Regexp { + return regexp.MustCompile(`^[a-z0-9]+(-[a-z0-9]+)*$`) +}) + +// IsSkillName reports whether name has the Agent Skills name format: at most +// MaxSkillNameLength lowercase letters, digits and single hyphens. The installer +// also rejects Windows device names such as "con"; that check is separate, in +// the installer, and IsSkillName does not make it. +func IsSkillName(name string) bool { + return len(name) <= MaxSkillNameLength && skillNamePattern().MatchString(name) +} + const ( // StableRepoDir is the release directory of stable skills (the repo_dir default). StableRepoDir = "skills" diff --git a/libs/skills/resolve_test.go b/libs/skills/resolve_test.go index f70d6a878be..611c8c66d48 100644 --- a/libs/skills/resolve_test.go +++ b/libs/skills/resolve_test.go @@ -44,12 +44,11 @@ func TestLocalCatalogFromAcceptanceFixture(t *testing.T) { dir := filepath.Join("..", "..", "acceptance", "cmd", "skills", "release") catalog, err := LocalCatalog(env.Set(t.Context(), "DATABRICKS_SKILLS_DIR", dir)) require.NoError(t, err) - require.Len(t, catalog.Skills, 5) - assert.Equal(t, "-foo", catalog.Skills[0].Name) - assert.Equal(t, "demo-skill", catalog.Skills[1].Name) - assert.Equal(t, "future-skill", catalog.Skills[2].Name) - assert.Equal(t, "guide-skill", catalog.Skills[3].Name) - assert.Equal(t, "preview-skill", catalog.Skills[4].Name) + require.Len(t, catalog.Skills, 4) + assert.Equal(t, "demo-skill", catalog.Skills[0].Name) + assert.Equal(t, "future-skill", catalog.Skills[1].Name) + assert.Equal(t, "guide-skill", catalog.Skills[2].Name) + assert.Equal(t, "preview-skill", catalog.Skills[3].Name) } func TestResolveWithoutDirServesBundledCopy(t *testing.T) { diff --git a/libs/skills/skills.go b/libs/skills/skills.go index f0cd938c335..e4122ffe30a 100644 --- a/libs/skills/skills.go +++ b/libs/skills/skills.go @@ -55,8 +55,10 @@ type FileSizer interface { // Catalog is the set of skills a source serves. type Catalog struct { - // Skills are sorted by name. Install also skips non-portable names and - // paths, which the catalog does not check yet. + // Skills are sorted by name. A skill whose name is not in the Agent Skills + // name format (see IsSkillName) is left out, so every name here is safe to + // print in a command. A Source must meet this. Install also skips reserved + // Windows device names and non-portable paths; the catalog checks neither yet. Skills []Skill // Version is the release version.