Files
Rusty ShacklefordandWillie 85bcf589a7 feat(sync): recognize and route agent-specific skill variants (#649)
* docs(proposals): propose agent-specific skill variants

Tracked repositories can ship different files for the same skill across
agents. Counting and syncing each copy independently sends incompatible
instructions to every target unless users maintain routing filters.

Propose repository-scoped logical identity and exact variant routing while
preserving source files, explicit controls, and existing naming behavior.
Leave activation and compatibility decisions for maintainer review.

Refs #648

* docs(proposals): clarify variant routing compatibility

Explain how recognition affects universal-only targets and how existing
per-variant overrides preserve the desired routing. Fix the precedence
between variant identity and generic host-style path inference.

Include analyze, move, and catalog consumers while preserving physical
identifiers and actual context-cost accounting.

Refs #648

* docs(proposals): clarify activation assumptions and scope

Make acceptance cases conditional on maintainer-approved recognition being
enabled for the tracked source. Separate the provisional implementation
estimate from this proposal-only PR so its size is clear.

Refs #648

* docs(proposals): qualify naming and opt-in assumptions

State that the reproduced routing behavior uses default flat target
naming. Leave any opt-in surface with the maintainer's activation decision
without ruling out future CLI or config fields.

Refs #648

* feat: recognize and route tracked agent skill variants

Count tracked variants as logical skills in source and repository summaries,
while preserving physical paths, sync operations, and context costs. Select
matching canonical agent variants, including account targets, through shared
discovery and selection with existing explicit routing precedence.

Refs #648

* fix: count logical skills in sync inventory summaries

Use shared logical and physical inventory counts in global and project sync
closing lines while preserving each target's operation totals.

Refs #648

* fix: preserve variant routing diagnostics across previews

Resolve target selections once so CLI and server previews keep ambiguity
reasons and physical skip counts. Explain managed variants removed by
automatic agent routing, and report logical inventory in doctor.

Keep warning rows read-only, document upgrade recovery in translated
filtering references, and remove the optional README-only addition.

Refs #648

* fix: align sync matrix previews with shared variant selection

Classify the full source selection once per target so matrix previews honor
canonical aliases, account identity, exact-agent routing, and ambiguous
variant skips. Preserve each rejected physical path's existing diagnostic
and draft filter disambiguation; skipped preview rows remain read-only.

Refs #648

* test: expect singular skill wording in doctor source checks

Keep the undiscovered-link count assertion aligned with logical source
inventory formatting for one skill in global and project mode.

Refs #648

* fix: retain selection diagnostics in catalog and diff verdicts

Report warning-only diff targets as requiring attention while preserving
local-only folder semantics. Reuse shared per-path selection diagnostics
in target-scoped catalogs without applying target naming to served paths.

Compute source counts once in summary callers and require variant counts
in the source status formatter.

Refs #648

* test: tighten tracked variant preview fixtures

Use a real committed tracked repository for status counts, verify sync
preview banners retain server warnings, and use emitted ambiguity reason
text in target preview fixtures.

Refs #648

* fix(sync): keep agent variants in shared target folders

Compatible targets sharing a skills folder need a combined selection so
Codex and universal do not prune one another in merge or copy mode.
Carry every shared reader through selection, pruning and previews,
including project targets, account identities and path aliases.

Require a matching skill suffix under two distinct agents before
inferring variants, keeping ordinary agent-named categories unrestricted.
Separate naming warnings from ambiguous selection in diff and explain
ambiguous removals. Add global/project regression coverage for repeated
sync, target-only sync, status JSON, diff and API consumers.

* fix(variants): align consumer counts, hints and diagnostics

Use logical source counts in list, tracked install and init summaries while matching audit caches against physical variants. Restore localized dashboard skip hints and explain symlink routing limits in status and doctor, including project and JSON output.

Document paired-agent detection and compatible shared-folder selection in the proposal, built-in skill and English plus translated command pages. Add focused regressions for the affected consumers.

* fix(sync): cover initial sync and ignored variant diagnostics

Init syncs a fresh in-memory config, so resolve shared target identities
before its first sync as well as when loading saved configuration.

A whole-directory symlink exposes ignored skills too. Diagnose agent
variants using physical inventory when active discovery has none, while
keeping logical counts and merge/copy selection restricted to enabled
skills. Cover global/project JSON status and doctor with regressions.

Reuse sourcewalk directory and canonical-path helpers so traversal guards
remain enforced and path resolution has a single existing owner.

* fix(variants): address conversion, counts and selection diagnostics

Ignore runtime agent identities when comparing convertible project targets and count logical skills during init. Keep selection warnings separate from naming warnings and preserve agent-named parent skills as ordinary skills.

Cover account sync/prune, project ambiguity and overrides in merge/copy modes, and init/status agreement. Focused verification ran in an isolated Linux Codex container, not the devcontainer, as authorized.

* refactor(variants): remove obsolete selection helpers

Remove the unused FilterSkillsByTarget wrapper and prune agent parameter, cache canonical target aliases once, and restore catalog skip ordering to avoid an unrelated behavior change. Completes the optional cleanup from PR 649 review.

* fix(ui): expose localized variant selection diagnostics

Show target warnings with source paths, localize ambiguity across preview and detail consumers, and let skipped preview rows add a physical exclude pattern. Show tracked repository variant counts alongside logical skills.

Drop display-only rendered cases while retaining mutation protection and plain TypeScript logic coverage. Inspect target states in Clean/Playful light/dark and Traditional Chinese.

* docs(variants): explain diagnostics and add sync runbook

Document doctor variant_routing and logical/physical counts, diff selection skips, and the nested parent-skill boundary. Keep all four command translations aligned and add isolated account/project E2E scenarios.

Both runbook steps passed in the authorized Linux Codex container with separate HOME/XDG/TMPDIR fixtures. Website typecheck and all five locale builds passed.

* test(config): use native paths in project conversion fixture

The shared-agent regression fixture appended slash-separated suffixes to Windows temporary paths, so project path matching returned no candidates. Build each path with filepath.Join and preserve all conversion assertions.

Focused project configuration tests passed in the authorized isolated Linux Codex container, not the devcontainer. Windows CI verifies the platform-specific fixture correction.

* fix(ui): recompute shared skill routing for filter previews

Evaluate edited include/exclude settings in the complete target map before
classifying the draft, so a formerly shared folder no longer keeps its old
union when the filters differ. Preserve the saved target configuration.

* fix(mcp): keep parent skills when peer variants route elsewhere

Do not treat an otherwise eligible variant for another agent as an unserved
nested skill. Continue blocking parent publication for explicitly filtered,
disabled, ambiguous, or invalid children so filters still hide their files.

* fix(ui): distinguish agent selection from naming skips

Expose separate naming and selection skip counts on target summaries.
Show each applicable remedy in target details and sync previews so ambiguity
keeps its filter/override guidance alongside naming and collision hints.

* fix(list): include disabled rows in tracked repository counts

Count the complete inventory that list displays, including ignored skills
and physical variants, without changing the enabled counts used by status,
install, or sync. Clone rows before counting to preserve disabled state.

* docs: align variant counts and routing recovery guidance

Show logical skill and physical variant counts in list/install documentation
and keep the four translations aligned with the filtering examples. Explain
that agent-named skill parents keep ordinary nested skills and retain the
devcontainer-only runbook environment. Removal warnings now recommend a
source-local override, since include filters cannot undo agent routing.

* fix(mcp): preserve explicit target restrictions on nested variants

Limit the differently routed child exemption to automatic layout inference.
Frontmatter targets and dashboard source-local overrides must still prevent
a parent from publishing files explicitly restricted to another target.

* fix(ui): fill target name in naming warning guidance

Pass the current target name when rendering localized warning hints so the
same naming guidance shown beside the controls does not expose a placeholder
in the warning banner. Verified in the populated target preview.

---------

Co-authored-by: Willie <egg358358@gmail.com>
2026-10-29 16:47:38 +08:00

458 lines
15 KiB
Go

package server
import (
"context"
"errors"
"fmt"
"maps"
"net/http"
"slices"
"sort"
"time"
"skillshare/internal/config"
"skillshare/internal/projects"
"skillshare/internal/resource"
"skillshare/internal/skillignore"
ssync "skillshare/internal/sync"
"skillshare/internal/utils"
)
// ignorePayload builds the common ignored-skills fields for JSON responses.
func ignorePayload(stats *skillignore.IgnoreStats) map[string]any {
r := ssync.NewIgnoreReport(stats, "", nil)
return map[string]any{
"ignored_count": r.IgnoredCount,
"ignored_skills": r.IgnoredSkills,
"ignore_root": r.IgnoreRoot,
"ignore_repos": r.IgnoreRepos,
}
}
type syncFailure = ssync.TargetFailure
type unmatchedInclude = ssync.TargetUnmatchedInclude
type syncTargetResult = ssync.TargetResult
func (s *Server) handleSync(w http.ResponseWriter, r *http.Request) {
start := time.Now()
s.mu.Lock()
defer s.mu.Unlock()
var body struct {
DryRun bool `json:"dryRun"`
Force bool `json:"force"`
Kind string `json:"kind"`
// Project limits the sync to one declared project's targets.
Project string `json:"project"`
}
if err := decodeJSON(w, r, &body, defaultJSONBodyLimit); errors.Is(err, errBodyTooLarge) {
return
}
// Any other decode error keeps the defaults: non-dry-run, non-force, empty kind (both)
if body.Kind != "" && body.Kind != kindSkill && body.Kind != kindAgent {
writeError(w, http.StatusBadRequest, "invalid kind: must be 'skill', 'agent', or empty")
return
}
if body.Project != "" {
root, lookupErr := lookupProjectRoot(s.cfg.Projects, body.Project)
if lookupErr != nil && !errors.Is(lookupErr, projects.ErrNotFound) {
writeError(w, http.StatusBadRequest, lookupErr.Error())
return
}
if _, ok := s.cfg.Projects[root]; !ok {
writeError(w, http.StatusBadRequest, "unknown project: "+body.Project)
return
}
body.Project = root
}
out, code, err := s.syncResources(start, body.DryRun, body.Force, body.Kind, body.Project)
if err != nil {
writeError(w, code, err.Error())
return
}
resp := map[string]any{
"results": out.results,
"warnings": out.warnings,
"unmatched": out.unmatched,
"failed": out.failed,
"folder_conflicts": out.folderConflicts,
"path_overlap": out.pathOverlap,
}
if len(out.skills) > 0 {
if contextCost := s.computeContextCost(out.skills); contextCost != nil {
resp["context_cost"] = contextCost
}
}
maps.Copy(resp, ignorePayload(out.ignoreStats))
maps.Copy(resp, agentIgnorePayload(s.agentsSource(), out.allAgents))
writeJSON(w, resp)
}
type syncOutcome struct {
results []syncTargetResult
warnings []string
unmatched []unmatchedInclude
failed []syncFailure
folderConflicts []config.SkillsFolderConflict
pathOverlap int // targets whose path overlap the folder conflicts don't explain
skills []ssync.DiscoveredSkill
ignoreStats *skillignore.IgnoreStats
allAgents []resource.DiscoveredResource // nil when agents were not synced
}
// folderConflicts returns the skills folders whose targets undo each other's
// sync, never nil, plus how many targets overlap in a way they don't explain.
// harmless is passed on to config.DetectPathOverlap.
func folderConflicts(targets map[string]config.TargetConfig, isProject bool, defaultMode string, harmless func(scanner, writer string) bool) ([]config.SkillsFolderConflict, int) {
return ssync.TargetFolderConflicts(targets, isProject, defaultMode, harmless)
}
// projectTargets returns the targets of the project declared under root.
func (s *Server) projectTargets(root string) map[string]config.TargetConfig {
abs := config.ExpandPath(root)
targets := map[string]config.TargetConfig{}
for name, target := range s.cfg.Targets {
if target.ProjectRoot() != "" && utils.PathsEqual(target.ProjectRoot(), abs) {
targets[name] = target
}
}
return targets
}
// syncResources links skills and agents (kind "" means both) into every target,
// or only the targets of the project declared under project, and logs the sync.
// A failed target, even when every target fails, is reported in failed and
// warnings; an error, with the HTTP status to report, means nothing could run.
// Callers must hold s.mu.
func (s *Server) syncResources(start time.Time, dryRun, force bool, kind, project string) (*syncOutcome, int, error) {
targets := s.cfg.Targets
if project != "" {
targets = s.projectTargets(project)
}
// Pre-check warnings via shared config validation
warnings, invalid, validErr := config.ValidateConfigForSync(s.cfg)
if validErr != nil {
return nil, http.StatusBadRequest, validErr
}
// A project target that cannot resolve (no path) is not in s.cfg.Targets;
// it fails like one with invalid settings.
maps.Copy(invalid, s.unresolvedTargets)
total := len(targets) + len(s.unresolvedTargets)
results := make([]syncTargetResult, 0)
failed := make([]syncFailure, 0)
unmatched := make([]unmatchedInclude, 0)
// A target with invalid settings fails alone: it is skipped for skills and agents.
runTargets := maps.Clone(targets)
for _, name := range slices.Sorted(maps.Keys(invalid)) {
if _, ok := targets[name]; !ok && s.unresolvedTargets[name] == nil {
continue
}
delete(runTargets, name)
err := invalid[name]
msg := name + ": invalid config: " + err.Error()
warnings = append(warnings, msg)
failed = append(failed, syncFailure{Target: name, Part: "config", Error: err.Error(), Message: msg})
}
if !dryRun {
warnings = append(warnings, s.backupBeforeSync(runTargets, kind != kindAgent, kind != kindSkill)...)
}
var ignoreStats *skillignore.IgnoreStats
var allSkills []ssync.DiscoveredSkill
var sourceIncomplete bool
// Skill sync (skip when kind == "agent")
if kind != kindAgent {
var err error
if !dryRun {
if adoptErr := s.adoptMovedSkills(); adoptErr != nil {
warnings = append(warnings, "install metadata not updated: "+adoptErr.Error())
}
}
walk := s.skillsWalk()
allSkills, ignoreStats, err = ssync.DiscoverSourceSkillsWithStatsAndContext(s.cfg.EffectiveSkillsSource(), walk)
if err != nil {
return nil, http.StatusInternalServerError, fmt.Errorf("failed to discover skills: %w", err)
}
sourceIncomplete = walk.Follow.Incomplete()
warnings = append(warnings, ssync.SourceLinkWarnings(walk, sourceIncomplete)...)
if len(allSkills) == 0 {
warnings = append(warnings, "source directory is empty (0 skills)")
}
}
discovery := ssync.TargetDiscovery{Skills: allSkills, IgnoreStats: ignoreStats, SourceIncomplete: sourceIncomplete}
if kind != kindSkill {
discovery.Agents, discovery.AllAgents, discovery.AgentsAvailable = ssync.DiscoverTargetAgents(s.agentsSource())
}
kinds := []string(nil)
if kind != "" {
kinds = []string{kind}
}
executed, err := ssync.ExecuteTargets(context.Background(), s.cfg, runTargets, ssync.ExecuteOptions{
Kinds: kinds, DryRun: dryRun, Force: force, ProjectMode: s.IsProjectMode(), ProjectRoot: s.projectRoot,
AgentsSource: s.agentsSource(), Discovery: discovery, BuiltinAgentTargets: s.builtinAgentTargets(),
ResolveExtension: s.resolveExtensionSpec, DiagnosticsTargets: s.cfg.Targets,
MovedTargets: ssync.MovedProjectTargets(s.projectCfg, s.cfg.Targets),
})
if err != nil {
return nil, http.StatusInternalServerError, err
}
results = executed.Results
failed = append(failed, executed.Failed...)
unmatched = executed.Unmatched
warnings = append(warnings, executed.Warnings...)
// Log the sync operation. A target counts once however many parts failed.
failedNames := failedTargetNames(failed)
logArgs := map[string]any{
"targets_total": total,
"targets_failed": len(failedNames),
"dry_run": dryRun,
"force": force,
"kind": kind,
"scope": "ui",
}
if len(failedNames) > 0 {
logArgs["failed_targets"] = failedNames
}
if project != "" {
logArgs["project"] = project
}
status := "ok"
switch {
case len(failedNames) >= total && len(failedNames) > 0:
status = "error"
case len(failedNames) > 0:
status = "partial"
}
s.writeOpsLog("sync", status, start, logArgs, "")
return &syncOutcome{results: results, warnings: warnings, unmatched: unmatched, failed: failed, folderConflicts: executed.FolderConflicts, pathOverlap: executed.PathOverlap, skills: executed.Skills, ignoreStats: executed.IgnoreStats, allAgents: executed.AllAgents}, 0, nil
}
// failedTargetNames returns the distinct targets in failed, sorted.
func failedTargetNames(failed []syncFailure) []string {
names := make([]string, 0, len(failed))
for _, f := range failed {
names = append(names, f.Target)
}
slices.Sort(names)
return slices.Compact(names)
}
// backupBeforeSync snapshots the target folders a sync may overwrite, as the
// CLI does, then trims old snapshots. Project mode backs up agents only, like
// `sync -p`. Failures come back as warnings. Callers must hold s.mu.
func (s *Server) backupBeforeSync(targets map[string]config.TargetConfig, skills, agents bool) []string {
return ssync.BackupTargets(targets, skills, agents, ssync.BackupTargetsOptions{
Dir: s.backupDir(), Retention: s.backupRetention(), ProjectMode: s.IsProjectMode(), ProjectRoot: s.projectRoot,
BuiltinAgentTargets: s.builtinAgentTargets(),
})
}
func (s *Server) computeContextCost(skills []ssync.DiscoveredSkill) map[string]any {
type tokenPair struct{ always, onDemand int }
groupMap := make(map[tokenPair][]string)
type skillToken struct {
name string
descTokens int
bodyTokens int
}
var maxAlways, maxOnDemand int
var worstAlwaysSkills, worstOnDemandSkills []skillToken
var worstAlwaysTarget, worstOnDemandTarget string
cfgMode := s.cfg.Mode
if cfgMode == "" {
cfgMode = "merge"
}
for name, target := range s.cfg.Targets {
filtered, fErr := ssync.TargetSkills(name, target, cfgMode, s.cfg.EffectiveSkillsSource(), skills)
if fErr != nil {
filtered = skills
}
var at, ot int
var perSkill []skillToken
for _, sk := range filtered {
at += sk.DescTokens
ot += sk.BodyTokens
perSkill = append(perSkill, skillToken{
name: sk.FlatName,
descTokens: sk.DescTokens,
bodyTokens: sk.BodyTokens,
})
}
groupMap[tokenPair{at, ot}] = append(groupMap[tokenPair{at, ot}], name)
if at > maxAlways {
maxAlways = at
worstAlwaysSkills = perSkill
worstAlwaysTarget = name
} else if at > 0 && at == maxAlways && name < worstAlwaysTarget {
worstAlwaysTarget = name
worstAlwaysSkills = perSkill
}
if ot > maxOnDemand {
maxOnDemand = ot
worstOnDemandSkills = perSkill
worstOnDemandTarget = name
} else if ot > 0 && ot == maxOnDemand && name < worstOnDemandTarget {
worstOnDemandTarget = name
worstOnDemandSkills = perSkill
}
}
type group struct {
Targets []string `json:"targets"`
AlwaysLoadedTokens int `json:"always_loaded_tokens"`
OnDemandTokens int `json:"on_demand_tokens"`
}
groups := make([]group, 0, len(groupMap))
for pair, names := range groupMap {
sort.Strings(names)
groups = append(groups, group{
Targets: names,
AlwaysLoadedTokens: pair.always,
OnDemandTokens: pair.onDemand,
})
}
sort.Slice(groups, func(i, j int) bool { return groups[i].Targets[0] < groups[j].Targets[0] })
budget := s.cfg.ContextBudget
if s.IsProjectMode() && s.projectCfg != nil {
budget = s.projectCfg.ContextBudget
}
result := map[string]any{"groups": groups}
type offender struct {
Name string `json:"name"`
Tokens int `json:"tokens"`
}
type warning struct {
Type string `json:"type"`
Target string `json:"target"`
Actual int `json:"actual"`
Budget int `json:"budget"`
TopOffenders []offender `json:"top_offenders"`
}
topN := func(perSkill []skillToken, n int, byDesc bool) []offender {
type pair struct {
name string
tokens int
}
pairs := make([]pair, 0, len(perSkill))
for _, sk := range perSkill {
t := sk.bodyTokens
if byDesc {
t = sk.descTokens
}
if t > 0 {
pairs = append(pairs, pair{sk.name, t})
}
}
sort.Slice(pairs, func(i, j int) bool { return pairs[i].tokens > pairs[j].tokens })
if len(pairs) > n {
pairs = pairs[:n]
}
out := make([]offender, len(pairs))
for i, p := range pairs {
out[i] = offender{Name: p.name, Tokens: p.tokens}
}
return out
}
var warns []warning
if t := budget.AlwaysLoadedThreshold(); t > 0 && maxAlways > t {
warns = append(warns, warning{
Type: "always_loaded", Target: worstAlwaysTarget,
Actual: maxAlways, Budget: t,
TopOffenders: topN(worstAlwaysSkills, 3, true),
})
}
if t := budget.OnDemandThreshold(); t > 0 && maxOnDemand > t {
warns = append(warns, warning{
Type: "on_demand", Target: worstOnDemandTarget,
Actual: maxOnDemand, Budget: t,
TopOffenders: topN(worstOnDemandSkills, 3, false),
})
}
if len(warns) > 0 {
result["warnings"] = warns
}
return result
}
type diffItem struct {
Skill string `json:"skill"`
Action string `json:"action"` // "link", "update", "skip", "prune", "local", "kept" (the skill stays at a legacy entry; force changes nothing)
Reason string `json:"reason"` // human-readable description
Kind string `json:"kind,omitempty"`
}
type diffTarget struct {
Target string `json:"target"`
Warnings []string `json:"warnings,omitempty"`
Items []diffItem `json:"items"`
SkippedCount int `json:"skippedCount,omitempty"`
CollisionCount int `json:"collisionCount,omitempty"`
// NamingSkippedCount is how many of SkippedCount break naming rules rather than collide.
NamingSkippedCount int `json:"namingSkippedCount,omitempty"`
}
func (s *Server) handleDiff(w http.ResponseWriter, r *http.Request) {
// Snapshot config under RLock, then release before slow I/O.
s.mu.RLock()
source := s.cfg.EffectiveSkillsSource()
agentsSource := s.agentsSource()
globalMode := s.cfg.Mode
ignorePatterns := ssync.EffectiveFileIgnorePatterns(s.cfg.Ignore)
targets := s.cloneTargets()
conflicts, _ := folderConflicts(targets, s.IsProjectMode(), globalMode, nil)
s.mu.RUnlock()
if globalMode == "" {
globalMode = "merge"
}
filterTarget := r.URL.Query().Get("target")
discovered, ignoreStats, err := ssync.DiscoverSourceSkillsWithStats(source, s.skillsWalk())
if err != nil {
writeError(w, http.StatusInternalServerError, err.Error())
return
}
diffs := make([]diffTarget, 0)
for name, target := range targets {
if filterTarget != "" && filterTarget != name {
continue
}
diffs = append(diffs, s.computeTargetDiff(name, target, discovered, globalMode, source, ignorePatterns))
}
diffs = s.appendAgentDiffs(diffs, targets, agentsSource, filterTarget)
resp := map[string]any{"diffs": diffs, "folder_conflicts": conflicts}
maps.Copy(resp, ignorePayload(ignoreStats))
maps.Copy(resp, agentIgnorePayload(agentsSource, nil))
writeJSON(w, resp)
}