Index product paths once during restore planning
This commit is contained in:
@@ -199,7 +199,12 @@ product directory remain invalid under the existing placement contract.
|
|||||||
|
|
||||||
**Priority:** P3
|
**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.
|
**Stages:** 19 and 21.
|
||||||
|
|
||||||
|
|||||||
@@ -163,6 +163,7 @@ func buildCommittedRestoreActions(
|
|||||||
return nil, fmt.Errorf("committed remote current state is required")
|
return nil, fmt.Errorf("committed remote current state is required")
|
||||||
}
|
}
|
||||||
actions := make([]RestoreAction, 0, len(current.Commit.Artifacts))
|
actions := make([]RestoreAction, 0, len(current.Commit.Artifacts))
|
||||||
|
products := newRestoreProductPaths(current.Manifest)
|
||||||
for _, artifact := range current.Commit.Artifacts {
|
for _, artifact := range current.Commit.Artifacts {
|
||||||
rel, include, err := restoreLocalRelativePathForCommittedArtifact(current.SessionPrefix, artifact, opts.IncludeAudio)
|
rel, include, err := restoreLocalRelativePathForCommittedArtifact(current.SessionPrefix, artifact, opts.IncludeAudio)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -171,7 +172,7 @@ func buildCommittedRestoreActions(
|
|||||||
if artifact.Type == artifacts.RemoteArtifactTypePublishedOutput && current.Manifest != nil {
|
if artifact.Type == artifacts.RemoteArtifactTypePublishedOutput && current.Manifest != nil {
|
||||||
identity, _ := artifacts.ParsePublishedOutputObjectKey(current.SessionPrefix, artifact.DestinationKey)
|
identity, _ := artifacts.ParsePublishedOutputObjectKey(current.SessionPrefix, artifact.DestinationKey)
|
||||||
if _, sourceErr := artifactpolicy.ClassifySource(artifact.Source); sourceErr == nil {
|
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)) {
|
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)
|
return nil, fmt.Errorf("map committed source %q: %w", artifact.Source, productErr)
|
||||||
}
|
}
|
||||||
@@ -201,28 +202,41 @@ func buildCommittedRestoreActions(
|
|||||||
return actions, nil
|
return actions, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// restoreProductRelativePath takes local product identity from the committed
|
// restoreProductPaths indexes evidence once, retaining duplicates for the
|
||||||
// session evidence; the remote publication destination is only an object key.
|
// per-source agreement checks. Unselected paths are not validated here.
|
||||||
func restoreProductRelativePath(cfg *config.Config, m *manifest.Manifest, destinationRoot, source string) (string, error) {
|
type restoreProductPaths struct {
|
||||||
producerRoot, hasProducerRoot := restoredManifestSessionRoot(m)
|
bySource map[string][]string
|
||||||
var paths []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 {
|
for _, record := range m.Stages {
|
||||||
if record == nil {
|
if record == nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
for _, output := range record.Outputs {
|
for _, output := range record.Outputs {
|
||||||
if output.SourceID == source {
|
index.bySource[output.SourceID] = append(index.bySource[output.SourceID], output.LocalPath)
|
||||||
paths = append(paths, output.LocalPath)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
for _, artifact := range record.AnalyzeArtifacts {
|
for _, artifact := range record.AnalyzeArtifacts {
|
||||||
if artifact.Output != nil && artifact.Output.SourceID == source {
|
if artifact.Output != nil {
|
||||||
paths = append(paths, artifact.Output.LocalPath)
|
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
|
var result string
|
||||||
for _, value := range paths {
|
for _, value := range index.bySource[source] {
|
||||||
local, err := rebaseRestoredLocalReference("published output", value, producerRoot, hasProducerRoot, destinationRoot)
|
local, err := rebaseRestoredLocalReference("published output", value, producerRoot, hasProducerRoot, destinationRoot)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
@@ -242,6 +256,7 @@ func restoreProductRelativePath(cfg *config.Config, m *manifest.Manifest, destin
|
|||||||
result = rel
|
result = rel
|
||||||
}
|
}
|
||||||
if result == "" {
|
if result == "" {
|
||||||
|
if index.fallback == nil {
|
||||||
catalog := artifacts.NewArtifactCatalog()
|
catalog := artifacts.NewArtifactCatalog()
|
||||||
if err := catalog.RegisterBuiltIns(); err != nil {
|
if err := catalog.RegisterBuiltIns(); err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
@@ -252,7 +267,9 @@ func restoreProductRelativePath(cfg *config.Config, m *manifest.Manifest, destin
|
|||||||
if err := catalog.RegisterExtractionArtifacts(artifacts.ExtractionDefinitionsFromConfig(cfg.Pipeline.Notarius)); err != nil {
|
if err := catalog.RegisterExtractionArtifacts(artifacts.ExtractionDefinitionsFromConfig(cfg.Pipeline.Notarius)); err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
if entry, ok := catalog.Lookup(source); ok {
|
index.fallback = catalog
|
||||||
|
}
|
||||||
|
if entry, ok := index.fallback.Lookup(source); ok {
|
||||||
relative, err := artifacts.ProductRelativePath(destinationRoot, entry.CanonicalRelPath)
|
relative, err := artifacts.ProductRelativePath(destinationRoot, entry.CanonicalRelPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
|
|||||||
35
internal/app/restore_product_paths_test.go
Normal file
35
internal/app/restore_product_paths_test.go
Normal file
@@ -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")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user