feat(curator): track and expose recent activity per curator

Add a small bounded activity tracker (activityTracker, in
internal/domain/curator/activity.go) fed directly from the runtime's
existing emitRun point: last-run time, last-run action count, and up to
20 recent actions per curator, newest first. No persistence layer -- it
resets on restart, same as the rest of the runtime's in-memory state.
Registry.RecordActivity/Activity expose it.

Closes archie-core-1786637489932-6-1f803e02.
This commit is contained in:
sam committed 2026-08-28 22:28:48 +10:00
1 parent 76d13b76ce
commit ced7bb8038
15 files changed
+641 -8

No files matched your search

+77
View File
@@ -0,0 +1,77 @@
package curator
import (
"sync"
"time"
)
// maxRecentActions bounds how much per-curator history activityTracker
// keeps in memory. Curator activity is inspectable, not archival -- there is
// deliberately no persistence layer here (archie-core-1786637489932-6),
// so this is a ring buffer, not a log.
const maxRecentActions = 20
// Activity is a curator's inspectable recent history: when it last ran, how
// many actions that run made, and the most recent actions across all runs.
// Recent is ordered newest first and capped at maxRecentActions.
type Activity struct {
LastRunAt time.Time
LastRunActions int
Recent []Action
}
// activityTracker records recent curator activity in memory, fed directly
// from the runtime's own emit point (Runtime.emitRun) rather than by
// re-subscribing to the event bus -- the runtime already has the pass
// result in hand, and EventSink is fire-and-forget with no replay. Activity
// resets on daemon restart, same as every other piece of runtime state the
// curator family holds only in memory.
type activityTracker struct {
mu sync.Mutex
data map[string]*Activity
}
func newActivityTracker() *activityTracker {
return &activityTracker{data: make(map[string]*Activity)}
}
// record appends one pass's actions to the curator's history, newest first,
// and updates the last-run summary. actions may be empty (an idle pass still
// updates LastRunAt).
func (t *activityTracker) record(name string, at time.Time, actions []Action) {
t.mu.Lock()
defer t.mu.Unlock()
a := t.data[name]
if a == nil {
a = &Activity{}
t.data[name] = a
}
a.LastRunAt = at
a.LastRunActions = len(actions)
merged := make([]Action, 0, len(actions)+len(a.Recent))
for i := len(actions) - 1; i >= 0; i-- {
merged = append(merged, actions[i])
}
merged = append(merged, a.Recent...)
if len(merged) > maxRecentActions {
merged = merged[:maxRecentActions]
}
a.Recent = merged
}
// snapshot returns a copy of the named curator's activity, safe for the
// caller to read and mutate without racing the tracker's own writes.
func (t *activityTracker) snapshot(name string) (Activity, bool) {
t.mu.Lock()
defer t.mu.Unlock()
a, ok := t.data[name]
if !ok {
return Activity{}, false
}
cp := *a
cp.Recent = append([]Action(nil), a.Recent...)
return cp, true
}
+125
View File
@@ -0,0 +1,125 @@
package curator
import (
"testing"
"time"
)
func TestActivityTrackerRecordAndSnapshot(t *testing.T) {
tr := newActivityTracker()
at := time.Date(2026, 1, 1, 12, 0, 0, 0, time.UTC)
actions := []Action{
{At: at, Type: "skill.updated", Detail: "rewrote foo", Reason: "stale"},
{At: at, Type: "memory.written", Detail: "noted bar", Reason: "input"},
}
tr.record("curator-a", at, actions)
got, ok := tr.snapshot("curator-a")
if !ok {
t.Fatalf("snapshot(curator-a) ok = false, want true")
}
if !got.LastRunAt.Equal(at) {
t.Fatalf("LastRunAt = %v, want %v", got.LastRunAt, at)
}
if got.LastRunActions != len(actions) {
t.Fatalf("LastRunActions = %d, want %d", got.LastRunActions, len(actions))
}
if len(got.Recent) != len(actions) {
t.Fatalf("Recent len = %d, want %d", len(got.Recent), len(actions))
}
// Newest-first within a single run: the last action recorded comes first.
if got.Recent[0].Type != "memory.written" {
t.Fatalf("Recent[0].Type = %q, want %q", got.Recent[0].Type, "memory.written")
}
}
func TestActivityTrackerUnknownCuratorNotFound(t *testing.T) {
tr := newActivityTracker()
if _, ok := tr.snapshot("nope"); ok {
t.Fatalf("snapshot(nope) ok = true, want false")
}
}
func TestActivityTrackerAccumulatesAcrossRuns(t *testing.T) {
tr := newActivityTracker()
base := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC)
tr.record("c", base, []Action{{At: base, Type: "first"}})
second := base.Add(time.Hour)
tr.record("c", second, []Action{{At: second, Type: "second"}})
got, ok := tr.snapshot("c")
if !ok {
t.Fatalf("snapshot(c) ok = false, want true")
}
if !got.LastRunAt.Equal(second) {
t.Fatalf("LastRunAt = %v, want %v", got.LastRunAt, second)
}
if got.LastRunActions != 1 {
t.Fatalf("LastRunActions = %d, want 1", got.LastRunActions)
}
if len(got.Recent) != 2 {
t.Fatalf("Recent len = %d, want 2", len(got.Recent))
}
if got.Recent[0].Type != "second" {
t.Fatalf("Recent[0].Type = %q, want %q (newest first)", got.Recent[0].Type, "second")
}
if got.Recent[1].Type != "first" {
t.Fatalf("Recent[1].Type = %q, want %q", got.Recent[1].Type, "first")
}
}
func TestActivityTrackerCapsRecentActions(t *testing.T) {
tr := newActivityTracker()
at := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC)
for range maxRecentActions + 5 {
tr.record("c", at, []Action{{At: at, Type: "a"}})
}
got, ok := tr.snapshot("c")
if !ok {
t.Fatalf("snapshot(c) ok = false, want true")
}
if len(got.Recent) != maxRecentActions {
t.Fatalf("Recent len = %d, want %d (capped)", len(got.Recent), maxRecentActions)
}
}
func TestActivityTrackerSnapshotIsolatesCallerFromMutation(t *testing.T) {
tr := newActivityTracker()
at := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC)
tr.record("c", at, []Action{{At: at, Type: "a"}})
got, _ := tr.snapshot("c")
got.Recent[0].Type = "mutated"
got2, _ := tr.snapshot("c")
if got2.Recent[0].Type != "a" {
t.Fatalf("internal state mutated via returned snapshot: got %q, want %q", got2.Recent[0].Type, "a")
}
}
func TestRegistryRecordsAndExposesActivity(t *testing.T) {
r := NewRegistry(Registrar{})
at := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC)
actions := []Action{{At: at, Type: "skill.updated", Detail: "d", Reason: "r"}}
r.RecordActivity("a", at, actions)
got, ok := r.Activity("a")
if !ok {
t.Fatalf("Activity(a) ok = false, want true")
}
if !got.LastRunAt.Equal(at) {
t.Fatalf("LastRunAt = %v, want %v", got.LastRunAt, at)
}
if len(got.Recent) != 1 {
t.Fatalf("Recent len = %d, want 1", len(got.Recent))
}
if _, ok := r.Activity("unknown"); ok {
t.Fatalf("Activity(unknown) ok = true, want false")
}
}
+22
View File
@@ -7,6 +7,7 @@ import (
"reflect"
"slices"
"sync"
"time"
)
// Registry errors.
@@ -43,6 +44,11 @@ type Registry struct {
status map[string]curatorStatus
order []string
state registryState
// activity tracks recent per-curator run history for observability
// (archie-core-1786637489932-6). It has its own mutex and is safe to
// use independently of r.mu.
activity *activityTracker
}
// NewRegistry builds the family registry. A nil Clock is replaced with the
@@ -56,9 +62,25 @@ func NewRegistry(host Registrar) *Registry {
host: host,
curators: make(map[string]CuratorEngine),
status: make(map[string]curatorStatus),
activity: newActivityTracker(),
}
}
// RecordActivity records one pass's outcome for the named curator so it is
// inspectable at runtime: last run time, action count, and recent actions
// with their reasons. Called by the runtime at the same point it emits the
// curator_run/curator_action events (Runtime.emitRun), so the two views
// never disagree.
func (r *Registry) RecordActivity(name string, at time.Time, actions []Action) {
r.activity.record(name, at, actions)
}
// Activity returns the named curator's recorded activity, if any has been
// recorded yet.
func (r *Registry) Activity(name string) (Activity, bool) {
return r.activity.snapshot(name)
}
// Register validates the curator's declared shape against the registrar and
// adds it, binding the curator to a host view filtered to its declared
// capabilities. Registration fails on nil engines, invalid manifests,
+2
View File
@@ -290,6 +290,8 @@ func (rt *Runtime) fail(name, phase string, err error) {
// emitRun publishes one run summary plus one event per action, so what ran,
// when, what it changed, and why all stay attributable (archie-core-114).
func (rt *Runtime) emitRun(name string, at time.Time, result PassResult) {
rt.registry.RecordActivity(name, at, result.Actions)
sink := rt.registry.Host().Events
if sink == nil {
return
+81
View File
@@ -0,0 +1,81 @@
package webui
import (
"net/http"
"time"
)
// CuratorActionView is one recorded curator action, as shown on the
// dashboard's curator activity view: what changed and why.
type CuratorActionView struct {
At time.Time `json:"at"`
Type string `json:"type"`
Detail string `json:"detail"`
Reason string `json:"reason"`
}
// CuratorView is one registered curator's observable state: identity,
// health, and recent activity. Backed by curator.Registry -- see
// handleCurators.
type CuratorView struct {
Name string `json:"name"`
Health CuratorHealthView `json:"health"`
// LastRunAt is a pointer so a curator that has never run omits the
// field entirely instead of serializing time.Time's zero value
// ("0001-01-01T00:00:00Z") -- json's "omitempty" does not treat a
// zero-valued struct as empty, and a non-empty string is truthy to
// the dashboard's "has this run yet" check.
LastRunAt *time.Time `json:"last_run_at,omitempty"`
LastRunActions int `json:"last_run_actions"`
RecentActions []CuratorActionView `json:"recent_actions"`
}
// CuratorHealthView mirrors curator.Health for the wire.
type CuratorHealthView struct {
Status string `json:"status"`
Message string `json:"message,omitempty"`
}
// handleCurators reports which curators are registered, their point-in-time
// health, and their recent activity -- which curators exist, when each last
// ran, what it changed, and why (archie-core-1786637489932-6). The registry
// is the sole source of truth; this handler is transport only.
func (s *Server) handleCurators(w http.ResponseWriter, r *http.Request) {
if s.Curators == nil {
writeJSON(w, map[string]any{"curators": []CuratorView{}})
return
}
names := s.Curators.Names()
health := s.Curators.Health(r.Context())
views := make([]CuratorView, 0, len(names))
for _, name := range names {
h := health[name]
view := CuratorView{
Name: name,
Health: CuratorHealthView{
Status: string(h.Status),
Message: h.Message,
},
RecentActions: []CuratorActionView{},
}
if activity, ok := s.Curators.Activity(name); ok && !activity.LastRunAt.IsZero() {
at := activity.LastRunAt
view.LastRunAt = &at
view.LastRunActions = activity.LastRunActions
view.RecentActions = make([]CuratorActionView, 0, len(activity.Recent))
for _, a := range activity.Recent {
view.RecentActions = append(view.RecentActions, CuratorActionView{
At: a.At,
Type: a.Type,
Detail: a.Detail,
Reason: a.Reason,
})
}
}
views = append(views, view)
}
writeJSON(w, map[string]any{"curators": views})
}
+144
View File
@@ -0,0 +1,144 @@
package webui
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"github.com/samcharles93/archie-core/internal/domain/curator"
)
type stubCuratorEngine struct {
name string
manifest curator.Manifest
health curator.Health
}
func (c *stubCuratorEngine) Name() string { return c.name }
func (c *stubCuratorEngine) Version() string { return "v1" }
func (c *stubCuratorEngine) Manifest() curator.Manifest { return c.manifest }
func (c *stubCuratorEngine) Bind(curator.Registrar) {}
func (c *stubCuratorEngine) Start(context.Context) error { return nil }
func (c *stubCuratorEngine) Stop(context.Context) error { return nil }
func (c *stubCuratorEngine) Health(context.Context) curator.Health { return c.health }
func (c *stubCuratorEngine) Check(context.Context) (bool, error) { return false, nil }
func (c *stubCuratorEngine) Pass(context.Context, curator.PassInput) (curator.PassResult, error) {
return curator.PassResult{}, nil
}
func TestHandleCuratorsNilRegistryReturnsEmpty(t *testing.T) {
srv := newTestServer(t)
w := httptest.NewRecorder()
srv.Handler().ServeHTTP(w, httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/api/curators", nil))
if w.Code != http.StatusOK {
t.Fatalf("status = %d, body = %s", w.Code, w.Body)
}
var response struct {
Curators []CuratorView `json:"curators"`
}
if err := json.Unmarshal(w.Body.Bytes(), &response); err != nil {
t.Fatal(err)
}
if len(response.Curators) != 0 {
t.Fatalf("curators = %#v, want empty", response.Curators)
}
}
func TestHandleCuratorsReportsNamesHealthAndActivity(t *testing.T) {
registry := curator.NewRegistry(curator.Registrar{})
c := &stubCuratorEngine{
name: "skill-curator",
manifest: curator.Manifest{Interval: time.Minute},
health: curator.Health{Status: curator.HealthHealthy, Message: "ok"},
}
if err := registry.Register(c); err != nil {
t.Fatal(err)
}
at := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC)
registry.RecordActivity("skill-curator", at, []curator.Action{
{At: at, Type: "skill.updated", Detail: "rewrote foo", Reason: "stale content"},
})
srv := newTestServer(t)
srv.Curators = registry
w := httptest.NewRecorder()
srv.Handler().ServeHTTP(w, httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/api/curators", nil))
if w.Code != http.StatusOK {
t.Fatalf("status = %d, body = %s", w.Code, w.Body)
}
var response struct {
Curators []CuratorView `json:"curators"`
}
if err := json.Unmarshal(w.Body.Bytes(), &response); err != nil {
t.Fatal(err)
}
if len(response.Curators) != 1 {
t.Fatalf("curators = %#v, want 1", response.Curators)
}
got := response.Curators[0]
if got.Name != "skill-curator" {
t.Fatalf("Name = %q, want skill-curator", got.Name)
}
if got.Health.Status != string(curator.HealthHealthy) || got.Health.Message != "ok" {
t.Fatalf("Health = %#v", got.Health)
}
if got.LastRunAt == nil || !got.LastRunAt.Equal(at) {
t.Fatalf("LastRunAt = %v, want %v", got.LastRunAt, at)
}
if got.LastRunActions != 1 {
t.Fatalf("LastRunActions = %d, want 1", got.LastRunActions)
}
if len(got.RecentActions) != 1 || got.RecentActions[0].Type != "skill.updated" || got.RecentActions[0].Reason != "stale content" {
t.Fatalf("RecentActions = %#v", got.RecentActions)
}
}
func TestHandleCuratorsReportsRegisteredCuratorWithNoActivityYet(t *testing.T) {
registry := curator.NewRegistry(curator.Registrar{})
c := &stubCuratorEngine{
name: "never-run",
manifest: curator.Manifest{Interval: time.Minute},
health: curator.Health{Status: curator.HealthHealthy},
}
if err := registry.Register(c); err != nil {
t.Fatal(err)
}
srv := newTestServer(t)
srv.Curators = registry
w := httptest.NewRecorder()
srv.Handler().ServeHTTP(w, httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/api/curators", nil))
if w.Code != http.StatusOK {
t.Fatalf("status = %d, body = %s", w.Code, w.Body)
}
var response struct {
Curators []CuratorView `json:"curators"`
}
if err := json.Unmarshal(w.Body.Bytes(), &response); err != nil {
t.Fatal(err)
}
if len(response.Curators) != 1 {
t.Fatalf("curators = %#v, want 1", response.Curators)
}
if got := response.Curators[0]; got.LastRunAt != nil || got.LastRunActions != 0 || len(got.RecentActions) != 0 {
t.Fatalf("expected zero-value activity for a never-run curator, got %#v", got)
}
// Regression guard: time.Time's zero value is not "empty" to
// encoding/json, so a naive time.Time field would serialize as
// "0001-01-01T00:00:00Z" (a truthy string on the frontend) instead of
// being omitted. Assert the raw JSON, not just the unmarshalled Go
// value, so a future revert of the *time.Time fix is caught here.
if body := w.Body.String(); strings.Contains(body, "0001-01-01") {
t.Fatalf("response leaks zero-value time.Time: %s", body)
}
}
+8
View File
@@ -19,6 +19,7 @@ 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/domain/workflow"
"github.com/samcharles93/archie-core/internal/events"
"github.com/samcharles93/archie-core/internal/gateway"
@@ -104,6 +105,12 @@ type Server struct {
// unavailable rather than failing when it is nil.
Memory *memory.Manager
// Curators is the daemon's live curator registry (epic archie-core-yp9).
// Backs GET /api/curators: registered names, per-curator health, and
// recent activity (archie-core-1786637489932-6). Optional: nil reports
// an empty curator list rather than failing the dashboard.
Curators *curator.Registry
// Chat exposes the same gateway router used by the conversational
// channels. Optional: the dashboard simply omits chat when it is nil.
Chat *ChatService
@@ -255,6 +262,7 @@ func (s *Server) Handler() http.Handler {
mux.HandleFunc("GET /api/workflows", s.handleWorkflows)
mux.HandleFunc("POST /api/work-requests", s.handleWorkRequest)
mux.HandleFunc("GET /api/skills", s.handleSkills)
mux.HandleFunc("GET /api/curators", s.handleCurators)
mux.HandleFunc("GET /api/captures", s.handleCaptures)
mux.HandleFunc("GET /api/mappings", s.handleMappingsList)
mux.HandleFunc("POST /api/mappings", s.handleMappingCreate)