From e5b66a77baad6c082b64d3d739c67d2f8efa531c Mon Sep 17 00:00:00 2001 From: jochen Date: Wed, 25 Nov 2026 17:04:25 +0100 Subject: [PATCH] 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) --- go/busretry/busretry.go | 18 ++--------- go/busretry/busretry_test.go | 7 +++-- go/process/call.go | 24 +++++--------- go/process/call_test.go | 16 +++++++--- go/stdio.go | 61 ++++++++++++++---------------------- go/stdio_test.go | 46 ++++++++++++--------------- 6 files changed, 68 insertions(+), 104 deletions(-) diff --git a/go/busretry/busretry.go b/go/busretry/busretry.go index 2b205d5f..52daccef 100644 --- a/go/busretry/busretry.go +++ b/go/busretry/busretry.go @@ -163,26 +163,12 @@ func (u *Unanswered) Error() string { func (u *Unanswered) Unwrap() error { return u.Last } // Fault says err is a transport fault of a kept call (§775): a request that reached no one, a call's own deadline -// passing, a holder's answer of the kind away (§776), or a stopping tool runner's answer of the kind stopping (design 47 -// H2 H-A-542, H-A-549); a callee's refusal never is. +// passing, or a holder's answer of the kind away (§776); a callee's refusal never is. func Fault(err error) bool { var away *Away - var stopping *Stopping - return TimedOut(err) || Undelivered(err) || errors.As(err, &away) || errors.As(err, &stopping) + return TimedOut(err) || Undelivered(err) || errors.As(err, &away) } -// ErrorKindStopping is the typed fault a stopping tool runner answers each call it does not take with (design 47 H2 -// H-A-542): its reply's `error-kind`. Nothing was done, and the new runner answers the call. -const ErrorKindStopping = "stopping" - -// Stopping is a tool runner's answer of the kind stopping, read as an error: the runner's words. -type Stopping struct{ Words string } - -func (s *Stopping) Error() string { return s.Words } - -// ErrorKind is the typed fault the answer named: stopping. -func (s *Stopping) ErrorKind() string { return ErrorKindStopping } - // ErrorKindAway is the seat error kind a holder answers when a part it depends on is away (design 47 H2 §776): its // answer's `error-kind`, beside `part` (what did not answer) and `words`. A caller reads it as a transport fault. const ErrorKindAway = "away" diff --git a/go/busretry/busretry_test.go b/go/busretry/busretry_test.go index 36a9f2bf..36da3a72 100644 --- a/go/busretry/busretry_test.go +++ b/go/busretry/busretry_test.go @@ -179,9 +179,10 @@ func subAKeptCallIsPaced(t *testing.T) { !strings.Contains(string(raw), `"error-kind":"away"`) { t.Fatalf("%s read as %+v", raw, a) } - // A stopping tool runner's answer is a transport fault too (design 47 H2 H-A-549), wrapped or not. - if !Fault(&Stopping{Words: "not taken"}) || !Fault(fmt.Errorf("asked: %w", &Stopping{Words: "not taken"})) { - t.Fatal("a stopping tool runner's answer is no transport fault") + // Design 47 H2 H-A-570 (3): no responders is the transport fault, wrapped or not; a reply naming the withdrawn kind + // stopping is the callee's answer, no fault. + if !Fault(nats.ErrNoResponders) || !Fault(fmt.Errorf("asked: %w", nats.ErrNoResponders)) || Fault(&Answered{Words: "the tool runner is stopping"}) { + t.Fatal("no responders is no transport fault, or a reply of the kind stopping is one") } if AwayIn([]byte(`{"error":"no","error-kind":"refused"}`)) != nil || AwayIn([]byte(`[1]`)) != nil || Fault(&Answered{Words: "away"}) { t.Fatal("a refusal reads as away") diff --git a/go/process/call.go b/go/process/call.go index 7d790525..1f5721af 100644 --- a/go/process/call.go +++ b/go/process/call.go @@ -189,15 +189,6 @@ func callWith(request func(string, []byte) (json.RawMessage, error), blackout fu return busretry.Do("the call on "+fallback, askBoth(request, called, fallback, data), blackout) } -// replyError is the error a reply naming one is the call's: a stopping tool runner's kind stopping is a transport fault a -// kept call's pacing asks again on, as away is (design 47 H2 H-A-542, H-A-549); any other is the callee's answer. -func replyError(words, kind string) error { - if kind == busretry.ErrorKindStopping { - return &busretry.Stopping{Words: words} - } - return answered(words) -} - // answered is the error a callee answered with: delivered, never asked again (busretry.Answered). func answered(words string) error { return &busretry.Answered{Words: words} } @@ -290,21 +281,20 @@ func (c *Conn) requestAsk(subject string, data []byte, ask string) (json.RawMess return result, nil } -// readReply reads one reply ({result, error, error-kind, node, held}): a reply naming an error is the call's failure, -// of the kind stopping a transport fault a kept call paces on (design 47 H2 H-A-549 (1)); a held call names its hold. +// readReply reads one reply ({result, error, error-kind, node, held}): a reply naming an error is the call's failure; a held +// call names its hold. func readReply(subject string, data []byte) (json.RawMessage, *approval.Held, error) { var r struct { - Result json.RawMessage `json:"result"` - Error string `json:"error"` - ErrorKind string `json:"error-kind"` - Node string `json:"node"` - Held *approval.Held `json:"held"` + Result json.RawMessage `json:"result"` + Error string `json:"error"` + Node string `json:"node"` + Held *approval.Held `json:"held"` } if err := json.Unmarshal(data, &r); err != nil { return nil, nil, fmt.Errorf("%s answered something that is not a seat's answer: %w", subject, err) } if r.Error != "" { - return nil, nil, replyError(r.Error, r.ErrorKind) + return nil, nil, answered(r.Error) } return r.Result, r.Held, nil } diff --git a/go/process/call_test.go b/go/process/call_test.go index 2e7236aa..41bdfca8 100644 --- a/go/process/call_test.go +++ b/go/process/call_test.go @@ -143,8 +143,8 @@ func TestACallIsAskedAgainOnlyWhenItReachedNoOne(t *testing.T) { } } // Design 47 H2 §776: a holder's answer of the kind away is a transport fault, called again at the pace; so is a - // stopping tool runner's reply of the kind stopping (H-A-542, H-A-549); a refusal of another kind is the caller's - // answer at once. + // refusal of another kind is the caller's answer at once, and so is a reply of the withdrawn kind stopping + // (H-A-570 (3)). for _, row := range []struct { name string answers []string @@ -153,9 +153,9 @@ func TestACallIsAskedAgainOnlyWhenItReachedNoOne(t *testing.T) { want string // the call's answer, where it is not the last answer }{ {"away, then answered", []string{`{"error":"x","error-kind":"away","part":"the forge's store","words":"401"}`, `{"merged":true}`}, 2, false, ""}, - // H-A-549 (1): the runner's reply of the kind stopping, read from its bytes, is paced on until the new runner answers. - {"stopping, then answered by the new runner", []string{`{"error":"git's trees was not taken: the tool runner is stopping","error-kind":"stopping"}`, - `{"result":{"merged":true}}`}, 2, true, `{"merged":true}`}, + // H-A-570 (3): a reply of the kind stopping, read from its bytes, is the callee's answer, asked once. + {"stopping withdrawn: answered at once", []string{`{"error":"git's trees was not taken: the tool runner is stopping","error-kind":"stopping"}`, + `{"result":{"merged":true}}`}, 1, true, ""}, {"refused: answered at once", []string{`{"error":"no","error-kind":"refused"}`}, 1, false, ""}, } { asks := 0 @@ -173,6 +173,12 @@ func TestACallIsAskedAgainOnlyWhenItReachedNoOne(t *testing.T) { want = row.answers[len(row.answers)-1] } got, err := callKeptWith(busretry.Kept{Seat: "git", Verb: "merge-pull"}, request, "called", "fallback", nil) + if row.replies && row.want == "" { + if asks != row.asks || err == nil { + t.Errorf("%s: asked %d times, %s, %v", row.name, asks, got, err) + } + continue + } if asks != row.asks || err != nil || string(got) != want { t.Errorf("%s: asked %d times, %s, %v", row.name, asks, got, err) } diff --git a/go/stdio.go b/go/stdio.go index d06b6d30..06a6c613 100644 --- a/go/stdio.go +++ b/go/stdio.go @@ -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 ", 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 `..>` hears every event on the seat's event. diff --git a/go/stdio_test.go b/go/stdio_test.go index f366988e..ca906676 100644 --- a/go/stdio_test.go +++ b/go/stdio_test.go @@ -2,6 +2,7 @@ package stdio import ( "bufio" + "context" "encoding/json" "errors" "io" @@ -9,8 +10,6 @@ import ( "sync" "testing" "time" - - "git.novox.be/novox/mesh-sdk/go/busretry" ) // A Go bundle serves as the module the runtime names, answers calls concurrently, and emits through @@ -416,27 +415,21 @@ func subABundleAsksAnAcknowledgedPublishAndASubscriptionForNewMessages(t *testin _ = inW.Close() } -// A call a stopping tool runner did not take reaches the bundle as the typed fault stopping and is asked again after a pause, -// at the kept-call pace, until the new runner answers; past Ask's bound the fault is the caller's as such, ErrStopping; a -// plain refusal, and a bare timeout, are never asked again (design 47 H2 H-A-542). Nothing sleeps: the pause moves a clock. -func TestAStoppingRunnersCallIsAskedAgainUntilTheNewRunnerAnswers(t *testing.T) { +// Ask makes its call once and asks nothing again itself (design 47 H2 H-A-570 (2)): a call met by no responders returns that fault +// after one ask, a reply of the withdrawn kind stopping is the runtime's refusal like any other, and at the bound (the setting +// ask-bound) the fault is "no answer within ", a call's own deadline. +func TestAskCallsOnceAndFaultsAtItsBound(t *testing.T) { servesAlone(t) t.Setenv(ServedAs, "builder") - clock := time.Unix(0, 0) - var paused []time.Duration - wasPause, wasNow := pause, askNow - t.Cleanup(func() { pause, askNow = wasPause, wasNow }) - pause = func(d time.Duration) { paused = append(paused, d); clock = clock.Add(d) } - askNow = func() time.Time { return clock } - // The bound is the setting ask-bound (H-A-549), here 10 s for its default 60 s. - t.Setenv("MESH_CONFIG_ASK_BOUND", "10s") + // The bound is the setting ask-bound (H-A-570 (4)), here 50 ms for its default 60 s. + t.Setenv("MESH_CONFIG_ASK_BOUND", "50ms") inR, inW := io.Pipe() outR, outW := io.Pipe() go func() { _ = ServeOn("", []Tool{{Name: "publish", Run: func(map[string]any) (any, error) { answer, err := Ask("seat:go-package-registry.publish", map[string]any{}) if err != nil { - return map[string]any{"stopping": errors.Is(err, ErrStopping), "words": err.Error()}, nil + return map[string]any{"deadline": errors.Is(err, context.DeadlineExceeded), "words": err.Error()}, nil } return json.RawMessage(answer), nil }}}, inR, outW) @@ -462,24 +455,28 @@ func TestAStoppingRunnersCallIsAskedAgainUntilTheNewRunnerAnswers(t *testing.T) send := func(m map[string]any) { b, _ := json.Marshal(m); _, _ = inW.Write(append(b, '\n')) } stopping := map[string]any{"code": -32000, "message": "registry's publish was not taken: the tool runner is stopping", "data": map[string]any{"error-kind": "stopping"}} + noResponders := map[string]any{"code": -32000, "message": "nats: no responders available for request"} for i, row := range []struct { name string answers []map[string]any // the runtime's answers to the asks, in order: an error, or a result asks int want string }{ - {"answered by the new runner after two stoppings", []map[string]any{{"error": stopping}, {"error": stopping}, {"result": "published"}}, 3, - `"published"`}, - {"stopping past the bound", []map[string]any{{"error": stopping}, {"error": stopping}, {"error": stopping}}, 3, - `"stopping":true`}, - {"a plain refusal", []map[string]any{{"error": map[string]any{"code": -32000, "message": "refused"}}}, 1, `"stopping":false`}, - {"a bare timeout", []map[string]any{{"error": map[string]any{"code": -32000, "message": "timeout"}}}, 1, `"words":"timeout"`}, + {"answered", []map[string]any{{"result": "published"}}, 1, `"published"`}, + {"no responders is returned after one ask", []map[string]any{{"error": noResponders}}, 1, `"words":"nats: no responders available for request"`}, + {"a reply of the withdrawn kind stopping is a refusal", []map[string]any{{"error": stopping}}, 1, `"deadline":false`}, + {"no answer within the bound", nil, 1, `"words":"no answer within 50ms: context deadline exceeded"`}, } { t.Run(row.name, func(t *testing.T) { - clock, paused = time.Unix(0, 0), nil - t.Cleanup(busretry.Waits(time.Second, 2*time.Second, 4*time.Second)) send(map[string]any{"jsonrpc": "2.0", "id": 100 + i, "method": "tools/call", "params": map[string]any{"name": "publish"}}) asks := 0 + if row.answers == nil { + m := next() + asks++ + if m["method"] != "mesh/ask" { + t.Fatalf("the bundle said %v, want an ask", m) + } + } for _, a := range row.answers { m := next() if m["method"] != "mesh/ask" { @@ -494,9 +491,6 @@ func TestAStoppingRunnersCallIsAskedAgainUntilTheNewRunnerAnswers(t *testing.T) if r["id"] != float64(100+i) || asks != row.asks || !strings.Contains(string(text), strings.ReplaceAll(row.want, `"`, `\"`)) { t.Errorf("asked %d times, answered %v; want %d asks and %s", asks, r, row.asks, row.want) } - if want := row.asks - 1; len(paused) != want || (want >= 2 && paused[1] != 2*paused[0]) { - t.Errorf("paused %v, want %d pauses at the kept-call pace", paused, want) - } }) } _ = inW.Close()