Files
mesh-delivery/modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge-tools/main.go
T
jschoubben 13d960a120 F1: an owed act failing on a part away stays owed, and a record's acts are performed again
An owed act met by the forge away (401 while the store was replaced) ended
could not be performed and nothing performed it again. Each act is now a kept
call at the module's pace (kept-call-pace-first, kept-call-pace-max) until
owed-away-bound (60 minutes), then ends could not be performed naming every
try. Before each try the bridge reads the pull request through the seat git's
pull: one closed unmerged ends owed.ended not-needed, never performed. The
gate's owed event gains record (the delivery record's source revision), kept
with the act in performed, and bridge_owed_again by record and why performs
again every act of that record that ended could not be performed, written in
the hand-act log first.

Builds: design 47 H2 §807, §775, §819, H-A-432
2026-11-22 01:46:49 +01:00

177 lines
7.4 KiB
Go

// Command mesh-delivery-forge-bridge-tools serves the forge bridge's tools (design 51; design 47 *What the gate owes the
// forge*): the runtime launches it for a call. bridge_tags reads a package's git tags through the seat git's verb tags,
// for the builder's floor; bridge_heads reads the head the bridge holds for each watched branch; bridge_owed, bridge_owed_again and bridge_owed_drop write an ask into the bridge's state
// owed-asks and wait for the bridge's answer there, since the bridge is the other program and serves no request subject
// (ADR 0437 §4).
package main
import (
"context"
"encoding/json"
"fmt"
"os"
"strings"
"sync"
"time"
"git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshhandactsclient"
stdio "git.novox.be/novox/mesh-sdk/go"
"git.novox.be/novox/mesh-sdk/go/stateobjects"
"git.novox.be/novox/mesh-sdk/go/views"
"mesh-delivery-forge-bridge/internal/bridge"
"mesh-delivery-forge-bridge/internal/groups"
)
// HeadsTool reads the bridge's state heads (design 47 H §739, H-A-48, H-A-85).
const HeadsTool = "bridge_heads"
// headState is the state heads as bridge_heads reads it (stdio.KeptState).
type headState interface {
Keys() ([]string, error)
Get(key string) (*stdio.StateEntry, error)
}
// heads is the state heads: each key, branch.<owner>.<name>.<escaped branch>, with the head the bridge holds for it.
func heads(s headState) (map[string]string, error) {
keys, err := s.Keys()
if err != nil {
return nil, fmt.Errorf("the bridge's state heads cannot be listed: %w", err)
}
out := map[string]string{}
for _, k := range keys {
e, err := s.Get(k)
if err != nil {
return nil, fmt.Errorf("the bridge's state heads cannot be read at %s: %w", k, err)
}
if e != nil {
out[k] = strings.TrimSpace(string(e.Value))
}
}
return out, nil
}
// Tools are the bridge's reading tools: forge calls the seat git, held is the state heads.
func Tools(forge bridge.Caller, held headState) []stdio.Tool {
str := func(args map[string]any, k string) string { s, _ := args[k].(string); return strings.TrimSpace(s) }
return []stdio.Tool{
{Name: bridge.TagsTool,
Description: "A package's git tags on the forge, for the builder's floor (design 51): every tag of repository whose name starts " +
"with prefix (go/v for a Go package in the go directory, v for one at the root), sorted. Read-only; read through the seat " +
"git's verb tags, at most " + fmt.Sprint(bridge.TagsMaxPages) + " pages (more says so).",
Input: map[string]any{"type": "object", "required": []string{"repository"}, "properties": map[string]any{
"repository": map[string]any{"type": "string", "description": "<owner>/<name>"},
"prefix": map[string]any{"type": "string", "description": "only tags starting with it"}}},
Run: func(args map[string]any) (any, error) {
return bridge.Tags(context.Background(), forge, str(args, "repository"), str(args, "prefix"))
}},
{Name: HeadsTool,
Description: "The head the forge bridge holds for each watched branch, the one its last head-moved named (design 47 H §739): " +
"each key of its state heads, branch.<owner>.<name>.<escaped branch>, with its head. Read-only; an untaken head is " +
"recovered with the gate's gate_head_take, never said again here.",
Input: map[string]any{"type": "object", "properties": map[string]any{}},
Run: func(map[string]any) (any, error) {
h, err := heads(held)
if err != nil {
return nil, err
}
return map[string]any{"heads": h}, nil
}},
}
}
// askState writes an ask's value at key of the state s (its bucket named bucket) and waits, on a watch of the key, at most
// bound for the bridge's answer, a later value that done says is answered.
func askState(s stdio.KeptState, bucket string, bound time.Duration, done func([]byte) bool) func(key string, value []byte) ([]byte, error) {
return func(key string, value []byte) ([]byte, error) {
answered := make(chan []byte, 1)
var rev uint64
var mu sync.Mutex
take := func(revision uint64, v []byte) {
mu.Lock()
mine := rev
mu.Unlock()
if mine == 0 || revision <= mine || !done(v) {
return
}
select {
case answered <- v:
default:
}
}
if err := s.Watch(key, func(c stdio.StateChange) error {
if c.Op == "put" {
take(c.Revision, c.Value)
}
return nil
}); err != nil {
return nil, fmt.Errorf("watching %s.%s: %w", bucket, key, err)
}
r, err := s.Put(key, json.RawMessage(value))
if err != nil {
return nil, fmt.Errorf("the ask could not be written to %s: %w", bucket, err)
}
mu.Lock()
rev = r
mu.Unlock()
if e, err := s.Get(key); err == nil && e != nil {
take(e.Revision, e.Value)
}
wait, cancel := context.WithTimeout(context.Background(), bound) // a context's bound, no timer (ADR 0437 §8)
defer cancel()
select {
case v := <-answered:
return v, nil
case <-wait.Done():
return nil, fmt.Errorf("the bridge did not answer within %s: its answer will be in %s.%s", bound, bucket, key)
}
}
}
// Module is the module whose tools these are; the generic prune tools are named for it (H-A-380 (3), H-A-433).
const Module = "mesh-delivery-forge-bridge"
// PerformedPruneTools are `mesh-delivery-forge-bridge_state_prune_now` and `mesh-delivery-forge-bridge_state_keep` over
// the state performed (design 47 H2 §812): a prune to prune-target now, and a key kept from the prune under _keep/<key>
// until a time, each written in the hand-act log first.
func PerformedPruneTools(performed stdio.Prunable, acts views.HandActs) []stdio.Tool {
return stateobjects.PruneTools(Module, []stateobjects.Pruning{{Name: groups.PerformedState, State: performed}}, acts)
}
// prunedPerformed is the state performed as the prune tools act on it, with the module's prune-mark and prune-target; a
// setting that is no percentage is said, and the defaults hold.
func prunedPerformed() *stdio.PrunedState {
p, err := stdio.PruneSettings()
if err != nil {
fmt.Fprintf(os.Stderr, "[mesh-delivery-forge-bridge-tools] the prune of performed keeps its defaults (%d %%, to %d %%): %v\n", stdio.PruneMarkDefault, stdio.PruneTargetDefault, err)
p = stdio.Prune{Mark: stdio.PruneMarkDefault, Target: stdio.PruneTargetDefault}
}
return stdio.State(groups.PerformedState).Pruned(p)
}
// askCaller calls a tool as this bundle's module, through the runtime (stdio.Ask), the module's own invokes granting it.
func askCaller(_ context.Context, key string, body any) (json.RawMessage, error) {
return stdio.Ask(key, body)
}
func main() {
// The module the runtime serves these tools as: the bridge's, whose states they ask (design 47 G A-296).
served := os.Getenv(stdio.ServedAs)
performed := stdio.State(groups.PerformedState)
tools := Tools(askCaller, stdio.State("heads"))
owedAsks := stdio.State(groups.OwedAsksState)
recorder := meshhandactsclient.Recorder{Ask: stdio.Ask}
tools = append(tools, OwedTools(askState(owedAsks, groups.OwedAsksBucket(served), 25*time.Second, func(v []byte) bool {
a, err := groups.DecodeOwedAsk(v)
return err == nil && a.Done()
}), func(f OwedFilter) ([]OwedRow, error) { return performedRows(performed, f) }, func(what, why, cause string) error {
_, err := recorder.Record(meshhandactsclient.Request{What: what, Why: why, Cause: cause})
return err
}, time.Now)...)
tools = append(tools, PerformedPruneTools(prunedPerformed(), recorder)...)
if err := stdio.Serve("", tools); err != nil {
fmt.Fprintf(os.Stderr, "[mesh-delivery-forge-bridge-tools] %v\n", err)
os.Exit(1)
}
}