Files
Willie 1a1eeee844 fix(check): compare project-relative local sources and record install layout (#337)
* fix(check): compare project-relative local sources and record install layout

Project installs record a local source as typed (./vendor/skill), relative
to the project root, so check skipped them. Resolve them against the project
root in both the CLI and the dashboard.

The copied shape of a local install was inferred from its file hashes, so a
CLI install whose source held only SKILL.md looked like a dashboard
collection-root install: a child skill added at the source later was neither
reported by check nor copied by update. Record the layout (skill-file or
directory) at install time and keep the hash heuristic only for entries
written before the field existed.

* fix(install): let a recorded layout decide the update shape without hashes

An entry whose hashing failed at install time still knows what was copied;
returning false there would turn a SKILL.md-only update into a full copy.
2026-10-02 20:56:55 +08:00

86 lines
2.5 KiB
Go

package install
import (
"path/filepath"
"strings"
)
// Layouts a local-path install records in MetadataEntry.Layout.
const (
// LayoutSkillFile: only SKILL.md was copied. The dashboard installs the
// root of a directory that also holds child skills this way.
LayoutSkillFile = "skill-file"
// LayoutDirectory: the whole source directory was copied, as
// `install <path>` does.
LayoutDirectory = "directory"
)
// CopiesSkillFileOnly reports whether a local install copies only SKILL.md
// from its source. source holds the current hashes of the source directory.
//
// The recorded layout decides. Entries written before the layout was recorded
// fall back to the recorded files: exactly SKILL.md, from a source that holds
// child skills. Anything else copied the whole directory, and updating it as
// SKILL.md alone would delete files.
func CopiesSkillFileOnly(entry *MetadataEntry, source map[string]string) bool {
if entry.Layout != "" {
return entry.Layout == LayoutSkillFile
}
if _, ok := entry.FileHashes["SKILL.md"]; !ok || len(entry.FileHashes) != 1 {
return false
}
for rel := range source {
if strings.HasSuffix(rel, "/SKILL.md") {
return true
}
}
return false
}
// discoveredLocalLayout returns the layout a discovery install of skill
// records for a local-path source, or "" when the copy is neither shape
// (a child skill with nested skills excluded) or the source is not local.
func discoveredLocalLayout(discovery *DiscoveryResult, skill SkillInfo) string {
if discovery.Source == nil || discovery.Source.Type != SourceTypeLocalPath {
return ""
}
excludes := descendantSkillPaths(discovery, skill)
switch {
case excludes == nil:
return LayoutDirectory
case skill.Path == "." && !discovery.Source.HasSubdir():
return LayoutSkillFile
}
return ""
}
// isSkillFileOnlyLocalInstall reports whether the local skill at destPath
// was installed as its SKILL.md alone, so an update keeps that shape.
func isSkillFileOnlyLocalInstall(source *Source, destPath, sourceDir string) bool {
if source.Type != SourceTypeLocalPath {
return false
}
if sourceDir == "" {
sourceDir = filepath.Dir(destPath)
}
rel, err := filepath.Rel(sourceDir, destPath)
if err != nil {
return false
}
entry := LoadMetadataOrNew(sourceDir).GetByPath(filepath.ToSlash(rel))
if entry == nil {
return false
}
if entry.Layout != "" {
return entry.Layout == LayoutSkillFile
}
if len(entry.FileHashes) == 0 {
return false
}
hashes, err := ComputeFileHashes(source.Path)
if err != nil {
return false
}
return CopiesSkillFileOnly(entry, hashes)
}