Separate Notarius invocation from extraction product promotion
This commit is contained in:
@@ -11,7 +11,6 @@ import (
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/adapters/notarius"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/artifactmodel"
|
||||
@@ -76,102 +75,26 @@ func (extractStage) Run(ctx context.Context, env *Env, m *manifest.Manifest) (re
|
||||
return nil, fmt.Errorf("extract: run id %q is not a safe path segment", runID)
|
||||
}
|
||||
|
||||
paths := sessionPathsForEnv(env, sessionID)
|
||||
input, err := artifacts.ResolveExtractionInputIdentity(paths, m)
|
||||
invocation, err := prepareExtractionInvocation(env, m, notariusConfig, sessionID, campaign, runID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("extract: resolve final-trimmed transcript identity: %w", err)
|
||||
}
|
||||
references, err := resolveExtractReferences(paths, m, notariusConfig, sessionID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("extract: resolve Notarius references: %w", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
timeout, err := time.ParseDuration(strings.TrimSpace(notariusConfig.Timeout))
|
||||
if err != nil || timeout <= 0 {
|
||||
return nil, fmt.Errorf("extract: invalid notarius timeout %q", notariusConfig.Timeout)
|
||||
}
|
||||
resolvedBinary, err := notarius.ResolveExecutable(notariusConfig.Binary)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("extract: resolve notarius binary: %w", err)
|
||||
}
|
||||
configPath, err := absolutePath(notariusConfig.ConfigPath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("extract: resolve notarius config path: %w", err)
|
||||
}
|
||||
inputPath := input.Path
|
||||
workingDirectory, err := absolutePath(notariusConfig.WorkingDirectory)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("extract: resolve notarius working directory: %w", err)
|
||||
}
|
||||
|
||||
workspaceRoot := strings.TrimSpace(paths.WorkspaceRoot)
|
||||
if workspaceRoot == "" {
|
||||
workspaceRoot = env.Config.Pipeline.Workspace.Root
|
||||
}
|
||||
reportPath, err := absolutePath(artifacts.SessionRunNotariusReportPathForCampaign(workspaceRoot, campaign, sessionID, runID))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("extract: resolve completion report path: %w", err)
|
||||
}
|
||||
logPath, err := absolutePath(artifacts.SessionRunNotariusLogPathForCampaign(workspaceRoot, campaign, sessionID, runID))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("extract: resolve stderr path: %w", err)
|
||||
}
|
||||
outputRoot, err := absolutePath(artifacts.SessionRunNotariusOutputRootForCampaign(workspaceRoot, campaign, sessionID, runID))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("extract: resolve output root: %w", err)
|
||||
}
|
||||
directories := []string{filepath.Dir(reportPath), outputRoot}
|
||||
for _, directory := range directories {
|
||||
if err := fileops.EnsureWorkspaceDirectory(directory); err != nil {
|
||||
return nil, fmt.Errorf("extract: create directory %q: %w", directory, err)
|
||||
}
|
||||
}
|
||||
referenceBindings, err := materializeExtractReferenceSnapshots(paths, references)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("extract: materialize Notarius reference snapshots: %w", err)
|
||||
}
|
||||
fingerprint, err := extractionFingerprint(notariusConfig, input, references.Identities)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("extract: build configuration fingerprint: %w", err)
|
||||
}
|
||||
cache := notariusConfig.Cache
|
||||
if env.Force {
|
||||
refresh := "refresh"
|
||||
cache = &refresh
|
||||
}
|
||||
debugDirectory, err := absolutePath(artifacts.SessionRunDebugDir(paths, runID, "extract"))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("extract: resolve debug directory: %w", err)
|
||||
}
|
||||
request := notarius.RunRequest{
|
||||
Binary: resolvedBinary, Credentials: notariusConfig.Credentials, ConfigOverlay: notariusConfig.ConfigOverlay,
|
||||
ConfigPath: configPath, PipelineID: notariusConfig.PipelineID,
|
||||
InputPath: inputPath, OutputRoot: outputRoot, WorkingDirectory: workingDirectory,
|
||||
ReportPath: reportPath, LogPath: logPath, Timeout: timeout, Cache: cache,
|
||||
Debug: notariusConfig.Debug, DebugDirectory: debugDirectory, References: referenceBindings,
|
||||
}
|
||||
// Producer files remain inspectable even when completion delivery fails.
|
||||
// Only Narratio's temporary promotion copies are cleaned below.
|
||||
adapterResult, err := env.Notarius.Run(ctx, request)
|
||||
adapterResult, err := env.Notarius.Run(ctx, invocation.request)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("extract: run notarius: %w", err)
|
||||
}
|
||||
report := adapterResult.Report
|
||||
bundle := filepath.Clean(adapterResult.BundleRoot)
|
||||
if report.Pipeline != notariusConfig.PipelineID || report.ArtifactCount < 0 || report.FallbackCount < 0 ||
|
||||
!filepath.IsAbs(bundle) || filepath.Clean(report.OutputDirectory) != bundle {
|
||||
return nil, fmt.Errorf("extract: notarius completion report is missing or incompatible")
|
||||
}
|
||||
if err := validateExtractBundleRoot(outputRoot, bundle); err != nil {
|
||||
accepted, err := acceptExtractionBundle(adapterResult, invocation.request.OutputRoot, notariusConfig.PipelineID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := normalizeNotariusBundle(bundle); err != nil {
|
||||
return nil, fmt.Errorf("extract: validate Notarius bundle: %w", err)
|
||||
}
|
||||
if err := verifyExtractReferenceSnapshots(paths, references); err != nil {
|
||||
if err := verifyExtractReferenceSnapshots(invocation.paths, invocation.references); err != nil {
|
||||
return nil, fmt.Errorf("extract: verify Notarius reference snapshots: %w", err)
|
||||
}
|
||||
staging, err := os.MkdirTemp(filepath.Dir(outputRoot), ".products-")
|
||||
|
||||
staging, err := os.MkdirTemp(filepath.Dir(invocation.request.OutputRoot), ".products-")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("extract: create product staging directory: %w", err)
|
||||
}
|
||||
@@ -180,10 +103,119 @@ func (extractStage) Run(ctx context.Context, env *Env, m *manifest.Manifest) (re
|
||||
resultErr = errors.Join(resultErr, fmt.Errorf("extract: remove staged products: %w", cleanupErr))
|
||||
}
|
||||
}()
|
||||
selected, err := stageRequiredNotariusLanes(bundle, staging, notariusConfig.Outputs)
|
||||
selected, err := stageRequiredNotariusLanes(accepted.bundle, staging, notariusConfig.Outputs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
outputs, err := promoteExtractionProducts(invocation.paths, sessionID, accepted.report.Pipeline, selected)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
report, bundle := accepted.report, accepted.bundle
|
||||
|
||||
if report.FallbackCount > 0 {
|
||||
env.Warn(fmt.Sprintf("Notarius completed with %d fallback operations", report.FallbackCount))
|
||||
}
|
||||
metadataReferences := invocation.references.Identities
|
||||
if len(metadataReferences) > config.MaxNotariusReferenceBindings {
|
||||
metadataReferences = metadataReferences[:config.MaxNotariusReferenceBindings]
|
||||
}
|
||||
metadata := map[string]any{"stage": "extract", "notarius_enabled": true, "bundle_root": bundle, "diagnostic_path": invocation.request.LogPath}
|
||||
for key, value := range artifacts.EncodeExtractionMetadata(artifacts.ExtractionMetadata{
|
||||
RunID: runID, ConfigurationFingerprint: invocation.fingerprint, Input: invocation.input, References: metadataReferences, ReferenceCount: len(invocation.references.Identities),
|
||||
Completion: artifacts.ExtractionCompletion{Pipeline: report.Pipeline, ArtifactCount: report.ArtifactCount, OutputDirectory: bundle, DebugDirectory: report.DebugDirectory, FallbackCount: report.FallbackCount},
|
||||
}) {
|
||||
metadata[key] = value
|
||||
}
|
||||
|
||||
result = &StageResult{Outputs: outputs, Logs: []string{invocation.request.ReportPath, invocation.request.LogPath}, Metadata: metadata}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
type extractInvocation struct {
|
||||
paths artifacts.SessionPaths
|
||||
input artifacts.ExtractionInputIdentity
|
||||
references resolvedExtractReferences
|
||||
fingerprint string
|
||||
request notarius.RunRequest
|
||||
}
|
||||
|
||||
func prepareExtractionInvocation(env *Env, m *manifest.Manifest, notariusConfig *config.NotariusConfig, sessionID, campaign, runID string) (extractInvocation, error) {
|
||||
paths := sessionPathsForEnv(env, sessionID)
|
||||
input, err := artifacts.ResolveExtractionInputIdentity(paths, m)
|
||||
if err != nil {
|
||||
return extractInvocation{}, fmt.Errorf("extract: resolve final-trimmed transcript identity: %w", err)
|
||||
}
|
||||
references, err := resolveExtractReferences(paths, m, notariusConfig, sessionID)
|
||||
if err != nil {
|
||||
return extractInvocation{}, fmt.Errorf("extract: resolve Notarius references: %w", err)
|
||||
}
|
||||
|
||||
options, err := resolveNotariusOptions(notariusConfig, env.Force)
|
||||
if err != nil {
|
||||
return extractInvocation{}, fmt.Errorf("extract: %w", err)
|
||||
}
|
||||
|
||||
workspaceRoot := strings.TrimSpace(paths.WorkspaceRoot)
|
||||
if workspaceRoot == "" {
|
||||
workspaceRoot = env.Config.Pipeline.Workspace.Root
|
||||
}
|
||||
reportPath, err := absolutePath(artifacts.SessionRunNotariusReportPathForCampaign(workspaceRoot, campaign, sessionID, runID))
|
||||
if err != nil {
|
||||
return extractInvocation{}, fmt.Errorf("extract: resolve completion report path: %w", err)
|
||||
}
|
||||
logPath, err := absolutePath(artifacts.SessionRunNotariusLogPathForCampaign(workspaceRoot, campaign, sessionID, runID))
|
||||
if err != nil {
|
||||
return extractInvocation{}, fmt.Errorf("extract: resolve stderr path: %w", err)
|
||||
}
|
||||
outputRoot, err := absolutePath(artifacts.SessionRunNotariusOutputRootForCampaign(workspaceRoot, campaign, sessionID, runID))
|
||||
if err != nil {
|
||||
return extractInvocation{}, fmt.Errorf("extract: resolve output root: %w", err)
|
||||
}
|
||||
directories := []string{filepath.Dir(reportPath), outputRoot}
|
||||
for _, directory := range directories {
|
||||
if err := fileops.EnsureWorkspaceDirectory(directory); err != nil {
|
||||
return extractInvocation{}, fmt.Errorf("extract: create directory %q: %w", directory, err)
|
||||
}
|
||||
}
|
||||
referenceBindings, err := materializeExtractReferenceSnapshots(paths, references)
|
||||
if err != nil {
|
||||
return extractInvocation{}, fmt.Errorf("extract: materialize Notarius reference snapshots: %w", err)
|
||||
}
|
||||
fingerprint, err := extractionFingerprint(notariusConfig, input, references.Identities)
|
||||
if err != nil {
|
||||
return extractInvocation{}, fmt.Errorf("extract: build configuration fingerprint: %w", err)
|
||||
}
|
||||
debugDirectory, err := absolutePath(artifacts.SessionRunDebugDir(paths, runID, "extract"))
|
||||
if err != nil {
|
||||
return extractInvocation{}, fmt.Errorf("extract: resolve debug directory: %w", err)
|
||||
}
|
||||
request := options.request(input.Path, outputRoot, reportPath, logPath, debugDirectory, referenceBindings)
|
||||
|
||||
return extractInvocation{paths: paths, input: input, references: references, fingerprint: fingerprint, request: request}, nil
|
||||
}
|
||||
|
||||
type acceptedExtractionBundle struct {
|
||||
report notarius.CompletionReport
|
||||
bundle string
|
||||
}
|
||||
|
||||
func acceptExtractionBundle(result notarius.RunResult, outputRoot, pipelineID string) (acceptedExtractionBundle, error) {
|
||||
report := result.Report
|
||||
bundle := filepath.Clean(result.BundleRoot)
|
||||
if report.Pipeline != pipelineID || report.ArtifactCount < 0 || report.FallbackCount < 0 || !filepath.IsAbs(bundle) || filepath.Clean(report.OutputDirectory) != bundle {
|
||||
return acceptedExtractionBundle{}, fmt.Errorf("extract: notarius completion report is missing or incompatible")
|
||||
}
|
||||
if err := validateExtractBundleRoot(outputRoot, bundle); err != nil {
|
||||
return acceptedExtractionBundle{}, err
|
||||
}
|
||||
if err := normalizeNotariusBundle(bundle); err != nil {
|
||||
return acceptedExtractionBundle{}, fmt.Errorf("extract: validate Notarius bundle: %w", err)
|
||||
}
|
||||
return acceptedExtractionBundle{report: report, bundle: bundle}, nil
|
||||
}
|
||||
|
||||
func promoteExtractionProducts(paths artifacts.SessionPaths, sessionID, pipelineID string, selected []stagedNotariusLane) ([]artifacts.Ref, error) {
|
||||
root, err := absolutePath(paths.Root)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("extract: resolve session root: %w", err)
|
||||
@@ -213,26 +245,10 @@ func (extractStage) Run(ctx context.Context, env *Env, m *manifest.Manifest) (re
|
||||
Kind: extractLaneOutputKind, SourceID: artifacts.ExtractionArtifactSourceID(lane.Key),
|
||||
Category: "artifacts", SessionID: sessionID, RelativePath: filepath.ToSlash(filepath.Join("artifacts", lane.Filename)),
|
||||
AbsolutePath: destination, Checksum: checksum, Size: lane.Size,
|
||||
ExternalProvenance: &artifactmodel.ExternalProvenance{System: "notarius", PipelineID: report.Pipeline, ArtifactID: lane.LaneID},
|
||||
ExternalProvenance: &artifactmodel.ExternalProvenance{System: "notarius", PipelineID: pipelineID, ArtifactID: lane.LaneID},
|
||||
})
|
||||
}
|
||||
if report.FallbackCount > 0 {
|
||||
env.Warn(fmt.Sprintf("Notarius completed with %d fallback operations", report.FallbackCount))
|
||||
}
|
||||
metadataReferences := references.Identities
|
||||
if len(metadataReferences) > config.MaxNotariusReferenceBindings {
|
||||
metadataReferences = metadataReferences[:config.MaxNotariusReferenceBindings]
|
||||
}
|
||||
metadata := map[string]any{"stage": "extract", "notarius_enabled": true, "bundle_root": bundle, "diagnostic_path": logPath}
|
||||
for key, value := range artifacts.EncodeExtractionMetadata(artifacts.ExtractionMetadata{
|
||||
RunID: runID, ConfigurationFingerprint: fingerprint, Input: input, References: metadataReferences, ReferenceCount: len(references.Identities),
|
||||
Completion: artifacts.ExtractionCompletion{Pipeline: report.Pipeline, ArtifactCount: report.ArtifactCount, OutputDirectory: bundle, DebugDirectory: report.DebugDirectory, FallbackCount: report.FallbackCount},
|
||||
}) {
|
||||
metadata[key] = value
|
||||
}
|
||||
|
||||
result = &StageResult{Outputs: outputs, Logs: []string{reportPath, logPath}, Metadata: metadata}
|
||||
return result, nil
|
||||
return outputs, nil
|
||||
}
|
||||
|
||||
func normalizeNotariusBundle(root string) error {
|
||||
|
||||
Reference in New Issue
Block a user