diff --git a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/wantdrop_tool.go b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/wantdrop_tool.go index ae447344..44bf1a93 100644 --- a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/wantdrop_tool.go +++ b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/wantdrop_tool.go @@ -4,6 +4,8 @@ import ( "encoding/json" "errors" "fmt" + "slices" + "strings" "git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshhandactsclient" mwc "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshwantsclient" @@ -49,14 +51,96 @@ func dropWant(st seatStore, ask func(string, any) (json.RawMessage, error), node return map[string]any{"key": key, "dropped": json.RawMessage(raw), "by": by, "hand-act": taken.Log}, nil } +// The kinds the all form counts what it dropped by (design 47 H-A-123). +const ( + dropHeld = "held" + dropCurrent = "current" + dropUndecodable = "undecodable" +) + +// dropAllWants deletes every want key of the state wants, held or not, and every value of an earlier form the decoder refuses +// (the all form, design 47 H-A-121 (2), H-A-123): each act recorded through the seat mesh-hand-acts first, as the one-item form +// does; it answers what it dropped, counted per kind. A drop not recorded stops it, saying what went before. +func dropAllWants(st seatStore, ask func(string, any) (json.RawMessage, error), why, by string) (any, error) { + switch { + case why == "": + return nil, errors.New("a want dropped by hand says why") + case by == "" || by == stdio.Unproven: + return nil, errors.New("a want dropped by hand names who drops it, and the runtime proved no caller") + } + all, err := st.keys(wantsState) + if err != nil { + return nil, err + } + slices.Sort(all) + counted := map[string]int{dropHeld: 0, dropCurrent: 0, dropUndecodable: 0} + dropped := map[string][]string{} + answer := func() map[string]any { + return map[string]any{"dropped": dropped, "counted": counted, "by": by} + } + for _, key := range all { + if !strings.HasPrefix(key, "want.") { + continue + } + raw, _, found, err := st.get(wantsState, key) + if err != nil { + return answer(), fmt.Errorf("the want %s cannot be read, so the drop stops there: %w", key, err) + } + if !found { + continue + } + kind := wantKind(key, raw) + taken, err := meshhandactsclient.Recorder{Ask: ask}.Record(meshhandactsclient.Request{What: wantDropTool + " " + key, Why: why + " (" + by + ")", Cause: "want-drop"}) + if err != nil { + return answer(), fmt.Errorf("the drop of %s is not recorded, so it and the wants after it stay: %w", key, err) + } + if err := st.del(wantsState, key); err != nil { + return answer(), fmt.Errorf("the want %s is not dropped: %w", key, err) + } + counted[kind]++ + dropped[kind] = append(dropped[kind], key+" (hand act "+taken.Log+")") + } + return answer(), nil +} + +// wantKind says how the all form counts a want: undecodable when its key or value is of no form the decoder takes, held when it +// holds, current otherwise (design 47 H-A-123). +func wantKind(key string, raw []byte) string { + if _, err := mwc.ParseWantNodeKey(key); err == nil { + if _, err := mwc.DecodeNodeWant(raw); err != nil { + return dropUndecodable + } + return dropCurrent + } + if _, _, err := mwc.ParseWantKey(key); err != nil { + return dropUndecodable + } + w, err := mwc.DecodeWant(raw) + switch { + case err != nil: + return dropUndecodable + case w.Hold: + return dropHeld + } + return dropCurrent +} + // wantDropTools is the tool over the runtime's states. func wantDropTools(st seatStore, ask func(string, any) (json.RawMessage, error)) []stdio.Tool { return []stdio.Tool{{Name: wantDropTool, Description: "Drop one want by hand (design 47 H ยง765): the want of module on node is " + "deleted from the state wants, as the clean state drops a held or failed want of the earlier model; the next pass writes the " + - "want of the new form. Recorded as a hand act first; refused with no want under the key.", - Input: schema(map[string]string{"node": "the node's name", "module": "the module's name", "why": "why the want is dropped"}, - []string{"node", "module", "why"}), + "want of the new form. Recorded as a hand act first; refused with no want under the key. With all true and no node or module, " + + "every want key goes, held or not, and every value of an earlier form the decoder refuses, each recorded as a hand act; it " + + "answers what it dropped, counted per kind (held, current, undecodable).", + Input: schema(map[string]string{"node": "the node's name", "module": "the module's name", "why": "why the want is dropped", + "all": "true drops every want of the state wants, naming no node or module"}, []string{"why"}, "all"), Run: func(a map[string]any) (any, error) { + if arg(a, "all") == "true" { + if arg(a, "node") != "" || arg(a, "module") != "" { + return nil, errors.New("the all form drops every want: name no node or module with it") + } + return dropAllWants(st, ask, arg(a, "why"), stdio.CallerOf(a)) + } return dropWant(st, ask, arg(a, "node"), arg(a, "module"), arg(a, "why"), stdio.CallerOf(a)) }}} } diff --git a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/wantdrop_tool_test.go b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/wantdrop_tool_test.go index bd3fd0c0..fefdab08 100644 --- a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/wantdrop_tool_test.go +++ b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/wantdrop_tool_test.go @@ -43,3 +43,57 @@ func TestAWantIsDroppedByHandAndRecorded(t *testing.T) { } } } + +// H-A-121 (2), H-A-123: assigner_want_drop's all form drops every want key, held or not, and every value of an earlier form, +// each recorded as a hand act first, answering the count per kind; a held fingerprint's key stays; a drop not recorded stops it; +// the all form names no node or module. +func TestEveryWantIsDroppedByHandAndCounted(t *testing.T) { + heldKey, _ := mwc.WantKey("ace", "web") + curKey, _ := mwc.WantKey("ace", "db") + nodeKey, _ := mwc.WantNodeKey("ace") + oldKey, _ := mwc.WantKey("g14", "web") + fpKey, _ := mwc.HeldKey("web", fpTest) + held, _ := mwc.EncodeWant(mwc.Want{Fp: fpTest, Put: true, Hold: true, Words: "put: x"}) + cur, _ := mwc.EncodeWant(mwc.Want{Fp: fpTest}) + node, _ := mwc.EncodeNodeWant(mwc.NodeWant{Self: true}) + values := func() map[string][]byte { + return map[string][]byte{heldKey: held, curKey: cur, nodeKey: node, oldKey: []byte(`{"fingerprint":"old"}`), fpKey: []byte(`{}`)} + } + for _, c := range []struct { + name string + refuse error + left int + }{ + {"dropped", nil, 1}, + {"not recorded", errors.New("no responders"), 5}, + } { + st := memStore{wantsState: &memKV{values: values()}} + var asked []string + ask := func(verb string, body any) (json.RawMessage, error) { + raw, _ := json.Marshal(body) + asked = append(asked, string(raw)) + return json.RawMessage(`{}`), c.refuse + } + out, err := dropAllWants(st, ask, "the clean state", "person.jochen") + if left := len(st[wantsState].values); left != c.left || (c.refuse == nil) != (err == nil) { + t.Errorf("%s: %v, %v; %d keys left, want %d", c.name, out, err, left, c.left) + } + if c.refuse != nil { + continue + } + if _, ok := st[wantsState].values[fpKey]; !ok { + t.Errorf("the held fingerprint's key %s is dropped: it is no want", fpKey) + } + counted := out.(map[string]any)["counted"].(map[string]int) + if counted[dropHeld] != 1 || counted[dropCurrent] != 2 || counted[dropUndecodable] != 1 || len(asked) != 4 { + t.Errorf("counted %v, asked %d hand acts; want held 1, current 2, undecodable 1 and 4 acts", counted, len(asked)) + } + } + if _, err := dropAllWants(memStore{wantsState: &memKV{values: values()}}, nil, "", "person.jochen"); err == nil { + t.Error("the all form dropped with no why") + } + run := wantDropTools(memStore{wantsState: &memKV{values: values()}}, nil)[0].Run + if _, err := run(map[string]any{"all": "true", "node": "ace", "why": "x"}); err == nil || !strings.Contains(err.Error(), "name no node") { + t.Errorf("the all form with a node: %v, want it refused", err) + } +}