The pipeline gains `unlogged`: a step's end is written in its key with its step-log entry in one compare-and-set, said as step-ended, then appended to the step log and cleared; a log that refuses an append ends no step, says full and starts no new pipeline; at its start the runner appends every unlogged entry in order after reading back the log's last entry of that pipeline, and once a minute while the log is full. The tools runner_unlogged and runner_unlogged_append (a hand act). Builds: design 47 G §486, §606, §549 Claude-Session: https://claude.ai/code/session_01NcbgfGKaxw984XyAPfLm4h
234 lines
9.1 KiB
Go
234 lines
9.1 KiB
Go
package pipeline
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
"time"
|
|
|
|
"git.novox.be/novox/mesh-contracts/operator-and-conversation/go/clients/operatorchannelclient"
|
|
oc "git.novox.be/novox/mesh-contracts/operator-and-conversation/go/seats/operatorchannel"
|
|
seat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshpipelinerunner"
|
|
"git.novox.be/novox/mesh-sdk/go/keys"
|
|
)
|
|
|
|
// The approval step is an authorising ask of tier approve, the seat operator-channel's accepted kind ask under the
|
|
// runner's own name, its about the module and the watched branch only, so a newer commit's ask replaces the older one's
|
|
// (design 47 G §26, §37; ADR 0666 §10). It is asked last, once every other required step passed.
|
|
const (
|
|
OptionApprove = "approve"
|
|
OptionDecline = "decline"
|
|
)
|
|
|
|
// About is what an approval ask is about: the module and the watched branch, each a key's name, joined by a dot.
|
|
func About(module, branch string) string {
|
|
return keys.EscapeName(module) + "." + keys.EscapeName(branch)
|
|
}
|
|
|
|
// approvalDue is the index of the approval step to ask now (§26): waiting, not bypassed, every other step ended and every
|
|
// other required step passed or carried; -1 when none is due.
|
|
func approvalDue(p seat.Pipeline) int {
|
|
at := -1
|
|
for i, s := range p.Steps {
|
|
if s.Step == seat.StepResultStepApproval {
|
|
if s.Status != seat.StepResultStatusBypass && s.Outcome == seat.StepResultOutcomeWaiting {
|
|
at = i
|
|
}
|
|
continue
|
|
}
|
|
if s.Outcome == seat.StepResultOutcomeWaiting || s.Outcome == seat.StepResultOutcomeRunning {
|
|
return -1
|
|
}
|
|
if s.Status == seat.StepResultStatusRequired && s.Outcome != seat.StepResultOutcomePassed && s.Outcome != seat.StepResultOutcomeCarried {
|
|
return -1
|
|
}
|
|
}
|
|
return at
|
|
}
|
|
|
|
// ApprovalAsk is the ask of a pipeline's approval step. Its id and its expiry follow from the pipeline and from when the
|
|
// step was asked, so the ask built again for the same pipeline is the same ask, digest included: a warrant is held to it.
|
|
func ApprovalAsk(p seat.Pipeline, from time.Time) (oc.Ask, error) {
|
|
// An approval asked again (ask-again) is an ask of its own, its id following from when it was asked again, so the
|
|
// ask rebuilt from the step is the same and the earlier, decided ask is not its duplicate.
|
|
idOf := p.Pipeline + "\n" + p.Commit
|
|
for _, st := range p.Steps {
|
|
if st.Step == seat.StepResultStepApproval && st.Rerun {
|
|
idOf += "\nasked again\n" + from.UTC().Format(time.RFC3339Nano)
|
|
}
|
|
}
|
|
sum := sha256.Sum256([]byte(idOf))
|
|
act := func(do string) (string, error) {
|
|
return operatorchannelclient.ActDigest(operatorchannelclient.Act{"pipeline": p.Pipeline, "commit": p.Commit,
|
|
"fingerprint": p.Fingerprint, "do": do})
|
|
}
|
|
approve, err := act(OptionApprove)
|
|
if err != nil {
|
|
return oc.Ask{}, err
|
|
}
|
|
decline, err := act(OptionDecline)
|
|
if err != nil {
|
|
return oc.Ask{}, err
|
|
}
|
|
a := oc.Ask{ID: "approval-" + hex.EncodeToString(sum[:12]), About: About(p.Module, p.Branch), Who: operatorchannelclient.Operator,
|
|
Headline: trimRunes("Deliver "+p.Module+" from "+p.Branch+"?", operatorchannelclient.HeadlineLength),
|
|
Explanation: fmt.Sprintf("Every required step of %s's pipeline at commit %s on %s passed; its policy asks your approval before it is delivered.", p.Module, p.Commit, p.Branch),
|
|
Details: "pipeline " + p.Pipeline + ", fingerprint " + p.Fingerprint,
|
|
Expires: operatorchannelclient.FormatTime(from.Add(operatorchannelclient.ApproveLasts)),
|
|
OnExpiry: "the step ends declined and the commit is not delivered; ask-again asks once more",
|
|
Options: []oc.Option{
|
|
{ID: OptionApprove, Label: "Approve", Does: "delivers this commit of " + p.Module, Level: operatorchannelclient.Approve, Binds: approve},
|
|
{ID: OptionDecline, Label: "Decline", Does: "ends the step declined; nothing is delivered", Level: operatorchannelclient.Approve, Binds: decline},
|
|
}}
|
|
return a, operatorchannelclient.Check(a, from)
|
|
}
|
|
|
|
func trimRunes(s string, n int) string {
|
|
r := []rune(s)
|
|
if len(r) <= n {
|
|
return s
|
|
}
|
|
return string(r[:n-1]) + "…"
|
|
}
|
|
|
|
// Asker is the runner's side of the operator-channel: Ask publishes an ask under the runner's name, its duplicate id the
|
|
// ask's own, so an ask published twice is kept once.
|
|
type Asker interface {
|
|
Ask(oc.Ask) error
|
|
}
|
|
|
|
// askApproval asks p's approval step when it is due, before the pipeline is written: an ask published and then not
|
|
// written is published again, the same ask, when its step's end is heard again.
|
|
func askApproval(key string, p *seat.Pipeline, from time.Time, asker Asker) (bool, error) {
|
|
i := approvalDue(*p)
|
|
if i < 0 || asker == nil {
|
|
return false, nil
|
|
}
|
|
// An approval asked again keeps the time ask-again wrote, so an ask published and then not written is the same ask.
|
|
if s := p.Steps[i]; s.Rerun && s.Started != nil {
|
|
from = *s.Started
|
|
}
|
|
a, err := ApprovalAsk(*p, from)
|
|
if err != nil {
|
|
return false, fmt.Errorf("the approval of %s cannot be asked: %w", key, err)
|
|
}
|
|
if err := asker.Ask(a); err != nil {
|
|
return false, fmt.Errorf("the approval of %s is not asked: %w", key, err)
|
|
}
|
|
s := &p.Steps[i]
|
|
s.Outcome, s.Ask, s.Seat, s.Started = seat.StepResultOutcomeRunning, a.ID, seat.Seat, &from
|
|
s.Why = "waiting on the operator"
|
|
return true, nil
|
|
}
|
|
|
|
// ErrNoAsk is a warrant naming no approval step the runner keeps for its about.
|
|
var ErrNoAsk = errors.New("no approval step asked under the warrant's ask")
|
|
|
|
// decide ends an approval step on its warrant: a chosen approve, held to the ask rebuilt from the step, ends it passed; a
|
|
// chosen decline, an expiry, a cancel or a refusal ends it declined; a replacement by a newer commit's ask ends it
|
|
// superseded (design 47 G §37). It says whether the step changed.
|
|
func decide(p *seat.Pipeline, w oc.Warrant, asker string, now time.Time) (bool, error) {
|
|
for i := range p.Steps {
|
|
s := &p.Steps[i]
|
|
if s.Step != seat.StepResultStepApproval || s.Ask != w.Ask || s.Started == nil {
|
|
continue
|
|
}
|
|
if s.Outcome != seat.StepResultOutcomeRunning {
|
|
return false, nil
|
|
}
|
|
switch w.Outcome {
|
|
case oc.WarrantOutcomeChosen:
|
|
asked, err := ApprovalAsk(*p, *s.Started)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
o, err := operatorchannelclient.For(w, asker, asked)
|
|
if err != nil {
|
|
return false, fmt.Errorf("the warrant for %s is not taken: %w", w.Ask, err)
|
|
}
|
|
s.Outcome = seat.StepResultOutcomeDeclined
|
|
if o.ID == OptionApprove {
|
|
s.Outcome = seat.StepResultOutcomePassed
|
|
}
|
|
if w.By != nil {
|
|
s.By = w.By.Identity
|
|
}
|
|
s.Why = "the operator chose " + o.ID
|
|
case oc.WarrantOutcomeReplaced:
|
|
s.Outcome, s.Why = seat.StepResultOutcomeSuperseded, "a newer commit's ask replaced it"
|
|
default:
|
|
s.Outcome, s.Why = seat.StepResultOutcomeDeclined, "the ask ended "+w.Outcome+" "+w.Words
|
|
}
|
|
s.Ended = &now
|
|
return true, nil
|
|
}
|
|
return false, ErrNoAsk
|
|
}
|
|
|
|
// ErrNoRecord is a decided event whose ask the router's record holds no warrant for yet: the event is heard again.
|
|
var ErrNoRecord = errors.New("the router's record of the ask holds no warrant yet")
|
|
|
|
// OnDecided takes the operator-channel's decided for the runner's own asks: it only wakes the runner (design 47 G A-89), which
|
|
// reads the warrant from the router's record of the ask through Where, never from the event's body (§37, §700). The approval
|
|
// step asked under that ask, among the pipelines of the module and watched branch the ask's about names, ends as decide says.
|
|
func (h *Hearer) OnDecided(event oc.Warrant) (seat.Pipeline, error) {
|
|
if event.Asker != h.Name {
|
|
return seat.Pipeline{}, fmt.Errorf("%w: the decided event is %s's, not %s's", ErrRefused, event.Asker, h.Name)
|
|
}
|
|
if h.Where == nil {
|
|
return seat.Pipeline{}, fmt.Errorf("the runner reads no router's record, so the ask %s is not taken", event.Ask)
|
|
}
|
|
_, rec, err := h.Where(event.Ask)
|
|
if err != nil {
|
|
return seat.Pipeline{}, fmt.Errorf("the router's record of the ask %s cannot be read: %w", event.Ask, err)
|
|
}
|
|
if rec == nil {
|
|
return seat.Pipeline{}, fmt.Errorf("%w: %s", ErrNoRecord, event.Ask)
|
|
}
|
|
w := *rec
|
|
module, branch, ok := strings.Cut(w.About, ".")
|
|
if !ok {
|
|
return seat.Pipeline{}, fmt.Errorf("%w: the warrant for %s is about %q, not a module and a branch", ErrNoAsk, w.Ask, w.About)
|
|
}
|
|
found, err := h.Store.Keys(branch + ".*." + module)
|
|
if err != nil {
|
|
return seat.Pipeline{}, err
|
|
}
|
|
for _, key := range found {
|
|
for {
|
|
p, rev, err := h.Store.Get(key)
|
|
if err != nil {
|
|
return seat.Pipeline{}, err
|
|
}
|
|
now := h.now()
|
|
changed, err := decide(&p, w, h.Name, now)
|
|
if errors.Is(err, ErrNoAsk) {
|
|
break
|
|
}
|
|
if err != nil || !changed {
|
|
return p, err
|
|
}
|
|
ended := endPipeline(&p, now)
|
|
i := stepOfAsk(p, w.Ask)
|
|
if i >= 0 {
|
|
p.Unlogged = append(p.Unlogged, endEntry(key, p.Steps[i], now))
|
|
}
|
|
if _, err := h.Store.Update(key, p, rev); errors.Is(err, ErrChanged) {
|
|
continue
|
|
} else if err != nil {
|
|
return p, err
|
|
}
|
|
for _, ev := range []string{seat.EventWaitEnded, seat.EventStepEnded} {
|
|
if err := h.say(ev, p, &p.Steps[i]); err != nil {
|
|
return p, fmt.Errorf("pipeline %s's %s is written but not said: %w", key, ev, err)
|
|
}
|
|
}
|
|
h.appendUnlogged(key)
|
|
return p, h.said(p, false, ended)
|
|
}
|
|
}
|
|
return seat.Pipeline{}, fmt.Errorf("%w: %s about %s", ErrNoAsk, w.Ask, w.About)
|
|
}
|