Files
mesh-delivery/modules/mesh-delivery-sdk/steprun/guard.go
T
jschoubben 109a3e1e49 A-471: buildrun and refusal texts name the language version
buildrun tests expect the agreed toolchain as ToolchainAgreement gives it; the guard's
refusal and a decline name the language version alone.

Builds: design 47 G A-471 (G-22-lead-answers-2.md)
2026-11-17 15:53:05 +01:00

53 lines
2.3 KiB
Go

package steprun
import (
"context"
"encoding/json"
"fmt"
sdkstep "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildstep"
)
// Guard is a build module's check that the toolchain installed on its node is the one its recipe builds with
// (novox/hq ADR 0469 §1; issue 1473). The same guard serves ADR 0470 §4's postgres major for the integration
// level, with its own Probe and Want.
type Guard struct {
// Language is the toolchain's key part: go or typescript (ToolchainKey).
Language string
// Want is the toolchain version the recipe builds with, and Recipe the recipe's own version. The indexer
// compares both texts as they are written here.
Want, Recipe string
// Probe reads the installed version (go version, node --version).
Probe func(ctx context.Context) (installed string, err error)
Node string
// Put is the toolchains state's Put (stdio.State(the seat's toolchains state).Put).
Put Put
}
// Check probes the installed version, writes it to <node>.<language> of the toolchains state every time it runs,
// and answers nil when it is the recipe's. Otherwise it answers a *Refusal naming both versions; the installed
// version is written even then, so the indexer sees that builders disagree. A probe that fails is a *Refusal
// naming its failure, and nothing is written. A write that fails is an error that is not a Refusal.
func (g Guard) Check(ctx context.Context) error {
installed, err := g.Probe(ctx)
if err != nil {
return &Refusal{Text: fmt.Sprintf("the installed %s version could not be read: %v", g.Language, err), Want: g.Want}
}
key, err := sdkstep.ToolchainKey(g.Node, g.Language)
if err != nil {
return err
}
raw, err := sdkstep.EncodeToolchain(sdkstep.Toolchain{Language: g.Language, Version: sdkstep.LanguageVersion(installed), Recipe: g.Recipe, At: now().UTC()})
if err != nil {
return err
}
if _, err := g.Put(key, json.RawMessage(raw)); err != nil {
return fmt.Errorf("steprun: writing the toolchain %s: %w", key, err)
}
if !sdkstep.SameToolchainVersion(installed, g.Want) {
return &Refusal{Text: fmt.Sprintf("%s %s installed, the recipe builds with %s %s", g.Language, sdkstep.LanguageVersion(installed), g.Language, sdkstep.LanguageVersion(g.Want)),
Want: sdkstep.LanguageVersion(g.Want), Have: sdkstep.LanguageVersion(installed)}
}
return nil
}