Compare commits
90
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
247d89e152 | ||
|
|
7ade318cfa | ||
|
|
7e96d61268 | ||
|
|
9d43fa4b24 | ||
|
|
cd3ef2fd41 | ||
|
|
b75481733b | ||
|
|
282758456c | ||
|
|
0e9f916806 | ||
|
|
daa5d3ea5f | ||
|
|
be993cb2c5 | ||
|
|
cd3b8fe8e9 | ||
|
|
f0c21ca033 | ||
|
|
c6ec42bc2e | ||
|
|
f65d789052 | ||
|
|
62b8251b99 | ||
|
|
90c88fc7b5 | ||
|
|
740c218139 | ||
|
|
1481465ec7 | ||
|
|
d2b8f64c94 | ||
|
|
eed95cd63b | ||
|
|
84106a9338 | ||
|
|
9ed13cd9dc | ||
|
|
d76c3588e3 | ||
|
|
377d42f5ea | ||
|
|
9ade421e12 | ||
|
|
46f3dc8cd7 | ||
|
|
7da6dd20a1 | ||
|
|
c87cf1ace5 | ||
|
|
3f3f6789dd | ||
|
|
8b5cc8be8d | ||
|
|
7c6aed9d69 | ||
|
|
4f9f307eaf | ||
|
|
e87c6cd3b6 | ||
|
|
73c2286ec9 | ||
|
|
3be2f79921 | ||
|
|
cf5ea429a2 | ||
|
|
1419dcef7d | ||
|
|
8c96a02a01 | ||
|
|
03cc8eac90 | ||
|
|
f31d6c645d | ||
|
|
bbc9eb7c93 | ||
|
|
1310c28102 | ||
|
|
affffa9c23 | ||
|
|
94f3cde557 | ||
|
|
6962da2f98 | ||
|
|
0aee792af6 | ||
|
|
86367f65b2 | ||
|
|
932e5c4e4b | ||
|
|
930c0fb783 | ||
|
|
79bd15884d | ||
|
|
13a0277f8c | ||
|
|
fcdf21d615 | ||
|
|
d6b75616f5 | ||
|
|
096668a14a | ||
|
|
2b1bc1d302 | ||
|
|
a55241c30b | ||
|
|
e1ee303a1c | ||
|
|
c9f8e6e8cd | ||
|
|
0afca54a0f | ||
|
|
8bb73eb61f | ||
|
|
229c887999 | ||
|
|
cdf168706b | ||
|
|
ae78fb039f | ||
|
|
928de07c43 | ||
|
|
666be37916 | ||
|
|
ccab80d726 | ||
|
|
1168376930 | ||
|
|
2679399d60 | ||
|
|
ce30601b0c | ||
|
|
d2c1a24a24 | ||
|
|
88ff34e855 | ||
|
|
76195e421f | ||
|
|
59704456c3 | ||
|
|
4a99f7044e | ||
|
|
7d17441a9a | ||
|
|
c006538064 | ||
|
|
bfc877209f | ||
|
|
99f3e23492 | ||
|
|
e8204a9c27 | ||
|
|
89397466aa | ||
|
|
28f616e379 | ||
|
|
8ea8ef9768 | ||
|
|
fb24d384a2 | ||
|
|
5236c2aa72 | ||
|
|
5df0fb64f5 | ||
|
|
ed536102ac | ||
|
|
a98b6ba77e | ||
|
|
b1125c02cf | ||
|
|
a4bc3d95ed | ||
|
|
6ba73d4e2a |
@@ -0,0 +1,123 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/parts"
|
||||
|
||||
"distribution/internal/holder"
|
||||
)
|
||||
|
||||
// stepBound bounds each bus operation of a publish well under the tool runner's 30 s call bound, so a hang is an error
|
||||
// naming its step and the publish answers with it.
|
||||
const stepBound = 10 * time.Second
|
||||
|
||||
// step runs f, logs its start and its end with its time, and gives up on it after bound: the error names the step. A step
|
||||
// given up on is not cancelled (the SDK's bus calls take no context): its call ends at the SDK's own bound of the ask, and
|
||||
// the log says, by the step's name, that it was given up and later when the call behind it ended and how (A-443, A-443a).
|
||||
func step[T any](bound time.Duration, what string, f func() (T, error)) (T, error) {
|
||||
type res struct {
|
||||
v T
|
||||
err error
|
||||
}
|
||||
start := time.Now()
|
||||
logf("step %s starts", what)
|
||||
ch := make(chan res, 1)
|
||||
go func() { v, err := f(); ch <- res{v, err} }()
|
||||
select {
|
||||
case r := <-ch:
|
||||
logf("step %s ended after %s (%v)", what, time.Since(start).Round(time.Millisecond), r.err)
|
||||
return r.v, r.err
|
||||
case <-time.After(bound):
|
||||
var zero T
|
||||
err := fmt.Errorf("%s did not answer within %s", what, bound)
|
||||
logf("step %s: %v; given up, its call is left to end at the SDK's own bound", what, err)
|
||||
go func() {
|
||||
r := <-ch
|
||||
logf("step %s, given up after %s: its call ended after %s (%v)", what, bound, time.Since(start).Round(time.Millisecond), r.err)
|
||||
}()
|
||||
return zero, err
|
||||
}
|
||||
}
|
||||
|
||||
func stepErr(bound time.Duration, what string, f func() error) error {
|
||||
_, err := step(bound, what, func() (struct{}, error) { return struct{}{}, f() })
|
||||
return err
|
||||
}
|
||||
|
||||
// boundedObjects is the incoming object store with each operation bounded and logged.
|
||||
type boundedObjects struct{ parts.Objects }
|
||||
|
||||
func (b boundedObjects) Used() (uint64, error) {
|
||||
return step(stepBound, "the incoming object store's fill (objects)", b.Objects.Used)
|
||||
}
|
||||
func (b boundedObjects) Put(name string, data []byte) error {
|
||||
return stepErr(stepBound, "the put of a chunk in the incoming object store (objects)", func() error { return b.Objects.Put(name, data) })
|
||||
}
|
||||
func (b boundedObjects) Get(name string) ([]byte, error) {
|
||||
return step(stepBound, "the get of a chunk from the incoming object store (objects)", func() ([]byte, error) { return b.Objects.Get(name) })
|
||||
}
|
||||
func (b boundedObjects) Delete(name string) error {
|
||||
return stepErr(stepBound, "the delete of a chunk in the incoming object store (objects)", func() error { return b.Objects.Delete(name) })
|
||||
}
|
||||
|
||||
// boundedTransfers is the transfers state with its reads and writes bounded and logged.
|
||||
type boundedTransfers struct {
|
||||
parts.Transfers[holder.Opening]
|
||||
}
|
||||
|
||||
func (b boundedTransfers) Get(id string) (parts.Transfer[holder.Opening], bool, error) {
|
||||
type r struct {
|
||||
t parts.Transfer[holder.Opening]
|
||||
ok bool
|
||||
}
|
||||
x, err := step(stepBound, "the read of the transfer in the transfers state", func() (r, error) {
|
||||
t, ok, err := b.Transfers.Get(id)
|
||||
return r{t, ok}, err
|
||||
})
|
||||
return x.t, x.ok, err
|
||||
}
|
||||
func (b boundedTransfers) Put(t parts.Transfer[holder.Opening]) error {
|
||||
return stepErr(stepBound, "the write of the transfer in the transfers state", func() error { return b.Transfers.Put(t) })
|
||||
}
|
||||
func (b boundedTransfers) Delete(id string) error {
|
||||
return stepErr(stepBound, "the delete of the transfer in the transfers state", func() error { return b.Transfers.Delete(id) })
|
||||
}
|
||||
|
||||
// boundedCap is the incoming cap read from its binding, bounded and logged.
|
||||
func boundedCap(c func() (uint64, error)) func() (uint64, error) {
|
||||
return func() (uint64, error) {
|
||||
return step(stepBound, "the read of incoming's cap from its binding (objects)", c)
|
||||
}
|
||||
}
|
||||
|
||||
// boundedArm is the idle key's write, bounded and logged.
|
||||
func boundedArm(a func(string, time.Duration) error) func(string, time.Duration) error {
|
||||
return func(id string, ttl time.Duration) error {
|
||||
return stepErr(stepBound, "the write of the idle key in the idle state", func() error { return a(id, ttl) })
|
||||
}
|
||||
}
|
||||
|
||||
// boundedFinding is the findings writer's say moved off the publish's path: the receiver says the incoming fill on every
|
||||
// open, and a publish never waits for it. One worker says them in order, each bounded and logged; one said while the worker
|
||||
// is full is dropped and logged (the next open says the fill again).
|
||||
func boundedFinding(f holder.Finding) holder.Finding {
|
||||
type said struct {
|
||||
kind, subject, words string
|
||||
raised bool
|
||||
}
|
||||
queue := make(chan said, 64)
|
||||
go func() {
|
||||
for s := range queue {
|
||||
_ = stepErr(stepBound, "the finding "+s.kind+" of "+s.subject, func() error { f(s.kind, s.subject, s.words, s.raised); return nil })
|
||||
}
|
||||
}()
|
||||
return func(kind, subject, words string, raised bool) {
|
||||
select {
|
||||
case queue <- said{kind, subject, words, raised}:
|
||||
default:
|
||||
logf("the finding %s of %s is not said now: the writer is behind, the next open says it again", kind, subject)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// A bus operation that never answers becomes an error naming its step, within the bound.
|
||||
func TestAHungStepAnswersWithItsName(t *testing.T) {
|
||||
block := make(chan struct{})
|
||||
defer close(block)
|
||||
_, err := step(10*time.Millisecond, "the write of the idle key", func() (int, error) { <-block; return 0, nil })
|
||||
if err == nil || !strings.Contains(err.Error(), "the write of the idle key did not answer within") {
|
||||
t.Fatalf("err = %v", err)
|
||||
}
|
||||
if v, err := step(time.Second, "x", func() (int, error) { return 7, nil }); v != 7 || err != nil {
|
||||
t.Fatalf("%d %v", v, err)
|
||||
}
|
||||
}
|
||||
|
||||
// A step given up on is said given up by name, and the log says later when the call behind it ended and how (A-443a).
|
||||
func TestAGivenUpStepSaysWhenItsCallEnded(t *testing.T) {
|
||||
var mu sync.Mutex
|
||||
var lines []string
|
||||
ended := make(chan struct{})
|
||||
w := func(f string, a ...any) {
|
||||
l := fmt.Sprintf(f, a...)
|
||||
mu.Lock()
|
||||
lines = append(lines, l)
|
||||
mu.Unlock()
|
||||
if strings.Contains(l, "the put of a chunk") && strings.Contains(l, "its call ended after") {
|
||||
close(ended)
|
||||
}
|
||||
}
|
||||
old := logTo.Swap(&w)
|
||||
defer logTo.Store(old)
|
||||
release := make(chan struct{})
|
||||
if _, err := step(10*time.Millisecond, "the put of a chunk", func() (int, error) { <-release; return 0, fmt.Errorf("bus closed") }); err == nil {
|
||||
t.Fatal("not given up")
|
||||
}
|
||||
close(release)
|
||||
<-ended
|
||||
mu.Lock()
|
||||
all := strings.Join(lines, "\n")
|
||||
mu.Unlock()
|
||||
if !strings.Contains(all, "given up") || !strings.Contains(all, "bus closed") {
|
||||
t.Fatalf("log: %s", all)
|
||||
}
|
||||
}
|
||||
|
||||
// The finding is said off the caller's path: a writer that never answers does not hold the call that says it.
|
||||
func TestAFindingIsSaidOffThePath(t *testing.T) {
|
||||
block := make(chan struct{})
|
||||
defer close(block)
|
||||
say := boundedFinding(func(string, string, string, bool) { <-block })
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
say("incoming-full", "incoming", "w", false)
|
||||
say("incoming-full", "incoming", "w", false)
|
||||
close(done)
|
||||
}()
|
||||
select {
|
||||
case <-done:
|
||||
case <-time.After(5 * time.Second):
|
||||
t.Fatal("the call was held by the finding")
|
||||
}
|
||||
}
|
||||
@@ -16,6 +16,7 @@ import (
|
||||
"os"
|
||||
"strings"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshhandactsclient"
|
||||
@@ -29,7 +30,15 @@ import (
|
||||
"distribution/internal/holder"
|
||||
)
|
||||
|
||||
func logf(format string, a ...any) { fmt.Fprintf(os.Stderr, "[distribution-tools] "+format+"\n", a...) }
|
||||
// logTo is where logf writes; a test replaces it (atomically: a step given up on logs from its own goroutine).
|
||||
var logTo atomic.Pointer[func(string, ...any)]
|
||||
|
||||
func init() {
|
||||
w := func(format string, a ...any) { fmt.Fprintf(os.Stderr, "[distribution-tools] "+format+"\n", a...) }
|
||||
logTo.Store(&w)
|
||||
}
|
||||
|
||||
func logf(format string, a ...any) { (*logTo.Load())(format, a...) }
|
||||
|
||||
// kept is one state of distribution's, through the runtime.
|
||||
type kept[T any] struct {
|
||||
@@ -249,10 +258,21 @@ func tools(v *holder.Verbs, windows kept[holder.Window]) []stdio.Tool {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
a, err := v.Publish(seat, stdio.CallerOf(args), publishOf(in))
|
||||
caller := stdio.CallerOf(args)
|
||||
start := time.Now()
|
||||
logf("%s.publish received from %s: %s %s (%s, %d parts)", seat, caller, in.Name, in.Version, in.Kind, len(in.Parts))
|
||||
a, err := v.Publish(seat, caller, publishOf(in))
|
||||
if err == nil {
|
||||
learnPublishing(windows, stdio.CallerOf(args))
|
||||
// The window's learning asks the delivery view (up to 60 s, past the runner's 30 s call bound),
|
||||
// so it never holds the answer.
|
||||
go func() {
|
||||
t := time.Now()
|
||||
logf("learning the publishing build of %s starts (mesh-delivery-view.node_haves)", caller)
|
||||
learnPublishing(windows, caller)
|
||||
logf("learning the publishing build of %s ended after %s", caller, time.Since(t).Round(time.Millisecond))
|
||||
}()
|
||||
}
|
||||
logf("%s.publish from %s answered after %s (error: %v)", seat, caller, time.Since(start).Round(time.Millisecond), err)
|
||||
return publishAnswer(a), err
|
||||
}},
|
||||
stdio.Tool{Name: seat + "." + holder.VerbPublishPart, Description: "Send one chunk (at most 1 MiB) of one part of an open transfer, from the caller that opened it.",
|
||||
@@ -468,8 +488,8 @@ func main() {
|
||||
}()
|
||||
base, _ := url.Parse("http://127.0.0.1:5001")
|
||||
store := &holder.OCI{Base: base, Upstream: &holder.Upstream{}}
|
||||
h := &holder.Holder{Objects: parts.BusObjects(holder.IncomingObjects), Builder: holder.Reserved{Ask: stdio.Ask}, Store: store, Say: say, Finding: findings.Say,
|
||||
Transfers: parts.BusTransfers[holder.Opening](holder.TransfersState), Cap: parts.BusCap(holder.IncomingObjects), Record: recordAct, Idle: parts.BusArm(holder.IdleState)}
|
||||
h := &holder.Holder{Logf: logf, Objects: boundedObjects{parts.BusObjects(holder.IncomingObjects)}, Builder: holder.Reserved{Ask: stdio.Ask}, Store: store, Say: say, Finding: boundedFinding(findings.Say),
|
||||
Transfers: boundedTransfers{parts.BusTransfers[holder.Opening](holder.TransfersState)}, Cap: boundedCap(parts.BusCap(holder.IncomingObjects)), Record: recordAct, Idle: boundedArm(parts.BusArm(holder.IdleState))}
|
||||
files := &holder.Files{Container: or(os.Getenv("MESH_REGISTRY_CONTAINER"), "mesh-registry"), Run: holder.ExecRunner, UID: os.Getuid()}
|
||||
v := &holder.Verbs{Holder: h, Catalog: store, Holds: kept[holder.Hold]{state: "holds", key: holder.Hold.Key}, Mirrors: mirrors{},
|
||||
Files: files, SweepHolder: func() (string, error) { return seatlist.HolderOf(stdio.Ask, holder.SweepSeat) }}
|
||||
|
||||
@@ -71,6 +71,7 @@ func (s *fakeStore) Put(t PublishRequest, parts map[string][]byte) (string, erro
|
||||
return t.Name + "@" + t.Digest, nil
|
||||
}
|
||||
func (s *fakeStore) Held(string, string, string) (string, bool, error) { return "", false, nil }
|
||||
func (s *fakeStore) Holds(r PublishRequest) (bool, error) { return s.has[r.Digest], nil }
|
||||
|
||||
type said struct {
|
||||
seat, event string
|
||||
@@ -231,6 +232,21 @@ func TestTheHolderOfTheTwoImageSeats(t *testing.T) {
|
||||
return PublishRequest{Transfer: id, Name: "m/program", Version: "0.1.4", Kind: KindBundle, Digest: part.Digest, Parts: []Part{part}}
|
||||
}
|
||||
|
||||
t.Run("a publish says each of its waits with its time", func(t *testing.T) {
|
||||
b := newBed(true)
|
||||
var lines []string
|
||||
b.h.Logf = func(f string, a ...any) { lines = append(lines, fmt.Sprintf(f, a...)) }
|
||||
if _, err := b.h.Publish(ArtifactSeat, builder, req("tl")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
all := strings.Join(lines, "\n")
|
||||
for _, want := range []string{"the store's read of what it holds (the program's registry) starts", "(the program's registry) ended after", "the opening of the transfer on the bus"} {
|
||||
if !strings.Contains(all, want) {
|
||||
t.Errorf("no log line %q in:\n%s", want, all)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("a transfer in parts, cut and resumed, hashed, numbered by the builder, published", func(t *testing.T) {
|
||||
b := newBed(true)
|
||||
a, err := b.h.Publish(ArtifactSeat, builder, req("t1"))
|
||||
@@ -650,30 +666,37 @@ func TestTheHolderOfTheTwoImageSeats(t *testing.T) {
|
||||
}
|
||||
}
|
||||
})
|
||||
t.Run("a late publish names a held bundle with no size: tagged at the store's size, nothing sent", func(t *testing.T) {
|
||||
t.Run("a publish with a version and no parts tags the held digest at the store's size; one not held is not stored", func(t *testing.T) {
|
||||
// Design 47 G A-417, A-73, A-50: the build stored the bundle untagged; the builder's publish of its version with that
|
||||
// digest and no parts tags it and says published, nothing sent; a digest the store holds none of is refused as not
|
||||
// stored, naming the digest.
|
||||
store := newFakeRegistry()
|
||||
sts := httptest.NewServer(store)
|
||||
defer sts.Close()
|
||||
base, _ := url.Parse(sts.URL)
|
||||
o := &OCI{Base: base}
|
||||
b := newBed(true)
|
||||
b.h.Store = &OCI{Base: base}
|
||||
blob := []byte(strings.Repeat("b", 4321))
|
||||
store.blobs["m/tools@"+digestOf(blob)] = blob
|
||||
ref, err := o.Put(PublishRequest{Name: "m/tools", Version: "0.1.4", Kind: KindBundle, Digest: digestOf(blob),
|
||||
Parts: []Part{{Digest: digestOf(blob)}}}, map[string][]byte{})
|
||||
if err != nil || ref != "m/tools@"+digestOf(blob) {
|
||||
t.Fatalf("Put = %q, %v", ref, err)
|
||||
got, err := b.h.Publish(ArtifactSeat, "x.mesh-delivery-builder", PublishRequest{Name: "m/tools", Version: "0.1.4", Kind: KindBundle, Digest: digestOf(blob),
|
||||
Transfer: "republish-m-0-1-4-tools"})
|
||||
if err != nil || !got.Ended || len(b.said) != 1 || b.said[0].event != EventPublished || b.said[0].body.Reference != SeatReference(ArtifactSeat, "m/tools@"+digestOf(blob)) {
|
||||
t.Fatalf("Publish = %+v, %v; said %+v", got, err, b.said)
|
||||
}
|
||||
var man struct {
|
||||
Layers []struct {
|
||||
Size int64 `json:"size"`
|
||||
Digest string `json:"digest"`
|
||||
Size int64 `json:"size"`
|
||||
} `json:"layers"`
|
||||
}
|
||||
if err := json.Unmarshal(store.manifests["m/tools:0.1.4"], &man); err != nil || len(man.Layers) != 1 || man.Layers[0].Size != int64(len(blob)) {
|
||||
if err := json.Unmarshal(store.manifests["m/tools:0.1.4"], &man); err != nil || len(man.Layers) != 1 || man.Layers[0].Size != int64(len(blob)) || man.Layers[0].Digest != digestOf(blob) {
|
||||
t.Errorf("the version's manifest %s, want one layer of %d bytes", store.manifests["m/tools:0.1.4"], len(blob))
|
||||
}
|
||||
if _, err := o.Put(PublishRequest{Name: "m/tools", Version: "0.1.5", Kind: KindBundle, Digest: "sha256:" + strings.Repeat("e", 64),
|
||||
Parts: []Part{{Digest: "sha256:" + strings.Repeat("e", 64)}}}, map[string][]byte{}); err == nil {
|
||||
t.Error("a digest the store does not hold was tagged")
|
||||
gone := "sha256:" + strings.Repeat("e", 64)
|
||||
_, err = b.h.Publish(ArtifactSeat, "x.mesh-delivery-builder", PublishRequest{Name: "m/tools", Version: "0.1.5", Kind: KindBundle, Digest: gone, Transfer: "republish-m-0-1-5-tools"})
|
||||
if err == nil || !strings.Contains(err.Error(), "not stored: ") || !strings.Contains(err.Error(), gone) || store.manifests["m/tools:0.1.5"] != nil ||
|
||||
len(b.said) != 2 || b.said[1].event != EventRefused || !b.findings["publish-refused m/tools@0.1.5"] {
|
||||
t.Errorf("a digest not held: %v; said %+v; findings %v", err, b.said, b.findings)
|
||||
}
|
||||
})
|
||||
t.Run("mirror answers at once and says the copy's end on the seat's events", func(t *testing.T) {
|
||||
|
||||
@@ -219,6 +219,25 @@ func (o *OCI) Held(name, version, digest string) (string, bool, error) {
|
||||
return name + "@" + d, true, nil
|
||||
}
|
||||
|
||||
// Holds is whether the store holds what r's digest names under r's name: an image's manifest by its digest, or a bundle's
|
||||
// or an archive's blob.
|
||||
func (o *OCI) Holds(r PublishRequest) (bool, error) {
|
||||
if r.Kind != KindImage && r.Kind != KindUpstream {
|
||||
return o.Has(r.Name, r.Digest)
|
||||
}
|
||||
resp, _, err := o.do(http.MethodHead, "/v2/"+r.Name+"/manifests/"+r.Digest, "", nil, accepted...)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
switch resp.StatusCode {
|
||||
case http.StatusOK:
|
||||
return true, nil
|
||||
case http.StatusNotFound:
|
||||
return false, nil
|
||||
}
|
||||
return false, fmt.Errorf("the store answered %d for the manifest %s of %s", resp.StatusCode, r.Digest, r.Name)
|
||||
}
|
||||
|
||||
// Versions are name's tags.
|
||||
func (o *OCI) Versions(name string) ([]string, error) {
|
||||
resp, b, err := o.do(http.MethodGet, "/v2/"+name+"/tags/list", "", nil)
|
||||
|
||||
@@ -143,6 +143,9 @@ type Store interface {
|
||||
// Held is the reference's path of name at version when the store already holds digest under it: a rebuild of a held
|
||||
// fingerprint answers it and stores nothing.
|
||||
Held(name, version, digest string) (reference string, ok bool, err error)
|
||||
// Holds says whether the store holds what a publish's digest names under its name: an image's manifest, or a bundle's or
|
||||
// an archive's blob. The store is the artifact store (A-50): `artifact-store://<name>/blobs/<digest>` is that blob.
|
||||
Holds(r PublishRequest) (bool, error)
|
||||
}
|
||||
|
||||
// SeatReference is a reference in the form the seat answers (novox/hq ADR 0605 §9): the seat's name, then the path the
|
||||
@@ -173,6 +176,20 @@ type Holder struct {
|
||||
// Idle writes a transfer's idle key with its own time to live, once (parts.BusArm over IdleState); the receiver
|
||||
// arms it on each open that leaves a transfer open. Nil arms nothing.
|
||||
Idle func(id string, ttl time.Duration) error
|
||||
// Logf says each wait of a publish with its time (stderr in the bundle); nil says nothing.
|
||||
Logf func(string, ...any)
|
||||
}
|
||||
|
||||
// timed runs f and logs its name and time when the holder logs.
|
||||
func (h *Holder) timed(what string, f func() error) error {
|
||||
if h.Logf == nil {
|
||||
return f()
|
||||
}
|
||||
start := time.Now()
|
||||
h.Logf("publish: %s starts", what)
|
||||
err := f()
|
||||
h.Logf("publish: %s ended after %s (%v)", what, time.Since(start).Round(time.Millisecond), err)
|
||||
return err
|
||||
}
|
||||
|
||||
func (h *Holder) now() time.Time {
|
||||
@@ -249,23 +266,83 @@ func (h *Holder) Publish(seat, caller string, r PublishRequest) (PublishAnswer,
|
||||
}
|
||||
// A rebuild of a held fingerprint answers the held version and stores nothing.
|
||||
if r.Version != "" {
|
||||
if ref, ok, err := h.Store.Held(r.Name, r.Version, r.Digest); err != nil {
|
||||
var ref string
|
||||
var ok bool
|
||||
err := h.timed("the store's read of what it holds (the program's registry)", func() (err error) {
|
||||
ref, ok, err = h.Store.Held(r.Name, r.Version, r.Digest)
|
||||
return err
|
||||
})
|
||||
if err != nil {
|
||||
return PublishAnswer{}, fmt.Errorf("publish: the store cannot say whether it holds %s@%s: %w", r.Name, r.Version, err)
|
||||
} else if ok {
|
||||
return PublishAnswer{Transfer: r.Transfer, Ended: true}, h.Say(seat, EventPublished, h.eventOf(seat, r, SeatReference(seat, ref), ""))
|
||||
return PublishAnswer{Transfer: r.Transfer, Ended: true}, h.timed("the published event on the seat", func() error {
|
||||
return h.Say(seat, EventPublished, h.eventOf(seat, r, SeatReference(seat, ref), ""))
|
||||
})
|
||||
}
|
||||
}
|
||||
if r.Version != "" && len(r.Parts) == 0 {
|
||||
return h.tagHeld(seat, r)
|
||||
}
|
||||
wants := make([]parts.Want, 0, len(r.Parts))
|
||||
for _, p := range r.Parts {
|
||||
wants = append(wants, parts.Want{Digest: p.Digest, Size: p.Size})
|
||||
}
|
||||
got, err := h.receiver().Open(r.Transfer, caller, wants, Opening{PublishRequest: r, Seat: seat})
|
||||
var got parts.Received
|
||||
err := h.timed("the opening of the transfer on the bus (objects, transfer state, idle key)", func() (err error) {
|
||||
got, err = h.receiver().Open(r.Transfer, caller, wants, Opening{PublishRequest: r, Seat: seat})
|
||||
return err
|
||||
})
|
||||
if err != nil {
|
||||
return answerOf(got), fmt.Errorf("publish: %w", err)
|
||||
}
|
||||
return answerOf(got), nil
|
||||
}
|
||||
|
||||
// tagHeld is a publish with a version, a digest and no parts (design 47 G A-417, A-73, A-50): what the store holds of that
|
||||
// digest under the name (the build's untagged publish, the artifact store's blob) is checked against the builder's reservation
|
||||
// and tagged with the version, and published is said; when the store holds none of it, the publish is refused as not stored,
|
||||
// naming the digest. Nothing travels.
|
||||
func (h *Holder) tagHeld(seat string, r PublishRequest) (PublishAnswer, error) {
|
||||
var held bool
|
||||
err := h.timed("the store's read of the digest it holds (the program's registry)", func() (err error) {
|
||||
held, err = h.Store.Holds(r)
|
||||
return err
|
||||
})
|
||||
if err != nil {
|
||||
return PublishAnswer{}, fmt.Errorf("publish: the store cannot say whether it holds %s of %s: %w", r.Digest, r.Name, err)
|
||||
}
|
||||
refuse := func(reason string) (PublishAnswer, error) {
|
||||
h.Finding("publish-refused", r.Name+"@"+r.Version, reason, true)
|
||||
if err := h.Say(seat, EventRefused, h.eventOf(seat, r, "", reason)); err != nil {
|
||||
return PublishAnswer{}, fmt.Errorf("publish: %s, and the refused is not said: %w", reason, err)
|
||||
}
|
||||
return PublishAnswer{Transfer: r.Transfer, Ended: true}, errors.New("publish: " + reason)
|
||||
}
|
||||
if !held {
|
||||
return refuse(fmt.Sprintf("not stored: the store holds no %s of %s, so version %s names nothing", r.Digest, r.Name, r.Version))
|
||||
}
|
||||
ok, why, err := h.Builder.Recorded(r.Name, r.Version, r.Digest)
|
||||
if err != nil {
|
||||
return PublishAnswer{}, fmt.Errorf("publish: the reservation could not be read, so nothing is tagged: %w", err)
|
||||
}
|
||||
if !ok {
|
||||
return refuse("the builder did not reserve it: " + why)
|
||||
}
|
||||
tag := r
|
||||
tag.Parts = []Part{{Digest: r.Digest}}
|
||||
var path string
|
||||
if err := h.timed("the store's tag of the held digest (the program's registry)", func() (err error) {
|
||||
path, err = h.Store.Put(tag, map[string][]byte{})
|
||||
return err
|
||||
}); err != nil {
|
||||
return refuse("the store did not tag it: " + err.Error())
|
||||
}
|
||||
h.Finding("publish-refused", r.Name+"@"+r.Version, "", false)
|
||||
return PublishAnswer{Transfer: r.Transfer, Ended: true}, h.timed("the published event on the seat", func() error {
|
||||
return h.Say(seat, EventPublished, h.eventOf(seat, r, SeatReference(seat, path), ""))
|
||||
})
|
||||
}
|
||||
|
||||
// PublishPart is `publish-part`: one chunk, from the caller that opened the transfer alone.
|
||||
func (h *Holder) PublishPart(seat, caller string, r PartRequest) (PublishAnswer, error) {
|
||||
got, err := h.receiver().Take(r.Transfer, caller, r.Digest, r.Sequence, r.Last, r.Data, func(t Transfer) error {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#!/bin/sh
|
||||
# The wallpaper (module feh, novox/hq ADR 0208). Owned by the mesh: replaced at every push. The
|
||||
# The wallpaper (module feh, novox/hq ADR 0208). Owned by the mesh: replaced at every delivery. The
|
||||
# session's start runs it, and so may anything that wants the declared wallpaper back. The image is
|
||||
# the module's own, in ~/.local/share/feh/wallpapers. feh_set changes the wallpaper for a session
|
||||
# without touching this file.
|
||||
# without touching this file (the second single-module proof, 2026-11-17).
|
||||
feh --no-fehbg --bg-fill "$HOME/.local/share/feh/wallpapers/default.jpg"
|
||||
|
||||
@@ -152,7 +152,10 @@ export function seatTools(gitea: GiteaClient, deps: SeatDeps): ToolDefinition[]
|
||||
verb(GIT_VERBS.branch, "The seat git's branch: a branch's head, and whether a protection rule refuses a direct push and admits only merges.",
|
||||
async (args): Promise<BranchAnswer> => {
|
||||
const { owner, repo, full } = repositoryOf(args.repository);
|
||||
const branch = String(args.branch ?? "");
|
||||
// No branch named is the repository's default branch, read from the forge, as the verb file reads it (A-399).
|
||||
let branch = String(args.branch ?? "");
|
||||
if (branch === "") branch = String((await gitea.getRepo(owner, repo)).default_branch ?? "");
|
||||
if (branch === "") throw new Error(`the forge names no default branch of ${full}`);
|
||||
// The head first: a branch the forge does not hold is its 404, the kind not-found, never an unprotected branch.
|
||||
const head = await gitea.branchHead(owner, repo, branch);
|
||||
return { repository: full, branch, head, protected: pushRefused(await gitea.appliedProtection(owner, repo, branch)) };
|
||||
|
||||
@@ -58,6 +58,7 @@ func main() {
|
||||
// compiles that program and runs it shut off, answers its mark judged by the candidate at the tree's commit, and the
|
||||
// program reached no key, no login, no name and no socket of the machine.
|
||||
func TestTheUnitLevelUnderTheWorkspaceRunnerRunsTheTreesProgramShutOff(t *testing.T) {
|
||||
t.Parallel()
|
||||
needTools(t, "the candidate's program shut off", "go", "git", "bwrap")
|
||||
home := t.TempDir()
|
||||
key, login := filepath.Join(home, ".ssh", "mesh_ed25519"), filepath.Join(home, "broker")
|
||||
|
||||
@@ -195,6 +195,7 @@ func TestACandidateThatDoesNotCompileFailsTheStepWithTheCompilersFirstErrorAndRu
|
||||
}
|
||||
|
||||
func TestTheInstalledProgramSaysItJudged(t *testing.T) { // 1898 K1, ADR 0535 §5
|
||||
t.Parallel()
|
||||
m, states, ws, _ := workspaceFor(t, "slow")
|
||||
for _, runner := range []any{nil, RunnerInstalled} {
|
||||
args := map[string]any{"workspace": ws, "level": LevelUnit}
|
||||
@@ -223,6 +224,7 @@ func TestAnUnknownRunnerIsRefused(t *testing.T) { // 1898 K1
|
||||
}
|
||||
|
||||
func TestTheShutOffProcessRunsTheStepItIsAsked(t *testing.T) { // 1898 K2: the candidate's side of the spec
|
||||
t.Parallel()
|
||||
m, _, ws, calls := workspaceFor(t, "slow")
|
||||
m.Inside = true
|
||||
a := m.answer(context.Background(), levelSpec{Step: stepUnit, Workspace: ws, Packages: []string{"./..."}, Bound: UnitBound})
|
||||
|
||||
@@ -24,70 +24,7 @@ import (
|
||||
"git.novox.be/novox/mesh-sdk/go/testbus"
|
||||
)
|
||||
|
||||
// nodeEngineTree is the node-engine's repository as the checker places it beside this check (MESH_CHECK_BESIDE, the
|
||||
// repository mesh-host), and with it every Go module of the other repositories placed there (mesh-sdk, mesh-contracts,
|
||||
// mesh-delivery, mesh-networking, the catalogue's trunk) and of the catalogue under check, first, as candidates: the workspace lays those the
|
||||
// node-engine's composer requires at the zero version, transitively (workspaceOf), so they resolve as the group lays
|
||||
// them, wherever the composer lives. The test
|
||||
// builds the node-engine's real composer command from that tree; it does not stand one in. With no beside directory it
|
||||
// fails, and with the node-engine beside but no composer command in it it fails: a composer test that could not build its
|
||||
// composer proves nothing. With a beside directory that holds no node-engine at all it skips aloud (NOT JUDGED), since a
|
||||
// checker from before this release places none for the catalogue.
|
||||
func nodeEngineTree(t *testing.T) (tree string, modules []string) {
|
||||
t.Helper()
|
||||
beside := os.Getenv("MESH_CHECK_BESIDE")
|
||||
if beside == "" {
|
||||
t.Fatal("NOT CHECKED: MESH_CHECK_BESIDE is not set, so the node-engine's repository (mesh-host) is not beside this check")
|
||||
}
|
||||
tree = filepath.Join(beside, "mesh-host")
|
||||
if !dirExists(tree) {
|
||||
// A checker from before novox/hq ADR 0593 places only mesh-sdk beside a catalogue check, so the node-engine's
|
||||
// repository is absent, not wrong: said aloud, since nothing here could build a composer to judge.
|
||||
t.Skipf("NOT JUDGED: the node-engine's repository (mesh-host) is not beside this check in MESH_CHECK_BESIDE=%s; the running checker places none for the catalogue, so the composer is judged only once it does", beside)
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(tree, "cmd", "mesh-compose")); err != nil {
|
||||
t.Fatalf("NOT CHECKED: the node-engine's composer command is not beside this check in MESH_CHECK_BESIDE=%s: %v", beside, err)
|
||||
}
|
||||
entries, err := os.ReadDir(beside)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var roots []string
|
||||
for _, e := range entries {
|
||||
if e.Name() == "mesh-host" || !dirExists(filepath.Join(beside, e.Name())) {
|
||||
continue
|
||||
}
|
||||
root, err := filepath.EvalSymlinks(filepath.Join(beside, e.Name()))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
roots = append(roots, root)
|
||||
}
|
||||
// The catalogue under check, first: beside lies its trunk (issue 1295), whose modules a head's must win over.
|
||||
catalogue, err := filepath.Abs(filepath.Join("..", "..", "..", ".."))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, root := range append([]string{catalogue}, roots...) {
|
||||
filepath.WalkDir(root, func(p string, d fs.DirEntry, err error) error {
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
if d.IsDir() && (d.Name() == ".git" || d.Name() == "testdata" || d.Name() == "node_modules") {
|
||||
return filepath.SkipDir
|
||||
}
|
||||
if !d.IsDir() && d.Name() == "go.mod" {
|
||||
modules = append(modules, filepath.Dir(p))
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
return tree, modules
|
||||
}
|
||||
|
||||
func dirExists(p string) bool { st, err := os.Stat(p); return err == nil && st.IsDir() }
|
||||
|
||||
// copyTree copies the files of a source tree into dst, leaving out its .git.
|
||||
// copyHostTree copies the files of a source tree into dst, leaving out its .git.
|
||||
func copyHostTree(t *testing.T, src, dst string) {
|
||||
t.Helper()
|
||||
src, err := filepath.EvalSymlinks(src)
|
||||
@@ -130,7 +67,7 @@ func goEnvOf(t *testing.T, name string) string {
|
||||
}
|
||||
|
||||
// 1622/AC1, AC3 against the real bus (ADR 0593 §7, C9): a compose ask put on the Go build seat's check stream is pulled by
|
||||
// the module started whole on that bus, which fetches the commit (the node-engine's repository beside the check, its Go module at the root), builds the composer command the ask names there with nothing fetched, fills the
|
||||
// the module started whole on that bus, which fetches the commit (a stand-in composer of this module's testdata, its Go module at the root), builds the composer command the ask names there with nothing fetched, fills the
|
||||
// request's manifests from the indexer's state and runs the composer; the step key <node>.<ask id> passes with the
|
||||
// composer's verdict. A second ask of the same commit runs the kept composer: fetched and built once.
|
||||
func TestAComposeAskIsBuiltOnceAndAnsweredOnTheBus(t *testing.T) {
|
||||
@@ -157,14 +94,11 @@ func TestAComposeAskIsBuiltOnceAndAnsweredOnTheBus(t *testing.T) {
|
||||
m.Run = StepRunner(t.TempDir())
|
||||
m.GoModCache = goEnvOf(t, "GOMODCACHE") // the module cache this module's own build filled, read and not fetched
|
||||
run, gocache := m.Run, goEnvOf(t, "GOCACHE") // this machine's build cache, so the composer's packages compile once
|
||||
hostTree, beside := nodeEngineTree(t)
|
||||
workFile := ""
|
||||
// The composer is a stand-in of this module's own testdata (design 47 G A-474: a module's tests read nothing outside it, and a
|
||||
// pipeline's step places no other repository beside the tree): a program of the composer's contract that imports nothing.
|
||||
hostTree := filepath.Join("testdata", "composer")
|
||||
m.Run = func(ctx context.Context, env []string, name string, args ...string) forgekey.Ran {
|
||||
env = append(env, "GOCACHE="+gocache)
|
||||
if workFile != "" {
|
||||
env = append(env, "GOWORK="+workFile, "GOFLAGS=-mod=readonly") // a workspace takes no -mod=mod
|
||||
}
|
||||
return run(ctx, env, name, args...)
|
||||
return run(ctx, append(env, "GOCACHE="+gocache), name, args...)
|
||||
}
|
||||
read, digests := catalogHolds(t, `{"module":"demo","version":"1"}`)
|
||||
m.ReadEntry = read
|
||||
@@ -181,18 +115,6 @@ func TestAComposeAskIsBuiltOnceAndAnsweredOnTheBus(t *testing.T) {
|
||||
return nil, err
|
||||
}
|
||||
copyHostTree(t, hostTree, tree)
|
||||
if len(beside) > 0 { // the group's other repositories, laid beside the node-engine's tree as one workspace
|
||||
work, err := workspaceOf(tree, beside)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(m.Work, ws, "go.work"), []byte(work), 0o600); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
workFile = filepath.Join(m.Work, ws, "go.work")
|
||||
// Nothing is downloaded here: this level has no network. The check's own fetch downloaded this workspace's
|
||||
// graph (downloadBeside), and a module it lacks fails the build naming it.
|
||||
}
|
||||
return map[string]any{"workspace": ws}, nil
|
||||
}
|
||||
}
|
||||
@@ -203,7 +125,7 @@ func TestAComposeAskIsBuiltOnceAndAnsweredOnTheBus(t *testing.T) {
|
||||
|
||||
ask := func() sdkstep.Step {
|
||||
a := composeAskOf(t, digests[0])
|
||||
a.Compose.Fetch.Repository, a.Compose.Dir, a.Compose.Package = "novox/mesh-host", catalogclient.RootPath, "github.com/novox/mesh-host/cmd/mesh-compose"
|
||||
a.Compose.Fetch.Repository, a.Compose.Dir, a.Compose.Package = "novox/mesh-catalog", catalogclient.RootPath, "standin/cmd/mesh-compose"
|
||||
raw, err := sdkstep.EncodeStepAsk(a)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
|
||||
@@ -260,7 +260,7 @@ func (m *Module) digestHere(ctx context.Context, workspace, level string, names
|
||||
namesHeader = []string{"node-names " + strings.Join(sortedNames, ",")}
|
||||
}
|
||||
|
||||
dirs, err := m.goModDirs(ctx, tree)
|
||||
dirs, err := m.testDirs(ctx, tree)
|
||||
if err != nil {
|
||||
return DigestResult{}, err
|
||||
}
|
||||
|
||||
@@ -273,6 +273,7 @@ func TestAPackageWithTheReadsOutsideMarkerIsKeptOnlyByWhatItLists(t *testing.T)
|
||||
}
|
||||
edit(t, tree, "docs/one.md", "one\n")
|
||||
edit(t, tree, "other.md", "other\n")
|
||||
// Each marker is judged in a fixture of its own, side by side: the digests are go commands, and the table is the test's longest part.
|
||||
for name, marker := range map[string]string{
|
||||
"an empty marker": "# nothing listed\n\n",
|
||||
"a sentence": "the tests read ../docs\n",
|
||||
@@ -282,19 +283,44 @@ func TestAPackageWithTheReadsOutsideMarkerIsKeptOnlyByWhatItLists(t *testing.T)
|
||||
"a path that is not clean": "docs/./one.md\n",
|
||||
"a good line and a bad line": "docs\nnope/*.md\n",
|
||||
} {
|
||||
edit(t, tree, "c/"+ReadsOutsideMarker, marker)
|
||||
first, second := digests(t, m, "unit"), digests(t, m, "unit")
|
||||
if len(first.Packages[c]) != 64 || first.Packages[c] == second.Packages[c] || first.Packages[c] == plain.Packages[c] {
|
||||
t.Errorf("%s: c's digest is stable: %s, %s, before %s", name, first.Packages[c], second.Packages[c], plain.Packages[c])
|
||||
}
|
||||
if strings.Join(first.ReadsOutside, ",") != c {
|
||||
t.Errorf("%s: the answer does not name the package that reads outside: %v", name, first.ReadsOutside)
|
||||
}
|
||||
if first.Packages[a] != plain.Packages[a] || first.Packages[a] != second.Packages[a] {
|
||||
t.Errorf("%s: a has no marker and its digest moved: %s, %s, before %s", name, first.Packages[a], second.Packages[a], plain.Packages[a])
|
||||
}
|
||||
t.Run(name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
m, tree := digestFixture(t)
|
||||
plain := digests(t, m, "unit")
|
||||
if err := os.MkdirAll(filepath.Join(tree, "docs"), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
edit(t, tree, "docs/one.md", "one\n")
|
||||
edit(t, tree, "other.md", "other\n")
|
||||
edit(t, tree, "c/"+ReadsOutsideMarker, marker)
|
||||
first, second := digests(t, m, "unit"), digests(t, m, "unit")
|
||||
if len(first.Packages[c]) != 64 || first.Packages[c] == second.Packages[c] || first.Packages[c] == plain.Packages[c] {
|
||||
t.Errorf("%s: c's digest is stable: %s, %s, before %s", name, first.Packages[c], second.Packages[c], plain.Packages[c])
|
||||
}
|
||||
if strings.Join(first.ReadsOutside, ",") != c {
|
||||
t.Errorf("%s: the answer does not name the package that reads outside: %v", name, first.ReadsOutside)
|
||||
}
|
||||
if first.Packages[a] != plain.Packages[a] || first.Packages[a] != second.Packages[a] {
|
||||
t.Errorf("%s: a has no marker and its digest moved: %s, %s, before %s", name, first.Packages[a], second.Packages[a], plain.Packages[a])
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// The marker that lists what the package's tests read, and the marker of what the package needs (issue 890/AC3): in a fixture of its
|
||||
// own, so it runs beside the table above.
|
||||
func TestAListedReadsOutsideMarkerKeepsTheDigestOfOnlyWhatItLists(t *testing.T) {
|
||||
t.Parallel()
|
||||
m, tree := digestFixture(t)
|
||||
plain := digests(t, m, "unit")
|
||||
c, a := "fixture/digest/c", "fixture/digest/a"
|
||||
if err := os.MkdirAll(filepath.Join(tree, "docs"), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
edit(t, tree, "docs/one.md", "one\n")
|
||||
edit(t, tree, "other.md", "other\n")
|
||||
|
||||
// A list: a glob, a directory read whole, and a path whose absence is read.
|
||||
edit(t, tree, "c/"+ReadsOutsideMarker, "# what c's tests read\ndocs/*.md\ndocs\n?gone\n")
|
||||
before := digests(t, m, "unit")
|
||||
|
||||
@@ -34,7 +34,7 @@ func TestTheGuardSaysToolchainReportedOnAChangeWithTheProgramDigest(t *testing.T
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(got) != 1 || got[0].subject != "mesh.seat.node-build-go.event.toolchain-reported.ace" || got[0].body["node"] != "ace" || got[0].body["language"] != "go" ||
|
||||
got[0].body["version"] != "go1.27.1" || got[0].body["recipe"] != m.Recipe.Version() || len(got[0].body["digest"]) != len("sha256:")+64 {
|
||||
got[0].body["version"] != "go1.27" || got[0].body["recipe"] != m.Recipe.Version() || len(got[0].body["digest"]) != len("sha256:")+64 {
|
||||
t.Fatalf("events = %+v, want one toolchain-reported for ace, go with the program's digest", got)
|
||||
}
|
||||
if tc, _ := states.toolchain("ace.go"); tc.Digest != got[0].body["digest"] {
|
||||
@@ -51,7 +51,7 @@ func TestTheGuardSaysToolchainReportedOnAChangeWithTheProgramDigest(t *testing.T
|
||||
os.WriteFile(prog, []byte("another go program"), 0o755)
|
||||
m.Probe = func(context.Context) (string, error) { return "go1.27.2", nil }
|
||||
m.Check(context.Background())
|
||||
if len(got) != 2 || got[1].body["version"] != "go1.27.2" || got[1].body["digest"] == got[0].body["digest"] {
|
||||
if len(got) != 2 || got[1].body["version"] != "go1.27" || got[1].body["digest"] == got[0].body["digest"] {
|
||||
t.Errorf("a new version said %+v, want a second event with another digest", got)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,6 +20,8 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"golang.org/x/mod/modfile"
|
||||
|
||||
sdkstep "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildstep"
|
||||
dforgekey "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/forgekey"
|
||||
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/steprun"
|
||||
@@ -106,6 +108,8 @@ func (m *Module) fetch(ctx context.Context, args map[string]any) (res any, err e
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// A fetch with no dir (the checker's check of a pull request, until G removes the checker; A-447e) fetches the tree
|
||||
// whole: every go.mod of it.
|
||||
siblings, err := siblingsArg(args["beside"])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -165,6 +169,20 @@ func (m *Module) fetchInto(ctx context.Context, req sdkstep.FetchRequest, id str
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if dir := cleanDir(req.Dir); dir != "" {
|
||||
// A fetch asked with a module's directory is of that Go module and what its go.mod replaces with a local path,
|
||||
// never another Go module of the tree (design 47 G, A-447). Said beside the tree, so every later step lists the
|
||||
// same modules.
|
||||
if dirs, err = scopedDirs(tree, dir, dirs); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(root, scopeMark), []byte(strings.Join(dirs, "\n")), 0o600); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(root, moduleDirMark), []byte(dir), 0o600); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
res.Modules = dirs
|
||||
if base != "" && res.Merge == "" {
|
||||
return res, nil // a conflict: the tree is at the trunk, nothing of the head is built or tested
|
||||
@@ -313,7 +331,13 @@ func (m *Module) shutOffLoad(ctx context.Context, env []string, dir, d string, j
|
||||
if _, err := os.Stat(filepath.Join(dir, "vendor", "modules.txt")); err == nil {
|
||||
return nil // a directory that vendors is read from its vendor directory, never through the module cache
|
||||
}
|
||||
missing, err := ownFilesCover(dir, loaded)
|
||||
// The files the steps build with: a directory with a temporary modfile (an asked version, a pin) is built with it and
|
||||
// the sums beside it, which hold the requirements of the versions taken (takeRequirementsOf).
|
||||
files := dir
|
||||
if modfile, ok := m.pinModfile(dir); ok {
|
||||
files = filepath.Dir(modfile)
|
||||
}
|
||||
missing, err := ownFilesCover(files, loaded)
|
||||
if err != nil {
|
||||
return fmt.Errorf("the go.mod and go.sum of %s: %w", d, err)
|
||||
}
|
||||
@@ -324,7 +348,7 @@ func (m *Module) shutOffLoad(ctx context.Context, env []string, dir, d string, j
|
||||
return nil
|
||||
}
|
||||
|
||||
// ownFilesCover is what the go.mod and go.sum in dir lack for the modules its packages load, each as the line it lacks:
|
||||
// ownFilesCover is what the go.mod and go.sum in dir (a directory's own, or those of its temporary modfile) lack for the modules its packages load, each as the line it lacks:
|
||||
// a mesh module (of the forge) required, the line it lacks naming the zero version (ADR 0573 §2; one named at a commit
|
||||
// is built against the asked version all the same, §4, and needs no lookup), every other required at the version the
|
||||
// load selected, with its two go.sum lines (the module's and its go.mod's) for the version Go reads it at. A module
|
||||
@@ -434,7 +458,11 @@ func (m *Module) downloadAndVerify(ctx context.Context, dir, d string) error {
|
||||
}
|
||||
|
||||
// goModDirs are the directories of the tree that hold a go.mod, relative to it, in order.
|
||||
// When the fetch was asked with a module's directory (scopeMark), the steps after it see only the directories it chose.
|
||||
func (m *Module) goModDirs(ctx context.Context, tree string) ([]string, error) {
|
||||
if b, err := os.ReadFile(filepath.Join(filepath.Dir(tree), scopeMark)); err == nil {
|
||||
return strings.Fields(string(b)), nil
|
||||
}
|
||||
r := m.Run(ctx, nil, "git", "-C", tree, "ls-files", "--", "go.mod", "*/go.mod")
|
||||
if r.Status != 0 || r.Err != "" {
|
||||
return nil, fmt.Errorf("git ls-files: %s", dforgekey.Said(r.Err+"\n"+r.Stderr))
|
||||
@@ -454,6 +482,27 @@ func (m *Module) goModDirs(ctx context.Context, tree string) ([]string, error) {
|
||||
return dirs, nil
|
||||
}
|
||||
|
||||
// testDirs are the directories the unit and integration steps and the digest take: the chosen go.mod directories at or
|
||||
// under the module's own directory. A directory the module's go.mod replaces from outside its own stays for download and
|
||||
// resolution alone; its tests are another module's (design 47 G, A-447c). A workspace fetched with no dir keeps every go.mod.
|
||||
func (m *Module) testDirs(ctx context.Context, tree string) ([]string, error) {
|
||||
all, err := m.goModDirs(ctx, tree)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
own, ok := moduleDirOf(tree)
|
||||
if !ok || own == "." || own == "" {
|
||||
return all, nil
|
||||
}
|
||||
out := []string{}
|
||||
for _, d := range all {
|
||||
if d == own || strings.HasPrefix(d, own+"/") {
|
||||
out = append(out, d)
|
||||
}
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// inTestdata says a path lies under a directory named testdata, whose go.mod files are fixtures Go ignores, not modules
|
||||
// of the tree (issue 1810).
|
||||
func inTestdata(path string) bool {
|
||||
@@ -608,3 +657,85 @@ func narrowed(tree string) bool {
|
||||
_, err := os.Stat(filepath.Join(filepath.Dir(tree), narrowedMark))
|
||||
return err == nil
|
||||
}
|
||||
|
||||
// scopeMark is the file beside a workspace's tree naming the Go module directories a fetch asked with a module's
|
||||
// directory chose, one to a line (empty: none).
|
||||
const scopeMark = "gomodules"
|
||||
|
||||
// moduleDirMark is the file beside a workspace's tree naming the module directory the fetch was asked with: the one
|
||||
// module.json a step compares generated files for (design 47 G, A-447c).
|
||||
const moduleDirMark = "moduledir"
|
||||
|
||||
// moduleDirOf is the module directory the workspace of tree was fetched for, ok false for a workspace with none.
|
||||
func moduleDirOf(tree string) (string, bool) {
|
||||
b, err := os.ReadFile(filepath.Join(filepath.Dir(tree), moduleDirMark))
|
||||
if err != nil {
|
||||
return "", false
|
||||
}
|
||||
return string(b), true
|
||||
}
|
||||
|
||||
// cleanDir is a request's module directory in the form goModDirs lists, "" for none.
|
||||
func cleanDir(dir string) string {
|
||||
if strings.TrimSpace(dir) == "" {
|
||||
return ""
|
||||
}
|
||||
return strings.Trim(filepath.ToSlash(filepath.Clean(dir)), "/")
|
||||
}
|
||||
|
||||
// scopedDirs are the go.mod directories of all that a module directory dir chooses: dir itself and each directory its
|
||||
// go.mod replaces with a local path, followed through their own replaces (design 47 G, A-447), in order. A directory
|
||||
// the tree holds no go.mod for is left out. A directory holding no go.mod chooses every go.mod under it, and one that
|
||||
// chooses no Go module at all is an error naming it (A-447a).
|
||||
func scopedDirs(tree, dir string, all []string) ([]string, error) {
|
||||
have := map[string]bool{}
|
||||
for _, d := range all {
|
||||
have[d] = true
|
||||
}
|
||||
chosen := map[string]bool{}
|
||||
todo := []string{dir}
|
||||
if !have[dir] {
|
||||
// A directory with no go.mod of its own (a module whose Go code lies in go/, a contracts module with a go.mod per
|
||||
// seat) is every go.mod under it; the tree is already narrowed to the module's paths (A-447a).
|
||||
todo = nil
|
||||
for _, d := range all {
|
||||
if dir == "." || strings.HasPrefix(d, dir+"/") {
|
||||
todo = append(todo, d)
|
||||
}
|
||||
}
|
||||
}
|
||||
for len(todo) > 0 {
|
||||
d := todo[0]
|
||||
todo = todo[1:]
|
||||
if chosen[d] || !have[d] {
|
||||
continue
|
||||
}
|
||||
chosen[d] = true
|
||||
file := filepath.Join(tree, filepath.FromSlash(d), "go.mod")
|
||||
body, err := os.ReadFile(file)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
f, err := modfile.Parse(file, body, nil) // not ParseLax: that one ignores the replaces
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%s: %w", file, err)
|
||||
}
|
||||
for _, r := range f.Replace {
|
||||
if !modfile.IsDirectoryPath(r.New.Path) {
|
||||
continue
|
||||
}
|
||||
target := filepath.ToSlash(filepath.Clean(filepath.Join(filepath.FromSlash(d), filepath.FromSlash(r.New.Path))))
|
||||
todo = append(todo, target)
|
||||
}
|
||||
}
|
||||
out := []string{}
|
||||
for _, d := range all {
|
||||
if chosen[d] {
|
||||
out = append(out, d)
|
||||
}
|
||||
}
|
||||
if len(out) == 0 {
|
||||
return nil, fmt.Errorf("the module directory %q holds no Go module (no go.mod in it or under it)", dir)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
@@ -61,6 +61,13 @@ func (f *fakeStep) run(ctx context.Context, env []string, name string, args ...s
|
||||
os.MkdirAll(filepath.Dir(filepath.Join(args[i+1], name)), 0o755)
|
||||
os.WriteFile(filepath.Join(args[i+1], name), []byte(text), 0o644)
|
||||
}
|
||||
// Every go.mod the fake lists exists, since a fetch asked with a directory reads its go.mod (A-447).
|
||||
for _, l := range strings.Fields(f.gomods) {
|
||||
if _, err := os.Stat(filepath.Join(args[i+1], l)); err != nil && f.files[l] == "" {
|
||||
os.MkdirAll(filepath.Dir(filepath.Join(args[i+1], l)), 0o755)
|
||||
os.WriteFile(filepath.Join(args[i+1], l), []byte("module fake\n"), 0o644)
|
||||
}
|
||||
}
|
||||
for _, v := range f.vendors {
|
||||
os.MkdirAll(filepath.Join(args[i+1], v, "vendor"), 0o755)
|
||||
os.WriteFile(filepath.Join(args[i+1], v, "vendor", "modules.txt"), nil, 0o644)
|
||||
@@ -91,7 +98,24 @@ func fetchModule(t *testing.T, f *fakeStep) (*Module, *keptStates) {
|
||||
return m, states
|
||||
}
|
||||
|
||||
// fetchTool is the fetch tool as a build module is asked, always with its module's directory (A-447b): the root of the
|
||||
// tree unless the args name one. rawFetchTool is the tool as it is.
|
||||
func fetchTool(t *testing.T, m *Module) func(map[string]any) (any, error) {
|
||||
t.Helper()
|
||||
raw := rawFetchTool(t, m)
|
||||
return func(args map[string]any) (any, error) {
|
||||
if _, ok := args["dir"]; !ok {
|
||||
named := map[string]any{"dir": "."}
|
||||
for k, v := range args {
|
||||
named[k] = v
|
||||
}
|
||||
args = named
|
||||
}
|
||||
return raw(args)
|
||||
}
|
||||
}
|
||||
|
||||
func rawFetchTool(t *testing.T, m *Module) func(map[string]any) (any, error) {
|
||||
t.Helper()
|
||||
for _, tool := range m.Tools(m.StepTools()) {
|
||||
if tool.Name == "build_go_fetch" {
|
||||
@@ -107,7 +131,9 @@ var fetchArgs = map[string]any{"repository": "novox/mesh-sdk", "commit": commitS
|
||||
// 1485/AC1: git is asked through FetchCommit with the ssh remote, and go mod download and verify run in each go.mod
|
||||
// directory (not in one that vendors) with GOTOOLCHAIN=local, GOFLAGS=-mod=mod and the module's cache paths.
|
||||
func TestFetchAsksGitForTheSshRemoteAndDownloadsEachModule(t *testing.T) {
|
||||
f := &fakeStep{gomods: "go.mod\nsub/dir/go.mod\nvend/go.mod\n", vendors: []string{"vend"}}
|
||||
// The root go.mod replaces the other two by directory: a fetch asked with a module's directory chooses what it replaces.
|
||||
f := &fakeStep{gomods: "go.mod\nsub/dir/go.mod\nvend/go.mod\n", vendors: []string{"vend"},
|
||||
files: map[string]string{"go.mod": "module fake\nreplace a => ./sub/dir\nreplace b => ./vend\n"}}
|
||||
m, states := fetchModule(t, f)
|
||||
if _, err := fetchTool(t, m)(fetchArgs); err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -270,7 +296,8 @@ func TestAVerifyThatFailsEndsTheStepNamingTheDirectoryAndTheLine(t *testing.T) {
|
||||
return forgekey.Ran{}
|
||||
}}
|
||||
m, states := fetchModule(t, f)
|
||||
if _, err := fetchTool(t, m)(fetchArgs); err != nil {
|
||||
// Asked for the module of tools, as a build is for one module (A-447b).
|
||||
if _, err := fetchTool(t, m)(map[string]any{"repository": "novox/mesh-sdk", "commit": commitSHA, "dir": "tools"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
st := states.waitEnd(t)
|
||||
|
||||
@@ -2,9 +2,13 @@ package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
sdkstep "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildstep"
|
||||
"git.novox.be/novox/mesh-sdk/go/forgekey"
|
||||
)
|
||||
|
||||
@@ -25,3 +29,123 @@ func TestGoModDirsLeaveOutTheFixturesUnderTestdata(t *testing.T) {
|
||||
t.Fatalf("go.mod directories = %v, want %v", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
// A-447: a fetch asked with a module's directory chooses that Go module and the directories its go.mod replaces with a
|
||||
// local path, through their own replaces; the later steps list the same. The takein module of the indexer is no module
|
||||
// of the assigner's build (live, 2026-11-16).
|
||||
func TestAFetchAskedWithADirectoryChoosesItsModuleAndWhatItReplacesAlone(t *testing.T) {
|
||||
all := []string{".", "modules/assigner", "modules/indexer", "modules/indexer/takein", "modules/lib", "modules/lib/deep", "modules/other", "rootless/x/go", "seats/a", "seats/b"}
|
||||
mods := map[string]string{
|
||||
"modules/assigner": "module a\nreplace x => ../lib\nreplace (\n\ty => ../../outside\n\tz v1.0.0 => z v1.1.0\n)\n",
|
||||
"modules/indexer": "module i\nreplace t => ./takein\n",
|
||||
"modules/indexer/takein": "module t\n",
|
||||
"modules/lib": "module l\nreplace d => ./deep\n",
|
||||
"modules/lib/deep": "module d\n",
|
||||
"modules/other": "module o\n",
|
||||
".": "module r\n",
|
||||
"seats/a": "module sa\nreplace q => ../q\n",
|
||||
"seats/b": "module sb\n",
|
||||
"rootless/x/go": "module rx\n",
|
||||
}
|
||||
for _, c := range []struct {
|
||||
dir string
|
||||
want []string
|
||||
}{
|
||||
{"modules/assigner", []string{"modules/assigner", "modules/lib", "modules/lib/deep"}},
|
||||
{"modules/indexer", []string{"modules/indexer", "modules/indexer/takein"}},
|
||||
{"modules/other/", []string{"modules/other"}},
|
||||
{"modules/lib/deep/", []string{"modules/lib/deep"}},
|
||||
{"seats", []string{"seats/a", "seats/b"}}, // a go.mod per seat, none in seats itself
|
||||
{".", []string{"."}}, // the root holds a go.mod
|
||||
{"rootless/x", []string{"rootless/x/go"}}, // Go code lies in go/
|
||||
{"modules/none", nil}, // chooses no Go module: an error naming it
|
||||
} {
|
||||
tree := t.TempDir()
|
||||
for d, body := range mods {
|
||||
if err := os.MkdirAll(filepath.Join(tree, d), 0o700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(tree, d, "go.mod"), []byte(body), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
got, err := scopedDirs(tree, cleanDir(c.dir), all)
|
||||
if c.want == nil {
|
||||
if err == nil || !strings.Contains(err.Error(), c.dir) {
|
||||
t.Errorf("dir %q chose %v, %v; want an error naming it", c.dir, got, err)
|
||||
}
|
||||
continue
|
||||
}
|
||||
if err != nil || !reflect.DeepEqual(got, c.want) {
|
||||
t.Errorf("dir %q chooses %v, %v; want %v", c.dir, got, err, c.want)
|
||||
}
|
||||
}
|
||||
// What the steps after the fetch list is what the fetch chose, beside the tree.
|
||||
root := t.TempDir()
|
||||
if err := os.WriteFile(filepath.Join(root, scopeMark), []byte("modules/assigner\nmodules/lib"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
m := &Module{}
|
||||
got, err := m.goModDirs(context.Background(), filepath.Join(root, "tree"))
|
||||
if err != nil || !reflect.DeepEqual(got, []string{"modules/assigner", "modules/lib"}) {
|
||||
t.Errorf("the later steps list %v, %v", got, err)
|
||||
}
|
||||
if cleanDir("") != "" || cleanDir(".") != "." {
|
||||
t.Errorf("cleanDir of nothing = %q, of . = %q", cleanDir(""), cleanDir("."))
|
||||
}
|
||||
}
|
||||
|
||||
// A-447e: a fetch with no dir (the checker's check of a pull request, until G removes the checker) fetches the tree whole:
|
||||
// it chooses every go.mod of it.
|
||||
func TestAFetchWithNoDirectoryChoosesEveryGoModOfTheTree(t *testing.T) {
|
||||
f := &fakeStep{gomods: "go.mod\nsub/go.mod\n"}
|
||||
m, states := fetchModule(t, f)
|
||||
if _, err := rawFetchTool(t, m)(map[string]any{"repository": "novox/mesh-sdk", "commit": commitSHA}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
st := states.waitEnd(t)
|
||||
if st.State != sdkstep.StepPassed || !strings.Contains(string(st.Result), `"modules":[".","sub"]`) {
|
||||
t.Fatalf("a fetch with no dir ended %s: %q %s", st.State, st.Reason, st.Result)
|
||||
}
|
||||
}
|
||||
|
||||
// A-447c: the unit level of a module whose go.mod replaces ../lib tests the module alone; lib is downloaded and resolved
|
||||
// for it, and its tests are another module's.
|
||||
func TestTheUnitLevelTestsOnlyTheModulesOwnDirectoriesNotWhatItReplaces(t *testing.T) {
|
||||
f := &fakeStep{}
|
||||
m, _ := fetchModule(t, f)
|
||||
root := filepath.Join(m.Work, "ws1")
|
||||
tree := filepath.Join(root, "tree")
|
||||
for d, body := range map[string]string{"mod": "module example.org/mod\nreplace example.org/lib => ../lib\n", "lib": "module example.org/lib\n"} {
|
||||
if err := os.MkdirAll(filepath.Join(tree, d), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(tree, d, "go.mod"), []byte(body), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
for name, body := range map[string]string{scopeMark: "lib\nmod", moduleDirMark: "mod"} {
|
||||
if err := os.WriteFile(filepath.Join(root, name), []byte(body), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
dirs, err := m.testDirs(context.Background(), tree)
|
||||
if err != nil || !reflect.DeepEqual(dirs, []string{"mod"}) {
|
||||
t.Fatalf("the test directories = %v, %v; want mod alone", dirs, err)
|
||||
}
|
||||
if _, err := m.runLevel(context.Background(), tree, levelOpts{workspace: "ws1", level: LevelUnit}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, l := range f.lines() {
|
||||
if strings.Contains(l, "/tree/lib test") {
|
||||
t.Errorf("the module's unit level tested lib: %s", l)
|
||||
}
|
||||
}
|
||||
// No dir asked: every go.mod of the tree stays.
|
||||
if err := os.Remove(filepath.Join(root, moduleDirMark)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if dirs, _ = m.testDirs(context.Background(), tree); len(dirs) != 2 {
|
||||
t.Errorf("a workspace with no module directory tests %v, want every go.mod", dirs)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -149,8 +149,8 @@ func TestAnyInstalledGoIsHealthyNamedAndReported(t *testing.T) {
|
||||
t.Errorf("health = %+v, want healthy naming %s", a, installed)
|
||||
}
|
||||
tc, ok := states.toolchain("ace.go")
|
||||
if !ok || tc.Version != installed || tc.Recipe != m.Recipe.Version() {
|
||||
t.Errorf("ace.go = %+v, %v; want version %s and recipe %s", tc, ok, installed, m.Recipe.Version())
|
||||
if !ok || tc.Version != sdkstep.LanguageVersion(installed) || tc.Recipe != m.Recipe.Version() {
|
||||
t.Errorf("ace.go = %+v, %v; want version %s and recipe %s", tc, ok, sdkstep.LanguageVersion(installed), m.Recipe.Version())
|
||||
}
|
||||
ran := false
|
||||
run := func(context.Context, map[string]any) (any, error) { ran = true; return "done", nil }
|
||||
|
||||
@@ -58,7 +58,7 @@ func (m *Module) integrationHere(ctx context.Context, workspace, tree string, pa
|
||||
defer os.RemoveAll(run)
|
||||
|
||||
began := m.clock()
|
||||
dirs, err := m.goModDirs(ctx, tree)
|
||||
dirs, err := m.testDirs(ctx, tree)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -186,6 +186,7 @@ func TestABuildRunsShutOffAndOnlyATrunkBuildWritesTheCaches(t *testing.T) {
|
||||
|
||||
// 1309-S3: the digest's go list runs shut off.
|
||||
func TestTheDigestListsPackagesShutOff(t *testing.T) {
|
||||
t.Parallel()
|
||||
m, _ := digestFixture(t)
|
||||
var seen []shutOff
|
||||
m.Run = throughBubblewrap(t, m.Run, &seen)
|
||||
|
||||
@@ -15,6 +15,7 @@ import (
|
||||
// resolves no name, reaches no socket of the machine (a TCP port on its loopback, a unix socket) and leaves the shared
|
||||
// caches unchanged. The probe is testdata/repos/probe.
|
||||
func TestARunStepReachesNoKeyLoginNameOrSocketAndLeavesTheCachesUnchanged(t *testing.T) {
|
||||
t.Parallel()
|
||||
needTools(t, "the shut-off unit level", "go", "git", "bwrap")
|
||||
tree := repository(t, "probe")
|
||||
work, err := os.MkdirTemp("", "w")
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.novox.be/novox/mesh-sdk/go/forgekey"
|
||||
)
|
||||
|
||||
// A directory built with a temporary modfile (an asked version, a pin) is judged by the modfile and the sums beside it, the
|
||||
// files its steps build with, and not by its own go.mod and go.sum: the requirements of an asked version of a mesh package
|
||||
// are in the modfile (takeRequirementsOf), and the directory's own files lack them.
|
||||
func TestADirectoryWithAModfileIsJudgedByTheModfileAndItsSums(t *testing.T) {
|
||||
const nats = "github.com/nats-io/nats.go"
|
||||
for _, c := range []struct {
|
||||
name, modfile, sums string
|
||||
lacks bool
|
||||
}{
|
||||
{name: "the modfile names it with its sums", modfile: "require " + nats + " v1.54.0\n", sums: nats + " v1.54.0 h1:a=\n" + nats + " v1.54.0/go.mod h1:b=\n"},
|
||||
{name: "the modfile lacks it", modfile: "", sums: "", lacks: true},
|
||||
{name: "the modfile names it and the sums lack it", modfile: "require " + nats + " v1.54.0\n", sums: nats + " v1.54.0 h1:a=\n", lacks: true},
|
||||
} {
|
||||
t.Run(c.name, func(t *testing.T) {
|
||||
m := &Module{Work: t.TempDir()}
|
||||
dir := filepath.Join(m.Work, "ws", "tree", "mod")
|
||||
if err := os.MkdirAll(dir, 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
head := "module fixture/mod\n\ngo 1.26\n\n"
|
||||
// The directory's own files lack the requirement, whatever the modfile holds.
|
||||
os.WriteFile(filepath.Join(dir, "go.mod"), []byte(head), 0o644)
|
||||
pins := m.pinsDir(dir)
|
||||
os.MkdirAll(pins, 0o755)
|
||||
os.WriteFile(filepath.Join(pins, "go.mod"), []byte(head+c.modfile), 0o644)
|
||||
os.WriteFile(filepath.Join(pins, "go.sum"), []byte(c.sums), 0o644)
|
||||
m.Run = func(ctx context.Context, env []string, name string, args ...string) forgekey.Ran {
|
||||
return forgekey.Ran{Stdout: loadedModuleMark + nats + " v1.54.0\n"}
|
||||
}
|
||||
err := m.shutOffLoad(context.Background(), nil, dir, "mod", true)
|
||||
if c.lacks != (err != nil) || (err != nil && !strings.Contains(err.Error(), "lacking")) {
|
||||
t.Errorf("shutOffLoad = %v, want a refusal naming what is lacking: %v", err, c.lacks)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -87,8 +87,9 @@ func main() {
|
||||
go func() {
|
||||
<-stdio.Served()
|
||||
if err := findings.Start(ctx); err != nil {
|
||||
logf("the finding writer: %v", err)
|
||||
os.Exit(1)
|
||||
// Said, and the module goes on serving: a finding writer that cannot hear its asks keeps its findings
|
||||
// in its state alone, as the indexer does (lead answer A-394); its builds and steps never wait on it.
|
||||
logf("the finding writer cannot start, its findings are kept in the state alone: %v", err)
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
@@ -195,6 +195,16 @@ func TestTheModuleInvokesTheArtifactSeatsVerbsAndWritesNoStore(t *testing.T) {
|
||||
if !slices.Contains(m.Consumes, "mesh-conditions.findings-asked") {
|
||||
t.Errorf("consumes %v lack mesh-conditions.findings-asked", m.Consumes)
|
||||
}
|
||||
// Design 47 G A-488a, A-488b: its runtime follows the seat list, so the module consumes the seat's two events (which makes its
|
||||
// durable consumer) and invokes the verb that reads the list.
|
||||
for _, ev := range []string{"mesh-catalog.seat-definition-changed", "mesh-catalog.seat-definition-removed"} {
|
||||
if !slices.Contains(m.Consumes, ev) {
|
||||
t.Errorf("consumes %v lack %s", m.Consumes, ev)
|
||||
}
|
||||
}
|
||||
if !slices.Contains(m.Invokes, "seat:mesh-catalog.seat-definitions") {
|
||||
t.Errorf("invokes %v lack seat:mesh-catalog.seat-definitions", m.Invokes)
|
||||
}
|
||||
// Not a foundation part, it asks its finding stream in requires, by the template events (design 47 G §529, A-30), and
|
||||
// declares no stream of that name.
|
||||
for _, st := range m.Streams {
|
||||
|
||||
@@ -158,6 +158,7 @@ func sleepABit() { time.Sleep(30 * time.Millisecond) }
|
||||
// The workspace is self-contained after the fetch: no alternates file, and git finds the commit with the mirror gone,
|
||||
// as a step shut off with the workspace alone needs. Real git, a local repository as the forge.
|
||||
func TestTheWorkspaceIsSelfContainedAfterAFetchThroughTheMirror(t *testing.T) {
|
||||
t.Parallel()
|
||||
if _, err := exec.LookPath("git"); err != nil {
|
||||
t.Skip("no git")
|
||||
}
|
||||
@@ -176,6 +177,10 @@ func TestTheWorkspaceIsSelfContainedAfterAFetchThroughTheMirror(t *testing.T) {
|
||||
return strings.TrimSpace(string(out))
|
||||
}
|
||||
git(src, "init", "-q")
|
||||
// A fetch is for one module (A-447b): the repository holds a module at its root.
|
||||
if err := os.WriteFile(filepath.Join(src, "go.mod"), []byte("module example.org/x\n\ngo 1.26\n"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(src, "f.txt"), []byte("one\n"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@@ -93,8 +93,8 @@ func TestStartWritesTheToolchainAndHealthIsHealthy(t *testing.T) {
|
||||
break
|
||||
}
|
||||
}
|
||||
if tc.Version != "go1.27.1" || tc.Recipe != m.Recipe.Version() || tc.Language != "go" {
|
||||
t.Fatalf("ace.go = %+v, want go1.27.1 and recipe %s", tc, m.Recipe.Version())
|
||||
if tc.Version != "go1.27" || tc.Recipe != m.Recipe.Version() || tc.Language != "go" {
|
||||
t.Fatalf("ace.go = %+v, want go1.27 and recipe %s", tc, m.Recipe.Version())
|
||||
}
|
||||
if a := m.Health(ctx); a.State != health.StateHealthy || !a.Healthy {
|
||||
t.Errorf("health = %+v, want healthy", a)
|
||||
|
||||
@@ -45,7 +45,7 @@ func TestAFetchWithNamedPathsNarrowsTheWorkspace(t *testing.T) {
|
||||
if strings.Join(res.Modules, ",") != "lib" {
|
||||
t.Errorf("the fetch answered the modules %v, want lib alone", res.Modules)
|
||||
}
|
||||
_, st = fetched(t, m, states, map[string]any{"repository": "novox/narrow", "commit": head, "paths": []any{"cmd"}})
|
||||
_, st = fetched(t, m, states, map[string]any{"repository": "novox/narrow", "commit": head, "dir": "", "paths": []any{"cmd"}})
|
||||
if st.State != sdkstep.StepFailed || !strings.Contains(st.Reason, "no module directory") {
|
||||
t.Errorf("paths without a directory: %s %q", st.State, st.Reason)
|
||||
}
|
||||
|
||||
@@ -21,6 +21,7 @@ import (
|
||||
// build_go_fetch is asked that head as the package's version and build_go_build runs on the member, then both pass, the
|
||||
// build with GOPROXY=off against the head: the older commit holds no Go package at all.
|
||||
func TestRealFetchAndBuildOfAMemberAskedAPullHeadItsGoModDoesNotName(t *testing.T) {
|
||||
t.Parallel()
|
||||
// Not skipped when a tool is missing: a pass of the check must mean this test ran (the check prints no test log).
|
||||
for _, tool := range []string{"go", "git"} {
|
||||
if _, err := exec.LookPath(tool); err != nil {
|
||||
|
||||
@@ -50,7 +50,15 @@ func newPinHarness(t *testing.T, gomod string, vendor bool, pulls string) (*pinH
|
||||
for _, g := range strings.Fields(f.gomods) {
|
||||
d := filepath.Join(args[i+1], filepath.Dir(g))
|
||||
os.MkdirAll(d, 0o755)
|
||||
os.WriteFile(filepath.Join(d, "go.mod"), []byte(gomod), 0o644)
|
||||
body := gomod
|
||||
if g == "go.mod" { // the root replaces the other directories, so a fetch asked with it chooses them (A-447)
|
||||
for _, o := range strings.Fields(f.gomods) {
|
||||
if o != "go.mod" {
|
||||
body += "\nreplace local/" + filepath.Dir(o) + " => ./" + filepath.Dir(o) + "\n"
|
||||
}
|
||||
}
|
||||
}
|
||||
os.WriteFile(filepath.Join(d, "go.mod"), []byte(body), 0o644)
|
||||
os.WriteFile(filepath.Join(d, "go.sum"), []byte("sum\n"), 0o644)
|
||||
}
|
||||
if vendor {
|
||||
@@ -296,6 +304,7 @@ func TestARelativeReplaceStillResolvesInTheTemporaryModfile(t *testing.T) {
|
||||
// the asked versions, else from the trunk's latest published version, and never looked for as the commit 000000000000
|
||||
// among the pull request heads.
|
||||
func TestARequirementAtTheZeroVersionIsTakenFromTheAskOrTheTrunk(t *testing.T) {
|
||||
t.Parallel()
|
||||
const sdk = "git.novox.be/novox/mesh-sdk/go"
|
||||
zeroMod := "module fixture/member\n\ngo 1.26\n\nrequire (\n\t" + sdk + " v0.0.0-00010101000000-" + zeroShort + "\n)\n"
|
||||
trunk := strings.Repeat("a", 40)
|
||||
|
||||
@@ -171,6 +171,7 @@ func TestRealFetchMergesConflictsAndPlainlyFetches(t *testing.T) {
|
||||
// sibling and passes with it; the sibling is a plain directory with no symlink in it; and a sibling that holds a
|
||||
// symlink fails the fetch naming it.
|
||||
func TestRealTestsReadTheSiblingBesideTheTree(t *testing.T) {
|
||||
t.Parallel()
|
||||
needTools(t, "the shut-off unit level", "go", "git", "bwrap") // the tests run in bubblewrap (1309)
|
||||
f := newForge(t)
|
||||
files := map[string]string{}
|
||||
|
||||
@@ -36,7 +36,7 @@ func (m *Module) run(ctx context.Context, fetch steprun.StepFunc, a sdkstep.Step
|
||||
return nil, err
|
||||
}
|
||||
key := sdkstep.RunKey(job.Program, sdkstep.ToolchainUsed{Version: installed, Recipe: m.Recipe.Version()}, a.Packages)
|
||||
fetchArgs := sdkstep.FetchRequest{Repository: job.Program.Repository, Commit: job.Program.Commit, Packages: a.Packages}.Args()
|
||||
fetchArgs := sdkstep.FetchRequest{Repository: job.Program.Repository, Commit: job.Program.Commit, Dir: job.Program.Dir, Packages: a.Packages}.Args()
|
||||
exe, built, err := m.programOf(ctx, fetch, "programs", "checking program", key, fetchArgs, job.Program.Dir, job.Program.Package)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
@@ -118,6 +118,7 @@ func resolve(t *testing.T, addr string, a builds.BuiltArtifact) []byte {
|
||||
// reference resolves in a real artifact store to bytes of that digest. The build is the real go's, shut off, of the
|
||||
// fixture testdata/repos/build, with an archive beside its bundle.
|
||||
func TestADoneBuildsArtifactsResolveInARealArtifactStore(t *testing.T) {
|
||||
t.Parallel()
|
||||
needTools(t, "the build", "go", "bwrap")
|
||||
work := t.TempDir()
|
||||
tree := filepath.Join(work, "ws1", "tree")
|
||||
|
||||
@@ -109,18 +109,22 @@ func (m *Module) test(ctx context.Context, args map[string]any) (any, error) {
|
||||
}
|
||||
|
||||
// compareGenerated regenerates the generated Go files of each contracts module in the tree (every module.json in the tree, in
|
||||
// any folder: ADR 0634 §8; CAT-BG5) and fails the level on any out of step, Why naming each path.
|
||||
// any folder: ADR 0634 §8; CAT-BG5; for a workspace fetched for a module's directory, that module's own alone) and fails the level on any out of step, Why naming each path.
|
||||
func compareGenerated(tree string, res *TestResult) error {
|
||||
dirs, err := steprun.ModuleDirs(tree)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, dir := range dirs {
|
||||
// A step for a module compares its own module.json alone, never another module's of the repository (A-447c).
|
||||
rel, _ := filepath.Rel(tree, dir)
|
||||
if own, ok := moduleDirOf(tree); ok && filepath.ToSlash(rel) != own {
|
||||
continue
|
||||
}
|
||||
paths, err := steprun.OutOfStep(dir, steprun.GeneratedRootGo)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
rel, _ := filepath.Rel(tree, dir)
|
||||
for _, p := range paths {
|
||||
res.OutOfStep = append(res.OutOfStep, filepath.ToSlash(filepath.Join(rel, p)))
|
||||
}
|
||||
@@ -252,7 +256,7 @@ type levelOpts struct {
|
||||
|
||||
// runLevel runs `go test -json` in each go.mod directory of the tree and judges the whole against the bounds.
|
||||
func (m *Module) runLevel(ctx context.Context, tree string, o levelOpts) (*TestResult, error) {
|
||||
dirs, err := m.goModDirs(ctx, tree)
|
||||
dirs, err := m.testDirs(ctx, tree)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -62,6 +62,7 @@ func repository(t *testing.T, name string) string {
|
||||
// test_test.go read the output captured from them, and this one proves the capture is what the real go says today. It
|
||||
// starts go test, so it carries the tag.
|
||||
func TestUnitLevelRunsTheRealGoOnTheFixtureRepositories(t *testing.T) {
|
||||
t.Parallel()
|
||||
needTools(t, "the unit level on fixtures", "go", "git", "bwrap")
|
||||
want := map[string]struct {
|
||||
passed, noTests bool
|
||||
@@ -104,6 +105,7 @@ func TestUnitLevelRunsTheRealGoOnTheFixtureRepositories(t *testing.T) {
|
||||
// 086 A-C3 against the real go (design 47 line 571 at b229f5c4): a tree holding an unformatted file passes its unit
|
||||
// level, its two tests run, and the lint step's body fails naming the file.
|
||||
func TestAnUnformattedFileFailsTheRealLintStepAndNotTheUnitLevel(t *testing.T) {
|
||||
t.Parallel()
|
||||
needTools(t, "the unit level's gofmt and vet", "go", "git", "bwrap")
|
||||
home := t.TempDir()
|
||||
m := &Module{Home: home, Work: t.TempDir(), Cache: t.TempDir()}
|
||||
@@ -150,7 +152,7 @@ func TestIntegrationLevelRunsAgainstARealStoreAndBusAndLeavesNothing(t *testing.
|
||||
needTools(t, "the integration level", "go", "git", "initdb", "pg_ctl", "postgres", "bwrap")
|
||||
tree := repository(t, "store")
|
||||
// A short work directory: a unix socket's path is bounded at 108 bytes.
|
||||
work, err := os.MkdirTemp("/tmp", "w")
|
||||
work, err := os.MkdirTemp(os.TempDir(), "w")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -185,6 +187,7 @@ func TestIntegrationLevelRunsAgainstARealStoreAndBusAndLeavesNothing(t *testing.
|
||||
// 1487/AC2: given a fixture Go module, when it is built twice, both digests are equal and the archive holds one
|
||||
// executable named as binaryName says. It runs the real go, so it carries the tag.
|
||||
func TestBuildTwiceGivesOneDigestAndOneExecutable(t *testing.T) {
|
||||
t.Parallel()
|
||||
needTools(t, "the build", "go", "bwrap")
|
||||
work := t.TempDir()
|
||||
tree := filepath.Join(work, "ws1", "tree")
|
||||
|
||||
@@ -340,3 +340,36 @@ func TestADirectoryThatVendorsIsReadFromItsVendorDirectory(t *testing.T) {
|
||||
t.Errorf("the last GOFLAGS of go test in a vendoring directory is %q", flags)
|
||||
}
|
||||
}
|
||||
|
||||
// A-447c: a workspace fetched for a module's directory compares the generated files of that module's own module.json
|
||||
// alone, never another module's of the repository.
|
||||
func TestUnitLevelComparesOnlyTheOwnModulesGeneratedFiles(t *testing.T) {
|
||||
m, states, ws, _ := workspaceFor(t, "empty")
|
||||
tree := filepath.Join(m.Work, ws, "tree")
|
||||
for _, folder := range []string{"domain", "other"} {
|
||||
for p, text := range map[string]string{
|
||||
"module.json": `{"module":"c","build":{"artifacts":[{"name":"go","kind":"package","language":"go","paths":["go"],"generated-from":"."}]}}`,
|
||||
"seats/one-store/seat.json": `{"format":1,"seat":"one-store","scope":"mesh","grain":"seat-list","domain":"Module","decision":"ADR 1","verbs":[{"name":"query","input":{"type":"object"}}]}`,
|
||||
"go/go.mod": "module example.org/c/go\n",
|
||||
"go/stray.go": "package stray\n",
|
||||
} {
|
||||
full := filepath.Join(tree, folder, filepath.FromSlash(p))
|
||||
_ = os.MkdirAll(filepath.Dir(full), 0o755)
|
||||
if err := os.WriteFile(full, []byte(text), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(m.Work, ws, moduleDirMark), []byte("domain"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
res, _ := runUnit(t, m, states, ws)
|
||||
if len(res.OutOfStep) == 0 {
|
||||
t.Fatalf("nothing out of step: %+v", res)
|
||||
}
|
||||
for _, p := range res.OutOfStep {
|
||||
if !strings.HasPrefix(p, "domain/") {
|
||||
t.Errorf("out of step = %v, want the module's own files under domain/ alone", res.OutOfStep)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+44
@@ -0,0 +1,44 @@
|
||||
// Command mesh-compose is the stand-in composer the compose ask's test builds: a program of the contract the Go build module
|
||||
// runs (`-request <file>`, the last line of its standard output a composer verdict of version 1), which judges nothing and
|
||||
// answers each case of the request by its name, with no manifest and no machine. It imports nothing of the mesh.
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
func main() {
|
||||
file := flag.String("request", "", "the request file")
|
||||
flag.Parse()
|
||||
raw, err := os.ReadFile(*file)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
var req struct {
|
||||
Cases []struct {
|
||||
Name string `json:"name"`
|
||||
} `json:"cases"`
|
||||
}
|
||||
if err := json.Unmarshal(raw, &req); err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
type verdict struct {
|
||||
Name string `json:"name"`
|
||||
Manifests map[string]any `json:"manifests"`
|
||||
}
|
||||
out := struct {
|
||||
V int `json:"v"`
|
||||
SDK string `json:"sdk"`
|
||||
Cases []verdict `json:"cases"`
|
||||
}{V: 1, SDK: "stand-in"}
|
||||
for _, c := range req.Cases {
|
||||
out.Cases = append(out.Cases, verdict{Name: c.Name, Manifests: map[string]any{}})
|
||||
}
|
||||
line, _ := json.Marshal(out)
|
||||
fmt.Println(string(line))
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
module standin
|
||||
|
||||
go 1.26
|
||||
@@ -74,6 +74,8 @@ func putReporting(put steprun.Put, r *toolchainReporter, digest func(version str
|
||||
}
|
||||
if eerr := r.Report(key, rev, t); eerr != nil && say != nil {
|
||||
say("toolchain-reported could not be said: %v", eerr)
|
||||
} else if eerr == nil && say != nil {
|
||||
say("toolchain-reported said: %s %s (key %s, revision %d)", t.Language, t.Version, key, rev)
|
||||
}
|
||||
return rev, nil
|
||||
}
|
||||
|
||||
@@ -53,7 +53,7 @@ func TestGoModVerifyRunsOncePerGoModAndGoSumDigest(t *testing.T) {
|
||||
|
||||
// Without bubblewrap the module cache is not read-only to a step: a pass is never reused.
|
||||
func TestAPassOfVerifyIsNotReusedWhenStepsAreNotShutOff(t *testing.T) {
|
||||
f := &fakeStep{gomods: "a/go.mod\nb/go.mod\n", files: map[string]string{"a/go.mod": "m\n", "b/go.mod": "m\n"}}
|
||||
f := &fakeStep{gomods: "a/go.mod\nb/go.mod\n", files: map[string]string{"a/go.mod": "module m\n", "b/go.mod": "module m\n"}}
|
||||
m, states := fetchModule(t, f) // no Bubblewrap
|
||||
fetchOnce(t, m, states, fetchArgs)
|
||||
fetchOnce(t, m, states, fetchArgs)
|
||||
@@ -65,7 +65,7 @@ func TestAPassOfVerifyIsNotReusedWhenStepsAreNotShutOff(t *testing.T) {
|
||||
// A verify that fails leaves no mark: the next fetch verifies again.
|
||||
func TestAFailedVerifyLeavesNoMark(t *testing.T) {
|
||||
fail := true
|
||||
f := &fakeStep{gomods: "go.mod\n", files: map[string]string{"go.mod": "m\n"}}
|
||||
f := &fakeStep{gomods: "go.mod\n", files: map[string]string{"go.mod": "module m\n"}}
|
||||
f.goRule = func(line string) forgekey.Ran {
|
||||
if strings.HasSuffix(line, " mod verify") && fail {
|
||||
return forgekey.Ran{Status: 1, Stdout: "x v1: dir has been modified\n"}
|
||||
@@ -88,7 +88,7 @@ func TestDownloadsRunSideBySideUpToTheBound(t *testing.T) {
|
||||
running, most := 0, 0
|
||||
release := make(chan struct{}) // closed once two run at once: they overlap however slowly the machine starts them
|
||||
f := &fakeStep{gomods: "a/go.mod\nb/go.mod\nc/go.mod\nd/go.mod\n", files: map[string]string{
|
||||
"a/go.mod": "a\n", "b/go.mod": "b\n", "c/go.mod": "c\n", "d/go.mod": "d\n"}}
|
||||
"a/go.mod": "module a\n", "b/go.mod": "module b\n", "c/go.mod": "module c\n", "d/go.mod": "module d\n"}}
|
||||
f.goRule = func(line string) forgekey.Ran {
|
||||
if strings.HasSuffix(line, " mod download") {
|
||||
mu.Lock()
|
||||
|
||||
@@ -53,6 +53,7 @@ type downloadInfo struct {
|
||||
Version string
|
||||
Sum string
|
||||
GoModSum string
|
||||
GoMod string // the go.mod of the version in the module cache
|
||||
Error string
|
||||
}
|
||||
|
||||
@@ -159,7 +160,10 @@ func (m *Module) takeVersion(ctx context.Context, root, dir string, tv *TakenVer
|
||||
return fmt.Errorf("the asked version %s of %s: %w", tv.Asked, tv.Package, err)
|
||||
}
|
||||
tv.Built, tv.Said = tv.Asked, "Go cannot fetch it: placed from the pull request's head, which has no checksum"
|
||||
return nil
|
||||
if _, err := os.Stat(filepath.Join(where, "go.mod")); err != nil {
|
||||
return nil
|
||||
}
|
||||
return m.takeRequirementsOf(ctx, pinsDir, filepath.Join(where, "go.mod"))
|
||||
}
|
||||
if tagged && compareVersions(tv.Named, info.Version) > 0 {
|
||||
tv.Said = fmt.Sprintf("go.mod names %s, newer than the group's tagged version %s, which is built: the group's check is of its tagged version (ADR 0607 §3)", tv.Named, info.Version)
|
||||
@@ -177,12 +181,21 @@ func (m *Module) takeVersion(ctx context.Context, root, dir string, tv *TakenVer
|
||||
return fmt.Errorf("the asked version %s of %s could not be required: %s", info.Version, tv.Package, causeLine(r.Err+"\n"+r.Stderr))
|
||||
}
|
||||
// The checksums Go computed of the download, which go mod verify checks the module cache against.
|
||||
if err := appendSums(pinsDir, tv.Package, info); err != nil {
|
||||
return err
|
||||
}
|
||||
// The requirements of that version go in too: the modfile names every module its packages load (ownFilesCover).
|
||||
return m.takeRequirementsOf(ctx, pinsDir, info.GoMod)
|
||||
}
|
||||
|
||||
// appendSums writes the checksums of one download (the module's and its go.mod's) into the go.sum in pinsDir.
|
||||
func appendSums(pinsDir, module string, info downloadInfo) error {
|
||||
sums := ""
|
||||
if info.Sum != "" {
|
||||
sums += tv.Package + " " + info.Version + " " + info.Sum + "\n"
|
||||
sums += module + " " + info.Version + " " + info.Sum + "\n"
|
||||
}
|
||||
if info.GoModSum != "" {
|
||||
sums += tv.Package + " " + info.Version + "/go.mod " + info.GoModSum + "\n"
|
||||
sums += module + " " + info.Version + "/go.mod " + info.GoModSum + "\n"
|
||||
}
|
||||
f, err := os.OpenFile(filepath.Join(pinsDir, "go.sum"), os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0o600)
|
||||
if err != nil {
|
||||
@@ -195,6 +208,46 @@ func (m *Module) takeVersion(ctx context.Context, root, dir string, tv *TakenVer
|
||||
return err
|
||||
}
|
||||
|
||||
// takeRequirementsOf requires, in the modfile of pinsDir, each module the go.mod at gomodPath (the go.mod of a version just
|
||||
// taken, from what the fetch downloaded) requires at a version the modfile lacks or names older, with the checksums of
|
||||
// each: the graph of a package version taken into a modfile is the version's, not the one go.mod was written against (a
|
||||
// module whose go.mod names mesh-sdk v0.1.13 and is built at a newer one loads what the newer requires). A module of the
|
||||
// forge is left to its own rule (the zero version, a pin, an asked version).
|
||||
func (m *Module) takeRequirementsOf(ctx context.Context, pinsDir, gomodPath string) error {
|
||||
if gomodPath == "" {
|
||||
return nil
|
||||
}
|
||||
raw, err := os.ReadFile(gomodPath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("the go.mod of the version taken: %w", err)
|
||||
}
|
||||
have, err := os.ReadFile(filepath.Join(pinsDir, "go.mod"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
want, _ := goModRequirements(string(raw))
|
||||
named, _ := goModRequirements(string(have))
|
||||
for _, module := range slices.Sorted(maps.Keys(want)) {
|
||||
version := want[module]
|
||||
if strings.HasPrefix(module, forgePrefix) || (named[module] != "" && compareVersions(named[module], version) >= 0) {
|
||||
continue
|
||||
}
|
||||
r := m.Run(ctx, m.goEnv(), "go", "-C", pinsDir, "mod", "download", "-json", module+"@"+version)
|
||||
var info downloadInfo
|
||||
_ = json.Unmarshal([]byte(r.Stdout), &info)
|
||||
if r.Status != 0 || r.Err != "" || info.Version == "" || info.Error != "" {
|
||||
return downloadFailure(strings.TrimSpace(info.Error+"\n"+r.Err+"\n"+r.Stderr), "downloading %s@%s, a requirement of the version taken", module, version)
|
||||
}
|
||||
if r := m.Run(ctx, m.goEnv(), "go", "-C", pinsDir, "mod", "edit", "-require="+module+"@"+info.Version); r.Status != 0 || r.Err != "" {
|
||||
return fmt.Errorf("%s@%s, a requirement of the version taken, could not be required: %s", module, info.Version, causeLine(r.Err+"\n"+r.Stderr))
|
||||
}
|
||||
if err := appendSums(pinsDir, module, info); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// goModRequirements are the module paths a go.mod requires, with the version each names, and those it replaces.
|
||||
func goModRequirements(gomod string) (required map[string]string, replaced map[string]bool) {
|
||||
required, replaced = map[string]string{}, map[string]bool{}
|
||||
|
||||
@@ -19,6 +19,7 @@ import (
|
||||
// group's head Go cannot fetch is placed from the forge's pull request heads; a go.mod that does not require the package
|
||||
// takes nothing.
|
||||
func TestAnAskedVersionIsWrittenIntoTheModfileWhateverGoModNames(t *testing.T) {
|
||||
t.Parallel()
|
||||
const sdk = "git.novox.be/novox/mesh-sdk/go"
|
||||
trunk := strings.Repeat("a", 40)
|
||||
offHead := strings.Repeat("b", 40) // a commit no pull request's head carries
|
||||
@@ -30,10 +31,17 @@ func TestAnAskedVersionIsWrittenIntoTheModfileWhateverGoModNames(t *testing.T) {
|
||||
forgeDown bool // the forge's listing of pull request heads fails too
|
||||
passing bool // the step ends failed as a transport fault, which the bus gives again (ADR 0664 §1)
|
||||
downloads int // the downloads asked in all (0: not counted)
|
||||
requires string // the go.mod of the version taken, its requirements to be taken too
|
||||
}{
|
||||
{name: "a trunk commit newer than go.mod's", asked: trunk,
|
||||
answer: `{"Path":"` + sdk + `","Version":"v0.1.15-0.20261101000000-aaaaaaaaaaaa","Sum":"h1:zip=","GoModSum":"h1:mod="}`,
|
||||
built: "v0.1.15-0.20261101000000-aaaaaaaaaaaa", edit: "-require=" + sdk + "@v0.1.15-0.20261101000000-aaaaaaaaaaaa"},
|
||||
// The requirements of the version taken go into the modfile with their sums, but a module of the forge is left to its
|
||||
// own rule and one the modfile names at or above the version is left as it is.
|
||||
{name: "a trunk commit whose go.mod requires more", asked: trunk,
|
||||
answer: `{"Path":"` + sdk + `","Version":"v0.1.16-0.20261116170327-aaaaaaaaaaaa","Sum":"h1:zip=","GoModSum":"h1:mod=","GoMod":"GOMOD"}`,
|
||||
built: "v0.1.16-0.20261116170327-aaaaaaaaaaaa", edit: "-require=" + sdk + "@v0.1.16-0.20261116170327-aaaaaaaaaaaa",
|
||||
requires: "module " + sdk + "\n\ngo 1.26\n\nrequire (\n\tgithub.com/nats-io/nats.go v1.54.0\n\tgithub.com/klauspost/compress v1.20.0\n\tgit.novox.be/novox/mesh-contracts/go/x v0.1.0\n)\n"},
|
||||
{name: "go.mod names a newer one", asked: trunk,
|
||||
answer: `{"Path":"` + sdk + `","Version":"v0.1.15-0.20261001000000-aaaaaaaaaaaa","Sum":"h1:zip="}`,
|
||||
built: "v0.1.15-0.20261022120000-" + pinShort, said: "newer than the asked"},
|
||||
@@ -96,11 +104,20 @@ func TestAnAskedVersionIsWrittenIntoTheModfileWhateverGoModNames(t *testing.T) {
|
||||
if name == "git" && strings.Contains(line, "ls-remote") && c.forgeDown {
|
||||
return forgekey.Ran{Status: 128, Stderr: "fatal: unable to access the forge: Could not resolve host\n"}
|
||||
}
|
||||
if name == "go" && strings.Contains(line, " mod download -json github.com/") {
|
||||
module, version, _ := strings.Cut(strings.Fields(line)[len(strings.Fields(line))-1], "@")
|
||||
return forgekey.Ran{Stdout: `{"Path":"` + module + `","Version":"` + version + `","Sum":"h1:` + module + `zip=","GoModSum":"h1:` + module + `mod="}`}
|
||||
}
|
||||
if name == "go" && strings.Contains(line, " mod download -json "+sdk+"@") {
|
||||
h.mu.Lock()
|
||||
h.cmds = append(h.cmds, cmdRecord{env: env, line: line})
|
||||
h.mu.Unlock()
|
||||
downloads++
|
||||
if c.requires != "" {
|
||||
gomod := filepath.Join(t.TempDir(), "go.mod")
|
||||
os.WriteFile(gomod, []byte(c.requires), 0o644)
|
||||
return forgekey.Ran{Stdout: strings.ReplaceAll(c.answer, "GOMOD", gomod)}
|
||||
}
|
||||
if c.failing {
|
||||
return forgekey.Ran{Status: 1, Stdout: c.answer}
|
||||
}
|
||||
@@ -158,6 +175,18 @@ func TestAnAskedVersionIsWrittenIntoTheModfileWhateverGoModNames(t *testing.T) {
|
||||
if placed := strings.Contains(string(body), "replace "+sdk+" => "); placed != c.failing {
|
||||
t.Errorf("the modfile replaces mesh-sdk by a placed head: %v, want %v:\n%s", placed, c.failing, body)
|
||||
}
|
||||
if c.requires != "" {
|
||||
for _, dep := range []string{"github.com/nats-io/nats.go@v1.54.0", "github.com/klauspost/compress@v1.20.0"} {
|
||||
module, _, _ := strings.Cut(dep, "@")
|
||||
if !h.ran("-require="+dep) || !strings.Contains(string(sums), module+" "+strings.TrimPrefix(dep, module+"@")+" h1:"+module+"zip=") ||
|
||||
!strings.Contains(string(sums), module+" "+strings.TrimPrefix(dep, module+"@")+"/go.mod h1:"+module+"mod=") {
|
||||
t.Errorf("the requirement %s of the version taken was not required with its sums:\n%s", dep, sums)
|
||||
}
|
||||
}
|
||||
if h.ran("mesh-contracts/go/x") {
|
||||
t.Error("a module of the forge required by the version taken was looked for as a requirement")
|
||||
}
|
||||
}
|
||||
if _, other := h.m.pinModfile(filepath.Join(h.m.Work, res.Workspace, "tree", "other")); other {
|
||||
t.Error("a go.mod that requires no asked package was handed a modfile")
|
||||
}
|
||||
|
||||
@@ -33,7 +33,9 @@
|
||||
}
|
||||
],
|
||||
"consumes": [
|
||||
"mesh-conditions.findings-asked"
|
||||
"mesh-conditions.findings-asked",
|
||||
"mesh-catalog.seat-definition-changed",
|
||||
"mesh-catalog.seat-definition-removed"
|
||||
],
|
||||
"invokes": [
|
||||
"seat:mesh-hand-acts.hand-act",
|
||||
@@ -46,6 +48,7 @@
|
||||
"seat:mesh-wants.input",
|
||||
"seat:mesh-module-assigner.assignments",
|
||||
"seat:mesh-catalog.module",
|
||||
"seat:mesh-catalog.seat-definitions",
|
||||
"seat:mesh-catalog.entry"
|
||||
],
|
||||
"config": {
|
||||
|
||||
@@ -24,7 +24,7 @@ func TestTheModuleInvokesTheImageSeatAndHoldsNoRegistry(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if want := []string{"seat:mesh-hand-acts.hand-act", "seat:container-image-registry.publish", "seat:container-image-registry.publish-part",
|
||||
"seat:container-image-registry.mirror", "seat:mesh-module-assigner.assignments", "seat:mesh-catalog.module"}; !slices.Equal(m.Invokes, want) {
|
||||
"seat:container-image-registry.mirror", "seat:mesh-module-assigner.assignments", "seat:mesh-catalog.module", "seat:mesh-catalog.seat-definitions"}; !slices.Equal(m.Invokes, want) {
|
||||
t.Errorf("invokes = %v, want %v", m.Invokes, want)
|
||||
}
|
||||
// The seat's outcomes a copy waits on; the version comes in the ask, so version.numbered is not heard (design 47 G §413).
|
||||
@@ -32,6 +32,7 @@ func TestTheModuleInvokesTheImageSeatAndHoldsNoRegistry(t *testing.T) {
|
||||
for _, e := range OutcomeEvents {
|
||||
consumes = append(consumes, "container-image-registry."+e)
|
||||
}
|
||||
consumes = append(consumes, "mesh-catalog.seat-definition-changed", "mesh-catalog.seat-definition-removed") // the seat list, A-488a
|
||||
if !slices.Equal(m.Consumes, consumes) {
|
||||
t.Errorf("consumes = %v, want %v", m.Consumes, consumes)
|
||||
}
|
||||
|
||||
@@ -113,6 +113,16 @@ func TestWorkAndStoreDirectoriesLieOutsideTheHome(t *testing.T) {
|
||||
// and the registry's address is bound into a file of the home that the program reads.
|
||||
func TestManifestKeepsItsStatesClaimsItsBuildSeatAndDeclaresItsHealthTool(t *testing.T) {
|
||||
m := readManifest(t)
|
||||
// Design 47 G A-488a, A-488b: its runtime follows the seat list, so the module consumes the seat's two events (which makes its
|
||||
// durable consumer) and invokes the verb that reads the list.
|
||||
for _, ev := range []string{"mesh-catalog.seat-definition-changed", "mesh-catalog.seat-definition-removed"} {
|
||||
if !slices.Contains(m.Consumes, ev) {
|
||||
t.Errorf("consumes %v lack %s", m.Consumes, ev)
|
||||
}
|
||||
}
|
||||
if !slices.Contains(m.Invokes, "seat:mesh-catalog.seat-definitions") {
|
||||
t.Errorf("invokes %v lack seat:mesh-catalog.seat-definitions", m.Invokes)
|
||||
}
|
||||
states := []string{}
|
||||
for _, s := range m.State {
|
||||
states = append(states, s.Name)
|
||||
|
||||
@@ -68,11 +68,14 @@
|
||||
"seat:container-image-registry.publish-part",
|
||||
"seat:container-image-registry.mirror",
|
||||
"seat:mesh-module-assigner.assignments",
|
||||
"seat:mesh-catalog.module"
|
||||
"seat:mesh-catalog.module",
|
||||
"seat:mesh-catalog.seat-definitions"
|
||||
],
|
||||
"consumes": [
|
||||
"container-image-registry.published",
|
||||
"container-image-registry.refused"
|
||||
"container-image-registry.refused",
|
||||
"mesh-catalog.seat-definition-changed",
|
||||
"mesh-catalog.seat-definition-removed"
|
||||
],
|
||||
"own-secrets": {
|
||||
"broker": "${dir:state}/broker"
|
||||
|
||||
@@ -34,7 +34,7 @@ func TestTheGuardSaysToolchainReportedOnAChangeWithTheProgramDigest(t *testing.T
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(got) != 1 || got[0].subject != "mesh.seat.node-build-node.event.toolchain-reported.ace" || got[0].body["node"] != "ace" || got[0].body["language"] != "typescript" ||
|
||||
got[0].body["version"] != "v26.10.0" || got[0].body["recipe"] != m.Recipe.Version() || len(got[0].body["digest"]) != len("sha256:")+64 {
|
||||
got[0].body["version"] != "v26.10" || got[0].body["recipe"] != m.Recipe.Version() || len(got[0].body["digest"]) != len("sha256:")+64 {
|
||||
t.Fatalf("events = %+v, want one toolchain-reported for ace, go with the program's digest", got)
|
||||
}
|
||||
if tc, _ := states.toolchain("ace.typescript"); tc.Digest != got[0].body["digest"] {
|
||||
@@ -51,7 +51,7 @@ func TestTheGuardSaysToolchainReportedOnAChangeWithTheProgramDigest(t *testing.T
|
||||
os.WriteFile(prog, []byte("another node program"), 0o755)
|
||||
m.Probe = func(context.Context) (string, error) { return "v26.11.0", nil }
|
||||
m.Check(context.Background())
|
||||
if len(got) != 2 || got[1].body["version"] != "v26.11.0" || got[1].body["digest"] == got[0].body["digest"] {
|
||||
if len(got) != 2 || got[1].body["version"] != "v26.11" || got[1].body["digest"] == got[0].body["digest"] {
|
||||
t.Errorf("a new version said %+v, want a second event with another digest", got)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
sdkstep "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildstep"
|
||||
"git.novox.be/novox/mesh-sdk/go/health"
|
||||
)
|
||||
|
||||
@@ -36,8 +37,8 @@ func TestAnyInstalledNodeIsHealthyNamedAndReported(t *testing.T) {
|
||||
t.Errorf("health = %+v, want healthy naming %s", a, installed)
|
||||
}
|
||||
tc, ok := states.toolchain("ace.typescript")
|
||||
if !ok || tc.Version != installed || tc.Recipe != m.Recipe.Version() {
|
||||
t.Errorf("ace.typescript = %+v, %v; want version %s and recipe %s", tc, ok, installed, m.Recipe.Version())
|
||||
if !ok || tc.Version != sdkstep.LanguageVersion(installed) || tc.Recipe != m.Recipe.Version() {
|
||||
t.Errorf("ace.typescript = %+v, %v; want version %s and recipe %s", tc, ok, sdkstep.LanguageVersion(installed), m.Recipe.Version())
|
||||
}
|
||||
if err := m.Prepare(context.Background()); err != nil {
|
||||
t.Errorf("no step can run on node %s: %v", installed, err)
|
||||
|
||||
@@ -277,6 +277,17 @@ func TestTheModuleRequiresNodeToolsItPullsThrough(t *testing.T) {
|
||||
if !slices.Contains(m.Requires, "node-tools") {
|
||||
t.Errorf("requires %v lack node-tools", m.Requires)
|
||||
}
|
||||
// Design 47 G A-488a: its runtime follows the seat list (seatlist.Start subscribes to the seat mesh-catalog's events), so the
|
||||
// module consumes both, which makes its durable consumer <node>_mesh-build-node_tools: with none, the seat list is never
|
||||
// read and the asks of a named asker are given back for ever.
|
||||
for _, ev := range []string{"mesh-catalog.seat-definition-changed", "mesh-catalog.seat-definition-removed"} {
|
||||
if !slices.Contains(m.Consumes, ev) {
|
||||
t.Errorf("consumes %v lack %s", m.Consumes, ev)
|
||||
}
|
||||
}
|
||||
if !slices.Contains(m.Invokes, "seat:mesh-catalog.seat-definitions") {
|
||||
t.Errorf("invokes %v lack seat:mesh-catalog.seat-definitions (A-488b)", m.Invokes)
|
||||
}
|
||||
}
|
||||
|
||||
// Design 47 G A-254, A-313, A-314: the module keeps a run's answer over what step-ended carries inline in the steps state's own
|
||||
|
||||
@@ -94,8 +94,8 @@ func TestStartWritesTheToolchainAndHealthIsHealthy(t *testing.T) {
|
||||
break
|
||||
}
|
||||
}
|
||||
if tc.Version != "v26.10.0" || tc.Recipe != m.Recipe.Version() || tc.Language != "typescript" {
|
||||
t.Fatalf("ace.typescript = %+v, want v26.10.0 and recipe %s", tc, m.Recipe.Version())
|
||||
if tc.Version != "v26.10" || tc.Recipe != m.Recipe.Version() || tc.Language != "typescript" {
|
||||
t.Fatalf("ace.typescript = %+v, want v26.10 and recipe %s", tc, m.Recipe.Version())
|
||||
}
|
||||
if a := m.Health(ctx); a.State != health.StateHealthy || !a.Healthy {
|
||||
t.Errorf("health = %+v, want healthy", a)
|
||||
|
||||
@@ -24,6 +24,10 @@
|
||||
"serves": ["current", "drop", "pause", "resume", "steps", "kept-artifact", "output"]
|
||||
}
|
||||
],
|
||||
"consumes": [
|
||||
"mesh-catalog.seat-definition-changed",
|
||||
"mesh-catalog.seat-definition-removed"
|
||||
],
|
||||
"config": {
|
||||
"capacity": {
|
||||
"type": "number",
|
||||
@@ -76,7 +80,8 @@
|
||||
"seat:npm-package-registry.publish",
|
||||
"seat:npm-package-registry.publish-part",
|
||||
"seat:mesh-module-assigner.assignments",
|
||||
"seat:mesh-catalog.module"
|
||||
"seat:mesh-catalog.module",
|
||||
"seat:mesh-catalog.seat-definitions"
|
||||
],
|
||||
"own-secrets": {
|
||||
"broker": "${dir:state}/broker"
|
||||
|
||||
@@ -289,12 +289,12 @@ var commands = []command{
|
||||
does: "clear one of a module's config values on one node", allowed: []string{"node", "why"},
|
||||
build: func(l line) (call, error) { return configChange(l, "clear") }},
|
||||
{words: []string{"config"}, usage: "[list|set|clear|history|propose|proposals] [--module m] [--node n] [--key k] [--value v] [--why w]",
|
||||
does: "the mesh's config values, at any level", allowed: []string{"module", "node", "key", "value", "why", "limit", "proposal", "pull", "clear"},
|
||||
does: "the mesh's config values, at any level", allowed: []string{"module", "node", "key", "value", "why", "limit", "proposal", "clear"},
|
||||
build: func(l line) (call, error) {
|
||||
if len(l.pos) > 1 {
|
||||
return call{}, usageErr("give at most one action")
|
||||
}
|
||||
a := pick(l, "module", "node", "key", "why", "proposal", "pull", "clear", "limit")
|
||||
a := pick(l, "module", "node", "key", "why", "proposal", "clear", "limit")
|
||||
if len(l.pos) == 1 {
|
||||
a["action"] = l.pos[0]
|
||||
}
|
||||
|
||||
@@ -173,6 +173,7 @@ func TestALineWithNoOwnerOrNotReadCallsNothingAndSaysWhy(t *testing.T) {
|
||||
{"conditions --bogus x", 2, "--bogus is not a flag"},
|
||||
{"settings set zsh {\"execute\":\"withhold\"} --node g14 --why x", 2, "set each key"},
|
||||
{"healers --days soon", 2, "takes a number"},
|
||||
{"config propose --pull novox/x#1", 2, "--pull is not a flag"},
|
||||
{"frobnicate", 2, "is not a command"},
|
||||
} {
|
||||
got := console(t, nil)
|
||||
|
||||
@@ -30,7 +30,7 @@ module none of whose manifests can be read is listed under `unresolved` with why
|
||||
values last resolved for it. Then: the manifest's default; mesh config for the predefined key the value falls back
|
||||
to; node config for that key on the node; the module's own value on that node, the most specific winning. A value
|
||||
naming no predefined key resolves from its default and the module's own value only (ADR 0348 §2). It resolves at
|
||||
start, whenever an assignment, a want or a trunk module moves, and every five minutes, writing only the values that moved.
|
||||
start, whenever an assignment, a want or a trunk module moves, writing only the values that moved.
|
||||
|
||||
## The verb
|
||||
|
||||
@@ -87,8 +87,9 @@ Each directory or archive this module declares says how it treats a file it did
|
||||
- `mesh-config_declared`: each placed module's declarations at the trunk, with the manifest and commit they come
|
||||
from, written only when they differ.
|
||||
- Its own sources hold always (novox/hq ADR 0590): declarations from the modules' own manifests, the list from the
|
||||
indexer's `predefined`, pull requests' config from the indexer's `config-candidates`. The controller's
|
||||
`pull-declares`, the take-over, compare and hand-back went with the controller. `config_declared` and
|
||||
indexer's `predefined`. The controller's `pull-declares`, the take-over, compare and hand-back went with the
|
||||
controller, and so did every pull request path (`pulls`, `kept`, `held`, a proposal's `pull`): the mesh knows no pull
|
||||
request (design 47 G, ADR 0502 ended by the operator's word, record pending). `config_declared` and
|
||||
`config_resync` read and write again what it keeps.
|
||||
- A trusted value is set only through propose and the operator's Approve: no controller relay is trusted (issue 1126).
|
||||
- Its sets, clears and warrants are recorded in the hand-act log of the seat `mesh-hand-acts` alone.
|
||||
|
||||
@@ -73,10 +73,6 @@ type Change struct {
|
||||
Approved string `json:"approved,omitempty"`
|
||||
At time.Time `json:"at"`
|
||||
Moved []Moved `json:"moved"`
|
||||
// Pull and Head name the pull request a value is held for, dropped from, or applied from at its merge (novox/hq
|
||||
// ADR 0502): the actions hold and drop are always about one, and a set with a pull is its merge.
|
||||
Pull string `json:"pull,omitempty"`
|
||||
Head string `json:"head,omitempty"`
|
||||
}
|
||||
|
||||
// Config is the module: its store, its clock, and one lock, so a set and a resolution never interleave.
|
||||
@@ -94,8 +90,6 @@ type Config struct {
|
||||
|
||||
// Asks is the operator channel as this module asks on it (proposals.go); nil, nothing can be proposed.
|
||||
Asks AskBus
|
||||
// Forge is the forge as pulls are asked of it (pulls.go); nil, a merge or a close is heard from its events alone.
|
||||
Forge PullForge
|
||||
// Delivery is what the delivery parts answer (renames.go, retiring.go); nil, no window closes and nothing retires.
|
||||
Delivery Delivery
|
||||
// Catalog is the catalog half: which modules exist and the manifest each entry carries, and the entry of a build's
|
||||
@@ -130,13 +124,6 @@ type Call struct {
|
||||
// Clear asks a proposal to clear the value instead of setting it; Proposal names one proposal to read.
|
||||
Clear bool
|
||||
Proposal string
|
||||
// Pull names the open pull request that introduces the key, for propose (a held value) and pull-declares; Head is
|
||||
// the commit its declarations are read at; Declares the config declarations the head adds or changes, by module;
|
||||
// Pulls the pull requests held asks about, each at a head.
|
||||
Pull string
|
||||
Head string
|
||||
Declares map[string]any
|
||||
Pulls []PullAt
|
||||
|
||||
// approval is the operator's warrant a set or a clear is made on: given by a decided proposal alone, never by
|
||||
// a call's arguments.
|
||||
@@ -177,13 +164,8 @@ func (c *Config) Do(call Call) (any, error) {
|
||||
return c.Propose(call)
|
||||
case "proposals":
|
||||
return c.Proposals(call)
|
||||
case "pull-declares":
|
||||
return c.PullDeclares(call)
|
||||
case "held":
|
||||
return c.Held(call)
|
||||
}
|
||||
return nil, fmt.Errorf("config does set, clear, list, history, propose, proposals, pull-declares or held; %q is "+
|
||||
"none of them", call.Action)
|
||||
return nil, fmt.Errorf("config does set, clear, list, history, propose or proposals; %q is none of them", call.Action)
|
||||
}
|
||||
|
||||
// level is the level a call names, refused as ADR 0348 §3 says: a module's own value is for one node.
|
||||
@@ -211,10 +193,6 @@ func (c *Config) Change(call Call) (*SetAnswer, error) {
|
||||
if call.Clear || call.Proposal != "" {
|
||||
return nil, fmt.Errorf("clear and proposal are a proposal's: %s takes neither", call.Action)
|
||||
}
|
||||
if call.Pull != "" {
|
||||
return nil, fmt.Errorf("pull names the pull request a value is held for, which only a proposal does: %s takes "+
|
||||
"none", call.Action)
|
||||
}
|
||||
call.approval = nil // only a decided proposal gives one
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
|
||||
@@ -131,3 +131,37 @@ func (v toolVault) Retire(r RetireRequest) error {
|
||||
_, err = v.ask(VaultRetireAsk, map[string]any{"node": r.Node, "module": r.Module, "name": r.Name, "warrant": rec.Act.ID, "ask": r.Ask})
|
||||
return err
|
||||
}
|
||||
|
||||
// unwrapAnswer reads a tool's answer whatever wraps it: the protocol's content list, or {output, ok, answer}.
|
||||
func unwrapAnswer(raw json.RawMessage) (json.RawMessage, string, bool) {
|
||||
var s string
|
||||
if json.Unmarshal(raw, &s) == nil {
|
||||
return unwrapAnswer(json.RawMessage(s))
|
||||
}
|
||||
var m map[string]json.RawMessage
|
||||
if json.Unmarshal(raw, &m) != nil {
|
||||
return raw, string(raw), true
|
||||
}
|
||||
if content, has := m["content"]; has {
|
||||
var items []struct {
|
||||
Text string `json:"text"`
|
||||
}
|
||||
var e struct {
|
||||
IsError bool `json:"isError"`
|
||||
}
|
||||
_ = json.Unmarshal(raw, &e)
|
||||
if json.Unmarshal(content, &items) == nil && len(items) > 0 {
|
||||
inner, out, ok := unwrapAnswer(json.RawMessage(items[0].Text))
|
||||
return inner, out, ok && !e.IsError
|
||||
}
|
||||
}
|
||||
var v struct {
|
||||
Output string `json:"output"`
|
||||
OK *bool `json:"ok"`
|
||||
Answer json.RawMessage `json:"answer"`
|
||||
}
|
||||
if json.Unmarshal(raw, &v) == nil && v.OK != nil {
|
||||
return v.Answer, v.Output, *v.OK
|
||||
}
|
||||
return raw, string(raw), true
|
||||
}
|
||||
|
||||
@@ -19,8 +19,6 @@ type Listed struct {
|
||||
Withheld string `json:"withheld,omitempty"`
|
||||
// Unresolved is each placed module the call narrows to whose declarations cannot be read, with why (issue 1953).
|
||||
Unresolved []Unresolved `json:"unresolved,omitempty"`
|
||||
// Held is the approved values held for open pull requests, not yet set (novox/hq ADR 0502).
|
||||
Held []HeldValue `json:"held,omitempty"`
|
||||
}
|
||||
|
||||
// SetValue is one value set, at its level.
|
||||
@@ -95,10 +93,6 @@ func (c *Config) List(call Call) (*Listed, error) {
|
||||
if call.Module == "" {
|
||||
out.Predefined = s.Predefined
|
||||
}
|
||||
out.Held = heldListed(s, call)
|
||||
if len(out.Held) == 0 {
|
||||
out.Held = nil
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -38,15 +38,19 @@ func logf(format string, a ...any) { fmt.Fprintf(os.Stderr, format+"\n", a...) }
|
||||
var theViews *ownerViews
|
||||
|
||||
// runtimeStore is the bus, through the runtime that launched this bundle. A state of the delivery owners that the
|
||||
// owners' tools answer is read from the picture those keep (ownerviews.go), the rest from the bus.
|
||||
type runtimeStore struct{}
|
||||
// owners' tools answer is read from the picture those keep (ownerviews.go), this module's own states from the copy their
|
||||
// watches keep (ownstates.go), the rest from the bus.
|
||||
type runtimeStore struct{ own *ownCopy }
|
||||
|
||||
func (runtimeStore) Get(state, key string) (*Entry, error) {
|
||||
func (r runtimeStore) Get(state, key string) (*Entry, error) {
|
||||
if theViews != nil {
|
||||
if e, followed, err := theViews.get(state, key); followed {
|
||||
return e, err
|
||||
}
|
||||
}
|
||||
if r.own != nil && ownStates[state] {
|
||||
return r.own.get(state, key)
|
||||
}
|
||||
e, err := stdio.State(state).Get(key)
|
||||
if err != nil || e == nil {
|
||||
return nil, err
|
||||
@@ -54,19 +58,30 @@ func (runtimeStore) Get(state, key string) (*Entry, error) {
|
||||
return &Entry{Key: e.Key, Value: e.Value, Revision: e.Revision}, nil
|
||||
}
|
||||
|
||||
func (runtimeStore) Put(state, key string, value any) error {
|
||||
func (r runtimeStore) Put(state, key string, value any) error {
|
||||
if r.own != nil && ownStates[state] {
|
||||
return r.own.write(state, key, value)
|
||||
}
|
||||
_, err := stdio.State(state).Put(key, value)
|
||||
return err
|
||||
}
|
||||
|
||||
func (runtimeStore) Delete(state, key string) error { return stdio.State(state).Delete(key) }
|
||||
func (r runtimeStore) Delete(state, key string) error {
|
||||
if r.own != nil && ownStates[state] {
|
||||
return r.own.remove(state, key)
|
||||
}
|
||||
return stdio.State(state).Delete(key)
|
||||
}
|
||||
|
||||
func (runtimeStore) Keys(state string) ([]string, error) {
|
||||
func (r runtimeStore) Keys(state string) ([]string, error) {
|
||||
if theViews != nil {
|
||||
if keys, followed, err := theViews.keys(state); followed {
|
||||
return keys, err
|
||||
}
|
||||
}
|
||||
if r.own != nil && ownStates[state] {
|
||||
return r.own.keys(state)
|
||||
}
|
||||
return stdio.State(state).Keys()
|
||||
}
|
||||
|
||||
@@ -119,8 +134,22 @@ func (runtimeAsks) Warranted(ask string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// armsDeadline says a write to this state can move the deadline timer: a proposal, a retirement or a rename window (A-490l).
|
||||
func armsDeadline(state string) bool {
|
||||
return state == ProposalsState || state == RetiringState || state == RenamesState
|
||||
}
|
||||
|
||||
func main() {
|
||||
c := &Config{Store: runtimeStore{}, Logf: logf, Asks: runtimeAsks{}, Forge: toolForge{ask: stdio.Ask},
|
||||
own, rearm := newOwnCopy(), make(chan struct{}, 1)
|
||||
own.changed = func(state string) {
|
||||
if armsDeadline(state) {
|
||||
select {
|
||||
case rearm <- struct{}{}:
|
||||
default:
|
||||
}
|
||||
}
|
||||
}
|
||||
c := &Config{Store: runtimeStore{own: own}, Logf: logf, Asks: runtimeAsks{},
|
||||
Delivery: toolDelivery{ask: stdio.Ask}, Vault: toolVault{ask: stdio.Ask, recorder: recorder}}
|
||||
// The module's one held writer (design 47 G §689, A-16, A-29, A-32): the renames' and retirements' findings and the views reader's go through it.
|
||||
w, err := views.NewStdioHeldWriter(views.WriterConfig[meshconditions.Finding, meshconditions.FindingCleared, meshconditions.FindingsStated, meshconditions.FindingsAskedEvent]{
|
||||
@@ -181,6 +210,7 @@ func main() {
|
||||
theViews.Start(context.Background())
|
||||
go resolving(c, moved)
|
||||
go hearing(c)
|
||||
go deciding(c, rearm)
|
||||
go hearingDelivery(c)
|
||||
if err := stdio.Serve("", append(Tools(c), branches.EntryAgainTool("config", c.Store.HandAct))); err != nil {
|
||||
logf("%v", err)
|
||||
@@ -188,23 +218,12 @@ func main() {
|
||||
}
|
||||
}
|
||||
|
||||
// resolving resolves at start and again whenever a source moves, and every five minutes besides, so a change
|
||||
// heard nowhere is still resolved. The runtime answers only once Serve runs, so the watches are asked again
|
||||
// until they are taken.
|
||||
// resolving resolves at start and again whenever a source moves: an assignment, a want, a catalog revision, an entry
|
||||
// answered or the predefined list (each reaches `moved` through its view, ownerviews.go and main), and never on a timer
|
||||
// (novox/hq principle 1, issue 1982). The runtime answers only once Serve runs.
|
||||
func resolving(c *Config, moved chan struct{}) {
|
||||
// The owners' states reach `moved` through their views (ownerviews.go); the builder's builds, which has no tool yet,
|
||||
// is read when a want moves, and nothing else is watched.
|
||||
tick := time.NewTicker(5 * time.Minute)
|
||||
defer tick.Stop()
|
||||
time.Sleep(time.Second)
|
||||
for sweep := true; ; {
|
||||
if sweep {
|
||||
// A merge or a close of a pull request a value is held for, heard nowhere, is found here (ADR 0502).
|
||||
for _, s := range c.SweepPulls() {
|
||||
logf("[mesh-config] %s", s)
|
||||
}
|
||||
}
|
||||
sweep = false
|
||||
<-stdio.Served()
|
||||
for {
|
||||
r, err := c.Reconcile()
|
||||
switch {
|
||||
case err != nil:
|
||||
@@ -215,21 +234,22 @@ func resolving(c *Config, moved chan struct{}) {
|
||||
if r.Published != "" {
|
||||
logf("[mesh-config] published %s", r.Published)
|
||||
}
|
||||
// A window or a retirement judged while the entry of the build it waits on was still asked stays open until the
|
||||
// entry answers: the answer moves this pass, and the open ones are judged again here (design 32 *A config value
|
||||
// renamed*). It reads nothing from the bus when none is open.
|
||||
for _, s := range c.Windows("") {
|
||||
logf("[mesh-config] %s", s)
|
||||
}
|
||||
<-moved
|
||||
time.Sleep(500 * time.Millisecond) // a burst of writes is one resolution
|
||||
select {
|
||||
case <-moved:
|
||||
time.Sleep(500 * time.Millisecond) // a burst of writes is one resolution
|
||||
select {
|
||||
case <-moved:
|
||||
default:
|
||||
}
|
||||
case <-tick.C:
|
||||
sweep = true
|
||||
default:
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// hearing takes the router's word on this module's asks, asked again until the runtime takes the subscription, and
|
||||
// reads the router's record of every waiting ask once a minute besides, so an answer heard nowhere is still taken.
|
||||
// hearing takes the router's word on this module's asks, asked again until the runtime takes the subscription.
|
||||
func hearing(c *Config) {
|
||||
go func() {
|
||||
<-stdio.Served() // subscribed as soon as the runtime serves, never after a guessed wait: an event sent meanwhile is heard
|
||||
@@ -251,32 +271,32 @@ func hearing(c *Config) {
|
||||
time.Sleep(wait)
|
||||
}
|
||||
}()
|
||||
for _, pattern := range []string{"git.pull-merged", "git.pull-closed"} {
|
||||
go func(pattern string) {
|
||||
<-stdio.Served() // subscribed as soon as the runtime serves, never after a guessed wait: an event sent meanwhile is heard
|
||||
for wait := 2 * time.Second; ; wait = min(wait*2, time.Minute) {
|
||||
err := stdio.Subscribe(pattern, func(e stdio.Envelope) error {
|
||||
p, ok := pullEventOf(e.Body)
|
||||
if !ok {
|
||||
logf("[mesh-config] %s is not readable", pattern)
|
||||
return nil
|
||||
}
|
||||
for _, s := range c.HeardPull(p, pattern == "git.pull-merged") {
|
||||
logf("[mesh-config] %s", s)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
logf("[mesh-config] does not hear %s yet (%v); asking again in %s", pattern, err, wait)
|
||||
time.Sleep(wait)
|
||||
}
|
||||
}(pattern)
|
||||
}
|
||||
|
||||
// DeadlineFloorKey is this module's setting for the least wait of its one deadline timer, in seconds (module.json `config`).
|
||||
const DeadlineFloorKey = "deadline-floor-seconds"
|
||||
|
||||
// DefaultDeadlineFloor is the floor when the setting cannot be read.
|
||||
const DefaultDeadlineFloor = time.Minute
|
||||
|
||||
// floorOf is the floor a setting's value names: its seconds, or the default for a value that is not a positive number.
|
||||
func floorOf(raw json.RawMessage, err error) time.Duration {
|
||||
var seconds float64
|
||||
if err != nil || json.Unmarshal(raw, &seconds) != nil || seconds < 1 {
|
||||
return DefaultDeadlineFloor
|
||||
}
|
||||
tick := time.NewTicker(time.Minute)
|
||||
defer tick.Stop()
|
||||
for range tick.C {
|
||||
return time.Duration(seconds * float64(time.Second))
|
||||
}
|
||||
|
||||
func deadlineFloor() time.Duration { return floorOf(stdio.Config(DeadlineFloorKey)) }
|
||||
|
||||
// deciding reads the router's record of every waiting ask once at start, which finds an answer said while this module was
|
||||
// down, and then when the router says an ask ended (hearing's `decided` subscription calls Decide itself) or the next
|
||||
// deadline of a waiting proposal or retirement passes: one timer for the earliest, set again whenever a proposal or a
|
||||
// retirement is written (issue 1982; novox/hq ADR 0426 §3: a timer is a deadline, never a poll).
|
||||
func deciding(c *Config, rearm chan struct{}) {
|
||||
<-stdio.Served()
|
||||
decide := func() {
|
||||
said, err := c.Decide()
|
||||
if err != nil {
|
||||
logf("[mesh-config] reading the operator's answers: %v", err)
|
||||
@@ -284,6 +304,25 @@ func hearing(c *Config) {
|
||||
for _, s := range said {
|
||||
logf("[mesh-config] %s", s)
|
||||
}
|
||||
if err := c.JudgeHeldWindows(); err != nil {
|
||||
logf("[mesh-config] the held windows cannot be judged: %v", err)
|
||||
}
|
||||
}
|
||||
decide()
|
||||
for {
|
||||
at, ok := c.NextDeadline()
|
||||
if !ok {
|
||||
<-rearm
|
||||
continue
|
||||
}
|
||||
// Not sooner than the floor: a deadline that passed and was not settled (an ask the router refused) is tried again, not spun on.
|
||||
timer := time.NewTimer(max(time.Until(at), deadlineFloor()))
|
||||
select {
|
||||
case <-timer.C:
|
||||
decide()
|
||||
case <-rearm:
|
||||
timer.Stop()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -379,13 +418,10 @@ func Tools(c *Config) []stdio.Tool {
|
||||
"who and why. A trusted value is set only on the operator's Approve: " +
|
||||
"propose asks the operator, under mesh-config's own name on a channel that proves who answered, and sets " +
|
||||
"the value on Approve if what it was shown against still stands; proposals lists them, or one with " +
|
||||
"proposal. A proposal naming the open pull request that introduces its key (pull) is judged against what " +
|
||||
"that pull request declares and asked of the operator, and on Approve is held until the pull request " +
|
||||
"merges, then set; list shows the held values. pull-declares and held are the controller's. list and " +
|
||||
"history are narrowed by module, node and key.",
|
||||
"proposal. list and history are narrowed by module, node and key.",
|
||||
Input: map[string]any{"type": "object", "properties": map[string]any{
|
||||
"action": map[string]any{"type": "string",
|
||||
"enum": []string{"list", "set", "clear", "history", "propose", "proposals", "pull-declares", "held"},
|
||||
"enum": []string{"list", "set", "clear", "history", "propose", "proposals"},
|
||||
"description": "list when not given"},
|
||||
"key": map[string]any{"type": "string", "description": "the config key"},
|
||||
"value": map[string]any{"description": "for set: the value, of the key's declared type"},
|
||||
@@ -398,14 +434,6 @@ func Tools(c *Config) []stdio.Tool {
|
||||
"clear": map[string]any{"type": "boolean",
|
||||
"description": "for propose: propose clearing the value at its level instead of setting one"},
|
||||
"proposal": map[string]any{"type": "string", "description": "for proposals: one proposal's id, read whole"},
|
||||
"pull": map[string]any{"type": "string", "description": "owner/repository#number: for propose, the open " +
|
||||
"pull request that introduces the key, so the value is judged against what it declares and held until " +
|
||||
"it merges (a module's own value on one node); for pull-declares, the pull request declared for"},
|
||||
"head": map[string]any{"type": "string", "description": "for pull-declares: the commit the declarations are read at"},
|
||||
"declares": map[string]any{"type": "object", "description": "for pull-declares (the controller's): the " +
|
||||
"config declarations the head adds or changes against its base, by module and key"},
|
||||
"pulls": map[string]any{"type": "array", "description": "for held (the controller's): the pull requests " +
|
||||
"asked about, each {pull, head}", "items": map[string]any{"type": "object"}},
|
||||
}},
|
||||
Run: func(args map[string]any) (any, error) {
|
||||
call, err := callOf(args)
|
||||
@@ -421,8 +449,7 @@ func Tools(c *Config) []stdio.Tool {
|
||||
// callOf reads a call's arguments strictly: an argument it does not know is refused, not ignored.
|
||||
func callOf(args map[string]any) (Call, error) {
|
||||
known := map[string]bool{"action": true, "key": true, "value": true, "module": true, "node": true, "why": true,
|
||||
"by": true, "limit": true, "clear": true, "proposal": true, "pull": true, "head": true, "declares": true,
|
||||
"pulls": true}
|
||||
"by": true, "limit": true, "clear": true, "proposal": true}
|
||||
var unknown []string
|
||||
for k := range args {
|
||||
if !known[k] {
|
||||
@@ -431,7 +458,7 @@ func callOf(args map[string]any) (Call, error) {
|
||||
}
|
||||
if len(unknown) > 0 {
|
||||
return Call{}, fmt.Errorf("config takes no %s; it takes action, key, value, module, node, why, by, limit, "+
|
||||
"clear, proposal, pull, head, declares and pulls",
|
||||
"clear and proposal",
|
||||
strings.Join(unknown, ", "))
|
||||
}
|
||||
str := func(k string) (string, error) {
|
||||
@@ -448,8 +475,7 @@ func callOf(args map[string]any) (Call, error) {
|
||||
var call Call
|
||||
var problems []error
|
||||
for k, into := range map[string]*string{"action": &call.Action, "key": &call.Key, "module": &call.Module,
|
||||
"node": &call.Node, "why": &call.Why, "by": &call.By, "proposal": &call.Proposal, "pull": &call.Pull,
|
||||
"head": &call.Head} {
|
||||
"node": &call.Node, "why": &call.Why, "by": &call.By, "proposal": &call.Proposal} {
|
||||
s, err := str(k)
|
||||
problems = append(problems, err)
|
||||
*into = s
|
||||
@@ -468,19 +494,6 @@ func callOf(args map[string]any) (Call, error) {
|
||||
}
|
||||
call.Clear = b
|
||||
}
|
||||
if v, ok := args["declares"]; ok && v != nil {
|
||||
m, ok := v.(map[string]any)
|
||||
if !ok {
|
||||
problems = append(problems, errors.New("declares is an object: a module to its keys' declarations"))
|
||||
}
|
||||
call.Declares = m
|
||||
}
|
||||
if v, ok := args["pulls"]; ok && v != nil {
|
||||
raw, _ := json.Marshal(v)
|
||||
if err := json.Unmarshal(raw, &call.Pulls); err != nil {
|
||||
problems = append(problems, errors.New("pulls is a list of {pull, head}"))
|
||||
}
|
||||
}
|
||||
if v, ok := args["value"]; ok {
|
||||
// The value as given, a JSON value: a number for a number, never its text; the validation judges it.
|
||||
call.Value = v
|
||||
|
||||
@@ -50,10 +50,10 @@ func TestEveryStateAndActHasItsTool(t *testing.T) {
|
||||
}
|
||||
via := map[string]string{
|
||||
ValuesState: "config list", ResolvedState: "config list", PredefinedState: "config list",
|
||||
ProposalsState: "config proposals", PullsState: "config list", KeptState: "config list", HistoryLog: "config history",
|
||||
ProposalsState: "config proposals", HistoryLog: "config history",
|
||||
DeclaredState: DeclaredTool,
|
||||
AssignmentsRead: "config list", WantsRead: "config list", IndexerPredefinedRead: "config list", CandidatesRead: ResyncTool,
|
||||
"a resolution": ResyncTool, "the indexer's candidates taken": ResyncTool,
|
||||
AssignmentsRead: "config list", WantsRead: "config list", IndexerPredefinedRead: "config list",
|
||||
"a resolution": ResyncTool,
|
||||
// design 32: each new state with the tool that reads it, each act with the tool that starts it.
|
||||
RenamesState: RenamesTool, RetiringState: RetiringTool, FindingsState: "config_renames and config_retiring",
|
||||
"a move refused": RenameAgainTool, "a window dropped": RenameCloseTool, "an ask again": RetiringTool,
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package main
|
||||
|
||||
// What mesh-config reads of the delivery owners' state, through their tools and views families (novox/hq ADR 0615 §1,
|
||||
// §4 to §6, ADR 0596): never a bucket of theirs. For each of the four states it follows (the indexer's predefined and
|
||||
// config-candidates, the assigner's assignments and wants) it asks the owner's read tool for the whole
|
||||
// §4 to §6, ADR 0596): never a bucket of theirs. For each of the three states it follows (the indexer's predefined, the
|
||||
// assigner's assignments and wants) it asks the owner's read tool for the whole
|
||||
// state at its start, keeps the picture from the owner's `views.<state>` events, and asks again only on a gap. Started
|
||||
// while an owner is down it keeps asking, and past two minutes writes a finding naming the owner and the tool.
|
||||
//
|
||||
@@ -28,7 +28,6 @@ import (
|
||||
// The tools of the owners that mesh-config asks, by the state they read.
|
||||
const (
|
||||
IndexerPredefTool = "indexer_read_predefined"
|
||||
IndexerCandidatesTool = "indexer_read_config_candidates"
|
||||
AssignerAssignmentsTool = "assigner_read_assignments"
|
||||
AssignerWantsTool = "assigner_read_wants"
|
||||
|
||||
@@ -40,7 +39,6 @@ const (
|
||||
// state and its tool.
|
||||
var followed = []struct{ Read, Owner, State, Tool string }{
|
||||
{IndexerPredefinedRead, indexerModule, catalog.StatePredefined, IndexerPredefTool},
|
||||
{CandidatesRead, indexerModule, catalog.StateConfigCandidates, IndexerCandidatesTool},
|
||||
{AssignmentsRead, assignerModule, "assignments", AssignerAssignmentsTool},
|
||||
{WantsRead, assignerModule, "wants", AssignerWantsTool},
|
||||
}
|
||||
@@ -189,7 +187,7 @@ func viewTools(c *Config, o func() *ownerViews) []stdio.Tool {
|
||||
return []stdio.Tool{
|
||||
{Name: FirstViewsTool,
|
||||
Description: "mesh-config's first views of the delivery owners' state: for each of the indexer's " +
|
||||
"predefined and config-candidates, the assigner's assignments and wants and the catalog half's registrations and " +
|
||||
"predefined, the assigner's assignments and wants and the catalog half's registrations and " +
|
||||
"each watched branch's revision, the tool it asks, the revision its " +
|
||||
"picture holds, when it asked and was answered, how many asks (and by hand), and whether its condition is raised.",
|
||||
Run: func(map[string]any) (any, error) {
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
stdio "git.novox.be/novox/mesh-sdk/go"
|
||||
)
|
||||
|
||||
// The module's own states are read from a copy each one's watch keeps, never key by key (novox/hq ADR 0580 §1, issue 1982):
|
||||
// a resolution reads every key of values, resolved, declared and the rest, and a get per key for each of them, once per
|
||||
// pass and once per delivery event, was about 4,000 requests to the bus every time. A state is watched on its first read, the
|
||||
// watch hands every current value over before the read is answered, and this process's own writes are taken into the copy
|
||||
// as they are made, so a read right after a write sees it.
|
||||
|
||||
// ownStates are the states this module keeps and reads through its store (its manifest's `state`, but the findings, which its
|
||||
// held writer alone reads and writes): the ones read from the copy.
|
||||
var ownStates = map[string]bool{
|
||||
ValuesState: true, ResolvedState: true, PredefinedState: true, ProposalsState: true, DeclaredState: true, RenamesState: true, RetiringState: true,
|
||||
}
|
||||
|
||||
// ownCopy is the copy of the module's own states over a bus: watch hands every current value of a state and then each change,
|
||||
// get reads one key, put and del write one.
|
||||
type ownCopy struct {
|
||||
copy *stdio.WatchedStates
|
||||
put func(state, key string, value any) (uint64, error)
|
||||
del func(state, key string) error
|
||||
// changed, when not nil, is told of each write or removal after it is in the copy: what waits on a deadline is judged again.
|
||||
changed func(state string)
|
||||
}
|
||||
|
||||
// newOwnCopy is the copy over this bundle's runtime.
|
||||
func newOwnCopy() *ownCopy {
|
||||
return &ownCopy{copy: stdio.NewWatchedStates(),
|
||||
put: func(state, key string, value any) (uint64, error) { return stdio.State(state).Put(key, value) },
|
||||
del: func(state, key string) error { return stdio.State(state).Delete(key) }}
|
||||
}
|
||||
|
||||
func (o *ownCopy) get(state, key string) (*Entry, error) {
|
||||
raw, ok, err := o.copy.Get(state, key)
|
||||
if err != nil || !ok {
|
||||
return nil, err
|
||||
}
|
||||
return &Entry{Key: key, Value: raw}, nil
|
||||
}
|
||||
|
||||
func (o *ownCopy) keys(state string) ([]string, error) { return o.copy.Keys(state) }
|
||||
|
||||
// write puts a key and takes it into the copy at the revision the bus answered.
|
||||
func (o *ownCopy) write(state, key string, value any) error {
|
||||
rev, err := o.put(state, key, value)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
raw, err := json.Marshal(value)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = o.copy.Wrote(state, key, raw, rev)
|
||||
o.told(state)
|
||||
return err
|
||||
}
|
||||
|
||||
func (o *ownCopy) told(state string) {
|
||||
if o.changed != nil {
|
||||
o.changed(state)
|
||||
}
|
||||
}
|
||||
|
||||
// remove deletes a key and takes that into the copy.
|
||||
func (o *ownCopy) remove(state, key string) error {
|
||||
if err := o.del(state, key); err != nil {
|
||||
return err
|
||||
}
|
||||
err := o.copy.Removed(state, key)
|
||||
o.told(state)
|
||||
return err
|
||||
}
|
||||
@@ -98,11 +98,9 @@ type Proposal struct {
|
||||
HadValue bool `json:"had-value"`
|
||||
WasDigest string `json:"was-digest"`
|
||||
Trusted bool `json:"trusted,omitempty"`
|
||||
// Pull is the open pull request the value is held for, when it is (novox/hq ADR 0502): the value is then not set
|
||||
// on Approve but kept apart until the pull request merges, and DeclDigest is the digest of the pull request's
|
||||
// declaration of the key it was judged and shown against.
|
||||
Pull string `json:"pull,omitempty"`
|
||||
DeclDigest string `json:"declaration,omitempty"`
|
||||
// Pull is read only: a proposal an earlier mesh-config kept for a pull request (ADR 0502, superseded by ADR 0681 §5).
|
||||
// It is never set on an Approve now, and judged not to be one this module makes (problem).
|
||||
Pull string `json:"pull,omitempty"`
|
||||
// Reaches is each module on each node whose value it moves, as it was judged when proposed.
|
||||
Reaches []string `json:"reaches"`
|
||||
// From is who proposed it, as the bus proved the caller; Why is theirs; At is when.
|
||||
@@ -175,9 +173,6 @@ func (p Proposal) Fingerprint() string {
|
||||
// about is what its ask is about: the value's place, so a newer proposal for it replaces an older one still open
|
||||
// (ADR 0277 §10, at a level, a key).
|
||||
func (p Proposal) about() string {
|
||||
if p.Pull != "" {
|
||||
return "config.held." + pullKey(p.Pull) + "." + p.Level.ValueKey(p.Key)
|
||||
}
|
||||
return "config." + p.Level.ValueKey(p.Key)
|
||||
}
|
||||
|
||||
@@ -188,9 +183,6 @@ func (p Proposal) Act(option string) occ.Act {
|
||||
"level": p.Level.Kind, "node": p.Level.Node, "module": p.Level.Module, "key": p.Key, "value": p.Digest,
|
||||
"was": p.WasDigest, "had-value": strconv.FormatBool(p.HadValue), "from": p.From,
|
||||
"at": p.At.UTC().Format(time.RFC3339Nano)}
|
||||
if p.Pull != "" {
|
||||
act["pull"], act["declaration"] = p.Pull, p.DeclDigest
|
||||
}
|
||||
return act
|
||||
}
|
||||
|
||||
@@ -270,16 +262,6 @@ func (p Proposal) summary() string {
|
||||
if p.Trusted {
|
||||
trusted = ", a trusted value,"
|
||||
}
|
||||
if p.Pull != "" {
|
||||
s := fmt.Sprintf("Holds %s for %s on %s for %s; approved, it takes effect when that pull request merges, "+
|
||||
"and is dropped if it closes unmerged. Nothing reaches a machine before the merge.", p.Key, p.Level.Module,
|
||||
p.Level.Node, p.Pull)
|
||||
if len([]rune(s)) > occ.SummaryLength {
|
||||
s = fmt.Sprintf("Holds %s for %s on %s for %s until it merges; dropped if it closes unmerged.", p.Key,
|
||||
p.Level.Module, p.Level.Node, p.Pull)
|
||||
}
|
||||
return s
|
||||
}
|
||||
reach := p.reaches()
|
||||
s := fmt.Sprintf("%s%s at %s; approved, it takes effect at once on every node it reaches: %s.", verb, trusted,
|
||||
p.Level, reach)
|
||||
@@ -305,9 +287,6 @@ func (p Proposal) headline() string {
|
||||
if p.Action == "clear" {
|
||||
verb = "Clear"
|
||||
}
|
||||
if p.Pull != "" {
|
||||
verb = "Hold"
|
||||
}
|
||||
where := "for the whole mesh"
|
||||
switch p.Level.Kind {
|
||||
case LevelNode:
|
||||
@@ -359,9 +338,6 @@ func (p Proposal) Ask() oc.Ask {
|
||||
a.WholeDetails = w
|
||||
}
|
||||
does := "the value is set at once, and every node it reaches takes it"
|
||||
if p.Pull != "" {
|
||||
does = "the value is held for " + p.Pull + ": it is set when that pull request merges, and dropped if it closes unmerged"
|
||||
}
|
||||
if p.Action == "clear" {
|
||||
does = "the value is cleared at once, and every node it reaches takes what is below it"
|
||||
}
|
||||
@@ -392,8 +368,8 @@ func (p Proposal) problem() string {
|
||||
return "its value is not the value its digest names: it changed after it was asked"
|
||||
case p.Action == "clear" && (len(p.Value) != 0 || p.Digest != valueDigest(nil, false)):
|
||||
return "a clear carries no value"
|
||||
case p.Pull != "" && (p.Action != "set" || p.Level.Kind != LevelModule || p.DeclDigest == ""):
|
||||
return "a value held for a pull request is a set of a module's own value, judged against its declaration"
|
||||
case p.Pull != "":
|
||||
return "it was held for a pull request, which the mesh knows no more: nothing is set on it"
|
||||
case valueDigest(p.Was, p.HadValue) != p.WasDigest:
|
||||
return "the value it was shown against is not the one its digest names: it changed after it was asked"
|
||||
}
|
||||
@@ -559,39 +535,16 @@ func (c *Config) propose(call Call, action string) (Proposal, oc.Ask, []string,
|
||||
// **Judged as a set judges it, before anyone is asked** (ADR 0277 §4): no ask for a value the verb would refuse.
|
||||
var r rule
|
||||
var trusted bool
|
||||
var declDigest string
|
||||
vk := level.ValueKey(call.Key)
|
||||
was, had := s.Values[vk]
|
||||
if call.Pull != "" {
|
||||
// **A value held for the open pull request that introduces its key** (novox/hq ADR 0502): judged against what
|
||||
// the pull request declares at its head, as the controller told it, and asked of the operator whatever the
|
||||
// key's trust.
|
||||
if action != "set" {
|
||||
return fail("a clear is not held for a pull request: only a value to set is")
|
||||
}
|
||||
if _, _, _, err := parsePull(call.Pull); err != nil {
|
||||
return fail("%v", err)
|
||||
}
|
||||
call.Pull = strings.TrimSpace(call.Pull)
|
||||
var herr error
|
||||
r, _, declDigest, herr = heldRule(s, call, level)
|
||||
if herr != nil {
|
||||
return fail("%v", herr)
|
||||
}
|
||||
trusted = false
|
||||
k, kept := s.Kept[keptKey(call.Pull, level.Node, level.Module, call.Key)]
|
||||
was, had = Value{Value: heldValue(k)}, kept
|
||||
} else {
|
||||
var derr error
|
||||
r, trusted, derr = c.declared(s, level, call.Key)
|
||||
if derr != nil {
|
||||
return fail("%v", derr)
|
||||
}
|
||||
r, trusted, derr := c.declared(s, level, call.Key)
|
||||
if derr != nil {
|
||||
return fail("%v", derr)
|
||||
}
|
||||
now := c.now()
|
||||
prop := Proposal{ID: newProposalID(), Action: action, Level: level, Key: call.Key, HadValue: had,
|
||||
WasDigest: valueDigest(was.Value, had), Trusted: trusted, From: call.Caller, Why: why, At: now,
|
||||
State: ProposalWaiting, Digest: valueDigest(nil, false), Pull: call.Pull, DeclDigest: declDigest}
|
||||
State: ProposalWaiting, Digest: valueDigest(nil, false)}
|
||||
if had {
|
||||
prop.Was = compact(was.Value)
|
||||
}
|
||||
@@ -618,19 +571,15 @@ func (c *Config) propose(call Call, action string) (Proposal, oc.Ask, []string,
|
||||
if had && string(compact(was.Value)) == string(prop.Value) {
|
||||
return fail("%s at %s already holds exactly %s", call.Key, level, prop.Value)
|
||||
}
|
||||
if call.Pull == "" {
|
||||
after.Values[vk] = Value{Value: prop.Value}
|
||||
}
|
||||
after.Values[vk] = Value{Value: prop.Value}
|
||||
case "clear":
|
||||
if !had {
|
||||
return fail("nothing is set for %s at %s, so there is nothing to clear", call.Key, level)
|
||||
}
|
||||
delete(after.Values, vk)
|
||||
}
|
||||
if call.Pull == "" {
|
||||
for _, m := range diff(Resolve(s), Resolve(after)) {
|
||||
prop.Reaches = append(prop.Reaches, m.Module+" on "+m.Node)
|
||||
}
|
||||
for _, m := range diff(Resolve(s), Resolve(after)) {
|
||||
prop.Reaches = append(prop.Reaches, m.Module+" on "+m.Node)
|
||||
}
|
||||
if prop.Reaches == nil {
|
||||
prop.Reaches = []string{}
|
||||
@@ -830,14 +779,6 @@ func (c *Config) decide(p Proposal, w oc.Warrant) string {
|
||||
return fmt.Sprintf("proposal %s: approved by the operator, and not set: the decision could not be kept (%v); "+
|
||||
"it is read again on the next look", p.ID, err)
|
||||
}
|
||||
if p.Pull != "" {
|
||||
// **A held value is kept apart, never set on Approve** (ADR 0502 clause 3).
|
||||
said, err := c.keep(p)
|
||||
if err != nil {
|
||||
return keep(ProposalRefused, "approved by the operator, and refused on Approve, so nothing is held: "+err.Error())
|
||||
}
|
||||
return keep(ProposalApproved, "approved by the operator and held: "+said)
|
||||
}
|
||||
call := Call{Action: p.Action, Key: p.Key, Module: p.Level.Module, Node: p.Level.Node, Why: p.Why, Caller: p.From,
|
||||
approval: &approval{Proposal: p.ID, By: "the operator", Says: p.Approved, Was: p.Was, HadValue: p.HadValue,
|
||||
WasDigest: p.WasDigest}}
|
||||
@@ -910,19 +851,63 @@ func (c *Config) HeardEnd(w oc.Warrant) []string {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
defer c.tellEnded(w.Ask, endedWord{Outcome: w.Outcome, Words: w.Words})
|
||||
// A retirement's ask ended with no choice is taken from the router's record the same way (retiring.go): the router's word
|
||||
// is the only thing that moves it, and no timer looks.
|
||||
said := c.decideRetirements()
|
||||
e, err := c.Store.Get(ProposalsState, w.Ask)
|
||||
if err != nil || e == nil {
|
||||
return nil
|
||||
return said
|
||||
}
|
||||
var p Proposal
|
||||
if json.Unmarshal(e.Value, &p) != nil || p.ID != w.Ask || p.State != ProposalWaiting {
|
||||
return nil
|
||||
return said
|
||||
}
|
||||
why := "the ask " + string(w.Outcome)
|
||||
if w.Words != "" {
|
||||
why += " (" + w.Words + ")"
|
||||
}
|
||||
return []string{c.end(p, why+"; nothing was set")}
|
||||
return append(said, c.end(p, why+"; nothing was set"))
|
||||
}
|
||||
|
||||
// NextDeadline is when a waiting proposal or retirement is next judged for want of an answer: a proposal with no record
|
||||
// at the router ends an hour after its ask lapses (Decide), and a retirement left unanswered is asked once more after
|
||||
// HeldFinding, and an open rename window's held finding comes after HeldFinding too (lead answer A-490l). The earliest of
|
||||
// them; false when nothing waits on a time.
|
||||
func (c *Config) NextDeadline() (time.Time, bool) {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
var at time.Time
|
||||
earlier := func(t time.Time) {
|
||||
if at.IsZero() || t.Before(at) {
|
||||
at = t
|
||||
}
|
||||
}
|
||||
if all, err := c.proposals(); err == nil {
|
||||
for _, p := range all {
|
||||
if p.State == ProposalWaiting {
|
||||
earlier(p.At.Add(ProposalLasts + time.Hour))
|
||||
}
|
||||
}
|
||||
}
|
||||
if held, err := c.readRetiring(); err == nil {
|
||||
for _, e := range held {
|
||||
if e.State == RetireUnanswered && !e.AskedAgain {
|
||||
earlier(e.Asked.Add(HeldFinding))
|
||||
}
|
||||
}
|
||||
}
|
||||
// An open rename window reaches its held finding after HeldFinding (renames.go): its deadline, while it lies ahead.
|
||||
if renames, err := c.readRenames(); err == nil {
|
||||
now := c.now()
|
||||
for _, r := range renames {
|
||||
for _, node := range r.Open() {
|
||||
if d := r.Nodes[node].Since.Add(HeldFinding); d.After(now) {
|
||||
earlier(d)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return at, !at.IsZero()
|
||||
}
|
||||
|
||||
// ---- the list ----------------------------------------------------------------------------------------------
|
||||
@@ -940,8 +925,6 @@ type ProposalRow struct {
|
||||
Expires time.Time `json:"expires"`
|
||||
Fingerprint string `json:"fingerprint"`
|
||||
Approved string `json:"approved,omitempty"`
|
||||
// Pull is the pull request a value is held for, when it is.
|
||||
Pull string `json:"pull,omitempty"`
|
||||
// What one proposal read whole says besides.
|
||||
Value json.RawMessage `json:"value,omitempty"`
|
||||
Was json.RawMessage `json:"was,omitempty"`
|
||||
@@ -970,8 +953,7 @@ func (c *Config) Proposals(call Call) ([]ProposalRow, error) {
|
||||
continue
|
||||
}
|
||||
row := ProposalRow{ID: p.ID, Action: p.Action, Level: p.Level, Key: p.Key, State: p.State, Outcome: p.Outcome,
|
||||
From: p.From, At: p.At, Expires: p.At.Add(ProposalLasts), Fingerprint: p.Fingerprint(), Approved: p.Approved,
|
||||
Pull: p.Pull}
|
||||
From: p.From, At: p.At, Expires: p.At.Add(ProposalLasts), Fingerprint: p.Fingerprint(), Approved: p.Approved}
|
||||
if p.State == ProposalWaiting && c.now().After(row.Expires) {
|
||||
row.State = "expired, not yet heard"
|
||||
}
|
||||
|
||||
@@ -590,3 +590,42 @@ func TestAnOwnersListOfNamesWithDigitsIsProposed(t *testing.T) {
|
||||
t.Fatalf("the owners were not put to the operator: %+v, asked on %v", a, r.subjects)
|
||||
}
|
||||
}
|
||||
|
||||
// **An approved proposal records a hand act that names no pull request** (ADR 0681 clause 1: nothing it records as a hand act is a
|
||||
// held value).
|
||||
func TestAnApprovedProposalsHandActNamesNoPullRequest(t *testing.T) {
|
||||
c, m, r := aProposingMesh(t)
|
||||
a := propose(t, c, Call{Key: "font-size", Value: 12.0, Node: "g14"})
|
||||
r.choose(t, a.Proposal, OptionApprove, nil)
|
||||
m.handActs = nil
|
||||
if said, err := c.Decide(); err != nil || len(said) == 0 {
|
||||
t.Fatalf("decide: %v, %v", said, err)
|
||||
}
|
||||
if len(m.handActs) == 0 {
|
||||
t.Fatal("the approval recorded no hand act")
|
||||
}
|
||||
for _, act := range m.handActs {
|
||||
if strings.Contains(strings.ToLower(act), "pull") || strings.Contains(act, "#") {
|
||||
t.Errorf("a hand act names a pull request: %q", act)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// A stored proposal that names a pull request is refused on its approval, saying why, and nothing is set (lead answer A-490l).
|
||||
func TestAStoredProposalNamingAPullRequestIsRefusedOnApproval(t *testing.T) {
|
||||
c, m, r := aProposingMesh(t)
|
||||
a := propose(t, c, Call{Key: "font-size", Value: 12.0, Node: "g14"})
|
||||
p := storedProposal(t, m, a.Proposal)
|
||||
p.Pull = "novox/mesh-catalog#7"
|
||||
if err := m.Put(ProposalsState, p.ID, p); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
r.choose(t, a.Proposal, OptionApprove, nil)
|
||||
said, _ := c.Decide()
|
||||
if got := storedProposal(t, m, a.Proposal); got.State == ProposalApproved || len(said) == 0 || !strings.Contains(strings.Join(said, " "), "pull request") {
|
||||
t.Errorf("said %v, proposal %+v", said, got)
|
||||
}
|
||||
if _, ok := storedValue(t, m, "node.g14.font-size"); ok {
|
||||
t.Error("a value was set on a proposal that names a pull request")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,54 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// toolForge.Pull reads the seat git's answer of the verb pull (design 47 G §654), bare or in the wrappings the runtime puts
|
||||
// around an answer; pullEventOf reads the seat's pull-merged and pull-closed bodies.
|
||||
func TestToolForgePullReadsTheSeatGitAnswersAndEventsAreRead(t *testing.T) {
|
||||
var asked struct {
|
||||
key string
|
||||
body map[string]any
|
||||
}
|
||||
for _, tc := range []struct {
|
||||
answer string
|
||||
open, merged bool
|
||||
head string
|
||||
}{
|
||||
{`{"pull":{"repository":"novox/mesh-catalog","number":435,"state":"merged","head":"7b5f7f84acb73aaf8189200548ff69fd9aac04b2"}}`, false, true, "7b5f7f84acb73aaf8189200548ff69fd9aac04b2"},
|
||||
{`{"pull":{"repository":"novox/mesh-catalog","number":434,"state":"open","head":"c948f3a61c74257e6e3147effb396d9d6434f491"}}`, true, false, "c948f3a61c74257e6e3147effb396d9d6434f491"},
|
||||
{`{"pull":{"state":"closed","head":"c948f3a61c74257e6e3147effb396d9d6434f491"}}`, false, false, "c948f3a61c74257e6e3147effb396d9d6434f491"},
|
||||
} {
|
||||
raw := json.RawMessage(tc.answer)
|
||||
content, _ := json.Marshal(map[string]any{"content": []map[string]any{{"type": "text", "text": tc.answer}}})
|
||||
ok, _ := json.Marshal(map[string]any{"ok": true, "output": "", "answer": raw})
|
||||
for name, answer := range map[string]json.RawMessage{"bare": raw, "content list": content, "ok answer": ok} {
|
||||
f := toolForge{ask: func(key string, body any) (json.RawMessage, error) {
|
||||
asked.key, asked.body = key, body.(map[string]any)
|
||||
return answer, nil
|
||||
}}
|
||||
open, merged, head, err := f.Pull("novox", "mesh-catalog", 434)
|
||||
if err != nil || open != tc.open || merged != tc.merged || head != tc.head {
|
||||
t.Errorf("%s (%s): open %v merged %v head %q, err %v", tc.answer, name, open, merged, head, err)
|
||||
}
|
||||
if asked.key != "seat:git.pull" || asked.body["repository"] != "novox/mesh-catalog" || asked.body["number"] != 434 {
|
||||
t.Errorf("asked %+v", asked)
|
||||
}
|
||||
}
|
||||
}
|
||||
failed := toolForge{ask: func(string, any) (json.RawMessage, error) {
|
||||
return json.RawMessage(`{"error":"no such pull request","error-kind":"not-found"}`), nil
|
||||
}}
|
||||
if _, _, _, err := failed.Pull("novox", "mesh-catalog", 9); err == nil {
|
||||
t.Error("an error answer was read as a pull request")
|
||||
}
|
||||
p, ok := pullEventOf([]byte(`{"repository":"novox/mesh-catalog","number":435,"branch":"fix/a","head":"7b5f7f84acb73aaf8189200548ff69fd9aac04b2","base":"main"}`))
|
||||
if !ok || p.Owner != "novox" || p.Repo != "mesh-catalog" || p.Number != 435 || p.HeadSHA != "7b5f7f84acb73aaf8189200548ff69fd9aac04b2" {
|
||||
t.Errorf("%+v %v", p, ok)
|
||||
}
|
||||
if _, ok := pullEventOf([]byte(`{"repository":"norepo","number":1}`)); ok {
|
||||
t.Error("a repository with no owner was read")
|
||||
}
|
||||
}
|
||||
@@ -1,644 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.novox.be/novox/mesh-contracts/go/clients/meshconfigclient"
|
||||
)
|
||||
|
||||
// A config value held for the open pull request that introduces its key (novox/hq ADR 0502, issue 1826).
|
||||
//
|
||||
// A key a pull request adds is declared by no registered manifest until it merges, so a value for it, which the
|
||||
// pull request's composed check needs, was refused. The controller reads each pull request's manifests from the
|
||||
// forge at its head and tells this module the config declarations the head adds or changes (PullDeclares); mesh-config
|
||||
// reads no manifest itself (ADR 0495 §1). A proposal that names the pull request is judged against those, asked of the
|
||||
// operator whatever the key's trust, and on Approve kept apart (KeptState) from the values it resolves. The
|
||||
// controller asks for the approved held values of a check's pull requests (Held) and hands them to the composed
|
||||
// check. At the merge the value is written as the module's own value on its node (PullMerged), where it resolves
|
||||
// once the trunk declares the key; a pull request closed unmerged drops it (PullClosed). Nothing reaches a machine
|
||||
// before the merge.
|
||||
|
||||
// The states of held values, and the actions their changes carry in the history and in `config-changed`.
|
||||
const (
|
||||
PullsState = "pulls"
|
||||
KeptState = "kept"
|
||||
|
||||
ActionHold = "hold"
|
||||
ActionDrop = "drop"
|
||||
)
|
||||
|
||||
// pullName is a pull request as it is named: owner/repository#number.
|
||||
var pullName = regexp.MustCompile(`^([A-Za-z0-9_.-]+)/([A-Za-z0-9_.-]+)#([0-9]+)$`)
|
||||
|
||||
func parsePull(s string) (owner, repo string, number int, err error) {
|
||||
m := pullName.FindStringSubmatch(strings.TrimSpace(s))
|
||||
if m == nil {
|
||||
return "", "", 0, fmt.Errorf("%q is not a pull request: name it owner/repository#number", s)
|
||||
}
|
||||
n, _ := strconv.Atoi(m[3])
|
||||
if n <= 0 {
|
||||
return "", "", 0, fmt.Errorf("%q is not a pull request: its number is above zero", s)
|
||||
}
|
||||
return m[1], m[2], n, nil
|
||||
}
|
||||
|
||||
// pullKey is a pull request's key in the states.
|
||||
func pullKey(pull string) string {
|
||||
return strings.NewReplacer("/", ".", "#", ".").Replace(strings.TrimSpace(pull))
|
||||
}
|
||||
|
||||
// PullDecl is what the controller told: the config declarations a pull request's head adds or changes, by module.
|
||||
type PullDecl struct {
|
||||
Pull string `json:"pull"`
|
||||
Head string `json:"head"`
|
||||
Declares map[string]map[string]Declaration `json:"declares"`
|
||||
At time.Time `json:"at"`
|
||||
}
|
||||
|
||||
// KeptValue is an approved held value, kept apart from the values mesh-config resolves, in the layout of the seat's state
|
||||
// `kept`, which the checker and the assigner's composition check read (novox/hq ADR 0565 §5, ADR 0634 §4).
|
||||
type KeptValue = meshconfigclient.Kept
|
||||
|
||||
// keptKey is a held value's key in `kept`: the pull request's key form (pullKey), the node, the module and the config
|
||||
// key, joined by dots.
|
||||
func keptKey(pull, node, module, key string) string {
|
||||
k, _ := meshconfigclient.HeldValueKey(pull, node, module, key) // a held value of no pull request has no key, and is found by none
|
||||
return k
|
||||
}
|
||||
|
||||
// heldValue is the value held, as the operator approved it, in its JSON form.
|
||||
func heldValue(k KeptValue) json.RawMessage {
|
||||
raw, _ := meshconfigclient.ValueOf(k) // a value that was read from JSON writes as JSON
|
||||
return raw
|
||||
}
|
||||
|
||||
func heldAt(t time.Time) *time.Time { return &t }
|
||||
|
||||
func keptStateKey(k KeptValue) string { return keptKey(k.Pull, k.Node, k.Module, k.Key) }
|
||||
|
||||
func keptLevel(k KeptValue) Level { return Level{Kind: LevelModule, Node: k.Node, Module: k.Module} }
|
||||
|
||||
// declDigest is the digest of what a declaration holds a value to: a held value stays only while it is the same.
|
||||
func declDigest(d Declaration) string {
|
||||
d.Description, d.Why, d.Kind, d.Runtime = "", "", "", false
|
||||
if !d.HasDefault() {
|
||||
d.Default = nil
|
||||
} else {
|
||||
d.Default = compact(d.Default)
|
||||
}
|
||||
raw, _ := json.Marshal(d)
|
||||
sum := sha256.Sum256(raw)
|
||||
return "sha256:" + hex.EncodeToString(sum[:])
|
||||
}
|
||||
|
||||
// PullForge is the forge as this module asks it of a pull request: its state, and whether it merged.
|
||||
type PullForge interface {
|
||||
Pull(owner, repo string, number int) (open, merged bool, head string, err error)
|
||||
}
|
||||
|
||||
// pulls reads what is kept for pull requests, and the held values.
|
||||
func (c *Config) readPulls(s *Sources) error {
|
||||
s.Pulls, s.Kept = map[string]PullDecl{}, map[string]KeptValue{}
|
||||
if err := c.each(PullsState, func(key string, raw []byte) error {
|
||||
var p PullDecl
|
||||
if err := json.Unmarshal(raw, &p); err != nil || p.Pull == "" {
|
||||
return nil // not one this module wrote
|
||||
}
|
||||
s.Pulls[pullKey(p.Pull)] = p
|
||||
return nil
|
||||
}); err != nil {
|
||||
return fmt.Errorf("the pull requests' declarations cannot be read: %w", err)
|
||||
}
|
||||
if err := c.each(KeptState, func(key string, raw []byte) error {
|
||||
k, err := meshconfigclient.DecodeKept(key, raw)
|
||||
if err != nil {
|
||||
return nil // not a held value of the seat's layout under its own key
|
||||
}
|
||||
s.Kept[key] = k
|
||||
return nil
|
||||
}); err != nil {
|
||||
return fmt.Errorf("the held values cannot be read: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ---- pull-declares ---------------------------------------------------------------------------------------
|
||||
|
||||
// PullDeclaresAnswer is what pull-declares answers.
|
||||
type PullDeclaresAnswer struct {
|
||||
Said string `json:"said"`
|
||||
Pull string `json:"pull"`
|
||||
Head string `json:"head"`
|
||||
Modules []string `json:"modules"`
|
||||
Dropped []string `json:"dropped,omitempty"`
|
||||
Unsaid []string `json:"unsaid,omitempty"`
|
||||
}
|
||||
|
||||
// PullDeclares is gone: a pull request's config declarations are read from the indexer's config-candidates
|
||||
// (takeCandidates), and the controller that told them is not placed (novox/hq ADR 0565 §4, ADR 0590).
|
||||
func (c *Config) PullDeclares(Call) (*PullDeclaresAnswer, error) {
|
||||
return nil, errors.New("pull-declares is gone: mesh-config reads a pull request's config declarations from the " +
|
||||
"indexer's config-candidates (novox/hq ADR 0565 §4, ADR 0590)")
|
||||
}
|
||||
|
||||
// declarePull keeps what a pull request's head declares and judges again the values held for it, from the indexer's
|
||||
// config candidates. The caller holds the lock.
|
||||
func (c *Config) declarePull(pull, head string, declares map[string]map[string]Declaration) (*PullDeclaresAnswer, error) {
|
||||
if err := c.Store.Put(PullsState, pullKey(pull), PullDecl{Pull: pull, Head: head, Declares: declares,
|
||||
At: c.now()}); err != nil {
|
||||
return nil, fmt.Errorf("the declarations were not kept: %v", err)
|
||||
}
|
||||
a := &PullDeclaresAnswer{Pull: pull, Head: head, Modules: sortedKeys(declares)}
|
||||
var s Sources
|
||||
if err := c.readPulls(&s); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, k := range sortedKeys(s.Kept) {
|
||||
kv := s.Kept[k]
|
||||
if pullKey(kv.Pull) != pullKey(pull) {
|
||||
continue
|
||||
}
|
||||
why := ""
|
||||
d, declared := declares[kv.Module][kv.Key]
|
||||
switch {
|
||||
case !declared:
|
||||
why = "the head no longer adds or changes its declaration"
|
||||
case declDigest(d) != kv.Declaration:
|
||||
why = "the head declares the key otherwise than the value was approved against"
|
||||
default:
|
||||
var v any
|
||||
_ = json.Unmarshal(heldValue(kv), &v)
|
||||
if p := d.rule().check(v); len(p) > 0 {
|
||||
why = "the value no longer passes: " + strings.Join(p, "; ")
|
||||
}
|
||||
}
|
||||
if why != "" {
|
||||
a.Unsaid = append(a.Unsaid, c.dropKept(kv, "the new head "+short(head)+": "+why)...)
|
||||
a.Dropped = append(a.Dropped, fmt.Sprintf("%s for %s on %s: %s", kv.Key, kv.Module, kv.Node, why))
|
||||
continue
|
||||
}
|
||||
if kv.Head != head {
|
||||
kv.Head = head
|
||||
if err := c.Store.Put(KeptState, k, kv); err != nil {
|
||||
a.Unsaid = append(a.Unsaid, "the held value "+k+" was not moved to the new head: "+err.Error())
|
||||
}
|
||||
}
|
||||
}
|
||||
a.Said = fmt.Sprintf("keeps what %s declares at %s for %d module(s)", pull, short(head), len(declares))
|
||||
if len(a.Dropped) > 0 {
|
||||
a.Said += fmt.Sprintf("; %d held value(s) dropped", len(a.Dropped))
|
||||
}
|
||||
return a, nil
|
||||
}
|
||||
|
||||
// heldChange records a hold, a drop or a merge's set of a held value in the history, the hand-act log and
|
||||
// config-changed; what could not be done is answered, not dropped.
|
||||
func (c *Config) heldChange(kv KeptValue, action, why, approved string) []string {
|
||||
level := keptLevel(kv)
|
||||
ch := Change{Action: action, Level: &level, Key: kv.Key, Value: heldValue(kv), By: kv.By, Via: kv.Via, Why: why,
|
||||
Approved: approved, Pull: kv.Pull, Head: kv.Head, At: c.now(), Moved: []Moved{}}
|
||||
unsaid := c.record(level.ValueKey(kv.Key), ch)
|
||||
cause := "a value held for " + kv.Pull
|
||||
what := fmt.Sprintf("config %s %s of %s for %s, held for %s", action, kv.Key, kv.Module, kv.Node, kv.Pull)
|
||||
if action != ActionDrop {
|
||||
what += " (" + string(heldValue(kv)) + ")"
|
||||
}
|
||||
if err := c.Store.HandAct(what, why, cause); err != nil {
|
||||
unsaid = append(unsaid, "the hand-act log does not hold it: "+err.Error())
|
||||
}
|
||||
return unsaid
|
||||
}
|
||||
|
||||
// dropKept removes a held value, saying why.
|
||||
func (c *Config) dropKept(kv KeptValue, why string) []string {
|
||||
if err := c.Store.Delete(KeptState, keptStateKey(kv)); err != nil {
|
||||
return []string{"the held value " + keptStateKey(kv) + " was not dropped: " + err.Error()}
|
||||
}
|
||||
c.logf("[mesh-config] dropped %s of %s on %s held for %s: %s", kv.Key, kv.Module, kv.Node, kv.Pull, why)
|
||||
return c.heldChange(kv, ActionDrop, "dropped, nothing was set: "+why, "")
|
||||
}
|
||||
|
||||
// ---- held ------------------------------------------------------------------------------------------------
|
||||
|
||||
// HeldAnswer is what held answers: the approved held values of the pull requests asked, at the heads asked.
|
||||
type HeldAnswer struct {
|
||||
Held []HeldValue `json:"held"`
|
||||
Skipped []HeldSkipped `json:"skipped"`
|
||||
}
|
||||
|
||||
// HeldValue is one approved held value as it is handed to a composed check and listed.
|
||||
type HeldValue struct {
|
||||
Pull string `json:"pull"`
|
||||
Head string `json:"head"`
|
||||
Node string `json:"node"`
|
||||
Module string `json:"module"`
|
||||
Key string `json:"key"`
|
||||
Value json.RawMessage `json:"value"`
|
||||
Approved string `json:"approved"`
|
||||
By string `json:"by,omitempty"`
|
||||
Why string `json:"why,omitempty"`
|
||||
At time.Time `json:"at"`
|
||||
}
|
||||
|
||||
// HeldSkipped is a pull request asked for whose held values are not answered, and why.
|
||||
type HeldSkipped struct {
|
||||
Pull string `json:"pull"`
|
||||
Why string `json:"why"`
|
||||
}
|
||||
|
||||
func heldValueOf(k KeptValue) HeldValue {
|
||||
return HeldValue{Pull: k.Pull, Head: k.Head, Node: k.Node, Module: k.Module, Key: k.Key, Value: compact(heldValue(k)),
|
||||
Approved: k.Approved, By: k.By, Why: k.Why, At: timeOf(k.At)}
|
||||
}
|
||||
|
||||
// Held answers the approved held values for the pull requests asked, at the heads asked, to the controller alone: a
|
||||
// pull request at another head than the one kept is skipped and said (ADR 0502 clause 4).
|
||||
func (c *Config) Held(call Call) (*HeldAnswer, error) {
|
||||
if call.Caller != ControllerCaller {
|
||||
return nil, fmt.Errorf("held is the controller's: it hands the held values to a composed check; %s is not "+
|
||||
"the controller", call.Caller)
|
||||
}
|
||||
asked := map[string]string{}
|
||||
var order []string
|
||||
for _, p := range call.Pulls {
|
||||
if _, _, _, err := parsePull(p.Pull); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if strings.TrimSpace(p.Head) == "" {
|
||||
return nil, fmt.Errorf("name the head of %s", p.Pull)
|
||||
}
|
||||
if _, dup := asked[pullKey(p.Pull)]; !dup {
|
||||
order = append(order, strings.TrimSpace(p.Pull))
|
||||
}
|
||||
asked[pullKey(p.Pull)] = strings.TrimSpace(p.Head)
|
||||
}
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
var s Sources
|
||||
if err := c.readPulls(&s); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out := &HeldAnswer{Held: []HeldValue{}, Skipped: []HeldSkipped{}}
|
||||
for _, p := range order {
|
||||
head := asked[pullKey(p)]
|
||||
var mine []KeptValue
|
||||
for _, k := range sortedKeys(s.Kept) {
|
||||
if pullKey(s.Kept[k].Pull) == pullKey(p) {
|
||||
mine = append(mine, s.Kept[k])
|
||||
}
|
||||
}
|
||||
for _, k := range mine {
|
||||
if k.Head != head {
|
||||
out.Skipped = append(out.Skipped, HeldSkipped{Pull: p, Why: fmt.Sprintf("%s for %s on %s is held "+
|
||||
"at head %s, not %s", k.Key, k.Module, k.Node, short(k.Head), short(head))})
|
||||
continue
|
||||
}
|
||||
out.Held = append(out.Held, heldValueOf(k))
|
||||
}
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// HeldListed is the held values a listing shows, narrowed by module, node and key.
|
||||
func heldListed(s Sources, call Call) []HeldValue {
|
||||
out := []HeldValue{}
|
||||
for _, k := range sortedKeys(s.Kept) {
|
||||
kv := s.Kept[k]
|
||||
if (call.Module == "" || kv.Module == call.Module) && (call.Node == "" || kv.Node == call.Node) &&
|
||||
(call.Key == "" || kv.Key == call.Key) {
|
||||
out = append(out, heldValueOf(kv))
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// ---- the proposal's rule -------------------------------------------------------------------------------
|
||||
|
||||
// heldRule is the rule a value held for a pull request is judged by: the pull request's declaration of the key for
|
||||
// the module at its head, and the module's assignment to the node. Refused, saying what the pull request does
|
||||
// declare, when it declares none.
|
||||
func heldRule(s Sources, call Call, level Level) (rule, PullDecl, string, error) {
|
||||
if level.Kind != LevelModule {
|
||||
return rule{}, PullDecl{}, "", fmt.Errorf("a value held for a pull request is a module's own value on one "+
|
||||
"node, and %s is not one: a predefined key a pull request adds is set after its merge (novox/hq ADR 0502)",
|
||||
level)
|
||||
}
|
||||
pd, ok := s.Pulls[pullKey(call.Pull)]
|
||||
if !ok {
|
||||
return rule{}, PullDecl{}, "", fmt.Errorf("mesh-config holds no declaration for %s: the controller tells it "+
|
||||
"what a pull request declares when its head is announced, and none has been", strings.TrimSpace(call.Pull))
|
||||
}
|
||||
d, ok := pd.Declares[level.Module][call.Key]
|
||||
if !ok {
|
||||
return rule{}, PullDecl{}, "", fmt.Errorf("%s does not add or change a value %q for %s at %s; it declares "+
|
||||
"for %s: %s", pd.Pull, call.Key, level.Module, short(pd.Head), level.Module, joinKeys(pd.Declares[level.Module]))
|
||||
}
|
||||
if _, reg := s.Declared[level.Module]; !reg {
|
||||
return rule{}, PullDecl{}, "", fmt.Errorf("%s is not a registered module, so a value cannot be held for it", level.Module)
|
||||
}
|
||||
if !assigned(s, level.Node, level.Module) {
|
||||
return rule{}, PullDecl{}, "", fmt.Errorf("%s is not placed on %s, and a module's own value is set for a node "+
|
||||
"it is placed on (novox/hq ADR 0348 §3); it is placed on: %s", level.Module, level.Node,
|
||||
strings.Join(nodesOf(s, level.Module), ", "))
|
||||
}
|
||||
return d.rule(), pd, declDigest(d), nil
|
||||
}
|
||||
|
||||
// keep holds an approved proposal's value apart. The caller holds the lock.
|
||||
func (c *Config) keep(p Proposal) (string, error) {
|
||||
var s Sources
|
||||
if err := c.readPulls(&s); err != nil {
|
||||
return "", err
|
||||
}
|
||||
pd, ok := s.Pulls[pullKey(p.Pull)]
|
||||
if !ok {
|
||||
return "", fmt.Errorf("mesh-config holds no declaration for %s any more", p.Pull)
|
||||
}
|
||||
d, ok := pd.Declares[p.Level.Module][p.Key]
|
||||
if !ok || declDigest(d) != p.DeclDigest {
|
||||
return "", fmt.Errorf("%s declares %s otherwise than the operator was shown, or no longer declares it: "+
|
||||
"propose it again", p.Pull, p.Key)
|
||||
}
|
||||
was, had := s.Kept[keptKey(p.Pull, p.Level.Node, p.Level.Module, p.Key)]
|
||||
if had != p.HadValue || valueDigest(heldValue(was), had) != p.WasDigest {
|
||||
return "", fmt.Errorf("the value held for %s changed since the operator was shown the change: propose it "+
|
||||
"again", p.Pull)
|
||||
}
|
||||
kv := KeptValue{Pull: p.Pull, Head: pd.Head, Declaration: p.DeclDigest, Node: p.Level.Node, Module: p.Level.Module,
|
||||
Key: p.Key, Value: p.Value, By: "the operator", Via: p.From, Why: p.Why, Approved: p.Approved, Proposal: p.ID,
|
||||
At: heldAt(c.now())}
|
||||
if err := c.Store.Put(KeptState, keptStateKey(kv), kv); err != nil {
|
||||
return "", fmt.Errorf("the value was not kept: %v", err)
|
||||
}
|
||||
unsaid := c.heldChange(kv, ActionHold, "held for "+p.Pull+" until it merges: "+p.Why, p.Approved)
|
||||
said := fmt.Sprintf("%s of %s on %s is held at %s for %s until it merges", p.Key, p.Level.Module, p.Level.Node,
|
||||
short(pd.Head), p.Pull)
|
||||
if len(unsaid) > 0 {
|
||||
said += " (" + strings.Join(unsaid, "; ") + ")"
|
||||
}
|
||||
return said, nil
|
||||
}
|
||||
|
||||
// ---- the merge and the close ---------------------------------------------------------------------------
|
||||
|
||||
// PullMerged writes each value held for a pull request as the module's own value on its node, with the approval kept
|
||||
// beside it; it resolves once the trunk declares the key. It answers a line for each.
|
||||
func (c *Config) PullMerged(pull, head string) []string {
|
||||
return c.settlePull(pull, true, head)
|
||||
}
|
||||
|
||||
// PullClosed drops the values held for a pull request closed unmerged, and says that nothing was set.
|
||||
func (c *Config) PullClosed(pull string) []string {
|
||||
return c.settlePull(pull, false, "")
|
||||
}
|
||||
|
||||
func (c *Config) settlePull(pull string, merged bool, head string) []string {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
var s Sources
|
||||
if err := c.readPulls(&s); err != nil {
|
||||
return []string{err.Error()}
|
||||
}
|
||||
var said []string
|
||||
wrote := false
|
||||
for _, k := range sortedKeys(s.Kept) {
|
||||
kv := s.Kept[k]
|
||||
if pullKey(kv.Pull) != pullKey(pull) {
|
||||
continue
|
||||
}
|
||||
if !merged {
|
||||
said = append(said, fmt.Sprintf("%s of %s on %s was held for %s, which closed unmerged: dropped, "+
|
||||
"nothing was set", kv.Key, kv.Module, kv.Node, kv.Pull))
|
||||
said = append(said, c.dropKept(kv, kv.Pull+" closed unmerged")...)
|
||||
continue
|
||||
}
|
||||
// **Only a value held at the merged head, whose key that head declares as it was approved** (ADR 0502 clause 7).
|
||||
why := ""
|
||||
pd, known := s.Pulls[pullKey(pull)]
|
||||
d, declared := pd.Declares[kv.Module][kv.Key]
|
||||
switch {
|
||||
case head == "":
|
||||
why = "the merged head is not known, so the value cannot be held to it"
|
||||
case !strings.EqualFold(head, kv.Head):
|
||||
why = fmt.Sprintf("it is held at head %s and the pull request merged at %s", short(kv.Head), short(head))
|
||||
case !known || !declared || declDigest(d) != kv.Declaration:
|
||||
why = "the merged head does not declare the key as the operator was shown"
|
||||
}
|
||||
if why != "" {
|
||||
said = append(said, fmt.Sprintf("%s of %s on %s was held for %s, which merged: dropped, nothing was set (%s)",
|
||||
kv.Key, kv.Module, kv.Node, kv.Pull, why))
|
||||
said = append(said, c.dropKept(kv, kv.Pull+" merged, but "+why)...)
|
||||
continue
|
||||
}
|
||||
vk := keptLevel(kv).ValueKey(kv.Key)
|
||||
v := Value{Value: compact(heldValue(kv)), By: kv.By, Via: kv.Via, Why: kv.Why + " (held for " + kv.Pull +
|
||||
", merged)", Approved: kv.Approved, At: c.now()}
|
||||
if err := c.Store.Put(ValuesState, vk, v); err != nil {
|
||||
said = append(said, fmt.Sprintf("%s of %s on %s held for %s is not written yet: %v; it is tried "+
|
||||
"again", kv.Key, kv.Module, kv.Node, kv.Pull, err))
|
||||
continue
|
||||
}
|
||||
if err := c.Store.Delete(KeptState, k); err != nil {
|
||||
said = append(said, "the held value "+k+" is written but not removed: "+err.Error())
|
||||
}
|
||||
wrote = true
|
||||
said = append(said, c.heldChange(kv, "set", kv.Pull+" merged: the held value is the module's own value now",
|
||||
kv.Approved)...)
|
||||
said = append(said, fmt.Sprintf("%s of %s on %s held for %s is set: %s", kv.Key, kv.Module, kv.Node,
|
||||
kv.Pull, kv.Value))
|
||||
}
|
||||
if wrote {
|
||||
if s2, err := c.sources(); err == nil {
|
||||
if _, err := c.writeResolved(s2); err != nil {
|
||||
said = append(said, "the resolved values are not all written yet ("+err.Error()+"); the next "+
|
||||
"resolution writes them")
|
||||
}
|
||||
}
|
||||
}
|
||||
left := 0
|
||||
for _, k := range sortedKeys(s.Kept) {
|
||||
if pullKey(s.Kept[k].Pull) != pullKey(pull) {
|
||||
continue
|
||||
}
|
||||
if e, err := c.Store.Get(KeptState, k); err != nil || e != nil {
|
||||
left++
|
||||
}
|
||||
}
|
||||
if left == 0 {
|
||||
if err := c.Store.Delete(PullsState, pullKey(pull)); err != nil {
|
||||
said = append(said, "the declarations of "+pull+" were not removed: "+err.Error())
|
||||
}
|
||||
}
|
||||
return said
|
||||
}
|
||||
|
||||
// PullEvent is a pull request merged or closed, as far as this module reads it: the seat `git`'s `pull-merged` or `pull-closed`
|
||||
// (design 47 G §654), read by pullEventOf.
|
||||
type PullEvent struct {
|
||||
Owner string
|
||||
Repo string
|
||||
Number int
|
||||
Merged bool
|
||||
// HeadSHA is the head the pull request merged at.
|
||||
HeadSHA string
|
||||
}
|
||||
|
||||
// pullEventOf reads the seat `git`'s `pull-merged` or `pull-closed` body: the repository `<owner>/<name>`, the number and the head.
|
||||
func pullEventOf(body []byte) (PullEvent, bool) {
|
||||
var e struct {
|
||||
Repository string `json:"repository"`
|
||||
Number int `json:"number"`
|
||||
Head string `json:"head"`
|
||||
}
|
||||
if json.Unmarshal(body, &e) != nil {
|
||||
return PullEvent{}, false
|
||||
}
|
||||
owner, repo, ok := strings.Cut(e.Repository, "/")
|
||||
return PullEvent{Owner: owner, Repo: repo, Number: e.Number, HeadSHA: e.Head}, ok
|
||||
}
|
||||
|
||||
func (p PullEvent) name() string { return fmt.Sprintf("%s/%s#%d", p.Owner, p.Repo, p.Number) }
|
||||
|
||||
// HeardPull takes the forge's word that a pull request merged or closed. A close is asked of the forge when it can
|
||||
// be, because a merged pull request may be announced as closed too.
|
||||
func (c *Config) HeardPull(p PullEvent, merged bool) []string {
|
||||
if p.Owner == "" || p.Repo == "" || p.Number <= 0 {
|
||||
return nil
|
||||
}
|
||||
head := p.HeadSHA
|
||||
if !merged && !p.Merged && c.Forge != nil {
|
||||
// A close is told from a merge by the forge; when it cannot be asked nothing is dropped, and the five-minute
|
||||
// look finds it.
|
||||
open, m, h, err := c.Forge.Pull(p.Owner, p.Repo, p.Number)
|
||||
if err != nil {
|
||||
c.logf("[mesh-config] %s closed, and the forge does not say whether it merged (%v); the next look decides", p.name(), err)
|
||||
return nil
|
||||
}
|
||||
if open {
|
||||
return nil
|
||||
}
|
||||
merged = m
|
||||
if head == "" {
|
||||
head = h
|
||||
}
|
||||
}
|
||||
return c.settlePull(p.name(), merged || p.Merged, head)
|
||||
}
|
||||
|
||||
// SweepPulls looks at the forge for every pull request it keeps something for, so a merge or a close it did not hear
|
||||
// is found: run with each five-minute resolution.
|
||||
func (c *Config) SweepPulls() []string {
|
||||
if c.Forge == nil {
|
||||
return nil
|
||||
}
|
||||
c.mu.Lock()
|
||||
var s Sources
|
||||
err := c.readPulls(&s)
|
||||
c.mu.Unlock()
|
||||
if err != nil {
|
||||
return []string{err.Error()}
|
||||
}
|
||||
seen := map[string]string{}
|
||||
for _, p := range s.Pulls {
|
||||
seen[pullKey(p.Pull)] = p.Pull
|
||||
}
|
||||
for _, k := range s.Kept {
|
||||
seen[pullKey(k.Pull)] = k.Pull
|
||||
}
|
||||
var said []string
|
||||
for _, key := range sortedKeys(seen) {
|
||||
pull := seen[key]
|
||||
owner, repo, n, err := parsePull(pull)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
open, merged, head, err := c.Forge.Pull(owner, repo, n)
|
||||
if err != nil {
|
||||
said = append(said, fmt.Sprintf("the forge does not say whether %s is open: %v", pull, err))
|
||||
continue
|
||||
}
|
||||
if !open {
|
||||
said = append(said, c.settlePull(pull, merged, head)...)
|
||||
}
|
||||
}
|
||||
return said
|
||||
}
|
||||
|
||||
// PullAt is a pull request at a head, as held asks about it.
|
||||
type PullAt struct {
|
||||
Pull string `json:"pull"`
|
||||
Head string `json:"head"`
|
||||
}
|
||||
|
||||
// toolForge asks the forge's holder through its tools.
|
||||
type toolForge struct {
|
||||
ask func(key string, body any) (json.RawMessage, error)
|
||||
}
|
||||
|
||||
// Pull asks the seat `git`'s verb `pull` whether a pull request is open, and whether it merged.
|
||||
func (f toolForge) Pull(owner, repo string, number int) (bool, bool, string, error) {
|
||||
raw, err := f.ask(GitPullAsk, map[string]any{"repository": owner + "/" + repo, "number": number})
|
||||
if err != nil {
|
||||
return false, false, "", err
|
||||
}
|
||||
answer, output, ok := unwrapAnswer(raw)
|
||||
if !ok {
|
||||
return false, false, "", fmt.Errorf("the seat git refused pull: %s", output)
|
||||
}
|
||||
var a struct {
|
||||
Pull struct {
|
||||
State string `json:"state"`
|
||||
Head string `json:"head"`
|
||||
} `json:"pull"`
|
||||
Error string `json:"error"`
|
||||
}
|
||||
if err := json.Unmarshal(answer, &a); err != nil || a.Error != "" || a.Pull.State == "" {
|
||||
return false, false, "", fmt.Errorf("the seat git's pull is not readable: %s", output)
|
||||
}
|
||||
return a.Pull.State == "open", a.Pull.State == "merged", a.Pull.Head, nil
|
||||
}
|
||||
|
||||
// GitPullAsk is the seat `git`'s verb that answers one pull request.
|
||||
const GitPullAsk = "seat:git.pull"
|
||||
|
||||
// unwrapAnswer reads a tool's answer whatever wraps it: the protocol's content list, or {output, ok, answer}.
|
||||
func unwrapAnswer(raw json.RawMessage) (json.RawMessage, string, bool) {
|
||||
var s string
|
||||
if json.Unmarshal(raw, &s) == nil {
|
||||
return unwrapAnswer(json.RawMessage(s))
|
||||
}
|
||||
var m map[string]json.RawMessage
|
||||
if json.Unmarshal(raw, &m) != nil {
|
||||
return raw, string(raw), true
|
||||
}
|
||||
if content, has := m["content"]; has {
|
||||
var items []struct {
|
||||
Text string `json:"text"`
|
||||
}
|
||||
var e struct {
|
||||
IsError bool `json:"isError"`
|
||||
}
|
||||
_ = json.Unmarshal(raw, &e)
|
||||
if json.Unmarshal(content, &items) == nil && len(items) > 0 {
|
||||
inner, out, ok := unwrapAnswer(json.RawMessage(items[0].Text))
|
||||
return inner, out, ok && !e.IsError
|
||||
}
|
||||
}
|
||||
var v struct {
|
||||
Output string `json:"output"`
|
||||
OK *bool `json:"ok"`
|
||||
Answer json.RawMessage `json:"answer"`
|
||||
}
|
||||
if json.Unmarshal(raw, &v) == nil && v.OK != nil {
|
||||
return v.Answer, v.Output, *v.OK
|
||||
}
|
||||
return raw, string(raw), true
|
||||
}
|
||||
@@ -1,504 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.novox.be/novox/mesh-contracts/go/clients/meshconfigclient"
|
||||
|
||||
occ "git.novox.be/novox/mesh-contracts/operator-and-conversation/go/clients/operatorchannelclient"
|
||||
)
|
||||
|
||||
// Issue 1826 (novox/hq ADR 0502): a value held for the open pull request that introduces its key.
|
||||
|
||||
const thePull = "novox/mesh-catalog#434"
|
||||
|
||||
// n8nPullDeclares is what the controller tells of the pull request: n8n gets a required number media-gid.
|
||||
func n8nPullDeclares() map[string]any {
|
||||
return map[string]any{"n8n": map[string]any{"media-gid": map[string]any{"type": "number",
|
||||
"description": "the media group", "required": true, "minimum": 1, "maximum": 65535}}}
|
||||
}
|
||||
|
||||
// aHoldingMesh is a proposing mesh with n8n assigned to ace and declaring nothing on the trunk (the case of the
|
||||
// issue), and a stand-in forge.
|
||||
func aHoldingMesh(t *testing.T) (*Config, *memStore, *standIn, *standForge) {
|
||||
t.Helper()
|
||||
c, m, r := aProposingMesh(t)
|
||||
m.declare(t, "n8n", `{"module":"n8n","settings":{}}`)
|
||||
m.assign(t, "ace", "n8n")
|
||||
f := &standForge{state: map[string][2]bool{}}
|
||||
c.Forge = f
|
||||
if _, err := c.Reconcile(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
m.events, m.handActs = nil, nil
|
||||
return c, m, r, f
|
||||
}
|
||||
|
||||
type standForge struct {
|
||||
state map[string][2]bool // pull -> open, merged
|
||||
head string
|
||||
err error
|
||||
}
|
||||
|
||||
func (f *standForge) Pull(owner, repo string, n int) (bool, bool, string, error) {
|
||||
if f.err != nil {
|
||||
return false, false, "", f.err
|
||||
}
|
||||
st, ok := f.state[(PullEvent{Owner: owner, Repo: repo, Number: n}).name()]
|
||||
if !ok {
|
||||
return true, false, f.head, nil
|
||||
}
|
||||
return st[0], st[1], f.head, nil
|
||||
}
|
||||
|
||||
// tell keeps what a pull request's head declares as the indexer's candidate is taken (takeCandidates).
|
||||
func tell(t *testing.T, c *Config, pull, head string, declares map[string]any) *PullDeclaresAnswer {
|
||||
t.Helper()
|
||||
raw, _ := json.Marshal(declares)
|
||||
d := map[string]map[string]Declaration{}
|
||||
if err := json.Unmarshal(raw, &d); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
a, err := c.declarePull(pull, head, d)
|
||||
if err != nil {
|
||||
t.Fatalf("declare: %v", err)
|
||||
}
|
||||
return a
|
||||
}
|
||||
|
||||
// holdIt proposes media-gid=2000 for n8n on ace held for the pull request and has the operator approve it.
|
||||
func holdIt(t *testing.T, c *Config, m *memStore, r *standIn) string {
|
||||
t.Helper()
|
||||
a := propose(t, c, Call{Module: "n8n", Node: "ace", Key: "media-gid", Value: 2000.0, Pull: thePull,
|
||||
Why: "n8n's container joins the media group, held for #434"})
|
||||
r.choose(t, a.Proposal, OptionApprove, nil)
|
||||
said, err := c.Decide()
|
||||
if err != nil || len(said) != 1 || !strings.Contains(said[0], "approved by the operator and held") {
|
||||
t.Fatalf("the Approve decided %v (%v)", said, err)
|
||||
}
|
||||
return a.Proposal
|
||||
}
|
||||
|
||||
func keptCount(m *memStore) int { k, _ := m.Keys(KeptState); return len(k) }
|
||||
|
||||
// 1826/K1: mesh-config keeps what a pull request declares per pull request; pull-declares is gone (ADR 0590).
|
||||
func TestAPullRequestsDeclarationsAreKeptPerPullRequest(t *testing.T) {
|
||||
c, m, _, _ := aHoldingMesh(t)
|
||||
for _, caller := range []string{"g14.node-tools", Unproven, "", ControllerCaller} {
|
||||
if _, err := c.Do(Call{Action: "pull-declares", Pull: thePull, Head: "abc1234", Declares: n8nPullDeclares(),
|
||||
Caller: caller}); err == nil || !strings.Contains(err.Error(), "config-candidates") {
|
||||
t.Errorf("pull-declares from %q: %v", caller, err)
|
||||
}
|
||||
}
|
||||
a := tell(t, c, thePull, "abc1234def", n8nPullDeclares())
|
||||
if len(a.Modules) != 1 || a.Modules[0] != "n8n" {
|
||||
t.Errorf("answered %+v", a)
|
||||
}
|
||||
e, _ := m.Get(PullsState, "novox.mesh-catalog.434")
|
||||
var pd PullDecl
|
||||
if e == nil || json.Unmarshal(e.Value, &pd) != nil || pd.Head != "abc1234def" ||
|
||||
pd.Declares["n8n"]["media-gid"].Type != "number" || !pd.Declares["n8n"]["media-gid"].Required {
|
||||
t.Errorf("kept %s", func() string {
|
||||
if e == nil {
|
||||
return "nothing"
|
||||
}
|
||||
return string(e.Value)
|
||||
}())
|
||||
}
|
||||
// Nothing is resolved from a pull request's declaration.
|
||||
if r, _ := m.Get(ResolvedState, ResolvedKey("ace", "n8n", "media-gid")); r != nil {
|
||||
t.Errorf("a pull request's declaration was resolved: %s", r.Value)
|
||||
}
|
||||
}
|
||||
|
||||
// 1826/K2: a proposal for a key only the pull request declares is refused without its declaration, for a mesh or
|
||||
// node level, for a clear, for an undeclared key, and for a module not placed on the node; and trunk-only
|
||||
// proposals are judged as before.
|
||||
func TestAProposalNamingAPullRequestIsJudgedAgainstItsDeclaration(t *testing.T) {
|
||||
c, m, _, _ := aHoldingMesh(t)
|
||||
try := func(call Call) error {
|
||||
call.Action, call.Caller, call.Why = "propose", "g14.node-tools", "held for 434"
|
||||
_, err := c.Propose(call)
|
||||
return err
|
||||
}
|
||||
base := Call{Module: "n8n", Node: "ace", Key: "media-gid", Value: 2000.0, Pull: thePull}
|
||||
if err := try(base); err == nil || !strings.Contains(err.Error(), "holds no declaration for "+thePull) {
|
||||
t.Errorf("without the pull request's declaration: %v", err)
|
||||
}
|
||||
// And without the pull argument it is the trunk's refusal, as before.
|
||||
plain := base
|
||||
plain.Pull = ""
|
||||
if err := try(plain); err == nil || !strings.Contains(err.Error(), `n8n declares no value "media-gid"`) {
|
||||
t.Errorf("the trunk's refusal changed: %v", err)
|
||||
}
|
||||
tell(t, c, thePull, "abc1234def", n8nPullDeclares())
|
||||
for name, tc := range map[string]struct {
|
||||
mutate func(*Call)
|
||||
want string
|
||||
}{
|
||||
"a mesh level": {func(c *Call) { c.Module, c.Node = "", "" }, "a module's own value on one node"},
|
||||
"a node level": {func(c *Call) { c.Module = "" }, "a module's own value on one node"},
|
||||
"a clear": {func(c *Call) { c.Clear, c.Value = true, nil }, "a clear is not held"},
|
||||
"a key not added": {func(c *Call) { c.Key = "other" }, "does not add or change a value"},
|
||||
"a bad value": {func(c *Call) { c.Value = 0.0 }, "below the minimum"},
|
||||
"a wrong type": {func(c *Call) { c.Value = "2000" }, "is not a number"},
|
||||
"another node": {func(c *Call) { c.Node = "g14" }, "is not placed on g14"},
|
||||
"another pull": {func(c *Call) { c.Pull = "novox/mesh-catalog#435" }, "holds no declaration for"},
|
||||
"a malformed pull": {func(c *Call) { c.Pull = "434" }, "owner/repository#number"},
|
||||
} {
|
||||
call := base
|
||||
tc.mutate(&call)
|
||||
if err := try(call); err == nil || !strings.Contains(err.Error(), tc.want) {
|
||||
t.Errorf("%s: %v, want %q", name, err, tc.want)
|
||||
}
|
||||
}
|
||||
// A set or a clear through the verb takes no pull request.
|
||||
if _, err := c.Change(Call{Action: "set", Module: "n8n", Node: "ace", Key: "media-gid", Value: 2000.0, Pull: thePull,
|
||||
Caller: "g14.node-tools", Why: "w"}); err == nil || !strings.Contains(err.Error(), "only a proposal") {
|
||||
t.Errorf("a set naming a pull request: %v", err)
|
||||
}
|
||||
if keptCount(m) != 0 {
|
||||
t.Error("something was held")
|
||||
}
|
||||
}
|
||||
|
||||
// 1826/K2: every held value is asked whatever the key's trust; the ask says it is held and dropped on a close; on
|
||||
// Approve the value is kept apart, never set or resolved; history, event and hand-act say it; held answers it.
|
||||
func TestAHeldValueIsAskedAndKeptApartOnApprove(t *testing.T) {
|
||||
c, m, r, _ := aHoldingMesh(t)
|
||||
tell(t, c, thePull, "abc1234def", n8nPullDeclares())
|
||||
a := propose(t, c, Call{Module: "n8n", Node: "ace", Key: "media-gid", Value: 2000.0, Pull: thePull, Why: "held for #434"})
|
||||
if len(a.Reaches) != 0 {
|
||||
t.Errorf("a held value reaches %v", a.Reaches)
|
||||
}
|
||||
ask := r.asks[a.Proposal]
|
||||
if err := occ.Check(ask, c.now()); err != nil {
|
||||
t.Fatalf("the ask: %v", err)
|
||||
}
|
||||
for _, want := range []string{thePull, "merges", "dropped if it closes unmerged", "media-gid", "n8n", "ace"} {
|
||||
if !strings.Contains(ask.Explanation, want) {
|
||||
t.Errorf("the ask does not say %q:\n%s", want, ask.Explanation)
|
||||
}
|
||||
}
|
||||
if strings.Contains(ask.Explanation, "takes effect at once") || !strings.Contains(ask.Headline, "Hold") {
|
||||
t.Errorf("the ask: %q\n%s", ask.Headline, ask.Explanation)
|
||||
}
|
||||
if ap := ask.Options[0]; !strings.Contains(ap.Does, thePull) || ap.Level == "" {
|
||||
t.Errorf("the approve option does %q", ap.Does)
|
||||
}
|
||||
if keptCount(m) != 0 {
|
||||
t.Fatal("a proposal held something")
|
||||
}
|
||||
r.choose(t, a.Proposal, OptionApprove, nil)
|
||||
said, _ := c.Decide()
|
||||
if len(said) != 1 || !strings.Contains(said[0], "held at abc1234d for "+thePull) {
|
||||
t.Fatalf("decided %v", said)
|
||||
}
|
||||
// Kept apart: not a value, not resolved.
|
||||
if _, set := storedValue(t, m, "module.ace.n8n.media-gid"); set {
|
||||
t.Error("an Approve set the value")
|
||||
}
|
||||
if e, _ := m.Get(ResolvedState, ResolvedKey("ace", "n8n", "media-gid")); e != nil {
|
||||
t.Errorf("an Approve resolved the value: %s", e.Value)
|
||||
}
|
||||
if keptCount(m) != 1 {
|
||||
t.Fatalf("%d values held", keptCount(m))
|
||||
}
|
||||
if len(m.events) != 1 || m.events[0].Action != ActionHold || m.events[0].Pull != thePull ||
|
||||
m.events[0].Key != "media-gid" || m.events[0].Head != "abc1234def" || len(m.events[0].Moved) != 0 ||
|
||||
!strings.Contains(m.events[0].Approved, "approved by the operator") {
|
||||
t.Errorf("raised %+v", m.events)
|
||||
}
|
||||
if len(m.handActs) != 1 || !strings.Contains(m.handActs[0], thePull) {
|
||||
t.Errorf("hand-acts %q", m.handActs)
|
||||
}
|
||||
h, _ := c.History(Call{Module: "n8n", Node: "ace"})
|
||||
if len(h) != 1 || h[0].Action != ActionHold || h[0].Pull != thePull {
|
||||
t.Errorf("history %+v", h)
|
||||
}
|
||||
// held answers it, at the head it is kept at, to the controller alone.
|
||||
if _, err := c.Do(Call{Action: "held", Caller: "g14.node-tools", Pulls: []PullAt{{thePull, "abc1234def"}}}); err == nil {
|
||||
t.Error("held answered a caller that is not the controller")
|
||||
}
|
||||
got, err := c.Do(Call{Action: "held", Caller: ControllerCaller, Pulls: []PullAt{{thePull, "abc1234def"}}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ha := got.(*HeldAnswer)
|
||||
if len(ha.Held) != 1 || ha.Held[0].Module != "n8n" || ha.Held[0].Node != "ace" || ha.Held[0].Key != "media-gid" ||
|
||||
string(ha.Held[0].Value) != "2000" || !strings.Contains(ha.Held[0].Approved, "approved by the operator") ||
|
||||
ha.Held[0].Pull != thePull || len(ha.Skipped) != 0 {
|
||||
t.Errorf("held answered %+v", ha)
|
||||
}
|
||||
// Another head, and another pull request, are not answered.
|
||||
got, _ = c.Do(Call{Action: "held", Caller: ControllerCaller,
|
||||
Pulls: []PullAt{{thePull, "ffff0000"}, {"novox/mesh-catalog#435", "abc"}}})
|
||||
if ha = got.(*HeldAnswer); len(ha.Held) != 0 || len(ha.Skipped) != 1 || !strings.Contains(ha.Skipped[0].Why, "held at") {
|
||||
t.Errorf("held at another head answered %+v", ha)
|
||||
}
|
||||
// The listing shows it as held, and the proposal as held for the pull request.
|
||||
l, _ := c.List(Call{Module: "n8n"})
|
||||
if len(l.Held) != 1 || len(l.Set) != 0 {
|
||||
t.Errorf("the listing: held %+v set %+v", l.Held, l.Set)
|
||||
}
|
||||
rows, _ := c.Proposals(Call{})
|
||||
if len(rows) != 1 || rows[0].Pull != thePull || rows[0].State != ProposalApproved {
|
||||
t.Errorf("proposals %+v", rows)
|
||||
}
|
||||
}
|
||||
|
||||
// 1826/K2: an Approve is refused when the pull request's declaration changed after the operator was shown it.
|
||||
func TestAnApproveOfAHeldValueIsRefusedWhenTheDeclarationChanged(t *testing.T) {
|
||||
c, m, r, _ := aHoldingMesh(t)
|
||||
tell(t, c, thePull, "abc1234def", n8nPullDeclares())
|
||||
a := propose(t, c, Call{Module: "n8n", Node: "ace", Key: "media-gid", Value: 2000.0, Pull: thePull, Why: "w"})
|
||||
changed := n8nPullDeclares()
|
||||
changed["n8n"].(map[string]any)["media-gid"].(map[string]any)["maximum"] = 1000.0
|
||||
tell(t, c, thePull, "def5678abc", changed)
|
||||
r.choose(t, a.Proposal, OptionApprove, nil)
|
||||
said, _ := c.Decide()
|
||||
if len(said) != 1 || !strings.Contains(said[0], "nothing is held") || keptCount(m) != 0 {
|
||||
t.Errorf("decided %v; %d held", said, keptCount(m))
|
||||
}
|
||||
}
|
||||
|
||||
// 1826/K1: at a new head a held value is kept if its key is declared the same and it still passes; otherwise it is
|
||||
// dropped, recorded, raised and said.
|
||||
func TestANewHeadKeepsAHeldValueOnlyWhileItsDeclarationIsUnchanged(t *testing.T) {
|
||||
c, m, r, _ := aHoldingMesh(t)
|
||||
tell(t, c, thePull, "abc1234def", n8nPullDeclares())
|
||||
holdIt(t, c, m, r)
|
||||
m.events = nil
|
||||
|
||||
// The same declaration at a new head (the description may differ): kept, moved to the new head.
|
||||
same := n8nPullDeclares()
|
||||
same["n8n"].(map[string]any)["media-gid"].(map[string]any)["description"] = "reworded"
|
||||
if a := tell(t, c, thePull, "1111111111", same); len(a.Dropped) != 0 {
|
||||
t.Errorf("an unchanged declaration dropped %v", a.Dropped)
|
||||
}
|
||||
got, _ := c.Do(Call{Action: "held", Caller: ControllerCaller, Pulls: []PullAt{{thePull, "1111111111"}}})
|
||||
if len(got.(*HeldAnswer).Held) != 1 || len(m.events) != 0 {
|
||||
t.Errorf("after an unchanged head: %+v, events %+v", got, m.events)
|
||||
}
|
||||
|
||||
// A changed declaration: dropped.
|
||||
changed := n8nPullDeclares()
|
||||
changed["n8n"].(map[string]any)["media-gid"].(map[string]any)["maximum"] = 1000.0
|
||||
a := tell(t, c, thePull, "2222222222", changed)
|
||||
if len(a.Dropped) != 1 || !strings.Contains(a.Dropped[0], "declares the key otherwise") || keptCount(m) != 0 {
|
||||
t.Fatalf("a changed declaration: %+v, %d held", a, keptCount(m))
|
||||
}
|
||||
if len(m.events) != 1 || m.events[0].Action != ActionDrop || m.events[0].Pull != thePull {
|
||||
t.Errorf("raised %+v", m.events)
|
||||
}
|
||||
got, _ = c.Do(Call{Action: "held", Caller: ControllerCaller, Pulls: []PullAt{{thePull, "2222222222"}}})
|
||||
if len(got.(*HeldAnswer).Held) != 0 {
|
||||
t.Error("a dropped value was answered")
|
||||
}
|
||||
|
||||
// A head that no longer adds the key drops it too.
|
||||
tell(t, c, thePull, "3333333333", n8nPullDeclares())
|
||||
holdIt(t, c, m, r)
|
||||
if a = tell(t, c, thePull, "4444444444", map[string]any{}); len(a.Dropped) != 1 ||
|
||||
!strings.Contains(a.Dropped[0], "no longer adds or changes") {
|
||||
t.Errorf("a head without the key: %+v", a)
|
||||
}
|
||||
}
|
||||
|
||||
// 1826/K3: the merge writes the held value as the module's own value with its approval kept, before the trunk
|
||||
// declares the key; it resolves right after the declared-config mirror moves.
|
||||
func TestTheMergeAppliesAHeldValueAndItResolvesOnceTheTrunkDeclaresTheKey(t *testing.T) {
|
||||
c, m, r, _ := aHoldingMesh(t)
|
||||
tell(t, c, thePull, "abc1234def", n8nPullDeclares())
|
||||
holdIt(t, c, m, r)
|
||||
approved := func() string {
|
||||
raw, _ := m.Get(KeptState, "novox.mesh-catalog.434.ace.n8n.media-gid")
|
||||
// the state kept is the seat's layout, under the seat's key: the checker and the assigner read it with this
|
||||
k, err := meshconfigclient.DecodeKept("novox.mesh-catalog.434.ace.n8n.media-gid", raw.Value)
|
||||
if err != nil || string(heldValue(k)) != "2000" || k.Declaration == "" || k.At == nil {
|
||||
t.Fatalf("the held value is not the seat's: %+v, %v", k, err)
|
||||
}
|
||||
return k.Approved
|
||||
}()
|
||||
m.events, m.handActs = nil, nil
|
||||
|
||||
said := c.HeardPull(PullEvent{Owner: "novox", Repo: "mesh-catalog", Number: 434, Merged: true, HeadSHA: "abc1234def"}, true)
|
||||
if len(said) == 0 {
|
||||
t.Fatal("the merge said nothing")
|
||||
}
|
||||
v, set := storedValue(t, m, "module.ace.n8n.media-gid")
|
||||
if !set || string(v.Value) != "2000" || v.Approved != approved || v.Approved == "" || v.By != "the operator" ||
|
||||
!strings.Contains(v.Why, thePull) {
|
||||
t.Fatalf("the merge wrote %+v", v)
|
||||
}
|
||||
if keptCount(m) != 0 {
|
||||
t.Error("the value is still held after the merge")
|
||||
}
|
||||
if p, _ := m.Keys(PullsState); len(p) != 0 {
|
||||
t.Errorf("the pull request's declarations are still kept: %v", p)
|
||||
}
|
||||
if len(m.events) != 1 || m.events[0].Action != "set" || m.events[0].Pull != thePull ||
|
||||
m.events[0].Approved != approved || len(m.handActs) != 1 {
|
||||
t.Errorf("raised %+v, recorded %q", m.events, m.handActs)
|
||||
}
|
||||
// Not resolved before the trunk declares it.
|
||||
if e, _ := m.Get(ResolvedState, ResolvedKey("ace", "n8n", "media-gid")); e != nil {
|
||||
t.Errorf("resolved before the trunk declares the key: %s", e.Value)
|
||||
}
|
||||
m.declare(t, "n8n", `{"module":"n8n","settings":{"media-gid":{"kind":"","default":null,"why":"",`+
|
||||
`"type":"number","required":true,"minimum":1,"maximum":65535}}}`)
|
||||
if _, err := c.Reconcile(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if res := m.resolved(t, "ace", "n8n", "media-gid"); string(res.Value) != "2000" || res.Source != SourceModule {
|
||||
t.Errorf("media-gid resolves as %+v", res)
|
||||
}
|
||||
l, _ := c.List(Call{Module: "n8n", Node: "ace"})
|
||||
if len(l.Set) != 1 || l.Set[0].Approved != approved || len(l.Held) != 0 {
|
||||
t.Errorf("the listing: set %+v held %+v", l.Set, l.Held)
|
||||
}
|
||||
// A merge heard twice sets nothing twice.
|
||||
m.events = nil
|
||||
c.HeardPull(PullEvent{Owner: "novox", Repo: "mesh-catalog", Number: 434, Merged: true, HeadSHA: "abc1234def"}, true)
|
||||
if len(m.events) != 0 {
|
||||
t.Errorf("a second merge raised %+v", m.events)
|
||||
}
|
||||
}
|
||||
|
||||
// 1826/K3: a pull request closed unmerged drops its held values and says so; nothing is set or resolved.
|
||||
func TestAClosedPullRequestDropsItsHeldValuesAndSaysSo(t *testing.T) {
|
||||
c, m, r, f := aHoldingMesh(t)
|
||||
tell(t, c, thePull, "abc1234def", n8nPullDeclares())
|
||||
holdIt(t, c, m, r)
|
||||
m.events = nil
|
||||
f.state[thePull] = [2]bool{false, false}
|
||||
said := c.HeardPull(PullEvent{Owner: "novox", Repo: "mesh-catalog", Number: 434}, false)
|
||||
if len(said) == 0 || !strings.Contains(strings.Join(said, "\n"), "closed unmerged: dropped, nothing was set") {
|
||||
t.Errorf("said %v", said)
|
||||
}
|
||||
if _, set := storedValue(t, m, "module.ace.n8n.media-gid"); set || keptCount(m) != 0 {
|
||||
t.Error("a closed pull request left a value")
|
||||
}
|
||||
if len(m.events) != 1 || m.events[0].Action != ActionDrop || m.events[0].Pull != thePull ||
|
||||
!strings.Contains(m.events[0].Why, "closed unmerged") {
|
||||
t.Errorf("raised %+v", m.events)
|
||||
}
|
||||
if p, _ := m.Keys(PullsState); len(p) != 0 {
|
||||
t.Errorf("declarations of a closed pull request kept: %v", p)
|
||||
}
|
||||
}
|
||||
|
||||
// 1826/K3: a close event for a pull request the forge says merged is the merge.
|
||||
func TestACloseEventForAMergedPullRequestIsItsMerge(t *testing.T) {
|
||||
c, m, r, f := aHoldingMesh(t)
|
||||
tell(t, c, thePull, "abc1234def", n8nPullDeclares())
|
||||
holdIt(t, c, m, r)
|
||||
f.state[thePull] = [2]bool{false, true}
|
||||
f.head = "abc1234def"
|
||||
c.HeardPull(PullEvent{Owner: "novox", Repo: "mesh-catalog", Number: 434}, false)
|
||||
if v, set := storedValue(t, m, "module.ace.n8n.media-gid"); !set || string(v.Value) != "2000" {
|
||||
t.Errorf("the merge of a pull request announced closed set %+v", v)
|
||||
}
|
||||
}
|
||||
|
||||
// 1826/K3: the five-minute look finds a merge, a close, and an open pull request, and says a forge that does not
|
||||
// answer.
|
||||
func TestTheLookAtTheForgeFindsAMissedMergeOrCloseAndLeavesAnOpenPullRequest(t *testing.T) {
|
||||
c, m, r, f := aHoldingMesh(t)
|
||||
tell(t, c, thePull, "abc1234def", n8nPullDeclares())
|
||||
holdIt(t, c, m, r)
|
||||
if said := c.SweepPulls(); len(said) != 0 || keptCount(m) != 1 {
|
||||
t.Errorf("an open pull request: said %v, %d held", said, keptCount(m))
|
||||
}
|
||||
f.err = errors.New("the forge is down")
|
||||
if said := c.SweepPulls(); len(said) != 1 || !strings.Contains(said[0], "does not say whether") || keptCount(m) != 1 {
|
||||
t.Errorf("a silent forge: said %v", said)
|
||||
}
|
||||
f.err = nil
|
||||
f.head = "abc1234def"
|
||||
f.state[thePull] = [2]bool{false, true}
|
||||
c.SweepPulls()
|
||||
if v, set := storedValue(t, m, "module.ace.n8n.media-gid"); !set || string(v.Value) != "2000" || keptCount(m) != 0 {
|
||||
t.Errorf("a missed merge: %+v", v)
|
||||
}
|
||||
// A missed close.
|
||||
tell(t, c, "novox/mesh-catalog#440", "abc", n8nPullDeclares())
|
||||
f.state["novox/mesh-catalog#440"] = [2]bool{false, false}
|
||||
c.SweepPulls()
|
||||
if p, _ := m.Keys(PullsState); len(p) != 0 {
|
||||
t.Errorf("a missed close left %v", p)
|
||||
}
|
||||
}
|
||||
|
||||
// 1826/K1: the new arguments are read strictly and the new actions are the verb's.
|
||||
func TestThePullArgumentsAreRead(t *testing.T) {
|
||||
call, err := callOf(map[string]any{"action": "pull-declares", "pull": " " + thePull + " ", "head": "abc",
|
||||
"declares": map[string]any{"n8n": map[string]any{}}, "pulls": []any{map[string]any{"pull": thePull, "head": "h"}}})
|
||||
if err != nil || call.Pull != thePull || call.Head != "abc" || len(call.Declares) != 1 || len(call.Pulls) != 1 ||
|
||||
call.Pulls[0].Head != "h" {
|
||||
t.Errorf("read %+v (%v)", call, err)
|
||||
}
|
||||
if _, err := callOf(map[string]any{"declares": "x"}); err == nil {
|
||||
t.Error("declares that is not an object was read")
|
||||
}
|
||||
if _, err := callOf(map[string]any{"pulls": "x"}); err == nil {
|
||||
t.Error("pulls that is not a list was read")
|
||||
}
|
||||
if _, err := callOf(map[string]any{"bogus": 1}); err == nil || !strings.Contains(err.Error(), "pull") {
|
||||
t.Errorf("an unknown argument: %v", err)
|
||||
}
|
||||
_ = time.Second
|
||||
}
|
||||
|
||||
// 1826 review r1: the merge applies a value only if it is held at the merged head; otherwise it is dropped and said.
|
||||
func TestTheMergeAppliesOnlyAValueHeldAtTheMergedHead(t *testing.T) {
|
||||
for name, head := range map[string]string{"another head": "9999999999", "no head told": ""} {
|
||||
c, m, r, _ := aHoldingMesh(t)
|
||||
tell(t, c, thePull, "abc1234def", n8nPullDeclares())
|
||||
holdIt(t, c, m, r)
|
||||
said := c.HeardPull(PullEvent{Owner: "novox", Repo: "mesh-catalog", Number: 434, Merged: true, HeadSHA: head}, true)
|
||||
if _, set := storedValue(t, m, "module.ace.n8n.media-gid"); set || keptCount(m) != 0 ||
|
||||
!strings.Contains(strings.Join(said, "\n"), "dropped, nothing was set") {
|
||||
t.Errorf("%s: set=%v held=%d said %v", name, set, keptCount(m), said)
|
||||
}
|
||||
if len(m.events) == 0 || m.events[len(m.events)-1].Action != ActionDrop {
|
||||
t.Errorf("%s: raised %+v", name, m.events)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 1826 review r1: a later head without the declaration drops the value, so the merge has nothing to apply.
|
||||
func TestALaterHeadWithoutTheDeclarationLeavesNothingToApplyAtTheMerge(t *testing.T) {
|
||||
c, m, r, _ := aHoldingMesh(t)
|
||||
tell(t, c, thePull, "abc1234def", n8nPullDeclares())
|
||||
holdIt(t, c, m, r)
|
||||
tell(t, c, thePull, "bbbbbbbbbb", map[string]any{})
|
||||
c.HeardPull(PullEvent{Owner: "novox", Repo: "mesh-catalog", Number: 434, Merged: true, HeadSHA: "bbbbbbbbbb"}, true)
|
||||
if _, set := storedValue(t, m, "module.ace.n8n.media-gid"); set {
|
||||
t.Error("a value held for a declaration the merged head lacks was set")
|
||||
}
|
||||
}
|
||||
|
||||
// 1826 review r1: a close the forge cannot be asked about drops nothing; the five-minute look decides.
|
||||
func TestACloseThatTheForgeCannotBeAskedAboutDropsNothing(t *testing.T) {
|
||||
c, m, r, f := aHoldingMesh(t)
|
||||
tell(t, c, thePull, "abc1234def", n8nPullDeclares())
|
||||
holdIt(t, c, m, r)
|
||||
f.err = errors.New("the forge is down")
|
||||
c.HeardPull(PullEvent{Owner: "novox", Repo: "mesh-catalog", Number: 434}, false)
|
||||
if keptCount(m) != 1 {
|
||||
t.Fatal("a close the forge could not confirm dropped the value")
|
||||
}
|
||||
f.err = nil
|
||||
f.state[thePull] = [2]bool{false, false}
|
||||
c.SweepPulls()
|
||||
if keptCount(m) != 0 {
|
||||
t.Error("the look did not drop it")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,134 @@
|
||||
//go:build integration
|
||||
|
||||
package main
|
||||
|
||||
// Issue 1982, against a real bus: mesh-config whole asks the bus for nothing while no input of it changes, and a changed input
|
||||
// resolves only what it touches. The binary runs as the node's tool runtime plays it (whole_integration_test.go), and every
|
||||
// request it makes of the runtime is counted: what a state read, a watch and a write ask of the bus.
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/nats-io/nats.go"
|
||||
"github.com/nats-io/nats.go/jetstream"
|
||||
|
||||
"git.novox.be/novox/mesh-sdk/go/testbus"
|
||||
)
|
||||
|
||||
func (p *player) snapshot() (calls map[string]int, puts []string) {
|
||||
p.mu.Lock()
|
||||
defer p.mu.Unlock()
|
||||
calls = map[string]int{}
|
||||
for k, v := range p.calls {
|
||||
calls[k] = v
|
||||
}
|
||||
return calls, slices.Clone(p.puts)
|
||||
}
|
||||
|
||||
// asked is how many requests of these methods the module made between two snapshots.
|
||||
func asked(before, after map[string]int, methods ...string) int {
|
||||
n := 0
|
||||
for _, m := range methods {
|
||||
n += after[m] - before[m]
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
func TestConfigWholeAsksTheBusForNothingWhileNoInputChanges(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
t.Cleanup(cancel)
|
||||
bin := filepath.Join(t.TempDir(), Module)
|
||||
build := exec.Command("go", "build", "-o", bin, ".")
|
||||
build.Stderr = os.Stderr
|
||||
if err := build.Run(); err != nil {
|
||||
t.Fatalf("mesh-config does not build: %v", err)
|
||||
}
|
||||
nc, err := nats.Connect(testbus.URL(t))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(nc.Close)
|
||||
js, err := jetstream.New(nc)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
buckets := []string{"mesh-delivery-assigner_assignments", "mesh-delivery-assigner_wants", "mesh-delivery-indexer_predefined"}
|
||||
for _, s := range []string{ValuesState, ResolvedState, PredefinedState, ProposalsState, DeclaredState,
|
||||
RenamesState, RetiringState, FindingsState} {
|
||||
buckets = append(buckets, Module+"_"+s)
|
||||
}
|
||||
for _, b := range buckets {
|
||||
if _, err := js.CreateKeyValue(ctx, jetstream.KeyValueConfig{Bucket: b, History: 2}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
haves := map[string]string{"g14.player": fpOf("player", fp1), "novox.player": fpOf("player", fp1), "g14.radio": fpOf("radio", fp1)}
|
||||
p := start(t, bin, js, newStandIn(), haves, nil)
|
||||
p.viewsReady()
|
||||
|
||||
const radio = `{"module":"radio","config":{"band":{"type":"number","default":1}}}`
|
||||
p.manifestIn("player", oldPlayer)
|
||||
p.manifestIn("radio", radio)
|
||||
p.buildIn("g14", "player", fpOf("player", fp1), oldPlayer)
|
||||
p.buildIn("novox", "player", fpOf("player", fp1), oldPlayer)
|
||||
p.buildIn("g14", "radio", fpOf("radio", fp1), radio)
|
||||
p.placed("g14", "player", "radio")
|
||||
p.placed("novox", "player")
|
||||
p.hearsTheViews()
|
||||
eventually(t, "every value is resolved", func() bool {
|
||||
a, okA := resolvedValue(p, "g14", "player", "volume")
|
||||
b, okB := resolvedValue(p, "novox", "player", "volume")
|
||||
c, okC := resolvedValue(p, "g14", "radio", "band")
|
||||
return okA && okB && okC && a == "5" && b == "5" && c == "1"
|
||||
})
|
||||
// The passes the setup moved have ended when the module answers a tool: it takes its lock for none.
|
||||
if _, err := p.call(ResyncTool, nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// No input changes: the delivery says it delivered and a node-engine says what it reads, with no rename window open and no retirement
|
||||
// waiting. Each is heard and none is a pass: nothing is asked of the bus. (The module has no polling timer; its one deadline
|
||||
// timer is set for a real deadline, and none is near.)
|
||||
before, putsBefore := p.snapshot()
|
||||
for i := 0; i < 20; i++ {
|
||||
p.event("mesh-delivery-view.delivered", map[string]any{"revision": "r"})
|
||||
p.event("node-engine.manifest-reads-changed", map[string]any{"node": "g14"})
|
||||
}
|
||||
after, putsAfter := p.snapshot()
|
||||
p.noGitAsk()
|
||||
if n := asked(before, after, "mesh/state.get", "mesh/state.keys", "mesh/state.watch", "mesh/state.put", "mesh/state.create",
|
||||
"mesh/state.update", "mesh/state.delete", "mesh/ask"); n != 0 || len(putsAfter) != len(putsBefore) {
|
||||
t.Errorf("40 events that move nothing asked %d requests of the bus (%v); wrote %v", n, after, putsAfter[len(putsBefore):])
|
||||
}
|
||||
|
||||
// One input changes: player's manifest declares a second value. Only player's values on its two nodes are written, and `declared`
|
||||
// for player; radio's resolved values are not written again, and nothing is read key by key.
|
||||
before, putsBefore = p.snapshot()
|
||||
const gain = `{"module":"player","config":{"volume":{"type":"number","default":5},"gain":{"type":"number","default":2}}}`
|
||||
p.manifestIn("player", gain)
|
||||
eventually(t, "the new value is resolved on both nodes", func() bool {
|
||||
a, okA := resolvedValue(p, "g14", "player", "gain")
|
||||
b, okB := resolvedValue(p, "novox", "player", "gain")
|
||||
return okA && okB && a == "2" && b == "2"
|
||||
})
|
||||
after, putsAfter = p.snapshot()
|
||||
var wrote []string
|
||||
for _, w := range putsAfter[len(putsBefore):] {
|
||||
wrote = append(wrote, strings.TrimPrefix(w, "mesh/state."))
|
||||
}
|
||||
for _, w := range wrote {
|
||||
if !strings.Contains(w, ResolvedState+"/g14.player.") && !strings.Contains(w, ResolvedState+"/novox.player.") &&
|
||||
!strings.Contains(w, DeclaredState+"/player") {
|
||||
t.Errorf("a changed manifest of player wrote %q, which is not player's", w)
|
||||
}
|
||||
}
|
||||
if n := asked(before, after, "mesh/state.get", "mesh/state.keys", "mesh/state.watch"); n != 0 {
|
||||
t.Errorf("a changed manifest read %d keys of the bus's states one by one (%v)", n, after)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,353 @@
|
||||
package main
|
||||
|
||||
// What mesh-config asks of the bus per input (issue 1982): its own states are read from the copy their watches keep, an event
|
||||
// that moves nothing reads nothing, and the one timer left is a deadline.
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
occ "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"
|
||||
stdio "git.novox.be/novox/mesh-sdk/go"
|
||||
)
|
||||
|
||||
// countingStore counts the reads of the states by state: what the bus would be asked.
|
||||
type countingStore struct {
|
||||
*memStore
|
||||
mu sync.Mutex
|
||||
reads map[string]int
|
||||
}
|
||||
|
||||
func (s *countingStore) Get(state, key string) (*Entry, error) {
|
||||
s.mu.Lock()
|
||||
s.reads[state]++
|
||||
s.mu.Unlock()
|
||||
return s.memStore.Get(state, key)
|
||||
}
|
||||
|
||||
func (s *countingStore) Keys(state string) ([]string, error) {
|
||||
s.mu.Lock()
|
||||
s.reads[state]++
|
||||
s.mu.Unlock()
|
||||
return s.memStore.Keys(state)
|
||||
}
|
||||
|
||||
// readsOf is the reads of every state but those named.
|
||||
func (s *countingStore) readsExcept(states ...string) map[string]int {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
out := map[string]int{}
|
||||
for k, n := range s.reads {
|
||||
out[k] = n
|
||||
}
|
||||
for _, k := range states {
|
||||
delete(out, k)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// **An event that moves nothing reads nothing but what it judges** (design 32 *A config value renamed*, *A value no longer
|
||||
// read*): with no rename window open, `delivered` and `manifest-reads-changed` read no source, and a `manifest-accepted`
|
||||
// whose rows are all marked or read again reads none either. Each was a read of every key of every state of the module
|
||||
// (about 4,000 requests) under a delivery's burst of events.
|
||||
func TestAnEventThatMovesNothingReadsNoSource(t *testing.T) {
|
||||
c, m, d, _, _, _ := retiringMesh(t)
|
||||
cs := &countingStore{memStore: m, reads: map[string]int{}}
|
||||
c.Store = cs
|
||||
d.rows = []ReaderRow{{Module: "player", Key: "volume", Kind: KindValue, Readers: []string{"g14"}}} // read: nothing to retire
|
||||
for name, event := range map[string]func(){
|
||||
"delivered": func() { c.Windows("") },
|
||||
"manifest-reads-changed": func() { c.Windows("g14") },
|
||||
"manifest-accepted": func() { c.Retirements("player") },
|
||||
} {
|
||||
cs.reads = map[string]int{}
|
||||
event()
|
||||
if got := cs.readsExcept(RenamesState, RetiringState); len(got) != 0 {
|
||||
t.Errorf("%s read %v", name, got)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// An open window is judged, and reads what judging it needs: the same event with a window open reads the sources.
|
||||
func TestAnEventWithAWindowOpenJudgesIt(t *testing.T) {
|
||||
c, m, _, _ := aRenamingMesh(t)
|
||||
m.declare(t, "player", newPlayer)
|
||||
if _, err := c.Reconcile(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
cs := &countingStore{memStore: m, reads: map[string]int{}}
|
||||
c.Store = cs
|
||||
c.Windows("")
|
||||
if cs.reads[ValuesState] == 0 {
|
||||
t.Errorf("an open window was judged on no source: %v", cs.reads)
|
||||
}
|
||||
}
|
||||
|
||||
// **An ask of a retirement that ends with no choice is taken when the router says so** (design 32 *A value no longer read*):
|
||||
// with no minute's look at the router's record, the `decided` event of an ask that expired moves the retirement.
|
||||
func TestARetirementWhoseAskExpiredIsTakenOnTheRoutersWord(t *testing.T) {
|
||||
c, m, d, r, _, _ := retiringMesh(t)
|
||||
for _, node := range []string{"g14", "novox"} {
|
||||
m.want(t, node, "player", fp3, donePlayer)
|
||||
d.haves[node+".player"] = fp3
|
||||
}
|
||||
c.Windows("")
|
||||
c.Retirements("player")
|
||||
rows := retiringOf(t, c)
|
||||
if len(rows) != 1 || rows[0].State != RetireAsked {
|
||||
t.Fatalf("retiring %+v, want asked", rows)
|
||||
}
|
||||
r.choose(t, rows[0].Ask, OptionApprove, func(w *oc.Warrant) { w.Outcome = occ.OutcomeExpired })
|
||||
c.HeardEnd(oc.Warrant{Ask: rows[0].Ask, Asker: Module, Outcome: occ.OutcomeExpired})
|
||||
if rows = retiringOf(t, c); rows[0].State != RetireUnanswered {
|
||||
t.Errorf("after the router said the ask expired: %+v", rows)
|
||||
}
|
||||
}
|
||||
|
||||
// The next deadline is the earliest of a waiting proposal's (an hour past its ask lapsing, with no record at the router)
|
||||
// and an unanswered retirement's (asked once more after HeldFinding); none when nothing waits on a time.
|
||||
func TestTheNextDeadlineIsTheEarliestOfWhatWaitsOnATime(t *testing.T) {
|
||||
m := newStore()
|
||||
c := &Config{Store: m, Catalog: m, Logf: t.Logf}
|
||||
if _, ok := c.NextDeadline(); ok {
|
||||
t.Fatal("a deadline with nothing waiting")
|
||||
}
|
||||
t0 := time.Date(2026, 11, 19, 10, 0, 0, 0, time.UTC)
|
||||
put := func(state, key string, v any) {
|
||||
if err := m.Put(state, key, v); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
put(ProposalsState, "p1", Proposal{ID: "p1", State: ProposalWaiting, At: t0.Add(48 * time.Hour)})
|
||||
put(ProposalsState, "p2", Proposal{ID: "p2", State: ProposalEnded, At: t0})
|
||||
want := t0.Add(48*time.Hour + ProposalLasts + time.Hour)
|
||||
if at, ok := c.NextDeadline(); !ok || !at.Equal(want) {
|
||||
t.Errorf("deadline %v, %v; want %v", at, ok, want)
|
||||
}
|
||||
put(RetiringState, "player.volume", Retiring{Module: "player", Key: "volume", State: RetireUnanswered, Asked: t0.Add(-6 * 24 * time.Hour)})
|
||||
want = t0.Add(HeldFinding - 6*24*time.Hour)
|
||||
if at, ok := c.NextDeadline(); !ok || !at.Equal(want) {
|
||||
t.Errorf("deadline %v, %v; want the retirement's %v", at, ok, want)
|
||||
}
|
||||
}
|
||||
|
||||
// busOf is a bus of states for the copy's tests: what each state holds, the watches handed out and the requests asked.
|
||||
type busOf struct {
|
||||
mu sync.Mutex
|
||||
held map[string]map[string][]byte
|
||||
handles map[string]func(stdio.StateChange) error
|
||||
watches int
|
||||
gets int
|
||||
writes int
|
||||
revision uint64
|
||||
}
|
||||
|
||||
func (b *busOf) ownCopy() *ownCopy {
|
||||
return &ownCopy{
|
||||
copy: stdio.WatchedStatesOver(func(state string, handle func(stdio.StateChange) error) error {
|
||||
b.mu.Lock()
|
||||
b.watches++
|
||||
b.handles[state] = handle
|
||||
current := map[string][]byte{}
|
||||
for k, v := range b.held[state] {
|
||||
current[k] = v
|
||||
}
|
||||
b.mu.Unlock()
|
||||
for k, v := range current {
|
||||
if err := handle(stdio.StateChange{State: state, Key: k, Op: stdio.OpPut, Value: v, Revision: 1, Current: true}); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}, func(state, key string) ([]byte, bool, error) {
|
||||
b.mu.Lock()
|
||||
defer b.mu.Unlock()
|
||||
b.gets++
|
||||
v, ok := b.held[state][key]
|
||||
return v, ok, nil
|
||||
}),
|
||||
put: func(state, key string, value any) (uint64, error) {
|
||||
raw, _ := json.Marshal(value)
|
||||
b.mu.Lock()
|
||||
defer b.mu.Unlock()
|
||||
b.writes++
|
||||
b.revision++
|
||||
if b.held[state] == nil {
|
||||
b.held[state] = map[string][]byte{}
|
||||
}
|
||||
b.held[state][key] = raw
|
||||
return b.revision + 1, nil
|
||||
},
|
||||
del: func(state, key string) error {
|
||||
b.mu.Lock()
|
||||
defer b.mu.Unlock()
|
||||
b.writes++
|
||||
delete(b.held[state], key)
|
||||
return nil
|
||||
}}
|
||||
}
|
||||
|
||||
// **The module's own states are read from a copy, never key by key** (ADR 0580 §1, issue 1982): a whole state read of 1,176
|
||||
// keys is one watch and no get, again and again; this process's write is in the copy at once, another's arrives by the watch.
|
||||
func TestAnOwnStateIsReadFromItsWatchedCopyNotKeyByKey(t *testing.T) {
|
||||
b := &busOf{held: map[string]map[string][]byte{ValuesState: {}}, handles: map[string]func(stdio.StateChange) error{}}
|
||||
for i := 0; i < 1176; i++ {
|
||||
b.held[ValuesState]["mesh.key-"+time.Duration(i).String()] = []byte(`{"value":1}`)
|
||||
}
|
||||
n := len(b.held[ValuesState])
|
||||
own := b.ownCopy()
|
||||
for pass := 0; pass < 3; pass++ {
|
||||
keys, err := own.keys(ValuesState)
|
||||
if err != nil || len(keys) != n {
|
||||
t.Fatalf("keys %d, %v; want %d", len(keys), err, n)
|
||||
}
|
||||
for _, k := range keys {
|
||||
if e, err := own.get(ValuesState, k); err != nil || e == nil {
|
||||
t.Fatalf("get %s: %v, %v", k, e, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
if b.watches != 1 || b.gets != 0 {
|
||||
t.Errorf("3 passes over %d keys asked the bus for %d watches and %d gets; want 1 and 0", n, b.watches, b.gets)
|
||||
}
|
||||
// Its own write and removal are in the copy at once.
|
||||
if err := own.write(ValuesState, "mine", map[string]string{"x": "mine"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if e, _ := own.get(ValuesState, "mine"); e == nil {
|
||||
t.Error("a key just written is not read")
|
||||
}
|
||||
if err := own.remove(ValuesState, "mine"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if e, _ := own.get(ValuesState, "mine"); e != nil {
|
||||
t.Error("a key just removed is still read")
|
||||
}
|
||||
// Another writer's change arrives by the watch.
|
||||
if err := b.handles[ValuesState](stdio.StateChange{State: ValuesState, Key: "theirs", Op: stdio.OpPut, Value: []byte(`{"value":2}`), Revision: 99}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if e, _ := own.get(ValuesState, "theirs"); e == nil {
|
||||
t.Error("a change another wrote is not read")
|
||||
}
|
||||
if b.gets != 0 {
|
||||
t.Errorf("%d gets", b.gets)
|
||||
}
|
||||
}
|
||||
|
||||
// A write and a removal tell what waits on a deadline, once each.
|
||||
func TestAWriteTellsWhatWaitsOnADeadline(t *testing.T) {
|
||||
b := &busOf{held: map[string]map[string][]byte{}, handles: map[string]func(stdio.StateChange) error{}}
|
||||
own := b.ownCopy()
|
||||
var told []string
|
||||
own.changed = func(state string) { told = append(told, state) }
|
||||
if err := own.write(ProposalsState, "p", Proposal{ID: "p"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := own.remove(ProposalsState, "p"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(told) != 2 || told[0] != ProposalsState {
|
||||
t.Errorf("told %v", told)
|
||||
}
|
||||
}
|
||||
|
||||
// The mesh knows no pull request (design 47 G; ADR 0502 ended by the operator's word, record pending): the verb takes no `pull`,
|
||||
// `head`, `declares` or `pulls`, and does neither `pull-declares` nor `held`.
|
||||
func TestTheVerbKnowsNoPullRequest(t *testing.T) {
|
||||
for _, arg := range []string{"pull", "head", "declares", "pulls"} {
|
||||
if _, err := callOf(map[string]any{"action": "propose", arg: "x"}); err == nil || !strings.Contains(err.Error(), arg) {
|
||||
t.Errorf("an argument %s was taken: %v", arg, err)
|
||||
}
|
||||
}
|
||||
c, _ := aMesh(t)
|
||||
for _, action := range []string{"pull-declares", "held"} {
|
||||
if _, err := c.Do(Call{Action: action}); err == nil || !strings.Contains(err.Error(), action) {
|
||||
t.Errorf("the action %s was done: %v", action, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// The deadline timer's floor is the setting deadline-floor-seconds, the default for a value that is not a positive number.
|
||||
func TestTheDeadlineFloorIsTheSettingOrItsDefault(t *testing.T) {
|
||||
for _, c := range []struct {
|
||||
raw string
|
||||
err error
|
||||
want time.Duration
|
||||
}{
|
||||
{`5`, nil, 5 * time.Second}, {`0`, nil, DefaultDeadlineFloor}, {`"x"`, nil, DefaultDeadlineFloor},
|
||||
{``, errors.New("not declared"), DefaultDeadlineFloor}, {`90`, nil, 90 * time.Second},
|
||||
} {
|
||||
if got := floorOf(json.RawMessage(c.raw), c.err); got != c.want {
|
||||
t.Errorf("floor of %q (%v) = %v, want %v", c.raw, c.err, got, c.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// **A rename window held seven days comes as a finding in a quiet mesh** (lead answer A-490l): the deadline is the window's
|
||||
// seventh day, the finding is written at it with no resolution pass, and a window that is not yet seven days old writes none.
|
||||
func TestAHeldWindowIsAFindingAtItsDeadlineWithNoPass(t *testing.T) {
|
||||
c, m, _, clock := aRenamingMesh(t)
|
||||
m.declare(t, "player", newPlayer)
|
||||
if _, err := c.Reconcile(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
at, ok := c.NextDeadline()
|
||||
if !ok || !at.Equal(clock.Add(HeldFinding)) {
|
||||
t.Fatalf("deadline %v, %v; want the window's seventh day %v", at, ok, clock.Add(HeldFinding))
|
||||
}
|
||||
if err := c.JudgeHeldWindows(); err != nil || len(findingsOf(t, m, FindingWindowHeld)) != 0 {
|
||||
t.Fatalf("a young window wrote a finding (%v)", err)
|
||||
}
|
||||
*clock = at
|
||||
cs := &countingStore{memStore: m, reads: map[string]int{}}
|
||||
c.Store = cs
|
||||
if err := c.JudgeHeldWindows(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if fs := findingsOf(t, m, FindingWindowHeld); len(fs) != 2 {
|
||||
t.Errorf("findings %+v, want one for each node holding the window", fs)
|
||||
}
|
||||
if got := cs.readsExcept(RenamesState); len(got) != 0 {
|
||||
t.Errorf("judging the held windows read %v", got)
|
||||
}
|
||||
if _, ok := c.NextDeadline(); ok {
|
||||
t.Error("a deadline is still set once the finding is written")
|
||||
}
|
||||
}
|
||||
|
||||
// A write of a proposal, a retirement or a rename window sets the deadline timer again, and no other state does: a window opened
|
||||
// after start, with nothing else waiting on a time, must not leave the timer blocked (A-490l).
|
||||
func TestAWriteOfARenameWindowRearmsTheDeadlineTimer(t *testing.T) {
|
||||
for state, want := range map[string]bool{ProposalsState: true, RetiringState: true, RenamesState: true,
|
||||
ValuesState: false, ResolvedState: false, DeclaredState: false} {
|
||||
if got := armsDeadline(state); got != want {
|
||||
t.Errorf("a write of %s arms the timer: %v, want %v", state, got, want)
|
||||
}
|
||||
}
|
||||
// Wired as main wires it: the copy's write of a window tells the channel the timer waits on.
|
||||
b := &busOf{held: map[string]map[string][]byte{}, handles: map[string]func(stdio.StateChange) error{}}
|
||||
own := b.ownCopy()
|
||||
rearm := make(chan struct{}, 1)
|
||||
own.changed = func(state string) {
|
||||
if armsDeadline(state) {
|
||||
select {
|
||||
case rearm <- struct{}{}:
|
||||
default:
|
||||
}
|
||||
}
|
||||
}
|
||||
if err := own.write(RenamesState, "player.level", Rename{Module: "player"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
select {
|
||||
case <-rearm:
|
||||
default:
|
||||
t.Error("a rename window written after start did not rearm the timer")
|
||||
}
|
||||
}
|
||||
@@ -293,14 +293,26 @@ func (c *Config) Windows(node string) []string {
|
||||
return said
|
||||
}
|
||||
|
||||
// windowOpen says whether any rename holds a window open on the node named, or on any node when none is named.
|
||||
func windowOpen(renames map[string]Rename, only string) bool {
|
||||
for _, r := range renames {
|
||||
for _, node := range r.Open() {
|
||||
if only == "" || only == node {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// closeWindows is Windows' rename half; the caller holds the lock.
|
||||
func (c *Config) closeWindows(only string) []string {
|
||||
renames, err := c.readRenames()
|
||||
if err != nil {
|
||||
return []string{"the renames cannot be read: " + err.Error()}
|
||||
}
|
||||
if c.Delivery == nil {
|
||||
return nil
|
||||
if c.Delivery == nil || !windowOpen(renames, only) {
|
||||
return nil // no window to judge: the delivery's word moves nothing, and no source is read for it (issue 1982)
|
||||
}
|
||||
s, err := c.sources()
|
||||
if err != nil {
|
||||
@@ -475,6 +487,20 @@ func (c *Config) RenameClose(module, to, node, why string) (any, error) {
|
||||
return map[string]any{"closed": node, "open": r.Open()}, nil
|
||||
}
|
||||
|
||||
// JudgeHeldWindows writes the finding of each window held open past HeldFinding, and clears one that is not, from the renames
|
||||
// kept: run at start and at the deadline the next window reaches (NextDeadline), so the finding comes in a quiet mesh too (lead
|
||||
// answer A-490l); it reads no source and asks nothing.
|
||||
func (c *Config) JudgeHeldWindows() error {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
renames, err := c.readRenames()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
c.sweepRenameFindings(&Sources{Renames: renames}, c.now())
|
||||
return nil
|
||||
}
|
||||
|
||||
// sweepRenameFindings writes a finding for each window held open past HeldFinding, naming the node, and clears one
|
||||
// whose window closed.
|
||||
func (c *Config) sweepRenameFindings(s *Sources, now time.Time) {
|
||||
|
||||
@@ -140,10 +140,6 @@ type Sources struct {
|
||||
Unresolved []Unresolved
|
||||
Predefined map[string]Published
|
||||
Values map[string]Value
|
||||
// Pulls is what the controller told of each open pull request's declarations, and Kept the approved values held
|
||||
// for them (pulls.go); neither is resolved.
|
||||
Pulls map[string]PullDecl
|
||||
Kept map[string]KeptValue
|
||||
// Renames are the renames kept, by `<module>.<to>`, whose open windows answer the old name (renames.go).
|
||||
Renames map[string]Rename
|
||||
}
|
||||
|
||||
@@ -148,9 +148,15 @@ func (c *Config) Retirements(module string) []string {
|
||||
if err != nil {
|
||||
return []string{"the retirements cannot be read: " + err.Error()}
|
||||
}
|
||||
s, err := c.sources()
|
||||
if err != nil {
|
||||
return []string{"the retirements cannot be judged: " + err.Error()}
|
||||
// The sources are read only when a row is one to judge: a row already marked, or read again, needs none (issue 1982).
|
||||
var s Sources
|
||||
for _, row := range rows {
|
||||
if _, has := held[retiringKey(module, row.Key)]; (row.Module == "" || row.Module == module) && (len(row.Readers) == 0 && !has) {
|
||||
if s, err = c.sources(); err != nil {
|
||||
return []string{"the retirements cannot be judged: " + err.Error()}
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
var said []string
|
||||
for _, row := range rows {
|
||||
|
||||
@@ -51,9 +51,6 @@ func (c *Config) sources() (Sources, error) {
|
||||
}); err != nil {
|
||||
return s, fmt.Errorf("the values cannot be read: %w", err)
|
||||
}
|
||||
if err := c.readPulls(&s); err != nil {
|
||||
return s, err
|
||||
}
|
||||
if renames, err := c.readRenames(); err != nil {
|
||||
return s, fmt.Errorf("the renames cannot be read: %w", err)
|
||||
} else {
|
||||
@@ -339,10 +336,8 @@ type Resolution struct {
|
||||
Moved []Moved `json:"moved"`
|
||||
Published string `json:"published,omitempty"`
|
||||
Withheld string `json:"withheld,omitempty"`
|
||||
// Declared are the modules whose `declared` key was written, and Candidates what the indexer's config candidates
|
||||
// did after the take-over.
|
||||
Declared []string `json:"declared,omitempty"`
|
||||
Candidates []string `json:"candidates,omitempty"`
|
||||
// Declared are the modules whose `declared` key was written.
|
||||
Declared []string `json:"declared,omitempty"`
|
||||
// Renamed is what the renames did in this pass.
|
||||
Renamed []string `json:"renamed,omitempty"`
|
||||
// SourceGone is said while the stored switch still names the controller, whose records are no longer read.
|
||||
@@ -383,9 +378,6 @@ func (c *Config) reconcileLocked() (Resolution, error) {
|
||||
} else if len(written) > 0 {
|
||||
r.Declared = written
|
||||
}
|
||||
if sw, serr := c.source(); serr == nil && sw.Source == SourceIndexer {
|
||||
r.Candidates = c.takeCandidates()
|
||||
}
|
||||
return r, err
|
||||
}
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@ package main
|
||||
|
||||
// Config's records out of the controller (novox/hq ADR 0565 §3, §15, and the progressive insight of 2026-10-29):
|
||||
// mesh-config keeps each placed module's declarations in its own state `declared`, reads the list of predefined keys
|
||||
// from the indexer's `predefined` and a pull request's config from the indexer's `config-candidates`.
|
||||
// from the indexer's `predefined`. A pull request's config is not read: the mesh knows no pull request (design 47 G).
|
||||
//
|
||||
// **Its own sources hold always** (ADR 0590, issue 1126): the controller ended by its unassignment, so its pull-declares,
|
||||
// the take-over that switched away from them, compare and hand-back are gone, and the switch is the indexer's whatever
|
||||
@@ -23,12 +23,11 @@ const (
|
||||
// DeclaredState is each placed module's declarations at the trunk, one key per module (ADR 0565 §3).
|
||||
DeclaredState = "declared"
|
||||
|
||||
// IndexerPredefinedRead and CandidatesRead are the indexer's states (ADR 0565 §2).
|
||||
// IndexerPredefinedRead is the indexer's state of the predefined list (ADR 0565 §2).
|
||||
IndexerPredefinedRead = indexerModule + "." + catalog.StatePredefined
|
||||
CandidatesRead = indexerModule + "." + catalog.StateConfigCandidates
|
||||
)
|
||||
|
||||
// SourceIndexer is the one source of a pull request's config and of the predefined list: the indexer's.
|
||||
// SourceIndexer is the one source of the predefined list: the indexer's.
|
||||
const SourceIndexer = "indexer"
|
||||
|
||||
// Switch is which source holds: the indexer's, always, since the controller ended (ADR 0590).
|
||||
@@ -105,7 +104,7 @@ func (c *Config) writeDeclared(s Sources) ([]string, error) {
|
||||
return written, errors.Join(problems...)
|
||||
}
|
||||
|
||||
// ---- the predefined list and the pull requests from the indexer ------------------------------------------
|
||||
// ---- the predefined list from the indexer -----------------------------------------------------------------------------------------
|
||||
|
||||
// indexerList is the indexer's predefined list as the mirror's list holds one: the keys, and the commit.
|
||||
func (c *Config) indexerList() (PredefinedList, bool, error) {
|
||||
@@ -128,75 +127,6 @@ func (c *Config) indexerList() (PredefinedList, bool, error) {
|
||||
return list, true, nil
|
||||
}
|
||||
|
||||
// candidates is every open pull request's config declarations as the indexer keeps them, by pull key.
|
||||
func (c *Config) candidates() (map[string]PullDecl, error) {
|
||||
out := map[string]PullDecl{}
|
||||
err := c.each(CandidatesRead, func(key string, raw []byte) error {
|
||||
var cand catalog.ConfigCandidatesByPullValue
|
||||
if err := json.Unmarshal(raw, &cand); err != nil || cand.Pull == "" {
|
||||
return fmt.Errorf("the indexer's config candidate %s is not one", key)
|
||||
}
|
||||
p := PullDecl{Pull: cand.Pull, Head: cand.Head, Declares: map[string]map[string]Declaration{}, At: timeOf(cand.At)}
|
||||
for module, keys := range cand.Declares {
|
||||
raw, _ := json.Marshal(keys)
|
||||
var d map[string]Declaration
|
||||
if err := json.Unmarshal(raw, &d); err != nil {
|
||||
return fmt.Errorf("the indexer's config candidate %s declares %s unreadably", key, module)
|
||||
}
|
||||
for k, decl := range d {
|
||||
if !decl.HasDefault() {
|
||||
decl.Default = nil
|
||||
}
|
||||
d[k] = decl
|
||||
}
|
||||
p.Declares[module] = d
|
||||
}
|
||||
out[pullKey(cand.Pull)] = p
|
||||
return nil
|
||||
})
|
||||
return out, err
|
||||
}
|
||||
|
||||
// takeCandidates is the indexer's config candidates taken in place of pull-declares, after the take-over: each one
|
||||
// whose head or declarations differ from what `pulls` holds is declared again, its held values judged again (ADR 0502
|
||||
// §6). The caller holds the lock.
|
||||
func (c *Config) takeCandidates() []string {
|
||||
cands, err := c.candidates()
|
||||
if err != nil {
|
||||
return []string{"the indexer's config candidates cannot be read: " + err.Error()}
|
||||
}
|
||||
var s Sources
|
||||
if err := c.readPulls(&s); err != nil {
|
||||
return []string{err.Error()}
|
||||
}
|
||||
var said []string
|
||||
for _, k := range sortedKeys(cands) {
|
||||
p := cands[k]
|
||||
if have, ok := s.Pulls[k]; ok && have.Head == p.Head && declaresDigest(have.Declares) == declaresDigest(p.Declares) {
|
||||
continue
|
||||
}
|
||||
a, err := c.declarePull(p.Pull, p.Head, p.Declares)
|
||||
if err != nil {
|
||||
said = append(said, fmt.Sprintf("%s from the indexer: %v", p.Pull, err))
|
||||
continue
|
||||
}
|
||||
said = append(said, a.Said+" (from the indexer)")
|
||||
}
|
||||
return said
|
||||
}
|
||||
|
||||
// declaresDigest is what a pull request's declarations hold a value to, in one encoding.
|
||||
func declaresDigest(d map[string]map[string]Declaration) string {
|
||||
flat := map[string]string{}
|
||||
for m, keys := range d {
|
||||
for k, decl := range keys {
|
||||
flat[m+"."+k] = declDigest(decl)
|
||||
}
|
||||
}
|
||||
raw, _ := json.Marshal(flat)
|
||||
return string(raw)
|
||||
}
|
||||
|
||||
// Declared is the read of `declared`: every module's key, or one whole.
|
||||
func (c *Config) Declared(module string) (any, error) {
|
||||
if module != "" {
|
||||
@@ -221,20 +151,14 @@ func (c *Config) Declared(module string) (any, error) {
|
||||
return map[string]any{"declared": rows, "count": len(rows), "source": sw}, err
|
||||
}
|
||||
|
||||
// Resync reads every source again and writes what differs: the published list, the resolved values, `declared`, and
|
||||
// the indexer's config candidates (ADR 0565 §15).
|
||||
// Resync reads every source again and writes what differs: the published list, the resolved values and `declared`
|
||||
// (ADR 0565 §15).
|
||||
func (c *Config) Resync() (any, error) {
|
||||
r, err := c.Reconcile()
|
||||
if err != nil {
|
||||
return r, err
|
||||
}
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
out := map[string]any{"resolution": r}
|
||||
if sw, err := c.source(); err == nil && sw.Source == SourceIndexer {
|
||||
out["candidates"] = c.takeCandidates()
|
||||
}
|
||||
return out, nil
|
||||
return map[string]any{"resolution": r}, nil
|
||||
}
|
||||
|
||||
// timeOf is an optional time of a seat's value as a time, the zero time when it is absent.
|
||||
|
||||
@@ -15,7 +15,7 @@ const (
|
||||
oldDeclared = `{"module":"old","settings":{"workers":{"kind":"preference","default":4,"why":"two per core"}}}`
|
||||
)
|
||||
|
||||
// anAgreeingMesh is aProposingMesh with the indexer's predefined list in place and no open pull request.
|
||||
// anAgreeingMesh is aProposingMesh with the indexer's predefined list in place.
|
||||
func anAgreeingMesh(t *testing.T) (*Config, *memStore, *standIn) {
|
||||
t.Helper()
|
||||
c, m, r := aProposingMesh(t)
|
||||
@@ -34,19 +34,9 @@ func indexerList(t *testing.T, m *memStore, commit, list string) {
|
||||
m.mirror(t, IndexerPredefinedRead, catalog.PredefinedByListKey(), string(raw))
|
||||
}
|
||||
|
||||
// candidate is the indexer's config candidate of a pull request.
|
||||
func candidate(t *testing.T, m *memStore, pull, head, declares string) {
|
||||
t.Helper()
|
||||
key, err := catalog.ConfigCandidatesByPullKey(pullKey(pull)), error(nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
m.mirror(t, CandidatesRead, key, `{"v":1,"pull":"`+pull+`","head":"`+head+`","declares":`+declares+`}`)
|
||||
}
|
||||
|
||||
// ADR 0590 (issue 1126): config reads its own sources always, with no take-over: declared is written once per
|
||||
// manifest, pull-declares is refused naming the indexer's candidates, and the list and a pull request's config come from
|
||||
// the indexer. compare, take-over and hand-back went with the controller.
|
||||
// manifest, and the list comes from the indexer. compare, take-over, hand-back and a pull request's config went (the
|
||||
// mesh knows no pull request, design 47 G).
|
||||
func TestConfigReadsItsOwnSourcesAlways(t *testing.T) {
|
||||
c, m, _ := anAgreeingMesh(t)
|
||||
steps := []struct {
|
||||
@@ -60,11 +50,6 @@ func TestConfigReadsItsOwnSourcesAlways(t *testing.T) {
|
||||
again, _ := c.Reconcile()
|
||||
return strings.Join(first.Declared, ",") + "|" + strings.Join(again.Declared, ","), err
|
||||
}, "dunst|"},
|
||||
{"pull-declares refused, naming the indexer", func() (string, error) {
|
||||
_, err := c.PullDeclares(Call{Caller: ControllerCaller, Pull: "novox/mesh-catalog#8", Head: "h1",
|
||||
Declares: map[string]any{}})
|
||||
return "", err
|
||||
}, "config-candidates"},
|
||||
{"the list comes from the indexer", func() (string, error) {
|
||||
indexerList(t, m, "cccccccccccc", `{"font-size":{"type":"number","description":"text size","minimum":6,"maximum":72},
|
||||
"agent-managed-settings":{"type":"object","description":"managed settings","trusted":true},
|
||||
@@ -72,11 +57,6 @@ func TestConfigReadsItsOwnSourcesAlways(t *testing.T) {
|
||||
res, err := c.Reconcile()
|
||||
return res.Published, err
|
||||
}, "theme"},
|
||||
{"a pull request's config comes from the indexer", func() (string, error) {
|
||||
candidate(t, m, "novox/mesh-catalog#9", "h9", `{"dunst":{"gap":{"kind":"","type":"number","default":2}}}`)
|
||||
res, err := c.Reconcile()
|
||||
return strings.Join(res.Candidates, "; "), err
|
||||
}, "from the indexer"},
|
||||
}
|
||||
for _, s := range steps {
|
||||
got, err := s.do()
|
||||
|
||||
@@ -81,6 +81,12 @@ type player struct {
|
||||
catalog map[string]catalog.CatalogEntry
|
||||
byFp map[string]catalog.CatalogEntry
|
||||
catRev int64
|
||||
// calls counts what the module asked of the runtime, by method (and by key for an ask); puts are the keys it wrote, "<state>/<key>".
|
||||
calls map[string]int
|
||||
asks map[string]int // the asks of tools and seat verbs, by key
|
||||
puts []string
|
||||
// stop ends the module this player runs: the restart of a module is the old process gone and a new one started.
|
||||
stop func()
|
||||
}
|
||||
|
||||
// ownedState is one state of an owner (the indexer's modules, the assigner's wants...), kept from its bucket.
|
||||
@@ -232,6 +238,12 @@ func (p *player) handle(m wire) {
|
||||
Bucket string `json:"bucket"`
|
||||
}
|
||||
_ = json.Unmarshal(m.Params, &a)
|
||||
p.mu.Lock()
|
||||
p.calls[m.Method]++
|
||||
if strings.HasPrefix(m.Method, "mesh/state.") && m.Method != "mesh/state.get" && m.Method != "mesh/state.keys" && m.Method != "mesh/state.watch" {
|
||||
p.puts = append(p.puts, m.Method+" "+a.State+"/"+a.Key)
|
||||
}
|
||||
p.mu.Unlock()
|
||||
kv, kvErr := p.kv(a.State)
|
||||
switch m.Method {
|
||||
case "mesh/state.get":
|
||||
@@ -357,6 +369,9 @@ func (p *player) handle(m wire) {
|
||||
Body json.RawMessage `json:"body"`
|
||||
}
|
||||
_ = json.Unmarshal(m.Params, &q)
|
||||
p.mu.Lock()
|
||||
p.asks[q.Key]++
|
||||
p.mu.Unlock()
|
||||
p.answer(m.ID, p.asked(q.Key, q.Body), nil)
|
||||
default:
|
||||
p.answer(m.ID, nil, errors.New("nats: no responders available for request"))
|
||||
@@ -675,14 +690,16 @@ func start(t *testing.T, bin string, js jetstream.JetStream, router *standIn, ha
|
||||
stdout, _ := cmd.StdoutPipe()
|
||||
cmd.Stderr = os.Stderr
|
||||
p := &player{t: t, ctx: ctx, js: js, router: router, in: stdin, pending: map[string]chan wire{}, haves: haves, readers: readers,
|
||||
owned: map[string]*ownedState{}, catalog: map[string]catalog.CatalogEntry{}, byFp: map[string]catalog.CatalogEntry{}}
|
||||
owned: map[string]*ownedState{}, catalog: map[string]catalog.CatalogEntry{}, byFp: map[string]catalog.CatalogEntry{},
|
||||
calls: map[string]int{}, asks: map[string]int{}}
|
||||
for _, f := range followed {
|
||||
p.own(f.Read)
|
||||
}
|
||||
if err := cmd.Start(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { _ = stdin.Close(); cancel(); _ = cmd.Wait() })
|
||||
p.stop = func() { _ = stdin.Close(); cancel(); _ = cmd.Wait() }
|
||||
t.Cleanup(p.stop)
|
||||
go p.serve(stdout)
|
||||
t.Cleanup(cancel)
|
||||
return p
|
||||
@@ -708,9 +725,8 @@ func TestConfigWholeRenamesAndRetiresAgainstTheBus(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
buckets := []string{"mesh-delivery-assigner_assignments", "mesh-delivery-assigner_wants",
|
||||
"mesh-delivery-indexer_predefined",
|
||||
"mesh-delivery-indexer_config-candidates"}
|
||||
for _, s := range []string{ValuesState, ResolvedState, PredefinedState, ProposalsState, PullsState, KeptState, DeclaredState,
|
||||
"mesh-delivery-indexer_predefined"}
|
||||
for _, s := range []string{ValuesState, ResolvedState, PredefinedState, ProposalsState, DeclaredState,
|
||||
RenamesState, RetiringState, FindingsState} {
|
||||
buckets = append(buckets, Module+"_"+s)
|
||||
}
|
||||
@@ -785,6 +801,7 @@ func TestConfigWholeRenamesAndRetiresAgainstTheBus(t *testing.T) {
|
||||
|
||||
// The module restarted inside the window keeps it, and says renamed no more.
|
||||
said := p.publishedCount(EventRenamed)
|
||||
p.stop()
|
||||
p2 := start(t, bin, js, router, haves, nil)
|
||||
p2.viewsReady()
|
||||
if _, err := p2.call(ResyncTool, nil); err != nil {
|
||||
@@ -869,6 +886,21 @@ func TestConfigWholeRenamesAndRetiresAgainstTheBus(t *testing.T) {
|
||||
p2.conforms(EventRetiring, &meshconfig.RetiringHolderEvent{}, meshconfig.HolderSubjectRetiring)
|
||||
p2.conforms(EventRetired, &meshconfig.RetiredHolderEvent{}, meshconfig.HolderSubjectRetired)
|
||||
eventually(t, "the retirement is dropped", func() bool { _, ok := p2.get(RetiringState, "radio.band"); return !ok })
|
||||
p.noGitAsk()
|
||||
p2.noGitAsk()
|
||||
}
|
||||
|
||||
// noGitAsk fails when the module asked the seat git for anything over the whole run (ADR 0681 clause 1: it calls no
|
||||
// `seat:git.pull`, and no other verb of the seat git is its).
|
||||
func (p *player) noGitAsk() {
|
||||
p.t.Helper()
|
||||
p.mu.Lock()
|
||||
defer p.mu.Unlock()
|
||||
for key, n := range p.asks {
|
||||
if strings.HasPrefix(key, "seat:git.") {
|
||||
p.t.Errorf("the module asked %s %d times: it holds no value for a pull request", key, n)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// streamCarries says whether the module's manifest names event among its own streams' events.
|
||||
|
||||
@@ -3,7 +3,6 @@ module mesh-config
|
||||
go 1.26.0
|
||||
|
||||
require (
|
||||
git.novox.be/novox/mesh-contracts/go/clients/meshconfigclient v0.0.0-00010101000000-000000000000
|
||||
git.novox.be/novox/mesh-contracts/go/clients/meshmoduleassignerclient v0.0.0-00010101000000-000000000000
|
||||
git.novox.be/novox/mesh-contracts/go/clients/nodeengineclient v0.0.0-00010101000000-000000000000
|
||||
git.novox.be/novox/mesh-contracts/go/seats/meshconfig v0.0.0-00010101000000-000000000000
|
||||
|
||||
@@ -19,13 +19,10 @@
|
||||
"operator-channel"
|
||||
],
|
||||
"consumes": [
|
||||
"git.pull-merged",
|
||||
"git.pull-closed",
|
||||
"mesh-delivery-view.delivered",
|
||||
"node-engine.manifest-reads-changed",
|
||||
"mesh-catalog.manifest-accepted",
|
||||
"mesh-delivery-indexer.views.predefined",
|
||||
"mesh-delivery-indexer.views.config-candidates",
|
||||
"mesh-delivery-assigner.views.assignments",
|
||||
"mesh-delivery-assigner.views.wants",
|
||||
"mesh-catalog.catalog-revision",
|
||||
@@ -36,13 +33,11 @@
|
||||
"invokes": [
|
||||
"seat:mesh-hand-acts.hand-act",
|
||||
"seat:mesh-hand-acts.warranted",
|
||||
"seat:git.pull",
|
||||
"mesh-delivery-view.node_haves",
|
||||
"seat:node-engine.manifest-reads",
|
||||
"mesh-delivery-indexer.indexer_config_readers",
|
||||
"seat:mesh-vault.retire",
|
||||
"mesh-delivery-indexer.indexer_read_predefined",
|
||||
"mesh-delivery-indexer.indexer_read_config_candidates",
|
||||
"mesh-delivery-assigner.assigner_read_assignments",
|
||||
"mesh-delivery-assigner.assigner_read_wants",
|
||||
"seat:mesh-catalog.revision",
|
||||
@@ -66,8 +61,6 @@
|
||||
"resolved",
|
||||
"predefined",
|
||||
"proposals",
|
||||
"pulls",
|
||||
"kept",
|
||||
"declared",
|
||||
"renames",
|
||||
"retiring",
|
||||
@@ -132,5 +125,16 @@
|
||||
"max-age-days": 7,
|
||||
"max-mib": 16
|
||||
}
|
||||
]
|
||||
],
|
||||
"config": {
|
||||
"deadline-floor-seconds": {
|
||||
"type": "number",
|
||||
"description": "the least wait, in seconds, of mesh-config's one deadline timer: it is set for the earliest real deadline it holds (a waiting proposal's end, an unanswered retirement's second ask, a rename window's seven-day held finding) and never sooner than this after a look, so a deadline passed and not settled is tried again, not spun on (lead answer A-490l, ADR 0681).",
|
||||
"minimum": 1,
|
||||
"maximum": 3600,
|
||||
"trusted": false,
|
||||
"default": 60,
|
||||
"runtime": true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -69,6 +69,10 @@ type DeliveryStore interface {
|
||||
type ForgeReads interface {
|
||||
OpenPulls() (gitseat.OpenPullsAnswer, error)
|
||||
Pull(repository string, number int64) (gitseat.PullAnswer, error)
|
||||
// Branch is a repository branch's head (the seat git's verb branch).
|
||||
Branch(repository, branch string) (gitseat.BranchAnswer, error)
|
||||
// Contains is whether commit is in (or is) the commit in (the seat git's verb contains).
|
||||
Contains(repository, commit, in string) (bool, error)
|
||||
}
|
||||
|
||||
// kvKept is the store over the runtime.
|
||||
|
||||
@@ -41,7 +41,10 @@ func (k *memKept) Delete(state, key string) error { delete(k.m[state], key); ret
|
||||
type fakeForge struct {
|
||||
open []gitseat.OpenPull
|
||||
pulls map[string]gitseat.Pull
|
||||
fail bool
|
||||
heads map[string]string // "<repository>@<branch>" -> head
|
||||
// contains says whether commit is in `in`; nil means every commit is in every other (a straight line forward)
|
||||
contains func(commit, in string) bool
|
||||
fail bool
|
||||
}
|
||||
|
||||
func (f *fakeForge) OpenPulls() (gitseat.OpenPullsAnswer, error) {
|
||||
@@ -50,6 +53,25 @@ func (f *fakeForge) OpenPulls() (gitseat.OpenPullsAnswer, error) {
|
||||
}
|
||||
return gitseat.OpenPullsAnswer{Pulls: f.open}, nil
|
||||
}
|
||||
func (f *fakeForge) Branch(repository, branch string) (gitseat.BranchAnswer, error) {
|
||||
if f.fail {
|
||||
return gitseat.BranchAnswer{}, errors.New("the forge is away")
|
||||
}
|
||||
head, ok := f.heads[repository+"@"+branch]
|
||||
if !ok {
|
||||
return gitseat.BranchAnswer{Error: "no such branch"}, nil
|
||||
}
|
||||
return gitseat.BranchAnswer{Branch: branch, Repository: repository, Head: head}, nil
|
||||
}
|
||||
func (f *fakeForge) Contains(repository, commit, in string) (bool, error) {
|
||||
if f.fail {
|
||||
return false, errors.New("the forge is away")
|
||||
}
|
||||
if f.contains == nil {
|
||||
return true, nil
|
||||
}
|
||||
return f.contains(commit, in), nil
|
||||
}
|
||||
func (f *fakeForge) Pull(repository string, number int64) (gitseat.PullAnswer, error) {
|
||||
p, ok := f.pulls[fmt.Sprintf("%s#%d", repository, number)]
|
||||
if !ok {
|
||||
|
||||
@@ -44,7 +44,7 @@ type FindingWriter interface {
|
||||
// generated contract of the seat that defines it (A-17). It is started once the runtime serves (A-40).
|
||||
func NewFindingWriter() (*views.HeldWriter[meshconditions.Finding, meshconditions.FindingCleared, meshconditions.FindingsStated, meshconditions.FindingsAskedEvent], error) {
|
||||
return views.NewStdioHeldWriter(views.WriterConfig[meshconditions.Finding, meshconditions.FindingCleared, meshconditions.FindingsStated, meshconditions.FindingsAskedEvent]{
|
||||
Asked: meshconditions.SubjectFindingsAsked, Module: Module, State: stdio.State("findings"), Kinds: []string{ApprovalCarriedOpen}})
|
||||
Asked: meshconditions.SubjectFindingsAsked, Module: Module, State: stdio.State("findings"), Kinds: []string{ApprovalCarriedOpen, SourceUncompared}})
|
||||
}
|
||||
|
||||
// approvalID is the id of a pull request's finding: its repository and number, each escaped as a key's name is.
|
||||
|
||||
@@ -23,11 +23,14 @@ type Holder struct {
|
||||
// Merges and Old are the old sections the switch reads (switch.go); Source is the gate's source (merges.go).
|
||||
Merges MergeStore
|
||||
// Kept is the gate's states deliveries and groups, and Forge the seat git's reads (deliveries.go, §716).
|
||||
Kept DeliveryStore
|
||||
Forge ForgeReads
|
||||
dmu sync.Mutex
|
||||
Old OldState
|
||||
Source SourceStore
|
||||
Kept DeliveryStore
|
||||
Forge ForgeReads
|
||||
// Catalog reads the seat mesh-catalog's registrations (sourcestart.go); startMu keeps one start of the sources at a time.
|
||||
Catalog Registered
|
||||
startMu sync.Mutex
|
||||
dmu sync.Mutex
|
||||
Old OldState
|
||||
Source SourceStore
|
||||
// Owed says what the gate owes the forge as the seat's event owed (owed.go).
|
||||
Owed OwedSayer
|
||||
// Records keeps the delivery records on the gate's ledger (record.go, ADR 0656 §7); nil keeps none.
|
||||
|
||||
@@ -32,7 +32,7 @@ func (l *listening) get() string { l.mu.Lock(); defer l.mu.Unlock(); return l.n
|
||||
|
||||
// The events this owner follows: the forge bridge's word on an act owed, and the step events the delivery records are built
|
||||
// from (consumer.go).
|
||||
var followed = append(append([]string{owedEndedEvent}, stepPatterns()...), pullUpdatedEvent, pullMergedEvent, pullClosedEvent)
|
||||
var followed = append(append([]string{owedEndedEvent}, stepPatterns()...), pullUpdatedEvent, pullMergedEvent, pullClosedEvent, registrationChangedEvent, forgeMergedEvent)
|
||||
|
||||
func main() {
|
||||
settingsFile := os.Getenv("MESH_DELIVERY_GATE_SETTINGS")
|
||||
@@ -68,6 +68,7 @@ func main() {
|
||||
h.Old = kvOld{}
|
||||
h.Kept = kvKept{}
|
||||
h.Forge = seatGit{ask: stdio.Ask}
|
||||
h.Catalog = seatCatalog{ask: stdio.Ask}
|
||||
h.Consumer = &StepConsumer{Records: h.Records, Families: stepFamilies, Now: h.Now, Logf: logf, Taken: h.TakenIn}
|
||||
// The gate's one held writer of its own findings (design 47 G A-39, A-37, A-40): made at once, started once the runtime serves.
|
||||
fw, err := NewFindingWriter()
|
||||
@@ -116,6 +117,14 @@ func run(h *Holder, l *listening) {
|
||||
}
|
||||
l.set("listening")
|
||||
logf("[mesh-delivery] listening: %s", strings.Join(followed, ", "))
|
||||
// A watched branch no source key holds starts from the forge, once at each start and at each registration change (A-401).
|
||||
// then each head is compared with the forge's, and a merge made outside the gate is followed (A-464).
|
||||
go func() {
|
||||
startSourcesAtStart(h)
|
||||
if _, err := h.FollowAtStart(); err != nil {
|
||||
logf("[mesh-delivery] the sources were not all compared with the forge's heads: %v", err)
|
||||
}
|
||||
}()
|
||||
// A missed pull event is put right from the forge's own record, once at each start (§716).
|
||||
if err := h.ReadForge(); err != nil {
|
||||
logf("[mesh-delivery] the open pull requests were not read from the forge, they are read again at the next start: %v", err)
|
||||
@@ -172,6 +181,13 @@ func take(h *Holder, e stdio.Envelope) error {
|
||||
if e.Key == pullMergedEvent {
|
||||
return errors.Join(h.DeliveryMerged(e.Body), h.pullEnded(e.Body, "merged"))
|
||||
}
|
||||
if e.Key == registrationChangedEvent {
|
||||
_, err := h.StartSources()
|
||||
return err
|
||||
}
|
||||
if forgeMergedEvent != "" && stdio.TopicMatches(forgeMergedEvent, e.Key) {
|
||||
return h.ForgeMerged(e.Body)
|
||||
}
|
||||
if e.Key == pullClosedEvent {
|
||||
return errors.Join(h.DeliveryClosed(e.Body), h.pullEnded(e.Body, "closed"))
|
||||
}
|
||||
|
||||
@@ -134,13 +134,16 @@ func TestItClaimsTheDeliverySeatAndFollowsWhatItHandles(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
// From G's delivery the gate hears neither the bridge's merged and pushed messages nor the teller's answers nor the view's
|
||||
// reach (design 47 G §592, §593): it consumes the indexer's take-in.taken and the bridge's owed.ended, and the git events that clear
|
||||
// From G's delivery the gate hears neither the bridge's pushed messages nor the teller's answers nor the view's reach
|
||||
// (design 47 G §592, §593); it hears the bridge's merged message alone, to follow every merge (A-464): it consumes the indexer's take-in.taken and the bridge's owed.ended, and the git events that clear
|
||||
// an approval carried.
|
||||
if want := append(slices.Clone(followed), meshconditions.Seat+"."+meshconditions.EventFindingsAsked); !reflect.DeepEqual(m.Consumes, want) {
|
||||
t.Fatalf("consumes %v, follows %v", m.Consumes, followed)
|
||||
}
|
||||
for _, c := range m.Consumes {
|
||||
if c == forgeMergedEvent {
|
||||
continue // A-464: the source follows every merge, so the gate hears the forge bridge's merged message, and it alone
|
||||
}
|
||||
for _, gone := range []string{".forge.", "teller", "view", "forge-pull", "forge-status"} {
|
||||
if strings.Contains(c, gone) {
|
||||
t.Errorf("the manifest still consumes %s, which G's gate does not hear (§431, §592, §593)", c)
|
||||
@@ -181,7 +184,7 @@ func TestItClaimsTheDeliverySeatAndFollowsWhatItHandles(t *testing.T) {
|
||||
}
|
||||
// It calls no controller verb and no forge verb: the forge's reads and writes are the bridge's (ADR 0437 §8; design 47 §592).
|
||||
// It uses operator-channel only to say its conditions (ADR 0437 §9, issue 1275): the teller asks no more (§431).
|
||||
want := []string{"seat:git.open-pulls", "seat:git.pull", "seat:mesh-hand-acts.hand-act", WantsEntryVerb, EngineCompositionVerb}
|
||||
want := []string{"seat:git.branch", "seat:git.open-pulls", "seat:git.pull", "seat:mesh-catalog.registrations", "seat:mesh-hand-acts.hand-act", WantsEntryVerb, EngineCompositionVerb}
|
||||
if !reflect.DeepEqual(m.Uses, []string{"operator-channel"}) {
|
||||
t.Fatalf("uses %v: operator-channel, for its conditions alone", m.Uses)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,278 @@
|
||||
package main
|
||||
|
||||
// The gate's source of a watched branch follows every merge into that branch, whoever made it (design 47 G A-464, A-464b, amending
|
||||
// A-463): on the forge bridge's `merged` of a repository the source names, at the branch the source watches, the gate moves that
|
||||
// repository's head to the merge and says source-moved, as a take-in moves it; at its start it compares each head with the
|
||||
// forge's branch head, asked of the seat git, and moves every one behind it the same way, so a merge whose message was lost is
|
||||
// followed too; gate_source_follow does the same for one repository now. A head the source does not name, or a branch it does
|
||||
// not watch, is left: only a start or a registration change adds them (sourcestart.go).
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshhandactsclient"
|
||||
"git.novox.be/novox/mesh-contracts/health-and-repair/go/seats/meshconditions"
|
||||
seat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdelivery"
|
||||
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-forge-bridge/forgemsg"
|
||||
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/trunk"
|
||||
"git.novox.be/novox/mesh-sdk/go/keys"
|
||||
)
|
||||
|
||||
// forgeMergedEvent is the forge bridge's merged message of any repository, as the manifest consumes it.
|
||||
var forgeMergedEvent = declared("forge.*.*.merged")
|
||||
|
||||
// moveHead moves the head the branch's source names for repository to head, and says source-moved with the key's revision. It
|
||||
// answers false, and writes nothing, when the branch has no source key, the key does not name the repository or names the head
|
||||
// already. group is the group that made the merge, empty for a merge no group of the gate made.
|
||||
func (h *Holder) moveHead(branch, repository, head, group string) (moved bool, err error) {
|
||||
if h.Source == nil || head == "" {
|
||||
return false, nil
|
||||
}
|
||||
held, _, err := h.Source.Read(branch)
|
||||
if err != nil || held == nil {
|
||||
return false, err
|
||||
}
|
||||
name := ""
|
||||
for r := range held.Heads {
|
||||
if trunk.SameRepository(r, repository) {
|
||||
name = r
|
||||
}
|
||||
}
|
||||
if name == "" || held.Heads[name] == head {
|
||||
return false, nil // not named, or no change: no revision and no source-moved (A-464c)
|
||||
}
|
||||
was := held.Heads[name]
|
||||
// Only forward (A-464c): the head held must be contained in the merge, asked of the seat git.
|
||||
if h.Forge == nil {
|
||||
return false, errors.New("this gate reads no forge, so it cannot tell whether the merge is ahead of the head")
|
||||
}
|
||||
ahead, err := h.Forge.Contains(name, was, head)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("whether %.12s is in %.12s of %s cannot be read, so the head stays: %w", was, head, name, err)
|
||||
}
|
||||
if !ahead {
|
||||
h.logf("[mesh-delivery] source.%s keeps %s at %.12s: the merge %.12s does not contain it (heard again, late or out of order), left without effect", branch, name, was, head)
|
||||
return false, nil
|
||||
}
|
||||
rev, err := h.Source.Set(branch, func(prev *Source) (Source, bool) {
|
||||
moved = false
|
||||
if prev == nil || prev.Heads[name] != was {
|
||||
return Source{}, false // another writer moved it first
|
||||
}
|
||||
next := Source{Group: group, Heads: map[string]string{}}
|
||||
for r, c := range prev.Heads {
|
||||
next.Heads[r] = c
|
||||
}
|
||||
next.Heads[name] = head
|
||||
moved = true
|
||||
return next, true
|
||||
})
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("source.%s cannot be moved: %w", branch, err)
|
||||
}
|
||||
if !moved || h.SaySeat == nil {
|
||||
return moved, nil
|
||||
}
|
||||
at := h.Now().UTC()
|
||||
return true, h.SaySeat(seat.SubjectSourceMoved(keys.EscapeName(branch)),
|
||||
seat.SourceMovedEvent{Branch: branch, Group: group, Revision: int64(rev), At: &at},
|
||||
"source-moved."+keys.EscapeName(branch)+"."+strconv.FormatUint(rev, 10))
|
||||
}
|
||||
|
||||
// ForgeMerged takes one merged message of the forge bridge: the head of its repository in the source of the branch it landed on
|
||||
// moves to the merge. A merge no delivery group made (the message names no group) is said so. A message that cannot be read, or
|
||||
// names no branch or merge, is said and taken: heard again it would be refused again.
|
||||
func (h *Holder) ForgeMerged(body json.RawMessage) error {
|
||||
f, err := forgemsg.DecodeForge(body)
|
||||
if err != nil {
|
||||
h.logf("[mesh-delivery] a merged message of the forge bridge is not readable, the source does not follow it: %v", err)
|
||||
return nil
|
||||
}
|
||||
if f.Kind != forgemsg.ForgeMerged {
|
||||
return nil
|
||||
}
|
||||
if f.Base == "" || f.Merge == "" {
|
||||
h.logf("[mesh-delivery] the merged message of %s names no branch or no merge, the source does not follow it", f.Repository)
|
||||
return nil
|
||||
}
|
||||
moved, err := h.moveHead(f.Base, f.Repository, f.Merge, f.Group)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if moved {
|
||||
if f.Group == "" { // A-464b: a merge whose message names no delivery group is one the gate did not make
|
||||
h.logf("[mesh-delivery] source.%s follows the merge %.12s of %s: not made by a delivery group of the gate", f.Base, f.Merge, f.Repository)
|
||||
} else {
|
||||
h.logf("[mesh-delivery] source.%s follows the merge %.12s of %s, made by the delivery group %s", f.Base, f.Merge, f.Repository, f.Group)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// SourceUncompared is the finding of a start comparison that left a head uncompared (A-464e).
|
||||
const SourceUncompared = "source-uncompared"
|
||||
|
||||
// FollowAtStart compares each head of every source key with the forge's head of that branch and moves every one behind it, at
|
||||
// each start. It ends only when every head was compared (A-464e): a head the forge could not be asked for (the seat git not
|
||||
// answering yet, as when its bundle starts in the same tool runner) is compared again after a growing pause of at most a
|
||||
// minute, and the finding source-uncompared names the repositories and the last error while any stands. It answers the heads
|
||||
// moved as "<repository>@<branch>".
|
||||
func (h *Holder) FollowAtStart() ([]string, error) {
|
||||
if h.Source == nil || h.Forge == nil {
|
||||
return nil, errors.New("this gate keeps no source or reads no forge")
|
||||
}
|
||||
var moved []string
|
||||
var only map[string]bool
|
||||
for wait := 2 * time.Second; ; wait = min(wait*2, time.Minute) {
|
||||
got, left := h.followPass(only)
|
||||
moved = append(moved, got...)
|
||||
if len(left) == 0 {
|
||||
h.clearUncompared()
|
||||
h.logf("[mesh-delivery] the sources compared with the forge's heads at the start: moved %v", moved)
|
||||
return moved, nil
|
||||
}
|
||||
only = map[string]bool{}
|
||||
var names []string
|
||||
var last error
|
||||
for n, err := range left {
|
||||
only[n] = true
|
||||
names = append(names, n)
|
||||
last = err
|
||||
}
|
||||
sort.Strings(names)
|
||||
h.sayUncompared(names, last)
|
||||
h.logf("[mesh-delivery] %d heads are not compared with the forge's yet (%s; last: %v); asking again in %s", len(names), strings.Join(names, ", "), last, wait)
|
||||
startSleep(wait)
|
||||
}
|
||||
}
|
||||
|
||||
// followPass compares the heads named in only (every head when nil) once; it answers the heads moved and, by "<repository>@<branch>",
|
||||
// each head it could not compare with why. A source it cannot list or read is one entry named for itself.
|
||||
func (h *Holder) followPass(only map[string]bool) (moved []string, left map[string]error) {
|
||||
left = map[string]error{}
|
||||
branches, err := h.Source.Branches()
|
||||
if err != nil {
|
||||
left["the branches of the source"] = err
|
||||
return nil, left
|
||||
}
|
||||
for _, branch := range branches {
|
||||
held, _, err := h.Source.Read(branch)
|
||||
if err != nil {
|
||||
left["source."+branch] = err
|
||||
continue
|
||||
}
|
||||
if held == nil {
|
||||
continue
|
||||
}
|
||||
for repository, head := range held.Heads {
|
||||
name := repository + "@" + branch
|
||||
if only != nil && !only[name] {
|
||||
continue
|
||||
}
|
||||
ok, err := h.followForge(branch, repository, head)
|
||||
if err != nil {
|
||||
left[name] = err
|
||||
continue
|
||||
}
|
||||
if ok {
|
||||
moved = append(moved, name)
|
||||
}
|
||||
}
|
||||
}
|
||||
return moved, left
|
||||
}
|
||||
|
||||
func (h *Holder) sayUncompared(names []string, last error) {
|
||||
if h.Findings == nil {
|
||||
return
|
||||
}
|
||||
at := h.Now().UTC()
|
||||
err := h.Findings.Write(context.Background(), meshconditions.Finding{Scope: FindingScope, ID: SourceUncompared, Kind: SourceUncompared,
|
||||
Subject: meshconditions.Subject{Scope: FindingScope, ID: SourceUncompared},
|
||||
Severity: meshconditions.FindingSeverityWarning, Resolver: "operator",
|
||||
Summary: fmt.Sprintf("the gate's source is not compared with the forge's head for %s; the last error: %v", strings.Join(names, ", "), last),
|
||||
Headline: "The gate's source is not compared with the forge",
|
||||
Explanation: "At its start the gate compares each head of its source with the forge's head of the branch. A head it could not compare is asked again after a growing pause, and this clears once every head is compared.",
|
||||
Needs: "the seat git answering; gate_source_follow moves one repository's head by hand.",
|
||||
Source: SourceUncompared, Observer: Module, At: at, Since: at})
|
||||
if err != nil {
|
||||
h.logf("[mesh-delivery] the finding %s is not written: %v", SourceUncompared, err)
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Holder) clearUncompared() {
|
||||
if h.Findings == nil {
|
||||
return
|
||||
}
|
||||
for _, f := range h.Findings.Held() {
|
||||
if f.Scope == FindingScope && f.ID == SourceUncompared && f.Kind == SourceUncompared {
|
||||
if err := h.Findings.Clear(context.Background(), FindingScope, SourceUncompared, SourceUncompared, "every head is compared with the forge's"); err != nil {
|
||||
h.logf("[mesh-delivery] the finding %s is not cleared: %v", SourceUncompared, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// followForge moves one repository's head in a branch's source to the forge's head of the branch when they differ.
|
||||
func (h *Holder) followForge(branch, repository, held string) (bool, error) {
|
||||
a, err := h.Forge.Branch(repository, branch)
|
||||
if err == nil && a.Error != "" {
|
||||
err = errors.New(a.Error)
|
||||
}
|
||||
if err == nil && a.Head == "" {
|
||||
err = errors.New("the forge named no head")
|
||||
}
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
if sameCommit(held, a.Head) && len(held) == len(a.Head) {
|
||||
return false, nil
|
||||
}
|
||||
moved, err := h.moveHead(branch, repository, a.Head, "")
|
||||
if moved {
|
||||
h.logf("[mesh-delivery] source.%s follows the forge: %s from %.12s to %.12s", branch, repository, held, a.Head)
|
||||
}
|
||||
return moved, err
|
||||
}
|
||||
|
||||
// SourceFollow moves one repository's head to the forge's head of the branch by hand, after the hand-act log took it.
|
||||
func (h *Holder) SourceFollow(branch, repository, why, by string, acts handActs) (map[string]any, error) {
|
||||
if strings.TrimSpace(branch) == "" || strings.TrimSpace(repository) == "" || strings.TrimSpace(why) == "" {
|
||||
return nil, errors.New("say the branch, the repository and why its head follows the forge")
|
||||
}
|
||||
if h.Source == nil || h.Forge == nil {
|
||||
return nil, errors.New("this gate keeps no source or reads no forge")
|
||||
}
|
||||
if acts == nil {
|
||||
return nil, errors.New("the hand-act log cannot be reached here, so nothing was moved")
|
||||
}
|
||||
held, _, err := h.Source.Read(branch)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if held == nil {
|
||||
return nil, fmt.Errorf("the gate keeps no source of the branch %s", branch)
|
||||
}
|
||||
name := ""
|
||||
for r := range held.Heads {
|
||||
if trunk.SameRepository(r, repository) {
|
||||
name = r
|
||||
}
|
||||
}
|
||||
if name == "" {
|
||||
return nil, fmt.Errorf("the source of %s names no head of %s", branch, repository)
|
||||
}
|
||||
if _, err := acts.Record(meshhandactsclient.Request{What: "moved the head of " + repository + " in the source of the branch " + branch + " to the forge's head",
|
||||
Why: why, Cause: "gate_source_follow", Verb: "source-follow"}); err != nil {
|
||||
return nil, fmt.Errorf("the hand act was not recorded through mesh-hand-acts, so nothing was moved: %w", err)
|
||||
}
|
||||
moved, err := h.followForge(branch, name, held.Heads[name])
|
||||
return map[string]any{"branch": branch, "repository": name, "moved": moved, "by": by}, err
|
||||
}
|
||||
@@ -0,0 +1,193 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.novox.be/novox/mesh-contracts/health-and-repair/go/seats/meshconditions"
|
||||
meshcatalog "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog"
|
||||
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-forge-bridge/forgemsg"
|
||||
stdio "git.novox.be/novox/mesh-sdk/go"
|
||||
)
|
||||
|
||||
// Design 47 G A-464, A-464b: the forge bridge's merged message moves the head of its repository in the source of the branch it
|
||||
// landed on and says source-moved; a merge naming no delivery group is said not made by one; a branch no source key holds, a
|
||||
// repository the key does not name, and a head the key holds already move nothing.
|
||||
func TestAMergedMessageMovesTheSourceOfItsBranchWhoeverMadeTheMerge(t *testing.T) {
|
||||
old, merge := strings.Repeat("a", 40), strings.Repeat("b", 40)
|
||||
body := func(repository, base, group string) []byte {
|
||||
f := forgemsg.Forge{V: 1, Kind: forgemsg.ForgeMerged, Repository: repository, Base: base, Merge: merge, Head: merge, Number: 14, Root: strings.Repeat("d", 40)}
|
||||
data, err := forgemsg.EncodeForge(f)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return data
|
||||
}
|
||||
for _, c := range []struct {
|
||||
name, repository, base string
|
||||
moved bool
|
||||
}{
|
||||
{"watched branch, named repository", "novox/mesh-contracts", "main", true},
|
||||
{"a branch no source key holds", "novox/mesh-contracts", "other", false},
|
||||
{"a repository the key does not name", "novox/other", "main", false},
|
||||
} {
|
||||
t.Run(c.name, func(t *testing.T) {
|
||||
src := &memSource{vals: map[string]Source{"main": {Group: "g", Heads: map[string]string{"novox/mesh-contracts": old}}}}
|
||||
var said []string
|
||||
var logged []string
|
||||
h := &Holder{Now: time.Now, Source: src, Forge: &fakeForge{},
|
||||
Logf: func(f string, a ...any) { logged = append(logged, f) },
|
||||
SaySeat: func(subject string, _ any, id string) error { said = append(said, id); return nil }}
|
||||
if err := h.ForgeMerged(body(c.repository, c.base, "")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got := src.vals["main"]
|
||||
if c.moved != (got.Heads["novox/mesh-contracts"] == merge) || c.moved != (len(said) == 1) || (c.moved && got.Group != "") {
|
||||
t.Fatalf("source %+v said %v: moved wanted %v", got, said, c.moved)
|
||||
}
|
||||
if c.moved && !strings.Contains(strings.Join(logged, "\n"), "not made by a delivery group of the gate") {
|
||||
t.Errorf("a merge no group made is not said so: %v", logged)
|
||||
}
|
||||
// Heard again, the key names the head already: nothing is moved or said twice.
|
||||
if err := h.ForgeMerged(body(c.repository, c.base, "")); err != nil || len(said) > 1 {
|
||||
t.Errorf("heard twice: %v, said %v", err, said)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Design 47 G A-464: at its start the gate compares each head with the forge's branch head and moves every one behind it; a head
|
||||
// the forge cannot read is said and left; gate_source_follow does it for one repository after the hand-act log took it.
|
||||
func TestTheStartAndTheToolMoveAHeadBehindTheForge(t *testing.T) {
|
||||
old, now := strings.Repeat("a", 40), strings.Repeat("c", 40)
|
||||
mk := func() (*Holder, *branchSource, *[]string) {
|
||||
src := &branchSource{memSource{vals: map[string]Source{"main": {Heads: map[string]string{"o/f1": old, "o/f2": old, "o/f3": now}}}}}
|
||||
var said []string
|
||||
return &Holder{Now: time.Now, Source: src, Logf: func(string, ...any) {},
|
||||
Forge: &fakeForge{heads: map[string]string{"o/f1@main": now, "o/f3@main": now}}, // o/f2 cannot be read
|
||||
SaySeat: func(_ string, _ any, id string) error { said = append(said, id); return nil }}, src, &said
|
||||
}
|
||||
h, src, said := mk()
|
||||
fw := &fakeFindings{}
|
||||
h.Findings = fw
|
||||
forge := h.Forge.(*fakeForge)
|
||||
var pauses []time.Duration
|
||||
var heldWhileWaiting int
|
||||
prevSleep := startSleep
|
||||
startSleep = func(d time.Duration) {
|
||||
pauses = append(pauses, d)
|
||||
heldWhileWaiting = len(fw.held) // the finding stands while a head is uncompared
|
||||
forge.heads["o/f2@main"] = now // the seat git answers by the second pass
|
||||
}
|
||||
defer func() { startSleep = prevSleep }()
|
||||
moved, err := h.FollowAtStart()
|
||||
if err != nil || len(moved) != 2 || src.vals["main"].Heads["o/f1"] != now || src.vals["main"].Heads["o/f2"] != now || len(*said) != 2 {
|
||||
t.Fatalf("start: moved %v err %v heads %v said %v: want o/f1 and, after the second pass, o/f2 moved", moved, err, src.vals["main"].Heads, *said)
|
||||
}
|
||||
if len(pauses) != 1 || heldWhileWaiting != 1 || len(fw.held) != 0 {
|
||||
t.Fatalf("pauses %v, finding held while waiting %d and after %d: want one pause, the finding raised while a head stood uncompared and cleared after", pauses, heldWhileWaiting, len(fw.held))
|
||||
}
|
||||
h, src, said = mk()
|
||||
acts := &handActsFake{refuse: errors.New("log down")}
|
||||
if _, err := h.SourceFollow("main", "o/f1", "missed a merge", "me", acts); err == nil || src.vals["main"].Heads["o/f1"] != old {
|
||||
t.Fatalf("moved without the log: %v", err)
|
||||
}
|
||||
acts.refuse = nil
|
||||
if _, err := h.SourceFollow("main", "o/f1", "", "me", acts); err == nil {
|
||||
t.Fatal("moved with no reason")
|
||||
}
|
||||
out, err := h.SourceFollow("main", "o/f1", "missed a merge", "me", acts)
|
||||
if err != nil || out["moved"] != true || len(acts.took) != 1 || acts.took[0].Cause != "gate_source_follow" || src.vals["main"].Heads["o/f1"] != now {
|
||||
t.Fatalf("tool: %v %v log %+v heads %v", out, err, acts.took, src.vals["main"].Heads)
|
||||
}
|
||||
}
|
||||
|
||||
// Design 47 G A-464: the gate takes the forge bridge's merged event of any repository, by the manifest's own pattern.
|
||||
func TestTheGateTakesTheForgeBridgesMergedEventOfAnyRepository(t *testing.T) {
|
||||
merge := strings.Repeat("b", 40)
|
||||
data, err := forgemsg.EncodeForge(forgemsg.Forge{V: 1, Kind: forgemsg.ForgeMerged, Repository: "novox/mesh-contracts", Base: "main", Merge: merge, Head: merge, Number: 14, Root: strings.Repeat("d", 40)})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
src := &memSource{vals: map[string]Source{"main": {Heads: map[string]string{"novox/mesh-contracts": strings.Repeat("a", 40)}}}}
|
||||
h := &Holder{Now: time.Now, Source: src, Forge: &fakeForge{}, Logf: func(string, ...any) {}}
|
||||
key := strings.Replace(strings.Replace(forgeMergedEvent, "*", "novox", 1), "*", "mesh-contracts", 1)
|
||||
if err := take(h, stdio.Envelope{Key: key, Body: data}); err != nil || src.vals["main"].Heads["novox/mesh-contracts"] != merge {
|
||||
t.Fatalf("event %q: %v, source %+v", key, err, src.vals["main"])
|
||||
}
|
||||
}
|
||||
|
||||
// Design 47 G A-464c: a head moves only forward and only when it changes. A merged message for a commit that does not contain
|
||||
// the held head (replayed, late, out of order) moves nothing and is said; a second message for the same head writes no
|
||||
// revision; and a take-in.taken for a head a merged already moved writes none and says no source-moved.
|
||||
func TestAHeadMovesOnlyForwardAndOnlyWhenItChanges(t *testing.T) {
|
||||
oldC, newC := strings.Repeat("a", 40), strings.Repeat("b", 40)
|
||||
msg := func(merge string) []byte {
|
||||
data, err := forgemsg.EncodeForge(forgemsg.Forge{V: 1, Kind: forgemsg.ForgeMerged, Repository: "novox/app", Base: "main", Merge: merge, Head: merge, Number: 3, Root: strings.Repeat("d", 40)})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return data
|
||||
}
|
||||
src := &memSource{vals: map[string]Source{"main": {Heads: map[string]string{"novox/app": newC}}}}
|
||||
var said, logged []string
|
||||
// newC contains oldC, not the other way round.
|
||||
forge := &fakeForge{contains: func(commit, in string) bool { return commit == in || (commit == oldC && in == newC) }}
|
||||
h := &Holder{Now: time.Now, Source: src, Forge: forge, Logf: func(f string, a ...any) { logged = append(logged, f) },
|
||||
SaySeat: func(_ string, _ any, id string) error { said = append(said, id); return nil }}
|
||||
if err := h.ForgeMerged(msg(oldC)); err != nil || src.vals["main"].Heads["novox/app"] != newC || src.rev != 0 || len(said) != 0 || len(logged) == 0 {
|
||||
t.Fatalf("a replayed old merge: %v, source %+v, revisions %d, said %v, logged %v: want nothing moved and a log line", err, src.vals["main"], src.rev, said, logged)
|
||||
}
|
||||
if err := h.ForgeMerged(msg(newC)); err != nil || src.rev != 0 || len(said) != 0 {
|
||||
t.Fatalf("a second message for the same head wrote revision %d, said %v", src.rev, said)
|
||||
}
|
||||
// A take-in.taken naming the head a merged moved already writes no revision and says nothing.
|
||||
body := []byte(`{"v":1,"kind":"taken","key":"g.feat","what":"group","group":"feat","branch":"main","source":7,` +
|
||||
`"merges":[{"repository":"novox/app","number":3,"merge":"` + newC + `"}],"entries":[]}`)
|
||||
if err := h.moveSource(mustTaken(t, body)); err != nil || src.rev != 0 || len(said) != 0 {
|
||||
t.Fatalf("take-in for a head already moved: %v, revision %d, said %v", err, src.rev, said)
|
||||
}
|
||||
// A real forward move still writes one revision.
|
||||
src.vals["main"] = Source{Heads: map[string]string{"novox/app": oldC}}
|
||||
if err := h.ForgeMerged(msg(newC)); err != nil || src.rev != 1 || len(said) != 1 {
|
||||
t.Fatalf("a forward move: %v, revision %d, said %v", err, src.rev, said)
|
||||
}
|
||||
}
|
||||
|
||||
func mustTaken(t *testing.T, body []byte) meshcatalog.TakeInTaken {
|
||||
t.Helper()
|
||||
var v meshcatalog.TakeInTaken
|
||||
if err := json.Unmarshal(body, &v); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
// Design 47 G A-464d: a take-in.taken arriving after a later merge moved the head leaves the head where it is.
|
||||
func TestATakeInTakenAfterALaterMergeLeavesTheHead(t *testing.T) {
|
||||
oldC, newC := strings.Repeat("a", 40), strings.Repeat("b", 40)
|
||||
src := &memSource{vals: map[string]Source{"main": {Heads: map[string]string{"novox/app": newC}}}}
|
||||
var said []string
|
||||
h := &Holder{Now: time.Now, Source: src, Logf: func(string, ...any) {},
|
||||
Forge: &fakeForge{contains: func(commit, in string) bool { return commit == in || (commit == oldC && in == newC) }},
|
||||
SaySeat: func(_ string, _ any, id string) error { said = append(said, id); return nil }}
|
||||
body := []byte(`{"v":1,"kind":"taken","key":"g.feat","what":"group","group":"feat","branch":"main","source":7,` +
|
||||
`"merges":[{"repository":"novox/app","number":3,"merge":"` + oldC + `"}],"entries":[]}`)
|
||||
if err := h.moveSource(mustTaken(t, body)); err != nil || src.vals["main"].Heads["novox/app"] != newC || src.rev != 0 || len(said) != 0 {
|
||||
t.Fatalf("take-in of an older merge: %v, source %+v, revision %d, said %v: want the head left at the later merge", err, src.vals["main"], src.rev, said)
|
||||
}
|
||||
}
|
||||
|
||||
// fakeFindings is the gate's finding writer in memory.
|
||||
type fakeFindings struct{ held []meshconditions.Finding }
|
||||
|
||||
func (f *fakeFindings) Write(_ context.Context, x meshconditions.Finding) error {
|
||||
f.held = append(f.held, x)
|
||||
return nil
|
||||
}
|
||||
func (f *fakeFindings) Clear(_ context.Context, _, _, _, _ string) error { f.held = nil; return nil }
|
||||
func (f *fakeFindings) Held() []meshconditions.Finding { return f.held }
|
||||
@@ -0,0 +1,248 @@
|
||||
package main
|
||||
|
||||
// A watched branch starts from the forge, as a clone does (design 47 G A-401, §430, §15): at its start and on each
|
||||
// registration-changed of the seat mesh-catalog, the gate writes source.<branch> for every watched branch the registrations name,
|
||||
// each registered repository at the head of that branch, asked of the seat git's verb branch, the group left empty, and says
|
||||
// source-moved. A repository whose head cannot be read is left out and said. A head a key holds is never changed here (A-403): from
|
||||
// then on only a take-in moves it (A-34).
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshhandactsclient"
|
||||
gitseat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/git"
|
||||
meshcatalog "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog"
|
||||
seat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdelivery"
|
||||
"git.novox.be/novox/mesh-sdk/go/keys"
|
||||
)
|
||||
|
||||
// registrationChangedEvent is the seat mesh-catalog's event, as the gate's manifest consumes it.
|
||||
var registrationChangedEvent = declared(meshcatalog.EventRegistrationChanged)
|
||||
|
||||
// Registered is what the gate reads of the seat mesh-catalog: its registrations.
|
||||
type Registered interface {
|
||||
Registrations() (meshcatalog.RegistrationsAnswer, error)
|
||||
}
|
||||
|
||||
// seatCatalog reads the seat mesh-catalog through the tool runner.
|
||||
type seatCatalog struct{ ask asking }
|
||||
|
||||
func (s seatCatalog) Registrations() (meshcatalog.RegistrationsAnswer, error) {
|
||||
var a meshcatalog.RegistrationsAnswer
|
||||
raw, err := s.ask("seat:"+meshcatalog.Seat+"."+meshcatalog.VerbRegistrations, meshcatalog.RegistrationsInput{})
|
||||
if err != nil {
|
||||
return a, err
|
||||
}
|
||||
answer, said, ok := answerOf(raw)
|
||||
if !ok {
|
||||
return a, fmt.Errorf("registrations refused: %s", lastLine(said))
|
||||
}
|
||||
if err := json.Unmarshal(answer, &a); err != nil {
|
||||
return a, fmt.Errorf("the answer of registrations is not readable: %w", err)
|
||||
}
|
||||
return a, nil
|
||||
}
|
||||
|
||||
// Branch is the seat git's verb branch: a repository's branch head.
|
||||
func (s seatGit) Branch(repository, branch string) (gitseat.BranchAnswer, error) {
|
||||
var a gitseat.BranchAnswer
|
||||
return a, s.call(gitseat.VerbBranch, gitseat.BranchInput{Repository: repository, Branch: branch}, &a)
|
||||
}
|
||||
|
||||
// Contains is whether commit is the commit in or an ancestor of it.
|
||||
func (s seatGit) Contains(repository, commit, in string) (bool, error) {
|
||||
var a gitseat.ContainsAnswer
|
||||
if err := s.call(gitseat.VerbContains, gitseat.ContainsInput{Repository: repository, Commit: commit, In: in}, &a); err != nil {
|
||||
return false, err
|
||||
}
|
||||
if a.Error != "" {
|
||||
return false, errors.New(a.Error)
|
||||
}
|
||||
return a.Contains, nil
|
||||
}
|
||||
|
||||
// watchedRepositories are the repositories each watched branch is registered for, each once, sorted.
|
||||
func watchedRepositories(regs []meshcatalog.Registration) map[string][]string {
|
||||
seen := map[string]map[string]bool{}
|
||||
for _, r := range regs {
|
||||
if strings.TrimSpace(r.Origin) == "" {
|
||||
continue
|
||||
}
|
||||
for _, b := range r.Branches {
|
||||
if strings.TrimSpace(b) == "" {
|
||||
continue
|
||||
}
|
||||
if seen[b] == nil {
|
||||
seen[b] = map[string]bool{}
|
||||
}
|
||||
seen[b][r.Origin] = true
|
||||
}
|
||||
}
|
||||
out := map[string][]string{}
|
||||
for b, rs := range seen {
|
||||
for r := range rs {
|
||||
out[b] = append(out[b], r)
|
||||
}
|
||||
sort.Strings(out[b])
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// StartSources adds to the source key of every watched branch the registrations name each registered repository it lacks, and answers the
|
||||
// branches it wrote. A registrations answer that cannot be read is an error (the event is offered again); a repository whose
|
||||
// head cannot be read is said and left out.
|
||||
func (h *Holder) StartSources() ([]string, error) {
|
||||
started, _, err := h.startSources("")
|
||||
return started, err
|
||||
}
|
||||
|
||||
// startSleep waits between the start's passes; a test replaces it.
|
||||
var startSleep = time.Sleep
|
||||
|
||||
// errRegistrationsUnread is a start pass's error when the registrations cannot be read.
|
||||
var errRegistrationsUnread = errors.New("the registrations cannot be read")
|
||||
|
||||
// startSourcesAtStart runs the start pass once (A-442), and asks again, as switchToRecords does (2 s doubling to a minute),
|
||||
// only while the registrations cannot be read: the seat mesh-catalog may serve after the gate does. A repository a pass
|
||||
// leaves out is said by the pass, and added at the next registration change or by gate_source_start (A-401).
|
||||
func startSourcesAtStart(h *Holder) {
|
||||
for wait := 2 * time.Second; ; wait = min(wait*2, time.Minute) {
|
||||
started, leftOut, err := h.startSources("")
|
||||
if err == nil || !errors.Is(err, errRegistrationsUnread) {
|
||||
h.logf("[mesh-delivery] the sources started from the forge at the start: %v; left out %v; %v", started, leftOut, err)
|
||||
return
|
||||
}
|
||||
h.logf("[mesh-delivery] the registrations cannot be read yet (%v); asking again in %s", err, wait)
|
||||
startSleep(wait)
|
||||
}
|
||||
}
|
||||
|
||||
// startSources starts the branches no key holds, or only branch when it is named; it answers the branches written and the
|
||||
// repositories left out as "<repository>@<branch>".
|
||||
func (h *Holder) startSources(only string) (started, leftOut []string, err error) {
|
||||
if h.Source == nil || h.Catalog == nil || h.Forge == nil {
|
||||
return nil, nil, errors.New("this gate keeps no source, reads no registrations or reads no forge")
|
||||
}
|
||||
h.startMu.Lock()
|
||||
defer h.startMu.Unlock()
|
||||
regs, err := h.Catalog.Registrations()
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("%w: %w", errRegistrationsUnread, err)
|
||||
}
|
||||
watched := watchedRepositories(regs.Registrations)
|
||||
if only != "" {
|
||||
if _, ok := watched[only]; !ok {
|
||||
return nil, nil, fmt.Errorf("no registration names the branch %s", only)
|
||||
}
|
||||
}
|
||||
names := make([]string, 0, len(watched))
|
||||
for b := range watched {
|
||||
if only == "" || b == only {
|
||||
names = append(names, b)
|
||||
}
|
||||
}
|
||||
sort.Strings(names)
|
||||
var errs []error
|
||||
for _, branch := range names {
|
||||
held, _, err := h.Source.Read(branch)
|
||||
if err != nil {
|
||||
errs = append(errs, err)
|
||||
continue
|
||||
}
|
||||
heads := map[string]string{}
|
||||
for _, repository := range watched[branch] {
|
||||
if held != nil {
|
||||
if _, has := held.Heads[repository]; has {
|
||||
continue // a head a key holds moves by a take-in alone (A-34)
|
||||
}
|
||||
}
|
||||
a, err := h.Forge.Branch(repository, branch)
|
||||
if err == nil && a.Error != "" {
|
||||
err = errors.New(a.Error)
|
||||
}
|
||||
if err == nil && a.Head == "" {
|
||||
err = errors.New("the forge named no head")
|
||||
}
|
||||
if err != nil {
|
||||
leftOut = append(leftOut, repository+"@"+branch)
|
||||
h.logf("[mesh-delivery] source.%s starts without %s: its head cannot be read (%v); it is added at the next registration change or by gate_source_start", branch, repository, err)
|
||||
continue
|
||||
}
|
||||
heads[repository] = a.Head
|
||||
}
|
||||
if len(heads) == 0 {
|
||||
continue
|
||||
}
|
||||
wrote := false
|
||||
rev, err := h.Source.Set(branch, func(prev *Source) (Source, bool) {
|
||||
next := Source{Heads: map[string]string{}}
|
||||
if prev != nil {
|
||||
next.Group = prev.Group
|
||||
for r, c := range prev.Heads {
|
||||
next.Heads[r] = c
|
||||
}
|
||||
}
|
||||
wrote = false
|
||||
for r, c := range heads {
|
||||
if _, has := next.Heads[r]; !has { // a held head is never changed
|
||||
next.Heads[r] = c
|
||||
wrote = true
|
||||
}
|
||||
}
|
||||
return next, wrote
|
||||
})
|
||||
if err != nil {
|
||||
errs = append(errs, fmt.Errorf("source.%s cannot be started: %w", branch, err))
|
||||
continue
|
||||
}
|
||||
if !wrote {
|
||||
continue
|
||||
}
|
||||
started = append(started, branch)
|
||||
if h.SaySeat == nil {
|
||||
continue
|
||||
}
|
||||
at := h.Now().UTC()
|
||||
if err := h.SaySeat(seat.SubjectSourceMoved(keys.EscapeName(branch)),
|
||||
seat.SourceMovedEvent{Branch: branch, Revision: int64(rev), At: &at},
|
||||
"source-moved."+keys.EscapeName(branch)+"."+strconv.FormatUint(rev, 10)); err != nil {
|
||||
errs = append(errs, err)
|
||||
}
|
||||
}
|
||||
return started, leftOut, errors.Join(errs...)
|
||||
}
|
||||
|
||||
func (h *Holder) logf(format string, a ...any) {
|
||||
if h.Logf != nil {
|
||||
h.Logf(format, a...)
|
||||
}
|
||||
}
|
||||
|
||||
// SourceStart starts one watched branch by hand, after the hand-act log took it: the same as the start does.
|
||||
func (h *Holder) SourceStart(branch, why, by string, acts handActs) (map[string]any, error) {
|
||||
if strings.TrimSpace(branch) == "" || strings.TrimSpace(why) == "" {
|
||||
return nil, errors.New("say the branch and why its source is started")
|
||||
}
|
||||
if h.Source == nil || h.Catalog == nil || h.Forge == nil {
|
||||
return nil, errors.New("this gate keeps no source, reads no registrations or reads no forge")
|
||||
}
|
||||
if acts == nil {
|
||||
return nil, errors.New("the hand-act log cannot be reached here, so nothing was started")
|
||||
}
|
||||
if _, err := acts.Record(meshhandactsclient.Request{What: "started the source of the branch " + branch,
|
||||
Why: why, Cause: "gate_source_start", Verb: "source-start"}); err != nil {
|
||||
return nil, fmt.Errorf("the hand act was not recorded through mesh-hand-acts, so nothing was started: %w", err)
|
||||
}
|
||||
started, leftOut, err := h.startSources(branch)
|
||||
out := map[string]any{"branch": branch, "started": started, "left-out": leftOut, "by": by}
|
||||
if err == nil && len(started) == 0 && len(leftOut) == 0 {
|
||||
out["note"] = "the key names every registered repository already: only a take-in moves a head"
|
||||
}
|
||||
return out, err
|
||||
}
|
||||
@@ -79,5 +79,26 @@ func sourceTools(h *Holder, acts handActs) []stdio.Tool {
|
||||
Run: func(a map[string]any) (any, error) {
|
||||
return h.SourceAgain(strArg(a, "branch"), strArg(a, "why"), stdio.CallerOf(a), acts)
|
||||
}},
|
||||
{Name: "gate_source_start",
|
||||
Description: "Start the source of one watched branch the registrations name and no source key holds yet, from the forge: each " +
|
||||
"registered repository at the branch's head, the group empty, then source-moved; recorded in the hand-act log of the seat " +
|
||||
"mesh-hand-acts first. A key the gate holds is never rewritten.",
|
||||
Input: map[string]any{"type": "object", "properties": map[string]any{
|
||||
"branch": str("the watched branch"), "why": str("why it is started by hand, in plain words")},
|
||||
"required": []string{"branch", "why"}},
|
||||
Run: func(a map[string]any) (any, error) {
|
||||
return h.SourceStart(strArg(a, "branch"), strArg(a, "why"), stdio.CallerOf(a), acts)
|
||||
}},
|
||||
{Name: "gate_source_follow",
|
||||
Description: "Move the head one watched branch's source names for one repository to the forge's head of that branch now, as " +
|
||||
"the gate does when it hears a merge and at its start (design 47 G A-464); recorded in the hand-act log of the seat " +
|
||||
"mesh-hand-acts first. Nothing is moved when the source already names the forge's head.",
|
||||
Input: map[string]any{"type": "object", "properties": map[string]any{
|
||||
"branch": str("the watched branch"), "repository": str("the repository, owner/name"),
|
||||
"why": str("why its head follows the forge by hand, in plain words")},
|
||||
"required": []string{"branch", "repository", "why"}},
|
||||
Run: func(a map[string]any) (any, error) {
|
||||
return h.SourceFollow(strArg(a, "branch"), strArg(a, "repository"), strArg(a, "why"), stdio.CallerOf(a), acts)
|
||||
}},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,13 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
meshcatalog "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog"
|
||||
seat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdelivery"
|
||||
stdio "git.novox.be/novox/mesh-sdk/go"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -103,3 +108,204 @@ func TestTheSourceAnswerNamesTheBranchItWasAskedAndStatusSaysEveryBranch(t *test
|
||||
t.Fatalf("status says every branch's source: %+v", h.SourceAll())
|
||||
}
|
||||
}
|
||||
|
||||
type fakeCatalog struct {
|
||||
regs []meshcatalog.Registration
|
||||
err error
|
||||
}
|
||||
|
||||
func (f fakeCatalog) Registrations() (meshcatalog.RegistrationsAnswer, error) {
|
||||
return meshcatalog.RegistrationsAnswer{Registrations: f.regs}, f.err
|
||||
}
|
||||
|
||||
// Design 47 G A-401: a watched branch starts from the forge as a clone does: each registered repository at the branch's head,
|
||||
// the group empty, source-moved said; a repository whose head cannot be read is left out; a key held is never rewritten.
|
||||
func TestAWatchedBranchStartsFromTheForge(t *testing.T) {
|
||||
regs := []meshcatalog.Registration{
|
||||
{Module: "car-hunter", Origin: "o/car-hunter", Branches: []string{"main"}},
|
||||
{Module: "mesh-a", Origin: "o/mesh", Branches: []string{"main"}},
|
||||
{Module: "mesh-b", Origin: "o/mesh", Branches: []string{"main"}}, // the same repository twice: once
|
||||
{Module: "f1", Origin: "o/f1-circuits", Branches: []string{"master"}},
|
||||
{Module: "l2p", Origin: "o/link2pay", Branches: []string{"nox-mesh"}},
|
||||
{Module: "ph", Origin: "o/photos", Branches: []string{"nox-mesh", "main"}},
|
||||
}
|
||||
forge := &fakeForge{heads: map[string]string{
|
||||
"o/car-hunter@main": "c1", "o/mesh@main": "c2", "o/f1-circuits@master": "c3", "o/link2pay@nox-mesh": "c4",
|
||||
"o/photos@main": "c6", // o/photos@nox-mesh cannot be read
|
||||
}}
|
||||
for _, c := range []struct {
|
||||
name string
|
||||
held map[string]Source
|
||||
catalog fakeCatalog
|
||||
only string
|
||||
started []string
|
||||
heads map[string]map[string]string
|
||||
events int
|
||||
wantErr bool
|
||||
}{
|
||||
{name: "every branch no key holds", catalog: fakeCatalog{regs: regs},
|
||||
started: []string{"main", "master", "nox-mesh"}, events: 3,
|
||||
heads: map[string]map[string]string{"main": {"o/car-hunter": "c1", "o/mesh": "c2", "o/photos": "c6"},
|
||||
"master": {"o/f1-circuits": "c3"}, "nox-mesh": {"o/link2pay": "c4"}}},
|
||||
{name: "a held key lacking repositories gets them, its heads unchanged", catalog: fakeCatalog{regs: regs},
|
||||
held: map[string]Source{"main": {Group: "g", Heads: map[string]string{"o/mesh": "old"}}},
|
||||
started: []string{"main", "master", "nox-mesh"}, events: 3,
|
||||
heads: map[string]map[string]string{"main": {"o/mesh": "old", "o/car-hunter": "c1", "o/photos": "c6"}, "master": {"o/f1-circuits": "c3"}, "nox-mesh": {"o/link2pay": "c4"}}},
|
||||
{name: "a held key lacking one repository", catalog: fakeCatalog{regs: regs[:2]},
|
||||
held: map[string]Source{"main": {Group: "g", Heads: map[string]string{"o/mesh": "old"}}},
|
||||
started: []string{"main"}, events: 1,
|
||||
heads: map[string]map[string]string{"main": {"o/mesh": "old", "o/car-hunter": "c1"}}},
|
||||
{name: "a held key naming all: nothing written or said", catalog: fakeCatalog{regs: regs[:2]},
|
||||
held: map[string]Source{"main": {Group: "g", Heads: map[string]string{"o/mesh": "old", "o/car-hunter": "older"}}},
|
||||
events: 0,
|
||||
heads: map[string]map[string]string{"main": {"o/mesh": "old", "o/car-hunter": "older"}}},
|
||||
{name: "by hand, one branch", catalog: fakeCatalog{regs: regs}, only: "master",
|
||||
started: []string{"master"}, events: 1, heads: map[string]map[string]string{"master": {"o/f1-circuits": "c3"}}},
|
||||
{name: "registrations unreadable", catalog: fakeCatalog{err: errors.New("away")}, wantErr: true},
|
||||
{name: "no registration names the branch", catalog: fakeCatalog{regs: regs}, only: "nope", wantErr: true},
|
||||
} {
|
||||
t.Run(c.name, func(t *testing.T) {
|
||||
src := &branchSource{memSource{vals: map[string]Source{}}}
|
||||
for b, s := range c.held {
|
||||
src.vals[b] = s
|
||||
}
|
||||
events := 0
|
||||
var logged []string
|
||||
h := &Holder{Now: time.Now, Source: src, Forge: forge, Catalog: c.catalog,
|
||||
Logf: func(f string, a ...any) { logged = append(logged, fmt.Sprintf(f, a...)) },
|
||||
SaySeat: func(subject string, body any, id string) error {
|
||||
events++
|
||||
if ev := body.(seat.SourceMovedEvent); ev.Group != "" {
|
||||
t.Errorf("a source-moved for started heads names no group (A-404), got %q", ev.Group)
|
||||
}
|
||||
return nil
|
||||
}}
|
||||
started, _, err := h.startSources(c.only)
|
||||
if (err != nil) != c.wantErr {
|
||||
t.Fatalf("err %v", err)
|
||||
}
|
||||
if c.wantErr {
|
||||
return
|
||||
}
|
||||
if !reflect.DeepEqual(started, c.started) || events != c.events {
|
||||
t.Fatalf("started %v (%d events), want %v (%d)", started, events, c.started, c.events)
|
||||
}
|
||||
for b, want := range c.heads {
|
||||
if got := src.vals[b].Heads; !reflect.DeepEqual(got, want) {
|
||||
t.Errorf("source.%s heads %v, want %v", b, got, want)
|
||||
}
|
||||
}
|
||||
for b, h := range c.held {
|
||||
if src.vals[b].Group != h.Group {
|
||||
t.Errorf("source.%s keeps the group %q a take-in wrote, got %q", b, h.Group, src.vals[b].Group)
|
||||
}
|
||||
}
|
||||
if c.only == "" && c.held == nil && !strings.Contains(strings.Join(logged, "\n"), "o/photos") {
|
||||
t.Errorf("the repository left out was not said: %v", logged)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Design 47 G A-401: gate_source_start is recorded in the hand-act log first, and starts nothing when the log refuses it.
|
||||
func TestSourceStartByHandIsRecordedFirst(t *testing.T) {
|
||||
src := &branchSource{memSource{vals: map[string]Source{}}}
|
||||
h := &Holder{Now: time.Now, Source: src, Forge: &fakeForge{heads: map[string]string{"o/f1@master": "c3"}},
|
||||
Catalog: fakeCatalog{regs: []meshcatalog.Registration{{Module: "f1", Origin: "o/f1", Branches: []string{"master"}}}}}
|
||||
acts := &handActsFake{refuse: errors.New("log down")}
|
||||
if _, err := h.SourceStart("master", "newly watched", "me", acts); err == nil || len(src.vals) != 0 {
|
||||
t.Fatalf("started without the log: %v %v", err, src.vals)
|
||||
}
|
||||
acts.refuse = nil
|
||||
if _, err := h.SourceStart("master", "", "me", acts); err == nil {
|
||||
t.Fatal("started with no reason")
|
||||
}
|
||||
if _, err := h.SourceStart("master", "newly watched", "me", acts); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(acts.took) != 1 || acts.took[0].Cause != "gate_source_start" || src.vals["master"].Heads["o/f1"] != "c3" {
|
||||
t.Fatalf("log %+v source %+v", acts.took, src.vals)
|
||||
}
|
||||
}
|
||||
|
||||
// Design 47 G A-401: the registration-changed branch of take and the start pass both write the new branch's key and say
|
||||
// source-moved; removing either leaves the branch unstarted.
|
||||
func TestRegistrationChangedAndTheStartPassStartABranch(t *testing.T) {
|
||||
regs := []meshcatalog.Registration{{Module: "f1", Origin: "o/f1", Branches: []string{"master"}}}
|
||||
for _, c := range []struct {
|
||||
name string
|
||||
run func(h *Holder) error
|
||||
}{
|
||||
{"registration-changed", func(h *Holder) error {
|
||||
return take(h, stdio.Envelope{Key: registrationChangedEvent, Body: json.RawMessage(`{}`)})
|
||||
}},
|
||||
{"the start pass", func(h *Holder) error { startSourcesAtStart(h); return nil }},
|
||||
} {
|
||||
t.Run(c.name, func(t *testing.T) {
|
||||
if registrationChangedEvent == "" {
|
||||
t.Fatal("the manifest does not consume registration-changed")
|
||||
}
|
||||
src := &branchSource{memSource{vals: map[string]Source{}}}
|
||||
var subjects []string
|
||||
h := &Holder{Now: time.Now, Source: src, Forge: &fakeForge{heads: map[string]string{"o/f1@master": "c3"}},
|
||||
Catalog: fakeCatalog{regs: regs}, Logf: func(string, ...any) {},
|
||||
SaySeat: func(subject string, body any, id string) error { subjects = append(subjects, subject); return nil }}
|
||||
if err := c.run(h); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if src.vals["master"].Heads["o/f1"] != "c3" || len(subjects) != 1 || !strings.HasSuffix(subjects[0], ".event.source-moved.master") {
|
||||
t.Fatalf("source %+v events %v", src.vals, subjects)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Design 47 G A-442: the start pass runs once; it asks again, doubling its wait, only while the registrations cannot be read,
|
||||
// and a repository left out is said, not waited for.
|
||||
func TestTheStartAsksAgainOnlyWhileTheRegistrationsCannotBeRead(t *testing.T) {
|
||||
regs := []meshcatalog.Registration{{Module: "f1", Origin: "o/f1", Branches: []string{"master"}}}
|
||||
for _, c := range []struct {
|
||||
name string
|
||||
unreadFor int // passes the registrations cannot be read
|
||||
forgeFail bool // the forge cannot name the head
|
||||
waits []time.Duration
|
||||
started bool
|
||||
}{
|
||||
{"registrations unread twice", 2, false, []time.Duration{2 * time.Second, 4 * time.Second}, true},
|
||||
{"a repository left out is not waited for", 0, true, nil, false},
|
||||
} {
|
||||
t.Run(c.name, func(t *testing.T) {
|
||||
var waits []time.Duration
|
||||
old := startSleep
|
||||
startSleep = func(d time.Duration) { waits = append(waits, d) }
|
||||
defer func() { startSleep = old }()
|
||||
src := &branchSource{memSource{vals: map[string]Source{}}}
|
||||
forge := &fakeForge{fail: c.forgeFail, heads: map[string]string{"o/f1@master": "c3"}}
|
||||
cat := &flakyCatalog{fakeCatalog: fakeCatalog{regs: regs}, failFor: c.unreadFor}
|
||||
var said []string
|
||||
h := &Holder{Now: time.Now, Source: src, Forge: forge, Catalog: cat,
|
||||
Logf: func(f string, a ...any) { said = append(said, fmt.Sprintf(f, a...)) }}
|
||||
startSourcesAtStart(h)
|
||||
if !reflect.DeepEqual(waits, c.waits) || (src.vals["master"].Heads["o/f1"] == "c3") != c.started {
|
||||
t.Fatalf("waits %v source %+v", waits, src.vals)
|
||||
}
|
||||
if c.forgeFail && !strings.Contains(strings.Join(said, "\n"), "o/f1@master") {
|
||||
t.Fatalf("the left-out repository is not said: %v", said)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// flakyCatalog cannot read the registrations for its first failFor asks.
|
||||
type flakyCatalog struct {
|
||||
fakeCatalog
|
||||
failFor int
|
||||
}
|
||||
|
||||
func (f *flakyCatalog) Registrations() (meshcatalog.RegistrationsAnswer, error) {
|
||||
if f.failFor > 0 {
|
||||
f.failFor--
|
||||
return meshcatalog.RegistrationsAnswer{}, errors.New("not served yet")
|
||||
}
|
||||
return f.fakeCatalog.Registrations()
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
mdc "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshdeliveryclient"
|
||||
meshcatalog "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog"
|
||||
seat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdelivery"
|
||||
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/trunk"
|
||||
"git.novox.be/novox/mesh-sdk/go/keys"
|
||||
)
|
||||
|
||||
@@ -107,9 +108,43 @@ func (h *Holder) moveSource(t meshcatalog.TakeInTaken) error {
|
||||
for _, m := range t.Merges {
|
||||
heads[m.Repository] = m.Merge
|
||||
}
|
||||
// A head moves only forward (A-464d): a repository whose held head is not contained in the merge is left where it is.
|
||||
if held, _, err := h.Source.Read(t.Branch); err != nil {
|
||||
return err
|
||||
} else if held != nil && h.Forge != nil {
|
||||
for r, c := range heads {
|
||||
for name, was := range held.Heads {
|
||||
if !trunk.SameRepository(name, r) || was == c {
|
||||
continue
|
||||
}
|
||||
ahead, err := h.Forge.Contains(name, was, c)
|
||||
if err != nil {
|
||||
return fmt.Errorf("whether %.12s is in %.12s of %s cannot be read, so the head stays: %w", was, c, name, err)
|
||||
}
|
||||
if !ahead {
|
||||
h.logf("[mesh-delivery] source.%s keeps %s at %.12s: the take-in's merge %.12s does not contain it, left without effect", t.Branch, name, was, c)
|
||||
delete(heads, r)
|
||||
}
|
||||
}
|
||||
}
|
||||
if len(heads) == 0 {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
wrote := false
|
||||
rev, err := h.Source.Set(t.Branch, func(prev *Source) (Source, bool) {
|
||||
wrote = false
|
||||
next := Source{Group: t.Group, Heads: heads}
|
||||
if prev != nil {
|
||||
same := true
|
||||
for r, c := range heads {
|
||||
if prev.Heads[r] != c {
|
||||
same = false
|
||||
}
|
||||
}
|
||||
if same {
|
||||
return Source{}, false // a merged message moved these heads already: no revision (A-464c)
|
||||
}
|
||||
next.Heads = map[string]string{}
|
||||
for r, c := range prev.Heads {
|
||||
next.Heads[r] = c
|
||||
@@ -118,12 +153,13 @@ func (h *Holder) moveSource(t meshcatalog.TakeInTaken) error {
|
||||
next.Heads[r] = c
|
||||
}
|
||||
}
|
||||
wrote = true
|
||||
return next, true
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("source.%s cannot be moved: %w", t.Branch, err)
|
||||
}
|
||||
if h.SaySeat == nil {
|
||||
if h.SaySeat == nil || !wrote {
|
||||
return nil
|
||||
}
|
||||
at := h.Now().UTC()
|
||||
|
||||
@@ -84,7 +84,7 @@ func TestATakeInTakenOpensOneRecordAndMovesItsBranchsSource(t *testing.T) {
|
||||
if s := src.vals["main"]; s.Group != "feat" || s.Heads["novox/app"] != merge {
|
||||
t.Fatalf("source.main %+v", s)
|
||||
}
|
||||
if len(events) != 2 || !strings.HasSuffix(events[0].subject, ".event.source-moved.main") {
|
||||
if len(events) != 1 || !strings.HasSuffix(events[0].subject, ".event.source-moved.main") {
|
||||
t.Fatalf("said %+v", events)
|
||||
}
|
||||
drop := json.RawMessage(`{"v":1,"kind":"taken","key":"m.00000000000000000002","what":"revision","dropped":true,"branch":"main","source":8,` +
|
||||
@@ -92,7 +92,7 @@ func TestATakeInTakenOpensOneRecordAndMovesItsBranchsSource(t *testing.T) {
|
||||
if err := c.Hear(stdio.Envelope{Key: "mesh-delivery-indexer.take-in.taken", Body: drop}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if kept, _ := h.Records.Kept(); len(kept) != 1 || len(events) != 2 {
|
||||
if kept, _ := h.Records.Kept(); len(kept) != 1 || len(events) != 1 {
|
||||
t.Fatalf("a hand drop opened %d records and said %d events", len(kept), len(events))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,6 +22,8 @@
|
||||
"git.pull-updated",
|
||||
"git.pull-merged",
|
||||
"git.pull-closed",
|
||||
"mesh-catalog.registration-changed",
|
||||
"mesh-delivery-forge-bridge.forge.*.*.merged",
|
||||
"mesh-conditions.findings-asked"
|
||||
],
|
||||
"emits": [
|
||||
@@ -351,8 +353,10 @@
|
||||
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-forge-bridge/forgemsg": "0.1"
|
||||
},
|
||||
"invokes": [
|
||||
"seat:git.branch",
|
||||
"seat:git.open-pulls",
|
||||
"seat:git.pull",
|
||||
"seat:mesh-catalog.registrations",
|
||||
"seat:mesh-hand-acts.hand-act",
|
||||
"seat:mesh-wants.entry",
|
||||
"seat:node-engine.composition"
|
||||
@@ -379,7 +383,9 @@
|
||||
"gate_record_repair",
|
||||
"gate_records",
|
||||
"gate_source",
|
||||
"gate_source_again"
|
||||
"gate_source_again",
|
||||
"gate_source_follow",
|
||||
"gate_source_start"
|
||||
],
|
||||
"config": {
|
||||
"record-retention": {
|
||||
|
||||
@@ -2,6 +2,19 @@
|
||||
// its own, on its own bus account (`runs-as`), so that its account alone writes the hand-act log (`acts`, its module
|
||||
// log: a stream that keeps every act in order) and `warrants`. It reads the router's record of each ask for a warrant; the controller's log is no longer read
|
||||
// (issue 774). stdout is the MCP channel; what this module says, it says on stderr.
|
||||
// Its first change delivered through its own pipeline: lint, unit, integration and composition, then its want (design 47 G).
|
||||
// Its second, with each step working on this module alone (principle 10).
|
||||
// Its third, once the runner names this module's directory on every step.
|
||||
// Its fourth, once the runner hears each step end and the assigner finds the node-engine.
|
||||
// Its fifth, once each pipeline runs at its own pinned catalog revision.
|
||||
// Its sixth, once the node-engine composes a pipeline's revision.
|
||||
// Its seventh, once the node-engine may read that revision.
|
||||
// Its eighth, once the tool that answers it lets the node-engine read it.
|
||||
// Its ninth, once the node-engine reads each manifest digest in the form its state keys use.
|
||||
// Its tenth, once the composed input travels in chunks.
|
||||
// Its eleventh, once a machine whose node-engine is not heard running composes with the catalog's node-engine.
|
||||
// Its twelfth, once the start branches are merged and taken in.
|
||||
// Its thirteenth, once the assigner hears a composition run's end on the kept seat events.
|
||||
package main
|
||||
|
||||
import (
|
||||
|
||||
@@ -601,14 +601,14 @@ func TestAnAskIsRefusedInWordsToItsAsker(t *testing.T) {
|
||||
}
|
||||
}
|
||||
all := strings.Join(refused, "\n")
|
||||
// A fourth offered ask is never refused (ADR 0666 §12): it is taken and the asker's oldest is held back.
|
||||
// A fourth offered ask is never refused and none is held back (design 47 G A-424).
|
||||
for _, want := range []string{"d-1: the ask is refused: an ask that destroys lasts at most", "d-2: its words carry"} {
|
||||
if !strings.Contains(all, want) {
|
||||
t.Errorf("not refused: %q in\n%s", want, all)
|
||||
}
|
||||
}
|
||||
if strings.Contains(all, "d-6") || store.asks["mesh-delivery.d-6"].State != StateOpen || store.asks["mesh-delivery.d-3"].State != StateHeldBack {
|
||||
t.Errorf("the fourth ask was refused or the oldest not held back: d-6 %s, d-3 %s", store.asks["mesh-delivery.d-6"].State, store.asks["mesh-delivery.d-3"].State)
|
||||
if strings.Contains(all, "d-6") || store.asks["mesh-delivery.d-6"].State != StateOpen || store.asks["mesh-delivery.d-3"].State != StateOpen {
|
||||
t.Errorf("the fourth ask was refused or the oldest held back: d-6 %s, d-3 %s", store.asks["mesh-delivery.d-6"].State, store.asks["mesh-delivery.d-3"].State)
|
||||
}
|
||||
// A refused ask is kept as a closed record with its outcome and words (design 47 G §570), and `asks` lists it refused.
|
||||
if rec, kept := store.asks["mesh-delivery.d-1"]; !kept || rec.State != string(occ.OutcomeRefused) || rec.Warrant == nil ||
|
||||
|
||||
@@ -563,9 +563,9 @@ func (h *Holder) TakeAsk(verb, asker string, body json.RawMessage) error {
|
||||
h.refuseAsk(asker, a, "its whole words carry "+refusal.String()+", which may not leave the mesh on any channel: withhold it")
|
||||
return nil
|
||||
}
|
||||
// A fourth offered ask is taken, never refused: the asker's oldest offered one is held back once this one is recorded
|
||||
// (holdBackOldest). An ask with the same `about` replaces the open one, a held-back one too.
|
||||
open, replaced := 0, ""
|
||||
// Every ask is offered when it is raised: the channel keeps no count of an asker's open asks and holds none back for room
|
||||
// (design 47 G A-424). An ask with the same `about` replaces the open one, a held-back one of before too.
|
||||
replaced := ""
|
||||
h.mu.Lock()
|
||||
for k, e := range h.asks {
|
||||
if e.rec.Asker != asker || !liveState(e.rec.State) {
|
||||
@@ -573,14 +573,9 @@ func (h *Holder) TakeAsk(verb, asker string, body json.RawMessage) error {
|
||||
}
|
||||
if a.About != "" && e.rec.Ask.About == a.About {
|
||||
replaced = k
|
||||
continue
|
||||
}
|
||||
if e.rec.State != StateHeldBack {
|
||||
open++
|
||||
}
|
||||
}
|
||||
h.mu.Unlock()
|
||||
holdBack := open >= occ.MostOpen
|
||||
rec := AskRecord{Asker: asker, Ask: a, State: StateOpen, Opened: now, Tickets: h.ticketsFor(a)}
|
||||
if len(rec.Tickets) == 0 {
|
||||
// Held, not refused (ADR 0397): the ask breaks no rule, and the operator is asked when a channel can carry it.
|
||||
@@ -611,9 +606,6 @@ func (h *Holder) TakeAsk(verb, asker string, body json.RawMessage) error {
|
||||
} else {
|
||||
h.keepAsk(key, rec, 0)
|
||||
}
|
||||
if holdBack {
|
||||
h.holdBackOldest(asker, key)
|
||||
}
|
||||
if rec.State == StateHeld {
|
||||
h.Logf("[messenger] %s asks %q (%s): held, not shown: %s", asker, a.Headline, a.ID, rec.HeldWhy)
|
||||
return nil
|
||||
|
||||
@@ -105,7 +105,8 @@ func TestAHeldAskExpiresAsAnyOpenAskDoes(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestAHeldAskCountsTowardTheOffersOfItsAskerAndTheOldestIsHeldBack(t *testing.T) {
|
||||
// Design 47 G A-424: four held asks of one asker are taken and held for want of a channel, and none is held back for room.
|
||||
func TestFourHeldAsksOfOneAskerAreHeldAndNoneHeldBack(t *testing.T) {
|
||||
h, _, _, c, store, w := newRouter(t)
|
||||
withoutVerifiedSender(h)
|
||||
for i := 1; i <= 4; i++ {
|
||||
@@ -114,8 +115,8 @@ func TestAHeldAskCountsTowardTheOffersOfItsAskerAndTheOldestIsHeldBack(t *testin
|
||||
submit(t, h, "mesh-delivery", a)
|
||||
c.pass(time.Second)
|
||||
}
|
||||
if got := w.of("mesh-delivery"); len(got) != 0 || store.asks["mesh-delivery.d-1"].State != StateHeldBack || store.asks["mesh-delivery.d-4"].State != StateHeld {
|
||||
t.Fatalf("%+v: a fourth is taken, never refused; the oldest is held back (%s)", got, store.asks["mesh-delivery.d-1"].State)
|
||||
if got := w.of("mesh-delivery"); len(got) != 0 || store.asks["mesh-delivery.d-1"].State != StateHeld || store.asks["mesh-delivery.d-4"].State != StateHeld {
|
||||
t.Fatalf("%+v: a fourth is taken, never refused, and none is held back (d-1 %s)", got, store.asks["mesh-delivery.d-1"].State)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -5,14 +5,12 @@ import (
|
||||
oc "git.novox.be/novox/mesh-contracts/operator-and-conversation/go/seats/operatorchannel"
|
||||
"sort"
|
||||
"time"
|
||||
|
||||
occ "git.novox.be/novox/mesh-contracts/operator-and-conversation/go/clients/operatorchannelclient"
|
||||
)
|
||||
|
||||
// An ask held back (novox/hq ADR 0666 §12, design 46 § 6): an asker has at most MostOpen asks offered at once; a fourth
|
||||
// is offered and nothing is refused, so no ask is hidden: the asker's oldest offered ask is held back, its tickets
|
||||
// withdrawn and its message edited to say it waits behind newer asks, and it is offered again, with new tickets, when
|
||||
// one of the asker's offered asks ends. `held` is another state, an ask no ready channel can carry (ADR 0397).
|
||||
// An ask held back (novox/hq ADR 0666 §12, design 46 § 6) was one kept open behind newer asks of its asker, offered again when
|
||||
// one of them ended. The channel holds none back any more (design 47 G A-424): every ask is offered when it is raised, and one
|
||||
// held back before the change is offered at the router's start (offerAllHeldBack) and by ask-again. `held` is another state, an
|
||||
// ask no ready channel can carry (ADR 0397).
|
||||
|
||||
// StateHeldBack is an ask kept open behind newer asks of its asker.
|
||||
const StateHeldBack = "held-back"
|
||||
@@ -90,50 +88,35 @@ func (h *Holder) freshAsk(key string) (AskRecord, uint64, bool) {
|
||||
return rec, rev, true
|
||||
}
|
||||
|
||||
// holdBackOldest holds back the asker's oldest offered ask other than except (the newest, just taken): its tickets
|
||||
// are withdrawn, it stays open, and its own message is edited to say it waits. One held for want of a channel counts as offered
|
||||
// and is held back the same way. The work lock is held.
|
||||
func (h *Holder) holdBackOldest(asker, except string) {
|
||||
h.mu.Lock()
|
||||
key := h.pickLocked(asker, except, StateOpen, StateHeld)
|
||||
h.mu.Unlock()
|
||||
if key == "" {
|
||||
return
|
||||
// offerHeldBack offers the asker's held-back asks of before the change again (A-424): there is no room to wait for. The work
|
||||
// lock is held.
|
||||
func (h *Holder) offerHeldBack(asker string) {
|
||||
for {
|
||||
h.mu.Lock()
|
||||
key := h.pickLocked(asker, "", StateHeldBack)
|
||||
h.mu.Unlock()
|
||||
if key == "" {
|
||||
return
|
||||
}
|
||||
h.offerOneHeldBack(key)
|
||||
if after := h.askOf(key); after == nil || after.rec.State == StateHeldBack {
|
||||
return // not offered (its record changed meanwhile): left for the next start rather than tried forever
|
||||
}
|
||||
}
|
||||
rec, rev, ok := h.freshAsk(key)
|
||||
if !ok || (rec.State != StateOpen && rec.State != StateHeld) {
|
||||
return
|
||||
}
|
||||
now := h.Now()
|
||||
rec.State, rec.HeldBackSince, rec.Tickets, rec.HeldSince, rec.HeldWhy = StateHeldBack, now, nil, time.Time{}, ""
|
||||
if !h.saveHeld(key, rec, rev) {
|
||||
return
|
||||
}
|
||||
h.Logf("[messenger] %s's ask %q (%s) is held back behind its newer asks: it is offered again when one ends", asker, rec.Ask.Headline, rec.Ask.ID)
|
||||
h.mu.Lock()
|
||||
r := h.open[rec.Record]
|
||||
h.mu.Unlock()
|
||||
if r == nil || r.Origin != "ask" {
|
||||
return // a condition's message is the newer ask's to show
|
||||
}
|
||||
h.mu.Lock()
|
||||
r.Answered = "Waits behind newer questions of " + asker + ".\nIt is offered again, with its answers, when one of them ends."
|
||||
h.mu.Unlock()
|
||||
h.edit(r, "answered", false)
|
||||
h.persist(r)
|
||||
}
|
||||
|
||||
// offerHeldBack offers the asker's oldest held-back ask again when a slot is free: new tickets, its message edited back
|
||||
// to the question. The work lock is held.
|
||||
func (h *Holder) offerHeldBack(asker string) {
|
||||
if h.offeredCount(asker) >= occ.MostOpen {
|
||||
return
|
||||
}
|
||||
// offerAllHeldBack offers every ask held back before the change, at each tick of the router (A-424). The work lock is held.
|
||||
func (h *Holder) offerAllHeldBack() {
|
||||
h.mu.Lock()
|
||||
key := h.pickLocked(asker, "", StateHeldBack)
|
||||
askers := map[string]bool{}
|
||||
for _, e := range h.asks {
|
||||
if e.rec.State == StateHeldBack {
|
||||
askers[e.rec.Asker] = true
|
||||
}
|
||||
}
|
||||
h.mu.Unlock()
|
||||
if key != "" {
|
||||
h.offerOneHeldBack(key)
|
||||
for asker := range askers {
|
||||
h.offerHeldBack(asker)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -172,9 +155,9 @@ func (h *Holder) offerOneHeldBack(key string) {
|
||||
h.showAsk(key, rec, now)
|
||||
}
|
||||
|
||||
// AskAgain is the seat operator-channel's verb ask-again (G §394, §450): offer a held-back ask now, holding back the
|
||||
// asker's oldest offered ask in its place when the asker already has the most offered. It answers the ask's state after it,
|
||||
// or the error that says why it was not offered.
|
||||
// AskAgain is the seat operator-channel's verb ask-again (G §394, §450): it offers an ask held back before the channel stopped
|
||||
// holding any back (A-424), and holds back nothing in its place. It answers the ask's state after it, or the error that says why
|
||||
// it was not offered.
|
||||
func (h *Holder) AskAgain(key string) oc.AskAgainAnswer {
|
||||
h.work.Lock()
|
||||
defer h.work.Unlock()
|
||||
@@ -186,10 +169,7 @@ func (h *Holder) AskAgain(key string) oc.AskAgainAnswer {
|
||||
case !ok:
|
||||
return oc.AskAgainAnswer{Error: fmt.Sprintf("this router holds no ask %q: its key is <asker>.<id>, as operator-channel.asks lists", key)}
|
||||
case rec.State != StateHeldBack:
|
||||
return oc.AskAgainAnswer{Error: fmt.Sprintf("the ask %s is %s, not held back: only a held-back ask is offered again", key, rec.State)}
|
||||
}
|
||||
if h.offeredCount(rec.Asker) >= occ.MostOpen {
|
||||
h.holdBackOldest(rec.Asker, key)
|
||||
return oc.AskAgainAnswer{Error: fmt.Sprintf("the ask %s is %s, not held back: only a held-back ask is offered again, and the channel holds none back any more", key, rec.State)}
|
||||
}
|
||||
h.offerOneHeldBack(key)
|
||||
got, _, _ := h.freshAsk(key)
|
||||
|
||||
@@ -9,100 +9,50 @@ import (
|
||||
occ "git.novox.be/novox/mesh-contracts/operator-and-conversation/go/clients/operatorchannelclient"
|
||||
)
|
||||
|
||||
// ADR 0666 §12, design 46 §6 and test 71: an asker has at most three asks offered at once; a fourth is offered and the
|
||||
// oldest is held back (its tickets withdrawn, its message edited, its record open), offered again with new tickets
|
||||
// when one of the three ends, offered now by messenger_ask_again; an ask with the same `about` replaces a held-back
|
||||
// one, and a Decline ends an ask whose asker may ask again at once.
|
||||
func TestNoAskIsHiddenTheOldestOfFourIsHeldBackAndOfferedAgain(t *testing.T) {
|
||||
h, tg, _, c, store, w := newRouter(t)
|
||||
// Design 47 G A-424: the channel keeps no count of an asker's open asks and holds none back: every ask is offered when it is
|
||||
// raised, five of one asker at once too, and none is refused. An ask held back before the change is offered again at the
|
||||
// router's next tick, with new tickets; ask-again offers one now and holds nothing back in its place.
|
||||
func TestEveryAskIsOfferedAndOneHeldBackBeforeIsOfferedAgain(t *testing.T) {
|
||||
h, _, _, c, store, w := newRouter(t)
|
||||
link(h, store, c, "42", c.now().Add(-2*time.Hour))
|
||||
ask := func(id, about string) {
|
||||
state := func(id string) string { return store.asks["mesh-delivery."+id].State }
|
||||
for i, id := range []string{"d-1", "d-2", "d-3", "d-4", "d-5"} {
|
||||
a := ackAsk(c, id)
|
||||
a.About = about
|
||||
a.About = "about-" + string(rune('1'+i))
|
||||
submit(t, h, "mesh-delivery", a)
|
||||
c.pass(time.Second)
|
||||
settle(h, c)
|
||||
}
|
||||
state := func(id string) string { return store.asks["mesh-delivery."+id].State }
|
||||
for i, id := range []string{"d-1", "d-2", "d-3"} {
|
||||
ask(id, "about-"+string(rune('1'+i)))
|
||||
}
|
||||
oldTicket := ""
|
||||
for tk := range store.asks["mesh-delivery.d-1"].Tickets {
|
||||
oldTicket = tk
|
||||
}
|
||||
ask("d-4", "about-4")
|
||||
// M1: the fourth reaches the channel, the oldest is held back, none is refused.
|
||||
if len(w.of("mesh-delivery")) != 0 || state("d-4") != StateOpen || state("d-1") != StateHeldBack {
|
||||
t.Fatalf("refused %+v; d-4 %s, d-1 %s", w.of("mesh-delivery"), state("d-4"), state("d-1"))
|
||||
}
|
||||
if rec := store.asks["mesh-delivery.d-1"]; len(rec.Tickets) != 0 || rec.HeldBackSince.IsZero() {
|
||||
t.Errorf("held back %+v: tickets withdrawn, since set", rec)
|
||||
}
|
||||
if m, ok := tg.edits["1"]; !ok || len(m.Actions) != 0 || !strings.Contains(m.Note, "Waits behind newer questions of mesh-delivery") {
|
||||
t.Errorf("its message was not edited to say it waits: %v %+v", ok, m)
|
||||
}
|
||||
if _, err := h.Chosen("telegram", choiceOf(oldTicket, "42", c.now())); err == nil || !strings.Contains(err.Error(), "waits behind newer questions") || len(w.of("mesh-delivery")) != 0 {
|
||||
t.Errorf("a tap of a withdrawn ticket: %v %+v", err, w.of("mesh-delivery"))
|
||||
}
|
||||
// M3: asks lists it with since when and the newer asks it waits behind; open includes it.
|
||||
q, _ := AsksQueryOf(map[string]any{"key": "mesh-delivery.d-1"})
|
||||
raw, _ := json.Marshal(q.Narrow(h.AsksNow()))
|
||||
for _, want := range []string{`"state":"held-back"`, `"held-since":"`, `"behind":["mesh-delivery.d-2","mesh-delivery.d-3","mesh-delivery.d-4"]`} {
|
||||
if !strings.Contains(string(raw), want) {
|
||||
t.Errorf("asks lacks %s: %s", want, raw)
|
||||
for _, id := range []string{"d-1", "d-2", "d-3", "d-4", "d-5"} {
|
||||
if state(id) != StateOpen || len(store.asks["mesh-delivery."+id].Tickets) == 0 {
|
||||
t.Fatalf("%s is %s with %d tickets: every ask is offered", id, state(id), len(store.asks["mesh-delivery."+id].Tickets))
|
||||
}
|
||||
}
|
||||
if q, _ = AsksQueryOf(nil); !strings.Contains(string(mustJSON(q.Narrow(h.AsksNow()))), "mesh-delivery.d-1") {
|
||||
t.Error("the open asks leave out the held-back one")
|
||||
}
|
||||
// M5: an ask with the same about replaces a held-back one; the three offered stay three, so the oldest of them is held back.
|
||||
ask("d-5", "about-1")
|
||||
if state("d-1") != string(occ.OutcomeReplaced) || state("d-2") != StateHeldBack || state("d-5") != StateOpen || state("d-3") != StateOpen {
|
||||
t.Fatalf("after d-5 about the held-back ask's thing: d-1 %s d-2 %s d-3 %s d-5 %s", state("d-1"), state("d-2"), state("d-3"), state("d-5"))
|
||||
}
|
||||
// M2: when one of the offered ends, the oldest held back is offered again with new tickets and its message asks again.
|
||||
if a := h.AskAgain("mesh-delivery.d-3"); !strings.Contains(a.Error, "not held back") {
|
||||
t.Errorf("ask-again of an offered ask: %+v", a)
|
||||
}
|
||||
if err := h.TakeAsk("cancel", "mesh-delivery", []byte(`{"id":"d-3"}`)); err != nil {
|
||||
// One held back before the change, as the store kept it.
|
||||
key := "mesh-delivery.d-2"
|
||||
got, rev, _ := store.GetAsk(key)
|
||||
got.State, got.Tickets, got.HeldBackSince = StateHeldBack, nil, c.now()
|
||||
rev, err := store.UpdateAsk(key, *got, rev)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
settle(h, c)
|
||||
if rec := store.asks["mesh-delivery.d-2"]; rec.State != StateOpen || len(rec.Tickets) == 0 || !rec.HeldBackSince.IsZero() {
|
||||
t.Fatalf("d-2 after d-3 ended: %+v", rec)
|
||||
}
|
||||
if m := tg.edits["2"]; len(m.Actions) == 0 || strings.Contains(m.Note, "Waits behind") {
|
||||
t.Errorf("its message does not ask again: %+v", m)
|
||||
}
|
||||
// M4: ask-again by hand offers a held-back ask now, holding back the oldest offered one; the seat operator-channel's ask-again (G §450).
|
||||
ask("d-6", "about-6")
|
||||
ask("d-7", "about-7")
|
||||
held := ""
|
||||
for _, id := range []string{"d-2", "d-4", "d-5", "d-6", "d-7"} {
|
||||
if state(id) == StateHeldBack {
|
||||
held = id
|
||||
}
|
||||
}
|
||||
if held == "" {
|
||||
t.Fatal("no ask is held back after two more")
|
||||
h.keepAsk(key, *got, rev)
|
||||
if a := h.AskAgain("mesh-delivery.d-1"); !strings.Contains(a.Error, "not held back") {
|
||||
t.Errorf("ask-again of an offered ask: %+v", a)
|
||||
}
|
||||
if a := h.AskAgain("mesh-delivery.d-none"); a.Error == "" || a.State != "" {
|
||||
t.Errorf("ask-again of an ask the router holds none of: %+v", a)
|
||||
}
|
||||
if a := h.AskAgain("mesh-delivery." + held); a.Error != "" || a.State != string(StateOpen) || state(held) != StateOpen {
|
||||
t.Fatalf("%s is %s; ask-again answered %+v", held, state(held), a)
|
||||
h.Tick()
|
||||
settle(h, c)
|
||||
if rec := store.asks[key]; rec.State != StateOpen || len(rec.Tickets) == 0 || !rec.HeldBackSince.IsZero() {
|
||||
t.Fatalf("the ask held back before the change, after a tick: %+v", rec)
|
||||
}
|
||||
n := 0
|
||||
for _, id := range []string{"d-2", "d-4", "d-5", "d-6", "d-7"} {
|
||||
if state(id) == StateHeldBack {
|
||||
n++
|
||||
for _, id := range []string{"d-1", "d-3", "d-4", "d-5"} {
|
||||
if state(id) != StateOpen {
|
||||
t.Errorf("%s is %s after the held-back one was offered: nothing is held back in its place", id, state(id))
|
||||
}
|
||||
}
|
||||
if n != 2 || state("d-5") != StateHeldBack {
|
||||
t.Errorf("%d asks held back after ask-again (d-5 %s): the oldest offered one took its place", n, state("d-5"))
|
||||
}
|
||||
// A Decline ends an ask; its asker may ask again at once, and nothing is refused.
|
||||
for _, wt := range w.of("mesh-delivery") {
|
||||
if wt.Outcome == occ.OutcomeRefused {
|
||||
t.Errorf("refused: %+v", wt)
|
||||
|
||||
@@ -778,6 +778,7 @@ func (h *Holder) Tick() {
|
||||
h.mu.Unlock()
|
||||
sort.Slice(recs, func(i, j int) bool { return recs[i].Raised.Before(recs[j].Raised) })
|
||||
h.sweepAsks(now)
|
||||
h.offerAllHeldBack() // held back before the channel stopped holding any back, offered at the first tick (design 47 G A-424)
|
||||
h.releaseHeld()
|
||||
h.offerUnsent()
|
||||
h.breakGlass()
|
||||
|
||||
@@ -197,13 +197,15 @@ func allTools() []stdio.Tool {
|
||||
out = append(out, keeperHealthTool(StatusInState, HeldInState, jsz))
|
||||
out = append(out, readTools(jsz, LiveMsgGet(LiveBus()))...)
|
||||
rs := RequestsInState()
|
||||
out = append(out, snapshotListTool(rs, bundleKeepDays())...)
|
||||
out = append(out, snapshotListTool(rs, bundleKeepDays)...)
|
||||
out = append(out, plansTool(PlansInState))
|
||||
return append(out, requestTools(rs)...)
|
||||
}
|
||||
|
||||
// bundleKeepDays is the setting snapshot-keep-days as this bundle's configuration holds it (design 47 G A-112), its
|
||||
// default when the runtime gives none.
|
||||
// default when the runtime gives none. It is read when a tool asks, never while the tools are listed: a config read
|
||||
// waits for the runtime's channel, which opens only once the bundle serves, so a read before Serve never returns and
|
||||
// the bundle never answers initialize.
|
||||
func bundleKeepDays() int {
|
||||
if raw, err := stdio.Config("snapshot-keep-days"); err == nil {
|
||||
var n int
|
||||
|
||||
@@ -35,6 +35,7 @@ const (
|
||||
consumerRemoved = "consumer-removed"
|
||||
bucketKeysRemoved = "bucket-keys-removed"
|
||||
streamRemoved = "stream-removed"
|
||||
streamPurged = "stream-purged"
|
||||
// maxKeysShown bounds the keys an answer lists; the count is always whole.
|
||||
maxKeysShown = 200
|
||||
)
|
||||
@@ -332,7 +333,6 @@ func RemoveStream(ctx context.Context, bus Bus, said Said, a map[string]any) (an
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// removalTools are the module's three tools that change the bus; every other tool here reads.
|
||||
// removalTools are the three removal tools. **They refuse an entry of the new form** (design 25 § 12.3; ADR 0595 §4 as
|
||||
// its insight of 2026-11-03 has it): the mesh deletes a module's kept data only through the bus module's drop and
|
||||
// recreate, so a stream, a bucket or a consumer of a stream the keeper made (its state asked, read through newForm) is
|
||||
@@ -360,6 +360,11 @@ func removalTools(bus Bus, said Said, newForm heldEntries) []stdio.Tool {
|
||||
Description: "Removes (deletes) one named JetStream stream whole, e.g. a stream the controller left: CONTROL, ASSIGNMENTS, one of its buckets as KV_<bucket> or a node's declaration files as OBJ_declaration-files-<node> (novox/hq ADR 0590 §6; a bucket's whole removal is this tool's, never nats_bucket_remove's). Needs `why`, kept in the audit trail; reads the stream first and answers its messages, bytes and consumers; refuses a stream that still has a consumer; a stream already gone is answered as such and is no error, so asking again is safe. `dry_run` only reads. The bus refuses any stream the module's credential is not granted.",
|
||||
Input: map[string]any{"stream": str("the stream, e.g. ASSIGNMENTS"), "why": str("why it is removed, for the audit trail (required)"), "dry_run": map[string]any{"type": "boolean", "description": "read the stream, remove nothing"}},
|
||||
Run: run(RemoveStream)},
|
||||
{Name: "nats_stream_purge",
|
||||
Description: "Purges (removes) the messages of one named JetStream stream below a sequence, keeping that sequence and every later one: e.g. a build seat's work queue (SEAT_NODE_BUILD_GO) emptied of old asks nobody waits for, up to the first ask still wanted. Needs `why`, kept in the audit trail; reads the stream first and answers its first and last sequence and its messages before and after; the stream and its consumers stay; refuses a `below` past the stream's end (a whole purge is not this tool's). `dry_run` only reads. Read the sequences with nats_stream_messages. The bus refuses any stream the module's credential is not granted.",
|
||||
Input: map[string]any{"stream": str("the stream, e.g. SEAT_NODE_BUILD_GO"), "below": num("the first sequence kept: every message below it is purged"),
|
||||
"why": str("why they are purged, for the audit trail (required)"), "dry_run": map[string]any{"type": "boolean", "description": "read the stream, purge nothing"}},
|
||||
Run: run(PurgeStream)},
|
||||
{Name: "nats_bucket_remove",
|
||||
Description: "Removes (purges) keys from a key-value bucket: one node's keys (`node`, the keys starting `<node>.`) or the keys under a `prefix`, e.g. a full acts bucket of the node-engine. Needs `why`, kept in the audit trail; never the whole bucket. Counts the keys first and lists them (`dry_run` only counts; `expect` refuses when the count differs); removes only those keys, and their history. See the buckets and their keys with nats_buckets. The bus refuses any bucket the module's credential is not granted.",
|
||||
Input: map[string]any{"bucket": str("the bucket as nats_buckets lists it, e.g. mesh-host_acts"), "node": str("a node whose keys (`<node>.`) go"), "prefix": str("key prefix instead of node"),
|
||||
@@ -388,3 +393,87 @@ func refuseNewForm(newForm heldEntries, stream string) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// StreamPurged is what nats_stream_purge answers and says.
|
||||
type StreamPurged struct {
|
||||
Stream string `json:"stream"`
|
||||
Below uint64 `json:"below"`
|
||||
Why string `json:"why"`
|
||||
DryRun bool `json:"dry_run"`
|
||||
First uint64 `json:"first_seq"`
|
||||
Last uint64 `json:"last_seq"`
|
||||
Before uint64 `json:"messages_before"`
|
||||
After uint64 `json:"messages_after"`
|
||||
Purged uint64 `json:"purged"`
|
||||
Said string `json:"said"`
|
||||
At time.Time `json:"at"`
|
||||
}
|
||||
|
||||
// PurgeStream removes the messages of one named stream below a sequence, keeping that sequence and every later one,
|
||||
// after reading the stream, and says so: a work queue whose old asks nobody waits for any more is emptied up to the
|
||||
// first ask still wanted (issue 1952's removal tools, one more of their kind). The stream and its consumers stay.
|
||||
func PurgeStream(ctx context.Context, bus Bus, said Said, a map[string]any) (any, error) {
|
||||
w, err := why(a)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
stream := arg(a, "stream")
|
||||
if !name.MatchString(stream) {
|
||||
return nil, errors.New("stream is required, one stream's name (letters, digits, - and _): nothing was purged")
|
||||
}
|
||||
below, ok := a["below"].(float64)
|
||||
if !ok || below < 2 || below != float64(uint64(below)) {
|
||||
return nil, errors.New("below is required, the first sequence kept (a whole number above 1): nothing was purged")
|
||||
}
|
||||
dry, _ := a["dry_run"].(bool)
|
||||
js, closeBus, err := bus(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer closeBus()
|
||||
out := StreamPurged{Stream: stream, Below: uint64(below), Why: w, DryRun: dry, At: time.Now().UTC()}
|
||||
sctx, cancel := step(ctx)
|
||||
s, err := js.Stream(sctx, stream)
|
||||
cancel()
|
||||
if err != nil {
|
||||
return nil, unanswered(err, fmt.Sprintf("the stream %s could not be read, so nothing was purged", stream))
|
||||
}
|
||||
sctx, cancel = step(ctx)
|
||||
info, err := s.Info(sctx)
|
||||
cancel()
|
||||
if err != nil {
|
||||
return nil, unanswered(err, fmt.Sprintf("the stream %s could not be read, so nothing was purged", stream))
|
||||
}
|
||||
out.First, out.Last, out.Before = info.State.FirstSeq, info.State.LastSeq, info.State.Msgs
|
||||
if out.Below > out.Last+1 {
|
||||
return nil, fmt.Errorf("%s ends at sequence %d: below %d would purge it whole, which is not this tool's; nothing was purged", stream, out.Last, out.Below)
|
||||
}
|
||||
if dry {
|
||||
out.Said = fmt.Sprintf("%s holds %d messages, sequences %d to %d; those below %d would be purged", stream, out.Before, out.First, out.Last, out.Below)
|
||||
return out, nil
|
||||
}
|
||||
sctx, cancel = step(ctx)
|
||||
err = s.Purge(sctx, jetstream.WithPurgeSequence(out.Below))
|
||||
cancel()
|
||||
if err != nil {
|
||||
return nil, unanswered(err, fmt.Sprintf("the messages of %s below %d were not purged", stream, out.Below))
|
||||
}
|
||||
sctx, cancel = step(ctx)
|
||||
info, err = s.Info(sctx)
|
||||
cancel()
|
||||
if err != nil {
|
||||
return nil, unanswered(err, fmt.Sprintf("the purge of %s below %d was accepted but could not be confirmed", stream, out.Below))
|
||||
}
|
||||
if info.State.FirstSeq < out.Below && info.State.Msgs > 0 {
|
||||
return nil, fmt.Errorf("%s still starts at sequence %d after the purge below %d was accepted", stream, info.State.FirstSeq, out.Below)
|
||||
}
|
||||
out.After = info.State.Msgs
|
||||
if out.Before > out.After {
|
||||
out.Purged = out.Before - out.After
|
||||
}
|
||||
out.Said = fmt.Sprintf("%s: %d messages below sequence %d purged, %d kept", stream, out.Purged, out.Below, out.After)
|
||||
if err := said(streamPurged, out); err != nil {
|
||||
return nil, fmt.Errorf("the messages of %s below %d WERE purged (%s) but the audit event could not be said: %w", stream, out.Below, w, err)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
@@ -324,7 +324,7 @@ func heldRequest(store keeper.Store) func(id string) (*keeper.Request, error) {
|
||||
}
|
||||
|
||||
// snapshotListTool is nats_snapshots, which the tools bundle keeps beside the verbs the keeper's process answers.
|
||||
func snapshotListTool(rs requestStore, keepDays int) []stdio.Tool {
|
||||
func snapshotListTool(rs requestStore, keepDays func() int) []stdio.Tool {
|
||||
return []stdio.Tool{
|
||||
{Name: "nats_snapshots",
|
||||
Description: "The keeper's snapshots of kept data taken before a drop or recreate, from its state requests: each one's request, module, local name, stream, size, digest, time, whether it is protected (restored only in place, never sent), whether it is expired (past snapshot-keep-days, removed at the keeper's next request) and whether it is recorded (a snapshot of before milestone G holds no digest and is sent or removed by hand only).",
|
||||
@@ -344,7 +344,7 @@ func snapshotListTool(rs requestStore, keepDays int) []stdio.Tool {
|
||||
continue
|
||||
}
|
||||
out = append(out, map[string]any{"request": r.ID, "module": r.Module, "local": r.Local, "stream": keeper.BusName(r.Module, r.Local),
|
||||
"size": r.Size, "digest": r.Digest, "at": r.At, "protected": r.Protected, "expired": r.Expired(rs.Now(), keepDays), "recorded": r.Recorded()})
|
||||
"size": r.Size, "digest": r.Digest, "at": r.At, "protected": r.Protected, "expired": r.Expired(rs.Now(), keepDays()), "recorded": r.Recorded()})
|
||||
}
|
||||
return map[string]any{"snapshots": out}, nil
|
||||
}},
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user