From 807fbbeda301bef7b37dd5c50b45df266a524ea7 Mon Sep 17 00:00:00 2001 From: Eric Rakestraw Date: Mon, 30 Nov 2026 04:03:27 +0000 Subject: [PATCH] Index product paths once during restore planning --- docs/roadmap/remediation.md | 7 ++- internal/app/restore_plan.go | 61 ++++++++++++++-------- internal/app/restore_product_paths_test.go | 35 +++++++++++++ 3 files changed, 80 insertions(+), 23 deletions(-) create mode 100644 internal/app/restore_product_paths_test.go diff --git a/docs/roadmap/remediation.md b/docs/roadmap/remediation.md index 83cfac8..a5a26e7 100644 --- a/docs/roadmap/remediation.md +++ b/docs/roadmap/remediation.md @@ -199,7 +199,12 @@ product directory remain invalid under the existing placement contract. **Priority:** P3 -**Status:** For consideration +**Status:** Complete + +Restore builds one source-path index and lazily constructs one fallback catalog +per plan. Duplicate evidence remains available for agreement checks; unrelated +paths are validated only when selected. Verified committed restore, fallback, +conflict, and path-agreement tests in `internal/app`. **Stages:** 19 and 21. diff --git a/internal/app/restore_plan.go b/internal/app/restore_plan.go index 39a9dec..8829926 100644 --- a/internal/app/restore_plan.go +++ b/internal/app/restore_plan.go @@ -163,6 +163,7 @@ func buildCommittedRestoreActions( return nil, fmt.Errorf("committed remote current state is required") } actions := make([]RestoreAction, 0, len(current.Commit.Artifacts)) + products := newRestoreProductPaths(current.Manifest) for _, artifact := range current.Commit.Artifacts { rel, include, err := restoreLocalRelativePathForCommittedArtifact(current.SessionPrefix, artifact, opts.IncludeAudio) if err != nil { @@ -171,7 +172,7 @@ func buildCommittedRestoreActions( if artifact.Type == artifacts.RemoteArtifactTypePublishedOutput && current.Manifest != nil { identity, _ := artifacts.ParsePublishedOutputObjectKey(current.SessionPrefix, artifact.DestinationKey) if _, sourceErr := artifactpolicy.ClassifySource(artifact.Source); sourceErr == nil { - productPath, productErr := restoreProductRelativePath(cfg, current.Manifest, sessionPaths.Root, artifact.Source) + productPath, productErr := products.relativePath(cfg, current.Manifest, sessionPaths.Root, artifact.Source) if productErr != nil && (!strings.HasPrefix(identity.Destination, config.PathAudioDirSegment+"/") || !errors.Is(productErr, errRestoreProductNotFound)) { return nil, fmt.Errorf("map committed source %q: %w", artifact.Source, productErr) } @@ -201,28 +202,41 @@ func buildCommittedRestoreActions( return actions, nil } -// restoreProductRelativePath takes local product identity from the committed -// session evidence; the remote publication destination is only an object key. -func restoreProductRelativePath(cfg *config.Config, m *manifest.Manifest, destinationRoot, source string) (string, error) { - producerRoot, hasProducerRoot := restoredManifestSessionRoot(m) - var paths []string +// restoreProductPaths indexes evidence once, retaining duplicates for the +// per-source agreement checks. Unselected paths are not validated here. +type restoreProductPaths struct { + bySource map[string][]string + fallback *artifacts.ArtifactCatalog +} + +func newRestoreProductPaths(m *manifest.Manifest) *restoreProductPaths { + index := &restoreProductPaths{bySource: make(map[string][]string)} + if m == nil { + return index + } for _, record := range m.Stages { if record == nil { continue } for _, output := range record.Outputs { - if output.SourceID == source { - paths = append(paths, output.LocalPath) - } + index.bySource[output.SourceID] = append(index.bySource[output.SourceID], output.LocalPath) } for _, artifact := range record.AnalyzeArtifacts { - if artifact.Output != nil && artifact.Output.SourceID == source { - paths = append(paths, artifact.Output.LocalPath) + if artifact.Output != nil { + output := artifact.Output + index.bySource[output.SourceID] = append(index.bySource[output.SourceID], output.LocalPath) } } } + return index +} + +// relativePath takes local product identity from the committed session +// evidence; the remote publication destination is only an object key. +func (index *restoreProductPaths) relativePath(cfg *config.Config, m *manifest.Manifest, destinationRoot, source string) (string, error) { + producerRoot, hasProducerRoot := restoredManifestSessionRoot(m) var result string - for _, value := range paths { + for _, value := range index.bySource[source] { local, err := rebaseRestoredLocalReference("published output", value, producerRoot, hasProducerRoot, destinationRoot) if err != nil { return "", err @@ -242,17 +256,20 @@ func restoreProductRelativePath(cfg *config.Config, m *manifest.Manifest, destin result = rel } if result == "" { - catalog := artifacts.NewArtifactCatalog() - if err := catalog.RegisterBuiltIns(); err != nil { - return "", err + if index.fallback == nil { + catalog := artifacts.NewArtifactCatalog() + if err := catalog.RegisterBuiltIns(); err != nil { + return "", err + } + if err := catalog.RegisterConfiguredArtifacts(artifacts.ConfiguredArtifactDefinitions(cfg.ConcreteArtifacts())); err != nil { + return "", err + } + if err := catalog.RegisterExtractionArtifacts(artifacts.ExtractionDefinitionsFromConfig(cfg.Pipeline.Notarius)); err != nil { + return "", err + } + index.fallback = catalog } - if err := catalog.RegisterConfiguredArtifacts(artifacts.ConfiguredArtifactDefinitions(cfg.ConcreteArtifacts())); err != nil { - return "", err - } - if err := catalog.RegisterExtractionArtifacts(artifacts.ExtractionDefinitionsFromConfig(cfg.Pipeline.Notarius)); err != nil { - return "", err - } - if entry, ok := catalog.Lookup(source); ok { + if entry, ok := index.fallback.Lookup(source); ok { relative, err := artifacts.ProductRelativePath(destinationRoot, entry.CanonicalRelPath) if err != nil { return "", err diff --git a/internal/app/restore_product_paths_test.go b/internal/app/restore_product_paths_test.go new file mode 100644 index 0000000..e963adb --- /dev/null +++ b/internal/app/restore_product_paths_test.go @@ -0,0 +1,35 @@ +package app + +import ( + "testing" + + "gitea.maximumdirect.net/eric/narratio/internal/manifest" +) + +func TestRestoreProductPathsRetainsAgreementChecks(t *testing.T) { + cfg := restorePlanConfig(t) + const source = "narratio.artifact.session_recap" + for _, conflicting := range []bool{false, true} { + m := &manifest.Manifest{Stages: map[string]*manifest.StageRecord{ + "analyze": {Outputs: []manifest.ArtifactRecord{{SourceID: source, LocalPath: "artifacts/session_recap.md"}}, + AnalyzeArtifacts: map[string]manifest.AnalyzeArtifactRecord{ + "session_recap": {Output: &manifest.ArtifactRecord{SourceID: source, LocalPath: "artifacts/session_recap.md"}}, + }}, + "other": {Outputs: []manifest.ArtifactRecord{{SourceID: "unselected", LocalPath: "../outside"}}}, + }} + if conflicting { + m.Stages["analyze"].AnalyzeArtifacts["session_recap"].Output.LocalPath = "artifacts/other.md" + } + index := newRestoreProductPaths(m) + path, err := index.relativePath(cfg, m, t.TempDir(), source) + if conflicting && err == nil { + t.Fatal("conflicting evidence accepted") + } + if !conflicting && (err != nil || path != "artifacts/session_recap.md") { + t.Fatalf("agreeing evidence: %q, %v", path, err) + } + if _, err := index.relativePath(cfg, m, t.TempDir(), "unselected"); err == nil { + t.Fatal("requested unsafe evidence accepted") + } + } +}