Index product paths once during restore planning

This commit is contained in:
2026-11-30 04:03:27 +00:00
parent 3795d86587
commit 807fbbeda3
3 changed files with 80 additions and 23 deletions

View File

@@ -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

View 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")
}
}
}