diff --git a/cmd/notarius/main.go b/cmd/notarius/main.go index 3755b7c8..ba5c76d5 100644 --- a/cmd/notarius/main.go +++ b/cmd/notarius/main.go @@ -1,11 +1,26 @@ package main import ( + "context" + "fmt" "os" + "gitea.maximumdirect.net/eric/notarius/internal/buildinfo" "gitea.maximumdirect.net/eric/notarius/internal/cli" + "go.maximumdirect.net/commandkit" ) func main() { - os.Exit(cli.Run(os.Args[1:], os.Stdout, os.Stderr)) + version, err := buildinfo.Version() + if err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } + app, err := cli.NewApplication(version, cli.Options{}) + if err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } + outcome := app.Execute(context.Background(), os.Args[1:], commandkit.Streams{Out: os.Stdout, Err: os.Stderr}) + os.Exit(outcome.Status) } diff --git a/docs/roadmap/implementation.md b/docs/roadmap/implementation.md index fb3a4c10..88c37abb 100644 --- a/docs/roadmap/implementation.md +++ b/docs/roadmap/implementation.md @@ -294,6 +294,17 @@ source-relative schemas, and missing-file behavior after root replacement. ## Stage 6: Install CommandKit And The Standard Configuration Suite +**Status:** Complete. Added the lazy native application constructor, generated +help/version and Bash/Zsh/Fish completion, and shared `app`/focused `promptkit` +ckconfig scopes with native overrides and comparison. The executable supplies +the release-linked build identity directly. Removed the manual configuration +handlers, dispatcher, and assembled help. Added focused built-in, scope/report, +explicit-empty selection, and diff-status tests. +Build-info/asset tests and `git diff --check` pass. CLI tests and executable +subprocess checks remain blocked by the previously recorded legacy YAML import +and not-yet-migrated pipeline consumers; final assembled verification must run +them. PromptKit-suite convenience aliases are installed with that suite in Stage 7. + **Goal:** one native application constructor replaces manual command plumbing. **Inspect/change:** `cmd/notarius`, `internal/cli/run.go`, `configkit_commands.go`, diff --git a/go.mod b/go.mod index 673087af..955b5a53 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,8 @@ require ( gitea.maximumdirect.net/eric/ConfigKit v0.5.0 gitea.maximumdirect.net/eric/promptkit v0.12.0 github.com/santhosh-tekuri/jsonschema/v6 v6.0.3 + go.maximumdirect.net/ckconfig v0.10.0 + go.maximumdirect.net/commandkit v0.7.0 go.maximumdirect.net/configkit v1.4.0 go.maximumdirect.net/promptkit v0.24.0 go.yaml.in/yaml/v4 v4.0.0 @@ -18,7 +20,5 @@ require ( github.com/inconshreveable/mousetrap v1.1.0 // indirect github.com/spf13/cobra v1.10.2 // indirect github.com/spf13/pflag v1.1.0 // indirect - go.maximumdirect.net/ckconfig v0.10.0 // indirect go.maximumdirect.net/ckprompt v0.11.0 // indirect - go.maximumdirect.net/commandkit v0.7.0 // indirect ) diff --git a/internal/cli/application.go b/internal/cli/application.go new file mode 100644 index 00000000..672e4955 --- /dev/null +++ b/internal/cli/application.go @@ -0,0 +1,38 @@ +package cli + +import ( + "context" + "fmt" + "strings" + + "gitea.maximumdirect.net/eric/notarius/internal/core/config" + "go.maximumdirect.net/ckconfig" + "go.maximumdirect.net/commandkit" + "go.maximumdirect.net/configkit" +) + +// 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) { + newState := func() *nativeLoaders { return &nativeLoaders{lookupEnv: options.LookupEnv} } + load := func(ctx context.Context, selection ckconfig.Selection, _ struct{}, state *nativeLoaders) (*configkit.Result[config.Document], error) { + return state.application(ctx, selection) + } + appScope := ckconfig.FromConfigKit("app", "Notarius application configuration.", load) + appScope.Features.Overrides = true + promptScope := ckconfig.FromConfigKitSubtree("promptkit", "Native PromptKit configuration.", configkit.Keys("promptkit"), load) + promptScope.Features.Overrides = true + return commandkit.New(commandkit.Identity{Name: "notarius", Version: version}, + commandkit.Completion("notarius"), + ckconfig.Command(ckconfig.Suite[struct{}, *nativeLoaders]{ + NewState: newState, + CheckInput: func(_ string, selection ckconfig.Selection, _ struct{}) error { + if selection.Config.Set && strings.TrimSpace(selection.Config.Value) == "" { + return fmt.Errorf("--config must not be empty") + } + return nil + }, + Scopes: []ckconfig.Scope[struct{}, *nativeLoaders]{appScope, promptScope}, + }), + ) +} diff --git a/internal/cli/application_test.go b/internal/cli/application_test.go new file mode 100644 index 00000000..4c40a408 --- /dev/null +++ b/internal/cli/application_test.go @@ -0,0 +1,76 @@ +package cli + +import ( + "bytes" + "context" + "encoding/json" + "path/filepath" + "strings" + "testing" + + "go.maximumdirect.net/commandkit" +) + +func TestNativeApplicationBuiltinsNeedNoConfiguration(t *testing.T) { + t.Parallel() + app, err := NewApplication("v1.2.3", Options{LookupEnv: func(string) (string, bool) { t.Fatal("builtin attempted configuration discovery"); return "", false }}) + if err != nil { + t.Fatal(err) + } + for _, args := range [][]string{{"--help"}, {"help"}, {"version"}, {"completion", "bash"}, {"completion", "zsh"}, {"completion", "fish"}, {"config", "--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 { + t.Fatalf("%v: %#v, %s", args, outcome, diagnostics.String()) + } + if args[0] == "version" && !strings.Contains(out.String(), "v1.2.3") { + t.Fatalf("release identity missing: %s", out.String()) + } + } +} + +func TestNativeConfigurationScopesAndDiffUseSelectedLoadEvidence(t *testing.T) { + t.Parallel() + dir := t.TempDir() + left, right := filepath.Join(dir, "left.yml"), filepath.Join(dir, "right.yml") + writeConfigKitFile(t, left, "version: 6\nconcurrency: {workers: 1}\n") + writeConfigKitFile(t, right, "version: 6\nconcurrency: {workers: 3}\n") + app, err := NewApplication("development", Options{}) + if err != nil { + t.Fatal(err) + } + for _, test := range []struct { + name string + args []string + status int + fragment string + }{ + {"application explanation", []string{"config", "explain", "--path", "/concurrency/workers", "--config", left}, 0, "workers"}, + {"focused settings", []string{"config", "show", "--scope", "promptkit", "--config", left, "--format", "json"}, 0, "notarius"}, + {"sources", []string{"config", "sources", "--config", left}, 0, "left.yml"}, + {"validation", []string{"config", "validate", "--scope", "all", "--config", left}, 0, ""}, + {"differences", []string{"config", "diff", "--config", left, "--against-config", right, "--check"}, 3, "workers"}, + {"identical", []string{"config", "diff", "--config", left, "--against-config", left, "--check"}, 0, ""}, + {"empty", []string{"config", "show", "--config", ""}, 2, ""}, + } { + t.Run(test.name, func(t *testing.T) { + var out, diagnostics bytes.Buffer + outcome := app.Execute(context.Background(), test.args, commandkit.Streams{Out: &out, Err: &diagnostics}) + if outcome.Status != test.status { + t.Fatalf("outcome = %#v, output %s, diagnostics %s", outcome, out.String(), diagnostics.String()) + } + if test.fragment != "" && !strings.Contains(out.String(), test.fragment) { + t.Fatalf("missing %q: %s", test.fragment, out.String()) + } + if test.name == "focused settings" { + var report map[string]any + if err := json.Unmarshal(out.Bytes(), &report); err != nil { + t.Fatal(err) + } + if _, found := report["pipelines"]; found { + t.Fatal("focused PromptKit report exposed application-root value") + } + } + }) + } +} diff --git a/internal/cli/configkit_commands.go b/internal/cli/configkit_commands.go deleted file mode 100644 index f81a4156..00000000 --- a/internal/cli/configkit_commands.go +++ /dev/null @@ -1,259 +0,0 @@ -package cli - -import ( - "context" - "encoding/json" - "errors" - "flag" - "fmt" - "io" - "strings" - - "gitea.maximumdirect.net/eric/notarius/internal/core/config" - frameworkllm "gitea.maximumdirect.net/eric/notarius/internal/framework/llm" - configkit "go.maximumdirect.net/configkit" -) - -func runConfigEffective(args []string, stdout, stderr io.Writer, opts Options) int { - fs := flag.NewFlagSet("config effective", flag.ContinueOnError) - fs.SetOutput(io.Discard) - configPath := fs.String("config", "", "config file path") - overlay := fs.String("config-overlay", "", "configuration overlay") - scope := fs.String("scope", "notarius", "configuration scope: notarius or promptkit") - if err := fs.Parse(args); err != nil { - fmt.Fprintf(stderr, "notarius: %v\n", err) - return 2 - } - if fs.NArg() != 0 { - fmt.Fprintf(stderr, "notarius: unexpected argument %q\n", fs.Arg(0)) - return 2 - } - if *scope != "notarius" && *scope != "promptkit" { - fmt.Fprintf(stderr, "notarius: invalid configuration scope %q (want notarius or promptkit)\n", *scope) - return 2 - } - loaded, _, err := loadConfigKit(*configPath, *overlay, nil, opts) - if err != nil { - writeConfigLoadError(stderr, err) - return 1 - } - if *scope == "notarius" { - if err := loaded.WriteEffective(stdout); err != nil { - fmt.Fprintf(stderr, "notarius: write effective configuration: %v\n", err) - return 1 - } - return 0 - } - inspector, err := newPromptKitInspection(loaded.Config, opts) - if err != nil { - fmt.Fprintf(stderr, "notarius: inspect PromptKit configuration: %v\n", err) - return 1 - } - if err := inspector.WriteEffectiveConfiguration(stdout); err != nil { - fmt.Fprintf(stderr, "notarius: write effective configuration: %v\n", err) - return 1 - } - return 0 -} - -func runConfigExplain(args []string, stdout, stderr io.Writer, opts Options) int { - fs := flag.NewFlagSet("config explain", flag.ContinueOnError) - fs.SetOutput(io.Discard) - configPath := fs.String("config", "", "config file path") - overlay := fs.String("config-overlay", "", "configuration overlay") - scope := fs.String("scope", "notarius", "configuration scope: notarius or promptkit") - if err := fs.Parse(args); err != nil { - fmt.Fprintf(stderr, "notarius: %v\n", err) - return 2 - } - if fs.NArg() != 1 { - fmt.Fprintln(stderr, "notarius: config explain requires exactly one JSON Pointer") - return 2 - } - if *scope != "notarius" && *scope != "promptkit" { - fmt.Fprintf(stderr, "notarius: invalid configuration scope %q (want notarius or promptkit)\n", *scope) - return 2 - } - pointer := fs.Arg(0) - parsed, err := configkit.ParsePath(pointer) - if err != nil || parsed.String() != pointer { - fmt.Fprintf(stderr, "notarius: %q is not a canonical JSON Pointer\n", pointer) - return 2 - } - if *scope == "promptkit" { - loaded, _, err := loadConfigKit(*configPath, *overlay, nil, opts) - if err != nil { - writeConfigLoadError(stderr, err) - return 1 - } - inspector, err := newPromptKitInspection(loaded.Config, opts) - if err != nil { - fmt.Fprintf(stderr, "notarius: inspect PromptKit configuration: %v\n", err) - return 1 - } - found, err := inspector.WriteConfigurationExplanation(stdout, pointer) - if err != nil { - if errors.Is(err, frameworkllm.ErrInvalidPromptKitConfigurationPointer) { - fmt.Fprintf(stderr, "notarius: %q is not a canonical JSON Pointer\n", pointer) - return 2 - } - fmt.Fprintf(stderr, "notarius: write PromptKit configuration explanation: %v\n", err) - return 1 - } - if !found { - fmt.Fprintf(stderr, "notarius: PromptKit configuration pointer %q is unknown\n", pointer) - return 1 - } - return 0 - } - loaded, _, err := loadConfigKit(*configPath, *overlay, nil, opts) - if err != nil { - writeConfigLoadError(stderr, err) - return 1 - } - if err := loaded.WriteExplanation(stdout, pointer); err != nil { - fmt.Fprintf(stderr, "notarius: write configuration explanation: %v\n", err) - return 1 - } - return 0 -} - -func runConfigProfile(args []string, stdout, stderr io.Writer, opts Options) int { - fs := flag.NewFlagSet("config profile", flag.ContinueOnError) - fs.SetOutput(io.Discard) - configPath := fs.String("config", "", "config file path") - overlay := fs.String("config-overlay", "", "configuration overlay") - if err := fs.Parse(args); err != nil { - fmt.Fprintf(stderr, "notarius: %v\n", err) - return 2 - } - if fs.NArg() != 1 || strings.TrimSpace(fs.Arg(0)) == "" { - fmt.Fprintln(stderr, "notarius: config profile requires exactly one nonblank profile ID") - return 2 - } - profileID := fs.Arg(0) - loaded, _, err := loadConfigKit(*configPath, *overlay, nil, opts) - if err != nil { - writeConfigLoadError(stderr, err) - return 1 - } - inspector, err := newPromptKitInspection(loaded.Config, opts) - if err != nil { - fmt.Fprintf(stderr, "notarius: inspect PromptKit configuration: %v\n", err) - return 1 - } - profile, err := inspector.InspectProfile(context.Background(), profileID) - if err != nil { - fmt.Fprintf(stderr, "notarius: %v\n", err) - return 1 - } - result := struct { - ProfileID string `json:"profile_id"` - APIKeyRequired bool `json:"api_key_required"` - EffectiveModelParams frameworkllm.PromptKitEffectiveModelParams `json:"effective_model_params"` - }{ - ProfileID: profile.ProfileID, - APIKeyRequired: profile.CredentialRequired, - EffectiveModelParams: profile.EffectiveModelParams, - } - if err := json.NewEncoder(stdout).Encode(result); err != nil { - fmt.Fprintf(stderr, "notarius: write PromptKit profile inspection: %v\n", err) - return 1 - } - return 0 -} - -func newPromptKitInspection(cfg config.Config, opts Options) (*frameworkllm.PromptKitProfileInspector, error) { - return frameworkllm.NewPromptKitProfileInspector(frameworkllm.PromptKitProfileInspectorConfig{ - Source: promptKitProfileSourceConfig(cfg), - Assets: opts.promptKitAssets, - }) -} - -func runConfigValidate(args []string, stdout, stderr io.Writer, opts Options) int { - fs := flag.NewFlagSet("config validate", flag.ContinueOnError) - fs.SetOutput(io.Discard) - configPath := fs.String("config", "", "config file path") - overlay := fs.String("config-overlay", "", "configuration overlay") - pipelineID := fs.String("pipeline", "", "pipeline ID") - onlyRaw := fs.String("only", "", "comma-separated artifact lanes") - if err := fs.Parse(args); err != nil { - fmt.Fprintf(stderr, "notarius: %v\n", err) - return 2 - } - if fs.NArg() != 0 { - fmt.Fprintf(stderr, "notarius: unexpected argument %q\n", fs.Arg(0)) - return 2 - } - if strings.TrimSpace(*onlyRaw) != "" && strings.TrimSpace(*pipelineID) == "" { - fmt.Fprintln(stderr, "notarius: --only requires --pipeline") - return 2 - } - only, err := parseOnly(*onlyRaw) - if err != nil { - fmt.Fprintf(stderr, "notarius: %v\n", err) - return 2 - } - loaded, path, err := loadConfigKit(*configPath, *overlay, nil, opts) - if err != nil { - writeConfigLoadError(stderr, err) - return 1 - } - if strings.TrimSpace(*pipelineID) == "" { - fmt.Fprintf(stdout, "config %q is valid\n", path) - return 0 - } - catalog, err := effectiveCatalog(opts) - if err != nil { - fmt.Fprintf(stderr, "notarius: %v\n", err) - return 1 - } - effective, err := loaded.Config.Resolve(config.ResolveInput{PipelineID: *pipelineID, Only: only, Catalog: catalog}) - if err != nil { - fmt.Fprintf(stderr, "notarius: %v\n", err) - return 1 - } - if err := validateExplicitPromptKitProfiles(context.Background(), effective.Config, effectiveLLMProfileIDs(effective.ResolvedPipeline), opts.promptKitAssets); err != nil { - fmt.Fprintf(stderr, "notarius: %v\n", err) - return 1 - } - fmt.Fprintf(stdout, "config %q is valid for pipeline %q\n", path, strings.TrimSpace(*pipelineID)) - return 0 -} - -func runPipelinesList(args []string, stdout, stderr io.Writer, opts Options) int { - fs := flag.NewFlagSet("pipelines list", flag.ContinueOnError) - fs.SetOutput(io.Discard) - configPath := fs.String("config", "", "config file path") - overlay := fs.String("config-overlay", "", "configuration overlay") - jsonOutput := fs.Bool("json", false, "write JSON output") - if err := fs.Parse(args); err != nil { - fmt.Fprintf(stderr, "notarius: %v\n", err) - return 2 - } - if fs.NArg() != 0 { - fmt.Fprintf(stderr, "notarius: unexpected argument %q\n", fs.Arg(0)) - return 2 - } - loaded, _, err := loadConfigKit(*configPath, *overlay, nil, opts) - if err != nil { - writeConfigLoadError(stderr, err) - return 1 - } - ids := sortedPipelineIDs(loaded.Config) - if *jsonOutput { - payload, err := json.Marshal(struct { - Pipelines []string `json:"pipelines"` - }{Pipelines: ids}) - if err != nil { - fmt.Fprintf(stderr, "notarius: marshal pipeline list: %v\n", err) - return 1 - } - fmt.Fprintf(stdout, "%s\n", payload) - return 0 - } - for _, id := range ids { - fmt.Fprintln(stdout, id) - } - return 0 -} diff --git a/internal/cli/run.go b/internal/cli/run.go index cfb0b360..2bff7f82 100644 --- a/internal/cli/run.go +++ b/internal/cli/run.go @@ -26,20 +26,10 @@ import ( 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" ) -const usage = `Usage: - notarius help - notarius --version - notarius run --input path/to/source.json [--json] [flags] - notarius config validate --config path/to/config.yml [--config-overlay name] [--pipeline pipeline-id] [--only lane-a,lane-b] - notarius config effective [--scope notarius|promptkit] --config path/to/config.yml [--config-overlay name] - notarius config explain [--scope notarius|promptkit] --config path/to/config.yml [--config-overlay name] - notarius config profile [--config path/to/config.yml] [--config-overlay name] - notarius pipelines list --config path/to/config.yml [--config-overlay name] [--json] -` - type Options struct { Catalog pipeline.ModuleCatalog Registries pipeline.Registries @@ -66,50 +56,17 @@ func Run(args []string, stdout, stderr io.Writer) int { } func RunWithOptions(args []string, stdout, stderr io.Writer, opts Options) int { - if len(args) > 0 && args[0] == "--version" { - if len(args) != 1 { - fmt.Fprintln(stderr, "notarius: --version does not accept arguments") - return 2 - } - version, err := buildinfo.Version() - if err != nil { - fmt.Fprintf(stderr, "notarius: %v\n", err) - return 1 - } - fmt.Fprintf(stdout, "notarius %s\n", version) - return 0 - } - - var err error - opts, err = normalizeOptions(opts) + version, err := buildinfo.Version() if err != nil { fmt.Fprintf(stderr, "notarius: %v\n", err) return 1 } - if len(args) == 0 { - writeUsage(stdout) - return 0 + app, err := NewApplication(version, opts) + if err != nil { + fmt.Fprintf(stderr, "notarius: %v\n", err) + return 1 } - - switch args[0] { - case "help", "--help", "-h": - writeUsage(stdout) - return 0 - case "config": - return runConfig(args[1:], stdout, stderr, opts) - case "pipelines": - return runPipelines(args[1:], stdout, stderr, opts) - case "run": - return runPipelineCommand(args[1:], stdout, stderr, opts) - default: - fmt.Fprintf(stderr, "notarius: unknown command %q\n", args[0]) - writeUsage(stderr) - return 2 - } -} - -func writeUsage(w io.Writer) { - fmt.Fprint(w, usage) + return app.Execute(context.Background(), args, commandkit.Streams{Out: stdout, Err: stderr}).Status } func normalizeOptions(opts Options) (Options, error) { @@ -1046,44 +1003,6 @@ func runMetadata(outputDir, debugDir string) map[string]any { return metadata } -func runConfig(args []string, stdout, stderr io.Writer, opts Options) int { - if len(args) == 0 { - fmt.Fprintln(stderr, "notarius: config requires a subcommand") - writeUsage(stderr) - return 2 - } - switch args[0] { - case "validate": - return runConfigValidate(args[1:], stdout, stderr, opts) - case "effective": - return runConfigEffective(args[1:], stdout, stderr, opts) - case "explain": - return runConfigExplain(args[1:], stdout, stderr, opts) - case "profile": - return runConfigProfile(args[1:], stdout, stderr, opts) - default: - fmt.Fprintf(stderr, "notarius: unknown config subcommand %q\n", args[0]) - writeUsage(stderr) - return 2 - } -} - -func runPipelines(args []string, stdout, stderr io.Writer, opts Options) int { - if len(args) == 0 { - fmt.Fprintln(stderr, "notarius: pipelines requires a subcommand") - writeUsage(stderr) - return 2 - } - switch args[0] { - case "list": - return runPipelinesList(args[1:], stdout, stderr, opts) - default: - fmt.Fprintf(stderr, "notarius: unknown pipelines subcommand %q\n", args[0]) - writeUsage(stderr) - return 2 - } -} - func parseOnly(raw string) ([]string, error) { if strings.TrimSpace(raw) == "" { return nil, nil