The tool now reads every watched branch's catalog half through the seat mesh-catalog's verbs registrations and revision, as the pass's world.trunk holds them; the pass table drives p.wants, so it fails without the clean state's call; the manifest test names assigner_assignment_drop. Builds: design 47 H2 H-A-509 (and the lead's answer of review round 1)
153 lines
6.6 KiB
Go
153 lines
6.6 KiB
Go
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, ®s); 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))
|
|
}}}
|
|
}
|