mirror of
https://github.com/headroomlabs-ai/headroom.git
synced 2026-08-27 14:17:10 -04:00
## Summary This PR implements transparent `headroom wrap opencode` support without asking users to edit OpenCode provider URLs, choose an extra CLI flag, or maintain a static provider list. The wrapper now lives at the runtime transport boundary: OpenCode keeps its user/provider config, while Headroom intercepts outbound provider traffic in-process and routes it through the local Headroom proxy. ## What changed ### Transparent OpenCode wrapping - `headroom wrap opencode` injects the `headroom-opencode` plugin through `OPENCODE_CONFIG_CONTENT`. - Existing OpenCode provider URLs are preserved. We do not rewrite user config URLs to point at Headroom. - Existing `OPENAI_BASE_URL` and `ANTHROPIC_BASE_URL` env vars are preserved. - Local OpenCode traffic, localhost traffic, and Headroom proxy traffic bypass the shim to avoid loops. ### Runtime transport interception - Added an OpenCode plugin transport shim that wraps: - `globalThis.fetch` - `http.request` / `http.get` - `https.request` / `https.get` - External provider calls are routed to the local Headroom proxy. - The original upstream origin is passed through `x-headroom-base-url`, so the proxy can forward to the real provider without changing OpenCode config. - External `http2.connect` is blocked loudly instead of allowing direct provider traffic to leak outside Headroom. ### Live provider additions Provider coverage is no longer based on a static config scan. Because routing happens at outbound request time, providers added mid-session are routed through Headroom automatically as long as they use the covered Node transport paths. ### Subagent and child-process coverage - The parent OpenCode plugin sets a packaged Node preload shim through `NODE_OPTIONS=--import=.../hook-shim/handler.js`. - The transport shim patches `child_process.spawn`, `exec`, `execFile`, and `fork` so child Node processes receive the Headroom preload even when OpenCode passes a custom `env`. - The child-process shim fails closed if it loads without `HEADROOM_OPENCODE_TRANSPORT_PROXY_URL`. - This closes the subagent leak path where a child Node process could otherwise start without Headroom transport interception. ## Why this goes beyond PR #1089 PR #1089 improves OpenCode provider registration, but it still focuses on provider config shape. This PR moves the enforcement boundary to runtime transport interception. This PR goes further because: - No provider URL rewriting is required. - New providers added mid-session are covered automatically. - Subagents and child Node processes inherit the Headroom transport shim. - Direct external HTTP/2 paths fail loudly instead of leaking. - The wrap remains transparent to the user's OpenCode provider config. - The wrapper is fail-closed for unsupported child-process preload state. ## Additional robustness fixes While validating the change in Docker, the full Python suite exposed unrelated Linux/container robustness issues. These are fixed in this PR so the suite is green: - Binary cache handling now treats cache paths under a non-writable existing parent as unavailable, including when tests run as root in Docker. - `release_version.py` honors `MANUAL_VER` before git calls so direct script execution works outside a `.git` checkout. - Test logger isolation now resets relevant Headroom child loggers so proxy logging setup cannot poison later `caplog` tests. - The scanner missing-path test now uses a guaranteed missing `tmp_path` child instead of relying on `/nonexistent/path`. ## Validation All implementation validation was run inside Docker. - Full Python suite from a fresh Docker copy: `6605 passed, 523 skipped`. - Ruff on changed Python/OpenCode paths: passed. - OpenCode plugin typecheck: passed. - OpenCode plugin tests: `9 passed`. - OpenCode plugin build: passed. - Hook shim preload smoke test: passed. ## Notes This PR intentionally does not add a CLI option. `headroom wrap opencode` means full wrap. Either Headroom wraps OpenCode transparently, or the path fails loudly instead of silently leaking provider traffic. --------- Co-authored-by: Rudimar Ronsoni <6081613+rudironsoni@users.noreply.github.com>
274 lines
7.9 KiB
Python
274 lines
7.9 KiB
Python
"""Shared pytest fixtures for Headroom tests."""
|
|
|
|
# CRITICAL: Must be set before ANY imports that could trigger sentence_transformers
|
|
# The Rust tokenizers use parallelism that deadlocks with pytest-asyncio
|
|
import os
|
|
|
|
os.environ["TOKENIZERS_PARALLELISM"] = "false"
|
|
|
|
import json
|
|
import tempfile
|
|
from datetime import datetime
|
|
from pathlib import Path
|
|
from unittest.mock import Mock
|
|
|
|
import pytest
|
|
|
|
# Import httpx for timeout handling (will be available since it's a dependency)
|
|
try:
|
|
import httpx
|
|
|
|
HTTPX_AVAILABLE = True
|
|
except ImportError:
|
|
HTTPX_AVAILABLE = False
|
|
|
|
|
|
# =============================================================================
|
|
# Global test hooks
|
|
# =============================================================================
|
|
|
|
|
|
@pytest.hookimpl(hookwrapper=True)
|
|
def pytest_runtest_call(item):
|
|
"""Wrap test execution to catch httpx.ReadTimeout and skip instead of fail.
|
|
|
|
This handles flaky network timeouts that occur when:
|
|
- HuggingFace Hub is slow during model downloads (sentence-transformers)
|
|
- External embedding APIs timeout
|
|
- Network connectivity issues in CI
|
|
"""
|
|
outcome = yield
|
|
|
|
if HTTPX_AVAILABLE and outcome.excinfo is not None:
|
|
exc_type, exc_value, exc_tb = outcome.excinfo
|
|
if isinstance(exc_value, httpx.ReadTimeout):
|
|
pytest.skip("Skipped due to network timeout (flaky CI)")
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def _reset_headroom_logger_propagation():
|
|
"""Keep `headroom.*` log records flowing to pytest's caplog handler.
|
|
|
|
`headroom.proxy.helpers._setup_file_logging` sets
|
|
``logging.getLogger("headroom").propagate = False`` once any test
|
|
triggers a proxy startup with `--log-file`. After that, every
|
|
subsequent test's `caplog` fixture stops capturing `headroom.*`
|
|
log records (caplog attaches to root, propagation is now blocked
|
|
at the headroom-logger boundary). Reset before every test so the
|
|
capture is deterministic regardless of run order.
|
|
"""
|
|
import logging as _logging
|
|
|
|
for logger_name in (
|
|
"headroom",
|
|
"headroom.proxy",
|
|
"headroom.proxy.forwarded_headers",
|
|
"headroom.transforms",
|
|
"headroom.transforms.kompress_compressor",
|
|
):
|
|
logger = _logging.getLogger(logger_name)
|
|
logger.disabled = False
|
|
logger.propagate = True
|
|
yield
|
|
|
|
|
|
# =============================================================================
|
|
# Sample messages fixtures
|
|
# =============================================================================
|
|
|
|
|
|
# Sample messages fixtures
|
|
@pytest.fixture
|
|
def sample_messages():
|
|
"""Basic conversation messages."""
|
|
return [
|
|
{"role": "system", "content": "You are a helpful assistant."},
|
|
{"role": "user", "content": "Hello, how are you?"},
|
|
{"role": "assistant", "content": "I'm doing well, thank you!"},
|
|
]
|
|
|
|
|
|
@pytest.fixture
|
|
def sample_messages_with_tools():
|
|
"""Conversation with tool calls and responses."""
|
|
return [
|
|
{"role": "system", "content": "You are a helpful assistant with tools."},
|
|
{"role": "user", "content": "Search for user 12345"},
|
|
{
|
|
"role": "assistant",
|
|
"content": None,
|
|
"tool_calls": [
|
|
{
|
|
"id": "call_123",
|
|
"type": "function",
|
|
"function": {"name": "search_user", "arguments": '{"user_id": "12345"}'},
|
|
}
|
|
],
|
|
},
|
|
{
|
|
"role": "tool",
|
|
"tool_call_id": "call_123",
|
|
"content": '{"id": "12345", "name": "Alice", "email": "alice@example.com"}',
|
|
},
|
|
{"role": "assistant", "content": "I found user Alice with ID 12345."},
|
|
]
|
|
|
|
|
|
@pytest.fixture
|
|
def sample_tool_output_large():
|
|
"""Large tool output for compression testing (100 items)."""
|
|
return json.dumps(
|
|
[
|
|
{
|
|
"id": i,
|
|
"name": f"Item {i}",
|
|
"score": i * 0.1,
|
|
"status": "active" if i % 2 == 0 else "inactive",
|
|
}
|
|
for i in range(100)
|
|
]
|
|
)
|
|
|
|
|
|
@pytest.fixture
|
|
def sample_tool_output_with_errors():
|
|
"""Tool output containing error items."""
|
|
items = [{"id": i, "status": "success"} for i in range(20)]
|
|
items[5] = {"id": 5, "status": "error", "message": "Connection refused"}
|
|
items[15] = {"id": 15, "status": "failed", "exception": "TimeoutError"}
|
|
return json.dumps(items)
|
|
|
|
|
|
@pytest.fixture
|
|
def sample_system_prompt_with_date():
|
|
"""System prompt containing dynamic date."""
|
|
return "You are a helpful assistant. Current date: 2025-01-06. Help the user with their tasks."
|
|
|
|
|
|
@pytest.fixture
|
|
def sample_anthropic_messages():
|
|
"""Anthropic-style messages with content blocks."""
|
|
return [
|
|
{
|
|
"role": "user",
|
|
"content": [
|
|
{"type": "text", "text": "Analyze this image"},
|
|
{
|
|
"type": "image",
|
|
"source": {"type": "base64", "media_type": "image/png", "data": "..."},
|
|
},
|
|
],
|
|
}
|
|
]
|
|
|
|
|
|
# Mock client fixtures
|
|
@pytest.fixture
|
|
def mock_openai_response():
|
|
"""Mock OpenAI API response."""
|
|
mock = Mock()
|
|
mock.id = "chatcmpl-123"
|
|
mock.model = "gpt-4o"
|
|
mock.usage = Mock()
|
|
mock.usage.prompt_tokens = 100
|
|
mock.usage.completion_tokens = 50
|
|
mock.usage.total_tokens = 150
|
|
mock.choices = [Mock()]
|
|
mock.choices[0].message = Mock()
|
|
mock.choices[0].message.content = "This is a response."
|
|
mock.choices[0].message.role = "assistant"
|
|
mock.choices[0].finish_reason = "stop"
|
|
return mock
|
|
|
|
|
|
@pytest.fixture
|
|
def mock_openai_client(mock_openai_response):
|
|
"""Mock OpenAI client."""
|
|
client = Mock()
|
|
client.chat = Mock()
|
|
client.chat.completions = Mock()
|
|
client.chat.completions.create = Mock(return_value=mock_openai_response)
|
|
return client
|
|
|
|
|
|
# Storage fixtures
|
|
@pytest.fixture
|
|
def temp_sqlite_db():
|
|
"""Temporary SQLite database path."""
|
|
with tempfile.NamedTemporaryFile(suffix=".db", delete=False) as f:
|
|
yield f.name
|
|
Path(f.name).unlink(missing_ok=True)
|
|
|
|
|
|
@pytest.fixture
|
|
def temp_jsonl_file():
|
|
"""Temporary JSONL file path."""
|
|
with tempfile.NamedTemporaryFile(suffix=".jsonl", delete=False) as f:
|
|
yield f.name
|
|
Path(f.name).unlink(missing_ok=True)
|
|
|
|
|
|
# Provider fixtures
|
|
@pytest.fixture
|
|
def openai_provider():
|
|
"""OpenAI provider instance."""
|
|
from headroom.providers.openai import OpenAIProvider
|
|
|
|
return OpenAIProvider()
|
|
|
|
|
|
@pytest.fixture
|
|
def openai_tokenizer():
|
|
"""OpenAI token counter for gpt-4o."""
|
|
from headroom.providers.openai import OpenAITokenCounter
|
|
|
|
return OpenAITokenCounter("gpt-4o")
|
|
|
|
|
|
# Config fixtures
|
|
@pytest.fixture
|
|
def default_config():
|
|
"""Default HeadroomConfig."""
|
|
from headroom.config import HeadroomConfig
|
|
|
|
return HeadroomConfig()
|
|
|
|
|
|
@pytest.fixture
|
|
def smart_crusher_config():
|
|
"""SmartCrusher config for testing."""
|
|
from headroom.config import SmartCrusherConfig
|
|
|
|
return SmartCrusherConfig(
|
|
enabled=True,
|
|
min_items_to_analyze=3,
|
|
min_tokens_to_crush=0, # Always crush for tests
|
|
max_items_after_crush=10,
|
|
)
|
|
|
|
|
|
# Helper for creating RequestMetrics
|
|
@pytest.fixture
|
|
def sample_request_metrics():
|
|
"""Sample RequestMetrics for storage tests."""
|
|
from headroom.config import RequestMetrics
|
|
|
|
return RequestMetrics(
|
|
request_id="test-123",
|
|
timestamp=datetime(2025, 1, 6, 12, 0, 0),
|
|
model="gpt-4o",
|
|
stream=False,
|
|
mode="audit",
|
|
tokens_input_before=1000,
|
|
tokens_input_after=800,
|
|
tokens_output=200,
|
|
block_breakdown={"system": 100, "user": 200, "assistant": 500},
|
|
waste_signals={"json_bloat": 50},
|
|
stable_prefix_hash="abc123",
|
|
cache_alignment_score=85.0,
|
|
cached_tokens=100,
|
|
transforms_applied=["CacheAligner", "SmartCrusher"],
|
|
tool_units_dropped=1,
|
|
turns_dropped=0,
|
|
messages_hash="def456",
|
|
)
|