Skip to content

test: stop ambient LMNR env vars deciding what the tracing tests measure - #4390

Merged
simonrosenberg merged 1 commit into
mainfrom
fix-without-lmnr-env-dep
Aug 6, 2026
Merged

simonrosenberg merged 1 commit into
mainfrom
fix-without-lmnr-env-dep

Conversation

@simonrosenberg

@simonrosenberg simonrosenberg commented Aug 6, 2026 •

Copy link
Copy Markdown
Member

HUMAN:

Fix Laminar tests: ambient LMNR_* state was changing what the tracing tests measured.


AGENT:

Why

Two tracing test files on main behave differently depending on whether LMNR_*
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:

                                          clean env    LMNR_PROJECT_API_KEY set
tests/sdk/agent + tests/sdk/observability  856 passed   1 failed, 854 passed, 1 skipped

test_tool_span_input.py skipped wholesale. Its fixture bailed out with
pytest.skip whenever lmnr was already initialized. A skipped tracing test is
indistinguishable 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_lmnr
failed.
Agent.step carries the lazy @observe wrapper. Once observability is
enabled 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_id assertion compares against the wrong ancestor:

E   assert 207883464541316575612700684987220977331 == 137876110781008933591910052811977992012

The test's name says without_lmnr, but whether it actually measured that was
decided 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 already
    up it borrows the existing LaminarSpanProcessor, swaps in an
    InMemorySpanExporter, and restores it in finally — which also keeps test spans
    off 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.initialize so no OTLP exporter is ever constructed.
  • test_parallel_tool_span_context.py: the without_lmnr end-to-end test runs
    inspect.unwrap(Agent.step), so the decorator cannot interpose regardless of
    ambient env.

Patching should_enable_observability is not sufficient for the second one: the
@observe wrapper caches its built form on first use (if wrapped is not None), so
once 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

uv run pytest tests/sdk/agent/ tests/sdk/observability/ -q
LMNR_PROJECT_API_KEY=fake-key uv run pytest tests/sdk/agent/ tests/sdk/observability/ -q
before after
clean environment 856 passed 856 passed
LMNR_PROJECT_API_KEY set 1 failed, 854 passed, 1 skipped 856 passed

Also uv run ruff check / ruff format --check clean and uv run pyright 0 errors on
both files.

Video/Screenshots

Not applicable — test-only change; the pass/skip/fail counts above are the observable
behaviour.

Type

  • Bug fix
  • Feature
  • Refactor
  • Breaking change
  • Docs / chore

Notes

Test-only. No production code is touched, so there is no runtime behaviour change.

Why inspect.unwrap rather than Agent.step.__wrapped__. Both work at runtime,
but __wrapped__ is not in the type stubs for FunctionType and pyright rejects it;
inspect.unwrap is the documented API and states the intent more plainly.

The remaining asymmetry is deliberate. The *_with_lmnr tests in
test_parallel_tool_span_context.py still assert lmnr's threading instrumentation is
live via _assert_lmnr_wraps_submit(), so they fail loudly rather than degrade if
that ever stops being true. Only the without_lmnr case needed pinning.


Agent Server images for this PR

• GHCR package: https://github.com/OpenHands/agent-sdk/pkgs/container/agent-server

Variants & Base Images

Variant Architectures Base Image Docs / Tags
java amd64, arm64 eclipse-temurin:17-jdk Link
python amd64, arm64 nikolaik/python-nodejs:python3.13-nodejs22-slim Link
golang amd64, arm64 golang:1.21-bookworm Link

Pull (multi-arch manifest)

# Each variant is a multi-arch manifest supporting both amd64 and arm64
docker pull ghcr.io/openhands/agent-server:c090a80-python

Run

docker run -it --rm \
  -p 8000:8000 \
  --name agent-server-c090a80-python \
  ghcr.io/openhands/agent-server:c090a80-python

All tags pushed for this build

