187 lines
7.3 KiB
Go
187 lines
7.3 KiB
Go
package app
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"time"
|
|
|
|
"gitea.maximumdirect.net/eric/narratio/internal/artifacts"
|
|
"gitea.maximumdirect.net/eric/narratio/internal/config"
|
|
"gitea.maximumdirect.net/eric/narratio/internal/stage"
|
|
"go.maximumdirect.net/commandkit"
|
|
)
|
|
|
|
type sessionPlanReport struct {
|
|
Campaign string `json:"campaign"`
|
|
SessionID string `json:"session_id"`
|
|
Stages []plannedStage `json:"stages"`
|
|
Publication []plannedPublication `json:"publication_actions"`
|
|
}
|
|
type plannedStage struct {
|
|
Name string `json:"name"`
|
|
Action string `json:"action"`
|
|
Reason string `json:"reason"`
|
|
Analysis *plannedAnalysis `json:"analysis,omitempty"`
|
|
}
|
|
type plannedAnalysis struct {
|
|
Targets []string `json:"targets"`
|
|
Prerequisites []plannedArtifact `json:"prerequisites"`
|
|
Execute []plannedArtifact `json:"execute"`
|
|
Reuse []plannedArtifact `json:"reuse"`
|
|
}
|
|
type plannedArtifact struct {
|
|
Key string `json:"key"`
|
|
Family string `json:"family,omitempty"`
|
|
CharacterID string `json:"character_id,omitempty"`
|
|
Role string `json:"role"`
|
|
Reason string `json:"reason"`
|
|
Forced bool `json:"forced"`
|
|
}
|
|
type plannedPublication struct {
|
|
Source string `json:"source"`
|
|
Destination string `json:"destination"`
|
|
Required bool `json:"required"`
|
|
Selected bool `json:"selected"`
|
|
}
|
|
|
|
func analysisPlanReport(summary *stage.AnalyzeResumeSummary) *plannedAnalysis {
|
|
convert := func(items []stage.AnalyzeResumeArtifact) []plannedArtifact {
|
|
out := make([]plannedArtifact, 0, len(items))
|
|
for _, item := range items {
|
|
out = append(out, plannedArtifact{Key: item.Key, Family: item.Family, CharacterID: item.CharacterID, Role: item.Role, Reason: item.Reason, Forced: item.Forced})
|
|
}
|
|
return out
|
|
}
|
|
return &plannedAnalysis{Targets: append([]string(nil), summary.ExplicitTargets...), Prerequisites: convert(summary.PrerequisiteWork), Execute: convert(summary.ExecutionOrder), Reuse: convert(summary.ReusedCurrent)}
|
|
}
|
|
|
|
func sessionPlanCommand(deps commandDependencies) commandkit.Command {
|
|
spec := executionCommandSpec("plan", "Preview session work without executing it.", false)
|
|
spec.Run = func(invocation commandkit.Invocation, input *executionInput) commandkit.Outcome {
|
|
report, err := planSelectedPipeline(invocation.Context, input, deps)
|
|
if report != nil {
|
|
err = errors.Join(err, invocation.Reporter().Report(commandkit.Report{Value: report}))
|
|
}
|
|
return operationOutcome(err)
|
|
}
|
|
return commandkit.NewCommand(spec)
|
|
}
|
|
|
|
func planSelectedPipeline(ctx context.Context, input *executionInput, deps commandDependencies) (report *sessionPlanReport, err error) {
|
|
deps = normalizeCommandDependencies(deps)
|
|
deps.secretsBootstrap = &secretsBootstrap{}
|
|
acquired := newConfigurationAcquisition(input.Selection, deps)
|
|
acquired.previousSessionID = input.PreviousSession.Value
|
|
defer func() { err = errors.Join(err, acquired.Close()) }()
|
|
cfg, err := acquired.Domain(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if err := config.Validate(cfg); err != nil {
|
|
return nil, err
|
|
}
|
|
selected, err := normalizeArtifactSelection(cfg, input.selectedArtifacts)
|
|
if err != nil {
|
|
return nil, &inputError{message: err.Error(), cause: err}
|
|
}
|
|
effective, err := resolveEffectiveArtifacts(cfg, selected)
|
|
if err != nil {
|
|
return nil, &inputError{message: err.Error(), cause: err}
|
|
}
|
|
return buildSessionPlan(ctx, cfg, input, selected, effective)
|
|
}
|
|
|
|
func buildSessionPlan(ctx context.Context, cfg *config.Config, input *executionInput, selectedArtifacts []string, effective artifacts.EffectiveArtifactSet) (*sessionPlanReport, error) {
|
|
if err := ctx.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
m, err := loadManifestIfPresent(ctx, cfg)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("plan: %w", err)
|
|
}
|
|
if err := validateBoundedPrerequisites(input.plan, m); err != nil {
|
|
return nil, fmt.Errorf("plan: %w", err)
|
|
}
|
|
store := artifacts.NewLocalStore(cfg.Pipeline.Workspace.Root)
|
|
model, err := cloneManifestForPlan(m, cfg)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("plan: clone session state: %w", err)
|
|
}
|
|
|
|
stages := input.plan.Stages()
|
|
stageEnv := &stage.Env{
|
|
Config: cfg, SelectedArtifactKeys: append([]string(nil), selectedArtifacts...),
|
|
EffectiveArtifacts: effective, ArtifactStore: store, Force: input.Force.Value,
|
|
}
|
|
|
|
report := &sessionPlanReport{Campaign: config.CampaignID(cfg.Campaign), SessionID: cfg.Session.SessionID, Stages: []plannedStage{}, Publication: []plannedPublication{}}
|
|
for _, selectedStage := range stages {
|
|
if err := ctx.Err(); err != nil {
|
|
return report, err
|
|
}
|
|
semanticConfig, err := currentStageSemanticConfig(selectedStage, stageEnv)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("plan: fingerprint semantic configuration for stage %q: %w", selectedStage.Name(), err)
|
|
}
|
|
decision := decideStageExecution(recordedStageStatus(model, selectedStage.Name()), input.Force.Value, nil)
|
|
var validation *stage.ResumeValidation
|
|
if decision.action == stageActionSkip {
|
|
checked, validationErr := evaluateStageResume(ctx, selectedStage, stageEnv, model, semanticConfig)
|
|
if validationErr != nil {
|
|
return nil, fmt.Errorf("plan: validate resume for stage %q: %w", selectedStage.Name(), validationErr)
|
|
}
|
|
validation = checked
|
|
decision = decideStageExecution(recordedStageStatus(model, selectedStage.Name()), input.Force.Value, checked)
|
|
}
|
|
if _, err := applyPreExecutionDecision(model, selectedStage.Name(), decision, time.Now().UTC()); err != nil {
|
|
return nil, fmt.Errorf("plan: model resume invalidation for stage %q: %w", selectedStage.Name(), err)
|
|
}
|
|
if decision.action == stageActionRun && selectedStage.Name() == "analyze" {
|
|
if validator, ok := selectedStage.(stage.ResumeValidator); ok {
|
|
checked, validationErr := validator.ValidateResume(ctx, stageEnv, model)
|
|
if validationErr != nil {
|
|
return nil, fmt.Errorf("plan: validate resume for stage %q: %w", selectedStage.Name(), validationErr)
|
|
}
|
|
checked = checked.Normalized()
|
|
validation = &checked
|
|
}
|
|
}
|
|
reason := decision.selectedStaleReason
|
|
if reason == "" {
|
|
switch {
|
|
case input.Force.Value:
|
|
reason = "forced"
|
|
case decision.action == stageActionSkip:
|
|
reason = "recorded result is reusable"
|
|
default:
|
|
reason = "no reusable successful result"
|
|
}
|
|
}
|
|
entry := plannedStage{Name: selectedStage.Name(), Action: string(decision.action), Reason: reason}
|
|
if validation != nil && validation.Analyze != nil {
|
|
entry.Analysis = analysisPlanReport(validation.Analyze)
|
|
}
|
|
report.Stages = append(report.Stages, entry)
|
|
if decision.action == stageActionRun {
|
|
if err := modelPlannedStageRun(model, selectedStage, cfg, decision, semanticConfig); err != nil {
|
|
return nil, fmt.Errorf("plan: model stage %q: %w", selectedStage.Name(), err)
|
|
}
|
|
}
|
|
}
|
|
if input.plan.Contains("publish") && cfg.Pipeline.Publish != nil && (cfg.Pipeline.Publish.Enabled == nil || *cfg.Pipeline.Publish.Enabled) {
|
|
catalog, err := buildHelperArtifactCatalog(cfg, model)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
actions, err := stage.PlanPublicationOutputs(catalog, cfg.PublicationOutputs(), selectedArtifacts)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
for _, action := range actions {
|
|
report.Publication = append(report.Publication, plannedPublication{Source: action.Source, Destination: action.Destination, Required: action.Required, Selected: action.Selected})
|
|
}
|
|
}
|
|
return report, ctx.Err()
|
|
}
|