Files
skillshare/internal/server/handler_toggle_batch.go
T
Willie 89fc4fbdf1 feat(sourcewalk): follow first-level source links when enabled
Users link a checkout directly under the skills source (e.g.
skills/_dev-skills -> ~/code/dev-skills) and expect it to behave like any
other first-level entry. sourcewalk resolved only the root, so these links
were invisible to discovery, sync, update, and the dashboard.

follow_source_links (global and project config, default false) enables a
per-operation sourcewalk.Follow policy. Walk, WalkDir, and ReadDir report a
followed link under its own name as a directory and descend into its
target, so names, RelPath, FlatName, SourcePath, tracked-repo detection, and
metadata keys stay logical. Only entries directly under the source root are
followed. The policy is built from config and passed explicitly to every
discovery caller in the CLI, server, and reconcile, so counts agree; the
server keeps a per-instance snapshot refreshed on config load and save
instead of a process-global flag.

A link is skipped, with one warning per operation, when its target is
missing or unreadable, is the source or a parent of it, or overlaps an
active skills target. A missing or unreadable target marks the operation
incomplete: sync then skips pruning and copy-orphan deletion, and reconcile
keeps metadata, so an unmounted drive or a re-clone in progress cannot
erase target entries or install records. A link to an existing file is not
a directory link and stays an ordinary entry; treating it as incomplete
would disable pruning for anyone sharing a file such as .skillignore.

doctor keeps raw link identity: with the key off it still says the link is
not followed and names follow_source_links: true; with it on it reports
followed links and skipped links with the reason.

The raw-walk allowlist gains entries for the policy path in Walk and
WalkDir: the source root walk with a policy, and the walk of a validated
link target whose paths are remapped to the logical link path. Both live
in sourcewalk itself, the package that owns source traversal.

Refs #274
2026-10-05 14:26:09 +08:00

212 lines
5.8 KiB
Go

