Bound D&D normalization diagnostics
This commit is contained in:
@@ -16,8 +16,12 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
Key = "dnd/scene-descriptions"
|
||||
normalizerPolicy = "dnd.scene_descriptions.normalizer.v1"
|
||||
Key = "dnd/scene-descriptions"
|
||||
normalizerPolicy = "dnd.scene_descriptions.normalizer.v1"
|
||||
ReasonCodeProseNormalized = "scene_description_prose_normalized"
|
||||
ReasonCodeOrderNormalized = "scene_description_order_normalized"
|
||||
ReasonCodeDuplicateCollapsed = "scene_description_duplicate_collapsed"
|
||||
ReasonCodeNormalizationWarningsOmitted = "scene_description_normalization_warnings_omitted"
|
||||
)
|
||||
|
||||
var requiredCapabilities = []string{"merged"}
|
||||
@@ -60,70 +64,106 @@ func (n *Normalizer) Normalize(ctx context.Context, req contracts.TypedNormalize
|
||||
if err := ctx.Err(); err != nil {
|
||||
return contracts.TypedNormalizeResult[dnd.SceneDescriptionList]{}, normalizerErrorf("context error before normalize: %w", err)
|
||||
}
|
||||
value, err := normalizeList(req.MergeOutput.Value, req.Source)
|
||||
value, warnings, err := normalizeList(req.MergeOutput.Value, req.Source)
|
||||
if err != nil {
|
||||
return contracts.TypedNormalizeResult[dnd.SceneDescriptionList]{}, normalizerErrorf("normalize scenes: %w", err)
|
||||
}
|
||||
return contracts.TypedNormalizeResult[dnd.SceneDescriptionList]{Value: value}, nil
|
||||
return contracts.TypedNormalizeResult[dnd.SceneDescriptionList]{
|
||||
Value: value,
|
||||
Warnings: diagnostics.LimitWarnings(warnings, "scenes", ReasonCodeNormalizationWarningsOmitted),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func normalizeList(input dnd.SceneDescriptionList, doc *source.SourceDocument) (dnd.SceneDescriptionList, error) {
|
||||
type normalizedScene struct {
|
||||
scene dnd.SceneDescription
|
||||
inputIndex int
|
||||
}
|
||||
|
||||
func normalizeList(input dnd.SceneDescriptionList, doc *source.SourceDocument) (dnd.SceneDescriptionList, []contracts.Warning, error) {
|
||||
if doc == nil {
|
||||
return dnd.SceneDescriptionList{}, fmt.Errorf("source document must not be nil")
|
||||
return dnd.SceneDescriptionList{}, nil, fmt.Errorf("source document must not be nil")
|
||||
}
|
||||
if input.Scenes == nil {
|
||||
return dnd.SceneDescriptionList{}, fmt.Errorf("scenes must be present")
|
||||
return dnd.SceneDescriptionList{}, nil, fmt.Errorf("scenes must be present")
|
||||
}
|
||||
if len(input.Scenes) == 0 {
|
||||
return dnd.SceneDescriptionList{}, fmt.Errorf("scenes must not be empty")
|
||||
return dnd.SceneDescriptionList{}, nil, fmt.Errorf("scenes must not be empty")
|
||||
}
|
||||
|
||||
documentIndex := source.NewDocumentIndex(doc)
|
||||
output := dnd.SceneDescriptionList{Scenes: make([]dnd.SceneDescription, len(input.Scenes))}
|
||||
records := make([]normalizedScene, len(input.Scenes))
|
||||
warnings := make([]contracts.Warning, 0)
|
||||
for sceneIndex, scene := range input.Scenes {
|
||||
originalTitle, originalSummary := scene.Title, scene.Summary
|
||||
scene.Title = strings.TrimSpace(scene.Title)
|
||||
scene.Summary = strings.TrimSpace(scene.Summary)
|
||||
if err := shape.Validate(dnd.SceneDescriptionList{Scenes: []dnd.SceneDescription{scene}}); err != nil {
|
||||
return dnd.SceneDescriptionList{}, fmt.Errorf("scenes[%d]: %w", sceneIndex, err)
|
||||
return dnd.SceneDescriptionList{}, nil, fmt.Errorf("scenes[%d]: %w", sceneIndex, err)
|
||||
}
|
||||
if err := documentIndex.ValidateRef(scene.SourceRef); err != nil {
|
||||
return dnd.SceneDescriptionList{}, fmt.Errorf("scenes[%d].source_ref: %s", sceneIndex, diagnostics.Truncate(err.Error()))
|
||||
return dnd.SceneDescriptionList{}, nil, fmt.Errorf("scenes[%d].source_ref: %s", sceneIndex, diagnostics.Truncate(err.Error()))
|
||||
}
|
||||
output.Scenes[sceneIndex] = scene
|
||||
if originalTitle != scene.Title || originalSummary != scene.Summary {
|
||||
warnings = append(warnings, contracts.Warning{
|
||||
Scope: sceneScope(sceneIndex),
|
||||
ReasonCode: ReasonCodeProseNormalized,
|
||||
Message: fmt.Sprintf("input index %d: title and/or summary whitespace normalized", sceneIndex),
|
||||
})
|
||||
}
|
||||
records[sceneIndex] = normalizedScene{scene: scene, inputIndex: sceneIndex}
|
||||
}
|
||||
|
||||
sort.SliceStable(output.Scenes, func(left, right int) bool {
|
||||
leftStart, _ := documentIndex.Position(output.Scenes[left].SourceRef.StartUnitID)
|
||||
rightStart, _ := documentIndex.Position(output.Scenes[right].SourceRef.StartUnitID)
|
||||
sort.SliceStable(records, func(left, right int) bool {
|
||||
leftStart, _ := documentIndex.Position(records[left].scene.SourceRef.StartUnitID)
|
||||
rightStart, _ := documentIndex.Position(records[right].scene.SourceRef.StartUnitID)
|
||||
if leftStart != rightStart {
|
||||
return leftStart < rightStart
|
||||
}
|
||||
return output.Scenes[left].ID < output.Scenes[right].ID
|
||||
return records[left].scene.ID < records[right].scene.ID
|
||||
})
|
||||
for position, record := range records {
|
||||
if position == record.inputIndex {
|
||||
continue
|
||||
}
|
||||
warnings = append(warnings, contracts.Warning{
|
||||
Scope: sceneScope(record.inputIndex),
|
||||
ReasonCode: ReasonCodeOrderNormalized,
|
||||
Message: fmt.Sprintf("input index %d moved to normalized position %d by canonical scene order",
|
||||
record.inputIndex, position),
|
||||
})
|
||||
}
|
||||
|
||||
unique := make([]dnd.SceneDescription, 0, len(output.Scenes))
|
||||
byID := make(map[string]dnd.SceneDescription, len(output.Scenes))
|
||||
byRange := make(map[source.SourceRef]dnd.SceneDescription, len(output.Scenes))
|
||||
seen := make(map[dnd.SceneDescription]struct{}, len(output.Scenes))
|
||||
for _, scene := range output.Scenes {
|
||||
unique := make([]dnd.SceneDescription, 0, len(records))
|
||||
byID := make(map[string]dnd.SceneDescription, len(records))
|
||||
byRange := make(map[source.SourceRef]dnd.SceneDescription, len(records))
|
||||
seen := make(map[dnd.SceneDescription]int, len(records))
|
||||
for _, record := range records {
|
||||
scene := record.scene
|
||||
if previous, ok := byID[scene.ID]; ok && !identical(previous, scene) {
|
||||
return dnd.SceneDescriptionList{}, fmt.Errorf("scene ID %s has conflicting records", diagnostics.Quote(scene.ID))
|
||||
return dnd.SceneDescriptionList{}, nil, fmt.Errorf("scene ID %s has conflicting records", diagnostics.Quote(scene.ID))
|
||||
}
|
||||
if previous, ok := byRange[scene.SourceRef]; ok && !sameModelContent(previous, scene) {
|
||||
return dnd.SceneDescriptionList{}, fmt.Errorf("source range %s has conflicting records", sourceRefLabel(scene.SourceRef))
|
||||
return dnd.SceneDescriptionList{}, nil, fmt.Errorf("source range %s has conflicting records", sourceRefLabel(scene.SourceRef))
|
||||
}
|
||||
if _, ok := seen[scene]; ok {
|
||||
if retainedIndex, ok := seen[scene]; ok {
|
||||
warnings = append(warnings, contracts.Warning{
|
||||
Scope: sceneScope(record.inputIndex),
|
||||
ReasonCode: ReasonCodeDuplicateCollapsed,
|
||||
Message: fmt.Sprintf("input index %d: exact duplicate scene collapsed; retained input index %d",
|
||||
record.inputIndex, retainedIndex),
|
||||
})
|
||||
continue
|
||||
}
|
||||
byID[scene.ID] = scene
|
||||
byRange[scene.SourceRef] = scene
|
||||
seen[scene] = struct{}{}
|
||||
seen[scene] = record.inputIndex
|
||||
unique = append(unique, scene)
|
||||
}
|
||||
output.Scenes = unique
|
||||
return output, nil
|
||||
return dnd.SceneDescriptionList{Scenes: unique}, warnings, nil
|
||||
}
|
||||
|
||||
func sceneScope(index int) string { return fmt.Sprintf("scenes[%d]", index) }
|
||||
|
||||
func identical(left, right dnd.SceneDescription) bool {
|
||||
return left == right
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user