diff --git a/cmd/mesh-host/composecandidate_chunks_test.go b/cmd/mesh-host/composecandidate_chunks_test.go new file mode 100644 index 00000000..9641f7ae --- /dev/null +++ b/cmd/mesh-host/composecandidate_chunks_test.go @@ -0,0 +1,94 @@ +package main + +import ( + "encoding/json" + "strings" + "sync" + "testing" + "time" + + nec "git.novox.be/novox/mesh-contracts/go/clients/nodeengineclient" + "git.novox.be/novox/mesh-contracts/go/seats/nodeengine" + "git.novox.be/novox/mesh-sdk/go/buscalls" + "github.com/nats-io/nats.go" +) + +// ackingLine is a bus connection that acknowledges every chunk published on it, as a caller does, and keeps the chunks. +type ackingLine struct { + mu sync.Mutex + onAck nats.MsgHandler + chunks map[string][]byte + done chan struct{} + want int +} + +func (l *ackingLine) Subscribe(_ string, cb nats.MsgHandler) (*nats.Subscription, error) { + l.mu.Lock() + l.onAck = cb + l.mu.Unlock() + return &nats.Subscription{}, nil +} + +func (l *ackingLine) Publish(subject string, data []byte) error { + l.mu.Lock() + l.chunks[subject] = data + n, cb := len(l.chunks), l.onAck + l.mu.Unlock() + if n == l.want { + defer close(l.done) + } + ack := strings.Replace(subject, ".chunk.", ".ack.", 1) + go cb(&nats.Msg{Subject: ack}) + return nil +} + +// A-237b: asked with input and a call token, compose-candidate answers the chunks and sends the composition input on the +// connection, whatever its size; asked with no token it answers the input inline. +func TestComposeCandidateSendsItsInputInChunksWhenAskedWithACallToken(t *testing.T) { + big := strings.Repeat("x", buscalls.SeatChunkBytes+1000) // past what one answer carries + answer := func() nodeengine.ComposeCandidateAnswer { + key, _ := nec.RevisionKey("main", 7) + return nodeengine.ComposeCandidateAnswer{Node: "n1", Candidate: key, Verdict: nec.Composed, Modules: []nodeengine.Module{}, + Own: nodeengine.Module{Module: nec.NodeWord, State: nec.ModuleComposed, Digest: "sha256:" + strings.Repeat("a", 64)}, + Input: map[string]any{"data": big}} + } + key, _ := nec.RevisionKey("main", 7) + line := &ackingLine{chunks: map[string][]byte{}, done: make(chan struct{}), want: 2} + const call = "0123456789abcdef0123456789abcdef" + raw, err := answerComposeCandidate(answer(), key, true, call, line) + if err != nil { + t.Fatal(err) + } + got, err := nec.DecodeAnswer(raw.(json.RawMessage)) + if err != nil || got.Chunks != 2 || got.Input != nil { + t.Fatalf("the answer %+v (%v), want 2 chunks and no inline input", got, err) + } + select { + case <-line.done: + case <-time.After(5 * time.Second): + t.Fatalf("the chunks sent: %d, want 2", len(line.chunks)) + } + var file []byte + for n := int64(1); n <= 2; n++ { + var c buscalls.SeatChunk + if err := json.Unmarshal(line.chunks[buscalls.SeatChunks{Seat: nodeengine.Seat, Verb: nodeengine.VerbComposeCandidate, Call: call}.Chunk(n)], &c); err != nil { + t.Fatal(err) + } + file = append(file, c.Data...) + } + var in map[string]string + if err := json.Unmarshal(file, &in); err != nil || in["data"] != big { + t.Errorf("the chunks held %d bytes that read back as %v", len(file), err) + } + // No token: the input stays in the answer, as the checker reads it. + raw, err = answerComposeCandidate(answer(), key, true, "", line) + if err != nil { + t.Fatal(err) + } + if inline, err := nec.DecodeAnswer(raw.(json.RawMessage)); err != nil || inline.Chunks != 0 || inline.Input == nil { + t.Errorf("an ask with no token answered %+v (%v), want the input inline", inline, err) + } + if _, err := answerComposeCandidate(answer(), key, true, call, nil); err == nil { + t.Error("an input was to be chunked with no connection to send it on") + } +} diff --git a/cmd/mesh-host/engineseat.go b/cmd/mesh-host/engineseat.go index a1ccbe1d..4470abeb 100644 --- a/cmd/mesh-host/engineseat.go +++ b/cmd/mesh-host/engineseat.go @@ -6,6 +6,7 @@ import ( "encoding/json" "errors" "fmt" + "log" "os" "path/filepath" "strings" @@ -17,6 +18,7 @@ import ( "git.novox.be/novox/mesh-contracts/go/seats/nodeengine" occ "git.novox.be/novox/mesh-contracts/operator-and-conversation/go/clients/operatorchannelclient" stdio "git.novox.be/novox/mesh-sdk/go" + "git.novox.be/novox/mesh-sdk/go/buscalls" "github.com/novox/mesh-host/internal/accounts" "github.com/novox/mesh-host/internal/agent" "github.com/novox/mesh-host/internal/apply" @@ -378,9 +380,9 @@ func packagesFrom(node string, session *link.Session, composer *atomic.Pointer[s // nec.AnswerBound, selfcompose candidateWithin). The answer is // the seat's shape (nodeengine.ComposeCandidateAnswer), checked as it is encoded; a node whose shadow does not run yet // answers unready, which the checker takes as no answer. -func composeCandidateFrom(node string, shadow *atomic.Pointer[selfcompose.Shadow]) func(candidate string, input bool) (any, error) { - return func(candidate string, input bool) (any, error) { - ask := nodeengine.ComposeCandidateInput{Candidate: candidate, Input: input} +func composeCandidateFrom(node string, shadow *atomic.Pointer[selfcompose.Shadow]) func(candidate string, input bool, call string, line buscalls.ChunkLine) (any, error) { + return func(candidate string, input bool, call string, line buscalls.ChunkLine) (any, error) { + ask := nodeengine.ComposeCandidateInput{Candidate: candidate, Input: input, Call: call} if _, err := nec.EncodeAsk(ask); err != nil { return nil, err } @@ -394,11 +396,7 @@ func composeCandidateFrom(node string, shadow *atomic.Pointer[selfcompose.Shadow defer cancel() a = s.ComposeCandidate(ctx, ask) } - raw, err := nec.EncodeAnswer(a) - if err != nil { - return nil, fmt.Errorf("this node's answer to %s does not fit the seat's shape: %w", candidate, err) - } - return json.RawMessage(raw), nil + return answerComposeCandidate(a, candidate, input, call, line) } } @@ -425,3 +423,35 @@ func entryAgainFrom(composer *atomic.Pointer[selfcompose.Composer]) func(ctx con return c.EntryAgain(ctx, fingerprint) } } + +// answerComposeCandidate encodes the answer of compose-candidate. The verb is marked chunks (A-237b): asked with input and a +// call token, the composition input goes in chunks over the link's connection, whatever its size, and the answer names them; +// asked with no token (the checker's, until G removes it, G-6 §321) it stays inline in the answer. +func answerComposeCandidate(a nodeengine.ComposeCandidateAnswer, candidate string, input bool, call string, line buscalls.ChunkLine) (any, error) { + var chunked []byte + if input && call != "" && a.Input != nil { + if line == nil { + return nil, errors.New("this engine has no bus connection to send the composition input on") + } + body, err := json.Marshal(a.Input) + if err != nil { + return nil, fmt.Errorf("the composition input of %s cannot be encoded: %w", candidate, err) + } + chunked, a.Input = body, nil + a.Chunks = int64((len(body) + buscalls.SeatChunkBytes - 1) / buscalls.SeatChunkBytes) + } + raw, err := nec.EncodeAnswer(a) + if err != nil { + return nil, fmt.Errorf("this node's answer to %s does not fit the seat's shape: %w", candidate, err) + } + if chunked != nil { + send := buscalls.SeatChunks{Seat: nodeengine.Seat, Verb: nodeengine.VerbComposeCandidate, Call: call} + // The answer names the chunks first; the send runs after it and ends at a chunk the caller does not acknowledge. + go func() { + if err := send.Send(line, chunked, buscalls.SeatAckWithin, log.Printf); err != nil { + log.Printf("the send %s of the composition input of %s: %v", call, candidate, err) + } + }() + } + return json.RawMessage(raw), nil +} diff --git a/cmd/mesh-host/nodeagent_integration_test.go b/cmd/mesh-host/nodeagent_integration_test.go index 597da0df..0b0ef574 100644 --- a/cmd/mesh-host/nodeagent_integration_test.go +++ b/cmd/mesh-host/nodeagent_integration_test.go @@ -728,7 +728,7 @@ func h8candidate(t *testing.T, b *itBed, fp, running string) { t.Fatal(err) } before, _ := b.composed.Status() - got, err := composeCandidateFrom(itNode, &shadowNow)(key, false) + got, err := composeCandidateFrom(itNode, &shadowNow)(key, false, "", nil) if err != nil { t.Fatal(err) } diff --git a/internal/link/engineseat.go b/internal/link/engineseat.go index 6fcc0136..5551322f 100644 --- a/internal/link/engineseat.go +++ b/internal/link/engineseat.go @@ -15,6 +15,7 @@ import ( "git.novox.be/novox/mesh-contracts/go/seats/nodeengine" oc "git.novox.be/novox/mesh-contracts/operator-and-conversation/go/seats/operatorchannel" stdio "git.novox.be/novox/mesh-sdk/go" + "git.novox.be/novox/mesh-sdk/go/buscalls" "git.novox.be/novox/mesh-sdk/go/busretry" "git.novox.be/novox/mesh-sdk/go/catalogue" "git.novox.be/novox/mesh-sdk/go/seatlist" @@ -53,6 +54,9 @@ type EngineCall struct { Verb string Caller string Args map[string]any + // Line is the link's bus connection, which a verb marked chunks sends its answer's file on (buscalls.SeatChunks.Send); nil + // where the call did not come over a live link. + Line buscalls.ChunkLine } // EngineFunc answers one call: its result, or why it is refused. @@ -162,9 +166,13 @@ func (l *natsLink) serveEngineSeat(handle EngineFunc, module string) { return } apart := make(chan struct{}, apartAtOnce) + withLine := func(c EngineCall) (any, error) { + c.Line = l.conn // a verb marked chunks sends its file on the link's own connection + return handle(c) + } for _, v := range ServedEngineSeat().Serves { verb := v.Name - answer := EngineAnswer(handle, verb, l.node, apart) + answer := EngineAnswer(withLine, verb, l.node, apart) meta := EngineEndpointMetadata(v, l.node, module) _ = svc.AddEndpoint(verb, answer, micro.WithEndpointSubject(EngineToolSubject(verb, l.node)), micro.WithEndpointMetadata(meta)) diff --git a/internal/link/handacts.go b/internal/link/handacts.go index 83ae1106..a083f638 100644 --- a/internal/link/handacts.go +++ b/internal/link/handacts.go @@ -8,6 +8,7 @@ import ( "encoding/json" "errors" "fmt" + "git.novox.be/novox/mesh-sdk/go/buscalls" sdkkeys "git.novox.be/novox/mesh-sdk/go/keys" "sort" "strconv" @@ -175,7 +176,8 @@ type HandActs struct { // ComposeCandidate composes a check's candidate (the indexer's candidate key) on this node in shadow and answers per // module, applying and writing nothing; with input, also the composition input it composed from (the verb // compose-candidate, ADR 0593 §7). - ComposeCandidate func(candidate string, input bool) (any, error) + // call is the caller's token for the chunks the input is sent in (A-237b) and line the connection they go on; with no token the input is inline. + ComposeCandidate func(candidate string, input bool, call string, line buscalls.ChunkLine) (any, error) // ManifestReads is what this engine states it reads of a manifest and when its record last carried them (the verb // manifest-reads, novox/hq ADR 0618 §3). ManifestReads func() ([]string, time.Time) @@ -289,7 +291,7 @@ func (h *HandActs) Call(c EngineCall) (any, error) { if b, ok := c.Args["input"].(bool); ok { input = b } - return h.ComposeCandidate(arg("candidate"), input) + return h.ComposeCandidate(arg("candidate"), input, arg("call"), c.Line) case ActManifestReads: if h.ManifestReads == nil { return nil, errors.New("this engine states no manifest reads yet: its node agent has not started") diff --git a/internal/selfcompose/candidate.go b/internal/selfcompose/candidate.go index 4aedc170..bbc6f6ab 100644 --- a/internal/selfcompose/candidate.go +++ b/internal/selfcompose/candidate.go @@ -238,7 +238,9 @@ func (s *Shadow) composeCandidate(ctx context.Context, c *Composer, ask nodeengi } } }) - if sentSize > maxSentInput { + // An ask with a call token takes the input in chunks, whatever its size (A-237b); one with none is the checker's, answered + // inline until G removes the checker (G-6 §321), so past what the bus carries in one answer it is refused. + if sentSize > maxSentInput && ask.Call == "" { // Past what the bus carries in one answer, the checker would hear nothing: refused, saying so. size := sentSize a.Input = nil