fix(tokens): warm fallback tokenizer in background (#5861)

* fix(tokens): keep fallback token estimation offline

* fix(tokens): warm fallback tokenizer in background

* test(tokens): isolate agent fixtures from tokenizer readiness

* test(tokens): keep Codex pressure fixtures within byte budget

* docs: remove manual wrapping from troubleshooting prose
This commit is contained in:
chengyongru authored and GitHub committed 2026-09-28 13:50:55 +08:00
1 parent e0b8efa0be
commit 4a7ad00e4f
16 files changed
+378 -101

No files matched your search

+43 -21
View File
@@ -1,6 +1,8 @@
import json
from nanobot.utils import helpers
import pytest
from nanobot.utils import helpers, token_encoding
from nanobot.utils.helpers import (
estimate_message_tokens,
estimate_prompt_tokens,
@@ -18,6 +20,39 @@ class _BrokenCounterProvider:
raise RuntimeError("counter unavailable")
@pytest.fixture(autouse=True)
def isolate_token_caches(monkeypatch, byte_encoding):
monkeypatch.setattr(token_encoding, "_encoding", byte_encoding)
helpers._TOOLS_TOKEN_CACHE.clear()
yield
helpers._TOOLS_TOKEN_CACHE.clear()
@pytest.mark.parametrize("text", ["hello world", "你好,世界🙂", "print(123)\n", "<|endoftext|>"])
def test_token_estimation_treats_prompt_text_as_ordinary(text, byte_encoding):
expected = len(byte_encoding.encode_ordinary(text)) + 4
assert estimate_prompt_tokens_chain(
_NoCounterProvider(), "custom-model", [{"role": "user", "content": text}],
) == (expected, "tiktoken")
assert estimate_message_tokens({"role": "user", "content": text}) == expected
assert len(byte_encoding.encode_ordinary(truncate_text_to_tokens(text * 100, 40))) <= 40
def test_provider_counter_does_not_initialize_fallback(monkeypatch):
def forbidden():
raise AssertionError("provider counter must take precedence")
class Provider:
def estimate_prompt_tokens(self, messages, tools=None, model=None):
assert model == "provider-specific-model"
return 1234, "provider-local"
monkeypatch.setattr(helpers, "_get_token_encoding", forbidden)
assert estimate_prompt_tokens_chain(
Provider(), "provider-specific-model", [{"role": "user", "content": "hi"}],
) == (1234, "provider-local")
def test_estimate_prompt_tokens_chain_falls_back_without_provider_counter() -> None:
tokens, source = estimate_prompt_tokens_chain(
_NoCounterProvider(),
@@ -58,7 +93,9 @@ def test_estimate_prompt_tokens_uses_conservative_fallback_when_tiktoken_fails(
messages,
)
actual_tokens = len(helpers.tiktoken.get_encoding("cl100k_base").encode(content)) + 4
encoding = token_encoding.get_token_encoding()
assert encoding is not None
actual_tokens = len(encoding.encode(content)) + 4
assert tokens == len(content.encode("utf-8")) + 4
assert tokens >= actual_tokens
assert chain_tokens == tokens
@@ -92,27 +129,16 @@ def test_truncate_text_to_tokens_uses_utf8_byte_budget_fallback(monkeypatch) ->
def test_estimate_prompt_tokens_caches_tools_encoding(monkeypatch) -> None:
helpers._get_token_encoding.cache_clear()
helpers._TOOLS_TOKEN_CACHE.clear()
class FakeEncoding:
def __init__(self) -> None:
self.encoded: list[str] = []
def encode(self, text: str) -> list[int]:
def encode_ordinary(self, text: str) -> list[int]:
self.encoded.append(text)
return list(range(max(1, len(text) // 4)))
fake_encoding = FakeEncoding()
get_encoding_calls = 0
def fake_get_encoding(name: str) -> FakeEncoding:
nonlocal get_encoding_calls
assert name == "cl100k_base"
get_encoding_calls += 1
return fake_encoding
monkeypatch.setattr(helpers.tiktoken, "get_encoding", fake_get_encoding)
monkeypatch.setattr(token_encoding, "_encoding", fake_encoding)
tools = [{"type": "function", "function": {"name": "demo", "description": "cached"}}]
messages = [{"role": "user", "content": "hello"}]
@@ -120,25 +146,21 @@ def test_estimate_prompt_tokens_caches_tools_encoding(monkeypatch) -> None:
second = estimate_prompt_tokens(messages, tools)
assert first == second
assert get_encoding_calls == 1
rendered_tools = "\n" + json.dumps(tools, ensure_ascii=False)
assert fake_encoding.encoded.count(rendered_tools) == 1
def test_estimate_prompt_tokens_recomputes_when_tool_items_change(monkeypatch) -> None:
helpers._get_token_encoding.cache_clear()
helpers._TOOLS_TOKEN_CACHE.clear()
class FakeEncoding:
def __init__(self) -> None:
self.encoded: list[str] = []
def encode(self, text: str) -> list[int]:
def encode_ordinary(self, text: str) -> list[int]:
self.encoded.append(text)
return list(range(max(1, len(text) // 4)))
fake_encoding = FakeEncoding()
monkeypatch.setattr(helpers.tiktoken, "get_encoding", lambda _name: fake_encoding)
monkeypatch.setattr(token_encoding, "_encoding", fake_encoding)
tools = [{"type": "function", "function": {"name": "before"}}]
messages = [{"role": "user", "content": "hello"}]