Files
skillshare/internal/server/handler_skills_batch.go
Willie 7a7cceefab feat(update): flag and protect skills edited since install (#622)
* feat(update): flag and protect skills edited since install

A skill installed from a repository and then edited by hand looked
exactly like an untouched one: check said up to date, and update either
reported it current or silently reinstalled it over the edit (#621).

Install already records each file's hash, so check now compares a
remote skill's files with those hashes and reports "modified", and
update skips such a skill until --force (CLI single, --all/--group,
dashboard). --force, the dashboard's Force Retry on the skipped row, or
the confirmation on the skill page discards the edit and reinstalls.

Two hash paths had to be made reliable for this to be trusted: the
dashboard's folder-wide target edit refreshed hashes by basename, so a
nested skill looked edited after skillshare's own change, and a skill
whose files cannot be hashed now counts as modified instead of being
overwritten.

* fix(server): keep earlier local edits when a target change rewrites SKILL.md

Changing a skill's targets in the dashboard rewrites its SKILL.md, and
both the folder-wide and the single-skill handler then re-hashed the
whole directory as the new baseline. An edit the user had already made
to any file in the skill was absorbed into that baseline, so the skill
stopped counting as modified and the next update overwrote the edit
without --force.

Both handlers now move only SKILL.md's recorded hash, and only when the
file still matched it before the rewrite. skillshare's own target edit
no longer reads as a local change, and an earlier edit still does. The
whole-directory refresh helpers lost their last callers and are removed.
2026-10-24 16:33:37 +08:00

309 lines
7.8 KiB
Go

package server
import (
"errors"
"io"
"net/http"
"os"
"path/filepath"
"strings"
"time"
"skillshare/internal/resource"
"skillshare/internal/sourcefs"
"skillshare/internal/sourcewalk"
ssync "skillshare/internal/sync"
"skillshare/internal/utils"
)
// matchesFolder reports whether relPath belongs to the given folder filter.
// folder="" → root-level only (dir == ".")
// folder="*" → all skills
// otherwise → dir == folder || dir has folder/ prefix
func matchesFolder(relPath, folder string) bool {
if folder == "*" {
return true
}
dir := filepath.ToSlash(filepath.Dir(relPath))
if folder == "" {
return dir == "."
}
return dir == folder || strings.HasPrefix(dir, folder+"/")
}
type batchSetTargetsRequest struct {
Folder string `json:"folder"`
Target string `json:"target"`
}
type batchSetTargetsResponse struct {
Updated int `json:"updated"`
Skipped int `json:"skipped"`
Errors []string `json:"errors"`
}
// handleBatchSetTargets handles POST /api/skills/batch/targets.
// Sets or removes the target for all skills in a folder.
func (s *Server) handleBatchSetTargets(w http.ResponseWriter, r *http.Request) {
start := time.Now()
var req batchSetTargetsRequest
if err := decodeJSON(w, r, &req, defaultJSONBodyLimit); err != nil {
if !errors.Is(err, errBodyTooLarge) {
writeError(w, http.StatusBadRequest, "invalid request body: "+err.Error())
}
return
}
// Reject path traversal in folder
if strings.Contains(req.Folder, "..") {
writeError(w, http.StatusBadRequest, "invalid folder: path traversal not allowed")
return
}
folder := filepath.ToSlash(filepath.Clean(req.Folder))
if req.Folder == "" {
folder = ""
}
// Snapshot config under read lock, then discover without holding the lock.
s.mu.RLock()
source := s.cfg.EffectiveSkillsSource()
s.mu.RUnlock()
walk := s.skillsWalk()
discovered, err := ssync.DiscoverSourceSkillsAll(source, walk)
if err != nil {
writeError(w, http.StatusInternalServerError, "failed to discover skills: "+err.Error())
return
}
var updated, skipped int
var errors []string
overridden := false
var values []string
if req.Target != "" {
values = []string{req.Target}
}
// Acquire write lock only for the file-write loop.
s.mu.Lock()
for _, d := range discovered {
relPath := filepath.ToSlash(d.RelPath)
if !matchesFolder(relPath, folder) {
continue
}
// Skip disabled skills
if d.Disabled {
skipped++
continue
}
// Tracked-repo members keep their SKILL.md untouched so the clone
// stays clean for update; the targets live in .metadata.json.
if d.IsInRepo {
s.skillsStore.SetTargetOverride(relPath, values)
overridden = true
updated++
continue
}
skillMDPath := filepath.Join(d.SourcePath, "SKILL.md")
before, _ := utils.FileHashFormatted(skillMDPath)
if err := setSourceFrontmatterList(source, skillMDPath, "metadata.targets", values, walk.Follow); err != nil {
errors = append(errors, d.FlatName+": "+err.Error())
continue
}
if after, err := utils.FileHashFormatted(skillMDPath); err == nil && s.skillsStore.CarryFileHash(relPath, "SKILL.md", before, after) {
overridden = true
}
updated++
}
// Save the overrides and hashes before unlocking: every API request reloads
// skillsStore from disk, so a change left unsaved here can be dropped by the next one.
if overridden {
if err := s.skillsStore.Save(s.cfg.EffectiveSkillsSource()); err != nil {
s.mu.Unlock()
writeError(w, http.StatusInternalServerError, "failed to save target overrides: "+err.Error())
return
}
}
s.mu.Unlock()
s.writeOpsLog("batch-set-targets", "ok", start, map[string]any{
"folder": req.Folder,
"target": req.Target,
"updated": updated,
"skipped": skipped,
"scope": "ui",
}, "")
if errors == nil {
errors = []string{}
}
writeJSON(w, batchSetTargetsResponse{
Updated: updated,
Skipped: skipped,
Errors: errors,
})
}
type setSkillTargetsRequest struct {
Target string `json:"target"`
}
// handleSetSkillTargets handles PATCH /api/skills/{name}/targets.
// Sets or removes the target for a single skill.
func (s *Server) handleSetSkillTargets(w http.ResponseWriter, r *http.Request) {
start := time.Now()
name := r.PathValue("name")
var req setSkillTargetsRequest
if err := decodeJSON(w, r, &req, defaultJSONBodyLimit); err != nil {
if !errors.Is(err, errBodyTooLarge) {
writeError(w, http.StatusBadRequest, "invalid request body: "+err.Error())
}
return
}
s.mu.RLock()
source := s.cfg.EffectiveSkillsSource()
s.mu.RUnlock()
walk := s.skillsWalk()
discovered, err := ssync.DiscoverSourceSkillsAll(source, walk)
if err != nil {
writeError(w, http.StatusInternalServerError, "failed to discover skills: "+err.Error())
return
}
for _, d := range discovered {
baseName := filepath.Base(d.SourcePath)
if d.FlatName != name && baseName != name {
continue
}
var values []string
if req.Target != "" {
values = []string{req.Target}
}
if d.IsInRepo {
// Tracked-repo members keep their SKILL.md untouched so the clone
// stays clean for update; the targets live in .metadata.json.
s.mu.Lock()
s.skillsStore.SetTargetOverride(d.RelPath, values)
err := s.skillsStore.Save(s.cfg.EffectiveSkillsSource())
s.mu.Unlock()
if err != nil {
writeError(w, http.StatusInternalServerError, "failed to update skill: "+err.Error())
return
}
} else {
skillMDPath := filepath.Join(d.SourcePath, "SKILL.md")
s.mu.Lock()
before, _ := utils.FileHashFormatted(skillMDPath)
err := setSourceFrontmatterList(source, skillMDPath, "metadata.targets", values, walk.Follow)
if err == nil {
if after, hashErr := utils.FileHashFormatted(skillMDPath); hashErr == nil && s.skillsStore.CarryFileHash(d.RelPath, "SKILL.md", before, after) {
err = s.skillsStore.Save(s.cfg.EffectiveSkillsSource())
}
}
s.mu.Unlock()
if err != nil {
status := http.StatusInternalServerError
if errors.Is(err, sourcefs.ErrLink) {
status = http.StatusBadRequest
}
writeError(w, status, "failed to update skill: "+err.Error())
return
}
}
s.writeOpsLog("set-skill-targets", "ok", start, map[string]any{
"name": name,
"target": req.Target,
"scope": "ui",
}, "")
writeJSON(w, map[string]any{"success": true})
return
}
// Try agents if skill not found
s.mu.RLock()
agentsSource := s.agentsSource()
s.mu.RUnlock()
if agentsSource != "" {
agents, _ := resource.AgentKind{}.Discover(agentsSource)
for _, d := range agents {
if d.FlatName != name {
continue
}
var values []string
if req.Target != "" {
values = []string{req.Target}
}
s.mu.Lock()
err := setSourceFrontmatterList(agentsSource, d.SourcePath, "targets", values, nil)
s.mu.Unlock()
if err != nil {
writeError(w, http.StatusInternalServerError, "failed to update agent: "+err.Error())
return
}
s.writeOpsLog("set-agent-targets", "ok", start, map[string]any{
"name": name,
"target": req.Target,
"scope": "ui",
}, "")
writeJSON(w, map[string]any{"success": true})
return
}
}
writeError(w, http.StatusNotFound, "resource not found: "+name)
}
// setSourceFrontmatterList keeps target assignment inside the source write boundary.
func setSourceFrontmatterList(source, path, field string, values []string, follow *sourcewalk.Follow) error {
root, err := sourcefs.Open(source, follow)
if err != nil {
return err
}
defer root.Close()
rel, err := root.Rel(path)
if err != nil {
return err
}
if err := root.CheckNoLink(rel); err != nil {
return err
}
file, err := root.OpenFile(rel, os.O_RDONLY, 0)
if err != nil {
return err
}
data, err := io.ReadAll(file)
file.Close()
if err != nil {
return err
}
data, err = utils.RewriteFrontmatterList(data, field, values)
if err != nil {
return err
}
return root.WriteFileAtomic(rel, data, 0644)
}