Index manifest input candidates within each operation

This commit is contained in:
2026-12-02 21:49:40 +00:00
parent 6fb15b0094
commit f7a7afe12f
14 changed files with 108 additions and 37 deletions

View File

@@ -41,10 +41,10 @@ their scope. Unless assigned otherwise, a finding remains independent audit
remediation. Shared code does not make a refactoring a prerequisite or assign remediation. Shared code does not make a refactoring a prerequisite or assign
the whole finding to a roadmap. the whole finding to a roadmap.
Thirty-seven entries are closed by the safety, transfer, audio discovery, S3 Thirty-eight entries are closed by the safety, transfer, audio discovery, S3
planning, prerequisite-history, publication-import, CLI classification, planning, prerequisite-history, publication-import, CLI classification,
inspection aggregation, test maintenance, dead-code removal, and associated inspection aggregation, test maintenance, dead-code removal, and associated
documentation work; 6 remain open. **Planned** identifies documentation work; 5 remain open. **Planned** identifies
an agreed remedy awaiting implementation. **Potentially eliminated** requires verification that the offending an agreed remedy awaiting implementation. **Potentially eliminated** requires verification that the offending
lifecycle is removed. **Coordinate** identifies shared changes, not a commitment lifecycle is removed. **Coordinate** identifies shared changes, not a commitment
that a related feature implements the entire finding. Findings close only after their that a related feature implements the entire finding. Findings close only after their
@@ -772,7 +772,13 @@ confinement coverage.
### CAT-27 — P3: Index repeated previous-input and prepared-record lookups ### CAT-27 — P3: Index repeated previous-input and prepared-record lookups
Status: Open Status: Closed
**Closure.** Previous publication declarations and optional absence use
operation-local indexes; prepared and previous input resolution share an
input-snapshot lookup retaining ordered candidates and verifying file contents.
The previous-cache, artifact, stage, and app package tests pass, including
snapshot replacement and content revalidation coverage.
**Disposition / owner.** Coordinate — [inventory-based import](../../internal/command-restore.md) **Disposition / owner.** Coordinate — [inventory-based import](../../internal/command-restore.md)
changed previous-input discovery and prepared-record consumers. Reassess the changed previous-input discovery and prepared-record consumers. Reassess the

View File

@@ -614,6 +614,8 @@ Validation: `go test ./internal/previouscache ./internal/app ./internal/stage`.
## Stage 23: Index Manifest Input Selection Within A Phase ## Stage 23: Index Manifest Input Selection Within A Phase
Status: Complete.
CAT IDs: CAT-27, completion. Status: Pending. CAT IDs: CAT-27, completion. Status: Pending.
Read Stage 22's EF-03 section and `artifacts/prepared_input.go`, Read Stage 22's EF-03 section and `artifacts/prepared_input.go`,

View File

