S1 kept call paced: carry the ask id, pace at the settings' defaults with jitter, stop at the caller's bound

The kept call is made again on a transport fault after kept-call-pace-first
(1 s) doubling to kept-call-pace-max (30 s), each wait within 20 %, until
the caller's bound; past it the answer names every call's time and words.
One journal line per seat at the first fault, then at most once a minute.
The ask id travels in the header x-ask; a holder reads it with
process.AskOf, a bundle with stdio.AskIDOf (TypeScript askIdOf) from the
call's _meta mesh/ask.

Builds: design 47 H2 §775; H-A-351
This commit is contained in:
2026-11-21 23:06:24 +01:00
parent bdc5cff0d6
commit 14c2532b0c
10 changed files with 474 additions and 45 deletions
+66 -26
View File
@@ -10,6 +10,7 @@ import (
"github.com/nats-io/nats.go"
stdio "git.novox.be/novox/mesh-sdk/go"
"git.novox.be/novox/mesh-sdk/go/approval"
"git.novox.be/novox/mesh-sdk/go/busretry"
"git.novox.be/novox/mesh-sdk/go/calladdress"
@@ -52,34 +53,51 @@ func (c *Conn) CallOn(node, key string, body any) (json.RawMessage, error) {
}
// CallKept is Call for a call whose answer the caller keeps and which is safe to make again (a read, or a write the
// callee deduplicates): it is asked again through a transport fault, a timeout or no responders, whether or not the bus
// is blacked out, until it is answered or busretry.Bound has passed (novox/hq ADR 0668, extending ADR 0615 §6; issue
// 1966). A callee that restarts answers no one for seconds; Call fails that at once. The caller keeps a call still
// unanswered past the bound as a wait of its own, and makes it again on the callee's event or its consumer's bind.
// callee deduplicates): mesh-sdk's kept-call form (design 47 H2 §775), asked again through a transport fault, a timeout
// or no responders at the kept-call pace (busretry.KeptPace, jittered), whether or not the bus is blacked out, until it
// is answered or busretry.Bound has passed (novox/hq ADR 0668, extending ADR 0615 §6; issue 1966). A refusal returns at
// once. A caller with an ask id and a bound of its own uses CallKeptAsk.
func (c *Conn) CallKept(key string, body any) (json.RawMessage, error) {
called, fallback, err := callSubjectsOn(key, "", c.callerToken())
if err != nil {
return nil, err
}
return c.callKeptOn(called, fallback, body)
return c.CallKeptAsk(KeptAsk{}, key, body)
}
// CallKeptOn is CallKept to the tool or seat verb of one named machine, as CallOn is Call's.
func (c *Conn) CallKeptOn(node, key string, body any) (json.RawMessage, error) {
return c.CallKeptAskOn(KeptAsk{}, node, key, body)
}
// KeptAsk is what a caller names of its kept call (design 47 H2 §775): the ask's id, which the call carries in the
// header stdio.AskHeader for the holder to take it once by (H-A-351), and the caller's bound (zero is busretry.Bound).
type KeptAsk struct {
ID string
Bound time.Duration
}
// CallKeptAsk is CallKept carrying ask's id and stopping at ask's bound: past it the error is a *busretry.Unanswered
// naming every call's time and words.
func (c *Conn) CallKeptAsk(ask KeptAsk, key string, body any) (json.RawMessage, error) {
return c.callKeptAskOn(ask, "", key, body, nil)
}
// CallKeptAskOn is CallKeptAsk to the tool or seat verb of one named machine.
func (c *Conn) CallKeptAskOn(ask KeptAsk, node, key string, body any) (json.RawMessage, error) {
if err := calladdress.CheckNode(node); err != nil {
return nil, err
}
called, fallback, err := callSubjectsOn(key, node, c.callerToken())
if err != nil {
return nil, err
}
return c.callKeptOn(called, fallback, body)
return c.callKeptAskOn(ask, node, key, body, nil)
}
// CallKeptWhileOpen is CallKept for an ask the asker can close or replace: it is asked again only while open says its
// key is still open (design 47 G §351, §460).
func (c *Conn) CallKeptWhileOpen(open busretry.Open, key string, body any) (json.RawMessage, error) {
called, fallback, err := callSubjectsOn(key, "", c.callerToken())
if open.Open == nil {
return nil, fmt.Errorf("the call on %s: a kept call names its key's open function", key)
}
return c.callKeptAskOn(KeptAsk{}, "", key, body, &open)
}
func (c *Conn) callKeptAskOn(ask KeptAsk, node, key string, body any, open *busretry.Open) (json.RawMessage, error) {
called, fallback, err := callSubjectsOn(key, node, c.callerToken())
if err != nil {
return nil, err
}
@@ -87,21 +105,25 @@ func (c *Conn) CallKeptWhileOpen(open busretry.Open, key string, body any) (json
if err != nil {
return nil, err
}
return busretry.DoKept("the call on "+fallback, open, askBoth(c.request, called, fallback, data), nil)
return callKeptWith(keptOf(key, ask, open), c.requestAsking(ask.ID), called, fallback, data)
}
func (c *Conn) callKeptOn(called, fallback string, body any) (json.RawMessage, error) {
data, err := json.Marshal(body)
if err != nil {
return nil, err
// keptOf is the paced form's naming of a call on key: its seat and verb, or its module and tool.
func keptOf(key string, ask KeptAsk, open *busretry.Open) busretry.Kept {
k := busretry.Kept{Ask: ask.ID, Bound: ask.Bound, Open: open}
if a, err := calladdress.Parse(key); err == nil {
k.Seat, k.Verb = a.Seat, a.Verb
if a.Seat == "" {
k.Seat, k.Verb = a.Module, a.Tool
}
}
return callKeptWith(c.request, called, fallback, data)
return k
}
// callKeptWith is callWith for a kept call: busretry.DoRepeatable with no blackout probe.
func callKeptWith(request func(string, []byte) (json.RawMessage, error), called, fallback string,
// callKeptWith is callWith for a kept call: busretry.DoPaced with no blackout probe.
func callKeptWith(k busretry.Kept, request func(string, []byte) (json.RawMessage, error), called, fallback string,
data []byte) (json.RawMessage, error) {
return busretry.DoRepeatable("the call on "+fallback, askBoth(request, called, fallback, data), nil)
return busretry.DoPaced(k, askBoth(request, called, fallback, data))
}
// askBoth asks called, then fallback when nothing answers it.
@@ -209,10 +231,28 @@ func callSubjectsOn(key, node, token string) (called, fallback string, err error
// request asks one subject once and reads its reply ({result, error, node}); a reply naming an error is
// the call's failure, not a transport one.
func (c *Conn) request(subject string, data []byte) (json.RawMessage, error) {
ctx, cancel := context.WithTimeout(context.Background(), CallTimeout)
defer cancel()
return c.requestAsk(subject, data, "")
}
// requestAsking is request carrying an ask's id in the header stdio.AskHeader (H-A-351); empty carries none.
func (c *Conn) requestAsking(ask string) func(string, []byte) (json.RawMessage, error) {
return func(subject string, data []byte) (json.RawMessage, error) { return c.requestAsk(subject, data, ask) }
}
// askMsg is the request a call publishes: its ask's id in stdio.AskHeader when it carries one (H-A-351).
func askMsg(subject string, data []byte, ask string) *nats.Msg {
req := nats.NewMsg(subject)
req.Data = data
if ask != "" {
req.Header.Set(stdio.AskHeader, ask)
}
return req
}
func (c *Conn) requestAsk(subject string, data []byte, ask string) (json.RawMessage, error) {
ctx, cancel := context.WithTimeout(context.Background(), CallTimeout)
defer cancel()
req := askMsg(subject, data, ask)
if strings.HasPrefix(subject, "mesh.seat.") {
// Every seat call says it waits for a held call's end; a verb that is not marked ignores it (lead answer A-244).
req.Header.Set(approval.WaitHeader, approval.WaitHeld)
+23 -1
View File
@@ -10,6 +10,8 @@ import (
"github.com/nats-io/nats.go"
stdio "git.novox.be/novox/mesh-sdk/go"
"git.novox.be/novox/mesh-sdk/go/approval"
"git.novox.be/novox/mesh-sdk/go/busretry"
)
@@ -128,7 +130,7 @@ func TestACallIsAskedAgainOnlyWhenItReachedNoOne(t *testing.T) {
}
var err error
if row.kept {
_, err = callKeptWith(request, "called", "fallback", nil)
_, err = callKeptWith(busretry.Kept{Seat: "s", Verb: "v"}, request, "called", "fallback", nil)
} else {
_, err = callWith(request, func() bool { return true }, "called", "fallback", nil)
}
@@ -136,6 +138,26 @@ func TestACallIsAskedAgainOnlyWhenItReachedNoOne(t *testing.T) {
t.Errorf("%s: asked %d times (%v), want %d", row.name, asks, err, row.asks)
}
}
// Design 47 H2 §775, H-A-351: a kept call carries its ask's id in the header, and a holder in a process reads it with
// AskOf while its handler runs; a call carrying none reads empty.
if m := askMsg("mesh.seat.mesh-builds.call.build", []byte(`{}`), "ask-9"); m.Header.Get(stdio.AskHeader) != "ask-9" {
t.Errorf("the ask's id is not in the header: %v", m.Header)
}
if m := askMsg("s", nil, ""); m.Header.Get(stdio.AskHeader) != "" {
t.Errorf("a call with no ask carries one: %v", m.Header)
}
c := &Conn{}
for _, ask := range []string{"ask-9", ""} {
msg := askMsg("mesh.seat.mesh-builds.tool.build", []byte(`{"x":1}`), ask)
read := "unread"
c.answerCall(msg, "mesh.seat.mesh-builds.tool.build", func(_ string, body json.RawMessage, _ bool) (any, *approval.Held, error) {
read = AskOf(body)
return nil, nil, nil
})
if read != ask || AskOf(json.RawMessage(msg.Data)) != "" {
t.Errorf("the holder read the ask %q, want %q, and none once its handler ended", read, ask)
}
}
}
// A-107: AnswerSeat reads the caller from a call's last token (dots written `~`), "" for anything that is not a
+25 -1
View File
@@ -6,9 +6,12 @@ import (
"regexp"
"sort"
"strings"
"sync"
"unsafe"
"github.com/nats-io/nats.go"
stdio "git.novox.be/novox/mesh-sdk/go"
"git.novox.be/novox/mesh-sdk/go/approval"
"git.novox.be/novox/mesh-sdk/go/calladdress"
"git.novox.be/novox/mesh-sdk/go/seatdef"
@@ -157,6 +160,21 @@ func (c *Conn) admitted(seat, verb string, definition func() (seatdef.Definition
}
}
// asks holds the ask id each call in flight carried (stdio.AskHeader), by its body's bytes, while its handler runs.
var asks sync.Map // unsafe.Pointer → string
// AskOf is the ask id the call whose body a SeatHandler was handed carried in stdio.AskHeader (design 47 H2 §775;
// H-A-351): what a holder in a process takes the ask once by. Empty when it carried none. Valid while the handler runs.
func AskOf(body json.RawMessage) string {
if len(body) == 0 {
return ""
}
if a, ok := asks.Load(unsafe.Pointer(unsafe.SliceData(body))); ok {
return a.(string)
}
return ""
}
// answerCall runs h for one call and replies {result, error, node}; a panic in h is the call's error, never silence.
func (c *Conn) answerCall(msg *nats.Msg, pattern string, h heldHandler) {
caller := ""
@@ -177,7 +195,13 @@ func (c *Conn) answerCall(msg *nats.Msg, pattern string, h heldHandler) {
reply.Error = fmt.Sprintf("%s failed: %v", msg.Subject, r)
}
}()
result, held, err := h(caller, json.RawMessage(msg.Data), wait)
body := json.RawMessage(msg.Data)
if ask := msg.Header.Get(stdio.AskHeader); ask != "" && len(body) > 0 {
key := unsafe.Pointer(unsafe.SliceData(body))
asks.Store(key, ask)
defer asks.Delete(key)
}
result, held, err := h(caller, body, wait)
switch {
case err != nil:
reply.Error = err.Error()