Files
pipelines/internal/stage/skills.go
T
alan 85a7284fba Stages: optional skills block loading pinned library skills per visit
A model stage may declare a skills block (name or name@N per line).
Drafts and publications are validated against the server skill library;
human/ntfy stages reject the block. Each run resolves unpinned names to the
latest version at admission and records name@N per stage (run
stage_skills, step skills); retry reuses the recorded versions. Every visit
gets a private read-only copy of exactly those bundles under the run's
artifact tree, and the harness loads only them: Claude via a session
plugin (--plugin-dir, Skill tool with per-skill allow rules, bundled skills
off, ambient loads fail the visit), Pi via --skill under --no-skills, and
OpenCode via per-session instruction entries (V2 only). The app shows the
block in the stage definition and the resolved versions on each visit.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-25 19:17:16 +00:00

131 lines
3.8 KiB
Go

package stage
import (
"fmt"
"strconv"
"strings"
"git.a-wan.net/alan/pipelines/internal/skills"
)
// SkillRef is one entry of a model stage's `skills` block: a server skill
// library name, pinned to Version when it is positive and otherwise resolved to
// the library's latest version when the run starts.
type SkillRef struct {
Name string
Version int
}
// String renders the entry as written in a definition: name or name@N.
func (r SkillRef) String() string {
if r.Version > 0 {
return r.Name + "@" + strconv.Itoa(r.Version)
}
return r.Name
}
// ParseSkillRef parses one `name` or `name@N` entry. N must be a positive
// integer without sign or leading zeros.
func ParseSkillRef(text string) (SkillRef, error) {
text = strings.TrimSpace(text)
name, version, pinned := strings.Cut(text, "@")
if err := skills.ValidateName(name); err != nil {
return SkillRef{}, fmt.Errorf("invalid skill %q: %w", text, err)
}
ref := SkillRef{Name: name}
if pinned {
n, err := strconv.Atoi(version)
if err != nil || n <= 0 || strconv.Itoa(n) != version {
return SkillRef{}, fmt.Errorf("invalid skill %q: the version after @ must be a positive integer", text)
}
ref.Version = n
}
return ref, nil
}
// SkillRefs parses a model stage's optional `skills` block, one entry per
// line. An absent or empty block configures no skills. A skill may appear
// only once, pinned or not.
func SkillRefs(def Definition) ([]SkillRef, error) {
text, present := def.Blocks["skills"]
if !present {
return nil, nil
}
var refs []SkillRef
seen := map[string]bool{}
for _, line := range strings.Split(text, "\n") {
if strings.TrimSpace(line) == "" {
continue
}
ref, err := ParseSkillRef(line)
if err != nil {
return nil, err
}
if seen[ref.Name] {
return nil, fmt.Errorf("duplicate skill %s", ref.Name)
}
seen[ref.Name] = true
refs = append(refs, ref)
}
return refs, nil
}
// errSkillsModelOnly is the clear diagnostic for a skills block on a stage that
// has no model session to load skills into.
func errSkillsModelOnly(kind string) error {
return fmt.Errorf("skills blocks are supported only on model stages, not %s stages", kind)
}
// ClaudeSkillPlugin is the name of the per-visit plugin a Claude stage's
// configured skills are loaded from; Claude Code names its skills
// ClaudeSkillPlugin:<skill>.
const ClaudeSkillPlugin = "pipelines-stage"
// SkillBundle is one resolved skill copied for a single stage visit. Dir holds
// the bundle (SKILL.md and its references/, scripts/ and assets/).
type SkillBundle struct {
Name string
Version int
Dir string
}
// SkillSet is the runtime-provided skill material for one model visit. Root is
// the visit's private skills directory: it is also a Claude Code plugin root
// (.claude-plugin/plugin.json) whose skills/<name>/ entries are the bundles.
// A nil or empty set means the stage configured no skills.
type SkillSet struct {
Root string
Bundles []SkillBundle
}
// Empty reports whether the set configures no skills.
func (s *SkillSet) Empty() bool { return s == nil || len(s.Bundles) == 0 }
// Names lists name@version for every bundle, in definition order.
func (s *SkillSet) Names() []string {
if s.Empty() {
return nil
}
names := make([]string, 0, len(s.Bundles))
for _, b := range s.Bundles {
names = append(names, b.Name+"@"+strconv.Itoa(b.Version))
}
return names
}
// claudeAllowed reports whether a Claude Code Skill tool call names one of the
// configured skills. Only the plugin-qualified name counts: a bare name could
// be an ambient skill of the same name.
func (s *SkillSet) claudeAllowed(name string) bool {
if s.Empty() {
return false
}
name = strings.TrimPrefix(strings.TrimSpace(name), "/")
for _, b := range s.Bundles {
if name == ClaudeSkillPlugin+":"+b.Name {
return true
}
}
return false
}