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

@@ -263,6 +263,13 @@ func ResolvePreviousSessionArtifactWithCatalog(
m *manifest.Manifest,
source string,
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) {
artifactName, ok := PreviousSessionArtifactName(source)
if !ok {
@@ -275,15 +282,9 @@ func ResolvePreviousSessionArtifactWithCatalog(
if _, ok := catalog.Lookup(ConfiguredArtifactSourceID(artifactName)); !ok {
return ResolvedSessionArtifact{}, fmt.Errorf("unsupported previous-session artifact source %q", source)
}
if m == nil {
return ResolvedSessionArtifact{}, &SessionArtifactNotFoundError{ArtifactID: source}
}
var selected manifest.InputRecord
count := 0
for _, input := range m.Inputs {
if input.Kind != "previous_artifact" || input.Source != source {
continue
}
for _, input := range lookup.bySource[inputSourceKey{"previous_artifact", source}] {
candidate := filepath.Clean(ResolveSessionLocalPathForRead(paths, input.Path))
if strings.TrimSpace(candidate) == "" {
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
// current manifest record and verifies the canonical file's identity.
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)
if !ok {
if sourceID == artifactpolicy.SourceInputGlossary {
@@ -59,27 +64,13 @@ func ResolvePreparedInput(paths SessionPaths, m *manifest.Manifest, sourceID str
return PreparedInputIdentity{}, err
}
matching := make([]manifest.InputRecord, 0, 1)
if m != nil {
for _, record := range m.Inputs {
if strings.TrimSpace(record.Kind) == descriptor.ManifestKind {
matching = append(matching, record)
}
}
}
matching := lookup.byKind[descriptor.ManifestKind]
if len(matching) == 0 {
if m != nil {
for _, record := range m.Inputs {
recordedPath, pathErr := resolvePreparedManifestPath(paths, record.Path, rootPath)
if pathErr == nil && recordedPath == canonicalPath {
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,
)
}
}
if records := lookup.byPath[canonicalPath]; len(records) > 0 {
return PreparedInputIdentity{}, fmt.Errorf(
"prepared input source %q canonical path is recorded with manifest kind %q, want %q",
descriptor.SourceID, strings.TrimSpace(records[0].Kind), descriptor.ManifestKind,
)
}
return PreparedInputIdentity{}, &PreparedInputAbsentError{SourceID: descriptor.SourceID}
}

View File

@@ -240,3 +240,22 @@ func TestResolvePreparedGlossaryDocuments(t *testing.T) {
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)
}
}