Files
mesh-delivery/modules/mesh-delivery-sdk/buildstep/stepoutput.go
T
jschoubben fad9f059ee BM4b (sdk): an end over the steps state's value is read through the verb answer
A step's end is an event, not a verb's answer, so an end over what the steps
state's value or step-ended carries has a reader of its own: keepOver keeps it
in the steps objects as answer.<ask>, the step key and step-ended carry
answer-object in place of output-object, and the build seats gain the read
verb answer (ask and call, marked chunks); output stays the run text's reader.
Step.Whole and StepEnded.ReadOutput read through answer.

Builds: design 47 H2 §791; H-A-412 (2) and its addendum
2026-11-22 01:08:58 +01:00

106 lines
4.4 KiB
Go

package buildstep
import (
"crypto/rand"
"encoding/base64"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
)
// OutputVerb is the verb of a build seat that answers a run's test output kept in its outputs objects (design 47 H2 §791,
// H-A-367 (1), H-A-412): the output's last 64 KB, its size, the failing tests with their words, or no longer kept.
const OutputVerb = "output"
// AnswerVerb is the verb of a build seat that answers, in chunks, a step's end kept as an object because it is over what the
// steps state's value or the event carries (design 47 G A-254, A-290; H2 H-A-412 addendum): the object `answer-object` names.
const AnswerVerb = "answer"
// AskVerb asks a seat verb as the runtime calls it (stdio.Ask): the key `seat:<seat>.<verb>[@<node>]` and the body in, the
// verb's answer out. A verb marked chunks is asked by the plain call, and the runtime hands the whole file in `file` (A-249).
type AskVerb func(key string, body any) (json.RawMessage, error)
// ReadOutput is the answer a build seat's step-ended carries (design 47 G A-254): its inline `output`, else the object
// `answer-object` names, read through the verb `answer` of the seat of language on the node that ran the ask. Every asker of a
// run reads the answer this way, whichever build seat ran it; none reads the seat's step state.
func (e StepEnded) ReadOutput(language string, ask AskVerb) ([]byte, error) {
if e.AnswerObject == "" {
return []byte(e.Output), nil
}
return readObject(language, e.Node, e.Ask, e.AnswerObject, ask)
}
// Whole is the step with its answer whole (design 47 G A-290): a step whose key names its answer as the object
// `answer-object`, its value being over what the steps state holds, has it read through the verb `answer` of the seat of
// language on the step's node, and put back where the key holds the answer itself: a run's (run true) as its run answer's
// output, any other's as its result. A step whose key holds its answer is answered as it is. Every reader of a step's answer
// reads it so, the checker and the builder alike (no step reads passed in one place and failed in another).
func (s Step) Whole(language string, run bool, ask AskVerb) (Step, error) {
if s.AnswerObject == "" {
return s, nil
}
body, err := readObject(language, s.Node, s.ID, s.AnswerObject, ask)
if err != nil {
return s, err
}
if !run {
if !json.Valid(body) {
return s, fmt.Errorf("buildstep: the answer %s of the ask %s is not JSON", s.AnswerObject, s.ID)
}
s.Result, s.AnswerObject = json.RawMessage(body), ""
return s, nil
}
var answer RunAnswer
if len(s.Result) > 0 {
if err := json.Unmarshal(s.Result, &answer); err != nil {
return s, fmt.Errorf("buildstep: the run answer of the ask %s is not one: %w", s.ID, err)
}
}
answer.Output = json.RawMessage(body)
raw, err := json.Marshal(answer)
if err != nil {
return s, fmt.Errorf("buildstep: the run answer of the ask %s with its output %s: %w", s.ID, s.AnswerObject, err)
}
s.Result, s.AnswerObject = raw, ""
return s, nil
}
// readObject reads the object a build seat's holder on node keeps of the ask askID through the seat's verb `answer`.
func readObject(language, node, askID, object string, ask AskVerb) ([]byte, error) {
seat, err := BuildSeat(language)
if err != nil {
return nil, err
}
if ask == nil {
return nil, errors.New("buildstep: the answer is in the answer object " + object + ", which this reader has no verb to read")
}
var call [16]byte
if _, err := rand.Read(call[:]); err != nil {
return nil, err
}
raw, err := ask("seat:"+seat+"."+AnswerVerb+"@"+node, map[string]any{"ask": askID, "call": hex.EncodeToString(call[:])})
if err != nil {
return nil, fmt.Errorf("buildstep: asking %s for the answer %s of the ask %s: %w", seat, object, askID, err)
}
var got struct {
Ask string `json:"ask"`
File string `json:"file"`
Chunks int `json:"chunks"`
}
if err := json.Unmarshal(raw, &got); err != nil {
return nil, fmt.Errorf("buildstep: the verb answer of %s answered no answer for %s: %w", seat, askID, err)
}
if got.Ask != askID {
return nil, fmt.Errorf("buildstep: the verb answer of %s answered the ask %q, asked %s", seat, got.Ask, askID)
}
if got.File == "" {
return nil, fmt.Errorf("buildstep: %s on %s keeps no answer of the ask %s", seat, node, askID)
}
body, err := base64.StdEncoding.DecodeString(got.File)
if err != nil {
return nil, fmt.Errorf("buildstep: the answer of %s in no base64: %w", askID, err)
}
return body, nil
}