fix(llm): install the embedded BPE loader from package init - #1228
Merged
lizhengfeng101 merged 1 commit intoSep 14, 2026
Merged
lizhengfeng101 merged 1 commit into
lizhengfeng101 merged 1 commit into
Conversation
Token counting fell back to a len(text)/4 byte estimate whenever the tiktoken encoding could not be downloaded, because InitEmbeddedLoader was only called by the CLI binary. Installing it from the package init keeps every consumer, tests included, on the embedded data. Fixes alibaba#1012
Contributor
|
✅ OpenCodeReview: Review complete: 0 finding(s) across 2 selected item(s). |
Contributor
|
@basil-k-aji-dev Great job! I actually ran into this exact problem locally yesterday! |
Githab-capibara
added a commit
to Githab-capibara/open-code-review
that referenced
this pull request
Sep 17, 2026
…1228) Token counting fell back to a len(text)/4 byte estimate whenever the tiktoken encoding could not be downloaded, because InitEmbeddedLoader was only called by the CLI binary. Installing it from the package init keeps every consumer, tests included, on the embedded data. Fixes alibaba#1012
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
TestSelectFilesTooLarge_Boundary(the test #1012 reports under its former nameTestFilterLargeDiffs_Boundary) fails on a clean checkout withfixture drift: llm.CountTokens(<80-token string>) = 39, want 80.The issue attributes this to BPE merging in the
exactNTokensfixture, but that is not the cause. The fixture is fine —strings.Repeat("a ", 80)really is 80cl100k_basetokens. The failure comes from token counting silently degrading:InitEmbeddedLoader()was only called fromcmd/opencodereview/main.go.internal/llm— the whole test suite included — therefore left tiktoken on its default loader, which downloads the encoding file over the network.countTokensWithEncodingfalls back tolen(text)/4.For the fixture,
len("a a a ... a") / 4=159 / 4= 39, which is exactly the number in the issue report.So the test does not fail because of the tokenizer; it fails on any machine where the encoding download is blocked or unavailable, and passes everywhere else. That also explains why it is not failing for everyone.
This affects two tests, not just the one in the issue —
internal/scan'sTestSelectScanItems_LargeBoundaryshares the same helper and fails identically.Fix
Install the embedded BPE loader from the
internal/llmpackageinit()rather than relying on an explicit startup call. The embedded data already ships in the package; it was just not wired up unlessmain()asked for it.InitEmbeddedLoader()stays exported and is now redundant inmain(), so that call is removed.This is more than a test fix: the silent
len(text)/4fallback affects real token budgeting for any consumer that does not go through the CLI entry point.Why not adjust the fixture instead
#1012 suggests building the fixture from the tokenizer's own output. That would make the test pass under the degraded byte estimate too, hiding the real defect. Keeping the strict
exactNTokenscheck leaves it as a canary, which is what caught this.Type of Change
How Has This Been Tested?
make testpasses locallyReproduced the failure deterministically by pointing
TIKTOKEN_CACHE_DIRat an empty directory and blackholing the proxy env, which makes the encoding download fail:With this change, the full suite passes under those same offline conditions, and
make checkandmake buildpass.Added
TestCountTokensUsesEmbeddedBpeData, which never callsInitEmbeddedLoaderand asserts the count is not thelen(text)/4estimate. I verified it fails (= 39) when the newinit()is removed.One environment note, for accuracy:
make testuses-race, and the race detector cannot start on my arm64 box (ThreadSanitizer: unsupported VMA range, Found 47 - Supported 48) for any package, onmainas well as on this branch. I ran the identical package list withLC_ALL=C go test -count=1instead; all 23 packages pass. CI will exercise the-racepath.Checklist
go fmt,go vet)AI disclosure: this change was developed with AI assistance (Claude Opus 5, via Claude Code). I reviewed the diff and the reasoning myself, no commits are attributed to AI, and I will answer maintainer questions in my own words.
Related Issues
Closes #1012