Plan imports from published product inventories

This commit is contained in:
2026-12-02 16:02:48 +00:00
parent 4f50a57a38
commit ad8a5df97b
9 changed files with 414 additions and 37 deletions

View File

@@ -14,6 +14,7 @@ import (
"strings"
"gitea.maximumdirect.net/eric/narratio/internal/adapters/storage"
"gitea.maximumdirect.net/eric/narratio/internal/artifactmodel"
"gitea.maximumdirect.net/eric/narratio/internal/artifactpolicy"
"gitea.maximumdirect.net/eric/narratio/internal/artifacts"
"gitea.maximumdirect.net/eric/narratio/internal/config"
@@ -43,6 +44,7 @@ const (
// RestoreAction is one deterministic planner action.
type RestoreAction struct {
Kind RestoreActionKind
SourceID string
RemoteKey string
LocalRelativePath string
LocalPath string
@@ -73,6 +75,9 @@ type RestorePlan struct {
DownloadCount int
SkipSameCount int
ConflictCount int
Satisfied []string
Remaining []string
candidate *manifest.Manifest
}
// RestorePlanOptions control restore planning scope and classification.
@@ -85,6 +90,14 @@ type RestorePlanOptions struct {
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) {
return buildRestorePlanWithInventory(ctx, cfg, current, store, opts, false)
}
func buildInventoryRestorePlan(ctx context.Context, cfg *config.Config, current *RemoteCurrentState, store storage.ObjectStore, opts RestorePlanOptions) (*RestorePlan, error) {
return buildRestorePlanWithInventory(ctx, cfg, current, store, opts, true)
}
func buildRestorePlanWithInventory(ctx context.Context, cfg *config.Config, current *RemoteCurrentState, store storage.ObjectStore, opts RestorePlanOptions, inventory bool) (*RestorePlan, error) {
if err := ctx.Err(); err != nil {
return nil, err
}
@@ -106,7 +119,12 @@ func buildRestorePlan(ctx context.Context, cfg *config.Config, current *RemoteCu
defer func() { _ = sessionRoot.Close() }()
}
actions, err := buildCurrentRestoreActions(ctx, cfg, current, store, sessionPaths, sessionRoot, opts)
var actions []RestoreAction
if inventory {
actions, err = buildInventoryRestoreActions(ctx, cfg, current, store, sessionPaths, sessionRoot, opts)
} else {
actions, err = buildCurrentRestoreActions(ctx, cfg, current, store, sessionPaths, sessionRoot, opts)
}
if err != nil {
return nil, err
}
@@ -132,6 +150,11 @@ func buildRestorePlan(ctx context.Context, cfg *config.Config, current *RemoteCu
})
plan := &RestorePlan{Actions: actions}
if inventory {
if err := projectInventoryCoverage(ctx, cfg, plan); err != nil {
return nil, err
}
}
for _, action := range actions {
switch action.Kind {
case RestoreActionDownload:
@@ -147,6 +170,118 @@ func buildRestorePlan(ctx context.Context, cfg *config.Config, current *RemoteCu
return plan, nil
}
// projectInventoryCoverage describes completion if all accepted transfers
// validate successfully. It never treats a declaration as verified content.
func projectInventoryCoverage(ctx context.Context, cfg *config.Config, plan *RestorePlan) error {
manifestPath := artifacts.SessionManifestPathForCampaign(cfg.Pipeline.Workspace.Root, cfg.Session.Campaign, cfg.Session.SessionID)
local, _, err := loadManifestAtPathIfPresent(ctx, &manifest.LocalStore{}, manifestPath)
if err != nil {
return err
}
plan.candidate, err = cloneManifestForPlan(local, cfg)
if err != nil {
return fmt.Errorf("clone local restore baseline: %w", err)
}
covered := make(map[string]bool)
available := make(map[string]bool)
pathsBySource := make(map[string]string)
products := newRestoreProductPaths(nil)
paths := artifacts.NewLocalStore(cfg.Pipeline.Workspace.Root).SessionPathsFor(cfg.Session.Campaign, cfg.Session.SessionID)
configured := artifacts.ConfiguredArtifactDefinitions(cfg.ConcreteArtifacts())
extraction := artifacts.ExtractionDefinitionsFromConfig(cfg.Pipeline.Notarius)
catalog := artifacts.NewArtifactCatalog()
if err := catalog.RegisterBuiltIns(); err != nil {
return err
}
if err := catalog.RegisterConfiguredArtifacts(configured); err != nil {
return err
}
if err := catalog.RegisterExtractionArtifacts(extraction); err != nil {
return err
}
catalog.HydrateAnalyzeArtifacts(paths, local, configured)
catalog.HydrateExtractionArtifacts(paths, local, extraction)
if local != nil && local.SuppliedProducts != nil {
for source, record := range local.SuppliedProducts.Products {
canonical, pathErr := products.relativePath(cfg, nil, paths.Root, source)
if pathErr == nil && artifacts.ValidateSuppliedProduct(paths, source, canonical, record) == nil {
available[source] = true
pathsBySource[source] = canonical
}
}
}
for _, action := range plan.Actions {
if action.Kind == RestoreActionDownload {
for source, path := range pathsBySource {
if path == action.LocalRelativePath {
delete(available, source)
delete(plan.candidate.SuppliedProducts.Products, source)
}
}
}
if action.SourceID != "" && action.Kind != RestoreActionConflict {
available[action.SourceID] = true
if action.Kind == RestoreActionSkipSame {
resolved, resolveErr := artifacts.ResolveSessionArtifactWithCatalog(paths, local, action.SourceID, catalog)
if resolveErr == nil && resolved.Checksum == action.SHA256 {
continue
}
}
if plan.candidate.SuppliedProducts == nil {
plan.candidate.SuppliedProducts = &manifest.SuppliedProductCollection{Version: manifest.SuppliedProductVersion, Products: make(map[string]manifest.SuppliedProduct)}
}
plan.candidate.SuppliedProducts.Products[action.SourceID] = manifest.SuppliedProduct{Path: action.LocalRelativePath, Checksum: action.SHA256, Size: action.Size}
}
}
// Recalculate supplied coverage from the projected source set, since a
// replacement can remove an otherwise valid local supplied product.
names := []string{"transcribe", "merge", "polish", "normalize", "trim"}
for source := range available {
for _, name := range names {
if artifacts.SuppliedTranscriptSourceCovers(source, name) {
covered[name] = true
}
}
}
if cfg.Pipeline.Render == nil || cfg.Pipeline.Render.Enabled == nil || *cfg.Pipeline.Render.Enabled {
selection := []string{"normalized", "trimmed"}
if cfg.Pipeline.Render != nil {
selection = cfg.Pipeline.Render.Variants
}
variants, err := artifactmodel.SelectRenderVariants(selection)
if err != nil {
return err
}
complete := true
for _, variant := range variants {
complete = complete && available[variant.OutputSource]
}
covered["render"] = covered["render"] || complete
}
if cfg.Pipeline.Notarius != nil && cfg.Pipeline.Notarius.Enabled {
definitions := artifacts.ExtractionDefinitionsFromConfig(cfg.Pipeline.Notarius)
complete := len(definitions) > 0
for key := range definitions {
complete = complete && available[artifacts.ExtractionArtifactSourceID(key)]
}
covered["extract"] = covered["extract"] || complete
}
if cfg.Pipeline.Render == nil || cfg.Pipeline.Render.Enabled == nil || *cfg.Pipeline.Render.Enabled {
names = append(names, "render")
}
if cfg.Pipeline.Notarius != nil && cfg.Pipeline.Notarius.Enabled {
names = append(names, "extract")
}
for _, name := range names {
if covered[name] {
plan.Satisfied = append(plan.Satisfied, name)
} else {
plan.Remaining = append(plan.Remaining, name)
}
}
return nil
}
func buildCurrentRestoreActions(
ctx context.Context,
cfg *config.Config,
@@ -159,6 +294,61 @@ func buildCurrentRestoreActions(
return buildCommittedRestoreActions(ctx, cfg, current, store, sessionPaths, sessionRoot, opts)
}
// buildInventoryRestoreActions maps product identities through the current
// catalog. Remote destinations identify transfer objects, not local names.
func buildInventoryRestoreActions(ctx context.Context, cfg *config.Config, current *RemoteCurrentState, store storage.ObjectStore, sessionPaths artifacts.SessionPaths, sessionRoot *os.Root, opts RestorePlanOptions) ([]RestoreAction, error) {
if current == nil || current.Commit == nil {
return nil, fmt.Errorf("committed remote inventory is required")
}
products := newRestoreProductPaths(nil)
var actions []RestoreAction
for _, artifact := range current.Commit.Artifacts {
var rel string
var include bool
var err error
if artifact.Type == artifacts.RemoteArtifactTypePublishedOutput {
if _, classifyErr := artifactpolicy.ClassifySource(artifact.Source); classifyErr == nil {
rel, err = products.relativePath(cfg, nil, sessionPaths.Root, artifact.Source)
if err == nil {
include = true
} else if !errors.Is(err, errRestoreProductNotFound) {
return nil, fmt.Errorf("map committed source %q: %w", artifact.Source, err)
}
}
if !include && opts.IncludeAudio {
var candidate string
candidate, include, err = restoreLocalRelativePathForCommittedArtifact(current.SessionPrefix, artifact, true)
if include && restoreRelativePathIsAudio(candidate) {
rel = candidate
} else {
include = false
}
}
} else if artifact.Type == artifacts.RemoteArtifactTypeRunFile {
rel, include, err = restoreLocalRelativePathForCommittedArtifact(current.SessionPrefix, artifact, opts.IncludeAudio)
}
if err != nil && !errors.Is(err, errRestoreProductNotFound) {
return nil, fmt.Errorf("map committed object %q: %w", artifact.DestinationKey, err)
}
if !include {
continue
}
localPath, err := joinWithinSessionRoot(sessionPaths.Root, rel)
if err != nil {
return nil, err
}
action, err := classifyRestoreAction(ctx, store, storage.ObjectInfo{Key: artifact.DestinationKey, Size: artifact.Size, ETag: artifact.Generation}, artifact.SHA256, rel, localPath, sessionRoot, opts.Force)
if err != nil {
return nil, fmt.Errorf("classify committed object %q: %w", artifact.DestinationKey, err)
}
if artifact.Type == artifacts.RemoteArtifactTypePublishedOutput && !restoreRelativePathIsAudio(rel) {
action.SourceID = artifact.Source
}
actions = append(actions, action)
}
return actions, nil
}
func buildCommittedRestoreActions(
ctx context.Context,
cfg *config.Config,