Fix overflow in chunk and reconciliation ranges

This commit is contained in:
2026-11-26 01:26:46 +00:00
parent cecd68f161
commit 4026e26e76
6 changed files with 77 additions and 10 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
work from the remaining audit remediation.
The **45 original finding entries** reduce to **30 outstanding catalog entries
and four resolved entries**. Repeated responsibility findings are
The **45 original finding entries** reduce to **28 outstanding catalog entries
and six resolved entries**. Repeated responsibility findings are
merged. Regression gaps are attached to the defects they protect rather than
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
@@ -65,7 +65,7 @@ Original P2/P3 ratings and relative refactoring priorities are retained for
context; they are not one universal severity scale. No audit establishes a P1
incident or a measured end-to-end production speedup.
CAT-007, CAT-015, CAT-016, and CAT-034 are resolved; the other entries remain
CAT-001, CAT-007, CAT-011, CAT-015, CAT-016, and CAT-034 are resolved; the other entries remain
outstanding. The original reports and their baseline observations are historical.
"Refactoring" denotes a maintenance opportunity, not a confirmed runtime
failure. CAT-024 explicitly requires revisiting an existing evidence-convenience
@@ -88,6 +88,10 @@ any of those findings.
**Assessment:** Reproduced correctness defect; original P2.
**Status:** Resolved. Chunk planning uses overflow-safe capacity and endpoint
arithmetic; option-decoding and planning regressions cover maximum size and
ordinary overlap.
**Sources:** [Correctness / Finding 1](01-correctness.md#finding-1-a-valid-large-generic-chunk-size-causes-a-panic),
[TEST-001](08-testing.md#test-001--accepted-numeric-boundaries-lack-regression-cases).
@@ -248,6 +252,9 @@ Signal handling stays at the executable boundary; implementation remains pending
**Assessment:** Reproduced latent internal-API defect; original P3.
**Status:** Resolved. Context expansion is bounded by source distance before
arithmetic; regressions cover the maximum radius, cited flags, and material limit.
**Sources:** [Correctness / Finding 3](01-correctness.md#finding-3-context-radius-addition-can-overflow-and-omit-all-evidence),
[TEST-001](08-testing.md#test-001--accepted-numeric-boundaries-lack-regression-cases).

View File

@@ -153,6 +153,12 @@ comparators for unproven inputs; canonical fast paths retain their existing prem
## Stage 1 — Fix Accepted Range Arithmetic
**Completed.** Chunk planning uses overflow-safe capacity and remaining-length
arithmetic; reconciliation bounds expansion by source distance. Regressions
reproduced both failures before the fixes and cover maximum values, overlap,
cited flags, and the material budget. The specified package tests pass. No
follow-up remains for this stage.
**Coverage:** CAT-001/CAT-011. **Prerequisites:** none.
Inspect generic `chunk/units/chunker.go` and reconciliation `preparation.go` plus

View File

@@ -225,8 +225,8 @@ func Prepare(document *source.SourceDocument, candidates []Candidate, limits Lim
for _, interval := range candidate.intervals {
citedIntervals = append(citedIntervals, interval)
contextIntervals = append(contextIntervals, sourceInterval{
start: max(0, interval.start-limits.ContextRadius),
end: min(len(document.Units)-1, interval.end+limits.ContextRadius),
start: interval.start - min(interval.start, limits.ContextRadius),
end: interval.end + min(len(document.Units)-1-interval.end, limits.ContextRadius),
})
}
}

View File

@@ -149,6 +149,42 @@ func TestPrepareBuildsContiguousCandidatesAndOwnedSourceContext(t *testing.T) {
}
}
func TestPrepareMaximumContextRadius(t *testing.T) {
document := &source.SourceDocument{ID: "session", Units: []source.SourceUnit{
{ID: 9, Text: "first"}, {ID: 3, Text: "second"}, {ID: 8, Text: "third"},
}}
candidates := []Candidate{
{Label: "One", SourceRefs: []source.SourceRef{{SourceID: document.ID, StartUnitID: 3, EndUnitID: 3}}},
{Label: "Two", SourceRefs: []source.SourceRef{{SourceID: document.ID, StartUnitID: 8, EndUnitID: 8}}},
}
limits := Limits{ContextRadius: math.MaxInt, MaximumCandidates: 2, MaximumMaterialBytes: 10000}
preparation, err := Prepare(document, candidates, limits)
if err != nil || preparation.Disposition() != Ready {
t.Fatalf("Prepare() disposition = %v, error = %v", preparation.Disposition(), err)
}
var transcript transcriptInput
if err := json.Unmarshal(preparation.Materials()[transcriptInputName], &transcript); err != nil {
t.Fatal(err)
}
if len(transcript.Windows) != 1 || len(transcript.Windows[0].Units) != 3 {
t.Fatalf("windows = %#v, want the entire source", transcript.Windows)
}
for i, want := range []struct {
id int
cited bool
}{{9, false}, {3, true}, {8, true}} {
unit := transcript.Windows[0].Units[i]
if unit.ID != want.id || unit.Cited != want.cited {
t.Fatalf("unit = %#v, want ID %d and cited %t", unit, want.id, want.cited)
}
}
limits.MaximumMaterialBytes = 1
preparation, err = Prepare(document, candidates, limits)
if err != nil || preparation.Disposition() != LimitExceeded {
t.Fatalf("Prepare() disposition = %v, error = %v, want material limit", preparation.Disposition(), err)
}
}
func TestPrepareRedactsInvalidSourceMetadata(t *testing.T) {
const sensitiveKey = "sensitive-metadata-key"
document := &source.SourceDocument{ID: "private-source", Units: []source.SourceUnit{

View File

@@ -63,12 +63,13 @@ func (c *Chunker) PrepareChunk(ctx context.Context, req contracts.ChunkInput) (c
}
step := c.options.MaxUnits - c.options.OverlapUnits
ranges := make([]source.ChunkRange, 0, (len(req.Source.Units)+step-1)/step)
count := len(req.Source.Units) / step
if len(req.Source.Units)%step != 0 {
count++
}
ranges := make([]source.ChunkRange, 0, count)
for start := 0; start < len(req.Source.Units); start += step {
end := start + c.options.MaxUnits
if end > len(req.Source.Units) {
end = len(req.Source.Units)
}
end := start + min(c.options.MaxUnits, len(req.Source.Units)-start)
ranges = append(ranges, source.ChunkRange{
StartUnitID: req.Source.Units[start].ID,
EndUnitID: req.Source.Units[end-1].ID,

View File

@@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"math"
"reflect"
"strings"
"testing"
@@ -93,6 +94,22 @@ func TestPlanOverlap(t *testing.T) {
}
}
func TestPlanMaximumUnitCount(t *testing.T) {
for _, overlap := range []int{0, math.MaxInt - 1} {
t.Run(fmt.Sprint(overlap), func(t *testing.T) {
chunker := newChunker(t, map[string]any{"max_units": math.MaxInt, "overlap_units": overlap})
result, err := runChunk(context.Background(), chunker, contracts.ChunkInput{Source: testSource(3)})
if err != nil {
t.Fatal(err)
}
want := []source.ChunkRange{{StartUnitID: 1, EndUnitID: 3}}
if !reflect.DeepEqual(result.Value.Ranges, want) {
t.Fatalf("ranges = %#v, want %#v", result.Value.Ranges, want)
}
})
}
}
func TestPlanRejectsInvalidOptions(t *testing.T) {
tests := []struct {
name string