Merge pull request 'The assigner drops a retired module's assignment key: by the tool assigner_assignment_drop and at its pass (design 47 H2 H-A-509)' (#344) from fix/tolerant-state-read into main

This commit was merged in pull request #344.
This commit is contained in:
2026-11-22 23:41:13 +00:00
6 changed files with 373 additions and 2 deletions
@@ -0,0 +1,152 @@
package main
import (
"encoding/json"
"errors"
"fmt"
"maps"
"slices"
mmac "git.novox.be/novox/mesh-contracts/go/clients/meshmoduleassignerclient"
mma "git.novox.be/novox/mesh-contracts/go/seats/meshmoduleassigner"
"git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshhandactsclient"
mcc "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshcatalogclient"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog"
stdio "git.novox.be/novox/mesh-sdk/go"
"git.novox.be/novox/mesh-sdk/go/buscalls"
)
// assignmentDropTool drops one assignment key of a module no manifest names any more by hand (design 47 H2 H-A-509): a
// module key `<node>.<module>` of a retired module, as the clean state of the pass drops it, the act written in the
// hand-act log first.
const assignmentDropTool = "assigner_assignment_drop"
// assignmentLeft is why a module key `<node>.<module>` is not dropped, "" when it may be (H-A-509 and its second
// addendum, and the lead's answer of review round 1): only when no watched branch's catalog half holds the module (trunk, as
// world.trunk is) and the node key does not name it, since the node key is the commit of a write and a hand act never leaves
// one node's keys disagreeing; nothing while no half holds any module.
func assignmentLeft(node, module string, trunk map[string]bool, nodeRaw []byte, nodeFound bool) string {
switch {
case len(trunk) == 0:
return "no watched branch's catalog half holds a module yet, so no module is known to be retired"
case trunk[module]:
return "a watched branch's catalog half holds " + module
case !nodeFound:
return ""
}
n, err := mmac.DecodeNode(nodeRaw)
if err != nil {
return fmt.Sprintf("the node key of %s does not read, so whether it names %s is not known: %v", node, module, err)
}
if slices.ContainsFunc(n.Modules, func(r mma.AssignmentsByNodeNodeValueModulesItem) bool { return r.Module == module }) {
return "the node key of " + node + " still names " + module + ", so it stays placed there"
}
return ""
}
// dropAssignment deletes the module key of module on node, the act first recorded through the seat mesh-hand-acts; trunk
// answers every module a watched branch's catalog half holds.
func dropAssignment(st seatStore, ask func(string, any) (json.RawMessage, error), trunk func() (map[string]bool, error), node, module, why, by string) (any, error) {
switch {
case node == "" || module == "":
return nil, errors.New("name the node and the module whose assignment key is dropped")
case why == "":
return nil, errors.New("an assignment key dropped by hand says why")
case by == "" || by == stdio.Unproven:
return nil, errors.New("an assignment key dropped by hand names who drops it, and the runtime proved no caller")
}
key, err := mmac.ModuleKey(node, module)
if err != nil {
return nil, err
}
nodeKey, err := mmac.NodeKey(node)
if err != nil {
return nil, err
}
raw, _, found, err := st.get(mma.StateAssignments, key)
if err != nil {
return nil, err
}
if !found {
return nil, fmt.Errorf("the assigner holds no assignment key %s", key)
}
modules, err := trunk()
if err != nil {
return nil, fmt.Errorf("the watched branches' modules cannot be read, so %s stays: %w", key, err)
}
nodeRaw, _, nodeFound, err := st.get(mma.StateAssignments, nodeKey)
if err != nil {
return nil, err
}
if left := assignmentLeft(node, module, modules, nodeRaw, nodeFound); left != "" {
return nil, fmt.Errorf("the assignment key %s stays: %s", key, left)
}
taken, err := meshhandactsclient.Recorder{Ask: ask}.Record(meshhandactsclient.Request{What: assignmentDropTool + " " + key, Why: why + " (" + by + ")", Cause: "assignment-drop"})
if err != nil {
return nil, fmt.Errorf("the drop of %s is not recorded, so the key stays: %w", key, err)
}
if err := st.del(mma.StateAssignments, key); err != nil {
return nil, fmt.Errorf("the assignment key %s is not dropped: %w", key, err)
}
return map[string]any{"key": key, "dropped": json.RawMessage(raw), "by": by, "hand-act": taken.Log}, nil
}
// watchedModules is every module any watched branch's catalog half holds, as the pass's world.trunk is (design 47 H2 H-A-509,
// the lead's answer of review round 1): the watched branches of the seat mesh-catalog's registrations, each branch's revision
// read whole through the verb revision, page by page; a branch with none yet holds none.
func watchedModules(call mcc.Call) func() (map[string]bool, error) {
return func() (map[string]bool, error) {
raw, err := call(mcc.RegistrationsVerb, meshcatalog.RegistrationsInput{})
if err != nil {
return nil, err
}
answer, said, ok := buscalls.Unwrap(raw)
if !ok {
return nil, fmt.Errorf("%s answered: %s", mcc.RegistrationsVerb, said)
}
var regs meshcatalog.RegistrationsAnswer
if err := json.Unmarshal(answer, &regs); err != nil || regs.Registrations == nil {
return nil, fmt.Errorf("%s answered no registrations: %v", mcc.RegistrationsVerb, err)
}
branches := map[string]bool{}
for _, reg := range regs.Registrations {
for _, b := range reg.Branches {
branches[b] = true
}
}
out := map[string]bool{}
for _, branch := range slices.Sorted(maps.Keys(branches)) {
for after := ""; ; {
a, err := askCandidatePage(call, meshcatalog.RevisionInput{Branch: branch, After: after})
if err != nil {
return nil, fmt.Errorf("the catalog half of %s cannot be read: %w", branch, err)
}
if a.NoneYet {
break
}
for _, e := range a.Entries {
out[e.Module] = true
}
if a.Next == "" {
break
}
after = a.Next
}
}
return out, nil
}
}
// assignmentDropTools is the tool over the runtime's states.
func assignmentDropTools(st seatStore, ask func(string, any) (json.RawMessage, error), trunk func() (map[string]bool, error)) []stdio.Tool {
return []stdio.Tool{{Name: assignmentDropTool, Description: "Drop one assignment key by hand (design 47 H2 H-A-509): the module " +
"key <node>.<module> of the seat mesh-module-assigner's assignments, for a module no watched branch's catalog half holds any more " +
"(a retired module's key, which may predate the value's version), as the clean state of the pass drops it. Refused while a " +
"watched branch's catalog half holds the module, while the node key <node>.node still names it, or with no key there. Recorded as a " +
"hand act first.",
Input: schema(map[string]string{"node": "the node's name", "module": "the module's name", "why": "why the key is dropped"},
[]string{"node", "module", "why"}),
Run: func(a map[string]any) (any, error) {
return dropAssignment(st, ask, trunk, arg(a, "node"), arg(a, "module"), arg(a, "why"), stdio.CallerOf(a))
}}}
}
@@ -0,0 +1,175 @@
package main
import (
"context"
"encoding/json"
"errors"
"strings"
"testing"
"time"
mmac "git.novox.be/novox/mesh-contracts/go/clients/meshmoduleassignerclient"
mma "git.novox.be/novox/mesh-contracts/go/seats/meshmoduleassigner"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshcatalogclient"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog"
plc "mesh-delivery-assigner/internal/assignment"
)
// H-A-509 and its second addendum: a module key of a module no manifest of the trunk names, that the node key does not name,
// is dropped, by hand through assigner_assignment_drop (recorded as a hand act first) and at the pass's clean state (named in
// the journal); a key a manifest of the trunk still names, or the node key still names, stays; nothing goes while the trunk
// is empty.
func TestARetiredModulesAssignmentKeyIsDropped(t *testing.T) {
// novox places web; the key of the retired checker predates the value's version; the key of the retired old is still
// named by the node key.
values, err := mmac.AssignmentsValues(mma.Assignments{V: mmac.Version, Node: "novox", Modules: []mma.PlacedModule{{Module: "web"}, {Module: "old"}}})
if err != nil {
t.Fatal(err)
}
values["novox.checker"] = []byte(`{"node":"novox"}`)
checker, web, old := "novox.checker", "novox.web", "novox.old"
trunkOf := func(names ...string) map[string]bool {
out := map[string]bool{}
for _, n := range names {
out[n] = true
}
return out
}
copyOf := func() map[string][]byte {
out := map[string][]byte{}
for k, v := range values {
out[k] = v
}
return out
}
t.Run("by hand", func(t *testing.T) {
for _, c := range []struct {
name, module string
trunk map[string]bool
refuse error
dropped bool
}{
{"a retired module's key", "checker", trunkOf("web"), nil, true},
{"a manifest of the trunk names it", "web", trunkOf("web"), nil, false},
{"the node key still names it", "old", trunkOf("web"), nil, false},
{"the trunk is empty", "checker", trunkOf(), nil, false},
{"no key there", "gone", trunkOf("web"), nil, false},
{"not recorded", "checker", trunkOf("web"), errors.New("no responders"), false},
} {
st := memStore{mma.StateAssignments: &memKV{values: copyOf()}}
var asked []string
ask := func(verb string, body any) (json.RawMessage, error) {
raw, _ := json.Marshal(body)
asked = append(asked, verb+" "+string(raw))
return json.RawMessage(`{}`), c.refuse
}
trunk := func() (map[string]bool, error) { return c.trunk, nil }
out, err := dropAssignment(st, ask, trunk, "novox", c.module, "retired", "person.jochen")
key := "novox." + c.module
_, there := st[mma.StateAssignments].values[key]
switch {
case c.dropped && (err != nil || there || len(asked) != 1 || !strings.Contains(asked[0], assignmentDropTool+" "+key)):
t.Errorf("%s: %v, %v; the key left %v, asked %v", c.name, out, err, there, asked)
case !c.dropped && err == nil:
t.Errorf("%s: %v, want it refused", c.name, out)
case len(st[mma.StateAssignments].values) != len(values)-btoi(c.dropped):
t.Errorf("%s: %d keys left, want %d", c.name, len(st[mma.StateAssignments].values), len(values)-btoi(c.dropped))
}
}
if _, err := dropAssignment(memStore{mma.StateAssignments: &memKV{values: copyOf()}}, nil, func() (map[string]bool, error) { return trunkOf("web"), nil },
"novox", "checker", "", "person.jochen"); err == nil {
t.Error("a drop with no why is not refused")
}
})
// The tool judges against every watched branch's half, as the pass does (the lead's answer of review round 1): the branches
// of the registrations, each read page by page; a branch with none yet holds none.
t.Run("every watched branch", func(t *testing.T) {
call := func(verb string, body any) (json.RawMessage, error) {
switch verb {
case meshcatalogclient.RegistrationsVerb:
return json.Marshal(meshcatalog.RegistrationsAnswer{Registrations: []meshcatalog.Registration{{Module: "web", Branches: []string{"main", "feature"}}, {Module: "db", Branches: []string{"fresh"}}}})
case meshcatalogclient.RevisionVerb:
in := body.(meshcatalog.RevisionInput)
switch {
case in.Branch == "main" && in.After == "":
return json.Marshal(meshcatalog.RevisionAnswer{Revision: &meshcatalog.CatalogRevision{}, Entries: []meshcatalog.CatalogEntry{{Module: "web"}}, Next: "web"})
case in.Branch == "main":
return json.Marshal(meshcatalog.RevisionAnswer{Revision: &meshcatalog.CatalogRevision{}, Entries: []meshcatalog.CatalogEntry{{Module: "db"}}})
case in.Branch == "feature":
return json.Marshal(meshcatalog.RevisionAnswer{Revision: &meshcatalog.CatalogRevision{}, Entries: []meshcatalog.CatalogEntry{{Module: "new"}}})
}
return json.Marshal(meshcatalog.RevisionAnswer{NoneYet: true})
}
return nil, errors.New("no such verb " + verb)
}
got, err := watchedModules(call)()
if err != nil || len(got) != 3 || !got["web"] || !got["db"] || !got["new"] {
t.Errorf("watchedModules = %v, %v; want web, db and new", got, err)
}
})
t.Run("at the pass", func(t *testing.T) {
for _, c := range []struct {
name string
trunk []string
gone []string
}{
{"a retired module's key goes, one the node key names stays", []string{"web"}, []string{checker}},
{"the trunk is empty", nil, nil},
} {
kv := &memKV{values: copyOf()}
p := newPasser(kvSet{plc.AssignmentsBucket(): kv}, time.Now())
var said []string
p.logf = func(f string, a ...any) { said = append(said, f) }
for k, v := range kv.values {
p.w.put(plc.AssignmentsBucket(), k, v)
}
// web is held by a watched branch that is not the trunk's first: the pass judges against every watched branch's half.
for _, n := range c.trunk {
mod, _ := meshcatalogclient.EncodeModule(meshcatalog.ModulesByModuleValue{Fp2: fpTest, Repository: "novox/" + n, Commit: strings.Repeat("1", 40), Path: meshcatalogclient.RootPath})
putModuleRaw(p.w, "feature", n, mod)
}
for range 2 {
p.wants(context.Background())
}
for _, key := range []string{checker, web, old, "novox.node"} {
_, there := kv.values[key]
if want := !contains(c.gone, key); there != want {
t.Errorf("%s: %s left = %v, want %v", c.name, key, there, want)
}
}
if dropped, left := countSaid(said, "is dropped"), countSaid(said, "is left"); len(c.gone) > 0 && (dropped != 1 || left != 1) {
t.Errorf("%s: said dropped %d and left %d times, want once each: %v", c.name, dropped, left, said)
}
}
})
}
func btoi(b bool) int {
if b {
return 1
}
return 0
}
func contains(list []string, s string) bool {
for _, x := range list {
if x == s {
return true
}
}
return false
}
func countSaid(said []string, words string) int {
n := 0
for _, s := range said {
if strings.Contains(s, words) {
n++
}
}
return n
}
@@ -6,6 +6,7 @@ import (
"encoding/json"
"errors"
"git.novox.be/novox/mesh-contracts/go/seats/nodeengine"
"maps"
"mesh-delivery-assigner/internal/buckets"
"reflect"
"slices"
@@ -14,6 +15,7 @@ import (
"strings"
"time"
mmac "git.novox.be/novox/mesh-contracts/go/clients/meshmoduleassignerclient"
"git.novox.be/novox/mesh-contracts/go/clients/nodeengineclient"
mma "git.novox.be/novox/mesh-contracts/go/seats/meshmoduleassigner"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-assigner/asks"
@@ -97,6 +99,9 @@ type passer struct {
// saidWaits is the last said of the seat's wait for its states (sayWaits).
saidWaits string
intended map[string]map[string]map[string]bool
// assignmentLeftSaid is, by assignment key, the why last said that the clean state leaves a retired module's key (H-A-509),
// so each is said once per why.
assignmentLeftSaid map[string]string
// laidHere are the proposals whose candidates this process wrote (design 47 G A-317), by id.
laidHere map[string]bool
// reads is the assigner's state engine-reads, what each node's engine said it reads (enginewords.go, design 47 H-A-104);
@@ -222,6 +227,7 @@ func (p *passer) wants(ctx context.Context) {
puts := p.putsByModule()
builds := placementBuilds(p.w.assignments())
p.staleAsks(ctx, trunk)
p.retiredAssignments(ctx, trunk)
names := map[string]bool{}
for name := range trunk {
names[name] = true
@@ -528,6 +534,42 @@ func (p *passer) staleAsks(ctx context.Context, trunk map[string]meshcatalog.Mod
}
}
// retiredAssignments is the clean state of the assignments at the pass (design 47 H2 H-A-509 and its second addendum): each
// module key `<node>.<module>` of a module no manifest the trunk holds names, that the node key does not name, is deleted and
// named in the journal, whether its value reads or not; one the node key still names is left, said once per why. Nothing goes
// while the trunk is empty, or with no assignments bucket open.
func (p *passer) retiredAssignments(ctx context.Context, trunk map[string]meshcatalog.ModulesByModuleValue) {
if len(trunk) == 0 || p.kvs[plc.AssignmentsBucket()] == nil {
return
}
names := make(map[string]bool, len(trunk))
for name := range trunk {
names[name] = true
}
held := p.w.read(plc.AssignmentsBucket())
for _, key := range slices.Sorted(maps.Keys(held)) {
node, module, kind, err := mmac.ParseKey(key)
if err != nil || kind != mmac.ModuleKind || names[module] {
continue
}
nodeKey, _ := mmac.NodeKey(node)
nodeRaw, nodeFound := held[nodeKey]
if left := assignmentLeft(node, module, names, nodeRaw, nodeFound); left != "" {
if p.assignmentLeftSaid[key] != left {
if p.assignmentLeftSaid == nil {
p.assignmentLeftSaid = map[string]string{}
}
p.assignmentLeftSaid[key] = left
p.logf("the assignment key %s is of a module no manifest of the trunk names, and is left: %s", key, left)
}
continue
}
delete(p.assignmentLeftSaid, key)
p.logf("the assignment key %s is of a module no manifest of the trunk names, and is dropped", key)
p.remove(ctx, plc.AssignmentsBucket(), key)
}
}
// remove deletes one key of the assigner's own bucket when it holds it; a delete that fails is said and made again at the next
// pass.
func (p *passer) remove(ctx context.Context, bucket, key string) {
@@ -417,6 +417,7 @@ func serveTools() error {
tools = append(tools, whyTools(runtimeStore{})...)
tools = append(tools, askAgainTools(runtimeStore{}, stdio.Ask, time.Now)...)
tools = append(tools, wantDropTools(runtimeStore{}, stdio.Ask)...)
tools = append(tools, assignmentDropTools(runtimeStore{}, stdio.Ask, watchedModules(stdio.Ask))...) // a retired module's assignment key (H-A-509)
tools = append(tools, asksTools(stdio.QueueWaiting, stdio.State(process.TakenState), stdio.Ask)...) // the seat's work queue (§777)
tools = append(tools, keepNowTools(stdio.Ask)...)
tools = append(tools, validateBeforeTools(runtimeStore{})...) // what a validate step did not count (§814) // the keeping by hand (§813 (2))
@@ -116,8 +116,8 @@ func TestManifest(t *testing.T) {
// which lists and asks again what each node's engine reads.
if !slices.Equal(m.Tools, []string{"assigner_put", "assigner_release", "assigner_puts", "assigner_waits", "assigner_inputs", "assigner_inputs_drop", "assigner_compositions", "assigner_compositions_drop", "assignments", "seat-holders", "wants", "asks",
"assigner_read_assignments", "assigner_read_seat_holders", "assigner_read_wants", "assigner_read_findings", "assigner_read_differences", "assigner_read_copies",
"assigner_first_views", "assigner_first_view_again", "assigner_engine_reads", "assigner_seat_list", "assigner_seat_list_again", "assigner_ask_again", "assigner_written", "assigner_why", "assigner_want_drop", "assigner_asks", "assigner_ask_drop", "assigner_keep_now", "assigner_validate_before"}) {
t.Errorf("the assigner serves tools %v, want its three put tools, three first views, six read tools, assigner_engine_reads, the seat list's two (ADR 0636 §7) assigner_ask_again (ADR 0668 §5) and assigner_why (design 47 G A-439)", m.Tools)
"assigner_first_views", "assigner_first_view_again", "assigner_engine_reads", "assigner_seat_list", "assigner_seat_list_again", "assigner_ask_again", "assigner_written", "assigner_why", "assigner_want_drop", "assigner_assignment_drop", "assigner_asks", "assigner_ask_drop", "assigner_keep_now", "assigner_validate_before"}) {
t.Errorf("the assigner serves tools %v, want its three put tools, three first views, six read tools, assigner_engine_reads, the seat list's two (ADR 0636 §7) assigner_ask_again (ADR 0668 §5), assigner_why (design 47 G A-439) and assigner_assignment_drop after assigner_want_drop (design 47 H2 H-A-509)", m.Tools)
}
if m.RunsAs != m.Module || m.SecretsOwner != m.RunsAs {
t.Errorf("runs-as = %q: the bundle runs on the assigner's own account (ADR 0621 §1)", m.RunsAs)
@@ -134,6 +134,7 @@
"assigner_written",
"assigner_why",
"assigner_want_drop",
"assigner_assignment_drop",
"assigner_asks",
"assigner_ask_drop",
"assigner_keep_now",