Files
skillshare/internal/server/handler_targets.go
T
Willie ec2a8ab3aa fix(sync): close gaps found while simulating the prefixed naming switch (#504)
* fix(sync): close gaps found while simulating the prefixed naming switch

Simulating the #497 reporter's switch from merge + flat to copy + prefixed
turned up four problems:

- `target X --mode copy --target-naming prefixed` applied only --mode, since
  the setter dispatch returned after the first flag. Both flags given
  together now check the final pair, then apply in an order that keeps the
  saved pair valid (copy mode first, any other mode last).
- Skipped skills were only counted, so a prefixed name over 64 characters
  vanished with "1 skill(s) skipped". Sync now names the first five with
  their reason; the cap keeps huge sources from flooding the output.
- In copy mode a merge-mode link to the skill was not a managed entry, so
  when a local folder held the new name the old link was pruned and the
  skill left the target, contrary to the documented "old managed entry is
  preserved". Copy mode now migrates such links like managed copies.
- PATCH /api/targets validated the mode/naming pair against the current
  skills state, so disabling skills while changing mode was rejected. It now
  checks the state the request leaves the target in.

* docs(target): drop stray space in the Japanese mode+naming sentence

* fix(diff): save paired target settings once and report kept legacy entries

- `target X --mode M --target-naming N` ran the two setters, each saving
  the config, so a failed second save left half the change behind (Codex
  on #504). Both fields are now set and saved once, after the final pair
  is checked.
- When a local folder holds the name a skill's naming now gives it, sync
  keeps the skill under its old entry and leaves the folder alone, even
  with --force. diff still called the folder a "local copy" and suggested
  `sync --force`, which does nothing for it. CLI and Web API diff, copy
  and merge mode, now report the folder and the entry the skill stays at.

* fix(sync): migrate only links the manifest records as merge-made

Copy mode treated any link to the skill's source at an old naming's name as
the entry merge mode left behind, so a user's own link was renamed and
replaced with a copy (Codex on #504). A link now migrates only when the
manifest records it as "symlink", which merge mode writes for links it makes;
other links stay as the user left them. The value is now a named constant.

* fix(diff): mark kept legacy entries as unaffected by force

Two problems Codex found on #504:

- The Web API diff sent a local folder on a skill's new name as a "skip",
  which the dashboard shows as an edited entry that Force replaces. Sync
  keeps that folder and the legacy entry even with force, so the preview
  promised a change that never happens. It is now its own "kept" action,
  shown with its reason and not counted.
- `target X --mode M --target-naming N` printed the target's own naming as
  the old value, so a target inheriting standard or prefixed showed
  "flat -> ...". It now prints the naming the target had in effect.

* fix(diff): keep kept-name conflicts out of the sync count

Two problems Codex found on #504:

- A local folder on a skill's new name counted toward "N to sync" and
  fell through to the plain `skillshare sync` hint, though sync changes
  nothing there until the folder is renamed or removed. Such rows now
  carry a kept flag, count as neither sync nor local, and print their own
  hint; a target whose only row is kept still shows that row.
- The dashboard grouped targets by row name and icon only, so two targets
  keeping the same skill at different old entries merged and showed one
  target's old location for both. The row detail is now part of the key.

* fix(diff): do not list a kept local folder's files as deletions

A kept row (a local folder on a skill's new name) still pointed at that
folder, so `diff --stat` and `--patch` listed each of its files as removed,
though sync never touches the folder (Codex on #504). Kept rows no longer
point at a target directory, so no file-level diff is built for them in the
CLI or the TUI.

* fix(diff): report kept skills consistently in every diff view

An exhaustive review of the kept-row change (prompted by two Codex findings
on #504) found the same gap in several places:

- A target whose only rows were kept was summarized as "in sync", and mixed
  summaries left it out of every count. Such targets now count as "N kept
  under old name" with a warning mark, and the scan progress counts them.
- Plain output and the TUI hid where the skill stays; rows now read
  "name (stays at old-name)". The TUI also lacked the kept Next hint and
  called kept-only targets "differs"; it now matches the plain output.
- `diff --json` emitted kept rows as "remove", indistinguishable from
  local-only folders; it now emits "kept", as the Web API does. The diff
  docs' JSON example also showed actions the CLI never emits.
- Renamed rows whose old entry is a merge-made link showed a bogus file
  stat, because the link was walked as a file; a link now stands for the
  folder it points to.
- A dry-run migration printed "Would copy" to the old path; it now names
  the new one.

* fix(sync): stop reporting kept folders and skipped skills as pending work (#509)

Three reports told users to do something sync cannot do, or hid what it
did:

- Copy and merge sync kept a folder the user made on a skill's name but
  counted it as "up to date" with no hint. Sync now counts it as local and
  adds one capped line per target naming the folders and saying that
  `sync --force` replaces them. The line is added in SyncSkillTarget, so
  the dashboard gets it too.
- status and doctor counted skills that sync skips on purpose (invalid
  names under standard or prefixed naming, name collisions) as "not
  synced — run skillshare sync". Running sync can never add them, so the
  advice looped. They now compare against the skills sync places.
- diff marked a target whose only items were local-only folders as "to
  sync" and `synced: false`. Those items are still listed, but they no
  longer make the target pending.

* fix(target): apply every setting flag, require flag values, report no-ops as unchanged (#507)

* fix(target): apply every setting flag, require flag values, report no-ops as unchanged

`skillshare target <name>` dispatched --mode, --agent-mode and
--target-naming through an if-chain that returned after the first match,
so `--agent-mode copy --target-naming prefixed` applied one setting and
silently dropped the other. Filters, --skills and --dry-run had the same
early-return problem.

- Check --mode, --agent-mode and --target-naming as a whole and save
  once, in global and project mode (one targetSettingsUpdate replaces six
  per-flag functions).
- Reject --mode/--agent-mode/--target-naming combined with filters or
  --skills, and --dry-run without --skills, instead of dropping them.
- A value flag followed by another flag (or an empty string) now reports
  "<flag> requires a value"; `--target-naming -p` used to report an
  invalid naming '-p'.
- A value the target already has is reported as unchanged and the config
  is not rewritten.

* fix(target): leave the skills pair alone on agents-only changes

A target with skills off may keep target_naming prefixed with a merge
mode it cannot sync; validateGlobalTarget and validateProjectTarget only
check that pair for enabled skills. The combined settings update ran the
pair check for every setting, so `target claude --agent-mode copy` was
refused on such a target although the old agent-mode handler never looked
at skills settings. Check the pair only when --mode or --target-naming is
given.

* docs(target): show combined settings in --help and the built-in skill

The combined --mode/--agent-mode/--target-naming form is now the way to
move a target to copy + prefixed in one step, but neither the help
examples nor the built-in skill's target reference showed it.
2026-10-09 23:59:47 +08:00

772 lines
24 KiB
Go

package server
import (
"cmp"
"errors"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"strings"
"time"
"skillshare/internal/config"
"skillshare/internal/hooks"
"skillshare/internal/mcp"
ssync "skillshare/internal/sync"
"skillshare/internal/targetsummary"
"skillshare/internal/utils"
)
type targetItem struct {
Name string `json:"name"`
Project string `json:"project,omitempty"` // root of the project this target belongs to
Agent string `json:"agent,omitempty"` // the built-in Agent this is another config directory of
ConfigDir string `json:"configDir,omitempty"`
CLI string `json:"cli,omitempty"` // the executable that runs its plugin commands, when not the Agent's own
Path string `json:"path"`
Mode string `json:"mode"`
TargetNaming string `json:"targetNaming"`
Status string `json:"status"`
LinkedCount int `json:"linkedCount"`
LocalCount int `json:"localCount"`
Include []string `json:"include"`
Exclude []string `json:"exclude"`
ExpectedSkillCount int `json:"expectedSkillCount"`
SkippedSkillCount int `json:"skippedSkillCount,omitempty"`
CollisionCount int `json:"collisionCount,omitempty"`
AgentPath string `json:"agentPath,omitempty"`
AgentMode string `json:"agentMode,omitempty"`
AgentExtension string `json:"agentExtension,omitempty"`
AgentInclude []string `json:"agentInclude,omitempty"`
AgentExclude []string `json:"agentExclude,omitempty"`
AgentLinkedCount *int `json:"agentLinkedCount,omitempty"`
AgentLocalCount *int `json:"agentLocalCount,omitempty"`
AgentExpectedCount *int `json:"agentExpectedCount,omitempty"`
SkillsEnabled bool `json:"skillsEnabled"`
// SkillsReadFrom (skills off) names the enabled targets whose skills folder
// this tool still reads; SkillsAlsoReadBy (skills on) names the targets with
// skills off whose tool reads this target's folder.
SkillsReadFrom []string `json:"skillsReadFrom,omitempty"`
SkillsAlsoReadBy []string `json:"skillsAlsoReadBy,omitempty"`
// SkillsSharedWith names the other targets with skills on that write to this
// target's skills folder, so the page can warn when their settings differ.
SkillsSharedWith []string `json:"skillsSharedWith,omitempty"`
}
var removeTargetPath = os.Remove
func (s *Server) handleListTargets(w http.ResponseWriter, r *http.Request) {
// Snapshot config under RLock, then release before I/O.
s.mu.RLock()
source := s.cfg.EffectiveSkillsSource()
cfgMode := s.cfg.Mode
targets := s.cloneTargets()
isProjectMode := s.IsProjectMode()
projectRoot := s.projectRoot
agentsSourcePath := s.agentsSource()
cfgSnapshot := *s.cfg
var projectEntries []config.ProjectTargetEntry
if isProjectMode && s.projectCfg != nil {
projectEntries = append([]config.ProjectTargetEntry(nil), s.projectCfg.Targets...)
}
s.mu.RUnlock()
globalMode := cfgMode
if globalMode == "" {
globalMode = "merge"
}
projectEntryByName := make(map[string]config.ProjectTargetEntry, len(projectEntries))
for _, entry := range projectEntries {
projectEntryByName[entry.Name] = entry
}
var (
agentBuilder *targetsummary.Builder
err error
)
if isProjectMode {
agentBuilder, err = targetsummary.NewProjectBuilder(agentsSourcePath, projectRoot)
} else {
agentBuilder, err = targetsummary.NewGlobalBuilder(&cfgSnapshot)
}
if err != nil {
writeError(w, http.StatusInternalServerError, "failed to discover agents: "+err.Error())
return
}
items := make([]targetItem, 0, len(targets))
discovered, discoveredErr := ssync.DiscoverSourceSkills(source, s.skillsWalk())
// Project targets are edited under projects, so they are listed only on request:
// scope=projects for them alone, scope=all for what a sync writes.
scope := r.URL.Query().Get("scope")
for name, target := range targets {
if scope != "all" && (target.ProjectRoot() != "") != (scope == "projects") {
continue
}
sc := target.SkillsConfig()
mode := sc.Mode
if mode == "" {
mode = globalMode
}
item := targetItem{
Name: name,
Project: target.ProjectRoot(),
Agent: target.Agent,
ConfigDir: target.ConfigDir,
CLI: target.CLI,
Path: sc.Path,
Mode: mode,
TargetNaming: config.EffectiveTargetNaming(sc.TargetNaming),
Include: func() []string {
if len(sc.Include) == 0 {
return []string{}
}
return append([]string(nil), sc.Include...)
}(),
Exclude: func() []string {
if len(sc.Exclude) == 0 {
return []string{}
}
return append([]string(nil), sc.Exclude...)
}(),
SkillsEnabled: sc.IsEnabled(),
}
switch {
case !sc.IsEnabled():
item.Status = "skills off"
item.SkillsReadFrom = config.SkillsReadFrom(targets, name, projectRoot)
case mode == "merge" || mode == "copy":
if discoveredErr == nil {
filtered, err := ssync.SelectTargetSkills(discovered, name, sc)
if err != nil {
writeError(w, http.StatusBadRequest, "invalid include/exclude for target "+name+": "+err.Error())
return
}
resolution, resErr := ssync.ResolveTargetSkillsForTarget(name, config.ResourceTargetConfig{
Path: sc.Path,
TargetNaming: sc.TargetNaming,
}, filtered)
if resErr == nil {
item.ExpectedSkillCount = len(resolution.Skills)
item.SkippedSkillCount = len(filtered) - len(resolution.Skills)
item.CollisionCount = len(resolution.Collisions)
} else {
item.ExpectedSkillCount = len(filtered)
}
}
if mode == "merge" {
status, linked, local := ssync.CheckStatusMerge(sc.Path, source)
item.Status = status.String()
item.LinkedCount = linked
item.LocalCount = local
} else {
status, managed, local := ssync.CheckStatusCopy(sc.Path)
item.Status = status.String()
item.LinkedCount = managed
item.LocalCount = local
}
default:
status := ssync.CheckStatus(sc.Path, source)
item.Status = status.String()
}
item.SkillsAlsoReadBy = config.SkillsAlsoReadBy(targets, name, projectRoot)
item.SkillsSharedWith = config.SkillsSharedWith(targets, name)
var agentSummary *targetsummary.AgentSummary
if isProjectMode {
if entry, ok := projectEntryByName[name]; ok {
agentSummary, err = agentBuilder.ProjectTarget(entry)
}
} else {
agentSummary, err = agentBuilder.GlobalTarget(name, target)
}
if err != nil {
writeError(w, http.StatusBadRequest, "invalid agent include/exclude for target "+name+": "+err.Error())
return
}
if agentSummary != nil {
item.AgentPath = agentSummary.Path
item.AgentMode = agentSummary.Mode
item.AgentExtension = agentSummary.Extension
item.AgentInclude = agentSummary.Include
item.AgentExclude = agentSummary.Exclude
item.AgentLinkedCount = intPtr(agentSummary.ManagedCount)
item.AgentLocalCount = intPtr(agentSummary.LocalCount)
item.AgentExpectedCount = intPtr(agentSummary.ExpectedCount)
}
items = append(items, item)
}
// Count source skills for drift detection
sourceSkillCount := 0
if discoveredErr == nil {
sourceSkillCount = len(discovered)
}
writeJSON(w, map[string]any{"targets": items, "sourceSkillCount": sourceSkillCount})
}
func intPtr(v int) *int {
return &v
}
func (s *Server) handleAddTarget(w http.ResponseWriter, r *http.Request) {
start := time.Now()
s.mu.Lock()
defer s.mu.Unlock()
var body struct {
Name string `json:"name"`
Path string `json:"path"`
AgentPath string `json:"agentPath"`
// Agent and ConfigDir add another config directory of a built-in Agent instead.
Agent string `json:"agent"`
ConfigDir string `json:"configDir"`
CLI string `json:"cli"`
// Instructions optionally names the instruction file of a custom target.
Instructions *config.TargetInstructionsConfig `json:"instructions"`
// SkillsEnabled false adds the target with skills off (default true).
SkillsEnabled *bool `json:"skills_enabled"`
}
if err := decodeJSON(w, r, &body, defaultJSONBodyLimit); err != nil {
if !errors.Is(err, errBodyTooLarge) {
writeError(w, http.StatusBadRequest, "invalid JSON body")
}
return
}
if body.Name == "" {
writeError(w, http.StatusBadRequest, "name is required")
return
}
if body.Agent != "" || body.ConfigDir != "" {
if s.IsProjectMode() {
writeError(w, http.StatusBadRequest, "a project has no Agent config directory to add")
return
}
if _, exists := s.cfg.Targets[body.Name]; exists {
writeError(w, http.StatusConflict, "target already exists: "+body.Name)
return
}
path, err := s.cfg.AddAgentConfigDirTarget(body.Name, body.Agent, body.ConfigDir, body.CLI)
if err != nil {
writeError(w, http.StatusBadRequest, err.Error())
return
}
if body.SkillsEnabled != nil && !*body.SkillsEnabled {
tc := s.cfg.Targets[body.Name]
tc.EnsureSkills().SetEnabled(false)
s.cfg.Targets[body.Name] = tc
}
if err := s.saveAndReloadConfig(); err != nil {
writeError(w, http.StatusInternalServerError, err.Error())
return
}
s.writeOpsLog("target", "ok", start, map[string]any{"action": "add", "name": body.Name, "target": path, "scope": "ui"}, "")
writeJSON(w, map[string]any{"success": true})
return
}
// In project mode, path can be resolved from known targets
if body.Path == "" {
if s.IsProjectMode() {
if known, ok := config.LookupProjectTarget(body.Name); ok {
body.Path = known.Path
} else {
writeError(w, http.StatusBadRequest, "unknown target, path is required")
return
}
} else {
writeError(w, http.StatusBadRequest, "name and path are required")
return
}
}
if _, exists := s.cfg.Targets[body.Name]; exists {
writeError(w, http.StatusConflict, "target already exists: "+body.Name)
return
}
if ic := body.Instructions; ic != nil {
ic.Path = strings.TrimSpace(ic.Path)
if err := config.ValidateTargetInstructions(ic, s.IsProjectMode()); err != nil {
writeError(w, http.StatusBadRequest, err.Error())
return
}
}
skillsOff := body.SkillsEnabled != nil && !*body.SkillsEnabled
tc := config.TargetConfig{Skills: &config.ResourceTargetConfig{Path: body.Path}, Instructions: body.Instructions}
if !s.IsProjectMode() {
tc.Skills.Mode = config.NewTargetSkillsMode(s.cfg.TargetNaming, s.cfg.Mode)
}
tc.Skills.SetEnabled(!skillsOff)
if body.AgentPath != "" {
tc.Agents = &config.ResourceTargetConfig{Path: body.AgentPath}
}
s.cfg.Targets[body.Name] = tc
// In project mode, also update the project config
if s.IsProjectMode() {
entry := config.ProjectTargetEntry{Name: body.Name, Instructions: body.Instructions}
if skillsOff {
entry.EnsureSkills().SetEnabled(false)
}
if mode := config.NewTargetSkillsMode(s.projectCfg.TargetNaming, ""); mode != "" {
entry.EnsureSkills().Mode = mode // a project target defaults to merge
}
s.projectCfg.Targets = append(s.projectCfg.Targets, entry)
}
if err := s.saveAndReloadConfig(); err != nil {
writeError(w, http.StatusInternalServerError, err.Error())
return
}
s.writeOpsLog("target", "ok", start, map[string]any{
"action": "add",
"name": body.Name,
"target": body.Path,
"scope": "ui",
}, "")
writeJSON(w, map[string]any{"success": true})
}
func (s *Server) handleRemoveTarget(w http.ResponseWriter, r *http.Request) {
start := time.Now()
s.mu.Lock()
defer s.mu.Unlock()
name := r.PathValue("name")
target, exists := s.cfg.Targets[name]
if !exists {
writeError(w, http.StatusNotFound, "target not found: "+name)
return
}
sc := target.SkillsConfig()
if target.ProjectRoot() != "" {
writeError(w, http.StatusBadRequest, name+" belongs to a project; remove the project instead")
return
}
// Another target writing the same folder (codex and universal) still owns its
// links; a target with skills off no longer manages its folder.
if sc.IsEnabled() && config.SkillsPathKeptBy(s.cfg.Targets, name, nil) == "" {
if status, err := s.detachSkillsTarget(sc.Path); err != nil {
writeError(w, status, err.Error())
return
}
}
// Read the MCP config before it is saved without the target: afterwards its own
// name no longer resolves, so the config no longer loads.
var warnings []string
if target.Agent != "" {
if warning := mcp.ReferenceWarning(s.configPath(), name); warning != "" {
warnings = append(warnings, warning)
}
if warning := hooks.ReferenceWarning(s.configPath(), name); warning != "" {
warnings = append(warnings, warning)
}
}
delete(s.cfg.Targets, name)
// In project mode, also remove from project config
if s.IsProjectMode() {
filtered := make([]config.ProjectTargetEntry, 0, len(s.projectCfg.Targets))
for _, t := range s.projectCfg.Targets {
if t.Name != name {
filtered = append(filtered, t)
}
}
s.projectCfg.Targets = filtered
}
if err := s.saveAndReloadConfig(); err != nil {
writeError(w, http.StatusInternalServerError, err.Error())
return
}
s.writeOpsLog("target", "ok", start, map[string]any{
"action": "remove",
"name": name,
"target": sc.Path,
"scope": "ui",
}, "")
writeJSON(w, map[string]any{"success": true, "name": name, "warnings": warnings})
}
func (s *Server) handleUpdateTarget(w http.ResponseWriter, r *http.Request) {
start := time.Now()
s.mu.Lock()
defer s.mu.Unlock()
name := r.PathValue("name")
target, exists := s.cfg.Targets[name]
if !exists {
writeError(w, http.StatusNotFound, "target not found: "+name)
return
}
if target.ProjectRoot() != "" {
writeError(w, http.StatusBadRequest, name+" belongs to a project; edit the project instead")
return
}
// Skills and Agents are pointers shared with s.cfg; edit copies so a
// request rejected halfway leaves the config untouched.
if target.Skills != nil {
sk := *target.Skills
target.Skills = &sk
}
if target.Agents != nil {
ag := *target.Agents
target.Agents = &ag
}
var body struct {
Include *[]string `json:"include"` // null = no change, [] = clear
Exclude *[]string `json:"exclude"`
Mode *string `json:"mode"`
TargetNaming *string `json:"target_naming"`
AgentMode *string `json:"agent_mode"`
AgentInclude *[]string `json:"agent_include"`
AgentExclude *[]string `json:"agent_exclude"`
AgentExtension *string `json:"agent_extension"` // "" = clear
SkillsEnabled *bool `json:"skills_enabled"`
}
if err := decodeJSON(w, r, &body, defaultJSONBodyLimit); err != nil {
if !errors.Is(err, errBodyTooLarge) {
writeError(w, http.StatusBadRequest, "invalid JSON body")
}
return
}
// An extension converts each agent, so it implies copy mode. Reject a
// conflicting mode sent in this same call, as extras targets do.
if body.AgentExtension != nil && *body.AgentExtension != "" && body.AgentMode != nil && *body.AgentMode != "copy" {
writeError(w, http.StatusBadRequest, "agent extension requires copy mode, but agent_mode "+*body.AgentMode+" was set")
return
}
target.EnsureSkills()
// Validate patterns
if body.Include != nil {
if _, err := ssync.FilterSkills(nil, *body.Include, nil); err != nil {
writeError(w, http.StatusBadRequest, "invalid include pattern: "+err.Error())
return
}
target.Skills.Include = *body.Include
}
if body.Exclude != nil {
if _, err := ssync.FilterSkills(nil, nil, *body.Exclude); err != nil {
writeError(w, http.StatusBadRequest, "invalid exclude pattern: "+err.Error())
return
}
target.Skills.Exclude = *body.Exclude
}
if body.Mode != nil {
switch *body.Mode {
case "merge", "symlink", "copy":
target.Skills.Mode = *body.Mode
default:
writeError(w, http.StatusBadRequest, "invalid mode: "+*body.Mode+"; must be merge, symlink, or copy")
return
}
}
if body.TargetNaming != nil {
if !config.IsValidTargetNaming(*body.TargetNaming) {
writeError(w, http.StatusBadRequest, "invalid target_naming: "+*body.TargetNaming+"; must be flat, standard, or prefixed")
return
}
target.Skills.TargetNaming = *body.TargetNaming
}
// Check the pair the target ends up with: an off target may keep a pair its
// mode cannot sync, and turning skills on checks it.
skillsOn := target.SkillsConfig().IsEnabled()
if body.SkillsEnabled != nil {
skillsOn = *body.SkillsEnabled
}
if skillsOn && (body.Mode != nil || body.TargetNaming != nil || body.SkillsEnabled != nil) {
if err := config.TargetNamingModeError(target.SkillsConfig().TargetNaming, cmp.Or(target.Skills.Mode, s.cfg.Mode)); err != nil {
writeError(w, http.StatusBadRequest, err.Error())
return
}
}
if body.AgentMode != nil {
switch *body.AgentMode {
case "merge", "symlink", "copy":
target.EnsureAgents().Mode = *body.AgentMode
default:
writeError(w, http.StatusBadRequest, "invalid agent_mode: "+*body.AgentMode+"; must be merge, symlink, or copy")
return
}
}
if body.AgentExtension != nil {
if *body.AgentExtension != "" {
target.EnsureAgents().Mode = "copy"
}
target.EnsureAgents().Extension = *body.AgentExtension
}
if body.AgentInclude != nil {
if _, err := ssync.FilterSkills(nil, *body.AgentInclude, nil); err != nil {
writeError(w, http.StatusBadRequest, "invalid agent include pattern: "+err.Error())
return
}
target.EnsureAgents().Include = *body.AgentInclude
}
if body.AgentExclude != nil {
if _, err := ssync.FilterSkills(nil, nil, *body.AgentExclude); err != nil {
writeError(w, http.StatusBadRequest, "invalid agent exclude pattern: "+err.Error())
return
}
target.EnsureAgents().Exclude = *body.AgentExclude
}
if body.SkillsEnabled != nil {
target.Skills.SetEnabled(*body.SkillsEnabled)
}
s.cfg.Targets[name] = target
// In project mode, also update the project config
if s.IsProjectMode() {
for i := range s.projectCfg.Targets {
if s.projectCfg.Targets[i].Name == name {
sk := s.projectCfg.Targets[i].EnsureSkills()
if body.SkillsEnabled != nil {
sk.SetEnabled(*body.SkillsEnabled)
}
if body.Include != nil {
sk.Include = *body.Include
}
if body.Exclude != nil {
sk.Exclude = *body.Exclude
}
if body.Mode != nil {
sk.Mode = *body.Mode
}
if body.TargetNaming != nil {
sk.TargetNaming = *body.TargetNaming
}
if body.AgentMode != nil {
s.projectCfg.Targets[i].EnsureAgents().Mode = *body.AgentMode
}
if body.AgentInclude != nil {
s.projectCfg.Targets[i].EnsureAgents().Include = *body.AgentInclude
}
if body.AgentExclude != nil {
s.projectCfg.Targets[i].EnsureAgents().Exclude = *body.AgentExclude
}
if body.AgentExtension != nil {
ag := s.projectCfg.Targets[i].EnsureAgents()
if *body.AgentExtension != "" {
ag.Mode = "copy"
}
ag.Extension = *body.AgentExtension
}
break
}
}
}
if err := s.saveAndReloadConfig(); err != nil {
writeError(w, http.StatusInternalServerError, err.Error())
return
}
// Skills off applies at once: the folder keeps only what skillshare did not link.
var detach *ssync.SkillsOffResult
if body.SkillsEnabled != nil && !*body.SkillsEnabled {
res, err := ssync.DetachSkills(s.cfg.Targets, name, s.cfg.EffectiveSkillsSource(), false)
if err != nil {
s.writeOpsLog("target", "error", start, map[string]any{"action": "skills", "name": name, "enabled": false, "scope": "ui"}, err.Error())
writeError(w, http.StatusInternalServerError, "skills off saved, but removing links failed: "+err.Error())
return
}
detach = res
}
hasFilter := body.Include != nil || body.Exclude != nil || body.AgentInclude != nil || body.AgentExclude != nil
hasSetting := body.Mode != nil || body.TargetNaming != nil || body.AgentMode != nil || body.AgentExtension != nil || body.SkillsEnabled != nil
action := "filter"
if hasSetting && hasFilter {
action = "settings+filter"
} else if hasSetting {
action = "settings"
}
logArgs := map[string]any{
"action": action,
"name": name,
"scope": "ui",
}
if body.SkillsEnabled != nil {
logArgs["skills_enabled"] = *body.SkillsEnabled
}
if detach != nil {
logArgs["removed"] = len(detach.Removed)
logArgs["kept"] = len(detach.Kept)
logArgs["copies"] = len(detach.Copies)
}
s.writeOpsLog("target", "ok", start, logArgs, "")
resp := map[string]any{"success": true}
if detach != nil {
resp["detach"] = map[string]any{"removed": detach.Removed, "kept": detach.Kept, "copies": detach.Copies}
}
writeJSON(w, resp)
}
// handleSkillsOffPreview reports what turning skills off for a target would
// remove and keep, without writing anything.
func (s *Server) handleSkillsOffPreview(w http.ResponseWriter, r *http.Request) {
s.mu.RLock()
targets := s.cloneTargets()
source := s.cfg.EffectiveSkillsSource()
s.mu.RUnlock()
name := r.PathValue("name")
target, ok := targets[name]
if !ok {
writeError(w, http.StatusNotFound, "target not found: "+name)
return
}
if target.ProjectRoot() != "" {
writeError(w, http.StatusBadRequest, name+" belongs to a project; edit the project instead")
return
}
res, err := ssync.DetachSkills(targets, name, source, true)
if err != nil {
writeError(w, http.StatusInternalServerError, err.Error())
return
}
resp := map[string]any{"remove": res.Removed, "keep": res.Kept, "copies": res.Copies}
if res.SharedWith != "" {
resp["sharedWith"] = res.SharedWith
}
writeJSON(w, resp)
}
// detachSkillsTarget removes what sync owns in a skills folder before its target leaves
// the config: the folder symlink, or the manifest and the symlinks into the source.
// Copies and local skills stay.
func (s *Server) detachSkillsTarget(path string) (int, error) {
info, err := os.Lstat(path)
if os.IsNotExist(err) {
return 0, nil
}
if err != nil {
return http.StatusInternalServerError, fmt.Errorf("failed to inspect target: %w", err)
}
if utils.IsLinkMode(path, info.Mode()) {
// Symlink mode: entire directory is a symlink
if err := removeTargetPath(path); err != nil {
return http.StatusInternalServerError, fmt.Errorf("failed to remove target symlink: %w", err)
}
} else if info.IsDir() {
// Remove manifest if present (merge/copy mode)
if err := ssync.RemoveManifest(path); err != nil {
return http.StatusInternalServerError, fmt.Errorf("failed to remove target manifest: %w", err)
}
// Merge mode: remove individual skill symlinks pointing to source
if err := s.unlinkMergeSymlinks(path); err != nil {
return http.StatusInternalServerError, fmt.Errorf("failed to clean target symlinks: %w", err)
}
}
return 0, nil
}
// unlinkMergeSymlinks removes symlinks in targetPath that point under the
// source directory and copies the skill contents back as real files.
func (s *Server) unlinkMergeSymlinks(targetPath string) error {
entries, err := os.ReadDir(targetPath)
if err != nil {
return err
}
absSource, err := filepath.Abs(s.cfg.EffectiveSkillsSource())
if err != nil {
return err
}
absSourcePrefix := absSource + string(filepath.Separator)
for _, entry := range entries {
skillPath := filepath.Join(targetPath, entry.Name())
if !utils.IsSymlinkOrJunction(skillPath) {
continue
}
absLink, err := utils.ResolveLinkTarget(skillPath)
if err != nil {
continue
}
if !utils.PathHasPrefix(absLink, absSourcePrefix) {
continue
}
// Remove symlink and copy the skill back if source still exists
if err := removeTargetPath(skillPath); err != nil {
return fmt.Errorf("remove %s: %w", skillPath, err)
}
if _, statErr := os.Stat(absLink); statErr == nil {
if err := copySkillDir(absLink, skillPath); err != nil {
return fmt.Errorf("restore %s from %s: %w", skillPath, absLink, err)
}
}
}
return nil
}
// copySkillDir copies a directory tree from src to dst.
func copySkillDir(src, dst string) error {
return filepath.Walk(src, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
relPath, err := filepath.Rel(src, path)
if err != nil {
return fmt.Errorf("failed to compute relative path from %s to %s: %w", src, path, err)
}
dstPath := filepath.Join(dst, relPath)
if info.IsDir() {
return os.MkdirAll(dstPath, info.Mode())
}
srcFile, err := os.Open(path)
if err != nil {
return err
}
defer srcFile.Close()
dstFile, err := os.OpenFile(dstPath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, info.Mode())
if err != nil {
return err
}
defer dstFile.Close()
_, err = io.Copy(dstFile, srcFile)
return err
})
}