mirror of
https://github.com/runkids/skillshare.git
synced 2026-11-19 02:17:09 +00:00
fix(status): report drift when an extension agent's source changed
Counting an extension output as synced from its output hash alone missed the common case: the user edits the agent in the source and runs status to see whether a sync is due. The output still matched the manifest, so status, doctor and the dashboard reported no drift although the next sync would regenerate it. Sync now records a SHA-256 of the source each output was converted from in the manifest (sources), and status counts an output only when both the output and that fingerprint are current. Outputs recorded by earlier releases have no fingerprint and count as current until the next sync records one, so upgrading does not show the 0/N the report was about. Refs #391, Codex review on #392.
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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"`
|
||||
}
|
||||
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user