Skip to content

Flaky test pollution: local SecretSource subclasses in test_acp_agent.py break test_state_serialization under xdist #3487

Closed

Description

@VascoSch92

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_deserialization
  • test_conversation_state_secrets_with_cipher
  • test_conversation_state_save_with_cipher_load_without
  • test_conversation_state_save_without_cipher_load_with
  • test_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
    (in test_registry_lookup_secret_injected_into_subprocess_env)
  • _CountingLookupSecret — test_acp_agent.py:5869
    (in test_acp_env_shadow_skips_registry_lookup)
  • _BrokenSecret — test_acp_agent.py:5934
    (in test_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

  1. added
    bug
    on Jun 3, 2026
  2. VascoSch92 commented on Jun 3, 2026

    @VascoSch92
    MemberAuthor

    @OpenHands Open a draft PR to solve this issue. Just do the minimal solution to make the tests not flasky anymore.

  3. self-assigned this
    on Jun 3, 2026
  4. openhands-ai commented on Jun 3, 2026

    @openhands-ai

    I'm on it! VascoSch92 can track my progress at all-hands.dev

  5. openhands-ai commented on Jun 3, 2026

    @openhands-ai

    Summary

    Opened draft PR #3489 (#3489) implementing the minimal fix for issue #3487.

    Checklist against the request

    • ✅ Draft PR opened as explicitly requested by @VascoSch92 in the key comment.
    • ✅ Minimal solution — only a test-file change; no production code touched, no defensive autouse fixture added.
    • ✅ Fixes the flakiness — hoisted _FakeLookupSecret, _CountingLookupSecret, and _BrokenSecret from inside test methods to module level in tests/sdk/agent/test_acp_agent.py, giving them stable (non-<locals>) qualnames so they no longer poison SecretSource.__subclasses__() for later ConversationState deserialization.
    • ✅ Verified before/after: reproduced 5 failures on main in the combined run; with the fix, 40 passed in that single-process run and 338 passed under pytest -n auto.
    • ✅ Lint/typecheck clean (uv run pre-commit run --files tests/sdk/agent/test_acp_agent.py).
    • ✅ Branch convention: openhands/fix-3487-local-secretsource-pollution.
    • ✅ PR conventions: links the issue with "Fixes Flaky test pollution: local SecretSource subclasses in test_acp_agent.py break test_state_serialization under xdist #3487", co-authored-by trailer, AI-disclosure line.

    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 PydanticField for the one class that uses default_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.

  6. VascoSch92 commented on Jun 4, 2026

    @VascoSch92
    MemberAuthor

    This is fixed in #3490

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

Metadata

Metadata

Assignees

Labels

bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions