Migrate item occurrences to native correction while preserving holder semantics
This commit is contained in:
@@ -8,20 +8,14 @@ import (
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd"
|
||||
itemidentity "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/items/identity"
|
||||
itemoccurrenceshape "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/validate/itemoccurrences/shape"
|
||||
)
|
||||
|
||||
func TestExtractGroundsOccurrencesInRequiredRegistry(t *testing.T) {
|
||||
id := itemidentity.DeriveID("Torch")
|
||||
rawResponse := []byte(`{"occurrences":[{"name":"Torch","kind":"lost","from":"party","source_refs":[{"start_unit_id":2,"end_unit_id":1}]}]}`)
|
||||
rawResponse := []byte(`{"occurrences":[{"name":"Torch","kind":"lost","quantity":null,"from":"party","to":null,"source_refs":[{"start_unit_id":2,"end_unit_id":1}]}]}`)
|
||||
client := &fakeItemOccurrencesLLMClient{content: append([]byte(nil), rawResponse...)}
|
||||
req := extractionRequest()
|
||||
req.References = itemRegistryReferences(t)
|
||||
correction, err := contracts.NewSemanticCorrection([]byte(`{"occurrences":[]}`), "Keep the transcript-grounded item occurrence.")
|
||||
if err != nil {
|
||||
t.Fatalf("NewSemanticCorrection() error = %v", err)
|
||||
}
|
||||
req.Correction = correction
|
||||
result, err := newExtractor(t, client, req.References).Extract(context.Background(), req)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -33,28 +27,18 @@ func TestExtractGroundsOccurrencesInRequiredRegistry(t *testing.T) {
|
||||
t.Fatalf("occurrence evidence = %#v, want current-source unit range", refs)
|
||||
}
|
||||
request := client.requests[0]
|
||||
input := request.Inputs[ItemRegistryReferenceSlot]
|
||||
if input.Name != ItemRegistryReferenceSlot || string(input.Content) != `{"items":[{"name":"Torch"}]}` || strings.Contains(string(input.Content), "item:sha256:") {
|
||||
t.Fatalf("registry prompt input = %#v, want names-only projection", input)
|
||||
|
||||
input := renderedInput(t, request, ItemRegistryReferenceSlot)
|
||||
if string(input) != `{"items":[{"name":"Torch"}]}` {
|
||||
t.Fatalf("names-only registry = %s", input)
|
||||
}
|
||||
if request.Correction == nil || string(request.Correction.AssistantResponse) != `{"occurrences":[]}` || request.Correction.UserGuidance != "Keep the transcript-grounded item occurrence." {
|
||||
t.Fatalf("correction = %#v, want exact request correction", request.Correction)
|
||||
}
|
||||
for _, material := range []string{string(request.Correction.AssistantResponse), request.Correction.UserGuidance} {
|
||||
if strings.Contains(material, id) || strings.Contains(material, "item:sha256:") {
|
||||
t.Fatalf("correction material leaked opaque item identity: %q", material)
|
||||
for _, message := range request.Prompt.Messages {
|
||||
if strings.Contains(message.Content, id) || strings.Contains(message.Content, "item:sha256:") {
|
||||
t.Fatal("model context exposed durable item identity")
|
||||
}
|
||||
}
|
||||
correction.AssistantResponse[0] = '['
|
||||
if got := string(request.Correction.AssistantResponse); got != `{"occurrences":[]}` {
|
||||
t.Fatalf("captured correction changed after caller mutation: %q", got)
|
||||
}
|
||||
if result.ModelCandidate == nil || result.ModelCandidate.Protocol != contracts.CorrectionProtocolSingleResponseV1 || string(result.ModelCandidate.Response) != string(rawResponse) {
|
||||
t.Fatalf("model candidate = %#v, want exact validated response", result.ModelCandidate)
|
||||
}
|
||||
client.content[0] = '['
|
||||
if got := string(result.ModelCandidate.Response); got != string(rawResponse) {
|
||||
t.Fatalf("model candidate changed after provider buffer mutation: %q", got)
|
||||
if request.Prompt.SessionID != req.SessionID || result.Outcome.Disposition != contracts.ProducerAccepted {
|
||||
t.Fatal("native selection or acceptance lost")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -83,8 +67,8 @@ func TestExtractRejectsUnknownNameAfterValidOccurrence(t *testing.T) {
|
||||
req := extractionRequest()
|
||||
req.References = itemRegistryReferences(t)
|
||||
result, err := newExtractor(t, client, req.References).Extract(context.Background(), req)
|
||||
if err == nil || !strings.Contains(err.Error(), "occurrences[1].name is not in the item registry") {
|
||||
t.Fatalf("Extract() error = %v, want occurrence index and unknown name", err)
|
||||
if err == nil || result.Outcome.Disposition != "" {
|
||||
t.Fatalf("Extract() error = %v, want atomic unknown-name failure", err)
|
||||
}
|
||||
if len(result.Value.Occurrences) != 0 {
|
||||
t.Fatalf("Extract() returned partial occurrences: %#v", result.Value.Occurrences)
|
||||
@@ -114,12 +98,12 @@ func TestExtractAcceptsOnlyEmptyResponseForEmptyRegistry(t *testing.T) {
|
||||
req.References = references
|
||||
result, err := newExtractor(t, client, references).Extract(context.Background(), req)
|
||||
if test.wantErr {
|
||||
if err == nil || !strings.Contains(err.Error(), "name is not in the item registry") || len(result.Value.Occurrences) != 0 {
|
||||
if err == nil || result.Outcome.Disposition != "" || len(result.Value.Occurrences) != 0 {
|
||||
t.Fatalf("Extract() = %#v, %v; want no accepted occurrences", result, err)
|
||||
}
|
||||
return
|
||||
}
|
||||
if err != nil || result.Value.Occurrences == nil || len(result.Value.Occurrences) != 0 {
|
||||
if err != nil || result.Value.Occurrences == nil || len(result.Value.Occurrences) != 0 || result.Outcome.Disposition != contracts.ProducerAccepted || !result.Outcome.ReuseEligible {
|
||||
t.Fatalf("Extract() = %#v, %v; want present empty occurrences", result, err)
|
||||
}
|
||||
})
|
||||
@@ -144,50 +128,9 @@ func TestExtractUsesOnlySupportedPromptInputs(t *testing.T) {
|
||||
if _, err := newExtractor(t, client, req.References).Extract(context.Background(), req); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, ok := client.requests[0].Inputs["unrelated"]; ok {
|
||||
t.Fatalf("unexpected prompt input: %#v", client.requests[0].Inputs)
|
||||
}
|
||||
}
|
||||
|
||||
func TestExtractCarriesRejectedHolderGuidanceIntoCorrectedAttempt(t *testing.T) {
|
||||
references := itemRegistryReferences(t)
|
||||
req := extractionRequest()
|
||||
req.References = references
|
||||
defectiveResponse := []byte(`{"occurrences":[{"name":"Torch","kind":"acquired","quantity":null,"from":"Chest","to":"party","source_refs":[{"start_unit_id":2,"end_unit_id":2}]}]}`)
|
||||
defective, err := newExtractor(t, &fakeItemOccurrencesLLMClient{content: defectiveResponse}, references).Extract(context.Background(), req)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
validation, err := itemoccurrenceshape.New(itemoccurrenceshape.Options{}).Validate(context.Background(), contracts.TypedValidationRequest[dnd.ItemOccurrenceList]{Value: defective.Value})
|
||||
if err != nil || validation.Approved {
|
||||
t.Fatalf("holder validation = %#v, %v; want rejection", validation, err)
|
||||
}
|
||||
for _, fragment := range []string{"For `acquired` occurrences", "`from` to JSON null", "item \"Torch\"", "source unit 2"} {
|
||||
if !strings.Contains(validation.CorrectionGuidance, fragment) {
|
||||
t.Fatalf("CorrectionGuidance = %q, want %q", validation.CorrectionGuidance, fragment)
|
||||
for _, message := range client.requests[0].Prompt.Messages {
|
||||
if strings.Contains(message.Content, "ignored") {
|
||||
t.Fatal("unsupported reference became a prompt input")
|
||||
}
|
||||
}
|
||||
for _, forbidden := range []string{defective.Value.Occurrences[0].ItemID, "item_id", "sha256", itemoccurrenceshape.Key, itemoccurrenceshape.ReasonCode} {
|
||||
if strings.Contains(validation.CorrectionGuidance, forbidden) {
|
||||
t.Fatalf("CorrectionGuidance leaked implementation identifier %q: %q", forbidden, validation.CorrectionGuidance)
|
||||
}
|
||||
}
|
||||
correction, err := contracts.NewSemanticCorrection(defective.ModelCandidate.Response, validation.CorrectionGuidance)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
correctedResponse := []byte(`{"occurrences":[{"name":"Torch","kind":"acquired","quantity":null,"from":null,"to":"party","source_refs":[{"start_unit_id":2,"end_unit_id":2}]}]}`)
|
||||
client := &fakeItemOccurrencesLLMClient{content: correctedResponse}
|
||||
req.Correction = correction
|
||||
corrected, err := newExtractor(t, client, references).Extract(context.Background(), req)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
accepted, err := itemoccurrenceshape.New(itemoccurrenceshape.Options{}).Validate(context.Background(), contracts.TypedValidationRequest[dnd.ItemOccurrenceList]{Value: corrected.Value})
|
||||
if err != nil || !accepted.Approved {
|
||||
t.Fatalf("corrected holder validation = %#v, %v; want approval", accepted, err)
|
||||
}
|
||||
if len(client.requests) != 1 || client.requests[0].Correction == nil || string(client.requests[0].Correction.AssistantResponse) != string(defectiveResponse) || client.requests[0].Correction.UserGuidance != validation.CorrectionGuidance {
|
||||
t.Fatalf("corrected request = %#v, want byte-faithful defective response and semantic guidance", client.requests)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user