node-engine: compose-candidate sends its composition input in chunks over the link's connection when asked with a call token (A-237b)

Builds: design 47 G (G-22 lead answers 2) A-237b; A-237, A-249
This commit is contained in:
2026-11-16 14:38:31 +01:00
parent 3b365ba7cc
commit f539cdb7ec
6 changed files with 149 additions and 13 deletions
@@ -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")
}
}
+38 -8
View File
@@ -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
}
+1 -1
View File
@@ -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)
}