fix: address code review findings

- Fix data race: read agentsSource inside RLock in handleDiffStream
- Validate kind param in handleSync (reject invalid values)
- Add test for symlink-points-elsewhere agent diff case
- Always show KindBadge on DiffItemRow (default to 'skill')
This commit is contained in:
Willie
2026-04-09 23:50:42 +08:00
parent 3dfbff7f53
commit 568d2b7f64
4 changed files with 37 additions and 2 deletions
@@ -56,6 +56,36 @@ func TestComputeAgentTargetDiff_LocalFile(t *testing.T) {
}
}
func TestComputeAgentTargetDiff_SymlinkPointsElsewhere(t *testing.T) {
sourceDir := t.TempDir()
otherDir := t.TempDir()
targetDir := t.TempDir()
srcFile := filepath.Join(sourceDir, "agent.md")
os.WriteFile(srcFile, []byte("# Agent"), 0644)
otherFile := filepath.Join(otherDir, "agent.md")
os.WriteFile(otherFile, []byte("# Other"), 0644)
// Symlink points to otherFile, not srcFile
os.Symlink(otherFile, filepath.Join(targetDir, "agent.md"))
agents := []resource.DiscoveredResource{
{FlatName: "agent.md", AbsPath: srcFile, RelPath: "agent.md"},
}
items := computeAgentTargetDiff(targetDir, agents)
if len(items) != 1 {
t.Fatalf("expected 1 item, got %d", len(items))
}
if items[0].Action != "update" {
t.Errorf("expected action 'update', got %q", items[0].Action)
}
if items[0].Reason != "symlink points elsewhere" {
t.Errorf("expected reason 'symlink points elsewhere', got %q", items[0].Reason)
}
}
func TestComputeAgentTargetDiff_InSync(t *testing.T) {
sourceDir := t.TempDir()
targetDir := t.TempDir()
+1 -1
View File
@@ -29,6 +29,7 @@ func (s *Server) handleDiffStream(w http.ResponseWriter, r *http.Request) {
// Snapshot config under RLock, then release before slow I/O.
s.mu.RLock()
source := s.cfg.Source
agentsSource := s.agentsSource()
globalMode := s.cfg.Mode
targets := s.cloneTargets()
s.mu.RUnlock()
@@ -68,7 +69,6 @@ func (s *Server) handleDiffStream(w http.ResponseWriter, r *http.Request) {
}
// Agent diffs — discover agents and compute per-target diffs
agentsSource := s.agentsSource()
var agents []resource.DiscoveredResource
if agentsSource != "" {
discovered, _ := resource.AgentKind{}.Discover(agentsSource)
+5
View File
@@ -57,6 +57,11 @@ func (s *Server) handleSync(w http.ResponseWriter, r *http.Request) {
// Default to non-dry-run, non-force, empty kind (both)
}
if body.Kind != "" && body.Kind != "skill" && body.Kind != "agent" {
writeError(w, http.StatusBadRequest, "invalid kind: must be 'skill', 'agent', or empty")
return
}
globalMode := s.cfg.Mode
if globalMode == "" {
globalMode = "merge"
+1 -1
View File
@@ -573,7 +573,7 @@ function DiffItemRow({ item }: { item: { action: string; skill: string; reason?:
<div className="flex items-center gap-2 text-base py-0.5">
<ActionBadge action={item.action} />
<ArrowRight size={12} className="text-muted-dark shrink-0" />
{item.kind && <KindBadge kind={item.kind} />}
<KindBadge kind={item.kind ?? 'skill'} />
<span className="font-mono text-pencil-light text-sm truncate">
{item.skill}
</span>