Preserve campaign defaults in materialized sessions

This commit is contained in:
2026-11-20 13:38:21 +00:00
parent 38866110a7
commit 25c59ff62f
8 changed files with 92 additions and 18 deletions

View File

@@ -241,7 +241,7 @@ inputs:
t.Fatalf("read generated session: %v", err)
}
got := string(data)
generated, err := config.LoadSession(outputPath)
generated, err := config.LoadSessionWithOptions(outputPath, sessionInitLoadOptions(t, campaignPath))
if err != nil {
t.Fatal(err)
}

View File

@@ -65,7 +65,7 @@ func TestPreparedSessionPublishAndRemoteLoad(t *testing.T) {
}
prefix := artifacts.S3SessionPrefix("dnd", m.Campaign, m.SessionID)
key := artifacts.S3RunRelativeDestinationKey(artifacts.S3RunPrefix(prefix, m.RunID), "prepare/inputs/session.yml")
if got := string(fake.Objects[key].Data); !strings.Contains(got, "Published title") || strings.Contains(got, "imports:") || !strings.Contains(got, "speakers_file:") {
if got := string(fake.Objects[key].Data); !strings.Contains(got, "Published title") || strings.Contains(got, "imports:") || strings.Contains(got, "speakers_file:") {
t.Fatalf("published session = %q", got)
}
for _, name := range []string{"campaign.yml", "party.yml"} {
@@ -85,7 +85,7 @@ func TestPreparedSessionPublishAndRemoteLoad(t *testing.T) {
if received.Session.Title != "Published title" || received.SessionSource.S3Key != key {
t.Fatalf("remote session = %#v, source=%#v", received.Session, received.SessionSource)
}
if got := received.StableInputs.SpeakersFile; got.Source != "session_config" || got.Path != filepath.Join(filepath.Dir(campaignPath), "speakers.yml") {
if got := received.StableInputs.SpeakersFile; got.Source != "campaign_config" || got.Path != filepath.Join(filepath.Dir(campaignPath), "speakers.yml") {
t.Fatalf("receiving campaign speakers = %#v", got)
}
current, err := discoverRemoteCurrentState(ctx, received, fake)

View File

@@ -32,7 +32,7 @@ func TestSessionInitializationCommandKeepsYAMLIndependentOfReportFormat(t *testi
if !report.Written || report.Remote || report.Destination != output {
t.Fatalf("report=%#v", report)
}
cfg, err := config.LoadSession(output)
cfg, err := config.LoadSessionWithOptions(output, sessionInitLoadOptions(t, campaign))
if err != nil {
t.Fatal(err)
}
@@ -70,7 +70,7 @@ func TestSessionInitializationCommandRemoteRoundTrips(t *testing.T) {
}
key := artifacts.S3SessionConfigKey(artifacts.S3SessionPrefix("dnd", "sample-campaign", "2026-05-04"))
data := fake.Objects[key].Data
session, err := config.LoadSessionBytesWithOptions("remote-created", data, config.SessionLoadOptions{})
session, err := config.LoadSessionBytesWithOptions("remote-created", data, sessionInitLoadOptions(t, campaign))
if err != nil {
t.Fatal(err)
}

View File

@@ -24,7 +24,7 @@ func TestSessionInitMaterializesLocalTemplateImports(t *testing.T) {
if err := os.Remove(fragment); err != nil {
t.Fatal(err)
}
loaded, err := config.LoadSession(output)
loaded, err := config.LoadSessionWithOptions(output, sessionInitLoadOptions(t, campaign))
if err != nil {
t.Fatal(err)
}
@@ -46,7 +46,7 @@ func TestSessionInitPublishesStandaloneImportedTemplate(t *testing.T) {
if err := os.Remove(fragment); err != nil {
t.Fatal(err)
}
loaded, err := config.LoadSessionBytesWithOptions("remote session", fake.Objects[key].Data, config.SessionLoadOptions{})
loaded, err := config.LoadSessionBytesWithOptions("remote session", fake.Objects[key].Data, sessionInitLoadOptions(t, campaign))
if err != nil {
t.Fatal(err)
}
@@ -61,7 +61,7 @@ func TestSessionInitPersistsInvocationRelativeAudio(t *testing.T) {
if err := invokeNativeCommandWith(context.Background(), []string{"session", "init", "2026-06-07", "--config", pipeline, "--campaign-file", campaign, "--output", output, "--audio-dir", "./audio"}, &bytes.Buffer{}, executePlan, commandDependencies{}); err != nil {
t.Fatal(err)
}
loaded, err := config.LoadSession(output)
loaded, err := config.LoadSessionWithOptions(output, sessionInitLoadOptions(t, campaign))
if err != nil {
t.Fatal(err)
}
@@ -73,3 +73,12 @@ func TestSessionInitPersistsInvocationRelativeAudio(t *testing.T) {
t.Fatalf("audio = %q; want %q", loaded.Inputs.AudioDir, want)
}
}
func sessionInitLoadOptions(t *testing.T, campaignPath string) config.SessionLoadOptions {
t.Helper()
campaign, err := config.LoadCampaign(campaignPath)
if err != nil {
t.Fatal(err)
}
return config.SessionLoadOptions{Campaign: campaign, CampaignPath: campaignPath}
}

View File

@@ -3,6 +3,7 @@ package config
import (
"fmt"
configkit "go.maximumdirect.net/configkit"
"gopkg.in/yaml.v3"
)
@@ -49,18 +50,35 @@ func RenderSessionDocument(cfg *Config, sessionID string) ([]byte, error) {
if id == "" {
id = sessionID
}
result := doc.ConfigKitResult()
var metadata configkit.Metadata
if result != nil {
metadata = result.Metadata()
}
// Materialized sessions retain authored overrides and receiving-campaign defaults.
inputValue := func(name, value string) *string {
if result != nil {
if !metadata.IsExplicit(configkit.Keys("inputs", name)) {
return nil
}
} else if value == "" {
return nil
}
return &value
}
inputs := struct {
AudioDir string `yaml:"audio_dir,omitempty"`
AudioFiles []string `yaml:"audio_files,omitempty"`
AudioS3 *SessionAudioS3Input `yaml:"audio_s3,omitempty"`
SpeakersFile string `yaml:"speakers_file,omitempty"`
AutocorrectFile string `yaml:"autocorrect_file,omitempty"`
GlossaryFile string `yaml:"glossary_file,omitempty"`
SpeakersFile *string `yaml:"speakers_file,omitempty"`
AutocorrectFile *string `yaml:"autocorrect_file,omitempty"`
GlossaryFile *string `yaml:"glossary_file,omitempty"`
SpellCatalogFile *string `yaml:"spell_catalog_file,omitempty"`
}{doc.Inputs.AudioDir, doc.Inputs.AudioFiles, doc.Inputs.AudioS3, doc.Inputs.SpeakersFile, doc.Inputs.AutocorrectFile, doc.Inputs.GlossaryFile, nil}
if doc.Inputs.SpellCatalogFile != "" || sessionFieldExplicit(doc, "spell_catalog_file") {
inputs.SpellCatalogFile = &doc.Inputs.SpellCatalogFile
}
}{doc.Inputs.AudioDir, doc.Inputs.AudioFiles, doc.Inputs.AudioS3,
inputValue("speakers_file", doc.Inputs.SpeakersFile),
inputValue("autocorrect_file", doc.Inputs.AutocorrectFile),
inputValue("glossary_file", doc.Inputs.GlossaryFile),
inputValue("spell_catalog_file", doc.Inputs.SpellCatalogFile)}
value := struct {
SessionID string `yaml:"session_id"`
PreviousSessionID string `yaml:"previous_session_id,omitempty"`

View File

@@ -89,6 +89,47 @@ func TestSessionDefaultsCompleteBeforeNativeHandoff(t *testing.T) {
if !reflect.DeepEqual(reloaded.Inputs, cfg.Session.Inputs) {
t.Fatalf("standalone rendering lost effective values or explicit clearing: %s", materialized)
}
writePipelineFixture(t, dir, "campaign/resources.yml", `inputs:
speakers_file: replacement-speakers.yml
autocorrect_file: replacement-autocorrect.yml
glossary_file: replacement-glossary.yml
party_file: party.yml
spell_catalog_file: replacement-spells.yml
`)
receiving, err := LoadCampaign(campaign)
if err != nil {
t.Fatal(err)
}
reloaded, err = LoadSessionBytesWithOptions("materialized", materialized, SessionLoadOptions{Campaign: receiving, CampaignPath: campaign})
if err != nil {
t.Fatal(err)
}
if reloaded.Inputs.GlossaryFile != receiving.Inputs.GlossaryFile || reloaded.Inputs.AutocorrectFile != receiving.Inputs.AutocorrectFile {
t.Fatalf("materialization froze inherited inputs: %+v", reloaded.Inputs)
}
if reloaded.Inputs.SpeakersFile != cfg.Session.Inputs.SpeakersFile || reloaded.Inputs.SpellCatalogFile != "" {
t.Fatalf("materialization lost explicit override or clearing: %+v", reloaded.Inputs)
}
}
func TestMaterializedSessionOmitsInheritedSpellCatalog(t *testing.T) {
dir := t.TempDir()
campaignPath := writePipelineFixture(t, dir, "campaign.yml", "campaign_id: campaign\ninputs: {speakers_file: speakers.yml, autocorrect_file: autocorrect.yml, glossary_file: glossary.yml, party_file: party.yml, spell_catalog_file: spells.yml}\n")
campaign, err := LoadCampaign(campaignPath)
if err != nil {
t.Fatal(err)
}
session, err := LoadSessionBytesWithOptions("session.yml", []byte("session_id: session\ninputs: {audio_dir: audio}\n"), SessionLoadOptions{Campaign: campaign, CampaignPath: campaignPath})
if err != nil {
t.Fatal(err)
}
data, err := RenderSessionDocument(&Config{Session: session}, "session")
if err != nil {
t.Fatal(err)
}
if strings.Contains(string(data), "spell_catalog_file") || strings.Contains(string(data), "speakers_file") {
t.Fatalf("materialized inherited fields: %s", data)
}
}
func TestSessionDefaultsRespectExplicitEmptyInputsAndIdentity(t *testing.T) {

View File

@@ -61,7 +61,7 @@ func TestPrepareMaterializesImportedDocumentsWithoutAuthoringFiles(t *testing.T)
if err != nil || preparedCampaign.Inputs.SpeakersFile != filepath.Join(dir, "speakers.yml") {
t.Fatalf("prepared campaign = %#v, %v", preparedCampaign, err)
}
preparedSession, err := config.LoadSession(filepath.Join(paths.InputsDir, "session.yml"))
preparedSession, err := config.LoadSessionWithOptions(filepath.Join(paths.InputsDir, "session.yml"), config.SessionLoadOptions{Campaign: preparedCampaign, CampaignPath: filepath.Join(paths.InputsDir, "campaign.yml")})
if err != nil || preparedSession.Title != "Imported title" || preparedSession.Inputs.SpeakersFile != filepath.Join(dir, "speakers.yml") {
t.Fatalf("prepared session = %#v, %v", preparedSession, err)
}