A merge call is the delivery's group from H1, so the gate no longer keeps deliveries and groups from the seat git's pull events, reads no open pull request at its start, and answers deliveries and show from the records alone. The verb groups stays, answered from the records, until the seat mesh-delivery's definition drops it. Builds: design 47 H §764 (the gate: the groups from a branch name, its states deliveries and groups, its consumption of pull-updated, pull-merged and pull-closed, its start reconciliation); H-A-28 Claude-Session: https://claude.ai/code/session_01JG73KHUnBze48rdua8u2sy
290 lines
12 KiB
Go
290 lines
12 KiB
Go
// mesh-delivery-gate: the holder of the mesh-delivery seat (novox/hq ADR 0239, to-be 47). A Go bundle the node's
|
|
// runtime launches. From milestone G's delivery it keeps no merge states and asks no Approve per merge (design 47 G §172,
|
|
// §431, §592): it keeps the delivery records, one per source revision of a watched branch the indexer takes in, and the
|
|
// source revision per watched branch, and says what it owes the forge as events the forge bridge performs. It merges
|
|
// nothing and asks the controller nothing. stdout is the MCP channel; what this module says, it says on stderr.
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
"git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshhandactsclient"
|
|
stdio "git.novox.be/novox/mesh-sdk/go"
|
|
)
|
|
|
|
func logf(format string, a ...any) { fmt.Fprintf(os.Stderr, format+"\n", a...) }
|
|
|
|
// listening is whether the events this owner follows reach it, in words.
|
|
type listening struct {
|
|
mu sync.Mutex
|
|
now string
|
|
}
|
|
|
|
func (l *listening) set(s string) { l.mu.Lock(); l.now = s; l.mu.Unlock() }
|
|
func (l *listening) get() string { l.mu.Lock(); defer l.mu.Unlock(); return l.now }
|
|
|
|
// The events this owner follows: the forge bridge's word on an act owed, and the step events the delivery records are built
|
|
// from (consumer.go).
|
|
var followed = append(append([]string{owedEndedEvent}, stepPatterns()...), registrationChangedEvent, forgeMergedEvent)
|
|
|
|
func main() {
|
|
settingsFile := os.Getenv("MESH_DELIVERY_GATE_SETTINGS")
|
|
var saidMu sync.Mutex
|
|
lastSaid := ""
|
|
settings := func() Settings {
|
|
s, err := readSettings(settingsFile)
|
|
saidMu.Lock()
|
|
defer saidMu.Unlock()
|
|
if err != nil && err.Error() != lastSaid {
|
|
logf("[mesh-delivery-gate] settings cannot be read, the defaults hold: %v", err)
|
|
}
|
|
lastSaid = ""
|
|
if err != nil {
|
|
lastSaid = err.Error()
|
|
}
|
|
return s
|
|
}
|
|
h := &Holder{
|
|
Settings: settings,
|
|
Logf: logf,
|
|
Source: kvSource{},
|
|
Owed: seatOwed{say: stdio.SeatPublish},
|
|
// The gate says what stalled to the operator itself (issue 1275, ADR 0437 §9).
|
|
Say: sayOnSeat,
|
|
Conditions: kvConditions{},
|
|
}
|
|
h.init()
|
|
h.SaySeat = stdio.SeatPublish
|
|
h.Records = &Records{Ledger: sdkLedger{}, Now: h.Now, Retention: h.recordRetention, Say: stdio.SeatPublish, Logf: logf}
|
|
h.Records.Changed = h.recordChanged
|
|
h.Forge = seatGit{ask: stdio.Ask}
|
|
h.Catalog = seatCatalog{ask: stdio.Ask}
|
|
h.Consumer = &StepConsumer{Records: h.Records, Families: stepFamilies, Now: h.Now, Logf: logf, Taken: h.TakenIn}
|
|
// The gate's one held writer of its own findings (design 47 G A-39, A-37, A-40): made at once, started once the runtime serves.
|
|
fw, err := NewFindingWriter()
|
|
if err != nil {
|
|
logf("%v", err)
|
|
os.Exit(1)
|
|
}
|
|
h.Findings = fw
|
|
go func() {
|
|
<-stdio.Served()
|
|
if err := fw.Start(context.Background()); err != nil {
|
|
logf("%v", err)
|
|
os.Exit(1)
|
|
}
|
|
}()
|
|
l := &listening{now: "not yet: starting"}
|
|
go run(h, l)
|
|
if err := announceKept(stdio.Keeps); err != nil {
|
|
logf("%v", err)
|
|
os.Exit(1)
|
|
}
|
|
if err := stdio.Serve("", tools(h, l)); err != nil {
|
|
logf("%v", err)
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
|
|
// run switches to the records, then takes the events, and acts on events from then on (novox/hq ADR 0406 §1, issue 975): no
|
|
// tick, no forge poll, no reconcile loop. Each step is retried, each failure said, never given up on quietly.
|
|
func run(h *Holder, l *listening) {
|
|
<-stdio.Served() // Serve first: the state is reached through it, and an event is heard from as soon as it serves
|
|
statePins(h, l)
|
|
go h.Work()
|
|
for _, pattern := range followed {
|
|
pattern := pattern
|
|
for wait := 2 * time.Second; ; wait = min(wait*2, time.Minute) {
|
|
err := stdio.Subscribe(pattern, func(e stdio.Envelope) error { return handle(h, e) })
|
|
if err == nil {
|
|
break
|
|
}
|
|
l.set("not yet: " + err.Error())
|
|
logf("[mesh-delivery] not hearing %s yet (%v); asking again in %s", pattern, err, wait)
|
|
time.Sleep(wait)
|
|
}
|
|
}
|
|
l.set("listening")
|
|
logf("[mesh-delivery] listening: %s", strings.Join(followed, ", "))
|
|
// A watched branch no source key holds starts from the forge, once at each start and at each registration change (A-401).
|
|
// then each head is compared with the forge's, and a merge made outside the gate is followed (A-464).
|
|
go func() {
|
|
startSourcesAtStart(h)
|
|
if _, err := h.FollowAtStart(); err != nil {
|
|
logf("[mesh-delivery] the sources were not all compared with the forge's heads: %v", err)
|
|
}
|
|
}()
|
|
}
|
|
|
|
// statePins says every pin the records hold once at each start (design 47 G §579), retrying until it can.
|
|
func statePins(h *Holder, l *listening) {
|
|
for wait := 2 * time.Second; ; wait = min(wait*2, time.Minute) {
|
|
n, err := h.Records.StatePins()
|
|
if err == nil {
|
|
logf("[mesh-delivery] pins stated: %d", n)
|
|
return
|
|
}
|
|
l.set("not yet: " + err.Error())
|
|
logf("[mesh-delivery] the pins cannot be stated yet (%v); asking again in %s", err, wait)
|
|
time.Sleep(wait)
|
|
}
|
|
}
|
|
|
|
// handle takes one event, then asks for a look at every step the event makes possible (Work, React). An event that cannot be read is
|
|
// said and acknowledged: read again it would fail again.
|
|
func handle(h *Holder, e stdio.Envelope) error {
|
|
taken := take(h, e)
|
|
h.Poke()
|
|
return taken
|
|
}
|
|
|
|
func take(h *Holder, e stdio.Envelope) error {
|
|
if h.Consumer != nil && h.Consumer.Takes(e.Key) {
|
|
return h.Consumer.Hear(e)
|
|
}
|
|
if e.Key == owedEndedEvent {
|
|
return h.OwedEnded(e.Body)
|
|
}
|
|
if e.Key == registrationChangedEvent {
|
|
_, err := h.StartSources()
|
|
return err
|
|
}
|
|
if forgeMergedEvent != "" && stdio.TopicMatches(forgeMergedEvent, e.Key) {
|
|
return h.ForgeMerged(e.Body)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func str(description string) map[string]any {
|
|
return map[string]any{"type": "string", "description": description}
|
|
}
|
|
|
|
func strArg(a map[string]any, k string) string { s, _ := a[k].(string); return strings.TrimSpace(s) }
|
|
|
|
// intArg is a whole number given as a number or as text; absent is zero.
|
|
func intArg(a map[string]any, k string) (int, error) {
|
|
switch v := a[k].(type) {
|
|
case nil:
|
|
return 0, nil
|
|
case float64:
|
|
if v == float64(int(v)) {
|
|
return int(v), nil
|
|
}
|
|
case int:
|
|
return v, nil
|
|
case string:
|
|
v = strings.TrimPrefix(strings.TrimSpace(v), "#")
|
|
if v == "" {
|
|
return 0, nil
|
|
}
|
|
if n, err := strconv.Atoi(v); err == nil {
|
|
return n, nil
|
|
}
|
|
}
|
|
return 0, fmt.Errorf("%q is not a whole number: %v", k, a[k])
|
|
}
|
|
|
|
func boolArg(a map[string]any, k string) bool {
|
|
switch v := a[k].(type) {
|
|
case bool:
|
|
return v
|
|
case string:
|
|
return v == "true"
|
|
}
|
|
return false
|
|
}
|
|
|
|
func need(a map[string]any, keys ...string) error {
|
|
for _, k := range keys {
|
|
if strArg(a, k) == "" {
|
|
return fmt.Errorf("needs %q", k)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func tools(h *Holder, l *listening) []stdio.Tool {
|
|
seat := Seat + "."
|
|
list := []stdio.Tool{
|
|
{Name: seat + "deliveries",
|
|
Description: "Every delivery record the gate keeps, as it is kept (design 47 G A-24): the open ones, and those that ended in " +
|
|
"the last thirty days (all: every one kept), each with its milestone or group, its pull requests, its entries' outcomes " +
|
|
"per node, its acts owed the forge and why it is not ended. Narrowed by state (open or ended), repository or group.",
|
|
Input: map[string]any{"type": "object", "properties": map[string]any{"state": str("open or ended"),
|
|
"repository": str("owner/repository"), "group": str("a group's id (its branch name)"),
|
|
"all": map[string]any{"type": "string", "enum": []string{"true", "false"}}}},
|
|
Run: func(a map[string]any) (any, error) {
|
|
return h.Deliveries(strArg(a, "state"), strArg(a, "repository"), strArg(a, "group"), boolArg(a, "all"))
|
|
}},
|
|
{Name: seat + "show",
|
|
Description: "One delivery record whole, as it is kept: by its instance, its milestone, its group's id or the id of one of " +
|
|
"its pull requests (owner/repository@commit).",
|
|
Input: map[string]any{"type": "object", "properties": map[string]any{"id": str("a delivery's id, or a group's")},
|
|
"required": []string{"id"}},
|
|
Run: func(a map[string]any) (any, error) { return h.Show(strArg(a, "id")) }},
|
|
{Name: seat + "groups",
|
|
Description: "Every delivery group, from the delivery records: its members, its state (open or ended), why, and the acts the " +
|
|
"gate said it owes the forge for it, each with its id and its recorded outcome (design 47 *What the gate owes the " +
|
|
"forge*). With a group, that group alone, ended or not, and no groups for a group it does not know. What reached the " +
|
|
"machines is the delivery view's.",
|
|
Input: map[string]any{"type": "object", "properties": map[string]any{
|
|
"all": map[string]any{"type": "string", "enum": []string{"true", "false"}, "description": "\"true\": the groups that ended too"},
|
|
"group": str("one group's id: that group alone, ended or not")}},
|
|
Run: func(a map[string]any) (any, error) { return h.Groups(boolArg(a, "all"), strArg(a, "group")), nil }},
|
|
{Name: seat + "source",
|
|
Description: "The source revision the gate holds: every repository it names, its trunk branch and that trunk's head, the " +
|
|
"group that moved it last and its revision. Read, never written: a node-engine reads the repositories here to call a " +
|
|
"reverted build's catch-up tool once per repository, never the gate's state.",
|
|
Input: map[string]any{"type": "object", "properties": map[string]any{
|
|
"branch": str("the watched branch, escaped as a key name is; absent: the list of the branches the gate keeps a source revision of")}},
|
|
Run: func(a map[string]any) (any, error) { return h.SourceRevision(strArg(a, "branch")), nil }},
|
|
{Name: seat + "recovery",
|
|
Description: "Name a merge a recovery (design 47 G §81): the delivery record that holds the commit is marked with who named it " +
|
|
"(the caller the runtime proved), why and the commit, and recovery-merged is said for every channel.",
|
|
Input: map[string]any{"type": "object", "properties": map[string]any{"repository": str("owner/repository"),
|
|
"commit": str("the merge commit"), "why": str("why the merge is a recovery")},
|
|
"required": []string{"repository", "commit", "why"}},
|
|
Run: func(a map[string]any) (any, error) {
|
|
if err := need(a, "repository", "commit", "why"); err != nil {
|
|
return nil, err
|
|
}
|
|
if h.Records == nil {
|
|
return nil, errors.New("this gate keeps no delivery records")
|
|
}
|
|
return h.Records.Recovery(strArg(a, "repository"), strArg(a, "commit"), strArg(a, "why"), stdio.CallerOf(a))
|
|
}},
|
|
{Name: seat + "stalled",
|
|
Description: "Every delivery record whose open wait is past its bound, with that wait (a record open a day after it opened), " +
|
|
"and every act owed the forge unanswered past fifteen minutes (design 47 G A-24, §165).",
|
|
Run: func(a map[string]any) (any, error) { return h.Stalled(), nil }},
|
|
{Name: "delivery_status",
|
|
Description: "Whether this owner works: how many delivery records are open and ended, what is owed to the forge and not yet " +
|
|
"answered, how many owed effects were tried since it started (done, failed and tried again, given up) and when the last " +
|
|
"was, whether its events arrive, the last refusals, and the source revision of every branch the gate's bucket source holds.",
|
|
Run: func(map[string]any) (any, error) {
|
|
out := h.Status(l.get())
|
|
out["source"] = h.SourceAll()
|
|
return out, nil
|
|
}},
|
|
}
|
|
list = append(list, recordTools(h, meshhandactsclient.Recorder{Ask: stdio.Ask})...)
|
|
list = append(list, sourceTools(h, meshhandactsclient.Recorder{Ask: stdio.Ask})...)
|
|
// A verb that changes a delivery asks for a look at what it makes possible (react.go).
|
|
for i := range list {
|
|
run := list[i].Run
|
|
list[i].Run = func(a map[string]any) (any, error) {
|
|
out, err := run(a)
|
|
h.Poke()
|
|
return out, err
|
|
}
|
|
}
|
|
return list
|
|
}
|