Files
mesh-delivery/modules/mesh-delivery-indexer/internal/index/fingerprints.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

168 lines
6.5 KiB
Go

package index
import (
"context"
"encoding/json"
"errors"
"fmt"
"maps"
"slices"
"strings"
"time"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshcatalogclient"
stdio "git.novox.be/novox/mesh-sdk/go"
)
// FingerprintsAgainTool computes the fingerprints again now (issue 1966; the instruction "tools are how a module is spoken
// to"): the indexer reads the packages state, takes no held fingerprint for the build's, and writes the import form for
// every module whose imports are all published, as a merge's recompute would. For the module whose fingerprint a pass
// wrote while the packages state was unread (the version line over the import form) and that nothing else moves.
const FingerprintsAgainTool = "indexer_fingerprints_again"
// FingerprintMove is one module's fingerprint before and after a recompute by hand.
type FingerprintMove struct {
Module string `json:"module"`
Old string `json:"old"`
New string `json:"new"`
}
// FingerprintsAgain computes the fingerprints of module again ("all" or empty: every held module) from the published
// packages as they are read now, and answers each fingerprint that moved. A read of the packages state that failed
// writes nothing, and says so.
func (ix *Indexer) FingerprintsAgain(ctx context.Context, module string) TakenIn {
module = strings.TrimSpace(module)
if module == "" {
module = "all"
}
out := TakenIn{Module: module}
if ix.Modules == nil || ix.Manifests == nil {
out.Answer = "the indexer writes no fingerprints"
return out
}
before, err := ix.fingerprintsHeld(ctx)
if err != nil {
out.Answer = "the modules state cannot be read: " + err.Error()
return out
}
if module != "all" {
if _, ok := before[module]; !ok {
out.Answer = "the indexer holds no fingerprint of " + module
return out
}
}
key := module
if module == "all" {
key = "*"
}
ix.freshMu.Lock()
ix.fresh = map[string]bool{key: true}
ix.freshMu.Unlock()
// The path of a merge's recompute: the modules, then the candidates their fingerprints reach (recomputeWith), then the
// pending merges a changed fingerprint settles (PendingAgain, as after a toolchain report).
err = ix.recomputeWith(ctx, nil, nil)
ix.freshMu.Lock()
ix.fresh = nil
ix.freshMu.Unlock()
if err != nil {
out.Answer = "nothing was written: " + err.Error()
return out
}
if err := ix.PendingAgain(ctx); err != nil {
ix.logf("the pending merges are not evaluated again after the fingerprints were computed again: %v", err)
}
after, err := ix.fingerprintsHeld(ctx)
if err != nil {
out.Answer = "computed, but the modules state cannot be read back: " + err.Error()
return out
}
for _, m := range slices.Sorted(maps.Keys(after)) {
if module != "all" && m != module {
continue
}
if before[m] != after[m] {
out.Fingerprints = append(out.Fingerprints, FingerprintMove{Module: m, Old: before[m], New: after[m]})
}
}
out.Stored = true
out.Answer = fmt.Sprintf("%d fingerprint(s) moved", len(out.Fingerprints))
return out
}
// isFresh says the module's import form is computed again by hand in this pass.
func (ix *Indexer) isFresh(module string) bool {
ix.freshMu.Lock()
defer ix.freshMu.Unlock()
return ix.fresh[module] || ix.fresh["*"]
}
// fingerprintsHeld is each held module's fingerprint of the new form.
func (ix *Indexer) fingerprintsHeld(ctx context.Context) (map[string]string, error) {
keys, err := ix.Modules.Keys(ctx)
if err != nil {
return nil, err
}
out := map[string]string{}
for _, k := range keys {
value, _, found, err := ix.Modules.Get(ctx, k)
if err != nil {
return nil, err
}
if !found {
continue
}
if m, err := meshcatalogclient.DecodeModule(value); err == nil {
out[k] = m.Fp2
}
}
return out, nil
}
// FingerprintsTools is the tool indexer_fingerprints_again over ask (the takeins state). It acts first and says the hand
// act after: record is told what was done and answers an error when it could not be recorded, which the answer says and
// which never refuses or delays the recovery beyond bound.
func FingerprintsTools(ask func(key string, value []byte) (TakeInAsk, error), now func() time.Time, record func(what, why string) error, bound time.Duration) []stdio.Tool {
return []stdio.Tool{{Name: FingerprintsAgainTool,
Description: "Compute the fingerprints of one module (or all) again now from the published packages as the indexer reads them: " +
"the import form is written for every module whose imports are all published, whatever fingerprint the module holds. For the " +
"fingerprints a pass wrote while the packages state was unread (the version line over the import form: builds then fail on an " +
"older package). Answers each module whose fingerprint moved, old and new; when the packages state cannot be read it writes " +
"nothing and says why. Runs as a merge's recompute does (the modules, the candidates they reach, the pending merges they settle). Acts first, then records the hand act through mesh-hand-acts; when that cannot be recorded the answer says so.",
Input: map[string]any{"type": "object", "required": []string{"why"}, "properties": map[string]any{
"module": map[string]any{"type": "string", "description": "the module's name, or all (the default)"},
"why": map[string]any{"type": "string", "description": "why it is computed again now, in plain words"}}},
Run: func(args map[string]any) (any, error) {
module, _ := args["module"].(string)
why, _ := args["why"].(string)
module, why = strings.TrimSpace(module), strings.TrimSpace(why)
if why == "" {
return nil, errors.New("say why: a hand act is said in the journal")
}
if module == "" {
module = "all"
}
data, err := json.Marshal(TakeInAsk{V: 1, Module: module, Fingerprints: true, Why: why, By: stdio.CallerOf(args), At: now().UTC()})
if err != nil {
return nil, err
}
a, err := ask("fingerprints."+module+"."+now().UTC().Format("20060102T150405.000000000"), data)
if err != nil {
return nil, err
}
res := map[string]any{"result": a.Result}
done := make(chan error, 1)
go func() { done <- record("computed the fingerprints of "+module+" again by hand", why) }()
wait, cancel := context.WithTimeout(context.Background(), bound) // a context's bound, no timer (ADR 0437 §8)
defer cancel()
select {
case err := <-done:
if err != nil {
res["hand-act"] = "not recorded: " + err.Error()
}
case <-wait.Done(): // a bound on the record, never on the recovery
res["hand-act"] = fmt.Sprintf("not recorded within %s: mesh-hand-acts did not answer", bound)
}
return res, nil
}}}
}