mesh-grants: a never-read durable is an orphan; both twins judged where uncomposed

Review round 2: a durable nobody ever read (no pull waiting, no delivery ever) was
named behind when its twin was not current, and the plain durable of a module whose
program left was never judged (live: claude-code's plain durables 1,000 to 3,000
behind beside a current `_tools` twin). A never-read durable is now an orphan
whatever its twin; one read once and lagging stays behind. The pass judges both
twins of every heard entry wherever no pass composes them, also on a stream where
only such durables are held.

Builds: design 47 H2 H-A-472 (eleventh addendum)
This commit is contained in:
2026-11-22 19:54:25 +01:00
parent cc1034630f
commit 82156f8128
2 changed files with 90 additions and 20 deletions
+62 -10
View File
@@ -4230,7 +4230,8 @@ func TestGrants(t *testing.T) {
}
// 1126 (the self-check's bus-consumer probe ended): a consumer the part owns past the bound behind its stream's
// head at two passes running raises mesh-grants.consumer-behind naming it; at the bound or under, it is cleared.
made["pending"] = grants.BehindBound + 1
// Read once, no pull now, far behind: behind (H-A-472 eleventh addendum), never an orphan.
made["pending"], made["last_delivered"] = grants.BehindBound+1, "2026-10-20T21:00:00Z"
if res = p.Compose(); len(res.Consumers.Behind) != 0 {
t.Errorf("one look at a burst was said far behind: %+v", res.Consumers)
}
@@ -4306,19 +4307,70 @@ func TestGrants(t *testing.T) {
if res = p.Compose(); slices.Contains(res.Consumers.Created, "EVENTS.b_mod-t_tools") || slices.Contains(res.Consumers.Created, "EVENTS.b_mod-t") {
t.Errorf("a module with no program of its own: created %v", res.Consumers.Created)
}
// The tenth addendum: a held twin no pass composes is still judged. b_mod-t_tools, read by nobody and with no
// current twin, is behind; b_mod-c_tools, with no pull waiting while the program's b_mod-c is current, is an orphan.
// The tenth and eleventh addenda: every held durable no pass composes is still judged, both twins of a module.
named := func(list []string, key string) bool {
return slices.ContainsFunc(list, func(s string) bool { return s == key || strings.HasPrefix(s, key+":") })
}
lastOrphans := func() string {
if len(orphaned) == 0 || orphaned[len(orphaned)-1].State != occ.StateRaised {
return ""
}
return orphaned[len(orphaned)-1].Explanation
}
// b_mod-c_tools, no pull waiting while the program's b_mod-c is current: an orphan by (2).
twin["waiting_pulls"], twin["pending"] = 0, grants.BehindBound+1
made["waiting_pulls"], made["last_delivered"], made["pending"] = 1, "2026-10-25T21:00:00.5Z", 0
b.consumers["EVENTS"] = append(b.consumers["EVENTS"], map[string]any{"name": "b_mod-t_tools",
"filters": []string{"mesh.mod.mod-y.event.done"}, "max_deliver": 5, "pending": grants.BehindBound + 1})
// b_mod-t_tools, never read (no pull, no delivery ever), mod-t having no program: an orphan, never behind.
tTools := map[string]any{"name": "b_mod-t_tools", "filters": []string{"mesh.mod.mod-y.event.done"}, "max_deliver": 5,
"pending": grants.BehindBound + 1}
b.consumers["EVENTS"] = append(b.consumers["EVENTS"], tTools)
p.Compose()
res = p.Compose()
if !slices.ContainsFunc(res.Consumers.Behind, func(s string) bool { return strings.HasPrefix(s, "EVENTS.b_mod-t_tools:") }) ||
slices.ContainsFunc(res.Consumers.Behind, func(s string) bool { return strings.HasPrefix(s, "EVENTS.b_mod-c_tools:") }) ||
len(orphaned) == 0 || orphaned[len(orphaned)-1].State != occ.StateRaised ||
!strings.Contains(orphaned[len(orphaned)-1].Explanation, "EVENTS.b_mod-c_tools") {
t.Errorf("the held twins: behind %v, orphaned said %+v", res.Consumers.Behind, orphaned)
if named(res.Consumers.Behind, "EVENTS.b_mod-t_tools") || named(res.Consumers.Behind, "EVENTS.b_mod-c_tools") ||
!strings.Contains(lastOrphans(), "EVENTS.b_mod-c_tools") || !strings.Contains(lastOrphans(), "EVENTS.b_mod-t_tools") {
t.Errorf("the held twins: behind %v, orphaned said %q", res.Consumers.Behind, lastOrphans())
}
// Read once, no pull now, far behind, with no current twin: behind.
tTools["last_delivered"] = "2026-10-20T21:00:00Z"
p.Compose()
if res = p.Compose(); !named(res.Consumers.Behind, "EVENTS.b_mod-t_tools") || strings.Contains(lastOrphans(), "EVENTS.b_mod-t_tools") {
t.Errorf("a twin read once and lagging: behind %v, orphaned said %q", res.Consumers.Behind, lastOrphans())
}
// The plain b_mod-t, which no pass composes (mod-t's program left), far behind with no pull beside a current
// b_mod-t_tools: an orphan by (2).
tTools["waiting_pulls"], tTools["last_delivered"], tTools["pending"] = 1, "2026-10-25T21:00:00.5Z", 0
b.consumers["EVENTS"] = append(b.consumers["EVENTS"], map[string]any{"name": "b_mod-t", "filters": []string{"mesh.mod.mod-y.event.done"},
"max_deliver": 5, "pending": grants.BehindBound + 1, "last_delivered": "2026-10-20T21:00:00Z"})
p.Compose()
if res = p.Compose(); named(res.Consumers.Behind, "EVENTS.b_mod-t") || !strings.Contains(lastOrphans(), "EVENTS.b_mod-t,") &&
!strings.HasSuffix(lastOrphans(), "EVENTS.b_mod-t") {
t.Errorf("the plain durable of a module whose program left: behind %v, orphaned said %q", res.Consumers.Behind, lastOrphans())
}
// A stream only twins are held on (no pass composes a consumer there): mod-s keeps the stream feed, which the
// program-less mod-u hears; its never-read twin there is judged and named.
b.manifestOf("mod-s", `{"module":"mod-s","version":"1","emits":["fed"],"streams":[{"name":"feed","events":["fed"],"max-age-days":2,"max-mib":5}]}`)
b.manifestOf("mod-u", `{"module":"mod-u","version":"1","own-secrets":{"broker":"/var/lib/u/broker"},"consumes":["mod-s.fed"]}`)
b.assign("b", "mod-s")
b.assign("b", "mod-u")
p.Compose()
var feed string
raw, _, _, _ := b.ownGet(grants.StateMemberships, "b.mod-u")
if bare, err := membership.BareGrantsMembership(raw); err == nil {
var m membership.Membership
_ = json.Unmarshal(bare, &m)
for _, h := range m.Hears {
if h.Stream != "EVENTS" {
feed = h.Stream
}
}
}
if feed == "" {
t.Fatalf("mod-u's membership hears no stream of mod-s: %s", raw)
}
b.consumers[feed] = []map[string]any{{"name": "b_mod-u_tools", "filters": []string{"x"}, "max_deliver": 5, "pending": grants.BehindBound + 1}}
p.Compose()
if res = p.Compose(); !strings.Contains(lastOrphans(), feed+".b_mod-u_tools") {
t.Errorf("a twin on a stream no pass composes a consumer on: orphaned said %q (unread %v)", lastOrphans(), res.Consumers.Unread)
}
})
@@ -118,7 +118,8 @@ func durableTwin(name string) string {
return name + "_tools"
}
// orphaned says whether a held durable is an orphan (design 47 H2, H-A-472 (2)): no pulls waiting on it, while its twin
// orphaned says whether a held durable is an orphan (design 47 H2, H-A-472 (2), eleventh addendum): never read at all, or
// no pulls waiting on it, while its twin
// on the same stream is current, with pulls waiting and a delivery within the bound (the setting twin-current-hours,
// the third addendum). A module that moved into the tool runner left its program's durable so, and one that runs its own
// program again its tool runner's.
@@ -126,6 +127,11 @@ func orphaned(h heldConsumer, held map[string]heldConsumer, now time.Time, withi
if h.WaitingPulls > 0 {
return false
}
// A durable nobody ever read, no pull waiting and no delivery ever, is an orphan whatever its twin's state; one that was
// read and lags is behind (H-A-472 eleventh addendum).
if neverDelivered(h) {
return true
}
t, ok := held[durableTwin(h.Name)]
if !ok || t.WaitingPulls == 0 {
return false
@@ -134,6 +140,15 @@ func orphaned(h heldConsumer, held map[string]heldConsumer, now time.Time, withi
return err == nil && now.Sub(last) < within
}
// neverDelivered says the bus never delivered from the durable: no time of a last delivery, or the zero time.
func neverDelivered(h heldConsumer) bool {
if h.LastDelivered == "" {
return true
}
last, err := time.Parse(time.RFC3339Nano, h.LastDelivered)
return err == nil && last.IsZero()
}
// heldConsumers reads the consumers the bus holds on stream, by name.
func heldConsumers(c Caller, stream string) (map[string]heldConsumer, error) {
raw, err := c.Call(CallStreams, map[string]any{"stream": stream})
@@ -161,16 +176,18 @@ func heldConsumers(c Caller, stream string) (map[string]heldConsumer, error) {
return out, nil
}
// toolsTwinsOf is, by stream, the tool runner's twin `<consumer>_tools` of every heard entry of every assigned module's
// membership (design 47 H2, H-A-472 tenth addendum): held or not, each is judged by the behind check.
func toolsTwinsOf(recs buscompose.Records) map[string][]string {
// heardDurablesOf is, by stream, both twins of every heard entry of every assigned module's membership, the plain
// `<consumer>` and the tool runner's `<consumer>_tools` (design 47 H2, H-A-472 tenth and eleventh addenda): each one held
// and composed by no pass is judged by the behind check.
func heardDurablesOf(recs buscompose.Records) map[string][]string {
out := map[string][]string{}
for _, node := range sortedKeys(recs.Assigned) {
for _, d := range recs.Assigned[node] {
for _, h := range buscompose.HeardIssuedFor(node, d) {
name := h.Consumer + buscompose.ToolsSuffix
if !slices.Contains(out[h.Stream], name) {
out[h.Stream] = append(out[h.Stream], name)
for _, name := range []string{h.Consumer, h.Consumer + buscompose.ToolsSuffix} {
if !slices.Contains(out[h.Stream], name) {
out[h.Stream] = append(out[h.Stream], name)
}
}
}
}
@@ -217,9 +234,10 @@ func (p *Part) consumers(res *Result, recs buscompose.Records, match string) {
for _, mc := range wanted {
byStream[mc.Consumer.Stream] = append(byStream[mc.Consumer.Stream], mc.Consumer)
}
// The tool runner's twins it makes itself from the membership's spec are composed by no pass, and still judged here, held
// on the streams each consuming module's heard entries name (design 47 H2, H-A-472 tenth addendum).
twins := toolsTwinsOf(recs)
// The durables no pass composes, the tool runner's twins it makes itself from the membership's spec and the plain one of a
// module whose program left, are still judged here, held on the streams each consuming module's heard entries name
// (design 47 H2, H-A-472 tenth and eleventh addenda).
twins := heardDurablesOf(recs)
for stream := range twins {
if _, composed := byStream[stream]; !composed {
byStream[stream] = nil