Install native run and artifact regeneration commands

This commit is contained in:
2026-11-20 05:10:04 +00:00
parent ed29bdc473
commit 0238f82847
8 changed files with 271 additions and 121 deletions

View File

@@ -23,13 +23,17 @@ func (f *artifactSelectionFlag) Set(value string) error {
}
func (f *artifactSelectionFlag) Normalize() ([]string, error) {
if len(f.values) == 0 {
return normalizeArtifactNames(f.values)
}
func normalizeArtifactNames(values []string) ([]string, error) {
if len(values) == 0 {
return nil, nil
}
seen := map[string]struct{}{}
out := make([]string, 0, len(f.values))
for _, raw := range f.values {
out := make([]string, 0, len(values))
for _, raw := range values {
for _, part := range strings.Split(raw, ",") {
name := strings.TrimSpace(part)
if name == "" {

View File

@@ -8,7 +8,12 @@ import (
// inputError identifies an invalid Narratio request independently of native
// configuration diagnostics. Operational errors retain their own classification.
type inputError struct{ message string }
type inputError struct {
message string
cause error
}
func (e *inputError) Unwrap() error { return e.cause }
func (e *inputError) Error() string { return e.message }

View File

@@ -17,7 +17,8 @@ func newApplication(deps commandDependencies, executor pipelineExecutor) (*comma
return commandkit.New(
commandkit.Identity{Name: "narratio", Version: buildinfo.Version},
configurationCommand(deps),
commandkit.Group("artifacts", "Inspect and regenerate configured artifacts."),
executionCommand("run", "Execute a session pipeline.", false, deps, executor),
commandkit.Group("artifacts", "Inspect and regenerate configured artifacts.", executionCommand("regenerate", "Rebuild extraction and analysis artifacts.", true, deps, executor)),
commandkit.Group("session", "Manage and inspect sessions."),
)
}

View File

@@ -0,0 +1,136 @@
package app
import (
"context"
"errors"
"fmt"
"gitea.maximumdirect.net/eric/narratio/internal/config"
"go.maximumdirect.net/commandkit"
)
type executionInput struct {
Selection configurationSelection
From, Through, Stage commandkit.Supplied[string]
Force commandkit.Supplied[bool]
Artifacts commandkit.Supplied[[]string]
PreviousSession commandkit.Supplied[string]
plan BoundedPlan
selectedArtifacts []string
}
type runReport struct {
Campaign string `json:"campaign"`
SessionID string `json:"session_id"`
RunID string `json:"run_id"`
Stages []string `json:"stages"`
Executed []string `json:"executed"`
Skipped []string `json:"skipped"`
ArtifactTargets []string `json:"artifact_targets"`
Complete bool `json:"complete"`
}
func checkExecutionInput(input *executionInput) error {
if err := checkConfigurationSelection(input.Selection); err != nil {
return err
}
if input.Stage.Set && (input.From.Set || input.Through.Set) {
return fmt.Errorf("--stage cannot be combined with --from or --through")
}
for _, field := range []struct {
name string
value commandkit.Supplied[string]
}{{"stage", input.Stage}, {"from", input.From}, {"through", input.Through}} {
if field.value.Set && field.value.Value == "" {
return fmt.Errorf("--%s requires a stage name", field.name)
}
}
from, through := input.From.Value, input.Through.Value
if input.Stage.Set {
from, through = input.Stage.Value, input.Stage.Value
}
plan, err := BuildBoundedPlan(from, through)
if err != nil {
return err
}
selected, err := normalizeArtifactNames(input.Artifacts.Value)
if err != nil {
return err
}
if len(selected) > 0 && !plan.Contains("analyze") && !plan.Contains("publish") {
return fmt.Errorf("--artifacts requires a selected range containing analyze or publish")
}
input.plan, input.selectedArtifacts = plan, selected
return nil
}
func executionCommand(name, summary string, regenerate bool, deps commandDependencies, executor pipelineExecutor) commandkit.Command {
common := newConfigurationFlags(func(input **executionInput) *configurationSelection { return &(*input).Selection })
flags := []commandkit.Flag[*executionInput]{common.Config, common.Overlay, common.Campaign, common.CampaignFile, common.SessionFile, common.PartyFile, common.SecretsDir,
commandkit.ValueFlag(commandkit.FlagMeta{Name: "previous-session-id", Usage: "Assert the previous session identity."}, nil, stringValue, func(input **executionInput, value commandkit.Supplied[string]) { (*input).PreviousSession = value }),
commandkit.RepeatFlag(commandkit.FlagMeta{Name: "artifacts", Usage: "Analysis artifact or family targets (repeatable or comma-separated)."}, []string(nil), stringValue, func(input **executionInput, value commandkit.Supplied[[]string]) { (*input).Artifacts = value }),
}
if !regenerate {
flags = append(flags,
commandkit.ValueFlag(commandkit.FlagMeta{Name: "stage", Usage: "Select one canonical pipeline stage."}, nil, stringValue, func(input **executionInput, value commandkit.Supplied[string]) { (*input).Stage = value }),
commandkit.ValueFlag(commandkit.FlagMeta{Name: "from", Usage: "First pipeline stage, inclusive."}, nil, stringValue, func(input **executionInput, value commandkit.Supplied[string]) { (*input).From = value }),
commandkit.ValueFlag(commandkit.FlagMeta{Name: "through", Usage: "Last pipeline stage, inclusive."}, nil, stringValue, func(input **executionInput, value commandkit.Supplied[string]) { (*input).Through = value }),
commandkit.BoolFlag(commandkit.FlagMeta{Name: "force", Usage: "Rerun selected stages."}, false, func(input **executionInput, value commandkit.Supplied[bool]) { (*input).Force = value }),
)
}
return commandkit.NewCommand(commandkit.CommandSpec[*executionInput]{
Name: name, Summary: summary, NewInput: func() *executionInput { return &executionInput{} }, Flags: flags,
Arguments: []commandkit.Argument[*executionInput]{commandkit.ValueArgument(commandkit.ArgumentMeta{Name: "SESSION", Usage: "Session identity."}, true, stringValue, func(input **executionInput, value commandkit.Supplied[string]) { (*input).Selection.Session = value })},
CheckInput: func(input *executionInput) error {
if regenerate {
input.From = commandkit.Supplied[string]{Value: "extract", Set: true}
input.Through = commandkit.Supplied[string]{Value: "analyze", Set: true}
input.Force = commandkit.Supplied[bool]{Value: true, Set: true}
}
return checkExecutionInput(input)
},
Run: func(invocation commandkit.Invocation, input *executionInput) commandkit.Outcome {
report, err := executeSelectedPipeline(invocation.Context, input, deps, executor)
if report != nil {
err = errors.Join(err, invocation.Reporter().Report(commandkit.Report{Value: report}))
}
return operationOutcome(err)
},
})
}
func executeSelectedPipeline(ctx context.Context, input *executionInput, deps commandDependencies, executor pipelineExecutor) (report *runReport, err error) {
invocationDeps := normalizeCommandDependencies(deps)
invocationDeps.secretsBootstrap = &secretsBootstrap{}
acquired := newConfigurationAcquisition(input.Selection, invocationDeps)
acquired.previousSessionID = input.PreviousSession.Value
defer func() {
err = errors.Join(err, acquired.Close())
if report != nil {
report.Complete = err == nil
}
}()
cfg, err := acquired.Domain(ctx)
if err != nil {
return nil, err
}
if err := config.Validate(cfg); err != nil {
return nil, err
}
selected, err := normalizeArtifactSelection(cfg, input.selectedArtifacts)
if err != nil {
return nil, &inputError{message: err.Error(), cause: err}
}
effective, err := resolveEffectiveArtifacts(cfg, selected)
if err != nil {
return nil, &inputError{message: err.Error(), cause: err}
}
summary, err := executor(ctx, cfg, input.plan, RunOptions{Force: input.Force.Value, SelectedArtifacts: selected, EffectiveArtifacts: effective})
if summary == nil {
if err == nil {
err = fmt.Errorf("pipeline execution returned no run summary")
}
return nil, err
}
return &runReport{Campaign: config.CampaignID(cfg.Campaign), SessionID: summary.SessionID, RunID: summary.RunID, Stages: input.plan.Names(), Executed: summary.Executed, Skipped: summary.Skipped, ArtifactTargets: effective.Keys(), Complete: err == nil}, err
}

View File

@@ -0,0 +1,103 @@
package app
import (
"bytes"
"context"
"encoding/json"
"reflect"
"testing"
"gitea.maximumdirect.net/eric/narratio/internal/config"
"go.maximumdirect.net/commandkit"
configkit "go.maximumdirect.net/configkit"
)
func TestExecutionCommandsUseTypedBoundedPlans(t *testing.T) {
pipeline, campaign, session := writeValidConfigFiles(t, t.TempDir())
full, err := BuildBoundedPlan("", "")
if err != nil {
t.Fatal(err)
}
for _, test := range []struct {
name string
args, want []string
force bool
}{
{"full", []string{"run", "2026-05-03"}, full.Names(), false},
{"single", []string{"run", "2026-05-03", "--stage", "analyze"}, []string{"analyze"}, false},
{"forced single", []string{"run", "2026-05-03", "--stage", "publish", "--force"}, []string{"publish"}, true},
{"range", []string{"run", "2026-05-03", "--from", "extract", "--through", "analyze"}, []string{"extract", "analyze"}, false},
{"regenerate", []string{"artifacts", "regenerate", "2026-05-03"}, []string{"extract", "analyze"}, true},
} {
t.Run(test.name, func(t *testing.T) {
calls := 0
executor := func(_ context.Context, cfg *config.Config, plan BoundedPlan, options RunOptions) (*RunSummary, error) {
calls++
if !reflect.DeepEqual(plan.Names(), test.want) || options.Force != test.force {
t.Fatalf("plan=%v force=%v", plan.Names(), options.Force)
}
if cfg.Session.SessionID != "2026-05-03" || !options.EffectiveArtifacts.Resolved() {
t.Fatal("missing resolved execution inputs")
}
return &RunSummary{SessionID: cfg.Session.SessionID, RunID: "synthetic-run", Executed: plan.Names()}, nil
}
app, err := newApplication(commandDependencies{}, executor)
if err != nil {
t.Fatal(err)
}
args := append(append([]string{}, test.args...), "--config", pipeline, "--campaign-file", campaign, "--session-file", session, "--format", "json")
var out, diagnostic bytes.Buffer
outcome := app.Execute(context.Background(), args, commandkit.Streams{Out: &out, Err: &diagnostic})
if outcome.Status != 0 || calls != 1 {
t.Fatalf("status=%d calls=%d diagnostics=%s", outcome.Status, calls, diagnostic.String())
}
var report runReport
if err := json.Unmarshal(out.Bytes(), &report); err != nil {
t.Fatal(err)
}
if !report.Complete || report.Campaign != "sample-campaign" || !reflect.DeepEqual(report.Executed, test.want) {
t.Fatalf("report=%#v", report)
}
})
}
}
func TestExecutionCommandsRejectInvalidSelectionBeforeBootstrap(t *testing.T) {
app, err := newApplication(commandDependencies{
loadSecretsDir: func(string) (*configkit.SecretsReport, error) {
t.Fatal("invalid input bootstrapped secrets")
return nil, nil
},
loadPipelineConfig: func(string, config.PipelineLoadOptions) (*config.PipelineConfig, error) {
t.Fatal("invalid input loaded config")
return nil, nil
},
}, func(context.Context, *config.Config, BoundedPlan, RunOptions) (*RunSummary, error) {
t.Fatal("invalid input executed pipeline")
return nil, nil
})
if err != nil {
t.Fatal(err)
}
for _, args := range [][]string{
{"run"}, {"run", "session", "--stage", "analyze", "--from", "analyze"},
{"run", "session", "--stage", "analyze", "--through", "analyze"},
{"run", "session", "--from", "publish", "--through", "prepare"},
{"run", "session", "--stage", "missing"},
{"run", "session", "--stage", "prepare", "--artifacts", "recap"},
{"artifacts", "regenerate", "session", "--force=false"},
{"artifacts", "regenerate", "session", "--through", "publish"},
{"run", "session", "--session-id", "session"},
{"run", "session", "--set", `/whisperx/language="fr"`},
{"run-stage", "analyze", "session"},
{"analyze", "session"},
{"publish", "session"},
{"regenerate-artifacts", "session"},
} {
var out, diagnostic bytes.Buffer
outcome := app.Execute(context.Background(), args, commandkit.Streams{Out: &out, Err: &diagnostic})
if outcome.Status != 2 {
t.Fatalf("%v: status=%d diagnostics=%s", args, outcome.Status, diagnostic.String())
}
}
}

View File

@@ -1,51 +0,0 @@
package app
import (
"context"
"fmt"
"io"
)
// RegenerateArtifacts expands the convenience command into its canonical run
// invocation. The run command remains the sole owner of parsing and execution.
func RegenerateArtifacts(ctx context.Context, args []string, out io.Writer) error {
return regenerateArtifactsWithDependencies(ctx, args, out, executePlan, commandDependencies{})
}
func regenerateArtifactsWithPipelineExecutor(ctx context.Context, args []string, out io.Writer, executor pipelineExecutor) error {
return regenerateArtifactsWithDependencies(ctx, args, out, executor, commandDependencies{})
}
func regenerateArtifactsWithDependencies(ctx context.Context, args []string, out io.Writer, executor pipelineExecutor, deps commandDependencies) error {
if containsHelpOption(args) {
printRegenerateArtifactsHelp(out)
return nil
}
expanded := make([]string, 0, len(args)+5)
if len(args) > 0 && !isCLIFlagToken(args[0]) {
expanded = append(expanded, args[0])
args = args[1:]
}
expanded = append(expanded, "--force", "--from", "extract", "--through", "analyze")
expanded = append(expanded, args...)
return runWithDependencies(ctx, expanded, out, executor, deps)
}
func containsHelpOption(args []string) bool {
for _, arg := range args {
if arg == "-h" || arg == "--help" {
return true
}
}
return false
}
func printRegenerateArtifactsHelp(out io.Writer) {
_, _ = fmt.Fprintln(out, "Usage: narratio regenerate-artifacts <session_id> [--artifacts <name[,name...]>] [common config flags]")
_, _ = fmt.Fprintln(out)
_, _ = fmt.Fprintln(out, "Exactly equivalent to:")
_, _ = fmt.Fprintln(out, " narratio run <session_id> --force --from extract --through analyze [caller options]")
_, _ = fmt.Fprintln(out)
_, _ = fmt.Fprintln(out, "Extraction always runs; selected analysis artifacts and their required prerequisites are rebuilt. Publish and notify never run.")
}

View File

@@ -24,12 +24,12 @@ func TestRegenerateArtifactsForwardsCanonicalPipelineRequest(t *testing.T) {
}
code := executeWithPipelineExecutor([]string{
"regenerate-artifacts", "2026-05-03",
"artifacts", "regenerate", "2026-05-03",
"--artifacts", "session_recap,player_handout",
"--artifacts=player_handout",
"--config", pipelinePath,
"--campaign-file", campaignPath,
"--session", sessionPath,
"--session-file", sessionPath,
"--overlay", "testing",
}, io.Discard, io.Discard, executor)
if code != 0 {
@@ -46,31 +46,6 @@ func TestRegenerateArtifactsForwardsCanonicalPipelineRequest(t *testing.T) {
}
}
func TestRegenerateArtifactsForwardsSessionIDCompatibilityFlag(t *testing.T) {
workspaceRoot := t.TempDir()
pipelinePath, campaignPath, sessionPath := writeValidConfigFilesWithScriptoriumArtifacts(t, workspaceRoot)
var capturedSessionID string
executor := func(_ context.Context, cfg *config.Config, _ BoundedPlan, _ RunOptions) (*RunSummary, error) {
capturedSessionID = cfg.Session.SessionID
return &RunSummary{SessionID: cfg.Session.SessionID, ManifestPath: "manifest.json"}, nil
}
code := executeWithPipelineExecutor([]string{
"regenerate-artifacts",
"--config", pipelinePath,
"--campaign-file", campaignPath,
"--session", sessionPath,
"--session-id", "2026-05-03",
"--artifacts", "session_recap",
}, io.Discard, io.Discard, executor)
if code != 0 {
t.Fatalf("executeWithPipelineExecutor() code = %d, want 0", code)
}
if capturedSessionID != "2026-05-03" {
t.Fatalf("session id = %q, want compatibility flag value", capturedSessionID)
}
}
func TestRegenerateArtifactsHelpDoesNotInvokeRun(t *testing.T) {
called := false
executor := func(context.Context, *config.Config, BoundedPlan, RunOptions) (*RunSummary, error) {
@@ -80,16 +55,16 @@ func TestRegenerateArtifactsHelpDoesNotInvokeRun(t *testing.T) {
var stdout bytes.Buffer
var stderr bytes.Buffer
if code := executeWithPipelineExecutor([]string{"regenerate-artifacts", "--help"}, &stdout, &stderr, executor); code != 0 {
if code := executeWithPipelineExecutor([]string{"artifacts", "regenerate", "--help"}, &stdout, &stderr, executor); code != 0 {
t.Fatalf("executeForTest() code = %d, stderr = %q", code, stderr.String())
}
if called {
t.Fatal("help invoked canonical run handler")
}
for _, detail := range []string{
"narratio run <session_id> --force --from extract --through analyze",
"Extraction always runs",
"Publish and notify never run",
"regenerate",
"--artifacts",
"--session-file",
} {
if !strings.Contains(stdout.String(), detail) {
t.Fatalf("help = %q, want %q", stdout.String(), detail)
@@ -99,37 +74,3 @@ func TestRegenerateArtifactsHelpDoesNotInvokeRun(t *testing.T) {
t.Fatalf("stderr = %q, want empty", stderr.String())
}
}
func TestRegenerateArtifactsOwnedOptionsFailThroughRunParser(t *testing.T) {
for _, test := range []struct {
name string
args []string
owned string
}{
{name: "force", args: []string{"--force"}, owned: "force"},
{name: "from", args: []string{"--from=render"}, owned: "from"},
{name: "through", args: []string{"--through", "publish"}, owned: "through"},
} {
t.Run(test.name, func(t *testing.T) {
args := []string{"regenerate-artifacts", "2026-05-03"}
args = append(args, test.args...)
var stderr bytes.Buffer
if code := executeForTest(args, io.Discard, &stderr); code == 0 {
t.Fatalf("executeForTest(%#v) code = 0", args)
}
if !strings.Contains(stderr.String(), "--"+test.owned+" may be specified only once") {
t.Fatalf("stderr = %q, want shared duplicate %s error", stderr.String(), test.owned)
}
})
}
}
func TestRegenerateArtifactsRejectsUnknownOptionsThroughRunParser(t *testing.T) {
var stderr bytes.Buffer
if code := executeForTest([]string{"regenerate-artifacts", "2026-05-03", "--regenerate-only"}, io.Discard, &stderr); code == 0 {
t.Fatal("executeForTest() code = 0")
}
if !strings.Contains(stderr.String(), "flag provided but not defined") || !strings.Contains(stderr.String(), "regenerate-only") {
t.Fatalf("stderr = %q, want canonical parser unknown-option error", stderr.String())
}
}