Capture evidence for core-owned operations

This commit is contained in:
2026-11-25 15:15:29 +00:00
parent 4b5f574ee7
commit bab7b2305f
4 changed files with 354 additions and 8 deletions

View File

@@ -25,10 +25,20 @@ func executeNative[T any](ctx context.Context, execution ExecutionContext[T], op
if execution.Controls.MaxAdditionalTurns < 0 {
return empty, fmt.Errorf("%w: additional turns must not be negative", promptkit.ErrInvalidRequest)
}
if err := recordDomainEvidence(execution.Execution, "preparation-domain.json", operation.Evidence); err != nil {
return empty, err
}
var current nativeEvaluation[T]
var fatal error
evaluate := func(result *promptkit.RunResult) {
current = nativeEvaluation[T]{}
if execution.Execution.Capture != nil && result != nil {
defer func() {
if err := captureEvaluation(execution.Execution, result.RunID, current.evaluation, current.assessment, current.guidance, current.accepted, errors.Join(current.failure, fatal)); err != nil {
fatal = errors.Join(fatal, err)
}
}()
}
if err := CheckNativeResult(result, nil); err != nil {
current.failure = err
return
@@ -82,16 +92,41 @@ func executeNative[T any](ctx context.Context, execution ExecutionContext[T], op
request := producerRequest(operation.Initial, execution.Execution)
request.CaptureExchange = request.CaptureExchange || (operation.Correction != nil && execution.Controls.MaxAdditionalTurns > 0)
request, capture := CaptureNativeRequest(request, scopeEvidence(execution.Execution.Capture, "initial"))
if err := capture.Err(); err != nil {
return empty, err
}
var result *promptkit.RunResult
var err error
var cacheKey string
cache := execution.Cache
if execution.Execution.Capture != nil {
if err := execution.Execution.RecordEvidence("cache/selection.json", map[string]any{"mode": execution.Controls.Cache, "reuse_eligible": execution.ReuseEligible, "store_available": cache != nil && cache.Store != nil}); err != nil {
return empty, captureFailure(err, capture.Finish(nil, err, false, false))
}
if cache != nil {
copy := *cache
existing := copy.Observe
copy.Observe = func(ctx context.Context, observation contracts.CacheObservation) error {
if err := captureCacheObservation(execution.Execution.Capture, observation); err != nil {
return err
}
if existing != nil {
return existing(ctx, observation)
}
return nil
}
cache = &copy
}
}
if cache != nil && cache.Store != nil && execution.ReuseEligible && execution.Controls.Cache != contracts.CacheBypass {
if cache.Encode == nil || cache.Decode == nil {
return empty, errors.New("producer cache codec must be supplied")
err := errors.New("producer cache codec must be supplied")
return empty, captureFailure(err, capture.Finish(nil, err, false, false))
}
if mode := execution.Controls.Cache; mode != "" && mode != contracts.CacheAuto && mode != contracts.CacheRefresh {
return empty, fmt.Errorf("%w: unknown cache mode %q", promptkit.ErrInvalidRequest, mode)
err := fmt.Errorf("%w: unknown cache mode %q", promptkit.ErrInvalidRequest, mode)
return empty, captureFailure(err, capture.Finish(nil, err, false, false))
}
var prepared *promptkit.PreparedExecution
prepared, err = execution.Engine.PrepareExecution(ctx, request)
@@ -99,14 +134,20 @@ func executeNative[T any](ctx context.Context, execution ExecutionContext[T], op
defer prepared.Discard()
details := prepared.Details()
cacheKey = details.MessageContentHash
if capture != nil {
capture.RetainPrepared(copyPreparedEvidence(details))
}
if err := observeProducerCache(ctx, cache, contracts.CacheObservation{Decision: "prepared", MessageContentHash: cacheKey, Prepared: &details}); err != nil {
return empty, err
return empty, captureFailure(err, capture.Finish(nil, err, false, false))
}
reused, lookupErr := lookupProducerCache(ctx, cache, execution.Controls.Cache, cacheKey)
if lookupErr != nil {
return empty, lookupErr
return empty, captureFailure(lookupErr, capture.Finish(nil, lookupErr, false, false))
}
if reused != nil {
if err := capture.Finish(nil, nil, false, true); err != nil {
return empty, err
}
return OperationResult[T]{Value: *reused, Outcome: contracts.ProducerOutcome{Disposition: contracts.ProducerAccepted, ReuseEligible: true}}, nil
}
result, err = execution.Engine.RunPrepared(ctx, prepared)
@@ -118,11 +159,14 @@ func executeNative[T any](ctx context.Context, execution ExecutionContext[T], op
decision = "ineligible"
}
if err := observeProducerCache(ctx, cache, contracts.CacheObservation{Decision: decision}); err != nil {
return empty, err
return empty, captureFailure(err, capture.Finish(nil, err, false, false))
}
}
result, err = execution.Engine.Run(ctx, request)
}
if captureErr := capture.Finish(result, err, ctx.Err() != nil, false); captureErr != nil {
return empty, captureFailure(errors.Join(ctx.Err(), err), captureErr)
}
if canceled := ctx.Err(); canceled != nil {
return empty, canceled
}
@@ -139,10 +183,17 @@ func executeNative[T any](ctx context.Context, execution ExecutionContext[T], op
return empty, fatal
}
if !current.accepted && err == nil && len(current.guidance) > 0 && operation.Correction != nil && execution.Controls.MaxAdditionalTurns > 0 {
var pendingCapture *NativeCapture
var recordingErr error
coordination, coordinateErr := execution.Engine.Coordinate(ctx, promptkit.CoordinateRequest{
Start: result.Exchange, AdditionalTurns: execution.Controls.MaxAdditionalTurns,
Next: func(_ context.Context, turn promptkit.ConversationTurn) (*promptkit.RunRequest, error) {
if turn.Result != nil {
recordingErr = pendingCapture.Finish(turn.Result, nil, false, false)
pendingCapture = nil
if recordingErr != nil {
return nil, recordingErr
}
evaluate(turn.Result)
}
if fatal != nil {
@@ -155,8 +206,18 @@ func executeNative[T any](ctx context.Context, execution ExecutionContext[T], op
// Selecting another execution invalidates the entire previous
// evaluation before even attempting to construct its request.
current = nativeEvaluation[T]{}
correctionPath := ""
if execution.Execution.Capture != nil {
correctionPath = "corrections/" + turn.Exchange.Details().RunID
if recordingErr = execution.Execution.RecordEvidence(correctionPath+"/assignment.json", map[string]any{"guidance": guidance}); recordingErr != nil {
return nil, recordingErr
}
}
next, err := operation.Correction(ctx, guidance)
if err != nil {
if correctionPath != "" {
recordingErr = execution.Execution.RecordEvidence(correctionPath+"/construction-failure.json", map[string]any{"error": err.Error()})
}
return nil, err
}
if operation.Initial.ProfileID != "" {
@@ -166,9 +227,21 @@ func executeNative[T any](ctx context.Context, execution ExecutionContext[T], op
next.SessionID = operation.Initial.SessionID
}
next = producerRequest(next, execution.Execution)
if correctionPath != "" {
next, pendingCapture = CaptureNativeRequest(next, scopeEvidence(execution.Execution.Capture, correctionPath))
if recordingErr = pendingCapture.Err(); recordingErr != nil {
return nil, recordingErr
}
}
return &next, nil
},
})
if pendingCapture != nil {
recordingErr = errors.Join(recordingErr, pendingCapture.Finish(coordination.Undelivered, coordinateErr, coordination.Undelivered != nil, false))
}
if recordingErr != nil {
return empty, captureFailure(errors.Join(ctx.Err(), coordinateErr), recordingErr)
}
if canceled := ctx.Err(); canceled != nil {
return empty, canceled
}