Capture native execution facts and complete scoped debug evidence
This commit is contained in:
@@ -44,6 +44,22 @@ func RunProducer[T any](
|
||||
accepted := false
|
||||
evaluate := func(result *promptkit.RunResult) {
|
||||
candidate, guidance, failure, accepted = nil, nil, nil, false
|
||||
var assessment contracts.CandidateAssessment
|
||||
defer func() {
|
||||
if operation.Execution.Capture == nil || result == nil {
|
||||
return
|
||||
}
|
||||
errorText := ""
|
||||
if failure != nil {
|
||||
errorText = failure.Error()
|
||||
}
|
||||
if err := operation.Execution.RecordEvidence("interpretations/"+result.RunID+".json", map[string]any{
|
||||
"run_id": result.RunID, "usable_candidate": candidate != nil, "accepted": accepted,
|
||||
"correction_guidance": guidance, "assessment": assessmentEvidence(assessment), "error": errorText,
|
||||
}); err != nil {
|
||||
assessmentError = errors.Join(assessmentError, err)
|
||||
}
|
||||
}()
|
||||
if canceled := ctx.Err(); canceled != nil {
|
||||
failure = canceled
|
||||
return
|
||||
@@ -58,6 +74,10 @@ func RunProducer[T any](
|
||||
return
|
||||
}
|
||||
if failure != nil {
|
||||
var captureErr *contracts.CaptureError
|
||||
if errors.As(failure, &captureErr) {
|
||||
assessmentError = failure
|
||||
}
|
||||
candidate, guidance = nil, nil
|
||||
return
|
||||
}
|
||||
@@ -65,7 +85,8 @@ func RunProducer[T any](
|
||||
failure = errors.New("producer response could not be interpreted as a usable candidate")
|
||||
return
|
||||
}
|
||||
assessment, err := operation.Assess(ctx, *candidate)
|
||||
var err error
|
||||
assessment, err = operation.Assess(ctx, *candidate)
|
||||
if err != nil {
|
||||
assessmentError = err
|
||||
return
|
||||
@@ -85,10 +106,31 @@ func RunProducer[T any](
|
||||
|
||||
request := producerRequest(initial, operation.Execution, true)
|
||||
request.CaptureExchange = request.CaptureExchange || (correct != nil && operation.Controls.MaxAdditionalTurns > 0)
|
||||
request, capture := CaptureNativeRequest(request, scopeEvidence(operation.Execution.Capture, "initial"))
|
||||
if captureErr := capture.Err(); captureErr != nil {
|
||||
return zero, contracts.ProducerOutcome{}, captureErr
|
||||
}
|
||||
var result *promptkit.RunResult
|
||||
var err error
|
||||
var cacheKey string
|
||||
cache := operation.Cache
|
||||
if captureErr := operation.Execution.RecordEvidence("cache/selection.json", map[string]any{"mode": operation.Controls.Cache, "reuse_eligible": operation.ReuseEligible, "store_available": cache != nil && cache.Store != nil}); captureErr != nil {
|
||||
return zero, contracts.ProducerOutcome{}, captureErr
|
||||
}
|
||||
if cache != nil && operation.Execution.Capture != nil {
|
||||
owned := *cache
|
||||
existing := owned.Observe
|
||||
owned.Observe = func(ctx context.Context, observation contracts.CacheObservation) error {
|
||||
if err := captureCacheObservation(operation.Execution.Capture, observation); err != nil {
|
||||
return err
|
||||
}
|
||||
if existing != nil {
|
||||
return existing(ctx, observation)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
cache = &owned
|
||||
}
|
||||
if cache != nil && cache.Store != nil && operation.ReuseEligible && operation.Controls.Cache != contracts.CacheBypass {
|
||||
if cache.Encode == nil || cache.Decode == nil {
|
||||
return zero, contracts.ProducerOutcome{}, errors.New("producer cache codec must be supplied")
|
||||
@@ -101,15 +143,21 @@ func RunProducer[T any](
|
||||
if err == nil {
|
||||
defer prepared.Discard()
|
||||
details := prepared.Details()
|
||||
if capture != nil {
|
||||
capture.RetainPrepared(copyPreparedEvidence(details))
|
||||
}
|
||||
cacheKey = details.MessageContentHash
|
||||
if captureErr := observeProducerCache(ctx, cache, contracts.CacheObservation{Decision: "prepared", MessageContentHash: cacheKey, Prepared: &details}); captureErr != nil {
|
||||
return zero, contracts.ProducerOutcome{}, captureErr
|
||||
return zero, contracts.ProducerOutcome{}, captureFailure(captureErr, capture.Finish(nil, captureErr, false, false))
|
||||
}
|
||||
reused, lookupErr := lookupProducerCache(ctx, cache, operation.Controls.Cache, cacheKey)
|
||||
if lookupErr != nil {
|
||||
return zero, contracts.ProducerOutcome{}, lookupErr
|
||||
return zero, contracts.ProducerOutcome{}, captureFailure(lookupErr, capture.Finish(nil, lookupErr, false, false))
|
||||
}
|
||||
if reused != nil {
|
||||
if captureErr := capture.Finish(nil, nil, false, true); captureErr != nil {
|
||||
return zero, contracts.ProducerOutcome{}, captureErr
|
||||
}
|
||||
return *reused, contracts.ProducerOutcome{Disposition: contracts.ProducerAccepted, ReuseEligible: true}, nil
|
||||
}
|
||||
result, err = engine.RunPrepared(ctx, prepared)
|
||||
@@ -121,11 +169,14 @@ func RunProducer[T any](
|
||||
decision = "ineligible"
|
||||
}
|
||||
if err := observeProducerCache(ctx, cache, contracts.CacheObservation{Decision: decision}); err != nil {
|
||||
return zero, contracts.ProducerOutcome{}, err
|
||||
return zero, contracts.ProducerOutcome{}, captureFailure(err, capture.Finish(nil, err, false, false))
|
||||
}
|
||||
}
|
||||
result, err = engine.Run(ctx, request)
|
||||
}
|
||||
if captureErr := capture.Finish(result, err, ctx.Err() != nil, false); captureErr != nil {
|
||||
return zero, contracts.ProducerOutcome{}, captureFailure(errors.Join(ctx.Err(), err), captureErr)
|
||||
}
|
||||
if canceled := ctx.Err(); canceled != nil {
|
||||
return zero, contracts.ProducerOutcome{}, canceled
|
||||
}
|
||||
@@ -142,11 +193,18 @@ func RunProducer[T any](
|
||||
}
|
||||
|
||||
if !accepted && err == nil && len(guidance) > 0 && correct != nil && operation.Controls.MaxAdditionalTurns > 0 {
|
||||
var pendingCapture *NativeCapture
|
||||
var recordingErr error
|
||||
coordination, coordinateErr := engine.Coordinate(ctx, promptkit.CoordinateRequest{
|
||||
Start: result.Exchange, AdditionalTurns: operation.Controls.MaxAdditionalTurns, Observer: operation.Execution.Observer,
|
||||
Next: func(_ context.Context, turn promptkit.ConversationTurn) (*promptkit.RunRequest, error) {
|
||||
// The first callback has no new result. Its findings already exist.
|
||||
if turn.Result != nil {
|
||||
recordingErr = pendingCapture.Finish(turn.Result, nil, false, false)
|
||||
pendingCapture = nil
|
||||
if recordingErr != nil {
|
||||
return nil, recordingErr
|
||||
}
|
||||
evaluate(turn.Result)
|
||||
}
|
||||
if assessmentError != nil {
|
||||
@@ -158,8 +216,19 @@ func RunProducer[T any](
|
||||
// Once another execution is selected, an earlier candidate is not a
|
||||
// terminal fallback source if construction or native execution fails.
|
||||
candidate = nil
|
||||
correctionPath := ""
|
||||
if operation.Execution.Capture != nil {
|
||||
correctionPath = "corrections/" + turn.Exchange.Details().RunID
|
||||
recordingErr = operation.Execution.RecordEvidence(correctionPath+"/assignment.json", map[string]any{"guidance": guidance})
|
||||
if recordingErr != nil {
|
||||
return nil, recordingErr
|
||||
}
|
||||
}
|
||||
next, buildErr := correct(ctx, guidance)
|
||||
if buildErr != nil {
|
||||
if correctionPath != "" {
|
||||
recordingErr = operation.Execution.RecordEvidence(correctionPath+"/construction-failure.json", map[string]any{"error": buildErr.Error()})
|
||||
}
|
||||
return nil, buildErr
|
||||
}
|
||||
if initial.ProfileID != "" {
|
||||
@@ -169,9 +238,21 @@ func RunProducer[T any](
|
||||
next.SessionID = initial.SessionID
|
||||
}
|
||||
next = producerRequest(next, operation.Execution, false)
|
||||
if operation.Execution.Capture != nil {
|
||||
next, pendingCapture = CaptureNativeRequest(next, scopeEvidence(operation.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 zero, contracts.ProducerOutcome{}, captureFailure(coordinateErr, recordingErr)
|
||||
}
|
||||
if canceled := ctx.Err(); canceled != nil {
|
||||
return zero, contracts.ProducerOutcome{}, canceled
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user