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
224 lines
12 KiB
Go
224 lines
12 KiB
Go
package approval
|
||
|
||
import (
|
||
"encoding/json"
|
||
"errors"
|
||
"fmt"
|
||
"strings"
|
||
"testing"
|
||
"time"
|
||
|
||
"git.novox.be/novox/mesh-sdk/go/operatorask"
|
||
"git.novox.be/novox/mesh-sdk/go/seatdef"
|
||
"git.novox.be/novox/mesh-sdk/go/seatlist"
|
||
)
|
||
|
||
// call is a fixture call of the marked tool wipe.
|
||
func call(name, args string) Call {
|
||
return Call{Address: "shell." + name, Module: "shell", Name: name, Fingerprint: "src2:1", Caller: "agent", Args: json.RawMessage(args),
|
||
Description: "Wipes a scratch directory.\nMore words."}
|
||
}
|
||
|
||
// Design 46 7-approval-mark §8, §10, §11: the act's fields give each call its digest, and the ask shows a call's arguments
|
||
// in their shape and its headline cut (the gate's other cases run on the tool runner's fixture mesh, node-tools
|
||
// internal/approval).
|
||
func TestTheActAndItsAsk(t *testing.T) {
|
||
base := call("wipe", `{"dir":"scratch","n":1}`)
|
||
digest := func(c Call) string {
|
||
act, err := ActOf(c, "n1")
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
d, _ := operatorask.ActDigest(act)
|
||
return d
|
||
}
|
||
act, _ := ActOf(base, "n1")
|
||
want := operatorask.Act{"address": "shell.wipe", "node": "n1", "module": "shell", "tool": "wipe", "build": "src2:1",
|
||
"arg.dir": "scratch", "arg.n": "1"}
|
||
if len(act) != len(want) {
|
||
t.Fatalf("act %v", act)
|
||
}
|
||
for k, v := range want {
|
||
if act[k] != v {
|
||
t.Errorf("act[%s] = %q, want %q", k, act[k], v)
|
||
}
|
||
}
|
||
seen := map[string]string{digest(base): "the call"}
|
||
for name, c := range map[string]Call{
|
||
"an omitted argument": call("wipe", `{"dir":"scratch"}`),
|
||
"another number": call("wipe", `{"dir":"scratch","n":2}`),
|
||
"an object": call("wipe", `{"dir":{"name":"scratch"},"n":1}`),
|
||
"a changed build": func() Call { c := base; c.Fingerprint = "src2:2"; return c }(),
|
||
"another node's seat": {Address: "node-login-shell.execute", Module: "shell", Seat: "node-login-shell", Name: "execute", Fingerprint: "src2:1"},
|
||
} {
|
||
d := digest(c)
|
||
if other, dup := seen[d]; dup {
|
||
t.Errorf("%s gives the digest of %s", name, other)
|
||
}
|
||
seen[d] = name
|
||
}
|
||
if got, _ := Canonical(json.RawMessage(`{"b":[1.0,2.5e-7,1e21],"a":"é<"}`)); got != `{"a":"é<","b":[1,2.5e-7,1e+21]}` {
|
||
t.Errorf("canonical form %s", got)
|
||
}
|
||
// §11: a path, an address and a long argument are shown in their shape and cut.
|
||
text, whole := shown(json.RawMessage(`{"dir":"/home/someone/scratch","host":"10.0.0.7","ok":"yes"}`))
|
||
if whole || text != "dir: ‹path›\nhost: ‹address›\nok: yes" {
|
||
t.Errorf("shown %q whole %v", text, whole)
|
||
}
|
||
long, _ := json.Marshal(map[string]string{"text": strings.Repeat("word ", 500)})
|
||
if text, whole := shown(long); whole || !strings.HasSuffix(text, "NOT SHOWN IN FULL") || len(text) > PartBytes+len("\nNOT SHOWN IN FULL") {
|
||
t.Errorf("a long argument shown whole %v, %d bytes", whole, len(text))
|
||
}
|
||
// §10: the headline, cut with … to 60 characters, ending Part shown.
|
||
for _, tc := range []struct{ name, node, want string }{
|
||
{"wipe", "n1", "Run wipe on n1?"},
|
||
{strings.Repeat("x", 50), "n1", "Run " + strings.Repeat("x", 50) + "?"},
|
||
{strings.Repeat("y", 80), "n1", "Run " + strings.Repeat("y", 54) + "…?"},
|
||
} {
|
||
if got := headlineOf("Run", tc.name, tc.node, true); got != tc.want || len([]rune(got)) > 60 {
|
||
t.Errorf("headline %q, want %q", got, tc.want)
|
||
}
|
||
}
|
||
if got := headlineOf("Run", "wipe", "n1", false); got != "Run wipe on n1? Part shown" {
|
||
t.Errorf("headline %q", got)
|
||
}
|
||
}
|
||
|
||
// unread is a catalog half whose first view is not read yet, beside the seat definitions the runner holds.
|
||
type unread struct{ defs map[string]seatdef.Definition }
|
||
|
||
func (unread) Ready() error { return errors.New("the registrations are not answered") }
|
||
func (unread) Revision() string { return "" }
|
||
func (unread) Served(string) (Entry, bool, string) { return Entry{}, false, "not read" }
|
||
func (unread) Newest(string) (Entry, bool) { return Entry{}, false }
|
||
func (u unread) Definitions() map[string]seatdef.Definition { return u.defs }
|
||
func (unread) Entries() []Entry { return nil }
|
||
|
||
// Design 47 G lead answers A-387, A-388, A-389 (and A-377): while its catalog half is unread, the gate lets through every
|
||
// seat verb the definition the runner holds (here the bootstrapped list) classes `read` explicitly and does not mark
|
||
// needs-approval, a module's own first-view tools, and a module tool another module's account calls, logged once per tool;
|
||
// it holds every other call as before.
|
||
func TestAnUnreadCatalogHalfLetsItsOwnReadsThrough(t *testing.T) {
|
||
defs := map[string]seatdef.Definition{}
|
||
for _, d := range seatlist.Bootstrapped() {
|
||
defs[d.Seat] = d
|
||
}
|
||
defs["marked"] = seatdef.Definition{Seat: "marked", Verbs: []seatdef.Verb{{Name: "look", Class: seatdef.ClassRead, NeedsApproval: true}}}
|
||
waited := 0
|
||
var logged []string
|
||
g := New(Deps{Node: "n1", Runner: "node-tools", Catalog: unread{defs: defs}, Wait: func(time.Duration) error {
|
||
waited++
|
||
return errors.New("still unread")
|
||
}, Logf: func(f string, a ...any) { logged = append(logged, fmt.Sprintf(f, a...)) }})
|
||
for _, tc := range []struct {
|
||
seat, name string
|
||
runs bool
|
||
env map[string]string
|
||
caller string
|
||
own bool
|
||
}{
|
||
{"mesh-catalog", "registrations", true, nil, "", false},
|
||
{"mesh-catalog", "revision", true, nil, "", false},
|
||
{"mesh-catalog", "entry", true, nil, "", false},
|
||
{"mesh-delivery", "source", true, nil, "", false},
|
||
{"mesh-broker", "list", true, nil, "", false},
|
||
{"mesh-catalog", "seat-definitions", true, nil, "", false},
|
||
{"mesh-catalog", "modules", true, nil, "", false},
|
||
{"git", "pull", true, nil, "", false},
|
||
{"git", "merge-pull", false, nil, "", false}, // an act
|
||
{"mesh-broker", "create", false, nil, "", false}, // an act
|
||
{"mesh-delivery-checker", "try", false, nil, "", false}, // names no class, so no read (A-388)
|
||
{"operator-channel", "tell", false, nil, "", false}, // names no class
|
||
{"marked", "look", false, nil, "", false}, // a read marked needs-approval
|
||
{"unheld", "list", false, nil, "", false}, // a seat the runner holds no definition of
|
||
{"mesh-broker", "events", false, nil, "", false},
|
||
// A-377: a module's own first-view tool runs unread, an ordinary tool of the same module stays held.
|
||
{"", "modules", true, map[string]string{"MESH_VIEW_CALLERS_MODULES": "grants"}, "", false},
|
||
{"", "first-things", true, map[string]string{"MESH_VIEW_CALLERS_FIRST_THINGS": "grants"}, "", false},
|
||
{"", "read_modules", false, map[string]string{"MESH_VIEW_CALLERS_MODULES": "grants"}, "", false},
|
||
{"mesh-catalog", "register", false, nil, "", false},
|
||
{"mesh-delivery", "registrations", false, nil, "", false},
|
||
{"", "registrations", false, nil, "", false}, // a module's tool of that name is no seat verb
|
||
{"", "revision", false, nil, "", false},
|
||
// A-389: a module tool another module's account calls runs unmarked; called otherwise it is held.
|
||
{"", "bridge_merged_message", true, nil, "n2.mesh-delivery-forge-bridge", false},
|
||
{"", "bridge_merged_message", true, nil, "n1.mesh-delivery-forge-bridge", false}, // logged once
|
||
{"", "read_modules", true, nil, "n1.mesh-delivery-runner", false},
|
||
{"", "bridge_merged_message", false, nil, "n1.node-tools", false}, // the console's and its sessions' account
|
||
{"", "bridge_merged_message", false, nil, "n2.node-tools", false}, // another machine's console
|
||
{"", "bridge_merged_message", false, nil, "person.operator", false}, // a person
|
||
{"", "bridge_merged_message", false, nil, "controller", false},
|
||
{"", "bridge_merged_message", false, nil, "unproven", false},
|
||
{"", "bridge_merged_message", true, nil, "n1.mesh-delivery-indexer", false}, // the tool's own module's account (A-433)
|
||
{"", "ledger_repair", false, nil, "n1.mesh-delivery-runner", true}, // the runner's own tool
|
||
{"git", "merge-pull", true, nil, "n1.mesh-delivery-runner", false}, // a module's account, a seat verb too (A-433)
|
||
{"git", "merge-pull", false, nil, "n1.node-tools", false}, // the console's account stays held
|
||
} {
|
||
ran := false
|
||
address := tc.seat + "." + tc.name
|
||
if tc.seat == "" {
|
||
address = "mesh-delivery-indexer." + tc.name
|
||
}
|
||
caller := tc.caller
|
||
if caller == "" {
|
||
caller = "node-tools"
|
||
}
|
||
_, err := g.Run(Call{Address: address, Module: "mesh-delivery-indexer", Seat: tc.seat, Name: tc.name, Fingerprint: "fp1",
|
||
Caller: caller, Env: tc.env, Own: tc.own}, func() (any, error) { ran = true; return "ok", nil })
|
||
if ran != tc.runs || (err == nil) != tc.runs {
|
||
t.Errorf("%s: ran %v (%v), want ran %v", address, ran, err, tc.runs)
|
||
}
|
||
if !tc.runs && (err == nil || !strings.Contains(err.Error(), "are not read yet")) {
|
||
t.Errorf("%s: refused with %v, want the marks not read yet", address, err)
|
||
}
|
||
}
|
||
if waited != 19 {
|
||
t.Errorf("the other calls waited for the catalog half %d times, want 19 (the start reads wait for nothing)", waited)
|
||
}
|
||
want := []string{
|
||
"[approval] mesh-delivery-indexer.bridge_merged_message called by n2.mesh-delivery-forge-bridge: run unmarked before the catalog half is read",
|
||
"[approval] mesh-delivery-indexer.read_modules called by n1.mesh-delivery-runner: run unmarked before the catalog half is read",
|
||
"[approval] git.merge-pull called by n1.mesh-delivery-runner: run unmarked before the catalog half is read",
|
||
}
|
||
if strings.Join(logged, "\n") != strings.Join(want, "\n") {
|
||
t.Errorf("logged %q, want each module tool run unmarked once with its caller %q", logged, want)
|
||
}
|
||
// A-433: a module's runtime of its own (its runner the module itself) runs its own account's seat call while unread, as the
|
||
// indexer's runtime asks its own mesh-catalog.revision for the first view it waits on; a seat the definition does not class
|
||
// read, so no start read.
|
||
own := New(Deps{Node: "n1", Runner: "mesh-delivery-indexer", Catalog: unread{defs: map[string]seatdef.Definition{}},
|
||
Wait: func(time.Duration) error { return errors.New("still unread") }, Logf: func(string, ...any) {}})
|
||
ran := false
|
||
if _, err := own.Run(Call{Address: "mesh-catalog.revision", Module: "mesh-delivery-indexer", Seat: "mesh-catalog", Name: "revision",
|
||
Fingerprint: "fp1", Caller: "n1.mesh-delivery-indexer"}, func() (any, error) { ran = true; return "ok", nil }); err != nil || !ran {
|
||
t.Errorf("the module's own seat call in its own runtime: ran %v (%v), want it run", ran, err)
|
||
}
|
||
}
|
||
|
||
// The deadlock of novox 2026-11-16: the seat list is read through the seat mesh-catalog, so while the catalog half is unread
|
||
// the runner holds no definitions, and the first views it waits on (its own and another runner's) were refused for ever. The
|
||
// foundation's bootstrapped definitions answer for them, from whichever account asks.
|
||
func TestTheFirstViewsRunBeforeAnySeatListIsRead(t *testing.T) {
|
||
g := New(Deps{Node: "n1", Runner: "node-tools", Catalog: unread{defs: map[string]seatdef.Definition{}},
|
||
Wait: func(time.Duration) error { return errors.New("still unread") }, Logf: func(string, ...any) {}})
|
||
for _, tc := range []struct {
|
||
seat, name, caller string
|
||
runs bool
|
||
}{
|
||
{"mesh-catalog", "revision", "n1.node-tools", true},
|
||
{"mesh-catalog", "revision", "n2.node-tools", true},
|
||
{"mesh-catalog", "registrations", "person.operator", true},
|
||
{"mesh-catalog", "entry", "n1.node-tools", true},
|
||
{"mesh-catalog", "seat-definitions", "n1.node-tools", true},
|
||
{"git", "merge-pull", "n1.node-tools", false},
|
||
{"unheld", "list", "n1.node-tools", false},
|
||
} {
|
||
ran := false
|
||
_, err := g.Run(Call{Address: tc.seat + "." + tc.name, Module: "mesh-delivery-indexer", Seat: tc.seat, Name: tc.name,
|
||
Fingerprint: "fp1", Caller: tc.caller}, func() (any, error) { ran = true; return "ok", nil })
|
||
if ran != tc.runs || (err == nil) != tc.runs {
|
||
t.Errorf("%s.%s by %s: ran %v (%v), want %v", tc.seat, tc.name, tc.caller, ran, err, tc.runs)
|
||
}
|
||
}
|
||
}
|