diff --git a/internal/sync/agent_sync.go b/internal/sync/agent_sync.go index 387654a0f..f8309a551 100644 --- a/internal/sync/agent_sync.go +++ b/internal/sync/agent_sync.go @@ -288,11 +288,13 @@ func SyncedAgentCopies(targetDir string, agents []resource.DiscoveredResource, p } // SyncedExtensionOutputs counts agents whose extension output is tracked in -// the target's manifest and unchanged since sync. An extension renames the -// output per output_ext and transforms its content, so neither the source -// name nor the source content can be compared; the manifest hash is the only -// record of what sync wrote. Any output_ext is matched by stem, so callers -// need not load the extension spec. Refs #391. +// the target's manifest, unchanged since sync, and converted from the +// source's current content. An extension renames the output per output_ext +// and transforms its content, so neither the source name nor the source +// content can be compared with the output; the manifest's output hash and +// source fingerprint are the record of what sync wrote and from what. Any +// output_ext is matched by stem, so callers need not load the extension +// spec. Refs #391. func SyncedExtensionOutputs(targetDir string, agents []resource.DiscoveredResource) int { copies := loadCopyTracker(targetDir) n := 0 @@ -302,7 +304,8 @@ func SyncedExtensionOutputs(targetDir string, agents []resource.DiscoveredResour if strings.TrimSuffix(key, filepath.Ext(key)) != stem { continue } - if copies.owns(filepath.FromSlash(key)) { + rel := filepath.FromSlash(key) + if copies.owns(rel) && copies.sourceMatches(rel, a.AbsPath) { n++ break } @@ -516,6 +519,7 @@ func SyncAgentsTransform(agents []resource.DiscoveredResource, sourceDir, target } if readErr == nil && bytes.Equal(existing, out) && !force { copies.record(name) // adopts outputs made before tracking + copies.recordSource(name, agent.AbsPath) result.Linked = append(result.Linked, name) continue } @@ -527,6 +531,7 @@ func SyncAgentsTransform(agents []resource.DiscoveredResource, sourceDir, target continue } copies.record(name) + copies.recordSource(name, agent.AbsPath) if readErr == nil { result.Updated = append(result.Updated, name) } else { diff --git a/internal/sync/filelink.go b/internal/sync/filelink.go index 7661f1811..2fcbe4c2a 100644 --- a/internal/sync/filelink.go +++ b/internal/sync/filelink.go @@ -89,12 +89,44 @@ func (t *copyTracker) record(rel string) { } } +// recordSource remembers which source content rel was converted from, so +// status can tell a stale extension output from a current one without +// running the extension. +func (t *copyTracker) recordSource(rel, srcPath string) { + key := filepath.ToSlash(rel) + h, err := utils.FileHash(srcPath) + if err != nil || t.m.Sources[key] == h { + return + } + if t.m.Sources == nil { + t.m.Sources = make(map[string]string) + } + t.m.Sources[key] = h + t.changed = true +} + +// sourceMatches reports whether rel was converted from srcPath's current +// content. An output recorded before source fingerprints existed has no +// entry and is taken as current; the next sync records it. +func (t *copyTracker) sourceMatches(rel, srcPath string) bool { + sum, ok := t.m.Sources[filepath.ToSlash(rel)] + if !ok { + return true + } + h, err := utils.FileHash(srcPath) + return err == nil && h == sum +} + func (t *copyTracker) forget(rel string) { key := filepath.ToSlash(rel) if _, ok := t.m.Managed[key]; ok { delete(t.m.Managed, key) t.changed = true } + if _, ok := t.m.Sources[key]; ok { + delete(t.m.Sources, key) + t.changed = true + } } // pruneOrphans removes tracked copies whose name is not in expected, keeping diff --git a/internal/sync/filelink_test.go b/internal/sync/filelink_test.go index bac55c5ea..2cab7cb6d 100644 --- a/internal/sync/filelink_test.go +++ b/internal/sync/filelink_test.go @@ -259,6 +259,26 @@ func TestSyncedExtensionOutputs_CountsTrackedRenamedOutputs(t *testing.T) { } } +func TestSyncedExtensionOutputs_ChangedSourceIsDrift(t *testing.T) { + src, tgt, agents := agentFixture(t) + spec := &ExtensionSpec{Run: []string{"cat"}, Dir: src, Name: "id", OutputExt: "toml"} + if _, err := SyncAgentsTransform(agents, src, tgt, "copy", spec, false, false); err != nil { + t.Fatal(err) + } + + os.WriteFile(agents[0].AbsPath, []byte("v2"), 0644) + + if n := SyncedExtensionOutputs(tgt, agents); n != 0 { + t.Fatalf("output of an old source counted: %d, want 0", n) + } + if _, err := SyncAgentsTransform(agents, src, tgt, "copy", spec, false, false); err != nil { + t.Fatal(err) + } + if n := SyncedExtensionOutputs(tgt, agents); n != 1 { + t.Errorf("after resync = %d, want 1", n) + } +} + func TestSyncAgents_MergeWithoutFileLinksPreservesIdenticalLocalFile(t *testing.T) { withoutFileLinks(t) src, tgt, agents := agentFixture(t) diff --git a/internal/sync/manifest.go b/internal/sync/manifest.go index e5394ead5..e750fb549 100644 --- a/internal/sync/manifest.go +++ b/internal/sync/manifest.go @@ -15,8 +15,9 @@ const ManifestFile = ".skillshare-manifest.json" // Manifest tracks which skills are managed by skillshare in a target directory. // Used by both merge mode (values: "symlink") and copy mode (values: SHA-256 checksum). type Manifest struct { - Managed map[string]string `json:"managed"` // flatName → "symlink" (merge) or SHA-256 checksum (copy) - Mtimes map[string]int64 `json:"mtimes,omitempty"` // flatName → source dir max mtime (UnixNano), copy mode only + Managed map[string]string `json:"managed"` // flatName → "symlink" (merge) or SHA-256 checksum (copy) + Mtimes map[string]int64 `json:"mtimes,omitempty"` // flatName → source dir max mtime (UnixNano), copy mode only + Sources map[string]string `json:"sources,omitempty"` // output name → SHA-256 of the source it was converted from, extension outputs only UpdatedAt time.Time `json:"updated_at"` } diff --git a/tests/integration/sync_agent_extension_test.go b/tests/integration/sync_agent_extension_test.go index 0dcd4ae53..313c1b277 100644 --- a/tests/integration/sync_agent_extension_test.go +++ b/tests/integration/sync_agent_extension_test.go @@ -80,6 +80,19 @@ func TestStatus_AgentsExtension_CountsConvertedOutputs(t *testing.T) { if !strings.Contains(doctor.Stdout, "2/2 linked") { t.Errorf("doctor should report 2/2 linked, got:\n%s", doctor.Stdout) } + + // A changed source is drift until it is converted again. + createAgentSource(t, sb, map[string]string{"reviewer.md": "body v2"}) + res = sb.RunCLI("status", "--json") + res.AssertSuccess(t) + if !strings.Contains(res.Stdout, `"linked": 1`) || !strings.Contains(res.Stdout, `"drift": true`) { + t.Errorf("status --json should report drift after a source change, got:\n%s", res.Stdout) + } + sb.RunCLI("sync", "agents").AssertSuccess(t) + res = sb.RunCLI("status", "--json") + if !strings.Contains(res.Stdout, `"linked": 2`) || !strings.Contains(res.Stdout, `"drift": false`) { + t.Errorf("status --json should report 2/2 after resync, got:\n%s", res.Stdout) + } } func TestSync_AgentsExtension_RejectsMergeMode(t *testing.T) {