fix(runtime): resolve compact-turn context once at the chat route

The first checkpoint removed terminal-metrics discovery, but a normal
compact chat turn still ran two context systems: build_chat_context's
legacy untyped lookup (directly or inside maybe_compact) and the typed
resolver inside stream_preview.

Resolve the typed ContextResolution once, at the chat route, before
build_chat_context, using the session's provider credentials. The
predicate mirrors _clean_v3_preview; every input it needs is known at
that point and the native-workspace term cannot veto a requested clean
route. The same object then:

- sizes legacy history shaping in build_chat_context through a new
  maybe_compact(context_length=...) override, so no legacy probe runs;
  an unknown window still shapes with DEFAULT_CONTEXT but gains no
  provenance;
- crosses stream_agent_loop (one new parameter, forwarded only at the
  compact dispatch) into stream_preview, which reuses it and probes only
  for callers that arrive without one or with one bound to another
  route.

ContextResolution now records the endpoint and model it describes
(endpoint URL excluded from repr and metrics). The bare legacy
context_length is never converted into typed evidence.

Credential scoping: origins compare with default ports normalized, an
empty host is never trusted, and the probe client never follows
redirects. Tests cover the configured origin, the server-resolved
Tailscale form, scheme/port/lookalike/userinfo/path origins, redirects,
and secret-free errors, logs and metrics.

The conftest guard now replaces only the resolver's I/O edges (HTTP
client and DNS-capable URL building) instead of the whole probe, and
exposes a context_probe_ledger fixture, so route integration tests run
the real resolver offline and can count metadata requests.
This commit is contained in:
Alexandre Teixeira
2026-10-01 22:31:21 +01:00
parent 837fbfd0ea
commit 0054557027
9 changed files with 666 additions and 26 deletions
+40 -11
View File
@@ -205,32 +205,61 @@ def _no_leaked_module_stubs():
@pytest.fixture(autouse=True)
def _no_context_window_network_probe(request):
"""Keep turn preparation from probing real provider metadata in tests.
"""Keep the turn context-window resolver offline in tests.
The compact runtime resolves its context window before the first model
request. Tests that drive it with placeholder endpoints must not perform
DNS or HTTP lookups; modules that exercise the probe opt in with a
module-level ``CONTEXT_PROBE_NETWORK = True`` and supply their own client.
Compact turns resolve their window before the first model request, and
most tests drive them with placeholder endpoints. Only the resolver's two
I/O edges are replaced: URL resolution (DNS/Tailscale lookups) and its
HTTP client, which records each attempted metadata request and fails it
as a transport error. Everything else (route wiring, caching, credential
scoping, evidence selection) runs for real, so an unintended extra probe
stays visible through the ``context_probe_ledger`` fixture.
Modules that install their own fake client opt out with a module-level
``CONTEXT_PROBE_NETWORK = True``.
"""
ledger = []
if getattr(request.module, "CONTEXT_PROBE_NETWORK", False):
yield
yield ledger
return
try:
from src.agent_runtime import context_resolution
except Exception:
yield
yield ledger
return
async def _disabled_probe(endpoint_url, model, headers, is_local, observations, errors, timeout):
errors.append("probe_disabled_in_tests")
class _OfflineMetadataClient:
def __init__(self, timeout=None):
pass
async def __aenter__(self):
return self
async def __aexit__(self, *args):
return False
async def get(self, url, headers=None):
ledger.append({"url": url, "headers": dict(headers or {})})
raise context_resolution.httpx.ConnectError("network disabled in tests")
def _offline_provider_urls(endpoint_url):
base = endpoint_url.split("/v1")[0] if "/v1" in endpoint_url else endpoint_url.rstrip("/")
return base + "/v1/models", endpoint_url
# A private patcher keeps the shared ``monkeypatch`` fixture's teardown
# order unchanged for tests that check their own sys.modules hygiene.
patcher = pytest.MonkeyPatch()
patcher.setattr(context_resolution, "_probe", _disabled_probe)
patcher.setattr(context_resolution, "_http_client", _OfflineMetadataClient)
patcher.setattr(context_resolution, "_provider_urls", _offline_provider_urls)
context_resolution.clear_probe_cache()
try:
yield
yield ledger
finally:
patcher.undo()
context_resolution.clear_probe_cache()
@pytest.fixture
def context_probe_ledger(_no_context_window_network_probe):
"""Metadata requests the context resolver attempted during this test."""
return _no_context_window_network_probe