Files
mesh-delivery/modules/mesh-grants/internal/grants/running.go
T
jschoubben 4a61381ff1 W8.7 rest, W8.6 (node place): the grants part keeps what each node-engine runs (running, grants_running) and reads seat holders from its digest; a machine's place in an event's subject is the definition's <node>
The part binds its own durable on the node-engine's kept stream (HearKept, tried at every wake until the pass has made it),
the seat's holders are read from the manifest of the digest each node says it runs, and a node-engine's account is granted its
own node at the part its definition names (running.<node>.*) and its durable on the kept stream by name.

Claude-Session: https://claude.ai/code/session_01JG73KEq4zoDfM9WtYpcQgZ

Builds: design 47 G §199, §249, §309
2026-11-11 14:40:26 +01:00

117 lines
4.3 KiB
Go

package grants
import (
"bytes"
"encoding/json"
"fmt"
"sort"
"strings"
"git.novox.be/novox/mesh-contracts/go/seats/nodeengine"
"git.novox.be/novox/mesh-sdk/go/catalogue"
)
// **What each node runs, as its node-engine says it** (novox/hq design 47, ADR 0660 §2): the seat node-engine's event
// `running`, kept last per subject on its own stream, is heard on the part's own durable and kept in the state `running`,
// one key `<node>.<module>` holding the event's body. holdersAfterPass reads the manifest of the digest each says, not the
// build the assigner wants.
// StateRunning is the part's state of what each node-engine said it runs.
const StateRunning = "running"
// RunningKey is a key of StateRunning.
func RunningKey(node, module string) string { return node + "." + module }
// HearRunning keeps one running event in the state running when it differs from the key's value (the time it was seen
// aside), and says whether the part must compose again. An event that is not a running body, or names a subject other
// than its own node and module, is refused with the reason; the caller drops it.
func (p *Part) HearRunning(subject string, data []byte) (changed bool, err error) {
var r nodeengine.RunningEvent
if err := json.Unmarshal(data, &r); err != nil {
return false, fmt.Errorf("%s is not a running body: %w", subject, err)
}
if r.Node == "" || r.Module == "" || r.Manifest == "" {
return false, fmt.Errorf("%s names no node, module or manifest digest", subject)
}
if !strings.HasSuffix(subject, "."+RunningKey(r.Node, r.Module)) {
return false, fmt.Errorf("%s does not carry the node and module its body names (%s)", subject, RunningKey(r.Node, r.Module))
}
key := RunningKey(r.Node, r.Module)
was, _, found, err := p.Store.Get(StateRunning, key)
if err != nil {
return false, fmt.Errorf("its own %s %s cannot be read: %w", StateRunning, key, err)
}
if found {
var w nodeengine.RunningEvent
if json.Unmarshal(was, &w) == nil && w.Manifest == r.Manifest && w.Fingerprint == r.Fingerprint {
return false, nil
}
}
if _, err := p.Store.Put(StateRunning, key, bytes.Clone(data)); err != nil {
return false, fmt.Errorf("its own %s %s cannot be written: %w", StateRunning, key, err)
}
return true, nil
}
// readRan reads each key of StateRunning and the manifest its digest names (the indexer's, by digest), into in.Ran. A key
// whose manifest cannot be read is a gap, and that node's module is read as it was before (the trunk's).
func (p *Part) readRan(in *Inputs) {
in.Ran = map[string]catalogue.Manifest{}
keys, err := p.Store.Keys(StateRunning)
if err != nil {
in.Gaps = append(in.Gaps, Gap{Input: StateRunning, Why: "its own state cannot be read, so no seat is read from what a node runs: " + err.Error()})
return
}
sort.Strings(keys)
for _, key := range keys {
raw, _, found, err := p.Store.Get(StateRunning, key)
if err != nil || !found {
continue
}
var r nodeengine.RunningEvent
if json.Unmarshal(raw, &r) != nil || r.Manifest == "" {
continue
}
m, err := p.cache.get(p.Read, r.Manifest)
if err != nil {
in.Gaps = append(in.Gaps, Gap{Input: "running manifest of " + key, Why: "the manifest the node-engine says it runs cannot be read, so the trunk's is used: " + err.Error()})
continue
}
in.Ran[key] = m
}
}
// RunningSaid is what the tool grants_running answers: each node and module the part has heard, the build and manifest
// digest, and when it was seen.
type RunningSaid struct {
Running []nodeengine.RunningEvent `json:"running"`
Note string `json:"note,omitempty"`
}
// RunningOf reads the state running, one key (`<node>.<module>`) or all.
func (p *Part) RunningOf(key string) (any, error) {
keys := []string{key}
if key == "" {
var err error
if keys, err = p.Store.Keys(StateRunning); err != nil {
return nil, err
}
sort.Strings(keys)
}
out := RunningSaid{Running: []nodeengine.RunningEvent{}}
for _, k := range keys {
raw, _, found, err := p.Store.Get(StateRunning, k)
if err != nil {
return nil, err
}
var r nodeengine.RunningEvent
if found && json.Unmarshal(raw, &r) == nil {
out.Running = append(out.Running, r)
}
}
if len(out.Running) == 0 {
out.Note = "no node-engine has said it runs anything that this part heard: seats are read from the trunk's manifests"
}
return out, nil
}