A1 tier-clock: tier-bound counts no time a mover waits for the operator's yes

Builds: design 47 H2 H-A-627 (1) (2) (3); row A1
This commit is contained in:
2026-11-25 17:06:39 +01:00
parent 3b1f03ce5b
commit 9d98b2352b
3 changed files with 136 additions and 9 deletions
@@ -360,7 +360,7 @@ func (e *pipelineEnds) branchOf(module, placed string) string {
// writtenTools is the tool assigner_written: every key of the state written, with its end.
func writtenTools(st store) []stdio.Tool {
return []stdio.Tool{{Name: writtenTool, Description: "the pipeline ends the assigner's wants follow: per module and watched branch, the newest version released whose pipeline passed, with its pipeline and rank; per pipeline whose wants are written tier by tier, its tier progress (tier, since, movers); with why, why each want is what it is (as assigner_why).",
return []stdio.Tool{{Name: writtenTool, Description: "the pipeline ends the assigner's wants follow: per module and watched branch, the newest version released whose pipeline passed, with its pipeline and rank; per pipeline whose wants are written tier by tier, its tier progress (tier, since, movers, and clock-from and paused-from: the time tier-bound counts from, and when a pause of the count by an ask of the operator began); with why, why each want is what it is (as assigner_why).",
Input: schema(map[string]string{"why": "true: why each want is what it is", "module": "with why: a module's name", "node": "with why: a node's name"}, nil, "why"),
Run: func(args map[string]any) (any, error) {
if args["why"] == true || args["why"] == "true" {
@@ -17,7 +17,9 @@ import (
"git.novox.be/novox/mesh-sdk/go/keys"
"git.novox.be/novox/mesh-sdk/go/views"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-assigner/asks"
"mesh-delivery-assigner/internal/assign"
"mesh-delivery-assigner/internal/buckets"
)
// One pipeline's wants are written tier by tier (design 47 H §751, H-A-10, H-A-56): on a passed pipeline-ended the assigner
@@ -55,6 +57,20 @@ type tierProgress struct {
Since time.Time `json:"since"`
Movers []tierMover `json:"movers"`
Source int64 `json:"source,omitempty"`
// ClockFrom is the time tier-bound counts from, in Since's form: Since when the progress starts, moved later by each
// pause; absent reads as Since (design 47 H2 H-A-627).
ClockFrom time.Time `json:"clock-from,omitzero"`
// PausedFrom is the time the standing pause began: a key of the state asks names a mover of the tier that is not running,
// and the count stands still. Absent while the bound counts (H-A-627).
PausedFrom time.Time `json:"paused-from,omitzero"`
}
// clockFrom is the time tier-bound counts from; Since in a progress kept before the field.
func (t tierProgress) clockFrom() time.Time {
if t.ClockFrom.IsZero() {
return t.Since
}
return t.ClockFrom
}
// tierKey is the key of pipeline's tier progress; pipeline is the runner's key `<branch>.<source>`, its branch escaped.
@@ -87,7 +103,8 @@ func (e *pipelineEnds) startTiers(pipeline string, source int64, movers []tierMo
for _, m := range movers {
first = min(first, m.Tier)
}
t = tierProgress{Tier: first, Since: e.clock().UTC(), Source: source}
at := e.clock().UTC()
t = tierProgress{Tier: first, Since: at, ClockFrom: at, Source: source}
}
moved := false
for i := range t.Movers {
@@ -169,6 +186,7 @@ func (p *passer) tiers(n assign.Nodes, placedOn func(module, branch string) []st
now := p.clock().UTC()
bound := p.tierBoundOf()
var next time.Time
standing := p.w.read(buckets.Asks())
for _, key := range ks {
if !isTierKey(key) {
continue
@@ -185,13 +203,38 @@ func (p *passer) tiers(n assign.Nodes, placedOn func(module, branch string) []st
break
}
notRunning := p.notRunning(n, placedOn, pipeline, branch, t, t.Tier)
late := !now.Before(t.Since.Add(bound))
// The count stands still while an ask of the operator names a mover of the tier that is not running (H-A-627): the first
// pass that finds one writes paused-from, the first that finds none moves clock-from later by the time it stood still.
clock := t.clockFrom()
paused := pausedBy(standing, t, notRunning)
if paused && t.PausedFrom.IsZero() && now.Before(clock.Add(bound)) {
t.PausedFrom = now
if now.Before(t.Since) {
t.PausedFrom = t.Since // never earlier than the progress's own start
}
if p.writeTier(key, t, rev, "paused-from") {
p.logf("pipeline %s: tier %d's count of tier-bound stands still: %s", pipeline, t.Tier, pauseNamed(standing, t, notRunning))
}
continue
}
if !paused && !t.PausedFrom.IsZero() {
held := now.Sub(t.PausedFrom)
t.ClockFrom, t.PausedFrom = clock.Add(held), time.Time{}
if p.writeTier(key, t, rev, "clock-from") {
p.logf("pipeline %s: tier %d's count of tier-bound stood still for %s and goes on; the tier is late at %s", pipeline, t.Tier, held.Round(time.Second), t.ClockFrom.Add(bound).Format(time.RFC3339))
}
continue
}
if !t.PausedFrom.IsZero() {
break // paused: no lateness, and no timer armed
}
late := !now.Before(clock.Add(bound))
if late {
p.tierLate(pipeline, branch, number, t, notRunning)
break
}
if len(notRunning) > 0 {
if d := t.Since.Add(bound); next.IsZero() || d.Before(next) {
if d := clock.Add(bound); next.IsZero() || d.Before(next) {
next = d
}
break
@@ -211,7 +254,7 @@ func (p *passer) tiers(n assign.Nodes, placedOn func(module, branch string) []st
p.sayPipelineWanted(pipeline, branch, number, t.Source, meshwants.PipelineWantedOutcomeRunning, nil)
break
}
value, err := json.Marshal(tierProgress{Tier: later, Since: now, Movers: t.Movers, Source: t.Source})
value, err := json.Marshal(tierProgress{Tier: later, Since: now, ClockFrom: now, Movers: t.Movers, Source: t.Source})
if err == nil {
err = p.ends.st.swap(key, value, rev)
}
@@ -366,3 +409,59 @@ func tierBoundSetting(getenv func(string) string) (time.Duration, error) {
// tierBoundUnit is the unit of the setting tier-bound, a minute; the whole test sets a shorter one, so a tier passes its bound
// without a test waiting minutes.
var tierBoundUnit = time.Minute
// writeTier swaps the tier progress at key by compare-and-set at rev; false when it was not written (the caller reads it again),
// the failure said but a lost race (what field the write was for names it).
func (p *passer) writeTier(key string, t tierProgress, rev uint64, field string) bool {
value, err := json.Marshal(t)
if err == nil {
err = p.ends.st.swap(key, value, rev)
}
if err != nil && !errors.Is(err, errSwapped) {
p.logf("the tier progress %s is not written (%s): %v", key, field, err)
}
return err == nil
}
// pausingAsks are the keys of asks (the assigner's state asks, G §717) that pause the count of tier t of its pipeline: a key of
// any kind but assignments that names a mover of the tier by its module, by the mover's fingerprint where the kind's key names
// one (every kind but record, whose key names the composition), and, where it names a machine (every kind but hold), one of
// the machines on which the mover is not running (H-A-627 (1)); sorted.
func pausingAsks(standing map[string][]byte, t tierProgress, notRunning []meshwants.NotRunning) []string {
var out []string
for key := range standing {
k, err := asks.ParseAnyAskKey(key)
if err != nil || k.Kind == asks.AskKindAssignments || k.Module == "" {
continue
}
for _, m := range t.Movers {
if m.Tier != t.Tier || m.Module != k.Module || (k.Kind != asks.AskKindRecord && m.Fp != k.FP) {
continue
}
if k.Kind == asks.AskKindHold || notRunningOn(notRunning, m.Module, k.Node) {
out = append(out, key)
}
}
}
sort.Strings(out)
return out
}
func notRunningOn(notRunning []meshwants.NotRunning, module, node string) bool {
return slices.ContainsFunc(notRunning, func(nr meshwants.NotRunning) bool { return nr.Module == module && slices.Contains(nr.Machines, node) })
}
// pausedBy says whether an ask of the operator pauses tier t's count (pausingAsks).
func pausedBy(standing map[string][]byte, t tierProgress, notRunning []meshwants.NotRunning) bool {
return len(pausingAsks(standing, t, notRunning)) > 0
}
// pauseNamed names the ask that pauses the count, for the journal: its key and the movers of the tier it names.
func pauseNamed(standing map[string][]byte, t tierProgress, notRunning []meshwants.NotRunning) string {
var named []string
for _, key := range pausingAsks(standing, t, notRunning) {
k, _ := asks.ParseAnyAskKey(key)
named = append(named, fmt.Sprintf("the ask %s about %s on %s", key, k.Module, k.Node))
}
return strings.Join(named, "; ")
}
@@ -18,6 +18,7 @@ import (
"git.novox.be/novox/mesh-sdk/go/catalogue"
"git.novox.be/novox/mesh-sdk/go/views"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-assigner/asks"
"mesh-delivery-assigner/internal/assign"
"mesh-delivery-assigner/internal/buckets"
)
@@ -32,7 +33,9 @@ func TestAPipelinesWantsAreWrittenTierByTier(t *testing.T) {
for _, c := range []struct {
runs bool
pipeline string
}{{true, trunkBranch + ".5"}, {false, trunkBranch + ".5"}, {true, trunkBranch + ".5.again.2"}, {false, trunkBranch + ".5.again.2"}} {
paused bool // H-A-627: the provider's record ask stands in asks from minute 5 to minute 45, and the count stands still
}{{true, trunkBranch + ".5", false}, {false, trunkBranch + ".5", false}, {true, trunkBranch + ".5.again.2", false}, {false, trunkBranch + ".5.again.2", false},
{true, trunkBranch + ".5", true}, {false, trunkBranch + ".5", true}} {
runs, pipeline := c.runs, c.pipeline
start := time.Date(2026, 11, 20, 4, 0, 0, 0, time.UTC)
now := start
@@ -83,12 +86,37 @@ func TestAPipelinesWantsAreWrittenTierByTier(t *testing.T) {
if !wanted("g14", "db") || !wanted("ace", "db") || wanted("g14", "web") || wanted("ace", "web") {
t.Fatalf("%s runs %v: tier 1 alone is not what is wanted first: %v", pipeline, runs, wantsKV.values)
}
runAt, lateBefore, lateAt := time.Minute, 29*time.Minute, 30*time.Minute
if c.paused {
runAt, lateBefore, lateAt = 50*time.Minute, 69*time.Minute, 70*time.Minute
akey, err := asks.RecordAskKey("g14", "db", "sha256:"+strings.Repeat("a", 64))
if err != nil {
t.Fatal(err)
}
now = start.Add(5 * time.Minute)
p.w.put(buckets.Asks(), akey, []byte(`{}`))
p.wants(context.Background())
now = start.Add(30 * time.Minute) // past tier-bound had the count not stood still
p.wants(context.Background())
if len(findings) != 0 || len(said) != 0 {
t.Fatalf("%s: late at minute 30 while its provider's ask stands: %v, %+v", pipeline, findings, said)
}
if prog, _, _ := p.ends.tiersOf(pipeline); prog.PausedFrom.IsZero() || !prog.PausedFrom.Equal(start.Add(5*time.Minute)) {
t.Fatalf("%s: the pause's start is not kept: %+v", pipeline, prog)
}
now = start.Add(45 * time.Minute)
p.w.put(buckets.Asks(), akey, nil)
p.wants(context.Background())
if prog, _, _ := p.ends.tiersOf(pipeline); !prog.PausedFrom.IsZero() || !prog.ClockFrom.Equal(start.Add(40*time.Minute)) {
t.Fatalf("%s: the pause's end does not move clock-from to minute 40: %+v", pipeline, prog)
}
}
if runs {
raw, _ := json.Marshal(nodeengine.Running{Node: "g14", Module: "db", Fingerprint: db})
if err := p.w.hearRunning("running.g14.db", raw); err != nil {
t.Fatal(err)
}
now = start.Add(time.Minute)
now = start.Add(runAt)
p.wants(context.Background())
if !wanted("g14", "web") || !wanted("ace", "web") {
t.Fatalf("%s: the consumer's want is not written once its provider runs: %v", pipeline, wantsKV.values)
@@ -104,12 +132,12 @@ func TestAPipelinesWantsAreWrittenTierByTier(t *testing.T) {
}
continue
}
now = start.Add(29 * time.Minute)
now = start.Add(lateBefore)
p.wants(context.Background())
if len(findings) != 0 || len(said) != 0 {
t.Fatalf("late before tier-bound: %v, %+v", findings, said)
}
now = start.Add(30 * time.Minute)
now = start.Add(lateAt)
p.wants(context.Background())
p.wants(context.Background())
f, ok := findings["delivery."+assign.KeyID(pipeline)+".tier-late"]