Files
archie/internal/webui/api_skills.go
T
sam b6f0aea930 refactor(daemon,webui): make Daemon.Cfg and webui.Server.Cfg reload-safe
Daemon.Cfg, webui.Server.Cfg, and telegramSetup.Cfg were all held by
value (or as a raw shared pointer in webui's case), so a future reload
path could not swap the running configuration -- the consumers held
frozen snapshots made at construction. This is the prerequisite for
issue #348.

Add config.Holder, a small RWMutex-backed snapshot holder:
  - Get returns a value copy under RLock; the copy shares its
    reference-type fields with the published snapshot, so a doc
    comment pins the immutability invariant (a published Config is
    immutable; reload constructs a fresh one).
  - Set replaces the snapshot under WLock.
  - Get is nil-safe; Set is not.

Convert every consumer to read through the accessor. The convention
'cfg := h.Get() once per logical operation' is documented on the
type so the next reader knows that inline d.Cfg.Get().Foo is a code
smell (a Set could land between two consecutive Gets).

Special cases:
  - webui.Server.Cfg: was *config.Config, a raw shared pointer read
    from handler goroutines. Convert to *config.Holder. The
    pre-existing 'cfg := s.Cfg' aliasing bug at api_config.go:295
    (which was *trying* to copy but only copied the pointer) now
    becomes 'cfg := s.Cfg.Get()', which is the value copy the
    read-only view always wanted.
  - daemon_test.go: one test mutated d.Cfg.DiffCapLines in place,
    which the Holder forbids. Replace with the documented
    read-modify-write pattern (prev := d.Cfg.Get(); prev.X = Y;
    d.Cfg.Set(prev)).
  - telegramSetup.Cfg: matches the daemon's API for consistency
    even though setup runs before the daemon is alive.

TaskContext.Cfg in internal/domain/workflow/workflow.go is
deliberately NOT converted -- a task that starts under one config
should finish under it, and a config changing between TDD and gate
stages is a bug, not a feature. Snapshotted at dispatch, as before.

Verified: go test ./... (61 packages), go vet, -race on
internal/daemon and internal/config.
2026-08-09 23:29:52 +10:00

70 lines
2.2 KiB
Go

package webui
import (
"net/http"
"github.com/samcharles93/archie-core/internal/skill"
)
// SkillView is one catalogued skill as shown on the dashboard's Skills page:
// what it does, in plain language, and where it was discovered.
type SkillView struct {
Name string `json:"name"`
Description string `json:"description"`
Workflow string `json:"workflow,omitempty"`
Source string `json:"source"`
}
// handleSkills reports the skill catalogue -- what Archie can actually do,
// beyond what is in the source. It answers the same question a fresh
// operator would ask by reading internal/skill/, without the reading.
//
// The catalogue layers project, explicitly shared, and user-global skills.
// Earlier roots win when names collide. Missing roots are legitimate and
// produce an empty or partial catalogue rather than a failed dashboard.
func (s *Server) handleSkills(w http.ResponseWriter, r *http.Request) {
if s.Cfg == nil {
writeJSON(w, map[string]any{"skills": []SkillView{}})
return
}
cfg := s.Cfg.Get()
roots := skill.DefaultRoots(cfg.WorkDir, cfg.SkillsDir)
catalog, err := skill.CatalogRoots(roots...)
if err != nil {
http.Error(w, "failed to read skill catalogue: "+err.Error(), http.StatusInternalServerError)
return
}
views := make([]SkillView, 0, len(catalog))
for _, entry := range catalog {
views = append(views, SkillView{
Name: entry.Name,
Description: entry.Description,
Workflow: entry.Workflow,
Source: skillSource(entry.Root, cfg.WorkDir, cfg.SkillsDir),
})
}
// Each skill already carries its own Source, which is what the page
// renders. A top-level dir/dirs pair had no consumer and implied the
// catalog came from one place when it is merged from several.
writeJSON(w, map[string]any{"skills": views})
}
// skillSource labels where a skill was loaded from.
//
// sharedDir is checked first: when it equals workDir the roots collapse to one
// entry, and matching workDir first labelled every shared skill as a project
// skill.
func skillSource(root, workDir, sharedDir string) string {
switch root {
case sharedDir:
return "shared skills"
case workDir:
return "project skills"
default:
return "user-global skills"
}
}