Merge imported products into local session state
This commit is contained in:
@@ -3,211 +3,90 @@ package app
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"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"
|
||||
)
|
||||
|
||||
func prepareRestoredManifest(
|
||||
ctx context.Context,
|
||||
cfg *config.Config,
|
||||
current *RemoteCurrentState,
|
||||
source io.Reader,
|
||||
destinationRoot string,
|
||||
requireCurrentIdentity bool,
|
||||
) (*manifest.Manifest, error) {
|
||||
manifestStore := &manifest.LocalStore{}
|
||||
m, err := manifestStore.LoadReader(ctx, source)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("validate manifest decode: %w", err)
|
||||
// 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")
|
||||
}
|
||||
if requireCurrentIdentity {
|
||||
if err := validateRestoredManifestIdentity(cfg, current, m); err != nil {
|
||||
return nil, err
|
||||
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 err := rebaseRestoredManifestPaths(cfg, m, destinationRoot); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
func validateRestoredManifestIdentity(cfg *config.Config, current *RemoteCurrentState, m *manifest.Manifest) error {
|
||||
if cfg == nil || cfg.Session == nil {
|
||||
return fmt.Errorf("resolved session config is required")
|
||||
}
|
||||
requestedSession := strings.TrimSpace(cfg.Session.SessionID)
|
||||
requestedCampaign := strings.TrimSpace(cfg.Session.Campaign)
|
||||
manifestSession := strings.TrimSpace(m.SessionID)
|
||||
manifestCampaign := strings.TrimSpace(m.Campaign)
|
||||
if manifestSession != requestedSession {
|
||||
return fmt.Errorf("manifest session_id %q does not match requested session_id %q", manifestSession, requestedSession)
|
||||
}
|
||||
if manifestCampaign == "" {
|
||||
return fmt.Errorf("manifest campaign is required")
|
||||
}
|
||||
if manifestCampaign != requestedCampaign {
|
||||
return fmt.Errorf("manifest campaign %q does not match requested campaign %q", manifestCampaign, requestedCampaign)
|
||||
}
|
||||
if current != nil {
|
||||
if expected := strings.TrimSpace(current.SessionID); expected != "" && manifestSession != expected {
|
||||
return fmt.Errorf("manifest session_id %q does not match discovered session_id %q", manifestSession, expected)
|
||||
if action.SourceID == "" {
|
||||
continue
|
||||
}
|
||||
if expected := strings.TrimSpace(current.Campaign); expected != "" && manifestCampaign != expected {
|
||||
return fmt.Errorf("manifest campaign %q does not match discovered campaign %q", manifestCampaign, expected)
|
||||
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 expected := strings.TrimSpace(current.RunID); expected != "" && strings.TrimSpace(m.RunID) != expected {
|
||||
return fmt.Errorf("manifest run_id %q does not match discovered run_id %q", strings.TrimSpace(m.RunID), expected)
|
||||
if priorSourceChecksum(plan.baseline, action.SourceID) == action.SHA256 {
|
||||
continue
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func rebaseRestoredManifestPaths(cfg *config.Config, m *manifest.Manifest, destinationRoot string) error {
|
||||
if cfg == nil || cfg.Pipeline == nil || m == nil {
|
||||
return fmt.Errorf("resolved config and manifest are required")
|
||||
}
|
||||
destinationRoot = filepath.Clean(strings.TrimSpace(destinationRoot))
|
||||
if destinationRoot == "" || destinationRoot == "." {
|
||||
return fmt.Errorf("restored manifest destination root is required")
|
||||
}
|
||||
producerRoot, hasProducerRoot := restoredManifestSessionRoot(m)
|
||||
rebase := func(field, value string) (string, error) {
|
||||
return rebaseRestoredLocalReference(field, value, producerRoot, hasProducerRoot, destinationRoot)
|
||||
}
|
||||
|
||||
for i := range m.Inputs {
|
||||
value, err := rebase("inputs.path", m.Inputs[i].Path)
|
||||
if err != nil {
|
||||
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
|
||||
}
|
||||
m.Inputs[i].Path = value
|
||||
}
|
||||
for i := range m.Artifacts {
|
||||
value, err := rebase("artifacts.local_path", m.Artifacts[i].LocalPath)
|
||||
if 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
|
||||
}
|
||||
m.Artifacts[i].LocalPath = value
|
||||
}
|
||||
for stageName, record := range m.Stages {
|
||||
for _, record := range m.Stages {
|
||||
if record == nil {
|
||||
continue
|
||||
}
|
||||
for i := range record.Outputs {
|
||||
value, err := rebase("stages."+stageName+".outputs.local_path", record.Outputs[i].LocalPath)
|
||||
if err != nil {
|
||||
return err
|
||||
for _, output := range record.Outputs {
|
||||
if output.SourceID == source {
|
||||
return output.Checksum
|
||||
}
|
||||
record.Outputs[i].LocalPath = value
|
||||
}
|
||||
// Logs and generated configuration files are invocation-local diagnostics,
|
||||
// not restored artifacts. Dropping them prevents a producer-machine path
|
||||
// from becoming a usable local reference.
|
||||
record.Logs = nil
|
||||
record.GeneratedConfigs = nil
|
||||
for key, artifact := range record.AnalyzeArtifacts {
|
||||
artifact.Logs = nil
|
||||
artifact.GeneratedConfigs = nil
|
||||
record.AnalyzeArtifacts[key] = artifact
|
||||
for _, artifact := range record.AnalyzeArtifacts {
|
||||
if artifact.Output != nil && artifact.Output.SourceID == source {
|
||||
return artifact.Output.Checksum
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if runID := strings.TrimSpace(m.RunID); runID != "" {
|
||||
m.LocalWorkDir = filepath.Join(destinationRoot, config.PathRunsDirSegment, runID)
|
||||
} else {
|
||||
m.LocalWorkDir = destinationRoot
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func restoredManifestSessionRoot(m *manifest.Manifest) (string, bool) {
|
||||
if m == nil {
|
||||
return "", false
|
||||
}
|
||||
runRoot := portableCleanPath(m.LocalWorkDir)
|
||||
runID := strings.TrimSpace(m.RunID)
|
||||
if !portableAbsolutePath(runRoot) || runID == "" || path.Base(runRoot) != runID {
|
||||
return "", false
|
||||
}
|
||||
runsDir := path.Dir(runRoot)
|
||||
if path.Base(runsDir) != config.PathRunsDirSegment {
|
||||
return "", false
|
||||
}
|
||||
return path.Dir(runsDir), true
|
||||
}
|
||||
|
||||
func rebaseRestoredLocalReference(field, value, producerRoot string, hasProducerRoot bool, destinationRoot string) (string, error) {
|
||||
value = strings.TrimSpace(value)
|
||||
if value == "" {
|
||||
return "", nil
|
||||
}
|
||||
if portableDrivePath(value) && !portableAbsolutePath(value) {
|
||||
return "", fmt.Errorf("%s has an unsafe relative path %q", field, value)
|
||||
}
|
||||
if portableAbsolutePath(value) {
|
||||
if !hasProducerRoot {
|
||||
return "", fmt.Errorf("%s has an absolute path without a producer session root", field)
|
||||
}
|
||||
relative, ok := portableRelativeWithinRoot(producerRoot, value)
|
||||
if !ok || relative == "" {
|
||||
return "", fmt.Errorf("%s absolute path is outside the producer session root", field)
|
||||
}
|
||||
resolved, err := joinWithinSessionRoot(destinationRoot, relative)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("%s: %w", field, err)
|
||||
}
|
||||
return resolved, nil
|
||||
}
|
||||
resolved, err := joinWithinSessionRoot(destinationRoot, strings.ReplaceAll(value, "\\", "/"))
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("%s has an unsafe relative path: %w", field, err)
|
||||
}
|
||||
return resolved, nil
|
||||
}
|
||||
|
||||
func portableRelativeWithinRoot(root, candidate string) (string, bool) {
|
||||
root = strings.TrimSuffix(portableCleanPath(root), "/")
|
||||
candidate = portableCleanPath(candidate)
|
||||
if root == "" || candidate == "" {
|
||||
return "", false
|
||||
}
|
||||
compareRoot, compareCandidate := root, candidate
|
||||
if portableDrivePath(root) || portableDrivePath(candidate) || strings.HasPrefix(root, "//") || strings.HasPrefix(candidate, "//") {
|
||||
compareRoot = strings.ToLower(compareRoot)
|
||||
compareCandidate = strings.ToLower(compareCandidate)
|
||||
}
|
||||
if compareCandidate == compareRoot {
|
||||
return "", true
|
||||
}
|
||||
if !strings.HasPrefix(compareCandidate, compareRoot+"/") {
|
||||
return "", false
|
||||
}
|
||||
return strings.TrimPrefix(candidate, root+"/"), true
|
||||
}
|
||||
|
||||
func portableCleanPath(value string) string {
|
||||
value = strings.ReplaceAll(strings.TrimSpace(value), "\\", "/")
|
||||
if value == "" {
|
||||
return ""
|
||||
}
|
||||
return path.Clean(value)
|
||||
}
|
||||
|
||||
func portableAbsolutePath(value string) bool {
|
||||
value = strings.TrimSpace(value)
|
||||
return strings.HasPrefix(value, "/") || strings.HasPrefix(value, "\\") || (len(value) >= 3 && isASCIIAlpha(value[0]) && value[1] == ':' && (value[2] == '/' || value[2] == '\\'))
|
||||
}
|
||||
|
||||
func portableDrivePath(value string) bool {
|
||||
value = strings.TrimSpace(value)
|
||||
return len(value) >= 2 && isASCIIAlpha(value[0]) && value[1] == ':'
|
||||
}
|
||||
|
||||
func isASCIIAlpha(value byte) bool {
|
||||
return (value >= 'a' && value <= 'z') || (value >= 'A' && value <= 'Z')
|
||||
return ""
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user