Skip to content

fix(tokens): count literal special-token markers as ordinary text - #1062

Merged
dovvnloading merged 1 commit into
mainfrom
agent/literal-token-estimation-1041
Nov 25, 2026
Merged

dovvnloading merged 1 commit into
mainfrom
agent/literal-token-estimation-1041

Conversation

@dovvnloading

@dovvnloading dovvnloading commented Nov 25, 2026 •

Copy link
Copy Markdown
Owner

Summary

Fixes #1041. Graphlink estimates tokens for arbitrary chat and document text, but tiktoken's default encode() rejects strings matching reserved special-token spellings. Use encode_ordinary() instead, so literal markers are counted as ordinary text rather than rejected or interpreted as control-token IDs. Empty input and the existing character-count fallback are unchanged.

What Changed

  • Change the shared TokenEstimator.count_tokens implementation; no separate ingestion workaround or changes to provider request encoding.
  • Add parameterized tests using the real tiktoken API with a tiny byte vocabulary that requires no download, plus explicit ordinary-encoding dispatch and fallback tests.
  • Add a real file-to-SQLite knowledge ingestion regression that checks literal markers, exact stored text, token counts and source offsets.

Why

  • Tokenizer documentation, code examples and other ordinary input should not fail merely because they mention reserved token spellings.
  • The shared fix covers both context estimation and knowledge chunking while preserving the existing fallback and singleton behavior. It does not enable special-token IDs in user text.

Validation

  • Isolated pytest checks using a mock tokenizer and contract double against the exact repository estimator and knowledge chunking source: 2 passed / 4 failed before; 6 passed / 0 failed after. Two are committed estimator tests and four are isolated chunking checks covering literal markers, ordinary input, Unicode and preserved offsets/counts.
  • Python compileall for the estimator, unchanged chunking module and new tests.
  • Verified local tested files match remote Git blob hashes; branch comparison shows one commit touching only two intended files and zero commits behind main at comparison time.
  • App launches
  • Relevant workflow was exercised manually
  • python -m compileall -q . passes (run from the repo root - matches CI)
  • python -m pytest -q passes (run from the repo root - matches CI)
  • cd web_ui && npm run check passes (matches CI)

Local full checkout is blocked by GitHub DNS resolution, and tiktoken cannot be installed in this environment. The five real-tokenizer parameterized cases and file-to-SQLite regression are committed but not run locally. The isolated checks do not load the full application or repository conftest. Full CI, lint, dependency checks, and manual verification remain outstanding.

Focused verification in a configured Python 3.12 checkout:

python -m pytest -q -n0 backend/tests/test_token_estimator.py backend/tests/test_knowledge_chunking.py backend/tests/test_knowledge_ingest.py backend/tests/test_knowledge_jobs.py

Manual verification: import a UTF-8 .txt document containing literal <|endoftext|> and <|fim_prefix|> markers into a temporary workspace, confirm the import succeeds, search for the surrounding text, and verify the stored excerpt retains both marker spellings. Also enter ordinary text and literal markers in the chat composer and confirm the token estimate remains usable.

UI Notes

  • Screenshots included if the change is visual
  • No screenshots needed

Additional Context

No dependency, schema, frontend, CI or unrelated changes. No direct changes to main.

Maintainer-requested review and validation update

All four required PR CI jobs passed (run 45588129470), including the full Windows pytest suite with coverage (8,293 passed, 56 skipped, 22 subtests passed), 16 serial performance tests, compile/lint/audit, POSIX permissions and Bash E2E, frontend checks, and wheel build/install/import. The prior local-validation limitations above are retained for transparency. App launch and manual native UI verification remain unchecked; the reviewed real-tokenizer and file-to-SQLite regression tests provide automated verification of this backend fix. No independent approval is claimed and no required gate is bypassed. See the submitted review for details.

Add offline-vocabulary token estimator and knowledge ingestion regressions for #1041.

@dovvnloading dovvnloading left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Maintainer-requested code review

Verdict: no blocking code findings; suitable for squash merge on the verified automated gates.

  • Reviewed both changed files and issue #1041, and traced the shared estimator through backend/token_counter.py and backend/knowledge_chunking.py. encode_ordinary() counts marker spellings as user text, rather than raising or enabling special-token IDs; empty input and missing-encoding fallback are preserved.
  • The real-tiktoken byte-vocabulary regression, explicit dispatch/fallback tests and file-to-SQLite ingestion regression cover the failure mode, exact stored text, token counts and offsets. No provider encoding, dependency, schema or CI changes.
  • CI run 45588129470 succeeded on the reviewed head: Windows Python checks (8,293 passed, 56 skipped, 22 subtests passed; separate performance suite 16 passed), POSIX permissions/Bash E2E, frontend checks and wheel build/install/import. The optional nightly Playwright job is skipped by design on PRs per AGENTS.md.
  • Compared the current main with the branch merge base: three intervening commits touch unrelated DAG/layout, workflow preset and documentation files, with no overlap with this fix. GitHub reports a clean merge and there are no outstanding review threads.

Limitations: no independent local native desktop launch or manual composer/import walkthrough; local GitHub DNS resolution and missing tiktoken prevent a configured checkout here. The automated real-tokenizer and ingestion tests are the basis for proceeding at the maintainer's request; manual UI verification remains unchecked. This is a review comment, not independent approval, because the connected account is also the PR author. No checks or branch protections are being overridden.

@dovvnloading
dovvnloading marked this pull request as ready for review November 25, 2026 10:22
@dovvnloading
dovvnloading merged commit d524dc2 into main Nov 25, 2026
5 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.

Literal tokenizer special-token strings cause token estimation and document indexing to fail

1 participant