Files
skillshare/internal/server/handler_diff_stream.go
T
Rusty ShacklefordandWillie 85bcf589a7 feat(sync): recognize and route agent-specific skill variants (#649)
* docs(proposals): propose agent-specific skill variants

Tracked repositories can ship different files for the same skill across
agents. Counting and syncing each copy independently sends incompatible
instructions to every target unless users maintain routing filters.

Propose repository-scoped logical identity and exact variant routing while
preserving source files, explicit controls, and existing naming behavior.
Leave activation and compatibility decisions for maintainer review.

Refs #648

* docs(proposals): clarify variant routing compatibility

Explain how recognition affects universal-only targets and how existing
per-variant overrides preserve the desired routing. Fix the precedence
between variant identity and generic host-style path inference.

Include analyze, move, and catalog consumers while preserving physical
identifiers and actual context-cost accounting.

Refs #648

* docs(proposals): clarify activation assumptions and scope

Make acceptance cases conditional on maintainer-approved recognition being
enabled for the tracked source. Separate the provisional implementation
estimate from this proposal-only PR so its size is clear.

Refs #648

* docs(proposals): qualify naming and opt-in assumptions

State that the reproduced routing behavior uses default flat target
naming. Leave any opt-in surface with the maintainer's activation decision
without ruling out future CLI or config fields.

Refs #648

* feat: recognize and route tracked agent skill variants

Count tracked variants as logical skills in source and repository summaries,
while preserving physical paths, sync operations, and context costs. Select
matching canonical agent variants, including account targets, through shared
discovery and selection with existing explicit routing precedence.

Refs #648

* fix: count logical skills in sync inventory summaries

Use shared logical and physical inventory counts in global and project sync
closing lines while preserving each target's operation totals.

Refs #648

* fix: preserve variant routing diagnostics across previews

Resolve target selections once so CLI and server previews keep ambiguity
reasons and physical skip counts. Explain managed variants removed by
automatic agent routing, and report logical inventory in doctor.

Keep warning rows read-only, document upgrade recovery in translated
filtering references, and remove the optional README-only addition.

Refs #648

* fix: align sync matrix previews with shared variant selection

Classify the full source selection once per target so matrix previews honor
canonical aliases, account identity, exact-agent routing, and ambiguous
variant skips. Preserve each rejected physical path's existing diagnostic
and draft filter disambiguation; skipped preview rows remain read-only.

Refs #648

* test: expect singular skill wording in doctor source checks

Keep the undiscovered-link count assertion aligned with logical source
inventory formatting for one skill in global and project mode.

Refs #648

* fix: retain selection diagnostics in catalog and diff verdicts

Report warning-only diff targets as requiring attention while preserving
local-only folder semantics. Reuse shared per-path selection diagnostics
in target-scoped catalogs without applying target naming to served paths.

Compute source counts once in summary callers and require variant counts
in the source status formatter.

Refs #648

* test: tighten tracked variant preview fixtures

Use a real committed tracked repository for status counts, verify sync
preview banners retain server warnings, and use emitted ambiguity reason
text in target preview fixtures.

Refs #648

* fix(sync): keep agent variants in shared target folders

Compatible targets sharing a skills folder need a combined selection so
Codex and universal do not prune one another in merge or copy mode.
Carry every shared reader through selection, pruning and previews,
including project targets, account identities and path aliases.

Require a matching skill suffix under two distinct agents before
inferring variants, keeping ordinary agent-named categories unrestricted.
Separate naming warnings from ambiguous selection in diff and explain
ambiguous removals. Add global/project regression coverage for repeated
sync, target-only sync, status JSON, diff and API consumers.

* fix(variants): align consumer counts, hints and diagnostics

Use logical source counts in list, tracked install and init summaries while matching audit caches against physical variants. Restore localized dashboard skip hints and explain symlink routing limits in status and doctor, including project and JSON output.

Document paired-agent detection and compatible shared-folder selection in the proposal, built-in skill and English plus translated command pages. Add focused regressions for the affected consumers.

* fix(sync): cover initial sync and ignored variant diagnostics

Init syncs a fresh in-memory config, so resolve shared target identities
before its first sync as well as when loading saved configuration.

A whole-directory symlink exposes ignored skills too. Diagnose agent
variants using physical inventory when active discovery has none, while
keeping logical counts and merge/copy selection restricted to enabled
skills. Cover global/project JSON status and doctor with regressions.

Reuse sourcewalk directory and canonical-path helpers so traversal guards
remain enforced and path resolution has a single existing owner.

* fix(variants): address conversion, counts and selection diagnostics

Ignore runtime agent identities when comparing convertible project targets and count logical skills during init. Keep selection warnings separate from naming warnings and preserve agent-named parent skills as ordinary skills.

Cover account sync/prune, project ambiguity and overrides in merge/copy modes, and init/status agreement. Focused verification ran in an isolated Linux Codex container, not the devcontainer, as authorized.

* refactor(variants): remove obsolete selection helpers

Remove the unused FilterSkillsByTarget wrapper and prune agent parameter, cache canonical target aliases once, and restore catalog skip ordering to avoid an unrelated behavior change. Completes the optional cleanup from PR 649 review.

* fix(ui): expose localized variant selection diagnostics

Show target warnings with source paths, localize ambiguity across preview and detail consumers, and let skipped preview rows add a physical exclude pattern. Show tracked repository variant counts alongside logical skills.

Drop display-only rendered cases while retaining mutation protection and plain TypeScript logic coverage. Inspect target states in Clean/Playful light/dark and Traditional Chinese.

* docs(variants): explain diagnostics and add sync runbook

Document doctor variant_routing and logical/physical counts, diff selection skips, and the nested parent-skill boundary. Keep all four command translations aligned and add isolated account/project E2E scenarios.

Both runbook steps passed in the authorized Linux Codex container with separate HOME/XDG/TMPDIR fixtures. Website typecheck and all five locale builds passed.

* test(config): use native paths in project conversion fixture

The shared-agent regression fixture appended slash-separated suffixes to Windows temporary paths, so project path matching returned no candidates. Build each path with filepath.Join and preserve all conversion assertions.

Focused project configuration tests passed in the authorized isolated Linux Codex container, not the devcontainer. Windows CI verifies the platform-specific fixture correction.

* fix(ui): recompute shared skill routing for filter previews

Evaluate edited include/exclude settings in the complete target map before
classifying the draft, so a formerly shared folder no longer keeps its old
union when the filters differ. Preserve the saved target configuration.

* fix(mcp): keep parent skills when peer variants route elsewhere

Do not treat an otherwise eligible variant for another agent as an unserved
nested skill. Continue blocking parent publication for explicitly filtered,
disabled, ambiguous, or invalid children so filters still hide their files.

* fix(ui): distinguish agent selection from naming skips

Expose separate naming and selection skip counts on target summaries.
Show each applicable remedy in target details and sync previews so ambiguity
keeps its filter/override guidance alongside naming and collision hints.

* fix(list): include disabled rows in tracked repository counts

Count the complete inventory that list displays, including ignored skills
and physical variants, without changing the enabled counts used by status,
install, or sync. Clone rows before counting to preserve disabled state.

* docs: align variant counts and routing recovery guidance

Show logical skill and physical variant counts in list/install documentation
and keep the four translations aligned with the filtering examples. Explain
that agent-named skill parents keep ordinary nested skills and retain the
devcontainer-only runbook environment. Removal warnings now recommend a
source-local override, since include filters cannot undo agent routing.

* fix(mcp): preserve explicit target restrictions on nested variants

Limit the differently routed child exemption to automatic layout inference.
Frontmatter targets and dashboard source-local overrides must still prevent
a parent from publishing files explicitly restricted to another target.

* fix(ui): fill target name in naming warning guidance

Pass the current target name when rendering localized warning hints so the
same naming guidance shown beside the controls does not expose a placeholder
in the warning banner. Verified in the populated target preview.

---------

Co-authored-by: Willie <egg358358@gmail.com>
2026-10-29 16:47:38 +08:00

251 lines
10 KiB
Go

package server
import (
"maps"
"net/http"
"os"
"path/filepath"
"skillshare/internal/config"
ssync "skillshare/internal/sync"
"skillshare/internal/utils"
)
// handleDiffStream serves an SSE endpoint that streams diff computation progress.
// Events:
// - "discovering" → {"phase":"..."} immediately on connect
// - "start" → {"total": N} after discovery (N = target count)
// - "result" → diffTarget per-target diff result
// - "done" → {"diffs":[...]} final payload (same shape as GET /api/diff)
func (s *Server) handleDiffStream(w http.ResponseWriter, r *http.Request) {
safeSend, ok := initSSE(w)
if !ok {
return
}
ctx := r.Context()
// Snapshot config under RLock, then release before slow I/O.
s.mu.RLock()
source := s.cfg.EffectiveSkillsSource()
agentsSource := s.agentsSource()
globalMode := s.cfg.Mode
ignorePatterns := ssync.EffectiveFileIgnorePatterns(s.cfg.Ignore)
targets := s.cloneTargets()
s.mu.RUnlock()
if globalMode == "" {
globalMode = "merge"
}
safeSend("discovering", map[string]string{"phase": "scanning source directory"})
discovered, ignoreStats, err := ssync.DiscoverSourceSkillsWithStats(source, s.skillsWalk())
if err != nil {
safeSend("error", map[string]string{"error": err.Error()})
return
}
safeSend("start", map[string]int{"total": len(targets)})
var diffs []diffTarget
checked := 0
for name, target := range targets {
select {
case <-ctx.Done():
return
default:
}
dt := s.computeTargetDiff(name, target, discovered, globalMode, source, ignorePatterns)
diffs = append(diffs, dt)
checked++
safeSend("result", map[string]any{
"diff": dt,
"checked": checked,
})
}
diffs = s.appendAgentDiffs(diffs, targets, agentsSource, "")
donePayload := map[string]any{"diffs": diffs}
maps.Copy(donePayload, ignorePayload(ignoreStats))
maps.Copy(donePayload, agentIgnorePayload(agentsSource, nil))
safeSend("done", donePayload)
}
// computeTargetDiff computes the diff for a single target.
// Extracted from handleDiff to share logic with the stream handler.
func (s *Server) computeTargetDiff(name string, target config.TargetConfig, discovered []ssync.DiscoveredSkill, globalMode, source string, ignorePatterns []string) diffTarget {
sc := target.SkillsConfig()
mode := sc.Mode
if mode == "" {
mode = globalMode
}
dt := diffTarget{Target: name, Items: make([]diffItem, 0)}
if !sc.IsEnabled() {
return dt // skills off: nothing is synced, so nothing differs
}
if mode == "symlink" {
status := ssync.CheckStatus(sc.Path, source)
if status != ssync.StatusLinked {
dt.Items = append(dt.Items, diffItem{Skill: "(entire directory)", Action: "link", Reason: "source only", Kind: kindSkill})
}
return dt
}
resolution, err := ssync.ResolveTargetSkillsForTarget(name, sc, discovered)
if err != nil {
dt.Items = append(dt.Items, diffItem{Skill: "(target naming)", Action: "skip", Reason: err.Error(), Kind: kindSkill})
return dt
}
// Surface collision/validation stats so the UI can show why skills were skipped
dt.CollisionCount = len(resolution.Collisions)
dt.SkippedCount = resolution.SkippedCount
dt.NamingSkippedCount = resolution.NamingSkippedCount
dt.Warnings = resolution.Warnings
for _, warning := range resolution.SelectionWarnings {
dt.Items = append(dt.Items, diffItem{Skill: "(skill selection)", Action: "skip", Reason: warning, Kind: kindSkill})
}
validNames := resolution.ValidTargetNames()
manifest, _ := ssync.ReadManifest(sc.Path)
legacyNames := resolution.LegacyNames(mode, sc.Path, manifest)
renamedFrom := ssync.RenamedFrom(legacyNames)
if mode == "copy" {
for _, resolved := range resolution.Skills {
skill := resolved.Skill
oldChecksum, isManaged := manifest.Managed[resolved.TargetName]
targetSkillPath := filepath.Join(sc.Path, resolved.TargetName)
if !isManaged {
if info, statErr := os.Stat(targetSkillPath); statErr == nil {
if old, ok := renamedFrom[resolved.TargetName]; ok {
dt.Items = append(dt.Items, diffItem{Skill: resolved.TargetName, Action: "kept", Reason: ssync.KeptLegacyReason(old), Kind: kindSkill})
} else if info.IsDir() {
dt.Items = append(dt.Items, diffItem{Skill: resolved.TargetName, Action: "skip", Reason: "local copy (sync --force to replace)", Kind: kindSkill})
} else {
dt.Items = append(dt.Items, diffItem{Skill: resolved.TargetName, Action: "update", Reason: "target entry is not a directory", Kind: kindSkill})
}
} else if old, ok := renamedFrom[resolved.TargetName]; ok && os.IsNotExist(statErr) {
dt.Items = append(dt.Items, diffItem{Skill: resolved.TargetName, Action: "update", Reason: ssync.RenameReason(old), Kind: kindSkill})
} else if os.IsNotExist(statErr) {
dt.Items = append(dt.Items, diffItem{Skill: resolved.TargetName, Action: "link", Reason: "source only", Kind: kindSkill})
} else {
dt.Items = append(dt.Items, diffItem{Skill: resolved.TargetName, Action: "update", Reason: "cannot access target entry", Kind: kindSkill})
}
} else {
targetInfo, statErr := os.Stat(targetSkillPath)
if os.IsNotExist(statErr) {
dt.Items = append(dt.Items, diffItem{Skill: resolved.TargetName, Action: "link", Reason: "missing (deleted from target)", Kind: kindSkill})
} else if statErr != nil {
dt.Items = append(dt.Items, diffItem{Skill: resolved.TargetName, Action: "update", Reason: "cannot access target entry", Kind: kindSkill})
} else if !targetInfo.IsDir() {
dt.Items = append(dt.Items, diffItem{Skill: resolved.TargetName, Action: "update", Reason: "target entry is not a directory", Kind: kindSkill})
} else if recorded := manifest.Naming[resolved.TargetName]; recorded != "" && recorded != resolution.Naming {
// A copy made under another naming carries another name:, so sync re-copies it.
dt.Items = append(dt.Items, diffItem{Skill: resolved.TargetName, Action: "update", Reason: ssync.NamingChangedReason, Kind: kindSkill})
} else {
oldMtime := manifest.Mtimes[resolved.TargetName]
currentMtime, mtimeErr := ssync.DirMaxMtimeWithIgnore(skill.SourcePath, ignorePatterns)
if mtimeErr == nil && oldMtime > 0 && currentMtime == oldMtime {
continue
}
srcChecksum, checksumErr := ssync.DirChecksumWithIgnore(skill.SourcePath, ignorePatterns)
if checksumErr != nil {
dt.Items = append(dt.Items, diffItem{Skill: resolved.TargetName, Action: "update", Reason: "cannot compute checksum", Kind: kindSkill})
} else if srcChecksum != oldChecksum {
dt.Items = append(dt.Items, diffItem{Skill: resolved.TargetName, Action: "update", Reason: "content changed", Kind: kindSkill})
}
}
}
}
for managedName := range manifest.Managed {
if _, keepLegacy := legacyNames[managedName]; keepLegacy {
continue
}
if !validNames[managedName] {
dt.Items = append(dt.Items, diffItem{Skill: managedName, Action: "prune", Reason: "orphan copy", Kind: kindSkill})
}
}
return dt
}
// Merge mode
for _, resolved := range resolution.Skills {
skill := resolved.Skill
targetSkillPath := filepath.Join(sc.Path, resolved.TargetName)
_, err := os.Lstat(targetSkillPath)
if err != nil {
if old, ok := renamedFrom[resolved.TargetName]; ok && os.IsNotExist(err) {
dt.Items = append(dt.Items, diffItem{Skill: resolved.TargetName, Action: "update", Reason: ssync.RenameReason(old), Kind: kindSkill})
} else if os.IsNotExist(err) {
dt.Items = append(dt.Items, diffItem{Skill: resolved.TargetName, Action: "link", Reason: "source only", Kind: kindSkill})
}
continue
}
if utils.IsSymlinkOrJunction(targetSkillPath) {
absLink, linkErr := utils.ResolveLinkTarget(targetSkillPath)
if linkErr != nil {
dt.Items = append(dt.Items, diffItem{Skill: resolved.TargetName, Action: "update", Reason: "link target unreadable", Kind: kindSkill})
continue
}
absSource, _ := filepath.Abs(skill.SourcePath)
if !utils.PathsEqual(absLink, absSource) {
dt.Items = append(dt.Items, diffItem{Skill: resolved.TargetName, Action: "update", Reason: "symlink points elsewhere", Kind: kindSkill})
}
} else if old, ok := renamedFrom[resolved.TargetName]; ok {
dt.Items = append(dt.Items, diffItem{Skill: resolved.TargetName, Action: "kept", Reason: ssync.KeptLegacyReason(old), Kind: kindSkill})
} else if manifest.OwnsCopy(resolved.TargetName, targetSkillPath) {
dt.Items = append(dt.Items, diffItem{Skill: resolved.TargetName, Action: "update", Reason: ssync.CopyToLinkReason, Kind: kindSkill})
} else {
dt.Items = append(dt.Items, diffItem{Skill: resolved.TargetName, Action: "skip", Reason: "local copy (sync --force to replace)", Kind: kindSkill})
}
}
// Orphan check
entries, _ := os.ReadDir(sc.Path)
for _, entry := range entries {
eName := entry.Name()
if manifest.SkipsHidden(eName) {
continue
}
if _, keepLegacy := legacyNames[eName]; keepLegacy {
continue
}
entryPath := filepath.Join(sc.Path, eName)
if !validNames[eName] {
info, statErr := os.Lstat(entryPath)
if statErr != nil {
continue
}
if utils.IsSymlinkOrJunction(entryPath) {
absLink, linkErr := utils.ResolveLinkTarget(entryPath)
if linkErr != nil {
continue
}
absSource, _ := filepath.Abs(source)
if utils.PathHasPrefix(absLink, absSource+string(filepath.Separator)) {
dt.Items = append(dt.Items, diffItem{Skill: eName, Action: "prune", Reason: "orphan symlink", Kind: kindSkill})
}
} else if info.IsDir() {
if _, inManifest := manifest.Managed[eName]; inManifest {
dt.Items = append(dt.Items, diffItem{Skill: eName, Action: "prune", Reason: "orphan managed directory (manifest)", Kind: kindSkill})
} else {
if resolution.Naming == "flat" && (utils.HasNestedSeparator(eName) || utils.IsTrackedRepoDir(eName)) {
dt.Items = append(dt.Items, diffItem{Skill: eName, Action: "prune", Reason: "orphan managed directory", Kind: kindSkill})
} else {
dt.Items = append(dt.Items, diffItem{Skill: eName, Action: "local", Reason: "local only", Kind: kindSkill})
}
}
}
}
}
return dt
}