Files
mesh-delivery/modules/mesh-delivery-indexer/internal/index/registrations_tools.go
T
jschoubben 71a35a6376 MV1: move contracts and sdk under modules/, importers follow
The delivery repository's contracts and SDK live under modules/ as
mesh-delivery-contracts and mesh-delivery-sdk, their Go module paths
renamed to match the folders (the TypeScript names already did), and
every importer in mesh-delivery, mesh-catalog, mesh-host, mesh-tools
and mesh-sdk follows in the same group: a design as it should be, one
path per module, checked by the group's check building every importer.

Builds: design 47 G §427, §88

Claude-Session: https://claude.ai/code/session_01NcbgfGKaxw984XyAPfLm4h
2026-11-12 05:09:56 +01:00

120 lines
5.5 KiB
Go

package index
import (
"context"
"encoding/json"
"errors"
"strings"
"time"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog"
stdio "git.novox.be/novox/mesh-sdk/go"
)
// RegistrationTools are the seat mesh-catalog's verbs registrations, register, policy, watch and unwatch (design 47 G §3, §4), and
// the indexer's own tools of the proposals (§667) and the seed (§8), served in the tools bundle. Every act is asked of the
// indexer through takeins, as the indexer's other tools are (ask writes the value and waits for the answer); the proposals
// are read from the state as the bundle reads it. record says a hand act in the hand-act log.
func RegistrationTools(proposals Store, ask func(key string, value []byte) (TakeInAsk, error), now func() time.Time, record func(cause, verb, what, why string) error) []stdio.Tool {
act := func(verb string, withInput bool) func(map[string]any) (any, error) {
return func(args map[string]any) (any, error) {
var input json.RawMessage
if withInput {
clean := map[string]any{}
for k, v := range args {
if k != "caller" && !strings.HasPrefix(k, "_") {
clean[k] = v
}
}
var err error
if input, err = json.Marshal(clean); err != nil {
return nil, err
}
}
why, _ := args["why"].(string)
key, value, err := NewRegistryAsk(RegistryAct{Verb: verb, Input: input}, why, stdio.CallerOf(args), now())
if err != nil {
return nil, err
}
return answerOf(ask(key, value))
}
}
return []stdio.Tool{
{Name: meshcatalog.Seat + "." + meshcatalog.VerbRegistrations, Run: act(ActRegistrations, true)},
{Name: meshcatalog.Seat + "." + meshcatalog.VerbRegister, Run: act(verbRegister, true)},
{Name: meshcatalog.Seat + "." + meshcatalog.VerbPolicy, Run: act(verbPolicy, true)},
{Name: meshcatalog.Seat + "." + meshcatalog.VerbWatch, Run: act(verbWatch, true)},
{Name: meshcatalog.Seat + "." + meshcatalog.VerbUnwatch, Run: act(verbUnwatch, true)},
{Name: ProposalsTool, Description: "The changes of the registrations that wait for the operator's warrant and those that ended (design 47 G §667): " +
"for each key (<verb>.<module>, with the branch for watch and unwatch, and seed) its state (open, applied, declined, " +
"expired, refused, dropped, lost or seed-waits), its ask, caller, the lines it moves, its digest and its outcome. An ended key stays until the next change of the same key replaces it.",
Input: map[string]any{"type": "object", "properties": map[string]any{}},
Run: func(map[string]any) (any, error) {
rows, err := ListProposals(context.Background(), proposals)
return map[string]any{"proposals": rows}, err
}},
{Name: ProposalDropTool, Description: "Cancel the open ask of one key of the proposals and end the key dropped (design 47 G §667). A hand act: who and why " +
"are said in the hand-act log. indexer_proposals names the keys.",
Input: map[string]any{"type": "object", "required": []string{"key", "why"}, "properties": map[string]any{
"key": map[string]any{"type": "string", "description": "the proposal's key, as indexer_proposals names it"},
"why": map[string]any{"type": "string", "description": "why it is dropped, in plain words"}}},
Run: func(args map[string]any) (any, error) {
key, _ := args["key"].(string)
why, _ := args["why"].(string)
if strings.TrimSpace(key) == "" || strings.TrimSpace(why) == "" {
return nil, errors.New("name the key (key) and say why (why): a hand act is said in the hand-act log")
}
k, value, err := NewRegistryAsk(RegistryAct{Verb: ActDrop, Key: strings.TrimSpace(key)}, why, stdio.CallerOf(args), now())
if err != nil {
return nil, err
}
out, err := answerOf(ask(k, value))
if err != nil {
return out, err
}
res := map[string]any{"result": out}
if rerr := record(ProposalDropTool, "proposal-drop", "dropped the proposal "+key, why); rerr != nil {
res["hand-act"] = "not recorded: " + rerr.Error()
}
return res, nil
}},
{Name: SeedAgainTool, Description: "Raise the ask of the first registrations again from the seed's key (design 47 G §8), after a Decline, an expiry or a drop " +
"ended it and while the indexer holds no registration. A hand act: who and why are said in the hand-act log.",
Input: map[string]any{"type": "object", "required": []string{"why"}, "properties": map[string]any{
"why": map[string]any{"type": "string", "description": "why it is asked again, in plain words"}}},
Run: func(args map[string]any) (any, error) {
why, _ := args["why"].(string)
if strings.TrimSpace(why) == "" {
return nil, errors.New("say why (why): a hand act is said in the hand-act log")
}
k, value, err := NewRegistryAsk(RegistryAct{Verb: ActSeed, Again: true}, why, stdio.CallerOf(args), now())
if err != nil {
return nil, err
}
out, err := answerOf(ask(k, value))
if err != nil {
return out, err
}
res := map[string]any{"result": out}
if rerr := record(SeedAgainTool, "seed-again", "asked the first registrations again", why); rerr != nil {
res["hand-act"] = "not recorded: " + rerr.Error()
}
return res, nil
}},
}
}
// answerOf is the answer of an act the indexer answered: the verb's own answer, or the words that say why not.
func answerOf(a TakeInAsk, err error) (any, error) {
if err != nil {
return nil, err
}
if a.Result == nil {
return nil, errors.New("the indexer gave no answer")
}
if !a.Result.Stored {
return nil, errors.New(a.Result.Answer)
}
return a.Result.Registry, nil
}