Capture evidence for core-owned operations
This commit is contained in:
@@ -31,7 +31,7 @@ type OperationResult[T any] struct {
|
||||
}
|
||||
|
||||
// ExecuteOperation invokes preparation once and executes its selected form.
|
||||
func ExecuteOperation[T any](ctx context.Context, execution ExecutionContext[T], prepare contracts.Preparation[T]) (OperationResult[T], error) {
|
||||
func ExecuteOperation[T any](ctx context.Context, execution ExecutionContext[T], prepare contracts.Preparation[T]) (result OperationResult[T], failure error) {
|
||||
var empty OperationResult[T]
|
||||
if ctx == nil {
|
||||
return empty, errors.New("operation context must not be nil")
|
||||
@@ -42,6 +42,18 @@ func ExecuteOperation[T any](ctx context.Context, execution ExecutionContext[T],
|
||||
if prepare == nil {
|
||||
return empty, errors.New("operation preparation must be supplied")
|
||||
}
|
||||
if execution.Execution.Capture != nil {
|
||||
defer func() {
|
||||
errorText := ""
|
||||
if failure != nil {
|
||||
errorText = failure.Error()
|
||||
}
|
||||
if err := execution.Execution.RecordEvidence("operation-terminal.json", map[string]any{"outcome": result.Outcome, "error": errorText}); err != nil {
|
||||
result = empty
|
||||
failure = errors.Join(failure, err)
|
||||
}
|
||||
}()
|
||||
}
|
||||
operation, err := prepare(ctx)
|
||||
if canceled := ctx.Err(); canceled != nil {
|
||||
return empty, canceled
|
||||
@@ -49,6 +61,11 @@ func ExecuteOperation[T any](ctx context.Context, execution ExecutionContext[T],
|
||||
if err != nil {
|
||||
return empty, err
|
||||
}
|
||||
if execution.Execution.Capture != nil {
|
||||
if err := execution.Execution.RecordEvidence("operation-preparation.json", map[string]any{"operation": fmt.Sprintf("%T", operation)}); err != nil {
|
||||
return empty, err
|
||||
}
|
||||
}
|
||||
switch prepared := operation.(type) {
|
||||
case contracts.DeterministicOperation[T]:
|
||||
return executeDeterministic(ctx, execution, prepared)
|
||||
@@ -58,11 +75,17 @@ func ExecuteOperation[T any](ctx context.Context, execution ExecutionContext[T],
|
||||
}
|
||||
return executeDeterministic(ctx, execution, *prepared)
|
||||
case contracts.PreparationLimit[T]:
|
||||
if err := recordDomainEvidence(execution.Execution, "preparation-domain.json", prepared.Evidence); err != nil {
|
||||
return empty, err
|
||||
}
|
||||
return finishOperation(ctx, execution.Controls.Mode, prepared.Cause, nil, contracts.CandidateAssessment{}, prepared.Fallback, prepared.Diagnostics)
|
||||
case *contracts.PreparationLimit[T]:
|
||||
if prepared == nil {
|
||||
return empty, errors.New("preparation limit must not be nil")
|
||||
}
|
||||
if err := recordDomainEvidence(execution.Execution, "preparation-domain.json", prepared.Evidence); err != nil {
|
||||
return empty, err
|
||||
}
|
||||
return finishOperation(ctx, execution.Controls.Mode, prepared.Cause, nil, contracts.CandidateAssessment{}, prepared.Fallback, prepared.Diagnostics)
|
||||
case contracts.NativeOperation[T]:
|
||||
return executeNative(ctx, execution, prepared)
|
||||
@@ -86,6 +109,11 @@ func executeDeterministic[T any](ctx context.Context, execution ExecutionContext
|
||||
}
|
||||
view := operation.Evaluation.View()
|
||||
assessment, err := execution.Assess(ctx, *view.Candidate)
|
||||
if execution.Execution.Capture != nil {
|
||||
if captureErr := captureEvaluation(execution.Execution, "deterministic", operation.Evaluation, assessment, view.Guidance, err == nil && assessment.Accepted() && len(view.Guidance) == 0, err); captureErr != nil {
|
||||
return empty, errors.Join(ctx.Err(), err, captureErr)
|
||||
}
|
||||
}
|
||||
if canceled := ctx.Err(); canceled != nil {
|
||||
return empty, canceled
|
||||
}
|
||||
@@ -99,6 +127,41 @@ func executeDeterministic[T any](ctx context.Context, execution ExecutionContext
|
||||
return completeOperation(*view.Candidate, operation.Evaluation, assessment, outcome, operation.Diagnostics)
|
||||
}
|
||||
|
||||
// recordDomainEvidence invokes optional domain projections only when capture
|
||||
// is enabled. Projection and writing failures are required-capture failures.
|
||||
func recordDomainEvidence(execution contracts.ExecutionOptions, name string, project func() (any, error)) error {
|
||||
if execution.Capture == nil || project == nil {
|
||||
return nil
|
||||
}
|
||||
value, err := project()
|
||||
if err != nil {
|
||||
return &contracts.CaptureError{Cause: err}
|
||||
}
|
||||
return execution.RecordEvidence(name, value)
|
||||
}
|
||||
|
||||
func captureEvaluation[T any](execution contracts.ExecutionOptions, id string, evaluation contracts.Evaluation[T], assessment contracts.CandidateAssessment, guidance []string, accepted bool, failure error) error {
|
||||
if execution.Capture == nil {
|
||||
return nil
|
||||
}
|
||||
var candidate bool
|
||||
if evaluation != nil {
|
||||
view := evaluation.View()
|
||||
candidate = view.Candidate != nil
|
||||
if err := recordDomainEvidence(execution, "interpretations/"+id+"-domain.json", view.Evidence); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
errorText := ""
|
||||
if failure != nil {
|
||||
errorText = failure.Error()
|
||||
}
|
||||
return execution.RecordEvidence("interpretations/"+id+".json", map[string]any{
|
||||
"usable_candidate": candidate, "accepted": accepted, "correction_guidance": guidance,
|
||||
"assessment": assessmentEvidence(assessment), "error": errorText,
|
||||
})
|
||||
}
|
||||
|
||||
func finishOperation[T any](ctx context.Context, mode contracts.ExecutionMode, cause error, evaluation contracts.Evaluation[T], assessment contracts.CandidateAssessment, fallback contracts.Fallback[T], diagnostics contracts.TerminalDiagnostics[T]) (OperationResult[T], error) {
|
||||
var construct func(context.Context) (T, error)
|
||||
if fallback != nil {
|
||||
|
||||
Reference in New Issue
Block a user