Files
mesh-catalog/modules/mesh-build-node/cmd/mesh-build-node/steps.go
T
jschoubben f4cd0e89fd mesh-build-node-narrowed-packages: narrow a step's workspace as H-A-136 and H-A-137 say
Review round 1. The node build module runs the seat generator's check at its unit level
(steprun.OutOfStep), so its narrowed workspace keeps what H-A-136 and H-A-137 name: every
generated-from directory of the module, and for a root module the seats/ directory and the
paths of every artifact naming generated-from. Narrowed bare, a step of one mesh-contracts
package named the generated files left out as out of step. The fetch and the steps verb now
narrow the same way, and the steps verb reads its narrowed tree as the levels do.

narrowTree and generatedFromDirs are copied from mesh-build-go without its Go part, not moved
into mesh-delivery-sdk's steprun: the move is a change to two more repositories and a group
delivery for some sixty lines, in a fix that unblocks the build steps now; the copy says so.
packageDir and narrowedAway share one path helper.

Builds: design 47 H H-A-136, H-A-137 (hq design/47-H1-answers-59), ADR 0499 clause 4
2026-11-20 16:10:08 +01:00

191 lines
7.2 KiB
Go

package main
// **The verb `steps`** (design 47 G §22, §307, §326, §328): which pipeline steps a TypeScript artifact can run, by the
// convention check.go tests by. The type-check is the lint step (§328) and is always one; the unit step is the packages
// holding a package.json script `test`, the integration step those holding `test:integration`. A level with none is not
// answered. The answer is kept in the state detected-steps by the artifact's fingerprint, replaced by a newer one and by
// again, as the Go build module keeps it.
import (
"context"
"fmt"
"os"
"path/filepath"
"sort"
"strings"
"time"
seat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/nodebuildnode"
sdkstep "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildstep"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/steprun"
stdio "git.novox.be/novox/mesh-sdk/go"
"git.novox.be/novox/mesh-sdk/go/forgekey"
)
// The pipeline steps `steps` answers (the contract's DetectedStep enum).
const (
StepLint = "lint"
StepUnit = "unit"
StepIntegration = "integration"
)
// DetectedStep is the contract's.
type DetectedStep = seat.DetectedStep
// StepsAnswer is what `steps` answers.
type StepsAnswer struct {
Artifact string `json:"artifact"`
Fingerprint string `json:"fingerprint,omitempty"`
Steps []DetectedStep `json:"steps"`
Kept bool `json:"kept,omitempty"`
// Dropped is the artifact whose key a drop deleted; Error why a drop deleted nothing, naming the hand-act log.
Dropped string `json:"dropped,omitempty"`
Error string `json:"error,omitempty"`
}
// detectSteps finds the steps of the package directories under dir; narrowedTree says the tree was narrowed to an
// artifact's paths, as testTargets reads it.
func detectSteps(dir string, narrowedTree bool) ([]DetectedStep, error) {
targets, err := testTargets(dir, ".", narrowedTree)
if err != nil {
return nil, err
}
unit, integration := []string{}, []string{}
for _, tg := range targets {
scripts, err := packageScripts(tg.dir)
if err != nil {
return nil, err
}
if scripts["test"] != "" {
unit = append(unit, tg.where)
}
if scripts["test:integration"] != "" {
integration = append(integration, tg.where)
}
}
steps := []DetectedStep{{Step: StepLint}}
for _, l := range []struct {
step string
pkgs []string
}{{StepUnit, unit}, {StepIntegration, integration}} {
if len(l.pkgs) > 0 {
sort.Strings(l.pkgs)
steps = append(steps, DetectedStep{Step: l.step, Packages: l.pkgs})
}
}
return steps, nil
}
// stepsTool is the seat's verb `steps`.
func (m *Module) stepsTool() stdio.Tool {
return stdio.Tool{Name: Seat + "." + seat.VerbSteps,
Description: "Which pipeline steps a TypeScript artifact can run: the type-check as lint, always; unit tests where a package has a test script; " +
"integration tests where one has a test:integration script. A level with no test is not answered.",
Input: map[string]any{"type": "object", "required": []string{"module", "artifact"}, "properties": map[string]any{
"module": map[string]any{"type": "string"},
"artifact": map[string]any{"type": "string", "description": "the artifact reference, <module>:<artifact>"},
"fingerprint": map[string]any{"type": "string"},
"fetch": map[string]any{"type": "object", "description": "what the fetch tool is asked: the repository, the full commit and the module's directory"},
"again": map[string]any{"type": "boolean", "description": "find them anew, not from the kept answer"},
"drop": map[string]any{"type": "boolean", "description": "delete the artifact's kept key instead of detecting; needs why (written in the hand-act log first)"},
"why": map[string]any{"type": "string", "description": "with drop, why: a non-empty text"},
}},
Run: m.Short(m.steps)}
}
func (m *Module) steps(ctx context.Context, args map[string]any) (any, error) {
str := func(k string) string { s, _ := args[k].(string); return s }
if str("artifact") == "" || str("module") == "" {
return nil, fmt.Errorf("steps: the module and the artifact are asked")
}
if drop, _ := args["drop"].(bool); drop {
return m.dropDetected(args, stdio.CallerOf(args))
}
fetch, _ := args["fetch"].(map[string]any)
req, err := sdkstep.ParseFetchRequest(fetch)
if err != nil {
return nil, err
}
artifact, fp := str("artifact"), str("fingerprint")
name := artifact
if _, n, ok := strings.Cut(artifact, ":"); ok {
name = n
}
key := seat.DetectedStepsByModuleArtifactKey(str("module"), name)
if again, _ := args["again"].(bool); !again && fp != "" && m.ReadDetected != nil {
if raw, err := m.ReadDetected(key); err == nil && len(raw) > 0 {
if kept, err := seat.DecodeDetectedStepsByModuleArtifactValue(raw); err == nil && kept.Fingerprint == fp {
return StepsAnswer{Artifact: artifact, Fingerprint: fp, Steps: kept.Steps, Kept: true}, nil
}
}
}
steps, err := m.stepsOfTree(ctx, req)
if err != nil {
return nil, err
}
if fp != "" && m.Detected != nil {
at := time.Now()
if m.Clock != nil {
at = m.Clock()
}
if _, err := m.Detected(key, seat.DetectedSteps{V: 1, Fingerprint: fp, Steps: steps, At: &at}); err != nil {
return nil, fmt.Errorf("steps: keeping the answer: %w", err)
}
}
return StepsAnswer{Artifact: artifact, Fingerprint: fp, Steps: steps}, nil
}
// dropDetected is `steps` with drop (§463, §480): the act is written in the hand-act log with the caller the bus proved,
// then the kept key is deleted; a log that refuses leaves the key.
func (m *Module) dropDetected(args map[string]any, caller string) (any, error) {
artifact, _ := args["artifact"].(string)
why, _ := args["why"].(string)
module, _ := args["module"].(string)
name := artifact
if _, n, ok := strings.Cut(artifact, ":"); ok {
name = n
}
key := seat.DetectedStepsByModuleArtifactKey(module, name)
failed, err := steprun.DropDetected(artifact, why, caller, key, m.RecordAct, m.ForgetDetected)
if err != nil {
return nil, err
}
if failed != "" {
return StepsAnswer{Artifact: artifact, Error: failed}, nil
}
return StepsAnswer{Artifact: artifact, Dropped: artifact}, nil
}
// stepsOfTree fetches the commit alone (nothing installed) into a workspace removed at the end.
func (m *Module) stepsOfTree(ctx context.Context, req sdkstep.FetchRequest) ([]DetectedStep, error) {
if err := os.MkdirAll(m.Work, 0o700); err != nil {
return nil, err
}
root, err := os.MkdirTemp(m.Work, "steps-")
if err != nil {
return nil, err
}
defer os.RemoveAll(root)
tree := filepath.Join(root, "tree")
if err := os.MkdirAll(tree, 0o700); err != nil {
return nil, err
}
gitRun := func(ctx context.Context, env []string, name string, a ...string) forgekey.Ran {
return m.Run(ctx, append(m.baseEnv(), env...), name, a...)
}
if err := steprun.FetchCommit(ctx, gitRun, tree, m.remote(req.Repository), req.Commit, req.Ref, m.FetchOptions...); err != nil {
return nil, err
}
if len(req.Paths) > 0 {
// Narrowed as the fetch of the step's workspace narrows (narrowTree), so the steps answered are those the levels run.
if err := narrowTree(tree, req); err != nil {
return nil, err
}
}
dir := tree
if req.Dir != "" && req.Dir != "." {
dir = filepath.Join(tree, filepath.FromSlash(req.Dir))
}
return detectSteps(dir, len(req.Paths) > 0)
}