DC2 branches-adopt-before-start: NewBranches adopts the catalog-revision event before the reader's Start

On a process reader NewBranches registers the seat mesh-catalog's catalog-revision event
(AdoptEvent) before the reader's Start, so a branch first followed after Start adopts its
view on a subscription the hub already holds and no first adopt of the event comes later;
the hub refuses a pattern first registered after Start. A row in the branch follower's
test starts a refusing hub before the branches come.

Builds: design 47 H2b §836 3., H-A-854, H-A-861
This commit is contained in:
2026-11-27 02:56:51 +01:00
parent d36511022f
commit cecf4142e6
2 changed files with 48 additions and 1 deletions
@@ -48,6 +48,12 @@ func (w processWiring) Adopt(f *views.Follower, seat, event string, hear func(co
}
func (w processWiring) Drop(owner, state string) error { return w.r.Drop(owner, state) }
// AdoptEvent registers a seat's event on the process reader before its Start, with a handler that has nothing to do: the
// event's one subscription is made at Start and every follower adopted on it later shares it (design 47 H2b §836 3.).
func (w processWiring) AdoptEvent(seat, event string, hear func(context.Context, []byte) error) error {
return w.r.AdoptEvent(seat, event, hear)
}
// ReaderWiring wires Branches on a bundle's reader.
func ReaderWiring(r *views.Reader) Wiring { return readerWiring{r} }
@@ -100,6 +106,16 @@ func NewBranches(w Wiring, call Call) (*Branches, error) {
b.Registrations.Follower.Changed = func(string, json.RawMessage, bool, time.Time) { b.reconcile() }
b.source = sourcebranches.New(sourcebranches.Call(call))
b.source.Follower.Changed = func(string, json.RawMessage, bool, time.Time) { b.reconcile() }
// The event of a branch's view is registered here, before the reader's Start: the hub refuses a pattern first
// registered after Start (design 47 H2b §836 3.; H-A-854, H-A-861), and a branch first followed later adopts its view on
// this one subscription (a wiring that has no hub to refuse, a bundle's reader, subscribes it at its own Start).
if ew, ok := w.(interface {
AdoptEvent(seat, event string, hear func(context.Context, []byte) error) error
}); ok {
if err := ew.AdoptEvent(seat.Seat, seat.EventCatalogRevision, func(context.Context, []byte) error { return nil }); err != nil {
return nil, err
}
}
if err := w.Adopt(b.Registrations.Follower, seat.Seat, seat.EventRegistrationChanged, b.hearRegistration); err != nil {
return nil, err
}
@@ -28,6 +28,29 @@ type fakeWiring struct {
follows []string
values map[string]map[string]json.RawMessage
followed chan string
// events are the seat events adopted, by `<seat>.<event>`; started says the hub has started: it refuses an event
// adopted for the first time after that (design 47 H2b §836 3.).
events map[string]bool
started bool
}
// AdoptEvent is the process reader's AdoptEvent: the event is heard on the one subscription the followers on it share.
func (w *fakeWiring) AdoptEvent(seat, event string, _ func(context.Context, []byte) error) error {
w.mu.Lock()
defer w.mu.Unlock()
return w.eventLocked(seat, event)
}
func (w *fakeWiring) eventLocked(seat, event string) error {
name := seat + "." + event
if w.events == nil {
w.events = map[string]bool{}
}
if !w.events[name] && w.started {
return fmt.Errorf("the pattern %s is registered after Start", name)
}
w.events[name] = true
return nil
}
func (w *fakeWiring) Follow(owner, state, tool string) (func() map[string]json.RawMessage, error) {
@@ -46,9 +69,12 @@ func (w *fakeWiring) Follow(owner, state, tool string) (func() map[string]json.R
}, nil
}
func (w *fakeWiring) Adopt(f *views.Follower, _, _ string, hear func(context.Context, []byte) error) error {
func (w *fakeWiring) Adopt(f *views.Follower, seat, event string, hear func(context.Context, []byte) error) error {
w.mu.Lock()
defer w.mu.Unlock()
if err := w.eventLocked(seat, event); err != nil {
return err
}
name := f.Owner + "." + f.State
if w.adopted[name] != nil {
return fmt.Errorf("%s is followed already", name)
@@ -164,6 +190,11 @@ func TestBranchesFollowSourceThenRegistrations(t *testing.T) {
t.Fatal(err)
}
b.Logf = func(format string, args ...any) { said <- fmt.Sprintf(format, args...) }
// design 47 H2b §836 3. (H-A-854, H-A-861): the hub is started now and refuses a pattern first registered after it,
// so a branch followed from here on hears its catalog-revision only if NewBranches adopted the event before.
w.mu.Lock()
w.started = true
w.mu.Unlock()
adopted := make(chan string, 8)
b.Adopted = func(branch string, c *Catalog) {
if c.Branch == branch {