package main import ( "context" "encoding/json" "errors" "fmt" "sort" "strings" "sync" "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshcatalogclient" stdio "git.novox.be/novox/mesh-sdk/go" "git.novox.be/novox/mesh-sdk/go/seatlist" "git.novox.be/novox/mesh-sdk/go/views" ) // BuildCurrentTool is build_current (design 47 H2 §801). const BuildCurrentTool = "build_current" // buildCurrentDescription is build_current's description. const buildCurrentDescription = "What every holder of a build seat takes and runs now (design 47 H2 section 801): the holders named by the " + "seat-holders view, each build seat on each node, and each holder's own answer to its seat's verb current, asked in turn and " + "answered together: whether it takes asks and why not (paused, capacity 0, unhealthy) since when, and every ask it runs, whoever " + "asked it. A holder that does not answer is listed with why. Read-only." // seatHoldersState is the assigner's view of who holds which seat on which node (ADR 0571 §6). const seatHoldersState = "seat-holders" // heldBuildSeat is one holder of a build seat as build_current answers it: the seat, its node and module, and the holder's // answer to current, or why there is none. type heldBuildSeat struct { Seat string `json:"seat"` Node string `json:"node"` Module string `json:"module,omitempty"` Current json.RawMessage `json:"current,omitempty"` Error string `json:"error,omitempty"` } // buildCurrentTool is build_current (§801; H-A-446 (1)): the holders of each build seat (seats) named from the seat-holders view // (holders, keyed `.`), each holder's current asked in turn at its node through ask (stdio.Ask), answered together; // it reads no presence and chooses no node. func buildCurrentTool(seats func() []string, holders func() (map[string]json.RawMessage, error), ask func(key string, body any) (json.RawMessage, error)) stdio.Tool { return stdio.Tool{Name: BuildCurrentTool, Description: buildCurrentDescription, Input: map[string]any{"type": "object", "properties": map[string]any{}}, Run: func(map[string]any) (any, error) { view, err := holders() if err != nil { return nil, err } var out []heldBuildSeat for _, seat := range seats() { for key, raw := range view { node, ok := strings.CutPrefix(key, seat+".") if !ok || node == "" { continue } var held struct { Holders []struct { Module string `json:"module"` } `json:"holders"` } if json.Unmarshal(raw, &held) != nil || len(held.Holders) == 0 { continue } out = append(out, heldBuildSeat{Seat: seat, Node: node, Module: held.Holders[0].Module}) } } sort.Slice(out, func(i, j int) bool { if out[i].Seat != out[j].Seat { return out[i].Seat < out[j].Seat } return out[i].Node < out[j].Node }) for i := range out { // in turn, one holder at a time raw, err := ask("seat:"+out[i].Seat+".current@"+out[i].Node, map[string]any{}) if err != nil { out[i].Error = err.Error() continue } out[i].Current = raw } if out == nil { out = []heldBuildSeat{} } return map[string]any{"holders": out, "count": len(out)}, nil }} } // seatHolders follows the seat-holders view of the claimant of the seat mesh-module-assigner once the catalog half names it. type seatHolders struct { mu sync.Mutex values func() map[string]json.RawMessage why string } // follow starts the follow when the bundle's first views stand (the claimant is read from the catalog half). func (s *seatHolders) follow(ctx context.Context, branches *meshcatalogclient.Branches, r *views.Reader) { s.mu.Lock() s.why = "the catalog half's first views are not read yet" s.mu.Unlock() go func() { select { case <-ctx.Done(): return case <-branches.FirstViews(ctx): } claimant, ok := branches.Claimant(seatlist.HolderSeat) if !ok { s.mu.Lock() s.why = "no module claims the seat " + seatlist.HolderSeat + " on the catalog half" s.mu.Unlock() return } w, ok := meshcatalogclient.ReaderWiring(r).(interface { Follow(owner, state, tool string) (func() map[string]json.RawMessage, error) }) var values func() map[string]json.RawMessage err := fmt.Errorf("the bundle's reader follows no plain view") if ok { values, err = w.Follow(claimant, seatHoldersState, claimant+"."+seatHoldersState) } s.mu.Lock() defer s.mu.Unlock() if err != nil { s.why = fmt.Sprintf("the seat-holders view of %s is not followed: %v", claimant, err) return } s.values, s.why = values, "" }() } // read is the view's values, or why it is not read. func (s *seatHolders) read() (map[string]json.RawMessage, error) { s.mu.Lock() defer s.mu.Unlock() if s.values == nil { return nil, errors.New("the seat-holders view is not read: " + s.why) } return s.values(), nil }