Define typed operation preparation and evaluation contracts
This commit is contained in:
@@ -150,8 +150,28 @@ or are renamed; a filter matching no tests does not verify the stage.
|
|||||||
|
|
||||||
## Stage 1 — Define The Typed Preparation And Evaluation API
|
## Stage 1 — Define The Typed Preparation And Evaluation API
|
||||||
|
|
||||||
|
**Status:** Complete.
|
||||||
|
|
||||||
**Goal:** Establish the authoring contract using representative integrations.
|
**Goal:** Establish the authoring contract using representative integrations.
|
||||||
|
|
||||||
|
### Migration checklist
|
||||||
|
|
||||||
|
- Native producer callers: D&D scene planning; spell, combat-turn, enemy-event,
|
||||||
|
scene-description, registry, and occurrence extractors; and NPC, item, and
|
||||||
|
location registry normalizers. Combat-turn and enemy-event extraction also
|
||||||
|
have deterministic no-model branches.
|
||||||
|
- Registry normalizers additionally call the deterministic assessment helper
|
||||||
|
for insufficient candidates and the terminal-failure helper for preparation
|
||||||
|
limits. Pipeline completion also calls the assessment helper.
|
||||||
|
- Migrate chunker registration and typed extractor, merger, and normalizer
|
||||||
|
registries, including generic unit chunking, append-order merge, and noop
|
||||||
|
normalization, plus deterministic D&D normalizers.
|
||||||
|
- Adapt direct producer, cache, capture, cancellation, operation, typed-registry,
|
||||||
|
CLI, integration, and module tests as their entry points move. Combat semantic
|
||||||
|
validation remains a separate one-shot native caller.
|
||||||
|
|
||||||
|
The inventory is a migration aid; confirm additional callers as interfaces move.
|
||||||
|
|
||||||
**Inspect:** `contracts/{contracts,typed_pipeline,producer}.go`,
|
**Inspect:** `contracts/{contracts,typed_pipeline,producer}.go`,
|
||||||
`pipeline/{typed_execution,construction}.go`, spell extraction, scene planning,
|
`pipeline/{typed_execution,construction}.go`, spell extraction, scene planning,
|
||||||
the three registry normalizers, and current source types.
|
the three registry normalizers, and current source types.
|
||||||
|
|||||||
154
internal/framework/contracts/preparation.go
Normal file
154
internal/framework/contracts/preparation.go
Normal file
@@ -0,0 +1,154 @@
|
|||||||
|
package contracts
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
"gitea.maximumdirect.net/eric/notarius/internal/core/source"
|
||||||
|
"go.maximumdirect.net/promptkit"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Preparation is invoked by core once for an operation. An ordinary preparation
|
||||||
|
// failure is returned as an error; only an explicitly supported limit is a
|
||||||
|
// PreparationLimit. Preparation does not choose execution collaborators.
|
||||||
|
type Preparation[T any] func(context.Context) (PreparedOperation[T], error)
|
||||||
|
|
||||||
|
// PreparedOperation is one of DeterministicOperation, NativeOperation, or
|
||||||
|
// PreparationLimit. Its inputs and callbacks belong to one invocation. A shared
|
||||||
|
// module must not keep mutable operation state in its fields.
|
||||||
|
//
|
||||||
|
// Preparation transfers its returned maps, slices, values, and closures to the
|
||||||
|
// operation; callers must not mutate shared input through them. Existing source
|
||||||
|
// and reference isolation boundaries still apply. Core does not promise a
|
||||||
|
// general deep copy of domain values or callback captures.
|
||||||
|
type PreparedOperation[T any] interface {
|
||||||
|
preparedOperation()
|
||||||
|
operationType(T)
|
||||||
|
}
|
||||||
|
|
||||||
|
// EvaluationView is the common information core needs to assess a result.
|
||||||
|
// Candidate may be nil when interpretation supplies actionable guidance before
|
||||||
|
// a value exists. Diagnostics and optional evidence belong to this evaluation,
|
||||||
|
// not to an earlier or later candidate. Supplying a candidate is not acceptance.
|
||||||
|
type EvaluationView[T any] struct {
|
||||||
|
Candidate *T
|
||||||
|
Guidance []string
|
||||||
|
Diagnostics []ProducerDiagnostic
|
||||||
|
// Evidence is evaluated only when operation evidence is enabled. It may
|
||||||
|
// capture typed evaluation details, which must remain fixed for its lifetime.
|
||||||
|
Evidence func() (any, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Evaluation allows a domain facility to retain typed information alongside
|
||||||
|
// the common view without exposing that information to core.
|
||||||
|
type Evaluation[T any] interface {
|
||||||
|
View() EvaluationView[T]
|
||||||
|
}
|
||||||
|
|
||||||
|
// CandidateEvaluation is the common evaluation for ordinary integrations.
|
||||||
|
type CandidateEvaluation[T any] struct{ Result EvaluationView[T] }
|
||||||
|
|
||||||
|
func (evaluation CandidateEvaluation[T]) View() EvaluationView[T] { return evaluation.Result }
|
||||||
|
|
||||||
|
// Fallback constructs and intrinsically checks supported output. The final
|
||||||
|
// evaluation is nil when no evaluation remains eligible. Core selects when to
|
||||||
|
// invoke it; the callback must not assert ordinary validator acceptance.
|
||||||
|
type Fallback[T any] func(context.Context, Evaluation[T]) (T, error)
|
||||||
|
|
||||||
|
// TerminalDiagnostics derives terminal domain diagnostics from the same
|
||||||
|
// evaluation and disposition as the returned value. It may be nil.
|
||||||
|
type TerminalDiagnostics[T any] func(Evaluation[T], ProducerOutcome) ([]ProducerDiagnostic, error)
|
||||||
|
|
||||||
|
// DeterministicOperation supplies one candidate without a native request.
|
||||||
|
type DeterministicOperation[T any] struct {
|
||||||
|
Evaluation Evaluation[T]
|
||||||
|
Fallback Fallback[T]
|
||||||
|
Diagnostics TerminalDiagnostics[T]
|
||||||
|
}
|
||||||
|
|
||||||
|
func (DeterministicOperation[T]) preparedOperation() {}
|
||||||
|
func (DeterministicOperation[T]) operationType(T) {}
|
||||||
|
|
||||||
|
// NativeOperation describes interpretation and correction, not execution.
|
||||||
|
// Initial contains the explicit profile/session selections. Core carries them
|
||||||
|
// and its resolved native options through both correction forms.
|
||||||
|
type NativeOperation[T any] struct {
|
||||||
|
Initial promptkit.RunRequest
|
||||||
|
Interpret func(context.Context, *promptkit.RunResult) (Evaluation[T], error)
|
||||||
|
Correction CorrectionBuilder
|
||||||
|
Fallback Fallback[T]
|
||||||
|
Diagnostics TerminalDiagnostics[T]
|
||||||
|
// Evidence is optional, lazy preparation evidence for this operation.
|
||||||
|
Evidence func() (any, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (NativeOperation[T]) preparedOperation() {}
|
||||||
|
func (NativeOperation[T]) operationType(T) {}
|
||||||
|
|
||||||
|
// CorrectionBuilder constructs only a native assignment; core and PromptKit
|
||||||
|
// coordinate continuation and conversation history.
|
||||||
|
type CorrectionBuilder func(context.Context, []string) (promptkit.RunRequest, error)
|
||||||
|
|
||||||
|
// PromptCorrection binds ordered guidance to an integration-selected native
|
||||||
|
// input. More specialized corrections can supply a CorrectionBuilder directly.
|
||||||
|
func PromptCorrection(promptID, inputName string, bind func([]string) promptkit.ArtifactRef) CorrectionBuilder {
|
||||||
|
return func(_ context.Context, guidance []string) (promptkit.RunRequest, error) {
|
||||||
|
return promptkit.RunRequest{PromptID: promptID, Inputs: map[string]promptkit.ArtifactRef{inputName: bind(guidance)}}, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// PreparationLimit represents an existing explicit limit, not an arbitrary
|
||||||
|
// preparation error. Core applies terminal policy to Cause and Fallback.
|
||||||
|
type PreparationLimit[T any] struct {
|
||||||
|
Cause error
|
||||||
|
Fallback Fallback[T]
|
||||||
|
Diagnostics TerminalDiagnostics[T]
|
||||||
|
Evidence func() (any, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (PreparationLimit[T]) preparedOperation() {}
|
||||||
|
func (PreparationLimit[T]) operationType(T) {}
|
||||||
|
|
||||||
|
// ExtractionInput contains domain inputs only. Its source, chunk, references,
|
||||||
|
// and metadata follow the existing detached request boundaries; a preparation
|
||||||
|
// that retains mutable data must own the data it may modify.
|
||||||
|
type ExtractionInput struct {
|
||||||
|
Source *source.SourceDocument
|
||||||
|
Chunk *source.Chunk
|
||||||
|
AmbientContext map[string]any
|
||||||
|
SourceInput LLMInputMaterial
|
||||||
|
SessionID string
|
||||||
|
References ReferenceSet
|
||||||
|
LLMProfile string
|
||||||
|
Metadata map[string]any
|
||||||
|
}
|
||||||
|
|
||||||
|
type MergeInput[T any] struct {
|
||||||
|
Source *source.SourceDocument
|
||||||
|
LaneID string
|
||||||
|
ExtractOutputs []ExtractArtifact[T]
|
||||||
|
SourceInput LLMInputMaterial
|
||||||
|
SessionID string
|
||||||
|
References ReferenceSet
|
||||||
|
LLMProfile string
|
||||||
|
Metadata map[string]any
|
||||||
|
}
|
||||||
|
|
||||||
|
type NormalizeInput[T any] struct {
|
||||||
|
Source *source.SourceDocument
|
||||||
|
LaneID string
|
||||||
|
MergeOutput MergeArtifact[T]
|
||||||
|
SourceInput LLMInputMaterial
|
||||||
|
SessionID string
|
||||||
|
References ReferenceSet
|
||||||
|
LLMProfile string
|
||||||
|
Metadata map[string]any
|
||||||
|
}
|
||||||
|
|
||||||
|
type ChunkInput struct {
|
||||||
|
Source *source.SourceDocument
|
||||||
|
SourceInput LLMInputMaterial
|
||||||
|
SessionID string
|
||||||
|
References ReferenceSet
|
||||||
|
LLMProfile string
|
||||||
|
Metadata map[string]any
|
||||||
|
}
|
||||||
138
internal/framework/contracts/preparation_test.go
Normal file
138
internal/framework/contracts/preparation_test.go
Normal file
@@ -0,0 +1,138 @@
|
|||||||
|
package contracts_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"gitea.maximumdirect.net/eric/notarius/internal/core/source"
|
||||||
|
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
|
||||||
|
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd"
|
||||||
|
"go.maximumdirect.net/promptkit"
|
||||||
|
)
|
||||||
|
|
||||||
|
// These small preparations exercise the authoring boundary without executing a
|
||||||
|
// producer. Production adapters retain their own interpretation and identity.
|
||||||
|
func spellPreparation(input contracts.ExtractionInput) contracts.Preparation[dnd.SpellList] {
|
||||||
|
return func(context.Context) (contracts.PreparedOperation[dnd.SpellList], error) {
|
||||||
|
return contracts.NativeOperation[dnd.SpellList]{
|
||||||
|
Initial: promptkit.RunRequest{PromptID: "spell", ProfileID: input.LLMProfile, SessionID: input.SessionID,
|
||||||
|
Inputs: map[string]promptkit.ArtifactRef{"text": promptkit.Inline(string(input.Chunk.Content))}},
|
||||||
|
Interpret: func(_ context.Context, result *promptkit.RunResult) (contracts.Evaluation[dnd.SpellList], error) {
|
||||||
|
var value dnd.SpellList
|
||||||
|
err := json.Unmarshal([]byte(result.RawOutput), &value)
|
||||||
|
return contracts.CandidateEvaluation[dnd.SpellList]{Result: contracts.EvaluationView[dnd.SpellList]{Candidate: &value}}, err
|
||||||
|
},
|
||||||
|
Correction: contracts.PromptCorrection("spell-repair", "feedback", func(guidance []string) promptkit.ArtifactRef {
|
||||||
|
return promptkit.Inline(strings.Join(guidance, "\n"))
|
||||||
|
}),
|
||||||
|
Fallback: func(context.Context, contracts.Evaluation[dnd.SpellList]) (dnd.SpellList, error) {
|
||||||
|
return dnd.SpellList{SpellCasts: []dnd.SpellCast{}}, nil
|
||||||
|
},
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func scenePreparation(input contracts.ChunkInput) contracts.Preparation[source.ChunkPlan] {
|
||||||
|
return func(context.Context) (contracts.PreparedOperation[source.ChunkPlan], error) {
|
||||||
|
return contracts.NativeOperation[source.ChunkPlan]{
|
||||||
|
Initial: promptkit.RunRequest{PromptID: "scenes", ProfileID: input.LLMProfile, SessionID: input.SessionID},
|
||||||
|
Interpret: func(context.Context, *promptkit.RunResult) (contracts.Evaluation[source.ChunkPlan], error) {
|
||||||
|
return contracts.CandidateEvaluation[source.ChunkPlan]{Result: contracts.EvaluationView[source.ChunkPlan]{
|
||||||
|
Guidance: []string{"Supply materializable boundaries."},
|
||||||
|
}}, nil
|
||||||
|
},
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type registryEvaluation struct {
|
||||||
|
contracts.CandidateEvaluation[[]string]
|
||||||
|
omissions []string
|
||||||
|
}
|
||||||
|
|
||||||
|
func registryPreparation(records []string) contracts.Preparation[[]string] {
|
||||||
|
originals := append([]string(nil), records...)
|
||||||
|
return func(context.Context) (contracts.PreparedOperation[[]string], error) {
|
||||||
|
if len(originals) < 2 {
|
||||||
|
return contracts.DeterministicOperation[[]string]{Evaluation: contracts.CandidateEvaluation[[]string]{Result: contracts.EvaluationView[[]string]{Candidate: &originals}}}, nil
|
||||||
|
}
|
||||||
|
return contracts.NativeOperation[[]string]{
|
||||||
|
Initial: promptkit.RunRequest{PromptID: "reconcile"},
|
||||||
|
Interpret: func(_ context.Context, _ *promptkit.RunResult) (contracts.Evaluation[[]string], error) {
|
||||||
|
applied := append([]string(nil), originals...)
|
||||||
|
omissions := []string{"unsafe group"}
|
||||||
|
return registryEvaluation{CandidateEvaluation: contracts.CandidateEvaluation[[]string]{Result: contracts.EvaluationView[[]string]{
|
||||||
|
Candidate: &applied, Guidance: omissions,
|
||||||
|
Evidence: func() (any, error) { return omissions, nil },
|
||||||
|
}}, omissions: omissions}, nil
|
||||||
|
},
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPreparationsDescribeDistinctOutcomes(t *testing.T) {
|
||||||
|
ctx := context.Background()
|
||||||
|
prepared, err := spellPreparation(contracts.ExtractionInput{Chunk: &source.Chunk{Content: []byte("cast")}, LLMProfile: "profile", SessionID: "session"})(ctx)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
spell := prepared.(contracts.NativeOperation[dnd.SpellList])
|
||||||
|
if spell.Initial.ProfileID != "profile" || spell.Initial.SessionID != "session" {
|
||||||
|
t.Fatal("native selections not retained")
|
||||||
|
}
|
||||||
|
correction, err := spell.Correction(ctx, []string{"fix the selection"})
|
||||||
|
if err != nil || correction.PromptID != "spell-repair" || correction.Inputs["feedback"] != promptkit.Inline("fix the selection") {
|
||||||
|
t.Fatalf("correction = %#v, %v", correction, err)
|
||||||
|
}
|
||||||
|
plan, err := scenePreparation(contracts.ChunkInput{})(ctx)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
interpreted, err := plan.(contracts.NativeOperation[source.ChunkPlan]).Interpret(ctx, &promptkit.RunResult{})
|
||||||
|
if err != nil || interpreted.View().Candidate != nil || len(interpreted.View().Guidance) != 1 {
|
||||||
|
t.Fatalf("interpretation = %#v, %v", interpreted, err)
|
||||||
|
}
|
||||||
|
deterministic, err := registryPreparation([]string{"one"})(ctx)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if got := deterministic.(contracts.DeterministicOperation[[]string]).Evaluation.View().Candidate; got == nil || len(*got) != 1 {
|
||||||
|
t.Fatalf("candidate = %v", got)
|
||||||
|
}
|
||||||
|
model, err := registryPreparation([]string{"one", "two"})(ctx)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
evaluation, err := model.(contracts.NativeOperation[[]string]).Interpret(ctx, &promptkit.RunResult{})
|
||||||
|
if err != nil || len(evaluation.(registryEvaluation).omissions) != 1 {
|
||||||
|
t.Fatalf("registry evaluation = %#v, %v", evaluation, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestTextDomainInputsDoNotRequireSceneOrRegistryConventions(t *testing.T) {
|
||||||
|
for _, input := range []contracts.ExtractionInput{
|
||||||
|
{Source: &source.SourceDocument{Kind: "email", Metadata: map[string]any{"subject": "meeting"}}, Chunk: &source.Chunk{Content: []byte("Please meet tomorrow.")}},
|
||||||
|
{Source: &source.SourceDocument{Kind: "forecast-discussion", Metadata: map[string]any{"office": "example"}}, Chunk: &source.Chunk{Content: []byte("A cold front approaches.")}},
|
||||||
|
} {
|
||||||
|
prepare := func(context.Context) (contracts.PreparedOperation[string], error) {
|
||||||
|
text := string(input.Chunk.Content)
|
||||||
|
metadataName := "subject"
|
||||||
|
if input.Source.Kind == "forecast-discussion" {
|
||||||
|
metadataName = "office"
|
||||||
|
}
|
||||||
|
return contracts.NativeOperation[string]{Initial: promptkit.RunRequest{PromptID: input.Source.Kind, Inputs: map[string]promptkit.ArtifactRef{
|
||||||
|
"text": promptkit.Inline(text), metadataName: promptkit.Inline(input.Source.Metadata[metadataName].(string)),
|
||||||
|
}}}, nil
|
||||||
|
}
|
||||||
|
operation, err := prepare(context.Background())
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
request := operation.(contracts.NativeOperation[string]).Initial
|
||||||
|
if request.PromptID != input.Source.Kind || len(request.Inputs) != 2 || request.Inputs["text"] != promptkit.Inline(string(input.Chunk.Content)) {
|
||||||
|
t.Fatalf("request = %#v", request)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user