Prepare shared reconciliation operations before evaluation migration
This commit is contained in:
@@ -599,6 +599,11 @@ for repetitive signature/adaptor work, not five new domain algorithms.
|
||||
|
||||
## Stage 15 — Build Shared Reconciliation Preparation
|
||||
|
||||
**Status:** Complete. Synthetic preparation and core pipeline tests pass.
|
||||
The contracts package's legacy fixture implementations do not compile after
|
||||
the interface migration and remain deferred to the cutover; proposal evaluation
|
||||
and production adapters remain scheduled below.
|
||||
|
||||
**Goal:** Give families one typed entry into reconciliation without execution glue.
|
||||
|
||||
**Inspect:** `semanticreconcile/{preparation,requests,application,proposal}.go`,
|
||||
|
||||
@@ -63,6 +63,7 @@ type DeterministicOperation[T any] struct {
|
||||
Evaluation Evaluation[T]
|
||||
Fallback Fallback[T]
|
||||
Diagnostics TerminalDiagnostics[T]
|
||||
Evidence func() (any, error)
|
||||
}
|
||||
|
||||
func (DeterministicOperation[T]) preparedOperation() {}
|
||||
|
||||
@@ -68,11 +68,17 @@ func ExecuteOperation[T any](ctx context.Context, execution ExecutionContext[T],
|
||||
}
|
||||
switch prepared := operation.(type) {
|
||||
case contracts.DeterministicOperation[T]:
|
||||
if err := recordDomainEvidence(execution.Execution, "preparation-domain.json", prepared.Evidence); err != nil {
|
||||
return empty, err
|
||||
}
|
||||
return executeDeterministic(ctx, execution, prepared)
|
||||
case *contracts.DeterministicOperation[T]:
|
||||
if prepared == nil {
|
||||
return empty, errors.New("deterministic operation must not be nil")
|
||||
}
|
||||
if err := recordDomainEvidence(execution.Execution, "preparation-domain.json", prepared.Evidence); err != nil {
|
||||
return empty, err
|
||||
}
|
||||
return executeDeterministic(ctx, execution, *prepared)
|
||||
case contracts.PreparationLimit[T]:
|
||||
if err := recordDomainEvidence(execution.Execution, "preparation-domain.json", prepared.Evidence); err != nil {
|
||||
|
||||
92
internal/framework/semanticreconcile/operation.go
Normal file
92
internal/framework/semanticreconcile/operation.go
Normal file
@@ -0,0 +1,92 @@
|
||||
package semanticreconcile
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/core/source"
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
|
||||
"go.maximumdirect.net/promptkit"
|
||||
)
|
||||
|
||||
// OperationInput describes one reconciliation over fixed, already owned
|
||||
// records. Original is the unprocessed merged value used by fallback;
|
||||
// Deterministic is the separately preprocessed no-model candidate. Callers
|
||||
// retain responsibility for cloning mutable values captured by callbacks.
|
||||
type OperationInput[T, R any] struct {
|
||||
Source *source.SourceDocument
|
||||
Original T
|
||||
Deterministic T
|
||||
Records []R
|
||||
Limits Limits
|
||||
PromptID string
|
||||
CorrectionID string
|
||||
ProfileID string
|
||||
SessionID string
|
||||
Project func([]R) ([]Candidate, error)
|
||||
Interpret func(context.Context, Preparation, *promptkit.RunResult) (contracts.Evaluation[T], error)
|
||||
Fallback func(context.Context, T, contracts.Evaluation[T]) (T, error)
|
||||
Diagnostics contracts.TerminalDiagnostics[T]
|
||||
BaseDiagnostics []contracts.ProducerDiagnostic
|
||||
}
|
||||
|
||||
// PrepareOperation selects the deterministic, limited, or native operation;
|
||||
// it never runs a model, assesses a value, or chooses a terminal disposition.
|
||||
func PrepareOperation[T, R any](input OperationInput[T, R]) (contracts.PreparedOperation[T], error) {
|
||||
var candidates []Candidate
|
||||
var preparation Preparation
|
||||
evidence := func() (any, error) {
|
||||
return map[string]any{"originals": input.Deterministic, "disposition": preparation.Disposition(), "candidate_number_range": preparation.CandidateNumberRange(), "candidate_mappings": preparation.CandidateMappings(), "candidates": candidates}, nil
|
||||
}
|
||||
var fallback contracts.Fallback[T]
|
||||
if input.Fallback != nil {
|
||||
fallback = func(ctx context.Context, evaluation contracts.Evaluation[T]) (T, error) {
|
||||
return input.Fallback(ctx, input.Original, evaluation)
|
||||
}
|
||||
}
|
||||
noModel := func() contracts.PreparedOperation[T] {
|
||||
value := input.Deterministic
|
||||
var originalFallback contracts.Fallback[T]
|
||||
if fallback != nil {
|
||||
originalFallback = func(ctx context.Context, _ contracts.Evaluation[T]) (T, error) { return fallback(ctx, nil) }
|
||||
}
|
||||
return contracts.DeterministicOperation[T]{Evaluation: contracts.CandidateEvaluation[T]{Result: contracts.EvaluationView[T]{Candidate: &value, Diagnostics: input.BaseDiagnostics}}, Fallback: originalFallback, Diagnostics: input.Diagnostics, Evidence: evidence}
|
||||
}
|
||||
if len(input.Records) < 2 {
|
||||
return noModel(), nil
|
||||
}
|
||||
if input.Project == nil {
|
||||
return nil, errors.New("semantic reconciliation candidate projection must be supplied")
|
||||
}
|
||||
var err error
|
||||
candidates, err = input.Project(input.Records)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
preparation, err = Prepare(input.Source, candidates, input.Limits)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
switch preparation.Disposition() {
|
||||
case InsufficientCandidates:
|
||||
return noModel(), nil
|
||||
case LimitExceeded:
|
||||
return contracts.PreparationLimit[T]{Cause: errors.New("candidate set exceeded semantic reconciliation preparation limits"), Fallback: fallback, Diagnostics: input.Diagnostics, Evidence: evidence}, nil
|
||||
}
|
||||
initial, err := InitialRequest(preparation, input.PromptID, input.ProfileID, input.SessionID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if input.Interpret == nil {
|
||||
return nil, errors.New("semantic reconciliation interpretation must be supplied")
|
||||
}
|
||||
return contracts.NativeOperation[T]{Initial: initial,
|
||||
Interpret: func(ctx context.Context, result *promptkit.RunResult) (contracts.Evaluation[T], error) {
|
||||
return input.Interpret(ctx, preparation, result)
|
||||
},
|
||||
Correction: func(_ context.Context, guidance []string) (promptkit.RunRequest, error) {
|
||||
return CorrectionRequest(input.CorrectionID, input.ProfileID, input.SessionID, guidance), nil
|
||||
},
|
||||
Fallback: fallback, Diagnostics: input.Diagnostics, Evidence: evidence,
|
||||
}, nil
|
||||
}
|
||||
78
internal/framework/semanticreconcile/operation_test.go
Normal file
78
internal/framework/semanticreconcile/operation_test.go
Normal file
@@ -0,0 +1,78 @@
|
||||
package semanticreconcile
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/core/source"
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/framework/pipeline"
|
||||
"go.maximumdirect.net/promptkit"
|
||||
)
|
||||
|
||||
func TestPrepareOperationSelectsModelOrDeterministicWork(t *testing.T) {
|
||||
doc := &source.SourceDocument{ID: "document", Units: []source.SourceUnit{{ID: 1, Text: "Alpha"}, {ID: 2, Text: "Beta"}}}
|
||||
records := []string{"Alpha", "Beta"}
|
||||
input := OperationInput[[]string, string]{Source: doc, Original: []string{"original"}, Deterministic: []string{"preprocessed"}, Records: records, Limits: DefaultLimits(), PromptID: "initial", CorrectionID: "correct", ProfileID: "profile", SessionID: "session",
|
||||
Project: func(records []string) ([]Candidate, error) {
|
||||
return []Candidate{{Label: records[0], SourceRefs: []source.SourceRef{{SourceID: doc.ID, StartUnitID: 1, EndUnitID: 1}}}, {Label: records[1], SourceRefs: []source.SourceRef{{SourceID: doc.ID, StartUnitID: 2, EndUnitID: 2}}}}, nil
|
||||
},
|
||||
Interpret: func(context.Context, Preparation, *promptkit.RunResult) (contracts.Evaluation[[]string], error) {
|
||||
return nil, nil
|
||||
},
|
||||
Fallback: func(_ context.Context, original []string, _ contracts.Evaluation[[]string]) ([]string, error) {
|
||||
return original, nil
|
||||
},
|
||||
}
|
||||
operation, err := PrepareOperation(input)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
native, ok := operation.(contracts.NativeOperation[[]string])
|
||||
if !ok || native.Initial.PromptID != "initial" || native.Initial.ProfileID != "profile" || native.Initial.SessionID != "session" {
|
||||
t.Fatalf("native operation = %#v", operation)
|
||||
}
|
||||
correction, err := native.Correction(context.Background(), []string{"repair"})
|
||||
if err != nil || correction.PromptID != "correct" {
|
||||
t.Fatalf("correction = %#v, error = %v", correction, err)
|
||||
}
|
||||
|
||||
input.Records = records[:1]
|
||||
input.Project = func([]string) ([]Candidate, error) {
|
||||
t.Fatal("single record should not require projection")
|
||||
return nil, nil
|
||||
}
|
||||
operation, err = PrepareOperation(input)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
assessments := 0
|
||||
result, err := pipeline.ExecuteOperation(context.Background(), pipeline.ExecutionContext[[]string]{Controls: contracts.ProducerControls{Mode: contracts.ModePermissive}, Assess: func(context.Context, []string) (contracts.CandidateAssessment, error) {
|
||||
assessments++
|
||||
return contracts.CandidateAssessment{Validators: []contracts.ValidatorAssessment{{ValidatorName: "rule", Decision: contracts.AssessmentRejected}}}, nil
|
||||
}}, func(context.Context) (contracts.PreparedOperation[[]string], error) { return operation, nil })
|
||||
if err != nil || assessments != 1 || len(result.Value) != 1 || result.Value[0] != "original" || result.Outcome.Disposition != contracts.ProducerFallback {
|
||||
t.Fatalf("result = %#v, assessments = %d, error = %v", result, assessments, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrepareOperationPropagatesProjectionErrorsAndLimits(t *testing.T) {
|
||||
bad := errors.New("projection failed")
|
||||
input := OperationInput[int, int]{Records: []int{1, 2}, Limits: DefaultLimits(), Project: func([]int) ([]Candidate, error) { return nil, bad }}
|
||||
if _, err := PrepareOperation(input); !errors.Is(err, bad) {
|
||||
t.Fatalf("projection error = %v", err)
|
||||
}
|
||||
input.Source = &source.SourceDocument{ID: "doc", Units: []source.SourceUnit{{ID: 1}, {ID: 2}}}
|
||||
input.Project = func([]int) ([]Candidate, error) {
|
||||
return []Candidate{{SourceRefs: []source.SourceRef{{SourceID: "doc", StartUnitID: 1, EndUnitID: 1}}}, {SourceRefs: []source.SourceRef{{SourceID: "doc", StartUnitID: 2, EndUnitID: 2}}}}, nil
|
||||
}
|
||||
input.Limits.MaximumCandidates = 1
|
||||
operation, err := PrepareOperation(input)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, ok := operation.(contracts.PreparationLimit[int]); !ok {
|
||||
t.Fatalf("operation = %T, want preparation limit", operation)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user