feat: add context budget system for LLM overflow (#19) #20
Reference in New Issue
Block a user
Delete Branch "fix/19-context-overflow"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Adds a
budgetpackage that estimates token usage and progressively trims context to fit within model-specific limits.Trim Order (least important first)
When content is trimmed, a note is appended to the user prompt so the LLM knows context was reduced.
Changes
budgetpackage:Fit(),EstimateTokens(),LimitForModel()review/prompt.go:BuildSystemBase()andBuildUserMeta()extract non-trimmable contentmain.gousesbudget.Fit()instead of direct prompt assemblyCloses #19
Summary
The new budget package and prompt refactor integrate cleanly and the trimming logic is reasonable and well-tested. I found only minor issues and suggestions for robustness and clarity; CI passed so no blocking problems were detected.
Findings
budget/budget.gobudget/budget.gobudget/budget.gobudget/budget.gobudget/budget.goRecommendation
APPROVE — Overall this change is well implemented: the new budget package cleanly separates trimmable vs non-trimmable content, integrates into main, and is covered by unit tests that exercise trim and truncation paths. I recommend merging with minor follow-ups: (1) make LimitForModel prefix matching deterministic (iterate over a sorted list of prefixes or compute longest-prefix match), (2) document or provide a way to plug in a more accurate tokenizer than the 1 token ≈ 4 chars heuristic, (3) clarify the fallback when available < 0 (comment or adjust the heuristic), and (4) consider making the diffBudget minimum configurable or documented. These are non-blocking improvements — given CI passed and tests cover the behavior, this can be approved.
Summary
The new budget system is well-structured and the refactor in main.go looks clean, with solid test coverage for most paths. However, there is a critical edge case where diff truncation can still exceed the model budget due to not accounting for the appended truncation marker, which defeats the primary purpose of this feature.
Findings
budget/budget.gobudget/budget.gobudget/budget.gobudget/budget.goreview/prompt.goreview/prompt.goRecommendation
REQUEST_CHANGES — Address the budget overshoot by accounting for the truncation marker when slicing the diff: define the marker string as a constant, subtract its estimated token cost (or exact length) from diffBudget before computing maxChars, and ensure finalTokens never exceed the limit (e.g., recompute and adjust if necessary). Add a unit test that exercises the diff-truncation path and asserts EstTokens <= limit. Update the misleading comment about "keep first 1000 chars" to match the 4000-character logic (or change the logic to 1000 if that was intended). For robustness, switch to rune-safe truncation for both UserMeta and Diff to avoid cutting multi-byte UTF-8 characters. Finally, add tests for the new exported functions BuildSystemBase and BuildUserMeta to comply with repository conventions.
Summary
Solid addition of a budget-aware context fitting system with good test coverage. The trimming logic and refactor to separate non-trimmable content are well-executed and CI passes.
Findings
budget/budget.gobudget/budget.gobudget/budget.gobudget/budget.goRecommendation
APPROVE — Overall, the feature is well-designed and tested, and CI has passed. Recommend merging with minor tweaks: (1) compute the 'removed' tokens message using effectiveBudget to avoid negative or misleading counts; (2) include a small fixed overhead in EstTokens for the static wrappers (headers, fences, and trimming note) or subtract it from reserveTokens; (3) in Fit, if baseTokens still exceed the limit after initial UserMeta truncation, iteratively reduce UserMeta further to ensure final tokens never exceed the limit even in pathological cases; (4) rename the 's' parameter in buildResult for readability. These are refinements and do not block merging.
Summary
Well-structured addition of a budget package with sensible trimming order, clear separation of trimmable/essential prompt sections, and thorough tests. CI passed and the integration into main is clean and backwards-compatible via deprecated helpers.
Findings
budget/budget.gobudget/budget.gobudget/budget.gobudget/budget.goRecommendation
APPROVE — The implementation is solid: token estimation, deterministic model limit matching, and progressive trimming align with the stated trim order. The system/user prompt split is clean and main.go integration respects timeouts and logging. CI is green and tests cover key paths, including over-budget scenarios and ensuring final estimates remain within limits. To further improve robustness, consider: (1) enforcing or testing model limit ordering so new entries don’t accidentally match broader prefixes; (2) adding UTF-8 multi-byte truncation tests; and (3) polishing minor UX details in truncation reporting and empty diff handling. None of these are blockers — approve as is.
Summary
The new budget package and prompt refactors are well-structured, tests cover trimming paths, and CI passed. A few minor issues and suggestions around heuristics, magic constants, and a subtle UTF-8 boundary check are noted below.
Findings
budget/budget.gobudget/budget.gobudget/budget.gobudget/budget.gocmd/review-bot/main.goRecommendation
APPROVE — Approve: The implementation is correct, tests are comprehensive, and CI passed. The suggestions above are low-risk improvements: document the 4000-byte truncation choice or perform truncation in token units; clarify the EstimateTokens heuristic and add a comment that it's intentionally approximate; add a clarifying comment in truncateUTF8 to make the boundary logic explicit; and optionally add validation for the modelLimits ordering. None of these prevent merging, but addressing them will improve clarity and robustness.
Summary
The PR cleanly adds a budget package to trim LLM prompt context and integrates it into prompt assembly. Tests and CI pass; the implementation is sensible and well-tested. I have a few minor suggestions to improve robustness and clarity (UTF-8 truncation, token-estimate documentation, and an edge-case when untrimmable base exceeds the budget).
Findings
budget/budget.gobudget/budget.gobudget/budget.gobudget/budget.goreview/prompt.gobudget/budget.go.gitea/workflows/ci.ymlRecommendation
APPROVE — Approve. The new budget package is a solid addition: it cleanly separates trimmable content from core instructions, integrates with the existing prompt builders, and is covered by comprehensive unit tests. Before shipping, I recommend small improvements: (1) replace the custom UTF-8 truncation with the standard unicode/utf8 utilities (safer and clearer), (2) document the token-estimation heuristic and consider making it configurable, and (3) add an explicit handling/notice when the non-trimmable SystemBase exceeds the model budget (so callers can be alerted rather than silently producing an oversized prompt). These are low-risk enhancements — they do not block merging but will make the feature more robust and maintainable.
e7bbd55daeto565a077b01Summary
The pull request adds a robust context budget system for prompt assembly, ensuring LLM input safety. Implementation follows idiomatic Go patterns, includes thorough unit tests, and integrates seamlessly into the main pipeline.
Recommendation
APPROVE — This PR provides a well-designed budgeting mechanism that prevents LLM prompt overruns and progressively degrades non-essential context. The approach is idiomatic, tested, and easily extensible to other LLMs. No major or minor issues were found. All CI checks have passed. Merge as-is.
Summary
Solid addition of a context budgeting system with sensible trim order, clean integration, and good test coverage. CI is green and the changes are idiomatic and cohesive.
Findings
budget/budget.gobudget/budget.gobudget/budget.goRecommendation
APPROVE — The budgeting logic is well thought out, the trim order aligns with stated priorities, and tests cover the primary paths (including trimming and diff truncation). The CI workflow adjustments are appropriate. Minor polish recommendations: clarify the EstimateTokens comment to reflect byte-based estimation, consider using utf8.RuneStart in truncateUTF8 for clarity, and optionally skip the diff block when empty. Otherwise, this is good to merge.
Summary
The PR introduces a well-designed context budget system for LLM prompt assembly, with correct trimming logic and thorough test coverage. All changes adhere to repository conventions and Go idioms.
Recommendation
APPROVE — The context budget system (
budgetpackage) is robust, testable, and modular. The main prompt assembly flow is now token-aware, prioritizes important information, and never risks LLM context window overflows. All critical paths and edge trimming scenarios are tested. The refactor respects existing boundaries (logic separated from presentation), and CI passes. Merging is recommended.Summary
Well-structured addition of a budget package with sensible trim order and thorough tests. Integration into main and prompt refactor look clean and idiomatic, and CI has passed.
Findings
budget/budget.gobudget/budget.gocmd/review-bot/main.goRecommendation
APPROVE — The new budgeting system is a solid improvement with clear trim semantics and good test coverage across scenarios (patterns/conventions trimming, diff truncation, and large UserMeta). The integration in main.go is clean and the prompt refactor properly separates non-trimmable base content. Address the minor inconsistencies and edge case noted: (1) update the Sections.UserMeta comment to reflect that it may be truncated when base content alone would exceed the budget (or change the implementation to match the "never trimmed" promise), and (2) ensure that when remaining diff budget is smaller than the truncation marker, the final estimated tokens cannot exceed the limit (e.g., skip the marker in that case or trim an additional small section). The variable naming nit in fetchPatterns is optional. With these small adjustments, the change set is ready to merge.
Summary
This PR introduces a robust context window budgeting system for LLM prompt construction, with well-structured and well-tested logic and no regressions to existing functionality. All changes follow Go idioms and explicit repository conventions.
Recommendation
APPROVE — The PR creates a modular
budgetpackage that accurately estimates token usage and progressively trims less important context sections while preserving essential content for LLM prompts. The trimming strategy is well implemented and thoroughly tested, with clear prioritization and test coverage for all key trim scenarios. Refactors to prompt assembly separate non-trimmable from trimmable content cleanly, allowing easy extension. All CI checks pass and no style, correctness, or design issues are present. I recommend merging as-is.Summary
Well-scoped introduction of a budget-aware prompt assembly layer with good separation of non-trimmable vs trimmable content and comprehensive tests. CI is green, and the refactor integrates cleanly into main without disrupting existing behavior.
Recommendation
APPROVE — The new budget package is thoughtfully designed and documented, integrates safely into the prompt-building flow, and includes solid unit tests covering key trim paths. The CI workflow updates look correct, and the small refactors in review/prompt.go keep backward compatibility via deprecated helpers while enabling context-aware assembly. Consider future enhancements such as making model limits configurable and slightly biasing token estimation toward overestimation (e.g., ceiling division) to further reduce any risk of overfilling the context window, but there are no blocking issues for this change.