Files
mesh-delivery/modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge-tools/main.go
T
jschoubben 9fe6330d79 B4: the bridge's whole test says head-moved; bridge_heads reads the heads held
The bridge's whole test runs the program against an embedded bus as the node does: a start, a
merge, an admitted push to an unprotected branch and a restart after a move each give one
head-moved naming the head read through the seat git's branch and the head held before (start
true at a start); a head heard twice, a branch not watched and a restart at the head held give
none, and the bridge asks the seat git nothing but branch. Its bus permissions are composed for
the manifest as it now stands. The pull, open pull request, group and unprotected-branch tests
go; the owed test performs the gate's statuses once, after a restart and again by hand.

bridge_heads answers each key of the state heads with the head the bridge holds, read-only.

Builds: design 47 H §739 (how checked), §774; lead answers H-A-48, H-A-85 (1)

Claude-Session: https://claude.ai/code/session_01JG73KHUnBze48rdua8u2sy
2026-11-20 03:34:07 +01:00

153 lines
6.0 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"
"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)
}
}
}
// 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() ([]OwedRow, error) { return performedRows(performed) }, func(what, why string) error {
_, err := recorder.Record(meshhandactsclient.Request{What: what, Why: why, Cause: "owed-dropped"})
return err
}, time.Now)...)
if err := stdio.Serve("", tools); err != nil {
fmt.Fprintf(os.Stderr, "[mesh-delivery-forge-bridge-tools] %v\n", err)
os.Exit(1)
}
}