Files
archie/internal/infrastructure/skillcurator/store.go
T
sam db346b159d fix(webui): mark cookies Secure behind a TLS proxy; run gosec in the gate (#1919)
Cookies took Secure from r.TLS, which is nil behind a proxy that terminates
TLS, so sign-in cookies went out without it. Every dashboard cookie now goes
through one builder that reads the scheme the way the origin check does.

The worktree reset and the agent worker's chown walk use os.Root, so a
symlink swapped in mid-walk can't point them outside the worktree.

gosec runs in golangci-lint, tests included. G104, G115, G204 and G304 are
excluded in config; each remaining finding is fixed or suppressed at the site
with its reason.
2026-11-13 03:16:15 +11:00

105 lines
3.2 KiB
Go

// Package skillcurator implements domain/curator.CuratorEngine for skill
// maintenance.
package skillcurator
import (
"context"
"fmt"
"os"
"path/filepath"
"github.com/samcharles93/archie-core/internal/domain/curator"
"github.com/samcharles93/archie-core/internal/skill"
)
// skillsDir mirrors internal/skill's own unexported constant of the same
// name and value -- the discovery convention is fixed
// across both packages, not configurable per caller.
const skillsDir = ".agents/skills"
// skillFile is the file name every skill's definition lives in, within
// its own directory under skillsDir.
const skillFile = "SKILL.md"
// Store implements curator.SkillStore over root's */SKILL.md files.
type Store struct {
root string
}
// NewStore builds a Store rooted at root.
func NewStore(root string) *Store {
return &Store{root: root}
}
func (s *Store) skillsPath() string {
return filepath.Join(s.root, skillsDir)
}
func (s *Store) skillPath(name string) string {
return filepath.Join(s.skillsPath(), name, skillFile)
}
// List returns every skill directory with a SKILL.md. A missing directory
// returns an empty list.
func (s *Store) List(_ context.Context) ([]curator.SkillRef, error) {
entries, err := os.ReadDir(s.skillsPath())
if os.IsNotExist(err) {
return nil, nil
}
if err != nil {
return nil, fmt.Errorf("skillcurator: reading %s: %w", s.skillsPath(), err)
}
var refs []curator.SkillRef
for _, e := range entries {
if !e.IsDir() {
continue
}
path := s.skillPath(e.Name())
if _, err := os.Stat(path); err != nil {
continue
}
refs = append(refs, curator.SkillRef{Name: e.Name(), Path: path})
}
return refs, nil
}
// Read returns the skill's raw SKILL.md. Description is empty when the
// frontmatter does not parse.
func (s *Store) Read(_ context.Context, name string) (curator.Skill, error) {
path := s.skillPath(name)
data, err := os.ReadFile(path)
if err != nil {
return curator.Skill{}, fmt.Errorf("skillcurator: reading %s: %w", path, err)
}
sk := curator.Skill{Name: name, Content: string(data)}
if fm, _, err := skill.Parse(data); err == nil {
sk.Description = fm.Description
}
return sk, nil
}
// Write overwrites an existing skill's SKILL.md.
func (s *Store) Write(_ context.Context, sk curator.Skill) error {
dir := filepath.Join(s.skillsPath(), sk.Name)
if info, err := os.Stat(dir); err != nil || !info.IsDir() {
return fmt.Errorf("skillcurator: skill %q does not exist under %s", sk.Name, s.skillsPath())
}
if err := os.WriteFile(s.skillPath(sk.Name), []byte(sk.Content), 0o644); err != nil { //nolint:gosec // G306: agents read skills as another user.
return fmt.Errorf("skillcurator: writing %s: %w", s.skillPath(sk.Name), err)
}
return nil
}
// Delete removes the named skill's entire directory (SKILL.md, plugins/,
// anything else under it) -- not just SKILL.md, matching "delete a
// skill." Deleting a name that does not exist is not an error: the end
// state (skill absent) already holds.
func (s *Store) Delete(_ context.Context, name string) error {
dir := filepath.Join(s.skillsPath(), name)
if err := os.RemoveAll(dir); err != nil {
return fmt.Errorf("skillcurator: deleting %s: %w", dir, err)
}
return nil
}