feat(webui): show which process is running which settings version

The settings page now reads apply status beside each resource: every known
process with the version it applied, its error if it rejected the edit, and
whether it is still reporting at all. It re-reads after each save, so the page
reflects what the processes did with an edit rather than what was stored.

The server derives liveness only, because it knows whether a record went stale
but not which version the page is showing. The client compares against the
resource it is rendering, which is what turns a live record on an older
version into a pending restart.

A process that stopped re-stamping and one that never reported are separate
rows with separate wording, and neither is an error: the setting may be fine
and the process simply absent, which an operator acts on differently. A failed
apply-status read leaves the previous rows rather than taking the settings
page down with it.

state-store-contract.md gains the pair in its inventory and its count moves
from 45 to 47, with the note that this read is a list rather than the
config-snapshot pair's found=false singleton.

Closes archie-core-pskb.
This commit is contained in:
sam committed 2026-09-21 15:57:46 +10:00
1 parent 48367151f3
commit dcdaa9f451
10 files changed
+358 -5

No files matched your search

+3
View File
@@ -79,6 +79,9 @@ func compose(d deps) *webui.Server {
// 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 applyStatus, ok := d.Store.(storecontract.ApplyStatusStore); ok {
srv.ApplyStatus = applyStatus
}
if mappings, ok := d.Store.(storecontract.MappingStore); ok {
srv.Mappings = mappings
}
+80
View File
@@ -0,0 +1,80 @@
package webui
import (
"net/http"
"time"
"github.com/samcharles93/archie-core/internal/domain/applystatus"
"github.com/samcharles93/archie-core/internal/domain/storecontract"
)
// The readings an operator acts on. A process is current when it is running
// what it last reported, failed when it rejected what the store holds, and
// unknown when it has stopped re-stamping: a record that outlived its process
// must never read as current (docs/prds/control-plane-apply-status.md).
const (
applyStateCurrent = "current"
applyStateFailed = "failed"
applyStateUnknown = "unknown"
)
type applyStatusRecord struct {
Process string `json:"process"`
Kind string `json:"kind"`
AppliedVersion int64 `json:"applied_version"`
Error string `json:"error,omitempty"`
ReportedAt time.Time `json:"reported_at"`
State string `json:"state"`
}
type applyStatusView struct {
Records []applyStatusRecord `json:"records"`
// Processes is every name a record may carry, so the page can show a
// process that has never reported at all rather than omitting the column.
Processes []string `json:"processes"`
}
// handleApplyStatus reports which version of each control-plane resource each
// process is running. A process with no apply-status wiring renders an empty
// page rather than failing: that is a deployment fact, not an error.
func (s *Server) handleApplyStatus(w http.ResponseWriter, r *http.Request) {
view := applyStatusView{Records: []applyStatusRecord{}, Processes: applystatus.Processes()}
if s.ApplyStatus == nil {
writeJSON(w, view)
return
}
records, err := s.ApplyStatus.ListApplyStatus(r.Context())
if err != nil {
http.Error(w, "apply status unavailable", http.StatusServiceUnavailable)
return
}
now := s.clock()
for _, record := range records {
view.Records = append(view.Records, applyStatusRecord{
Process: record.Process, Kind: record.Kind, AppliedVersion: record.AppliedVersion,
Error: record.Error, ReportedAt: record.ReportedAt, State: applyState(record, now),
})
}
writeJSON(w, view)
}
// applyState reads staleness before the error: a process that stopped
// reporting is unknown whatever its last report said, because that report
// describes a process that may no longer exist.
func applyState(record storecontract.ApplyStatus, now time.Time) string {
switch {
case applystatus.Stale(record.ReportedAt, now):
return applyStateUnknown
case record.Error != "":
return applyStateFailed
default:
return applyStateCurrent
}
}
func (s *Server) clock() time.Time {
if s.now != nil {
return s.now()
}
return time.Now().UTC()
}
+102
View File
@@ -0,0 +1,102 @@
package webui
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/samcharles93/archie-core/internal/domain/applystatus"
"github.com/samcharles93/archie-core/internal/domain/storecontract"
)
type stubApplyStatus struct {
records []storecontract.ApplyStatus
err error
}
func (s stubApplyStatus) PutApplyStatus(_ context.Context, _ storecontract.ApplyStatus) error {
return nil
}
func (s stubApplyStatus) ListApplyStatus(context.Context) ([]storecontract.ApplyStatus, error) {
return s.records, s.err
}
func applyStatusResponse(t *testing.T, server *Server) applyStatusView {
t.Helper()
recorder := httptest.NewRecorder()
server.Handler().ServeHTTP(recorder, httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/api/control-plane/apply-status", nil))
if recorder.Code != http.StatusOK {
t.Fatalf("GET apply-status = %d, want 200: %s", recorder.Code, recorder.Body.String())
}
var view applyStatusView
if err := json.Unmarshal(recorder.Body.Bytes(), &view); err != nil {
t.Fatalf("decode apply status: %v (body %s)", err, recorder.Body.String())
}
return view
}
// TestApplyStatusDerivesState pins the three readings an operator acts on: a
// process on the current version is live, one on an older version of a
// restart-required kind is pending a restart, and one that has stopped
// re-stamping is unknown rather than current (archie-core-pskb).
func TestApplyStatusDerivesState(t *testing.T) {
now := time.Date(2026, 9, 21, 12, 0, 0, 0, time.UTC)
server := &Server{ApplyStatus: stubApplyStatus{records: []storecontract.ApplyStatus{
{Process: applystatus.Daemon, Kind: "tool-settings", AppliedVersion: 4, ReportedAt: now.Add(-10 * time.Second)},
{Process: applystatus.Gateway, Kind: "tool-settings", AppliedVersion: 3, ReportedAt: now.Add(-10 * time.Second)},
{Process: applystatus.Messaging, Kind: "tool-settings", AppliedVersion: 4, ReportedAt: now.Add(-10 * time.Minute)},
}}}
server.now = func() time.Time { return now }
got := applyStatusResponse(t, server)
states := map[string]string{}
for _, record := range got.Records {
states[record.Process] = record.State
}
for process, want := range map[string]string{
applystatus.Daemon: applyStateCurrent,
applystatus.Gateway: applyStateCurrent,
applystatus.Messaging: applyStateUnknown,
} {
if states[process] != want {
t.Errorf("%s state = %q, want %q", process, states[process], want)
}
}
}
// TestApplyStatusReportsAnError pins that a process that rejected an edit
// reads as failed with its error, and keeps showing the version still live
// in it.
func TestApplyStatusReportsAnError(t *testing.T) {
now := time.Date(2026, 9, 21, 12, 0, 0, 0, time.UTC)
server := &Server{ApplyStatus: stubApplyStatus{records: []storecontract.ApplyStatus{
{
Process: applystatus.Daemon, Kind: "tool-settings", AppliedVersion: 3,
Error: "validate database settings: bad policy", ReportedAt: now,
},
}}}
server.now = func() time.Time { return now }
got := applyStatusResponse(t, server)
if len(got.Records) != 1 {
t.Fatalf("got %d records, want 1", len(got.Records))
}
record := got.Records[0]
if record.State != applyStateFailed || record.Error == "" || record.AppliedVersion != 3 {
t.Fatalf("record = %+v, want failed with its error and version 3 still live", record)
}
}
// TestApplyStatusWithoutAStoreIsEmptyNotAnError: a process with no apply
// status wiring must render an empty page, the same way the capture list
// reports disabled rather than failing the dashboard.
func TestApplyStatusWithoutAStoreIsEmptyNotAnError(t *testing.T) {
got := applyStatusResponse(t, &Server{})
if len(got.Records) != 0 {
t.Fatalf("got %d records from a server with no store, want none", len(got.Records))
}
}
+10
View File
@@ -14,6 +14,7 @@ import (
"log/slog"
"net/http"
"sync"
"time"
"github.com/samcharles93/archie-core/internal/channels/status"
controlpb "github.com/samcharles93/archie-core/internal/contracts/controlplane/v1"
@@ -87,6 +88,14 @@ type Server struct {
ControlPlane controlpb.ControlPlaneServiceClient
Identities identity.Repository
// ApplyStatus reports which control-plane resource version each process
// is running. Optional: nil renders an empty page rather than failing the
// dashboard (docs/prds/control-plane-apply-status.md).
ApplyStatus storecontract.ApplyStatusStore
// now is the clock applyState reads staleness against. Nil means
// time.Now; tests set it to pin a record's age.
now func() time.Time
// Token gates access. Empty means no check, which is how loopback binds
// stay frictionless -- see IsLoopback.
Token string
@@ -247,6 +256,7 @@ func (s *Server) registerMappingAndBindingRoutes(mux *http.ServeMux) {
func (s *Server) registerConfigAndLogRoutes(mux *http.ServeMux) {
mux.HandleFunc("GET /api/control-plane/catalog", s.handleControlPlaneCatalog)
mux.HandleFunc("GET /api/control-plane/apply-status", s.handleApplyStatus)
mux.HandleFunc("GET /api/control-plane/resources/{kind}", s.handleControlPlaneQuery)
mux.HandleFunc("GET /api/control-plane/resources/{kind}/history", s.handleControlPlaneHistory)
mux.HandleFunc("POST /api/control-plane/resources/{kind}/commands/{command}", s.handleControlPlaneCommand)