184 lines
8.7 KiB
Go
184 lines
8.7 KiB
Go
package pipeline
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"testing"
|
|
|
|
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
|
|
"go.maximumdirect.net/promptkit"
|
|
)
|
|
|
|
func cacheableNumberOperation(initial promptkit.RunRequest) contracts.Preparation[int] {
|
|
return func(context.Context) (contracts.PreparedOperation[int], error) {
|
|
return contracts.NativeOperation[int]{Initial: initial, Correction: producerCorrection,
|
|
Interpret: func(ctx context.Context, native *promptkit.RunResult) (contracts.Evaluation[int], error) {
|
|
candidate, guidance, err := interpretProducerNumber(ctx, native)
|
|
return contracts.CandidateEvaluation[int]{Result: contracts.EvaluationView[int]{Candidate: candidate, Guidance: guidance}}, err
|
|
}, Fallback: func(context.Context, contracts.Evaluation[int]) (int, error) { return -1, nil }}, nil
|
|
}
|
|
}
|
|
|
|
func TestProducerCacheOperationStoresCorrectionAndChecksCurrentSource(t *testing.T) {
|
|
_, store := filesystemProducerCache(t)
|
|
calls, assessments, decodes := 0, 0, 0
|
|
engine := nativeProducerEngine(t, producerClientFunc(func(context.Context, promptkit.GenerateRequest) (*promptkit.GenerateResponse, error) {
|
|
value := 1
|
|
if calls == 0 {
|
|
value = 0
|
|
}
|
|
calls++
|
|
return &promptkit.GenerateResponse{Content: fmt.Sprintf(`{"value":%d}`, value)}, nil
|
|
}))
|
|
bound := numberCacheOperation(store)
|
|
execution := ExecutionContext[int]{Engine: engine, Controls: bound.Controls, ReuseEligible: true, Cache: bound.Cache,
|
|
Assess: func(ctx context.Context, value int) (contracts.CandidateAssessment, error) {
|
|
assessments++
|
|
if value == 1 {
|
|
return acceptedProducerAssessment(ctx, value)
|
|
}
|
|
return rejectedProducerAssessment(ctx, value)
|
|
}}
|
|
usable := true
|
|
execution.Cache.Decode = func(content []byte) (int, error) {
|
|
decodes++
|
|
if !usable {
|
|
return 0, errors.New("not usable for current source")
|
|
}
|
|
var value int
|
|
err := json.Unmarshal(content, &value)
|
|
return value, err
|
|
}
|
|
var preparedKeys []string
|
|
execution.Cache.Observe = func(_ context.Context, observation contracts.CacheObservation) error {
|
|
if observation.Prepared != nil {
|
|
preparedKeys = append(preparedKeys, observation.MessageContentHash)
|
|
}
|
|
return nil
|
|
}
|
|
prepare := cacheableNumberOperation(nativeProducerInitial())
|
|
first, err := ExecuteOperation(context.Background(), execution, prepare)
|
|
if err != nil || first.Value != 1 || calls != 2 || assessments != 2 || store.writes != 1 {
|
|
t.Fatalf("initial=%#v %v calls=%d assessments=%d writes=%d", first, err, calls, assessments, store.writes)
|
|
}
|
|
hit, err := ExecuteOperation(context.Background(), execution, prepare)
|
|
if err != nil || hit.Value != 1 || calls != 2 || assessments != 2 || decodes != 1 || hit.Evaluation != nil || len(preparedKeys) != 2 || preparedKeys[0] != preparedKeys[1] {
|
|
t.Fatalf("hit=%#v %v calls=%d assessments=%d decodes=%d keys=%v", hit, err, calls, assessments, decodes, preparedKeys)
|
|
}
|
|
usable = false
|
|
_, err = ExecuteOperation(context.Background(), execution, prepare)
|
|
if err != nil || calls != 3 || assessments != 3 || store.writes != 2 {
|
|
t.Fatalf("unusable source hit: %v calls=%d assessments=%d writes=%d", err, calls, assessments, store.writes)
|
|
}
|
|
}
|
|
|
|
func TestProducerExecutesOperationFrozenHandleAndPreservesFailedRefresh(t *testing.T) {
|
|
_, store := filesystemProducerCache(t)
|
|
initial := nativeProducerInitial()
|
|
calls := 0
|
|
fail := false
|
|
engine := nativeProducerEngine(t, producerClientFunc(func(_ context.Context, request promptkit.GenerateRequest) (*promptkit.GenerateResponse, error) {
|
|
calls++
|
|
if fail {
|
|
return nil, errors.New("provider failed")
|
|
}
|
|
if request.Prompt.Messages[1].Content != "Source: fixed source" {
|
|
t.Fatalf("prepared input changed: %#v", request.Prompt.Messages)
|
|
}
|
|
return &promptkit.GenerateResponse{Content: `{"value":1}`}, nil
|
|
}))
|
|
bound := numberCacheOperation(store)
|
|
execution := ExecutionContext[int]{Engine: engine, Controls: bound.Controls, ReuseEligible: true, Cache: bound.Cache, Assess: acceptedProducerAssessment}
|
|
execution.Cache.Observe = func(_ context.Context, observation contracts.CacheObservation) error {
|
|
if observation.Prepared != nil {
|
|
initial.Inputs["source"] = promptkit.Inline("changed after preparation")
|
|
}
|
|
return nil
|
|
}
|
|
if result, err := ExecuteOperation(context.Background(), execution, cacheableNumberOperation(initial)); err != nil || result.Value != 1 {
|
|
t.Fatalf("frozen = %#v %v", result, err)
|
|
}
|
|
initial = nativeProducerInitial()
|
|
execution.Controls.Cache = contracts.CacheRefresh
|
|
fail = true
|
|
if _, err := ExecuteOperation(context.Background(), execution, cacheableNumberOperation(initial)); err == nil {
|
|
t.Fatal("failed refresh succeeded")
|
|
}
|
|
initial = nativeProducerInitial()
|
|
execution.Controls.Cache = contracts.CacheAuto
|
|
result, err := ExecuteOperation(context.Background(), execution, cacheableNumberOperation(initial))
|
|
if err != nil || result.Value != 1 || calls != 2 || store.writes != 1 {
|
|
t.Fatalf("retained cache = %#v %v calls=%d writes=%d", result, err, calls, store.writes)
|
|
}
|
|
}
|
|
|
|
func TestProducerCacheOperationSkipsFallbackDerivedAndDeterministicWork(t *testing.T) {
|
|
_, store := filesystemProducerCache(t)
|
|
calls := 0
|
|
engine := nativeProducerEngine(t, producerClientFunc(func(context.Context, promptkit.GenerateRequest) (*promptkit.GenerateResponse, error) {
|
|
calls++
|
|
return &promptkit.GenerateResponse{Content: `{"value":0}`}, nil
|
|
}))
|
|
bound := numberCacheOperation(store)
|
|
execution := ExecutionContext[int]{Engine: engine, Controls: contracts.ProducerControls{Mode: contracts.ModePermissive, Cache: contracts.CacheAuto}, Cache: bound.Cache, Assess: rejectedProducerAssessment}
|
|
result, err := ExecuteOperation(context.Background(), execution, cacheableNumberOperation(nativeProducerInitial()))
|
|
if err != nil || result.Outcome.Disposition != contracts.ProducerFallback || store.reads != 0 || store.writes != 0 || calls != 1 {
|
|
t.Fatalf("ineligible result=%#v err=%v reads=%d writes=%d calls=%d", result, err, store.reads, store.writes, calls)
|
|
}
|
|
candidate := 2
|
|
execution.ReuseEligible = true
|
|
execution.Assess = acceptedProducerAssessment
|
|
_, err = ExecuteOperation(context.Background(), execution, func(context.Context) (contracts.PreparedOperation[int], error) {
|
|
return contracts.DeterministicOperation[int]{Evaluation: contracts.CandidateEvaluation[int]{Result: contracts.EvaluationView[int]{Candidate: &candidate}}}, nil
|
|
})
|
|
if err != nil || store.reads != 0 || store.writes != 0 || calls != 1 {
|
|
t.Fatalf("deterministic err=%v reads=%d writes=%d calls=%d", err, store.reads, store.writes, calls)
|
|
}
|
|
execution.Controls.Cache = contracts.CacheBypass
|
|
_, err = ExecuteOperation(context.Background(), execution, cacheableNumberOperation(nativeProducerInitial()))
|
|
if err != nil || store.reads != 0 || store.writes != 0 || calls != 2 {
|
|
t.Fatalf("bypass err=%v reads=%d writes=%d calls=%d", err, store.reads, store.writes, calls)
|
|
}
|
|
}
|
|
|
|
func TestProducerCacheOperationDistinguishesStorageAndEncodingErrors(t *testing.T) {
|
|
for _, encodeFailure := range []bool{false, true} {
|
|
_, store := filesystemProducerCache(t)
|
|
store.readErr, store.writeErr = errors.New("lookup unavailable"), errors.New("write unavailable")
|
|
encodingErr := errors.New("encoding failed")
|
|
calls, fallbacks := 0, 0
|
|
engine := nativeProducerEngine(t, producerClientFunc(func(context.Context, promptkit.GenerateRequest) (*promptkit.GenerateResponse, error) {
|
|
calls++
|
|
return &promptkit.GenerateResponse{Content: `{"value":1}`}, nil
|
|
}))
|
|
bound := numberCacheOperation(store)
|
|
var decisions []string
|
|
bound.Cache.Observe = func(_ context.Context, observation contracts.CacheObservation) error {
|
|
decisions = append(decisions, observation.Decision)
|
|
return nil
|
|
}
|
|
if encodeFailure {
|
|
bound.Cache.Encode = func(int) ([]byte, error) { return nil, encodingErr }
|
|
}
|
|
execution := ExecutionContext[int]{Engine: engine, Controls: bound.Controls, ReuseEligible: true, Cache: bound.Cache, Assess: acceptedProducerAssessment}
|
|
result, err := ExecuteOperation(context.Background(), execution, func(ctx context.Context) (contracts.PreparedOperation[int], error) {
|
|
prepared, err := cacheableNumberOperation(nativeProducerInitial())(ctx)
|
|
operation := prepared.(contracts.NativeOperation[int])
|
|
operation.Fallback = func(context.Context, contracts.Evaluation[int]) (int, error) { fallbacks++; return -1, nil }
|
|
return operation, err
|
|
})
|
|
if encodeFailure {
|
|
if !errors.Is(err, encodingErr) || result.Outcome.Disposition != "" {
|
|
t.Fatalf("encoding error=%#v %v", result, err)
|
|
}
|
|
} else if err != nil || result.Value != 1 || result.Outcome.Disposition != contracts.ProducerAccepted || len(decisions) != 3 || decisions[1] != "lookup_failed" || decisions[2] != "store_failed" {
|
|
t.Fatalf("storage diagnostics=%#v %v %v", result, err, decisions)
|
|
}
|
|
if calls != 1 || fallbacks != 0 {
|
|
t.Fatalf("calls=%d fallbacks=%d", calls, fallbacks)
|
|
}
|
|
}
|
|
}
|