refactor(runtime): dedup UTF-8 byte counters to shared measureUtf8ByteLength - #12136
refactor(runtime): dedup UTF-8 byte counters to shared measureUtf8ByteLength#12136YuriNachos wants to merge 1 commit into
Conversation
|
/track-community-pr |
…eLength
The three runtime search-bound modules (provider / repo-ref / file) each
carried a byte-identical private getCodePointUtf8ByteLength plus a hand-rolled
UTF-8 byte-counting loop. The canonical src/shared/utf8-byte-limits.ts already
exports measureUtf8ByteLength — the same loop, with the same early-exit on the
byte budget and the same surrogate-pair step. This finishes the extraction the
shared module was clearly meant to replace.
Each bound helper now delegates to measureUtf8ByteLength(text, { stopAfterBytes
}). Behaviour is 1:1 (the three co-located \*.test.ts suites stay green), and a
future UTF-8 edge-case fix lands once instead of three times.
Co-authored-by: Claude <noreply@anthropic.com>
41c11c9 to
a46e1b3
Compare
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (3)
🚧 Files skipped from review as they are similar to previous changes (3)
📝 WalkthroughWalkthroughThe file, provider, and repository runtime search-bound validators now use the shared 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Greptile SummaryThis PR removes three duplicate private UTF-8 byte-counting implementations from co-located
Confidence Score: 5/5Safe to merge — the refactor is a clean mechanical substitution of three identical private implementations with an already-tested shared helper. The old loop ( Files Needing Attention: No files require special attention.
|
| Filename | Overview |
|---|---|
| src/renderer/src/runtime/runtime-file-search-bounds.ts | Removes private getCodePointUtf8ByteLength + manual loop; delegates to measureUtf8ByteLength with stopAfterBytes. Semantically identical. |
| src/renderer/src/runtime/runtime-provider-search-bounds.ts | Same dedup as the other two files; null/undefined early-return guard is preserved before the delegate call. |
| src/renderer/src/runtime/runtime-repo-search-bounds.ts | Removes private helper and manual loop; delegates to measureUtf8ByteLength. Logic is unchanged. |
Flowchart
%%{init: {'theme': 'neutral'}}%%
flowchart TD
A[isRuntimeProviderSearchQueryWithinLimit] -->|null/undefined| B[return true]
A -->|string| C[measureUtf8ByteLength]
D[isRuntimeRepoRefSearchQueryWithinLimit] --> C
E[isRuntimeFileSearchTextWithinLimit] --> C
C -->|stopAfterBytes = maxBytes| F{byteLength > maxBytes?}
F -->|yes| G[exceededLimit: true → return false]
F -->|no| H[exceededLimit: false → return true]
Reviews (1): Last reviewed commit: "refactor(runtime): dedup UTF-8 byte coun..." | Re-trigger Greptile
|
Closing for now — this has gone stale against |
Summary
Three co-located modules in
src/renderer/src/runtime/each carried a byte-identical privategetCodePointUtf8ByteLengthand a byte-identical ~12-line UTF-8 byte-counting loop:runtime-provider-search-bounds.ts(isRuntimeProviderSearchQueryWithinLimit)runtime-repo-search-bounds.ts(isRuntimeRepoRefSearchQueryWithinLimit)runtime-file-search-bounds.ts(isRuntimeFileSearchTextWithinLimit)The canonical
src/shared/utf8-byte-limits.tsalready exportsmeasureUtf8ByteLength— the same loop, with the samestopAfterBytesearly-exit and the same surrogate-pairindex += 1step. This finishes the extraction the shared module was clearly meant to replace (same shape as the recent #12078 FrameDecoder collapse, #12082 provider-contract share, #12091 push-target reuse).Each bound helper now delegates:
The
providervariant keeps itsnull | undefined → trueguard; therepoandfilevariants take non-nullable input and delegate directly.Screenshots
No visual change — pure refactor.
Testing
pnpm lintpnpm typecheckpnpm testPassed:
*.test.tssuites already pin the multibyte + ASCII-oversize + null/undefined behaviour each helper must preserve:runtime-provider-search-bounds.test.ts—undefined/null→ true;'😀'@ 3 → false;'x'.repeat(9*1024)→ false.runtime-repo-search-bounds.test.ts—'😀'@ 3 → false;'x'.repeat(3*1024)→ false.runtime-file-search-bounds.test.ts—'😀'@ 3 → false; oversizedincludePattern→'includePattern'.byteLength > maxBytescheck, including the surrogateindex += 1step.pnpm typecheckgreen across all three tsconfigs (node, tc.cli, tc.web).pnpm exec oxlintclean (exit 0).git diff --stat= 3 files, -66 net lines (8 insertions, 74 deletions).AI Review Report
stopAfterBytesshort-circuit matches the old> maxBytescheck exactly.Security Audit
Pure refactor: three private byte-counter copies replaced by a call to an existing, already-tested shared helper. No new input handling, IPC, auth, path, or dependency surface. The shared
measureUtf8ByteLengthis already imported across the codebase (clipboard-text.ts,terminal-input.ts, etc.).Notes
measureUtf8ByteLengthreturns{ byteLength, exceededLimit }; the bound helpers consume only.exceededLimit, which is exactly the oldbyteLength > maxBytesoutcome.getCodePointUtf8ByteLengthcopies exist elsewhere (e.g.monaco-large-text-paste.ts,comment-body-submit-state.ts); they carry additional logic (getNextChunkBoundary) and are a cleaner separate follow-up, intentionally out of scope to keep this PR small.