Match prepared inputs with a linear record index
This commit is contained in:
@@ -139,7 +139,13 @@ types and upstream-invalid null scalar fields. Default tests remain offline.
|
||||
|
||||
Priority: P3
|
||||
|
||||
Status: Pending consideration
|
||||
Status: Complete
|
||||
|
||||
Resume comparison now indexes managed records by kind and audio destination,
|
||||
retaining multiplicity and consuming each matched entry once. Matching is
|
||||
expected O(E+M), excluding content processing. Stage/application tests cover
|
||||
reordering, duplicate/missing/extra records, checksum normalization, semantic
|
||||
snapshots, and each remote identity field; they passed with Go 1.27.4.
|
||||
|
||||
Affected stage: 8. This is an inherited algorithm extended by the glossary
|
||||
cutover, rather than a newly introduced regression.
|
||||
|
||||
@@ -238,12 +238,28 @@ func prepareSourceInputsMatch(paths artifacts.SessionPaths, expected []prepareEx
|
||||
}
|
||||
expectedCounts := make(map[string]int, len(managedKinds))
|
||||
recordCounts := make(map[string]int, len(managedKinds))
|
||||
type inputKey struct{ kind, destination string }
|
||||
type indexedRecord struct {
|
||||
record manifest.InputRecord
|
||||
count int
|
||||
}
|
||||
// Preparation gives audio unique destination names; other managed kinds
|
||||
// represent singleton inputs. Keep multiplicity so duplicates cannot hide.
|
||||
index := make(map[inputKey]indexedRecord, len(records))
|
||||
for _, input := range expected {
|
||||
expectedCounts[input.kind]++
|
||||
}
|
||||
for _, record := range records {
|
||||
if _, managed := managedKinds[record.Kind]; managed {
|
||||
recordCounts[record.Kind]++
|
||||
key := inputKey{kind: record.Kind}
|
||||
if record.Kind == "audio" {
|
||||
key.destination = filepath.Base(record.Path)
|
||||
}
|
||||
entry := index[key]
|
||||
entry.record = record
|
||||
entry.count++
|
||||
index[key] = entry
|
||||
}
|
||||
}
|
||||
for kind := range managedKinds {
|
||||
@@ -251,22 +267,13 @@ func prepareSourceInputsMatch(paths artifacts.SessionPaths, expected []prepareEx
|
||||
return false
|
||||
}
|
||||
}
|
||||
matched := make(map[int]struct{}, len(expected))
|
||||
for _, input := range expected {
|
||||
found := -1
|
||||
for index, record := range records {
|
||||
if _, used := matched[index]; used || !prepareInputRecordMatches(paths, input, record) {
|
||||
continue
|
||||
}
|
||||
if found != -1 {
|
||||
return false
|
||||
}
|
||||
found = index
|
||||
}
|
||||
if found == -1 {
|
||||
key := inputKey{input.kind, input.destinationName}
|
||||
entry := index[key]
|
||||
if entry.count != 1 || !prepareInputRecordMatches(paths, input, entry.record) {
|
||||
return false
|
||||
}
|
||||
matched[found] = struct{}{}
|
||||
delete(index, key)
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package stage
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
@@ -12,6 +13,81 @@ import (
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/manifest"
|
||||
)
|
||||
|
||||
func TestPrepareResumeIndexedRecords(t *testing.T) {
|
||||
const count = 128
|
||||
inputs := make([]prepareExpectedInput, count)
|
||||
records := make([]manifest.InputRecord, count)
|
||||
for i := range inputs {
|
||||
name := fmt.Sprintf("speaker-%03d.flac", i)
|
||||
inputs[i] = prepareExpectedInput{kind: "audio", destinationName: name, checksum: "abc"}
|
||||
records[count-1-i] = manifest.InputRecord{Kind: "audio", Path: filepath.Join("audio", name), Checksum: " ABC "}
|
||||
}
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
mutate func([]manifest.InputRecord) []manifest.InputRecord
|
||||
valid bool
|
||||
}{
|
||||
{"reordered", func(r []manifest.InputRecord) []manifest.InputRecord { return r }, true},
|
||||
{"missing", func(r []manifest.InputRecord) []manifest.InputRecord { return r[1:] }, false},
|
||||
{"extra managed", func(r []manifest.InputRecord) []manifest.InputRecord {
|
||||
return append(r, manifest.InputRecord{Kind: "party", Checksum: "abc"})
|
||||
}, false},
|
||||
{"duplicate destination", func(r []manifest.InputRecord) []manifest.InputRecord { r[0] = r[1]; return r }, false},
|
||||
{"changed checksum", func(r []manifest.InputRecord) []manifest.InputRecord { r[0].Checksum = "different"; return r }, false},
|
||||
{"unmanaged", func(r []manifest.InputRecord) []manifest.InputRecord {
|
||||
return append(r, manifest.InputRecord{Kind: "previous_session"})
|
||||
}, true},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
r := tc.mutate(append([]manifest.InputRecord(nil), records...))
|
||||
if got := prepareSourceInputsMatch(artifacts.SessionPaths{}, inputs, r); got != tc.valid {
|
||||
t.Fatalf("match = %t, want %t", got, tc.valid)
|
||||
}
|
||||
})
|
||||
}
|
||||
duplicated := append([]prepareExpectedInput(nil), inputs...)
|
||||
duplicated[1] = duplicated[0]
|
||||
if prepareSourceInputsMatch(artifacts.SessionPaths{}, duplicated, records) {
|
||||
t.Fatal("record reused")
|
||||
}
|
||||
|
||||
remote := prepareExpectedInput{kind: "audio", destinationName: "one.flac", s3Bucket: "archive", s3Key: "audio/one.flac", s3Size: 7, s3ETag: "etag"}
|
||||
record := manifest.InputRecord{Kind: "audio", Path: "audio/one.flac", Source: "s3", S3Bucket: " archive ", S3Key: " audio/one.flac ", S3Size: 7, S3ETag: " etag "}
|
||||
if !prepareSourceInputsMatch(artifacts.SessionPaths{}, []prepareExpectedInput{remote}, []manifest.InputRecord{record}) {
|
||||
t.Fatal("remote identity mismatch")
|
||||
}
|
||||
for _, field := range []string{"source", "bucket", "key", "size", "etag"} {
|
||||
t.Run(field, func(t *testing.T) {
|
||||
changed := record
|
||||
switch field {
|
||||
case "source":
|
||||
changed.Source = "local"
|
||||
case "bucket":
|
||||
changed.S3Bucket = "other"
|
||||
case "key":
|
||||
changed.S3Key = "other"
|
||||
case "size":
|
||||
changed.S3Size++
|
||||
case "etag":
|
||||
changed.S3ETag = "other"
|
||||
}
|
||||
if prepareSourceInputsMatch(artifacts.SessionPaths{}, []prepareExpectedInput{remote}, []manifest.InputRecord{changed}) {
|
||||
t.Fatal("changed identity accepted")
|
||||
}
|
||||
})
|
||||
}
|
||||
// Configuration snapshots retain semantic rather than bytewise comparison.
|
||||
path := filepath.Join(t.TempDir(), "session.yml")
|
||||
writeFile(t, path, "title: Example\ninputs: {setting: before}\n")
|
||||
digest, err := prepareConfigSnapshotChecksum([]byte("inputs: {setting: after}\ntitle: Example\n"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !prepareSourceInputsMatch(artifacts.SessionPaths{}, []prepareExpectedInput{{kind: "session_config", checksum: digest, configSnapshot: true}}, []manifest.InputRecord{{Kind: "session_config", Path: path}}) {
|
||||
t.Fatal("equivalent snapshot rejected")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrepareResumeValidatesCurrentLocalSourcesAndPreparedCopies(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
|
||||
Reference in New Issue
Block a user