Bind extraction reuse to transcript identity
This commit is contained in:
@@ -31,7 +31,11 @@ func (c *ArtifactCatalog) HydrateExtractionArtifacts(
|
||||
if c == nil || m == nil || len(configured) == 0 {
|
||||
return
|
||||
}
|
||||
proof := InspectExtractionEvidence(paths, m, configured)
|
||||
input, err := ResolveExtractionInputIdentity(paths, m)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
proof := InspectExtractionEvidence(paths, m, configured, input)
|
||||
if proof.State != ExtractionEvidenceValid {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -79,9 +79,10 @@ func TestHydrateExtractionArtifactsAcceptsOnlyCompleteCurrentBundle(t *testing.T
|
||||
|
||||
func TestInspectExtractionEvidenceClassifiesBundleStates(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
state ExtractionEvidenceState
|
||||
mutate func(*testing.T, *SessionPaths, *manifest.Manifest)
|
||||
name string
|
||||
state ExtractionEvidenceState
|
||||
recomputeInputIdentity bool
|
||||
mutate func(*testing.T, *SessionPaths, *manifest.Manifest)
|
||||
}{
|
||||
{name: "valid", state: ExtractionEvidenceValid, mutate: func(_ *testing.T, _ *SessionPaths, _ *manifest.Manifest) {}},
|
||||
{name: "absent", state: ExtractionEvidenceAbsent, mutate: func(_ *testing.T, _ *SessionPaths, m *manifest.Manifest) {
|
||||
@@ -90,6 +91,12 @@ func TestInspectExtractionEvidenceClassifiesBundleStates(t *testing.T) {
|
||||
{name: "obsolete version", state: ExtractionEvidenceObsolete, mutate: func(_ *testing.T, _ *SessionPaths, m *manifest.Manifest) {
|
||||
m.Stages["extract"].Outputs[0].Contract.SchemaVersion = "99"
|
||||
}},
|
||||
{name: "changed direct transcript", state: ExtractionEvidenceObsolete, recomputeInputIdentity: true, mutate: func(t *testing.T, _ *SessionPaths, m *manifest.Manifest) {
|
||||
writeExtractionFixtureFile(t, m.Stages["trim"].Outputs[0].LocalPath, `{"segments":[{"id":"changed"}]}`)
|
||||
}},
|
||||
{name: "changed direct producer", state: ExtractionEvidenceObsolete, recomputeInputIdentity: true, mutate: func(_ *testing.T, _ *SessionPaths, m *manifest.Manifest) {
|
||||
m.Stages["trim"].Outputs[0].ProducerRunID = "trim-run-2"
|
||||
}},
|
||||
{name: "incomplete", state: ExtractionEvidenceObsolete, mutate: func(_ *testing.T, _ *SessionPaths, m *manifest.Manifest) {
|
||||
m.Stages["extract"].Outputs = m.Stages["extract"].Outputs[1:]
|
||||
}},
|
||||
@@ -115,8 +122,18 @@ func TestInspectExtractionEvidenceClassifiesBundleStates(t *testing.T) {
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
paths, currentManifest, definitions := validExtractionCatalogFixture(t)
|
||||
input, err := ResolveExtractionInputIdentity(paths, currentManifest)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
test.mutate(t, &paths, currentManifest)
|
||||
proof := InspectExtractionEvidence(paths, currentManifest, definitions)
|
||||
if test.recomputeInputIdentity {
|
||||
input, err = ResolveExtractionInputIdentity(paths, currentManifest)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
proof := InspectExtractionEvidence(paths, currentManifest, definitions, input)
|
||||
if proof.State != test.state {
|
||||
t.Fatalf("proof = %#v, want %q", proof, test.state)
|
||||
}
|
||||
@@ -231,18 +248,31 @@ func validExtractionCatalogFixture(t *testing.T) (SessionPaths, *manifest.Manife
|
||||
t.Helper()
|
||||
root := t.TempDir()
|
||||
paths := SessionPaths{Root: root, ArtifactsDir: filepath.Join(root, "artifacts")}
|
||||
trimmedPath := filepath.Join(paths.Root, filepath.FromSlash(TranscriptPathFinalTrimmed))
|
||||
bundleRoot := filepath.Join(paths.ArtifactsDir, "notarius", "extract-run-1")
|
||||
lanePath := filepath.Join(bundleRoot, "lanes", "encounters.json")
|
||||
indexPath := filepath.Join(bundleRoot, "index.json")
|
||||
writeExtractionFixtureFile(t, lanePath, `{"encounters":[]}`)
|
||||
writeExtractionFixtureFile(t, indexPath, `{"lanes":[]}`)
|
||||
writeExtractionFixtureFile(t, trimmedPath, `{"segments":[]}`)
|
||||
definitions := extractionFixtureDefinitions()
|
||||
m := manifest.New("session", fixtureTime)
|
||||
m.Stages["trim"] = &manifest.StageRecord{
|
||||
Name: "trim", Status: manifest.StatusSucceeded,
|
||||
Outputs: []manifest.ArtifactRecord{{
|
||||
Kind: TranscriptOutputKindFinalTrimmed, LocalPath: trimmedPath, ProducerRunID: "trim-run-1",
|
||||
}},
|
||||
}
|
||||
input, err := ResolveExtractionInputIdentity(paths, m)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
m.Stages["extract"] = &manifest.StageRecord{
|
||||
Name: "extract", Status: manifest.StatusSucceeded,
|
||||
Metadata: map[string]any{
|
||||
"narratio_run_id": "extract-run-1", "bundle_root": bundleRoot,
|
||||
"receipt": map[string]any{"run_id": "notarius-run-1", "pipeline_id": "campaign.extract"},
|
||||
"receipt": map[string]any{"run_id": "notarius-run-1", "pipeline_id": "campaign.extract"},
|
||||
extractionMetadataInput: input.Metadata(),
|
||||
},
|
||||
Outputs: []manifest.ArtifactRecord{
|
||||
{
|
||||
|
||||
@@ -26,8 +26,13 @@ type ExtractionEvidence struct {
|
||||
Outputs map[string]string
|
||||
}
|
||||
|
||||
// InspectExtractionEvidence verifies structure, confinement, identity, contracts, and payload bytes.
|
||||
func InspectExtractionEvidence(paths SessionPaths, m *manifest.Manifest, configured map[string]ExtractionArtifactDefinition) ExtractionEvidence {
|
||||
// InspectExtractionEvidence verifies structure, confinement, identities, contracts, and payload bytes.
|
||||
func InspectExtractionEvidence(
|
||||
paths SessionPaths,
|
||||
m *manifest.Manifest,
|
||||
configured map[string]ExtractionArtifactDefinition,
|
||||
input ExtractionInputIdentity,
|
||||
) ExtractionEvidence {
|
||||
if m == nil || len(configured) == 0 {
|
||||
return ExtractionEvidence{State: ExtractionEvidenceAbsent, Reason: "extraction evidence is absent"}
|
||||
}
|
||||
@@ -35,6 +40,9 @@ func InspectExtractionEvidence(paths SessionPaths, m *manifest.Manifest, configu
|
||||
if r == nil || r.Name != extractStageName || r.Status != manifest.StatusSucceeded {
|
||||
return ExtractionEvidence{State: ExtractionEvidenceAbsent, Reason: "extract stage has no succeeded result"}
|
||||
}
|
||||
if !extractionInputMatchesMetadata(r.Metadata, input) {
|
||||
return ExtractionEvidence{State: ExtractionEvidenceObsolete, Reason: "extract result does not match the current trimmed transcript"}
|
||||
}
|
||||
runID := extractionMetadataString(r.Metadata, extractionMetadataRun)
|
||||
if !safeExtractionPathSegment(runID) {
|
||||
return ExtractionEvidence{State: ExtractionEvidenceObsolete, Reason: "extract result has an invalid producing run ID"}
|
||||
|
||||
76
internal/artifacts/extraction_input.go
Normal file
76
internal/artifacts/extraction_input.go
Normal file
@@ -0,0 +1,76 @@
|
||||
package artifacts
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/fileops"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/manifest"
|
||||
)
|
||||
|
||||
const extractionMetadataInput = "direct_input"
|
||||
|
||||
// ExtractionInputIdentity identifies the exact trimmed transcript used for extraction.
|
||||
type ExtractionInputIdentity struct {
|
||||
Path string `json:"-"`
|
||||
Checksum string `json:"checksum"`
|
||||
SourceID string `json:"source_id"`
|
||||
ProducerStage string `json:"producer_stage"`
|
||||
OutputKind string `json:"output_kind"`
|
||||
ProducerRunID string `json:"producer_run_id"`
|
||||
Provenance string `json:"provenance"`
|
||||
}
|
||||
|
||||
// ResolveExtractionInputIdentity resolves and hashes the direct transcript input.
|
||||
func ResolveExtractionInputIdentity(paths SessionPaths, m *manifest.Manifest) (ExtractionInputIdentity, error) {
|
||||
resolved, err := ResolveSessionArtifact(paths, m, ArtifactTranscriptFinalTrimmed)
|
||||
if err != nil {
|
||||
return ExtractionInputIdentity{}, err
|
||||
}
|
||||
path, err := filepath.Abs(resolved.Path)
|
||||
if err != nil {
|
||||
return ExtractionInputIdentity{}, fmt.Errorf("resolve trimmed transcript path: %w", err)
|
||||
}
|
||||
bytes, err := fileops.ReadRegularFile(path, MaxResolvedArtifactBytes)
|
||||
if err != nil {
|
||||
return ExtractionInputIdentity{}, fmt.Errorf("read trimmed transcript: %w", err)
|
||||
}
|
||||
digest := sha256.Sum256(bytes)
|
||||
return ExtractionInputIdentity{
|
||||
Path: filepath.Clean(path),
|
||||
Checksum: hex.EncodeToString(digest[:]),
|
||||
SourceID: resolved.ID,
|
||||
ProducerStage: resolved.ProducerStage,
|
||||
OutputKind: resolved.OutputKind,
|
||||
ProducerRunID: resolved.ProducerRunID,
|
||||
Provenance: resolved.Provenance,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Metadata returns the durable fields needed to compare this identity on reuse.
|
||||
func (identity ExtractionInputIdentity) Metadata() map[string]any {
|
||||
return map[string]any{
|
||||
"checksum": identity.Checksum,
|
||||
"source_id": identity.SourceID,
|
||||
"producer_stage": identity.ProducerStage,
|
||||
"output_kind": identity.OutputKind,
|
||||
"producer_run_id": identity.ProducerRunID,
|
||||
"provenance": identity.Provenance,
|
||||
}
|
||||
}
|
||||
|
||||
func extractionInputMatchesMetadata(metadata map[string]any, current ExtractionInputIdentity) bool {
|
||||
stored, ok := metadata[extractionMetadataInput].(map[string]any)
|
||||
if !ok || strings.TrimSpace(current.Checksum) == "" || strings.TrimSpace(current.SourceID) == "" {
|
||||
return false
|
||||
}
|
||||
for key, want := range current.Metadata() {
|
||||
if extractionMetadataString(stored, key) != want {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
Reference in New Issue
Block a user