Repository navigation
test: stop ambient LMNR env vars deciding what the tracing tests measure - #4390
Conversation
Both files behaved differently depending on whether LMNR_* env vars happened to be set — the normal state anywhere tracing is configured, including CI. test_tool_span_input skipped wholesale once lmnr was already initialized, and a skipped tracing test is indistinguishable from a passing one. It now borrows the existing span processor and restores it, which also keeps test spans off whatever real endpoint that tracer was pointed at, and only builds its own when none exists. test_agent_step_tool_spans_nest_under_dispatcher_without_lmnr failed instead: agent.step carries the lazy @observe wrapper, which builds a real lmnr span once observability is enabled anywhere in the process, interposing between the test's parent span and the tool spans. Patching should_enable_observability is not enough because that wrapper is cached on first use, so the test now runs the undecorated step and measures the configuration its name claims. Verified: tests/sdk/agent + tests/sdk/observability give 856 passed with a clean environment and 856 passed with LMNR_PROJECT_API_KEY set, where they previously gave 1 failed / 1 skipped. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Python API breakage checks — ✅ PASSEDResult: ✅ PASSED |
REST API breakage checks (OpenAPI) — ✅ PASSEDResult: ✅ PASSED |
|
🤖 OpenHands is reviewing this PR. Head commit: This comment was posted by an AI agent (OpenHands). |
|
✅ Review complete. This review was performed through OpenHands Cloud Automation. You can log in and view the conversation here. |
all-hands-bot
left a comment
There was a problem hiding this comment.
🟢 Taste Rating: Good taste
This is a focused test-only fix for a real source of nondeterminism: ambient LMNR_* state was changing what the tracing tests measured. The changes keep the tests exercising the intended code paths without touching production behavior.
Verification run locally:
uv run pytest tests/sdk/agent/test_tool_span_input.py tests/sdk/agent/test_parallel_tool_span_context.py -q→ 12 passedLMNR_PROJECT_API_KEY=fake-key uv run pytest tests/sdk/agent/test_tool_span_input.py tests/sdk/agent/test_parallel_tool_span_context.py -q→ 12 passed
[RISK ASSESSMENT]
- [Overall PR]
⚠️ Risk Assessment: 🟢 LOW
Test-only change in tracing regression coverage. No production code, API surface, dependency, or runtime behavior changes; the main risk is test-fixture interaction with process-wide Laminar state, which is exactly what the focused tests now cover.
VERDICT:
✅ Worth merging: The regression tests now measure the intended behavior independent of ambient LMNR environment state.
KEY INSIGHT:
The fix removes environment-dependent special cases from the tests rather than weakening or skipping the assertions.
This review was generated by an AI agent (OpenHands) on behalf of the user through OpenHands Automation. View conversation
all-hands-bot
left a comment
There was a problem hiding this comment.
This review was created by an AI agent (OpenHands) on behalf of the repository maintainers.
Overall verdict: APPROVE — no material issues
Risk: LOW. Test-only change; no production code is touched, so there is no runtime behaviour change.
The PR fixes a genuine test-reliability defect: two tracing tests behaved differently depending on whether LMNR_* env vars were set in the ambient environment (as they are in CI). Both fixes are correct and well-reasoned.
test_parallel_tool_span_context.py — inspect.unwrap(Agent.step)
Correct. Agent.step carries the lazy @observe decorator. Once observability is enabled anywhere in the process, the wrapper builds and caches its lmnr-backed form (if wrapped is not None), so a later patch("…should_enable_observability") is never consulted again. Removing the decorator via inspect.unwrap is the right fix — it guarantees the without_lmnr test measures what its name claims regardless of ambient state. The PR description's note about __wrapped__ being absent from FunctionType stubs (hence pyright rejection) is accurate; inspect.unwrap is the documented API.
test_tool_span_input.py — borrow-and-swap fixture
The redesigned fixture correctly replaces the pytest.skip with a borrow-or-build strategy. Swapping processor.instance (the inner BatchSpanProcessor/SimpleSpanProcessor) rather than the LaminarSpanProcessor itself is exactly the right level: on_start/on_end still run the Laminar path/attribute logic, while export is redirected to the in-memory exporter. The assert isinstance(processor, LaminarSpanProcessor) is a good guard that fails loudly if lmnr internals change. The restoration in finally is unconditional and correct, and the not-borrowed teardown path matches the original fixture.
One minor observation (non-blocking)
In the exported() fixture, the borrowed flag tracks TracerWrapper.verify_initialized(), but the Laminar.initialize() / Laminar.shutdown() teardown is gated on not borrowed rather than on whether this fixture actually called Laminar.initialize(). In the theoretical case where a TracerWrapper exists but Laminar.is_initialized() is False (e.g. another test constructed TracerWrapper directly without going through Laminar.initialize), the fixture calls Laminar.initialize(...) but never calls Laminar.shutdown() in finally, leaving Laminar in an initialized state for later tests. This state is unlikely in practice (the only direct TracerWrapper construction in the suite is the lmnr_initialized fixture, which tears it down), and the leak is benign (later tests already tolerate ambient initialized state), so it does not block merge. If you want to close the gap, track whether this fixture called Laminar.initialize() separately from the borrowed flag.
No security concerns: the fixture uses a fake project_api_key and redirects spans to an in-memory exporter, so no test spans reach a real endpoint.
| if hasattr(TracerWrapper, "instance"): | ||
| del TracerWrapper.instance | ||
| processor.instance = previous | ||
| if not borrowed: |
There was a problem hiding this comment.
Minor: the teardown condition if not borrowed gates Laminar.shutdown() on whether TracerWrapper was pre-existing, but the Laminar.initialize() call at line 100 is gated on Laminar.is_initialized() — a separate check. In the edge case where TracerWrapper exists but Laminar was not initialized, this fixture calls Laminar.initialize() yet never shuts it down (since borrowed=True). Unlikely in practice and benign if it occurs, but tracking whether this fixture called Laminar.initialize() separately from borrowed would close the gap.
HUMAN:
Fix Laminar tests: ambient LMNR_* state was changing what the tracing tests measured.
AGENT:
Why
Two tracing test files on
mainbehave differently depending on whetherLMNR_*env vars happen to be set — the normal state anywhere tracing is configured,
including CI and the automation environment. Neither failure is visible in a
default local run:
test_tool_span_input.pyskipped wholesale. Its fixture bailed out withpytest.skipwhenever lmnr was already initialized. A skipped tracing test isindistinguishable from a passing one in a CI summary, so the assertion it exists to
make simply stopped happening in exactly the environments that have tracing on.
test_parallel_tool_span_context.py::test_agent_step_tool_spans_nest_under_dispatcher_without_lmnrfailed.
Agent.stepcarries the lazy@observewrapper. Once observability isenabled anywhere in the process, that wrapper builds a real lmnr span, which
interposes between the test's own parent span and the tool spans — so the
trace_idassertion compares against the wrong ancestor:The test's name says
without_lmnr, but whether it actually measured that wasdecided by the ambient environment.
Found while addressing a review on #4376, which flagged the same class of defect in
that PR's tests.
Summary
test_tool_span_input.py: the fixture no longer skips. When lmnr is alreadyup it borrows the existing
LaminarSpanProcessor, swaps in anInMemorySpanExporter, and restores it infinally— which also keeps test spansoff whatever real endpoint that tracer was configured with. Only when nothing is
initialized does it build (and tear down) its own, with the exporter installed
before
Laminar.initializeso no OTLP exporter is ever constructed.test_parallel_tool_span_context.py: thewithout_lmnrend-to-end test runsinspect.unwrap(Agent.step), so the decorator cannot interpose regardless ofambient env.
Patching
should_enable_observabilityis not sufficient for the second one: the@observewrapper caches its built form on first use (if wrapped is not None), soonce any earlier test in the process has triggered the build, the check is never
consulted again. That is why the fix removes the decorator rather than the flag —
verified by trying the flag first and watching the full-suite run still fail while
the single-test run passed.
Issue Number
None. Follow-up to review feedback on #4376; the two files were introduced by #4360
and #4379.
How to Test
LMNR_PROJECT_API_KEYsetAlso
uv run ruff check/ruff format --checkclean anduv run pyright0 errors onboth files.
Video/Screenshots
Not applicable — test-only change; the pass/skip/fail counts above are the observable
behaviour.
Type
Notes
Test-only. No production code is touched, so there is no runtime behaviour change.
Why
inspect.unwraprather thanAgent.step.__wrapped__. Both work at runtime,but
__wrapped__is not in the type stubs forFunctionTypeand pyright rejects it;inspect.unwrapis the documented API and states the intent more plainly.The remaining asymmetry is deliberate. The
*_with_lmnrtests intest_parallel_tool_span_context.pystill assert lmnr's threading instrumentation islive via
_assert_lmnr_wraps_submit(), so they fail loudly rather than degrade ifthat ever stops being true. Only the
without_lmnrcase needed pinning.Agent Server images for this PR
• GHCR package: https://github.com/OpenHands/agent-sdk/pkgs/container/agent-server
Variants & Base Images
eclipse-temurin:17-jdknikolaik/python-nodejs:python3.13-nodejs22-slimgolang:1.21-bookwormPull (multi-arch manifest)
# Each variant is a multi-arch manifest supporting both amd64 and arm64 docker pull ghcr.io/openhands/agent-server:c090a80-pythonRun
All tags pushed for this build
About Multi-Architecture Support
c090a80-python) is a multi-arch manifest supporting both amd64 and arm64c090a80-python-amd64) are also available if needed