Files
skillshare/internal/server/handler_audit_stream.go
T
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

100 lines
2.5 KiB
Go

package server
import (
"net/http"
"sync"
"sync/atomic"
"time"
"skillshare/internal/audit"
)
// handleAuditStream serves an SSE endpoint that streams audit progress in real time.
// Events:
// - "start" → {"total": N} after skill discovery
// - "progress" → {"scanned": N} every 200ms during scan
// - "done" → {"results":…,"summary":…} final payload (same shape as GET /api/audit)
func (s *Server) handleAuditStream(w http.ResponseWriter, r *http.Request) {
safeSend, ok := initSSE(w)
if !ok {
return
}
start := time.Now()
// Snapshot config under RLock, then release before slow I/O.
s.mu.RLock()
source, resultKind, isAgents := s.resolveAuditSource(r)
projectRoot := s.projectRoot
policy := s.auditPolicy()
s.mu.RUnlock()
// 1. Discover skills/agents
walk := s.skillsWalk()
var skills []skillEntry
var err error
if isAgents {
skills, err = discoverAuditAgents(source)
} else {
skills, err = discoverAuditSkills(source, walk)
}
if err != nil {
safeSend("error", map[string]string{"error": err.Error()})
return
}
safeSend("start", map[string]int{"total": len(skills)})
// 2. Atomic counter + ticker for progress events
var scanned atomic.Int64
ctx := r.Context()
done := make(chan struct{})
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
ticker := time.NewTicker(200 * time.Millisecond)
defer ticker.Stop()
for {
select {
case <-ticker.C:
safeSend("progress", map[string]int64{"scanned": scanned.Load()})
case <-done:
return
case <-ctx.Done():
return
}
}
}()
onDone := func() { scanned.Add(1) }
// 3. Parallel scan (blocks until all skills are scanned)
var inputs []audit.SkillInput
if isAgents {
inputs = make([]audit.SkillInput, len(skills))
for i, s := range skills {
inputs[i] = audit.SkillInput{Name: s.name, Path: s.path, IsFile: true}
}
} else {
inputs = skillsToAuditInputs(skills)
}
outputs := audit.ParallelScan(inputs, projectRoot, onDone, nil, walk.Follow)
close(done) // signal ticker goroutine to stop
wg.Wait() // wait for it to fully exit before writing to w
// 4. Process results
agg := processAuditResults(skills, outputs, policy, projectRoot)
for i := range agg.Results {
agg.Results[i].Kind = resultKind
}
s.writeAuditLog(agg.Status, start, agg.LogArgs, agg.Message)
// 5. Send final result (no concurrent writers at this point)
safeSend("done", map[string]any{
"results": agg.Results,
"summary": agg.Summary,
})
}