fix(review): retry a context-length-overflowed RAG chunk truncated instead of dropping it - #5724
Conversation
…stead of dropping it GITTENSORY-D (518 occurrences, marked regressed): CHUNK_CHARS is a character-count proxy for bge-m3's real 8192-TOKEN context budget, so a dense/minified chunk can still exceed the model's actual limit despite staying under the character budget -- the observed cause of production ai_embed_http_400s. embedTexts already isolates a bad item so its batch siblings survive, but the bad item itself was always dropped entirely. Give embedSingleText one additional retry, specifically for a detected context-length overflow, at a conservatively truncated length (4000 chars, safe even at a pessimistic ~1 char/token ratio) -- a truncated-but-present vector still carries retrieval signal, which beats losing the chunk's RAG context outright. Any other error still fails fast, unchanged.
|
Superagent didn't find any vulnerabilities or security issues in this PR. |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #5724 +/- ##
=======================================
Coverage 95.05% 95.05%
=======================================
Files 581 581
Lines 46164 46169 +5
Branches 14810 14811 +1
=======================================
+ Hits 43880 43885 +5
Misses 1523 1523
Partials 761 761
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
|
Warning 🟨🟨🟨🟨🟨🟨🟨🟨🟨🟨🟨🟨 ⏸️ LoopOver review result - manual review recommendedReview updated: 2026-07-14 07:59:54 UTC
⏸️ Suggested Action - Manual Review Review summary Nits — 4 non-blocking
Concerns raised — review before merging
📋 Copy for AI agents — paste into your coding agent
Review context
Contributor next steps
Signal definitions
[BETA] Chat with GittensoryAsk Gittensory a question about this PR directly in a comment — grounded only in the same cached, public-safe facts shown above, never a new claim.
Full command reference: https://gittensory.aethereal.dev/docs/gittensory-commands 🟩 Safe / merged · 🟦 Advisory · 🟨 Held for review · 🟥 Blocked / closed 💰 Earn for open-source contributions like this. Gittensor lets GitHub contributors earn for the work they already do — register to start earning →. Checked by LoopOver, a quiet PR intelligence layer for OSS maintainers.
|
Summary
CHUNK_CHARSis a character-count proxy for bge-m3's real 8192-token context budget. Dense/minified content has a worse char-to-token ratio than typical code, so a chunk can still exceed the model's actual token limit despite staying under the character budget — the documented, previously-diagnosed cause of productionai_embed_http_400s (see the existing#4996/#5046comments insrc/review/rag.ts).embedTextsalready isolates a bad item so its batch siblings survive (a prior fix), but the bad item itself was always dropped entirely — losing that chunk's RAG signal outright.embedSingleTextexactly one additional retry, specifically for a detected context-length overflow (narrow regex match on the provider's own error text, so this never fires for an unrelated error), at a conservatively truncated length (4000 chars — safe even at a pessimistic ~1 char/token ratio for very dense content). A truncated-but-present vector still carries retrieval signal, which beats losing the chunk's context entirely.CHUNK_CHARSitself — bge-m3 was chosen specifically for its large context window to minimize vector count per file (documented design goal), and shrinking the general chunk budget to marginally reduce a rare edge case would work against that for the 99%+ case that already embeds fine.Scope
wantedPaths(src/,test/)site/,CNAME,**/lovable/**CHANGELOG.mdeditValidation
npx vitest run test/unit/rag.test.ts— 98/98 pass, including 4 new tests covering: truncate-retry succeeds, truncate-retry also fails (bounded to exactly one retry, no infinite loop), a non-context-length error does NOT retry, and a short context-length-erroring text does NOT retry (nothing smaller to try)npx vitest run test/unit/rag.test.ts --coverage --coverage.include='src/review/rag.ts'— 100% branch coverage on every line this PR touches (confirmed via a before/after diff of the coverage report; the one pre-existing uncovered branch is unrelated and unchanged)npx tsc --noEmit— clean on changed filesnpm audit --audit-level=moderate— 0 vulnerabilitiesgit diff --check— cleanSafety