indexer: indexer_pin_drop's all form drops every pin for the clean state
Review round 1, H-A-121 (2): beside the one-item form, all true drops every pin of the state pins, each recorded through mesh-hand-acts and asked of the indexer as the one-item form is; it answers the count dropped and the pipelines the pins named. Builds: design 47 H §765, H-A-121, H-A-124 Claude-Session: https://claude.ai/code/session_01JG73KHUnBze48rdua8u2sy
This commit is contained in:
@@ -2,6 +2,8 @@ package index
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"slices"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -71,3 +73,50 @@ func TestTheHeldAndPinToolsAskWhatTheIndexerReads(t *testing.T) {
|
||||
t.Errorf("since %q is not RFC 3339: %v", answer.Held[0].Since, err)
|
||||
}
|
||||
}
|
||||
|
||||
// Design 47 H-A-121 (2), H-A-124: indexer_pin_drop's all form drops every pin of the state pins, each recorded in the hand-act
|
||||
// log and asked as the one-item form is, and answers the count and the pipelines they named; it names no key, says why, and stops
|
||||
// at a drop not recorded.
|
||||
func TestEveryPinIsDroppedByHandAndCounted(t *testing.T) {
|
||||
pins := newMem()
|
||||
for k, v := range map[string]string{"main.7": `{"records":["pipeline.main.4","take-in.main.4"]}`, "main.8": `{"records":["pipeline.main.5"]}`, "dev.2": `{"records":[]}`} {
|
||||
_ = pins.Create(t.Context(), k, []byte(v))
|
||||
}
|
||||
var asked []string
|
||||
ask := func(_ string, value []byte) (TakeInAsk, error) {
|
||||
a, err := DecodeTakeIn(value)
|
||||
if err != nil {
|
||||
t.Errorf("the indexer cannot read the ask %s: %v", value, err)
|
||||
}
|
||||
asked = append(asked, a.PinDrop)
|
||||
a.Result = &TakenIn{Stored: true}
|
||||
return a, nil
|
||||
}
|
||||
for _, c := range []struct {
|
||||
name string
|
||||
args map[string]any
|
||||
refuse error
|
||||
dropped int
|
||||
failed bool
|
||||
}{
|
||||
{"all", map[string]any{"all": true, "why": "the clean state"}, nil, 3, false},
|
||||
{"all with a key", map[string]any{"all": true, "key": "main.7", "why": "x"}, nil, 0, true},
|
||||
{"no why", map[string]any{"all": "true"}, nil, 0, true},
|
||||
{"not recorded", map[string]any{"all": true, "why": "x"}, errors.New("no responders"), 0, true},
|
||||
} {
|
||||
asked = nil
|
||||
var recorded int
|
||||
record := func(string, string) error { recorded++; return c.refuse }
|
||||
got, err := PinsTools(pins, newMemObjects(0), ask, time.Now, record)[1].Run(c.args)
|
||||
if c.failed != (err != nil) || len(asked) != c.dropped {
|
||||
t.Fatalf("%s: %+v (%v), asked %v", c.name, got, err, asked)
|
||||
}
|
||||
if c.failed {
|
||||
continue
|
||||
}
|
||||
d := got.(PinsDropped)
|
||||
if d.Dropped != 3 || recorded != 3 || !slices.Equal(d.Pins, []string{"dev.2", "main.7", "main.8"}) || !slices.Equal(d.Pipelines, []string{"main.4", "main.5"}) {
|
||||
t.Errorf("%s answered %+v with %d hand acts, want 3 pins, 3 acts and the pipelines main.4 and main.5", c.name, d, recorded)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -407,13 +407,25 @@ func PinsTools(pins Store, o Objects, ask func(key string, value []byte) (TakeIn
|
||||
{Name: PinDropTool,
|
||||
Description: "Drop one pin of the indexer's state pins by its key `<branch>.<revision>`, every record's, for a pin that names no " +
|
||||
"running pipeline (design 47 H §765); the removal rule then applies (a revision no record pins and not current goes). A hand " +
|
||||
"act, recorded through mesh-hand-acts before it is asked. Clears the finding pin-missing of the key. indexer_pins names the keys.",
|
||||
Input: map[string]any{"type": "object", "required": []string{"key", "why"}, "properties": map[string]any{
|
||||
"act, recorded through mesh-hand-acts before it is asked. Clears the finding pin-missing of the key. indexer_pins names the keys. " +
|
||||
"With all true and no key, every pin of the state goes, each recorded and asked as one: the clean state's tool, run while no " +
|
||||
"pipeline runs (design 47 H-A-121, H-A-124); it answers the count dropped and the pipelines they named.",
|
||||
Input: map[string]any{"type": "object", "required": []string{"why"}, "properties": map[string]any{
|
||||
"key": map[string]any{"type": "string", "description": "the pin's key `<branch>.<revision>`, as indexer_pins names it"},
|
||||
"all": map[string]any{"type": "boolean", "description": "true drops every pin of the state pins, naming no key"},
|
||||
"why": map[string]any{"type": "string", "description": "why it is dropped, in plain words"}}},
|
||||
Run: func(args map[string]any) (any, error) {
|
||||
str := func(k string) string { s, _ := args[k].(string); return strings.TrimSpace(s) }
|
||||
key, why := str("key"), str("why")
|
||||
if all, _ := args["all"].(bool); all || str("all") == "true" {
|
||||
if key != "" {
|
||||
return nil, errors.New("the all form drops every pin: name no key with it")
|
||||
}
|
||||
if why == "" {
|
||||
return nil, errors.New("say why (why): a drop is a hand act, recorded through mesh-hand-acts")
|
||||
}
|
||||
return dropAllPins(context.Background(), pins, ask, now, record, why, stdio.CallerOf(args))
|
||||
}
|
||||
if _, _, ok := ParsePinKey(key); !ok || why == "" {
|
||||
return nil, errors.New("name the pin's key as indexer_pins names it (key) and say why (why): a drop is a hand act, recorded through mesh-hand-acts")
|
||||
}
|
||||
@@ -432,3 +444,64 @@ func PinsTools(pins Store, o Objects, ask func(key string, value []byte) (TakeIn
|
||||
}},
|
||||
}
|
||||
}
|
||||
|
||||
// PinsDropped is what indexer_pin_drop's all form answers: the pins dropped, counted, and the pipelines they named (design 47
|
||||
// H-A-124).
|
||||
type PinsDropped struct {
|
||||
Dropped int `json:"dropped"`
|
||||
Pins []string `json:"pins"`
|
||||
Pipelines []string `json:"pipelines"`
|
||||
}
|
||||
|
||||
// dropAllPins drops every pin of the state pins (indexer_pin_drop all, design 47 H-A-121 (2), H-A-124), each as the one-item
|
||||
// form does: recorded through mesh-hand-acts, then asked of the indexer, the state's owner. A drop not recorded or not asked
|
||||
// stops it, saying what went before.
|
||||
func dropAllPins(ctx context.Context, pins Store, ask func(key string, value []byte) (TakeInAsk, error), now func() time.Time,
|
||||
record func(what, why string) error, why, by string) (PinsDropped, error) {
|
||||
out := PinsDropped{Pins: []string{}, Pipelines: []string{}}
|
||||
all, err := pins.Keys(ctx)
|
||||
if err != nil {
|
||||
return out, fmt.Errorf("the state %s cannot be read: %w", PinsState, err)
|
||||
}
|
||||
slices.Sort(all)
|
||||
for _, key := range all {
|
||||
if _, _, ok := ParsePinKey(key); !ok {
|
||||
continue
|
||||
}
|
||||
v, _, found, err := pins.Get(ctx, key)
|
||||
if err != nil {
|
||||
return out, fmt.Errorf("the pin %s cannot be read, so the drop stops there: %w", key, err)
|
||||
}
|
||||
if !found {
|
||||
continue
|
||||
}
|
||||
var pv PinValue
|
||||
_ = json.Unmarshal(v, &pv)
|
||||
data, err := json.Marshal(TakeInAsk{V: 1, PinDrop: key, Why: why, By: by, At: now().UTC()})
|
||||
if err != nil {
|
||||
return out, err
|
||||
}
|
||||
if err := record("dropped the pin "+key+" of the catalog graph revisions by hand", why); err != nil {
|
||||
return out, fmt.Errorf("the hand act was not recorded through mesh-hand-acts, so %s and the pins after it stay: %w", key, err)
|
||||
}
|
||||
a, err := ask("pin-drop."+keys.EscapeName(key), data)
|
||||
if err != nil {
|
||||
return out, fmt.Errorf("the drop of %s was not asked: %w", key, err)
|
||||
}
|
||||
if a.Result == nil {
|
||||
return out, fmt.Errorf("the drop of %s was asked and not answered", key)
|
||||
}
|
||||
if !a.Result.Stored {
|
||||
return out, fmt.Errorf("the pin %s is not dropped: %s", key, a.Result.Answer)
|
||||
}
|
||||
out.Dropped++
|
||||
out.Pins = append(out.Pins, key)
|
||||
for _, r := range pv.Records {
|
||||
if p, ok := strings.CutPrefix(r, "pipeline."); ok && !slices.Contains(out.Pipelines, p) {
|
||||
out.Pipelines = append(out.Pipelines, p)
|
||||
}
|
||||
}
|
||||
}
|
||||
sort.Strings(out.Pipelines)
|
||||
return out, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user