Use native configuration validation and diagnostics

This commit is contained in:
2026-11-20 15:22:57 +00:00
parent 7e45ffca43
commit 39b4bddbc9
47 changed files with 351 additions and 308 deletions

View File

@@ -38,7 +38,7 @@ func artifactCatalogCommand(deps commandDependencies) commandkit.Command {
if report != nil {
err = errors.Join(err, invocation.Reporter().Report(commandkit.Report{Value: report}))
}
return operationOutcome(err)
return reportOperationOutcome(invocation, err)
},
})
}

View File

@@ -64,7 +64,7 @@ func cleanCommand(deps commandDependencies) commandkit.Command {
if report != nil {
err = errors.Join(err, invocation.Reporter().Report(commandkit.Report{Value: report}))
}
return operationOutcome(err)
return reportOperationOutcome(invocation, err)
},
})
}

View File

@@ -4,6 +4,7 @@ import (
"gitea.maximumdirect.net/eric/narratio/internal/config"
"go.maximumdirect.net/ckconfig"
"go.maximumdirect.net/commandkit"
"go.maximumdirect.net/configkit"
)
// inputError identifies an invalid Narratio request independently of native
@@ -29,7 +30,7 @@ func operationOutcome(err error) commandkit.Outcome {
func classifyOperationError(err error) commandkit.OutcomeKind {
switch err.(type) {
case *inputError, *config.SessionIdentityError, *config.ArtifactCatalogInputError:
case *inputError, *config.ArtifactCatalogInputError:
return commandkit.OutcomeUsage
}
switch wrapped := err.(type) {
@@ -54,3 +55,37 @@ func classifyOperationError(err error) commandkit.OutcomeKind {
}
return ckconfig.ClassifyConfigurationError(err)
}
// reportOperationOutcome leaves native configuration evidence to ckconfig while
// retaining the operation's context and any joined cleanup or persistence causes.
func reportOperationOutcome(inv commandkit.Invocation, err error) commandkit.Outcome {
outcome := operationOutcome(err)
var details []commandkit.Detail
var visit func(error)
visit = func(e error) {
if e == nil {
return
}
if load, ok := e.(*configkit.LoadError); ok {
warnings, explanation := ckconfig.AdaptDiagnostics("Configuration:\n", load.Sources(), load.Diagnostics())
for _, warning := range warnings {
_ = inv.Reporter().Warn("", warning.Write)
}
details = append(details, explanation.Details...)
return
}
switch wrapped := e.(type) {
case interface{ Unwrap() []error }:
for _, child := range wrapped.Unwrap() {
visit(child)
}
case interface{ Unwrap() error }:
visit(wrapped.Unwrap())
}
}
visit(err)
if len(details) > 0 {
outcome = outcome.WithExplanation(err.Error(), details...)
}
return outcome
}

View File

@@ -129,7 +129,6 @@ func TestOperationOutcomePreservesCausesAndOperationalPrecedence(t *testing.T) {
{catalogInput, 2},
{errors.Join(catalogInput, operational), 1},
{errors.Join(catalogInput, context.Canceled), 1},
{&config.SessionIdentityError{Source: "session.yml", Field: "session_id", Expected: "one", Actual: "two"}, 2},
{fmt.Errorf("request: %w", errors.Join(input, invalid)), 2},
{errors.Join(input, operational), 1},
{fmt.Errorf("request: %w", errors.Join(invalid, context.Canceled)), 1},
@@ -175,3 +174,62 @@ func TestNativeTreeRejectsRemovedRoutesAndMissingOperandsBeforeAcquisition(t *te
}
}
}
func TestDomainCommandsUseNativeConfigurationDiagnostics(t *testing.T) {
root := t.TempDir()
invalid := filepath.Join(root, "invalid.yml")
if err := os.WriteFile(invalid, []byte("unknown_field: true\n"), 0600); err != nil {
t.Fatal(err)
}
for _, tc := range []struct {
path, code string
status int
}{
{invalid, "decode.unknown_field", 2},
{filepath.Join(root, "missing.yml"), "source.missing", 1},
} {
for _, command := range [][]string{{"config", "show"}, {"artifacts", "list"}} {
var out, diag bytes.Buffer
args := append(append([]string{}, command...), "--config", tc.path, "--quiet")
result := Execute(context.Background(), args, commandkit.Streams{Out: &out, Err: &diag})
var load *configkit.LoadError
if result.Status != tc.status || !errors.As(result.Cause, &load) || out.Len() != 0 {
t.Fatalf("%v: %+v", args, result)
}
if !strings.Contains(diag.String(), tc.code) || !strings.Contains(diag.String(), tc.path) || strings.Contains(diag.String(), "strict decode failed or validation failed") {
t.Fatalf("%v: %s", args, diag.String())
}
}
}
}
func TestNativeDiagnosticsRetainWarningsAndJoinedOperationalCause(t *testing.T) {
_, loadErr := configkit.Load(configkit.NoSource(), configkit.Options[struct{}]{Definition: configkit.Definition[struct{}]{Validate: func(_ *struct{}, v *configkit.Validation) {
v.Warning(configkit.Keys(), "review warning")
v.Error(configkit.Keys(), "review invalid input")
}}})
if loadErr == nil {
t.Fatal("expected validation failure")
}
cleanup := errors.New("resource release failed")
for _, verbose := range []bool{false, true} {
application, err := commandkit.New(commandkit.Identity{Name: "test"}, commandkit.NewCommand(commandkit.CommandSpec[struct{}]{Name: "fail", Run: func(inv commandkit.Invocation, _ struct{}) commandkit.Outcome {
return reportOperationOutcome(inv, errors.Join(loadErr, cleanup))
}}))
if err != nil {
t.Fatal(err)
}
args := []string{"fail"}
if verbose {
args = append(args, "--verbose")
}
var diag bytes.Buffer
outcome := application.Execute(context.Background(), args, commandkit.Streams{Out: io.Discard, Err: &diag})
if outcome.Status != 1 || !errors.Is(outcome.Cause, cleanup) || !errors.Is(outcome.Cause, loadErr) {
t.Fatalf("%+v", outcome)
}
if !strings.Contains(diag.String(), "review invalid input") || !strings.Contains(diag.String(), cleanup.Error()) || strings.Contains(diag.String(), "review warning") != verbose {
t.Fatalf("verbose=%t: %s", verbose, diag.String())
}
}
}

View File

@@ -115,14 +115,11 @@ func (a *configurationAcquisition) Campaign(ctx context.Context) (*config.Campai
if err != nil {
return nil, err
}
cfg, err := config.LoadCampaign(path)
cfg, err := config.LoadCampaignWithIdentity(path, a.selection.Campaign.Value)
err = errors.Join(err, ctx.Err())
if err != nil {
return nil, err
}
if a.selection.Campaign.Set && config.CampaignID(cfg) != a.selection.Campaign.Value {
return nil, &config.SessionIdentityError{Source: path, Field: "campaign_id", Expected: a.selection.Campaign.Value, Actual: config.CampaignID(cfg)}
}
a.campaign, a.campaignPath = cfg, path
if a.deps.reporter != nil {
a.deps.reporter.Info("selected campaign configuration", "campaign", config.CampaignID(cfg), "source", path)

View File

@@ -72,8 +72,8 @@ func TestConfigurationAcquisitionIdentityAndExplicitEmpty(t *testing.T) {
CampaignFile: suppliedSelection(campaignPath), Campaign: suppliedSelection("other-campaign"),
}, commandDependencies{})
_, err := acquired.Campaign(context.Background())
var identity *config.SessionIdentityError
if !errors.As(err, &identity) || identity.Field != "campaign_id" {
var identity *configkit.LoadError
if !errors.As(err, &identity) {
t.Fatalf("identity error=%v", err)
}
acquired = newConfigurationAcquisition(configurationSelection{CampaignFile: suppliedSelection("")}, commandDependencies{})

View File

@@ -77,10 +77,6 @@ func configurationCommand(deps commandDependencies) commandkit.Command {
scopes[index].Acquire = func(ctx context.Context, request ckconfig.Request, input configurationSelection, state *configurationInspectionState) (commandkit.Acquired[ckconfig.View], error) {
result, err := acquire(ctx, request, input, state)
result.Diagnostics = append(result.Diagnostics, state.bootstrapDiagnostics()...)
var identity *config.SessionIdentityError
if errors.As(err, &identity) {
result.Explanation.Message = identity.Error()
}
return result, err
}
compare := scopes[index].Compare
@@ -89,10 +85,6 @@ func configurationCommand(deps commandDependencies) commandkit.Command {
separate.independent = true
result, err := compare(ctx, request, &separate)
result.Diagnostics = append(result.Diagnostics, state.bootstrapDiagnostics()...)
var identity *config.SessionIdentityError
if errors.As(err, &identity) {
result.Explanation.Message = identity.Error()
}
return result, err
}
}

View File

@@ -175,17 +175,18 @@ func TestConfigurationInspectionExplainsIdentityMismatches(t *testing.T) {
{"config", "show", "--scope", "campaign", "--campaign-file", campaign, "--campaign", "other-campaign"},
{"config", "show", "--scope", "session", "--campaign-file", campaign, "--session-file", session, "--session", "other-session"},
{"config", "diff", "--scope", "campaign", "--campaign-file", campaign, "--against-campaign-file", campaign, "--against-campaign", "other-campaign"},
{"config", "diff", "--scope", "session", "--campaign-file", campaign, "--session-file", session, "--against-campaign-file", campaign, "--against-session-file", session, "--against-session", "other-session"},
} {
var output, diagnostics bytes.Buffer
result := Execute(context.Background(), args, commandkit.Streams{Out: &output, Err: &diagnostics})
var identity *config.SessionIdentityError
if result.Status == 0 || !errors.As(result.Cause, &identity) {
var identity *configkit.LoadError
if result.Status != 2 || !errors.As(result.Cause, &identity) {
t.Fatalf("%v: identity cause missing: %#v", args, result)
}
if output.Len() != 0 || !strings.Contains(diagnostics.String(), identity.Field) || !strings.Contains(diagnostics.String(), identity.Expected) || !strings.Contains(diagnostics.String(), identity.Actual) {
if output.Len() != 0 || !strings.Contains(diagnostics.String(), "mismatch") || !strings.Contains(diagnostics.String(), "other-") || !strings.Contains(diagnostics.String(), "validate.error") {
t.Fatalf("%v: assertion not explained: %s", args, diagnostics.String())
}
if identity.Field == "campaign_id" && strings.Contains(diagnostics.String(), "session file") {
if strings.Contains(diagnostics.String(), "campaign_id") && strings.Contains(diagnostics.String(), "session file") {
t.Fatalf("campaign assertion mislabeled: %s", diagnostics.String())
}
}

View File

@@ -0,0 +1,21 @@
package app
import (
"bytes"
"errors"
"go.maximumdirect.net/configkit"
)
// configurationErrorText renders native evidence for assertions about validation messages.
func configurationErrorText(err error) string {
if err == nil {
return ""
}
var native *configkit.LoadError
if !errors.As(err, &native) {
return err.Error()
}
var b bytes.Buffer
_ = configkit.WriteDiagnostics(&b, native.Diagnostics())
return err.Error() + "\n" + b.String()
}

View File

@@ -73,7 +73,7 @@ func executionCommand(name, summary string, regenerate bool, deps commandDepende
if report != nil {
err = errors.Join(err, invocation.Reporter().Report(commandkit.Report{Value: report}))
}
return operationOutcome(err)
return reportOperationOutcome(invocation, err)
}
return commandkit.NewCommand(spec)
}

View File

@@ -67,7 +67,7 @@ func lockCommand(operation string, deps commandDependencies) commandkit.Command
if report != nil {
err = errors.Join(err, invocation.Reporter().Report(commandkit.Report{Value: report}))
}
return operationOutcome(err)
return reportOperationOutcome(invocation, err)
},
})
}

View File

@@ -419,7 +419,7 @@ inputs:
if code == 0 {
t.Fatal("exit code = 0, want non-zero")
}
if !strings.Contains(stderr.String(), "strict decode failed") {
if !strings.Contains(stderr.String(), "decode.unknown_field") {
t.Fatalf("stderr = %q, want strict decode error", stderr.String())
}
}

View File

@@ -41,7 +41,7 @@ func TestRunOverlaySelectionFlowsThroughSharedLoader(t *testing.T) {
for _, overlay := range []string{"unknown"} {
err := invokeNativeCommandWith(context.Background(), []string{"run", "2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session-file", sessionPath, "--overlay", overlay}, io.Discard, executor, commandDependencies{})
if err == nil || !strings.Contains(err.Error(), "overlay") {
if err == nil || !strings.Contains(configurationErrorText(err), "overlay") {
t.Fatalf("overlay %q error = %v, want selection rejection", overlay, err)
}
}

View File

@@ -63,7 +63,7 @@ func sessionPlanCommand(deps commandDependencies) commandkit.Command {
if report != nil {
err = errors.Join(err, invocation.Reporter().Report(commandkit.Report{Value: report}))
}
return operationOutcome(err)
return reportOperationOutcome(invocation, err)
}
return commandkit.NewCommand(spec)
}

View File

@@ -395,7 +395,7 @@ func TestExecuteRemoteSessionMalformedYAMLFailsStrictDecode(t *testing.T) {
if code == 0 {
t.Fatal("exit code = 0, want non-zero")
}
if !strings.Contains(stderr.String(), "strict decode failed") {
if !strings.Contains(stderr.String(), "decode.unknown_field") {
t.Fatalf("stderr = %q, want strict decode context", stderr.String())
}
}

View File

@@ -174,7 +174,7 @@ func restoreCommand(deps commandDependencies) commandkit.Command {
if report != nil {
err = errors.Join(err, invocation.Reporter().Report(commandkit.Report{Value: report}))
}
return operationOutcome(err)
return reportOperationOutcome(invocation, err)
},
})
}

