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
5 changed files with 106 additions and 2 deletions
Showing only changes of commit e568e6dc63 - Show all commits
+15
View File
@@ -26,6 +26,8 @@ type EventHub struct {
routes []hubRoute
cons jetstream.ConsumeContext
bound []func(created time.Time)
// unhandled are told each subject no handler took (OnUnhandled).
unhandled []func(subject string)
// kept are the module's durables on the kept seat events' streams it handles (StartKept).
kept []jetstream.ConsumeContext
}
@@ -72,6 +74,15 @@ func (h *EventHub) OnBound(f func(created time.Time)) {
h.mu.Unlock()
}
// OnUnhandled registers f, told the subject of each message no handler takes, after the hub said it in the log and
// acknowledged it: the module's process reader counts them in its finding `event-unhandled` (design 47 H, H-A-166,
// H-A-169).
func (h *EventHub) OnUnhandled(f func(subject string)) {
h.mu.Lock()
h.unhandled = append(h.unhandled, f)
h.mu.Unlock()
}
// Patterns are the registered subject patterns, in order.
func (h *EventHub) Patterns() []string {
h.mu.RLock()
@@ -235,10 +246,14 @@ func (h *EventHub) hear(m jetstream.Msg) {
break
}
}
told := h.unhandled
h.mu.RUnlock()
if take == nil {
h.logf("events: %s is taken by no handler of this module, acknowledged", m.Subject())
_ = m.Ack()
for _, f := range told {
f(m.Subject())
}
return
}
take(m)
+9
View File
@@ -37,6 +37,15 @@ func TestTheHubMatchesSubjectsAndRefusesOverlappingPatterns(t *testing.T) {
if err := h.Handle("mesh.mod.q.event.>", nop); err != nil {
t.Fatal(err)
}
// H-A-169: a message no handler takes is acknowledged and told to each OnUnhandled hook, with its subject; one taken is not.
var told []string
h.OnUnhandled(func(subject string) { told = append(told, subject) })
taken, lost := &bindMsg{subject: "mesh.mod.q.event.x"}, &bindMsg{subject: "mesh.seat.s.event.nobody"}
h.hear(taken)
h.hear(lost)
if !lost.acked || len(told) != 1 || told[0] != "mesh.seat.s.event.nobody" {
t.Errorf("the unhandled message: acked %v, told %v", lost.acked, told)
}
// W1.12: a seat's event kept in a stream of its own is heard on one bus name and one durable, the maker's and the
// hearer's alike; an event kept last per subject is delivered from the last of each subject. The name is the
// contract's Stream<E> as it stands (seatdef.KeptStream), never made again here: a dotted or unprefixed name is
+11
View File
@@ -23,6 +23,9 @@ const (
// EntryNotFoundKind is a part's finding of an entry by fingerprint the seat mesh-catalog's `entry` answered it does not hold
// (design 47 G §614, A-15, A-103): scope the part, id the fingerprint.
EntryNotFoundKind = "entry-not-found"
// EventUnhandledKind is a process's finding of the events its durable consumer delivered that no handler of the module
// took, one per module, counted since the process started (design 47 H, H-A-166, H-A-169).
EventUnhandledKind = "event-unhandled"
)
// viewFinding is what a views part says is wrong with a state it owns or follows, in plain fields: kind and id name it, summary
@@ -120,3 +123,11 @@ func WriteEntryNotFound(w WireWriter, fingerprint string, since, at time.Time) e
func ClearEntryNotFound(w WireWriter, fingerprint string) error {
return viewFinding{kind: EntryNotFoundKind, id: fingerprint}.drop(w)
}
// unhandledFinding is the module's one finding of the events no handler took since its process started (H-A-169): its id the
// module, its summary the count and the last subject.
func unhandledFinding(module string, count int, last string, since, at time.Time) viewFinding {
summary := fmt.Sprintf("%s took %d events by no handler since its process started at %s, the last on %s: what the module consumes it does not act on",
module, count, since.UTC().Format(time.RFC3339), last)
return viewFinding{kind: EventUnhandledKind, id: module, summary: summary, headline: fmt.Sprintf("%s hears events it does not handle", module), at: at.UTC()}
}
+54
View File
@@ -73,6 +73,10 @@ type ProcessReader struct {
asked bool
begun map[string]context.CancelFunc
status *process.AwaitedBucket
// unheard counts the events no handler of the module took since Start, the last of them on lastUnheard (H-A-169).
unheard int
lastUnheard string
startedAt time.Time
}
// adoptedHear is one adopted follower's handler of its seat event, by its picture's bucket.
@@ -395,6 +399,50 @@ func (r *ProcessReader) Hear(ctx context.Context) func(jetstream.Msg) {
}
r.logf("views: %s is no views event of an owner followed, acknowledged", m.Subject())
_ = m.Ack()
r.unhandled(m.Subject())
}
}
// now is the reader's clock.
func (r *ProcessReader) now() time.Time {
if r.Now != nil {
return r.Now()
}
return time.Now()
}
// unhandled counts an event no handler of the module took, said in the log already, in the module's finding
// `event-unhandled` (design 47 H, H-A-166, H-A-169); with no findings writer the log line stands alone.
func (r *ProcessReader) unhandled(subject string) {
if r.Findings == nil {
return
}
r.mu.Lock()
r.unheard++
r.lastUnheard = subject
count, since := r.unheard, r.startedAt
r.mu.Unlock()
w := r.Findings()
if w == nil {
return
}
if err := unhandledFinding(r.Module, count, subject, since, r.now()).put(w); err != nil {
r.logf("views: the finding %s of %s is not written: %v", EventUnhandledKind, r.Module, err)
}
}
// clearUnhandled clears the module's finding `event-unhandled` at the process's start: a new build may handle the event
// (H-A-169).
func (r *ProcessReader) clearUnhandled() {
if r.Findings == nil {
return
}
w := r.Findings()
if w == nil {
return
}
if err := (viewFinding{kind: EventUnhandledKind, id: r.Module}).drop(w); err != nil {
r.logf("views: the finding %s of %s is not cleared at its start: %v", EventUnhandledKind, r.Module, err)
}
}
@@ -847,7 +895,13 @@ func (r *ProcessReader) Start(ctx context.Context, js jetstream.JetStream, cred
})
hear := r.Hear(ctx)
err := hub.Handle(pattern, hear)
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()
r.startedAt = r.now()
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
+17 -2
View File
@@ -955,8 +955,23 @@ func subViews(t *testing.T) {
t.Fatal(err)
}
m := msg()
if pr.Hear(ctx)(m); len(pfs.m) != 0 || len(pr.Followers()) != 1 || len(*heard["pnext"]) != 1 || !m.acked {
t.Errorf("after the drop: findings %v, followers %d, heard %v, acked %v", pfs.m, len(pr.Followers()), *heard["pnext"], m.acked)
if pr.Hear(ctx)(m); len(pr.Followers()) != 1 || len(*heard["pnext"]) != 1 || !m.acked {
t.Errorf("after the drop: followers %d, heard %v, acked %v", len(pr.Followers()), *heard["pnext"], m.acked)
}
// H-A-166, H-A-169: the event no handler takes, the dropped view's here, is the module's one finding event-unhandled,
// counted since the start, and the next start clears it.
if fd := pfs.next(t); fd.Kind != EventUnhandledKind || fd.ID != "reader" || !strings.Contains(fd.Summary, "took 1 events") ||
!strings.Contains(fd.Summary, m.subject) || len(pfs.m) != 1 {
t.Errorf("after the drop: findings %v, the unhandled event's %+v", pfs.m, fd)
}
pr.Hear(ctx)(&seatMsg{subject: "mesh.seat.s.event.nobody"})
if fd := pfs.next(t); !strings.Contains(fd.Summary, "took 2 events") || !strings.Contains(fd.Summary, "mesh.seat.s.event.nobody") || len(pfs.m) != 1 {
t.Errorf("the second unhandled event: findings %v, %+v", pfs.m, fd)
}
again := &ProcessReader{Module: "reader", Findings: func() WireWriter { return pfs }, Now: now, Hub: process.NewEventHub(t.Logf)}
again.Start(ctx, nil, process.Account{}, nil)
if len(pfs.m) != 0 {
t.Errorf("the start left the finding %v", pfs.m)
}
})
// Design 47 G §717: a view found by its seat starts without a holder, waits naming the seat, follows the holder the