merge(run/pb-eda-realtime): announce binding and mapping edits on the activity stream

Lane C. A successful binding or mapping write on the State Store's
PocketBase tables now emits one activity event on the same task-DB path
capture arrival already uses, so a dashboard edit is visible on the
activity stream without a manual refresh.

The event is an invalidation signal, not a payload: it carries the record
id and the action, and consumers refetch through the authenticated API. A
nil notifier is a silent no-op, so stores composed without one (tests,
tooling) are unchanged.
This commit is contained in:
sam committed 2026-09-23 03:02:41 +10:00
commit 5ca499b366
5 files changed
+342 -6

No files matched your search

+9
View File
@@ -105,6 +105,15 @@ const (
// trigger. data: session, channel.
KindTurnCompleted = "turn_completed"
// EDA edit activity (bindings and mappings on the captures store). Each
// successful write on the State Store's PocketBase tables emits one
// event through the same task-DB path capture arrival uses, so a
// dashboard edit is visible on the activity stream without a manual
// refresh. data: id (the edited record), action (create, update,
// approve, delete).
KindBindingChanged = "binding_changed"
KindMappingChanged = "mapping_changed"
// KindUpdateReport carries the phase-2 outcome of a dashboard-initiated
// update -- whether the restarted daemon came back up healthy and on
// the version it claimed, relayed once on the boot that finds the
@@ -10,6 +10,7 @@ import (
"github.com/samcharles93/archie-core/internal/domain/binding"
"github.com/samcharles93/archie-core/internal/domain/mapping"
"github.com/samcharles93/archie-core/internal/events"
)
// --- mappings ---
@@ -54,6 +55,7 @@ func (s *Store) InsertMapping(_ context.Context, m mapping.Mapping) (string, err
if err := s.app.Save(r); err != nil {
return "", fmt.Errorf("edastore: insert mapping: %w", err)
}
s.notifyWrite(events.KindMappingChanged, "mapping", "create", r.Id)
return r.Id, nil
}
@@ -96,7 +98,11 @@ func (s *Store) UpdateMapping(_ context.Context, m mapping.Mapping) error {
if err := s.applyMapping(r, m); err != nil {
return err
}
return s.app.Save(r)
if err := s.app.Save(r); err != nil {
return err
}
s.notifyWrite(events.KindMappingChanged, "mapping", "update", m.ID)
return nil
}
func (s *Store) DeleteMapping(_ context.Context, id string) error {
@@ -107,7 +113,11 @@ func (s *Store) DeleteMapping(_ context.Context, id string) error {
if r == nil {
return ErrMappingNotFound
}
return s.app.Delete(r)
if err := s.app.Delete(r); err != nil {
return err
}
s.notifyWrite(events.KindMappingChanged, "mapping", "delete", id)
return nil
}
// --- bindings ---
@@ -189,6 +199,7 @@ func (s *Store) InsertBinding(ctx context.Context, b binding.Binding) (string, e
return "", fmt.Errorf("edastore: insert binding: %w", err)
}
_ = ctx
s.notifyWrite(events.KindBindingChanged, "binding", "create", r.Id)
return r.Id, nil
}
@@ -262,7 +273,11 @@ func (s *Store) UpdateBinding(_ context.Context, b binding.Binding) error {
}
r.Set("status", string(binding.StatusPendingApproval))
r.Set("version", r.GetInt("version")+1)
return s.app.Save(r)
if err := s.app.Save(r); err != nil {
return err
}
s.notifyWrite(events.KindBindingChanged, "binding", "update", b.ID)
return nil
}
func (s *Store) DeleteBinding(_ context.Context, id string) error {
@@ -273,7 +288,11 @@ func (s *Store) DeleteBinding(_ context.Context, id string) error {
if r == nil {
return ErrBindingNotFound
}
return s.app.Delete(r)
if err := s.app.Delete(r); err != nil {
return err
}
s.notifyWrite(events.KindBindingChanged, "binding", "delete", id)
return nil
}
// ApproveBinding is the only transition that arms a binding. It moves
@@ -303,7 +322,11 @@ func (s *Store) ApproveBinding(_ context.Context, id string) error {
return ErrBindingTransition
}
r.Set("status", string(binding.StatusArmed))
return s.app.Save(r)
if err := s.app.Save(r); err != nil {
return err
}
s.notifyWrite(events.KindBindingChanged, "binding", "approve", id)
return nil
}
// --- dispatch ledgers ---
+9 -1
View File
@@ -29,6 +29,7 @@ import (
_ "modernc.org/sqlite"
"github.com/samcharles93/archie-core/internal/domain/storecontract"
"github.com/samcharles93/archie-core/internal/events"
)
// Collection names. They are unprefixed because this store owns its database;
@@ -71,6 +72,12 @@ type Config struct {
// Cipher encrypts binding secrets at rest. Nil keeps them plaintext,
// which is the behaviour that predates the option.
Cipher BindingCipher
// Notify receives one event per successful write on the bindings and
// mappings tables, so an operator edit surfaces on the dashboard's
// activity stream exactly like capture arrival does. Nil keeps writes
// silent, which is the behaviour that predates the option and what
// every existing constructor gets.
Notify func(events.Event)
// DBPath is the SQLite file PocketBase owns for these collections.
DBPath string
// DataDir holds PocketBase's auxiliary database and settings.
@@ -81,6 +88,7 @@ type Config struct {
type Store struct {
app *pocketbase.PocketBase
cipher BindingCipher
notify func(events.Event)
}
// Open bootstraps the store and ensures its collections exist.
@@ -110,7 +118,7 @@ func Open(cfg Config) (*Store, error) {
if err := ensureCollections(app); err != nil {
return nil, err
}
return &Store{app: app, cipher: cfg.Cipher}, nil
return &Store{app: app, cipher: cfg.Cipher, notify: cfg.Notify}, nil
}
// App exposes the PocketBase application so one process can serve this store
@@ -0,0 +1,26 @@
package edastore
import "github.com/samcharles93/archie-core/internal/events"
// notifyWrite announces one successful write on the bindings or mappings
// tables. It rides the same activity-event path capture arrival uses: the
// State Store's event insert feeds the webui pump, which broadcasts to every
// connected browser, so an operator edit is visible without a manual
// refresh. The data carries the record id and action; the row itself stays
// the only source of the full record, and consumers refetch through the
// authenticated API -- the event is an invalidation signal, not a payload.
//
// A nil notifier is a silent no-op: stores composed without one (tests,
// tooling) keep working unchanged, mirroring the nil Publish on
// captureintake.Receiver.
func (s *Store) notifyWrite(kind, subject, action, id string) {
if s.notify == nil {
return
}
past := map[string]string{"create": "created", "update": "updated", "approve": "approved", "delete": "deleted"}[action]
s.notify(events.Event{
Kind: kind,
Detail: subject + " " + past,
Data: map[string]any{"id": id, "action": action},
})
}
@@ -0,0 +1,270 @@
package edastore
import (
"errors"
"testing"
"github.com/samcharles93/archie-core/internal/domain/binding"
"github.com/samcharles93/archie-core/internal/domain/mapping"
"github.com/samcharles93/archie-core/internal/events"
)
// recordingNotifier collects every event the store emits so a test can
// assert on the exact sequence, mirroring captureintake's recordingPublisher.
type recordingNotifier struct {
got []events.Event
}
func (r *recordingNotifier) notify(e events.Event) { r.got = append(r.got, e) }
func (r *recordingNotifier) reset() { r.got = nil }
// notifiedStore is an OpenTest store with the notifier wired, the way
// composition does it through Config.Notify.
func notifiedStore(t *testing.T) (*Store, *recordingNotifier) {
t.Helper()
s := OpenTest(t)
n := &recordingNotifier{}
s.notify = n.notify
return s, n
}
func TestWriteMethodsNotifyOnSuccess(t *testing.T) {
b := func(source string) binding.Binding {
return binding.Binding{
Name: "webhook to workflow",
Matcher: binding.Matcher{Source: source},
Workflow: "triage",
}
}
t.Run("InsertBinding", func(t *testing.T) {
s, n := notifiedStore(t)
id, err := s.InsertBinding(t.Context(), b("src-insert"))
if err != nil {
t.Fatalf("InsertBinding() error = %v", err)
}
if len(n.got) != 1 {
t.Fatalf("InsertBinding notified %d times, want 1: %+v", len(n.got), n.got)
}
assertEditEvent(t, n.got[0], events.KindBindingChanged, "create", id)
})
t.Run("UpdateBinding", func(t *testing.T) {
s, n := notifiedStore(t)
id := insertBinding(t, s, bindingInput{Name: "before", Source: "src-update", Workflow: "triage"})
n.reset()
bb := b("src-update")
bb.ID = id
bb.Workflow = "escalate"
if err := s.UpdateBinding(t.Context(), bb); err != nil {
t.Fatalf("UpdateBinding() error = %v", err)
}
if len(n.got) != 1 {
t.Fatalf("UpdateBinding notified %d times, want 1: %+v", len(n.got), n.got)
}
assertEditEvent(t, n.got[0], events.KindBindingChanged, "update", id)
})
t.Run("ApproveBinding", func(t *testing.T) {
s, n := notifiedStore(t)
id := insertBinding(t, s, bindingInput{Name: "draft", Source: "src-approve", Workflow: "triage"})
bb := b("src-approve")
bb.ID = id
// Approval requires pending_approval; the edit transition also fires.
if err := s.UpdateBinding(t.Context(), bb); err != nil {
t.Fatalf("UpdateBinding() error = %v", err)
}
n.reset()
if err := s.ApproveBinding(t.Context(), id); err != nil {
t.Fatalf("ApproveBinding() error = %v", err)
}
if len(n.got) != 1 {
t.Fatalf("ApproveBinding notified %d times, want 1: %+v", len(n.got), n.got)
}
assertEditEvent(t, n.got[0], events.KindBindingChanged, "approve", id)
})
t.Run("DeleteBinding", func(t *testing.T) {
s, n := notifiedStore(t)
id := insertBinding(t, s, bindingInput{Name: "doomed", Source: "src-delete", Workflow: "triage"})
n.reset()
if err := s.DeleteBinding(t.Context(), id); err != nil {
t.Fatalf("DeleteBinding() error = %v", err)
}
if len(n.got) != 1 {
t.Fatalf("DeleteBinding notified %d times, want 1: %+v", len(n.got), n.got)
}
assertEditEvent(t, n.got[0], events.KindBindingChanged, "delete", id)
})
t.Run("InsertMapping", func(t *testing.T) {
s, n := notifiedStore(t)
id, err := s.InsertMapping(t.Context(), mapping.Mapping{Name: "payload-map"})
if err != nil {
t.Fatalf("InsertMapping() error = %v", err)
}
if len(n.got) != 1 {
t.Fatalf("InsertMapping notified %d times, want 1: %+v", len(n.got), n.got)
}
assertEditEvent(t, n.got[0], events.KindMappingChanged, "create", id)
})
t.Run("UpdateMapping", func(t *testing.T) {
s, n := notifiedStore(t)
id, err := s.InsertMapping(t.Context(), mapping.Mapping{Name: "payload-map"})
if err != nil {
t.Fatalf("InsertMapping() error = %v", err)
}
n.reset()
m, err := s.GetMapping(t.Context(), id)
if err != nil {
t.Fatalf("GetMapping() error = %v", err)
}
m.Name = "renamed"
if err := s.UpdateMapping(t.Context(), *m); err != nil {
t.Fatalf("UpdateMapping() error = %v", err)
}
if len(n.got) != 1 {
t.Fatalf("UpdateMapping notified %d times, want 1: %+v", len(n.got), n.got)
}
assertEditEvent(t, n.got[0], events.KindMappingChanged, "update", id)
})
t.Run("DeleteMapping", func(t *testing.T) {
s, n := notifiedStore(t)
id, err := s.InsertMapping(t.Context(), mapping.Mapping{Name: "doomed"})
if err != nil {
t.Fatalf("InsertMapping() error = %v", err)
}
n.reset()
if err := s.DeleteMapping(t.Context(), id); err != nil {
t.Fatalf("DeleteMapping() error = %v", err)
}
if len(n.got) != 1 {
t.Fatalf("DeleteMapping notified %d times, want 1: %+v", len(n.got), n.got)
}
assertEditEvent(t, n.got[0], events.KindMappingChanged, "delete", id)
})
}
func assertEditEvent(t *testing.T, e events.Event, kind, action, id string) {
t.Helper()
if e.Kind != kind {
t.Errorf("event kind = %q, want %q", e.Kind, kind)
}
if e.Detail == "" {
t.Errorf("event detail is empty")
}
if got := e.Data["id"]; got != id {
t.Errorf("event data id = %v, want %q", got, id)
}
if got := e.Data["action"]; got != action {
t.Errorf("event data action = %v, want %q", got, action)
}
}
func TestFailedWritesNotifyNothing(t *testing.T) {
s, n := notifiedStore(t)
// Seed one binding and one mapping, then clear the seed-time events.
seedID := insertBinding(t, s, bindingInput{Name: "seed", Source: "src-seed", Workflow: "triage"})
mid, err := s.InsertMapping(t.Context(), mapping.Mapping{Name: "seed-map"})
if err != nil {
t.Fatalf("InsertMapping() error = %v", err)
}
n.reset()
t.Run("UpdateBinding nonexistent", func(t *testing.T) {
bb := binding.Binding{ID: "missing", Matcher: binding.Matcher{Source: "src-other"}}
if err := s.UpdateBinding(t.Context(), bb); !errors.Is(err, ErrBindingNotFound) {
t.Fatalf("UpdateBinding() error = %v, want ErrBindingNotFound", err)
}
})
t.Run("ApproveBinding refuses draft", func(t *testing.T) {
if err := s.ApproveBinding(t.Context(), seedID); !errors.Is(err, ErrBindingTransition) {
t.Fatalf("ApproveBinding() error = %v, want ErrBindingTransition", err)
}
})
t.Run("InsertBinding overlaps source", func(t *testing.T) {
if _, err := s.InsertBinding(t.Context(), binding.Binding{
Matcher: binding.Matcher{Source: "src-seed"},
}); !errors.Is(err, ErrBindingOverlap) {
t.Fatalf("InsertBinding() error = %v, want ErrBindingOverlap", err)
}
})
t.Run("DeleteBinding nonexistent", func(t *testing.T) {
if err := s.DeleteBinding(t.Context(), "missing"); !errors.Is(err, ErrBindingNotFound) {
t.Fatalf("DeleteBinding() error = %v, want ErrBindingNotFound", err)
}
})
t.Run("UpdateMapping nonexistent", func(t *testing.T) {
if err := s.UpdateMapping(t.Context(), mapping.Mapping{ID: "missing"}); !errors.Is(err, ErrMappingNotFound) {
t.Fatalf("UpdateMapping() error = %v, want ErrMappingNotFound", err)
}
})
t.Run("DeleteMapping nonexistent", func(t *testing.T) {
if err := s.DeleteMapping(t.Context(), "missing"); !errors.Is(err, ErrMappingNotFound) {
t.Fatalf("DeleteMapping() error = %v, want ErrMappingNotFound", err)
}
})
_ = mid
if len(n.got) != 0 {
t.Fatalf("failed writes notified %d times: %+v", len(n.got), n.got)
}
}
// TestNilNotifierIsNoOp pins that a store composed without a notifier keeps
// every write path working: the existing constructors and tests never set
// one, so a nil-dereference there would break them all.
func TestNilNotifierIsNoOp(t *testing.T) {
s := OpenTest(t)
id, err := s.InsertBinding(t.Context(), binding.Binding{
Name: "nil-safe",
Matcher: binding.Matcher{Source: "src-nil"},
})
if err != nil {
t.Fatalf("InsertBinding with nil notifier: error = %v", err)
}
bb, err := s.GetBinding(t.Context(), id)
if err != nil {
t.Fatalf("GetBinding() error = %v", err)
}
bb.Workflow = "escalate"
if err := s.UpdateBinding(t.Context(), *bb); err != nil {
t.Fatalf("UpdateBinding() error = %v", err)
}
if err := s.ApproveBinding(t.Context(), id); err != nil {
t.Fatalf("ApproveBinding() error = %v", err)
}
if err := s.DeleteBinding(t.Context(), id); err != nil {
t.Fatalf("DeleteBinding() error = %v", err)
}
if _, err := s.InsertMapping(t.Context(), mapping.Mapping{Name: "m"}); err != nil {
t.Fatalf("InsertMapping() error = %v", err)
}
}
// TestOpenHonoursConfigNotify pins the composition surface: the wire-in sets
// Config.Notify, not the unexported field.
func TestOpenHonoursConfigNotify(t *testing.T) {
n := &recordingNotifier{}
dir := t.TempDir()
s, err := Open(Config{
DBPath: t.TempDir() + "/eda.sqlite",
DataDir: dir,
Notify: n.notify,
})
if err != nil {
t.Fatalf("Open() error = %v", err)
}
t.Cleanup(func() { _ = s.Close() })
if _, err := s.InsertMapping(t.Context(), mapping.Mapping{Name: "wired"}); err != nil {
t.Fatalf("InsertMapping() error = %v", err)
}
if len(n.got) != 1 {
t.Fatalf("store built from Config.Notify notified %d times, want 1: %+v", len(n.got), n.got)
}
}