fix: address final review findings
- Comment: "~4 characters" → "~4 bytes" (len() counts bytes, not runes) - Use utf8.RuneStart from stdlib instead of custom isUTF8Start helper - Skip diff block entirely when Diff is empty (handles edge cases: draft→ready with no delta, force-push matching base, etc.)
This commit is contained in:
+8
-12
@@ -8,6 +8,7 @@ package budget
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
// modelLimit pairs a model name prefix with its context window size.
|
||||
@@ -38,7 +39,7 @@ const diffTooLargeMarker = "... [diff too large for context window — review ma
|
||||
const userMetaTruncMarker = "\n... [description truncated] ..."
|
||||
|
||||
// EstimateTokens estimates the number of tokens in a string.
|
||||
// Uses the rough heuristic of ~4 characters per token, which is
|
||||
// Uses the rough heuristic of ~4 bytes per token, which is
|
||||
// conservative for English text and code.
|
||||
func EstimateTokens(s string) int {
|
||||
return len(s) / 4
|
||||
@@ -188,9 +189,11 @@ func buildResult(s Sections, trimmed []string, estTokens int) Result {
|
||||
usr.WriteString(s.FileContext)
|
||||
usr.WriteString("\n")
|
||||
}
|
||||
usr.WriteString("\n### Diff (changes to review)\n\n```diff\n")
|
||||
usr.WriteString(s.Diff)
|
||||
usr.WriteString("\n```\n")
|
||||
if s.Diff != "" {
|
||||
usr.WriteString("\n### Diff (changes to review)\n\n```diff\n")
|
||||
usr.WriteString(s.Diff)
|
||||
usr.WriteString("\n```\n")
|
||||
}
|
||||
|
||||
if len(trimmed) > 0 {
|
||||
usr.WriteString("\n⚠️ Note: Context was trimmed to fit model limits. Dropped: ")
|
||||
@@ -212,15 +215,8 @@ func truncateUTF8(s string, maxBytes int) string {
|
||||
if len(s) <= maxBytes {
|
||||
return s
|
||||
}
|
||||
// Walk backwards from maxBytes to find a valid UTF-8 boundary
|
||||
for maxBytes > 0 && !isUTF8Start(s[maxBytes]) {
|
||||
for maxBytes > 0 && !utf8.RuneStart(s[maxBytes]) {
|
||||
maxBytes--
|
||||
}
|
||||
return s[:maxBytes]
|
||||
}
|
||||
|
||||
// isUTF8Start returns true if b is a valid start byte for a UTF-8 sequence
|
||||
// (single-byte ASCII or multi-byte lead byte, not a continuation byte).
|
||||
func isUTF8Start(b byte) bool {
|
||||
return b&0xC0 != 0x80
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user