From 47a98b81ff2becef23d496d37e137baecfa078bf Mon Sep 17 00:00:00 2001 From: Willie Date: Sun, 4 Oct 2026 22:21:00 +0800 Subject: [PATCH] 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 --- cmd/skillshare/status.go | 9 +++- cmd/skillshare/status_agents.go | 23 +++++++++-- cmd/skillshare/status_project.go | 20 ++++++--- tests/integration/agent_coverage_gaps_test.go | 41 +++++++++++++++++++ 4 files changed, 83 insertions(+), 10 deletions(-) diff --git a/cmd/skillshare/status.go b/cmd/skillshare/status.go index a3ceb0f91..1033c20b2 100644 --- a/cmd/skillshare/status.go +++ b/cmd/skillshare/status.go @@ -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) diff --git a/cmd/skillshare/status_agents.go b/cmd/skillshare/status_agents.go index b6775ca27..adbaea680 100644 --- a/cmd/skillshare/status_agents.go +++ b/cmd/skillshare/status_agents.go @@ -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. diff --git a/cmd/skillshare/status_project.go b/cmd/skillshare/status_project.go index ddf47af6e..c2ce36b04 100644 --- a/cmd/skillshare/status_project.go +++ b/cmd/skillshare/status_project.go @@ -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) diff --git a/tests/integration/agent_coverage_gaps_test.go b/tests/integration/agent_coverage_gaps_test.go index 6be6ddc13..c40efb5ed 100644 --- a/tests/integration/agent_coverage_gaps_test.go +++ b/tests/integration/agent_coverage_gaps_test.go @@ -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) + } +}