Use native contexts for execution observation

This commit is contained in:
2026-11-23 02:38:21 +00:00
parent 88ebdbd819
commit 7aa4f2ec4d
9 changed files with 99 additions and 59 deletions

View File

@@ -80,8 +80,8 @@ func TestProducerCachesAppliedCorrectionUnderInitialKeyAndReusesWithoutExecution
operation := numberCacheOperation(store)
operation.Execution = contracts.ExecutionOptions{
OnPrepared: func(context.Context, promptkit.PreparedRun) { nativePrepared++ },
Observer: func(promptkit.ExecutionEvent) { events++ },
}
ctx := promptkit.WithExecutionObserver(context.Background(), func(promptkit.ExecutionEvent) { events++ })
var keys []string
var decisions []string
operation.Cache.Observe = func(_ context.Context, observation contracts.CacheObservation) error {
@@ -115,7 +115,7 @@ func TestProducerCachesAppliedCorrectionUnderInitialKeyAndReusesWithoutExecution
t.Fatalf("early or incorrect cache store: %s %q, calls=%d assessments=%d", hash, content, calls, assessments)
}
}
value, outcome, err := RunProducer(context.Background(), engine, operation, nativeProducerInitial(), interpretApplied, producerCorrection, nil)
value, outcome, err := RunProducer(ctx, engine, operation, nativeProducerInitial(), interpretApplied, producerCorrection, nil)
if err != nil || value != 20 || outcome.Disposition != contracts.ProducerAccepted || store.writes != 1 || nativePrepared != 2 || events == 0 {
t.Fatalf("accepted correction = %d %#v %v; writes %d, prepared %d, events %d", value, outcome, err, store.writes, nativePrepared, events)
}
@@ -132,7 +132,7 @@ func TestProducerCachesAppliedCorrectionUnderInitialKeyAndReusesWithoutExecution
t.Fatal("cache hit reassessed")
return contracts.CandidateAssessment{}, nil
}
value, outcome, err = RunProducer(context.Background(), engine, operation, initial,
value, outcome, err = RunProducer(ctx, engine, operation, initial,
func(context.Context, *promptkit.RunResult) (*int, []string, error) {
t.Fatal("cache hit interpreted a model result")
return nil, nil, nil
@@ -145,7 +145,7 @@ func TestProducerCachesAppliedCorrectionUnderInitialKeyAndReusesWithoutExecution
}
operation.Assess = acceptedProducerAssessment
initial.Inputs["source"] = promptkit.Inline("changed source content")
value, _, err = RunProducer(context.Background(), engine, operation, initial, interpretApplied, producerCorrection, nil)
value, _, err = RunProducer(ctx, engine, operation, initial, interpretApplied, producerCorrection, nil)
if err != nil || value != 20 || calls != 3 || len(keys) != 3 || keys[0] == keys[2] || store.writes != 2 {
t.Fatalf("content change did not miss: value %d error %v calls %d keys %v", value, err, calls, keys)
}
@@ -154,6 +154,15 @@ func TestProducerCachesAppliedCorrectionUnderInitialKeyAndReusesWithoutExecution
func TestProducerExecutesFrozenPreparedHandleAfterLookup(t *testing.T) {
_, store := filesystemProducerCache(t)
initial := nativeProducerInitial()
started, finished := 0, 0
ctx := promptkit.WithExecutionObserver(context.Background(), func(event promptkit.ExecutionEvent) {
if event.Kind == promptkit.ExecutionStarted {
started++
}
if event.Kind == promptkit.ExecutionFinished {
finished++
}
})
engine := nativeProducerEngine(t, producerClientFunc(func(_ context.Context, request promptkit.GenerateRequest) (*promptkit.GenerateResponse, error) {
if len(request.Prompt.Messages) != 2 || request.Prompt.Messages[1].Content != "Source: fixed source" {
t.Fatalf("request was prepared again after cache lookup: %#v", request.Prompt.Messages)
@@ -164,6 +173,9 @@ func TestProducerExecutesFrozenPreparedHandleAfterLookup(t *testing.T) {
var hash string
operation.Cache.Observe = func(_ context.Context, observation contracts.CacheObservation) error {
if observation.Prepared != nil {
if started != 0 || finished != 0 {
t.Fatal("preparation emitted an execution")
}
hash = observation.MessageContentHash
initial.Inputs["source"] = promptkit.Inline("mutation after preparation")
observation.Prepared.Messages[1].Content = "mutated detached details"
@@ -176,9 +188,12 @@ func TestProducerExecutesFrozenPreparedHandleAfterLookup(t *testing.T) {
t.Fatalf("mutable snapshot changed key: %s", key)
}
}
if value, _, err := RunProducer(context.Background(), engine, operation, initial, interpretProducerNumber, nil, nil); err != nil || value != 1 {
if value, _, err := RunProducer(ctx, engine, operation, initial, interpretProducerNumber, nil, nil); err != nil || value != 1 {
t.Fatalf("prepared execution = %d, %v", value, err)
}
if started != 1 || finished != 1 {
t.Fatalf("prepared execution lost contextual observation: %d/%d", started, finished)
}
}
func TestProducerCacheSkipsIneligibleBypassedAndNonacceptedWrites(t *testing.T) {