Skip to content

test: stabilize token boundary fixtures - #1015

Merged
lizhengfeng101 merged 1 commit into
alibaba:mainfrom
Qiyuanqiii:fix/token-boundary-fixture
Sep 22, 2026
Merged

lizhengfeng101 merged 1 commit into
alibaba:mainfrom
Qiyuanqiii:fix/token-boundary-fixture

Conversation

@Qiyuanqiii

@Qiyuanqiii Qiyuanqiii commented Aug 20, 2026 •

Copy link
Copy Markdown
Contributor

Description

Summary

  • Replace the review and full-scan boundary fixtures with a verified fragment that stays token-exact under both cl100k_base and OCR's byte-length fallback.
  • Preserve the exact 80-token-kept and 81-token-dropped assertions in both packages.
  • Remove the boundary tests' dependency on successful tokenizer initialization.

Root Cause and Scope

The previous helper built its fixture with strings.TrimSpace(strings.Repeat("a ", n)). For n = 80, that produces 159 bytes. When cl100k_base is available, the tested encoding reports 80 tokens. When tiktoken.GetEncoding fails because the encoding data cannot be downloaded or cached, CountTokens silently falls back to len([]byte(text)) / 4, which reports 159 / 4 = 39. This matches the exact "39, want 80" failure from #1012.

The evidence therefore points to the tokenizer fallback path rather than a BPE merge of the repeated "a " pattern. This PR fixes the boundary-test fixture for both supported counting paths. It does not change production tokenizer loading or fallback behavior.

Implementation

  • Repeat the ASCII fragment "test" exactly n times in each exactNTokens helper.
  • "test" is four bytes and one cl100k_base token in the repeated fixture, so both the real tokenizer and the four-bytes-per-token fallback report exactly n.
  • Keep the explicit llm.CountTokens assertion so future fixture drift still fails loudly.
  • Apply the same fixture to internal/agent and internal/scan.

Compatibility and User Impact

  • This is a test-only change; production behavior and public APIs are unchanged.
  • The 80% filtering threshold and its boundary assertions are unchanged.
  • The affected tests now pass whether tokenizer data is available or the byte-length fallback is active.

Type of Change

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Refactoring (no functional changes)
  • Documentation update
  • CI / Build / Tooling

How Has This Been Tested?

Reproduction Environment

  • OS: Windows 11, build 26200, amd64
  • Baseline: 699c914
  • Go: go1.25.5 windows/amd64
  • Tokenizer dependency: github.com/pkoukk/tiktoken-go v0.1.8

Baseline Reproduction

At the unmodified baseline, tokenizer initialization was forced onto the fallback path by making the tokenizer cache location unusable:

--- FAIL: TestFilterLargeDiffs_Boundary
    agent_test.go:630: fixture drift: llm.CountTokens(<80-token string>) = 39, want 80

--- FAIL: TestFilterLargeScans_Boundary
    agent_test.go:280: fixture drift: llm.CountTokens(<80-token string>) = 39, want 80

Red/Green Regression Tests

Before the change, both boundary tests failed under the forced fallback. After the change, both the normal tokenizer path and a forced offline fallback path pass:

=== RUN   TestFilterLargeDiffs_Boundary
[ocr] Skipping over-limit.go (~81 tokens exceeds 80% of max_tokens(100))
--- PASS: TestFilterLargeDiffs_Boundary

=== RUN   TestFilterLargeScans_Boundary
[ocr] Skipping over-limit.go (~81 tokens exceeds 80% of max_tokens(100))
--- PASS: TestFilterLargeScans_Boundary

Final Validation

  • Focused boundary tests with normal cl100k_base loading
  • Focused boundary tests with an isolated empty cache and unreachable proxy to force the byte-length fallback
  • License-header and English-only source checks
  • go mod tidy, gofmt -s -w ., and go vet across the repository package set
  • Full non-extension Go test suite with -v -race -count=1
  • Repository coverage check: 90.8%, above the 90% threshold
  • OpenCodeReview self-review rerun with an explicit test-file include (2/2 files completed, 0 findings)
  • git diff --check and LF-only verification for both changed files

Checklist

  • My code follows the project's coding style
  • I have performed a self-review of my code
  • I have added or updated tests that prove the fix is effective
  • New and existing unit tests pass locally with my changes
  • No user-facing documentation changes are required
  • I have signed the CLA

Known Limitations

  • The original macOS/arm64 environment was not available locally; the same fallback result was reproduced deterministically on Windows.
  • GNU Make was unavailable on this Windows host, so the commands behind make check, make test, and make coverage were run directly.
  • The GitHub Actions OCR job completed with no selected items because both changed paths are Go test files, which OCR excludes by default. A local rerun with deepseek-v4-flash and a temporary **/*_test.go include rule reviewed both files successfully and reported no findings.

Related Issues

Closes #1012.

@github-actions

Copy link
Copy Markdown
Contributor

✅ OpenCodeReview: Review skipped: no items were selected.

@Qiyuanqiii

Copy link
Copy Markdown
Contributor Author

The original issue was reported on macOS. Since I don’t have access to a macOS device, I’m asking others to verify whether this has been successfully fixed.

@Qiyuanqiii
Qiyuanqiii marked this pull request as ready for review August 20, 2026 07:23

@lizhengfeng101 lizhengfeng101 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@lizhengfeng101
lizhengfeng101 merged commit 7003e01 into alibaba:main Sep 22, 2026
13 checks passed
@Qiyuanqiii
Qiyuanqiii deleted the fix/token-boundary-fixture branch September 22, 2026 01:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TestFilterLargeDiffs_Boundary fails because exactNTokens fixture is not token-exact

3 participants