Skip to content

Compress tool outputs, logs, files, and RAG chunks before they reach the LLM. 20% fewer tokens for coding agents, 60-95% fewer tokens for JSON, same answers. Library, proxy, MCP server.

fix(compression): use thread-local tree-sitter parsers in code handler (#893)

Ashish6cdb8462000d
diff --git a/headroom/compression/handlers/code_handler.py b/headroom/compression/handlers/code_handler.py
index 3de5a20cb..a15466e65 100644
--- a/headroom/compression/handlers/code_handler.py
+++ b/headroom/compression/handlers/code_handler.py
@@ -29,8 +29,7 @@ logger = logging.getLogger(__name__)
# Lazy-loaded tree-sitter
_tree_sitter_available: bool | None = None
-_tree_sitter_parsers: dict[str, Any] = {}
-_tree_sitter_lock = threading.Lock()
+_tree_sitter_local = threading.local()
def _check_tree_sitter() -> bool:
@@ -47,19 +46,28 @@ def _check_tree_sitter() -> bool:
def _get_parser(language: str) -> Any:
- """Get tree-sitter parser for language."""
- global _tree_sitter_parsers
+ """Return a **thread-local** tree-sitter parser for ``language``.
+ tree-sitter ``Parser`` objects are pyo3 ``unsendable`` — touching
+ one from a thread other than its creator panics. Handlers run on
+ executor pool threads in the proxy, so parsers must never be shared
+ across threads. One parser per (thread, language); same fix as
+ ``transforms/code_compressor.py`` (#604).
+ """
if not _check_tree_sitter():
raise ImportError("tree-sitter-language-pack not installed")
- with _tree_sitter_lock:
- if language not in _tree_sitter_parsers:
- from tree_sitter_language_pack import get_parser
+ cache: dict[str, Any] | None = getattr(_tree_sitter_local, "parsers", None)
+ if cache is None:
+ cache = {}
+ _tree_sitter_local.parsers = cache
+
+ if language not in cache:
+ from tree_sitter_language_pack import get_parser
- _tree_sitter_parsers[language] = get_parser(language) # type: ignore[arg-type]
+ cache[language] = get_parser(language) # type: ignore[arg-type]
- return _tree_sitter_parsers[language]
+ return cache[language]
# tree-sitter API compatibility. tree-sitter-language-pack switched to a
diff --git a/tests/test_compression/test_code_handler.py b/tests/test_compression/test_code_handler.py
index 50fb3939c..4cebc697e 100644
--- a/tests/test_compression/test_code_handler.py
+++ b/tests/test_compression/test_code_handler.py
@@ -229,6 +229,31 @@ class TestTreeSitterContainers:
result.mask.mask[i] for i in range(start, start + len("let body_line = 5;"))
), "impl method body must be compressible"
+ def test_concurrent_parsing_uses_tree_sitter(self, handler):
+ """Parsers must be thread-local.
+
+ Regression: parsers were cached in a process-global dict and
+ shared across threads. tree-sitter Parser objects are pyo3
+ unsendable — touching one from a non-creator thread panics (or
+ raises, dropping the handler to the regex fallback). Parsing
+ from a thread pool must succeed on the tree-sitter path in
+ every thread.
+ """
+ from concurrent.futures import ThreadPoolExecutor
+
+ code = "class Foo:\n def m(self):\n x = 1\n return x\n"
+
+ def work(_: int) -> str:
+ result = handler.get_mask(code, language="python")
+ return str(result.metadata["parser"])
+
+ with ThreadPoolExecutor(max_workers=4) as pool:
+ parsers = list(pool.map(work, range(16)))
+
+ assert parsers == ["tree-sitter"] * 16, (
+ f"all threads must parse via tree-sitter, got: {set(parsers)}"
+ )
+
def test_non_ascii_content_mask_alignment(self, handler):
"""Byte offsets must be converted to char offsets.