Skip to content

Graph-Native Infrastructure for Context and Accountable AI Systems

fix(split): validate sliding window progress

hsien weidfd668c20628
diff --git a/semantica/split/sliding_window_chunker.py b/semantica/split/sliding_window_chunker.py
index 1ba3a630..40e1d6fb 100644
--- a/semantica/split/sliding_window_chunker.py
+++ b/semantica/split/sliding_window_chunker.py
@@ -64,6 +64,8 @@ class SlidingWindowChunker:
raise ValidationError("overlap must be non-negative")
if self.overlap >= self.chunk_size:
raise ValidationError("overlap must be less than chunk_size")
+ if self.stride <= 0:
+ raise ValidationError("stride must be positive")
def chunk(self, text: str, **options) -> List[Chunk]:
"""
@@ -215,15 +217,20 @@ class SlidingWindowChunker:
Returns:
list: List of chunks
"""
+ if overlap_size is None:
+ return self.chunk(text)
+ if overlap_size < 0:
+ raise ValidationError("overlap_size must be non-negative")
+ if overlap_size >= self.chunk_size:
+ raise ValidationError("overlap_size must be less than chunk_size")
+
original_overlap = self.overlap
- if overlap_size is not None:
+ original_stride = self.stride
+
+ try:
self.overlap = overlap_size
self.stride = self.chunk_size - self.overlap
-
- chunks = self.chunk(text)
-
- # Restore original overlap
- self.overlap = original_overlap
- self.stride = self.chunk_size - self.overlap
-
- return chunks
+ return self.chunk(text)
+ finally:
+ self.overlap = original_overlap
+ self.stride = original_stride
diff --git a/tests/split/test_chunkers.py b/tests/split/test_chunkers.py
index 7210a9de..0fff307c 100644
--- a/tests/split/test_chunkers.py
+++ b/tests/split/test_chunkers.py
@@ -60,6 +60,11 @@ class TestSlidingWindowChunker:
with pytest.raises(ValidationError):
SlidingWindowChunker(chunk_size=100, overlap=100)
+ @pytest.mark.parametrize("stride", [0, -1])
+ def test_init_rejects_non_positive_stride(self, stride):
+ with pytest.raises(ValidationError, match="stride must be positive"):
+ SlidingWindowChunker(chunk_size=100, stride=stride)
+
def test_empty_text_returns_empty(self):
chunker = SlidingWindowChunker(chunk_size=50, overlap=10)
assert chunker.chunk("") == []
@@ -97,6 +102,49 @@ class TestSlidingWindowChunker:
chunks = chunker.chunk_with_overlap(text, overlap_size=15)
assert len(chunks) >= 2
assert chunker.overlap == 0
+ assert chunker.stride == 50
+
+ @pytest.mark.parametrize("overlap_size", [-1, 50, 51])
+ def test_chunk_with_overlap_rejects_invalid_override(self, overlap_size):
+ chunker = SlidingWindowChunker(chunk_size=50)
+
+ with pytest.raises(ValidationError):
+ chunker.chunk_with_overlap(
+ "non-empty input", overlap_size=overlap_size
+ )
+
+ def test_chunk_with_overlap_accepts_largest_valid_override(self):
+ chunker = SlidingWindowChunker(chunk_size=5)
+
+ chunks = chunker.chunk_with_overlap("abcdefghij", overlap_size=4)
+
+ assert [chunk.start_index for chunk in chunks] == list(range(10))
+
+ def test_chunk_with_overlap_restores_custom_stride(self):
+ chunker = SlidingWindowChunker(chunk_size=10, overlap=2, stride=3)
+
+ chunker.chunk_with_overlap(
+ "abcdefghijklmnopqrstuvwxyz", overlap_size=4
+ )
+
+ assert chunker.overlap == 2
+ assert chunker.stride == 3
+
+ def test_chunk_with_overlap_restores_state_when_chunk_raises(
+ self, monkeypatch
+ ):
+ chunker = SlidingWindowChunker(chunk_size=10, overlap=2, stride=3)
+
+ def raise_error(text):
+ raise RuntimeError("chunk failed")
+
+ monkeypatch.setattr(chunker, "chunk", raise_error)
+
+ with pytest.raises(RuntimeError, match="chunk failed"):
+ chunker.chunk_with_overlap("non-empty input", overlap_size=4)
+
+ assert chunker.overlap == 2
+ assert chunker.stride == 3
def test_boundary_preservation_avoids_mid_word_when_possible(self):
text = (