Restore prepared inputs and flat session products

This commit is contained in:
2026-11-30 01:53:11 +00:00
parent e19ba81406
commit 3118bc35ba
12 changed files with 282 additions and 62 deletions

View File

@@ -17,6 +17,7 @@ import (
"gitea.maximumdirect.net/eric/narratio/internal/artifactpolicy"
"gitea.maximumdirect.net/eric/narratio/internal/artifacts"
"gitea.maximumdirect.net/eric/narratio/internal/config"
"gitea.maximumdirect.net/eric/narratio/internal/manifest"
"gitea.maximumdirect.net/eric/narratio/internal/pathsafe"
"gitea.maximumdirect.net/eric/narratio/internal/previouscache"
)
@@ -72,6 +73,8 @@ type RestorePlanOptions struct {
DryRun bool
}
var errRestoreProductNotFound = errors.New("restore product source is unavailable")
func buildRestorePlan(ctx context.Context, cfg *config.Config, current *RemoteCurrentState, store storage.ObjectStore, opts RestorePlanOptions) (*RestorePlan, error) {
if err := ctx.Err(); err != nil {
return nil, err
@@ -94,7 +97,7 @@ func buildRestorePlan(ctx context.Context, cfg *config.Config, current *RemoteCu
defer func() { _ = sessionRoot.Close() }()
}
actions, err := buildCurrentRestoreActions(ctx, current, store, sessionPaths, sessionRoot, opts)
actions, err := buildCurrentRestoreActions(ctx, cfg, current, store, sessionPaths, sessionRoot, opts)
if err != nil {
return nil, err
}
@@ -104,6 +107,13 @@ func buildRestorePlan(ctx context.Context, cfg *config.Config, current *RemoteCu
return nil, err
}
actions = append(actions, previousActions...)
seenPaths := make(map[string]string, len(actions))
for _, action := range actions {
if prior, exists := seenPaths[action.LocalRelativePath]; exists {
return nil, fmt.Errorf("multiple committed objects target %q (%q and %q)", action.LocalRelativePath, prior, action.RemoteKey)
}
seenPaths[action.LocalRelativePath] = action.RemoteKey
}
sort.Slice(actions, func(i, j int) bool {
if actions[i].LocalRelativePath == actions[j].LocalRelativePath {
@@ -130,17 +140,19 @@ func buildRestorePlan(ctx context.Context, cfg *config.Config, current *RemoteCu
func buildCurrentRestoreActions(
ctx context.Context,
cfg *config.Config,
current *RemoteCurrentState,
store storage.ObjectStore,
sessionPaths artifacts.SessionPaths,
sessionRoot *os.Root,
opts RestorePlanOptions,
) ([]RestoreAction, error) {
return buildCommittedRestoreActions(ctx, current, store, sessionPaths, sessionRoot, opts)
return buildCommittedRestoreActions(ctx, cfg, current, store, sessionPaths, sessionRoot, opts)
}
func buildCommittedRestoreActions(
ctx context.Context,
cfg *config.Config,
current *RemoteCurrentState,
store storage.ObjectStore,
sessionPaths artifacts.SessionPaths,
@@ -156,6 +168,18 @@ func buildCommittedRestoreActions(
if err != nil {
return nil, fmt.Errorf("map committed object %q: %w", artifact.DestinationKey, err)
}
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)
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)
}
if productErr == nil {
rel, include = productPath, true
}
}
}
if !include {
continue
}
@@ -177,15 +201,86 @@ 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
for _, record := range m.Stages {
if record == nil {
continue
}
for _, output := range record.Outputs {
if output.SourceID == source {
paths = append(paths, output.LocalPath)
}
}
for _, artifact := range record.AnalyzeArtifacts {
if artifact.Output != nil && artifact.Output.SourceID == source {
paths = append(paths, artifact.Output.LocalPath)
}
}
}
var result string
for _, value := range paths {
local, err := rebaseRestoredLocalReference("published output", value, producerRoot, hasProducerRoot, destinationRoot)
if err != nil {
return "", err
}
rel, err := filepath.Rel(destinationRoot, local)
if err != nil {
return "", err
}
rel = filepath.ToSlash(rel)
parts := strings.Split(rel, "/")
if len(parts) != 2 || (parts[0] != config.PathArtifactsDirSegment && parts[0] != config.PathTranscriptsSegment) || parts[1] == "" || parts[1] == "." || parts[1] == ".." {
return "", fmt.Errorf("product path %q is not a flat canonical product", rel)
}
if result != "" && result != rel {
return "", fmt.Errorf("conflicting product paths for %q", source)
}
result = rel
}
if result == "" {
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
}
if entry, ok := catalog.Lookup(source); ok {
result = filepath.ToSlash(entry.CanonicalRelPath)
parts := strings.Split(result, "/")
if len(parts) != 2 || (parts[0] != config.PathArtifactsDirSegment && parts[0] != config.PathTranscriptsSegment) || parts[1] == "" || parts[1] == "." || parts[1] == ".." {
return "", fmt.Errorf("configured product path %q is not flat", result)
}
} else {
return "", fmt.Errorf("%w: %q", errRestoreProductNotFound, source)
}
}
return result, nil
}
func restoreLocalRelativePathForCommittedArtifact(sessionPrefix string, artifact artifacts.RemoteArtifact, includeAudio bool) (string, bool, error) {
if artifact.Type == artifacts.RemoteArtifactTypeSessionManifest {
return config.PathManifestFile, true, nil
}
if artifact.Type == artifacts.RemoteArtifactTypeRunFile {
prepared := map[string]bool{"run:prepare/inputs/session.yml": true, "run:prepare/inputs/campaign.yml": true, "run:prepare/inputs/party.yml": true}
prepared := map[string]bool{
"run:prepare/inputs/session.yml": true, "run:prepare/inputs/campaign.yml": true,
"run:prepare/inputs/party.yml": true, "run:prepare/inputs/players.yml": true,
"run:prepare/inputs/speakers.yml": true, "run:prepare/inputs/autocorrect.yml": true,
"run:prepare/inputs/pipeline.resolved.yml": true,
}
for _, descriptor := range artifactpolicy.GlossaryInputSources() {
prepared["run:prepare/inputs/"+descriptor.Filename] = true
}
combined, _ := artifactpolicy.DescribePreparedInputSource(artifactpolicy.SourceInputGlossaryCombined)
prepared["run:prepare/inputs/"+combined.Filename] = true
if prepared[artifact.Source] {
identity, err := artifacts.ParseImmutableRunObjectKey(sessionPrefix, artifact.DestinationKey)
if err != nil || "run:"+identity.RelativePath != artifact.Source {