Files
Willie 9dbcd925d8 feat(audit): honor findings accepted with --force via --respect-accepted (#872)
* feat(audit): honor findings accepted with --force via --respect-accepted

install and update --force record the findings a user accepted in
audit_accepted, but audit ignored that record. A skill installed with
--force therefore kept failing `skillshare audit` in CI with no way to
let the accepted findings pass (#869).

audit --respect-accepted marks those findings as acknowledged. They stay
in every output (JSON acknowledged: true, a text/TUI note, "(accepted
earlier)" in Markdown, SARIF external suppressions), but they are counted
in summary.accepted instead of their severity and no longer fail the
audit. Without the flag nothing changes, so existing CI gates keep their
behavior. A changed file or snippet produces a new key and blocks again.

Tracked repositories record acceptances at the repo root with paths
relative to it, so ApplyAcceptedFindings walks up from each skill to
match them. The install, update and edit gates use the same function and
now honor repo-wide acceptances for skills inside tracked repos too.

The dashboard always honors accepted findings: the Audit page mutes them
and counts them apart, the skill Audit tab collapses them, and the
Dashboard attention card no longer lists a skill whose only blocking
findings were accepted. The CLI JSON summary and the server summary share
Result.CountOpenBySeverity so both count accepted findings the same way.
The oplog records the accepted count and the log views show it.

* fix(audit): match acceptances when a single file is scanned

A single-file scan names its findings by the file's base name, but the
ancestor lookup used the file's whole path as the prefix, so it hashed
SKILL.md/SKILL.md instead of the recorded SKILL.md. Auditing a skill file
directly with --respect-accepted, and auditing agents inside a tracked
repository in the CLI or the dashboard, still failed on unchanged
accepted findings. For a file target the prefix is now the file's own
directory.
2026-11-12 11:15:44 +08:00

106 lines
2.9 KiB
Go

package install
import (
"os"
"path"
"path/filepath"
"sort"
"strings"
"skillshare/internal/audit"
)
// acceptedKeyPath returns the .metadata.json key under which accepted findings
// for the resource at path are stored. Mirrors WriteMetaToStore's path logic.
func acceptedKeyPath(sourceDir, path string) (string, string, bool) {
if sourceDir == "" {
sourceDir = filepath.Dir(path)
}
rel, err := filepath.Rel(sourceDir, path)
if err != nil || rel == "." || strings.HasPrefix(rel, "..") {
return "", "", false
}
return sourceDir, filepath.ToSlash(rel), true
}
// ApplyAcceptedFindings marks findings previously accepted via --force for the
// resource at path so they no longer block. No-op when nothing was recorded.
func ApplyAcceptedFindings(sourceDir, target string, res *audit.Result) int {
if res == nil {
return 0
}
dir, rel, ok := acceptedKeyPath(sourceDir, target)
if !ok {
return 0
}
store, err := loadMetadataFile(dir)
if err != nil {
return 0
}
n := res.MarkAcknowledged(store.AuditAccepted[rel])
// Findings name files relative to the scanned directory; a single-file
// scan names them relative to the file's own directory.
scanned := rel
if info, err := os.Stat(target); err == nil && !info.IsDir() {
scanned = path.Dir(rel)
}
// A tracked repository records the acceptances of its whole-repo scan, and
// a skill directory those of its own scan, so a resource inside either also
// matches those, named relative to where they were recorded.
for parent := path.Dir(rel); parent != "."; parent = path.Dir(parent) {
if keys := store.AuditAccepted[parent]; len(keys) > 0 {
prefix := ""
if scanned != parent {
prefix = strings.TrimPrefix(scanned, parent+"/")
}
n += res.MarkAcknowledgedUnder(keys, prefix)
}
}
return n
}
// RecordAcceptedFindings persists the keys of unacknowledged findings at/above
// threshold — the ones the user just overrode — merged with earlier records.
// Returns the number of newly recorded keys.
func RecordAcceptedFindings(sourceDir, path string, res *audit.Result, threshold string) (int, error) {
if res == nil {
return 0, nil
}
keys := res.AcceptKeysAtOrAbove(threshold)
if len(keys) == 0 {
return 0, nil
}
dir, rel, ok := acceptedKeyPath(sourceDir, path)
if !ok {
return 0, nil
}
store, err := LoadMetadata(dir)
if err != nil {
return 0, err
}
if store.AuditAccepted == nil {
store.AuditAccepted = make(map[string][]string)
}
existing := make(map[string]bool, len(store.AuditAccepted[rel]))
for _, k := range store.AuditAccepted[rel] {
existing[k] = true
}
added := 0
for _, k := range keys {
if !existing[k] {
existing[k] = true
added++
}
}
if added == 0 {
return 0, nil
}
merged := make([]string, 0, len(existing))
for k := range existing {
merged = append(merged, k)
}
sort.Strings(merged)
store.AuditAccepted[rel] = merged
return added, store.Save(dir)
}