Split a slice into two halves at a newline near its midpoint, or None. Used by the adaptive-retry path when a single slice still overflows the model's output: halving it produces a smaller response. Returns None when the slice is already too small to split meaningfully.
(fs: FileSlice)
| 297 | |
| 298 | |
| 299 | def bisect_slice(fs: FileSlice) -> tuple[FileSlice, FileSlice] | None: |
| 300 | """Split a slice into two halves at a newline near its midpoint, or None. |
| 301 | |
| 302 | Used by the adaptive-retry path when a single slice still overflows the |
| 303 | model's output: halving it produces a smaller response. Returns None when the |
| 304 | slice is already too small to split meaningfully. |
| 305 | """ |
| 306 | if fs.end - fs.start <= 1: |
| 307 | return None |
| 308 | # Index the SAME string the slice offsets were computed against and the |
| 309 | # prompt carries: for a PDF that is the extracted text via unit_source_text, |
| 310 | # not the raw container bytes. Reading the container here (the old behavior) |
| 311 | # searched for the newline cut in binary coordinates, so a compressed PDF |
| 312 | # slice could cut mid-line or past the text end (#2906). Any converter/read |
| 313 | # failure means we cannot split, so fall back to None (treated as atomic). |
| 314 | try: |
| 315 | text = unit_source_text(fs.path) |
| 316 | except Exception: |
| 317 | return None |
| 318 | mid = (fs.start + fs.end) // 2 |
| 319 | nl = text.find("\n", mid, fs.end) |
| 320 | cut = nl + 1 if (nl != -1 and fs.start < nl + 1 < fs.end) else mid |
| 321 | if not (fs.start < cut < fs.end): |
| 322 | return None |
| 323 | left = FileSlice(fs.path, fs.start, cut, fs.index, fs.total) |
| 324 | right = FileSlice(fs.path, cut, fs.end, fs.index, fs.total) |
| 325 | return left, right |
| 326 | |
| 327 | |
| 328 | def make_full_slice(path: "Path | FileSlice") -> FileSlice | None: |