Wire native pipeline run commands and invocation-wide overrides

This commit is contained in:
2026-11-22 03:51:08 +00:00
parent 2a9c5a6075
commit 0cbc2b39cb
19 changed files with 1529 additions and 1344 deletions

View File

@@ -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,

View File

@@ -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 {

View File

@@ -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 {

View File

@@ -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
}

View File

@@ -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)
}
}
}

34
internal/cli/options.go Normal file
View File

@@ -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
}

View File

@@ -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))

View File

@@ -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())
},
})
}

View File

@@ -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())
}
}

View File

@@ -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
}
}

View File

@@ -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
}

File diff suppressed because it is too large Load Diff

78
internal/cli/run_input.go Normal file
View File

@@ -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
}

View File

@@ -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
}

View File

@@ -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 == "" {

View File

@@ -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)

View File

@@ -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)
}

View File

@@ -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 == "" {

View File

@@ -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)
}