Move native debug accounting into the CLI capture lifecycle
This commit is contained in:
@@ -23,12 +23,13 @@ func pipelineRunCommand(version string, newState func() *nativeLoaders, modules
|
||||
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 {
|
||||
ctx := invocation.Context
|
||||
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)
|
||||
result, err := state.application(ctx, selection)
|
||||
if err != nil {
|
||||
return pipelineConfigurationFailure(err)
|
||||
}
|
||||
@@ -55,12 +56,16 @@ func pipelineRunCommand(version string, newState func() *nativeLoaders, modules
|
||||
}
|
||||
outputDirectory := filepath.Join(cfg.Output.Directory, runID)
|
||||
var capture *frameworkdebug.Recorder
|
||||
var group *promptkit.ExecutionGroup
|
||||
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 group != nil {
|
||||
_ = capture.WriteJSON("executions.json", group.Snapshot())
|
||||
}
|
||||
if err := capture.Finalize(captureOutcome(), outcome.Cause); err != nil {
|
||||
if outcome.Status == 0 {
|
||||
outcome = commandkit.Failure(err.Error())
|
||||
@@ -82,6 +87,10 @@ func pipelineRunCommand(version string, newState func() *nativeLoaders, modules
|
||||
if err != nil {
|
||||
return finish(commandkit.Failure(err.Error()).WithCause(err))
|
||||
}
|
||||
if capture.Enabled() {
|
||||
group = &promptkit.ExecutionGroup{}
|
||||
ctx = promptkit.WithExecutionObserver(ctx, group.Observe)
|
||||
}
|
||||
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))
|
||||
}
|
||||
@@ -110,12 +119,12 @@ func pipelineRunCommand(version string, newState func() *nativeLoaders, modules
|
||||
}
|
||||
var engine *promptkit.Engine
|
||||
if preflight.RequiresEngine(effective.ResolvedPipeline) {
|
||||
engine, err = state.engine(invocation.Context, selection)
|
||||
engine, err = state.engine(ctx, selection)
|
||||
if err != nil {
|
||||
return finish(pipelineConfigurationFailure(err))
|
||||
}
|
||||
}
|
||||
if err := preflight.Validate(invocation.Context, effective.ResolvedPipeline, catalog, engine); err != nil {
|
||||
if err := preflight.Validate(ctx, effective.ResolvedPipeline, catalog, engine); err != nil {
|
||||
return finish(pipelineResourceFailure(err))
|
||||
}
|
||||
workingDirectory, err := os.Getwd()
|
||||
@@ -146,9 +155,8 @@ func pipelineRunCommand(version string, newState func() *nativeLoaders, modules
|
||||
if capture.Enabled() {
|
||||
recorder = newRunEvidenceRecorder(capture, result)
|
||||
}
|
||||
group := &promptkit.ExecutionGroup{}
|
||||
_ = invocation.Reporter().Progress(fmt.Sprintf("Running pipeline %q.", effective.ResolvedPipeline.ID))
|
||||
output, err = pipeline.New().Run(invocation.Context, pipeline.RunInput{Prepared: prepared, Path: input.InputFile.Value, RawInput: raw, SessionID: sessionID, RunID: runID, StartedAt: startedAt, Diagnostics: referenceDiagnostics, Cache: cache, Debug: recorder, Workers: cfg.Concurrency.Workers, Executions: group, Execution: contracts.ExecutionOptions{Execution: input.Overrides.Execution.Execution, TransientRetries: input.Overrides.Execution.TransientRetries}})
|
||||
output, err = pipeline.New().Run(ctx, pipeline.RunInput{Prepared: prepared, Path: input.InputFile.Value, RawInput: raw, SessionID: sessionID, RunID: runID, StartedAt: startedAt, Diagnostics: referenceDiagnostics, Cache: cache, Debug: recorder, Workers: cfg.Concurrency.Workers, Execution: contracts.ExecutionOptions{Execution: input.Overrides.Execution.Execution, TransientRetries: input.Overrides.Execution.TransientRetries}})
|
||||
reportRunDiagnostics(invocation.Reporter(), output.Diagnostics)
|
||||
if err != nil {
|
||||
return finish(commandkit.Failure(err.Error()).WithCause(err))
|
||||
|
||||
@@ -183,7 +183,7 @@ func TestNativeRunPrimaryFailureSurvivesCaptureAndDiagnosticFailures(t *testing.
|
||||
app, file, input, _ := runFixture(t, probe)
|
||||
debug := filepath.Join(filepath.Dir(file), "debug-must-not-exist", "run-1")
|
||||
probe.onGenerate = func() {
|
||||
if err := os.Mkdir(filepath.Join(debug, "outcome.json"), 0700); err != nil {
|
||||
if err := os.Mkdir(filepath.Join(debug, "executions.json"), 0700); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
@@ -241,31 +241,35 @@ func TestNativeRunDiagnosticDeliveryFailureIsNotAnOperationFailure(t *testing.T)
|
||||
}
|
||||
|
||||
func TestNativeRunCaptureFailureAfterPublicationDoesNotReportSuccess(t *testing.T) {
|
||||
probe := &runProbe{}
|
||||
app, file, input, _ := runFixture(t, probe)
|
||||
var once sync.Once
|
||||
debug := filepath.Join(filepath.Dir(file), "debug-must-not-exist", "run-1")
|
||||
probe.onGenerate = func() {
|
||||
once.Do(func() {
|
||||
// The external provider boundary runs after capture allocation but
|
||||
// before terminalization. Make only the terminal file unusable.
|
||||
if err := os.Mkdir(filepath.Join(debug, "outcome.json"), 0700); err != nil {
|
||||
t.Fatal(err)
|
||||
for _, blocked := range []string{"outcome.json", "executions.json"} {
|
||||
t.Run(blocked, func(t *testing.T) {
|
||||
probe := &runProbe{}
|
||||
app, file, input, _ := runFixture(t, probe)
|
||||
var once sync.Once
|
||||
debug := filepath.Join(filepath.Dir(file), "debug-must-not-exist", "run-1")
|
||||
probe.onGenerate = func() {
|
||||
once.Do(func() {
|
||||
// The external provider boundary runs after capture allocation but
|
||||
// before terminalization. Make only this final capture file unusable.
|
||||
if err := os.Mkdir(filepath.Join(debug, blocked), 0700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
})
|
||||
}
|
||||
var output, diagnostics bytes.Buffer
|
||||
outcome := app.Execute(context.Background(), []string{"run", "demo", "--config", file, "--input-file", input, "--cache", "bypass", "--debug", "--format", "json"}, commandkit.Streams{Out: &output, Err: &diagnostics})
|
||||
if outcome.Status != 1 || outcome.Cause == nil || output.Len() != 0 || probe.produced != 1 {
|
||||
t.Fatalf("post-publication capture failure: %#v stdout=%s stderr=%s", outcome, output.String(), diagnostics.String())
|
||||
}
|
||||
assertCompletedRunFiles(t, file, false)
|
||||
if !strings.Contains(diagnostics.String(), debug) || !strings.Contains(diagnostics.String(), filepath.Join(filepath.Dir(file), "output-must-not-exist", "run-1")) {
|
||||
t.Fatalf("post-publication locations: %s", diagnostics.String())
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(debug, "context.json")); err != nil {
|
||||
t.Fatalf("existing capture was removed: %v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
var output, diagnostics bytes.Buffer
|
||||
outcome := app.Execute(context.Background(), []string{"run", "demo", "--config", file, "--input-file", input, "--cache", "bypass", "--debug", "--format", "json"}, commandkit.Streams{Out: &output, Err: &diagnostics})
|
||||
if outcome.Status != 1 || outcome.Cause == nil || output.Len() != 0 || probe.produced != 1 {
|
||||
t.Fatalf("post-publication capture failure: %#v stdout=%s stderr=%s", outcome, output.String(), diagnostics.String())
|
||||
}
|
||||
assertCompletedRunFiles(t, file, false)
|
||||
if !strings.Contains(diagnostics.String(), debug) || !strings.Contains(diagnostics.String(), filepath.Join(filepath.Dir(file), "output-must-not-exist", "run-1")) {
|
||||
t.Fatalf("post-publication locations: %s", diagnostics.String())
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(debug, "executions.json")); err != nil {
|
||||
t.Fatalf("existing capture was removed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNativeRunFailureCategoriesPreserveCausesWithoutSuccessReports(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user