Resolve citations using coalesced source ranges

This commit is contained in:
2026-11-26 01:32:35 +00:00
parent 97bcf8cae6
commit 4b03a439cb
4 changed files with 72 additions and 13 deletions

View File

@@ -20,8 +20,8 @@ The [Core And Integration API Roadmap](../core-integration-api.md) established
the core/integration boundary. The reassessment below distinguishes its completed the core/integration boundary. The reassessment below distinguishes its completed
work from the remaining audit remediation. work from the remaining audit remediation.
The **45 original finding entries** reduce to **25 outstanding catalog entries The **45 original finding entries** reduce to **24 outstanding catalog entries
and nine resolved entries**. Repeated responsibility findings are and ten resolved entries**. Repeated responsibility findings are
merged. Regression gaps are attached to the defects they protect rather than merged. Regression gaps are attached to the defects they protect rather than
counted as additional runtime bugs. In particular, TEST-001, TEST-002, and counted as additional runtime bugs. In particular, TEST-001, TEST-002, and
TEST-004 each cover several distinct defects; TEST-003's native-fixture oracle TEST-004 each cover several distinct defects; TEST-003's native-fixture oracle
@@ -65,8 +65,8 @@ Original P2/P3 ratings and relative refactoring priorities are retained for
context; they are not one universal severity scale. No audit establishes a P1 context; they are not one universal severity scale. No audit establishes a P1
incident or a measured end-to-end production speedup. incident or a measured end-to-end production speedup.
CAT-001, CAT-002, CAT-007, CAT-011, CAT-012, CAT-013, CAT-015, CAT-016, and CAT-001, CAT-002, CAT-004, CAT-007, CAT-011, CAT-012, CAT-013, CAT-015,
CAT-034 are resolved; CAT-016, and CAT-034 are resolved;
the other entries remain outstanding. The original reports and their baseline the other entries remain outstanding. The original reports and their baseline
observations are historical. observations are historical.
"Refactoring" denotes a maintenance opportunity, not a confirmed runtime "Refactoring" denotes a maintenance opportunity, not a confirmed runtime
@@ -136,6 +136,9 @@ unnecessary transient allocation, not a proven permanent leak.
**Assessment:** Source-established algorithmic opportunity; original P2. **Assessment:** Source-established algorithmic opportunity; original P2.
**Status:** Resolved. Position intervals are validated, sorted, and coalesced
before only their union is visited, with literal source-order regression cases.
**Source:** [EFF-002](02-efficiency.md#eff-002--build-cited-text-from-coalesced-ranges-not-a-document-wide-bitmap). **Source:** [EFF-002](02-efficiency.md#eff-002--build-cited-text-from-coalesced-ranges-not-a-document-wide-bitmap).
`CitationResolver.CitedText` allocates document-sized buffers and scans every `CitationResolver.CitedText` allocates document-sized buffers and scans every

View File

@@ -235,6 +235,11 @@ concrete containers. No shared-array ban, JSON round trip, or option-copier merg
## Stage 5 — Resolve Sparse Citations From Coalesced Intervals ## Stage 5 — Resolve Sparse Citations From Coalesced Intervals
**Completed.** Citation resolution validates in input order, sorts and coalesces
position intervals, and visits only selected units. Literal tests cover sparse,
overlapping, disjoint, invalid, non-monotonic, and empty-text cases. The specified
shared and validator tests pass; no performance timing claim or follow-up remains.
**Coverage:** CAT-004. **Prerequisites:** none. **Coverage:** CAT-004. **Prerequisites:** none.
Rewrite `dnd/shared/citations.go` as selected. Validate refs in input order before Rewrite `dnd/shared/citations.go` as selected. Validate refs in input order before

View File

@@ -2,6 +2,7 @@ package shared
import ( import (
"fmt" "fmt"
"sort"
"strings" "strings"
"gitea.maximumdirect.net/eric/notarius/internal/core/source" "gitea.maximumdirect.net/eric/notarius/internal/core/source"
@@ -24,23 +25,41 @@ func NewCitationResolver(doc *source.SourceDocument) (*CitationResolver, error)
// CitedText resolves cited source ranges in document order, including each // CitedText resolves cited source ranges in document order, including each
// source unit once, and joins the resulting text with newlines. // source unit once, and joins the resulting text with newlines.
func (r *CitationResolver) CitedText(refs []source.SourceRef) (string, error) { func (r *CitationResolver) CitedText(refs []source.SourceRef) (string, error) {
included := make([]bool, len(r.doc.Units)) type interval struct{ start, end int }
intervals := make([]interval, 0, len(refs))
for _, ref := range refs { for _, ref := range refs {
if err := r.index.ValidateRef(ref); err != nil { if err := r.index.ValidateRef(ref); err != nil {
return "", fmt.Errorf("resolve cited source range: %w", err) return "", fmt.Errorf("resolve cited source range: %w", err)
} }
start, _ := r.index.Position(ref.StartUnitID) start, _ := r.index.Position(ref.StartUnitID)
end, _ := r.index.Position(ref.EndUnitID) end, _ := r.index.Position(ref.EndUnitID)
for index := start; index <= end; index++ { intervals = append(intervals, interval{start: start, end: end})
included[index] = true }
sort.Slice(intervals, func(i, j int) bool {
if intervals[i].start != intervals[j].start {
return intervals[i].start < intervals[j].start
}
return intervals[i].end < intervals[j].end
})
merged := intervals[:0]
for _, item := range intervals {
if len(merged) == 0 || item.start > merged[len(merged)-1].end+1 {
merged = append(merged, item)
} else {
previous := &merged[len(merged)-1]
previous.end = max(previous.end, item.end)
} }
} }
var text strings.Builder
parts := make([]string, 0, len(r.doc.Units)) first := true
for index, unit := range r.doc.Units { for _, item := range merged {
if included[index] { for position := item.start; position <= item.end; position++ {
parts = append(parts, unit.Text) if !first {
text.WriteByte('\n')
}
first = false
text.WriteString(r.doc.Units[position].Text)
} }
} }
return strings.Join(parts, "\n"), nil return text.String(), nil
} }

View File

@@ -75,6 +75,38 @@ func TestNewCitationResolverRejectsNilDocument(t *testing.T) {
} }
} }
func TestCitationResolverUsesSourceOrderAndPreservesEmptyText(t *testing.T) {
doc := &source.SourceDocument{ID: "session", Units: []source.SourceUnit{
{ID: 40, Text: "first"}, {ID: 10, Text: ""}, {ID: 70, Text: "third"}, {ID: 20, Text: "last"},
}}
resolver, err := NewCitationResolver(doc)
if err != nil {
t.Fatal(err)
}
for _, tc := range []struct {
name string
refs []source.SourceRef
want string
}{
{"sparse", []source.SourceRef{{SourceID: "session", StartUnitID: 20, EndUnitID: 20}, {SourceID: "session", StartUnitID: 40, EndUnitID: 40}}, "first\nlast"},
{"nested and overlapping", []source.SourceRef{{SourceID: "session", StartUnitID: 10, EndUnitID: 70}, {SourceID: "session", StartUnitID: 40, EndUnitID: 70}, {SourceID: "session", StartUnitID: 70, EndUnitID: 20}}, "first\n\nthird\nlast"},
{"empty text", []source.SourceRef{{SourceID: "session", StartUnitID: 10, EndUnitID: 10}}, ""},
} {
t.Run(tc.name, func(t *testing.T) {
got, err := resolver.CitedText(tc.refs)
if err != nil || got != tc.want {
t.Fatalf("CitedText() = %q, %v; want %q", got, err, tc.want)
}
})
}
firstInvalid := source.SourceRef{SourceID: "other", StartUnitID: 40, EndUnitID: 40}
_, firstErr := resolver.CitedText([]source.SourceRef{firstInvalid})
_, err = resolver.CitedText([]source.SourceRef{firstInvalid, {SourceID: "session", StartUnitID: 20, EndUnitID: 40}})
if firstErr == nil || err == nil || err.Error() != firstErr.Error() {
t.Fatalf("CitedText() error = %v, want first reference error %v", err, firstErr)
}
}
func citationDocument() *source.SourceDocument { func citationDocument() *source.SourceDocument {
return &source.SourceDocument{ return &source.SourceDocument{
ID: "session", ID: "session",