Adopt registry-backed NPC occurrence artifacts
This commit is contained in:
@@ -29,7 +29,7 @@ type Validator struct {
|
||||
npcResolver *npcregistry.Resolver
|
||||
}
|
||||
|
||||
var _ contracts.TypedValidator[dnd.NPCInteractionList] = (*Validator)(nil)
|
||||
var _ contracts.TypedValidator[dnd.NPCOccurrenceList] = (*Validator)(nil)
|
||||
var _ contracts.ManifestMetadataProvider = (*Validator)(nil)
|
||||
var _ pipeline.CheckpointFingerprintProvider = (*Validator)(nil)
|
||||
|
||||
@@ -72,11 +72,11 @@ func (v *Validator) CheckpointFingerprints() []pipeline.CheckpointFingerprint {
|
||||
}
|
||||
return []pipeline.CheckpointFingerprint{
|
||||
{Name: "policy", Value: policy},
|
||||
{Name: "npc_registry", Value: v.npcResolver.Seeded().ProjectionDigest()},
|
||||
{Name: "npc_registry", Value: v.npcResolver.Seeded().IdentityPromptInput().Digest},
|
||||
}
|
||||
}
|
||||
|
||||
func (v *Validator) Validate(_ context.Context, req contracts.TypedValidationRequest[dnd.NPCInteractionList]) (contracts.ValidationResult, error) {
|
||||
func (v *Validator) Validate(_ context.Context, req contracts.TypedValidationRequest[dnd.NPCOccurrenceList]) (contracts.ValidationResult, error) {
|
||||
if interactionshape.Validate(req.Value) != nil {
|
||||
return contracts.ValidationResult{Approved: true}, nil
|
||||
}
|
||||
@@ -105,25 +105,28 @@ func (v *Validator) Validate(_ context.Context, req contracts.TypedValidationReq
|
||||
}, nil
|
||||
}
|
||||
|
||||
func allSourceRefsValid(index source.DocumentIndex, value dnd.NPCInteractionList) bool {
|
||||
for _, interaction := range value.Interactions {
|
||||
if !interactionmodel.ValidSourceRefs(index, interaction.SourceRefs) {
|
||||
func allSourceRefsValid(index source.DocumentIndex, value dnd.NPCOccurrenceList) bool {
|
||||
for _, occurrence := range value.Occurrences {
|
||||
if !interactionmodel.ValidSourceRefs(index, occurrence.SourceRefs) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func issuesFor(order shared.SourceRefOrder, value dnd.NPCInteractionList, npcRegistry *npcregistry.Registry) []string {
|
||||
func issuesFor(order shared.SourceRefOrder, value dnd.NPCOccurrenceList, npcRegistry *npcregistry.Registry) []string {
|
||||
issues := make([]string, 0)
|
||||
for index, interaction := range value.Interactions {
|
||||
prefix := fmt.Sprintf("interactions[%d]", index)
|
||||
if canonical, ok := npcRegistry.Lookup(interaction.Name); ok && interaction.Name != canonical.Name {
|
||||
issues = append(issues, prefix+".name is not the canonical NPC display name: "+diagnostics.Quote(interaction.Name))
|
||||
for index, occurrence := range value.Occurrences {
|
||||
prefix := fmt.Sprintf("occurrences[%d]", index)
|
||||
canonical, ok := npcRegistry.LookupID(occurrence.NPCID)
|
||||
if !ok {
|
||||
issues = append(issues, prefix+".npc_id is not in the NPC registry: "+diagnostics.Quote(occurrence.NPCID))
|
||||
} else if occurrence.Name != canonical.Name {
|
||||
issues = append(issues, prefix+".name does not match npc_id: "+diagnostics.Quote(occurrence.Name))
|
||||
}
|
||||
for refIndex := 1; refIndex < len(interaction.SourceRefs); refIndex++ {
|
||||
previous := interaction.SourceRefs[refIndex-1]
|
||||
current := interaction.SourceRefs[refIndex]
|
||||
for refIndex := 1; refIndex < len(occurrence.SourceRefs); refIndex++ {
|
||||
previous := occurrence.SourceRefs[refIndex-1]
|
||||
current := occurrence.SourceRefs[refIndex]
|
||||
if order.Less(current, previous) {
|
||||
issues = append(issues, fmt.Sprintf("%s.source_refs are not in canonical order at index %d", prefix, refIndex))
|
||||
} else if current == previous {
|
||||
@@ -132,16 +135,16 @@ func issuesFor(order shared.SourceRefOrder, value dnd.NPCInteractionList, npcReg
|
||||
}
|
||||
}
|
||||
|
||||
if !sort.SliceIsSorted(value.Interactions, func(left, right int) bool {
|
||||
return interactionmodel.Less(order, value.Interactions[left], value.Interactions[right])
|
||||
if !sort.SliceIsSorted(value.Occurrences, func(left, right int) bool {
|
||||
return interactionmodel.Less(order, value.Occurrences[left], value.Occurrences[right])
|
||||
}) {
|
||||
issues = append(issues, "interactions are not in canonical order")
|
||||
issues = append(issues, "occurrences are not in canonical order")
|
||||
}
|
||||
seen := make(map[string]int)
|
||||
for index, interaction := range value.Interactions {
|
||||
key := interactionmodel.ExactIdentity(interaction)
|
||||
for index, occurrence := range value.Occurrences {
|
||||
key := interactionmodel.ExactIdentity(occurrence)
|
||||
if previous, ok := seen[key]; ok {
|
||||
issues = append(issues, fmt.Sprintf("interactions[%d] duplicates interaction %d", index, previous))
|
||||
issues = append(issues, fmt.Sprintf("occurrences[%d] duplicates occurrence %d", index, previous))
|
||||
continue
|
||||
}
|
||||
seen[key] = index
|
||||
@@ -154,7 +157,7 @@ func Spec() pipeline.ValidatorSpec {
|
||||
}
|
||||
|
||||
func Register(registry *pipeline.ValidatorRegistry) error {
|
||||
return pipeline.RegisterTypedValidatorBuilder(registry, dnd.NPCInteractionListKind, Spec(), validateOptions, func(request pipeline.BuildRequest) (contracts.TypedValidator[dnd.NPCInteractionList], error) {
|
||||
return pipeline.RegisterTypedValidatorBuilder(registry, dnd.NPCOccurrenceListKind, Spec(), validateOptions, func(request pipeline.BuildRequest) (contracts.TypedValidator[dnd.NPCOccurrenceList], error) {
|
||||
options, err := DecodeOptions(request.Options)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
@@ -28,26 +28,26 @@ func TestValidatorRejectsOwnedCanonicalNameReferenceOrderListOrderAndDuplicates(
|
||||
references := registryReferences(t, "Aria", "Borin")
|
||||
for _, test := range []struct {
|
||||
name string
|
||||
mutate func(*dnd.NPCInteractionList)
|
||||
mutate func(*dnd.NPCOccurrenceList)
|
||||
want string
|
||||
}{
|
||||
{"canonical name", func(value *dnd.NPCInteractionList) { value.Interactions[0].Name = " aria " }, "canonical NPC display name"},
|
||||
{"reference order", func(value *dnd.NPCInteractionList) {
|
||||
value.Interactions[0].SourceRefs = []source.SourceRef{{SourceID: "session", StartUnitID: 20, EndUnitID: 20}, {SourceID: "session", StartUnitID: 10, EndUnitID: 10}}
|
||||
{"mismatched name", func(value *dnd.NPCOccurrenceList) { value.Occurrences[0].Name = " aria " }, "does not match npc_id"},
|
||||
{"reference order", func(value *dnd.NPCOccurrenceList) {
|
||||
value.Occurrences[0].SourceRefs = []source.SourceRef{{SourceID: "session", StartUnitID: 20, EndUnitID: 20}, {SourceID: "session", StartUnitID: 10, EndUnitID: 10}}
|
||||
}, "not in canonical order"},
|
||||
{"duplicate reference", func(value *dnd.NPCInteractionList) {
|
||||
value.Interactions[0].SourceRefs = append(value.Interactions[0].SourceRefs, value.Interactions[0].SourceRefs[0])
|
||||
{"duplicate reference", func(value *dnd.NPCOccurrenceList) {
|
||||
value.Occurrences[0].SourceRefs = append(value.Occurrences[0].SourceRefs, value.Occurrences[0].SourceRefs[0])
|
||||
}, "duplicates the previous reference"},
|
||||
{"list order", func(value *dnd.NPCInteractionList) {
|
||||
value.Interactions[0], value.Interactions[1] = value.Interactions[1], value.Interactions[0]
|
||||
}, "interactions are not in canonical order"},
|
||||
{"name tie breaker", func(value *dnd.NPCInteractionList) {
|
||||
value.Interactions[0] = dnd.NPCInteraction{Name: "Borin", Kind: dnd.NPCInteractionKindDialogue, SourceRefs: []source.SourceRef{{SourceID: "session", StartUnitID: 10, EndUnitID: 10}}}
|
||||
value.Interactions[1] = dnd.NPCInteraction{Name: "Aria", Kind: dnd.NPCInteractionKindDialogue, SourceRefs: []source.SourceRef{{SourceID: "session", StartUnitID: 10, EndUnitID: 10}}}
|
||||
}, "interactions are not in canonical order"},
|
||||
{"duplicate record", func(value *dnd.NPCInteractionList) {
|
||||
value.Interactions = append(value.Interactions, value.Interactions[0])
|
||||
}, "duplicates interaction"},
|
||||
{"list order", func(value *dnd.NPCOccurrenceList) {
|
||||
value.Occurrences[0], value.Occurrences[1] = value.Occurrences[1], value.Occurrences[0]
|
||||
}, "occurrences are not in canonical order"},
|
||||
{"NPC ID tie breaker", func(value *dnd.NPCOccurrenceList) {
|
||||
value.Occurrences[0] = dnd.NPCOccurrence{NPCID: identity.DeriveID("Aria"), Name: "Aria", Kind: dnd.NPCOccurrenceKindDialogue, SourceRefs: []source.SourceRef{{SourceID: "session", StartUnitID: 10, EndUnitID: 10}}}
|
||||
value.Occurrences[1] = dnd.NPCOccurrence{NPCID: identity.DeriveID("Borin"), Name: "Borin", Kind: dnd.NPCOccurrenceKindDialogue, SourceRefs: []source.SourceRef{{SourceID: "session", StartUnitID: 10, EndUnitID: 10}}}
|
||||
}, "occurrences are not in canonical order"},
|
||||
{"duplicate record", func(value *dnd.NPCOccurrenceList) {
|
||||
value.Occurrences = append(value.Occurrences, value.Occurrences[0])
|
||||
}, "duplicates occurrence"},
|
||||
} {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
value := normalizedList()
|
||||
@@ -62,9 +62,9 @@ func TestValidatorRejectsOwnedCanonicalNameReferenceOrderListOrderAndDuplicates(
|
||||
|
||||
func TestValidatorAcceptsValidEvidenceBeforeInvalidEvidence(t *testing.T) {
|
||||
references := registryReferences(t, "Aria", "Borin")
|
||||
value := dnd.NPCInteractionList{Interactions: []dnd.NPCInteraction{
|
||||
{Name: "Borin", Kind: dnd.NPCInteractionKindMentioned, SourceRefs: []source.SourceRef{{SourceID: "session", StartUnitID: 20, EndUnitID: 20}}},
|
||||
{Name: "Aria", Kind: dnd.NPCInteractionKindDialogue, SourceRefs: []source.SourceRef{{SourceID: "session", StartUnitID: 99, EndUnitID: 99}}},
|
||||
value := dnd.NPCOccurrenceList{Occurrences: []dnd.NPCOccurrence{
|
||||
{NPCID: "npc:test", Name: "Borin", Kind: dnd.NPCOccurrenceKindMentioned, SourceRefs: []source.SourceRef{{SourceID: "session", StartUnitID: 20, EndUnitID: 20}}},
|
||||
{NPCID: "npc:test", Name: "Aria", Kind: dnd.NPCOccurrenceKindDialogue, SourceRefs: []source.SourceRef{{SourceID: "session", StartUnitID: 99, EndUnitID: 99}}},
|
||||
}}
|
||||
result, err := newValidator(t, references).Validate(context.Background(), request(references, value))
|
||||
if err != nil || !result.Approved {
|
||||
@@ -74,9 +74,9 @@ func TestValidatorAcceptsValidEvidenceBeforeInvalidEvidence(t *testing.T) {
|
||||
|
||||
func TestValidatorDefersShapeAndSourceReferenceFailuresAndRequiresRegistry(t *testing.T) {
|
||||
references := registryReferences(t, "Aria", "Borin")
|
||||
for _, value := range []dnd.NPCInteractionList{
|
||||
{Interactions: []dnd.NPCInteraction{{Name: "Aria"}}},
|
||||
{Interactions: []dnd.NPCInteraction{{Name: "Aria", Kind: dnd.NPCInteractionKindDialogue, SourceRefs: []source.SourceRef{{SourceID: "session", StartUnitID: 99, EndUnitID: 99}}}}},
|
||||
for _, value := range []dnd.NPCOccurrenceList{
|
||||
{Occurrences: []dnd.NPCOccurrence{{NPCID: "npc:test", Name: "Aria"}}},
|
||||
{Occurrences: []dnd.NPCOccurrence{{NPCID: "npc:test", Name: "Aria", Kind: dnd.NPCOccurrenceKindDialogue, SourceRefs: []source.SourceRef{{SourceID: "session", StartUnitID: 99, EndUnitID: 99}}}}},
|
||||
} {
|
||||
result, err := newValidator(t, references).Validate(context.Background(), request(references, value))
|
||||
if err != nil || !result.Approved {
|
||||
@@ -126,26 +126,26 @@ func newValidator(t *testing.T, references ...contracts.ReferenceSet) *Validator
|
||||
return validator
|
||||
}
|
||||
|
||||
func request(references contracts.ReferenceSet, value dnd.NPCInteractionList) contracts.TypedValidationRequest[dnd.NPCInteractionList] {
|
||||
return contracts.TypedValidationRequest[dnd.NPCInteractionList]{Source: document(), References: references, Value: value}
|
||||
func request(references contracts.ReferenceSet, value dnd.NPCOccurrenceList) contracts.TypedValidationRequest[dnd.NPCOccurrenceList] {
|
||||
return contracts.TypedValidationRequest[dnd.NPCOccurrenceList]{Source: document(), References: references, Value: value}
|
||||
}
|
||||
|
||||
func document() *source.SourceDocument {
|
||||
return &source.SourceDocument{ID: "session", Units: []source.SourceUnit{{ID: 10}, {ID: 20}}}
|
||||
}
|
||||
|
||||
func normalizedList() dnd.NPCInteractionList {
|
||||
return dnd.NPCInteractionList{Interactions: []dnd.NPCInteraction{
|
||||
{Name: "Aria", Kind: dnd.NPCInteractionKindDialogue, SourceRefs: []source.SourceRef{{SourceID: "session", StartUnitID: 10, EndUnitID: 10}}},
|
||||
{Name: "Borin", Kind: dnd.NPCInteractionKindMentioned, SourceRefs: []source.SourceRef{{SourceID: "session", StartUnitID: 20, EndUnitID: 20}}},
|
||||
func normalizedList() dnd.NPCOccurrenceList {
|
||||
return dnd.NPCOccurrenceList{Occurrences: []dnd.NPCOccurrence{
|
||||
{NPCID: identity.DeriveID("Aria"), Name: "Aria", Kind: dnd.NPCOccurrenceKindDialogue, SourceRefs: []source.SourceRef{{SourceID: "session", StartUnitID: 10, EndUnitID: 10}}},
|
||||
{NPCID: identity.DeriveID("Borin"), Name: "Borin", Kind: dnd.NPCOccurrenceKindMentioned, SourceRefs: []source.SourceRef{{SourceID: "session", StartUnitID: 20, EndUnitID: 20}}},
|
||||
}}
|
||||
}
|
||||
|
||||
func cloneList(value dnd.NPCInteractionList) dnd.NPCInteractionList {
|
||||
copyValue := dnd.NPCInteractionList{Interactions: make([]dnd.NPCInteraction, len(value.Interactions))}
|
||||
for index, interaction := range value.Interactions {
|
||||
copyValue.Interactions[index] = interaction
|
||||
copyValue.Interactions[index].SourceRefs = append([]source.SourceRef(nil), interaction.SourceRefs...)
|
||||
func cloneList(value dnd.NPCOccurrenceList) dnd.NPCOccurrenceList {
|
||||
copyValue := dnd.NPCOccurrenceList{Occurrences: make([]dnd.NPCOccurrence, len(value.Occurrences))}
|
||||
for index, interaction := range value.Occurrences {
|
||||
copyValue.Occurrences[index] = interaction
|
||||
copyValue.Occurrences[index].SourceRefs = append([]source.SourceRef(nil), interaction.SourceRefs...)
|
||||
}
|
||||
return copyValue
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user