From ae8b0c65bcd18557a6fc6ca22c9310973b41920c Mon Sep 17 00:00:00 2001 From: jochen Date: Fri, 20 Nov 2026 06:35:44 +0100 Subject: [PATCH] NATS-R2: nats_consumer_remove judges durables by every accepted module MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The refusal read the keeper's state asked, which holds only a module that asks for kept data, so a running module keeping no bus data would lose its live durables. It now reads the keeper's state plans, one key per module of a watched branch's newest revision; the keeper no longer deletes a plan key when a module's asked key goes, so plans stays that accepted set. Builds: design 47 H H-A-126, H-A-127, H-A-128; design 47 G §538 Claude-Session: https://claude.ai/code/session_01JG73KHUnBze48rdua8u2sy --- modules/nats/cmd/nats-tools/main.go | 2 +- modules/nats/cmd/nats-tools/removal.go | 25 ++++++++++++++------- modules/nats/cmd/nats-tools/removal_test.go | 2 +- modules/nats/cmd/nats-tools/tools_test.go | 12 ++++++++-- modules/nats/internal/keeper/keeper.go | 5 +++-- modules/nats/internal/keeper/keeper_test.go | 24 ++++++++++++++++++++ 6 files changed, 56 insertions(+), 14 deletions(-) diff --git a/modules/nats/cmd/nats-tools/main.go b/modules/nats/cmd/nats-tools/main.go index c2c000df5..d84305c97 100644 --- a/modules/nats/cmd/nats-tools/main.go +++ b/modules/nats/cmd/nats-tools/main.go @@ -191,7 +191,7 @@ func allTools() []stdio.Tool { defer cancel() return monitor(ctx, heldJsz) } - out := append(tools(monitor, ReadUsers, UsersWritten), removalTools(LiveBus(), SaidByTheRuntime, HeldInState, RecordedBySeat)...) + out := append(tools(monitor, ReadUsers, UsersWritten), removalTools(LiveBus(), SaidByTheRuntime, HeldInState, PlansInState, RecordedBySeat)...) out = append(out, expiryTools(LiveBus(), SaidByTheRuntime, RecordedBySeat)...) out = append(out, brokerTools(HeldInState, StatusInState, jsz, StreamHistory(LiveBus()))...) out = append(out, keeperHealthTool(StatusInState, HeldInState, jsz)) diff --git a/modules/nats/cmd/nats-tools/removal.go b/modules/nats/cmd/nats-tools/removal.go index e179a3e43..c1fd5c86e 100644 --- a/modules/nats/cmd/nats-tools/removal.go +++ b/modules/nats/cmd/nats-tools/removal.go @@ -24,6 +24,8 @@ import ( stdio "git.novox.be/novox/mesh-sdk/go" "github.com/nats-io/nats.go" "github.com/nats-io/nats.go/jetstream" + + "nats-tools/internal/keeper" ) // Bus opens JetStream on the bus as the module's credential for one call; close ends the connection. @@ -341,12 +343,13 @@ func RemoveStream(ctx context.Context, bus Bus, said Said, a map[string]any) (an // that cannot be read refuses too: what cannot be told apart is not removed. // // **A consumer is judged by its name, not by its stream** (design 47 H, H-A-126, H-A-127): the mesh names a module's -// durable _, so a durable of that form for a module of an accepted manifest (the keeper's accepted set) is -// refused, on any stream; every other durable is an orphan and goes, `why` its warrant, at once, and its removal is +// durable _, so a durable of that form for a module of an accepted manifest (the keeper's accepted set: +// every module it heard as accepted, its state plans, one key per module of a watched branch's newest revision whether +// it asks for kept data or not, design 47 G §538) is refused, on any stream; every other durable is an orphan and goes, `why` its warrant, at once, and its removal is // recorded as a hand act through mesh-hand-acts, as nats_bucket_expiry's change is. -func removalTools(bus Bus, said Said, newForm heldEntries, record Recorded) []stdio.Tool { +func removalTools(bus Bus, said Said, newForm heldEntries, accepted acceptedModules, record Recorded) []stdio.Tool { removeConsumer := func(a map[string]any) (any, error) { - if err := refuseAcceptedDurable(newForm, arg(a, "consumer")); err != nil { + if err := refuseAcceptedDurable(accepted, arg(a, "consumer")); err != nil { return nil, err } out, err := RemoveConsumer(context.Background(), bus, said, a) @@ -398,16 +401,19 @@ func removalTools(bus Bus, said Said, newForm heldEntries, record Recorded) []st } // refuseAcceptedDurable refuses a durable named _, or beginning __, for a module of the -// keeper's accepted set, on any node (design 47 H, H-A-127, H-A-128); a set that cannot be read refuses too: what cannot be told apart is not removed. -func refuseAcceptedDurable(newForm heldEntries, consumer string) error { - if newForm == nil { +// keeper's accepted set, on any node (design 47 H, H-A-127, H-A-128): every module it heard as accepted, read from its +// state plans (design 47 G §538), never from its state asked, which holds only a module that asks for kept data (a +// module keeping none would lose its live durables: H-A-128's silent failure, P3). A set that cannot be read refuses +// too: what cannot be told apart is not removed. +func refuseAcceptedDurable(accepted acceptedModules, consumer string) error { + if accepted == nil { return nil } node, module, ok := strings.Cut(consumer, "_") if !ok || node == "" || module == "" { return nil } - held, err := newForm() + held, err := accepted() if err != nil { return fmt.Errorf("whether %s is the durable of a module of an accepted manifest cannot be read (%v), so nothing was removed", consumer, err) } @@ -419,6 +425,9 @@ func refuseAcceptedDurable(newForm heldEntries, consumer string) error { return nil } +// acceptedModules reads the keeper's accepted set: every module it heard as accepted, by name (its state plans, §538). +type acceptedModules func() (map[string]keeper.Planned, error) + // refuseNewForm refuses a stream the keeper made for a module's bus state, naming the tool that removes it. func refuseNewForm(newForm heldEntries, stream string) error { if newForm == nil || stream == "" { diff --git a/modules/nats/cmd/nats-tools/removal_test.go b/modules/nats/cmd/nats-tools/removal_test.go index 3e5d47433..f924f8f02 100644 --- a/modules/nats/cmd/nats-tools/removal_test.go +++ b/modules/nats/cmd/nats-tools/removal_test.go @@ -195,7 +195,7 @@ accounts { MESH { jetstream: enabled, users: [ // Through the tool, `why` its warrant: each removal is recorded as a hand act (design 47 H, H-A-127). var handActs []meshhandactsclient.Request var removeConsumer func(map[string]any) (any, error) - for _, tl := range removalTools(bus, say, nil, func(q meshhandactsclient.Request) error { handActs = append(handActs, q); return nil }) { + for _, tl := range removalTools(bus, say, nil, nil, func(q meshhandactsclient.Request) error { handActs = append(handActs, q); return nil }) { if tl.Name == "nats_consumer_remove" { removeConsumer = tl.Run } diff --git a/modules/nats/cmd/nats-tools/tools_test.go b/modules/nats/cmd/nats-tools/tools_test.go index 053b57655..bc233d76e 100644 --- a/modules/nats/cmd/nats-tools/tools_test.go +++ b/modules/nats/cmd/nats-tools/tools_test.go @@ -234,6 +234,11 @@ func TestTheRemovalToolsRefuseBeforeTheyReachTheBus(t *testing.T) { } return map[string]keeper.Asked{"m": {Module: "m", Made: made}}, nil } + // The keeper's accepted set (its state plans, §538): m, which asks for kept data, and quiet, which asks for none and + // so has no key in asked (review round 2: a module keeping no bus data still has live durables). + accepted := func() (map[string]keeper.Planned, error) { + return map[string]keeper.Planned{"m": {Module: "m"}, "quiet": {Module: "quiet"}}, nil + } for _, c := range []struct { tool string args map[string]any @@ -260,9 +265,12 @@ func TestTheRemovalToolsRefuseBeforeTheyReachTheBus(t *testing.T) { {"nats_consumer_remove", map[string]any{"stream": "m_log", "consumer": "ace_m", "why": "x"}, "a module of an accepted manifest"}, // H-A-128: a durable beginning __ is the module's too. {"nats_consumer_remove", map[string]any{"stream": "EVENTS", "consumer": "novox_m_keeper", "why": "x"}, "a module of an accepted manifest"}, + // H-A-128: an accepted module that asks for no kept data is refused too, its durables and its suffixed ones. + {"nats_consumer_remove", map[string]any{"stream": "EVENTS", "consumer": "novox_quiet", "why": "x"}, "a module of an accepted manifest"}, + {"nats_consumer_remove", map[string]any{"stream": "EVENTS", "consumer": "novox_quiet_tools", "why": "x"}, "a module of an accepted manifest"}, } { var run func(map[string]any) (any, error) - for _, tl := range removalTools(bus, said, newForm, nil) { + for _, tl := range removalTools(bus, said, newForm, accepted, nil) { if tl.Name == c.tool { run = tl.Run } @@ -278,7 +286,7 @@ func TestTheRemovalToolsRefuseBeforeTheyReachTheBus(t *testing.T) { unreachable := func(context.Context) (jetstream.JetStream, func(), error) { return nil, nil, errors.New("the bus is not reached here") } - for _, tl := range removalTools(unreachable, said, newForm, nil) { + for _, tl := range removalTools(unreachable, said, newForm, accepted, nil) { if tl.Name != "nats_consumer_remove" { continue } diff --git a/modules/nats/internal/keeper/keeper.go b/modules/nats/internal/keeper/keeper.go index 68788bed4..b214641cd 100644 --- a/modules/nats/internal/keeper/keeper.go +++ b/modules/nats/internal/keeper/keeper.go @@ -1005,13 +1005,14 @@ func (k *Keeper) asked(module string) (Asked, uint64, bool) { } // write keeps a module's key, or removes it when its manifest asks for nothing and nothing it made is held: a key goes -// only so, never because a module is missing from a first view. +// only so, never because a module is missing from a first view. Its plan key stays: plans is the accepted set, one key +// per module of a watched branch's newest revision, deleted only when no such revision lists it (design 47 G §538; +// H-A-128: nats_consumer_remove judges a module's durables by it). func (k *Keeper) write(a Asked) { if len(a.Entries) == 0 && len(a.Made) == 0 && len(a.Drifts) == 0 { if err := k.Store.Delete(StateAsked, a.Module); err != nil { k.logf("the key of %s cannot be removed from its own %s: %v", a.Module, StateAsked, err) } - _ = k.Store.Delete(StatePlans, a.Module) return } raw, _ := json.Marshal(a) diff --git a/modules/nats/internal/keeper/keeper_test.go b/modules/nats/internal/keeper/keeper_test.go index 500eb3d04..944ed4fc6 100644 --- a/modules/nats/internal/keeper/keeper_test.go +++ b/modules/nats/internal/keeper/keeper_test.go @@ -544,6 +544,30 @@ func TestTheKeeperMakesWhatAVerdictAsksForAndSaysIt(t *testing.T) { t.Errorf("old sections %d: 8 MiB counted", k.Status().OldBytes) } }}, + // §538, H-A-128: plans is the accepted set: a module that asks for no kept data keeps its plan key (no key in + // asked), even when a drift it had clears; the key goes only when no watched branch's newest revision lists it. + {"a module asking for no kept data keeps its plan key while it is listed", func(t *testing.T, k *Keeper, bus *fakeBus, out *said) { + quiet := CatalogModule{Accepted: Accepted{Module: "quiet", Revision: 1}} + k.Catalog = func() (CatalogView, error) { + return CatalogView{Modules: []CatalogModule{quiet}, Drifts: []Drift{{Module: "quiet", Local: "x", Branch: "feature", From: "main"}}}, nil + } + _ = k.PlanCatalog() + k.Catalog = func() (CatalogView, error) { return CatalogView{Modules: []CatalogModule{quiet}}, nil } + _ = k.PlanCatalog() + if _, _, found, _ := k.Store.Get(StatePlans, "quiet"); !found { + t.Errorf("quiet is listed but has no plan key: the accepted set lost a module that keeps no bus data") + } + if _, ok := k.Held()["quiet"]; ok { + t.Errorf("quiet asks for nothing and has a key in asked: %+v", k.Held()["quiet"]) + } + k.Catalog = func() (CatalogView, error) { + return CatalogView{Modules: []CatalogModule{{Accepted: Accepted{Module: "m", Revision: 2}}}}, nil + } + _ = k.PlanCatalog() + if _, _, found, _ := k.Store.Get(StatePlans, "quiet"); found { + t.Errorf("quiet is listed nowhere and keeps its plan key") + } + }}, // A-367: a trunk with no revision yet (none-yet) is not yet viewed: nothing is planned, deleted or orphaned. {"a none-yet catalog half plans nothing and orphans nothing until a module is listed", func(t *testing.T, k *Keeper, bus *fakeBus, out *said) { k.Heard(Accepted{Module: "m", Revision: 9, BusState: []catalogue.BusStateEntry{drafts}})