Load campaign and session imports from their sources

This commit is contained in:
2026-11-17 22:15:05 +00:00
parent c3023cb609
commit 1657c9f65d
16 changed files with 326 additions and 209 deletions

View File

@@ -59,12 +59,12 @@ func loadCommandConfigWithDependencies(ctx context.Context, pipelineFlag, campai
return retainedCommandConfig(cfg), nil
}
discoveredSession, err := discoverSessionConfigPathWithCandidates(config.DefaultSessionConfigSearchPaths)
discoveredSession, discoveredPath, err := config.DiscoverSession(config.DefaultSessionConfigSearchPaths, sessionOpts)
if err != nil {
return nil, err
}
if discoveredSession.Path != "" {
cfg, err := config.LoadSessionWithPipelineCampaignOptions(*base, discoveredSession.Path, sessionOpts)
if discoveredSession != nil {
cfg, err := config.ResolveLoadedPipelineCampaign(*base, discoveredPath, discoveredSession, config.SessionSource{Source: "session_config", LocalPath: discoveredPath})
if err != nil {
return nil, err
}
@@ -73,7 +73,7 @@ func loadCommandConfigWithDependencies(ctx context.Context, pipelineFlag, campai
sessionID := strings.TrimSpace(sessionOpts.SessionID)
if sessionID == "" {
return nil, missingSessionConfigError(discoveredSession.Searched, "remote session loading requires a session_id")
return nil, missingSessionConfigError(config.DefaultSessionConfigSearchPaths, "remote session loading requires a session_id")
}
rootPrefix := ""
@@ -88,16 +88,16 @@ func loadCommandConfigWithDependencies(ctx context.Context, pipelineFlag, campai
}
store, err := newCommandObjectStoreWithDependencies(ctx, partialCfg, nil, deps)
if err != nil {
return nil, missingSessionConfigError(discoveredSession.Searched, fmt.Sprintf("remote session %q unavailable: %v", remoteKey, err))
return nil, missingSessionConfigError(config.DefaultSessionConfigSearchPaths, fmt.Sprintf("remote session %q unavailable: %v", remoteKey, err))
}
sessionInfo, err := findRemoteSessionConfig(ctx, store, sessionPrefix, remoteKey)
if err != nil {
return nil, missingSessionConfigError(discoveredSession.Searched, err.Error())
return nil, missingSessionConfigError(config.DefaultSessionConfigSearchPaths, err.Error())
}
sessionTempPath, err := deps.downloadObjectToTemp(ctx, store, remoteKey, "narratio-session-*.yml")
if err != nil {
return nil, missingSessionConfigError(discoveredSession.Searched, fmt.Sprintf("remote session %q download failed: %v", remoteKey, err))
return nil, missingSessionConfigError(config.DefaultSessionConfigSearchPaths, fmt.Sprintf("remote session %q download failed: %v", remoteKey, err))
}
cleanup = func() error {
if err := fileops.RemoveAllUnderRoot(filepath.Dir(sessionTempPath), sessionTempPath); err != nil {

View File

@@ -257,6 +257,29 @@ inputs:
}
}
func TestExecuteSessionInitUsesImportedTemplateLocation(t *testing.T) {
pipeline, campaign, _ := writeValidConfigFiles(t, t.TempDir())
dir := filepath.Dir(campaign)
original, err := os.ReadFile(campaign)
if err != nil {
t.Fatal(err)
}
mustWriteTestFile(t, filepath.Join(dir, "fragments", "template-settings.yml"), "session_template_file: templates/session.yml\n")
mustWriteTestFile(t, filepath.Join(dir, "fragments", "templates", "session.yml"), "session_id: '{{ session_id }}'\ninputs:\n audio_s3: {prefix: audio/}\n")
if err := os.WriteFile(campaign, append([]byte("config:\n imports:\n - path: fragments/template-settings.yml\n"), original...), 0o644); err != nil {
t.Fatal(err)
}
output := filepath.Join(t.TempDir(), "session.yml")
var stdout, stderr bytes.Buffer
if code := Execute([]string{"session", "init", "2026-06-07", "--config", pipeline, "--campaign-file", campaign, "--output", output}, &stdout, &stderr); code != 0 {
t.Fatalf("exit = %d; stderr = %q", code, stderr.String())
}
data, err := os.ReadFile(output)
if err != nil || !strings.Contains(string(data), "2026-06-07") {
t.Fatalf("generated session = %q, %v", data, err)
}
}
func TestExecuteSessionInitRemoteRendersCampaignTemplate(t *testing.T) {
workspaceRoot := t.TempDir()
pipelinePath, campaignPath, _ := writeValidConfigFiles(t, workspaceRoot)

View File

@@ -46,6 +46,24 @@ inputs:
}
}
func TestInvalidDiscoveredSessionDoesNotTriggerRemoteFallback(t *testing.T) {
pipeline, campaign, _ := writeValidConfigFiles(t, t.TempDir())
selected := filepath.Join(t.TempDir(), "session.yml")
if err := os.WriteFile(selected, []byte("unexpected: value\n"), 0o644); err != nil {
t.Fatal(err)
}
setSessionConfigSearchPaths(t, []string{selected})
storeCalls := 0
deps := fakeCommandDependencies(&storage.FakeBackend{}, &storeCalls)
var stdout, stderr bytes.Buffer
if code := executeWithDependencies([]string{"session", "plan", "2026-05-03", "--config", pipeline, "--campaign-file", campaign}, &stdout, &stderr, deps, executePlan); code == 0 {
t.Fatal("invalid session was accepted")
}
if storeCalls != 0 || !strings.Contains(stderr.String(), "decode.unknown_field") {
t.Fatalf("store calls=%d, stderr=%q", storeCalls, stderr.String())
}
}
func TestRemoteSessionLoadingRetainsCampaignCanonicalParty(t *testing.T) {
workspaceRoot := t.TempDir()
pipelinePath, campaignPath, _ := writeValidConfigFiles(t, workspaceRoot)

View File

@@ -131,7 +131,7 @@ func TestRunStageAcceptsPositionalSessionIDAndParsesStageName(t *testing.T) {
}
func TestResolveSessionConfigPathErrorIncludesSearchedPaths(t *testing.T) {
_, err := resolveSessionConfigPathWithCandidates("", []string{"/usr/local/etc/narratio/session.yml", "/etc/narratio/session.yml"})
err := missingSessionConfigError([]string{"/usr/local/etc/narratio/session.yml", "/etc/narratio/session.yml"}, "")
if err == nil {
t.Fatal("expected error, got nil")
}

View File

@@ -1,57 +1,10 @@
package app
import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"
)
func resolveSessionConfigPathWithCandidates(flagValue string, candidates []string) (string, error) {
if explicit := strings.TrimSpace(flagValue); explicit != "" {
return explicit, nil
}
resolved, err := discoverSessionConfigPathWithCandidates(candidates)
if err != nil {
return "", err
}
if resolved.Path != "" {
return resolved.Path, nil
}
return "", missingSessionConfigError(resolved.Searched, "")
}
type sessionConfigDiscovery struct {
Path string
Searched []string
}
func discoverSessionConfigPathWithCandidates(candidates []string) (sessionConfigDiscovery, error) {
ordered := make([]string, 0, len(candidates))
for _, raw := range candidates {
path := strings.TrimSpace(raw)
if path == "" {
continue
}
ordered = append(ordered, path)
info, err := os.Stat(path)
if err == nil {
if info.IsDir() {
continue
}
return sessionConfigDiscovery{Path: filepath.Clean(path), Searched: ordered}, nil
}
if errors.Is(err, os.ErrNotExist) {
continue
}
return sessionConfigDiscovery{}, fmt.Errorf("check default session config %q: %w", path, err)
}
return sessionConfigDiscovery{Searched: ordered}, nil
}
func missingSessionConfigError(searched []string, remoteDetail string) error {
ordered := append([]string(nil), searched...)
if len(ordered) == 0 {

View File

@@ -1,68 +0,0 @@
package app
import (
"os"
"path/filepath"
"strings"
"testing"
)
func TestResolveSessionConfigPathWithCandidatesExplicitWins(t *testing.T) {
got, err := resolveSessionConfigPathWithCandidates(" ./custom/session.yml ", []string{"./session.yml", "/a", "/b"})
if err != nil {
t.Fatalf("resolveSessionConfigPathWithCandidates() error = %v", err)
}
if got != "./custom/session.yml" {
t.Fatalf("resolved path = %q, want explicit path", got)
}
}
func TestResolveSessionConfigPathWithCandidatesUsesFirstExisting(t *testing.T) {
dir := t.TempDir()
first := filepath.Join(dir, "first.yml")
second := filepath.Join(dir, "second.yml")
if err := os.WriteFile(second, []byte("session_id: 2026-05-03\n"), 0o644); err != nil {
t.Fatalf("write second default: %v", err)
}
got, err := resolveSessionConfigPathWithCandidates("", []string{first, second})
if err != nil {
t.Fatalf("resolveSessionConfigPathWithCandidates() error = %v", err)
}
if got != filepath.Clean(second) {
t.Fatalf("resolved path = %q, want %q", got, filepath.Clean(second))
}
}
func TestResolveSessionConfigPathWithCandidatesPrecedence(t *testing.T) {
dir := t.TempDir()
first := filepath.Join(dir, "first.yml")
second := filepath.Join(dir, "second.yml")
if err := os.WriteFile(first, []byte("session_id: 2026-05-03\n"), 0o644); err != nil {
t.Fatalf("write first default: %v", err)
}
if err := os.WriteFile(second, []byte("session_id: 2026-05-03\n"), 0o644); err != nil {
t.Fatalf("write second default: %v", err)
}
got, err := resolveSessionConfigPathWithCandidates("", []string{first, second})
if err != nil {
t.Fatalf("resolveSessionConfigPathWithCandidates() error = %v", err)
}
if got != filepath.Clean(first) {
t.Fatalf("resolved path = %q, want first candidate %q", got, filepath.Clean(first))
}
}
func TestResolveSessionConfigPathWithCandidatesMissing(t *testing.T) {
_, err := resolveSessionConfigPathWithCandidates("", []string{"/does/not/exist/one.yml", "/does/not/exist/two.yml"})
if err == nil {
t.Fatal("expected error, got nil")
}
if !strings.Contains(err.Error(), "no default session config found") {
t.Fatalf("error = %q, want missing-defaults context", err.Error())
}
if !strings.Contains(err.Error(), "pass --session") {
t.Fatalf("error = %q, want explicit-path guidance", err.Error())
}
}

View File

@@ -5,8 +5,8 @@ import (
"sort"
"strings"
configkit "go.maximumdirect.net/configkit"
"gitea.maximumdirect.net/eric/narratio/internal/artifactpolicy"
configkit "go.maximumdirect.net/configkit"
)
// EffectiveArtifactRecord describes one concrete artifact after configuration

View File

@@ -90,7 +90,7 @@ func TestCampaignStrictDecodeAcceptsSessionTemplateFile(t *testing.T) {
if err != nil {
t.Fatalf("LoadWithSessionOptions() error = %v", err)
}
if cfg.Campaign.SessionTemplateFile != "./session.template.yml" {
if cfg.Campaign.SessionTemplateFile != filepath.Join(filepath.Dir(campaignPath), "session.template.yml") {
t.Fatalf("SessionTemplateFile = %q, want ./session.template.yml", cfg.Campaign.SessionTemplateFile)
}
}
@@ -173,8 +173,8 @@ func TestCampaignSessionMergeSpellCatalog(t *testing.T) {
wantConfigPath = sessionPath
}
assertResolvedStableInput(t, cfg.StableInputs.SpellCatalogFile, tt.wantPath, wantConfigPath, tt.wantSource)
if cfg.Session.Inputs.SpellCatalogFile != tt.wantPath {
t.Fatalf("session spell_catalog_file = %q, want %q", cfg.Session.Inputs.SpellCatalogFile, tt.wantPath)
if cfg.Session.Inputs.SpellCatalogFile != cfg.StableInputs.SpellCatalogFile.Path {
t.Fatalf("session spell_catalog_file = %q, want %q", cfg.Session.Inputs.SpellCatalogFile, cfg.StableInputs.SpellCatalogFile.Path)
}
})
}
@@ -275,6 +275,9 @@ func writeCampaignConfigTestFiles(t *testing.T, campaignYAML, sessionYAML string
func assertResolvedStableInput(t *testing.T, got ResolvedInputFile, wantPath, wantConfigPath, wantSource string) {
t.Helper()
if wantPath != "" && !filepath.IsAbs(wantPath) {
wantPath = filepath.Join(filepath.Dir(wantConfigPath), wantPath)
}
if got.Path != wantPath || got.ConfigPath != wantConfigPath || got.Source != wantSource {
t.Fatalf("resolved input = %#v, want path=%q config_path=%q source=%q", got, wantPath, wantConfigPath, wantSource)
}

View File

@@ -5,12 +5,14 @@ import configkit "go.maximumdirect.net/configkit"
// Config is the resolved combined configuration from pipeline.yml,
// campaign.yml, and session.yml.
type Config struct {
Pipeline *PipelineConfig
Campaign *CampaignConfig
Session *SessionConfig
PipelinePath string
CampaignPath string
SessionPath string
Pipeline *PipelineConfig
Campaign *CampaignConfig
Session *SessionConfig
PipelinePath string
CampaignPath string
SessionPath string
CampaignDocument *CampaignConfig
SessionDocument *SessionConfig
StableInputs ResolvedStableInputs
SessionSource SessionSource
@@ -58,6 +60,7 @@ type CampaignConfig struct {
CampaignID string `yaml:"campaign_id" json:"campaign_id"`
SessionTemplateFile string `yaml:"session_template_file" json:"session_template_file"`
Inputs CampaignInputsConfig `yaml:"inputs" json:"inputs"`
resolution *documentResolution
}
// CampaignInputsConfig contains stable campaign-level input file references.
@@ -77,6 +80,7 @@ type SessionConfig struct {
Date string `yaml:"date" json:"date"`
Title string `yaml:"title" json:"title"`
Inputs SessionInputsConfig `yaml:"inputs" json:"inputs"`
resolution *documentResolution
}
// WorkspaceConfig configures local workspace behavior.

View File

@@ -1,32 +1,123 @@
package config
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
configkit "go.maximumdirect.net/configkit"
)
const maxConfigDocumentBytes int64 = 8 << 20
type documentResolution struct {
campaign *configkit.Result[CampaignConfig]
session *configkit.Result[SessionConfig]
sources map[string]string
}
func documentResource(resolution *configkit.Resolution, path configkit.Path, value *string, invocationDir string, sources map[string]string) {
if info, ok := resolution.EntryInfo(path); ok && len(info.Origin.Locations) == 1 {
if source, ok := resolution.Source(info.Origin.Locations[0].SourceID); ok {
name := source.Path
if name == "" {
name = source.Label
}
sources[path.String()] = name
}
}
resolveHostResource(resolution, path, value, invocationDir)
}
func campaignDefinition(invocationDir string, sources map[string]string) configkit.Definition[CampaignConfig] {
return configkit.Definition[CampaignConfig]{Resolve: func(cfg *CampaignConfig, resolution *configkit.Resolution) error {
documentResource(resolution, configkit.Keys("session_template_file"), &cfg.SessionTemplateFile, invocationDir, sources)
inputs := configkit.Keys("inputs")
for _, field := range []struct {
name string
value *string
}{
{"speakers_file", &cfg.Inputs.SpeakersFile}, {"autocorrect_file", &cfg.Inputs.AutocorrectFile},
{"glossary_file", &cfg.Inputs.GlossaryFile}, {"party_file", &cfg.Inputs.PartyFile},
{"spell_catalog_file", &cfg.Inputs.SpellCatalogFile},
} {
documentResource(resolution, inputs.Key(field.name), field.value, invocationDir, sources)
}
return nil
}}
}
func sessionDefinition(invocationDir string, sources map[string]string) configkit.Definition[SessionConfig] {
return configkit.Definition[SessionConfig]{Resolve: func(cfg *SessionConfig, resolution *configkit.Resolution) error {
inputs := configkit.Keys("inputs")
for _, field := range []struct {
name string
value *string
}{
{"audio_dir", &cfg.Inputs.AudioDir}, {"speakers_file", &cfg.Inputs.SpeakersFile},
{"autocorrect_file", &cfg.Inputs.AutocorrectFile}, {"glossary_file", &cfg.Inputs.GlossaryFile},
{"spell_catalog_file", &cfg.Inputs.SpellCatalogFile},
} {
documentResource(resolution, inputs.Key(field.name), field.value, invocationDir, sources)
}
for index := range cfg.Inputs.AudioFiles {
documentResource(resolution, inputs.Key("audio_files").Index(index), &cfg.Inputs.AudioFiles[index], invocationDir, sources)
}
return nil
}}
}
func loadCampaignWithConfigKit(path string) (*CampaignConfig, error) {
result, err := configkit.Load(configkit.File(path), configkit.Options[CampaignConfig]{
Definition: configkit.Definition[CampaignConfig]{},
})
invocationDir, err := os.Getwd()
if err != nil {
return nil, err
}
sources := make(map[string]string)
result, err := configkit.Load(configkit.File(path), configkit.Options[CampaignConfig]{Imports: true, Definition: campaignDefinition(invocationDir, sources)})
if err != nil {
return nil, describeConfigKitLoadError("campaign", err)
}
return &result.Config, nil
cfg := result.Config
cfg.resolution = &documentResolution{campaign: result, sources: sources}
return &cfg, nil
}
func loadSessionWithConfigKit(label string, data []byte) (*SessionConfig, error) {
if int64(len(data)) > maxConfigDocumentBytes {
return nil, fmt.Errorf("load session config: session file %q exceeds %d-byte limit", label, maxConfigDocumentBytes)
}
result, err := configkit.Load(configkit.Bytes(label, data), configkit.Options[SessionConfig]{
Definition: configkit.Definition[SessionConfig]{},
})
func loadSessionSource(source configkit.Source, label string, opts SessionLoadOptions, imports bool) (*SessionConfig, string, error) {
invocationDir, err := os.Getwd()
if err != nil {
return nil, describeConfigKitLoadError("session", err)
return nil, "", err
}
return &result.Config, nil
sources := make(map[string]string)
result, err := configkit.Load(source, configkit.Options[SessionConfig]{Imports: imports, Definition: sessionDefinition(invocationDir, sources)})
if err != nil {
return nil, "", describeConfigKitLoadError("session", err)
}
acquired := result.Sources()
if len(acquired) == 0 {
return nil, "", nil
}
selected := acquired[0].Path
if selected == "" {
selected = label
}
cfg := result.Config
data, err := json.Marshal(cfg)
if err != nil {
return nil, "", fmt.Errorf("inspect session config: %w", err)
}
if err := rejectSessionTemplatePlaceholders(selected, string(data)); err != nil {
return nil, "", fmt.Errorf("load session config: %w", err)
}
if err := validateSessionIdentity(selected, &cfg, opts); err != nil {
return nil, "", err
}
cfg.resolution = &documentResolution{session: result, sources: sources}
return &cfg, filepath.Clean(selected), nil
}
// DiscoverSession loads the first selected local candidate, or returns nil when
// none exists. A selected but invalid source is an error.
func DiscoverSession(candidates []string, opts SessionLoadOptions) (*SessionConfig, string, error) {
return loadSessionSource(configkit.SearchConfig("narratio", configkit.SearchPaths(candidates...)), "", opts, true)
}

View File

@@ -8,32 +8,20 @@ import (
"testing"
)
func TestSingleSourceDocumentsRejectConfigKitDirectives(t *testing.T) {
dir := t.TempDir()
campaignPath := filepath.Join(dir, "campaign.yml")
if err := os.WriteFile(campaignPath, []byte("config:\n imports: []\ncampaign_id: sample\n"), 0o644); err != nil {
t.Fatal(err)
}
tests := []struct {
name string
load func() error
}{
{name: "campaign", load: func() error { _, err := LoadCampaign(campaignPath); return err }},
{name: "session", load: func() error {
_, err := LoadSessionBytesWithOptions("session.yml", []byte("config:\n imports: []\nsession_id: sample\n"), SessionLoadOptions{})
func TestContextlessDocumentsRejectImports(t *testing.T) {
for _, load := range []func() error{
func() error {
_, err := LoadSessionBytesWithOptions("remote", []byte("config:\n imports: []\nsession_id: sample\n"), SessionLoadOptions{})
return err
}},
{name: "party", load: func() error {
},
func() error {
_, err := ParseParty([]byte("config:\n imports: []\nschema_version: narratio.party.v1\ncharacters: {}\n"))
return err
}},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if err := test.load(); err == nil || !strings.Contains(err.Error(), "import.disabled") {
t.Fatalf("load error = %v, want disabled-import diagnostic", err)
}
})
},
} {
if err := load(); err == nil || !strings.Contains(err.Error(), "import.disabled") {
t.Fatalf("load error = %v, want disabled-import diagnostic", err)
}
}
}

View File

@@ -0,0 +1,97 @@
package config
import (
"os"
"path/filepath"
"strings"
"testing"
)
func TestImportedCampaignAndSessionResourcesRetainTheirSources(t *testing.T) {
dir := t.TempDir()
pipeline := writePipelineFixture(t, dir, "pipeline.yml", "workspace: {root: /tmp/narratio}\n")
campaign := writePipelineFixture(t, dir, "campaign.yml", "config:\n imports:\n - path: campaign/shared.yml\ncampaign_id: sample\n")
fragment := writePipelineFixture(t, dir, "campaign/shared.yml", `session_template_file: templates/session.yml
inputs:
speakers_file: speakers.yml
autocorrect_file: autocorrect.yml
glossary_file: glossary.yml
party_file: party.yml
`)
writePipelineFixture(t, dir, "campaign/party.yml", canonicalPartyFixture)
session := writePipelineFixture(t, dir, "session.yml", "config:\n imports:\n - path: session/inputs.yml\nsession_id: sample-session\n")
sessionFragment := writePipelineFixture(t, dir, "session/inputs.yml", "inputs:\n speakers_file: override.yml\n audio_files: [a.flac, b.flac]\n")
cfg, err := LoadWithSessionOptions(pipeline, campaign, session, SessionLoadOptions{})
if err != nil {
t.Fatal(err)
}
if cfg.Campaign.SessionTemplateFile != filepath.Join(dir, "campaign/templates/session.yml") {
t.Fatalf("template = %q", cfg.Campaign.SessionTemplateFile)
}
if got := cfg.StableInputs.SpeakersFile; got.Path != filepath.Join(dir, "session/override.yml") || got.ConfigPath != sessionFragment {
t.Fatalf("session override = %#v", got)
}
if got := cfg.StableInputs.GlossaryFile; got.Path != filepath.Join(dir, "campaign/glossary.yml") || got.ConfigPath != fragment {
t.Fatalf("campaign input = %#v", got)
}
if cfg.Party.Source.ConfigPath != fragment || cfg.Party.Source.Path != filepath.Join(dir, "campaign/party.yml") {
t.Fatalf("party source = %#v", cfg.Party.Source)
}
for i, name := range []string{"a.flac", "b.flac"} {
if got := cfg.Session.Inputs.AudioFiles[i]; got != filepath.Join(dir, "session", name) {
t.Fatalf("audio[%d] = %q", i, got)
}
}
if cfg.SessionDocument.Inputs.GlossaryFile != "" || cfg.SessionDocument.Inputs.SpeakersFile != filepath.Join(dir, "session/override.yml") {
t.Fatalf("detached session = %#v", cfg.SessionDocument)
}
if cfg.CampaignDocument.Inputs.GlossaryFile != filepath.Join(dir, "campaign/glossary.yml") {
t.Fatalf("detached campaign = %#v", cfg.CampaignDocument)
}
}
func TestSessionDiscoveryDistinguishesAbsenceFromInvalidSelection(t *testing.T) {
dir := t.TempDir()
first := filepath.Join(dir, "first.yml")
second := writePipelineFixture(t, dir, "second.yml", "session_id: valid\n")
if cfg, path, err := DiscoverSession([]string{first, second}, SessionLoadOptions{}); err != nil || cfg == nil || path != second {
t.Fatalf("selected = %#v %q %v", cfg, path, err)
}
if _, _, err := DiscoverSession([]string{first, second}, SessionLoadOptions{SessionID: "different"}); err == nil || !strings.Contains(err.Error(), "mismatch") {
t.Fatalf("identity error = %v", err)
}
if err := os.WriteFile(first, []byte("inputs: [invalid\n"), 0o644); err != nil {
t.Fatal(err)
}
if _, _, err := DiscoverSession([]string{first, second}, SessionLoadOptions{}); err == nil {
t.Fatal("invalid first candidate fell through")
}
if cfg, path, err := DiscoverSession([]string{filepath.Join(dir, "absent.yml")}, SessionLoadOptions{}); err != nil || cfg != nil || path != "" {
t.Fatalf("absent result = %#v %q %v", cfg, path, err)
}
}
func TestImportedSessionPlaceholderIsRejected(t *testing.T) {
dir := t.TempDir()
root := writePipelineFixture(t, dir, "session.yml", "config:\n imports:\n - path: fragment.yml\nsession_id: test\n")
writePipelineFixture(t, dir, "fragment.yml", "title: '{{ title }}'\n")
if _, err := LoadSession(root); err == nil || !strings.Contains(err.Error(), "title") || !strings.Contains(err.Error(), "session init") {
t.Fatalf("placeholder error = %v", err)
}
}
func TestRemoteSessionResourceBaseIgnoresDiagnosticLabel(t *testing.T) {
invocation, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
for _, label := range []string{"s3://bucket/session.yml", filepath.Join(t.TempDir(), "download.yml")} {
cfg, err := LoadSessionBytesWithOptions(label, []byte("session_id: test\ninputs: {audio_dir: ./audio}\n"), SessionLoadOptions{})
if err != nil {
t.Fatal(err)
}
if got := cfg.Inputs.AudioDir; got != filepath.Join(invocation, "audio") {
t.Fatalf("resource from %q = %q", label, got)
}
}
}

View File

@@ -4,10 +4,10 @@ import (
"fmt"
"path/filepath"
"regexp"
"slices"
"sort"
"strings"
"gitea.maximumdirect.net/eric/narratio/internal/fileops"
configkit "go.maximumdirect.net/configkit"
)
@@ -80,51 +80,30 @@ type SessionLoadOptions struct {
// LoadSessionWithOptions loads session configuration from a YAML file with
// strict field checking.
func LoadSessionWithOptions(path string, opts SessionLoadOptions) (*SessionConfig, error) {
sessionBytes, err := fileops.ReadFileBounded(path, maxConfigDocumentBytes)
if err != nil {
return nil, fmt.Errorf("load session config: session file %q: open: %w", path, err)
}
return LoadSessionBytesWithOptions(path, sessionBytes, opts)
cfg, _, err := loadSessionSource(configkit.File(path), path, opts, true)
return cfg, err
}
// LoadSessionBytesWithOptions loads session configuration from YAML bytes with
// strict field checking.
// LoadSessionBytesWithOptions loads a contextless session snapshot.
func LoadSessionBytesWithOptions(label string, data []byte, opts SessionLoadOptions) (*SessionConfig, error) {
if err := rejectSessionTemplatePlaceholders(label, string(data)); err != nil {
return nil, fmt.Errorf("load session config: %w", err)
}
cfg, _, err := loadSessionSource(configkit.Bytes(label, data), label, opts, false)
return cfg, err
}
cfg, err := loadSessionWithConfigKit(label, data)
if err != nil {
return nil, err
}
func validateSessionIdentity(label string, cfg *SessionConfig, opts SessionLoadOptions) error {
if strings.TrimSpace(opts.SessionID) != "" && strings.TrimSpace(cfg.SessionID) != "" && strings.TrimSpace(cfg.SessionID) != strings.TrimSpace(opts.SessionID) {
return nil, fmt.Errorf(
"load session config: session file %q: session_id mismatch: --session-id %q does not match session_id %q",
label,
strings.TrimSpace(opts.SessionID),
strings.TrimSpace(cfg.SessionID),
)
return fmt.Errorf("load session config: session file %q: session_id mismatch: --session-id %q does not match session_id %q", label, strings.TrimSpace(opts.SessionID), strings.TrimSpace(cfg.SessionID))
}
if expectedPreviousSessionID := strings.TrimSpace(opts.PreviousSessionID); expectedPreviousSessionID != "" {
actualPreviousSessionID := strings.TrimSpace(cfg.PreviousSessionID)
if actualPreviousSessionID == "" {
return nil, fmt.Errorf(
"load session config: session file %q: previous_session_id is required when --previous-session-id %q is provided",
label,
expectedPreviousSessionID,
)
if expected := strings.TrimSpace(opts.PreviousSessionID); expected != "" {
actual := strings.TrimSpace(cfg.PreviousSessionID)
if actual == "" {
return fmt.Errorf("load session config: session file %q: previous_session_id is required when --previous-session-id %q is provided", label, expected)
}
if actualPreviousSessionID != expectedPreviousSessionID {
return nil, fmt.Errorf(
"load session config: session file %q: previous_session_id mismatch: --previous-session-id %q does not match previous_session_id %q",
label,
expectedPreviousSessionID,
actualPreviousSessionID,
)
if actual != expected {
return fmt.Errorf("load session config: session file %q: previous_session_id mismatch: --previous-session-id %q does not match previous_session_id %q", label, expected, actual)
}
}
return cfg, nil
return nil
}
// Load loads and resolves combined pipeline, campaign, and session configuration.
@@ -252,9 +231,18 @@ func ResolveLoadedPipelineCampaign(loaded LoadedPipelineCampaign, sessionPath st
SessionSource: sessionSource,
Party: loaded.Party,
}
campaignDocument := *loaded.Campaign
cfg.CampaignDocument = &campaignDocument
if sessionCfg == nil {
return cfg, nil
}
sessionDocument := *sessionCfg
sessionDocument.Inputs.AudioFiles = slices.Clone(sessionCfg.Inputs.AudioFiles)
if sessionCfg.Inputs.AudioS3 != nil {
audio := *sessionCfg.Inputs.AudioS3
sessionDocument.Inputs.AudioS3 = &audio
}
cfg.SessionDocument = &sessionDocument
stableInputs, err := mergeCampaignSession(loaded.Campaign, sessionCfg, loaded.CampaignPath, sessionPath)
if err != nil {
@@ -356,6 +344,22 @@ func mergeCampaignSession(campaignCfg *CampaignConfig, sessionCfg *SessionConfig
sessionPath,
),
}
for _, field := range []struct {
name string
input *ResolvedInputFile
}{
{"speakers_file", &stable.SpeakersFile}, {"autocorrect_file", &stable.AutocorrectFile},
{"glossary_file", &stable.GlossaryFile}, {"party_file", &stable.PartyFile},
{"spell_catalog_file", &stable.SpellCatalogFile},
} {
metadata := campaignCfg.resolution
if field.input.Source == "session_config" {
metadata = sessionCfg.resolution
}
if metadata != nil && metadata.sources["/inputs/"+field.name] != "" {
field.input.ConfigPath = metadata.sources["/inputs/"+field.name]
}
}
sessionCfg.Inputs.SpeakersFile = stable.SpeakersFile.Path
sessionCfg.Inputs.AutocorrectFile = stable.AutocorrectFile.Path

View File

@@ -8,8 +8,8 @@ import (
"strings"
"unicode"
configkit "go.maximumdirect.net/configkit"
"gitea.maximumdirect.net/eric/narratio/internal/artifactpolicy"
configkit "go.maximumdirect.net/configkit"
"gopkg.in/yaml.v3"
)

View File

@@ -47,9 +47,13 @@ func campaignPartyInput(campaignPath string, campaign *CampaignConfig) (Resolved
if strings.TrimSpace(campaign.Inputs.PartyFile) == "" {
return ResolvedInputFile{}, fmt.Errorf("campaign.inputs.party_file is required")
}
sourcePath := campaignPath
if campaign.resolution != nil && campaign.resolution.sources["/inputs/party_file"] != "" {
sourcePath = campaign.resolution.sources["/inputs/party_file"]
}
return ResolvedInputFile{
Path: campaign.Inputs.PartyFile,
ConfigPath: campaignPath,
ConfigPath: sourcePath,
Source: "campaign_config",
}, nil
}