Skip to content

feat(review): enforce an aggregate token budget across buildUserPrompt's context sections #3900

Description

@JSONbored

Context

buildUserPrompt (src/services/ai-review.ts) concatenates six independently-capped context sections: the diff (120,000 chars), PR description (2,000 chars), grounding (FILE_CONTENT_BUDGET 60,000 chars), RAG context (MAX_CONTEXT_CHARS 14,000), impact-map context (MAX_PROMPT_CHARS 6,000), culture-profile context, and enrichment (MAX_ENRICHMENT_PROMPT_SECTION_CHARS 8,000). Every section individually enforces its own cap — a genuinely good, repeated pattern in this codebase — but nothing bounds or prioritizes the combined result. With every convergence flag enabled together on one repo (grounding + RAG + impact-map + culture-profile + enrichment), the worst-case user prompt alone is ~208,000+ characters before the system prompt is even added.

The only place the combined length is consulted at all is the estimateNeurons(system.length + user.length, ...) call — that's a cost-quota estimate for billing accounting, not a truncation or priority mechanism, so it has zero effect on what's actually sent to the model. As more convergence features get enabled together per repo, the combined prompt silently grows without any section yielding budget to another, degrading signal-to-noise on exactly the large/complex PRs that most need focused attention.

Requirements

  • Add a single aggregate char/token budget check in (or wrapping) buildUserPrompt, applied after all sections are assembled.
  • Define an explicit truncation priority order (diff first, since it's the primary review target, then grounding, RAG, impact-map, enrichment, culture-profile last) that trims lower-priority sections first when the total would exceed the chosen ceiling.

Acceptance criteria

  • With every convergence feature enabled simultaneously, the assembled user prompt never exceeds the new aggregate ceiling.
  • A test with all sections enabled and deliberately oversized verifies the correct truncation order (diff preserved in full, lowest-priority sections trimmed first).
  • Normal single/few-feature cases are byte-identical to today (no truncation triggers below the ceiling).

Parent: #1667

Metadata

Metadata

Assignees

Labels

maintainer-onlyOwner-only work — yields no Gittensor points.

Projects

Status
Done

Relationships

None yet

Development

No branches or pull requests

Issue actions