@@ -263,6 +263,13 @@ func ResolvePreviousSessionArtifactWithCatalog(
m *manifest.Manifest, m *manifest.Manifest,
source string, source string,
catalog *ArtifactCatalog, catalog *ArtifactCatalog,
) (ResolvedSessionArtifact, error) {
return ResolvePreviousSessionArtifactWithLookup(paths, NewInputLookup(paths, m), source, catalog)
}
// ResolvePreviousSessionArtifactWithLookup resolves from one input snapshot.
func ResolvePreviousSessionArtifactWithLookup(
paths SessionPaths, lookup *InputLookup, source string, catalog *ArtifactCatalog,
) (ResolvedSessionArtifact, error) { ) (ResolvedSessionArtifact, error) {
artifactName, ok := PreviousSessionArtifactName(source) artifactName, ok := PreviousSessionArtifactName(source)
if !ok { if !ok {
@@ -275,15 +282,9 @@ func ResolvePreviousSessionArtifactWithCatalog(
if _, ok := catalog.Lookup(ConfiguredArtifactSourceID(artifactName)); !ok { if _, ok := catalog.Lookup(ConfiguredArtifactSourceID(artifactName)); !ok {
return ResolvedSessionArtifact{}, fmt.Errorf("unsupported previous-session artifact source %q", source) return ResolvedSessionArtifact{}, fmt.Errorf("unsupported previous-session artifact source %q", source)
} }
if m == nil {
return ResolvedSessionArtifact{}, &SessionArtifactNotFoundError{ArtifactID: source}
}
var selected manifest.InputRecord var selected manifest.InputRecord
count := 0 count := 0
for _, input := range m.Inputs { for _, input := range lookup.bySource[inputSourceKey{"previous_artifact", source}] {
if input.Kind != "previous_artifact" || input.Source != source {
continue
}
candidate := filepath.Clean(ResolveSessionLocalPathForRead(paths, input.Path)) candidate := filepath.Clean(ResolveSessionLocalPathForRead(paths, input.Path))
if strings.TrimSpace(candidate) == "" { if strings.TrimSpace(candidate) == "" {
continue continue

View File

@@ -0,0 +1,39 @@
package artifacts
import (
"path/filepath"
"strings"
"gitea.maximumdirect.net/eric/narratio/internal/manifest"
)
type inputSourceKey struct{ kind, source string }
// InputLookup holds candidate records for one manifest input snapshot. Construct
// a new lookup whenever the manifest's inputs are replaced.
type InputLookup struct {
byKind map[string][]manifest.InputRecord
bySource map[inputSourceKey][]manifest.InputRecord
byPath map[string][]manifest.InputRecord
}
// NewInputLookup indexes candidates without validating their content or paths.
func NewInputLookup(paths SessionPaths, m *manifest.Manifest) *InputLookup {
lookup := &InputLookup{byKind: make(map[string][]manifest.InputRecord), bySource: make(map[inputSourceKey][]manifest.InputRecord), byPath: make(map[string][]manifest.InputRecord)}
if m == nil {
return lookup
}
root, rootErr := filepath.Abs(strings.TrimSpace(paths.Root))
for _, record := range m.Inputs {
kind := strings.TrimSpace(record.Kind)
lookup.byKind[kind] = append(lookup.byKind[kind], record)
key := inputSourceKey{record.Kind, record.Source}
lookup.bySource[key] = append(lookup.bySource[key], record)
if rootErr == nil && strings.TrimSpace(paths.Root) != "" {
if resolved, err := resolvePreparedManifestPath(paths, record.Path, filepath.Clean(root)); err == nil {
lookup.byPath[resolved] = append(lookup.byPath[resolved], record)
}
}
}
return lookup
}

View File

@@ -46,6 +46,11 @@ type PreparedInputIdentity struct {
// ResolvePreparedInput resolves a prepared stable source exclusively from its // ResolvePreparedInput resolves a prepared stable source exclusively from its
// current manifest record and verifies the canonical file's identity. // current manifest record and verifies the canonical file's identity.
func ResolvePreparedInput(paths SessionPaths, m *manifest.Manifest, sourceID string) (PreparedInputIdentity, error) { func ResolvePreparedInput(paths SessionPaths, m *manifest.Manifest, sourceID string) (PreparedInputIdentity, error) {
return ResolvePreparedInputWithLookup(paths, NewInputLookup(paths, m), sourceID)
}
// ResolvePreparedInputWithLookup resolves a source against one input snapshot.
func ResolvePreparedInputWithLookup(paths SessionPaths, lookup *InputLookup, sourceID string) (PreparedInputIdentity, error) {
descriptor, ok := artifactpolicy.DescribePreparedInputSource(sourceID) descriptor, ok := artifactpolicy.DescribePreparedInputSource(sourceID)
if !ok { if !ok {
if sourceID == artifactpolicy.SourceInputGlossary { if sourceID == artifactpolicy.SourceInputGlossary {
@@ -59,27 +64,13 @@ func ResolvePreparedInput(paths SessionPaths, m *manifest.Manifest, sourceID str
return PreparedInputIdentity{}, err return PreparedInputIdentity{}, err
} }
matching := make([]manifest.InputRecord, 0, 1) matching := lookup.byKind[descriptor.ManifestKind]
if m != nil {
for _, record := range m.Inputs {
if strings.TrimSpace(record.Kind) == descriptor.ManifestKind {
matching = append(matching, record)
}
}
}
if len(matching) == 0 { if len(matching) == 0 {
if m != nil { if records := lookup.byPath[canonicalPath]; len(records) > 0 {
for _, record := range m.Inputs { return PreparedInputIdentity{}, fmt.Errorf(
recordedPath, pathErr := resolvePreparedManifestPath(paths, record.Path, rootPath) "prepared input source %q canonical path is recorded with manifest kind %q, want %q",
if pathErr == nil && recordedPath == canonicalPath { descriptor.SourceID, strings.TrimSpace(records[0].Kind), descriptor.ManifestKind,
return PreparedInputIdentity{}, fmt.Errorf( )
"prepared input source %q canonical path is recorded with manifest kind %q, want %q",
descriptor.SourceID,
strings.TrimSpace(record.Kind),
descriptor.ManifestKind,
)
}
}
} }
return PreparedInputIdentity{}, &PreparedInputAbsentError{SourceID: descriptor.SourceID} return PreparedInputIdentity{}, &PreparedInputAbsentError{SourceID: descriptor.SourceID}
} }

View File

@@ -240,3 +240,22 @@ func TestResolvePreparedGlossaryDocuments(t *testing.T) {
t.Fatalf("retired source: %v", err) t.Fatalf("retired source: %v", err)
} }
} }
func TestInputLookupKeepsSnapshotAndRechecksFileContent(t *testing.T) {
paths, m, file, _ := preparedInputFixture(t, artifactpolicy.SourceInputParty, []byte("party: ready\n"))
m.Inputs = append(m.Inputs, manifest.InputRecord{Kind: "unrelated", Path: "../../outside"})
lookup := NewInputLookup(paths, m)
m.Inputs = nil
if _, err := ResolvePreparedInputWithLookup(paths, lookup, artifactpolicy.SourceInputParty); err != nil {
t.Fatalf("snapshot resolution: %v", err)
}
if _, err := ResolvePreparedInputWithLookup(paths, NewInputLookup(paths, m), artifactpolicy.SourceInputParty); !errors.Is(err, ErrPreparedInputAbsent) {
t.Fatalf("replaced inputs: %v", err)
}
if err := os.WriteFile(file, []byte("party: changed\n"), 0o644); err != nil {
t.Fatal(err)
}
if _, err := ResolvePreparedInputWithLookup(paths, lookup, artifactpolicy.SourceInputParty); err == nil || !strings.Contains(err.Error(), "checksum mismatch") {
t.Fatalf("changed content: %v", err)
}
}

View File

@@ -37,6 +37,7 @@ type analyzeArtifactExecutionResult struct {
} }
type analyzeExecutionContext struct { type analyzeExecutionContext struct {
InputLookup *artifacts.InputLookup
Graph *artifactdeps.Graph Graph *artifactdeps.Graph
Env *Env Env *Env
Manifest *manifest.Manifest Manifest *manifest.Manifest
@@ -124,6 +125,7 @@ func (analyzeStage) Run(ctx context.Context, env *Env, m *manifest.Manifest) (*S
SessionID: sessionID, SessionID: sessionID,
TranscriptRefs: discoverAnalyzeTranscriptRefs(m, paths), TranscriptRefs: discoverAnalyzeTranscriptRefs(m, paths),
Catalog: runtimeCatalog, Catalog: runtimeCatalog,
InputLookup: artifacts.NewInputLookup(paths, m),
} }
reconciliation, err := reconcileAnalyzeArtifacts(scriptoriumCfg, execution) reconciliation, err := reconcileAnalyzeArtifacts(scriptoriumCfg, execution)
if err != nil { if err != nil {

View File

@@ -131,6 +131,9 @@ func computeAnalyzeArtifactFingerprint(
scriptoriumCfg *config.ScriptoriumConfig, scriptoriumCfg *config.ScriptoriumConfig,
execution analyzeExecutionContext, execution analyzeExecutionContext,
) (string, resolvedAnalyzeInputs, []analyzeDependencyOutputIdentity, error) { ) (string, resolvedAnalyzeInputs, []analyzeDependencyOutputIdentity, error) {
if execution.InputLookup == nil {
execution.InputLookup = artifacts.NewInputLookup(execution.Paths, execution.Manifest)
}
artifactCfg, ok := artifactCfgMap(execution)[key] artifactCfg, ok := artifactCfgMap(execution)[key]
if !ok { if !ok {
return "", resolvedAnalyzeInputs{}, nil, fmt.Errorf("configured artifact %q is not defined", key) return "", resolvedAnalyzeInputs{}, nil, fmt.Errorf("configured artifact %q is not defined", key)

View File

@@ -75,6 +75,9 @@ func resolveAnalyzeInputIdentities(
inputs map[string]config.ScriptoriumInputConfig, inputs map[string]config.ScriptoriumInputConfig,
execution analyzeExecutionContext, execution analyzeExecutionContext,
) (resolvedAnalyzeInputs, error) { ) (resolvedAnalyzeInputs, error) {
if execution.InputLookup == nil {
execution.InputLookup = artifacts.NewInputLookup(execution.Paths, execution.Manifest)
}
result := resolvedAnalyzeInputs{ result := resolvedAnalyzeInputs{
Ordered: make([]analyzeInputIdentity, 0, len(inputs)), Ordered: make([]analyzeInputIdentity, 0, len(inputs)),
paths: make(map[string]string, len(inputs)), paths: make(map[string]string, len(inputs)),
@@ -115,7 +118,7 @@ func resolveAnalyzeInputIdentity(
} }
if descriptor.Source.Kind == artifactpolicy.SourceKindStableInput { if descriptor.Source.Kind == artifactpolicy.SourceKindStableInput {
prepared, preparedErr := artifacts.ResolvePreparedInput(execution.Paths, execution.Manifest, descriptor.Source.ID) prepared, preparedErr := artifacts.ResolvePreparedInputWithLookup(execution.Paths, execution.InputLookup, descriptor.Source.ID)
if preparedErr == nil { if preparedErr == nil {
identity.Present = true identity.Present = true
identity.Checksum = prepared.Checksum identity.Checksum = prepared.Checksum
@@ -143,8 +146,8 @@ func resolveAnalyzeInputIdentity(
var resolved artifacts.ResolvedSessionArtifact var resolved artifacts.ResolvedSessionArtifact
if descriptor.Source.Kind == artifactpolicy.SourceKindPreviousArtifact { if descriptor.Source.Kind == artifactpolicy.SourceKindPreviousArtifact {
resolved, err = artifacts.ResolvePreviousSessionArtifactWithCatalog( resolved, err = artifacts.ResolvePreviousSessionArtifactWithLookup(
execution.Paths, execution.Manifest, descriptor.Source.ID, execution.Catalog, execution.Paths, execution.InputLookup, descriptor.Source.ID, execution.Catalog,
) )
} else { } else {
resolved, err = artifacts.ResolveSessionArtifactWithCatalog( resolved, err = artifacts.ResolveSessionArtifactWithCatalog(

View File

@@ -54,6 +54,7 @@ func (analyzeStage) ValidateResume(_ context.Context, env *Env, m *manifest.Mani
Graph: graph, Graph: graph,
Env: env, Manifest: m, Paths: paths, SessionID: sessionID, Env: env, Manifest: m, Paths: paths, SessionID: sessionID,
TranscriptRefs: discoverAnalyzeTranscriptRefs(m, paths), Catalog: catalog, TranscriptRefs: discoverAnalyzeTranscriptRefs(m, paths), Catalog: catalog,
InputLookup: artifacts.NewInputLookup(paths, m),
} }
reconciliation, err := reconcileAnalyzeArtifacts(cfg, execution) reconciliation, err := reconcileAnalyzeArtifacts(cfg, execution)
if err != nil { if err != nil {

View File

@@ -263,10 +263,11 @@ func resolveExtractReferences(
Bindings: make([]notarius.ReferenceBinding, 0, len(configured)), Bindings: make([]notarius.ReferenceBinding, 0, len(configured)),
Identities: make([]extractReferenceIdentity, 0, len(configured)), Identities: make([]extractReferenceIdentity, 0, len(configured)),
} }
lookup := artifacts.NewInputLookup(paths, m)
for _, reference := range configured { for _, reference := range configured {
selector := reference.selector selector := reference.selector
sourceID := reference.sourceID sourceID := reference.sourceID
identity, err := artifacts.ResolvePreparedInput(paths, m, sourceID) identity, err := artifacts.ResolvePreparedInputWithLookup(paths, lookup, sourceID)
if err != nil { if err != nil {
return resolvedExtractReferences{}, fmt.Errorf( return resolvedExtractReferences{}, fmt.Errorf(
"reference %q source %q is unavailable or invalid; run narratio run %s --stage prepare --force: %w", "reference %q source %q is unavailable or invalid; run narratio run %s --stage prepare --force: %w",

View File

@@ -37,8 +37,9 @@ func polishGlossaryEvidence(paths artifacts.SessionPaths, m *manifest.Manifest)
Source, Checksum string Source, Checksum string
Size int64 Size int64
} }
lookup := artifacts.NewInputLookup(paths, m)
for _, descriptor := range artifactpolicy.GlossaryInputSources() { for _, descriptor := range artifactpolicy.GlossaryInputSources() {
identity, err := artifacts.ResolvePreparedInput(paths, m, descriptor.SourceID) identity, err := artifacts.ResolvePreparedInputWithLookup(paths, lookup, descriptor.SourceID)
if err != nil { if err != nil {
return nil, "", fmt.Errorf("prepared glossary %s is unavailable or invalid; run prepare: %w", descriptor.SourceID, err) return nil, "", fmt.Errorf("prepared glossary %s is unavailable or invalid; run prepare: %w", descriptor.SourceID, err)
} }

View File

@@ -68,8 +68,9 @@ func (prepareStage) ValidateResume(ctx context.Context, env *Env, m *manifest.Ma
return NonResumable(fmt.Sprintf("prepared sources could not be verified; rerun prepare: %v", err)), nil return NonResumable(fmt.Sprintf("prepared sources could not be verified; rerun prepare: %v", err)), nil
} }
paths := sessionPathsForEnv(env, m.SessionID) paths := sessionPathsForEnv(env, m.SessionID)
lookup := artifacts.NewInputLookup(paths, m)
for _, source := range []string{artifactpolicy.SourceInputGlossaryParty, artifactpolicy.SourceInputGlossaryCampaign, artifactpolicy.SourceInputGlossarySetting, artifactpolicy.SourceInputGlossarySpells, artifactpolicy.SourceInputGlossaryCombined} { for _, source := range []string{artifactpolicy.SourceInputGlossaryParty, artifactpolicy.SourceInputGlossaryCampaign, artifactpolicy.SourceInputGlossarySetting, artifactpolicy.SourceInputGlossarySpells, artifactpolicy.SourceInputGlossaryCombined} {
if _, err := artifacts.ResolvePreparedInput(paths, m, source); err != nil { if _, err := artifacts.ResolvePreparedInputWithLookup(paths, lookup, source); err != nil {
return NonResumable(prepareResumeChangedReason), nil return NonResumable(prepareResumeChangedReason), nil
} }
} }

View File

@@ -43,6 +43,7 @@ func collectPublishPreparedInputs(store artifacts.Store, paths artifacts.Session
filenames[combined.ManifestKind] = combined.Filename filenames[combined.ManifestKind] = combined.Filename
glossaries[combined.ManifestKind] = combined glossaries[combined.ManifestKind] = combined
var result []publishUploadFile var result []publishUploadFile
lookup := artifacts.NewInputLookup(paths, &manifest.Manifest{Inputs: records})
seen := make(map[string]bool) seen := make(map[string]bool)
for _, record := range records { for _, record := range records {
name, ok := filenames[record.Kind] name, ok := filenames[record.Kind]
@@ -57,7 +58,7 @@ func collectPublishPreparedInputs(store artifacts.Store, paths artifacts.Session
return nil, fmt.Errorf("prepared input %q lacks canonical path or checksum", record.Kind) return nil, fmt.Errorf("prepared input %q lacks canonical path or checksum", record.Kind)
} }
if descriptor, ok := glossaries[record.Kind]; ok { if descriptor, ok := glossaries[record.Kind]; ok {
if _, err := artifacts.ResolvePreparedInput(paths, &manifest.Manifest{Inputs: records}, descriptor.SourceID); err != nil { if _, err := artifacts.ResolvePreparedInputWithLookup(paths, lookup, descriptor.SourceID); err != nil {
return nil, fmt.Errorf("verify prepared glossary: %w", err) return nil, fmt.Errorf("verify prepared glossary: %w", err)
} }
} }