Files
skillshare/internal/server/handler_check.go
T
Willie b80224f501 fix(ui): check agents on the Updates tab (#966)
* fix(ui): check agents on the Updates tab

Pressing Check on the Agents Updates tab marked every agent "Check
failed". The page marks the items of both kinds as checking and turns
the ones without a result into failures, but the check stream only ever
looked at the skills source, so no agent got a result.

The stream now reports agents as well, using the checks the CLI runs for
`skillshare check agents`, and counts their remotes in the progress
total. A check limited by names still covers tracked repos and skills
only.

A locally edited agent can now show "Modified locally". The link that
lists local changes reads skills only, so it is left out for agents.

* fix(ui): apply skill check results to skills only

A mixed repository installed with --track for both kinds gives the skills
source and the agents source a checkout with the same folder name. The
check reports a tracked repo of the skills source under tracked_repos, but
the Updates tab copied that status to every item whose repo folder matched,
whatever its kind. A full check hid this because the agents' own results
are applied afterwards. "Check N selected" returns no agent results, so
checking the skills repo replaced an agents repo's "Update available" or
"Modified locally" with "Up to date".

The per-skill results had the same gap: a skill result could land on an
agent of the same name. Both loops now match skills only, as the update
results already do.
2026-11-20 17:15:55 +08:00

133 lines
4.3 KiB
Go

package server
import (
"context"
"net/http"
"path/filepath"
"strings"
"skillshare/internal/check"
"skillshare/internal/install"
"skillshare/internal/sourcewalk"
)
type repoCheckResult struct {
Name string `json:"name"`
Status string `json:"status"`
Behind int `json:"behind"`
Message string `json:"message,omitempty"`
}
type skillCheckResult struct {
Name string `json:"name"`
Source string `json:"source"`
Version string `json:"version"`
Status string `json:"status"`
InstalledAt string `json:"installed_at,omitempty"`
Kind string `json:"kind,omitempty"`
Message string `json:"message,omitempty"`
}
// checkNames reads the names query param (comma-separated): the tracked repos
// and skills a check is limited to. Without it, nil: everything is checked.
func checkNames(r *http.Request) map[string]bool {
param := r.URL.Query().Get("names")
if param == "" {
return nil
}
only := map[string]bool{}
for name := range strings.SplitSeq(param, ",") {
if name = strings.TrimSpace(name); name != "" {
only[name] = true
}
}
return only
}
// planSkillCheck plans the update check of every installed skill outside a
// followed checkout, or of those in only when it is not nil. It takes
// s.mu.RLock, so the caller must not hold s.mu.
func (s *Server) planSkillCheck(sourceDir, projectRoot string, walk sourcewalk.Options, only map[string]bool) *check.Resolution {
names, _ := install.GetUpdatableSkills(sourceDir)
var skills []check.Skill
for _, name := range names {
if only != nil && !only[name] {
continue
}
if followedCheckout(filepath.Join(sourceDir, name), walk.Follow) != "" {
continue
}
skills = append(skills, check.Skill{Name: name, Entry: s.skillEntry(name), Dir: filepath.Join(sourceDir, name), Follow: walk.Follow})
}
return check.Plan(skills, projectRoot)
}
// dashboardSkillResults converts resolved statuses to the dashboard payload,
// which is narrower than the CLI's: a skill without a remote carries only its
// name, status and message.
func dashboardSkillResults(results []check.SkillResult) []skillCheckResult {
out := make([]skillCheckResult, 0, len(results))
for _, r := range results {
if r.Local {
out = append(out, skillCheckResult{Name: r.Name, Status: r.Status, Message: r.Message})
continue
}
out = append(out, skillCheckResult{
Name: r.Name,
Source: r.Source,
Version: r.Version,
Status: r.Status,
InstalledAt: r.InstalledAt,
Message: r.Message,
})
}
return out
}
// dashboardAgentResults converts agent statuses to the dashboard payload.
func dashboardAgentResults(results []check.AgentCheckResult) []skillCheckResult {
out := make([]skillCheckResult, 0, len(results))
for _, r := range results {
out = append(out, skillCheckResult{Name: r.Name, Source: r.Source, Version: r.Version, Status: r.Status, Message: r.Message})
}
return out
}
// checkTrackedRepo checks one tracked repo with the same logic as the CLI:
// auth-aware fetch and a reported dirty-check error.
func checkTrackedRepo(name, repoPath string, parseOpts install.ParseOptions) repoCheckResult {
out := check.ParallelCheckRepos([]check.RepoCheckInput{{Name: name, RepoPath: repoPath, ParseOptions: parseOpts}}, nil)[0]
return repoCheckResult{Name: out.Name, Status: out.Status, Behind: out.Behind, Message: out.Message}
}
func (s *Server) handleCheck(w http.ResponseWriter, r *http.Request) {
// Snapshot config under RLock, then release before I/O.
s.mu.RLock()
sourceDir := s.skillsSource()
projectRoot := s.projectRoot
walk := s.skillsWalk()
parseOpts := s.parseOpts()
s.mu.RUnlock()
repos, linked := dashboardRepos(sourceDir, walk)
plan := s.planSkillCheck(sourceDir, projectRoot, walk, nil)
var repoResults []repoCheckResult
for _, repo := range repos {
repoResults = append(repoResults, checkTrackedRepo(repo, filepath.Join(sourceDir, repo), parseOpts))
}
// Without a context the check runs to completion, so the error is always nil.
resolved, _ := plan.Run(context.Background(), check.Options{ParseOptions: parseOpts})
skillResults := dashboardSkillResults(resolved)
if repoResults == nil {
repoResults = []repoCheckResult{}
}
writeJSON(w, map[string]any{
"tracked_repos": repoResults,
"linked_repos": linked,
"skills": skillResults,
})
}