Files
tracer-cloud--opensre/tests/packaging/test_frozen_tiktoken_bootstrap.py
wehub-resource-sync 4b6817381b
Benchmark image — build + push to ECR (any adapter) / build + push (push) Waiting to run
CI / quality (ubuntu-latest) (push) Waiting to run
CI / test (tools-runtime) (push) Waiting to run
CI / test (e2e-general) (push) Waiting to run
CI / test (cli-runtime) (push) Waiting to run
CI / test (e2e-provider-and-openclaw) (push) Waiting to run
CI / test (integrations-and-misc) (push) Waiting to run
CI / coverage-report (push) Blocked by required conditions
CI / test-kubernetes (push) Waiting to run
CI / should-run-thorough (push) Waiting to run
CI / test-thorough (cloudwatch-demo) (push) Blocked by required conditions
CI / test-thorough (flink-ecs) (push) Blocked by required conditions
CI / test-thorough (upstream-lambda) (push) Blocked by required conditions
CI / test-thorough (prefect-ecs-fargate) (push) Blocked by required conditions
CodeQL / Analyze (python) (push) Waiting to run
Release / build-binaries (zip, opensre.exe, onefile, windows-latest, windows-x64) (push) Blocked by required conditions
Release / publish-release (push) Blocked by required conditions
Release / publish-main-release (push) Blocked by required conditions
Release / prepare (push) Waiting to run
Release / verify (push) Blocked by required conditions
Release / build-python-dist (push) Blocked by required conditions
Release / build-binaries (tar.gz, opensre, onedir, macos-15-intel, darwin-x64) (push) Blocked by required conditions
Release / build-binaries (tar.gz, opensre, onedir, macos-latest, darwin-arm64) (push) Blocked by required conditions
Release / build-binaries (tar.gz, opensre, onedir, ubuntu-22.04, linux-x64) (push) Blocked by required conditions
Release / build-binaries (tar.gz, opensre, onedir, ubuntu-22.04-arm, linux-arm64) (push) Blocked by required conditions
Synthetic Deterministic Tests / Synthetic offline (deterministic) (push) Waiting to run
Interactive Shell Live (PR + post-merge) / turn-checks (no-LLM) (push) Waiting to run
Interactive Shell Live (PR + post-merge) / turn-live shard ${{ matrix.shard_index }} (push) Waiting to run
CI (OpenClaw E2E) / openclaw test (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:10:45 +08:00

83 lines
3.1 KiB
Python

"""Regression tests for the frozen-binary tiktoken plugin discovery bootstrap.
``litellm`` imports ``tiktoken`` at module load time and calls
``tiktoken.get_encoding("cl100k_base")`` immediately. tiktoken discovers that
encoding by walking the ``tiktoken_ext`` namespace package with
``pkgutil.iter_modules``, which only sees loose files on disk and finds
nothing in a PyInstaller frozen build -- crashing with ``ValueError: Unknown
encoding cl100k_base`` (``Plugins found: []``, see issue #3631). These tests
guard the bootstrap that bypasses the broken directory walk in frozen builds.
"""
from __future__ import annotations
import sys
from pathlib import Path
import pytest
import tiktoken.registry as registry
from core.llm.transports.litellm.frozen_tiktoken_bootstrap import (
ensure_tiktoken_encodings_discoverable,
)
_REPO_ROOT = Path(__file__).resolve().parents[2]
_RELEASE_WORKFLOW = _REPO_ROOT / ".github" / "workflows" / "release.yml"
@pytest.fixture(autouse=True)
def _reset_tiktoken_registry(monkeypatch: pytest.MonkeyPatch) -> None:
"""Isolate each test from tiktoken's process-wide plugin/encoding caches."""
monkeypatch.setattr(registry, "ENCODING_CONSTRUCTORS", None)
monkeypatch.setattr(registry, "ENCODINGS", {})
def _simulate_broken_frozen_plugin_scan(monkeypatch: pytest.MonkeyPatch) -> None:
"""Reproduce the frozen-build failure: the namespace-package walk finds nothing."""
monkeypatch.setattr(registry, "_available_plugin_modules", lambda: ())
def test_non_frozen_build_leaves_discovery_untouched(monkeypatch: pytest.MonkeyPatch) -> None:
"""Outside a frozen build the bootstrap must be a no-op."""
monkeypatch.delattr(sys, "frozen", raising=False)
original = registry._available_plugin_modules
ensure_tiktoken_encodings_discoverable()
assert registry._available_plugin_modules is original
def test_frozen_build_without_bootstrap_reproduces_reported_crash(
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""Confirms the failure mode this bootstrap exists to fix (issue #3631)."""
monkeypatch.setattr(sys, "frozen", True, raising=False)
_simulate_broken_frozen_plugin_scan(monkeypatch)
with pytest.raises(ValueError, match="Unknown encoding cl100k_base"):
registry.get_encoding("cl100k_base")
def test_frozen_build_bootstrap_resolves_encoding(monkeypatch: pytest.MonkeyPatch) -> None:
"""With the bootstrap applied, the frozen build must resolve the encoding."""
monkeypatch.setattr(sys, "frozen", True, raising=False)
_simulate_broken_frozen_plugin_scan(monkeypatch)
ensure_tiktoken_encodings_discoverable()
encoding = registry.get_encoding("cl100k_base")
assert encoding.name == "cl100k_base"
def test_release_workflow_bundles_tiktoken_plugin() -> None:
"""The release build must hidden-import tiktoken's plugin module.
Ties the bootstrap's direct-import target to the PyInstaller build command
so renaming or dropping the hidden-import fails fast instead of only
surfacing as a release-time binary crash.
"""
workflow = _RELEASE_WORKFLOW.read_text(encoding="utf-8")
assert "tiktoken_ext.openai_public" in workflow
assert "tiktoken_ext" in workflow