diff --git a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/integration_test.go b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/integration_test.go index 9a7ce606..c00a478a 100644 --- a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/integration_test.go +++ b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/integration_test.go @@ -599,7 +599,7 @@ func TestWantsAreWrittenAndNoDifferenceIs(t *testing.T) { // its mark, and refuses every other account, the assigner's own included. firstViewsBeforeAnyNewFormState(t, b, map[string][]string{ "assignments": {"n1.mesh-grants", "n1.node-tools", "n1.mesh-delivery-indexer", "n1.mesh-delivery-builder", "n1.mesh-delivery-checker"}, - "seat-holders": {"n1.node-tools"}, + "seat-holders": {"n1.node-tools", "n1.mesh-delivery-builder", "n1.mesh-delivery-view"}, // A-298: the parts following it "wants": {"n1.mesh-grants"}, }) // The reader raises the finding of the first view through the module's finding writer (A-26), its id escaped as a key's name. diff --git a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/main.go b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/main.go index 8362c537..fa7256de 100644 --- a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/main.go +++ b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/main.go @@ -384,6 +384,8 @@ var watched = []string{ // each change; changes that come during a pass make one more. It returns when ctx ends or a watch ends. func follow(ctx context.Context, js jetstream.JetStream, nc process.Subscriber, kvs kvSet, say func(oc.Condition) error, findings assign.Findings, bus caller, iv *catalogViews, inputs *inputKeeper, gate *seatlist.Part, logger *log.Logger, getenv func(string) string) error { w := newWorld() + // The callers' seat entries are resolved from the assigner's own seat-holders as it last read them (A-298). + gate.Holders(seatlist.HoldersIn(func() (map[string][]byte, error) { return w.read(plc.SeatHoldersBucket), nil })) dirty := make(chan struct{}, 1) wake := func() { select { diff --git a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/seat_tools_test.go b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/seat_tools_test.go index e846396e..b59d1a84 100644 --- a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/seat_tools_test.go +++ b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/seat_tools_test.go @@ -271,10 +271,14 @@ func TestTheSeatsVerbs(t *testing.T) { } s.seatList = nil t.Run("1966: the seat list's again tool answers an operator's session through the mesh MCP server", func(t *testing.T) { - // Such a session is proved as its machine's tool runner (issue 627); the healers too, no other module. - for caller, refused := range map[string]bool{"person.jochen": false, "shanks.node-tools": false, "ace.mesh-healers": false, - "ace.messenger": true} { - if err := seatlist.Refusal("assigner_seat_list_again", seatListAgainCallers, caller); (err != nil) != refused { + // Such a session is proved as its machine's tool runner (issue 627); the healers too, no other module. The list names + // the seats, resolved from the assigner's own seat-holders (A-295, A-298): a module holding neither is refused. + held := map[string][]byte{"node-tool-runner.shanks": []byte(`{"holders":[{"module":"node-tools","node":"shanks"}]}`), + "mesh-healers": []byte(`{"holders":[{"module":"mesh-delivery-watchman","node":"ace"}]}`)} + holders := seatlist.HoldersIn(func() (map[string][]byte, error) { return held, nil }) + for caller, refused := range map[string]bool{"person.jochen": false, "shanks.node-tools": false, "ace.mesh-delivery-watchman": false, + "ace.mesh-healers": true, "ace.messenger": true} { + if err := seatlist.RefusalWith("assigner_seat_list_again", seatListAgainCallers, caller, holders); (err != nil) != refused { t.Errorf("%s: refused=%v, err %v", caller, refused, err) } } diff --git a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/seatlist.go b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/seatlist.go index 0b8f66e0..cf2da054 100644 --- a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/seatlist.go +++ b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/seatlist.go @@ -10,6 +10,7 @@ import ( // seat-holders state read; until the list is read, a mesh seat the bootstrapped definitions do not name is never judged held once // (plc.CheckAssignments), and seat-holders is not written. -// A call through the mesh MCP server is proved only as its machine's tool runner (.node-tools, issue 627), so -// node-tools stands for an operator's session until a session is proved (issue 1966). -var seatListAgainCallers = []string{stdio.AnyPerson, "node-tools", "mesh-healers"} +// A call through the mesh MCP server is proved only as its machine's tool runner (issue 627), so the holders of the seat +// node-tool-runner stand for an operator's session until a session is proved (issue 1966). The list names seats, never a +// module, resolved from the assigner's own seat-holders (design 47 G A-210, A-295, A-298). +var seatListAgainCallers = []string{stdio.AnyPerson, stdio.CallerSeatPrefix + "node-tool-runner", stdio.CallerSeatPrefix + "mesh-healers"} diff --git a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/tools.go b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/tools.go index ce66162a..c92a7566 100644 --- a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/tools.go +++ b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/tools.go @@ -1,6 +1,8 @@ package main import ( + mma "git.novox.be/novox/mesh-contracts/go/seats/meshmoduleassigner" + "encoding/json" "errors" "fmt" @@ -413,6 +415,8 @@ func serveTools() error { return stdio.SeatPublish(wantsEventSubject(e), b, id) }) wants.watched = stdio.NewWatchedStates() // the large states read whole from one watched copy (ADR 0580 clause 3) + // The callers' seat entries are resolved from the assigner's own seat-holders, its watched copy (A-298). + gate.Holders(seatlist.HoldersIn(func() (map[string][]byte, error) { return wants.watched.Values(mma.StateSeatHolders) })) wants.revisions = runtimeObjects{busstate.ObjectsOf(assigngraph.RevisionsObjects)} tools = append(tools, wants.tools()...) tools = append(tools, inputsRead(&inputKeeper{vals: stateobjects.ForState(buckets.InputsState), now: time.Now, logf: func(string, ...any) {}})) diff --git a/modules/mesh-delivery-assigner/manifest_test.go b/modules/mesh-delivery-assigner/manifest_test.go index 6c4b465b..a3e01885 100644 --- a/modules/mesh-delivery-assigner/manifest_test.go +++ b/modules/mesh-delivery-assigner/manifest_test.go @@ -112,7 +112,7 @@ func TestManifest(t *testing.T) { t.Errorf("runs-as = %q: the bundle runs on the assigner's own account (ADR 0621 §1)", m.RunsAs) } // ADR 0621 §2: each first-view tool answers exactly its readers, never the assigner. - callers := map[string]string{"assignments": "mesh-grants,node-tools,mesh-delivery-indexer,mesh-delivery-builder,mesh-delivery-checker", "seat-holders": "node-tools", "wants": "mesh-grants"} + callers := map[string]string{"assignments": "mesh-grants,node-tools,mesh-delivery-indexer,mesh-delivery-builder,mesh-delivery-checker", "seat-holders": "node-tools,mesh-delivery-builder,mesh-delivery-view", "wants": "mesh-grants"} env := m.Build.Artifacts[0].Env for tool, list := range callers { if env[views.CallersEnv(tool)] != list || views.ParseCallers(env[views.CallersEnv(tool)]).Allows("n1."+m.Module) { diff --git a/modules/mesh-delivery-assigner/module.json b/modules/mesh-delivery-assigner/module.json index 08a3e60b..5efc0f95 100644 --- a/modules/mesh-delivery-assigner/module.json +++ b/modules/mesh-delivery-assigner/module.json @@ -248,7 +248,7 @@ ], "env": { "MESH_VIEW_CALLERS_ASSIGNMENTS": "mesh-grants,node-tools,mesh-delivery-indexer,mesh-delivery-builder,mesh-delivery-checker", - "MESH_VIEW_CALLERS_SEAT_HOLDERS": "node-tools", + "MESH_VIEW_CALLERS_SEAT_HOLDERS": "node-tools,mesh-delivery-builder,mesh-delivery-view", "MESH_VIEW_CALLERS_WANTS": "mesh-grants" } } diff --git a/modules/mesh-delivery-builder/cmd/mesh-delivery-builder-tools/main.go b/modules/mesh-delivery-builder/cmd/mesh-delivery-builder-tools/main.go index 805d2874..382bc39e 100644 --- a/modules/mesh-delivery-builder/cmd/mesh-delivery-builder-tools/main.go +++ b/modules/mesh-delivery-builder/cmd/mesh-delivery-builder-tools/main.go @@ -408,7 +408,9 @@ func main() { pk.Start(context.Background()) // The build modules are no names (design 47 G A-82): the build seats on the seat list, each held by the module the catalog // half's manifests say; the seat list and its two tools wired once by mesh-sdk (§204). - gate := seatlist.NewPart("builder", []string{stdio.AnyPerson, "node-tools", "mesh-healers"}) + // Its callers name seats, never a module, resolved from the seat-holders view the catalog half's claimant serves (A-295, A-298). + gate := seatlist.NewPart("builder", []string{stdio.AnyPerson, stdio.CallerSeatPrefix + "node-tool-runner", stdio.CallerSeatPrefix + "mesh-healers"}) + gate.Holders(branches.SeatHolders(context.Background())) go func() { <-stdio.Served() gate.Follow(seatlist.StdioBus(), nil, func(f string, a ...any) { fmt.Fprintf(os.Stderr, "[mesh-delivery-builder-tools] "+f+"\n", a...) }) diff --git a/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/catalog.go b/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/catalog.go index 7e975128..e6f4d2db 100644 --- a/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/catalog.go +++ b/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/catalog.go @@ -161,6 +161,17 @@ func (w sweepWiring) Adopt(f *views.Follower, seatName, event string, hear func( return nil } +// Follow follows a plain view through the wrapped wiring (meshcatalogclient's SeatHolders, A-298). +func (w sweepWiring) Follow(owner, state, tool string) (func() map[string]json.RawMessage, error) { + f, ok := w.Wiring.(interface { + Follow(owner, state, tool string) (func() map[string]json.RawMessage, error) + }) + if !ok { + return nil, fmt.Errorf("the builder's reader follows no plain view of %s.%s", owner, state) + } + return f.Follow(owner, state, tool) +} + func (w sweepWiring) Drop(owner, state string) error { err := w.Wiring.Drop(owner, state) w.half.dropped(state) diff --git a/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/main.go b/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/main.go index d015baad..ed90d144 100644 --- a/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/main.go +++ b/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/main.go @@ -119,6 +119,7 @@ func run(ctx context.Context, getenv func(string) string, logOut io.Writer) erro // The build modules are no names (design 47 G A-82): the holders of the build seats, the seat list's definitions and the // catalog half's claims, found at each use. seatGate := newSeatGate() + seatGate.Holders(branches.SeatHolders(ctx)) seats := buildrun.NewBuildSeats(seatGate.definitions, versions.HolderOf(branches)) bound := defaultStepBound if v := strings.TrimSpace(getenv("MESH_BUILDER_STEP_BOUND")); v != "" { diff --git a/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/seatlist.go b/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/seatlist.go index 7d96edc9..73afe11a 100644 --- a/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/seatlist.go +++ b/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/seatlist.go @@ -9,8 +9,9 @@ import ( // The builder reads the seat list as every part does, through mesh-sdk's one wiring (go/seatlist.Part; design 47 G §204): its // definitions name the build seats (the seats whose acting rule is the step build), its tools `builder_seat_list` and -// `builder_seat_list_again`. A call through the mesh MCP server is proved only as its machine's tool runner (issue 627). -var seatListAgainCallers = []string{stdio.AnyPerson, "node-tools", "mesh-healers"} +// `builder_seat_list_again`. A call through the mesh MCP server is proved only as its machine's tool runner (issue 627). The +// list names seats, never a module, resolved by the holder function meshcatalogclient's SeatHolders answers (A-295, A-298). +var seatListAgainCallers = []string{stdio.AnyPerson, stdio.CallerSeatPrefix + "node-tool-runner", stdio.CallerSeatPrefix + "mesh-healers"} // seatGate is the builder's wiring of the seat list. type seatGate struct{ *seatlist.Part } diff --git a/modules/mesh-delivery-builder/internal/versions/entrynames.go b/modules/mesh-delivery-builder/internal/versions/entrynames.go index 71b6e8d9..d3761808 100644 --- a/modules/mesh-delivery-builder/internal/versions/entrynames.go +++ b/modules/mesh-delivery-builder/internal/versions/entrynames.go @@ -2,15 +2,11 @@ package versions import ( "context" - "encoding/json" - "slices" "sort" "strings" - "sync" "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshcatalogclient" "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog" - "git.novox.be/novox/mesh-sdk/go/catalogue" ) // Entries are the catalog graph entries of the halves the builder follows, one per module and watched branch (design 47 G §348, @@ -85,55 +81,10 @@ func EntriesOf(branches *meshcatalogclient.Branches) Entries { } } -// HolderOf finds the module that holds a seat: the one whose entry in the first followed branch's half (the trunk's) has a -// manifest that claims it (design 47 G A-82; the seat list answers seat definitions alone, §527). More than one claimant, or -// none, is no holder. +// HolderOf finds the module that holds a seat: its claimant in the trunk's half, as meshcatalogclient's Branches.Claimant +// answers it (design 47 G A-82, A-298). func HolderOf(branches *meshcatalogclient.Branches) func(seat string) (string, bool) { - var mu sync.Mutex - claims := map[string][]string{} // manifest digest -> the seats it claims - claimed := func(e meshcatalog.CatalogEntry) []string { - mu.Lock() - defer mu.Unlock() - if seats, ok := claims[e.ManifestDigest]; ok && e.ManifestDigest != "" { - return seats - } - var seats []string - if raw, err := json.Marshal(e.Manifest); err == nil { - if mf, err := catalogue.Decode(raw); err == nil { - for _, c := range mf.Claims { - seats = append(seats, c.Name) - } - } - } - if e.ManifestDigest != "" { - claims[e.ManifestDigest] = seats - } - return seats - } - return func(seat string) (string, bool) { - followed := branches.Followed() - if len(followed) == 0 { - return "", false - } - cat, ok := branches.Catalog(followed[0]) - if !ok { - return "", false - } - half, ok := cat.View() - if !ok { - return "", false - } - var holders []string - for _, e := range half.Entries { - if slices.Contains(claimed(e), seat) { - holders = append(holders, e.Module) - } - } - if len(holders) != 1 { - return "", false - } - return holders[0], true - } + return branches.Claimant } // RequirementsFrom answers the versions of a package the entries of every followed half require, each once, in order: what a diff --git a/modules/mesh-delivery-builder/manifest_test.go b/modules/mesh-delivery-builder/manifest_test.go index 01fdc05f..4d126c2d 100644 --- a/modules/mesh-delivery-builder/manifest_test.go +++ b/modules/mesh-delivery-builder/manifest_test.go @@ -161,6 +161,7 @@ func TestManifestDeclarations(t *testing.T) { // 1607/AC1 "go-package-registry.kept", "go-package-registry.published", "go-package-registry.refused", "go-package-registry.removed", "mesh-artifact-sweep.artifacts-gone", "mesh-catalog.manifest-accepted", "mesh-catalog.seat-definition-changed", "mesh-catalog.seat-definition-removed", "mesh-catalog.version-released", "mesh-catalog.versions-filled", "mesh-conditions.condition-cleared", "mesh-conditions.findings-asked", "seat:mesh-module-assigner.views.assignments", + "seat:mesh-module-assigner.views.seat-holders", // A-298: the claimant's seat-holders view resolves the callers' seat entries "mesh-catalog.catalog-revision", "mesh-catalog.registration-changed", "mesh-delivery.source-moved", "node-build-go.toolchain-reported", "node-build-node.toolchain-reported", "node-engine.manifest-reads-changed", "node-engine.running", "npm-package-registry.kept", "npm-package-registry.published", "npm-package-registry.refused", "npm-package-registry.removed", "oci-artifact-registry.kept", "oci-artifact-registry.published", "oci-artifact-registry.refused", "oci-artifact-registry.removed", "operator-channel.decided"}; !reflect.DeepEqual(sorted(m.Consumes), sorted(want)) { diff --git a/modules/mesh-delivery-builder/module.json b/modules/mesh-delivery-builder/module.json index 1b724b7f..aa8f9443 100644 --- a/modules/mesh-delivery-builder/module.json +++ b/modules/mesh-delivery-builder/module.json @@ -73,6 +73,7 @@ "oci-artifact-registry.refused", "oci-artifact-registry.removed", "seat:mesh-module-assigner.views.assignments", + "seat:mesh-module-assigner.views.seat-holders", "mesh-catalog.catalog-revision", "mesh-catalog.registration-changed", "mesh-delivery.source-moved" diff --git a/modules/mesh-delivery-contracts/go/clients/meshcatalogclient/branches.go b/modules/mesh-delivery-contracts/go/clients/meshcatalogclient/branches.go index 573f4d81..55a222bb 100644 --- a/modules/mesh-delivery-contracts/go/clients/meshcatalogclient/branches.go +++ b/modules/mesh-delivery-contracts/go/clients/meshcatalogclient/branches.go @@ -7,6 +7,7 @@ import ( "fmt" "slices" "sync" + "sync/atomic" "time" seat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog" @@ -67,6 +68,10 @@ type Branches struct { Registrations *Registrations // Entries is the cache of the entries read by fingerprint. Entries *Entries + // claims are the seats each manifest digest claims (Claimant), holders the part's follow of the seat-holders view (SeatHolders). + claimsMu sync.Mutex + claims map[string][]string + holders atomic.Pointer[seatHolders] // Changed, when not nil, is told of each change to a branch's view (module, its entry, deleted), set before Wire. Changed func(branch, module string, entry json.RawMessage, deleted bool, at time.Time) // Adopted, when not nil, is told on a goroutine of its own of each branch whose view was adopted, with that view (a part that waits for @@ -243,6 +248,7 @@ func (b *Branches) reconcile() { } branch := want[e] c.Follower.Changed = func(module string, entry json.RawMessage, deleted bool, at time.Time) { + b.holders.Load().changed() if b.Changed != nil { b.Changed(branch, module, entry, deleted, at) } diff --git a/modules/mesh-delivery-contracts/go/clients/meshcatalogclient/branches_test.go b/modules/mesh-delivery-contracts/go/clients/meshcatalogclient/branches_test.go index 0b44efa6..66248302 100644 --- a/modules/mesh-delivery-contracts/go/clients/meshcatalogclient/branches_test.go +++ b/modules/mesh-delivery-contracts/go/clients/meshcatalogclient/branches_test.go @@ -23,6 +23,26 @@ type fakeWiring struct { adopted map[string]*views.Follower hears map[string]func(context.Context, []byte) error dropped []string + // follows are the plain views followed (SeatHolders), values what each answers. + follows []string + values map[string]map[string]json.RawMessage + followed chan string +} + +func (w *fakeWiring) Follow(owner, state, tool string) (func() map[string]json.RawMessage, error) { + w.mu.Lock() + defer w.mu.Unlock() + name := owner + "." + state + w.adopted[name] = &views.Follower{Owner: owner, State: state, Tool: tool} + w.follows = append(w.follows, tool) + if w.followed != nil { + w.followed <- owner + } + return func() map[string]json.RawMessage { + w.mu.Lock() + defer w.mu.Unlock() + return w.values[owner] + }, nil } func (w *fakeWiring) Adopt(f *views.Follower, _, _ string, hear func(context.Context, []byte) error) error { @@ -74,6 +94,8 @@ type fakeSeats struct { version int64 entries map[string]seat.CatalogEntry entryAsk int + // trunk, when set, is the revision answered for every branch. + trunk *seat.RevisionAnswer } func (f *fakeSeats) call(key string, body any) (json.RawMessage, error) { @@ -89,6 +111,9 @@ func (f *fakeSeats) call(key string, body any) (json.RawMessage, error) { } return json.Marshal(a) case RevisionVerb: + if f.trunk != nil { + return json.Marshal(f.trunk) + } return json.Marshal(seat.RevisionAnswer{NoneYet: true}) case EntryVerb: f.entryAsk++ @@ -272,3 +297,64 @@ func TestEntriesAreAskedOnceAnsweredOrNotFound(t *testing.T) { t.Fatalf("a call that reached no holder: rows %+v", rows) } } + +// A-298 (§717, A-295): the part's holder function follows the seat-holders view of the module the trunk's half names as the +// claimant of mesh-module-assigner, asked of its tool seat-holders; a revision naming another claimant drops it and follows the +// new one's; nothing is followed while no claimant is named. +func TestSeatHoldersFollowsTheClaimantOfTheAssignerSeat(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + claims := func(rev int64, assigner string) *seat.RevisionAnswer { + return &seat.RevisionAnswer{Revision: &seat.CatalogRevision{Kind: KindTrunk, Branch: "main", Revision: rev}, Entries: []seat.CatalogEntry{ + {Module: assigner, ManifestDigest: assigner, Manifest: map[string]any{"module": assigner, "claims": []any{map[string]any{"name": "mesh-module-assigner", "scope": "mesh"}}}}, + {Module: "app", ManifestDigest: "app", Manifest: map[string]any{"module": "app"}}}} + } + seats := &fakeSeats{source: []string{"main"}, regs: map[string][]string{}, version: 1} + w := &fakeWiring{adopted: map[string]*views.Follower{}, hears: map[string]func(context.Context, []byte) error{}, + values: map[string]map[string]json.RawMessage{ + "asg-a": {"node-tool-runner.ace": json.RawMessage(`{"holders":[{"module":"node-tools","node":"ace"}]}`)}, + "asg-b": {"node-tool-runner.ace": json.RawMessage(`{"holders":[{"module":"tool-runner-2","node":"ace"}]}`)}}, + followed: make(chan string, 4)} + b, err := NewBranches(w, seats.call) + if err != nil { + t.Fatal(err) + } + holders := b.SeatHolders(ctx) + src, _ := w.get("mesh-delivery.source") + if err := src.Start(ctx); err != nil { + t.Fatal(err) + } + if got := holders("node-tool-runner"); len(got) != 0 { + t.Fatalf("holders before any claimant = %v", got) + } + // The follow runs on its own goroutine: each Follow is told, and the holder function answers once that follow is held. + answered := func(owner, want string) { + t.Helper() + if got := <-w.followed; got != owner { + t.Fatalf("followed %s, want %s", got, owner) + } + if got := holders("node-tool-runner"); !slices.Equal(got, []string{want}) { + t.Fatalf("holders = %v, want %s", got, want) + } + } + main, _ := w.get("mesh-catalog.catalog-revisions.main") + seats.mu.Lock() + seats.trunk = claims(1, "asg-a") + seats.mu.Unlock() + if err := main.Start(ctx); err != nil { + t.Fatal(err) + } + answered("asg-a", "node-tools") + seats.mu.Lock() + seats.trunk = claims(2, "asg-b") + seats.mu.Unlock() + if err := main.Again(ctx, "a revision naming another claimant"); err != nil { + t.Fatal(err) + } + answered("asg-b", "tool-runner-2") + w.mu.Lock() + defer w.mu.Unlock() + if !slices.Equal(w.follows, []string{"asg-a.seat-holders", "asg-b.seat-holders"}) || !slices.Equal(w.dropped, []string{"asg-a.seat-holders"}) { + t.Errorf("followed %v and dropped %v, want asg-a's then asg-b's, asg-a's dropped", w.follows, w.dropped) + } +} diff --git a/modules/mesh-delivery-contracts/go/clients/meshcatalogclient/claimant.go b/modules/mesh-delivery-contracts/go/clients/meshcatalogclient/claimant.go new file mode 100644 index 00000000..403b870f --- /dev/null +++ b/modules/mesh-delivery-contracts/go/clients/meshcatalogclient/claimant.go @@ -0,0 +1,163 @@ +package meshcatalogclient + +import ( + "context" + "encoding/json" + "slices" + "sync" + + seat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog" + "git.novox.be/novox/mesh-sdk/go/catalogue" + "git.novox.be/novox/mesh-sdk/go/seatlist" +) + +// seatHoldersState is the state of the seat that places modules (seatlist.HolderSeat) its definition offers as a view: who holds +// which seat, keyed `` and `.` (§616, §699). +const seatHoldersState = "seat-holders" + +// Claimant is the module that claims seat: the one whose entry in the first followed branch's half (the trunk's) has a manifest +// that claims it (design 47 G A-82, §614, A-298). More than one claimant, or none, is none. +func (b *Branches) Claimant(seatName string) (string, bool) { + followed := b.Followed() + if len(followed) == 0 { + return "", false + } + cat, ok := b.Catalog(followed[0]) + if !ok { + return "", false + } + half, ok := cat.View() + if !ok { + return "", false + } + var holders []string + for _, e := range half.Entries { + if slices.Contains(b.claimed(e), seatName) { + holders = append(holders, e.Module) + } + } + if len(holders) != 1 { + return "", false + } + return holders[0], true +} + +// claimed is the seats an entry's manifest claims, decoded once per manifest digest. +func (b *Branches) claimed(e seat.CatalogEntry) []string { + b.claimsMu.Lock() + defer b.claimsMu.Unlock() + if seats, ok := b.claims[e.ManifestDigest]; ok && e.ManifestDigest != "" { + return seats + } + var seats []string + if raw, err := json.Marshal(e.Manifest); err == nil { + if mf, err := catalogue.Decode(raw); err == nil { + for _, c := range mf.Claims { + seats = append(seats, c.Name) + } + } + } + if e.ManifestDigest != "" { + if b.claims == nil { + b.claims = map[string][]string{} + } + b.claims[e.ManifestDigest] = seats + } + return seats +} + +// viewFollowing is a Wiring that also follows an owner's state as a plain view (Reader.Follow, ProcessReader.Follow), and answers +// the keys it last heard. +type viewFollowing interface { + Follow(owner, state, tool string) (func() map[string]json.RawMessage, error) +} + +func (w readerWiring) Follow(owner, state, tool string) (func() map[string]json.RawMessage, error) { + f := w.r.Follow(owner, state, tool) + return f.Values, nil +} + +func (w processWiring) Follow(owner, state, tool string) (func() map[string]json.RawMessage, error) { + p, err := w.r.Follow(owner, state, tool) + if err != nil { + return nil, err + } + return p.Follower().Values, nil +} + +// seatHolders is the part's follow of the claimant's seat-holders view: the claimant followed and what it answers. +type seatHolders struct { + mu sync.Mutex + owner string + values func() map[string]json.RawMessage + kick chan struct{} +} + +// SeatHolders is the part's holder function (seatlist.Part.Holders, design 47 G A-295, A-298, §717): it follows the view +// `seat-holders` of the module the catalog half names as the claimant of the seat that places modules (seatlist.HolderSeat), its +// first view asked of the claimant's tool `seat-holders`, and at each catalog change naming another claimant drops that view and +// follows the new one's (§532); with no claimant it keeps what it has. Nothing is asked on a timer. The part's manifest consumes +// `seat:mesh-module-assigner.views.seat-holders`. A Wiring that follows no plain view answers no holders, said in the log. +func (b *Branches) SeatHolders(ctx context.Context) func(seat string) []string { + sh := &seatHolders{kick: make(chan struct{}, 1)} + b.holders.Store(sh) + fw, ok := b.w.(viewFollowing) + if !ok { + b.logf("meshcatalogclient: this part's reader follows no plain view, so no seat's holders are known") + } else { + go func() { + for { + select { + case <-ctx.Done(): + return + case <-sh.kick: + } + b.refollow(sh, fw) + } + }() + sh.changed() + } + return seatlist.HoldersIn(func() (map[string]json.RawMessage, error) { + sh.mu.Lock() + values := sh.values + sh.mu.Unlock() + if values == nil { + return nil, nil + } + return values(), nil + }) +} + +// changed asks the follow to read the claimant again, without blocking the caller (a view's change is told under its lock). +func (sh *seatHolders) changed() { + if sh == nil { + return + } + select { + case sh.kick <- struct{}{}: + default: + } +} + +// refollow follows the claimant's seat-holders view when the claimant is another than the one followed. +func (b *Branches) refollow(sh *seatHolders, fw viewFollowing) { + claimant, ok := b.Claimant(seatlist.HolderSeat) + sh.mu.Lock() + defer sh.mu.Unlock() + if !ok || claimant == sh.owner { + return + } + if sh.owner != "" { + if err := b.w.Drop(sh.owner, seatHoldersState); err != nil { + b.logf("meshcatalogclient: the seat-holders view of %s is not dropped: %v", sh.owner, err) + } + sh.owner, sh.values = "", nil + } + values, err := fw.Follow(claimant, seatHoldersState, claimant+"."+seatHoldersState) + if err != nil { + b.logf("meshcatalogclient: the seat-holders view of %s is not followed: %v", claimant, err) + return + } + sh.owner, sh.values = claimant, values + b.logf("meshcatalogclient: the seat-holders view of %s, the claimant of %s, is followed", claimant, seatlist.HolderSeat) +} diff --git a/modules/mesh-delivery-view/cmd/mesh-delivery-view-tools/main.go b/modules/mesh-delivery-view/cmd/mesh-delivery-view-tools/main.go index 55d40c03..c0a6f8da 100644 --- a/modules/mesh-delivery-view/cmd/mesh-delivery-view-tools/main.go +++ b/modules/mesh-delivery-view/cmd/mesh-delivery-view-tools/main.go @@ -445,7 +445,9 @@ func main() { r.Pending = pendingOf // The build modules are no names (design 47 G A-82): the build seats on the seat list, each held by the module the seat // list names for it; the seat list and its two tools are wired once by mesh-sdk (§204). - gate := seatlist.NewPart("view", []string{stdio.AnyPerson, "node-tools", "mesh-healers"}) + // Its callers name seats, never a module, resolved from the seat-holders view the catalog half's claimant serves (A-295, A-298). + gate := seatlist.NewPart("view", []string{stdio.AnyPerson, stdio.CallerSeatPrefix + "node-tool-runner", stdio.CallerSeatPrefix + "mesh-healers"}) + gate.Holders(branches.SeatHolders(context.Background())) go func() { <-stdio.Served() gate.Follow(seatlist.StdioBus(), nil, func(f string, a ...any) { fmt.Fprintf(os.Stderr, "[mesh-delivery-view-tools] "+f+"\n", a...) }) diff --git a/modules/mesh-delivery-view/manifest_test.go b/modules/mesh-delivery-view/manifest_test.go index 06b1beed..37f76567 100644 --- a/modules/mesh-delivery-view/manifest_test.go +++ b/modules/mesh-delivery-view/manifest_test.go @@ -173,7 +173,7 @@ func TestManifestIsTakenAndReadsOnly(t *testing.T) { // 1134/AC7 if m.Version != "0.1" { t.Errorf("version = %q, want 0.1", m.Version) } - if want := []string{"mesh-conditions.findings-asked", "mesh-delivery-assigner.views.asks", "mesh-delivery-assigner.views.assignments", "mesh-delivery-assigner.views.wants", + if want := []string{"mesh-conditions.findings-asked", "mesh-delivery-assigner.views.asks", "mesh-delivery-assigner.views.assignments", "seat:mesh-module-assigner.views.seat-holders", "mesh-delivery-assigner.views.wants", "mesh-catalog.catalog-revision", "mesh-catalog.registration-changed", "mesh-delivery.source-moved", "mesh-catalog.seat-definition-changed", "mesh-catalog.seat-definition-removed", "mesh-delivery-indexer.views.candidates", "mesh-delivery-indexer.views.taken", "mesh-delivery-teller.views.answers", "mesh-wants.wait-held", "mesh-wants.wait-ended", "node-engine.wait-held", "node-engine.wait-ended", "mesh-pipeline-runner.wait-held", "mesh-pipeline-runner.wait-ended", "mesh-grants.wait-held", "mesh-grants.wait-ended", "mesh-delivery-builder.wait-held", "mesh-delivery-builder.wait-ended", diff --git a/modules/mesh-delivery-view/module.json b/modules/mesh-delivery-view/module.json index 90ee08be..5df396dc 100644 --- a/modules/mesh-delivery-view/module.json +++ b/modules/mesh-delivery-view/module.json @@ -66,6 +66,7 @@ "mesh-conditions.findings-asked", "mesh-delivery-assigner.views.asks", "mesh-delivery-assigner.views.assignments", + "seat:mesh-module-assigner.views.seat-holders", "mesh-delivery-assigner.views.wants", "mesh-catalog.catalog-revision", "mesh-catalog.registration-changed",