Select prepared execution observers at run time
This commit is contained in:
@@ -3,6 +3,7 @@ package promptkit_test
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"reflect"
|
||||
"testing"
|
||||
"testing/synctest"
|
||||
"time"
|
||||
@@ -10,6 +11,31 @@ import (
|
||||
"go.maximumdirect.net/promptkit"
|
||||
)
|
||||
|
||||
func TestCoordinateObserverOrderWithContext(t *testing.T) {
|
||||
engine := observationEngine(t, &fakeLLMClient{response: &promptkit.GenerateResponse{Content: "ok"}})
|
||||
initial, err := engine.Run(context.Background(), promptkit.RunRequest{PromptID: "prompt", CaptureExchange: true})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var order []string
|
||||
observer := func(name string) promptkit.ExecutionObserver {
|
||||
return func(promptkit.ExecutionEvent) { order = append(order, name) }
|
||||
}
|
||||
ctx := promptkit.WithExecutionObserver(context.Background(), observer("context"))
|
||||
_, err = engine.Coordinate(ctx, promptkit.CoordinateRequest{Start: initial.Exchange, AdditionalTurns: 1, Observer: observer("coordinator"), Next: func(_ context.Context, turn promptkit.ConversationTurn) (*promptkit.RunRequest, error) {
|
||||
if turn.RemainingTurns == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
return &promptkit.RunRequest{PromptID: "prompt", Observer: observer("request")}, nil
|
||||
}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !reflect.DeepEqual(order, []string{"request", "coordinator", "context", "request", "coordinator", "context", "request", "coordinator", "context"}) {
|
||||
t.Fatalf("observer order = %v", order)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCoordinateUsesIndependentOperationBudgets(t *testing.T) {
|
||||
synctest.Test(t, func(t *testing.T) {
|
||||
engine := newBackendCapacityEngine(t, observationClientFunc(func(_ context.Context, req promptkit.GenerateRequest) (*promptkit.GenerateResponse, error) {
|
||||
|
||||
@@ -23,7 +23,9 @@ preparation or execution. Delegated calls from an injected collaborator retain
|
||||
the caller's cancellation, deadline, and unrelated context values, but do not
|
||||
reuse the enclosing invocation's accounting. Ordinary runs compose the request
|
||||
observer before any observers selected by the invocation context; contextual
|
||||
selection survives this boundary. Prepared handles use their retained observer.
|
||||
selection survives this boundary. Prepared executions compose the retained
|
||||
request observer with selection from the execution context after a successful
|
||||
claim; the preparation context does not select execution observers.
|
||||
|
||||
A recorder keeps only counters, resolved identity fields, and the selected
|
||||
callback. It has no global history, queue, goroutine, or mutex held across a
|
||||
|
||||
@@ -100,6 +100,8 @@ internal observer tests. No provider requests are needed.
|
||||
|
||||
## Stage 2 — Execution-Time Context For Prepared Handles
|
||||
|
||||
**Completed.**
|
||||
|
||||
Touch root `RunPrepared`, internal `usecase/prepared_execution.go`, and observer
|
||||
conversion only where required.
|
||||
|
||||
|
||||
@@ -687,9 +687,9 @@ func (e *Engine) run(ctx context.Context, req RunRequest) (*RunResult, error) {
|
||||
// content-validation rejection, including repair exhaustion, is returned in
|
||||
// RunResult, not as an operational
|
||||
// error. RunPrepared uses the retry choice frozen during PrepareExecution. An
|
||||
// operational error returns no partial RunResult. A retained Observer begins
|
||||
// only after a successful atomic claim, with identities from the frozen
|
||||
// snapshot, and receives a terminal event on normal return after public error
|
||||
// operational error returns no partial RunResult. The retained Observer and
|
||||
// execution-context observers begin only after a successful atomic claim, with
|
||||
// identities from the frozen snapshot, and receive a terminal event on normal return after public error
|
||||
// mapping. Invalid, foreign, discarded, or reused handles emit no events.
|
||||
// Callbacks run outside the handle and capacity locks; panics propagate and
|
||||
// do not promise a terminal event.
|
||||
|
||||
@@ -117,9 +117,10 @@ func (r *Runner) RunPrepared(ctx context.Context, prepared *PreparedExecution) (
|
||||
|
||||
var recorder *observation.Recorder
|
||||
var runID string
|
||||
if payload.observer != nil {
|
||||
observer := observation.Compose(payload.observer, observation.ObserverFromContext(ctx))
|
||||
if observer != nil {
|
||||
var idErr error
|
||||
recorder, idErr = observation.New(payload.observer)
|
||||
recorder, idErr = observation.New(observer)
|
||||
recorder.SetParentRunID(payload.parent.RunID())
|
||||
observation.RegisterClaim(ctx, recorder)
|
||||
ctx = observation.WithRecorder(ctx, recorder)
|
||||
|
||||
@@ -3,6 +3,7 @@ package promptkit_test
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"reflect"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
@@ -10,6 +11,50 @@ import (
|
||||
"go.maximumdirect.net/promptkit"
|
||||
)
|
||||
|
||||
func TestPreparedObservationUsesExecutionContext(t *testing.T) {
|
||||
engine := newPreparedContractEngine(t, &preparedRecordingClient{response: &promptkit.GenerateResponse{Content: "ok"}}, "hello")
|
||||
foreign := newPreparedContractEngine(t, &preparedRecordingClient{response: &promptkit.GenerateResponse{Content: "ok"}}, "hello")
|
||||
var order []string
|
||||
observe := func(name string) promptkit.ExecutionObserver {
|
||||
return func(promptkit.ExecutionEvent) { order = append(order, name) }
|
||||
}
|
||||
prepareCtx := promptkit.WithExecutionObserver(context.Background(), observe("preparation"))
|
||||
executeCtx := promptkit.WithExecutionObserver(context.Background(), observe("execution"))
|
||||
handle, err := engine.PrepareExecution(prepareCtx, promptkit.RunRequest{PromptID: "prepared", Observer: observe("explicit")})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(order) != 0 {
|
||||
t.Fatalf("preparation emitted events: %v", order)
|
||||
}
|
||||
if _, err := foreign.RunPrepared(executeCtx, handle); !errors.Is(err, promptkit.ErrInvalidRequest) || len(order) != 0 {
|
||||
t.Fatalf("foreign claim: %v, %v", err, order)
|
||||
}
|
||||
if _, err := engine.RunPrepared(executeCtx, handle); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !reflect.DeepEqual(order, []string{"explicit", "execution", "explicit", "execution", "explicit", "execution"}) {
|
||||
t.Fatalf("observer order: %v", order)
|
||||
}
|
||||
order = nil
|
||||
if _, err := engine.RunPrepared(executeCtx, handle); !errors.Is(err, promptkit.ErrInvalidRequest) || len(order) != 0 {
|
||||
t.Fatalf("reused claim: %v, %v", err, order)
|
||||
}
|
||||
if _, err := engine.RunPrepared(executeCtx, nil); !errors.Is(err, promptkit.ErrInvalidRequest) || len(order) != 0 {
|
||||
t.Fatalf("nil claim: %v, %v", err, order)
|
||||
}
|
||||
handle, err = engine.PrepareExecution(prepareCtx, promptkit.RunRequest{PromptID: "prepared"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := engine.RunPrepared(executeCtx, handle); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !reflect.DeepEqual(order, []string{"execution", "execution", "execution"}) {
|
||||
t.Fatalf("context-only order: %v", order)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPreparedObservationStartsOnlyAfterClaimAndKeepsObserver(t *testing.T) {
|
||||
engine := newPreparedContractEngine(t, &preparedRecordingClient{response: &promptkit.GenerateResponse{Content: "ok"}}, "hello")
|
||||
var events []promptkit.ExecutionEvent
|
||||
|
||||
Reference in New Issue
Block a user