Render imported party rosters as canonical inputs
This commit is contained in:
@@ -200,7 +200,7 @@ mismatches, imported session-init template paths, absence versus selected-source
|
||||
failure, and remote snapshots independent of temporary directories. Add no
|
||||
secondary overlay flags.
|
||||
|
||||
## Stage 5: Resolve imported party definitions and canonical roster output
|
||||
## Stage 5: Resolve imported party definitions and canonical roster output [x]
|
||||
|
||||
Read `party.go`, `party_resolution.go`, `artifact_families.go`, the party
|
||||
integration contract, and focused party/family tests.
|
||||
|
||||
@@ -314,7 +314,11 @@ characters:
|
||||
if err != nil {
|
||||
t.Fatalf("ReadFile(prepared party) error = %v", err)
|
||||
}
|
||||
if string(contents) != string(partyBytes) || partyPath == cfg.Party.Source.Path {
|
||||
wantParty, err := partyDocument.YAML()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if string(contents) != string(wantParty) || partyPath == cfg.Party.Source.Path {
|
||||
t.Fatalf("prepared party path=%q contents=%q source=%q", partyPath, contents, cfg.Party.Source.Path)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"math"
|
||||
"sort"
|
||||
@@ -20,11 +19,8 @@ const (
|
||||
PlayersSchemaVersion = "narratio.players.v1"
|
||||
)
|
||||
|
||||
// CanonicalParty is the normalized domain value for narratio.party.v1. Raw is
|
||||
// retained independently so later preparation can copy the authored party file
|
||||
// byte-for-byte rather than serializing normalized values.
|
||||
// CanonicalParty is the normalized domain value for narratio.party.v1.
|
||||
type CanonicalParty struct {
|
||||
Raw []byte
|
||||
Characters []PartyCharacter
|
||||
}
|
||||
|
||||
@@ -70,13 +66,13 @@ type partyPlayer struct {
|
||||
|
||||
type partyCharacterDetails struct {
|
||||
Name string `yaml:"name"`
|
||||
Aliases []string `yaml:"alias"`
|
||||
Aliases []string `yaml:"alias,omitempty"`
|
||||
Classes []partyClass `yaml:"classes"`
|
||||
}
|
||||
|
||||
type partyClass struct {
|
||||
Name string `yaml:"name"`
|
||||
Level *int `yaml:"level"`
|
||||
Level *int `yaml:"level,omitempty"`
|
||||
}
|
||||
|
||||
type partyDomainError struct {
|
||||
@@ -89,6 +85,15 @@ func ParseParty(data []byte) (*CanonicalParty, error) {
|
||||
if int64(len(data)) > maxConfigDocumentBytes {
|
||||
return nil, fmt.Errorf("parse party config: party document exceeds %d-byte limit", maxConfigDocumentBytes)
|
||||
}
|
||||
return loadParty(configkit.Bytes("party.yml", data), false)
|
||||
}
|
||||
|
||||
// LoadParty loads an authored party file and its native imports.
|
||||
func LoadParty(path string) (*CanonicalParty, error) {
|
||||
return loadParty(configkit.File(path), true)
|
||||
}
|
||||
|
||||
func loadParty(source configkit.Source, imports bool) (*CanonicalParty, error) {
|
||||
definition := configkit.Definition[partyDocument]{
|
||||
Version: &configkit.VersionRule{
|
||||
Path: configkit.Keys("schema_version"),
|
||||
@@ -96,11 +101,11 @@ func ParseParty(data []byte) (*CanonicalParty, error) {
|
||||
},
|
||||
Validate: validatePartyDocument,
|
||||
}
|
||||
result, err := configkit.Load(configkit.Bytes("party.yml", data), configkit.Options[partyDocument]{Definition: definition})
|
||||
result, err := configkit.Load(source, configkit.Options[partyDocument]{Imports: imports, Definition: definition})
|
||||
if err != nil {
|
||||
return nil, describeConfigKitLoadError("party", err)
|
||||
}
|
||||
return canonicalPartyFromResult(result, data), nil
|
||||
return canonicalPartyFromResult(result), nil
|
||||
}
|
||||
|
||||
func validatePartyVersion(version configkit.Version, validation *configkit.Validation) error {
|
||||
@@ -200,21 +205,37 @@ func partyNameAmbiguous(name string, existing []string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func canonicalPartyFromResult(result *configkit.Result[partyDocument], raw []byte) *CanonicalParty {
|
||||
func canonicalPartyFromResult(result *configkit.Result[partyDocument]) *CanonicalParty {
|
||||
type orderedCharacter struct {
|
||||
id string
|
||||
location configkit.Location
|
||||
source int
|
||||
}
|
||||
metadata := result.Metadata()
|
||||
sourceOrder := make(map[string]int)
|
||||
for index, source := range result.Sources() {
|
||||
sourceOrder[source.ID] = index
|
||||
}
|
||||
ordered := make([]orderedCharacter, 0, len(result.Config.Characters))
|
||||
for id := range result.Config.Characters {
|
||||
entry := orderedCharacter{id: id, location: configkit.Location{Line: math.MaxInt}}
|
||||
entry := orderedCharacter{id: id, source: math.MaxInt, location: configkit.Location{Line: math.MaxInt, Column: math.MaxInt}}
|
||||
if info, ok := metadata.Lookup(configkit.Keys("characters", id)); ok && len(info.Origin.Locations) != 0 {
|
||||
entry.location = info.Origin.Locations[0]
|
||||
for _, inheritance := range info.Origin.Inheritance {
|
||||
if inheritance.To.String() == configkit.Keys("characters", id).String() && len(inheritance.Locations) != 0 {
|
||||
entry.location = inheritance.Locations[0]
|
||||
}
|
||||
}
|
||||
if index, ok := sourceOrder[entry.location.SourceID]; ok {
|
||||
entry.source = index
|
||||
}
|
||||
}
|
||||
ordered = append(ordered, entry)
|
||||
}
|
||||
sort.Slice(ordered, func(left, right int) bool {
|
||||
if ordered[left].source != ordered[right].source {
|
||||
return ordered[left].source < ordered[right].source
|
||||
}
|
||||
if ordered[left].location.Line != ordered[right].location.Line {
|
||||
return ordered[left].location.Line < ordered[right].location.Line
|
||||
}
|
||||
@@ -223,7 +244,7 @@ func canonicalPartyFromResult(result *configkit.Result[partyDocument], raw []byt
|
||||
}
|
||||
return ordered[left].id < ordered[right].id
|
||||
})
|
||||
party := &CanonicalParty{Raw: bytes.Clone(raw), Characters: make([]PartyCharacter, 0, len(ordered))}
|
||||
party := &CanonicalParty{Characters: make([]PartyCharacter, 0, len(ordered))}
|
||||
for _, orderedCharacter := range ordered {
|
||||
input := result.Config.Characters[orderedCharacter.id]
|
||||
classes := make([]PartyClass, len(input.Character.Classes))
|
||||
@@ -241,6 +262,34 @@ func canonicalPartyFromResult(result *configkit.Result[partyDocument], raw []byt
|
||||
return party
|
||||
}
|
||||
|
||||
// YAML renders the effective roster as a standalone canonical party document.
|
||||
func (p *CanonicalParty) YAML() ([]byte, error) {
|
||||
if p == nil {
|
||||
return nil, fmt.Errorf("canonical party is required")
|
||||
}
|
||||
characters := &yaml.Node{Kind: yaml.MappingNode, Tag: "!!map"}
|
||||
for _, entry := range p.Characters {
|
||||
value := partyCharacter{Player: partyPlayer{Name: entry.Player.Name}, Character: partyCharacterDetails{Name: entry.Character.Name, Aliases: entry.Character.Aliases}}
|
||||
for _, class := range entry.Character.Classes {
|
||||
value.Character.Classes = append(value.Character.Classes, partyClass{Name: class.Name, Level: class.Level})
|
||||
}
|
||||
var node yaml.Node
|
||||
if err := node.Encode(value); err != nil {
|
||||
return nil, fmt.Errorf("render canonical party: %w", err)
|
||||
}
|
||||
characters.Content = append(characters.Content, &yaml.Node{Kind: yaml.ScalarNode, Tag: "!!str", Value: entry.ID}, &node)
|
||||
}
|
||||
document := &yaml.Node{Kind: yaml.MappingNode, Tag: "!!map", Content: []*yaml.Node{
|
||||
{Kind: yaml.ScalarNode, Tag: "!!str", Value: "schema_version"}, {Kind: yaml.ScalarNode, Tag: "!!str", Value: PartySchemaVersion},
|
||||
{Kind: yaml.ScalarNode, Tag: "!!str", Value: "characters"}, characters,
|
||||
}}
|
||||
data, err := yaml.Marshal(document)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("render canonical party: %w", err)
|
||||
}
|
||||
return data, nil
|
||||
}
|
||||
|
||||
// ClassSummary preserves the declaration order required by the party contract.
|
||||
func (c PartyCharacter) ClassSummary() string {
|
||||
parts := make([]string, 0, len(c.Character.Classes))
|
||||
|
||||
100
internal/config/party_imports_test.go
Normal file
100
internal/config/party_imports_test.go
Normal file
@@ -0,0 +1,100 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestImportedPartyRendersStandaloneRoster(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
root := writePipelineFixture(t, dir, "party.yml", `schema_version: narratio.party.v1
|
||||
config:
|
||||
imports:
|
||||
- path: fragments/roster.yml
|
||||
destination: [characters]
|
||||
required: true
|
||||
characters:
|
||||
zara:
|
||||
player: {name: Zoe}
|
||||
character: {name: Zara, classes: [{name: wizard, level: 8}]}
|
||||
child:
|
||||
extends: base
|
||||
character: {name: Child, alias: [One, Two]}
|
||||
`)
|
||||
writePipelineFixture(t, dir, "fragments/roster.yml", `base:
|
||||
player: {name: Alex}
|
||||
character: {name: Base, classes: [{name: fighter}, {name: cleric, level: 2}]}
|
||||
beta:
|
||||
player: {name: Bea}
|
||||
character: {name: Beta, classes: [{name: ranger}]}
|
||||
`)
|
||||
party, err := LoadParty(root)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var ids []string
|
||||
for _, character := range party.Characters {
|
||||
ids = append(ids, character.ID)
|
||||
}
|
||||
if want := []string{"zara", "child", "base", "beta"}; !reflect.DeepEqual(ids, want) {
|
||||
t.Fatalf("roster order = %v, want %v", ids, want)
|
||||
}
|
||||
if got := party.Characters[1].ClassSummary(); got != "fighter / cleric 2" {
|
||||
t.Fatalf("inherited classes = %q", got)
|
||||
}
|
||||
if got := party.Characters[1].AliasSummary(); got != "One, Two" {
|
||||
t.Fatalf("aliases = %q", got)
|
||||
}
|
||||
data, err := party.YAML()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if strings.Contains(string(data), "extends:") || strings.Contains(string(data), "imports:") {
|
||||
t.Fatalf("unresolved canonical output: %s", data)
|
||||
}
|
||||
reloaded, err := ParseParty(data)
|
||||
if err != nil {
|
||||
t.Fatalf("reload canonical output: %v\n%s", err, data)
|
||||
}
|
||||
if !reflect.DeepEqual(party.Characters, reloaded.Characters) {
|
||||
t.Fatalf("round trip changed roster: %#v / %#v", party.Characters, reloaded.Characters)
|
||||
}
|
||||
players, err := party.PlayersYAML()
|
||||
if err != nil || !strings.Contains(string(players), "id: child") {
|
||||
t.Fatalf("players = %q, %v", players, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestImportedPartyRejectsDuplicateEffectiveNames(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
root := writePipelineFixture(t, dir, "party.yml", "schema_version: narratio.party.v1\nconfig:\n imports:\n - path: roster.yml\ncharacters:\n first:\n player: {name: A}\n character: {name: Shared, classes: [{name: wizard}]}\n")
|
||||
writePipelineFixture(t, dir, "roster.yml", "characters:\n second:\n player: {name: B}\n character: {name: Other, alias: [shared], classes: [{name: fighter}]}\n")
|
||||
if _, err := LoadParty(root); err == nil || !strings.Contains(err.Error(), "ambiguous") {
|
||||
t.Fatalf("duplicate effective name error = %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestImportedPartyExpandsFamilyIdentities(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
writePipelineFixture(t, dir, "party.yml", "schema_version: narratio.party.v1\nconfig:\n imports:\n - path: roster.yml\n")
|
||||
writePipelineFixture(t, dir, "roster.yml", "characters:\n alpha:\n player: {name: A}\n character: {name: Alpha, classes: [{name: wizard}]}\n beta:\n player: {name: B}\n character: {name: Beta, classes: [{name: ranger}]}\n")
|
||||
pipeline, campaign, session := writeArtifactFamilyConfig(t, dir, artifactFamilyPipelineYAML(`
|
||||
character_meta:
|
||||
enabled: true
|
||||
for_each: party.characters
|
||||
prompt_id: character_meta
|
||||
output_path_pattern: artifacts/{character_id}.md
|
||||
inputs:
|
||||
transcript: {source: narratio.transcript.final_trimmed, required: true}
|
||||
`))
|
||||
cfg, err := LoadWithSessionOptions(pipeline, campaign, session, SessionLoadOptions{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, id := range []string{"character_meta_alpha", "character_meta_beta"} {
|
||||
if _, ok := cfg.Pipeline.Scriptorium.Artifacts[id]; !ok {
|
||||
t.Fatalf("missing family member %q", id)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,8 +4,6 @@ import (
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/fileops"
|
||||
)
|
||||
|
||||
func resolvePartyInput(input ResolvedInputFile) (ResolvedParty, error) {
|
||||
@@ -22,11 +20,7 @@ func resolvePartyInput(input ResolvedInputFile) (ResolvedParty, error) {
|
||||
path = filepath.Join(filepath.Dir(configPath), path)
|
||||
}
|
||||
path = filepath.Clean(path)
|
||||
raw, err := fileops.ReadFileBounded(path, maxConfigDocumentBytes)
|
||||
if err != nil {
|
||||
return ResolvedParty{}, fmt.Errorf("read party input %q: %w", path, err)
|
||||
}
|
||||
party, err := ParseParty(raw)
|
||||
party, err := LoadParty(path)
|
||||
if err != nil {
|
||||
return ResolvedParty{}, fmt.Errorf("parse party input %q: %w", path, err)
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
@@ -53,8 +52,12 @@ inputs:
|
||||
if cfg.Party.Source.Source != "campaign_config" || cfg.Party.Source.ConfigPath != campaignPath {
|
||||
t.Fatalf("party provenance = %#v, want campaign source", cfg.Party.Source)
|
||||
}
|
||||
if !bytes.Equal(cfg.Party.Canonical.Raw, partyYAML) {
|
||||
t.Fatal("canonical party bytes were not retained")
|
||||
canonical, err := cfg.Party.Canonical.YAML()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := ParseParty(canonical); err != nil {
|
||||
t.Fatalf("reload canonical party: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -27,9 +27,6 @@ characters:
|
||||
if err != nil {
|
||||
t.Fatalf("ParseParty() error = %v", err)
|
||||
}
|
||||
if !bytes.Equal(document.Raw, data) {
|
||||
t.Fatal("canonical raw bytes were not preserved")
|
||||
}
|
||||
characters := document.Characters
|
||||
if len(characters) != 1 || characters[0].ID != "arannis" {
|
||||
t.Fatalf("characters = %#v, want ordered arannis character", characters)
|
||||
|
||||
@@ -35,11 +35,12 @@ func materializeCanonicalPartyInputs(
|
||||
if resolved.Canonical == nil {
|
||||
return nil, fmt.Errorf("canonical party data is required")
|
||||
}
|
||||
if len(resolved.Canonical.Raw) == 0 {
|
||||
return nil, fmt.Errorf("canonical party raw bytes are required")
|
||||
partyBytes, err := resolved.Canonical.YAML()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("render canonical party: %w", err)
|
||||
}
|
||||
partyPath := filepath.Join(inputsDir, party.Filename)
|
||||
partyChecksum, err := writeBytesIfChanged(store, partyPath, resolved.Canonical.Raw)
|
||||
partyChecksum, err := writeBytesIfChanged(store, partyPath, partyBytes)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("materialize canonical party: %w", err)
|
||||
}
|
||||
|
||||
@@ -132,15 +132,19 @@ func currentPreparePartySourceInputs(env *Env) ([]prepareExpectedInput, error) {
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("players prepared-input descriptor is unavailable")
|
||||
}
|
||||
if env.Config.Party.Canonical == nil || len(env.Config.Party.Canonical.Raw) == 0 {
|
||||
if env.Config.Party.Canonical == nil {
|
||||
return nil, fmt.Errorf("canonical party data is unavailable")
|
||||
}
|
||||
partyBytes, err := env.Config.Party.Canonical.YAML()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
playersBytes, err := env.Config.Party.Canonical.PlayersYAML()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return []prepareExpectedInput{
|
||||
{kind: party.ManifestKind, checksum: checksumPrepareBytes(env.Config.Party.Canonical.Raw)},
|
||||
{kind: party.ManifestKind, checksum: checksumPrepareBytes(partyBytes)},
|
||||
{kind: players.ManifestKind, checksum: checksumPrepareBytes(playersBytes)},
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -193,8 +193,12 @@ characters:
|
||||
paths := sessionPathsForEnv(env, m.SessionID)
|
||||
preparedParty := filepath.Join(paths.InputsDir, "party.yml")
|
||||
preparedPlayers := filepath.Join(paths.InputsDir, "players.yml")
|
||||
if got, err := os.ReadFile(preparedParty); err != nil || !bytes.Equal(got, partyBytes) {
|
||||
t.Fatalf("prepared party = %q, %v; want unchanged canonical bytes", got, err)
|
||||
wantParty, err := document.YAML()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got, err := os.ReadFile(preparedParty); err != nil || !bytes.Equal(got, wantParty) {
|
||||
t.Fatalf("prepared party = %q, %v; want rendered canonical bytes", got, err)
|
||||
}
|
||||
wantPlayers, err := document.PlayersYAML()
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user