2026-08-10 22:06:48 +00:00
|
|
|
package artifacts
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"os"
|
|
|
|
|
"path/filepath"
|
|
|
|
|
"strings"
|
|
|
|
|
|
2026-08-29 19:02:16 +00:00
|
|
|
"gitea.maximumdirect.net/eric/narratio/internal/artifactmodel"
|
2026-08-10 22:06:48 +00:00
|
|
|
"gitea.maximumdirect.net/eric/narratio/internal/fileops"
|
|
|
|
|
"gitea.maximumdirect.net/eric/narratio/internal/manifest"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type ExtractionEvidenceState string
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
ExtractionEvidenceValid ExtractionEvidenceState = "valid"
|
|
|
|
|
ExtractionEvidenceAbsent ExtractionEvidenceState = "absent"
|
|
|
|
|
ExtractionEvidenceObsolete ExtractionEvidenceState = "obsolete"
|
|
|
|
|
ExtractionEvidenceUnsafe ExtractionEvidenceState = "unsafe"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// ExtractionEvidence is a policy-neutral proof of configured extraction output.
|
|
|
|
|
type ExtractionEvidence struct {
|
|
|
|
|
State ExtractionEvidenceState
|
|
|
|
|
Reason, ProducerRunID string
|
2026-08-29 19:02:16 +00:00
|
|
|
Outputs map[string]ExtractionEvidenceOutput
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ExtractionEvidenceOutput is the verified semantic identity of one lane.
|
|
|
|
|
type ExtractionEvidenceOutput struct {
|
|
|
|
|
Path string
|
|
|
|
|
Checksum string
|
|
|
|
|
Size int64
|
|
|
|
|
Contract *artifactmodel.ContractMetadata
|
2026-08-10 22:06:48 +00:00
|
|
|
}
|
|
|
|
|
|
2026-08-10 22:15:53 +00:00
|
|
|
// InspectExtractionEvidence verifies structure, confinement, identities, contracts, and payload bytes.
|
|
|
|
|
func InspectExtractionEvidence(
|
|
|
|
|
paths SessionPaths,
|
|
|
|
|
m *manifest.Manifest,
|
|
|
|
|
configured map[string]ExtractionArtifactDefinition,
|
|
|
|
|
input ExtractionInputIdentity,
|
|
|
|
|
) ExtractionEvidence {
|
2026-08-10 22:06:48 +00:00
|
|
|
if m == nil || len(configured) == 0 {
|
|
|
|
|
return ExtractionEvidence{State: ExtractionEvidenceAbsent, Reason: "extraction evidence is absent"}
|
|
|
|
|
}
|
|
|
|
|
r := m.Stages[extractStageName]
|
|
|
|
|
if r == nil || r.Name != extractStageName || r.Status != manifest.StatusSucceeded {
|
|
|
|
|
return ExtractionEvidence{State: ExtractionEvidenceAbsent, Reason: "extract stage has no succeeded result"}
|
|
|
|
|
}
|
2026-08-10 22:15:53 +00:00
|
|
|
if !extractionInputMatchesMetadata(r.Metadata, input) {
|
|
|
|
|
return ExtractionEvidence{State: ExtractionEvidenceObsolete, Reason: "extract result does not match the current trimmed transcript"}
|
|
|
|
|
}
|
2026-08-10 22:06:48 +00:00
|
|
|
runID := extractionMetadataString(r.Metadata, extractionMetadataRun)
|
|
|
|
|
if !safeExtractionPathSegment(runID) {
|
|
|
|
|
return ExtractionEvidence{State: ExtractionEvidenceObsolete, Reason: "extract result has an invalid producing run ID"}
|
|
|
|
|
}
|
2026-11-30 01:02:01 +00:00
|
|
|
root, err := filepath.Abs(paths.Root)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return ExtractionEvidence{State: ExtractionEvidenceUnsafe, Reason: "session root cannot be resolved"}
|
2026-08-10 22:06:48 +00:00
|
|
|
}
|
2026-11-30 01:02:01 +00:00
|
|
|
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"}
|
2026-08-10 22:06:48 +00:00
|
|
|
}
|
2026-11-30 01:02:01 +00:00
|
|
|
pipelineID := extractionMetadataString(completion, "pipeline")
|
2026-08-10 22:06:48 +00:00
|
|
|
expected := make(map[string]ExtractionArtifactDefinition, len(configured))
|
2026-11-30 01:02:01 +00:00
|
|
|
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
|
2026-08-10 22:06:48 +00:00
|
|
|
}
|
2026-08-29 19:02:16 +00:00
|
|
|
seen, outputs := map[string]struct{}{}, map[string]ExtractionEvidenceOutput{}
|
2026-08-10 22:06:48 +00:00
|
|
|
for _, out := range r.Outputs {
|
|
|
|
|
if strings.TrimSpace(out.ProducerRunID) != runID {
|
|
|
|
|
return ExtractionEvidence{State: ExtractionEvidenceObsolete, Reason: "extract output producer identity is inconsistent"}
|
|
|
|
|
}
|
|
|
|
|
d, ok := expected[out.SourceID]
|
|
|
|
|
if !ok || out.Kind != extractionLaneKind {
|
|
|
|
|
return ExtractionEvidence{State: ExtractionEvidenceObsolete, Reason: "extract result source set differs from current configuration"}
|
|
|
|
|
}
|
|
|
|
|
if _, duplicate := seen[out.SourceID]; duplicate {
|
|
|
|
|
return ExtractionEvidence{State: ExtractionEvidenceObsolete, Reason: "extract result contains a duplicate configured source"}
|
|
|
|
|
}
|
2026-11-30 01:02:01 +00:00
|
|
|
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"}
|
2026-08-10 22:06:48 +00:00
|
|
|
}
|
2026-08-29 19:02:16 +00:00
|
|
|
state, reason, size := inspectExtractionPayload(root, out.LocalPath, out.Checksum)
|
|
|
|
|
if state != ExtractionEvidenceValid {
|
2026-08-10 22:06:48 +00:00
|
|
|
return ExtractionEvidence{State: state, Reason: reason}
|
|
|
|
|
}
|
2026-11-30 01:02:01 +00:00
|
|
|
if out.Size <= 0 || out.Size != size {
|
|
|
|
|
return ExtractionEvidence{State: ExtractionEvidenceObsolete, Reason: "extract output size does not match durable bytes"}
|
2026-08-29 19:02:16 +00:00
|
|
|
}
|
2026-11-30 01:02:01 +00:00
|
|
|
seen[out.SourceID] = struct{}{}
|
|
|
|
|
outputs[out.SourceID] = ExtractionEvidenceOutput{Path: out.LocalPath, Checksum: out.Checksum, Size: size}
|
2026-08-10 22:06:48 +00:00
|
|
|
}
|
2026-11-30 01:02:01 +00:00
|
|
|
if len(seen) != len(expected) || len(r.Outputs) != len(expected) {
|
2026-08-10 22:06:48 +00:00
|
|
|
return ExtractionEvidence{State: ExtractionEvidenceObsolete, Reason: "extract result is incomplete"}
|
|
|
|
|
}
|
2026-11-30 01:02:01 +00:00
|
|
|
|
2026-08-10 22:06:48 +00:00
|
|
|
return ExtractionEvidence{State: ExtractionEvidenceValid, ProducerRunID: runID, Outputs: outputs}
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-29 19:02:16 +00:00
|
|
|
func inspectExtractionPayload(root, path, checksum string) (ExtractionEvidenceState, string, int64) {
|
2026-08-10 22:06:48 +00:00
|
|
|
if !filepath.IsAbs(path) || !pathWithinExtractionRoot(root, path) || strings.TrimSpace(checksum) == "" {
|
2026-08-29 19:02:16 +00:00
|
|
|
return ExtractionEvidenceUnsafe, "extract output path or checksum is unsafe", 0
|
2026-08-10 22:06:48 +00:00
|
|
|
}
|
|
|
|
|
info, err := os.Lstat(path)
|
|
|
|
|
if os.IsNotExist(err) {
|
2026-08-29 19:02:16 +00:00
|
|
|
return ExtractionEvidenceObsolete, "extract output is missing", 0
|
2026-08-10 22:06:48 +00:00
|
|
|
}
|
|
|
|
|
if !safeExtractionComponents(root, path) {
|
2026-08-29 19:02:16 +00:00
|
|
|
return ExtractionEvidenceUnsafe, "extract output path contains unsafe components", 0
|
2026-08-10 22:06:48 +00:00
|
|
|
}
|
|
|
|
|
if err != nil || !info.Mode().IsRegular() || info.Mode()&os.ModeSymlink != 0 {
|
2026-08-29 19:02:16 +00:00
|
|
|
return ExtractionEvidenceUnsafe, "extract output is not a regular file", 0
|
2026-08-10 22:06:48 +00:00
|
|
|
}
|
|
|
|
|
actual, err := SHA256File(path)
|
|
|
|
|
if err != nil {
|
2026-08-29 19:02:16 +00:00
|
|
|
return ExtractionEvidenceUnsafe, "extract output checksum cannot be read", 0
|
2026-08-10 22:06:48 +00:00
|
|
|
}
|
|
|
|
|
if actual != checksum {
|
2026-08-29 19:02:16 +00:00
|
|
|
return ExtractionEvidenceObsolete, "extract output checksum does not match durable bytes", 0
|
2026-08-10 22:06:48 +00:00
|
|
|
}
|
2026-09-01 17:51:37 +00:00
|
|
|
body, err := fileops.ReadFileBounded(path, MaxExtractionPayloadBytes)
|
2026-08-10 22:06:48 +00:00
|
|
|
if err != nil || !json.Valid(body) {
|
2026-08-29 19:02:16 +00:00
|
|
|
return ExtractionEvidenceObsolete, "extract output is not valid JSON", 0
|
2026-08-10 22:06:48 +00:00
|
|
|
}
|
2026-08-29 19:02:16 +00:00
|
|
|
return ExtractionEvidenceValid, "", info.Size()
|
2026-08-10 22:06:48 +00:00
|
|
|
}
|