From 5c7648744d7a36971e7765427008c8516bf729e5 Mon Sep 17 00:00:00 2001 From: jochen Date: Sat, 21 Nov 2026 01:03:32 +0100 Subject: [PATCH] Indexer: read a takeins value's hand acts from TakeInAsk's tags, not a hand-kept list MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit indexer_take_in_again wrote TakeInAsk{Again} and DecodeTakeIn refused it: its hand-kept list of act fields lacked Again, so the handler never reached its Again branch. Every field of TakeInAsk now says by its tag takein whether it is an act, an argument or of the ask; DecodeTakeIn reads the acts from the tags, and a table test builds every hand tool's value by its own Run path, decodes it and acts on it through takenIn, and refuses a field left unclassed. Builds: design 47 H §774, H-A-208, H-A-210 --- .../cmd/mesh-delivery-indexer/takein_test.go | 147 ++++++++++++++++++ .../internal/index/takein.go | 78 +++++++--- 2 files changed, 201 insertions(+), 24 deletions(-) create mode 100644 modules/mesh-delivery-indexer/cmd/mesh-delivery-indexer/takein_test.go diff --git a/modules/mesh-delivery-indexer/cmd/mesh-delivery-indexer/takein_test.go b/modules/mesh-delivery-indexer/cmd/mesh-delivery-indexer/takein_test.go new file mode 100644 index 00000000..713eb631 --- /dev/null +++ b/modules/mesh-delivery-indexer/cmd/mesh-delivery-indexer/takein_test.go @@ -0,0 +1,147 @@ +package main + +import ( + "context" + "encoding/json" + "reflect" + "slices" + "strings" + "testing" + "time" + + "github.com/nats-io/nats.go/jetstream" + + "mesh-delivery-indexer/internal/index" +) + +// askedBy is a takerIn that names the hand act it was asked. +type askedBy struct{ asked []string } + +func (s *askedBy) did(act string) index.TakenIn { + s.asked = append(s.asked, act) + return index.TakenIn{Module: "m", Stored: true, Answer: act} +} +func (s *askedBy) SeatDefinitionsAgain(context.Context, string) index.TakenIn { + return s.did("seat-definitions") +} +func (s *askedBy) FingerprintsAgain(context.Context, string) index.TakenIn { + return s.did("fingerprints") +} +func (s *askedBy) RecomputeByHand(context.Context, string, string) index.TakenIn { + return s.did("recompute") +} +func (s *askedBy) DropCandidateGraph(context.Context, string, string, string, string) index.TakenIn { + return s.did("candidate-group") +} +func (s *askedBy) NamedDrop(context.Context, string, string, string, string) index.TakenIn { + return s.did("named-fingerprint") +} +func (s *askedBy) PinDrop(context.Context, string, string, string) index.TakenIn { + return s.did("pin-drop") +} +func (s *askedBy) DropRunByHand(context.Context, string, string, string) index.TakenIn { + return s.did("run-drop") +} +func (s *askedBy) RegistryAct(context.Context, index.TakeInAsk) index.TakenIn { + return s.did("registry") +} +func (s *askedBy) HeldAgain(context.Context, string) index.TakenIn { return s.did("held") } +func (s *askedBy) HeldSkip(context.Context, string, int64, string, string) index.TakenIn { + return s.did("held-skip") +} +func (s *askedBy) TakeInAgain(context.Context, string) index.TakenIn { return s.did("again") } + +// answeredKV keeps the answer takenIn writes back. +type answeredKV struct { + jetstream.KeyValue + written []byte +} + +func (k *answeredKV) Update(_ context.Context, _ string, value []byte, _ uint64) (uint64, error) { + k.written = value + return 2, nil +} + +type askEntry struct { + jetstream.KeyValueEntry + key string + value []byte +} + +func (e askEntry) Key() string { return e.key } +func (e askEntry) Value() []byte { return e.value } +func (e askEntry) Revision() uint64 { return 1 } +func (e askEntry) Operation() jetstream.KeyValueOp { return jetstream.KeyValuePut } + +// Design 47 H-A-208, H-A-210, H §774: every hand tool's takeins value, built by the tool's own Run path (or the constructor it +// calls), is read by DecodeTakeIn and acted on by takenIn as the act it names; indexer_take_in_again's was refused, its Again +// missing from a hand-kept list. Every field of TakeInAsk says by its tag whether it is an act, so an act added is read. +func TestEveryHandToolsAskIsReadAndActedOn(t *testing.T) { + t.Run("every field is classed", func(t *testing.T) { + ty := reflect.TypeFor[index.TakeInAsk]() + for i := range ty.NumField() { + if c := ty.Field(i).Tag.Get("takein"); !slices.Contains([]string{"act", "argument", "ask"}, c) { + t.Errorf("TakeInAsk.%s has takein %q, want act, argument or ask", ty.Field(i).Name, c) + } + } + }) + var values [][]byte + ask := func(_ string, value []byte) (index.TakeInAsk, error) { + values = append(values, value) + return index.TakeInAsk{V: 1, Result: &index.TakenIn{Stored: true}, Answered: time.Now()}, nil + } + record := func(string, string) error { return nil } + tools := map[string]func(map[string]any) (any, error){} + for _, tl := range slices.Concat(index.HeldTools(nil, ask, time.Now, record), index.PinsTools(nil, nil, ask, time.Now, record), + index.RunsTools(nil, nil, ask, time.Now, record), index.NamedTools(ask, time.Now, record), + index.FingerprintsTools(ask, time.Now, record, time.Minute), index.CatalogRevisionTools(nil, ask, time.Now, record), + index.RegistrationTools(nil, nil, ask, time.Now, func(string, string, string, string) error { return nil })) { + tools[tl.Name] = tl.Run + } + tools[index.SeatDefinitionsAgainTool] = func(args map[string]any) (any, error) { // its tool lives in the tools bundle + _, value, err := index.NewSeatDefinitionsAgain(args["module"].(string), "why", "by", time.Now()) + values = append(values, value) + return nil, err + } + for _, c := range []struct { + tool string + args map[string]any + act string + }{ + {index.TakeInAgainTool, map[string]any{"repository": "novox/mesh-host", "why": "rule changed"}, "again"}, + {index.TakeInPendingTool, map[string]any{"branch": "main"}, "held"}, + {index.TakeInSkipTool, map[string]any{"branch": "main", "source": float64(4), "why": "stale"}, "held-skip"}, + {index.PinDropTool, map[string]any{"key": "main.7", "why": "no pipeline"}, "pin-drop"}, + {index.RunDropTool, map[string]any{"run": "run-12", "why": "never ends"}, "run-drop"}, + {index.NamedDropTool, map[string]any{"fingerprint": "abc", "member": "ace.feh", "why": "gone"}, "named-fingerprint"}, + {index.FingerprintsAgainTool, map[string]any{"module": "feh", "why": "unread packages"}, "fingerprints"}, + {index.RecomputeTool, map[string]any{"why": "check"}, "recompute"}, + {index.DropCandidateGraphTool, map[string]any{"group": "g", "heads": "h", "why": "no gate event"}, "candidate-group"}, + {index.SeedAgainTool, map[string]any{"why": "empty"}, "registry"}, + {index.SeatDefinitionsAgainTool, map[string]any{"module": "mesh-contracts"}, "seat-definitions"}, + } { + t.Run(c.tool, func(t *testing.T) { + run, ok := tools[c.tool] + if !ok { + t.Fatalf("no tool %s", c.tool) + } + values = nil + if _, err := run(c.args); err != nil || len(values) != 1 { + t.Fatalf("%s wrote %d values (%v)", c.tool, len(values), err) + } + if _, err := index.DecodeTakeIn(values[0]); err != nil { + t.Fatalf("DecodeTakeIn refuses %s: %v", values[0], err) + } + ix, kv := &askedBy{}, &answeredKV{} + var said []string + takenIn(t.Context(), ix, kv, askEntry{key: "k", value: values[0]}, func(f string, a ...any) { said = append(said, f) }) + if !slices.Equal(ix.asked, []string{c.act}) { + t.Errorf("takenIn asked %v of %s, want %s (said %s)", ix.asked, values[0], c.act, strings.Join(said, "; ")) + } + var answered index.TakeInAsk + if err := json.Unmarshal(kv.written, &answered); err != nil || !answered.Done() { + t.Errorf("takenIn wrote back %s (%v), want the answered ask", kv.written, err) + } + }) + } +} diff --git a/modules/mesh-delivery-indexer/internal/index/takein.go b/modules/mesh-delivery-indexer/internal/index/takein.go index 3cba48d0..5e7074c3 100644 --- a/modules/mesh-delivery-indexer/internal/index/takein.go +++ b/modules/mesh-delivery-indexer/internal/index/takein.go @@ -4,6 +4,8 @@ import ( "encoding/json" "errors" "fmt" + "reflect" + "strings" "time" ) @@ -18,45 +20,48 @@ const ( ) // TakeInAsk is a value of the takeins state: which hand act a person asked, why, who and when, and, once the indexer read -// it, its answer. +// it, its answer. Every field says by its tag takein what it is: an act (a field that alone names a hand act the indexer +// serves), an argument of an act, or of the ask itself (version, who, why, when, the answer); DecodeTakeIn reads the acts +// from these tags, so a hand act added here is read the moment its field is (H-A-208, H-A-210: indexer_take_in_again's +// Again was refused by a hand-kept list). type TakeInAsk struct { - V int `json:"v"` - Module string `json:"module,omitempty"` - Why string `json:"why,omitempty"` + V int `json:"v" takein:"ask"` + Module string `json:"module,omitempty" takein:"argument"` + Why string `json:"why,omitempty" takein:"ask"` // SeatDefinitions asks a contracts module's trunk seat definitions to be read again now (indexer_seat_definitions_again, // DLV-I19). - SeatDefinitions bool `json:"seat-definitions,omitempty"` + SeatDefinitions bool `json:"seat-definitions,omitempty" takein:"act"` // Fingerprints asks the fingerprints of Module ("all": every module) to be computed again now, from the published // packages as they are read (indexer_fingerprints_again, fingerprints.go). - Fingerprints bool `json:"fingerprints,omitempty"` + Fingerprints bool `json:"fingerprints,omitempty" takein:"act"` // Recompute asks the catalog graph to be computed again now and compared with the newest kept revision (indexer_recompute). - Recompute bool `json:"recompute,omitempty"` + Recompute bool `json:"recompute,omitempty" takein:"act"` // CandidateGroup and CandidateHeads, with Drop, ask that candidate's catalog half to be dropped (indexer_drop_candidate_graph). - CandidateGroup string `json:"candidate-group,omitempty"` - CandidateHeads string `json:"candidate-heads,omitempty"` - Drop bool `json:"drop,omitempty"` + CandidateGroup string `json:"candidate-group,omitempty" takein:"act"` + CandidateHeads string `json:"candidate-heads,omitempty" takein:"argument"` + Drop bool `json:"drop,omitempty" takein:"argument"` // NamedFingerprint and NamedMember, with Drop, ask one member to be removed from that fingerprint's set of the state named // (indexer_named_drop, named.go, design 47 G §675). - NamedFingerprint string `json:"named-fingerprint,omitempty"` + NamedFingerprint string `json:"named-fingerprint,omitempty" takein:"act"` // PinDrop asks one pin of the state pins, by its key, to be dropped (indexer_pin_drop, design 47 H §765). - PinDrop string `json:"pin-drop,omitempty"` + PinDrop string `json:"pin-drop,omitempty" takein:"act"` // Held names the watched branch whose held source-moved is taken in again now (indexer_take_in_pending) or, with Skip and // HeldSource, ended unwritten (indexer_take_in_skip; design 47 H §744). - Held string `json:"held,omitempty"` - HeldSource int64 `json:"held-source,omitempty"` - Skip bool `json:"skip,omitempty"` - NamedMember string `json:"named-member,omitempty"` + Held string `json:"held,omitempty" takein:"act"` + HeldSource int64 `json:"held-source,omitempty" takein:"argument"` + Skip bool `json:"skip,omitempty" takein:"argument"` + NamedMember string `json:"named-member,omitempty" takein:"argument"` // Again names the repository taken in again at its kept merge commit (indexer_take_in_again, take_in_again.go; H-A-208, H-A-210). - Again string `json:"again,omitempty"` + Again string `json:"again,omitempty" takein:"act"` // RunDrop asks one run on request, by its group run-, to be dropped with its candidate (indexer_run_drop, runs_tools.go, A-194). - RunDrop string `json:"run-drop,omitempty"` + RunDrop string `json:"run-drop,omitempty" takein:"act"` // Registry asks one of the registrations' acts (registrations.go): a trusted change of one, the verb registrations, // indexer_proposal_drop or indexer_seed_again (design 47 G §3, §4, §8, §667). - Registry *RegistryAct `json:"registry,omitempty"` - By string `json:"by,omitempty"` - At time.Time `json:"at"` - Result *TakenIn `json:"result,omitempty"` - Answered time.Time `json:"answered,omitzero"` + Registry *RegistryAct `json:"registry,omitempty" takein:"act"` + By string `json:"by,omitempty" takein:"ask"` + At time.Time `json:"at" takein:"ask"` + Result *TakenIn `json:"result,omitempty" takein:"ask"` + Answered time.Time `json:"answered,omitzero" takein:"ask"` } // Done says the ask is answered. @@ -71,8 +76,33 @@ func DecodeTakeIn(data []byte) (TakeInAsk, error) { if a.V != 1 { return a, fmt.Errorf("a takeins value of version %d, not 1", a.V) } - if a.Module == "" && !a.Fingerprints && !a.Recompute && a.CandidateGroup == "" && a.NamedFingerprint == "" && a.PinDrop == "" && a.Held == "" && a.RunDrop == "" && a.Registry == nil { + if len(a.Acts()) == 0 { return a, errors.New("a takeins value naming no hand act the indexer serves") } return a, nil } + +// takeInActs are the indices of TakeInAsk's fields tagged takein:"act". +var takeInActs = func() []int { + var out []int + t := reflect.TypeFor[TakeInAsk]() + for i := range t.NumField() { + if t.Field(i).Tag.Get("takein") == "act" { + out = append(out, i) + } + } + return out +}() + +// Acts names the hand acts the ask names, by their json names: its fields tagged takein:"act" that are set. +func (a TakeInAsk) Acts() []string { + var out []string + v := reflect.ValueOf(a) + for _, i := range takeInActs { + if !v.Field(i).IsZero() { + name, _, _ := strings.Cut(v.Type().Field(i).Tag.Get("json"), ",") + out = append(out, name) + } + } + return out +}