Files
archie/internal/webui/api_setup.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

83 lines
2.6 KiB
Go

package webui
import (
"net/http"
"strings"
)
// SetupStep is one item in the dashboard's getting-started checklist.
type SetupStep struct {
Title string `json:"title"`
Detail string `json:"detail,omitempty"`
Done bool `json:"done"`
}
// handleSetup reports which parts of archied are configured.
//
// The checklist exists so a new operator is told what is missing rather than
// shown a dashboard of zeroes and left to infer it. Steps are phrased as
// actions in plain language -- "Connect a chat channel", not "channels.telegram
// .bot_token unset" -- because someone who already knows the config key does
// not need the checklist.
//
// Every step is derived from live state, so the list disappears on its own
// once setup is complete rather than needing to be dismissed.
func (s *Server) handleSetup(w http.ResponseWriter, r *http.Request) {
steps := []SetupStep{}
if s.Cfg == nil {
// No config wired in: report nothing rather than guessing, so the
// dashboard simply omits the panel.
writeJSON(w, map[string]any{"steps": steps})
return
}
// The operator's name is deployment data, configured once in [chat]. The
// dashboard greets with it rather than hardcoding a name, which would be
// wrong on every deployment but one.
cfg := s.Cfg.Get()
operator := strings.TrimSpace(cfg.Chat.Operator)
steps = append(steps, SetupStep{
Title: "Give Archie an identity",
Detail: "The account it commits and comments as.",
Done: strings.TrimSpace(cfg.BotUser) != "",
})
steps = append(steps, SetupStep{
Title: "Connect a repository",
Detail: "Archie polls these for issues assigned to it.",
Done: len(cfg.Repos) > 0,
})
steps = append(steps, SetupStep{
Title: "Connect a chat channel",
Detail: "Talk to Archie and approve its work from your phone.",
Done: s.hasChannel(),
})
// Only meaningful once there is somewhere to run work.
if count, err := s.Store.StatusCounts(r.Context()); err == nil {
total := 0
for _, n := range count {
total += n
}
steps = append(steps, SetupStep{
Title: "Run your first task",
Detail: "Assign Archie an issue, or ask it for something in chat.",
Done: total > 0,
})
}
writeJSON(w, map[string]any{"steps": steps, "operator": operator})
}
// hasChannel reports whether any conversational front-end is configured.
// Chat is disabled entirely when no channel token is set, so the presence of
// one is the honest signal here.
func (s *Server) hasChannel() bool {
cfg := s.Cfg.Get()
return strings.TrimSpace(cfg.Chat.Telegram.TokenEnv) != "" ||
strings.TrimSpace(cfg.Chat.WebhookAddr) != ""
}