S2 ask-once-go: Ask calls once; the kind stopping goes

Builds: design 47 H2 H-A-570 (2) (3) (4), H-A-624 (3)
This commit is contained in:
2026-11-25 17:04:29 +01:00
parent cedfa71169
commit e5b66a77ba
6 changed files with 68 additions and 104 deletions
+24 -37
View File
@@ -14,6 +14,7 @@ package stdio
import (
"bufio"
"context"
"crypto/rand"
"encoding/hex"
"encoding/json"
@@ -143,39 +144,39 @@ func (c *channel) askAs(id, method string, params any, timeout time.Duration) (j
c.mu.Lock()
delete(c.waiting, id)
c.mu.Unlock()
return nil, fmt.Errorf("the runtime did not answer %s in %s", method, timeout)
return nil, &noAnswer{method: method, timeout: timeout}
}
}
// noAnswer is the runtime's silence: its deadline passed with no answer to the request.
type noAnswer struct {
method string
timeout time.Duration
}
func (n *noAnswer) Error() string {
return fmt.Sprintf("the runtime did not answer %s in %s", n.method, n.timeout)
}
// refused is the runtime's error answer, as opposed to no answer: what it refused, it did not do.
type refused struct {
message string
code int
// kind is the typed fault the answer names, its `error-kind` (ErrorKindStopping), "" for none.
// kind is the typed fault the answer names, its `error-kind`, "" for none.
kind string
}
func (r *refused) Error() string { return r.message }
// Is makes a compare-and-set the runtime refused because another writer was first ErrChanged, and a call a stopping tool
// runner did not take ErrStopping.
// Is makes a compare-and-set the runtime refused because another writer was first ErrChanged.
func (r *refused) Is(target error) bool {
return target == ErrChanged && r.code == CodeChanged || target == errors.ErrUnsupported && r.code == CodeNoMethod ||
target == ErrStopping && r.kind == ErrorKindStopping
return target == ErrChanged && r.code == CodeChanged || target == errors.ErrUnsupported && r.code == CodeNoMethod
}
// ErrorKind is the typed fault the runtime's answer named (ErrorKindStopping), "" for none: a reader that does not import
// ErrorKind is the typed fault the runtime's answer named, "" for none: a reader that does not import
// this package tells it by this method, never by the words.
func (r *refused) ErrorKind() string { return r.kind }
// ErrorKindStopping is the typed fault a stopping tool runner answers each call it does not take with (design 47 H2 H-A-542):
// the answer's `error-kind`. Ask asks again after a pause until the new runner answers or its bound ends.
const ErrorKindStopping = busretry.ErrorKindStopping
// ErrStopping is a call a stopping tool runner did not take (ErrorKindStopping): nothing was done, and the new runner answers
// it. Told apart by its kind, never by its words.
var ErrStopping = errors.New("the tool runner is stopping")
// CodeNoMethod is the error code the runtime answers a verb it does not have with (JSON-RPC's method not found).
const CodeNoMethod = -32601
@@ -603,27 +604,19 @@ func Ask(key string, body any) (json.RawMessage, error) {
if err != nil {
return nil, err
}
// A call a stopping tool runner did not take is asked again after a pause, at the kept-call pace, until the new runner
// answers or Ask's own bound (the setting ask-bound) ends; then its fault is the caller's as such, ErrStopping (design 47 H2
// H-A-542, H-A-549).
// The call is made once and nothing is asked again here: a call its caller marks safe to repeat is called again by a kept
// call (design 47 H2 §775) through no responders as through every transport fault. At the bound (the setting ask-bound)
// the fault is "no answer within <bound>", a call's own deadline (design 47 H2 H-A-570 (2), (4)).
askBound, err := SettingDuration(AskBoundSetting, DefaultAskBound)
if err != nil {
return nil, err
}
start, wait := askNow(), busretry.KeptPace().First
for {
left := askBound - askNow().Sub(start)
answer, err := c.ask("mesh/ask", map[string]any{"key": key, "body": body}, left)
if !errors.Is(err, ErrStopping) {
return answer, err
}
if left = askBound - askNow().Sub(start); wait >= left {
return nil, fmt.Errorf("%s: the tool runner was stopping, asked again until Ask's bound of %s: %w", key, askBound, err)
}
fmt.Fprintf(os.Stderr, "[mesh-sdk] %s: the tool runner is stopping (%v); asked again in %s\n", key, err, wait)
pause(wait)
wait = min(wait*2, busretry.KeptPace().Max)
answer, err := c.ask("mesh/ask", map[string]any{"key": key, "body": body}, askBound)
var timedOut *noAnswer
if errors.As(err, &timedOut) {
return nil, fmt.Errorf("no answer within %s: %w", askBound, context.DeadlineExceeded)
}
return answer, err
}
// AskBoundSetting is the module's setting of how long Ask waits for its answer, asks again included, a duration text;
@@ -633,12 +626,6 @@ const (
DefaultAskBound = 60 * time.Second
)
// pause is how Ask waits between two asks and askNow its clock, a test's to replace so nothing sleeps.
var (
pause = time.Sleep
askNow = time.Now
)
// TopicMatches says whether an event's type matches a pattern: `*` is one dot-separated segment, `#`
// (or `**`) any number, as the TypeScript SDK matches; the bus's own `>` as the last token matches one or more
// further tokens (design 47 G A-101), so an adopted follower's `<seat>.<event>.>` hears every event on the seat's event.