Files
mesh-sdk/go/approval/run.go
T
jschoubben 2559c205bb approval: the first views run before any seat list is read (novox 2026-11-16 deadlock)
Cause: startRead looked only in the definitions the runner holds. The seat list is itself read through the seat
mesh-catalog, so before the catalog half is ready the runner holds none, and every first view it waits on (its own
and another runner's, from any account) was refused with "marks not read yet" for ever.

Quick fix: let the one account that stalled through (calledByModule). Thorough fix (chosen): the foundation's bootstrapped
definitions answer for the seats they name wherever the held list lacks them, so a first view runs whoever asks; an act
of the same seat and a seat no definition names stay held.

Test: TestTheFirstViewsRunBeforeAnySeatListIsRead (fails without run.go change).

Builds: design 47 G A-387, A-388, A-433
2026-11-16 18:00:04 +01:00

257 lines
9.5 KiB
Go

package approval
import (
"encoding/json"
"fmt"
"slices"
"strings"
"sync"
"git.novox.be/novox/mesh-sdk/go/operatorask"
"git.novox.be/novox/mesh-sdk/go/seatdef"
"git.novox.be/novox/mesh-sdk/go/seatlist"
)
// Run is a call through the gate (§20): an unmarked call runs; a marked one runs only on the operator's approval of that
// one call, judged on the router's record and claimed once (§14, §15), and is otherwise answered as an error saying why
// (§12, §17, §19). run is called at most once.
func (g *Gate) Run(c Call, run func() (any, error)) (any, error) {
m, err := g.marksOf(c)
if err != nil {
return nil, err
}
if missing := g.unservedOf(c.Module); len(missing) > 0 {
return nil, fmt.Errorf("Not run: %s's marks name %s, which its bundle on %s does not register, so none of its tools is "+
"served here (design 46 7-approval-mark §4)", c.Module, strings.Join(missing, ", "), g.d.Node)
}
marked, _ := m.Marked(c.Seat, c.Name)
since := g.markedSince(c, marked, m.Revision)
if !marked {
return run()
}
if c.Own {
return nil, g.refused(c, "", "it is one of the tool runner's own tools, which no ask covers")
}
if m.Unread != "" {
return nil, g.refused(c, "", m.Unread)
}
if b, ok := g.d.State.(Binding); g.d.State == nil || ok && !b.Bound() {
return nil, g.refused(c, "", "the tool runner's membership binds no state "+StateName+" to keep its asks in")
}
if why := refusedUnasked(c.Args); why != "" {
return nil, g.refused(c, "", why)
}
if why := g.held(); why != "" {
return nil, g.refused(c, "", why)
}
act, err := ActOf(c, g.d.Node)
if err != nil {
return nil, g.refused(c, "", err.Error())
}
digest, err := operatorask.ActDigest(act)
if err != nil {
return nil, g.refused(c, "", err.Error())
}
named, err := g.d.State.Get(g.actKey(digest))
if err != nil {
g.stateFailed(err)
return nil, g.refused(c, digest, "its state "+StateName+" cannot be read: "+err.Error())
}
g.stateRead()
if named == nil {
return nil, g.raise(c, act, digest, since)
}
var id string
if err := json.Unmarshal(named.Value, &id); err != nil || id == "" {
return nil, g.refused(c, digest, fmt.Sprintf("its act's key in %s names no ask", StateName))
}
return g.judge(c, act, digest, id, since, run)
}
// Binding is a Store that says whether the runner's membership binds the state now (§13).
type Binding interface{ Bound() bool }
// ViewCallersPrefix starts the name of the env value that holds a first-view tool's callers list in its owner's bundle
// (views.CallersEnv adds the tool's name), by which the gate tells a first-view tool.
const ViewCallersPrefix = "MESH_VIEW_CALLERS_"
// startRead says the call runs while the catalog half is unread (design 47 G lead answers A-387, A-388): a seat verb the
// seat definition the runner holds classes `read` explicitly and does not mark needs-approval, so the runner that serves
// them answers the reads its own catalog half and the first take-in wait on; and a module's own first-view tool (A-377),
// the tool whose callers list its bundle's environment carries, a list that still limits who may call it.
func (g *Gate) startRead(c Call) bool {
if c.Seat == "" {
_, first := c.Env[ViewCallersPrefix+strings.ToUpper(strings.ReplaceAll(c.Name, "-", "_"))]
return first
}
def, ok := g.d.Catalog.Definitions()[c.Seat]
if !ok {
// The seat list is itself read through the seat mesh-catalog, so before its first view the held definitions are
// none: the foundation's bootstrapped definitions answer for the seats they name (the first view of the catalog half
// must run before the catalog half is ready: novox 2026-11-16, the indexer's mesh-catalog.revision refused for 20 minutes).
def, ok = bootstrapped(c.Seat)
if !ok {
return false
}
}
i := slices.IndexFunc(def.Verbs, func(v seatdef.Verb) bool { return v.Name == c.Name })
return i >= 0 && def.Verbs[i].Class == seatdef.ClassRead && !def.Verbs[i].NeedsApproval
}
// calledByModule says a call runs unmarked while the catalog half is unread (design 47 G lead answers A-389, A-433): one
// whose proven caller is a module's account, `<node>.<module>`, a seat verb or a module tool alike, and the module itself
// in a runtime of its own; never the runner's own account where the runner serves other modules (the console's, which the
// sessions and agents call as), a person's, the controller's or an unproven caller. Approval marks are for agent calls; a
// module's account may do everything (principle 6), and a runtime of its own asks its own seat for the very first view it
// waits on (novox 2026-11-16: the indexer's runtime refused its own mesh-catalog.revision for ever). The runner's own tools
// are none. Each such call is logged once, with its caller.
func (g *Gate) calledByModule(c Call) bool {
if c.Own || strings.HasPrefix(c.Caller, "person.") {
return false
}
node, module, ok := strings.Cut(c.Caller, ".")
if !ok || node == "" || module == "" || strings.Contains(module, ".") || (module == g.d.Runner && g.d.Runner != c.Module) {
return false
}
g.mu.Lock()
if g.unmarkedLogged == nil {
g.unmarkedLogged = map[string]bool{}
}
first := !g.unmarkedLogged[c.Address]
g.unmarkedLogged[c.Address] = true
g.mu.Unlock()
if first {
g.d.Logf("[approval] %s called by %s: run unmarked before the catalog half is read", c.Address, c.Caller)
}
return true
}
// marksOf is the module's marks as the catalog half has them now; before its first view, a start read runs unmarked
// (A-387), and so does a module tool another module calls (A-389); any other waits for it within RecordWithin and is then judged, refused naming
// mesh_first_views when it is still unread (§19).
func (g *Gate) marksOf(c Call) (Marks, error) {
if g.d.Catalog == nil {
return Marks{}, g.refused(c, "", "the tool runner reads no catalog half, so its marks are not read")
}
if err := g.d.Catalog.Ready(); err != nil {
if g.startRead(c) || g.calledByModule(c) {
return Marks{Module: c.Module}, nil
}
if g.d.Wait != nil {
err = g.d.Wait(RecordWithin)
}
if err != nil {
return Marks{}, fmt.Errorf("Not run: the marks of %s are not read yet (%v); mesh_first_views says what the tool runner "+
"waits for", c.Module, err)
}
}
m := Derive(g.d.Catalog, c.Module, c.Fingerprint)
g.checkServed(m)
return m, nil
}
func (g *Gate) unservedOf(module string) []string {
g.mu.Lock()
defer g.mu.Unlock()
return g.unserved[module]
}
// markedSince is the revision a call was marked at when the same caller last called it unmarked (§12), empty otherwise.
func (g *Gate) markedSince(c Call, marked bool, revision string) string {
key := c.Caller + " " + c.Address
g.mu.Lock()
defer g.mu.Unlock()
if g.unmarkedAt == nil {
g.unmarkedAt = map[string]bool{}
}
was := g.unmarkedAt[key]
if marked {
delete(g.unmarkedAt, key)
if was {
return revision
}
return ""
}
g.unmarkedAt[key] = true
return ""
}
// judge is the same call made again, judged on the router's own record of its act's newest ask (§14 to §17).
func (g *Gate) judge(c Call, act operatorask.Act, digest, id, since string, run func() (any, error)) (any, error) {
rec, err := g.record(c.Module, id)
if err != nil {
return nil, g.refused(c, digest, "the router's record of its ask "+id+" cannot be read: "+err.Error())
}
if rec == nil {
return nil, g.firstAnswer(c, id, digest, since) // §17: not yet taken by the router
}
again := func(why string) (any, error) {
err := g.raise(c, act, digest, since)
return nil, fmt.Errorf("The ask %s %s; asked again. %w", id, why, err)
}
switch rec.State {
case "open", "held-back":
return nil, fmt.Errorf("Not run: still waiting for the operator's answer to ask %s (fingerprint %s); it expires at %s",
id, Fingerprint(digest), rec.Expires)
case "refused":
return nil, g.refused(c, digest, "the router refused its ask "+id+": "+orNone(rec.Answer))
case "chosen":
default: // expired, cancelled, replaced: each asks again (§15, §17)
return again("ended " + rec.State)
}
w := rec.Warrant
if w == nil {
return nil, g.refused(c, digest, "the router's record of its ask "+id+" is chosen and carries no warrant")
}
at, _ := parseTime(w.At)
if w.Option == OptionDecline {
if until := at.Add(DeclinedFor); g.d.Now().Before(until) {
return nil, fmt.Errorf("Not run: the operator declined ask %s; the same call is not asked again until %s",
id, formatTime(until))
}
return again("was declined more than 10 minutes ago")
}
if g.d.Now().After(at.Add(RunWithin)) {
return again("was approved too long ago (more than 30 minutes)")
}
var answer any
var ran error
h, err := g.d.Channel.Claim(c.Module, *w, act, func() { answer, ran = run() })
switch {
case h == ClaimDone:
return again("was already run")
case h == ClaimRefused:
return nil, g.refused(c, digest, "its warrant does not hold: "+errText(err))
case h == "" && err != nil:
g.stateFailed(err)
return nil, g.refused(c, digest, "its state "+StateName+" cannot be written: "+err.Error())
case err != nil: // the act about to run is not the one the option bound: nothing runs, the warrant stays used
return nil, g.refused(c, digest, err.Error())
}
return g.ran(c, id, rec, answer, ran)
}
func errText(err error) string {
if err == nil {
return "no reason given"
}
return err.Error()
}
var (
bootOnce sync.Once
boot map[string]seatdef.Definition
)
// bootstrapped is the foundation's definition of a seat, which every runner knows before any seat list is read.
func bootstrapped(seat string) (seatdef.Definition, bool) {
bootOnce.Do(func() {
boot = map[string]seatdef.Definition{}
for _, d := range seatlist.Bootstrapped() {
boot[d.Seat] = d
}
})
d, ok := boot[seat]
return d, ok
}