Migrate NPC and location occurrences to native correction and explicit empty fallbacks
This commit is contained in:
@@ -2,7 +2,6 @@ package locationoccurrences
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"reflect"
|
||||
"strings"
|
||||
@@ -70,22 +69,19 @@ func TestExtractResolvesContextualSelectorsAndUsesCurrentTranscriptEvidenceOnly(
|
||||
if occurrence := result.Value.Occurrences[0]; occurrence.LocationID != second.ID || occurrence.Name != second.Name || occurrence.SourceRefs[0].SourceID != req.Source.ID {
|
||||
t.Fatalf("occurrence = %#v", occurrence)
|
||||
}
|
||||
input := client.requests[0].Inputs[LocationRegistryReferenceSlot]
|
||||
if input.Name != LocationRegistryReferenceSlot || !strings.Contains(string(input.Content), `"registry_refs":[{"start_unit_id":20,"end_unit_id":20}]`) {
|
||||
t.Fatalf("location prompt input = %#v", input)
|
||||
input := renderedInput(t, client.requests[0], LocationRegistryReferenceSlot)
|
||||
if !strings.Contains(string(input), `"registry_refs":[{"start_unit_id":20,"end_unit_id":20}]`) {
|
||||
t.Fatalf("contextual registry=%s", input)
|
||||
}
|
||||
for _, forbidden := range []string{"source_refs", "source_id", first.ID, second.ID} {
|
||||
if strings.Contains(string(input.Content), forbidden) {
|
||||
t.Fatalf("location prompt leaked %q: %s", forbidden, input.Content)
|
||||
if strings.Contains(string(input), forbidden) {
|
||||
t.Fatalf("location projection exposed %q", forbidden)
|
||||
}
|
||||
}
|
||||
if strings.Contains(string(client.requests[0].Inputs["transcript"].Content), "other-session") {
|
||||
t.Fatal("transcript input contains registry evidence")
|
||||
}
|
||||
metadata, err := json.Marshal(newExtractor(t, &fakeOccurrencesLLMClient{}, references).ManifestMetadata())
|
||||
if err != nil || strings.Contains(string(metadata), "other-session") || strings.Contains(string(metadata), first.ID) {
|
||||
t.Fatalf("manifest metadata = %s, %v", metadata, err)
|
||||
if strings.Contains(string(renderedInput(t, client.requests[0], "transcript")), "other-session") {
|
||||
t.Fatal("registry provenance became occurrence evidence")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestExtractRejectsUnknownMalformedOrMismatchedSelectorsAtomically(t *testing.T) {
|
||||
@@ -101,7 +97,7 @@ func TestExtractRejectsUnknownMalformedOrMismatchedSelectorsAtomically(t *testin
|
||||
{{Name: known.Name, RegistryRefs: []locationregistry.RegistryRef{}, Kind: "mentioned", SourceRefs: occurrenceRefs(10, 10)}, {Name: "Unknown", RegistryRefs: []locationregistry.RegistryRef{}, Kind: "mentioned", SourceRefs: occurrenceRefs(20, 20)}},
|
||||
} {
|
||||
client := &fakeOccurrencesLLMClient{response: extractionResponse{Occurrences: occurrences}}
|
||||
if result, err := newExtractor(t, client, references).Extract(context.Background(), req); err == nil || result.Value.Occurrences != nil || !strings.Contains(err.Error(), "location selector") {
|
||||
if result, err := newExtractor(t, client, references).Extract(context.Background(), req); err == nil || result.Value.Occurrences != nil {
|
||||
t.Fatalf("Extract() = %#v, %v", result, err)
|
||||
}
|
||||
}
|
||||
@@ -125,7 +121,7 @@ func TestExtractRejectsUnknownMalformedOrMismatchedSelectorsAtomically(t *testin
|
||||
client := &fakeOccurrencesLLMClient{response: extractionResponse{Occurrences: []occurrenceResponse{{Name: selector.name, RegistryRefs: selector.refs, Kind: "mentioned", SourceRefs: occurrenceRefs(10, 10)}}}}
|
||||
duplicateRequest := extractionRequest()
|
||||
duplicateRequest.References = duplicateReferences
|
||||
if _, err := newExtractor(t, client, duplicateReferences).Extract(context.Background(), duplicateRequest); err == nil || !strings.Contains(err.Error(), "location selector") {
|
||||
if result, err := newExtractor(t, client, duplicateReferences).Extract(context.Background(), duplicateRequest); err == nil || result.Outcome.Disposition != "" || len(result.Value.Occurrences) != 0 {
|
||||
t.Fatalf("Extract(%#v) error = %v", selector, err)
|
||||
}
|
||||
}
|
||||
@@ -163,28 +159,25 @@ func TestExtractResolvesGeneratedRegistryAtOperationTimeAndDoesNotMutateResponse
|
||||
if err != nil || result.Value.Occurrences[0].Name != "The Mill" {
|
||||
t.Fatalf("Extract() = %#v, %v", result, err)
|
||||
}
|
||||
if input := client.requests[0].Inputs[LocationRegistryReferenceSlot]; strings.Contains(string(input.Content), location.ID) || input.OriginURI != "" {
|
||||
t.Fatalf("generated registry prompt input = %#v", input)
|
||||
}
|
||||
if _, ok := extractor.ManifestMetadata()["location_registry_digest"]; ok {
|
||||
t.Fatalf("operation registry leaked into static metadata: %#v", extractor.ManifestMetadata())
|
||||
if strings.Contains(string(renderedInput(t, client.requests[0], LocationRegistryReferenceSlot)), location.ID) {
|
||||
t.Fatal("generated registry exposed a durable ID")
|
||||
}
|
||||
if client.response.Occurrences[0].SourceRefs[0].StartUnitID != 30 {
|
||||
t.Fatalf("model response mutated: %#v", client.response)
|
||||
}
|
||||
}
|
||||
|
||||
func TestExtractorContractsMetadataAndFailures(t *testing.T) {
|
||||
if _, err := New(nil, Options{}); err == nil || !strings.Contains(err.Error(), "LLM client") {
|
||||
func TestExtractorContractsDependenciesAndFailures(t *testing.T) {
|
||||
if _, err := New(nil, Options{}); err == nil || !strings.Contains(err.Error(), "native engine") {
|
||||
t.Fatalf("New(nil) error = %v", err)
|
||||
}
|
||||
if _, err := New(&fakeOccurrencesLLMClient{}, Options{}, contracts.ReferenceSet{}, contracts.ReferenceSet{}); err == nil || !strings.Contains(err.Error(), "at most one reference set") {
|
||||
if _, err := New(newExtractor(t, &fakeOccurrencesLLMClient{}).engine, Options{}, contracts.ReferenceSet{}, contracts.ReferenceSet{}); err == nil || !strings.Contains(err.Error(), "at most one reference set") {
|
||||
t.Fatalf("New() error = %v", err)
|
||||
}
|
||||
malformed := contracts.ReferenceSet{Slots: map[string]contracts.ResolvedReferenceSlot{LocationRegistryReferenceSlot: {
|
||||
Items: []contracts.ReferenceItem{{SlotName: LocationRegistryReferenceSlot, MediaType: "application/json", Content: []byte(`{"secret":"registry evidence"}`)}},
|
||||
}}}
|
||||
if _, err := New(&fakeOccurrencesLLMClient{}, Options{}, malformed); err == nil || !strings.Contains(err.Error(), "prepare location registry") || strings.Contains(err.Error(), "registry evidence") {
|
||||
if _, err := New(newExtractor(t, &fakeOccurrencesLLMClient{}).engine, Options{}, malformed); err == nil || !strings.Contains(err.Error(), "prepare location registry") || strings.Contains(err.Error(), "registry evidence") {
|
||||
t.Fatalf("New() error = %v", err)
|
||||
}
|
||||
|
||||
@@ -197,11 +190,11 @@ func TestExtractorContractsMetadataAndFailures(t *testing.T) {
|
||||
for _, test := range []struct {
|
||||
name string
|
||||
extractor *Extractor
|
||||
req contracts.TypedExtractionRequest
|
||||
req contracts.TypedExtractionRequest[dnd.LocationOccurrenceList]
|
||||
want string
|
||||
}{
|
||||
{"nil extractor", nilExtractor, req, "extractor"},
|
||||
{"nil client", &Extractor{}, req, "LLM client"},
|
||||
{"nil client", &Extractor{}, req, "native engine"},
|
||||
{"invalid request", extractor, mismatchedSourceInputRequest(req), "must match chunk"},
|
||||
} {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
@@ -215,7 +208,7 @@ func TestExtractorContractsMetadataAndFailures(t *testing.T) {
|
||||
}
|
||||
|
||||
spec := ModuleSpec()
|
||||
if spec.Key != Key || spec.Stage != pipeline.StageExtract || spec.ExecutionClass != contracts.ExecutionClassLLMBacked || spec.CorrectionProtocol != contracts.CorrectionProtocolSingleResponseV1 || spec.ArtifactKind != dnd.LocationOccurrenceListKind {
|
||||
if spec.Key != Key || spec.Stage != pipeline.StageExtract || spec.ExecutionClass != contracts.ExecutionClassLLMBacked || !reflect.DeepEqual(spec.Prompts, []string{PromptID, CorrectionPromptID}) || spec.ArtifactKind != dnd.LocationOccurrenceListKind {
|
||||
t.Fatalf("ModuleSpec() = %#v", spec)
|
||||
}
|
||||
var slot contracts.ReferenceSlot
|
||||
@@ -237,24 +230,6 @@ func TestExtractorContractsMetadataAndFailures(t *testing.T) {
|
||||
if _, err := DecodeOptions(map[string]any{"unexpected": true}); err == nil {
|
||||
t.Fatal("DecodeOptions() accepted unknown options")
|
||||
}
|
||||
metadata := newExtractor(t, &fakeOccurrencesLLMClient{}, references).ManifestMetadata()
|
||||
for _, key := range []string{"prompt_sha256", "response_schema_sha256", "location_registry_digest"} {
|
||||
if value, ok := metadata[key].(string); !ok || !strings.HasPrefix(value, "sha256:") {
|
||||
t.Fatalf("metadata[%q] = %#v", key, metadata[key])
|
||||
}
|
||||
}
|
||||
if got := newExtractor(t, &fakeOccurrencesLLMClient{}, references).CheckpointFingerprints(); len(got) != 4 || got[3].Name != "location_registry" || got[3].Value != locationRegistryIdentityDigest(t, references) {
|
||||
t.Fatalf("fingerprints = %#v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func locationRegistryIdentityDigest(t *testing.T, references contracts.ReferenceSet) string {
|
||||
t.Helper()
|
||||
resolver, err := locationregistry.NewResolver(references)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return resolver.Seeded().IdentityDigest()
|
||||
}
|
||||
|
||||
func locationRegistry(t *testing.T, names ...string) dnd.LocationRegistry {
|
||||
|
||||
Reference in New Issue
Block a user