gate: the start comparison ends only when every head was compared (A-464e)

Builds: design 47 G-22 A-464e, A-464
This commit is contained in:
2026-11-17 09:03:29 +01:00
parent 9ade421e12
commit 377d42f5ea
3 changed files with 116 additions and 15 deletions
@@ -44,7 +44,7 @@ type FindingWriter interface {
// generated contract of the seat that defines it (A-17). It is started once the runtime serves (A-40).
func NewFindingWriter() (*views.HeldWriter[meshconditions.Finding, meshconditions.FindingCleared, meshconditions.FindingsStated, meshconditions.FindingsAskedEvent], error) {
return views.NewStdioHeldWriter(views.WriterConfig[meshconditions.Finding, meshconditions.FindingCleared, meshconditions.FindingsStated, meshconditions.FindingsAskedEvent]{
Asked: meshconditions.SubjectFindingsAsked, Module: Module, State: stdio.State("findings"), Kinds: []string{ApprovalCarriedOpen}})
Asked: meshconditions.SubjectFindingsAsked, Module: Module, State: stdio.State("findings"), Kinds: []string{ApprovalCarriedOpen, SourceUncompared}})
}
// approvalID is the id of a pull request's finding: its repository and number, each escaped as a key's name is.
@@ -8,13 +8,17 @@ package main
// not watch, is left: only a start or a registration change adds them (sourcestart.go).
import (
"context"
"encoding/json"
"errors"
"fmt"
"sort"
"strconv"
"strings"
"time"
"git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshhandactsclient"
"git.novox.be/novox/mesh-contracts/health-and-repair/go/seats/meshconditions"
seat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdelivery"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-forge-bridge/forgemsg"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/trunk"
@@ -112,38 +116,108 @@ func (h *Holder) ForgeMerged(body json.RawMessage) error {
return nil
}
// FollowAtStart compares each head of every source key with the forge's head of that branch and moves every one behind it, once
// at each start. A head the forge cannot read is said and left. It answers the heads moved as "<repository>@<branch>".
// SourceUncompared is the finding of a start comparison that left a head uncompared (A-464e).
const SourceUncompared = "source-uncompared"
// FollowAtStart compares each head of every source key with the forge's head of that branch and moves every one behind it, at
// each start. It ends only when every head was compared (A-464e): a head the forge could not be asked for (the seat git not
// answering yet, as when its bundle starts in the same tool runner) is compared again after a growing pause of at most a
// minute, and the finding source-uncompared names the repositories and the last error while any stands. It answers the heads
// moved as "<repository>@<branch>".
func (h *Holder) FollowAtStart() ([]string, error) {
if h.Source == nil || h.Forge == nil {
return nil, errors.New("this gate keeps no source or reads no forge")
}
var moved []string
var only map[string]bool
for wait := 2 * time.Second; ; wait = min(wait*2, time.Minute) {
got, left := h.followPass(only)
moved = append(moved, got...)
if len(left) == 0 {
h.clearUncompared()
h.logf("[mesh-delivery] the sources compared with the forge's heads at the start: moved %v", moved)
return moved, nil
}
only = map[string]bool{}
var names []string
var last error
for n, err := range left {
only[n] = true
names = append(names, n)
last = err
}
sort.Strings(names)
h.sayUncompared(names, last)
h.logf("[mesh-delivery] %d heads are not compared with the forge's yet (%s; last: %v); asking again in %s", len(names), strings.Join(names, ", "), last, wait)
startSleep(wait)
}
}
// followPass compares the heads named in only (every head when nil) once; it answers the heads moved and, by "<repository>@<branch>",
// each head it could not compare with why. A source it cannot list or read is one entry named for itself.
func (h *Holder) followPass(only map[string]bool) (moved []string, left map[string]error) {
left = map[string]error{}
branches, err := h.Source.Branches()
if err != nil {
return nil, err
left["the branches of the source"] = err
return nil, left
}
var moved []string
var errs []error
for _, branch := range branches {
held, _, err := h.Source.Read(branch)
if err != nil || held == nil {
errs = append(errs, err)
if err != nil {
left["source."+branch] = err
continue
}
if held == nil {
continue
}
for repository, head := range held.Heads {
name := repository + "@" + branch
if only != nil && !only[name] {
continue
}
ok, err := h.followForge(branch, repository, head)
if err != nil {
errs = append(errs, err)
h.logf("[mesh-delivery] source.%s: %s is not compared with the forge's head: %v", branch, repository, err)
left[name] = err
continue
}
if ok {
moved = append(moved, repository+"@"+branch)
moved = append(moved, name)
}
}
}
return moved, left
}
func (h *Holder) sayUncompared(names []string, last error) {
if h.Findings == nil {
return
}
at := h.Now().UTC()
err := h.Findings.Write(context.Background(), meshconditions.Finding{Scope: FindingScope, ID: SourceUncompared, Kind: SourceUncompared,
Subject: meshconditions.Subject{Scope: FindingScope, ID: SourceUncompared},
Severity: meshconditions.FindingSeverityWarning, Resolver: "operator",
Summary: fmt.Sprintf("the gate's source is not compared with the forge's head for %s; the last error: %v", strings.Join(names, ", "), last),
Headline: "The gate's source is not compared with the forge",
Explanation: "At its start the gate compares each head of its source with the forge's head of the branch. A head it could not compare is asked again after a growing pause, and this clears once every head is compared.",
Needs: "the seat git answering; gate_source_follow moves one repository's head by hand.",
Source: SourceUncompared, Observer: Module, At: at, Since: at})
if err != nil {
h.logf("[mesh-delivery] the finding %s is not written: %v", SourceUncompared, err)
}
}
func (h *Holder) clearUncompared() {
if h.Findings == nil {
return
}
for _, f := range h.Findings.Held() {
if f.Scope == FindingScope && f.ID == SourceUncompared && f.Kind == SourceUncompared {
if err := h.Findings.Clear(context.Background(), FindingScope, SourceUncompared, SourceUncompared, "every head is compared with the forge's"); err != nil {
h.logf("[mesh-delivery] the finding %s is not cleared: %v", SourceUncompared, err)
}
}
}
h.logf("[mesh-delivery] the sources compared with the forge's heads at the start: moved %v", moved)
return moved, errors.Join(errs...)
}
// followForge moves one repository's head in a branch's source to the forge's head of the branch when they differ.
@@ -1,12 +1,14 @@
package main
import (
"context"
"encoding/json"
"errors"
"strings"
"testing"
"time"
"git.novox.be/novox/mesh-contracts/health-and-repair/go/seats/meshconditions"
meshcatalog "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-forge-bridge/forgemsg"
stdio "git.novox.be/novox/mesh-sdk/go"
@@ -70,9 +72,24 @@ func TestTheStartAndTheToolMoveAHeadBehindTheForge(t *testing.T) {
SaySeat: func(_ string, _ any, id string) error { said = append(said, id); return nil }}, src, &said
}
h, src, said := mk()
fw := &fakeFindings{}
h.Findings = fw
forge := h.Forge.(*fakeForge)
var pauses []time.Duration
var heldWhileWaiting int
prevSleep := startSleep
startSleep = func(d time.Duration) {
pauses = append(pauses, d)
heldWhileWaiting = len(fw.held) // the finding stands while a head is uncompared
forge.heads["o/f2@main"] = now // the seat git answers by the second pass
}
defer func() { startSleep = prevSleep }()
moved, err := h.FollowAtStart()
if err == nil || len(moved) != 1 || moved[0] != "o/f1@main" || src.vals["main"].Heads["o/f1"] != now || src.vals["main"].Heads["o/f2"] != old || len(*said) != 1 {
t.Fatalf("start: moved %v err %v heads %v said %v: want only o/f1 moved, o/f2 left with an error said", moved, err, src.vals["main"].Heads, *said)
if err != nil || len(moved) != 2 || src.vals["main"].Heads["o/f1"] != now || src.vals["main"].Heads["o/f2"] != now || len(*said) != 2 {
t.Fatalf("start: moved %v err %v heads %v said %v: want o/f1 and, after the second pass, o/f2 moved", moved, err, src.vals["main"].Heads, *said)
}
if len(pauses) != 1 || heldWhileWaiting != 1 || len(fw.held) != 0 {
t.Fatalf("pauses %v, finding held while waiting %d and after %d: want one pause, the finding raised while a head stood uncompared and cleared after", pauses, heldWhileWaiting, len(fw.held))
}
h, src, said = mk()
acts := &handActsFake{refuse: errors.New("log down")}
@@ -164,3 +181,13 @@ func TestATakeInTakenAfterALaterMergeLeavesTheHead(t *testing.T) {
t.Fatalf("take-in of an older merge: %v, source %+v, revision %d, said %v: want the head left at the later merge", err, src.vals["main"], src.rev, said)
}
}
// fakeFindings is the gate's finding writer in memory.
type fakeFindings struct{ held []meshconditions.Finding }
func (f *fakeFindings) Write(_ context.Context, x meshconditions.Finding) error {
f.held = append(f.held, x)
return nil
}
func (f *fakeFindings) Clear(_ context.Context, _, _, _, _ string) error { f.held = nil; return nil }
func (f *fakeFindings) Held() []meshconditions.Finding { return f.held }