Repository navigation
Description
Summary
Five tests in tests/sdk/conversation/local/test_state_serialization.py fail
non-deterministically under pytest -n auto (xdist) with:
pydantic_core._pydantic_core.ValidationError: ... for ConversationState
secret_registry.secret_sources.API_KEY
Value error, Local classes not supported!
tests.sdk.agent.test_acp_agent._CountingLookupSecret / openhands.sdk.secret.secrets.SecretSource
(Since they may not exist at deserialization time)
The failure is cross-test pollution: the error originates from a class
defined in a different test file (test_acp_agent.py), not from the tests
that fail.
Affected tests
All in tests/sdk/conversation/local/test_state_serialization.py:
test_conversation_state_secrets_serialization_deserializationtest_conversation_state_secrets_with_ciphertest_conversation_state_save_with_cipher_load_withouttest_conversation_state_save_without_cipher_load_withtest_conversation_state_cipher_mismatch
Reproduction (deterministic)
Run the two files together in one process, ACP first:
uv run pytest tests/sdk/agent/test_acp_agent.py \
tests/sdk/conversation/local/test_state_serialization.py -q
# => 5 failed, 313 passed
Reproduces on main (independent of any open PR).
Root cause
SecretSource is a DiscriminatedUnionMixin that discovers its concrete
subclasses via Python's cls.__subclasses__(). test_acp_agent.py defines
SecretSource subclasses inside test methods (i.e. <locals> classes):
_FakeLookupSecret—test_acp_agent.py:5832
(intest_registry_lookup_secret_injected_into_subprocess_env)_CountingLookupSecret—test_acp_agent.py:5869
(intest_acp_env_shadow_skips_registry_lookup)_BrokenSecret—test_acp_agent.py:5934
(intest_failing_registry_lookup_swallowed)
These classes linger in the process after their test runs (held alive by the
pydantic schema / instance references), so SecretSource.__subclasses__()
keeps returning them. When ConversationState with secrets is later
deserialized in the same xdist worker,
_get_checked_concrete_subclasses (openhands-sdk/openhands/sdk/utils/models.py:159)
rejects the leaked <locals> class:
if "<locals>" in sub.__qualname__:
raise ValueError("Local classes not supported! ...")The SDK intentionally forbids local subclasses of a discriminated union (they
may not exist at deserialization time) — so the test is violating a real
contract.
Why it's flaky
Under -n auto, it only fires when test_acp_agent.py and
test_state_serialization.py land on the same worker and the ACP tests run
first. The collision depends on worker count and load-based scheduling, so it
passes on some machines/runs and fails on others (e.g. it surfaced on a CI
runner but passed on a local -n auto run with a different core count).
Suggested fix
Hoist the three test-only SecretSource subclasses in test_acp_agent.py to
module level so they have stable qualnames and are no longer <locals>
(module-level subclasses pass the check and don't poison deserialization).
Optionally add a defensive autouse fixture that snapshots/restores the
discriminated-union registry (or calls clear_subclass_cache()) for tests that
must define ad-hoc subclasses.
Notes
Discovered while working on #3486 (an unrelated switch_llm deadlock fix): that
PR added/removed tests, which nudged xdist's distribution enough to surface this
latent collision. The two are independent — this reproduces on main.
CI example:
https://github.com/OpenHands/software-agent-sdk/actions/runs/26897685300/job/79341165449
Activity
VascoSch92 commentedon Jun 3, 2026
@OpenHands Open a draft PR to solve this issue. Just do the minimal solution to make the tests not flasky anymore.
openhands-ai commentedon Jun 3, 2026
I'm on it! VascoSch92 can track my progress at all-hands.dev
openhands-ai commentedon Jun 3, 2026
Summary
Opened draft PR #3489 (#3489) implementing the minimal fix for issue #3487.
Checklist against the request
_FakeLookupSecret,_CountingLookupSecret, and_BrokenSecretfrom inside test methods to module level intests/sdk/agent/test_acp_agent.py, giving them stable (non-<locals>) qualnames so they no longer poisonSecretSource.__subclasses__()for laterConversationStatedeserialization.mainin the combined run; with the fix, 40 passed in that single-process run and 338 passed underpytest -n auto.uv run pre-commit run --files tests/sdk/agent/test_acp_agent.py).openhands/fix-3487-local-secretsource-pollution.Conciseness
The diff is tight: 33 insertions / 27 deletions in a single file. The three classes are defined once at module level, the inline
imports and inline class definitions are removed from the three test methods, and two new module-level imports are added (SecretSource;Field as PydanticFieldfor the one class that usesdefault_factory). A short comment block above the hoisted classes explains why they must stay at module level, citing the discriminated-union check and the issue number — so the constraint won't get re-violated by a future contributor. No extraneous changes; nothing to revert.VascoSch92 commentedon Jun 4, 2026
This is fixed in #3490