Files
mesh-catalog/modules/mesh-vault/cmd/mesh-vault/assignmentsview.go
T
jschoubben c3e75e35e1 Merge origin/main (licence manager on the bus, issue 1409) into feat/47-G-join9
claude-code's and mesh-vault's manifests take both sides' additions (G's seat reads and
findings-asked, main's licence seat verbs and events); claude-code's manifest test lists both.
mesh-vault's licence-bindings follower still uses the removed views.FindingWriter: fixed after.

Builds: design 47 G §610

Claude-Session: https://claude.ai/code/session_01NcbgfGKaxw984XyAPfLm4h
2026-11-12 01:48:43 +01:00

198 lines
7.3 KiB
Go

package main
// The vault's reading of the seat mesh-module-assigner's assignments, through its owner (novox/hq ADR 0615 §1, §4 to §6, ADR
// 0596): the assigner's tool `assigner_read_assignments` asked for the whole state at start, the owner's `views.assignments`
// 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"
"git.novox.be/novox/mesh-sdk/go/views"
)
// AssignmentsTool is the assigner's ordinary read tool of its assignments; AssignmentsViewsEvent the event of its views family.
const (
// assignerModule is the holder of the seat mesh-module-assigner, named from this module's manifest (invokes its read tool,
// consumes its views; TestTheAssignmentsNamesAreTheManifests).
assignerModule = "mesh-delivery-assigner"
AssignmentsTool = assignerModule + ".assigner_read_assignments"
AssignmentsViewsEvent = assignerModule + ".views.assignments"
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"
// assignmentsView is the picture; nil until main begins it.
var assignmentsView *views.Follower
// newAssignmentsView makes the follower: ask calls the tool, gone is told of a deleted key, changed of every change after
// it is held (a first view's keys too).
func newAssignmentsView(ask func(key string, body any) (json.RawMessage, error), gone func(key string), changed func(),
findings views.WireWriter) (*views.Follower, *views.Reader) {
// A views.Reader holding the vault's writer follows it (design 47 G A-28), so the reader writes and clears its findings
// (past its bound the finding names the owner and the tool, and the answer clears it; no secret) and hears the owner's
// views event, asking again on a gap.
r := &views.Reader{Module: Seat, Findings: findings, Logf: say,
Changed: func(_, key string, _ json.RawMessage, deleted bool) {
if deleted {
gone(key)
}
if changed != nil {
changed()
}
}}
f := r.Follow(assignerModule, "assignments", AssignmentsTool)
f.Ask = func(_ context.Context, after string) (views.Answer, error) {
body := map[string]any{"values": true}
if after != "" {
body["after"] = after
}
raw, err := ask(AssignmentsTool, body)
if err != nil {
return views.Answer{}, err
}
return views.DecodeAnswer(unwrapToolAnswer(raw))
}
return f, r
}
// 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
}
// viewAssignments is the assignments 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 viewAssignments(f func() *views.Follower) AssignerReader {
ready := func() (*views.Follower, error) {
fol := f()
if fol == nil {
return nil, fmt.Errorf("the vault reads the assigner's assignments 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 assignments is not answered yet (%s asked %d times)", s.Tool, s.Asks)
}
}
return AssignerReader{
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 assignments is a node's own key, `<node>.node`.
func leaverKey(key string) (string, bool) {
node, ok := strings.CutSuffix(key, ".node")
return node, ok && node != ""
}
// firstViews are the vault's first views begun, by the owner's state each follows (`<owner>.<state>`): the assigner's
// assignments and the licence manager's bindings (design 39 bus-and-switch B21, B42).
func firstViews() ([]string, map[string]*views.Follower) {
all := map[string]*views.Follower{}
if assignmentsView != nil {
all[assignerModule+".assignments"] = assignmentsView
}
if licenceBindingsView != nil {
all["claude-licence-manager.bindings"] = licenceBindingsView
}
names := make([]string, 0, len(all))
for n := range all {
names = append(names, n)
}
sort.Strings(names)
return names, all
}
// viewTools are the read tool of the first views and their re-read, as every reader has them (design 25 § 12.2; design
// 39 bus-and-switch B21, B42).
func viewTools(env *Env) []toolDef {
return []toolDef{
{Name: FirstViewsTool,
Description: "The vault's first views, of the assigner's assignments and of the licence manager's bindings: for each, 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) {
names, all := firstViews()
if len(names) == 0 {
return nil, fmt.Errorf("the vault keeps its first views only when it runs as the module")
}
out := []views.Status{}
for _, n := range names {
out = append(out, all[n].Status())
}
return map[string]any{"first-views": out}, nil
}},
{Name: FirstViewAgainTool,
Description: "Ask a first view once more by hand, with why, recorded in the vault's log: of names it by its owner's state " +
"(mesh-delivery-assigner.assignments or claude-licence-manager.bindings); absent, both are asked again.",
Input: map[string]any{"why": str("why the first view is asked again"),
"of": str("the first view, by its owner's state: mesh-delivery-assigner.assignments or claude-licence-manager.bindings; absent for both")},
run: func(caller string, a map[string]any) (any, error) {
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
}
names, all := firstViews()
if of := strings.TrimSpace(arg(a, "of")); of != "" {
if all[of] == nil {
return nil, fmt.Errorf("the vault keeps no first view of %q here; it keeps %v", of, names)
}
names = []string{of}
}
if len(names) == 0 {
return nil, fmt.Errorf("the vault keeps its first views only when it runs as the module")
}
for _, n := range names {
say("the first view of %s is asked again by %s: %s", n, caller, why)
if err := all[n].Again(context.Background(), why); err != nil {
return nil, err
}
}
return map[string]any{"asked-again": true}, nil
}},
}
}