Files
mesh-catalog/modules/mesh-vault/cmd/mesh-vault/placementview.go
T
jschoubben 9d83987815 Join item 1: the catalogue's modules take the router's ask from operator-channel's contract and client
mesh-sdk holds neither asks nor asker, nor the vault's answer types, and mesh-delivery's
sdk/views is mesh-sdk's go/views (a1-progress.md, the rewrite map for the join): thirteen
modules switch to the operatorchannel contract and client, mesh-sdk's channel package and
meshvault, with go.mod requiring each at the group pin (nats: meshbroker too). The gate
holds the checks mesh-sdk dropped (an ask's id is its key's asking, at least one option),
with a row that fails without them; claude-code refuses a router record whose times do
not parse, as its JSON decode did, with a row.

Claude-Session: https://claude.ai/code/session_01JG73KJNrH5EgFXMKKYSrig
2026-11-04 15:33:27 +01:00

199 lines
7.1 KiB
Go

package main
// The vault's reading of the seat mesh-placement's placement, through its owner (novox/hq ADR 0615 §1, §4 to §6, ADR
// 0596): the assigner's tool `assigner_read_placement` asked for the whole state at start, the owner's `views.placement`
// events after it, and the tool once more only on a gap. Started while the assigner is down it keeps asking and says so;
// it never reads the assigner's bucket instead. (The vault's other reads, the node-engines' haves, the grants part's
// accounts and the provisioner's plans and provisions, are not the indexer's or the assigner's state and stay as they are.)
import (
"context"
"encoding/json"
"fmt"
"sort"
"strings"
"time"
stdio "git.novox.be/novox/mesh-sdk/go"
"git.novox.be/novox/mesh-sdk/go/delivery"
"git.novox.be/novox/mesh-sdk/go/views"
)
// PlacementTool is the assigner's ordinary read tool of its placement; PlacementViewsEvent the event of its views family.
const (
PlacementTool = delivery.AssignerModule + ".assigner_read_placement"
PlacementViewsEvent = delivery.AssignerModule + ".views.placement"
FirstViewsTool = "vault_first_views"
FirstViewAgainTool = "vault_first_view_again"
)
// FindingsState is the vault's state of findings, which the condition keeper reads (ADR 0564): a finding names no secret.
const FindingsState = "findings"
// placementView is the picture; nil until main begins it.
var placementView *views.Follower
// newPlacementView makes the follower: ask calls the tool, gone is told of a deleted key.
func newPlacementView(ask func(key string, body any) (json.RawMessage, error), gone func(key string),
put func(key string, value json.RawMessage) error, drop func(key string) error) *views.Follower {
return &views.Follower{Owner: delivery.AssignerModule, Tool: PlacementTool, State: "placement", Logf: say,
Ask: func(_ context.Context, after string) (views.Answer, error) {
body := map[string]any{"values": true}
if after != "" {
body["after"] = after
}
raw, err := ask(PlacementTool, body)
if err != nil {
return views.Answer{}, err
}
return views.DecodeAnswer(unwrapToolAnswer(raw))
},
// Past its bound the vault's finding names the owner and the tool, and the condition keeper raises it (the
// answer deletes it); the vault keeps asking meanwhile.
Raise: func(s views.Status) {
say("the vault has no first view of the assigner's placement: %s has not answered since %s (%s); it keeps asking", s.Tool,
s.Asked.Format(time.RFC3339), s.Error)
if k, v, err := views.UnansweredFinding(Seat, s, time.Now()); err == nil {
if err := put(k, v); err != nil {
say("the finding %s is not written: %v", k, err)
}
}
},
Clear: func(s views.Status) {
if k, _, err := views.UnansweredFinding(Seat, s, time.Now()); err == nil {
_ = drop(k)
}
},
Changed: func(key string, _ json.RawMessage, deleted bool, _ time.Time) {
if deleted {
gone(key)
}
}}
}
// unwrapToolAnswer takes a tool's answer out of the protocol's content list when it comes in one.
func unwrapToolAnswer(raw json.RawMessage) []byte {
var r struct {
Content []struct {
Text string `json:"text"`
} `json:"content"`
}
if json.Unmarshal(raw, &r) == nil && len(r.Content) == 1 {
return []byte(r.Content[0].Text)
}
return raw
}
// beginPlacementView asks the first view until answered and hears the owner's events.
func beginPlacementView(f *views.Follower) {
go func() {
if err := f.Start(context.Background()); err != nil {
say("the first view of the assigner's placement ended unanswered: %v", err)
}
}()
go func() {
<-stdio.Served()
for wait := 2 * time.Second; ; wait = min(wait*2, time.Minute) {
err := stdio.Subscribe(PlacementViewsEvent, func(e stdio.Envelope) error {
ev, err := views.DecodeEvent(e.Body)
if err != nil {
say("%s is not a views event: %v", PlacementViewsEvent, err)
return nil
}
if gap, why := f.Apply(ev); gap {
say("a gap in the assigner's placement (%s): its first view is asked once more", why)
go func() { _ = f.Start(context.Background()) }()
}
return nil
})
if err == nil {
return
}
say("does not hear %s yet (%v); asking again in %s", PlacementViewsEvent, err, wait)
time.Sleep(wait)
}
}()
}
// viewPlacement is the placement reader over the picture: a key asked before the first view is answered is an error, so
// the caller waits (the "not yet" of the vault's other reads), never a read of the owner's bucket.
func viewPlacement(f func() *views.Follower) PlacementReader {
ready := func() (*views.Follower, error) {
fol := f()
if fol == nil {
return nil, fmt.Errorf("the vault reads the assigner's placement only when it runs as the module")
}
select {
case <-fol.Ready():
return fol, nil
default:
s := fol.Status()
return nil, fmt.Errorf("the first view of the assigner's placement is not answered yet (%s asked %d times)", s.Tool, s.Asks)
}
}
return PlacementReader{
Keys: func() ([]string, error) {
fol, err := ready()
if err != nil {
return nil, err
}
var keys []string
for k := range fol.Values() {
keys = append(keys, k)
}
sort.Strings(keys)
return keys, nil
},
Read: func(key string) ([]byte, error) {
fol, err := ready()
if err != nil {
return nil, err
}
v, ok := fol.Get(key)
if !ok {
return nil, nil
}
return v, nil
},
}
}
// leaverKey is whether a deleted key of the placement is a node's own key, `<node>.node`.
func leaverKey(key string) (string, bool) {
node, ok := strings.CutSuffix(key, ".node")
return node, ok && node != ""
}
// viewTools are the read tool of the first view and its re-read, as every reader has them (design 25 § 12.2).
func viewTools(env *Env) []toolDef {
return []toolDef{
{Name: FirstViewsTool,
Description: "The vault's first view of the assigner's placement: the tool it asks, the revision its picture holds, when it " +
"asked and was answered, how many asks (and by hand), and whether it is waiting.",
run: func(string, map[string]any) (any, error) {
if placementView == nil {
return nil, fmt.Errorf("the vault reads the assigner's placement only when it runs as the module")
}
return map[string]any{"first-views": []views.Status{placementView.Status()}}, nil
}},
{Name: FirstViewAgainTool,
Description: "Ask the first view of the assigner's placement once more by hand, with why, recorded in the vault's log.",
Input: map[string]any{"why": str("why the first view is asked again")},
run: func(caller string, a map[string]any) (any, error) {
if placementView == nil {
return nil, fmt.Errorf("the vault reads the assigner's placement only when it runs as the module")
}
why := strings.TrimSpace(arg(a, "why"))
if why == "" {
return nil, fmt.Errorf("say why: a first view asked by hand keeps its why")
}
if err := fromAGrantedCaller(caller, FirstViewAgainTool); err != nil {
return nil, err
}
say("the first view of the assigner's placement is asked again by %s: %s", caller, why)
return map[string]any{"asked-again": true}, placementView.Again(context.Background(), why)
}},
}
}