Repository navigation
Batch chunk filtering so a long page fits Jev's context - #1
Merged
Merged
Conversation
A page was split into ~2,000-character chunks and then sent to Jev as a single request carrying one question per chunk. On a long page that request is far too big: scraping RFC 9110 with --max-chars 400000 produced 219 chunks and Jev rejected the lot with HTTP 400 max_tokens_exceeded. The fallback then made it worse. A failed filter keeps the page whole, so one search emitted 400KB of unfiltered text into the caller's context — exactly what --filter-chunks exists to prevent — while reporting "219/219 chunks kept", which reads like a filter that ran and liked everything. Chunks are now packed into batches that stay under an estimated-token budget and sent in parallel. The estimate accounts for each chunk being sent twice, once in the request state and again in its rendered question, plus the measured prompt boilerplate; batches are capped at 24 chunks besides. A batch Jev still rejects for size is halved and retried down to a single chunk, so the estimate's accuracy is a performance concern rather than a correctness one. Failure is now contained per batch. FilterChunks returns *PartialFilterError when only some batches failed, naming the chunks left unjudged; the caller keeps those chunks and still filters the rest, since losing a verdict must never silently delete page content. Only a total failure falls back to the whole unfiltered page. Counters no longer claim unjudged chunks were judged: output reports "k/n chunks kept, u unjudged" and JSON gains chunks_unjudged. Measured on the case above, same query and flags: before 401,981 chars delivered, filter failed, 219/219 "kept" after 7,729 chars delivered, no error, 3/219 kept and the answer, which lives 227,905 characters into the RFC, survives in both the RFC's own text and the summary page.
Each chunk is judged with the last 20% of the previous chunk attached as preceding context, cut at a sentence or word boundary. The prompt says the context is not part of the chunk, so a heading or sentence split at a chunk edge is judged with what follows it without letting boilerplate borrow relevance from its neighbour. Kept chunks are output bare, so nothing is duplicated in the caller's context. --chunk-chars sets the chunk size (default 2000); the overlap scales with it. Behavior version 0.0.027, package version 0.1.4.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The bug
--filter-chunkssplit a page into ~2,000-character chunks and then sent them to Jev as one request with one question per chunk. On a long page that request is far too large.Scraping RFC 9110 with
--max-chars 400000:The fallback made it worse. A failed filter keeps the page whole, so a single search emitted 400 KB of unfiltered text into the caller's context — precisely what
--filter-chunksexists to prevent. Worse for debugging, it reported219/219 chunks kept, which reads like a filter that ran and approved everything.Measured threshold, sweeping
--max-charson the same page:--max-charsmax_tokens_exceededmax_tokens_exceededThis also quietly amputates primary sources at the default. On that same query at default settings,
datatracker.ietf.org/doc/html/rfc9110returned0/27 chunks kept, 0 chars— truncated at 50 KB, the first 10% of a 503 KB document, while the answer lives at 45%. The right answer only arrived because MDN happened to cover the same header.The fix
Budget ahead of time. Chunks are packed into batches that stay under an estimated-token budget. The estimate accounts for each chunk being sent twice — once in the request state, once in its rendered question — plus the boilerplate, measured by rendering a probe question rather than guessed. Batches are capped at 24 chunks regardless.
Parallel. Batches for a page are sent concurrently, bounded so that the existing per-page concurrency doesn't multiply out.
Retry by halving. A batch Jev still rejects for size is split in half and retried, down to a single chunk. Only size errors trigger this — a 500 isn't retried into 2× the calls. That makes the token estimate's accuracy a performance concern rather than a correctness one.
Fail gracefully, per batch.
FilterChunksreturns*PartialFilterErrorwhen only some batches failed, naming the chunks left unjudged. The caller keeps those chunks and still filters the rest — losing a verdict must never silently delete page content. Only a total failure falls back to the whole unfiltered page, as before.Stop misreporting. Unjudged chunks are no longer counted as judged. Output reads
k/n chunks kept, u unjudged, the summary distinguishes partly-filtered from unfiltered pages, and JSON gainschunks_unjudged.Result
Same query, same flags,
--max-chars 400000:max_tokens_exceeded52× less text, and the answer survives — including from the RFC's own text at character 227,905, which the old path never reached.
Second case, RFC 8446
key_share:Tests
tooLargematches 400/413 size errors and not 500s or unrelated 400s1 unjudgedgo test ./...andgo vet ./...pass.Note
The chunk text is sent twice per chunk — in
state.chunks[].textand again inside the rendered question. Halving that would roughly double effective batch capacity, but it changes the prompt Jev sees, so I left it alone here rather than mix a quality change into a correctness fix. Worth its own look.