View File

@@ -40,11 +40,11 @@ inputs:
if err == nil {
t.Fatal("expected error, got nil")
}
if !strings.Contains(err.Error(), "session.yml must be concrete") {
t.Fatalf("error = %q, want concrete session guidance", err.Error())
if !strings.Contains(configurationErrorText(err), "session.yml must be concrete") {
t.Fatalf("error = %q, want concrete session guidance", configurationErrorText(err))
}
if !strings.Contains(err.Error(), "run narratio session init") {
t.Fatalf("error = %q, want session init guidance", err.Error())
if !strings.Contains(configurationErrorText(err), "run narratio session init") {
t.Fatalf("error = %q, want session init guidance", configurationErrorText(err))
}
}
@@ -57,8 +57,8 @@ func TestPlanFailsWhenSessionIDMismatchesConcreteSession(t *testing.T) {
if err == nil {
t.Fatal("expected error, got nil")
}
if !strings.Contains(err.Error(), "session_id mismatch") {
t.Fatalf("error = %q, want mismatch context", err.Error())
if !strings.Contains(configurationErrorText(err), "session_id mismatch") {
t.Fatalf("error = %q, want mismatch context", configurationErrorText(err))
}
}
@@ -88,8 +88,8 @@ inputs:
if err == nil {
t.Fatal("expected error, got nil")
}
if !strings.Contains(err.Error(), "previous_session_id mismatch") {
t.Fatalf("error = %q, want mismatch context", err.Error())
if !strings.Contains(configurationErrorText(err), "previous_session_id mismatch") {
t.Fatalf("error = %q, want mismatch context", configurationErrorText(err))
}
}
@@ -106,8 +106,8 @@ func TestPlanRequiresExpectedPreviousSessionID(t *testing.T) {
if err == nil {
t.Fatal("expected error, got nil")
}
if !strings.Contains(err.Error(), "previous_session_id is required") {
t.Fatalf("error = %q, want missing previous-session expectation", err.Error())
if !strings.Contains(configurationErrorText(err), "previous_session_id is required") {
t.Fatalf("error = %q, want missing previous-session expectation", configurationErrorText(err))
}
}
@@ -130,10 +130,10 @@ func TestResolveSessionConfigPathErrorIncludesSearchedPaths(t *testing.T) {
if err == nil {
t.Fatal("expected error, got nil")
}
if !strings.Contains(err.Error(), "searched") {
t.Fatalf("error = %q, want searched paths", err.Error())
if !strings.Contains(configurationErrorText(err), "searched") {
t.Fatalf("error = %q, want searched paths", configurationErrorText(err))
}
if !strings.Contains(err.Error(), "pass --session") {
t.Fatalf("error = %q, want explicit-session guidance", err.Error())
if !strings.Contains(configurationErrorText(err), "pass --session") {
t.Fatalf("error = %q, want explicit-session guidance", configurationErrorText(err))
}
}

View File

@@ -88,7 +88,7 @@ func sessionInitializationCommand(deps commandDependencies) commandkit.Command {
if report != nil {
err = errors.Join(err, invocation.Reporter().Report(commandkit.Report{Value: report}))
}
return operationOutcome(err)
return reportOperationOutcome(invocation, err)
},
})
}

View File

@@ -50,7 +50,7 @@ func sessionValidationCommand(deps commandDependencies) commandkit.Command {
if report != nil {
err = errors.Join(err, invocation.Reporter().Report(commandkit.Report{Value: report}))
}
return operationOutcome(err)
return reportOperationOutcome(invocation, err)
}
return commandkit.NewCommand(spec)
}

View File

@@ -80,7 +80,7 @@ func sessionStateCommand(name string, status bool, deps commandDependencies) com
if report != nil {
err = errors.Join(err, invocation.Reporter().Report(commandkit.Report{Value: report}))
}
return operationOutcome(err)
return reportOperationOutcome(invocation, err)
}
return commandkit.NewCommand(spec)
}