Files
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

107 lines
3.3 KiB
Go

package server
import (
"cmp"
"net/http"
"slices"
ssync "skillshare/internal/sync"
)
type analyzeCharTokensResponse struct {
Chars int `json:"chars"`
EstimatedTokens int `json:"estimated_tokens"`
}
type analyzeSkillResponse struct {
Name string `json:"name"`
DescriptionChars int `json:"description_chars"`
DescriptionTokens int `json:"description_tokens"`
BodyChars int `json:"body_chars"`
BodyTokens int `json:"body_tokens"`
LintIssues []ssync.LintIssue `json:"lint_issues,omitempty"`
Local bool `json:"local,omitempty"`
Disabled bool `json:"disabled,omitempty"`
Path string `json:"path"`
IsTracked bool `json:"is_tracked"`
Targets []string `json:"targets,omitempty"`
Description string `json:"description,omitempty"`
}
type analyzeTargetResponse struct {
Name string `json:"name"`
SkillCount int `json:"skill_count"`
AlwaysLoaded analyzeCharTokensResponse `json:"always_loaded"`
OnDemandMax analyzeCharTokensResponse `json:"on_demand_max"`
Skills []analyzeSkillResponse `json:"skills"`
}
func (s *Server) handleAnalyze(w http.ResponseWriter, r *http.Request) {
s.mu.RLock()
source := s.cfg.EffectiveSkillsSource()
targets := s.cfg.Targets
defaultMode := s.cfg.Mode
s.mu.RUnlock()
discovered, err := ssync.DiscoverSourceSkillsForAnalyze(source, s.skillsWalk())
if err != nil {
writeError(w, http.StatusInternalServerError, err.Error())
return
}
entries := make([]analyzeTargetResponse, 0)
for name, target := range targets {
filtered, ferr := ssync.TargetSkills(name, target, defaultMode, source, discovered)
if ferr != nil || len(filtered) == 0 {
continue
}
skills := make([]analyzeSkillResponse, 0, len(filtered))
var totalDescChars, totalBodyChars, totalDescTokens, totalBodyTokens int
for _, sk := range filtered {
totalDescChars += sk.DescChars
totalBodyChars += sk.BodyChars
totalDescTokens += sk.DescTokens
totalBodyTokens += sk.BodyTokens
skills = append(skills, analyzeSkillResponse{
Name: sk.FlatName,
DescriptionChars: sk.DescChars,
DescriptionTokens: sk.DescTokens,
BodyChars: sk.BodyChars,
BodyTokens: sk.BodyTokens,
LintIssues: sk.LintIssues,
Local: sk.Local,
Disabled: sk.Disabled,
Path: sk.RelPath,
IsTracked: sk.IsInRepo,
Targets: sk.Targets,
Description: sk.Description,
})
}
slices.SortFunc(skills, func(a, b analyzeSkillResponse) int {
return cmp.Compare(b.DescriptionChars, a.DescriptionChars)
})
entries = append(entries, analyzeTargetResponse{
Name: name,
SkillCount: len(skills),
AlwaysLoaded: analyzeCharTokensResponse{
Chars: totalDescChars,
EstimatedTokens: totalDescTokens,
},
OnDemandMax: analyzeCharTokensResponse{
Chars: totalBodyChars,
EstimatedTokens: totalBodyTokens,
},
Skills: skills,
})
}
slices.SortFunc(entries, func(a, b analyzeTargetResponse) int {
return cmp.Compare(b.AlwaysLoaded.Chars, a.AlwaysLoaded.Chars)
})
writeJSON(w, map[string]any{"targets": entries})
}