Files
mesh-delivery/modules/mesh-delivery-builder/cmd/mesh-delivery-builder-tools/asks.go
T
jschoubben 905a055a1e B2: the builder serves the seat mesh-builds' drop and its queue's tools
A caller whose step ended by its bound could not stop the builder working on
its ask, and nobody could see what waited on the seat's work queue. The
builder now serves the seat's verb drop: an ask it runs is cancelled, one not
pulled yet is kept dropped in its state taken (dropped.<ask>, for
ask-taken-bound); both end the step could-not-run naming the drop and who
asked it. builder_asks reads the waiting asks through the runtime and the
taken ones and the drops from the state taken; builder_ask_drop asks the
seat's drop once the hand act is recorded.

Builds: design 47 H2 §777, §778; H-A-401, H-A-404, H-A-410
2026-11-22 01:08:30 +01:00

132 lines
5.9 KiB
Go

package main
import (
"encoding/json"
"errors"
"fmt"
"sort"
"strings"
"time"
"git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshhandactsclient"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshbuilds"
stdio "git.novox.be/novox/mesh-sdk/go"
"mesh-delivery-builder/internal/build"
)
// dropVerbKey is the seat verb builder_ask_drop asks (the manifest's invokes).
const dropVerbKey = "seat:" + meshbuilds.Seat + "." + meshbuilds.VerbDrop
// takenReader is the builder's state taken, as the bundle reads it (stdio.State).
type takenReader interface {
Keys() ([]string, error)
Get(key string) (*stdio.StateEntry, error)
}
// TakenAsk is an ask of the seat mesh-builds the builder took (its id in the state taken, kept for ask-taken-bound), or
// whose drop it keeps before its pull, as builder_asks shows it.
type TakenAsk struct {
Ask string `json:"ask"`
At time.Time `json:"at,omitzero"`
By string `json:"by,omitempty"`
Why string `json:"why,omitempty"`
}
// WaitingRow is an ask the runtime reads as waiting on the seat's work queue, as builder_asks shows it: State is
// waiting (taken by no holder yet), waiting again (handed out before and given back, its id not taken) or held (handed
// out again while the builder holds its id in the state taken: its run answers it).
type WaitingRow struct {
stdio.WaitingAsk
State string `json:"state"`
}
// The states of a waiting ask.
const (
StateWaiting = "waiting"
StateWaitingAgain = "waiting again"
StateHeld = "held"
)
// asksTools are builder_asks, what waits on and is taken from the seat mesh-builds' work queue: the waiting asks read
// through the runtime (stdio.QueueWaiting, H-A-410), the taken ones and the drops kept before a pull from the builder's
// state taken; and builder_ask_drop, which ends one ask through the seat's verb drop once the hand act is recorded
// (design 47 H2 §777, §778; H-A-401, H-A-404, H-A-410).
func asksTools(waiting func(seat string) (stdio.Waiting, error), taken takenReader, drop func(meshbuilds.DropInput) (json.RawMessage, error), now func() time.Time, acts meshhandactsclient.Recorder) []stdio.Tool {
str := func(args map[string]any, k string) string { s, _ := args[k].(string); return strings.TrimSpace(s) }
return []stdio.Tool{
{Name: build.AsksTool,
Description: "What waits on and is taken from the seat mesh-builds' work queue: waiting, each build step ask the queue holds unacknowledged, oldest " +
"first, with its id, the module that asked it, its age and its state: " + StateWaiting + " (taken by no holder yet), " + StateWaitingAgain +
" (handed out before and given back) or " + StateHeld + " (its id taken by the builder, its run answers it); capped when the runtime cut the read; taken, each ask the builder took " +
"within ask-taken-bound, with when; dropped, each drop kept for an ask not pulled yet, with who dropped it and why. Read-only.",
Input: map[string]any{"type": "object", "properties": map[string]any{}},
Run: func(map[string]any) (any, error) {
w, err := waiting(meshbuilds.Seat)
if err != nil {
return nil, fmt.Errorf("the waiting asks of %s are not read: %w", meshbuilds.Seat, err)
}
keys, err := taken.Keys()
if err != nil {
return nil, fmt.Errorf("the state taken is not read: %w", err)
}
sort.Strings(keys)
tk, dr := []TakenAsk{}, []TakenAsk{}
for _, k := range keys {
e, err := taken.Get(k)
if err != nil || e == nil {
continue
}
if id, ok := strings.CutPrefix(k, build.DroppedPrefix); ok {
var d build.Dropped
_ = json.Unmarshal(e.Value, &d)
dr = append(dr, TakenAsk{Ask: id, At: e.At, By: d.By, Why: d.Why})
} else if id, ok := strings.CutPrefix(k, meshbuilds.Seat+"."); ok {
tk = append(tk, TakenAsk{Ask: id, At: e.At})
}
}
held := map[string]bool{}
for _, a := range tk {
held[a.Ask] = true
}
rows := make([]WaitingRow, 0, len(w.Asks))
for _, a := range w.Asks {
r := WaitingRow{WaitingAsk: a, State: StateWaiting}
if held[a.ID] {
r.State = StateHeld
} else if a.Again {
r.State = StateWaitingAgain
}
rows = append(rows, r)
}
return map[string]any{"seat": meshbuilds.Seat, "waiting": rows, "capped": w.Capped, "taken": tk, "dropped": dr}, nil
}},
{Name: build.AskDropTool,
Description: "End one ask on the seat mesh-builds' work queue through the seat's verb drop: an ask the builder runs is cancelled, one not " +
"pulled yet is kept dropped for ask-taken-bound; either way its step ends could-not-run naming the drop and who asked it. Written in " +
"the hand-act log (mesh-hand-acts).",
Input: map[string]any{"type": "object", "required": []string{"ask", "why"}, "properties": map[string]any{
"ask": map[string]any{"type": "string", "description": "the ask's id"},
"why": map[string]any{"type": "string", "description": "why it is dropped, in plain words"}}},
Run: func(args map[string]any) (any, error) {
ask, why := str(args, "ask"), str(args, "why")
if ask == "" || why == "" {
return nil, errors.New("give the ask's id and why it is dropped")
}
if _, err := acts.Record(meshhandactsclient.Request{What: fmt.Sprintf("dropped the ask %s of the seat %s by hand", ask, meshbuilds.Seat),
Why: why, Cause: build.AskDropTool, Verb: meshbuilds.VerbDrop}); err != nil {
return nil, fmt.Errorf("the hand act was not recorded through mesh-hand-acts, so nothing was dropped: %w", err)
}
raw, err := drop(meshbuilds.DropInput{ID: ask, Why: why, By: stdio.CallerOf(args)})
if err != nil {
return nil, fmt.Errorf("the seat %s's drop did not answer for %s: %w", meshbuilds.Seat, ask, err)
}
var a meshbuilds.DropAnswer
if err := json.Unmarshal(raw, &a); err != nil {
return nil, err
}
return map[string]any{"ask": ask, "dropped": true, "held": a["held"]}, nil
}},
}
}