Files
skillshare/internal/config/projects.go
T
Willie b0d87fba28 feat(sync): make prefixed target naming misconfigurations visible (#503)
* feat(sync): make prefixed target naming misconfigurations visible

Prefixed naming (#497) works, but several of its failure modes only showed up
at sync time or gave advice that cannot be followed:

- A collision that only prefixing creates told the user to "rename one in
  SKILL.md", which a tracked skill cannot do, and never said the prefix caused
  it. The hint now names the prefixing and points at renaming the other skill or
  re-tracking the repo with --name.
- `target add` silently wrote mode: copy when the inherited naming is prefixed.
  The CLI now says so.
- A target that resolves to prefixed outside copy mode was reported only by
  sync. status, doctor and target list (plain and JSON) now flag it, and the
  sync/validation and `--skills=true` errors say how to fix it.
- The dim "duplicate skill names (isolated by ...)" line printed on every sync
  even when nothing was wrong, and before a failing sync. Isolated duplicates
  are not a problem, so it is dropped; the website docs still showed an
  outdated form of this output and are corrected in all locales.

* fix(status): flag prefixed-outside-copy in the TUI list and status --json

Codex review of #503 found two surfaces the new check missed:

- The interactive `target list` (the default on a terminal) never read the
  item's naming error, so a prefixed + merge target looked healthy there. The
  list row now marks the mode with "!" and the detail panel shows the fix.
- `status --json` (global and project) builds its target entries directly, so
  scripts got no signal. Each target now carries a `warning` string, matching
  `target list --json`.

* docs(troubleshooting): sync no longer prints a message for isolated duplicates

The collision troubleshooting tip still said sync shows an info message when
filters isolate duplicate skill names. That line was dropped, so the tip now
says sync prints nothing about them. Codex review of #503 flagged the stale
English page and its four translations.

* fix(config): point managed-project naming errors at the project settings

The "set mode: copy on the target" hint is wrong for a target a global
`projects:` entry expands into: it cannot be edited with `target` or under
`targets:`, and the mode lives in the project's skills settings. The hint now
depends on whether the target belongs to a managed project (Codex review of
#503).

The naming/mode check becomes a TargetConfig method so status, doctor and
target list share it, which also lets the hint see the project root. Along the
way: doctor reuses it, the TUI list renders the mode once, and
reportTargetAdded takes a flag instead of comparing a mode string.

* fix(config): hint the project settings for managed-project naming errors

ProjectNamingError (the config-load check for a global `projects:` entry) still
returned the bare naming error, so a project that inherits prefixed with a
non-copy mode got no fix. It now names the project's skills settings, like the
expanded targets do. The built-in skill's sync reference also mentions that
status, doctor and target list flag the misconfiguration before sync.

Found by the second /code-review pass on #503.

* fix(sync): detect prefix collisions from the colliding paths

reportCollisions decided a collision came from prefixing when its name was not
also a duplicate in the source. A tracked skill that joined two ordinary
skills already sharing the name was therefore sent to the "rename one in
SKILL.md" hint, which a tracked skill cannot follow (Codex review of #503).

A collision on a prefixed target that includes a tracked path now gets the
tracked-skill hint, whatever else shares the name. The wording no longer claims
prefixing renamed the skill, since a name that already starts with the repo
name is unchanged.

* fix(sync): use discovered repo metadata for the tracked-skill hint

The collision hint treated any path segment starting with "_" as a tracked
repo, so an ordinary skill under a folder such as org/_drafts (no .git) was
told it could not be renamed and to re-track a repo that does not exist. It now
uses DiscoveredSkill.IsInRepo, the same signal prefixed naming uses.

The prefixed docs also gain the fix for a target a global `projects:` entry
expands into (projects.<root>.skills.mode: copy), since `target` cannot edit
those. Both from Codex review of #503.

* fix(sync): keep both collision hints when targets disagree

The same name can clash on a prefixed target with a tracked skill and on
another target between ordinary skills only. The prefixed flag was OR-ed across
targets, so the second case lost its rename/filter hint. Track both kinds
separately while grouping, and print each hint that applies (Codex review of
#503).

* fix(sync): keep the rename hint inside a mixed collision group

When a tracked skill and two ordinary skills share a prefixed name, re-tracking
the repo fixes only the tracked one; the ordinary pair still clashes and is
excluded. The group now also shows the rename/filter hint when at least two of
its participants are not tracked (Codex review of #503).

* fix(sync): offer filters in the tracked-skill collision hint

Two tracked skills of the same repo with the same name collide after
prefixing, and neither renaming a tracked skill nor re-tracking the repo
separates them. The hint now also names include/exclude filters, so every
collision on a prefixed target has an actionable remedy (Codex review of #503).

* docs(sync-modes): limit the target list claim to targets under targets

target list builds from the config without project expansion, so it cannot
flag a target a `projects:` entry expands into; status and doctor can. The
prefixed paragraph and the built-in skill reference said all three flag it
(Codex review of #503).
2026-10-10 00:00:23 +08:00

346 lines
12 KiB
Go

package config
import (
"cmp"
"fmt"
"maps"
"os"
"path/filepath"
"reflect"
"slices"
"sort"
"strings"
)
// ProjectTargetSep joins a project name and a tool into a target name. User-defined
// target names cannot contain it, so a project target never shadows one.
const ProjectTargetSep = "@"
// ManagedProject is a project folder the global config syncs skills and agents into,
// using each tool's project paths from targets.yaml. The folder needs no .skillshare/.
// Its MCP servers live under mcp.projects, keyed by the same root.
type ManagedProject struct {
// Name labels the project's targets. Defaults to the folder's base name.
Name string `yaml:"name,omitempty"`
Targets []string `yaml:"targets,omitempty"`
// Skills and Agents turn that resource on when present; an empty value syncs all.
Skills *ResourceTargetConfig `yaml:"skills,omitempty"`
Agents *ResourceTargetConfig `yaml:"agents,omitempty"`
}
// ProjectTargetName returns the target name of one tool in a project.
func ProjectTargetName(project, tool string) string {
return project + ProjectTargetSep + tool
}
// SplitProjectTarget splits a project target name. ok is false for an ordinary target.
func SplitProjectTarget(name string) (project, tool string, ok bool) {
i := strings.LastIndex(name, ProjectTargetSep)
if i < 0 {
return "", name, false
}
return name[:i], name[i+1:], true
}
// ProjectRoot is the folder a project target writes into, or "" for an ordinary target.
func (tc TargetConfig) ProjectRoot() string { return tc.projectRoot }
// ProjectName returns the effective name of the project declared under root.
func (p ManagedProject) ProjectName(root string) string {
if p.Name != "" {
return p.Name
}
return filepath.Base(expandPath(root))
}
// ProjectToolGroup is the tools of one project that write skills to the same folder.
type ProjectToolGroup struct {
Tools []string
SkillsPath string // absolute
AgentsPath string // absolute; "" when no tool in the group has a project agents path
}
// Target is the tool the group's target is named after: the one that takes agents,
// else the first listed.
func (g ProjectToolGroup) Target() string {
for _, tool := range g.Tools {
if _, ok := LookupProjectAgentTarget(tool); ok {
return tool
}
}
return g.Tools[0]
}
// ToolGroups resolves the project's tools to folders under root, merging tools that
// share a skills folder such as .agents/skills so it is written once.
func (p ManagedProject) ToolGroups(root string) ([]ProjectToolGroup, error) {
root = expandPath(root)
var groups []ProjectToolGroup
byPath := map[string]int{}
for _, tool := range p.Targets {
skills, ok := LookupProjectTarget(tool)
if !ok {
return nil, fmt.Errorf("unknown target %q", tool)
}
path := filepath.Join(root, skills.Path)
i, seen := byPath[path]
if !seen {
i = len(groups)
byPath[path] = i
groups = append(groups, ProjectToolGroup{SkillsPath: path})
}
groups[i].Tools = append(groups[i].Tools, tool)
// ponytail: one agents folder per group. targets.yaml has at most one tool with
// project agents per shared skills folder; split the group if that changes.
if agents, ok := LookupProjectAgentTarget(tool); ok && groups[i].AgentsPath == "" {
groups[i].AgentsPath = filepath.Join(root, agents.Path)
}
}
return groups, nil
}
// MissingProjects lists declared roots that are not folders. They get no targets, so a
// sync never creates a project that was moved or deleted.
func (c *Config) MissingProjects() []string {
var missing []string
for root := range c.Projects {
if info, err := os.Stat(expandPath(root)); err != nil || !info.IsDir() {
missing = append(missing, root)
}
}
sort.Strings(missing)
return missing
}
// expandProjects adds each project's targets to c.Targets, where sync, status, diff and
// doctor find them like any other. Save leaves them out again.
func (c *Config) expandProjects() error {
names := map[string]string{}
for root, project := range c.Projects {
abs := expandPath(root)
if !filepath.IsAbs(abs) {
return fmt.Errorf("projects: %s must be an absolute path or start with ~", root)
}
name := project.ProjectName(root)
if strings.ContainsAny(name, ProjectTargetSep+`/\`) {
return fmt.Errorf("projects: %s: name %q cannot contain %s, / or \\", root, name, ProjectTargetSep)
}
if other, taken := names[name]; taken {
return fmt.Errorf("projects: %s and %s are both named %q; set name on one of them", other, root, name)
}
names[name] = root
for _, rc := range []*ResourceTargetConfig{project.Skills, project.Agents} {
if rc != nil && rc.Path != "" {
return fmt.Errorf("projects: %s: path is not allowed; each target's project path is used", root)
}
}
groups, err := project.ToolGroups(root)
if err != nil {
return fmt.Errorf("projects: %s: %w", root, err)
}
if info, err := os.Stat(abs); err != nil || !info.IsDir() {
continue
}
if project.Skills == nil && project.Agents == nil {
continue
}
for _, group := range groups {
if project.Skills == nil && group.AgentsPath == "" {
continue
}
// ponytail: every target syncs skills, so an agents-only project excludes
// them all and is left with an empty skills folder. Teach the sync loops
// to skip skills if that folder ever matters.
// It syncs no skill, so it takes a naming every mode accepts rather than inherit one.
skills := ResourceTargetConfig{Exclude: []string{"*"}, TargetNaming: "flat"}
if project.Skills != nil {
skills = *project.Skills
}
skills.Path = group.SkillsPath
target := TargetConfig{Skills: &skills, projectRoot: abs, defaultTargetNaming: c.TargetNaming}
if project.Agents != nil && group.AgentsPath != "" {
agents := *project.Agents
agents.Path = group.AgentsPath
target.Agents = &agents
}
if c.Targets == nil {
c.Targets = map[string]TargetConfig{}
}
c.Targets[ProjectTargetName(name, group.Target())] = target
}
}
return nil
}
// OwnTargets returns the targets declared under targets, without the project ones.
func (c *Config) OwnTargets() map[string]TargetConfig {
own := make(map[string]TargetConfig, len(c.Targets))
for name, target := range c.Targets {
if target.projectRoot == "" {
own[name] = target
}
}
return own
}
// withoutProjectTargets returns c as it should be written: project targets come from
// projects, never from targets, and a path derived from config_dir stays derived.
func (c *Config) withoutProjectTargets() *Config {
out := *c
out.Targets = c.OwnTargets()
for name, target := range out.Targets {
out.Targets[name] = target.withoutDerivedPaths()
}
return &out
}
// LoadWithoutProjects loads the config for commands that edit or collect from targets.
// Project targets are edited under projects, and a project's own skills stay in it.
func LoadWithoutProjects() (*Config, error) {
cfg, err := Load()
if err != nil {
return nil, err
}
cfg.Targets = cfg.OwnTargets()
return cfg, nil
}
// ValidateProjects reports whether projects could replace c.Projects, without changing c.
func (c *Config) ValidateProjects(projects map[string]ManagedProject) error {
probe := Config{Targets: c.OwnTargets(), Projects: projects, TargetNaming: c.TargetNaming}
for _, project := range projects {
for _, rc := range []*ResourceTargetConfig{project.Skills, project.Agents} {
if rc != nil && !IsValidSyncMode(rc.Mode) {
return fmt.Errorf("invalid sync mode %q (valid: %s)", rc.Mode, strings.Join(ValidSyncModes, ", "))
}
}
}
if err := c.ProjectNamingError(projects, c.Mode); err != nil {
return err
}
return probe.expandProjects()
}
// ProjectNamingError checks each project's effective skills naming against its
// effective mode under global mode, which the targets it expands into will inherit.
// It covers projects whose folder is missing, which expand into no target yet.
func (c *Config) ProjectNamingError(projects map[string]ManagedProject, mode string) error {
for _, root := range slices.Sorted(maps.Keys(projects)) {
if sk := projects[root].Skills; sk != nil && sk.IsEnabled() {
if err := TargetNamingModeConfigError(cmp.Or(sk.TargetNaming, c.TargetNaming), cmp.Or(sk.Mode, mode), root); err != nil {
return fmt.Errorf("projects: %s: %w", root, err)
}
}
}
return nil
}
// ConvertibleProject is a group of ordinary targets whose folders are the tool paths of
// one project folder, so projects can declare them instead.
type ConvertibleProject struct {
Root string
Targets []string // target names, sorted
Project ManagedProject
}
// ConvertibleProjects finds targets with a custom path such as ~/work/app/.claude/skills.
// A folder is offered only when its targets agree on mode and filters, because a project
// has one set of each.
func (c *Config) ConvertibleProjects() []ConvertibleProject {
specs, err := loadTargetSpecs()
if err != nil {
return nil
}
home, _ := os.UserHomeDir()
declared := map[string]bool{}
for root := range c.Projects {
declared[expandPath(root)] = true
}
withoutPath := func(rc ResourceTargetConfig) ResourceTargetConfig {
rc.Path = ""
return rc
}
byRoot := map[string]*ConvertibleProject{}
mixed := map[string]bool{}
for _, name := range sortedTargetNames(c.OwnTargets()) {
target := c.Targets[name]
skills := target.SkillsConfig()
root, tool := projectToolForPath(specs, name, skills.Path)
if root == "" || root == home || declared[root] {
continue
}
var agents *ResourceTargetConfig
if ac := target.AgentsConfig(); ac.Path != "" {
known, ok := LookupProjectAgentTarget(tool)
if !ok || ac.Path != filepath.Join(root, known.Path) {
mixed[root] = true
continue
}
rc := withoutPath(ac)
agents = &rc
}
sk := withoutPath(skills)
sk.TargetNaming = ""
if target.Skills != nil {
sk.TargetNaming = target.Skills.TargetNaming
}
found := byRoot[root]
if found == nil {
byRoot[root] = &ConvertibleProject{Root: root, Targets: []string{name}, Project: ManagedProject{Targets: []string{tool}, Skills: &sk, Agents: agents}}
continue
}
if !reflect.DeepEqual(*found.Project.Skills, sk) || (agents != nil && found.Project.Agents != nil && !reflect.DeepEqual(*found.Project.Agents, *agents)) {
mixed[root] = true
continue
}
found.Targets = append(found.Targets, name)
found.Project.Targets = append(found.Project.Targets, tool)
if found.Project.Agents == nil {
found.Project.Agents = agents
}
}
var out []ConvertibleProject
for root, found := range byRoot {
if !mixed[root] {
out = append(out, *found)
}
}
sort.Slice(out, func(i, j int) bool { return out[i].Root < out[j].Root })
return out
}
func sortedTargetNames(targets map[string]TargetConfig) []string {
names := make([]string, 0, len(targets))
for name := range targets {
names = append(names, name)
}
sort.Strings(names)
return names
}
// projectToolForPath reads a skills folder as <root>/<a tool's project path>. The longest
// project path wins; among tools sharing it, the one named like the target, then
// universal, then the first listed.
func projectToolForPath(specs []targetSpec, targetName, path string) (root, tool string) {
longest := 0
for _, spec := range specs {
rel := normalizeTargetPath(spec.Skills.Project)
// ponytail: a bare folder name such as "skills" would match almost any custom
// path, so only tool paths with a parent folder are recognised.
if !strings.ContainsRune(rel, filepath.Separator) || !strings.HasSuffix(path, string(filepath.Separator)+rel) {
continue
}
better := len(rel) > longest
if len(rel) == longest {
better = tool != targetName && (targetSpecMatchesName(spec, targetName) || (spec.Name == "universal" && !strings.EqualFold(tool, "universal")))
}
if better {
longest = len(rel)
root, tool = strings.TrimSuffix(path, string(filepath.Separator)+rel), spec.Name
}
}
return root, tool
}