Design 47 H1: a view reader hears an event on its bare subject; an unhandled event is the module's finding (H-A-166, H-A-169) #203

Merged
mesh-tools merged 3 commits from fix/47-H1-sdk-bare-subject into main 2026-11-20 16:55:40 +00:00
2 changed files with 54 additions and 3 deletions
Showing only changes of commit a5225dfac3 - Show all commits
+12 -2
View File
@@ -77,6 +77,8 @@ type ProcessReader struct {
unheard int
lastUnheard string
startedAt time.Time
// hooked is the hub the count of unhandled events is wired on, so a second Start counts no event twice.
hooked *process.EventHub
}
// adoptedHear is one adopted follower's handler of its seat event, by its picture's bucket.
@@ -898,10 +900,18 @@ func (r *ProcessReader) Start(ctx context.Context, js jetstream.JetStream, cred
if r.Findings != nil {
// H-A-169: the events no handler of the module takes are its finding, cleared at each start
r.clearUnhandled()
hub.OnUnhandled(r.unhandled)
r.mu.Lock()
wire := r.hooked != hub
r.hooked = hub
r.mu.Unlock()
if wire {
hub.OnUnhandled(r.unhandled)
}
}
r.mu.Lock()
r.startedAt = r.now()
if r.startedAt.IsZero() {
r.startedAt = r.now() // the process's start; a second Start counts on from it
}
r.status = status
if !own {
// an event adopted after the start is handled on the module's hub (Adopt); the reader's own hub takes every one
+42 -1
View File
@@ -8,6 +8,7 @@ import (
"errors"
"fmt"
"slices"
"strings"
"testing"
"time"
@@ -157,7 +158,8 @@ func subTheViewsReaderAndTwoHandlersShareTheModulesOneDurable(t *testing.T) {
t.Fatal(err)
}
}
r := &views.ProcessReader{Module: "reader", Hub: hub, Logf: t.Logf,
found := &seenFindings{put: make(chan map[string]any, 8)}
r := &views.ProcessReader{Module: "reader", Hub: hub, Logf: t.Logf, Findings: func() views.WireWriter { return found },
Call: func(tool string, body any) (json.RawMessage, error) {
raw, _ := json.Marshal(body)
m, err := nc.Request("mesh.mod.o.call.o_read_modules.n0", raw, 5*time.Second)
@@ -235,6 +237,38 @@ func subTheViewsReaderAndTwoHandlersShareTheModulesOneDurable(t *testing.T) {
if info, err := cons.Info(ctx); err != nil || info.NumAckPending != 0 {
t.Fatalf("a message was left unacknowledged: %+v %v", info, err)
}
// H-A-169: the one event no handler takes is the module's finding event-unhandled, counted once, naming its subject,
// though the reader started twice.
r.Start(ctx, js, cred, nil)
select {
case f := <-found.put:
summary, _ := f["summary"].(string)
if f["kind"] != views.EventUnhandledKind || f["id"] != "reader" || !strings.Contains(summary, "took 1 events") ||
!strings.Contains(summary, "mesh.mod.z.event.nobody") {
t.Fatalf("the unhandled event's finding: %v", f)
}
case <-time.After(10 * time.Second):
t.Fatal("the unhandled event wrote no finding")
}
if _, err := js.Publish(ctx, "mesh.mod.z.event.nobody", []byte("{}")); err != nil {
t.Fatal(err)
}
if _, err := js.Publish(ctx, "mesh.mod.p.event.a", []byte("{}")); err != nil {
t.Fatal(err)
}
select {
case <-got["a"]: // the hub hands messages in order: every hook of the event before has run
case <-time.After(10 * time.Second):
t.Fatal("the handled event after the unhandled one was not heard")
}
select {
case f := <-found.put:
if summary, _ := f["summary"].(string); !strings.Contains(summary, "took 2 events") || len(found.put) != 0 {
t.Fatalf("after a second start one event is counted once: %v, and %d more", f, len(found.put))
}
default:
t.Fatal("the unhandled event after the second start wrote no finding")
}
// A hub started before the bus holds its durable says so, and binds at a later start: a module is not lost for the
// order in which the bus makes its consumer.
late := process.Account{Node: "n1", Module: "late"}
@@ -270,3 +304,10 @@ func TestAReaderHearsAnOwnersChangesAsOrdinaryEvents(t *testing.T) {
t.Run("TheViewsReaderAndTwoHandlersShareTheModulesOneDurable", subTheViewsReaderAndTwoHandlersShareTheModulesOneDurable)
t.Run("WriterSaysItsEventsAndHearsAsksOnTheBus", subWriterSaysItsEventsAndHearsAsksOnTheBus)
}
// seenFindings is a findings writer that hands each finding written to the test.
type seenFindings struct{ put chan map[string]any }
func (f *seenFindings) WriteWire(_ context.Context, w map[string]any) error { f.put <- w; return nil }
func (f *seenFindings) Clear(context.Context, string, string, string, string) error { return nil }
func (f *seenFindings) ModuleNode() (string, string) { return "reader", "n1" }