Compare commits
118
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
25e6a46b0a | ||
|
|
6476e3595a | ||
|
|
b0ed577596 | ||
|
|
5326fb6142 | ||
|
|
af8173651b | ||
|
|
c791e5aff2 | ||
|
|
fb71ff5439 | ||
|
|
7d07459c8e | ||
|
|
3b64060968 | ||
|
|
fb2ff9e9db | ||
|
|
3443760205 | ||
|
|
faaebee7df | ||
|
|
7ae223d4ff | ||
|
|
33bb6e8e41 | ||
|
|
319f535002 | ||
|
|
e0bd0bedc4 | ||
|
|
92722b95bf | ||
|
|
1a54a432cf | ||
|
|
758a8ad653 | ||
|
|
3dd92f1c4b | ||
|
|
4150402020 | ||
|
|
2b06ab31d7 | ||
|
|
739fb6bf47 | ||
|
|
5dc8cb343b | ||
|
|
98da0e0b68 | ||
|
|
30a3e665e0 | ||
|
|
4500d98ca6 | ||
|
|
2785c69d95 | ||
|
|
280adcce68 | ||
|
|
22b92b827d | ||
|
|
ed14a10006 | ||
|
|
575fe56243 | ||
|
|
3ec18c9cd2 | ||
|
|
872ed19648 | ||
|
|
39ed20c5bc | ||
|
|
d3d9ff5183 | ||
|
|
1697b7041c | ||
|
|
46990669de | ||
|
|
e335e859d5 | ||
|
|
1d8839640e | ||
|
|
9afbd35759 | ||
|
|
7519191861 | ||
|
|
c63b4ae31d | ||
|
|
a6132b32f6 | ||
|
|
bff5e98b30 | ||
|
|
74dd8af699 | ||
|
|
ac4ed46359 | ||
|
|
0cb4090096 | ||
|
|
ee5e7f232f | ||
|
|
6b17965d98 | ||
|
|
b93253e698 | ||
|
|
681de09f04 | ||
|
|
54fe0dd7cc | ||
|
|
cb5de6fd31 | ||
|
|
4e381c323b | ||
|
|
5d2d0bade8 | ||
|
|
872a02b378 | ||
|
|
83012ad97b | ||
|
|
607a1d76ea | ||
|
|
41610e903f | ||
|
|
e3fff86f1a | ||
|
|
eac56f9990 | ||
|
|
e866a64be3 | ||
|
|
6f744d6602 | ||
|
|
7765947476 | ||
|
|
410451a2a2 | ||
|
|
69940d1019 | ||
|
|
34f55bdf79 | ||
|
|
babcd1bb31 | ||
|
|
d545299a71 | ||
|
|
086745edd5 | ||
|
|
7030e1420c | ||
|
|
b5c05b326a | ||
|
|
ebf4d9896b | ||
|
|
02371b32de | ||
|
|
0bc03bba35 | ||
|
|
0206ec825e | ||
|
|
1cbe0b1294 | ||
|
|
51c27203de | ||
|
|
1ad170705e | ||
|
|
c5a593eb05 | ||
|
|
32e1ed6c55 | ||
|
|
d7b23f50b4 | ||
|
|
6527ebd077 | ||
|
|
31abcad907 | ||
|
|
f589b5af5c | ||
|
|
38ea9ce25c | ||
|
|
09d5758343 | ||
|
|
83482a4513 | ||
|
|
7bd1e74086 | ||
|
|
553bfb0ccf | ||
|
|
3c27ea7a11 | ||
|
|
e2571a6468 | ||
|
|
94bcc14a82 | ||
|
|
461868b9fe | ||
|
|
4fb3ab6df5 | ||
|
|
a5976cf299 | ||
|
|
820840076f | ||
|
|
83802dc4a6 | ||
|
|
efc8c52e68 | ||
|
|
72872b045a | ||
|
|
603d4c7557 | ||
|
|
8d1c0d7d1d | ||
|
|
c01a816385 | ||
|
|
eb443cfc24 | ||
|
|
7c6cf18532 | ||
|
|
a8f2316d08 | ||
|
|
af004a8ef6 | ||
|
|
e1974b770e | ||
|
|
c9beea9a38 | ||
|
|
74488f3e66 | ||
|
|
49cc5b3d7d | ||
|
|
08ec25e367 | ||
|
|
ec305f08e6 | ||
|
|
c050f23860 | ||
|
|
ae9bb17e0f | ||
|
|
be6eb0358f | ||
|
|
e1d2af8d6e |
@@ -77,6 +77,7 @@ func Check(root string) (map[string]map[string][]string, error) {
|
||||
}
|
||||
set := map[string]bool{}
|
||||
setBy(manifest, set)
|
||||
settingWords(manifest, set)
|
||||
reads := map[string]map[string]bool{}
|
||||
consts := map[string]map[string]string{} // directory -> constant -> word
|
||||
var goFiles []string
|
||||
@@ -180,6 +181,17 @@ func setBy(v any, set map[string]bool) {
|
||||
}
|
||||
}
|
||||
|
||||
// settingWords collects the words the node-engine composes for every setting the manifest's `config` declares,
|
||||
// MESH_CONFIG_<KEY>, the key upper-cased with '-' and '.' as '_', into the module's processes and its tool runner slice
|
||||
// alike (design 47 H2 §781; H-A-370, H-A-400 (2)).
|
||||
func settingWords(v any, set map[string]bool) {
|
||||
m, _ := v.(map[string]any)
|
||||
config, _ := m["config"].(map[string]any)
|
||||
for key := range config {
|
||||
set["MESH_CONFIG_"+strings.NewReplacer("-", "_", ".", "_").Replace(strings.ToUpper(key))] = true
|
||||
}
|
||||
}
|
||||
|
||||
// Listed is the list's `<module> <WORD>` lines; a line starting with # and a blank line are not entries.
|
||||
func Listed(path string) (map[string]bool, error) {
|
||||
f, err := os.Open(path)
|
||||
|
||||
@@ -32,13 +32,14 @@ func module(t *testing.T, root, name, manifest string, files map[string]string)
|
||||
// not; tests, node_modules and dist are not read.
|
||||
func TestAWordReadThatNothingSetsIsFound(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
module(t, root, "reader", `{"module":"reader","build":{"artifacts":[{"name":"tools","env":{"MESH_SET":"x"}}]},`+
|
||||
module(t, root, "reader", `{"module":"reader","config":{"ask-taken-bound":{"type":"string"}},"build":{"artifacts":[{"name":"tools","env":{"MESH_SET":"x"}}]},`+
|
||||
`"resources":[{"id":"p","type":"process","environment":{"variables":{"MESH_VARIABLE":"y"}}}]}`, map[string]string{
|
||||
"cmd/a/main.go": `package main; const reach = "MESH_REACH"; var _ = os.Getenv(reach); var _ = os.Getenv("MESH_SET")`,
|
||||
"cmd/a/other.go": `package main; var _, _ = os.LookupEnv("MESH_LITERAL"); var _ = os.Getenv("MESH_MODULE")`,
|
||||
"cmd/a/write.go": `package main; var _ = []string{"MESH_WRITTEN_FOR_A_CHILD=1"}`,
|
||||
"cmd/a/a_test.go": `package main; var _ = os.Getenv("MESH_IN_A_TEST")`,
|
||||
"client.ts": `const a = process.env.MESH_TS ?? "d"; const b = env["MESH_BRACKET"]; const c = env.MESH_VARIABLE;`,
|
||||
"cmd/a/main.go": `package main; const reach = "MESH_REACH"; var _ = os.Getenv(reach); var _ = os.Getenv("MESH_SET")`,
|
||||
"cmd/a/other.go": `package main; var _, _ = os.LookupEnv("MESH_LITERAL"); var _ = os.Getenv("MESH_MODULE")`,
|
||||
"cmd/a/write.go": `package main; var _ = []string{"MESH_WRITTEN_FOR_A_CHILD=1"}`,
|
||||
"cmd/a/a_test.go": `package main; var _ = os.Getenv("MESH_IN_A_TEST")`,
|
||||
// 47 H2 §781, H-A-370: a declared setting's word, which the node-engine composes; one undeclared is set by nothing.
|
||||
"client.ts": `const a = process.env.MESH_TS ?? "d"; const b = env["MESH_BRACKET"]; const c = env.MESH_VARIABLE; const d = env.MESH_CONFIG_ASK_TAKEN_BOUND; const e = env.MESH_CONFIG_UNDECLARED;`,
|
||||
"files/env": "MESH_FROM_FILE=${config:x}\n",
|
||||
"index.ts": `const f = env.MESH_FROM_FILE;`,
|
||||
"test/x.test.ts": `env.MESH_IN_A_TEST_DIR`,
|
||||
@@ -50,16 +51,17 @@ func TestAWordReadThatNothingSetsIsFound(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
want := map[string]map[string][]string{"reader": {
|
||||
"MESH_REACH": {"cmd/a/main.go"},
|
||||
"MESH_LITERAL": {"cmd/a/other.go"},
|
||||
"MESH_TS": {"client.ts"},
|
||||
"MESH_BRACKET": {"client.ts"},
|
||||
"MESH_REACH": {"cmd/a/main.go"},
|
||||
"MESH_LITERAL": {"cmd/a/other.go"},
|
||||
"MESH_TS": {"client.ts"},
|
||||
"MESH_BRACKET": {"client.ts"},
|
||||
"MESH_CONFIG_UNDECLARED": {"client.ts"},
|
||||
}}
|
||||
if !reflect.DeepEqual(got, want) {
|
||||
t.Fatalf("found %v, want %v", got, want)
|
||||
}
|
||||
refused, stale := Judge(got, map[string]bool{"reader MESH_REACH": true, "reader MESH_GONE": true})
|
||||
if !reflect.DeepEqual(refused, []string{"reader MESH_BRACKET", "reader MESH_LITERAL", "reader MESH_TS"}) ||
|
||||
if !reflect.DeepEqual(refused, []string{"reader MESH_BRACKET", "reader MESH_CONFIG_UNDECLARED", "reader MESH_LITERAL", "reader MESH_TS"}) ||
|
||||
!reflect.DeepEqual(stale, []string{"reader MESH_GONE"}) {
|
||||
t.Errorf("refused %v, stale %v", refused, stale)
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
{
|
||||
"module": "adwaita",
|
||||
"version": "0.1",
|
||||
"class": "leaf",
|
||||
"moved": true,
|
||||
"capabilities": [
|
||||
"package-manager"
|
||||
|
||||
@@ -119,3 +119,16 @@ removed for `retention` on its seat (`release`, `remove`). It reads a reference
|
||||
and `<seat>/<module>/<artifact>@<digest>`, as one artifact, and says artifacts-gone in the form the build named it. A
|
||||
call a seat refuses stops the sweep and writes the finding `sweep-refused` in the module's findings; the next sweep no
|
||||
seat refuses clears it (`sweepseats.go`).
|
||||
|
||||
### One pending pass
|
||||
|
||||
A finished build or a changed want marks the one pending pass (design 47 H, H-A-250); a pass running when one is marked
|
||||
runs once more when it ends, however many events marked it. The builder's builds are a followed view: the watch's first view
|
||||
at start, then its updates, kept in memory (H-A-255), each build's end taken from the builder's log's build-ended entries the
|
||||
pass reads once, and only for a build the log lacks from its key's history (H-A-258); a watch asked again after an error replaces the view whole with its handover (H-A-257). A pass reads nothing
|
||||
of them on the bus, judges nothing before the first view is in, and sweeps and observes S25
|
||||
from that one judgement. A list the registry
|
||||
seats cannot answer ends the pass with the finding `artifact-list-unread`, written once; nothing asks the list again
|
||||
until the next event, and a pass that reads it clears the finding. A pass that cannot judge the builds (the view not yet taken in, or a
|
||||
done build's end unread) ends the same way with the finding `artifact-builds-unread` (H-A-260) (`holder.go`,
|
||||
`sweepseats.go`).
|
||||
|
||||
@@ -116,12 +116,16 @@ func (runtimeBus) Publish(subject string, body any) error {
|
||||
return stdio.SeatPublish(subject, body, "")
|
||||
}
|
||||
|
||||
// follow watches the builder's done keys and the wants: a build's end sweeps and
|
||||
// observes again; a want changed observes again. Event-driven: nothing reads them on a timer.
|
||||
// follow watches the builder's done keys and the wants: the builds are a followed view, the watch's first view then its
|
||||
// updates kept in memory (design 47 H, H-A-255); a build's end or a want changed marks the one pending pass (H-A-250 (1)),
|
||||
// which sweeps and observes from the views. Event-driven: nothing reads them on a timer.
|
||||
func follow(h *Holder, v *views.Reader) {
|
||||
<-stdio.Served()
|
||||
watch := func(state string, handle func(stdio.StateChange) error) {
|
||||
for {
|
||||
if state == BuildsRead {
|
||||
h.Watching() // each handover, the first or one asked again, replaces the view whole (H-A-257)
|
||||
}
|
||||
if err := stdio.State(state).Watch("", handle); err == nil {
|
||||
return
|
||||
} else {
|
||||
@@ -130,12 +134,8 @@ func follow(h *Holder, v *views.Reader) {
|
||||
time.Sleep(10 * time.Second)
|
||||
}
|
||||
}
|
||||
observe := func() {
|
||||
if err := h.Observe(context.Background()); err != nil {
|
||||
h.logf("image-missing could not be observed: %v", err)
|
||||
}
|
||||
}
|
||||
watch(BuildsRead, func(c stdio.StateChange) error {
|
||||
h.Heard(c)
|
||||
if c.Current || c.Op != stdio.OpPut {
|
||||
return nil
|
||||
}
|
||||
@@ -143,10 +143,12 @@ func follow(h *Holder, v *views.Reader) {
|
||||
State string `json:"state"`
|
||||
}
|
||||
if json.Unmarshal(c.Value, &b) == nil && b.State == "done" {
|
||||
go func() { h.AfterBuild(c.Key); observe() }()
|
||||
h.Wanted("after the build " + c.Key)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
// The watch hands every current value over before it returns: the first view is in.
|
||||
h.TakenIn()
|
||||
// The wants are the assigner's: the first view then its events (ADR 0615). What Watch hands over before it returns
|
||||
// is the first view, not a change.
|
||||
var current atomic.Bool
|
||||
@@ -154,7 +156,7 @@ func follow(h *Holder, v *views.Reader) {
|
||||
for {
|
||||
err := v.Watch(WantsRead, func(string, []byte, bool) {
|
||||
if !current.Load() {
|
||||
go observe()
|
||||
h.Wanted("a want changed")
|
||||
}
|
||||
})
|
||||
if err == nil {
|
||||
@@ -164,5 +166,5 @@ func follow(h *Holder, v *views.Reader) {
|
||||
time.Sleep(10 * time.Second)
|
||||
}
|
||||
current.Store(false)
|
||||
observe()
|
||||
h.Wanted("the first view of the wants")
|
||||
}
|
||||
|
||||
@@ -38,6 +38,10 @@ import (
|
||||
"git.novox.be/novox/mesh-sdk/go/views"
|
||||
)
|
||||
|
||||
// ErrBuildsUnread is a judgement refused because the builds cannot be judged: the followed view not yet taken in, or a
|
||||
// done key's end unread (H-A-258, H-A-260).
|
||||
var ErrBuildsUnread = errors.New("the builder's builds cannot be judged")
|
||||
|
||||
// What the holder reads and keeps.
|
||||
const (
|
||||
WantsRead = assignerModule + ".wants"
|
||||
@@ -95,8 +99,19 @@ type Holder struct {
|
||||
|
||||
sweep sync.Mutex // one sweep at a time
|
||||
mu sync.Mutex
|
||||
fmu sync.Mutex // one pass of Observe at a time
|
||||
ended map[string]time.Time // a done key's end, read once from its history
|
||||
// pmu guards the pending pass (H-A-250 (1)): running while a pass runs, wanted when an event marked it since.
|
||||
pmu sync.Mutex
|
||||
running bool
|
||||
wanted bool
|
||||
wantedBy string
|
||||
fmu sync.Mutex // one pass of Observe at a time
|
||||
ended map[string]time.Time // a done key's end, read once from its history
|
||||
// done is the followed view of the builder's builds (design 47 H, H-A-255): the done builds by key, taken in from the
|
||||
// watch's first view and its updates (Heard); taken says the first view is in. A pass judges from it, reading nothing.
|
||||
done map[string]Build
|
||||
taken bool
|
||||
// next is the handover a watch asked (again) is giving, which replaces done whole once it is in (H-A-257).
|
||||
next map[string]Build
|
||||
}
|
||||
|
||||
// Build is one done build of a module, as its key holds it, with when it ended.
|
||||
@@ -108,39 +123,81 @@ type Build struct {
|
||||
Manifest string
|
||||
}
|
||||
|
||||
// Builds is every done build the builder's keys hold, each with its end.
|
||||
// Builds is every done build of the followed view, by key, read from memory, never from the bus (design 47 H, H-A-250
|
||||
// (2), H-A-255): each with the end known for it, zero where none is known yet (withEnds gives it). Before the view's first
|
||||
// view is taken in there is none to judge from, and it says so, so no pass sweeps against an empty keep set.
|
||||
func (h *Holder) Builds() ([]Build, error) {
|
||||
keys, err := h.Bus.Keys(BuildsRead)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("the builder's builds: %w", err)
|
||||
h.mu.Lock()
|
||||
defer h.mu.Unlock()
|
||||
if !h.taken {
|
||||
return nil, fmt.Errorf("%w: they are not taken in yet, their first view has not come", ErrBuildsUnread)
|
||||
}
|
||||
var out []Build
|
||||
for _, k := range keys {
|
||||
module, fp, err := builds.ParseBuilderBuildKey(k)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
raw, found, err := h.Bus.Get(BuildsRead, k)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("the builder's build %s: %w", k, err)
|
||||
}
|
||||
if !found {
|
||||
continue
|
||||
}
|
||||
b, err := builds.DecodeBuilderBuild(raw)
|
||||
if err != nil || b.State != builds.BuildDone {
|
||||
continue
|
||||
}
|
||||
at, err := h.endOf(k)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, Build{Module: module, FP: fp, Key: k, At: at, Artifacts: b.Artifacts, Manifest: b.Manifest})
|
||||
out := make([]Build, 0, len(h.done))
|
||||
for _, b := range h.done {
|
||||
b.At = h.ended[b.Key]
|
||||
out = append(out, b)
|
||||
}
|
||||
sort.Slice(out, func(a, b int) bool { return out[a].Key < out[b].Key })
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// endOf is when a done key was last written done, read once from its history: a done key is not written again.
|
||||
// Heard takes one change of the builder's builds into the followed view: a done value is kept, anything else drops the
|
||||
// key. Its end is not read here: withEnds takes it from the builder's log at the next pass (H-A-258); a key written done
|
||||
// anew forgets the end it had. While a watch's handover is gathered (Watching), every change goes into it, current or
|
||||
// not, so nothing heard before TakenIn is lost by the swap (H-A-257).
|
||||
func (h *Holder) Heard(c stdio.StateChange) {
|
||||
module, fp, err := builds.ParseBuilderBuildKey(c.Key)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
var b builds.BuilderBuild
|
||||
if c.Op == stdio.OpPut {
|
||||
b, err = builds.DecodeBuilderBuild(c.Value)
|
||||
}
|
||||
h.mu.Lock()
|
||||
defer h.mu.Unlock()
|
||||
if !c.Current {
|
||||
delete(h.ended, c.Key) // written anew or gone: an end it had is no longer its
|
||||
}
|
||||
if h.next == nil && h.done == nil {
|
||||
h.done = map[string]Build{}
|
||||
}
|
||||
into := h.done
|
||||
if h.next != nil {
|
||||
into = h.next
|
||||
}
|
||||
if c.Op != stdio.OpPut || err != nil || b.State != builds.BuildDone {
|
||||
delete(into, c.Key)
|
||||
return
|
||||
}
|
||||
into[c.Key] = Build{Module: module, FP: fp, Key: c.Key, Artifacts: b.Artifacts, Manifest: b.Manifest}
|
||||
}
|
||||
|
||||
// Watching says a watch of the builds is asked, first or again after an error: its handover of current values is
|
||||
// gathered apart and replaces the view whole at TakenIn, so a key deleted while no watch ran leaves it (H-A-257).
|
||||
func (h *Holder) Watching() {
|
||||
h.mu.Lock()
|
||||
h.next = map[string]Build{}
|
||||
h.mu.Unlock()
|
||||
}
|
||||
|
||||
// TakenIn says the watch's handover is in: every current value was Heard. It replaces the view whole, and forgets the
|
||||
// end of every key it no longer holds.
|
||||
func (h *Holder) TakenIn() {
|
||||
h.mu.Lock()
|
||||
defer h.mu.Unlock()
|
||||
if h.next != nil {
|
||||
h.done, h.next = h.next, nil
|
||||
for k := range h.ended {
|
||||
if _, kept := h.done[k]; !kept {
|
||||
delete(h.ended, k)
|
||||
}
|
||||
}
|
||||
}
|
||||
h.taken = true
|
||||
}
|
||||
|
||||
// endOf is when a done key was last written done, read from its history and kept: for a build the builder's log lacks.
|
||||
func (h *Holder) endOf(key string) (time.Time, error) {
|
||||
h.mu.Lock()
|
||||
at, known := h.ended[key]
|
||||
@@ -320,6 +377,8 @@ type judged struct {
|
||||
mirrors map[string]*Ref
|
||||
// mirrorsHeld says why no copy is eligible now, when one is not: a kept build whose bases the log does not name.
|
||||
mirrorsHeld string
|
||||
// unread is why the registry seats' list could not be read, nil when it was (or none is read).
|
||||
unread error
|
||||
}
|
||||
|
||||
func (h *Holder) judge(ctx context.Context) (*judged, error) {
|
||||
@@ -327,6 +386,15 @@ func (h *Holder) judge(ctx context.Context) (*judged, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// The builder's log, read once: what each build stood on, and each build's end (H-A-258).
|
||||
against, ends, logErr := h.logged()
|
||||
if logErr != nil {
|
||||
h.logf("the builder's log could not be read, so every copy is kept as unrecorded and each end is read from its history: %v", logErr)
|
||||
against, ends = nil, nil
|
||||
}
|
||||
if builds, err = h.withEnds(builds, ends); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
wants, err := h.Wants()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -335,15 +403,10 @@ func (h *Holder) judge(ctx context.Context) (*judged, error) {
|
||||
if h.Listed != nil {
|
||||
if j.view, err = h.Listed(ctx); err != nil {
|
||||
h.logf("the store's listing could not be read, so nothing is said gone: %v", err)
|
||||
j.view = nil
|
||||
j.view, j.unread = nil, err
|
||||
}
|
||||
}
|
||||
if j.view != nil {
|
||||
against, err := h.against()
|
||||
if err != nil {
|
||||
h.logf("the builder's log could not be read, so every copy is kept as unrecorded: %v", err)
|
||||
against = nil
|
||||
}
|
||||
j.mirrors, j.mirrorsHeld = Mirrors(j.view, builds, keptBuilds(builds, wants), against)
|
||||
for r, m := range j.mirrors {
|
||||
if len(m.Builds) > 0 {
|
||||
@@ -354,13 +417,47 @@ func (h *Holder) judge(ctx context.Context) (*judged, error) {
|
||||
return j, nil
|
||||
}
|
||||
|
||||
// against is what each build the builder's log says ended stood on, by build key: the bases' references.
|
||||
func (h *Holder) against() (map[string][]string, error) {
|
||||
// withEnds gives each build its end: the one known, else the builder's log's build-ended entry, else its key's history,
|
||||
// each kept once read (H-A-258). A done key whose end can be read from neither stays unread, and then nothing is judged:
|
||||
// never from a view missing a build.
|
||||
func (h *Holder) withEnds(built []Build, ends map[string]time.Time) ([]Build, error) {
|
||||
var unread []string
|
||||
var why error
|
||||
for i := range built {
|
||||
if !built[i].At.IsZero() {
|
||||
continue
|
||||
}
|
||||
if at, logged := ends[built[i].Key]; logged {
|
||||
h.mu.Lock()
|
||||
if h.ended == nil {
|
||||
h.ended = map[string]time.Time{}
|
||||
}
|
||||
h.ended[built[i].Key] = at
|
||||
h.mu.Unlock()
|
||||
built[i].At = at
|
||||
continue
|
||||
}
|
||||
at, err := h.endOf(built[i].Key)
|
||||
if err != nil {
|
||||
unread, why = append(unread, built[i].Key), err
|
||||
continue
|
||||
}
|
||||
built[i].At = at
|
||||
}
|
||||
if len(unread) > 0 {
|
||||
return nil, fmt.Errorf("%w: the end of %d done builds could not be read (%s first), so nothing is judged: %v", ErrBuildsUnread, len(unread), unread[0], why)
|
||||
}
|
||||
return built, nil
|
||||
}
|
||||
|
||||
// logged is what each build the builder's log says ended stood on, by build key (the bases' references), and when it
|
||||
// ended: one read of the log.
|
||||
func (h *Holder) logged() (map[string][]string, map[string]time.Time, error) {
|
||||
entries, err := h.Bus.LogSince(builds.BuildLogRead, h.now().Add(-builds.BuilderLogDays*24*time.Hour))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, nil, err
|
||||
}
|
||||
out := map[string][]string{}
|
||||
out, ends := map[string][]string{}, map[string]time.Time{}
|
||||
for _, raw := range entries {
|
||||
e, err := builds.DecodeBuildLogEntry(raw.Value)
|
||||
if err != nil || e.Event != builds.BuildEndedEvent {
|
||||
@@ -371,8 +468,11 @@ func (h *Holder) against() (map[string][]string, error) {
|
||||
continue
|
||||
}
|
||||
out[key] = e.Against
|
||||
if e.At.After(ends[key]) {
|
||||
ends[key] = e.At
|
||||
}
|
||||
}
|
||||
return out, nil
|
||||
return out, ends, nil
|
||||
}
|
||||
|
||||
// Mirrors judges each copy in the store's mirror repositories (upstream/<host>/<path>, <module>/on-<argument>; ADR
|
||||
@@ -514,6 +614,11 @@ func (h *Holder) Sweep(ctx context.Context, s Sweeping) (*CollectAnswer, error)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return h.sweepFrom(ctx, j, s)
|
||||
}
|
||||
|
||||
// sweepFrom is a sweep from a judgement already read; the caller holds h.sweep.
|
||||
func (h *Holder) sweepFrom(ctx context.Context, j *judged, s Sweeping) (*CollectAnswer, error) {
|
||||
a := &CollectAnswer{DryRun: s.DryRun, WouldLetGo: []string{}, LetGo: []string{}}
|
||||
var keptArchives, eligible []string
|
||||
for _, r := range j.sorted() {
|
||||
@@ -658,15 +763,73 @@ func namedBy(b Build, r string) string {
|
||||
return r
|
||||
}
|
||||
|
||||
// AfterBuild is the sweep at a build's end.
|
||||
func (h *Holder) AfterBuild(key string) {
|
||||
a, err := h.Sweep(context.Background(), Sweeping{Most: MostPerSweep, By: "after the build " + key})
|
||||
// Wanted marks the pass wanted (design 47 H, H-A-250 (1)): a finished build or a want change asks it, by naming what
|
||||
// asked. With no pass running one starts; a pass running when it is marked runs once more when it ends, however many
|
||||
// events marked it, never once per event.
|
||||
func (h *Holder) Wanted(by string) {
|
||||
if h.mark(by) {
|
||||
go h.passes(by)
|
||||
}
|
||||
}
|
||||
|
||||
// mark sets the pending mark, and says whether no pass ran, so the caller starts one.
|
||||
func (h *Holder) mark(by string) bool {
|
||||
h.pmu.Lock()
|
||||
defer h.pmu.Unlock()
|
||||
if h.running {
|
||||
h.wanted, h.wantedBy = true, by
|
||||
return false
|
||||
}
|
||||
h.running = true
|
||||
return true
|
||||
}
|
||||
|
||||
// passes runs the pass, then once more for as long as an event marked it during the one that ran.
|
||||
func (h *Holder) passes(by string) {
|
||||
for {
|
||||
h.Pass(context.Background(), by)
|
||||
h.pmu.Lock()
|
||||
if !h.wanted {
|
||||
h.running = false
|
||||
h.pmu.Unlock()
|
||||
return
|
||||
}
|
||||
h.wanted, by = false, h.wantedBy
|
||||
h.pmu.Unlock()
|
||||
}
|
||||
}
|
||||
|
||||
// Pass is one pass of the seat: the keep set judged from one read of the builds, the wants and the store's list, then
|
||||
// the sweep and S25's observation from that one judgement. A list the registry seats cannot answer ends the pass with
|
||||
// the finding artifact-list-unread, asked again only at the next event (H-A-250 (3)); a list read again clears it.
|
||||
func (h *Holder) Pass(ctx context.Context, by string) {
|
||||
h.sweep.Lock()
|
||||
j, err := h.judge(ctx)
|
||||
if err != nil {
|
||||
h.logf("the sweep after %s: %v", key, err)
|
||||
h.sweep.Unlock()
|
||||
h.logf("the pass %s: %v", by, err)
|
||||
if errors.Is(err, ErrBuildsUnread) {
|
||||
h.unreadFinding(BuildsUnread, err) // H-A-260
|
||||
}
|
||||
return
|
||||
}
|
||||
if len(a.LetGo) > 0 || a.Stopped != "" || a.Missing > 0 {
|
||||
h.logf("the sweep after %s let go of %d (%d left, %d missing): %s", key, len(a.LetGo), a.Left, a.Missing, a.Stopped)
|
||||
h.readAgain(BuildsUnread)
|
||||
if j.unread != nil {
|
||||
h.sweep.Unlock()
|
||||
h.unreadFinding(ListUnread, j.unread)
|
||||
return
|
||||
}
|
||||
h.readAgain(ListUnread)
|
||||
a, err := h.sweepFrom(ctx, j, Sweeping{Most: MostPerSweep, By: by})
|
||||
h.sweep.Unlock()
|
||||
switch {
|
||||
case err != nil:
|
||||
h.logf("the sweep %s: %v", by, err)
|
||||
case len(a.LetGo) > 0 || a.Stopped != "" || a.Missing > 0:
|
||||
h.logf("the sweep %s let go of %d (%d left, %d missing): %s", by, len(a.LetGo), a.Left, a.Missing, a.Stopped)
|
||||
}
|
||||
if err := h.observeFrom(ctx, j); err != nil {
|
||||
h.logf("image-missing could not be observed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -857,10 +1020,16 @@ func inStoreAt(image, address string) string {
|
||||
// want's machine, written once and cleared once it no longer holds. Nothing is said without a listing. What stands is what
|
||||
// the held writer holds (design 47 G A-32, A-38), so a restart neither drops nor rewrites it.
|
||||
func (h *Holder) Observe(ctx context.Context) error {
|
||||
now, err := h.observed(ctx)
|
||||
j, err := h.judge(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return h.observeFrom(ctx, j)
|
||||
}
|
||||
|
||||
// observeFrom is Observe from a judgement already read.
|
||||
func (h *Holder) observeFrom(ctx context.Context, j *judged) error {
|
||||
now := h.observed(j)
|
||||
if now == nil {
|
||||
return nil
|
||||
}
|
||||
@@ -899,13 +1068,9 @@ type Findings interface {
|
||||
}
|
||||
|
||||
// observed is S25's findings now by key, nil without a listing.
|
||||
func (h *Holder) observed(ctx context.Context) (map[string]meshconditions.Finding, error) {
|
||||
j, err := h.judge(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
func (h *Holder) observed(j *judged) map[string]meshconditions.Finding {
|
||||
if j.view == nil {
|
||||
return nil, nil
|
||||
return nil
|
||||
}
|
||||
address := ""
|
||||
if d, err := h.Door(); err == nil {
|
||||
@@ -944,7 +1109,7 @@ func (h *Holder) observed(ctx context.Context) (map[string]meshconditions.Findin
|
||||
break
|
||||
}
|
||||
}
|
||||
return now, nil
|
||||
return now
|
||||
}
|
||||
|
||||
// imageMissing is S25's finding, in the controller's words (cmd/mesh-controller/image_missing.go watchMissingImages); its id
|
||||
|
||||
@@ -11,6 +11,7 @@ package main
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
@@ -200,6 +201,15 @@ func TestTheSeatSweepsAtABuildsEndOnARealBusWithNoLease(t *testing.T) {
|
||||
}
|
||||
h := &Holder{Bus: findingsAt{realBus: bus}, Findings: findingsThrough{findingsAt{realBus: bus}}, Door: mem.Door, Listed: mem.Listed, Seats: ask, Now: time.Now, Logf: t.Logf}
|
||||
h.Acts = meshhandactsclient.Recorder{Ask: ask}
|
||||
// The followed view of the builds: the six on the bus handed over as the watch's first view does.
|
||||
h.Watching()
|
||||
if keys, err := bus.Keys(BuildsRead); err == nil {
|
||||
for _, k := range keys {
|
||||
raw, _, _ := bus.Get(BuildsRead, k)
|
||||
h.Heard(stdio.StateChange{State: BuildsRead, Key: k, Op: stdio.OpPut, Value: raw, Current: true})
|
||||
}
|
||||
}
|
||||
h.TakenIn()
|
||||
|
||||
// A build's end heard from the builds bucket, as follow hands it over.
|
||||
kv, _ := bus.kv(BuildsRead)
|
||||
@@ -215,7 +225,8 @@ func TestTheSeatSweepsAtABuildsEndOnARealBusWithNoLease(t *testing.T) {
|
||||
}
|
||||
select {
|
||||
case e := <-w.Updates():
|
||||
h.AfterBuild(e.Key())
|
||||
h.Heard(stdio.StateChange{State: BuildsRead, Key: e.Key(), Op: stdio.OpPut, Value: e.Value()})
|
||||
h.Pass(ctx, "after the build "+e.Key())
|
||||
case <-ctx.Done():
|
||||
t.Fatal("the build's end was not heard")
|
||||
}
|
||||
@@ -234,26 +245,82 @@ func TestTheSeatSweepsAtABuildsEndOnARealBusWithNoLease(t *testing.T) {
|
||||
}
|
||||
time.Sleep(20 * time.Millisecond)
|
||||
}
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
if len(acts) != 3 || !strings.Contains(acts[0], archiveRef("letta", 2)) || !strings.Contains(acts[1], imageRef("letta", 2)) ||
|
||||
!strings.Contains(acts[2], mirror(1)) {
|
||||
t.Fatalf("the hand acts recorded are %v; the store was asked %v", acts, door.asked)
|
||||
}
|
||||
if len(gone) != 1 || !strings.Contains(gone[0], `"fingerprint":"`+fp(2)+`"`) {
|
||||
t.Fatalf("artifacts-gone said %v", gone)
|
||||
}
|
||||
func() {
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
if len(acts) != 3 || !strings.Contains(acts[0], archiveRef("letta", 2)) || !strings.Contains(acts[1], imageRef("letta", 2)) ||
|
||||
!strings.Contains(acts[2], mirror(1)) {
|
||||
t.Fatalf("the hand acts recorded are %v; the store was asked %v", acts, door.asked)
|
||||
}
|
||||
if len(gone) != 1 || !strings.Contains(gone[0], `"fingerprint":"`+fp(2)+`"`) {
|
||||
t.Fatalf("artifacts-gone said %v", gone)
|
||||
}
|
||||
|
||||
// S25 through the real findings bucket: letta's wanted image missing from the store is a finding there.
|
||||
view, _ := mem.Listed(ctx)
|
||||
repo, digest, _ := splitReference(imageRef("letta", 1))
|
||||
delete(view.L.Repos[repo].Revisions, digest)
|
||||
if err := h.Observe(ctx); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if keys, _ := bus.Keys("artifact-store-tools." + FindingsState); len(keys) != 1 || keys[0] != "module.letta.ace.image-missing" {
|
||||
t.Fatalf("the findings are %v", keys)
|
||||
}
|
||||
// S25 through the real findings bucket: letta's wanted image missing from the store is a finding there.
|
||||
view, _ := mem.Listed(ctx)
|
||||
repo, digest, _ := splitReference(imageRef("letta", 1))
|
||||
delete(view.L.Repos[repo].Revisions, digest)
|
||||
if err := h.Observe(ctx); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if keys, _ := bus.Keys("artifact-store-tools." + FindingsState); len(keys) != 1 || keys[0] != "module.letta.ace.image-missing" {
|
||||
t.Fatalf("the findings are %v", keys)
|
||||
}
|
||||
}()
|
||||
|
||||
// Design 47 H2 §813 (7): a pass reads oci-artifact-registry's list page by page, each at most 768 KB, over 20,000
|
||||
// artifacts, so its view holds every one.
|
||||
t.Run("a pass reads 20,000 artifacts page by page", func(t *testing.T) {
|
||||
const many, pageBytes = 20000, 768 << 10
|
||||
repos := make([]map[string]any, many)
|
||||
for i := range repos {
|
||||
repos[i] = map[string]any{"name": fmt.Sprintf("m%05d/bundle", i), "manifests": []map[string]any{{
|
||||
"digest": fmt.Sprintf("sha256:%064x", i), "artifact": fmt.Sprintf("sha256:%064x", many+i), "size": 100}}}
|
||||
}
|
||||
pages := 0
|
||||
sub, err := nc.Subscribe("mesh.seat.oci-artifact-registry.tool.list.paged", func(m *nats.Msg) {
|
||||
var in struct{ Next string }
|
||||
_ = json.Unmarshal(m.Data, &in)
|
||||
page, size, next := []map[string]any{}, 0, ""
|
||||
for _, r := range repos {
|
||||
if r["name"].(string) <= in.Next {
|
||||
continue
|
||||
}
|
||||
raw, _ := json.Marshal(r)
|
||||
if size+len(raw)+1 > pageBytes-1024 { // its comma, and room for the answer's other fields
|
||||
next = page[len(page)-1]["name"].(string)
|
||||
break
|
||||
}
|
||||
size += len(raw) + 1
|
||||
page = append(page, r)
|
||||
}
|
||||
pages++
|
||||
out, _ := json.Marshal(map[string]any{"repositories": page, "next": next, "capped": next != "", "total": many})
|
||||
_ = m.Respond(out)
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer func() { _ = sub.Unsubscribe() }()
|
||||
store := &Store{Ask: func(key string, body any) (json.RawMessage, error) {
|
||||
if key != "seat:oci-artifact-registry.list" {
|
||||
return ask(key, body)
|
||||
}
|
||||
raw, _ := json.Marshal(body)
|
||||
m, err := nc.Request("mesh.seat.oci-artifact-registry.tool.list.paged", raw, 5*time.Second)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(m.Data) > pageBytes {
|
||||
t.Errorf("a page of %d bytes", len(m.Data))
|
||||
}
|
||||
return m.Data, nil
|
||||
}}
|
||||
got, err := store.ListOf("oci-artifact-registry", "")
|
||||
if err != nil || len(got) != many || got[many-1].Name != "m19999/bundle" || pages < 2 {
|
||||
t.Fatalf("read %d repositories in %d pages: %v", len(got), pages, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// findingsAt reads and writes the holder's own findings in its bucket, artifact-store-tools_findings.
|
||||
|
||||
@@ -34,6 +34,17 @@ type memBus struct {
|
||||
history map[string][]stdio.StateRevision
|
||||
log []stdio.LogEntry
|
||||
published []string
|
||||
// historyErr fails the history read of a key.
|
||||
historyErr map[string]error
|
||||
// calls counts each read by "<verb> <state>": a pass reads nothing of the builds, judged from the followed view.
|
||||
calls map[string]int
|
||||
}
|
||||
|
||||
func (b *memBus) count(verb, state string) {
|
||||
if b.calls == nil {
|
||||
b.calls = map[string]int{}
|
||||
}
|
||||
b.calls[verb+" "+state]++
|
||||
}
|
||||
|
||||
func (b *memBus) LogSince(read string, at time.Time) ([]stdio.LogEntry, error) {
|
||||
@@ -49,6 +60,7 @@ func (b *memBus) LogSince(read string, at time.Time) ([]stdio.LogEntry, error) {
|
||||
func (b *memBus) Keys(state string) ([]string, error) {
|
||||
b.mu.Lock()
|
||||
defer b.mu.Unlock()
|
||||
b.count("keys", state)
|
||||
var out []string
|
||||
for k := range b.states[state] {
|
||||
out = append(out, k)
|
||||
@@ -60,11 +72,18 @@ func (b *memBus) Keys(state string) ([]string, error) {
|
||||
func (b *memBus) Get(state, key string) ([]byte, bool, error) {
|
||||
b.mu.Lock()
|
||||
defer b.mu.Unlock()
|
||||
b.count("get", state)
|
||||
v, ok := b.states[state][key]
|
||||
return v, ok, nil
|
||||
}
|
||||
|
||||
func (b *memBus) History(state, key string) ([]stdio.StateRevision, error) {
|
||||
b.mu.Lock()
|
||||
defer b.mu.Unlock()
|
||||
b.count("history", state)
|
||||
if err := b.historyErr[key]; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return b.history[state+" "+key], nil
|
||||
}
|
||||
|
||||
@@ -229,7 +248,7 @@ func aMesh(t *testing.T) (*Holder, *memBus, *memDoor, *[]string) {
|
||||
want, _ := json.Marshal(meshwants.WantsByWantNodeModuleValue{V: 1, Fp: fp(1), Self: true})
|
||||
_ = b.Put(WantsRead, "want.ace.letta", json.RawMessage(want))
|
||||
var acts []string
|
||||
h := &Holder{Bus: b, Findings: findingsThrough{b}, Door: func() (Door, error) { return door, nil }, Seats: door.seat,
|
||||
h := &Holder{Logf: t.Logf, Bus: b, Findings: findingsThrough{b}, Door: func() (Door, error) { return door, nil }, Seats: door.seat,
|
||||
Listed: func(context.Context) (*View, error) { return &View{L: listing}, nil },
|
||||
Now: func() time.Time { return day.Add(48 * time.Hour) },
|
||||
Acts: meshhandactsclient.Recorder{Ask: func(key string, body any) (json.RawMessage, error) {
|
||||
@@ -237,9 +256,49 @@ func aMesh(t *testing.T) (*Holder, *memBus, *memDoor, *[]string) {
|
||||
acts = append(acts, key+" "+string(raw))
|
||||
return json.RawMessage(`{}`), nil
|
||||
}}}
|
||||
takeIn(h, b)
|
||||
return h, b, door, &acts
|
||||
}
|
||||
|
||||
// takeIn hands the builds over as the watch's first view does, then forgets the reads that took: a pass's are counted.
|
||||
func takeIn(h *Holder, b *memBus) {
|
||||
handOver(h, b)
|
||||
h.TakenIn()
|
||||
b.calls = nil
|
||||
}
|
||||
|
||||
// handOver is a watch's handover of every current value, not yet taken in.
|
||||
func handOver(h *Holder, b *memBus) {
|
||||
h.Watching()
|
||||
keys, _ := b.Keys(BuildsRead)
|
||||
for _, k := range keys {
|
||||
raw, _, _ := b.Get(BuildsRead, k)
|
||||
h.Heard(stdio.StateChange{State: BuildsRead, Key: k, Op: stdio.OpPut, Value: raw, Current: true})
|
||||
}
|
||||
}
|
||||
|
||||
// keyOf is letta's n-th build's key.
|
||||
func keyOf(n int) string { k, _ := builds.BuilderBuildKey("letta", fp(n)); return k }
|
||||
|
||||
// viewKeys are the keys the followed view holds.
|
||||
func viewKeys(t *testing.T, h *Holder) []string {
|
||||
t.Helper()
|
||||
h.mu.Lock()
|
||||
defer h.mu.Unlock()
|
||||
var out []string
|
||||
for k := range h.done {
|
||||
out = append(out, k)
|
||||
}
|
||||
sort.Strings(out)
|
||||
return out
|
||||
}
|
||||
|
||||
// putBuild writes a build's key and hands it over as the watch's update does.
|
||||
func putBuild(h *Holder, b *memBus, key string, raw []byte) {
|
||||
_ = b.Put(BuildsRead, key, json.RawMessage(raw))
|
||||
h.Heard(stdio.StateChange{State: BuildsRead, Key: key, Op: stdio.OpPut, Value: raw})
|
||||
}
|
||||
|
||||
// ADR 0571 clause 2, 1126/A4/AC2: the keep set from the store's listing, the wants and the five newest builds; with no
|
||||
// lease key, a build's end and a confirmed collect let go of the eligible within the bound, leave an index for a
|
||||
// confirmed collect, record each deletion as a hand act and say artifacts-gone.
|
||||
@@ -312,7 +371,7 @@ func TestTheSeatKeepsTheFiveNewestAndTheWantedAndSweepsWithNoLease(t *testing.T)
|
||||
raw, _ := builds.EncodeBuilderBuild(builds.BuilderBuild{State: builds.BuildDone, Manifest: strings.Repeat("d", 64), Artifacts: []builds.BuiltArtifact{
|
||||
{Name: "image", Kind: "image", Digest: digestOf(imageRef("letta", n)), Reference: artifactref.SeatReference(reg.Seat, "letta", "image", digestOf(imageRef("letta", n)))},
|
||||
{Name: "files", Kind: "archive", Digest: digestOf(archiveRef("letta", n)), Reference: artifactref.SeatReference(ociseat.Seat, "letta", "files", digestOf(archiveRef("letta", n)))}}})
|
||||
_ = b.Put(BuildsRead, key, json.RawMessage(raw))
|
||||
putBuild(h, b, key, raw)
|
||||
}
|
||||
}
|
||||
if c.refuse != "" {
|
||||
@@ -323,7 +382,7 @@ func TestTheSeatKeepsTheFiveNewestAndTheWantedAndSweepsWithNoLease(t *testing.T)
|
||||
}
|
||||
switch c.how {
|
||||
case "build":
|
||||
h.AfterBuild("letta.x")
|
||||
h.Pass(context.Background(), "after the build letta.x")
|
||||
var gone []string
|
||||
for _, x := range door.asked {
|
||||
if r, ok := strings.CutPrefix(x, "let go "); ok {
|
||||
@@ -420,6 +479,7 @@ func TestAWantedImageTheStoreLacksIsAFindingAndImagesNamesWantsAndHaves(t *testi
|
||||
view.L.Repos[repo].Revisions[digest] = true
|
||||
// A restart: a holder made afresh holds no finding of its own, and clears what the writer holds (design 47 G A-32, A-38).
|
||||
h = &Holder{Bus: h.Bus, Findings: h.Findings, Door: h.Door, Listed: h.Listed, Seats: h.Seats, Now: h.Now, Acts: h.Acts, Logf: h.Logf}
|
||||
takeIn(h, b)
|
||||
if err := h.Observe(context.Background()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -482,3 +542,154 @@ func (f findingsThrough) Held() []meshconditions.Finding {
|
||||
func (f findingsThrough) Clear(_ context.Context, scope, id, kind, _ string) error {
|
||||
return f.bus.Delete(FindingsState, views.FindingKey(scope, id, kind))
|
||||
}
|
||||
|
||||
// writesCounted counts the findings written through the test's writer.
|
||||
type writesCounted struct {
|
||||
findingsThrough
|
||||
writes *int
|
||||
}
|
||||
|
||||
func (w writesCounted) Write(ctx context.Context, f meshconditions.Finding) error {
|
||||
*w.writes++
|
||||
return w.findingsThrough.Write(ctx, f)
|
||||
}
|
||||
|
||||
// Design 47 H, H-A-250: the starts fold into one pending pass, a pass reads the builds in one page, and a list the
|
||||
// registry seats cannot answer ends the pass with artifact-list-unread, written once and asked again only at an event.
|
||||
func TestOnePendingPassReadsTheBuildsOnceAndEndsOnAnUnreadList(t *testing.T) {
|
||||
t.Run("three events during a pass run exactly one more", func(t *testing.T) {
|
||||
h, _, _, _ := aMesh(t)
|
||||
listed, inner := 0, h.Listed
|
||||
h.Listed = func(ctx context.Context) (*View, error) {
|
||||
listed++
|
||||
if listed == 1 {
|
||||
for _, by := range []string{"after the build a", "after the build b", "a want changed"} {
|
||||
h.Wanted(by) // a pass runs: each only marks it
|
||||
}
|
||||
}
|
||||
return inner(ctx)
|
||||
}
|
||||
if !h.mark("after the build letta.x") {
|
||||
t.Fatal("no pass ran, and the mark did not start one")
|
||||
}
|
||||
h.passes("after the build letta.x")
|
||||
if listed != 2 {
|
||||
t.Fatalf("three events during a pass ran %d passes, want 2", listed)
|
||||
}
|
||||
if h.running || h.wanted {
|
||||
t.Fatalf("after the passes running is %v and wanted %v", h.running, h.wanted)
|
||||
}
|
||||
})
|
||||
t.Run("a pass reads nothing of the builds and their ends come from the log", func(t *testing.T) {
|
||||
h, b, door, _ := aMesh(t)
|
||||
handOver(h, b) // a fresh take-in, counted: no history read for a build the log holds (H-A-258)
|
||||
h.TakenIn()
|
||||
h.Pass(context.Background(), "after the build letta.x")
|
||||
if n := b.calls["keys "+BuildsRead] - 1 + b.calls["get "+BuildsRead] - 7 + b.calls["history "+BuildsRead]; n != 0 {
|
||||
t.Fatalf("a take-in and a pass read the builds by %v", b.calls)
|
||||
}
|
||||
if !slices.Contains(door.asked, "let go "+imageRef("letta", 2)) {
|
||||
t.Fatalf("the pass swept nothing: %v", door.asked)
|
||||
}
|
||||
// Only a build the log lacks is read from its history, once.
|
||||
h, b, _, _ = aMesh(t)
|
||||
b.log = append(b.log[:2:2], b.log[3:]...) // letta's 3rd
|
||||
h.Pass(context.Background(), "a")
|
||||
h.Pass(context.Background(), "b")
|
||||
if b.calls["history "+BuildsRead] != 1 {
|
||||
t.Fatalf("two passes read the builds' history %d times, want once for the build the log lacks", b.calls["history "+BuildsRead])
|
||||
}
|
||||
})
|
||||
t.Run("no pass judges before the first view, and says so", func(t *testing.T) {
|
||||
h, b, door, _ := aMesh(t)
|
||||
h.taken = false
|
||||
h.Pass(context.Background(), "after the build letta.x")
|
||||
_, found, _ := b.Get(FindingsState, views.FindingKey("module", Module, BuildsUnread))
|
||||
if len(door.asked) != 0 || !found {
|
||||
t.Fatalf("a pass before the builds were taken in asked %v; artifact-builds-unread is written: %v", door.asked, found)
|
||||
}
|
||||
})
|
||||
t.Run("an unread end holds every pass until it is read", func(t *testing.T) {
|
||||
h, b, door, _ := aMesh(t)
|
||||
writes := 0
|
||||
h.Findings = writesCounted{findingsThrough{b}, &writes}
|
||||
b.log = append(b.log[:2:2], b.log[3:]...) // letta's 3rd: the log lacks it, and its history fails
|
||||
b.historyErr = map[string]error{keyOf(3): errors.New("no responders")}
|
||||
h.Pass(context.Background(), "a")
|
||||
h.Pass(context.Background(), "b")
|
||||
key := views.FindingKey("module", Module, BuildsUnread)
|
||||
if _, found, _ := b.Get(FindingsState, key); !found || writes != 1 || len(door.asked) != 0 {
|
||||
t.Fatalf("with an end unread the passes asked %v; the finding is written %v, %d times", door.asked, found, writes)
|
||||
}
|
||||
b.historyErr = nil
|
||||
h.Pass(context.Background(), "c")
|
||||
if _, found, _ := b.Get(FindingsState, key); found || !slices.Contains(door.asked, "let go "+imageRef("letta", 2)) {
|
||||
t.Fatalf("once the end was read the pass asked %v and left the finding: %v", door.asked, found)
|
||||
}
|
||||
})
|
||||
t.Run("a change heard during the handover is in the view it gives", func(t *testing.T) {
|
||||
h, b, _, _ := aMesh(t)
|
||||
handOver(h, b)
|
||||
raw, _, _ := b.Get(BuildsRead, keyOf(1))
|
||||
h.Heard(stdio.StateChange{State: BuildsRead, Key: keyOf(8), Op: stdio.OpPut, Value: raw}) // after the handover, before TakenIn
|
||||
h.Heard(stdio.StateChange{State: BuildsRead, Key: keyOf(3), Op: stdio.OpDelete})
|
||||
h.TakenIn()
|
||||
got := viewKeys(t, h)
|
||||
if !slices.Contains(got, keyOf(8)) || slices.Contains(got, keyOf(3)) {
|
||||
t.Fatalf("the view after the handover holds %v", got)
|
||||
}
|
||||
})
|
||||
t.Run("a key deleted or no longer done leaves the view; one written done again takes its new end", func(t *testing.T) {
|
||||
h, b, _, _ := aMesh(t)
|
||||
h.Pass(context.Background(), "a") // every end known
|
||||
building, _ := builds.EncodeBuilderBuild(builds.BuilderBuild{State: "building"})
|
||||
h.Heard(stdio.StateChange{State: BuildsRead, Key: keyOf(3), Op: stdio.OpDelete})
|
||||
h.Heard(stdio.StateChange{State: BuildsRead, Key: keyOf(4), Op: stdio.OpPut, Value: building})
|
||||
if got := viewKeys(t, h); slices.Contains(got, keyOf(3)) || slices.Contains(got, keyOf(4)) {
|
||||
t.Fatalf("the view holds %v", got)
|
||||
}
|
||||
later := b.log[len(b.log)-1].At.Add(time.Hour)
|
||||
e := builds.BuildLogEntry{Event: builds.BuildEndedEvent, Module: "letta", FP: fp(5), Repository: "novox/x", Commit: "c", Node: "ace", At: later, Outcome: "done"}
|
||||
entry, _ := builds.EncodeBuildLogEntry(e)
|
||||
b.log = append(b.log, stdio.LogEntry{Sequence: 99, Key: "letta", Value: entry, At: later})
|
||||
raw, _, _ := b.Get(BuildsRead, keyOf(5))
|
||||
h.Heard(stdio.StateChange{State: BuildsRead, Key: keyOf(5), Op: stdio.OpPut, Value: raw})
|
||||
h.Pass(context.Background(), "b")
|
||||
if got, _ := h.Builds(); !slices.ContainsFunc(got, func(x Build) bool { return x.Key == keyOf(5) && x.At.Equal(later) }) {
|
||||
t.Fatalf("letta's 5th written done again does not end at %v: %+v", later, got)
|
||||
}
|
||||
})
|
||||
t.Run("a watch asked again replaces the view whole", func(t *testing.T) {
|
||||
h, b, _, _ := aMesh(t)
|
||||
gone, _ := builds.BuilderBuildKey("letta", fp(3))
|
||||
_ = b.Delete(BuildsRead, gone) // deleted while no watch ran: no change of it is heard
|
||||
takeIn(h, b)
|
||||
got, err := h.Builds()
|
||||
if err != nil || len(got) != 6 || slices.ContainsFunc(got, func(x Build) bool { return x.Key == gone }) {
|
||||
t.Fatalf("after the watch was asked again the view holds %d builds (%v), %s among them", len(got), err, gone)
|
||||
}
|
||||
})
|
||||
t.Run("a list unread ends the pass with its finding once", func(t *testing.T) {
|
||||
h, b, door, _ := aMesh(t)
|
||||
writes, listed, inner := 0, 0, h.Listed
|
||||
h.Findings = writesCounted{findingsThrough{b}, &writes}
|
||||
h.Listed = func(context.Context) (*View, error) {
|
||||
listed++
|
||||
return nil, errors.New("the answer exceeds the bus's one megabyte")
|
||||
}
|
||||
h.Pass(context.Background(), "after the build a")
|
||||
h.Pass(context.Background(), "a want changed")
|
||||
key := views.FindingKey("module", Module, ListUnread)
|
||||
if raw, found, _ := b.Get(FindingsState, key); !found || writes != 1 || !strings.Contains(string(raw), "one megabyte") {
|
||||
t.Fatalf("the finding is %s (found %v), written %d times", raw, found, writes)
|
||||
}
|
||||
if listed != 2 || len(door.asked) != 0 || len(b.published) != 0 {
|
||||
t.Fatalf("two passes listed %d times, asked %v and said %v", listed, door.asked, b.published)
|
||||
}
|
||||
h.Listed = inner
|
||||
h.Pass(context.Background(), "after the build b")
|
||||
if _, found, _ := b.Get(FindingsState, key); found || len(door.asked) == 0 {
|
||||
t.Fatalf("a pass that read the list left the finding (%v) or swept nothing (%v)", found, door.asked)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -79,20 +79,46 @@ type Store struct {
|
||||
Ask func(key string, body any) (json.RawMessage, error)
|
||||
}
|
||||
|
||||
// ListOf asks seat's list, of one repository or, with name empty, of every one.
|
||||
// ListOf asks seat's list, of one repository or, with name empty, of every one. oci-artifact-registry's list is read page by
|
||||
// page from its cursor until it answers none (design 47 H2 §813 (7), §779); container-image-registry's answers whole.
|
||||
func (s *Store) ListOf(seat, name string) ([]reg.Repository, error) {
|
||||
if s == nil || s.Ask == nil {
|
||||
return nil, fmt.Errorf("this process reaches no registry seat")
|
||||
}
|
||||
raw, err := s.Ask("seat:"+seat+"."+reg.VerbList, reg.ListInput{Name: name})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%s's list: %w", seat, err)
|
||||
if seat != ociseat.Seat {
|
||||
raw, err := s.Ask("seat:"+seat+"."+reg.VerbList, reg.ListInput{Name: name})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%s's list: %w", seat, err)
|
||||
}
|
||||
var a reg.ListAnswer
|
||||
if err := json.Unmarshal(raw, &a); err != nil {
|
||||
return nil, fmt.Errorf("%s's answer to list: %w", seat, err)
|
||||
}
|
||||
return a.Repositories, nil
|
||||
}
|
||||
var a reg.ListAnswer
|
||||
if err := json.Unmarshal(raw, &a); err != nil {
|
||||
return nil, fmt.Errorf("%s's answer to list: %w", seat, err)
|
||||
var out []reg.Repository
|
||||
seen := map[string]bool{}
|
||||
for in := (ociseat.ListInput{Name: name}); ; {
|
||||
raw, err := s.Ask("seat:"+seat+"."+reg.VerbList, in)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%s's list: %w", seat, err)
|
||||
}
|
||||
var a struct {
|
||||
Repositories []reg.Repository `json:"repositories"`
|
||||
Next string `json:"next"`
|
||||
}
|
||||
if err := json.Unmarshal(raw, &a); err != nil {
|
||||
return nil, fmt.Errorf("%s's answer to list: %w", seat, err)
|
||||
}
|
||||
out = append(out, a.Repositories...)
|
||||
if a.Next == "" {
|
||||
return out, nil
|
||||
}
|
||||
if seen[a.Next] {
|
||||
return nil, fmt.Errorf("%s's list answered the cursor %q twice", seat, a.Next)
|
||||
}
|
||||
seen[a.Next], in.Next = true, a.Next
|
||||
}
|
||||
return a.Repositories, nil
|
||||
}
|
||||
|
||||
// View reads the store now, through both seats.
|
||||
|
||||
@@ -32,7 +32,7 @@ func main() {
|
||||
h.Acts = meshhandactsclient.Recorder{Ask: stdio.Ask}
|
||||
// The module's one held writer (design 47 G §689, A-16, A-29, A-32): the holder's findings and the reader's go through it.
|
||||
w, err := views.NewStdioHeldWriter(views.WriterConfig[meshconditions.Finding, meshconditions.FindingCleared, meshconditions.FindingsStated, meshconditions.FindingsAskedEvent]{
|
||||
Asked: meshconditions.SubjectFindingsAsked, Module: Module, State: stdio.State("findings"), Kinds: []string{ImageMissing, SweepRefused}})
|
||||
Asked: meshconditions.SubjectFindingsAsked, Module: Module, State: stdio.State("findings"), Kinds: []string{ImageMissing, SweepRefused, ListUnread, BuildsUnread}})
|
||||
if err != nil {
|
||||
logf("%v", err)
|
||||
os.Exit(1)
|
||||
|
||||
@@ -92,7 +92,20 @@ func (f *fakeStore) ask(t *testing.T) func(string, any) (json.RawMessage, error)
|
||||
repos = append(repos, reg.Repository{Name: name, Manifests: ms})
|
||||
}
|
||||
}
|
||||
return json.Marshal(reg.ListAnswer{Repositories: repos})
|
||||
if seat != "oci-artifact-registry" {
|
||||
return json.Marshal(reg.ListAnswer{Repositories: repos})
|
||||
}
|
||||
// oci-artifact-registry pages (design 47 H2 §813 (7)): one repository a page, after the one its cursor names.
|
||||
var page struct{ Next string }
|
||||
_ = json.Unmarshal(raw, &page)
|
||||
for len(repos) > 0 && repos[0].Name <= page.Next {
|
||||
repos = repos[1:]
|
||||
}
|
||||
next := ""
|
||||
if len(repos) > 1 {
|
||||
repos, next = repos[:1], repos[0].Name
|
||||
}
|
||||
return json.Marshal(map[string]any{"repositories": repos, "next": next, "capped": next != ""})
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -27,6 +27,9 @@ import (
|
||||
// SweepRefused is the sweep's finding: a seat refused a hold or a remove the sweep asked.
|
||||
const SweepRefused = "sweep-refused"
|
||||
|
||||
// ListUnread is the pass's finding: the registry seats' list could not be read, so the pass ended (design 47 H, H-A-250 (3)).
|
||||
const ListUnread = "artifact-list-unread"
|
||||
|
||||
// canonical is a kept reference in the older form, `artifact-store://<path>`, whichever form it came in; another is as
|
||||
// it was.
|
||||
func canonical(reference string) string {
|
||||
@@ -140,6 +143,53 @@ func (h *Holder) sweepClean() {
|
||||
}
|
||||
}
|
||||
|
||||
// BuildsUnread is the pass's finding: the builder's builds could not be judged, so the pass ended (design 47 H, H-A-260).
|
||||
const BuildsUnread = "artifact-builds-unread"
|
||||
|
||||
// unreadFinding writes a pass's finding of what it could not read (ListUnread, BuildsUnread) once: a pass that fails the
|
||||
// same way while the writer holds it writes nothing more. Nothing asks again until the next event marks a pass.
|
||||
func (h *Holder) unreadFinding(kind string, err error) {
|
||||
for _, was := range h.Findings.Held() {
|
||||
if was.Kind == kind {
|
||||
return
|
||||
}
|
||||
}
|
||||
at := h.now().UTC()
|
||||
f := meshconditions.Finding{Scope: "module", ID: keys.EscapeName(Module), Kind: kind,
|
||||
Subject: meshconditions.Subject{Scope: "module", ID: keys.EscapeName(Module)},
|
||||
Severity: meshconditions.FindingSeverityWarning, Resolver: "operator",
|
||||
Summary: "the artifact sweep stopped: the registry seats' list could not be read (the evidence names why)",
|
||||
Headline: "The artifact sweep cannot read the store's list",
|
||||
Explanation: "The sweep judges what to keep against the store's list, which it asks the registry seats. The list " +
|
||||
"could not be read, so the sweep removed nothing and observed nothing; what it kept stays kept.",
|
||||
Needs: "read the registry seat holder's journal for why its list failed; the next finished build or want change asks again.",
|
||||
Evidence: []meshconditions.Evidence{{At: at, Said: err.Error()}},
|
||||
Source: "CAT-AS1", Observer: Module, At: at}
|
||||
if kind == BuildsUnread {
|
||||
f.Summary = "the artifact sweep stopped: the builder's builds could not be judged (the evidence names why)"
|
||||
f.Headline = "The artifact sweep cannot judge the builds"
|
||||
f.Explanation = "The sweep keeps each module's newest builds and every wanted one, judged from the builder's builds " +
|
||||
"and when each ended. Those could not all be read, so the sweep removed nothing and observed nothing; what " +
|
||||
"it kept stays kept."
|
||||
f.Needs = "read this module's journal for which build's end is unread; the next finished build or want change asks again."
|
||||
}
|
||||
if werr := h.Findings.Write(context.Background(), f); werr != nil {
|
||||
h.logf("the pass's finding %s (%v) could not be written: %v", kind, err, werr)
|
||||
}
|
||||
}
|
||||
|
||||
// readAgain clears a pass's finding of kind, when the held writer holds it, once a pass read what it could not.
|
||||
func (h *Holder) readAgain(kind string) {
|
||||
for _, was := range h.Findings.Held() {
|
||||
if was.Kind != kind {
|
||||
continue
|
||||
}
|
||||
if err := h.Findings.Clear(context.Background(), was.Scope, was.ID, was.Kind, "a pass read it again"); err != nil {
|
||||
h.logf("the pass's finding %s could not be cleared: %v", kind, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// keptWhy is why a kept reference is held, as the seat's hold takes it.
|
||||
func keptWhy(r *Ref) string {
|
||||
if r == nil || len(r.Why) == 0 {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
{
|
||||
"module": "artifact-store-tools",
|
||||
"version": "0.1",
|
||||
"class": "core",
|
||||
"packages": {
|
||||
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-builder/builds": "0.1"
|
||||
},
|
||||
@@ -111,7 +112,10 @@
|
||||
{
|
||||
"name": "findings",
|
||||
"storage": "key-value",
|
||||
"template": "state"
|
||||
"template": "state",
|
||||
"options": {
|
||||
"max-mib": 8
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "finding-events",
|
||||
@@ -124,7 +128,7 @@
|
||||
"findings-stated.*"
|
||||
],
|
||||
"max-age-days": 7,
|
||||
"max-mib": 16
|
||||
"max-mib": 8
|
||||
}
|
||||
}
|
||||
]
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
{
|
||||
"module": "asus-zephyrus-g14",
|
||||
"version": "0.1",
|
||||
"class": "leaf",
|
||||
"moved": true,
|
||||
"templates": "templates",
|
||||
"capabilities": [
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
{
|
||||
"module": "audit-logger",
|
||||
"version": "0.1",
|
||||
"class": "leaf",
|
||||
"slug": "audit",
|
||||
"consumes": [
|
||||
"**"
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
{
|
||||
"module": "avahi",
|
||||
"version": "0.1",
|
||||
"class": "leaf",
|
||||
"capabilities": [
|
||||
"package-manager",
|
||||
"service-manager"
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
{
|
||||
"module": "baserow",
|
||||
"version": "0.1",
|
||||
"class": "leaf",
|
||||
"capabilities": [
|
||||
"container-runtime"
|
||||
],
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
{
|
||||
"module": "blueman",
|
||||
"version": "0.1",
|
||||
"class": "leaf",
|
||||
"capabilities": [
|
||||
"package-manager"
|
||||
],
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
{
|
||||
"module": "bluetooth",
|
||||
"version": "0.1",
|
||||
"class": "leaf",
|
||||
"capabilities": [
|
||||
"package-manager",
|
||||
"service-manager"
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
{
|
||||
"module": "brother",
|
||||
"version": "0.1",
|
||||
"class": "core",
|
||||
"requires": [
|
||||
{
|
||||
"name": "s3-bucket",
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
{
|
||||
"module": "buildah",
|
||||
"version": "0.1",
|
||||
"class": "leaf",
|
||||
"capabilities": [
|
||||
"package-manager",
|
||||
"user-namespaces"
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
{
|
||||
"module": "ca-trust",
|
||||
"version": "0.1",
|
||||
"class": "core",
|
||||
"slug": "catrust",
|
||||
"capabilities": [
|
||||
"service-manager"
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
{
|
||||
"module": "claude-code",
|
||||
"version": "0.1",
|
||||
"class": "leaf",
|
||||
"moved": true,
|
||||
"slug": "agent",
|
||||
"capabilities": [
|
||||
@@ -23,7 +24,7 @@
|
||||
"findings-stated.*"
|
||||
],
|
||||
"max-age-days": 7,
|
||||
"max-mib": 16
|
||||
"max-mib": 8
|
||||
}
|
||||
},
|
||||
{
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
{
|
||||
"module": "claude-licence-manager",
|
||||
"version": "0.1",
|
||||
"class": "core",
|
||||
"slug": "licmgr",
|
||||
"runs-as": "claude-licence-manager",
|
||||
"requires": [
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
{
|
||||
"module": "clipmenu",
|
||||
"version": "0.1",
|
||||
"class": "core",
|
||||
"moved": true,
|
||||
"capabilities": [
|
||||
"package-manager"
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
{
|
||||
"module": "confluence",
|
||||
"version": "0.1",
|
||||
"class": "leaf",
|
||||
"slug": "confl",
|
||||
"own-secrets": {
|
||||
"token": "${dir:state}/token"
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
{
|
||||
"module": "cups",
|
||||
"version": "0.1",
|
||||
"class": "leaf",
|
||||
"capabilities": [
|
||||
"package-manager",
|
||||
"service-manager"
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
{
|
||||
"module": "dbus",
|
||||
"version": "0.1",
|
||||
"class": "core",
|
||||
"capabilities": [
|
||||
"package-manager",
|
||||
"service-manager"
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
{
|
||||
"module": "desk-channel",
|
||||
"version": "0.1",
|
||||
"class": "core",
|
||||
"slug": "desk",
|
||||
"runs-as": "desk-channel",
|
||||
"claims": [
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
{
|
||||
"module": "dev-tools",
|
||||
"version": "0.1",
|
||||
"class": "leaf",
|
||||
"capabilities": [
|
||||
"package-manager"
|
||||
],
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
{
|
||||
"module": "disk-load",
|
||||
"version": "0.1",
|
||||
"class": "leaf",
|
||||
"tools": [
|
||||
"disk_load",
|
||||
"disk_top",
|
||||
|
||||
@@ -46,8 +46,13 @@ func stepErr(bound time.Duration, what string, f func() error) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// boundedObjects is the incoming object store with each operation bounded and logged.
|
||||
type boundedObjects struct{ parts.Objects }
|
||||
// boundedObjects is the incoming object store with each operation bounded and logged. Purge is the store's purge of a
|
||||
// deleted object's chunks (busstate.Objects.Purge): every delete of a chunk is followed by it, so what a delete left
|
||||
// behind is dropped and the store's kept descriptions equal its live objects (design 47 H2 §813 (8); H-A-227, H-A-366).
|
||||
type boundedObjects struct {
|
||||
parts.Objects
|
||||
Purge func(name string) error
|
||||
}
|
||||
|
||||
func (b boundedObjects) Used() (uint64, error) {
|
||||
return step(stepBound, "the incoming object store's fill (objects)", b.Objects.Used)
|
||||
@@ -59,7 +64,15 @@ 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) })
|
||||
err := stepErr(stepBound, "the delete of a chunk in the incoming object store (objects)", func() error { return b.Objects.Delete(name) })
|
||||
if b.Purge == nil {
|
||||
return err
|
||||
}
|
||||
perr := stepErr(stepBound, "the purge of a deleted chunk's leftovers in the incoming object store (objects)", func() error { return b.Purge(name) })
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return perr
|
||||
}
|
||||
|
||||
// boundedTransfers is the transfers state with its reads and writes bounded and logged.
|
||||
|
||||
@@ -8,8 +8,16 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// The incoming store's steps (design 47 H2 §819: one test function for the bounded steps).
|
||||
func TestTheBoundedSteps(t *testing.T) {
|
||||
t.Run("a hung step answers with its name", hungStepAnswersWithItsName)
|
||||
t.Run("a given-up step says when its call ended", givenUpStepSaysWhenItsCallEnded)
|
||||
t.Run("a deleted chunk is purged", deletedChunkIsPurged)
|
||||
t.Run("a finding is said off the path", findingIsSaidOffThePath)
|
||||
}
|
||||
|
||||
// A bus operation that never answers becomes an error naming its step, within the bound.
|
||||
func TestAHungStepAnswersWithItsName(t *testing.T) {
|
||||
func hungStepAnswersWithItsName(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 })
|
||||
@@ -22,7 +30,7 @@ func TestAHungStepAnswersWithItsName(t *testing.T) {
|
||||
}
|
||||
|
||||
// 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) {
|
||||
func givenUpStepSaysWhenItsCallEnded(t *testing.T) {
|
||||
var mu sync.Mutex
|
||||
var lines []string
|
||||
ended := make(chan struct{})
|
||||
@@ -52,7 +60,7 @@ func TestAGivenUpStepSaysWhenItsCallEnded(t *testing.T) {
|
||||
}
|
||||
|
||||
// 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) {
|
||||
func findingIsSaidOffThePath(t *testing.T) {
|
||||
block := make(chan struct{})
|
||||
defer close(block)
|
||||
say := boundedFinding(func(string, string, string, bool) { <-block })
|
||||
@@ -68,3 +76,31 @@ func TestAFindingIsSaidOffThePath(t *testing.T) {
|
||||
t.Fatal("the call was held by the finding")
|
||||
}
|
||||
}
|
||||
|
||||
// fakeObjects is an object store that counts deletes.
|
||||
type fakeObjects struct{ deleted []string }
|
||||
|
||||
func (f *fakeObjects) Put(string, []byte) error { return nil }
|
||||
func (f *fakeObjects) Get(string) ([]byte, error) { return nil, nil }
|
||||
func (f *fakeObjects) Delete(name string) error { f.deleted = append(f.deleted, name); return nil }
|
||||
func (f *fakeObjects) Used() (uint64, error) { return 0, nil }
|
||||
|
||||
// Every delete of a chunk purges what it left behind, so the store's kept descriptions equal its live objects (design 47
|
||||
// H2 §813 (8)); a purge's failure is said, never swallowed.
|
||||
func deletedChunkIsPurged(t *testing.T) {
|
||||
f := &fakeObjects{}
|
||||
var purged []string
|
||||
b := boundedObjects{f, func(name string) error { purged = append(purged, name); return nil }}
|
||||
for i := range 1000 {
|
||||
if err := b.Delete(fmt.Sprintf("chunk.%d", i)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if len(f.deleted) != 1000 || len(purged) != 1000 || purged[999] != "chunk.999" {
|
||||
t.Fatalf("deleted %d, purged %d", len(f.deleted), len(purged))
|
||||
}
|
||||
b.Purge = func(string) error { return fmt.Errorf("no responders") }
|
||||
if err := b.Delete("chunk.x"); err == nil || !strings.Contains(err.Error(), "no responders") {
|
||||
t.Fatalf("a failed purge answered %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,7 +36,7 @@ func TestTheVerbsSpeakTheSeatsContract(t *testing.T) {
|
||||
reg.VerbVersions: reg.VersionsInput{}, reg.VerbRemove: reg.RemoveInput{}, reg.VerbHold: reg.HoldInput{},
|
||||
reg.VerbRelease: reg.ReleaseInput{}, reg.VerbMirror: reg.MirrorInput{}, reg.VerbTags: reg.TagsInput{}}
|
||||
seen := 0
|
||||
for _, tool := range tools(&holder.Verbs{}, kept[holder.Window]{}) {
|
||||
for _, tool := range tools(&holder.Verbs{}, kept[holder.Window]{}, nil) {
|
||||
seat, verb, ok := strings.Cut(tool.Name, ".")
|
||||
in, isVerb := inputs[verb]
|
||||
if !ok || !isVerb || (seat != holder.ImageSeat && seat != holder.ArtifactSeat) {
|
||||
|
||||
@@ -22,9 +22,10 @@ import (
|
||||
"git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshhandactsclient"
|
||||
meshconditions "git.novox.be/novox/mesh-contracts/health-and-repair/go/seats/meshconditions"
|
||||
reg "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/containerimageregistry"
|
||||
oci "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/ociartifactregistry"
|
||||
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/parts"
|
||||
stdio "git.novox.be/novox/mesh-sdk/go"
|
||||
"git.novox.be/novox/mesh-sdk/go/seatlist"
|
||||
"git.novox.be/novox/mesh-sdk/go/busstate"
|
||||
"git.novox.be/novox/mesh-sdk/go/views"
|
||||
|
||||
"distribution/internal/holder"
|
||||
@@ -243,7 +244,7 @@ func arg[T any](args map[string]any) (T, error) {
|
||||
func str(args map[string]any, k string) string { s, _ := args[k].(string); return strings.TrimSpace(s) }
|
||||
|
||||
// tools are distribution's tools over v.
|
||||
func tools(v *holder.Verbs, windows kept[holder.Window]) []stdio.Tool {
|
||||
func tools(v *holder.Verbs, windows kept[holder.Window], taken *asks) []stdio.Tool {
|
||||
obj := func(props map[string]any, req ...string) map[string]any {
|
||||
return map[string]any{"type": "object", "required": req, "properties": props}
|
||||
}
|
||||
@@ -261,7 +262,9 @@ func tools(v *holder.Verbs, windows kept[holder.Window]) []stdio.Tool {
|
||||
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))
|
||||
r := publishOf(in)
|
||||
a, err := taken.once(seat, stdio.AskIDOf(args), func() (holder.PublishAnswer, error) { return v.Publish(seat, caller, r) },
|
||||
func() (holder.PublishAnswer, error) { return v.Repeat(seat, caller, r) })
|
||||
if err == nil {
|
||||
// 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.
|
||||
@@ -311,15 +314,7 @@ func tools(v *holder.Verbs, windows kept[holder.Window]) []stdio.Tool {
|
||||
}
|
||||
return reg.RemoveAnswer{Removed: got.Removed, Kept: got.Kept, Reason: got.Reason, Tags: got.Tags, Artifact: ®.Artifact{Name: in.Name, Version: in.Version, Tag: in.Tag, Digest: in.Digest}}, nil
|
||||
}},
|
||||
stdio.Tool{Name: seat + "." + holder.VerbList, Description: "What the store holds of one repository, or of every repository the seat holds: each manifest's digest, whether it is an index and the manifests it names, the tags naming it, its size and whether it is held (ADR 0632 §3). Read from the store's own files; changes nothing.",
|
||||
Input: obj(map[string]any{"name": s("<module>/<artifact>; every repository when not given")}),
|
||||
Run: func(args map[string]any) (any, error) {
|
||||
repos, err := v.List(context.Background(), seat, stdio.CallerOf(args), str(args, "name"))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return reg.ListAnswer{Repositories: repos}, nil
|
||||
}},
|
||||
listTool(v, seat),
|
||||
stdio.Tool{Name: seat + "." + holder.VerbHold, Description: "Keep one untagged bundle, archive or copied base from retention (the artifact sweep alone); says held.",
|
||||
Input: obj(map[string]any{"name": s("name"), "digest": s("sha256:…"), "reason": s("which of the sweep's rules keeps it")}, "name", "digest", "reason"),
|
||||
Run: func(args map[string]any) (any, error) {
|
||||
@@ -488,11 +483,11 @@ func main() {
|
||||
}()
|
||||
base, _ := url.Parse("http://127.0.0.1:5001")
|
||||
store := &holder.OCI{Base: base, Upstream: &holder.Upstream{}}
|
||||
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),
|
||||
h := &holder.Holder{Logf: logf, Objects: boundedObjects{parts.BusObjects(holder.IncomingObjects), busstate.ObjectsOf(holder.IncomingObjects).Purge}, 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) }}
|
||||
Files: files}
|
||||
go followMirrors(v)
|
||||
go parts.FollowIdle(holder.IdleState, parts.IDs(h.Transfers), h.Expired, func(id string, dropped bool, err error) {
|
||||
if err != nil {
|
||||
@@ -502,7 +497,13 @@ func main() {
|
||||
logf("transfer %s had no part for %s: dropped with its parts", id, holder.TransferIdle)
|
||||
})
|
||||
windows := kept[holder.Window]{state: "windows", key: func(w holder.Window) string { return w.Name }}
|
||||
if err := stdio.Serve("", tools(v, windows)); err != nil {
|
||||
bound, err := stdio.SettingDuration(askTakenBound, askTakenDefault)
|
||||
if err != nil {
|
||||
logf("the setting %s: %v", askTakenBound, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
taken := &asks{take: stdio.State(stdio.TakenState).TakeOnce, bound: bound}
|
||||
if err := stdio.Serve("", tools(v, windows, taken)); err != nil {
|
||||
logf("%v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
@@ -514,3 +515,41 @@ func or(v, d string) string {
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
// listTool is the seat's verb list. On oci-artifact-registry it filters by name or module while it reads and pages at 768 KB
|
||||
// by the cursor next (design 47 H2 §779, §811, §813 (7)); on container-image-registry, whose form takes no cursor, it answers
|
||||
// whole.
|
||||
func listTool(v *holder.Verbs, seat string) stdio.Tool {
|
||||
s := func(d string) map[string]any { return map[string]any{"type": "string", "description": d} }
|
||||
description := "What the store holds of one repository, or of every repository the seat holds: each manifest's digest, whether it is an index and the manifests it names, the tags naming it, its size and whether it is held (ADR 0632 §3). Read from the store's own files; changes nothing."
|
||||
props := map[string]any{"name": s("<module>/<artifact>; every repository when not given")}
|
||||
if seat == holder.ArtifactSeat {
|
||||
description += " Filtered by name or module while it reads and paged by the cursor next, at most 768 KB a page, capped saying the rest is read from next."
|
||||
props["module"] = s("only the repositories of this module, <module>/*")
|
||||
props["next"] = s("the cursor the previous page answered; absent for the first page")
|
||||
}
|
||||
return stdio.Tool{Name: seat + "." + holder.VerbList, Description: description, Input: map[string]any{"type": "object", "properties": props},
|
||||
Run: func(args map[string]any) (any, error) {
|
||||
if seat != holder.ArtifactSeat {
|
||||
page, err := v.List(context.Background(), seat, stdio.CallerOf(args), holder.ListQuery{Name: str(args, "name")})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return reg.ListAnswer{Repositories: page.Repositories}, nil
|
||||
}
|
||||
in, err := arg[oci.ListInput](args)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
page, err := v.List(context.Background(), seat, stdio.CallerOf(args), holder.ListQuery{Name: in.Name, Module: in.Module, Next: in.Next, PageBytes: holder.ListPageBytes})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out := oci.ListAnswer{Form: oci.Form, Repositories: []oci.Repository{}, Capped: page.Capped, Next: page.Next, Total: int64(page.Total)}
|
||||
raw, _ := json.Marshal(page.Repositories)
|
||||
if err := json.Unmarshal(raw, &out.Repositories); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"distribution/internal/holder"
|
||||
)
|
||||
|
||||
// askTakenBound is the setting the taken key of a publish ask lives (design 47 H2 §775, H-A-381 (2)): the holder never
|
||||
// receives the caller's bound, so it keeps an ask's id this long, the largest caller's bound by default.
|
||||
const askTakenBound = "ask-taken-bound"
|
||||
|
||||
// askTakenDefault is ask-taken-bound's default, the runner's ask-unanswered-bound (H-A-381 (2)).
|
||||
const askTakenDefault = 10 * time.Minute
|
||||
|
||||
// asks takes each publish ask once by its id (design 47 H2 §775; ADR 0668 §2; H-A-381): the first call with an id is
|
||||
// kept in the state taken and acts; a repeat while that act runs joins it and gets its answer; a repeat after it ended
|
||||
// answers the act's state read afresh (holder.Repeat), never acting twice. No answer is kept in taken. A call that
|
||||
// carries no ask id is no kept call, and acts as it did before.
|
||||
type asks struct {
|
||||
take func(seat, ask string, bound time.Duration) (bool, error)
|
||||
bound time.Duration
|
||||
mu sync.Mutex
|
||||
running map[string]*flight
|
||||
// joined, when set, is told each repeat that joins a running act (a test's hook).
|
||||
joined func()
|
||||
}
|
||||
|
||||
// flight is one act of a taken ask while it runs, which a repeat joins.
|
||||
type flight struct {
|
||||
done chan struct{}
|
||||
a holder.PublishAnswer
|
||||
err error
|
||||
}
|
||||
|
||||
func (k *asks) once(seat, ask string, act, repeat func() (holder.PublishAnswer, error)) (holder.PublishAnswer, error) {
|
||||
if ask == "" || k == nil || k.take == nil {
|
||||
return act()
|
||||
}
|
||||
key := seat + "." + ask
|
||||
k.mu.Lock()
|
||||
if f, ok := k.running[key]; ok {
|
||||
k.mu.Unlock()
|
||||
if k.joined != nil {
|
||||
k.joined()
|
||||
}
|
||||
<-f.done
|
||||
return f.a, f.err
|
||||
}
|
||||
if k.running == nil {
|
||||
k.running = map[string]*flight{}
|
||||
}
|
||||
f := &flight{done: make(chan struct{})}
|
||||
k.running[key] = f
|
||||
k.mu.Unlock()
|
||||
defer func() {
|
||||
k.mu.Lock()
|
||||
delete(k.running, key)
|
||||
k.mu.Unlock()
|
||||
close(f.done)
|
||||
}()
|
||||
took, err := k.take(seat, ask, k.bound)
|
||||
switch {
|
||||
case err != nil:
|
||||
f.err = fmt.Errorf("publish: the ask %s of %s cannot be taken: %w", ask, seat, err)
|
||||
case took:
|
||||
f.a, f.err = act()
|
||||
default:
|
||||
logf("%s.publish: the ask %s was taken before; it answers the act's state, acting not again", seat, ask)
|
||||
f.a, f.err = repeat()
|
||||
}
|
||||
return f.a, f.err
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"distribution/internal/holder"
|
||||
)
|
||||
|
||||
// A publish ask given twice acts once (design 47 H2 §775; H-A-381): a repeat while the act runs joins it and gets its
|
||||
// answer, a repeat after it ended answers the act's state, a call with no ask id acts as before, a take that fails is said.
|
||||
func TestAPublishAskIsTakenOnce(t *testing.T) {
|
||||
taken := map[string]bool{}
|
||||
var mu sync.Mutex
|
||||
k := &asks{bound: time.Minute, take: func(seat, ask string, bound time.Duration) (bool, error) {
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
if ask == "broken" {
|
||||
return false, errors.New("no responders")
|
||||
}
|
||||
if bound != time.Minute || taken[seat+"."+ask] {
|
||||
return false, nil
|
||||
}
|
||||
taken[seat+"."+ask] = true
|
||||
return true, nil
|
||||
}}
|
||||
var acts, repeats atomic.Int32
|
||||
release, started, joined := make(chan struct{}), make(chan struct{}, 1), make(chan struct{}, 1)
|
||||
k.joined = func() { joined <- struct{}{} }
|
||||
act := func() (holder.PublishAnswer, error) {
|
||||
acts.Add(1)
|
||||
started <- struct{}{}
|
||||
<-release
|
||||
return holder.PublishAnswer{Transfer: "t", Missing: []string{"d"}}, nil
|
||||
}
|
||||
repeat := func() (holder.PublishAnswer, error) { repeats.Add(1); return holder.PublishAnswer{Transfer: "t"}, nil }
|
||||
answers := make(chan holder.PublishAnswer, 2)
|
||||
go func() { a, _ := k.once(holder.ArtifactSeat, "a1", act, repeat); answers <- a }()
|
||||
<-started
|
||||
go func() { a, _ := k.once(holder.ArtifactSeat, "a1", act, repeat); answers <- a }()
|
||||
<-joined
|
||||
close(release)
|
||||
for range 2 {
|
||||
if a := <-answers; len(a.Missing) != 1 {
|
||||
t.Errorf("an answer of the running act = %+v", a)
|
||||
}
|
||||
}
|
||||
if a, _ := k.once(holder.ArtifactSeat, "a1", act, repeat); acts.Load() != 1 || repeats.Load() != 1 || a.Transfer != "t" {
|
||||
t.Errorf("acts %d, repeats %d after a repeat of an ended act", acts.Load(), repeats.Load())
|
||||
}
|
||||
release = make(chan struct{})
|
||||
close(release)
|
||||
act2 := func() (holder.PublishAnswer, error) { acts.Add(1); return holder.PublishAnswer{}, nil }
|
||||
if _, err := k.once(holder.ArtifactSeat, "", act2, repeat); err != nil || acts.Load() != 2 {
|
||||
t.Errorf("a call with no ask id: acts %d, %v", acts.Load(), err)
|
||||
}
|
||||
if _, err := k.once(holder.ArtifactSeat, "broken", act2, repeat); err == nil || acts.Load() != 2 {
|
||||
t.Errorf("a failed take: acts %d, %v", acts.Load(), err)
|
||||
}
|
||||
}
|
||||
@@ -10,6 +10,7 @@ require (
|
||||
git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/containerimageregistry v0.0.0-00010101000000-000000000000
|
||||
git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshbuilds v0.0.0-00010101000000-000000000000
|
||||
git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog v0.0.0-00010101000000-000000000000
|
||||
git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/ociartifactregistry v0.0.0-00010101000000-000000000000
|
||||
git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk v0.0.0-00010101000000-000000000000
|
||||
git.novox.be/novox/mesh-sdk/go v0.0.0-00010101000000-000000000000
|
||||
github.com/nats-io/nats.go v1.54.0
|
||||
@@ -31,7 +32,6 @@ require (
|
||||
|
||||
require (
|
||||
git.novox.be/novox/mesh-contracts/health-and-repair/go/seats/meshhandacts v0.0.0-00010101000000-000000000000 // indirect
|
||||
git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/ociartifactregistry v0.0.0-00010101000000-000000000000 // indirect
|
||||
)
|
||||
|
||||
require git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdelivery v0.0.0-00010101000000-000000000000 // indirect
|
||||
|
||||
@@ -62,17 +62,35 @@ func (b fakeBuilder) Recorded(string, string, string) (bool, string, error) {
|
||||
}
|
||||
|
||||
type fakeStore struct {
|
||||
has map[string]bool
|
||||
put []map[string][]byte
|
||||
has map[string]bool
|
||||
held map[string]heldAt // name@version to what the store holds there
|
||||
put []map[string][]byte
|
||||
}
|
||||
|
||||
// heldAt is what a version's tag names in the store: the manifest's digest and, for a bundle or an archive, the one layer
|
||||
// that manifest keeps, two digests as the store holds them (H-A-474).
|
||||
type heldAt struct{ manifest, layer string }
|
||||
|
||||
func (s *fakeStore) Has(_, d string) (bool, error) { return s.has[d], nil }
|
||||
func (s *fakeStore) Put(t PublishRequest, parts map[string][]byte) (string, error) {
|
||||
s.put = append(s.put, parts)
|
||||
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 }
|
||||
func (s *fakeStore) Held(r PublishRequest) (string, string, error) {
|
||||
at, ok := s.held[r.Name+"@"+r.Version]
|
||||
if !ok {
|
||||
return "", "", nil
|
||||
}
|
||||
d := at.layer
|
||||
if r.Kind == KindImage || r.Kind == KindUpstream {
|
||||
d = at.manifest
|
||||
}
|
||||
if d != r.Digest {
|
||||
return "", d, nil
|
||||
}
|
||||
return r.Name + "@" + d, "", nil
|
||||
}
|
||||
func (s *fakeStore) Holds(r PublishRequest) (bool, error) { return s.has[r.Digest], nil }
|
||||
|
||||
type said struct {
|
||||
seat, event string
|
||||
@@ -206,8 +224,7 @@ func newBed(builderOK bool) *bed {
|
||||
return nil
|
||||
},
|
||||
Finding: func(kind, subject, _ string, raised bool) { b.findings[kind+" "+subject] = raised }}
|
||||
b.v = &Verbs{Holder: b.h, Catalog: b.cat, Holds: memHolds{}, Mirrors: memMirrors{},
|
||||
SweepHolder: func() (string, error) { return "artifact-store-tools", nil }}
|
||||
b.v = &Verbs{Holder: b.h, Catalog: b.cat, Holds: memHolds{}, Mirrors: memMirrors{}}
|
||||
return b
|
||||
}
|
||||
|
||||
@@ -251,6 +268,25 @@ func TestTheHolderOfTheTwoImageSeats(t *testing.T) {
|
||||
}
|
||||
})
|
||||
|
||||
// A publish ask taken before answers the act's state read afresh and never publishes again (design 47 H2 §775;
|
||||
// H-A-381): an open transfer its lacking parts, a transfer gone with nothing held a refusal naming it.
|
||||
t.Run("a repeated ask answers the act's state", func(t *testing.T) {
|
||||
b := newBed(true)
|
||||
if _, err := b.h.Repeat(ArtifactSeat, builder, req("tr")); err == nil || !strings.Contains(err.Error(), "transfer tr is gone") {
|
||||
t.Errorf("a repeat of an act that never happened answered %v", err)
|
||||
}
|
||||
if _, err := b.h.Publish(ArtifactSeat, builder, req("tr")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
opened := len(b.h.Transfers.(memTransfers))
|
||||
if a, err := b.h.Repeat(ArtifactSeat, builder, req("tr")); err != nil || !reflect.DeepEqual(a.Missing, []string{part.Digest}) {
|
||||
t.Errorf("a repeat of an open transfer = %+v, %v", a, err)
|
||||
}
|
||||
if len(b.h.Transfers.(memTransfers)) != opened || len(b.said) != 0 {
|
||||
t.Errorf("a repeat acted: transfers %d, said %+v", len(b.h.Transfers.(memTransfers)), b.said)
|
||||
}
|
||||
})
|
||||
|
||||
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"))
|
||||
@@ -317,6 +353,16 @@ func TestTheHolderOfTheTwoImageSeats(t *testing.T) {
|
||||
_, err := b.h.Publish(ArtifactSeat, builder, req("t"))
|
||||
return err
|
||||
}, "the builder did not reserve it"},
|
||||
{"a version the store holds at another digest", func(b *bed) error {
|
||||
b.store.held = map[string]heldAt{"m/program@0.1.4": {manifest: "sha256:cc", layer: "sha256:bb"}}
|
||||
r := req("t")
|
||||
r.Digest = "sha256:aa"
|
||||
_, err := b.h.Publish(ArtifactSeat, builder, r) // the reservation names no digest yet: the bed's builder says yes
|
||||
if !b.findings["publish-refused m/program@0.1.4"] || len(b.store.put) != 0 {
|
||||
return errors.New("not refused loudly, or stored")
|
||||
}
|
||||
return err
|
||||
}, "the store holds m/program@0.1.4 at digest sha256:bb, not sha256:aa"},
|
||||
{"an image on the artifact seat", func(b *bed) error {
|
||||
r := req("t")
|
||||
r.Kind = KindImage
|
||||
@@ -327,10 +373,6 @@ func TestTheHolderOfTheTwoImageSeats(t *testing.T) {
|
||||
_, err := b.v.Remove(ArtifactSeat, "novox.mesh-delivery-builder", RemoveRequest{Name: "m/program", Version: "0.1.4", Reason: "branch ended"})
|
||||
return err
|
||||
}, "has no tag"},
|
||||
{"a retention removal from another caller than the sweep", func(b *bed) error {
|
||||
_, err := b.v.Remove(ArtifactSeat, "novox.mesh-delivery-builder", RemoveRequest{Name: "m/program", Digest: part.Digest, Reason: ReasonRetention})
|
||||
return err
|
||||
}, "artifact sweep's alone"},
|
||||
{"a retention removal of a held archive", func(b *bed) error {
|
||||
_ = b.v.HoldOne(ArtifactSeat, "novox.artifact-store-tools", HoldRequest{Name: "m/a", Kind: KindArchive, Digest: part.Digest})
|
||||
_, err := b.v.Remove(ArtifactSeat, "novox.artifact-store-tools", RemoveRequest{Name: "m/a", Digest: part.Digest, Reason: ReasonRetention})
|
||||
@@ -339,16 +381,6 @@ func TestTheHolderOfTheTwoImageSeats(t *testing.T) {
|
||||
{"a hold of an image", func(b *bed) error {
|
||||
return b.v.HoldOne(ImageSeat, "novox.artifact-store-tools", HoldRequest{Name: "m/i", Kind: KindImage, Digest: part.Digest})
|
||||
}, "never held"},
|
||||
{"a retention removal by the module that held the sweep seat before", func(b *bed) error {
|
||||
b.v.SweepHolder = func() (string, error) { return "artifact-sweeper", nil } // the seat's holder as the seat says it now
|
||||
_, err := b.v.Remove(ArtifactSeat, "novox.artifact-store-tools", RemoveRequest{Name: "m/program", Digest: part.Digest, Reason: ReasonRetention})
|
||||
return err
|
||||
}, "artifact sweep's alone"},
|
||||
{"a retention removal while the sweep's holder cannot be read", func(b *bed) error {
|
||||
b.v.SweepHolder = func() (string, error) { return "", errors.New("the seat's holders are not answered") }
|
||||
_, err := b.v.Remove(ArtifactSeat, "novox.artifact-store-tools", RemoveRequest{Name: "m/program", Digest: part.Digest, Reason: ReasonRetention})
|
||||
return err
|
||||
}, "cannot be read"},
|
||||
{"a release in distribution's own name", func(b *bed) error {
|
||||
_, err := b.v.Release(ArtifactSeat, "novox.distribution", HoldRequest{Name: "m/a", Digest: part.Digest})
|
||||
return err
|
||||
@@ -364,6 +396,47 @@ func TestTheHolderOfTheTwoImageSeats(t *testing.T) {
|
||||
}
|
||||
}
|
||||
})
|
||||
// H-A-474: the store judges a held version like with like, through its own OCI code over a store in memory. A bundle
|
||||
// is stored as a manifest whose digest is not its layer's: the same bytes published again are already held (ended, no
|
||||
// error, nothing stored again, also on a repeated ask); other bytes are refused naming the layer the store holds. An
|
||||
// image is judged by its manifest's digest.
|
||||
t.Run("a held version is judged by the artifact's own digest", func(t *testing.T) {
|
||||
reg := newFakeRegistry()
|
||||
sts := httptest.NewServer(reg)
|
||||
defer sts.Close()
|
||||
u, _ := url.Parse(sts.URL)
|
||||
o := &OCI{Base: u}
|
||||
if _, err := o.Put(req("first"), map[string][]byte{part.Digest: blob}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if m, _ := o.Digest("m/program", "0.1.4"); m == "" || m == part.Digest {
|
||||
t.Fatalf("the stored manifest's digest %q; want one, not the layer's %s", m, part.Digest)
|
||||
}
|
||||
b := newBed(true)
|
||||
b.h.Store = o
|
||||
uploads := len(reg.blobs)
|
||||
a, err := b.h.Publish(ArtifactSeat, builder, req("again"))
|
||||
if err != nil || !a.Ended || len(a.Missing) != 0 || len(b.said) != 1 || b.said[0].event != EventPublished || b.findings["publish-refused m/program@0.1.4"] {
|
||||
t.Errorf("the same bundle again = %+v, %v; said %+v; want already held, published said, nothing refused", a, err, b.said)
|
||||
}
|
||||
if a, err := b.h.Repeat(ArtifactSeat, builder, req("again")); err != nil || !a.Ended {
|
||||
t.Errorf("a repeated ask of the held bundle = %+v, %v; want ended", a, err)
|
||||
}
|
||||
other := req("other")
|
||||
other.Digest = digestOf([]byte("other bytes"))
|
||||
if _, err := b.h.Publish(ArtifactSeat, builder, other); err == nil || !strings.Contains(err.Error(), "at digest "+part.Digest+", not "+other.Digest) {
|
||||
t.Errorf("other bytes at the held version = %v; want refused naming the held layer %s", err, part.Digest)
|
||||
}
|
||||
if len(reg.blobs) != uploads {
|
||||
t.Errorf("the store took %d blobs more; want none", len(reg.blobs)-uploads)
|
||||
}
|
||||
img := []byte(`{"schemaVersion":2,"mediaType":"application/vnd.oci.image.manifest.v1+json","layers":[]}`)
|
||||
reg.manifests["m/image:0.2.0"], reg.types["m/image:0.2.0"] = img, mediaManifest
|
||||
ir := PublishRequest{Name: "m/image", Version: "0.2.0", Kind: KindImage, Digest: digestOf(img)}
|
||||
if ref, other, err := o.Held(ir); err != nil || ref != "m/image@"+digestOf(img) || other != "" {
|
||||
t.Errorf("an image held at its manifest = %q, %q, %v; want held", ref, other, err)
|
||||
}
|
||||
})
|
||||
t.Run("only the missing layers are sent, and an idle transfer is dropped", func(t *testing.T) {
|
||||
b := newBed(true)
|
||||
other := []byte("layer two")
|
||||
@@ -451,7 +524,8 @@ func TestTheHolderOfTheTwoImageSeats(t *testing.T) {
|
||||
t.Run("the sweep's removal, hold and release", func(t *testing.T) {
|
||||
b := newBed(true)
|
||||
sweep := "novox.artifact-store-tools"
|
||||
if _, err := b.v.Remove(ArtifactSeat, sweep, RemoveRequest{Name: "m/a", Digest: part.Digest, Kind: KindArchive, Reason: ReasonRetention}); err != nil {
|
||||
// A removal for retention is taken whoever asks it; the seat's holders are not read (design 47 H2 §813 (7)).
|
||||
if _, err := b.v.Remove(ArtifactSeat, "novox.artifact-sweeper", RemoveRequest{Name: "m/a", Digest: part.Digest, Kind: KindArchive, Reason: ReasonRetention}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := b.v.HoldOne(ArtifactSeat, sweep, HoldRequest{Name: "m/b", Kind: KindArchive, Digest: part.Digest}); err != nil {
|
||||
@@ -468,7 +542,7 @@ func TestTheHolderOfTheTwoImageSeats(t *testing.T) {
|
||||
for _, s := range b.said {
|
||||
events = append(events, s.event+" "+s.body.By)
|
||||
}
|
||||
if want := []string{"removed " + sweep, "held " + sweep, "released person.jochen"}; !reflect.DeepEqual(events, want) {
|
||||
if want := []string{"removed novox.artifact-sweeper", "held " + sweep, "released person.jochen"}; !reflect.DeepEqual(events, want) {
|
||||
t.Errorf("said %v, want %v", events, want)
|
||||
}
|
||||
b.cat.tags["m/p:0.1.5-feat-x.1"] = part.Digest
|
||||
@@ -904,10 +978,24 @@ func TestTheHolderOfTheTwoImageSeats(t *testing.T) {
|
||||
Name string `json:"name"`
|
||||
Port int `json:"port"`
|
||||
} `json:"listens"`
|
||||
Class string `json:"class"`
|
||||
State []struct {
|
||||
Name string `json:"name"`
|
||||
MaxMiB int `json:"max-mib"`
|
||||
} `json:"state"`
|
||||
}
|
||||
if err := json.Unmarshal(raw, &m); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// Design 47 H2 H-A-411 (2): a core module, and every state names its cap, 8 MiB.
|
||||
if m.Class != "core" {
|
||||
t.Errorf("class %q, want core", m.Class)
|
||||
}
|
||||
for _, st := range m.State {
|
||||
if st.MaxMiB != 8 {
|
||||
t.Errorf("the state %s names max-mib %d, want 8", st.Name, st.MaxMiB)
|
||||
}
|
||||
}
|
||||
// The reservation is asked through the seat mesh-builds, so the manifest takes no package of the builder (design 47 §381).
|
||||
if len(m.Packages) != 0 {
|
||||
t.Errorf("packages %v: want none", m.Packages)
|
||||
|
||||
@@ -9,9 +9,12 @@ package holder
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
reg "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/containerimageregistry"
|
||||
)
|
||||
@@ -25,27 +28,52 @@ type StoreFiles interface {
|
||||
Manifests(ctx context.Context, l *Listing) (map[string]*Manifest, map[string]string)
|
||||
}
|
||||
|
||||
// List is `list` on seat: the repositories it holds, or name alone; a repository the seat holds nothing of is not named,
|
||||
// and name not held is an empty answer. A manifest whose content cannot be read fails the answer, by name: a listing
|
||||
// missing one would read as the store not holding it.
|
||||
func (v *Verbs) List(ctx context.Context, seat, caller, name string) ([]reg.Repository, error) {
|
||||
// ListPageBytes is the most a page of oci-artifact-registry's list answers (design 47 H2 §811): 768 KB.
|
||||
const ListPageBytes = 768 << 10
|
||||
|
||||
// ListQuery is what a list is asked: Name or Module (`<module>/*`) filter the repositories while the files are read, Next
|
||||
// is the cursor a page answered, and PageBytes the most a page answers, 0 for the whole answer at once (the seat
|
||||
// container-image-registry's list, whose form takes no cursor).
|
||||
type ListQuery struct {
|
||||
Name, Module, Next string
|
||||
PageBytes int
|
||||
}
|
||||
|
||||
// ListPage is one page of a list: its repositories in order of their names, Total the repositories the filters hold in
|
||||
// all, and Next the cursor of the next page, empty at the end; Capped says the page stopped at PageBytes.
|
||||
type ListPage struct {
|
||||
Repositories []reg.Repository
|
||||
Capped bool
|
||||
Total int
|
||||
Next string
|
||||
}
|
||||
|
||||
// List is `list` on seat: the repositories it holds, or those q's filters name; a repository the seat holds nothing of is
|
||||
// not named, and a name not held is an empty answer. A paged list continues after the repository its cursor names, in
|
||||
// order of the names (design 47 H2 §779, §811, §813 (7)). A manifest whose content cannot be read fails the answer, by
|
||||
// name: a listing missing one would read as the store not holding it.
|
||||
func (v *Verbs) List(ctx context.Context, seat, caller string, q ListQuery) (ListPage, error) {
|
||||
if seat != ImageSeat && seat != ArtifactSeat {
|
||||
return nil, fmt.Errorf("list: %s is no seat of distribution's", seat)
|
||||
return ListPage{}, fmt.Errorf("list: %s is no seat of distribution's", seat)
|
||||
}
|
||||
if v.Files == nil {
|
||||
return nil, errors.New("list: this process reads no store files")
|
||||
return ListPage{}, errors.New("list: this process reads no store files")
|
||||
}
|
||||
after, err := listCursor(q.Next)
|
||||
if err != nil {
|
||||
return ListPage{}, err
|
||||
}
|
||||
l, err := v.Files.List(ctx)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("list: the store's files cannot be listed: %w", err)
|
||||
return ListPage{}, fmt.Errorf("list: the store's files cannot be listed: %w", err)
|
||||
}
|
||||
if name != "" {
|
||||
r, held := l.Repos[name]
|
||||
l = &Listing{Blobs: l.Blobs, Repos: map[string]*Repo{}}
|
||||
if held {
|
||||
l.Repos[name] = r
|
||||
kept := &Listing{Blobs: l.Blobs, Repos: map[string]*Repo{}}
|
||||
for name, r := range l.Repos {
|
||||
if (q.Name == "" || name == q.Name) && (q.Module == "" || strings.HasPrefix(name, q.Module+"/")) {
|
||||
kept.Repos[name] = r
|
||||
}
|
||||
}
|
||||
l = kept
|
||||
docs, unread := v.Files.Manifests(ctx, l)
|
||||
if len(unread) > 0 {
|
||||
keys := make([]string, 0, len(unread))
|
||||
@@ -53,11 +81,11 @@ func (v *Verbs) List(ctx context.Context, seat, caller, name string) ([]reg.Repo
|
||||
keys = append(keys, k)
|
||||
}
|
||||
sort.Strings(keys)
|
||||
return nil, fmt.Errorf("list: %d manifests cannot be read, the first %s: %s", len(keys), keys[0], unread[keys[0]])
|
||||
return ListPage{}, fmt.Errorf("list: %d manifests cannot be read, the first %s: %s", len(keys), keys[0], unread[keys[0]])
|
||||
}
|
||||
holds, err := v.Holds.List()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("list: the holds cannot be read: %w", err)
|
||||
return ListPage{}, fmt.Errorf("list: the holds cannot be read: %w", err)
|
||||
}
|
||||
held := map[string]bool{}
|
||||
for _, h := range holds {
|
||||
@@ -102,5 +130,37 @@ func (v *Verbs) List(ctx context.Context, seat, caller, name string) ([]reg.Repo
|
||||
out = append(out, reg.Repository{Name: repo, Manifests: manifests})
|
||||
}
|
||||
}
|
||||
return out, nil
|
||||
page := ListPage{Repositories: []reg.Repository{}, Total: len(out)}
|
||||
size := 0
|
||||
for _, r := range out {
|
||||
if r.Name <= after {
|
||||
continue
|
||||
}
|
||||
if q.PageBytes > 0 {
|
||||
raw, _ := json.Marshal(r)
|
||||
if size+len(raw) > q.PageBytes {
|
||||
if len(page.Repositories) == 0 {
|
||||
return ListPage{}, fmt.Errorf("list: the repository %s alone is over a page's %d bytes (%d)", r.Name, q.PageBytes, len(raw))
|
||||
}
|
||||
page.Capped = true
|
||||
page.Next = base64.RawURLEncoding.EncodeToString([]byte(page.Repositories[len(page.Repositories)-1].Name))
|
||||
break
|
||||
}
|
||||
size += len(raw)
|
||||
}
|
||||
page.Repositories = append(page.Repositories, r)
|
||||
}
|
||||
return page, nil
|
||||
}
|
||||
|
||||
// listCursor is the repository a page's cursor continues after, "" for the first page; a cursor no page answered is refused.
|
||||
func listCursor(next string) (string, error) {
|
||||
if next == "" {
|
||||
return "", nil
|
||||
}
|
||||
name, err := base64.RawURLEncoding.DecodeString(next)
|
||||
if err != nil || len(name) == 0 {
|
||||
return "", fmt.Errorf("list: %q is no cursor a page of list answered", next)
|
||||
}
|
||||
return string(name), nil
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package holder
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strings"
|
||||
@@ -132,7 +133,8 @@ func TestListAnswersTheStoreThroughTheSeat(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ctx := context.Background()
|
||||
images, err := b.v.List(ctx, ImageSeat, "mesh-anyone", "")
|
||||
page, err := b.v.List(ctx, ImageSeat, "mesh-anyone", ListQuery{})
|
||||
images := page.Repositories
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -155,7 +157,8 @@ func TestListAnswersTheStoreThroughTheSeat(t *testing.T) {
|
||||
if !reflect.DeepEqual(images, want) {
|
||||
t.Errorf("the image seat lists %+v\nwant %+v", images, want)
|
||||
}
|
||||
artifacts, err := b.v.List(ctx, ArtifactSeat, "mesh-anyone", "")
|
||||
page, err = b.v.List(ctx, ArtifactSeat, "mesh-anyone", ListQuery{})
|
||||
artifacts := page.Repositories
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -166,16 +169,42 @@ func TestListAnswersTheStoreThroughTheSeat(t *testing.T) {
|
||||
if !reflect.DeepEqual(artifacts, wantA) {
|
||||
t.Errorf("the artifact seat lists %+v\nwant %+v", artifacts, wantA)
|
||||
}
|
||||
if one, err := b.v.List(ctx, ImageSeat, "mesh-anyone", "app/tools"); err != nil || len(one) != 0 {
|
||||
if one, err := b.v.List(ctx, ImageSeat, "mesh-anyone", ListQuery{Name: "app/tools"}); err != nil || len(one.Repositories) != 0 {
|
||||
t.Errorf("the image seat's list of a bundle's repository: %+v, %v", one, err)
|
||||
}
|
||||
if one, err := b.v.List(ctx, ArtifactSeat, "mesh-anyone", "mesh/facts"); err != nil || len(one) != 1 || one[0].Name != "mesh/facts" {
|
||||
if one, err := b.v.List(ctx, ArtifactSeat, "mesh-anyone", ListQuery{Name: "mesh/facts"}); err != nil || len(one.Repositories) != 1 || one.Repositories[0].Name != "mesh/facts" {
|
||||
t.Errorf("one repository: %+v, %v", one, err)
|
||||
}
|
||||
// Design 47 H2 §813 (7), §811, §779: the artifact seat's list filters by module while it reads, and pages by its cursor in
|
||||
// order of the names, each page within its bytes, saying capped and the total the filters hold.
|
||||
if mod, err := b.v.List(ctx, ArtifactSeat, "mesh-anyone", ListQuery{Module: "app", PageBytes: ListPageBytes}); err != nil ||
|
||||
!reflect.DeepEqual(mod, ListPage{Repositories: wantA[:1], Total: 1}) {
|
||||
t.Errorf("the module app's repositories: %+v, %v", mod, err)
|
||||
}
|
||||
first, _ := json.Marshal(wantA[0])
|
||||
second, _ := json.Marshal(wantA[1])
|
||||
var paged []reg.Repository
|
||||
q := ListQuery{PageBytes: max(len(first), len(second))} // one repository a page
|
||||
for pages := 0; ; pages++ {
|
||||
p, err := b.v.List(ctx, ArtifactSeat, "mesh-anyone", q)
|
||||
if err != nil || p.Total != 2 || p.Capped != (p.Next != "") || pages > 2 {
|
||||
t.Fatalf("page %d: %+v, %v", pages, p, err)
|
||||
}
|
||||
paged = append(paged, p.Repositories...)
|
||||
if q.Next = p.Next; q.Next == "" {
|
||||
break
|
||||
}
|
||||
}
|
||||
if !reflect.DeepEqual(paged, wantA) {
|
||||
t.Errorf("the pages hold %+v, want %+v", paged, wantA)
|
||||
}
|
||||
if _, err := b.v.List(ctx, ArtifactSeat, "mesh-anyone", ListQuery{Next: "!"}); err == nil {
|
||||
t.Error("a cursor no page answered was taken")
|
||||
}
|
||||
// A manifest whose content cannot be read fails the answer: a listing without it would read as the store not holding it.
|
||||
delete(f.manifests, dg("m-old"))
|
||||
b.v.Files = &Files{Container: "mesh-registry", Run: f.run(t)} // what was read is kept: a fresh reader reads again
|
||||
if _, err := b.v.List(ctx, ImageSeat, "mesh-anyone", ""); err == nil || !strings.Contains(err.Error(), "cannot be read") {
|
||||
if _, err := b.v.List(ctx, ImageSeat, "mesh-anyone", ListQuery{}); err == nil || !strings.Contains(err.Error(), "cannot be read") {
|
||||
t.Errorf("an unreadable manifest: %v", err)
|
||||
}
|
||||
// The files' listing: a repository with slashes read, a cut or stray one refused.
|
||||
|
||||
@@ -207,16 +207,25 @@ func (o *OCI) layerSize(t PublishRequest, parts map[string][]byte) (int64, error
|
||||
return resp.ContentLength, nil
|
||||
}
|
||||
|
||||
// Held is the reference of name at version when the store holds digest under it.
|
||||
func (o *OCI) Held(name, version, digest string) (string, bool, error) {
|
||||
d, err := o.Digest(name, version)
|
||||
// Held is the reference of r's name at r's version when the store holds r's digest under it, compared like with like
|
||||
// (H-A-474): an image's by the manifest's digest the version's tag names, a bundle's or an archive's by the one layer
|
||||
// that manifest keeps (the blob Put stored and the reference names). When the store holds the version at another digest,
|
||||
// other names that digest of the same kind.
|
||||
func (o *OCI) Held(r PublishRequest) (string, string, error) {
|
||||
var d string
|
||||
var err error
|
||||
if r.Kind == KindImage || r.Kind == KindUpstream {
|
||||
d, err = o.Digest(r.Name, r.Version)
|
||||
} else {
|
||||
d, err = o.Blob(r.Name, r.Version)
|
||||
}
|
||||
if err != nil || d == "" {
|
||||
return "", false, err
|
||||
return "", "", err
|
||||
}
|
||||
if d != digest {
|
||||
return "", false, nil
|
||||
if d != r.Digest {
|
||||
return "", d, nil
|
||||
}
|
||||
return name + "@" + d, true, nil
|
||||
return r.Name + "@" + d, "", 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
|
||||
|
||||
@@ -3,13 +3,14 @@ package holder
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshbuilds"
|
||||
)
|
||||
|
||||
// Reserved is the holder's check of a publish against the builder's reservation (novox/hq design 47 §381, §478, §504): it asks the
|
||||
// seat mesh-builds' verb reserved with the publish's name and version, a package's name in its ecosystem, and accepts the publish
|
||||
// only when that version is the one reserved and an artifact of it has the publish's digest. It reads no catalog and a publish gains
|
||||
// only when that version is the one reserved and an artifact of it has the publish's digest or none recorded yet (A-490j). It reads no catalog and a publish gains
|
||||
// no field. It is the Builder the holder's Check asks.
|
||||
type Reserved struct {
|
||||
// Ask is the runtime's request and reply over the bus (mesh-sdk stdio.Ask).
|
||||
@@ -31,12 +32,20 @@ func (r Reserved) Recorded(name, version, digest string) (bool, string, error) {
|
||||
return false, "", fmt.Errorf("%s answered no reservation for %s@%s: %s", Address, name, version, raw)
|
||||
}
|
||||
if a.Version != version {
|
||||
return false, fmt.Sprintf("%s reserved %s for %s, not %s", a.Module, a.Version, name, version), nil
|
||||
return false, fmt.Sprintf("%s has no reservation of version %s of %s (it reserved %s)", a.Module, version, name, a.Version), nil
|
||||
}
|
||||
var reserved []string
|
||||
for _, art := range a.Artifacts {
|
||||
if art.Digest == digest {
|
||||
// An artifact whose build has not recorded its digest yet takes the publish: the builder records a build's digests only when
|
||||
// the build ends, while the build module publishes during it with the reserved version (design 47 G §369, A-415, A-490j,
|
||||
// H-A-387). A digest the build then records that differs from what the holder holds fails the build.
|
||||
if art.Digest == digest || art.Digest == "" {
|
||||
return true, "", nil
|
||||
}
|
||||
reserved = append(reserved, art.Digest)
|
||||
}
|
||||
return false, fmt.Sprintf("%s reserved %s@%s for another digest than %s", a.Module, name, version, digest), nil
|
||||
if len(reserved) == 0 {
|
||||
return false, fmt.Sprintf("%s reserved %s@%s with no artifact, so no digest takes %s", a.Module, name, version, digest), nil
|
||||
}
|
||||
return false, fmt.Sprintf("%s reserved %s@%s for digest %s, not %s", a.Module, name, version, strings.Join(reserved, ", "), digest), nil
|
||||
}
|
||||
|
||||
@@ -19,8 +19,10 @@ func reservedChecksAPublishAgainstTheReservation(t *testing.T) {
|
||||
}{
|
||||
{name: "the reserved version and digest", answer: `{"module":"mesh-image","version":"0.1.7","artifacts":[{"artifact":"mesh-image:image","digest":"sha256:aa"}]}`, ok: true},
|
||||
{name: "another artifact of it carries the digest", answer: `{"module":"mesh-image","version":"0.1.7","artifacts":[{"artifact":"mesh-image:aux"},{"artifact":"mesh-image:image","digest":"sha256:aa"}]}`, ok: true},
|
||||
{name: "another digest", answer: `{"module":"mesh-image","version":"0.1.7","artifacts":[{"artifact":"mesh-image:image","digest":"sha256:bb"}]}`, why: "another digest than sha256:aa"},
|
||||
{name: "another version reserved", answer: `{"module":"mesh-image","version":"0.1.8","artifacts":[]}`, why: "reserved 0.1.8 for registry/mesh-image, not 0.1.7"},
|
||||
{name: "no digest recorded yet", answer: `{"module":"mesh-image","version":"0.1.7","artifacts":[{"artifact":"mesh-image:image"}]}`, ok: true},
|
||||
{name: "another digest", answer: `{"module":"mesh-image","version":"0.1.7","artifacts":[{"artifact":"mesh-image:image","digest":"sha256:bb"}]}`, why: "reserved registry/mesh-image@0.1.7 for digest sha256:bb, not sha256:aa"},
|
||||
{name: "no artifact reserved", answer: `{"module":"mesh-image","version":"0.1.7","artifacts":[]}`, why: "with no artifact, so no digest takes sha256:aa"},
|
||||
{name: "another version reserved", answer: `{"module":"mesh-image","version":"0.1.8","artifacts":[]}`, why: "no reservation of version 0.1.7 of registry/mesh-image (it reserved 0.1.8)"},
|
||||
{name: "the builder refused the name", err: errors.New("no entry carries registry/mesh-image"), why: "no entry carries registry/mesh-image"},
|
||||
{name: "an answer that is none", answer: `{"error":"x"}`, why: "answered no reservation"},
|
||||
} {
|
||||
|
||||
@@ -19,9 +19,6 @@ const (
|
||||
OldSeat = "mesh-artifact-store"
|
||||
)
|
||||
|
||||
// SweepSeat is the seat of the artifact sweep, whose holder alone removes an untagged version for retention.
|
||||
const SweepSeat = "mesh-artifact-sweep"
|
||||
|
||||
// The verbs of the seats (design 26 *A seat's definition*; design 51 *One contract*).
|
||||
const (
|
||||
VerbPublish = "publish"
|
||||
|
||||
@@ -140,9 +140,11 @@ type Store interface {
|
||||
Has(name, digest string) (bool, error)
|
||||
// Put stores a publish's parts under its name and version and answers the reference's path (SeatReference).
|
||||
Put(r PublishRequest, parts map[string][]byte) (reference string, err error)
|
||||
// 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)
|
||||
// Held is the reference's path of a publish's name at its version when the store already holds its digest under it: a
|
||||
// rebuild of a held fingerprint answers it and stores nothing. The digest is compared like with like (H-A-474): an
|
||||
// image's manifest digest, a bundle's or an archive's blob digest. When the store holds the version at another digest,
|
||||
// other names that digest and reference is empty; neither is set when the store holds no such version.
|
||||
Held(r PublishRequest) (reference, other string, 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)
|
||||
@@ -266,15 +268,19 @@ 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 != "" {
|
||||
var ref string
|
||||
var ok bool
|
||||
var ref, other string
|
||||
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)
|
||||
ref, other, err = h.Store.Held(r)
|
||||
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 {
|
||||
} else if other != "" {
|
||||
// A second publish of a held version with another digest is refused before the reservation is asked, as the forge
|
||||
// holds the first (A-490j, H-A-393): while the reserved digest is still empty it would pass, and the store's Put
|
||||
// would move the version's tag.
|
||||
return h.refusePublish(seat, r, fmt.Sprintf("the store holds %s@%s at digest %s, not %s", r.Name, r.Version, other, r.Digest))
|
||||
} else if 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), ""))
|
||||
})
|
||||
@@ -298,6 +304,16 @@ func (h *Holder) Publish(seat, caller string, r PublishRequest) (PublishAnswer,
|
||||
return answerOf(got), nil
|
||||
}
|
||||
|
||||
// refusePublish ends a publish refused before anything is stored: its finding raised, refused said on the seat, the reason
|
||||
// answered.
|
||||
func (h *Holder) refusePublish(seat string, r PublishRequest, 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)
|
||||
}
|
||||
|
||||
// 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,
|
||||
@@ -311,13 +327,7 @@ func (h *Holder) tagHeld(seat string, r PublishRequest) (PublishAnswer, error) {
|
||||
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)
|
||||
}
|
||||
refuse := func(reason string) (PublishAnswer, error) { return h.refusePublish(seat, r, 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))
|
||||
}
|
||||
@@ -397,6 +407,38 @@ func (h *Holder) DropIdle() ([]string, error) { return h.receiver().DropIdle() }
|
||||
// already gone. It answers whether it dropped the transfer.
|
||||
func (h *Holder) Expired(id string) (bool, error) { return h.receiver().Expired(id) }
|
||||
|
||||
// Repeat is `publish` given again with an ask id the holder already took and whose first act ended (design 47 H2 §775;
|
||||
// H-A-381): it never publishes twice and keeps no answer, but answers the act's state as it stands, read afresh, which is
|
||||
// what the act answers when done: a version the store holds ended, an open transfer the parts it lacks; a transfer that
|
||||
// is gone, with nothing held, says the act never happened, and is refused naming it.
|
||||
func (h *Holder) Repeat(seat, caller string, r PublishRequest) (PublishAnswer, error) {
|
||||
if err := checkPublish(seat, r); err != nil {
|
||||
return PublishAnswer{}, err
|
||||
}
|
||||
if r.Version != "" {
|
||||
var ref string
|
||||
err := h.timed("the store's read of what it holds (the program's registry)", func() (err error) {
|
||||
ref, _, err = h.Store.Held(r)
|
||||
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 ref != "" {
|
||||
return PublishAnswer{Transfer: r.Transfer, Ended: true}, nil
|
||||
}
|
||||
}
|
||||
t, found, err := h.Transfers.Get(r.Transfer)
|
||||
switch {
|
||||
case err != nil:
|
||||
return PublishAnswer{}, fmt.Errorf("publish: the transfer %s of a repeated ask cannot be read: %w", r.Transfer, err)
|
||||
case !found:
|
||||
return PublishAnswer{}, fmt.Errorf("publish: refused: the ask was taken before, and transfer %s is gone with nothing of %s held: the act did not happen", r.Transfer, r.Name)
|
||||
case t.Opener != caller:
|
||||
return PublishAnswer{}, fmt.Errorf("publish: refused for %s: transfer %s was opened by another caller", caller, r.Transfer)
|
||||
}
|
||||
return PublishAnswer{Transfer: t.ID, Missing: t.Lacks()}, nil
|
||||
}
|
||||
|
||||
func checkPublish(seat string, r PublishRequest) error {
|
||||
switch {
|
||||
case !slices.Contains(Seats, seat):
|
||||
|
||||
@@ -88,9 +88,6 @@ type Verbs struct {
|
||||
Mirrors Mirrors
|
||||
// Files are the store's own files, which list reads (ADR 0632 §3).
|
||||
Files StoreFiles
|
||||
// SweepHolder is the module holding the seat `mesh-artifact-sweep`, as the seat's holders say it now (design 47 G §7, §428);
|
||||
// a removal for retention is that module's alone.
|
||||
SweepHolder func() (string, error)
|
||||
}
|
||||
|
||||
// VersionsAnswer is `versions`: the versions the seat holds of name.
|
||||
@@ -150,7 +147,7 @@ type Removal struct {
|
||||
const ReasonSharedDigest = reg.RemoveAnswerReasonSharedDigest
|
||||
|
||||
// Remove is `remove`: the builder removes a tagged version; the artifact sweep's holder an untagged bundle, archive or
|
||||
// copied base with the reason retention, which a hold keeps. Every removal says `removed` with its reason.
|
||||
// copied base with the reason retention, which a hold keeps, its caller not judged (design 47 H2 §813 (7)). Every removal says `removed` with its reason.
|
||||
func (v *Verbs) Remove(seat, caller string, r RemoveRequest) (Removal, error) {
|
||||
if strings.TrimSpace(r.Name) == "" || strings.TrimSpace(r.Reason) == "" {
|
||||
return Removal{}, errors.New("remove: a name and a reason are required")
|
||||
@@ -158,13 +155,8 @@ func (v *Verbs) Remove(seat, caller string, r RemoveRequest) (Removal, error) {
|
||||
ev := Event{Name: r.Name, Version: r.Version, Kind: r.Kind, Digest: r.Digest, Holder: Self, Reason: r.Reason, By: caller}
|
||||
switch {
|
||||
case r.Reason == ReasonRetention:
|
||||
sweep, err := v.SweepHolder()
|
||||
if err != nil {
|
||||
return Removal{}, fmt.Errorf("remove: the holder of %s cannot be read, so nothing is removed for retention: %w", SweepSeat, err)
|
||||
}
|
||||
if CallerModule(caller) != sweep {
|
||||
return Removal{}, fmt.Errorf("remove: refused for %s: a removal for retention is the artifact sweep's alone", caller)
|
||||
}
|
||||
// The removal for retention the holder of mesh-artifact-sweep asks; its caller is not judged, as every account may call
|
||||
// every verb (design 47 H2 §813 (7)).
|
||||
if r.Digest == "" {
|
||||
return Removal{}, errors.New("remove: a removal for retention names the digest")
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
{
|
||||
"module": "distribution",
|
||||
"version": "0.1",
|
||||
"class": "core",
|
||||
"provides": [
|
||||
{
|
||||
"name": "artifact-store",
|
||||
@@ -249,14 +250,44 @@
|
||||
}
|
||||
],
|
||||
"state": [
|
||||
"transfers",
|
||||
{
|
||||
"name": "transfers",
|
||||
"max-mib": 8
|
||||
},
|
||||
{
|
||||
"name": "idle",
|
||||
"key-ttl": true
|
||||
"key-ttl": true,
|
||||
"max-mib": 8
|
||||
},
|
||||
"holds",
|
||||
"windows",
|
||||
"mirrors",
|
||||
"findings"
|
||||
]
|
||||
{
|
||||
"name": "holds",
|
||||
"max-mib": 8
|
||||
},
|
||||
{
|
||||
"name": "windows",
|
||||
"max-mib": 8
|
||||
},
|
||||
{
|
||||
"name": "mirrors",
|
||||
"max-mib": 8
|
||||
},
|
||||
{
|
||||
"name": "findings",
|
||||
"max-mib": 8
|
||||
},
|
||||
{
|
||||
"name": "taken",
|
||||
"key-ttl": true,
|
||||
"max-mib": 8
|
||||
}
|
||||
],
|
||||
"config": {
|
||||
"ask-taken-bound": {
|
||||
"type": "string",
|
||||
"description": "How long the registry keeps the id of a publish ask it took, as a duration such as 10m (design 47 H2 §775): a publish given again with that id within it acts not again but answers the transfer as it stands. The largest caller's bound, the pipeline runner's ask-unanswered-bound, by default.",
|
||||
"trusted": false,
|
||||
"runtime": true,
|
||||
"default": "10m"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
{
|
||||
"module": "dmenu",
|
||||
"version": "0.1",
|
||||
"class": "leaf",
|
||||
"capabilities": [
|
||||
"package-manager"
|
||||
],
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
{
|
||||
"module": "dnsmasq",
|
||||
"version": "0.1",
|
||||
"class": "core",
|
||||
"upgrade": {
|
||||
"policy": "record",
|
||||
"why": "the mesh's resolver: a build that breaks it can stop a machine resolving the bus, and then neither the gate's rollback nor a push reaches it (hq ADR 0236, issue 260)"
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
{
|
||||
"module": "docker-compose",
|
||||
"version": "0.1",
|
||||
"class": "leaf",
|
||||
"capabilities": [
|
||||
"package-manager"
|
||||
],
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
{
|
||||
"module": "docker",
|
||||
"version": "0.1",
|
||||
"class": "core",
|
||||
"capabilities": [
|
||||
"package-manager",
|
||||
"service-manager",
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
{
|
||||
"module": "dunst",
|
||||
"version": "0.1",
|
||||
"class": "core",
|
||||
"moved": true,
|
||||
"capabilities": [
|
||||
"package-manager"
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
{
|
||||
"module": "fail2ban",
|
||||
"version": "0.1",
|
||||
"class": "core",
|
||||
"capabilities": [
|
||||
"firewall"
|
||||
],
|
||||
|
||||
@@ -2,5 +2,5 @@
|
||||
# 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 (the second single-module proof, 2026-11-17).
|
||||
# without touching this file (design 47 H1, proof 1 (second run): delivered by its pipeline, 2026-11-21).
|
||||
feh --no-fehbg --bg-fill "$HOME/.local/share/feh/wallpapers/default.jpg"
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
{
|
||||
"module": "feh",
|
||||
"version": "0.1",
|
||||
"class": "leaf",
|
||||
"moved": true,
|
||||
"capabilities": [
|
||||
"package-manager"
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
{
|
||||
"module": "flatpak",
|
||||
"version": "0.1",
|
||||
"class": "leaf",
|
||||
"capabilities": [
|
||||
"package-manager"
|
||||
],
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
{
|
||||
"module": "fonts",
|
||||
"version": "0.1",
|
||||
"class": "leaf",
|
||||
"capabilities": [
|
||||
"package-manager"
|
||||
],
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
{
|
||||
"module": "forticlient",
|
||||
"version": "0.1",
|
||||
"class": "leaf",
|
||||
"upgrade": {
|
||||
"policy": "record",
|
||||
"why": "its adapter routes the company's domains on the machine a person works on: a build that breaks it cuts the person off from work names until a person pushes the next (hq ADR 0236, ADR 0247)"
|
||||
|
||||
@@ -100,7 +100,8 @@ search that reaches the call's deadline stops there and answers what it found, w
|
||||
|
||||
It reaches the forge through the same HTTP client as the package-registry bundle (`internal/forgehttp`), as the
|
||||
tools' own account (novox/hq ADR 0365 §2, issue 469): the token the TypeScript bundle mints for `mesh-tools` and
|
||||
keeps (`MESH_GITEA_TOKEN_FILE`), sent only when its scopes line names that account, never the admin login. One tool
|
||||
keeps in the vault as the module's own (`module/gitea/mesh-tools`, design 47 H2 §808), read through the vault seat's
|
||||
`get` at start and at each `secret-changed` naming it, never the admin login. One tool
|
||||
call takes at most 50 seconds.
|
||||
`go test ./cmd/forge` runs it against a fake forge: the files of one commit created, updated and deleted on
|
||||
a new branch from its base; an existing branch committed on; the default branch, a protected branch and
|
||||
@@ -247,9 +248,10 @@ version already gone counts as deleted.
|
||||
|
||||
Over the forge's HTTP interface on the machine (`MESH_GITEA_URL`), never as the admin account (design 51: each
|
||||
holder keeps its write side as an own secret beside a read-only tool token). It **reads** as the tools' account,
|
||||
with the token the TypeScript bundle keeps for it (`MESH_GITEA_TOKEN_FILE`, scopes including `read:package`), and
|
||||
**writes** (a publish, a remove, retention's deletion) as the packages account `mesh-packages`
|
||||
(`MESH_GITEA_PACKAGES_USER`), with its own token (`MESH_GITEA_PACKAGES_TOKEN_FILE`, the one scope `write:package`).
|
||||
with the token the TypeScript bundle keeps for it in the vault (`module/gitea/mesh-tools`, scopes including
|
||||
`read:package`), and **writes** (a publish, a remove, retention's deletion) as the packages account `mesh-packages`
|
||||
(`MESH_GITEA_PACKAGES_USER`), with its own token (`module/gitea/mesh-packages`, the one scope `write:package`), both
|
||||
read through the vault seat's `get` at start and at each `secret-changed` (design 47 H2 §808).
|
||||
That account is plain, a member of the team `mesh-packages` that writes the organisation's packages and nothing else;
|
||||
the watcher makes it with the administrator's login and mints and keeps its token at its start (`packages-account.ts`).
|
||||
No timer looks at it after that: when the forge refuses a write (401) this bundle raises the finding
|
||||
|
||||
@@ -70,16 +70,18 @@ export class Calls {
|
||||
|
||||
/** A kept member of a call. */
|
||||
export const keptMember = (m: { repository: string; number: number; branch: string; head: string; state: CallMember["state"];
|
||||
commit?: string; why?: string }): CallMember => ({ repository: m.repository, number: m.number, branch: m.branch, head: m.head,
|
||||
state: m.state, ...(m.commit ? { commit: m.commit } : {}), ...(m.why ? { why: m.why } : {}) });
|
||||
commit?: string; why?: string; note?: string }): CallMember => ({ repository: m.repository, number: m.number, branch: m.branch,
|
||||
head: m.head, state: m.state, ...(m.commit ? { commit: m.commit } : {}), ...(m.why ? { why: m.why } : {}),
|
||||
...(m.note ? { note: m.note } : {}) });
|
||||
|
||||
/** Says a seat event on its subject, under an id the bus keeps once. */
|
||||
export type Say = (subject: string, body: unknown, id?: string) => Promise<void>;
|
||||
|
||||
/** The merge-called of a kept call (design 47 H §736, §767): its landed members, each with its base and merge commit, and its
|
||||
* refused ones; undefined for a call that landed no member, which says none. */
|
||||
* refused ones; undefined for a call that landed no member, which says none. A member merged plainly, carrying the note
|
||||
* no delivery (its repository has no watched branch), starts no delivery and is left out, by the kept note (H2 H-A-471). */
|
||||
export function mergeCalledOf(c: KeptCall): MergeCalledEvent | undefined {
|
||||
const landed = c.members.filter((m) => m.state === "merged" && m.commit);
|
||||
const landed = c.members.filter((m) => m.state === "merged" && m.commit && !m.note);
|
||||
if (landed.length === 0) return undefined;
|
||||
// A member judged and not merged (a stop mid-call) is in neither: its head is an untaken head (§740).
|
||||
const refused = c.members.filter((m) => m.state === "refused");
|
||||
|
||||
+43
-3
@@ -182,7 +182,19 @@ export const FORGE_REQUEST_TIMEOUT_MS = 20_000;
|
||||
/** The most a tool's answer carries of one file: the bus's message bound is 1 MiB and a file travels as text (issue 1577). */
|
||||
export const MAX_FILE_BYTES = 512 * 1024;
|
||||
|
||||
/** A directory of a listing's `files` the commit does not hold: the seat git answers it with the kind not-found, the error
|
||||
* naming the directory (seat.ts failure; novox/hq design 47 H H-A-232). */
|
||||
export class DirectoryNotInTree extends Error {
|
||||
constructor(message: string) {
|
||||
super(message);
|
||||
this.name = "DirectoryNotInTree";
|
||||
}
|
||||
}
|
||||
|
||||
/** One request to the forge, cut at the limit and failing loud naming the request. */
|
||||
/** What a forge's answer says when its store does not answer (design 47 H2 §776). */
|
||||
const STORE_UNAVAILABLE = /database|\bdb\b|store/i;
|
||||
|
||||
async function timedFetch(url: string, init: RequestInit, label: string, timeoutMs: number): Promise<Response> {
|
||||
try {
|
||||
return await fetch(url, { ...init, signal: init.signal ? AbortSignal.any([init.signal, AbortSignal.timeout(timeoutMs)]) : AbortSignal.timeout(timeoutMs) });
|
||||
@@ -267,6 +279,28 @@ export class GiteaClient {
|
||||
return this.request<T>(path, options);
|
||||
}
|
||||
|
||||
/**
|
||||
* The forge's own version read, unauthenticated (GET /api/v1/version): whether the forge answers at all, and whether
|
||||
* it names its store unavailable (design 47 H2 §776). What the seat git reads before it says a 401, a 502, a 503 or a
|
||||
* connection fault is the forge's refusal or the forge away; never throws.
|
||||
*/
|
||||
async forgeVersion(): Promise<{ ok: true; version: string } | { ok: false; words: string; store: boolean }> {
|
||||
const label = "GET /version";
|
||||
try {
|
||||
const res = await timedFetch(`${this.baseUrl}/api/v1/version`, { headers: { Accept: "application/json" } }, label, this.requestTimeoutMs);
|
||||
const text = await res.text();
|
||||
const store = STORE_UNAVAILABLE.test(text);
|
||||
if (!res.ok) return { ok: false, words: `the forge's version read answered ${res.status} ${text}`.trim(), store };
|
||||
let version = "";
|
||||
try { version = String(JSON.parse(text)?.version ?? ""); } catch { /* no version said */ }
|
||||
if (store) return { ok: false, words: `the forge's version read names its store unavailable: ${text}`, store };
|
||||
if (!version) return { ok: false, words: `the forge's version read named no version: ${text}`, store };
|
||||
return { ok: true, version };
|
||||
} catch (err) {
|
||||
return { ok: false, words: `the forge's version read: ${err instanceof Error ? err.message : String(err)}`, store: false };
|
||||
}
|
||||
}
|
||||
|
||||
// ---- Repositories ----
|
||||
|
||||
/** Every repository this token can see, one page. `/user/repos` is only what the token's own
|
||||
@@ -490,11 +524,17 @@ export class GiteaClient {
|
||||
}
|
||||
|
||||
/** One page of a repository's tags, newest first, by name: what the seat git's `tags` answers. */
|
||||
async tags(owner: string, repo: string, page: number, limit: number): Promise<string[]> {
|
||||
async tags(owner: string, repo: string, page: number, limit: number): Promise<{ tag: string; commit: string }[]> {
|
||||
if (!Number.isInteger(page) || page < 1) throw new Error(`${page} is not a page, from 1`);
|
||||
const list = await this.request<any[]>(`${repoPath(owner, repo)}/tags?page=${page}&limit=${limit}`);
|
||||
if (!Array.isArray(list)) throw new Error(`the forge answered no list of the tags of ${owner}/${repo}`);
|
||||
return list.map((t) => String(t?.name ?? "")).filter((n) => n !== "");
|
||||
// Each tag with the commit it names (design 47 H2 §796): a tag the forge names no commit for is no answer, never a
|
||||
// tag without its commit.
|
||||
return list.filter((t) => String(t?.name ?? "") !== "").map((t) => {
|
||||
const tag = String(t.name), commit = String(t?.commit?.sha ?? "");
|
||||
if (!/^[0-9a-f]{40}$/.test(commit)) throw new Error(`the forge named no commit for ${owner}/${repo}'s tag ${tag}`);
|
||||
return { tag, commit };
|
||||
});
|
||||
}
|
||||
|
||||
/** The commit a tag names, or null when the repository has no such tag (the forge's 404 only). */
|
||||
@@ -634,7 +674,7 @@ export class GiteaClient {
|
||||
// is whole (a cut one failed above), so a directory absent from it is absent at the commit.
|
||||
filesOf = {};
|
||||
for (const dir of files) {
|
||||
if (dir !== "" && dirOf.get(dir) === undefined) throw new Error(`the directory ${dir} is not in ${owner}/${repo} at ${sha}`);
|
||||
if (dir !== "" && dirOf.get(dir) === undefined) throw new DirectoryNotInTree(`the directory ${dir} is not in ${owner}/${repo} at ${sha}`);
|
||||
const prefix = dir === "" ? "" : `${dir}/`;
|
||||
const blobs: Record<string, string> = {};
|
||||
for (const e of entries) {
|
||||
|
||||
@@ -1,35 +1,54 @@
|
||||
package main
|
||||
|
||||
// The commit and the reads act as the tools' own account (novox/hq ADR 0365 §2, issue 469): the token the module
|
||||
// keeps for `mesh-tools`, never the admin login. That account is no repository's administrator, so the forge
|
||||
// keeps for `mesh-tools` in the vault (design 47 H2 §808), never the admin login. That account is no repository's administrator, so the forge
|
||||
// refuses it the protection rules (403, as gitea's reqAdmin does); the commit then judges an existing branch by the
|
||||
// protection the forge says it applies, and leaves a branch not made yet to the forge's own judgement.
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.novox.be/novox/mesh-sdk/go/seal"
|
||||
|
||||
"gitea/internal/forgehttp"
|
||||
)
|
||||
|
||||
const toolsToken = "tools-token-never-shown"
|
||||
|
||||
// asTools serves the fake forge to a bundle holding the tools' token: only that token is taken, the rules are
|
||||
// refused as to a non-administrator, and the branches named protected say so as the forge does.
|
||||
func asTools(t *testing.T, f *fakeForge, protected map[string]string, scopes string) (*Forge, *[]string) {
|
||||
dir := t.TempDir()
|
||||
file := filepath.Join(dir, "token")
|
||||
if err := os.WriteFile(file, []byte(toolsToken+"\n"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(file+".scopes", []byte(scopes+"\n"), 0o600); err != nil {
|
||||
// vaultHolding is the vault seat's get answering account's token sealed to the key it is asked with; "" holds none.
|
||||
func vaultHolding(t *testing.T, account, token string) *forgehttp.VaultToken {
|
||||
t.Helper()
|
||||
v, err := forgehttp.NewVaultToken(account, func(address string, args any) (json.RawMessage, error) {
|
||||
a := args.(map[string]any)
|
||||
if address != forgehttp.VaultGet || a["name"] != forgehttp.VaultName(account) {
|
||||
return nil, fmt.Errorf("asked %s for %v", address, a["name"])
|
||||
}
|
||||
if token == "" {
|
||||
return nil, fmt.Errorf("the vault holds no secret named %s", a["name"])
|
||||
}
|
||||
// The value the minter writes: the token and the scopes it was minted with (H-A-439).
|
||||
value, _ := json.Marshal(map[string]any{"token": token, "scopes": []string{"write:repository", "write:issue", "read:user", "read:package"}})
|
||||
box, err := seal.Seal(string(value), a["public_key"].(string))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return json.Marshal(map[string]any{"name": a["name"], "generation": 1, "sealed": box})
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
// asTools serves the fake forge to a bundle holding the token the vault hands: only the tools' token is taken, the
|
||||
// rules are refused as to a non-administrator, and the branches named protected say so as the forge does.
|
||||
func asTools(t *testing.T, f *fakeForge, protected map[string]string, held string) (*Forge, *[]string) {
|
||||
var seen []string
|
||||
const repo = "/api/v1/repos/acme/app"
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -52,12 +71,12 @@ func asTools(t *testing.T, f *fakeForge, protected map[string]string, scopes str
|
||||
f.handle(w, r)
|
||||
}))
|
||||
t.Cleanup(srv.Close)
|
||||
return &Forge{Client: forgehttp.Client{URL: srv.URL, TokenFile: file, Account: "mesh-tools"}}, &seen
|
||||
return &Forge{Client: forgehttp.Client{URL: srv.URL, Token: vaultHolding(t, "mesh-tools", held), Account: "mesh-tools"}}, &seen
|
||||
}
|
||||
|
||||
func TestTheToolsAccountCommitsOnANewBranchWithItsTokenThoughTheRulesAreNotItsToRead(t *testing.T) {
|
||||
f := newFake(t)
|
||||
forge, _ := asTools(t, f, nil, "read:user,write:issue,write:repository for mesh-tools")
|
||||
forge, _ := asTools(t, f, nil, toolsToken)
|
||||
got, err := commit(context.Background(), forge, ask(t, map[string]any{"branch": "fix/365",
|
||||
"files": filesArg(map[string]any{"path": "x", "content": "x"})}))
|
||||
if err != nil {
|
||||
@@ -75,7 +94,7 @@ func TestTheToolsAccountIsRefusedAProtectedBranchTheForgeSaysItGuardsAndTheDefau
|
||||
if rule != "" {
|
||||
protected[branch] = rule
|
||||
}
|
||||
forge, _ := asTools(t, f, protected, "read:user,write:issue,write:repository for mesh-tools")
|
||||
forge, _ := asTools(t, f, protected, toolsToken)
|
||||
_, err := commit(context.Background(), forge, ask(t, map[string]any{"branch": branch,
|
||||
"files": filesArg(map[string]any{"path": "x", "content": "x"})}))
|
||||
if err == nil || !strings.Contains(err.Error(), "refused") || !strings.Contains(err.Error(), "ADR 0266") {
|
||||
@@ -90,13 +109,13 @@ func TestTheToolsAccountIsRefusedAProtectedBranchTheForgeSaysItGuardsAndTheDefau
|
||||
}
|
||||
}
|
||||
|
||||
func TestATokenKeptForTheAdministratorIsNeverSent(t *testing.T) {
|
||||
func TestNoTokenIsSentWhileTheVaultHoldsNoneForTheAccount(t *testing.T) {
|
||||
f := newFake(t)
|
||||
// What the module kept before ADR 0365: the admin's token, its scopes line naming no account.
|
||||
forge, seen := asTools(t, f, nil, "read:user,write:issue,write:repository")
|
||||
// Design 47 H2 §808: the token is read through the vault seat's get; while the module has minted none, nothing goes.
|
||||
forge, seen := asTools(t, f, nil, "")
|
||||
_, err := commit(context.Background(), forge, ask(t, map[string]any{"branch": "fix/365",
|
||||
"files": filesArg(map[string]any{"path": "x", "content": "x"})}))
|
||||
if err == nil || !strings.Contains(err.Error(), "not mesh-tools's") {
|
||||
if err == nil || !strings.Contains(err.Error(), "not kept yet") {
|
||||
t.Fatalf("%v", err)
|
||||
}
|
||||
if len(*seen) != 0 {
|
||||
@@ -104,9 +123,8 @@ func TestATokenKeptForTheAdministratorIsNeverSent(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestTheBundleHandedATokenFileNeedsNoAdminLogin(t *testing.T) {
|
||||
func TestTheBundleReadsItsTokenFromTheVaultAndNeedsNoAdminLogin(t *testing.T) {
|
||||
t.Setenv("MESH_GITEA_URL", "http://127.0.0.1:3000")
|
||||
t.Setenv("MESH_GITEA_TOKEN_FILE", "/state/token")
|
||||
t.Setenv("MESH_GITEA_TOOLS_USER", "")
|
||||
t.Setenv("MESH_GITEA_ADMIN_USER", "")
|
||||
t.Setenv("MESH_GITEA_ADMIN_PASSWORD_FILE", "")
|
||||
@@ -114,7 +132,7 @@ func TestTheBundleHandedATokenFileNeedsNoAdminLogin(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if f.TokenFile != "/state/token" || f.Account != "mesh-tools" || f.User != "" || f.PasswordFile != "" {
|
||||
if f.Token == nil || f.Token.Account != "mesh-tools" || f.Account != "mesh-tools" || f.User != "" || f.PasswordFile != "" {
|
||||
t.Fatalf("%+v", f.Client)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,7 +21,10 @@ type Forge struct {
|
||||
|
||||
// ForgeFromEnv is the forge as the manifest hands it to this bundle.
|
||||
func ForgeFromEnv() (*Forge, error) {
|
||||
c, missing := forgehttp.FromEnv()
|
||||
c, missing, err := forgehttp.ToolsFromEnv()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(missing) > 0 {
|
||||
return nil, fmt.Errorf("the forge cannot be reached: %s not set by the manifest", strings.Join(missing, ", "))
|
||||
}
|
||||
|
||||
@@ -691,8 +691,8 @@ func TestToolsAreTheOnesTheReadmeNamesAndTheManifestBuilds(t *testing.T) {
|
||||
// The tools' own account, never the admin login (novox/hq ADR 0365 §2); the package registry's bundle also writes
|
||||
// as the packages account (design 51: the write side an own secret beside a read-only tool token).
|
||||
wants := map[string][]string{
|
||||
"cmd/forge": {"MESH_GITEA_URL", "MESH_GITEA_TOOLS_USER", "MESH_GITEA_TOKEN_FILE"},
|
||||
"cmd/package-registry": {"MESH_GITEA_URL", "MESH_GITEA_TOOLS_USER", "MESH_GITEA_TOKEN_FILE", "MESH_GITEA_PACKAGES_USER", "MESH_GITEA_PACKAGES_TOKEN_FILE"},
|
||||
"cmd/forge": {"MESH_GITEA_URL", "MESH_GITEA_TOOLS_USER"},
|
||||
"cmd/package-registry": {"MESH_GITEA_URL", "MESH_GITEA_TOOLS_USER", "MESH_GITEA_PACKAGES_USER"},
|
||||
}
|
||||
for _, a := range m.Build.Artifacts {
|
||||
want, ok := wants[a.From]
|
||||
@@ -713,6 +713,12 @@ func TestToolsAreTheOnesTheReadmeNamesAndTheManifestBuilds(t *testing.T) {
|
||||
t.Errorf("%s is given %s: it acts as an account of its own, not the administrator", a.From, k)
|
||||
}
|
||||
}
|
||||
// Design 47 H2 §808, H-A-422 (1): the token is read through the vault seat's get, never from a file.
|
||||
for _, k := range []string{"MESH_GITEA_TOKEN_FILE", "MESH_GITEA_PACKAGES_TOKEN_FILE"} {
|
||||
if a.Env[k] != "" {
|
||||
t.Errorf("%s is given %s: its token is read from the vault", a.From, k)
|
||||
}
|
||||
}
|
||||
}
|
||||
for from := range wants {
|
||||
t.Errorf("the manifest builds no %s bundle", from)
|
||||
|
||||
@@ -14,6 +14,8 @@ import (
|
||||
"strings"
|
||||
|
||||
stdio "git.novox.be/novox/mesh-sdk/go"
|
||||
|
||||
"gitea/internal/forgehttp"
|
||||
)
|
||||
|
||||
// ToolNames are the tools this bundle serves. The module's manifest lists no `tools` (see the package-registry
|
||||
@@ -21,6 +23,16 @@ import (
|
||||
var ToolNames = []string{"gitea_commit", "gitea_commits", "gitea_compare", "gitea_pull_reviews", "gitea_list_dir", "gitea_search_code"}
|
||||
|
||||
func main() {
|
||||
// The tools' account's token, read from the vault at start and at each secret-changed naming it (design 47 H2 §808).
|
||||
go func() {
|
||||
account := os.Getenv("MESH_GITEA_TOOLS_USER")
|
||||
if account == "" {
|
||||
account = forgehttp.ToolsAccount
|
||||
}
|
||||
if err := forgehttp.Follow(func(f string, a ...any) { fmt.Fprintf(os.Stderr, f+"\n", a...) }, account); err != nil {
|
||||
fmt.Fprintln(os.Stderr, "the vault's changes of the forge token are not heard:", err)
|
||||
}
|
||||
}()
|
||||
if err := stdio.Serve("", Tools(ForgeFromEnv)); err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(1)
|
||||
|
||||
@@ -78,8 +78,14 @@ func (f *Forge) As(typ string) *Forge {
|
||||
|
||||
// ForgeFromEnv is the forge as the manifest hands it to this bundle.
|
||||
func ForgeFromEnv() (*Forge, error) {
|
||||
c, missing := forgehttp.FromEnv()
|
||||
w, writeMissing := forgehttp.WriterFromEnv()
|
||||
c, missing, err := forgehttp.ToolsFromEnv()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
w, writeMissing, err := forgehttp.WriterFromEnv()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, m := range writeMissing {
|
||||
if m != "MESH_GITEA_URL" {
|
||||
missing = append(missing, m)
|
||||
@@ -168,7 +174,7 @@ func (f *Forge) DistTags(ctx context.Context, name string) (map[string]string, e
|
||||
// write is the registry's write side, refused when the manifest handed none.
|
||||
func (f *Forge) write() (*forgehttp.Client, error) {
|
||||
if f.Writer == nil {
|
||||
return nil, errors.New("the package registry's write side is not handed to this bundle (MESH_GITEA_PACKAGES_TOKEN_FILE); nothing was written")
|
||||
return nil, errors.New("the package registry's write side is not handed to this bundle (the packages account's token); nothing was written")
|
||||
}
|
||||
return f.Writer, nil
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@ import (
|
||||
stdio "git.novox.be/novox/mesh-sdk/go"
|
||||
"git.novox.be/novox/mesh-sdk/go/views"
|
||||
|
||||
"gitea/internal/forgehttp"
|
||||
"gitea/internal/packageseats"
|
||||
)
|
||||
|
||||
@@ -49,6 +50,19 @@ func main() {
|
||||
os.Exit(1)
|
||||
}
|
||||
}()
|
||||
// Both accounts' tokens, read from the vault at start and at each secret-changed naming one (design 47 H2 §808).
|
||||
go func() {
|
||||
tools, packages := os.Getenv("MESH_GITEA_TOOLS_USER"), os.Getenv("MESH_GITEA_PACKAGES_USER")
|
||||
if tools == "" {
|
||||
tools = forgehttp.ToolsAccount
|
||||
}
|
||||
if packages == "" {
|
||||
packages = forgehttp.PackagesAccount
|
||||
}
|
||||
if err := forgehttp.Follow(func(f string, a ...any) { fmt.Fprintf(os.Stderr, f+"\n", a...) }, tools, packages); err != nil {
|
||||
fmt.Fprintln(os.Stderr, "the vault's changes of the forge tokens are not heard:", err)
|
||||
}
|
||||
}()
|
||||
forge := func() (*Forge, error) { return ForgeFromEnv() }
|
||||
holder := func() (*packageseats.Holder, error) { return busHolder(forge) }
|
||||
go followIdle(holder)
|
||||
|
||||
@@ -480,22 +480,24 @@ func TestToolsAreTheOnesTheReadmeNamesAndSayWhatTheyDo(t *testing.T) {
|
||||
Objects any `json:"objects"`
|
||||
State any `json:"state"`
|
||||
}{m.Objects, m.State})
|
||||
for _, want := range []string{`"name":"incoming"`, `"transfers"`, `"key-ttl":true,"name":"idle"`} {
|
||||
// Each with its cap (design 47 H2 §812, H-A-411 (2)).
|
||||
for _, want := range []string{`"name":"incoming"`, `"name":"transfers"`, `"key-ttl":true,"max-mib":8,"name":"idle"`} {
|
||||
if !strings.Contains(string(req), want) {
|
||||
t.Errorf("the manifest does not keep %s in its old sections (%s)", want, req)
|
||||
}
|
||||
}
|
||||
// findings is one local name in one form (novox/hq ADR 0595 §11): the old section state, which the TypeScript bundle
|
||||
// announces readable for the condition keeper (kept.ts), never also under requires.
|
||||
if strings.Contains(string(req), `"name":"findings"`) {
|
||||
t.Errorf("findings is required as well as kept in state: %s", req)
|
||||
if required, _ := json.Marshal(m.Requires); strings.Contains(string(required), `"name":"findings"`) {
|
||||
t.Errorf("findings is required as well as kept in state: %s", required)
|
||||
}
|
||||
// transfers keeps for good: a lifetime of its own would drop a transfer's key and leave its chunks in incoming.
|
||||
if strings.Contains(string(req), `"name":"transfers","options"`) {
|
||||
t.Errorf("transfers has options (a lifetime): %s", req)
|
||||
}
|
||||
// The retention asks the builder's versions; the forge's other bundles ask the catalog half and record a re-read.
|
||||
if strings.Join(m.Invokes, ",") != "seat:mesh-catalog.revision,seat:mesh-catalog.registrations,seat:mesh-hand-acts.hand-act,seat:mesh-builds.reserved" {
|
||||
// The forge module's tokens through the vault seat: own-put by the minter, get by every process (design 47 H2 §808).
|
||||
if strings.Join(m.Invokes, ",") != "seat:mesh-catalog.revision,seat:mesh-catalog.registrations,seat:mesh-hand-acts.hand-act,seat:mesh-builds.reserved,seat:mesh-vault.own-put,seat:mesh-vault.get" {
|
||||
t.Errorf("the manifest invokes %v", m.Invokes)
|
||||
}
|
||||
if len(m.Tools) != 0 {
|
||||
@@ -505,7 +507,7 @@ func TestToolsAreTheOnesTheReadmeNamesAndSayWhatTheyDo(t *testing.T) {
|
||||
for _, a := range m.Build.Artifacts {
|
||||
if a.From == "cmd/package-registry" {
|
||||
found = true
|
||||
for _, k := range []string{"MESH_GITEA_URL", "MESH_GITEA_TOKEN_FILE", "MESH_GITEA_PACKAGES_TOKEN_FILE", "MESH_NPM_OWNER"} {
|
||||
for _, k := range []string{"MESH_GITEA_URL", "MESH_NPM_OWNER"} {
|
||||
if a.Env[k] == "" {
|
||||
t.Errorf("the bundle is not given %s", k)
|
||||
}
|
||||
|
||||
@@ -9,17 +9,17 @@ import (
|
||||
"encoding/base64"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
keyparts "git.novox.be/novox/mesh-sdk/go/keys"
|
||||
"git.novox.be/novox/mesh-sdk/go/seal"
|
||||
"git.novox.be/novox/mesh-sdk/go/views"
|
||||
"gitea/internal/forgehttp"
|
||||
|
||||
@@ -116,15 +116,10 @@ func TestThePackageSeatsStoreIntoTheForge(t *testing.T) {
|
||||
g := ®istry{uploaded: map[string][]byte{}, held: map[string][]byte{}}
|
||||
srv := httptest.NewServer(http.HandlerFunc(g.handle))
|
||||
defer srv.Close()
|
||||
dir := t.TempDir()
|
||||
kept := func(name, token, line string) string {
|
||||
file := filepath.Join(dir, name)
|
||||
_ = os.WriteFile(file, []byte(token+"\n"), 0o600)
|
||||
_ = os.WriteFile(file+".scopes", []byte(line+"\n"), 0o600)
|
||||
return file
|
||||
}
|
||||
reader := forgehttp.Client{URL: srv.URL, TokenFile: kept("token", "reader", "read:package,read:user for mesh-tools"), Account: "mesh-tools"}
|
||||
writer := forgehttp.Client{URL: srv.URL, TokenFile: kept("packages-token", "writer", "write:package for mesh-packages"), Account: "mesh-packages"}
|
||||
vault := map[string]string{"mesh-tools": "reader", "mesh-packages": "writer"}
|
||||
writerToken := vaultHolding(t, "mesh-packages", vault)
|
||||
reader := forgehttp.Client{URL: srv.URL, Token: vaultHolding(t, "mesh-tools", vault), Account: "mesh-tools"}
|
||||
writer := forgehttp.Client{URL: srv.URL, Token: writerToken, Account: "mesh-packages"}
|
||||
forge := &Forge{Client: reader, Writer: &writer, Owner: "acme"}
|
||||
if _, err := (seatStore{forge: func() (*Forge, error) { return &Forge{Client: reader, Owner: "acme"}, nil }}).Put(packageseats.GoSeat, packageseats.Publish{}, nil); err == nil ||
|
||||
!strings.Contains(err.Error(), "write side") {
|
||||
@@ -138,12 +133,22 @@ func TestThePackageSeatsStoreIntoTheForge(t *testing.T) {
|
||||
if k := views.FindingKey("seat", keyparts.EscapeName(packageseats.Self)+".write", PackagesTokenRefused); k != PackagesTokenRefusedKey {
|
||||
t.Errorf("the refusal's key is %s, the watcher watches %s", k, PackagesTokenRefusedKey)
|
||||
}
|
||||
_ = os.WriteFile(writer.TokenFile, []byte("stale\n"), 0o600)
|
||||
vault["mesh-packages"] = "stale"
|
||||
if read, err := writerToken.Changed([]byte(`{"name":"module/gitea/mesh-packages","generation":2}`)); !read || err != nil {
|
||||
t.Fatalf("the secret-changed of the packages token was not read again: %v %v", read, err)
|
||||
}
|
||||
if _, err := s.Put(packageseats.GoSeat, packageseats.Publish{Version: "0.1.15"}, moduleZip(t, "git.example/acme/sdk@v0.1.15")); err == nil ||
|
||||
len(refused) != 1 || !refused[0] {
|
||||
t.Errorf("a write the forge refused with 401 raised no finding: %v %v", err, refused)
|
||||
}
|
||||
_ = os.WriteFile(writer.TokenFile, []byte("writer\n"), 0o600)
|
||||
vault["mesh-packages"] = "writer"
|
||||
// Design 47 H2 §808: a secret-changed naming another secret reads nothing; the one naming this token reads it again.
|
||||
if read, _ := writerToken.Changed([]byte(`{"name":"module/gitea/mesh-tools","generation":3}`)); read {
|
||||
t.Error("a secret-changed of the tools' token read the packages token again")
|
||||
}
|
||||
if read, err := writerToken.Changed([]byte(`{"name":"module/gitea/mesh-packages","generation":3}`)); !read || err != nil {
|
||||
t.Fatalf("the secret-changed of the packages token was not read again: %v %v", read, err)
|
||||
}
|
||||
|
||||
tgz := tarball(t, `{"name":"@acme/contracts","version":"0.1.15-feat-x.2","main":"index.js"}`)
|
||||
p := packageseats.Publish{Name: "@acme/contracts", Version: "0.1.15-feat-x.2", Tag: "feat-x", Kind: packageseats.KindPackage, Digest: "sha256:" + sha(tgz)}
|
||||
@@ -197,3 +202,30 @@ func TestThePackageSeatsStoreIntoTheForge(t *testing.T) {
|
||||
t.Errorf("a write the forge took after the refusal did not clear it: %v", refused)
|
||||
}
|
||||
}
|
||||
|
||||
// vaultHolding is the vault seat's get answering account's token from held, sealed to the key it is asked with; an
|
||||
// account held has no entry is a secret the vault holds none of.
|
||||
func vaultHolding(t *testing.T, account string, held map[string]string) *forgehttp.VaultToken {
|
||||
t.Helper()
|
||||
v, err := forgehttp.NewVaultToken(account, func(address string, args any) (json.RawMessage, error) {
|
||||
a := args.(map[string]any)
|
||||
if address != forgehttp.VaultGet || a["name"] != forgehttp.VaultName(account) {
|
||||
return nil, fmt.Errorf("asked %s for %v", address, a["name"])
|
||||
}
|
||||
token, ok := held[account]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("the vault holds no secret named %s", a["name"])
|
||||
}
|
||||
// The value the minter writes: the token and the scopes it was minted with (H-A-439).
|
||||
value, _ := json.Marshal(map[string]any{"token": token, "scopes": []string{"write:repository", "write:issue", "read:user", "read:package"}})
|
||||
box, err := seal.Seal(string(value), a["public_key"].(string))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return json.Marshal(map[string]any{"name": a["name"], "generation": 1, "sealed": box})
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
@@ -40,6 +40,7 @@ import (
|
||||
"github.com/nats-io/nats.go/jetstream"
|
||||
|
||||
"git.novox.be/novox/mesh-sdk/go/buscalls"
|
||||
"git.novox.be/novox/mesh-sdk/go/seal"
|
||||
"git.novox.be/novox/mesh-sdk/go/testbus"
|
||||
|
||||
"gitea/internal/packageseats"
|
||||
@@ -65,6 +66,7 @@ type player struct {
|
||||
answers map[string]chan wire
|
||||
puts map[string][]byte // the put sessions open, by call
|
||||
gets map[string][]byte // the get sessions open, by call
|
||||
vault map[string]string // the vault's secrets the seat's get hands, by name (design 47 H2 §808)
|
||||
}
|
||||
|
||||
func (p *player) send(m map[string]any) {
|
||||
@@ -130,6 +132,21 @@ func (p *player) handle(m wire) {
|
||||
break
|
||||
}
|
||||
p.answer(m.ID, map[string]any{"module": "m", "version": q.Version, "artifacts": []map[string]any{{"artifact": "m:a", "digest": d}}}, nil)
|
||||
case m.Method == "mesh/ask" && a.Key == "seat:mesh-vault.get":
|
||||
var q struct {
|
||||
Name string `json:"name"`
|
||||
PublicKey string `json:"public_key"`
|
||||
}
|
||||
_ = json.Unmarshal(a.Body, &q)
|
||||
token, ok := p.vault[q.Name]
|
||||
if !ok {
|
||||
p.answer(m.ID, nil, errors.New("the vault holds no secret named "+q.Name))
|
||||
break
|
||||
}
|
||||
// The value the minter writes: the token and the scopes it was minted with (H-A-439).
|
||||
value, _ := json.Marshal(map[string]any{"token": token, "scopes": []string{"write:repository", "write:issue", "read:user", "read:package"}})
|
||||
box, err := seal.Seal(string(value), q.PublicKey)
|
||||
p.answer(m.ID, map[string]any{"name": q.Name, "generation": 1, "sealed": box}, err)
|
||||
case m.Method == "mesh/seat.publish":
|
||||
p.answer(m.ID, map[string]any{}, p.nc.Publish(a.Subject, a.Body))
|
||||
case m.Method == "mesh/capabilities":
|
||||
@@ -378,17 +395,9 @@ func TestThePackageSeatsWhole(t *testing.T) {
|
||||
}
|
||||
}))
|
||||
t.Cleanup(srv.Close)
|
||||
dir := t.TempDir()
|
||||
kept := func(name, token, line string) string {
|
||||
file := filepath.Join(dir, name)
|
||||
_ = os.WriteFile(file, []byte(token+"\n"), 0o600)
|
||||
_ = os.WriteFile(file+".scopes", []byte(line+"\n"), 0o600)
|
||||
return file
|
||||
}
|
||||
cmd := exec.CommandContext(ctx, bin)
|
||||
cmd.Env = append(os.Environ(), "MESH_NODE=novox", "MESH_GITEA_URL="+srv.URL, "MESH_NPM_OWNER=acme", "MESH_GITEA_TOOLS_USER=mesh-tools",
|
||||
"MESH_GITEA_TOKEN_FILE="+kept("token", "reader", "read:package,read:user,write:issue,write:repository for mesh-tools"),
|
||||
"MESH_GITEA_PACKAGES_TOKEN_FILE="+kept("packages-token", "writer", "write:package for mesh-packages"))
|
||||
"MESH_GITEA_PACKAGES_USER=mesh-packages")
|
||||
cmd.Stderr = os.Stderr
|
||||
in, _ := cmd.StdinPipe()
|
||||
out, _ := cmd.StdoutPipe()
|
||||
@@ -400,6 +409,7 @@ func TestThePackageSeatsWhole(t *testing.T) {
|
||||
zipped := bigZip(t, "git.example/acme/sdk@v0.1.15")
|
||||
digest := "sha256:" + sha(zipped)
|
||||
p := &player{ctx: ctx, t: t, nc: nc, js: js, in: in, answers: map[string]chan wire{},
|
||||
vault: map[string]string{"module/gitea/mesh-tools": "reader", "module/gitea/mesh-packages": "writer"},
|
||||
numbered: map[string]string{"git.example/acme/sdk@0.1.15": digest}}
|
||||
go p.serve(out)
|
||||
p.send(map[string]any{"id": "init", "method": "initialize", "params": map[string]any{}})
|
||||
|
||||
@@ -36,10 +36,6 @@ func runSync() int {
|
||||
missing = append(missing, k)
|
||||
}
|
||||
}
|
||||
if forge.TokenFile != "" {
|
||||
// The tools' account cannot make or protect a repository, and must not (ADR 0428 §8).
|
||||
missing = append(missing, "the setup login (MESH_GITEA_TOKEN_FILE is set instead)")
|
||||
}
|
||||
if len(missing) > 0 {
|
||||
fmt.Fprintf(os.Stderr, "repositories: %v not set by the manifest; nothing was changed\n", missing)
|
||||
return 1
|
||||
|
||||
@@ -68,6 +68,15 @@ export async function appendNote(runner: Runner, container: string, owner: strin
|
||||
return { added: true, lines: lines.length + 1 };
|
||||
}
|
||||
|
||||
/** Whether a commit's note holds a line carrying the act's id, read without writing (design 47 H2 §775: a repeat of a
|
||||
* taken ask answers what its act did). */
|
||||
export async function noteHolds(runner: Runner, container: string, owner: string, repo: string, commit: string,
|
||||
notesRef: string, id: string): Promise<boolean> {
|
||||
const shown = await runner("docker", noteArgs(container, owner, repo, commit, notesRef));
|
||||
if (shown.code !== 0 && shown.code !== 1) throw new Error(`the note on ${commit.slice(0, 8)} could not be read (git exited ${shown.code})`);
|
||||
return shown.code === 0 && shown.stdout.split("\n").some((l) => l.includes(`[${id}]`));
|
||||
}
|
||||
|
||||
/** The marker that makes one comment of a pull request the delivery's view. */
|
||||
export const VIEW_MARKER = "<!-- mesh-delivery:view -->";
|
||||
|
||||
|
||||
+17
-5
@@ -32,7 +32,7 @@
|
||||
import { emit } from "@novox/mesh-sdk/events";
|
||||
import { registerModuleTools } from "@novox/mesh-sdk/tools";
|
||||
import { statesAnnouncement } from "./kept.js";
|
||||
import { state } from "@novox/mesh-sdk/state";
|
||||
import { state, TAKEN_STATE } from "@novox/mesh-sdk/state";
|
||||
import { on } from "@novox/mesh-sdk/events";
|
||||
import { broker } from "@novox/mesh-sdk/messaging";
|
||||
import { STATUS_STATE, keepStatus, statusTools, viewWaitsFinding, type Follower, type Status } from "@novox/mesh-sdk/views";
|
||||
@@ -45,8 +45,11 @@ import { REGISTRATION_CHANGED_EVENT, Registrations } from "./registrations.js";
|
||||
import { FailedSteps, Heads, readHeads } from "./failedsteps.js";
|
||||
import { failedStepsTools } from "./tools/failedsteps.js";
|
||||
import { Calls, callRetentionDays, sayUnsaid } from "./calls.js";
|
||||
import { Once, askTakenBoundMs, takenState } from "./taken.js";
|
||||
import { Partials } from "./partials.js";
|
||||
import { callTools } from "./tools/calls.js";
|
||||
import { callTools, mergedTool } from "./tools/calls.js";
|
||||
import { tokenLeaseTool } from "./tools/tokenlease.js";
|
||||
import { processVaultAndLease } from "./token.js";
|
||||
import { seatPublish } from "@novox/mesh-sdk/runtime";
|
||||
import { seat as GIT, events as GIT_EVENTS, type BranchMovedEvent, type PullClosedEvent, type PullMergedEvent, type PullUpdatedEvent } from "@novox/mesh-delivery-contracts-git";
|
||||
import { seat as RUNNER, events as RUNNER_EVENTS, type StepEndedEvent } from "@novox/mesh-delivery-contracts-mesh-pipeline-runner";
|
||||
@@ -59,7 +62,7 @@ import { announcementOf } from "./updated.js";
|
||||
import { ReadAll, readAllTools } from "./readall.js";
|
||||
import { DeliveredWrites, writeTools } from "./writes.js";
|
||||
import { TOOLS_ACCOUNT, TOOLS_ORG, meshAccounts } from "./tools-account.js";
|
||||
import { PACKAGES_TOKEN_REFUSED, keepPackagesToken, packagesToken, renewOnRefusal } from "./packages-account.js";
|
||||
import { PACKAGES_ACCOUNT, PACKAGES_TOKEN_REFUSED, keepPackagesToken, packagesToken, renewOnRefusal } from "./packages-account.js";
|
||||
|
||||
// Without a way to a token — configured, or mintable with the admin account (token.ts) — there is
|
||||
// nothing to watch; log and stay quiet rather than crash the runtime. With one, the first poll mints
|
||||
@@ -282,6 +285,10 @@ const heldHeads = new Heads();
|
||||
let reloadHeads: () => Promise<void> = async () => {};
|
||||
// The merge calls, each written before its first merge (calls.ts; design 47 H §766).
|
||||
const calls = new Calls(state<unknown>("calls"), () => callRetentionDays(), (line) => console.log(`[gitea] ${line}`));
|
||||
// Each act of the seat git is taken once by its ask id, the ids kept in the state taken for ask-taken-bound (taken.ts;
|
||||
// design 47 H2 §775).
|
||||
const once = new Once(takenState(state<unknown>(TAKEN_STATE), askTakenBoundMs(process.env, (line) => console.log(`[gitea] ${line}`))),
|
||||
(line) => console.log(`[gitea] ${line}`));
|
||||
// At the start, every call whose merge-called is not said says it for what landed (§766).
|
||||
const partials = new Partials(state<unknown>("findings"), calls, (line) => console.log(`[gitea] ${line}`));
|
||||
void sayUnsaid(calls, seatPublish, (line) => console.log(`[gitea] ${line}`)).catch((err) =>
|
||||
@@ -297,8 +304,11 @@ void failedSteps.expireSeen().catch((err) => console.log(`[gitea] the seen keys
|
||||
registerModuleTools("gitea", () => [eventsTool(pace), ...readAllTools(readAll), ...writeTools(writes), ...statusTools("gitea", "gitea_first_views", "gitea_first_view_again", followers, firstViews,
|
||||
async (act) => { await broker().request("seat:mesh-hand-acts.hand-act", act); }),
|
||||
...(gitea ? seatTools(gitea, { run, container: FORGE_CONTAINER, registered: () => registrations.written, judge: (head) => failedSteps.judge(head),
|
||||
watched: () => registrations.watched, calls, say: seatPublish, partials }) : []),
|
||||
watched: () => (registrations.current ? registrations.watched : null), calls, say: seatPublish, partials, once }) : []),
|
||||
...callTools(calls, partials, seatPublish, async (act) => { await broker().request("seat:mesh-hand-acts.hand-act", act); }),
|
||||
...(gitea ? [mergedTool(gitea, calls)] : []),
|
||||
tokenLeaseTool(processVaultAndLease().vault, processVaultAndLease().lease,
|
||||
[process.env.MESH_GITEA_TOOLS_USER || TOOLS_ACCOUNT, process.env.MESH_GITEA_PACKAGES_USER || PACKAGES_ACCOUNT]),
|
||||
...failedStepsTools(failedSteps, async (act) => { await broker().request("seat:mesh-hand-acts.hand-act", act); })]);
|
||||
|
||||
/** What the forge's event asks of the announcer at once (issue 729, 1422): see watch.ts. */
|
||||
@@ -453,7 +463,9 @@ if (gitea) {
|
||||
const read = await readHeads(forge, registrations.watched, (line) => console.log(`[gitea] ${line}`));
|
||||
await failedSteps.lost(heldHeads.replace(read.names, read.branches, read.pulls));
|
||||
};
|
||||
// The runner's step-ended is said on step-ended.<module>.<branch>.<commit> (the seat's definition).
|
||||
// The runner's step-ended is said on step-ended.<module>.<branch>.<commit> (the seat's definition); a source-only
|
||||
// check's module is the registration's key source.<owner>.<name> (design 47 H2 §815, H-A-408), one subject part as
|
||||
// a key part escapes it (mesh-sdk keys.EscapeName), and its body names it whole.
|
||||
const stepEnded = `${RUNNER}.${RUNNER_EVENTS.stepEnded}.*.*.*`;
|
||||
void bindUntilHeld(stepEnded, () => on<StepEndedEvent>(stepEnded, (event) => failedSteps.stepEnded(event.body)));
|
||||
// A branch's head is read from the forge, never taken from the event (the seat's definition of branch-moved).
|
||||
|
||||
@@ -3,13 +3,12 @@
|
||||
//
|
||||
// Two ways in, by what the manifest hands the bundle:
|
||||
//
|
||||
// - **as the tools' account** (novox/hq ADR 0365 §2), when MESH_GITEA_TOKEN_FILE names the token the module
|
||||
// keeps for it (token.ts mints it, for `mesh-tools`, and keeps it at 0600 with its scopes line beside it). A
|
||||
// token whose scopes line does not say it is the tools' account's — one kept from before, the administrator's —
|
||||
// is never sent: the call fails, saying so, until the module has minted the account's own. This bundle mints
|
||||
// nothing; a token the forge rejects is renewed by the module's next call, and said here.
|
||||
// - **as the tools' account or the packages account** (novox/hq ADR 0365 §2; design 47 H2 §808), with the account's
|
||||
// token the module keeps in the vault as its own (token.ts mints it; vault.go reads it through the vault seat's
|
||||
// `get` at start and at `secret-changed`). This bundle mints nothing; a token the forge rejects is renewed by the
|
||||
// module's next call, and said here.
|
||||
// - **as the admin account**, whose password the vault delivered into a file this module owns (novox/hq ADR
|
||||
// 0086), by basic authentication, when no token file is named.
|
||||
// 0086), by basic authentication, when no token is given.
|
||||
//
|
||||
// A credential is read when a call is made and never logged, answered or put in an error.
|
||||
package forgehttp
|
||||
@@ -35,12 +34,12 @@ type Client struct {
|
||||
URL string // e.g. http://127.0.0.1:3000
|
||||
User string
|
||||
PasswordFile string
|
||||
// TokenFile, when set, is the tools' account's token (MESH_GITEA_TOKEN_FILE), sent instead of the admin login;
|
||||
// Account the account its scopes line must name (MESH_GITEA_TOOLS_USER, default mesh-tools).
|
||||
TokenFile string
|
||||
Account string
|
||||
HTTP *http.Client // when nil, a client with Timeout
|
||||
Timeout time.Duration // one request's limit when HTTP is nil; 20 s when zero
|
||||
// Token, when set, is the account's token as the vault holds it (vault.go), sent instead of the admin login;
|
||||
// Account the account it is (MESH_GITEA_TOOLS_USER, default mesh-tools; MESH_GITEA_PACKAGES_USER for the writer).
|
||||
Token *VaultToken
|
||||
Account string
|
||||
HTTP *http.Client // when nil, a client with Timeout
|
||||
Timeout time.Duration // one request's limit when HTTP is nil; 20 s when zero
|
||||
}
|
||||
|
||||
// ToolsAccount is the account the forge's tools act as when the manifest names none (token.ts, tools-account.ts).
|
||||
@@ -52,37 +51,35 @@ const ToolsAccount = "mesh-tools"
|
||||
const PackagesAccount = "mesh-packages"
|
||||
|
||||
// WriterFromEnv is the package registry's write side as the manifest hands it to a bundle: the packages account's
|
||||
// token (MESH_GITEA_PACKAGES_TOKEN_FILE, kept by the module beside the tools' token) and the names of the variables
|
||||
// it left unset. Never the admin login: a bundle handed no writer's token writes nothing.
|
||||
func WriterFromEnv() (Client, []string) {
|
||||
c := Client{URL: strings.TrimRight(os.Getenv("MESH_GITEA_URL"), "/"), TokenFile: os.Getenv("MESH_GITEA_PACKAGES_TOKEN_FILE"),
|
||||
Account: os.Getenv("MESH_GITEA_PACKAGES_USER")}
|
||||
if c.Account == "" {
|
||||
c.Account = PackagesAccount
|
||||
}
|
||||
var missing []string
|
||||
for _, kv := range [][2]string{{"MESH_GITEA_URL", c.URL}, {"MESH_GITEA_PACKAGES_TOKEN_FILE", c.TokenFile}} {
|
||||
if kv[1] == "" {
|
||||
missing = append(missing, kv[0])
|
||||
}
|
||||
}
|
||||
return c, missing
|
||||
// token, read from the vault (vault.go), and the names of the variables it left unset. Never the admin login.
|
||||
func WriterFromEnv() (Client, []string, error) {
|
||||
return accountFromEnv(os.Getenv("MESH_GITEA_PACKAGES_USER"), PackagesAccount)
|
||||
}
|
||||
|
||||
// FromEnv is the forge as the manifest hands it to a bundle, and the names of the variables it left unset. A bundle
|
||||
// handed MESH_GITEA_TOKEN_FILE acts as the tools' account and needs no admin login.
|
||||
func FromEnv() (Client, []string) {
|
||||
if file := os.Getenv("MESH_GITEA_TOKEN_FILE"); file != "" {
|
||||
c := Client{URL: strings.TrimRight(os.Getenv("MESH_GITEA_URL"), "/"), TokenFile: file,
|
||||
Account: os.Getenv("MESH_GITEA_TOOLS_USER")}
|
||||
if c.Account == "" {
|
||||
c.Account = ToolsAccount
|
||||
}
|
||||
if c.URL == "" {
|
||||
return c, []string{"MESH_GITEA_URL"}
|
||||
}
|
||||
return c, nil
|
||||
// ToolsFromEnv is the forge as the tools' account, its token read from the vault (vault.go), and the names of the
|
||||
// variables the manifest left unset; it needs no admin login.
|
||||
func ToolsFromEnv() (Client, []string, error) {
|
||||
return accountFromEnv(os.Getenv("MESH_GITEA_TOOLS_USER"), ToolsAccount)
|
||||
}
|
||||
|
||||
func accountFromEnv(account, fallback string) (Client, []string, error) {
|
||||
if account == "" {
|
||||
account = fallback
|
||||
}
|
||||
token, err := TokenOf(account)
|
||||
if err != nil {
|
||||
return Client{}, nil, err
|
||||
}
|
||||
c := Client{URL: strings.TrimRight(os.Getenv("MESH_GITEA_URL"), "/"), Token: token, Account: account}
|
||||
if c.URL == "" {
|
||||
return c, []string{"MESH_GITEA_URL"}, nil
|
||||
}
|
||||
return c, nil, nil
|
||||
}
|
||||
|
||||
// FromEnv is the forge as the admin account, as the manifest hands it to a bundle, and the names of the variables it
|
||||
// left unset.
|
||||
func FromEnv() (Client, []string) {
|
||||
c := Client{
|
||||
URL: strings.TrimRight(os.Getenv("MESH_GITEA_URL"), "/"),
|
||||
User: os.Getenv("MESH_GITEA_ADMIN_USER"),
|
||||
@@ -132,27 +129,10 @@ func (c *Client) password() (string, error) {
|
||||
return strings.TrimSpace(string(raw)), nil
|
||||
}
|
||||
|
||||
// token is the tools' account's token, refused unless its scopes line names that account.
|
||||
func (c *Client) token() (string, error) {
|
||||
raw, err := os.ReadFile(c.TokenFile)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("the token for %s is not kept yet (the module mints it): %w", c.Account, err)
|
||||
}
|
||||
line, err := os.ReadFile(c.TokenFile + ".scopes")
|
||||
if err != nil || !strings.HasSuffix(strings.TrimSpace(string(line)), " for "+c.Account) {
|
||||
return "", fmt.Errorf("the token kept at %s is not %s's (novox/hq ADR 0365): the module mints that account's own at its next call; nothing was sent", c.TokenFile, c.Account)
|
||||
}
|
||||
token := strings.TrimSpace(string(raw))
|
||||
if token == "" {
|
||||
return "", fmt.Errorf("the token kept at %s is empty; nothing was sent", c.TokenFile)
|
||||
}
|
||||
return token, nil
|
||||
}
|
||||
|
||||
// authorize puts the bundle's credential on a request: the tools' token, or the admin login.
|
||||
func (c *Client) authorize(req *http.Request) error {
|
||||
if c.TokenFile != "" {
|
||||
token, err := c.token()
|
||||
if c.Token != nil {
|
||||
token, err := c.Token.Token()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -212,7 +192,7 @@ func (c *Client) Send(ctx context.Context, method, path string, raw []byte, cont
|
||||
return nil, fmt.Errorf("the forge's answer to %s %s is larger than %d MiB and was not read whole", method, path, limit>>20)
|
||||
}
|
||||
switch {
|
||||
case res.StatusCode == http.StatusUnauthorized && c.TokenFile != "":
|
||||
case res.StatusCode == http.StatusUnauthorized && c.Token != nil:
|
||||
return nil, fmt.Errorf("the forge rejected %s's token for %s (401): the module mints it afresh; nothing was done: %w",
|
||||
c.Account, method+" "+path, &StatusError{method, path, res.StatusCode, "unauthorized"})
|
||||
case res.StatusCode == http.StatusNotFound:
|
||||
|
||||
@@ -0,0 +1,173 @@
|
||||
package forgehttp
|
||||
|
||||
// The forge module's tokens as its Go bundles read them (design 47 H2 §808; H-A-422 (1), H-A-426): kept in the vault as
|
||||
// the module's own, `module/gitea/<account>`, written by the TypeScript bundle's minter through own-put, and read here
|
||||
// through the seat `mesh-vault`'s `get`, sealed to this process's own key (the Go SDK's seal opens it), at start and at
|
||||
// each `secret-changed` that names it. Never a file: the bundle holds the token in memory only, and says nothing of it.
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
stdio "git.novox.be/novox/mesh-sdk/go"
|
||||
"git.novox.be/novox/mesh-sdk/go/seal"
|
||||
)
|
||||
|
||||
// VaultGet is the vault seat's verb a token is read by, and SecretChanged the vault's event that a secret changed.
|
||||
const (
|
||||
VaultGet = "seat:mesh-vault.get"
|
||||
SecretChanged = "mesh-vault.secret-changed"
|
||||
)
|
||||
|
||||
// VaultName is the vault's name of an account's token (H-A-426).
|
||||
func VaultName(account string) string { return "module/gitea/" + account }
|
||||
|
||||
// Ask asks one seat verb and answers its reply (stdio.Ask).
|
||||
type Ask func(address string, args any) (json.RawMessage, error)
|
||||
|
||||
// VaultToken is one account's token as this process holds it, read from the vault.
|
||||
type VaultToken struct {
|
||||
Account string
|
||||
ask Ask
|
||||
keys seal.KeyPair
|
||||
|
||||
mu sync.Mutex
|
||||
token string
|
||||
generation int
|
||||
}
|
||||
|
||||
// NewVaultToken is an account's token read through ask, sealed to a key made for this process.
|
||||
func NewVaultToken(account string, ask Ask) (*VaultToken, error) {
|
||||
keys, err := seal.GenerateKeyPair()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("no key to read %s's token with: %w", account, err)
|
||||
}
|
||||
return &VaultToken{Account: account, ask: ask, keys: keys}, nil
|
||||
}
|
||||
|
||||
// Load reads the account's token from the vault again.
|
||||
func (t *VaultToken) Load() error {
|
||||
raw, err := t.ask(VaultGet, map[string]any{"name": VaultName(t.Account), "public_key": t.keys.PublicKey})
|
||||
if err != nil {
|
||||
return fmt.Errorf("the vault did not hand %s: %w", VaultName(t.Account), err)
|
||||
}
|
||||
var got struct {
|
||||
Generation int `json:"generation"`
|
||||
Sealed *seal.Box `json:"sealed"`
|
||||
}
|
||||
if err := json.Unmarshal(raw, &got); err != nil || got.Sealed == nil {
|
||||
return fmt.Errorf("the vault answered no sealed %s", VaultName(t.Account))
|
||||
}
|
||||
opened, err := seal.Open(*got.Sealed, t.keys.PrivateKey)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%s does not open with this process's key: %w", VaultName(t.Account), err)
|
||||
}
|
||||
// The value holds the token and the scopes it was minted with (H-A-439); the minter judges the scopes, this reads the token.
|
||||
var value struct {
|
||||
Token string `json:"token"`
|
||||
}
|
||||
if err := json.Unmarshal([]byte(opened), &value); err != nil {
|
||||
return fmt.Errorf("the vault's %s is not a token with its scopes: %w", VaultName(t.Account), err)
|
||||
}
|
||||
token := strings.TrimSpace(value.Token)
|
||||
if token == "" {
|
||||
return fmt.Errorf("the vault's %s is empty", VaultName(t.Account))
|
||||
}
|
||||
t.mu.Lock()
|
||||
t.token, t.generation = token, got.Generation
|
||||
t.mu.Unlock()
|
||||
return nil
|
||||
}
|
||||
|
||||
// Token is the token held; read from the vault when none is yet (the module mints it first).
|
||||
func (t *VaultToken) Token() (string, error) {
|
||||
t.mu.Lock()
|
||||
token := t.token
|
||||
t.mu.Unlock()
|
||||
if token != "" {
|
||||
return token, nil
|
||||
}
|
||||
if err := t.Load(); err != nil {
|
||||
return "", fmt.Errorf("the token for %s is not kept yet (the module mints it): %w", t.Account, err)
|
||||
}
|
||||
t.mu.Lock()
|
||||
defer t.mu.Unlock()
|
||||
return t.token, nil
|
||||
}
|
||||
|
||||
// Generation is the vault's generation of the token held, 0 when none is held.
|
||||
func (t *VaultToken) Generation() int {
|
||||
t.mu.Lock()
|
||||
defer t.mu.Unlock()
|
||||
return t.generation
|
||||
}
|
||||
|
||||
// Changed takes one `secret-changed`: the token is read again when it names this account's; answers whether it did.
|
||||
func (t *VaultToken) Changed(body []byte) (bool, error) {
|
||||
var e struct {
|
||||
Name string `json:"name"`
|
||||
}
|
||||
if err := json.Unmarshal(body, &e); err != nil {
|
||||
return false, fmt.Errorf("%s is not a secret's change: %w", SecretChanged, err)
|
||||
}
|
||||
if e.Name != VaultName(t.Account) {
|
||||
return false, nil
|
||||
}
|
||||
return true, t.Load()
|
||||
}
|
||||
|
||||
var (
|
||||
processTokens = map[string]*VaultToken{}
|
||||
processTokensMu sync.Mutex
|
||||
)
|
||||
|
||||
// TokenOf is this process's one token of account, read through the runtime's seat calls.
|
||||
func TokenOf(account string) (*VaultToken, error) {
|
||||
processTokensMu.Lock()
|
||||
defer processTokensMu.Unlock()
|
||||
if t, ok := processTokens[account]; ok {
|
||||
return t, nil
|
||||
}
|
||||
t, err := NewVaultToken(account, stdio.Ask)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
processTokens[account] = t
|
||||
return t, nil
|
||||
}
|
||||
|
||||
// Follow reads each account's token once the runtime serves, and again at each `secret-changed` naming it, saying
|
||||
// through logf what it read (never the token). It returns when the subscription ends.
|
||||
func Follow(logf func(format string, args ...any), accounts ...string) error {
|
||||
var held []*VaultToken
|
||||
for _, a := range accounts {
|
||||
t, err := TokenOf(a)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
held = append(held, t)
|
||||
}
|
||||
<-stdio.Served()
|
||||
for _, t := range held {
|
||||
if err := t.Load(); err != nil {
|
||||
logf("%s is not read at the start (the module mints it): %v", VaultName(t.Account), err)
|
||||
} else {
|
||||
logf("read %s at generation %d", VaultName(t.Account), t.Generation())
|
||||
}
|
||||
}
|
||||
return stdio.Subscribe(SecretChanged, func(e stdio.Envelope) error {
|
||||
var errs []error
|
||||
for _, t := range held {
|
||||
if read, err := t.Changed(e.Body); err != nil {
|
||||
errs = append(errs, err)
|
||||
logf("%s changed and could not be read again: %v", VaultName(t.Account), err)
|
||||
} else if read {
|
||||
logf("read %s again at generation %d", VaultName(t.Account), t.Generation())
|
||||
}
|
||||
}
|
||||
return errors.Join(errs...)
|
||||
})
|
||||
}
|
||||
@@ -12,12 +12,20 @@ package seatgit
|
||||
// - A runner's step-ended of a failed required step at a pull request's head and fingerprint is kept, and a merge call
|
||||
// naming that head merges nothing and names the step (§735, test 114).
|
||||
// - A call naming two pull requests in two repositories merges both, in order, and says one merge-called (§734, §736).
|
||||
// - An hq pull request, of a source-only registration, whose Go checks failed is refused naming the step (H2 §815).
|
||||
// - With the forge's store stopped a merge call answers away, and called again once it is back it lands (H2 §776).
|
||||
// - A forge that refuses the second merge gives one merge-called with the first member and `refused`, and merge-partial
|
||||
// raised; a later call landing the refused member clears it (§767, test 115).
|
||||
// - No token is configured: the bundle mints the tools' account's token at the fixture forge with the admin's login,
|
||||
// writes it through the vault seat's two-step sealed own-put and reads it through its sealed get, both answered
|
||||
// here, and the forge holds one token for the account after, the old fixed-name token deleted (H2 §808, H-A-422,
|
||||
// H-A-430 (1), H-A-453 (3)).
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
@@ -33,9 +41,11 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.novox.be/novox/mesh-sdk/go/seal"
|
||||
"git.novox.be/novox/mesh-sdk/go/testbus"
|
||||
"github.com/nats-io/nats.go"
|
||||
"github.com/nats-io/nats.go/jetstream"
|
||||
"golang.org/x/crypto/nacl/box"
|
||||
)
|
||||
|
||||
const within = 30 * time.Second
|
||||
@@ -54,12 +64,132 @@ type forge struct {
|
||||
pulls map[string]*pull
|
||||
merges []string
|
||||
paths []string
|
||||
// away is the forge's store stopped (§776): the API answers 503 and the version read names the store unavailable.
|
||||
away bool
|
||||
// admin is the admin's login, which mints (§808); tokens what each account holds; every other call is refused 401
|
||||
// unless it carries one of them.
|
||||
admin string
|
||||
tokens map[string][]forgeToken
|
||||
nextID int
|
||||
}
|
||||
|
||||
// forgeToken is one access token the fixture forge minted.
|
||||
type forgeToken struct {
|
||||
ID int `json:"id"`
|
||||
Name string `json:"name"`
|
||||
sha1 string
|
||||
}
|
||||
|
||||
// authorized is the admin's basic login or a token the forge holds.
|
||||
func (f *forge) authorized(r *http.Request) bool {
|
||||
if u, p, ok := r.BasicAuth(); ok {
|
||||
return u == f.admin && p == adminPassword
|
||||
}
|
||||
given := strings.TrimPrefix(r.Header.Get("Authorization"), "token ")
|
||||
for _, held := range f.tokens {
|
||||
for _, t := range held {
|
||||
if given != "" && t.sha1 == given {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// held answers the names of the tokens an account holds and their values.
|
||||
func (f *forge) held(account string) (names, values []string) {
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
for _, t := range f.tokens[account] {
|
||||
names, values = append(names, t.Name), append(values, t.sha1)
|
||||
}
|
||||
return names, values
|
||||
}
|
||||
|
||||
// serveAdmin answers the admin API the mint uses: the admin's own read, the account and its team, and the account's
|
||||
// tokens (mint, list, delete). False when the path is not one of them.
|
||||
func (f *forge) serveAdmin(w http.ResponseWriter, r *http.Request) bool {
|
||||
switch p := r.URL.Path; {
|
||||
case p == "/api/v1/user":
|
||||
_ = json.NewEncoder(w).Encode(map[string]any{"login": f.admin})
|
||||
case p == "/api/v1/admin/users" && r.Method == http.MethodPost:
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
case p == "/api/v1/orgs/novox/teams" && r.Method == http.MethodGet:
|
||||
_, _ = w.Write([]byte("[]"))
|
||||
case p == "/api/v1/orgs/novox/teams" && r.Method == http.MethodPost:
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
_, _ = w.Write([]byte(`{"id":1}`))
|
||||
case strings.HasPrefix(p, "/api/v1/teams/1/members/"):
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
default:
|
||||
m := regexp.MustCompile(`^/api/v1/users/([^/]+)/tokens(?:/(\d+))?$`).FindStringSubmatch(p)
|
||||
if m == nil {
|
||||
return false
|
||||
}
|
||||
account := m[1]
|
||||
switch {
|
||||
case r.Method == http.MethodPost:
|
||||
var asked struct {
|
||||
Name string `json:"name"`
|
||||
}
|
||||
_ = json.NewDecoder(r.Body).Decode(&asked)
|
||||
for _, t := range f.tokens[account] {
|
||||
if t.Name == asked.Name {
|
||||
http.Error(w, `{"message":"access token name has been used already"}`, http.StatusUnprocessableEntity)
|
||||
return true
|
||||
}
|
||||
}
|
||||
f.nextID++
|
||||
t := forgeToken{ID: f.nextID, Name: asked.Name, sha1: fmt.Sprintf("%040x", 0xa0000+f.nextID)}
|
||||
f.tokens[account] = append(f.tokens[account], t)
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
_ = json.NewEncoder(w).Encode(map[string]any{"id": t.ID, "name": t.Name, "sha1": t.sha1})
|
||||
case r.Method == http.MethodGet:
|
||||
if r.URL.Query().Get("page") != "1" {
|
||||
_, _ = w.Write([]byte("[]"))
|
||||
return true
|
||||
}
|
||||
_ = json.NewEncoder(w).Encode(f.tokens[account])
|
||||
case r.Method == http.MethodDelete:
|
||||
kept := f.tokens[account][:0]
|
||||
for _, t := range f.tokens[account] {
|
||||
if fmt.Sprint(t.ID) != m[2] {
|
||||
kept = append(kept, t)
|
||||
}
|
||||
}
|
||||
f.tokens[account] = kept
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// adminPassword is the admin's password the fixture forge takes, in the file the host would unseal it into.
|
||||
const adminPassword = "admin-password"
|
||||
|
||||
func (f *forge) serve(w http.ResponseWriter, r *http.Request) {
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
f.paths = append(f.paths, r.Method+" "+r.URL.Path)
|
||||
if r.URL.Path == "/api/v1/version" {
|
||||
if f.away {
|
||||
http.Error(w, `{"message":"the database is unavailable"}`, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
_, _ = w.Write([]byte(`{"version":"1.27.3"}`))
|
||||
return
|
||||
}
|
||||
if f.away {
|
||||
http.Error(w, "Service Unavailable", http.StatusServiceUnavailable)
|
||||
return
|
||||
}
|
||||
if !f.authorized(r) {
|
||||
http.Error(w, `{"message":"token is required"}`, http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
if f.serveAdmin(w, r) {
|
||||
return
|
||||
}
|
||||
m := regexp.MustCompile(`^/api/v1/repos/([^/]+/[^/]+)/pulls/(\d+)(/merge)?$`).FindStringSubmatch(r.URL.Path)
|
||||
if m != nil {
|
||||
p := f.pulls[m[1]+"#"+m[2]]
|
||||
@@ -120,6 +250,76 @@ type runtime struct {
|
||||
// regs are what the seat mesh-catalog's registrations answers; subs the patterns the bundle subscribed.
|
||||
regs []map[string]any
|
||||
subs []string
|
||||
// vault is the seat mesh-vault as this test answers it (§808, §822): the module's own secrets by name, and the
|
||||
// own-put calls opened, each with the key its value is sealed to.
|
||||
vault map[string]vaulted
|
||||
puts map[string]openPut
|
||||
}
|
||||
|
||||
// openPut is an own-put call its first step opened: the name it puts and the key its value is sealed to.
|
||||
type openPut struct {
|
||||
name string
|
||||
pub, priv *[32]byte
|
||||
}
|
||||
|
||||
// vaulted is one secret the test's vault keeps: its newest value and generation.
|
||||
type vaulted struct {
|
||||
value string
|
||||
generation int
|
||||
}
|
||||
|
||||
// vaultAsk answers the seat mesh-vault's get, sealed to the asking process's key, and its two-step own-put, whose value
|
||||
// comes sealed to the key the first step made (§822).
|
||||
func (r *runtime) vaultAsk(key string, body json.RawMessage) (any, error) {
|
||||
var b struct {
|
||||
Name string `json:"name"`
|
||||
PublicKey string `json:"public_key"`
|
||||
Call string `json:"call"`
|
||||
Sealed string `json:"sealed"`
|
||||
}
|
||||
_ = json.Unmarshal(body, &b)
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
switch {
|
||||
case key == "seat:mesh-vault.get":
|
||||
v, ok := r.vault[b.Name]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("the vault holds no secret named %s", b.Name)
|
||||
}
|
||||
sealed, err := seal.Seal(v.value, b.PublicKey)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return map[string]any{"generation": v.generation, "changed-at": time.Now().UTC().Format(time.RFC3339), "sealed": sealed}, nil
|
||||
case b.Call == "":
|
||||
pub, priv, err := box.GenerateKey(rand.Reader)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
r.next++
|
||||
call := fmt.Sprintf("put-%d", r.next)
|
||||
r.puts[call] = openPut{name: b.Name, pub: pub, priv: priv}
|
||||
return map[string]any{"call": call, "seal_to": base64.StdEncoding.EncodeToString(pub[:])}, nil
|
||||
default:
|
||||
put, ok := r.puts[b.Call]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("no own-put call %s is open", b.Call)
|
||||
}
|
||||
raw, err := base64.StdEncoding.DecodeString(b.Sealed)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
value, ok := box.OpenAnonymous(nil, raw, put.pub, put.priv)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("the own-put value of %s is not sealed to its call's key", b.Call)
|
||||
}
|
||||
// The module named by the caller's account, never by an argument: here always the forge module (§822).
|
||||
name := "module/gitea/" + put.name
|
||||
delete(r.puts, b.Call)
|
||||
v := vaulted{value: string(value), generation: r.vault[name].generation + 1}
|
||||
r.vault[name] = v
|
||||
return map[string]any{"generation": v.generation}, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (r *runtime) subscribed(pattern string) bool {
|
||||
@@ -192,7 +392,15 @@ func (r *runtime) answer(id json.RawMessage, method string, params json.RawMessa
|
||||
r.subs = append(r.subs, p.Pattern)
|
||||
r.mu.Unlock()
|
||||
return map[string]any{}, nil
|
||||
case "mesh/publish", "mesh/state.watch", "mesh/seat.record":
|
||||
case "mesh/state.watch":
|
||||
// The mint's lease is watched for its release (§808): its changes are delivered as the runtime delivers them.
|
||||
if p.State == "token-lease" {
|
||||
var watch string
|
||||
_ = json.Unmarshal(id, &watch)
|
||||
return map[string]any{}, r.watchState(kv, p.Key, watch)
|
||||
}
|
||||
return map[string]any{}, nil
|
||||
case "mesh/publish", "mesh/seat.record":
|
||||
return map[string]any{}, nil
|
||||
case "mesh/capabilities":
|
||||
return map[string]any{"words": []string{}}, nil
|
||||
@@ -210,6 +418,8 @@ func (r *runtime) answer(id json.RawMessage, method string, params json.RawMessa
|
||||
return map[string]any{"registrations": r.regs}, nil
|
||||
case "seat:mesh-hand-acts.hand-act":
|
||||
return map[string]any{}, nil
|
||||
case "seat:mesh-vault.get", "seat:mesh-vault.own-put":
|
||||
return r.vaultAsk(a.Key, p.Body)
|
||||
}
|
||||
return nil, fmt.Errorf("the runtime answers no ask %s in this test", a.Key)
|
||||
case "mesh/state.get":
|
||||
@@ -224,6 +434,15 @@ func (r *runtime) answer(id json.RawMessage, method string, params json.RawMessa
|
||||
return nil, err
|
||||
}
|
||||
return map[string]any{"key": p.Key, "value": json.RawMessage(e.Value()), "revision": e.Revision()}, nil
|
||||
case "mesh/state.create":
|
||||
if kv == nil {
|
||||
return nil, fmt.Errorf("the state %q is not declared", p.State)
|
||||
}
|
||||
rev, err := kv.Create(ctx, p.Key, p.Value)
|
||||
if errors.Is(err, jetstream.ErrKeyExists) {
|
||||
return nil, changed{err}
|
||||
}
|
||||
return map[string]any{"revision": rev}, err
|
||||
case "mesh/state.put":
|
||||
if kv == nil {
|
||||
return nil, fmt.Errorf("the state %q is not declared", p.State)
|
||||
@@ -253,12 +472,46 @@ func (r *runtime) answer(id json.RawMessage, method string, params json.RawMessa
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
r.say(map[string]any{"id": id, "error": map[string]any{"code": -32000, "message": err.Error()}})
|
||||
code := -32000
|
||||
if errors.As(err, new(changed)) {
|
||||
code = -32010
|
||||
}
|
||||
r.say(map[string]any{"id": id, "error": map[string]any{"code": code, "message": err.Error()}})
|
||||
return
|
||||
}
|
||||
r.say(map[string]any{"id": id, "result": result})
|
||||
}
|
||||
|
||||
// watchState delivers each later change of one key of a state to the bundle's watch, as `mesh/state` requests naming
|
||||
// the watch, a delete without its current value.
|
||||
func (r *runtime) watchState(kv jetstream.KeyValue, key, watch string) error {
|
||||
w, err := kv.Watch(context.Background(), key, jetstream.UpdatesOnly())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
r.t.Cleanup(func() { _ = w.Stop() })
|
||||
go func() {
|
||||
for e := range w.Updates() {
|
||||
if e == nil {
|
||||
continue
|
||||
}
|
||||
change := map[string]any{"key": e.Key()}
|
||||
if e.Operation() == jetstream.KeyValuePut {
|
||||
change["current"] = map[string]any{"value": json.RawMessage(e.Value()), "revision": e.Revision()}
|
||||
}
|
||||
r.mu.Lock()
|
||||
r.next++
|
||||
n := r.next
|
||||
r.mu.Unlock()
|
||||
r.say(map[string]any{"id": fmt.Sprintf("rt-watch-%d", n), "method": "mesh/state", "params": map[string]any{"watch": watch, "change": change}})
|
||||
}
|
||||
}()
|
||||
return nil
|
||||
}
|
||||
|
||||
// changed is the runtime's refusal of a create whose key has a value (mesh-sdk's CODE_CHANGED, -32010).
|
||||
type changed struct{ error }
|
||||
|
||||
// ask sends the bundle one request and answers its result.
|
||||
func (r *runtime) ask(method string, params map[string]any) json.RawMessage {
|
||||
r.t.Helper()
|
||||
@@ -344,14 +597,27 @@ func numbers(v any) []float64 {
|
||||
return out
|
||||
}
|
||||
|
||||
// needTheModulesPackages ends the test NOT RUN, never failed, where what it builds and runs with is missing: node and npm
|
||||
// on the PATH and the module's installed SDK package under node_modules. The Go integration level is given the tree
|
||||
// without its npm packages and no network (issue 1960, H-A-294), as the Node build module's realMachine skips by name.
|
||||
func needTheModulesPackages(t *testing.T, dir string) {
|
||||
t.Helper()
|
||||
for _, p := range []string{"node", "npm"} {
|
||||
if _, err := exec.LookPath(p); err != nil {
|
||||
t.Skipf("NOT RUN: no %s installed where this check runs; proven in a working copy of the module after npm install", p)
|
||||
}
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(dir, "node_modules", "@novox", "mesh-sdk")); err != nil {
|
||||
t.Skipf("NOT RUN: the forge module's npm packages are not installed in %s (%v); proven in a working copy of the module after npm install", dir, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTheSeatGitWholeOverARealBus(t *testing.T) {
|
||||
dir, err := filepath.Abs(filepath.Join("..", ".."))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(dir, "node_modules", "@novox", "mesh-sdk")); err != nil {
|
||||
t.Fatalf("the forge module's packages are not installed in %s (npm install first): %v", dir, err)
|
||||
}
|
||||
needTheModulesPackages(t, dir)
|
||||
build := exec.Command("npm", "run", "build")
|
||||
build.Dir = dir
|
||||
if out, err := build.CombinedOutput(); err != nil {
|
||||
@@ -370,14 +636,14 @@ func TestTheSeatGitWholeOverARealBus(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
kvs := map[string]jetstream.KeyValue{}
|
||||
for _, name := range []string{"bound", "announced", "findings", "first-views", "transfers", "idle", "failed-steps", "calls"} {
|
||||
for _, name := range []string{"bound", "announced", "findings", "first-views", "transfers", "idle", "failed-steps", "calls", "token-lease"} {
|
||||
kv, err := js.CreateKeyValue(ctx, jetstream.KeyValueConfig{Bucket: "gitea_" + name})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
kvs[name] = kv
|
||||
}
|
||||
A, B, X := strings.Repeat("a", 40), strings.Repeat("b", 40), strings.Repeat("e", 40)
|
||||
A, B, X, H := strings.Repeat("a", 40), strings.Repeat("b", 40), strings.Repeat("e", 40), strings.Repeat("f", 40)
|
||||
// A call stopped after the second of three merges, kept and not said (§766).
|
||||
stopped, _ := json.Marshal(map[string]any{"by": "lead@shanks", "account": "shanks.claude-code", "at": "2026-11-20T03:00:00Z", "said": false,
|
||||
"members": []any{
|
||||
@@ -394,16 +660,24 @@ func TestTheSeatGitWholeOverARealBus(t *testing.T) {
|
||||
"novox/app#7": {repo: "novox/app", base: "main", head: A, number: 7},
|
||||
"novox/lib#9": {repo: "novox/lib", base: "main", head: B, number: 9, refuse: true},
|
||||
"novox/lib#10": {repo: "novox/lib", base: "main", head: B, number: 10},
|
||||
"novox/hq#11": {repo: "novox/hq", base: "main", head: H, number: 11},
|
||||
}}
|
||||
// The forge holds the tools' account's token under its old fixed name, which the first mint replaces (H-A-430 (1)).
|
||||
f.admin, f.nextID = "mesh-admin", 1
|
||||
f.tokens = map[string][]forgeToken{"mesh-tools": {{ID: 1, Name: "mesh-tools", sha1: strings.Repeat("0", 40)}}}
|
||||
srv := httptest.NewServer(http.HandlerFunc(f.serve))
|
||||
t.Cleanup(srv.Close)
|
||||
|
||||
state := t.TempDir()
|
||||
passwordFile := filepath.Join(state, "admin.secret")
|
||||
if err := os.WriteFile(passwordFile, []byte(adminPassword+"\n"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
launch := `const sdk = await import("@novox/mesh-sdk/stdio"); await import("./dist/index.js"); await import("./dist/tools/index.js"); await sdk.serveRegisteredOverStdio("gitea");`
|
||||
cmd := exec.Command("node", "--input-type=module", "-e", launch)
|
||||
cmd.Dir = dir
|
||||
cmd.Env = append(os.Environ(), "MESH_SERVED_MODULE=gitea", "MESH_MODULE=gitea", "MESH_NODE=test", "MESH_GITEA_URL="+srv.URL,
|
||||
"GITEA_TOKEN=t", "MESH_GITEA_STATE_DIR="+state, "MESH_GITEA_HOOK_PORT=0", "MESH_GITEA_CONFIG_FILE="+filepath.Join(state, "config.json"))
|
||||
"MESH_GITEA_ADMIN_USER=mesh-admin", "MESH_GITEA_ADMIN_PASSWORD_FILE="+passwordFile, "MESH_GITEA_STATE_DIR="+state, "MESH_GITEA_HOOK_PORT=0", "MESH_GITEA_CONFIG_FILE="+filepath.Join(state, "config.json"))
|
||||
in, _ := cmd.StdinPipe()
|
||||
out, _ := cmd.StdoutPipe()
|
||||
cmd.Stderr = testLog{t}
|
||||
@@ -413,7 +687,11 @@ func TestTheSeatGitWholeOverARealBus(t *testing.T) {
|
||||
t.Cleanup(func() { _ = in.Close(); _ = cmd.Process.Kill(); _ = cmd.Wait() })
|
||||
rt := &runtime{t: t, in: in, js: js, kvs: kvs, wait: map[string]chan json.RawMessage{}, regs: []map[string]any{
|
||||
{"module": "app", "origin": "novox/app", "branches": []string{"main"}},
|
||||
{"module": "lib", "origin": "novox/lib", "branches": []string{"main"}}}}
|
||||
{"module": "lib", "origin": "novox/lib", "branches": []string{"main"}},
|
||||
// A source-only registration (H2 §815): no module, one watched branch, its check a build seat's step.
|
||||
{"origin": "novox/hq", "branches": []string{"main"}, "policy": map[string]any{"steps": map[string]any{"unit": "required"}},
|
||||
"check": map[string]any{"seat": "node-build-go", "step": "unit"}}}}
|
||||
rt.vault, rt.puts = map[string]vaulted{}, map[string]openPut{}
|
||||
go rt.read(out)
|
||||
|
||||
// At the start the stopped call says one merge-called naming the two that landed (§766).
|
||||
@@ -443,6 +721,32 @@ func TestTheSeatGitWholeOverARealBus(t *testing.T) {
|
||||
t.Fatalf("merged %v", got)
|
||||
}
|
||||
|
||||
// An hq pull request whose Go checks fail is refused by merge-pull naming the failed step (H2 §815): the runner's
|
||||
// step-ended of a source-only check names the registration's key as its module and the commit's root tree hash as its
|
||||
// fingerprint (H-A-408), escaped as one part of its subject, judged as a module's.
|
||||
rt.ask("mesh/event", map[string]any{"envelope": map[string]any{"key": "mesh-pipeline-runner.step-ended.source=2enovox=2ehq.main." + H, "node": "test",
|
||||
"body": map[string]any{"v": 1, "pipeline": "main.5", "module": "source.novox.hq", "branch": "main", "commit": H, "fingerprint": strings.Repeat("7", 40),
|
||||
"at": "2026-11-22T01:00:00Z", "result": map[string]any{"step": "unit", "status": "required", "outcome": "failed"}}}})
|
||||
hq := rt.mergePull(map[string]any{"repository": "novox/hq", "number": 11, "head": H})
|
||||
hqm, _ := hq["members"].([]any)
|
||||
if len(hqm) != 1 || hqm[0].(map[string]any)["state"] != "refused" ||
|
||||
!strings.Contains(fmt.Sprint(hqm[0].(map[string]any)["why"]), "the unit step of source.novox.hq failed in run main.5") {
|
||||
t.Fatalf("an hq head at a failed check answered %v", hq)
|
||||
}
|
||||
|
||||
// With the forge's store stopped, a merge call answers away, merging nothing (H2 §776); called again once the store is
|
||||
// back, it lands (below).
|
||||
f.mu.Lock()
|
||||
f.away = true
|
||||
f.mu.Unlock()
|
||||
away := rt.mergePull(map[string]any{"repository": "novox/app", "number": 7, "head": A}, map[string]any{"repository": "novox/lib", "number": 10, "head": B})
|
||||
if away["error-kind"] != "away" || away["part"] != "the forge's store" || len(f.merged()) != 0 {
|
||||
t.Fatalf("a merge call with the forge's store stopped answered %v, merged %v", away, f.merged())
|
||||
}
|
||||
f.mu.Lock()
|
||||
f.away = false
|
||||
f.mu.Unlock()
|
||||
|
||||
// Two pull requests in two repositories merge, in order, and say one merge-called (§734, §736).
|
||||
both := rt.mergePull(map[string]any{"repository": "novox/app", "number": 7, "head": A}, map[string]any{"repository": "novox/lib", "number": 10, "head": B})
|
||||
call, _ := both["call"].(string)
|
||||
@@ -472,6 +776,27 @@ func TestTheSeatGitWholeOverARealBus(t *testing.T) {
|
||||
_, err := kvs["findings"].Get(ctx, key)
|
||||
return errors.Is(err, jetstream.ErrKeyNotFound)
|
||||
})
|
||||
|
||||
// No token was configured: the calls above ran on the token the bundle minted with the admin's login, wrote through
|
||||
// the two-step sealed own-put and read through the sealed get; the forge holds one token for the account after, the
|
||||
// one the vault keeps, and the old fixed-name token is gone (§808, H-A-430 (1)).
|
||||
until(t, "the forge holds one token for the tools' account", func() bool {
|
||||
names, _ := f.held("mesh-tools")
|
||||
return len(names) == 1
|
||||
})
|
||||
names, values := f.held("mesh-tools")
|
||||
rt.mu.Lock()
|
||||
kept := rt.vault["module/gitea/mesh-tools"]
|
||||
rt.mu.Unlock()
|
||||
var opened struct {
|
||||
Token string `json:"token"`
|
||||
Scopes []string `json:"scopes"`
|
||||
}
|
||||
_ = json.Unmarshal([]byte(kept.value), &opened)
|
||||
if fmt.Sprint(names) != "[mesh-tools-1]" || opened.Token != values[0] || kept.generation != 1 || len(opened.Scopes) == 0 {
|
||||
t.Fatalf("the forge holds %v for the tools' account; the vault keeps generation %d of a token %t to it, scopes %v",
|
||||
names, kept.generation, opened.Token == values[0], opened.Scopes)
|
||||
}
|
||||
}
|
||||
|
||||
type testLog struct{ t *testing.T }
|
||||
|
||||
@@ -12,6 +12,8 @@ export const KEPT: readonly { readonly name: string; readonly readable?: boolean
|
||||
{ name: "failed-steps" },
|
||||
{ name: "first-views" },
|
||||
{ name: "idle" },
|
||||
{ name: "taken" },
|
||||
{ name: "token-lease" },
|
||||
{ name: "transfers" },
|
||||
];
|
||||
|
||||
|
||||
@@ -5,7 +5,13 @@
|
||||
// `already-merged` with its commit), and no run at the head's build-input fingerprint failed a `required` step (the
|
||||
// module's own `failed-steps`). No branch protection is judged (ADR 0682 §13). When one member is refused, none is merged
|
||||
// and the answer names every refused member and why; else the members are merged in the order the call names them. The
|
||||
// verb answers once, at the call's end: `call` and each member's outcome.
|
||||
// verb answers once, at the call's end: `call` and each member's outcome. A member whose repository has no watched branch
|
||||
// (no entry, or an empty set, in the registrations' watched branches) is the forge's business alone (H2 H-A-471 and its
|
||||
// addendum): judged against nothing of the module graph, merged plainly in the order named, answered and kept merged with
|
||||
// the note no delivery, and left out of merge-called, so it starts no delivery. A watched repository whose base is no
|
||||
// watched branch stays refused. A member is merged plainly only from a view of the watched branches read since the last
|
||||
// registration-changed heard (the second addendum): while the view is unread or stale, every member is refused, never
|
||||
// merged with the note.
|
||||
|
||||
import { randomBytes } from "node:crypto";
|
||||
import { UNPROVEN, callerOf } from "@novox/mesh-sdk/callers";
|
||||
@@ -20,8 +26,9 @@ import type { Partials } from "./partials.js";
|
||||
export interface CallDeps {
|
||||
/** Whether the first registrations were written: until then no merge is made (design 47 §95). */
|
||||
registered: () => boolean;
|
||||
/** The watched branches the registrations name, by their origin repository `<owner>/<name>`. */
|
||||
watched: () => ReadonlyMap<string, ReadonlySet<string>>;
|
||||
/** The watched branches the registrations name, by their origin repository `<owner>/<name>`; null while they are not
|
||||
* read since the last registration-changed heard (H-A-471, second addendum). */
|
||||
watched: () => ReadonlyMap<string, ReadonlySet<string>> | null;
|
||||
/** The failed `required` step that refuses a head, or null (failedsteps.ts). */
|
||||
judge: (head: string) => Promise<Judged | null>;
|
||||
/** A new call's id: 16 lowercase hex characters (H-A-24). */
|
||||
@@ -50,8 +57,18 @@ export interface Member {
|
||||
state: MemberOutcome["state"];
|
||||
commit?: string;
|
||||
why?: string;
|
||||
/** Set on a member merged plainly, its repository having no watched branch (H-A-471). */
|
||||
note?: string;
|
||||
/** Its repository has no watched branch: judged against nothing of the module graph (H-A-471). */
|
||||
plain?: boolean;
|
||||
}
|
||||
|
||||
/** The note of a member merged plainly: it starts no delivery (H-A-471). */
|
||||
export const NO_DELIVERY = "no delivery: the repository has no watched branch";
|
||||
|
||||
/** Why every member is refused while the watched branches are unread or stale (H-A-471, second addendum). */
|
||||
export const NOT_READ = "the watched branches are not read yet";
|
||||
|
||||
const SHA = /^[0-9a-f]{40}$/;
|
||||
export const CALL = /^[0-9a-f]{16}$/;
|
||||
|
||||
@@ -78,7 +95,7 @@ export function membersOf(raw: unknown): Member[] {
|
||||
}
|
||||
|
||||
/** Why a member is refused at judging, or "" when it is judged. */
|
||||
async function judgeMember(forge: CallForge, deps: CallDeps, m: Member): Promise<string> {
|
||||
async function judgeMember(forge: CallForge, deps: CallDeps, watched: ReadonlyMap<string, ReadonlySet<string>>, m: Member): Promise<string> {
|
||||
const pull = await forge.getPullRequest(m.owner, m.repo, m.number);
|
||||
m.branch = String(pull.base ?? "");
|
||||
if (pull.merged) {
|
||||
@@ -89,7 +106,13 @@ async function judgeMember(forge: CallForge, deps: CallDeps, m: Member): Promise
|
||||
if (pull.head_sha !== m.head) {
|
||||
return `head-moved: its head is ${String(pull.head_sha).slice(0, 12)}, not the ${m.head.slice(0, 12)} the caller names`;
|
||||
}
|
||||
if (!deps.watched().get(m.repository)?.has(m.branch)) return `its base ${m.branch} is no watched branch`;
|
||||
const branches = watched.get(m.repository);
|
||||
// A repository of no module: no build-input fingerprint and no required step judge it (H-A-471).
|
||||
if (!branches || branches.size === 0) {
|
||||
m.plain = true;
|
||||
return "";
|
||||
}
|
||||
if (!branches.has(m.branch)) return `its base ${m.branch} is no watched branch`;
|
||||
const failed = await deps.judge(m.head);
|
||||
if (failed) {
|
||||
return `the ${failed.step} step of ${failed.module} failed in run ${failed.run} at its build-input fingerprint ${failed.fingerprint}`;
|
||||
@@ -117,7 +140,7 @@ async function mergeMember(forge: CallForge, m: Member): Promise<string> {
|
||||
|
||||
/** A member's outcome as the answer says it. */
|
||||
export const outcomeOf = (m: Member): MemberOutcome => ({ repository: m.repository, number: m.number, state: m.state,
|
||||
...(m.commit ? { commit: m.commit } : {}), ...(m.why ? { why: m.why } : {}) });
|
||||
...(m.commit ? { commit: m.commit } : {}), ...(m.why ? { why: m.why } : {}), ...(m.note ? { note: m.note } : {}) });
|
||||
|
||||
/** merge-pull: one merge call (design 47 H §734, §735). */
|
||||
export async function mergeCall(forge: CallForge, args: Record<string, unknown>, deps: CallDeps): Promise<MergePullAnswer> {
|
||||
@@ -130,10 +153,12 @@ export async function mergeCall(forge: CallForge, args: Record<string, unknown>,
|
||||
if (!deps.registered()) throw new Refused("no module is registered yet: the catalog has said no registration-changed, so no merge is made until the first registrations are written");
|
||||
const call = (deps.newCall ?? newCallId)();
|
||||
if (!CALL.test(call)) throw new Error(`the call's id ${JSON.stringify(call)} is not 16 lowercase hex characters`);
|
||||
// One view judges the whole call; unread or stale, it judges no member (H-A-471, second addendum).
|
||||
const watched = deps.watched();
|
||||
// Every member is judged before any is merged (§735).
|
||||
let refused = false;
|
||||
for (const m of members) {
|
||||
const why = await judgeMember(forge, deps, m);
|
||||
const why = watched ? await judgeMember(forge, deps, watched, m) : NOT_READ;
|
||||
if (why) {
|
||||
m.state = "refused";
|
||||
m.why = why;
|
||||
@@ -157,6 +182,7 @@ export async function mergeCall(forge: CallForge, args: Record<string, unknown>,
|
||||
try {
|
||||
m.commit = await mergeMember(forge, m);
|
||||
m.state = "merged";
|
||||
if (m.plain) m.note = NO_DELIVERY;
|
||||
at = now().toISOString();
|
||||
await keep(false);
|
||||
} catch (err) {
|
||||
|
||||
+65
-14
@@ -1,7 +1,11 @@
|
||||
{
|
||||
"module": "gitea",
|
||||
"version": "0.1",
|
||||
"class": "core",
|
||||
"moved": true,
|
||||
"check-needs": [
|
||||
"node-build-node"
|
||||
],
|
||||
"requires": [
|
||||
"postgres-database",
|
||||
"route",
|
||||
@@ -237,6 +241,27 @@
|
||||
"default": [
|
||||
"jschoubben"
|
||||
]
|
||||
},
|
||||
"ask-taken-bound": {
|
||||
"type": "string",
|
||||
"description": "How long the forge module keeps the id of an act of the seat git it took, as a duration such as 10m (design 47 H2 §775): an act given again with that id within it acts not again but answers what the forge holds of it. The largest caller's bound, the pipeline runner's ask-unanswered-bound, by default.",
|
||||
"trusted": false,
|
||||
"runtime": true,
|
||||
"default": "10m"
|
||||
},
|
||||
"token-ttl": {
|
||||
"type": "string",
|
||||
"description": "How old the forge module's token of an account may grow, as a duration such as 24h (design 47 H2 \u00a7808): a token the vault has kept longer than this, by its changed-at, is minted again, one process at a time, and the one it replaces is deleted at the forge.",
|
||||
"trusted": false,
|
||||
"runtime": true,
|
||||
"default": "24h"
|
||||
},
|
||||
"mint-lease": {
|
||||
"type": "string",
|
||||
"description": "How long one process of the forge module may hold the lease to mint a token, as a duration such as 60s (design 47 H2 \u00a7808): the lease key mint in the state token-lease lives this long, so a process that died holding it is replaced after it, and a process that finds it taken waits at most this long.",
|
||||
"trusted": false,
|
||||
"runtime": true,
|
||||
"default": "60s"
|
||||
}
|
||||
},
|
||||
"facts": {
|
||||
@@ -519,9 +544,7 @@
|
||||
"env": {
|
||||
"MESH_GITEA_URL": "http://127.0.0.1:${port:3000}",
|
||||
"MESH_GITEA_TOOLS_USER": "mesh-tools",
|
||||
"MESH_GITEA_TOKEN_FILE": "${dir:runtime-state}/token",
|
||||
"MESH_GITEA_PACKAGES_USER": "mesh-packages",
|
||||
"MESH_GITEA_PACKAGES_TOKEN_FILE": "${dir:runtime-state}/packages-token",
|
||||
"MESH_NPM_OWNER": "novox"
|
||||
}
|
||||
},
|
||||
@@ -538,7 +561,6 @@
|
||||
"env": {
|
||||
"MESH_GITEA_URL": "http://127.0.0.1:${port:3000}",
|
||||
"MESH_GITEA_TOOLS_USER": "mesh-tools",
|
||||
"MESH_GITEA_TOKEN_FILE": "${dir:runtime-state}/token",
|
||||
"MESH_GITEA_PATCH_DIR": "${dir:forge-patches}"
|
||||
}
|
||||
},
|
||||
@@ -616,25 +638,51 @@
|
||||
}
|
||||
],
|
||||
"state": [
|
||||
"bound",
|
||||
"announced",
|
||||
"findings",
|
||||
{
|
||||
"name": "bound",
|
||||
"max-mib": 8
|
||||
},
|
||||
{
|
||||
"name": "announced",
|
||||
"max-mib": 8
|
||||
},
|
||||
{
|
||||
"name": "findings",
|
||||
"max-mib": 8
|
||||
},
|
||||
{
|
||||
"name": "first-views",
|
||||
"history": 1
|
||||
"history": 1,
|
||||
"max-mib": 8
|
||||
},
|
||||
{
|
||||
"name": "transfers",
|
||||
"max-mib": 8
|
||||
},
|
||||
"transfers",
|
||||
{
|
||||
"name": "idle",
|
||||
"key-ttl": true
|
||||
"key-ttl": true,
|
||||
"max-mib": 8
|
||||
},
|
||||
{
|
||||
"name": "failed-steps",
|
||||
"history": 1
|
||||
"history": 1,
|
||||
"max-mib": 8
|
||||
},
|
||||
{
|
||||
"name": "calls",
|
||||
"history": 1
|
||||
"history": 1,
|
||||
"max-mib": 8
|
||||
},
|
||||
{
|
||||
"name": "taken",
|
||||
"key-ttl": true,
|
||||
"max-mib": 8
|
||||
},
|
||||
{
|
||||
"name": "token-lease",
|
||||
"key-ttl": true,
|
||||
"max-mib": 1
|
||||
}
|
||||
],
|
||||
"reads": [
|
||||
@@ -649,7 +697,9 @@
|
||||
"seat:mesh-catalog.revision",
|
||||
"seat:mesh-catalog.registrations",
|
||||
"seat:mesh-hand-acts.hand-act",
|
||||
"seat:mesh-builds.reserved"
|
||||
"seat:mesh-builds.reserved",
|
||||
"seat:mesh-vault.own-put",
|
||||
"seat:mesh-vault.get"
|
||||
],
|
||||
"consumes": [
|
||||
"mesh-catalog.catalog-revision",
|
||||
@@ -659,7 +709,8 @@
|
||||
"git.pull-updated",
|
||||
"git.pull-closed",
|
||||
"git.pull-merged",
|
||||
"mesh-conditions.findings-asked"
|
||||
"mesh-conditions.findings-asked",
|
||||
"mesh-vault.secret-changed"
|
||||
],
|
||||
"streams": [
|
||||
{
|
||||
@@ -670,7 +721,7 @@
|
||||
"findings-stated.*"
|
||||
],
|
||||
"max-age-days": 7,
|
||||
"max-mib": 16
|
||||
"max-mib": 8
|
||||
}
|
||||
],
|
||||
"objects": [
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/node": "^22.0.0",
|
||||
"tweetnacl": "1.0.3",
|
||||
"typescript": "^5.6.0"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,28 +9,25 @@
|
||||
// makes `mesh-tools`; its password random, sent once and kept nowhere.
|
||||
// - **the team**: `mesh-packages` in the organisation that owns the registry, writing packages and nothing else:
|
||||
// no code, no issues, no pull requests, no administration.
|
||||
// - **the token**: minted for the account by the administrator's login with the one scope `write:package`, kept at
|
||||
// 0600 in the module's runtime state as `packages-token`, its scopes line beside it saying whose it is
|
||||
// (MintedToken: one mint at a time across the bundle's processes, re-minted when the kept one is gone or other).
|
||||
// - **the token**: minted for the account by the administrator's login with the one scope `write:package`, kept in
|
||||
// the vault as the module's own `module/gitea/mesh-packages` (design 47 H2 §808, H-A-422, H-A-426; MintedToken: one
|
||||
// mint at a time across the bundle's processes, a name per mint, re-minted when refused or older than token-ttl).
|
||||
//
|
||||
// The watcher mints it at its start (keepPackagesToken). No timer looks at it after that: the Go bundle mints nothing,
|
||||
// and when the forge refuses its write it raises the finding PACKAGES_TOKEN_REFUSED in the module's findings state; the
|
||||
// watcher, watching that key, looks again (a refused token is minted afresh) and clears the finding (renewOnRefusal).
|
||||
|
||||
import { randomBytes } from "node:crypto";
|
||||
import { join } from "node:path";
|
||||
import { MintedToken } from "./token.js";
|
||||
import { adminFromEnv, MintedToken } from "./token.js";
|
||||
import { TOOLS_ORG, type ToolsForge } from "./tools-account.js";
|
||||
|
||||
/** The account the package seats write as (internal/forgehttp PackagesAccount). */
|
||||
export const PACKAGES_ACCOUNT = "mesh-packages";
|
||||
/** Its team in the registry's organisation, and the token's name in the account's list. */
|
||||
/** Its team in the registry's organisation, and the token's fixed name in the account's list before a name per mint. */
|
||||
export const PACKAGES_TEAM = "mesh-packages";
|
||||
export const PACKAGES_TOKEN_NAME = "mesh-packages";
|
||||
/** The one scope the write side needs; reads go with the tools' token. */
|
||||
export const PACKAGES_TOKEN_SCOPES: readonly string[] = ["write:package"];
|
||||
/** Where the token is kept in the module's runtime state (the manifest's MESH_GITEA_PACKAGES_TOKEN_FILE). */
|
||||
export const PACKAGES_TOKEN_FILE = "packages-token";
|
||||
|
||||
/** What the team grants: write on the organisation's packages, and no more. */
|
||||
export const PACKAGES_TEAM_UNITS = {
|
||||
@@ -47,26 +44,19 @@ export async function setUpPackagesAccount(forge: ToolsForge, account = PACKAGES
|
||||
}
|
||||
|
||||
/** The packages account's token, minted by the administrator's login from what the manifest hands the watcher. */
|
||||
export function packagesToken(url: string, env: NodeJS.ProcessEnv = process.env, log?: (line: string) => void): MintedToken {
|
||||
const admin = env.MESH_GITEA_ADMIN_USER;
|
||||
const passwordFile = env.MESH_GITEA_ADMIN_PASSWORD_FILE;
|
||||
const stateDir = env.MESH_GITEA_STATE_DIR;
|
||||
const missing = [
|
||||
admin ? null : "MESH_GITEA_ADMIN_USER",
|
||||
passwordFile ? null : "MESH_GITEA_ADMIN_PASSWORD_FILE",
|
||||
stateDir ? null : "MESH_GITEA_STATE_DIR",
|
||||
].filter((v): v is string => v !== null);
|
||||
if (missing.length) throw new Error(`the package seats' write token cannot be minted: ${missing.join(", ")} not set by the manifest`);
|
||||
export function packagesToken(url: string, env: NodeJS.ProcessEnv = process.env, log?: (line: string) => void,
|
||||
over: Partial<Pick<ConstructorParameters<typeof MintedToken>[0], "vault" | "lease" | "fetch" | "now">> = {}): MintedToken {
|
||||
const { admin, passwordFile } = adminFromEnv(env, "the package seats' write token cannot be minted:");
|
||||
const account = env.MESH_GITEA_PACKAGES_USER || PACKAGES_ACCOUNT;
|
||||
if (account === admin) throw new Error(`MESH_GITEA_PACKAGES_USER names the admin account "${admin}"; the package seats write as an account of their own`);
|
||||
const base = over.vault && over.lease ? {} : MintedToken.settingsFromEnv(env);
|
||||
return new MintedToken({
|
||||
url, admin: admin!, passwordFile: passwordFile!, file: join(stateDir!, PACKAGES_TOKEN_FILE), account,
|
||||
name: PACKAGES_TOKEN_NAME, scopes: PACKAGES_TOKEN_SCOPES, log,
|
||||
...base, ...over, url, admin, passwordFile, account, oldName: PACKAGES_TOKEN_NAME, scopes: PACKAGES_TOKEN_SCOPES, log,
|
||||
setUp: async (password) => {
|
||||
const { GiteaAdmin } = await import("./client.js");
|
||||
await setUpPackagesAccount(new GiteaAdmin(url, admin!, password), account, () => randomBytes(24).toString("base64url"));
|
||||
await setUpPackagesAccount(new GiteaAdmin(url, admin, password), account, () => randomBytes(24).toString("base64url"));
|
||||
},
|
||||
});
|
||||
} as ConstructorParameters<typeof MintedToken>[0]);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -4,11 +4,17 @@
|
||||
// two ways, both through the seat `mesh-catalog`: its verb `registrations` asked at the start (a restart after the first
|
||||
// registrations were written), and its event `registration-changed` (the first being written, or heard again from the
|
||||
// seat's kept stream). Neither is the holder's state or a module named.
|
||||
//
|
||||
// **A plain merge needs a view read since the last change heard** (design 47 H2 H-A-471, second addendum): the holder keeps
|
||||
// the time of its last read of the watched branches and of the last registration-changed it heard; the view is current only
|
||||
// when a read asked after that change has answered. A read that fails is said and asked again with a growing wait until it
|
||||
// answers, so an unread view is never taken as "no watched branch".
|
||||
|
||||
import {
|
||||
seat as CATALOG, verbs as CATALOG_VERBS, events as CATALOG_EVENTS,
|
||||
type RegistrationChangedEvent, type RegistrationsAnswer, type RegistrationsInput,
|
||||
} from "@novox/mesh-delivery-contracts-mesh-catalog";
|
||||
import { bindWait } from "./bind.js";
|
||||
|
||||
/** The seat verb asked, as the manifest's `invokes` names it. */
|
||||
export const REGISTRATIONS_VERB = `seat:${CATALOG}.${CATALOG_VERBS.registrations}`;
|
||||
@@ -18,8 +24,18 @@ export const REGISTRATION_CHANGED_EVENT = `${CATALOG}.${CATALOG_EVENTS.registrat
|
||||
export class Registrations {
|
||||
private first = false;
|
||||
private byRepository = new Map<string, Set<string>>();
|
||||
/** The registration-changed events heard; the count a read was asked at, once it answered (-1: never read). */
|
||||
private changes = 0;
|
||||
private readAtChange = -1;
|
||||
/** The reads asked, and the latest whose answer is the view (an older answer landing later is not taken). */
|
||||
private asked = 0;
|
||||
private applied = 0;
|
||||
private lastRead?: Date;
|
||||
private lastChange?: Date;
|
||||
constructor(private readonly ask: (verb: string, input: RegistrationsInput) => Promise<unknown>,
|
||||
private readonly log: (line: string) => void = () => {}) {}
|
||||
private readonly log: (line: string) => void = () => {},
|
||||
private readonly sleep: (ms: number) => Promise<void> = (ms) => new Promise((r) => setTimeout(r, ms)),
|
||||
private readonly now: () => Date = () => new Date()) {}
|
||||
|
||||
/** Whether the first registrations are written: the one fact the merge switches on. */
|
||||
get written(): boolean {
|
||||
@@ -31,6 +47,19 @@ export class Registrations {
|
||||
return this.byRepository;
|
||||
}
|
||||
|
||||
/** Whether the watched branches were read since the last registration-changed heard (H-A-471, second addendum). */
|
||||
get current(): boolean {
|
||||
return this.readAtChange === this.changes;
|
||||
}
|
||||
|
||||
/** The time the last read that answered was asked, and of the last registration-changed heard. */
|
||||
get readAt(): Date | undefined {
|
||||
return this.lastRead;
|
||||
}
|
||||
get changedAt(): Date | undefined {
|
||||
return this.lastChange;
|
||||
}
|
||||
|
||||
/** One `registration-changed`: the first registration written, however many were before this module started. */
|
||||
hear(ev: RegistrationChangedEvent | undefined): void {
|
||||
if (!ev || typeof ev.module !== "string" || ev.module === "") {
|
||||
@@ -38,25 +67,40 @@ export class Registrations {
|
||||
}
|
||||
if (!this.first) this.log(`the first registration is written (${ev.module}); merge-pull merges from here`);
|
||||
this.first = true;
|
||||
this.changes++;
|
||||
this.lastChange = this.now();
|
||||
}
|
||||
|
||||
/** The start's first view, asked again after each `registration-changed`: any registration held is the first written, and its
|
||||
* watched branches are read. A failed ask is said and left to the event. */
|
||||
* watched branches are read. A failed ask is said and asked again with a growing wait until it answers (H-A-471, second
|
||||
* addendum); until then no member is merged plainly. */
|
||||
async start(): Promise<void> {
|
||||
try {
|
||||
let a: unknown = await this.ask(REGISTRATIONS_VERB, {});
|
||||
if (typeof a === "string") a = JSON.parse(a);
|
||||
const r = (a as RegistrationsAnswer | undefined)?.registrations;
|
||||
if (!Array.isArray(r)) throw new Error(`${REGISTRATIONS_VERB} answered no registrations: ${JSON.stringify(a ?? null).slice(0, 200)}`);
|
||||
const by = new Map<string, Set<string>>();
|
||||
for (const x of r) for (const b of x.branches ?? []) (by.get(x.origin) ?? by.set(x.origin, new Set()).get(x.origin)!).add(b);
|
||||
this.byRepository = by;
|
||||
if (r.length > 0) {
|
||||
this.first = true;
|
||||
this.log(`${r.length} registrations are held; merge-pull merges`);
|
||||
for (let attempt = 1; ; attempt++) {
|
||||
const at = this.changes, seq = ++this.asked, askedAt = this.now();
|
||||
try {
|
||||
let a: unknown = await this.ask(REGISTRATIONS_VERB, {});
|
||||
if (typeof a === "string") a = JSON.parse(a);
|
||||
const r = (a as RegistrationsAnswer | undefined)?.registrations;
|
||||
if (!Array.isArray(r)) throw new Error(`${REGISTRATIONS_VERB} answered no registrations: ${JSON.stringify(a ?? null).slice(0, 200)}`);
|
||||
if (seq < this.applied) return;
|
||||
const by = new Map<string, Set<string>>();
|
||||
for (const x of r) for (const b of x.branches ?? []) (by.get(x.origin) ?? by.set(x.origin, new Set()).get(x.origin)!).add(b);
|
||||
this.byRepository = by;
|
||||
this.applied = seq;
|
||||
this.readAtChange = at;
|
||||
this.lastRead = askedAt;
|
||||
if (attempt > 1) this.log(`the registrations are read after ${attempt} attempts`);
|
||||
if (r.length > 0) {
|
||||
this.first = true;
|
||||
this.log(`${r.length} registrations are held; merge-pull merges`);
|
||||
}
|
||||
return;
|
||||
} catch (err) {
|
||||
const wait = bindWait(attempt);
|
||||
this.log(`the registrations could not be read: ${err instanceof Error ? err.message : String(err)}; ` +
|
||||
`no member is merged plainly until they are, asked again in ${wait / 1000}s`);
|
||||
await this.sleep(wait);
|
||||
}
|
||||
} catch (err) {
|
||||
this.log(`the registrations could not be read: ${err instanceof Error ? err.message : String(err)}; merge-pull waits for registration-changed`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+147
-27
@@ -7,7 +7,8 @@
|
||||
//
|
||||
// **A failed read answers an error, never an empty answer**: the forge's 404 is the kind `not-found`, its refusal (another
|
||||
// 4xx, or a refusal of this holder) the kind `refused`, a forge that failed or did not answer the kind `failed`; an empty list or an absent file is answered
|
||||
// only when the forge said so.
|
||||
// only when the forge said so. A 401, 502, 503 or connection fault whose version read fails, or names the forge's store
|
||||
// unavailable, is the kind `away` (design 47 H2 §776; failureOf).
|
||||
//
|
||||
// **The acts carry the caller's id** (status, comment, view, note): an id the pull request or the note already holds is
|
||||
// answered `held` and nothing is done again. `merge-pull` is one merge call of one or more pull requests, every member
|
||||
@@ -21,14 +22,18 @@ import {
|
||||
type TagAnswer, type TagsAnswer, type TreesAnswer, type ViewAnswer,
|
||||
} from "@novox/mesh-delivery-contracts-git";
|
||||
import type { CommitStatus, GiteaClient, GiteaComment, GiteaPull } from "./client.js";
|
||||
import { MAX_FILE_BYTES } from "./client.js";
|
||||
import { DirectoryNotInTree, MAX_FILE_BYTES } from "./client.js";
|
||||
import { containedFrom, newestByContext } from "./checks.js";
|
||||
import { Refused, checkName } from "./guard.js";
|
||||
import { appendNote, viewBody, viewComment, type Runner } from "./delivery.js";
|
||||
import { AdminRefused } from "./token.js";
|
||||
import { awayAnswer } from "@novox/mesh-sdk/callers";
|
||||
import { appendNote, noteHolds, viewBody, viewComment, type Runner } from "./delivery.js";
|
||||
import type { Judged } from "./failedsteps.js";
|
||||
import { mergeCall } from "./mergecall.js";
|
||||
import { mergeCall, membersOf } from "./mergecall.js";
|
||||
import type { Calls, Say } from "./calls.js";
|
||||
import type { Partials } from "./partials.js";
|
||||
import type { Once } from "./taken.js";
|
||||
import { askIdOf } from "@novox/mesh-sdk/callers";
|
||||
|
||||
/** The verbs of the seat, in its definition's order. */
|
||||
export const VERBS: readonly string[] = Object.values(GIT_VERBS);
|
||||
@@ -49,14 +54,17 @@ export interface SeatDeps {
|
||||
registered: () => boolean;
|
||||
/** The failed `required` step that refuses a head, judged from the module's own state `failed-steps` (failedsteps.ts), or null. */
|
||||
judge: (head: string) => Promise<Judged | null>;
|
||||
/** The watched branches the registrations name, by their origin repository (registrations.ts): a member's base is one. */
|
||||
watched: () => ReadonlyMap<string, ReadonlySet<string>>;
|
||||
/** The watched branches the registrations name, by their origin repository (registrations.ts): a member's base is one;
|
||||
* null while they are not read since the last registration-changed heard (H-A-471, second addendum). */
|
||||
watched: () => ReadonlyMap<string, ReadonlySet<string>> | null;
|
||||
/** The state calls (calls.ts): each merge call written before its first merge (design 47 H §766). */
|
||||
calls?: Calls;
|
||||
/** Says merge-called on the seat (mesh-sdk runtime.seatPublish; design 47 H §736). */
|
||||
say?: Say;
|
||||
/** The findings merge-partial (partials.ts; design 47 H §767). */
|
||||
partials?: Partials;
|
||||
/** Takes each act once by its ask id (taken.ts; design 47 H2 §775): absent, an act acts at every call. */
|
||||
once?: Once;
|
||||
}
|
||||
|
||||
/** A failure as the contract says it: the forge's or this holder's refusal, or a forge that failed. */
|
||||
@@ -64,10 +72,46 @@ export function failure(err: unknown): { error: string; "error-kind": ErrorKind
|
||||
const text = err instanceof Error ? err.message : String(err);
|
||||
// The forge's 404: what was asked about (a repository, a commit, a pull request, a branch, a tag) does not exist.
|
||||
if (/^Gitea API [^:]*: 404 /.test(text)) return { error: text, "error-kind": "not-found" };
|
||||
// A directory of `files` the commit does not hold (client.ts commitTrees): not-found, the error naming the directory, so the
|
||||
// indexer reads the others alone and leaves the gone one out (novox/hq design 47 H H-A-232).
|
||||
if (err instanceof DirectoryNotInTree) return { error: text, "error-kind": "not-found" };
|
||||
const refused = err instanceof Refused || /^Gitea API [^:]*: 4\d\d /.test(text) || /^Gitea API: /.test(text);
|
||||
return { error: text, "error-kind": refused ? "refused" : "failed" };
|
||||
}
|
||||
|
||||
/** The forge's own version read, which tells the forge away from its refusal (client.ts forgeVersion). */
|
||||
export type VersionRead = Pick<GiteaClient, "forgeVersion">;
|
||||
|
||||
/**
|
||||
* Which failure may mean the forge or its store is away (design 47 H2 §776): a 401 (or the admin account refused at the
|
||||
* mint the 401 asked), a 502 or 503, or a connection fault (no answer, or an answer cut at its limit); else null.
|
||||
*/
|
||||
function awayCause(err: unknown): "unauthorized" | "unavailable" | null {
|
||||
if (err instanceof AdminRefused) return "unauthorized";
|
||||
const text = err instanceof Error ? err.message : String(err);
|
||||
if (/^Gitea [^:]*: 401 /.test(text)) return "unauthorized";
|
||||
if (/^Gitea [^:]*: 50[23] /.test(text)) return "unavailable";
|
||||
if (/^Gitea API [^:]*: no answer in /.test(text)) return "unavailable";
|
||||
if (err instanceof TypeError && /fetch failed/.test(text)) return "unavailable";
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* A failure as the contract says it, the forge's own version read deciding a 401, a 502, a 503 or a connection fault
|
||||
* (design 47 H2 §776): the read failing, or naming the forge's store unavailable, answers `away`, which every caller reads
|
||||
* as a transport fault; the read passing, a 401 is the forge refusing the mesh's admin account (the client minted a
|
||||
* fresh token on the first 401 and was refused again, token.ts), never worded as an account to create; else as failure.
|
||||
*/
|
||||
export async function failureOf(forge: VersionRead, err: unknown): Promise<{ error: string; "error-kind": ErrorKind; part?: string; words?: string }> {
|
||||
const cause = awayCause(err);
|
||||
if (cause === null) return failure(err);
|
||||
const text = err instanceof Error ? err.message : String(err);
|
||||
const read = await forge.forgeVersion();
|
||||
if (read.ok === false) return awayAnswer(read.store ? "the forge's store" : "the forge", `${text}; ${read.words}`);
|
||||
if (cause === "unauthorized") return { error: `the forge refuses the mesh's admin account: ${text}`, "error-kind": "refused" };
|
||||
return failure(err);
|
||||
}
|
||||
|
||||
/** owner/name of a `repository` argument, each checked as a name (guard.ts). */
|
||||
export function repositoryOf(raw: unknown): { owner: string; repo: string; full: string } {
|
||||
const parts = String(raw ?? "").split("/");
|
||||
@@ -77,8 +121,10 @@ export function repositoryOf(raw: unknown): { owner: string; repo: string; full:
|
||||
}
|
||||
|
||||
const SHA = /^[0-9a-f]{40}$/;
|
||||
/** The git tag of a released Go version (design 51; design 47 §370). */
|
||||
const GO_TAG = /^go\/v\d+\.\d+\.\d+$/;
|
||||
/** The git tag of a released version, <directory>/vMAJOR.MINOR.PATCH (design 47 H2 §796; design 47 §370). */
|
||||
const VERSION_TAG = /^(?:[A-Za-z0-9_][A-Za-z0-9._-]*\/)+v\d+\.\d+\.\d+$/;
|
||||
/** The form of the seat git's values and answers this holder answers in (design 47 H2 §780, §796). */
|
||||
const GIT_FORM = 3;
|
||||
const STATES = new Set(["pending", "success", "failure", "error", "warning"]);
|
||||
function commitOf(name: string, raw: unknown): string {
|
||||
const sha = String(raw ?? "").trim();
|
||||
@@ -130,18 +176,24 @@ async function mergingOf(gitea: GiteaClient, owner: string, repo: string, p: Git
|
||||
|
||||
/** The seat's verbs as tools `git.<verb>`, each answering in the seat's contract. */
|
||||
export function seatTools(gitea: GiteaClient, deps: SeatDeps): ToolDefinition[] {
|
||||
const verb = (name: string, description: string, act: (args: Record<string, unknown>) => Promise<object>): ToolDefinition => ({
|
||||
const answered = async (args: Record<string, unknown>, act: (args: Record<string, unknown>) => Promise<object>): Promise<object> => {
|
||||
try {
|
||||
return await act(args);
|
||||
} catch (err) {
|
||||
return failureOf(gitea, err);
|
||||
}
|
||||
};
|
||||
// An act given `standing`, what it did read afresh, is taken once by its ask id (§775, H-A-381, H-A-400).
|
||||
const verb = (name: string, description: string, act: (args: Record<string, unknown>) => Promise<object>,
|
||||
standing?: (args: Record<string, unknown>) => Promise<object>): ToolDefinition => ({
|
||||
name: `${GIT}.${name}`,
|
||||
description,
|
||||
input: {},
|
||||
run: async (args: Record<string, unknown>) => {
|
||||
try {
|
||||
return await act(args);
|
||||
} catch (err) {
|
||||
return failure(err);
|
||||
}
|
||||
},
|
||||
run: async (args: Record<string, unknown>) => standing && deps.once
|
||||
? deps.once.run(askIdOf(args), () => answered(args, act), () => answered(args, standing))
|
||||
: answered(args, act),
|
||||
});
|
||||
const s = standingActs(gitea, deps);
|
||||
|
||||
return [
|
||||
verb(GIT_VERBS.pull, "The seat git's pull: one pull request by its repository and number or by its merge commit, and for an open one what the forge would merge now.",
|
||||
@@ -175,7 +227,7 @@ export function seatTools(gitea: GiteaClient, deps: SeatDeps): ToolDefinition[]
|
||||
}));
|
||||
return { pulls };
|
||||
}),
|
||||
verb(GIT_VERBS.tags, "The seat git's tags: a repository's tags, a page at a time, newest first; more when another page exists.",
|
||||
verb(GIT_VERBS.tags, "The seat git's tags: a repository's tags, each with the commit it names, a page at a time, newest first; more when another page exists.",
|
||||
async (args): Promise<TagsAnswer> => {
|
||||
const { owner, repo, full } = repositoryOf(args.repository);
|
||||
const page = args.page === undefined ? 1 : Number(args.page);
|
||||
@@ -183,8 +235,8 @@ export function seatTools(gitea: GiteaClient, deps: SeatDeps): ToolDefinition[]
|
||||
// One more than the page is asked, so whether another page exists is the forge's answer, not a guess.
|
||||
const got = await gitea.tags(owner, repo, page, TAGS_PAGE + 1);
|
||||
const more = got.length > TAGS_PAGE;
|
||||
const tags = got.slice(0, TAGS_PAGE).filter((t) => t.startsWith(prefix));
|
||||
return { repository: full, page, bound: TAGS_PAGE, tags, ...(more ? { more } : {}) };
|
||||
const tags = got.slice(0, TAGS_PAGE).filter((t) => t.tag.startsWith(prefix));
|
||||
return { form: GIT_FORM, repository: full, page, bound: TAGS_PAGE, tags, ...(more ? { more } : {}) };
|
||||
}),
|
||||
verb(GIT_VERBS.trees, "The seat git's trees: the trees of one commit, with files and paths when asked; a listing the forge cut is refused.",
|
||||
async (args): Promise<TreesAnswer> => {
|
||||
@@ -232,7 +284,7 @@ export function seatTools(gitea: GiteaClient, deps: SeatDeps): ToolDefinition[]
|
||||
await gitea.setCommitStatus(owner, repo, commit, { state: state as CommitStatus["state"], context: String(args.context ?? ""),
|
||||
description: String(args.description ?? ""), ...(args.target ? { target_url: String(args.target) } : {}) });
|
||||
return { id, performed: true };
|
||||
}),
|
||||
}, s.status),
|
||||
verb(GIT_VERBS.comment, "The seat git's comment: an act of the forge bridge, a comment on a pull request carrying the act's id; an id it already holds is held.",
|
||||
async (args): Promise<CommentAnswer> => {
|
||||
const id = idOf(args.id);
|
||||
@@ -241,7 +293,7 @@ export function seatTools(gitea: GiteaClient, deps: SeatDeps): ToolDefinition[]
|
||||
if (holds(await gitea.listComments(owner, repo, number), id)) return { id, held: true, performed: true };
|
||||
await gitea.addComment(owner, repo, number, `${String(args.text ?? "")}\n\n${actMarker(id)}`);
|
||||
return { id, performed: true };
|
||||
}),
|
||||
}, s.comment),
|
||||
verb(GIT_VERBS.view, "The seat git's view: an act of the forge bridge, the delivery's one view comment kept on a pull request; an id it already holds is held.",
|
||||
async (args): Promise<ViewAnswer> => {
|
||||
const id = idOf(args.id);
|
||||
@@ -253,7 +305,7 @@ export function seatTools(gitea: GiteaClient, deps: SeatDeps): ToolDefinition[]
|
||||
if (existing) await gitea.editComment(owner, repo, existing.id, body);
|
||||
else await gitea.addComment(owner, repo, number, body);
|
||||
return { id, performed: true };
|
||||
}),
|
||||
}, s.view),
|
||||
verb(GIT_VERBS.note, "The seat git's note: an act of the forge bridge, one line on a commit's note under refs/notes/mesh-plan carrying the act's id; an id it already holds is held.",
|
||||
async (args): Promise<NoteAnswer> => {
|
||||
const id = idOf(args.id);
|
||||
@@ -261,24 +313,92 @@ export function seatTools(gitea: GiteaClient, deps: SeatDeps): ToolDefinition[]
|
||||
const commit = commitOf("commit", args.commit);
|
||||
const said = await appendNote(deps.run, deps.container, owner, repo, commit, "mesh-plan", `${String(args.line ?? "")} [${id}]`, id);
|
||||
return said.added ? { id, performed: true } : { id, held: true, performed: true };
|
||||
}),
|
||||
}, s.note),
|
||||
verb(GIT_VERBS.mergePull, "The seat git's merge-pull: one merge call, a delivery's start and its group: the pull requests named (pulls, each {repository, number, head}) and by (who@node); every member is judged before any is merged (still its head, its base a watched branch, no required step failed at its fingerprint), none merged when one is refused, then each merged in the order named; it answers the call and each member's outcome.",
|
||||
async (args): Promise<MergePullAnswer> => mergeCall(gitea, args, deps)),
|
||||
verb(GIT_VERBS.tag, "The seat git's tag: the git tag go/vMAJOR.MINOR.PATCH of a released version, written at the commit its build was made from; a tag that is at that commit is performed.",
|
||||
async (args): Promise<MergePullAnswer> => mergeCall(gitea, args, deps), s.mergePull),
|
||||
verb(GIT_VERBS.tag, "The seat git's tag: the git tag <directory>/vMAJOR.MINOR.PATCH of a released version, written at the commit its build was made from; a tag that is at that commit is performed.",
|
||||
async (args): Promise<TagAnswer> => {
|
||||
const { owner, repo } = repositoryOf(args.repository);
|
||||
const tag = String(args.tag ?? "");
|
||||
if (!GO_TAG.test(tag)) throw new Refused(`${JSON.stringify(tag)} is not go/vMAJOR.MINOR.PATCH`);
|
||||
if (!VERSION_TAG.test(tag)) throw new Refused(`${JSON.stringify(tag)} is not <directory>/vMAJOR.MINOR.PATCH`);
|
||||
const commit = commitOf("commit", args.commit);
|
||||
const at = await gitea.tagCommit(owner, repo, tag);
|
||||
if (at === commit) return { tag, performed: true };
|
||||
if (at !== null) throw new Refused(`${tag} is at ${at.slice(0, 12)} already, not at ${commit.slice(0, 12)}; a tag is never moved`);
|
||||
await gitea.createTag(owner, repo, tag, commit);
|
||||
return { tag, performed: true };
|
||||
}),
|
||||
}, s.tag),
|
||||
];
|
||||
}
|
||||
|
||||
function holds(comments: GiteaComment[], id: string): boolean {
|
||||
return comments.some((c) => String(c.body ?? "").includes(actMarker(id)));
|
||||
}
|
||||
|
||||
/**
|
||||
* What each act did, read afresh from the forge, for an ask given again after its act ended (design 47 H2 §775; H-A-381
|
||||
* (1)): the act is never performed twice, and what the forge holds is what the act answers when done. A repeat for which
|
||||
* the forge holds nothing of the act is a refusal naming it.
|
||||
*/
|
||||
function standingActs(gitea: GiteaClient, deps: SeatDeps) {
|
||||
const never = (ask: string, what: string) => new Refused(`the ask ${ask} was taken and its act acted, but ${what}: answered as it stands, never acted again`);
|
||||
const askOf = (args: Record<string, unknown>) => askIdOf(args);
|
||||
return {
|
||||
status: async (args: Record<string, unknown>): Promise<StatusAnswer> => {
|
||||
const id = idOf(args.id);
|
||||
const { owner, repo } = repositoryOf(args.repository);
|
||||
const commit = commitOf("commit", args.commit);
|
||||
const context = String(args.context ?? ""), state = String(args.state ?? "");
|
||||
const now = newestByContext((await gitea.combinedStatus(owner, repo, commit)).statuses).find((x) => x.context === context);
|
||||
if (!now || now.state !== state) throw never(askOf(args), `${commit.slice(0, 12)} carries no ${context} ${state}`);
|
||||
return { id, held: true, performed: true };
|
||||
},
|
||||
comment: async (args: Record<string, unknown>): Promise<CommentAnswer> => {
|
||||
const id = idOf(args.id);
|
||||
const { owner, repo, full } = repositoryOf(args.repository);
|
||||
const number = numberOf(args.number);
|
||||
if (!holds(await gitea.listComments(owner, repo, number), id)) throw never(askOf(args), `${full}#${number} holds no comment of the act ${id}`);
|
||||
return { id, held: true, performed: true };
|
||||
},
|
||||
view: async (args: Record<string, unknown>): Promise<ViewAnswer> => {
|
||||
const id = idOf(args.id);
|
||||
const { owner, repo, full } = repositoryOf(args.repository);
|
||||
const number = numberOf(args.number);
|
||||
const existing = viewComment(await gitea.listComments(owner, repo, number));
|
||||
if (!existing?.body.includes(actMarker(id))) throw never(askOf(args), `${full}#${number}'s view is not the act ${id}'s`);
|
||||
return { id, held: true, performed: true };
|
||||
},
|
||||
note: async (args: Record<string, unknown>): Promise<NoteAnswer> => {
|
||||
const id = idOf(args.id);
|
||||
const { owner, repo } = repositoryOf(args.repository);
|
||||
const commit = commitOf("commit", args.commit);
|
||||
if (!(await noteHolds(deps.run, deps.container, owner, repo, commit, "mesh-plan", id))) {
|
||||
throw never(askOf(args), `the note on ${commit.slice(0, 12)} holds no line of the act ${id}`);
|
||||
}
|
||||
return { id, held: true, performed: true };
|
||||
},
|
||||
mergePull: async (args: Record<string, unknown>): Promise<MergePullAnswer> => {
|
||||
// Each member as the forge holds it: merged, with its merge commit; the call that merged them, when one is kept.
|
||||
const members = membersOf(args.pulls);
|
||||
const out = await Promise.all(members.map(async (m) => {
|
||||
const p = await gitea.getPullRequest(m.owner, m.repo, m.number);
|
||||
return p.merged && p.merge_commit_sha
|
||||
? { repository: m.repository, number: m.number, state: "merged" as const, commit: p.merge_commit_sha }
|
||||
: { repository: m.repository, number: m.number, state: "refused" as const, why: `not merged: the forge holds it ${p.state === "open" ? "open" : "closed"}` };
|
||||
}));
|
||||
if (!out.some((m) => m.state === "merged")) throw never(askOf(args), "the forge merged none of its pull requests");
|
||||
const named = (c: { members: { repository: string; number: number; state: string }[] }) =>
|
||||
out.every((m) => m.state !== "merged" || c.members.some((k) => k.repository === m.repository && k.number === m.number && k.state === "merged"));
|
||||
const kept = (await deps.calls?.all())?.find(named);
|
||||
return { ...(kept ? { call: kept.call } : {}), members: out };
|
||||
},
|
||||
tag: async (args: Record<string, unknown>): Promise<TagAnswer> => {
|
||||
const { owner, repo } = repositoryOf(args.repository);
|
||||
const tag = String(args.tag ?? "");
|
||||
const commit = commitOf("commit", args.commit);
|
||||
const at = await gitea.tagCommit(owner, repo, tag);
|
||||
if (at !== commit) throw never(askOf(args), at === null ? `the forge holds no tag ${tag}` : `${tag} is at ${at.slice(0, 12)}, not ${commit.slice(0, 12)}`);
|
||||
return { tag, performed: true };
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
@@ -0,0 +1,107 @@
|
||||
// **The forge module takes an act of the seat git once by its ask id** (novox/hq design 47 H2 §775; H-A-381, H-A-400): a
|
||||
// kept call carries its ask id (the runtime names it in the call's `_meta`, mesh-sdk askIdOf), and the forge module keeps
|
||||
// each id it took in its state `taken`, key `git.<ask>`, for `ask-taken-bound` (a setting, default 10 minutes, the
|
||||
// largest caller's bound). An ask given again while its act runs joins that act and gets its answer; given again after
|
||||
// the act ended, it answers the act's state as it stands, read afresh from the forge (a merge call answers each pull
|
||||
// request as it is: merged, with its merge commit), and the act is never performed twice. No answer is kept in `taken`.
|
||||
// The key is kept only for an act that acted: an answer that performed nothing (away, a transport fault, a refusal
|
||||
// before any act) drops it, so the kept call's repeat under the same id acts (H-A-400 (1)). A repeat for which the
|
||||
// forge's state says the act that acted never happened is a refusal naming it.
|
||||
|
||||
import { takeOnce, takenKey, TAKEN_STATE, type State } from "@novox/mesh-sdk/state";
|
||||
|
||||
/** The seat the forge module takes asks of. */
|
||||
export const SEAT = "git";
|
||||
|
||||
/** ask-taken-bound's default (H-A-381 (2)). */
|
||||
export const ASK_TAKEN_BOUND_DEFAULT = "10m";
|
||||
|
||||
/** A duration as the mesh writes one ("10m", "1h30m", "45s", "500ms"), in milliseconds; null when it is none. */
|
||||
export function durationMs(text: string): number | null {
|
||||
const s = text.trim();
|
||||
if (!/^(\d+(\.\d+)?(ms|h|m|s))+$/.test(s)) return null;
|
||||
let ms = 0;
|
||||
for (const [, n, , unit] of s.matchAll(/(\d+(\.\d+)?)(ms|h|m|s)/g)) {
|
||||
ms += Number(n) * ({ ms: 1, s: 1_000, m: 60_000, h: 3_600_000 } as Record<string, number>)[unit];
|
||||
}
|
||||
return ms > 0 ? ms : null;
|
||||
}
|
||||
|
||||
/** ask-taken-bound in milliseconds, from the word the node-engine composes for it (H-A-370, H-A-400 (2)); the default when
|
||||
* it is unset. A value that is no duration is said and the default taken. */
|
||||
export function askTakenBoundMs(env: NodeJS.ProcessEnv = process.env, log: (line: string) => void = () => {}): number {
|
||||
const raw = env.MESH_CONFIG_ASK_TAKEN_BOUND;
|
||||
const fallback = durationMs(ASK_TAKEN_BOUND_DEFAULT)!;
|
||||
if (raw === undefined || raw.trim() === "") return fallback;
|
||||
const ms = durationMs(raw);
|
||||
if (ms === null) {
|
||||
log(`the setting ask-taken-bound is ${JSON.stringify(raw)}, no duration: the default ${ASK_TAKEN_BOUND_DEFAULT} holds`);
|
||||
return fallback;
|
||||
}
|
||||
return ms;
|
||||
}
|
||||
|
||||
/** The state taken, as the forge module reaches it: take an ask once, drop a key whose act performed nothing. */
|
||||
export interface Taken {
|
||||
/** Whether this holder takes the ask now (mesh-sdk takeOnce: created, refused when the key exists). */
|
||||
take(ask: string): Promise<boolean>;
|
||||
/** Drops the key of an ask whose act performed nothing, so its repeat acts. */
|
||||
drop(ask: string): Promise<void>;
|
||||
}
|
||||
|
||||
/** The module's state taken over the runtime, its keys living ask-taken-bound. */
|
||||
export function takenState(st: Pick<State<unknown>, "delete">, boundMs: number): Taken {
|
||||
return {
|
||||
take: (ask) => takeOnce(SEAT, ask, boundMs, TAKEN_STATE),
|
||||
drop: (ask) => st.delete(takenKey(SEAT, ask)),
|
||||
};
|
||||
}
|
||||
|
||||
/** Whether an act's answer performed nothing: an error answer, or a merge call that merged no member. */
|
||||
export function performedNothing(answer: object): boolean {
|
||||
const a = answer as Record<string, unknown>;
|
||||
if (a["error-kind"] !== undefined) return true;
|
||||
if (Array.isArray(a.members)) return !a.members.some((m: any) => m?.state === "merged");
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Takes an act once by its ask id: `act` runs for an ask this holder takes now; an ask given while its act runs joins it;
|
||||
* an ask taken before whose act ended answers `standing`, the act's state read afresh. An act with no ask id acts as
|
||||
* before. The taken key is dropped when the act performed nothing.
|
||||
*/
|
||||
export class Once {
|
||||
private readonly running = new Map<string, Promise<object>>();
|
||||
|
||||
constructor(private readonly taken: Taken, private readonly log: (line: string) => void = () => {}) {}
|
||||
|
||||
async run(ask: string, act: () => Promise<object>, standing: () => Promise<object>): Promise<object> {
|
||||
if (!ask) return act();
|
||||
const joined = this.running.get(ask);
|
||||
if (joined) return joined;
|
||||
if (!(await this.taken.take(ask))) {
|
||||
// Taken before, and its act ended (or another process runs it): what it did, read afresh, never done again.
|
||||
return this.running.get(ask) ?? standing();
|
||||
}
|
||||
const acting = (async () => {
|
||||
let answer: object;
|
||||
try {
|
||||
answer = await act();
|
||||
} catch (err) {
|
||||
await this.taken.drop(ask).catch(() => {});
|
||||
throw err;
|
||||
}
|
||||
if (performedNothing(answer)) {
|
||||
await this.taken.drop(ask).catch((err) =>
|
||||
this.log(`the ask ${ask} performed nothing and its taken key could not be dropped, so its repeat is answered as it stands — ${err instanceof Error ? err.message : err}`));
|
||||
}
|
||||
return answer;
|
||||
})();
|
||||
this.running.set(ask, acting);
|
||||
try {
|
||||
return await acting;
|
||||
} finally {
|
||||
this.running.delete(ask);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -91,6 +91,15 @@ test("a call says one merge-called of what landed; none for a call that landed n
|
||||
assert.deepEqual(said.map((s) => s.body.members.map((x: any) => x.number)), [[1, 2]]);
|
||||
assert.equal(said[0].body.refused, undefined, "a member not merged after a stop is no refused member");
|
||||
assert.deepEqual(await sayUnsaid(calls, say), [], "said once");
|
||||
// A member merged plainly, its repository having no watched branch, starts no delivery: the start's re-say leaves it
|
||||
// out by the kept note (design 47 H2 H-A-471 addendum (2)), and a call of such members alone says none.
|
||||
const noted = { ...m(4, "merged"), repository: "novox/hq", note: "no delivery: the repository has no watched branch" };
|
||||
await calls.put("00000000000000dd", { by: "lead@shanks", account: "x", at: "2026-11-20T03:02:00Z", members: [noted, m(5, "merged")], said: false } as any);
|
||||
await calls.put("00000000000000ee", { by: "lead@shanks", account: "x", at: "2026-11-20T03:03:00Z", members: [noted], said: false } as any);
|
||||
said.length = 0;
|
||||
assert.deepEqual(await sayUnsaid(calls, say), ["00000000000000dd"]);
|
||||
assert.deepEqual(said.map((s) => s.body.members.map((x: any) => x.number)), [[5]]);
|
||||
assert.equal((await calls.get("00000000000000ee"))?.said, true, "kept said, with nothing to say");
|
||||
});
|
||||
|
||||
// The merge calls' tools (design 47 H §766): forge_calls answers the calls newest first with each member's state, and
|
||||
@@ -117,3 +126,27 @@ test("forge_calls reads the calls newest first; forge_call_say_again says one ag
|
||||
const none: any = await tool("forge_call_say_again").run({ call: "00000000000000bb", why: "x" });
|
||||
assert.equal(none.said, false, "a call that landed none says none");
|
||||
});
|
||||
|
||||
// 47 H2 §812, §811, H-A-402: forge_merged lists a repository's merged pull requests by number, from..to, each with its merge
|
||||
// commit, when, and the merge call that made it; a number the forge does not hold or one not merged is left out; capped at
|
||||
// 768 KB with the next number as the cursor.
|
||||
test("forge_merged lists merged pull requests by number with the merge call that made each", async () => {
|
||||
const { mergedTool, ANSWER_CAP_BYTES } = await import("../dist/tools/calls.js");
|
||||
const sha = (n: number) => String(n % 10).repeat(40);
|
||||
const forge = { getPullRequest: async (_o: string, _r: string, n: number) => {
|
||||
if (n === 3) throw new Error("Gitea API /repos/novox/app/pulls/3: 404 Not Found");
|
||||
return n === 4 ? { number: n, state: "open", merged: false } : { number: n, state: "closed", merged: true, merge_commit_sha: sha(n), merged_at: `2026-11-2${n}T10:00:00Z` };
|
||||
} };
|
||||
const calls = { all: async () => [{ call: "00000000000000aa", members: [{ repository: "novox/app", number: 2, state: "merged" }, { repository: "novox/lib", number: 1, state: "merged" }] }] };
|
||||
const got: any = await mergedTool(forge as any, calls as any).run({ repository: "novox/app", from: 1, to: 5 });
|
||||
assert.deepEqual(got, { repository: "novox/app", from: 1, to: 5, total: 5, merged: [
|
||||
{ number: 1, "merge-commit": sha(1), "merged-at": "2026-11-21T10:00:00Z" },
|
||||
{ number: 2, "merge-commit": sha(2), "merged-at": "2026-11-22T10:00:00Z", call: "00000000000000aa" },
|
||||
{ number: 5, "merge-commit": sha(5), "merged-at": "2026-11-25T10:00:00Z" }] });
|
||||
await assert.rejects(mergedTool(forge as any, calls as any).run({ repository: "novox/app", from: 5, to: 4 }), /to not below from/);
|
||||
// Past 768 KB it is capped, the cursor the next number to read from.
|
||||
const many: any = await mergedTool(forge as any, { all: async () => [] } as any).run({ repository: "novox/app", from: 5, to: 20_000 });
|
||||
assert.equal(many.capped, true);
|
||||
assert.ok(JSON.stringify(many.merged).length <= ANSWER_CAP_BYTES);
|
||||
assert.equal(many.cursor, many.merged.at(-1).number + 1);
|
||||
});
|
||||
|
||||
@@ -50,8 +50,9 @@ async function serve(pages: any[]) {
|
||||
res.end(JSON.stringify(pages[page - 1] ?? { sha: COMMIT, tree: [], truncated: false, page, total_count: 0 }));
|
||||
});
|
||||
await new Promise<void>((r) => server.listen(0, r));
|
||||
const client = new GiteaClient(`http://127.0.0.1:${(server.address() as any).port}`, "t");
|
||||
return { client, server, asked };
|
||||
const url = `http://127.0.0.1:${(server.address() as any).port}`;
|
||||
const client = new GiteaClient(url, "t");
|
||||
return { client, server, asked, url };
|
||||
}
|
||||
|
||||
test("a commit's trees: the root and the tree of each directory holding a module.json", async () => {
|
||||
@@ -238,12 +239,19 @@ test("files: every blob under each asked directory, recursively, as its path rel
|
||||
});
|
||||
|
||||
test("a directory of files the commit lacks, or spelled badly, or a listing the forge cut, fails and answers nothing", async () => {
|
||||
const { client, server } = await serve([{ sha: COMMIT, url: "u", tree: LISTING, truncated: false, page: 1, total_count: LISTING.length }]);
|
||||
const { client, server, url } = await serve([{ sha: COMMIT, url: "u", tree: LISTING, truncated: false, page: 1, total_count: LISTING.length }]);
|
||||
try {
|
||||
await assert.rejects(client.commitTrees("novox", "hq", COMMIT, undefined, ["modules/a", "modules/gone"]), (err: Error) => {
|
||||
assert.match(err.message, /modules\/gone/);
|
||||
// The seat answers it with the kind not-found naming the directory (novox/hq design 47 H H-A-232), never failed: the
|
||||
// holder's own client and failure, as the module runs them.
|
||||
const { failure } = await import("../dist/seat.js");
|
||||
const { GiteaClient } = await import("../dist/client.js");
|
||||
await assert.rejects(new GiteaClient(url, "t").commitTrees("novox", "hq", COMMIT, undefined, ["modules/a", "modules/gone"]), (err: Error) => {
|
||||
const said = failure(err);
|
||||
assert.equal(said["error-kind"], "not-found", JSON.stringify(said));
|
||||
assert.match(said.error, /the directory modules\/gone is not in novox\/hq/);
|
||||
return true;
|
||||
});
|
||||
await assert.rejects(client.commitTrees("novox", "hq", COMMIT, undefined, ["modules/a", "modules/gone"]), /modules\/gone/);
|
||||
await assert.rejects(client.commitTrees("novox", "hq", COMMIT, undefined, ["README.md"]), /README\.md/, "a file is no directory");
|
||||
for (const bad of ["../x", "/abs", "a//b", "modules/a/", "."]) {
|
||||
await assert.rejects(client.commitTrees("novox", "hq", COMMIT, undefined, [bad]), /files/, bad);
|
||||
|
||||
@@ -0,0 +1,96 @@
|
||||
// The bus as the forge module's token needs it, in memory (design 47 H2 §808, §822): the vault seat's `own-put` in its
|
||||
// two sealed steps and its `get` sealed to the asker's key, and the runtime's state with a create-only key that lives
|
||||
// its TTL, as the runtime that launched the bundle answers them. `order` records each own-put and each forge delete a
|
||||
// test also writes into it, so the delete's place after the own-put can be read.
|
||||
|
||||
import nacl from "tweetnacl";
|
||||
import blakejs from "blakejs";
|
||||
import { seal } from "@novox/mesh-sdk/seal";
|
||||
import { useBroker, type Broker } from "@novox/mesh-sdk/messaging";
|
||||
import { useRuntime } from "@novox/mesh-sdk/runtime";
|
||||
import { useStateChannel, type StateChange } from "@novox/mesh-sdk/state";
|
||||
|
||||
export interface Held { value: string; generation: number; changedAt: string }
|
||||
|
||||
export function fakeBus(now: () => Date = () => new Date(), order: string[] = []) {
|
||||
const vault = new Map<string, Held>();
|
||||
const calls = new Map<string, { name: string; secretKey: Uint8Array }>();
|
||||
const kv = new Map<string, { value: unknown; until: number | null }>();
|
||||
const watchers: { state: string; key: string; deliver: (c: StateChange<unknown>) => Promise<void> }[] = [];
|
||||
let revision = 0;
|
||||
const keyOf = (state: string, key: string) => `${state}/${key}`;
|
||||
const live = (k: string) => {
|
||||
const e = kv.get(k);
|
||||
if (e && e.until !== null && e.until <= now().getTime()) kv.delete(k);
|
||||
return kv.get(k);
|
||||
};
|
||||
const tell = async (state: string, key: string, op: "put" | "delete", value?: unknown) => {
|
||||
revision++;
|
||||
for (const w of watchers) if (w.state === state && (w.key === "" || w.key === key)) await w.deliver({ state, key, op, value, revision, current: false });
|
||||
};
|
||||
const broker: Broker = {
|
||||
async request<Req, Res>(key: string, body: Req): Promise<Res> {
|
||||
const b = body as Record<string, string>;
|
||||
if (key === "seat:mesh-vault.own-put" && b.call === undefined) {
|
||||
const keys = nacl.box.keyPair();
|
||||
const call = `call-${calls.size + 1}`;
|
||||
calls.set(call, { name: `module/gitea/${b.name}`, secretKey: keys.secretKey });
|
||||
return { call, seal_to: Buffer.from(keys.publicKey).toString("base64"), secret: `module/gitea/${b.name}` } as Res;
|
||||
}
|
||||
if (key === "seat:mesh-vault.own-put") {
|
||||
const c = calls.get(b.call);
|
||||
if (!c) throw new Error(`no own-put call ${b.call}`);
|
||||
calls.delete(b.call);
|
||||
const boxed = new Uint8Array(Buffer.from(b.sealed, "base64"));
|
||||
const eph = boxed.subarray(0, 32);
|
||||
const pub = nacl.box.keyPair.fromSecretKey(c.secretKey).publicKey;
|
||||
const nonce = blakejs.blake2b(Buffer.concat([eph, pub]), undefined, 24);
|
||||
const opened = nacl.box.open(boxed.subarray(32), nonce, eph, c.secretKey);
|
||||
if (!opened) throw new Error("the sealed value does not open");
|
||||
const generation = (vault.get(c.name)?.generation ?? 0) + 1;
|
||||
vault.set(c.name, { value: Buffer.from(opened).toString("utf8"), generation, changedAt: now().toISOString() });
|
||||
order.push(`own-put ${c.name} ${generation}`);
|
||||
return { name: c.name, generation } as Res;
|
||||
}
|
||||
if (key === "seat:mesh-vault.get") {
|
||||
const held = vault.get(b.name);
|
||||
if (!held) throw new Error(`the vault holds no secret named ${b.name}`);
|
||||
return { name: b.name, generation: held.generation, "changed-at": held.changedAt, sealed: seal(held.value, b.public_key) } as Res;
|
||||
}
|
||||
throw new Error(`not played here: ${key}`);
|
||||
},
|
||||
handle: async () => () => {},
|
||||
publish: async () => {},
|
||||
subscribe: async () => () => {},
|
||||
close: async () => {},
|
||||
};
|
||||
const ask = async (method: string, params: unknown): Promise<unknown> => {
|
||||
const p = params as { state: string; key: string; value?: unknown; ttl_seconds?: number };
|
||||
const k = keyOf(p.state, p.key);
|
||||
switch (method) {
|
||||
case "mesh/state.create":
|
||||
if (live(k)) throw Object.assign(new Error("the key has a value"), { code: -32010 });
|
||||
kv.set(k, { value: p.value, until: p.ttl_seconds ? now().getTime() + p.ttl_seconds * 1000 : null });
|
||||
await tell(p.state, p.key, "put", p.value);
|
||||
return { revision };
|
||||
case "mesh/state.get": {
|
||||
const e = live(k);
|
||||
return e ? { key: p.key, value: e.value, revision } : null;
|
||||
}
|
||||
case "mesh/state.put":
|
||||
kv.set(k, { value: p.value, until: null });
|
||||
await tell(p.state, p.key, "put", p.value);
|
||||
return { revision };
|
||||
case "mesh/state.delete":
|
||||
kv.delete(k);
|
||||
await tell(p.state, p.key, "delete");
|
||||
return null;
|
||||
default:
|
||||
throw new Error(`not played here: ${method}`);
|
||||
}
|
||||
};
|
||||
useBroker(() => broker);
|
||||
useRuntime(ask);
|
||||
useStateChannel({ ask, watch: async (params, deliver) => { const w = params as { state: string; key: string }; watchers.push({ ...w, deliver }); } });
|
||||
return { vault, kv, order, ask };
|
||||
}
|
||||
@@ -474,6 +474,7 @@ test("every read and act of the seat git answers its contract, and an error is a
|
||||
const SHA = "a".repeat(40), OTHER = "b".repeat(40);
|
||||
const gone = () => { throw new Error("Gitea API GET /repos/novox/app/pulls/9: 404 Not Found"); };
|
||||
const down = () => { throw new Error("Gitea API GET /repos/novox/app/x: 502 Bad Gateway"); };
|
||||
const unauthorized = () => { throw new Error("Gitea API /repos/novox/app/pulls/9: 401 Unauthorized"); };
|
||||
const pr = { number: 9, title: "t", state: "open", merged: false, head: "feat/x", head_sha: SHA, base: "main", user: "jochen", html_url: "http://f/9", mergeable: true };
|
||||
const tagged: string[] = [];
|
||||
let comments: { id: number; body: string }[] = [], noteShown = "", added = 0, noteWritten = 0;
|
||||
@@ -483,7 +484,8 @@ test("every read and act of the seat git answers its contract, and an error is a
|
||||
appliedProtection: async () => ({ rule_name: "main", enable_status_check: true, status_check_contexts: ["mesh/check"] }),
|
||||
getRepo: async () => ({ writes: true }),
|
||||
openPulls: async () => [{ owner: "novox", repo: "app", number: 9, base: "main", head: "feat/x", head_sha: SHA, required: ["mesh/check"], checks: {} }],
|
||||
tags: async () => ["go/v0.1.1", "go/v0.1.0", "v1"],
|
||||
forgeVersion: async () => ({ ok: true, version: "1.27.3" }),
|
||||
tags: async () => [{ tag: "go/v0.1.1", commit: SHA }, { tag: "go/v0.1.0", commit: OTHER }, { tag: "v1", commit: SHA }],
|
||||
commitTrees: async () => ({ root: SHA, trees: { "": SHA } }),
|
||||
getFile: async (_o: string, _r: string, path: string) => (path === "gone" ? (() => { throw new Error("Gitea API GET /repos/novox/app/contents/gone: 404 Not Found"); })() : path === "down" ? down() : { path, sha: OTHER, size: 3, content: "abc" }),
|
||||
compare: async () => ({ total_commits: 2, commits: [{ sha: OTHER }, { sha: SHA }] }),
|
||||
@@ -505,11 +507,18 @@ test("every read and act of the seat git answers its contract, and an error is a
|
||||
["pull", "by merge commit", { repository: "novox/app", "merge-commit": OTHER }, {}, (a) => [a.pull.state, a.pull["merge-commit"], a.merging], ["merged", OTHER, undefined]],
|
||||
["pull", "unknown", { repository: "novox/app", number: 404 }, {}, (a) => [a.pull, a["error-kind"]], [undefined, "not-found"]],
|
||||
["pull", "forge down", { repository: "novox/app", number: 9 }, { getPullRequest: down }, (a) => [a.pull, a["error-kind"]], [undefined, "failed"]],
|
||||
// 47 H2 §776: a 401, 502, 503 or connection fault is away when the forge's version read fails or names its store
|
||||
// unavailable; with the read passing a 401 (the fresh token refused too) is the forge refusing the mesh's admin account.
|
||||
["pull", "502 and the version read failing is away", { repository: "novox/app", number: 9 }, { getPullRequest: down, forgeVersion: async () => ({ ok: false, words: "no answer", store: false }) }, (a) => [a.pull, a["error-kind"], a.part], [undefined, "away", "the forge"]],
|
||||
["pull", "401 and the store named unavailable is away", { repository: "novox/app", number: 9 }, { getPullRequest: unauthorized, forgeVersion: async () => ({ ok: false, words: "database is down", store: true }) }, (a) => [a["error-kind"], a.part, /401/.test(a.words)], ["away", "the forge's store", true]],
|
||||
["pull", "a connection fault and the version read failing is away", { repository: "novox/app", number: 9 }, { getPullRequest: async () => { throw new TypeError("fetch failed"); }, forgeVersion: async () => ({ ok: false, words: "fetch failed", store: false }) }, (a) => [a["error-kind"]], ["away"]],
|
||||
["pull", "401 with the version read passing is refused", { repository: "novox/app", number: 9 }, { getPullRequest: unauthorized }, (a) => [a["error-kind"], /refuses the mesh's admin account/.test(a.error), /[Cc]reate/.test(a.error)], ["refused", true, false]],
|
||||
["branch", "a branch's head, no protection read (ADR 0682 §13)", { repository: "novox/app", branch: "main" }, { branchHead: async () => SHA, appliedProtection: async () => { throw new Error("read"); } }, (a) => [a.head, a.protected, a.error], [SHA, undefined, undefined]],
|
||||
["branch", "no such branch", { repository: "novox/app", branch: "nope" }, { branchHead: gone }, (a) => [a.head, a["error-kind"]], [undefined, "not-found"]],
|
||||
["open-pulls", "one", {}, {}, (a) => [a.pulls.length, a.pulls[0].required], [1, ["mesh/check"]]],
|
||||
["open-pulls", "forge down", {}, { openPulls: down }, (a) => [a.pulls, a["error-kind"]], [undefined, "failed"]],
|
||||
["tags", "a page", { repository: "novox/app", prefix: "go/" }, {}, (a) => [a.tags, a.page, a.bound, a.more], [["go/v0.1.1", "go/v0.1.0"], 1, 50, undefined]],
|
||||
// 47 H2 §796: each tag names its commit, at the seat's form 3 (raised by H-A-471's note).
|
||||
["tags", "a page, each tag with its commit", { repository: "novox/app", prefix: "go/" }, {}, (a) => [a.tags, a.page, a.bound, a.more, a.form], [[{ tag: "go/v0.1.1", commit: SHA }, { tag: "go/v0.1.0", commit: OTHER }], 1, 50, undefined, 3]],
|
||||
["tags", "forge down", { repository: "novox/app" }, { tags: down }, (a) => [a.tags, a["error-kind"]], [undefined, "failed"]],
|
||||
["trees", "root", { repository: "novox/app", commit: SHA }, {}, (a) => [a.commit, a.root], [SHA, SHA]],
|
||||
["trees", "forge down", { repository: "novox/app", commit: SHA }, { commitTrees: down }, (a) => [a.root, a["error-kind"]], [undefined, "failed"]],
|
||||
@@ -523,6 +532,9 @@ test("every read and act of the seat git answers its contract, and an error is a
|
||||
["tag", "written", { repository: "novox/app", tag: "go/v1.2.3", commit: SHA }, { tagCommit: async () => null, createTag: async (_o: string, _r: string, tag: string, c: string) => { tagged.push(`${tag}@${c}`); } }, (a) => [a.performed, tagged.splice(0)], [true, [`go/v1.2.3@${SHA}`]]],
|
||||
["tag", "already at that commit", { repository: "novox/app", tag: "go/v1.2.3", commit: SHA }, { tagCommit: async () => SHA, createTag: async () => { throw new Error("written again"); } }, (a) => [a.performed, a.error], [true, undefined]],
|
||||
["tag", "at another commit is never moved", { repository: "novox/app", tag: "go/v1.2.3", commit: SHA }, { tagCommit: async () => OTHER }, (a) => [a.performed, a["error-kind"]], [undefined, "refused"]],
|
||||
// 47 H2 §796: a Go package's tag is <directory>/v<version>.
|
||||
["tag", "a package directory's version", { repository: "novox/app", tag: "modules/x/go/v1.2.3", commit: SHA }, { tagCommit: async () => null, createTag: async (_o: string, _r: string, tag: string, c: string) => { tagged.push(`${tag}@${c}`); } }, (a) => [a.performed, tagged.splice(0)], [true, [`modules/x/go/v1.2.3@${SHA}`]]],
|
||||
["tag", "no directory", { repository: "novox/app", tag: "v1.2.3", commit: SHA }, {}, (a) => [a.performed, a["error-kind"]], [undefined, "refused"]],
|
||||
["tag", "no Go version", { repository: "novox/app", tag: "v1", commit: SHA }, {}, (a) => [a.performed, a["error-kind"]], [undefined, "refused"]],
|
||||
["status", "refused state", { id: "s1", repository: "novox/app", commit: SHA, context: "c", state: "bogus" }, {}, (a) => [a.performed, a["error-kind"]], [undefined, "refused"]],
|
||||
["comment", "first time", { id: "c1", repository: "novox/app", number: 9, text: "hi" }, {}, (a) => [a.performed, a.held, added], [true, undefined, 1]],
|
||||
@@ -558,7 +570,10 @@ test("the first registrations are learned from the seat mesh-catalog's verb at t
|
||||
const held = reg(JSON.stringify({ registrations: [{ module: "mesh-sdk" }] }));
|
||||
await held.start();
|
||||
assert.equal(held.written, true, "a registration held at the start");
|
||||
const down = reg(new Error("no responders"));
|
||||
// A failed first view is said and asked again (H-A-471, second addendum): the test's wait answers it with none.
|
||||
let downAnswer: unknown = new Error("no responders");
|
||||
const down = new Registrations(async () => { if (downAnswer instanceof Error) throw downAnswer; return downAnswer; }, (l) => said.push(l),
|
||||
async () => { downAnswer = { registrations: [] }; });
|
||||
await down.start();
|
||||
assert.equal(down.written, false);
|
||||
assert.match(said.join("\n"), /could not be read: no responders/, "a failed first view is said, never taken as none silently");
|
||||
|
||||
@@ -16,8 +16,8 @@ test("the states the module announces are the states its manifest declares", asy
|
||||
|
||||
test("the announcement has the layout mesh-sdk's go/announce reads: version 1, the module, the node, the states sorted by name", async () => {
|
||||
const { statesAnnouncement } = await import("../kept.ts");
|
||||
assert.deepEqual(statesAnnouncement("gitea", "ace"), { v: 1, module: "gitea", node: "ace", states: [{ name: "announced" }, { name: "bound", readable: true }, { name: "calls" }, { name: "failed-steps" }, { name: "findings", readable: true }, { name: "first-views" }, { name: "idle" }, { name: "transfers" }] });
|
||||
assert.deepEqual(statesAnnouncement("gitea", undefined), { v: 1, module: "gitea", states: [{ name: "announced" }, { name: "bound", readable: true }, { name: "calls" }, { name: "failed-steps" }, { name: "findings", readable: true }, { name: "first-views" }, { name: "idle" }, { name: "transfers" }] });
|
||||
assert.deepEqual(statesAnnouncement("gitea", "ace"), { v: 1, module: "gitea", node: "ace", states: [{ name: "announced" }, { name: "bound", readable: true }, { name: "calls" }, { name: "failed-steps" }, { name: "findings", readable: true }, { name: "first-views" }, { name: "idle" }, { name: "taken" }, { name: "token-lease" }, { name: "transfers" }] });
|
||||
assert.deepEqual(statesAnnouncement("gitea", undefined), { v: 1, module: "gitea", states: [{ name: "announced" }, { name: "bound", readable: true }, { name: "calls" }, { name: "failed-steps" }, { name: "findings", readable: true }, { name: "first-views" }, { name: "idle" }, { name: "taken" }, { name: "token-lease" }, { name: "transfers" }] });
|
||||
});
|
||||
|
||||
test("index.ts says the announcement as the event `states` before the tools are served", () => {
|
||||
|
||||
@@ -83,6 +83,96 @@ test("a member refused at judging merges none and names every refused member and
|
||||
}
|
||||
});
|
||||
|
||||
// **A repository of no module is merged plainly** (design 47 H2 H-A-471 and its addendum): a member whose repository has
|
||||
// no watched branch (no entry, or an empty set, in the registrations' watched branches) is judged against nothing of the
|
||||
// module graph (no failed required step), merged in the order named, answered merged with the note no delivery, and left
|
||||
// out of merge-called; the watched members of the same call are judged and delivered as any call's. A watched
|
||||
// repository whose base is unwatched stays refused (the row above).
|
||||
test("a member of a repository with no watched branch is merged plainly, noted no delivery, and left out of merge-called", async () => {
|
||||
const NOTE = "no delivery: the repository has no watched branch";
|
||||
const rows: { name: string; watched: Map<string, Set<string>> }[] = [
|
||||
{ name: "no entry", watched: new Map([["novox/app", new Set(["main"])]]) },
|
||||
{ name: "an empty set", watched: new Map([["novox/app", new Set(["main"])], ["novox/hq", new Set<string>()]]) },
|
||||
];
|
||||
for (const r of rows) {
|
||||
const pulls = open();
|
||||
pulls["novox/hq#799"] = { base: "main", head: B, state: "open", merged: false };
|
||||
const f = forge(pulls);
|
||||
const judged: string[] = [];
|
||||
const said: any[] = [];
|
||||
const mixed = { by: "lead@shanks", pulls: [{ repository: "novox/hq", number: 799, head: B }, two.pulls[0]] };
|
||||
const got = await call(f, mixed, { watched: () => r.watched, say: async (_s: string, body: unknown) => { said.push(body); },
|
||||
judge: async (h: string) => { judged.push(h); return { run: "main.7", module: "hq", step: "unit", fingerprint: "fp" }; } });
|
||||
assert.deepEqual(f.merges, [], `${r.name}: a refused watched member merges none`);
|
||||
assert.deepEqual(judged, [A], `${r.name}: the unwatched member is judged against nothing of the module graph`);
|
||||
// The watched member's failed required step refuses the call whole, the unwatched one judged plainly beside it.
|
||||
assert.equal(got.members[0].state, "judged", r.name);
|
||||
assert.equal(got.members[1].state, "refused", r.name);
|
||||
// Without the failed step the call lands both: the unwatched noted, merge-called naming only the watched.
|
||||
const f2 = forge(Object.assign(open(), { "novox/hq#799": { base: "main", head: B, state: "open", merged: false } }));
|
||||
said.length = 0;
|
||||
const landed = await call(f2, mixed, { watched: () => r.watched, say: async (_s: string, body: unknown) => { said.push(body); } });
|
||||
assert.deepEqual(f2.merges, ["novox/hq#799", "novox/app#5"], `${r.name}: merged in the order named`);
|
||||
assert.deepEqual(landed.members.map((m: any) => [m.repository, m.state, m.note, m.why]),
|
||||
[["novox/hq", "merged", NOTE, undefined], ["novox/app", "merged", undefined, undefined]], r.name);
|
||||
assert.deepEqual(said.map((b) => b.members.map((m: any) => m.repository)), [["novox/app"]], `${r.name}: merge-called leaves it out`);
|
||||
// A call of unwatched members alone merges and starts no delivery: no merge-called.
|
||||
const f3 = forge({ "novox/hq#799": { base: "main", head: B, state: "open", merged: false } });
|
||||
said.length = 0;
|
||||
const alone = await call(f3, { by: "lead@shanks", pulls: [mixed.pulls[0]] }, { watched: () => r.watched,
|
||||
say: async (_s: string, body: unknown) => { said.push(body); } });
|
||||
assert.deepEqual([f3.merges, alone.members[0].state, alone.members[0].note, said], [["novox/hq#799"], "merged", NOTE, []], r.name);
|
||||
}
|
||||
});
|
||||
|
||||
// **A plain merge needs a view read since the last change heard** (design 47 H2 H-A-471, second addendum): while the
|
||||
// watched branches are unread, or were read before a later registration-changed, every member is refused ("the watched
|
||||
// branches are not read yet"), never merged with the note; a view read after the change merges the unwatched member
|
||||
// plainly. A read that fails is said and asked again until it answers.
|
||||
test("a member is merged plainly only from a watched-branches view read since the last registration-changed", async () => {
|
||||
const { Registrations } = await import("../dist/registrations.js");
|
||||
const NOT_READ = "the watched branches are not read yet";
|
||||
const said: string[] = [];
|
||||
const waits: number[] = [];
|
||||
let answer: unknown = new Error("no responders");
|
||||
const reg = new Registrations(async () => { if (answer instanceof Error) throw answer; return answer; }, (l) => said.push(l),
|
||||
async (ms) => { waits.push(ms); answer = { registrations: [{ module: "app", origin: "novox/app", branches: ["main"] }] }; });
|
||||
const hq = { by: "lead@shanks", pulls: [{ repository: "novox/hq", number: 799, head: B }, two.pulls[0]] };
|
||||
const pulls = () => Object.assign(open(), { "novox/hq#799": { base: "main", head: B, state: "open", merged: false } });
|
||||
const view = () => (reg.current ? reg.watched : null);
|
||||
reg.hear({ module: "app", moved: ["watch"], version: 1, warrant: "q-1" });
|
||||
// Unread: every member refused, nothing merged.
|
||||
const f1 = forge(pulls());
|
||||
const unread = await call(f1, hq, { watched: view });
|
||||
assert.deepEqual([f1.merges, unread.members.map((m: any) => [m.state, m.why, m.note])],
|
||||
[[], [["refused", NOT_READ, undefined], ["refused", NOT_READ, undefined]]], "an unread view refuses every member");
|
||||
// A failed read is said and asked again until it answers; read, the view is current.
|
||||
await reg.start();
|
||||
assert.deepEqual([waits, reg.current], [[2_000], true]);
|
||||
assert.match(said.join("\n"), /could not be read: no responders; no member is merged plainly until they are, asked again in 2s/);
|
||||
assert.ok(reg.readAt && reg.changedAt && reg.readAt >= reg.changedAt, "the times of the last read and the last change are kept");
|
||||
// A registration-changed heard after the read: stale, every member refused again.
|
||||
reg.hear({ module: "app", moved: ["watch"], version: 2, warrant: "q-2" });
|
||||
const f2 = forge(pulls());
|
||||
const stale = await call(f2, hq, { watched: view });
|
||||
assert.deepEqual([f2.merges, stale.members.map((m: any) => m.why)], [[], [NOT_READ, NOT_READ]], "a view read before a later change refuses");
|
||||
// A change heard while a read is asked leaves the view stale.
|
||||
let release!: () => void;
|
||||
answer = { registrations: [{ module: "app", origin: "novox/app", branches: ["main"] }] };
|
||||
const slow = new Registrations(() => new Promise((r) => { release = () => r(answer); }));
|
||||
const asking = slow.start();
|
||||
slow.hear({ module: "app", moved: ["watch"], version: 3, warrant: "q-3" });
|
||||
release();
|
||||
await asking;
|
||||
assert.equal(slow.current, false, "a read asked before the change heard is not since it");
|
||||
// Read again after the change: the unwatched member merges plainly with the note, the watched one as any call's.
|
||||
await reg.start();
|
||||
const f3 = forge(pulls());
|
||||
const landed = await call(f3, hq, { watched: view });
|
||||
assert.deepEqual([f3.merges, landed.members.map((m: any) => [m.state, m.note])],
|
||||
[["novox/hq#799", "novox/app#5"], [["merged", "no delivery: the repository has no watched branch"], ["merged", undefined]]]);
|
||||
});
|
||||
|
||||
test("a call is refused whole with no proved caller, no by, no registration or no pull request", async () => {
|
||||
const f = forge(open());
|
||||
await assert.rejects(call(f, two, {}, ""), /proved no caller account/);
|
||||
|
||||
@@ -108,3 +108,48 @@ test("a file over 512 KiB is refused from its directory listing, and its body is
|
||||
await new Promise<void>((r) => server.close(() => r()));
|
||||
}
|
||||
});
|
||||
|
||||
// 47 H2 §796: a tag names its commit; a tag the forge names no commit for is an error, never a tag without one.
|
||||
test("the forge's tags are read with the commit each names", async () => {
|
||||
const { GiteaClient } = await import("../client.ts");
|
||||
let body: unknown[] = [];
|
||||
const server = createServer((_req, res) => res.end(JSON.stringify(body)));
|
||||
await new Promise<void>((r) => server.listen(0, r));
|
||||
try {
|
||||
const client = new GiteaClient(`http://127.0.0.1:${(server.address() as any).port}`, "t");
|
||||
body = [{ name: "go/v0.1.1", commit: { sha: "a".repeat(40) } }, { name: "go/v0.1.0", commit: { sha: "b".repeat(40) } }];
|
||||
assert.deepEqual(await client.tags("novox", "hq", 1, 51), [{ tag: "go/v0.1.1", commit: "a".repeat(40) }, { tag: "go/v0.1.0", commit: "b".repeat(40) }]);
|
||||
body = [{ name: "go/v0.1.2" }];
|
||||
await assert.rejects(client.tags("novox", "hq", 1, 51), /named no commit for novox\/hq's tag go\/v0.1.2/);
|
||||
} finally {
|
||||
server.close();
|
||||
}
|
||||
});
|
||||
|
||||
// 47 H2 §776: the forge's own version read, unauthenticated, tells the forge away from its refusal.
|
||||
test("the forge's version read says whether the forge answers and whether its store is away", async () => {
|
||||
const { GiteaClient } = await import("../client.ts");
|
||||
let status = 200, body = `{"version":"1.27.3"}`;
|
||||
const auth: (string | undefined)[] = [];
|
||||
const server = createServer((req, res) => { auth.push(req.headers.authorization); res.statusCode = status; res.end(body); });
|
||||
await new Promise<void>((r) => server.listen(0, r));
|
||||
const port = (server.address() as any).port;
|
||||
try {
|
||||
const client = new GiteaClient(`http://127.0.0.1:${port}`, "t");
|
||||
const rows: [number, string, unknown][] = [
|
||||
[200, `{"version":"1.27.3"}`, { ok: true, version: "1.27.3" }],
|
||||
[500, "database is locked", { ok: false, store: true }],
|
||||
[503, "Service Unavailable", { ok: false, store: false }],
|
||||
];
|
||||
for (const [s, b, want] of rows) {
|
||||
status = s; body = b;
|
||||
const got: any = await client.forgeVersion();
|
||||
assert.deepEqual(got.ok ? got : { ok: got.ok, store: got.store }, want, `${s} ${b}`);
|
||||
}
|
||||
assert.deepEqual(auth.filter(Boolean), [], "the version read is unauthenticated");
|
||||
} finally {
|
||||
server.close();
|
||||
}
|
||||
const gone: any = await new GiteaClient(`http://127.0.0.1:${port}`, "t").forgeVersion();
|
||||
assert.deepEqual([gone.ok, gone.store], [false, false]);
|
||||
});
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { test } from "node:test";
|
||||
|
||||
// 47 H2 §775, H-A-381, H-A-400: the forge module takes an act of the seat git once by its ask id. Given the same ask id
|
||||
// twice it acts once: a repeat while the act runs joins it, a repeat after it ended answers what the forge holds of the
|
||||
// act; an act that performed nothing (away, a refusal before any merge) drops its key, so the repeat acts.
|
||||
test("an act of the seat git given the same ask id twice acts once", async () => {
|
||||
const { seatTools } = await import("../dist/seat.js");
|
||||
const { Once, durationMs, askTakenBoundMs } = await import("../dist/taken.js");
|
||||
const { runAs } = await import("@novox/mesh-sdk/callers");
|
||||
const SHA = "a".repeat(40), MERGE = "c".repeat(40);
|
||||
const keys = new Set<string>();
|
||||
const taken = { take: async (ask: string) => (keys.has(ask) ? false : (keys.add(ask), true)), drop: async (ask: string) => { keys.delete(ask); } };
|
||||
let merges = 0, comments: { id: number; body: string }[] = [], added = 0, away = false;
|
||||
const pr = () => ({ number: 9, state: merges ? "closed" : "open", merged: merges > 0, head: "feat/x", head_sha: SHA, base: "main", ...(merges ? { merge_commit_sha: MERGE } : {}) });
|
||||
let release: () => void = () => {}, entered: () => void = () => {};
|
||||
const merging = new Promise<void>((r) => { entered = r; });
|
||||
const forge: Record<string, any> = {
|
||||
forgeVersion: async () => (away ? { ok: false, words: "no answer", store: true } : { ok: true, version: "1.27.3" }),
|
||||
getPullRequest: async () => { if (away) throw new Error("Gitea API /repos/novox/app/pulls/9: 503 Service Unavailable"); return pr(); },
|
||||
mergePullRequest: async () => { const held = new Promise<void>((r) => { release = r; }); entered(); await held; merges++; },
|
||||
commitParents: async () => [SHA, SHA],
|
||||
listComments: async () => comments,
|
||||
addComment: async (_o: string, _r: string, _n: number, body: string) => { added++; comments.push({ id: added, body }); },
|
||||
};
|
||||
const seat = seatTools(forge as any, { run: (async () => ({ stdout: "", code: 1 })) as any, container: "gitea", registered: () => true,
|
||||
judge: async () => null, watched: () => new Map([["novox/app", new Set(["main"])]]), once: new Once(taken) });
|
||||
const call = (verb: string, ask: string, args: Record<string, unknown>) =>
|
||||
runAs({ "mesh/caller": "ace.x", "mesh/ask": ask }, args, () => seat.find((x: any) => x.name === `git.${verb}`)!.run(args)) as Promise<any>;
|
||||
const merge = { by: "lead@shanks", pulls: [{ repository: "novox/app", number: 9, head: SHA }] };
|
||||
|
||||
// Away first: nothing performed, the key dropped, so the kept call's repeat under the same id acts (H-A-400 (1)).
|
||||
away = true;
|
||||
assert.equal((await call("merge-pull", "ask1", merge))["error-kind"], "away");
|
||||
assert.equal(keys.has("ask1"), false, "an act that performed nothing keeps no taken key");
|
||||
away = false;
|
||||
|
||||
// The repeat while the first runs joins it: one merge, both answered the same.
|
||||
const first = call("merge-pull", "ask1", merge);
|
||||
await merging;
|
||||
const joined = call("merge-pull", "ask1", merge);
|
||||
release();
|
||||
const [a, b] = await Promise.all([first, joined]);
|
||||
assert.equal(merges, 1, "a repeat while the act runs joins it");
|
||||
assert.deepEqual(b, a);
|
||||
assert.equal(a.members[0].state, "merged");
|
||||
|
||||
// After it ended, the repeat answers the pull request as the forge holds it, merged with its merge commit, and merges nothing.
|
||||
const again = await call("merge-pull", "ask1", merge);
|
||||
assert.deepEqual([merges, again.members], [1, [{ repository: "novox/app", number: 9, state: "merged", commit: MERGE }]]);
|
||||
|
||||
// A comment given its ask again is not written twice; a repeat whose comment the forge does not hold is refused, naming it.
|
||||
const comment = { id: "c1", repository: "novox/app", number: 9, text: "hi" };
|
||||
assert.equal((await call("comment", "ask2", comment)).performed, true);
|
||||
assert.deepEqual([(await call("comment", "ask2", comment)).held, added], [true, 1]);
|
||||
comments = [];
|
||||
const gone = await call("comment", "ask2", comment);
|
||||
assert.deepEqual([gone["error-kind"], /ask2 was taken/.test(gone.error), added], ["refused", true, 1]);
|
||||
|
||||
// ask-taken-bound is read from the word the node-engine composes (H-A-400 (2)), 10 minutes by default.
|
||||
assert.deepEqual([askTakenBoundMs({}), askTakenBoundMs({ MESH_CONFIG_ASK_TAKEN_BOUND: "90s" }), askTakenBoundMs({ MESH_CONFIG_ASK_TAKEN_BOUND: "soon" }), durationMs("1h30m")],
|
||||
[600_000, 90_000, 600_000, 5_400_000]);
|
||||
});
|
||||
+197
-113
@@ -10,15 +10,19 @@
|
||||
import { test, after } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { createServer, type IncomingMessage, type ServerResponse } from "node:http";
|
||||
import { mkdir, mkdtemp, readFile, rm, stat, writeFile } from "node:fs/promises";
|
||||
import { mkdtemp, stat, writeFile } from "node:fs/promises";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
|
||||
import { collectTools } from "@novox/mesh-sdk/tools";
|
||||
import { AdminRefused, MintedToken, TOKEN_SCOPES } from "../dist/token.js";
|
||||
import { AdminRefused, MintedToken, TOKEN_NAME, TOKEN_SCOPES, leaseOverState, openValue, vaultOverBus, vaultValue, type MintedTokenOptions } from "../dist/token.js";
|
||||
import { tokenLeaseTool } from "../dist/tools/tokenlease.js";
|
||||
import { fakeBus } from "./fakebus.ts";
|
||||
import { GiteaClient } from "../dist/client.js";
|
||||
import { getGiteaTools } from "../dist/tools/index.js";
|
||||
|
||||
/** What the vault and the forge were asked, in order: each own-put and each token deleted (design 47 H2 §808). */
|
||||
const ORDER: string[] = [];
|
||||
const ADMIN = "mesh-admin";
|
||||
const PASSWORD = "the-vault-minted-this";
|
||||
|
||||
@@ -126,6 +130,7 @@ function fakeForge(): Promise<Forge> {
|
||||
if (matches.length > 1) return json(res, 422, { message: `multiple matches for token name '${key}'` });
|
||||
const t = matches[0];
|
||||
forge.deletedBy.push(byId ? "id" : "name");
|
||||
ORDER.push(`delete ${t.name}`);
|
||||
const twin = forge.twins.findIndex((w) => w.id === t.id);
|
||||
if (twin >= 0) forge.twins.splice(twin, 1);
|
||||
else forge.tokens.delete(t.name);
|
||||
@@ -253,58 +258,62 @@ function fakeForge(): Promise<Forge> {
|
||||
|
||||
// ---- What the runtime's environment gives the module. ----
|
||||
|
||||
async function delivered(forge: Forge): Promise<{ env: NodeJS.ProcessEnv; file: string; logs: string[] }> {
|
||||
async function delivered(forge: Forge, now: () => Date = () => new Date()) {
|
||||
const dir = await mkdtemp(join(tmpdir(), "gitea-"));
|
||||
const passwordFile = join(dir, "admin.secret");
|
||||
await writeFile(passwordFile, PASSWORD + "\n", { mode: 0o600 });
|
||||
const state = join(dir, "state");
|
||||
ORDER.length = 0;
|
||||
return {
|
||||
env: {
|
||||
MESH_GITEA_URL: forge.url,
|
||||
MESH_GITEA_ADMIN_USER: ADMIN,
|
||||
MESH_GITEA_ADMIN_PASSWORD_FILE: passwordFile,
|
||||
MESH_GITEA_STATE_DIR: state,
|
||||
},
|
||||
file: join(state, "token"),
|
||||
logs: [],
|
||||
MESH_GITEA_STATE_DIR: join(dir, "state"),
|
||||
} as NodeJS.ProcessEnv,
|
||||
bus: fakeBus(now, ORDER),
|
||||
logs: [] as string[],
|
||||
};
|
||||
}
|
||||
|
||||
/** A client as a fresh process would build it: a new source over the kept file, its log captured. */
|
||||
function minted(env: NodeJS.ProcessEnv, logs: string[]): GiteaClient {
|
||||
const source = new MintedToken({
|
||||
url: env.MESH_GITEA_URL!,
|
||||
admin: env.MESH_GITEA_ADMIN_USER!,
|
||||
passwordFile: env.MESH_GITEA_ADMIN_PASSWORD_FILE!,
|
||||
file: join(env.MESH_GITEA_STATE_DIR!, "token"),
|
||||
log: (l) => logs.push(l),
|
||||
});
|
||||
return new GiteaClient(env.MESH_GITEA_URL!, source);
|
||||
/** A client as a fresh process would build it: a new source over the vault and the lease on the bus, its log captured. */
|
||||
function minted(env: NodeJS.ProcessEnv, logs: string[], extra: Partial<MintedTokenOptions> = {}): GiteaClient {
|
||||
return new GiteaClient(env.MESH_GITEA_URL!, source(env, logs, extra));
|
||||
}
|
||||
|
||||
function source(env: NodeJS.ProcessEnv, logs: string[], extra: Partial<MintedTokenOptions> = {}): MintedToken {
|
||||
return new MintedToken({
|
||||
url: env.MESH_GITEA_URL!, admin: env.MESH_GITEA_ADMIN_USER!, passwordFile: env.MESH_GITEA_ADMIN_PASSWORD_FILE!,
|
||||
account: "mesh-tools", oldName: TOKEN_NAME, vault: vaultOverBus(), lease: leaseOverState(), log: (l) => logs.push(l), ...extra,
|
||||
});
|
||||
}
|
||||
|
||||
const VAULT = "module/gitea/mesh-tools";
|
||||
/** The token the vault keeps for the tools' account, out of the value that holds it with its scopes (H-A-439). */
|
||||
const vaulted = (bus: ReturnType<typeof fakeBus>) => openValue(bus.vault.get(VAULT)?.value ?? "").token;
|
||||
const forge = await fakeForge();
|
||||
after(() => forge.close());
|
||||
|
||||
test("first start: mints with the admin account, keeps the token at 0600, asks for the tools' scopes only", async () => {
|
||||
const { env, file, logs } = await delivered(forge);
|
||||
test("first start: mints mesh-tools-1 with the admin account, writes it through own-put, asks for the tools' scopes only", async () => {
|
||||
const { env, bus, logs } = await delivered(forge);
|
||||
forge.tokens.clear();
|
||||
|
||||
const repos = await minted(env, logs).listRepos();
|
||||
|
||||
assert.equal(repos[0]?.full_name, "novox/hq");
|
||||
assert.equal(forge.mints, 1);
|
||||
// Never write:admin (novox/hq ADR 0266): the forge's administration is the operator's, not a tool's.
|
||||
assert.deepEqual(forge.lastScopes, ["write:repository", "write:issue", "read:user", "read:package"]);
|
||||
assert.deepEqual(forge.lastScopes, [...TOKEN_SCOPES]);
|
||||
const token = forge.tokens.get("mesh-tools")!;
|
||||
assert.equal(await readFile(file, "utf8"), token + "\n");
|
||||
assert.equal(await readFile(file + ".scopes", "utf8"), "read:package,read:user,write:issue,write:repository\n");
|
||||
assert.equal((await stat(file)).mode & 0o777, 0o600);
|
||||
// Said that it minted, and where it keeps it — never what it is.
|
||||
assert.ok(logs.some((l) => l.startsWith("minted a token")), logs.join("\n"));
|
||||
// Design 47 H2 §808, H-A-422 (2), H-A-426: named per mint, kept as the module's own secret, never a file.
|
||||
const token = forge.tokens.get("mesh-tools-1")!;
|
||||
assert.ok(token);
|
||||
assert.deepEqual([vaulted(bus), bus.vault.get(VAULT)?.generation], [token, 1]);
|
||||
assert.deepEqual(openValue(bus.vault.get(VAULT)!.value).scopes, [...TOKEN_SCOPES], "kept with the scopes it was minted with (H-A-439)");
|
||||
await assert.rejects(stat(join(env.MESH_GITEA_STATE_DIR!, "token")), /ENOENT/);
|
||||
assert.ok(logs.some((l) => l.startsWith("minted mesh-tools-1")), logs.join("\n"));
|
||||
assert.ok(logs.every((l) => !l.includes(token) && !l.includes(PASSWORD)), logs.join("\n"));
|
||||
});
|
||||
|
||||
test("second start: reuses the kept token, mints nothing", async () => {
|
||||
test("second start: reuses the vault's token, mints nothing", async () => {
|
||||
const { env, logs } = await delivered(forge);
|
||||
await minted(env, logs).listRepos();
|
||||
const before = forge.mints;
|
||||
@@ -313,54 +322,83 @@ test("second start: reuses the kept token, mints nothing", async () => {
|
||||
await minted(env, again).listRepos();
|
||||
|
||||
assert.equal(forge.mints, before);
|
||||
assert.ok(again.some((l) => l.startsWith("reusing the token kept at")), again.join("\n"));
|
||||
assert.ok(again.every((l) => !l.includes(forge.tokens.get("mesh-tools")!)), again.join("\n"));
|
||||
assert.ok(again.some((l) => l.startsWith(`reusing the token the vault keeps as ${VAULT}`)), again.join("\n"));
|
||||
});
|
||||
|
||||
test("the forge rejects the kept token (its data was restored): minted afresh, once, and the call goes through", async () => {
|
||||
const { env, file, logs } = await delivered(forge);
|
||||
test("the forge rejects the vault's token: mesh-tools-2 is minted once, written through own-put, and only then is mesh-tools-1 deleted by its id", async () => {
|
||||
const { env, bus, logs } = await delivered(forge);
|
||||
forge.tokens.clear();
|
||||
const client = minted(env, logs);
|
||||
await client.listRepos();
|
||||
const before = forge.mints;
|
||||
ORDER.length = 0;
|
||||
forge.deletedBy.length = 0;
|
||||
|
||||
forge.tokens.clear(); // the forge no longer knows any token — a restore from the predecessor
|
||||
forge.tokens.set("mesh-tools-1", "revoked-meanwhile"); // the forge no longer takes the vault's token
|
||||
const repos = await client.listRepos();
|
||||
|
||||
assert.equal(repos.length, 1);
|
||||
assert.equal(forge.mints, before + 1);
|
||||
assert.equal(await readFile(file, "utf8"), forge.tokens.get("mesh-tools") + "\n");
|
||||
assert.ok(logs.some((l) => l.startsWith("the forge rejected the kept token")), logs.join("\n"));
|
||||
assert.equal(vaulted(bus), forge.tokens.get("mesh-tools-2"));
|
||||
// H-A-422 (2): the own-put first, the replaced token's delete after, by id; the forge holds one tools' token after.
|
||||
assert.deepEqual(ORDER, [`own-put ${VAULT} 2`, "delete mesh-tools-1"]);
|
||||
assert.deepEqual(forge.deletedBy, ["id"]);
|
||||
assert.deepEqual([...forge.tokens.keys()].filter((n) => n.startsWith("mesh-tools")), ["mesh-tools-2"]);
|
||||
assert.ok(logs.some((l) => l.startsWith("the forge rejected the vault's token")), logs.join("\n"));
|
||||
});
|
||||
|
||||
test("a token kept with write:admin by an earlier build is minted again without it at the next start, and the forge drops the old one", async () => {
|
||||
const { env, file, logs } = await delivered(forge);
|
||||
await minted(env, logs).listRepos();
|
||||
const old = forge.tokens.get("mesh-tools")!;
|
||||
forge.scopesOf.set(old, ["write:repository", "write:issue", "read:user", "write:admin"]);
|
||||
await rm(file + ".scopes"); // an earlier build kept no scopes beside it
|
||||
test("an own-put that fails leaves the old token standing, and the next mint replaces the unused one it left", async () => {
|
||||
const { env, bus, logs } = await delivered(forge);
|
||||
forge.tokens.clear();
|
||||
const client = minted(env, logs);
|
||||
await client.listRepos();
|
||||
const old = forge.tokens.get("mesh-tools-1")!;
|
||||
forge.tokens.set("mesh-tools-1", "revoked-meanwhile");
|
||||
ORDER.length = 0;
|
||||
let failPut = true;
|
||||
const failing = minted(env, logs, { vault: { get: vaultOverBus().get, put: async (a, t, scopes) => {
|
||||
if (failPut) { failPut = false; throw new Error("the vault is away"); }
|
||||
return vaultOverBus().put(a, t, scopes);
|
||||
} } });
|
||||
|
||||
await assert.rejects(failing.listRepos(), /the vault is away/);
|
||||
assert.deepEqual(ORDER, [], "nothing deleted while the vault holds the old token");
|
||||
assert.equal(bus.vault.get(VAULT)?.generation, 1);
|
||||
assert.ok(forge.tokens.has("mesh-tools-2"), "the unused token of the failed mint stands");
|
||||
|
||||
assert.equal((await failing.listRepos()).length, 1);
|
||||
assert.deepEqual(ORDER, ["delete mesh-tools-2", `own-put ${VAULT} 2`, "delete mesh-tools-1"]);
|
||||
assert.notEqual(vaulted(bus), old);
|
||||
});
|
||||
|
||||
test("a token older than token-ttl is minted again, and the one it replaces deleted", async () => {
|
||||
let clock = Date.parse("2026-11-22T00:00:00Z");
|
||||
const now = () => new Date(clock);
|
||||
const { env, bus, logs } = await delivered(forge, now);
|
||||
forge.tokens.clear();
|
||||
const client = minted(env, logs, { now, ttlMs: 24 * 3600_000 });
|
||||
await client.listRepos();
|
||||
const before = forge.mints;
|
||||
|
||||
const again: string[] = [];
|
||||
await minted(env, again).listRepos();
|
||||
clock += 23 * 3600_000;
|
||||
await client.listRepos();
|
||||
assert.equal(forge.mints, before, "a token within token-ttl is used");
|
||||
clock += 2 * 3600_000;
|
||||
await client.listRepos();
|
||||
|
||||
assert.equal(forge.mints, before + 1);
|
||||
assert.deepEqual(forge.lastScopes, [...TOKEN_SCOPES]);
|
||||
assert.ok(!forge.lastScopes!.includes("write:admin"));
|
||||
assert.notEqual(forge.tokens.get("mesh-tools"), old);
|
||||
assert.ok(![...forge.tokens.values()].includes(old), "the old token still stands on the forge");
|
||||
assert.ok(again.some((l) => l.includes("minted with other scopes")), again.join("\n"));
|
||||
// An admin route through the escape hatch is refused before it is sent.
|
||||
const tool = getGiteaTools(minted(env, again)).find((t) => t.name === "gitea_api")!;
|
||||
await assert.rejects(tool.run({ path: "/admin/users/mesh_novox_builder", method: "PATCH", body: { admin: true } }),
|
||||
/not a write gitea_api makes/);
|
||||
assert.equal(bus.vault.get(VAULT)?.generation, 2);
|
||||
assert.deepEqual([...forge.tokens.keys()].filter((n) => n.startsWith("mesh-tools")), ["mesh-tools-2"]);
|
||||
assert.ok(logs.some((l) => l.includes("older than token-ttl")), logs.join("\n"));
|
||||
});
|
||||
|
||||
test("a kept token lacking a scope the tools need: the forge refuses the call for the scope, the token is re-minted with the whole list, and the call goes through", async () => {
|
||||
const { env, file, logs } = await delivered(forge);
|
||||
test("a token lacking a scope the tools need: the forge refuses the call for the scope, the token is re-minted with the whole list, and the call goes through", async (t) => {
|
||||
const { env, bus, logs } = await delivered(forge);
|
||||
forge.tokens.clear();
|
||||
const client = minted(env, logs);
|
||||
await client.listRepos();
|
||||
const before = forge.mints;
|
||||
const old = forge.tokens.get("mesh-tools")!;
|
||||
const old = forge.tokens.get("mesh-tools-1")!;
|
||||
forge.scopesOf.set(old, ["write:repository", "write:issue"]); // minted before read:user was added
|
||||
|
||||
const repos = await client.api<unknown[]>("/user/repos");
|
||||
@@ -368,37 +406,37 @@ test("a kept token lacking a scope the tools need: the forge refuses the call fo
|
||||
assert.equal(repos.length, 1);
|
||||
assert.equal(forge.mints, before + 1);
|
||||
assert.deepEqual(forge.lastScopes, [...TOKEN_SCOPES]);
|
||||
assert.notEqual(forge.tokens.get("mesh-tools"), old);
|
||||
assert.equal(await readFile(file, "utf8"), forge.tokens.get("mesh-tools") + "\n");
|
||||
assert.ok(logs.some((l) => l.startsWith("the forge rejected the kept token")), logs.join("\n"));
|
||||
assert.equal(vaulted(bus), forge.tokens.get("mesh-tools-2"));
|
||||
|
||||
// Design 47 H2 §808, H-A-439: the vault keeps the scopes beside the token, so one carrying a scope beyond the tools'
|
||||
// is minted again unasked, as when they were kept in a file beside it.
|
||||
await t.test("a token kept with write:admin is minted again without it at the next start, and the forge drops the old one", async () => {
|
||||
const { env, bus, logs } = await delivered(forge);
|
||||
forge.tokens.clear();
|
||||
await minted(env, logs).listRepos();
|
||||
const old = forge.tokens.get("mesh-tools-1")!;
|
||||
// An earlier mint asked write:admin too, and the vault keeps what it asked.
|
||||
bus.vault.get(VAULT)!.value = vaultValue(old, [...TOKEN_SCOPES, "write:admin"]);
|
||||
forge.scopesOf.set(old, [...TOKEN_SCOPES, "write:admin"]);
|
||||
const before = forge.mints;
|
||||
|
||||
const again: string[] = [];
|
||||
await minted(env, again).listRepos();
|
||||
|
||||
assert.equal(forge.mints, before + 1);
|
||||
assert.deepEqual(forge.lastScopes, [...TOKEN_SCOPES]);
|
||||
assert.ok(![...forge.tokens.values()].includes(old), "the old token still stands on the forge");
|
||||
assert.deepEqual(openValue(bus.vault.get(VAULT)!.value), { token: forge.tokens.get("mesh-tools-2"), scopes: [...TOKEN_SCOPES] });
|
||||
assert.ok(again.some((l) => l.includes("minted with other scopes")), again.join("\n"));
|
||||
// An admin route through the escape hatch is refused before it is sent.
|
||||
const tool = getGiteaTools(minted(env, again)).find((x) => x.name === "gitea_api")!;
|
||||
await assert.rejects(tool.run({ path: "/admin/users/mesh_novox_builder", method: "PATCH", body: { admin: true } }, undefined as never),
|
||||
/not a write gitea_api makes/);
|
||||
});
|
||||
});
|
||||
|
||||
test("the kept file is gone but the forge still holds a token by that name: replaced, not refused", async () => {
|
||||
const { env, file, logs } = await delivered(forge);
|
||||
await minted(env, logs).listRepos();
|
||||
const before = forge.mints;
|
||||
await rm(file);
|
||||
|
||||
const repos = await minted(env, logs).listRepos();
|
||||
|
||||
assert.equal(repos.length, 1);
|
||||
assert.equal(forge.mints, before + 1);
|
||||
assert.equal([...forge.tokens.keys()].filter((n) => n === "mesh-tools").length, 1);
|
||||
assert.ok(logs.some((l) => l.includes('already holds a token named "mesh-tools"')), logs.join("\n"));
|
||||
});
|
||||
|
||||
test("concurrent first calls share one mint", async () => {
|
||||
test("the first mint deletes by id every token under the old fixed name, two raced ones too, and keeps the account's others", async () => {
|
||||
const { env, logs } = await delivered(forge);
|
||||
const client = minted(env, logs);
|
||||
const before = forge.mints;
|
||||
|
||||
await Promise.all([client.listRepos(), client.listRepos(), client.listRepos()]);
|
||||
|
||||
assert.equal(forge.mints, before + 1);
|
||||
});
|
||||
|
||||
test("two tokens hold the name (two mints raced): both are deleted by their id, and one fresh token is minted", async () => {
|
||||
const { env, file, logs } = await delivered(forge);
|
||||
forge.tokens.clear();
|
||||
forge.twins.length = 0;
|
||||
for (const other of ["laptop", "backup", "ci"]) forge.plant(other); // the account's other tokens: kept
|
||||
@@ -411,44 +449,68 @@ test("two tokens hold the name (two mints raced): both are deleted by their id,
|
||||
|
||||
assert.equal(repos.length, 1);
|
||||
assert.equal(forge.mints, before + 1);
|
||||
// H-A-430 (1): the tokens the module kept before under the old fixed name are the ones the first mint replaces.
|
||||
assert.deepEqual(forge.deletedBy, ["id", "id"]);
|
||||
assert.deepEqual(ORDER, [`own-put ${VAULT} 1`, "delete mesh-tools", "delete mesh-tools"]);
|
||||
assert.equal(forge.twins.length, 0);
|
||||
assert.deepEqual([...forge.tokens.keys()].sort(), ["backup", "ci", "laptop", "mesh-tools"]);
|
||||
assert.equal(await readFile(file, "utf8"), forge.tokens.get("mesh-tools") + "\n");
|
||||
assert.ok(logs.some((l) => l.includes('holds 2 tokens named "mesh-tools"')), logs.join("\n"));
|
||||
assert.deepEqual([...forge.tokens.keys()].sort(), ["backup", "ci", "laptop", "mesh-tools-1"]);
|
||||
});
|
||||
|
||||
test("two processes on one kept file, both without a token, mint once: the second waits for the first and reuses it", async () => {
|
||||
test("concurrent first calls share one mint", async () => {
|
||||
const { env, logs } = await delivered(forge);
|
||||
forge.tokens.clear();
|
||||
forge.twins.length = 0;
|
||||
const client = minted(env, logs);
|
||||
const before = forge.mints;
|
||||
// Two sources over one state directory, as the tools bundle and the watcher are two processes.
|
||||
const first = minted(env, logs);
|
||||
const second = minted(env, logs);
|
||||
|
||||
await Promise.all([first.listRepos(), second.listRepos()]);
|
||||
await Promise.all([client.listRepos(), client.listRepos(), client.listRepos()]);
|
||||
|
||||
assert.equal(forge.mints, before + 1);
|
||||
assert.equal(forge.twins.length, 0);
|
||||
assert.ok(logs.some((l) => l.includes("another process minted it")), logs.join("\n"));
|
||||
});
|
||||
|
||||
test("a lock left by a process that died mid-mint is taken over, not waited on for ever", async () => {
|
||||
const { env, file, logs } = await delivered(forge);
|
||||
test("two processes, one holding the old token, both refused, mint one token in all: the second waits for the lease and uses the vault's newer token", async () => {
|
||||
const { env, bus, logs } = await delivered(forge);
|
||||
forge.tokens.clear();
|
||||
forge.twins.length = 0;
|
||||
await mkdir(join(env.MESH_GITEA_STATE_DIR!), { recursive: true });
|
||||
await writeFile(`${file}.lock`, `2147483646 ${Date.now()} gone\n`); // a pid no process holds
|
||||
const first = minted(env, logs);
|
||||
await first.listRepos();
|
||||
const second = minted(env, logs);
|
||||
await second.listRepos(); // both hold mesh-tools-1
|
||||
const before = forge.mints;
|
||||
|
||||
assert.equal((await minted(env, logs).listRepos()).length, 1);
|
||||
forge.tokens.set("mesh-tools-1", "revoked-meanwhile");
|
||||
await Promise.all([first.listRepos(), second.listRepos()]);
|
||||
|
||||
// Design 47 H2 §808: one mint in all, and the forge holds one token for the account after.
|
||||
assert.equal(forge.mints, before + 1);
|
||||
await assert.rejects(stat(`${file}.lock`), /ENOENT/);
|
||||
assert.deepEqual([...forge.tokens.keys()].filter((n) => n.startsWith("mesh-tools")), ["mesh-tools-2"]);
|
||||
assert.equal(bus.vault.get(VAULT)?.generation, 2);
|
||||
assert.ok(logs.some((l) => l.includes("another process holds the mint lease")), logs.join("\n"));
|
||||
assert.equal(bus.kv.size, 0, "the lease is deleted after the mint");
|
||||
});
|
||||
|
||||
test("a minter killed while holding the lease is replaced after mint-lease: the waiter creates the lease itself and mints", async () => {
|
||||
let clock = Date.parse("2026-11-22T00:00:00Z");
|
||||
const now = () => new Date(clock);
|
||||
const { env, bus, logs } = await delivered(forge, now);
|
||||
forge.tokens.clear();
|
||||
// A process that took the lease and died: its key lives mint-lease (60 s) and nobody deletes it.
|
||||
await bus.ask("mesh/state.create", { state: "token-lease", key: "mint", value: { holder: "ace/4242", since: now().toISOString() }, ttl_seconds: 60 });
|
||||
const waited: number[] = [];
|
||||
const lease = leaseOverState();
|
||||
// Waiting for the key to change: nothing changes, so the wait lasts its bound, the clock moving on by it.
|
||||
const before = forge.mints;
|
||||
|
||||
const repos = await minted(env, logs, { lease: { ...lease, changed: async (ms) => { waited.push(ms); clock += ms; } } }).listRepos();
|
||||
|
||||
assert.equal(repos.length, 1);
|
||||
assert.deepEqual(waited, [60_000], "the waiter waited mint-lease once");
|
||||
assert.equal(forge.mints, before + 1);
|
||||
assert.ok(logs.some((l) => l.includes("another process holds the mint lease (ace/4242 since")), logs.join("\n"));
|
||||
});
|
||||
|
||||
test("the admin account is refused: said plainly, nothing kept, and the next call fails the same way rather than crashing", async () => {
|
||||
const { env, file, logs } = await delivered(forge);
|
||||
const { env, bus, logs } = await delivered(forge);
|
||||
forge.tokens.clear();
|
||||
forge.admins.delete(ADMIN); // the forge's data came from a predecessor; mesh-admin was never created there
|
||||
try {
|
||||
const client = minted(env, logs);
|
||||
@@ -456,15 +518,15 @@ test("the admin account is refused: said plainly, nothing kept, and the next cal
|
||||
|
||||
await assert.rejects(client.listRepos(), (err: unknown) => {
|
||||
assert.ok(err instanceof AdminRefused, String(err));
|
||||
assert.match(err.message, /refused the admin account "mesh-admin" \(401\)/);
|
||||
assert.match(err.message, /admin-bootstrap step creates it/);
|
||||
assert.match(err.message, /came from a predecessor/);
|
||||
// 47 H2 §776: the forge's refusal, never worded as an account to create.
|
||||
assert.match(err.message, /the forge refuses the mesh's admin account "mesh-admin" \(401\)/);
|
||||
assert.doesNotMatch(err.message, /[Cc]reate/);
|
||||
assert.ok(!err.message.includes(PASSWORD));
|
||||
return true;
|
||||
});
|
||||
await assert.rejects(client.listRepos(), AdminRefused);
|
||||
assert.equal(forge.mints, before);
|
||||
await assert.rejects(stat(file), /ENOENT/);
|
||||
assert.equal(bus.vault.size, 0);
|
||||
|
||||
// The account appears (the operator created it): the very next call mints and works.
|
||||
forge.admins.set(ADMIN, PASSWORD);
|
||||
@@ -475,25 +537,47 @@ test("the admin account is refused: said plainly, nothing kept, and the next cal
|
||||
}
|
||||
});
|
||||
|
||||
test("one process shares one source per kept file — the watcher and the tools never renew against each other", async () => {
|
||||
test("one process shares one source per account — the watcher and the tools never renew against each other", async () => {
|
||||
const { env } = await delivered(forge);
|
||||
assert.equal(MintedToken.fromEnv(env.MESH_GITEA_URL!, env), MintedToken.fromEnv(env.MESH_GITEA_URL!, env));
|
||||
});
|
||||
|
||||
test("a second process finds the token the first renewed, and reuses it instead of minting over it", async () => {
|
||||
test("a second process finds the token the first renewed in the vault, and reuses it instead of minting over it", async () => {
|
||||
const { env, logs } = await delivered(forge);
|
||||
forge.tokens.clear();
|
||||
const first = minted(env, logs);
|
||||
const second = minted(env, logs);
|
||||
await first.listRepos();
|
||||
await second.listRepos(); // both hold the same kept token
|
||||
await second.listRepos(); // both hold the same token
|
||||
const before = forge.mints;
|
||||
|
||||
forge.tokens.clear();
|
||||
forge.tokens.set("mesh-tools-1", "revoked-meanwhile");
|
||||
await first.listRepos(); // renews: one mint
|
||||
await second.listRepos(); // rejected too — but the kept file already carries the renewed one
|
||||
await second.listRepos(); // rejected too — but the vault already holds the renewed one
|
||||
|
||||
assert.equal(forge.mints, before + 1);
|
||||
assert.ok(logs.some((l) => l.includes("is newer — reusing it")), logs.join("\n"));
|
||||
assert.ok(logs.some((l) => l.includes("is newer (generation 2) — reusing it")), logs.join("\n"));
|
||||
});
|
||||
|
||||
test("forge_token_lease says the lease's holder and since when, and each token's age from the vault, never the token", async () => {
|
||||
let clock = Date.parse("2026-11-22T00:00:00Z");
|
||||
const now = () => new Date(clock);
|
||||
const { env, bus, logs } = await delivered(forge, now);
|
||||
forge.tokens.clear();
|
||||
await minted(env, logs, { now }).listRepos();
|
||||
await bus.ask("mesh/state.create", { state: "token-lease", key: "mint", value: { holder: "ace/4242", since: "2026-11-22T00:00:30Z" }, ttl_seconds: 60 });
|
||||
clock += 90_000;
|
||||
const tool = tokenLeaseTool(vaultOverBus(), leaseOverState(), ["mesh-tools", "mesh-packages"], now);
|
||||
|
||||
const said = await tool.run({}, undefined as never) as any;
|
||||
|
||||
assert.equal(tool.name, "forge_token_lease");
|
||||
assert.deepEqual(said.tokens.map((t: any) => [t.account, t.held, t.generation, t["age-seconds"]]),
|
||||
[["mesh-tools", true, 1, 90], ["mesh-packages", false, undefined, undefined]]);
|
||||
assert.ok(!JSON.stringify(said).includes(vaulted(bus)), "the token is never said");
|
||||
// The lease lives mint-lease: past it, nobody holds it.
|
||||
clock += 30_000;
|
||||
assert.deepEqual((await tool.run({}, undefined as never) as any).lease, { held: false });
|
||||
});
|
||||
|
||||
test("a configured token wins, and is reported rather than minted over when the forge rejects it", async () => {
|
||||
@@ -510,8 +594,8 @@ test("a configured token wins, and is reported rather than minted over when the
|
||||
|
||||
test("nothing to mint with and no token: the client says what is missing", async () => {
|
||||
assert.throws(
|
||||
() => GiteaClient.fromEnv({ MESH_GITEA_URL: forge.url, MESH_GITEA_ADMIN_USER: ADMIN }),
|
||||
/set GITEA_TOKEN, or MESH_GITEA_ADMIN_PASSWORD_FILE, MESH_GITEA_STATE_DIR/,
|
||||
() => GiteaClient.fromEnv({ MESH_GITEA_URL: forge.url, MESH_GITEA_ADMIN_USER: ADMIN, MESH_GITEA_TOOLS_USER: "mesh-tools-nothing" }),
|
||||
/set GITEA_TOKEN, or MESH_GITEA_ADMIN_PASSWORD_FILE/,
|
||||
);
|
||||
});
|
||||
|
||||
|
||||
@@ -10,11 +10,12 @@
|
||||
import { test, after } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { createServer, type IncomingMessage, type ServerResponse } from "node:http";
|
||||
import { mkdtemp, readFile, writeFile, mkdir } from "node:fs/promises";
|
||||
import { mkdtemp, readFile, writeFile } from "node:fs/promises";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
|
||||
import { MintedToken, TOKEN_SCOPES } from "../dist/token.js";
|
||||
import { MintedToken, TOKEN_NAME, TOKEN_SCOPES, leaseOverState, openValue, vaultOverBus } from "../dist/token.js";
|
||||
import { fakeBus } from "./fakebus.ts";
|
||||
import { GiteaClient } from "../dist/client.js";
|
||||
import { TOOLS_ACCOUNT, TOOLS_TEAM_UNITS, BUILDER_ACCOUNT, AGENT_ACCOUNT, meshAccounts } from "../dist/tools-account.js";
|
||||
import { PACKAGES_ACCOUNT, PACKAGES_TEAM_UNITS, PACKAGES_TOKEN_REFUSED, keepPackagesToken, packagesToken, renewOnRefusal } from "../dist/packages-account.js";
|
||||
@@ -266,7 +267,12 @@ function fakeForge() {
|
||||
const forge = await fakeForge();
|
||||
after(() => forge.close());
|
||||
|
||||
/** The bus of the environment made last: the vault's secrets and the lease (design 47 H2 §808). */
|
||||
let bus = fakeBus();
|
||||
const vaulted = (account: string) => { const v = bus.vault.get(`module/gitea/${account}`)?.value; return v === undefined ? undefined : openValue(v).token; };
|
||||
|
||||
async function environment(keepMainRules = false): Promise<NodeJS.ProcessEnv & { MESH_GITEA_STATE_DIR: string }> {
|
||||
bus = fakeBus();
|
||||
const dir = await mkdtemp(join(tmpdir(), "gitea-tools-account-"));
|
||||
const passwordFile = join(dir, "admin.secret");
|
||||
await writeFile(passwordFile, PASSWORD + "\n", { mode: 0o600 });
|
||||
@@ -281,7 +287,7 @@ async function environment(keepMainRules = false): Promise<NodeJS.ProcessEnv & {
|
||||
};
|
||||
}
|
||||
|
||||
/** A source as a fresh process builds it from the manifest's environment (fromEnv shares one per file). */
|
||||
/** A source as a fresh process builds it from the manifest's environment (fromEnv shares one per account and state). */
|
||||
function fresh(env: NodeJS.ProcessEnv): GiteaClient {
|
||||
return GiteaClient.fromEnv(env);
|
||||
}
|
||||
@@ -293,9 +299,9 @@ test("the tools' token is minted for an account of their own, which is no admini
|
||||
const account = forge.users.get(TOOLS_ACCOUNT);
|
||||
assert.ok(account, "the module made the tools' account");
|
||||
assert.equal(account!.admin, false, "the tools' account is no administrator");
|
||||
const held = forge.tokens.filter((t) => t.name === "mesh-tools");
|
||||
assert.deepEqual(held.map((t) => t.user), [TOOLS_ACCOUNT], "the one token is the tools' account's, none is the administrator's");
|
||||
assert.equal(await readFile(join(env.MESH_GITEA_STATE_DIR, "token"), "utf8"), held[0].sha1 + "\n");
|
||||
const held = forge.tokens.filter((t) => t.name === "mesh-tools-1" || t.name === "mesh-tools");
|
||||
assert.deepEqual(held.map((t) => [t.user, t.name]), [[TOOLS_ACCOUNT, "mesh-tools-1"]], "the one token is the tools' account's, none is the administrator's");
|
||||
assert.equal(vaulted(TOOLS_ACCOUNT), held[0].sha1, "kept in the vault as the module's own (design 47 H2 §808)");
|
||||
|
||||
const team = forge.teams.find((t) => t.name === "mesh-tools");
|
||||
assert.ok(team, "the team is made in the mesh's organisation");
|
||||
@@ -334,17 +340,13 @@ test("the administrator's token from before goes: a kept admin token is minted a
|
||||
// What the module kept before ADR 0365: a token of the administrator's own, by the same name, its scopes beside it.
|
||||
const old = { id: 900, name: "mesh-tools", sha1: "sha-admin-old", user: ADMIN, scopes: ["write:repository", "write:issue", "read:user"] };
|
||||
forge.tokens.push(old);
|
||||
await mkdir(env.MESH_GITEA_STATE_DIR, { recursive: true });
|
||||
await writeFile(join(env.MESH_GITEA_STATE_DIR, "token"), old.sha1 + "\n", { mode: 0o600 });
|
||||
await writeFile(join(env.MESH_GITEA_STATE_DIR, "token.scopes"), "read:user,write:issue,write:repository\n", { mode: 0o600 });
|
||||
|
||||
await fresh(env).listRepos();
|
||||
|
||||
assert.ok(!forge.tokens.some((t) => t.user === ADMIN), "the administrator holds no tools' token any more");
|
||||
const kept = (await readFile(join(env.MESH_GITEA_STATE_DIR, "token"), "utf8")).trim();
|
||||
const kept = vaulted(TOOLS_ACCOUNT)!;
|
||||
assert.notEqual(kept, old.sha1);
|
||||
assert.equal(forge.tokens.find((t) => t.sha1 === kept)?.user, TOOLS_ACCOUNT);
|
||||
assert.match(await readFile(join(env.MESH_GITEA_STATE_DIR, "token.scopes"), "utf8"), / for mesh-tools\n$/);
|
||||
});
|
||||
|
||||
test("a tools' account someone made an administrator is made a plain account again at the next mint", async () => {
|
||||
@@ -363,17 +365,24 @@ test("the manifest hands the tools their account, and the Go forge bundle holds
|
||||
const manifest = JSON.parse(await readFile(new URL("../module.json", import.meta.url), "utf8"));
|
||||
const code = manifest.build.artifacts.find((a: any) => a.name === "code");
|
||||
assert.equal(code.env.MESH_GITEA_TOOLS_USER, TOOLS_ACCOUNT);
|
||||
// The Go forge bundle reads the token the tools' account holds, never the administrator's password.
|
||||
// The Go forge bundle reads the token the tools' account holds, never the administrator's password; through the
|
||||
// vault seat's get, never a file (design 47 H2 §808, H-A-422 (1)).
|
||||
const forgeBundle = manifest.build.artifacts.find((a: any) => a.name === "forge");
|
||||
assert.ok(forgeBundle.env.MESH_GITEA_TOKEN_FILE);
|
||||
assert.equal(forgeBundle.env.MESH_GITEA_TOOLS_USER, TOOLS_ACCOUNT);
|
||||
assert.equal(forgeBundle.env.MESH_GITEA_ADMIN_PASSWORD_FILE, undefined);
|
||||
// The package registry's bundle reads with the tools' token and writes with the packages account's (design 51).
|
||||
const packages = manifest.build.artifacts.find((a: any) => a.name === "package-registry");
|
||||
assert.equal(packages.env.MESH_GITEA_TOKEN_FILE, forgeBundle.env.MESH_GITEA_TOKEN_FILE);
|
||||
assert.equal(packages.env.MESH_GITEA_PACKAGES_USER, PACKAGES_ACCOUNT);
|
||||
assert.match(packages.env.MESH_GITEA_PACKAGES_TOKEN_FILE, /^\$\{dir:runtime-state\}\/packages-token$/);
|
||||
assert.equal(packages.env.MESH_GITEA_ADMIN_PASSWORD_FILE, undefined);
|
||||
assert.equal(code.env.MESH_GITEA_STATE_DIR, "${dir:runtime-state}", "the watcher mints the packages token where the bundle reads it");
|
||||
for (const a of manifest.build.artifacts) {
|
||||
assert.equal(a.env?.MESH_GITEA_TOKEN_FILE ?? a.env?.MESH_GITEA_PACKAGES_TOKEN_FILE, undefined, `${a.name} is handed a token file`);
|
||||
}
|
||||
assert.ok(manifest.invokes.includes("seat:mesh-vault.own-put") && manifest.invokes.includes("seat:mesh-vault.get"), manifest.invokes.join(","));
|
||||
assert.ok(manifest.consumes.includes("mesh-vault.secret-changed"), "the Go bundles hear the vault's changes");
|
||||
// The lease and the two settings (§808, §820).
|
||||
assert.deepEqual(manifest.state.find((s: any) => s.name === "token-lease"), { name: "token-lease", "key-ttl": true, "max-mib": 1 });
|
||||
assert.deepEqual([manifest.config["token-ttl"]?.default, manifest.config["token-ttl"]?.runtime], ["24h", true]);
|
||||
assert.deepEqual([manifest.config["mint-lease"]?.default, manifest.config["mint-lease"]?.runtime], ["60s", true]);
|
||||
});
|
||||
|
||||
test("the package seats write as an account of their own: plain, in a team writing only packages, its token write:package alone, renewed when the forge rejects it", async () => {
|
||||
@@ -383,10 +392,10 @@ test("the package seats write as an account of their own: plain, in a team writi
|
||||
const account = forge.users.get(PACKAGES_ACCOUNT);
|
||||
assert.ok(account, "the module made the packages account");
|
||||
assert.equal(account!.admin, false, "the packages account is no administrator");
|
||||
const held = forge.tokens.filter((t) => t.name === "mesh-packages");
|
||||
assert.deepEqual(held.map((t) => [t.user, t.scopes]), [[PACKAGES_ACCOUNT, ["write:package"]]]);
|
||||
assert.equal(await readFile(join(env.MESH_GITEA_STATE_DIR, "packages-token"), "utf8"), held[0].sha1 + "\n");
|
||||
assert.equal(await readFile(join(env.MESH_GITEA_STATE_DIR, "packages-token.scopes"), "utf8"), "write:package for mesh-packages\n");
|
||||
const named = (t: { name: string }) => t.name === "mesh-packages" || t.name.startsWith("mesh-packages-");
|
||||
const held = forge.tokens.filter(named);
|
||||
assert.deepEqual(held.map((t) => [t.user, t.name, t.scopes]), [[PACKAGES_ACCOUNT, "mesh-packages-1", ["write:package"]]]);
|
||||
assert.equal(vaulted(PACKAGES_ACCOUNT), held[0].sha1, "kept in the vault as module/gitea/mesh-packages (H-A-426)");
|
||||
const team = forge.teams.find((t) => t.name === "mesh-packages");
|
||||
assert.ok(team?.members.has(PACKAGES_ACCOUNT));
|
||||
assert.deepEqual(team!.units.units_map, PACKAGES_TEAM_UNITS.units_map, "the team writes packages and nothing else");
|
||||
@@ -398,11 +407,12 @@ test("the package seats write as an account of their own: plain, in a team writi
|
||||
const renew = renewOnRefusal(source, forge.url, { delete: async (k) => { cleared.push(k); } }, (l) => said.push(l));
|
||||
await renew({ key: "seat.gitea.other", op: "put" });
|
||||
await renew({ key: PACKAGES_TOKEN_REFUSED, op: "delete" });
|
||||
assert.equal(forge.tokens.filter((t) => t.name === "mesh-packages").length, 0, "nothing renews but the refusal");
|
||||
assert.equal(forge.tokens.filter(named).length, 0, "nothing renews but the refusal");
|
||||
await renew({ key: PACKAGES_TOKEN_REFUSED, op: "put" });
|
||||
assert.deepEqual(said, ["the forge rejected the package seats' write token; minted afresh"]);
|
||||
assert.deepEqual(cleared, [PACKAGES_TOKEN_REFUSED]);
|
||||
assert.equal(forge.tokens.filter((t) => t.name === "mesh-packages").length, 1);
|
||||
assert.deepEqual(forge.tokens.filter(named).map((t) => t.name), ["mesh-packages-2"]);
|
||||
assert.equal(vaulted(PACKAGES_ACCOUNT), forge.tokens.find(named)!.sha1);
|
||||
// The tools' token reads packages too: read:package (design 51's read-only tool token).
|
||||
assert.ok(TOKEN_SCOPES.includes("read:package"));
|
||||
});
|
||||
@@ -433,12 +443,12 @@ test("an old token that cannot be deleted after the mint is said, the new token
|
||||
} finally {
|
||||
forge.faults.deleteToken = false;
|
||||
}
|
||||
const kept = (await readFile(join(env.MESH_GITEA_STATE_DIR, "token"), "utf8")).trim();
|
||||
const kept = vaulted(TOOLS_ACCOUNT)!;
|
||||
assert.equal(forge.tokens.find((t) => t.sha1 === kept)?.user, TOOLS_ACCOUNT, "the account's token is kept and works");
|
||||
assert.ok(forge.tokens.some((t) => t.sha1 === old.sha1));
|
||||
// The next process reuses the kept token and retires the old one it could not before.
|
||||
// The next process reuses the vault's token and retires the old one it could not before.
|
||||
const next = new MintedToken({ url: forge.url, admin: ADMIN, passwordFile: env.MESH_GITEA_ADMIN_PASSWORD_FILE!,
|
||||
file: join(env.MESH_GITEA_STATE_DIR, "token"), account: TOOLS_ACCOUNT, log: () => {} });
|
||||
account: TOOLS_ACCOUNT, oldName: TOKEN_NAME, vault: vaultOverBus(), lease: leaseOverState(), stateDir: env.MESH_GITEA_STATE_DIR, log: () => {} });
|
||||
assert.equal(await next.current(), kept);
|
||||
assert.ok(!forge.tokens.some((t) => t.sha1 === old.sha1), "the admin's old token was deleted at the next start");
|
||||
});
|
||||
@@ -548,7 +558,7 @@ test("the start's pass gives each of the mesh's accounts read, as a collaborator
|
||||
assert.deepEqual((await fresh(env).listAllRepos()).map((r) => r.full_name).sort(), ["jschoubben/txt-game", "novox/hq"]);
|
||||
|
||||
// The account's own token cannot grant itself: the forge asks the repository's administration (the reason for the admin login).
|
||||
const own = (await readFile(join(env.MESH_GITEA_STATE_DIR, "token"), "utf8")).trim();
|
||||
const own = vaulted(TOOLS_ACCOUNT)!;
|
||||
const self = await fetch(`${forge.url}/api/v1/repos/jschoubben/car-hunter/collaborators/${TOOLS_ACCOUNT}`,
|
||||
{ method: "PUT", headers: { Authorization: `token ${own}`, "Content-Type": "application/json" }, body: JSON.stringify({ permission: "read" }) });
|
||||
assert.equal(self.status, 404, "a repository the account cannot read is not even there for it");
|
||||
|
||||
+393
-260
@@ -4,45 +4,88 @@
|
||||
// paste in, and pasting one into settings would put a secret in the inventory in plaintext. What
|
||||
// the mesh does deliver is the admin account: a login the manifest names and a password the vault
|
||||
// minted and the host unsealed into a file (novox/hq ADR 0086). That account is enough to mint a
|
||||
// token, so the module mints its own (hq issue 100, the forge's tools):
|
||||
// token, so the module mints its own (hq issue 100, the forge's tools), for each of its two accounts, the tools'
|
||||
// (`mesh-tools`) and the package seats' (`mesh-packages`):
|
||||
//
|
||||
// - at first use, when none is kept: POST /users/{admin}/tokens over basic auth, with the two
|
||||
// scopes the tools and the watcher need, and no more;
|
||||
// - kept in the module's own state, a 0600 file, and read back on the next start — the forge
|
||||
// hands a token's value out exactly once, so a token not kept is a token lost;
|
||||
// - re-minted when the forge rejects it (401) or the kept file is gone. The one case that is not
|
||||
// a fault: the forge's data was restored from a predecessor and the token the file names never
|
||||
// existed there;
|
||||
// - minted by one process at a time. The runtime starts each of the bundle's entrypoints as a process
|
||||
// of its own (novox/hq ADR 0193): the watcher (index.js) and the tools (tools/index.js) are two
|
||||
// processes over one kept file. Two that minted at once left the forge two tokens by one name, which
|
||||
// gitea then refuses to delete by name — for ever, and every forge tool with it (the 2026-10-14
|
||||
// outage). So a mint holds a lock file beside the kept token, and reads the kept file again once it
|
||||
// holds it: what another process minted meanwhile is reused, not minted over;
|
||||
// - replaced by id: an old token by this name is found in the account's list and deleted by its id,
|
||||
// every one of them, so a name held twice is cleared rather than refused.
|
||||
//
|
||||
// - minted for the tools' account, not the administrator (novox/hq ADR 0365 §2, issue 469): the administrator's
|
||||
// login mints it for `mesh-tools` (tools-account.ts), after making that account and its team, and deletes any
|
||||
// token by this name the administrator itself still holds — the token the tools used before. A token kept
|
||||
// for another account than the one asked (the scopes line says whose) is minted again at the next start.
|
||||
// - **kept as a secret, the module's own in the vault** (design 47 H2 §808, §822; H-A-422, H-A-426): written through
|
||||
// the seat `mesh-vault`'s two-step sealed write `own-put` under the account's name, so the vault keeps it as
|
||||
// `module/gitea/<account>`, its newest generation only, and read through the seat's `get`, sealed to this process's
|
||||
// key. Never a file in the runtime state and never plain in a state: the forge hands a token's value out exactly
|
||||
// once, and the vault is where the module keeps what it must not lose;
|
||||
// - **one name per mint**, `<account>-<generation>`, the generation the one this own-put makes (get's generation plus
|
||||
// one; 1 when the vault holds none; H-A-430 (1)). The token minted is written through own-put first, and only then
|
||||
// is the token it replaces deleted at the forge, by its id: no moment is without a valid token, and a mint that
|
||||
// fails leaves the old one standing. The first mint replaces the tokens the module kept before under the account's
|
||||
// old fixed name;
|
||||
// - **minted by one process at a time** (§808). The runtime starts each of the bundle's entrypoints as a process of its
|
||||
// own (novox/hq ADR 0193), and two that minted at once left the forge two tokens by one name (the 2026-10-14 outage).
|
||||
// A process the forge refuses first reads the vault's token; only when the refused token is the vault's current one
|
||||
// does it take the lease — the key `mint` in the module's state `token-lease`, created only, living `mint-lease` (a
|
||||
// setting, default 60 s) — and mint. A process that finds the lease taken waits for that key to change, at most the
|
||||
// lease, then reads the vault again; past it, the lease has expired and the waiter creates it itself;
|
||||
// - **minted again when older than `token-ttl`** (a setting, default 24 hours), its age the vault's `changed-at`;
|
||||
// - **minted again when minted with other scopes** (H-A-439): the value the vault keeps holds the token and the scopes
|
||||
// it was minted with, so a token carrying a scope beyond the ones the tools take (such as write:admin) is minted again
|
||||
// unasked, and the one it replaces deleted, as when the scopes were kept in a file beside the token;
|
||||
// - minted for the account, not the administrator (novox/hq ADR 0365 §2, issue 469): the administrator's login mints
|
||||
// it for the account, after making that account and its team, and deletes any token named `mesh-tools` the
|
||||
// administrator itself still holds — the token the tools used before.
|
||||
//
|
||||
// An explicitly configured token still wins, and is never minted over: if it is rejected, that is
|
||||
// reported, not repaired — somebody chose it.
|
||||
//
|
||||
// The token is never logged. Lines say that one was minted, reused or renewed, and where it is
|
||||
// kept; never what it is.
|
||||
// The token is never logged. Lines say that one was minted, reused or renewed, and under which name;
|
||||
// never what it is.
|
||||
|
||||
import { chmodSync, closeSync, existsSync, mkdirSync, openSync, readFileSync, renameSync, rmSync, statSync, unlinkSync, writeFileSync, writeSync } from "node:fs";
|
||||
import { existsSync, mkdirSync, rmSync, writeFileSync, readFileSync } from "node:fs";
|
||||
import { randomBytes } from "node:crypto";
|
||||
import { dirname, join } from "node:path";
|
||||
import { join } from "node:path";
|
||||
import { generateKeyPair, open, sealAnonymous, type Box, type KeyPair } from "@novox/mesh-sdk/seal";
|
||||
import { broker } from "@novox/mesh-sdk/messaging";
|
||||
import { askRuntime } from "@novox/mesh-sdk/runtime";
|
||||
import { state, type State } from "@novox/mesh-sdk/state";
|
||||
import { setUpToolsAccount, TOOLS_ACCOUNT, type AdminCall } from "./tools-account.js";
|
||||
|
||||
/** The name the token carries in the forge's own list — one per mesh runtime, found by name. */
|
||||
import { keepMainRules } from "./mainrules.js";
|
||||
import { durationMs } from "./taken.js";
|
||||
|
||||
/** The tools' token's old fixed name in the forge's list, the one the admin's own token carried too (ADR 0365 §2). */
|
||||
export const TOKEN_NAME = "mesh-tools";
|
||||
|
||||
/** The module whose own secrets the vault keeps its tokens as (§822: the module named by the caller's account). */
|
||||
export const VAULT_MODULE = "gitea";
|
||||
|
||||
/** The vault's name of an account's token: `module/gitea/<account>` (H-A-426). */
|
||||
export function vaultName(account: string): string {
|
||||
return `module/${VAULT_MODULE}/${account}`;
|
||||
}
|
||||
|
||||
/** The forge's name of an account's token of one generation: `<account>-<generation>` (H-A-422 (2)). */
|
||||
export function mintName(account: string, generation: number): string {
|
||||
return `${account}-${generation}`;
|
||||
}
|
||||
|
||||
/** The state the mint's lease is kept in, and its one key (§808, §820). */
|
||||
export const LEASE_STATE = "token-lease";
|
||||
export const LEASE_KEY = "mint";
|
||||
|
||||
/** token-ttl's and mint-lease's defaults (§808). */
|
||||
export const TOKEN_TTL_DEFAULT = "24h";
|
||||
export const MINT_LEASE_DEFAULT = "60s";
|
||||
|
||||
/** A setting the node-engine composes as `MESH_CONFIG_<NAME>`, a duration, in milliseconds; the default when unset, and
|
||||
* when it is no duration (said). */
|
||||
export function settingMs(env: NodeJS.ProcessEnv, setting: string, fallback: string, log: (line: string) => void = () => {}): number {
|
||||
const raw = env[`MESH_CONFIG_${setting.toUpperCase().replace(/-/g, "_")}`];
|
||||
const dflt = durationMs(fallback)!;
|
||||
if (raw === undefined || raw.trim() === "") return dflt;
|
||||
const ms = durationMs(raw);
|
||||
if (ms === null) {
|
||||
log(`the setting ${setting} is ${JSON.stringify(raw)}, no duration: the default ${fallback} holds`);
|
||||
return dflt;
|
||||
}
|
||||
return ms;
|
||||
}
|
||||
|
||||
/**
|
||||
* The least the fifteen tools and the watcher need (gitea's route groups, 1.20+ scoped tokens):
|
||||
* write:repository — create/delete repositories, pull requests (list/get/open/merge);
|
||||
@@ -58,8 +101,6 @@ export const TOKEN_NAME = "mesh-tools";
|
||||
* **Not write:admin** (novox/hq ADR 0266). It was here so a person could settle the forge's users through
|
||||
* `gitea_api` (novox/hq 229); whoever may call a tool includes agents, and the forge's administration is the
|
||||
* operator's. The provisioner settles users with the admin account's own login (GiteaAdmin), never this token.
|
||||
* A token kept with a different list is minted again with this one at the next start (the scopes are kept
|
||||
* beside it), and the old one is deleted from the forge by its id — so the scope a kept token had does not outlive this.
|
||||
*
|
||||
* A token kept from before a scope was added lacks it: the forge answers such a call with
|
||||
* `403 token does not have at least one of required scope(s)`, and the client treats that like a
|
||||
@@ -91,19 +132,135 @@ export class ConfiguredToken implements TokenSource {
|
||||
}
|
||||
}
|
||||
|
||||
/** The forge would not take the admin account: it is missing, or its password is not the one the mesh holds. */
|
||||
/** The forge would not take the admin account: it refused the login the mesh holds (design 47 H2 §776). Said as the
|
||||
* forge's refusal, never as an account to create: whether the forge is away is the seat's to judge (seat.ts). */
|
||||
export class AdminRefused extends Error {
|
||||
constructor(admin: string, status: number) {
|
||||
super(
|
||||
`the forge refused the admin account "${admin}" (${status}) — it does not exist there, or its password is not ` +
|
||||
`the one the vault delivered. The admin-bootstrap step creates it on a forge the mesh raised; a forge whose data ` +
|
||||
`came from a predecessor does not have it. Create "${admin}" on the forge with the delivered password and the ` +
|
||||
`token is minted on the next call — the tools stay registered and the watcher keeps trying`,
|
||||
);
|
||||
super(`the forge refuses the mesh's admin account "${admin}" (${status})`);
|
||||
this.name = "AdminRefused";
|
||||
}
|
||||
}
|
||||
|
||||
/** A token as the vault holds it: the token, the scopes it was minted with, the generation and when it changed (`get`'s
|
||||
* answer, opened). */
|
||||
export interface VaultToken {
|
||||
readonly token: string;
|
||||
readonly scopes: readonly string[];
|
||||
readonly generation: number;
|
||||
readonly changedAt: string;
|
||||
}
|
||||
|
||||
/** The value the vault keeps for an account: the token and the scopes it was minted with (§808, H-A-439). */
|
||||
export function vaultValue(token: string, scopes: readonly string[]): string {
|
||||
return JSON.stringify({ token, scopes: [...scopes] });
|
||||
}
|
||||
|
||||
/** A value the vault keeps, opened: its token and scopes. A value of another form names no scopes, so it is minted again. */
|
||||
export function openValue(value: string): { token: string; scopes: readonly string[] } {
|
||||
try {
|
||||
const v = JSON.parse(value);
|
||||
if (v && typeof v.token === "string" && Array.isArray(v.scopes)) return { token: v.token, scopes: v.scopes.map(String) };
|
||||
} catch {
|
||||
// not the token and its scopes
|
||||
}
|
||||
return { token: value.trim(), scopes: [] };
|
||||
}
|
||||
|
||||
/** Whether two lists name the same scopes, in any order. */
|
||||
function sameScopes(a: readonly string[], b: readonly string[]): boolean {
|
||||
return [...a].sort().join(",") === [...b].sort().join(",");
|
||||
}
|
||||
|
||||
/** The vault as the minter reaches it: the seat `mesh-vault`'s `get` and `own-put` (§808, §822). */
|
||||
export interface TokenVault {
|
||||
/** The account's token the vault holds, or null when it holds none. */
|
||||
get(account: string): Promise<VaultToken | null>;
|
||||
/** Write the account's token and the scopes it was minted with as the module's own; answers the generation the vault kept. */
|
||||
put(account: string, token: string, scopes: readonly string[]): Promise<number>;
|
||||
}
|
||||
|
||||
/** The lease `mint` in the state `token-lease` (§808). */
|
||||
export interface MintLease {
|
||||
/** Create the lease for `holder`, living `ms`; false when another holds it. */
|
||||
take(holder: string, ms: number): Promise<boolean>;
|
||||
/** The lease's holder and since when, or null when nobody holds it. */
|
||||
read(): Promise<{ holder: string; since: string } | null>;
|
||||
/** Delete the lease, when `holder` still holds it. */
|
||||
release(holder: string): Promise<void>;
|
||||
/** Resolves when the lease's key changes (deleted, or written again), or after `ms`, whichever is first. */
|
||||
changed(ms: number): Promise<void>;
|
||||
}
|
||||
|
||||
/** The vault's words when it holds no secret by the name asked (mesh-vault's Get). */
|
||||
const HOLDS_NONE = /the vault holds no secret named/;
|
||||
|
||||
/** The vault over the bus: `get` sealed to this process's own key, made once a process; `own-put` in its two steps. */
|
||||
export function vaultOverBus(request: (verb: string, body: unknown) => Promise<unknown> = (v, b) => broker().request(v, b)): TokenVault {
|
||||
let keys: KeyPair | undefined;
|
||||
return {
|
||||
async get(account) {
|
||||
keys ??= generateKeyPair();
|
||||
let answer: { generation?: number; "changed-at"?: string; sealed?: Box };
|
||||
try {
|
||||
answer = (await request("seat:mesh-vault.get", { name: vaultName(account), public_key: keys.publicKey })) as typeof answer;
|
||||
} catch (err) {
|
||||
if (HOLDS_NONE.test(err instanceof Error ? err.message : String(err))) return null;
|
||||
throw err;
|
||||
}
|
||||
if (!answer?.sealed) throw new Error(`the vault answered no sealed ${vaultName(account)}`);
|
||||
return { ...openValue(open(answer.sealed, keys.privateKey)), generation: answer.generation ?? 0, changedAt: answer["changed-at"] ?? "" };
|
||||
},
|
||||
async put(account, token, scopes) {
|
||||
const opened = (await request("seat:mesh-vault.own-put", { name: account })) as { call?: string; seal_to?: string };
|
||||
if (!opened?.call || !opened.seal_to) throw new Error(`the vault opened no own-put call for ${vaultName(account)}`);
|
||||
const kept = (await request("seat:mesh-vault.own-put", { call: opened.call, sealed: sealAnonymous(vaultValue(token, scopes), opened.seal_to) })) as { generation?: number };
|
||||
return kept?.generation ?? 0;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
/** The runtime's refusal of a create whose key has a value (mesh-sdk's CODE_CHANGED). */
|
||||
const CODE_CHANGED = -32010;
|
||||
|
||||
/** The lease in the module's state `token-lease` over the runtime: a create with the lease as its time to live, and one
|
||||
* watch of the key a process, handing each change to the waiters. */
|
||||
export function leaseOverState(st: State<{ holder: string; since: string }> = state(LEASE_STATE),
|
||||
ask: (method: string, params: unknown) => Promise<unknown> = askRuntime): MintLease {
|
||||
const waiters = new Set<() => void>();
|
||||
let watching: Promise<void> | undefined;
|
||||
return {
|
||||
async take(holder, ms) {
|
||||
try {
|
||||
await ask("mesh/state.create", { state: LEASE_STATE, key: LEASE_KEY, value: { holder, since: new Date().toISOString() },
|
||||
ttl_seconds: Math.max(1, Math.ceil(ms / 1000)) });
|
||||
return true;
|
||||
} catch (err) {
|
||||
if ((err as { code?: number }).code === CODE_CHANGED) return false;
|
||||
throw err;
|
||||
}
|
||||
},
|
||||
async read() {
|
||||
return (await st.get(LEASE_KEY))?.value ?? null;
|
||||
},
|
||||
async release(holder) {
|
||||
if ((await st.get(LEASE_KEY))?.value?.holder === holder) await st.delete(LEASE_KEY);
|
||||
},
|
||||
async changed(ms) {
|
||||
watching ??= st.watch((change) => {
|
||||
if (!change.current) for (const wake of [...waiters]) wake();
|
||||
}, { key: LEASE_KEY });
|
||||
await watching;
|
||||
await new Promise<void>((resolve) => {
|
||||
const wake = () => { clearTimeout(timer); waiters.delete(wake); resolve(); };
|
||||
const timer = setTimeout(wake, ms);
|
||||
waiters.add(wake);
|
||||
// Released between the refused create and this wait: nothing to wait for.
|
||||
void st.get(LEASE_KEY).then((e) => { if (!e) wake(); }, () => {});
|
||||
});
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export interface MintedTokenOptions {
|
||||
/** The forge, e.g. http://127.0.0.1:3000. */
|
||||
readonly url: string;
|
||||
@@ -111,115 +268,121 @@ export interface MintedTokenOptions {
|
||||
readonly admin: string;
|
||||
/** The file the host unsealed the admin password into (ADR 0086). Read at mint time, so a rotation takes. */
|
||||
readonly passwordFile: string;
|
||||
/** Where the token is kept: a 0600 file in the module's own state. */
|
||||
readonly file: string;
|
||||
readonly name?: string;
|
||||
/** The account the token is minted for, and the vault name it is kept under (H-A-426). */
|
||||
readonly account: string;
|
||||
/** The token's fixed name before a name per mint: the first mint deletes the account's tokens by it (H-A-430 (1)). */
|
||||
readonly oldName: string;
|
||||
readonly scopes?: readonly string[];
|
||||
/**
|
||||
* The account the token is minted for: the tools' own (novox/hq ADR 0365 §2). The admin login mints it, after
|
||||
* `setUp` made the account, and the admin's own tokens by this name are deleted. Left out, the token is the
|
||||
* admin's own, as it was before.
|
||||
*/
|
||||
readonly account?: string;
|
||||
/** Run with the admin's password before each mint for `account`: makes the account and its team (tools-account.ts). */
|
||||
/** Run with the admin's password before each mint: makes the account and its team (tools-account.ts). */
|
||||
readonly setUp?: (password: string) => Promise<void>;
|
||||
/** Where the module keeps its token, and the mint's lease (§808). */
|
||||
readonly vault: TokenVault;
|
||||
readonly lease: MintLease;
|
||||
/** token-ttl and mint-lease, in milliseconds (§808). */
|
||||
readonly ttlMs?: number;
|
||||
readonly leaseMs?: number;
|
||||
/** Who holds the lease when this process does: the node and the process. */
|
||||
readonly holder?: string;
|
||||
/** The module's runtime state directory, where a retirement of the admin's old token that failed is remembered. */
|
||||
readonly stateDir?: string;
|
||||
readonly now?: () => Date;
|
||||
readonly log?: (line: string) => void;
|
||||
readonly fetch?: typeof fetch;
|
||||
}
|
||||
|
||||
/** The token the module mints for itself, kept in its state and renewed when the forge rejects it. */
|
||||
/** The token the module mints for itself, kept in the vault and renewed when the forge rejects it. */
|
||||
export class MintedToken implements TokenSource {
|
||||
private held: string | null = null;
|
||||
private readFile = false;
|
||||
private held: VaultToken | null = null;
|
||||
private inflight: Promise<string> | null = null;
|
||||
private readonly name: string;
|
||||
private readonly scopes: readonly string[];
|
||||
private readonly account: string;
|
||||
private readonly ttlMs: number;
|
||||
private readonly leaseMs: number;
|
||||
private readonly holder: string;
|
||||
private readonly now: () => Date;
|
||||
private readonly log: (line: string) => void;
|
||||
private readonly fetchImpl: typeof fetch;
|
||||
|
||||
constructor(private readonly opts: MintedTokenOptions) {
|
||||
this.name = opts.name ?? TOKEN_NAME;
|
||||
this.scopes = opts.scopes ?? TOKEN_SCOPES;
|
||||
this.account = opts.account ?? opts.admin;
|
||||
this.ttlMs = opts.ttlMs ?? durationMs(TOKEN_TTL_DEFAULT)!;
|
||||
this.leaseMs = opts.leaseMs ?? durationMs(MINT_LEASE_DEFAULT)!;
|
||||
this.holder = opts.holder ?? `${process.env.MESH_NODE || "unknown"}/${process.pid}`;
|
||||
this.now = opts.now ?? (() => new Date());
|
||||
this.log = opts.log ?? ((line) => console.log(`[gitea] ${line}`));
|
||||
this.fetchImpl = opts.fetch ?? fetch;
|
||||
}
|
||||
|
||||
/** The account this source mints for. */
|
||||
get account(): string {
|
||||
return this.opts.account;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build from the runtime's environment: the forge's URL, the admin login and password file the
|
||||
* manifest hands the runtime, and the module's state directory (MESH_GITEA_STATE_DIR, a directory
|
||||
* the runtime mounts writable). Throws, naming what is missing, rather than hand back a source
|
||||
* that cannot mint.
|
||||
* The tools' token from the runtime's environment: the forge's URL, the admin login and password file the manifest
|
||||
* hands the runtime, the vault and the lease over the bus. Throws, naming what is missing, rather than hand back a
|
||||
* source that cannot mint. One source per account and runtime state in a process: two would each renew on a 401.
|
||||
*/
|
||||
static fromEnv(url: string, env: NodeJS.ProcessEnv = process.env): MintedToken {
|
||||
const opts = MintedToken.optionsFromEnv(url, env);
|
||||
// One source per kept file in a process: two sources in one process would each renew on a 401.
|
||||
// This covers one process only. The watcher and the tools are two processes (the runtime starts
|
||||
// each entrypoint as its own), and those are kept apart by the mint's lock file (mint, below).
|
||||
const shared = MintedToken.shared.get(opts.file);
|
||||
const account = env.MESH_GITEA_TOOLS_USER || TOOLS_ACCOUNT;
|
||||
const { admin, passwordFile } = adminFromEnv(env, "no Gitea token, and nothing to mint one with — set GITEA_TOKEN, or");
|
||||
const key = `${account} ${env.MESH_GITEA_STATE_DIR ?? ""}`;
|
||||
const shared = MintedToken.shared.get(key);
|
||||
if (shared) return shared;
|
||||
const source = new MintedToken(opts);
|
||||
MintedToken.shared.set(opts.file, source);
|
||||
if (account === admin) throw new Error(`MESH_GITEA_TOOLS_USER names the admin account "${admin}"; the tools act as an account of their own (novox/hq ADR 0365)`);
|
||||
const source = new MintedToken({
|
||||
...MintedToken.settingsFromEnv(env), url, admin, passwordFile, account, oldName: TOKEN_NAME,
|
||||
// GiteaAdmin is loaded when first used: client.ts imports this file.
|
||||
setUp: async (password) => {
|
||||
const { GiteaAdmin } = await import("./client.js");
|
||||
await setUpToolsAccount(new GiteaAdmin(url, admin, password), account, () => randomBytes(24).toString("base64url"), keepMainRules(env));
|
||||
},
|
||||
});
|
||||
MintedToken.shared.set(key, source);
|
||||
return source;
|
||||
}
|
||||
|
||||
private static readonly shared = new Map<string, MintedToken>();
|
||||
|
||||
private static optionsFromEnv(url: string, env: NodeJS.ProcessEnv): MintedTokenOptions {
|
||||
const admin = env.MESH_GITEA_ADMIN_USER;
|
||||
const passwordFile = env.MESH_GITEA_ADMIN_PASSWORD_FILE;
|
||||
const stateDir = env.MESH_GITEA_STATE_DIR;
|
||||
const missing = [
|
||||
admin ? null : "MESH_GITEA_ADMIN_USER",
|
||||
passwordFile ? null : "MESH_GITEA_ADMIN_PASSWORD_FILE",
|
||||
stateDir ? null : "MESH_GITEA_STATE_DIR",
|
||||
].filter((v): v is string => v !== null);
|
||||
if (missing.length) {
|
||||
throw new Error(`no Gitea token, and nothing to mint one with — set GITEA_TOKEN, or ${missing.join(", ")}`);
|
||||
}
|
||||
// The tools' own account, never the administrator (novox/hq ADR 0365 §2): MESH_GITEA_TOOLS_USER names it.
|
||||
const account = env.MESH_GITEA_TOOLS_USER || TOOLS_ACCOUNT;
|
||||
if (account === admin) throw new Error(`MESH_GITEA_TOOLS_USER names the admin account "${admin}"; the tools act as an account of their own (novox/hq ADR 0365)`);
|
||||
/** What every account's source takes from the environment: the vault and lease over the bus, the two settings. */
|
||||
static settingsFromEnv(env: NodeJS.ProcessEnv): Pick<MintedTokenOptions, "vault" | "lease" | "ttlMs" | "leaseMs" | "stateDir"> {
|
||||
const log = (line: string) => console.log(`[gitea] ${line}`);
|
||||
return {
|
||||
url, admin: admin!, passwordFile: passwordFile!, file: join(stateDir!, "token"), account,
|
||||
// GiteaAdmin is loaded when first used: client.ts imports this file.
|
||||
setUp: async (password) => {
|
||||
const { GiteaAdmin } = await import("./client.js");
|
||||
await setUpToolsAccount(new GiteaAdmin(url, admin!, password), account, () => randomBytes(24).toString("base64url"), keepMainRules(env));
|
||||
},
|
||||
vault: sharedVault(), lease: sharedLease(),
|
||||
ttlMs: settingMs(env, "token-ttl", TOKEN_TTL_DEFAULT, log), leaseMs: settingMs(env, "mint-lease", MINT_LEASE_DEFAULT, log),
|
||||
stateDir: env.MESH_GITEA_STATE_DIR || undefined,
|
||||
};
|
||||
}
|
||||
|
||||
/** The token held, read from the vault when none is, minted when the vault holds none or one older than token-ttl. */
|
||||
async current(): Promise<string> {
|
||||
if (this.held !== null) return this.held;
|
||||
if (!this.readFile) {
|
||||
this.readFile = true;
|
||||
const kept = this.read();
|
||||
if (kept !== null && this.keptScopes() === this.scopesLine()) {
|
||||
this.held = kept;
|
||||
this.log(`reusing the token kept at ${this.opts.file}`);
|
||||
// A retirement of the admin's old token that failed after the mint is tried again, once a process.
|
||||
if (existsSync(this.retireFile)) await this.locked(() => this.retireAdmin()).catch((err) => this.log(`${err}`));
|
||||
return kept;
|
||||
}
|
||||
if (kept !== null) return this.mint("the kept token was minted with other scopes than the tools take now — minting one with them", kept);
|
||||
if (this.held !== null && !this.expired(this.held)) return this.held.token;
|
||||
const kept = await this.opts.vault.get(this.opts.account);
|
||||
if (kept === null) return this.mint("the vault holds no token for this account — minting one", null);
|
||||
if (this.expired(kept)) return this.mint(`the vault's token is older than token-ttl (${this.ttlMs / 1000}s) — minting one`, kept.token);
|
||||
if (!sameScopes(kept.scopes, this.scopes)) {
|
||||
return this.mint(`the vault's token was minted with other scopes than the tools take now (${kept.scopes.join(", ") || "none kept"}) — minting one with them`, kept.token);
|
||||
}
|
||||
return this.mint("no token kept — minting one", null);
|
||||
if (this.held === null) {
|
||||
this.log(`reusing the token the vault keeps as ${vaultName(this.opts.account)} (generation ${kept.generation})`);
|
||||
// A retirement of the admin's old token that failed after the mint is tried again, once a process.
|
||||
if (this.retireFile && existsSync(this.retireFile)) await this.retireAdmin().catch((err) => this.log(`${err}`));
|
||||
}
|
||||
this.held = kept;
|
||||
return kept.token;
|
||||
}
|
||||
|
||||
async renew(rejected: string): Promise<string> {
|
||||
// Another caller already renewed while this one was in flight with the old token.
|
||||
if (this.held !== null && this.held !== rejected) return this.held;
|
||||
// Or another process did, and kept it: use what is kept before minting over it.
|
||||
const kept = this.read();
|
||||
if (kept !== null && kept !== rejected) {
|
||||
if (this.held !== null && this.held.token !== rejected) return this.held.token;
|
||||
// A refusal of a stale token mints nothing: the vault's newer one is used (§808).
|
||||
const kept = await this.opts.vault.get(this.opts.account);
|
||||
if (kept !== null && kept.token !== rejected) {
|
||||
this.held = kept;
|
||||
this.log(`the forge rejected the token held; the kept one at ${this.opts.file} is newer — reusing it`);
|
||||
return kept;
|
||||
this.log(`the forge rejected the token held; the vault's ${vaultName(this.opts.account)} is newer (generation ${kept.generation}) — reusing it`);
|
||||
return kept.token;
|
||||
}
|
||||
this.held = null;
|
||||
return this.mint("the forge rejected the kept token — minting a fresh one", rejected);
|
||||
return this.mint("the forge rejected the vault's token — minting a fresh one", rejected);
|
||||
}
|
||||
|
||||
/** What the forge's scoped tokens say when a kept token predates a scope the tools now need. */
|
||||
@@ -227,132 +390,55 @@ export class MintedToken implements TokenSource {
|
||||
return status === 403 && /required scope/i.test(body);
|
||||
}
|
||||
|
||||
private expired(t: VaultToken): boolean {
|
||||
const at = Date.parse(t.changedAt);
|
||||
return Number.isFinite(at) && this.now().getTime() - at > this.ttlMs;
|
||||
}
|
||||
|
||||
/**
|
||||
* One mint at a time: concurrent first calls in this process share it, and a mint in another process
|
||||
* on this machine is waited for (the lock file). Once the lock is held, the kept file is read again:
|
||||
* a token there that is not `stale` (the one rejected, or kept with other scopes) and carries the
|
||||
* scopes asked for was minted by the process that held the lock before — it is reused.
|
||||
* One mint at a time: concurrent calls in this process share it, and a mint in another process is waited for (the
|
||||
* lease). Holding the lease, the vault is read again: a token there that is not `stale` (the one refused or expired)
|
||||
* was minted by the process that held the lease before — it is used, not minted over.
|
||||
*/
|
||||
private mint(why: string, stale: string | null): Promise<string> {
|
||||
if (this.inflight === null) {
|
||||
this.log(why);
|
||||
this.inflight = this.locked(async () => {
|
||||
const kept = this.read();
|
||||
if (kept !== null && kept !== stale && this.keptScopes() === this.scopesLine()) {
|
||||
this.held = kept;
|
||||
this.log(`another process minted it meanwhile — reusing the token kept at ${this.opts.file}`);
|
||||
return kept;
|
||||
}
|
||||
return this.doMint();
|
||||
}).finally(() => {
|
||||
this.inflight = this.leased(stale).finally(() => {
|
||||
this.inflight = null;
|
||||
});
|
||||
}
|
||||
return this.inflight;
|
||||
}
|
||||
|
||||
/** How long a mint may hold the lock before another process takes it as left behind, and how long one waits. */
|
||||
static readonly LOCK_STALE_MS = 120_000;
|
||||
static readonly LOCK_WAIT_MS = 150_000;
|
||||
|
||||
/**
|
||||
* Run `work` holding the lock file beside the kept token: created exclusively, holding this process's
|
||||
* pid, the time and a nonce; removed after, only when it is still ours. A lock whose process is gone,
|
||||
* or older than LOCK_STALE_MS, was left by a process that died mid-mint and is taken over — never
|
||||
* waited on for ever, which would be the same lock-out by another road.
|
||||
*/
|
||||
private async locked<T>(work: () => Promise<T>): Promise<T> {
|
||||
const lock = `${this.opts.file}.lock`;
|
||||
mkdirSync(dirname(this.opts.file), { recursive: true, mode: 0o700 });
|
||||
const mine = `${process.pid} ${Date.now()} ${randomBytes(8).toString("hex")}\n`;
|
||||
const deadline = Date.now() + MintedToken.LOCK_WAIT_MS;
|
||||
private async leased(stale: string | null): Promise<string> {
|
||||
for (;;) {
|
||||
try {
|
||||
const fd = openSync(lock, "wx", 0o600);
|
||||
try { writeSync(fd, mine); } finally { closeSync(fd); }
|
||||
break;
|
||||
} catch (err) {
|
||||
if ((err as NodeJS.ErrnoException).code !== "EEXIST") {
|
||||
throw new Error(`cannot take the mint lock at ${lock}: ${(err as Error).message}`);
|
||||
if (await this.opts.lease.take(this.holder, this.leaseMs)) {
|
||||
try {
|
||||
const kept = await this.opts.vault.get(this.opts.account);
|
||||
if (kept !== null && kept.token !== stale && !this.expired(kept) && sameScopes(kept.scopes, this.scopes)) {
|
||||
this.held = kept;
|
||||
this.log(`another process minted it meanwhile — reusing the vault's generation ${kept.generation}`);
|
||||
return kept.token;
|
||||
}
|
||||
return await this.doMint(kept);
|
||||
} finally {
|
||||
await this.opts.lease.release(this.holder).catch((err) =>
|
||||
this.log(`the mint lease could not be deleted; it ends with mint-lease — ${err instanceof Error ? err.message : err}`));
|
||||
}
|
||||
}
|
||||
if (this.leftBehind(lock)) {
|
||||
this.log(`the mint lock at ${lock} was left by a process that is gone — taking it over`);
|
||||
try { unlinkSync(lock); } catch { /* another waiter took it over first */ }
|
||||
continue;
|
||||
const other = await this.opts.lease.read().catch(() => null);
|
||||
this.log(`another process holds the mint lease${other ? ` (${other.holder} since ${other.since})` : ""} — waiting for it, at most mint-lease (${this.leaseMs / 1000}s)`);
|
||||
await this.opts.lease.changed(this.leaseMs);
|
||||
const kept = await this.opts.vault.get(this.opts.account);
|
||||
if (kept !== null && kept.token !== stale && !this.expired(kept) && sameScopes(kept.scopes, this.scopes)) {
|
||||
this.held = kept;
|
||||
this.log(`the process that held the mint lease minted generation ${kept.generation} — reusing it`);
|
||||
return kept.token;
|
||||
}
|
||||
if (Date.now() > deadline) {
|
||||
throw new Error(`another process has held the mint lock at ${lock} for over ${MintedToken.LOCK_WAIT_MS / 1000}s — not minting beside it`);
|
||||
}
|
||||
await new Promise((r) => setTimeout(r, 50 + Math.floor(Math.random() * 50)));
|
||||
}
|
||||
try {
|
||||
return await work();
|
||||
} finally {
|
||||
try {
|
||||
if (readFileSync(lock, "utf8") === mine) unlinkSync(lock);
|
||||
} catch { /* already gone */ }
|
||||
}
|
||||
}
|
||||
|
||||
/** Whether the lock's holder is gone: its pid answers no signal, or it is older than LOCK_STALE_MS. */
|
||||
private leftBehind(lock: string): boolean {
|
||||
let text: string;
|
||||
let age: number;
|
||||
try {
|
||||
text = readFileSync(lock, "utf8");
|
||||
age = Date.now() - statSync(lock).mtimeMs;
|
||||
} catch {
|
||||
return false; // released meanwhile: the next attempt takes it
|
||||
}
|
||||
if (age > MintedToken.LOCK_STALE_MS) return true;
|
||||
const pid = Number(text.split(" ")[0]);
|
||||
if (!Number.isInteger(pid) || pid <= 0) return age > 1_000; // half-written, or not ours: give it a moment
|
||||
try {
|
||||
process.kill(pid, 0);
|
||||
return false;
|
||||
} catch (err) {
|
||||
return (err as NodeJS.ErrnoException).code === "ESRCH";
|
||||
}
|
||||
}
|
||||
|
||||
private read(): string | null {
|
||||
try {
|
||||
const token = readFileSync(this.opts.file, "utf8").replace(/\n$/, "");
|
||||
return token.length ? token : null;
|
||||
} catch (err) {
|
||||
if ((err as NodeJS.ErrnoException).code === "ENOENT") return null;
|
||||
throw new Error(`cannot read the kept Gitea token at ${this.opts.file}: ${(err as Error).message}`);
|
||||
}
|
||||
}
|
||||
|
||||
/** The scopes as kept beside the token: sorted, one line, and whose it is when it is not the admin's. */
|
||||
private scopesLine(): string {
|
||||
const scopes = [...this.scopes].sort().join(",");
|
||||
return this.account === this.opts.admin ? scopes : `${scopes} for ${this.account}`;
|
||||
}
|
||||
|
||||
/** The scopes the kept token was minted with, "" when not kept (a token kept before they were). */
|
||||
private keptScopes(): string {
|
||||
try {
|
||||
return readFileSync(`${this.opts.file}.scopes`, "utf8").trim();
|
||||
} catch {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
/** Write the token at 0600, whole or not at all: a temp file beside it, then a rename. Its scopes beside it. */
|
||||
private keep(token: string): void {
|
||||
mkdirSync(dirname(this.opts.file), { recursive: true, mode: 0o700 });
|
||||
const tmp = `${this.opts.file}.tmp`;
|
||||
writeFileSync(tmp, token + "\n", { mode: 0o600 });
|
||||
chmodSync(tmp, 0o600);
|
||||
renameSync(tmp, this.opts.file);
|
||||
writeFileSync(`${this.opts.file}.scopes.tmp`, this.scopesLine() + "\n", { mode: 0o600 });
|
||||
renameSync(`${this.opts.file}.scopes.tmp`, `${this.opts.file}.scopes`);
|
||||
}
|
||||
|
||||
/** One call to the forge's API with the admin login, the password read now. */
|
||||
/** The admin password, read now. */
|
||||
private password(): string {
|
||||
try {
|
||||
return readFileSync(this.opts.passwordFile, "utf8").replace(/\n$/, "");
|
||||
@@ -361,6 +447,7 @@ export class MintedToken implements TokenSource {
|
||||
}
|
||||
}
|
||||
|
||||
/** One call to the forge's API with the admin login. */
|
||||
private adminCall(password = this.password()): AdminCall {
|
||||
const authorization = "Basic " + Buffer.from(`${this.opts.admin}:${password}`).toString("base64");
|
||||
const api = `${this.opts.url.replace(/\/+$/, "")}/api/v1`;
|
||||
@@ -381,71 +468,81 @@ export class MintedToken implements TokenSource {
|
||||
}
|
||||
|
||||
/** Where a retirement of the admin's old token that failed is remembered, to be tried again (retireAdmin). */
|
||||
private get retireFile(): string {
|
||||
return `${this.opts.file}.retire-admin`;
|
||||
private get retireFile(): string | null {
|
||||
return this.opts.stateDir ? join(this.opts.stateDir, `${this.opts.account}.retire-admin`) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* The admin's own token by this name goes: what the tools used before must not outlive the move (novox/hq ADR
|
||||
* 0365 §2). Run after the account's token is minted and kept, so a failure here never leaves the tools without
|
||||
* a token: it is said, remembered beside the kept token, and tried again when a process next reads that token.
|
||||
* The admin's own token named `mesh-tools` goes: what the tools used before must not outlive the move (novox/hq ADR
|
||||
* 0365 §2). Run after the account's token is minted and kept, so a failure here never leaves the tools without a
|
||||
* token: it is said, remembered, and tried again when a process next reads the vault's token.
|
||||
*/
|
||||
private async retireAdmin(admin: AdminCall = this.adminCall()): Promise<void> {
|
||||
if (this.account === this.opts.admin) return;
|
||||
if (this.opts.oldName !== TOKEN_NAME) return;
|
||||
try {
|
||||
const retired = await this.dropNamed(admin, `/users/${encodeURIComponent(this.opts.admin)}/tokens`, this.opts.admin);
|
||||
if (retired > 0) this.log(`deleted ${retired} token(s) named "${this.name}" the admin account "${this.opts.admin}" held: the tools act as "${this.account}" now`);
|
||||
rmSync(this.retireFile, { force: true });
|
||||
const retired = await this.dropNamed(admin, `/users/${encodeURIComponent(this.opts.admin)}/tokens`, this.opts.admin, TOKEN_NAME);
|
||||
if (retired > 0) this.log(`deleted ${retired} token(s) named "${TOKEN_NAME}" the admin account "${this.opts.admin}" held: the tools act as "${this.opts.account}" now`);
|
||||
if (this.retireFile) rmSync(this.retireFile, { force: true });
|
||||
} catch (err) {
|
||||
writeFileSync(this.retireFile, `${new Date().toISOString()}\n`, { mode: 0o600 });
|
||||
this.log(`FAILED to delete the token(s) named "${this.name}" the admin account "${this.opts.admin}" holds — tried again at the next start: ${(err as Error).message}`);
|
||||
if (this.retireFile) {
|
||||
mkdirSync(this.opts.stateDir!, { recursive: true, mode: 0o700 });
|
||||
writeFileSync(this.retireFile, `${new Date().toISOString()}\n`, { mode: 0o600 });
|
||||
}
|
||||
this.log(`FAILED to delete the token(s) named "${TOKEN_NAME}" the admin account "${this.opts.admin}" holds — tried again at the next start: ${(err as Error).message}`);
|
||||
}
|
||||
}
|
||||
|
||||
private async doMint(): Promise<string> {
|
||||
/**
|
||||
* Mint `<account>-<generation>` (the generation this own-put makes), write it through own-put, and only then delete
|
||||
* at the forge, by id, the token it replaces: `replaced`'s by its name, or at the first mint the tokens under the
|
||||
* account's old fixed name (H-A-422 (2), H-A-430 (1)).
|
||||
*/
|
||||
private async doMint(replaced: VaultToken | null): Promise<string> {
|
||||
const password = this.password();
|
||||
const admin = this.adminCall(password);
|
||||
const tokensOf = (account: string) => `/users/${encodeURIComponent(account)}/tokens`;
|
||||
const call = (method: string, path = "", body?: unknown) => admin(method, tokensOf(this.account) + path, body);
|
||||
const account = this.opts.account;
|
||||
const tokens = `/users/${encodeURIComponent(account)}/tokens`;
|
||||
const generation = (replaced?.generation ?? 0) + 1;
|
||||
const name = mintName(account, generation);
|
||||
|
||||
if (this.account !== this.opts.admin) {
|
||||
// The tools' account and its team first (novox/hq ADR 0365 §2): a failure here fails the mint, tried again
|
||||
// at the next call.
|
||||
const me = await admin("GET", "/user");
|
||||
if (me.status === 401 || me.status === 403) throw new AdminRefused(this.opts.admin, me.status);
|
||||
await this.opts.setUp?.(password);
|
||||
}
|
||||
// The account and its team first (novox/hq ADR 0365 §2): a failure here fails the mint, tried again at the next call.
|
||||
const me = await admin("GET", "/user");
|
||||
if (me.status === 401 || me.status === 403) throw new AdminRefused(this.opts.admin, me.status);
|
||||
await this.opts.setUp?.(password);
|
||||
|
||||
let res = await call("POST", "", { name: this.name, scopes: this.scopes });
|
||||
let res = await admin("POST", tokens, { name, scopes: this.scopes });
|
||||
if (res.status === 401 || res.status === 403) throw new AdminRefused(this.opts.admin, res.status);
|
||||
if (res.status === 400 || res.status === 422) {
|
||||
// The forge still holds a token by this name whose value we no longer have — the kept file
|
||||
// went while the forge's data stayed, or two processes once minted at once and left two. It is
|
||||
// ours to replace: find every one by this name in the account's list and delete each by its id
|
||||
// (gitea refuses a delete by a name held twice: "multiple matches for token name"), then mint.
|
||||
await this.dropNamed(admin, tokensOf(this.account), this.account, true);
|
||||
res = await call("POST", "", { name: this.name, scopes: this.scopes });
|
||||
// A mint of this generation whose own-put never landed left its token at the forge, unused: deleted by its id
|
||||
// (gitea refuses a delete by a name held twice), then minted again.
|
||||
await this.dropNamed(admin, tokens, account, name, true);
|
||||
res = await admin("POST", tokens, { name, scopes: this.scopes });
|
||||
}
|
||||
if (res.status !== 201 && res.status !== 200) {
|
||||
throw new Error(`Gitea POST /users/${this.account}/tokens: ${res.status} ${detail(res.body)}`);
|
||||
throw new Error(`Gitea POST /users/${account}/tokens: ${res.status} ${detail(res.body)}`);
|
||||
}
|
||||
const token = typeof res.body?.sha1 === "string" ? res.body.sha1 : null;
|
||||
if (!token) throw new Error(`Gitea POST /users/${this.account}/tokens: ${res.status} but no token in the reply`);
|
||||
if (!token) throw new Error(`Gitea POST /users/${account}/tokens: ${res.status} but no token in the reply`);
|
||||
|
||||
this.keep(token);
|
||||
this.held = token;
|
||||
this.log(`minted a token for "${this.account}" (${this.scopes.join(", ")}), kept at ${this.opts.file}`);
|
||||
// Only now, with the account's own token kept, does the admin's old one go.
|
||||
const kept = await this.opts.vault.put(account, token, this.scopes);
|
||||
if (kept !== generation) this.log(`the vault kept ${vaultName(account)} at generation ${kept}, the forge names it ${name}`);
|
||||
this.held = { token, scopes: this.scopes, generation: kept, changedAt: this.now().toISOString() };
|
||||
this.log(`minted ${name} for "${account}" (${this.scopes.join(", ")}), kept in the vault as ${vaultName(account)}`);
|
||||
|
||||
// Only now, with the new token kept, does the one it replaces go.
|
||||
const gone = replaced ? mintName(account, replaced.generation) : this.opts.oldName;
|
||||
try {
|
||||
const dropped = await this.dropNamed(admin, tokens, account, gone);
|
||||
if (dropped > 0) this.log(`deleted ${dropped} token(s) named "${gone}" "${account}" held: ${name} replaces them`);
|
||||
} catch (err) {
|
||||
this.log(`FAILED to delete the token "${gone}" ${name} replaces; the forge holds both until the next mint — ${(err as Error).message}`);
|
||||
}
|
||||
await this.retireAdmin(admin);
|
||||
return token;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete every token by this name an account holds, each by its id (gitea refuses a delete by a name held twice:
|
||||
* "multiple matches for token name"); answers how many. `replacing` says the line for a mint that found its name
|
||||
* taken.
|
||||
*/
|
||||
private async dropNamed(admin: AdminCall, tokens: string, account: string, replacing = false): Promise<number> {
|
||||
/** Delete every token named `name` an account holds, each by its id; answers how many. */
|
||||
private async dropNamed(admin: AdminCall, tokens: string, account: string, name: string, replacing = false): Promise<number> {
|
||||
const held: number[] = [];
|
||||
for (let page = 1; page <= 100; page++) {
|
||||
const listed = await admin("GET", `${tokens}?page=${page}&limit=50`);
|
||||
@@ -453,23 +550,59 @@ export class MintedToken implements TokenSource {
|
||||
throw new Error(`Gitea GET /users/${account}/tokens: ${listed.status} ${detail(listed.body)}`);
|
||||
}
|
||||
if (listed.body.length === 0) break;
|
||||
for (const t of listed.body) if (t?.name === this.name && typeof t.id === "number") held.push(t.id);
|
||||
}
|
||||
if (replacing) {
|
||||
this.log(held.length === 1
|
||||
? `the forge already holds a token named "${this.name}" — replacing it`
|
||||
: `the forge holds ${held.length} tokens named "${this.name}" — deleting each by its id and minting one`);
|
||||
for (const t of listed.body) if (t?.name === name && typeof t.id === "number") held.push(t.id);
|
||||
}
|
||||
if (replacing) this.log(`the forge already holds ${held.length} token(s) named "${name}" — deleting each by its id and minting one`);
|
||||
for (const id of held) {
|
||||
const dropped = await admin("DELETE", `${tokens}/${id}`);
|
||||
if (dropped.status !== 204 && dropped.status !== 404) {
|
||||
throw new Error(`Gitea DELETE /users/${account}/tokens/${id} (named ${this.name}): ${dropped.status} ${detail(dropped.body)}`);
|
||||
throw new Error(`Gitea DELETE /users/${account}/tokens/${id} (named ${name}): ${dropped.status} ${detail(dropped.body)}`);
|
||||
}
|
||||
}
|
||||
return held.length;
|
||||
}
|
||||
}
|
||||
|
||||
/** The admin login and password file the manifest hands the runtime, or an error naming what is missing. */
|
||||
export function adminFromEnv(env: NodeJS.ProcessEnv, missingWords: string): { admin: string; passwordFile: string } {
|
||||
const admin = env.MESH_GITEA_ADMIN_USER;
|
||||
const passwordFile = env.MESH_GITEA_ADMIN_PASSWORD_FILE;
|
||||
const missing = [admin ? null : "MESH_GITEA_ADMIN_USER", passwordFile ? null : "MESH_GITEA_ADMIN_PASSWORD_FILE"]
|
||||
.filter((v): v is string => v !== null);
|
||||
if (missing.length) throw new Error(`${missingWords} ${missing.join(", ")}`);
|
||||
return { admin: admin!, passwordFile: passwordFile! };
|
||||
}
|
||||
|
||||
// One vault and one lease a process, shared by both accounts' sources: one key and one watch.
|
||||
let vaultOnce: TokenVault | undefined;
|
||||
let leaseOnce: MintLease | undefined;
|
||||
function sharedVault(): TokenVault {
|
||||
return (vaultOnce ??= vaultOverBus());
|
||||
}
|
||||
function sharedLease(): MintLease {
|
||||
return (leaseOnce ??= leaseOverState());
|
||||
}
|
||||
|
||||
/** The tool forge_token_lease's reading (§808): the lease's holder and since when, and each account's token age, read
|
||||
* from the vault's `changed-at`; never the token. */
|
||||
export async function tokenLease(vault: TokenVault, lease: MintLease, accounts: readonly string[], now: Date = new Date()) {
|
||||
const held = await lease.read();
|
||||
const out: { account: string; vault: string; generation?: number; "changed-at"?: string; "age-seconds"?: number; held: boolean }[] = [];
|
||||
for (const account of accounts) {
|
||||
const t = await vault.get(account);
|
||||
out.push(t === null ? { account, vault: vaultName(account), held: false } : {
|
||||
account, vault: vaultName(account), held: true, generation: t.generation, "changed-at": t.changedAt,
|
||||
"age-seconds": Math.max(0, Math.floor((now.getTime() - Date.parse(t.changedAt)) / 1000)),
|
||||
});
|
||||
}
|
||||
return { lease: held === null ? { held: false } : { held: true, holder: held.holder, since: held.since }, tokens: out };
|
||||
}
|
||||
|
||||
/** The vault and the lease this process mints through, for the tool forge_token_lease. */
|
||||
export function processVaultAndLease(): { vault: TokenVault; lease: MintLease } {
|
||||
return { vault: sharedVault(), lease: sharedLease() };
|
||||
}
|
||||
|
||||
function detail(body: unknown): string {
|
||||
return typeof body === "string" ? body : JSON.stringify(body);
|
||||
}
|
||||
|
||||
@@ -7,6 +7,8 @@ import { callerOf } from "@novox/mesh-sdk/callers";
|
||||
import type { HandAct } from "@novox/mesh-sdk/views";
|
||||
import type { Partials } from "../partials.js";
|
||||
import { sayCall, type Calls, type Say } from "../calls.js";
|
||||
import type { GiteaClient } from "../client.js";
|
||||
import { repositoryOf } from "../seat.js";
|
||||
|
||||
const word = (v: unknown): string => (typeof v === "string" ? v.trim() : "");
|
||||
|
||||
@@ -70,3 +72,55 @@ export function callTools(calls: Calls, partials: Partials, say: Say, acts: (act
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
/** The most a read tool answers (design 47 H2 §811). */
|
||||
export const ANSWER_CAP_BYTES = 768 * 1024;
|
||||
|
||||
/**
|
||||
* forge_merged (design 47 H2 §812, §811; H-A-402): a repository's merged pull requests by number, from `from` to `to`
|
||||
* inclusive, each read from the forge one by one (the forge lists none by number), with its merge commit, when it was
|
||||
* merged, and the merge call that made it when a kept call names it merged. A number the forge does not hold, or a pull
|
||||
* request not merged, is left out. At most 768 KB: past it `capped`, with `cursor` the next number to give as `from`.
|
||||
*/
|
||||
export function mergedTool(forge: Pick<GiteaClient, "getPullRequest">, calls: Pick<Calls, "all">): ToolDefinition {
|
||||
return {
|
||||
name: "forge_merged",
|
||||
description: "A repository's merged pull requests by number, from..to inclusive: each with its merge commit, when it was merged, and " +
|
||||
"the merge call that made it (none when it was merged outside a merge call). At most 768 KB: capped, with the cursor to give as from " +
|
||||
"(design 47 H2 §812).",
|
||||
input: { type: "object", required: ["repository", "from", "to"], properties: {
|
||||
repository: { type: "string", description: "the repository, <owner>/<name>" },
|
||||
from: { type: "integer", description: "the first pull request number" },
|
||||
to: { type: "integer", description: "the last pull request number" } } },
|
||||
run: async (args: Record<string, unknown>) => {
|
||||
const { owner, repo, full } = repositoryOf(args.repository);
|
||||
const from = Number(args.from), to = Number(args.to);
|
||||
if (!Number.isInteger(from) || from < 1 || !Number.isInteger(to) || to < from) {
|
||||
throw new Error("forge_merged names from and to, pull request numbers from 1, to not below from");
|
||||
}
|
||||
const madeBy = new Map<number, string>();
|
||||
for (const c of await calls.all()) {
|
||||
for (const m of c.members) if (m.repository === full && m.state === "merged") madeBy.set(m.number, c.call);
|
||||
}
|
||||
const merged: { number: number; "merge-commit": string; "merged-at"?: string; call?: string }[] = [];
|
||||
let size = 0;
|
||||
for (let n = from; n <= to; n++) {
|
||||
let p;
|
||||
try {
|
||||
p = await forge.getPullRequest(owner, repo, n);
|
||||
} catch (err) {
|
||||
if (/: 404 /.test(err instanceof Error ? err.message : "")) continue;
|
||||
throw err;
|
||||
}
|
||||
if (!p.merged || !p.merge_commit_sha) continue;
|
||||
const row = { number: n, "merge-commit": p.merge_commit_sha, ...(p.merged_at ? { "merged-at": p.merged_at } : {}),
|
||||
...(madeBy.has(n) ? { call: madeBy.get(n) } : {}) };
|
||||
const rowSize = JSON.stringify(row).length + 1;
|
||||
if (size + rowSize > ANSWER_CAP_BYTES) return { repository: full, from, to, total: to - from + 1, merged, capped: true, cursor: n };
|
||||
size += rowSize;
|
||||
merged.push(row);
|
||||
}
|
||||
return { repository: full, from, to, total: to - from + 1, merged };
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
// The tool forge_token_lease (design 47 H2 §808, the tools table): the mint lease's holder and since when, and each
|
||||
// account's token age, read from the vault's `changed-at`; never the token.
|
||||
|
||||
import type { ToolDefinition } from "@novox/mesh-sdk/tools";
|
||||
import { tokenLease, type MintLease, type TokenVault } from "../token.js";
|
||||
|
||||
export function tokenLeaseTool(vault: TokenVault, lease: MintLease, accounts: readonly string[], now: () => Date = () => new Date()): ToolDefinition {
|
||||
return {
|
||||
name: "forge_token_lease",
|
||||
description: "The forge token's mint lease and each account's token age (design 47 H2 §808): who holds the lease `mint` in the " +
|
||||
"state token-lease and since when, and for each account (" + accounts.join(", ") + ") the vault's generation of its token " +
|
||||
"and when it changed, never the token itself.",
|
||||
input: { type: "object", properties: {} },
|
||||
run: async () => tokenLease(vault, lease, accounts, now()),
|
||||
};
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
{
|
||||
"module": "gitlab",
|
||||
"version": "0.1",
|
||||
"class": "leaf",
|
||||
"own-secrets": {
|
||||
"token": "${dir:state}/token"
|
||||
},
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
{
|
||||
"module": "gnome-keyring",
|
||||
"version": "0.1",
|
||||
"class": "core",
|
||||
"moved": true,
|
||||
"capabilities": [
|
||||
"package-manager"
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user