Project sequence evidence through item mappings

This commit is contained in:
2026-10-23 18:07:15 +00:00
parent 7b04dc6a50
commit f6b02dec03
10 changed files with 374 additions and 66 deletions

View File

@@ -83,6 +83,48 @@ type sequenceRange struct {
path Path
start int
end int
items []sequenceItemProjection
projection sequenceItemProjectionIndex
}
func (assigned sequenceRange) projections() []sequenceItemProjection {
if assigned.items != nil {
return append([]sequenceItemProjection{}, assigned.items...)
}
return sequenceItemProjections(assigned.end-assigned.start, assigned.start)
}
func (assigned sequenceRange) outputForInput(inputIndex int) (int, bool) {
if assigned.projection.outputByInput != nil {
return assigned.projection.outputForInput(inputIndex)
}
if assigned.items != nil {
return newSequenceItemProjectionIndex(assigned.items).outputForInput(inputIndex)
}
if inputIndex < 0 || inputIndex >= assigned.end-assigned.start {
return 0, false
}
return assigned.start + inputIndex, true
}
func (assigned sequenceRange) inputForOutput(outputIndex int) (int, bool) {
if assigned.projection.inputByOutput != nil {
return assigned.projection.inputForOutput(outputIndex)
}
if assigned.items != nil {
return newSequenceItemProjectionIndex(assigned.items).inputForOutput(outputIndex)
}
if outputIndex < assigned.start || outputIndex >= assigned.end {
return 0, false
}
return outputIndex - assigned.start, true
}
func indexSequenceRange(assigned sequenceRange) sequenceRange {
if assigned.projection.outputByInput == nil && assigned.projection.inputByOutput == nil {
assigned.projection = newSequenceItemProjectionIndex(assigned.projections())
}
return assigned
}
// composeDocuments recursively joins disjoint mapping entries. Source order
@@ -371,6 +413,11 @@ func cloneSequenceRanges(ranges []sequenceRange) []sequenceRange {
for i, assigned := range ranges {
cloned[i] = assigned
cloned[i].path = assigned.path.clone()
if assigned.items != nil {
cloned[i].items = append([]sequenceItemProjection{}, assigned.items...)
}
cloned[i].projection.outputByInput = append([]int(nil), assigned.projection.outputByInput...)
cloned[i].projection.inputByOutput = append([]int(nil), assigned.projection.inputByOutput...)
}
return cloned
}
@@ -410,12 +457,22 @@ func documentEntryIndex(entries []document.Entry, key string) int {
func projectedCompositionOrigins(inputs []*document.Node, ranges []sequenceRange) map[string]Origin {
origins := make(map[string]Origin)
rangeBySourcePath := make(map[sequenceRangeKey]int, len(ranges))
for index, assigned := range ranges {
rangeBySourcePath[sequenceRangeKey{sourceIndex: assigned.sourceIndex, path: assigned.path.String()}] = index
sequencesBySource := make([][]sequenceHistoryContribution, len(inputs))
for _, assigned := range ranges {
sequencesBySource[assigned.sourceIndex] = append(sequencesBySource[assigned.sourceIndex], sequenceHistoryContribution{
path: assigned.path.clone(), mode: sequenceHistoryMapped, items: assigned.projections(),
})
}
for sourceIndex, input := range inputs {
collectProjectedCompositionOrigins(input, Path{}, Path{}, sourceIndex, ranges, rangeBySourcePath, origins)
application := historyApplication{value: input, sequences: sequencesBySource[sourceIndex]}
application.sequencesByPath = indexSequenceHistoryContributions(application.sequences)
walkHistoryApplication(application, func(_, effective Path, node *document.Node) {
pointer := effective.String()
origin := origins[pointer]
origin.Layer = LayerBase
origin.Locations = append(origin.Locations, publicLocation(node.Location))
origins[pointer] = origin
})
}
return origins
}
@@ -433,39 +490,12 @@ func indexSequenceRanges(ranges []sequenceRange) sequenceRangeIndex {
}
index := make(sequenceRangeIndex, len(ranges))
for _, assigned := range ranges {
assigned = indexSequenceRange(assigned)
index[sequenceRangeKey{sourceIndex: assigned.sourceIndex, path: assigned.path.String()}] = assigned
}
return index
}
// Keep local paths for matching Stage 2 ranges and effective paths for emitted
// evidence. The paths are transformed segment by segment; pointer strings are
// only used as map keys after projection is complete.
func collectProjectedCompositionOrigins(node *document.Node, localPath, effectivePath Path, sourceIndex int, ranges []sequenceRange, rangeBySourcePath map[sequenceRangeKey]int, origins map[string]Origin) {
pointer := effectivePath.String()
origin := origins[pointer]
origin.Layer = LayerBase
origin.Locations = append(origin.Locations, publicLocation(node.Location))
origins[pointer] = origin
switch node.Kind {
case document.Mapping:
for _, entry := range node.Mapping {
collectProjectedCompositionOrigins(entry.Value, localPath.Key(entry.Key), effectivePath.Key(entry.Key), sourceIndex, ranges, rangeBySourcePath, origins)
}
case document.Sequence:
start := 0
if rangeIndex, ok := rangeBySourcePath[sequenceRangeKey{sourceIndex: sourceIndex, path: localPath.String()}]; ok {
start = ranges[rangeIndex].start
}
for index, child := range node.Sequence {
localChild := localPath.Index(index)
effectiveChild := effectivePath.Index(start + index)
collectProjectedCompositionOrigins(child, localChild, effectiveChild, sourceIndex, ranges, rangeBySourcePath, origins)
}
}
}
func applyCompositionOrigins(decoded *decodedConfiguration, origins map[string]Origin) {
for pointer, origin := range origins {
if _, present := decoded.presence[pointer]; present {