From 78a0c4d154363036fd90cc5bb931d7e8800b88d6 Mon Sep 17 00:00:00 2001 From: jochen Date: Mon, 23 Nov 2026 11:45:40 +0100 Subject: [PATCH] stdio: a stopping tool runner's fault reaches the bundle as such and is asked again (H-A-542) A call a stopping tool runner did not take reached a bundle as a bare words error, or, before the runner said so, as a "timeout", so a build step's publish failed when the runner restarted (main.440). The runtime's answer now names its typed fault (the error's data error-kind); stdio keeps it (ErrStopping, ErrorKind), and Ask asks again after a pause at the kept-call pace until the new runner answers or Ask's own bound ends, then hands the caller ErrStopping. A plain refusal and a bare timeout are never asked again. Builds: design 47 H2 H-A-542 --- go/stdio.go | 51 +++++++++++++++++++++++++--- go/stdio_test.go | 87 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 134 insertions(+), 4 deletions(-) diff --git a/go/stdio.go b/go/stdio.go index 4fb2ee17..523dcc69 100644 --- a/go/stdio.go +++ b/go/stdio.go @@ -55,6 +55,10 @@ type message struct { Error *struct { Code int `json:"code"` Message string `json:"message"` + // Data carries a typed fault's kind, `error-kind` (a stopping tool runner's ErrorKindStopping, design 47 H2 H-A-542). + Data struct { + ErrorKind string `json:"error-kind"` + } `json:"data"` } `json:"error,omitempty"` } @@ -132,7 +136,7 @@ func (c *channel) askAs(id, method string, params any, timeout time.Duration) (j select { case m := <-answer: if m.Error != nil { - return nil, &refused{message: m.Error.Message, code: m.Error.Code} + return nil, &refused{message: m.Error.Message, code: m.Error.Code, kind: m.Error.Data.ErrorKind} } return m.Result, nil case <-time.After(timeout): @@ -147,15 +151,31 @@ func (c *channel) askAs(id, method string, params any, timeout time.Duration) (j type refused struct { message string code int + // kind is the typed fault the answer names, its `error-kind` (ErrorKindStopping), "" 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. +// 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. func (r *refused) Is(target error) bool { - return target == ErrChanged && r.code == CodeChanged || target == errors.ErrUnsupported && r.code == CodeNoMethod + return target == ErrChanged && r.code == CodeChanged || target == errors.ErrUnsupported && r.code == CodeNoMethod || + target == ErrStopping && r.kind == ErrorKindStopping } +// ErrorKind is the typed fault the runtime's answer named (ErrorKindStopping), "" 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 = "stopping" + +// 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 @@ -583,9 +603,32 @@ func Ask(key string, body any) (json.RawMessage, error) { if err != nil { return nil, err } - return c.ask("mesh/ask", map[string]any{"key": key, "body": body}, 60*time.Second) + // 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 ends; then its fault is the caller's as such, ErrStopping (design 47 H2 H-A-542). + 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) + } } +// askBound is how long Ask waits for its answer, asks again included; pause is how it waits between two asks and askNow its +// clock, a test's to replace so nothing sleeps. +var ( + askBound = 60 * time.Second + 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 cb462e73..0f915f1c 100644 --- a/go/stdio_test.go +++ b/go/stdio_test.go @@ -9,6 +9,8 @@ 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 @@ -413,3 +415,88 @@ 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) { + servesAlone(t) + t.Setenv(ServedAs, "builder") + clock := time.Unix(0, 0) + var paused []time.Duration + wasPause, wasNow, wasBound := pause, askNow, askBound + t.Cleanup(func() { pause, askNow, askBound = wasPause, wasNow, wasBound }) + pause = func(d time.Duration) { paused = append(paused, d); clock = clock.Add(d) } + askNow = func() time.Time { return clock } + askBound = 10 * time.Second + 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 json.RawMessage(answer), nil + }}}, inR, outW) + }() + lines := bufio.NewScanner(outR) + next := func() map[string]any { + done := make(chan map[string]any, 1) + go func() { + if lines.Scan() { + var m map[string]any + _ = json.Unmarshal(lines.Bytes(), &m) + done <- m + } + }() + select { + case m := <-done: + return m + case <-time.After(3 * time.Second): + t.Fatal("the bundle said nothing") + return nil + } + } + 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"}} + 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"`}, + } { + 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 + for _, a := range row.answers { + m := next() + if m["method"] != "mesh/ask" { + t.Fatalf("the bundle said %v, want an ask", m) + } + asks++ + a["jsonrpc"], a["id"] = "2.0", m["id"] + send(a) + } + r := next() + text, _ := json.Marshal(r["result"]) + 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() +}