fix(status): expect only the agents a target would sync

status counted every discovered agent against each target, while sync
agents writes only the agents left after .agentignore, the target's
include/exclude and the frontmatter targets field. A target that leaves
some agents out was reported as N/M with drift after a complete sync,
in the table and in --json, in global and project mode. doctor and the
dashboard already filtered per target.

status now applies the same three filters before counting and expecting.
The table reports an invalid include/exclude as an error, like it does
for skills; --json falls back to the active agents.

Refs #395
This commit is contained in:
Willie
2026-10-04 22:21:00 +08:00
parent a265155844
commit 47a98b81ff
4 changed files with 83 additions and 10 deletions
+7 -2
View File
@@ -347,9 +347,14 @@ func printTargetsStatus(cfg *config.Config, discovered []sync.DiscoveredSkill) e
}
if agentsExist {
if agentPath := resolveAgentTargetPath(target, builtinAgents, name); agentPath != "" {
ac := target.AgentsConfig()
expected, err := expectedAgentsForTarget(ac, name, agents)
if err != nil {
return err
}
preserved := 0
linked := countLinkedAgents(target.AgentsConfig(), agentPath, agents, &preserved)
row.agents = agentsCell(linked, len(agents), preserved)
linked := countLinkedAgents(ac, agentPath, expected, &preserved)
row.agents = agentsCell(linked, len(expected), preserved)
}
}
rows = append(rows, row)
+20 -3
View File
@@ -48,19 +48,36 @@ func buildAgentStatusJSON(cfg *config.Config) *statusJSONAgents {
continue
}
linked := countLinkedAgents(target.AgentsConfig(), agentPath, agents)
ac := target.AgentsConfig()
expected, err := expectedAgentsForTarget(ac, name, agents)
if err != nil {
expected = resource.ActiveAgents(agents) // invalid include/exclude: keep .agentignore at least
}
linked := countLinkedAgents(ac, agentPath, expected)
result.Targets = append(result.Targets, statusJSONAgentTarget{
Name: name,
Path: agentPath,
Expected: len(agents),
Expected: len(expected),
Linked: linked,
Drift: linked != len(agents) && len(agents) > 0,
Drift: linked != len(expected) && len(expected) > 0,
})
}
return result
}
// expectedAgentsForTarget applies the filters sync agents applies before
// writing to a target: .agentignore (ActiveAgents), the target's
// include/exclude, and the agents' frontmatter targets. status counts and
// expects only these, so a target that leaves some agents out is not drift.
func expectedAgentsForTarget(ac config.ResourceTargetConfig, targetName string, agents []resource.DiscoveredResource) ([]resource.DiscoveredResource, error) {
filtered, err := sync.FilterAgents(resource.ActiveAgents(agents), ac.Include, ac.Exclude)
if err != nil {
return nil, fmt.Errorf("target %s has invalid agent include/exclude config: %w", targetName, err)
}
return sync.FilterAgentsByTarget(filtered, targetName), nil
}
// countLinkedAgents counts healthy .md symlinks in the target agent directory,
// plus up-to-date copies made where file links are unavailable. With an
// extension, it counts the tracked converted outputs instead.
+15 -5
View File
@@ -142,13 +142,18 @@ func buildProjectAgentStatusJSON(rt *projectRuntime) *statusJSONAgents {
continue
}
linked := countLinkedAgents(entry.AgentsConfig(), agentPath, agents)
ac := entry.AgentsConfig()
expected, err := expectedAgentsForTarget(ac, entry.Name, agents)
if err != nil {
expected = resource.ActiveAgents(agents) // invalid include/exclude: keep .agentignore at least
}
linked := countLinkedAgents(ac, agentPath, expected)
result.Targets = append(result.Targets, statusJSONAgentTarget{
Name: entry.Name,
Path: agentPath,
Expected: len(agents),
Expected: len(expected),
Linked: linked,
Drift: linked != len(agents) && len(agents) > 0,
Drift: linked != len(expected) && len(expected) > 0,
})
}
@@ -214,9 +219,14 @@ func printProjectTargetsStatus(runtime *projectRuntime, discovered []sync.Discov
}
if agentsExist {
if agentPath := resolveProjectAgentTargetPath(entry, builtinAgents, runtime.root); agentPath != "" {
ac := entry.AgentsConfig()
expected, err := expectedAgentsForTarget(ac, entry.Name, agents)
if err != nil {
return err
}
preserved := 0
linked := countLinkedAgents(entry.AgentsConfig(), agentPath, agents, &preserved)
row.agents = agentsCell(linked, len(agents), preserved)
linked := countLinkedAgents(ac, agentPath, expected, &preserved)
row.agents = agentsCell(linked, len(expected), preserved)
}
}
rows = append(rows, row)
@@ -6,6 +6,7 @@ import (
"encoding/json"
"os"
"path/filepath"
"strings"
"testing"
"skillshare/internal/testutil"
@@ -279,3 +280,43 @@ targets:
result.AssertAnyOutputContains(t, `"expected"`)
result.AssertAnyOutputContains(t, `"linked"`)
}
// Refs #395: status counted every discovered agent, so a target that excludes
// some of them was reported as drifted after a complete sync.
func TestStatus_Agents_JSON_AppliesSyncFilters(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
agentsDir := createAgentSource(t, sb, map[string]string{
"tutor.md": "# Tutor",
"reviewer.md": "# Reviewer",
"ignored.md": "# Ignored",
"cursor.md": "---\ntargets: [cursor]\n---\n# Cursor only",
})
sb.WriteFile(filepath.Join(agentsDir, ".agentignore"), "ignored.md\n")
claudeAgents := createAgentTarget(t, sb, "claude")
sb.WriteConfig(`source: ` + sb.SourcePath + `
targets:
claude:
skills:
path: ` + sb.CreateTarget("claude") + `
agents:
path: ` + claudeAgents + `
exclude: [reviewer]
`)
sb.RunCLI("sync", "agents").AssertSuccess(t)
result := sb.RunCLI("status", "--json")
result.AssertSuccess(t)
if !strings.Contains(result.Stdout, `"expected": 1`) || !strings.Contains(result.Stdout, `"linked": 1`) || !strings.Contains(result.Stdout, `"drift": false`) {
t.Errorf("status --json should expect only tutor (1/1, no drift), got:\n%s", result.Stdout)
}
human := sb.RunCLI("status")
human.AssertSuccess(t)
if strings.Contains(human.Stdout, "1/4") {
t.Errorf("status should not report 1/4 agents, got:\n%s", human.Stdout)
}
}