Observe claimed prepared executions

This commit is contained in:
2026-09-23 17:15:51 +00:00
parent 0e1ad784d1
commit 63bedf2a97
12 changed files with 279 additions and 11 deletions

View File

@@ -71,6 +71,7 @@ type RunRequest struct {
Validation *OutputContract
AppendedMessages []RenderedMessage
TransientRetries *int
Observer ExecutionObserver `json:"-"`
}
// RunResult represents the complete result of a prompt execution run.

View File

@@ -202,6 +202,21 @@ func FromContext(ctx context.Context) *Recorder {
return recorder
}
type claimSinkKey struct{}
// WithClaimSink lets the facade learn which invocation actually claimed a
// prepared handle, so it can emit the terminal event after public mapping.
func WithClaimSink(ctx context.Context, sink **Recorder) context.Context {
return context.WithValue(ctx, claimSinkKey{}, sink)
}
// RegisterClaim records an invocation after its prepared handle is claimed.
func RegisterClaim(ctx context.Context, recorder *Recorder) {
if sink, ok := ctx.Value(claimSinkKey{}).(**Recorder); ok && sink != nil {
*sink = recorder
}
}
type usageField struct {
value int
known, complete bool

View File

@@ -34,6 +34,7 @@ type preparedExecutionPayload struct {
validation validate.PreparedValidation
directKey string
transientRetries uint64
observer domain.ExecutionObserver
}
// PrepareExecution completes preparation without generation or admission and
@@ -68,6 +69,7 @@ func (r *Runner) PrepareExecution(ctx context.Context, req domain.RunRequest) (*
validation: operation.validation,
directKey: state.effectiveModel.APIKey,
transientRetries: state.transientRetries,
observer: req.Observer,
},
}, nil
}
@@ -115,9 +117,28 @@ func (r *Runner) RunPrepared(ctx context.Context, prepared *PreparedExecution) (
}
defer payload.clear()
runID, err := observation.NewRunID()
if err != nil {
return nil, fmt.Errorf("failed to create run id: %w", err)
var recorder *observation.Recorder
var runID string
if payload.observer != nil {
var idErr error
recorder, idErr = observation.New(payload.observer)
observation.RegisterClaim(ctx, recorder)
ctx = observation.WithRecorder(ctx, recorder)
selected := payload.prepared
recorder.UpdateSelection(observation.Selection{PromptID: selected.PromptID,
PromptVersion: selected.PromptVersion, ProfileID: selected.SelectedProfileID,
BackendID: selected.SelectedBackendID, Model: selected.EffectiveModelParams.Model})
recorder.Start()
if idErr != nil {
return nil, fmt.Errorf("failed to create run id: %w", idErr)
}
runID = recorder.RunID()
} else {
var err error
runID, err = observation.NewRunID()
if err != nil {
return nil, fmt.Errorf("failed to create run id: %w", err)
}
}
start := time.Now().UTC()
@@ -173,6 +194,7 @@ func (p *preparedExecutionPayload) clear() {
p.validation = nil
p.directKey = ""
p.transientRetries = 0
p.observer = nil
}
type noOpPreparedValidation struct {