Files
mesh-delivery/modules/mesh-delivery-sdk/graph/cycle.go
T
jschoubben 5bf6fd9b01 graph: the refused cycle is shared code, read by the indexer and the validate step
RefusedCycle, with its running-version break and its path search, moves from the indexer's
internal/index into mesh-delivery-sdk/graph, so the assigner's validate step reads the same rule
the indexer's take-in does; the indexer calls the shared function. The rule's table test moves
with it; the indexer keeps its test of the trunk catalogues against the shared rule.

Builds: design 47 H §745; lead answers H-A-34, H-A-104 (2)

Claude-Session: https://claude.ai/code/session_01JG73KHUnBze48rdua8u2sy
2026-11-20 04:00:13 +01:00

95 lines
3.4 KiB
Go

package graph
import (
"maps"
"slices"
"strings"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog"
"git.novox.be/novox/mesh-sdk/go/seatdef"
)
// RefusedCycle is a cycle of the entries' edges that the take-in and the validate step refuse (design 47 H-A-34, H-A-104) (design 47 G §49, A-326, A-333), as the artifacts it
// passes through with the first named again at its end; nil when there is none. A cycle of delivery edges (kinds 2, 4, 5, 6)
// alone is delivered as one step, so only a cycle holding a build edge (kind 1 or 3) is refused, and not one of package edges
// (kind 1) alone: the language's own build refuses a real import cycle (A-333). A build edge of kind 3 that goes out of an
// artifact of a module holding a build seat into an artifact that seat builds is where such a cycle is broken: the running
// version builds the new one (§270), so a cycle through it is allowed, as the cycle through a build module's own inputs is.
func RefusedCycle(entries map[string]meshcatalog.CatalogEntry) []meshcatalog.ArtifactRef {
var edges []meshcatalog.Edge
for _, m := range slices.Sorted(maps.Keys(entries)) {
for _, x := range entries[m].Edges {
if x.From != "" && x.To != "" && !brokenAtTheRunningVersion(x, entries) {
edges = append(edges, x)
}
}
}
next := map[meshcatalog.ArtifactRef][]meshcatalog.Edge{}
for _, x := range edges {
next[x.From] = append(next[x.From], x)
}
for _, x := range edges {
if x.Kind != 1 && x.Kind != 3 {
continue
}
if path := pathBack(next, x.To, x.From, x.Kind != 1); path != nil {
return append([]meshcatalog.ArtifactRef{x.From}, path...)
}
}
return nil
}
// brokenAtTheRunningVersion says whether x is a build edge of kind 3 out of an artifact of a module that holds a build seat
// (an acting rule of step build) into an artifact that seat builds (A-326).
func brokenAtTheRunningVersion(x meshcatalog.Edge, entries map[string]meshcatalog.CatalogEntry) bool {
if x.Kind != 3 {
return false
}
builder, to := entries[strings.SplitN(x.From, ":", 2)[0]], entries[strings.SplitN(x.To, ":", 2)[0]]
i := slices.IndexFunc(to.Artifacts, func(a meshcatalog.Artifact) bool { return a.Ref == x.To })
if i < 0 {
return false
}
art := to.Artifacts[i]
return slices.ContainsFunc(builder.Acts, func(r meshcatalog.ActingRule) bool {
if r.Step != seatdef.StepBuild {
return false
}
if r.On == seatdef.OnKindImage {
return art.Kind == "image"
}
return seatdef.ActingRule{On: r.On}.ActsOn(art.Language)
})
}
// pathBack is the shortest path of next from start to end, start and end included, that holds an edge of a kind other than 1
// unless other is true already (a cycle of package edges alone is none of §49's, A-333); nil when there is none.
func pathBack(next map[meshcatalog.ArtifactRef][]meshcatalog.Edge, start, end meshcatalog.ArtifactRef, other bool) []meshcatalog.ArtifactRef {
type at struct {
ref meshcatalog.ArtifactRef
other bool
}
from := at{start, other}
before := map[at]at{from: {}}
queue := []at{from}
for len(queue) > 0 {
cur := queue[0]
queue = queue[1:]
if cur.ref == end && cur.other {
var path []meshcatalog.ArtifactRef
for ; cur != (at{}); cur = before[cur] {
path = append([]meshcatalog.ArtifactRef{cur.ref}, path...)
}
return path
}
for _, x := range next[cur.ref] {
n := at{x.To, cur.other || x.Kind != 1}
if _, seen := before[n]; !seen {
before[n] = cur
queue = append(queue, n)
}
}
}
return nil
}