From f31b0f30a20eb64d2f6dea4488f1d8e82a768e13 Mon Sep 17 00:00:00 2001 From: Eric Rakestraw Date: Thu, 26 Nov 2026 05:16:56 +0000 Subject: [PATCH] Size chunk range storage to the emitted windows --- docs/roadmap/remediation.md | 8 +++++++ .../modules/generic/chunk/units/chunker.go | 12 +++++++--- .../generic/chunk/units/chunker_test.go | 22 +++++++++++++++++++ 3 files changed, 39 insertions(+), 3 deletions(-) diff --git a/docs/roadmap/remediation.md b/docs/roadmap/remediation.md index baa59c52..16e3061b 100644 --- a/docs/roadmap/remediation.md +++ b/docs/roadmap/remediation.md @@ -275,6 +275,14 @@ oracle is sufficient; no benchmark or framework is needed. ### ALG-003 — Chunk Range Capacity Can Scale With Source Size For One Range +**Status:** Completed. Range capacity now uses the first window plus quotient/ +remainder arithmetic for uncovered units, so reservation follows emitted ranges +without overflowing addition. Literal high-overlap, exact/partial remaining-step, +ordinary overlap, and maximum-integer tests pass. The temporary diagnostic now +reserves one slot for each one-range case (4 and 40 source units); no permanent +capacity assertion was added. Final repository tests, vet, build, pipeline/chunker +race tests, and whitespace checks passed with all three remediations in place. + **Priority:** Low; avoidable space/zeroing cost for valid high-overlap settings. Stage 1's overflow defect is fixed; this concerns the retained upper-bound capacity. diff --git a/internal/modules/generic/chunk/units/chunker.go b/internal/modules/generic/chunk/units/chunker.go index c25102d2..eb8f92ac 100644 --- a/internal/modules/generic/chunk/units/chunker.go +++ b/internal/modules/generic/chunk/units/chunker.go @@ -63,9 +63,15 @@ func (c *Chunker) PrepareChunk(ctx context.Context, req contracts.ChunkInput) (c } step := c.options.MaxUnits - c.options.OverlapUnits - count := len(req.Source.Units) / step - if len(req.Source.Units)%step != 0 { - count++ + // The first range covers MaxUnits; stop once a range reaches the end, + // rather than reserving trailing windows that overlap the final range. + count := 1 + if len(req.Source.Units) > c.options.MaxUnits { + remaining := len(req.Source.Units) - c.options.MaxUnits + count += remaining / step + if remaining%step != 0 { + count++ + } } ranges := make([]source.ChunkRange, 0, count) for start := 0; start < len(req.Source.Units); start += step { diff --git a/internal/modules/generic/chunk/units/chunker_test.go b/internal/modules/generic/chunk/units/chunker_test.go index 3ba9448b..cbabfa84 100644 --- a/internal/modules/generic/chunk/units/chunker_test.go +++ b/internal/modules/generic/chunk/units/chunker_test.go @@ -110,6 +110,28 @@ func TestPlanMaximumUnitCount(t *testing.T) { } } +func TestPlanHighOverlapStopsAtSourceEnd(t *testing.T) { + for _, test := range []struct { + name string + units int + max int + overlap int + want []source.ChunkRange + }{ + {"one complete range", 40, 40, 39, []source.ChunkRange{{StartUnitID: 1, EndUnitID: 40}}}, + {"exact remaining step", 5, 4, 3, []source.ChunkRange{{StartUnitID: 1, EndUnitID: 4}, {StartUnitID: 2, EndUnitID: 5}}}, + {"partial remaining step", 7, 4, 2, []source.ChunkRange{{StartUnitID: 1, EndUnitID: 4}, {StartUnitID: 3, EndUnitID: 6}, {StartUnitID: 5, EndUnitID: 7}}}, + } { + t.Run(test.name, func(t *testing.T) { + chunker := newChunker(t, map[string]any{"max_units": test.max, "overlap_units": test.overlap}) + result, err := runChunk(context.Background(), chunker, contracts.ChunkInput{Source: testSource(test.units)}) + if err != nil || !reflect.DeepEqual(result.Value.Ranges, test.want) { + t.Fatalf("ranges = %#v, %v; want %#v", result.Value.Ranges, err, test.want) + } + }) + } +} + func TestPlanRejectsInvalidOptions(t *testing.T) { tests := []struct { name string