package server
import (
"errors"
"net/http"
"path/filepath"
"time"
"skillshare/internal/resource"
"skillshare/internal/skillignore"
ssync "skillshare/internal/sync"
)
type batchToggleRequest struct {
Names []string `json:"names"`
Kind string `json:"kind"`
Enable bool `json:"enable"`
}
type batchToggleItemResult struct {
Name string `json:"name"`
Success bool `json:"success"`
Disabled bool `json:"disabled"`
Error string `json:"error,omitempty"`
}
type batchToggleResponse struct {
Results []batchToggleItemResult `json:"results"`
Summary struct {
Updated int `json:"updated"`
Unchanged int `json:"unchanged"`
Failed int `json:"failed"`
} `json:"summary"`
}
// handleBatchToggleSkills handles POST /api/resources/batch/toggle.
// Enables or disables many skills or agents at once by adding/removing their
// paths in the shared .skillignore / .agentignore file. One kind per request.
func (s *Server) handleBatchToggleSkills(w http.ResponseWriter, r *http.Request) {
start := time.Now()
var req batchToggleRequest
if err := decodeJSON(w, r, &req, defaultJSONBodyLimit); err != nil {
if !errors.Is(err, errBodyTooLarge) {
writeError(w, http.StatusBadRequest, "invalid request body: "+err.Error())
}
return
}
if req.Kind != "" && req.Kind != "agent" && req.Kind != "skill" {
writeError(w, http.StatusBadRequest, "invalid kind: "+req.Kind)
return
}
// Snapshot config under read lock, then discover without holding the lock.
s.mu.RLock()
source := s.cfg.EffectiveSkillsSource()
agentsSource := s.agentsSource()
s.mu.RUnlock()
// Discover once and build a name → status lookup. First match wins, matching
// the resolve* helpers used by the single-resource toggle handler.
type entry struct {
relPath string
disabled bool
}
lookup := map[string]entry{}
put := func(key string, e entry) {
if key == "" {
return
}
if _, exists := lookup[key]; !exists {
lookup[key] = e
}
}
var ignorePath string
if req.Kind == "agent" {
if agentsSource == "" {
writeError(w, http.StatusNotFound, "no agents source configured")
return
}
discovered, err := resource.AgentKind{}.Discover(agentsSource)
if err != nil {
writeError(w, http.StatusInternalServerError, "failed to discover agents: "+err.Error())
return
}
for _, d := range discovered {
e := entry{relPath: d.RelPath, disabled: d.Disabled}
put(d.FlatName, e)
put(d.Name, e)
put(d.RelPath, e)
put(agentDisplayName(d.RelPath), e)
}
ignorePath = filepath.Join(agentsSource, ".agentignore")
} else {
discovered, err := ssync.DiscoverSourceSkillsAll(source, s.skillsWalk())
if err != nil {
writeError(w, http.StatusInternalServerError, "failed to discover skills: "+err.Error())
return
}
for _, d := range discovered {
e := entry{relPath: d.RelPath, disabled: d.Disabled}
put(d.FlatName, e)
put(filepath.Base(d.SourcePath), e)
put(d.RelPath, e)
}
ignorePath = filepath.Join(source, ".skillignore")
}
var resp batchToggleResponse
resp.Results = make([]batchToggleItemResult, 0, len(req.Names))
// Each successful result, so its outcome can be checked after writing.
type pending struct {
relPath string
updated bool
}
written := map[int]pending{}
succeed := func(name, relPath string, updated bool) {
written[len(resp.Results)] = pending{relPath, updated}
resp.Results = append(resp.Results, batchToggleItemResult{Name: name, Success: true, Disabled: !req.Enable})
if updated {
resp.Summary.Updated++
} else {
resp.Summary.Unchanged++
}
}
// Hold the write lock while writing and re-checking the outcome.
s.mu.Lock()
for _, name := range req.Names {
e, ok := lookup[name]
if !ok {
resp.Results = append(resp.Results, batchToggleItemResult{Name: name, Error: "resource not found: " + name})
resp.Summary.Failed++
continue
}
if req.Enable {
if !e.disabled {
succeed(name, e.relPath, false)
continue
}
removed, err := skillignore.RemovePattern(ignorePath, e.relPath)
if err != nil {
resp.Results = append(resp.Results, batchToggleItemResult{Name: name, Disabled: true, Error: err.Error()})
resp.Summary.Failed++
continue
}
if !removed {
resp.Results = append(resp.Results, batchToggleItemResult{
Name: name,
Disabled: true,
Error: "disabled by a glob or directory pattern — edit the ignore file manually to remove the matching rule",
})
resp.Summary.Failed++
continue
}
succeed(name, e.relPath, true)
continue
}
added, err := skillignore.AddPattern(ignorePath, e.relPath)
if err != nil {
resp.Results = append(resp.Results, batchToggleItemResult{Name: name, Error: err.Error()})
resp.Summary.Failed++
continue
}
succeed(name, e.relPath, added)
}
// A written rule can still be overridden by a later one, such as a "!" line
// in the .local file, so re-check the outcome the way discovery sees it.
if len(written) > 0 {
states, err := s.disabledByRelPath(req.Kind, source, agentsSource)
if err != nil {
s.mu.Unlock()
writeError(w, http.StatusInternalServerError, err.Error())
return
}
for i, p := range written {
msg := toggleOverrideError(req.Kind, req.Enable, states[p.relPath])
if msg == "" {
continue
}
if p.updated {
resp.Summary.Updated--
} else {
resp.Summary.Unchanged--
}
resp.Results[i] = batchToggleItemResult{Name: resp.Results[i].Name, Disabled: req.Enable, Error: msg}
resp.Summary.Failed++
}
}
s.mu.Unlock()
action := "batch-disable"
if req.Enable {
action = "batch-enable"
}
s.writeOpsLog(action, "ok", start, map[string]any{
"kind": req.Kind,
"enable": req.Enable,
"count": len(req.Names),
"updated": resp.Summary.Updated,
"unchanged": resp.Summary.Unchanged,
"failed": resp.Summary.Failed,
"scope": "ui",
}, "")
writeJSON(w, resp)
}