feat(review): enforce an aggregate token budget across the AI review prompt - #3941
Conversation
…prompt buildUserPrompt concatenated six independently-capped context sections (grounding, RAG, impact-map, enrichment, culture-profile, test-evidence) with no combined limit. Every section enforces its own cap, but with every convergence feature enabled on one repo the worst-case assembled prompt exceeds 200,000 characters before the system prompt is even added -- degrading signal-to-noise on exactly the large/complex PRs that most need focused attention. The only place the combined length was consulted was the neuron-budget cost estimate, which affects billing accounting, not what's actually sent. Add a priority-ordered cutoff (grounding > RAG > impact-map > enrichment > culture-profile > test-evidence): walk sections highest-priority-first, including each while it fits, and stop entirely once one would overflow -- a predictable priority cutoff, not a bin-packing optimization. The 200,000-char ceiling sits comfortably above diff+description+grounding's own worst case (~182k) so grounding is effectively never trimmed; normal single/few-feature cases stay byte-identical since they never approach the ceiling. Closes #3900
|
Superagent didn't find any vulnerabilities or security issues in this PR. |
|
Warning 🟨🟨🟨🟨🟨🟨🟨🟨🟨🟨🟨🟨 ⏸️ Gittensory review result - manual review recommendedReview updated: 2026-07-07 07:37:55 UTC
⏸️ Suggested Action - Manual Review Review summary Nits — 6 non-blocking
Review context
Contributor next steps
Signal definitions
🟩 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 Gittensory, a quiet PR intelligence layer for OSS maintainers.
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #3941 +/- ##
=======================================
Coverage 93.68% 93.68%
=======================================
Files 372 372
Lines 34891 34902 +11
Branches 12767 12769 +2
=======================================
+ Hits 32688 32699 +11
Misses 1584 1584
Partials 619 619
🚀 New features to boost your workflow:
|
Summary
buildUserPromptconcatenates six independently-capped context sections (grounding, RAG, impact-map, enrichment, culture-profile, test-evidence). Every section enforces its own cap (FILE_CONTENT_BUDGET,MAX_CONTEXT_CHARS,MAX_PROMPT_CHARS,MAX_ENRICHMENT_PROMPT_SECTION_CHARS...), but nothing bounded the combined total: with every convergence feature enabled on one repo, the worst-case assembled prompt exceeds 200,000 characters before the system prompt is even added.estimateNeurons(system.length + user.length, ...)— a cost-quota estimate for billing accounting, with zero effect on what's actually sent to the model.selectContextSectionsWithinBudget: a priority-ordered hard cutoff. Sections are evaluated highest-priority-first (grounding > RAG > impact-map > enrichment > culture-profile > test-evidence); once one would push the running total over the ceiling, that section AND every lower-priority one after it are dropped. This is a predictable priority cutoff, not a bin-packing optimization that could skip a large blocked section to squeeze in a smaller, lower-priority one.AGGREGATE_CONTEXT_BUDGET_CHARS = 200_000sits comfortably above diff+description+grounding's own worst case (~182k chars: 120k diff + 2k description + 60k grounding), so grounding is effectively never trimmed. Normal single/few-feature repos stay byte-identical since their combined total never approaches the ceiling — this only changes behavior for the genuine "every feature enabled together" case.Found via a fresh performance/scalability/accuracy hardening audit of the self-host ORB stack. Tracked under #1667.
Scope
src/services/ai-review.ts—selectContextSectionsWithinBudget+ wiring intobuildUserPrompttest/unit/ai-review.test.ts— unit tests for the cutoff helper (inclusion, hard-cutoff-not-bin-packing, absent-section handling, exact boundary) + integration tests viabuildUserPrompt(byte-identical when under budget, correct priority-ordered trimming when over, grounding never trimmed at its own real-world max)Validation
npm run typechecknpx vitest run test/unit/ai-review.test.ts test/unit/ai-review-advisory.test.ts test/unit/ai-review-cache.test.ts test/unit/ai-review-cache-input.test.ts --coverage— 248/248 passing; changed lines fully covered (remaining uncovered lines are pre-existing and untouched by this diff)npm run test:coverage(full unsharded) — 560 files / 11115 tests passedgit diff --checkcleanSafety
Closes #3900