Promote native Notarius lane products
This commit is contained in:
@@ -2,6 +2,7 @@ package artifacts
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
@@ -26,6 +27,7 @@ type ConfiguredArtifactDefinition struct {
|
||||
type ExtractionArtifactDefinition struct {
|
||||
LaneID string
|
||||
PipelineID string
|
||||
Filename string
|
||||
MediaType string
|
||||
SchemaID string
|
||||
SchemaVersion string
|
||||
@@ -40,12 +42,8 @@ func ExtractionDefinitionsFromConfig(cfg *config.NotariusConfig) map[string]Extr
|
||||
definitions := make(map[string]ExtractionArtifactDefinition, len(cfg.Outputs))
|
||||
for key, output := range cfg.Outputs {
|
||||
definitions[key] = ExtractionArtifactDefinition{
|
||||
LaneID: output.LaneID,
|
||||
PipelineID: cfg.PipelineID,
|
||||
MediaType: output.MediaType,
|
||||
SchemaID: output.SchemaID,
|
||||
SchemaVersion: output.SchemaVersion,
|
||||
ModuleKey: output.ModuleKey,
|
||||
LaneID: output.LaneID, PipelineID: cfg.PipelineID,
|
||||
Filename: config.ExtractionFilename(key, output),
|
||||
}
|
||||
}
|
||||
return definitions
|
||||
@@ -103,17 +101,18 @@ func (c *ArtifactCatalog) RegisterExtractionArtifacts(configured map[string]Extr
|
||||
return fmt.Errorf("duplicate extraction artifact key %q", trimmed)
|
||||
}
|
||||
def := configured[key]
|
||||
filename := def.Filename
|
||||
if filename == "" {
|
||||
filename = trimmed + ".json"
|
||||
}
|
||||
sourceID := ExtractionArtifactSourceID(trimmed)
|
||||
if err := c.addEntry(CatalogEntry{
|
||||
SourceID: sourceID,
|
||||
ExtractionKey: trimmed,
|
||||
ProducerStage: "extract",
|
||||
OutputKind: "notarius_lane",
|
||||
Planned: true,
|
||||
Contract: &artifactmodel.ContractMetadata{
|
||||
MediaType: def.MediaType, SchemaID: def.SchemaID,
|
||||
SchemaVersion: def.SchemaVersion, ModuleKey: def.ModuleKey,
|
||||
},
|
||||
SourceID: sourceID,
|
||||
ExtractionKey: trimmed,
|
||||
CanonicalRelPath: filepath.Join("artifacts", filename),
|
||||
ProducerStage: "extract",
|
||||
OutputKind: "notarius_lane",
|
||||
Planned: true,
|
||||
}); err != nil {
|
||||
return fmt.Errorf("register extraction artifact %q: %w", trimmed, err)
|
||||
}
|
||||
|
||||
@@ -5,7 +5,6 @@ import (
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/artifactmodel"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/manifest"
|
||||
)
|
||||
|
||||
@@ -13,11 +12,9 @@ import (
|
||||
const MaxExtractionPayloadBytes int64 = 64 * 1024 * 1024
|
||||
|
||||
const (
|
||||
extractStageName = "extract"
|
||||
extractionLaneKind = "notarius_lane"
|
||||
extractionIndexKind = "notarius_index"
|
||||
extractionMetadataRun = "narratio_run_id"
|
||||
extractionMetadataRoot = "bundle_root"
|
||||
extractStageName = "extract"
|
||||
extractionLaneKind = "notarius_lane"
|
||||
extractionMetadataRun = "narratio_run_id"
|
||||
)
|
||||
|
||||
// HydrateExtractionArtifacts marks extraction sources available only when the current
|
||||
@@ -47,28 +44,6 @@ func (c *ArtifactCatalog) HydrateExtractionArtifacts(
|
||||
}
|
||||
}
|
||||
|
||||
func compatibleCatalogExtractionContract(got *artifactmodel.ContractMetadata, want ExtractionArtifactDefinition) bool {
|
||||
return got != nil && got.MediaType == want.MediaType && got.SchemaID == want.SchemaID &&
|
||||
got.SchemaVersion == want.SchemaVersion && (want.ModuleKey == "" || got.ModuleKey == want.ModuleKey)
|
||||
}
|
||||
|
||||
func compatibleCatalogExtractionProvenance(
|
||||
got *artifactmodel.ExternalProvenance,
|
||||
runID, pipelineID string,
|
||||
want ExtractionArtifactDefinition,
|
||||
) bool {
|
||||
return got != nil && got.System == "notarius" && got.RunID == runID && got.PipelineID == pipelineID &&
|
||||
pipelineID == strings.TrimSpace(want.PipelineID) && got.ArtifactID == want.LaneID
|
||||
}
|
||||
|
||||
func safeExistingExtractionDirectory(sessionRoot, bundleRoot string) bool {
|
||||
if !pathWithinExtractionRoot(sessionRoot, bundleRoot) || !safeExtractionComponents(sessionRoot, bundleRoot) {
|
||||
return false
|
||||
}
|
||||
info, err := os.Lstat(bundleRoot)
|
||||
return err == nil && info.IsDir() && info.Mode()&os.ModeSymlink == 0
|
||||
}
|
||||
|
||||
func safeExtractionComponents(root, target string) bool {
|
||||
relative, err := filepath.Rel(filepath.Clean(root), filepath.Clean(target))
|
||||
if err != nil || relative == "." || relative == ".." || strings.HasPrefix(relative, ".."+string(filepath.Separator)) {
|
||||
@@ -99,8 +74,3 @@ func extractionMetadataString(metadata map[string]any, key string) string {
|
||||
value, _ := metadata[key].(string)
|
||||
return strings.TrimSpace(value)
|
||||
}
|
||||
|
||||
func extractionReceiptIdentity(metadata map[string]any) (string, string) {
|
||||
receipt, _ := metadata["receipt"].(map[string]any)
|
||||
return extractionMetadataString(receipt, "run_id"), extractionMetadataString(receipt, "pipeline_id")
|
||||
}
|
||||
|
||||
@@ -56,47 +56,35 @@ func InspectExtractionEvidence(
|
||||
if !safeExtractionPathSegment(runID) {
|
||||
return ExtractionEvidence{State: ExtractionEvidenceObsolete, Reason: "extract result has an invalid producing run ID"}
|
||||
}
|
||||
root := filepath.Clean(filepath.Join(paths.ArtifactsDir, "notarius", runID))
|
||||
if !filepath.IsAbs(root) || extractionMetadataString(r.Metadata, extractionMetadataRoot) != root {
|
||||
return ExtractionEvidence{State: ExtractionEvidenceObsolete, Reason: "extract result does not identify its canonical immutable bundle"}
|
||||
root, err := filepath.Abs(paths.Root)
|
||||
if err != nil {
|
||||
return ExtractionEvidence{State: ExtractionEvidenceUnsafe, Reason: "session root cannot be resolved"}
|
||||
}
|
||||
info, err := os.Lstat(root)
|
||||
if os.IsNotExist(err) {
|
||||
return ExtractionEvidence{State: ExtractionEvidenceObsolete, Reason: "immutable Notarius bundle is missing"}
|
||||
}
|
||||
if err != nil || info.Mode()&os.ModeSymlink != 0 {
|
||||
return ExtractionEvidence{State: ExtractionEvidenceUnsafe, Reason: "immutable Notarius bundle is unsafe"}
|
||||
}
|
||||
if !info.IsDir() {
|
||||
return ExtractionEvidence{State: ExtractionEvidenceObsolete, Reason: "immutable Notarius bundle is not a directory"}
|
||||
}
|
||||
if !safeExistingExtractionDirectory(paths.Root, root) {
|
||||
return ExtractionEvidence{State: ExtractionEvidenceUnsafe, Reason: "immutable Notarius bundle is unsafe"}
|
||||
}
|
||||
receiptRunID, receiptPipelineID := extractionReceiptIdentity(r.Metadata)
|
||||
if receiptRunID == "" || receiptPipelineID == "" {
|
||||
return ExtractionEvidence{State: ExtractionEvidenceObsolete, Reason: "extract result has incompatible Notarius receipt identity"}
|
||||
artifactRoot := filepath.Join(root, "artifacts")
|
||||
completion, ok := r.Metadata["completion"].(map[string]any)
|
||||
if !ok || extractionMetadataString(completion, "pipeline") == "" {
|
||||
return ExtractionEvidence{State: ExtractionEvidenceObsolete, Reason: "extract result has no native completion identity"}
|
||||
}
|
||||
pipelineID := extractionMetadataString(completion, "pipeline")
|
||||
expected := make(map[string]ExtractionArtifactDefinition, len(configured))
|
||||
for key, d := range configured {
|
||||
expected[ExtractionArtifactSourceID(key)] = d
|
||||
filenames := make(map[string]string, len(configured))
|
||||
for key, definition := range configured {
|
||||
source := ExtractionArtifactSourceID(key)
|
||||
expected[source] = definition
|
||||
filename := definition.Filename
|
||||
if filename == "" {
|
||||
filename = key + ".json"
|
||||
}
|
||||
if filename == "." || filename == ".." || filepath.Base(filename) != filename || strings.ContainsAny(filename, `/\\`) {
|
||||
return ExtractionEvidence{State: ExtractionEvidenceUnsafe, Reason: "configured extraction filename is unsafe"}
|
||||
}
|
||||
filenames[source] = filename
|
||||
}
|
||||
seen, outputs := map[string]struct{}{}, map[string]ExtractionEvidenceOutput{}
|
||||
indexSeen := false
|
||||
for _, out := range r.Outputs {
|
||||
if strings.TrimSpace(out.ProducerRunID) != runID {
|
||||
return ExtractionEvidence{State: ExtractionEvidenceObsolete, Reason: "extract output producer identity is inconsistent"}
|
||||
}
|
||||
if out.SourceID == "" {
|
||||
if indexSeen || out.Kind != extractionIndexKind || filepath.Clean(out.LocalPath) != filepath.Join(root, "index.json") {
|
||||
return ExtractionEvidence{State: ExtractionEvidenceObsolete, Reason: "extract index path is not canonical"}
|
||||
}
|
||||
if state, reason, _ := inspectExtractionPayload(root, out.LocalPath, out.Checksum); state != ExtractionEvidenceValid {
|
||||
return ExtractionEvidence{State: state, Reason: reason}
|
||||
}
|
||||
indexSeen = true
|
||||
continue
|
||||
}
|
||||
d, ok := expected[out.SourceID]
|
||||
if !ok || out.Kind != extractionLaneKind {
|
||||
return ExtractionEvidence{State: ExtractionEvidenceObsolete, Reason: "extract result source set differs from current configuration"}
|
||||
@@ -104,22 +92,27 @@ func InspectExtractionEvidence(
|
||||
if _, duplicate := seen[out.SourceID]; duplicate {
|
||||
return ExtractionEvidence{State: ExtractionEvidenceObsolete, Reason: "extract result contains a duplicate configured source"}
|
||||
}
|
||||
if !compatibleCatalogExtractionContract(out.Contract, d) || !compatibleCatalogExtractionProvenance(out.ExternalProvenance, receiptRunID, receiptPipelineID, d) {
|
||||
return ExtractionEvidence{State: ExtractionEvidenceObsolete, Reason: "extract output contract or provenance is incompatible"}
|
||||
if !filepath.IsAbs(out.LocalPath) || filepath.Clean(out.LocalPath) != filepath.Join(artifactRoot, filenames[out.SourceID]) {
|
||||
return ExtractionEvidence{State: ExtractionEvidenceUnsafe, Reason: "extract output path is not its canonical destination"}
|
||||
}
|
||||
provenance := out.ExternalProvenance
|
||||
if provenance == nil || provenance.System != "notarius" || provenance.RunID != "" || provenance.PipelineID != pipelineID || pipelineID != strings.TrimSpace(d.PipelineID) || provenance.ArtifactID != d.LaneID {
|
||||
return ExtractionEvidence{State: ExtractionEvidenceObsolete, Reason: "extract output lane or pipeline identity is incompatible"}
|
||||
}
|
||||
state, reason, size := inspectExtractionPayload(root, out.LocalPath, out.Checksum)
|
||||
if state != ExtractionEvidenceValid {
|
||||
return ExtractionEvidence{State: state, Reason: reason}
|
||||
}
|
||||
seen[out.SourceID] = struct{}{}
|
||||
outputs[out.SourceID] = ExtractionEvidenceOutput{
|
||||
Path: out.LocalPath, Checksum: out.Checksum, Size: size,
|
||||
Contract: cloneArtifactContract(out.Contract),
|
||||
if out.Size <= 0 || out.Size != size {
|
||||
return ExtractionEvidence{State: ExtractionEvidenceObsolete, Reason: "extract output size does not match durable bytes"}
|
||||
}
|
||||
seen[out.SourceID] = struct{}{}
|
||||
outputs[out.SourceID] = ExtractionEvidenceOutput{Path: out.LocalPath, Checksum: out.Checksum, Size: size}
|
||||
}
|
||||
if !indexSeen || len(seen) != len(expected) || len(r.Outputs) != len(expected)+1 {
|
||||
if len(seen) != len(expected) || len(r.Outputs) != len(expected) {
|
||||
return ExtractionEvidence{State: ExtractionEvidenceObsolete, Reason: "extract result is incomplete"}
|
||||
}
|
||||
|
||||
return ExtractionEvidence{State: ExtractionEvidenceValid, ProducerRunID: runID, Outputs: outputs}
|
||||
}
|
||||
|
||||
|
||||
@@ -138,6 +138,11 @@ func SessionRunNotariusOutputRootForCampaign(rootDir, campaign, sessionID, runID
|
||||
return filepath.Join(SessionRunExtractDirForCampaign(rootDir, campaign, sessionID, runID), "notarius-output")
|
||||
}
|
||||
|
||||
// SessionRunNotariusRetentionPath records the session-durable role of a bundle.
|
||||
func SessionRunNotariusRetentionPath(paths SessionPaths, runID string) string {
|
||||
return filepath.Join(paths.RunsDir, runID, "extract", "notarius-retention.json")
|
||||
}
|
||||
|
||||
// SessionNotariusReferenceCacheDir returns the stable cache root for verified
|
||||
// reference snapshots supplied to Notarius.
|
||||
func SessionNotariusReferenceCacheDir(paths SessionPaths) string {
|
||||
|
||||
@@ -18,6 +18,7 @@ type Ref struct {
|
||||
ArchivePath string
|
||||
RemoteKey string
|
||||
Checksum string
|
||||
Size int64
|
||||
ProducerRunID string
|
||||
Contract *artifactmodel.ContractMetadata
|
||||
ExternalProvenance *artifactmodel.ExternalProvenance
|
||||
|
||||
Reference in New Issue
Block a user