Files
mesh-delivery/modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge/call.go
T
jschoubben 7ac48fd882 B2, B3: the forge bridge says head-moved and carries the one model
B2: on every branch-moved of a watched branch, and at its start for every watched branch's head,
the bridge reads the head through the seat git's verb branch, as a kept call that waits on the
holder's return while the branch is watched, and writes one head-moved (repository, branch,
head, before the head held in its state heads, start) on its stream forge; a head equal to the
one held writes nothing. It reads no pull request, no tree, no file and no protection: the
merged and pushed messages, their inputs and the state inputs, the call of git.pull, the finding
branch-unprotected with its held finding writer, and the ten-second redelivery go. A move whose
head cannot be read is left unacknowledged for the bus to deliver again.

B3: the pull-message path (Convert, the open pull requests at start and reconnect, the
forge-delete message), the groups (internal/groups' group part, forge.group.merged, end-asks,
bridge_groups, bridge_group_end_again), bridge_merged_message with the forge-made reader, the
checker's owed and the merge act of a switched key (with the switch's bookkeeping read) go, with
their consumes, invokes, states and requires. The gate's owed path stays (H-A-71), its catch-up
from the gate's verb groups gone with that verb.

The two meet in main.go and the package compiles only with both, as it did not since B1. The
unit suites pass; the whole test (integration) is rewritten in B4.

Builds: design 47 H §739, §764 (the forge bridge; the branch protection); lead answers H-A-23, H-A-27, H-A-48, H-A-71

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

83 lines
3.2 KiB
Go

package main
import (
"context"
"encoding/json"
"errors"
"fmt"
"strings"
"time"
"github.com/nats-io/nats.go"
)
// callTimeout bounds a call to a tool (mesh-sdk go/process/call.go CallTimeout).
const callTimeout = 30 * time.Second
// caller calls a module's tool by its manifest key (`<module>.<tool>`) with a JSON body and returns the tool's result.
type caller func(ctx context.Context, key string, body any) (json.RawMessage, error)
// callerOn calls tools and seat verbs as the bridge's own principal. It copies mesh-sdk go/process/call.go (Call, callSubjects,
// request; hand-written here until the SDK's helper is used, issue 1242): the request is the tool's arguments as
// JSON, published on the caller-named subject `mesh.mod.<module>.call.<tool>.<user, dots as ~>`, or on the subject that
// names no caller, `mesh.mod.<module>.tool.<tool>`, when nothing answers the first; the reply is
// {"result", "error", "node"} and a non-empty error is the call's failure (mesh-tools node-tools/internal/bus/bus.go
// answerOf). The grant to publish on these subjects comes from the manifest's invokes alone (mesh-control
// internal/broker/nats.go invokedSubjects).
func callerOn(nc *nats.Conn, user string) caller {
token := strings.ReplaceAll(user, ".", "~")
request := func(ctx context.Context, subject string, data []byte) (json.RawMessage, error) {
ctx, cancel := context.WithTimeout(ctx, callTimeout)
defer cancel()
msg, err := nc.RequestWithContext(ctx, subject, data)
if err != nil {
return nil, err
}
var r struct {
Result json.RawMessage `json:"result"`
Error string `json:"error"`
Node string `json:"node"`
}
if err := json.Unmarshal(msg.Data, &r); err != nil {
return nil, fmt.Errorf("%s answered something that is not a tool's answer: %w", subject, err)
}
if r.Error != "" {
return nil, errors.New(r.Error)
}
return r.Result, nil
}
return func(ctx context.Context, key string, body any) (json.RawMessage, error) {
called, fallback, err := callSubjects(key, token)
if err != nil {
return nil, err
}
data, err := json.Marshal(body)
if err != nil {
return nil, err
}
result, err := request(ctx, called, data)
if errors.Is(err, nats.ErrNoResponders) {
result, err = request(ctx, fallback, data)
}
return result, err
}
}
// callSubjects is the caller-named subject a key is called on and its plain fallback: a seat's verb
// (`seat:<seat>.<verb>`) on mesh.seat.<seat>.call.<verb>.<token>, else a module's tool (`<module>.<tool>`) on
// mesh.mod.<module>.call.<tool>.<token> (mesh-sdk go/process/call.go callSubjectsOn).
func callSubjects(key, token string) (called, fallback string, err error) {
if rest, isSeat := strings.CutPrefix(key, "seat:"); isSeat {
seat, verb, ok := strings.Cut(rest, ".")
if !ok || seat == "" || verb == "" {
return "", "", fmt.Errorf("%q is not seat:<seat>.<verb>", key)
}
return "mesh.seat." + seat + ".call." + verb + "." + token, "mesh.seat." + seat + ".tool." + verb, nil
}
module, tool, ok := strings.Cut(key, ".")
if !ok || module == "" || tool == "" {
return "", "", fmt.Errorf("%q is not <module>.<tool>", key)
}
return "mesh.mod." + module + ".call." + tool + "." + token, "mesh.mod." + module + ".tool." + tool, nil
}