Files
skillshare/internal/audit/parallel.go
T
Willie 1b9ba78719 fix(audit): scan agents as individual files, not directories
Agents are individual .md files discovered by resource.AgentKind{}.Discover(),
but audit was using collectInstalledSkillPaths() which calls
sync.DiscoverSourceSkillsLite() — designed for skill directories. Then
ParallelScan() called ScanSkill() which expects directories, not files.

Changes:
- Add IsFile flag to audit.SkillInput; when set, ParallelScan uses
  ScanFile/ScanFileForProject instead of ScanSkill variants
- Add collectInstalledAgentPaths() using resource.AgentKind{}.Discover()
  and resource.ActiveAgents() for proper agent file discovery
- Add toAgentAuditInputs() helper that sets IsFile=true
- Branch on kindAgents in auditInstalled(), auditFiltered(), and
  launchAuditTUIWithTabs() to use agent-specific discovery and inputs
- Add discoverAuditAgents() in server handler and update
  resolveAuditSource() to return isAgents flag
- Update handleAuditAll, handleAuditStream to use agent-aware discovery
  and build SkillInput with IsFile=true for agents
2026-04-09 14:45:24 +08:00

92 lines
2.1 KiB
Go

package audit
import (
"runtime"
"sync"
"time"
)
// 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) []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)
} else {
res, err = ScanSkillFiltered(input.Path, registry)
}
} else {
if projectRoot != "" {
res, err = ScanSkillForProject(input.Path, projectRoot)
} else {
res, err = ScanSkill(input.Path)
}
}
outputs[idx] = ScanOutput{
Result: res,
Err: err,
Elapsed: time.Since(start),
}
if onDone != nil {
onDone()
}
}(i, sk)
}
wg.Wait()
return outputs
}