146 lines
5.4 KiB
Go
146 lines
5.4 KiB
Go
package app
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"reflect"
|
|
"strings"
|
|
|
|
"gitea.maximumdirect.net/eric/narratio/internal/artifacts"
|
|
"gitea.maximumdirect.net/eric/narratio/internal/config"
|
|
"gitea.maximumdirect.net/eric/narratio/internal/manifest"
|
|
"gitea.maximumdirect.net/eric/narratio/internal/stage"
|
|
)
|
|
|
|
// prepareBoundedReferences makes current references available before downstream
|
|
// checks when supplied products replace the normal preparation prefix.
|
|
func prepareBoundedReferences(runtime *invocationRuntime) error {
|
|
if runtime.plan.Contains("prepare") || !hasSuppliedBaseline(runtime.manifest) {
|
|
return nil
|
|
}
|
|
if !stagesContainAny(runtime.stages, "polish", "trim", "extract", "analyze") {
|
|
return nil
|
|
}
|
|
projection, err := projectSelectedWork(runtime.ctx, runtime.cfg, runtime.plan, runtime.opts.Force, runtime.opts.SelectedArtifacts, runtime.effectiveArtifacts, runtime.artifactStore, runtime.env.ObjectStore, runtime.manifest)
|
|
if err != nil {
|
|
return fmt.Errorf("project reference preparation: %w", err)
|
|
}
|
|
if !projectionNeedsReferences(projection) {
|
|
return nil
|
|
}
|
|
prepare, err := stage.Select("prepare")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
env := *runtime.env
|
|
env.Force = false
|
|
env.ReferenceOnly = true
|
|
requirements, err := referenceHistoryRequirements(runtime.cfg, runtime.effectiveArtifacts, projection)
|
|
if err != nil {
|
|
return fmt.Errorf("reference preparation requirements: %w", err)
|
|
}
|
|
env.PreparePreviousRequirements = requirements
|
|
current, err := referencePreparationCurrent(runtime.ctx, runtime.cfg, runtime.manifest, runtime.artifactStore, runtime.effectiveArtifacts, requirements)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if current {
|
|
return nil
|
|
}
|
|
if len(requirements) > 0 && strings.TrimSpace(runtime.cfg.Session.PreviousSessionID) != "" && runtime.env.ObjectStore == nil {
|
|
runtime.env.ObjectStore, err = runtime.dependencies.newObjectStore(runtime.ctx, runtime.cfg)
|
|
if err != nil {
|
|
return fmt.Errorf("initialize previous-session storage: %w", err)
|
|
}
|
|
}
|
|
priorInputs := append([]manifest.InputRecord(nil), runtime.manifest.Inputs...)
|
|
// Preserve any lazily composed history store on the reference environment.
|
|
env.ObjectStore = runtime.env.ObjectStore
|
|
result, err := stage.PrepareReferences(runtime.ctx, &env, runtime.manifest)
|
|
if err != nil {
|
|
return fmt.Errorf("prepare references: %w", err)
|
|
}
|
|
semantic, err := currentStageSemanticConfig(prepare, &env)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
at := runtime.dependencies.now()
|
|
runtime.manifest.MarkStageSucceeded("prepare", at, nil)
|
|
applyStageResultToManifest(runtime.manifest, "prepare", result)
|
|
setSessionStageSemanticConfig(runtime.manifest, "prepare", semantic)
|
|
if !reflect.DeepEqual(priorInputs, runtime.manifest.Inputs) {
|
|
if _, err := invalidateDependentSucceededStagesWithReason(runtime.manifest, "prepare", at, staleReasonChangedResult); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
if err := runtime.manifestStore.Save(runtime.ctx, runtime.manifestPath, runtime.manifest); err != nil {
|
|
return fmt.Errorf("save prepared references: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func referencePreparationCurrent(ctx context.Context, cfg *config.Config, m *manifest.Manifest, store artifacts.Store, effective artifacts.EffectiveArtifactSet, requirements []artifacts.PreviousArtifactRequirement) (bool, error) {
|
|
if recordedStageStatus(m, "prepare") != manifest.StatusSucceeded {
|
|
return false, nil
|
|
}
|
|
prepare, err := stage.Select("prepare")
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
env := &stage.Env{Config: cfg, ArtifactStore: store, EffectiveArtifacts: effective, ReferenceOnly: true, PreparePreviousRequirements: requirements}
|
|
semantic, err := currentStageSemanticConfig(prepare, env)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
validation, err := evaluateStageResume(ctx, prepare, env, m, semantic)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
return validation != nil && validation.Resumable, nil
|
|
}
|
|
|
|
func referenceHistoryRequirements(cfg *config.Config, effective artifacts.EffectiveArtifactSet, projection *workProjection) ([]artifacts.PreviousArtifactRequirement, error) {
|
|
var keys []string
|
|
for _, item := range projection.Stages {
|
|
if item.Name != "analyze" || item.Decision.action != stageActionRun {
|
|
continue
|
|
}
|
|
if item.Analysis == nil {
|
|
return artifacts.CollectPreviousArtifactRequirements(configuredScriptoriumArtifacts(cfg), effective)
|
|
}
|
|
for _, work := range item.Analysis.ExecutionOrder {
|
|
keys = append(keys, work.Key)
|
|
}
|
|
// Local checkpoints still rely on their ordinary input evidence. Keep
|
|
// their history available across reference preparation and revalidation.
|
|
for _, reused := range item.Analysis.ReusedCurrent {
|
|
if reused.Reason != "supplied" {
|
|
keys = append(keys, reused.Key)
|
|
}
|
|
}
|
|
}
|
|
requirements, err := artifacts.PreviousArtifactRequirementsForKeys(configuredScriptoriumArtifacts(cfg), keys)
|
|
// An explicit empty set distinguishes no planned history from the ordinary
|
|
// preparation default, which collects the full structural target closure.
|
|
if requirements == nil {
|
|
requirements = []artifacts.PreviousArtifactRequirement{}
|
|
}
|
|
return requirements, err
|
|
}
|
|
|
|
func projectionNeedsReferences(projection *workProjection) bool {
|
|
needsReferences := false
|
|
for _, item := range projection.Stages {
|
|
if item.Decision.action != stageActionRun {
|
|
continue
|
|
}
|
|
switch item.Name {
|
|
case "polish", "trim", "extract":
|
|
needsReferences = true
|
|
case "analyze":
|
|
needsReferences = needsReferences || item.Analysis == nil || len(item.Analysis.ExecutionOrder) > 0
|
|
}
|
|
}
|
|
return needsReferences
|
|
}
|