Make preparation reuse aware of glossary contents
This commit is contained in:
@@ -5,13 +5,16 @@ import (
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/artifactpolicy"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/artifacts"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/config"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/manifest"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
const prepareResumeChangedReason = "prepared inputs or their current sources changed; rerun prepare"
|
||||
@@ -20,6 +23,7 @@ type prepareExpectedInput struct {
|
||||
kind string
|
||||
destinationName string
|
||||
checksum string
|
||||
configSnapshot bool
|
||||
s3Bucket string
|
||||
s3Key string
|
||||
s3Size int64
|
||||
@@ -47,20 +51,26 @@ func (prepareStage) ValidateResume(ctx context.Context, env *Env, m *manifest.Ma
|
||||
}
|
||||
return NonResumable(prepareResumeChangedReason), nil
|
||||
}
|
||||
if !preparedInputRecordsCurrent(env, m.Inputs) || !prepareSourceInputsMatch(current, m.Inputs) {
|
||||
paths := sessionPathsForEnv(env, m.SessionID)
|
||||
for _, source := range []string{artifactpolicy.SourceInputGlossaryParty, artifactpolicy.SourceInputGlossaryCampaign, artifactpolicy.SourceInputGlossarySetting, artifactpolicy.SourceInputGlossarySpells} {
|
||||
if _, err := artifacts.ResolvePreparedInput(paths, m, source); err != nil {
|
||||
return NonResumable(prepareResumeChangedReason), nil
|
||||
}
|
||||
}
|
||||
if !preparedInputRecordsCurrent(env, paths, m.Inputs) || !prepareSourceInputsMatch(paths, current, m.Inputs) {
|
||||
return NonResumable(prepareResumeChangedReason), nil
|
||||
}
|
||||
return Resumable(), nil
|
||||
}
|
||||
|
||||
func preparedInputRecordsCurrent(env *Env, records []manifest.InputRecord) bool {
|
||||
func preparedInputRecordsCurrent(env *Env, paths artifacts.SessionPaths, records []manifest.InputRecord) bool {
|
||||
for _, record := range records {
|
||||
path := strings.TrimSpace(record.Path)
|
||||
expected := strings.TrimSpace(record.Checksum)
|
||||
if path == "" || expected == "" {
|
||||
return false
|
||||
}
|
||||
actual, err := env.ArtifactStore.Checksum(path)
|
||||
actual, err := env.ArtifactStore.Checksum(artifacts.ResolveSessionLocalPathForRead(paths, path))
|
||||
if err != nil || !strings.EqualFold(actual, expected) {
|
||||
return false
|
||||
}
|
||||
@@ -79,7 +89,15 @@ func currentPrepareSourceInputs(ctx context.Context, env *Env, m *manifest.Manif
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
expected = append(expected, prepareExpectedInput{kind: "campaign_config", checksum: checksumPrepareBytes(campaign)}, prepareExpectedInput{kind: "session_config", checksum: checksumPrepareBytes(session)})
|
||||
campaignChecksum, err := prepareConfigSnapshotChecksum(campaign)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
sessionChecksum, err := prepareConfigSnapshotChecksum(session)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
expected = append(expected, prepareExpectedInput{kind: "campaign_config", checksum: campaignChecksum, configSnapshot: true}, prepareExpectedInput{kind: "session_config", checksum: sessionChecksum, configSnapshot: true})
|
||||
appendFile := func(kind string, input config.ResolvedInputFile, fallback string) error {
|
||||
resolved := stableInputSource(input, fallback, cfg.SessionPath)
|
||||
sourcePath, err := resolveConfigRelativePath(resolved)
|
||||
@@ -100,13 +118,20 @@ func currentPrepareSourceInputs(ctx context.Context, env *Env, m *manifest.Manif
|
||||
if err := appendFile("autocorrect", cfg.StableInputs.AutocorrectFile, cfg.Session.Inputs.AutocorrectFile); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
glossary, ok := artifactpolicy.DescribePreparedInputSource(artifactpolicy.SourceInputGlossary)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("glossary prepared-input descriptor is unavailable")
|
||||
}
|
||||
if err := appendFile(glossary.ManifestKind, cfg.StableInputs.GlossaryFile, config.OptionalString(cfg.Session.Inputs.GlossaryFile)); err != nil {
|
||||
documents, err := config.ResolveGlossaries(cfg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, item := range []struct{ name, source string }{
|
||||
{"party", artifactpolicy.SourceInputGlossaryParty}, {"campaign", artifactpolicy.SourceInputGlossaryCampaign},
|
||||
{"setting", artifactpolicy.SourceInputGlossarySetting}, {"spells", artifactpolicy.SourceInputGlossarySpells},
|
||||
} {
|
||||
descriptor, ok := artifactpolicy.DescribePreparedInputSource(item.source)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("glossary descriptor %q unavailable", item.source)
|
||||
}
|
||||
expected = append(expected, prepareExpectedInput{kind: descriptor.ManifestKind, checksum: checksumPrepareBytes(documents[item.name])})
|
||||
}
|
||||
spellCatalog, ok := artifactpolicy.DescribePreparedInputSource(artifactpolicy.SourceInputSpellCatalog)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("spell catalog prepared-input descriptor is unavailable")
|
||||
@@ -201,13 +226,14 @@ func currentPrepareAudioSourceInputs(ctx context.Context, env *Env, m *manifest.
|
||||
return expected, nil
|
||||
}
|
||||
|
||||
func prepareSourceInputsMatch(expected []prepareExpectedInput, records []manifest.InputRecord) bool {
|
||||
func prepareSourceInputsMatch(paths artifacts.SessionPaths, expected []prepareExpectedInput, records []manifest.InputRecord) bool {
|
||||
if len(expected) == 0 {
|
||||
return false
|
||||
}
|
||||
managedKinds := map[string]struct{}{
|
||||
"campaign_config": {}, "session_config": {},
|
||||
"audio": {}, "speakers": {}, "autocorrect": {}, "glossary": {},
|
||||
"glossary_party": {}, "glossary_campaign": {}, "glossary_setting": {}, "glossary_spells": {},
|
||||
"players": {}, "party": {}, "spell_catalog": {},
|
||||
}
|
||||
expectedCounts := make(map[string]int, len(managedKinds))
|
||||
@@ -229,7 +255,7 @@ func prepareSourceInputsMatch(expected []prepareExpectedInput, records []manifes
|
||||
for _, input := range expected {
|
||||
found := -1
|
||||
for index, record := range records {
|
||||
if _, used := matched[index]; used || !prepareInputRecordMatches(input, record) {
|
||||
if _, used := matched[index]; used || !prepareInputRecordMatches(paths, input, record) {
|
||||
continue
|
||||
}
|
||||
if found != -1 {
|
||||
@@ -245,7 +271,7 @@ func prepareSourceInputsMatch(expected []prepareExpectedInput, records []manifes
|
||||
return true
|
||||
}
|
||||
|
||||
func prepareInputRecordMatches(expected prepareExpectedInput, record manifest.InputRecord) bool {
|
||||
func prepareInputRecordMatches(paths artifacts.SessionPaths, expected prepareExpectedInput, record manifest.InputRecord) bool {
|
||||
if record.Kind != expected.kind {
|
||||
return false
|
||||
}
|
||||
@@ -257,9 +283,35 @@ func prepareInputRecordMatches(expected prepareExpectedInput, record manifest.In
|
||||
strings.TrimSpace(record.S3Key) == expected.s3Key && record.S3Size == expected.s3Size &&
|
||||
strings.TrimSpace(record.S3ETag) == expected.s3ETag
|
||||
}
|
||||
if expected.configSnapshot {
|
||||
raw, err := os.ReadFile(artifacts.ResolveSessionLocalPathForRead(paths, record.Path))
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
checksum, err := prepareConfigSnapshotChecksum(raw)
|
||||
return err == nil && checksum == expected.checksum
|
||||
}
|
||||
return strings.EqualFold(strings.TrimSpace(record.Checksum), expected.checksum)
|
||||
}
|
||||
|
||||
// Glossary authoring is compared through its resolved documents rather than source layout.
|
||||
func prepareConfigSnapshotChecksum(data []byte) (string, error) {
|
||||
var document map[string]any
|
||||
if err := yaml.Unmarshal(data, &document); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if inputs, ok := document["inputs"].(map[string]any); ok {
|
||||
for _, key := range []string{"setting", "system", "glossary_file", "glossary"} {
|
||||
delete(inputs, key)
|
||||
}
|
||||
}
|
||||
normalized, err := yaml.Marshal(document)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return checksumPrepareBytes(normalized), nil
|
||||
}
|
||||
|
||||
func checksumPrepareBytes(data []byte) string {
|
||||
digest := sha256.Sum256(data)
|
||||
return hex.EncodeToString(digest[:])
|
||||
|
||||
@@ -43,7 +43,7 @@ func TestPrepareResumeValidatesCurrentLocalSourcesAndPreparedCopies(t *testing.T
|
||||
name: "prepared copy changed",
|
||||
mutate: func(t *testing.T, env *Env, _ string) {
|
||||
paths := env.ArtifactStore.SessionPathsFor(env.Config.Session.Campaign, env.Config.Session.SessionID)
|
||||
writeFile(t, filepath.Join(paths.InputsDir, "glossary.yml"), "tampered: true\n")
|
||||
writeFile(t, filepath.Join(paths.InputsDir, "glossaries", "campaign.yml"), "tampered: true\n")
|
||||
},
|
||||
},
|
||||
}
|
||||
@@ -162,3 +162,54 @@ func TestPrepareResumeMissingPreparedFileReruns(t *testing.T) {
|
||||
}
|
||||
assertPrepareResume(t, env, m, false)
|
||||
}
|
||||
|
||||
func TestPrepareResumeTracksEffectiveGlossaryVocabulary(t *testing.T) {
|
||||
env, m := setupPrepareEnv(t)
|
||||
root := filepath.Dir(env.Config.SessionPath)
|
||||
writeFile(t, filepath.Join(root, "audio", "one.flac"), "audio")
|
||||
source := filepath.Join(root, "vocabulary.yml")
|
||||
fragment := filepath.Join(root, "fragment.yml")
|
||||
writeFile(t, source, "config:\n imports:\n - path: fragment.yml\ncampaign: {instruction: Names}\n")
|
||||
writeFile(t, fragment, "campaign: {entries: [{id: watch, name: Watch, category: organization}]}\n")
|
||||
env.Config.Session.Inputs.GlossaryFile = &source
|
||||
if _, err := (prepareStage{}).Run(context.Background(), env, m); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
assertPrepareResume(t, env, m, true)
|
||||
relocated := filepath.Join(root, "relocated.yml")
|
||||
writeFile(t, relocated, "campaign:\n instruction: Names\n entries:\n - {name: Watch, id: watch, category: organization}\n")
|
||||
env.Config.Session.Inputs.GlossaryFile = &relocated
|
||||
assertPrepareResume(t, env, m, true)
|
||||
env.Config.Session.Inputs.GlossaryFile = &source
|
||||
writeFile(t, fragment, "campaign: {entries: [{id: watch, name: Changed, category: organization}]}\n")
|
||||
assertPrepareResume(t, env, m, false)
|
||||
}
|
||||
|
||||
func TestPrepareResumeRequiresAllGlossaryRecords(t *testing.T) {
|
||||
env, m, _ := prepareResumeLocalFixture(t)
|
||||
for i, record := range m.Inputs {
|
||||
if record.Kind == "glossary_spells" {
|
||||
m.Inputs = append(m.Inputs[:i], m.Inputs[i+1:]...)
|
||||
break
|
||||
}
|
||||
}
|
||||
m.Inputs = append(m.Inputs, manifest.InputRecord{Kind: "glossary", Path: "inputs/glossary.yml", Checksum: "obsolete"})
|
||||
assertPrepareResume(t, env, m, false)
|
||||
}
|
||||
|
||||
func TestPrepareResumeChecksSelectedCatalogContent(t *testing.T) {
|
||||
env, m := setupPrepareEnv(t)
|
||||
root := filepath.Dir(env.Config.SessionPath)
|
||||
writeFile(t, filepath.Join(root, "audio", "one.flac"), "audio")
|
||||
one, two := filepath.Join(root, "one.yml"), filepath.Join(root, "two.yml")
|
||||
writeFile(t, one, "setting: {entries: [{id: place, name: One, category: place}]}\n")
|
||||
writeFile(t, two, "setting: {entries: [{id: place, name: Two, category: place}]}\n")
|
||||
env.Config.Pipeline.ReferenceCatalog.Settings = map[string]string{"one": one, "two": two}
|
||||
env.Config.Session.Inputs.Setting = new("one")
|
||||
if _, err := (prepareStage{}).Run(context.Background(), env, m); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
assertPrepareResume(t, env, m, true)
|
||||
env.Config.Session.Inputs.Setting = new("two")
|
||||
assertPrepareResume(t, env, m, false)
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
prepareSemanticConfigVersion = 3
|
||||
prepareSemanticConfigVersion = 4
|
||||
transcribeSemanticConfigVersion = 1
|
||||
mergeSemanticConfigVersion = 1
|
||||
)
|
||||
@@ -152,7 +152,8 @@ func prepareStableInputSemantics(cfg *config.Config) []prepareStableInputSelecti
|
||||
return []prepareStableInputSelection{
|
||||
prepareStableInput("speakers", stable.SpeakersFile, cfg.Session.Inputs.SpeakersFile),
|
||||
prepareStableInput("autocorrect", stable.AutocorrectFile, cfg.Session.Inputs.AutocorrectFile),
|
||||
prepareStableInput("glossary", stable.GlossaryFile, config.OptionalString(cfg.Session.Inputs.GlossaryFile)),
|
||||
{Kind: "glossary_party", Present: true}, {Kind: "glossary_campaign", Present: true},
|
||||
{Kind: "glossary_setting", Present: true}, {Kind: "glossary_spells", Present: true},
|
||||
prepareStableInput("party", stable.PartyFile, ""),
|
||||
prepareStableInput("spell_catalog", stable.SpellCatalogFile, cfg.Session.Inputs.SpellCatalogFile),
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user