Migrate NPC and location normalization to native correction and explicit fallback handling

This commit is contained in:
2026-11-22 00:27:34 +00:00
parent 772b8619c1
commit 4f82d01534
16 changed files with 1062 additions and 732 deletions

View File

@@ -3,54 +3,110 @@ package locationregistry
import (
"context"
"encoding/json"
"fmt"
"strings"
"testing"
"gitea.maximumdirect.net/eric/notarius/assets"
"go.maximumdirect.net/configkit"
"go.maximumdirect.net/promptkit"
"gitea.maximumdirect.net/eric/notarius/internal/core/source"
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd"
)
type recordingLocationNormalizerClient struct {
response string
err error
requests []contracts.StructuredCompletionRequest
response string
responses []string
err error
errors []error
requests []promptkit.GenerateRequest
onGenerate func(context.Context, promptkit.GenerateRequest)
}
func (c *recordingLocationNormalizerClient) CompleteStructured(_ context.Context, request contracts.StructuredCompletionRequest, output any) (contracts.StructuredCompletionResponse, error) {
snapshot, err := contracts.CloneStructuredCompletionRequest(request)
if err != nil {
return contracts.StructuredCompletionResponse{}, fmt.Errorf("clone recording request: %w", err)
func (c *recordingLocationNormalizerClient) Generate(ctx context.Context, request promptkit.GenerateRequest) (*promptkit.GenerateResponse, error) {
index := len(c.requests)
c.requests = append(c.requests, request)
if c.onGenerate != nil {
c.onGenerate(ctx, request)
}
c.requests = append(c.requests, snapshot)
if c.err != nil {
return contracts.StructuredCompletionResponse{}, c.err
return nil, c.err
}
if index < len(c.errors) && c.errors[index] != nil {
return nil, c.errors[index]
}
response := c.response
if index < len(c.responses) {
response = c.responses[index]
}
if response == "" {
response = `{"duplicate_groups":[]}`
}
content := []byte(response)
if err := json.Unmarshal(content, output); err != nil {
return contracts.StructuredCompletionResponse{}, err
}
return contracts.StructuredCompletionResponse{Content: content}, nil
return &promptkit.GenerateResponse{Content: response}, nil
}
func newNormalizer(t *testing.T, client contracts.StructuredLLMClient) *Normalizer {
func newNormalizer(t *testing.T, client promptkit.LLMClient) *Normalizer {
t.Helper()
normalizer, err := New(client, Options{})
type settings struct {
PromptKit promptkit.Settings `yaml:"promptkit"`
}
definition := promptkit.Configure(configkit.Definition[settings]{}, func(value *settings) *promptkit.Settings { return &value.PromptKit })
filesystem := configkit.NewFilesystem("Notarius assets", assets.FS())
result, err := configkit.LoadLayered(configkit.Input{Source: configkit.Bytes("normalizer fixture", []byte(`promptkit:
profiles:
- id: location-normalize-test
endpoint: https://example.invalid/v1
model: normalization-test-model
api_key_required: false
`))}, configkit.LayeredOptions[settings]{Definition: definition, Underlays: []configkit.Underlay{
promptkit.BundledOpenRouterUnderlay(),
{Inputs: []configkit.Input{{Source: filesystem.File("notarius.yml"), Imports: true, Filesystem: &configkit.FilesystemContext{FS: &filesystem, Root: "."}}}},
}})
if err != nil {
t.Fatalf("New() error = %v", err)
t.Fatal(err)
}
engine, err := promptkit.NewFromConfig(result, promptkit.RuntimeConfig{}, promptkit.WithLLMClient(client))
if err != nil {
t.Fatal(err)
}
normalizer, err := New(engine, Options{})
if err != nil {
t.Fatal(err)
}
return normalizer
}
func renderedInput(t *testing.T, request promptkit.GenerateRequest, name string) []byte {
t.Helper()
key := "candidate_number_range"
if name == "transcript" {
key = "windows"
}
for _, message := range request.Prompt.Messages {
start := strings.Index(message.Content, "{")
if start < 0 {
continue
}
content := []byte(message.Content[start:])
var fields map[string]json.RawMessage
if json.Unmarshal(content, &fields) == nil && fields[key] != nil {
return content
}
}
t.Fatalf("missing rendered %s input", name)
return nil
}
func normalizeRequest(value dnd.LocationRegistry) contracts.TypedNormalizeRequest[dnd.LocationRegistry] {
return contracts.TypedNormalizeRequest[dnd.LocationRegistry]{
Source: &source.SourceDocument{},
Source: &source.SourceDocument{}, LLMProfile: "location-normalize-test",
Operation: contracts.ProducerOperation[dnd.LocationRegistry]{Controls: contracts.ProducerControls{Mode: contracts.ModeStrict, Cache: contracts.CacheBypass}, ReuseEligible: true, Assess: func(context.Context, dnd.LocationRegistry) (contracts.CandidateAssessment, error) {
return contracts.CandidateAssessment{}, nil
}},
MergeOutput: contracts.MergeArtifact[dnd.LocationRegistry]{Value: value},
}
}
func normalizeRequestWithSource(value dnd.LocationRegistry, doc *source.SourceDocument) contracts.TypedNormalizeRequest[dnd.LocationRegistry] {
request := normalizeRequest(value)
request.Source = doc