From 6593ccbeaf27a72e1f75febc1d76070df2403f2f Mon Sep 17 00:00:00 2001 From: Eric Rakestraw Date: Tue, 17 Nov 2026 22:06:29 +0000 Subject: [PATCH] Use native pipeline discovery and independent secrets bootstrap --- docs/roadmap/implementation.md | 2 +- .../app/analyze_artifacts_commands_test.go | 10 +- internal/app/command_dependencies.go | 7 +- internal/app/commands_test.go | 20 +-- internal/app/config_commands_test.go | 14 ++- internal/app/config_diff.go | 2 +- internal/app/config_loader.go | 13 +- internal/app/extract_lifecycle_test.go | 2 +- internal/app/operator_helpers_test.go | 26 ++-- internal/app/pipeline_config_path.go | 49 -------- internal/app/pipeline_config_path_test.go | 117 ------------------ .../provider_credentials_integration_test.go | 4 +- internal/app/remote_session_test.go | 4 +- internal/app/restore_test.go | 4 +- internal/app/secrets_bootstrap.go | 14 +-- internal/app/secrets_bootstrap_test.go | 30 ++++- internal/app/test_environment_test.go | 27 ++-- internal/config/defaults.go | 28 ++--- internal/config/load.go | 4 +- internal/config/pipeline_configkit.go | 14 ++- internal/config/pipeline_discovery_test.go | 87 +++++++++++++ 21 files changed, 208 insertions(+), 270 deletions(-) delete mode 100644 internal/app/pipeline_config_path.go delete mode 100644 internal/app/pipeline_config_path_test.go create mode 100644 internal/config/pipeline_discovery_test.go diff --git a/docs/roadmap/implementation.md b/docs/roadmap/implementation.md index 1605fc0..8b39a1b 100644 --- a/docs/roadmap/implementation.md +++ b/docs/roadmap/implementation.md @@ -98,7 +98,7 @@ Check module identity and attribution. Attempt focused config/app tests; record failures assigned to later stages. Complete when the dependency/API baseline is established, without preserving old behavioral expectations as compatibility. -## Stage 2: Integrate native pipeline discovery and independent bootstrap +## Stage 2: Integrate native pipeline discovery and independent bootstrap [x] Read `internal/app/pipeline_config_path.go`, `secrets_bootstrap.go`, `command_dependencies.go`, `loadPipelineConfigWithDependencies` and callers, diff --git a/internal/app/analyze_artifacts_commands_test.go b/internal/app/analyze_artifacts_commands_test.go index 06f504e..ae579dd 100644 --- a/internal/app/analyze_artifacts_commands_test.go +++ b/internal/app/analyze_artifacts_commands_test.go @@ -272,7 +272,7 @@ func TestExecuteAnalyzeRejectsPositionalArgsAndForceFlag(t *testing.T) { } func TestExecuteAnalyzeMissingConfigUsesRunStageLoadingPath(t *testing.T) { - deps, missingPath, executor := guardedMissingPipelineDiscovery(t) + deps, _, executor := guardedMissingPipelineDiscovery(t) var stdout bytes.Buffer var stderr bytes.Buffer code := executeWithDependencies([]string{"analyze", "2026-05-03"}, &stdout, &stderr, deps, executor) @@ -282,8 +282,7 @@ func TestExecuteAnalyzeMissingConfigUsesRunStageLoadingPath(t *testing.T) { if stdout.Len() != 0 { t.Fatalf("stdout = %q, want empty", stdout.String()) } - if got := stderr.String(); !strings.Contains(got, "analyze: no pipeline config path provided") || - !strings.Contains(got, "no default pipeline config found") || !strings.Contains(got, missingPath) { + if got := stderr.String(); !strings.Contains(got, "source.missing") { t.Fatalf("stderr = %q, want pipeline discovery error", stderr.String()) } } @@ -376,7 +375,7 @@ func TestExecutePublishUnknownArtifactFailsValidation(t *testing.T) { } func TestExecutePublishMissingConfigUsesRunStageLoadingPath(t *testing.T) { - deps, missingPath, executor := guardedMissingPipelineDiscovery(t) + deps, _, executor := guardedMissingPipelineDiscovery(t) var stdout bytes.Buffer var stderr bytes.Buffer code := executeWithDependencies([]string{"publish", "2026-05-03"}, &stdout, &stderr, deps, executor) @@ -386,8 +385,7 @@ func TestExecutePublishMissingConfigUsesRunStageLoadingPath(t *testing.T) { if stdout.Len() != 0 { t.Fatalf("stdout = %q, want empty", stdout.String()) } - if got := stderr.String(); !strings.Contains(got, "publish: no pipeline config path provided") || - !strings.Contains(got, "no default pipeline config found") || !strings.Contains(got, missingPath) { + if got := stderr.String(); !strings.Contains(got, "source.missing") { t.Fatalf("stderr = %q, want pipeline discovery error", stderr.String()) } } diff --git a/internal/app/command_dependencies.go b/internal/app/command_dependencies.go index 6076675..7ef8a58 100644 --- a/internal/app/command_dependencies.go +++ b/internal/app/command_dependencies.go @@ -4,14 +4,14 @@ import ( "context" "io" - configkit "go.maximumdirect.net/configkit" "gitea.maximumdirect.net/eric/narratio/internal/adapters/storage" "gitea.maximumdirect.net/eric/narratio/internal/config" + configkit "go.maximumdirect.net/configkit" ) type commandDependencies struct { diagnostics io.Writer - resolvePipelineConfigPath func(string) (string, error) + pipelineSearch *configkit.Search loadPipelineConfig func(string, config.PipelineLoadOptions) (*config.PipelineConfig, error) downloadObjectToTemp func(context.Context, storage.ObjectStore, string, string) (string, error) loadSecretsDir func(string) (*configkit.SecretsReport, error) @@ -26,9 +26,6 @@ func normalizeCommandDependencies(deps commandDependencies) commandDependencies if deps.diagnostics == nil { deps.diagnostics = io.Discard } - if deps.resolvePipelineConfigPath == nil { - deps.resolvePipelineConfigPath = resolvePipelineConfigPath - } if deps.loadPipelineConfig == nil { deps.loadPipelineConfig = config.LoadPipelineWithOptions } diff --git a/internal/app/commands_test.go b/internal/app/commands_test.go index 0c59a3c..f0e1ec7 100644 --- a/internal/app/commands_test.go +++ b/internal/app/commands_test.go @@ -13,7 +13,6 @@ import ( "time" "gitea.maximumdirect.net/eric/narratio/internal/artifacts" - "gitea.maximumdirect.net/eric/narratio/internal/config" "gitea.maximumdirect.net/eric/narratio/internal/manifest" ) @@ -70,7 +69,7 @@ func TestExecuteMissingRequiredFlags(t *testing.T) { {name: "resume removed", args: []string{"resume"}, want: `unknown command: "resume"`}, {name: "run-stage missing name", args: []string{"run-stage", "--config", "a", "--session", "b"}, want: "run-stage: expected stage name and session_id"}, {name: "run-stage missing session", args: []string{"run-stage", "polish"}, want: "run-stage: expected stage name and session_id"}, - {name: "run missing config uses defaults", args: []string{"run", "2026-05-03", "--session", "session.yml"}, want: "run: no pipeline config path provided", controlledDiscovery: true}, + {name: "run missing config uses defaults", args: []string{"run", "2026-05-03", "--session", "session.yml"}, want: "source.missing", controlledDiscovery: true}, } for _, tc := range cases { @@ -80,9 +79,8 @@ func TestExecuteMissingRequiredFlags(t *testing.T) { deps := commandDependencies{} executor := pipelineExecutor(executePlan) - missingPath := "" if tc.controlledDiscovery { - deps, missingPath, executor = guardedMissingPipelineDiscovery(t) + deps, _, executor = guardedMissingPipelineDiscovery(t) } code := executeWithDependencies(tc.args, &stdout, &stderr, deps, executor) if code == 0 { @@ -94,11 +92,7 @@ func TestExecuteMissingRequiredFlags(t *testing.T) { if !strings.Contains(stderr.String(), tc.want) { t.Fatalf("stderr = %q, want to contain %q", stderr.String(), tc.want) } - if tc.controlledDiscovery { - if got := stderr.String(); !strings.Contains(got, "no default pipeline config found") || !strings.Contains(got, missingPath) { - t.Fatalf("stderr = %q, want controlled searched path %q", got, missingPath) - } - } + }) } } @@ -267,7 +261,7 @@ inputs: var stdout bytes.Buffer var stderr bytes.Buffer - code := Execute([]string{"run-stage", "polish", sessionID, "--config", pipelinePath, "--campaign-file", campaignPath, "--session", sessionPath, "--force"}, &stdout, &stderr) + code := Execute([]string{"run-stage", "polish", sessionID, "--config", pipelinePath, "--campaign-file", campaignPath, "--session", sessionPath, "--force", "--secrets-dir", secretsDir}, &stdout, &stderr) if code != 0 { t.Fatalf("exit code = %d, want 0; stderr=%q", code, stderr.String()) } @@ -375,11 +369,7 @@ func TestExecuteUsesDefaultPipelineConfigPathWhenConfigFlagOmitted(t *testing.T) defer srv.Close() pipelinePath, campaignPath, sessionPath := writeValidConfigFiles(t, workspaceRoot, srv.URL) - originalDefaults := append([]string(nil), config.DefaultPipelineConfigSearchPaths...) - config.DefaultPipelineConfigSearchPaths = []string{pipelinePath} - defer func() { - config.DefaultPipelineConfigSearchPaths = originalDefaults - }() + withDefaultPipelineCampaignConfigs(t, pipelinePath, campaignPath) _ = campaignPath var stdout bytes.Buffer diff --git a/internal/app/config_commands_test.go b/internal/app/config_commands_test.go index f42b152..1675bc3 100644 --- a/internal/app/config_commands_test.go +++ b/internal/app/config_commands_test.go @@ -11,8 +11,8 @@ import ( "strings" "testing" - configkit "go.maximumdirect.net/configkit" "gitea.maximumdirect.net/eric/narratio/internal/config" + configkit "go.maximumdirect.net/configkit" ) func TestConfigCommandHelpAndDispatch(t *testing.T) { @@ -339,6 +339,7 @@ func TestConfigDiffBootstrapsSecretsOnceForBothOverlays(t *testing.T) { } mustWriteSecretFile(t, filepath.Join(secretsDir, secretName), "diff-secret-must-not-appear\n") bootstrapCalls := 0 + var loadedPaths []string var diagnostics bytes.Buffer deps := commandDependencies{loadSecretsDir: func(got string) (*configkit.SecretsReport, error) { bootstrapCalls++ @@ -347,15 +348,24 @@ func TestConfigDiffBootstrapsSecretsOnceForBothOverlays(t *testing.T) { } return configkit.LoadSecretsIntoEnvironment(got) }, diagnostics: &diagnostics} + search := configkit.SearchPaths(pipelinePath) + deps.pipelineSearch = &search + deps.loadPipelineConfig = func(path string, opts config.PipelineLoadOptions) (*config.PipelineConfig, error) { + loadedPaths = append(loadedPaths, path) + return config.LoadPipelineWithOptions(path, opts) + } if err := configDiffWithDependencies(context.Background(), []string{ - "production", "testing", "--config", pipelinePath, "--secrets-dir", secretsDir, + "production", "testing", "--secrets-dir", secretsDir, }, io.Discard, deps); err != nil { t.Fatalf("config diff: %v", err) } if bootstrapCalls != 1 { t.Fatalf("bootstrap calls = %d, want 1", bootstrapCalls) } + if len(loadedPaths) != 2 || loadedPaths[0] != "" || loadedPaths[1] != pipelinePath { + t.Fatalf("pipeline load paths = %q, want discovery then selected root %q", loadedPaths, pipelinePath) + } if got := diagnostics.String(); strings.Count(got, `name="`+secretName+`"`) != 1 { t.Fatalf("bootstrap report count = %d, want 1; diagnostics = %q", strings.Count(got, `name="`+secretName+`"`), got) } diff --git a/internal/app/config_diff.go b/internal/app/config_diff.go index ee09197..f73aecb 100644 --- a/internal/app/config_diff.go +++ b/internal/app/config_diff.go @@ -179,7 +179,7 @@ func resolveConfigDiff(request configDiffRequest, deps commandDependencies) (*co if err != nil { return nil, fmt.Errorf("load left pipeline overlay: %w", err) } - _, rightPipeline, err := loadPipelineConfigWithDependencies(request.flags.pipelinePath.value, config.PipelineLoadOptions{Overlay: &request.rightOverlay}, request.flags.secretsDir.pointer(), deps) + _, rightPipeline, err := loadPipelineConfigWithDependencies(pipelinePath, config.PipelineLoadOptions{Overlay: &request.rightOverlay}, request.flags.secretsDir.pointer(), deps) if err != nil { return nil, fmt.Errorf("load right pipeline overlay: %w", err) } diff --git a/internal/app/config_loader.go b/internal/app/config_loader.go index de75556..207d373 100644 --- a/internal/app/config_loader.go +++ b/internal/app/config_loader.go @@ -163,21 +163,22 @@ func loadPipelineCampaignConfigWithDependencies(pipelineFlag, campaignFlag, camp func loadPipelineConfigWithDependencies(pipelineFlag string, opts config.PipelineLoadOptions, secretsDir *string, deps commandDependencies) (string, *config.PipelineConfig, error) { deps = normalizeCommandDependencies(deps) - resolvedPipelinePath, err := deps.resolvePipelineConfigPath(pipelineFlag) - if err != nil { - return "", nil, err - } if !deps.secretsBootstrap.selectionSet { deps.secretsBootstrap.selection = secretsDir deps.secretsBootstrap.selectionSet = true } - if err := deps.secretsBootstrap.load(resolvedPipelinePath, deps.loadSecretsDir, deps.diagnostics); err != nil { + if err := deps.secretsBootstrap.load(deps.loadSecretsDir, deps.diagnostics); err != nil { return "", nil, err } - pipelineCfg, err := deps.loadPipelineConfig(resolvedPipelinePath, opts) + opts.Search = deps.pipelineSearch + pipelineCfg, err := deps.loadPipelineConfig(strings.TrimSpace(pipelineFlag), opts) if err != nil { return "", nil, err } + resolvedPipelinePath := config.EffectivePipelineRootPath(pipelineCfg) + if resolvedPipelinePath == "" { + resolvedPipelinePath = strings.TrimSpace(pipelineFlag) + } return resolvedPipelinePath, pipelineCfg, nil } diff --git a/internal/app/extract_lifecycle_test.go b/internal/app/extract_lifecycle_test.go index 69f4c64..423677e 100644 --- a/internal/app/extract_lifecycle_test.go +++ b/internal/app/extract_lifecycle_test.go @@ -13,7 +13,6 @@ import ( "testing" "time" - configkit "go.maximumdirect.net/configkit" "gitea.maximumdirect.net/eric/narratio/internal/adapters/notarius" "gitea.maximumdirect.net/eric/narratio/internal/artifactmodel" "gitea.maximumdirect.net/eric/narratio/internal/artifactpolicy" @@ -21,6 +20,7 @@ import ( "gitea.maximumdirect.net/eric/narratio/internal/config" "gitea.maximumdirect.net/eric/narratio/internal/manifest" "gitea.maximumdirect.net/eric/narratio/internal/stage" + configkit "go.maximumdirect.net/configkit" ) type materializingNotariusRunner struct { diff --git a/internal/app/operator_helpers_test.go b/internal/app/operator_helpers_test.go index b9f7f9f..66f12b0 100644 --- a/internal/app/operator_helpers_test.go +++ b/internal/app/operator_helpers_test.go @@ -164,7 +164,7 @@ func TestExecuteSessionInitRequiresSessionID(t *testing.T) { } func TestExecuteSessionInitMissingDefaultConfigReportsSearchedPaths(t *testing.T) { - deps, missingPath, executor := guardedMissingPipelineDiscovery(t) + deps, _, executor := guardedMissingPipelineDiscovery(t) var stdout bytes.Buffer var stderr bytes.Buffer @@ -175,8 +175,7 @@ func TestExecuteSessionInitMissingDefaultConfigReportsSearchedPaths(t *testing.T if stdout.Len() != 0 { t.Fatalf("stdout = %q, want empty", stdout.String()) } - if got := stderr.String(); !strings.Contains(got, "session init: no pipeline config path provided") || - !strings.Contains(got, "no default pipeline config found") || !strings.Contains(got, missingPath) { + if got := stderr.String(); !strings.Contains(got, "source.missing") { t.Fatalf("stderr = %q, want default pipeline searched-path error", stderr.String()) } } @@ -203,7 +202,7 @@ func TestExecuteSessionInitRemoteLoadsSecretsBeforeObjectStoreInit(t *testing.T) var stdout bytes.Buffer var stderr bytes.Buffer - code := executeWithDependencies([]string{"session", "init", "2026-06-07", "--remote"}, &stdout, &stderr, deps, executePlan) + code := executeWithDependencies([]string{"session", "init", "2026-06-07", "--remote", "--secrets-dir", secretsDir}, &stdout, &stderr, deps, executePlan) if code != 0 { t.Fatalf("exit code = %d, want 0; stderr=%q", code, stderr.String()) } @@ -436,7 +435,7 @@ inputs: var stdout bytes.Buffer var stderr bytes.Buffer - code := executeWithDependencies([]string{"session", "validate", "2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session", sessionPath}, &stdout, &stderr, deps, executePlan) + code := executeWithDependencies([]string{"session", "validate", "2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session", sessionPath, "--secrets-dir", secretsDir}, &stdout, &stderr, deps, executePlan) if code != 0 { t.Fatalf("exit code = %d, want 0; stdout=%q stderr=%q", code, stdout.String(), stderr.String()) } @@ -800,11 +799,18 @@ func TestExecuteTopLevelLockAndUnlockAreRemoved(t *testing.T) { func withDefaultPipelineCampaignConfigs(t *testing.T, pipelinePath, campaignPath string) { t.Helper() - origPipelineDefaults := append([]string(nil), config.DefaultPipelineConfigSearchPaths...) - config.DefaultPipelineConfigSearchPaths = []string{pipelinePath} - t.Cleanup(func() { - config.DefaultPipelineConfigSearchPaths = origPipelineDefaults - }) + dir := t.TempDir() + if err := os.Link(pipelinePath, filepath.Join(dir, "narratio.yml")); err != nil { + t.Fatal(err) + } + original, err := os.Getwd() + if err != nil { + t.Fatal(err) + } + if err := os.Chdir(dir); err != nil { + t.Fatal(err) + } + t.Cleanup(func() { _ = os.Chdir(original) }) _ = campaignPath } diff --git a/internal/app/pipeline_config_path.go b/internal/app/pipeline_config_path.go deleted file mode 100644 index 457a55c..0000000 --- a/internal/app/pipeline_config_path.go +++ /dev/null @@ -1,49 +0,0 @@ -package app - -import ( - "errors" - "fmt" - "os" - "path/filepath" - "strings" - - "gitea.maximumdirect.net/eric/narratio/internal/config" -) - -func resolvePipelineConfigPath(flagValue string) (string, error) { - return resolvePipelineConfigPathWithCandidates(flagValue, config.DefaultPipelineConfigSearchPaths) -} - -func resolvePipelineConfigPathWithCandidates(flagValue string, candidates []string) (string, error) { - if explicit := strings.TrimSpace(flagValue); explicit != "" { - return explicit, nil - } - - 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 filepath.Clean(path), nil - } - if errors.Is(err, os.ErrNotExist) { - continue - } - return "", fmt.Errorf("check default pipeline config %q: %w", path, err) - } - - if len(ordered) == 0 { - return "", fmt.Errorf("no pipeline config path provided and no default locations configured") - } - return "", fmt.Errorf( - "no pipeline config path provided and no default pipeline config found; searched: %s", - strings.Join(ordered, ", "), - ) -} diff --git a/internal/app/pipeline_config_path_test.go b/internal/app/pipeline_config_path_test.go deleted file mode 100644 index 968be9a..0000000 --- a/internal/app/pipeline_config_path_test.go +++ /dev/null @@ -1,117 +0,0 @@ -package app - -import ( - "bytes" - "context" - "errors" - "os" - "path/filepath" - "strings" - "testing" - - configkit "go.maximumdirect.net/configkit" - "gitea.maximumdirect.net/eric/narratio/internal/config" -) - -func TestResolvePipelineConfigPathWithCandidatesExplicitWins(t *testing.T) { - got, err := resolvePipelineConfigPathWithCandidates(" ./custom/pipeline.yml ", []string{"/a", "/b"}) - if err != nil { - t.Fatalf("resolvePipelineConfigPathWithCandidates() error = %v", err) - } - if got != "./custom/pipeline.yml" { - t.Fatalf("resolved path = %q, want explicit path", got) - } -} - -func TestResolvePipelineConfigPathWithCandidatesUsesFirstExisting(t *testing.T) { - dir := t.TempDir() - first := filepath.Join(dir, "first.yml") - second := filepath.Join(dir, "second.yml") - if err := os.WriteFile(second, []byte("workspace:\n root: ./tmp\n"), 0o644); err != nil { - t.Fatalf("write second default: %v", err) - } - - got, err := resolvePipelineConfigPathWithCandidates("", []string{first, second}) - if err != nil { - t.Fatalf("resolvePipelineConfigPathWithCandidates() error = %v", err) - } - if got != filepath.Clean(second) { - t.Fatalf("resolved path = %q, want %q", got, filepath.Clean(second)) - } -} - -func TestResolvePipelineConfigPathWithCandidatesPrecedence(t *testing.T) { - dir := t.TempDir() - first := filepath.Join(dir, "first.yml") - second := filepath.Join(dir, "second.yml") - if err := os.WriteFile(first, []byte("workspace:\n root: ./tmp\n"), 0o644); err != nil { - t.Fatalf("write first default: %v", err) - } - if err := os.WriteFile(second, []byte("workspace:\n root: ./tmp\n"), 0o644); err != nil { - t.Fatalf("write second default: %v", err) - } - - got, err := resolvePipelineConfigPathWithCandidates("", []string{first, second}) - if err != nil { - t.Fatalf("resolvePipelineConfigPathWithCandidates() error = %v", err) - } - if got != filepath.Clean(first) { - t.Fatalf("resolved path = %q, want first candidate %q", got, filepath.Clean(first)) - } -} - -func TestResolvePipelineConfigPathWithCandidatesMissing(t *testing.T) { - directory := t.TempDir() - first := filepath.Join(directory, "one.yml") - second := filepath.Join(directory, "two.yml") - _, err := resolvePipelineConfigPathWithCandidates("", []string{first, second}) - if err == nil { - t.Fatal("expected error, got nil") - } - if !strings.Contains(err.Error(), "no default pipeline config found") { - t.Fatalf("error = %q, want missing-defaults context", err.Error()) - } - if !strings.Contains(err.Error(), first) || !strings.Contains(err.Error(), second) { - t.Fatalf("error = %q, want controlled searched paths", err.Error()) - } -} - -func TestExecuteMissingExplicitConfigDiscoversExistingDefaultBeforeLoading(t *testing.T) { - directory := t.TempDir() - missing := filepath.Join(directory, "missing.yml") - existing := filepath.Join(directory, "pipeline.yml") - if err := os.WriteFile(existing, []byte("workspace:\n root: ./tmp\n"), 0o644); err != nil { - t.Fatalf("write default pipeline: %v", err) - } - - controlledErr := errors.New("controlled pipeline loader boundary") - loadedPath := "" - deps := commandDependenciesForPipelineCandidates(missing, existing) - deps.loadPipelineConfig = func(path string, _ config.PipelineLoadOptions) (*config.PipelineConfig, error) { - loadedPath = path - return nil, controlledErr - } - deps.loadSecretsDir = func(string) (*configkit.SecretsReport, error) { - t.Fatal("absent conventional secrets directory unexpectedly invoked bootstrap loader") - return nil, nil - } - - var stdout bytes.Buffer - var stderr bytes.Buffer - code := executeWithDependencies([]string{"config", "validate"}, &stdout, &stderr, deps, func(context.Context, *config.Config, BoundedPlan, RunOptions) (*RunSummary, error) { - t.Fatal("config validation unexpectedly invoked pipeline executor") - return nil, nil - }) - if code == 0 { - t.Fatal("exit code = 0, want controlled loader failure") - } - if loadedPath != existing { - t.Fatalf("loader path = %q, want discovered default %q", loadedPath, existing) - } - if stdout.Len() != 0 { - t.Fatalf("stdout = %q, want empty", stdout.String()) - } - if !strings.Contains(stderr.String(), controlledErr.Error()) { - t.Fatalf("stderr = %q, want controlled loader error", stderr.String()) - } -} diff --git a/internal/app/provider_credentials_integration_test.go b/internal/app/provider_credentials_integration_test.go index cb22be6..4c3ccfc 100644 --- a/internal/app/provider_credentials_integration_test.go +++ b/internal/app/provider_credentials_integration_test.go @@ -90,7 +90,7 @@ inputs: `) var stdout, stderr bytes.Buffer - if code := Execute([]string{"config", "show", "--config", configPath}, &stdout, &stderr); code != 0 { + if code := Execute([]string{"config", "show", "--config", configPath, "--secrets-dir", secretsDir}, &stdout, &stderr); code != 0 { t.Fatalf("config show exit = %d; stderr=%q", code, stderr.String()) } if !strings.Contains(stdout.String(), sharedProviderName) || !strings.Contains(stdout.String(), auditaOnlyName) { @@ -104,7 +104,7 @@ inputs: mustWriteTestFile(t, basePath, `{"schema":"seriatim-intermediate","segments":[]}`) seedCurrentBuiltInEvidence(t, manifestPath, "2026-05-03", "sample-campaign", "merge", artifacts.TranscriptOutputKindBase, artifacts.ArtifactTranscriptBase, basePath) mustWriteTestFile(t, filepath.Join(workRoot, "inputs", "glossary.yml"), "[]\n") - args := []string{"--config", configPath, "--campaign-file", campaignPath, "--session", sessionPath} + args := []string{"--secrets-dir", secretsDir, "--config", configPath, "--campaign-file", campaignPath, "--session", sessionPath} stdout.Reset() stderr.Reset() if code := Execute(append([]string{"run-stage", "polish", "2026-05-03", "--force"}, args...), &stdout, &stderr); code != 0 { diff --git a/internal/app/remote_session_test.go b/internal/app/remote_session_test.go index 2eee393..23e527f 100644 --- a/internal/app/remote_session_test.go +++ b/internal/app/remote_session_test.go @@ -10,10 +10,10 @@ import ( "strings" "testing" - configkit "go.maximumdirect.net/configkit" "gitea.maximumdirect.net/eric/narratio/internal/adapters/storage" "gitea.maximumdirect.net/eric/narratio/internal/artifacts" "gitea.maximumdirect.net/eric/narratio/internal/config" + configkit "go.maximumdirect.net/configkit" ) func TestExecuteRemoteSessionFallbackLoadsFromObjectStore(t *testing.T) { @@ -262,7 +262,7 @@ inputs: var stdout bytes.Buffer var stderr bytes.Buffer - code := executeWithDependencies([]string{"session", "plan", "2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath}, &stdout, &stderr, deps, executePlan) + code := executeWithDependencies([]string{"session", "plan", "2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--secrets-dir", secretsDir}, &stdout, &stderr, deps, executePlan) if code != 0 { t.Fatalf("exit code = %d, want 0; stdout=%q stderr=%q", code, stdout.String(), stderr.String()) } diff --git a/internal/app/restore_test.go b/internal/app/restore_test.go index 5b4ee51..270835e 100644 --- a/internal/app/restore_test.go +++ b/internal/app/restore_test.go @@ -179,9 +179,6 @@ func TestExecuteRestoreLoadsSecretsBeforeObjectStoreInit(t *testing.T) { secretsDir := filepath.Join(t.TempDir(), "secrets") mustWriteSecretFile(t, filepath.Join(secretsDir, accessKeyEnv), "test-access-key-id\n") mustWriteSecretFile(t, filepath.Join(secretsDir, secretKeyEnv), "test-secret-key\n") - if err := os.Symlink(secretsDir, filepath.Join(filepath.Dir(pipelinePath), "secrets")); err != nil { - t.Fatalf("link conventional secrets directory: %v", err) - } storeInitCalled := false deps := commandDependencies{newObjectStore: func(context.Context, *config.Config) (storage.ObjectStore, error) { @@ -226,6 +223,7 @@ func TestExecuteRestoreLoadsSecretsBeforeObjectStoreInit(t *testing.T) { "--campaign-file", campaignPath, "--session", sessionPath, "--dry-run", + "--secrets-dir", secretsDir, }, &stdout, &stderr, diff --git a/internal/app/secrets_bootstrap.go b/internal/app/secrets_bootstrap.go index 06c5e7d..7ecf049 100644 --- a/internal/app/secrets_bootstrap.go +++ b/internal/app/secrets_bootstrap.go @@ -18,12 +18,14 @@ type secretsBootstrap struct { called bool } -func (b *secretsBootstrap) load(pipelinePath string, loader func(string) (*configkit.SecretsReport, error), diagnostics io.Writer) error { +var conventionalSecretsDirectory = "/usr/local/etc/narratio/secrets" + +func (b *secretsBootstrap) load(loader func(string) (*configkit.SecretsReport, error), diagnostics io.Writer) error { if b == nil || b.called { return nil } b.called = true - directory, required, err := selectedSecretsDirectory(pipelinePath, b.selection) + directory, required, err := selectedSecretsDirectory(b.selection) if err != nil { return err } @@ -53,7 +55,7 @@ func (b *secretsBootstrap) load(pipelinePath string, loader func(string) (*confi return fmt.Errorf("bootstrap secrets directory: %w", loadErr) } -func selectedSecretsDirectory(pipelinePath string, selection *string) (string, bool, error) { +func selectedSecretsDirectory(selection *string) (string, bool, error) { if selection != nil { if strings.TrimSpace(*selection) == "" { return "", true, fmt.Errorf("--secrets-dir requires a non-empty path") @@ -64,9 +66,5 @@ func selectedSecretsDirectory(pipelinePath string, selection *string) (string, b } return filepath.Clean(path), true, nil } - root, err := filepath.Abs(pipelinePath) - if err != nil { - return "", false, fmt.Errorf("resolve pipeline path for secrets discovery: %w", err) - } - return filepath.Join(filepath.Dir(root), "secrets"), false, nil + return conventionalSecretsDirectory, false, nil } diff --git a/internal/app/secrets_bootstrap_test.go b/internal/app/secrets_bootstrap_test.go index fb63a3f..4b48931 100644 --- a/internal/app/secrets_bootstrap_test.go +++ b/internal/app/secrets_bootstrap_test.go @@ -9,8 +9,8 @@ import ( "strings" "testing" - configkit "go.maximumdirect.net/configkit" "gitea.maximumdirect.net/eric/narratio/internal/config" + configkit "go.maximumdirect.net/configkit" ) func TestPipelineLoadSkipsAbsentConventionalSecretsDirectory(t *testing.T) { @@ -39,6 +39,27 @@ func TestPipelineLoadSkipsAbsentConventionalSecretsDirectory(t *testing.T) { } } +func TestPipelineLoadUsesIndependentConventionalSecretsDirectory(t *testing.T) { + dir := t.TempDir() + previous := conventionalSecretsDirectory + conventionalSecretsDirectory = dir + t.Cleanup(func() { conventionalSecretsDirectory = previous }) + const name = "NARRATIO_TEST_CONVENTIONAL_SECRET" + restoreEnvAfterTest(t, name) + mustWriteSecretFile(t, filepath.Join(dir, name), "synthetic-value\n") + pipelinePath := filepath.Join(t.TempDir(), "pipeline.yml") + if err := os.WriteFile(pipelinePath, []byte("notarius:\n enabled: false\n credentials: ["+name+"]\n"), 0o644); err != nil { + t.Fatal(err) + } + _, pipeline, err := loadPipelineConfigWithDependencies(pipelinePath, config.PipelineLoadOptions{}, nil, commandDependencies{}) + if err != nil { + t.Fatal(err) + } + if got := pipeline.Notarius.Credentials[0].Value(); got != "synthetic-value" { + t.Fatal("conventional directory credential was not resolved") + } +} + func TestExplicitSecretsDirectoryIsResolvedFromCallerWorkingDirectory(t *testing.T) { callerDir := t.TempDir() selected := filepath.Join(callerDir, "credentials") @@ -54,7 +75,7 @@ func TestExplicitSecretsDirectoryIsResolvedFromCallerWorkingDirectory(t *testing } t.Cleanup(func() { _ = os.Chdir(original) }) - got, required, err := selectedSecretsDirectory(filepath.Join(t.TempDir(), "pipeline.yml"), stringPointer("credentials")) + got, required, err := selectedSecretsDirectory(stringPointer("credentials")) if err != nil { t.Fatalf("select secrets: %v", err) } @@ -103,7 +124,7 @@ func TestSecretsBootstrapPreservesExistingEnvironmentValues(t *testing.T) { bootstrap := &secretsBootstrap{selection: &directory, selectionSet: true} var diagnostics bytes.Buffer - if err := bootstrap.load(filepath.Join(t.TempDir(), "pipeline.yml"), configkit.LoadSecretsIntoEnvironment, &diagnostics); err != nil { + if err := bootstrap.load(configkit.LoadSecretsIntoEnvironment, &diagnostics); err != nil { t.Fatalf("bootstrap: %v", err) } if got := diagnostics.String(); !strings.Contains(got, `status="skipped_existing"`) || !strings.Contains(got, `existing_empty=true`) { @@ -128,7 +149,6 @@ func TestNotariusCredentialLoadsFromCommandSecretBootstrap(t *testing.T) { fileValue string wantValue string }{ - {name: "conventional", fileValue: "conventional-value", wantValue: "conventional-value"}, {name: "explicit", explicit: true, fileValue: "explicit-value", wantValue: "explicit-value"}, {name: "environment precedence", explicit: true, environment: "environment-value", fileValue: "file-value", wantValue: "environment-value"}, } @@ -170,7 +190,7 @@ func TestSecretsBootstrapPreservesSafePartialReportOnFailure(t *testing.T) { directory := t.TempDir() mustWriteSecretFile(t, filepath.Join(directory, "INVALID-NAME"), "must-not-appear\n") bootstrap := &secretsBootstrap{selection: &directory, selectionSet: true} - err := bootstrap.load(filepath.Join(t.TempDir(), "pipeline.yml"), configkit.LoadSecretsIntoEnvironment, io.Discard) + err := bootstrap.load(configkit.LoadSecretsIntoEnvironment, io.Discard) if err == nil { t.Fatal("bootstrap succeeded, want error") } diff --git a/internal/app/test_environment_test.go b/internal/app/test_environment_test.go index 99d91d1..1c9d4ed 100644 --- a/internal/app/test_environment_test.go +++ b/internal/app/test_environment_test.go @@ -6,10 +6,21 @@ import ( "path/filepath" "testing" - configkit "go.maximumdirect.net/configkit" "gitea.maximumdirect.net/eric/narratio/internal/config" + configkit "go.maximumdirect.net/configkit" ) +func TestMain(m *testing.M) { + dir, err := os.MkdirTemp("", "narratio-app-test-*") + if err != nil { + panic(err) + } + conventionalSecretsDirectory = filepath.Join(dir, "secrets") + code := m.Run() + _ = os.RemoveAll(dir) + os.Exit(code) +} + // restoreEnvAfterTest clears names for a test and restores both their prior // values and their set/unset state during cleanup. func restoreEnvAfterTest(t *testing.T, names ...string) { @@ -51,24 +62,14 @@ func mustWriteSecretFile(t *testing.T, path, value string) { } func commandDependenciesForPipelineCandidates(candidates ...string) commandDependencies { - ownedCandidates := append([]string(nil), candidates...) - return commandDependencies{resolvePipelineConfigPath: func(flagValue string) (string, error) { - return resolvePipelineConfigPathWithCandidates(flagValue, ownedCandidates) - }} + search := configkit.SearchPaths(candidates...) + return commandDependencies{pipelineSearch: &search} } func guardedMissingPipelineDiscovery(t *testing.T) (commandDependencies, string, pipelineExecutor) { t.Helper() candidate := filepath.Join(t.TempDir(), "missing-pipeline.yml") deps := commandDependenciesForPipelineCandidates(candidate) - deps.loadSecretsDir = func(string) (*configkit.SecretsReport, error) { - t.Fatal("secret bootstrap continued after pipeline discovery failed") - return nil, nil - } - deps.loadPipelineConfig = func(string, config.PipelineLoadOptions) (*config.PipelineConfig, error) { - t.Fatal("typed pipeline loading continued after pipeline discovery failed") - return nil, nil - } executor := func(context.Context, *config.Config, BoundedPlan, RunOptions) (*RunSummary, error) { t.Fatal("pipeline execution continued after pipeline discovery failed") return nil, nil diff --git a/internal/config/defaults.go b/internal/config/defaults.go index e950b7a..e8c0714 100644 --- a/internal/config/defaults.go +++ b/internal/config/defaults.go @@ -5,16 +5,14 @@ import "gitea.maximumdirect.net/eric/narratio/internal/artifactmodel" // Default filesystem locations for config lookup when config path flags are // omitted. Order is highest to lowest precedence. const ( - DefaultPipelineConfigPathUsrLocal = "/usr/local/etc/narratio/pipeline.yml" - DefaultPipelineConfigPathEtc = "/etc/narratio/pipeline.yml" - DefaultSessionConfigPathUsrLocal = "/usr/local/etc/narratio/session.yml" - DefaultSessionConfigPathEtc = "/etc/narratio/session.yml" - DefaultStorageS3RootPrefix = "dnd" - DefaultWorkspaceRoot = "/var/lib/narratio" - DefaultCampaignsRoot = "/usr/local/share/narratio/campaigns" - DefaultSpoolRoot = "/var/spool/narratio" - DefaultCacheRoot = "/var/cache/narratio" - DefaultCacheS3Audio = true + DefaultSessionConfigPathUsrLocal = "/usr/local/etc/narratio/session.yml" + DefaultSessionConfigPathEtc = "/etc/narratio/session.yml" + DefaultStorageS3RootPrefix = "dnd" + DefaultWorkspaceRoot = "/var/lib/narratio" + DefaultCampaignsRoot = "/usr/local/share/narratio/campaigns" + DefaultSpoolRoot = "/var/spool/narratio" + DefaultCacheRoot = "/var/cache/narratio" + DefaultCacheS3Audio = true DefaultWhisperXLanguage = "en" DefaultWhisperXTimeout = "30m" @@ -104,16 +102,6 @@ var DefaultPublishOutputs = []PublishOutputRule{ {Source: artifactmodel.SourceTranscriptFinalTrimmedMarkdown, Dest: artifactmodel.TranscriptPathFinalTrimmedMarkdown}, } -// DefaultPipelineConfigSearchPaths defines the default search order for -// pipeline.yml when callers do not provide an explicit path. -// -// Keep this in a variable so future defaults can be extended without changing -// call sites. -var DefaultPipelineConfigSearchPaths = []string{ - DefaultPipelineConfigPathUsrLocal, - DefaultPipelineConfigPathEtc, -} - // DefaultSessionConfigSearchPaths defines the default search order for // session.yml when callers do not provide an explicit path. // diff --git a/internal/config/load.go b/internal/config/load.go index d547818..5800cfc 100644 --- a/internal/config/load.go +++ b/internal/config/load.go @@ -8,6 +8,7 @@ import ( "strings" "gitea.maximumdirect.net/eric/narratio/internal/fileops" + configkit "go.maximumdirect.net/configkit" ) // LoadPipeline loads pipeline configuration from a YAML file with strict field checking. @@ -19,6 +20,7 @@ func LoadPipeline(path string) (*PipelineConfig, error) { // empty, and the exact name "default" select the base configuration. type PipelineLoadOptions struct { Overlay *string + Search *configkit.Search } // LoadPipelineWithOptions loads pipeline configuration with strict field @@ -28,7 +30,7 @@ func LoadPipelineWithOptions(path string, opts PipelineLoadOptions) (*PipelineCo if err != nil { return nil, err } - return finalizeLoadedPipeline(path, cfg) + return finalizeLoadedPipeline(EffectivePipelineRootPath(cfg), cfg) } // LoadPipelineOverlayPair resolves two overlays as independent ConfigKit loads. diff --git a/internal/config/pipeline_configkit.go b/internal/config/pipeline_configkit.go index da72931..c0f633e 100644 --- a/internal/config/pipeline_configkit.go +++ b/internal/config/pipeline_configkit.go @@ -34,7 +34,15 @@ type pipelineOverlaySelection struct { func loadPipelineWithConfigKit(path string, opts PipelineLoadOptions) (*PipelineConfig, error) { overlay := selectedPipelineOverlayName(opts) - result, err := loadPipelineConfigKitResult(path, overlay) + source := configkit.File(strings.TrimSpace(path)) + if strings.TrimSpace(path) == "" { + search := configkit.Searches(configkit.WorkingDirectory(), configkit.UserConfigDirectory(), configkit.UnixSystemDirectories()) + if opts.Search != nil { + search = *opts.Search + } + source = configkit.SearchConfig("narratio", search, configkit.SearchOptions{Required: true}) + } + result, err := loadPipelineConfigKitResult(source, overlay) if err != nil { return nil, describePipelineLoadError(err) } @@ -43,8 +51,8 @@ func loadPipelineWithConfigKit(path string, opts PipelineLoadOptions) (*Pipeline return &cfg, nil } -func loadPipelineConfigKitResult(path, overlay string) (*configkit.Result[PipelineConfig], error) { - return configkit.Load(configkit.File(path), configkit.Options[PipelineConfig]{ +func loadPipelineConfigKitResult(source configkit.Source, overlay string) (*configkit.Result[PipelineConfig], error) { + return configkit.Load(source, configkit.Options[PipelineConfig]{ Imports: true, Overlay: overlay, Definition: configkit.Definition[PipelineConfig]{ diff --git a/internal/config/pipeline_discovery_test.go b/internal/config/pipeline_discovery_test.go new file mode 100644 index 0000000..2032f83 --- /dev/null +++ b/internal/config/pipeline_discovery_test.go @@ -0,0 +1,87 @@ +package config + +import ( + "os" + "path/filepath" + "strings" + "testing" + + configkit "go.maximumdirect.net/configkit" +) + +func TestPipelineDiscoverySelectsFirstSourceAndExplicitFileWins(t *testing.T) { + dir := t.TempDir() + first := filepath.Join(dir, "first.yml") + second := filepath.Join(dir, "second.yml") + for path, root := range map[string]string{first: "/first", second: "/second"} { + if err := os.WriteFile(path, []byte("workspace:\n root: "+root+"\n"), 0o644); err != nil { + t.Fatal(err) + } + } + search := configkit.SearchPaths(first, second) + for _, test := range []struct{ explicit, root, path string }{ + {"", "/first", first}, + {second, "/second", second}, + } { + cfg, err := LoadPipelineWithOptions(test.explicit, PipelineLoadOptions{Search: &search}) + if err != nil { + t.Fatal(err) + } + if cfg.Workspace.Root != test.root || EffectivePipelineRootPath(cfg) != test.path { + t.Fatalf("selected root = %q, source = %q", cfg.Workspace.Root, EffectivePipelineRootPath(cfg)) + } + } + if _, err := LoadPipelineWithOptions(filepath.Join(dir, "missing.yml"), PipelineLoadOptions{Search: &search}); err == nil { + t.Fatal("missing explicit file fell back to search") + } + if err := os.WriteFile(first, []byte("workspace: [invalid\n"), 0o644); err != nil { + t.Fatal(err) + } + if _, err := LoadPipelineWithOptions("", PipelineLoadOptions{Search: &search}); err == nil || !strings.Contains(err.Error(), first) { + t.Fatalf("invalid selected source should fail: %v", err) + } + missing := configkit.SearchPaths(filepath.Join(dir, "missing.yml")) + if _, err := LoadPipelineWithOptions("", PipelineLoadOptions{Search: &missing}); err == nil { + t.Fatal("empty required search succeeded") + } +} + +func TestPipelineDiscoveryUsesWorkingDirectoryBeforeUserConfig(t *testing.T) { + working := t.TempDir() + user := t.TempDir() + t.Setenv("XDG_CONFIG_HOME", user) + original, err := os.Getwd() + if err != nil { + t.Fatal(err) + } + if err := os.Chdir(working); err != nil { + t.Fatal(err) + } + t.Cleanup(func() { _ = os.Chdir(original) }) + userDir := filepath.Join(user, "narratio") + if err := os.MkdirAll(userDir, 0o755); err != nil { + t.Fatal(err) + } + userPath := filepath.Join(userDir, "config.yml") + localPath := filepath.Join(working, "narratio.yml") + for path, root := range map[string]string{userPath: "/user", localPath: "/working"} { + if err := os.WriteFile(path, []byte("workspace:\n root: "+root+"\n"), 0o644); err != nil { + t.Fatal(err) + } + } + search := configkit.Searches(configkit.WorkingDirectory(), configkit.UserConfigDirectory()) + for _, want := range []string{localPath, userPath} { + cfg, err := LoadPipelineWithOptions("", PipelineLoadOptions{Search: &search}) + if err != nil { + t.Fatal(err) + } + if got := EffectivePipelineRootPath(cfg); got != want { + t.Fatalf("selected %q, want %q", got, want) + } + if want == localPath { + if err := os.Remove(localPath); err != nil { + t.Fatal(err) + } + } + } +}