fix(server): restore skills through followed source links

Pass the current global or project follow policy to skill trash restore so the dashboard matches CLI behavior and can restore into followed link targets. Keep disabled-link refusals and agent restores unchanged, and cover both scopes and link naming styles with handler regression tests.
This commit is contained in:
Willie
2026-10-05 18:27:12 +08:00
parent 93708478a6
commit 6e641dca14
3 changed files with 91 additions and 1 deletions
+1 -1
View File
@@ -163,7 +163,7 @@ func (s *Server) handleRestoreTrash(w http.ResponseWriter, r *http.Request) {
case trashKindAgent:
err = trash.RestoreAgent(resolved.entry, resolved.dest)
default:
err = trash.Restore(resolved.entry, resolved.dest)
err = trash.Restore(resolved.entry, resolved.dest, s.skillsWalk().Follow)
}
if err != nil {
writeError(w, http.StatusInternalServerError, "failed to restore: "+err.Error())
@@ -0,0 +1,88 @@
package server
import (
"encoding/json"
"net/http"
"net/http/httptest"
"net/url"
"os"
"path/filepath"
"testing"
"skillshare/internal/config"
"skillshare/internal/trash"
)
func TestHandleRestoreTrash_SourceLinkFollowPolicy(t *testing.T) {
for _, project := range []bool{false, true} {
for _, enabled := range []bool{false, true} {
for _, linkName := range []string{"_dev-skills", "dev-skills"} {
name := map[bool]string{false: "global", true: "project"}[project] + "/" + map[bool]string{false: "disabled", true: "enabled"}[enabled] + "/" + linkName
t.Run(name, func(t *testing.T) {
s, src := newTestServer(t)
if project {
s.cfg.FollowSourceLinks = !enabled
src = t.TempDir()
s = NewProject(s.cfg, &config.ProjectConfig{
Sources: config.ProjectSources{Skills: src}, FollowSourceLinks: enabled,
}, t.TempDir(), "127.0.0.1:0", "", "")
} else {
s.cfg.FollowSourceLinks = enabled
}
if err := s.saveConfig(); err != nil {
t.Fatal(err)
}
target := t.TempDir()
addSkill(t, target, "foo")
addSkill(t, target, "bar")
link := filepath.Join(src, linkName)
if err := os.Symlink(target, link); err != nil {
t.Fatal(err)
}
logicalName := linkName + "/foo"
if _, err := trash.MoveToTrash(filepath.Join(target, "foo"), logicalName, s.trashBase()); err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
s.handler.ServeHTTP(rr, httptest.NewRequest(http.MethodPost, "/api/trash/"+url.PathEscape(logicalName)+"/restore", nil))
if enabled {
if rr.Code != http.StatusOK {
t.Fatalf("restore failed: %d: %s", rr.Code, rr.Body.String())
}
if _, err := os.Stat(filepath.Join(target, "foo", "SKILL.md")); err != nil {
t.Fatalf("skill not restored to link target: %v", err)
}
if entry := trash.FindByName(s.trashBase(), logicalName); entry != nil {
t.Fatalf("restored skill still in trash: %+v", entry)
}
} else {
var resp struct{ Error string }
if err := json.Unmarshal(rr.Body.Bytes(), &resp); err != nil {
t.Fatal(err)
}
want := "failed to restore: restore path unsafe: " + link + " is a link; edit its target directly"
if rr.Code != http.StatusInternalServerError || resp.Error != want {
t.Fatalf("expected link refusal %q, got %d: %s", want, rr.Code, rr.Body.String())
}
if _, err := os.Stat(filepath.Join(target, "foo")); !os.IsNotExist(err) {
t.Fatalf("refused restore modified target: %v", err)
}
entry := trash.FindByName(s.trashBase(), logicalName)
if entry == nil {
t.Fatal("refused restore lost trash entry")
}
if _, err := os.Stat(filepath.Join(entry.Path, "SKILL.md")); err != nil {
t.Fatalf("refused restore lost trashed skill: %v", err)
}
}
if got, err := os.Readlink(link); err != nil || got != target {
t.Fatalf("source link changed: %q, %v", got, err)
}
if _, err := os.Stat(filepath.Join(target, "bar", "SKILL.md")); err != nil {
t.Fatalf("sibling skill changed: %v", err)
}
})
}
}
}
}
+2
View File
@@ -56,6 +56,8 @@ Both call `internal/sourcelink`, share the uninstall routes' middleware, and ret
`DELETE /api/resources/{name}` and `POST /api/uninstall/batch` (`{names: [flatName], kind?: "skill", force?: bool}`) resolve individual skills beneath followed first-level links with the same follow policy as discovery, before applying tracked-repo restrictions. They move only the selected skill through `sourcefs.CheckMoveOut`, preserving its logical path (for example, `_dev-skills/foo`) in trash for policy-aware restore. The link and sibling skills stay in place. Real tracked-repo members remain protected, and links skipped by the policy are not treated as followed. Single uninstall returns `{success, name, movedToTrash}`; batch returns per-name `results` and a `summary` with `succeeded` and `failed` counts.
`POST /api/trash/{name}/restore` restores skills using the current global/project follow policy. A logical name such as `_dev-skills/foo` restores into the link target when the policy follows that link; with `follow_source_links` off, it returns HTTP 500 with the existing `is a link; edit its target directly` refusal and preserves the trash entry. Link names do not determine permission to follow. Agent restore is unchanged.
## Report
Use a concise table for each dimension: