93 lines
3.0 KiB
Go
93 lines
3.0 KiB
Go
package app
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"time"
|
|
|
|
"gitea.maximumdirect.net/eric/narratio/internal/artifactmodel"
|
|
"gitea.maximumdirect.net/eric/narratio/internal/artifacts"
|
|
"gitea.maximumdirect.net/eric/narratio/internal/config"
|
|
"gitea.maximumdirect.net/eric/narratio/internal/manifest"
|
|
)
|
|
|
|
// installInventoryBaseline accepts only products whose installed bytes validate.
|
|
// The caller holds the workspace lock and retains the recovery marker until
|
|
// this final manifest save succeeds.
|
|
func installInventoryBaseline(ctx context.Context, cfg *config.Config, plan *RestorePlan, store manifest.Store) error {
|
|
if plan == nil || plan.candidate == nil || plan.baseline == nil || store == nil {
|
|
return fmt.Errorf("planned local baseline is required")
|
|
}
|
|
paths := artifacts.NewLocalStore(cfg.Pipeline.Workspace.Root).SessionPathsFor(cfg.Session.Campaign, cfg.Session.SessionID)
|
|
for _, action := range plan.Actions {
|
|
if action.Kind == RestoreActionConflict {
|
|
return fmt.Errorf("conflicting product cannot be accepted")
|
|
}
|
|
if action.SourceID == "" {
|
|
continue
|
|
}
|
|
record := manifest.SuppliedProduct{Path: action.LocalRelativePath, Checksum: action.SHA256, Size: action.Size}
|
|
if err := artifacts.ValidateSuppliedProduct(paths, action.SourceID, action.LocalRelativePath, record); err != nil {
|
|
return fmt.Errorf("validate imported product %q: %w", action.SourceID, err)
|
|
}
|
|
if priorSourceChecksum(plan.baseline, action.SourceID) == action.SHA256 {
|
|
continue
|
|
}
|
|
producer := importedProductProducer(action.SourceID)
|
|
if producer == "" {
|
|
return fmt.Errorf("unknown imported product source %q", action.SourceID)
|
|
}
|
|
at := time.Now().UTC()
|
|
if existing := plan.candidate.Stages[producer]; existing != nil && existing.Status == manifest.StatusSucceeded {
|
|
plan.candidate.MarkStageStale(producer, at, staleReasonChangedResult)
|
|
}
|
|
if _, err := invalidateDependentSucceededStagesWithReason(plan.candidate, producer, at, staleReasonChangedResult); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return store.Save(ctx, paths.ManifestPath, plan.candidate)
|
|
}
|
|
|
|
func importedProductProducer(source string) string {
|
|
if spec, ok := artifactmodel.LookupRuntimeTranscriptArtifact(source); ok {
|
|
return spec.ProducerStage
|
|
}
|
|
if source == artifacts.ArtifactBoundsSession {
|
|
return "trim"
|
|
}
|
|
if artifacts.IsExtractionArtifactSource(source) {
|
|
return "extract"
|
|
}
|
|
if artifacts.IsConfiguredArtifactSource(source) {
|
|
return "analyze"
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func priorSourceChecksum(m *manifest.Manifest, source string) string {
|
|
if m == nil {
|
|
return ""
|
|
}
|
|
if m.SuppliedProducts != nil {
|
|
if product, ok := m.SuppliedProducts.Products[source]; ok {
|
|
return product.Checksum
|
|
}
|
|
}
|
|
for _, record := range m.Stages {
|
|
if record == nil {
|
|
continue
|
|
}
|
|
for _, output := range record.Outputs {
|
|
if output.SourceID == source {
|
|
return output.Checksum
|
|
}
|
|
}
|
|
for _, artifact := range record.AnalyzeArtifacts {
|
|
if artifact.Output != nil && artifact.Output.SourceID == source {
|
|
return artifact.Output.Checksum
|
|
}
|
|
}
|
|
}
|
|
return ""
|
|
}
|