Skip to content

Batch chunk filtering so a long page fits Jev's context - #1

Merged
dorkitude merged 2 commits into
mainfrom
fix-chunk-filter-batching
Sep 20, 2026
Merged

dorkitude merged 2 commits into
mainfrom
fix-chunk-filter-batching

Conversation

@dorkitude

Copy link
Copy Markdown
Owner

The bug

--filter-chunks split 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:

chunks=219/219  filter_error='Jev API returned HTTP 400: {"error_type":"max_tokens_exceeded"}'

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-chunks exists to prevent. Worse for debugging, it reported 219/219 chunks kept, which reads like a filter that ran and approved everything.

Measured threshold, sweeping --max-chars on the same page:

--max-chars chunks chunk filter
50,000 27 works
100,000 54 works
150,000 81 max_tokens_exceeded
250,000 137 max_tokens_exceeded

This also quietly amputates primary sources at the default. On that same query at default settings, datatracker.ietf.org/doc/html/rfc9110 returned 0/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. FilterChunks returns *PartialFilterError when 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 gains chunks_unjudged.

Result

Same query, same flags, --max-chars 400000:

content delivered chunks error
before 401,981 chars 219/219 "kept" max_tokens_exceeded
after 7,729 chars 3/219 kept none

52× 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:

rfc-editor.org/info/rfc8446/                3,867 chars   2/162 chunks   no error
iana.org/assignments/tls-extensiontype-...  1,981 chars   1/3 chunks     no error
key_share(51) present: True

Tests

  • long page splits into multiple requests, verdicts complete, usage accumulates
  • batch planning stays within budget, contiguous, covers every chunk, honors the cap
  • a single chunk larger than the whole budget still gets sent, not dropped
  • oversized batch is halved and recovers
  • one failed batch costs only its own chunks, names them, keeps the rest filtered
  • total failure stays a plain error, not a partial one
  • tooLarge matches 400/413 size errors and not 500s or unrelated 400s
  • CLI: a partly-failed page keeps unjudged chunks, still drops rejected ones, reports 1 unjudged

go test ./... and go vet ./... pass.

Note

The chunk text is sent twice per chunk — in state.chunks[].text and 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.

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.
@dorkitude
dorkitude merged commit ad74d14 into main Sep 20, 2026
@dorkitude
dorkitude deleted the fix-chunk-filter-batching branch September 20, 2026 23:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant