Files
mesh-delivery/modules/mesh-delivery-indexer/internal/index/held.go
T
jschoubben 3447dd5f4d I6: indexer_pin_drop drops a pin by its key; the held asks are read
I6: indexer_unpin is renamed indexer_pin_drop and takes the pin's key <branch>.<revision>
and why: it drops every record's pin of that key, clears its finding pin-missing and the
removal rule then applies; a hand act recorded in the hand-act log before it is asked
(§765, H-A-57).

I5 follow-up: the asks of indexer_take_in_pending and indexer_take_in_skip were refused by
the ask reader (DecodeTakeIn named no held branch); they are read now, and
indexer_pending answers {held: [{branch, source, why, since}]}, since in RFC 3339, the
state held's form (H-A-102). The test drives each tool's ask through the reader.

Builds: design 47 H §765 (indexer_pin_drop), §744 (the held tools); H-A-57, H-A-97, H-A-102

Claude-Session: https://claude.ai/code/session_01JG73KHUnBze48rdua8u2sy
2026-11-20 04:56:27 +01:00

182 lines
8.1 KiB
Go

package index
import (
"context"
"encoding/json"
"errors"
"fmt"
"slices"
"strings"
"time"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdelivery"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-indexer/takein"
stdio "git.novox.be/novox/mesh-sdk/go"
"git.novox.be/novox/mesh-sdk/go/keys"
)
// HeldState is the indexer's state held (design 47 H §744, H-A-97): one key per watched branch, the branch escaped as a key's
// name is, holding the source-moved its consumer gave and the indexer could not take in, written when it is held and deleted at
// its take-in or skip. indexer_pending reads it.
const HeldState = "held"
// The held take-in's tools (§744).
const (
PendingTool = "indexer_pending"
TakeInPendingTool = "indexer_take_in_pending"
TakeInSkipTool = "indexer_take_in_skip"
)
// HeldTakeIn is a value of the state held: the branch's held source revision, why it is held and since when.
type HeldTakeIn struct {
Branch string `json:"branch"`
Source int64 `json:"source"`
Why string `json:"why"`
Since time.Time `json:"since"`
}
// PendingAnswer is indexer_pending's answer (H-A-102): every branch's held take-in, in the state held's form (since in RFC 3339).
type PendingAnswer struct {
Held []HeldTakeIn `json:"held"`
}
// hold writes the branch's held take-in and says take-in.pending, once per source: a source already held is neither written nor
// said again, so its since stays.
func (ix *Indexer) hold(ctx context.Context, e meshdelivery.SourceMovedEvent, why string) {
if ix.Held != nil {
key := keys.EscapeName(e.Branch)
if v, _, found, err := ix.Held.Get(ctx, key); err == nil && found {
var h HeldTakeIn
if json.Unmarshal(v, &h) == nil && h.Source == e.Source {
return
}
}
data, err := json.Marshal(HeldTakeIn{Branch: e.Branch, Source: e.Source, Why: why, Since: ix.now().UTC()})
if err == nil {
err = write(ctx, ix.Held, key, func([]byte, bool) ([]byte, bool, error) { return data, false, nil })
}
if err != nil {
ix.logf("take-in: %s.%s is not written: %v", HeldState, key, err)
}
}
ix.logf("take-in: source revision %d of %s is held: %s", e.Source, e.Branch, why)
if ix.SayTakeIn == nil {
return
}
p := meshcatalog.TakeInPending{Branch: e.Branch, Source: e.Source, Why: why}
if err := ix.SayTakeIn(ctx, takein.TakeInPendingEvent, PendingID(e.Branch, e.Source), p); err != nil {
ix.logf("take-in: take-in.pending of %s is not said: %v", BranchObject(e.Branch, e.Source), err)
}
}
// unhold deletes the branch's held take-in, once its source revision is taken in or skipped.
func (ix *Indexer) unhold(ctx context.Context, branch string) {
if ix.Held == nil {
return
}
key := keys.EscapeName(branch)
if err := write(ctx, ix.Held, key, func(_ []byte, found bool) ([]byte, bool, error) { return nil, found, nil }); err != nil {
ix.logf("take-in: %s.%s is not deleted: %v", HeldState, key, err)
}
}
// SkipTakeIn ends a held source revision unwritten by hand (indexer_take_in_skip, §744): it says take-in.taken with no entries,
// skipped, by and why, and deletes the branch's held take-in; the caller acknowledges the source-moved.
func (ix *Indexer) SkipTakeIn(ctx context.Context, e meshdelivery.SourceMovedEvent, by, why string) error {
if err := ix.sayTaken(ctx, meshcatalog.TakeInTaken{Branch: e.Branch, Source: e.Source, Call: e.Call, Skipped: true, By: by, Why: why}); err != nil {
return err
}
ix.unhold(ctx, e.Branch)
ix.logf("take-in: source revision %d of %s skipped by %s: %s", e.Source, e.Branch, by, why)
return nil
}
// ReadHeld is every branch's held take-in, by branch.
func ReadHeld(ctx context.Context, st Store) ([]HeldTakeIn, error) {
ks, err := st.Keys(ctx)
if err != nil {
return nil, err
}
slices.Sort(ks)
out := []HeldTakeIn{}
for _, k := range ks {
v, _, found, err := st.Get(ctx, k)
if err != nil {
return nil, err
}
var h HeldTakeIn
if !found || json.Unmarshal(v, &h) != nil {
continue
}
out = append(out, h)
}
return out, nil
}
// HeldTools are the held take-in's tools (§744): indexer_pending reads the state held; indexer_take_in_pending takes a branch's
// held source-moved again now and indexer_take_in_skip acknowledges it unwritten, each an ask in the state takeins the indexer
// acts on (H-A-97), the skip a hand act recorded through mesh-hand-acts before it is asked.
func HeldTools(held Store, ask func(key string, value []byte) (TakeInAsk, error), now func() time.Time, record func(what, why string) error) []stdio.Tool {
str := func(args map[string]any, k string) string { s, _ := args[k].(string); return strings.TrimSpace(s) }
return []stdio.Tool{
{Name: PendingTool,
Description: "Each watched branch's held take-in (design 47 H §744): the source revision of the gate's source-moved the indexer " +
"could not take in (the catalog store full, a read of the forge unanswered), why and since when; it stays unacknowledged " +
"until the indexer's room wakes it. indexer_take_in_pending takes it again now, indexer_take_in_skip ends it unwritten.",
Input: map[string]any{"type": "object", "properties": map[string]any{}},
Run: func(map[string]any) (any, error) {
h, err := ReadHeld(context.Background(), held)
return PendingAnswer{Held: h}, err
}},
{Name: TakeInPendingTool,
Description: "Take a watched branch's held source-moved in again now (design 47 H §744), as the indexer's room would: " +
"answers whether it was taken in. indexer_pending names the held branches.",
Input: map[string]any{"type": "object", "required": []string{"branch"}, "properties": map[string]any{
"branch": map[string]any{"type": "string", "description": "the watched branch, as indexer_pending names it"},
"why": map[string]any{"type": "string", "description": "why it is taken again now"}}},
Run: func(args map[string]any) (any, error) {
branch := str(args, "branch")
if branch == "" {
return nil, errors.New("name the held branch (branch), as indexer_pending names it")
}
data, err := json.Marshal(TakeInAsk{V: 1, Held: branch, Why: str(args, "why"), By: stdio.CallerOf(args), At: now().UTC()})
if err != nil {
return nil, err
}
a, err := ask("held."+keys.EscapeName(branch)+".again", data)
if err != nil {
return nil, err
}
return a.Result, nil
}},
{Name: TakeInSkipTool,
Description: "End a watched branch's held source-moved unwritten (design 47 H §744): it is acknowledged, take-in.taken is said " +
"with no entries and skipped, and the hand act is recorded through mesh-hand-acts before it is asked. indexer_pending " +
"names the held branch and source.",
Input: map[string]any{"type": "object", "required": []string{"branch", "source", "why"}, "properties": map[string]any{
"branch": map[string]any{"type": "string", "description": "the watched branch, as indexer_pending names it"},
"source": map[string]any{"type": "integer", "description": "the held source revision"},
"why": map[string]any{"type": "string", "description": "why it is skipped, in plain words"}}},
Run: func(args map[string]any) (any, error) {
branch, why := str(args, "branch"), str(args, "why")
source, _ := args["source"].(float64)
if branch == "" || source <= 0 || why == "" {
return nil, errors.New("name the held branch (branch) and source revision (source) and say why (why): a skip is a hand act, recorded through mesh-hand-acts")
}
data, err := json.Marshal(TakeInAsk{V: 1, Held: branch, HeldSource: int64(source), Skip: true, Why: why, By: stdio.CallerOf(args), At: now().UTC()})
if err != nil {
return nil, err
}
if err := record(fmt.Sprintf("skipped the held source revision %d of %s unwritten by hand", int64(source), branch), why); err != nil {
return nil, fmt.Errorf("the hand act was not recorded through mesh-hand-acts, so nothing was skipped: %w", err)
}
a, err := ask(fmt.Sprintf("held.%s.skip.%d", keys.EscapeName(branch), int64(source)), data)
if err != nil {
return nil, err
}
return a.Result, nil
}},
}
}