Resolve and project shared glossary collections
This commit is contained in:
@@ -211,7 +211,14 @@ source bases. Update current tests that require a monolithic glossary file.
|
|||||||
|
|
||||||
## Stage 5. Resolve native vocabulary and derive the four domain collections
|
## Stage 5. Resolve native vocabulary and derive the four domain collections
|
||||||
|
|
||||||
Status: Pending
|
Status: Complete
|
||||||
|
|
||||||
|
Added a shared native ConfigKit resolution path and deterministic, validated
|
||||||
|
party, campaign, setting and spells documents. Focused glossary and configuration
|
||||||
|
tests cover imports, inheritance, whole-record upsert, native replacement,
|
||||||
|
independent session overrides, missing sources, and unchanged native values.
|
||||||
|
`go test ./internal/glossary ./internal/config ./internal/doccheck` passed.
|
||||||
|
Preparation and resume integration are performed in Stages 7 and 8.
|
||||||
|
|
||||||
Read roadmap “Glossary authoring and precedence” in full. Implement one production
|
Read roadmap “Glossary authoring and precedence” in full. Implement one production
|
||||||
resolution path shared by preparation and prepare-resume comparison. Configuration
|
resolution path shared by preparation and prepare-resume comparison. Configuration
|
||||||
|
|||||||
28
internal/config/glossary_resolve.go
Normal file
28
internal/config/glossary_resolve.go
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
package config
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"gitea.maximumdirect.net/eric/narratio/internal/glossary"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ResolveGlossaries derives standalone vocabulary without altering native configuration.
|
||||||
|
func ResolveGlossaries(cfg *Config) (glossary.Documents, error) {
|
||||||
|
if cfg == nil || cfg.Pipeline == nil || cfg.Session == nil || cfg.Party.Canonical == nil {
|
||||||
|
return nil, fmt.Errorf("pipeline, session and canonical party are required to resolve glossaries")
|
||||||
|
}
|
||||||
|
in := cfg.Session.Inputs
|
||||||
|
setting, err := cfg.Pipeline.ReferenceCatalog.SelectSetting(OptionalString(in.Setting))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
system, err := cfg.Pipeline.ReferenceCatalog.SelectSystem(OptionalString(in.System))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
characters := make([]glossary.Character, 0, len(cfg.Party.Canonical.Characters))
|
||||||
|
for _, c := range cfg.Party.Canonical.Characters {
|
||||||
|
characters = append(characters, glossary.Character{ID: c.ID, Name: c.Character.Name, Aliases: c.Character.Aliases})
|
||||||
|
}
|
||||||
|
return glossary.Resolve(glossary.Sources{CampaignFile: OptionalString(in.GlossaryFile), CampaignInline: in.Glossary, SettingFile: setting, SpellsFile: system}, characters)
|
||||||
|
}
|
||||||
109
internal/config/glossary_resolve_test.go
Normal file
109
internal/config/glossary_resolve_test.go
Normal file
@@ -0,0 +1,109 @@
|
|||||||
|
package config
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestResolveGlossariesThroughNativeAcquisition(t *testing.T) {
|
||||||
|
dir := t.TempDir()
|
||||||
|
pipeline := writePipelineFixture(t, dir, "pipeline.yml", testTranscriptionConfig+`config:
|
||||||
|
imports:
|
||||||
|
- path: catalog/catalog.yml
|
||||||
|
`)
|
||||||
|
writePipelineFixture(t, dir, "catalog/catalog.yml", `reference_catalog:
|
||||||
|
settings: {coast: setting.yml}
|
||||||
|
systems: {magic: spells.yml}
|
||||||
|
`)
|
||||||
|
writePipelineFixture(t, dir, "catalog/setting.yml", "setting: {entries: [{id: coast, name: Coast, category: place}]}\n")
|
||||||
|
writePipelineFixture(t, dir, "catalog/spells.yml", "spells: {entries: [{id: glow, name: Glow, category: spell}]}\n")
|
||||||
|
campaign := writePipelineFixture(t, dir, "campaign.yml", `campaign_id: campaign
|
||||||
|
inputs:
|
||||||
|
speakers_file: speakers.yml
|
||||||
|
autocorrect_file: autocorrect.yml
|
||||||
|
party_file: party.yml
|
||||||
|
setting: coast
|
||||||
|
system: magic
|
||||||
|
glossary_file: vocab.yml
|
||||||
|
glossary:
|
||||||
|
entries: [{id: watch, name: Silver Watch, category: organization}]
|
||||||
|
`)
|
||||||
|
writePipelineFixture(t, dir, "party.yml", canonicalPartyFixture)
|
||||||
|
writePipelineFixture(t, dir, "vocab.yml", `config:
|
||||||
|
imports:
|
||||||
|
- path: vocabulary/shared.yml
|
||||||
|
campaign:
|
||||||
|
instruction: Keep names.
|
||||||
|
entries: [{id: watch, name: Old Watch, category: organization, aliases: [Old]}]
|
||||||
|
`)
|
||||||
|
writePipelineFixture(t, dir, "vocabulary/shared.yml", "campaign:\n entries: [{id: harbor, name: Harbor, category: place}]\n")
|
||||||
|
session := writePipelineFixture(t, dir, "session.yml", "session_id: session\ninputs: {audio_dir: audio}\n")
|
||||||
|
cfg, err := LoadWithSessionOptions(pipeline, campaign, session, SessionLoadOptions{})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
before, err := RenderSessionDocument(cfg, "session")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
docs, err := ResolveGlossaries(cfg)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
for _, item := range []struct{ name, text string }{{"campaign", "Silver Watch"}, {"campaign", "Harbor"}, {"setting", "Coast"}, {"spells", "Glow"}} {
|
||||||
|
if !strings.Contains(string(docs[item.name]), item.text) {
|
||||||
|
t.Fatalf("%s: %s", item.name, docs[item.name])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if strings.Contains(string(docs["campaign"]), "Old Watch") || strings.Contains(string(docs["campaign"]), "aliases:") {
|
||||||
|
t.Fatalf("not a whole-record replacement: %s", docs["campaign"])
|
||||||
|
}
|
||||||
|
after, err := RenderSessionDocument(cfg, "session")
|
||||||
|
if err != nil || !bytes.Equal(before, after) {
|
||||||
|
t.Fatalf("configuration changed: %v", err)
|
||||||
|
}
|
||||||
|
again, err := ResolveGlossaries(cfg)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
for k, v := range docs {
|
||||||
|
if !bytes.Equal(v, again[k]) {
|
||||||
|
t.Fatalf("nondeterministic %s", k)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
writePipelineFixture(t, dir, "replacement.yml", "campaign: {entries: [{id: watch, name: Alternate, category: organization}, {id: inn, name: Inn, category: place}]}\n")
|
||||||
|
for _, tc := range []struct{ name, body, want, absent string }{
|
||||||
|
{"inline override", "glossary: {instruction: Session context.}", "Old Watch", "Silver Watch"},
|
||||||
|
{"file override", "glossary_file: replacement.yml", "Silver Watch", "Old Watch"},
|
||||||
|
} {
|
||||||
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
sessionPath := writePipelineFixture(t, dir, "session.yml", "session_id: session\ninputs:\n audio_dir: audio\n "+tc.body+"\n")
|
||||||
|
selected, err := LoadWithSessionOptions(pipeline, campaign, sessionPath, SessionLoadOptions{})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
resolved, err := ResolveGlossaries(selected)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if !strings.Contains(string(resolved["campaign"]), tc.want) || strings.Contains(string(resolved["campaign"]), tc.absent) {
|
||||||
|
t.Fatalf("campaign: %s", resolved["campaign"])
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
writePipelineFixture(t, dir, "vocab.yml", "config:\n sequences:\n - destination: [campaign, entries]\n mode: replace\ncampaign:\n instruction: Keep names.\n entries: [{id: watch, name: Old Watch, category: organization}]\n")
|
||||||
|
clearedSession := writePipelineFixture(t, dir, "session.yml", "session_id: session\ninputs:\n audio_dir: audio\n glossary: {entries: []}\n")
|
||||||
|
cleared, err := LoadWithSessionOptions(pipeline, campaign, clearedSession, SessionLoadOptions{})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
clearedDocs, err := ResolveGlossaries(cleared)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if !strings.Contains(string(clearedDocs["campaign"]), "entries: []") || !strings.Contains(string(clearedDocs["campaign"]), "Keep names") {
|
||||||
|
t.Fatalf("clearing: %s", clearedDocs["campaign"])
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
107
internal/glossary/resolve.go
Normal file
107
internal/glossary/resolve.go
Normal file
@@ -0,0 +1,107 @@
|
|||||||
|
package glossary
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"sort"
|
||||||
|
|
||||||
|
configkit "go.maximumdirect.net/configkit"
|
||||||
|
"gopkg.in/yaml.v3"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Sources identifies the selected, already source-resolved vocabulary files.
|
||||||
|
type Sources struct {
|
||||||
|
CampaignFile string
|
||||||
|
CampaignInline map[string]any
|
||||||
|
SettingFile string
|
||||||
|
SpellsFile string
|
||||||
|
}
|
||||||
|
|
||||||
|
// Character contains the roster fields needed for a party vocabulary entry.
|
||||||
|
type Character struct {
|
||||||
|
ID string
|
||||||
|
Name string
|
||||||
|
Aliases []string
|
||||||
|
}
|
||||||
|
|
||||||
|
// Documents holds the four completed standalone named collections.
|
||||||
|
type Documents map[string][]byte
|
||||||
|
|
||||||
|
// Resolve builds independent, validated vocabulary snapshots using native composition.
|
||||||
|
func Resolve(sources Sources, characters []Character) (Documents, error) {
|
||||||
|
party := make([]any, 0, len(characters))
|
||||||
|
ordered := append([]Character(nil), characters...)
|
||||||
|
sort.Slice(ordered, func(i, j int) bool { return ordered[i].ID < ordered[j].ID })
|
||||||
|
for _, c := range ordered {
|
||||||
|
entry := map[string]any{"id": c.ID, "name": c.Name, "category": "person"}
|
||||||
|
if len(c.Aliases) > 0 {
|
||||||
|
entry["aliases"] = c.Aliases
|
||||||
|
}
|
||||||
|
party = append(party, entry)
|
||||||
|
}
|
||||||
|
collections := map[string]any{"party": map[string]any{"entries": party}}
|
||||||
|
for _, item := range []struct {
|
||||||
|
name, path string
|
||||||
|
inline map[string]any
|
||||||
|
}{
|
||||||
|
{"campaign", sources.CampaignFile, sources.CampaignInline},
|
||||||
|
{"setting", sources.SettingFile, nil},
|
||||||
|
{"spells", sources.SpellsFile, nil},
|
||||||
|
} {
|
||||||
|
value, err := resolveCollection(item.name, item.path, item.inline)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
collections[item.name] = value
|
||||||
|
}
|
||||||
|
result := make(Documents, len(collections))
|
||||||
|
for _, name := range []string{"party", "campaign", "setting", "spells"} {
|
||||||
|
doc := map[string]any{name: collections[name]}
|
||||||
|
if err := ValidateDocument(doc); err != nil {
|
||||||
|
return nil, fmt.Errorf("%s vocabulary: %w", name, err)
|
||||||
|
}
|
||||||
|
data, err := yaml.Marshal(doc)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("serialize %s vocabulary: %w", name, err)
|
||||||
|
}
|
||||||
|
result[name] = data
|
||||||
|
}
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func resolveCollection(name, path string, inline map[string]any) (any, error) {
|
||||||
|
if path == "" && inline == nil {
|
||||||
|
return map[string]any{"entries": []any{}}, nil
|
||||||
|
}
|
||||||
|
definition := configkit.Definition[map[string]any]{SequenceIdentities: []configkit.SequenceIdentity{
|
||||||
|
configkit.SequenceIdentityMatching(configkit.SequenceSelector{configkit.SequenceAnyMappingKey(), configkit.SequenceLiteralKey("entries")}, "id"),
|
||||||
|
}}
|
||||||
|
var underlays []configkit.Underlay
|
||||||
|
if path == "" && inline != nil {
|
||||||
|
empty := configkit.Mapping("empty vocabulary", map[string]any{name: map[string]any{"entries": []any{}}})
|
||||||
|
underlays = append(underlays, configkit.Underlay{Inputs: []configkit.Input{{Source: empty}}})
|
||||||
|
}
|
||||||
|
if path != "" {
|
||||||
|
file := configkit.Input{Source: configkit.File(path), Imports: true}
|
||||||
|
loaded, err := configkit.LoadLayered(file, configkit.LayeredOptions[map[string]any]{Definition: definition})
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("load %s vocabulary %q: %w", name, path, err)
|
||||||
|
}
|
||||||
|
if _, ok := loaded.Config[name]; !ok {
|
||||||
|
return nil, fmt.Errorf("%s vocabulary %q does not define collection %q", name, path, name)
|
||||||
|
}
|
||||||
|
if inline == nil {
|
||||||
|
return loaded.Config[name], nil
|
||||||
|
}
|
||||||
|
underlays = []configkit.Underlay{{Inputs: []configkit.Input{file}}}
|
||||||
|
}
|
||||||
|
main := configkit.Input{Source: configkit.Mapping("inline vocabulary", inline), Destination: configkit.Keys(name)}
|
||||||
|
loaded, err := configkit.LoadLayered(main, configkit.LayeredOptions[map[string]any]{Definition: definition, Underlays: underlays})
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("compose %s vocabulary: %w", name, err)
|
||||||
|
}
|
||||||
|
value, ok := loaded.Config[name]
|
||||||
|
if !ok {
|
||||||
|
return nil, fmt.Errorf("%s vocabulary is missing", name)
|
||||||
|
}
|
||||||
|
return value, nil
|
||||||
|
}
|
||||||
83
internal/glossary/resolve_test.go
Normal file
83
internal/glossary/resolve_test.go
Normal file
@@ -0,0 +1,83 @@
|
|||||||
|
package glossary
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"gopkg.in/yaml.v3"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestResolveComposesAndProjectsVocabulary(t *testing.T) {
|
||||||
|
dir := t.TempDir()
|
||||||
|
path := filepath.Join(dir, "campaign.yml")
|
||||||
|
data := `campaign:
|
||||||
|
instruction: Use established names.
|
||||||
|
entries:
|
||||||
|
- {id: watch, name: Old Watch, category: organization, aliases: [Old]}
|
||||||
|
- {id: harbor, name: North Harbor, category: place, source_note: synthetic}
|
||||||
|
`
|
||||||
|
if err := os.WriteFile(path, []byte(data), 0600); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
inline := map[string]any{"entries": []any{map[string]any{"id": "watch", "name": "Silver Watch", "category": "organization"}}}
|
||||||
|
docs, err := Resolve(Sources{CampaignFile: path, CampaignInline: inline}, []Character{{ID: "z", Name: "Zed"}, {ID: "a", Name: "Ada", Aliases: []string{"A"}}})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
var campaign map[string]map[string]any
|
||||||
|
if err := yaml.Unmarshal(docs["campaign"], &campaign); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
entries := campaign["campaign"]["entries"].([]any)
|
||||||
|
first := entries[0].(map[string]any)
|
||||||
|
if first["name"] != "Silver Watch" || first["aliases"] != nil || !strings.Contains(string(docs["campaign"]), "source_note: synthetic") {
|
||||||
|
t.Fatalf("campaign: %s", docs["campaign"])
|
||||||
|
}
|
||||||
|
if !strings.Contains(string(docs["campaign"]), "instruction: Use established names.") {
|
||||||
|
t.Fatalf("instruction: %s", docs["campaign"])
|
||||||
|
}
|
||||||
|
if strings.Index(string(docs["party"]), "id: a") > strings.Index(string(docs["party"]), "id: z") {
|
||||||
|
t.Fatalf("party order: %s", docs["party"])
|
||||||
|
}
|
||||||
|
for _, name := range []string{"setting", "spells"} {
|
||||||
|
if !strings.Contains(string(docs[name]), "entries: []") {
|
||||||
|
t.Fatalf("empty %s: %s", name, docs[name])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if err := os.WriteFile(path, []byte("config:\n sequences:\n - destination: [campaign, entries]\n mode: replace\n"+data), 0600); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
docs, err = Resolve(Sources{CampaignFile: path, CampaignInline: map[string]any{"entries": []any{}}}, nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if !strings.Contains(string(docs["campaign"]), "entries: []") || !strings.Contains(string(docs["campaign"]), "instruction:") {
|
||||||
|
t.Fatalf("replacement: %s", docs["campaign"])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestResolveHandlesInheritancePartialAndFailures(t *testing.T) {
|
||||||
|
dir := t.TempDir()
|
||||||
|
path := filepath.Join(dir, "setting.yml")
|
||||||
|
if err := os.WriteFile(path, []byte("common:\n entries: [{id: tower, name: Tower, category: place}]\nsetting:\n extends: common\n instruction: ''\n"), 0600); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
docs, err := Resolve(Sources{SettingFile: path, CampaignInline: map[string]any{"instruction": "Only instruction"}}, nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if !strings.Contains(string(docs["setting"]), "Tower") || !strings.Contains(string(docs["setting"]), "instruction: \"\"") {
|
||||||
|
t.Fatalf("inherited: %s", docs["setting"])
|
||||||
|
}
|
||||||
|
if !strings.Contains(string(docs["campaign"]), "entries: []") {
|
||||||
|
t.Fatalf("partial: %s", docs["campaign"])
|
||||||
|
}
|
||||||
|
if _, err := Resolve(Sources{SpellsFile: path}, nil); err == nil || !strings.Contains(err.Error(), "spells") {
|
||||||
|
t.Fatalf("missing selection: %v", err)
|
||||||
|
}
|
||||||
|
if _, err := Resolve(Sources{CampaignFile: filepath.Join(dir, "missing.yml")}, nil); err == nil {
|
||||||
|
t.Fatal("missing file accepted")
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user