Files
mesh-tools/node-tools/internal/runtime/gate.go
T
jschoubben f2a05fb440 T2 no-callers-hook: the runner admits every caller by serving, with no hook that judges who may call
The `admitted` hook, its parameter of serveSeats and its comments go; the test rows that built definitions with Callers and Class
go with the field mesh-sdk removes. A seat's verb is answered for every caller.

Builds: design 47 H §752, §764 (Every seat contract), H-A-41

Claude-Session: https://claude.ai/code/session_01JG73KHUnBze48rdua8u2sy
2026-11-20 02:38:21 +01:00

197 lines
7.0 KiB
Go

package runtime
import (
"fmt"
"strings"
"time"
"git.novox.be/novox/mesh-sdk/go/approval"
"git.novox.be/novox/mesh-sdk/go/seatdef"
"github.com/novox/mesh-tools/node-tools/internal/launch"
)
// FingerprintWord is the word of a module's composed environment that names the build its bundle serves (design 47 G §424).
const FingerprintWord = "MESH_FINGERPRINT"
// Gate is the approval gate the runner serves through (design 46 7-approval-mark §4, §20; node-tools/internal/approval).
type Gate interface {
// Unserved is, of a module's marked tools, those its bundle does not register, saying it as the module's finding.
Unserved(module, fingerprint string, registered []string) []string
// Run is a call through the gate: run is called only when the call may run now, once.
Run(call approval.Call, run func() (any, error)) (any, error)
// Approvals is the seat verb `approvals` for a caller: the calls the gate holds, its acts and claims (A-265, A-271).
Approvals(caller string) (approval.Approvals, error)
}
// gatedRun is how a handler calls a tool: through the gate where there is one.
type gatedRun func(call approval.Call, t launch.Tool, caller string) (any, error)
// gated calls the tool through the gate, or straight where the runner has none.
func (c *consumers) gated(call approval.Call, t launch.Tool, caller string) (any, error) {
run := func() (any, error) { return t.Run(caller, call.Args) }
if c.gate == nil {
return run()
}
return c.gate.Run(call, run)
}
// approvalsOf is how the runner answers a held seat's verb `approvals`: from its gate's record, nil without a gate.
func (r *runner) approvalsOf() func(caller string) (any, error) {
if r.events.gate == nil {
return nil
}
return func(caller string) (any, error) { return r.events.gate.Approvals(caller) }
}
// WithGate serves every call through the approval gate.
func WithGate(g Gate) Option { return func(c *consumers) { c.gate = g } }
var _ Gate = (*approval.Gate)(nil)
// WithSeatDefinitions gives the runner the seat definitions its seat list holds: a verb's `chunks` and `timeout` are read from them
// (design 47 H §760). No definition names who may call: every caller is answered (design 47 H §752).
func WithSeatDefinitions(defs func() map[string]seatdef.Definition) Option {
return func(c *consumers) { c.definitions = defs }
}
// unservedMarks is, of the module's marked tools, those its bundle's registrations under its own name do not register.
func (r *runner) unservedMarks(module string, regs []launch.Registration) []string {
if r.events.gate == nil {
return nil
}
var registered []string
for _, reg := range regs {
if reg.Module == module {
for _, t := range reg.Tools {
registered = append(registered, t.Name)
}
}
}
return r.events.gate.Unserved(module, r.fingerprintOf(module), registered)
}
// WithSeatDefinitionsAgain gives the runner the way to read the seat list again (the seat list reader's Reload): a seat verb a bundle asks
// that the definitions held do not name is looked up again after it, never taken for a verb that sends no chunks.
func WithSeatDefinitionsAgain(again func() error) Option {
return func(c *consumers) { c.reread = again }
}
// chunkedAsked is chunked for a verb a bundle asks, where the answer decides whether the runner chooses the `call` the holder
// requires (AskAsChunked) (design 47 G A-490m). A verb of a seat the definitions held DEFINE, whose definition does not name it,
// is not taken for one that sends no chunks, since the holder then refuses an ask that carries no call: the seat list is read
// again once (one read shared by the asks that wait on it) and a verb still unnamed is an error saying so. An ask of a seat the
// definitions do not define at all, a module's tool and a runner that holds no definitions (nil) are asked as before.
func (c *consumers) chunkedAsked(key string) (bool, error) {
if c.definitions == nil || !strings.HasPrefix(key, "seat:") {
return false, nil
}
name, _, _ := strings.Cut(key, "@")
seat, verb, ok := strings.Cut(strings.TrimPrefix(name, "seat:"), ".")
if !ok {
return false, nil
}
find := func() (chunks, defined, named bool) {
d, defined := c.definitions()[seat]
for _, v := range d.Verbs {
if v.Name == verb {
return v.Chunks, true, true
}
}
return false, defined, false
}
chunks, defined, named := find()
if named || !defined {
return chunks, nil
}
var why string
if c.reread != nil {
if err := c.rereadShared(); err != nil {
why = " (reading the seat list again failed: " + err.Error() + ")"
}
chunks, defined, named = find()
if named {
return chunks, nil
}
if !defined {
return false, nil
}
}
return false, fmt.Errorf("the definition of the seat %s names no verb %s, so the runner cannot tell whether it sends its answer in chunks and does not ask it%s", seat, verb, why)
}
// rereadShared reads the seat list again; asks that arrive while a read runs wait for it and share its result.
func (c *consumers) rereadShared() error {
c.rereadMu.Lock()
if f := c.rereading; f != nil {
f.waiters++
c.rereadMu.Unlock()
<-f.done
return f.err
}
f := &rereadFlight{done: make(chan struct{})}
c.rereading = f
c.rereadMu.Unlock()
f.err = c.reread()
c.rereadMu.Lock()
c.rereading = nil
c.rereadMu.Unlock()
close(f.done)
return f.err
}
type rereadFlight struct {
done chan struct{}
err error
waiters int // the asks that joined the read (under rereadMu)
}
// rereadWaiting is how many asks have joined the read now running; a test waits on it instead of sleeping.
func (c *consumers) rereadWaiting() int {
c.rereadMu.Lock()
defer c.rereadMu.Unlock()
if c.rereading == nil {
return 0
}
return c.rereading.waiters
}
// chunked says whether key names a seat's verb whose definition says `chunks` (design 47 G A-249): its answer is received in
// chunks by the runner, whose call hands the bundle the whole.
func (c *consumers) chunked(key string) bool {
if c.definitions == nil || !strings.HasPrefix(key, "seat:") {
return false
}
name, _, _ := strings.Cut(key, "@") // a verb asked of the machine that ran the ask is chunked as the verb is (A-254)
seat, verb, ok := strings.Cut(strings.TrimPrefix(name, "seat:"), ".")
if !ok {
return false
}
for _, v := range c.definitions()[seat].Verbs {
if v.Name == verb {
return v.Chunks
}
}
return false
}
// withinOf is how long a call of key waits for its answer: the `timeout` the verb's definition names, in whole seconds (design 47
// H §760, H-A-43); zero, which the bus reads as the tool runner's own bound, for a key that is no seat's verb, a seat the
// definitions do not hold, a verb they do not name and a verb that names none.
func (c *consumers) withinOf(key string) time.Duration {
if c.definitions == nil || !strings.HasPrefix(key, "seat:") {
return 0
}
name, _, _ := strings.Cut(key, "@")
seat, verb, ok := strings.Cut(strings.TrimPrefix(name, "seat:"), ".")
if !ok {
return 0
}
for _, v := range c.definitions()[seat].Verbs {
if v.Name == verb && v.Timeout != nil {
return time.Duration(*v.Timeout) * time.Second
}
}
return 0
}