fix(audit): keep the skills source when auditing agents only (#412)

* fix(audit): keep the skills source when auditing agents only

`skillshare audit --agents` replaced sourcePath with the agents source
before building auditTUIContext, so the TUI's Skills tab discovered
skills inside the agents directory. The agents-only branches now read
agentsSourcePath themselves and the skills source is left alone.

Also from the #390 review, independent of .skillfollow:

- /api/skillignore reads the source through skillsSource() like the
  other routes (equal in project mode today; consistency only).
- scripts/windows/e2e-file-links.ps1 checked doctor output for a
  "Config:" line that no longer exists and could not match the ~-shortened
  path, so the run always aborted. It now checks status --json
  source.path, as e2e-skillfollow.ps1 does.
- windows-test runs internal/sourcewalk; GOOS=windows vet passes and the
  job on this PR is the first real run.

Refs #404, #407.

* fix(audit): relativize agent names against the agents source

With discovery moved to the agents source, auditInstalled and
auditFiltered still printed the header and computed SkillName against
the skills source, so `audit agents --json` reported names such as
`../agents/helper.md` when the two sources are siblings (Codex review on
#412). auditScanRoot now picks the root per kind for discovery, header,
and relativization; the skills source is kept only for TUI tab switching.

Also drops internal/sourcewalk from windows-test again: the first run on
windows-latest fails TestWalkParity, TestWalkDirParity and
TestSymlinkRoot (callback paths come from the canonical root, so
filepath.Rel against the test's root yields `..\..` chains). Logged on
#344.
This commit is contained in:
Willie
2026-10-05 11:29:39 +08:00
committed by GitHub
parent c61f48865e
commit d1371db7ea
5 changed files with 87 additions and 31 deletions
+19 -24
View File
@@ -205,11 +205,6 @@ func cmdAudit(args []string) error {
cfgPath = config.ConfigPath()
}
// When kind is agents-only, override sourcePath to the agents source directory.
if kind == kindAgents && agentsSourcePath != "" {
sourcePath = agentsSourcePath
}
policy := audit.ResolvePolicy(audit.PolicyInputs{
Profile: opts.Profile,
Threshold: opts.Threshold,
@@ -241,7 +236,7 @@ func cmdAudit(args []string) error {
case isSinglePath:
results, summary, err = auditPath(opts.Targets[0], modeString(mode), projectRoot, threshold, opts.Format, opts.PolicyLine, registry)
case isSingleName:
results, summary, err = auditSkillByName(sourcePath, opts.Targets[0], modeString(mode), projectRoot, threshold, opts.Format, opts.PolicyLine, kind, registry)
results, summary, err = auditSkillByName(auditScanRoot(kind, sourcePath, agentsSourcePath), opts.Targets[0], modeString(mode), projectRoot, threshold, opts.Format, opts.PolicyLine, kind, registry)
default:
results, summary, err = auditFiltered(sourcePath, agentsSourcePath, opts.Targets, opts.Groups, modeString(mode), projectRoot, threshold, kind, opts, registry)
}
@@ -499,6 +494,16 @@ func discoverForKind(kind resourceKindFilter, sourcePath string) ([]auditSkillRe
return collectInstalledSkillPaths(sourcePath)
}
// auditScanRoot picks the source a kind-filtered scan reads and relativizes
// against. The skills source is kept apart so the TUI can scan the other kind
// when switching tabs.
func auditScanRoot(kind resourceKindFilter, sourcePath, agentsSourcePath string) string {
if kind == kindAgents {
return agentsSourcePath
}
return sourcePath
}
func toInputsForKind(kind resourceKindFilter, items []auditSkillRef) []audit.SkillInput {
return toAuditInputs(items, kind == kindAgents)
}
@@ -531,13 +536,8 @@ func auditInstalled(sourcePath, agentsSourcePath, mode, projectRoot, threshold s
if !jsonOutput {
spinner = ui.StartSpinner(fmt.Sprintf("Discovering %s...", kind.Noun(2)))
}
var skillPaths []auditSkillRef
var err error
if kind == kindAgents {
skillPaths, err = collectInstalledAgentPaths(sourcePath)
} else {
skillPaths, err = collectInstalledSkillPaths(sourcePath)
}
scanRoot := auditScanRoot(kind, sourcePath, agentsSourcePath)
skillPaths, err := discoverForKind(kind, scanRoot)
if err != nil {
if spinner != nil {
spinner.Fail("Discovery failed")
@@ -570,7 +570,7 @@ func auditInstalled(sourcePath, agentsSourcePath, mode, projectRoot, threshold s
// Print the header before scan so user sees context while waiting.
if !jsonOutput {
printAuditHeader(mode, sourcePath, threshold, opts.PolicyLine)
printAuditHeader(mode, scanRoot, threshold, opts.PolicyLine)
}
// Phase 1: parallel scan with progress bar.
@@ -606,7 +606,7 @@ func auditInstalled(sourcePath, agentsSourcePath, mode, projectRoot, threshold s
sr.Result.IsBlocked = sr.Result.HasSeverityAtOrAbove(threshold)
sr.Result.Kind = kind.SingularNoun()
// Use relative path so TUI shows group hierarchy (e.g. "frontend/vue/skill").
if rel, err := filepath.Rel(sourcePath, sr.Result.ScanTarget); err == nil && rel != sr.Result.SkillName {
if rel, err := filepath.Rel(scanRoot, sr.Result.ScanTarget); err == nil && rel != sr.Result.SkillName {
sr.Result.SkillName = rel
}
results = append(results, sr.Result)
@@ -649,13 +649,8 @@ func auditFiltered(sourcePath, agentsSourcePath string, names, groups []string,
Threshold: threshold,
}
var allSkills []auditSkillRef
var err error
if kind == kindAgents {
allSkills, err = collectInstalledAgentPaths(sourcePath)
} else {
allSkills, err = collectInstalledSkillPaths(sourcePath)
}
scanRoot := auditScanRoot(kind, sourcePath, agentsSourcePath)
allSkills, err := discoverForKind(kind, scanRoot)
if err != nil {
return nil, base, err
}
@@ -715,7 +710,7 @@ func auditFiltered(sourcePath, agentsSourcePath string, names, groups []string,
// Print the header before scan so user sees context while waiting.
scanStart := time.Now()
if !jsonOutput {
printAuditHeader(mode, sourcePath, threshold, opts.PolicyLine)
printAuditHeader(mode, scanRoot, threshold, opts.PolicyLine)
}
// Phase 1: parallel scan with progress bar.
@@ -750,7 +745,7 @@ func auditFiltered(sourcePath, agentsSourcePath string, names, groups []string,
sr.Result.Threshold = threshold
sr.Result.IsBlocked = sr.Result.HasSeverityAtOrAbove(threshold)
sr.Result.Kind = kind.SingularNoun()
if rel, err := filepath.Rel(sourcePath, sr.Result.ScanTarget); err == nil && rel != sr.Result.SkillName {
if rel, err := filepath.Rel(scanRoot, sr.Result.ScanTarget); err == nil && rel != sr.Result.SkillName {
sr.Result.SkillName = rel
}
results = append(results, sr.Result)
@@ -0,0 +1,42 @@
package main
import (
"os"
"path/filepath"
"strings"
"testing"
"skillshare/internal/audit"
)
// The agents-only audit must read the agents source itself: the caller keeps
// the skills source untouched so the TUI's Skills tab scans skills, not agents.
func TestAuditInstalled_AgentsKindScansAgentsSource(t *testing.T) {
skills := t.TempDir()
agents := t.TempDir()
if err := os.MkdirAll(filepath.Join(skills, "demo"), 0755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(skills, "demo", "SKILL.md"), []byte("---\nname: demo\n---\n# demo\n"), 0644); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(agents, "helper.md"), []byte("---\nname: helper\n---\n# helper\n"), 0644); err != nil {
t.Fatal(err)
}
var results []*audit.Result
captureStdout(t, func() {
var err error
results, _, err = auditInstalled(skills, agents, "global", "", "medium", kindAgents, auditOptions{Format: formatJSON}, audit.DefaultRegistry())
if err != nil {
t.Errorf("auditInstalled: %v", err)
}
})
if len(results) != 1 || results[0].Kind != "agent" || !strings.HasPrefix(results[0].ScanTarget, agents) {
t.Fatalf("expected the one agent from the agents source, got %+v", results)
}
if results[0].SkillName != "helper.md" {
t.Fatalf("expected the name relative to the agents source, got %q", results[0].SkillName)
}
}
+2 -2
View File
@@ -45,7 +45,7 @@ func readIgnoreLocal(ignorePath string) *ignoreLocalFile {
func (s *Server) handleGetSkillignore(w http.ResponseWriter, r *http.Request) {
// Snapshot source path under RLock, then release before I/O.
s.mu.RLock()
source := s.cfg.EffectiveSkillsSource()
source := s.skillsSource()
s.mu.RUnlock()
ignorePath := filepath.Join(source, ".skillignore")
@@ -96,7 +96,7 @@ func (s *Server) handlePutSkillignore(w http.ResponseWriter, r *http.Request) {
return
}
source := s.cfg.EffectiveSkillsSource()
source := s.skillsSource()
ignorePath := filepath.Join(source, ".skillignore")
if body.Raw == "" {
@@ -6,6 +6,7 @@ import (
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
)
@@ -41,3 +42,20 @@ func TestHandleGetSkillignore_NoLocalFile(t *testing.T) {
t.Fatalf("expected no local file, got %+v", resp.Local)
}
}
// In project mode the route serves the project skills source; the handler now
// reads it through skillsSource() like the other source-reading routes.
func TestHandleGetSkillignore_ProjectModeUsesProjectSource(t *testing.T) {
s, projectRoot := newTestProjectServerWithExtras(t, nil)
rr := httptest.NewRecorder()
s.handler.ServeHTTP(rr, httptest.NewRequest(http.MethodGet, "/api/skillignore", nil))
var resp skillignoreResponse
if err := json.Unmarshal(rr.Body.Bytes(), &resp); err != nil {
t.Fatalf("decode: %v (body=%s)", err, rr.Body.String())
}
if !strings.HasPrefix(resp.Path, projectRoot+string(filepath.Separator)) {
t.Fatalf("expected .skillignore under the project source %s, got %s", projectRoot, resp.Path)
}
}
+6 -5
View File
@@ -90,11 +90,12 @@ extras:
- path: '$H\.codex'
"@ | Set-Content "$env:APPDATA\skillshare\config.yaml"
Section 'isolation check (doctor Config line)'
$doc = & $Exe doctor 2>&1 | ForEach-Object { "$_" }
$cfgLine = ($doc | Select-String 'Config:' | Select-Object -First 1) -join ''
Log $cfgLine.Trim()
if ($cfgLine -notlike "*$Root*") { Log 'ABORT: config is not under the test root'; Log 'DONE'; exit 1 }
# status --json reports the source from the loaded config; it matches only the config written above.
# (doctor prints the config path shortened to ~, so its text cannot prove the path is under $Root.)
Section 'isolation check (status --json source.path)'
$st = try { (& $Exe status --json 2> "$env:TEMP\stderr.txt" | Out-String) | ConvertFrom-Json } catch { $null }
Log "source.path=$($st.source.path)"
if (-not $st -or ($st.source.path.TrimEnd('\') -ine "$S\skills")) { Log 'ABORT: config is not under the test root'; Log 'DONE'; exit 1 }
$paths = @("$H\.claude\skills\demo-skill", "$H\.claude\agents\demo-agent.md", "$H\.claude\rules\style.md", "$H\.codex\AGENTS.md")
$manifests = @("$H\.claude\skills", "$H\.claude\agents", "$H\.claude\rules", "$H\.codex")