package artifacts import ( "fmt" "os" "path/filepath" "gitea.maximumdirect.net/eric/narratio/internal/artifactpolicy" "gitea.maximumdirect.net/eric/narratio/internal/manifest" ) // ValidateSuppliedProduct checks a recorded product against its current canonical // location and actual local content. Configured product paths come from the // current concrete catalog; built-in paths are fixed by the registry. func ValidateSuppliedProduct(paths SessionPaths, source, canonicalPath string, record manifest.SuppliedProduct) error { collection := &manifest.SuppliedProductCollection{Version: manifest.SuppliedProductVersion, Products: map[string]manifest.SuppliedProduct{source: record}} if err := manifest.ValidateSuppliedProducts(collection); err != nil { return err } classified, _ := artifactpolicy.ClassifySource(source) kind := contentText if spec, ok := artifactRegistry[source]; ok { canonicalPath = spec.CanonicalRelPath kind = spec.ContentKind } else if classified.Kind == artifactpolicy.SourceKindExtraction { kind = contentJSON } if record.Path != canonicalPath { return fmt.Errorf("supplied product %q has noncanonical path %q", source, record.Path) } path := filepath.Join(paths.Root, filepath.FromSlash(record.Path)) if err := requireSafeSessionRegularFile(paths.Root, path); err != nil { return fmt.Errorf("supplied product %q: %w", source, err) } info, err := os.Stat(path) if err != nil { return err } if info.Size() != record.Size { return fmt.Errorf("supplied product %q: size mismatch", source) } if err := validateResolvedContent(path, kind); err != nil { return fmt.Errorf("supplied product %q: %w", source, err) } checksum, err := SHA256File(path) if err != nil { return err } if checksum != record.Checksum { return fmt.Errorf("supplied product %q: checksum mismatch", source) } return nil } var suppliedTranscriptWork = []struct { source string stages []string }{ {ArtifactTranscriptBase, []string{"transcribe", "merge"}}, {ArtifactTranscriptPolished, []string{"transcribe", "merge", "polish"}}, {ArtifactTranscriptFinal, []string{"transcribe", "merge", "polish", "normalize"}}, {ArtifactTranscriptFinalTrimmed, []string{"transcribe", "merge", "polish", "normalize", "trim"}}, } // SuppliedTranscriptSourceCovers reports the fixed relationship independently // of current file validity, including when retiring superseded authority. func SuppliedTranscriptSourceCovers(source, name string) bool { for _, entry := range suppliedTranscriptWork { if source == entry.source { for _, stage := range entry.stages { if stage == name { return true } } } } return false } // SuppliedTranscriptCoverage returns transcript work satisfied by valid supplied // products. It neither records nor creates the intermediate products. func SuppliedTranscriptCoverage(paths SessionPaths, collection *manifest.SuppliedProductCollection) map[string]bool { covered := map[string]bool{} if collection == nil || manifest.ValidateSuppliedProducts(collection) != nil { return covered } for _, entry := range suppliedTranscriptWork { record, ok := collection.Products[entry.source] if !ok || ValidateSuppliedProduct(paths, entry.source, "", record) != nil { continue } for _, name := range entry.stages { covered[name] = true } } return covered } // hydrateSuppliedProduct publishes one valid supplied identity to the runtime catalog. func (c *ArtifactCatalog) hydrateSuppliedProduct(paths SessionPaths, m *manifest.Manifest, source, configuredPath string) bool { if c == nil || m == nil || m.SuppliedProducts == nil { return false } record, ok := m.SuppliedProducts.Products[source] if !ok { return false } canonical, err := ProductRelativePath(paths.Root, configuredPath) if err != nil || ValidateSuppliedProduct(paths, source, canonical, record) != nil { return false } path := filepath.Join(paths.Root, filepath.FromSlash(record.Path)) if err := c.markAvailable(source, path, ArtifactProvenanceSupplied); err != nil { return false } entry := c.entries[source] entry.CanonicalRelPath = canonical entry.Contract = cloneArtifactContract(record.Contract) entry.Checksum = record.Checksum entry.Size = record.Size c.entries[source] = entry return true } // SuppliedExtractionComplete reports whether every configured extraction product // is available as a valid supplied product, independent of generation history. func SuppliedExtractionComplete(paths SessionPaths, m *manifest.Manifest, configured map[string]ExtractionArtifactDefinition) bool { if m == nil || m.SuppliedProducts == nil || len(configured) == 0 { return false } for key, definition := range configured { source := ExtractionArtifactSourceID(key) filename := definition.Filename if filename == "" { filename = key + ".json" } path, err := ProductRelativePath(paths.Root, filepath.Join("artifacts", filename)) if err != nil { return false } record, ok := m.SuppliedProducts.Products[source] if !ok || ValidateSuppliedProduct(paths, source, path, record) != nil { return false } } return true }