diff --git a/go/process/call_test.go b/go/process/call_test.go index 41bdfca8..2e50f44c 100644 --- a/go/process/call_test.go +++ b/go/process/call_test.go @@ -203,6 +203,30 @@ func TestACallIsAskedAgainOnlyWhenItReachedNoOne(t *testing.T) { t.Errorf("the holder read the ask %q, want %q, and none once its handler ended", read, ask) } } + // Design 47 H2 H-A-708 (2): a handler's `away` fault is answered as §776's `away` (error-kind, part, words) and read back + // by the caller as the transport fault; a plain refusal names no kind. + for _, row := range []struct { + err error + kind string + part string + fault bool + }{ + {&busretry.Away{Part: "the trunk's revision 512", Words: "not read yet since 23:35:40"}, "away", "the trunk's revision 512", true}, + {fmt.Errorf("reading: %w", &busretry.Away{Part: "the state seat-definitions", Words: "w"}), "away", "the state seat-definitions", true}, + {errors.New("refused"), "", "", false}, + } { + body := c.answerBody(askMsg("mesh.seat.mesh-catalog.tool.revision", []byte(`{}`), ""), "mesh.seat.mesh-catalog.tool.revision", + func(string, json.RawMessage, bool) (any, *approval.Held, error) { return nil, nil, row.err }) + var got struct{ Kind, Part string } + var raw map[string]any + _ = json.Unmarshal(body, &raw) + got.Kind, _ = raw["error-kind"].(string) + got.Part, _ = raw["part"].(string) + away := busretry.AwayIn(body) + if got.Kind != row.kind || got.Part != row.part || (away != nil) != row.fault || (away != nil && busretry.Fault(away) != true) { + t.Errorf("%v answered %s", row.err, body) + } + } // Design 47 H2 §775, H-A-405: a kept chunked call (CallChunksKept, CallFileKept) asks its verb through the kept-call // form, carries its one ask id on every chunk's acknowledgement, and past its bound ends with the kept call's words. send := buscalls.SeatChunks{Seat: "node-build-go", Verb: "output", Call: "c1"} diff --git a/go/process/serve.go b/go/process/serve.go index 4e2eabf1..0748aedc 100644 --- a/go/process/serve.go +++ b/go/process/serve.go @@ -2,6 +2,7 @@ package process import ( "encoding/json" + "errors" "fmt" "regexp" "sort" @@ -13,6 +14,7 @@ import ( 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" "git.novox.be/novox/mesh-sdk/go/seatdef" ) @@ -177,6 +179,11 @@ func AskOf(body json.RawMessage) string { // 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) { + _ = msg.Respond(c.answerBody(msg, pattern, h)) +} + +// answerBody is the reply answerCall sends. +func (c *Conn) answerBody(msg *nats.Msg, pattern string, h heldHandler) []byte { caller := "" if strings.HasSuffix(pattern, ".*") { caller = userOfToken(msg.Subject[strings.LastIndex(msg.Subject, ".")+1:]) @@ -184,7 +191,11 @@ func (c *Conn) answerCall(msg *nats.Msg, pattern string, h heldHandler) { var reply struct { Result any `json:"result,omitempty"` Error string `json:"error,omitempty"` - Node string `json:"node,omitempty"` + // ErrorKind, Part and Words are an answer of the kind away (design 47 H2 §776, H-A-708 (2)). + ErrorKind string `json:"error-kind,omitempty"` + Part string `json:"part,omitempty"` + Words string `json:"words,omitempty"` + Node string `json:"node,omitempty"` // Held is a kept call's answer (design 47 G §677; lead answer A-244). Held *approval.Held `json:"held,omitempty"` } @@ -205,6 +216,10 @@ func (c *Conn) answerCall(msg *nats.Msg, pattern string, h heldHandler) { switch { case err != nil: reply.Error = err.Error() + var away *busretry.Away + if errors.As(err, &away) { + reply.ErrorKind, reply.Part, reply.Words = busretry.ErrorKindAway, away.Part, away.Words + } case held != nil: reply.Held = held case result == nil: @@ -218,5 +233,5 @@ func (c *Conn) answerCall(msg *nats.Msg, pattern string, h heldHandler) { if err != nil { body, _ = json.Marshal(map[string]string{"error": "the answer is not JSON: " + err.Error(), "node": c.account.Node}) } - _ = msg.Respond(body) + return body } diff --git a/go/state_test.go b/go/state_test.go index 85a80848..53dcc242 100644 --- a/go/state_test.go +++ b/go/state_test.go @@ -5,6 +5,7 @@ import ( "encoding/json" "errors" "fmt" + "git.novox.be/novox/mesh-sdk/go/busretry" "io" "sort" "strings" @@ -610,3 +611,35 @@ func strs(v any) []string { } return out } + +// Design 47 H2 H-A-708 (2): Unread says `away`, naming the state and since when, while the state's copy has not taken in its +// current values, whether its watch is not asked for yet or is still handing them over, and nothing once it has. +func TestAWatchedCopyIsUnreadUntilItsCurrentValuesAreIn(t *testing.T) { + release, entered := make(chan struct{}), make(chan struct{}) + w := WatchedStatesOver(func(state string, handle func(StateChange) error) error { + close(entered) + <-release + return handle(StateChange{State: state, Key: "a", Op: OpPut, Value: []byte("1"), Current: true}) + }, nil) + asAway := func() *busretry.Away { + var away *busretry.Away + if !errors.As(w.Unread("placement"), &away) { + return nil + } + return away + } + if a := asAway(); a == nil || a.Part != "the state placement" || !strings.HasPrefix(a.Words, "not read yet since ") { + t.Fatalf("before the watch is asked: %+v", a) + } + done := make(chan struct{}) + go func() { _, _ = w.Keys("placement"); close(done) }() + <-entered + if a := asAway(); a == nil { + t.Fatal("while the watch hands its current values over, the copy is read") + } + close(release) + <-done + if err := w.Unread("placement"); err != nil { + t.Fatalf("after the current values are in: %v", err) + } +} diff --git a/go/stdio.go b/go/stdio.go index bdc6fbb7..86105db7 100644 --- a/go/stdio.go +++ b/go/stdio.go @@ -301,6 +301,12 @@ func ServeOn(name string, tools []Tool, in io.Reader, out io.Writer) error { args = map[string]any{} } result, err := runAs(t, inFlight{caller, call, ask}, args) + // A tool's `away` fault is its result in §776's form, so it reaches the caller whole (design 47 H2 H-A-708 (2), + // H-A-717 DQ-58); any other fault is an error as before. + var away *busretry.Away + if errors.As(err, &away) { + result, err = busretry.AwayAnswer(away.Part, away.Words), nil + } if err != nil { c.say(map[string]any{"id": id, "result": map[string]any{"content": []map[string]any{{"type": "text", "text": err.Error()}}, "isError": true}}) return diff --git a/go/stdio_test.go b/go/stdio_test.go index 14d331db..df7bbdcf 100644 --- a/go/stdio_test.go +++ b/go/stdio_test.go @@ -5,7 +5,9 @@ import ( "context" "encoding/json" "errors" + "fmt" "git.novox.be/novox/mesh-sdk/go/buscalls" + "git.novox.be/novox/mesh-sdk/go/busretry" "io" "strings" "sync" @@ -582,3 +584,54 @@ func TestABundlesLedgerWatchIsHandedItsChangesInOrder(t *testing.T) { } _ = inW.Close() } + +// Design 47 H2 H-A-708 (2), H-A-717 DQ-58: a tool's `away` fault is its result in §776's form (error-kind, part, words), no +// error; a plain refusal stays an error. +func TestAToolsAwayFaultIsItsResultInTheAwayForm(t *testing.T) { + servesAlone(t) + t.Setenv(ServedAs, "indexer") + inR, inW := io.Pipe() + outR, outW := io.Pipe() + go func() { + _ = ServeOn("", []Tool{ + {Name: "revision", Run: func(map[string]any) (any, error) { + return nil, fmt.Errorf("reading: %w", &busretry.Away{Part: "the trunk's revision 512", Words: "not read yet since 23:35:40"}) + }}, + {Name: "refuse", Run: func(map[string]any) (any, error) { return nil, errors.New("refused") }}, + }, inR, outW) + }() + lines := bufio.NewScanner(outR) + for i, row := range []struct { + tool, want string + isError bool + }{ + {"revision", string(mustJSON(t, busretry.AwayAnswer("the trunk's revision 512", "not read yet since 23:35:40"))), false}, + {"refuse", "refused", true}, + } { + b, _ := json.Marshal(map[string]any{"jsonrpc": "2.0", "id": i + 1, "method": "tools/call", "params": map[string]any{"name": row.tool}}) + _, _ = inW.Write(append(b, '\n')) + if !lines.Scan() { + t.Fatal("the bundle said nothing") + } + var m struct { + Result struct { + Content []struct{ Text string } + IsError bool `json:"isError"` + } + } + _ = json.Unmarshal(lines.Bytes(), &m) + if len(m.Result.Content) != 1 || m.Result.Content[0].Text != row.want || m.Result.IsError != row.isError { + t.Errorf("%s answered %s", row.tool, lines.Text()) + } + } + _ = inW.Close() +} + +func mustJSON(t *testing.T, v any) []byte { + t.Helper() + b, err := json.Marshal(v) + if err != nil { + t.Fatal(err) + } + return b +} diff --git a/go/views/follow.go b/go/views/follow.go index 7fb9f00d..7a8cb8f2 100644 --- a/go/views/follow.go +++ b/go/views/follow.go @@ -14,6 +14,7 @@ import ( "github.com/nats-io/nats.go/jetstream" "git.novox.be/novox/mesh-sdk/go/buscalls" + "git.novox.be/novox/mesh-sdk/go/busretry" ) // Follower is a reader's picture of one state of an owner (ADR 0615 §4 to §6): it asks the owner's first-view tool at @@ -67,6 +68,7 @@ type Follower struct { // catching is set while catchUp runs: one catch-up at a time. catching bool ready chan struct{} + born time.Time // when the follower began (Unread) } // Status is what a reader's read tool says of one first view (design 25 § 12.2): the owner, the tool and the revision @@ -186,6 +188,7 @@ func (f *Follower) init() { f.values = map[string]json.RawMessage{} f.ready = make(chan struct{}) f.st = Status{Owner: f.Owner, Tool: f.Tool, State: f.State} + f.born = f.now() } } @@ -222,6 +225,23 @@ func (f *Follower) Ready() <-chan struct{} { return f.ready } +// Unread says whether the first view was not answered yet: the fault `away` (busretry.Away, design 47 H2 §776) naming the +// view's owner verb (its Tool) and saying "not read yet since