Skip to content

fix(tokens): count literal special-token text safely - #19520

Merged
DeJeune merged 1 commit into
CherryHQ:mainfrom
userInner:fix/tokenizer-literal-special-tokens
Aug 27, 2026
Merged

fix(tokens): count literal special-token text safely#19520
DeJeune merged 1 commit into
CherryHQ:mainfrom
userInner:fix/tokenizer-literal-special-tokens

Conversation

@userInner

Copy link
Copy Markdown
Contributor

Branch strategy

  • Active development targets main.

What this PR does

Before this PR:

The exact o200k token-budget estimator used gpt-tokenizer's default special-token policy. Persisted or user-authored text containing a literal such as <|im_start|> therefore threw before the provider request was created, and the persisted error text could trigger the same failure on the next send.

After this PR:

The estimator counts special-token spellings as ordinary BPE text. It does not mutate message content or treat those spellings as encoder control tokens. Regression coverage exercises all five literals reported in the issue, both alone and inside surrounding prose.

Refs #19465

Why we need it and why it was done in this way

This tokenizer boundary estimates budgets over arbitrary user and persisted content; it is not the provider encoder. gpt-tokenizer exposes disallowedSpecial, so an empty set expresses the required semantics directly at the narrow adapter boundary and keeps every downstream budget consumer safe.

The following tradeoffs were made:

  • Literal special-token spellings count as multiple ordinary text tokens, which is intentionally more conservative than counting each spelling as one control token.
  • The options object and empty set are allocated once with the lazily loaded tokenizer.

The following alternatives were considered:

  • Sanitizing or removing the literals would change user content.
  • Allowing all special tokens would count the literals as control tokens and understate ordinary-text usage.
  • Catching the exception and falling back to the heuristic tokenizer would make estimates depend on message content.

Links to places where the discussion took place: #19465 (comment)

Breaking changes

None.

Special notes for your reviewer

Validation completed locally:

  • Confirmed the regression test fails on the previous implementation for <|im_start|>, <|im_end|>, and <|endoftext|>.
  • pnpm exec vitest run --project main src/main/ai/tokens/__tests__ (55 tests)
  • Full main-process Vitest suite
  • pnpm typecheck:node
  • Biome, Oxlint, ESLint, and git diff --check
  • pnpm build

Checklist

This checklist is not enforcing, but it's a reminder of items that could be relevant to every PR.
Approvers are expected to review this list.

Release note

Fixed chat sends failing when message history contains literal special-token text.

Signed-off-by: userInner <1239989762@qq.com>
@userInner
userInner requested a review from DeJeune as a code owner August 26, 2026 18:09

@kangfenmao kangfenmao left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM

@DeJeune
DeJeune merged commit ad506ac into CherryHQ:main Aug 27, 2026
20 of 21 checks passed
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.

3 participants