From 0cbc2b39cb322bc2180f8336ace21930d883dc89 Mon Sep 17 00:00:00 2001 From: Eric Rakestraw Date: Sun, 22 Nov 2026 03:51:08 +0000 Subject: [PATCH] Wire native pipeline run commands and invocation-wide overrides --- docs/roadmap/implementation.md | 21 + internal/cli/application.go | 4 +- internal/cli/application_test.go | 2 +- internal/cli/catalog.go | 100 +- internal/cli/native_process_test.go | 33 + internal/cli/options.go | 34 + internal/cli/pipeline_commands.go | 5 +- internal/cli/pipeline_run.go | 152 ++ internal/cli/pipeline_run_test.go | 459 ++++++ internal/cli/reference_selectors.go | 588 ++++++++ internal/cli/registries.go | 59 + internal/cli/run.go | 1232 +---------------- internal/cli/run_input.go | 78 ++ internal/cli/run_output.go | 74 + internal/cli/session.go | 9 + internal/core/config/effective_config.go | 4 +- .../config/effective_config_contract_test.go | 2 +- internal/framework/pipeline/profile.go | 11 +- internal/framework/pipeline/profile_test.go | 6 +- 19 files changed, 1529 insertions(+), 1344 deletions(-) create mode 100644 internal/cli/native_process_test.go create mode 100644 internal/cli/options.go create mode 100644 internal/cli/pipeline_run.go create mode 100644 internal/cli/pipeline_run_test.go create mode 100644 internal/cli/reference_selectors.go create mode 100644 internal/cli/registries.go create mode 100644 internal/cli/run_input.go create mode 100644 internal/cli/run_output.go diff --git a/docs/roadmap/implementation.md b/docs/roadmap/implementation.md index 81672c71..8a36833b 100644 --- a/docs/roadmap/implementation.md +++ b/docs/roadmap/implementation.md @@ -1550,6 +1550,27 @@ partial profile, no-credential/no-transcript operation, and useful scope context ## Stage 33: Wire Native Pipeline Run Inputs And Runtime Composition +**Status:** Complete. Installed native `pipeline run` and its CommandKit `run` +alias with typed required input, literal repeated lanes, output/cache selection, +reference binds/unbinds, debug controls, and native resource/execution overrides. +One captured application result and engine serve preflight and every model role. +Reference expansion preserves specificity, required slots, generated conflicts, +and CLI/config file origins. Generated sessions remain input-based; explicit +empty profile/session/reasoning and false/zero inputs preserve native behavior. +Debug allocation precedes engine/preflight, which precedes source execution. +Removed the old run dispatcher/parsers/help, retired flags, and legacy runtime +client/asset construction. Retained confined publication helpers for the next +reporting integration. Real-kit/fake-provider run/reference/session/override tests, +focused pipeline and configuration tests, supporting preflight/assets/contracts/ +cache/debug/generic/Seriatim tests, focused vet/race checks, and diff checks pass. +Full CLI/executable checks, including the new subprocess route tests, remain +blocked at setup by `run_result.go` importing the removed reporting package and +old ConfigKit importing YAML's removed `plugin/limit`. The full profile test file +also retains retired `CorrectionProtocol` fixtures. Those checks are not claimed +passing; Stages 34–35 own their remaining integration and fixture cleanup, and +final assembled verification must execute them. Native completion reporting and +publication/presentation failure outcomes remain Stage 34. + **Goal:** the executable runs the migrated pipeline through one native handler. **Inspect/change:** CLI application/pipeline commands, `run.go`, catalog, diff --git a/internal/cli/application.go b/internal/cli/application.go index 0d0e1874..7c8d1ae4 100644 --- a/internal/cli/application.go +++ b/internal/cli/application.go @@ -15,6 +15,7 @@ import ( // NewApplication declares the native command surface. Acquisition is deferred to // the selected handler; construction performs no configuration or engine work. func NewApplication(version string, options Options) (*commandkit.App, error) { + options = defaultOptions(options) newState := func() *nativeLoaders { state := &nativeLoaders{lookupEnv: options.LookupEnv, runtime: options.LLMRuntime} if options.LLMClient != nil { @@ -41,7 +42,8 @@ func NewApplication(version string, options Options) (*commandkit.App, error) { definitions = ckprompt.WithNamespaces(definitions, promptkit.Namespaces{}) declarations := []commandkit.Declaration{ commandkit.Completion("notarius"), - pipelineCommands(newState, func() (pipeline.ModuleCatalog, error) { return effectiveCatalog(options) }), + pipelineCommands(newState, func() (pipeline.ModuleCatalog, error) { return effectiveCatalog(options) }, pipelineRunCommand(version, newState, func() (pipeline.Registries, error) { return effectiveRegistries(options) }, options)), + commandkit.Alias("run", "pipeline run"), ckconfig.Command(ckconfig.Suite[struct{}, *nativeLoaders]{ NewState: newState, CheckInput: func(_ string, selection ckconfig.Selection, _ struct{}) error { diff --git a/internal/cli/application_test.go b/internal/cli/application_test.go index 403fe8d7..70410b4a 100644 --- a/internal/cli/application_test.go +++ b/internal/cli/application_test.go @@ -17,7 +17,7 @@ func TestNativeApplicationBuiltinsNeedNoConfiguration(t *testing.T) { if err != nil { t.Fatal(err) } - for _, args := range [][]string{{"--help"}, {"help"}, {"version"}, {"completion", "bash"}, {"completion", "zsh"}, {"completion", "fish"}, {"config", "--help"}, {"pipeline", "--help"}, {"pipeline", "validate", "--help"}} { + for _, args := range [][]string{{"--help"}, {"help"}, {"version"}, {"completion", "bash"}, {"completion", "zsh"}, {"completion", "fish"}, {"config", "--help"}, {"pipeline", "--help"}, {"pipeline", "validate", "--help"}, {"pipeline", "run", "--help"}, {"run", "--help"}} { var out, diagnostics bytes.Buffer outcome := app.Execute(context.Background(), args, commandkit.Streams{Out: &out, Err: &diagnostics}) if outcome.Status != 0 || out.Len() == 0 { diff --git a/internal/cli/catalog.go b/internal/cli/catalog.go index 905d82da..0edf8ee6 100644 --- a/internal/cli/catalog.go +++ b/internal/cli/catalog.go @@ -1,13 +1,8 @@ package cli import ( - "context" "fmt" - "gitea.maximumdirect.net/eric/notarius/internal/core/artifacts" - "gitea.maximumdirect.net/eric/notarius/internal/core/config" - "gitea.maximumdirect.net/eric/notarius/internal/framework/contracts" - "gitea.maximumdirect.net/eric/notarius/internal/framework/llm" "gitea.maximumdirect.net/eric/notarius/internal/framework/pipeline" dndregister "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/register" genericregister "gitea.maximumdirect.net/eric/notarius/internal/modules/generic/register" @@ -16,7 +11,6 @@ import ( type productionComponents struct { registries pipeline.Registries - assets *llm.AssetRegistry } func newProductionComponents() (productionComponents, error) { @@ -32,7 +26,6 @@ func newProductionComponents() (productionComponents, error) { ValidatorChains: pipeline.NewValidatorChainRegistry(), Outputs: pipeline.NewOutputEncoderRegistry(), } - assets := llm.NewAssetRegistry() registrars := []struct { name string register func(pipeline.Registries) error @@ -46,7 +39,7 @@ func newProductionComponents() (productionComponents, error) { return productionComponents{}, fmt.Errorf("register %s module family: %w", registrar.name, err) } } - return productionComponents{registries: registries, assets: assets}, nil + return productionComponents{registries: registries}, nil } func productionRegistries() (pipeline.Registries, error) { @@ -62,11 +55,6 @@ func productionCatalog() (pipeline.ModuleCatalog, error) { return catalogFromRegistries(registries), nil } -func productionPromptAssets() (*llm.AssetRegistry, error) { - components, err := newProductionComponents() - return components.assets, err -} - func effectiveCatalog(opts Options) (pipeline.ModuleCatalog, error) { if !isEmptyCatalog(opts.Catalog) { return opts.Catalog, nil @@ -86,89 +74,3 @@ func effectiveRegistries(opts Options) (pipeline.Registries, error) { } return productionRegistries() } - -func catalogFromRegistries(registries pipeline.Registries) pipeline.ModuleCatalog { - return pipeline.ModuleCatalog{ - Inputs: registries.Inputs, - Chunkers: registries.Chunkers, - ArtifactCodecs: registries.ArtifactCodecs, - ArtifactEvidence: registries.ArtifactEvidence, - Extractors: registries.Extractors, - Mergers: registries.Mergers, - Normalizers: registries.Normalizers, - Validators: registries.Validators, - ValidatorChains: registries.ValidatorChains, - Outputs: registries.Outputs, - } -} - -func registriesFromCatalog(catalog pipeline.ModuleCatalog) pipeline.Registries { - return pipeline.Registries{ - Inputs: catalog.Inputs, - Chunkers: catalog.Chunkers, - ArtifactCodecs: catalog.ArtifactCodecs, - ArtifactEvidence: catalog.ArtifactEvidence, - Extractors: catalog.Extractors, - Mergers: catalog.Mergers, - Normalizers: catalog.Normalizers, - Validators: catalog.Validators, - ValidatorChains: catalog.ValidatorChains, - Outputs: catalog.Outputs, - } -} - -func isEmptyCatalog(catalog pipeline.ModuleCatalog) bool { - return catalog.Inputs == nil && - catalog.Chunkers == nil && - catalog.ArtifactCodecs == nil && - catalog.ArtifactEvidence == nil && - catalog.Extractors == nil && - catalog.Mergers == nil && - catalog.Normalizers == nil && - catalog.Validators == nil && - catalog.ValidatorChains == nil && - catalog.Outputs == nil -} - -func isEmptyRegistries(registries pipeline.Registries) bool { - return registries.Inputs == nil && - registries.Chunkers == nil && - registries.ArtifactCodecs == nil && - registries.ArtifactEvidence == nil && - registries.Extractors == nil && - registries.Mergers == nil && - registries.Normalizers == nil && - registries.Validators == nil && - registries.ValidatorChains == nil && - registries.Outputs == nil -} - -func productionLLMClientFactoryWithAssets(assets *llm.AssetRegistry) LLMClientFactory { - return func(ctx context.Context, cfg config.Config, profileID string, overrides LLMRuntimeOverrides) (contracts.StructuredLLMClient, []artifacts.LLMProfileManifest, error) { - return buildProductionLLMClient(ctx, cfg, profileID, overrides, assets) - } -} - -func buildProductionLLMClient(ctx context.Context, cfg config.Config, profileID string, overrides LLMRuntimeOverrides, assets *llm.AssetRegistry) (contracts.StructuredLLMClient, []artifacts.LLMProfileManifest, error) { - if err := ctx.Err(); err != nil { - return nil, nil, err - } - if assets == nil { - return nil, nil, fmt.Errorf("production asset registry must not be nil") - } - recorder := llm.NewLLMProfileRecorder() - transientRetries := cfg.PromptKit.TransientRetries - client, err := llm.NewPromptKitClient(llm.PromptKitClientConfig{ - ProfileDir: cfg.PromptKit.ProfileDir, - ProfileFile: cfg.PromptKit.ProfileFile, - TransientRetries: &transientRetries, - LocalBackend: mapPromptKitLocalBackend(cfg.PromptKit.LocalBackend), - Assets: assets, - Recorder: recorder, - ReasoningEffort: overrides.ReasoningEffort, - }) - if err != nil { - return nil, nil, fmt.Errorf("create PromptKit-backed LLM client: %w", err) - } - return client, nil, nil -} diff --git a/internal/cli/native_process_test.go b/internal/cli/native_process_test.go new file mode 100644 index 00000000..4afe5e9e --- /dev/null +++ b/internal/cli/native_process_test.go @@ -0,0 +1,33 @@ +package cli + +import ( + "bytes" + "errors" + "os" + "os/exec" + "path/filepath" + "runtime" + "testing" +) + +func TestExecutableRunRoutesDeclareTheSameRequiredInputs(t *testing.T) { + binary := filepath.Join(t.TempDir(), "notarius") + build := exec.Command(filepath.Join(runtime.GOROOT(), "bin", "go"), "build", "-o", binary, "../../cmd/notarius") + build.Env = append(os.Environ(), "GOWORK=off") + if output, err := build.CombinedOutput(); err != nil { + t.Fatalf("build executable: %v\n%s", err, output) + } + for _, route := range [][]string{{"run"}, {"pipeline", "run"}} { + help := exec.Command(binary, append(append([]string(nil), route...), "--help")...) + output, err := help.CombinedOutput() + if err != nil || !bytes.Contains(output, []byte("pipeline-id")) || !bytes.Contains(output, []byte("--input-file")) { + t.Fatalf("%v help: %v\n%s", route, err, output) + } + missingInput := exec.Command(binary, append(append([]string(nil), route...), "demo")...) + output, err = missingInput.CombinedOutput() + var exit *exec.ExitError + if !errors.As(err, &exit) || exit.ExitCode() != 2 || !bytes.Contains(output, []byte("--input-file")) { + t.Fatalf("%v missing input: %v\n%s", route, err, output) + } + } +} diff --git a/internal/cli/options.go b/internal/cli/options.go new file mode 100644 index 00000000..3faab25f --- /dev/null +++ b/internal/cli/options.go @@ -0,0 +1,34 @@ +package cli + +import ( + "os" + "time" + + "gitea.maximumdirect.net/eric/notarius/internal/framework/pipeline" + "go.maximumdirect.net/promptkit" +) + +type Options struct { + LLMClient promptkit.LLMClient + LLMRuntime promptkit.RuntimeConfig + Catalog pipeline.ModuleCatalog + Registries pipeline.Registries + RunIDGenerator RunIDGenerator + LookupEnv func(string) (string, bool) + Now func() time.Time +} + +// Defaulting function values is inert; command construction does not acquire +// configuration, register modules, resolve credentials, or allocate run storage. +func defaultOptions(options Options) Options { + if options.LookupEnv == nil { + options.LookupEnv = os.LookupEnv + } + if options.Now == nil { + options.Now = time.Now + } + if options.RunIDGenerator == nil { + options.RunIDGenerator = defaultRunIDGenerator + } + return options +} diff --git a/internal/cli/pipeline_commands.go b/internal/cli/pipeline_commands.go index 6dceef90..73b5afaf 100644 --- a/internal/cli/pipeline_commands.go +++ b/internal/cli/pipeline_commands.go @@ -53,7 +53,7 @@ func parseConfigurationOverride(raw string) (configkit.Override, error) { return configkit.Override{Path: path, Value: value, Flag: "--set"}, nil } -func pipelineCommands(newState func() *nativeLoaders, catalog func() (pipeline.ModuleCatalog, error)) commandkit.Command { +func pipelineCommands(newState func() *nativeLoaders, catalog func() (pipeline.ModuleCatalog, error), additional ...commandkit.Command) commandkit.Command { command := func(operation, summary string) commandkit.Command { var arguments []commandkit.Argument[pipelineInspectionInput] if operation != "list" { @@ -116,7 +116,8 @@ func pipelineCommands(newState func() *nativeLoaders, catalog func() (pipeline.M }, }) } - return commandkit.Group("pipeline", "Inspect, validate, or execute configured pipelines.", command("list", "List configured pipeline identifiers."), command("show", "Show a resolved pipeline composition."), command("validate", "Validate known pipeline dependencies offline.")) + children := []commandkit.Command{command("list", "List configured pipeline identifiers."), command("show", "Show a resolved pipeline composition."), command("validate", "Validate known pipeline dependencies offline.")} + return commandkit.Group("pipeline", "Inspect, validate, or execute configured pipelines.", append(children, additional...)...) } var pipelineValidationTemplate = template.Must(commandkit.ParseTextTemplate("pipeline-validation", "Pipeline {{printf \"%q\" .Pipeline}} is valid.\n", nil)) diff --git a/internal/cli/pipeline_run.go b/internal/cli/pipeline_run.go new file mode 100644 index 00000000..581e56ce --- /dev/null +++ b/internal/cli/pipeline_run.go @@ -0,0 +1,152 @@ +package cli + +import ( + "fmt" + "os" + "path/filepath" + + "gitea.maximumdirect.net/eric/notarius/internal/core/config" + "gitea.maximumdirect.net/eric/notarius/internal/framework/artifactcache" + "gitea.maximumdirect.net/eric/notarius/internal/framework/contracts" + frameworkdebug "gitea.maximumdirect.net/eric/notarius/internal/framework/debug" + "gitea.maximumdirect.net/eric/notarius/internal/framework/pipeline" + "gitea.maximumdirect.net/eric/notarius/internal/preflight" + "go.maximumdirect.net/commandkit" + "go.maximumdirect.net/configkit" + "go.maximumdirect.net/promptkit" +) + +func pipelineRunCommand(version string, newState func() *nativeLoaders, modules func() (pipeline.Registries, error), options Options) commandkit.Command { + options = defaultOptions(options) + return commandkit.NewCommand(commandkit.CommandSpec[pipelineRunInput]{Name: "run", Summary: "Execute a configured pipeline and publish its artifacts.", + Flags: runFlags(), Arguments: []commandkit.Argument[pipelineRunInput]{commandkit.ValueArgument(commandkit.ArgumentMeta{Name: "pipeline-id", Usage: "Configured pipeline identifier."}, true, parseRequiredRunValue, func(input *pipelineRunInput, value commandkit.Supplied[string]) { input.PipelineID = value.Value })}, + CheckInput: checkPipelineRunInput, + Run: func(invocation commandkit.Invocation, input pipelineRunInput) commandkit.Outcome { + state := newState() + selection := captureSelection(input.Selection) + if input.OutputDirectory.Set { + selection.Overrides = append(selection.Overrides, configkit.Override{Path: configkit.Keys("output", "directory"), Value: input.OutputDirectory.Value, Flag: "--output-dir"}) + } + result, err := state.application(invocation.Context, selection) + if err != nil { + return pipelineConfigurationFailure(err) + } + loaded, err := config.FromResult(result) + if err != nil { + return pipelineInvalidConfiguration(err) + } + cfg := loaded.RuntimeConfig() + startedAt := options.Now().UTC() + runID, err := options.RunIDGenerator(startedAt) + if err != nil { + return commandkit.Failure("Generate run identity.").WithCause(err) + } + if err := validateRunID(runID); err != nil { + return commandkit.Failure(err.Error()).WithCause(err) + } + outputDirectory := filepath.Join(cfg.Output.Directory, runID) + var capture *frameworkdebug.Recorder + var output pipeline.RunOutput + published := false + captureOutcome := func() any { + return map[string]any{"pipeline": input.PipelineID, "output_directory": outputDirectory, "artifact_count": output.ArtifactCount(), "fallback_count": output.FallbackCount, "published": published} + } + finish := func(outcome commandkit.Outcome) commandkit.Outcome { + if err := capture.Finalize(captureOutcome(), outcome.Cause); err != nil { + message := err.Error() + if published { + message += fmt.Sprintf("\nArtifacts: %s", outputDirectory) + } + if capture.Path() != "" { + message += fmt.Sprintf("\nDebug capture: %s", capture.Path()) + } + if outcome.Status == 0 { + outcome = commandkit.Failure(message) + } else { + outcome = outcome.WithExplanation(message) + } + outcome = outcome.WithCause(err) + } + return outcome + } + capture, err = startRunDebug(input.Debug, cfg.Debug.Directory, runID, startedAt) + if err != nil { + return finish(commandkit.Failure(err.Error()).WithCause(err)) + } + if err := capture.WriteJSON("context.json", map[string]any{"application_version": version, "pipeline": input.PipelineID, "input_path": input.InputFile.Value, "run_id": runID, "started_at": startedAt, "lanes": input.Lanes, "resources": input.Overrides.Resources, "execution_overrides": input.Overrides.Execution}); err != nil { + return finish(commandkit.Failure(err.Error()).WithCause(err)) + } + if err := captureRunConfiguration(capture, result); err != nil { + return finish(commandkit.Failure(err.Error()).WithCause(err)) + } + registries, err := modules() + if err != nil { + return finish(commandkit.Failure(err.Error()).WithCause(err)) + } + catalog := catalogFromRegistries(registries) + binds, unbinds, err := resolveCLIReferenceRequests(cfg, input.PipelineID, input.Lanes.Value, catalog, input.References.Value, input.Unbinds.Value) + if err != nil { + return finish(pipelineInvalidConfiguration(err)) + } + resolve := config.ResolveInput{PipelineID: input.PipelineID, Only: input.Lanes.Value, Catalog: catalog, ReferenceOverrides: binds, ReferenceUnbinds: unbinds} + if input.Cache.Set { + resolve.CacheOverride = &input.Cache.Value + } + if input.Overrides.Resources.Profile.Set { + resolve.LLMProfileOverride = &input.Overrides.Resources.Profile.Value + } + effective, err := cfg.Resolve(resolve) + if err != nil { + return finish(pipelineInvalidConfiguration(err)) + } + var engine *promptkit.Engine + if preflight.RequiresEngine(effective.ResolvedPipeline) { + engine, err = state.engine(invocation.Context, selection) + if err != nil { + return finish(pipelineConfigurationFailure(err)) + } + } + if err := preflight.Validate(invocation.Context, effective.ResolvedPipeline, catalog, engine); err != nil { + return finish(pipelineInvalidConfiguration(err)) + } + workingDirectory, err := os.Getwd() + if err != nil { + return finish(commandkit.Failure(err.Error()).WithCause(err)) + } + materialized, diagnostics, err := pipeline.MaterializeReferences(effective.ResolvedPipeline, catalog, pipeline.ReferenceMaterializationOptions{ConfigPath: loaded.MainSourcePath(), WorkingDir: workingDirectory}) + if err != nil { + return finish(commandkit.Failure(err.Error()).WithCause(err)) + } + prepared, err := pipeline.Prepare(materialized, registries, pipeline.ModuleDependencies{Engine: engine}) + if err != nil { + return finish(commandkit.Failure(err.Error()).WithCause(err)) + } + raw, err := os.ReadFile(input.InputFile.Value) + if err != nil { + return finish(commandkit.Failure(fmt.Sprintf("Read input %q: %v", input.InputFile.Value, err)).WithCause(err)) + } + sessionID, err := runSessionID(input.Overrides.Resources.SessionID, materialized.Input.Module, raw) + if err != nil { + return finish(commandkit.Failure(err.Error()).WithCause(err)) + } + cache, err := artifactcache.NewFilesystemStore(cfg.CacheDirectory) + if err != nil { + return finish(commandkit.Failure(err.Error()).WithCause(err)) + } + var recorder pipeline.DebugRecorder + if capture.Enabled() { + recorder = newRunEvidenceRecorder(capture, result) + } + group := &promptkit.ExecutionGroup{} + output, err = pipeline.New().Run(invocation.Context, pipeline.RunInput{Prepared: prepared, Path: input.InputFile.Value, RawInput: raw, SessionID: sessionID, RunID: runID, StartedAt: startedAt, Diagnostics: diagnostics, Cache: cache, Debug: recorder, Workers: cfg.Concurrency.Workers, Executions: group, Execution: contracts.ExecutionOptions{Execution: input.Overrides.Execution.Execution, TransientRetries: input.Overrides.Execution.TransientRetries}}) + if err != nil { + return finish(commandkit.Failure(err.Error()).WithCause(err)) + } + if err := writeOutputFiles(outputDirectory, output.OutputFiles); err != nil { + return finish(commandkit.Failure(err.Error()).WithCause(err)) + } + published = true + return finish(commandkit.Success()) + }, + }) +} diff --git a/internal/cli/pipeline_run_test.go b/internal/cli/pipeline_run_test.go new file mode 100644 index 00000000..f715d619 --- /dev/null +++ b/internal/cli/pipeline_run_test.go @@ -0,0 +1,459 @@ +package cli + +import ( + "bytes" + "context" + "encoding/json" + "fmt" + "net/url" + "os" + "path/filepath" + "strings" + "sync" + "testing" + "time" + + "gitea.maximumdirect.net/eric/notarius/internal/core/source" + "gitea.maximumdirect.net/eric/notarius/internal/framework/contracts" + "gitea.maximumdirect.net/eric/notarius/internal/framework/pipeline" + units "gitea.maximumdirect.net/eric/notarius/internal/modules/generic/chunk/units" + jsonoutput "gitea.maximumdirect.net/eric/notarius/internal/modules/generic/output/json" + "go.maximumdirect.net/commandkit" + "go.maximumdirect.net/promptkit" +) + +type runProbe struct { + mu sync.Mutex + requests []promptkit.GenerateRequest + prepared []promptkit.PreparedRun + references []contracts.ReferenceItem + engines []*promptkit.Engine + parsed int + produced int + correctFirst bool +} + +func (probe *runProbe) Generate(_ context.Context, request promptkit.GenerateRequest) (*promptkit.GenerateResponse, error) { + probe.mu.Lock() + defer probe.mu.Unlock() + probe.requests = append(probe.requests, request) + value := "accepted" + if request.Prompt.Messages[len(request.Prompt.Messages)-1].Content != "Assess." { + probe.produced++ + if probe.correctFirst && probe.produced == 1 { + value = "rejected" + } + } + return &promptkit.GenerateResponse{Content: fmt.Sprintf(`{"value":%q}`, value)}, nil +} +func (probe *runProbe) onPrepared(_ context.Context, details promptkit.PreparedRun) { + probe.mu.Lock() + defer probe.mu.Unlock() + probe.prepared = append(probe.prepared, details) +} + +type runInputAdapter struct{ probe *runProbe } + +func (*runInputAdapter) Key() string { return "fixture/input" } +func (adapter *runInputAdapter) Parse(_ context.Context, request contracts.ParseRequest) (*source.SourceDocument, error) { + adapter.probe.mu.Lock() + adapter.probe.parsed++ + adapter.probe.mu.Unlock() + document := &source.SourceDocument{ID: "source", Kind: "fixture", Format: "text", Units: []source.SourceUnit{{ID: 1, Kind: "text", Text: string(request.Raw), Ref: source.SourceRef{SourceID: "source", StartUnitID: 1, EndUnitID: 1}}}} + var err error + document.Digest, err = source.DigestDocument(document) + return document, err +} + +type runProducer struct { + key string + engine *promptkit.Engine + probe *runProbe +} + +func (producer *runProducer) Key() string { return producer.key } +func (*runProducer) ReferenceSlots() []contracts.ReferenceSlot { + return runReferenceSlots() +} +func runReferenceSlots() []contracts.ReferenceSlot { + return []contracts.ReferenceSlot{ + {Name: "context", Required: true, AcceptedMediaTypes: []string{"text/plain", "application/json"}, AcceptedArtifactKinds: []contracts.ArtifactKind{"test/inspection"}}, + {Name: "secondary", AcceptedMediaTypes: []string{"text/plain"}}, + } +} +func (producer *runProducer) Extract(ctx context.Context, request contracts.TypedExtractionRequest[inspectionArtifact]) (contracts.TypedExtractionResult[inspectionArtifact], error) { + content := string(request.SourceInput.Content) + for _, slot := range request.References.Slots { + for _, item := range slot.Items { + content += "\n" + string(item.Content) + producer.probe.mu.Lock() + producer.probe.references = append(producer.probe.references, item) + producer.probe.mu.Unlock() + } + } + initial := promptkit.RunRequest{PromptID: "inspect-initial", ProfileID: request.LLMProfile, SessionID: request.SessionID, Inputs: map[string]promptkit.ArtifactRef{"source": promptkit.Inline(content)}, OnPrepared: producer.probe.onPrepared} + value, outcome, err := pipeline.RunProducer(ctx, producer.engine, request.Operation, initial, func(_ context.Context, result *promptkit.RunResult) (*inspectionArtifact, []string, error) { + var value inspectionArtifact + err := json.Unmarshal([]byte(result.RawOutput), &value) + return &value, nil, err + }, func(context.Context, []string) (promptkit.RunRequest, error) { + return promptkit.RunRequest{PromptID: "inspect-correction", ProfileID: request.LLMProfile, SessionID: request.SessionID, OnPrepared: producer.probe.onPrepared}, nil + }, nil) + return contracts.TypedExtractionResult[inspectionArtifact]{Value: value, Outcome: outcome}, err +} +func (producer *runProducer) Merge(_ context.Context, request contracts.TypedMergeRequest[inspectionArtifact]) (contracts.TypedMergeResult[inspectionArtifact], error) { + return contracts.TypedMergeResult[inspectionArtifact]{Value: request.ExtractOutputs[0].Value}, nil +} +func (producer *runProducer) Normalize(_ context.Context, request contracts.TypedNormalizeRequest[inspectionArtifact]) (contracts.TypedNormalizeResult[inspectionArtifact], error) { + return contracts.TypedNormalizeResult[inspectionArtifact]{Value: request.MergeOutput.Value}, nil +} + +type runValidator struct { + engine *promptkit.Engine + probe *runProbe +} + +func (*runValidator) Name() string { return "fixture/validator" } +func (*runValidator) ExecutionClass() contracts.ExecutionClass { + return contracts.ExecutionClassLLMBacked +} +func (validator *runValidator) Validate(ctx context.Context, request contracts.TypedValidationRequest[inspectionArtifact]) (contracts.ValidationResult, error) { + result, err := validator.engine.Run(ctx, promptkit.RunRequest{PromptID: "inspect-validator", ProfileID: request.LLMProfile, SessionID: request.SessionID, Execution: request.Execution.Execution, TransientRetries: request.Execution.TransientRetries, Observer: request.Execution.Observer, OnPrepared: validator.probe.onPrepared}) + if err != nil { + return contracts.ValidationResult{}, err + } + if !result.Validation.IsValid { + return contracts.ValidationResult{}, fmt.Errorf("native validator contract failed") + } + if request.Value.Value != "accepted" { + return contracts.ValidationResult{ReasonCode: "rejected", CorrectionGuidance: "Return accepted."}, nil + } + return contracts.ValidationResult{Approved: true}, nil +} + +func runRegistries(t *testing.T, probe *runProbe) pipeline.Registries { + t.Helper() + registries := pipeline.Registries{Inputs: pipeline.NewInputAdapterRegistry(), Chunkers: pipeline.NewChunkerRegistry(), ArtifactCodecs: pipeline.NewArtifactCodecRegistry(), Extractors: pipeline.NewExtractorRegistry(), Mergers: pipeline.NewMergerRegistry(), Normalizers: pipeline.NewNormalizerRegistry(), Validators: pipeline.NewValidatorRegistry(), ValidatorChains: pipeline.NewValidatorChainRegistry(), Outputs: pipeline.NewOutputEncoderRegistry()} + check := func(err error) { + t.Helper() + if err != nil { + t.Fatal(err) + } + } + check(registries.Inputs.RegisterWithSpec(pipeline.ModuleSpec{Key: "fixture/input", Stage: pipeline.StageInput, ExecutionClass: contracts.ExecutionClassDeterministic}, func() (contracts.InputAdapter, error) { return &runInputAdapter{probe}, nil })) + check(units.Register(registries.Chunkers)) + check(jsonoutput.Register(registries.Outputs)) + check(pipeline.RegisterArtifactCodec(registries.ArtifactCodecs, inspectionCodec{})) + slots := runReferenceSlots() + check(pipeline.RegisterExtractorBuilder(registries.Extractors, pipeline.ModuleSpec{Key: "fixture/extract", Stage: pipeline.StageExtract, ArtifactKind: "test/inspection", ExecutionClass: contracts.ExecutionClassLLMBacked, Prompts: []string{"inspect-initial", "inspect-correction"}, ReferenceSlots: slots}, func(options map[string]any) error { return pipeline.RejectUnknownOptions(options) }, func(request pipeline.BuildRequest) (contracts.Extractor[inspectionArtifact], error) { + probe.engines = append(probe.engines, request.Dependencies.Engine) + return &runProducer{key: "fixture/extract", engine: request.Dependencies.Engine, probe: probe}, nil + })) + check(pipeline.RegisterMerger(registries.Mergers, pipeline.ModuleSpec{Key: "fixture/merge", Stage: pipeline.StageMerge, ArtifactKind: "test/inspection", ExecutionClass: contracts.ExecutionClassDeterministic}, func() (contracts.Merger[inspectionArtifact], error) { + return &runProducer{key: "fixture/merge", probe: probe}, nil + })) + check(pipeline.RegisterNormalizer(registries.Normalizers, pipeline.ModuleSpec{Key: "fixture/normalize", Stage: pipeline.StageNormalize, ArtifactKind: "test/inspection", ExecutionClass: contracts.ExecutionClassDeterministic, Provides: []string{"normalized"}}, func() (contracts.Normalizer[inspectionArtifact], error) { + return &runProducer{key: "fixture/normalize", probe: probe}, nil + })) + check(pipeline.RegisterTypedValidatorBuilder(registries.Validators, "test/inspection", pipeline.ValidatorSpec{Key: "fixture/validator", ExecutionClass: contracts.ExecutionClassLLMBacked, Prompts: []string{"inspect-validator"}}, func(options map[string]any) error { return pipeline.RejectUnknownOptions(options) }, func(request pipeline.BuildRequest) (contracts.TypedValidator[inspectionArtifact], error) { + probe.engines = append(probe.engines, request.Dependencies.Engine) + return &runValidator{request.Dependencies.Engine, probe}, nil + })) + return registries +} + +func runFixture(t *testing.T, probe *runProbe, configure ...func(string) string) (*commandkit.App, string, string, **nativeLoaders) { + t.Helper() + root := t.TempDir() + configuration := strings.ReplaceAll(inspectionConfiguration, "fixture/chunk", "generic") + configuration = strings.ReplaceAll(configuration, "fixture/output", "json") + configuration = strings.Replace(configuration, " api_key_env: NOTARIUS_INSPECTION_NO_CREDENTIAL_REQUIRED\n api_key_required: true", " endpoint: https://example.invalid/v1\n api_key_required: false", 1) + configuration = strings.Replace(configuration, " prompts:", ` - id: chosen + endpoint: https://example.invalid/v1 + model: chosen-model + reasoning_effort: high + temperature: 0.8 + api_key_required: false + prompts:`, 1) + for _, update := range configure { + configuration = update(configuration) + } + file := filepath.Join(root, "config.yml") + input := filepath.Join(root, "input.txt") + for path, content := range map[string]string{file: configuration, input: "source transcript", filepath.Join(root, "reference-must-not-exist.txt"): "configured reference"} { + if err := os.WriteFile(path, []byte(content), 0600); err != nil { + t.Fatal(err) + } + } + registries := runRegistries(t, probe) + options := Options{Now: func() time.Time { return time.Unix(42, 0) }} + runs := 0 + options.RunIDGenerator = func(time.Time) (string, error) { runs++; return fmt.Sprintf("run-%d", runs), nil } + var state *nativeLoaders + newState := func() *nativeLoaders { + state = &nativeLoaders{engineOptions: []promptkit.Option{promptkit.WithLLMClient(probe)}} + return state + } + app, err := commandkit.New(commandkit.Identity{Name: "notarius"}, pipelineCommands(newState, func() (pipeline.ModuleCatalog, error) { return catalogFromRegistries(registries), nil }, pipelineRunCommand("development", newState, func() (pipeline.Registries, error) { return registries, nil }, options)), commandkit.Alias("run", "pipeline run")) + if err != nil { + t.Fatal(err) + } + return app, file, input, &state +} + +func executeRunFixture(t *testing.T, app *commandkit.App, args ...string) commandkit.Outcome { + t.Helper() + var out, diagnostics bytes.Buffer + outcome := app.Execute(context.Background(), args, commandkit.Streams{Out: &out, Err: &diagnostics}) + if outcome.Status != 0 { + writeConfigLoadError(&diagnostics, outcome.Cause) + t.Fatalf("%v: %#v stderr=%s", args, outcome, diagnostics.String()) + } + return outcome +} + +func TestNativeRunRoutesShareOneHandlerAndApplyOverridesToEveryRole(t *testing.T) { + probe := &runProbe{correctFirst: true} + app, file, input, state := runFixture(t, probe) + executeRunFixture(t, app, "run", "demo", "--config", file, "--input-file", input, "--cache", "bypass", "--llm-profile", "chosen", "--llm-model", "operator-model", "--llm-session-id", "operator-session", "--llm-reasoning-effort=", "--llm-temperature=0", "--llm-transient-retries=0") + if probe.produced != 2 || len(probe.requests) != 4 || probe.parsed != 1 { + t.Fatalf("native operation/correction/validation = %d requests=%d parse=%d", probe.produced, len(probe.requests), probe.parsed) + } + for _, request := range probe.requests { + if request.Target.Model != "operator-model" || request.Prompt.SessionID != "operator-session" || request.Target.ReasoningEffort != "" || request.Target.Temperature != 0 || !request.TargetPresence.Temperature { + t.Fatalf("override missing on a native role: %#v", request) + } + } + for _, prepared := range probe.prepared { + if prepared.SelectedProfileID != "chosen" { + t.Fatalf("profile override missing: %s", prepared.SelectedProfileID) + } + } + if len((*state).selections) != 1 || len(probe.engines) != 2 || probe.engines[0] == nil || probe.engines[0] != probe.engines[1] || probe.engines[0] != (*state).selections[0].engine { + t.Fatal("run used a second load or engine") + } + if (*state).selections[0].result.Config.Pipelines["demo"].LLMProfile != nil { + t.Fatal("request override mutated captured configuration") + } + executeRunFixture(t, app, "pipeline", "run", "demo", "--config", file, "--input-file", input, "--cache", "bypass") + for _, id := range []string{"run-1", "run-2"} { + content, err := os.ReadFile(filepath.Join(filepath.Dir(file), "output-must-not-exist", id, "lanes", "notes.json")) + if err != nil || !strings.Contains(string(content), "accepted") { + t.Fatalf("published artifact %s: %s %v", id, content, err) + } + } +} + +func TestNativeRunSessionIdentityIgnoresReferenceChangesAndHonorsEmptyOverride(t *testing.T) { + probe := &runProbe{} + app, file, input, _ := runFixture(t, probe) + first := filepath.Join(t.TempDir(), "reference one.txt") + second := filepath.Join(t.TempDir(), "reference two.txt") + for path, text := range map[string]string{first: "first reference", second: "second reference"} { + if err := os.WriteFile(path, []byte(text), 0600); err != nil { + t.Fatal(err) + } + } + executeRunFixture(t, app, "run", "demo", "--config", file, "--input-file", input, "--reference", "context="+first, "--cache", "bypass") + session := probe.requests[0].Prompt.SessionID + executeRunFixture(t, app, "run", "demo", "--config", file, "--input-file", input, "--reference", "context="+second, "--cache", "bypass") + if session == "" || session != probe.requests[2].Prompt.SessionID || !strings.Contains(probe.requests[0].Prompt.Messages[0].Content, "first reference") || !strings.Contains(probe.requests[2].Prompt.Messages[0].Content, "second reference") { + t.Fatal("reference selection changed generated session or failed to reach request") + } + executeRunFixture(t, app, "run", "demo", "--config", file, "--input-file", input, "--cache", "bypass", "--set", `/pipelines/demo/llm_profile="chosen"`, "--llm-session-id=", "--llm-profile=") + if probe.requests[4].Prompt.SessionID != "" || probe.prepared[4].SelectedProfileID != "partial" { + t.Fatal("explicit empty native selections were replaced by application defaults") + } +} + +func TestNativeRunValidatesEverySuppliedValueBeforeWork(t *testing.T) { + probe := &runProbe{} + app, file, input, _ := runFixture(t, probe) + for _, args := range [][]string{ + {"run", "demo", "--config", file}, + {"run", "--config", file, "--input-file", input}, + {"run", "demo", "--config", file, "--input-file", "", "--input-file", input}, + {"run", "demo", "--config", file, "--input-file", input, "--cache", "bad", "--cache", "auto"}, + {"run", "demo", "--config", file, "--input-file", input, "--output-dir", "", "--output-dir", t.TempDir()}, + {"run", "demo", "--config", file, "--input-file", input, "--reference", "invalid"}, + {"run", "demo", "--config", file, "--input-file", input, "--without-reference", "context"}, + {"run", "demo", "--config", file, "--input-file", input, "--llm-temperature=invalid", "--llm-temperature=0"}, + {"run", "demo", "--config", file, "--input-file", input, "--only", "notes"}, + {"run", "demo", "--config", file, "--input-file", input, "--json"}, + {"run", "demo", "--config", file, "--input-file", input, "--resume"}, + {"run", "demo", "--config", file, "--input-file", input, "--recompute-step", "build"}, + {"run", "demo", "--config", file, "--input-file", input, "--chunk-cache", "auto"}, + } { + var out, diagnostics bytes.Buffer + outcome := app.Execute(context.Background(), args, commandkit.Streams{Out: &out, Err: &diagnostics}) + if outcome.Status != 2 { + t.Fatalf("%v: %#v %s", args, outcome, diagnostics.String()) + } + } + if len(probe.requests) != 0 || probe.parsed != 0 { + t.Fatal("invalid invocation started source/model execution") + } +} + +func TestNativeRunDebugAndOutputOverridesRemainInvocationRelative(t *testing.T) { + probe := &runProbe{} + app, file, input, _ := runFixture(t, probe) + output := t.TempDir() + debug := t.TempDir() + workingDirectory, err := os.Getwd() + if err != nil { + t.Fatal(err) + } + relativeOutput, err := filepath.Rel(workingDirectory, output) + if err != nil { + t.Fatal(err) + } + relativeDebug, err := filepath.Rel(workingDirectory, debug) + if err != nil { + t.Fatal(err) + } + executeRunFixture(t, app, "run", "demo", "--config", file, "--input-file", input, "--output-dir", relativeOutput, "--debug", "--debug-dir", relativeDebug, "--cache", "bypass") + if _, err := os.Stat(filepath.Join(output, "run-1", "lanes", "notes.json")); err != nil { + t.Fatal(err) + } + if _, err := os.Stat(filepath.Join(debug, "run-1", "outcome.json")); err != nil { + t.Fatal(err) + } + executeRunFixture(t, app, "run", "demo", "--config", file, "--input-file", input, "--debug", "--debug-dir=", "--cache", "bypass") + if _, err := os.Stat(filepath.Join(filepath.Dir(file), "debug-must-not-exist")); !os.IsNotExist(err) { + t.Fatalf("explicit empty debug path allocated capture: %v", err) + } + executeRunFixture(t, app, "run", "demo", "--config", file, "--input-file", input, "--debug", "--debug=false", "--debug-dir", debug, "--cache", "bypass") + if _, err := os.Stat(filepath.Join(debug, "run-3")); !os.IsNotExist(err) { + t.Fatalf("explicit false debug selection allocated capture: %v", err) + } +} + +func TestNativeRunReferencesKeepSpecificityAndFileOrigins(t *testing.T) { + probe := &runProbe{} + app, file, input, _ := runFixture(t, probe, func(configuration string) string { + return strings.Replace(configuration, "references: {context: {path: reference-must-not-exist.txt}}", "references: {context: {path: reference-must-not-exist.txt}, secondary: {path: never-read.txt}}", 1) + }) + workingDirectory, err := os.Getwd() + if err != nil { + t.Fatal(err) + } + paths := make(map[string]string) + for _, name := range []string{"broad", "lane", "binding", "secondary"} { + absolute := filepath.Join(t.TempDir(), name+" reference.txt") + if err := os.WriteFile(absolute, []byte(name+" reference"), 0600); err != nil { + t.Fatal(err) + } + paths[name], err = filepath.Rel(workingDirectory, absolute) + if err != nil { + t.Fatal(err) + } + } + executeRunFixture(t, app, "run", "demo", "--config", file, "--input-file", input, "--cache", "bypass", + "--reference", "notes.extract.context="+paths["binding"], + "--reference", "notes.context="+paths["lane"], + "--reference", "context="+paths["broad"], + "--reference", "secondary="+paths["secondary"]) + if len(probe.references) != 2 { + t.Fatalf("materialized references: %#v", probe.references) + } + for _, item := range probe.references { + name := "binding" + if string(item.Content) == "secondary reference" { + name = "secondary" + } + absolute := filepath.Clean(filepath.Join(workingDirectory, paths[name])) + if string(item.Content) != name+" reference" || item.BindingSource != contracts.ReferenceBindingSourceCLI || item.Origin.URI != (&url.URL{Scheme: "file", Path: absolute}).String() { + t.Fatalf("CLI reference content/origin: %#v", item) + } + } + probe.references = nil + executeRunFixture(t, app, "run", "demo", "--config", file, "--input-file", input, "--cache", "bypass", + "--without-reference", "secondary") + configured := (&url.URL{Scheme: "file", Path: filepath.Join(filepath.Dir(file), "reference-must-not-exist.txt")}).String() + if len(probe.references) != 1 || string(probe.references[0].Content) != "configured reference" || probe.references[0].BindingSource != contracts.ReferenceBindingSourceConfig || probe.references[0].Origin.URI != configured { + t.Fatalf("config reference origin or optional unbind: %#v", probe.references) + } +} + +func TestNativeRunLiteralLanesAndFinalSingletonValues(t *testing.T) { + probe := &runProbe{} + lane := `{"extract":{"module":"fixture/extract","references":{"context":{"path":"reference-must-not-exist.txt"}},"validators":[{"module":"fixture/validator"}]},"merge":{"module":"fixture/merge"},"normalize":{"module":"fixture/normalize"}}` + artifacts := `{"notes,extra":` + lane + `,"other":` + lane + `,"unselected":` + lane + `}` + app, file, input, _ := runFixture(t, probe, func(configuration string) string { + start := strings.Index(configuration, " steps:") + end := strings.Index(configuration, "promptkit:") + return configuration[:start] + " artifacts: " + artifacts + "\n" + configuration[end:] + }) + output := t.TempDir() + executeRunFixture(t, app, "run", "--config", file, + "--input-file", filepath.Join(t.TempDir(), "earlier-input.txt"), "--input-file", input, + "--output-dir", filepath.Join(t.TempDir(), "earlier-output"), "--output-dir", output, + "--llm-profile", "not-the-final-profile", "--llm-profile", "chosen", + "--cache", "auto", "--cache", "bypass", + "--lane", "notes,extra", "--lane", "other", "--", "demo") + if probe.produced != 2 || probe.parsed != 1 { + t.Fatalf("selected lanes or singleton input: produced=%d parsed=%d", probe.produced, probe.parsed) + } + files, err := os.ReadDir(filepath.Join(output, "run-1", "lanes")) + if err != nil || len(files) != 2 { + t.Fatalf("selected output files: %#v %v", files, err) + } + for _, prepared := range probe.prepared { + if prepared.SelectedProfileID != "chosen" { + t.Fatalf("final singleton profile not used: %q", prepared.SelectedProfileID) + } + } + if _, err := os.Stat(filepath.Join(filepath.Dir(file), "cache-must-not-exist")); !os.IsNotExist(err) { + t.Fatalf("final bypass value allocated cache: %v", err) + } +} + +func TestNativeRunProtectsGeneratedReferencesBeforeReadingFiles(t *testing.T) { + probe := &runProbe{} + app, file, input, _ := runFixture(t, probe) + steps := `[{"id":"produce","artifacts":{"earlier":{"extract":{"module":"fixture/extract","references":{"context":{"path":"reference-must-not-exist.txt"}}},"merge":{"module":"fixture/merge"},"normalize":{"module":"fixture/normalize"}}}},{"id":"consume","artifacts":{"later":{"extract":{"module":"fixture/extract","references":{"context":{"artifact":{"step":"produce","lane":"earlier"}}}},"merge":{"module":"fixture/merge"},"normalize":{"module":"fixture/normalize"}}}}]` + var out, diagnostics bytes.Buffer + outcome := app.Execute(context.Background(), []string{"run", "demo", "--config", file, "--input-file", input, "--set", "/pipelines/demo/steps=" + steps, "--reference", "context=never-read.txt"}, commandkit.Streams{Out: &out, Err: &diagnostics}) + if outcome.Status != 2 || !strings.Contains(diagnostics.String(), "conflicting generated and external bindings") || len(probe.requests) != 0 || probe.parsed != 0 { + t.Fatalf("generated/external conflict: %#v %s requests=%d parsed=%d", outcome, diagnostics.String(), len(probe.requests), probe.parsed) + } +} + +func TestNativeRunCapturesPreflightFailureBeforeSourceExecution(t *testing.T) { + probe := &runProbe{} + app, file, _, state := runFixture(t, probe) + content, err := os.ReadFile(file) + if err != nil { + t.Fatal(err) + } + content = bytes.Replace(content, []byte("id: inspect-correction"), []byte("id: different-correction"), 1) + if err := os.WriteFile(file, content, 0600); err != nil { + t.Fatal(err) + } + var out, diagnostics bytes.Buffer + outcome := app.Execute(context.Background(), []string{"run", "demo", "--config", file, "--input-file", filepath.Join(t.TempDir(), "missing-input.txt"), "--debug"}, commandkit.Streams{Out: &out, Err: &diagnostics}) + if outcome.Status != 2 || !strings.Contains(diagnostics.String(), "inspect-correction") || probe.parsed != 0 || len(probe.requests) != 0 || len(probe.engines) != 0 { + t.Fatalf("preflight order: %#v %s", outcome, diagnostics.String()) + } + if (*state).selections[0].engine == nil { + t.Fatal("preflight did not inspect the captured engine") + } + if _, err := os.Stat(filepath.Join(filepath.Dir(file), "debug-must-not-exist", "run-1", "outcome.json")); err != nil { + t.Fatalf("failed preflight capture: %v", err) + } +} + +func TestNativeRunDebugAllocationFailurePreventsEngineConstruction(t *testing.T) { + probe := &runProbe{} + app, file, input, state := runFixture(t, probe) + debug := filepath.Join(t.TempDir(), "not-a-directory") + if err := os.WriteFile(debug, []byte("occupied"), 0600); err != nil { + t.Fatal(err) + } + var out, diagnostics bytes.Buffer + outcome := app.Execute(context.Background(), []string{"run", "demo", "--config", file, "--input-file", input, "--debug", "--debug-dir", debug}, commandkit.Streams{Out: &out, Err: &diagnostics}) + if outcome.Status != 1 || probe.parsed != 0 || len(probe.requests) != 0 || len(probe.engines) != 0 || (*state).selections[0].engine != nil { + t.Fatalf("capture allocation order: %#v %s", outcome, diagnostics.String()) + } +} diff --git a/internal/cli/reference_selectors.go b/internal/cli/reference_selectors.go new file mode 100644 index 00000000..06d7f0e4 --- /dev/null +++ b/internal/cli/reference_selectors.go @@ -0,0 +1,588 @@ +package cli + +import ( + "fmt" + "sort" + "strings" + + "gitea.maximumdirect.net/eric/notarius/internal/core/config" + "gitea.maximumdirect.net/eric/notarius/internal/framework/contracts" + "gitea.maximumdirect.net/eric/notarius/internal/framework/pipeline" +) + +type cliReferenceRequest struct { + Selector cliReferenceSelector + Source string +} + +type cliReferenceUnbindRequest struct { + Selector cliReferenceSelector +} + +type cliReferenceSelector struct { + Scope cliReferenceSelectorScope + LaneID string + Stage pipeline.ModuleStage + SlotName string +} + +type cliReferenceSelectorScope uint8 + +const ( + cliReferenceScopePipeline cliReferenceSelectorScope = iota + cliReferenceScopeLane + cliReferenceScopeChunk + cliReferenceScopeBinding +) + +func parseReferenceFlags(values []string) ([]cliReferenceRequest, error) { + if len(values) == 0 { + return nil, nil + } + requests := make([]cliReferenceRequest, 0, len(values)) + for _, raw := range values { + name, source, ok := strings.Cut(raw, "=") + if !ok { + return nil, fmt.Errorf("--reference must use slot=path, lane.slot=path, or lane.stage.slot=path") + } + if strings.TrimSpace(source) == "" { + return nil, fmt.Errorf("--reference path must not be empty; use --without-reference to unbind") + } + selector, err := parseReferenceSelector(name, "--reference") + if err != nil { + return nil, err + } + requests = append(requests, cliReferenceRequest{ + Selector: selector, + Source: strings.TrimSpace(source), + }) + } + return requests, nil +} + +func parseReferenceUnbindFlags(values []string) ([]cliReferenceUnbindRequest, error) { + if len(values) == 0 { + return nil, nil + } + requests := make([]cliReferenceUnbindRequest, 0, len(values)) + for _, raw := range values { + if strings.Contains(raw, "=") { + return nil, fmt.Errorf("--without-reference must use a reference selector without =path") + } + selector, err := parseReferenceSelector(raw, "--without-reference") + if err != nil { + return nil, err + } + requests = append(requests, cliReferenceUnbindRequest{ + Selector: selector, + }) + } + return requests, nil +} + +func parseReferenceSelector(raw string, flagName string) (cliReferenceSelector, error) { + selector := strings.TrimSpace(raw) + if selector == "" { + return cliReferenceSelector{}, fmt.Errorf("%s reference slot must not be empty", flagName) + } + parts := strings.Split(selector, ".") + for _, part := range parts { + if strings.TrimSpace(part) == "" { + return cliReferenceSelector{}, fmt.Errorf("%s must use non-empty reference selector values", flagName) + } + } + switch len(parts) { + case 1: + return cliReferenceSelector{Scope: cliReferenceScopePipeline, SlotName: strings.TrimSpace(parts[0])}, nil + case 2: + first := strings.TrimSpace(parts[0]) + slotName := strings.TrimSpace(parts[1]) + if first == string(pipeline.StageChunk) { + return cliReferenceSelector{Scope: cliReferenceScopeChunk, Stage: pipeline.StageChunk, SlotName: slotName}, nil + } + return cliReferenceSelector{Scope: cliReferenceScopeLane, LaneID: first, SlotName: slotName}, nil + case 3: + laneID := strings.TrimSpace(parts[0]) + stage := pipeline.ModuleStage(strings.TrimSpace(parts[1])) + slotName := strings.TrimSpace(parts[2]) + if stage != pipeline.StageExtract && stage != pipeline.StageMerge && stage != pipeline.StageNormalize { + return cliReferenceSelector{}, fmt.Errorf("%s lane-qualified selector must use lane.extract.slot, lane.merge.slot, or lane.normalize.slot", flagName) + } + return cliReferenceSelector{Scope: cliReferenceScopeBinding, LaneID: laneID, Stage: stage, SlotName: slotName}, nil + default: + return cliReferenceSelector{}, fmt.Errorf("%s must use slot, chunk.slot, lane.slot, lane.extract.slot, lane.merge.slot, or lane.normalize.slot", flagName) + } +} + +func resolveCLIReferenceRequests( + cfg config.Config, + pipelineID string, + only []string, + catalog pipeline.ModuleCatalog, + referenceRequests []cliReferenceRequest, + unbindRequests []cliReferenceUnbindRequest, +) ([]pipeline.ReferenceBinding, []pipeline.ReferenceUnbind, error) { + if len(referenceRequests) == 0 && len(unbindRequests) == 0 { + return nil, nil, nil + } + + targets, err := selectedReferenceTargets(cfg, pipelineID, only, catalog) + if err != nil { + return nil, nil, err + } + + // Broad CLI selectors are only presentation syntax. Collapse them into one + // highest-specificity action per concrete framework target before pipeline + // resolution so the generic reference contract stays stage-and-lane exact. + actions := make(map[cliReferenceTargetKey]resolvedCLIReferenceAction) + for _, request := range referenceRequests { + matches, err := resolveCLIReferenceTargets(targets, request.Selector) + if err != nil { + return nil, nil, err + } + for _, target := range matches { + candidate := resolvedCLIReferenceAction{ + kind: cliReferenceActionBind, + selector: request.Selector, + target: target, + slotName: request.Selector.SlotName, + source: request.Source, + specificity: request.Selector.specificity(), + } + if err := mergeCLIReferenceAction(actions, candidate); err != nil { + return nil, nil, err + } + } + } + for _, request := range unbindRequests { + matches, err := resolveCLIReferenceTargets(targets, request.Selector) + if err != nil { + return nil, nil, err + } + for _, target := range matches { + candidate := resolvedCLIReferenceAction{ + kind: cliReferenceActionUnbind, + selector: request.Selector, + target: target, + slotName: request.Selector.SlotName, + specificity: request.Selector.specificity(), + } + if err := mergeCLIReferenceAction(actions, candidate); err != nil { + return nil, nil, err + } + } + } + + resolved := make([]resolvedCLIReferenceAction, 0, len(actions)) + for _, action := range actions { + resolved = append(resolved, action) + } + sort.Slice(resolved, func(i, j int) bool { + left, right := resolved[i], resolved[j] + if left.target.laneID != right.target.laneID { + return left.target.laneID < right.target.laneID + } + if left.target.stage != right.target.stage { + return referenceStageOrder(left.target.stage) < referenceStageOrder(right.target.stage) + } + return left.slotName < right.slotName + }) + + overrides := make([]pipeline.ReferenceBinding, 0, len(resolved)) + unbinds := make([]pipeline.ReferenceUnbind, 0, len(resolved)) + for _, action := range resolved { + switch action.kind { + case cliReferenceActionBind: + overrides = append(overrides, pipeline.ReferenceBinding{ + Stage: action.target.stage, + LaneID: action.target.laneID, + SlotName: action.slotName, + Source: action.source, + BindingSource: contracts.ReferenceBindingSourceCLI, + }) + case cliReferenceActionUnbind: + unbinds = append(unbinds, pipeline.ReferenceUnbind{ + Stage: action.target.stage, + LaneID: action.target.laneID, + SlotName: action.slotName, + }) + } + } + return overrides, unbinds, nil +} + +type cliReferenceActionKind uint8 + +const ( + cliReferenceActionBind cliReferenceActionKind = iota + cliReferenceActionUnbind +) + +type cliReferenceTargetKey struct { + stage pipeline.ModuleStage + laneID string + slotName string +} + +type resolvedCLIReferenceAction struct { + kind cliReferenceActionKind + selector cliReferenceSelector + target selectedReferenceTarget + slotName string + source string + specificity int +} + +func mergeCLIReferenceAction(actions map[cliReferenceTargetKey]resolvedCLIReferenceAction, candidate resolvedCLIReferenceAction) error { + key := cliReferenceTargetKey{stage: candidate.target.stage, laneID: candidate.target.laneID, slotName: candidate.slotName} + current, ok := actions[key] + if !ok || candidate.specificity > current.specificity { + actions[key] = candidate + return nil + } + if candidate.specificity < current.specificity { + return nil + } + if candidate.kind != current.kind { + return fmt.Errorf("reference target %q slot %q is both bound by %q and unbound by %q at the same specificity", targetLabel(candidate.target), candidate.slotName, current.selector.String(), candidate.selector.String()) + } + actions[key] = candidate + return nil +} + +func (selector cliReferenceSelector) specificity() int { + switch selector.Scope { + case cliReferenceScopePipeline: + return 0 + case cliReferenceScopeLane: + return 1 + case cliReferenceScopeChunk, cliReferenceScopeBinding: + return 2 + default: + return -1 + } +} + +func (selector cliReferenceSelector) String() string { + switch selector.Scope { + case cliReferenceScopePipeline: + return selector.SlotName + case cliReferenceScopeLane: + return selector.LaneID + "." + selector.SlotName + case cliReferenceScopeChunk: + return "chunk." + selector.SlotName + case cliReferenceScopeBinding: + return selector.LaneID + "." + string(selector.Stage) + "." + selector.SlotName + default: + return selector.SlotName + } +} + +type selectedReferenceTarget struct { + laneID string + stage pipeline.ModuleStage + module string + slots map[string]struct{} +} + +func selectedReferenceTargets(cfg config.Config, pipelineID string, only []string, catalog pipeline.ModuleCatalog) ([]selectedReferenceTarget, error) { + profile, ok := lookupCLIReferencePipeline(cfg.Pipelines, pipelineID) + if !ok { + return nil, fmt.Errorf("pipeline %q is not configured", strings.TrimSpace(pipelineID)) + } + if profile.Steps != nil && len(only) > 0 { + return nil, fmt.Errorf("pipeline %q lane selection is not supported for explicit ordered steps", strings.TrimSpace(pipelineID)) + } + + lanesByID := make(map[string]pipeline.ArtifactLaneProfile, len(profile.Artifacts)) + addLanes := func(values map[string]pipeline.ArtifactLaneProfile) error { + for rawLaneID, lane := range values { + laneID := strings.TrimSpace(rawLaneID) + if laneID == "" { + return fmt.Errorf("pipeline %q artifact lane id must not be empty", strings.TrimSpace(pipelineID)) + } + if _, ok := lanesByID[laneID]; ok { + return fmt.Errorf("pipeline %q artifact lane %q is duplicated after trimming", strings.TrimSpace(pipelineID), laneID) + } + lanesByID[laneID] = lane + } + return nil + } + if profile.Steps == nil { + if err := addLanes(profile.Artifacts); err != nil { + return nil, err + } + } else { + for _, step := range profile.Steps { + if err := addLanes(step.Artifacts); err != nil { + return nil, err + } + } + } + + selectedIDs := make([]string, 0, len(lanesByID)) + if len(only) == 0 { + for laneID := range lanesByID { + selectedIDs = append(selectedIDs, laneID) + } + } else { + seen := make(map[string]struct{}, len(only)) + for _, rawLaneID := range only { + laneID := strings.TrimSpace(rawLaneID) + if laneID == "" { + return nil, fmt.Errorf("pipeline %q selected artifact lane id must not be empty", strings.TrimSpace(pipelineID)) + } + if _, ok := lanesByID[laneID]; !ok { + return nil, fmt.Errorf("pipeline %q selected artifact lane %q is not declared", strings.TrimSpace(pipelineID), laneID) + } + if _, ok := seen[laneID]; !ok { + selectedIDs = append(selectedIDs, laneID) + seen[laneID] = struct{}{} + } + } + } + sort.Strings(selectedIDs) + + targets := make([]selectedReferenceTarget, 0, 1+len(selectedIDs)*3) + chunk := pipeline.Binding(profile.Chunk.Module) + chunk.Module = strings.TrimSpace(profile.Chunk.Module) + if chunk.Module == "" { + chunk.Module = pipeline.DefaultChunkModule + } + chunkSpec, err := cliReferenceChunkerSpec(catalog, chunk.Module) + if err != nil { + return nil, fmt.Errorf("pipeline %q chunk module %q: %w", strings.TrimSpace(pipelineID), chunk.Module, err) + } + targets = append(targets, selectedReferenceTarget{ + stage: pipeline.StageChunk, + module: chunk.Module, + slots: referenceSlotSet(chunkSpec.ReferenceSlots), + }) + + for _, laneID := range selectedIDs { + lane := lanesByID[laneID] + extractModule := strings.TrimSpace(lane.Extract.Module) + if extractModule == "" { + return nil, fmt.Errorf("pipeline %q lane %q extract module must not be empty", strings.TrimSpace(pipelineID), laneID) + } + extractSpec, err := cliReferenceExtractorSpec(catalog, extractModule) + if err != nil { + return nil, fmt.Errorf("pipeline %q lane %q extract module %q: %w", strings.TrimSpace(pipelineID), laneID, extractModule, err) + } + artifactKind := extractSpec.ArtifactKind + if artifactKind == "" { + return nil, fmt.Errorf("pipeline %q lane %q extract module %q does not declare an artifact kind", strings.TrimSpace(pipelineID), laneID, extractModule) + } + targets = append(targets, selectedReferenceTarget{ + laneID: laneID, + stage: pipeline.StageExtract, + module: extractModule, + slots: referenceSlotSet(extractSpec.ReferenceSlots), + }) + + mergeModule := strings.TrimSpace(lane.Merge.Module) + if mergeModule == "" { + mergeModule = pipeline.DefaultMergeModule + } + mergeSpec, err := cliReferenceMergerSpec(catalog, mergeModule, artifactKind) + if err != nil { + return nil, fmt.Errorf("pipeline %q lane %q merge module %q: %w", strings.TrimSpace(pipelineID), laneID, mergeModule, err) + } + targets = append(targets, selectedReferenceTarget{ + laneID: laneID, + stage: pipeline.StageMerge, + module: mergeModule, + slots: referenceSlotSet(mergeSpec.ReferenceSlots), + }) + + normalizeModule := strings.TrimSpace(lane.Normalize.Module) + if normalizeModule == "" { + normalizeModule = pipeline.DefaultNormalizeModule + } + normalizeSpec, err := cliReferenceNormalizerSpec(catalog, normalizeModule, artifactKind) + if err != nil { + return nil, fmt.Errorf("pipeline %q lane %q normalize module %q: %w", strings.TrimSpace(pipelineID), laneID, normalizeModule, err) + } + targets = append(targets, selectedReferenceTarget{ + laneID: laneID, + stage: pipeline.StageNormalize, + module: normalizeModule, + slots: referenceSlotSet(normalizeSpec.ReferenceSlots), + }) + } + return targets, nil +} + +func lookupCLIReferencePipeline(profiles map[string]pipeline.PipelineProfile, pipelineID string) (pipeline.PipelineProfile, bool) { + pipelineID = strings.TrimSpace(pipelineID) + for rawID, profile := range profiles { + if strings.TrimSpace(rawID) == pipelineID { + return profile, true + } + } + return pipeline.PipelineProfile{}, false +} + +func cliReferenceChunkerSpec(catalog pipeline.ModuleCatalog, module string) (pipeline.ModuleSpec, error) { + if catalog.Chunkers == nil { + return pipeline.ModuleSpec{}, fmt.Errorf("module %q is not registered", module) + } + spec, ok := catalog.Chunkers.Spec(module) + if !ok { + return pipeline.ModuleSpec{}, fmt.Errorf("module %q is not registered", module) + } + return spec, nil +} + +func cliReferenceExtractorSpec(catalog pipeline.ModuleCatalog, module string) (pipeline.ModuleSpec, error) { + if catalog.Extractors == nil { + return pipeline.ModuleSpec{}, fmt.Errorf("module %q is not registered", module) + } + spec, ok := catalog.Extractors.Spec(module) + if !ok { + return pipeline.ModuleSpec{}, fmt.Errorf("module %q is not registered", module) + } + return spec, nil +} + +func cliReferenceMergerSpec(catalog pipeline.ModuleCatalog, module string, kind contracts.ArtifactKind) (pipeline.ModuleSpec, error) { + if catalog.Mergers == nil { + return pipeline.ModuleSpec{}, fmt.Errorf("module %q is not registered", module) + } + registered := catalog.Mergers.RegisteredArtifactKinds(module) + if len(registered) == 0 { + return pipeline.ModuleSpec{}, fmt.Errorf("module %q is not registered", module) + } + spec, ok := catalog.Mergers.SpecForArtifact(module, kind) + if !ok { + return pipeline.ModuleSpec{}, cliReferenceArtifactVariantError("merger", module, kind, registered) + } + return spec, nil +} + +func cliReferenceNormalizerSpec(catalog pipeline.ModuleCatalog, module string, kind contracts.ArtifactKind) (pipeline.ModuleSpec, error) { + if catalog.Normalizers == nil { + return pipeline.ModuleSpec{}, fmt.Errorf("module %q is not registered", module) + } + registered := catalog.Normalizers.RegisteredArtifactKinds(module) + if len(registered) == 0 { + return pipeline.ModuleSpec{}, fmt.Errorf("module %q is not registered", module) + } + spec, ok := catalog.Normalizers.SpecForArtifact(module, kind) + if !ok { + return pipeline.ModuleSpec{}, cliReferenceArtifactVariantError("normalizer", module, kind, registered) + } + return spec, nil +} + +func cliReferenceArtifactVariantError(moduleType string, module string, kind contracts.ArtifactKind, registered []contracts.ArtifactKind) error { + values := make([]string, len(registered)) + for i, value := range registered { + values[i] = string(value) + } + if len(values) == 0 { + return fmt.Errorf("%s %q has no typed variant for artifact kind %q", moduleType, module, kind) + } + return fmt.Errorf("%s %q has no typed variant for artifact kind %q; registered kinds: %s", moduleType, module, kind, strings.Join(values, ", ")) +} + +func referenceSlotSet(slots []contracts.ReferenceSlot) map[string]struct{} { + slotSet := make(map[string]struct{}, len(slots)) + for _, slot := range slots { + slotSet[slot.Name] = struct{}{} + } + return slotSet +} + +func resolveCLIReferenceTargets(targets []selectedReferenceTarget, selector cliReferenceSelector) ([]selectedReferenceTarget, error) { + slotName := strings.TrimSpace(selector.SlotName) + if slotName == "" { + return nil, fmt.Errorf("reference slot must not be empty") + } + switch selector.Scope { + case cliReferenceScopePipeline: + matches := make([]selectedReferenceTarget, 0, len(targets)) + for _, target := range targets { + if _, ok := target.slots[slotName]; ok { + matches = append(matches, target) + } + } + if len(matches) == 0 { + return nil, fmt.Errorf("reference slot %q is not declared by any selected target", slotName) + } + return matches, nil + case cliReferenceScopeChunk: + for _, target := range targets { + if target.stage != pipeline.StageChunk { + continue + } + if _, ok := target.slots[slotName]; !ok { + return nil, fmt.Errorf("reference slot %q is not declared by chunk module %q", slotName, target.module) + } + return []selectedReferenceTarget{target}, nil + } + return nil, fmt.Errorf("reference chunk target is not selected") + case cliReferenceScopeLane: + laneSelected := false + matches := make([]selectedReferenceTarget, 0, 3) + for _, target := range targets { + if target.laneID != selector.LaneID { + continue + } + laneSelected = true + if _, ok := target.slots[slotName]; ok { + matches = append(matches, target) + } + } + if !laneSelected { + return nil, fmt.Errorf("reference lane %q is not selected", selector.LaneID) + } + if len(matches) == 0 { + return nil, fmt.Errorf("reference slot %q is not declared by selected lane %q", slotName, selector.LaneID) + } + return matches, nil + case cliReferenceScopeBinding: + laneSelected := false + for _, target := range targets { + if target.laneID != selector.LaneID { + continue + } + laneSelected = true + if target.stage != selector.Stage { + continue + } + if _, ok := target.slots[slotName]; !ok { + return nil, fmt.Errorf("reference slot %q is not declared by selected %s target %q", slotName, selector.Stage, targetLabel(target)) + } + return []selectedReferenceTarget{target}, nil + } + if !laneSelected { + return nil, fmt.Errorf("reference lane %q is not selected", selector.LaneID) + } + return nil, fmt.Errorf("reference %s target is not selected for lane %q", selector.Stage, selector.LaneID) + default: + return nil, fmt.Errorf("reference selector has unknown scope") + } +} + +func targetLabel(target selectedReferenceTarget) string { + if target.stage == pipeline.StageChunk { + return "chunk" + } + return target.laneID + "." + string(target.stage) +} + +func referenceStageOrder(stage pipeline.ModuleStage) int { + switch stage { + case pipeline.StageChunk: + return 0 + case pipeline.StageExtract: + return 1 + case pipeline.StageMerge: + return 2 + case pipeline.StageNormalize: + return 3 + default: + return 4 + } +} diff --git a/internal/cli/registries.go b/internal/cli/registries.go new file mode 100644 index 00000000..69a6e4a0 --- /dev/null +++ b/internal/cli/registries.go @@ -0,0 +1,59 @@ +package cli + +import "gitea.maximumdirect.net/eric/notarius/internal/framework/pipeline" + +func catalogFromRegistries(registries pipeline.Registries) pipeline.ModuleCatalog { + return pipeline.ModuleCatalog{ + Inputs: registries.Inputs, + Chunkers: registries.Chunkers, + ArtifactCodecs: registries.ArtifactCodecs, + ArtifactEvidence: registries.ArtifactEvidence, + Extractors: registries.Extractors, + Mergers: registries.Mergers, + Normalizers: registries.Normalizers, + Validators: registries.Validators, + ValidatorChains: registries.ValidatorChains, + Outputs: registries.Outputs, + } +} + +func registriesFromCatalog(catalog pipeline.ModuleCatalog) pipeline.Registries { + return pipeline.Registries{ + Inputs: catalog.Inputs, + Chunkers: catalog.Chunkers, + ArtifactCodecs: catalog.ArtifactCodecs, + ArtifactEvidence: catalog.ArtifactEvidence, + Extractors: catalog.Extractors, + Mergers: catalog.Mergers, + Normalizers: catalog.Normalizers, + Validators: catalog.Validators, + ValidatorChains: catalog.ValidatorChains, + Outputs: catalog.Outputs, + } +} + +func isEmptyCatalog(catalog pipeline.ModuleCatalog) bool { + return catalog.Inputs == nil && + catalog.Chunkers == nil && + catalog.ArtifactCodecs == nil && + catalog.ArtifactEvidence == nil && + catalog.Extractors == nil && + catalog.Mergers == nil && + catalog.Normalizers == nil && + catalog.Validators == nil && + catalog.ValidatorChains == nil && + catalog.Outputs == nil +} + +func isEmptyRegistries(registries pipeline.Registries) bool { + return registries.Inputs == nil && + registries.Chunkers == nil && + registries.ArtifactCodecs == nil && + registries.ArtifactEvidence == nil && + registries.Extractors == nil && + registries.Mergers == nil && + registries.Normalizers == nil && + registries.Validators == nil && + registries.ValidatorChains == nil && + registries.Outputs == nil +} diff --git a/internal/cli/run.go b/internal/cli/run.go index e26d32e7..56b37855 100644 --- a/internal/cli/run.go +++ b/internal/cli/run.go @@ -2,1255 +2,27 @@ package cli import ( "context" - "crypto/sha256" - "encoding/hex" - "errors" - "flag" "fmt" "io" - "os" - "path" - "path/filepath" - "sort" - "strings" - "time" "gitea.maximumdirect.net/eric/notarius/internal/buildinfo" - "gitea.maximumdirect.net/eric/notarius/internal/core/artifacts" - "gitea.maximumdirect.net/eric/notarius/internal/core/config" - "gitea.maximumdirect.net/eric/notarius/internal/core/fileio" - "gitea.maximumdirect.net/eric/notarius/internal/framework/artifactcache" - "gitea.maximumdirect.net/eric/notarius/internal/framework/contracts" - frameworkdebug "gitea.maximumdirect.net/eric/notarius/internal/framework/debug" - frameworkllm "gitea.maximumdirect.net/eric/notarius/internal/framework/llm" - "gitea.maximumdirect.net/eric/notarius/internal/framework/pipeline" "go.maximumdirect.net/commandkit" - configkit "go.maximumdirect.net/configkit" - "go.maximumdirect.net/promptkit" ) -type Options struct { - LLMClient promptkit.LLMClient - LLMRuntime promptkit.RuntimeConfig - Catalog pipeline.ModuleCatalog - Registries pipeline.Registries - LLMClientFactory LLMClientFactory - RunIDGenerator RunIDGenerator - LookupEnv func(string) (string, bool) - Now func() time.Time - UserCacheDir func() (string, error) - promptKitAssets *frameworkllm.AssetRegistry -} - -type LLMRuntimeOverrides struct { - ReasoningEffort *string -} - -type LLMClientFactory func(ctx context.Context, cfg config.Config, profileID string, overrides LLMRuntimeOverrides) (contracts.StructuredLLMClient, []artifacts.LLMProfileManifest, error) - -// Run executes the command-line interface and returns a process exit code. func Run(args []string, stdout, stderr io.Writer) int { return RunWithOptions(args, stdout, stderr, Options{}) } -func RunWithOptions(args []string, stdout, stderr io.Writer, opts Options) int { +func RunWithOptions(args []string, stdout, stderr io.Writer, options Options) int { version, err := buildinfo.Version() if err != nil { fmt.Fprintf(stderr, "notarius: %v\n", err) return 1 } - app, err := NewApplication(version, opts) + app, err := NewApplication(version, options) if err != nil { fmt.Fprintf(stderr, "notarius: %v\n", err) return 1 } return app.Execute(context.Background(), args, commandkit.Streams{Out: stdout, Err: stderr}).Status } - -func normalizeOptions(opts Options) (Options, error) { - if opts.LookupEnv == nil { - opts.LookupEnv = os.LookupEnv - } - if opts.Now == nil { - opts.Now = time.Now - } - if opts.RunIDGenerator == nil { - opts.RunIDGenerator = defaultRunIDGenerator - } - if opts.UserCacheDir == nil { - opts.UserCacheDir = os.UserCacheDir - } - if isEmptyCatalog(opts.Catalog) && isEmptyRegistries(opts.Registries) { - components, err := newProductionComponents() - if err != nil { - return Options{}, err - } - opts.Registries = components.registries - opts.Catalog = catalogFromRegistries(components.registries) - opts.promptKitAssets = components.assets - } - if opts.LLMClientFactory == nil { - if opts.promptKitAssets == nil { - assets, err := productionPromptAssets() - if err != nil { - return Options{}, err - } - opts.promptKitAssets = assets - } - opts.LLMClientFactory = productionLLMClientFactoryWithAssets(opts.promptKitAssets) - } - return opts, nil -} - -func runPipelineCommand(args []string, stdout, stderr io.Writer, opts Options) int { - fs := flag.NewFlagSet("run", flag.ContinueOnError) - fs.SetOutput(io.Discard) - configPath := fs.String("config", "", "config file path") - configOverlay := fs.String("config-overlay", "", "configuration overlay") - inputPath := fs.String("input", "", "source input file path") - onlyRaw := fs.String("only", "", "comma-separated artifact lanes") - outputDir := fs.String("output-dir", "", "output directory") - machineOutput := fs.Bool("json", false, "write the successful run result as JSON") - debug := fs.Bool("debug", false, "write a debug bundle") - debugDir := fs.String("debug-dir", "", "debug bundle directory") - llmProfile := singleValueFlag{name: "--llm-profile"} - reasoningEffort := singleValueFlag{name: "--reasoning-effort"} - clearReasoningEffort := fs.Bool("clear-reasoning-effort", false, "clear the LLM profile reasoning effort") - requestedSessionID := sessionIDFlag{} - referenceFlags := stringListFlag{} - withoutReferenceFlags := stringListFlag{} - fs.Var(&requestedSessionID, "session-id", "prompt session identifier") - fs.Var(&llmProfile, "llm-profile", "LLM profile override") - fs.Var(&reasoningEffort, "reasoning-effort", "reasoning effort override") - fs.Var(&referenceFlags, "reference", "reference binding, as slot=path, chunk.slot=path, lane.slot=path, lane.extract.slot=path, lane.merge.slot=path, or lane.normalize.slot=path") - fs.Var(&withoutReferenceFlags, "without-reference", "unbind a reference, using the same selector forms as --reference") - if err := validateRunFlagValues(args); err != nil { - fmt.Fprintf(stderr, "notarius: %v\n", err) - return 2 - } - if err := fs.Parse(reorderRunArgs(args)); err != nil { - fmt.Fprintf(stderr, "notarius: %v\n", err) - return 2 - } - if fs.NArg() == 0 { - fmt.Fprintln(stderr, "notarius: run requires a pipeline ID") - return 2 - } - if fs.NArg() > 1 { - fmt.Fprintf(stderr, "notarius: unexpected argument %q\n", fs.Arg(1)) - return 2 - } - pipelineID := strings.TrimSpace(fs.Arg(0)) - if pipelineID == "" { - fmt.Fprintln(stderr, "notarius: run requires a pipeline ID") - return 2 - } - if strings.TrimSpace(*inputPath) == "" { - fmt.Fprintln(stderr, "notarius: run requires --input") - return 2 - } - if strings.TrimSpace(*outputDir) == "" && flagWasProvided(args, "--output-dir") { - fmt.Fprintln(stderr, "notarius: --output-dir must not be empty") - return 2 - } - if requestedSessionID.set && strings.TrimSpace(requestedSessionID.value) == "" { - fmt.Fprintln(stderr, "notarius: --session-id must not be empty") - return 2 - } - if llmProfile.set && strings.TrimSpace(llmProfile.value) == "" { - fmt.Fprintln(stderr, "notarius: --llm-profile must not be empty") - return 2 - } - if reasoningEffort.set && *clearReasoningEffort { - fmt.Fprintln(stderr, "notarius: --reasoning-effort cannot be combined with --clear-reasoning-effort") - return 2 - } - if reasoningEffort.set && strings.TrimSpace(reasoningEffort.value) == "" { - fmt.Fprintln(stderr, "notarius: --reasoning-effort must not be empty") - return 2 - } - runtimeOverrides := LLMRuntimeOverrides{} - if reasoningEffort.set { - value := strings.TrimSpace(reasoningEffort.value) - runtimeOverrides.ReasoningEffort = &value - } else if *clearReasoningEffort { - value := "" - runtimeOverrides.ReasoningEffort = &value - } - only, err := parseOnly(*onlyRaw) - if err != nil { - fmt.Fprintf(stderr, "notarius: %v\n", err) - return 2 - } - referenceRequests, err := parseReferenceFlags(referenceFlags) - if err != nil { - fmt.Fprintf(stderr, "notarius: %v\n", err) - return 2 - } - referenceUnbindRequests, err := parseReferenceUnbindFlags(withoutReferenceFlags) - if err != nil { - fmt.Fprintf(stderr, "notarius: %v\n", err) - return 2 - } - - loaded, loadedConfigPath, err := loadConfigKit(*configPath, *configOverlay, runConfigurationOverrides(*outputDir), opts) - if err != nil { - writeConfigLoadError(stderr, err) - return 1 - } - cfg := loaded.RuntimeConfig() - if err := cfg.Validate(); err != nil { - fmt.Fprintf(stderr, "notarius: %v\n", err) - return 1 - } - startedAt := opts.Now().UTC() - runID, err := opts.RunIDGenerator(startedAt) - if err != nil { - fmt.Fprintf(stderr, "notarius: generate run ID: %v\n", err) - return 1 - } - if err := validateRunID(runID); err != nil { - fmt.Fprintf(stderr, "notarius: invalid generated run ID: %v\n", err) - return 1 - } - runOutputDir := filepath.Join(cfg.Output.Directory, runID) - - var capture *frameworkdebug.Recorder - var output pipeline.RunOutput - published := false - captureOutcome := func() any { - return map[string]any{"pipeline": pipelineID, "output_directory": runOutputDir, "artifact_count": output.ArtifactCount(), "fallback_count": output.FallbackCount, "published": published} - } - fail := func(primary error, extra ...error) int { - cause := errors.Join(append([]error{primary}, extra...)...) - cause = capture.Finalize(captureOutcome(), cause) - fmt.Fprintf(stderr, "notarius: %v\n", cause) - if published { - fmt.Fprintf(stderr, "notarius: artifacts=%s\n", runOutputDir) - } - if capture.Path() != "" { - fmt.Fprintf(stderr, "notarius: debug=%s\n", capture.Path()) - } - return 1 - } - capture, err = startRunDebug(runDebugInput{ - Enabled: commandkit.Supplied[bool]{Value: *debug, Set: flagWasProvided(args, "--debug")}, - Directory: commandkit.Supplied[string]{Value: *debugDir, Set: flagWasProvided(args, "--debug-dir")}, - }, cfg.Debug.Directory, runID, startedAt) - if err != nil { - return fail(err) - } - debugPath := capture.Path() - var debugRecorder pipeline.DebugRecorder = pipeline.NoopDebugRecorder() - if capture.Enabled() { - debugRecorder = newRunEvidenceRecorder(capture, loaded.Result()) - } - version, err := buildinfo.Version() - if err != nil { - return fail(err) - } - if err := capture.WriteJSON("context.json", map[string]any{"application_version": version, "pipeline": pipelineID, "input_path": *inputPath, "config_path": loadedConfigPath, "run_id": runID, "started_at": startedAt, "selected_lanes": only, "llm_profile": llmProfile.value, "llm_session_id": requestedSessionID.value, "execution_overrides": runtimeOverrides}); err != nil { - return fail(err) - } - if err := captureRunConfiguration(capture, loaded.Result()); err != nil { - return fail(err) - } - - catalog, err := effectiveCatalog(opts) - if err != nil { - return fail(err) - } - referenceOverrides, referenceUnbinds, err := resolveCLIReferenceRequests(cfg, pipelineID, only, catalog, referenceRequests, referenceUnbindRequests) - if err != nil { - return fail(err) - } - effective, err := cfg.Resolve(config.ResolveInput{ - PipelineID: pipelineID, - Only: only, - Catalog: catalog, - LLMProfileOverride: strings.TrimSpace(llmProfile.value), - ReferenceOverrides: referenceOverrides, - ReferenceUnbinds: referenceUnbinds, - }) - if err != nil { - return fail(err) - } - profileIDs := effectiveLLMProfileIDs(effective.ResolvedPipeline) - if err := validateExplicitPromptKitProfiles(context.Background(), effective.Config, profileIDs, opts.promptKitAssets); err != nil { - return fail(err) - } - workingDir, err := os.Getwd() - if err != nil { - return fail(fmt.Errorf("resolve working directory: %w", err)) - } - materialized, referenceDiagnostics, err := pipeline.MaterializeReferences(effective.ResolvedPipeline, catalog, pipeline.ReferenceMaterializationOptions{ - ConfigPath: loadedConfigPath, - WorkingDir: workingDir, - }) - if err != nil { - return fail(err) - } - effective.ResolvedPipeline = materialized - - registries, err := effectiveRegistries(opts) - if err != nil { - return fail(err) - } - - ctx := context.Background() - factoryProfileID := "" - if len(profileIDs) == 1 { - factoryProfileID = profileIDs[0] - } - llmClient, llmProfiles, err := opts.LLMClientFactory(ctx, effective.Config, factoryProfileID, runtimeOverrides) - if err != nil { - return fail(fmt.Errorf("create LLM client for profile %q: %w", factoryProfileID, err)) - } - prepared, err := pipeline.Prepare(effective.ResolvedPipeline, registries, pipeline.ModuleDependencies{LLM: llmClient}) - if err != nil { - return fail(fmt.Errorf("prepare pipeline %q: %w", pipelineID, err)) - } - rawInput, err := os.ReadFile(strings.TrimSpace(*inputPath)) - if err != nil { - return fail(fmt.Errorf("read input %q: %w", strings.TrimSpace(*inputPath), err)) - } - effectiveSessionID, err := resolvePromptSessionID(requestedSessionID.value, effective.ResolvedPipeline.Input.Module, rawInput) - if err != nil { - return fail(err) - } - cache, err := artifactcache.NewFilesystemStore(effective.Config.CacheDirectory) - if err != nil { - return fail(err) - } - - output, err = pipeline.New().Run(ctx, pipeline.RunInput{ - Prepared: prepared, - Path: strings.TrimSpace(*inputPath), - RawInput: rawInput, - SessionID: effectiveSessionID, - RunID: runID, - StartedAt: startedAt, - LLMProfiles: llmProfiles, - Metadata: runMetadata(effective.Config.Output.Directory, debugPath), - Diagnostics: referenceDiagnostics, - Cache: cache, - Debug: debugRecorder, - Workers: cfg.Concurrency.Workers, - }) - diagnosticProjection, diagnosticErr := contracts.ProjectDiagnosticCollection(output.Diagnostics) - if err != nil { - primaryErr := fmt.Errorf("run pipeline %q: %w", pipelineID, err) - if diagnosticErr != nil { - return fail(primaryErr, fmt.Errorf("summarize run diagnostics: %w", diagnosticErr)) - } - return fail(primaryErr) - } - if diagnosticErr != nil { - return fail(fmt.Errorf("summarize run diagnostics: %w", diagnosticErr)) - } - - var encodedResult []byte - if *machineOutput { - result, err := newRunResultWithDiagnostics(effective.ResolvedPipeline, output, runOutputDir, debugPath, diagnosticProjection) - if err != nil { - return fail(err) - } - encodedResult, err = encodeRunResult(result) - if err != nil { - return fail(err) - } - } - if err := writeOutputFiles(runOutputDir, output.OutputFiles); err != nil { - return fail(err) - } - published = true - if err := capture.Finalize(captureOutcome(), nil); err != nil { - return fail(err) - } - - if *machineOutput { - if err := writeRunResult(stdout, encodedResult); err != nil { - return fail(errors.New("write run result")) - } - } else { - fmt.Fprintf(stdout, "pipeline %q complete: artifacts=%d output=%s\n", effective.PipelineID, output.ArtifactCount(), runOutputDir) - if debugPath != "" { - fmt.Fprintf(stdout, "debug=%s\n", debugPath) - } - } - if warningGroups := len(diagnosticProjection.Warnings); warningGroups > 0 { - fmt.Fprintf(stderr, "notarius: run completed with %d warning group(s), %d occurrence(s)", warningGroups, diagnosticProjection.WarningOccurrenceCount) - if warningFile, ok := logicalOutputFile(output.OutputFiles, "warnings.json"); ok { - fmt.Fprintf(stderr, "; details=%s", filepath.Join(runOutputDir, warningFile)) - } - fmt.Fprintln(stderr) - } - return 0 -} - -func logicalOutputFile(files []contracts.OutputFile, name string) (string, bool) { - for _, file := range files { - if file.Name == name { - return file.Name, true - } - } - return "", false -} - -func rawInputDigest(data []byte) string { - sum := sha256.Sum256(data) - return "sha256:" + hex.EncodeToString(sum[:]) -} - -func configSource(configPath string) string { - if strings.TrimSpace(configPath) != "" { - return "flag" - } - return "discovered" -} - -func writeOutputFiles(runOutputDir string, files []contracts.OutputFile) error { - for _, file := range files { - if _, err := outputFilePath(runOutputDir, file.Name); err != nil { - return err - } - } - - outputParent := filepath.Dir(runOutputDir) - if err := os.MkdirAll(outputParent, 0o755); err != nil { - return fmt.Errorf("create output parent %q: %w", outputParent, err) - } - if err := os.Mkdir(runOutputDir, 0o755); err != nil { - if os.IsExist(err) { - return fmt.Errorf("output run directory %q already exists", runOutputDir) - } - return fmt.Errorf("create output run directory %q: %w", runOutputDir, err) - } - for _, file := range files { - if err := fileio.WriteBytes(runOutputDir, file.Name, file.Bytes, 0o755, 0o644); err != nil { - return fmt.Errorf("write output file %q: %w", file.Name, err) - } - } - return nil -} - -func outputFilePath(runOutputDir, logicalName string) (string, error) { - name := strings.TrimSpace(logicalName) - if name == "" { - return "", fmt.Errorf("output file name must not be empty") - } - if strings.Contains(name, `\`) { - return "", fmt.Errorf("output file name %q must use slash-separated relative paths", name) - } - if path.IsAbs(name) || filepath.IsAbs(name) { - return "", fmt.Errorf("output file name %q must be relative", name) - } - if strings.Contains(name, "..") { - return "", fmt.Errorf("output file name %q must not contain ..", name) - } - cleaned := path.Clean(name) - if cleaned == "." || cleaned != name { - return "", fmt.Errorf("output file name %q must be clean", name) - } - - root, err := filepath.Abs(runOutputDir) - if err != nil { - return "", fmt.Errorf("resolve output directory %q: %w", runOutputDir, err) - } - target, err := filepath.Abs(filepath.Join(root, filepath.FromSlash(cleaned))) - if err != nil { - return "", fmt.Errorf("resolve output file %q: %w", name, err) - } - rel, err := filepath.Rel(root, target) - if err != nil { - return "", fmt.Errorf("resolve output file %q: %w", name, err) - } - if rel == "." || strings.HasPrefix(rel, ".."+string(filepath.Separator)) || rel == ".." { - return "", fmt.Errorf("output file name %q resolves outside output directory", name) - } - return target, nil -} - -func reorderRunArgs(args []string) []string { - var flags []string - var positionals []string - for i := 0; i < len(args); i++ { - arg := args[i] - if arg == "--" { - positionals = append(positionals, args[i+1:]...) - break - } - if strings.HasPrefix(arg, "-") { - flags = append(flags, arg) - if runFlagTakesValue(arg) && !strings.Contains(arg, "=") && i+1 < len(args) { - i++ - flags = append(flags, args[i]) - } - continue - } - positionals = append(positionals, arg) - } - return append(flags, positionals...) -} - -func runFlagTakesValue(arg string) bool { - switch arg { - case "--config", "--config-overlay", "--input", "--only", "--output-dir", "--debug-dir", "--llm-profile", "--session-id", "--reasoning-effort", "--reference", "--without-reference": - return true - default: - return false - } -} - -func runConfigurationOverrides(outputDir string) []configkit.Override { - overrides := make([]configkit.Override, 0, 3) - if outputDir = strings.TrimSpace(outputDir); outputDir != "" { - overrides = append(overrides, configkit.Override{ - Path: configkit.Keys("output", "directory"), - Value: outputDir, - Flag: "--output-dir", - }) - } - return overrides -} - -func validateRunFlagValues(args []string) error { - for i, arg := range args { - if arg != "--config-overlay" && arg != "--session-id" && arg != "--reasoning-effort" { - continue - } - if i+1 >= len(args) || strings.HasPrefix(args[i+1], "-") { - return fmt.Errorf("flag needs an argument: %s", arg) - } - } - return nil -} - -func effectiveConfigOverlay(overlay string) string { - if overlay == "" || overlay == "default" { - return "" - } - return overlay -} - -func flagWasProvided(args []string, name string) bool { - for _, arg := range args { - if arg == name || strings.HasPrefix(arg, name+"=") { - return true - } - } - return false -} - -func effectiveLLMProfileIDs(resolved pipeline.ResolvedPipeline) []string { - seen := make(map[string]struct{}) - add := func(binding pipeline.ModuleBinding) { - id := strings.TrimSpace(binding.LLMProfile) - if id != "" { - seen[id] = struct{}{} - } - } - if resolved.InputExecutionClass == contracts.ExecutionClassLLMBacked { - add(resolved.Input) - } - if resolved.ChunkExecutionClass == contracts.ExecutionClassLLMBacked { - add(resolved.Chunk) - } - for _, lane := range resolved.AllArtifactLanes() { - if lane.ExtractExecutionClass == contracts.ExecutionClassLLMBacked { - add(lane.Extract) - } - if lane.MergeExecutionClass == contracts.ExecutionClassLLMBacked { - add(lane.Merge) - } - if lane.NormalizeExecutionClass == contracts.ExecutionClassLLMBacked { - add(lane.Normalize) - } - } - for _, chain := range resolved.ValidatorChains { - for _, validator := range chain.Validators { - if validator.ExecutionClass == contracts.ExecutionClassLLMBacked { - add(validator.Binding) - } - } - } - if resolved.OutputExecutionClass == contracts.ExecutionClassLLMBacked { - add(resolved.Output) - } - ids := make([]string, 0, len(seen)) - for id := range seen { - ids = append(ids, id) - } - sort.Strings(ids) - return ids -} - -func runMetadata(outputDir, debugDir string) map[string]any { - metadata := make(map[string]any) - if dir := strings.TrimSpace(outputDir); dir != "" { - metadata["output_dir"] = dir - } - if dir := strings.TrimSpace(debugDir); dir != "" { - metadata["debug_dir"] = dir - } - if len(metadata) == 0 { - return nil - } - return metadata -} - -func parseOnly(raw string) ([]string, error) { - if strings.TrimSpace(raw) == "" { - return nil, nil - } - parts := strings.Split(raw, ",") - result := make([]string, 0, len(parts)) - for _, part := range parts { - trimmed := strings.TrimSpace(part) - if trimmed == "" { - return nil, fmt.Errorf("--only must contain comma-separated non-empty artifact lane IDs") - } - result = append(result, trimmed) - } - return result, nil -} - -type stringListFlag []string - -func (flag *stringListFlag) String() string { - if flag == nil { - return "" - } - return strings.Join(*flag, ",") -} - -func (flag *stringListFlag) Set(value string) error { - *flag = append(*flag, value) - return nil -} - -type sessionIDFlag struct { - value string - set bool -} - -type singleValueFlag struct { - name string - value string - set bool -} - -func (flag *singleValueFlag) String() string { - if flag == nil { - return "" - } - return flag.value -} - -func (flag *singleValueFlag) Set(value string) error { - if flag.set { - return fmt.Errorf("%s may be specified only once", flag.name) - } - flag.value = value - flag.set = true - return nil -} - -func (flag *sessionIDFlag) String() string { - if flag == nil { - return "" - } - return flag.value -} - -func (flag *sessionIDFlag) Set(value string) error { - flag.value = value - flag.set = true - return nil -} - -type cliReferenceRequest struct { - Selector cliReferenceSelector - Source string -} - -type cliReferenceUnbindRequest struct { - Selector cliReferenceSelector -} - -type cliReferenceSelector struct { - Scope cliReferenceSelectorScope - LaneID string - Stage pipeline.ModuleStage - SlotName string -} - -type cliReferenceSelectorScope uint8 - -const ( - cliReferenceScopePipeline cliReferenceSelectorScope = iota - cliReferenceScopeLane - cliReferenceScopeChunk - cliReferenceScopeBinding -) - -func parseReferenceFlags(values []string) ([]cliReferenceRequest, error) { - if len(values) == 0 { - return nil, nil - } - requests := make([]cliReferenceRequest, 0, len(values)) - for _, raw := range values { - name, source, ok := strings.Cut(raw, "=") - if !ok { - return nil, fmt.Errorf("--reference must use slot=path, lane.slot=path, or lane.stage.slot=path") - } - if strings.TrimSpace(source) == "" { - return nil, fmt.Errorf("--reference path must not be empty; use --without-reference to unbind") - } - selector, err := parseReferenceSelector(name, "--reference") - if err != nil { - return nil, err - } - requests = append(requests, cliReferenceRequest{ - Selector: selector, - Source: strings.TrimSpace(source), - }) - } - return requests, nil -} - -func parseReferenceUnbindFlags(values []string) ([]cliReferenceUnbindRequest, error) { - if len(values) == 0 { - return nil, nil - } - requests := make([]cliReferenceUnbindRequest, 0, len(values)) - for _, raw := range values { - if strings.Contains(raw, "=") { - return nil, fmt.Errorf("--without-reference must use a reference selector without =path") - } - selector, err := parseReferenceSelector(raw, "--without-reference") - if err != nil { - return nil, err - } - requests = append(requests, cliReferenceUnbindRequest{ - Selector: selector, - }) - } - return requests, nil -} - -func parseReferenceSelector(raw string, flagName string) (cliReferenceSelector, error) { - selector := strings.TrimSpace(raw) - if selector == "" { - return cliReferenceSelector{}, fmt.Errorf("%s reference slot must not be empty", flagName) - } - parts := strings.Split(selector, ".") - for _, part := range parts { - if strings.TrimSpace(part) == "" { - return cliReferenceSelector{}, fmt.Errorf("%s must use non-empty reference selector values", flagName) - } - } - switch len(parts) { - case 1: - return cliReferenceSelector{Scope: cliReferenceScopePipeline, SlotName: strings.TrimSpace(parts[0])}, nil - case 2: - first := strings.TrimSpace(parts[0]) - slotName := strings.TrimSpace(parts[1]) - if first == string(pipeline.StageChunk) { - return cliReferenceSelector{Scope: cliReferenceScopeChunk, Stage: pipeline.StageChunk, SlotName: slotName}, nil - } - return cliReferenceSelector{Scope: cliReferenceScopeLane, LaneID: first, SlotName: slotName}, nil - case 3: - laneID := strings.TrimSpace(parts[0]) - stage := pipeline.ModuleStage(strings.TrimSpace(parts[1])) - slotName := strings.TrimSpace(parts[2]) - if stage != pipeline.StageExtract && stage != pipeline.StageMerge && stage != pipeline.StageNormalize { - return cliReferenceSelector{}, fmt.Errorf("%s lane-qualified selector must use lane.extract.slot, lane.merge.slot, or lane.normalize.slot", flagName) - } - return cliReferenceSelector{Scope: cliReferenceScopeBinding, LaneID: laneID, Stage: stage, SlotName: slotName}, nil - default: - return cliReferenceSelector{}, fmt.Errorf("%s must use slot, chunk.slot, lane.slot, lane.extract.slot, lane.merge.slot, or lane.normalize.slot", flagName) - } -} - -func resolveCLIReferenceRequests( - cfg config.Config, - pipelineID string, - only []string, - catalog pipeline.ModuleCatalog, - referenceRequests []cliReferenceRequest, - unbindRequests []cliReferenceUnbindRequest, -) ([]pipeline.ReferenceBinding, []pipeline.ReferenceUnbind, error) { - if len(referenceRequests) == 0 && len(unbindRequests) == 0 { - return nil, nil, nil - } - - targets, err := selectedReferenceTargets(cfg, pipelineID, only, catalog) - if err != nil { - return nil, nil, err - } - - // Broad CLI selectors are only presentation syntax. Collapse them into one - // highest-specificity action per concrete framework target before pipeline - // resolution so the generic reference contract stays stage-and-lane exact. - actions := make(map[cliReferenceTargetKey]resolvedCLIReferenceAction) - for _, request := range referenceRequests { - matches, err := resolveCLIReferenceTargets(targets, request.Selector) - if err != nil { - return nil, nil, err - } - for _, target := range matches { - candidate := resolvedCLIReferenceAction{ - kind: cliReferenceActionBind, - selector: request.Selector, - target: target, - slotName: request.Selector.SlotName, - source: request.Source, - specificity: request.Selector.specificity(), - } - if err := mergeCLIReferenceAction(actions, candidate); err != nil { - return nil, nil, err - } - } - } - for _, request := range unbindRequests { - matches, err := resolveCLIReferenceTargets(targets, request.Selector) - if err != nil { - return nil, nil, err - } - for _, target := range matches { - candidate := resolvedCLIReferenceAction{ - kind: cliReferenceActionUnbind, - selector: request.Selector, - target: target, - slotName: request.Selector.SlotName, - specificity: request.Selector.specificity(), - } - if err := mergeCLIReferenceAction(actions, candidate); err != nil { - return nil, nil, err - } - } - } - - resolved := make([]resolvedCLIReferenceAction, 0, len(actions)) - for _, action := range actions { - resolved = append(resolved, action) - } - sort.Slice(resolved, func(i, j int) bool { - left, right := resolved[i], resolved[j] - if left.target.laneID != right.target.laneID { - return left.target.laneID < right.target.laneID - } - if left.target.stage != right.target.stage { - return referenceStageOrder(left.target.stage) < referenceStageOrder(right.target.stage) - } - return left.slotName < right.slotName - }) - - overrides := make([]pipeline.ReferenceBinding, 0, len(resolved)) - unbinds := make([]pipeline.ReferenceUnbind, 0, len(resolved)) - for _, action := range resolved { - switch action.kind { - case cliReferenceActionBind: - overrides = append(overrides, pipeline.ReferenceBinding{ - Stage: action.target.stage, - LaneID: action.target.laneID, - SlotName: action.slotName, - Source: action.source, - BindingSource: contracts.ReferenceBindingSourceCLI, - }) - case cliReferenceActionUnbind: - unbinds = append(unbinds, pipeline.ReferenceUnbind{ - Stage: action.target.stage, - LaneID: action.target.laneID, - SlotName: action.slotName, - }) - } - } - return overrides, unbinds, nil -} - -type cliReferenceActionKind uint8 - -const ( - cliReferenceActionBind cliReferenceActionKind = iota - cliReferenceActionUnbind -) - -type cliReferenceTargetKey struct { - stage pipeline.ModuleStage - laneID string - slotName string -} - -type resolvedCLIReferenceAction struct { - kind cliReferenceActionKind - selector cliReferenceSelector - target selectedReferenceTarget - slotName string - source string - specificity int -} - -func mergeCLIReferenceAction(actions map[cliReferenceTargetKey]resolvedCLIReferenceAction, candidate resolvedCLIReferenceAction) error { - key := cliReferenceTargetKey{stage: candidate.target.stage, laneID: candidate.target.laneID, slotName: candidate.slotName} - current, ok := actions[key] - if !ok || candidate.specificity > current.specificity { - actions[key] = candidate - return nil - } - if candidate.specificity < current.specificity { - return nil - } - if candidate.kind != current.kind { - return fmt.Errorf("reference target %q slot %q is both bound by %q and unbound by %q at the same specificity", targetLabel(candidate.target), candidate.slotName, current.selector.String(), candidate.selector.String()) - } - actions[key] = candidate - return nil -} - -func (selector cliReferenceSelector) specificity() int { - switch selector.Scope { - case cliReferenceScopePipeline: - return 0 - case cliReferenceScopeLane: - return 1 - case cliReferenceScopeChunk, cliReferenceScopeBinding: - return 2 - default: - return -1 - } -} - -func (selector cliReferenceSelector) String() string { - switch selector.Scope { - case cliReferenceScopePipeline: - return selector.SlotName - case cliReferenceScopeLane: - return selector.LaneID + "." + selector.SlotName - case cliReferenceScopeChunk: - return "chunk." + selector.SlotName - case cliReferenceScopeBinding: - return selector.LaneID + "." + string(selector.Stage) + "." + selector.SlotName - default: - return selector.SlotName - } -} - -type selectedReferenceTarget struct { - laneID string - stage pipeline.ModuleStage - module string - slots map[string]struct{} -} - -func selectedReferenceTargets(cfg config.Config, pipelineID string, only []string, catalog pipeline.ModuleCatalog) ([]selectedReferenceTarget, error) { - profile, ok := lookupCLIReferencePipeline(cfg.Pipelines, pipelineID) - if !ok { - return nil, fmt.Errorf("pipeline %q is not configured", strings.TrimSpace(pipelineID)) - } - if profile.Steps != nil && len(only) > 0 { - return nil, fmt.Errorf("pipeline %q --only is not supported for explicit ordered steps", strings.TrimSpace(pipelineID)) - } - - lanesByID := make(map[string]pipeline.ArtifactLaneProfile, len(profile.Artifacts)) - addLanes := func(values map[string]pipeline.ArtifactLaneProfile) error { - for rawLaneID, lane := range values { - laneID := strings.TrimSpace(rawLaneID) - if laneID == "" { - return fmt.Errorf("pipeline %q artifact lane id must not be empty", strings.TrimSpace(pipelineID)) - } - if _, ok := lanesByID[laneID]; ok { - return fmt.Errorf("pipeline %q artifact lane %q is duplicated after trimming", strings.TrimSpace(pipelineID), laneID) - } - lanesByID[laneID] = lane - } - return nil - } - if profile.Steps == nil { - if err := addLanes(profile.Artifacts); err != nil { - return nil, err - } - } else { - for _, step := range profile.Steps { - if err := addLanes(step.Artifacts); err != nil { - return nil, err - } - } - } - - selectedIDs := make([]string, 0, len(lanesByID)) - if len(only) == 0 { - for laneID := range lanesByID { - selectedIDs = append(selectedIDs, laneID) - } - } else { - seen := make(map[string]struct{}, len(only)) - for _, rawLaneID := range only { - laneID := strings.TrimSpace(rawLaneID) - if laneID == "" { - return nil, fmt.Errorf("pipeline %q selected artifact lane id must not be empty", strings.TrimSpace(pipelineID)) - } - if _, ok := lanesByID[laneID]; !ok { - return nil, fmt.Errorf("pipeline %q selected artifact lane %q is not declared", strings.TrimSpace(pipelineID), laneID) - } - if _, ok := seen[laneID]; !ok { - selectedIDs = append(selectedIDs, laneID) - seen[laneID] = struct{}{} - } - } - } - sort.Strings(selectedIDs) - - targets := make([]selectedReferenceTarget, 0, 1+len(selectedIDs)*3) - chunk := pipeline.Binding(profile.Chunk.Module) - chunk.Module = strings.TrimSpace(profile.Chunk.Module) - if chunk.Module == "" { - chunk.Module = pipeline.DefaultChunkModule - } - chunkSpec, err := cliReferenceChunkerSpec(catalog, chunk.Module) - if err != nil { - return nil, fmt.Errorf("pipeline %q chunk module %q: %w", strings.TrimSpace(pipelineID), chunk.Module, err) - } - targets = append(targets, selectedReferenceTarget{ - stage: pipeline.StageChunk, - module: chunk.Module, - slots: referenceSlotSet(chunkSpec.ReferenceSlots), - }) - - for _, laneID := range selectedIDs { - lane := lanesByID[laneID] - extractModule := strings.TrimSpace(lane.Extract.Module) - if extractModule == "" { - return nil, fmt.Errorf("pipeline %q lane %q extract module must not be empty", strings.TrimSpace(pipelineID), laneID) - } - extractSpec, err := cliReferenceExtractorSpec(catalog, extractModule) - if err != nil { - return nil, fmt.Errorf("pipeline %q lane %q extract module %q: %w", strings.TrimSpace(pipelineID), laneID, extractModule, err) - } - artifactKind := extractSpec.ArtifactKind - if artifactKind == "" { - return nil, fmt.Errorf("pipeline %q lane %q extract module %q does not declare an artifact kind", strings.TrimSpace(pipelineID), laneID, extractModule) - } - targets = append(targets, selectedReferenceTarget{ - laneID: laneID, - stage: pipeline.StageExtract, - module: extractModule, - slots: referenceSlotSet(extractSpec.ReferenceSlots), - }) - - mergeModule := strings.TrimSpace(lane.Merge.Module) - if mergeModule == "" { - mergeModule = pipeline.DefaultMergeModule - } - mergeSpec, err := cliReferenceMergerSpec(catalog, mergeModule, artifactKind) - if err != nil { - return nil, fmt.Errorf("pipeline %q lane %q merge module %q: %w", strings.TrimSpace(pipelineID), laneID, mergeModule, err) - } - targets = append(targets, selectedReferenceTarget{ - laneID: laneID, - stage: pipeline.StageMerge, - module: mergeModule, - slots: referenceSlotSet(mergeSpec.ReferenceSlots), - }) - - normalizeModule := strings.TrimSpace(lane.Normalize.Module) - if normalizeModule == "" { - normalizeModule = pipeline.DefaultNormalizeModule - } - normalizeSpec, err := cliReferenceNormalizerSpec(catalog, normalizeModule, artifactKind) - if err != nil { - return nil, fmt.Errorf("pipeline %q lane %q normalize module %q: %w", strings.TrimSpace(pipelineID), laneID, normalizeModule, err) - } - targets = append(targets, selectedReferenceTarget{ - laneID: laneID, - stage: pipeline.StageNormalize, - module: normalizeModule, - slots: referenceSlotSet(normalizeSpec.ReferenceSlots), - }) - } - return targets, nil -} - -func lookupCLIReferencePipeline(profiles map[string]pipeline.PipelineProfile, pipelineID string) (pipeline.PipelineProfile, bool) { - pipelineID = strings.TrimSpace(pipelineID) - for rawID, profile := range profiles { - if strings.TrimSpace(rawID) == pipelineID { - return profile, true - } - } - return pipeline.PipelineProfile{}, false -} - -func cliReferenceChunkerSpec(catalog pipeline.ModuleCatalog, module string) (pipeline.ModuleSpec, error) { - if catalog.Chunkers == nil { - return pipeline.ModuleSpec{}, fmt.Errorf("module %q is not registered", module) - } - spec, ok := catalog.Chunkers.Spec(module) - if !ok { - return pipeline.ModuleSpec{}, fmt.Errorf("module %q is not registered", module) - } - return spec, nil -} - -func cliReferenceExtractorSpec(catalog pipeline.ModuleCatalog, module string) (pipeline.ModuleSpec, error) { - if catalog.Extractors == nil { - return pipeline.ModuleSpec{}, fmt.Errorf("module %q is not registered", module) - } - spec, ok := catalog.Extractors.Spec(module) - if !ok { - return pipeline.ModuleSpec{}, fmt.Errorf("module %q is not registered", module) - } - return spec, nil -} - -func cliReferenceMergerSpec(catalog pipeline.ModuleCatalog, module string, kind contracts.ArtifactKind) (pipeline.ModuleSpec, error) { - if catalog.Mergers == nil { - return pipeline.ModuleSpec{}, fmt.Errorf("module %q is not registered", module) - } - registered := catalog.Mergers.RegisteredArtifactKinds(module) - if len(registered) == 0 { - return pipeline.ModuleSpec{}, fmt.Errorf("module %q is not registered", module) - } - spec, ok := catalog.Mergers.SpecForArtifact(module, kind) - if !ok { - return pipeline.ModuleSpec{}, cliReferenceArtifactVariantError("merger", module, kind, registered) - } - return spec, nil -} - -func cliReferenceNormalizerSpec(catalog pipeline.ModuleCatalog, module string, kind contracts.ArtifactKind) (pipeline.ModuleSpec, error) { - if catalog.Normalizers == nil { - return pipeline.ModuleSpec{}, fmt.Errorf("module %q is not registered", module) - } - registered := catalog.Normalizers.RegisteredArtifactKinds(module) - if len(registered) == 0 { - return pipeline.ModuleSpec{}, fmt.Errorf("module %q is not registered", module) - } - spec, ok := catalog.Normalizers.SpecForArtifact(module, kind) - if !ok { - return pipeline.ModuleSpec{}, cliReferenceArtifactVariantError("normalizer", module, kind, registered) - } - return spec, nil -} - -func cliReferenceArtifactVariantError(moduleType string, module string, kind contracts.ArtifactKind, registered []contracts.ArtifactKind) error { - values := make([]string, len(registered)) - for i, value := range registered { - values[i] = string(value) - } - if len(values) == 0 { - return fmt.Errorf("%s %q has no typed variant for artifact kind %q", moduleType, module, kind) - } - return fmt.Errorf("%s %q has no typed variant for artifact kind %q; registered kinds: %s", moduleType, module, kind, strings.Join(values, ", ")) -} - -func referenceSlotSet(slots []contracts.ReferenceSlot) map[string]struct{} { - slotSet := make(map[string]struct{}, len(slots)) - for _, slot := range slots { - slotSet[slot.Name] = struct{}{} - } - return slotSet -} - -func resolveCLIReferenceTargets(targets []selectedReferenceTarget, selector cliReferenceSelector) ([]selectedReferenceTarget, error) { - slotName := strings.TrimSpace(selector.SlotName) - if slotName == "" { - return nil, fmt.Errorf("reference slot must not be empty") - } - switch selector.Scope { - case cliReferenceScopePipeline: - matches := make([]selectedReferenceTarget, 0, len(targets)) - for _, target := range targets { - if _, ok := target.slots[slotName]; ok { - matches = append(matches, target) - } - } - if len(matches) == 0 { - return nil, fmt.Errorf("reference slot %q is not declared by any selected target", slotName) - } - return matches, nil - case cliReferenceScopeChunk: - for _, target := range targets { - if target.stage != pipeline.StageChunk { - continue - } - if _, ok := target.slots[slotName]; !ok { - return nil, fmt.Errorf("reference slot %q is not declared by chunk module %q", slotName, target.module) - } - return []selectedReferenceTarget{target}, nil - } - return nil, fmt.Errorf("reference chunk target is not selected") - case cliReferenceScopeLane: - laneSelected := false - matches := make([]selectedReferenceTarget, 0, 3) - for _, target := range targets { - if target.laneID != selector.LaneID { - continue - } - laneSelected = true - if _, ok := target.slots[slotName]; ok { - matches = append(matches, target) - } - } - if !laneSelected { - return nil, fmt.Errorf("reference lane %q is not selected", selector.LaneID) - } - if len(matches) == 0 { - return nil, fmt.Errorf("reference slot %q is not declared by selected lane %q", slotName, selector.LaneID) - } - return matches, nil - case cliReferenceScopeBinding: - laneSelected := false - for _, target := range targets { - if target.laneID != selector.LaneID { - continue - } - laneSelected = true - if target.stage != selector.Stage { - continue - } - if _, ok := target.slots[slotName]; !ok { - return nil, fmt.Errorf("reference slot %q is not declared by selected %s target %q", slotName, selector.Stage, targetLabel(target)) - } - return []selectedReferenceTarget{target}, nil - } - if !laneSelected { - return nil, fmt.Errorf("reference lane %q is not selected", selector.LaneID) - } - return nil, fmt.Errorf("reference %s target is not selected for lane %q", selector.Stage, selector.LaneID) - default: - return nil, fmt.Errorf("reference selector has unknown scope") - } -} - -func targetLabel(target selectedReferenceTarget) string { - if target.stage == pipeline.StageChunk { - return "chunk" - } - return target.laneID + "." + string(target.stage) -} - -func referenceStageOrder(stage pipeline.ModuleStage) int { - switch stage { - case pipeline.StageChunk: - return 0 - case pipeline.StageExtract: - return 1 - case pipeline.StageMerge: - return 2 - case pipeline.StageNormalize: - return 3 - default: - return 4 - } -} - -func sortedPipelineIDs(cfg config.Config) []string { - ids := make([]string, 0, len(cfg.Pipelines)) - for id := range cfg.Pipelines { - ids = append(ids, strings.TrimSpace(id)) - } - sort.Strings(ids) - return ids -} diff --git a/internal/cli/run_input.go b/internal/cli/run_input.go new file mode 100644 index 00000000..a548bf9a --- /dev/null +++ b/internal/cli/run_input.go @@ -0,0 +1,78 @@ +package cli + +import ( + "fmt" + "strings" + + "gitea.maximumdirect.net/eric/notarius/internal/framework/contracts" + "go.maximumdirect.net/ckconfig" + "go.maximumdirect.net/commandkit" +) + +type pipelineRunInput struct { + Selection ckconfig.Selection + PipelineID string + InputFile commandkit.Supplied[string] + Lanes commandkit.Supplied[[]string] + OutputDirectory commandkit.Supplied[string] + Cache commandkit.Supplied[contracts.CacheMode] + References commandkit.Supplied[[]cliReferenceRequest] + Unbinds commandkit.Supplied[[]cliReferenceUnbindRequest] + Debug runDebugInput + Overrides executionOverrides +} + +func runFlags() []commandkit.Flag[pipelineRunInput] { + flags := configurationSelectionFlags(func(input *pipelineRunInput) *ckconfig.Selection { return &input.Selection }) + flags = append(flags, + commandkit.ValueFlag(commandkit.FlagMeta{Name: "input-file", Usage: "Source input file.", Completion: commandkit.CompleteFiles()}, (*string)(nil), parseRequiredRunValue, func(input *pipelineRunInput, value commandkit.Supplied[string]) { input.InputFile = value }), + commandkit.RepeatFlag(commandkit.FlagMeta{Name: "lane", Usage: "Select a literal artifact lane identifier; repeat for multiple lanes."}, []string(nil), parseRequiredRunValue, func(input *pipelineRunInput, value commandkit.Supplied[[]string]) { input.Lanes = value }), + commandkit.ValueFlag(commandkit.FlagMeta{Name: "output-dir", Usage: "Output directory.", Completion: commandkit.CompleteFiles()}, (*string)(nil), parseRequiredRunValue, func(input *pipelineRunInput, value commandkit.Supplied[string]) { input.OutputDirectory = value }), + commandkit.ValueFlag(commandkit.FlagMeta{Name: "cache", Usage: "Use auto, bypass, or refresh for all cacheable work.", Completion: commandkit.CompleteChoices(commandkit.CompletionChoice{Value: "auto"}, commandkit.CompletionChoice{Value: "bypass"}, commandkit.CompletionChoice{Value: "refresh"})}, (*contracts.CacheMode)(nil), func(value string) (contracts.CacheMode, error) { + mode := contracts.CacheMode(value) + switch mode { + case contracts.CacheAuto, contracts.CacheBypass, contracts.CacheRefresh: + return mode, nil + default: + return "", fmt.Errorf("cache must be auto, bypass, or refresh") + } + }, func(input *pipelineRunInput, value commandkit.Supplied[contracts.CacheMode]) { input.Cache = value }), + commandkit.RepeatFlag(commandkit.FlagMeta{Name: "reference", Usage: "Bind SELECTOR=PATH to compatible reference slots."}, []cliReferenceRequest(nil), func(value string) (cliReferenceRequest, error) { + parsed, err := parseReferenceFlags([]string{value}) + if err != nil { + return cliReferenceRequest{}, err + } + return parsed[0], nil + }, func(input *pipelineRunInput, value commandkit.Supplied[[]cliReferenceRequest]) { + input.References = value + }), + commandkit.RepeatFlag(commandkit.FlagMeta{Name: "without-reference", Usage: "Unbind an optional reference SELECTOR."}, []cliReferenceUnbindRequest(nil), func(value string) (cliReferenceUnbindRequest, error) { + parsed, err := parseReferenceUnbindFlags([]string{value}) + if err != nil { + return cliReferenceUnbindRequest{}, err + } + return parsed[0], nil + }, func(input *pipelineRunInput, value commandkit.Supplied[[]cliReferenceUnbindRequest]) { + input.Unbinds = value + }), + ) + flags = append(flags, runDebugFlags(func(input *pipelineRunInput) *runDebugInput { return &input.Debug })...) + return append(flags, executionOverrideFlags(func(input *pipelineRunInput) *executionOverrides { return &input.Overrides })...) +} + +func parseRequiredRunValue(value string) (string, error) { + if strings.TrimSpace(value) == "" { + return "", fmt.Errorf("value must not be empty") + } + return value, nil +} + +func checkPipelineRunInput(input pipelineRunInput) error { + if err := checkConfigSelection(input.Selection); err != nil { + return err + } + if !input.InputFile.Set { + return fmt.Errorf("--input-file is required") + } + return nil +} diff --git a/internal/cli/run_output.go b/internal/cli/run_output.go new file mode 100644 index 00000000..375e05b5 --- /dev/null +++ b/internal/cli/run_output.go @@ -0,0 +1,74 @@ +package cli + +import ( + "fmt" + "os" + "path" + "path/filepath" + "strings" + + "gitea.maximumdirect.net/eric/notarius/internal/core/fileio" + "gitea.maximumdirect.net/eric/notarius/internal/framework/contracts" +) + +func writeOutputFiles(runOutputDir string, files []contracts.OutputFile) error { + for _, file := range files { + if _, err := outputFilePath(runOutputDir, file.Name); err != nil { + return err + } + } + + outputParent := filepath.Dir(runOutputDir) + if err := os.MkdirAll(outputParent, 0o755); err != nil { + return fmt.Errorf("create output parent %q: %w", outputParent, err) + } + if err := os.Mkdir(runOutputDir, 0o755); err != nil { + if os.IsExist(err) { + return fmt.Errorf("output run directory %q already exists", runOutputDir) + } + return fmt.Errorf("create output run directory %q: %w", runOutputDir, err) + } + for _, file := range files { + if err := fileio.WriteBytes(runOutputDir, file.Name, file.Bytes, 0o755, 0o644); err != nil { + return fmt.Errorf("write output file %q: %w", file.Name, err) + } + } + return nil +} + +func outputFilePath(runOutputDir, logicalName string) (string, error) { + name := strings.TrimSpace(logicalName) + if name == "" { + return "", fmt.Errorf("output file name must not be empty") + } + if strings.Contains(name, `\`) { + return "", fmt.Errorf("output file name %q must use slash-separated relative paths", name) + } + if path.IsAbs(name) || filepath.IsAbs(name) { + return "", fmt.Errorf("output file name %q must be relative", name) + } + if strings.Contains(name, "..") { + return "", fmt.Errorf("output file name %q must not contain ..", name) + } + cleaned := path.Clean(name) + if cleaned == "." || cleaned != name { + return "", fmt.Errorf("output file name %q must be clean", name) + } + + root, err := filepath.Abs(runOutputDir) + if err != nil { + return "", fmt.Errorf("resolve output directory %q: %w", runOutputDir, err) + } + target, err := filepath.Abs(filepath.Join(root, filepath.FromSlash(cleaned))) + if err != nil { + return "", fmt.Errorf("resolve output file %q: %w", name, err) + } + rel, err := filepath.Rel(root, target) + if err != nil { + return "", fmt.Errorf("resolve output file %q: %w", name, err) + } + if rel == "." || strings.HasPrefix(rel, ".."+string(filepath.Separator)) || rel == ".." { + return "", fmt.Errorf("output file name %q resolves outside output directory", name) + } + return target, nil +} diff --git a/internal/cli/session.go b/internal/cli/session.go index 2c47a040..470f16f7 100644 --- a/internal/cli/session.go +++ b/internal/cli/session.go @@ -5,10 +5,19 @@ import ( "encoding/hex" "fmt" "strings" + + "go.maximumdirect.net/commandkit" ) const generatedSessionIDPrefix = "notarius:v1:" +func runSessionID(explicit commandkit.Supplied[string], inputModule string, rawInput []byte) (string, error) { + if explicit.Set { + return strings.TrimSpace(explicit.Value), nil + } + return resolvePromptSessionID("", inputModule, rawInput) +} + func resolvePromptSessionID(explicitSessionID, inputModule string, rawInput []byte) (string, error) { inputModule = strings.TrimSpace(inputModule) if inputModule == "" { diff --git a/internal/core/config/effective_config.go b/internal/core/config/effective_config.go index 15a141e8..4ce9adc3 100644 --- a/internal/core/config/effective_config.go +++ b/internal/core/config/effective_config.go @@ -14,7 +14,7 @@ type ResolveInput struct { PipelineID string Only []string Catalog pipeline.ModuleCatalog - LLMProfileOverride string + LLMProfileOverride *string ReferenceOverrides []pipeline.ReferenceBinding ReferenceUnbinds []pipeline.ReferenceUnbind } @@ -56,7 +56,7 @@ func (c Config) Resolve(input ResolveInput) (EffectiveConfig, error) { resolved, err := pipeline.ResolvePipeline(profile, pipeline.ResolveOptions{ Only: input.Only, CacheOverride: cloneValue(input.CacheOverride), - LLMProfileOverride: input.LLMProfileOverride, + LLMProfileOverride: cloneValue(input.LLMProfileOverride), ReferenceOverrides: append([]pipeline.ReferenceBinding(nil), input.ReferenceOverrides...), ReferenceUnbinds: append([]pipeline.ReferenceUnbind(nil), input.ReferenceUnbinds...), }, input.Catalog) diff --git a/internal/core/config/effective_config_contract_test.go b/internal/core/config/effective_config_contract_test.go index 3010c0f0..e1625c82 100644 --- a/internal/core/config/effective_config_contract_test.go +++ b/internal/core/config/effective_config_contract_test.go @@ -174,7 +174,7 @@ func TestEffectiveConfigLLMProfileOverrideChangesDigestAndOverridesValidators(t if err != nil { t.Fatalf("base Resolve() error = %v", err) } - overridden, err := resolveEffectiveProfile(t, profile, ResolveInput{LLMProfileOverride: "override-profile"}) + overridden, err := resolveEffectiveProfile(t, profile, ResolveInput{LLMProfileOverride: documentValue("override-profile")}) if err != nil { t.Fatalf("overridden Resolve() error = %v", err) } diff --git a/internal/framework/pipeline/profile.go b/internal/framework/pipeline/profile.go index 6bd5d683..60a96dd3 100644 --- a/internal/framework/pipeline/profile.go +++ b/internal/framework/pipeline/profile.go @@ -176,7 +176,7 @@ type PipelineProfile struct { type ResolveOptions struct { CacheOverride *contracts.CacheMode Only []string - LLMProfileOverride string + LLMProfileOverride *string ReferenceOverrides []ReferenceBinding ReferenceUnbinds []ReferenceUnbind } @@ -345,7 +345,7 @@ func ResolvePipeline(profile PipelineProfile, options ResolveOptions, catalog Mo return ResolvedPipeline{}, fmt.Errorf("pipeline %q must not declare both artifacts and steps", pipelineID) } if explicitSteps && len(options.Only) > 0 { - return ResolvedPipeline{}, fmt.Errorf("pipeline %q --only is not supported for explicit ordered steps", pipelineID) + return ResolvedPipeline{}, fmt.Errorf("pipeline %q lane selection is not supported for explicit ordered steps", pipelineID) } steps := profile.Steps if !explicitSteps { @@ -1367,9 +1367,8 @@ func resolveModuleKey(module string, defaultModule string) string { return module } -func applyEffectiveLLMProfiles(resolved *ResolvedPipeline, pipelineProfile, overrideProfile string) error { +func applyEffectiveLLMProfiles(resolved *ResolvedPipeline, pipelineProfile string, overrideProfile *string) error { pipelineProfile = strings.TrimSpace(pipelineProfile) - overrideProfile = strings.TrimSpace(overrideProfile) apply := func(stage ModuleStage, laneID, module string, binding *ModuleBinding, executionClass contracts.ExecutionClass, kind string) error { binding.LLMProfile = strings.TrimSpace(binding.LLMProfile) @@ -1382,8 +1381,8 @@ func applyEffectiveLLMProfiles(resolved *ResolvedPipeline, pipelineProfile, over } return nil } - if overrideProfile != "" { - binding.LLMProfile = overrideProfile + if overrideProfile != nil { + binding.LLMProfile = strings.TrimSpace(*overrideProfile) return nil } if binding.LLMProfile == "" { diff --git a/internal/framework/pipeline/profile_test.go b/internal/framework/pipeline/profile_test.go index 000531a7..614af0f0 100644 --- a/internal/framework/pipeline/profile_test.go +++ b/internal/framework/pipeline/profile_test.go @@ -381,6 +381,7 @@ func TestResolvePipelineRejectsEmptyReferenceSlotAfterTrimming(t *testing.T) { } func TestResolvePipelineAppliesEffectiveLLMProfiles(t *testing.T) { + runtimeProfile := " runtime " for _, test := range []struct { name string profile PipelineProfile @@ -395,7 +396,7 @@ func TestResolvePipelineAppliesEffectiveLLMProfiles(t *testing.T) { profile.Chunk.LLMProfile = "binding" return profile }(), - options: ResolveOptions{LLMProfileOverride: " runtime "}, + options: ResolveOptions{LLMProfileOverride: &runtimeProfile}, want: llmProfileValues("runtime"), }, { @@ -464,10 +465,11 @@ func TestResolvePipelineAppliesProfilesOnlyToSelectedLLMBackedBindings(t *testin } func TestResolvePipelineLeavesUnusedProfilesOffDeterministicBindings(t *testing.T) { + override := "also-unused" profile := baselineProfile() profile.LLMProfile = "unused" - resolved, err := ResolvePipeline(profile, ResolveOptions{LLMProfileOverride: "also-unused"}, newProfileCatalog(t)) + resolved, err := ResolvePipeline(profile, ResolveOptions{LLMProfileOverride: &override}, newProfileCatalog(t)) if err != nil { t.Fatalf("ResolvePipeline() error = %v, want nil", err) }