113 lines
4.0 KiB
Go
113 lines
4.0 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 a supplied transcript replaces the normal preparation prefix.
|
|
func prepareBoundedReferences(runtime *invocationRuntime) error {
|
|
if runtime.plan.Contains("prepare") || !suppliedCompletion(runtime.cfg, runtime.manifest)["transcribe"] {
|
|
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
|
|
current, err := referencePreparationCurrent(runtime.ctx, runtime.cfg, runtime.manifest, runtime.artifactStore, runtime.effectiveArtifacts)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if current {
|
|
return nil
|
|
}
|
|
requirements, err := artifacts.CollectPreviousArtifactRequirements(configuredScriptoriumArtifacts(runtime.cfg), runtime.effectiveArtifacts)
|
|
if err != nil {
|
|
return fmt.Errorf("reference preparation requirements: %w", err)
|
|
}
|
|
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...)
|
|
result, err := stage.PrepareReferences(runtime.ctx, runtime.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) (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}
|
|
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 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
|
|
}
|