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

135 lines
3.5 KiB
Go

package webui
import (
"encoding/json"
"net/http"
"slices"
"strconv"
"strings"
"github.com/samcharles93/archie-core/internal/logging"
)
// handleLogs serves a page of log history.
//
// This handler is transport only: parsing the log format belongs to the
// logging package, which owns it. Live output reaches the dashboard over the
// existing /events stream; this endpoint exists so the view has history from
// before the browser connected, which is the whole reason file logging landed
// first.
func (s *Server) handleLogs(w http.ResponseWriter, r *http.Request) {
path := s.logFile()
if path == "" {
// Not an error: file logging is optional. Say so plainly so the UI can
// explain how to turn it on rather than showing an empty table.
writeJSON(w, map[string]any{
"entries": s.LogFeed.Snapshot(),
"file": "",
"disabled": true,
"components": mergeComponents(nil, s.LogFeed.Snapshot()),
})
return
}
q := r.URL.Query()
limit, _ := strconv.Atoi(q.Get("limit"))
result, err := logging.Tail(path, logging.Query{
Levels: splitCSV(q.Get("level")),
Component: strings.TrimSpace(q.Get("component")),
Contains: strings.TrimSpace(q.Get("q")),
Limit: limit,
})
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
components, err := logging.Components(path)
if err != nil {
// The filter list is a convenience; failing to build it must not cost
// the caller their log entries.
s.Log.Warn("log components unavailable", "err", err)
components = []string{}
}
liveEntries := s.LogFeed.Snapshot()
components = mergeComponents(components, liveEntries)
writeJSON(w, map[string]any{
"entries": append(result.Entries, liveEntries...),
"truncated": result.Truncated,
"file": result.File,
"components": components,
})
}
func (s *Server) handleLogStream(w http.ResponseWriter, r *http.Request) {
if s.LogFeed == nil {
http.Error(w, "live daemon log feed unavailable", http.StatusServiceUnavailable)
return
}
flusher, ok := w.(http.Flusher)
if !ok {
http.Error(w, "streaming unsupported", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "text/event-stream")
w.Header().Set("Cache-Control", "no-cache")
entries := s.LogFeed.Subscribe(r.Context())
for {
select {
case <-r.Context().Done():
return
case entry, ok := <-entries:
if !ok {
return
}
data, err := json.Marshal(entry)
if err != nil {
continue
}
_, _ = w.Write([]byte("id: " + strconv.FormatInt(entry.ID, 10) + "\ndata: " + string(data) + "\n\n"))
flusher.Flush()
}
}
}
func mergeComponents(history []string, live []logging.Entry) []string {
seen := make(map[string]struct{}, len(history))
for _, component := range history {
seen[component] = struct{}{}
}
for _, entry := range live {
if component, ok := entry.Fields["component"].(string); ok && component != "" {
seen[component] = struct{}{}
}
}
components := make([]string, 0, len(seen))
for component := range seen {
components = append(components, component)
}
slices.Sort(components)
return components
}
// logFile reports the configured log path, or "" when file logging is off.
func (s *Server) logFile() string {
if s.Cfg == nil {
return ""
}
return strings.TrimSpace(s.Cfg.Get().Log.File)
}
func splitCSV(v string) []string {
if strings.TrimSpace(v) == "" {
return nil
}
parts := strings.Split(v, ",")
out := make([]string, 0, len(parts))
for _, p := range parts {
if p = strings.TrimSpace(p); p != "" {
out = append(out, p)
}
}
return out
}