Files
Willie c0e66720ce fix(source-links): preserve trashed links and resolve audit and hash roots
Trash moves and restores the link entry without copying or removing its target, including when rename fails. Relative link text is stored as an absolute target so relocation cannot change its meaning; list and TUI expose the link target without inventorying its contents.

Pass the operation-scoped Follow policy into audit and hash operations, resolving their physical root while keeping logical result and metadata keys. Calls without a policy keep their existing behavior and ScanSkill retains its original function signature.

Update six stale trash raw-write allowances for the injected rename test seam and classify moveLink creation/removal: it moves a checked source entry to trash or restores an entry into source, never writes through its target. Raw sourcewalk calls and allowances are unchanged.
2026-10-05 14:45:41 +08:00

94 lines
2.2 KiB
Go

package audit
import (
"runtime"
"sync"
"time"
"skillshare/internal/sourcewalk"
)
// workerCount returns a bounded worker count based on available CPUs.
// Floor: 2 (always some parallelism). Ceiling: 32 (avoid fd exhaustion).
func workerCount() int {
n := runtime.NumCPU()
if n < 2 {
return 2
}
if n > 32 {
return 32
}
return n
}
// SkillInput describes a skill to scan.
type SkillInput struct {
Name string
Path string
IsFile bool // true for individual file scanning (agents)
}
// ScanOutput holds the result of scanning a single skill.
type ScanOutput struct {
Result *Result
Err error
Elapsed time.Duration
}
// ParallelScan scans skills concurrently with bounded workers.
// projectRoot being empty means global mode; non-empty means project mode.
// onDone is called after each skill finishes (nil-safe); use it to drive a progress bar.
// Returns []ScanOutput aligned by index with the input slice.
func ParallelScan(skills []SkillInput, projectRoot string, onDone func(), registry *Registry, follow ...*sourcewalk.Follow) []ScanOutput {
outputs := make([]ScanOutput, len(skills))
if len(skills) == 0 {
return outputs
}
sem := make(chan struct{}, workerCount())
var wg sync.WaitGroup
for i, sk := range skills {
wg.Add(1)
sem <- struct{}{}
go func(idx int, input SkillInput) {
defer wg.Done()
defer func() { <-sem }()
start := time.Now()
var res *Result
var err error
if input.IsFile {
// Agent: scan individual file
if projectRoot != "" {
res, err = ScanFileForProject(input.Path, projectRoot)
} else {
res, err = ScanFile(input.Path)
}
} else if registry != nil {
if projectRoot != "" {
res, err = ScanSkillFilteredForProject(input.Path, projectRoot, registry, follow...)
} else {
res, err = ScanSkillFiltered(input.Path, registry, follow...)
}
} else {
if projectRoot != "" {
res, err = ScanSkillForProject(input.Path, projectRoot, follow...)
} else {
res, err = scanSkillImpl(input.Path, nil, disabledIDsGlobal(), nil, follow...)
}
}
outputs[idx] = ScanOutput{
Result: res,
Err: err,
Elapsed: time.Since(start),
}
if onDone != nil {
onDone()
}
}(i, sk)
}
wg.Wait()
return outputs
}