ghcr.io/openhands/agent-server:c090a80-golang-amd64
ghcr.io/openhands/agent-server:c090a80c130e6a0a6cf25cd76df7086b234f8b60-golang-amd64
ghcr.io/openhands/agent-server:fix-without-lmnr-env-dep-golang-amd64
ghcr.io/openhands/agent-server:c090a80-golang_tag_1.21-bookworm-amd64
ghcr.io/openhands/agent-server:c090a80-golang-arm64
ghcr.io/openhands/agent-server:c090a80c130e6a0a6cf25cd76df7086b234f8b60-golang-arm64
ghcr.io/openhands/agent-server:fix-without-lmnr-env-dep-golang-arm64
ghcr.io/openhands/agent-server:c090a80-golang_tag_1.21-bookworm-arm64
ghcr.io/openhands/agent-server:c090a80-java-amd64
ghcr.io/openhands/agent-server:c090a80c130e6a0a6cf25cd76df7086b234f8b60-java-amd64
ghcr.io/openhands/agent-server:fix-without-lmnr-env-dep-java-amd64
ghcr.io/openhands/agent-server:c090a80-eclipse-temurin_tag_17-jdk-amd64
ghcr.io/openhands/agent-server:c090a80-java-arm64
ghcr.io/openhands/agent-server:c090a80c130e6a0a6cf25cd76df7086b234f8b60-java-arm64
ghcr.io/openhands/agent-server:fix-without-lmnr-env-dep-java-arm64
ghcr.io/openhands/agent-server:c090a80-eclipse-temurin_tag_17-jdk-arm64
ghcr.io/openhands/agent-server:c090a80-python-amd64
ghcr.io/openhands/agent-server:c090a80c130e6a0a6cf25cd76df7086b234f8b60-python-amd64
ghcr.io/openhands/agent-server:fix-without-lmnr-env-dep-python-amd64
ghcr.io/openhands/agent-server:c090a80-nikolaik_s_python-nodejs_tag_python3.13-nodejs22-slim-amd64
ghcr.io/openhands/agent-server:c090a80-python-arm64
ghcr.io/openhands/agent-server:c090a80c130e6a0a6cf25cd76df7086b234f8b60-python-arm64
ghcr.io/openhands/agent-server:fix-without-lmnr-env-dep-python-arm64
ghcr.io/openhands/agent-server:c090a80-nikolaik_s_python-nodejs_tag_python3.13-nodejs22-slim-arm64
ghcr.io/openhands/agent-server:c090a80-golang
ghcr.io/openhands/agent-server:c090a80c130e6a0a6cf25cd76df7086b234f8b60-golang
ghcr.io/openhands/agent-server:fix-without-lmnr-env-dep-golang
ghcr.io/openhands/agent-server:c090a80-golang_tag_1.21-bookworm
ghcr.io/openhands/agent-server:c090a80-java
ghcr.io/openhands/agent-server:c090a80c130e6a0a6cf25cd76df7086b234f8b60-java
ghcr.io/openhands/agent-server:fix-without-lmnr-env-dep-java
ghcr.io/openhands/agent-server:c090a80-eclipse-temurin_tag_17-jdk
ghcr.io/openhands/agent-server:c090a80-python
ghcr.io/openhands/agent-server:c090a80c130e6a0a6cf25cd76df7086b234f8b60-python
ghcr.io/openhands/agent-server:fix-without-lmnr-env-dep-python
ghcr.io/openhands/agent-server:c090a80-nikolaik_s_python-nodejs_tag_python3.13-nodejs22-slim

About Multi-Architecture Support

  • Each variant tag (e.g., c090a80-python) is a multi-arch manifest supporting both amd64 and arm64
  • Docker automatically pulls the correct architecture for your platform
  • Individual architecture tags (e.g., c090a80-python-amd64) are also available if needed

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>
@github-actions

github-actions Bot commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

Python API breakage checks — ✅ PASSED

Result: ✅ PASSED

Action log

@github-actions

github-actions Bot commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

REST API breakage checks (OpenAPI) — ✅ PASSED

Result: ✅ PASSED

Action log

@simonrosenberg simonrosenberg self-assigned this Aug 6, 2026
@simonrosenberg
simonrosenberg marked this pull request as ready for review August 6, 2026 10:24
@all-hands-bot

Copy link
Copy Markdown
Collaborator

🤖 OpenHands is reviewing this PR.

Head commit: c090a80c130e6a0a6cf25cd76df7086b234f8b60
View the conversation: https://oss-agent-canvas.ngrok.dev/conversations/7bae18e3-8055-4bd0-98d8-bf807fdbf29c

This comment was posted by an AI agent (OpenHands).

all-hands-bot commented Aug 6, 2026 •

Copy link
Copy Markdown
Collaborator

✅ Review complete.

This review was performed through OpenHands Cloud Automation. You can log in and view the conversation here.

@github-actions

github-actions Bot commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

Coverage

Coverage Report •
FileStmtsMissCoverMissing
TOTAL390021113371% 
report-only-changed-files is enabled. No files were changed during this commit :)

@all-hands-bot all-hands-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 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 passed
  • LMNR_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

@simonrosenberg
simonrosenberg merged commit 4e7d5b0 into main Aug 6, 2026
39 of 40 checks passed
@simonrosenberg
simonrosenberg deleted the fix-without-lmnr-env-dep branch August 6, 2026 10:31

@all-hands-bot all-hands-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants