Files
Willie 2ce46834e2 fix(collect,install): collect only real skills; let --track take a local git path (#508)
* fix(collect,install): collect only real skills; let --track take a local git path

collect treated every directory in a target as a local skill, so a scratch
folder was listed and `collect --force` copied it into the source. A folder
now counts only when it holds SKILL.md; the dashboard scan shares
FindLocalSkills and the dashboard collect request rejects such a folder.

`install /path/to/repo --track` failed with "requires a git repository
source" while file:///path worked. A local path that is a git repository
root is now normalized to the same file:// clone URL for --track (also past
the --branch guard); other local folders get an error that names file://.
Help text, docs (English and i18n) and the built-in skill reference follow.

* fix(install): keep the drive letter in file:// clone URLs for local --track paths

A Windows path such as C:/repo produced file://C:/repo, which makes C: the
URI authority instead of part of the path, so a tracked install from a local
Windows repository could clone the wrong location. Prefix the missing slash
so the URL is file:///C:/repo; POSIX paths are unchanged. Also cover the
dashboard install endpoint with a local-path --track test.

* fix(collect,install): keep UNC file URLs intact; require SKILL.md to be a file

The drive-letter fix lived in the helper shared with explicit file:// URLs,
which rewrote file://server/share/repo into a local path. Keep the slash
logic in the local-path --track case only and leave explicit URLs as parsed.

A subdirectory named SKILL.md passed the existence check, so a scratch folder
holding one was still collected. Both collect entry points (scan and the
dashboard request) now share sync.HasSkillFile, which rejects directories.

* fix(install): escape the local path in the file:// URL used by --track

Git decodes a file:// URL, so a repository path containing %20, %2F, a space
or # was cloned from a different path than the one IsGitRepo accepted. Build
the URL with net/url so those characters are escaped; drive-letter and POSIX
paths keep their form. The integration test now installs from a directory
whose name contains a space and a literal %20.

* fix(install): accept a local bare repository path for --track

A bare repository has HEAD and objects at its root but no .git entry, so the
local-path check rejected it while file:///same/path cloned fine. Add
IsLocalGitRepo (working tree or bare) and use it for both the --track
normalization and the --branch guard.

* test(install): build the bare repo path from a Windows file URL correctly

makeRemote returns file:///C:/dir on Windows; trimming only file:// left
/C:/dir, which filepath.Abs turned into D:\C:\dir, so the bare-repo test
failed on the Windows runner. Drop the slash before the drive letter.

* fix(install): refuse a --track local source that is the install destination

--force removes the tracked destination before cloning. When the local path
given to --track is that same directory, the removal deleted the only clone
source (and any unpushed work) before the clone failed. Compare the two with
os.SameFile, which also covers symlinked paths, and stop before touching the
destination.

* fix(install,collect): keep a tracked source inside its destination; reject special SKILL.md files

--track --force removed the destination before cloning, so a local source
nested below it (or given as an explicit file:// URL) was deleted along with
any unpushed work, and the clone then failed. Resolve the source and refuse
when it is the destination or lies under it, for dry runs too.

HasSkillFile used a not-a-directory test, so a FIFO named SKILL.md counted as
a skill and the collect copy blocked on it. Require a regular file; a
symlinked SKILL.md still resolves through os.Stat.

* fix(install): treat file://localhost URLs as local clone sources

Git clones file://localhost/path from the local filesystem, but the
containment guard only recognised an empty authority, so --track --force
could still delete a source given that way. Accept localhost (any case) as
local.

* test(install): build the localhost file URL from a slash path

On Windows the temp path starts with a drive letter, so prefixing it onto
file://localhost produced file://localhostC:\... instead of a valid URL and
the case failed on the Windows runner. Convert to slashes and add the
separator explicitly.

* fix(install): clone --track --force into a staging dir and swap it in last

--force removed the existing tracked repo before cloning, so any source that
lived under it (plain path, file:///, file://localhost, UNC, symlink or 8.3
alias) was destroyed before git could read it, and a failed clone also lost
the old repo. Clone beside the destination, run the checks and the audit on
the staging copy, then move the old repo aside, rename the new one in and
drop the backup; on any failure the staging copy is removed and the old repo
stays. The pre-removal containment refusal now covers plain local paths only
(the url.Parse special-casing is gone).

* fix(install): refuse a file:// --track source inside the forced destination

The staged swap in 7375400 still lost data for a file:///, file://localhost
or UNC source nested under the destination: the clone succeeded, the old repo
(with the source inside it) was renamed to the backup, and removing the backup
deleted the source's uncommitted work, which a clone cannot carry.

Restore the file URL to local path mapping (now including UNC hosts) so the
containment refusal covers every local spelling, not only plain paths. The
test now keeps an uncommitted file in the source and requires it to survive.

* fix(install): apply the source-inside-destination refusal only to --force

The refusal ran before the --update branch, so `install <tracked repo path>
--track --update` was rejected even though an update pulls the existing
checkout in place and removes nothing. Only the forced replacement can
destroy a source that lives under the destination, so check there.

* fix(install): report a forced clone's root skill under its tracked name

With --force the repo is discovered in the .skillshare-clone-<stamp> staging
directory, and discoverSkills names a root SKILL.md after the directory, so
the CLI summary, dashboard response and oplog listed a random staging name
instead of _foo. Use the tracked name for the root entry.

* fix(install): stage fresh tracked clones so cleanup never hits another installer

A fresh tracked install cloned straight into the destination, and the
deferred cleanup removed it on any failure. If another process created the
same tracked repo between the existence check and the clone, the failing
install deleted that process's checkout. Clone every install into its own
staging directory and rename it in last; a claimed destination makes the
rename fail and only the staging copy is removed.
2026-10-10 01:06:23 +08:00

329 lines
9.0 KiB
Go

package server
import (
"errors"
"maps"
"net/http"
"os"
"path/filepath"
"time"
ssync "skillshare/internal/sync"
"skillshare/internal/utils"
)
// --- Scan types ---
type localSkillItem struct {
Name string `json:"name"`
Kind string `json:"kind,omitempty"`
Path string `json:"path"`
TargetName string `json:"targetName"`
Size int64 `json:"size"`
ModTime string `json:"modTime"`
}
type scanTarget struct {
TargetName string `json:"targetName"`
Skills []localSkillItem `json:"skills"`
}
// --- Collect types ---
type collectSkillRef struct {
Name string `json:"name"`
TargetName string `json:"targetName"`
Kind string `json:"kind,omitempty"`
}
// handleCollectScan scans targets for local (non-symlinked) skills and/or agents.
// GET /api/collect/scan?target=<name>&kind=skill|agent (both optional)
// When kind is omitted, scans for both skills and agents.
func (s *Server) handleCollectScan(w http.ResponseWriter, r *http.Request) {
// Snapshot config under RLock, then release before I/O.
s.mu.RLock()
source := s.cfg.EffectiveSkillsSource()
globalMode := s.cfg.Mode
targets := s.cfg.OwnTargets() // a project's own skills stay in the project
agentsSource := s.agentsSource()
s.mu.RUnlock()
filterTarget := r.URL.Query().Get("target")
kind := r.URL.Query().Get("kind")
if kind != "" && kind != kindSkill && kind != kindAgent {
writeError(w, http.StatusBadRequest, "invalid kind: must be 'skill', 'agent', or empty")
return
}
// Collect items per target, merging skills and agents.
targetItems := make(map[string][]localSkillItem)
totalCount := 0
// --- Skill scan ---
if kind != kindAgent {
for name, target := range targets {
if filterTarget != "" && filterTarget != name {
continue
}
sc := target.SkillsConfig()
mode := ssync.EffectiveMode(sc.Mode)
if sc.Mode == "" && globalMode != "" {
mode = globalMode
}
locals, err := ssync.FindLocalSkills(sc.Path, source, mode)
if err != nil {
writeError(w, http.StatusInternalServerError, "scan failed for "+name+": "+err.Error())
return
}
for _, sk := range locals {
targetItems[name] = append(targetItems[name], localSkillItem{
Name: sk.Name,
Kind: kindSkill,
Path: sk.Path,
TargetName: name,
Size: ssync.CalculateDirSize(sk.Path),
ModTime: sk.ModTime.Format(time.RFC3339),
})
}
}
}
// --- Agent scan ---
if kind != kindSkill {
builtinAgents := s.builtinAgentTargets()
for name, target := range targets {
if filterTarget != "" && filterTarget != name {
continue
}
agentPath := resolveAgentPath(target, builtinAgents, name, s.IsProjectMode())
// Extension targets hold converted output, never agents to collect.
if agentPath == "" || agentsSource == "" || target.AgentsConfig().Extension != "" {
continue
}
localAgents, err := ssync.FindLocalAgents(agentPath, agentsSource)
if err != nil {
writeError(w, http.StatusInternalServerError, "agent scan failed for "+name+": "+err.Error())
return
}
for _, ag := range localAgents {
var size int64
var modTime string
if info, err := os.Stat(ag.Path); err == nil {
size = info.Size()
modTime = info.ModTime().Format(time.RFC3339)
}
targetItems[name] = append(targetItems[name], localSkillItem{
Name: ag.Name,
Kind: kindAgent,
Path: ag.Path,
TargetName: name,
Size: size,
ModTime: modTime,
})
}
}
}
// Build response from merged map.
var scanTargets []scanTarget
for name := range targets {
items := targetItems[name]
if len(items) == 0 && kind != "" {
// When filtering by kind, skip targets with no items of that kind.
continue
}
totalCount += len(items)
if items == nil {
items = []localSkillItem{}
}
scanTargets = append(scanTargets, scanTarget{
TargetName: name,
Skills: items,
})
}
if scanTargets == nil {
scanTargets = []scanTarget{}
}
writeJSON(w, map[string]any{
"targets": scanTargets,
"totalCount": totalCount,
})
}
// handleCollect pulls selected local skills and/or agents from targets to source.
// POST /api/collect { skills: [{name, targetName, kind?}], force: bool }
// Items with kind="agent" are pulled as agents; others as skills (backward compat).
func (s *Server) handleCollect(w http.ResponseWriter, r *http.Request) {
start := time.Now()
s.mu.Lock()
defer s.mu.Unlock()
var body struct {
Skills []collectSkillRef `json:"skills"`
Force bool `json:"force"`
}
if err := decodeJSON(w, r, &body, defaultJSONBodyLimit); err != nil {
if !errors.Is(err, errBodyTooLarge) {
writeError(w, http.StatusBadRequest, "invalid request body: "+err.Error())
}
return
}
if len(body.Skills) == 0 {
writeError(w, http.StatusBadRequest, "no skills specified")
return
}
// Split items by kind.
var skillRefs, agentRefs []collectSkillRef
for _, ref := range body.Skills {
if ref.Kind == kindAgent {
agentRefs = append(agentRefs, ref)
} else {
skillRefs = append(skillRefs, ref)
}
}
opts := ssync.PullOptions{Force: body.Force}
// Merged results across skills and agents.
var allPulled, allSkipped []string
warnings := make(map[string]string)
allFailed := make(map[string]error)
var skillsPulled, agentsPulled int
// --- Pull skills ---
if len(skillRefs) > 0 {
var resolved []ssync.LocalSkillInfo
for _, ref := range skillRefs {
target, ok := s.cfg.Targets[ref.TargetName]
if !ok {
writeError(w, http.StatusBadRequest, "unknown target: "+ref.TargetName)
return
}
skillPath := filepath.Join(target.SkillsConfig().Path, ref.Name)
info, err := os.Lstat(skillPath)
if err != nil {
writeError(w, http.StatusBadRequest, "skill not found: "+ref.Name+" in "+ref.TargetName)
return
}
if utils.IsLinkMode(skillPath, info.Mode()) {
writeError(w, http.StatusBadRequest, "skill is a symlink (not local): "+ref.Name)
return
}
if !info.IsDir() {
writeError(w, http.StatusBadRequest, "skill is not a directory: "+ref.Name)
return
}
if !ssync.HasSkillFile(skillPath) {
writeError(w, http.StatusBadRequest, "not a skill (no SKILL.md): "+ref.Name)
return
}
resolved = append(resolved, ssync.LocalSkillInfo{
Name: ref.Name,
Path: skillPath,
TargetName: ref.TargetName,
})
}
opts.Walk = s.skillsWalk()
result, err := ssync.PullSkills(resolved, s.cfg.EffectiveSkillsSource(), opts)
if err != nil {
writeError(w, http.StatusInternalServerError, "collect failed: "+err.Error())
return
}
skillsPulled = len(result.Pulled)
for name, warning := range result.Warnings {
warnings[name] = warning
}
allPulled = append(allPulled, result.Pulled...)
allSkipped = append(allSkipped, result.Skipped...)
maps.Copy(allFailed, result.Failed)
}
// --- Pull agents ---
if len(agentRefs) > 0 {
builtinAgents := s.builtinAgentTargets()
agentsSource := s.agentsSource()
var resolved []ssync.LocalAgentInfo
for _, ref := range agentRefs {
target, ok := s.cfg.Targets[ref.TargetName]
if !ok {
writeError(w, http.StatusBadRequest, "unknown target: "+ref.TargetName)
return
}
agentPath := resolveAgentPath(target, builtinAgents, ref.TargetName, s.IsProjectMode())
if agentPath == "" {
writeError(w, http.StatusBadRequest, "no agent path for target: "+ref.TargetName)
return
}
filePath := filepath.Join(agentPath, ref.Name)
info, err := os.Lstat(filePath)
if err != nil {
writeError(w, http.StatusBadRequest, "agent not found: "+ref.Name+" in "+ref.TargetName)
return
}
if utils.IsLinkMode(filePath, info.Mode()) {
writeError(w, http.StatusBadRequest, "agent is a symlink (not local): "+ref.Name)
return
}
resolved = append(resolved, ssync.LocalAgentInfo{
Name: ref.Name,
Path: filePath,
TargetName: ref.TargetName,
})
}
result, err := ssync.PullAgents(resolved, agentsSource, opts)
if err != nil {
writeError(w, http.StatusInternalServerError, "agent collect failed: "+err.Error())
return
}
agentsPulled = len(result.Pulled)
allPulled = append(allPulled, result.Pulled...)
allSkipped = append(allSkipped, result.Skipped...)
maps.Copy(allFailed, result.Failed)
}
// Convert Failed map to string values for JSON
failed := make(map[string]string, len(allFailed))
for k, v := range allFailed {
failed[k] = v.Error()
}
status := "ok"
msg := ""
if len(allFailed) > 0 {
status = "partial"
msg = "some items failed to collect"
}
s.writeOpsLog("collect", status, start, map[string]any{
"skills_selected": len(skillRefs),
"skills_pulled": skillsPulled,
"agents_selected": len(agentRefs),
"agents_pulled": agentsPulled,
"total_pulled": len(allPulled),
"total_skipped": len(allSkipped),
"total_failed": len(allFailed),
"force": body.Force,
"scope": "ui",
}, msg)
writeJSON(w, map[string]any{
"pulled": allPulled,
"skipped": allSkipped,
"warnings": warnings,
"failed": failed,
})
}