feat(webui): report which dashboard sections a process can serve

The same dashboard is served by the daemon, which holds every runtime handle,
and by the UI process, which holds two remote contracts. A section with
nothing behind it still answers -- an empty list, a "disabled" marker, a 501 --
and the browser cannot tell that from a deployment where nothing has happened
yet. GET /api/capabilities says which sections this process can back, and the
nav drops the rest rather than offering a page that is permanently empty.

It fails open in both directions: an unreported section stays visible, and a
failed read hides nothing, because an entry that silently vanished is worse
than a page that explains itself.

Mappings and bindings move the other way. Both are ratified State Store
contracts carried by the client the UI process already holds, so leaving them
nil degraded two pages that have an owner; compose wires them.

A table test now enumerates every route whose capability is daemon-local and
pins what it answers without one (requirement 4 of archie-core-8cda.5.4):
task logs, the log feed and its stream, version and the update flow, channels
and reload, curators, memory, skills, workflows and work requests. Adding a
route with no owner now fails that test rather than the operator's browser.
This commit is contained in:
sam committed 2026-09-09 23:49:40 +10:00
1 parent 6f34e34adb
commit 54862f9fe6
11 files changed
+371 -20

No files matched your search

+10
View File
@@ -55,6 +55,16 @@ func compose(d deps) *webui.Server {
if snapshots, ok := d.Store.(store.ConfigSnapshotStore); ok {
srv.ConfigSource = webui.RemoteConfigView(snapshots)
}
// Mappings and bindings are ratified State Store contracts, and the same
// client already carries them: withholding them would degrade two pages
// that have an owner, which is a different thing from the intake surfaces
// below.
if mappings, ok := d.Store.(store.MappingStore); ok {
srv.Mappings = mappings
}
if bindings, ok := d.Store.(store.BindingStore); ok {
srv.Bindings = bindings
}
if d.Chat != nil {
srv.Chat = &webui.ChatService{Contract: d.Chat}
}
+6 -2
View File
@@ -91,8 +91,6 @@ func TestComposeUIServerHoldsNoDaemonState(t *testing.T) {
"LogFeed": srv.LogFeed,
"TaskLogs": srv.TaskLogs,
"WorkRequests": srv.WorkRequests,
"Mappings": srv.Mappings,
"Bindings": srv.Bindings,
"Captures": srv.Captures,
"BindingDispatcher": srv.BindingDispatcher,
"CaptureLimiter": srv.CaptureLimiter,
@@ -105,6 +103,12 @@ func TestComposeUIServerHoldsNoDaemonState(t *testing.T) {
if srv.Workflows != nil {
t.Error("Workflows is wired; the definition catalog is a filesystem scan on the daemon host")
}
// Mappings and bindings are the other way round: ratified State Store
// contracts, carried by the client this process already holds, so
// leaving them nil would degrade two pages that have an owner.
if srv.Mappings == nil || srv.Bindings == nil {
t.Error("Mappings/Bindings are unwired; both are State Store contracts the composed client implements")
}
if srv.UpdateReportPath != "" {
t.Error("UpdateReportPath is set; host-local update relay stays with the daemon")
}
@@ -0,0 +1,176 @@
package archieui
import (
"encoding/json"
"io"
"log/slog"
"net/http"
"net/http/httptest"
"path/filepath"
"strconv"
"strings"
"testing"
"google.golang.org/grpc"
"github.com/samcharles93/archie-core/internal/infrastructure/staterpc"
"github.com/samcharles93/archie-core/internal/store"
"github.com/samcharles93/archie-core/internal/webui"
)
// TestRoutesWithoutAContractDegradeExplicitly enumerates every dashboard
// route whose backing capability lives in the daemon and has no contract the
// UI process can reach (archie-core-8cda.5.4 requirement 4).
//
// The enumeration is the point. Each of these is nil in the UI process, and a
// nil that reaches a handler by accident is a panic, a hang, or an empty
// answer indistinguishable from real emptiness. Pinning the response here
// means adding a route without an owner fails this test rather than the
// operator's browser, and gives the SPA a documented status to hide on.
func TestRoutesWithoutAContractDegradeExplicitly(t *testing.T) {
srv, taskID := composeUIProcess(t)
ts := httptest.NewServer(srv.Handler())
defer ts.Close()
for _, tc := range []struct {
name string
method string
path string
body string
want int
// emptyJSON asserts the response carries a well-formed empty
// answer rather than a fabricated one.
emptyJSON string
}{
{
name: "daemon log feed", method: http.MethodGet, path: "/api/logs",
want: http.StatusOK, emptyJSON: `"disabled":true`,
},
{
name: "daemon log stream", method: http.MethodGet, path: "/api/logs/stream",
want: http.StatusServiceUnavailable,
},
{
// The task exists; its logs do not, because the files live on
// the daemon's host. "disabled" is the same answer the daemon
// gives when task logging is off, and the page already reads it.
name: "per-task logs", method: http.MethodGet, path: "/api/tasks/" + strconv.FormatInt(taskID, 10) + "/logs",
want: http.StatusOK, emptyJSON: `"disabled":true`,
},
{
name: "release version and update flow", method: http.MethodGet, path: "/api/version",
want: http.StatusNotImplemented,
},
{
name: "chat update check", method: http.MethodGet, path: "/api/chat/update",
want: http.StatusNotImplemented,
},
{
name: "channel lifecycle", method: http.MethodGet, path: "/api/channels",
want: http.StatusOK, emptyJSON: `[]`,
},
{
name: "channel reload", method: http.MethodPost, path: "/api/channels/telegram/reload",
want: http.StatusNotImplemented,
},
{
name: "curator registry", method: http.MethodGet, path: "/api/curators",
want: http.StatusOK, emptyJSON: `[]`,
},
{
name: "memory", method: http.MethodGet, path: "/api/memory",
want: http.StatusOK,
},
{
name: "skills catalog", method: http.MethodGet, path: "/api/skills",
want: http.StatusOK, emptyJSON: `[]`,
},
{
// Statistics come from the State Store, the definition list from
// the daemon's registry: the remote half answers, the local half
// is absent, and both are absent-shaped rather than missing keys.
name: "workflow registry", method: http.MethodGet, path: "/api/workflows",
want: http.StatusOK, emptyJSON: `"definitions":null`,
},
{
name: "dashboard work request", method: http.MethodPost, path: "/api/work-requests",
body: `{"repository":"acme/widget","workflow":"implement","title":"do it"}`,
want: http.StatusServiceUnavailable,
},
{
// The browser reads this to decide which sections to show at
// all, so it is the one route that must answer here.
name: "capabilities", method: http.MethodGet, path: "/api/capabilities",
want: http.StatusOK, emptyJSON: `"logs":false`,
},
} {
t.Run(tc.name, func(t *testing.T) {
var reader *strings.Reader
if tc.body != "" {
reader = strings.NewReader(tc.body)
} else {
reader = strings.NewReader("")
}
req, err := http.NewRequestWithContext(t.Context(), tc.method, ts.URL+tc.path, reader)
if err != nil {
t.Fatalf("request: %v", err)
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-Archie-CSRF", "1")
resp, err := ts.Client().Do(req)
if err != nil {
t.Fatalf("%s %s: %v", tc.method, tc.path, err)
}
defer resp.Body.Close()
raw, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatalf("read body: %v", err)
}
body := string(raw)
if resp.StatusCode != tc.want {
t.Fatalf("%s %s = %d (%s), want %d", tc.method, tc.path, resp.StatusCode, body, tc.want)
}
if tc.emptyJSON != "" && !strings.Contains(body, tc.emptyJSON) {
t.Fatalf("%s %s body = %s, want it to carry %s", tc.method, tc.path, body, tc.emptyJSON)
}
if resp.StatusCode == http.StatusOK && !json.Valid([]byte(body)) {
t.Fatalf("%s %s returned invalid JSON: %s", tc.method, tc.path, body)
}
})
}
}
// composeUIProcess builds the dashboard exactly as the UI process does, over
// a State Store with nothing published and a Gateway that is not wired at
// all -- the shape every route below has to survive.
func composeUIProcess(t *testing.T) (*webui.Server, int64) {
t.Helper()
st, err := store.Open(t.Context(), filepath.Join(t.TempDir(), "tasks.sqlite"))
if err != nil {
t.Fatalf("open store: %v", err)
}
t.Cleanup(func() { _ = st.Close() })
target, stop := serveGRPC(t, func(r grpc.ServiceRegistrar) {
staterpc.RegisterServer(r, staterpc.Deps{Tasks: st, ConfigSnapshots: st, Log: slog.New(slog.DiscardHandler)})
})
t.Cleanup(stop)
client, closeClient, err := staterpc.Dial(target, "")
if err != nil {
t.Fatalf("dial state store: %v", err)
}
t.Cleanup(closeClient)
task, err := st.EnqueueChatTask(t.Context(), "acme", "widget", "a task", "body", "implement", "")
if err != nil {
t.Fatalf("seed task: %v", err)
}
options := Options{Listen: "127.0.0.1:0", DependencyTimeout: defaultDependencyTimeout}
return compose(deps{
Options: options,
Log: slog.New(slog.DiscardHandler),
Store: client,
Health: newReadinessRegistry(options, client, nil),
}), task.ID
}
+41
View File
@@ -0,0 +1,41 @@
package webui
import "net/http"
// handleCapabilities reports which dashboard sections this process can
// actually serve.
//
// The dashboard is served by two different compositions: the daemon, which
// holds every runtime handle, and the UI process, which holds two remote
// contracts and nothing else. Sections whose capability lives only in the
// daemon still answer -- with an empty list, a "disabled" marker, or a 501 --
// and the page cannot tell that apart from a deployment where nothing has
// happened yet. Rather than have every page guess, the server says which
// sections it can back, and the browser hides the rest.
//
// A section is reported available when the handle behind it is wired, which
// is the same condition its handler already uses to decide between real data
// and its degraded answer. Sections backed by the task store are absent from
// this list: the store is mandatory in both compositions.
func (s *Server) handleCapabilities(w http.ResponseWriter, _ *http.Request) {
writeJSON(w, map[string]any{
"sections": map[string]bool{
"chat": s.Chat != nil && s.Chat.Contract != nil,
"logs": s.LogFeed != nil,
"skills": s.Cfg != nil,
"memory": s.Memory != nil,
"curators": s.Curators != nil,
"channels": s.Channels != nil || s.Cfg != nil,
"captures": s.Captures != nil,
"mappings": s.Mappings != nil,
"bindings": s.Bindings != nil,
// The workflows page draws its statistics from the task store
// and its definition list from the local registry, so it is
// worth showing with only the former.
"workflows": true,
// Configuration renders from whichever source is wired; the
// page reports its own editability (see ConfigView.Editable).
"settings": true,
},
})
}
+53
View File
@@ -11,6 +11,9 @@ import (
"github.com/samcharles93/archie-core/internal/channels"
"github.com/samcharles93/archie-core/internal/config"
"github.com/samcharles93/archie-core/internal/domain/curator"
"github.com/samcharles93/archie-core/internal/logging"
"github.com/samcharles93/archie-core/internal/memory"
"github.com/samcharles93/archie-core/internal/secret"
"github.com/samcharles93/archie-core/internal/store"
)
@@ -685,3 +688,53 @@ func (s stubSnapshots) PutConfigSnapshot(context.Context, store.ConfigSnapshot)
func (s stubSnapshots) ConfigSnapshot(context.Context) (store.ConfigSnapshot, bool, error) {
return s.snapshot, s.found, s.err
}
// TestCapabilitiesReportWhatThisProcessCanServe: the same dashboard is served
// by the daemon, which holds every runtime handle, and by the UI process,
// which holds two remote contracts. A section with nothing behind it answers
// empty rather than failing, which the browser cannot tell from a quiet
// deployment -- so the server says which sections it can back.
func TestCapabilitiesReportWhatThisProcessCanServe(t *testing.T) {
get := func(srv *Server) map[string]bool {
t.Helper()
req := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/api/capabilities", nil)
res := httptest.NewRecorder()
srv.Handler().ServeHTTP(res, req)
if res.Code != http.StatusOK {
t.Fatalf("GET /api/capabilities = %d, want 200", res.Code)
}
var body struct {
Sections map[string]bool `json:"sections"`
}
if err := json.Unmarshal(res.Body.Bytes(), &body); err != nil {
t.Fatalf("decode capabilities: %v", err)
}
return body.Sections
}
bare := get(newTestServer(t))
for _, section := range []string{"logs", "memory", "curators", "channels", "mappings", "bindings", "skills"} {
if bare[section] {
t.Errorf("section %q reported available with nothing wired behind it", section)
}
}
// The task board and configuration page work from the store and the
// configured source, so they are never hidden.
for _, section := range []string{"workflows", "settings"} {
if !bare[section] {
t.Errorf("section %q reported unavailable; it is served in every composition", section)
}
}
wired := newTestServer(t)
wired.Cfg = config.NewHolder(config.Config{})
wired.LogFeed = logging.NewFeed(10)
wired.Memory = &memory.Manager{}
wired.Curators = &curator.Registry{}
got := get(wired)
for _, section := range []string{"logs", "memory", "curators", "skills", "channels"} {
if !got[section] {
t.Errorf("section %q reported unavailable despite being wired", section)
}
}
}
+1
View File
@@ -276,6 +276,7 @@ func (s *Server) registerCoreRoutes(mux *http.ServeMux) {
mux.HandleFunc("POST /api/channels/{id}/reload", s.handleChannelReload)
mux.HandleFunc("GET /api/version", s.handleVersion)
mux.HandleFunc("GET /api/memory", s.handleMemory)
mux.HandleFunc("GET /api/capabilities", s.handleCapabilities)
}
func (s *Server) registerTaskRoutes(mux *http.ServeMux) {