fix(server): skip extras targets that the agents sync manages

The CLI skips targets of the "agents" extra whose path the agents sync
already writes to, so the two syncs do not fight over the same folder.
The Web API synced them anyway. It now builds the same set of agent
target paths and marks those targets with skipped_by "agents", the CLI's
JSON field name; existing response fields are unchanged.
This commit is contained in:
Willie
2026-09-30 02:07:22 +08:00
parent b62faeed60
commit b062a11322
2 changed files with 47 additions and 16 deletions
+40 -9
View File
@@ -14,6 +14,7 @@ import (
"time"
"skillshare/internal/config"
"skillshare/internal/resource"
syncpkg "skillshare/internal/sync"
"skillshare/internal/utils"
)
@@ -569,14 +570,15 @@ func (s *Server) handleExtrasCreate(w http.ResponseWriter, r *http.Request) {
}
type extraTargetSyncResult struct {
Target string `json:"target"`
Mode string `json:"mode"`
Synced int `json:"synced"`
Skipped int `json:"skipped"`
Pruned int `json:"pruned"`
Errors []string `json:"errors,omitempty"`
Error string `json:"error,omitempty"`
Warnings []string `json:"warnings,omitempty"`
Target string `json:"target"`
Mode string `json:"mode"`
Synced int `json:"synced"`
Skipped int `json:"skipped"`
Pruned int `json:"pruned"`
Errors []string `json:"errors,omitempty"`
Error string `json:"error,omitempty"`
Warnings []string `json:"warnings,omitempty"`
SkippedBy string `json:"skipped_by,omitempty"`
}
type extraSyncResult struct {
@@ -643,7 +645,6 @@ func (s *Server) syncExtras(name string, dryRun, force bool) []extraSyncResult {
projectExtrasParent = s.projectCfg.EffectiveExtrasSource(s.projectRoot)
}
// The server does not skip targets managed by the agents sync.
opts := syncpkg.ExtraRunOptions{
DryRun: dryRun,
Force: force,
@@ -651,6 +652,12 @@ func (s *Server) syncExtras(name string, dryRun, force bool) []extraSyncResult {
ResolvePath: func(path string) string { return resolveExtrasTargetPath(projectRoot, path) },
ResolveExtension: s.resolveExtensionSpec,
}
for _, extra := range s.extrasConfig() {
if extra.Name == "agents" {
opts.AgentTargetPaths = s.extrasAgentTargetPaths()
break
}
}
results := make([]extraSyncResult, 0)
@@ -682,6 +689,9 @@ func (s *Server) syncExtras(name string, dryRun, force bool) []extraSyncResult {
Errors: []string{},
}
switch {
case tr.SkippedBy != "":
out.Mode = tr.Mode
out.SkippedBy = tr.SkippedBy
case tr.ExtensionErr != nil:
out.Error = "extension " + t.Extension + ": " + tr.ExtensionErr.Error()
case tr.ModeErr != nil:
@@ -708,6 +718,27 @@ func (s *Server) syncExtras(name string, dryRun, force bool) []extraSyncResult {
return results
}
// extrasAgentTargetPaths returns the cleaned agent target paths the agents
// sync writes to, or nil when the agents source holds no agents, matching the
// CLI. Callers must hold s.mu.
func (s *Server) extrasAgentTargetPaths() map[string]bool {
agentsSource := s.agentsSource()
if _, err := os.Stat(agentsSource); err != nil {
return nil
}
if agents, err := (resource.AgentKind{}).Discover(agentsSource); err != nil || len(agents) == 0 {
return nil
}
builtinAgents := s.builtinAgentTargets()
paths := make(map[string]bool)
for name, target := range s.cfg.Targets {
if p := resolveAgentPath(target, builtinAgents, name, s.IsProjectMode()); p != "" {
paths[filepath.Clean(resolveExtrasTargetPath(s.projectRoot, p))] = true
}
}
return paths
}
// handleExtrasMode — PATCH /api/extras/{name}/mode
func (s *Server) handleExtrasMode(w http.ResponseWriter, r *http.Request) {
start := time.Now()
+7 -7
View File
@@ -748,9 +748,9 @@ func postExtrasSync(t *testing.T, s *Server, body string) []map[string]any {
return resp.Extras[0].Targets
}
// Characterization: unlike the CLI, the server syncs an "agents" extra target
// even when it is also an agents sync target.
func TestHandleExtrasSync_DoesNotSkipAgentOverlapTarget(t *testing.T) {
// Like the CLI, the server skips an "agents" extra target that the agents sync
// already manages.
func TestHandleExtrasSync_SkipsAgentOverlapTarget(t *testing.T) {
agentsTarget := t.TempDir()
extras := []config.ExtraConfig{{
Name: "agents",
@@ -774,11 +774,11 @@ func TestHandleExtrasSync_DoesNotSkipAgentOverlapTarget(t *testing.T) {
targets := postExtrasSync(t, s, `{"name":"agents"}`)
if _, ok := targets[0]["skipped_by"]; ok {
t.Errorf("server must not report skipped_by, got %v", targets[0])
if got := targets[0]["skipped_by"]; got != "agents" {
t.Errorf("skipped_by = %v, want %q (full entry %v)", got, "agents", targets[0])
}
if _, err := os.Stat(filepath.Join(agentsTarget, "extra-agent.md")); err != nil {
t.Errorf("expected extra-agent.md synced into the agents target: %v", err)
if _, err := os.Stat(filepath.Join(agentsTarget, "extra-agent.md")); !os.IsNotExist(err) {
t.Errorf("extra-agent.md must not be synced into the agents target, stat err = %v", err)
}
}