refactor: simplify audit code — constants, dedup helpers, cache pattern

- Add audit.CrossSkillResultName constant, use in Go + TS
- Merge toAuditInputs/toAgentAuditInputs into one with isFile param
- Extract discoverForKind() and toInputsForKind() to eliminate 5+ if-else branches
- Simplify launchAuditTUIWithTabs with otherKindFilter dispatch
- Use CROSS_SKILL_NAME constant in AuditPage.tsx
This commit is contained in:
Willie
2026-04-09 16:23:01 +08:00
parent f0f371c604
commit aaabc3ad88
4 changed files with 30 additions and 43 deletions
+17 -24
View File
@@ -477,18 +477,10 @@ func scanSkillPath(skillPath, projectRoot string, registry *audit.Registry) (*au
return audit.ScanSkill(skillPath)
}
func toAuditInputs(skills []auditSkillRef) []audit.SkillInput {
inputs := make([]audit.SkillInput, len(skills))
for i, s := range skills {
inputs[i] = audit.SkillInput{Name: s.name, Path: s.path}
}
return inputs
}
func toAgentAuditInputs(agents []auditSkillRef) []audit.SkillInput {
inputs := make([]audit.SkillInput, len(agents))
for i, a := range agents {
inputs[i] = audit.SkillInput{Name: a.name, Path: a.path, IsFile: true}
func toAuditInputs(items []auditSkillRef, isFile bool) []audit.SkillInput {
inputs := make([]audit.SkillInput, len(items))
for i, item := range items {
inputs[i] = audit.SkillInput{Name: item.name, Path: item.path, IsFile: isFile}
}
return inputs
}
@@ -512,6 +504,17 @@ func collectInstalledAgentPaths(agentsSourcePath string) ([]auditSkillRef, error
return agentPaths, nil
}
func discoverForKind(kind resourceKindFilter, sourcePath string) ([]auditSkillRef, error) {
if kind == kindAgents {
return collectInstalledAgentPaths(sourcePath)
}
return collectInstalledSkillPaths(sourcePath)
}
func toInputsForKind(kind resourceKindFilter, items []auditSkillRef) []audit.SkillInput {
return toAuditInputs(items, kind == kindAgents)
}
func scanPathTarget(targetPath, projectRoot string, registry *audit.Registry) (*audit.Result, error) {
info, err := os.Stat(targetPath)
if err != nil {
@@ -596,12 +599,7 @@ func auditInstalled(sourcePath, agentsSourcePath, mode, projectRoot, threshold s
progressBar.Increment()
}
}
var scanInputs []audit.SkillInput
if kind == kindAgents {
scanInputs = toAgentAuditInputs(skillPaths)
} else {
scanInputs = toAuditInputs(skillPaths)
}
scanInputs := toInputsForKind(kind, skillPaths)
scanResults := audit.ParallelScan(scanInputs, projectRoot, onDone, reg)
if progressBar != nil {
progressBar.Stop()
@@ -755,12 +753,7 @@ func auditFiltered(sourcePath, agentsSourcePath string, names, groups []string,
progressBar.Increment()
}
}
var scanInputs []audit.SkillInput
if kind == kindAgents {
scanInputs = toAgentAuditInputs(matched)
} else {
scanInputs = toAuditInputs(matched)
}
scanInputs := toInputsForKind(kind, matched)
scanResults := audit.ParallelScan(scanInputs, projectRoot, onDone, reg)
if progressBar != nil {
progressBar.Stop()
+6 -17
View File
@@ -77,27 +77,16 @@ func launchAuditTUIWithTabs(results []*audit.Result, scanOutputs []audit.ScanOut
var otherOutputs []audit.ScanOutput
var otherSummary auditRunSummary
otherKindFilter := kindAgents
otherSource := ctx.agentsSourcePath
otherKind := "agent"
if ctx.kind == kindAgents {
otherKindFilter = kindSkills
otherSource = ctx.sourcePath
otherKind = "skill"
}
if otherSource != "" {
var otherPaths []auditSkillRef
var err error
if otherKind == "agent" {
otherPaths, err = collectInstalledAgentPaths(otherSource)
} else {
otherPaths, err = collectInstalledSkillPaths(otherSource)
}
var otherInputs []audit.SkillInput
if otherKind == "agent" {
otherInputs = toAgentAuditInputs(otherPaths)
} else {
otherInputs = toAuditInputs(otherPaths)
}
otherPaths, err := discoverForKind(otherKindFilter, otherSource)
otherInputs := toInputsForKind(otherKindFilter, otherPaths)
if err == nil && len(otherPaths) > 0 {
otherScanResults := audit.ParallelScan(otherInputs, ctx.projectRoot, nil, ctx.registry)
for i := range otherPaths {
@@ -106,7 +95,7 @@ func launchAuditTUIWithTabs(results []*audit.Result, scanOutputs []audit.ScanOut
if sr.Err == nil {
sr.Result.Threshold = ctx.threshold
sr.Result.IsBlocked = sr.Result.HasSeverityAtOrAbove(ctx.threshold)
sr.Result.Kind = otherKind
sr.Result.Kind = otherKindFilter.SingularNoun()
if rel, relErr := filepath.Rel(otherSource, sr.Result.ScanTarget); relErr == nil {
sr.Result.SkillName = rel
}
@@ -124,7 +113,7 @@ func launchAuditTUIWithTabs(results []*audit.Result, scanOutputs []audit.ScanOut
var filteredResults []*audit.Result
var filteredOutputs []audit.ScanOutput
for i, r := range results {
if r.SkillName != "_cross-skill" {
if r.SkillName != audit.CrossSkillResultName {
filteredResults = append(filteredResults, r)
if i < len(scanOutputs) {
filteredOutputs = append(filteredOutputs, scanOutputs[i])
+4 -1
View File
@@ -6,6 +6,9 @@ import (
"strings"
)
// CrossSkillResultName is the synthetic skill name used for cross-skill analysis results.
const CrossSkillResultName = "_cross-skill"
// skillCapability summarises the security-relevant capabilities of a single skill,
// derived entirely from its existing Result (TierProfile + Findings).
type skillCapability struct {
@@ -134,7 +137,7 @@ func CrossSkillAnalysis(results []*Result) *Result {
}
r := &Result{
SkillName: "_cross-skill",
SkillName: CrossSkillResultName,
Findings: findings,
Analyzability: 1.0,
}
+3 -1
View File
@@ -35,6 +35,8 @@ import { queryKeys, staleTimes } from '../lib/queryKeys';
type SeverityFilter = 'CRITICAL' | 'HIGH' | 'MEDIUM' | 'LOW' | 'INFO';
type AuditKind = 'skills' | 'agents';
const CROSS_SKILL_NAME = '_cross-skill';
const severityFilterOptions: { value: SeverityFilter; label: string }[] = [
{ value: 'INFO', label: 'All (INFO+)' },
{ value: 'LOW', label: 'LOW+' },
@@ -79,7 +81,7 @@ export default function AuditPage() {
// Cross-skill analysis is a derived insight, not an actual scanned resource.
const realResults = useMemo(() => {
if (!data) return [];
return data.results.filter((r) => r.skillName !== '_cross-skill');
return data.results.filter((r) => r.skillName !== CROSS_SKILL_NAME);
}, [data]);
const totalFindings = useMemo(() => {